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

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

Make fat_cluster_walk() return an error code.

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