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

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

exfat: add the free_block_count() callback implementation.

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