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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25c60f4 was 5d5863c, checked in by Oleg Romanenko <romanenko.oleg@…>, 14 years ago

exFAT: move bitmap_* function to separate file exfat_bitmap.c

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