Subversion Repositories HelenOS

Rev

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

Rev 3153 Rev 3403
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 "../../vfs/vfs.h"
39
#include "../../vfs/vfs.h"
40
#include <libfs.h>
40
#include <libfs.h>
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
-
 
42
#include <ipc/services.h>
-
 
43
#include <ipc/devmap.h>
42
#include <async.h>
44
#include <async.h>
43
#include <errno.h>
45
#include <errno.h>
44
#include <string.h>
46
#include <string.h>
45
#include <byteorder.h>
47
#include <byteorder.h>
46
#include <libadt/hash_table.h>
48
#include <libadt/hash_table.h>
47
#include <libadt/list.h>
49
#include <libadt/list.h>
48
#include <assert.h>
50
#include <assert.h>
49
#include <futex.h>
51
#include <futex.h>
-
 
52
#include <sys/mman.h>
50
 
53
 
51
#define BS_BLOCK        0
54
#define BS_BLOCK        0
-
 
55
#define BS_SIZE         512
52
 
56
 
53
/** Futex protecting the list of cached free FAT nodes. */
57
/** Futex protecting the list of cached free FAT nodes. */
54
static futex_t ffn_futex = FUTEX_INITIALIZER;
58
static futex_t ffn_futex = FUTEX_INITIALIZER;
55
 
59
 
56
/** List of cached free FAT nodes. */
60
/** List of cached free FAT nodes. */
57
static LIST_INITIALIZE(ffn_head);
61
static LIST_INITIALIZE(ffn_head);
58
 
62
 
59
#define FAT_NAME_LEN        8
63
#define FAT_NAME_LEN        8
60
#define FAT_EXT_LEN     3
64
#define FAT_EXT_LEN     3
61
 
65
 
62
#define FAT_PAD         ' ' 
66
#define FAT_PAD         ' ' 
63
 
67
 
64
#define FAT_DENTRY_UNUSED   0x00
68
#define FAT_DENTRY_UNUSED   0x00
65
#define FAT_DENTRY_E5_ESC   0x05
69
#define FAT_DENTRY_E5_ESC   0x05
66
#define FAT_DENTRY_DOT      0x2e
70
#define FAT_DENTRY_DOT      0x2e
67
#define FAT_DENTRY_ERASED   0xe5
71
#define FAT_DENTRY_ERASED   0xe5
68
 
72
 
-
 
73
#define min(a, b)       ((a) < (b) ? (a) : (b))
-
 
74
 
69
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
75
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
70
{
76
{
71
    int i;
77
    int i;
72
 
78
 
73
    for (i = 0; i < FAT_NAME_LEN; i++) {
79
    for (i = 0; i < FAT_NAME_LEN; i++) {
74
        if (d->name[i] == FAT_PAD) {
80
        if (d->name[i] == FAT_PAD)
75
            buf++;
-
 
76
            break;
81
            break;
77
        }
-
 
78
        if (d->name[i] == FAT_DENTRY_E5_ESC)
82
        if (d->name[i] == FAT_DENTRY_E5_ESC)
79
            *buf++ = 0xe5;
83
            *buf++ = 0xe5;
80
        else
84
        else
81
            *buf++ = d->name[i];
85
            *buf++ = d->name[i];
82
    }
86
    }
83
    if (d->ext[0] != FAT_PAD)
87
    if (d->ext[0] != FAT_PAD)
84
        *buf++ = '.';
88
        *buf++ = '.';
85
    for (i = 0; i < FAT_EXT_LEN; i++) {
89
    for (i = 0; i < FAT_EXT_LEN; i++) {
86
        if (d->ext[i] == FAT_PAD) {
90
        if (d->ext[i] == FAT_PAD) {
87
            *buf = '\0';
91
            *buf = '\0';
88
            return;
92
            return;
89
        }
93
        }
90
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
94
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
91
            *buf++ = 0xe5;
95
            *buf++ = 0xe5;
92
        else
96
        else
93
            *buf++ = d->ext[i];
97
            *buf++ = d->ext[i];
94
    }
98
    }
-
 
99
    *buf = '\0';
95
}
100
}
96
 
101
 
-
 
102
static int dev_phone = -1;      /* FIXME */
-
 
103
static void *dev_buffer = NULL;     /* FIXME */
-
 
104
 
97
/* TODO move somewhere else */
105
/* TODO move somewhere else */
98
typedef struct {
106
typedef struct {
99
    void *data;
107
    void *data;
-
 
108
    size_t size;
100
} block_t;
109
} block_t;
101
 
110
 
102
static block_t *block_get(dev_handle_t dev_handle, off_t offset)
111
static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
103
{
112
{
-
 
113
    /* FIXME */
-
 
114
    block_t *b;
-
 
115
    off_t bufpos = 0;
-
 
116
    size_t buflen = 0;
-
 
117
    off_t pos = offset * bs;
-
 
118
 
-
 
119
    assert(dev_phone != -1);
-
 
120
    assert(dev_buffer);
-
 
121
 
-
 
122
    b = malloc(sizeof(block_t));
-
 
123
    if (!b)
104
    return NULL;    /* TODO */
124
        return NULL;
-
 
125
   
-
 
126
    b->data = malloc(bs);
-
 
127
    if (!b->data) {
-
 
128
        free(b);
-
 
129
        return NULL;
-
 
130
    }
-
 
131
    b->size = bs;
-
 
132
 
-
 
133
    if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos,
-
 
134
        b->data, bs, bs)) {
-
 
135
        free(b->data);
-
 
136
        free(b);
-
 
137
        return NULL;
-
 
138
    }
-
 
139
 
-
 
140
    return b;
105
}
141
}
106
 
142
 
107
static void block_put(block_t *block)
143
static void block_put(block_t *block)
108
{
144
{
109
    /* TODO */
145
    /* FIXME */
-
 
146
    free(block->data);
-
 
147
    free(block);
110
}
148
}
111
 
149
 
112
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
150
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
113
 
151
 
114
#define FAT_CLST_RES0   0x0000
152
#define FAT_CLST_RES0   0x0000
115
#define FAT_CLST_RES1   0x0001
153
#define FAT_CLST_RES1   0x0001
116
#define FAT_CLST_FIRST  0x0002
154
#define FAT_CLST_FIRST  0x0002
117
#define FAT_CLST_BAD    0xfff7
155
#define FAT_CLST_BAD    0xfff7
118
#define FAT_CLST_LAST1  0xfff8
156
#define FAT_CLST_LAST1  0xfff8
119
#define FAT_CLST_LAST8  0xffff
157
#define FAT_CLST_LAST8  0xffff
120
 
158
 
121
/* internally used to mark root directory's parent */
159
/* internally used to mark root directory's parent */
122
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
160
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
123
/* internally used to mark root directory */
161
/* internally used to mark root directory */
124
#define FAT_CLST_ROOT       FAT_CLST_RES1
162
#define FAT_CLST_ROOT       FAT_CLST_RES1
125
 
163
 
126
#define fat_block_get(np, off) \
164
#define fat_block_get(np, off) \
127
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
165
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
128
 
166
 
129
static block_t *
167
static block_t *
130
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
168
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
131
{
169
{
132
    block_t *bb;
170
    block_t *bb;
133
    block_t *b;
171
    block_t *b;
134
    unsigned bps;
172
    unsigned bps;
135
    unsigned spc;
173
    unsigned spc;
136
    unsigned rscnt;     /* block address of the first FAT */
174
    unsigned rscnt;     /* block address of the first FAT */
137
    unsigned fatcnt;
175
    unsigned fatcnt;
138
    unsigned rde;
176
    unsigned rde;
139
    unsigned rds;       /* root directory size */
177
    unsigned rds;       /* root directory size */
140
    unsigned sf;
178
    unsigned sf;
141
    unsigned ssa;       /* size of the system area */
179
    unsigned ssa;       /* size of the system area */
142
    unsigned clusters;
180
    unsigned clusters;
143
    fat_cluster_t clst = firstc;
181
    fat_cluster_t clst = firstc;
144
    unsigned i;
182
    unsigned i;
145
 
183
 
146
    bb = block_get(dev_handle, BS_BLOCK);
184
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
147
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
185
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
148
    spc = FAT_BS(bb)->spc;
186
    spc = FAT_BS(bb)->spc;
149
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
187
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
150
    fatcnt = FAT_BS(bb)->fatcnt;
188
    fatcnt = FAT_BS(bb)->fatcnt;
151
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
189
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
152
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
190
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
153
    block_put(bb);
191
    block_put(bb);
154
 
192
 
155
    rds = (sizeof(fat_dentry_t) * rde) / bps;
193
    rds = (sizeof(fat_dentry_t) * rde) / bps;
156
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
194
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
157
    ssa = rscnt + fatcnt * sf + rds;
195
    ssa = rscnt + fatcnt * sf + rds;
158
 
196
 
159
    if (firstc == FAT_CLST_ROOT) {
197
    if (firstc == FAT_CLST_ROOT) {
160
        /* root directory special case */
198
        /* root directory special case */
161
        assert(offset < rds);
199
        assert(offset < rds);
162
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset);
200
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
163
        return b;
201
        return b;
164
    }
202
    }
165
 
203
 
166
    clusters = offset / spc;
204
    clusters = offset / spc;
167
    for (i = 0; i < clusters; i++) {
205
    for (i = 0; i < clusters; i++) {
168
        unsigned fsec;  /* sector offset relative to FAT1 */
206
        unsigned fsec;  /* sector offset relative to FAT1 */
169
        unsigned fidx;  /* FAT1 entry index */
207
        unsigned fidx;  /* FAT1 entry index */
170
 
208
 
171
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
209
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
172
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
210
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
173
        fidx = clst % (bps / sizeof(fat_cluster_t));
211
        fidx = clst % (bps / sizeof(fat_cluster_t));
174
        /* read FAT1 */
212
        /* read FAT1 */
175
        b = block_get(dev_handle, rscnt + fsec);
213
        b = block_get(dev_handle, rscnt + fsec, bps);
176
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
214
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
177
        assert(clst != FAT_CLST_BAD);
215
        assert(clst != FAT_CLST_BAD);
178
        assert(clst < FAT_CLST_LAST1);
216
        assert(clst < FAT_CLST_LAST1);
179
        block_put(b);
217
        block_put(b);
180
    }
218
    }
181
 
219
 
182
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
220
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
183
        offset % spc);
221
        offset % spc, bps);
184
 
222
 
185
    return b;
223
    return b;
186
}
224
}
187
 
225
 
-
 
226
/** Return number of blocks allocated to a file.
-
 
227
 *
-
 
228
 * @param dev_handle    Device handle of the device with the file.
-
 
229
 * @param firstc    First cluster of the file.
-
 
230
 *
-
 
231
 * @return      Number of blocks allocated to the file.
-
 
232
 */
-
 
233
static uint16_t
-
 
234
_fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc)
-
 
235
{
-
 
236
    block_t *bb;
-
 
237
    block_t *b;
-
 
238
    unsigned bps;
-
 
239
    unsigned spc;
-
 
240
    unsigned rscnt;     /* block address of the first FAT */
-
 
241
    unsigned clusters = 0;
-
 
242
    fat_cluster_t clst = firstc;
-
 
243
 
-
 
244
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
245
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
246
    spc = FAT_BS(bb)->spc;
-
 
247
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
-
 
248
    block_put(bb);
-
 
249
 
-
 
250
    if (firstc == FAT_CLST_RES0) {
-
 
251
        /* No space allocated to the file. */
-
 
252
        return 0;
-
 
253
    }
-
 
254
 
-
 
255
    while (clst < FAT_CLST_LAST1) {
-
 
256
        unsigned fsec;  /* sector offset relative to FAT1 */
-
 
257
        unsigned fidx;  /* FAT1 entry index */
-
 
258
 
-
 
259
        assert(clst >= FAT_CLST_FIRST);
-
 
260
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
-
 
261
        fidx = clst % (bps / sizeof(fat_cluster_t));
-
 
262
        /* read FAT1 */
-
 
263
        b = block_get(dev_handle, rscnt + fsec, bps);
-
 
264
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
-
 
265
        assert(clst != FAT_CLST_BAD);
-
 
266
        block_put(b);
-
 
267
        clusters++;
-
 
268
    }
-
 
269
 
-
 
270
    return clusters * spc;
-
 
271
}
-
 
272
 
188
static void fat_node_initialize(fat_node_t *node)
273
static void fat_node_initialize(fat_node_t *node)
189
{
274
{
190
    futex_initialize(&node->lock, 1);
275
    futex_initialize(&node->lock, 1);
191
    node->idx = NULL;
276
    node->idx = NULL;
192
    node->type = 0;
277
    node->type = 0;
193
    link_initialize(&node->ffn_link);
278
    link_initialize(&node->ffn_link);
194
    node->size = 0;
279
    node->size = 0;
195
    node->lnkcnt = 0;
280
    node->lnkcnt = 0;
196
    node->refcnt = 0;
281
    node->refcnt = 0;
197
    node->dirty = false;
282
    node->dirty = false;
198
}
283
}
199
 
284
 
200
static uint16_t fat_bps_get(dev_handle_t dev_handle)
285
static uint16_t fat_bps_get(dev_handle_t dev_handle)
201
{
286
{
202
    block_t *bb;
287
    block_t *bb;
203
    uint16_t bps;
288
    uint16_t bps;
204
   
289
   
205
    bb = block_get(dev_handle, BS_BLOCK);
290
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
206
    assert(bb != NULL);
291
    assert(bb != NULL);
207
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
292
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
208
    block_put(bb);
293
    block_put(bb);
209
 
294
 
210
    return bps;
295
    return bps;
211
}
296
}
212
 
297
 
213
typedef enum {
298
typedef enum {
214
    FAT_DENTRY_SKIP,
299
    FAT_DENTRY_SKIP,
215
    FAT_DENTRY_LAST,
300
    FAT_DENTRY_LAST,
216
    FAT_DENTRY_VALID
301
    FAT_DENTRY_VALID
217
} fat_dentry_clsf_t;
302
} fat_dentry_clsf_t;
218
 
303
 
219
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
304
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
220
{
305
{
221
    if (d->attr & FAT_ATTR_VOLLABEL) {
306
    if (d->attr & FAT_ATTR_VOLLABEL) {
222
        /* volume label entry */
307
        /* volume label entry */
223
        return FAT_DENTRY_SKIP;
308
        return FAT_DENTRY_SKIP;
224
    }
309
    }
225
    if (d->name[0] == FAT_DENTRY_ERASED) {
310
    if (d->name[0] == FAT_DENTRY_ERASED) {
226
        /* not-currently-used entry */
311
        /* not-currently-used entry */
227
        return FAT_DENTRY_SKIP;
312
        return FAT_DENTRY_SKIP;
228
    }
313
    }
229
    if (d->name[0] == FAT_DENTRY_UNUSED) {
314
    if (d->name[0] == FAT_DENTRY_UNUSED) {
230
        /* never used entry */
315
        /* never used entry */
231
        return FAT_DENTRY_LAST;
316
        return FAT_DENTRY_LAST;
232
    }
317
    }
233
    if (d->name[0] == FAT_DENTRY_DOT) {
318
    if (d->name[0] == FAT_DENTRY_DOT) {
234
        /*
319
        /*
235
         * Most likely '.' or '..'.
320
         * Most likely '.' or '..'.
236
         * It cannot occur in a regular file name.
321
         * It cannot occur in a regular file name.
237
         */
322
         */
238
        return FAT_DENTRY_SKIP;
323
        return FAT_DENTRY_SKIP;
239
    }
324
    }
240
    return FAT_DENTRY_VALID;
325
    return FAT_DENTRY_VALID;
241
}
326
}
242
 
327
 
243
static void fat_node_sync(fat_node_t *node)
328
static void fat_node_sync(fat_node_t *node)
244
{
329
{
245
    /* TODO */
330
    /* TODO */
246
}
331
}
247
 
332
 
248
/** Internal version of fat_node_get().
333
/** Internal version of fat_node_get().
249
 *
334
 *
250
 * @param idxp      Locked index structure.
335
 * @param idxp      Locked index structure.
251
 */
336
 */
252
static void *fat_node_get_core(fat_idx_t *idxp)
337
static void *fat_node_get_core(fat_idx_t *idxp)
253
{
338
{
254
    block_t *b;
339
    block_t *b;
255
    fat_dentry_t *d;
340
    fat_dentry_t *d;
256
    fat_node_t *nodep;
341
    fat_node_t *nodep = NULL;
257
    unsigned bps;
342
    unsigned bps;
258
    unsigned dps;
343
    unsigned dps;
259
 
344
 
260
    if (idxp->nodep) {
345
    if (idxp->nodep) {
261
        /*
346
        /*
262
         * We are lucky.
347
         * We are lucky.
263
         * The node is already instantiated in memory.
348
         * The node is already instantiated in memory.
264
         */
349
         */
265
        futex_down(&idxp->nodep->lock);
350
        futex_down(&idxp->nodep->lock);
266
        if (!idxp->nodep->refcnt++)
351
        if (!idxp->nodep->refcnt++)
267
            list_remove(&nodep->ffn_link);
352
            list_remove(&idxp->nodep->ffn_link);
268
        futex_up(&idxp->nodep->lock);
353
        futex_up(&idxp->nodep->lock);
269
        return idxp->nodep;
354
        return idxp->nodep;
270
    }
355
    }
271
 
356
 
272
    /*
357
    /*
273
     * We must instantiate the node from the file system.
358
     * We must instantiate the node from the file system.
274
     */
359
     */
275
   
360
   
276
    assert(idxp->pfc);
361
    assert(idxp->pfc);
277
 
362
 
278
    futex_down(&ffn_futex);
363
    futex_down(&ffn_futex);
279
    if (!list_empty(&ffn_head)) {
364
    if (!list_empty(&ffn_head)) {
280
        /* Try to use a cached free node structure. */
365
        /* Try to use a cached free node structure. */
281
        fat_idx_t *idxp_tmp;
366
        fat_idx_t *idxp_tmp;
282
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
367
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
283
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
368
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
284
            goto skip_cache;
369
            goto skip_cache;
285
        idxp_tmp = nodep->idx;
370
        idxp_tmp = nodep->idx;
286
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
371
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
287
            futex_up(&nodep->lock);
372
            futex_up(&nodep->lock);
288
            goto skip_cache;
373
            goto skip_cache;
289
        }
374
        }
290
        list_remove(&nodep->ffn_link);
375
        list_remove(&nodep->ffn_link);
291
        futex_up(&ffn_futex);
376
        futex_up(&ffn_futex);
292
        if (nodep->dirty)
377
        if (nodep->dirty)
293
            fat_node_sync(nodep);
378
            fat_node_sync(nodep);
294
        idxp_tmp->nodep = NULL;
379
        idxp_tmp->nodep = NULL;
295
        futex_up(&nodep->lock);
380
        futex_up(&nodep->lock);
296
        futex_up(&idxp_tmp->lock);
381
        futex_up(&idxp_tmp->lock);
297
    } else {
382
    } else {
298
skip_cache:
383
skip_cache:
299
        /* Try to allocate a new node structure. */
384
        /* Try to allocate a new node structure. */
300
        futex_up(&ffn_futex);
385
        futex_up(&ffn_futex);
301
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
386
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
302
        if (!nodep)
387
        if (!nodep)
303
            return NULL;
388
            return NULL;
304
    }
389
    }
305
    fat_node_initialize(nodep);
390
    fat_node_initialize(nodep);
306
 
391
 
307
    bps = fat_bps_get(idxp->dev_handle);
392
    bps = fat_bps_get(idxp->dev_handle);
308
    dps = bps / sizeof(fat_dentry_t);
393
    dps = bps / sizeof(fat_dentry_t);
309
 
394
 
310
    /* Read the block that contains the dentry of interest. */
395
    /* Read the block that contains the dentry of interest. */
311
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
396
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
312
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
397
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
313
    assert(b);
398
    assert(b);
314
 
399
 
315
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
400
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
316
    if (d->attr & FAT_ATTR_SUBDIR) {
401
    if (d->attr & FAT_ATTR_SUBDIR) {
317
        /*
402
        /*
318
         * The only directory which does not have this bit set is the
403
         * The only directory which does not have this bit set is the
319
         * root directory itself. The root directory node is handled
404
         * root directory itself. The root directory node is handled
320
         * and initialized elsewhere.
405
         * and initialized elsewhere.
321
         */
406
         */
322
        nodep->type = FAT_DIRECTORY;
407
        nodep->type = FAT_DIRECTORY;
-
 
408
        /*
-
 
409
         * Unfortunately, the 'size' field of the FAT dentry is not
-
 
410
         * defined for the directory entry type. We must determine the
-
 
411
         * size of the directory by walking the FAT.
-
 
412
         */
-
 
413
        nodep->size = bps * _fat_blcks_get(idxp->dev_handle,
-
 
414
            uint16_t_le2host(d->firstc));
323
    } else {
415
    } else {
324
        nodep->type = FAT_FILE;
416
        nodep->type = FAT_FILE;
-
 
417
        nodep->size = uint32_t_le2host(d->size);
325
    }
418
    }
326
    nodep->firstc = uint16_t_le2host(d->firstc);
419
    nodep->firstc = uint16_t_le2host(d->firstc);
327
    nodep->size = uint32_t_le2host(d->size);
-
 
328
    nodep->lnkcnt = 1;
420
    nodep->lnkcnt = 1;
329
    nodep->refcnt = 1;
421
    nodep->refcnt = 1;
330
 
422
 
331
    block_put(b);
423
    block_put(b);
332
 
424
 
333
    /* Link the idx structure with the node structure. */
425
    /* Link the idx structure with the node structure. */
334
    nodep->idx = idxp;
426
    nodep->idx = idxp;
335
    idxp->nodep = nodep;
427
    idxp->nodep = nodep;
336
 
428
 
337
    return nodep;
429
    return nodep;
338
}
430
}
339
 
431
 
340
/** Instantiate a FAT in-core node. */
432
/** Instantiate a FAT in-core node. */
341
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
433
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
342
{
434
{
343
    void *node;
435
    void *node;
344
    fat_idx_t *idxp;
436
    fat_idx_t *idxp;
345
 
437
 
346
    idxp = fat_idx_get_by_index(dev_handle, index);
438
    idxp = fat_idx_get_by_index(dev_handle, index);
347
    if (!idxp)
439
    if (!idxp)
348
        return NULL;
440
        return NULL;
349
    /* idxp->lock held */
441
    /* idxp->lock held */
350
    node = fat_node_get_core(idxp);
442
    node = fat_node_get_core(idxp);
351
    futex_up(&idxp->lock);
443
    futex_up(&idxp->lock);
352
    return node;
444
    return node;
353
}
445
}
354
 
446
 
355
static void fat_node_put(void *node)
447
static void fat_node_put(void *node)
356
{
448
{
357
    fat_node_t *nodep = (fat_node_t *)node;
449
    fat_node_t *nodep = (fat_node_t *)node;
358
 
450
 
359
    futex_down(&nodep->lock);
451
    futex_down(&nodep->lock);
360
    if (!--nodep->refcnt) {
452
    if (!--nodep->refcnt) {
361
        futex_down(&ffn_futex);
453
        futex_down(&ffn_futex);
362
        list_append(&nodep->ffn_link, &ffn_head);
454
        list_append(&nodep->ffn_link, &ffn_head);
363
        futex_up(&ffn_futex);
455
        futex_up(&ffn_futex);
364
    }
456
    }
365
    futex_up(&nodep->lock);
457
    futex_up(&nodep->lock);
366
}
458
}
367
 
459
 
368
static void *fat_create(int flags)
460
static void *fat_create(int flags)
369
{
461
{
370
    return NULL;    /* not supported at the moment */
462
    return NULL;    /* not supported at the moment */
371
}
463
}
372
 
464
 
373
static int fat_destroy(void *node)
465
static int fat_destroy(void *node)
374
{
466
{
375
    return ENOTSUP; /* not supported at the moment */
467
    return ENOTSUP; /* not supported at the moment */
376
}
468
}
377
 
469
 
378
static bool fat_link(void *prnt, void *chld, const char *name)
470
static bool fat_link(void *prnt, void *chld, const char *name)
379
{
471
{
380
    return false;   /* not supported at the moment */
472
    return false;   /* not supported at the moment */
381
}
473
}
382
 
474
 
383
static int fat_unlink(void *prnt, void *chld)
475
static int fat_unlink(void *prnt, void *chld)
384
{
476
{
385
    return ENOTSUP; /* not supported at the moment */
477
    return ENOTSUP; /* not supported at the moment */
386
}
478
}
387
 
479
 
388
static void *fat_match(void *prnt, const char *component)
480
static void *fat_match(void *prnt, const char *component)
389
{
481
{
390
    fat_node_t *parentp = (fat_node_t *)prnt;
482
    fat_node_t *parentp = (fat_node_t *)prnt;
391
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
483
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
392
    unsigned i, j;
484
    unsigned i, j;
393
    unsigned bps;       /* bytes per sector */
485
    unsigned bps;       /* bytes per sector */
394
    unsigned dps;       /* dentries per sector */
486
    unsigned dps;       /* dentries per sector */
395
    unsigned blocks;
487
    unsigned blocks;
396
    fat_dentry_t *d;
488
    fat_dentry_t *d;
397
    block_t *b;
489
    block_t *b;
398
 
490
 
399
    futex_down(&parentp->idx->lock);
491
    futex_down(&parentp->idx->lock);
400
    bps = fat_bps_get(parentp->idx->dev_handle);
492
    bps = fat_bps_get(parentp->idx->dev_handle);
401
    dps = bps / sizeof(fat_dentry_t);
493
    dps = bps / sizeof(fat_dentry_t);
402
    blocks = parentp->size / bps + (parentp->size % bps != 0);
494
    blocks = parentp->size / bps + (parentp->size % bps != 0);
403
    for (i = 0; i < blocks; i++) {
495
    for (i = 0; i < blocks; i++) {
404
        unsigned dentries;
496
        unsigned dentries;
405
       
497
       
406
        b = fat_block_get(parentp, i);
498
        b = fat_block_get(parentp, i);
407
        dentries = (i == blocks - 1) ?
499
        dentries = (i == blocks - 1) ?
408
            parentp->size % sizeof(fat_dentry_t) :
500
            parentp->size % sizeof(fat_dentry_t) :
409
            dps;
501
            dps;
410
        for (j = 0; j < dentries; j++) {
502
        for (j = 0; j < dentries; j++) {
411
            d = ((fat_dentry_t *)b->data) + j;
503
            d = ((fat_dentry_t *)b->data) + j;
412
            switch (fat_classify_dentry(d)) {
504
            switch (fat_classify_dentry(d)) {
413
            case FAT_DENTRY_SKIP:
505
            case FAT_DENTRY_SKIP:
414
                continue;
506
                continue;
415
            case FAT_DENTRY_LAST:
507
            case FAT_DENTRY_LAST:
416
                block_put(b);
508
                block_put(b);
417
                futex_up(&parentp->idx->lock);
509
                futex_up(&parentp->idx->lock);
418
                return NULL;
510
                return NULL;
419
            default:
511
            default:
420
            case FAT_DENTRY_VALID:
512
            case FAT_DENTRY_VALID:
421
                dentry_name_canonify(d, name);
513
                dentry_name_canonify(d, name);
422
                break;
514
                break;
423
            }
515
            }
424
            if (strcmp(name, component) == 0) {
516
            if (stricmp(name, component) == 0) {
425
                /* hit */
517
                /* hit */
426
                void *node;
518
                void *node;
427
                /*
519
                /*
428
                 * Assume tree hierarchy for locking.  We
520
                 * Assume tree hierarchy for locking.  We
429
                 * already have the parent and now we are going
521
                 * already have the parent and now we are going
430
                 * to lock the child.  Never lock in the oposite
522
                 * to lock the child.  Never lock in the oposite
431
                 * order.
523
                 * order.
432
                 */
524
                 */
433
                fat_idx_t *idx = fat_idx_get_by_pos(
525
                fat_idx_t *idx = fat_idx_get_by_pos(
434
                    parentp->idx->dev_handle, parentp->firstc,
526
                    parentp->idx->dev_handle, parentp->firstc,
435
                    i * dps + j);
527
                    i * dps + j);
436
                futex_up(&parentp->idx->lock);
528
                futex_up(&parentp->idx->lock);
437
                if (!idx) {
529
                if (!idx) {
438
                    /*
530
                    /*
439
                     * Can happen if memory is low or if we
531
                     * Can happen if memory is low or if we
440
                     * run out of 32-bit indices.
532
                     * run out of 32-bit indices.
441
                     */
533
                     */
442
                    block_put(b);
534
                    block_put(b);
443
                    return NULL;
535
                    return NULL;
444
                }
536
                }
445
                node = fat_node_get_core(idx);
537
                node = fat_node_get_core(idx);
446
                futex_up(&idx->lock);
538
                futex_up(&idx->lock);
447
                block_put(b);
539
                block_put(b);
448
                return node;
540
                return node;
449
            }
541
            }
450
        }
542
        }
451
        block_put(b);
543
        block_put(b);
452
    }
544
    }
453
    futex_up(&parentp->idx->lock);
545
    futex_up(&parentp->idx->lock);
454
    return NULL;
546
    return NULL;
455
}
547
}
456
 
548
 
457
static fs_index_t fat_index_get(void *node)
549
static fs_index_t fat_index_get(void *node)
458
{
550
{
459
    fat_node_t *fnodep = (fat_node_t *)node;
551
    fat_node_t *fnodep = (fat_node_t *)node;
460
    if (!fnodep)
552
    if (!fnodep)
461
        return 0;
553
        return 0;
462
    return fnodep->idx->index;
554
    return fnodep->idx->index;
463
}
555
}
464
 
556
 
465
static size_t fat_size_get(void *node)
557
static size_t fat_size_get(void *node)
466
{
558
{
467
    return ((fat_node_t *)node)->size;
559
    return ((fat_node_t *)node)->size;
468
}
560
}
469
 
561
 
470
static unsigned fat_lnkcnt_get(void *node)
562
static unsigned fat_lnkcnt_get(void *node)
471
{
563
{
472
    return ((fat_node_t *)node)->lnkcnt;
564
    return ((fat_node_t *)node)->lnkcnt;
473
}
565
}
474
 
566
 
475
static bool fat_has_children(void *node)
567
static bool fat_has_children(void *node)
476
{
568
{
477
    fat_node_t *nodep = (fat_node_t *)node;
569
    fat_node_t *nodep = (fat_node_t *)node;
478
    unsigned bps;
570
    unsigned bps;
479
    unsigned dps;
571
    unsigned dps;
480
    unsigned blocks;
572
    unsigned blocks;
481
    block_t *b;
573
    block_t *b;
482
    unsigned i, j;
574
    unsigned i, j;
483
 
575
 
484
    if (nodep->type != FAT_DIRECTORY)
576
    if (nodep->type != FAT_DIRECTORY)
485
        return false;
577
        return false;
486
 
578
 
487
    futex_down(&nodep->idx->lock);
579
    futex_down(&nodep->idx->lock);
488
    bps = fat_bps_get(nodep->idx->dev_handle);
580
    bps = fat_bps_get(nodep->idx->dev_handle);
489
    dps = bps / sizeof(fat_dentry_t);
581
    dps = bps / sizeof(fat_dentry_t);
490
 
582
 
491
    blocks = nodep->size / bps + (nodep->size % bps != 0);
583
    blocks = nodep->size / bps + (nodep->size % bps != 0);
492
 
584
 
493
    for (i = 0; i < blocks; i++) {
585
    for (i = 0; i < blocks; i++) {
494
        unsigned dentries;
586
        unsigned dentries;
495
        fat_dentry_t *d;
587
        fat_dentry_t *d;
496
   
588
   
497
        b = fat_block_get(nodep, i);
589
        b = fat_block_get(nodep, i);
498
        dentries = (i == blocks - 1) ?
590
        dentries = (i == blocks - 1) ?
499
            nodep->size % sizeof(fat_dentry_t) :
591
            nodep->size % sizeof(fat_dentry_t) :
500
            dps;
592
            dps;
501
        for (j = 0; j < dentries; j++) {
593
        for (j = 0; j < dentries; j++) {
502
            d = ((fat_dentry_t *)b->data) + j;
594
            d = ((fat_dentry_t *)b->data) + j;
503
            switch (fat_classify_dentry(d)) {
595
            switch (fat_classify_dentry(d)) {
504
            case FAT_DENTRY_SKIP:
596
            case FAT_DENTRY_SKIP:
505
                continue;
597
                continue;
506
            case FAT_DENTRY_LAST:
598
            case FAT_DENTRY_LAST:
507
                block_put(b);
599
                block_put(b);
508
                futex_up(&nodep->idx->lock);
600
                futex_up(&nodep->idx->lock);
509
                return false;
601
                return false;
510
            default:
602
            default:
511
            case FAT_DENTRY_VALID:
603
            case FAT_DENTRY_VALID:
512
                block_put(b);
604
                block_put(b);
513
                futex_up(&nodep->idx->lock);
605
                futex_up(&nodep->idx->lock);
514
                return true;
606
                return true;
515
            }
607
            }
516
            block_put(b);
608
            block_put(b);
517
            futex_up(&nodep->idx->lock);
609
            futex_up(&nodep->idx->lock);
518
            return true;
610
            return true;
519
        }
611
        }
520
        block_put(b);
612
        block_put(b);
521
    }
613
    }
522
 
614
 
523
    futex_up(&nodep->idx->lock);
615
    futex_up(&nodep->idx->lock);
524
    return false;
616
    return false;
525
}
617
}
526
 
618
 
527
static void *fat_root_get(dev_handle_t dev_handle)
619
static void *fat_root_get(dev_handle_t dev_handle)
528
{
620
{
529
    return fat_node_get(dev_handle, 0);
621
    return fat_node_get(dev_handle, 0);
530
}
622
}
531
 
623
 
532
static char fat_plb_get_char(unsigned pos)
624
static char fat_plb_get_char(unsigned pos)
533
{
625
{
534
    return fat_reg.plb_ro[pos % PLB_SIZE];
626
    return fat_reg.plb_ro[pos % PLB_SIZE];
535
}
627
}
536
 
628
 
537
static bool fat_is_directory(void *node)
629
static bool fat_is_directory(void *node)
538
{
630
{
539
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
631
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
540
}
632
}
541
 
633
 
542
static bool fat_is_file(void *node)
634
static bool fat_is_file(void *node)
543
{
635
{
544
    return ((fat_node_t *)node)->type == FAT_FILE;
636
    return ((fat_node_t *)node)->type == FAT_FILE;
545
}
637
}
546
 
638
 
547
/** libfs operations */
639
/** libfs operations */
548
libfs_ops_t fat_libfs_ops = {
640
libfs_ops_t fat_libfs_ops = {
549
    .match = fat_match,
641
    .match = fat_match,
550
    .node_get = fat_node_get,
642
    .node_get = fat_node_get,
551
    .node_put = fat_node_put,
643
    .node_put = fat_node_put,
552
    .create = fat_create,
644
    .create = fat_create,
553
    .destroy = fat_destroy,
645
    .destroy = fat_destroy,
554
    .link = fat_link,
646
    .link = fat_link,
555
    .unlink = fat_unlink,
647
    .unlink = fat_unlink,
556
    .index_get = fat_index_get,
648
    .index_get = fat_index_get,
557
    .size_get = fat_size_get,
649
    .size_get = fat_size_get,
558
    .lnkcnt_get = fat_lnkcnt_get,
650
    .lnkcnt_get = fat_lnkcnt_get,
559
    .has_children = fat_has_children,
651
    .has_children = fat_has_children,
560
    .root_get = fat_root_get,
652
    .root_get = fat_root_get,
561
    .plb_get_char = fat_plb_get_char,
653
    .plb_get_char = fat_plb_get_char,
562
    .is_directory = fat_is_directory,
654
    .is_directory = fat_is_directory,
563
    .is_file = fat_is_file
655
    .is_file = fat_is_file
564
};
656
};
565
 
657
 
566
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
658
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
567
{
659
{
568
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
660
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
569
    block_t *bb;
661
    block_t *bb;
-
 
662
    uint16_t bps;
570
    uint16_t rde;
663
    uint16_t rde;
571
    int rc;
664
    int rc;
572
 
665
 
-
 
666
    /*
-
 
667
     * For now, we don't bother to remember dev_handle, dev_phone or
-
 
668
     * dev_buffer in some data structure. We use global variables because we
-
 
669
     * know there will be at most one mount on this file system.
-
 
670
     * Of course, this is a huge TODO item.
-
 
671
     */
-
 
672
    dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
-
 
673
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-
 
674
   
-
 
675
    if (!dev_buffer) {
-
 
676
        ipc_answer_0(rid, ENOMEM);
-
 
677
        return;
-
 
678
    }
-
 
679
 
-
 
680
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
-
 
681
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
-
 
682
 
-
 
683
    if (dev_phone < 0) {
-
 
684
        munmap(dev_buffer, BS_SIZE);
-
 
685
        ipc_answer_0(rid, dev_phone);
-
 
686
        return;
-
 
687
    }
-
 
688
 
-
 
689
    rc = ipc_share_out_start(dev_phone, dev_buffer,
-
 
690
        AS_AREA_READ | AS_AREA_WRITE);
-
 
691
    if (rc != EOK) {
-
 
692
            munmap(dev_buffer, BS_SIZE);
-
 
693
        ipc_answer_0(rid, rc);
-
 
694
        return;
-
 
695
    }
-
 
696
 
573
    /* Read the number of root directory entries. */
697
    /* Read the number of root directory entries. */
574
    bb = block_get(dev_handle, BS_BLOCK);
698
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
699
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
575
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
700
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
576
    block_put(bb);
701
    block_put(bb);
577
 
702
 
-
 
703
    if (bps != BS_SIZE) {
-
 
704
        munmap(dev_buffer, BS_SIZE);
-
 
705
        ipc_answer_0(rid, ENOTSUP);
-
 
706
        return;
-
 
707
    }
-
 
708
 
578
    rc = fat_idx_init_by_dev_handle(dev_handle);
709
    rc = fat_idx_init_by_dev_handle(dev_handle);
579
    if (rc != EOK) {
710
    if (rc != EOK) {
-
 
711
            munmap(dev_buffer, BS_SIZE);
580
        ipc_answer_0(rid, rc);
712
        ipc_answer_0(rid, rc);
581
        return;
713
        return;
582
    }
714
    }
583
 
715
 
584
    /* Initialize the root node. */
716
    /* Initialize the root node. */
585
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
717
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
586
    if (!rootp) {
718
    if (!rootp) {
-
 
719
            munmap(dev_buffer, BS_SIZE);
587
        fat_idx_fini_by_dev_handle(dev_handle);
720
        fat_idx_fini_by_dev_handle(dev_handle);
588
        ipc_answer_0(rid, ENOMEM);
721
        ipc_answer_0(rid, ENOMEM);
589
        return;
722
        return;
590
    }
723
    }
591
    fat_node_initialize(rootp);
724
    fat_node_initialize(rootp);
592
 
725
 
593
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
726
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
594
    if (!ridxp) {
727
    if (!ridxp) {
-
 
728
            munmap(dev_buffer, BS_SIZE);
595
        free(rootp);
729
        free(rootp);
596
        fat_idx_fini_by_dev_handle(dev_handle);
730
        fat_idx_fini_by_dev_handle(dev_handle);
597
        ipc_answer_0(rid, ENOMEM);
731
        ipc_answer_0(rid, ENOMEM);
598
        return;
732
        return;
599
    }
733
    }
600
    assert(ridxp->index == 0);
734
    assert(ridxp->index == 0);
601
    /* ridxp->lock held */
735
    /* ridxp->lock held */
602
 
736
 
603
    rootp->type = FAT_DIRECTORY;
737
    rootp->type = FAT_DIRECTORY;
604
    rootp->firstc = FAT_CLST_ROOT;
738
    rootp->firstc = FAT_CLST_ROOT;
605
    rootp->refcnt = 1;
739
    rootp->refcnt = 1;
-
 
740
    rootp->lnkcnt = 0;  /* FS root is not linked */
606
    rootp->size = rde * sizeof(fat_dentry_t);
741
    rootp->size = rde * sizeof(fat_dentry_t);
607
    rootp->idx = ridxp;
742
    rootp->idx = ridxp;
608
    ridxp->nodep = rootp;
743
    ridxp->nodep = rootp;
609
   
744
   
610
    futex_up(&ridxp->lock);
745
    futex_up(&ridxp->lock);
611
 
746
 
612
    ipc_answer_0(rid, EOK);
747
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
613
}
748
}
614
 
749
 
615
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
750
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
616
{
751
{
617
    ipc_answer_0(rid, ENOTSUP);
752
    ipc_answer_0(rid, ENOTSUP);
618
}
753
}
619
 
754
 
620
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
755
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
621
{
756
{
622
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
757
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
623
}
758
}
-
 
759
 
-
 
760
void fat_read(ipc_callid_t rid, ipc_call_t *request)
-
 
761
{
-
 
762
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
-
 
763
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
-
 
764
    off_t pos = (off_t)IPC_GET_ARG3(*request);
-
 
765
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
-
 
766
    uint16_t bps = fat_bps_get(dev_handle);
-
 
767
    size_t bytes;
-
 
768
    block_t *b;
-
 
769
 
-
 
770
    if (!nodep) {
-
 
771
        ipc_answer_0(rid, ENOENT);
-
 
772
        return;
-
 
773
    }
-
 
774
 
-
 
775
    ipc_callid_t callid;
-
 
776
    size_t len;
-
 
777
    if (!ipc_data_read_receive(&callid, &len)) {
-
 
778
        fat_node_put(nodep);
-
 
779
        ipc_answer_0(callid, EINVAL);
-
 
780
        ipc_answer_0(rid, EINVAL);
-
 
781
        return;
-
 
782
    }
-
 
783
 
-
 
784
    if (nodep->type == FAT_FILE) {
-
 
785
        /*
-
 
786
         * Our strategy for regular file reads is to read one block at
-
 
787
         * most and make use of the possibility to return less data than
-
 
788
         * requested. This keeps the code very simple.
-
 
789
         */
-
 
790
        bytes = min(len, bps - pos % bps);
-
 
791
        b = fat_block_get(nodep, pos / bps);
-
 
792
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
-
 
793
            bytes);
-
 
794
        block_put(b);
-
 
795
    } else {
-
 
796
        unsigned bnum;
-
 
797
        off_t spos = pos;
-
 
798
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
-
 
799
        fat_dentry_t *d;
-
 
800
 
-
 
801
        assert(nodep->type == FAT_DIRECTORY);
-
 
802
        assert(nodep->size % bps == 0);
-
 
803
        assert(bps % sizeof(fat_dentry_t) == 0);
-
 
804
 
-
 
805
        /*
-
 
806
         * Our strategy for readdir() is to use the position pointer as
-
 
807
         * an index into the array of all dentries. On entry, it points
-
 
808
         * to the first unread dentry. If we skip any dentries, we bump
-
 
809
         * the position pointer accordingly.
-
 
810
         */
-
 
811
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
-
 
812
        while (bnum < nodep->size / bps) {
-
 
813
            off_t o;
-
 
814
 
-
 
815
            b = fat_block_get(nodep, bnum);
-
 
816
            for (o = pos % (bps / sizeof(fat_dentry_t));
-
 
817
                o < bps / sizeof(fat_dentry_t);
-
 
818
                o++, pos++) {
-
 
819
                d = ((fat_dentry_t *)b->data) + o;
-
 
820
                switch (fat_classify_dentry(d)) {
-
 
821
                case FAT_DENTRY_SKIP:
-
 
822
                    continue;
-
 
823
                case FAT_DENTRY_LAST:
-
 
824
                    block_put(b);
-
 
825
                    goto miss;
-
 
826
                default:
-
 
827
                case FAT_DENTRY_VALID:
-
 
828
                    dentry_name_canonify(d, name);
-
 
829
                    block_put(b);
-
 
830
                    goto hit;
-
 
831
                }
-
 
832
            }
-
 
833
            block_put(b);
-
 
834
            bnum++;
-
 
835
        }
-
 
836
miss:
-
 
837
        fat_node_put(nodep);
-
 
838
        ipc_answer_0(callid, ENOENT);
-
 
839
        ipc_answer_1(rid, ENOENT, 0);
-
 
840
        return;
-
 
841
hit:
-
 
842
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
-
 
843
        bytes = (pos - spos) + 1;
-
 
844
    }
-
 
845
 
-
 
846
    fat_node_put(nodep);
-
 
847
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
-
 
848
}
624
 
849
 
625
/**
850
/**
626
 * @}
851
 * @}
627
 */
852
 */
628
 
853