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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d0a1e9b6 was d0a1e9b6, checked in by Manuele Conti <conti.ma@…>, 12 years ago

Update implementation size, total, free block operations like new stucture statfs.

  • Property mode set to 100644
File size: 34.7 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 <block.h>
46#include <ipc/services.h>
47#include <ipc/loc.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_list);
73
74/*
75 * Forward declarations of FAT libfs operations.
76 */
77static int fat_root_get(fs_node_t **, service_id_t);
78static int fat_match(fs_node_t **, fs_node_t *, const char *);
79static int fat_node_get(fs_node_t **, service_id_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 **, service_id_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 bool fat_is_directory(fs_node_t *);
91static bool fat_is_file(fs_node_t *node);
92static service_id_t fat_service_get(fs_node_t *node);
93static uint32_t fat_size_block(service_id_t);
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->service_id);
126
127 /* Read the block that contains the dentry of interest. */
128 rc = _fat_block_get(&b, bs, node->idx->service_id, 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_service_id(service_id_t service_id)
151{
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 list_foreach(ffn_list, lnk) {
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->service_id != service_id) {
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 ffn_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_list)) {
216 /* Try to use a cached free node structure. */
217 fat_idx_t *idxp_tmp;
218 nodep = list_get_instance(list_first(&ffn_list), fat_node_t,
219 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->service_id);
305
306 /* Read the block that contains the dentry of interest. */
307 rc = _fat_block_get(&b, bs, idxp->service_id, 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 } 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 uint32_t clusters;
335 rc = fat_clusters_get(&clusters, bs, idxp->service_id,
336 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, service_id_t service_id)
370{
371 return fat_node_get(rfn, service_id, 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 service_id_t service_id;
380 int rc;
381
382 fibril_mutex_lock(&parentp->idx->lock);
383 service_id = parentp->idx->service_id;
384 fibril_mutex_unlock(&parentp->idx->lock);
385
386 fat_directory_t di;
387 rc = fat_directory_open(parentp, &di);
388 if (rc != EOK)
389 return rc;
390
391 while (fat_directory_read(&di, name, &d) == EOK) {
392 if (fat_dentry_namecmp(name, component) == 0) {
393 /* hit */
394 fat_node_t *nodep;
395 aoff64_t o = di.pos %
396 (BPS(di.bs) / sizeof(fat_dentry_t));
397 fat_idx_t *idx = fat_idx_get_by_pos(service_id,
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, service_id_t service_id, 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(service_id, 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_list);
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, service_id_t service_id, 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(service_id);
496 if (flags & L_DIRECTORY) {
497 /* allocate a cluster */
498 rc = fat_alloc_clusters(bs, service_id, 1, &mcl, &lcl);
499 if (rc != EOK)
500 return rc;
501 /* populate the new cluster with unused dentries */
502 rc = fat_zero_cluster(bs, service_id, mcl);
503 if (rc != EOK) {
504 (void) fat_free_clusters(bs, service_id, mcl);
505 return rc;
506 }
507 }
508
509 rc = fat_node_get_new(&nodep);
510 if (rc != EOK) {
511 (void) fat_free_clusters(bs, service_id, mcl);
512 return rc;
513 }
514 rc = fat_idx_get_new(&idxp, service_id);
515 if (rc != EOK) {
516 (void) fat_free_clusters(bs, service_id, 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->service_id);
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->service_id,
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->service_id);
606 rc = fat_directory_open(parentp, &di);
607 if (rc != EOK) {
608 fibril_mutex_unlock(&parentp->idx->lock);
609 return rc;
610 }
611
612 /*
613 * At this point we only establish the link between the parent and the
614 * child. The dentry, except of the name and the extension, will remain
615 * uninitialized until the corresponding node is synced. Thus the valid
616 * dentry data is kept in the child node structure.
617 */
618 memset(&de, 0, sizeof(fat_dentry_t));
619
620 rc = fat_directory_write(&di, name, &de);
621 if (rc != EOK) {
622 (void) fat_directory_close(&di);
623 fibril_mutex_unlock(&parentp->idx->lock);
624 return rc;
625 }
626 rc = fat_directory_close(&di);
627 if (rc != EOK) {
628 fibril_mutex_unlock(&parentp->idx->lock);
629 return rc;
630 }
631
632 fibril_mutex_unlock(&parentp->idx->lock);
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 (memcmp(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 (memcmp(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->service_id);
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
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
841service_id_t fat_service_get(fs_node_t *node)
842{
843 return 0;
844}
845
846uint32_t fat_size_block(service_id_t service_id)
847{
848 fat_bs_t *bs;
849 bs = block_bb_get(service_id);
850
851 return BPC(bs);
852}
853
854/** libfs operations */
855libfs_ops_t fat_libfs_ops = {
856 .root_get = fat_root_get,
857 .match = fat_match,
858 .node_get = fat_node_get,
859 .node_open = fat_node_open,
860 .node_put = fat_node_put,
861 .create = fat_create_node,
862 .destroy = fat_destroy_node,
863 .link = fat_link,
864 .unlink = fat_unlink,
865 .has_children = fat_has_children,
866 .index_get = fat_index_get,
867 .size_get = fat_size_get,
868 .lnkcnt_get = fat_lnkcnt_get,
869 .is_directory = fat_is_directory,
870 .is_file = fat_is_file,
871 .service_get = fat_service_get,
872 .size_block = fat_size_block
873};
874
875/*
876 * FAT VFS_OUT operations.
877 */
878
879static int
880fat_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
881 aoff64_t *size, unsigned *linkcnt)
882{
883 enum cache_mode cmode = CACHE_MODE_WB;
884 fat_bs_t *bs;
885 fat_instance_t *instance;
886 int rc;
887
888 instance = malloc(sizeof(fat_instance_t));
889 if (!instance)
890 return ENOMEM;
891 instance->lfn_enabled = true;
892
893 /* Parse mount options. */
894 char *mntopts = (char *) opts;
895 char *saveptr;
896 char *opt;
897 while ((opt = strtok_r(mntopts, " ,", &saveptr)) != NULL) {
898 if (str_cmp(opt, "wtcache") == 0)
899 cmode = CACHE_MODE_WT;
900 else if (str_cmp(opt, "nolfn") == 0)
901 instance->lfn_enabled = false;
902 mntopts = NULL;
903 }
904
905 /* initialize libblock */
906 rc = block_init(EXCHANGE_SERIALIZE, service_id, BS_SIZE);
907 if (rc != EOK) {
908 free(instance);
909 return rc;
910 }
911
912 /* prepare the boot block */
913 rc = block_bb_read(service_id, BS_BLOCK);
914 if (rc != EOK) {
915 free(instance);
916 block_fini(service_id);
917 return rc;
918 }
919
920 /* get the buffer with the boot sector */
921 bs = block_bb_get(service_id);
922
923 if (BPS(bs) != BS_SIZE) {
924 free(instance);
925 block_fini(service_id);
926 return ENOTSUP;
927 }
928
929 /* Initialize the block cache */
930 rc = block_cache_init(service_id, BPS(bs), 0 /* XXX */, cmode);
931 if (rc != EOK) {
932 free(instance);
933 block_fini(service_id);
934 return rc;
935 }
936
937 /* Do some simple sanity checks on the file system. */
938 rc = fat_sanity_check(bs, service_id);
939 if (rc != EOK) {
940 free(instance);
941 (void) block_cache_fini(service_id);
942 block_fini(service_id);
943 return rc;
944 }
945
946 rc = fat_idx_init_by_service_id(service_id);
947 if (rc != EOK) {
948 free(instance);
949 (void) block_cache_fini(service_id);
950 block_fini(service_id);
951 return rc;
952 }
953
954 /* Initialize the root node. */
955 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
956 if (!rfn) {
957 free(instance);
958 (void) block_cache_fini(service_id);
959 block_fini(service_id);
960 fat_idx_fini_by_service_id(service_id);
961 return ENOMEM;
962 }
963
964 fs_node_initialize(rfn);
965 fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
966 if (!rootp) {
967 free(instance);
968 free(rfn);
969 (void) block_cache_fini(service_id);
970 block_fini(service_id);
971 fat_idx_fini_by_service_id(service_id);
972 return ENOMEM;
973 }
974 fat_node_initialize(rootp);
975
976 fat_idx_t *ridxp = fat_idx_get_by_pos(service_id, FAT_CLST_ROOTPAR, 0);
977 if (!ridxp) {
978 free(instance);
979 free(rfn);
980 free(rootp);
981 (void) block_cache_fini(service_id);
982 block_fini(service_id);
983 fat_idx_fini_by_service_id(service_id);
984 return ENOMEM;
985 }
986 assert(ridxp->index == 0);
987 /* ridxp->lock held */
988
989 rootp->type = FAT_DIRECTORY;
990 rootp->firstc = FAT_ROOT_CLST(bs);
991 rootp->refcnt = 1;
992 rootp->lnkcnt = 0; /* FS root is not linked */
993
994 if (FAT_IS_FAT32(bs)) {
995 uint32_t clusters;
996 rc = fat_clusters_get(&clusters, bs, service_id, rootp->firstc);
997 if (rc != EOK) {
998 fibril_mutex_unlock(&ridxp->lock);
999 free(instance);
1000 free(rfn);
1001 free(rootp);
1002 (void) block_cache_fini(service_id);
1003 block_fini(service_id);
1004 fat_idx_fini_by_service_id(service_id);
1005 return ENOTSUP;
1006 }
1007 rootp->size = BPS(bs) * SPC(bs) * clusters;
1008 } else
1009 rootp->size = RDE(bs) * sizeof(fat_dentry_t);
1010
1011 rc = fs_instance_create(service_id, instance);
1012 if (rc != EOK) {
1013 fibril_mutex_unlock(&ridxp->lock);
1014 free(instance);
1015 free(rfn);
1016 free(rootp);
1017 (void) block_cache_fini(service_id);
1018 block_fini(service_id);
1019 fat_idx_fini_by_service_id(service_id);
1020 return rc;
1021 }
1022
1023 rootp->idx = ridxp;
1024 ridxp->nodep = rootp;
1025 rootp->bp = rfn;
1026 rfn->data = rootp;
1027
1028 fibril_mutex_unlock(&ridxp->lock);
1029
1030 *index = ridxp->index;
1031 *size = rootp->size;
1032 *linkcnt = rootp->lnkcnt;
1033
1034 return EOK;
1035}
1036
1037static int fat_update_fat32_fsinfo(service_id_t service_id)
1038{
1039 fat_bs_t *bs;
1040 fat32_fsinfo_t *info;
1041 block_t *b;
1042 int rc;
1043
1044 bs = block_bb_get(service_id);
1045 assert(FAT_IS_FAT32(bs));
1046
1047 rc = block_get(&b, service_id, uint16_t_le2host(bs->fat32.fsinfo_sec),
1048 BLOCK_FLAGS_NONE);
1049 if (rc != EOK)
1050 return rc;
1051
1052 info = (fat32_fsinfo_t *) b->data;
1053
1054 if (memcmp(info->sig1, FAT32_FSINFO_SIG1, sizeof(info->sig1)) != 0 ||
1055 memcmp(info->sig2, FAT32_FSINFO_SIG2, sizeof(info->sig2)) != 0 ||
1056 memcmp(info->sig3, FAT32_FSINFO_SIG3, sizeof(info->sig3)) != 0) {
1057 (void) block_put(b);
1058 return EINVAL;
1059 }
1060
1061 /* For now, invalidate the counter. */
1062 info->free_clusters = host2uint16_t_le(-1);
1063
1064 b->dirty = true;
1065 return block_put(b);
1066}
1067
1068static int fat_unmounted(service_id_t service_id)
1069{
1070 fs_node_t *fn;
1071 fat_node_t *nodep;
1072 fat_bs_t *bs;
1073 int rc;
1074
1075 bs = block_bb_get(service_id);
1076
1077 rc = fat_root_get(&fn, service_id);
1078 if (rc != EOK)
1079 return rc;
1080 nodep = FAT_NODE(fn);
1081
1082 /*
1083 * We expect exactly two references on the root node. One for the
1084 * fat_root_get() above and one created in fat_mounted().
1085 */
1086 if (nodep->refcnt != 2) {
1087 (void) fat_node_put(fn);
1088 return EBUSY;
1089 }
1090
1091 if (FAT_IS_FAT32(bs)) {
1092 /*
1093 * Attempt to update the FAT32 FS info.
1094 */
1095 (void) fat_update_fat32_fsinfo(service_id);
1096 }
1097
1098 /*
1099 * Put the root node and force it to the FAT free node list.
1100 */
1101 (void) fat_node_put(fn);
1102 (void) fat_node_put(fn);
1103
1104 /*
1105 * Perform cleanup of the node structures, index structures and
1106 * associated data. Write back this file system's dirty blocks and
1107 * stop using libblock for this instance.
1108 */
1109 (void) fat_node_fini_by_service_id(service_id);
1110 fat_idx_fini_by_service_id(service_id);
1111 (void) block_cache_fini(service_id);
1112 block_fini(service_id);
1113
1114 void *data;
1115 if (fs_instance_get(service_id, &data) == EOK) {
1116 fs_instance_destroy(service_id);
1117 free(data);
1118 }
1119
1120 return EOK;
1121}
1122
1123static int
1124fat_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
1125 size_t *rbytes)
1126{
1127 fs_node_t *fn;
1128 fat_node_t *nodep;
1129 fat_bs_t *bs;
1130 size_t bytes;
1131 block_t *b;
1132 int rc;
1133
1134 rc = fat_node_get(&fn, service_id, index);
1135 if (rc != EOK)
1136 return rc;
1137 if (!fn)
1138 return ENOENT;
1139 nodep = FAT_NODE(fn);
1140
1141 ipc_callid_t callid;
1142 size_t len;
1143 if (!async_data_read_receive(&callid, &len)) {
1144 fat_node_put(fn);
1145 async_answer_0(callid, EINVAL);
1146 return EINVAL;
1147 }
1148
1149 bs = block_bb_get(service_id);
1150
1151 if (nodep->type == FAT_FILE) {
1152 /*
1153 * Our strategy for regular file reads is to read one block at
1154 * most and make use of the possibility to return less data than
1155 * requested. This keeps the code very simple.
1156 */
1157 if (pos >= nodep->size) {
1158 /* reading beyond the EOF */
1159 bytes = 0;
1160 (void) async_data_read_finalize(callid, NULL, 0);
1161 } else {
1162 bytes = min(len, BPS(bs) - pos % BPS(bs));
1163 bytes = min(bytes, nodep->size - pos);
1164 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
1165 BLOCK_FLAGS_NONE);
1166 if (rc != EOK) {
1167 fat_node_put(fn);
1168 async_answer_0(callid, rc);
1169 return rc;
1170 }
1171 (void) async_data_read_finalize(callid,
1172 b->data + pos % BPS(bs), bytes);
1173 rc = block_put(b);
1174 if (rc != EOK) {
1175 fat_node_put(fn);
1176 return rc;
1177 }
1178 }
1179 } else {
1180 aoff64_t spos = pos;
1181 char name[FAT_LFN_NAME_SIZE];
1182 fat_dentry_t *d;
1183
1184 assert(nodep->type == FAT_DIRECTORY);
1185 assert(nodep->size % BPS(bs) == 0);
1186 assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
1187
1188 fat_directory_t di;
1189 rc = fat_directory_open(nodep, &di);
1190 if (rc != EOK)
1191 goto err;
1192 rc = fat_directory_seek(&di, pos);
1193 if (rc != EOK) {
1194 (void) fat_directory_close(&di);
1195 goto err;
1196 }
1197
1198 rc = fat_directory_read(&di, name, &d);
1199 if (rc == EOK)
1200 goto hit;
1201 if (rc == ENOENT)
1202 goto miss;
1203
1204err:
1205 (void) fat_node_put(fn);
1206 async_answer_0(callid, rc);
1207 return rc;
1208
1209miss:
1210 rc = fat_directory_close(&di);
1211 if (rc != EOK)
1212 goto err;
1213 rc = fat_node_put(fn);
1214 async_answer_0(callid, rc != EOK ? rc : ENOENT);
1215 *rbytes = 0;
1216 return rc != EOK ? rc : ENOENT;
1217
1218hit:
1219 pos = di.pos;
1220 rc = fat_directory_close(&di);
1221 if (rc != EOK)
1222 goto err;
1223 (void) async_data_read_finalize(callid, name,
1224 str_size(name) + 1);
1225 bytes = (pos - spos) + 1;
1226 }
1227
1228 rc = fat_node_put(fn);
1229 *rbytes = bytes;
1230 return rc;
1231}
1232
1233static int
1234fat_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
1235 size_t *wbytes, aoff64_t *nsize)
1236{
1237 fs_node_t *fn;
1238 fat_node_t *nodep;
1239 fat_bs_t *bs;
1240 size_t bytes;
1241 block_t *b;
1242 aoff64_t boundary;
1243 int flags = BLOCK_FLAGS_NONE;
1244 int rc;
1245
1246 rc = fat_node_get(&fn, service_id, index);
1247 if (rc != EOK)
1248 return rc;
1249 if (!fn)
1250 return ENOENT;
1251 nodep = FAT_NODE(fn);
1252
1253 ipc_callid_t callid;
1254 size_t len;
1255 if (!async_data_write_receive(&callid, &len)) {
1256 (void) fat_node_put(fn);
1257 async_answer_0(callid, EINVAL);
1258 return EINVAL;
1259 }
1260
1261 bs = block_bb_get(service_id);
1262
1263 /*
1264 * In all scenarios, we will attempt to write out only one block worth
1265 * of data at maximum. There might be some more efficient approaches,
1266 * but this one greatly simplifies fat_write(). Note that we can afford
1267 * to do this because the client must be ready to handle the return
1268 * value signalizing a smaller number of bytes written.
1269 */
1270 bytes = min(len, BPS(bs) - pos % BPS(bs));
1271 if (bytes == BPS(bs))
1272 flags |= BLOCK_FLAGS_NOREAD;
1273
1274 boundary = ROUND_UP(nodep->size, BPC(bs));
1275 if (pos < boundary) {
1276 /*
1277 * This is the easier case - we are either overwriting already
1278 * existing contents or writing behind the EOF, but still within
1279 * the limits of the last cluster. The node size may grow to the
1280 * next block size boundary.
1281 */
1282 rc = fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
1283 if (rc != EOK) {
1284 (void) fat_node_put(fn);
1285 async_answer_0(callid, rc);
1286 return rc;
1287 }
1288 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1289 if (rc != EOK) {
1290 (void) fat_node_put(fn);
1291 async_answer_0(callid, rc);
1292 return rc;
1293 }
1294 (void) async_data_write_finalize(callid,
1295 b->data + pos % BPS(bs), bytes);
1296 b->dirty = true; /* need to sync block */
1297 rc = block_put(b);
1298 if (rc != EOK) {
1299 (void) fat_node_put(fn);
1300 return rc;
1301 }
1302 if (pos + bytes > nodep->size) {
1303 nodep->size = pos + bytes;
1304 nodep->dirty = true; /* need to sync node */
1305 }
1306 *wbytes = bytes;
1307 *nsize = nodep->size;
1308 rc = fat_node_put(fn);
1309 return rc;
1310 } else {
1311 /*
1312 * This is the more difficult case. We must allocate new
1313 * clusters for the node and zero them out.
1314 */
1315 unsigned nclsts;
1316 fat_cluster_t mcl, lcl;
1317
1318 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1319 /* create an independent chain of nclsts clusters in all FATs */
1320 rc = fat_alloc_clusters(bs, service_id, nclsts, &mcl, &lcl);
1321 if (rc != EOK) {
1322 /* could not allocate a chain of nclsts clusters */
1323 (void) fat_node_put(fn);
1324 async_answer_0(callid, rc);
1325 return rc;
1326 }
1327 /* zero fill any gaps */
1328 rc = fat_fill_gap(bs, nodep, mcl, pos);
1329 if (rc != EOK) {
1330 (void) fat_free_clusters(bs, service_id, mcl);
1331 (void) fat_node_put(fn);
1332 async_answer_0(callid, rc);
1333 return rc;
1334 }
1335 rc = _fat_block_get(&b, bs, service_id, lcl, NULL,
1336 (pos / BPS(bs)) % SPC(bs), flags);
1337 if (rc != EOK) {
1338 (void) fat_free_clusters(bs, service_id, mcl);
1339 (void) fat_node_put(fn);
1340 async_answer_0(callid, rc);
1341 return rc;
1342 }
1343 (void) async_data_write_finalize(callid,
1344 b->data + pos % BPS(bs), bytes);
1345 b->dirty = true; /* need to sync block */
1346 rc = block_put(b);
1347 if (rc != EOK) {
1348 (void) fat_free_clusters(bs, service_id, mcl);
1349 (void) fat_node_put(fn);
1350 return rc;
1351 }
1352 /*
1353 * Append the cluster chain starting in mcl to the end of the
1354 * node's cluster chain.
1355 */
1356 rc = fat_append_clusters(bs, nodep, mcl, lcl);
1357 if (rc != EOK) {
1358 (void) fat_free_clusters(bs, service_id, mcl);
1359 (void) fat_node_put(fn);
1360 return rc;
1361 }
1362 *nsize = nodep->size = pos + bytes;
1363 rc = fat_node_put(fn);
1364 nodep->dirty = true; /* need to sync node */
1365 *wbytes = bytes;
1366 return rc;
1367 }
1368}
1369
1370static int
1371fat_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
1372{
1373 fs_node_t *fn;
1374 fat_node_t *nodep;
1375 fat_bs_t *bs;
1376 int rc;
1377
1378 rc = fat_node_get(&fn, service_id, index);
1379 if (rc != EOK)
1380 return rc;
1381 if (!fn)
1382 return ENOENT;
1383 nodep = FAT_NODE(fn);
1384
1385 bs = block_bb_get(service_id);
1386
1387 if (nodep->size == size) {
1388 rc = EOK;
1389 } else if (nodep->size < size) {
1390 /*
1391 * The standard says we have the freedom to grow the node.
1392 * For now, we simply return an error.
1393 */
1394 rc = EINVAL;
1395 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1396 /*
1397 * The node will be shrunk, but no clusters will be deallocated.
1398 */
1399 nodep->size = size;
1400 nodep->dirty = true; /* need to sync node */
1401 rc = EOK;
1402 } else {
1403 /*
1404 * The node will be shrunk, clusters will be deallocated.
1405 */
1406 if (size == 0) {
1407 rc = fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1408 if (rc != EOK)
1409 goto out;
1410 } else {
1411 fat_cluster_t lastc;
1412 rc = fat_cluster_walk(bs, service_id, nodep->firstc,
1413 &lastc, NULL, (size - 1) / BPC(bs));
1414 if (rc != EOK)
1415 goto out;
1416 rc = fat_chop_clusters(bs, nodep, lastc);
1417 if (rc != EOK)
1418 goto out;
1419 }
1420 nodep->size = size;
1421 nodep->dirty = true; /* need to sync node */
1422 rc = EOK;
1423 }
1424out:
1425 fat_node_put(fn);
1426 return rc;
1427}
1428
1429static int fat_close(service_id_t service_id, fs_index_t index)
1430{
1431 return EOK;
1432}
1433
1434static int fat_destroy(service_id_t service_id, fs_index_t index)
1435{
1436 fs_node_t *fn;
1437 fat_node_t *nodep;
1438 int rc;
1439
1440 rc = fat_node_get(&fn, service_id, index);
1441 if (rc != EOK)
1442 return rc;
1443 if (!fn)
1444 return ENOENT;
1445
1446 nodep = FAT_NODE(fn);
1447 /*
1448 * We should have exactly two references. One for the above
1449 * call to fat_node_get() and one from fat_unlink().
1450 */
1451 assert(nodep->refcnt == 2);
1452
1453 rc = fat_destroy_node(fn);
1454 return rc;
1455}
1456
1457static int fat_sync(service_id_t service_id, fs_index_t index)
1458{
1459 fs_node_t *fn;
1460 int rc = fat_node_get(&fn, service_id, index);
1461 if (rc != EOK)
1462 return rc;
1463 if (!fn)
1464 return ENOENT;
1465
1466 fat_node_t *nodep = FAT_NODE(fn);
1467
1468 nodep->dirty = true;
1469 rc = fat_node_sync(nodep);
1470
1471 fat_node_put(fn);
1472 return rc;
1473}
1474
1475vfs_out_ops_t fat_ops = {
1476 .mounted = fat_mounted,
1477 .unmounted = fat_unmounted,
1478 .read = fat_read,
1479 .write = fat_write,
1480 .truncate = fat_truncate,
1481 .close = fat_close,
1482 .destroy = fat_destroy,
1483 .sync = fat_sync,
1484};
1485
1486/**
1487 * @}
1488 */
Note: See TracBrowser for help on using the repository browser.