Subversion Repositories HelenOS

Rev

Rev 3598 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3598 Rev 3674
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    fat_fat.c
34
 * @file    fat_fat.c
35
 * @brief   Functions that manipulate the File Allocation Tables.
35
 * @brief   Functions that manipulate the File Allocation Tables.
36
 */
36
 */
37
 
37
 
38
#include "fat_fat.h"
38
#include "fat_fat.h"
39
#include "fat_dentry.h"
39
#include "fat_dentry.h"
40
#include "fat.h"
40
#include "fat.h"
41
#include "../../vfs/vfs.h"
41
#include "../../vfs/vfs.h"
42
#include <libfs.h>
42
#include <libfs.h>
43
#include <libblock.h>
43
#include <libblock.h>
44
#include <errno.h>
44
#include <errno.h>
45
#include <byteorder.h>
45
#include <byteorder.h>
46
#include <align.h>
46
#include <align.h>
47
#include <assert.h>
47
#include <assert.h>
48
#include <futex.h>
48
#include <futex.h>
49
 
49
 
50
/**
50
/**
51
 * The fat_alloc_lock futex protects all copies of the File Allocation Table
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
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
/** 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 lastc     If non-NULL, output argument hodling the last cluster number visited.
62
 * @param lastc     If non-NULL, output argument hodling the last cluster number visited.
63
 * @param max_clusters  Maximum number of clusters to visit.   
63
 * @param max_clusters  Maximum number of clusters to visit.   
64
 *
64
 *
65
 * @return      Number of clusters seen during the walk.
65
 * @return      Number of clusters seen during the walk.
66
 */
66
 */
67
uint16_t
67
uint16_t
68
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,
69
    fat_cluster_t *lastc, uint16_t max_clusters)
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 rscnt;     /* block address of the first FAT */
73
    unsigned rscnt;     /* block address of the first FAT */
74
    uint16_t clusters = 0;
74
    uint16_t clusters = 0;
75
    fat_cluster_t clst = firstc;
75
    fat_cluster_t clst = firstc;
76
 
76
 
77
    bps = uint16_t_le2host(bs->bps);
77
    bps = uint16_t_le2host(bs->bps);
78
    rscnt = uint16_t_le2host(bs->rscnt);
78
    rscnt = uint16_t_le2host(bs->rscnt);
79
 
79
 
80
    if (firstc == FAT_CLST_RES0) {
80
    if (firstc == FAT_CLST_RES0) {
81
        /* No space allocated to the file. */
81
        /* No space allocated to the file. */
82
        if (lastc)
82
        if (lastc)
83
            *lastc = firstc;
83
            *lastc = firstc;
84
        return 0;
84
        return 0;
85
    }
85
    }
86
 
86
 
87
    while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
87
    while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
88
        bn_t fsec;  /* sector offset relative to FAT1 */
88
        bn_t fsec;  /* sector offset relative to FAT1 */
89
        unsigned fidx;  /* FAT1 entry index */
89
        unsigned fidx;  /* FAT1 entry index */
90
 
90
 
91
        assert(clst >= FAT_CLST_FIRST);
91
        assert(clst >= FAT_CLST_FIRST);
92
        if (lastc)
92
        if (lastc)
93
            *lastc = clst;  /* remember the last cluster number */
93
            *lastc = clst;  /* remember the last cluster number */
94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
94
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
95
        fidx = clst % (bps / sizeof(fat_cluster_t));
96
        /* read FAT1 */
96
        /* read FAT1 */
97
        b = block_get(dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
97
        b = block_get(dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
98
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
99
        assert(clst != FAT_CLST_BAD);
99
        assert(clst != FAT_CLST_BAD);
100
        block_put(b);
100
        block_put(b);
101
        clusters++;
101
        clusters++;
102
    }
102
    }
103
 
103
 
104
    if (lastc && clst < FAT_CLST_LAST1)
104
    if (lastc && clst < FAT_CLST_LAST1)
105
        *lastc = clst;
105
        *lastc = clst;
106
 
106
 
107
    return clusters;
107
    return clusters;
108
}
108
}
109
 
109
 
110
/** Read block from file located on a FAT file system.
110
/** Read block from file located on a FAT file system.
111
 *
111
 *
112
 * @param bs        Buffer holding the boot sector of the file system.
112
 * @param bs        Buffer holding the boot sector of the file system.
113
 * @param dev_handle    Device handle 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
114
 * @param firstc    First cluster used by the file. Can be zero if the file
115
 *          is empty.
115
 *          is empty.
116
 * @param bn        Block number.
116
 * @param bn        Block number.
117
 * @param flags     Flags passed to libblock.
117
 * @param flags     Flags passed to libblock.
118
 *
118
 *
119
 * @return      Block structure holding the requested block.
119
 * @return      Block structure holding the requested block.
120
 */
120
 */
121
block_t *
121
block_t *
122
_fat_block_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,
123
    bn_t bn, int flags)
123
    bn_t bn, int flags)
124
{
124
{
125
    block_t *b;
125
    block_t *b;
126
    unsigned bps;
126
    unsigned bps;
127
    unsigned rscnt;     /* block address of the first FAT */
127
    unsigned rscnt;     /* block address of the first FAT */
128
    unsigned rde;
128
    unsigned rde;
129
    unsigned rds;       /* root directory size */
129
    unsigned rds;       /* root directory size */
130
    unsigned sf;
130
    unsigned sf;
131
    unsigned ssa;       /* size of the system area */
131
    unsigned ssa;       /* size of the system area */
132
    unsigned clusters, max_clusters;
132
    unsigned clusters, max_clusters;
133
    fat_cluster_t lastc;
133
    fat_cluster_t lastc;
134
 
134
 
135
    bps = uint16_t_le2host(bs->bps);
135
    bps = uint16_t_le2host(bs->bps);
136
    rscnt = uint16_t_le2host(bs->rscnt);
136
    rscnt = uint16_t_le2host(bs->rscnt);
137
    rde = uint16_t_le2host(bs->root_ent_max);
137
    rde = uint16_t_le2host(bs->root_ent_max);
138
    sf = uint16_t_le2host(bs->sec_per_fat);
138
    sf = uint16_t_le2host(bs->sec_per_fat);
139
 
139
 
140
    rds = (sizeof(fat_dentry_t) * rde) / bps;
140
    rds = (sizeof(fat_dentry_t) * rde) / bps;
141
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
141
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
142
    ssa = rscnt + bs->fatcnt * sf + rds;
142
    ssa = rscnt + bs->fatcnt * sf + rds;
143
 
143
 
144
    if (firstc == FAT_CLST_ROOT) {
144
    if (firstc == FAT_CLST_ROOT) {
145
        /* root directory special case */
145
        /* root directory special case */
146
        assert(bn < rds);
146
        assert(bn < rds);
147
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn, flags);
147
        b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn, flags);
148
        return b;
148
        return b;
149
    }
149
    }
150
 
150
 
151
    max_clusters = bn / bs->spc;
151
    max_clusters = bn / bs->spc;
152
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
152
    clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
153
        max_clusters);
153
        max_clusters);
154
    assert(clusters == max_clusters);
154
    assert(clusters == max_clusters);
155
 
155
 
156
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
156
    b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
157
        bn % bs->spc, flags);
157
        bn % bs->spc, flags);
158
 
158
 
159
    return b;
159
    return b;
160
}
160
}
161
 
161
 
162
/** Fill the gap between EOF and a new file position.
162
/** Fill the gap between EOF and a new file position.
163
 *
163
 *
164
 * @param bs        Buffer holding the boot sector for nodep.
164
 * @param bs        Buffer holding the boot sector for nodep.
165
 * @param nodep     FAT node with the gap.
165
 * @param nodep     FAT node with the gap.
166
 * @param mcl       First cluster in an independent cluster chain that will
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
167
 *          be later appended to the end of the node's own cluster
168
 *          chain. If pos is still in the last allocated cluster,
168
 *          chain. If pos is still in the last allocated cluster,
169
 *          this argument is ignored.
169
 *          this argument is ignored.
170
 * @param pos       Position in the last node block.
170
 * @param pos       Position in the last node block.
171
 */
171
 */
172
void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
172
void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
173
{
173
{
174
    uint16_t bps;
174
    uint16_t bps;
175
    unsigned spc;
175
    unsigned spc;
176
    block_t *b;
176
    block_t *b;
177
    off_t o, boundary;
177
    off_t o, boundary;
178
 
178
 
179
    bps = uint16_t_le2host(bs->bps);
179
    bps = uint16_t_le2host(bs->bps);
180
    spc = bs->spc;
180
    spc = bs->spc;
181
   
181
   
182
    boundary = ROUND_UP(nodep->size, bps * spc);
182
    boundary = ROUND_UP(nodep->size, bps * spc);
183
 
183
 
184
    /* zero out already allocated space */
184
    /* zero out already allocated space */
185
    for (o = nodep->size - 1; o < pos && o < boundary;
185
    for (o = nodep->size; o < pos && o < boundary;
186
        o = ALIGN_DOWN(o + bps, bps)) {
186
        o = ALIGN_DOWN(o + bps, bps)) {
187
            int flags = (o % bps == 0) ?
187
            int flags = (o % bps == 0) ?
188
            BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
188
            BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
189
        b = fat_block_get(bs, nodep, o / bps, flags);
189
        b = fat_block_get(bs, nodep, o / bps, flags);
190
        memset(b->data + o % bps, 0, bps - o % bps);
190
        memset(b->data + o % bps, 0, bps - o % bps);
191
        b->dirty = true;        /* need to sync node */
191
        b->dirty = true;        /* need to sync node */
192
        block_put(b);
192
        block_put(b);
193
    }
193
    }
194
   
194
   
195
    if (o >= pos)
195
    if (o >= pos)
196
        return;
196
        return;
197
   
197
   
198
    /* zero out the initial part of the new cluster chain */
198
    /* zero out the initial part of the new cluster chain */
199
    for (o = boundary; o < pos; o += bps) {
199
    for (o = boundary; o < pos; o += bps) {
200
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
200
        b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
201
            (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
201
            (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
202
        memset(b->data, 0, min(bps, pos - o));
202
        memset(b->data, 0, min(bps, pos - o));
203
        b->dirty = true;        /* need to sync node */
203
        b->dirty = true;        /* need to sync node */
204
        block_put(b);
204
        block_put(b);
205
    }
205
    }
206
}
206
}
207
 
207
 
208
/** Get cluster from the first FAT.
208
/** Get cluster from the first FAT.
209
 *
209
 *
210
 * @param bs        Buffer holding the boot sector for the file system.
210
 * @param bs        Buffer holding the boot sector for the file system.
211
 * @param dev_handle    Device handle for the file system.
211
 * @param dev_handle    Device handle for the file system.
212
 * @param clst      Cluster which to get.
212
 * @param clst      Cluster which to get.
213
 *
213
 *
214
 * @return      Value found in the cluster.
214
 * @return      Value found in the cluster.
215
 */
215
 */
216
fat_cluster_t
216
fat_cluster_t
217
fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst)
217
fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst)
218
{
218
{
219
    block_t *b;
219
    block_t *b;
220
    uint16_t bps;
220
    uint16_t bps;
221
    uint16_t rscnt;
221
    uint16_t rscnt;
222
    fat_cluster_t *cp, value;
222
    fat_cluster_t *cp, value;
223
 
223
 
224
    bps = uint16_t_le2host(bs->bps);
224
    bps = uint16_t_le2host(bs->bps);
225
    rscnt = uint16_t_le2host(bs->rscnt);
225
    rscnt = uint16_t_le2host(bs->rscnt);
226
 
226
 
227
    b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps,
227
    b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps,
228
        BLOCK_FLAGS_NONE);
228
        BLOCK_FLAGS_NONE);
229
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
229
    cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
230
    value = uint16_t_le2host(*cp);
230
    value = uint16_t_le2host(*cp);
231
    block_put(b);
231
    block_put(b);
232
   
232
   
233
    return value;
233
    return value;
234
}
234
}
235
 
235
 
236
/** Set cluster in one instance of FAT.
236
/** Set cluster in one instance of FAT.
237
 *
237
 *
238
 * @param bs        Buffer holding the boot sector for the file system.
238
 * @param bs        Buffer holding the boot sector for the file system.
239
 * @param dev_handle    Device handle for the file system.
239
 * @param dev_handle    Device handle for the file system.
240
 * @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.
241
 * @param clst      Cluster which is to be set.
241
 * @param clst      Cluster which is to be set.
242
 * @param value     Value to set the cluster with.
242
 * @param value     Value to set the cluster with.
243
 */
243
 */
244
void
244
void
245
fat_set_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,
246
    fat_cluster_t clst, fat_cluster_t value)
246
    fat_cluster_t clst, fat_cluster_t value)
247
{
247
{
248
    block_t *b;
248
    block_t *b;
249
    uint16_t bps;
249
    uint16_t bps;
250
    uint16_t rscnt;
250
    uint16_t rscnt;
251
    uint16_t sf;
251
    uint16_t sf;
252
    fat_cluster_t *cp;
252
    fat_cluster_t *cp;
253
 
253
 
254
    bps = uint16_t_le2host(bs->bps);
254
    bps = uint16_t_le2host(bs->bps);
255
    rscnt = uint16_t_le2host(bs->rscnt);
255
    rscnt = uint16_t_le2host(bs->rscnt);
256
    sf = uint16_t_le2host(bs->sec_per_fat);
256
    sf = uint16_t_le2host(bs->sec_per_fat);
257
 
257
 
258
    assert(fatno < bs->fatcnt);
258
    assert(fatno < bs->fatcnt);
259
    b = block_get(dev_handle, rscnt + sf * fatno +
259
    b = block_get(dev_handle, rscnt + sf * fatno +
260
        (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
260
        (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
261
    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));
262
    *cp = host2uint16_t_le(value);
262
    *cp = host2uint16_t_le(value);
263
    b->dirty = true;        /* need to sync block */
263
    b->dirty = true;        /* need to sync block */
264
    block_put(b);
264
    block_put(b);
265
}
265
}
266
 
266
 
267
/** Replay the allocatoin of clusters in all shadow instances of FAT.
267
/** Replay the allocatoin of clusters in all shadow instances of FAT.
268
 *
268
 *
269
 * @param bs        Buffer holding the boot sector of the file system.
269
 * @param bs        Buffer holding the boot sector of the file system.
270
 * @param dev_handle    Device handle of the file system.
270
 * @param dev_handle    Device handle of the file system.
271
 * @param lifo      Chain of allocated clusters.
271
 * @param lifo      Chain of allocated clusters.
272
 * @param nclsts    Number of clusters in the lifo chain.
272
 * @param nclsts    Number of clusters in the lifo chain.
273
 */
273
 */
274
void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
274
void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
275
    fat_cluster_t *lifo, unsigned nclsts)
275
    fat_cluster_t *lifo, unsigned nclsts)
276
{
276
{
277
    uint8_t fatno;
277
    uint8_t fatno;
278
    unsigned c;
278
    unsigned c;
279
 
279
 
280
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
280
    for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
281
        for (c = 0; c < nclsts; c++) {
281
        for (c = 0; c < nclsts; c++) {
282
            fat_set_cluster(bs, dev_handle, fatno, lifo[c],
282
            fat_set_cluster(bs, dev_handle, fatno, lifo[c],
283
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
283
                c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
284
        }
284
        }
285
    }
285
    }
286
}
286
}
287
 
287
 
288
/** Allocate clusters in FAT1.
288
/** Allocate clusters in all copies of FAT.
289
 *
289
 *
290
 * This function will attempt to allocate the requested number of clusters in
290
 * This function will attempt to allocate the requested number of clusters in
291
 * the first FAT instance.  The FAT will be altered so that the allocated
291
 * all instances of the FAT.  The FAT will be altered so that the allocated
292
 * clusters form an independent chain (i.e. a chain which does not belong to any
292
 * clusters form an independent chain (i.e. a chain which does not belong to any
293
 * file yet).
293
 * file yet).
294
 *
294
 *
295
 * @param bs        Buffer holding the boot sector of the file system.
295
 * @param bs        Buffer holding the boot sector of the file system.
296
 * @param dev_handle    Device handle of the file system.
296
 * @param dev_handle    Device handle of the file system.
297
 * @param nclsts    Number of clusters to allocate.
297
 * @param nclsts    Number of clusters to allocate.
298
 * @param mcl       Output parameter where the first cluster in the chain
298
 * @param mcl       Output parameter where the first cluster in the chain
299
 *          will be returned.
299
 *          will be returned.
300
 * @param lcl       Output parameter where the last cluster in the chain
300
 * @param lcl       Output parameter where the last cluster in the chain
301
 *          will be returned.
301
 *          will be returned.
302
 *
302
 *
303
 * @return      EOK on success, a negative error code otherwise.
303
 * @return      EOK on success, a negative error code otherwise.
304
 */
304
 */
305
int
305
int
306
fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
306
fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
307
    fat_cluster_t *mcl, fat_cluster_t *lcl)
307
    fat_cluster_t *mcl, fat_cluster_t *lcl)
308
{
308
{
309
    uint16_t bps;
309
    uint16_t bps;
310
    uint16_t rscnt;
310
    uint16_t rscnt;
311
    uint16_t sf;
311
    uint16_t sf;
312
    block_t *blk;
312
    block_t *blk;
313
    fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
313
    fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
314
    unsigned found = 0; /* top of the free cluster number stack */
314
    unsigned found = 0; /* top of the free cluster number stack */
315
    unsigned b, c, cl;
315
    unsigned b, c, cl;
316
 
316
 
317
    lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
317
    lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
318
    if (lifo)
318
    if (!lifo)
319
        return ENOMEM;
319
        return ENOMEM;
320
   
320
   
321
    bps = uint16_t_le2host(bs->bps);
321
    bps = uint16_t_le2host(bs->bps);
322
    rscnt = uint16_t_le2host(bs->rscnt);
322
    rscnt = uint16_t_le2host(bs->rscnt);
323
    sf = uint16_t_le2host(bs->sec_per_fat);
323
    sf = uint16_t_le2host(bs->sec_per_fat);
324
   
324
   
325
    /*
325
    /*
326
     * Search FAT1 for unused clusters.
326
     * Search FAT1 for unused clusters.
327
     */
327
     */
328
    futex_down(&fat_alloc_lock);
328
    futex_down(&fat_alloc_lock);
329
    for (b = 0, cl = 0; b < sf; blk++) {
329
    for (b = 0, cl = 0; b < sf; b++) {
330
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NOREAD);
330
        blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
331
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
331
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
332
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
332
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
333
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
333
            if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
334
                /*
334
                /*
335
                 * The cluster is free. Put it into our stack
335
                 * The cluster is free. Put it into our stack
336
                 * of found clusters and mark it as non-free.
336
                 * of found clusters and mark it as non-free.
337
                 */
337
                 */
338
                lifo[found] = cl;
338
                lifo[found] = cl;
339
                *clst = (found == 0) ?
339
                *clst = (found == 0) ?
340
                    host2uint16_t_le(FAT_CLST_LAST1) :
340
                    host2uint16_t_le(FAT_CLST_LAST1) :
341
                    host2uint16_t_le(lifo[found - 1]);
341
                    host2uint16_t_le(lifo[found - 1]);
342
                blk->dirty = true;  /* need to sync block */
342
                blk->dirty = true;  /* need to sync block */
343
                if (++found == nclsts) {
343
                if (++found == nclsts) {
344
                    /* we are almost done */
344
                    /* we are almost done */
345
                    block_put(blk);
345
                    block_put(blk);
346
                    /* update the shadow copies of FAT */
346
                    /* update the shadow copies of FAT */
347
                    fat_alloc_shadow_clusters(bs,
347
                    fat_alloc_shadow_clusters(bs,
348
                        dev_handle, lifo, nclsts);
348
                        dev_handle, lifo, nclsts);
349
                    *mcl = lifo[found - 1];
349
                    *mcl = lifo[found - 1];
350
                    *lcl = lifo[0];
350
                    *lcl = lifo[0];
351
                    free(lifo);
351
                    free(lifo);
352
                    futex_up(&fat_alloc_lock);
352
                    futex_up(&fat_alloc_lock);
353
                    return EOK;
353
                    return EOK;
354
                }
354
                }
355
            }
355
            }
356
        }
356
        }
357
        block_put(blk);
357
        block_put(blk);
358
    }
358
    }
359
    futex_up(&fat_alloc_lock);
359
    futex_up(&fat_alloc_lock);
360
 
360
 
361
    /*
361
    /*
362
     * 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
363
     * we have allocated so far.
363
     * we have allocated so far.
364
     */
364
     */
365
    while (found--) {
365
    while (found--) {
366
        fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
366
        fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
367
            FAT_CLST_RES0);
367
            FAT_CLST_RES0);
368
    }
368
    }
369
   
369
   
370
    free(lifo);
370
    free(lifo);
371
    return ENOSPC;
371
    return ENOSPC;
372
}
372
}
373
 
373
 
374
/** Free clusters forming a cluster chain in all copies of FAT.
374
/** Free clusters forming a cluster chain in all copies of FAT.
375
 *
375
 *
376
 * @param bs        Buffer hodling the boot sector of the file system.
376
 * @param bs        Buffer hodling the boot sector of the file system.
377
 * @param dev_handle    Device handle 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.
378
 * @param firstc    First cluster in the chain which is to be freed.
379
 */
379
 */
380
void
380
void
381
fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
381
fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
382
{
382
{
383
    unsigned fatno;
383
    unsigned fatno;
384
    fat_cluster_t nextc;
384
    fat_cluster_t nextc;
385
 
385
 
386
    /* Mark all clusters in the chain as free in all copies of FAT. */
386
    /* Mark all clusters in the chain as free in all copies of FAT. */
387
    while (firstc < FAT_CLST_LAST1) {
387
    while (firstc < FAT_CLST_LAST1) {
-
 
388
        assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
388
        nextc = fat_get_cluster(bs, dev_handle, firstc);
389
        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++)
390
        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
391
            fat_set_cluster(bs, dev_handle, fatno, firstc,
391
            fat_set_cluster(bs, dev_handle, fatno, firstc,
392
                FAT_CLST_RES0);
392
                FAT_CLST_RES0);
393
        firstc = nextc;
393
        firstc = nextc;
394
    }
394
    }
395
}
395
}
396
 
396
 
397
/** 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.
398
 *
398
 *
399
 * @param bs        Buffer holding the boot sector of the file system.
399
 * @param bs        Buffer holding the boot sector of the file system.
400
 * @param nodep     Node representing the file.
400
 * @param nodep     Node representing the file.
401
 * @param mcl       First cluster of the cluster chain to append.
401
 * @param mcl       First cluster of the cluster chain to append.
402
 */
402
 */
403
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)
404
{
404
{
405
    dev_handle_t dev_handle = nodep->idx->dev_handle;
405
    dev_handle_t dev_handle = nodep->idx->dev_handle;
406
    fat_cluster_t lcl;
406
    fat_cluster_t lcl;
407
    uint8_t fatno;
407
    uint8_t fatno;
408
 
408
 
409
    if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
409
    if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
410
        (uint16_t) -1) == 0) {
410
        (uint16_t) -1) == 0) {
411
        /* No clusters allocated to the node yet. */
411
        /* No clusters allocated to the node yet. */
412
        nodep->firstc = host2uint16_t_le(mcl);
412
        nodep->firstc = mcl;
413
        nodep->dirty = true;        /* need to sync node */
413
        nodep->dirty = true;        /* need to sync node */
414
        return;
414
        return;
415
    }
415
    }
416
 
416
 
417
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
417
    for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
418
        fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
418
        fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
419
}
419
}
420
 
420
 
421
/** Chop off node clusters in all copies of FAT.
421
/** Chop off node clusters in all copies of FAT.
422
 *
422
 *
423
 * @param bs        Buffer holding the boot sector of the file system.
423
 * @param bs        Buffer holding the boot sector of the file system.
424
 * @param nodep     FAT node where the chopping will take place.
424
 * @param nodep     FAT node where the chopping will take place.
425
 * @param lastc     Last cluster which will remain in the node. If this
425
 * @param lastc     Last cluster which will remain in the node. If this
426
 *          argument is FAT_CLST_RES0, then all clusters will
426
 *          argument is FAT_CLST_RES0, then all clusters will
427
 *          be chopped off.
427
 *          be chopped off.
428
 */
428
 */
429
void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
429
void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
430
{
430
{
431
    dev_handle_t dev_handle = nodep->idx->dev_handle;
431
    dev_handle_t dev_handle = nodep->idx->dev_handle;
432
    if (lastc == FAT_CLST_RES0) {
432
    if (lastc == FAT_CLST_RES0) {
433
        /* The node will have zero size and no clusters allocated. */
433
        /* The node will have zero size and no clusters allocated. */
434
        fat_free_clusters(bs, dev_handle, nodep->firstc);
434
        fat_free_clusters(bs, dev_handle, nodep->firstc);
435
        nodep->firstc = FAT_CLST_RES0;
435
        nodep->firstc = FAT_CLST_RES0;
436
        nodep->dirty = true;        /* need to sync node */
436
        nodep->dirty = true;        /* need to sync node */
437
    } else {
437
    } else {
438
        fat_cluster_t nextc;
438
        fat_cluster_t nextc;
439
        unsigned fatno;
439
        unsigned fatno;
440
 
440
 
441
        nextc = fat_get_cluster(bs, dev_handle, lastc);
441
        nextc = fat_get_cluster(bs, dev_handle, lastc);
442
 
442
 
443
        /* Terminate the cluster chain in all copies of FAT. */
443
        /* Terminate the cluster chain in all copies of FAT. */
444
        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
444
        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
445
            fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1);
445
            fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1);
446
 
446
 
447
        /* Free all following clusters. */
447
        /* Free all following clusters. */
448
        fat_free_clusters(bs, dev_handle, nextc);
448
        fat_free_clusters(bs, dev_handle, nextc);
449
    }
449
    }
450
}
450
}
451
 
451
 
452
/**
452
/**
453
 * @}
453
 * @}
454
 */
454
 */
455
 
455