Subversion Repositories HelenOS

Rev

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

Rev 3505 Rev 3506
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_ops.c
34
 * @file    fat_ops.c
35
 * @brief   Implementation of VFS operations for the FAT file system server.
35
 * @brief   Implementation of VFS operations for the FAT file system server.
36
 */
36
 */
37
 
37
 
38
#include "fat.h"
38
#include "fat.h"
39
#include "fat_dentry.h"
39
#include "fat_dentry.h"
40
#include "fat_fat.h"
40
#include "fat_fat.h"
41
#include "../../vfs/vfs.h"
41
#include "../../vfs/vfs.h"
42
#include <libfs.h>
42
#include <libfs.h>
43
#include <ipc/ipc.h>
43
#include <ipc/ipc.h>
44
#include <ipc/services.h>
44
#include <ipc/services.h>
45
#include <ipc/devmap.h>
45
#include <ipc/devmap.h>
46
#include <async.h>
46
#include <async.h>
47
#include <errno.h>
47
#include <errno.h>
48
#include <string.h>
48
#include <string.h>
49
#include <byteorder.h>
49
#include <byteorder.h>
50
#include <libadt/hash_table.h>
50
#include <libadt/hash_table.h>
51
#include <libadt/list.h>
51
#include <libadt/list.h>
52
#include <assert.h>
52
#include <assert.h>
53
#include <futex.h>
53
#include <futex.h>
54
#include <sys/mman.h>
54
#include <sys/mman.h>
55
#include <align.h>
55
#include <align.h>
56
 
56
 
57
#define BS_BLOCK        0
-
 
58
#define BS_SIZE         512
-
 
59
 
-
 
60
/** Futex protecting the list of cached free FAT nodes. */
57
/** Futex protecting the list of cached free FAT nodes. */
61
static futex_t ffn_futex = FUTEX_INITIALIZER;
58
static futex_t ffn_futex = FUTEX_INITIALIZER;
62
 
59
 
63
/** List of cached free FAT nodes. */
60
/** List of cached free FAT nodes. */
64
static LIST_INITIALIZE(ffn_head);
61
static LIST_INITIALIZE(ffn_head);
65
 
62
 
66
#define min(a, b)       ((a) < (b) ? (a) : (b))
-
 
67
 
-
 
68
static int dev_phone = -1;      /* FIXME */
63
static int dev_phone = -1;      /* FIXME */
69
static void *dev_buffer = NULL;     /* FIXME */
64
static void *dev_buffer = NULL;     /* FIXME */
70
 
65
 
71
/* TODO move somewhere else */
-
 
72
typedef struct {
-
 
73
    void *data;
-
 
74
    size_t size;
-
 
75
    bool dirty;
-
 
76
} block_t;
-
 
77
 
-
 
78
static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
66
block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
79
{
67
{
80
    /* FIXME */
68
    /* FIXME */
81
    block_t *b;
69
    block_t *b;
82
    off_t bufpos = 0;
70
    off_t bufpos = 0;
83
    size_t buflen = 0;
71
    size_t buflen = 0;
84
    off_t pos = offset * bs;
72
    off_t pos = offset * bs;
85
 
73
 
86
    assert(dev_phone != -1);
74
    assert(dev_phone != -1);
87
    assert(dev_buffer);
75
    assert(dev_buffer);
88
 
76
 
89
    b = malloc(sizeof(block_t));
77
    b = malloc(sizeof(block_t));
90
    if (!b)
78
    if (!b)
91
        return NULL;
79
        return NULL;
92
   
80
   
93
    b->data = malloc(bs);
81
    b->data = malloc(bs);
94
    if (!b->data) {
82
    if (!b->data) {
95
        free(b);
83
        free(b);
96
        return NULL;
84
        return NULL;
97
    }
85
    }
98
    b->size = bs;
86
    b->size = bs;
99
 
87
 
100
    if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos,
88
    if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos,
101
        b->data, bs, bs)) {
89
        b->data, bs, bs)) {
102
        free(b->data);
90
        free(b->data);
103
        free(b);
91
        free(b);
104
        return NULL;
92
        return NULL;
105
    }
93
    }
106
 
94
 
107
    return b;
95
    return b;
108
}
96
}
109
 
97
 
110
static void block_put(block_t *block)
98
void block_put(block_t *block)
111
{
99
{
112
    /* FIXME */
100
    /* FIXME */
113
    free(block->data);
101
    free(block->data);
114
    free(block);
102
    free(block);
115
}
103
}
116
 
104
 
117
#define FAT1        0
-
 
118
 
-
 
119
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
-
 
120
 
-
 
121
#define FAT_CLST_RES0   0x0000
-
 
122
#define FAT_CLST_RES1   0x0001
-
 
123
#define FAT_CLST_FIRST  0x0002
-
 
124
#define FAT_CLST_BAD    0xfff7
-
 
125
#define FAT_CLST_LAST1  0xfff8
-
 
126
#define FAT_CLST_LAST8  0xffff
-
 
127
 
-
 
128
/* internally used to mark root directory's parent */
-
 
129
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
-
 
130
/* internally used to mark root directory */
-
 
131
#define FAT_CLST_ROOT       FAT_CLST_RES1
-
 
132
 
-
 
133
#define fat_block_get(np, off) \
-
 
134
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
-
 
135
 
-
 
136
static block_t *
-
 
137
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
-
 
138
{
-
 
139
    block_t *bb;
-
 
140
    block_t *b;
-
 
141
    unsigned bps;
-
 
142
    unsigned spc;
-
 
143
    unsigned rscnt;     /* block address of the first FAT */
-
 
144
    unsigned fatcnt;
-
 
145
    unsigned rde;
-
 
146
    unsigned rds;       /* root directory size */
-
 
147
    unsigned sf;
-
 
148
    unsigned ssa;       /* size of the system area */
-
 
149
    unsigned clusters;
-
 
150
    fat_cluster_t clst = firstc;
-
 
151
    unsigned i;
-
 
152
 
-
 
153
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
154
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
155
    spc = FAT_BS(bb)->spc;
-
 
156
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
-
 
157
    fatcnt = FAT_BS(bb)->fatcnt;
-
 
158
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
-
 
159
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
-
 
160
    block_put(bb);
-
 
161
 
-
 
162
    rds = (sizeof(fat_dentry_t) * rde) / bps;
-
 
163
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
-
 
164
    ssa = rscnt + fatcnt * sf + rds;
-
 
165
 
-
 
166
    if (firstc == FAT_CLST_ROOT) {
-
 
167
        /* root directory special case */
-
 
168
        assert(offset < rds);
-
 
169
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
-
 
170
        return b;
-
 
171
    }
-
 
172
 
-
 
173
    clusters = offset / spc;
-
 
174
    for (i = 0; i < clusters; i++) {
-
 
175
        unsigned fsec;  /* sector offset relative to FAT1 */
-
 
176
        unsigned fidx;  /* FAT1 entry index */
-
 
177
 
-
 
178
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
-
 
179
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
-
 
180
        fidx = clst % (bps / sizeof(fat_cluster_t));
-
 
181
        /* read FAT1 */
-
 
182
        b = block_get(dev_handle, rscnt + fsec, bps);
-
 
183
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
-
 
184
        assert(clst != FAT_CLST_BAD);
-
 
185
        assert(clst < FAT_CLST_LAST1);
-
 
186
        block_put(b);
-
 
187
    }
-
 
188
 
-
 
189
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
-
 
190
        offset % spc, bps);
-
 
191
 
-
 
192
    return b;
-
 
193
}
-
 
194
 
-
 
195
/** Return number of blocks allocated to a file.
-
 
196
 *
-
 
197
 * @param dev_handle    Device handle of the device with the file.
-
 
198
 * @param firstc    First cluster of the file.
-
 
199
 *
-
 
200
 * @return      Number of blocks allocated to the file.
-
 
201
 */
-
 
202
static uint16_t
-
 
203
_fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc)
-
 
204
{
-
 
205
    block_t *bb;
-
 
206
    block_t *b;
-
 
207
    unsigned bps;
-
 
208
    unsigned spc;
-
 
209
    unsigned rscnt;     /* block address of the first FAT */
-
 
210
    unsigned clusters = 0;
-
 
211
    fat_cluster_t clst = firstc;
-
 
212
 
-
 
213
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
214
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
215
    spc = FAT_BS(bb)->spc;
-
 
216
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
-
 
217
    block_put(bb);
-
 
218
 
-
 
219
    if (firstc == FAT_CLST_RES0) {
-
 
220
        /* No space allocated to the file. */
-
 
221
        return 0;
-
 
222
    }
-
 
223
 
-
 
224
    while (clst < FAT_CLST_LAST1) {
-
 
225
        unsigned fsec;  /* sector offset relative to FAT1 */
-
 
226
        unsigned fidx;  /* FAT1 entry index */
-
 
227
 
-
 
228
        assert(clst >= FAT_CLST_FIRST);
-
 
229
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
-
 
230
        fidx = clst % (bps / sizeof(fat_cluster_t));
-
 
231
        /* read FAT1 */
-
 
232
        b = block_get(dev_handle, rscnt + fsec, bps);
-
 
233
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
-
 
234
        assert(clst != FAT_CLST_BAD);
-
 
235
        block_put(b);
-
 
236
        clusters++;
-
 
237
    }
-
 
238
 
-
 
239
    return clusters * spc;
-
 
240
}
-
 
241
 
-
 
242
static void fat_node_initialize(fat_node_t *node)
105
static void fat_node_initialize(fat_node_t *node)
243
{
106
{
244
    futex_initialize(&node->lock, 1);
107
    futex_initialize(&node->lock, 1);
245
    node->idx = NULL;
108
    node->idx = NULL;
246
    node->type = 0;
109
    node->type = 0;
247
    link_initialize(&node->ffn_link);
110
    link_initialize(&node->ffn_link);
248
    node->size = 0;
111
    node->size = 0;
249
    node->lnkcnt = 0;
112
    node->lnkcnt = 0;
250
    node->refcnt = 0;
113
    node->refcnt = 0;
251
    node->dirty = false;
114
    node->dirty = false;
252
}
115
}
253
 
116
 
254
static uint16_t fat_bps_get(dev_handle_t dev_handle)
-
 
255
{
-
 
256
    block_t *bb;
-
 
257
    uint16_t bps;
-
 
258
   
-
 
259
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
260
    assert(bb != NULL);
-
 
261
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
262
    block_put(bb);
-
 
263
 
-
 
264
    return bps;
-
 
265
}
-
 
266
 
-
 
267
static void fat_node_sync(fat_node_t *node)
117
static void fat_node_sync(fat_node_t *node)
268
{
118
{
269
    /* TODO */
119
    /* TODO */
270
}
120
}
271
 
121
 
272
/** Internal version of fat_node_get().
122
/** Internal version of fat_node_get().
273
 *
123
 *
274
 * @param idxp      Locked index structure.
124
 * @param idxp      Locked index structure.
275
 */
125
 */
276
static void *fat_node_get_core(fat_idx_t *idxp)
126
static void *fat_node_get_core(fat_idx_t *idxp)
277
{
127
{
278
    block_t *b;
128
    block_t *b;
279
    fat_dentry_t *d;
129
    fat_dentry_t *d;
280
    fat_node_t *nodep = NULL;
130
    fat_node_t *nodep = NULL;
281
    unsigned bps;
131
    unsigned bps;
282
    unsigned dps;
132
    unsigned dps;
283
 
133
 
284
    if (idxp->nodep) {
134
    if (idxp->nodep) {
285
        /*
135
        /*
286
         * We are lucky.
136
         * We are lucky.
287
         * The node is already instantiated in memory.
137
         * The node is already instantiated in memory.
288
         */
138
         */
289
        futex_down(&idxp->nodep->lock);
139
        futex_down(&idxp->nodep->lock);
290
        if (!idxp->nodep->refcnt++)
140
        if (!idxp->nodep->refcnt++)
291
            list_remove(&idxp->nodep->ffn_link);
141
            list_remove(&idxp->nodep->ffn_link);
292
        futex_up(&idxp->nodep->lock);
142
        futex_up(&idxp->nodep->lock);
293
        return idxp->nodep;
143
        return idxp->nodep;
294
    }
144
    }
295
 
145
 
296
    /*
146
    /*
297
     * We must instantiate the node from the file system.
147
     * We must instantiate the node from the file system.
298
     */
148
     */
299
   
149
   
300
    assert(idxp->pfc);
150
    assert(idxp->pfc);
301
 
151
 
302
    futex_down(&ffn_futex);
152
    futex_down(&ffn_futex);
303
    if (!list_empty(&ffn_head)) {
153
    if (!list_empty(&ffn_head)) {
304
        /* Try to use a cached free node structure. */
154
        /* Try to use a cached free node structure. */
305
        fat_idx_t *idxp_tmp;
155
        fat_idx_t *idxp_tmp;
306
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
156
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
307
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
157
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
308
            goto skip_cache;
158
            goto skip_cache;
309
        idxp_tmp = nodep->idx;
159
        idxp_tmp = nodep->idx;
310
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
160
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
311
            futex_up(&nodep->lock);
161
            futex_up(&nodep->lock);
312
            goto skip_cache;
162
            goto skip_cache;
313
        }
163
        }
314
        list_remove(&nodep->ffn_link);
164
        list_remove(&nodep->ffn_link);
315
        futex_up(&ffn_futex);
165
        futex_up(&ffn_futex);
316
        if (nodep->dirty)
166
        if (nodep->dirty)
317
            fat_node_sync(nodep);
167
            fat_node_sync(nodep);
318
        idxp_tmp->nodep = NULL;
168
        idxp_tmp->nodep = NULL;
319
        futex_up(&nodep->lock);
169
        futex_up(&nodep->lock);
320
        futex_up(&idxp_tmp->lock);
170
        futex_up(&idxp_tmp->lock);
321
    } else {
171
    } else {
322
skip_cache:
172
skip_cache:
323
        /* Try to allocate a new node structure. */
173
        /* Try to allocate a new node structure. */
324
        futex_up(&ffn_futex);
174
        futex_up(&ffn_futex);
325
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
175
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
326
        if (!nodep)
176
        if (!nodep)
327
            return NULL;
177
            return NULL;
328
    }
178
    }
329
    fat_node_initialize(nodep);
179
    fat_node_initialize(nodep);
330
 
180
 
331
    bps = fat_bps_get(idxp->dev_handle);
181
    bps = fat_bps_get(idxp->dev_handle);
332
    dps = bps / sizeof(fat_dentry_t);
182
    dps = bps / sizeof(fat_dentry_t);
333
 
183
 
334
    /* Read the block that contains the dentry of interest. */
184
    /* Read the block that contains the dentry of interest. */
335
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
185
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
336
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
186
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
337
    assert(b);
187
    assert(b);
338
 
188
 
339
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
189
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
340
    if (d->attr & FAT_ATTR_SUBDIR) {
190
    if (d->attr & FAT_ATTR_SUBDIR) {
341
        /*
191
        /*
342
         * The only directory which does not have this bit set is the
192
         * The only directory which does not have this bit set is the
343
         * root directory itself. The root directory node is handled
193
         * root directory itself. The root directory node is handled
344
         * and initialized elsewhere.
194
         * and initialized elsewhere.
345
         */
195
         */
346
        nodep->type = FAT_DIRECTORY;
196
        nodep->type = FAT_DIRECTORY;
347
        /*
197
        /*
348
         * Unfortunately, the 'size' field of the FAT dentry is not
198
         * Unfortunately, the 'size' field of the FAT dentry is not
349
         * defined for the directory entry type. We must determine the
199
         * defined for the directory entry type. We must determine the
350
         * size of the directory by walking the FAT.
200
         * size of the directory by walking the FAT.
351
         */
201
         */
352
        nodep->size = bps * _fat_blcks_get(idxp->dev_handle,
202
        nodep->size = bps * _fat_blcks_get(idxp->dev_handle,
353
            uint16_t_le2host(d->firstc));
203
            uint16_t_le2host(d->firstc));
354
    } else {
204
    } else {
355
        nodep->type = FAT_FILE;
205
        nodep->type = FAT_FILE;
356
        nodep->size = uint32_t_le2host(d->size);
206
        nodep->size = uint32_t_le2host(d->size);
357
    }
207
    }
358
    nodep->firstc = uint16_t_le2host(d->firstc);
208
    nodep->firstc = uint16_t_le2host(d->firstc);
359
    nodep->lnkcnt = 1;
209
    nodep->lnkcnt = 1;
360
    nodep->refcnt = 1;
210
    nodep->refcnt = 1;
361
 
211
 
362
    block_put(b);
212
    block_put(b);
363
 
213
 
364
    /* Link the idx structure with the node structure. */
214
    /* Link the idx structure with the node structure. */
365
    nodep->idx = idxp;
215
    nodep->idx = idxp;
366
    idxp->nodep = nodep;
216
    idxp->nodep = nodep;
367
 
217
 
368
    return nodep;
218
    return nodep;
369
}
219
}
370
 
220
 
371
/** Instantiate a FAT in-core node. */
221
/** Instantiate a FAT in-core node. */
372
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
222
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
373
{
223
{
374
    void *node;
224
    void *node;
375
    fat_idx_t *idxp;
225
    fat_idx_t *idxp;
376
 
226
 
377
    idxp = fat_idx_get_by_index(dev_handle, index);
227
    idxp = fat_idx_get_by_index(dev_handle, index);
378
    if (!idxp)
228
    if (!idxp)
379
        return NULL;
229
        return NULL;
380
    /* idxp->lock held */
230
    /* idxp->lock held */
381
    node = fat_node_get_core(idxp);
231
    node = fat_node_get_core(idxp);
382
    futex_up(&idxp->lock);
232
    futex_up(&idxp->lock);
383
    return node;
233
    return node;
384
}
234
}
385
 
235
 
386
static void fat_node_put(void *node)
236
static void fat_node_put(void *node)
387
{
237
{
388
    fat_node_t *nodep = (fat_node_t *)node;
238
    fat_node_t *nodep = (fat_node_t *)node;
389
 
239
 
390
    futex_down(&nodep->lock);
240
    futex_down(&nodep->lock);
391
    if (!--nodep->refcnt) {
241
    if (!--nodep->refcnt) {
392
        futex_down(&ffn_futex);
242
        futex_down(&ffn_futex);
393
        list_append(&nodep->ffn_link, &ffn_head);
243
        list_append(&nodep->ffn_link, &ffn_head);
394
        futex_up(&ffn_futex);
244
        futex_up(&ffn_futex);
395
    }
245
    }
396
    futex_up(&nodep->lock);
246
    futex_up(&nodep->lock);
397
}
247
}
398
 
248
 
399
static void *fat_create(int flags)
249
static void *fat_create(int flags)
400
{
250
{
401
    return NULL;    /* not supported at the moment */
251
    return NULL;    /* not supported at the moment */
402
}
252
}
403
 
253
 
404
static int fat_destroy(void *node)
254
static int fat_destroy(void *node)
405
{
255
{
406
    return ENOTSUP; /* not supported at the moment */
256
    return ENOTSUP; /* not supported at the moment */
407
}
257
}
408
 
258
 
409
static bool fat_link(void *prnt, void *chld, const char *name)
259
static bool fat_link(void *prnt, void *chld, const char *name)
410
{
260
{
411
    return false;   /* not supported at the moment */
261
    return false;   /* not supported at the moment */
412
}
262
}
413
 
263
 
414
static int fat_unlink(void *prnt, void *chld)
264
static int fat_unlink(void *prnt, void *chld)
415
{
265
{
416
    return ENOTSUP; /* not supported at the moment */
266
    return ENOTSUP; /* not supported at the moment */
417
}
267
}
418
 
268
 
419
static void *fat_match(void *prnt, const char *component)
269
static void *fat_match(void *prnt, const char *component)
420
{
270
{
421
    fat_node_t *parentp = (fat_node_t *)prnt;
271
    fat_node_t *parentp = (fat_node_t *)prnt;
422
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
272
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
423
    unsigned i, j;
273
    unsigned i, j;
424
    unsigned bps;       /* bytes per sector */
274
    unsigned bps;       /* bytes per sector */
425
    unsigned dps;       /* dentries per sector */
275
    unsigned dps;       /* dentries per sector */
426
    unsigned blocks;
276
    unsigned blocks;
427
    fat_dentry_t *d;
277
    fat_dentry_t *d;
428
    block_t *b;
278
    block_t *b;
429
 
279
 
430
    futex_down(&parentp->idx->lock);
280
    futex_down(&parentp->idx->lock);
431
    bps = fat_bps_get(parentp->idx->dev_handle);
281
    bps = fat_bps_get(parentp->idx->dev_handle);
432
    dps = bps / sizeof(fat_dentry_t);
282
    dps = bps / sizeof(fat_dentry_t);
433
    blocks = parentp->size / bps + (parentp->size % bps != 0);
283
    blocks = parentp->size / bps + (parentp->size % bps != 0);
434
    for (i = 0; i < blocks; i++) {
284
    for (i = 0; i < blocks; i++) {
435
        unsigned dentries;
285
        unsigned dentries;
436
       
286
       
437
        b = fat_block_get(parentp, i);
287
        b = fat_block_get(parentp, i);
438
        dentries = (i == blocks - 1) ?
288
        dentries = (i == blocks - 1) ?
439
            parentp->size % sizeof(fat_dentry_t) :
289
            parentp->size % sizeof(fat_dentry_t) :
440
            dps;
290
            dps;
441
        for (j = 0; j < dentries; j++) {
291
        for (j = 0; j < dentries; j++) {
442
            d = ((fat_dentry_t *)b->data) + j;
292
            d = ((fat_dentry_t *)b->data) + j;
443
            switch (fat_classify_dentry(d)) {
293
            switch (fat_classify_dentry(d)) {
444
            case FAT_DENTRY_SKIP:
294
            case FAT_DENTRY_SKIP:
445
                continue;
295
                continue;
446
            case FAT_DENTRY_LAST:
296
            case FAT_DENTRY_LAST:
447
                block_put(b);
297
                block_put(b);
448
                futex_up(&parentp->idx->lock);
298
                futex_up(&parentp->idx->lock);
449
                return NULL;
299
                return NULL;
450
            default:
300
            default:
451
            case FAT_DENTRY_VALID:
301
            case FAT_DENTRY_VALID:
452
                dentry_name_canonify(d, name);
302
                dentry_name_canonify(d, name);
453
                break;
303
                break;
454
            }
304
            }
455
            if (stricmp(name, component) == 0) {
305
            if (stricmp(name, component) == 0) {
456
                /* hit */
306
                /* hit */
457
                void *node;
307
                void *node;
458
                /*
308
                /*
459
                 * Assume tree hierarchy for locking.  We
309
                 * Assume tree hierarchy for locking.  We
460
                 * already have the parent and now we are going
310
                 * already have the parent and now we are going
461
                 * to lock the child.  Never lock in the oposite
311
                 * to lock the child.  Never lock in the oposite
462
                 * order.
312
                 * order.
463
                 */
313
                 */
464
                fat_idx_t *idx = fat_idx_get_by_pos(
314
                fat_idx_t *idx = fat_idx_get_by_pos(
465
                    parentp->idx->dev_handle, parentp->firstc,
315
                    parentp->idx->dev_handle, parentp->firstc,
466
                    i * dps + j);
316
                    i * dps + j);
467
                futex_up(&parentp->idx->lock);
317
                futex_up(&parentp->idx->lock);
468
                if (!idx) {
318
                if (!idx) {
469
                    /*
319
                    /*
470
                     * Can happen if memory is low or if we
320
                     * Can happen if memory is low or if we
471
                     * run out of 32-bit indices.
321
                     * run out of 32-bit indices.
472
                     */
322
                     */
473
                    block_put(b);
323
                    block_put(b);
474
                    return NULL;
324
                    return NULL;
475
                }
325
                }
476
                node = fat_node_get_core(idx);
326
                node = fat_node_get_core(idx);
477
                futex_up(&idx->lock);
327
                futex_up(&idx->lock);
478
                block_put(b);
328
                block_put(b);
479
                return node;
329
                return node;
480
            }
330
            }
481
        }
331
        }
482
        block_put(b);
332
        block_put(b);
483
    }
333
    }
484
    futex_up(&parentp->idx->lock);
334
    futex_up(&parentp->idx->lock);
485
    return NULL;
335
    return NULL;
486
}
336
}
487
 
337
 
488
static fs_index_t fat_index_get(void *node)
338
static fs_index_t fat_index_get(void *node)
489
{
339
{
490
    fat_node_t *fnodep = (fat_node_t *)node;
340
    fat_node_t *fnodep = (fat_node_t *)node;
491
    if (!fnodep)
341
    if (!fnodep)
492
        return 0;
342
        return 0;
493
    return fnodep->idx->index;
343
    return fnodep->idx->index;
494
}
344
}
495
 
345
 
496
static size_t fat_size_get(void *node)
346
static size_t fat_size_get(void *node)
497
{
347
{
498
    return ((fat_node_t *)node)->size;
348
    return ((fat_node_t *)node)->size;
499
}
349
}
500
 
350
 
501
static unsigned fat_lnkcnt_get(void *node)
351
static unsigned fat_lnkcnt_get(void *node)
502
{
352
{
503
    return ((fat_node_t *)node)->lnkcnt;
353
    return ((fat_node_t *)node)->lnkcnt;
504
}
354
}
505
 
355
 
506
static bool fat_has_children(void *node)
356
static bool fat_has_children(void *node)
507
{
357
{
508
    fat_node_t *nodep = (fat_node_t *)node;
358
    fat_node_t *nodep = (fat_node_t *)node;
509
    unsigned bps;
359
    unsigned bps;
510
    unsigned dps;
360
    unsigned dps;
511
    unsigned blocks;
361
    unsigned blocks;
512
    block_t *b;
362
    block_t *b;
513
    unsigned i, j;
363
    unsigned i, j;
514
 
364
 
515
    if (nodep->type != FAT_DIRECTORY)
365
    if (nodep->type != FAT_DIRECTORY)
516
        return false;
366
        return false;
517
 
367
 
518
    futex_down(&nodep->idx->lock);
368
    futex_down(&nodep->idx->lock);
519
    bps = fat_bps_get(nodep->idx->dev_handle);
369
    bps = fat_bps_get(nodep->idx->dev_handle);
520
    dps = bps / sizeof(fat_dentry_t);
370
    dps = bps / sizeof(fat_dentry_t);
521
 
371
 
522
    blocks = nodep->size / bps + (nodep->size % bps != 0);
372
    blocks = nodep->size / bps + (nodep->size % bps != 0);
523
 
373
 
524
    for (i = 0; i < blocks; i++) {
374
    for (i = 0; i < blocks; i++) {
525
        unsigned dentries;
375
        unsigned dentries;
526
        fat_dentry_t *d;
376
        fat_dentry_t *d;
527
   
377
   
528
        b = fat_block_get(nodep, i);
378
        b = fat_block_get(nodep, i);
529
        dentries = (i == blocks - 1) ?
379
        dentries = (i == blocks - 1) ?
530
            nodep->size % sizeof(fat_dentry_t) :
380
            nodep->size % sizeof(fat_dentry_t) :
531
            dps;
381
            dps;
532
        for (j = 0; j < dentries; j++) {
382
        for (j = 0; j < dentries; j++) {
533
            d = ((fat_dentry_t *)b->data) + j;
383
            d = ((fat_dentry_t *)b->data) + j;
534
            switch (fat_classify_dentry(d)) {
384
            switch (fat_classify_dentry(d)) {
535
            case FAT_DENTRY_SKIP:
385
            case FAT_DENTRY_SKIP:
536
                continue;
386
                continue;
537
            case FAT_DENTRY_LAST:
387
            case FAT_DENTRY_LAST:
538
                block_put(b);
388
                block_put(b);
539
                futex_up(&nodep->idx->lock);
389
                futex_up(&nodep->idx->lock);
540
                return false;
390
                return false;
541
            default:
391
            default:
542
            case FAT_DENTRY_VALID:
392
            case FAT_DENTRY_VALID:
543
                block_put(b);
393
                block_put(b);
544
                futex_up(&nodep->idx->lock);
394
                futex_up(&nodep->idx->lock);
545
                return true;
395
                return true;
546
            }
396
            }
547
            block_put(b);
397
            block_put(b);
548
            futex_up(&nodep->idx->lock);
398
            futex_up(&nodep->idx->lock);
549
            return true;
399
            return true;
550
        }
400
        }
551
        block_put(b);
401
        block_put(b);
552
    }
402
    }
553
 
403
 
554
    futex_up(&nodep->idx->lock);
404
    futex_up(&nodep->idx->lock);
555
    return false;
405
    return false;
556
}
406
}
557
 
407
 
558
static void *fat_root_get(dev_handle_t dev_handle)
408
static void *fat_root_get(dev_handle_t dev_handle)
559
{
409
{
560
    return fat_node_get(dev_handle, 0);
410
    return fat_node_get(dev_handle, 0);
561
}
411
}
562
 
412
 
563
static char fat_plb_get_char(unsigned pos)
413
static char fat_plb_get_char(unsigned pos)
564
{
414
{
565
    return fat_reg.plb_ro[pos % PLB_SIZE];
415
    return fat_reg.plb_ro[pos % PLB_SIZE];
566
}
416
}
567
 
417
 
568
static bool fat_is_directory(void *node)
418
static bool fat_is_directory(void *node)
569
{
419
{
570
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
420
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
571
}
421
}
572
 
422
 
573
static bool fat_is_file(void *node)
423
static bool fat_is_file(void *node)
574
{
424
{
575
    return ((fat_node_t *)node)->type == FAT_FILE;
425
    return ((fat_node_t *)node)->type == FAT_FILE;
576
}
426
}
577
 
427
 
578
/** libfs operations */
428
/** libfs operations */
579
libfs_ops_t fat_libfs_ops = {
429
libfs_ops_t fat_libfs_ops = {
580
    .match = fat_match,
430
    .match = fat_match,
581
    .node_get = fat_node_get,
431
    .node_get = fat_node_get,
582
    .node_put = fat_node_put,
432
    .node_put = fat_node_put,
583
    .create = fat_create,
433
    .create = fat_create,
584
    .destroy = fat_destroy,
434
    .destroy = fat_destroy,
585
    .link = fat_link,
435
    .link = fat_link,
586
    .unlink = fat_unlink,
436
    .unlink = fat_unlink,
587
    .index_get = fat_index_get,
437
    .index_get = fat_index_get,
588
    .size_get = fat_size_get,
438
    .size_get = fat_size_get,
589
    .lnkcnt_get = fat_lnkcnt_get,
439
    .lnkcnt_get = fat_lnkcnt_get,
590
    .has_children = fat_has_children,
440
    .has_children = fat_has_children,
591
    .root_get = fat_root_get,
441
    .root_get = fat_root_get,
592
    .plb_get_char = fat_plb_get_char,
442
    .plb_get_char = fat_plb_get_char,
593
    .is_directory = fat_is_directory,
443
    .is_directory = fat_is_directory,
594
    .is_file = fat_is_file
444
    .is_file = fat_is_file
595
};
445
};
596
 
446
 
597
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
447
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
598
{
448
{
599
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
449
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
600
    block_t *bb;
450
    block_t *bb;
601
    uint16_t bps;
451
    uint16_t bps;
602
    uint16_t rde;
452
    uint16_t rde;
603
    int rc;
453
    int rc;
604
 
454
 
605
    /*
455
    /*
606
     * For now, we don't bother to remember dev_handle, dev_phone or
456
     * For now, we don't bother to remember dev_handle, dev_phone or
607
     * dev_buffer in some data structure. We use global variables because we
457
     * dev_buffer in some data structure. We use global variables because we
608
     * know there will be at most one mount on this file system.
458
     * know there will be at most one mount on this file system.
609
     * Of course, this is a huge TODO item.
459
     * Of course, this is a huge TODO item.
610
     */
460
     */
611
    dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
461
    dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
612
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
462
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
613
   
463
   
614
    if (!dev_buffer) {
464
    if (!dev_buffer) {
615
        ipc_answer_0(rid, ENOMEM);
465
        ipc_answer_0(rid, ENOMEM);
616
        return;
466
        return;
617
    }
467
    }
618
 
468
 
619
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
469
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
620
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
470
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
621
 
471
 
622
    if (dev_phone < 0) {
472
    if (dev_phone < 0) {
623
        munmap(dev_buffer, BS_SIZE);
473
        munmap(dev_buffer, BS_SIZE);
624
        ipc_answer_0(rid, dev_phone);
474
        ipc_answer_0(rid, dev_phone);
625
        return;
475
        return;
626
    }
476
    }
627
 
477
 
628
    rc = ipc_share_out_start(dev_phone, dev_buffer,
478
    rc = ipc_share_out_start(dev_phone, dev_buffer,
629
        AS_AREA_READ | AS_AREA_WRITE);
479
        AS_AREA_READ | AS_AREA_WRITE);
630
    if (rc != EOK) {
480
    if (rc != EOK) {
631
            munmap(dev_buffer, BS_SIZE);
481
            munmap(dev_buffer, BS_SIZE);
632
        ipc_answer_0(rid, rc);
482
        ipc_answer_0(rid, rc);
633
        return;
483
        return;
634
    }
484
    }
635
 
485
 
636
    /* Read the number of root directory entries. */
486
    /* Read the number of root directory entries. */
637
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
487
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
638
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
488
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
639
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
489
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
640
    block_put(bb);
490
    block_put(bb);
641
 
491
 
642
    if (bps != BS_SIZE) {
492
    if (bps != BS_SIZE) {
643
        munmap(dev_buffer, BS_SIZE);
493
        munmap(dev_buffer, BS_SIZE);
644
        ipc_answer_0(rid, ENOTSUP);
494
        ipc_answer_0(rid, ENOTSUP);
645
        return;
495
        return;
646
    }
496
    }
647
 
497
 
648
    rc = fat_idx_init_by_dev_handle(dev_handle);
498
    rc = fat_idx_init_by_dev_handle(dev_handle);
649
    if (rc != EOK) {
499
    if (rc != EOK) {
650
            munmap(dev_buffer, BS_SIZE);
500
            munmap(dev_buffer, BS_SIZE);
651
        ipc_answer_0(rid, rc);
501
        ipc_answer_0(rid, rc);
652
        return;
502
        return;
653
    }
503
    }
654
 
504
 
655
    /* Initialize the root node. */
505
    /* Initialize the root node. */
656
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
506
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
657
    if (!rootp) {
507
    if (!rootp) {
658
            munmap(dev_buffer, BS_SIZE);
508
            munmap(dev_buffer, BS_SIZE);
659
        fat_idx_fini_by_dev_handle(dev_handle);
509
        fat_idx_fini_by_dev_handle(dev_handle);
660
        ipc_answer_0(rid, ENOMEM);
510
        ipc_answer_0(rid, ENOMEM);
661
        return;
511
        return;
662
    }
512
    }
663
    fat_node_initialize(rootp);
513
    fat_node_initialize(rootp);
664
 
514
 
665
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
515
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
666
    if (!ridxp) {
516
    if (!ridxp) {
667
            munmap(dev_buffer, BS_SIZE);
517
            munmap(dev_buffer, BS_SIZE);
668
        free(rootp);
518
        free(rootp);
669
        fat_idx_fini_by_dev_handle(dev_handle);
519
        fat_idx_fini_by_dev_handle(dev_handle);
670
        ipc_answer_0(rid, ENOMEM);
520
        ipc_answer_0(rid, ENOMEM);
671
        return;
521
        return;
672
    }
522
    }
673
    assert(ridxp->index == 0);
523
    assert(ridxp->index == 0);
674
    /* ridxp->lock held */
524
    /* ridxp->lock held */
675
 
525
 
676
    rootp->type = FAT_DIRECTORY;
526
    rootp->type = FAT_DIRECTORY;
677
    rootp->firstc = FAT_CLST_ROOT;
527
    rootp->firstc = FAT_CLST_ROOT;
678
    rootp->refcnt = 1;
528
    rootp->refcnt = 1;
679
    rootp->lnkcnt = 0;  /* FS root is not linked */
529
    rootp->lnkcnt = 0;  /* FS root is not linked */
680
    rootp->size = rde * sizeof(fat_dentry_t);
530
    rootp->size = rde * sizeof(fat_dentry_t);
681
    rootp->idx = ridxp;
531
    rootp->idx = ridxp;
682
    ridxp->nodep = rootp;
532
    ridxp->nodep = rootp;
683
   
533
   
684
    futex_up(&ridxp->lock);
534
    futex_up(&ridxp->lock);
685
 
535
 
686
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
536
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
687
}
537
}
688
 
538
 
689
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
539
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
690
{
540
{
691
    ipc_answer_0(rid, ENOTSUP);
541
    ipc_answer_0(rid, ENOTSUP);
692
}
542
}
693
 
543
 
694
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
544
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
695
{
545
{
696
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
546
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
697
}
547
}
698
 
548
 
699
void fat_read(ipc_callid_t rid, ipc_call_t *request)
549
void fat_read(ipc_callid_t rid, ipc_call_t *request)
700
{
550
{
701
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
551
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
702
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
552
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
703
    off_t pos = (off_t)IPC_GET_ARG3(*request);
553
    off_t pos = (off_t)IPC_GET_ARG3(*request);
704
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
554
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
705
    uint16_t bps = fat_bps_get(dev_handle);
555
    uint16_t bps = fat_bps_get(dev_handle);
706
    size_t bytes;
556
    size_t bytes;
707
    block_t *b;
557
    block_t *b;
708
 
558
 
709
    if (!nodep) {
559
    if (!nodep) {
710
        ipc_answer_0(rid, ENOENT);
560
        ipc_answer_0(rid, ENOENT);
711
        return;
561
        return;
712
    }
562
    }
713
 
563
 
714
    ipc_callid_t callid;
564
    ipc_callid_t callid;
715
    size_t len;
565
    size_t len;
716
    if (!ipc_data_read_receive(&callid, &len)) {
566
    if (!ipc_data_read_receive(&callid, &len)) {
717
        fat_node_put(nodep);
567
        fat_node_put(nodep);
718
        ipc_answer_0(callid, EINVAL);
568
        ipc_answer_0(callid, EINVAL);
719
        ipc_answer_0(rid, EINVAL);
569
        ipc_answer_0(rid, EINVAL);
720
        return;
570
        return;
721
    }
571
    }
722
 
572
 
723
    if (nodep->type == FAT_FILE) {
573
    if (nodep->type == FAT_FILE) {
724
        /*
574
        /*
725
         * Our strategy for regular file reads is to read one block at
575
         * Our strategy for regular file reads is to read one block at
726
         * most and make use of the possibility to return less data than
576
         * most and make use of the possibility to return less data than
727
         * requested. This keeps the code very simple.
577
         * requested. This keeps the code very simple.
728
         */
578
         */
729
        bytes = min(len, bps - pos % bps);
579
        bytes = min(len, bps - pos % bps);
730
        b = fat_block_get(nodep, pos / bps);
580
        b = fat_block_get(nodep, pos / bps);
731
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
581
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
732
            bytes);
582
            bytes);
733
        block_put(b);
583
        block_put(b);
734
    } else {
584
    } else {
735
        unsigned bnum;
585
        unsigned bnum;
736
        off_t spos = pos;
586
        off_t spos = pos;
737
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
587
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
738
        fat_dentry_t *d;
588
        fat_dentry_t *d;
739
 
589
 
740
        assert(nodep->type == FAT_DIRECTORY);
590
        assert(nodep->type == FAT_DIRECTORY);
741
        assert(nodep->size % bps == 0);
591
        assert(nodep->size % bps == 0);
742
        assert(bps % sizeof(fat_dentry_t) == 0);
592
        assert(bps % sizeof(fat_dentry_t) == 0);
743
 
593
 
744
        /*
594
        /*
745
         * Our strategy for readdir() is to use the position pointer as
595
         * Our strategy for readdir() is to use the position pointer as
746
         * an index into the array of all dentries. On entry, it points
596
         * an index into the array of all dentries. On entry, it points
747
         * to the first unread dentry. If we skip any dentries, we bump
597
         * to the first unread dentry. If we skip any dentries, we bump
748
         * the position pointer accordingly.
598
         * the position pointer accordingly.
749
         */
599
         */
750
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
600
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
751
        while (bnum < nodep->size / bps) {
601
        while (bnum < nodep->size / bps) {
752
            off_t o;
602
            off_t o;
753
 
603
 
754
            b = fat_block_get(nodep, bnum);
604
            b = fat_block_get(nodep, bnum);
755
            for (o = pos % (bps / sizeof(fat_dentry_t));
605
            for (o = pos % (bps / sizeof(fat_dentry_t));
756
                o < bps / sizeof(fat_dentry_t);
606
                o < bps / sizeof(fat_dentry_t);
757
                o++, pos++) {
607
                o++, pos++) {
758
                d = ((fat_dentry_t *)b->data) + o;
608
                d = ((fat_dentry_t *)b->data) + o;
759
                switch (fat_classify_dentry(d)) {
609
                switch (fat_classify_dentry(d)) {
760
                case FAT_DENTRY_SKIP:
610
                case FAT_DENTRY_SKIP:
761
                    continue;
611
                    continue;
762
                case FAT_DENTRY_LAST:
612
                case FAT_DENTRY_LAST:
763
                    block_put(b);
613
                    block_put(b);
764
                    goto miss;
614
                    goto miss;
765
                default:
615
                default:
766
                case FAT_DENTRY_VALID:
616
                case FAT_DENTRY_VALID:
767
                    dentry_name_canonify(d, name);
617
                    dentry_name_canonify(d, name);
768
                    block_put(b);
618
                    block_put(b);
769
                    goto hit;
619
                    goto hit;
770
                }
620
                }
771
            }
621
            }
772
            block_put(b);
622
            block_put(b);
773
            bnum++;
623
            bnum++;
774
        }
624
        }
775
miss:
625
miss:
776
        fat_node_put(nodep);
626
        fat_node_put(nodep);
777
        ipc_answer_0(callid, ENOENT);
627
        ipc_answer_0(callid, ENOENT);
778
        ipc_answer_1(rid, ENOENT, 0);
628
        ipc_answer_1(rid, ENOENT, 0);
779
        return;
629
        return;
780
hit:
630
hit:
781
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
631
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
782
        bytes = (pos - spos) + 1;
632
        bytes = (pos - spos) + 1;
783
    }
633
    }
784
 
634
 
785
    fat_node_put(nodep);
635
    fat_node_put(nodep);
786
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
636
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
787
}
637
}
788
 
-
 
789
/** Fill the gap between EOF and a new file position.
-
 
790
 *
-
 
791
 * @param nodep     FAT node with the gap.
-
 
792
 * @param mcl       First cluster in an independent cluster chain that will
-
 
793
 *          be later appended to the end of the node's own cluster
-
 
794
 *          chain. If pos is still in the last allocated cluster,
-
 
795
 *          this argument is ignored.
-
 
796
 * @param pos       Position in the last node block.
-
 
797
 */
-
 
798
static void
-
 
799
fat_fill_gap(fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
-
 
800
{
-
 
801
    uint16_t bps;
-
 
802
    unsigned spc;
-
 
803
    block_t *bb, *b;
-
 
804
    off_t o, boundary;
-
 
805
 
-
 
806
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE);
-
 
807
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
808
    spc = FAT_BS(bb)->spc;
-
 
809
    block_put(bb);
-
 
810
   
-
 
811
    boundary = ROUND_UP(nodep->size, bps * spc);
-
 
812
 
-
 
813
    /* zero out already allocated space */
-
 
814
    for (o = nodep->size - 1; o < pos && o < boundary;
-
 
815
        o = ALIGN_DOWN(o + bps, bps)) {
-
 
816
        b = fat_block_get(nodep, o / bps);
-
 
817
        memset(b->data + o % bps, 0, bps - o % bps);
-
 
818
        b->dirty = true;        /* need to sync node */
-
 
819
        block_put(b);
-
 
820
    }
-
 
821
   
-
 
822
    if (o >= pos)
-
 
823
        return;
-
 
824
   
-
 
825
    /* zero out the initial part of the new cluster chain */
-
 
826
    for (o = boundary; o < pos; o += bps) {
-
 
827
        b = _fat_block_get(nodep->idx->dev_handle, mcl,
-
 
828
            (o - boundary) / bps);
-
 
829
        memset(b->data, 0, min(bps, pos - o));
-
 
830
        b->dirty = true;        /* need to sync node */
-
 
831
        block_put(b);
-
 
832
    }
-
 
833
}
-
 
834
 
-
 
835
static void
-
 
836
fat_mark_cluster(dev_handle_t dev_handle, unsigned fatno, fat_cluster_t clst,
-
 
837
    fat_cluster_t value)
-
 
838
{
-
 
839
    /* TODO */
-
 
840
}
-
 
841
 
-
 
842
static void
-
 
843
fat_alloc_shadow_clusters(dev_handle_t dev_handle, fat_cluster_t *lifo,
-
 
844
    unsigned nclsts)
-
 
845
{
-
 
846
    /* TODO */
-
 
847
}
-
 
848
 
-
 
849
static int
-
 
850
fat_alloc_clusters(dev_handle_t dev_handle, unsigned nclsts, fat_cluster_t *mcl,
-
 
851
    fat_cluster_t *lcl)
-
 
852
{
-
 
853
    uint16_t bps;
-
 
854
    uint16_t rscnt;
-
 
855
    uint16_t sf;
-
 
856
    block_t *bb, *blk;
-
 
857
    fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
-
 
858
    unsigned found = 0; /* top of the free cluster number stack */
-
 
859
    unsigned b, c, cl;
-
 
860
 
-
 
861
    lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
-
 
862
    if (lifo)
-
 
863
        return ENOMEM;
-
 
864
   
-
 
865
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
866
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
867
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
-
 
868
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
-
 
869
    block_put(bb);
-
 
870
   
-
 
871
    /*
-
 
872
     * Search FAT1 for unused clusters.
-
 
873
     */
-
 
874
    for (b = 0, cl = 0; b < sf; blk++) {
-
 
875
        blk = block_get(dev_handle, rscnt + b, bps);
-
 
876
        for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
-
 
877
            fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
-
 
878
            if (*clst == FAT_CLST_RES0) {
-
 
879
                /*
-
 
880
                 * The cluster is free. Put it into our stack
-
 
881
                 * of found clusters and mark it as non-free.
-
 
882
                 */
-
 
883
                lifo[found] = cl;
-
 
884
                if (found == 0)
-
 
885
                    *clst = FAT_CLST_LAST1;
-
 
886
                else
-
 
887
                    *clst = lifo[found - 1];
-
 
888
                blk->dirty = true;  /* need to sync block */
-
 
889
                if (++found == nclsts) {
-
 
890
                    /* we are almost done */
-
 
891
                    block_put(blk);
-
 
892
                    /* update the shadow copies of FAT */
-
 
893
                    fat_alloc_shadow_clusters(dev_handle,
-
 
894
                        lifo, nclsts);
-
 
895
                    *mcl = lifo[found - 1];
-
 
896
                    *lcl = lifo[0];
-
 
897
                    free(lifo);
-
 
898
                    return EOK;
-
 
899
                }
-
 
900
            }
-
 
901
        }
-
 
902
        block_put(blk);
-
 
903
    }
-
 
904
 
-
 
905
    /*
-
 
906
     * We could not find enough clusters. Now we need to free the clusters
-
 
907
     * we have allocated so far.
-
 
908
     */
-
 
909
    while (found--)
-
 
910
        fat_mark_cluster(dev_handle, FAT1, lifo[found], FAT_CLST_RES0);
-
 
911
   
-
 
912
    free(lifo);
-
 
913
    return ENOSPC;
-
 
914
}
-
 
915
 
-
 
916
static void
-
 
917
fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl)
-
 
918
{
-
 
919
}
-
 
920
 
638
 
921
void fat_write(ipc_callid_t rid, ipc_call_t *request)
639
void fat_write(ipc_callid_t rid, ipc_call_t *request)
922
{
640
{
923
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
641
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
924
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
642
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
925
    off_t pos = (off_t)IPC_GET_ARG3(*request);
643
    off_t pos = (off_t)IPC_GET_ARG3(*request);
926
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
644
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
927
    size_t bytes;
645
    size_t bytes;
928
    block_t *b, *bb;
646
    block_t *b, *bb;
929
    uint16_t bps;
647
    uint16_t bps;
930
    unsigned spc;
648
    unsigned spc;
931
    off_t boundary;
649
    off_t boundary;
932
   
650
   
933
    if (!nodep) {
651
    if (!nodep) {
934
        ipc_answer_0(rid, ENOENT);
652
        ipc_answer_0(rid, ENOENT);
935
        return;
653
        return;
936
    }
654
    }
937
   
655
   
938
    /* XXX remove me when you are ready */
656
    /* XXX remove me when you are ready */
939
    {
657
    {
940
        ipc_answer_0(rid, ENOTSUP);
658
        ipc_answer_0(rid, ENOTSUP);
941
        fat_node_put(nodep);
659
        fat_node_put(nodep);
942
        return;
660
        return;
943
    }
661
    }
944
 
662
 
945
    ipc_callid_t callid;
663
    ipc_callid_t callid;
946
    size_t len;
664
    size_t len;
947
    if (!ipc_data_write_receive(&callid, &len)) {
665
    if (!ipc_data_write_receive(&callid, &len)) {
948
        fat_node_put(nodep);
666
        fat_node_put(nodep);
949
        ipc_answer_0(callid, EINVAL);
667
        ipc_answer_0(callid, EINVAL);
950
        ipc_answer_0(rid, EINVAL);
668
        ipc_answer_0(rid, EINVAL);
951
        return;
669
        return;
952
    }
670
    }
953
 
671
 
954
    /*
672
    /*
955
     * In all scenarios, we will attempt to write out only one block worth
673
     * In all scenarios, we will attempt to write out only one block worth
956
     * of data at maximum. There might be some more efficient approaches,
674
     * of data at maximum. There might be some more efficient approaches,
957
     * but this one greatly simplifies fat_write(). Note that we can afford
675
     * but this one greatly simplifies fat_write(). Note that we can afford
958
     * to do this because the client must be ready to handle the return
676
     * to do this because the client must be ready to handle the return
959
     * value signalizing a smaller number of bytes written.
677
     * value signalizing a smaller number of bytes written.
960
     */
678
     */
961
    bytes = min(len, bps - pos % bps);
679
    bytes = min(len, bps - pos % bps);
962
 
680
 
963
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
681
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
964
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
682
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
965
    spc = FAT_BS(bb)->spc;
683
    spc = FAT_BS(bb)->spc;
966
    block_put(bb);
684
    block_put(bb);
967
   
685
   
968
    boundary = ROUND_UP(nodep->size, bps * spc);
686
    boundary = ROUND_UP(nodep->size, bps * spc);
969
    if (pos < boundary) {
687
    if (pos < boundary) {
970
        /*
688
        /*
971
         * This is the easier case - we are either overwriting already
689
         * This is the easier case - we are either overwriting already
972
         * existing contents or writing behind the EOF, but still within
690
         * existing contents or writing behind the EOF, but still within
973
         * the limits of the last cluster. The node size may grow to the
691
         * the limits of the last cluster. The node size may grow to the
974
         * next block size boundary.
692
         * next block size boundary.
975
         */
693
         */
976
        fat_fill_gap(nodep, FAT_CLST_RES0, pos);
694
        fat_fill_gap(nodep, FAT_CLST_RES0, pos);
977
        b = fat_block_get(nodep, pos / bps);
695
        b = fat_block_get(nodep, pos / bps);
978
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
696
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
979
            bytes);
697
            bytes);
980
        b->dirty = true;        /* need to sync block */
698
        b->dirty = true;        /* need to sync block */
981
        block_put(b);
699
        block_put(b);
982
        if (pos + bytes > nodep->size) {
700
        if (pos + bytes > nodep->size) {
983
            nodep->size = pos + bytes;
701
            nodep->size = pos + bytes;
984
            nodep->dirty = true;    /* need to sync node */
702
            nodep->dirty = true;    /* need to sync node */
985
        }
703
        }
986
        fat_node_put(nodep);
704
        fat_node_put(nodep);
987
        ipc_answer_1(rid, EOK, bytes); 
705
        ipc_answer_1(rid, EOK, bytes); 
988
        return;
706
        return;
989
    } else {
707
    } else {
990
        /*
708
        /*
991
         * This is the more difficult case. We must allocate new
709
         * This is the more difficult case. We must allocate new
992
         * clusters for the node and zero them out.
710
         * clusters for the node and zero them out.
993
         */
711
         */
994
        int status;
712
        int status;
995
        unsigned nclsts;
713
        unsigned nclsts;
996
        fat_cluster_t mcl, lcl;
714
        fat_cluster_t mcl, lcl;
997
   
715
   
998
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
716
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
999
            bps * spc;
717
            bps * spc;
1000
        /* create an independent chain of nclsts clusters in all FATs */
718
        /* create an independent chain of nclsts clusters in all FATs */
1001
        status = fat_alloc_clusters(dev_handle, nclsts, &mcl, &lcl);
719
        status = fat_alloc_clusters(dev_handle, nclsts, &mcl, &lcl);
1002
        if (status != EOK) {
720
        if (status != EOK) {
1003
            /* could not allocate a chain of nclsts clusters */
721
            /* could not allocate a chain of nclsts clusters */
1004
            fat_node_put(nodep);
722
            fat_node_put(nodep);
1005
            ipc_answer_0(callid, status);
723
            ipc_answer_0(callid, status);
1006
            ipc_answer_0(rid, status);
724
            ipc_answer_0(rid, status);
1007
            return;
725
            return;
1008
        }
726
        }
1009
        /* zero fill any gaps */
727
        /* zero fill any gaps */
1010
        fat_fill_gap(nodep, mcl, pos);
728
        fat_fill_gap(nodep, mcl, pos);
1011
        b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc);
729
        b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc);
1012
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
730
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
1013
            bytes);
731
            bytes);
1014
        b->dirty = true;        /* need to sync block */
732
        b->dirty = true;        /* need to sync block */
1015
        block_put(b);
733
        block_put(b);
1016
        /*
734
        /*
1017
         * Append the cluster chain starting in mcl to the end of the
735
         * Append the cluster chain starting in mcl to the end of the
1018
         * node's cluster chain.
736
         * node's cluster chain.
1019
         */
737
         */
1020
        fat_append_clusters(nodep, mcl);
738
        fat_append_clusters(nodep, mcl);
1021
        nodep->size = pos + bytes;
739
        nodep->size = pos + bytes;
1022
        nodep->dirty = true;        /* need to sync node */
740
        nodep->dirty = true;        /* need to sync node */
1023
        fat_node_put(nodep);
741
        fat_node_put(nodep);
1024
        ipc_answer_1(rid, EOK, bytes);
742
        ipc_answer_1(rid, EOK, bytes);
1025
        return;
743
        return;
1026
    }
744
    }
1027
}
745
}
1028
 
746
 
1029
/**
747
/**
1030
 * @}
748
 * @}
1031
 */
749
 */
1032
 
750