Subversion Repositories HelenOS

Rev

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

Rev 2890 Rev 2891
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 <async.h>
42
#include <async.h>
43
#include <errno.h>
43
#include <errno.h>
44
#include <string.h>
44
#include <string.h>
45
#include <byteorder.h>
45
#include <byteorder.h>
46
#include <libadt/hash_table.h>
46
#include <libadt/hash_table.h>
47
#include <libadt/list.h>
47
#include <libadt/list.h>
48
#include <assert.h>
48
#include <assert.h>
49
#include <futex.h>
49
#include <futex.h>
50
 
50
 
51
#define BS_BLOCK        0
51
#define BS_BLOCK        0
52
 
52
 
53
#define FIN_KEY_DEV_HANDLE  0
53
#define FIN_KEY_DEV_HANDLE  0
54
#define FIN_KEY_INDEX       1
54
#define FIN_KEY_INDEX       1
55
 
55
 
56
/** Hash table of FAT in-core nodes. */
56
/** Hash table of FAT in-core nodes. */
57
hash_table_t fin_hash;
57
hash_table_t fin_hash;
58
 
58
 
59
/** List of free FAT in-core nodes. */
59
/** List of free FAT in-core nodes. */
60
link_t ffn_head;
60
link_t ffn_head;
61
 
61
 
62
#define FAT_NAME_LEN        8
62
#define FAT_NAME_LEN        8
63
#define FAT_EXT_LEN     3
63
#define FAT_EXT_LEN     3
64
 
64
 
65
#define FAT_PAD         ' ' 
65
#define FAT_PAD         ' ' 
66
 
66
 
67
#define FAT_DENTRY_UNUSED   0x00
67
#define FAT_DENTRY_UNUSED   0x00
68
#define FAT_DENTRY_E5_ESC   0x05
68
#define FAT_DENTRY_E5_ESC   0x05
69
#define FAT_DENTRY_DOT      0x2e
69
#define FAT_DENTRY_DOT      0x2e
70
#define FAT_DENTRY_ERASED   0xe5
70
#define FAT_DENTRY_ERASED   0xe5
71
 
71
 
72
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
72
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
73
{
73
{
74
    int i;
74
    int i;
75
 
75
 
76
    for (i = 0; i < FAT_NAME_LEN; i++) {
76
    for (i = 0; i < FAT_NAME_LEN; i++) {
77
        if (d->name[i] == FAT_PAD) {
77
        if (d->name[i] == FAT_PAD) {
78
            buf++;
78
            buf++;
79
            break;
79
            break;
80
        }
80
        }
81
        if (d->name[i] == FAT_DENTRY_E5_ESC)
81
        if (d->name[i] == FAT_DENTRY_E5_ESC)
82
            *buf++ = 0xe5;
82
            *buf++ = 0xe5;
83
        else
83
        else
84
            *buf++ = d->name[i];
84
            *buf++ = d->name[i];
85
    }
85
    }
86
    if (d->ext[0] != FAT_PAD)
86
    if (d->ext[0] != FAT_PAD)
87
        *buf++ = '.';
87
        *buf++ = '.';
88
    for (i = 0; i < FAT_EXT_LEN; i++) {
88
    for (i = 0; i < FAT_EXT_LEN; i++) {
89
        if (d->ext[i] == FAT_PAD) {
89
        if (d->ext[i] == FAT_PAD) {
90
            *buf = '\0';
90
            *buf = '\0';
91
            return;
91
            return;
92
        }
92
        }
93
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
93
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
94
            *buf++ = 0xe5;
94
            *buf++ = 0xe5;
95
        else
95
        else
96
            *buf++ = d->ext[i];
96
            *buf++ = d->ext[i];
97
    }
97
    }
98
}
98
}
99
 
99
 
100
/* TODO and also move somewhere else */
100
/* TODO and also move somewhere else */
101
typedef struct {
101
typedef struct {
102
    void *data;
102
    void *data;
103
} block_t;
103
} block_t;
104
 
104
 
105
static block_t *block_get(dev_handle_t dev_handle, off_t offset)
105
static block_t *block_get(dev_handle_t dev_handle, off_t offset)
106
{
106
{
107
    return NULL;    /* TODO */
107
    return NULL;    /* TODO */
108
}
108
}
109
 
109
 
110
static void block_put(block_t *block)
110
static void block_put(block_t *block)
111
{
111
{
112
    /* TODO */
112
    /* TODO */
113
}
113
}
114
 
114
 
115
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
115
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
116
 
116
 
117
#define FAT_CLST_RES0   0x0000
117
#define FAT_CLST_RES0   0x0000
118
#define FAT_CLST_RES1   0x0001  /* internally used to mark root directory */
118
#define FAT_CLST_RES1   0x0001  /* internally used to mark root directory */
119
#define FAT_CLST_FIRST  0x0002
119
#define FAT_CLST_FIRST  0x0002
120
#define FAT_CLST_BAD    0xfff7
120
#define FAT_CLST_BAD    0xfff7
121
#define FAT_CLST_LAST1  0xfff8
121
#define FAT_CLST_LAST1  0xfff8
122
#define FAT_CLST_LAST8  0xffff
122
#define FAT_CLST_LAST8  0xffff
123
 
123
 
-
 
124
#define fat_block_get(np, off) \
-
 
125
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
-
 
126
 
-
 
127
static block_t *
124
static block_t *fat_block_get(fat_node_t *nodep, off_t offset)
128
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t fc, off_t offset)
125
{
129
{
126
    block_t *bb;
130
    block_t *bb;
127
    block_t *b;
131
    block_t *b;
128
    unsigned bps;
132
    unsigned bps;
129
    unsigned spc;
133
    unsigned spc;
130
    unsigned rscnt;     /* block address of the first FAT */
134
    unsigned rscnt;     /* block address of the first FAT */
131
    unsigned fatcnt;
135
    unsigned fatcnt;
132
    unsigned rde;
136
    unsigned rde;
133
    unsigned rds;       /* root directory size */
137
    unsigned rds;       /* root directory size */
134
    unsigned sf;
138
    unsigned sf;
135
    unsigned ssa;       /* size of the system area */
139
    unsigned ssa;       /* size of the system area */
136
    unsigned clusters;
140
    unsigned clusters;
137
    fat_cluster_t clst = nodep->firstc;
141
    fat_cluster_t clst = fc;
138
    unsigned i;
142
    unsigned i;
139
 
143
 
140
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK);
144
    bb = block_get(dev_handle, BS_BLOCK);
141
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
145
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
142
    spc = FAT_BS(bb)->spc;
146
    spc = FAT_BS(bb)->spc;
143
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
147
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
144
    fatcnt = FAT_BS(bb)->fatcnt;
148
    fatcnt = FAT_BS(bb)->fatcnt;
145
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
149
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
146
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
150
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
147
    block_put(bb);
151
    block_put(bb);
148
 
152
 
149
    rds = (sizeof(fat_dentry_t) * rde) / bps;
153
    rds = (sizeof(fat_dentry_t) * rde) / bps;
150
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
154
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
151
    ssa = rscnt + fatcnt * sf + rds;
155
    ssa = rscnt + fatcnt * sf + rds;
152
 
156
 
153
    if (nodep->idx->index == FAT_CLST_RES1) {
157
    if (fc == FAT_CLST_RES1) {
154
        /* root directory special case */
158
        /* root directory special case */
155
        assert(offset < rds);
159
        assert(offset < rds);
156
        b = block_get(nodep->idx->dev_handle,
-
 
157
            rscnt + fatcnt * sf + offset);
160
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset);
158
        return b;
161
        return b;
159
    }
162
    }
160
 
163
 
161
    clusters = offset / spc;
164
    clusters = offset / spc;
162
    for (i = 0; i < clusters; i++) {
165
    for (i = 0; i < clusters; i++) {
163
        unsigned fsec;  /* sector offset relative to FAT1 */
166
        unsigned fsec;  /* sector offset relative to FAT1 */
164
        unsigned fidx;  /* FAT1 entry index */
167
        unsigned fidx;  /* FAT1 entry index */
165
 
168
 
166
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
169
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
167
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
170
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
168
        fidx = clst % (bps / sizeof(fat_cluster_t));
171
        fidx = clst % (bps / sizeof(fat_cluster_t));
169
        /* read FAT1 */
172
        /* read FAT1 */
170
        b = block_get(nodep->idx->dev_handle, rscnt + fsec);
173
        b = block_get(dev_handle, rscnt + fsec);
171
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
174
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
172
        assert(clst != FAT_CLST_BAD);
175
        assert(clst != FAT_CLST_BAD);
173
        assert(clst < FAT_CLST_LAST1);
176
        assert(clst < FAT_CLST_LAST1);
174
        block_put(b);
177
        block_put(b);
175
    }
178
    }
176
 
179
 
177
    b = block_get(nodep->idx->dev_handle, ssa +
180
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
178
        (clst - FAT_CLST_FIRST) * spc + offset % spc);
181
        offset % spc);
179
 
182
 
180
    return b;
183
    return b;
181
}
184
}
182
 
185
 
183
static void fat_node_initialize(fat_node_t *node)
186
static void fat_node_initialize(fat_node_t *node)
184
{
187
{
185
    node->idx = NULL;
188
    node->idx = NULL;
186
    node->type = 0;
189
    node->type = 0;
187
    link_initialize(&node->fin_link);
190
    link_initialize(&node->fin_link);
188
    link_initialize(&node->ffn_link);
191
    link_initialize(&node->ffn_link);
189
    node->size = 0;
192
    node->size = 0;
190
    node->lnkcnt = 0;
193
    node->lnkcnt = 0;
191
    node->refcnt = 0;
194
    node->refcnt = 0;
192
    node->dirty = false;
195
    node->dirty = false;
193
}
196
}
194
 
197
 
195
static uint16_t fat_bps_get(dev_handle_t dev_handle)
198
static uint16_t fat_bps_get(dev_handle_t dev_handle)
196
{
199
{
197
    block_t *bb;
200
    block_t *bb;
198
    uint16_t bps;
201
    uint16_t bps;
199
   
202
   
200
    bb = block_get(dev_handle, BS_BLOCK);
203
    bb = block_get(dev_handle, BS_BLOCK);
201
    assert(bb != NULL);
204
    assert(bb != NULL);
202
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
205
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
203
    block_put(bb);
206
    block_put(bb);
204
 
207
 
205
    return bps;
208
    return bps;
206
}
209
}
207
 
210
 
208
typedef enum {
211
typedef enum {
209
    FAT_DENTRY_SKIP,
212
    FAT_DENTRY_SKIP,
210
    FAT_DENTRY_LAST,
213
    FAT_DENTRY_LAST,
211
    FAT_DENTRY_VALID
214
    FAT_DENTRY_VALID
212
} fat_dentry_clsf_t;
215
} fat_dentry_clsf_t;
213
 
216
 
214
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
217
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
215
{
218
{
216
    if (d->attr & FAT_ATTR_VOLLABEL) {
219
    if (d->attr & FAT_ATTR_VOLLABEL) {
217
        /* volume label entry */
220
        /* volume label entry */
218
        return FAT_DENTRY_SKIP;
221
        return FAT_DENTRY_SKIP;
219
    }
222
    }
220
    if (d->name[0] == FAT_DENTRY_ERASED) {
223
    if (d->name[0] == FAT_DENTRY_ERASED) {
221
        /* not-currently-used entry */
224
        /* not-currently-used entry */
222
        return FAT_DENTRY_SKIP;
225
        return FAT_DENTRY_SKIP;
223
    }
226
    }
224
    if (d->name[0] == FAT_DENTRY_UNUSED) {
227
    if (d->name[0] == FAT_DENTRY_UNUSED) {
225
        /* never used entry */
228
        /* never used entry */
226
        return FAT_DENTRY_LAST;
229
        return FAT_DENTRY_LAST;
227
    }
230
    }
228
    if (d->name[0] == FAT_DENTRY_DOT) {
231
    if (d->name[0] == FAT_DENTRY_DOT) {
229
        /*
232
        /*
230
         * Most likely '.' or '..'.
233
         * Most likely '.' or '..'.
231
         * It cannot occur in a regular file name.
234
         * It cannot occur in a regular file name.
232
         */
235
         */
233
        return FAT_DENTRY_SKIP;
236
        return FAT_DENTRY_SKIP;
234
    }
237
    }
235
    return FAT_DENTRY_VALID;
238
    return FAT_DENTRY_VALID;
236
}
239
}
237
 
240
 
238
static void fat_sync_node(fat_node_t *node)
241
static void fat_sync_node(fat_node_t *node)
239
{
242
{
240
    /* TODO */
243
    /* TODO */
241
}
244
}
242
 
245
 
243
/** Instantiate a FAT in-core node. */
246
/** Instantiate a FAT in-core node. */
244
static void *
-
 
245
fat_node_get(dev_handle_t dev_handle, fs_index_t index)
247
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
246
{
248
{
-
 
249
    fat_idx_t *idx;
-
 
250
    block_t *b;
-
 
251
    fat_dentry_t *d;
-
 
252
    fat_node_t *nodep;
-
 
253
    unsigned bps;
-
 
254
    unsigned dps;
-
 
255
 
-
 
256
    idx = fat_idx_get_by_index(dev_handle, index);
-
 
257
    if (!idx)
247
    return NULL;    /* TODO */
258
        return NULL;
-
 
259
 
-
 
260
    if (idx->nodep) {
-
 
261
        /*
-
 
262
         * We are lucky.
-
 
263
         * The node is already instantiated in memory.
-
 
264
         */
-
 
265
        idx->nodep->refcnt++;
-
 
266
        return idx->nodep;
-
 
267
    }
-
 
268
 
-
 
269
    /*
-
 
270
     * We must instantiate the node from the file system.
-
 
271
     */
-
 
272
   
-
 
273
    assert(idx->pfc);
-
 
274
 
-
 
275
    nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
-
 
276
    if (!nodep)
-
 
277
        return NULL;
-
 
278
    fat_node_initialize(nodep);
-
 
279
 
-
 
280
    bps = fat_bps_get(dev_handle);
-
 
281
    dps = bps / sizeof(fat_dentry_t);
-
 
282
 
-
 
283
    b = _fat_block_get(dev_handle, idx->pfc,
-
 
284
        (idx->pdi * sizeof(fat_dentry_t)) / bps);
-
 
285
 
-
 
286
    assert(b);
-
 
287
 
-
 
288
    d = ((fat_dentry_t *)b->data) + (idx->pdi % dps);
-
 
289
    /* XXX */
248
}
290
}
249
 
291
 
250
static void fat_node_put(void *node)
292
static void fat_node_put(void *node)
251
{
293
{
252
    /* TODO */
294
    /* TODO */
253
}
295
}
254
 
296
 
255
static void *fat_create(int flags)
297
static void *fat_create(int flags)
256
{
298
{
257
    return NULL;    /* not supported at the moment */
299
    return NULL;    /* not supported at the moment */
258
}
300
}
259
 
301
 
260
static int fat_destroy(void *node)
302
static int fat_destroy(void *node)
261
{
303
{
262
    return ENOTSUP; /* not supported at the moment */
304
    return ENOTSUP; /* not supported at the moment */
263
}
305
}
264
 
306
 
265
static bool fat_link(void *prnt, void *chld, const char *name)
307
static bool fat_link(void *prnt, void *chld, const char *name)
266
{
308
{
267
    return false;   /* not supported at the moment */
309
    return false;   /* not supported at the moment */
268
}
310
}
269
 
311
 
270
static int fat_unlink(void *prnt, void *chld)
312
static int fat_unlink(void *prnt, void *chld)
271
{
313
{
272
    return ENOTSUP; /* not supported at the moment */
314
    return ENOTSUP; /* not supported at the moment */
273
}
315
}
274
 
316
 
275
static void *fat_match(void *prnt, const char *component)
317
static void *fat_match(void *prnt, const char *component)
276
{
318
{
277
    fat_node_t *parentp = (fat_node_t *)prnt;
319
    fat_node_t *parentp = (fat_node_t *)prnt;
278
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
320
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
279
    unsigned i, j;
321
    unsigned i, j;
280
    unsigned bps;       /* bytes per sector */
322
    unsigned bps;       /* bytes per sector */
281
    unsigned dps;       /* dentries per sector */
323
    unsigned dps;       /* dentries per sector */
282
    unsigned blocks;
324
    unsigned blocks;
283
    fat_dentry_t *d;
325
    fat_dentry_t *d;
284
    block_t *b;
326
    block_t *b;
285
 
327
 
286
    bps = fat_bps_get(parentp->idx->dev_handle);
328
    bps = fat_bps_get(parentp->idx->dev_handle);
287
    dps = bps / sizeof(fat_dentry_t);
329
    dps = bps / sizeof(fat_dentry_t);
288
    blocks = parentp->size / bps + (parentp->size % bps != 0);
330
    blocks = parentp->size / bps + (parentp->size % bps != 0);
289
    for (i = 0; i < blocks; i++) {
331
    for (i = 0; i < blocks; i++) {
290
        unsigned dentries;
332
        unsigned dentries;
291
       
333
       
292
        b = fat_block_get(parentp, i);
334
        b = fat_block_get(parentp, i);
293
        dentries = (i == blocks - 1) ?
335
        dentries = (i == blocks - 1) ?
294
            parentp->size % sizeof(fat_dentry_t) :
336
            parentp->size % sizeof(fat_dentry_t) :
295
            dps;
337
            dps;
296
        for (j = 0; j < dentries; j++) {
338
        for (j = 0; j < dentries; j++) {
297
            d = ((fat_dentry_t *)b->data) + j;
339
            d = ((fat_dentry_t *)b->data) + j;
298
            switch (fat_classify_dentry(d)) {
340
            switch (fat_classify_dentry(d)) {
299
            case FAT_DENTRY_SKIP:
341
            case FAT_DENTRY_SKIP:
300
                continue;
342
                continue;
301
            case FAT_DENTRY_LAST:
343
            case FAT_DENTRY_LAST:
302
                block_put(b);
344
                block_put(b);
303
                return NULL;
345
                return NULL;
304
            default:
346
            default:
305
            case FAT_DENTRY_VALID:
347
            case FAT_DENTRY_VALID:
306
                dentry_name_canonify(d, name);
348
                dentry_name_canonify(d, name);
307
                break;
349
                break;
308
            }
350
            }
309
            if (strcmp(name, component) == 0) {
351
            if (strcmp(name, component) == 0) {
310
                /* hit */
352
                /* hit */
311
                fat_idx_t *idx = fat_idx_get_by_pos(
353
                fat_idx_t *idx = fat_idx_get_by_pos(
312
                    parentp->idx->dev_handle, parentp->firstc,
354
                    parentp->idx->dev_handle, parentp->firstc,
313
                    i * dps + j);
355
                    i * dps + j);
314
                if (!idx) {
356
                if (!idx) {
315
                    /*
357
                    /*
316
                     * Can happen if memory is low or if we
358
                     * Can happen if memory is low or if we
317
                     * run out of 32-bit indices.
359
                     * run out of 32-bit indices.
318
                     */
360
                     */
319
                    block_put(b);
361
                    block_put(b);
320
                    return NULL;
362
                    return NULL;
321
                }
363
                }
322
                void *node = fat_node_get(idx->dev_handle,
364
                void *node = fat_node_get(idx->dev_handle,
323
                    idx->index);
365
                    idx->index);
324
                block_put(b);
366
                block_put(b);
325
                return node;
367
                return node;
326
            }
368
            }
327
        }
369
        }
328
        block_put(b);
370
        block_put(b);
329
    }
371
    }
330
 
372
 
331
    return NULL;
373
    return NULL;
332
}
374
}
333
 
375
 
334
static fs_index_t fat_index_get(void *node)
376
static fs_index_t fat_index_get(void *node)
335
{
377
{
336
    fat_node_t *fnodep = (fat_node_t *)node;
378
    fat_node_t *fnodep = (fat_node_t *)node;
337
    if (!fnodep)
379
    if (!fnodep)
338
        return 0;
380
        return 0;
339
    return fnodep->idx->index;
381
    return fnodep->idx->index;
340
}
382
}
341
 
383
 
342
static size_t fat_size_get(void *node)
384
static size_t fat_size_get(void *node)
343
{
385
{
344
    return ((fat_node_t *)node)->size;
386
    return ((fat_node_t *)node)->size;
345
}
387
}
346
 
388
 
347
static unsigned fat_lnkcnt_get(void *node)
389
static unsigned fat_lnkcnt_get(void *node)
348
{
390
{
349
    return ((fat_node_t *)node)->lnkcnt;
391
    return ((fat_node_t *)node)->lnkcnt;
350
}
392
}
351
 
393
 
352
static bool fat_has_children(void *node)
394
static bool fat_has_children(void *node)
353
{
395
{
354
    fat_node_t *nodep = (fat_node_t *)node;
396
    fat_node_t *nodep = (fat_node_t *)node;
355
    unsigned bps;
397
    unsigned bps;
356
    unsigned dps;
398
    unsigned dps;
357
    unsigned blocks;
399
    unsigned blocks;
358
    block_t *b;
400
    block_t *b;
359
    unsigned i, j;
401
    unsigned i, j;
360
 
402
 
361
    if (nodep->type != FAT_DIRECTORY)
403
    if (nodep->type != FAT_DIRECTORY)
362
        return false;
404
        return false;
363
 
405
 
364
    bps = fat_bps_get(nodep->idx->dev_handle);
406
    bps = fat_bps_get(nodep->idx->dev_handle);
365
    dps = bps / sizeof(fat_dentry_t);
407
    dps = bps / sizeof(fat_dentry_t);
366
 
408
 
367
    blocks = nodep->size / bps + (nodep->size % bps != 0);
409
    blocks = nodep->size / bps + (nodep->size % bps != 0);
368
 
410
 
369
    for (i = 0; i < blocks; i++) {
411
    for (i = 0; i < blocks; i++) {
370
        unsigned dentries;
412
        unsigned dentries;
371
        fat_dentry_t *d;
413
        fat_dentry_t *d;
372
   
414
   
373
        b = fat_block_get(nodep, i);
415
        b = fat_block_get(nodep, i);
374
        dentries = (i == blocks - 1) ?
416
        dentries = (i == blocks - 1) ?
375
            nodep->size % sizeof(fat_dentry_t) :
417
            nodep->size % sizeof(fat_dentry_t) :
376
            dps;
418
            dps;
377
        for (j = 0; j < dentries; j++) {
419
        for (j = 0; j < dentries; j++) {
378
            d = ((fat_dentry_t *)b->data) + j;
420
            d = ((fat_dentry_t *)b->data) + j;
379
            switch (fat_classify_dentry(d)) {
421
            switch (fat_classify_dentry(d)) {
380
            case FAT_DENTRY_SKIP:
422
            case FAT_DENTRY_SKIP:
381
                continue;
423
                continue;
382
            case FAT_DENTRY_LAST:
424
            case FAT_DENTRY_LAST:
383
                block_put(b);
425
                block_put(b);
384
                return false;
426
                return false;
385
            default:
427
            default:
386
            case FAT_DENTRY_VALID:
428
            case FAT_DENTRY_VALID:
387
                block_put(b);
429
                block_put(b);
388
                return true;
430
                return true;
389
            }
431
            }
390
            block_put(b);
432
            block_put(b);
391
            return true;
433
            return true;
392
        }
434
        }
393
        block_put(b);
435
        block_put(b);
394
    }
436
    }
395
 
437
 
396
    return false;
438
    return false;
397
}
439
}
398
 
440
 
399
static void *fat_root_get(dev_handle_t dev_handle)
441
static void *fat_root_get(dev_handle_t dev_handle)
400
{
442
{
401
    return fat_node_get(dev_handle, FAT_CLST_RES1);
443
    return fat_node_get(dev_handle, 0); /* TODO */
402
}
444
}
403
 
445
 
404
static char fat_plb_get_char(unsigned pos)
446
static char fat_plb_get_char(unsigned pos)
405
{
447
{
406
    return fat_reg.plb_ro[pos % PLB_SIZE];
448
    return fat_reg.plb_ro[pos % PLB_SIZE];
407
}
449
}
408
 
450
 
409
static bool fat_is_directory(void *node)
451
static bool fat_is_directory(void *node)
410
{
452
{
411
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
453
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
412
}
454
}
413
 
455
 
414
static bool fat_is_file(void *node)
456
static bool fat_is_file(void *node)
415
{
457
{
416
    return ((fat_node_t *)node)->type == FAT_FILE;
458
    return ((fat_node_t *)node)->type == FAT_FILE;
417
}
459
}
418
 
460
 
419
/** libfs operations */
461
/** libfs operations */
420
libfs_ops_t fat_libfs_ops = {
462
libfs_ops_t fat_libfs_ops = {
421
    .match = fat_match,
463
    .match = fat_match,
422
    .node_get = fat_node_get,
464
    .node_get = fat_node_get,
423
    .node_put = fat_node_put,
465
    .node_put = fat_node_put,
424
    .create = fat_create,
466
    .create = fat_create,
425
    .destroy = fat_destroy,
467
    .destroy = fat_destroy,
426
    .link = fat_link,
468
    .link = fat_link,
427
    .unlink = fat_unlink,
469
    .unlink = fat_unlink,
428
    .index_get = fat_index_get,
470
    .index_get = fat_index_get,
429
    .size_get = fat_size_get,
471
    .size_get = fat_size_get,
430
    .lnkcnt_get = fat_lnkcnt_get,
472
    .lnkcnt_get = fat_lnkcnt_get,
431
    .has_children = fat_has_children,
473
    .has_children = fat_has_children,
432
    .root_get = fat_root_get,
474
    .root_get = fat_root_get,
433
    .plb_get_char = fat_plb_get_char,
475
    .plb_get_char = fat_plb_get_char,
434
    .is_directory = fat_is_directory,
476
    .is_directory = fat_is_directory,
435
    .is_file = fat_is_file
477
    .is_file = fat_is_file
436
};
478
};
437
 
479
 
438
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
480
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
439
{
481
{
440
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
482
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
441
}
483
}
442
 
484
 
443
/**
485
/**
444
 * @}
486
 * @}
445
 */
487
 */
446
 
488