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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 375ab5e was 375ab5e, checked in by Jakub Jermar <jakub@…>, 14 years ago

Merge from lp:~romanenko-oleg/helenos/fat.

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