source: mainline/uspace/srv/fs/exfat/exfat_ops.c@ f061de75

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

exFAT: add exfat_directory_open_parent for managing parent directory
without node pointer

  • Property mode set to 100644
File size: 35.1 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 exfat_ops.c
36 * @brief Implementation of VFS operations for the exFAT file system server.
37 */
38
39#include "exfat.h"
40#include "exfat_fat.h"
41#include "exfat_dentry.h"
42#include "exfat_directory.h"
43#include "exfat_bitmap.h"
44#include "../../vfs/vfs.h"
45#include <libfs.h>
46#include <libblock.h>
47#include <ipc/services.h>
48#include <ipc/devmap.h>
49#include <macros.h>
50#include <async.h>
51#include <errno.h>
52#include <str.h>
53#include <byteorder.h>
54#include <adt/hash_table.h>
55#include <adt/list.h>
56#include <assert.h>
57#include <fibril_synch.h>
58#include <sys/mman.h>
59#include <align.h>
60#include <malloc.h>
61#include <stdio.h>
62
63/** Mutex protecting the list of cached free FAT nodes. */
64static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
65
66/** List of cached free FAT nodes. */
67static LIST_INITIALIZE(ffn_list);
68
69/*
70 * Forward declarations of FAT libfs operations.
71 */
72
73static int exfat_root_get(fs_node_t **, devmap_handle_t);
74static int exfat_match(fs_node_t **, fs_node_t *, const char *);
75static int exfat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
76static int exfat_node_open(fs_node_t *);
77/* static int exfat_node_put(fs_node_t *); */
78static int exfat_create_node(fs_node_t **, devmap_handle_t, int);
79static int exfat_destroy_node(fs_node_t *);
80static int exfat_link(fs_node_t *, fs_node_t *, const char *);
81static int exfat_unlink(fs_node_t *, fs_node_t *, const char *);
82static int exfat_has_children(bool *, fs_node_t *);
83static fs_index_t exfat_index_get(fs_node_t *);
84static aoff64_t exfat_size_get(fs_node_t *);
85static unsigned exfat_lnkcnt_get(fs_node_t *);
86static bool exfat_is_directory(fs_node_t *);
87static bool exfat_is_file(fs_node_t *node);
88static devmap_handle_t exfat_device_get(fs_node_t *node);
89
90/*
91 * Helper functions.
92 */
93static void exfat_node_initialize(exfat_node_t *node)
94{
95 fibril_mutex_initialize(&node->lock);
96 node->bp = NULL;
97 node->idx = NULL;
98 node->type = EXFAT_UNKNOW;
99 link_initialize(&node->ffn_link);
100 node->size = 0;
101 node->lnkcnt = 0;
102 node->refcnt = 0;
103 node->dirty = false;
104 node->fragmented = false;
105 node->lastc_cached_valid = false;
106 node->lastc_cached_value = 0;
107 node->currc_cached_valid = false;
108 node->currc_cached_bn = 0;
109 node->currc_cached_value = 0;
110}
111
112static int exfat_node_sync(exfat_node_t *node)
113{
114// block_t *b;
115// exfat_bs_t *bs;
116// fat_dentry_t *d;
117// int rc;
118
119// assert(node->dirty);
120
121// bs = block_bb_get(node->idx->devmap_handle);
122
123 /* Read the block that contains the dentry of interest. */
124/*
125 rc = _fat_block_get(&b, bs, node->idx->devmap_handle, node->idx->pfc,
126 NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
127 BLOCK_FLAGS_NONE);
128 if (rc != EOK)
129 return rc;
130
131 d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));
132
133 d->firstc = host2uint16_t_le(node->firstc);
134 if (node->type == FAT_FILE) {
135 d->size = host2uint32_t_le(node->size);
136 } else if (node->type == FAT_DIRECTORY) {
137 d->attr = FAT_ATTR_SUBDIR;
138 }
139*/
140 /* TODO: update other fields? (e.g time fields) */
141
142// b->dirty = true; /* need to sync block */
143// rc = block_put(b);
144// return rc;
145 return EOK;
146}
147
148static int exfat_node_fini_by_devmap_handle(devmap_handle_t devmap_handle)
149{
150 exfat_node_t *nodep;
151 int rc;
152
153 /*
154 * We are called from fat_unmounted() and assume that there are already
155 * no nodes belonging to this instance with non-zero refcount. Therefore
156 * it is sufficient to clean up only the FAT free node list.
157 */
158
159restart:
160 fibril_mutex_lock(&ffn_mutex);
161 list_foreach(ffn_list, lnk) {
162 nodep = list_get_instance(lnk, exfat_node_t, ffn_link);
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->devmap_handle != devmap_handle) {
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 = exfat_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 the ffn_list. */
199 goto restart;
200 }
201 fibril_mutex_unlock(&ffn_mutex);
202
203 return EOK;
204}
205
206static int exfat_node_get_new(exfat_node_t **nodepp)
207{
208 fs_node_t *fn;
209 exfat_node_t *nodep;
210 int rc;
211
212 fibril_mutex_lock(&ffn_mutex);
213 if (!list_empty(&ffn_list)) {
214 /* Try to use a cached free node structure. */
215 exfat_idx_t *idxp_tmp;
216 nodep = list_get_instance(list_first(&ffn_list), exfat_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 = exfat_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 {
243skip_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 = (exfat_node_t *)malloc(sizeof(exfat_node_t));
250 if (!nodep) {
251 free(fn);
252 return ENOMEM;
253 }
254 }
255 exfat_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
264static int exfat_node_get_new_by_pos(exfat_node_t **nodepp,
265 devmap_handle_t devmap_handle, exfat_cluster_t pfc, unsigned pdi)
266{
267 exfat_idx_t *idxp = exfat_idx_get_by_pos(devmap_handle, pfc, pdi);
268 if (!idxp)
269 return ENOMEM;
270 if (exfat_node_get_new(nodepp) != EOK)
271 return ENOMEM;
272 (*nodepp)->idx = idxp;
273 idxp->nodep = *nodepp;
274 return EOK;
275}
276
277
278/** Internal version of exfat_node_get().
279 *
280 * @param idxp Locked index structure.
281 */
282static int exfat_node_get_core(exfat_node_t **nodepp, exfat_idx_t *idxp)
283{
284 block_t *b=NULL;
285 exfat_bs_t *bs;
286 exfat_dentry_t *d;
287 exfat_node_t *nodep = NULL;
288 int rc;
289
290 if (idxp->nodep) {
291 /*
292 * We are lucky.
293 * The node is already instantiated in memory.
294 */
295 fibril_mutex_lock(&idxp->nodep->lock);
296 if (!idxp->nodep->refcnt++) {
297 fibril_mutex_lock(&ffn_mutex);
298 list_remove(&idxp->nodep->ffn_link);
299 fibril_mutex_unlock(&ffn_mutex);
300 }
301 fibril_mutex_unlock(&idxp->nodep->lock);
302 *nodepp = idxp->nodep;
303 return EOK;
304 }
305
306 /*
307 * We must instantiate the node from the file system.
308 */
309
310 assert(idxp->pfc);
311
312 rc = exfat_node_get_new(&nodep);
313 if (rc != EOK)
314 return rc;
315
316 bs = block_bb_get(idxp->devmap_handle);
317
318 rc = exfat_block_get_by_clst(&b, bs, idxp->devmap_handle,
319 idxp->parent_fragmented, idxp->pfc, NULL,
320 (idxp->pdi * sizeof(exfat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
321 if (rc != EOK) {
322 (void) exfat_node_put(FS_NODE(nodep));
323 return rc;
324 }
325
326 d = ((exfat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
327 switch (exfat_classify_dentry(d)) {
328 case EXFAT_DENTRY_FILE:
329 nodep->type = (d->file.attr & EXFAT_ATTR_SUBDIR)?
330 EXFAT_DIRECTORY : EXFAT_FILE;
331 rc = block_put(b);
332 if (rc != EOK) {
333 (void) exfat_node_put(FS_NODE(nodep));
334 return rc;
335 }
336 rc = exfat_block_get_by_clst(&b, bs, idxp->devmap_handle,
337 idxp->parent_fragmented, idxp->pfc, NULL,
338 ((idxp->pdi+1) * sizeof(exfat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
339 if (rc != EOK) {
340 (void) exfat_node_put(FS_NODE(nodep));
341 return rc;
342 }
343 d = ((exfat_dentry_t *)b->data) + ((idxp->pdi+1) % DPS(bs));
344
345 nodep->firstc = d->stream.firstc;
346 nodep->size = d->stream.data_size;
347 nodep->fragmented = (d->stream.flags & 0x02) == 0;
348 break;
349 case EXFAT_DENTRY_BITMAP:
350 nodep->type = EXFAT_BITMAP;
351 nodep->firstc = d->bitmap.firstc;
352 nodep->size = d->bitmap.size;
353 nodep->fragmented = true;
354 break;
355 case EXFAT_DENTRY_UCTABLE:
356 nodep->type = EXFAT_UCTABLE;
357 nodep->firstc = d->uctable.firstc;
358 nodep->size = d->uctable.size;
359 nodep->fragmented = true;
360 break;
361 default:
362 case EXFAT_DENTRY_SKIP:
363 case EXFAT_DENTRY_LAST:
364 case EXFAT_DENTRY_FREE:
365 case EXFAT_DENTRY_VOLLABEL:
366 case EXFAT_DENTRY_GUID:
367 case EXFAT_DENTRY_STREAM:
368 case EXFAT_DENTRY_NAME:
369 (void) block_put(b);
370 (void) exfat_node_put(FS_NODE(nodep));
371 return ENOENT;
372 }
373
374 nodep->lnkcnt = 1;
375 nodep->refcnt = 1;
376
377 rc = block_put(b);
378 if (rc != EOK) {
379 (void) exfat_node_put(FS_NODE(nodep));
380 return rc;
381 }
382
383 /* Link the idx structure with the node structure. */
384 nodep->idx = idxp;
385 idxp->nodep = nodep;
386
387 *nodepp = nodep;
388 return EOK;
389}
390
391static int exfat_node_expand(devmap_handle_t devmap_handle, exfat_node_t *nodep, exfat_cluster_t clusters)
392{
393 exfat_bs_t *bs;
394 int rc;
395 bs = block_bb_get(devmap_handle);
396
397 if (nodep->fragmented) {
398 rc = bitmap_append_clusters(bs, nodep, clusters);
399 if (rc != ENOSPC)
400 return rc;
401 if (rc == ENOSPC) {
402 nodep->fragmented = true;
403 nodep->dirty = true; /* need to sync node */
404 rc = bitmap_replicate_clusters(bs, nodep);
405 if (rc != EOK)
406 return rc;
407 }
408 }
409
410 /* If we cant linear expand the node, we should use FAT instead */
411 exfat_cluster_t mcl, lcl;
412
413 /* create an independent chain of nclsts clusters in all FATs */
414 rc = exfat_alloc_clusters(bs, devmap_handle, clusters, &mcl, &lcl);
415 if (rc != EOK)
416 return rc;
417 /*
418 * Append the cluster chain starting in mcl to the end of the
419 * node's cluster chain.
420 */
421 rc = exfat_append_clusters(bs, nodep, mcl, lcl);
422 if (rc != EOK) {
423 (void) exfat_free_clusters(bs, devmap_handle, mcl);
424 return rc;
425 }
426
427 return EOK;
428}
429
430static int exfat_node_shrink(devmap_handle_t devmap_handle, exfat_node_t *nodep, aoff64_t size)
431{
432 exfat_bs_t *bs;
433 int rc;
434 bs = block_bb_get(devmap_handle);
435
436 if (nodep->fragmented) {
437 exfat_cluster_t clsts, prev_clsts, new_clsts;
438 prev_clsts = ROUND_UP(nodep->size, BPC(bs)) / BPC(bs);
439 new_clsts = ROUND_UP(size, BPC(bs)) / BPC(bs);
440
441 assert(new_clsts < prev_clsts);
442
443 clsts = prev_clsts - new_clsts;
444 rc = bitmap_free_clusters(bs, nodep, clsts);
445 if (rc != EOK)
446 return rc;
447 } else {
448 /*
449 * The node will be shrunk, clusters will be deallocated.
450 */
451 if (size == 0) {
452 rc = exfat_chop_clusters(bs, nodep, 0);
453 if (rc != EOK)
454 return rc;
455 } else {
456 exfat_cluster_t lastc;
457 rc = exfat_cluster_walk(bs, devmap_handle, nodep->firstc,
458 &lastc, NULL, (size - 1) / BPC(bs));
459 if (rc != EOK)
460 return rc;
461 rc = exfat_chop_clusters(bs, nodep, lastc);
462 if (rc != EOK)
463 return rc;
464 }
465 }
466
467 nodep->size = size;
468 nodep->dirty = true; /* need to sync node */
469 return EOK;
470}
471
472
473/*
474 * EXFAT libfs operations.
475 */
476
477int exfat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
478{
479 return exfat_node_get(rfn, devmap_handle, EXFAT_ROOT_IDX);
480}
481
482int exfat_bitmap_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
483{
484 return exfat_node_get(rfn, devmap_handle, EXFAT_BITMAP_IDX);
485}
486
487/*
488int exfat_uctable_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
489{
490 return exfat_node_get(rfn, devmap_handle, EXFAT_UCTABLE_IDX);
491}
492*/
493
494int exfat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
495{
496 exfat_node_t *parentp = EXFAT_NODE(pfn);
497 char name[EXFAT_FILENAME_LEN+1];
498 exfat_file_dentry_t df;
499 exfat_stream_dentry_t ds;
500 devmap_handle_t devmap_handle;
501 int rc;
502
503 fibril_mutex_lock(&parentp->idx->lock);
504 devmap_handle = parentp->idx->devmap_handle;
505 fibril_mutex_unlock(&parentp->idx->lock);
506
507 exfat_directory_t di;
508 rc = exfat_directory_open(parentp, &di);
509 if (rc != EOK)
510 return rc;
511
512 while (exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
513 &df, &ds) == EOK) {
514 if (stricmp(name, component) == 0) {
515 /* hit */
516 exfat_node_t *nodep;
517 aoff64_t o = di.pos % (BPS(di.bs) / sizeof(exfat_dentry_t));
518 exfat_idx_t *idx = exfat_idx_get_by_pos(devmap_handle,
519 parentp->firstc, di.bnum * DPS(di.bs) + o);
520 if (!idx) {
521 /*
522 * Can happen if memory is low or if we
523 * run out of 32-bit indices.
524 */
525 rc = exfat_directory_close(&di);
526 return (rc == EOK) ? ENOMEM : rc;
527 }
528 rc = exfat_node_get_core(&nodep, idx);
529 fibril_mutex_unlock(&idx->lock);
530 if (rc != EOK) {
531 (void) exfat_directory_close(&di);
532 return rc;
533 }
534 *rfn = FS_NODE(nodep);
535 rc = exfat_directory_close(&di);
536 if (rc != EOK)
537 (void) exfat_node_put(*rfn);
538 return rc;
539 } else {
540 rc = exfat_directory_next(&di);
541 if (rc != EOK)
542 break;
543 }
544 }
545 (void) exfat_directory_close(&di);
546 *rfn = NULL;
547 return EOK;
548}
549
550/** Instantiate a exFAT in-core node. */
551int exfat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
552{
553 exfat_node_t *nodep;
554 exfat_idx_t *idxp;
555 int rc;
556
557 idxp = exfat_idx_get_by_index(devmap_handle, index);
558 if (!idxp) {
559 *rfn = NULL;
560 return EOK;
561 }
562 /* idxp->lock held */
563 rc = exfat_node_get_core(&nodep, idxp);
564 fibril_mutex_unlock(&idxp->lock);
565 if (rc == EOK)
566 *rfn = FS_NODE(nodep);
567 return rc;
568}
569
570int exfat_node_open(fs_node_t *fn)
571{
572 /*
573 * Opening a file is stateless, nothing
574 * to be done here.
575 */
576 return EOK;
577}
578
579int exfat_node_put(fs_node_t *fn)
580{
581 exfat_node_t *nodep = EXFAT_NODE(fn);
582 bool destroy = false;
583
584 fibril_mutex_lock(&nodep->lock);
585 if (!--nodep->refcnt) {
586 if (nodep->idx) {
587 fibril_mutex_lock(&ffn_mutex);
588 list_append(&nodep->ffn_link, &ffn_list);
589 fibril_mutex_unlock(&ffn_mutex);
590 } else {
591 /*
592 * The node does not have any index structure associated
593 * with itself. This can only mean that we are releasing
594 * the node after a failed attempt to allocate the index
595 * structure for it.
596 */
597 destroy = true;
598 }
599 }
600 fibril_mutex_unlock(&nodep->lock);
601 if (destroy) {
602 free(nodep->bp);
603 free(nodep);
604 }
605 return EOK;
606}
607
608int exfat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
609{
610 exfat_idx_t *idxp;
611 exfat_node_t *nodep;
612 int rc;
613
614 rc = exfat_node_get_new(&nodep);
615 if (rc != EOK)
616 return rc;
617
618 rc = exfat_idx_get_new(&idxp, devmap_handle);
619 if (rc != EOK) {
620 (void) exfat_node_put(FS_NODE(nodep));
621 return rc;
622 }
623
624 if (flags & L_DIRECTORY)
625 nodep->type = EXFAT_DIRECTORY;
626 else
627 nodep->type = EXFAT_FILE;
628
629 nodep->firstc = 0;
630 nodep->size = 0;
631 nodep->fragmented = false;
632 nodep->lnkcnt = 0; /* not linked anywhere */
633 nodep->refcnt = 1;
634 nodep->dirty = true;
635
636 nodep->idx = idxp;
637 idxp->nodep = nodep;
638
639 fibril_mutex_unlock(&idxp->lock);
640 *rfn = FS_NODE(nodep);
641 return EOK;
642}
643
644int exfat_destroy_node(fs_node_t *fn)
645{
646 exfat_node_t *nodep = EXFAT_NODE(fn);
647 exfat_bs_t *bs;
648 bool has_children;
649 int rc;
650
651 /*
652 * The node is not reachable from the file system. This means that the
653 * link count should be zero and that the index structure cannot be
654 * found in the position hash. Obviously, we don't need to lock the node
655 * nor its index structure.
656 */
657 assert(nodep->lnkcnt == 0);
658
659 /*
660 * The node may not have any children.
661 */
662 rc = exfat_has_children(&has_children, fn);
663 if (rc != EOK)
664 return rc;
665 assert(!has_children);
666
667 bs = block_bb_get(nodep->idx->devmap_handle);
668 if (nodep->firstc != 0) {
669 assert(nodep->size);
670 /* Free all clusters allocated to the node. */
671 if (nodep->fragmented)
672 rc = exfat_free_clusters(bs, nodep->idx->devmap_handle,
673 nodep->firstc);
674 else
675 rc = bitmap_free_clusters(bs, nodep,
676 ROUND_UP(nodep->size, BPC(bs)) / BPC(bs));
677 }
678
679 exfat_idx_destroy(nodep->idx);
680 free(nodep->bp);
681 free(nodep);
682 return rc;
683}
684
685int exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
686{
687 exfat_node_t *parentp = EXFAT_NODE(pfn);
688 exfat_node_t *childp = EXFAT_NODE(cfn);
689 exfat_directory_t di;
690 int rc;
691
692 fibril_mutex_lock(&childp->lock);
693 if (childp->lnkcnt == 1) {
694 /*
695 * We don't support multiple hard links.
696 */
697 fibril_mutex_unlock(&childp->lock);
698 return EMLINK;
699 }
700 assert(childp->lnkcnt == 0);
701 fibril_mutex_unlock(&childp->lock);
702
703 if (!exfat_valid_name(name))
704 return ENOTSUP;
705
706 fibril_mutex_lock(&parentp->idx->lock);
707 rc = exfat_directory_open(parentp, &di);
708 if (rc != EOK)
709 return rc;
710
711 /*
712 * At this point we only establish the link between the parent and the
713 * child. The dentry, except of the name and the extension, will remain
714 * uninitialized until the corresponding node is synced. Thus the valid
715 * dentry data is kept in the child node structure.
716 */
717 rc = exfat_directory_write_file(&di, name);
718 if (rc!=EOK)
719 return rc;
720 rc = exfat_directory_close(&di);
721 if (rc!=EOK)
722 return rc;
723
724 fibril_mutex_unlock(&parentp->idx->lock);
725 if (rc != EOK)
726 return rc;
727
728 fibril_mutex_lock(&childp->idx->lock);
729
730
731 childp->idx->pfc = parentp->firstc;
732 childp->idx->parent_fragmented = parentp->fragmented;
733 childp->idx->pdi = di.pos; /* di.pos holds absolute position of SFN entry */
734 fibril_mutex_unlock(&childp->idx->lock);
735
736 fibril_mutex_lock(&childp->lock);
737 childp->lnkcnt = 1;
738 childp->dirty = true; /* need to sync node */
739 fibril_mutex_unlock(&childp->lock);
740
741 /*
742 * Hash in the index structure into the position hash.
743 */
744 exfat_idx_hashin(childp->idx);
745
746 return EOK;
747
748}
749
750int exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
751{
752 exfat_node_t *parentp = EXFAT_NODE(pfn);
753 exfat_node_t *childp = EXFAT_NODE(cfn);
754 bool has_children;
755 int rc;
756
757 if (!parentp)
758 return EBUSY;
759
760 rc = exfat_has_children(&has_children, cfn);
761 if (rc != EOK)
762 return rc;
763 if (has_children)
764 return ENOTEMPTY;
765
766 fibril_mutex_lock(&parentp->lock);
767 fibril_mutex_lock(&childp->lock);
768 assert(childp->lnkcnt == 1);
769 fibril_mutex_lock(&childp->idx->lock);
770
771 exfat_directory_t di;
772 rc = exfat_directory_open(parentp,&di);
773 if (rc != EOK)
774 goto error;
775 rc = exfat_directory_erase_file(&di, childp->idx->pdi);
776 if (rc != EOK)
777 goto error;
778 rc = exfat_directory_close(&di);
779 if (rc != EOK)
780 goto error;
781
782 /* remove the index structure from the position hash */
783 exfat_idx_hashout(childp->idx);
784 /* clear position information */
785 childp->idx->pfc = 0;
786 childp->idx->pdi = 0;
787 fibril_mutex_unlock(&childp->idx->lock);
788 childp->lnkcnt = 0;
789 childp->refcnt++; /* keep the node in memory until destroyed */
790 childp->dirty = true;
791 fibril_mutex_unlock(&childp->lock);
792 fibril_mutex_unlock(&parentp->lock);
793
794 return EOK;
795
796error:
797 (void) exfat_directory_close(&di);
798 fibril_mutex_unlock(&childp->idx->lock);
799 fibril_mutex_unlock(&childp->lock);
800 fibril_mutex_unlock(&parentp->lock);
801 return rc;
802
803}
804
805int exfat_has_children(bool *has_children, fs_node_t *fn)
806{
807 exfat_directory_t di;
808 exfat_dentry_t *d;
809 exfat_node_t *nodep = EXFAT_NODE(fn);
810 int rc;
811
812 *has_children = false;
813
814 if (nodep->type != EXFAT_DIRECTORY)
815 return EOK;
816
817 fibril_mutex_lock(&nodep->idx->lock);
818
819 rc = exfat_directory_open(nodep, &di);
820 if (rc != EOK) {
821 fibril_mutex_unlock(&nodep->idx->lock);
822 return rc;
823 }
824
825 do {
826 rc = exfat_directory_get(&di, &d);
827 if (rc != EOK) {
828 (void) exfat_directory_close(&di);
829 fibril_mutex_unlock(&nodep->idx->lock);
830 return rc;
831 }
832 switch (exfat_classify_dentry(d)) {
833 case EXFAT_DENTRY_SKIP:
834 case EXFAT_DENTRY_FREE:
835 continue;
836 case EXFAT_DENTRY_LAST:
837 *has_children = false;
838 goto exit;
839 default:
840 *has_children = true;
841 goto exit;
842 }
843 } while (exfat_directory_next(&di) == EOK);
844
845exit:
846 rc = exfat_directory_close(&di);
847 fibril_mutex_unlock(&nodep->idx->lock);
848 return rc;
849}
850
851
852fs_index_t exfat_index_get(fs_node_t *fn)
853{
854 return EXFAT_NODE(fn)->idx->index;
855}
856
857aoff64_t exfat_size_get(fs_node_t *fn)
858{
859 return EXFAT_NODE(fn)->size;
860}
861
862unsigned exfat_lnkcnt_get(fs_node_t *fn)
863{
864 return EXFAT_NODE(fn)->lnkcnt;
865}
866
867bool exfat_is_directory(fs_node_t *fn)
868{
869 return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
870}
871
872bool exfat_is_file(fs_node_t *fn)
873{
874 return EXFAT_NODE(fn)->type == EXFAT_FILE;
875}
876
877devmap_handle_t exfat_device_get(fs_node_t *node)
878{
879 return 0;
880}
881
882
883/** libfs operations */
884libfs_ops_t exfat_libfs_ops = {
885 .root_get = exfat_root_get,
886 .match = exfat_match,
887 .node_get = exfat_node_get,
888 .node_open = exfat_node_open,
889 .node_put = exfat_node_put,
890 .create = exfat_create_node,
891 .destroy = exfat_destroy_node,
892 .link = exfat_link,
893 .unlink = exfat_unlink,
894 .has_children = exfat_has_children,
895 .index_get = exfat_index_get,
896 .size_get = exfat_size_get,
897 .lnkcnt_get = exfat_lnkcnt_get,
898 .is_directory = exfat_is_directory,
899 .is_file = exfat_is_file,
900 .device_get = exfat_device_get
901};
902
903
904/*
905 * VFS_OUT operations.
906 */
907
908/* Print debug info */
909static void exfat_fsinfo(exfat_bs_t *bs, devmap_handle_t devmap_handle)
910{
911 printf("exFAT file system mounted\n");
912 printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
913 printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
914 printf("Volume first sector: %lld\n", VOL_FS(bs));
915 printf("Volume sectors: %lld\n", VOL_CNT(bs));
916 printf("FAT first sector: %d\n", FAT_FS(bs));
917 printf("FAT sectors: %d\n", FAT_CNT(bs));
918 printf("Data first sector: %d\n", DATA_FS(bs));
919 printf("Data sectors: %d\n", DATA_CNT(bs));
920 printf("Root dir first cluster: %d\n", ROOT_FC(bs));
921 printf("Bytes per sector: %d\n", BPS(bs));
922 printf("Sectors per cluster: %d\n", SPC(bs));
923 printf("KBytes per cluster: %d\n", SPC(bs)*BPS(bs)/1024);
924
925 /* bitmap_set_cluster(bs, devmap_handle, 9); */
926 /* bitmap_clear_cluster(bs, devmap_handle, 9); */
927
928 int i, rc;
929 exfat_cluster_t clst;
930 for (i=0; i<=10; i++) {
931 rc = exfat_get_cluster(bs, devmap_handle, i, &clst);
932 if (rc != EOK)
933 return;
934 printf("Clst %d: %x", i, clst);
935 if (i>=2)
936 printf(", Bitmap: %d\n", bitmap_is_free(bs, devmap_handle, i)!=EOK);
937 else
938 printf("\n");
939 }
940}
941
942static int
943exfat_mounted(devmap_handle_t devmap_handle, const char *opts, fs_index_t *index,
944 aoff64_t *size, unsigned *linkcnt)
945{
946 int rc;
947 exfat_node_t *rootp=NULL, *bitmapp=NULL, *uctablep=NULL;
948 enum cache_mode cmode;
949 exfat_bs_t *bs;
950
951 /* Check for option enabling write through. */
952 if (str_cmp(opts, "wtcache") == 0)
953 cmode = CACHE_MODE_WT;
954 else
955 cmode = CACHE_MODE_WB;
956
957 /* initialize libblock */
958 rc = block_init(EXCHANGE_SERIALIZE, devmap_handle, BS_SIZE);
959 if (rc != EOK)
960 return rc;
961
962 /* prepare the boot block */
963 rc = block_bb_read(devmap_handle, BS_BLOCK);
964 if (rc != EOK) {
965 block_fini(devmap_handle);
966 return rc;
967 }
968
969 /* get the buffer with the boot sector */
970 bs = block_bb_get(devmap_handle);
971
972 /* Initialize the block cache */
973 rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
974 if (rc != EOK) {
975 block_fini(devmap_handle);
976 return rc;
977 }
978
979 /* Do some simple sanity checks on the file system. */
980 rc = exfat_sanity_check(bs, devmap_handle);
981 if (rc != EOK) {
982 (void) block_cache_fini(devmap_handle);
983 block_fini(devmap_handle);
984 return rc;
985 }
986
987 rc = exfat_idx_init_by_devmap_handle(devmap_handle);
988 if (rc != EOK) {
989 (void) block_cache_fini(devmap_handle);
990 block_fini(devmap_handle);
991 return rc;
992 }
993
994 /* Initialize the root node. */
995 rc = exfat_node_get_new_by_pos(&rootp, devmap_handle, EXFAT_ROOT_PAR,
996 EXFAT_ROOT_POS);
997 if (rc!=EOK) {
998 (void) block_cache_fini(devmap_handle);
999 block_fini(devmap_handle);
1000 exfat_idx_fini_by_devmap_handle(devmap_handle);
1001 return ENOMEM;
1002 }
1003 assert(rootp->idx->index == EXFAT_ROOT_IDX);
1004
1005 rootp->type = EXFAT_DIRECTORY;
1006 rootp->firstc = ROOT_FC(bs);
1007 rootp->fragmented = true;
1008 rootp->refcnt = 1;
1009 rootp->lnkcnt = 0; /* FS root is not linked */
1010
1011 uint32_t clusters;
1012 rc = exfat_clusters_get(&clusters, bs, devmap_handle, rootp->firstc);
1013 if (rc != EOK) {
1014 free(rootp);
1015 (void) block_cache_fini(devmap_handle);
1016 block_fini(devmap_handle);
1017 exfat_idx_fini_by_devmap_handle(devmap_handle);
1018 return ENOTSUP;
1019 }
1020 rootp->size = BPS(bs) * SPC(bs) * clusters;
1021 fibril_mutex_unlock(&rootp->idx->lock);
1022
1023 /* Open root directory and looking for Bitmap and UC-Table */
1024 exfat_directory_t di;
1025 exfat_dentry_t *de;
1026 rc = exfat_directory_open(rootp, &di);
1027 if (rc != EOK) {
1028 free(rootp);
1029 (void) block_cache_fini(devmap_handle);
1030 block_fini(devmap_handle);
1031 exfat_idx_fini_by_devmap_handle(devmap_handle);
1032 return ENOTSUP;
1033 }
1034
1035 /* Initialize the bitmap node. */
1036 rc = exfat_directory_find(&di, EXFAT_DENTRY_BITMAP, &de);
1037 if (rc != EOK) {
1038 free(rootp);
1039 (void) block_cache_fini(devmap_handle);
1040 block_fini(devmap_handle);
1041 exfat_idx_fini_by_devmap_handle(devmap_handle);
1042 return ENOTSUP;
1043 }
1044
1045 rc = exfat_node_get_new_by_pos(&bitmapp, devmap_handle, rootp->firstc,
1046 di.pos);
1047 if (rc!=EOK) {
1048 free(rootp);
1049 (void) block_cache_fini(devmap_handle);
1050 block_fini(devmap_handle);
1051 exfat_idx_fini_by_devmap_handle(devmap_handle);
1052 return ENOMEM;
1053 }
1054 assert(bitmapp->idx->index == EXFAT_BITMAP_IDX);
1055 fibril_mutex_unlock(&bitmapp->idx->lock);
1056
1057 bitmapp->type = EXFAT_BITMAP;
1058 bitmapp->firstc = de->bitmap.firstc;
1059 bitmapp->fragmented = true;
1060 bitmapp->idx->parent_fragmented = true;
1061 bitmapp->refcnt = 1;
1062 bitmapp->lnkcnt = 0;
1063 bitmapp->size = de->bitmap.size;
1064
1065 /* Initialize the uctable node. */
1066 rc = exfat_directory_seek(&di, 0);
1067 if (rc != EOK) {
1068 free(rootp);
1069 free(bitmapp);
1070 (void) block_cache_fini(devmap_handle);
1071 block_fini(devmap_handle);
1072 exfat_idx_fini_by_devmap_handle(devmap_handle);
1073 return ENOTSUP;
1074 }
1075
1076 rc = exfat_directory_find(&di, EXFAT_DENTRY_UCTABLE, &de);
1077 if (rc != EOK) {
1078 free(rootp);
1079 free(bitmapp);
1080 (void) block_cache_fini(devmap_handle);
1081 block_fini(devmap_handle);
1082 exfat_idx_fini_by_devmap_handle(devmap_handle);
1083 return ENOTSUP;
1084 }
1085
1086 rc = exfat_node_get_new_by_pos(&uctablep, devmap_handle, rootp->firstc,
1087 di.pos);
1088 if (rc!=EOK) {
1089 free(rootp);
1090 free(bitmapp);
1091 (void) block_cache_fini(devmap_handle);
1092 block_fini(devmap_handle);
1093 exfat_idx_fini_by_devmap_handle(devmap_handle);
1094 return ENOMEM;
1095 }
1096 assert(uctablep->idx->index == EXFAT_UCTABLE_IDX);
1097 fibril_mutex_unlock(&uctablep->idx->lock);
1098
1099 uctablep->type = EXFAT_UCTABLE;
1100 uctablep->firstc = de->uctable.firstc;
1101 uctablep->fragmented = true;
1102 uctablep->idx->parent_fragmented = true;
1103 uctablep->refcnt = 1;
1104 uctablep->lnkcnt = 0;
1105 uctablep->size = de->uctable.size;
1106
1107 rc = exfat_directory_close(&di);
1108 if (rc!=EOK) {
1109 free(rootp);
1110 free(bitmapp);
1111 free(uctablep);
1112 (void) block_cache_fini(devmap_handle);
1113 block_fini(devmap_handle);
1114 exfat_idx_fini_by_devmap_handle(devmap_handle);
1115 return ENOMEM;
1116 }
1117
1118 exfat_fsinfo(bs, devmap_handle);
1119 printf("Root dir size: %lld\n", rootp->size);
1120
1121 *index = rootp->idx->index;
1122 *size = rootp->size;
1123 *linkcnt = rootp->lnkcnt;
1124
1125 return EOK;
1126}
1127
1128static int exfat_unmounted(devmap_handle_t devmap_handle)
1129{
1130 fs_node_t *fn;
1131 exfat_node_t *nodep;
1132 int rc;
1133
1134 rc = exfat_root_get(&fn, devmap_handle);
1135 if (rc != EOK)
1136 return rc;
1137 nodep = EXFAT_NODE(fn);
1138
1139 /*
1140 * We expect exactly two references on the root node. One for the
1141 * fat_root_get() above and one created in fat_mounted().
1142 */
1143 if (nodep->refcnt != 2) {
1144 (void) exfat_node_put(fn);
1145 return EBUSY;
1146 }
1147
1148 /*
1149 * Put the root node and force it to the FAT free node list.
1150 */
1151 (void) exfat_node_put(fn);
1152 (void) exfat_node_put(fn);
1153
1154 /*
1155 * Perform cleanup of the node structures, index structures and
1156 * associated data. Write back this file system's dirty blocks and
1157 * stop using libblock for this instance.
1158 */
1159 (void) exfat_node_fini_by_devmap_handle(devmap_handle);
1160 exfat_idx_fini_by_devmap_handle(devmap_handle);
1161 (void) block_cache_fini(devmap_handle);
1162 block_fini(devmap_handle);
1163
1164 return EOK;
1165}
1166
1167/*
1168int bitmap_is_allocated(exfat_bs_t *bs, devmap_handle_t devmap_handle,
1169 exfat_cluster_t clst, bool *status)
1170{
1171 fs_node_t *fn;
1172 exfat_node_t *bitmap;
1173 int rc;
1174
1175 rc = exfat_bitmap_get(&fn, devmap_handle);
1176 if (rc != EOK)
1177 return rc;
1178
1179 nbitmap = EXFAT_NODE(fn);
1180
1181
1182 return EOK;
1183}
1184*/
1185
1186static int
1187exfat_read(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
1188 size_t *rbytes)
1189{
1190 fs_node_t *fn;
1191 exfat_node_t *nodep;
1192 exfat_bs_t *bs;
1193 size_t bytes=0;
1194 block_t *b;
1195 int rc;
1196
1197 rc = exfat_node_get(&fn, devmap_handle, index);
1198 if (rc != EOK)
1199 return rc;
1200 if (!fn)
1201 return ENOENT;
1202 nodep = EXFAT_NODE(fn);
1203
1204 ipc_callid_t callid;
1205 size_t len;
1206 if (!async_data_read_receive(&callid, &len)) {
1207 exfat_node_put(fn);
1208 async_answer_0(callid, EINVAL);
1209 return EINVAL;
1210 }
1211
1212 bs = block_bb_get(devmap_handle);
1213
1214 if (nodep->type == EXFAT_FILE) {
1215 /*
1216 * Our strategy for regular file reads is to read one block at
1217 * most and make use of the possibility to return less data than
1218 * requested. This keeps the code very simple.
1219 */
1220 if (pos >= nodep->size) {
1221 /* reading beyond the EOF */
1222 bytes = 0;
1223 (void) async_data_read_finalize(callid, NULL, 0);
1224 } else {
1225 bytes = min(len, BPS(bs) - pos % BPS(bs));
1226 bytes = min(bytes, nodep->size - pos);
1227 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
1228 BLOCK_FLAGS_NONE);
1229 if (rc != EOK) {
1230 exfat_node_put(fn);
1231 async_answer_0(callid, rc);
1232 return rc;
1233 }
1234 (void) async_data_read_finalize(callid,
1235 b->data + pos % BPS(bs), bytes);
1236 rc = block_put(b);
1237 if (rc != EOK) {
1238 exfat_node_put(fn);
1239 return rc;
1240 }
1241 }
1242 } else {
1243 if (nodep->type != EXFAT_DIRECTORY) {
1244 async_answer_0(callid, ENOTSUP);
1245 return ENOTSUP;
1246 }
1247
1248 aoff64_t spos = pos;
1249 char name[EXFAT_FILENAME_LEN+1];
1250 exfat_file_dentry_t df;
1251 exfat_stream_dentry_t ds;
1252
1253 assert(nodep->size % BPS(bs) == 0);
1254 assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
1255
1256 exfat_directory_t di;
1257 rc = exfat_directory_open(nodep, &di);
1258 if (rc != EOK) goto err;
1259 rc = exfat_directory_seek(&di, pos);
1260 if (rc != EOK) {
1261 (void) exfat_directory_close(&di);
1262 goto err;
1263 }
1264
1265 rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN, &df, &ds);
1266 if (rc == EOK) goto hit;
1267 if (rc == ENOENT) goto miss;
1268
1269err:
1270 (void) exfat_node_put(fn);
1271 async_answer_0(callid, rc);
1272 return rc;
1273
1274miss:
1275 rc = exfat_directory_close(&di);
1276 if (rc!=EOK)
1277 goto err;
1278 rc = exfat_node_put(fn);
1279 async_answer_0(callid, rc != EOK ? rc : ENOENT);
1280 *rbytes = 0;
1281 return rc != EOK ? rc : ENOENT;
1282
1283hit:
1284 pos = di.pos;
1285 rc = exfat_directory_close(&di);
1286 if (rc!=EOK)
1287 goto err;
1288 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
1289 bytes = (pos - spos)+1;
1290 }
1291
1292 rc = exfat_node_put(fn);
1293 *rbytes = bytes;
1294 return rc;
1295}
1296
1297static int exfat_close(devmap_handle_t devmap_handle, fs_index_t index)
1298{
1299 return EOK;
1300}
1301
1302static int exfat_sync(devmap_handle_t devmap_handle, fs_index_t index)
1303{
1304 fs_node_t *fn;
1305 int rc = exfat_node_get(&fn, devmap_handle, index);
1306 if (rc != EOK)
1307 return rc;
1308 if (!fn)
1309 return ENOENT;
1310
1311 exfat_node_t *nodep = EXFAT_NODE(fn);
1312
1313 nodep->dirty = true;
1314 rc = exfat_node_sync(nodep);
1315
1316 exfat_node_put(fn);
1317 return rc;
1318}
1319
1320static int
1321exfat_write(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
1322 size_t *wbytes, aoff64_t *nsize)
1323{
1324 fs_node_t *fn;
1325 exfat_node_t *nodep;
1326 exfat_bs_t *bs;
1327 size_t bytes;
1328 block_t *b;
1329 aoff64_t boundary;
1330 int flags = BLOCK_FLAGS_NONE;
1331 int rc;
1332
1333 rc = exfat_node_get(&fn, devmap_handle, index);
1334 if (rc != EOK)
1335 return rc;
1336 if (!fn)
1337 return ENOENT;
1338 nodep = EXFAT_NODE(fn);
1339
1340 ipc_callid_t callid;
1341 size_t len;
1342 if (!async_data_write_receive(&callid, &len)) {
1343 (void) exfat_node_put(fn);
1344 async_answer_0(callid, EINVAL);
1345 return EINVAL;
1346 }
1347
1348 bs = block_bb_get(devmap_handle);
1349
1350 /*
1351 * In all scenarios, we will attempt to write out only one block worth
1352 * of data at maximum. There might be some more efficient approaches,
1353 * but this one greatly simplifies fat_write(). Note that we can afford
1354 * to do this because the client must be ready to handle the return
1355 * value signalizing a smaller number of bytes written.
1356 */
1357 bytes = min(len, BPS(bs) - pos % BPS(bs));
1358 if (bytes == BPS(bs))
1359 flags |= BLOCK_FLAGS_NOREAD;
1360
1361 boundary = ROUND_UP(nodep->size, BPC(bs));
1362
1363 if (pos >= boundary) {
1364 unsigned nclsts;
1365 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1366 rc = exfat_node_expand(devmap_handle, nodep, nclsts);
1367 if (rc != EOK) {
1368 /* could not expand node */
1369 (void) exfat_node_put(fn);
1370 async_answer_0(callid, rc);
1371 return rc;
1372 }
1373 }
1374
1375 if (pos + bytes > nodep->size) {
1376 nodep->size = pos + bytes;
1377 nodep->dirty = true; /* need to sync node */
1378 }
1379
1380 /*
1381 * This is the easier case - we are either overwriting already
1382 * existing contents or writing behind the EOF, but still within
1383 * the limits of the last cluster. The node size may grow to the
1384 * next block size boundary.
1385 */
1386 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1387 if (rc != EOK) {
1388 (void) exfat_node_put(fn);
1389 async_answer_0(callid, rc);
1390 return rc;
1391 }
1392
1393 (void) async_data_write_finalize(callid,
1394 b->data + pos % BPS(bs), bytes);
1395 b->dirty = true; /* need to sync block */
1396 rc = block_put(b);
1397 if (rc != EOK) {
1398 (void) exfat_node_put(fn);
1399 return rc;
1400 }
1401
1402
1403 *wbytes = bytes;
1404 *nsize = nodep->size;
1405 rc = exfat_node_put(fn);
1406 return rc;
1407}
1408
1409static int
1410exfat_truncate(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t size)
1411{
1412 fs_node_t *fn;
1413 exfat_node_t *nodep;
1414 exfat_bs_t *bs;
1415 int rc;
1416
1417 rc = exfat_node_get(&fn, devmap_handle, index);
1418 if (rc != EOK)
1419 return rc;
1420 if (!fn)
1421 return ENOENT;
1422 nodep = EXFAT_NODE(fn);
1423
1424 bs = block_bb_get(devmap_handle);
1425
1426 if (nodep->size == size) {
1427 rc = EOK;
1428 } else if (nodep->size < size) {
1429 /*
1430 * The standard says we have the freedom to grow the node.
1431 * For now, we simply return an error.
1432 */
1433 rc = EINVAL;
1434 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1435 /*
1436 * The node will be shrunk, but no clusters will be deallocated.
1437 */
1438 nodep->size = size;
1439 nodep->dirty = true; /* need to sync node */
1440 rc = EOK;
1441 } else {
1442 rc = exfat_node_shrink(devmap_handle, nodep, size);
1443 }
1444
1445 (void) exfat_node_put(fn);
1446 return rc;
1447}
1448
1449static int exfat_destroy(devmap_handle_t devmap_handle, fs_index_t index)
1450{
1451 fs_node_t *fn;
1452 exfat_node_t *nodep;
1453 int rc;
1454
1455 rc = exfat_node_get(&fn, devmap_handle, index);
1456 if (rc != EOK)
1457 return rc;
1458 if (!fn)
1459 return ENOENT;
1460
1461 nodep = EXFAT_NODE(fn);
1462 /*
1463 * We should have exactly two references. One for the above
1464 * call to fat_node_get() and one from fat_unlink().
1465 */
1466 assert(nodep->refcnt == 2);
1467
1468 rc = exfat_destroy_node(fn);
1469 return rc;
1470}
1471
1472vfs_out_ops_t exfat_ops = {
1473 .mounted = exfat_mounted,
1474 .unmounted = exfat_unmounted,
1475 .read = exfat_read,
1476 .write = exfat_write,
1477 .truncate = exfat_truncate,
1478 .close = exfat_close,
1479 .destroy = exfat_destroy,
1480 .sync = exfat_sync,
1481};
1482
1483
1484/**
1485 * @}
1486 */
Note: See TracBrowser for help on using the repository browser.