Subversion Repositories HelenOS

Rev

Rev 3625 | Rev 3629 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3625 Rev 3628
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
/*
219
/*
220
 * Forward declarations of FAT libfs operations.
220
 * Forward declarations of FAT libfs operations.
221
 */
221
 */
222
static void *fat_node_get(dev_handle_t, fs_index_t);
222
static void *fat_node_get(dev_handle_t, fs_index_t);
223
static void fat_node_put(void *);
223
static void fat_node_put(void *);
224
static void *fat_create_node(dev_handle_t, int);
224
static void *fat_create_node(dev_handle_t, int);
225
static int fat_destroy_node(void *);
225
static int fat_destroy_node(void *);
226
static int fat_link(void *, void *, const char *);
226
static int fat_link(void *, void *, const char *);
227
static int fat_unlink(void *, void *);
227
static int fat_unlink(void *, void *);
228
static void *fat_match(void *, const char *);
228
static void *fat_match(void *, const char *);
229
static fs_index_t fat_index_get(void *);
229
static fs_index_t fat_index_get(void *);
230
static size_t fat_size_get(void *);
230
static size_t fat_size_get(void *);
231
static unsigned fat_lnkcnt_get(void *);
231
static unsigned fat_lnkcnt_get(void *);
232
static bool fat_has_children(void *);
232
static bool fat_has_children(void *);
233
static void *fat_root_get(dev_handle_t);
233
static void *fat_root_get(dev_handle_t);
234
static char fat_plb_get_char(unsigned);
234
static char fat_plb_get_char(unsigned);
235
static bool fat_is_directory(void *);
235
static bool fat_is_directory(void *);
236
static bool fat_is_file(void *node);
236
static bool fat_is_file(void *node);
237
 
237
 
238
/*
238
/*
239
 * FAT libfs operations.
239
 * FAT libfs operations.
240
 */
240
 */
241
 
241
 
242
/** Instantiate a FAT in-core node. */
242
/** Instantiate a FAT in-core node. */
243
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)
244
{
244
{
245
    void *node;
245
    void *node;
246
    fat_idx_t *idxp;
246
    fat_idx_t *idxp;
247
 
247
 
248
    idxp = fat_idx_get_by_index(dev_handle, index);
248
    idxp = fat_idx_get_by_index(dev_handle, index);
249
    if (!idxp)
249
    if (!idxp)
250
        return NULL;
250
        return NULL;
251
    /* idxp->lock held */
251
    /* idxp->lock held */
252
    node = fat_node_get_core(idxp);
252
    node = fat_node_get_core(idxp);
253
    futex_up(&idxp->lock);
253
    futex_up(&idxp->lock);
254
    return node;
254
    return node;
255
}
255
}
256
 
256
 
257
void fat_node_put(void *node)
257
void fat_node_put(void *node)
258
{
258
{
259
    fat_node_t *nodep = (fat_node_t *)node;
259
    fat_node_t *nodep = (fat_node_t *)node;
260
    bool destroy = false;
260
    bool destroy = false;
261
 
261
 
262
    futex_down(&nodep->lock);
262
    futex_down(&nodep->lock);
263
    if (!--nodep->refcnt) {
263
    if (!--nodep->refcnt) {
264
        if (nodep->idx) {
264
        if (nodep->idx) {
265
            futex_down(&ffn_futex);
265
            futex_down(&ffn_futex);
266
            list_append(&nodep->ffn_link, &ffn_head);
266
            list_append(&nodep->ffn_link, &ffn_head);
267
            futex_up(&ffn_futex);
267
            futex_up(&ffn_futex);
268
        } else {
268
        } else {
269
            /*
269
            /*
270
             * The node does not have any index structure associated
270
             * The node does not have any index structure associated
271
             * with itself. This can only mean that we are releasing
271
             * with itself. This can only mean that we are releasing
272
             * the node after a failed attempt to allocate the index
272
             * the node after a failed attempt to allocate the index
273
             * structure for it.
273
             * structure for it.
274
             */
274
             */
275
            destroy = true;
275
            destroy = true;
276
        }
276
        }
277
    }
277
    }
278
    futex_up(&nodep->lock);
278
    futex_up(&nodep->lock);
279
    if (destroy)
279
    if (destroy)
280
        free(node);
280
        free(node);
281
}
281
}
282
 
282
 
283
void *fat_create_node(dev_handle_t dev_handle, int flags)
283
void *fat_create_node(dev_handle_t dev_handle, int flags)
284
{
284
{
285
    fat_idx_t *idxp;
285
    fat_idx_t *idxp;
286
    fat_node_t *nodep;
286
    fat_node_t *nodep;
287
 
287
 
288
    nodep = fat_node_get_new();
288
    nodep = fat_node_get_new();
289
    if (!nodep)
289
    if (!nodep)
290
        return NULL;
290
        return NULL;
291
    idxp = fat_idx_get_new(dev_handle);
291
    idxp = fat_idx_get_new(dev_handle);
292
    if (!idxp) {
292
    if (!idxp) {
293
        fat_node_put(nodep);
293
        fat_node_put(nodep);
294
        return NULL;
294
        return NULL;
295
    }
295
    }
296
    /* idxp->lock held */
296
    /* idxp->lock held */
297
    if (flags & L_DIRECTORY) {
297
    if (flags & L_DIRECTORY) {
298
        nodep->type = FAT_DIRECTORY;
298
        nodep->type = FAT_DIRECTORY;
299
    } else {
299
    } else {
300
        nodep->type = FAT_FILE;
300
        nodep->type = FAT_FILE;
301
    }
301
    }
302
    nodep->size = 0;
302
    nodep->size = 0;
303
    nodep->firstc = FAT_CLST_RES0;
303
    nodep->firstc = FAT_CLST_RES0;
304
    nodep->lnkcnt = 0;  /* not linked anywhere */
304
    nodep->lnkcnt = 0;  /* not linked anywhere */
305
    nodep->refcnt = 1;
305
    nodep->refcnt = 1;
306
 
306
 
307
    nodep->idx = idxp;
307
    nodep->idx = idxp;
308
    idxp->nodep = nodep;
308
    idxp->nodep = nodep;
309
 
309
 
310
    futex_up(&idxp->lock);
310
    futex_up(&idxp->lock);
311
    return nodep;
311
    return nodep;
312
}
312
}
313
 
313
 
314
int fat_destroy_node(void *node)
314
int fat_destroy_node(void *node)
315
{
315
{
316
    fat_node_t *nodep = (fat_node_t *)node;
316
    fat_node_t *nodep = (fat_node_t *)node;
317
    fat_bs_t *bs;
317
    fat_bs_t *bs;
318
 
318
 
319
    /*
319
    /*
320
     * The node is not reachable from the file system. This means that the
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
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
322
     * found in the position hash. Obviously, we don't need to lock the node
323
     * nor its index structure.
323
     * nor its index structure.
324
     */
324
     */
325
    assert(nodep->lnkcnt == 0);
325
    assert(nodep->lnkcnt == 0);
326
 
326
 
327
    /*
327
    /*
328
     * The node may not have any children.
328
     * The node may not have any children.
329
     */
329
     */
330
    assert(fat_has_children(node) == false);
330
    assert(fat_has_children(node) == false);
331
 
331
 
332
    bs = block_bb_get(nodep->idx->dev_handle);
332
    bs = block_bb_get(nodep->idx->dev_handle);
333
    if (nodep->firstc != FAT_CLST_RES0) {
333
    if (nodep->firstc != FAT_CLST_RES0) {
334
        assert(nodep->size);
334
        assert(nodep->size);
335
        /* Free all clusters allocated to the node. */
335
        /* Free all clusters allocated to the node. */
336
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
336
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
337
    }
337
    }
338
 
338
 
339
    fat_idx_destroy(nodep->idx);
339
    fat_idx_destroy(nodep->idx);
340
    free(nodep);
340
    free(nodep);
341
    return EOK;
341
    return EOK;
342
}
342
}
343
 
343
 
344
int fat_link(void *prnt, void *chld, const char *name)
344
int fat_link(void *prnt, void *chld, const char *name)
345
{
345
{
-
 
346
    fat_node_t *parentp = (fat_node_t *)prnt;
-
 
347
    fat_node_t *childp = (fat_node_t *)chld;
-
 
348
    fat_dentry_t *d;
-
 
349
    fat_bs_t *bs;
-
 
350
    block_t *b;
-
 
351
    int i, j;
-
 
352
    uint16_t bps;
-
 
353
    unsigned dps;
-
 
354
    unsigned blocks;
-
 
355
 
-
 
356
    futex_down(&childp->lock);
-
 
357
    if (childp->lnkcnt == 1) {
-
 
358
        /*
346
    return ENOTSUP; /* not supported at the moment */
359
         * On FAT, we don't support multiple hard links.
-
 
360
         */
-
 
361
        futex_up(&childp->lock);
-
 
362
        return EMLINK;
-
 
363
    }
-
 
364
    assert(childp->lnkcnt == 0);
-
 
365
    futex_up(&childp->lock);
-
 
366
 
-
 
367
    if (!fat_dentry_name_verify(name)) {
-
 
368
        /*
-
 
369
         * Attempt to create unsupported name.
-
 
370
         */
-
 
371
        return ENOTSUP;
-
 
372
    }
-
 
373
 
-
 
374
    /*
-
 
375
     * Get us an unused parent node's dentry or grow the parent and allocate
-
 
376
     * a new one.
-
 
377
     */
-
 
378
   
-
 
379
    futex_down(&parentp->idx->lock);
-
 
380
    bs = block_bb_get(parentp->idx->dev_handle);
-
 
381
    bps = uint16_t_le2host(bs->bps);
-
 
382
    dps = bps / sizeof(fat_dentry_t);
-
 
383
 
-
 
384
    blocks = parentp->size / bps;
-
 
385
 
-
 
386
    for (i = 0; i < blocks; i++) {
-
 
387
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
-
 
388
        for (j = 0; j < dps; j++) {
-
 
389
            d = ((fat_dentry_t *)b->data) + j;
-
 
390
            switch (fat_classify_dentry(d)) {
-
 
391
            case FAT_DENTRY_SKIP:
-
 
392
            case FAT_DENTRY_VALID:
-
 
393
                /* skipping used and meta entries */
-
 
394
                continue;
-
 
395
            case FAT_DENTRY_FREE:
-
 
396
            case FAT_DENTRY_LAST:
-
 
397
                /* found an empty slot */
-
 
398
                goto hit;
-
 
399
            }
-
 
400
        }
-
 
401
        block_put(b);
-
 
402
    }
-
 
403
   
-
 
404
    /*
-
 
405
     * We need to grow the parent in order to create a new unused dentry.
-
 
406
     */
-
 
407
    futex_up(&parentp->idx->lock);
-
 
408
    return ENOTSUP; /* XXX */
-
 
409
 
-
 
410
hit:
-
 
411
    /*
-
 
412
     * At this point we only establish the link between the parent and the
-
 
413
     * child.  The dentry, except of the name and the extension, will remain
-
 
414
     * uninitialized until the the corresponding node is synced. Thus the
-
 
415
     * valid dentry data is kept in the child node structure.
-
 
416
     */
-
 
417
    memset(d, 0, sizeof(fat_dentry_t));
-
 
418
    fat_dentry_name_set(d, name);
-
 
419
    b->dirty = true;        /* need to sync block */
-
 
420
    block_put(b);
-
 
421
    futex_up(&parentp->idx->lock);
-
 
422
 
-
 
423
    futex_down(&childp->idx->lock);
-
 
424
    childp->idx->pfc = parentp->firstc;
-
 
425
    childp->idx->pdi = i * dps + j;
-
 
426
    futex_up(&childp->idx->lock);
-
 
427
 
-
 
428
    futex_down(&childp->lock);
-
 
429
    childp->lnkcnt = 1;
-
 
430
    childp->dirty = true;       /* need to sync node */
-
 
431
    futex_up(&childp->lock);
-
 
432
 
-
 
433
    /*
-
 
434
     * Hash in the index structure into the position hash.
-
 
435
     */
-
 
436
    fat_idx_hashin(childp->idx);
-
 
437
 
-
 
438
    return EOK;
347
}
439
}
348
 
440
 
349
int fat_unlink(void *prnt, void *chld)
441
int fat_unlink(void *prnt, void *chld)
350
{
442
{
351
    return ENOTSUP; /* not supported at the moment */
443
    return ENOTSUP; /* not supported at the moment */
352
}
444
}
353
 
445
 
354
void *fat_match(void *prnt, const char *component)
446
void *fat_match(void *prnt, const char *component)
355
{
447
{
356
    fat_bs_t *bs;
448
    fat_bs_t *bs;
357
    fat_node_t *parentp = (fat_node_t *)prnt;
449
    fat_node_t *parentp = (fat_node_t *)prnt;
358
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
450
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
359
    unsigned i, j;
451
    unsigned i, j;
360
    unsigned bps;       /* bytes per sector */
452
    unsigned bps;       /* bytes per sector */
361
    unsigned dps;       /* dentries per sector */
453
    unsigned dps;       /* dentries per sector */
362
    unsigned blocks;
454
    unsigned blocks;
363
    fat_dentry_t *d;
455
    fat_dentry_t *d;
364
    block_t *b;
456
    block_t *b;
365
 
457
 
366
    futex_down(&parentp->idx->lock);
458
    futex_down(&parentp->idx->lock);
367
    bs = block_bb_get(parentp->idx->dev_handle);
459
    bs = block_bb_get(parentp->idx->dev_handle);
368
    bps = uint16_t_le2host(bs->bps);
460
    bps = uint16_t_le2host(bs->bps);
369
    dps = bps / sizeof(fat_dentry_t);
461
    dps = bps / sizeof(fat_dentry_t);
370
    blocks = parentp->size / bps;
462
    blocks = parentp->size / bps;
371
    for (i = 0; i < blocks; i++) {
463
    for (i = 0; i < blocks; i++) {
372
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
464
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
373
        for (j = 0; j < dps; j++) {
465
        for (j = 0; j < dps; j++) {
374
            d = ((fat_dentry_t *)b->data) + j;
466
            d = ((fat_dentry_t *)b->data) + j;
375
            switch (fat_classify_dentry(d)) {
467
            switch (fat_classify_dentry(d)) {
376
            case FAT_DENTRY_SKIP:
468
            case FAT_DENTRY_SKIP:
-
 
469
            case FAT_DENTRY_FREE:
377
                continue;
470
                continue;
378
            case FAT_DENTRY_LAST:
471
            case FAT_DENTRY_LAST:
379
                block_put(b);
472
                block_put(b);
380
                futex_up(&parentp->idx->lock);
473
                futex_up(&parentp->idx->lock);
381
                return NULL;
474
                return NULL;
382
            default:
475
            default:
383
            case FAT_DENTRY_VALID:
476
            case FAT_DENTRY_VALID:
384
                dentry_name_canonify(d, name);
477
                fat_dentry_name_get(d, name);
385
                break;
478
                break;
386
            }
479
            }
387
            if (stricmp(name, component) == 0) {
480
            if (stricmp(name, component) == 0) {
388
                /* hit */
481
                /* hit */
389
                void *node;
482
                void *node;
390
                /*
483
                /*
391
                 * Assume tree hierarchy for locking.  We
484
                 * Assume tree hierarchy for locking.  We
392
                 * already have the parent and now we are going
485
                 * already have the parent and now we are going
393
                 * to lock the child.  Never lock in the oposite
486
                 * to lock the child.  Never lock in the oposite
394
                 * order.
487
                 * order.
395
                 */
488
                 */
396
                fat_idx_t *idx = fat_idx_get_by_pos(
489
                fat_idx_t *idx = fat_idx_get_by_pos(
397
                    parentp->idx->dev_handle, parentp->firstc,
490
                    parentp->idx->dev_handle, parentp->firstc,
398
                    i * dps + j);
491
                    i * dps + j);
399
                futex_up(&parentp->idx->lock);
492
                futex_up(&parentp->idx->lock);
400
                if (!idx) {
493
                if (!idx) {
401
                    /*
494
                    /*
402
                     * Can happen if memory is low or if we
495
                     * Can happen if memory is low or if we
403
                     * run out of 32-bit indices.
496
                     * run out of 32-bit indices.
404
                     */
497
                     */
405
                    block_put(b);
498
                    block_put(b);
406
                    return NULL;
499
                    return NULL;
407
                }
500
                }
408
                node = fat_node_get_core(idx);
501
                node = fat_node_get_core(idx);
409
                futex_up(&idx->lock);
502
                futex_up(&idx->lock);
410
                block_put(b);
503
                block_put(b);
411
                return node;
504
                return node;
412
            }
505
            }
413
        }
506
        }
414
        block_put(b);
507
        block_put(b);
415
    }
508
    }
416
 
509
 
417
    futex_up(&parentp->idx->lock);
510
    futex_up(&parentp->idx->lock);
418
    return NULL;
511
    return NULL;
419
}
512
}
420
 
513
 
421
fs_index_t fat_index_get(void *node)
514
fs_index_t fat_index_get(void *node)
422
{
515
{
423
    fat_node_t *fnodep = (fat_node_t *)node;
516
    fat_node_t *fnodep = (fat_node_t *)node;
424
    if (!fnodep)
517
    if (!fnodep)
425
        return 0;
518
        return 0;
426
    return fnodep->idx->index;
519
    return fnodep->idx->index;
427
}
520
}
428
 
521
 
429
size_t fat_size_get(void *node)
522
size_t fat_size_get(void *node)
430
{
523
{
431
    return ((fat_node_t *)node)->size;
524
    return ((fat_node_t *)node)->size;
432
}
525
}
433
 
526
 
434
unsigned fat_lnkcnt_get(void *node)
527
unsigned fat_lnkcnt_get(void *node)
435
{
528
{
436
    return ((fat_node_t *)node)->lnkcnt;
529
    return ((fat_node_t *)node)->lnkcnt;
437
}
530
}
438
 
531
 
439
bool fat_has_children(void *node)
532
bool fat_has_children(void *node)
440
{
533
{
441
    fat_bs_t *bs;
534
    fat_bs_t *bs;
442
    fat_node_t *nodep = (fat_node_t *)node;
535
    fat_node_t *nodep = (fat_node_t *)node;
443
    unsigned bps;
536
    unsigned bps;
444
    unsigned dps;
537
    unsigned dps;
445
    unsigned blocks;
538
    unsigned blocks;
446
    block_t *b;
539
    block_t *b;
447
    unsigned i, j;
540
    unsigned i, j;
448
 
541
 
449
    if (nodep->type != FAT_DIRECTORY)
542
    if (nodep->type != FAT_DIRECTORY)
450
        return false;
543
        return false;
451
   
544
   
452
    futex_down(&nodep->idx->lock);
545
    futex_down(&nodep->idx->lock);
453
    bs = block_bb_get(nodep->idx->dev_handle);
546
    bs = block_bb_get(nodep->idx->dev_handle);
454
    bps = uint16_t_le2host(bs->bps);
547
    bps = uint16_t_le2host(bs->bps);
455
    dps = bps / sizeof(fat_dentry_t);
548
    dps = bps / sizeof(fat_dentry_t);
456
 
549
 
457
    blocks = nodep->size / bps;
550
    blocks = nodep->size / bps;
458
 
551
 
459
    for (i = 0; i < blocks; i++) {
552
    for (i = 0; i < blocks; i++) {
460
        fat_dentry_t *d;
553
        fat_dentry_t *d;
461
   
554
   
462
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
555
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
463
        for (j = 0; j < dps; j++) {
556
        for (j = 0; j < dps; j++) {
464
            d = ((fat_dentry_t *)b->data) + j;
557
            d = ((fat_dentry_t *)b->data) + j;
465
            switch (fat_classify_dentry(d)) {
558
            switch (fat_classify_dentry(d)) {
466
            case FAT_DENTRY_SKIP:
559
            case FAT_DENTRY_SKIP:
-
 
560
            case FAT_DENTRY_FREE:
467
                continue;
561
                continue;
468
            case FAT_DENTRY_LAST:
562
            case FAT_DENTRY_LAST:
469
                block_put(b);
563
                block_put(b);
470
                futex_up(&nodep->idx->lock);
564
                futex_up(&nodep->idx->lock);
471
                return false;
565
                return false;
472
            default:
566
            default:
473
            case FAT_DENTRY_VALID:
567
            case FAT_DENTRY_VALID:
474
                block_put(b);
568
                block_put(b);
475
                futex_up(&nodep->idx->lock);
569
                futex_up(&nodep->idx->lock);
476
                return true;
570
                return true;
477
            }
571
            }
478
            block_put(b);
572
            block_put(b);
479
            futex_up(&nodep->idx->lock);
573
            futex_up(&nodep->idx->lock);
480
            return true;
574
            return true;
481
        }
575
        }
482
        block_put(b);
576
        block_put(b);
483
    }
577
    }
484
 
578
 
485
    futex_up(&nodep->idx->lock);
579
    futex_up(&nodep->idx->lock);
486
    return false;
580
    return false;
487
}
581
}
488
 
582
 
489
void *fat_root_get(dev_handle_t dev_handle)
583
void *fat_root_get(dev_handle_t dev_handle)
490
{
584
{
491
    return fat_node_get(dev_handle, 0);
585
    return fat_node_get(dev_handle, 0);
492
}
586
}
493
 
587
 
494
char fat_plb_get_char(unsigned pos)
588
char fat_plb_get_char(unsigned pos)
495
{
589
{
496
    return fat_reg.plb_ro[pos % PLB_SIZE];
590
    return fat_reg.plb_ro[pos % PLB_SIZE];
497
}
591
}
498
 
592
 
499
bool fat_is_directory(void *node)
593
bool fat_is_directory(void *node)
500
{
594
{
501
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
595
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
502
}
596
}
503
 
597
 
504
bool fat_is_file(void *node)
598
bool fat_is_file(void *node)
505
{
599
{
506
    return ((fat_node_t *)node)->type == FAT_FILE;
600
    return ((fat_node_t *)node)->type == FAT_FILE;
507
}
601
}
508
 
602
 
509
/** libfs operations */
603
/** libfs operations */
510
libfs_ops_t fat_libfs_ops = {
604
libfs_ops_t fat_libfs_ops = {
511
    .match = fat_match,
605
    .match = fat_match,
512
    .node_get = fat_node_get,
606
    .node_get = fat_node_get,
513
    .node_put = fat_node_put,
607
    .node_put = fat_node_put,
514
    .create = fat_create_node,
608
    .create = fat_create_node,
515
    .destroy = fat_destroy_node,
609
    .destroy = fat_destroy_node,
516
    .link = fat_link,
610
    .link = fat_link,
517
    .unlink = fat_unlink,
611
    .unlink = fat_unlink,
518
    .index_get = fat_index_get,
612
    .index_get = fat_index_get,
519
    .size_get = fat_size_get,
613
    .size_get = fat_size_get,
520
    .lnkcnt_get = fat_lnkcnt_get,
614
    .lnkcnt_get = fat_lnkcnt_get,
521
    .has_children = fat_has_children,
615
    .has_children = fat_has_children,
522
    .root_get = fat_root_get,
616
    .root_get = fat_root_get,
523
    .plb_get_char = fat_plb_get_char,
617
    .plb_get_char = fat_plb_get_char,
524
    .is_directory = fat_is_directory,
618
    .is_directory = fat_is_directory,
525
    .is_file = fat_is_file
619
    .is_file = fat_is_file
526
};
620
};
527
 
621
 
528
/*
622
/*
529
 * VFS operations.
623
 * VFS operations.
530
 */
624
 */
531
 
625
 
532
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
626
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
533
{
627
{
534
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
628
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
535
    fat_bs_t *bs;
629
    fat_bs_t *bs;
536
    uint16_t bps;
630
    uint16_t bps;
537
    uint16_t rde;
631
    uint16_t rde;
538
    int rc;
632
    int rc;
539
 
633
 
540
    /* initialize libblock */
634
    /* initialize libblock */
541
    rc = block_init(dev_handle, BS_SIZE);
635
    rc = block_init(dev_handle, BS_SIZE);
542
    if (rc != EOK) {
636
    if (rc != EOK) {
543
        ipc_answer_0(rid, rc);
637
        ipc_answer_0(rid, rc);
544
        return;
638
        return;
545
    }
639
    }
546
 
640
 
547
    /* prepare the boot block */
641
    /* prepare the boot block */
548
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
642
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
549
    if (rc != EOK) {
643
    if (rc != EOK) {
550
        block_fini(dev_handle);
644
        block_fini(dev_handle);
551
        ipc_answer_0(rid, rc);
645
        ipc_answer_0(rid, rc);
552
        return;
646
        return;
553
    }
647
    }
554
 
648
 
555
    /* get the buffer with the boot sector */
649
    /* get the buffer with the boot sector */
556
    bs = block_bb_get(dev_handle);
650
    bs = block_bb_get(dev_handle);
557
   
651
   
558
    /* Read the number of root directory entries. */
652
    /* Read the number of root directory entries. */
559
    bps = uint16_t_le2host(bs->bps);
653
    bps = uint16_t_le2host(bs->bps);
560
    rde = uint16_t_le2host(bs->root_ent_max);
654
    rde = uint16_t_le2host(bs->root_ent_max);
561
 
655
 
562
    if (bps != BS_SIZE) {
656
    if (bps != BS_SIZE) {
563
        block_fini(dev_handle);
657
        block_fini(dev_handle);
564
        ipc_answer_0(rid, ENOTSUP);
658
        ipc_answer_0(rid, ENOTSUP);
565
        return;
659
        return;
566
    }
660
    }
567
 
661
 
568
    /* Initialize the block cache */
662
    /* Initialize the block cache */
569
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
663
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
570
    if (rc != EOK) {
664
    if (rc != EOK) {
571
        block_fini(dev_handle);
665
        block_fini(dev_handle);
572
        ipc_answer_0(rid, rc);
666
        ipc_answer_0(rid, rc);
573
        return;
667
        return;
574
    }
668
    }
575
 
669
 
576
    rc = fat_idx_init_by_dev_handle(dev_handle);
670
    rc = fat_idx_init_by_dev_handle(dev_handle);
577
    if (rc != EOK) {
671
    if (rc != EOK) {
578
        block_fini(dev_handle);
672
        block_fini(dev_handle);
579
        ipc_answer_0(rid, rc);
673
        ipc_answer_0(rid, rc);
580
        return;
674
        return;
581
    }
675
    }
582
 
676
 
583
    /* Initialize the root node. */
677
    /* Initialize the root node. */
584
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
678
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
585
    if (!rootp) {
679
    if (!rootp) {
586
        block_fini(dev_handle);
680
        block_fini(dev_handle);
587
        fat_idx_fini_by_dev_handle(dev_handle);
681
        fat_idx_fini_by_dev_handle(dev_handle);
588
        ipc_answer_0(rid, ENOMEM);
682
        ipc_answer_0(rid, ENOMEM);
589
        return;
683
        return;
590
    }
684
    }
591
    fat_node_initialize(rootp);
685
    fat_node_initialize(rootp);
592
 
686
 
593
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
687
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
594
    if (!ridxp) {
688
    if (!ridxp) {
595
        block_fini(dev_handle);
689
        block_fini(dev_handle);
596
        free(rootp);
690
        free(rootp);
597
        fat_idx_fini_by_dev_handle(dev_handle);
691
        fat_idx_fini_by_dev_handle(dev_handle);
598
        ipc_answer_0(rid, ENOMEM);
692
        ipc_answer_0(rid, ENOMEM);
599
        return;
693
        return;
600
    }
694
    }
601
    assert(ridxp->index == 0);
695
    assert(ridxp->index == 0);
602
    /* ridxp->lock held */
696
    /* ridxp->lock held */
603
 
697
 
604
    rootp->type = FAT_DIRECTORY;
698
    rootp->type = FAT_DIRECTORY;
605
    rootp->firstc = FAT_CLST_ROOT;
699
    rootp->firstc = FAT_CLST_ROOT;
606
    rootp->refcnt = 1;
700
    rootp->refcnt = 1;
607
    rootp->lnkcnt = 0;  /* FS root is not linked */
701
    rootp->lnkcnt = 0;  /* FS root is not linked */
608
    rootp->size = rde * sizeof(fat_dentry_t);
702
    rootp->size = rde * sizeof(fat_dentry_t);
609
    rootp->idx = ridxp;
703
    rootp->idx = ridxp;
610
    ridxp->nodep = rootp;
704
    ridxp->nodep = rootp;
611
   
705
   
612
    futex_up(&ridxp->lock);
706
    futex_up(&ridxp->lock);
613
 
707
 
614
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
708
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
615
}
709
}
616
 
710
 
617
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
711
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
618
{
712
{
619
    ipc_answer_0(rid, ENOTSUP);
713
    ipc_answer_0(rid, ENOTSUP);
620
}
714
}
621
 
715
 
622
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
716
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
623
{
717
{
624
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
718
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
625
}
719
}
626
 
720
 
627
void fat_read(ipc_callid_t rid, ipc_call_t *request)
721
void fat_read(ipc_callid_t rid, ipc_call_t *request)
628
{
722
{
629
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
723
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
630
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
724
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
631
    off_t pos = (off_t)IPC_GET_ARG3(*request);
725
    off_t pos = (off_t)IPC_GET_ARG3(*request);
632
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
726
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
633
    fat_bs_t *bs;
727
    fat_bs_t *bs;
634
    uint16_t bps;
728
    uint16_t bps;
635
    size_t bytes;
729
    size_t bytes;
636
    block_t *b;
730
    block_t *b;
637
 
731
 
638
    if (!nodep) {
732
    if (!nodep) {
639
        ipc_answer_0(rid, ENOENT);
733
        ipc_answer_0(rid, ENOENT);
640
        return;
734
        return;
641
    }
735
    }
642
 
736
 
643
    ipc_callid_t callid;
737
    ipc_callid_t callid;
644
    size_t len;
738
    size_t len;
645
    if (!ipc_data_read_receive(&callid, &len)) {
739
    if (!ipc_data_read_receive(&callid, &len)) {
646
        fat_node_put(nodep);
740
        fat_node_put(nodep);
647
        ipc_answer_0(callid, EINVAL);
741
        ipc_answer_0(callid, EINVAL);
648
        ipc_answer_0(rid, EINVAL);
742
        ipc_answer_0(rid, EINVAL);
649
        return;
743
        return;
650
    }
744
    }
651
 
745
 
652
    bs = block_bb_get(dev_handle);
746
    bs = block_bb_get(dev_handle);
653
    bps = uint16_t_le2host(bs->bps);
747
    bps = uint16_t_le2host(bs->bps);
654
 
748
 
655
    if (nodep->type == FAT_FILE) {
749
    if (nodep->type == FAT_FILE) {
656
        /*
750
        /*
657
         * Our strategy for regular file reads is to read one block at
751
         * Our strategy for regular file reads is to read one block at
658
         * most and make use of the possibility to return less data than
752
         * most and make use of the possibility to return less data than
659
         * requested. This keeps the code very simple.
753
         * requested. This keeps the code very simple.
660
         */
754
         */
661
        if (pos >= nodep->size) {
755
        if (pos >= nodep->size) {
662
            /* reading beyond the EOF */
756
            /* reading beyond the EOF */
663
            bytes = 0;
757
            bytes = 0;
664
            (void) ipc_data_read_finalize(callid, NULL, 0);
758
            (void) ipc_data_read_finalize(callid, NULL, 0);
665
        } else {
759
        } else {
666
            bytes = min(len, bps - pos % bps);
760
            bytes = min(len, bps - pos % bps);
667
            bytes = min(bytes, nodep->size - pos);
761
            bytes = min(bytes, nodep->size - pos);
668
            b = fat_block_get(bs, nodep, pos / bps,
762
            b = fat_block_get(bs, nodep, pos / bps,
669
                BLOCK_FLAGS_NONE);
763
                BLOCK_FLAGS_NONE);
670
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
764
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
671
                bytes);
765
                bytes);
672
            block_put(b);
766
            block_put(b);
673
        }
767
        }
674
    } else {
768
    } else {
675
        unsigned bnum;
769
        unsigned bnum;
676
        off_t spos = pos;
770
        off_t spos = pos;
677
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
771
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
678
        fat_dentry_t *d;
772
        fat_dentry_t *d;
679
 
773
 
680
        assert(nodep->type == FAT_DIRECTORY);
774
        assert(nodep->type == FAT_DIRECTORY);
681
        assert(nodep->size % bps == 0);
775
        assert(nodep->size % bps == 0);
682
        assert(bps % sizeof(fat_dentry_t) == 0);
776
        assert(bps % sizeof(fat_dentry_t) == 0);
683
 
777
 
684
        /*
778
        /*
685
         * Our strategy for readdir() is to use the position pointer as
779
         * Our strategy for readdir() is to use the position pointer as
686
         * an index into the array of all dentries. On entry, it points
780
         * an index into the array of all dentries. On entry, it points
687
         * to the first unread dentry. If we skip any dentries, we bump
781
         * to the first unread dentry. If we skip any dentries, we bump
688
         * the position pointer accordingly.
782
         * the position pointer accordingly.
689
         */
783
         */
690
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
784
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
691
        while (bnum < nodep->size / bps) {
785
        while (bnum < nodep->size / bps) {
692
            off_t o;
786
            off_t o;
693
 
787
 
694
            b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
788
            b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
695
            for (o = pos % (bps / sizeof(fat_dentry_t));
789
            for (o = pos % (bps / sizeof(fat_dentry_t));
696
                o < bps / sizeof(fat_dentry_t);
790
                o < bps / sizeof(fat_dentry_t);
697
                o++, pos++) {
791
                o++, pos++) {
698
                d = ((fat_dentry_t *)b->data) + o;
792
                d = ((fat_dentry_t *)b->data) + o;
699
                switch (fat_classify_dentry(d)) {
793
                switch (fat_classify_dentry(d)) {
700
                case FAT_DENTRY_SKIP:
794
                case FAT_DENTRY_SKIP:
-
 
795
                case FAT_DENTRY_FREE:
701
                    continue;
796
                    continue;
702
                case FAT_DENTRY_LAST:
797
                case FAT_DENTRY_LAST:
703
                    block_put(b);
798
                    block_put(b);
704
                    goto miss;
799
                    goto miss;
705
                default:
800
                default:
706
                case FAT_DENTRY_VALID:
801
                case FAT_DENTRY_VALID:
707
                    dentry_name_canonify(d, name);
802
                    fat_dentry_name_get(d, name);
708
                    block_put(b);
803
                    block_put(b);
709
                    goto hit;
804
                    goto hit;
710
                }
805
                }
711
            }
806
            }
712
            block_put(b);
807
            block_put(b);
713
            bnum++;
808
            bnum++;
714
        }
809
        }
715
miss:
810
miss:
716
        fat_node_put(nodep);
811
        fat_node_put(nodep);
717
        ipc_answer_0(callid, ENOENT);
812
        ipc_answer_0(callid, ENOENT);
718
        ipc_answer_1(rid, ENOENT, 0);
813
        ipc_answer_1(rid, ENOENT, 0);
719
        return;
814
        return;
720
hit:
815
hit:
721
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
816
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
722
        bytes = (pos - spos) + 1;
817
        bytes = (pos - spos) + 1;
723
    }
818
    }
724
 
819
 
725
    fat_node_put(nodep);
820
    fat_node_put(nodep);
726
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
821
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
727
}
822
}
728
 
823
 
729
void fat_write(ipc_callid_t rid, ipc_call_t *request)
824
void fat_write(ipc_callid_t rid, ipc_call_t *request)
730
{
825
{
731
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
826
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
732
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
827
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
733
    off_t pos = (off_t)IPC_GET_ARG3(*request);
828
    off_t pos = (off_t)IPC_GET_ARG3(*request);
734
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
829
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
735
    fat_bs_t *bs;
830
    fat_bs_t *bs;
736
    size_t bytes;
831
    size_t bytes;
737
    block_t *b;
832
    block_t *b;
738
    uint16_t bps;
833
    uint16_t bps;
739
    unsigned spc;
834
    unsigned spc;
740
    unsigned bpc;       /* bytes per cluster */
835
    unsigned bpc;       /* bytes per cluster */
741
    off_t boundary;
836
    off_t boundary;
742
    int flags = BLOCK_FLAGS_NONE;
837
    int flags = BLOCK_FLAGS_NONE;
743
   
838
   
744
    if (!nodep) {
839
    if (!nodep) {
745
        ipc_answer_0(rid, ENOENT);
840
        ipc_answer_0(rid, ENOENT);
746
        return;
841
        return;
747
    }
842
    }
748
   
843
   
749
    ipc_callid_t callid;
844
    ipc_callid_t callid;
750
    size_t len;
845
    size_t len;
751
    if (!ipc_data_write_receive(&callid, &len)) {
846
    if (!ipc_data_write_receive(&callid, &len)) {
752
        fat_node_put(nodep);
847
        fat_node_put(nodep);
753
        ipc_answer_0(callid, EINVAL);
848
        ipc_answer_0(callid, EINVAL);
754
        ipc_answer_0(rid, EINVAL);
849
        ipc_answer_0(rid, EINVAL);
755
        return;
850
        return;
756
    }
851
    }
757
 
852
 
758
    bs = block_bb_get(dev_handle);
853
    bs = block_bb_get(dev_handle);
759
    bps = uint16_t_le2host(bs->bps);
854
    bps = uint16_t_le2host(bs->bps);
760
    spc = bs->spc;
855
    spc = bs->spc;
761
    bpc = bps * spc;
856
    bpc = bps * spc;
762
 
857
 
763
    /*
858
    /*
764
     * In all scenarios, we will attempt to write out only one block worth
859
     * In all scenarios, we will attempt to write out only one block worth
765
     * of data at maximum. There might be some more efficient approaches,
860
     * of data at maximum. There might be some more efficient approaches,
766
     * but this one greatly simplifies fat_write(). Note that we can afford
861
     * but this one greatly simplifies fat_write(). Note that we can afford
767
     * to do this because the client must be ready to handle the return
862
     * to do this because the client must be ready to handle the return
768
     * value signalizing a smaller number of bytes written.
863
     * value signalizing a smaller number of bytes written.
769
     */
864
     */
770
    bytes = min(len, bps - pos % bps);
865
    bytes = min(len, bps - pos % bps);
771
    if (bytes == bps)
866
    if (bytes == bps)
772
        flags |= BLOCK_FLAGS_NOREAD;
867
        flags |= BLOCK_FLAGS_NOREAD;
773
   
868
   
774
    boundary = ROUND_UP(nodep->size, bpc);
869
    boundary = ROUND_UP(nodep->size, bpc);
775
    if (pos < boundary) {
870
    if (pos < boundary) {
776
        /*
871
        /*
777
         * This is the easier case - we are either overwriting already
872
         * This is the easier case - we are either overwriting already
778
         * existing contents or writing behind the EOF, but still within
873
         * existing contents or writing behind the EOF, but still within
779
         * the limits of the last cluster. The node size may grow to the
874
         * the limits of the last cluster. The node size may grow to the
780
         * next block size boundary.
875
         * next block size boundary.
781
         */
876
         */
782
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
877
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
783
        b = fat_block_get(bs, nodep, pos / bps, flags);
878
        b = fat_block_get(bs, nodep, pos / bps, flags);
784
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
879
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
785
            bytes);
880
            bytes);
786
        b->dirty = true;        /* need to sync block */
881
        b->dirty = true;        /* need to sync block */
787
        block_put(b);
882
        block_put(b);
788
        if (pos + bytes > nodep->size) {
883
        if (pos + bytes > nodep->size) {
789
            nodep->size = pos + bytes;
884
            nodep->size = pos + bytes;
790
            nodep->dirty = true;    /* need to sync node */
885
            nodep->dirty = true;    /* need to sync node */
791
        }
886
        }
792
        ipc_answer_2(rid, EOK, bytes, nodep->size);
887
        ipc_answer_2(rid, EOK, bytes, nodep->size);
793
        fat_node_put(nodep);
888
        fat_node_put(nodep);
794
        return;
889
        return;
795
    } else {
890
    } else {
796
        /*
891
        /*
797
         * This is the more difficult case. We must allocate new
892
         * This is the more difficult case. We must allocate new
798
         * clusters for the node and zero them out.
893
         * clusters for the node and zero them out.
799
         */
894
         */
800
        int status;
895
        int status;
801
        unsigned nclsts;
896
        unsigned nclsts;
802
        fat_cluster_t mcl, lcl;
897
        fat_cluster_t mcl, lcl;
803
 
898
 
804
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
899
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
805
        /* create an independent chain of nclsts clusters in all FATs */
900
        /* create an independent chain of nclsts clusters in all FATs */
806
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
901
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
807
        if (status != EOK) {
902
        if (status != EOK) {
808
            /* could not allocate a chain of nclsts clusters */
903
            /* could not allocate a chain of nclsts clusters */
809
            fat_node_put(nodep);
904
            fat_node_put(nodep);
810
            ipc_answer_0(callid, status);
905
            ipc_answer_0(callid, status);
811
            ipc_answer_0(rid, status);
906
            ipc_answer_0(rid, status);
812
            return;
907
            return;
813
        }
908
        }
814
        /* zero fill any gaps */
909
        /* zero fill any gaps */
815
        fat_fill_gap(bs, nodep, mcl, pos);
910
        fat_fill_gap(bs, nodep, mcl, pos);
816
        b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
911
        b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
817
            flags);
912
            flags);
818
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
913
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
819
            bytes);
914
            bytes);
820
        b->dirty = true;        /* need to sync block */
915
        b->dirty = true;        /* need to sync block */
821
        block_put(b);
916
        block_put(b);
822
        /*
917
        /*
823
         * Append the cluster chain starting in mcl to the end of the
918
         * Append the cluster chain starting in mcl to the end of the
824
         * node's cluster chain.
919
         * node's cluster chain.
825
         */
920
         */
826
        fat_append_clusters(bs, nodep, mcl);
921
        fat_append_clusters(bs, nodep, mcl);
827
        nodep->size = pos + bytes;
922
        nodep->size = pos + bytes;
828
        nodep->dirty = true;        /* need to sync node */
923
        nodep->dirty = true;        /* need to sync node */
829
        ipc_answer_2(rid, EOK, bytes, nodep->size);
924
        ipc_answer_2(rid, EOK, bytes, nodep->size);
830
        fat_node_put(nodep);
925
        fat_node_put(nodep);
831
        return;
926
        return;
832
    }
927
    }
833
}
928
}
834
 
929
 
835
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
930
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
836
{
931
{
837
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
932
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
838
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
933
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
839
    size_t size = (off_t)IPC_GET_ARG3(*request);
934
    size_t size = (off_t)IPC_GET_ARG3(*request);
840
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
935
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
841
    fat_bs_t *bs;
936
    fat_bs_t *bs;
842
    uint16_t bps;
937
    uint16_t bps;
843
    uint8_t spc;
938
    uint8_t spc;
844
    unsigned bpc;   /* bytes per cluster */
939
    unsigned bpc;   /* bytes per cluster */
845
    int rc;
940
    int rc;
846
 
941
 
847
    if (!nodep) {
942
    if (!nodep) {
848
        ipc_answer_0(rid, ENOENT);
943
        ipc_answer_0(rid, ENOENT);
849
        return;
944
        return;
850
    }
945
    }
851
 
946
 
852
    bs = block_bb_get(dev_handle);
947
    bs = block_bb_get(dev_handle);
853
    bps = uint16_t_le2host(bs->bps);
948
    bps = uint16_t_le2host(bs->bps);
854
    spc = bs->spc;
949
    spc = bs->spc;
855
    bpc = bps * spc;
950
    bpc = bps * spc;
856
 
951
 
857
    if (nodep->size == size) {
952
    if (nodep->size == size) {
858
        rc = EOK;
953
        rc = EOK;
859
    } else if (nodep->size < size) {
954
    } else if (nodep->size < size) {
860
        /*
955
        /*
861
         * The standard says we have the freedom to grow the node.
956
         * The standard says we have the freedom to grow the node.
862
         * For now, we simply return an error.
957
         * For now, we simply return an error.
863
         */
958
         */
864
        rc = EINVAL;
959
        rc = EINVAL;
865
    } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
960
    } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
866
        /*
961
        /*
867
         * The node will be shrunk, but no clusters will be deallocated.
962
         * The node will be shrunk, but no clusters will be deallocated.
868
         */
963
         */
869
        nodep->size = size;
964
        nodep->size = size;
870
        nodep->dirty = true;        /* need to sync node */
965
        nodep->dirty = true;        /* need to sync node */
871
        rc = EOK;  
966
        rc = EOK;  
872
    } else {
967
    } else {
873
        /*
968
        /*
874
         * The node will be shrunk, clusters will be deallocated.
969
         * The node will be shrunk, clusters will be deallocated.
875
         */
970
         */
876
        if (size == 0) {
971
        if (size == 0) {
877
            fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
972
            fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
878
        } else {
973
        } else {
879
            fat_cluster_t lastc;
974
            fat_cluster_t lastc;
880
            (void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
975
            (void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
881
                &lastc, (size - 1) / bpc);
976
                &lastc, (size - 1) / bpc);
882
            fat_chop_clusters(bs, nodep, lastc);
977
            fat_chop_clusters(bs, nodep, lastc);
883
        }
978
        }
884
        nodep->size = size;
979
        nodep->size = size;
885
        nodep->dirty = true;        /* need to sync node */
980
        nodep->dirty = true;        /* need to sync node */
886
        rc = EOK;  
981
        rc = EOK;  
887
    }
982
    }
888
    fat_node_put(nodep);
983
    fat_node_put(nodep);
889
    ipc_answer_0(rid, rc);
984
    ipc_answer_0(rid, rc);
890
    return;
985
    return;
891
}
986
}
892
 
987
 
893
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
988
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
894
{
989
{
895
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
990
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
896
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
991
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
897
    int rc;
992
    int rc;
898
 
993
 
899
    fat_node_t *nodep = fat_node_get(dev_handle, index);
994
    fat_node_t *nodep = fat_node_get(dev_handle, index);
900
    if (!nodep) {
995
    if (!nodep) {
901
        ipc_answer_0(rid, ENOENT);
996
        ipc_answer_0(rid, ENOENT);
902
        return;
997
        return;
903
    }
998
    }
904
 
999
 
905
    rc = fat_destroy_node(nodep);
1000
    rc = fat_destroy_node(nodep);
906
    ipc_answer_0(rid, rc);
1001
    ipc_answer_0(rid, rc);
907
}
1002
}
908
 
1003
 
909
/**
1004
/**
910
 * @}
1005
 * @}
911
 */
1006
 */
912
 
1007