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

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

Using local variables for storing LFN data instead of global fat_directory_t struct

  • Property mode set to 100644
File size: 35.6 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 wchar_t wname[FAT_LFN_NAME_SIZE];
590
591 fibril_mutex_lock(&childp->lock);
592 if (childp->lnkcnt == 1) {
593 /*
594 * On FAT, we don't support multiple hard links.
595 */
596 fibril_mutex_unlock(&childp->lock);
597 return EMLINK;
598 }
599 assert(childp->lnkcnt == 0);
600 fibril_mutex_unlock(&childp->lock);
601
602 rc = str_to_wstr(wname, FAT_LFN_NAME_SIZE, name);
603 if (rc != EOK)
604 return rc;
605
606 if (!fat_lfn_valid(wname))
607 return ENOTSUP;
608
609 fibril_mutex_lock(&parentp->idx->lock);
610 bs = block_bb_get(parentp->idx->devmap_handle);
611 rc = fat_directory_open(parentp, &di);
612 if (rc != EOK)
613 return rc;
614
615 /*
616 * At this point we only establish the link between the parent and the
617 * child. The dentry, except of the name and the extension, will remain
618 * uninitialized until the corresponding node is synced. Thus the valid
619 * dentry data is kept in the child node structure.
620 */
621 memset(&de, 0, sizeof(fat_dentry_t));
622
623 rc = fat_directory_write(&di, name, &de);
624 if (rc!=EOK)
625 return rc;
626 rc = fat_directory_close(&di);
627 if (rc!=EOK)
628 return rc;
629
630 fibril_mutex_unlock(&parentp->idx->lock);
631 if (rc != EOK)
632 return rc;
633
634 fibril_mutex_lock(&childp->idx->lock);
635
636 if (childp->type == FAT_DIRECTORY) {
637 /*
638 * If possible, create the Sub-directory Identifier Entry and
639 * the Sub-directory Parent Pointer Entry (i.e. "." and "..").
640 * These entries are not mandatory according to Standard
641 * ECMA-107 and HelenOS VFS does not use them anyway, so this is
642 * rather a sign of our good will.
643 */
644 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
645 if (rc != EOK) {
646 /*
647 * Rather than returning an error, simply skip the
648 * creation of these two entries.
649 */
650 goto skip_dots;
651 }
652 d = (fat_dentry_t *) b->data;
653 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
654 (bcmp(d->name, FAT_NAME_DOT, FAT_NAME_LEN)) == 0) {
655 memset(d, 0, sizeof(fat_dentry_t));
656 memcpy(d->name, FAT_NAME_DOT, FAT_NAME_LEN);
657 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
658 d->attr = FAT_ATTR_SUBDIR;
659 d->firstc = host2uint16_t_le(childp->firstc);
660 /* TODO: initialize also the date/time members. */
661 }
662 d++;
663 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
664 (bcmp(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN) == 0)) {
665 memset(d, 0, sizeof(fat_dentry_t));
666 memcpy(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN);
667 memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
668 d->attr = FAT_ATTR_SUBDIR;
669 d->firstc = (parentp->firstc == FAT_ROOT_CLST(bs)) ?
670 host2uint16_t_le(FAT_CLST_ROOTPAR) :
671 host2uint16_t_le(parentp->firstc);
672 /* TODO: initialize also the date/time members. */
673 }
674 b->dirty = true; /* need to sync block */
675 /*
676 * Ignore the return value as we would have fallen through on error
677 * anyway.
678 */
679 (void) block_put(b);
680 }
681skip_dots:
682
683 childp->idx->pfc = parentp->firstc;
684 childp->idx->pdi = di.pos; /* di.pos holds absolute position of SFN entry */
685 fibril_mutex_unlock(&childp->idx->lock);
686
687 fibril_mutex_lock(&childp->lock);
688 childp->lnkcnt = 1;
689 childp->dirty = true; /* need to sync node */
690 fibril_mutex_unlock(&childp->lock);
691
692 /*
693 * Hash in the index structure into the position hash.
694 */
695 fat_idx_hashin(childp->idx);
696
697 return EOK;
698}
699
700int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
701{
702 fat_node_t *parentp = FAT_NODE(pfn);
703 fat_node_t *childp = FAT_NODE(cfn);
704 bool has_children;
705 int rc;
706
707 if (!parentp)
708 return EBUSY;
709
710 rc = fat_has_children(&has_children, cfn);
711 if (rc != EOK)
712 return rc;
713 if (has_children)
714 return ENOTEMPTY;
715
716 fibril_mutex_lock(&parentp->lock);
717 fibril_mutex_lock(&childp->lock);
718 assert(childp->lnkcnt == 1);
719 fibril_mutex_lock(&childp->idx->lock);
720
721 fat_directory_t di;
722 rc = fat_directory_open(parentp,&di);
723 if (rc != EOK)
724 goto error;
725 rc = fat_directory_seek(&di, childp->idx->pdi);
726 if (rc != EOK)
727 goto error;
728 rc = fat_directory_erase(&di);
729 if (rc != EOK)
730 goto error;
731 rc = fat_directory_close(&di);
732 if (rc != EOK)
733 goto error;
734
735 /* remove the index structure from the position hash */
736 fat_idx_hashout(childp->idx);
737 /* clear position information */
738 childp->idx->pfc = FAT_CLST_RES0;
739 childp->idx->pdi = 0;
740 fibril_mutex_unlock(&childp->idx->lock);
741 childp->lnkcnt = 0;
742 childp->refcnt++; /* keep the node in memory until destroyed */
743 childp->dirty = true;
744 fibril_mutex_unlock(&childp->lock);
745 fibril_mutex_unlock(&parentp->lock);
746
747 return EOK;
748
749error:
750 (void) fat_directory_close(&di);
751 fibril_mutex_unlock(&childp->idx->lock);
752 fibril_mutex_unlock(&childp->lock);
753 fibril_mutex_unlock(&parentp->lock);
754 return rc;
755}
756
757int fat_has_children(bool *has_children, fs_node_t *fn)
758{
759 fat_bs_t *bs;
760 fat_node_t *nodep = FAT_NODE(fn);
761 unsigned blocks;
762 block_t *b;
763 unsigned i, j;
764 int rc;
765
766 if (nodep->type != FAT_DIRECTORY) {
767 *has_children = false;
768 return EOK;
769 }
770
771 fibril_mutex_lock(&nodep->idx->lock);
772 bs = block_bb_get(nodep->idx->devmap_handle);
773
774 blocks = nodep->size / BPS(bs);
775
776 for (i = 0; i < blocks; i++) {
777 fat_dentry_t *d;
778
779 rc = fat_block_get(&b, bs, nodep, i, BLOCK_FLAGS_NONE);
780 if (rc != EOK) {
781 fibril_mutex_unlock(&nodep->idx->lock);
782 return rc;
783 }
784 for (j = 0; j < DPS(bs); j++) {
785 d = ((fat_dentry_t *)b->data) + j;
786 switch (fat_classify_dentry(d)) {
787 case FAT_DENTRY_SKIP:
788 case FAT_DENTRY_FREE:
789 continue;
790 case FAT_DENTRY_LAST:
791 rc = block_put(b);
792 fibril_mutex_unlock(&nodep->idx->lock);
793 *has_children = false;
794 return rc;
795 default:
796 case FAT_DENTRY_VALID:
797 rc = block_put(b);
798 fibril_mutex_unlock(&nodep->idx->lock);
799 *has_children = true;
800 return rc;
801 }
802 }
803 rc = block_put(b);
804 if (rc != EOK) {
805 fibril_mutex_unlock(&nodep->idx->lock);
806 return rc;
807 }
808 }
809
810 fibril_mutex_unlock(&nodep->idx->lock);
811 *has_children = false;
812 return EOK;
813}
814
815
816fs_index_t fat_index_get(fs_node_t *fn)
817{
818 return FAT_NODE(fn)->idx->index;
819}
820
821aoff64_t fat_size_get(fs_node_t *fn)
822{
823 return FAT_NODE(fn)->size;
824}
825
826unsigned fat_lnkcnt_get(fs_node_t *fn)
827{
828 return FAT_NODE(fn)->lnkcnt;
829}
830
831char fat_plb_get_char(unsigned pos)
832{
833 return fat_reg.plb_ro[pos % PLB_SIZE];
834}
835
836bool fat_is_directory(fs_node_t *fn)
837{
838 return FAT_NODE(fn)->type == FAT_DIRECTORY;
839}
840
841bool fat_is_file(fs_node_t *fn)
842{
843 return FAT_NODE(fn)->type == FAT_FILE;
844}
845
846devmap_handle_t fat_device_get(fs_node_t *node)
847{
848 return 0;
849}
850
851/** libfs operations */
852libfs_ops_t fat_libfs_ops = {
853 .root_get = fat_root_get,
854 .match = fat_match,
855 .node_get = fat_node_get,
856 .node_open = fat_node_open,
857 .node_put = fat_node_put,
858 .create = fat_create_node,
859 .destroy = fat_destroy_node,
860 .link = fat_link,
861 .unlink = fat_unlink,
862 .has_children = fat_has_children,
863 .index_get = fat_index_get,
864 .size_get = fat_size_get,
865 .lnkcnt_get = fat_lnkcnt_get,
866 .plb_get_char = fat_plb_get_char,
867 .is_directory = fat_is_directory,
868 .is_file = fat_is_file,
869 .device_get = fat_device_get
870};
871
872/*
873 * VFS operations.
874 */
875
876void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
877{
878 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
879 enum cache_mode cmode;
880 fat_bs_t *bs;
881
882 /* Accept the mount options */
883 char *opts;
884 int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
885
886 if (rc != EOK) {
887 async_answer_0(rid, rc);
888 return;
889 }
890
891 /* Check for option enabling write through. */
892 if (str_cmp(opts, "wtcache") == 0)
893 cmode = CACHE_MODE_WT;
894 else
895 cmode = CACHE_MODE_WB;
896
897 free(opts);
898
899 /* initialize libblock */
900 rc = block_init(devmap_handle, BS_SIZE);
901 if (rc != EOK) {
902 async_answer_0(rid, rc);
903 return;
904 }
905
906 /* prepare the boot block */
907 rc = block_bb_read(devmap_handle, BS_BLOCK);
908 if (rc != EOK) {
909 block_fini(devmap_handle);
910 async_answer_0(rid, rc);
911 return;
912 }
913
914 /* get the buffer with the boot sector */
915 bs = block_bb_get(devmap_handle);
916
917 if (BPS(bs) != BS_SIZE) {
918 block_fini(devmap_handle);
919 async_answer_0(rid, ENOTSUP);
920 return;
921 }
922
923 /* Initialize the block cache */
924 rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
925 if (rc != EOK) {
926 block_fini(devmap_handle);
927 async_answer_0(rid, rc);
928 return;
929 }
930
931 /* Do some simple sanity checks on the file system. */
932 rc = fat_sanity_check(bs, devmap_handle);
933 if (rc != EOK) {
934 (void) block_cache_fini(devmap_handle);
935 block_fini(devmap_handle);
936 async_answer_0(rid, rc);
937 return;
938 }
939
940 rc = fat_idx_init_by_devmap_handle(devmap_handle);
941 if (rc != EOK) {
942 (void) block_cache_fini(devmap_handle);
943 block_fini(devmap_handle);
944 async_answer_0(rid, rc);
945 return;
946 }
947
948 /* Initialize the root node. */
949 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
950 if (!rfn) {
951 (void) block_cache_fini(devmap_handle);
952 block_fini(devmap_handle);
953 fat_idx_fini_by_devmap_handle(devmap_handle);
954 async_answer_0(rid, ENOMEM);
955 return;
956 }
957
958 fs_node_initialize(rfn);
959 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
960 if (!rootp) {
961 free(rfn);
962 (void) block_cache_fini(devmap_handle);
963 block_fini(devmap_handle);
964 fat_idx_fini_by_devmap_handle(devmap_handle);
965 async_answer_0(rid, ENOMEM);
966 return;
967 }
968 fat_node_initialize(rootp);
969
970 fat_idx_t *ridxp = fat_idx_get_by_pos(devmap_handle, FAT_CLST_ROOTPAR, 0);
971 if (!ridxp) {
972 free(rfn);
973 free(rootp);
974 (void) block_cache_fini(devmap_handle);
975 block_fini(devmap_handle);
976 fat_idx_fini_by_devmap_handle(devmap_handle);
977 async_answer_0(rid, ENOMEM);
978 return;
979 }
980 assert(ridxp->index == 0);
981 /* ridxp->lock held */
982
983 rootp->type = FAT_DIRECTORY;
984 rootp->firstc = FAT_ROOT_CLST(bs);
985 rootp->refcnt = 1;
986 rootp->lnkcnt = 0; /* FS root is not linked */
987
988 if (FAT_IS_FAT32(bs)) {
989 uint16_t clusters;
990 rc = fat_clusters_get(&clusters, bs, devmap_handle, rootp->firstc);
991 if (rc != EOK) {
992 free(rfn);
993 free(rootp);
994 free(ridxp); /* TODO: Is it right way to free ridxp? */
995 (void) block_cache_fini(devmap_handle);
996 block_fini(devmap_handle);
997 fat_idx_fini_by_devmap_handle(devmap_handle);
998 async_answer_0(rid, ENOTSUP);
999 return;
1000 }
1001 rootp->size = BPS(bs) * SPC(bs) * clusters;
1002 } else
1003 rootp->size = RDE(bs) * sizeof(fat_dentry_t);
1004
1005 rootp->idx = ridxp;
1006 ridxp->nodep = rootp;
1007 rootp->bp = rfn;
1008 rfn->data = rootp;
1009
1010 fibril_mutex_unlock(&ridxp->lock);
1011
1012 async_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
1013}
1014
1015void fat_mount(ipc_callid_t rid, ipc_call_t *request)
1016{
1017 libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1018}
1019
1020void fat_unmounted(ipc_callid_t rid, ipc_call_t *request)
1021{
1022 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1023 fs_node_t *fn;
1024 fat_node_t *nodep;
1025 int rc;
1026
1027 rc = fat_root_get(&fn, devmap_handle);
1028 if (rc != EOK) {
1029 async_answer_0(rid, rc);
1030 return;
1031 }
1032 nodep = FAT_NODE(fn);
1033
1034 /*
1035 * We expect exactly two references on the root node. One for the
1036 * fat_root_get() above and one created in fat_mounted().
1037 */
1038 if (nodep->refcnt != 2) {
1039 (void) fat_node_put(fn);
1040 async_answer_0(rid, EBUSY);
1041 return;
1042 }
1043
1044 /*
1045 * Put the root node and force it to the FAT free node list.
1046 */
1047 (void) fat_node_put(fn);
1048 (void) fat_node_put(fn);
1049
1050 /*
1051 * Perform cleanup of the node structures, index structures and
1052 * associated data. Write back this file system's dirty blocks and
1053 * stop using libblock for this instance.
1054 */
1055 (void) fat_node_fini_by_devmap_handle(devmap_handle);
1056 fat_idx_fini_by_devmap_handle(devmap_handle);
1057 (void) block_cache_fini(devmap_handle);
1058 block_fini(devmap_handle);
1059
1060 async_answer_0(rid, EOK);
1061}
1062
1063void fat_unmount(ipc_callid_t rid, ipc_call_t *request)
1064{
1065 libfs_unmount(&fat_libfs_ops, rid, request);
1066}
1067
1068void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
1069{
1070 libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1071}
1072
1073void fat_read(ipc_callid_t rid, ipc_call_t *request)
1074{
1075 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1076 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1077 aoff64_t pos =
1078 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
1079 fs_node_t *fn;
1080 fat_node_t *nodep;
1081 fat_bs_t *bs;
1082 size_t bytes;
1083 block_t *b;
1084 int rc;
1085
1086 rc = fat_node_get(&fn, devmap_handle, index);
1087 if (rc != EOK) {
1088 async_answer_0(rid, rc);
1089 return;
1090 }
1091 if (!fn) {
1092 async_answer_0(rid, ENOENT);
1093 return;
1094 }
1095 nodep = FAT_NODE(fn);
1096
1097 ipc_callid_t callid;
1098 size_t len;
1099 if (!async_data_read_receive(&callid, &len)) {
1100 fat_node_put(fn);
1101 async_answer_0(callid, EINVAL);
1102 async_answer_0(rid, EINVAL);
1103 return;
1104 }
1105
1106 bs = block_bb_get(devmap_handle);
1107
1108 if (nodep->type == FAT_FILE) {
1109 /*
1110 * Our strategy for regular file reads is to read one block at
1111 * most and make use of the possibility to return less data than
1112 * requested. This keeps the code very simple.
1113 */
1114 if (pos >= nodep->size) {
1115 /* reading beyond the EOF */
1116 bytes = 0;
1117 (void) async_data_read_finalize(callid, NULL, 0);
1118 } else {
1119 bytes = min(len, BPS(bs) - pos % BPS(bs));
1120 bytes = min(bytes, nodep->size - pos);
1121 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
1122 BLOCK_FLAGS_NONE);
1123 if (rc != EOK) {
1124 fat_node_put(fn);
1125 async_answer_0(callid, rc);
1126 async_answer_0(rid, rc);
1127 return;
1128 }
1129 (void) async_data_read_finalize(callid,
1130 b->data + pos % BPS(bs), bytes);
1131 rc = block_put(b);
1132 if (rc != EOK) {
1133 fat_node_put(fn);
1134 async_answer_0(rid, rc);
1135 return;
1136 }
1137 }
1138 } else {
1139 aoff64_t spos = pos;
1140 char name[FAT_LFN_NAME_SIZE];
1141 fat_dentry_t *d;
1142
1143 assert(nodep->type == FAT_DIRECTORY);
1144 assert(nodep->size % BPS(bs) == 0);
1145 assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
1146
1147 fat_directory_t di;
1148 rc = fat_directory_open(nodep, &di);
1149 if (rc != EOK) goto err;
1150 rc = fat_directory_seek(&di, pos);
1151 if (rc != EOK) {
1152 (void) fat_directory_close(&di);
1153 goto err;
1154 }
1155
1156 rc = fat_directory_read(&di, name, &d);
1157 if (rc == EOK) goto hit;
1158 if (rc == ENOENT) goto miss;
1159
1160err:
1161 (void) fat_node_put(fn);
1162 async_answer_0(callid, rc);
1163 async_answer_0(rid, rc);
1164 return;
1165
1166miss:
1167 rc = fat_directory_close(&di);
1168 if (rc!=EOK)
1169 goto err;
1170 rc = fat_node_put(fn);
1171 async_answer_0(callid, rc != EOK ? rc : ENOENT);
1172 async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
1173 return;
1174
1175hit:
1176 pos = di.pos;
1177 rc = fat_directory_close(&di);
1178 if (rc!=EOK)
1179 goto err;
1180 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
1181 bytes = (pos - spos)+1;
1182 }
1183
1184 rc = fat_node_put(fn);
1185 async_answer_1(rid, rc, (sysarg_t)bytes);
1186}
1187
1188void fat_write(ipc_callid_t rid, ipc_call_t *request)
1189{
1190 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1191 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1192 aoff64_t pos =
1193 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
1194 fs_node_t *fn;
1195 fat_node_t *nodep;
1196 fat_bs_t *bs;
1197 size_t bytes, size;
1198 block_t *b;
1199 aoff64_t boundary;
1200 int flags = BLOCK_FLAGS_NONE;
1201 int rc;
1202
1203 rc = fat_node_get(&fn, devmap_handle, index);
1204 if (rc != EOK) {
1205 async_answer_0(rid, rc);
1206 return;
1207 }
1208 if (!fn) {
1209 async_answer_0(rid, ENOENT);
1210 return;
1211 }
1212 nodep = FAT_NODE(fn);
1213
1214 ipc_callid_t callid;
1215 size_t len;
1216 if (!async_data_write_receive(&callid, &len)) {
1217 (void) fat_node_put(fn);
1218 async_answer_0(callid, EINVAL);
1219 async_answer_0(rid, EINVAL);
1220 return;
1221 }
1222
1223 bs = block_bb_get(devmap_handle);
1224
1225 /*
1226 * In all scenarios, we will attempt to write out only one block worth
1227 * of data at maximum. There might be some more efficient approaches,
1228 * but this one greatly simplifies fat_write(). Note that we can afford
1229 * to do this because the client must be ready to handle the return
1230 * value signalizing a smaller number of bytes written.
1231 */
1232 bytes = min(len, BPS(bs) - pos % BPS(bs));
1233 if (bytes == BPS(bs))
1234 flags |= BLOCK_FLAGS_NOREAD;
1235
1236 boundary = ROUND_UP(nodep->size, BPC(bs));
1237 if (pos < boundary) {
1238 /*
1239 * This is the easier case - we are either overwriting already
1240 * existing contents or writing behind the EOF, but still within
1241 * the limits of the last cluster. The node size may grow to the
1242 * next block size boundary.
1243 */
1244 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
1245 if (rc != EOK) {
1246 (void) fat_node_put(fn);
1247 async_answer_0(callid, rc);
1248 async_answer_0(rid, rc);
1249 return;
1250 }
1251 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1252 if (rc != EOK) {
1253 (void) fat_node_put(fn);
1254 async_answer_0(callid, rc);
1255 async_answer_0(rid, rc);
1256 return;
1257 }
1258 (void) async_data_write_finalize(callid,
1259 b->data + pos % BPS(bs), bytes);
1260 b->dirty = true; /* need to sync block */
1261 rc = block_put(b);
1262 if (rc != EOK) {
1263 (void) fat_node_put(fn);
1264 async_answer_0(rid, rc);
1265 return;
1266 }
1267 if (pos + bytes > nodep->size) {
1268 nodep->size = pos + bytes;
1269 nodep->dirty = true; /* need to sync node */
1270 }
1271 size = nodep->size;
1272 rc = fat_node_put(fn);
1273 async_answer_2(rid, rc, bytes, nodep->size);
1274 return;
1275 } else {
1276 /*
1277 * This is the more difficult case. We must allocate new
1278 * clusters for the node and zero them out.
1279 */
1280 unsigned nclsts;
1281 fat_cluster_t mcl, lcl;
1282
1283 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1284 /* create an independent chain of nclsts clusters in all FATs */
1285 rc = fat_alloc_clusters(bs, devmap_handle, nclsts, &mcl, &lcl);
1286 if (rc != EOK) {
1287 /* could not allocate a chain of nclsts clusters */
1288 (void) fat_node_put(fn);
1289 async_answer_0(callid, rc);
1290 async_answer_0(rid, rc);
1291 return;
1292 }
1293 /* zero fill any gaps */
1294 rc = fat_fill_gap(bs, nodep, mcl, pos);
1295 if (rc != EOK) {
1296 (void) fat_free_clusters(bs, devmap_handle, mcl);
1297 (void) fat_node_put(fn);
1298 async_answer_0(callid, rc);
1299 async_answer_0(rid, rc);
1300 return;
1301 }
1302 rc = _fat_block_get(&b, bs, devmap_handle, lcl, NULL,
1303 (pos / BPS(bs)) % SPC(bs), flags);
1304 if (rc != EOK) {
1305 (void) fat_free_clusters(bs, devmap_handle, mcl);
1306 (void) fat_node_put(fn);
1307 async_answer_0(callid, rc);
1308 async_answer_0(rid, rc);
1309 return;
1310 }
1311 (void) async_data_write_finalize(callid,
1312 b->data + pos % BPS(bs), bytes);
1313 b->dirty = true; /* need to sync block */
1314 rc = block_put(b);
1315 if (rc != EOK) {
1316 (void) fat_free_clusters(bs, devmap_handle, mcl);
1317 (void) fat_node_put(fn);
1318 async_answer_0(rid, rc);
1319 return;
1320 }
1321 /*
1322 * Append the cluster chain starting in mcl to the end of the
1323 * node's cluster chain.
1324 */
1325 rc = fat_append_clusters(bs, nodep, mcl, lcl);
1326 if (rc != EOK) {
1327 (void) fat_free_clusters(bs, devmap_handle, mcl);
1328 (void) fat_node_put(fn);
1329 async_answer_0(rid, rc);
1330 return;
1331 }
1332 nodep->size = size = pos + bytes;
1333 nodep->dirty = true; /* need to sync node */
1334 rc = fat_node_put(fn);
1335 async_answer_2(rid, rc, bytes, size);
1336 return;
1337 }
1338}
1339
1340void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1341{
1342 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1343 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1344 aoff64_t size =
1345 (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
1346 fs_node_t *fn;
1347 fat_node_t *nodep;
1348 fat_bs_t *bs;
1349 int rc;
1350
1351 rc = fat_node_get(&fn, devmap_handle, index);
1352 if (rc != EOK) {
1353 async_answer_0(rid, rc);
1354 return;
1355 }
1356 if (!fn) {
1357 async_answer_0(rid, ENOENT);
1358 return;
1359 }
1360 nodep = FAT_NODE(fn);
1361
1362 bs = block_bb_get(devmap_handle);
1363
1364 if (nodep->size == size) {
1365 rc = EOK;
1366 } else if (nodep->size < size) {
1367 /*
1368 * The standard says we have the freedom to grow the node.
1369 * For now, we simply return an error.
1370 */
1371 rc = EINVAL;
1372 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1373 /*
1374 * The node will be shrunk, but no clusters will be deallocated.
1375 */
1376 nodep->size = size;
1377 nodep->dirty = true; /* need to sync node */
1378 rc = EOK;
1379 } else {
1380 /*
1381 * The node will be shrunk, clusters will be deallocated.
1382 */
1383 if (size == 0) {
1384 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1385 if (rc != EOK)
1386 goto out;
1387 } else {
1388 fat_cluster_t lastc;
1389 rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
1390 &lastc, NULL, (size - 1) / BPC(bs));
1391 if (rc != EOK)
1392 goto out;
1393 rc = fat_chop_clusters(bs, nodep, lastc);
1394 if (rc != EOK)
1395 goto out;
1396 }
1397 nodep->size = size;
1398 nodep->dirty = true; /* need to sync node */
1399 rc = EOK;
1400 }
1401out:
1402 fat_node_put(fn);
1403 async_answer_0(rid, rc);
1404 return;
1405}
1406
1407void fat_close(ipc_callid_t rid, ipc_call_t *request)
1408{
1409 async_answer_0(rid, EOK);
1410}
1411
1412void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1413{
1414 devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
1415 fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1416 fs_node_t *fn;
1417 fat_node_t *nodep;
1418 int rc;
1419
1420 rc = fat_node_get(&fn, devmap_handle, index);
1421 if (rc != EOK) {
1422 async_answer_0(rid, rc);
1423 return;
1424 }
1425 if (!fn) {
1426 async_answer_0(rid, ENOENT);
1427 return;
1428 }
1429
1430 nodep = FAT_NODE(fn);
1431 /*
1432 * We should have exactly two references. One for the above
1433 * call to fat_node_get() and one from fat_unlink().
1434 */
1435 assert(nodep->refcnt == 2);
1436
1437 rc = fat_destroy_node(fn);
1438 async_answer_0(rid, rc);
1439}
1440
1441void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
1442{
1443 libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1444}
1445
1446void fat_stat(ipc_callid_t rid, ipc_call_t *request)
1447{
1448 libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
1449}
1450
1451void fat_sync(ipc_callid_t rid, ipc_call_t *request)
1452{
1453 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
1454 fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
1455
1456 fs_node_t *fn;
1457 int rc = fat_node_get(&fn, devmap_handle, index);
1458 if (rc != EOK) {
1459 async_answer_0(rid, rc);
1460 return;
1461 }
1462 if (!fn) {
1463 async_answer_0(rid, ENOENT);
1464 return;
1465 }
1466
1467 fat_node_t *nodep = FAT_NODE(fn);
1468
1469 nodep->dirty = true;
1470 rc = fat_node_sync(nodep);
1471
1472 fat_node_put(fn);
1473 async_answer_0(rid, rc);
1474}
1475
1476/**
1477 * @}
1478 */
Note: See TracBrowser for help on using the repository browser.