Subversion Repositories HelenOS

Rev

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

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