Subversion Repositories HelenOS

Rev

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

Rev 3537 Rev 3539
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);
92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps);
93
 
93
 
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
95
 
95
 
96
    d->firstc = host2uint16_t_le(node->firstc);
96
    d->firstc = host2uint16_t_le(node->firstc);
97
    if (node->type == FAT_FILE)
97
    if (node->type == FAT_FILE)
98
        d->size = host2uint32_t_le(node->size);
98
        d->size = host2uint32_t_le(node->size);
99
    /* TODO: update other fields? (e.g time fields, attr field) */
99
    /* TODO: update other fields? (e.g time fields, attr field) */
100
   
100
   
101
    b->dirty = true;        /* need to sync block */
101
    b->dirty = true;        /* need to sync block */
102
    block_put(b);
102
    block_put(b);
103
}
103
}
104
 
104
 
105
/** Internal version of fat_node_get().
105
/** Internal version of fat_node_get().
106
 *
106
 *
107
 * @param idxp      Locked index structure.
107
 * @param idxp      Locked index structure.
108
 */
108
 */
109
static void *fat_node_get_core(fat_idx_t *idxp)
109
static void *fat_node_get_core(fat_idx_t *idxp)
110
{
110
{
111
    block_t *b;
111
    block_t *b;
112
    fat_bs_t *bs;
112
    fat_bs_t *bs;
113
    fat_dentry_t *d;
113
    fat_dentry_t *d;
114
    fat_node_t *nodep = NULL;
114
    fat_node_t *nodep = NULL;
115
    unsigned bps;
115
    unsigned bps;
116
    unsigned dps;
116
    unsigned dps;
117
 
117
 
118
    if (idxp->nodep) {
118
    if (idxp->nodep) {
119
        /*
119
        /*
120
         * We are lucky.
120
         * We are lucky.
121
         * The node is already instantiated in memory.
121
         * The node is already instantiated in memory.
122
         */
122
         */
123
        futex_down(&idxp->nodep->lock);
123
        futex_down(&idxp->nodep->lock);
124
        if (!idxp->nodep->refcnt++)
124
        if (!idxp->nodep->refcnt++)
125
            list_remove(&idxp->nodep->ffn_link);
125
            list_remove(&idxp->nodep->ffn_link);
126
        futex_up(&idxp->nodep->lock);
126
        futex_up(&idxp->nodep->lock);
127
        return idxp->nodep;
127
        return idxp->nodep;
128
    }
128
    }
129
 
129
 
130
    /*
130
    /*
131
     * We must instantiate the node from the file system.
131
     * We must instantiate the node from the file system.
132
     */
132
     */
133
   
133
   
134
    assert(idxp->pfc);
134
    assert(idxp->pfc);
135
 
135
 
136
    futex_down(&ffn_futex);
136
    futex_down(&ffn_futex);
137
    if (!list_empty(&ffn_head)) {
137
    if (!list_empty(&ffn_head)) {
138
        /* Try to use a cached free node structure. */
138
        /* Try to use a cached free node structure. */
139
        fat_idx_t *idxp_tmp;
139
        fat_idx_t *idxp_tmp;
140
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
140
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
141
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
141
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
142
            goto skip_cache;
142
            goto skip_cache;
143
        idxp_tmp = nodep->idx;
143
        idxp_tmp = nodep->idx;
144
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
144
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
145
            futex_up(&nodep->lock);
145
            futex_up(&nodep->lock);
146
            goto skip_cache;
146
            goto skip_cache;
147
        }
147
        }
148
        list_remove(&nodep->ffn_link);
148
        list_remove(&nodep->ffn_link);
149
        futex_up(&ffn_futex);
149
        futex_up(&ffn_futex);
150
        if (nodep->dirty)
150
        if (nodep->dirty)
151
            fat_node_sync(nodep);
151
            fat_node_sync(nodep);
152
        idxp_tmp->nodep = NULL;
152
        idxp_tmp->nodep = NULL;
153
        futex_up(&nodep->lock);
153
        futex_up(&nodep->lock);
154
        futex_up(&idxp_tmp->lock);
154
        futex_up(&idxp_tmp->lock);
155
    } else {
155
    } else {
156
skip_cache:
156
skip_cache:
157
        /* Try to allocate a new node structure. */
157
        /* Try to allocate a new node structure. */
158
        futex_up(&ffn_futex);
158
        futex_up(&ffn_futex);
159
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
159
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
160
        if (!nodep)
160
        if (!nodep)
161
            return NULL;
161
            return NULL;
162
    }
162
    }
163
    fat_node_initialize(nodep);
163
    fat_node_initialize(nodep);
164
 
164
 
165
    bs = block_bb_get(idxp->dev_handle);
165
    bs = block_bb_get(idxp->dev_handle);
166
    bps = uint16_t_le2host(bs->bps);
166
    bps = uint16_t_le2host(bs->bps);
167
    dps = bps / sizeof(fat_dentry_t);
167
    dps = bps / sizeof(fat_dentry_t);
168
 
168
 
169
    /* Read the block that contains the dentry of interest. */
169
    /* Read the block that contains the dentry of interest. */
170
    b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
170
    b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
171
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
171
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
172
    assert(b);
172
    assert(b);
173
 
173
 
174
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
174
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
175
    if (d->attr & FAT_ATTR_SUBDIR) {
175
    if (d->attr & FAT_ATTR_SUBDIR) {
176
        /*
176
        /*
177
         * The only directory which does not have this bit set is the
177
         * The only directory which does not have this bit set is the
178
         * root directory itself. The root directory node is handled
178
         * root directory itself. The root directory node is handled
179
         * and initialized elsewhere.
179
         * and initialized elsewhere.
180
         */
180
         */
181
        nodep->type = FAT_DIRECTORY;
181
        nodep->type = FAT_DIRECTORY;
182
        /*
182
        /*
183
         * Unfortunately, the 'size' field of the FAT dentry is not
183
         * Unfortunately, the 'size' field of the FAT dentry is not
184
         * defined for the directory entry type. We must determine the
184
         * defined for the directory entry type. We must determine the
185
         * size of the directory by walking the FAT.
185
         * size of the directory by walking the FAT.
186
         */
186
         */
187
        nodep->size = bps * _fat_blcks_get(bs, idxp->dev_handle,
187
        nodep->size = bps * _fat_blcks_get(bs, idxp->dev_handle,
188
            uint16_t_le2host(d->firstc), NULL);
188
            uint16_t_le2host(d->firstc), NULL);
189
    } else {
189
    } else {
190
        nodep->type = FAT_FILE;
190
        nodep->type = FAT_FILE;
191
        nodep->size = uint32_t_le2host(d->size);
191
        nodep->size = uint32_t_le2host(d->size);
192
    }
192
    }
193
    nodep->firstc = uint16_t_le2host(d->firstc);
193
    nodep->firstc = uint16_t_le2host(d->firstc);
194
    nodep->lnkcnt = 1;
194
    nodep->lnkcnt = 1;
195
    nodep->refcnt = 1;
195
    nodep->refcnt = 1;
196
 
196
 
197
    block_put(b);
197
    block_put(b);
198
 
198
 
199
    /* Link the idx structure with the node structure. */
199
    /* Link the idx structure with the node structure. */
200
    nodep->idx = idxp;
200
    nodep->idx = idxp;
201
    idxp->nodep = nodep;
201
    idxp->nodep = nodep;
202
 
202
 
203
    return nodep;
203
    return nodep;
204
}
204
}
205
 
205
 
206
/** Instantiate a FAT in-core node. */
206
/** Instantiate a FAT in-core node. */
207
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
207
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
208
{
208
{
209
    void *node;
209
    void *node;
210
    fat_idx_t *idxp;
210
    fat_idx_t *idxp;
211
 
211
 
212
    idxp = fat_idx_get_by_index(dev_handle, index);
212
    idxp = fat_idx_get_by_index(dev_handle, index);
213
    if (!idxp)
213
    if (!idxp)
214
        return NULL;
214
        return NULL;
215
    /* idxp->lock held */
215
    /* idxp->lock held */
216
    node = fat_node_get_core(idxp);
216
    node = fat_node_get_core(idxp);
217
    futex_up(&idxp->lock);
217
    futex_up(&idxp->lock);
218
    return node;
218
    return node;
219
}
219
}
220
 
220
 
221
static void fat_node_put(void *node)
221
static void fat_node_put(void *node)
222
{
222
{
223
    fat_node_t *nodep = (fat_node_t *)node;
223
    fat_node_t *nodep = (fat_node_t *)node;
224
 
224
 
225
    futex_down(&nodep->lock);
225
    futex_down(&nodep->lock);
226
    if (!--nodep->refcnt) {
226
    if (!--nodep->refcnt) {
227
        futex_down(&ffn_futex);
227
        futex_down(&ffn_futex);
228
        list_append(&nodep->ffn_link, &ffn_head);
228
        list_append(&nodep->ffn_link, &ffn_head);
229
        futex_up(&ffn_futex);
229
        futex_up(&ffn_futex);
230
    }
230
    }
231
    futex_up(&nodep->lock);
231
    futex_up(&nodep->lock);
232
}
232
}
233
 
233
 
234
static void *fat_create(int flags)
234
static void *fat_create(int flags)
235
{
235
{
236
    return NULL;    /* not supported at the moment */
236
    return NULL;    /* not supported at the moment */
237
}
237
}
238
 
238
 
239
static int fat_destroy(void *node)
239
static int fat_destroy(void *node)
240
{
240
{
241
    return ENOTSUP; /* not supported at the moment */
241
    return ENOTSUP; /* not supported at the moment */
242
}
242
}
243
 
243
 
244
static bool fat_link(void *prnt, void *chld, const char *name)
244
static bool fat_link(void *prnt, void *chld, const char *name)
245
{
245
{
246
    return false;   /* not supported at the moment */
246
    return false;   /* not supported at the moment */
247
}
247
}
248
 
248
 
249
static int fat_unlink(void *prnt, void *chld)
249
static int fat_unlink(void *prnt, void *chld)
250
{
250
{
251
    return ENOTSUP; /* not supported at the moment */
251
    return ENOTSUP; /* not supported at the moment */
252
}
252
}
253
 
253
 
254
static void *fat_match(void *prnt, const char *component)
254
static void *fat_match(void *prnt, const char *component)
255
{
255
{
256
    fat_bs_t *bs;
256
    fat_bs_t *bs;
257
    fat_node_t *parentp = (fat_node_t *)prnt;
257
    fat_node_t *parentp = (fat_node_t *)prnt;
258
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
258
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
259
    unsigned i, j;
259
    unsigned i, j;
260
    unsigned bps;       /* bytes per sector */
260
    unsigned bps;       /* bytes per sector */
261
    unsigned dps;       /* dentries per sector */
261
    unsigned dps;       /* dentries per sector */
262
    unsigned blocks;
262
    unsigned blocks;
263
    fat_dentry_t *d;
263
    fat_dentry_t *d;
264
    block_t *b;
264
    block_t *b;
265
 
265
 
266
    futex_down(&parentp->idx->lock);
266
    futex_down(&parentp->idx->lock);
267
    bs = block_bb_get(parentp->idx->dev_handle);
267
    bs = block_bb_get(parentp->idx->dev_handle);
268
    bps = uint16_t_le2host(bs->bps);
268
    bps = uint16_t_le2host(bs->bps);
269
    dps = bps / sizeof(fat_dentry_t);
269
    dps = bps / sizeof(fat_dentry_t);
270
    blocks = parentp->size / bps;
270
    blocks = parentp->size / bps;
271
    for (i = 0; i < blocks; i++) {
271
    for (i = 0; i < blocks; i++) {
272
        b = fat_block_get(bs, parentp, i);
272
        b = fat_block_get(bs, parentp, i);
273
        for (j = 0; j < dps; j++) {
273
        for (j = 0; j < dps; j++) {
274
            d = ((fat_dentry_t *)b->data) + j;
274
            d = ((fat_dentry_t *)b->data) + j;
275
            switch (fat_classify_dentry(d)) {
275
            switch (fat_classify_dentry(d)) {
276
            case FAT_DENTRY_SKIP:
276
            case FAT_DENTRY_SKIP:
277
                continue;
277
                continue;
278
            case FAT_DENTRY_LAST:
278
            case FAT_DENTRY_LAST:
279
                block_put(b);
279
                block_put(b);
280
                futex_up(&parentp->idx->lock);
280
                futex_up(&parentp->idx->lock);
281
                return NULL;
281
                return NULL;
282
            default:
282
            default:
283
            case FAT_DENTRY_VALID:
283
            case FAT_DENTRY_VALID:
284
                dentry_name_canonify(d, name);
284
                dentry_name_canonify(d, name);
285
                break;
285
                break;
286
            }
286
            }
287
            if (stricmp(name, component) == 0) {
287
            if (stricmp(name, component) == 0) {
288
                /* hit */
288
                /* hit */
289
                void *node;
289
                void *node;
290
                /*
290
                /*
291
                 * Assume tree hierarchy for locking.  We
291
                 * Assume tree hierarchy for locking.  We
292
                 * already have the parent and now we are going
292
                 * already have the parent and now we are going
293
                 * to lock the child.  Never lock in the oposite
293
                 * to lock the child.  Never lock in the oposite
294
                 * order.
294
                 * order.
295
                 */
295
                 */
296
                fat_idx_t *idx = fat_idx_get_by_pos(
296
                fat_idx_t *idx = fat_idx_get_by_pos(
297
                    parentp->idx->dev_handle, parentp->firstc,
297
                    parentp->idx->dev_handle, parentp->firstc,
298
                    i * dps + j);
298
                    i * dps + j);
299
                futex_up(&parentp->idx->lock);
299
                futex_up(&parentp->idx->lock);
300
                if (!idx) {
300
                if (!idx) {
301
                    /*
301
                    /*
302
                     * Can happen if memory is low or if we
302
                     * Can happen if memory is low or if we
303
                     * run out of 32-bit indices.
303
                     * run out of 32-bit indices.
304
                     */
304
                     */
305
                    block_put(b);
305
                    block_put(b);
306
                    return NULL;
306
                    return NULL;
307
                }
307
                }
308
                node = fat_node_get_core(idx);
308
                node = fat_node_get_core(idx);
309
                futex_up(&idx->lock);
309
                futex_up(&idx->lock);
310
                block_put(b);
310
                block_put(b);
311
                return node;
311
                return node;
312
            }
312
            }
313
        }
313
        }
314
        block_put(b);
314
        block_put(b);
315
    }
315
    }
316
 
316
 
317
    futex_up(&parentp->idx->lock);
317
    futex_up(&parentp->idx->lock);
318
    return NULL;
318
    return NULL;
319
}
319
}
320
 
320
 
321
static fs_index_t fat_index_get(void *node)
321
static fs_index_t fat_index_get(void *node)
322
{
322
{
323
    fat_node_t *fnodep = (fat_node_t *)node;
323
    fat_node_t *fnodep = (fat_node_t *)node;
324
    if (!fnodep)
324
    if (!fnodep)
325
        return 0;
325
        return 0;
326
    return fnodep->idx->index;
326
    return fnodep->idx->index;
327
}
327
}
328
 
328
 
329
static size_t fat_size_get(void *node)
329
static size_t fat_size_get(void *node)
330
{
330
{
331
    return ((fat_node_t *)node)->size;
331
    return ((fat_node_t *)node)->size;
332
}
332
}
333
 
333
 
334
static unsigned fat_lnkcnt_get(void *node)
334
static unsigned fat_lnkcnt_get(void *node)
335
{
335
{
336
    return ((fat_node_t *)node)->lnkcnt;
336
    return ((fat_node_t *)node)->lnkcnt;
337
}
337
}
338
 
338
 
339
static bool fat_has_children(void *node)
339
static bool fat_has_children(void *node)
340
{
340
{
341
    fat_bs_t *bs;
341
    fat_bs_t *bs;
342
    fat_node_t *nodep = (fat_node_t *)node;
342
    fat_node_t *nodep = (fat_node_t *)node;
343
    unsigned bps;
343
    unsigned bps;
344
    unsigned dps;
344
    unsigned dps;
345
    unsigned blocks;
345
    unsigned blocks;
346
    block_t *b;
346
    block_t *b;
347
    unsigned i, j;
347
    unsigned i, j;
348
 
348
 
349
    if (nodep->type != FAT_DIRECTORY)
349
    if (nodep->type != FAT_DIRECTORY)
350
        return false;
350
        return false;
351
   
351
   
352
    futex_down(&nodep->idx->lock);
352
    futex_down(&nodep->idx->lock);
353
    bs = block_bb_get(nodep->idx->dev_handle);
353
    bs = block_bb_get(nodep->idx->dev_handle);
354
    bps = uint16_t_le2host(bs->bps);
354
    bps = uint16_t_le2host(bs->bps);
355
    dps = bps / sizeof(fat_dentry_t);
355
    dps = bps / sizeof(fat_dentry_t);
356
 
356
 
357
    blocks = nodep->size / bps;
357
    blocks = nodep->size / bps;
358
 
358
 
359
    for (i = 0; i < blocks; i++) {
359
    for (i = 0; i < blocks; i++) {
360
        fat_dentry_t *d;
360
        fat_dentry_t *d;
361
   
361
   
362
        b = fat_block_get(bs, nodep, i);
362
        b = fat_block_get(bs, nodep, i);
363
        for (j = 0; j < dps; j++) {
363
        for (j = 0; j < dps; j++) {
364
            d = ((fat_dentry_t *)b->data) + j;
364
            d = ((fat_dentry_t *)b->data) + j;
365
            switch (fat_classify_dentry(d)) {
365
            switch (fat_classify_dentry(d)) {
366
            case FAT_DENTRY_SKIP:
366
            case FAT_DENTRY_SKIP:
367
                continue;
367
                continue;
368
            case FAT_DENTRY_LAST:
368
            case FAT_DENTRY_LAST:
369
                block_put(b);
369
                block_put(b);
370
                futex_up(&nodep->idx->lock);
370
                futex_up(&nodep->idx->lock);
371
                return false;
371
                return false;
372
            default:
372
            default:
373
            case FAT_DENTRY_VALID:
373
            case FAT_DENTRY_VALID:
374
                block_put(b);
374
                block_put(b);
375
                futex_up(&nodep->idx->lock);
375
                futex_up(&nodep->idx->lock);
376
                return true;
376
                return true;
377
            }
377
            }
378
            block_put(b);
378
            block_put(b);
379
            futex_up(&nodep->idx->lock);
379
            futex_up(&nodep->idx->lock);
380
            return true;
380
            return true;
381
        }
381
        }
382
        block_put(b);
382
        block_put(b);
383
    }
383
    }
384
 
384
 
385
    futex_up(&nodep->idx->lock);
385
    futex_up(&nodep->idx->lock);
386
    return false;
386
    return false;
387
}
387
}
388
 
388
 
389
static void *fat_root_get(dev_handle_t dev_handle)
389
static void *fat_root_get(dev_handle_t dev_handle)
390
{
390
{
391
    return fat_node_get(dev_handle, 0);
391
    return fat_node_get(dev_handle, 0);
392
}
392
}
393
 
393
 
394
static char fat_plb_get_char(unsigned pos)
394
static char fat_plb_get_char(unsigned pos)
395
{
395
{
396
    return fat_reg.plb_ro[pos % PLB_SIZE];
396
    return fat_reg.plb_ro[pos % PLB_SIZE];
397
}
397
}
398
 
398
 
399
static bool fat_is_directory(void *node)
399
static bool fat_is_directory(void *node)
400
{
400
{
401
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
401
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
402
}
402
}
403
 
403
 
404
static bool fat_is_file(void *node)
404
static bool fat_is_file(void *node)
405
{
405
{
406
    return ((fat_node_t *)node)->type == FAT_FILE;
406
    return ((fat_node_t *)node)->type == FAT_FILE;
407
}
407
}
408
 
408
 
409
/** libfs operations */
409
/** libfs operations */
410
libfs_ops_t fat_libfs_ops = {
410
libfs_ops_t fat_libfs_ops = {
411
    .match = fat_match,
411
    .match = fat_match,
412
    .node_get = fat_node_get,
412
    .node_get = fat_node_get,
413
    .node_put = fat_node_put,
413
    .node_put = fat_node_put,
414
    .create = fat_create,
414
    .create = fat_create,
415
    .destroy = fat_destroy,
415
    .destroy = fat_destroy,
416
    .link = fat_link,
416
    .link = fat_link,
417
    .unlink = fat_unlink,
417
    .unlink = fat_unlink,
418
    .index_get = fat_index_get,
418
    .index_get = fat_index_get,
419
    .size_get = fat_size_get,
419
    .size_get = fat_size_get,
420
    .lnkcnt_get = fat_lnkcnt_get,
420
    .lnkcnt_get = fat_lnkcnt_get,
421
    .has_children = fat_has_children,
421
    .has_children = fat_has_children,
422
    .root_get = fat_root_get,
422
    .root_get = fat_root_get,
423
    .plb_get_char = fat_plb_get_char,
423
    .plb_get_char = fat_plb_get_char,
424
    .is_directory = fat_is_directory,
424
    .is_directory = fat_is_directory,
425
    .is_file = fat_is_file
425
    .is_file = fat_is_file
426
};
426
};
427
 
427
 
428
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
428
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
429
{
429
{
430
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
430
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
431
    fat_bs_t *bs;
431
    fat_bs_t *bs;
432
    uint16_t bps;
432
    uint16_t bps;
433
    uint16_t rde;
433
    uint16_t rde;
434
    int rc;
434
    int rc;
435
 
435
 
436
    /* initialize libblock */
436
    /* initialize libblock */
437
    rc = block_init(dev_handle, BS_SIZE);
437
    rc = block_init(dev_handle, BS_SIZE);
438
    if (rc != EOK) {
438
    if (rc != EOK) {
439
        ipc_answer_0(rid, rc);
439
        ipc_answer_0(rid, rc);
440
        return;
440
        return;
441
    }
441
    }
442
 
442
 
443
    /* prepare the boot block */
443
    /* prepare the boot block */
444
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
444
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
445
    if (rc != EOK) {
445
    if (rc != EOK) {
446
        block_fini(dev_handle);
446
        block_fini(dev_handle);
447
        ipc_answer_0(rid, rc);
447
        ipc_answer_0(rid, rc);
448
        return;
448
        return;
449
    }
449
    }
450
 
450
 
451
    /* get the buffer with the boot sector */
451
    /* get the buffer with the boot sector */
452
    bs = block_bb_get(dev_handle);
452
    bs = block_bb_get(dev_handle);
453
   
453
   
454
    /* Read the number of root directory entries. */
454
    /* Read the number of root directory entries. */
455
    bps = uint16_t_le2host(bs->bps);
455
    bps = uint16_t_le2host(bs->bps);
456
    rde = uint16_t_le2host(bs->root_ent_max);
456
    rde = uint16_t_le2host(bs->root_ent_max);
457
 
457
 
458
    if (bps != BS_SIZE) {
458
    if (bps != BS_SIZE) {
459
        block_fini(dev_handle);
459
        block_fini(dev_handle);
460
        ipc_answer_0(rid, ENOTSUP);
460
        ipc_answer_0(rid, ENOTSUP);
461
        return;
461
        return;
462
    }
462
    }
463
 
463
 
-
 
464
    /* Initialize the block cache */
-
 
465
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
-
 
466
    if (rc != EOK) {
-
 
467
        block_fini(dev_handle);
-
 
468
        ipc_answer_0(rid, rc);
-
 
469
        return;
-
 
470
    }
-
 
471
 
464
    rc = fat_idx_init_by_dev_handle(dev_handle);
472
    rc = fat_idx_init_by_dev_handle(dev_handle);
465
    if (rc != EOK) {
473
    if (rc != EOK) {
466
        block_fini(dev_handle);
474
        block_fini(dev_handle);
467
        ipc_answer_0(rid, rc);
475
        ipc_answer_0(rid, rc);
468
        return;
476
        return;
469
    }
477
    }
470
 
478
 
471
    /* Initialize the root node. */
479
    /* Initialize the root node. */
472
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
480
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
473
    if (!rootp) {
481
    if (!rootp) {
474
        block_fini(dev_handle);
482
        block_fini(dev_handle);
475
        fat_idx_fini_by_dev_handle(dev_handle);
483
        fat_idx_fini_by_dev_handle(dev_handle);
476
        ipc_answer_0(rid, ENOMEM);
484
        ipc_answer_0(rid, ENOMEM);
477
        return;
485
        return;
478
    }
486
    }
479
    fat_node_initialize(rootp);
487
    fat_node_initialize(rootp);
480
 
488
 
481
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
489
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
482
    if (!ridxp) {
490
    if (!ridxp) {
483
        block_fini(dev_handle);
491
        block_fini(dev_handle);
484
        free(rootp);
492
        free(rootp);
485
        fat_idx_fini_by_dev_handle(dev_handle);
493
        fat_idx_fini_by_dev_handle(dev_handle);
486
        ipc_answer_0(rid, ENOMEM);
494
        ipc_answer_0(rid, ENOMEM);
487
        return;
495
        return;
488
    }
496
    }
489
    assert(ridxp->index == 0);
497
    assert(ridxp->index == 0);
490
    /* ridxp->lock held */
498
    /* ridxp->lock held */
491
 
499
 
492
    rootp->type = FAT_DIRECTORY;
500
    rootp->type = FAT_DIRECTORY;
493
    rootp->firstc = FAT_CLST_ROOT;
501
    rootp->firstc = FAT_CLST_ROOT;
494
    rootp->refcnt = 1;
502
    rootp->refcnt = 1;
495
    rootp->lnkcnt = 0;  /* FS root is not linked */
503
    rootp->lnkcnt = 0;  /* FS root is not linked */
496
    rootp->size = rde * sizeof(fat_dentry_t);
504
    rootp->size = rde * sizeof(fat_dentry_t);
497
    rootp->idx = ridxp;
505
    rootp->idx = ridxp;
498
    ridxp->nodep = rootp;
506
    ridxp->nodep = rootp;
499
   
507
   
500
    futex_up(&ridxp->lock);
508
    futex_up(&ridxp->lock);
501
 
509
 
502
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
510
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
503
}
511
}
504
 
512
 
505
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
513
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
506
{
514
{
507
    ipc_answer_0(rid, ENOTSUP);
515
    ipc_answer_0(rid, ENOTSUP);
508
}
516
}
509
 
517
 
510
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
518
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
511
{
519
{
512
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
520
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
513
}
521
}
514
 
522
 
515
void fat_read(ipc_callid_t rid, ipc_call_t *request)
523
void fat_read(ipc_callid_t rid, ipc_call_t *request)
516
{
524
{
517
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
525
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
518
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
526
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
519
    off_t pos = (off_t)IPC_GET_ARG3(*request);
527
    off_t pos = (off_t)IPC_GET_ARG3(*request);
520
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
528
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
521
    fat_bs_t *bs;
529
    fat_bs_t *bs;
522
    uint16_t bps;
530
    uint16_t bps;
523
    size_t bytes;
531
    size_t bytes;
524
    block_t *b;
532
    block_t *b;
525
 
533
 
526
    if (!nodep) {
534
    if (!nodep) {
527
        ipc_answer_0(rid, ENOENT);
535
        ipc_answer_0(rid, ENOENT);
528
        return;
536
        return;
529
    }
537
    }
530
 
538
 
531
    ipc_callid_t callid;
539
    ipc_callid_t callid;
532
    size_t len;
540
    size_t len;
533
    if (!ipc_data_read_receive(&callid, &len)) {
541
    if (!ipc_data_read_receive(&callid, &len)) {
534
        fat_node_put(nodep);
542
        fat_node_put(nodep);
535
        ipc_answer_0(callid, EINVAL);
543
        ipc_answer_0(callid, EINVAL);
536
        ipc_answer_0(rid, EINVAL);
544
        ipc_answer_0(rid, EINVAL);
537
        return;
545
        return;
538
    }
546
    }
539
 
547
 
540
    bs = block_bb_get(dev_handle);
548
    bs = block_bb_get(dev_handle);
541
    bps = uint16_t_le2host(bs->bps);
549
    bps = uint16_t_le2host(bs->bps);
542
 
550
 
543
    if (nodep->type == FAT_FILE) {
551
    if (nodep->type == FAT_FILE) {
544
        /*
552
        /*
545
         * Our strategy for regular file reads is to read one block at
553
         * Our strategy for regular file reads is to read one block at
546
         * most and make use of the possibility to return less data than
554
         * most and make use of the possibility to return less data than
547
         * requested. This keeps the code very simple.
555
         * requested. This keeps the code very simple.
548
         */
556
         */
549
        if (pos >= nodep->size) {
557
        if (pos >= nodep->size) {
550
            /* reading beyond the EOF */
558
            /* reading beyond the EOF */
551
            bytes = 0;
559
            bytes = 0;
552
            (void) ipc_data_read_finalize(callid, NULL, 0);
560
            (void) ipc_data_read_finalize(callid, NULL, 0);
553
        } else {
561
        } else {
554
            bytes = min(len, bps - pos % bps);
562
            bytes = min(len, bps - pos % bps);
555
            bytes = min(bytes, nodep->size - pos);
563
            bytes = min(bytes, nodep->size - pos);
556
            b = fat_block_get(bs, nodep, pos / bps);
564
            b = fat_block_get(bs, nodep, pos / bps);
557
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
565
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
558
                bytes);
566
                bytes);
559
            block_put(b);
567
            block_put(b);
560
        }
568
        }
561
    } else {
569
    } else {
562
        unsigned bnum;
570
        unsigned bnum;
563
        off_t spos = pos;
571
        off_t spos = pos;
564
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
572
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
565
        fat_dentry_t *d;
573
        fat_dentry_t *d;
566
 
574
 
567
        assert(nodep->type == FAT_DIRECTORY);
575
        assert(nodep->type == FAT_DIRECTORY);
568
        assert(nodep->size % bps == 0);
576
        assert(nodep->size % bps == 0);
569
        assert(bps % sizeof(fat_dentry_t) == 0);
577
        assert(bps % sizeof(fat_dentry_t) == 0);
570
 
578
 
571
        /*
579
        /*
572
         * Our strategy for readdir() is to use the position pointer as
580
         * Our strategy for readdir() is to use the position pointer as
573
         * an index into the array of all dentries. On entry, it points
581
         * an index into the array of all dentries. On entry, it points
574
         * to the first unread dentry. If we skip any dentries, we bump
582
         * to the first unread dentry. If we skip any dentries, we bump
575
         * the position pointer accordingly.
583
         * the position pointer accordingly.
576
         */
584
         */
577
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
585
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
578
        while (bnum < nodep->size / bps) {
586
        while (bnum < nodep->size / bps) {
579
            off_t o;
587
            off_t o;
580
 
588
 
581
            b = fat_block_get(bs, nodep, bnum);
589
            b = fat_block_get(bs, nodep, bnum);
582
            for (o = pos % (bps / sizeof(fat_dentry_t));
590
            for (o = pos % (bps / sizeof(fat_dentry_t));
583
                o < bps / sizeof(fat_dentry_t);
591
                o < bps / sizeof(fat_dentry_t);
584
                o++, pos++) {
592
                o++, pos++) {
585
                d = ((fat_dentry_t *)b->data) + o;
593
                d = ((fat_dentry_t *)b->data) + o;
586
                switch (fat_classify_dentry(d)) {
594
                switch (fat_classify_dentry(d)) {
587
                case FAT_DENTRY_SKIP:
595
                case FAT_DENTRY_SKIP:
588
                    continue;
596
                    continue;
589
                case FAT_DENTRY_LAST:
597
                case FAT_DENTRY_LAST:
590
                    block_put(b);
598
                    block_put(b);
591
                    goto miss;
599
                    goto miss;
592
                default:
600
                default:
593
                case FAT_DENTRY_VALID:
601
                case FAT_DENTRY_VALID:
594
                    dentry_name_canonify(d, name);
602
                    dentry_name_canonify(d, name);
595
                    block_put(b);
603
                    block_put(b);
596
                    goto hit;
604
                    goto hit;
597
                }
605
                }
598
            }
606
            }
599
            block_put(b);
607
            block_put(b);
600
            bnum++;
608
            bnum++;
601
        }
609
        }
602
miss:
610
miss:
603
        fat_node_put(nodep);
611
        fat_node_put(nodep);
604
        ipc_answer_0(callid, ENOENT);
612
        ipc_answer_0(callid, ENOENT);
605
        ipc_answer_1(rid, ENOENT, 0);
613
        ipc_answer_1(rid, ENOENT, 0);
606
        return;
614
        return;
607
hit:
615
hit:
608
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
616
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
609
        bytes = (pos - spos) + 1;
617
        bytes = (pos - spos) + 1;
610
    }
618
    }
611
 
619
 
612
    fat_node_put(nodep);
620
    fat_node_put(nodep);
613
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
621
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
614
}
622
}
615
 
623
 
616
void fat_write(ipc_callid_t rid, ipc_call_t *request)
624
void fat_write(ipc_callid_t rid, ipc_call_t *request)
617
{
625
{
618
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
626
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
619
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
627
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
620
    off_t pos = (off_t)IPC_GET_ARG3(*request);
628
    off_t pos = (off_t)IPC_GET_ARG3(*request);
621
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
629
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
622
    fat_bs_t *bs;
630
    fat_bs_t *bs;
623
    size_t bytes;
631
    size_t bytes;
624
    block_t *b;
632
    block_t *b;
625
    uint16_t bps;
633
    uint16_t bps;
626
    unsigned spc;
634
    unsigned spc;
627
    off_t boundary;
635
    off_t boundary;
628
   
636
   
629
    if (!nodep) {
637
    if (!nodep) {
630
        ipc_answer_0(rid, ENOENT);
638
        ipc_answer_0(rid, ENOENT);
631
        return;
639
        return;
632
    }
640
    }
633
   
641
   
634
    /* XXX remove me when you are ready */
642
    /* XXX remove me when you are ready */
635
    {
643
    {
636
        ipc_answer_0(rid, ENOTSUP);
644
        ipc_answer_0(rid, ENOTSUP);
637
        fat_node_put(nodep);
645
        fat_node_put(nodep);
638
        return;
646
        return;
639
    }
647
    }
640
 
648
 
641
    ipc_callid_t callid;
649
    ipc_callid_t callid;
642
    size_t len;
650
    size_t len;
643
    if (!ipc_data_write_receive(&callid, &len)) {
651
    if (!ipc_data_write_receive(&callid, &len)) {
644
        fat_node_put(nodep);
652
        fat_node_put(nodep);
645
        ipc_answer_0(callid, EINVAL);
653
        ipc_answer_0(callid, EINVAL);
646
        ipc_answer_0(rid, EINVAL);
654
        ipc_answer_0(rid, EINVAL);
647
        return;
655
        return;
648
    }
656
    }
649
 
657
 
650
    /*
658
    /*
651
     * In all scenarios, we will attempt to write out only one block worth
659
     * In all scenarios, we will attempt to write out only one block worth
652
     * of data at maximum. There might be some more efficient approaches,
660
     * of data at maximum. There might be some more efficient approaches,
653
     * but this one greatly simplifies fat_write(). Note that we can afford
661
     * but this one greatly simplifies fat_write(). Note that we can afford
654
     * to do this because the client must be ready to handle the return
662
     * to do this because the client must be ready to handle the return
655
     * value signalizing a smaller number of bytes written.
663
     * value signalizing a smaller number of bytes written.
656
     */
664
     */
657
    bytes = min(len, bps - pos % bps);
665
    bytes = min(len, bps - pos % bps);
658
 
666
 
659
    bs = block_bb_get(dev_handle);
667
    bs = block_bb_get(dev_handle);
660
    bps = uint16_t_le2host(bs->bps);
668
    bps = uint16_t_le2host(bs->bps);
661
    spc = bs->spc;
669
    spc = bs->spc;
662
   
670
   
663
    boundary = ROUND_UP(nodep->size, bps * spc);
671
    boundary = ROUND_UP(nodep->size, bps * spc);
664
    if (pos < boundary) {
672
    if (pos < boundary) {
665
        /*
673
        /*
666
         * This is the easier case - we are either overwriting already
674
         * This is the easier case - we are either overwriting already
667
         * existing contents or writing behind the EOF, but still within
675
         * existing contents or writing behind the EOF, but still within
668
         * the limits of the last cluster. The node size may grow to the
676
         * the limits of the last cluster. The node size may grow to the
669
         * next block size boundary.
677
         * next block size boundary.
670
         */
678
         */
671
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
679
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
672
        b = fat_block_get(bs, nodep, pos / bps);
680
        b = fat_block_get(bs, nodep, pos / bps);
673
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
681
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
674
            bytes);
682
            bytes);
675
        b->dirty = true;        /* need to sync block */
683
        b->dirty = true;        /* need to sync block */
676
        block_put(b);
684
        block_put(b);
677
        if (pos + bytes > nodep->size) {
685
        if (pos + bytes > nodep->size) {
678
            nodep->size = pos + bytes;
686
            nodep->size = pos + bytes;
679
            nodep->dirty = true;    /* need to sync node */
687
            nodep->dirty = true;    /* need to sync node */
680
        }
688
        }
681
        fat_node_put(nodep);
689
        fat_node_put(nodep);
682
        ipc_answer_1(rid, EOK, bytes); 
690
        ipc_answer_1(rid, EOK, bytes); 
683
        return;
691
        return;
684
    } else {
692
    } else {
685
        /*
693
        /*
686
         * This is the more difficult case. We must allocate new
694
         * This is the more difficult case. We must allocate new
687
         * clusters for the node and zero them out.
695
         * clusters for the node and zero them out.
688
         */
696
         */
689
        int status;
697
        int status;
690
        unsigned nclsts;
698
        unsigned nclsts;
691
        fat_cluster_t mcl, lcl;
699
        fat_cluster_t mcl, lcl;
692
   
700
   
693
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
701
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
694
            bps * spc;
702
            bps * spc;
695
        /* create an independent chain of nclsts clusters in all FATs */
703
        /* create an independent chain of nclsts clusters in all FATs */
696
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl,
704
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl,
697
            &lcl);
705
            &lcl);
698
        if (status != EOK) {
706
        if (status != EOK) {
699
            /* could not allocate a chain of nclsts clusters */
707
            /* could not allocate a chain of nclsts clusters */
700
            fat_node_put(nodep);
708
            fat_node_put(nodep);
701
            ipc_answer_0(callid, status);
709
            ipc_answer_0(callid, status);
702
            ipc_answer_0(rid, status);
710
            ipc_answer_0(rid, status);
703
            return;
711
            return;
704
        }
712
        }
705
        /* zero fill any gaps */
713
        /* zero fill any gaps */
706
        fat_fill_gap(bs, nodep, mcl, pos);
714
        fat_fill_gap(bs, nodep, mcl, pos);
707
        b = _fat_block_get(bs, dev_handle, lcl,
715
        b = _fat_block_get(bs, dev_handle, lcl,
708
            (pos / bps) % spc);
716
            (pos / bps) % spc);
709
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
717
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
710
            bytes);
718
            bytes);
711
        b->dirty = true;        /* need to sync block */
719
        b->dirty = true;        /* need to sync block */
712
        block_put(b);
720
        block_put(b);
713
        /*
721
        /*
714
         * Append the cluster chain starting in mcl to the end of the
722
         * Append the cluster chain starting in mcl to the end of the
715
         * node's cluster chain.
723
         * node's cluster chain.
716
         */
724
         */
717
        fat_append_clusters(bs, nodep, mcl);
725
        fat_append_clusters(bs, nodep, mcl);
718
        nodep->size = pos + bytes;
726
        nodep->size = pos + bytes;
719
        nodep->dirty = true;        /* need to sync node */
727
        nodep->dirty = true;        /* need to sync node */
720
        fat_node_put(nodep);
728
        fat_node_put(nodep);
721
        ipc_answer_1(rid, EOK, bytes);
729
        ipc_answer_1(rid, EOK, bytes);
722
        return;
730
        return;
723
    }
731
    }
724
}
732
}
725
 
733
 
726
/**
734
/**
727
 * @}
735
 * @}
728
 */
736
 */
729
 
737