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

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

merge the df branch

  • Property mode set to 100644
File size: 36.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 int exfat_size_block(service_id_t, uint32_t *);
92static int exfat_total_block_count(service_id_t, uint64_t *);
93static int 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 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 int 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 int exfat_node_get_new(exfat_node_t **nodepp)
215{
216 fs_node_t *fn;
217 exfat_node_t *nodep;
218 int 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 {
251skip_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 int 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 int 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 int 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
401int exfat_node_expand(service_id_t service_id, exfat_node_t *nodep,
402 exfat_cluster_t clusters)
403{
404 exfat_bs_t *bs;
405 int 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 int exfat_node_shrink(service_id_t service_id, exfat_node_t *nodep,
447 aoff64_t size)
448{
449 exfat_bs_t *bs;
450 int 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
494int 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
499int 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
504int 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
510int 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 int 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 (stricmp(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. */
568int 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 int 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
587int 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
596int exfat_node_put(fs_node_t *fn)
597{
598 exfat_node_t *nodep = EXFAT_NODE(fn);
599 bool destroy = false;
600
601 fibril_mutex_lock(&nodep->lock);
602 if (!--nodep->refcnt) {
603 if (nodep->idx) {
604 fibril_mutex_lock(&ffn_mutex);
605 list_append(&nodep->ffn_link, &ffn_list);
606 fibril_mutex_unlock(&ffn_mutex);
607 } else {
608 /*
609 * The node does not have any index structure associated
610 * with itself. This can only mean that we are releasing
611 * the node after a failed attempt to allocate the index
612 * structure for it.
613 */
614 destroy = true;
615 }
616 }
617 fibril_mutex_unlock(&nodep->lock);
618 if (destroy) {
619 free(nodep->bp);
620 free(nodep);
621 }
622 return EOK;
623}
624
625int exfat_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
626{
627 exfat_idx_t *idxp;
628 exfat_node_t *nodep;
629 exfat_bs_t *bs;
630 int rc;
631
632 bs = block_bb_get(service_id);
633 rc = exfat_node_get_new(&nodep);
634 if (rc != EOK)
635 return rc;
636
637 rc = exfat_idx_get_new(&idxp, service_id);
638 if (rc != EOK) {
639 (void) exfat_node_put(FS_NODE(nodep));
640 return rc;
641 }
642
643 nodep->firstc = 0;
644 nodep->size = 0;
645 nodep->fragmented = false;
646 nodep->lnkcnt = 0; /* not linked anywhere */
647 nodep->refcnt = 1;
648 nodep->dirty = true;
649
650 nodep->idx = idxp;
651 idxp->nodep = nodep;
652 fibril_mutex_unlock(&idxp->lock);
653
654 if (flags & L_DIRECTORY) {
655 nodep->type = EXFAT_DIRECTORY;
656 rc = exfat_node_expand(service_id, nodep, 1);
657 if (rc != EOK) {
658 (void) exfat_node_put(FS_NODE(nodep));
659 return rc;
660 }
661
662 rc = exfat_zero_cluster(bs, service_id, nodep->firstc);
663 if (rc != EOK) {
664 (void) exfat_node_put(FS_NODE(nodep));
665 return rc;
666 }
667
668 nodep->size = BPC(bs);
669 } else {
670 nodep->type = EXFAT_FILE;
671 }
672
673 *rfn = FS_NODE(nodep);
674 return EOK;
675}
676
677int exfat_destroy_node(fs_node_t *fn)
678{
679 exfat_node_t *nodep = EXFAT_NODE(fn);
680 exfat_bs_t *bs;
681 bool has_children;
682 int rc;
683
684 /*
685 * The node is not reachable from the file system. This means that the
686 * link count should be zero and that the index structure cannot be
687 * found in the position hash. Obviously, we don't need to lock the node
688 * nor its index structure.
689 */
690 assert(nodep->lnkcnt == 0);
691
692 /*
693 * The node may not have any children.
694 */
695 rc = exfat_has_children(&has_children, fn);
696 if (rc != EOK)
697 return rc;
698 assert(!has_children);
699
700 bs = block_bb_get(nodep->idx->service_id);
701 if (nodep->firstc != 0) {
702 assert(nodep->size);
703 /* Free all clusters allocated to the node. */
704 if (nodep->fragmented)
705 rc = exfat_free_clusters(bs, nodep->idx->service_id,
706 nodep->firstc);
707 else
708 rc = exfat_bitmap_free_clusters(bs, nodep,
709 ROUND_UP(nodep->size, BPC(bs)) / BPC(bs));
710 }
711
712 exfat_idx_destroy(nodep->idx);
713 free(nodep->bp);
714 free(nodep);
715 return rc;
716}
717
718int exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
719{
720 exfat_node_t *parentp = EXFAT_NODE(pfn);
721 exfat_node_t *childp = EXFAT_NODE(cfn);
722 exfat_directory_t di;
723 int rc;
724
725 fibril_mutex_lock(&childp->lock);
726 if (childp->lnkcnt == 1) {
727 /*
728 * We don't support multiple hard links.
729 */
730 fibril_mutex_unlock(&childp->lock);
731 return EMLINK;
732 }
733 assert(childp->lnkcnt == 0);
734 fibril_mutex_unlock(&childp->lock);
735
736 if (!exfat_valid_name(name))
737 return ENOTSUP;
738
739 fibril_mutex_lock(&parentp->idx->lock);
740 rc = exfat_directory_open(parentp, &di);
741 if (rc != EOK)
742 return rc;
743 /*
744 * At this point we only establish the link between the parent and the
745 * child. The dentry, except of the name and the extension, will remain
746 * uninitialized until the corresponding node is synced. Thus the valid
747 * dentry data is kept in the child node structure.
748 */
749 rc = exfat_directory_write_file(&di, name);
750 if (rc != EOK) {
751 (void) exfat_directory_close(&di);
752 fibril_mutex_unlock(&parentp->idx->lock);
753 return rc;
754 }
755 rc = exfat_directory_close(&di);
756 if (rc != EOK) {
757 fibril_mutex_unlock(&parentp->idx->lock);
758 return rc;
759 }
760
761 fibril_mutex_unlock(&parentp->idx->lock);
762 fibril_mutex_lock(&childp->idx->lock);
763
764 childp->idx->pfc = parentp->firstc;
765 childp->idx->parent_fragmented = parentp->fragmented;
766 childp->idx->pdi = di.pos;
767 fibril_mutex_unlock(&childp->idx->lock);
768
769 fibril_mutex_lock(&childp->lock);
770 childp->lnkcnt = 1;
771 childp->dirty = true; /* need to sync node */
772 fibril_mutex_unlock(&childp->lock);
773
774 /*
775 * Hash in the index structure into the position hash.
776 */
777 exfat_idx_hashin(childp->idx);
778
779 return EOK;
780
781}
782
783int exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
784{
785 exfat_node_t *parentp = EXFAT_NODE(pfn);
786 exfat_node_t *childp = EXFAT_NODE(cfn);
787 bool has_children;
788 int rc;
789
790 if (!parentp)
791 return EBUSY;
792
793 rc = exfat_has_children(&has_children, cfn);
794 if (rc != EOK)
795 return rc;
796 if (has_children)
797 return ENOTEMPTY;
798
799 fibril_mutex_lock(&parentp->lock);
800 fibril_mutex_lock(&childp->lock);
801 assert(childp->lnkcnt == 1);
802 fibril_mutex_lock(&childp->idx->lock);
803
804 exfat_directory_t di;
805 rc = exfat_directory_open(parentp,&di);
806 if (rc != EOK)
807 goto error;
808 rc = exfat_directory_erase_file(&di, childp->idx->pdi);
809 if (rc != EOK)
810 goto error;
811 rc = exfat_directory_close(&di);
812 if (rc != EOK)
813 goto error;
814
815 /* remove the index structure from the position hash */
816 exfat_idx_hashout(childp->idx);
817 /* clear position information */
818 childp->idx->pfc = 0;
819 childp->idx->pdi = 0;
820 fibril_mutex_unlock(&childp->idx->lock);
821 childp->lnkcnt = 0;
822 childp->refcnt++; /* keep the node in memory until destroyed */
823 childp->dirty = true;
824 fibril_mutex_unlock(&childp->lock);
825 fibril_mutex_unlock(&parentp->lock);
826
827 return EOK;
828
829error:
830 (void) exfat_directory_close(&di);
831 fibril_mutex_unlock(&childp->idx->lock);
832 fibril_mutex_unlock(&childp->lock);
833 fibril_mutex_unlock(&parentp->lock);
834 return rc;
835
836}
837
838int exfat_has_children(bool *has_children, fs_node_t *fn)
839{
840 exfat_directory_t di;
841 exfat_dentry_t *d;
842 exfat_node_t *nodep = EXFAT_NODE(fn);
843 int rc;
844
845 *has_children = false;
846
847 if (nodep->type != EXFAT_DIRECTORY)
848 return EOK;
849
850 fibril_mutex_lock(&nodep->idx->lock);
851
852 rc = exfat_directory_open(nodep, &di);
853 if (rc != EOK) {
854 fibril_mutex_unlock(&nodep->idx->lock);
855 return rc;
856 }
857
858 do {
859 rc = exfat_directory_get(&di, &d);
860 if (rc != EOK) {
861 (void) exfat_directory_close(&di);
862 fibril_mutex_unlock(&nodep->idx->lock);
863 return rc;
864 }
865 switch (exfat_classify_dentry(d)) {
866 case EXFAT_DENTRY_SKIP:
867 case EXFAT_DENTRY_FREE:
868 continue;
869 case EXFAT_DENTRY_LAST:
870 *has_children = false;
871 goto exit;
872 default:
873 *has_children = true;
874 goto exit;
875 }
876 } while (exfat_directory_next(&di) == EOK);
877
878exit:
879 rc = exfat_directory_close(&di);
880 fibril_mutex_unlock(&nodep->idx->lock);
881 return rc;
882}
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
915int 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
924int 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
933int exfat_free_block_count(service_id_t service_id, uint64_t *count)
934{
935 fs_node_t *node;
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 int 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 node = NULL;
949 rc = exfat_bitmap_get(&node, service_id);
950 if (rc != EOK)
951 goto exit;
952
953 bmap_node = (exfat_node_t *) node->data;
954
955 unsigned const bmap_sectors = ROUND_UP(bmap_node->size, BPS(bs)) /
956 BPS(bs);
957
958 for (sector = 0; sector < bmap_sectors; ++sector) {
959
960 block_t *block;
961 uint8_t *bitmap;
962 unsigned bit;
963
964 rc = exfat_block_get(&block, bs, bmap_node, sector,
965 BLOCK_FLAGS_NONE);
966 if (rc != EOK) {
967 free_block_count = 0;
968 goto exit;
969 }
970
971 bitmap = (uint8_t *) block->data;
972
973 for (bit = 0; bit < BPS(bs) * 8 && block_count > 0;
974 ++bit, --block_count) {
975 if (!(bitmap[bit / 8] & (1 << (bit % 8))))
976 ++free_block_count;
977 }
978
979 block_put(block);
980
981 if (block_count == 0) {
982 /* Reached the end of the bitmap */
983 goto exit;
984 }
985 }
986
987exit:
988 exfat_node_put(node);
989 *count = free_block_count;
990 return rc;
991}
992
993/** libfs operations */
994libfs_ops_t exfat_libfs_ops = {
995 .root_get = exfat_root_get,
996 .match = exfat_match,
997 .node_get = exfat_node_get,
998 .node_open = exfat_node_open,
999 .node_put = exfat_node_put,
1000 .create = exfat_create_node,
1001 .destroy = exfat_destroy_node,
1002 .link = exfat_link,
1003 .unlink = exfat_unlink,
1004 .has_children = exfat_has_children,
1005 .index_get = exfat_index_get,
1006 .size_get = exfat_size_get,
1007 .lnkcnt_get = exfat_lnkcnt_get,
1008 .is_directory = exfat_is_directory,
1009 .is_file = exfat_is_file,
1010 .service_get = exfat_service_get,
1011 .size_block = exfat_size_block,
1012 .total_block_count = exfat_total_block_count,
1013 .free_block_count = exfat_free_block_count
1014};
1015
1016
1017/*
1018 * VFS_OUT operations.
1019 */
1020
1021/* Print debug info */
1022/*
1023static void exfat_fsinfo(exfat_bs_t *bs, service_id_t service_id)
1024{
1025 printf("exFAT file system mounted\n");
1026 printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
1027 printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
1028 printf("Volume first sector: %lld\n", VOL_FS(bs));
1029 printf("Volume sectors: %lld\n", VOL_CNT(bs));
1030 printf("FAT first sector: %d\n", FAT_FS(bs));
1031 printf("FAT sectors: %d\n", FAT_CNT(bs));
1032 printf("Data first sector: %d\n", DATA_FS(bs));
1033 printf("Data sectors: %d\n", DATA_CNT(bs));
1034 printf("Root dir first cluster: %d\n", ROOT_FC(bs));
1035 printf("Bytes per sector: %d\n", BPS(bs));
1036 printf("Sectors per cluster: %d\n", SPC(bs));
1037 printf("KBytes per cluster: %d\n", SPC(bs)*BPS(bs)/1024);
1038
1039 int i, rc;
1040 exfat_cluster_t clst;
1041 for (i=0; i<=7; i++) {
1042 rc = exfat_get_cluster(bs, service_id, i, &clst);
1043 if (rc != EOK)
1044 return;
1045 printf("Clst %d: %x", i, clst);
1046 if (i>=2)
1047 printf(", Bitmap: %d\n", bitmap_is_free(bs, service_id, i)!=EOK);
1048 else
1049 printf("\n");
1050 }
1051}
1052*/
1053
1054static int
1055exfat_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
1056 aoff64_t *size, unsigned *linkcnt)
1057{
1058 int rc;
1059 exfat_node_t *rootp = NULL, *bitmapp = NULL, *uctablep = NULL;
1060 enum cache_mode cmode;
1061 exfat_bs_t *bs;
1062
1063 /* Check for option enabling write through. */
1064 if (str_cmp(opts, "wtcache") == 0)
1065 cmode = CACHE_MODE_WT;
1066 else
1067 cmode = CACHE_MODE_WB;
1068
1069 /* initialize libblock */
1070 rc = block_init(EXCHANGE_SERIALIZE, service_id, BS_SIZE);
1071 if (rc != EOK)
1072 return rc;
1073
1074 /* prepare the boot block */
1075 rc = block_bb_read(service_id, BS_BLOCK);
1076 if (rc != EOK) {
1077 block_fini(service_id);
1078 return rc;
1079 }
1080
1081 /* get the buffer with the boot sector */
1082 bs = block_bb_get(service_id);
1083
1084 /* Initialize the block cache */
1085 rc = block_cache_init(service_id, BPS(bs), 0 /* XXX */, cmode);
1086 if (rc != EOK) {
1087 block_fini(service_id);
1088 return rc;
1089 }
1090
1091 /* Do some simple sanity checks on the file system. */
1092 rc = exfat_sanity_check(bs, service_id);
1093 if (rc != EOK) {
1094 (void) block_cache_fini(service_id);
1095 block_fini(service_id);
1096 return rc;
1097 }
1098
1099 rc = exfat_idx_init_by_service_id(service_id);
1100 if (rc != EOK) {
1101 (void) block_cache_fini(service_id);
1102 block_fini(service_id);
1103 return rc;
1104 }
1105
1106 /* Initialize the root node. */
1107 rc = exfat_node_get_new_by_pos(&rootp, service_id, EXFAT_ROOT_PAR,
1108 EXFAT_ROOT_POS);
1109 if (rc!=EOK) {
1110 (void) block_cache_fini(service_id);
1111 block_fini(service_id);
1112 exfat_idx_fini_by_service_id(service_id);
1113 return ENOMEM;
1114 }
1115 assert(rootp->idx->index == EXFAT_ROOT_IDX);
1116
1117 rootp->type = EXFAT_DIRECTORY;
1118 rootp->firstc = ROOT_FC(bs);
1119 rootp->fragmented = true;
1120 rootp->refcnt = 1;
1121 rootp->lnkcnt = 0; /* FS root is not linked */
1122
1123 uint32_t clusters;
1124 rc = exfat_clusters_get(&clusters, bs, service_id, rootp->firstc);
1125 if (rc != EOK) {
1126 free(rootp);
1127 (void) block_cache_fini(service_id);
1128 block_fini(service_id);
1129 exfat_idx_fini_by_service_id(service_id);
1130 return ENOTSUP;
1131 }
1132 rootp->size = BPS(bs) * SPC(bs) * clusters;
1133 fibril_mutex_unlock(&rootp->idx->lock);
1134
1135 /* Open root directory and looking for Bitmap and UC-Table */
1136 exfat_directory_t di;
1137 exfat_dentry_t *de;
1138 rc = exfat_directory_open(rootp, &di);
1139 if (rc != EOK) {
1140 free(rootp);
1141 (void) block_cache_fini(service_id);
1142 block_fini(service_id);
1143 exfat_idx_fini_by_service_id(service_id);
1144 return ENOTSUP;
1145 }
1146
1147 /* Initialize the bitmap node. */
1148 rc = exfat_directory_find(&di, EXFAT_DENTRY_BITMAP, &de);
1149 if (rc != EOK) {
1150 free(rootp);
1151 (void) block_cache_fini(service_id);
1152 block_fini(service_id);
1153 exfat_idx_fini_by_service_id(service_id);
1154 return ENOTSUP;
1155 }
1156
1157 rc = exfat_node_get_new_by_pos(&bitmapp, service_id, rootp->firstc,
1158 di.pos);
1159 if (rc != EOK) {
1160 free(rootp);
1161 (void) block_cache_fini(service_id);
1162 block_fini(service_id);
1163 exfat_idx_fini_by_service_id(service_id);
1164 return ENOMEM;
1165 }
1166 assert(bitmapp->idx->index == EXFAT_BITMAP_IDX);
1167 fibril_mutex_unlock(&bitmapp->idx->lock);
1168
1169 bitmapp->type = EXFAT_BITMAP;
1170 bitmapp->firstc = uint32_t_le2host(de->bitmap.firstc);
1171 bitmapp->fragmented = true;
1172 bitmapp->idx->parent_fragmented = true;
1173 bitmapp->refcnt = 1;
1174 bitmapp->lnkcnt = 0;
1175 bitmapp->size = uint64_t_le2host(de->bitmap.size);
1176
1177 /* Initialize the uctable node. */
1178 rc = exfat_directory_seek(&di, 0);
1179 if (rc != EOK) {
1180 free(rootp);
1181 free(bitmapp);
1182 (void) block_cache_fini(service_id);
1183 block_fini(service_id);
1184 exfat_idx_fini_by_service_id(service_id);
1185 return ENOTSUP;
1186 }
1187
1188 rc = exfat_directory_find(&di, EXFAT_DENTRY_UCTABLE, &de);
1189 if (rc != EOK) {
1190 free(rootp);
1191 free(bitmapp);
1192 (void) block_cache_fini(service_id);
1193 block_fini(service_id);
1194 exfat_idx_fini_by_service_id(service_id);
1195 return ENOTSUP;
1196 }
1197
1198 rc = exfat_node_get_new_by_pos(&uctablep, service_id, rootp->firstc,
1199 di.pos);
1200 if (rc != EOK) {
1201 free(rootp);
1202 free(bitmapp);
1203 (void) block_cache_fini(service_id);
1204 block_fini(service_id);
1205 exfat_idx_fini_by_service_id(service_id);
1206 return ENOMEM;
1207 }
1208 assert(uctablep->idx->index == EXFAT_UCTABLE_IDX);
1209 fibril_mutex_unlock(&uctablep->idx->lock);
1210
1211 uctablep->type = EXFAT_UCTABLE;
1212 uctablep->firstc = uint32_t_le2host(de->uctable.firstc);
1213 uctablep->fragmented = true;
1214 uctablep->idx->parent_fragmented = true;
1215 uctablep->refcnt = 1;
1216 uctablep->lnkcnt = 0;
1217 uctablep->size = uint64_t_le2host(de->uctable.size);
1218
1219 rc = exfat_directory_close(&di);
1220 if (rc != EOK) {
1221 free(rootp);
1222 free(bitmapp);
1223 free(uctablep);
1224 (void) block_cache_fini(service_id);
1225 block_fini(service_id);
1226 exfat_idx_fini_by_service_id(service_id);
1227 return ENOMEM;
1228 }
1229
1230 /* exfat_fsinfo(bs, service_id); */
1231
1232 *index = rootp->idx->index;
1233 *size = rootp->size;
1234 *linkcnt = rootp->lnkcnt;
1235
1236 return EOK;
1237}
1238
1239static int exfat_unmounted(service_id_t service_id)
1240{
1241 fs_node_t *fn;
1242 exfat_node_t *nodep;
1243 int rc;
1244
1245 rc = exfat_root_get(&fn, service_id);
1246 if (rc != EOK)
1247 return rc;
1248 nodep = EXFAT_NODE(fn);
1249
1250 /*
1251 * We expect exactly two references on the root node. One for the
1252 * fat_root_get() above and one created in fat_mounted().
1253 */
1254 if (nodep->refcnt != 2) {
1255 (void) exfat_node_put(fn);
1256 return EBUSY;
1257 }
1258
1259 /*
1260 * Put the root node and force it to the FAT free node list.
1261 */
1262 (void) exfat_node_put(fn);
1263 (void) exfat_node_put(fn);
1264
1265 /*
1266 * Perform cleanup of the node structures, index structures and
1267 * associated data. Write back this file system's dirty blocks and
1268 * stop using libblock for this instance.
1269 */
1270 (void) exfat_node_fini_by_service_id(service_id);
1271 exfat_idx_fini_by_service_id(service_id);
1272 (void) block_cache_fini(service_id);
1273 block_fini(service_id);
1274
1275 return EOK;
1276}
1277
1278static int
1279exfat_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
1280 size_t *rbytes)
1281{
1282 fs_node_t *fn;
1283 exfat_node_t *nodep;
1284 exfat_bs_t *bs;
1285 size_t bytes = 0;
1286 block_t *b;
1287 int rc;
1288
1289 rc = exfat_node_get(&fn, service_id, index);
1290 if (rc != EOK)
1291 return rc;
1292 if (!fn)
1293 return ENOENT;
1294 nodep = EXFAT_NODE(fn);
1295
1296 ipc_callid_t callid;
1297 size_t len;
1298 if (!async_data_read_receive(&callid, &len)) {
1299 exfat_node_put(fn);
1300 async_answer_0(callid, EINVAL);
1301 return EINVAL;
1302 }
1303
1304 bs = block_bb_get(service_id);
1305
1306 if (nodep->type == EXFAT_FILE) {
1307 /*
1308 * Our strategy for regular file reads is to read one block at
1309 * most and make use of the possibility to return less data than
1310 * requested. This keeps the code very simple.
1311 */
1312 if (pos >= nodep->size) {
1313 /* reading beyond the EOF */
1314 bytes = 0;
1315 (void) async_data_read_finalize(callid, NULL, 0);
1316 } else {
1317 bytes = min(len, BPS(bs) - pos % BPS(bs));
1318 bytes = min(bytes, nodep->size - pos);
1319 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
1320 BLOCK_FLAGS_NONE);
1321 if (rc != EOK) {
1322 exfat_node_put(fn);
1323 async_answer_0(callid, rc);
1324 return rc;
1325 }
1326 (void) async_data_read_finalize(callid,
1327 b->data + pos % BPS(bs), bytes);
1328 rc = block_put(b);
1329 if (rc != EOK) {
1330 exfat_node_put(fn);
1331 return rc;
1332 }
1333 }
1334 } else {
1335 if (nodep->type != EXFAT_DIRECTORY) {
1336 async_answer_0(callid, ENOTSUP);
1337 return ENOTSUP;
1338 }
1339
1340 aoff64_t spos = pos;
1341 char name[EXFAT_FILENAME_LEN + 1];
1342 exfat_file_dentry_t df;
1343 exfat_stream_dentry_t ds;
1344
1345 assert(nodep->size % BPS(bs) == 0);
1346 assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
1347
1348 exfat_directory_t di;
1349 rc = exfat_directory_open(nodep, &di);
1350 if (rc != EOK)
1351 goto err;
1352
1353 rc = exfat_directory_seek(&di, pos);
1354 if (rc != EOK) {
1355 (void) exfat_directory_close(&di);
1356 goto err;
1357 }
1358
1359 rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
1360 &df, &ds);
1361 if (rc == EOK)
1362 goto hit;
1363 else if (rc == ENOENT)
1364 goto miss;
1365
1366 (void) exfat_directory_close(&di);
1367
1368err:
1369 (void) exfat_node_put(fn);
1370 async_answer_0(callid, rc);
1371 return rc;
1372
1373miss:
1374 rc = exfat_directory_close(&di);
1375 if (rc != EOK)
1376 goto err;
1377 rc = exfat_node_put(fn);
1378 async_answer_0(callid, rc != EOK ? rc : ENOENT);
1379 *rbytes = 0;
1380 return rc != EOK ? rc : ENOENT;
1381
1382hit:
1383 pos = di.pos;
1384 rc = exfat_directory_close(&di);
1385 if (rc != EOK)
1386 goto err;
1387 (void) async_data_read_finalize(callid, name,
1388 str_size(name) + 1);
1389 bytes = (pos - spos) + 1;
1390 }
1391
1392 rc = exfat_node_put(fn);
1393 *rbytes = bytes;
1394 return rc;
1395}
1396
1397static int exfat_close(service_id_t service_id, fs_index_t index)
1398{
1399 return EOK;
1400}
1401
1402static int exfat_sync(service_id_t service_id, fs_index_t index)
1403{
1404 fs_node_t *fn;
1405 int rc = exfat_node_get(&fn, service_id, index);
1406 if (rc != EOK)
1407 return rc;
1408 if (!fn)
1409 return ENOENT;
1410
1411 exfat_node_t *nodep = EXFAT_NODE(fn);
1412
1413 nodep->dirty = true;
1414 rc = exfat_node_sync(nodep);
1415
1416 exfat_node_put(fn);
1417 return rc;
1418}
1419
1420static int
1421exfat_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
1422 size_t *wbytes, aoff64_t *nsize)
1423{
1424 fs_node_t *fn;
1425 exfat_node_t *nodep;
1426 exfat_bs_t *bs;
1427 size_t bytes;
1428 block_t *b;
1429 aoff64_t boundary;
1430 int flags = BLOCK_FLAGS_NONE;
1431 int rc;
1432
1433 rc = exfat_node_get(&fn, service_id, index);
1434 if (rc != EOK)
1435 return rc;
1436 if (!fn)
1437 return ENOENT;
1438 nodep = EXFAT_NODE(fn);
1439
1440 ipc_callid_t callid;
1441 size_t len;
1442 if (!async_data_write_receive(&callid, &len)) {
1443 (void) exfat_node_put(fn);
1444 async_answer_0(callid, EINVAL);
1445 return EINVAL;
1446 }
1447
1448 bs = block_bb_get(service_id);
1449
1450 /*
1451 * In all scenarios, we will attempt to write out only one block worth
1452 * of data at maximum. There might be some more efficient approaches,
1453 * but this one greatly simplifies fat_write(). Note that we can afford
1454 * to do this because the client must be ready to handle the return
1455 * value signalizing a smaller number of bytes written.
1456 */
1457 bytes = min(len, BPS(bs) - pos % BPS(bs));
1458 if (bytes == BPS(bs))
1459 flags |= BLOCK_FLAGS_NOREAD;
1460
1461 boundary = ROUND_UP(nodep->size, BPC(bs));
1462 if (pos >= boundary) {
1463 unsigned nclsts;
1464 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
1465 rc = exfat_node_expand(service_id, nodep, nclsts);
1466 if (rc != EOK) {
1467 /* could not expand node */
1468 (void) exfat_node_put(fn);
1469 async_answer_0(callid, rc);
1470 return rc;
1471 }
1472 }
1473
1474 if (pos + bytes > nodep->size) {
1475 nodep->size = pos + bytes;
1476 nodep->dirty = true; /* need to sync node */
1477 }
1478
1479 /*
1480 * This is the easier case - we are either overwriting already
1481 * existing contents or writing behind the EOF, but still within
1482 * the limits of the last cluster. The node size may grow to the
1483 * next block size boundary.
1484 */
1485 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
1486 if (rc != EOK) {
1487 (void) exfat_node_put(fn);
1488 async_answer_0(callid, rc);
1489 return rc;
1490 }
1491
1492 (void) async_data_write_finalize(callid,
1493 b->data + pos % BPS(bs), bytes);
1494 b->dirty = true; /* need to sync block */
1495 rc = block_put(b);
1496 if (rc != EOK) {
1497 (void) exfat_node_put(fn);
1498 return rc;
1499 }
1500
1501 *wbytes = bytes;
1502 *nsize = nodep->size;
1503 rc = exfat_node_put(fn);
1504 return rc;
1505}
1506
1507static int
1508exfat_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
1509{
1510 fs_node_t *fn;
1511 exfat_node_t *nodep;
1512 exfat_bs_t *bs;
1513 int rc;
1514
1515 rc = exfat_node_get(&fn, service_id, index);
1516 if (rc != EOK)
1517 return rc;
1518 if (!fn)
1519 return ENOENT;
1520 nodep = EXFAT_NODE(fn);
1521
1522 bs = block_bb_get(service_id);
1523
1524 if (nodep->size == size) {
1525 rc = EOK;
1526 } else if (nodep->size < size) {
1527 /*
1528 * The standard says we have the freedom to grow the node.
1529 * For now, we simply return an error.
1530 */
1531 rc = EINVAL;
1532 } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
1533 /*
1534 * The node will be shrunk, but no clusters will be deallocated.
1535 */
1536 nodep->size = size;
1537 nodep->dirty = true; /* need to sync node */
1538 rc = EOK;
1539 } else {
1540 rc = exfat_node_shrink(service_id, nodep, size);
1541 }
1542
1543 int rc2 = exfat_node_put(fn);
1544 if (rc == EOK && rc2 != EOK)
1545 rc = rc2;
1546
1547 return rc;
1548}
1549
1550static int exfat_destroy(service_id_t service_id, fs_index_t index)
1551{
1552 fs_node_t *fn;
1553 exfat_node_t *nodep;
1554 int rc;
1555
1556 rc = exfat_node_get(&fn, service_id, index);
1557 if (rc != EOK)
1558 return rc;
1559 if (!fn)
1560 return ENOENT;
1561
1562 nodep = EXFAT_NODE(fn);
1563 /*
1564 * We should have exactly two references. One for the above
1565 * call to fat_node_get() and one from fat_unlink().
1566 */
1567 assert(nodep->refcnt == 2);
1568
1569 rc = exfat_destroy_node(fn);
1570 return rc;
1571}
1572
1573vfs_out_ops_t exfat_ops = {
1574 .mounted = exfat_mounted,
1575 .unmounted = exfat_unmounted,
1576 .read = exfat_read,
1577 .write = exfat_write,
1578 .truncate = exfat_truncate,
1579 .close = exfat_close,
1580 .destroy = exfat_destroy,
1581 .sync = exfat_sync,
1582};
1583
1584/**
1585 * @}
1586 */
Note: See TracBrowser for help on using the repository browser.