Subversion Repositories HelenOS

Rev

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

Rev 3561 Rev 3588
Line 57... Line 57...
57
/** Walk the cluster chain.
57
/** Walk the cluster chain.
58
 *
58
 *
59
 * @param bs        Buffer holding the boot sector for the file.
59
 * @param bs        Buffer holding the boot sector for the file.
60
 * @param dev_handle    Device handle of the device with the file.
60
 * @param dev_handle    Device handle of the device with the file.
61
 * @param firstc    First cluster to start the walk with.
61
 * @param firstc    First cluster to start the walk with.
62
 * @param penult    If non-NULL, output argument hodling the
62
 * @param lastc     If non-NULL, output argument hodling the last cluster number visited.
63
 *          the penultimate cluster visited.
-
 
64
 * @param ult       If non-NULL, output argument holding the
-
 
65
 *          ultimate cluster visited.
-
 
66
 * @param max_clusters  Maximum number of clusters to visit.   
63
 * @param max_clusters  Maximum number of clusters to visit.   
67
 *
64
 *
68
 * @return      Number of clusters seen during the walk.
65
 * @return      Number of clusters seen during the walk.
69
 */
66
 */
70
uint16_t
67
uint16_t
71
fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
68
fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
72
    fat_cluster_t *penult, fat_cluster_t *ult, uint16_t max_clusters)
69
    fat_cluster_t *lastc, uint16_t max_clusters)
73
{
70
{
74
    block_t *b;
71
    block_t *b;
75
    unsigned bps;
72
    unsigned bps;
76
    unsigned rscnt;     /* block address of the first FAT */
73
    unsigned rscnt;     /* block address of the first FAT */
77
    uint16_t clusters = 0;
74
    uint16_t clusters = 0;
Line 80... Line 77...
80
    bps = uint16_t_le2host(bs->bps);
77
    bps = uint16_t_le2host(bs->bps);
81
    rscnt = uint16_t_le2host(bs->rscnt);
78
    rscnt = uint16_t_le2host(bs->rscnt);
82
 
79
 
83
    if (firstc == FAT_CLST_RES0) {
80
    if (firstc == FAT_CLST_RES0) {
84
        /* No space allocated to the file. */
81
        /* No space allocated to the file. */
85
        if (ult)
82
        if (lastc)
86
            *ult = firstc;
83
            *lastc = firstc;
87
        return 0;
84
        return 0;
88
    }
85
    }
89
 
86
 
90
    /* At this point, the meaning of penult is not well-defined. */
-
 
91
    if (penult)
-
 
92
        *penult = FAT_CLST_RES0;
-
 
93
 
-
 
94
    while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
87
    while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
95
        unsigned fsec;  /* sector offset relative to FAT1 */
88
        bn_t fsec;  /* sector offset relative to FAT1 */
96
        unsigned fidx;  /* FAT1 entry index */
89
        unsigned fidx;  /* FAT1 entry index */
97
 
90
 
98
        assert(clst >= FAT_CLST_FIRST);
91
        assert(clst >= FAT_CLST_FIRST);
99
        if (penult)
92
        if (lastc)
100
            *penult = clst; /* remember the penultimate cluster */
93
            *lastc = clst;  /* remember the last cluster number */
101
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
102
        fidx = clst % (bps / sizeof(fat_cluster_t));
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
103
        /* read FAT1 */
96
        /* read FAT1 */
104
        b = block_get(dev_handle, rscnt + fsec);
97
        b = block_get(dev_handle, rscnt + fsec);
105
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
106
        assert(clst != FAT_CLST_BAD);
99
        assert(clst != FAT_CLST_BAD);
107
        block_put(b);
100
        block_put(b);
108
        clusters++;
101
        clusters++;
109
    }
102
    }
110
 
103
 
111
    if (ult)
104
    if (lastc && clst < FAT_CLST_LAST1)
112
        *ult = clst;
105
        *lastc = clst;
113
 
106
 
114
    return clusters;
107
    return clusters;
115
}
108
}
116
 
109
 
117
/** Read block from file located on a FAT file system.
110
/** Read block from file located on a FAT file system.
118
 *
111
 *
119
 * @param bs        Buffer holding the boot sector of the file system.
112
 * @param bs        Buffer holding the boot sector of the file system.
120
 * @param dev_handle    Device handle of the file system.
113
 * @param dev_handle    Device handle of the file system.
121
 * @param firstc    First cluster used by the file. Can be zero if the file
114
 * @param firstc    First cluster used by the file. Can be zero if the file
122
 *          is empty.
115
 *          is empty.
123
 * @param offset    Offset in blocks.
116
 * @param bn        Block number.
124
 *
117
 *
125
 * @return      Block structure holding the requested block.
118
 * @return      Block structure holding the requested block.
126
 */
119
 */
127
block_t *
120
block_t *
128
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
121
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
129
    off_t offset)
122
    bn_t bn)
130
{
123
{
131
    block_t *b;
124
    block_t *b;
132
    unsigned bps;
125
    unsigned bps;
133
    unsigned rscnt;     /* block address of the first FAT */
126
    unsigned rscnt;     /* block address of the first FAT */
134
    unsigned rde;
127
    unsigned rde;
135
    unsigned rds;       /* root directory size */
128
    unsigned rds;       /* root directory size */
136
    unsigned sf;
129
    unsigned sf;
137
    unsigned ssa;       /* size of the system area */
130
    unsigned ssa;       /* size of the system area */
138
    unsigned clusters, max_clusters;
131
    unsigned clusters, max_clusters;
139
    fat_cluster_t lastc, clst = firstc;
132
    fat_cluster_t lastc;
140
 
133
 
141
    bps = uint16_t_le2host(bs->bps);
134
    bps = uint16_t_le2host(bs->bps);
142
    rscnt = uint16_t_le2host(bs->rscnt);
135
    rscnt = uint16_t_le2host(bs->rscnt);
143
    rde = uint16_t_le2host(bs->root_ent_max);
136
    rde = uint16_t_le2host(bs->root_ent_max);
144
    sf = uint16_t_le2host(bs->sec_per_fat);
137
    sf = uint16_t_le2host(bs->sec_per_fat);
Line 147... Line 140...
147
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
140
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
148
    ssa = rscnt + bs->fatcnt * sf + rds;
141
    ssa = rscnt + bs->fatcnt * sf + rds;
149
 
142
 
150
    if (firstc == FAT_CLST_ROOT) {
143
    if (firstc == FAT_CLST_ROOT) {
151
        /* root directory special case */
144
        /* root directory special case */
152
        assert(offset < rds);
145
        assert(bn < rds);
153
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + offset);
146
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn);
154
        return b;
147
        return b;
155
    }
148
    }
156
 
149
 
157
    max_clusters = offset / bs->spc;
150
    max_clusters = bn / bs->spc;
158
    clusters = fat_cluster_walk(bs, dev_handle, firstc, NULL, &lastc,
151
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
159
        max_clusters);
152
        max_clusters);
160
    assert(clusters == max_clusters);
153
    assert(clusters == max_clusters);
161
 
154
 
162
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
155
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
163
        offset % bs->spc);
156
        bn % bs->spc);
164
 
157
 
165
    return b;
158
    return b;
166
}
159
}
167
 
160
 
168
 
-
 
169
/** Fill the gap between EOF and a new file position.
161
/** Fill the gap between EOF and a new file position.
170
 *
162
 *
171
 * @param bs        Buffer holding the boot sector for nodep.
163
 * @param bs        Buffer holding the boot sector for nodep.
172
 * @param nodep     FAT node with the gap.
164
 * @param nodep     FAT node with the gap.
173
 * @param mcl       First cluster in an independent cluster chain that will
165
 * @param mcl       First cluster in an independent cluster chain that will
Line 208... Line 200...
208
        b->dirty = true;        /* need to sync node */
200
        b->dirty = true;        /* need to sync node */
209
        block_put(b);
201
        block_put(b);
210
    }
202
    }
211
}
203
}
212
 
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
 
213
/** Mark cluster in one instance of FAT.
232
/** Set cluster in one instance of FAT.
214
 *
233
 *
215
 * @param bs        Buffer holding the boot sector for the file system.
234
 * @param bs        Buffer holding the boot sector for the file system.
216
 * @param dev_handle    Device handle for the file system.
235
 * @param dev_handle    Device handle for the file system.
217
 * @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.
218
 * @param clst      Cluster which is to be marked.
237
 * @param clst      Cluster which is to be set.
219
 * @param value     Value mark the cluster with.
238
 * @param value     Value to set the cluster with.
220
 */
239
 */
221
void
240
void
222
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,
223
    fat_cluster_t clst, fat_cluster_t value)
242
    fat_cluster_t clst, fat_cluster_t value)
224
{
243
{
225
    block_t *b;
244
    block_t *b;
226
    uint16_t bps;
245
    uint16_t bps;
227
    uint16_t rscnt;
246
    uint16_t rscnt;
Line 254... Line 273...
254
    uint8_t fatno;
273
    uint8_t fatno;
255
    unsigned c;
274
    unsigned c;
256
 
275
 
257
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
276
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
258
        for (c = 0; c < nclsts; c++) {
277
        for (c = 0; c < nclsts; c++) {
259
            fat_mark_cluster(bs, dev_handle, fatno, lifo[c],
278
            fat_set_cluster(bs, dev_handle, fatno, lifo[c],
260
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
279
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
261
        }
280
        }
262
    }
281
    }
263
}
282
}
264
 
283
 
Line 338... Line 357...
338
    /*
357
    /*
339
     * 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
340
     * we have allocated so far.
359
     * we have allocated so far.
341
     */
360
     */
342
    while (found--) {
361
    while (found--) {
343
        fat_mark_cluster(bs, dev_handle, FAT1, lifo[found],
362
        fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
344
            FAT_CLST_RES0);
363
            FAT_CLST_RES0);
345
    }
364
    }
346
   
365
   
347
    free(lifo);
366
    free(lifo);
348
    return ENOSPC;
367
    return ENOSPC;
349
}
368
}
350
 
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
 
351
/** 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.
352
 *
394
 *
353
 * @param bs        Buffer holding boot sector of the file system.
395
 * @param bs        Buffer holding the boot sector of the file system.
354
 * @param nodep     Node representing the file.
396
 * @param nodep     Node representing the file.
355
 * @param mcl       First cluster of the cluster chain to append.
397
 * @param mcl       First cluster of the cluster chain to append.
356
 */
398
 */
357
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)
358
{
400
{
359
    dev_handle_t dev_handle = nodep->idx->dev_handle;
401
    dev_handle_t dev_handle = nodep->idx->dev_handle;
360
    fat_cluster_t lcl;
402
    fat_cluster_t lcl;
361
    uint8_t fatno;
403
    uint8_t fatno;
362
 
404
 
363
    if (fat_cluster_walk(bs, nodep->idx->dev_handle, nodep->firstc, &lcl,
405
    if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
364
        NULL, (uint16_t) -1) == 0) {
406
        (uint16_t) -1) == 0) {
365
        /* No clusters allocated to the node yet. */
407
        /* No clusters allocated to the node yet. */
366
        nodep->firstc = host2uint16_t_le(mcl);
408
        nodep->firstc = host2uint16_t_le(mcl);
367
        nodep->dirty = true;        /* need to sync node */
409
        nodep->dirty = true;        /* need to sync node */
368
        return;
410
        return;
369
    }
411
    }
370
 
412
 
371
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
413
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
372
        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 chopped 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
    }
373
}
446
}
374
 
447
 
375
/**
448
/**
376
 * @}
449
 * @}
377
 */
450
 */