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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0041cd6d was 062d900, checked in by Jakub Jermar <jakub@…>, 13 years ago

Cherrypick userspace hash table changes from lp:~adam-hraska+lp/helenos/rcu/.

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