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

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

Support for using new function fat_valid_name in fat_match

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