Subversion Repositories HelenOS

Rev

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

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