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

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

rename device_get() method to service_get() to better reflect changes in the location service

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