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

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

Make fat_append_clusters(), fat_chop_clusters(), fat_free_clusters(),
fat_alloc_shadow_clusters(), fat_set_cluster(), fat_fill_gap() and
fat_zero_cluster() return an error code.

  • Property mode set to 100644
File size: 30.9 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 (void) fat_free_clusters(bs, dev_handle, mcl);
332 return NULL;
333 }
334 idxp = fat_idx_get_new(dev_handle);
335 if (!idxp) {
336 (void) 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 rc = fat_zero_cluster(bs, dev_handle, mcl);
344 assert(rc == EOK);
345 nodep->type = FAT_DIRECTORY;
346 nodep->firstc = mcl;
347 nodep->size = bps * bs->spc;
348 } else {
349 nodep->type = FAT_FILE;
350 nodep->firstc = FAT_CLST_RES0;
351 nodep->size = 0;
352 }
353 nodep->lnkcnt = 0; /* not linked anywhere */
354 nodep->refcnt = 1;
355 nodep->dirty = true;
356
357 nodep->idx = idxp;
358 idxp->nodep = nodep;
359
360 fibril_mutex_unlock(&idxp->lock);
361 return FS_NODE(nodep);
362}
363
364int fat_destroy_node(fs_node_t *fn)
365{
366 fat_node_t *nodep = FAT_NODE(fn);
367 fat_bs_t *bs;
368 int rc = EOK;
369
370 /*
371 * The node is not reachable from the file system. This means that the
372 * link count should be zero and that the index structure cannot be
373 * found in the position hash. Obviously, we don't need to lock the node
374 * nor its index structure.
375 */
376 assert(nodep->lnkcnt == 0);
377
378 /*
379 * The node may not have any children.
380 */
381 assert(fat_has_children(fn) == false);
382
383 bs = block_bb_get(nodep->idx->dev_handle);
384 if (nodep->firstc != FAT_CLST_RES0) {
385 assert(nodep->size);
386 /* Free all clusters allocated to the node. */
387 rc = fat_free_clusters(bs, nodep->idx->dev_handle,
388 nodep->firstc);
389 }
390
391 fat_idx_destroy(nodep->idx);
392 free(nodep->bp);
393 free(nodep);
394 return rc;
395}
396
397int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
398{
399 fat_node_t *parentp = FAT_NODE(pfn);
400 fat_node_t *childp = FAT_NODE(cfn);
401 fat_dentry_t *d;
402 fat_bs_t *bs;
403 block_t *b;
404 unsigned i, j;
405 uint16_t bps;
406 unsigned dps;
407 unsigned blocks;
408 fat_cluster_t mcl, lcl;
409 int rc;
410
411 fibril_mutex_lock(&childp->lock);
412 if (childp->lnkcnt == 1) {
413 /*
414 * On FAT, we don't support multiple hard links.
415 */
416 fibril_mutex_unlock(&childp->lock);
417 return EMLINK;
418 }
419 assert(childp->lnkcnt == 0);
420 fibril_mutex_unlock(&childp->lock);
421
422 if (!fat_dentry_name_verify(name)) {
423 /*
424 * Attempt to create unsupported name.
425 */
426 return ENOTSUP;
427 }
428
429 /*
430 * Get us an unused parent node's dentry or grow the parent and allocate
431 * a new one.
432 */
433
434 fibril_mutex_lock(&parentp->idx->lock);
435 bs = block_bb_get(parentp->idx->dev_handle);
436 bps = uint16_t_le2host(bs->bps);
437 dps = bps / sizeof(fat_dentry_t);
438
439 blocks = parentp->size / bps;
440
441 for (i = 0; i < blocks; i++) {
442 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
443 assert(rc == EOK);
444 for (j = 0; j < dps; j++) {
445 d = ((fat_dentry_t *)b->data) + j;
446 switch (fat_classify_dentry(d)) {
447 case FAT_DENTRY_SKIP:
448 case FAT_DENTRY_VALID:
449 /* skipping used and meta entries */
450 continue;
451 case FAT_DENTRY_FREE:
452 case FAT_DENTRY_LAST:
453 /* found an empty slot */
454 goto hit;
455 }
456 }
457 rc = block_put(b);
458 assert(rc == EOK);
459 }
460 j = 0;
461
462 /*
463 * We need to grow the parent in order to create a new unused dentry.
464 */
465 if (parentp->firstc == FAT_CLST_ROOT) {
466 /* Can't grow the root directory. */
467 fibril_mutex_unlock(&parentp->idx->lock);
468 return ENOSPC;
469 }
470 rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
471 if (rc != EOK) {
472 fibril_mutex_unlock(&parentp->idx->lock);
473 return rc;
474 }
475 rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
476 assert(rc == EOK);
477 rc = fat_append_clusters(bs, parentp, mcl);
478 assert(rc == EOK);
479 parentp->size += bps * bs->spc;
480 parentp->dirty = true; /* need to sync node */
481 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
482 assert(rc == EOK);
483 d = (fat_dentry_t *)b->data;
484
485hit:
486 /*
487 * At this point we only establish the link between the parent and the
488 * child. The dentry, except of the name and the extension, will remain
489 * uninitialized until the corresponding node is synced. Thus the valid
490 * dentry data is kept in the child node structure.
491 */
492 memset(d, 0, sizeof(fat_dentry_t));
493 fat_dentry_name_set(d, name);
494 b->dirty = true; /* need to sync block */
495 rc = block_put(b);
496 assert(rc == EOK);
497 fibril_mutex_unlock(&parentp->idx->lock);
498
499 fibril_mutex_lock(&childp->idx->lock);
500
501 /*
502 * If possible, create the Sub-directory Identifier Entry and the
503 * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
504 * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
505 * not use them anyway, so this is rather a sign of our good will.
506 */
507 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
508 assert(rc == EOK);
509 d = (fat_dentry_t *)b->data;
510 if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
511 str_cmp(d->name, FAT_NAME_DOT) == 0) {
512 memset(d, 0, sizeof(fat_dentry_t));
513 str_cpy(d->name, 8, FAT_NAME_DOT);
514 str_cpy(d->ext, 3, FAT_EXT_PAD);
515 d->attr = FAT_ATTR_SUBDIR;
516 d->firstc = host2uint16_t_le(childp->firstc);
517 /* TODO: initialize also the date/time members. */
518 }
519 d++;
520 if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
521 str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
522 memset(d, 0, sizeof(fat_dentry_t));
523 str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
524 str_cpy(d->ext, 3, FAT_EXT_PAD);
525 d->attr = FAT_ATTR_SUBDIR;
526 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
527 host2uint16_t_le(FAT_CLST_RES0) :
528 host2uint16_t_le(parentp->firstc);
529 /* TODO: initialize also the date/time members. */
530 }
531 b->dirty = true; /* need to sync block */
532 rc = block_put(b);
533 assert(rc == EOK);
534
535 childp->idx->pfc = parentp->firstc;
536 childp->idx->pdi = i * dps + j;
537 fibril_mutex_unlock(&childp->idx->lock);
538
539 fibril_mutex_lock(&childp->lock);
540 childp->lnkcnt = 1;
541 childp->dirty = true; /* need to sync node */
542 fibril_mutex_unlock(&childp->lock);
543
544 /*
545 * Hash in the index structure into the position hash.
546 */
547 fat_idx_hashin(childp->idx);
548
549 return EOK;
550}
551
552int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
553{
554 fat_node_t *parentp = FAT_NODE(pfn);
555 fat_node_t *childp = FAT_NODE(cfn);
556 fat_bs_t *bs;
557 fat_dentry_t *d;
558 uint16_t bps;
559 block_t *b;
560 int rc;
561
562 if (!parentp)
563 return EBUSY;
564
565 if (fat_has_children(cfn))
566 return ENOTEMPTY;
567
568 fibril_mutex_lock(&parentp->lock);
569 fibril_mutex_lock(&childp->lock);
570 assert(childp->lnkcnt == 1);
571 fibril_mutex_lock(&childp->idx->lock);
572 bs = block_bb_get(childp->idx->dev_handle);
573 bps = uint16_t_le2host(bs->bps);
574
575 rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
576 (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
577 BLOCK_FLAGS_NONE);
578 assert(rc == EOK);
579 d = (fat_dentry_t *)b->data +
580 (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
581 /* mark the dentry as not-currently-used */
582 d->name[0] = FAT_DENTRY_ERASED;
583 b->dirty = true; /* need to sync block */
584 rc = block_put(b);
585 assert(rc == EOK);
586
587 /* remove the index structure from the position hash */
588 fat_idx_hashout(childp->idx);
589 /* clear position information */
590 childp->idx->pfc = FAT_CLST_RES0;
591 childp->idx->pdi = 0;
592 fibril_mutex_unlock(&childp->idx->lock);
593 childp->lnkcnt = 0;
594 childp->dirty = true;
595 fibril_mutex_unlock(&childp->lock);
596 fibril_mutex_unlock(&parentp->lock);
597
598 return EOK;
599}
600
601fs_node_t *fat_match(fs_node_t *pfn, const char *component)
602{
603 fat_bs_t *bs;
604 fat_node_t *parentp = FAT_NODE(pfn);
605 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
606 unsigned i, j;
607 unsigned bps; /* bytes per sector */
608 unsigned dps; /* dentries per sector */
609 unsigned blocks;
610 fat_dentry_t *d;
611 block_t *b;
612 int rc;
613
614 fibril_mutex_lock(&parentp->idx->lock);
615 bs = block_bb_get(parentp->idx->dev_handle);
616 bps = uint16_t_le2host(bs->bps);
617 dps = bps / sizeof(fat_dentry_t);
618 blocks = parentp->size / bps;
619 for (i = 0; i < blocks; i++) {
620 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
621 assert(rc == EOK);
622 for (j = 0; j < dps; j++) {
623 d = ((fat_dentry_t *)b->data) + j;
624 switch (fat_classify_dentry(d)) {
625 case FAT_DENTRY_SKIP:
626 case FAT_DENTRY_FREE:
627 continue;
628 case FAT_DENTRY_LAST:
629 rc = block_put(b);
630 assert(rc == EOK);
631 fibril_mutex_unlock(&parentp->idx->lock);
632 return NULL;
633 default:
634 case FAT_DENTRY_VALID:
635 fat_dentry_name_get(d, name);
636 break;
637 }
638 if (fat_dentry_namecmp(name, component) == 0) {
639 /* hit */
640 fat_node_t *nodep;
641 /*
642 * Assume tree hierarchy for locking. We
643 * already have the parent and now we are going
644 * to lock the child. Never lock in the oposite
645 * order.
646 */
647 fat_idx_t *idx = fat_idx_get_by_pos(
648 parentp->idx->dev_handle, parentp->firstc,
649 i * dps + j);
650 fibril_mutex_unlock(&parentp->idx->lock);
651 if (!idx) {
652 /*
653 * Can happen if memory is low or if we
654 * run out of 32-bit indices.
655 */
656 rc = block_put(b);
657 assert(rc == EOK);
658 return NULL;
659 }
660 nodep = fat_node_get_core(idx);
661 fibril_mutex_unlock(&idx->lock);
662 rc = block_put(b);
663 assert(rc == EOK);
664 return FS_NODE(nodep);
665 }
666 }
667 rc = block_put(b);
668 assert(rc == EOK);
669 }
670
671 fibril_mutex_unlock(&parentp->idx->lock);
672 return NULL;
673}
674
675fs_index_t fat_index_get(fs_node_t *fn)
676{
677 return FAT_NODE(fn)->idx->index;
678}
679
680size_t fat_size_get(fs_node_t *fn)
681{
682 return FAT_NODE(fn)->size;
683}
684
685unsigned fat_lnkcnt_get(fs_node_t *fn)
686{
687 return FAT_NODE(fn)->lnkcnt;
688}
689
690bool fat_has_children(fs_node_t *fn)
691{
692 fat_bs_t *bs;
693 fat_node_t *nodep = FAT_NODE(fn);
694 unsigned bps;
695 unsigned dps;
696 unsigned blocks;
697 block_t *b;
698 unsigned i, j;
699 int rc;
700
701 if (nodep->type != FAT_DIRECTORY)
702 return false;
703
704 fibril_mutex_lock(&nodep->idx->lock);
705 bs = block_bb_get(nodep->idx->dev_handle);
706 bps = uint16_t_le2host(bs->bps);
707 dps = bps / sizeof(fat_dentry_t);
708
709 blocks = nodep->size / bps;
710
711 for (i = 0; i < blocks; i++) {
712 fat_dentry_t *d;
713
714 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
715 assert(rc == EOK);
716 for (j = 0; j < dps; j++) {
717 d = ((fat_dentry_t *)b->data) + j;
718 switch (fat_classify_dentry(d)) {
719 case FAT_DENTRY_SKIP:
720 case FAT_DENTRY_FREE:
721 continue;
722 case FAT_DENTRY_LAST:
723 rc = block_put(b);
724 assert(rc == EOK);
725 fibril_mutex_unlock(&nodep->idx->lock);
726 return false;
727 default:
728 case FAT_DENTRY_VALID:
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 fibril_mutex_unlock(&nodep->idx->lock);
737 return true;
738 }
739 rc = block_put(b);
740 assert(rc == EOK);
741 }
742
743 fibril_mutex_unlock(&nodep->idx->lock);
744 return false;
745}
746
747fs_node_t *fat_root_get(dev_handle_t dev_handle)
748{
749 return fat_node_get(dev_handle, 0);
750}
751
752char fat_plb_get_char(unsigned pos)
753{
754 return fat_reg.plb_ro[pos % PLB_SIZE];
755}
756
757bool fat_is_directory(fs_node_t *fn)
758{
759 return FAT_NODE(fn)->type == FAT_DIRECTORY;
760}
761
762bool fat_is_file(fs_node_t *fn)
763{
764 return FAT_NODE(fn)->type == FAT_FILE;
765}
766
767/** libfs operations */
768libfs_ops_t fat_libfs_ops = {
769 .match = fat_match,
770 .node_get = fat_node_get,
771 .node_put = fat_node_put,
772 .create = fat_create_node,
773 .destroy = fat_destroy_node,
774 .link = fat_link,
775 .unlink = fat_unlink,
776 .index_get = fat_index_get,
777 .size_get = fat_size_get,
778 .lnkcnt_get = fat_lnkcnt_get,
779 .has_children = fat_has_children,
780 .root_get = fat_root_get,
781 .plb_get_char = fat_plb_get_char,
782 .is_directory = fat_is_directory,
783 .is_file = fat_is_file
784};
785
786/*
787 * VFS operations.
788 */
789
790void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
791{
792 dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
793 enum cache_mode cmode;
794 fat_bs_t *bs;
795 uint16_t bps;
796 uint16_t rde;
797 int rc;
798
799 /* accept the mount options */
800 ipc_callid_t callid;
801 size_t size;
802 if (!ipc_data_write_receive(&callid, &size)) {
803 ipc_answer_0(callid, EINVAL);
804 ipc_answer_0(rid, EINVAL);
805 return;
806 }
807 char *opts = malloc(size + 1);
808 if (!opts) {
809 ipc_answer_0(callid, ENOMEM);
810 ipc_answer_0(rid, ENOMEM);
811 return;
812 }
813 ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
814 if (retval != EOK) {
815 ipc_answer_0(rid, retval);
816 free(opts);
817 return;
818 }
819 opts[size] = '\0';
820
821 /* Check for option enabling write through. */
822 if (str_cmp(opts, "wtcache") == 0)
823 cmode = CACHE_MODE_WT;
824 else
825 cmode = CACHE_MODE_WB;
826
827 /* initialize libblock */
828 rc = block_init(dev_handle, BS_SIZE);
829 if (rc != EOK) {
830 ipc_answer_0(rid, rc);
831 return;
832 }
833
834 /* prepare the boot block */
835 rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
836 if (rc != EOK) {
837 block_fini(dev_handle);
838 ipc_answer_0(rid, rc);
839 return;
840 }
841
842 /* get the buffer with the boot sector */
843 bs = block_bb_get(dev_handle);
844
845 /* Read the number of root directory entries. */
846 bps = uint16_t_le2host(bs->bps);
847 rde = uint16_t_le2host(bs->root_ent_max);
848
849 if (bps != BS_SIZE) {
850 block_fini(dev_handle);
851 ipc_answer_0(rid, ENOTSUP);
852 return;
853 }
854
855 /* Initialize the block cache */
856 rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
857 if (rc != EOK) {
858 block_fini(dev_handle);
859 ipc_answer_0(rid, rc);
860 return;
861 }
862
863 rc = fat_idx_init_by_dev_handle(dev_handle);
864 if (rc != EOK) {
865 block_fini(dev_handle);
866 ipc_answer_0(rid, rc);
867 return;
868 }
869
870 /* Initialize the root node. */
871 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
872 if (!rfn) {
873 block_fini(dev_handle);
874 fat_idx_fini_by_dev_handle(dev_handle);
875 ipc_answer_0(rid, ENOMEM);
876 return;
877 }
878 fs_node_initialize(rfn);
879 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
880 if (!rootp) {
881 free(rfn);
882 block_fini(dev_handle);
883 fat_idx_fini_by_dev_handle(dev_handle);
884 ipc_answer_0(rid, ENOMEM);
885 return;
886 }
887 fat_node_initialize(rootp);
888
889 fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
890 if (!ridxp) {
891 free(rfn);
892 free(rootp);
893 block_fini(dev_handle);
894 fat_idx_fini_by_dev_handle(dev_handle);
895 ipc_answer_0(rid, ENOMEM);
896 return;
897 }
898 assert(ridxp->index == 0);
899 /* ridxp->lock held */
900
901 rootp->type = FAT_DIRECTORY;
902 rootp->firstc = FAT_CLST_ROOT;
903 rootp->refcnt = 1;
904 rootp->lnkcnt = 0; /* FS root is not linked */
905 rootp->size = rde * sizeof(fat_dentry_t);
906 rootp->idx = ridxp;
907 ridxp->nodep = rootp;
908 rootp->bp = rfn;
909 rfn->data = rootp;
910
911 fibril_mutex_unlock(&ridxp->lock);
912
913 ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
914}
915
916void fat_mount(ipc_callid_t rid, ipc_call_t *request)
917{
918 libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
919}
920
921void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
922{
923 libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
924}
925
926void fat_read(ipc_callid_t rid, ipc_call_t *request)
927{
928 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
929 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
930 off_t pos = (off_t)IPC_GET_ARG3(*request);
931 fs_node_t *fn = fat_node_get(dev_handle, index);
932 fat_node_t *nodep;
933 fat_bs_t *bs;
934 uint16_t bps;
935 size_t bytes;
936 block_t *b;
937 int rc;
938
939 if (!fn) {
940 ipc_answer_0(rid, ENOENT);
941 return;
942 }
943 nodep = FAT_NODE(fn);
944
945 ipc_callid_t callid;
946 size_t len;
947 if (!ipc_data_read_receive(&callid, &len)) {
948 fat_node_put(fn);
949 ipc_answer_0(callid, EINVAL);
950 ipc_answer_0(rid, EINVAL);
951 return;
952 }
953
954 bs = block_bb_get(dev_handle);
955 bps = uint16_t_le2host(bs->bps);
956
957 if (nodep->type == FAT_FILE) {
958 /*
959 * Our strategy for regular file reads is to read one block at
960 * most and make use of the possibility to return less data than
961 * requested. This keeps the code very simple.
962 */
963 if (pos >= nodep->size) {
964 /* reading beyond the EOF */
965 bytes = 0;
966 (void) ipc_data_read_finalize(callid, NULL, 0);
967 } else {
968 bytes = min(len, bps - pos % bps);
969 bytes = min(bytes, nodep->size - pos);
970 rc = fat_block_get(&b, bs, nodep, pos / bps,
971 BLOCK_FLAGS_NONE);
972 assert(rc == EOK);
973 (void) ipc_data_read_finalize(callid, b->data + pos % bps,
974 bytes);
975 rc = block_put(b);
976 assert(rc == EOK);
977 }
978 } else {
979 unsigned bnum;
980 off_t spos = pos;
981 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
982 fat_dentry_t *d;
983
984 assert(nodep->type == FAT_DIRECTORY);
985 assert(nodep->size % bps == 0);
986 assert(bps % sizeof(fat_dentry_t) == 0);
987
988 /*
989 * Our strategy for readdir() is to use the position pointer as
990 * an index into the array of all dentries. On entry, it points
991 * to the first unread dentry. If we skip any dentries, we bump
992 * the position pointer accordingly.
993 */
994 bnum = (pos * sizeof(fat_dentry_t)) / bps;
995 while (bnum < nodep->size / bps) {
996 off_t o;
997
998 rc = fat_block_get(&b, bs, nodep, bnum,
999 BLOCK_FLAGS_NONE);
1000 assert(rc == EOK);
1001 for (o = pos % (bps / sizeof(fat_dentry_t));
1002 o < bps / sizeof(fat_dentry_t);
1003 o++, pos++) {
1004 d = ((fat_dentry_t *)b->data) + o;
1005 switch (fat_classify_dentry(d)) {
1006 case FAT_DENTRY_SKIP:
1007 case FAT_DENTRY_FREE:
1008 continue;
1009 case FAT_DENTRY_LAST:
1010 rc = block_put(b);
1011 assert(rc == EOK);
1012 goto miss;
1013 default:
1014 case FAT_DENTRY_VALID:
1015 fat_dentry_name_get(d, name);
1016 rc == block_put(b);
1017 assert(rc == EOK);
1018 goto hit;
1019 }
1020 }
1021 rc = block_put(b);
1022 assert(rc == EOK);
1023 bnum++;
1024 }
1025miss:
1026 fat_node_put(fn);
1027 ipc_answer_0(callid, ENOENT);
1028 ipc_answer_1(rid, ENOENT, 0);
1029 return;
1030hit:
1031 (void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
1032 bytes = (pos - spos) + 1;
1033 }
1034
1035 fat_node_put(fn);
1036 ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
1037}
1038
1039void fat_write(ipc_callid_t rid, ipc_call_t *request)
1040{
1041 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1042 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1043 off_t pos = (off_t)IPC_GET_ARG3(*request);
1044 fs_node_t *fn = fat_node_get(dev_handle, index);
1045 fat_node_t *nodep;
1046 fat_bs_t *bs;
1047 size_t bytes;
1048 block_t *b;
1049 uint16_t bps;
1050 unsigned spc;
1051 unsigned bpc; /* bytes per cluster */
1052 off_t boundary;
1053 int flags = BLOCK_FLAGS_NONE;
1054 int rc;
1055
1056 if (!fn) {
1057 ipc_answer_0(rid, ENOENT);
1058 return;
1059 }
1060 nodep = FAT_NODE(fn);
1061
1062 ipc_callid_t callid;
1063 size_t len;
1064 if (!ipc_data_write_receive(&callid, &len)) {
1065 fat_node_put(fn);
1066 ipc_answer_0(callid, EINVAL);
1067 ipc_answer_0(rid, EINVAL);
1068 return;
1069 }
1070
1071 bs = block_bb_get(dev_handle);
1072 bps = uint16_t_le2host(bs->bps);
1073 spc = bs->spc;
1074 bpc = bps * spc;
1075
1076 /*
1077 * In all scenarios, we will attempt to write out only one block worth
1078 * of data at maximum. There might be some more efficient approaches,
1079 * but this one greatly simplifies fat_write(). Note that we can afford
1080 * to do this because the client must be ready to handle the return
1081 * value signalizing a smaller number of bytes written.
1082 */
1083 bytes = min(len, bps - pos % bps);
1084 if (bytes == bps)
1085 flags |= BLOCK_FLAGS_NOREAD;
1086
1087 boundary = ROUND_UP(nodep->size, bpc);
1088 if (pos < boundary) {
1089 /*
1090 * This is the easier case - we are either overwriting already
1091 * existing contents or writing behind the EOF, but still within
1092 * the limits of the last cluster. The node size may grow to the
1093 * next block size boundary.
1094 */
1095 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
1096 assert(rc == EOK);
1097 rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
1098 assert(rc == EOK);
1099 (void) ipc_data_write_finalize(callid, b->data + pos % bps,
1100 bytes);
1101 b->dirty = true; /* need to sync block */
1102 rc = block_put(b);
1103 assert(rc == EOK);
1104 if (pos + bytes > nodep->size) {
1105 nodep->size = pos + bytes;
1106 nodep->dirty = true; /* need to sync node */
1107 }
1108 ipc_answer_2(rid, EOK, bytes, nodep->size);
1109 fat_node_put(fn);
1110 return;
1111 } else {
1112 /*
1113 * This is the more difficult case. We must allocate new
1114 * clusters for the node and zero them out.
1115 */
1116 int status;
1117 unsigned nclsts;
1118 fat_cluster_t mcl, lcl;
1119
1120 nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1121 /* create an independent chain of nclsts clusters in all FATs */
1122 status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1123 if (status != EOK) {
1124 /* could not allocate a chain of nclsts clusters */
1125 fat_node_put(fn);
1126 ipc_answer_0(callid, status);
1127 ipc_answer_0(rid, status);
1128 return;
1129 }
1130 /* zero fill any gaps */
1131 rc = fat_fill_gap(bs, nodep, mcl, pos);
1132 assert(rc == EOK);
1133 rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
1134 flags);
1135 assert(rc == EOK);
1136 (void) ipc_data_write_finalize(callid, b->data + pos % bps,
1137 bytes);
1138 b->dirty = true; /* need to sync block */
1139 rc = block_put(b);
1140 assert(rc == EOK);
1141 /*
1142 * Append the cluster chain starting in mcl to the end of the
1143 * node's cluster chain.
1144 */
1145 rc = fat_append_clusters(bs, nodep, mcl);
1146 assert(rc == EOK);
1147 nodep->size = pos + bytes;
1148 nodep->dirty = true; /* need to sync node */
1149 ipc_answer_2(rid, EOK, bytes, nodep->size);
1150 fat_node_put(fn);
1151 return;
1152 }
1153}
1154
1155void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1156{
1157 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1158 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1159 size_t size = (off_t)IPC_GET_ARG3(*request);
1160 fs_node_t *fn = fat_node_get(dev_handle, index);
1161 fat_node_t *nodep;
1162 fat_bs_t *bs;
1163 uint16_t bps;
1164 uint8_t spc;
1165 unsigned bpc; /* bytes per cluster */
1166 int rc;
1167
1168 if (!fn) {
1169 ipc_answer_0(rid, ENOENT);
1170 return;
1171 }
1172 nodep = FAT_NODE(fn);
1173
1174 bs = block_bb_get(dev_handle);
1175 bps = uint16_t_le2host(bs->bps);
1176 spc = bs->spc;
1177 bpc = bps * spc;
1178
1179 if (nodep->size == size) {
1180 rc = EOK;
1181 } else if (nodep->size < size) {
1182 /*
1183 * The standard says we have the freedom to grow the node.
1184 * For now, we simply return an error.
1185 */
1186 rc = EINVAL;
1187 } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
1188 /*
1189 * The node will be shrunk, but no clusters will be deallocated.
1190 */
1191 nodep->size = size;
1192 nodep->dirty = true; /* need to sync node */
1193 rc = EOK;
1194 } else {
1195 /*
1196 * The node will be shrunk, clusters will be deallocated.
1197 */
1198 if (size == 0) {
1199 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1200 if (rc != EOK)
1201 goto out;
1202 } else {
1203 fat_cluster_t lastc;
1204 rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
1205 &lastc, NULL, (size - 1) / bpc);
1206 if (rc != EOK)
1207 goto out;
1208 rc = fat_chop_clusters(bs, nodep, lastc);
1209 if (rc != EOK)
1210 goto out;
1211 }
1212 nodep->size = size;
1213 nodep->dirty = true; /* need to sync node */
1214 rc = EOK;
1215 }
1216out:
1217 fat_node_put(fn);
1218 ipc_answer_0(rid, rc);
1219 return;
1220}
1221
1222void fat_close(ipc_callid_t rid, ipc_call_t *request)
1223{
1224 ipc_answer_0(rid, EOK);
1225}
1226
1227void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1228{
1229 dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1230 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1231 int rc;
1232
1233 fs_node_t *fn = fat_node_get(dev_handle, index);
1234 if (!fn) {
1235 ipc_answer_0(rid, ENOENT);
1236 return;
1237 }
1238
1239 rc = fat_destroy_node(fn);
1240 ipc_answer_0(rid, rc);
1241}
1242
1243void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
1244{
1245 libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1246}
1247
1248void fat_stat(ipc_callid_t rid, ipc_call_t *request)
1249{
1250 libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1251}
1252
1253void fat_sync(ipc_callid_t rid, ipc_call_t *request)
1254{
1255 /* Dummy implementation */
1256 ipc_answer_0(rid, EOK);
1257}
1258
1259/**
1260 * @}
1261 */
Note: See TracBrowser for help on using the repository browser.