Subversion Repositories HelenOS

Rev

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

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