Subversion Repositories HelenOS

Rev

Rev 3571 | Rev 3573 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3571 Rev 3572
Line 127... Line 127...
127
    unsigned rde;
127
    unsigned rde;
128
    unsigned rds;       /* root directory size */
128
    unsigned rds;       /* root directory size */
129
    unsigned sf;
129
    unsigned sf;
130
    unsigned ssa;       /* size of the system area */
130
    unsigned ssa;       /* size of the system area */
131
    unsigned clusters, max_clusters;
131
    unsigned clusters, max_clusters;
132
    fat_cluster_t lastc, clst = firstc;
132
    fat_cluster_t lastc;
133
 
133
 
134
    bps = uint16_t_le2host(bs->bps);
134
    bps = uint16_t_le2host(bs->bps);
135
    rscnt = uint16_t_le2host(bs->rscnt);
135
    rscnt = uint16_t_le2host(bs->rscnt);
136
    rde = uint16_t_le2host(bs->root_ent_max);
136
    rde = uint16_t_le2host(bs->root_ent_max);
137
    sf = uint16_t_le2host(bs->sec_per_fat);
137
    sf = uint16_t_le2host(bs->sec_per_fat);
Line 156... Line 156...
156
        bn % bs->spc);
156
        bn % bs->spc);
157
 
157
 
158
    return b;
158
    return b;
159
}
159
}
160
 
160
 
161
 
-
 
162
/** Fill the gap between EOF and a new file position.
161
/** Fill the gap between EOF and a new file position.
163
 *
162
 *
164
 * @param bs        Buffer holding the boot sector for nodep.
163
 * @param bs        Buffer holding the boot sector for nodep.
165
 * @param nodep     FAT node with the gap.
164
 * @param nodep     FAT node with the gap.
166
 * @param mcl       First cluster in an independent cluster chain that will
165
 * @param mcl       First cluster in an independent cluster chain that will
Line 201... Line 200...
201
        b->dirty = true;        /* need to sync node */
200
        b->dirty = true;        /* need to sync node */
202
        block_put(b);
201
        block_put(b);
203
    }
202
    }
204
}
203
}
205
 
204
 
-
 
205
/** Get cluster from the first FAT.
-
 
206
 *
-
 
207
 * @param bs        Buffer holding the boot sector for the file system.
-
 
208
 * @param dev_handle    Device handle for the file system.
-
 
209
 * @param clst      Cluster which to get.
-
 
210
 *
-
 
211
 * @return      Value found in the cluster.
-
 
212
 */
-
 
213
fat_cluster_t
-
 
214
fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst)
-
 
215
{
-
 
216
    block_t *b;
-
 
217
    uint16_t bps;
-
 
218
    uint16_t rscnt;
-
 
219
    fat_cluster_t *cp, value;
-
 
220
 
-
 
221
    bps = uint16_t_le2host(bs->bps);
-
 
222
    rscnt = uint16_t_le2host(bs->rscnt);
-
 
223
 
-
 
224
    b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps);
-
 
225
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
-
 
226
    value = uint16_t_le2host(*cp);
-
 
227
    block_put(b);
-
 
228
   
-
 
229
    return value;
-
 
230
}
-
 
231
 
206
/** Mark cluster in one instance of FAT.
232
/** Set cluster in one instance of FAT.
207
 *
233
 *
208
 * @param bs        Buffer holding the boot sector for the file system.
234
 * @param bs        Buffer holding the boot sector for the file system.
209
 * @param dev_handle    Device handle for the file system.
235
 * @param dev_handle    Device handle for the file system.
210
 * @param fatno     Number of the FAT instance where to make the change.
236
 * @param fatno     Number of the FAT instance where to make the change.
211
 * @param clst      Cluster which is to be marked.
237
 * @param clst      Cluster which is to be set.
212
 * @param value     Value mark the cluster with.
238
 * @param value     Value to set the cluster with.
213
 */
239
 */
214
void
240
void
215
fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
241
fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
216
    fat_cluster_t clst, fat_cluster_t value)
242
    fat_cluster_t clst, fat_cluster_t value)
217
{
243
{
218
    block_t *b;
244
    block_t *b;
219
    uint16_t bps;
245
    uint16_t bps;
220
    uint16_t rscnt;
246
    uint16_t rscnt;
Line 247... Line 273...
247
    uint8_t fatno;
273
    uint8_t fatno;
248
    unsigned c;
274
    unsigned c;
249
 
275
 
250
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
276
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
251
        for (c = 0; c < nclsts; c++) {
277
        for (c = 0; c < nclsts; c++) {
252
            fat_mark_cluster(bs, dev_handle, fatno, lifo[c],
278
            fat_set_cluster(bs, dev_handle, fatno, lifo[c],
253
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
279
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
254
        }
280
        }
255
    }
281
    }
256
}
282
}
257
 
283
 
Line 331... Line 357...
331
    /*
357
    /*
332
     * We could not find enough clusters. Now we need to free the clusters
358
     * We could not find enough clusters. Now we need to free the clusters
333
     * we have allocated so far.
359
     * we have allocated so far.
334
     */
360
     */
335
    while (found--) {
361
    while (found--) {
336
        fat_mark_cluster(bs, dev_handle, FAT1, lifo[found],
362
        fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
337
            FAT_CLST_RES0);
363
            FAT_CLST_RES0);
338
    }
364
    }
339
   
365
   
340
    free(lifo);
366
    free(lifo);
341
    return ENOSPC;
367
    return ENOSPC;
342
}
368
}
343
 
369
 
-
 
370
/** Free clusters forming a cluster chain in all copies of FAT.
-
 
371
 *
-
 
372
 * @param bs        Buffer hodling the boot sector of the file system.
-
 
373
 * @param dev_handle    Device handle of the file system.
-
 
374
 * @param firstc    First cluster in the chain which is to be freed.
-
 
375
 */
-
 
376
void
-
 
377
fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
-
 
378
{
-
 
379
    unsigned fatno;
-
 
380
    fat_cluster_t nextc;
-
 
381
 
-
 
382
    /* Mark all clusters in the chain as free in all copies of FAT. */
-
 
383
    while (firstc < FAT_CLST_LAST1) {
-
 
384
        nextc = fat_get_cluster(bs, dev_handle, firstc);
-
 
385
        assert(nextc >= FAT_CLST_FIRST && nextc < FAT_CLST_BAD);
-
 
386
        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
-
 
387
            fat_set_cluster(bs, dev_handle, fatno, firstc,
-
 
388
                FAT_CLST_RES0);
-
 
389
        firstc = nextc;
-
 
390
    }
-
 
391
}
-
 
392
 
344
/** Append a cluster chain to the last file cluster in all FATs.
393
/** Append a cluster chain to the last file cluster in all FATs.
345
 *
394
 *
346
 * @param bs        Buffer holding boot sector of the file system.
395
 * @param bs        Buffer holding the boot sector of the file system.
347
 * @param nodep     Node representing the file.
396
 * @param nodep     Node representing the file.
348
 * @param mcl       First cluster of the cluster chain to append.
397
 * @param mcl       First cluster of the cluster chain to append.
349
 */
398
 */
350
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
399
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
351
{
400
{
352
    dev_handle_t dev_handle = nodep->idx->dev_handle;
401
    dev_handle_t dev_handle = nodep->idx->dev_handle;
353
    fat_cluster_t lcl;
402
    fat_cluster_t lcl;
354
    uint8_t fatno;
403
    uint8_t fatno;
355
 
404
 
356
    if (fat_cluster_walk(bs, nodep->idx->dev_handle, nodep->firstc, &lcl,
405
    if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
357
        (uint16_t) -1) == 0) {
406
        (uint16_t) -1) == 0) {
358
        /* No clusters allocated to the node yet. */
407
        /* No clusters allocated to the node yet. */
359
        nodep->firstc = host2uint16_t_le(mcl);
408
        nodep->firstc = host2uint16_t_le(mcl);
360
        nodep->dirty = true;        /* need to sync node */
409
        nodep->dirty = true;        /* need to sync node */
361
        return;
410
        return;
362
    }
411
    }
363
 
412
 
364
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
413
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
365
        fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
414
        fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
-
 
415
}
-
 
416
 
-
 
417
/** Chop off node clusters in all copies of FAT.
-
 
418
 *
-
 
419
 * @param bs        Buffer holding the boot sector of the file system.
-
 
420
 * @param nodep     FAT node where the chopping will take place.
-
 
421
 * @param lastc     Last cluster which will remain in the node. If this
-
 
422
 *          argument is FAT_CLST_RES0, then all clusters will
-
 
423
 *          be choped off.
-
 
424
 */
-
 
425
void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
-
 
426
{
-
 
427
    dev_handle_t dev_handle = nodep->idx->dev_handle;
-
 
428
    if (lastc == FAT_CLST_RES0) {
-
 
429
        /* The node will have zero size and no clusters allocated. */
-
 
430
        fat_free_clusters(bs, dev_handle, nodep->firstc);
-
 
431
        nodep->firstc = FAT_CLST_RES0;
-
 
432
        nodep->dirty = true;        /* need to sync node */
-
 
433
    } else {
-
 
434
        fat_cluster_t nextc;
-
 
435
        unsigned fatno;
-
 
436
 
-
 
437
        nextc = fat_get_cluster(bs, dev_handle, lastc);
-
 
438
 
-
 
439
        /* Terminate the cluster chain in all copies of FAT. */
-
 
440
        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
-
 
441
            fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1);
-
 
442
 
-
 
443
        /* Free all following clusters. */
-
 
444
        fat_free_clusters(bs, dev_handle, nextc);
-
 
445
    }
366
}
446
}
367
 
447
 
368
/**
448
/**
369
 * @}
449
 * @}
370
 */
450
 */