source: mainline/uspace/srv/fs/fat/fat_ops.c@ e32b65a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e32b65a was e32b65a, checked in by Jakub Jermar <jakub@…>, 17 years ago

Add the ability to grow a FAT directory.

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