Changes in uspace/srv/fs/fat/fat_fat.c [2f636b6:c91f2d1b] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat_fat.c
r2f636b6 rc91f2d1b 61 61 * @param dev_handle Device handle of the device with the file. 62 62 * @param firstc First cluster to start the walk with. 63 * @param lastc If non-NULL, output argument hodling the last cluster 64 * number visited. 65 * @param numc If non-NULL, output argument holding the number of 66 * clusters seen during the walk. 63 * @param lastc If non-NULL, output argument hodling the last cluster number visited. 67 64 * @param max_clusters Maximum number of clusters to visit. 68 65 * 69 * @return EOK on success or a negative error code.70 */ 71 int66 * @return Number of clusters seen during the walk. 67 */ 68 uint16_t 72 69 fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, 73 fat_cluster_t *lastc, uint16_t *numc, uint16_tmax_clusters)70 fat_cluster_t *lastc, uint16_t max_clusters) 74 71 { 75 72 block_t *b; … … 87 84 if (lastc) 88 85 *lastc = firstc; 89 if (numc) 90 *numc = 0; 91 return EOK; 86 return 0; 92 87 } 93 88 … … 103 98 /* read FAT1 */ 104 99 rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE); 105 if (rc != EOK) 106 return rc; 100 assert(rc == EOK); 107 101 clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); 108 102 assert(clst != FAT_CLST_BAD); 109 103 rc = block_put(b); 110 if (rc != EOK) 111 return rc; 104 assert(rc == EOK); 112 105 clusters++; 113 106 } … … 115 108 if (lastc && clst < FAT_CLST_LAST1) 116 109 *lastc = clst; 117 if (numc) 118 *numc = clusters; 119 120 return EOK; 110 111 return clusters; 121 112 } 122 113 123 114 /** Read block from file located on a FAT file system. 124 115 * 125 * @param block Pointer to a block pointer for storing result.126 116 * @param bs Buffer holding the boot sector of the file system. 127 117 * @param dev_handle Device handle of the file system. … … 131 121 * @param flags Flags passed to libblock. 132 122 * 133 * @return EOK on success or a negative error code. 134 */ 135 int 136 _fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle, 137 fat_cluster_t firstc, bn_t bn, int flags) 138 { 123 * @return Block structure holding the requested block. 124 */ 125 block_t * 126 _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, 127 bn_t bn, int flags) 128 { 129 block_t *b; 139 130 unsigned bps; 140 131 unsigned rscnt; /* block address of the first FAT */ … … 143 134 unsigned sf; 144 135 unsigned ssa; /* size of the system area */ 145 uint16_t clusters; 146 unsigned max_clusters; 136 unsigned clusters, max_clusters; 147 137 fat_cluster_t lastc; 148 138 int rc; … … 160 150 /* root directory special case */ 161 151 assert(bn < rds); 162 rc = block_get( block, dev_handle, rscnt + bs->fatcnt * sf + bn,152 rc = block_get(&b, dev_handle, rscnt + bs->fatcnt * sf + bn, 163 153 flags); 164 return rc; 154 assert(rc == EOK); 155 return b; 165 156 } 166 157 167 158 max_clusters = bn / bs->spc; 168 rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,159 clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc, 169 160 max_clusters); 170 if (rc != EOK)171 return rc;172 161 assert(clusters == max_clusters); 173 162 174 rc = block_get(block, dev_handle, 175 ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags); 176 177 return rc; 163 rc = block_get(&b, dev_handle, ssa + 164 (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags); 165 assert(rc == EOK); 166 167 return b; 178 168 } 179 169 … … 187 177 * this argument is ignored. 188 178 * @param pos Position in the last node block. 189 * 190 * @return EOK on success or a negative error code. 191 */ 192 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) 179 */ 180 void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) 193 181 { 194 182 uint16_t bps; … … 208 196 int flags = (o % bps == 0) ? 209 197 BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE; 210 rc = fat_block_get(&b, bs, nodep, o / bps, flags); 211 if (rc != EOK) 212 return rc; 198 b = fat_block_get(bs, nodep, o / bps, flags); 213 199 memset(b->data + o % bps, 0, bps - o % bps); 214 200 b->dirty = true; /* need to sync node */ 215 201 rc = block_put(b); 216 if (rc != EOK) 217 return rc; 202 assert(rc == EOK); 218 203 } 219 204 220 205 if (o >= pos) 221 return EOK;206 return; 222 207 223 208 /* zero out the initial part of the new cluster chain */ 224 209 for (o = boundary; o < pos; o += bps) { 225 rc = _fat_block_get(&b,bs, nodep->idx->dev_handle, mcl,210 b = _fat_block_get(bs, nodep->idx->dev_handle, mcl, 226 211 (o - boundary) / bps, BLOCK_FLAGS_NOREAD); 227 if (rc != EOK)228 return rc;229 212 memset(b->data, 0, min(bps, pos - o)); 230 213 b->dirty = true; /* need to sync node */ 231 214 rc = block_put(b); 232 if (rc != EOK) 233 return rc; 234 } 235 236 return EOK; 215 assert(rc == EOK); 216 } 237 217 } 238 218 … … 242 222 * @param dev_handle Device handle for the file system. 243 223 * @param clst Cluster which to get. 244 * @param value Output argument holding the value of the cluster. 245 * 246 * @return EOK or a negative error code. 247 */ 248 int 249 fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst, 250 fat_cluster_t *value) 224 * 225 * @return Value found in the cluster. 226 */ 227 fat_cluster_t 228 fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst) 251 229 { 252 230 block_t *b; 253 231 uint16_t bps; 254 232 uint16_t rscnt; 255 fat_cluster_t *cp ;233 fat_cluster_t *cp, value; 256 234 int rc; 257 235 … … 261 239 rc = block_get(&b, dev_handle, rscnt + 262 240 (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE); 263 if (rc != EOK) 264 return rc; 241 assert(rc == EOK); 265 242 cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); 266 *value = uint16_t_le2host(*cp);243 value = uint16_t_le2host(*cp); 267 244 rc = block_put(b); 268 269 return rc; 245 assert(rc == EOK); 246 247 return value; 270 248 } 271 249 … … 277 255 * @param clst Cluster which is to be set. 278 256 * @param value Value to set the cluster with. 279 * 280 * @return EOK on success or a negative error code. 281 */ 282 int 257 */ 258 void 283 259 fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, 284 260 fat_cluster_t clst, fat_cluster_t value) … … 298 274 rc = block_get(&b, dev_handle, rscnt + sf * fatno + 299 275 (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE); 300 if (rc != EOK) 301 return rc; 276 assert(rc == EOK); 302 277 cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); 303 278 *cp = host2uint16_t_le(value); 304 279 b->dirty = true; /* need to sync block */ 305 280 rc = block_put(b); 306 return rc;281 assert(rc == EOK); 307 282 } 308 283 … … 313 288 * @param lifo Chain of allocated clusters. 314 289 * @param nclsts Number of clusters in the lifo chain. 315 * 316 * @return EOK on success or a negative error code. 317 */ 318 int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, 290 */ 291 void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, 319 292 fat_cluster_t *lifo, unsigned nclsts) 320 293 { 321 294 uint8_t fatno; 322 295 unsigned c; 323 int rc;324 296 325 297 for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { 326 298 for (c = 0; c < nclsts; c++) { 327 rc =fat_set_cluster(bs, dev_handle, fatno, lifo[c],299 fat_set_cluster(bs, dev_handle, fatno, lifo[c], 328 300 c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); 329 if (rc != EOK)330 return rc;331 301 } 332 302 } 333 334 return EOK;335 303 } 336 304 … … 379 347 for (b = 0, cl = 0; b < sf; b++) { 380 348 rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE); 381 if (rc != EOK)382 goto error;383 349 for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { 384 350 fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; … … 396 362 /* we are almost done */ 397 363 rc = block_put(blk); 398 if (rc != EOK) 399 goto error; 364 assert(rc == EOK); 400 365 /* update the shadow copies of FAT */ 401 rc =fat_alloc_shadow_clusters(bs,366 fat_alloc_shadow_clusters(bs, 402 367 dev_handle, lifo, nclsts); 403 if (rc != EOK)404 goto error;405 368 *mcl = lifo[found - 1]; 406 369 *lcl = lifo[0]; … … 412 375 } 413 376 rc = block_put(blk); 414 if (rc != EOK) { 415 error: 416 fibril_mutex_unlock(&fat_alloc_lock); 417 free(lifo); 418 return rc; 419 } 377 assert(rc == EOK); 420 378 } 421 379 fibril_mutex_unlock(&fat_alloc_lock); … … 426 384 */ 427 385 while (found--) { 428 rc =fat_set_cluster(bs, dev_handle, FAT1, lifo[found],386 fat_set_cluster(bs, dev_handle, FAT1, lifo[found], 429 387 FAT_CLST_RES0); 430 if (rc != EOK) {431 free(lifo);432 return rc;433 }434 388 } 435 389 … … 443 397 * @param dev_handle Device handle of the file system. 444 398 * @param firstc First cluster in the chain which is to be freed. 445 * 446 * @return EOK on success or a negative return code. 447 */ 448 int 399 */ 400 void 449 401 fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc) 450 402 { 451 403 unsigned fatno; 452 404 fat_cluster_t nextc; 453 int rc;454 405 455 406 /* Mark all clusters in the chain as free in all copies of FAT. */ 456 407 while (firstc < FAT_CLST_LAST1) { 457 408 assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD); 458 rc = fat_get_cluster(bs, dev_handle, firstc, &nextc); 459 if (rc != EOK) 460 return rc; 461 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 462 rc = fat_set_cluster(bs, dev_handle, fatno, firstc, 409 nextc = fat_get_cluster(bs, dev_handle, firstc); 410 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) 411 fat_set_cluster(bs, dev_handle, fatno, firstc, 463 412 FAT_CLST_RES0); 464 if (rc != EOK)465 return rc;466 }467 468 413 firstc = nextc; 469 414 } 470 471 return EOK;472 415 } 473 416 … … 477 420 * @param nodep Node representing the file. 478 421 * @param mcl First cluster of the cluster chain to append. 479 * 480 * @return EOK on success or a negative error code. 481 */ 482 int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) 422 */ 423 void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) 483 424 { 484 425 dev_handle_t dev_handle = nodep->idx->dev_handle; 485 426 fat_cluster_t lcl; 486 uint16_t numc;487 427 uint8_t fatno; 488 int rc; 489 490 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc, 491 (uint16_t) -1); 492 if (rc != EOK) 493 return rc; 494 495 if (numc == 0) { 428 429 if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, 430 (uint16_t) -1) == 0) { 496 431 /* No clusters allocated to the node yet. */ 497 432 nodep->firstc = mcl; 498 433 nodep->dirty = true; /* need to sync node */ 499 return EOK; 500 } 501 502 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 503 rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, 504 mcl); 505 if (rc != EOK) 506 return rc; 507 } 508 509 return EOK; 434 return; 435 } 436 437 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) 438 fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); 510 439 } 511 440 … … 517 446 * argument is FAT_CLST_RES0, then all clusters will 518 447 * be chopped off. 519 * 520 * @return EOK on success or a negative return code. 521 */ 522 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc) 523 { 524 int rc; 525 448 */ 449 void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc) 450 { 526 451 dev_handle_t dev_handle = nodep->idx->dev_handle; 527 452 if (lastc == FAT_CLST_RES0) { 528 453 /* The node will have zero size and no clusters allocated. */ 529 rc = fat_free_clusters(bs, dev_handle, nodep->firstc); 530 if (rc != EOK) 531 return rc; 454 fat_free_clusters(bs, dev_handle, nodep->firstc); 532 455 nodep->firstc = FAT_CLST_RES0; 533 456 nodep->dirty = true; /* need to sync node */ … … 536 459 unsigned fatno; 537 460 538 rc = fat_get_cluster(bs, dev_handle, lastc, &nextc); 539 if (rc != EOK) 540 return rc; 461 nextc = fat_get_cluster(bs, dev_handle, lastc); 541 462 542 463 /* Terminate the cluster chain in all copies of FAT. */ 543 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 544 rc = fat_set_cluster(bs, dev_handle, fatno, lastc, 545 FAT_CLST_LAST1); 546 if (rc != EOK) 547 return rc; 548 } 464 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) 465 fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1); 549 466 550 467 /* Free all following clusters. */ 551 rc = fat_free_clusters(bs, dev_handle, nextc); 552 if (rc != EOK) 553 return rc; 554 } 555 556 return EOK; 557 } 558 559 int 468 fat_free_clusters(bs, dev_handle, nextc); 469 } 470 } 471 472 void 560 473 fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c) 561 474 { … … 568 481 569 482 for (i = 0; i < bs->spc; i++) { 570 rc = _fat_block_get(&b, bs, dev_handle, c, i, 571 BLOCK_FLAGS_NOREAD); 572 if (rc != EOK) 573 return rc; 483 b = _fat_block_get(bs, dev_handle, c, i, BLOCK_FLAGS_NOREAD); 574 484 memset(b->data, 0, bps); 575 485 b->dirty = true; 576 486 rc = block_put(b); 577 if (rc != EOK) 578 return rc; 579 } 580 581 return EOK; 487 assert(rc == EOK); 488 } 582 489 } 583 490
Note:
See TracChangeset
for help on using the changeset viewer.