Subversion Repositories HelenOS

Rev

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

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