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

Last change on this file was 7ae01d5, checked in by Jiri Svoboda <jiri@…>, 15 months ago

Remove unused comm_size parameter of block_init()

  • Property mode set to 100644
File size: 38.1 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * Copyright (c) 2008 Jakub Jermar
4 * Copyright (c) 2011 Oleg Romanenko
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup exfat
32 * @{
33 */
34
35/**
36 * @file exfat_ops.c
37 * @brief Implementation of VFS operations for the exFAT file system
38 * server.
39 */
40
41#include "exfat.h"
42#include "exfat_fat.h"
43#include "exfat_dentry.h"
44#include "exfat_directory.h"
45#include "exfat_bitmap.h"
46#include "../../vfs/vfs.h"
47#include <libfs.h>
48#include <block.h>
49#include <ipc/services.h>
50#include <ipc/loc.h>
51#include <macros.h>
52#include <async.h>
53#include <errno.h>
54#include <str.h>
55#include <byteorder.h>
56#include <adt/hash_table.h>
57#include <adt/hash.h>
58#include <adt/list.h>
59#include <assert.h>
60#include <fibril_synch.h>
61#include <align.h>
62#include <stdio.h>
63#include <stdlib.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 void exfat_fsinfo(exfat_bs_t *, service_id_t);
76static errno_t exfat_root_get(fs_node_t **, service_id_t);
77static errno_t exfat_match(fs_node_t **, fs_node_t *, const char *);
78static errno_t exfat_node_get(fs_node_t **, service_id_t, fs_index_t);
79static errno_t exfat_node_open(fs_node_t *);
80/* static errno_t exfat_node_put(fs_node_t *); */
81static errno_t exfat_create_node(fs_node_t **, service_id_t, int);
82static errno_t exfat_destroy_node(fs_node_t *);
83static errno_t exfat_link(fs_node_t *, fs_node_t *, const char *);
84static errno_t exfat_unlink(fs_node_t *, fs_node_t *, const char *);
85static errno_t exfat_has_children(bool *, fs_node_t *);
86static fs_index_t exfat_index_get(fs_node_t *);
87static aoff64_t exfat_size_get(fs_node_t *);
88static unsigned exfat_lnkcnt_get(fs_node_t *);
89static bool exfat_is_directory(fs_node_t *);
90static bool exfat_is_file(fs_node_t *node);
91static service_id_t exfat_service_get(fs_node_t *node);
92static errno_t exfat_size_block(service_id_t, uint32_t *);
93static errno_t exfat_total_block_count(service_id_t, uint64_t *);
94static errno_t exfat_free_block_count(service_id_t, uint64_t *);
95
96/*
97 * Helper functions.
98 */
99static void exfat_node_initialize(exfat_node_t *node)
100{
101 fibril_mutex_initialize(&node->lock);
102 node->bp = NULL;
103 node->idx = NULL;
104 node->type = EXFAT_UNKNOW;
105 link_initialize(&node->ffn_link);
106 node->size = 0;
107 node->lnkcnt = 0;
108 node->refcnt = 0;
109 node->dirty = false;
110 node->fragmented = false;
111 node->lastc_cached_valid = false;
112 node->lastc_cached_value = 0;
113 node->currc_cached_valid = false;
114 node->currc_cached_bn = 0;
115 node->currc_cached_value = 0;
116}
117
118static errno_t exfat_node_sync(exfat_node_t *node)
119{
120 errno_t rc;
121 exfat_directory_t di;
122 exfat_file_dentry_t df;
123 exfat_stream_dentry_t ds;
124
125 if (!(node->type == EXFAT_DIRECTORY || node->type == EXFAT_FILE))
126 return EOK;
127
128 if (node->type == EXFAT_DIRECTORY)
129 df.attr = EXFAT_ATTR_SUBDIR;
130 else
131 df.attr = 0;
132
133 ds.firstc = node->firstc;
134 if (node->size == 0 && node->firstc == 0) {
135 ds.flags = 0;
136 } else {
137 ds.flags = 1;
138 ds.flags |= (!node->fragmented << 1);
139 }
140 ds.valid_data_size = node->size;
141 ds.data_size = node->size;
142
143 exfat_directory_open_parent(&di, node->idx->service_id, node->idx->pfc,
144 node->idx->parent_fragmented);
145 rc = exfat_directory_seek(&di, node->idx->pdi);
146 if (rc != EOK) {
147 (void) exfat_directory_close(&di);
148 return rc;
149 }
150
151 rc = exfat_directory_sync_file(&di, &df, &ds);
152 if (rc != EOK) {
153 (void) exfat_directory_close(&di);
154 return rc;
155 }
156 return exfat_directory_close(&di);
157}
158
159static errno_t exfat_node_fini_by_service_id(service_id_t service_id)
160{
161 errno_t 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, ffn_link, exfat_node_t, nodep) {
172 if (!fibril_mutex_trylock(&nodep->lock)) {
173 fibril_mutex_unlock(&ffn_mutex);
174 goto restart;
175 }
176 if (!fibril_mutex_trylock(&nodep->idx->lock)) {
177 fibril_mutex_unlock(&nodep->lock);
178 fibril_mutex_unlock(&ffn_mutex);
179 goto restart;
180 }
181 if (nodep->idx->service_id != service_id) {
182 fibril_mutex_unlock(&nodep->idx->lock);
183 fibril_mutex_unlock(&nodep->lock);
184 continue;
185 }
186
187 list_remove(&nodep->ffn_link);
188 fibril_mutex_unlock(&ffn_mutex);
189
190 /*
191 * We can unlock the node and its index structure because we are
192 * the last player on this playground and VFS is preventing new
193 * players from entering.
194 */
195 fibril_mutex_unlock(&nodep->idx->lock);
196 fibril_mutex_unlock(&nodep->lock);
197
198 if (nodep->dirty) {
199 rc = exfat_node_sync(nodep);
200 if (rc != EOK)
201 return rc;
202 }
203 nodep->idx->nodep = NULL;
204 free(nodep->bp);
205 free(nodep);
206
207 /* Need to restart because we changed the ffn_list. */
208 goto restart;
209 }
210 fibril_mutex_unlock(&ffn_mutex);
211
212 return EOK;
213}
214
215static errno_t exfat_node_get_new(exfat_node_t **nodepp)
216{
217 fs_node_t *fn;
218 exfat_node_t *nodep;
219 errno_t rc;
220
221 fibril_mutex_lock(&ffn_mutex);
222 if (!list_empty(&ffn_list)) {
223 /* Try to use a cached free node structure. */
224 exfat_idx_t *idxp_tmp;
225 nodep = list_get_instance(list_first(&ffn_list), exfat_node_t,
226 ffn_link);
227 if (!fibril_mutex_trylock(&nodep->lock))
228 goto skip_cache;
229 idxp_tmp = nodep->idx;
230 if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
231 fibril_mutex_unlock(&nodep->lock);
232 goto skip_cache;
233 }
234 list_remove(&nodep->ffn_link);
235 fibril_mutex_unlock(&ffn_mutex);
236 if (nodep->dirty) {
237 rc = exfat_node_sync(nodep);
238 if (rc != EOK) {
239 idxp_tmp->nodep = NULL;
240 fibril_mutex_unlock(&nodep->lock);
241 fibril_mutex_unlock(&idxp_tmp->lock);
242 free(nodep->bp);
243 free(nodep);
244 return rc;
245 }
246 }
247 idxp_tmp->nodep = NULL;
248 fibril_mutex_unlock(&nodep->lock);
249 fibril_mutex_unlock(&idxp_tmp->lock);
250 fn = FS_NODE(nodep);
251 } else {
252 skip_cache:
253 /* Try to allocate a new node structure. */
254 fibril_mutex_unlock(&ffn_mutex);
255 fn = (fs_node_t *)malloc(sizeof(fs_node_t));
256 if (!fn)
257 return ENOMEM;
258 nodep = (exfat_node_t *)malloc(sizeof(exfat_node_t));
259 if (!nodep) {
260 free(fn);
261 return ENOMEM;
262 }
263 }
264 exfat_node_initialize(nodep);
265 fs_node_initialize(fn);
266 fn->data = nodep;
267 nodep->bp = fn;
268
269 *nodepp = nodep;
270 return EOK;
271}
272
273static errno_t exfat_node_get_new_by_pos(exfat_node_t **nodepp,
274 service_id_t service_id, exfat_cluster_t pfc, unsigned pdi)
275{
276 exfat_idx_t *idxp = exfat_idx_get_by_pos(service_id, pfc, pdi);
277 if (!idxp)
278 return ENOMEM;
279 if (exfat_node_get_new(nodepp) != EOK)
280 return ENOMEM;
281 (*nodepp)->idx = idxp;
282 idxp->nodep = *nodepp;
283 return EOK;
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 * EXFAT libfs operations.
491 */
492
493errno_t 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
498errno_t 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
503errno_t 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
508errno_t exfat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
509{
510 exfat_node_t *parentp = EXFAT_NODE(pfn);
511 char name[EXFAT_FILENAME_LEN + 1];
512 exfat_file_dentry_t df;
513 exfat_stream_dentry_t ds;
514 service_id_t service_id;
515 errno_t rc;
516
517 fibril_mutex_lock(&parentp->idx->lock);
518 service_id = parentp->idx->service_id;
519 fibril_mutex_unlock(&parentp->idx->lock);
520
521 exfat_directory_t di;
522 rc = exfat_directory_open(parentp, &di);
523 if (rc != EOK)
524 return rc;
525
526 while (exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN, &df,
527 &ds) == EOK) {
528 if (str_casecmp(name, component) == 0) {
529 /* hit */
530 exfat_node_t *nodep;
531 aoff64_t o = di.pos %
532 (BPS(di.bs) / sizeof(exfat_dentry_t));
533 exfat_idx_t *idx = exfat_idx_get_by_pos(service_id,
534 parentp->firstc, di.bnum * DPS(di.bs) + o);
535 if (!idx) {
536 /*
537 * Can happen if memory is low or if we
538 * run out of 32-bit indices.
539 */
540 rc = exfat_directory_close(&di);
541 return (rc == EOK) ? ENOMEM : rc;
542 }
543 rc = exfat_node_get_core(&nodep, idx);
544 fibril_mutex_unlock(&idx->lock);
545 if (rc != EOK) {
546 (void) exfat_directory_close(&di);
547 return rc;
548 }
549 *rfn = FS_NODE(nodep);
550 rc = exfat_directory_close(&di);
551 if (rc != EOK)
552 (void) exfat_node_put(*rfn);
553 return rc;
554 } else {
555 rc = exfat_directory_next(&di);
556 if (rc != EOK)
557 break;
558 }
559 }
560 (void) exfat_directory_close(&di);
561 *rfn = NULL;
562 return EOK;
563}
564
565/** Instantiate a exFAT in-core node. */
566errno_t exfat_node_get(fs_node_t **rfn, service_id_t service_id, fs_index_t index)
567{
568 exfat_node_t *nodep;
569 exfat_idx_t *idxp;
570 errno_t rc;
571
572 idxp = exfat_idx_get_by_index(service_id, index);
573 if (!idxp) {
574 *rfn = NULL;
575 return EOK;
576 }
577 /* idxp->lock held */
578 rc = exfat_node_get_core(&nodep, idxp);
579 fibril_mutex_unlock(&idxp->lock);
580 if (rc == EOK)
581 *rfn = FS_NODE(nodep);
582 return rc;
583}
584
585errno_t exfat_node_open(fs_node_t *fn)
586{
587 /*
588 * Opening a file is stateless, nothing
589 * to be done here.
590 */
591 return EOK;
592}
593
594errno_t exfat_node_put(fs_node_t *fn)
595{
596 if (fn == NULL)
597 return EOK;
598
599 exfat_node_t *nodep = EXFAT_NODE(fn);
600 bool destroy = false;
601
602 fibril_mutex_lock(&nodep->lock);
603 if (!--nodep->refcnt) {
604 if (nodep->idx) {
605 fibril_mutex_lock(&ffn_mutex);
606 list_append(&nodep->ffn_link, &ffn_list);
607 fibril_mutex_unlock(&ffn_mutex);
608 } else {
609 /*
610 * The node does not have any index structure associated
611 * with itself. This can only mean that we are releasing
612 * the node after a failed attempt to allocate the index
613 * structure for it.
614 */
615 destroy = true;
616 }
617 }
618 fibril_mutex_unlock(&nodep->lock);
619 if (destroy) {
620 free(nodep->bp);
621 free(nodep);
622 }
623 return EOK;
624}
625
626errno_t exfat_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
627{
628 exfat_idx_t *idxp;
629 exfat_node_t *nodep;
630 exfat_bs_t *bs;
631 errno_t rc;
632
633 bs = block_bb_get(service_id);
634 rc = exfat_node_get_new(&nodep);
635 if (rc != EOK)
636 return rc;
637
638 rc = exfat_idx_get_new(&idxp, service_id);
639 if (rc != EOK) {
640 (void) exfat_node_put(FS_NODE(nodep));
641 return rc;
642 }
643
644 nodep->firstc = 0;
645 nodep->size = 0;
646 nodep->fragmented = false;
647 nodep->lnkcnt = 0; /* not linked anywhere */
648 nodep->refcnt = 1;
649 nodep->dirty = true;
650
651 nodep->idx = idxp;
652 idxp->nodep = nodep;
653 fibril_mutex_unlock(&idxp->lock);
654
655 if (flags & L_DIRECTORY) {
656 nodep->type = EXFAT_DIRECTORY;
657 rc = exfat_node_expand(service_id, nodep, 1);
658 if (rc != EOK) {
659 (void) exfat_node_put(FS_NODE(nodep));
660 return rc;
661 }
662
663 rc = exfat_zero_cluster(bs, service_id, nodep->firstc);
664 if (rc != EOK) {
665 (void) exfat_node_put(FS_NODE(nodep));
666 return rc;
667 }
668
669 nodep->size = BPC(bs);
670 } else {
671 nodep->type = EXFAT_FILE;
672 }
673
674 *rfn = FS_NODE(nodep);
675 return EOK;
676}
677
678errno_t exfat_destroy_node(fs_node_t *fn)
679{
680 exfat_node_t *nodep = EXFAT_NODE(fn);
681 exfat_bs_t *bs;
682 bool has_children;
683 errno_t rc;
684
685 /*
686 * The node is not reachable from the file system. This means that the
687 * link count should be zero and that the index structure cannot be
688 * found in the position hash. Obviously, we don't need to lock the node
689 * nor its index structure.
690 */
691 assert(nodep->lnkcnt == 0);
692
693 /*
694 * The node may not have any children.
695 */
696 rc = exfat_has_children(&has_children, fn);
697 if (rc != EOK)
698 return rc;
699 assert(!has_children);
700
701 bs = block_bb_get(nodep->idx->service_id);
702 if (nodep->firstc != 0) {
703 assert(nodep->size);
704 /* Free all clusters allocated to the node. */
705 if (nodep->fragmented)
706 rc = exfat_free_clusters(bs, nodep->idx->service_id,
707 nodep->firstc);
708 else
709 rc = exfat_bitmap_free_clusters(bs, nodep,
710 ROUND_UP(nodep->size, BPC(bs)) / BPC(bs));
711 }
712
713 exfat_idx_destroy(nodep->idx);
714 free(nodep->bp);
715 free(nodep);
716 return rc;
717}
718
719errno_t exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
720{
721 exfat_node_t *parentp = EXFAT_NODE(pfn);
722 exfat_node_t *childp = EXFAT_NODE(cfn);
723 exfat_directory_t di;
724 errno_t rc;
725
726 fibril_mutex_lock(&childp->lock);
727 if (childp->lnkcnt == 1) {
728 /*
729 * We don't support multiple hard links.
730 */
731 fibril_mutex_unlock(&childp->lock);
732 return EMLINK;
733 }
734 assert(childp->lnkcnt == 0);
735 fibril_mutex_unlock(&childp->lock);
736
737 if (!exfat_valid_name(name))
738 return ENOTSUP;
739
740 fibril_mutex_lock(&parentp->idx->lock);
741 rc = exfat_directory_open(parentp, &di);
742 if (rc != EOK)
743 return rc;
744 /*
745 * At this point we only establish the link between the parent and the
746 * child. The dentry, except of the name and the extension, will remain
747 * uninitialized until the corresponding node is synced. Thus the valid
748 * dentry data is kept in the child node structure.
749 */
750 rc = exfat_directory_write_file(&di, name);
751 if (rc != EOK) {
752 (void) exfat_directory_close(&di);
753 fibril_mutex_unlock(&parentp->idx->lock);
754 return rc;
755 }
756 rc = exfat_directory_close(&di);
757 if (rc != EOK) {
758 fibril_mutex_unlock(&parentp->idx->lock);
759 return rc;
760 }
761
762 fibril_mutex_unlock(&parentp->idx->lock);
763 fibril_mutex_lock(&childp->idx->lock);
764
765 childp->idx->pfc = parentp->firstc;
766 childp->idx->parent_fragmented = parentp->fragmented;
767 childp->idx->pdi = di.pos;
768 fibril_mutex_unlock(&childp->idx->lock);
769
770 fibril_mutex_lock(&childp->lock);
771 childp->lnkcnt = 1;
772 childp->dirty = true; /* need to sync node */
773 fibril_mutex_unlock(&childp->lock);
774
775 /*
776 * Hash in the index structure into the position hash.
777 */
778 exfat_idx_hashin(childp->idx);
779
780 return EOK;
781
782}
783
784errno_t exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
785{
786 exfat_node_t *parentp = EXFAT_NODE(pfn);
787 exfat_node_t *childp = EXFAT_NODE(cfn);
788 bool has_children;
789 errno_t rc;
790
791 if (!parentp)
792 return EBUSY;
793
794 rc = exfat_has_children(&has_children, cfn);
795 if (rc != EOK)
796 return rc;
797 if (has_children)
798 return ENOTEMPTY;
799
800 fibril_mutex_lock(&parentp->lock);
801 fibril_mutex_lock(&childp->lock);
802 assert(childp->lnkcnt == 1);
803 fibril_mutex_lock(&childp->idx->lock);
804
805 exfat_directory_t di;
806 rc = exfat_directory_open(parentp, &di);
807 if (rc != EOK)
808 goto error;
809 rc = exfat_directory_erase_file(&di, childp->idx->pdi);
810 if (rc != EOK)
811 goto error;
812 rc = exfat_directory_close(&di);
813 if (rc != EOK)
814 goto error;
815
816 /* remove the index structure from the position hash */
817 exfat_idx_hashout(childp->idx);
818 /* clear position information */
819 childp->idx->pfc = 0;
820 childp->idx->pdi = 0;
821 fibril_mutex_unlock(&childp->idx->lock);
822 childp->lnkcnt = 0;
823 childp->refcnt++; /* keep the node in memory until destroyed */
824 childp->dirty = true;
825 fibril_mutex_unlock(&childp->lock);
826 fibril_mutex_unlock(&parentp->lock);
827
828 return EOK;
829
830error:
831 (void) exfat_directory_close(&di);
832 fibril_mutex_unlock(&childp->idx->lock);
833 fibril_mutex_unlock(&childp->lock);
834 fibril_mutex_unlock(&parentp->lock);
835 return rc;
836
837}
838
839errno_t exfat_has_children(bool *has_children, fs_node_t *fn)
840{
841 exfat_directory_t di;
842 exfat_dentry_t *d;
843 exfat_node_t *nodep = EXFAT_NODE(fn);
844 errno_t rc;
845
846 *has_children = false;
847
848 if (nodep->type != EXFAT_DIRECTORY)
849 return EOK;
850
851 fibril_mutex_lock(&nodep->idx->lock);
852
853 rc = exfat_directory_open(nodep, &di);
854 if (rc != EOK) {
855 fibril_mutex_unlock(&nodep->idx->lock);
856 return rc;
857 }
858
859 do {
860 rc = exfat_directory_get(&di, &d);
861 if (rc != EOK) {
862 (void) exfat_directory_close(&di);
863 fibril_mutex_unlock(&nodep->idx->lock);
864 return rc;
865 }
866 switch (exfat_classify_dentry(d)) {
867 case EXFAT_DENTRY_SKIP:
868 case EXFAT_DENTRY_FREE:
869 continue;
870 case EXFAT_DENTRY_LAST:
871 *has_children = false;
872 goto exit;
873 default:
874 *has_children = true;
875 goto exit;
876 }
877 } while (exfat_directory_next(&di) == EOK);
878
879exit:
880 rc = exfat_directory_close(&di);
881 fibril_mutex_unlock(&nodep->idx->lock);
882 return rc;
883}
884
885fs_index_t exfat_index_get(fs_node_t *fn)
886{
887 return EXFAT_NODE(fn)->idx->index;
888}
889
890aoff64_t exfat_size_get(fs_node_t *fn)
891{
892 return EXFAT_NODE(fn)->size;
893}
894
895unsigned exfat_lnkcnt_get(fs_node_t *fn)
896{
897 return EXFAT_NODE(fn)->lnkcnt;
898}
899
900bool exfat_is_directory(fs_node_t *fn)
901{
902 return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
903}
904
905bool exfat_is_file(fs_node_t *fn)
906{
907 return EXFAT_NODE(fn)->type == EXFAT_FILE;
908}
909
910service_id_t exfat_service_get(fs_node_t *node)
911{
912 return 0;
913}
914
915errno_t exfat_size_block(service_id_t service_id, uint32_t *size)
916{
917 exfat_bs_t *bs;
918 bs = block_bb_get(service_id);
919 *size = BPC(bs);
920
921 return EOK;
922}
923
924errno_t exfat_total_block_count(service_id_t service_id, uint64_t *count)
925{
926 exfat_bs_t *bs;
927 bs = block_bb_get(service_id);
928 *count = DATA_CNT(bs);
929
930 return EOK;
931}
932
933errno_t exfat_free_block_count(service_id_t service_id, uint64_t *count)
934{
935 fs_node_t *node = NULL;
936 exfat_node_t *bmap_node;
937 exfat_bs_t *bs;
938 uint64_t free_block_count = 0;
939 uint64_t block_count;
940 unsigned sector;
941 errno_t rc;
942
943 rc = exfat_total_block_count(service_id, &block_count);
944 if (rc != EOK)
945 goto exit;
946
947 bs = block_bb_get(service_id);
948 rc = exfat_bitmap_get(&node, service_id);
949 if (rc != EOK)
950 goto exit;
951
952 bmap_node = (exfat_node_t *) node->data;
953
954 unsigned const bmap_sectors = ROUND_UP(bmap_node->size, BPS(bs)) /
955 BPS(bs);
956
957 for (sector = 0; sector < bmap_sectors; ++sector) {
958
959 block_t *block;
960 uint8_t *bitmap;
961 unsigned bit;
962
963 rc = exfat_block_get(&block, bs, bmap_node, sector,
964 BLOCK_FLAGS_NONE);
965 if (rc != EOK) {
966 free_block_count = 0;
967 goto exit;
968 }
969
970 bitmap = (uint8_t *) block->data;
971
972 for (bit = 0; bit < BPS(bs) * 8 && block_count > 0;
973 ++bit, --block_count) {
974 if (!(bitmap[bit / 8] & (1 << (bit % 8))))
975 ++free_block_count;
976 }
977
978 block_put(block);
979
980 if (block_count == 0) {
981 /* Reached the end of the bitmap */
982 goto exit;
983 }
984 }
985
986exit:
987 exfat_node_put(node);
988 *count = free_block_count;
989 return rc;
990}
991
992/** libfs operations */
993libfs_ops_t exfat_libfs_ops = {
994 .root_get = exfat_root_get,
995 .match = exfat_match,
996 .node_get = exfat_node_get,
997 .node_open = exfat_node_open,
998 .node_put = exfat_node_put,
999 .create = exfat_create_node,
1000 .destroy = exfat_destroy_node,
1001 .link = exfat_link,
1002 .unlink = exfat_unlink,
1003 .has_children = exfat_has_children,
1004 .index_get = exfat_index_get,
1005 .size_get = exfat_size_get,
1006 .lnkcnt_get = exfat_lnkcnt_get,
1007 .is_directory = exfat_is_directory,
1008 .is_file = exfat_is_file,
1009 .service_get = exfat_service_get,
1010 .size_block = exfat_size_block,
1011 .total_block_count = exfat_total_block_count,
1012 .free_block_count = exfat_free_block_count
1013};
1014
1015static errno_t exfat_fs_open(service_id_t service_id, enum cache_mode cmode,
1016 fs_node_t **rrfn, exfat_idx_t **rridxp, vfs_fs_probe_info_t *info)
1017{
1018 errno_t rc;
1019 exfat_node_t *rootp = NULL, *bitmapp = NULL, *uctablep = NULL;
1020 exfat_bs_t *bs;
1021
1022 /* initialize libblock */
1023 rc = block_init(service_id);
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 /* Do some simple sanity checks on the file system. */
1038 rc = exfat_sanity_check(bs);
1039 if (rc != EOK) {
1040 (void) block_cache_fini(service_id);
1041 block_fini(service_id);
1042 return rc;
1043 }
1044
1045 /* Initialize the block cache */
1046 rc = block_cache_init(service_id, BPS(bs), 0 /* XXX */, cmode);
1047 if (rc != EOK) {
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 if (info != NULL) {
1173 /* Read volume label. */
1174 rc = exfat_directory_read_vollabel(&di, info->label,
1175 FS_LABEL_MAXLEN + 1);
1176 if (rc != EOK) {
1177 free(rootp);
1178 free(bitmapp);
1179 free(uctablep);
1180 (void) block_cache_fini(service_id);
1181 block_fini(service_id);
1182 exfat_idx_fini_by_service_id(service_id);
1183 return ENOTSUP;
1184 }
1185 }
1186
1187 rc = exfat_directory_close(&di);
1188 if (rc != EOK) {
1189 free(rootp);
1190 free(bitmapp);
1191 free(uctablep);
1192 (void) block_cache_fini(service_id);
1193 block_fini(service_id);
1194 exfat_idx_fini_by_service_id(service_id);
1195 return ENOMEM;
1196 }
1197
1198 if (0)
1199 exfat_fsinfo(bs, service_id);
1200
1201 *rrfn = FS_NODE(rootp);
1202 *rridxp = rootp->idx;
1203
1204 if (info != NULL) {
1205#if 0
1206 str_cpy(info->label, FS_LABEL_MAXLEN + 1, label);
1207#endif
1208 }
1209
1210 return EOK;
1211}
1212
1213static void exfat_fs_close(service_id_t service_id, fs_node_t *rfn)
1214{
1215 /*
1216 * Put the root node and force it to the FAT free node list.
1217 */
1218 (void) exfat_node_put(rfn);
1219 (void) exfat_node_put(rfn);
1220
1221 /*
1222 * Perform cleanup of the node structures, index structures and
1223 * associated data. Write back this file system's dirty blocks and
1224 * stop using libblock for this instance.
1225 */
1226 (void) exfat_node_fini_by_service_id(service_id);
1227 exfat_idx_fini_by_service_id(service_id);
1228 (void) block_cache_fini(service_id);
1229 block_fini(service_id);
1230}
1231
1232/*
1233 * VFS_OUT operations.
1234 */
1235
1236/* Print debug info */
1237static void exfat_fsinfo(exfat_bs_t *bs, service_id_t service_id)
1238{
1239 printf("exFAT file system mounted\n");
1240 printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
1241 printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
1242 printf("Volume first sector: %" PRId64 "\n", VOL_FS(bs));
1243 printf("Volume sectors: %" PRId64 "\n", VOL_CNT(bs));
1244 printf("FAT first sector: %d\n", FAT_FS(bs));
1245 printf("FAT sectors: %d\n", FAT_CNT(bs));
1246 printf("Data first sector: %d\n", DATA_FS(bs));
1247 printf("Data sectors: %d\n", DATA_CNT(bs));
1248 printf("Root dir first cluster: %d\n", ROOT_FC(bs));
1249 printf("Bytes per sector: %d\n", BPS(bs));
1250 printf("Sectors per cluster: %d\n", SPC(bs));
1251 printf("KBytes per cluster: %d\n", SPC(bs) * BPS(bs) / 1024);
1252
1253 int i, rc;
1254 exfat_cluster_t clst;
1255 for (i = 0; i <= 7; i++) {
1256 rc = exfat_get_cluster(bs, service_id, i, &clst);
1257 if (rc != EOK)
1258 return;
1259 printf("Clst %d: %x", i, clst);
1260 if (i >= 2) {
1261 printf(", Bitmap: %d\n", exfat_bitmap_is_free(bs,
1262 service_id, i) != EOK);
1263 } else {
1264 printf("\n");
1265 }
1266 }
1267}
1268
1269static errno_t exfat_fsprobe(service_id_t service_id, vfs_fs_probe_info_t *info)
1270{
1271 errno_t rc;
1272 exfat_idx_t *ridxp;
1273 fs_node_t *rfn;
1274
1275 rc = exfat_fs_open(service_id, CACHE_MODE_WT, &rfn, &ridxp, info);
1276 if (rc != EOK)
1277 return rc;
1278
1279 exfat_fs_close(service_id, rfn);
1280 return EOK;
1281}
1282
1283static errno_t
1284exfat_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
1285 aoff64_t *size)
1286{
1287 errno_t rc;
1288 enum cache_mode cmode;
1289 exfat_idx_t *ridxp;
1290 fs_node_t *rfn;
1291
1292 /* Check for option enabling write through. */
1293 if (str_cmp(opts, "wtcache") == 0)
1294 cmode = CACHE_MODE_WT;
1295 else
1296 cmode = CACHE_MODE_WB;
1297
1298 rc = exfat_fs_open(service_id, cmode, &rfn, &ridxp, NULL);
1299 if (rc != EOK)
1300 return rc;
1301
1302 *index = ridxp->index;
1303 *size = EXFAT_NODE(rfn)->size;
1304
1305 return EOK;
1306}
1307
1308static errno_t exfat_unmounted(service_id_t service_id)
1309{
1310 fs_node_t *rfn;
1311 errno_t rc;
1312
1313 rc = exfat_root_get(&rfn, service_id);
1314 if (rc != EOK)
1315 return rc;
1316
1317 exfat_fs_close(service_id, rfn);
1318 return EOK;
1319}
1320
1321static errno_t
1322exfat_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
1323 size_t *rbytes)
1324{
1325 fs_node_t *fn;
1326 exfat_node_t *nodep;
1327 exfat_bs_t *bs;
1328 size_t bytes = 0;
1329 block_t *b;
1330 errno_t rc;
1331
1332 rc = exfat_node_get(&fn, service_id, index);
1333 if (rc != EOK)
1334 return rc;
1335 if (!fn)
1336 return ENOENT;
1337 nodep = EXFAT_NODE(fn);
1338
1339 ipc_call_t call;
1340 size_t len;
1341 if (!async_data_read_receive(&call, &len)) {
1342 exfat_node_put(fn);
1343 async_answer_0(&call, EINVAL);
1344 return EINVAL;
1345 }
1346
1347 bs = block_bb_get(service_id);
1348
1349 if (nodep->type == EXFAT_FILE) {
1350 /*
1351 * Our strategy for regular file reads is to read one block at
1352 * most and make use of the possibility to return less data than
1353 * requested. This keeps the code very simple.
1354 */
1355 if (pos >= nodep->size) {
1356 /* reading beyond the EOF */
1357 bytes = 0;
1358 (void) async_data_read_finalize(&call, NULL, 0);
1359 } else {
1360 bytes = min(len, BPS(bs) - pos % BPS(bs));
1361 bytes = min(bytes, nodep->size - pos);
1362 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
1363 BLOCK_FLAGS_NONE);
1364 if (rc != EOK) {
1365 exfat_node_put(fn);
1366 async_answer_0(&call, rc);
1367 return rc;
1368 }
1369 (void) async_data_read_finalize(&call,
1370 b->data + pos % BPS(bs), bytes);
1371 rc = block_put(b);
1372 if (rc != EOK) {
1373 exfat_node_put(fn);
1374 return rc;
1375 }
1376 }
1377 } else {
1378 if (nodep->type != EXFAT_DIRECTORY) {
1379 async_answer_0(&call, ENOTSUP);
1380 return ENOTSUP;
1381 }
1382
1383 aoff64_t spos = pos;
1384 char name[EXFAT_FILENAME_LEN + 1];
1385 exfat_file_dentry_t df;
1386 exfat_stream_dentry_t ds;
1387
1388 assert(nodep->size % BPS(bs) == 0);
1389 assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
1390
1391 exfat_directory_t di;
1392 rc = exfat_directory_open(nodep, &di);
1393 if (rc != EOK)
1394 goto err;
1395
1396 rc = exfat_directory_seek(&di, pos);
1397 if (rc != EOK) {
1398 (void) exfat_directory_close(&di);
1399 goto err;
1400 }
1401
1402 rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
1403 &df, &ds);
1404 if (rc == EOK)
1405 goto hit;
1406 else if (rc == ENOENT)
1407 goto miss;
1408
1409 (void) exfat_directory_close(&di);
1410
1411 err:
1412 (void) exfat_node_put(fn);
1413 async_answer_0(&call, rc);
1414 return rc;
1415
1416 miss:
1417 rc = exfat_directory_close(&di);
1418 if (rc != EOK)
1419 goto err;
1420 rc = exfat_node_put(fn);
1421 async_answer_0(&call, rc != EOK ? rc : ENOENT);
1422 *rbytes = 0;
1423 return rc != EOK ? rc : ENOENT;
1424
1425 hit:
1426 pos = di.pos;
1427 rc = exfat_directory_close(&di);
1428 if (rc != EOK)
1429 goto err;
1430 (void) async_data_read_finalize(&call, name,
1431 str_size(name) + 1);
1432 bytes = (pos - spos) + 1;
1433 }
1434
1435 rc = exfat_node_put(fn);
1436 *rbytes = bytes;
1437 return rc;
1438}
1439
1440static errno_t exfat_close(service_id_t service_id, fs_index_t index)
1441{
1442 return EOK;
1443}
1444
1445static errno_t exfat_sync(service_id_t service_id, fs_index_t index)
1446{
1447 fs_node_t *fn;
1448 errno_t rc = exfat_node_get(&fn, service_id, index);
1449 if (rc != EOK)
1450 return rc;
1451 if (!fn)
1452 return ENOENT;
1453
1454 exfat_node_t *nodep = EXFAT_NODE(fn);
1455
1456 nodep->dirty = true;
1457 rc = exfat_node_sync(nodep);
1458
1459 exfat_node_put(fn);
1460 return rc;
1461}
1462
1463static errno_t
1464exfat_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
1465 size_t *wbytes, aoff64_t *nsize)
1466{
1467 fs_node_t *fn;
1468 exfat_node_t *nodep;
1469 exfat_bs_t *bs;
1470 size_t bytes;
1471 block_t *b;
1472 aoff64_t boundary;
1473 int flags = BLOCK_FLAGS_NONE;
1474 errno_t rc;
1475
1476 rc = exfat_node_get(&fn, service_id, index);
1477 if (rc != EOK)
1478 return rc;
1479 if (!fn)
1480 return ENOENT;
1481 nodep = EXFAT_NODE(fn);
1482
1483 ipc_call_t call;
1484 size_t len;
1485 if (!async_data_write_receive(&call, &len)) {
1486 (void) exfat_node_put(fn);
1487 async_answer_0(&call, EINVAL);
1488 return EINVAL;
1489 }
1490
1491 bs = block_bb_get(service_id);
1492
1493 /*
1494 * In all scenarios, we will attempt to write out only one block worth
1495 * of data at maximum. There might be some more efficient approaches,
1496 * but this one greatly simplifies fat_write(). Note that we can afford
1497 * to do this because the client must be ready to handle the return
1498 * value signalizing a smaller number of bytes written.
1499 */
1500 bytes = min(len, BPS(bs) - pos % BPS(bs));
1501 if (bytes == BPS(bs))
1502 flags |= BLOCK_FLAGS_NOREAD;
1503
1504 boundary = ROUND_UP(nodep->size, BPC(bs));
1505 if (pos >= boundary) {
1506 unsigned nclsts;
1507 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1508 rc = exfat_node_expand(service_id, nodep, nclsts);
1509 if (rc != EOK) {
1510 /* could not expand node */
1511 (void) exfat_node_put(fn);
1512 async_answer_0(&call, rc);
1513 return rc;
1514 }
1515 }
1516
1517 if (pos + bytes > nodep->size) {
1518 nodep->size = pos + bytes;
1519 nodep->dirty = true; /* need to sync node */
1520 }
1521
1522 /*
1523 * This is the easier case - we are either overwriting already
1524 * existing contents or writing behind the EOF, but still within
1525 * the limits of the last cluster. The node size may grow to the
1526 * next block size boundary.
1527 */
1528 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1529 if (rc != EOK) {
1530 (void) exfat_node_put(fn);
1531 async_answer_0(&call, rc);
1532 return rc;
1533 }
1534
1535 (void) async_data_write_finalize(&call,
1536 b->data + pos % BPS(bs), bytes);
1537 b->dirty = true; /* need to sync block */
1538 rc = block_put(b);
1539 if (rc != EOK) {
1540 (void) exfat_node_put(fn);
1541 return rc;
1542 }
1543
1544 *wbytes = bytes;
1545 *nsize = nodep->size;
1546 rc = exfat_node_put(fn);
1547 return rc;
1548}
1549
1550static errno_t
1551exfat_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
1552{
1553 fs_node_t *fn;
1554 exfat_node_t *nodep;
1555 exfat_bs_t *bs;
1556 errno_t rc;
1557
1558 rc = exfat_node_get(&fn, service_id, index);
1559 if (rc != EOK)
1560 return rc;
1561 if (!fn)
1562 return ENOENT;
1563 nodep = EXFAT_NODE(fn);
1564
1565 bs = block_bb_get(service_id);
1566
1567 if (nodep->size == size) {
1568 rc = EOK;
1569 } else if (nodep->size < size) {
1570 /*
1571 * The standard says we have the freedom to grow the node.
1572 * For now, we simply return an error.
1573 */
1574 rc = EINVAL;
1575 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1576 /*
1577 * The node will be shrunk, but no clusters will be deallocated.
1578 */
1579 nodep->size = size;
1580 nodep->dirty = true; /* need to sync node */
1581 rc = EOK;
1582 } else {
1583 rc = exfat_node_shrink(service_id, nodep, size);
1584 }
1585
1586 errno_t rc2 = exfat_node_put(fn);
1587 if (rc == EOK && rc2 != EOK)
1588 rc = rc2;
1589
1590 return rc;
1591}
1592
1593static errno_t exfat_destroy(service_id_t service_id, fs_index_t index)
1594{
1595 fs_node_t *fn;
1596 exfat_node_t *nodep;
1597 errno_t rc;
1598
1599 rc = exfat_node_get(&fn, service_id, index);
1600 if (rc != EOK)
1601 return rc;
1602 if (!fn)
1603 return ENOENT;
1604
1605 nodep = EXFAT_NODE(fn);
1606 /*
1607 * We should have exactly two references. One for the above
1608 * call to fat_node_get() and one from fat_unlink().
1609 */
1610 assert(nodep->refcnt == 2);
1611
1612 rc = exfat_destroy_node(fn);
1613 return rc;
1614}
1615
1616vfs_out_ops_t exfat_ops = {
1617 .fsprobe = exfat_fsprobe,
1618 .mounted = exfat_mounted,
1619 .unmounted = exfat_unmounted,
1620 .read = exfat_read,
1621 .write = exfat_write,
1622 .truncate = exfat_truncate,
1623 .close = exfat_close,
1624 .destroy = exfat_destroy,
1625 .sync = exfat_sync,
1626};
1627
1628/**
1629 * @}
1630 */
Note: See TracBrowser for help on using the repository browser.