Subversion Repositories HelenOS

Rev

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

Rev 3612 Rev 3623
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 "fat_dentry.h"
39
#include "fat_dentry.h"
40
#include "fat_fat.h"
40
#include "fat_fat.h"
41
#include "../../vfs/vfs.h"
41
#include "../../vfs/vfs.h"
42
#include <libfs.h>
42
#include <libfs.h>
43
#include <libblock.h>
43
#include <libblock.h>
44
#include <ipc/ipc.h>
44
#include <ipc/ipc.h>
45
#include <ipc/services.h>
45
#include <ipc/services.h>
46
#include <ipc/devmap.h>
46
#include <ipc/devmap.h>
47
#include <async.h>
47
#include <async.h>
48
#include <errno.h>
48
#include <errno.h>
49
#include <string.h>
49
#include <string.h>
50
#include <byteorder.h>
50
#include <byteorder.h>
51
#include <libadt/hash_table.h>
51
#include <libadt/hash_table.h>
52
#include <libadt/list.h>
52
#include <libadt/list.h>
53
#include <assert.h>
53
#include <assert.h>
54
#include <futex.h>
54
#include <futex.h>
55
#include <sys/mman.h>
55
#include <sys/mman.h>
56
#include <align.h>
56
#include <align.h>
57
 
57
 
58
/** Futex protecting the list of cached free FAT nodes. */
58
/** Futex protecting the list of cached free FAT nodes. */
59
static futex_t ffn_futex = FUTEX_INITIALIZER;
59
static futex_t ffn_futex = FUTEX_INITIALIZER;
60
 
60
 
61
/** List of cached free FAT nodes. */
61
/** List of cached free FAT nodes. */
62
static LIST_INITIALIZE(ffn_head);
62
static LIST_INITIALIZE(ffn_head);
63
 
63
 
64
static void fat_node_initialize(fat_node_t *node)
64
static void fat_node_initialize(fat_node_t *node)
65
{
65
{
66
    futex_initialize(&node->lock, 1);
66
    futex_initialize(&node->lock, 1);
67
    node->idx = NULL;
67
    node->idx = NULL;
68
    node->type = 0;
68
    node->type = 0;
69
    link_initialize(&node->ffn_link);
69
    link_initialize(&node->ffn_link);
70
    node->size = 0;
70
    node->size = 0;
71
    node->lnkcnt = 0;
71
    node->lnkcnt = 0;
72
    node->refcnt = 0;
72
    node->refcnt = 0;
73
    node->dirty = false;
73
    node->dirty = false;
74
}
74
}
75
 
75
 
76
static void fat_node_sync(fat_node_t *node)
76
static void fat_node_sync(fat_node_t *node)
77
{
77
{
78
    block_t *b;
78
    block_t *b;
79
    fat_bs_t *bs;
79
    fat_bs_t *bs;
80
    fat_dentry_t *d;
80
    fat_dentry_t *d;
81
    uint16_t bps;
81
    uint16_t bps;
82
    unsigned dps;
82
    unsigned dps;
83
   
83
   
84
    assert(node->dirty);
84
    assert(node->dirty);
85
 
85
 
86
    bs = block_bb_get(node->idx->dev_handle);
86
    bs = block_bb_get(node->idx->dev_handle);
87
    bps = uint16_t_le2host(bs->bps);
87
    bps = uint16_t_le2host(bs->bps);
88
    dps = bps / sizeof(fat_dentry_t);
88
    dps = bps / sizeof(fat_dentry_t);
89
   
89
   
90
    /* Read the block that contains the dentry of interest. */
90
    /* Read the block that contains the dentry of interest. */
91
    b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc,
91
    b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc,
92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
93
 
93
 
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
95
 
95
 
96
    d->firstc = host2uint16_t_le(node->firstc);
96
    d->firstc = host2uint16_t_le(node->firstc);
97
    if (node->type == FAT_FILE)
97
    if (node->type == FAT_FILE)
98
        d->size = host2uint32_t_le(node->size);
98
        d->size = host2uint32_t_le(node->size);
99
    /* TODO: update other fields? (e.g time fields, attr field) */
99
    /* TODO: update other fields? (e.g time fields, attr field) */
100
   
100
   
101
    b->dirty = true;        /* need to sync block */
101
    b->dirty = true;        /* need to sync block */
102
    block_put(b);
102
    block_put(b);
103
}
103
}
104
 
104
 
105
static fat_node_t *fat_node_get_new(void)
105
static fat_node_t *fat_node_get_new(void)
106
{
106
{
107
    fat_node_t *nodep;
107
    fat_node_t *nodep;
108
 
108
 
109
    futex_down(&ffn_futex);
109
    futex_down(&ffn_futex);
110
    if (!list_empty(&ffn_head)) {
110
    if (!list_empty(&ffn_head)) {
111
        /* Try to use a cached free node structure. */
111
        /* Try to use a cached free node structure. */
112
        fat_idx_t *idxp_tmp;
112
        fat_idx_t *idxp_tmp;
113
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
113
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
114
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
114
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
115
            goto skip_cache;
115
            goto skip_cache;
116
        idxp_tmp = nodep->idx;
116
        idxp_tmp = nodep->idx;
117
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
117
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
118
            futex_up(&nodep->lock);
118
            futex_up(&nodep->lock);
119
            goto skip_cache;
119
            goto skip_cache;
120
        }
120
        }
121
        list_remove(&nodep->ffn_link);
121
        list_remove(&nodep->ffn_link);
122
        futex_up(&ffn_futex);
122
        futex_up(&ffn_futex);
123
        if (nodep->dirty)
123
        if (nodep->dirty)
124
            fat_node_sync(nodep);
124
            fat_node_sync(nodep);
125
        idxp_tmp->nodep = NULL;
125
        idxp_tmp->nodep = NULL;
126
        futex_up(&nodep->lock);
126
        futex_up(&nodep->lock);
127
        futex_up(&idxp_tmp->lock);
127
        futex_up(&idxp_tmp->lock);
128
    } else {
128
    } else {
129
skip_cache:
129
skip_cache:
130
        /* Try to allocate a new node structure. */
130
        /* Try to allocate a new node structure. */
131
        futex_up(&ffn_futex);
131
        futex_up(&ffn_futex);
132
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
132
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
133
        if (!nodep)
133
        if (!nodep)
134
            return NULL;
134
            return NULL;
135
    }
135
    }
136
    fat_node_initialize(nodep);
136
    fat_node_initialize(nodep);
137
   
137
   
138
    return nodep;
138
    return nodep;
139
}
139
}
140
 
140
 
141
/** Internal version of fat_node_get().
141
/** Internal version of fat_node_get().
142
 *
142
 *
143
 * @param idxp      Locked index structure.
143
 * @param idxp      Locked index structure.
144
 */
144
 */
145
static void *fat_node_get_core(fat_idx_t *idxp)
145
static void *fat_node_get_core(fat_idx_t *idxp)
146
{
146
{
147
    block_t *b;
147
    block_t *b;
148
    fat_bs_t *bs;
148
    fat_bs_t *bs;
149
    fat_dentry_t *d;
149
    fat_dentry_t *d;
150
    fat_node_t *nodep = NULL;
150
    fat_node_t *nodep = NULL;
151
    unsigned bps;
151
    unsigned bps;
152
    unsigned spc;
152
    unsigned spc;
153
    unsigned dps;
153
    unsigned dps;
154
 
154
 
155
    if (idxp->nodep) {
155
    if (idxp->nodep) {
156
        /*
156
        /*
157
         * We are lucky.
157
         * We are lucky.
158
         * The node is already instantiated in memory.
158
         * The node is already instantiated in memory.
159
         */
159
         */
160
        futex_down(&idxp->nodep->lock);
160
        futex_down(&idxp->nodep->lock);
161
        if (!idxp->nodep->refcnt++)
161
        if (!idxp->nodep->refcnt++)
162
            list_remove(&idxp->nodep->ffn_link);
162
            list_remove(&idxp->nodep->ffn_link);
163
        futex_up(&idxp->nodep->lock);
163
        futex_up(&idxp->nodep->lock);
164
        return idxp->nodep;
164
        return idxp->nodep;
165
    }
165
    }
166
 
166
 
167
    /*
167
    /*
168
     * We must instantiate the node from the file system.
168
     * We must instantiate the node from the file system.
169
     */
169
     */
170
   
170
   
171
    assert(idxp->pfc);
171
    assert(idxp->pfc);
172
 
172
 
173
    nodep = fat_node_get_new();
173
    nodep = fat_node_get_new();
174
    if (!nodep)
174
    if (!nodep)
175
        return NULL;
175
        return NULL;
176
 
176
 
177
    bs = block_bb_get(idxp->dev_handle);
177
    bs = block_bb_get(idxp->dev_handle);
178
    bps = uint16_t_le2host(bs->bps);
178
    bps = uint16_t_le2host(bs->bps);
179
    spc = bs->spc;
179
    spc = bs->spc;
180
    dps = bps / sizeof(fat_dentry_t);
180
    dps = bps / sizeof(fat_dentry_t);
181
 
181
 
182
    /* Read the block that contains the dentry of interest. */
182
    /* Read the block that contains the dentry of interest. */
183
    b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
183
    b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
184
        (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
184
        (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
185
    assert(b);
185
    assert(b);
186
 
186
 
187
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
187
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
188
    if (d->attr & FAT_ATTR_SUBDIR) {
188
    if (d->attr & FAT_ATTR_SUBDIR) {
189
        /*
189
        /*
190
         * The only directory which does not have this bit set is the
190
         * The only directory which does not have this bit set is the
191
         * root directory itself. The root directory node is handled
191
         * root directory itself. The root directory node is handled
192
         * and initialized elsewhere.
192
         * and initialized elsewhere.
193
         */
193
         */
194
        nodep->type = FAT_DIRECTORY;
194
        nodep->type = FAT_DIRECTORY;
195
        /*
195
        /*
196
         * Unfortunately, the 'size' field of the FAT dentry is not
196
         * Unfortunately, the 'size' field of the FAT dentry is not
197
         * defined for the directory entry type. We must determine the
197
         * defined for the directory entry type. We must determine the
198
         * size of the directory by walking the FAT.
198
         * size of the directory by walking the FAT.
199
         */
199
         */
200
        nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle,
200
        nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle,
201
            uint16_t_le2host(d->firstc));
201
            uint16_t_le2host(d->firstc));
202
    } else {
202
    } else {
203
        nodep->type = FAT_FILE;
203
        nodep->type = FAT_FILE;
204
        nodep->size = uint32_t_le2host(d->size);
204
        nodep->size = uint32_t_le2host(d->size);
205
    }
205
    }
206
    nodep->firstc = uint16_t_le2host(d->firstc);
206
    nodep->firstc = uint16_t_le2host(d->firstc);
207
    nodep->lnkcnt = 1;
207
    nodep->lnkcnt = 1;
208
    nodep->refcnt = 1;
208
    nodep->refcnt = 1;
209
 
209
 
210
    block_put(b);
210
    block_put(b);
211
 
211
 
212
    /* Link the idx structure with the node structure. */
212
    /* Link the idx structure with the node structure. */
213
    nodep->idx = idxp;
213
    nodep->idx = idxp;
214
    idxp->nodep = nodep;
214
    idxp->nodep = nodep;
215
 
215
 
216
    return nodep;
216
    return nodep;
217
}
217
}
218
 
218
 
-
 
219
/*
-
 
220
 * Forward declarations of FAT libfs operations.
-
 
221
 */
-
 
222
static void *fat_node_get(dev_handle_t, fs_index_t);
-
 
223
static void fat_node_put(void *);
-
 
224
static void *fat_create_node(dev_handle_t, int);
-
 
225
static int fat_destroy_node(void *);
-
 
226
static bool fat_link(void *, void *, const char *);
-
 
227
static int fat_unlink(void *, void *);
-
 
228
static void *fat_match(void *, const char *);
-
 
229
static fs_index_t fat_index_get(void *);
-
 
230
static size_t fat_size_get(void *);
-
 
231
static unsigned fat_lnkcnt_get(void *);
-
 
232
static bool fat_has_children(void *);
-
 
233
static void *fat_root_get(dev_handle_t);
-
 
234
static char fat_plb_get_char(unsigned);
-
 
235
static bool fat_is_directory(void *);
-
 
236
static bool fat_is_file(void *node);
-
 
237
 
-
 
238
/*
-
 
239
 * FAT libfs operations.
-
 
240
 */
-
 
241
 
219
/** Instantiate a FAT in-core node. */
242
/** Instantiate a FAT in-core node. */
220
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
243
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
221
{
244
{
222
    void *node;
245
    void *node;
223
    fat_idx_t *idxp;
246
    fat_idx_t *idxp;
224
 
247
 
225
    idxp = fat_idx_get_by_index(dev_handle, index);
248
    idxp = fat_idx_get_by_index(dev_handle, index);
226
    if (!idxp)
249
    if (!idxp)
227
        return NULL;
250
        return NULL;
228
    /* idxp->lock held */
251
    /* idxp->lock held */
229
    node = fat_node_get_core(idxp);
252
    node = fat_node_get_core(idxp);
230
    futex_up(&idxp->lock);
253
    futex_up(&idxp->lock);
231
    return node;
254
    return node;
232
}
255
}
233
 
256
 
234
static void fat_node_put(void *node)
257
void fat_node_put(void *node)
235
{
258
{
236
    fat_node_t *nodep = (fat_node_t *)node;
259
    fat_node_t *nodep = (fat_node_t *)node;
237
    bool destroy = false;
260
    bool destroy = false;
238
 
261
 
239
    futex_down(&nodep->lock);
262
    futex_down(&nodep->lock);
240
    if (!--nodep->refcnt) {
263
    if (!--nodep->refcnt) {
241
        if (nodep->idx) {
264
        if (nodep->idx) {
242
            futex_down(&ffn_futex);
265
            futex_down(&ffn_futex);
243
            list_append(&nodep->ffn_link, &ffn_head);
266
            list_append(&nodep->ffn_link, &ffn_head);
244
            futex_up(&ffn_futex);
267
            futex_up(&ffn_futex);
245
        } else {
268
        } else {
246
            /*
269
            /*
247
             * The node does not have any index structure associated
270
             * The node does not have any index structure associated
248
             * with itself. This can only mean that we are releasing
271
             * with itself. This can only mean that we are releasing
249
             * the node after a failed attempt to allocate the index
272
             * the node after a failed attempt to allocate the index
250
             * structure for it.
273
             * structure for it.
251
             */
274
             */
252
            destroy = true;
275
            destroy = true;
253
        }
276
        }
254
    }
277
    }
255
    futex_up(&nodep->lock);
278
    futex_up(&nodep->lock);
256
    if (destroy)
279
    if (destroy)
257
        free(node);
280
        free(node);
258
}
281
}
259
 
282
 
260
static void *fat_create_node(dev_handle_t dev_handle, int flags)
283
void *fat_create_node(dev_handle_t dev_handle, int flags)
261
{
284
{
262
    fat_idx_t *idxp;
285
    fat_idx_t *idxp;
263
    fat_node_t *nodep;
286
    fat_node_t *nodep;
264
 
287
 
265
    nodep = fat_node_get_new();
288
    nodep = fat_node_get_new();
266
    if (!nodep)
289
    if (!nodep)
267
        return NULL;
290
        return NULL;
268
    idxp = fat_idx_get_new(dev_handle);
291
    idxp = fat_idx_get_new(dev_handle);
269
    if (!idxp) {
292
    if (!idxp) {
270
        fat_node_put(nodep);
293
        fat_node_put(nodep);
271
        return NULL;
294
        return NULL;
272
    }
295
    }
273
    /* idxp->lock held */
296
    /* idxp->lock held */
274
    if (flags & L_DIRECTORY) {
297
    if (flags & L_DIRECTORY) {
275
        nodep->type = FAT_DIRECTORY;
298
        nodep->type = FAT_DIRECTORY;
276
    } else {
299
    } else {
277
        nodep->type = FAT_FILE;
300
        nodep->type = FAT_FILE;
278
    }
301
    }
279
    nodep->size = 0;
302
    nodep->size = 0;
280
    nodep->firstc = FAT_CLST_RES0;
303
    nodep->firstc = FAT_CLST_RES0;
281
    nodep->lnkcnt = 0;  /* not linked anywhere */
304
    nodep->lnkcnt = 0;  /* not linked anywhere */
282
    nodep->refcnt = 1;
305
    nodep->refcnt = 1;
283
 
306
 
284
    nodep->idx = idxp;
307
    nodep->idx = idxp;
285
    idxp->nodep = nodep;
308
    idxp->nodep = nodep;
286
 
309
 
287
    futex_up(&idxp->lock);
310
    futex_up(&idxp->lock);
288
    return nodep;
311
    return nodep;
289
}
312
}
290
 
313
 
291
static int fat_destroy_node(void *node)
314
int fat_destroy_node(void *node)
292
{
315
{
-
 
316
    fat_node_t *nodep = (fat_node_t *)node;
-
 
317
    fat_bs_t *bs;
-
 
318
 
-
 
319
    /*
-
 
320
     * The node is not reachable from the file system. This means that the
-
 
321
     * link count should be zero and that the index structure cannot be
-
 
322
     * found in the position hash. Obviously, we don't need to lock the node
-
 
323
     * nor its index structure.
-
 
324
     */
-
 
325
    assert(nodep->lnkcnt == 0);
-
 
326
 
-
 
327
    /*
-
 
328
     * The node may not have any children.
-
 
329
     */
-
 
330
    assert(fat_has_children(node) == false);
-
 
331
 
-
 
332
    bs = block_bb_get(nodep->idx->dev_handle);
-
 
333
    if (nodep->firstc != FAT_CLST_RES0) {
-
 
334
        assert(nodep->size);
293
    return ENOTSUP; /* not supported at the moment */
335
        /* Free all clusters allocated to the node. */
-
 
336
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
-
 
337
    }
-
 
338
 
-
 
339
    fat_idx_destroy(nodep->idx);
-
 
340
    free(nodep);
-
 
341
    return EOK;
294
}
342
}
295
 
343
 
296
static bool fat_link(void *prnt, void *chld, const char *name)
344
bool fat_link(void *prnt, void *chld, const char *name)
297
{
345
{
298
    return false;   /* not supported at the moment */
346
    return false;   /* not supported at the moment */
299
}
347
}
300
 
348
 
301
static int fat_unlink(void *prnt, void *chld)
349
int fat_unlink(void *prnt, void *chld)
302
{
350
{
303
    return ENOTSUP; /* not supported at the moment */
351
    return ENOTSUP; /* not supported at the moment */
304
}
352
}
305
 
353
 
306
static void *fat_match(void *prnt, const char *component)
354
void *fat_match(void *prnt, const char *component)
307
{
355
{
308
    fat_bs_t *bs;
356
    fat_bs_t *bs;
309
    fat_node_t *parentp = (fat_node_t *)prnt;
357
    fat_node_t *parentp = (fat_node_t *)prnt;
310
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
358
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
311
    unsigned i, j;
359
    unsigned i, j;
312
    unsigned bps;       /* bytes per sector */
360
    unsigned bps;       /* bytes per sector */
313
    unsigned dps;       /* dentries per sector */
361
    unsigned dps;       /* dentries per sector */
314
    unsigned blocks;
362
    unsigned blocks;
315
    fat_dentry_t *d;
363
    fat_dentry_t *d;
316
    block_t *b;
364
    block_t *b;
317
 
365
 
318
    futex_down(&parentp->idx->lock);
366
    futex_down(&parentp->idx->lock);
319
    bs = block_bb_get(parentp->idx->dev_handle);
367
    bs = block_bb_get(parentp->idx->dev_handle);
320
    bps = uint16_t_le2host(bs->bps);
368
    bps = uint16_t_le2host(bs->bps);
321
    dps = bps / sizeof(fat_dentry_t);
369
    dps = bps / sizeof(fat_dentry_t);
322
    blocks = parentp->size / bps;
370
    blocks = parentp->size / bps;
323
    for (i = 0; i < blocks; i++) {
371
    for (i = 0; i < blocks; i++) {
324
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
372
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
325
        for (j = 0; j < dps; j++) {
373
        for (j = 0; j < dps; j++) {
326
            d = ((fat_dentry_t *)b->data) + j;
374
            d = ((fat_dentry_t *)b->data) + j;
327
            switch (fat_classify_dentry(d)) {
375
            switch (fat_classify_dentry(d)) {
328
            case FAT_DENTRY_SKIP:
376
            case FAT_DENTRY_SKIP:
329
                continue;
377
                continue;
330
            case FAT_DENTRY_LAST:
378
            case FAT_DENTRY_LAST:
331
                block_put(b);
379
                block_put(b);
332
                futex_up(&parentp->idx->lock);
380
                futex_up(&parentp->idx->lock);
333
                return NULL;
381
                return NULL;
334
            default:
382
            default:
335
            case FAT_DENTRY_VALID:
383
            case FAT_DENTRY_VALID:
336
                dentry_name_canonify(d, name);
384
                dentry_name_canonify(d, name);
337
                break;
385
                break;
338
            }
386
            }
339
            if (stricmp(name, component) == 0) {
387
            if (stricmp(name, component) == 0) {
340
                /* hit */
388
                /* hit */
341
                void *node;
389
                void *node;
342
                /*
390
                /*
343
                 * Assume tree hierarchy for locking.  We
391
                 * Assume tree hierarchy for locking.  We
344
                 * already have the parent and now we are going
392
                 * already have the parent and now we are going
345
                 * to lock the child.  Never lock in the oposite
393
                 * to lock the child.  Never lock in the oposite
346
                 * order.
394
                 * order.
347
                 */
395
                 */
348
                fat_idx_t *idx = fat_idx_get_by_pos(
396
                fat_idx_t *idx = fat_idx_get_by_pos(
349
                    parentp->idx->dev_handle, parentp->firstc,
397
                    parentp->idx->dev_handle, parentp->firstc,
350
                    i * dps + j);
398
                    i * dps + j);
351
                futex_up(&parentp->idx->lock);
399
                futex_up(&parentp->idx->lock);
352
                if (!idx) {
400
                if (!idx) {
353
                    /*
401
                    /*
354
                     * Can happen if memory is low or if we
402
                     * Can happen if memory is low or if we
355
                     * run out of 32-bit indices.
403
                     * run out of 32-bit indices.
356
                     */
404
                     */
357
                    block_put(b);
405
                    block_put(b);
358
                    return NULL;
406
                    return NULL;
359
                }
407
                }
360
                node = fat_node_get_core(idx);
408
                node = fat_node_get_core(idx);
361
                futex_up(&idx->lock);
409
                futex_up(&idx->lock);
362
                block_put(b);
410
                block_put(b);
363
                return node;
411
                return node;
364
            }
412
            }
365
        }
413
        }
366
        block_put(b);
414
        block_put(b);
367
    }
415
    }
368
 
416
 
369
    futex_up(&parentp->idx->lock);
417
    futex_up(&parentp->idx->lock);
370
    return NULL;
418
    return NULL;
371
}
419
}
372
 
420
 
373
static fs_index_t fat_index_get(void *node)
421
fs_index_t fat_index_get(void *node)
374
{
422
{
375
    fat_node_t *fnodep = (fat_node_t *)node;
423
    fat_node_t *fnodep = (fat_node_t *)node;
376
    if (!fnodep)
424
    if (!fnodep)
377
        return 0;
425
        return 0;
378
    return fnodep->idx->index;
426
    return fnodep->idx->index;
379
}
427
}
380
 
428
 
381
static size_t fat_size_get(void *node)
429
size_t fat_size_get(void *node)
382
{
430
{
383
    return ((fat_node_t *)node)->size;
431
    return ((fat_node_t *)node)->size;
384
}
432
}
385
 
433
 
386
static unsigned fat_lnkcnt_get(void *node)
434
unsigned fat_lnkcnt_get(void *node)
387
{
435
{
388
    return ((fat_node_t *)node)->lnkcnt;
436
    return ((fat_node_t *)node)->lnkcnt;
389
}
437
}
390
 
438
 
391
static bool fat_has_children(void *node)
439
bool fat_has_children(void *node)
392
{
440
{
393
    fat_bs_t *bs;
441
    fat_bs_t *bs;
394
    fat_node_t *nodep = (fat_node_t *)node;
442
    fat_node_t *nodep = (fat_node_t *)node;
395
    unsigned bps;
443
    unsigned bps;
396
    unsigned dps;
444
    unsigned dps;
397
    unsigned blocks;
445
    unsigned blocks;
398
    block_t *b;
446
    block_t *b;
399
    unsigned i, j;
447
    unsigned i, j;
400
 
448
 
401
    if (nodep->type != FAT_DIRECTORY)
449
    if (nodep->type != FAT_DIRECTORY)
402
        return false;
450
        return false;
403
   
451
   
404
    futex_down(&nodep->idx->lock);
452
    futex_down(&nodep->idx->lock);
405
    bs = block_bb_get(nodep->idx->dev_handle);
453
    bs = block_bb_get(nodep->idx->dev_handle);
406
    bps = uint16_t_le2host(bs->bps);
454
    bps = uint16_t_le2host(bs->bps);
407
    dps = bps / sizeof(fat_dentry_t);
455
    dps = bps / sizeof(fat_dentry_t);
408
 
456
 
409
    blocks = nodep->size / bps;
457
    blocks = nodep->size / bps;
410
 
458
 
411
    for (i = 0; i < blocks; i++) {
459
    for (i = 0; i < blocks; i++) {
412
        fat_dentry_t *d;
460
        fat_dentry_t *d;
413
   
461
   
414
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
462
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
415
        for (j = 0; j < dps; j++) {
463
        for (j = 0; j < dps; j++) {
416
            d = ((fat_dentry_t *)b->data) + j;
464
            d = ((fat_dentry_t *)b->data) + j;
417
            switch (fat_classify_dentry(d)) {
465
            switch (fat_classify_dentry(d)) {
418
            case FAT_DENTRY_SKIP:
466
            case FAT_DENTRY_SKIP:
419
                continue;
467
                continue;
420
            case FAT_DENTRY_LAST:
468
            case FAT_DENTRY_LAST:
421
                block_put(b);
469
                block_put(b);
422
                futex_up(&nodep->idx->lock);
470
                futex_up(&nodep->idx->lock);
423
                return false;
471
                return false;
424
            default:
472
            default:
425
            case FAT_DENTRY_VALID:
473
            case FAT_DENTRY_VALID:
426
                block_put(b);
474
                block_put(b);
427
                futex_up(&nodep->idx->lock);
475
                futex_up(&nodep->idx->lock);
428
                return true;
476
                return true;
429
            }
477
            }
430
            block_put(b);
478
            block_put(b);
431
            futex_up(&nodep->idx->lock);
479
            futex_up(&nodep->idx->lock);
432
            return true;
480
            return true;
433
        }
481
        }
434
        block_put(b);
482
        block_put(b);
435
    }
483
    }
436
 
484
 
437
    futex_up(&nodep->idx->lock);
485
    futex_up(&nodep->idx->lock);
438
    return false;
486
    return false;
439
}
487
}
440
 
488
 
441
static void *fat_root_get(dev_handle_t dev_handle)
489
void *fat_root_get(dev_handle_t dev_handle)
442
{
490
{
443
    return fat_node_get(dev_handle, 0);
491
    return fat_node_get(dev_handle, 0);
444
}
492
}
445
 
493
 
446
static char fat_plb_get_char(unsigned pos)
494
char fat_plb_get_char(unsigned pos)
447
{
495
{
448
    return fat_reg.plb_ro[pos % PLB_SIZE];
496
    return fat_reg.plb_ro[pos % PLB_SIZE];
449
}
497
}
450
 
498
 
451
static bool fat_is_directory(void *node)
499
bool fat_is_directory(void *node)
452
{
500
{
453
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
501
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
454
}
502
}
455
 
503
 
456
static bool fat_is_file(void *node)
504
bool fat_is_file(void *node)
457
{
505
{
458
    return ((fat_node_t *)node)->type == FAT_FILE;
506
    return ((fat_node_t *)node)->type == FAT_FILE;
459
}
507
}
460
 
508
 
461
/** libfs operations */
509
/** libfs operations */
462
libfs_ops_t fat_libfs_ops = {
510
libfs_ops_t fat_libfs_ops = {
463
    .match = fat_match,
511
    .match = fat_match,
464
    .node_get = fat_node_get,
512
    .node_get = fat_node_get,
465
    .node_put = fat_node_put,
513
    .node_put = fat_node_put,
466
    .create = fat_create_node,
514
    .create = fat_create_node,
467
    .destroy = fat_destroy_node,
515
    .destroy = fat_destroy_node,
468
    .link = fat_link,
516
    .link = fat_link,
469
    .unlink = fat_unlink,
517
    .unlink = fat_unlink,
470
    .index_get = fat_index_get,
518
    .index_get = fat_index_get,
471
    .size_get = fat_size_get,
519
    .size_get = fat_size_get,
472
    .lnkcnt_get = fat_lnkcnt_get,
520
    .lnkcnt_get = fat_lnkcnt_get,
473
    .has_children = fat_has_children,
521
    .has_children = fat_has_children,
474
    .root_get = fat_root_get,
522
    .root_get = fat_root_get,
475
    .plb_get_char = fat_plb_get_char,
523
    .plb_get_char = fat_plb_get_char,
476
    .is_directory = fat_is_directory,
524
    .is_directory = fat_is_directory,
477
    .is_file = fat_is_file
525
    .is_file = fat_is_file
478
};
526
};
479
 
527
 
480
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
528
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
481
{
529
{
482
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
530
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
483
    fat_bs_t *bs;
531
    fat_bs_t *bs;
484
    uint16_t bps;
532
    uint16_t bps;
485
    uint16_t rde;
533
    uint16_t rde;
486
    int rc;
534
    int rc;
487
 
535
 
488
    /* initialize libblock */
536
    /* initialize libblock */
489
    rc = block_init(dev_handle, BS_SIZE);
537
    rc = block_init(dev_handle, BS_SIZE);
490
    if (rc != EOK) {
538
    if (rc != EOK) {
491
        ipc_answer_0(rid, rc);
539
        ipc_answer_0(rid, rc);
492
        return;
540
        return;
493
    }
541
    }
494
 
542
 
495
    /* prepare the boot block */
543
    /* prepare the boot block */
496
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
544
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
497
    if (rc != EOK) {
545
    if (rc != EOK) {
498
        block_fini(dev_handle);
546
        block_fini(dev_handle);
499
        ipc_answer_0(rid, rc);
547
        ipc_answer_0(rid, rc);
500
        return;
548
        return;
501
    }
549
    }
502
 
550
 
503
    /* get the buffer with the boot sector */
551
    /* get the buffer with the boot sector */
504
    bs = block_bb_get(dev_handle);
552
    bs = block_bb_get(dev_handle);
505
   
553
   
506
    /* Read the number of root directory entries. */
554
    /* Read the number of root directory entries. */
507
    bps = uint16_t_le2host(bs->bps);
555
    bps = uint16_t_le2host(bs->bps);
508
    rde = uint16_t_le2host(bs->root_ent_max);
556
    rde = uint16_t_le2host(bs->root_ent_max);
509
 
557
 
510
    if (bps != BS_SIZE) {
558
    if (bps != BS_SIZE) {
511
        block_fini(dev_handle);
559
        block_fini(dev_handle);
512
        ipc_answer_0(rid, ENOTSUP);
560
        ipc_answer_0(rid, ENOTSUP);
513
        return;
561
        return;
514
    }
562
    }
515
 
563
 
516
    /* Initialize the block cache */
564
    /* Initialize the block cache */
517
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
565
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
518
    if (rc != EOK) {
566
    if (rc != EOK) {
519
        block_fini(dev_handle);
567
        block_fini(dev_handle);
520
        ipc_answer_0(rid, rc);
568
        ipc_answer_0(rid, rc);
521
        return;
569
        return;
522
    }
570
    }
523
 
571
 
524
    rc = fat_idx_init_by_dev_handle(dev_handle);
572
    rc = fat_idx_init_by_dev_handle(dev_handle);
525
    if (rc != EOK) {
573
    if (rc != EOK) {
526
        block_fini(dev_handle);
574
        block_fini(dev_handle);
527
        ipc_answer_0(rid, rc);
575
        ipc_answer_0(rid, rc);
528
        return;
576
        return;
529
    }
577
    }
530
 
578
 
531
    /* Initialize the root node. */
579
    /* Initialize the root node. */
532
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
580
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
533
    if (!rootp) {
581
    if (!rootp) {
534
        block_fini(dev_handle);
582
        block_fini(dev_handle);
535
        fat_idx_fini_by_dev_handle(dev_handle);
583
        fat_idx_fini_by_dev_handle(dev_handle);
536
        ipc_answer_0(rid, ENOMEM);
584
        ipc_answer_0(rid, ENOMEM);
537
        return;
585
        return;
538
    }
586
    }
539
    fat_node_initialize(rootp);
587
    fat_node_initialize(rootp);
540
 
588
 
541
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
589
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
542
    if (!ridxp) {
590
    if (!ridxp) {
543
        block_fini(dev_handle);
591
        block_fini(dev_handle);
544
        free(rootp);
592
        free(rootp);
545
        fat_idx_fini_by_dev_handle(dev_handle);
593
        fat_idx_fini_by_dev_handle(dev_handle);
546
        ipc_answer_0(rid, ENOMEM);
594
        ipc_answer_0(rid, ENOMEM);
547
        return;
595
        return;
548
    }
596
    }
549
    assert(ridxp->index == 0);
597
    assert(ridxp->index == 0);
550
    /* ridxp->lock held */
598
    /* ridxp->lock held */
551
 
599
 
552
    rootp->type = FAT_DIRECTORY;
600
    rootp->type = FAT_DIRECTORY;
553
    rootp->firstc = FAT_CLST_ROOT;
601
    rootp->firstc = FAT_CLST_ROOT;
554
    rootp->refcnt = 1;
602
    rootp->refcnt = 1;
555
    rootp->lnkcnt = 0;  /* FS root is not linked */
603
    rootp->lnkcnt = 0;  /* FS root is not linked */
556
    rootp->size = rde * sizeof(fat_dentry_t);
604
    rootp->size = rde * sizeof(fat_dentry_t);
557
    rootp->idx = ridxp;
605
    rootp->idx = ridxp;
558
    ridxp->nodep = rootp;
606
    ridxp->nodep = rootp;
559
   
607
   
560
    futex_up(&ridxp->lock);
608
    futex_up(&ridxp->lock);
561
 
609
 
562
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
610
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
563
}
611
}
564
 
612
 
565
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
613
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
566
{
614
{
567
    ipc_answer_0(rid, ENOTSUP);
615
    ipc_answer_0(rid, ENOTSUP);
568
}
616
}
569
 
617
 
570
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
618
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
571
{
619
{
572
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
620
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
573
}
621
}
574
 
622
 
575
void fat_read(ipc_callid_t rid, ipc_call_t *request)
623
void fat_read(ipc_callid_t rid, ipc_call_t *request)
576
{
624
{
577
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
625
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
578
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
626
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
579
    off_t pos = (off_t)IPC_GET_ARG3(*request);
627
    off_t pos = (off_t)IPC_GET_ARG3(*request);
580
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
628
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
581
    fat_bs_t *bs;
629
    fat_bs_t *bs;
582
    uint16_t bps;
630
    uint16_t bps;
583
    size_t bytes;
631
    size_t bytes;
584
    block_t *b;
632
    block_t *b;
585
 
633
 
586
    if (!nodep) {
634
    if (!nodep) {
587
        ipc_answer_0(rid, ENOENT);
635
        ipc_answer_0(rid, ENOENT);
588
        return;
636
        return;
589
    }
637
    }
590
 
638
 
591
    ipc_callid_t callid;
639
    ipc_callid_t callid;
592
    size_t len;
640
    size_t len;
593
    if (!ipc_data_read_receive(&callid, &len)) {
641
    if (!ipc_data_read_receive(&callid, &len)) {
594
        fat_node_put(nodep);
642
        fat_node_put(nodep);
595
        ipc_answer_0(callid, EINVAL);
643
        ipc_answer_0(callid, EINVAL);
596
        ipc_answer_0(rid, EINVAL);
644
        ipc_answer_0(rid, EINVAL);
597
        return;
645
        return;
598
    }
646
    }
599
 
647
 
600
    bs = block_bb_get(dev_handle);
648
    bs = block_bb_get(dev_handle);
601
    bps = uint16_t_le2host(bs->bps);
649
    bps = uint16_t_le2host(bs->bps);
602
 
650
 
603
    if (nodep->type == FAT_FILE) {
651
    if (nodep->type == FAT_FILE) {
604
        /*
652
        /*
605
         * Our strategy for regular file reads is to read one block at
653
         * Our strategy for regular file reads is to read one block at
606
         * most and make use of the possibility to return less data than
654
         * most and make use of the possibility to return less data than
607
         * requested. This keeps the code very simple.
655
         * requested. This keeps the code very simple.
608
         */
656
         */
609
        if (pos >= nodep->size) {
657
        if (pos >= nodep->size) {
610
            /* reading beyond the EOF */
658
            /* reading beyond the EOF */
611
            bytes = 0;
659
            bytes = 0;
612
            (void) ipc_data_read_finalize(callid, NULL, 0);
660
            (void) ipc_data_read_finalize(callid, NULL, 0);
613
        } else {
661
        } else {
614
            bytes = min(len, bps - pos % bps);
662
            bytes = min(len, bps - pos % bps);
615
            bytes = min(bytes, nodep->size - pos);
663
            bytes = min(bytes, nodep->size - pos);
616
            b = fat_block_get(bs, nodep, pos / bps,
664
            b = fat_block_get(bs, nodep, pos / bps,
617
                BLOCK_FLAGS_NONE);
665
                BLOCK_FLAGS_NONE);
618
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
666
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
619
                bytes);
667
                bytes);
620
            block_put(b);
668
            block_put(b);
621
        }
669
        }
622
    } else {
670
    } else {
623
        unsigned bnum;
671
        unsigned bnum;
624
        off_t spos = pos;
672
        off_t spos = pos;
625
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
673
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
626
        fat_dentry_t *d;
674
        fat_dentry_t *d;
627
 
675
 
628
        assert(nodep->type == FAT_DIRECTORY);
676
        assert(nodep->type == FAT_DIRECTORY);
629
        assert(nodep->size % bps == 0);
677
        assert(nodep->size % bps == 0);
630
        assert(bps % sizeof(fat_dentry_t) == 0);
678
        assert(bps % sizeof(fat_dentry_t) == 0);
631
 
679
 
632
        /*
680
        /*
633
         * Our strategy for readdir() is to use the position pointer as
681
         * Our strategy for readdir() is to use the position pointer as
634
         * an index into the array of all dentries. On entry, it points
682
         * an index into the array of all dentries. On entry, it points
635
         * to the first unread dentry. If we skip any dentries, we bump
683
         * to the first unread dentry. If we skip any dentries, we bump
636
         * the position pointer accordingly.
684
         * the position pointer accordingly.
637
         */
685
         */
638
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
686
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
639
        while (bnum < nodep->size / bps) {
687
        while (bnum < nodep->size / bps) {
640
            off_t o;
688
            off_t o;
641
 
689
 
642
            b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
690
            b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
643
            for (o = pos % (bps / sizeof(fat_dentry_t));
691
            for (o = pos % (bps / sizeof(fat_dentry_t));
644
                o < bps / sizeof(fat_dentry_t);
692
                o < bps / sizeof(fat_dentry_t);
645
                o++, pos++) {
693
                o++, pos++) {
646
                d = ((fat_dentry_t *)b->data) + o;
694
                d = ((fat_dentry_t *)b->data) + o;
647
                switch (fat_classify_dentry(d)) {
695
                switch (fat_classify_dentry(d)) {
648
                case FAT_DENTRY_SKIP:
696
                case FAT_DENTRY_SKIP:
649
                    continue;
697
                    continue;
650
                case FAT_DENTRY_LAST:
698
                case FAT_DENTRY_LAST:
651
                    block_put(b);
699
                    block_put(b);
652
                    goto miss;
700
                    goto miss;
653
                default:
701
                default:
654
                case FAT_DENTRY_VALID:
702
                case FAT_DENTRY_VALID:
655
                    dentry_name_canonify(d, name);
703
                    dentry_name_canonify(d, name);
656
                    block_put(b);
704
                    block_put(b);
657
                    goto hit;
705
                    goto hit;
658
                }
706
                }
659
            }
707
            }
660
            block_put(b);
708
            block_put(b);
661
            bnum++;
709
            bnum++;
662
        }
710
        }
663
miss:
711
miss:
664
        fat_node_put(nodep);
712
        fat_node_put(nodep);
665
        ipc_answer_0(callid, ENOENT);
713
        ipc_answer_0(callid, ENOENT);
666
        ipc_answer_1(rid, ENOENT, 0);
714
        ipc_answer_1(rid, ENOENT, 0);
667
        return;
715
        return;
668
hit:
716
hit:
669
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
717
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
670
        bytes = (pos - spos) + 1;
718
        bytes = (pos - spos) + 1;
671
    }
719
    }
672
 
720
 
673
    fat_node_put(nodep);
721
    fat_node_put(nodep);
674
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
722
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
675
}
723
}
676
 
724
 
677
void fat_write(ipc_callid_t rid, ipc_call_t *request)
725
void fat_write(ipc_callid_t rid, ipc_call_t *request)
678
{
726
{
679
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
727
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
680
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
728
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
681
    off_t pos = (off_t)IPC_GET_ARG3(*request);
729
    off_t pos = (off_t)IPC_GET_ARG3(*request);
682
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
730
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
683
    fat_bs_t *bs;
731
    fat_bs_t *bs;
684
    size_t bytes;
732
    size_t bytes;
685
    block_t *b;
733
    block_t *b;
686
    uint16_t bps;
734
    uint16_t bps;
687
    unsigned spc;
735
    unsigned spc;
688
    unsigned bpc;       /* bytes per cluster */
736
    unsigned bpc;       /* bytes per cluster */
689
    off_t boundary;
737
    off_t boundary;
690
    int flags = BLOCK_FLAGS_NONE;
738
    int flags = BLOCK_FLAGS_NONE;
691
   
739
   
692
    if (!nodep) {
740
    if (!nodep) {
693
        ipc_answer_0(rid, ENOENT);
741
        ipc_answer_0(rid, ENOENT);
694
        return;
742
        return;
695
    }
743
    }
696
   
744
   
697
    ipc_callid_t callid;
745
    ipc_callid_t callid;
698
    size_t len;
746
    size_t len;
699
    if (!ipc_data_write_receive(&callid, &len)) {
747
    if (!ipc_data_write_receive(&callid, &len)) {
700
        fat_node_put(nodep);
748
        fat_node_put(nodep);
701
        ipc_answer_0(callid, EINVAL);
749
        ipc_answer_0(callid, EINVAL);
702
        ipc_answer_0(rid, EINVAL);
750
        ipc_answer_0(rid, EINVAL);
703
        return;
751
        return;
704
    }
752
    }
705
 
753
 
706
    bs = block_bb_get(dev_handle);
754
    bs = block_bb_get(dev_handle);
707
    bps = uint16_t_le2host(bs->bps);
755
    bps = uint16_t_le2host(bs->bps);
708
    spc = bs->spc;
756
    spc = bs->spc;
709
    bpc = bps * spc;
757
    bpc = bps * spc;
710
 
758
 
711
    /*
759
    /*
712
     * In all scenarios, we will attempt to write out only one block worth
760
     * In all scenarios, we will attempt to write out only one block worth
713
     * of data at maximum. There might be some more efficient approaches,
761
     * of data at maximum. There might be some more efficient approaches,
714
     * but this one greatly simplifies fat_write(). Note that we can afford
762
     * but this one greatly simplifies fat_write(). Note that we can afford
715
     * to do this because the client must be ready to handle the return
763
     * to do this because the client must be ready to handle the return
716
     * value signalizing a smaller number of bytes written.
764
     * value signalizing a smaller number of bytes written.
717
     */
765
     */
718
    bytes = min(len, bps - pos % bps);
766
    bytes = min(len, bps - pos % bps);
719
    if (bytes == bps)
767
    if (bytes == bps)
720
        flags |= BLOCK_FLAGS_NOREAD;
768
        flags |= BLOCK_FLAGS_NOREAD;
721
   
769
   
722
    boundary = ROUND_UP(nodep->size, bpc);
770
    boundary = ROUND_UP(nodep->size, bpc);
723
    if (pos < boundary) {
771
    if (pos < boundary) {
724
        /*
772
        /*
725
         * This is the easier case - we are either overwriting already
773
         * This is the easier case - we are either overwriting already
726
         * existing contents or writing behind the EOF, but still within
774
         * existing contents or writing behind the EOF, but still within
727
         * the limits of the last cluster. The node size may grow to the
775
         * the limits of the last cluster. The node size may grow to the
728
         * next block size boundary.
776
         * next block size boundary.
729
         */
777
         */
730
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
778
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
731
        b = fat_block_get(bs, nodep, pos / bps, flags);
779
        b = fat_block_get(bs, nodep, pos / bps, flags);
732
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
780
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
733
            bytes);
781
            bytes);
734
        b->dirty = true;        /* need to sync block */
782
        b->dirty = true;        /* need to sync block */
735
        block_put(b);
783
        block_put(b);
736
        if (pos + bytes > nodep->size) {
784
        if (pos + bytes > nodep->size) {
737
            nodep->size = pos + bytes;
785
            nodep->size = pos + bytes;
738
            nodep->dirty = true;    /* need to sync node */
786
            nodep->dirty = true;    /* need to sync node */
739
        }
787
        }
740
        ipc_answer_2(rid, EOK, bytes, nodep->size);
788
        ipc_answer_2(rid, EOK, bytes, nodep->size);
741
        fat_node_put(nodep);
789
        fat_node_put(nodep);
742
        return;
790
        return;
743
    } else {
791
    } else {
744
        /*
792
        /*
745
         * This is the more difficult case. We must allocate new
793
         * This is the more difficult case. We must allocate new
746
         * clusters for the node and zero them out.
794
         * clusters for the node and zero them out.
747
         */
795
         */
748
        int status;
796
        int status;
749
        unsigned nclsts;
797
        unsigned nclsts;
750
        fat_cluster_t mcl, lcl;
798
        fat_cluster_t mcl, lcl;
751
 
799
 
752
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
800
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
753
        /* create an independent chain of nclsts clusters in all FATs */
801
        /* create an independent chain of nclsts clusters in all FATs */
754
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
802
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
755
        if (status != EOK) {
803
        if (status != EOK) {
756
            /* could not allocate a chain of nclsts clusters */
804
            /* could not allocate a chain of nclsts clusters */
757
            fat_node_put(nodep);
805
            fat_node_put(nodep);
758
            ipc_answer_0(callid, status);
806
            ipc_answer_0(callid, status);
759
            ipc_answer_0(rid, status);
807
            ipc_answer_0(rid, status);
760
            return;
808
            return;
761
        }
809
        }
762
        /* zero fill any gaps */
810
        /* zero fill any gaps */
763
        fat_fill_gap(bs, nodep, mcl, pos);
811
        fat_fill_gap(bs, nodep, mcl, pos);
764
        b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
812
        b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
765
            flags);
813
            flags);
766
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
814
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
767
            bytes);
815
            bytes);
768
        b->dirty = true;        /* need to sync block */
816
        b->dirty = true;        /* need to sync block */
769
        block_put(b);
817
        block_put(b);
770
        /*
818
        /*
771
         * Append the cluster chain starting in mcl to the end of the
819
         * Append the cluster chain starting in mcl to the end of the
772
         * node's cluster chain.
820
         * node's cluster chain.
773
         */
821
         */
774
        fat_append_clusters(bs, nodep, mcl);
822
        fat_append_clusters(bs, nodep, mcl);
775
        nodep->size = pos + bytes;
823
        nodep->size = pos + bytes;
776
        nodep->dirty = true;        /* need to sync node */
824
        nodep->dirty = true;        /* need to sync node */
777
        ipc_answer_2(rid, EOK, bytes, nodep->size);
825
        ipc_answer_2(rid, EOK, bytes, nodep->size);
778
        fat_node_put(nodep);
826
        fat_node_put(nodep);
779
        return;
827
        return;
780
    }
828
    }
781
}
829
}
782
 
830
 
783
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
831
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
784
{
832
{
785
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
833
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
786
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
834
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
787
    size_t size = (off_t)IPC_GET_ARG3(*request);
835
    size_t size = (off_t)IPC_GET_ARG3(*request);
788
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
836
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
789
    fat_bs_t *bs;
837
    fat_bs_t *bs;
790
    uint16_t bps;
838
    uint16_t bps;
791
    uint8_t spc;
839
    uint8_t spc;
792
    unsigned bpc;   /* bytes per cluster */
840
    unsigned bpc;   /* bytes per cluster */
793
    int rc;
841
    int rc;
794
 
842
 
795
    if (!nodep) {
843
    if (!nodep) {
796
        ipc_answer_0(rid, ENOENT);
844
        ipc_answer_0(rid, ENOENT);
797
        return;
845
        return;
798
    }
846
    }
799
 
847
 
800
    bs = block_bb_get(dev_handle);
848
    bs = block_bb_get(dev_handle);
801
    bps = uint16_t_le2host(bs->bps);
849
    bps = uint16_t_le2host(bs->bps);
802
    spc = bs->spc;
850
    spc = bs->spc;
803
    bpc = bps * spc;
851
    bpc = bps * spc;
804
 
852
 
805
    if (nodep->size == size) {
853
    if (nodep->size == size) {
806
        rc = EOK;
854
        rc = EOK;
807
    } else if (nodep->size < size) {
855
    } else if (nodep->size < size) {
808
        /*
856
        /*
809
         * The standard says we have the freedom to grow the node.
857
         * The standard says we have the freedom to grow the node.
810
         * For now, we simply return an error.
858
         * For now, we simply return an error.
811
         */
859
         */
812
        rc = EINVAL;
860
        rc = EINVAL;
813
    } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
861
    } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
814
        /*
862
        /*
815
         * The node will be shrunk, but no clusters will be deallocated.
863
         * The node will be shrunk, but no clusters will be deallocated.
816
         */
864
         */
817
        nodep->size = size;
865
        nodep->size = size;
818
        nodep->dirty = true;        /* need to sync node */
866
        nodep->dirty = true;        /* need to sync node */
819
        rc = EOK;  
867
        rc = EOK;  
820
    } else {
868
    } else {
821
        /*
869
        /*
822
         * The node will be shrunk, clusters will be deallocated.
870
         * The node will be shrunk, clusters will be deallocated.
823
         */
871
         */
824
        if (size == 0) {
872
        if (size == 0) {
825
            fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
873
            fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
826
        } else {
874
        } else {
827
            fat_cluster_t lastc;
875
            fat_cluster_t lastc;
828
            (void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
876
            (void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
829
                &lastc, (size - 1) / bpc);
877
                &lastc, (size - 1) / bpc);
830
            fat_chop_clusters(bs, nodep, lastc);
878
            fat_chop_clusters(bs, nodep, lastc);
831
        }
879
        }
832
        nodep->size = size;
880
        nodep->size = size;
833
        nodep->dirty = true;        /* need to sync node */
881
        nodep->dirty = true;        /* need to sync node */
834
        rc = EOK;  
882
        rc = EOK;  
835
    }
883
    }
836
    fat_node_put(nodep);
884
    fat_node_put(nodep);
837
    ipc_answer_0(rid, rc);
885
    ipc_answer_0(rid, rc);
838
    return;
886
    return;
839
}
887
}
-
 
888
 
-
 
889
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
-
 
890
{
-
 
891
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
-
 
892
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
-
 
893
    int rc;
-
 
894
 
-
 
895
    fat_node_t *nodep = fat_node_get(dev_handle, index);
-
 
896
    if (!nodep) {
-
 
897
        ipc_answer_0(rid, ENOENT);
-
 
898
        return;
-
 
899
    }
-
 
900
 
-
 
901
    rc = fat_destroy_node(nodep);
-
 
902
    ipc_answer_0(rid, rc);
-
 
903
}
840
 
904
 
841
/**
905
/**
842
 * @}
906
 * @}
843
 */
907
 */
844
 
908