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

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

do not intermix low-level IPC methods with async framework methods

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