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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 92d9406 was 6722766, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

exfat: exfat_read(), close the directory instance in case of error.

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