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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 563686b was 563686b, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

Fix for fat_read. Add error checking for fat_directory_open and use
new function: fat_directory_seek instead explicit assigning position.

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