Rev 3550 | Rev 3573 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2627 | jermar | 1 | /* |
2793 | jermar | 2 | * Copyright (c) 2008 Jakub Jermar |
2627 | jermar | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | /** @addtogroup fs |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file fat_ops.c |
||
35 | * @brief Implementation of VFS operations for the FAT file system server. |
||
36 | */ |
||
37 | |||
38 | #include "fat.h" |
||
3505 | jermar | 39 | #include "fat_dentry.h" |
40 | #include "fat_fat.h" |
||
2638 | jermar | 41 | #include "../../vfs/vfs.h" |
2793 | jermar | 42 | #include <libfs.h> |
3521 | jermar | 43 | #include <libblock.h> |
2627 | jermar | 44 | #include <ipc/ipc.h> |
3257 | jermar | 45 | #include <ipc/services.h> |
46 | #include <ipc/devmap.h> |
||
2627 | jermar | 47 | #include <async.h> |
48 | #include <errno.h> |
||
2793 | jermar | 49 | #include <string.h> |
2798 | jermar | 50 | #include <byteorder.h> |
2831 | jermar | 51 | #include <libadt/hash_table.h> |
52 | #include <libadt/list.h> |
||
53 | #include <assert.h> |
||
2856 | jermar | 54 | #include <futex.h> |
3257 | jermar | 55 | #include <sys/mman.h> |
3499 | jermar | 56 | #include <align.h> |
2627 | jermar | 57 | |
2951 | jermar | 58 | /** Futex protecting the list of cached free FAT nodes. */ |
59 | static futex_t ffn_futex = FUTEX_INITIALIZER; |
||
2843 | jermar | 60 | |
2951 | jermar | 61 | /** List of cached free FAT nodes. */ |
62 | static LIST_INITIALIZE(ffn_head); |
||
63 | |||
2831 | jermar | 64 | static void fat_node_initialize(fat_node_t *node) |
2793 | jermar | 65 | { |
2951 | jermar | 66 | futex_initialize(&node->lock, 1); |
2864 | jermar | 67 | node->idx = NULL; |
2831 | jermar | 68 | node->type = 0; |
69 | link_initialize(&node->ffn_link); |
||
70 | node->size = 0; |
||
71 | node->lnkcnt = 0; |
||
72 | node->refcnt = 0; |
||
73 | node->dirty = false; |
||
2793 | jermar | 74 | } |
75 | |||
2893 | jermar | 76 | static void fat_node_sync(fat_node_t *node) |
2831 | jermar | 77 | { |
3530 | jermar | 78 | block_t *b; |
79 | fat_bs_t *bs; |
||
3519 | jermar | 80 | fat_dentry_t *d; |
81 | uint16_t bps; |
||
82 | unsigned dps; |
||
83 | |||
84 | assert(node->dirty); |
||
85 | |||
3530 | jermar | 86 | bs = block_bb_get(node->idx->dev_handle); |
87 | bps = uint16_t_le2host(bs->bps); |
||
3519 | jermar | 88 | dps = bps / sizeof(fat_dentry_t); |
89 | |||
90 | /* Read the block that contains the dentry of interest. */ |
||
3530 | jermar | 91 | b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
3519 | jermar | 92 | (node->idx->pdi * sizeof(fat_dentry_t)) / bps); |
93 | |||
94 | d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
||
95 | |||
96 | d->firstc = host2uint16_t_le(node->firstc); |
||
97 | if (node->type == FAT_FILE) |
||
98 | d->size = host2uint32_t_le(node->size); |
||
99 | /* TODO: update other fields? (e.g time fields, attr field) */ |
||
100 | |||
101 | b->dirty = true; /* need to sync block */ |
||
102 | block_put(b); |
||
2831 | jermar | 103 | } |
104 | |||
2951 | jermar | 105 | /** Internal version of fat_node_get(). |
106 | * |
||
107 | * @param idxp Locked index structure. |
||
108 | */ |
||
109 | static void *fat_node_get_core(fat_idx_t *idxp) |
||
2831 | jermar | 110 | { |
3530 | jermar | 111 | block_t *b; |
112 | fat_bs_t *bs; |
||
2891 | jermar | 113 | fat_dentry_t *d; |
3312 | jermar | 114 | fat_node_t *nodep = NULL; |
2891 | jermar | 115 | unsigned bps; |
3550 | jermar | 116 | unsigned spc; |
2891 | jermar | 117 | unsigned dps; |
118 | |||
2951 | jermar | 119 | if (idxp->nodep) { |
2891 | jermar | 120 | /* |
121 | * We are lucky. |
||
122 | * The node is already instantiated in memory. |
||
123 | */ |
||
2951 | jermar | 124 | futex_down(&idxp->nodep->lock); |
125 | if (!idxp->nodep->refcnt++) |
||
3312 | jermar | 126 | list_remove(&idxp->nodep->ffn_link); |
2951 | jermar | 127 | futex_up(&idxp->nodep->lock); |
128 | return idxp->nodep; |
||
2891 | jermar | 129 | } |
130 | |||
131 | /* |
||
132 | * We must instantiate the node from the file system. |
||
133 | */ |
||
134 | |||
2951 | jermar | 135 | assert(idxp->pfc); |
2891 | jermar | 136 | |
2951 | jermar | 137 | futex_down(&ffn_futex); |
2893 | jermar | 138 | if (!list_empty(&ffn_head)) { |
2951 | jermar | 139 | /* Try to use a cached free node structure. */ |
140 | fat_idx_t *idxp_tmp; |
||
2893 | jermar | 141 | nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
2951 | jermar | 142 | if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
143 | goto skip_cache; |
||
144 | idxp_tmp = nodep->idx; |
||
145 | if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
||
146 | futex_up(&nodep->lock); |
||
147 | goto skip_cache; |
||
148 | } |
||
149 | list_remove(&nodep->ffn_link); |
||
150 | futex_up(&ffn_futex); |
||
2893 | jermar | 151 | if (nodep->dirty) |
152 | fat_node_sync(nodep); |
||
2951 | jermar | 153 | idxp_tmp->nodep = NULL; |
154 | futex_up(&nodep->lock); |
||
155 | futex_up(&idxp_tmp->lock); |
||
2893 | jermar | 156 | } else { |
2951 | jermar | 157 | skip_cache: |
2893 | jermar | 158 | /* Try to allocate a new node structure. */ |
2951 | jermar | 159 | futex_up(&ffn_futex); |
2893 | jermar | 160 | nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
161 | if (!nodep) |
||
162 | return NULL; |
||
163 | } |
||
2891 | jermar | 164 | fat_node_initialize(nodep); |
165 | |||
3530 | jermar | 166 | bs = block_bb_get(idxp->dev_handle); |
167 | bps = uint16_t_le2host(bs->bps); |
||
3550 | jermar | 168 | spc = bs->spc; |
2891 | jermar | 169 | dps = bps / sizeof(fat_dentry_t); |
170 | |||
2893 | jermar | 171 | /* Read the block that contains the dentry of interest. */ |
3530 | jermar | 172 | b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
2951 | jermar | 173 | (idxp->pdi * sizeof(fat_dentry_t)) / bps); |
2891 | jermar | 174 | assert(b); |
175 | |||
2951 | jermar | 176 | d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
2893 | jermar | 177 | if (d->attr & FAT_ATTR_SUBDIR) { |
178 | /* |
||
179 | * The only directory which does not have this bit set is the |
||
180 | * root directory itself. The root directory node is handled |
||
181 | * and initialized elsewhere. |
||
182 | */ |
||
183 | nodep->type = FAT_DIRECTORY; |
||
3282 | jermar | 184 | /* |
3325 | jermar | 185 | * Unfortunately, the 'size' field of the FAT dentry is not |
186 | * defined for the directory entry type. We must determine the |
||
187 | * size of the directory by walking the FAT. |
||
3282 | jermar | 188 | */ |
3550 | jermar | 189 | nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
190 | uint16_t_le2host(d->firstc)); |
||
2893 | jermar | 191 | } else { |
192 | nodep->type = FAT_FILE; |
||
3282 | jermar | 193 | nodep->size = uint32_t_le2host(d->size); |
2893 | jermar | 194 | } |
195 | nodep->firstc = uint16_t_le2host(d->firstc); |
||
196 | nodep->lnkcnt = 1; |
||
197 | nodep->refcnt = 1; |
||
198 | |||
199 | block_put(b); |
||
200 | |||
201 | /* Link the idx structure with the node structure. */ |
||
2951 | jermar | 202 | nodep->idx = idxp; |
203 | idxp->nodep = nodep; |
||
2893 | jermar | 204 | |
205 | return nodep; |
||
2831 | jermar | 206 | } |
207 | |||
2951 | jermar | 208 | /** Instantiate a FAT in-core node. */ |
209 | static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
||
210 | { |
||
211 | void *node; |
||
212 | fat_idx_t *idxp; |
||
213 | |||
214 | idxp = fat_idx_get_by_index(dev_handle, index); |
||
215 | if (!idxp) |
||
216 | return NULL; |
||
217 | /* idxp->lock held */ |
||
218 | node = fat_node_get_core(idxp); |
||
219 | futex_up(&idxp->lock); |
||
220 | return node; |
||
221 | } |
||
222 | |||
2852 | jermar | 223 | static void fat_node_put(void *node) |
224 | { |
||
2910 | jermar | 225 | fat_node_t *nodep = (fat_node_t *)node; |
226 | |||
2951 | jermar | 227 | futex_down(&nodep->lock); |
2910 | jermar | 228 | if (!--nodep->refcnt) { |
2951 | jermar | 229 | futex_down(&ffn_futex); |
2910 | jermar | 230 | list_append(&nodep->ffn_link, &ffn_head); |
2951 | jermar | 231 | futex_up(&ffn_futex); |
2910 | jermar | 232 | } |
2951 | jermar | 233 | futex_up(&nodep->lock); |
2852 | jermar | 234 | } |
235 | |||
2857 | jermar | 236 | static void *fat_create(int flags) |
237 | { |
||
238 | return NULL; /* not supported at the moment */ |
||
239 | } |
||
240 | |||
2858 | jermar | 241 | static int fat_destroy(void *node) |
2857 | jermar | 242 | { |
2858 | jermar | 243 | return ENOTSUP; /* not supported at the moment */ |
2857 | jermar | 244 | } |
245 | |||
246 | static bool fat_link(void *prnt, void *chld, const char *name) |
||
247 | { |
||
248 | return false; /* not supported at the moment */ |
||
249 | } |
||
250 | |||
251 | static int fat_unlink(void *prnt, void *chld) |
||
252 | { |
||
253 | return ENOTSUP; /* not supported at the moment */ |
||
254 | } |
||
255 | |||
2793 | jermar | 256 | static void *fat_match(void *prnt, const char *component) |
257 | { |
||
3530 | jermar | 258 | fat_bs_t *bs; |
2793 | jermar | 259 | fat_node_t *parentp = (fat_node_t *)prnt; |
260 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
2822 | jermar | 261 | unsigned i, j; |
2828 | jermar | 262 | unsigned bps; /* bytes per sector */ |
2822 | jermar | 263 | unsigned dps; /* dentries per sector */ |
264 | unsigned blocks; |
||
2793 | jermar | 265 | fat_dentry_t *d; |
3530 | jermar | 266 | block_t *b; |
2793 | jermar | 267 | |
2953 | jermar | 268 | futex_down(&parentp->idx->lock); |
3530 | jermar | 269 | bs = block_bb_get(parentp->idx->dev_handle); |
270 | bps = uint16_t_le2host(bs->bps); |
||
2828 | jermar | 271 | dps = bps / sizeof(fat_dentry_t); |
3526 | jermar | 272 | blocks = parentp->size / bps; |
2822 | jermar | 273 | for (i = 0; i < blocks; i++) { |
3530 | jermar | 274 | b = fat_block_get(bs, parentp, i); |
3526 | jermar | 275 | for (j = 0; j < dps; j++) { |
2822 | jermar | 276 | d = ((fat_dentry_t *)b->data) + j; |
2845 | jermar | 277 | switch (fat_classify_dentry(d)) { |
278 | case FAT_DENTRY_SKIP: |
||
2822 | jermar | 279 | continue; |
2845 | jermar | 280 | case FAT_DENTRY_LAST: |
2822 | jermar | 281 | block_put(b); |
2953 | jermar | 282 | futex_up(&parentp->idx->lock); |
2822 | jermar | 283 | return NULL; |
2845 | jermar | 284 | default: |
285 | case FAT_DENTRY_VALID: |
||
286 | dentry_name_canonify(d, name); |
||
287 | break; |
||
2822 | jermar | 288 | } |
3272 | jermar | 289 | if (stricmp(name, component) == 0) { |
2822 | jermar | 290 | /* hit */ |
2951 | jermar | 291 | void *node; |
2953 | jermar | 292 | /* |
293 | * Assume tree hierarchy for locking. We |
||
294 | * already have the parent and now we are going |
||
295 | * to lock the child. Never lock in the oposite |
||
296 | * order. |
||
297 | */ |
||
2890 | jermar | 298 | fat_idx_t *idx = fat_idx_get_by_pos( |
2881 | jermar | 299 | parentp->idx->dev_handle, parentp->firstc, |
2864 | jermar | 300 | i * dps + j); |
2953 | jermar | 301 | futex_up(&parentp->idx->lock); |
2890 | jermar | 302 | if (!idx) { |
303 | /* |
||
304 | * Can happen if memory is low or if we |
||
305 | * run out of 32-bit indices. |
||
306 | */ |
||
307 | block_put(b); |
||
308 | return NULL; |
||
309 | } |
||
2951 | jermar | 310 | node = fat_node_get_core(idx); |
311 | futex_up(&idx->lock); |
||
2822 | jermar | 312 | block_put(b); |
313 | return node; |
||
314 | } |
||
2793 | jermar | 315 | } |
2822 | jermar | 316 | block_put(b); |
2639 | jermar | 317 | } |
3516 | jermar | 318 | |
2953 | jermar | 319 | futex_up(&parentp->idx->lock); |
2793 | jermar | 320 | return NULL; |
2638 | jermar | 321 | } |
322 | |||
2831 | jermar | 323 | static fs_index_t fat_index_get(void *node) |
324 | { |
||
325 | fat_node_t *fnodep = (fat_node_t *)node; |
||
326 | if (!fnodep) |
||
327 | return 0; |
||
2864 | jermar | 328 | return fnodep->idx->index; |
2831 | jermar | 329 | } |
330 | |||
331 | static size_t fat_size_get(void *node) |
||
332 | { |
||
333 | return ((fat_node_t *)node)->size; |
||
334 | } |
||
335 | |||
336 | static unsigned fat_lnkcnt_get(void *node) |
||
337 | { |
||
338 | return ((fat_node_t *)node)->lnkcnt; |
||
339 | } |
||
340 | |||
2845 | jermar | 341 | static bool fat_has_children(void *node) |
342 | { |
||
3530 | jermar | 343 | fat_bs_t *bs; |
2845 | jermar | 344 | fat_node_t *nodep = (fat_node_t *)node; |
345 | unsigned bps; |
||
346 | unsigned dps; |
||
347 | unsigned blocks; |
||
3530 | jermar | 348 | block_t *b; |
2845 | jermar | 349 | unsigned i, j; |
350 | |||
351 | if (nodep->type != FAT_DIRECTORY) |
||
352 | return false; |
||
3526 | jermar | 353 | |
2951 | jermar | 354 | futex_down(&nodep->idx->lock); |
3530 | jermar | 355 | bs = block_bb_get(nodep->idx->dev_handle); |
356 | bps = uint16_t_le2host(bs->bps); |
||
2845 | jermar | 357 | dps = bps / sizeof(fat_dentry_t); |
358 | |||
3526 | jermar | 359 | blocks = nodep->size / bps; |
2845 | jermar | 360 | |
361 | for (i = 0; i < blocks; i++) { |
||
362 | fat_dentry_t *d; |
||
363 | |||
3530 | jermar | 364 | b = fat_block_get(bs, nodep, i); |
3526 | jermar | 365 | for (j = 0; j < dps; j++) { |
2845 | jermar | 366 | d = ((fat_dentry_t *)b->data) + j; |
367 | switch (fat_classify_dentry(d)) { |
||
368 | case FAT_DENTRY_SKIP: |
||
369 | continue; |
||
370 | case FAT_DENTRY_LAST: |
||
371 | block_put(b); |
||
2951 | jermar | 372 | futex_up(&nodep->idx->lock); |
2845 | jermar | 373 | return false; |
374 | default: |
||
375 | case FAT_DENTRY_VALID: |
||
376 | block_put(b); |
||
2951 | jermar | 377 | futex_up(&nodep->idx->lock); |
2845 | jermar | 378 | return true; |
379 | } |
||
380 | block_put(b); |
||
2951 | jermar | 381 | futex_up(&nodep->idx->lock); |
2845 | jermar | 382 | return true; |
383 | } |
||
384 | block_put(b); |
||
385 | } |
||
386 | |||
2951 | jermar | 387 | futex_up(&nodep->idx->lock); |
2845 | jermar | 388 | return false; |
389 | } |
||
390 | |||
2844 | jermar | 391 | static void *fat_root_get(dev_handle_t dev_handle) |
392 | { |
||
3119 | jermar | 393 | return fat_node_get(dev_handle, 0); |
2844 | jermar | 394 | } |
395 | |||
396 | static char fat_plb_get_char(unsigned pos) |
||
397 | { |
||
398 | return fat_reg.plb_ro[pos % PLB_SIZE]; |
||
399 | } |
||
400 | |||
2831 | jermar | 401 | static bool fat_is_directory(void *node) |
402 | { |
||
403 | return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
||
404 | } |
||
405 | |||
406 | static bool fat_is_file(void *node) |
||
407 | { |
||
408 | return ((fat_node_t *)node)->type == FAT_FILE; |
||
409 | } |
||
410 | |||
2793 | jermar | 411 | /** libfs operations */ |
412 | libfs_ops_t fat_libfs_ops = { |
||
413 | .match = fat_match, |
||
414 | .node_get = fat_node_get, |
||
2852 | jermar | 415 | .node_put = fat_node_put, |
2857 | jermar | 416 | .create = fat_create, |
417 | .destroy = fat_destroy, |
||
418 | .link = fat_link, |
||
419 | .unlink = fat_unlink, |
||
2831 | jermar | 420 | .index_get = fat_index_get, |
421 | .size_get = fat_size_get, |
||
422 | .lnkcnt_get = fat_lnkcnt_get, |
||
2845 | jermar | 423 | .has_children = fat_has_children, |
2844 | jermar | 424 | .root_get = fat_root_get, |
425 | .plb_get_char = fat_plb_get_char, |
||
2831 | jermar | 426 | .is_directory = fat_is_directory, |
427 | .is_file = fat_is_file |
||
2793 | jermar | 428 | }; |
429 | |||
3110 | jermar | 430 | void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
431 | { |
||
432 | dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
||
3530 | jermar | 433 | fat_bs_t *bs; |
3257 | jermar | 434 | uint16_t bps; |
3119 | jermar | 435 | uint16_t rde; |
3110 | jermar | 436 | int rc; |
437 | |||
3530 | jermar | 438 | /* initialize libblock */ |
3537 | jermar | 439 | rc = block_init(dev_handle, BS_SIZE); |
3257 | jermar | 440 | if (rc != EOK) { |
3537 | jermar | 441 | ipc_answer_0(rid, rc); |
3257 | jermar | 442 | return; |
443 | } |
||
444 | |||
3537 | jermar | 445 | /* prepare the boot block */ |
446 | rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
||
447 | if (rc != EOK) { |
||
448 | block_fini(dev_handle); |
||
449 | ipc_answer_0(rid, rc); |
||
450 | return; |
||
451 | } |
||
452 | |||
3530 | jermar | 453 | /* get the buffer with the boot sector */ |
454 | bs = block_bb_get(dev_handle); |
||
455 | |||
3119 | jermar | 456 | /* Read the number of root directory entries. */ |
3530 | jermar | 457 | bps = uint16_t_le2host(bs->bps); |
458 | rde = uint16_t_le2host(bs->root_ent_max); |
||
3119 | jermar | 459 | |
3257 | jermar | 460 | if (bps != BS_SIZE) { |
3530 | jermar | 461 | block_fini(dev_handle); |
3257 | jermar | 462 | ipc_answer_0(rid, ENOTSUP); |
463 | return; |
||
464 | } |
||
465 | |||
3539 | jermar | 466 | /* Initialize the block cache */ |
467 | rc = block_cache_init(dev_handle, bps, 0 /* XXX */); |
||
468 | if (rc != EOK) { |
||
469 | block_fini(dev_handle); |
||
470 | ipc_answer_0(rid, rc); |
||
471 | return; |
||
472 | } |
||
473 | |||
3110 | jermar | 474 | rc = fat_idx_init_by_dev_handle(dev_handle); |
475 | if (rc != EOK) { |
||
3530 | jermar | 476 | block_fini(dev_handle); |
3110 | jermar | 477 | ipc_answer_0(rid, rc); |
478 | return; |
||
479 | } |
||
480 | |||
3119 | jermar | 481 | /* Initialize the root node. */ |
482 | fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
||
483 | if (!rootp) { |
||
3530 | jermar | 484 | block_fini(dev_handle); |
3119 | jermar | 485 | fat_idx_fini_by_dev_handle(dev_handle); |
486 | ipc_answer_0(rid, ENOMEM); |
||
487 | return; |
||
488 | } |
||
489 | fat_node_initialize(rootp); |
||
490 | |||
491 | fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
||
492 | if (!ridxp) { |
||
3530 | jermar | 493 | block_fini(dev_handle); |
3119 | jermar | 494 | free(rootp); |
495 | fat_idx_fini_by_dev_handle(dev_handle); |
||
496 | ipc_answer_0(rid, ENOMEM); |
||
497 | return; |
||
498 | } |
||
499 | assert(ridxp->index == 0); |
||
500 | /* ridxp->lock held */ |
||
501 | |||
502 | rootp->type = FAT_DIRECTORY; |
||
503 | rootp->firstc = FAT_CLST_ROOT; |
||
504 | rootp->refcnt = 1; |
||
3352 | jermar | 505 | rootp->lnkcnt = 0; /* FS root is not linked */ |
3119 | jermar | 506 | rootp->size = rde * sizeof(fat_dentry_t); |
507 | rootp->idx = ridxp; |
||
508 | ridxp->nodep = rootp; |
||
509 | |||
510 | futex_up(&ridxp->lock); |
||
511 | |||
3352 | jermar | 512 | ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
3110 | jermar | 513 | } |
514 | |||
515 | void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
||
516 | { |
||
517 | ipc_answer_0(rid, ENOTSUP); |
||
518 | } |
||
519 | |||
2627 | jermar | 520 | void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
521 | { |
||
2793 | jermar | 522 | libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
2627 | jermar | 523 | } |
524 | |||
3307 | jermar | 525 | void fat_read(ipc_callid_t rid, ipc_call_t *request) |
526 | { |
||
527 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
||
528 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
529 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
530 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
3530 | jermar | 531 | fat_bs_t *bs; |
3516 | jermar | 532 | uint16_t bps; |
3308 | jermar | 533 | size_t bytes; |
3530 | jermar | 534 | block_t *b; |
3308 | jermar | 535 | |
3307 | jermar | 536 | if (!nodep) { |
537 | ipc_answer_0(rid, ENOENT); |
||
538 | return; |
||
539 | } |
||
540 | |||
541 | ipc_callid_t callid; |
||
542 | size_t len; |
||
3314 | jermar | 543 | if (!ipc_data_read_receive(&callid, &len)) { |
3307 | jermar | 544 | fat_node_put(nodep); |
545 | ipc_answer_0(callid, EINVAL); |
||
546 | ipc_answer_0(rid, EINVAL); |
||
547 | return; |
||
548 | } |
||
549 | |||
3530 | jermar | 550 | bs = block_bb_get(dev_handle); |
551 | bps = uint16_t_le2host(bs->bps); |
||
3516 | jermar | 552 | |
3307 | jermar | 553 | if (nodep->type == FAT_FILE) { |
3335 | jermar | 554 | /* |
555 | * Our strategy for regular file reads is to read one block at |
||
556 | * most and make use of the possibility to return less data than |
||
557 | * requested. This keeps the code very simple. |
||
558 | */ |
||
3532 | jermar | 559 | if (pos >= nodep->size) { |
3533 | jermar | 560 | /* reading beyond the EOF */ |
561 | bytes = 0; |
||
3532 | jermar | 562 | (void) ipc_data_read_finalize(callid, NULL, 0); |
563 | } else { |
||
564 | bytes = min(len, bps - pos % bps); |
||
565 | bytes = min(bytes, nodep->size - pos); |
||
566 | b = fat_block_get(bs, nodep, pos / bps); |
||
567 | (void) ipc_data_read_finalize(callid, b->data + pos % bps, |
||
568 | bytes); |
||
569 | block_put(b); |
||
570 | } |
||
3307 | jermar | 571 | } else { |
3335 | jermar | 572 | unsigned bnum; |
573 | off_t spos = pos; |
||
574 | char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
||
575 | fat_dentry_t *d; |
||
576 | |||
3307 | jermar | 577 | assert(nodep->type == FAT_DIRECTORY); |
3335 | jermar | 578 | assert(nodep->size % bps == 0); |
579 | assert(bps % sizeof(fat_dentry_t) == 0); |
||
580 | |||
581 | /* |
||
582 | * Our strategy for readdir() is to use the position pointer as |
||
583 | * an index into the array of all dentries. On entry, it points |
||
584 | * to the first unread dentry. If we skip any dentries, we bump |
||
585 | * the position pointer accordingly. |
||
586 | */ |
||
587 | bnum = (pos * sizeof(fat_dentry_t)) / bps; |
||
588 | while (bnum < nodep->size / bps) { |
||
589 | off_t o; |
||
590 | |||
3530 | jermar | 591 | b = fat_block_get(bs, nodep, bnum); |
3335 | jermar | 592 | for (o = pos % (bps / sizeof(fat_dentry_t)); |
593 | o < bps / sizeof(fat_dentry_t); |
||
594 | o++, pos++) { |
||
595 | d = ((fat_dentry_t *)b->data) + o; |
||
596 | switch (fat_classify_dentry(d)) { |
||
597 | case FAT_DENTRY_SKIP: |
||
598 | continue; |
||
599 | case FAT_DENTRY_LAST: |
||
600 | block_put(b); |
||
601 | goto miss; |
||
602 | default: |
||
603 | case FAT_DENTRY_VALID: |
||
604 | dentry_name_canonify(d, name); |
||
605 | block_put(b); |
||
606 | goto hit; |
||
607 | } |
||
608 | } |
||
609 | block_put(b); |
||
610 | bnum++; |
||
611 | } |
||
612 | miss: |
||
3307 | jermar | 613 | fat_node_put(nodep); |
3335 | jermar | 614 | ipc_answer_0(callid, ENOENT); |
615 | ipc_answer_1(rid, ENOENT, 0); |
||
3307 | jermar | 616 | return; |
3335 | jermar | 617 | hit: |
618 | (void) ipc_data_read_finalize(callid, name, strlen(name) + 1); |
||
619 | bytes = (pos - spos) + 1; |
||
3307 | jermar | 620 | } |
621 | |||
622 | fat_node_put(nodep); |
||
3308 | jermar | 623 | ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
3307 | jermar | 624 | } |
625 | |||
3497 | jermar | 626 | void fat_write(ipc_callid_t rid, ipc_call_t *request) |
627 | { |
||
3499 | jermar | 628 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
629 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
630 | off_t pos = (off_t)IPC_GET_ARG3(*request); |
||
631 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
3530 | jermar | 632 | fat_bs_t *bs; |
3499 | jermar | 633 | size_t bytes; |
3530 | jermar | 634 | block_t *b; |
3499 | jermar | 635 | uint16_t bps; |
636 | unsigned spc; |
||
3572 | jermar | 637 | unsigned bpc; /* bytes per cluster */ |
3501 | jermar | 638 | off_t boundary; |
3499 | jermar | 639 | |
640 | if (!nodep) { |
||
641 | ipc_answer_0(rid, ENOENT); |
||
642 | return; |
||
643 | } |
||
644 | |||
645 | ipc_callid_t callid; |
||
646 | size_t len; |
||
647 | if (!ipc_data_write_receive(&callid, &len)) { |
||
648 | fat_node_put(nodep); |
||
649 | ipc_answer_0(callid, EINVAL); |
||
650 | ipc_answer_0(rid, EINVAL); |
||
651 | return; |
||
652 | } |
||
653 | |||
3572 | jermar | 654 | bs = block_bb_get(dev_handle); |
655 | bps = uint16_t_le2host(bs->bps); |
||
656 | spc = bs->spc; |
||
657 | bpc = bps * spc; |
||
658 | |||
3499 | jermar | 659 | /* |
660 | * In all scenarios, we will attempt to write out only one block worth |
||
661 | * of data at maximum. There might be some more efficient approaches, |
||
662 | * but this one greatly simplifies fat_write(). Note that we can afford |
||
663 | * to do this because the client must be ready to handle the return |
||
664 | * value signalizing a smaller number of bytes written. |
||
665 | */ |
||
666 | bytes = min(len, bps - pos % bps); |
||
667 | |||
3572 | jermar | 668 | boundary = ROUND_UP(nodep->size, bpc); |
3501 | jermar | 669 | if (pos < boundary) { |
3499 | jermar | 670 | /* |
671 | * This is the easier case - we are either overwriting already |
||
672 | * existing contents or writing behind the EOF, but still within |
||
673 | * the limits of the last cluster. The node size may grow to the |
||
674 | * next block size boundary. |
||
675 | */ |
||
3530 | jermar | 676 | fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
677 | b = fat_block_get(bs, nodep, pos / bps); |
||
3499 | jermar | 678 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
679 | bytes); |
||
680 | b->dirty = true; /* need to sync block */ |
||
3500 | jermar | 681 | block_put(b); |
3499 | jermar | 682 | if (pos + bytes > nodep->size) { |
683 | nodep->size = pos + bytes; |
||
684 | nodep->dirty = true; /* need to sync node */ |
||
685 | } |
||
686 | fat_node_put(nodep); |
||
687 | ipc_answer_1(rid, EOK, bytes); |
||
688 | return; |
||
689 | } else { |
||
690 | /* |
||
691 | * This is the more difficult case. We must allocate new |
||
692 | * clusters for the node and zero them out. |
||
693 | */ |
||
3500 | jermar | 694 | int status; |
3499 | jermar | 695 | unsigned nclsts; |
3548 | jermar | 696 | fat_cluster_t mcl, lcl; |
697 | |||
3572 | jermar | 698 | nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
3500 | jermar | 699 | /* create an independent chain of nclsts clusters in all FATs */ |
3572 | jermar | 700 | status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
3500 | jermar | 701 | if (status != EOK) { |
702 | /* could not allocate a chain of nclsts clusters */ |
||
703 | fat_node_put(nodep); |
||
704 | ipc_answer_0(callid, status); |
||
705 | ipc_answer_0(rid, status); |
||
706 | return; |
||
707 | } |
||
708 | /* zero fill any gaps */ |
||
3530 | jermar | 709 | fat_fill_gap(bs, nodep, mcl, pos); |
3548 | jermar | 710 | b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc); |
3500 | jermar | 711 | (void) ipc_data_write_finalize(callid, b->data + pos % bps, |
712 | bytes); |
||
3501 | jermar | 713 | b->dirty = true; /* need to sync block */ |
3500 | jermar | 714 | block_put(b); |
715 | /* |
||
716 | * Append the cluster chain starting in mcl to the end of the |
||
717 | * node's cluster chain. |
||
718 | */ |
||
3530 | jermar | 719 | fat_append_clusters(bs, nodep, mcl); |
3500 | jermar | 720 | nodep->size = pos + bytes; |
3501 | jermar | 721 | nodep->dirty = true; /* need to sync node */ |
3500 | jermar | 722 | fat_node_put(nodep); |
723 | ipc_answer_1(rid, EOK, bytes); |
||
724 | return; |
||
3499 | jermar | 725 | } |
3497 | jermar | 726 | } |
727 | |||
3546 | jermar | 728 | void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
729 | { |
||
3548 | jermar | 730 | dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
731 | fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
||
732 | size_t size = (off_t)IPC_GET_ARG3(*request); |
||
733 | fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
||
3572 | jermar | 734 | fat_bs_t *bs; |
735 | uint16_t bps; |
||
736 | uint8_t spc; |
||
737 | unsigned bpc; /* bytes per cluster */ |
||
3548 | jermar | 738 | int rc; |
739 | |||
740 | if (!nodep) { |
||
741 | ipc_answer_0(rid, ENOENT); |
||
742 | return; |
||
743 | } |
||
744 | |||
3572 | jermar | 745 | bs = block_bb_get(dev_handle); |
746 | bps = uint16_t_le2host(bs->bps); |
||
747 | spc = bs->spc; |
||
748 | bpc = bps * spc; |
||
749 | |||
3548 | jermar | 750 | if (nodep->size == size) { |
751 | rc = EOK; |
||
752 | } else if (nodep->size < size) { |
||
753 | /* |
||
3572 | jermar | 754 | * The standard says we have the freedom to grow the node. |
3548 | jermar | 755 | * For now, we simply return an error. |
756 | */ |
||
757 | rc = EINVAL; |
||
3572 | jermar | 758 | } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
759 | /* |
||
760 | * The node will be shrunk, but no clusters will be deallocated. |
||
761 | */ |
||
762 | nodep->size = size; |
||
763 | nodep->dirty = true; /* need to sync node */ |
||
764 | rc = EOK; |
||
3548 | jermar | 765 | } else { |
766 | /* |
||
3572 | jermar | 767 | * The node will be shrunk, clusters will be deallocated. |
3548 | jermar | 768 | */ |
3572 | jermar | 769 | if (size == 0) { |
770 | fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
||
771 | } else { |
||
772 | fat_cluster_t lastc; |
||
773 | (void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
||
774 | &lastc, (size - 1) / bpc); |
||
775 | fat_chop_clusters(bs, nodep, lastc); |
||
776 | } |
||
777 | nodep->size = size; |
||
778 | nodep->dirty = true; /* need to sync node */ |
||
779 | rc = EOK; |
||
3548 | jermar | 780 | } |
781 | fat_node_put(nodep); |
||
782 | ipc_answer_0(rid, rc); |
||
783 | return; |
||
3546 | jermar | 784 | } |
785 | |||
2627 | jermar | 786 | /** |
787 | * @} |
||
788 | */ |