Rev 3597 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3597 | Rev 3675 | ||
|---|---|---|---|
| Line 180... | Line 180... | ||
| 180 | spc = bs->spc; |
180 | spc = bs->spc; |
| 181 | 181 | ||
| 182 | boundary = ROUND_UP(nodep->size, bps * spc); |
182 | boundary = ROUND_UP(nodep->size, bps * spc); |
| 183 | 183 | ||
| 184 | /* zero out already allocated space */ |
184 | /* zero out already allocated space */ |
| 185 | for (o = nodep->size - 1; o < pos && o < boundary; |
185 | for (o = nodep->size; o < pos && o < boundary; |
| 186 | o = ALIGN_DOWN(o + bps, bps)) { |
186 | o = ALIGN_DOWN(o + bps, bps)) { |
| 187 | int flags = (o % bps == 0) ? |
187 | int flags = (o % bps == 0) ? |
| 188 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE; |
188 | BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE; |
| 189 | b = fat_block_get(bs, nodep, o / bps, flags); |
189 | b = fat_block_get(bs, nodep, o / bps, flags); |
| 190 | memset(b->data + o % bps, 0, bps - o % bps); |
190 | memset(b->data + o % bps, 0, bps - o % bps); |
| Line 283... | Line 283... | ||
| 283 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
283 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
| 284 | } |
284 | } |
| 285 | } |
285 | } |
| 286 | } |
286 | } |
| 287 | 287 | ||
| 288 | /** Allocate clusters in FAT1. |
288 | /** Allocate clusters in all copies of FAT. |
| 289 | * |
289 | * |
| 290 | * This function will attempt to allocate the requested number of clusters in |
290 | * This function will attempt to allocate the requested number of clusters in |
| 291 | * the first FAT instance. The FAT will be altered so that the allocated |
291 | * all instances of the FAT. The FAT will be altered so that the allocated |
| 292 | * clusters form an independent chain (i.e. a chain which does not belong to any |
292 | * clusters form an independent chain (i.e. a chain which does not belong to any |
| 293 | * file yet). |
293 | * file yet). |
| 294 | * |
294 | * |
| 295 | * @param bs Buffer holding the boot sector of the file system. |
295 | * @param bs Buffer holding the boot sector of the file system. |
| 296 | * @param dev_handle Device handle of the file system. |
296 | * @param dev_handle Device handle of the file system. |
| Line 313... | Line 313... | ||
| 313 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
313 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
| 314 | unsigned found = 0; /* top of the free cluster number stack */ |
314 | unsigned found = 0; /* top of the free cluster number stack */ |
| 315 | unsigned b, c, cl; |
315 | unsigned b, c, cl; |
| 316 | 316 | ||
| 317 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
317 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
| 318 | if (lifo) |
318 | if (!lifo) |
| 319 | return ENOMEM; |
319 | return ENOMEM; |
| 320 | 320 | ||
| 321 | bps = uint16_t_le2host(bs->bps); |
321 | bps = uint16_t_le2host(bs->bps); |
| 322 | rscnt = uint16_t_le2host(bs->rscnt); |
322 | rscnt = uint16_t_le2host(bs->rscnt); |
| 323 | sf = uint16_t_le2host(bs->sec_per_fat); |
323 | sf = uint16_t_le2host(bs->sec_per_fat); |
| 324 | 324 | ||
| 325 | /* |
325 | /* |
| 326 | * Search FAT1 for unused clusters. |
326 | * Search FAT1 for unused clusters. |
| 327 | */ |
327 | */ |
| 328 | futex_down(&fat_alloc_lock); |
328 | futex_down(&fat_alloc_lock); |
| 329 | for (b = 0, cl = 0; b < sf; blk++) { |
329 | for (b = 0, cl = 0; b < sf; b++) { |
| 330 | blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NOREAD); |
330 | blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE); |
| 331 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
331 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
| 332 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
332 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
| 333 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
333 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
| 334 | /* |
334 | /* |
| 335 | * The cluster is free. Put it into our stack |
335 | * The cluster is free. Put it into our stack |
| Line 383... | Line 383... | ||
| 383 | unsigned fatno; |
383 | unsigned fatno; |
| 384 | fat_cluster_t nextc; |
384 | fat_cluster_t nextc; |
| 385 | 385 | ||
| 386 | /* Mark all clusters in the chain as free in all copies of FAT. */ |
386 | /* Mark all clusters in the chain as free in all copies of FAT. */ |
| 387 | while (firstc < FAT_CLST_LAST1) { |
387 | while (firstc < FAT_CLST_LAST1) { |
| - | 388 | assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD); |
|
| 388 | nextc = fat_get_cluster(bs, dev_handle, firstc); |
389 | nextc = fat_get_cluster(bs, dev_handle, firstc); |
| 389 | assert(nextc >= FAT_CLST_FIRST && nextc < FAT_CLST_BAD); |
- | |
| 390 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
390 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
| 391 | fat_set_cluster(bs, dev_handle, fatno, firstc, |
391 | fat_set_cluster(bs, dev_handle, fatno, firstc, |
| 392 | FAT_CLST_RES0); |
392 | FAT_CLST_RES0); |
| 393 | firstc = nextc; |
393 | firstc = nextc; |
| 394 | } |
394 | } |
| Line 407... | Line 407... | ||
| 407 | uint8_t fatno; |
407 | uint8_t fatno; |
| 408 | 408 | ||
| 409 | if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, |
409 | if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, |
| 410 | (uint16_t) -1) == 0) { |
410 | (uint16_t) -1) == 0) { |
| 411 | /* No clusters allocated to the node yet. */ |
411 | /* No clusters allocated to the node yet. */ |
| 412 | nodep->firstc = host2uint16_t_le(mcl); |
412 | nodep->firstc = mcl; |
| 413 | nodep->dirty = true; /* need to sync node */ |
413 | nodep->dirty = true; /* need to sync node */ |
| 414 | return; |
414 | return; |
| 415 | } |
415 | } |
| 416 | 416 | ||
| 417 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
417 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |