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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 87159eb8 was 87159eb8, checked in by Manuele Conti <conti.ma@…>, 12 years ago

Add get total and free block operation for exfat.
Note: allocated_percent doesn't work.

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