Subversion Repositories HelenOS

Rev

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

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