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

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

exfat: exfat_create_node(), set the content of the first cluster of new dirs to zero, failure
to do so will corrupt the exfat filesystem.

  • 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
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
660 rc = exfat_zero_cluster(bs, service_id, nodep->firstc);
661 if (rc != EOK) {
662 (void) exfat_node_put(FS_NODE(nodep));
663 return rc;
664 }
665
666 nodep->size = BPC(bs);
667 } else {
668 nodep->type = EXFAT_FILE;
669 }
670
671 *rfn = FS_NODE(nodep);
672 return EOK;
673}
674
675int exfat_destroy_node(fs_node_t *fn)
676{
677 exfat_node_t *nodep = EXFAT_NODE(fn);
678 exfat_bs_t *bs;
679 bool has_children;
680 int rc;
681
682 /*
683 * The node is not reachable from the file system. This means that the
684 * link count should be zero and that the index structure cannot be
685 * found in the position hash. Obviously, we don't need to lock the node
686 * nor its index structure.
687 */
688 assert(nodep->lnkcnt == 0);
689
690 /*
691 * The node may not have any children.
692 */
693 rc = exfat_has_children(&has_children, fn);
694 if (rc != EOK)
695 return rc;
696 assert(!has_children);
697
698 bs = block_bb_get(nodep->idx->service_id);
699 if (nodep->firstc != 0) {
700 assert(nodep->size);
701 /* Free all clusters allocated to the node. */
702 if (nodep->fragmented)
703 rc = exfat_free_clusters(bs, nodep->idx->service_id,
704 nodep->firstc);
705 else
706 rc = bitmap_free_clusters(bs, nodep,
707 ROUND_UP(nodep->size, BPC(bs)) / BPC(bs));
708 }
709
710 exfat_idx_destroy(nodep->idx);
711 free(nodep->bp);
712 free(nodep);
713 return rc;
714}
715
716int exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
717{
718 exfat_node_t *parentp = EXFAT_NODE(pfn);
719 exfat_node_t *childp = EXFAT_NODE(cfn);
720 exfat_directory_t di;
721 int rc;
722
723 fibril_mutex_lock(&childp->lock);
724 if (childp->lnkcnt == 1) {
725 /*
726 * We don't support multiple hard links.
727 */
728 fibril_mutex_unlock(&childp->lock);
729 return EMLINK;
730 }
731 assert(childp->lnkcnt == 0);
732 fibril_mutex_unlock(&childp->lock);
733
734 if (!exfat_valid_name(name))
735 return ENOTSUP;
736
737 fibril_mutex_lock(&parentp->idx->lock);
738 rc = exfat_directory_open(parentp, &di);
739 if (rc != EOK)
740 return rc;
741 /*
742 * At this point we only establish the link between the parent and the
743 * child. The dentry, except of the name and the extension, will remain
744 * uninitialized until the corresponding node is synced. Thus the valid
745 * dentry data is kept in the child node structure.
746 */
747 rc = exfat_directory_write_file(&di, name);
748 if (rc != EOK) {
749 (void) exfat_directory_close(&di);
750 fibril_mutex_unlock(&parentp->idx->lock);
751 return rc;
752 }
753 rc = exfat_directory_close(&di);
754 if (rc != EOK) {
755 fibril_mutex_unlock(&parentp->idx->lock);
756 return rc;
757 }
758
759 fibril_mutex_unlock(&parentp->idx->lock);
760 if (rc != EOK)
761 return rc;
762
763 fibril_mutex_lock(&childp->idx->lock);
764
765
766 childp->idx->pfc = parentp->firstc;
767 childp->idx->parent_fragmented = parentp->fragmented;
768 childp->idx->pdi = di.pos;
769 fibril_mutex_unlock(&childp->idx->lock);
770
771 fibril_mutex_lock(&childp->lock);
772 childp->lnkcnt = 1;
773 childp->dirty = true; /* need to sync node */
774 fibril_mutex_unlock(&childp->lock);
775
776 /*
777 * Hash in the index structure into the position hash.
778 */
779 exfat_idx_hashin(childp->idx);
780
781 return EOK;
782
783}
784
785int exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
786{
787 exfat_node_t *parentp = EXFAT_NODE(pfn);
788 exfat_node_t *childp = EXFAT_NODE(cfn);
789 bool has_children;
790 int rc;
791
792 if (!parentp)
793 return EBUSY;
794
795 rc = exfat_has_children(&has_children, cfn);
796 if (rc != EOK)
797 return rc;
798 if (has_children)
799 return ENOTEMPTY;
800
801 fibril_mutex_lock(&parentp->lock);
802 fibril_mutex_lock(&childp->lock);
803 assert(childp->lnkcnt == 1);
804 fibril_mutex_lock(&childp->idx->lock);
805
806 exfat_directory_t di;
807 rc = exfat_directory_open(parentp,&di);
808 if (rc != EOK)
809 goto error;
810 rc = exfat_directory_erase_file(&di, childp->idx->pdi);
811 if (rc != EOK)
812 goto error;
813 rc = exfat_directory_close(&di);
814 if (rc != EOK)
815 goto error;
816
817 /* remove the index structure from the position hash */
818 exfat_idx_hashout(childp->idx);
819 /* clear position information */
820 childp->idx->pfc = 0;
821 childp->idx->pdi = 0;
822 fibril_mutex_unlock(&childp->idx->lock);
823 childp->lnkcnt = 0;
824 childp->refcnt++; /* keep the node in memory until destroyed */
825 childp->dirty = true;
826 fibril_mutex_unlock(&childp->lock);
827 fibril_mutex_unlock(&parentp->lock);
828
829 return EOK;
830
831error:
832 (void) exfat_directory_close(&di);
833 fibril_mutex_unlock(&childp->idx->lock);
834 fibril_mutex_unlock(&childp->lock);
835 fibril_mutex_unlock(&parentp->lock);
836 return rc;
837
838}
839
840int exfat_has_children(bool *has_children, fs_node_t *fn)
841{
842 exfat_directory_t di;
843 exfat_dentry_t *d;
844 exfat_node_t *nodep = EXFAT_NODE(fn);
845 int rc;
846
847 *has_children = false;
848
849 if (nodep->type != EXFAT_DIRECTORY)
850 return EOK;
851
852 fibril_mutex_lock(&nodep->idx->lock);
853
854 rc = exfat_directory_open(nodep, &di);
855 if (rc != EOK) {
856 fibril_mutex_unlock(&nodep->idx->lock);
857 return rc;
858 }
859
860 do {
861 rc = exfat_directory_get(&di, &d);
862 if (rc != EOK) {
863 (void) exfat_directory_close(&di);
864 fibril_mutex_unlock(&nodep->idx->lock);
865 return rc;
866 }
867 switch (exfat_classify_dentry(d)) {
868 case EXFAT_DENTRY_SKIP:
869 case EXFAT_DENTRY_FREE:
870 continue;
871 case EXFAT_DENTRY_LAST:
872 *has_children = false;
873 goto exit;
874 default:
875 *has_children = true;
876 goto exit;
877 }
878 } while (exfat_directory_next(&di) == EOK);
879
880exit:
881 rc = exfat_directory_close(&di);
882 fibril_mutex_unlock(&nodep->idx->lock);
883 return rc;
884}
885
886
887fs_index_t exfat_index_get(fs_node_t *fn)
888{
889 return EXFAT_NODE(fn)->idx->index;
890}
891
892aoff64_t exfat_size_get(fs_node_t *fn)
893{
894 return EXFAT_NODE(fn)->size;
895}
896
897unsigned exfat_lnkcnt_get(fs_node_t *fn)
898{
899 return EXFAT_NODE(fn)->lnkcnt;
900}
901
902bool exfat_is_directory(fs_node_t *fn)
903{
904 return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
905}
906
907bool exfat_is_file(fs_node_t *fn)
908{
909 return EXFAT_NODE(fn)->type == EXFAT_FILE;
910}
911
912service_id_t exfat_service_get(fs_node_t *node)
913{
914 return 0;
915}
916
917
918/** libfs operations */
919libfs_ops_t exfat_libfs_ops = {
920 .root_get = exfat_root_get,
921 .match = exfat_match,
922 .node_get = exfat_node_get,
923 .node_open = exfat_node_open,
924 .node_put = exfat_node_put,
925 .create = exfat_create_node,
926 .destroy = exfat_destroy_node,
927 .link = exfat_link,
928 .unlink = exfat_unlink,
929 .has_children = exfat_has_children,
930 .index_get = exfat_index_get,
931 .size_get = exfat_size_get,
932 .lnkcnt_get = exfat_lnkcnt_get,
933 .is_directory = exfat_is_directory,
934 .is_file = exfat_is_file,
935 .service_get = exfat_service_get
936};
937
938
939/*
940 * VFS_OUT operations.
941 */
942
943/* Print debug info */
944/*
945static void exfat_fsinfo(exfat_bs_t *bs, service_id_t service_id)
946{
947 printf("exFAT file system mounted\n");
948 printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
949 printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
950 printf("Volume first sector: %lld\n", VOL_FS(bs));
951 printf("Volume sectors: %lld\n", VOL_CNT(bs));
952 printf("FAT first sector: %d\n", FAT_FS(bs));
953 printf("FAT sectors: %d\n", FAT_CNT(bs));
954 printf("Data first sector: %d\n", DATA_FS(bs));
955 printf("Data sectors: %d\n", DATA_CNT(bs));
956 printf("Root dir first cluster: %d\n", ROOT_FC(bs));
957 printf("Bytes per sector: %d\n", BPS(bs));
958 printf("Sectors per cluster: %d\n", SPC(bs));
959 printf("KBytes per cluster: %d\n", SPC(bs)*BPS(bs)/1024);
960
961 int i, rc;
962 exfat_cluster_t clst;
963 for (i=0; i<=7; i++) {
964 rc = exfat_get_cluster(bs, service_id, i, &clst);
965 if (rc != EOK)
966 return;
967 printf("Clst %d: %x", i, clst);
968 if (i>=2)
969 printf(", Bitmap: %d\n", bitmap_is_free(bs, service_id, i)!=EOK);
970 else
971 printf("\n");
972 }
973}
974*/
975
976static int
977exfat_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
978 aoff64_t *size, unsigned *linkcnt)
979{
980 int rc;
981 exfat_node_t *rootp = NULL, *bitmapp = NULL, *uctablep = NULL;
982 enum cache_mode cmode;
983 exfat_bs_t *bs;
984
985 /* Check for option enabling write through. */
986 if (str_cmp(opts, "wtcache") == 0)
987 cmode = CACHE_MODE_WT;
988 else
989 cmode = CACHE_MODE_WB;
990
991 /* initialize libblock */
992 rc = block_init(EXCHANGE_SERIALIZE, service_id, BS_SIZE);
993 if (rc != EOK)
994 return rc;
995
996 /* prepare the boot block */
997 rc = block_bb_read(service_id, BS_BLOCK);
998 if (rc != EOK) {
999 block_fini(service_id);
1000 return rc;
1001 }
1002
1003 /* get the buffer with the boot sector */
1004 bs = block_bb_get(service_id);
1005
1006 /* Initialize the block cache */
1007 rc = block_cache_init(service_id, BPS(bs), 0 /* XXX */, cmode);
1008 if (rc != EOK) {
1009 block_fini(service_id);
1010 return rc;
1011 }
1012
1013 /* Do some simple sanity checks on the file system. */
1014 rc = exfat_sanity_check(bs, service_id);
1015 if (rc != EOK) {
1016 (void) block_cache_fini(service_id);
1017 block_fini(service_id);
1018 return rc;
1019 }
1020
1021 rc = exfat_idx_init_by_service_id(service_id);
1022 if (rc != EOK) {
1023 (void) block_cache_fini(service_id);
1024 block_fini(service_id);
1025 return rc;
1026 }
1027
1028 /* Initialize the root node. */
1029 rc = exfat_node_get_new_by_pos(&rootp, service_id, EXFAT_ROOT_PAR,
1030 EXFAT_ROOT_POS);
1031 if (rc!=EOK) {
1032 (void) block_cache_fini(service_id);
1033 block_fini(service_id);
1034 exfat_idx_fini_by_service_id(service_id);
1035 return ENOMEM;
1036 }
1037 assert(rootp->idx->index == EXFAT_ROOT_IDX);
1038
1039 rootp->type = EXFAT_DIRECTORY;
1040 rootp->firstc = ROOT_FC(bs);
1041 rootp->fragmented = true;
1042 rootp->refcnt = 1;
1043 rootp->lnkcnt = 0; /* FS root is not linked */
1044
1045 uint32_t clusters;
1046 rc = exfat_clusters_get(&clusters, bs, service_id, rootp->firstc);
1047 if (rc != EOK) {
1048 free(rootp);
1049 (void) block_cache_fini(service_id);
1050 block_fini(service_id);
1051 exfat_idx_fini_by_service_id(service_id);
1052 return ENOTSUP;
1053 }
1054 rootp->size = BPS(bs) * SPC(bs) * clusters;
1055 fibril_mutex_unlock(&rootp->idx->lock);
1056
1057 /* Open root directory and looking for Bitmap and UC-Table */
1058 exfat_directory_t di;
1059 exfat_dentry_t *de;
1060 rc = exfat_directory_open(rootp, &di);
1061 if (rc != EOK) {
1062 free(rootp);
1063 (void) block_cache_fini(service_id);
1064 block_fini(service_id);
1065 exfat_idx_fini_by_service_id(service_id);
1066 return ENOTSUP;
1067 }
1068
1069 /* Initialize the bitmap node. */
1070 rc = exfat_directory_find(&di, EXFAT_DENTRY_BITMAP, &de);
1071 if (rc != EOK) {
1072 free(rootp);
1073 (void) block_cache_fini(service_id);
1074 block_fini(service_id);
1075 exfat_idx_fini_by_service_id(service_id);
1076 return ENOTSUP;
1077 }
1078
1079 rc = exfat_node_get_new_by_pos(&bitmapp, service_id, rootp->firstc,
1080 di.pos);
1081 if (rc != EOK) {
1082 free(rootp);
1083 (void) block_cache_fini(service_id);
1084 block_fini(service_id);
1085 exfat_idx_fini_by_service_id(service_id);
1086 return ENOMEM;
1087 }
1088 assert(bitmapp->idx->index == EXFAT_BITMAP_IDX);
1089 fibril_mutex_unlock(&bitmapp->idx->lock);
1090
1091 bitmapp->type = EXFAT_BITMAP;
1092 bitmapp->firstc = uint32_t_le2host(de->bitmap.firstc);
1093 bitmapp->fragmented = true;
1094 bitmapp->idx->parent_fragmented = true;
1095 bitmapp->refcnt = 1;
1096 bitmapp->lnkcnt = 0;
1097 bitmapp->size = uint64_t_le2host(de->bitmap.size);
1098
1099 /* Initialize the uctable node. */
1100 rc = exfat_directory_seek(&di, 0);
1101 if (rc != EOK) {
1102 free(rootp);
1103 free(bitmapp);
1104 (void) block_cache_fini(service_id);
1105 block_fini(service_id);
1106 exfat_idx_fini_by_service_id(service_id);
1107 return ENOTSUP;
1108 }
1109
1110 rc = exfat_directory_find(&di, EXFAT_DENTRY_UCTABLE, &de);
1111 if (rc != EOK) {
1112 free(rootp);
1113 free(bitmapp);
1114 (void) block_cache_fini(service_id);
1115 block_fini(service_id);
1116 exfat_idx_fini_by_service_id(service_id);
1117 return ENOTSUP;
1118 }
1119
1120 rc = exfat_node_get_new_by_pos(&uctablep, service_id, rootp->firstc,
1121 di.pos);
1122 if (rc != EOK) {
1123 free(rootp);
1124 free(bitmapp);
1125 (void) block_cache_fini(service_id);
1126 block_fini(service_id);
1127 exfat_idx_fini_by_service_id(service_id);
1128 return ENOMEM;
1129 }
1130 assert(uctablep->idx->index == EXFAT_UCTABLE_IDX);
1131 fibril_mutex_unlock(&uctablep->idx->lock);
1132
1133 uctablep->type = EXFAT_UCTABLE;
1134 uctablep->firstc = uint32_t_le2host(de->uctable.firstc);
1135 uctablep->fragmented = true;
1136 uctablep->idx->parent_fragmented = true;
1137 uctablep->refcnt = 1;
1138 uctablep->lnkcnt = 0;
1139 uctablep->size = uint64_t_le2host(de->uctable.size);
1140
1141 rc = exfat_directory_close(&di);
1142 if (rc != EOK) {
1143 free(rootp);
1144 free(bitmapp);
1145 free(uctablep);
1146 (void) block_cache_fini(service_id);
1147 block_fini(service_id);
1148 exfat_idx_fini_by_service_id(service_id);
1149 return ENOMEM;
1150 }
1151
1152 /* exfat_fsinfo(bs, service_id); */
1153
1154 *index = rootp->idx->index;
1155 *size = rootp->size;
1156 *linkcnt = rootp->lnkcnt;
1157
1158 return EOK;
1159}
1160
1161static int exfat_unmounted(service_id_t service_id)
1162{
1163 fs_node_t *fn;
1164 exfat_node_t *nodep;
1165 int rc;
1166
1167 rc = exfat_root_get(&fn, service_id);
1168 if (rc != EOK)
1169 return rc;
1170 nodep = EXFAT_NODE(fn);
1171
1172 /*
1173 * We expect exactly two references on the root node. One for the
1174 * fat_root_get() above and one created in fat_mounted().
1175 */
1176 if (nodep->refcnt != 2) {
1177 (void) exfat_node_put(fn);
1178 return EBUSY;
1179 }
1180
1181 /*
1182 * Put the root node and force it to the FAT free node list.
1183 */
1184 (void) exfat_node_put(fn);
1185 (void) exfat_node_put(fn);
1186
1187 /*
1188 * Perform cleanup of the node structures, index structures and
1189 * associated data. Write back this file system's dirty blocks and
1190 * stop using libblock for this instance.
1191 */
1192 (void) exfat_node_fini_by_service_id(service_id);
1193 exfat_idx_fini_by_service_id(service_id);
1194 (void) block_cache_fini(service_id);
1195 block_fini(service_id);
1196
1197 return EOK;
1198}
1199
1200static int
1201exfat_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
1202 size_t *rbytes)
1203{
1204 fs_node_t *fn;
1205 exfat_node_t *nodep;
1206 exfat_bs_t *bs;
1207 size_t bytes = 0;
1208 block_t *b;
1209 int rc;
1210
1211 rc = exfat_node_get(&fn, service_id, index);
1212 if (rc != EOK)
1213 return rc;
1214 if (!fn)
1215 return ENOENT;
1216 nodep = EXFAT_NODE(fn);
1217
1218 ipc_callid_t callid;
1219 size_t len;
1220 if (!async_data_read_receive(&callid, &len)) {
1221 exfat_node_put(fn);
1222 async_answer_0(callid, EINVAL);
1223 return EINVAL;
1224 }
1225
1226 bs = block_bb_get(service_id);
1227
1228 if (nodep->type == EXFAT_FILE) {
1229 /*
1230 * Our strategy for regular file reads is to read one block at
1231 * most and make use of the possibility to return less data than
1232 * requested. This keeps the code very simple.
1233 */
1234 if (pos >= nodep->size) {
1235 /* reading beyond the EOF */
1236 bytes = 0;
1237 (void) async_data_read_finalize(callid, NULL, 0);
1238 } else {
1239 bytes = min(len, BPS(bs) - pos % BPS(bs));
1240 bytes = min(bytes, nodep->size - pos);
1241 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
1242 BLOCK_FLAGS_NONE);
1243 if (rc != EOK) {
1244 exfat_node_put(fn);
1245 async_answer_0(callid, rc);
1246 return rc;
1247 }
1248 (void) async_data_read_finalize(callid,
1249 b->data + pos % BPS(bs), bytes);
1250 rc = block_put(b);
1251 if (rc != EOK) {
1252 exfat_node_put(fn);
1253 return rc;
1254 }
1255 }
1256 } else {
1257 if (nodep->type != EXFAT_DIRECTORY) {
1258 async_answer_0(callid, ENOTSUP);
1259 return ENOTSUP;
1260 }
1261
1262 aoff64_t spos = pos;
1263 char name[EXFAT_FILENAME_LEN + 1];
1264 exfat_file_dentry_t df;
1265 exfat_stream_dentry_t ds;
1266
1267 assert(nodep->size % BPS(bs) == 0);
1268 assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
1269
1270 exfat_directory_t di;
1271 rc = exfat_directory_open(nodep, &di);
1272 if (rc != EOK)
1273 goto err;
1274
1275 rc = exfat_directory_seek(&di, pos);
1276 if (rc != EOK) {
1277 (void) exfat_directory_close(&di);
1278 goto err;
1279 }
1280
1281 rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
1282 &df, &ds);
1283 if (rc == EOK)
1284 goto hit;
1285 else if (rc == ENOENT)
1286 goto miss;
1287
1288 (void) exfat_directory_close(&di);
1289
1290err:
1291 (void) exfat_node_put(fn);
1292 async_answer_0(callid, rc);
1293 return rc;
1294
1295miss:
1296 rc = exfat_directory_close(&di);
1297 if (rc != EOK)
1298 goto err;
1299 rc = exfat_node_put(fn);
1300 async_answer_0(callid, rc != EOK ? rc : ENOENT);
1301 *rbytes = 0;
1302 return rc != EOK ? rc : ENOENT;
1303
1304hit:
1305 pos = di.pos;
1306 rc = exfat_directory_close(&di);
1307 if (rc != EOK)
1308 goto err;
1309 (void) async_data_read_finalize(callid, name,
1310 str_size(name) + 1);
1311 bytes = (pos - spos) + 1;
1312 }
1313
1314 rc = exfat_node_put(fn);
1315 *rbytes = bytes;
1316 return rc;
1317}
1318
1319static int exfat_close(service_id_t service_id, fs_index_t index)
1320{
1321 return EOK;
1322}
1323
1324static int exfat_sync(service_id_t service_id, fs_index_t index)
1325{
1326 fs_node_t *fn;
1327 int rc = exfat_node_get(&fn, service_id, index);
1328 if (rc != EOK)
1329 return rc;
1330 if (!fn)
1331 return ENOENT;
1332
1333 exfat_node_t *nodep = EXFAT_NODE(fn);
1334
1335 nodep->dirty = true;
1336 rc = exfat_node_sync(nodep);
1337
1338 exfat_node_put(fn);
1339 return rc;
1340}
1341
1342static int
1343exfat_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
1344 size_t *wbytes, aoff64_t *nsize)
1345{
1346 fs_node_t *fn;
1347 exfat_node_t *nodep;
1348 exfat_bs_t *bs;
1349 size_t bytes;
1350 block_t *b;
1351 aoff64_t boundary;
1352 int flags = BLOCK_FLAGS_NONE;
1353 int rc;
1354
1355 rc = exfat_node_get(&fn, service_id, index);
1356 if (rc != EOK)
1357 return rc;
1358 if (!fn)
1359 return ENOENT;
1360 nodep = EXFAT_NODE(fn);
1361
1362 ipc_callid_t callid;
1363 size_t len;
1364 if (!async_data_write_receive(&callid, &len)) {
1365 (void) exfat_node_put(fn);
1366 async_answer_0(callid, EINVAL);
1367 return EINVAL;
1368 }
1369
1370 bs = block_bb_get(service_id);
1371
1372 /*
1373 * In all scenarios, we will attempt to write out only one block worth
1374 * of data at maximum. There might be some more efficient approaches,
1375 * but this one greatly simplifies fat_write(). Note that we can afford
1376 * to do this because the client must be ready to handle the return
1377 * value signalizing a smaller number of bytes written.
1378 */
1379 bytes = min(len, BPS(bs) - pos % BPS(bs));
1380 if (bytes == BPS(bs))
1381 flags |= BLOCK_FLAGS_NOREAD;
1382
1383 boundary = ROUND_UP(nodep->size, BPC(bs));
1384 if (pos >= boundary) {
1385 unsigned nclsts;
1386 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1387 rc = exfat_node_expand(service_id, nodep, nclsts);
1388 if (rc != EOK) {
1389 /* could not expand node */
1390 (void) exfat_node_put(fn);
1391 async_answer_0(callid, rc);
1392 return rc;
1393 }
1394 }
1395
1396 if (pos + bytes > nodep->size) {
1397 nodep->size = pos + bytes;
1398 nodep->dirty = true; /* need to sync node */
1399 }
1400
1401 /*
1402 * This is the easier case - we are either overwriting already
1403 * existing contents or writing behind the EOF, but still within
1404 * the limits of the last cluster. The node size may grow to the
1405 * next block size boundary.
1406 */
1407 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1408 if (rc != EOK) {
1409 (void) exfat_node_put(fn);
1410 async_answer_0(callid, rc);
1411 return rc;
1412 }
1413
1414 (void) async_data_write_finalize(callid,
1415 b->data + pos % BPS(bs), bytes);
1416 b->dirty = true; /* need to sync block */
1417 rc = block_put(b);
1418 if (rc != EOK) {
1419 (void) exfat_node_put(fn);
1420 return rc;
1421 }
1422
1423 *wbytes = bytes;
1424 *nsize = nodep->size;
1425 rc = exfat_node_put(fn);
1426 return rc;
1427}
1428
1429static int
1430exfat_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
1431{
1432 fs_node_t *fn;
1433 exfat_node_t *nodep;
1434 exfat_bs_t *bs;
1435 int rc;
1436
1437 rc = exfat_node_get(&fn, service_id, index);
1438 if (rc != EOK)
1439 return rc;
1440 if (!fn)
1441 return ENOENT;
1442 nodep = EXFAT_NODE(fn);
1443
1444 bs = block_bb_get(service_id);
1445
1446 if (nodep->size == size) {
1447 rc = EOK;
1448 } else if (nodep->size < size) {
1449 /*
1450 * The standard says we have the freedom to grow the node.
1451 * For now, we simply return an error.
1452 */
1453 rc = EINVAL;
1454 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1455 /*
1456 * The node will be shrunk, but no clusters will be deallocated.
1457 */
1458 nodep->size = size;
1459 nodep->dirty = true; /* need to sync node */
1460 rc = EOK;
1461 } else {
1462 rc = exfat_node_shrink(service_id, nodep, size);
1463 }
1464
1465 (void) exfat_node_put(fn);
1466 return rc;
1467}
1468
1469static int exfat_destroy(service_id_t service_id, fs_index_t index)
1470{
1471 fs_node_t *fn;
1472 exfat_node_t *nodep;
1473 int rc;
1474
1475 rc = exfat_node_get(&fn, service_id, index);
1476 if (rc != EOK)
1477 return rc;
1478 if (!fn)
1479 return ENOENT;
1480
1481 nodep = EXFAT_NODE(fn);
1482 /*
1483 * We should have exactly two references. One for the above
1484 * call to fat_node_get() and one from fat_unlink().
1485 */
1486 assert(nodep->refcnt == 2);
1487
1488 rc = exfat_destroy_node(fn);
1489 return rc;
1490}
1491
1492vfs_out_ops_t exfat_ops = {
1493 .mounted = exfat_mounted,
1494 .unmounted = exfat_unmounted,
1495 .read = exfat_read,
1496 .write = exfat_write,
1497 .truncate = exfat_truncate,
1498 .close = exfat_close,
1499 .destroy = exfat_destroy,
1500 .sync = exfat_sync,
1501};
1502
1503/**
1504 * @}
1505 */
Note: See TracBrowser for help on using the repository browser.