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

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

Categorize the remaining orphan doxygroups

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