Subversion Repositories HelenOS

Rev

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

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