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

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