Changes in / [9a1d8ab:14f2100] in mainline
- Location:
- uspace/srv/fs/fat
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/fs/fat/fat.h
r9a1d8ab r14f2100 48 48 49 49 #define min(a, b) ((a) < (b) ? (a) : (b)) 50 51 /*52 * Convenience macros for accessing some frequently used boot sector members.53 */54 #define BPS(bs) uint16_t_le2host((bs)->bps)55 #define SPC(bs) (bs)->spc56 #define RSCNT(bs) uint16_t_le2host((bs)->rscnt)57 #define FATCNT(bs) (bs)->fatcnt58 #define SF(bs) uint16_t_le2host((bs)->sec_per_fat)59 #define RDE(bs) uint16_t_le2host((bs)->root_ent_max)60 #define TS(bs) (uint16_t_le2host((bs)->totsec16) != 0 ? \61 uint16_t_le2host((bs)->totsec16) : \62 uint32_t_le2host(bs->totsec32))63 50 64 51 #define BS_BLOCK 0 … … 211 198 unsigned refcnt; 212 199 bool dirty; 213 214 /*215 * Cache of the node's last and "current" cluster to avoid some216 * unnecessary FAT walks.217 */218 /* Node's last cluster in FAT. */219 bool lastc_cached_valid;220 fat_cluster_t lastc_cached_value;221 /* Node's "current" cluster, i.e. where the last I/O took place. */222 bool currc_cached_valid;223 aoff64_t currc_cached_bn;224 fat_cluster_t currc_cached_value;225 200 } fat_node_t; 226 201 -
uspace/srv/fs/fat/fat_fat.c
r9a1d8ab r14f2100 49 49 #include <mem.h> 50 50 51 /*52 * Convenience macros for computing some frequently used values from the53 * primitive boot sector members.54 */55 #define RDS(bs) ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \56 (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)57 #define SSA(bs) (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))58 59 #define CLBN2PBN(bs, cl, bn) \60 (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))61 62 51 /** 63 52 * The fat_alloc_lock mutex protects all copies of the File Allocation Table … … 85 74 { 86 75 block_t *b; 76 unsigned bps; 77 unsigned rscnt; /* block address of the first FAT */ 87 78 uint16_t clusters = 0; 88 79 fat_cluster_t clst = firstc; 89 80 int rc; 81 82 bps = uint16_t_le2host(bs->bps); 83 rscnt = uint16_t_le2host(bs->rscnt); 90 84 91 85 if (firstc == FAT_CLST_RES0) { … … 105 99 if (lastc) 106 100 *lastc = clst; /* remember the last cluster number */ 107 fsec = (clst * sizeof(fat_cluster_t)) / BPS(bs);108 fidx = clst % ( BPS(bs)/ sizeof(fat_cluster_t));101 fsec = (clst * sizeof(fat_cluster_t)) / bps; 102 fidx = clst % (bps / sizeof(fat_cluster_t)); 109 103 /* read FAT1 */ 110 rc = block_get(&b, dev_handle, RSCNT(bs) + fsec, 111 BLOCK_FLAGS_NONE); 104 rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE); 112 105 if (rc != EOK) 113 106 return rc; … … 132 125 * @param block Pointer to a block pointer for storing result. 133 126 * @param bs Buffer holding the boot sector of the file system. 134 * @param nodep FAT node. 127 * @param dev_handle Device handle of the file system. 128 * @param firstc First cluster used by the file. Can be zero if the file 129 * is empty. 135 130 * @param bn Block number. 136 131 * @param flags Flags passed to libblock. … … 139 134 */ 140 135 int 141 fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,142 aoff64_t bn, int flags)143 {144 fat_cluster_t firstc = nodep->firstc;145 fat_cluster_t currc;146 aoff64_t relbn = bn;147 int rc;148 149 if (!nodep->size)150 return ELIMIT;151 152 if (nodep->firstc == FAT_CLST_ROOT)153 goto fall_through;154 155 if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&156 nodep->lastc_cached_valid) {157 /*158 * This is a request to read a block within the last cluster159 * when fortunately we have the last cluster number cached.160 */161 return block_get(block, nodep->idx->dev_handle,162 CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);163 }164 165 if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {166 /*167 * We can start with the cluster cached by the previous call to168 * fat_block_get().169 */170 firstc = nodep->currc_cached_value;171 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);172 }173 174 fall_through:175 rc = _fat_block_get(block, bs, nodep->idx->dev_handle, firstc,176 &currc, relbn, flags);177 if (rc != EOK)178 return rc;179 180 /*181 * Update the "current" cluster cache.182 */183 nodep->currc_cached_valid = true;184 nodep->currc_cached_bn = bn;185 nodep->currc_cached_value = currc;186 187 return rc;188 }189 190 /** Read block from file located on a FAT file system.191 *192 * @param block Pointer to a block pointer for storing result.193 * @param bs Buffer holding the boot sector of the file system.194 * @param dev_handle Device handle of the file system.195 * @param fcl First cluster used by the file. Can be zero if the file196 * is empty.197 * @param clp If not NULL, address where the cluster containing bn198 * will be stored.199 * stored200 * @param bn Block number.201 * @param flags Flags passed to libblock.202 *203 * @return EOK on success or a negative error code.204 */205 int206 136 _fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle, 207 fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags) 208 { 137 fat_cluster_t firstc, aoff64_t bn, int flags) 138 { 139 unsigned bps; 140 unsigned rscnt; /* block address of the first FAT */ 141 unsigned rde; 142 unsigned rds; /* root directory size */ 143 unsigned sf; 144 unsigned ssa; /* size of the system area */ 209 145 uint16_t clusters; 210 146 unsigned max_clusters; 211 fat_cluster_t c;147 fat_cluster_t lastc; 212 148 int rc; 213 149 … … 215 151 * This function can only operate on non-zero length files. 216 152 */ 217 if (f cl== FAT_CLST_RES0)153 if (firstc == FAT_CLST_RES0) 218 154 return ELIMIT; 219 155 220 if (fcl == FAT_CLST_ROOT) { 156 bps = uint16_t_le2host(bs->bps); 157 rscnt = uint16_t_le2host(bs->rscnt); 158 rde = uint16_t_le2host(bs->root_ent_max); 159 sf = uint16_t_le2host(bs->sec_per_fat); 160 161 rds = (sizeof(fat_dentry_t) * rde) / bps; 162 rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); 163 ssa = rscnt + bs->fatcnt * sf + rds; 164 165 if (firstc == FAT_CLST_ROOT) { 221 166 /* root directory special case */ 222 assert(bn < RDS(bs));223 rc = block_get(block, dev_handle, 224 RSCNT(bs) + FATCNT(bs) * SF(bs) + bn,flags);167 assert(bn < rds); 168 rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn, 169 flags); 225 170 return rc; 226 171 } 227 172 228 max_clusters = bn / SPC(bs); 229 rc = fat_cluster_walk(bs, dev_handle, fcl, &c, &clusters, max_clusters); 173 max_clusters = bn / bs->spc; 174 rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters, 175 max_clusters); 230 176 if (rc != EOK) 231 177 return rc; 232 178 assert(clusters == max_clusters); 233 179 234 rc = block_get(block, dev_handle, CLBN2PBN(bs, c, bn), flags); 235 236 if (clp) 237 *clp = c; 180 rc = block_get(block, dev_handle, 181 ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags); 238 182 239 183 return rc; … … 254 198 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos) 255 199 { 200 uint16_t bps; 201 unsigned spc; 256 202 block_t *b; 257 203 aoff64_t o, boundary; 258 204 int rc; 259 205 260 boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs)); 206 bps = uint16_t_le2host(bs->bps); 207 spc = bs->spc; 208 209 boundary = ROUND_UP(nodep->size, bps * spc); 261 210 262 211 /* zero out already allocated space */ 263 212 for (o = nodep->size; o < pos && o < boundary; 264 o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {265 int flags = (o % BPS(bs)== 0) ?213 o = ALIGN_DOWN(o + bps, bps)) { 214 int flags = (o % bps == 0) ? 266 215 BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE; 267 rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);268 if (rc != EOK) 269 return rc; 270 memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));216 rc = fat_block_get(&b, bs, nodep, o / bps, flags); 217 if (rc != EOK) 218 return rc; 219 memset(b->data + o % bps, 0, bps - o % bps); 271 220 b->dirty = true; /* need to sync node */ 272 221 rc = block_put(b); … … 279 228 280 229 /* zero out the initial part of the new cluster chain */ 281 for (o = boundary; o < pos; o += BPS(bs)) {230 for (o = boundary; o < pos; o += bps) { 282 231 rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl, 283 NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);284 if (rc != EOK) 285 return rc; 286 memset(b->data, 0, min( BPS(bs), pos - o));232 (o - boundary) / bps, BLOCK_FLAGS_NOREAD); 233 if (rc != EOK) 234 return rc; 235 memset(b->data, 0, min(bps, pos - o)); 287 236 b->dirty = true; /* need to sync node */ 288 237 rc = block_put(b); … … 308 257 { 309 258 block_t *b; 259 uint16_t bps; 260 uint16_t rscnt; 261 uint16_t sf; 310 262 fat_cluster_t *cp; 311 263 int rc; 312 264 313 rc = block_get(&b, dev_handle, RSCNT(bs) + SF(bs) * fatno + 314 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE); 265 bps = uint16_t_le2host(bs->bps); 266 rscnt = uint16_t_le2host(bs->rscnt); 267 sf = uint16_t_le2host(bs->sec_per_fat); 268 269 rc = block_get(&b, dev_handle, rscnt + sf * fatno + 270 (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE); 315 271 if (rc != EOK) 316 272 return rc; 317 cp = (fat_cluster_t *)b->data + 318 clst % (BPS(bs) / sizeof(fat_cluster_t)); 273 cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); 319 274 *value = uint16_t_le2host(*cp); 320 275 rc = block_put(b); … … 338 293 { 339 294 block_t *b; 295 uint16_t bps; 296 uint16_t rscnt; 297 uint16_t sf; 340 298 fat_cluster_t *cp; 341 299 int rc; 342 300 343 assert(fatno < FATCNT(bs)); 344 rc = block_get(&b, dev_handle, RSCNT(bs) + SF(bs) * fatno + 345 (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE); 301 bps = uint16_t_le2host(bs->bps); 302 rscnt = uint16_t_le2host(bs->rscnt); 303 sf = uint16_t_le2host(bs->sec_per_fat); 304 305 assert(fatno < bs->fatcnt); 306 rc = block_get(&b, dev_handle, rscnt + sf * fatno + 307 (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE); 346 308 if (rc != EOK) 347 309 return rc; 348 cp = (fat_cluster_t *)b->data + 349 clst % (BPS(bs) / sizeof(fat_cluster_t)); 310 cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); 350 311 *cp = host2uint16_t_le(value); 351 312 b->dirty = true; /* need to sync block */ … … 403 364 fat_cluster_t *mcl, fat_cluster_t *lcl) 404 365 { 366 uint16_t bps; 367 uint16_t rscnt; 368 uint16_t sf; 369 uint32_t ts; 370 unsigned rde; 371 unsigned rds; 372 unsigned ssa; 405 373 block_t *blk; 406 374 fat_cluster_t *lifo; /* stack for storing free cluster numbers */ … … 413 381 return ENOMEM; 414 382 383 bps = uint16_t_le2host(bs->bps); 384 rscnt = uint16_t_le2host(bs->rscnt); 385 sf = uint16_t_le2host(bs->sec_per_fat); 386 rde = uint16_t_le2host(bs->root_ent_max); 387 ts = (uint32_t) uint16_t_le2host(bs->totsec16); 388 if (ts == 0) 389 ts = uint32_t_le2host(bs->totsec32); 390 391 rds = (sizeof(fat_dentry_t) * rde) / bps; 392 rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); 393 ssa = rscnt + bs->fatcnt * sf + rds; 394 415 395 /* 416 396 * Search FAT1 for unused clusters. 417 397 */ 418 398 fibril_mutex_lock(&fat_alloc_lock); 419 for (b = 0, cl = 0; b < SF(bs); b++) { 420 rc = block_get(&blk, dev_handle, RSCNT(bs) + b, 421 BLOCK_FLAGS_NONE); 399 for (b = 0, cl = 0; b < sf; b++) { 400 rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE); 422 401 if (rc != EOK) 423 402 goto error; 424 for (c = 0; c < BPS(bs)/ sizeof(fat_cluster_t); c++, cl++) {403 for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { 425 404 /* 426 405 * Check if the cluster is physically there. This check … … 429 408 * from the size of the file allocation table. 430 409 */ 431 if ((cl >= 2) && 432 ((cl - 2) * SPC(bs) + SSA(bs) >= TS(bs))) { 410 if ((cl >= 2) && ((cl - 2) * bs->spc + ssa >= ts)) { 433 411 rc = block_put(blk); 434 412 if (rc != EOK) … … 533 511 * @param nodep Node representing the file. 534 512 * @param mcl First cluster of the cluster chain to append. 535 * @param lcl Last cluster of the cluster chain to append.536 513 * 537 514 * @return EOK on success or a negative error code. 538 515 */ 539 int 540 fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, 541 fat_cluster_t lcl) 516 int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) 542 517 { 543 518 dev_handle_t dev_handle = nodep->idx->dev_handle; 544 fat_cluster_t l astc;519 fat_cluster_t lcl; 545 520 uint16_t numc; 546 521 uint8_t fatno; 547 522 int rc; 548 523 549 if (nodep->lastc_cached_valid) { 550 lastc = nodep->lastc_cached_value; 551 nodep->lastc_cached_valid = false; 552 } else { 553 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lastc, 554 &numc, (uint16_t) -1); 555 if (rc != EOK) 556 return rc; 557 558 if (numc == 0) { 559 /* No clusters allocated to the node yet. */ 560 nodep->firstc = mcl; 561 nodep->dirty = true; /* need to sync node */ 562 return EOK; 563 } 524 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc, 525 (uint16_t) -1); 526 if (rc != EOK) 527 return rc; 528 529 if (numc == 0) { 530 /* No clusters allocated to the node yet. */ 531 nodep->firstc = mcl; 532 nodep->dirty = true; /* need to sync node */ 533 return EOK; 564 534 } 565 535 566 536 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 567 rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, l astc,537 rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, 568 538 mcl); 569 539 if (rc != EOK) 570 540 return rc; 571 541 } 572 573 nodep->lastc_cached_valid = true;574 nodep->lastc_cached_value = lcl;575 542 576 543 return EOK; … … 581 548 * @param bs Buffer holding the boot sector of the file system. 582 549 * @param nodep FAT node where the chopping will take place. 583 * @param l clLast cluster which will remain in the node. If this550 * @param lastc Last cluster which will remain in the node. If this 584 551 * argument is FAT_CLST_RES0, then all clusters will 585 552 * be chopped off. … … 587 554 * @return EOK on success or a negative return code. 588 555 */ 589 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl) 590 { 591 int rc; 556 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc) 557 { 558 int rc; 559 592 560 dev_handle_t dev_handle = nodep->idx->dev_handle; 593 594 /* 595 * Invalidate cached cluster numbers. 596 */ 597 nodep->lastc_cached_valid = false; 598 if (nodep->currc_cached_value != lcl) 599 nodep->currc_cached_valid = false; 600 601 if (lcl == FAT_CLST_RES0) { 561 if (lastc == FAT_CLST_RES0) { 602 562 /* The node will have zero size and no clusters allocated. */ 603 563 rc = fat_free_clusters(bs, dev_handle, nodep->firstc); … … 610 570 unsigned fatno; 611 571 612 rc = fat_get_cluster(bs, dev_handle, FAT1, l cl, &nextc);572 rc = fat_get_cluster(bs, dev_handle, FAT1, lastc, &nextc); 613 573 if (rc != EOK) 614 574 return rc; … … 616 576 /* Terminate the cluster chain in all copies of FAT. */ 617 577 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) { 618 rc = fat_set_cluster(bs, dev_handle, fatno, l cl,578 rc = fat_set_cluster(bs, dev_handle, fatno, lastc, 619 579 FAT_CLST_LAST1); 620 580 if (rc != EOK) … … 628 588 } 629 589 630 /*631 * Update and re-enable the last cluster cache.632 */633 nodep->lastc_cached_valid = true;634 nodep->lastc_cached_value = lcl;635 636 590 return EOK; 637 591 } … … 642 596 int i; 643 597 block_t *b; 644 int rc; 645 646 for (i = 0; i < SPC(bs); i++) { 647 rc = _fat_block_get(&b, bs, dev_handle, c, NULL, i, 598 unsigned bps; 599 int rc; 600 601 bps = uint16_t_le2host(bs->bps); 602 603 for (i = 0; i < bs->spc; i++) { 604 rc = _fat_block_get(&b, bs, dev_handle, c, i, 648 605 BLOCK_FLAGS_NOREAD); 649 606 if (rc != EOK) 650 607 return rc; 651 memset(b->data, 0, BPS(bs));608 memset(b->data, 0, bps); 652 609 b->dirty = true; 653 610 rc = block_put(b); -
uspace/srv/fs/fat/fat_fat.h
r9a1d8ab r14f2100 64 64 fat_cluster_t *, uint16_t *, uint16_t); 65 65 66 extern int fat_block_get(block_t **, struct fat_bs *, struct fat_node *, 67 aoff64_t, int); 66 #define fat_block_get(b, bs, np, bn, flags) \ 67 _fat_block_get((b), (bs), (np)->idx->dev_handle, (np)->firstc, (bn), \ 68 (flags)) 69 68 70 extern int _fat_block_get(block_t **, struct fat_bs *, dev_handle_t, 69 fat_cluster_t, fat_cluster_t *,aoff64_t, int);71 fat_cluster_t, aoff64_t, int); 70 72 71 73 extern int fat_append_clusters(struct fat_bs *, struct fat_node *, 72 fat_cluster_t , fat_cluster_t);74 fat_cluster_t); 73 75 extern int fat_chop_clusters(struct fat_bs *, struct fat_node *, 74 76 fat_cluster_t); -
uspace/srv/fs/fat/fat_ops.c
r9a1d8ab r14f2100 60 60 #define FS_NODE(node) ((node) ? (node)->bp : NULL) 61 61 62 #define DPS(bs) (BPS((bs)) / sizeof(fat_dentry_t))63 #define BPC(bs) (BPS((bs)) * SPC((bs)))64 65 62 /** Mutex protecting the list of cached free FAT nodes. */ 66 63 static FIBRIL_MUTEX_INITIALIZE(ffn_mutex); … … 104 101 node->refcnt = 0; 105 102 node->dirty = false; 106 node->lastc_cached_valid = false;107 node->lastc_cached_value = FAT_CLST_LAST1;108 node->currc_cached_valid = false;109 node->currc_cached_bn = 0;110 node->currc_cached_value = FAT_CLST_LAST1;111 103 } 112 104 … … 116 108 fat_bs_t *bs; 117 109 fat_dentry_t *d; 110 uint16_t bps; 111 unsigned dps; 118 112 int rc; 119 113 … … 121 115 122 116 bs = block_bb_get(node->idx->dev_handle); 117 bps = uint16_t_le2host(bs->bps); 118 dps = bps / sizeof(fat_dentry_t); 123 119 124 120 /* Read the block that contains the dentry of interest. */ 125 121 rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc, 126 NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs), 127 BLOCK_FLAGS_NONE); 122 (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); 128 123 if (rc != EOK) 129 124 return rc; 130 125 131 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));126 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); 132 127 133 128 d->firstc = host2uint16_t_le(node->firstc); … … 271 266 fat_dentry_t *d; 272 267 fat_node_t *nodep = NULL; 268 unsigned bps; 269 unsigned spc; 270 unsigned dps; 273 271 int rc; 274 272 … … 300 298 301 299 bs = block_bb_get(idxp->dev_handle); 300 bps = uint16_t_le2host(bs->bps); 301 spc = bs->spc; 302 dps = bps / sizeof(fat_dentry_t); 302 303 303 304 /* Read the block that contains the dentry of interest. */ 304 rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc, NULL,305 (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);305 rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc, 306 (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); 306 307 if (rc != EOK) { 307 308 (void) fat_node_put(FS_NODE(nodep)); … … 309 310 } 310 311 311 d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));312 d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); 312 313 if (d->attr & FAT_ATTR_SUBDIR) { 313 314 /* … … 329 330 return rc; 330 331 } 331 nodep->size = BPS(bs) * SPC(bs)* clusters;332 nodep->size = bps * spc * clusters; 332 333 } else { 333 334 nodep->type = FAT_FILE; … … 367 368 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; 368 369 unsigned i, j; 370 unsigned bps; /* bytes per sector */ 371 unsigned dps; /* dentries per sector */ 369 372 unsigned blocks; 370 373 fat_dentry_t *d; … … 374 377 fibril_mutex_lock(&parentp->idx->lock); 375 378 bs = block_bb_get(parentp->idx->dev_handle); 376 blocks = parentp->size / BPS(bs); 379 bps = uint16_t_le2host(bs->bps); 380 dps = bps / sizeof(fat_dentry_t); 381 blocks = parentp->size / bps; 377 382 for (i = 0; i < blocks; i++) { 378 383 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); … … 381 386 return rc; 382 387 } 383 for (j = 0; j < DPS(bs); j++) {388 for (j = 0; j < dps; j++) { 384 389 d = ((fat_dentry_t *)b->data) + j; 385 390 switch (fat_classify_dentry(d)) { … … 409 414 fat_idx_t *idx = fat_idx_get_by_pos( 410 415 parentp->idx->dev_handle, parentp->firstc, 411 i * DPS(bs)+ j);416 i * dps + j); 412 417 fibril_mutex_unlock(&parentp->idx->lock); 413 418 if (!idx) { … … 508 513 fat_bs_t *bs; 509 514 fat_cluster_t mcl, lcl; 515 uint16_t bps; 510 516 int rc; 511 517 512 518 bs = block_bb_get(dev_handle); 519 bps = uint16_t_le2host(bs->bps); 513 520 if (flags & L_DIRECTORY) { 514 521 /* allocate a cluster */ … … 539 546 nodep->type = FAT_DIRECTORY; 540 547 nodep->firstc = mcl; 541 nodep->size = BPS(bs) * SPC(bs);548 nodep->size = bps * bs->spc; 542 549 } else { 543 550 nodep->type = FAT_FILE; … … 602 609 block_t *b; 603 610 unsigned i, j; 611 uint16_t bps; 612 unsigned dps; 604 613 unsigned blocks; 605 614 fat_cluster_t mcl, lcl; … … 631 640 fibril_mutex_lock(&parentp->idx->lock); 632 641 bs = block_bb_get(parentp->idx->dev_handle); 633 634 blocks = parentp->size / BPS(bs); 642 bps = uint16_t_le2host(bs->bps); 643 dps = bps / sizeof(fat_dentry_t); 644 645 blocks = parentp->size / bps; 635 646 636 647 for (i = 0; i < blocks; i++) { … … 640 651 return rc; 641 652 } 642 for (j = 0; j < DPS(bs); j++) {653 for (j = 0; j < dps; j++) { 643 654 d = ((fat_dentry_t *)b->data) + j; 644 655 switch (fat_classify_dentry(d)) { … … 680 691 return rc; 681 692 } 682 rc = fat_append_clusters(bs, parentp, mcl , lcl);693 rc = fat_append_clusters(bs, parentp, mcl); 683 694 if (rc != EOK) { 684 695 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl); … … 686 697 return rc; 687 698 } 688 parentp->size += BPS(bs) * SPC(bs);699 parentp->size += bps * bs->spc; 689 700 parentp->dirty = true; /* need to sync node */ 690 701 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE); … … 760 771 761 772 childp->idx->pfc = parentp->firstc; 762 childp->idx->pdi = i * DPS(bs)+ j;773 childp->idx->pdi = i * dps + j; 763 774 fibril_mutex_unlock(&childp->idx->lock); 764 775 … … 782 793 fat_bs_t *bs; 783 794 fat_dentry_t *d; 795 uint16_t bps; 784 796 block_t *b; 785 797 bool has_children; … … 800 812 fibril_mutex_lock(&childp->idx->lock); 801 813 bs = block_bb_get(childp->idx->dev_handle); 814 bps = uint16_t_le2host(bs->bps); 802 815 803 816 rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc, 804 NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),817 (childp->idx->pdi * sizeof(fat_dentry_t)) / bps, 805 818 BLOCK_FLAGS_NONE); 806 819 if (rc != EOK) 807 820 goto error; 808 821 d = (fat_dentry_t *)b->data + 809 (childp->idx->pdi % ( BPS(bs)/ sizeof(fat_dentry_t)));822 (childp->idx->pdi % (bps / sizeof(fat_dentry_t))); 810 823 /* mark the dentry as not-currently-used */ 811 824 d->name[0] = FAT_DENTRY_ERASED; … … 839 852 fat_bs_t *bs; 840 853 fat_node_t *nodep = FAT_NODE(fn); 854 unsigned bps; 855 unsigned dps; 841 856 unsigned blocks; 842 857 block_t *b; … … 851 866 fibril_mutex_lock(&nodep->idx->lock); 852 867 bs = block_bb_get(nodep->idx->dev_handle); 853 854 blocks = nodep->size / BPS(bs); 868 bps = uint16_t_le2host(bs->bps); 869 dps = bps / sizeof(fat_dentry_t); 870 871 blocks = nodep->size / bps; 855 872 856 873 for (i = 0; i < blocks; i++) { … … 862 879 return rc; 863 880 } 864 for (j = 0; j < DPS(bs); j++) {881 for (j = 0; j < dps; j++) { 865 882 d = ((fat_dentry_t *)b->data) + j; 866 883 switch (fat_classify_dentry(d)) { … … 959 976 enum cache_mode cmode; 960 977 fat_bs_t *bs; 978 uint16_t bps; 979 uint16_t rde; 961 980 962 981 /* Accept the mount options */ … … 995 1014 bs = block_bb_get(dev_handle); 996 1015 997 if (BPS(bs) != BS_SIZE) { 1016 /* Read the number of root directory entries. */ 1017 bps = uint16_t_le2host(bs->bps); 1018 rde = uint16_t_le2host(bs->root_ent_max); 1019 1020 if (bps != BS_SIZE) { 998 1021 block_fini(dev_handle); 999 1022 ipc_answer_0(rid, ENOTSUP); … … 1002 1025 1003 1026 /* Initialize the block cache */ 1004 rc = block_cache_init(dev_handle, BPS(bs), 0 /* XXX */, cmode);1027 rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode); 1005 1028 if (rc != EOK) { 1006 1029 block_fini(dev_handle); … … 1064 1087 rootp->refcnt = 1; 1065 1088 rootp->lnkcnt = 0; /* FS root is not linked */ 1066 rootp->size = RDE(bs)* sizeof(fat_dentry_t);1089 rootp->size = rde * sizeof(fat_dentry_t); 1067 1090 rootp->idx = ridxp; 1068 1091 ridxp->nodep = rootp; … … 1142 1165 fat_node_t *nodep; 1143 1166 fat_bs_t *bs; 1167 uint16_t bps; 1144 1168 size_t bytes; 1145 1169 block_t *b; … … 1167 1191 1168 1192 bs = block_bb_get(dev_handle); 1193 bps = uint16_t_le2host(bs->bps); 1169 1194 1170 1195 if (nodep->type == FAT_FILE) { … … 1179 1204 (void) async_data_read_finalize(callid, NULL, 0); 1180 1205 } else { 1181 bytes = min(len, BPS(bs) - pos % BPS(bs));1206 bytes = min(len, bps - pos % bps); 1182 1207 bytes = min(bytes, nodep->size - pos); 1183 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),1208 rc = fat_block_get(&b, bs, nodep, pos / bps, 1184 1209 BLOCK_FLAGS_NONE); 1185 1210 if (rc != EOK) { … … 1189 1214 return; 1190 1215 } 1191 (void) async_data_read_finalize(callid, 1192 b ->data + pos % BPS(bs), bytes);1216 (void) async_data_read_finalize(callid, b->data + pos % bps, 1217 bytes); 1193 1218 rc = block_put(b); 1194 1219 if (rc != EOK) { … … 1205 1230 1206 1231 assert(nodep->type == FAT_DIRECTORY); 1207 assert(nodep->size % BPS(bs)== 0);1208 assert( BPS(bs)% sizeof(fat_dentry_t) == 0);1232 assert(nodep->size % bps == 0); 1233 assert(bps % sizeof(fat_dentry_t) == 0); 1209 1234 1210 1235 /* … … 1214 1239 * the position pointer accordingly. 1215 1240 */ 1216 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs);1217 while (bnum < nodep->size / BPS(bs)) {1241 bnum = (pos * sizeof(fat_dentry_t)) / bps; 1242 while (bnum < nodep->size / bps) { 1218 1243 aoff64_t o; 1219 1244 … … 1222 1247 if (rc != EOK) 1223 1248 goto err; 1224 for (o = pos % ( BPS(bs)/ sizeof(fat_dentry_t));1225 o < BPS(bs)/ sizeof(fat_dentry_t);1249 for (o = pos % (bps / sizeof(fat_dentry_t)); 1250 o < bps / sizeof(fat_dentry_t); 1226 1251 o++, pos++) { 1227 1252 d = ((fat_dentry_t *)b->data) + o; … … 1281 1306 size_t bytes, size; 1282 1307 block_t *b; 1308 uint16_t bps; 1309 unsigned spc; 1310 unsigned bpc; /* bytes per cluster */ 1283 1311 aoff64_t boundary; 1284 1312 int flags = BLOCK_FLAGS_NONE; … … 1306 1334 1307 1335 bs = block_bb_get(dev_handle); 1336 bps = uint16_t_le2host(bs->bps); 1337 spc = bs->spc; 1338 bpc = bps * spc; 1308 1339 1309 1340 /* … … 1314 1345 * value signalizing a smaller number of bytes written. 1315 1346 */ 1316 bytes = min(len, BPS(bs) - pos % BPS(bs));1317 if (bytes == BPS(bs))1347 bytes = min(len, bps - pos % bps); 1348 if (bytes == bps) 1318 1349 flags |= BLOCK_FLAGS_NOREAD; 1319 1350 1320 boundary = ROUND_UP(nodep->size, BPC(bs));1351 boundary = ROUND_UP(nodep->size, bpc); 1321 1352 if (pos < boundary) { 1322 1353 /* … … 1333 1364 return; 1334 1365 } 1335 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);1366 rc = fat_block_get(&b, bs, nodep, pos / bps, flags); 1336 1367 if (rc != EOK) { 1337 1368 (void) fat_node_put(fn); … … 1340 1371 return; 1341 1372 } 1342 (void) async_data_write_finalize(callid, 1343 b ->data + pos % BPS(bs), bytes);1373 (void) async_data_write_finalize(callid, b->data + pos % bps, 1374 bytes); 1344 1375 b->dirty = true; /* need to sync block */ 1345 1376 rc = block_put(b); … … 1365 1396 fat_cluster_t mcl, lcl; 1366 1397 1367 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);1398 nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; 1368 1399 /* create an independent chain of nclsts clusters in all FATs */ 1369 1400 rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); … … 1384 1415 return; 1385 1416 } 1386 rc = _fat_block_get(&b, bs, dev_handle, lcl, NULL,1387 (pos / BPS(bs)) % SPC(bs),flags);1417 rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc, 1418 flags); 1388 1419 if (rc != EOK) { 1389 1420 (void) fat_free_clusters(bs, dev_handle, mcl); … … 1393 1424 return; 1394 1425 } 1395 (void) async_data_write_finalize(callid, 1396 b ->data + pos % BPS(bs), bytes);1426 (void) async_data_write_finalize(callid, b->data + pos % bps, 1427 bytes); 1397 1428 b->dirty = true; /* need to sync block */ 1398 1429 rc = block_put(b); … … 1407 1438 * node's cluster chain. 1408 1439 */ 1409 rc = fat_append_clusters(bs, nodep, mcl , lcl);1440 rc = fat_append_clusters(bs, nodep, mcl); 1410 1441 if (rc != EOK) { 1411 1442 (void) fat_free_clusters(bs, dev_handle, mcl); … … 1431 1462 fat_node_t *nodep; 1432 1463 fat_bs_t *bs; 1464 uint16_t bps; 1465 uint8_t spc; 1466 unsigned bpc; /* bytes per cluster */ 1433 1467 int rc; 1434 1468 … … 1445 1479 1446 1480 bs = block_bb_get(dev_handle); 1481 bps = uint16_t_le2host(bs->bps); 1482 spc = bs->spc; 1483 bpc = bps * spc; 1447 1484 1448 1485 if (nodep->size == size) { … … 1454 1491 */ 1455 1492 rc = EINVAL; 1456 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {1493 } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { 1457 1494 /* 1458 1495 * The node will be shrunk, but no clusters will be deallocated. … … 1472 1509 fat_cluster_t lastc; 1473 1510 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, 1474 &lastc, NULL, (size - 1) / BPC(bs));1511 &lastc, NULL, (size - 1) / bpc); 1475 1512 if (rc != EOK) 1476 1513 goto out;
Note:
See TracChangeset
for help on using the changeset viewer.