Subversion Repositories HelenOS

Rev

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

Rev 3253 Rev 3257
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    fat_ops.c
34
 * @file    fat_ops.c
35
 * @brief   Implementation of VFS operations for the FAT file system server.
35
 * @brief   Implementation of VFS operations for the FAT file system server.
36
 */
36
 */
37
 
37
 
38
#include "fat.h"
38
#include "fat.h"
39
#include "../../vfs/vfs.h"
39
#include "../../vfs/vfs.h"
40
#include <libfs.h>
40
#include <libfs.h>
41
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
-
 
42
#include <ipc/services.h>
-
 
43
#include <ipc/devmap.h>
42
#include <async.h>
44
#include <async.h>
43
#include <errno.h>
45
#include <errno.h>
44
#include <string.h>
46
#include <string.h>
45
#include <byteorder.h>
47
#include <byteorder.h>
46
#include <libadt/hash_table.h>
48
#include <libadt/hash_table.h>
47
#include <libadt/list.h>
49
#include <libadt/list.h>
48
#include <assert.h>
50
#include <assert.h>
49
#include <futex.h>
51
#include <futex.h>
-
 
52
#include <sys/mman.h>
50
 
53
 
51
#define BS_BLOCK        0
54
#define BS_BLOCK        0
52
#define BS_SIZE         512
55
#define BS_SIZE         512
53
 
56
 
54
/** Futex protecting the list of cached free FAT nodes. */
57
/** Futex protecting the list of cached free FAT nodes. */
55
static futex_t ffn_futex = FUTEX_INITIALIZER;
58
static futex_t ffn_futex = FUTEX_INITIALIZER;
56
 
59
 
57
/** List of cached free FAT nodes. */
60
/** List of cached free FAT nodes. */
58
static LIST_INITIALIZE(ffn_head);
61
static LIST_INITIALIZE(ffn_head);
59
 
62
 
60
#define FAT_NAME_LEN        8
63
#define FAT_NAME_LEN        8
61
#define FAT_EXT_LEN     3
64
#define FAT_EXT_LEN     3
62
 
65
 
63
#define FAT_PAD         ' ' 
66
#define FAT_PAD         ' ' 
64
 
67
 
65
#define FAT_DENTRY_UNUSED   0x00
68
#define FAT_DENTRY_UNUSED   0x00
66
#define FAT_DENTRY_E5_ESC   0x05
69
#define FAT_DENTRY_E5_ESC   0x05
67
#define FAT_DENTRY_DOT      0x2e
70
#define FAT_DENTRY_DOT      0x2e
68
#define FAT_DENTRY_ERASED   0xe5
71
#define FAT_DENTRY_ERASED   0xe5
69
 
72
 
70
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
73
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
71
{
74
{
72
    int i;
75
    int i;
73
 
76
 
74
    for (i = 0; i < FAT_NAME_LEN; i++) {
77
    for (i = 0; i < FAT_NAME_LEN; i++) {
75
        if (d->name[i] == FAT_PAD) {
78
        if (d->name[i] == FAT_PAD) {
76
            buf++;
79
            buf++;
77
            break;
80
            break;
78
        }
81
        }
79
        if (d->name[i] == FAT_DENTRY_E5_ESC)
82
        if (d->name[i] == FAT_DENTRY_E5_ESC)
80
            *buf++ = 0xe5;
83
            *buf++ = 0xe5;
81
        else
84
        else
82
            *buf++ = d->name[i];
85
            *buf++ = d->name[i];
83
    }
86
    }
84
    if (d->ext[0] != FAT_PAD)
87
    if (d->ext[0] != FAT_PAD)
85
        *buf++ = '.';
88
        *buf++ = '.';
86
    for (i = 0; i < FAT_EXT_LEN; i++) {
89
    for (i = 0; i < FAT_EXT_LEN; i++) {
87
        if (d->ext[i] == FAT_PAD) {
90
        if (d->ext[i] == FAT_PAD) {
88
            *buf = '\0';
91
            *buf = '\0';
89
            return;
92
            return;
90
        }
93
        }
91
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
94
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
92
            *buf++ = 0xe5;
95
            *buf++ = 0xe5;
93
        else
96
        else
94
            *buf++ = d->ext[i];
97
            *buf++ = d->ext[i];
95
    }
98
    }
96
}
99
}
97
 
100
 
-
 
101
static int dev_phone = -1;      /* FIXME */
-
 
102
static void *dev_buffer = NULL;     /* FIXME */
-
 
103
 
98
/* TODO move somewhere else */
104
/* TODO move somewhere else */
99
typedef struct {
105
typedef struct {
100
    void *data;
106
    void *data;
-
 
107
    size_t size;
101
} block_t;
108
} block_t;
102
 
109
 
103
static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
110
static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
104
{
111
{
-
 
112
    /* FIXME */
-
 
113
    block_t *b;
-
 
114
    off_t bufpos = 0;
-
 
115
    size_t buflen = 0;
-
 
116
 
-
 
117
    assert(dev_phone != -1);
-
 
118
    assert(dev_buffer);
-
 
119
 
-
 
120
    b = malloc(sizeof(block_t));
-
 
121
    if (!b)
105
    return NULL;    /* TODO */
122
        return NULL;
-
 
123
   
-
 
124
    b->data = malloc(bs);
-
 
125
    if (!b->data) {
-
 
126
        free(b);
-
 
127
        return NULL;
-
 
128
    }
-
 
129
    b->size = bs;
-
 
130
 
-
 
131
    if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &offset,
-
 
132
        b->data, bs, bs)) {
-
 
133
        free(b->data);
-
 
134
        free(b);
-
 
135
        return NULL;
-
 
136
    }
-
 
137
 
-
 
138
    return b;
106
}
139
}
107
 
140
 
108
static void block_put(block_t *block)
141
static void block_put(block_t *block)
109
{
142
{
110
    /* TODO */
143
    /* FIXME */
-
 
144
    free(block->data);
-
 
145
    free(block);
111
}
146
}
112
 
147
 
113
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
148
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
114
 
149
 
115
#define FAT_CLST_RES0   0x0000
150
#define FAT_CLST_RES0   0x0000
116
#define FAT_CLST_RES1   0x0001
151
#define FAT_CLST_RES1   0x0001
117
#define FAT_CLST_FIRST  0x0002
152
#define FAT_CLST_FIRST  0x0002
118
#define FAT_CLST_BAD    0xfff7
153
#define FAT_CLST_BAD    0xfff7
119
#define FAT_CLST_LAST1  0xfff8
154
#define FAT_CLST_LAST1  0xfff8
120
#define FAT_CLST_LAST8  0xffff
155
#define FAT_CLST_LAST8  0xffff
121
 
156
 
122
/* internally used to mark root directory's parent */
157
/* internally used to mark root directory's parent */
123
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
158
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
124
/* internally used to mark root directory */
159
/* internally used to mark root directory */
125
#define FAT_CLST_ROOT       FAT_CLST_RES1
160
#define FAT_CLST_ROOT       FAT_CLST_RES1
126
 
161
 
127
#define fat_block_get(np, off) \
162
#define fat_block_get(np, off) \
128
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
163
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
129
 
164
 
130
static block_t *
165
static block_t *
131
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
166
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
132
{
167
{
133
    block_t *bb;
168
    block_t *bb;
134
    block_t *b;
169
    block_t *b;
135
    unsigned bps;
170
    unsigned bps;
136
    unsigned spc;
171
    unsigned spc;
137
    unsigned rscnt;     /* block address of the first FAT */
172
    unsigned rscnt;     /* block address of the first FAT */
138
    unsigned fatcnt;
173
    unsigned fatcnt;
139
    unsigned rde;
174
    unsigned rde;
140
    unsigned rds;       /* root directory size */
175
    unsigned rds;       /* root directory size */
141
    unsigned sf;
176
    unsigned sf;
142
    unsigned ssa;       /* size of the system area */
177
    unsigned ssa;       /* size of the system area */
143
    unsigned clusters;
178
    unsigned clusters;
144
    fat_cluster_t clst = firstc;
179
    fat_cluster_t clst = firstc;
145
    unsigned i;
180
    unsigned i;
146
 
181
 
147
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
182
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
148
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
183
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
149
    spc = FAT_BS(bb)->spc;
184
    spc = FAT_BS(bb)->spc;
150
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
185
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
151
    fatcnt = FAT_BS(bb)->fatcnt;
186
    fatcnt = FAT_BS(bb)->fatcnt;
152
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
187
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
153
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
188
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
154
    block_put(bb);
189
    block_put(bb);
155
 
190
 
156
    rds = (sizeof(fat_dentry_t) * rde) / bps;
191
    rds = (sizeof(fat_dentry_t) * rde) / bps;
157
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
192
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
158
    ssa = rscnt + fatcnt * sf + rds;
193
    ssa = rscnt + fatcnt * sf + rds;
159
 
194
 
160
    if (firstc == FAT_CLST_ROOT) {
195
    if (firstc == FAT_CLST_ROOT) {
161
        /* root directory special case */
196
        /* root directory special case */
162
        assert(offset < rds);
197
        assert(offset < rds);
163
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
198
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
164
        return b;
199
        return b;
165
    }
200
    }
166
 
201
 
167
    clusters = offset / spc;
202
    clusters = offset / spc;
168
    for (i = 0; i < clusters; i++) {
203
    for (i = 0; i < clusters; i++) {
169
        unsigned fsec;  /* sector offset relative to FAT1 */
204
        unsigned fsec;  /* sector offset relative to FAT1 */
170
        unsigned fidx;  /* FAT1 entry index */
205
        unsigned fidx;  /* FAT1 entry index */
171
 
206
 
172
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
207
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
173
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
208
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
174
        fidx = clst % (bps / sizeof(fat_cluster_t));
209
        fidx = clst % (bps / sizeof(fat_cluster_t));
175
        /* read FAT1 */
210
        /* read FAT1 */
176
        b = block_get(dev_handle, rscnt + fsec, bps);
211
        b = block_get(dev_handle, rscnt + fsec, bps);
177
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
212
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
178
        assert(clst != FAT_CLST_BAD);
213
        assert(clst != FAT_CLST_BAD);
179
        assert(clst < FAT_CLST_LAST1);
214
        assert(clst < FAT_CLST_LAST1);
180
        block_put(b);
215
        block_put(b);
181
    }
216
    }
182
 
217
 
183
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
218
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
184
        offset % spc, bps);
219
        offset % spc, bps);
185
 
220
 
186
    return b;
221
    return b;
187
}
222
}
188
 
223
 
189
static void fat_node_initialize(fat_node_t *node)
224
static void fat_node_initialize(fat_node_t *node)
190
{
225
{
191
    futex_initialize(&node->lock, 1);
226
    futex_initialize(&node->lock, 1);
192
    node->idx = NULL;
227
    node->idx = NULL;
193
    node->type = 0;
228
    node->type = 0;
194
    link_initialize(&node->ffn_link);
229
    link_initialize(&node->ffn_link);
195
    node->size = 0;
230
    node->size = 0;
196
    node->lnkcnt = 0;
231
    node->lnkcnt = 0;
197
    node->refcnt = 0;
232
    node->refcnt = 0;
198
    node->dirty = false;
233
    node->dirty = false;
199
}
234
}
200
 
235
 
201
static uint16_t fat_bps_get(dev_handle_t dev_handle)
236
static uint16_t fat_bps_get(dev_handle_t dev_handle)
202
{
237
{
203
    block_t *bb;
238
    block_t *bb;
204
    uint16_t bps;
239
    uint16_t bps;
205
   
240
   
206
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
241
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
207
    assert(bb != NULL);
242
    assert(bb != NULL);
208
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
243
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
209
    block_put(bb);
244
    block_put(bb);
210
 
245
 
211
    return bps;
246
    return bps;
212
}
247
}
213
 
248
 
214
typedef enum {
249
typedef enum {
215
    FAT_DENTRY_SKIP,
250
    FAT_DENTRY_SKIP,
216
    FAT_DENTRY_LAST,
251
    FAT_DENTRY_LAST,
217
    FAT_DENTRY_VALID
252
    FAT_DENTRY_VALID
218
} fat_dentry_clsf_t;
253
} fat_dentry_clsf_t;
219
 
254
 
220
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
255
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
221
{
256
{
222
    if (d->attr & FAT_ATTR_VOLLABEL) {
257
    if (d->attr & FAT_ATTR_VOLLABEL) {
223
        /* volume label entry */
258
        /* volume label entry */
224
        return FAT_DENTRY_SKIP;
259
        return FAT_DENTRY_SKIP;
225
    }
260
    }
226
    if (d->name[0] == FAT_DENTRY_ERASED) {
261
    if (d->name[0] == FAT_DENTRY_ERASED) {
227
        /* not-currently-used entry */
262
        /* not-currently-used entry */
228
        return FAT_DENTRY_SKIP;
263
        return FAT_DENTRY_SKIP;
229
    }
264
    }
230
    if (d->name[0] == FAT_DENTRY_UNUSED) {
265
    if (d->name[0] == FAT_DENTRY_UNUSED) {
231
        /* never used entry */
266
        /* never used entry */
232
        return FAT_DENTRY_LAST;
267
        return FAT_DENTRY_LAST;
233
    }
268
    }
234
    if (d->name[0] == FAT_DENTRY_DOT) {
269
    if (d->name[0] == FAT_DENTRY_DOT) {
235
        /*
270
        /*
236
         * Most likely '.' or '..'.
271
         * Most likely '.' or '..'.
237
         * It cannot occur in a regular file name.
272
         * It cannot occur in a regular file name.
238
         */
273
         */
239
        return FAT_DENTRY_SKIP;
274
        return FAT_DENTRY_SKIP;
240
    }
275
    }
241
    return FAT_DENTRY_VALID;
276
    return FAT_DENTRY_VALID;
242
}
277
}
243
 
278
 
244
static void fat_node_sync(fat_node_t *node)
279
static void fat_node_sync(fat_node_t *node)
245
{
280
{
246
    /* TODO */
281
    /* TODO */
247
}
282
}
248
 
283
 
249
/** Internal version of fat_node_get().
284
/** Internal version of fat_node_get().
250
 *
285
 *
251
 * @param idxp      Locked index structure.
286
 * @param idxp      Locked index structure.
252
 */
287
 */
253
static void *fat_node_get_core(fat_idx_t *idxp)
288
static void *fat_node_get_core(fat_idx_t *idxp)
254
{
289
{
255
    block_t *b;
290
    block_t *b;
256
    fat_dentry_t *d;
291
    fat_dentry_t *d;
257
    fat_node_t *nodep;
292
    fat_node_t *nodep;
258
    unsigned bps;
293
    unsigned bps;
259
    unsigned dps;
294
    unsigned dps;
260
 
295
 
261
    if (idxp->nodep) {
296
    if (idxp->nodep) {
262
        /*
297
        /*
263
         * We are lucky.
298
         * We are lucky.
264
         * The node is already instantiated in memory.
299
         * The node is already instantiated in memory.
265
         */
300
         */
266
        futex_down(&idxp->nodep->lock);
301
        futex_down(&idxp->nodep->lock);
267
        if (!idxp->nodep->refcnt++)
302
        if (!idxp->nodep->refcnt++)
268
            list_remove(&nodep->ffn_link);
303
            list_remove(&nodep->ffn_link);
269
        futex_up(&idxp->nodep->lock);
304
        futex_up(&idxp->nodep->lock);
270
        return idxp->nodep;
305
        return idxp->nodep;
271
    }
306
    }
272
 
307
 
273
    /*
308
    /*
274
     * We must instantiate the node from the file system.
309
     * We must instantiate the node from the file system.
275
     */
310
     */
276
   
311
   
277
    assert(idxp->pfc);
312
    assert(idxp->pfc);
278
 
313
 
279
    futex_down(&ffn_futex);
314
    futex_down(&ffn_futex);
280
    if (!list_empty(&ffn_head)) {
315
    if (!list_empty(&ffn_head)) {
281
        /* Try to use a cached free node structure. */
316
        /* Try to use a cached free node structure. */
282
        fat_idx_t *idxp_tmp;
317
        fat_idx_t *idxp_tmp;
283
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
318
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
284
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
319
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
285
            goto skip_cache;
320
            goto skip_cache;
286
        idxp_tmp = nodep->idx;
321
        idxp_tmp = nodep->idx;
287
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
322
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
288
            futex_up(&nodep->lock);
323
            futex_up(&nodep->lock);
289
            goto skip_cache;
324
            goto skip_cache;
290
        }
325
        }
291
        list_remove(&nodep->ffn_link);
326
        list_remove(&nodep->ffn_link);
292
        futex_up(&ffn_futex);
327
        futex_up(&ffn_futex);
293
        if (nodep->dirty)
328
        if (nodep->dirty)
294
            fat_node_sync(nodep);
329
            fat_node_sync(nodep);
295
        idxp_tmp->nodep = NULL;
330
        idxp_tmp->nodep = NULL;
296
        futex_up(&nodep->lock);
331
        futex_up(&nodep->lock);
297
        futex_up(&idxp_tmp->lock);
332
        futex_up(&idxp_tmp->lock);
298
    } else {
333
    } else {
299
skip_cache:
334
skip_cache:
300
        /* Try to allocate a new node structure. */
335
        /* Try to allocate a new node structure. */
301
        futex_up(&ffn_futex);
336
        futex_up(&ffn_futex);
302
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
337
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
303
        if (!nodep)
338
        if (!nodep)
304
            return NULL;
339
            return NULL;
305
    }
340
    }
306
    fat_node_initialize(nodep);
341
    fat_node_initialize(nodep);
307
 
342
 
308
    bps = fat_bps_get(idxp->dev_handle);
343
    bps = fat_bps_get(idxp->dev_handle);
309
    dps = bps / sizeof(fat_dentry_t);
344
    dps = bps / sizeof(fat_dentry_t);
310
 
345
 
311
    /* Read the block that contains the dentry of interest. */
346
    /* Read the block that contains the dentry of interest. */
312
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
347
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
313
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
348
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
314
    assert(b);
349
    assert(b);
315
 
350
 
316
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
351
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
317
    if (d->attr & FAT_ATTR_SUBDIR) {
352
    if (d->attr & FAT_ATTR_SUBDIR) {
318
        /*
353
        /*
319
         * The only directory which does not have this bit set is the
354
         * The only directory which does not have this bit set is the
320
         * root directory itself. The root directory node is handled
355
         * root directory itself. The root directory node is handled
321
         * and initialized elsewhere.
356
         * and initialized elsewhere.
322
         */
357
         */
323
        nodep->type = FAT_DIRECTORY;
358
        nodep->type = FAT_DIRECTORY;
324
    } else {
359
    } else {
325
        nodep->type = FAT_FILE;
360
        nodep->type = FAT_FILE;
326
    }
361
    }
327
    nodep->firstc = uint16_t_le2host(d->firstc);
362
    nodep->firstc = uint16_t_le2host(d->firstc);
328
    nodep->size = uint32_t_le2host(d->size);
363
    nodep->size = uint32_t_le2host(d->size);
329
    nodep->lnkcnt = 1;
364
    nodep->lnkcnt = 1;
330
    nodep->refcnt = 1;
365
    nodep->refcnt = 1;
331
 
366
 
332
    block_put(b);
367
    block_put(b);
333
 
368
 
334
    /* Link the idx structure with the node structure. */
369
    /* Link the idx structure with the node structure. */
335
    nodep->idx = idxp;
370
    nodep->idx = idxp;
336
    idxp->nodep = nodep;
371
    idxp->nodep = nodep;
337
 
372
 
338
    return nodep;
373
    return nodep;
339
}
374
}
340
 
375
 
341
/** Instantiate a FAT in-core node. */
376
/** Instantiate a FAT in-core node. */
342
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
377
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
343
{
378
{
344
    void *node;
379
    void *node;
345
    fat_idx_t *idxp;
380
    fat_idx_t *idxp;
346
 
381
 
347
    idxp = fat_idx_get_by_index(dev_handle, index);
382
    idxp = fat_idx_get_by_index(dev_handle, index);
348
    if (!idxp)
383
    if (!idxp)
349
        return NULL;
384
        return NULL;
350
    /* idxp->lock held */
385
    /* idxp->lock held */
351
    node = fat_node_get_core(idxp);
386
    node = fat_node_get_core(idxp);
352
    futex_up(&idxp->lock);
387
    futex_up(&idxp->lock);
353
    return node;
388
    return node;
354
}
389
}
355
 
390
 
356
static void fat_node_put(void *node)
391
static void fat_node_put(void *node)
357
{
392
{
358
    fat_node_t *nodep = (fat_node_t *)node;
393
    fat_node_t *nodep = (fat_node_t *)node;
359
 
394
 
360
    futex_down(&nodep->lock);
395
    futex_down(&nodep->lock);
361
    if (!--nodep->refcnt) {
396
    if (!--nodep->refcnt) {
362
        futex_down(&ffn_futex);
397
        futex_down(&ffn_futex);
363
        list_append(&nodep->ffn_link, &ffn_head);
398
        list_append(&nodep->ffn_link, &ffn_head);
364
        futex_up(&ffn_futex);
399
        futex_up(&ffn_futex);
365
    }
400
    }
366
    futex_up(&nodep->lock);
401
    futex_up(&nodep->lock);
367
}
402
}
368
 
403
 
369
static void *fat_create(int flags)
404
static void *fat_create(int flags)
370
{
405
{
371
    return NULL;    /* not supported at the moment */
406
    return NULL;    /* not supported at the moment */
372
}
407
}
373
 
408
 
374
static int fat_destroy(void *node)
409
static int fat_destroy(void *node)
375
{
410
{
376
    return ENOTSUP; /* not supported at the moment */
411
    return ENOTSUP; /* not supported at the moment */
377
}
412
}
378
 
413
 
379
static bool fat_link(void *prnt, void *chld, const char *name)
414
static bool fat_link(void *prnt, void *chld, const char *name)
380
{
415
{
381
    return false;   /* not supported at the moment */
416
    return false;   /* not supported at the moment */
382
}
417
}
383
 
418
 
384
static int fat_unlink(void *prnt, void *chld)
419
static int fat_unlink(void *prnt, void *chld)
385
{
420
{
386
    return ENOTSUP; /* not supported at the moment */
421
    return ENOTSUP; /* not supported at the moment */
387
}
422
}
388
 
423
 
389
static void *fat_match(void *prnt, const char *component)
424
static void *fat_match(void *prnt, const char *component)
390
{
425
{
391
    fat_node_t *parentp = (fat_node_t *)prnt;
426
    fat_node_t *parentp = (fat_node_t *)prnt;
392
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
427
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
393
    unsigned i, j;
428
    unsigned i, j;
394
    unsigned bps;       /* bytes per sector */
429
    unsigned bps;       /* bytes per sector */
395
    unsigned dps;       /* dentries per sector */
430
    unsigned dps;       /* dentries per sector */
396
    unsigned blocks;
431
    unsigned blocks;
397
    fat_dentry_t *d;
432
    fat_dentry_t *d;
398
    block_t *b;
433
    block_t *b;
399
 
434
 
400
    futex_down(&parentp->idx->lock);
435
    futex_down(&parentp->idx->lock);
401
    bps = fat_bps_get(parentp->idx->dev_handle);
436
    bps = fat_bps_get(parentp->idx->dev_handle);
402
    dps = bps / sizeof(fat_dentry_t);
437
    dps = bps / sizeof(fat_dentry_t);
403
    blocks = parentp->size / bps + (parentp->size % bps != 0);
438
    blocks = parentp->size / bps + (parentp->size % bps != 0);
404
    for (i = 0; i < blocks; i++) {
439
    for (i = 0; i < blocks; i++) {
405
        unsigned dentries;
440
        unsigned dentries;
406
       
441
       
407
        b = fat_block_get(parentp, i);
442
        b = fat_block_get(parentp, i);
408
        dentries = (i == blocks - 1) ?
443
        dentries = (i == blocks - 1) ?
409
            parentp->size % sizeof(fat_dentry_t) :
444
            parentp->size % sizeof(fat_dentry_t) :
410
            dps;
445
            dps;
411
        for (j = 0; j < dentries; j++) {
446
        for (j = 0; j < dentries; j++) {
412
            d = ((fat_dentry_t *)b->data) + j;
447
            d = ((fat_dentry_t *)b->data) + j;
413
            switch (fat_classify_dentry(d)) {
448
            switch (fat_classify_dentry(d)) {
414
            case FAT_DENTRY_SKIP:
449
            case FAT_DENTRY_SKIP:
415
                continue;
450
                continue;
416
            case FAT_DENTRY_LAST:
451
            case FAT_DENTRY_LAST:
417
                block_put(b);
452
                block_put(b);
418
                futex_up(&parentp->idx->lock);
453
                futex_up(&parentp->idx->lock);
419
                return NULL;
454
                return NULL;
420
            default:
455
            default:
421
            case FAT_DENTRY_VALID:
456
            case FAT_DENTRY_VALID:
422
                dentry_name_canonify(d, name);
457
                dentry_name_canonify(d, name);
423
                break;
458
                break;
424
            }
459
            }
425
            if (strcmp(name, component) == 0) {
460
            if (strcmp(name, component) == 0) {
426
                /* hit */
461
                /* hit */
427
                void *node;
462
                void *node;
428
                /*
463
                /*
429
                 * Assume tree hierarchy for locking.  We
464
                 * Assume tree hierarchy for locking.  We
430
                 * already have the parent and now we are going
465
                 * already have the parent and now we are going
431
                 * to lock the child.  Never lock in the oposite
466
                 * to lock the child.  Never lock in the oposite
432
                 * order.
467
                 * order.
433
                 */
468
                 */
434
                fat_idx_t *idx = fat_idx_get_by_pos(
469
                fat_idx_t *idx = fat_idx_get_by_pos(
435
                    parentp->idx->dev_handle, parentp->firstc,
470
                    parentp->idx->dev_handle, parentp->firstc,
436
                    i * dps + j);
471
                    i * dps + j);
437
                futex_up(&parentp->idx->lock);
472
                futex_up(&parentp->idx->lock);
438
                if (!idx) {
473
                if (!idx) {
439
                    /*
474
                    /*
440
                     * Can happen if memory is low or if we
475
                     * Can happen if memory is low or if we
441
                     * run out of 32-bit indices.
476
                     * run out of 32-bit indices.
442
                     */
477
                     */
443
                    block_put(b);
478
                    block_put(b);
444
                    return NULL;
479
                    return NULL;
445
                }
480
                }
446
                node = fat_node_get_core(idx);
481
                node = fat_node_get_core(idx);
447
                futex_up(&idx->lock);
482
                futex_up(&idx->lock);
448
                block_put(b);
483
                block_put(b);
449
                return node;
484
                return node;
450
            }
485
            }
451
        }
486
        }
452
        block_put(b);
487
        block_put(b);
453
    }
488
    }
454
    futex_up(&parentp->idx->lock);
489
    futex_up(&parentp->idx->lock);
455
    return NULL;
490
    return NULL;
456
}
491
}
457
 
492
 
458
static fs_index_t fat_index_get(void *node)
493
static fs_index_t fat_index_get(void *node)
459
{
494
{
460
    fat_node_t *fnodep = (fat_node_t *)node;
495
    fat_node_t *fnodep = (fat_node_t *)node;
461
    if (!fnodep)
496
    if (!fnodep)
462
        return 0;
497
        return 0;
463
    return fnodep->idx->index;
498
    return fnodep->idx->index;
464
}
499
}
465
 
500
 
466
static size_t fat_size_get(void *node)
501
static size_t fat_size_get(void *node)
467
{
502
{
468
    return ((fat_node_t *)node)->size;
503
    return ((fat_node_t *)node)->size;
469
}
504
}
470
 
505
 
471
static unsigned fat_lnkcnt_get(void *node)
506
static unsigned fat_lnkcnt_get(void *node)
472
{
507
{
473
    return ((fat_node_t *)node)->lnkcnt;
508
    return ((fat_node_t *)node)->lnkcnt;
474
}
509
}
475
 
510
 
476
static bool fat_has_children(void *node)
511
static bool fat_has_children(void *node)
477
{
512
{
478
    fat_node_t *nodep = (fat_node_t *)node;
513
    fat_node_t *nodep = (fat_node_t *)node;
479
    unsigned bps;
514
    unsigned bps;
480
    unsigned dps;
515
    unsigned dps;
481
    unsigned blocks;
516
    unsigned blocks;
482
    block_t *b;
517
    block_t *b;
483
    unsigned i, j;
518
    unsigned i, j;
484
 
519
 
485
    if (nodep->type != FAT_DIRECTORY)
520
    if (nodep->type != FAT_DIRECTORY)
486
        return false;
521
        return false;
487
 
522
 
488
    futex_down(&nodep->idx->lock);
523
    futex_down(&nodep->idx->lock);
489
    bps = fat_bps_get(nodep->idx->dev_handle);
524
    bps = fat_bps_get(nodep->idx->dev_handle);
490
    dps = bps / sizeof(fat_dentry_t);
525
    dps = bps / sizeof(fat_dentry_t);
491
 
526
 
492
    blocks = nodep->size / bps + (nodep->size % bps != 0);
527
    blocks = nodep->size / bps + (nodep->size % bps != 0);
493
 
528
 
494
    for (i = 0; i < blocks; i++) {
529
    for (i = 0; i < blocks; i++) {
495
        unsigned dentries;
530
        unsigned dentries;
496
        fat_dentry_t *d;
531
        fat_dentry_t *d;
497
   
532
   
498
        b = fat_block_get(nodep, i);
533
        b = fat_block_get(nodep, i);
499
        dentries = (i == blocks - 1) ?
534
        dentries = (i == blocks - 1) ?
500
            nodep->size % sizeof(fat_dentry_t) :
535
            nodep->size % sizeof(fat_dentry_t) :
501
            dps;
536
            dps;
502
        for (j = 0; j < dentries; j++) {
537
        for (j = 0; j < dentries; j++) {
503
            d = ((fat_dentry_t *)b->data) + j;
538
            d = ((fat_dentry_t *)b->data) + j;
504
            switch (fat_classify_dentry(d)) {
539
            switch (fat_classify_dentry(d)) {
505
            case FAT_DENTRY_SKIP:
540
            case FAT_DENTRY_SKIP:
506
                continue;
541
                continue;
507
            case FAT_DENTRY_LAST:
542
            case FAT_DENTRY_LAST:
508
                block_put(b);
543
                block_put(b);
509
                futex_up(&nodep->idx->lock);
544
                futex_up(&nodep->idx->lock);
510
                return false;
545
                return false;
511
            default:
546
            default:
512
            case FAT_DENTRY_VALID:
547
            case FAT_DENTRY_VALID:
513
                block_put(b);
548
                block_put(b);
514
                futex_up(&nodep->idx->lock);
549
                futex_up(&nodep->idx->lock);
515
                return true;
550
                return true;
516
            }
551
            }
517
            block_put(b);
552
            block_put(b);
518
            futex_up(&nodep->idx->lock);
553
            futex_up(&nodep->idx->lock);
519
            return true;
554
            return true;
520
        }
555
        }
521
        block_put(b);
556
        block_put(b);
522
    }
557
    }
523
 
558
 
524
    futex_up(&nodep->idx->lock);
559
    futex_up(&nodep->idx->lock);
525
    return false;
560
    return false;
526
}
561
}
527
 
562
 
528
static void *fat_root_get(dev_handle_t dev_handle)
563
static void *fat_root_get(dev_handle_t dev_handle)
529
{
564
{
530
    return fat_node_get(dev_handle, 0);
565
    return fat_node_get(dev_handle, 0);
531
}
566
}
532
 
567
 
533
static char fat_plb_get_char(unsigned pos)
568
static char fat_plb_get_char(unsigned pos)
534
{
569
{
535
    return fat_reg.plb_ro[pos % PLB_SIZE];
570
    return fat_reg.plb_ro[pos % PLB_SIZE];
536
}
571
}
537
 
572
 
538
static bool fat_is_directory(void *node)
573
static bool fat_is_directory(void *node)
539
{
574
{
540
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
575
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
541
}
576
}
542
 
577
 
543
static bool fat_is_file(void *node)
578
static bool fat_is_file(void *node)
544
{
579
{
545
    return ((fat_node_t *)node)->type == FAT_FILE;
580
    return ((fat_node_t *)node)->type == FAT_FILE;
546
}
581
}
547
 
582
 
548
/** libfs operations */
583
/** libfs operations */
549
libfs_ops_t fat_libfs_ops = {
584
libfs_ops_t fat_libfs_ops = {
550
    .match = fat_match,
585
    .match = fat_match,
551
    .node_get = fat_node_get,
586
    .node_get = fat_node_get,
552
    .node_put = fat_node_put,
587
    .node_put = fat_node_put,
553
    .create = fat_create,
588
    .create = fat_create,
554
    .destroy = fat_destroy,
589
    .destroy = fat_destroy,
555
    .link = fat_link,
590
    .link = fat_link,
556
    .unlink = fat_unlink,
591
    .unlink = fat_unlink,
557
    .index_get = fat_index_get,
592
    .index_get = fat_index_get,
558
    .size_get = fat_size_get,
593
    .size_get = fat_size_get,
559
    .lnkcnt_get = fat_lnkcnt_get,
594
    .lnkcnt_get = fat_lnkcnt_get,
560
    .has_children = fat_has_children,
595
    .has_children = fat_has_children,
561
    .root_get = fat_root_get,
596
    .root_get = fat_root_get,
562
    .plb_get_char = fat_plb_get_char,
597
    .plb_get_char = fat_plb_get_char,
563
    .is_directory = fat_is_directory,
598
    .is_directory = fat_is_directory,
564
    .is_file = fat_is_file
599
    .is_file = fat_is_file
565
};
600
};
566
 
601
 
567
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
602
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
568
{
603
{
569
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
604
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
570
    block_t *bb;
605
    block_t *bb;
-
 
606
    uint16_t bps;
571
    uint16_t rde;
607
    uint16_t rde;
572
    int rc;
608
    int rc;
573
 
609
 
-
 
610
    /*
-
 
611
     * For now, we don't bother to remember dev_handle, dev_phone or
-
 
612
     * dev_buffer in some data structure. We use global variables because we
-
 
613
     * know there will be at most one mount on this file system.
-
 
614
     * Of course, this is a huge TODO item.
-
 
615
     */
-
 
616
    dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
-
 
617
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-
 
618
   
-
 
619
    if (!dev_buffer) {
-
 
620
        ipc_answer_0(rid, ENOMEM);
-
 
621
        return;
-
 
622
    }
-
 
623
 
-
 
624
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
-
 
625
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
-
 
626
 
-
 
627
    if (dev_phone < 0) {
-
 
628
        munmap(dev_buffer, BS_SIZE);
-
 
629
        ipc_answer_0(rid, dev_phone);
-
 
630
        return;
-
 
631
    }
-
 
632
 
-
 
633
    rc = ipc_share_out_start(dev_phone, dev_buffer,
-
 
634
        AS_AREA_READ | AS_AREA_WRITE);
-
 
635
    if (rc != EOK) {
-
 
636
            munmap(dev_buffer, BS_SIZE);
-
 
637
        ipc_answer_0(rid, rc);
-
 
638
        return;
-
 
639
    }
-
 
640
 
574
    /* Read the number of root directory entries. */
641
    /* Read the number of root directory entries. */
575
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
642
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
643
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
576
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
644
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
577
    block_put(bb);
645
    block_put(bb);
578
 
646
 
-
 
647
    if (bps != BS_SIZE) {
-
 
648
        munmap(dev_buffer, BS_SIZE);
-
 
649
        ipc_answer_0(rid, ENOTSUP);
-
 
650
        return;
-
 
651
    }
-
 
652
 
579
    rc = fat_idx_init_by_dev_handle(dev_handle);
653
    rc = fat_idx_init_by_dev_handle(dev_handle);
580
    if (rc != EOK) {
654
    if (rc != EOK) {
-
 
655
            munmap(dev_buffer, BS_SIZE);
581
        ipc_answer_0(rid, rc);
656
        ipc_answer_0(rid, rc);
582
        return;
657
        return;
583
    }
658
    }
584
 
659
 
585
    /* Initialize the root node. */
660
    /* Initialize the root node. */
586
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
661
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
587
    if (!rootp) {
662
    if (!rootp) {
-
 
663
            munmap(dev_buffer, BS_SIZE);
588
        fat_idx_fini_by_dev_handle(dev_handle);
664
        fat_idx_fini_by_dev_handle(dev_handle);
589
        ipc_answer_0(rid, ENOMEM);
665
        ipc_answer_0(rid, ENOMEM);
590
        return;
666
        return;
591
    }
667
    }
592
    fat_node_initialize(rootp);
668
    fat_node_initialize(rootp);
593
 
669
 
594
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
670
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
595
    if (!ridxp) {
671
    if (!ridxp) {
-
 
672
            munmap(dev_buffer, BS_SIZE);
596
        free(rootp);
673
        free(rootp);
597
        fat_idx_fini_by_dev_handle(dev_handle);
674
        fat_idx_fini_by_dev_handle(dev_handle);
598
        ipc_answer_0(rid, ENOMEM);
675
        ipc_answer_0(rid, ENOMEM);
599
        return;
676
        return;
600
    }
677
    }
601
    assert(ridxp->index == 0);
678
    assert(ridxp->index == 0);
602
    /* ridxp->lock held */
679
    /* ridxp->lock held */
603
 
680
 
604
    rootp->type = FAT_DIRECTORY;
681
    rootp->type = FAT_DIRECTORY;
605
    rootp->firstc = FAT_CLST_ROOT;
682
    rootp->firstc = FAT_CLST_ROOT;
606
    rootp->refcnt = 1;
683
    rootp->refcnt = 1;
607
    rootp->size = rde * sizeof(fat_dentry_t);
684
    rootp->size = rde * sizeof(fat_dentry_t);
608
    rootp->idx = ridxp;
685
    rootp->idx = ridxp;
609
    ridxp->nodep = rootp;
686
    ridxp->nodep = rootp;
610
   
687
   
611
    futex_up(&ridxp->lock);
688
    futex_up(&ridxp->lock);
612
 
689
 
613
    ipc_answer_0(rid, EOK);
690
    ipc_answer_0(rid, EOK);
614
}
691
}
615
 
692
 
616
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
693
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
617
{
694
{
618
    ipc_answer_0(rid, ENOTSUP);
695
    ipc_answer_0(rid, ENOTSUP);
619
}
696
}
620
 
697
 
621
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
698
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
622
{
699
{
623
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
700
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
624
}
701
}
625
 
702
 
626
/**
703
/**
627
 * @}
704
 * @}
628
 */
705
 */
629
 
706