Subversion Repositories HelenOS

Rev

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

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