Subversion Repositories HelenOS

Rev

Rev 3516 | Rev 3518 | 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>
43
#include <errno.h>
44
#include <byteorder.h>
45
#include <align.h>
46
#include <assert.h>
3517 jermar 47
#include <futex.h>
3504 jermar 48
 
3517 jermar 49
/**
50
 * The fat_alloc_lock futex protects all copies of the File Allocation Table
51
 * during allocation of clusters. The lock does not have to be held durring
52
 * deallocation of clusters.
53
 */  
54
static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
55
 
3506 jermar 56
block_t *
3516 jermar 57
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
58
    off_t offset)
3506 jermar 59
{
60
    block_t *b;
61
    unsigned bps;
62
    unsigned spc;
63
    unsigned rscnt;     /* block address of the first FAT */
64
    unsigned fatcnt;
65
    unsigned rde;
66
    unsigned rds;       /* root directory size */
67
    unsigned sf;
68
    unsigned ssa;       /* size of the system area */
69
    unsigned clusters;
70
    fat_cluster_t clst = firstc;
71
    unsigned i;
72
 
3516 jermar 73
    bps = uint16_t_le2host(bs->bps);
74
    spc = bs->spc;
75
    rscnt = uint16_t_le2host(bs->rscnt);
76
    fatcnt = bs->fatcnt;
77
    rde = uint16_t_le2host(bs->root_ent_max);
78
    sf = uint16_t_le2host(bs->sec_per_fat);
3506 jermar 79
 
80
    rds = (sizeof(fat_dentry_t) * rde) / bps;
81
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
82
    ssa = rscnt + fatcnt * sf + rds;
83
 
84
    if (firstc == FAT_CLST_ROOT) {
85
        /* root directory special case */
86
        assert(offset < rds);
87
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
88
        return b;
89
    }
90
 
91
    clusters = offset / spc;
92
    for (i = 0; i < clusters; i++) {
93
        unsigned fsec;  /* sector offset relative to FAT1 */
94
        unsigned fidx;  /* FAT1 entry index */
95
 
96
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
97
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
98
        fidx = clst % (bps / sizeof(fat_cluster_t));
99
        /* read FAT1 */
100
        b = block_get(dev_handle, rscnt + fsec, bps);
101
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
102
        assert(clst != FAT_CLST_BAD);
103
        assert(clst < FAT_CLST_LAST1);
104
        block_put(b);
105
    }
106
 
107
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
108
        offset % spc, bps);
109
 
110
    return b;
111
}
112
 
113
/** Return number of blocks allocated to a file.
114
 *
3516 jermar 115
 * @param bs        Buffer holding the boot sector for the file.
3506 jermar 116
 * @param dev_handle    Device handle of the device with the file.
117
 * @param firstc    First cluster of the file.
3513 jermar 118
 * @param lastc     If non-NULL, output argument holding the
119
 *          last cluster.
3506 jermar 120
 *
121
 * @return      Number of blocks allocated to the file.
122
 */
123
uint16_t
3516 jermar 124
_fat_blcks_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
3513 jermar 125
    fat_cluster_t *lastc)
3506 jermar 126
{
127
    block_t *b;
128
    unsigned bps;
129
    unsigned spc;
130
    unsigned rscnt;     /* block address of the first FAT */
131
    unsigned clusters = 0;
132
    fat_cluster_t clst = firstc;
133
 
3516 jermar 134
    bps = uint16_t_le2host(bs->bps);
135
    spc = bs->spc;
136
    rscnt = uint16_t_le2host(bs->rscnt);
3506 jermar 137
 
138
    if (firstc == FAT_CLST_RES0) {
139
        /* No space allocated to the file. */
3513 jermar 140
        if (lastc)
141
            *lastc = firstc;
3506 jermar 142
        return 0;
143
    }
144
 
145
    while (clst < FAT_CLST_LAST1) {
146
        unsigned fsec;  /* sector offset relative to FAT1 */
147
        unsigned fidx;  /* FAT1 entry index */
148
 
149
        assert(clst >= FAT_CLST_FIRST);
3513 jermar 150
        if (lastc)
151
            *lastc = clst;      /* remember the last cluster */
3506 jermar 152
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
153
        fidx = clst % (bps / sizeof(fat_cluster_t));
154
        /* read FAT1 */
155
        b = block_get(dev_handle, rscnt + fsec, bps);
156
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
157
        assert(clst != FAT_CLST_BAD);
158
        block_put(b);
159
        clusters++;
160
    }
161
 
3513 jermar 162
    if (lastc)
163
        *lastc = clst;
3506 jermar 164
    return clusters * spc;
165
}
166
 
167
/** Fill the gap between EOF and a new file position.
168
 *
3516 jermar 169
 * @param bs        Buffer holding the boot sector for nodep.
3506 jermar 170
 * @param nodep     FAT node with the gap.
171
 * @param mcl       First cluster in an independent cluster chain that will
172
 *          be later appended to the end of the node's own cluster
173
 *          chain. If pos is still in the last allocated cluster,
174
 *          this argument is ignored.
175
 * @param pos       Position in the last node block.
176
 */
3516 jermar 177
void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
3506 jermar 178
{
179
    uint16_t bps;
180
    unsigned spc;
3516 jermar 181
    block_t *b;
3506 jermar 182
    off_t o, boundary;
183
 
3516 jermar 184
    bps = uint16_t_le2host(bs->bps);
185
    spc = bs->spc;
3506 jermar 186
 
187
    boundary = ROUND_UP(nodep->size, bps * spc);
188
 
189
    /* zero out already allocated space */
190
    for (o = nodep->size - 1; o < pos && o < boundary;
191
        o = ALIGN_DOWN(o + bps, bps)) {
3516 jermar 192
        b = fat_block_get(bs, nodep, o / bps);
3506 jermar 193
        memset(b->data + o % bps, 0, bps - o % bps);
194
        b->dirty = true;        /* need to sync node */
195
        block_put(b);
196
    }
197
 
198
    if (o >= pos)
199
        return;
200
 
201
    /* zero out the initial part of the new cluster chain */
202
    for (o = boundary; o < pos; o += bps) {
3516 jermar 203
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
3506 jermar 204
            (o - boundary) / bps);
205
        memset(b->data, 0, min(bps, pos - o));
206
        b->dirty = true;        /* need to sync node */
207
        block_put(b);
208
    }
209
}
210
 
211
void
3516 jermar 212
fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
213
    fat_cluster_t clst, fat_cluster_t value)
3506 jermar 214
{
3516 jermar 215
    block_t *b;
3510 jermar 216
    uint16_t bps;
217
    uint16_t rscnt;
218
    uint16_t sf;
219
    fat_cluster_t *cp;
220
 
3516 jermar 221
    bps = uint16_t_le2host(bs->bps);
222
    rscnt = uint16_t_le2host(bs->rscnt);
223
    sf = uint16_t_le2host(bs->sec_per_fat);
3510 jermar 224
 
3516 jermar 225
    assert(fatno < bs->fatcnt);
226
    b = block_get(dev_handle, rscnt + sf * fatno +
3510 jermar 227
        (clst * sizeof(fat_cluster_t)) / bps, bps);
3516 jermar 228
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
3510 jermar 229
    *cp = host2uint16_t_le(value);
3516 jermar 230
    b->dirty = true;        /* need to sync block */
231
    block_put(b);
3506 jermar 232
}
233
 
3516 jermar 234
void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
235
    fat_cluster_t *lifo, unsigned nclsts)
3506 jermar 236
{
3507 jermar 237
    uint8_t fatno;
238
    unsigned c;
239
 
3516 jermar 240
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
3507 jermar 241
        for (c = 0; c < nclsts; c++) {
3516 jermar 242
            fat_mark_cluster(bs, dev_handle, fatno, lifo[c],
3507 jermar 243
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
244
        }
245
    }
3506 jermar 246
}
247
 
248
int
3516 jermar 249
fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
250
    fat_cluster_t *mcl, fat_cluster_t *lcl)
3506 jermar 251
{
252
    uint16_t bps;
253
    uint16_t rscnt;
254
    uint16_t sf;
3516 jermar 255
    block_t *blk;
3506 jermar 256
    fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
257
    unsigned found = 0; /* top of the free cluster number stack */
258
    unsigned b, c, cl;
259
 
260
    lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
261
    if (lifo)
262
        return ENOMEM;
263
 
3516 jermar 264
    bps = uint16_t_le2host(bs->bps);
265
    rscnt = uint16_t_le2host(bs->rscnt);
266
    sf = uint16_t_le2host(bs->sec_per_fat);
3506 jermar 267
 
268
    /*
269
     * Search FAT1 for unused clusters.
270
     */
3517 jermar 271
    futex_down(&fat_alloc_lock);
3506 jermar 272
    for (b = 0, cl = 0; b < sf; blk++) {
273
        blk = block_get(dev_handle, rscnt + b, bps);
274
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
275
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
3512 jermar 276
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
3506 jermar 277
                /*
278
                 * The cluster is free. Put it into our stack
279
                 * of found clusters and mark it as non-free.
280
                 */
281
                lifo[found] = cl;
3512 jermar 282
                *clst = (found == 0) ?
283
                    host2uint16_t_le(FAT_CLST_LAST1) :
284
                    host2uint16_t_le(lifo[found - 1]);
3506 jermar 285
                blk->dirty = true;  /* need to sync block */
286
                if (++found == nclsts) {
287
                    /* we are almost done */
288
                    block_put(blk);
289
                    /* update the shadow copies of FAT */
3516 jermar 290
                    fat_alloc_shadow_clusters(bs,
291
                        dev_handle, lifo, nclsts);
3506 jermar 292
                    *mcl = lifo[found - 1];
293
                    *lcl = lifo[0];
294
                    free(lifo);
3517 jermar 295
                    futex_up(&fat_alloc_lock);
3506 jermar 296
                    return EOK;
297
                }
298
            }
299
        }
300
        block_put(blk);
301
    }
3517 jermar 302
    futex_up(&fat_alloc_lock);
3506 jermar 303
 
304
    /*
305
     * We could not find enough clusters. Now we need to free the clusters
306
     * we have allocated so far.
307
     */
3516 jermar 308
    while (found--) {
309
        fat_mark_cluster(bs, dev_handle, FAT1, lifo[found],
310
            FAT_CLST_RES0);
311
    }
3506 jermar 312
 
313
    free(lifo);
314
    return ENOSPC;
315
}
316
 
3516 jermar 317
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
3506 jermar 318
{
3516 jermar 319
    dev_handle_t dev_handle = nodep->idx->dev_handle;
3513 jermar 320
    fat_cluster_t lcl;
3516 jermar 321
    uint8_t fatno;
3513 jermar 322
 
3516 jermar 323
    if (_fat_blcks_get(bs, dev_handle, nodep->firstc, &lcl) == 0) {
3513 jermar 324
        nodep->firstc = host2uint16_t_le(mcl);
325
        nodep->dirty = true;        /* need to sync node */
326
        return;
327
    }
328
 
3516 jermar 329
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
330
        fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
3506 jermar 331
}
332
 
3504 jermar 333
/**
334
 * @}
335
 */