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

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f783081 was f783081, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

libfs's service_get() returns service ID for service-special files

Not the service ID of the owning file system. Any file-system except
locfs should return zero.

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