Subversion Repositories HelenOS

Rev

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

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