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

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

exfat: Do not ignore the return value of exfat_node_put().

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