Subversion Repositories HelenOS

Rev

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

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