Subversion Repositories HelenOS

Rev

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

Rev 3009 Rev 3153
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
/** Futex protecting the list of cached free FAT nodes. */
53
/** Futex protecting the list of cached free FAT nodes. */
54
static futex_t ffn_futex = FUTEX_INITIALIZER;
54
static futex_t ffn_futex = FUTEX_INITIALIZER;
55
 
55
 
56
/** List of cached free FAT nodes. */
56
/** List of cached free FAT nodes. */
57
static LIST_INITIALIZE(ffn_head);
57
static LIST_INITIALIZE(ffn_head);
58
 
58
 
59
#define FAT_NAME_LEN        8
59
#define FAT_NAME_LEN        8
60
#define FAT_EXT_LEN     3
60
#define FAT_EXT_LEN     3
61
 
61
 
62
#define FAT_PAD         ' ' 
62
#define FAT_PAD         ' ' 
63
 
63
 
64
#define FAT_DENTRY_UNUSED   0x00
64
#define FAT_DENTRY_UNUSED   0x00
65
#define FAT_DENTRY_E5_ESC   0x05
65
#define FAT_DENTRY_E5_ESC   0x05
66
#define FAT_DENTRY_DOT      0x2e
66
#define FAT_DENTRY_DOT      0x2e
67
#define FAT_DENTRY_ERASED   0xe5
67
#define FAT_DENTRY_ERASED   0xe5
68
 
68
 
69
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
69
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
70
{
70
{
71
    int i;
71
    int i;
72
 
72
 
73
    for (i = 0; i < FAT_NAME_LEN; i++) {
73
    for (i = 0; i < FAT_NAME_LEN; i++) {
74
        if (d->name[i] == FAT_PAD) {
74
        if (d->name[i] == FAT_PAD) {
75
            buf++;
75
            buf++;
76
            break;
76
            break;
77
        }
77
        }
78
        if (d->name[i] == FAT_DENTRY_E5_ESC)
78
        if (d->name[i] == FAT_DENTRY_E5_ESC)
79
            *buf++ = 0xe5;
79
            *buf++ = 0xe5;
80
        else
80
        else
81
            *buf++ = d->name[i];
81
            *buf++ = d->name[i];
82
    }
82
    }
83
    if (d->ext[0] != FAT_PAD)
83
    if (d->ext[0] != FAT_PAD)
84
        *buf++ = '.';
84
        *buf++ = '.';
85
    for (i = 0; i < FAT_EXT_LEN; i++) {
85
    for (i = 0; i < FAT_EXT_LEN; i++) {
86
        if (d->ext[i] == FAT_PAD) {
86
        if (d->ext[i] == FAT_PAD) {
87
            *buf = '\0';
87
            *buf = '\0';
88
            return;
88
            return;
89
        }
89
        }
90
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
90
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
91
            *buf++ = 0xe5;
91
            *buf++ = 0xe5;
92
        else
92
        else
93
            *buf++ = d->ext[i];
93
            *buf++ = d->ext[i];
94
    }
94
    }
95
}
95
}
96
 
96
 
97
/* TODO move somewhere else */
97
/* TODO move somewhere else */
98
typedef struct {
98
typedef struct {
99
    void *data;
99
    void *data;
100
} block_t;
100
} block_t;
101
 
101
 
102
static block_t *block_get(dev_handle_t dev_handle, off_t offset)
102
static block_t *block_get(dev_handle_t dev_handle, off_t offset)
103
{
103
{
104
    return NULL;    /* TODO */
104
    return NULL;    /* TODO */
105
}
105
}
106
 
106
 
107
static void block_put(block_t *block)
107
static void block_put(block_t *block)
108
{
108
{
109
    /* TODO */
109
    /* TODO */
110
}
110
}
111
 
111
 
112
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
112
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
113
 
113
 
114
#define FAT_CLST_RES0   0x0000
114
#define FAT_CLST_RES0   0x0000
115
#define FAT_CLST_RES1   0x0001  /* internally used to mark root directory */
115
#define FAT_CLST_RES1   0x0001
116
#define FAT_CLST_FIRST  0x0002
116
#define FAT_CLST_FIRST  0x0002
117
#define FAT_CLST_BAD    0xfff7
117
#define FAT_CLST_BAD    0xfff7
118
#define FAT_CLST_LAST1  0xfff8
118
#define FAT_CLST_LAST1  0xfff8
119
#define FAT_CLST_LAST8  0xffff
119
#define FAT_CLST_LAST8  0xffff
120
 
120
 
-
 
121
/* internally used to mark root directory's parent */
-
 
122
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
-
 
123
/* internally used to mark root directory */
-
 
124
#define FAT_CLST_ROOT       FAT_CLST_RES1
-
 
125
 
121
#define fat_block_get(np, off) \
126
#define fat_block_get(np, off) \
122
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
127
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
123
 
128
 
124
static block_t *
129
static block_t *
125
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
130
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
126
{
131
{
127
    block_t *bb;
132
    block_t *bb;
128
    block_t *b;
133
    block_t *b;
129
    unsigned bps;
134
    unsigned bps;
130
    unsigned spc;
135
    unsigned spc;
131
    unsigned rscnt;     /* block address of the first FAT */
136
    unsigned rscnt;     /* block address of the first FAT */
132
    unsigned fatcnt;
137
    unsigned fatcnt;
133
    unsigned rde;
138
    unsigned rde;
134
    unsigned rds;       /* root directory size */
139
    unsigned rds;       /* root directory size */
135
    unsigned sf;
140
    unsigned sf;
136
    unsigned ssa;       /* size of the system area */
141
    unsigned ssa;       /* size of the system area */
137
    unsigned clusters;
142
    unsigned clusters;
138
    fat_cluster_t clst = firstc;
143
    fat_cluster_t clst = firstc;
139
    unsigned i;
144
    unsigned i;
140
 
145
 
141
    bb = block_get(dev_handle, BS_BLOCK);
146
    bb = block_get(dev_handle, BS_BLOCK);
142
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
147
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
143
    spc = FAT_BS(bb)->spc;
148
    spc = FAT_BS(bb)->spc;
144
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
149
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
145
    fatcnt = FAT_BS(bb)->fatcnt;
150
    fatcnt = FAT_BS(bb)->fatcnt;
146
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
151
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
147
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
152
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
148
    block_put(bb);
153
    block_put(bb);
149
 
154
 
150
    rds = (sizeof(fat_dentry_t) * rde) / bps;
155
    rds = (sizeof(fat_dentry_t) * rde) / bps;
151
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
156
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
152
    ssa = rscnt + fatcnt * sf + rds;
157
    ssa = rscnt + fatcnt * sf + rds;
153
 
158
 
154
    if (firstc == FAT_CLST_RES1) {
159
    if (firstc == FAT_CLST_ROOT) {
155
        /* root directory special case */
160
        /* root directory special case */
156
        assert(offset < rds);
161
        assert(offset < rds);
157
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset);
162
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset);
158
        return b;
163
        return b;
159
    }
164
    }
160
 
165
 
161
    clusters = offset / spc;
166
    clusters = offset / spc;
162
    for (i = 0; i < clusters; i++) {
167
    for (i = 0; i < clusters; i++) {
163
        unsigned fsec;  /* sector offset relative to FAT1 */
168
        unsigned fsec;  /* sector offset relative to FAT1 */
164
        unsigned fidx;  /* FAT1 entry index */
169
        unsigned fidx;  /* FAT1 entry index */
165
 
170
 
166
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
171
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
167
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
172
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
168
        fidx = clst % (bps / sizeof(fat_cluster_t));
173
        fidx = clst % (bps / sizeof(fat_cluster_t));
169
        /* read FAT1 */
174
        /* read FAT1 */
170
        b = block_get(dev_handle, rscnt + fsec);
175
        b = block_get(dev_handle, rscnt + fsec);
171
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
176
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
172
        assert(clst != FAT_CLST_BAD);
177
        assert(clst != FAT_CLST_BAD);
173
        assert(clst < FAT_CLST_LAST1);
178
        assert(clst < FAT_CLST_LAST1);
174
        block_put(b);
179
        block_put(b);
175
    }
180
    }
176
 
181
 
177
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
182
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
178
        offset % spc);
183
        offset % spc);
179
 
184
 
180
    return b;
185
    return b;
181
}
186
}
182
 
187
 
183
static void fat_node_initialize(fat_node_t *node)
188
static void fat_node_initialize(fat_node_t *node)
184
{
189
{
185
    futex_initialize(&node->lock, 1);
190
    futex_initialize(&node->lock, 1);
186
    node->idx = NULL;
191
    node->idx = NULL;
187
    node->type = 0;
192
    node->type = 0;
188
    link_initialize(&node->ffn_link);
193
    link_initialize(&node->ffn_link);
189
    node->size = 0;
194
    node->size = 0;
190
    node->lnkcnt = 0;
195
    node->lnkcnt = 0;
191
    node->refcnt = 0;
196
    node->refcnt = 0;
192
    node->dirty = false;
197
    node->dirty = false;
193
}
198
}
194
 
199
 
195
static uint16_t fat_bps_get(dev_handle_t dev_handle)
200
static uint16_t fat_bps_get(dev_handle_t dev_handle)
196
{
201
{
197
    block_t *bb;
202
    block_t *bb;
198
    uint16_t bps;
203
    uint16_t bps;
199
   
204
   
200
    bb = block_get(dev_handle, BS_BLOCK);
205
    bb = block_get(dev_handle, BS_BLOCK);
201
    assert(bb != NULL);
206
    assert(bb != NULL);
202
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
207
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
203
    block_put(bb);
208
    block_put(bb);
204
 
209
 
205
    return bps;
210
    return bps;
206
}
211
}
207
 
212
 
208
typedef enum {
213
typedef enum {
209
    FAT_DENTRY_SKIP,
214
    FAT_DENTRY_SKIP,
210
    FAT_DENTRY_LAST,
215
    FAT_DENTRY_LAST,
211
    FAT_DENTRY_VALID
216
    FAT_DENTRY_VALID
212
} fat_dentry_clsf_t;
217
} fat_dentry_clsf_t;
213
 
218
 
214
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
219
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
215
{
220
{
216
    if (d->attr & FAT_ATTR_VOLLABEL) {
221
    if (d->attr & FAT_ATTR_VOLLABEL) {
217
        /* volume label entry */
222
        /* volume label entry */
218
        return FAT_DENTRY_SKIP;
223
        return FAT_DENTRY_SKIP;
219
    }
224
    }
220
    if (d->name[0] == FAT_DENTRY_ERASED) {
225
    if (d->name[0] == FAT_DENTRY_ERASED) {
221
        /* not-currently-used entry */
226
        /* not-currently-used entry */
222
        return FAT_DENTRY_SKIP;
227
        return FAT_DENTRY_SKIP;
223
    }
228
    }
224
    if (d->name[0] == FAT_DENTRY_UNUSED) {
229
    if (d->name[0] == FAT_DENTRY_UNUSED) {
225
        /* never used entry */
230
        /* never used entry */
226
        return FAT_DENTRY_LAST;
231
        return FAT_DENTRY_LAST;
227
    }
232
    }
228
    if (d->name[0] == FAT_DENTRY_DOT) {
233
    if (d->name[0] == FAT_DENTRY_DOT) {
229
        /*
234
        /*
230
         * Most likely '.' or '..'.
235
         * Most likely '.' or '..'.
231
         * It cannot occur in a regular file name.
236
         * It cannot occur in a regular file name.
232
         */
237
         */
233
        return FAT_DENTRY_SKIP;
238
        return FAT_DENTRY_SKIP;
234
    }
239
    }
235
    return FAT_DENTRY_VALID;
240
    return FAT_DENTRY_VALID;
236
}
241
}
237
 
242
 
238
static void fat_node_sync(fat_node_t *node)
243
static void fat_node_sync(fat_node_t *node)
239
{
244
{
240
    /* TODO */
245
    /* TODO */
241
}
246
}
242
 
247
 
243
/** Internal version of fat_node_get().
248
/** Internal version of fat_node_get().
244
 *
249
 *
245
 * @param idxp      Locked index structure.
250
 * @param idxp      Locked index structure.
246
 */
251
 */
247
static void *fat_node_get_core(fat_idx_t *idxp)
252
static void *fat_node_get_core(fat_idx_t *idxp)
248
{
253
{
249
    block_t *b;
254
    block_t *b;
250
    fat_dentry_t *d;
255
    fat_dentry_t *d;
251
    fat_node_t *nodep;
256
    fat_node_t *nodep;
252
    unsigned bps;
257
    unsigned bps;
253
    unsigned dps;
258
    unsigned dps;
254
 
259
 
255
    if (idxp->nodep) {
260
    if (idxp->nodep) {
256
        /*
261
        /*
257
         * We are lucky.
262
         * We are lucky.
258
         * The node is already instantiated in memory.
263
         * The node is already instantiated in memory.
259
         */
264
         */
260
        futex_down(&idxp->nodep->lock);
265
        futex_down(&idxp->nodep->lock);
261
        if (!idxp->nodep->refcnt++)
266
        if (!idxp->nodep->refcnt++)
262
            list_remove(&nodep->ffn_link);
267
            list_remove(&nodep->ffn_link);
263
        futex_up(&idxp->nodep->lock);
268
        futex_up(&idxp->nodep->lock);
264
        return idxp->nodep;
269
        return idxp->nodep;
265
    }
270
    }
266
 
271
 
267
    /*
272
    /*
268
     * We must instantiate the node from the file system.
273
     * We must instantiate the node from the file system.
269
     */
274
     */
270
   
275
   
271
    assert(idxp->pfc);
276
    assert(idxp->pfc);
272
 
277
 
273
    futex_down(&ffn_futex);
278
    futex_down(&ffn_futex);
274
    if (!list_empty(&ffn_head)) {
279
    if (!list_empty(&ffn_head)) {
275
        /* Try to use a cached free node structure. */
280
        /* Try to use a cached free node structure. */
276
        fat_idx_t *idxp_tmp;
281
        fat_idx_t *idxp_tmp;
277
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
282
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
278
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
283
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
279
            goto skip_cache;
284
            goto skip_cache;
280
        idxp_tmp = nodep->idx;
285
        idxp_tmp = nodep->idx;
281
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
286
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
282
            futex_up(&nodep->lock);
287
            futex_up(&nodep->lock);
283
            goto skip_cache;
288
            goto skip_cache;
284
        }
289
        }
285
        list_remove(&nodep->ffn_link);
290
        list_remove(&nodep->ffn_link);
286
        futex_up(&ffn_futex);
291
        futex_up(&ffn_futex);
287
        if (nodep->dirty)
292
        if (nodep->dirty)
288
            fat_node_sync(nodep);
293
            fat_node_sync(nodep);
289
        idxp_tmp->nodep = NULL;
294
        idxp_tmp->nodep = NULL;
290
        futex_up(&nodep->lock);
295
        futex_up(&nodep->lock);
291
        futex_up(&idxp_tmp->lock);
296
        futex_up(&idxp_tmp->lock);
292
    } else {
297
    } else {
293
skip_cache:
298
skip_cache:
294
        /* Try to allocate a new node structure. */
299
        /* Try to allocate a new node structure. */
295
        futex_up(&ffn_futex);
300
        futex_up(&ffn_futex);
296
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
301
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
297
        if (!nodep)
302
        if (!nodep)
298
            return NULL;
303
            return NULL;
299
    }
304
    }
300
    fat_node_initialize(nodep);
305
    fat_node_initialize(nodep);
301
 
306
 
302
    bps = fat_bps_get(idxp->dev_handle);
307
    bps = fat_bps_get(idxp->dev_handle);
303
    dps = bps / sizeof(fat_dentry_t);
308
    dps = bps / sizeof(fat_dentry_t);
304
 
309
 
305
    /* Read the block that contains the dentry of interest. */
310
    /* Read the block that contains the dentry of interest. */
306
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
311
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
307
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
312
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
308
    assert(b);
313
    assert(b);
309
 
314
 
310
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
315
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
311
    if (d->attr & FAT_ATTR_SUBDIR) {
316
    if (d->attr & FAT_ATTR_SUBDIR) {
312
        /*
317
        /*
313
         * The only directory which does not have this bit set is the
318
         * The only directory which does not have this bit set is the
314
         * root directory itself. The root directory node is handled
319
         * root directory itself. The root directory node is handled
315
         * and initialized elsewhere.
320
         * and initialized elsewhere.
316
         */
321
         */
317
        nodep->type = FAT_DIRECTORY;
322
        nodep->type = FAT_DIRECTORY;
318
    } else {
323
    } else {
319
        nodep->type = FAT_FILE;
324
        nodep->type = FAT_FILE;
320
    }
325
    }
321
    nodep->firstc = uint16_t_le2host(d->firstc);
326
    nodep->firstc = uint16_t_le2host(d->firstc);
322
    nodep->size = uint32_t_le2host(d->size);
327
    nodep->size = uint32_t_le2host(d->size);
323
    nodep->lnkcnt = 1;
328
    nodep->lnkcnt = 1;
324
    nodep->refcnt = 1;
329
    nodep->refcnt = 1;
325
 
330
 
326
    block_put(b);
331
    block_put(b);
327
 
332
 
328
    /* Link the idx structure with the node structure. */
333
    /* Link the idx structure with the node structure. */
329
    nodep->idx = idxp;
334
    nodep->idx = idxp;
330
    idxp->nodep = nodep;
335
    idxp->nodep = nodep;
331
 
336
 
332
    return nodep;
337
    return nodep;
333
}
338
}
334
 
339
 
335
/** Instantiate a FAT in-core node. */
340
/** Instantiate a FAT in-core node. */
336
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
341
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
337
{
342
{
338
    void *node;
343
    void *node;
339
    fat_idx_t *idxp;
344
    fat_idx_t *idxp;
340
 
345
 
341
    idxp = fat_idx_get_by_index(dev_handle, index);
346
    idxp = fat_idx_get_by_index(dev_handle, index);
342
    if (!idxp)
347
    if (!idxp)
343
        return NULL;
348
        return NULL;
344
    /* idxp->lock held */
349
    /* idxp->lock held */
345
    node = fat_node_get_core(idxp);
350
    node = fat_node_get_core(idxp);
346
    futex_up(&idxp->lock);
351
    futex_up(&idxp->lock);
347
    return node;
352
    return node;
348
}
353
}
349
 
354
 
350
static void fat_node_put(void *node)
355
static void fat_node_put(void *node)
351
{
356
{
352
    fat_node_t *nodep = (fat_node_t *)node;
357
    fat_node_t *nodep = (fat_node_t *)node;
353
 
358
 
354
    futex_down(&nodep->lock);
359
    futex_down(&nodep->lock);
355
    if (!--nodep->refcnt) {
360
    if (!--nodep->refcnt) {
356
        futex_down(&ffn_futex);
361
        futex_down(&ffn_futex);
357
        list_append(&nodep->ffn_link, &ffn_head);
362
        list_append(&nodep->ffn_link, &ffn_head);
358
        futex_up(&ffn_futex);
363
        futex_up(&ffn_futex);
359
    }
364
    }
360
    futex_up(&nodep->lock);
365
    futex_up(&nodep->lock);
361
}
366
}
362
 
367
 
363
static void *fat_create(int flags)
368
static void *fat_create(int flags)
364
{
369
{
365
    return NULL;    /* not supported at the moment */
370
    return NULL;    /* not supported at the moment */
366
}
371
}
367
 
372
 
368
static int fat_destroy(void *node)
373
static int fat_destroy(void *node)
369
{
374
{
370
    return ENOTSUP; /* not supported at the moment */
375
    return ENOTSUP; /* not supported at the moment */
371
}
376
}
372
 
377
 
373
static bool fat_link(void *prnt, void *chld, const char *name)
378
static bool fat_link(void *prnt, void *chld, const char *name)
374
{
379
{
375
    return false;   /* not supported at the moment */
380
    return false;   /* not supported at the moment */
376
}
381
}
377
 
382
 
378
static int fat_unlink(void *prnt, void *chld)
383
static int fat_unlink(void *prnt, void *chld)
379
{
384
{
380
    return ENOTSUP; /* not supported at the moment */
385
    return ENOTSUP; /* not supported at the moment */
381
}
386
}
382
 
387
 
383
static void *fat_match(void *prnt, const char *component)
388
static void *fat_match(void *prnt, const char *component)
384
{
389
{
385
    fat_node_t *parentp = (fat_node_t *)prnt;
390
    fat_node_t *parentp = (fat_node_t *)prnt;
386
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
391
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
387
    unsigned i, j;
392
    unsigned i, j;
388
    unsigned bps;       /* bytes per sector */
393
    unsigned bps;       /* bytes per sector */
389
    unsigned dps;       /* dentries per sector */
394
    unsigned dps;       /* dentries per sector */
390
    unsigned blocks;
395
    unsigned blocks;
391
    fat_dentry_t *d;
396
    fat_dentry_t *d;
392
    block_t *b;
397
    block_t *b;
393
 
398
 
394
    futex_down(&parentp->idx->lock);
399
    futex_down(&parentp->idx->lock);
395
    bps = fat_bps_get(parentp->idx->dev_handle);
400
    bps = fat_bps_get(parentp->idx->dev_handle);
396
    dps = bps / sizeof(fat_dentry_t);
401
    dps = bps / sizeof(fat_dentry_t);
397
    blocks = parentp->size / bps + (parentp->size % bps != 0);
402
    blocks = parentp->size / bps + (parentp->size % bps != 0);
398
    for (i = 0; i < blocks; i++) {
403
    for (i = 0; i < blocks; i++) {
399
        unsigned dentries;
404
        unsigned dentries;
400
       
405
       
401
        b = fat_block_get(parentp, i);
406
        b = fat_block_get(parentp, i);
402
        dentries = (i == blocks - 1) ?
407
        dentries = (i == blocks - 1) ?
403
            parentp->size % sizeof(fat_dentry_t) :
408
            parentp->size % sizeof(fat_dentry_t) :
404
            dps;
409
            dps;
405
        for (j = 0; j < dentries; j++) {
410
        for (j = 0; j < dentries; j++) {
406
            d = ((fat_dentry_t *)b->data) + j;
411
            d = ((fat_dentry_t *)b->data) + j;
407
            switch (fat_classify_dentry(d)) {
412
            switch (fat_classify_dentry(d)) {
408
            case FAT_DENTRY_SKIP:
413
            case FAT_DENTRY_SKIP:
409
                continue;
414
                continue;
410
            case FAT_DENTRY_LAST:
415
            case FAT_DENTRY_LAST:
411
                block_put(b);
416
                block_put(b);
412
                futex_up(&parentp->idx->lock);
417
                futex_up(&parentp->idx->lock);
413
                return NULL;
418
                return NULL;
414
            default:
419
            default:
415
            case FAT_DENTRY_VALID:
420
            case FAT_DENTRY_VALID:
416
                dentry_name_canonify(d, name);
421
                dentry_name_canonify(d, name);
417
                break;
422
                break;
418
            }
423
            }
419
            if (strcmp(name, component) == 0) {
424
            if (strcmp(name, component) == 0) {
420
                /* hit */
425
                /* hit */
421
                void *node;
426
                void *node;
422
                /*
427
                /*
423
                 * Assume tree hierarchy for locking.  We
428
                 * Assume tree hierarchy for locking.  We
424
                 * already have the parent and now we are going
429
                 * already have the parent and now we are going
425
                 * to lock the child.  Never lock in the oposite
430
                 * to lock the child.  Never lock in the oposite
426
                 * order.
431
                 * order.
427
                 */
432
                 */
428
                fat_idx_t *idx = fat_idx_get_by_pos(
433
                fat_idx_t *idx = fat_idx_get_by_pos(
429
                    parentp->idx->dev_handle, parentp->firstc,
434
                    parentp->idx->dev_handle, parentp->firstc,
430
                    i * dps + j);
435
                    i * dps + j);
431
                futex_up(&parentp->idx->lock);
436
                futex_up(&parentp->idx->lock);
432
                if (!idx) {
437
                if (!idx) {
433
                    /*
438
                    /*
434
                     * Can happen if memory is low or if we
439
                     * Can happen if memory is low or if we
435
                     * run out of 32-bit indices.
440
                     * run out of 32-bit indices.
436
                     */
441
                     */
437
                    block_put(b);
442
                    block_put(b);
438
                    return NULL;
443
                    return NULL;
439
                }
444
                }
440
                node = fat_node_get_core(idx);
445
                node = fat_node_get_core(idx);
441
                futex_up(&idx->lock);
446
                futex_up(&idx->lock);
442
                block_put(b);
447
                block_put(b);
443
                return node;
448
                return node;
444
            }
449
            }
445
        }
450
        }
446
        block_put(b);
451
        block_put(b);
447
    }
452
    }
448
    futex_up(&parentp->idx->lock);
453
    futex_up(&parentp->idx->lock);
449
    return NULL;
454
    return NULL;
450
}
455
}
451
 
456
 
452
static fs_index_t fat_index_get(void *node)
457
static fs_index_t fat_index_get(void *node)
453
{
458
{
454
    fat_node_t *fnodep = (fat_node_t *)node;
459
    fat_node_t *fnodep = (fat_node_t *)node;
455
    if (!fnodep)
460
    if (!fnodep)
456
        return 0;
461
        return 0;
457
    return fnodep->idx->index;
462
    return fnodep->idx->index;
458
}
463
}
459
 
464
 
460
static size_t fat_size_get(void *node)
465
static size_t fat_size_get(void *node)
461
{
466
{
462
    return ((fat_node_t *)node)->size;
467
    return ((fat_node_t *)node)->size;
463
}
468
}
464
 
469
 
465
static unsigned fat_lnkcnt_get(void *node)
470
static unsigned fat_lnkcnt_get(void *node)
466
{
471
{
467
    return ((fat_node_t *)node)->lnkcnt;
472
    return ((fat_node_t *)node)->lnkcnt;
468
}
473
}
469
 
474
 
470
static bool fat_has_children(void *node)
475
static bool fat_has_children(void *node)
471
{
476
{
472
    fat_node_t *nodep = (fat_node_t *)node;
477
    fat_node_t *nodep = (fat_node_t *)node;
473
    unsigned bps;
478
    unsigned bps;
474
    unsigned dps;
479
    unsigned dps;
475
    unsigned blocks;
480
    unsigned blocks;
476
    block_t *b;
481
    block_t *b;
477
    unsigned i, j;
482
    unsigned i, j;
478
 
483
 
479
    if (nodep->type != FAT_DIRECTORY)
484
    if (nodep->type != FAT_DIRECTORY)
480
        return false;
485
        return false;
481
 
486
 
482
    futex_down(&nodep->idx->lock);
487
    futex_down(&nodep->idx->lock);
483
    bps = fat_bps_get(nodep->idx->dev_handle);
488
    bps = fat_bps_get(nodep->idx->dev_handle);
484
    dps = bps / sizeof(fat_dentry_t);
489
    dps = bps / sizeof(fat_dentry_t);
485
 
490
 
486
    blocks = nodep->size / bps + (nodep->size % bps != 0);
491
    blocks = nodep->size / bps + (nodep->size % bps != 0);
487
 
492
 
488
    for (i = 0; i < blocks; i++) {
493
    for (i = 0; i < blocks; i++) {
489
        unsigned dentries;
494
        unsigned dentries;
490
        fat_dentry_t *d;
495
        fat_dentry_t *d;
491
   
496
   
492
        b = fat_block_get(nodep, i);
497
        b = fat_block_get(nodep, i);
493
        dentries = (i == blocks - 1) ?
498
        dentries = (i == blocks - 1) ?
494
            nodep->size % sizeof(fat_dentry_t) :
499
            nodep->size % sizeof(fat_dentry_t) :
495
            dps;
500
            dps;
496
        for (j = 0; j < dentries; j++) {
501
        for (j = 0; j < dentries; j++) {
497
            d = ((fat_dentry_t *)b->data) + j;
502
            d = ((fat_dentry_t *)b->data) + j;
498
            switch (fat_classify_dentry(d)) {
503
            switch (fat_classify_dentry(d)) {
499
            case FAT_DENTRY_SKIP:
504
            case FAT_DENTRY_SKIP:
500
                continue;
505
                continue;
501
            case FAT_DENTRY_LAST:
506
            case FAT_DENTRY_LAST:
502
                block_put(b);
507
                block_put(b);
503
                futex_up(&nodep->idx->lock);
508
                futex_up(&nodep->idx->lock);
504
                return false;
509
                return false;
505
            default:
510
            default:
506
            case FAT_DENTRY_VALID:
511
            case FAT_DENTRY_VALID:
507
                block_put(b);
512
                block_put(b);
508
                futex_up(&nodep->idx->lock);
513
                futex_up(&nodep->idx->lock);
509
                return true;
514
                return true;
510
            }
515
            }
511
            block_put(b);
516
            block_put(b);
512
            futex_up(&nodep->idx->lock);
517
            futex_up(&nodep->idx->lock);
513
            return true;
518
            return true;
514
        }
519
        }
515
        block_put(b);
520
        block_put(b);
516
    }
521
    }
517
 
522
 
518
    futex_up(&nodep->idx->lock);
523
    futex_up(&nodep->idx->lock);
519
    return false;
524
    return false;
520
}
525
}
521
 
526
 
522
static void *fat_root_get(dev_handle_t dev_handle)
527
static void *fat_root_get(dev_handle_t dev_handle)
523
{
528
{
524
    return NULL;    /* TODO */
529
    return fat_node_get(dev_handle, 0);
525
}
530
}
526
 
531
 
527
static char fat_plb_get_char(unsigned pos)
532
static char fat_plb_get_char(unsigned pos)
528
{
533
{
529
    return fat_reg.plb_ro[pos % PLB_SIZE];
534
    return fat_reg.plb_ro[pos % PLB_SIZE];
530
}
535
}
531
 
536
 
532
static bool fat_is_directory(void *node)
537
static bool fat_is_directory(void *node)
533
{
538
{
534
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
539
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
535
}
540
}
536
 
541
 
537
static bool fat_is_file(void *node)
542
static bool fat_is_file(void *node)
538
{
543
{
539
    return ((fat_node_t *)node)->type == FAT_FILE;
544
    return ((fat_node_t *)node)->type == FAT_FILE;
540
}
545
}
541
 
546
 
542
/** libfs operations */
547
/** libfs operations */
543
libfs_ops_t fat_libfs_ops = {
548
libfs_ops_t fat_libfs_ops = {
544
    .match = fat_match,
549
    .match = fat_match,
545
    .node_get = fat_node_get,
550
    .node_get = fat_node_get,
546
    .node_put = fat_node_put,
551
    .node_put = fat_node_put,
547
    .create = fat_create,
552
    .create = fat_create,
548
    .destroy = fat_destroy,
553
    .destroy = fat_destroy,
549
    .link = fat_link,
554
    .link = fat_link,
550
    .unlink = fat_unlink,
555
    .unlink = fat_unlink,
551
    .index_get = fat_index_get,
556
    .index_get = fat_index_get,
552
    .size_get = fat_size_get,
557
    .size_get = fat_size_get,
553
    .lnkcnt_get = fat_lnkcnt_get,
558
    .lnkcnt_get = fat_lnkcnt_get,
554
    .has_children = fat_has_children,
559
    .has_children = fat_has_children,
555
    .root_get = fat_root_get,
560
    .root_get = fat_root_get,
556
    .plb_get_char = fat_plb_get_char,
561
    .plb_get_char = fat_plb_get_char,
557
    .is_directory = fat_is_directory,
562
    .is_directory = fat_is_directory,
558
    .is_file = fat_is_file
563
    .is_file = fat_is_file
559
};
564
};
560
 
565
 
-
 
566
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
-
 
567
{
-
 
568
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
-
 
569
    block_t *bb;
-
 
570
    uint16_t rde;
-
 
571
    int rc;
-
 
572
 
-
 
573
    /* Read the number of root directory entries. */
-
 
574
    bb = block_get(dev_handle, BS_BLOCK);
-
 
575
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
-
 
576
    block_put(bb);
-
 
577
 
-
 
578
    rc = fat_idx_init_by_dev_handle(dev_handle);
-
 
579
    if (rc != EOK) {
-
 
580
        ipc_answer_0(rid, rc);
-
 
581
        return;
-
 
582
    }
-
 
583
 
-
 
584
    /* Initialize the root node. */
-
 
585
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
-
 
586
    if (!rootp) {
-
 
587
        fat_idx_fini_by_dev_handle(dev_handle);
-
 
588
        ipc_answer_0(rid, ENOMEM);
-
 
589
        return;
-
 
590
    }
-
 
591
    fat_node_initialize(rootp);
-
 
592
 
-
 
593
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
-
 
594
    if (!ridxp) {
-
 
595
        free(rootp);
-
 
596
        fat_idx_fini_by_dev_handle(dev_handle);
-
 
597
        ipc_answer_0(rid, ENOMEM);
-
 
598
        return;
-
 
599
    }
-
 
600
    assert(ridxp->index == 0);
-
 
601
    /* ridxp->lock held */
-
 
602
 
-
 
603
    rootp->type = FAT_DIRECTORY;
-
 
604
    rootp->firstc = FAT_CLST_ROOT;
-
 
605
    rootp->refcnt = 1;
-
 
606
    rootp->size = rde * sizeof(fat_dentry_t);
-
 
607
    rootp->idx = ridxp;
-
 
608
    ridxp->nodep = rootp;
-
 
609
   
-
 
610
    futex_up(&ridxp->lock);
-
 
611
 
-
 
612
    ipc_answer_0(rid, EOK);
-
 
613
}
-
 
614
 
-
 
615
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
-
 
616
{
-
 
617
    ipc_answer_0(rid, ENOTSUP);
-
 
618
}
-
 
619
 
561
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
620
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
562
{
621
{
563
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
622
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
564
}
623
}
565
 
624
 
566
/**
625
/**
567
 * @}
626
 * @}
568
 */
627
 */
569
 
628