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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc22069 was fc22069, checked in by Martin Decky <martin@…>, 10 years ago

block devices use the same interface, therefore the API of libblock should not expose the implementation details

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