Subversion Repositories HelenOS

Rev

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

Rev 2864 Rev 2876
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
static fat_idx_t *fat_idx_map(fat_cluster_t pfc, unsigned pdi)
-
 
116
{
-
 
117
    return NULL;    /* TODO */
-
 
118
}
-
 
119
 
-
 
120
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
115
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
121
 
116
 
122
#define FAT_CLST_RES0   0x0000
117
#define FAT_CLST_RES0   0x0000
123
#define FAT_CLST_RES1   0x0001  /* internally used to mark root directory */
118
#define FAT_CLST_RES1   0x0001  /* internally used to mark root directory */
124
#define FAT_CLST_FIRST  0x0002
119
#define FAT_CLST_FIRST  0x0002
125
#define FAT_CLST_BAD    0xfff7
120
#define FAT_CLST_BAD    0xfff7
126
#define FAT_CLST_LAST1  0xfff8
121
#define FAT_CLST_LAST1  0xfff8
127
#define FAT_CLST_LAST8  0xffff
122
#define FAT_CLST_LAST8  0xffff
128
 
123
 
129
static block_t *fat_block_get(fat_node_t *nodep, off_t offset)
124
static block_t *fat_block_get(fat_node_t *nodep, off_t offset)
130
{
125
{
131
    block_t *bb;
126
    block_t *bb;
132
    block_t *b;
127
    block_t *b;
133
    unsigned bps;
128
    unsigned bps;
134
    unsigned spc;
129
    unsigned spc;
135
    unsigned rscnt;     /* block address of the first FAT */
130
    unsigned rscnt;     /* block address of the first FAT */
136
    unsigned fatcnt;
131
    unsigned fatcnt;
137
    unsigned rde;
132
    unsigned rde;
138
    unsigned rds;       /* root directory size */
133
    unsigned rds;       /* root directory size */
139
    unsigned sf;
134
    unsigned sf;
140
    unsigned ssa;       /* size of the system area */
135
    unsigned ssa;       /* size of the system area */
141
    unsigned clusters;
136
    unsigned clusters;
142
    fat_cluster_t clst = nodep->firstc;
137
    fat_cluster_t clst = nodep->firstc;
143
    unsigned i;
138
    unsigned i;
144
 
139
 
145
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK);
140
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK);
146
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
141
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
147
    spc = FAT_BS(bb)->spc;
142
    spc = FAT_BS(bb)->spc;
148
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
143
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
149
    fatcnt = FAT_BS(bb)->fatcnt;
144
    fatcnt = FAT_BS(bb)->fatcnt;
150
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
145
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
151
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
146
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
152
    block_put(bb);
147
    block_put(bb);
153
 
148
 
154
    rds = (sizeof(fat_dentry_t) * rde) / bps;
149
    rds = (sizeof(fat_dentry_t) * rde) / bps;
155
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
150
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
156
    ssa = rscnt + fatcnt * sf + rds;
151
    ssa = rscnt + fatcnt * sf + rds;
157
 
152
 
158
    if (nodep->idx->index == FAT_CLST_RES1) {
153
    if (nodep->idx->index == FAT_CLST_RES1) {
159
        /* root directory special case */
154
        /* root directory special case */
160
        assert(offset < rds);
155
        assert(offset < rds);
161
        b = block_get(nodep->idx->dev_handle,
156
        b = block_get(nodep->idx->dev_handle,
162
            rscnt + fatcnt * sf + offset);
157
            rscnt + fatcnt * sf + offset);
163
        return b;
158
        return b;
164
    }
159
    }
165
 
160
 
166
    clusters = offset / spc;
161
    clusters = offset / spc;
167
    for (i = 0; i < clusters; i++) {
162
    for (i = 0; i < clusters; i++) {
168
        unsigned fsec;  /* sector offset relative to FAT1 */
163
        unsigned fsec;  /* sector offset relative to FAT1 */
169
        unsigned fidx;  /* FAT1 entry index */
164
        unsigned fidx;  /* FAT1 entry index */
170
 
165
 
171
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
166
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
172
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
167
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
173
        fidx = clst % (bps / sizeof(fat_cluster_t));
168
        fidx = clst % (bps / sizeof(fat_cluster_t));
174
        /* read FAT1 */
169
        /* read FAT1 */
175
        b = block_get(nodep->idx->dev_handle, rscnt + fsec);
170
        b = block_get(nodep->idx->dev_handle, rscnt + fsec);
176
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
171
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
177
        assert(clst != FAT_CLST_BAD);
172
        assert(clst != FAT_CLST_BAD);
178
        assert(clst < FAT_CLST_LAST1);
173
        assert(clst < FAT_CLST_LAST1);
179
        block_put(b);
174
        block_put(b);
180
    }
175
    }
181
 
176
 
182
    b = block_get(nodep->idx->dev_handle, ssa +
177
    b = block_get(nodep->idx->dev_handle, ssa +
183
        (clst - FAT_CLST_FIRST) * spc + offset % spc);
178
        (clst - FAT_CLST_FIRST) * spc + offset % spc);
184
 
179
 
185
    return b;
180
    return b;
186
}
181
}
187
 
182
 
188
static void fat_node_initialize(fat_node_t *node)
183
static void fat_node_initialize(fat_node_t *node)
189
{
184
{
190
    node->idx = NULL;
185
    node->idx = NULL;
191
    node->type = 0;
186
    node->type = 0;
192
    link_initialize(&node->fin_link);
187
    link_initialize(&node->fin_link);
193
    link_initialize(&node->ffn_link);
188
    link_initialize(&node->ffn_link);
194
    node->size = 0;
189
    node->size = 0;
195
    node->lnkcnt = 0;
190
    node->lnkcnt = 0;
196
    node->refcnt = 0;
191
    node->refcnt = 0;
197
    node->dirty = false;
192
    node->dirty = false;
198
}
193
}
199
 
194
 
200
static uint16_t fat_bps_get(dev_handle_t dev_handle)
195
static uint16_t fat_bps_get(dev_handle_t dev_handle)
201
{
196
{
202
    block_t *bb;
197
    block_t *bb;
203
    uint16_t bps;
198
    uint16_t bps;
204
   
199
   
205
    bb = block_get(dev_handle, BS_BLOCK);
200
    bb = block_get(dev_handle, BS_BLOCK);
206
    assert(bb != NULL);
201
    assert(bb != NULL);
207
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
202
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
208
    block_put(bb);
203
    block_put(bb);
209
 
204
 
210
    return bps;
205
    return bps;
211
}
206
}
212
 
207
 
213
typedef enum {
208
typedef enum {
214
    FAT_DENTRY_SKIP,
209
    FAT_DENTRY_SKIP,
215
    FAT_DENTRY_LAST,
210
    FAT_DENTRY_LAST,
216
    FAT_DENTRY_VALID
211
    FAT_DENTRY_VALID
217
} fat_dentry_clsf_t;
212
} fat_dentry_clsf_t;
218
 
213
 
219
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
214
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
220
{
215
{
221
    if (d->attr & FAT_ATTR_VOLLABEL) {
216
    if (d->attr & FAT_ATTR_VOLLABEL) {
222
        /* volume label entry */
217
        /* volume label entry */
223
        return FAT_DENTRY_SKIP;
218
        return FAT_DENTRY_SKIP;
224
    }
219
    }
225
    if (d->name[0] == FAT_DENTRY_ERASED) {
220
    if (d->name[0] == FAT_DENTRY_ERASED) {
226
        /* not-currently-used entry */
221
        /* not-currently-used entry */
227
        return FAT_DENTRY_SKIP;
222
        return FAT_DENTRY_SKIP;
228
    }
223
    }
229
    if (d->name[0] == FAT_DENTRY_UNUSED) {
224
    if (d->name[0] == FAT_DENTRY_UNUSED) {
230
        /* never used entry */
225
        /* never used entry */
231
        return FAT_DENTRY_LAST;
226
        return FAT_DENTRY_LAST;
232
    }
227
    }
233
    if (d->name[0] == FAT_DENTRY_DOT) {
228
    if (d->name[0] == FAT_DENTRY_DOT) {
234
        /*
229
        /*
235
         * Most likely '.' or '..'.
230
         * Most likely '.' or '..'.
236
         * It cannot occur in a regular file name.
231
         * It cannot occur in a regular file name.
237
         */
232
         */
238
        return FAT_DENTRY_SKIP;
233
        return FAT_DENTRY_SKIP;
239
    }
234
    }
240
    return FAT_DENTRY_VALID;
235
    return FAT_DENTRY_VALID;
241
}
236
}
242
 
237
 
243
static void fat_sync_node(fat_node_t *node)
238
static void fat_sync_node(fat_node_t *node)
244
{
239
{
245
    /* TODO */
240
    /* TODO */
246
}
241
}
247
 
242
 
248
/** Instantiate a FAT in-core node. */
243
/** Instantiate a FAT in-core node. */
249
static void *
244
static void *
250
fat_node_get(dev_handle_t dev_handle, fs_index_t index)
245
fat_node_get(dev_handle_t dev_handle, fs_index_t index)
251
{
246
{
252
    return NULL;    /* TODO */
247
    return NULL;    /* TODO */
253
}
248
}
254
 
249
 
255
static void fat_node_put(void *node)
250
static void fat_node_put(void *node)
256
{
251
{
257
    /* TODO */
252
    /* TODO */
258
}
253
}
259
 
254
 
260
static void *fat_create(int flags)
255
static void *fat_create(int flags)
261
{
256
{
262
    return NULL;    /* not supported at the moment */
257
    return NULL;    /* not supported at the moment */
263
}
258
}
264
 
259
 
265
static int fat_destroy(void *node)
260
static int fat_destroy(void *node)
266
{
261
{
267
    return ENOTSUP; /* not supported at the moment */
262
    return ENOTSUP; /* not supported at the moment */
268
}
263
}
269
 
264
 
270
static bool fat_link(void *prnt, void *chld, const char *name)
265
static bool fat_link(void *prnt, void *chld, const char *name)
271
{
266
{
272
    return false;   /* not supported at the moment */
267
    return false;   /* not supported at the moment */
273
}
268
}
274
 
269
 
275
static int fat_unlink(void *prnt, void *chld)
270
static int fat_unlink(void *prnt, void *chld)
276
{
271
{
277
    return ENOTSUP; /* not supported at the moment */
272
    return ENOTSUP; /* not supported at the moment */
278
}
273
}
279
 
274
 
280
static void *fat_match(void *prnt, const char *component)
275
static void *fat_match(void *prnt, const char *component)
281
{
276
{
282
    fat_node_t *parentp = (fat_node_t *)prnt;
277
    fat_node_t *parentp = (fat_node_t *)prnt;
283
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
278
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
284
    unsigned i, j;
279
    unsigned i, j;
285
    unsigned bps;       /* bytes per sector */
280
    unsigned bps;       /* bytes per sector */
286
    unsigned dps;       /* dentries per sector */
281
    unsigned dps;       /* dentries per sector */
287
    unsigned blocks;
282
    unsigned blocks;
288
    fat_dentry_t *d;
283
    fat_dentry_t *d;
289
    block_t *b;
284
    block_t *b;
290
 
285
 
291
    bps = fat_bps_get(parentp->idx->dev_handle);
286
    bps = fat_bps_get(parentp->idx->dev_handle);
292
    dps = bps / sizeof(fat_dentry_t);
287
    dps = bps / sizeof(fat_dentry_t);
293
    blocks = parentp->size / bps + (parentp->size % bps != 0);
288
    blocks = parentp->size / bps + (parentp->size % bps != 0);
294
    for (i = 0; i < blocks; i++) {
289
    for (i = 0; i < blocks; i++) {
295
        unsigned dentries;
290
        unsigned dentries;
296
       
291
       
297
        b = fat_block_get(parentp, i);
292
        b = fat_block_get(parentp, i);
298
        dentries = (i == blocks - 1) ?
293
        dentries = (i == blocks - 1) ?
299
            parentp->size % sizeof(fat_dentry_t) :
294
            parentp->size % sizeof(fat_dentry_t) :
300
            dps;
295
            dps;
301
        for (j = 0; j < dentries; j++) {
296
        for (j = 0; j < dentries; j++) {
302
            d = ((fat_dentry_t *)b->data) + j;
297
            d = ((fat_dentry_t *)b->data) + j;
303
            switch (fat_classify_dentry(d)) {
298
            switch (fat_classify_dentry(d)) {
304
            case FAT_DENTRY_SKIP:
299
            case FAT_DENTRY_SKIP:
305
                continue;
300
                continue;
306
            case FAT_DENTRY_LAST:
301
            case FAT_DENTRY_LAST:
307
                block_put(b);
302
                block_put(b);
308
                return NULL;
303
                return NULL;
309
            default:
304
            default:
310
            case FAT_DENTRY_VALID:
305
            case FAT_DENTRY_VALID:
311
                dentry_name_canonify(d, name);
306
                dentry_name_canonify(d, name);
312
                break;
307
                break;
313
            }
308
            }
314
            if (strcmp(name, component) == 0) {
309
            if (strcmp(name, component) == 0) {
315
                /* hit */
310
                /* hit */
316
                fat_idx_t *idx = fat_idx_map(parentp->firstc,
311
                fat_idx_t *idx = fat_idx_map(parentp->firstc,
317
                    i * dps + j);
312
                    i * dps + j);
318
                void *node = fat_node_get(idx->dev_handle,
313
                void *node = fat_node_get(idx->dev_handle,
319
                    idx->index);
314
                    idx->index);
320
                block_put(b);
315
                block_put(b);
321
                return node;
316
                return node;
322
            }
317
            }
323
        }
318
        }
324
        block_put(b);
319
        block_put(b);
325
    }
320
    }
326
 
321
 
327
    return NULL;
322
    return NULL;
328
}
323
}
329
 
324
 
330
static fs_index_t fat_index_get(void *node)
325
static fs_index_t fat_index_get(void *node)
331
{
326
{
332
    fat_node_t *fnodep = (fat_node_t *)node;
327
    fat_node_t *fnodep = (fat_node_t *)node;
333
    if (!fnodep)
328
    if (!fnodep)
334
        return 0;
329
        return 0;
335
    return fnodep->idx->index;
330
    return fnodep->idx->index;
336
}
331
}
337
 
332
 
338
static size_t fat_size_get(void *node)
333
static size_t fat_size_get(void *node)
339
{
334
{
340
    return ((fat_node_t *)node)->size;
335
    return ((fat_node_t *)node)->size;
341
}
336
}
342
 
337
 
343
static unsigned fat_lnkcnt_get(void *node)
338
static unsigned fat_lnkcnt_get(void *node)
344
{
339
{
345
    return ((fat_node_t *)node)->lnkcnt;
340
    return ((fat_node_t *)node)->lnkcnt;
346
}
341
}
347
 
342
 
348
static bool fat_has_children(void *node)
343
static bool fat_has_children(void *node)
349
{
344
{
350
    fat_node_t *nodep = (fat_node_t *)node;
345
    fat_node_t *nodep = (fat_node_t *)node;
351
    unsigned bps;
346
    unsigned bps;
352
    unsigned dps;
347
    unsigned dps;
353
    unsigned blocks;
348
    unsigned blocks;
354
    block_t *b;
349
    block_t *b;
355
    unsigned i, j;
350
    unsigned i, j;
356
 
351
 
357
    if (nodep->type != FAT_DIRECTORY)
352
    if (nodep->type != FAT_DIRECTORY)
358
        return false;
353
        return false;
359
 
354
 
360
    bps = fat_bps_get(nodep->idx->dev_handle);
355
    bps = fat_bps_get(nodep->idx->dev_handle);
361
    dps = bps / sizeof(fat_dentry_t);
356
    dps = bps / sizeof(fat_dentry_t);
362
 
357
 
363
    blocks = nodep->size / bps + (nodep->size % bps != 0);
358
    blocks = nodep->size / bps + (nodep->size % bps != 0);
364
 
359
 
365
    for (i = 0; i < blocks; i++) {
360
    for (i = 0; i < blocks; i++) {
366
        unsigned dentries;
361
        unsigned dentries;
367
        fat_dentry_t *d;
362
        fat_dentry_t *d;
368
   
363
   
369
        b = fat_block_get(nodep, i);
364
        b = fat_block_get(nodep, i);
370
        dentries = (i == blocks - 1) ?
365
        dentries = (i == blocks - 1) ?
371
            nodep->size % sizeof(fat_dentry_t) :
366
            nodep->size % sizeof(fat_dentry_t) :
372
            dps;
367
            dps;
373
        for (j = 0; j < dentries; j++) {
368
        for (j = 0; j < dentries; j++) {
374
            d = ((fat_dentry_t *)b->data) + j;
369
            d = ((fat_dentry_t *)b->data) + j;
375
            switch (fat_classify_dentry(d)) {
370
            switch (fat_classify_dentry(d)) {
376
            case FAT_DENTRY_SKIP:
371
            case FAT_DENTRY_SKIP:
377
                continue;
372
                continue;
378
            case FAT_DENTRY_LAST:
373
            case FAT_DENTRY_LAST:
379
                block_put(b);
374
                block_put(b);
380
                return false;
375
                return false;
381
            default:
376
            default:
382
            case FAT_DENTRY_VALID:
377
            case FAT_DENTRY_VALID:
383
                block_put(b);
378
                block_put(b);
384
                return true;
379
                return true;
385
            }
380
            }
386
            block_put(b);
381
            block_put(b);
387
            return true;
382
            return true;
388
        }
383
        }
389
        block_put(b);
384
        block_put(b);
390
    }
385
    }
391
 
386
 
392
    return false;
387
    return false;
393
}
388
}
394
 
389
 
395
static void *fat_root_get(dev_handle_t dev_handle)
390
static void *fat_root_get(dev_handle_t dev_handle)
396
{
391
{
397
    return fat_node_get(dev_handle, FAT_CLST_RES1);
392
    return fat_node_get(dev_handle, FAT_CLST_RES1);
398
}
393
}
399
 
394
 
400
static char fat_plb_get_char(unsigned pos)
395
static char fat_plb_get_char(unsigned pos)
401
{
396
{
402
    return fat_reg.plb_ro[pos % PLB_SIZE];
397
    return fat_reg.plb_ro[pos % PLB_SIZE];
403
}
398
}
404
 
399
 
405
static bool fat_is_directory(void *node)
400
static bool fat_is_directory(void *node)
406
{
401
{
407
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
402
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
408
}
403
}
409
 
404
 
410
static bool fat_is_file(void *node)
405
static bool fat_is_file(void *node)
411
{
406
{
412
    return ((fat_node_t *)node)->type == FAT_FILE;
407
    return ((fat_node_t *)node)->type == FAT_FILE;
413
}
408
}
414
 
409
 
415
/** libfs operations */
410
/** libfs operations */
416
libfs_ops_t fat_libfs_ops = {
411
libfs_ops_t fat_libfs_ops = {
417
    .match = fat_match,
412
    .match = fat_match,
418
    .node_get = fat_node_get,
413
    .node_get = fat_node_get,
419
    .node_put = fat_node_put,
414
    .node_put = fat_node_put,
420
    .create = fat_create,
415
    .create = fat_create,
421
    .destroy = fat_destroy,
416
    .destroy = fat_destroy,
422
    .link = fat_link,
417
    .link = fat_link,
423
    .unlink = fat_unlink,
418
    .unlink = fat_unlink,
424
    .index_get = fat_index_get,
419
    .index_get = fat_index_get,
425
    .size_get = fat_size_get,
420
    .size_get = fat_size_get,
426
    .lnkcnt_get = fat_lnkcnt_get,
421
    .lnkcnt_get = fat_lnkcnt_get,
427
    .has_children = fat_has_children,
422
    .has_children = fat_has_children,
428
    .root_get = fat_root_get,
423
    .root_get = fat_root_get,
429
    .plb_get_char = fat_plb_get_char,
424
    .plb_get_char = fat_plb_get_char,
430
    .is_directory = fat_is_directory,
425
    .is_directory = fat_is_directory,
431
    .is_file = fat_is_file
426
    .is_file = fat_is_file
432
};
427
};
433
 
428
 
434
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
429
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
435
{
430
{
436
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
431
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
437
}
432
}
438
 
433
 
439
/**
434
/**
440
 * @}
435
 * @}
441
 */
436
 */
442
 
437