Subversion Repositories HelenOS

Rev

Rev 3551 | Rev 3572 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3504 jermar 1
/*
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup fs
30
 * @{
31
 */
32
 
33
/**
34
 * @file    fat_fat.c
3517 jermar 35
 * @brief   Functions that manipulate the File Allocation Tables.
3504 jermar 36
 */
37
 
3506 jermar 38
#include "fat_fat.h"
39
#include "fat_dentry.h"
40
#include "fat.h"
41
#include "../../vfs/vfs.h"
42
#include <libfs.h>
3521 jermar 43
#include <libblock.h>
3506 jermar 44
#include <errno.h>
45
#include <byteorder.h>
46
#include <align.h>
47
#include <assert.h>
3517 jermar 48
#include <futex.h>
3504 jermar 49
 
3517 jermar 50
/**
51
 * The fat_alloc_lock futex protects all copies of the File Allocation Table
52
 * during allocation of clusters. The lock does not have to be held durring
53
 * deallocation of clusters.
54
 */  
55
static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
56
 
3550 jermar 57
/** Walk the cluster chain.
3518 jermar 58
 *
3550 jermar 59
 * @param bs        Buffer holding the boot sector for the file.
60
 * @param dev_handle    Device handle of the device with the file.
61
 * @param firstc    First cluster to start the walk with.
3571 jermar 62
 * @param lastc     If non-NULL, output argument hodling the last cluster number visited.
3550 jermar 63
 * @param max_clusters  Maximum number of clusters to visit.   
3518 jermar 64
 *
3550 jermar 65
 * @return      Number of clusters seen during the walk.
3518 jermar 66
 */
3550 jermar 67
uint16_t
68
fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
3571 jermar 69
    fat_cluster_t *lastc, uint16_t max_clusters)
3506 jermar 70
{
71
    block_t *b;
72
    unsigned bps;
73
    unsigned rscnt;     /* block address of the first FAT */
3550 jermar 74
    uint16_t clusters = 0;
3506 jermar 75
    fat_cluster_t clst = firstc;
76
 
3516 jermar 77
    bps = uint16_t_le2host(bs->bps);
78
    rscnt = uint16_t_le2host(bs->rscnt);
3506 jermar 79
 
3550 jermar 80
    if (firstc == FAT_CLST_RES0) {
81
        /* No space allocated to the file. */
3571 jermar 82
        if (lastc)
83
            *lastc = firstc;
3550 jermar 84
        return 0;
3506 jermar 85
    }
86
 
3550 jermar 87
    while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
3571 jermar 88
        bn_t fsec;  /* sector offset relative to FAT1 */
3506 jermar 89
        unsigned fidx;  /* FAT1 entry index */
90
 
3550 jermar 91
        assert(clst >= FAT_CLST_FIRST);
3571 jermar 92
        if (lastc)
93
            *lastc = clst;  /* remember the last cluster number */
3506 jermar 94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
96
        /* read FAT1 */
3542 jermar 97
        b = block_get(dev_handle, rscnt + fsec);
3506 jermar 98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
99
        assert(clst != FAT_CLST_BAD);
100
        block_put(b);
3550 jermar 101
        clusters++;
3506 jermar 102
    }
103
 
3571 jermar 104
    if (lastc && clst < FAT_CLST_LAST1)
105
        *lastc = clst;
3506 jermar 106
 
3550 jermar 107
    return clusters;
3506 jermar 108
}
109
 
3550 jermar 110
/** Read block from file located on a FAT file system.
3506 jermar 111
 *
3550 jermar 112
 * @param bs        Buffer holding the boot sector of the file system.
113
 * @param dev_handle    Device handle of the file system.
114
 * @param firstc    First cluster used by the file. Can be zero if the file
115
 *          is empty.
3571 jermar 116
 * @param bn        Block number.
3506 jermar 117
 *
3550 jermar 118
 * @return      Block structure holding the requested block.
3506 jermar 119
 */
3550 jermar 120
block_t *
121
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
3571 jermar 122
    bn_t bn)
3506 jermar 123
{
124
    block_t *b;
125
    unsigned bps;
126
    unsigned rscnt;     /* block address of the first FAT */
3550 jermar 127
    unsigned rde;
128
    unsigned rds;       /* root directory size */
129
    unsigned sf;
130
    unsigned ssa;       /* size of the system area */
131
    unsigned clusters, max_clusters;
132
    fat_cluster_t lastc, clst = firstc;
3506 jermar 133
 
3516 jermar 134
    bps = uint16_t_le2host(bs->bps);
135
    rscnt = uint16_t_le2host(bs->rscnt);
3550 jermar 136
    rde = uint16_t_le2host(bs->root_ent_max);
137
    sf = uint16_t_le2host(bs->sec_per_fat);
3506 jermar 138
 
3550 jermar 139
    rds = (sizeof(fat_dentry_t) * rde) / bps;
140
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
141
    ssa = rscnt + bs->fatcnt * sf + rds;
142
 
143
    if (firstc == FAT_CLST_ROOT) {
144
        /* root directory special case */
3571 jermar 145
        assert(bn < rds);
146
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn);
3550 jermar 147
        return b;
3506 jermar 148
    }
149
 
3571 jermar 150
    max_clusters = bn / bs->spc;
151
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
3550 jermar 152
        max_clusters);
153
    assert(clusters == max_clusters);
3506 jermar 154
 
3550 jermar 155
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
3571 jermar 156
        bn % bs->spc);
3506 jermar 157
 
3550 jermar 158
    return b;
3506 jermar 159
}
160
 
3550 jermar 161
 
3506 jermar 162
/** Fill the gap between EOF and a new file position.
163
 *
3516 jermar 164
 * @param bs        Buffer holding the boot sector for nodep.
3506 jermar 165
 * @param nodep     FAT node with the gap.
166
 * @param mcl       First cluster in an independent cluster chain that will
167
 *          be later appended to the end of the node's own cluster
168
 *          chain. If pos is still in the last allocated cluster,
169
 *          this argument is ignored.
170
 * @param pos       Position in the last node block.
171
 */
3516 jermar 172
void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
3506 jermar 173
{
174
    uint16_t bps;
175
    unsigned spc;
3516 jermar 176
    block_t *b;
3506 jermar 177
    off_t o, boundary;
178
 
3516 jermar 179
    bps = uint16_t_le2host(bs->bps);
180
    spc = bs->spc;
3506 jermar 181
 
182
    boundary = ROUND_UP(nodep->size, bps * spc);
183
 
184
    /* zero out already allocated space */
185
    for (o = nodep->size - 1; o < pos && o < boundary;
186
        o = ALIGN_DOWN(o + bps, bps)) {
3516 jermar 187
        b = fat_block_get(bs, nodep, o / bps);
3506 jermar 188
        memset(b->data + o % bps, 0, bps - o % bps);
189
        b->dirty = true;        /* need to sync node */
190
        block_put(b);
191
    }
192
 
193
    if (o >= pos)
194
        return;
195
 
196
    /* zero out the initial part of the new cluster chain */
197
    for (o = boundary; o < pos; o += bps) {
3516 jermar 198
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
3506 jermar 199
            (o - boundary) / bps);
200
        memset(b->data, 0, min(bps, pos - o));
201
        b->dirty = true;        /* need to sync node */
202
        block_put(b);
203
    }
204
}
205
 
3518 jermar 206
/** Mark cluster in one instance of FAT.
207
 *
208
 * @param bs        Buffer holding the boot sector for the file system.
209
 * @param dev_handle    Device handle for the file system.
210
 * @param fatno     Number of the FAT instance where to make the change.
211
 * @param clst      Cluster which is to be marked.
212
 * @param value     Value mark the cluster with.
213
 */
3506 jermar 214
void
3516 jermar 215
fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
216
    fat_cluster_t clst, fat_cluster_t value)
3506 jermar 217
{
3516 jermar 218
    block_t *b;
3510 jermar 219
    uint16_t bps;
220
    uint16_t rscnt;
221
    uint16_t sf;
222
    fat_cluster_t *cp;
223
 
3516 jermar 224
    bps = uint16_t_le2host(bs->bps);
225
    rscnt = uint16_t_le2host(bs->rscnt);
226
    sf = uint16_t_le2host(bs->sec_per_fat);
3510 jermar 227
 
3516 jermar 228
    assert(fatno < bs->fatcnt);
229
    b = block_get(dev_handle, rscnt + sf * fatno +
3542 jermar 230
        (clst * sizeof(fat_cluster_t)) / bps);
3516 jermar 231
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
3510 jermar 232
    *cp = host2uint16_t_le(value);
3516 jermar 233
    b->dirty = true;        /* need to sync block */
234
    block_put(b);
3506 jermar 235
}
236
 
3518 jermar 237
/** Replay the allocatoin of clusters in all shadow instances of FAT.
238
 *
239
 * @param bs        Buffer holding the boot sector of the file system.
240
 * @param dev_handle    Device handle of the file system.
241
 * @param lifo      Chain of allocated clusters.
242
 * @param nclsts    Number of clusters in the lifo chain.
243
 */
3516 jermar 244
void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
245
    fat_cluster_t *lifo, unsigned nclsts)
3506 jermar 246
{
3507 jermar 247
    uint8_t fatno;
248
    unsigned c;
249
 
3516 jermar 250
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
3507 jermar 251
        for (c = 0; c < nclsts; c++) {
3516 jermar 252
            fat_mark_cluster(bs, dev_handle, fatno, lifo[c],
3507 jermar 253
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
254
        }
255
    }
3506 jermar 256
}
257
 
3518 jermar 258
/** Allocate clusters in FAT1.
259
 *
260
 * This function will attempt to allocate the requested number of clusters in
261
 * the first FAT instance.  The FAT will be altered so that the allocated
262
 * clusters form an independent chain (i.e. a chain which does not belong to any
263
 * file yet).
264
 *
265
 * @param bs        Buffer holding the boot sector of the file system.
266
 * @param dev_handle    Device handle of the file system.
267
 * @param nclsts    Number of clusters to allocate.
268
 * @param mcl       Output parameter where the first cluster in the chain
269
 *          will be returned.
270
 * @param lcl       Output parameter where the last cluster in the chain
271
 *          will be returned.
272
 *
273
 * @return      EOK on success, a negative error code otherwise.
274
 */
3506 jermar 275
int
3516 jermar 276
fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
277
    fat_cluster_t *mcl, fat_cluster_t *lcl)
3506 jermar 278
{
279
    uint16_t bps;
280
    uint16_t rscnt;
281
    uint16_t sf;
3516 jermar 282
    block_t *blk;
3506 jermar 283
    fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
284
    unsigned found = 0; /* top of the free cluster number stack */
285
    unsigned b, c, cl;
286
 
287
    lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
288
    if (lifo)
289
        return ENOMEM;
290
 
3516 jermar 291
    bps = uint16_t_le2host(bs->bps);
292
    rscnt = uint16_t_le2host(bs->rscnt);
293
    sf = uint16_t_le2host(bs->sec_per_fat);
3506 jermar 294
 
295
    /*
296
     * Search FAT1 for unused clusters.
297
     */
3517 jermar 298
    futex_down(&fat_alloc_lock);
3506 jermar 299
    for (b = 0, cl = 0; b < sf; blk++) {
3542 jermar 300
        blk = block_get(dev_handle, rscnt + b);
3506 jermar 301
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
302
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
3512 jermar 303
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
3506 jermar 304
                /*
305
                 * The cluster is free. Put it into our stack
306
                 * of found clusters and mark it as non-free.
307
                 */
308
                lifo[found] = cl;
3512 jermar 309
                *clst = (found == 0) ?
310
                    host2uint16_t_le(FAT_CLST_LAST1) :
311
                    host2uint16_t_le(lifo[found - 1]);
3506 jermar 312
                blk->dirty = true;  /* need to sync block */
313
                if (++found == nclsts) {
314
                    /* we are almost done */
315
                    block_put(blk);
316
                    /* update the shadow copies of FAT */
3516 jermar 317
                    fat_alloc_shadow_clusters(bs,
318
                        dev_handle, lifo, nclsts);
3506 jermar 319
                    *mcl = lifo[found - 1];
320
                    *lcl = lifo[0];
321
                    free(lifo);
3517 jermar 322
                    futex_up(&fat_alloc_lock);
3506 jermar 323
                    return EOK;
324
                }
325
            }
326
        }
327
        block_put(blk);
328
    }
3517 jermar 329
    futex_up(&fat_alloc_lock);
3506 jermar 330
 
331
    /*
332
     * We could not find enough clusters. Now we need to free the clusters
333
     * we have allocated so far.
334
     */
3516 jermar 335
    while (found--) {
336
        fat_mark_cluster(bs, dev_handle, FAT1, lifo[found],
337
            FAT_CLST_RES0);
338
    }
3506 jermar 339
 
340
    free(lifo);
341
    return ENOSPC;
342
}
343
 
3518 jermar 344
/** Append a cluster chain to the last file cluster in all FATs.
345
 *
346
 * @param bs        Buffer holding boot sector of the file system.
347
 * @param nodep     Node representing the file.
348
 * @param mcl       First cluster of the cluster chain to append.
349
 */
3516 jermar 350
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
3506 jermar 351
{
3516 jermar 352
    dev_handle_t dev_handle = nodep->idx->dev_handle;
3513 jermar 353
    fat_cluster_t lcl;
3516 jermar 354
    uint8_t fatno;
3513 jermar 355
 
3551 jermar 356
    if (fat_cluster_walk(bs, nodep->idx->dev_handle, nodep->firstc, &lcl,
3571 jermar 357
        (uint16_t) -1) == 0) {
3550 jermar 358
        /* No clusters allocated to the node yet. */
3513 jermar 359
        nodep->firstc = host2uint16_t_le(mcl);
360
        nodep->dirty = true;        /* need to sync node */
361
        return;
362
    }
363
 
3516 jermar 364
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
365
        fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
3506 jermar 366
}
367
 
3504 jermar 368
/**
369
 * @}
370
 */