Subversion Repositories HelenOS

Rev

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

Rev 3536 Rev 3597
Line 52... Line 52...
52
 * during allocation of clusters. The lock does not have to be held durring
52
 * during allocation of clusters. The lock does not have to be held durring
53
 * deallocation of clusters.
53
 * deallocation of clusters.
54
 */  
54
 */  
55
static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
55
static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
56
 
56
 
57
/** Read block from file located on a FAT file system.
57
/** Walk the cluster chain.
58
 *
58
 *
59
 * @param bs        Buffer holding the boot sector of the file system.
59
 * @param bs        Buffer holding the boot sector for the file.
60
 * @param dev_handle    Device handle of the file system.
60
 * @param dev_handle    Device handle of the device with the file.
61
 * @param firstc    First cluster used by the file. Can be zero if the file
61
 * @param firstc    First cluster to start the walk with.
62
 *          is empty.
62
 * @param lastc     If non-NULL, output argument hodling the last cluster number visited.
63
 * @param offset    Offset in blocks.
63
 * @param max_clusters  Maximum number of clusters to visit.   
64
 *
64
 *
65
 * @return      Block structure holding the requested block.
65
 * @return      Number of clusters seen during the walk.
66
 */
66
 */
67
block_t *
67
uint16_t
68
_fat_block_get(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,
69
    off_t offset)
69
    fat_cluster_t *lastc, uint16_t max_clusters)
70
{
70
{
71
    block_t *b;
71
    block_t *b;
72
    unsigned bps;
72
    unsigned bps;
73
    unsigned spc;
-
 
74
    unsigned rscnt;     /* block address of the first FAT */
73
    unsigned rscnt;     /* block address of the first FAT */
75
    unsigned fatcnt;
-
 
76
    unsigned rde;
-
 
77
    unsigned rds;       /* root directory size */
-
 
78
    unsigned sf;
-
 
79
    unsigned ssa;       /* size of the system area */
-
 
80
    unsigned clusters;
74
    uint16_t clusters = 0;
81
    fat_cluster_t clst = firstc;
75
    fat_cluster_t clst = firstc;
82
    unsigned i;
-
 
83
 
76
 
84
    bps = uint16_t_le2host(bs->bps);
77
    bps = uint16_t_le2host(bs->bps);
85
    spc = bs->spc;
-
 
86
    rscnt = uint16_t_le2host(bs->rscnt);
78
    rscnt = uint16_t_le2host(bs->rscnt);
87
    fatcnt = bs->fatcnt;
-
 
88
    rde = uint16_t_le2host(bs->root_ent_max);
-
 
89
    sf = uint16_t_le2host(bs->sec_per_fat);
-
 
90
 
79
 
91
    rds = (sizeof(fat_dentry_t) * rde) / bps;
-
 
92
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
-
 
93
    ssa = rscnt + fatcnt * sf + rds;
-
 
94
 
-
 
95
    if (firstc == FAT_CLST_ROOT) {
80
    if (firstc == FAT_CLST_RES0) {
96
        /* root directory special case */
81
        /* No space allocated to the file. */
97
        assert(offset < rds);
82
        if (lastc)
98
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
83
            *lastc = firstc;
99
        return b;
84
        return 0;
100
    }
85
    }
101
 
86
 
102
    clusters = offset / spc;
-
 
103
    for (i = 0; i < clusters; i++) {
87
    while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
104
        unsigned fsec;  /* sector offset relative to FAT1 */
88
        bn_t fsec;  /* sector offset relative to FAT1 */
105
        unsigned fidx;  /* FAT1 entry index */
89
        unsigned fidx;  /* FAT1 entry index */
106
 
90
 
107
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
91
        assert(clst >= FAT_CLST_FIRST);
-
 
92
        if (lastc)
-
 
93
            *lastc = clst;  /* remember the last cluster number */
108
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
109
        fidx = clst % (bps / sizeof(fat_cluster_t));
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
110
        /* read FAT1 */
96
        /* read FAT1 */
111
        b = block_get(dev_handle, rscnt + fsec, bps);
97
        b = block_get(dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
112
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
113
        assert(clst != FAT_CLST_BAD);
99
        assert(clst != FAT_CLST_BAD);
114
        assert(clst < FAT_CLST_LAST1);
-
 
115
        block_put(b);
100
        block_put(b);
-
 
101
        clusters++;
116
    }
102
    }
117
 
103
 
118
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
104
    if (lastc && clst < FAT_CLST_LAST1)
119
        offset % spc, bps);
105
        *lastc = clst;
120
 
106
 
121
    return b;
107
    return clusters;
122
}
108
}
123
 
109
 
124
/** Return number of blocks allocated to a file.
110
/** Read block from file located on a FAT file system.
125
 *
111
 *
126
 * @param bs        Buffer holding the boot sector for the file.
112
 * @param bs        Buffer holding the boot sector of the file system.
127
 * @param dev_handle    Device handle of the device with the file.
113
 * @param dev_handle    Device handle of the file system.
128
 * @param firstc    First cluster of the file.
114
 * @param firstc    First cluster used by the file. Can be zero if the file
-
 
115
 *          is empty.
129
 * @param lastc     If non-NULL, output argument holding the
116
 * @param bn        Block number.
130
 *          last cluster.
117
 * @param flags     Flags passed to libblock.
131
 *
118
 *
132
 * @return      Number of blocks allocated to the file.
119
 * @return      Block structure holding the requested block.
133
 */
120
 */
134
uint16_t
121
block_t *
135
_fat_blcks_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
122
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
136
    fat_cluster_t *lastc)
123
    bn_t bn, int flags)
137
{
124
{
138
    block_t *b;
125
    block_t *b;
139
    unsigned bps;
126
    unsigned bps;
140
    unsigned spc;
-
 
141
    unsigned rscnt;     /* block address of the first FAT */
127
    unsigned rscnt;     /* block address of the first FAT */
-
 
128
    unsigned rde;
-
 
129
    unsigned rds;       /* root directory size */
-
 
130
    unsigned sf;
-
 
131
    unsigned ssa;       /* size of the system area */
142
    unsigned clusters = 0;
132
    unsigned clusters, max_clusters;
143
    fat_cluster_t clst = firstc;
133
    fat_cluster_t lastc;
144
 
134
 
145
    bps = uint16_t_le2host(bs->bps);
135
    bps = uint16_t_le2host(bs->bps);
146
    spc = bs->spc;
-
 
147
    rscnt = uint16_t_le2host(bs->rscnt);
136
    rscnt = uint16_t_le2host(bs->rscnt);
-
 
137
    rde = uint16_t_le2host(bs->root_ent_max);
-
 
138
    sf = uint16_t_le2host(bs->sec_per_fat);
148
 
139
 
-
 
140
    rds = (sizeof(fat_dentry_t) * rde) / bps;
-
 
141
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
-
 
142
    ssa = rscnt + bs->fatcnt * sf + rds;
-
 
143
 
149
    if (firstc == FAT_CLST_RES0) {
144
    if (firstc == FAT_CLST_ROOT) {
150
        /* No space allocated to the file. */
145
        /* root directory special case */
151
        if (lastc)
146
        assert(bn < rds);
152
            *lastc = firstc;
147
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn, flags);
153
        return 0;
148
        return b;
154
    }
149
    }
155
 
150
 
156
    while (clst < FAT_CLST_LAST1) {
151
    max_clusters = bn / bs->spc;
157
        unsigned fsec;  /* sector offset relative to FAT1 */
152
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
-
 
153
        max_clusters);
158
        unsigned fidx;  /* FAT1 entry index */
154
    assert(clusters == max_clusters);
159
 
155
 
160
        assert(clst >= FAT_CLST_FIRST);
-
 
161
        if (lastc)
-
 
162
            *lastc = clst;      /* remember the last cluster */
-
 
163
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
-
 
164
        fidx = clst % (bps / sizeof(fat_cluster_t));
-
 
165
        /* read FAT1 */
-
 
166
        b = block_get(dev_handle, rscnt + fsec, bps);
156
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
167
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
-
 
168
        assert(clst != FAT_CLST_BAD);
157
        bn % bs->spc, flags);
169
        block_put(b);
-
 
170
        clusters++;
-
 
171
    }
-
 
172
 
158
 
173
    if (lastc)
-
 
174
        *lastc = clst;
-
 
175
    return clusters * spc;
159
    return b;
176
}
160
}
177
 
161
 
178
/** Fill the gap between EOF and a new file position.
162
/** Fill the gap between EOF and a new file position.
179
 *
163
 *
180
 * @param bs        Buffer holding the boot sector for nodep.
164
 * @param bs        Buffer holding the boot sector for nodep.
Line 198... Line 182...
198
    boundary = ROUND_UP(nodep->size, bps * spc);
182
    boundary = ROUND_UP(nodep->size, bps * spc);
199
 
183
 
200
    /* zero out already allocated space */
184
    /* zero out already allocated space */
201
    for (o = nodep->size - 1; o < pos && o < boundary;
185
    for (o = nodep->size - 1; o < pos && o < boundary;
202
        o = ALIGN_DOWN(o + bps, bps)) {
186
        o = ALIGN_DOWN(o + bps, bps)) {
-
 
187
            int flags = (o % bps == 0) ?
-
 
188
            BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
203
        b = fat_block_get(bs, nodep, o / bps);
189
        b = fat_block_get(bs, nodep, o / bps, flags);
204
        memset(b->data + o % bps, 0, bps - o % bps);
190
        memset(b->data + o % bps, 0, bps - o % bps);
205
        b->dirty = true;        /* need to sync node */
191
        b->dirty = true;        /* need to sync node */
206
        block_put(b);
192
        block_put(b);
207
    }
193
    }
208
   
194
   
Line 210... Line 196...
210
        return;
196
        return;
211
   
197
   
212
    /* zero out the initial part of the new cluster chain */
198
    /* zero out the initial part of the new cluster chain */
213
    for (o = boundary; o < pos; o += bps) {
199
    for (o = boundary; o < pos; o += bps) {
214
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
200
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
215
            (o - boundary) / bps);
201
            (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
216
        memset(b->data, 0, min(bps, pos - o));
202
        memset(b->data, 0, min(bps, pos - o));
217
        b->dirty = true;        /* need to sync node */
203
        b->dirty = true;        /* need to sync node */
218
        block_put(b);
204
        block_put(b);
219
    }
205
    }
220
}
206
}
221
 
207
 
-
 
208
/** Get cluster from the first FAT.
-
 
209
 *
-
 
210
 * @param bs        Buffer holding the boot sector for the file system.
-
 
211
 * @param dev_handle    Device handle for the file system.
-
 
212
 * @param clst      Cluster which to get.
-
 
213
 *
-
 
214
 * @return      Value found in the cluster.
-
 
215
 */
-
 
216
fat_cluster_t
-
 
217
fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst)
-
 
218
{
-
 
219
    block_t *b;
-
 
220
    uint16_t bps;
-
 
221
    uint16_t rscnt;
-
 
222
    fat_cluster_t *cp, value;
-
 
223
 
-
 
224
    bps = uint16_t_le2host(bs->bps);
-
 
225
    rscnt = uint16_t_le2host(bs->rscnt);
-
 
226
 
-
 
227
    b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps,
-
 
228
        BLOCK_FLAGS_NONE);
-
 
229
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
-
 
230
    value = uint16_t_le2host(*cp);
-
 
231
    block_put(b);
-
 
232
   
-
 
233
    return value;
-
 
234
}
-
 
235
 
222
/** Mark cluster in one instance of FAT.
236
/** Set cluster in one instance of FAT.
223
 *
237
 *
224
 * @param bs        Buffer holding the boot sector for the file system.
238
 * @param bs        Buffer holding the boot sector for the file system.
225
 * @param dev_handle    Device handle for the file system.
239
 * @param dev_handle    Device handle for the file system.
226
 * @param fatno     Number of the FAT instance where to make the change.
240
 * @param fatno     Number of the FAT instance where to make the change.
227
 * @param clst      Cluster which is to be marked.
241
 * @param clst      Cluster which is to be set.
228
 * @param value     Value mark the cluster with.
242
 * @param value     Value to set the cluster with.
229
 */
243
 */
230
void
244
void
231
fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
245
fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
232
    fat_cluster_t clst, fat_cluster_t value)
246
    fat_cluster_t clst, fat_cluster_t value)
233
{
247
{
234
    block_t *b;
248
    block_t *b;
235
    uint16_t bps;
249
    uint16_t bps;
236
    uint16_t rscnt;
250
    uint16_t rscnt;
Line 241... Line 255...
241
    rscnt = uint16_t_le2host(bs->rscnt);
255
    rscnt = uint16_t_le2host(bs->rscnt);
242
    sf = uint16_t_le2host(bs->sec_per_fat);
256
    sf = uint16_t_le2host(bs->sec_per_fat);
243
 
257
 
244
    assert(fatno < bs->fatcnt);
258
    assert(fatno < bs->fatcnt);
245
    b = block_get(dev_handle, rscnt + sf * fatno +
259
    b = block_get(dev_handle, rscnt + sf * fatno +
246
        (clst * sizeof(fat_cluster_t)) / bps, bps);
260
        (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
247
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
261
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
248
    *cp = host2uint16_t_le(value);
262
    *cp = host2uint16_t_le(value);
249
    b->dirty = true;        /* need to sync block */
263
    b->dirty = true;        /* need to sync block */
250
    block_put(b);
264
    block_put(b);
251
}
265
}
Line 263... Line 277...
263
    uint8_t fatno;
277
    uint8_t fatno;
264
    unsigned c;
278
    unsigned c;
265
 
279
 
266
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
280
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
267
        for (c = 0; c < nclsts; c++) {
281
        for (c = 0; c < nclsts; c++) {
268
            fat_mark_cluster(bs, dev_handle, fatno, lifo[c],
282
            fat_set_cluster(bs, dev_handle, fatno, lifo[c],
269
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
283
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
270
        }
284
        }
271
    }
285
    }
272
}
286
}
273
 
287
 
Line 311... Line 325...
311
    /*
325
    /*
312
     * Search FAT1 for unused clusters.
326
     * Search FAT1 for unused clusters.
313
     */
327
     */
314
    futex_down(&fat_alloc_lock);
328
    futex_down(&fat_alloc_lock);
315
    for (b = 0, cl = 0; b < sf; blk++) {
329
    for (b = 0, cl = 0; b < sf; blk++) {
316
        blk = block_get(dev_handle, rscnt + b, bps);
330
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NOREAD);
317
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
331
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
318
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
332
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
319
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
333
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
320
                /*
334
                /*
321
                 * The cluster is free. Put it into our stack
335
                 * The cluster is free. Put it into our stack
Line 347... Line 361...
347
    /*
361
    /*
348
     * We could not find enough clusters. Now we need to free the clusters
362
     * We could not find enough clusters. Now we need to free the clusters
349
     * we have allocated so far.
363
     * we have allocated so far.
350
     */
364
     */
351
    while (found--) {
365
    while (found--) {
352
        fat_mark_cluster(bs, dev_handle, FAT1, lifo[found],
366
        fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
353
            FAT_CLST_RES0);
367
            FAT_CLST_RES0);
354
    }
368
    }
355
   
369
   
356
    free(lifo);
370
    free(lifo);
357
    return ENOSPC;
371
    return ENOSPC;
358
}
372
}
359
 
373
 
-
 
374
/** Free clusters forming a cluster chain in all copies of FAT.
-
 
375
 *
-
 
376
 * @param bs        Buffer hodling the boot sector of the file system.
-
 
377
 * @param dev_handle    Device handle of the file system.
-
 
378
 * @param firstc    First cluster in the chain which is to be freed.
-
 
379
 */
-
 
380
void
-
 
381
fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
-
 
382
{
-
 
383
    unsigned fatno;
-
 
384
    fat_cluster_t nextc;
-
 
385
 
-
 
386
    /* Mark all clusters in the chain as free in all copies of FAT. */
-
 
387
    while (firstc < FAT_CLST_LAST1) {
-
 
388
        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++)
-
 
391
            fat_set_cluster(bs, dev_handle, fatno, firstc,
-
 
392
                FAT_CLST_RES0);
-
 
393
        firstc = nextc;
-
 
394
    }
-
 
395
}
-
 
396
 
360
/** Append a cluster chain to the last file cluster in all FATs.
397
/** Append a cluster chain to the last file cluster in all FATs.
361
 *
398
 *
362
 * @param bs        Buffer holding boot sector of the file system.
399
 * @param bs        Buffer holding the boot sector of the file system.
363
 * @param nodep     Node representing the file.
400
 * @param nodep     Node representing the file.
364
 * @param mcl       First cluster of the cluster chain to append.
401
 * @param mcl       First cluster of the cluster chain to append.
365
 */
402
 */
366
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
403
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
367
{
404
{
368
    dev_handle_t dev_handle = nodep->idx->dev_handle;
405
    dev_handle_t dev_handle = nodep->idx->dev_handle;
369
    fat_cluster_t lcl;
406
    fat_cluster_t lcl;
370
    uint8_t fatno;
407
    uint8_t fatno;
371
 
408
 
372
    if (_fat_blcks_get(bs, dev_handle, nodep->firstc, &lcl) == 0) {
409
    if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
-
 
410
        (uint16_t) -1) == 0) {
-
 
411
        /* No clusters allocated to the node yet. */
373
        nodep->firstc = host2uint16_t_le(mcl);
412
        nodep->firstc = host2uint16_t_le(mcl);
374
        nodep->dirty = true;        /* need to sync node */
413
        nodep->dirty = true;        /* need to sync node */
375
        return;
414
        return;
376
    }
415
    }
377
 
416
 
378
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
417
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
379
        fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
418
        fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
-
 
419
}
-
 
420
 
-
 
421
/** Chop off node clusters in all copies of FAT.
-
 
422
 *
-
 
423
 * @param bs        Buffer holding the boot sector of the file system.
-
 
424
 * @param nodep     FAT node where the chopping will take place.
-
 
425
 * @param lastc     Last cluster which will remain in the node. If this
-
 
426
 *          argument is FAT_CLST_RES0, then all clusters will
-
 
427
 *          be chopped off.
-
 
428
 */
-
 
429
void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
-
 
430
{
-
 
431
    dev_handle_t dev_handle = nodep->idx->dev_handle;
-
 
432
    if (lastc == FAT_CLST_RES0) {
-
 
433
        /* The node will have zero size and no clusters allocated. */
-
 
434
        fat_free_clusters(bs, dev_handle, nodep->firstc);
-
 
435
        nodep->firstc = FAT_CLST_RES0;
-
 
436
        nodep->dirty = true;        /* need to sync node */
-
 
437
    } else {
-
 
438
        fat_cluster_t nextc;
-
 
439
        unsigned fatno;
-
 
440
 
-
 
441
        nextc = fat_get_cluster(bs, dev_handle, lastc);
-
 
442
 
-
 
443
        /* Terminate the cluster chain in all copies of FAT. */
-
 
444
        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
-
 
445
            fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1);
-
 
446
 
-
 
447
        /* Free all following clusters. */
-
 
448
        fat_free_clusters(bs, dev_handle, nextc);
-
 
449
    }
380
}
450
}
381
 
451
 
382
/**
452
/**
383
 * @}
453
 * @}
384
 */
454
 */