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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ef90ffb3 was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

  • Property mode set to 100644
File size: 37.7 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/services.h>
45#include <ipc/devmap.h>
46#include <macros.h>
47#include <async.h>
48#include <errno.h>
49#include <str.h>
50#include <byteorder.h>
51#include <adt/hash_table.h>
52#include <adt/list.h>
53#include <assert.h>
54#include <fibril_synch.h>
55#include <sys/mman.h>
56#include <align.h>
57#include <malloc.h>
58
59#define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL)
60#define FS_NODE(node) ((node) ? (node)->bp : NULL)
61
62#define DPS(bs) (BPS((bs)) / sizeof(fat_dentry_t))
63#define BPC(bs) (BPS((bs)) * SPC((bs)))
64
65/** Mutex protecting the list of cached free FAT nodes. */
66static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
67
68/** List of cached free FAT nodes. */
69static LIST_INITIALIZE(ffn_head);
70
71/*
72 * Forward declarations of FAT libfs operations.
73 */
74static int fat_root_get(fs_node_t **, devmap_handle_t);
75static int fat_match(fs_node_t **, fs_node_t *, const char *);
76static int fat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
77static int fat_node_open(fs_node_t *);
78static int fat_node_put(fs_node_t *);
79static int fat_create_node(fs_node_t **, devmap_handle_t, int);
80static int fat_destroy_node(fs_node_t *);
81static int fat_link(fs_node_t *, fs_node_t *, const char *);
82static int fat_unlink(fs_node_t *, fs_node_t *, const char *);
83static int fat_has_children(bool *, fs_node_t *);
84static fs_index_t fat_index_get(fs_node_t *);
85static aoff64_t fat_size_get(fs_node_t *);
86static unsigned fat_lnkcnt_get(fs_node_t *);
87static char fat_plb_get_char(unsigned);
88static bool fat_is_directory(fs_node_t *);
89static bool fat_is_file(fs_node_t *node);
90static devmap_handle_t fat_device_get(fs_node_t *node);
91
92/*
93 * Helper functions.
94 */
95static void fat_node_initialize(fat_node_t *node)
96{
97 fibril_mutex_initialize(&node->lock);
98 node->bp = NULL;
99 node->idx = NULL;
100 node->type = 0;
101 link_initialize(&node->ffn_link);
102 node->size = 0;
103 node->lnkcnt = 0;
104 node->refcnt = 0;
105 node->dirty = false;
106 node->lastc_cached_valid = false;
107 node->lastc_cached_value = FAT_CLST_LAST1;
108 node->currc_cached_valid = false;
109 node->currc_cached_bn = 0;
110 node->currc_cached_value = FAT_CLST_LAST1;
111}
112
113static int fat_node_sync(fat_node_t *node)
114{
115 block_t *b;
116 fat_bs_t *bs;
117 fat_dentry_t *d;
118 int rc;
119
120 assert(node->dirty);
121
122 bs = block_bb_get(node->idx->devmap_handle);
123
124 /* Read the block that contains the dentry of interest. */
125 rc = _fat_block_get(&b, bs, node->idx->devmap_handle, node->idx->pfc,
126 NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
127 BLOCK_FLAGS_NONE);
128 if (rc != EOK)
129 return rc;
130
131 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));
132
133 d->firstc = host2uint16_t_le(node->firstc);
134 if (node->type == FAT_FILE) {
135 d->size = host2uint32_t_le(node->size);
136 } else if (node->type == FAT_DIRECTORY) {
137 d->attr = FAT_ATTR_SUBDIR;
138 }
139
140 /* TODO: update other fields? (e.g time fields) */
141
142 b->dirty = true; /* need to sync block */
143 rc = block_put(b);
144 return rc;
145}
146
147static int fat_node_fini_by_devmap_handle(devmap_handle_t devmap_handle)
148{
149 link_t *lnk;
150 fat_node_t *nodep;
151 int rc;
152
153 /*
154 * We are called from fat_unmounted() and assume that there are already
155 * no nodes belonging to this instance with non-zero refcount. Therefore
156 * it is sufficient to clean up only the FAT free node list.
157 */
158
159restart:
160 fibril_mutex_lock(&ffn_mutex);
161 for (lnk = ffn_head.next; lnk != &ffn_head; lnk = lnk->next) {
162 nodep = list_get_instance(lnk, fat_node_t, ffn_link);
163 if (!fibril_mutex_trylock(&nodep->lock)) {
164 fibril_mutex_unlock(&ffn_mutex);
165 goto restart;
166 }
167 if (!fibril_mutex_trylock(&nodep->idx->lock)) {
168 fibril_mutex_unlock(&nodep->lock);
169 fibril_mutex_unlock(&ffn_mutex);
170 goto restart;
171 }
172 if (nodep->idx->devmap_handle != devmap_handle) {
173 fibril_mutex_unlock(&nodep->idx->lock);
174 fibril_mutex_unlock(&nodep->lock);
175 continue;
176 }
177
178 list_remove(&nodep->ffn_link);
179 fibril_mutex_unlock(&ffn_mutex);
180
181 /*
182 * We can unlock the node and its index structure because we are
183 * the last player on this playground and VFS is preventing new
184 * players from entering.
185 */
186 fibril_mutex_unlock(&nodep->idx->lock);
187 fibril_mutex_unlock(&nodep->lock);
188
189 if (nodep->dirty) {
190 rc = fat_node_sync(nodep);
191 if (rc != EOK)
192 return rc;
193 }
194 nodep->idx->nodep = NULL;
195 free(nodep->bp);
196 free(nodep);
197
198 /* Need to restart because we changed the ffn_head list. */
199 goto restart;
200 }
201 fibril_mutex_unlock(&ffn_mutex);
202
203 return EOK;
204}
205
206static int fat_node_get_new(fat_node_t **nodepp)
207{
208 fs_node_t *fn;
209 fat_node_t *nodep;
210 int rc;
211
212 fibril_mutex_lock(&ffn_mutex);
213 if (!list_empty(&ffn_head)) {
214 /* Try to use a cached free node structure. */
215 fat_idx_t *idxp_tmp;
216 nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
217 if (!fibril_mutex_trylock(&nodep->lock))
218 goto skip_cache;
219 idxp_tmp = nodep->idx;
220 if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
221 fibril_mutex_unlock(&nodep->lock);
222 goto skip_cache;
223 }
224 list_remove(&nodep->ffn_link);
225 fibril_mutex_unlock(&ffn_mutex);
226 if (nodep->dirty) {
227 rc = fat_node_sync(nodep);
228 if (rc != EOK) {
229 idxp_tmp->nodep = NULL;
230 fibril_mutex_unlock(&nodep->lock);
231 fibril_mutex_unlock(&idxp_tmp->lock);
232 free(nodep->bp);
233 free(nodep);
234 return rc;
235 }
236 }
237 idxp_tmp->nodep = NULL;
238 fibril_mutex_unlock(&nodep->lock);
239 fibril_mutex_unlock(&idxp_tmp->lock);
240 fn = FS_NODE(nodep);
241 } else {
242skip_cache:
243 /* Try to allocate a new node structure. */
244 fibril_mutex_unlock(&ffn_mutex);
245 fn = (fs_node_t *)malloc(sizeof(fs_node_t));
246 if (!fn)
247 return ENOMEM;
248 nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
249 if (!nodep) {
250 free(fn);
251 return ENOMEM;
252 }
253 }
254 fat_node_initialize(nodep);
255 fs_node_initialize(fn);
256 fn->data = nodep;
257 nodep->bp = fn;
258
259 *nodepp = nodep;
260 return EOK;
261}
262
263/** Internal version of fat_node_get().
264 *
265 * @param idxp Locked index structure.
266 */
267static int fat_node_get_core(fat_node_t **nodepp, fat_idx_t *idxp)
268{
269 block_t *b;
270 fat_bs_t *bs;
271 fat_dentry_t *d;
272 fat_node_t *nodep = NULL;
273 int rc;
274
275 if (idxp->nodep) {
276 /*
277 * We are lucky.
278 * The node is already instantiated in memory.
279 */
280 fibril_mutex_lock(&idxp->nodep->lock);
281 if (!idxp->nodep->refcnt++) {
282 fibril_mutex_lock(&ffn_mutex);
283 list_remove(&idxp->nodep->ffn_link);
284 fibril_mutex_unlock(&ffn_mutex);
285 }
286 fibril_mutex_unlock(&idxp->nodep->lock);
287 *nodepp = idxp->nodep;
288 return EOK;
289 }
290
291 /*
292 * We must instantiate the node from the file system.
293 */
294
295 assert(idxp->pfc);
296
297 rc = fat_node_get_new(&nodep);
298 if (rc != EOK)
299 return rc;
300
301 bs = block_bb_get(idxp->devmap_handle);
302
303 /* Read the block that contains the dentry of interest. */
304 rc = _fat_block_get(&b, bs, idxp->devmap_handle, idxp->pfc, NULL,
305 (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
306 if (rc != EOK) {
307 (void) fat_node_put(FS_NODE(nodep));
308 return rc;
309 }
310
311 d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
312 if (d->attr & FAT_ATTR_SUBDIR) {
313 /*
314 * The only directory which does not have this bit set is the
315 * root directory itself. The root directory node is handled
316 * and initialized elsewhere.
317 */
318 nodep->type = FAT_DIRECTORY;
319 /*
320 * Unfortunately, the 'size' field of the FAT dentry is not
321 * defined for the directory entry type. We must determine the
322 * size of the directory by walking the FAT.
323 */
324 uint16_t clusters;
325 rc = fat_clusters_get(&clusters, bs, idxp->devmap_handle,
326 uint16_t_le2host(d->firstc));
327 if (rc != EOK) {
328 (void) block_put(b);
329 (void) fat_node_put(FS_NODE(nodep));
330 return rc;
331 }
332 nodep->size = BPS(bs) * SPC(bs) * clusters;
333 } else {
334 nodep->type = FAT_FILE;
335 nodep->size = uint32_t_le2host(d->size);
336 }
337 nodep->firstc = uint16_t_le2host(d->firstc);
338 nodep->lnkcnt = 1;
339 nodep->refcnt = 1;
340
341 rc = block_put(b);
342 if (rc != EOK) {
343 (void) fat_node_put(FS_NODE(nodep));
344 return rc;
345 }
346
347 /* Link the idx structure with the node structure. */
348 nodep->idx = idxp;
349 idxp->nodep = nodep;
350
351 *nodepp = nodep;
352 return EOK;
353}
354
355/*
356 * FAT libfs operations.
357 */
358
359int fat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
360{
361 return fat_node_get(rfn, devmap_handle, 0);
362}
363
364int fat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
365{
366 fat_bs_t *bs;
367 fat_node_t *parentp = FAT_NODE(pfn);
368 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
369 unsigned i, j;
370 unsigned blocks;
371 fat_dentry_t *d;
372 devmap_handle_t devmap_handle;
373 block_t *b;
374 int rc;
375
376 fibril_mutex_lock(&parentp->idx->lock);
377 devmap_handle = parentp->idx->devmap_handle;
378 fibril_mutex_unlock(&parentp->idx->lock);
379
380 bs = block_bb_get(devmap_handle);
381 blocks = parentp->size / BPS(bs);
382 for (i = 0; i < blocks; i++) {
383 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
384 if (rc != EOK)
385 return rc;
386 for (j = 0; j < DPS(bs); j++) {
387 d = ((fat_dentry_t *)b->data) + j;
388 switch (fat_classify_dentry(d)) {
389 case FAT_DENTRY_SKIP:
390 case FAT_DENTRY_FREE:
391 continue;
392 case FAT_DENTRY_LAST:
393 /* miss */
394 rc = block_put(b);
395 *rfn = NULL;
396 return rc;
397 default:
398 case FAT_DENTRY_VALID:
399 fat_dentry_name_get(d, name);
400 break;
401 }
402 if (fat_dentry_namecmp(name, component) == 0) {
403 /* hit */
404 fat_node_t *nodep;
405 fat_idx_t *idx = fat_idx_get_by_pos(devmap_handle,
406 parentp->firstc, i * DPS(bs) + j);
407 if (!idx) {
408 /*
409 * Can happen if memory is low or if we
410 * run out of 32-bit indices.
411 */
412 rc = block_put(b);
413 return (rc == EOK) ? ENOMEM : rc;
414 }
415 rc = fat_node_get_core(&nodep, idx);
416 fibril_mutex_unlock(&idx->lock);
417 if (rc != EOK) {
418 (void) block_put(b);
419 return rc;
420 }
421 *rfn = FS_NODE(nodep);
422 rc = block_put(b);
423 if (rc != EOK)
424 (void) fat_node_put(*rfn);
425 return rc;
426 }
427 }
428 rc = block_put(b);
429 if (rc != EOK)
430 return rc;
431 }
432
433 *rfn = NULL;
434 return EOK;
435}
436
437/** Instantiate a FAT in-core node. */
438int fat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
439{
440 fat_node_t *nodep;
441 fat_idx_t *idxp;
442 int rc;
443
444 idxp = fat_idx_get_by_index(devmap_handle, index);
445 if (!idxp) {
446 *rfn = NULL;
447 return EOK;
448 }
449 /* idxp->lock held */
450 rc = fat_node_get_core(&nodep, idxp);
451 fibril_mutex_unlock(&idxp->lock);
452 if (rc == EOK)
453 *rfn = FS_NODE(nodep);
454 return rc;
455}
456
457int fat_node_open(fs_node_t *fn)
458{
459 /*
460 * Opening a file is stateless, nothing
461 * to be done here.
462 */
463 return EOK;
464}
465
466int fat_node_put(fs_node_t *fn)
467{
468 fat_node_t *nodep = FAT_NODE(fn);
469 bool destroy = false;
470
471 fibril_mutex_lock(&nodep->lock);
472 if (!--nodep->refcnt) {
473 if (nodep->idx) {
474 fibril_mutex_lock(&ffn_mutex);
475 list_append(&nodep->ffn_link, &ffn_head);
476 fibril_mutex_unlock(&ffn_mutex);
477 } else {
478 /*
479 * The node does not have any index structure associated
480 * with itself. This can only mean that we are releasing
481 * the node after a failed attempt to allocate the index
482 * structure for it.
483 */
484 destroy = true;
485 }
486 }
487 fibril_mutex_unlock(&nodep->lock);
488 if (destroy) {
489 free(nodep->bp);
490 free(nodep);
491 }
492 return EOK;
493}
494
495int fat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
496{
497 fat_idx_t *idxp;
498 fat_node_t *nodep;
499 fat_bs_t *bs;
500 fat_cluster_t mcl, lcl;
501 int rc;
502
503 bs = block_bb_get(devmap_handle);
504 if (flags & L_DIRECTORY) {
505 /* allocate a cluster */
506 rc = fat_alloc_clusters(bs, devmap_handle, 1, &mcl, &lcl);
507 if (rc != EOK)
508 return rc;
509 /* populate the new cluster with unused dentries */
510 rc = fat_zero_cluster(bs, devmap_handle, mcl);
511 if (rc != EOK) {
512 (void) fat_free_clusters(bs, devmap_handle, mcl);
513 return rc;
514 }
515 }
516
517 rc = fat_node_get_new(&nodep);
518 if (rc != EOK) {
519 (void) fat_free_clusters(bs, devmap_handle, mcl);
520 return rc;
521 }
522 rc = fat_idx_get_new(&idxp, devmap_handle);
523 if (rc != EOK) {
524 (void) fat_free_clusters(bs, devmap_handle, mcl);
525 (void) fat_node_put(FS_NODE(nodep));
526 return rc;
527 }
528 /* idxp->lock held */
529 if (flags & L_DIRECTORY) {
530 nodep->type = FAT_DIRECTORY;
531 nodep->firstc = mcl;
532 nodep->size = BPS(bs) * SPC(bs);
533 } else {
534 nodep->type = FAT_FILE;
535 nodep->firstc = FAT_CLST_RES0;
536 nodep->size = 0;
537 }
538 nodep->lnkcnt = 0; /* not linked anywhere */
539 nodep->refcnt = 1;
540 nodep->dirty = true;
541
542 nodep->idx = idxp;
543 idxp->nodep = nodep;
544
545 fibril_mutex_unlock(&idxp->lock);
546 *rfn = FS_NODE(nodep);
547 return EOK;
548}
549
550int fat_destroy_node(fs_node_t *fn)
551{
552 fat_node_t *nodep = FAT_NODE(fn);
553 fat_bs_t *bs;
554 bool has_children;
555 int rc;
556
557 /*
558 * The node is not reachable from the file system. This means that the
559 * link count should be zero and that the index structure cannot be
560 * found in the position hash. Obviously, we don't need to lock the node
561 * nor its index structure.
562 */
563 assert(nodep->lnkcnt == 0);
564
565 /*
566 * The node may not have any children.
567 */
568 rc = fat_has_children(&has_children, fn);
569 if (rc != EOK)
570 return rc;
571 assert(!has_children);
572
573 bs = block_bb_get(nodep->idx->devmap_handle);
574 if (nodep->firstc != FAT_CLST_RES0) {
575 assert(nodep->size);
576 /* Free all clusters allocated to the node. */
577 rc = fat_free_clusters(bs, nodep->idx->devmap_handle,
578 nodep->firstc);
579 }
580
581 fat_idx_destroy(nodep->idx);
582 free(nodep->bp);
583 free(nodep);
584 return rc;
585}
586
587int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
588{
589 fat_node_t *parentp = FAT_NODE(pfn);
590 fat_node_t *childp = FAT_NODE(cfn);
591 fat_dentry_t *d;
592 fat_bs_t *bs;
593 block_t *b;
594 unsigned i, j;
595 unsigned blocks;
596 fat_cluster_t mcl, lcl;
597 int rc;
598
599 fibril_mutex_lock(&childp->lock);
600 if (childp->lnkcnt == 1) {
601 /*
602 * On FAT, we don't support multiple hard links.
603 */
604 fibril_mutex_unlock(&childp->lock);
605 return EMLINK;
606 }
607 assert(childp->lnkcnt == 0);
608 fibril_mutex_unlock(&childp->lock);
609
610 if (!fat_dentry_name_verify(name)) {
611 /*
612 * Attempt to create unsupported name.
613 */
614 return ENOTSUP;
615 }
616
617 /*
618 * Get us an unused parent node's dentry or grow the parent and allocate
619 * a new one.
620 */
621
622 fibril_mutex_lock(&parentp->idx->lock);
623 bs = block_bb_get(parentp->idx->devmap_handle);
624
625 blocks = parentp->size / BPS(bs);
626
627 for (i = 0; i < blocks; i++) {
628 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
629 if (rc != EOK) {
630 fibril_mutex_unlock(&parentp->idx->lock);
631 return rc;
632 }
633 for (j = 0; j < DPS(bs); j++) {
634 d = ((fat_dentry_t *)b->data) + j;
635 switch (fat_classify_dentry(d)) {
636 case FAT_DENTRY_SKIP:
637 case FAT_DENTRY_VALID:
638 /* skipping used and meta entries */
639 continue;
640 case FAT_DENTRY_FREE:
641 case FAT_DENTRY_LAST:
642 /* found an empty slot */
643 goto hit;
644 }
645 }
646 rc = block_put(b);
647 if (rc != EOK) {
648 fibril_mutex_unlock(&parentp->idx->lock);
649 return rc;
650 }
651 }
652 j = 0;
653
654 /*
655 * We need to grow the parent in order to create a new unused dentry.
656 */
657 if (parentp->firstc == FAT_CLST_ROOT) {
658 /* Can't grow the root directory. */
659 fibril_mutex_unlock(&parentp->idx->lock);
660 return ENOSPC;
661 }
662 rc = fat_alloc_clusters(bs, parentp->idx->devmap_handle, 1, &mcl, &lcl);
663 if (rc != EOK) {
664 fibril_mutex_unlock(&parentp->idx->lock);
665 return rc;
666 }
667 rc = fat_zero_cluster(bs, parentp->idx->devmap_handle, mcl);
668 if (rc != EOK) {
669 (void) fat_free_clusters(bs, parentp->idx->devmap_handle, mcl);
670 fibril_mutex_unlock(&parentp->idx->lock);
671 return rc;
672 }
673 rc = fat_append_clusters(bs, parentp, mcl, lcl);
674 if (rc != EOK) {
675 (void) fat_free_clusters(bs, parentp->idx->devmap_handle, mcl);
676 fibril_mutex_unlock(&parentp->idx->lock);
677 return rc;
678 }
679 parentp->size += BPS(bs) * SPC(bs);
680 parentp->dirty = true; /* need to sync node */
681 rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
682 if (rc != EOK) {
683 fibril_mutex_unlock(&parentp->idx->lock);
684 return rc;
685 }
686 d = (fat_dentry_t *)b->data;
687
688hit:
689 /*
690 * At this point we only establish the link between the parent and the
691 * child. The dentry, except of the name and the extension, will remain
692 * uninitialized until the corresponding node is synced. Thus the valid
693 * dentry data is kept in the child node structure.
694 */
695 memset(d, 0, sizeof(fat_dentry_t));
696 fat_dentry_name_set(d, name);
697 b->dirty = true; /* need to sync block */
698 rc = block_put(b);
699 fibril_mutex_unlock(&parentp->idx->lock);
700 if (rc != EOK)
701 return rc;
702
703 fibril_mutex_lock(&childp->idx->lock);
704
705 if (childp->type == FAT_DIRECTORY) {
706 /*
707 * If possible, create the Sub-directory Identifier Entry and
708 * the Sub-directory Parent Pointer Entry (i.e. "." and "..").
709 * These entries are not mandatory according to Standard
710 * ECMA-107 and HelenOS VFS does not use them anyway, so this is
711 * rather a sign of our good will.
712 */
713 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
714 if (rc != EOK) {
715 /*
716 * Rather than returning an error, simply skip the
717 * creation of these two entries.
718 */
719 goto skip_dots;
720 }
721 d = (fat_dentry_t *) b->data;
722 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
723 (str_cmp((char *) d->name, FAT_NAME_DOT)) == 0) {
724 memset(d, 0, sizeof(fat_dentry_t));
725 memcpy(d->name, FAT_NAME_DOT, FAT_NAME_LEN);
726 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
727 d->attr = FAT_ATTR_SUBDIR;
728 d->firstc = host2uint16_t_le(childp->firstc);
729 /* TODO: initialize also the date/time members. */
730 }
731 d++;
732 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
733 (str_cmp((char *) d->name, FAT_NAME_DOT_DOT) == 0)) {
734 memset(d, 0, sizeof(fat_dentry_t));
735 memcpy(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN);
736 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
737 d->attr = FAT_ATTR_SUBDIR;
738 d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
739 host2uint16_t_le(FAT_CLST_RES0) :
740 host2uint16_t_le(parentp->firstc);
741 /* TODO: initialize also the date/time members. */
742 }
743 b->dirty = true; /* need to sync block */
744 /*
745 * Ignore the return value as we would have fallen through on error
746 * anyway.
747 */
748 (void) block_put(b);
749 }
750skip_dots:
751
752 childp->idx->pfc = parentp->firstc;
753 childp->idx->pdi = i * DPS(bs) + j;
754 fibril_mutex_unlock(&childp->idx->lock);
755
756 fibril_mutex_lock(&childp->lock);
757 childp->lnkcnt = 1;
758 childp->dirty = true; /* need to sync node */
759 fibril_mutex_unlock(&childp->lock);
760
761 /*
762 * Hash in the index structure into the position hash.
763 */
764 fat_idx_hashin(childp->idx);
765
766 return EOK;
767}
768
769int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
770{
771 fat_node_t *parentp = FAT_NODE(pfn);
772 fat_node_t *childp = FAT_NODE(cfn);
773 fat_bs_t *bs;
774 fat_dentry_t *d;
775 block_t *b;
776 bool has_children;
777 int rc;
778
779 if (!parentp)
780 return EBUSY;
781
782 rc = fat_has_children(&has_children, cfn);
783 if (rc != EOK)
784 return rc;
785 if (has_children)
786 return ENOTEMPTY;
787
788 fibril_mutex_lock(&parentp->lock);
789 fibril_mutex_lock(&childp->lock);
790 assert(childp->lnkcnt == 1);
791 fibril_mutex_lock(&childp->idx->lock);
792 bs = block_bb_get(childp->idx->devmap_handle);
793
794 rc = _fat_block_get(&b, bs, childp->idx->devmap_handle, childp->idx->pfc,
795 NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
796 BLOCK_FLAGS_NONE);
797 if (rc != EOK)
798 goto error;
799 d = (fat_dentry_t *)b->data +
800 (childp->idx->pdi % (BPS(bs) / sizeof(fat_dentry_t)));
801 /* mark the dentry as not-currently-used */
802 d->name[0] = FAT_DENTRY_ERASED;
803 b->dirty = true; /* need to sync block */
804 rc = block_put(b);
805 if (rc != EOK)
806 goto error;
807
808 /* remove the index structure from the position hash */
809 fat_idx_hashout(childp->idx);
810 /* clear position information */
811 childp->idx->pfc = FAT_CLST_RES0;
812 childp->idx->pdi = 0;
813 fibril_mutex_unlock(&childp->idx->lock);
814 childp->lnkcnt = 0;
815 childp->refcnt++; /* keep the node in memory until destroyed */
816 childp->dirty = true;
817 fibril_mutex_unlock(&childp->lock);
818 fibril_mutex_unlock(&parentp->lock);
819
820 return EOK;
821
822error:
823 fibril_mutex_unlock(&parentp->idx->lock);
824 fibril_mutex_unlock(&childp->lock);
825 fibril_mutex_unlock(&childp->idx->lock);
826 return rc;
827}
828
829int fat_has_children(bool *has_children, fs_node_t *fn)
830{
831 fat_bs_t *bs;
832 fat_node_t *nodep = FAT_NODE(fn);
833 unsigned blocks;
834 block_t *b;
835 unsigned i, j;
836 int rc;
837
838 if (nodep->type != FAT_DIRECTORY) {
839 *has_children = false;
840 return EOK;
841 }
842
843 fibril_mutex_lock(&nodep->idx->lock);
844 bs = block_bb_get(nodep->idx->devmap_handle);
845
846 blocks = nodep->size / BPS(bs);
847
848 for (i = 0; i < blocks; i++) {
849 fat_dentry_t *d;
850
851 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
852 if (rc != EOK) {
853 fibril_mutex_unlock(&nodep->idx->lock);
854 return rc;
855 }
856 for (j = 0; j < DPS(bs); j++) {
857 d = ((fat_dentry_t *)b->data) + j;
858 switch (fat_classify_dentry(d)) {
859 case FAT_DENTRY_SKIP:
860 case FAT_DENTRY_FREE:
861 continue;
862 case FAT_DENTRY_LAST:
863 rc = block_put(b);
864 fibril_mutex_unlock(&nodep->idx->lock);
865 *has_children = false;
866 return rc;
867 default:
868 case FAT_DENTRY_VALID:
869 rc = block_put(b);
870 fibril_mutex_unlock(&nodep->idx->lock);
871 *has_children = true;
872 return rc;
873 }
874 }
875 rc = block_put(b);
876 if (rc != EOK) {
877 fibril_mutex_unlock(&nodep->idx->lock);
878 return rc;
879 }
880 }
881
882 fibril_mutex_unlock(&nodep->idx->lock);
883 *has_children = false;
884 return EOK;
885}
886
887
888fs_index_t fat_index_get(fs_node_t *fn)
889{
890 return FAT_NODE(fn)->idx->index;
891}
892
893aoff64_t fat_size_get(fs_node_t *fn)
894{
895 return FAT_NODE(fn)->size;
896}
897
898unsigned fat_lnkcnt_get(fs_node_t *fn)
899{
900 return FAT_NODE(fn)->lnkcnt;
901}
902
903char fat_plb_get_char(unsigned pos)
904{
905 return fat_reg.plb_ro[pos % PLB_SIZE];
906}
907
908bool fat_is_directory(fs_node_t *fn)
909{
910 return FAT_NODE(fn)->type == FAT_DIRECTORY;
911}
912
913bool fat_is_file(fs_node_t *fn)
914{
915 return FAT_NODE(fn)->type == FAT_FILE;
916}
917
918devmap_handle_t fat_device_get(fs_node_t *node)
919{
920 return 0;
921}
922
923/** libfs operations */
924libfs_ops_t fat_libfs_ops = {
925 .root_get = fat_root_get,
926 .match = fat_match,
927 .node_get = fat_node_get,
928 .node_open = fat_node_open,
929 .node_put = fat_node_put,
930 .create = fat_create_node,
931 .destroy = fat_destroy_node,
932 .link = fat_link,
933 .unlink = fat_unlink,
934 .has_children = fat_has_children,
935 .index_get = fat_index_get,
936 .size_get = fat_size_get,
937 .lnkcnt_get = fat_lnkcnt_get,
938 .plb_get_char = fat_plb_get_char,
939 .is_directory = fat_is_directory,
940 .is_file = fat_is_file,
941 .device_get = fat_device_get
942};
943
944/*
945 * VFS operations.
946 */
947
948void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
949{
950 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
951 enum cache_mode cmode;
952 fat_bs_t *bs;
953
954 /* Accept the mount options */
955 char *opts;
956 int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
957
958 if (rc != EOK) {
959 async_answer_0(rid, rc);
960 return;
961 }
962
963 /* Check for option enabling write through. */
964 if (str_cmp(opts, "wtcache") == 0)
965 cmode = CACHE_MODE_WT;
966 else
967 cmode = CACHE_MODE_WB;
968
969 free(opts);
970
971 /* initialize libblock */
972 rc = block_init(devmap_handle, BS_SIZE);
973 if (rc != EOK) {
974 async_answer_0(rid, rc);
975 return;
976 }
977
978 /* prepare the boot block */
979 rc = block_bb_read(devmap_handle, BS_BLOCK);
980 if (rc != EOK) {
981 block_fini(devmap_handle);
982 async_answer_0(rid, rc);
983 return;
984 }
985
986 /* get the buffer with the boot sector */
987 bs = block_bb_get(devmap_handle);
988
989 if (BPS(bs) != BS_SIZE) {
990 block_fini(devmap_handle);
991 async_answer_0(rid, ENOTSUP);
992 return;
993 }
994
995 /* Initialize the block cache */
996 rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
997 if (rc != EOK) {
998 block_fini(devmap_handle);
999 async_answer_0(rid, rc);
1000 return;
1001 }
1002
1003 /* Do some simple sanity checks on the file system. */
1004 rc = fat_sanity_check(bs, devmap_handle);
1005 if (rc != EOK) {
1006 (void) block_cache_fini(devmap_handle);
1007 block_fini(devmap_handle);
1008 async_answer_0(rid, rc);
1009 return;
1010 }
1011
1012 rc = fat_idx_init_by_devmap_handle(devmap_handle);
1013 if (rc != EOK) {
1014 (void) block_cache_fini(devmap_handle);
1015 block_fini(devmap_handle);
1016 async_answer_0(rid, rc);
1017 return;
1018 }
1019
1020 /* Initialize the root node. */
1021 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
1022 if (!rfn) {
1023 (void) block_cache_fini(devmap_handle);
1024 block_fini(devmap_handle);
1025 fat_idx_fini_by_devmap_handle(devmap_handle);
1026 async_answer_0(rid, ENOMEM);
1027 return;
1028 }
1029 fs_node_initialize(rfn);
1030 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
1031 if (!rootp) {
1032 free(rfn);
1033 (void) block_cache_fini(devmap_handle);
1034 block_fini(devmap_handle);
1035 fat_idx_fini_by_devmap_handle(devmap_handle);
1036 async_answer_0(rid, ENOMEM);
1037 return;
1038 }
1039 fat_node_initialize(rootp);
1040
1041 fat_idx_t *ridxp = fat_idx_get_by_pos(devmap_handle, FAT_CLST_ROOTPAR, 0);
1042 if (!ridxp) {
1043 free(rfn);
1044 free(rootp);
1045 (void) block_cache_fini(devmap_handle);
1046 block_fini(devmap_handle);
1047 fat_idx_fini_by_devmap_handle(devmap_handle);
1048 async_answer_0(rid, ENOMEM);
1049 return;
1050 }
1051 assert(ridxp->index == 0);
1052 /* ridxp->lock held */
1053
1054 rootp->type = FAT_DIRECTORY;
1055 rootp->firstc = FAT_CLST_ROOT;
1056 rootp->refcnt = 1;
1057 rootp->lnkcnt = 0; /* FS root is not linked */
1058 rootp->size = RDE(bs) * sizeof(fat_dentry_t);
1059 rootp->idx = ridxp;
1060 ridxp->nodep = rootp;
1061 rootp->bp = rfn;
1062 rfn->data = rootp;
1063
1064 fibril_mutex_unlock(&ridxp->lock);
1065
1066 async_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
1067}
1068
1069void fat_mount(ipc_callid_t rid, ipc_call_t *request)
1070{
1071 libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1072}
1073
1074void fat_unmounted(ipc_callid_t rid, ipc_call_t *request)
1075{
1076 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1077 fs_node_t *fn;
1078 fat_node_t *nodep;
1079 int rc;
1080
1081 rc = fat_root_get(&fn, devmap_handle);
1082 if (rc != EOK) {
1083 async_answer_0(rid, rc);
1084 return;
1085 }
1086 nodep = FAT_NODE(fn);
1087
1088 /*
1089 * We expect exactly two references on the root node. One for the
1090 * fat_root_get() above and one created in fat_mounted().
1091 */
1092 if (nodep->refcnt != 2) {
1093 (void) fat_node_put(fn);
1094 async_answer_0(rid, EBUSY);
1095 return;
1096 }
1097
1098 /*
1099 * Put the root node and force it to the FAT free node list.
1100 */
1101 (void) fat_node_put(fn);
1102 (void) fat_node_put(fn);
1103
1104 /*
1105 * Perform cleanup of the node structures, index structures and
1106 * associated data. Write back this file system's dirty blocks and
1107 * stop using libblock for this instance.
1108 */
1109 (void) fat_node_fini_by_devmap_handle(devmap_handle);
1110 fat_idx_fini_by_devmap_handle(devmap_handle);
1111 (void) block_cache_fini(devmap_handle);
1112 block_fini(devmap_handle);
1113
1114 async_answer_0(rid, EOK);
1115}
1116
1117void fat_unmount(ipc_callid_t rid, ipc_call_t *request)
1118{
1119 libfs_unmount(&fat_libfs_ops, rid, request);
1120}
1121
1122void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
1123{
1124 libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1125}
1126
1127void fat_read(ipc_callid_t rid, ipc_call_t *request)
1128{
1129 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1130 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1131 aoff64_t pos =
1132 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
1133 fs_node_t *fn;
1134 fat_node_t *nodep;
1135 fat_bs_t *bs;
1136 size_t bytes;
1137 block_t *b;
1138 int rc;
1139
1140 rc = fat_node_get(&fn, devmap_handle, index);
1141 if (rc != EOK) {
1142 async_answer_0(rid, rc);
1143 return;
1144 }
1145 if (!fn) {
1146 async_answer_0(rid, ENOENT);
1147 return;
1148 }
1149 nodep = FAT_NODE(fn);
1150
1151 ipc_callid_t callid;
1152 size_t len;
1153 if (!async_data_read_receive(&callid, &len)) {
1154 fat_node_put(fn);
1155 async_answer_0(callid, EINVAL);
1156 async_answer_0(rid, EINVAL);
1157 return;
1158 }
1159
1160 bs = block_bb_get(devmap_handle);
1161
1162 if (nodep->type == FAT_FILE) {
1163 /*
1164 * Our strategy for regular file reads is to read one block at
1165 * most and make use of the possibility to return less data than
1166 * requested. This keeps the code very simple.
1167 */
1168 if (pos >= nodep->size) {
1169 /* reading beyond the EOF */
1170 bytes = 0;
1171 (void) async_data_read_finalize(callid, NULL, 0);
1172 } else {
1173 bytes = min(len, BPS(bs) - pos % BPS(bs));
1174 bytes = min(bytes, nodep->size - pos);
1175 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
1176 BLOCK_FLAGS_NONE);
1177 if (rc != EOK) {
1178 fat_node_put(fn);
1179 async_answer_0(callid, rc);
1180 async_answer_0(rid, rc);
1181 return;
1182 }
1183 (void) async_data_read_finalize(callid,
1184 b->data + pos % BPS(bs), bytes);
1185 rc = block_put(b);
1186 if (rc != EOK) {
1187 fat_node_put(fn);
1188 async_answer_0(rid, rc);
1189 return;
1190 }
1191 }
1192 } else {
1193 unsigned bnum;
1194 aoff64_t spos = pos;
1195 char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
1196 fat_dentry_t *d;
1197
1198 assert(nodep->type == FAT_DIRECTORY);
1199 assert(nodep->size % BPS(bs) == 0);
1200 assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
1201
1202 /*
1203 * Our strategy for readdir() is to use the position pointer as
1204 * an index into the array of all dentries. On entry, it points
1205 * to the first unread dentry. If we skip any dentries, we bump
1206 * the position pointer accordingly.
1207 */
1208 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs);
1209 while (bnum < nodep->size / BPS(bs)) {
1210 aoff64_t o;
1211
1212 rc = fat_block_get(&b, bs, nodep, bnum,
1213 BLOCK_FLAGS_NONE);
1214 if (rc != EOK)
1215 goto err;
1216 for (o = pos % (BPS(bs) / sizeof(fat_dentry_t));
1217 o < BPS(bs) / sizeof(fat_dentry_t);
1218 o++, pos++) {
1219 d = ((fat_dentry_t *)b->data) + o;
1220 switch (fat_classify_dentry(d)) {
1221 case FAT_DENTRY_SKIP:
1222 case FAT_DENTRY_FREE:
1223 continue;
1224 case FAT_DENTRY_LAST:
1225 rc = block_put(b);
1226 if (rc != EOK)
1227 goto err;
1228 goto miss;
1229 default:
1230 case FAT_DENTRY_VALID:
1231 fat_dentry_name_get(d, name);
1232 rc = block_put(b);
1233 if (rc != EOK)
1234 goto err;
1235 goto hit;
1236 }
1237 }
1238 rc = block_put(b);
1239 if (rc != EOK)
1240 goto err;
1241 bnum++;
1242 }
1243miss:
1244 rc = fat_node_put(fn);
1245 async_answer_0(callid, rc != EOK ? rc : ENOENT);
1246 async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
1247 return;
1248
1249err:
1250 (void) fat_node_put(fn);
1251 async_answer_0(callid, rc);
1252 async_answer_0(rid, rc);
1253 return;
1254
1255hit:
1256 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
1257 bytes = (pos - spos) + 1;
1258 }
1259
1260 rc = fat_node_put(fn);
1261 async_answer_1(rid, rc, (sysarg_t)bytes);
1262}
1263
1264void fat_write(ipc_callid_t rid, ipc_call_t *request)
1265{
1266 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1267 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1268 aoff64_t pos =
1269 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
1270 fs_node_t *fn;
1271 fat_node_t *nodep;
1272 fat_bs_t *bs;
1273 size_t bytes, size;
1274 block_t *b;
1275 aoff64_t boundary;
1276 int flags = BLOCK_FLAGS_NONE;
1277 int rc;
1278
1279 rc = fat_node_get(&fn, devmap_handle, index);
1280 if (rc != EOK) {
1281 async_answer_0(rid, rc);
1282 return;
1283 }
1284 if (!fn) {
1285 async_answer_0(rid, ENOENT);
1286 return;
1287 }
1288 nodep = FAT_NODE(fn);
1289
1290 ipc_callid_t callid;
1291 size_t len;
1292 if (!async_data_write_receive(&callid, &len)) {
1293 (void) fat_node_put(fn);
1294 async_answer_0(callid, EINVAL);
1295 async_answer_0(rid, EINVAL);
1296 return;
1297 }
1298
1299 bs = block_bb_get(devmap_handle);
1300
1301 /*
1302 * In all scenarios, we will attempt to write out only one block worth
1303 * of data at maximum. There might be some more efficient approaches,
1304 * but this one greatly simplifies fat_write(). Note that we can afford
1305 * to do this because the client must be ready to handle the return
1306 * value signalizing a smaller number of bytes written.
1307 */
1308 bytes = min(len, BPS(bs) - pos % BPS(bs));
1309 if (bytes == BPS(bs))
1310 flags |= BLOCK_FLAGS_NOREAD;
1311
1312 boundary = ROUND_UP(nodep->size, BPC(bs));
1313 if (pos < boundary) {
1314 /*
1315 * This is the easier case - we are either overwriting already
1316 * existing contents or writing behind the EOF, but still within
1317 * the limits of the last cluster. The node size may grow to the
1318 * next block size boundary.
1319 */
1320 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
1321 if (rc != EOK) {
1322 (void) fat_node_put(fn);
1323 async_answer_0(callid, rc);
1324 async_answer_0(rid, rc);
1325 return;
1326 }
1327 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1328 if (rc != EOK) {
1329 (void) fat_node_put(fn);
1330 async_answer_0(callid, rc);
1331 async_answer_0(rid, rc);
1332 return;
1333 }
1334 (void) async_data_write_finalize(callid,
1335 b->data + pos % BPS(bs), bytes);
1336 b->dirty = true; /* need to sync block */
1337 rc = block_put(b);
1338 if (rc != EOK) {
1339 (void) fat_node_put(fn);
1340 async_answer_0(rid, rc);
1341 return;
1342 }
1343 if (pos + bytes > nodep->size) {
1344 nodep->size = pos + bytes;
1345 nodep->dirty = true; /* need to sync node */
1346 }
1347 size = nodep->size;
1348 rc = fat_node_put(fn);
1349 async_answer_2(rid, rc, bytes, nodep->size);
1350 return;
1351 } else {
1352 /*
1353 * This is the more difficult case. We must allocate new
1354 * clusters for the node and zero them out.
1355 */
1356 unsigned nclsts;
1357 fat_cluster_t mcl, lcl;
1358
1359 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1360 /* create an independent chain of nclsts clusters in all FATs */
1361 rc = fat_alloc_clusters(bs, devmap_handle, nclsts, &mcl, &lcl);
1362 if (rc != EOK) {
1363 /* could not allocate a chain of nclsts clusters */
1364 (void) fat_node_put(fn);
1365 async_answer_0(callid, rc);
1366 async_answer_0(rid, rc);
1367 return;
1368 }
1369 /* zero fill any gaps */
1370 rc = fat_fill_gap(bs, nodep, mcl, pos);
1371 if (rc != EOK) {
1372 (void) fat_free_clusters(bs, devmap_handle, mcl);
1373 (void) fat_node_put(fn);
1374 async_answer_0(callid, rc);
1375 async_answer_0(rid, rc);
1376 return;
1377 }
1378 rc = _fat_block_get(&b, bs, devmap_handle, lcl, NULL,
1379 (pos / BPS(bs)) % SPC(bs), flags);
1380 if (rc != EOK) {
1381 (void) fat_free_clusters(bs, devmap_handle, mcl);
1382 (void) fat_node_put(fn);
1383 async_answer_0(callid, rc);
1384 async_answer_0(rid, rc);
1385 return;
1386 }
1387 (void) async_data_write_finalize(callid,
1388 b->data + pos % BPS(bs), bytes);
1389 b->dirty = true; /* need to sync block */
1390 rc = block_put(b);
1391 if (rc != EOK) {
1392 (void) fat_free_clusters(bs, devmap_handle, mcl);
1393 (void) fat_node_put(fn);
1394 async_answer_0(rid, rc);
1395 return;
1396 }
1397 /*
1398 * Append the cluster chain starting in mcl to the end of the
1399 * node's cluster chain.
1400 */
1401 rc = fat_append_clusters(bs, nodep, mcl, lcl);
1402 if (rc != EOK) {
1403 (void) fat_free_clusters(bs, devmap_handle, mcl);
1404 (void) fat_node_put(fn);
1405 async_answer_0(rid, rc);
1406 return;
1407 }
1408 nodep->size = size = pos + bytes;
1409 nodep->dirty = true; /* need to sync node */
1410 rc = fat_node_put(fn);
1411 async_answer_2(rid, rc, bytes, size);
1412 return;
1413 }
1414}
1415
1416void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1417{
1418 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1419 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1420 aoff64_t size =
1421 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
1422 fs_node_t *fn;
1423 fat_node_t *nodep;
1424 fat_bs_t *bs;
1425 int rc;
1426
1427 rc = fat_node_get(&fn, devmap_handle, index);
1428 if (rc != EOK) {
1429 async_answer_0(rid, rc);
1430 return;
1431 }
1432 if (!fn) {
1433 async_answer_0(rid, ENOENT);
1434 return;
1435 }
1436 nodep = FAT_NODE(fn);
1437
1438 bs = block_bb_get(devmap_handle);
1439
1440 if (nodep->size == size) {
1441 rc = EOK;
1442 } else if (nodep->size < size) {
1443 /*
1444 * The standard says we have the freedom to grow the node.
1445 * For now, we simply return an error.
1446 */
1447 rc = EINVAL;
1448 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1449 /*
1450 * The node will be shrunk, but no clusters will be deallocated.
1451 */
1452 nodep->size = size;
1453 nodep->dirty = true; /* need to sync node */
1454 rc = EOK;
1455 } else {
1456 /*
1457 * The node will be shrunk, clusters will be deallocated.
1458 */
1459 if (size == 0) {
1460 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1461 if (rc != EOK)
1462 goto out;
1463 } else {
1464 fat_cluster_t lastc;
1465 rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
1466 &lastc, NULL, (size - 1) / BPC(bs));
1467 if (rc != EOK)
1468 goto out;
1469 rc = fat_chop_clusters(bs, nodep, lastc);
1470 if (rc != EOK)
1471 goto out;
1472 }
1473 nodep->size = size;
1474 nodep->dirty = true; /* need to sync node */
1475 rc = EOK;
1476 }
1477out:
1478 fat_node_put(fn);
1479 async_answer_0(rid, rc);
1480 return;
1481}
1482
1483void fat_close(ipc_callid_t rid, ipc_call_t *request)
1484{
1485 async_answer_0(rid, EOK);
1486}
1487
1488void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1489{
1490 devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
1491 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1492 fs_node_t *fn;
1493 fat_node_t *nodep;
1494 int rc;
1495
1496 rc = fat_node_get(&fn, devmap_handle, index);
1497 if (rc != EOK) {
1498 async_answer_0(rid, rc);
1499 return;
1500 }
1501 if (!fn) {
1502 async_answer_0(rid, ENOENT);
1503 return;
1504 }
1505
1506 nodep = FAT_NODE(fn);
1507 /*
1508 * We should have exactly two references. One for the above
1509 * call to fat_node_get() and one from fat_unlink().
1510 */
1511 assert(nodep->refcnt == 2);
1512
1513 rc = fat_destroy_node(fn);
1514 async_answer_0(rid, rc);
1515}
1516
1517void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
1518{
1519 libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1520}
1521
1522void fat_stat(ipc_callid_t rid, ipc_call_t *request)
1523{
1524 libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1525}
1526
1527void fat_sync(ipc_callid_t rid, ipc_call_t *request)
1528{
1529 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1530 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1531
1532 fs_node_t *fn;
1533 int rc = fat_node_get(&fn, devmap_handle, index);
1534 if (rc != EOK) {
1535 async_answer_0(rid, rc);
1536 return;
1537 }
1538 if (!fn) {
1539 async_answer_0(rid, ENOENT);
1540 return;
1541 }
1542
1543 fat_node_t *nodep = FAT_NODE(fn);
1544
1545 nodep->dirty = true;
1546 rc = fat_node_sync(nodep);
1547
1548 fat_node_put(fn);
1549 async_answer_0(rid, rc);
1550}
1551
1552/**
1553 * @}
1554 */
Note: See TracBrowser for help on using the repository browser.