Subversion Repositories HelenOS

Rev

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

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