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

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

Update exfat_ops.c to support new VFS_OUT interface

  • Property mode set to 100644
File size: 25.3 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 "../../vfs/vfs.h"
44#include <libfs.h>
45#include <libblock.h>
46#include <ipc/services.h>
47#include <ipc/devmap.h>
48#include <macros.h>
49#include <async.h>
50#include <errno.h>
51#include <str.h>
52#include <byteorder.h>
53#include <adt/hash_table.h>
54#include <adt/list.h>
55#include <assert.h>
56#include <fibril_synch.h>
57#include <sys/mman.h>
58#include <align.h>
59#include <malloc.h>
60#include <stdio.h>
61
62#define EXFAT_NODE(node) ((node) ? (exfat_node_t *) (node)->data : NULL)
63#define FS_NODE(node) ((node) ? (node)->bp : NULL)
64#define DPS(bs) (BPS((bs)) / sizeof(exfat_dentry_t))
65
66/** Mutex protecting the list of cached free FAT nodes. */
67static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
68
69/** List of cached free FAT nodes. */
70static LIST_INITIALIZE(ffn_list);
71
72/*
73 * Forward declarations of FAT libfs operations.
74 */
75/*
76static int exfat_bitmap_get(fs_node_t **, devmap_handle_t);
77static int exfat_uctable_get(fs_node_t **, devmap_handle_t);
78*/
79static int exfat_root_get(fs_node_t **, devmap_handle_t);
80static int exfat_match(fs_node_t **, fs_node_t *, const char *);
81static int exfat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
82static int exfat_node_open(fs_node_t *);
83static int exfat_node_put(fs_node_t *);
84static int exfat_create_node(fs_node_t **, devmap_handle_t, int);
85static int exfat_destroy_node(fs_node_t *);
86static int exfat_link(fs_node_t *, fs_node_t *, const char *);
87static int exfat_unlink(fs_node_t *, fs_node_t *, const char *);
88static int exfat_has_children(bool *, fs_node_t *);
89static fs_index_t exfat_index_get(fs_node_t *);
90static aoff64_t exfat_size_get(fs_node_t *);
91static unsigned exfat_lnkcnt_get(fs_node_t *);
92static bool exfat_is_directory(fs_node_t *);
93static bool exfat_is_file(fs_node_t *node);
94static devmap_handle_t exfat_device_get(fs_node_t *node);
95
96/*
97 * Helper functions.
98 */
99static void exfat_node_initialize(exfat_node_t *node)
100{
101 fibril_mutex_initialize(&node->lock);
102 node->bp = NULL;
103 node->idx = NULL;
104 node->type = EXFAT_UNKNOW;
105 link_initialize(&node->ffn_link);
106 node->size = 0;
107 node->lnkcnt = 0;
108 node->refcnt = 0;
109 node->dirty = false;
110 node->fragmented = true;
111 node->lastc_cached_valid = false;
112 node->lastc_cached_value = 0;
113 node->currc_cached_valid = false;
114 node->currc_cached_bn = 0;
115 node->currc_cached_value = 0;
116}
117
118static int exfat_node_sync(exfat_node_t *node)
119{
120 return EOK;
121}
122
123static int exfat_node_fini_by_devmap_handle(devmap_handle_t devmap_handle)
124{
125 exfat_node_t *nodep;
126 int rc;
127
128 /*
129 * We are called from fat_unmounted() and assume that there are already
130 * no nodes belonging to this instance with non-zero refcount. Therefore
131 * it is sufficient to clean up only the FAT free node list.
132 */
133
134restart:
135 fibril_mutex_lock(&ffn_mutex);
136 list_foreach(ffn_list, lnk) {
137 nodep = list_get_instance(lnk, exfat_node_t, ffn_link);
138 if (!fibril_mutex_trylock(&nodep->lock)) {
139 fibril_mutex_unlock(&ffn_mutex);
140 goto restart;
141 }
142 if (!fibril_mutex_trylock(&nodep->idx->lock)) {
143 fibril_mutex_unlock(&nodep->lock);
144 fibril_mutex_unlock(&ffn_mutex);
145 goto restart;
146 }
147 if (nodep->idx->devmap_handle != devmap_handle) {
148 fibril_mutex_unlock(&nodep->idx->lock);
149 fibril_mutex_unlock(&nodep->lock);
150 continue;
151 }
152
153 list_remove(&nodep->ffn_link);
154 fibril_mutex_unlock(&ffn_mutex);
155
156 /*
157 * We can unlock the node and its index structure because we are
158 * the last player on this playground and VFS is preventing new
159 * players from entering.
160 */
161 fibril_mutex_unlock(&nodep->idx->lock);
162 fibril_mutex_unlock(&nodep->lock);
163
164 if (nodep->dirty) {
165 rc = exfat_node_sync(nodep);
166 if (rc != EOK)
167 return rc;
168 }
169 nodep->idx->nodep = NULL;
170 free(nodep->bp);
171 free(nodep);
172
173 /* Need to restart because we changed the ffn_list. */
174 goto restart;
175 }
176 fibril_mutex_unlock(&ffn_mutex);
177
178 return EOK;
179}
180
181static int exfat_node_get_new(exfat_node_t **nodepp)
182{
183 fs_node_t *fn;
184 exfat_node_t *nodep;
185 int rc;
186
187 fibril_mutex_lock(&ffn_mutex);
188 if (!list_empty(&ffn_list)) {
189 /* Try to use a cached free node structure. */
190 exfat_idx_t *idxp_tmp;
191 nodep = list_get_instance(list_first(&ffn_list), exfat_node_t,
192 ffn_link);
193 if (!fibril_mutex_trylock(&nodep->lock))
194 goto skip_cache;
195 idxp_tmp = nodep->idx;
196 if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
197 fibril_mutex_unlock(&nodep->lock);
198 goto skip_cache;
199 }
200 list_remove(&nodep->ffn_link);
201 fibril_mutex_unlock(&ffn_mutex);
202 if (nodep->dirty) {
203 rc = exfat_node_sync(nodep);
204 if (rc != EOK) {
205 idxp_tmp->nodep = NULL;
206 fibril_mutex_unlock(&nodep->lock);
207 fibril_mutex_unlock(&idxp_tmp->lock);
208 free(nodep->bp);
209 free(nodep);
210 return rc;
211 }
212 }
213 idxp_tmp->nodep = NULL;
214 fibril_mutex_unlock(&nodep->lock);
215 fibril_mutex_unlock(&idxp_tmp->lock);
216 fn = FS_NODE(nodep);
217 } else {
218skip_cache:
219 /* Try to allocate a new node structure. */
220 fibril_mutex_unlock(&ffn_mutex);
221 fn = (fs_node_t *)malloc(sizeof(fs_node_t));
222 if (!fn)
223 return ENOMEM;
224 nodep = (exfat_node_t *)malloc(sizeof(exfat_node_t));
225 if (!nodep) {
226 free(fn);
227 return ENOMEM;
228 }
229 }
230 exfat_node_initialize(nodep);
231 fs_node_initialize(fn);
232 fn->data = nodep;
233 nodep->bp = fn;
234
235 *nodepp = nodep;
236 return EOK;
237}
238
239static int exfat_node_get_new_by_pos(exfat_node_t **nodepp,
240 devmap_handle_t devmap_handle, exfat_cluster_t pfc, unsigned pdi)
241{
242 exfat_idx_t *idxp = exfat_idx_get_by_pos(devmap_handle, pfc, pdi);
243 if (!idxp)
244 return ENOMEM;
245 if (exfat_node_get_new(nodepp) != EOK)
246 return ENOMEM;
247 (*nodepp)->idx = idxp;
248 idxp->nodep = *nodepp;
249 return EOK;
250}
251
252
253/** Internal version of exfat_node_get().
254 *
255 * @param idxp Locked index structure.
256 */
257static int exfat_node_get_core(exfat_node_t **nodepp, exfat_idx_t *idxp)
258{
259 block_t *b=NULL;
260 exfat_bs_t *bs;
261 exfat_dentry_t *d;
262 exfat_node_t *nodep = NULL;
263 int rc;
264
265 if (idxp->nodep) {
266 /*
267 * We are lucky.
268 * The node is already instantiated in memory.
269 */
270 fibril_mutex_lock(&idxp->nodep->lock);
271 if (!idxp->nodep->refcnt++) {
272 fibril_mutex_lock(&ffn_mutex);
273 list_remove(&idxp->nodep->ffn_link);
274 fibril_mutex_unlock(&ffn_mutex);
275 }
276 fibril_mutex_unlock(&idxp->nodep->lock);
277 *nodepp = idxp->nodep;
278 return EOK;
279 }
280
281 /*
282 * We must instantiate the node from the file system.
283 */
284
285 assert(idxp->pfc);
286
287 rc = exfat_node_get_new(&nodep);
288 if (rc != EOK)
289 return rc;
290
291 bs = block_bb_get(idxp->devmap_handle);
292
293 rc = exfat_block_get_by_clst(&b, bs, idxp->devmap_handle,
294 idxp->parent_fragmented, idxp->pfc, NULL,
295 (idxp->pdi * sizeof(exfat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
296 if (rc != EOK) {
297 (void) exfat_node_put(FS_NODE(nodep));
298 return rc;
299 }
300
301 d = ((exfat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
302 switch (exfat_classify_dentry(d)) {
303 case EXFAT_DENTRY_FILE:
304 nodep->type = (d->file.attr & EXFAT_ATTR_SUBDIR)?
305 EXFAT_DIRECTORY : EXFAT_FILE;
306 rc = block_put(b);
307 if (rc != EOK) {
308 (void) exfat_node_put(FS_NODE(nodep));
309 return rc;
310 }
311 rc = exfat_block_get_by_clst(&b, bs, idxp->devmap_handle,
312 idxp->parent_fragmented, idxp->pfc, NULL,
313 ((idxp->pdi+1) * sizeof(exfat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
314 if (rc != EOK) {
315 (void) exfat_node_put(FS_NODE(nodep));
316 return rc;
317 }
318 d = ((exfat_dentry_t *)b->data) + ((idxp->pdi+1) % DPS(bs));
319
320 nodep->firstc = d->stream.firstc;
321 nodep->size = d->stream.data_size;
322 nodep->fragmented = (d->stream.flags & 0x02) == 0;
323 break;
324 case EXFAT_DENTRY_BITMAP:
325 nodep->type = EXFAT_BITMAP;
326 nodep->firstc = d->bitmap.firstc;
327 nodep->size = d->bitmap.size;
328 nodep->fragmented = true;
329 break;
330 case EXFAT_DENTRY_UCTABLE:
331 nodep->type = EXFAT_UCTABLE;
332 nodep->firstc = d->uctable.firstc;
333 nodep->size = d->uctable.size;
334 nodep->fragmented = true;
335 break;
336 default:
337 case EXFAT_DENTRY_SKIP:
338 case EXFAT_DENTRY_LAST:
339 case EXFAT_DENTRY_FREE:
340 case EXFAT_DENTRY_VOLLABEL:
341 case EXFAT_DENTRY_GUID:
342 case EXFAT_DENTRY_STREAM:
343 case EXFAT_DENTRY_NAME:
344 (void) block_put(b);
345 (void) exfat_node_put(FS_NODE(nodep));
346 return ENOENT;
347 }
348
349 nodep->lnkcnt = 1;
350 nodep->refcnt = 1;
351
352 rc = block_put(b);
353 if (rc != EOK) {
354 (void) exfat_node_put(FS_NODE(nodep));
355 return rc;
356 }
357
358 /* Link the idx structure with the node structure. */
359 nodep->idx = idxp;
360 idxp->nodep = nodep;
361
362 *nodepp = nodep;
363 return EOK;
364}
365
366
367
368/*
369 * EXFAT libfs operations.
370 */
371
372int exfat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
373{
374 return exfat_node_get(rfn, devmap_handle, EXFAT_ROOT_IDX);
375}
376
377/*
378int exfat_bitmap_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
379{
380 return exfat_node_get(rfn, devmap_handle, EXFAT_BITMAP_IDX);
381}
382*/
383/*
384int exfat_uctable_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
385{
386 return exfat_node_get(rfn, devmap_handle, EXFAT_UCTABLE_IDX);
387}
388*/
389
390int exfat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
391{
392 exfat_node_t *parentp = EXFAT_NODE(pfn);
393 char name[EXFAT_FILENAME_LEN+1];
394 exfat_file_dentry_t df;
395 exfat_stream_dentry_t ds;
396 devmap_handle_t devmap_handle;
397 int rc;
398
399 fibril_mutex_lock(&parentp->idx->lock);
400 devmap_handle = parentp->idx->devmap_handle;
401 fibril_mutex_unlock(&parentp->idx->lock);
402
403 exfat_directory_t di;
404 rc = exfat_directory_open(parentp, &di);
405 if (rc != EOK)
406 return rc;
407
408 while (exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN,
409 &df, &ds) == EOK) {
410 if (stricmp(name, component) == 0) {
411 /* hit */
412 exfat_node_t *nodep;
413 aoff64_t o = di.pos % (BPS(di.bs) / sizeof(exfat_dentry_t));
414 exfat_idx_t *idx = exfat_idx_get_by_pos(devmap_handle,
415 parentp->firstc, di.bnum * DPS(di.bs) + o);
416 if (!idx) {
417 /*
418 * Can happen if memory is low or if we
419 * run out of 32-bit indices.
420 */
421 rc = exfat_directory_close(&di);
422 return (rc == EOK) ? ENOMEM : rc;
423 }
424 rc = exfat_node_get_core(&nodep, idx);
425 fibril_mutex_unlock(&idx->lock);
426 if (rc != EOK) {
427 (void) exfat_directory_close(&di);
428 return rc;
429 }
430 *rfn = FS_NODE(nodep);
431 rc = exfat_directory_close(&di);
432 if (rc != EOK)
433 (void) exfat_node_put(*rfn);
434 return rc;
435 } else {
436 rc = exfat_directory_next(&di);
437 if (rc != EOK)
438 break;
439 }
440 }
441 (void) exfat_directory_close(&di);
442 *rfn = NULL;
443 return EOK;
444}
445
446/** Instantiate a exFAT in-core node. */
447int exfat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
448{
449 exfat_node_t *nodep;
450 exfat_idx_t *idxp;
451 int rc;
452
453 idxp = exfat_idx_get_by_index(devmap_handle, index);
454 if (!idxp) {
455 *rfn = NULL;
456 return EOK;
457 }
458 /* idxp->lock held */
459 rc = exfat_node_get_core(&nodep, idxp);
460 fibril_mutex_unlock(&idxp->lock);
461 if (rc == EOK)
462 *rfn = FS_NODE(nodep);
463 return rc;
464}
465
466int exfat_node_open(fs_node_t *fn)
467{
468 /*
469 * Opening a file is stateless, nothing
470 * to be done here.
471 */
472 return EOK;
473}
474
475int exfat_node_put(fs_node_t *fn)
476{
477 exfat_node_t *nodep = EXFAT_NODE(fn);
478 bool destroy = false;
479
480 fibril_mutex_lock(&nodep->lock);
481 if (!--nodep->refcnt) {
482 if (nodep->idx) {
483 fibril_mutex_lock(&ffn_mutex);
484 list_append(&nodep->ffn_link, &ffn_list);
485 fibril_mutex_unlock(&ffn_mutex);
486 } else {
487 /*
488 * The node does not have any index structure associated
489 * with itself. This can only mean that we are releasing
490 * the node after a failed attempt to allocate the index
491 * structure for it.
492 */
493 destroy = true;
494 }
495 }
496 fibril_mutex_unlock(&nodep->lock);
497 if (destroy) {
498 free(nodep->bp);
499 free(nodep);
500 }
501 return EOK;
502}
503
504int exfat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
505{
506 /* TODO */
507 *rfn = NULL;
508 return EOK;
509}
510
511int exfat_destroy_node(fs_node_t *fn)
512{
513 /* TODO */
514 return EOK;
515}
516
517int exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
518{
519 /* TODO */
520 return EOK;
521}
522
523int exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
524{
525 /* TODO */
526 return EOK;
527}
528
529int exfat_has_children(bool *has_children, fs_node_t *fn)
530{
531 exfat_directory_t di;
532 exfat_dentry_t *d;
533 exfat_node_t *nodep = EXFAT_NODE(fn);
534 int rc;
535
536 *has_children = false;
537
538 if (nodep->type != EXFAT_DIRECTORY)
539 return EOK;
540
541 fibril_mutex_lock(&nodep->idx->lock);
542
543 rc = exfat_directory_open(nodep, &di);
544 if (rc != EOK) {
545 fibril_mutex_unlock(&nodep->idx->lock);
546 return rc;
547 }
548
549 do {
550 rc = exfat_directory_get(&di, &d);
551 if (rc != EOK) {
552 (void) exfat_directory_close(&di);
553 fibril_mutex_unlock(&nodep->idx->lock);
554 return rc;
555 }
556 switch (exfat_classify_dentry(d)) {
557 case EXFAT_DENTRY_SKIP:
558 case EXFAT_DENTRY_FREE:
559 continue;
560 case EXFAT_DENTRY_LAST:
561 *has_children = false;
562 goto exit;
563 default:
564 *has_children = true;
565 goto exit;
566 }
567 } while (exfat_directory_next(&di) == EOK);
568
569exit:
570 rc = exfat_directory_close(&di);
571 fibril_mutex_unlock(&nodep->idx->lock);
572 return rc;
573}
574
575
576fs_index_t exfat_index_get(fs_node_t *fn)
577{
578 return EXFAT_NODE(fn)->idx->index;
579}
580
581aoff64_t exfat_size_get(fs_node_t *fn)
582{
583 return EXFAT_NODE(fn)->size;
584}
585
586unsigned exfat_lnkcnt_get(fs_node_t *fn)
587{
588 return EXFAT_NODE(fn)->lnkcnt;
589}
590
591bool exfat_is_directory(fs_node_t *fn)
592{
593 return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
594}
595
596bool exfat_is_file(fs_node_t *fn)
597{
598 return EXFAT_NODE(fn)->type == EXFAT_FILE;
599}
600
601devmap_handle_t exfat_device_get(fs_node_t *node)
602{
603 return 0;
604}
605
606
607/** libfs operations */
608libfs_ops_t exfat_libfs_ops = {
609 .root_get = exfat_root_get,
610 .match = exfat_match,
611 .node_get = exfat_node_get,
612 .node_open = exfat_node_open,
613 .node_put = exfat_node_put,
614 .create = exfat_create_node,
615 .destroy = exfat_destroy_node,
616 .link = exfat_link,
617 .unlink = exfat_unlink,
618 .has_children = exfat_has_children,
619 .index_get = exfat_index_get,
620 .size_get = exfat_size_get,
621 .lnkcnt_get = exfat_lnkcnt_get,
622 .is_directory = exfat_is_directory,
623 .is_file = exfat_is_file,
624 .device_get = exfat_device_get
625};
626
627
628/*
629 * VFS_OUT operations.
630 */
631
632/* Print debug info */
633static void exfat_fsinfo(exfat_bs_t *bs, devmap_handle_t devmap_handle)
634{
635 printf("exFAT file system mounted\n");
636 printf("Version: %d.%d\n", bs->version.major, bs->version.minor);
637 printf("Volume serial: %d\n", uint32_t_le2host(bs->volume_serial));
638 printf("Volume first sector: %lld\n", VOL_FS(bs));
639 printf("Volume sectors: %lld\n", VOL_CNT(bs));
640 printf("FAT first sector: %d\n", FAT_FS(bs));
641 printf("FAT sectors: %d\n", FAT_CNT(bs));
642 printf("Data first sector: %d\n", DATA_FS(bs));
643 printf("Data sectors: %d\n", DATA_CNT(bs));
644 printf("Root dir first cluster: %d\n", ROOT_FC(bs));
645 printf("Bytes per sector: %d\n", BPS(bs));
646 printf("Sectors per cluster: %d\n", SPC(bs));
647 printf("KBytes per cluster: %d\n", SPC(bs)*BPS(bs)/1024);
648
649 int i, rc;
650 exfat_cluster_t clst;
651 for (i=0; i<6; i++) {
652 rc = fat_get_cluster(bs, devmap_handle, i, &clst);
653 if (rc != EOK)
654 return;
655 printf("Clst %d: %x\n", i, clst);
656 }
657}
658
659static int
660exfat_mounted(devmap_handle_t devmap_handle, const char *opts, fs_index_t *index,
661 aoff64_t *size, unsigned *linkcnt)
662{
663 int rc;
664 exfat_node_t *rootp=NULL, *bitmapp=NULL, *uctablep=NULL;
665 enum cache_mode cmode;
666 exfat_bs_t *bs;
667
668 /* Check for option enabling write through. */
669 if (str_cmp(opts, "wtcache") == 0)
670 cmode = CACHE_MODE_WT;
671 else
672 cmode = CACHE_MODE_WB;
673
674 /* initialize libblock */
675 rc = block_init(EXCHANGE_SERIALIZE, devmap_handle, BS_SIZE);
676 if (rc != EOK)
677 return rc;
678
679 /* prepare the boot block */
680 rc = block_bb_read(devmap_handle, BS_BLOCK);
681 if (rc != EOK) {
682 block_fini(devmap_handle);
683 return rc;
684 }
685
686 /* get the buffer with the boot sector */
687 bs = block_bb_get(devmap_handle);
688
689 /* Initialize the block cache */
690 rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
691 if (rc != EOK) {
692 block_fini(devmap_handle);
693 return rc;
694 }
695
696 /* Do some simple sanity checks on the file system. */
697 rc = exfat_sanity_check(bs, devmap_handle);
698 if (rc != EOK) {
699 (void) block_cache_fini(devmap_handle);
700 block_fini(devmap_handle);
701 return rc;
702 }
703
704 rc = exfat_idx_init_by_devmap_handle(devmap_handle);
705 if (rc != EOK) {
706 (void) block_cache_fini(devmap_handle);
707 block_fini(devmap_handle);
708 return rc;
709 }
710
711 /* Initialize the root node. */
712 rc = exfat_node_get_new_by_pos(&rootp, devmap_handle, EXFAT_ROOT_PAR,
713 EXFAT_ROOT_POS);
714 if (rc!=EOK) {
715 (void) block_cache_fini(devmap_handle);
716 block_fini(devmap_handle);
717 exfat_idx_fini_by_devmap_handle(devmap_handle);
718 return ENOMEM;
719 }
720 assert(rootp->idx->index == EXFAT_ROOT_IDX);
721
722 rootp->type = EXFAT_DIRECTORY;
723 rootp->firstc = ROOT_FC(bs);
724 rootp->fragmented = true;
725 rootp->refcnt = 1;
726 rootp->lnkcnt = 0; /* FS root is not linked */
727
728 uint32_t clusters;
729 rc = fat_clusters_get(&clusters, bs, devmap_handle, rootp->firstc);
730 if (rc != EOK) {
731 free(rootp);
732 (void) block_cache_fini(devmap_handle);
733 block_fini(devmap_handle);
734 exfat_idx_fini_by_devmap_handle(devmap_handle);
735 return ENOTSUP;
736 }
737 rootp->size = BPS(bs) * SPC(bs) * clusters;
738 fibril_mutex_unlock(&rootp->idx->lock);
739
740 /* Open root directory and looking for Bitmap and UC-Table */
741 exfat_directory_t di;
742 exfat_dentry_t *de;
743 rc = exfat_directory_open(rootp, &di);
744 if (rc != EOK) {
745 free(rootp);
746 (void) block_cache_fini(devmap_handle);
747 block_fini(devmap_handle);
748 exfat_idx_fini_by_devmap_handle(devmap_handle);
749 return ENOTSUP;
750 }
751
752 /* Initialize the bitmap node. */
753 rc = exfat_directory_find(&di, EXFAT_DENTRY_BITMAP, &de);
754 if (rc != EOK) {
755 free(rootp);
756 (void) block_cache_fini(devmap_handle);
757 block_fini(devmap_handle);
758 exfat_idx_fini_by_devmap_handle(devmap_handle);
759 return ENOTSUP;
760 }
761
762 rc = exfat_node_get_new_by_pos(&bitmapp, devmap_handle, rootp->firstc,
763 di.pos);
764 if (rc!=EOK) {
765 free(rootp);
766 (void) block_cache_fini(devmap_handle);
767 block_fini(devmap_handle);
768 exfat_idx_fini_by_devmap_handle(devmap_handle);
769 return ENOMEM;
770 }
771 assert(bitmapp->idx->index == EXFAT_BITMAP_IDX);
772 fibril_mutex_unlock(&bitmapp->idx->lock);
773
774 bitmapp->type = EXFAT_BITMAP;
775 bitmapp->firstc = de->bitmap.firstc;
776 bitmapp->fragmented = true;
777 bitmapp->idx->parent_fragmented = true;
778 bitmapp->refcnt = 1;
779 bitmapp->lnkcnt = 0;
780 bitmapp->size = de->bitmap.size;
781
782 /* Initialize the uctable node. */
783 rc = exfat_directory_seek(&di, 0);
784 if (rc != EOK) {
785 free(rootp);
786 free(bitmapp);
787 (void) block_cache_fini(devmap_handle);
788 block_fini(devmap_handle);
789 exfat_idx_fini_by_devmap_handle(devmap_handle);
790 return ENOTSUP;
791 }
792
793 rc = exfat_directory_find(&di, EXFAT_DENTRY_UCTABLE, &de);
794 if (rc != EOK) {
795 free(rootp);
796 free(bitmapp);
797 (void) block_cache_fini(devmap_handle);
798 block_fini(devmap_handle);
799 exfat_idx_fini_by_devmap_handle(devmap_handle);
800 return ENOTSUP;
801 }
802
803 rc = exfat_node_get_new_by_pos(&uctablep, devmap_handle, rootp->firstc,
804 di.pos);
805 if (rc!=EOK) {
806 free(rootp);
807 free(bitmapp);
808 (void) block_cache_fini(devmap_handle);
809 block_fini(devmap_handle);
810 exfat_idx_fini_by_devmap_handle(devmap_handle);
811 return ENOMEM;
812 }
813 assert(uctablep->idx->index == EXFAT_UCTABLE_IDX);
814 fibril_mutex_unlock(&uctablep->idx->lock);
815
816 uctablep->type = EXFAT_UCTABLE;
817 uctablep->firstc = de->uctable.firstc;
818 uctablep->fragmented = true;
819 uctablep->idx->parent_fragmented = true;
820 uctablep->refcnt = 1;
821 uctablep->lnkcnt = 0;
822 uctablep->size = de->uctable.size;
823
824 rc = exfat_directory_close(&di);
825 if (rc!=EOK) {
826 free(rootp);
827 free(bitmapp);
828 free(uctablep);
829 (void) block_cache_fini(devmap_handle);
830 block_fini(devmap_handle);
831 exfat_idx_fini_by_devmap_handle(devmap_handle);
832 return ENOMEM;
833 }
834
835 exfat_fsinfo(bs, devmap_handle);
836 printf("Root dir size: %lld\n", rootp->size);
837
838 *index = rootp->idx->index;
839 *size = rootp->size;
840 *linkcnt = rootp->lnkcnt;
841
842 return EOK;
843}
844
845static int exfat_unmounted(devmap_handle_t devmap_handle)
846{
847 fs_node_t *fn;
848 exfat_node_t *nodep;
849 int rc;
850
851 rc = exfat_root_get(&fn, devmap_handle);
852 if (rc != EOK)
853 return rc;
854 nodep = EXFAT_NODE(fn);
855
856 /*
857 * We expect exactly two references on the root node. One for the
858 * fat_root_get() above and one created in fat_mounted().
859 */
860 if (nodep->refcnt != 2) {
861 (void) exfat_node_put(fn);
862 return EBUSY;
863 }
864
865 /*
866 * Put the root node and force it to the FAT free node list.
867 */
868 (void) exfat_node_put(fn);
869 (void) exfat_node_put(fn);
870
871 /*
872 * Perform cleanup of the node structures, index structures and
873 * associated data. Write back this file system's dirty blocks and
874 * stop using libblock for this instance.
875 */
876 (void) exfat_node_fini_by_devmap_handle(devmap_handle);
877 exfat_idx_fini_by_devmap_handle(devmap_handle);
878 (void) block_cache_fini(devmap_handle);
879 block_fini(devmap_handle);
880
881 return EOK;
882}
883
884/*
885int bitmap_is_allocated(exfat_bs_t *bs, devmap_handle_t devmap_handle,
886 exfat_cluster_t clst, bool *status)
887{
888 fs_node_t *fn;
889 exfat_node_t *bitmap;
890 int rc;
891
892 rc = exfat_bitmap_get(&fn, devmap_handle);
893 if (rc != EOK)
894 return rc;
895
896 nbitmap = EXFAT_NODE(fn);
897
898
899 return EOK;
900}
901*/
902
903static int
904exfat_read(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
905 size_t *rbytes)
906{
907 fs_node_t *fn;
908 exfat_node_t *nodep;
909 exfat_bs_t *bs;
910 size_t bytes=0;
911 block_t *b;
912 int rc;
913
914 rc = exfat_node_get(&fn, devmap_handle, index);
915 if (rc != EOK)
916 return rc;
917 if (!fn)
918 return ENOENT;
919 nodep = EXFAT_NODE(fn);
920
921 ipc_callid_t callid;
922 size_t len;
923 if (!async_data_read_receive(&callid, &len)) {
924 exfat_node_put(fn);
925 async_answer_0(callid, EINVAL);
926 return EINVAL;
927 }
928
929 bs = block_bb_get(devmap_handle);
930
931 if (nodep->type == EXFAT_FILE) {
932 /*
933 * Our strategy for regular file reads is to read one block at
934 * most and make use of the possibility to return less data than
935 * requested. This keeps the code very simple.
936 */
937 if (pos >= nodep->size) {
938 /* reading beyond the EOF */
939 bytes = 0;
940 (void) async_data_read_finalize(callid, NULL, 0);
941 } else {
942 bytes = min(len, BPS(bs) - pos % BPS(bs));
943 bytes = min(bytes, nodep->size - pos);
944 rc = exfat_block_get(&b, bs, nodep, pos / BPS(bs),
945 BLOCK_FLAGS_NONE);
946 if (rc != EOK) {
947 exfat_node_put(fn);
948 async_answer_0(callid, rc);
949 return rc;
950 }
951 (void) async_data_read_finalize(callid,
952 b->data + pos % BPS(bs), bytes);
953 rc = block_put(b);
954 if (rc != EOK) {
955 exfat_node_put(fn);
956 return rc;
957 }
958 }
959 } else {
960 if (nodep->type != EXFAT_DIRECTORY) {
961 async_answer_0(callid, ENOTSUP);
962 return ENOTSUP;
963 }
964
965 aoff64_t spos = pos;
966 char name[EXFAT_FILENAME_LEN+1];
967 exfat_file_dentry_t df;
968 exfat_stream_dentry_t ds;
969
970 assert(nodep->size % BPS(bs) == 0);
971 assert(BPS(bs) % sizeof(exfat_dentry_t) == 0);
972
973 exfat_directory_t di;
974 rc = exfat_directory_open(nodep, &di);
975 if (rc != EOK) goto err;
976 rc = exfat_directory_seek(&di, pos);
977 if (rc != EOK) {
978 (void) exfat_directory_close(&di);
979 goto err;
980 }
981
982 rc = exfat_directory_read_file(&di, name, EXFAT_FILENAME_LEN, &df, &ds);
983 if (rc == EOK) goto hit;
984 if (rc == ENOENT) goto miss;
985
986err:
987 (void) exfat_node_put(fn);
988 async_answer_0(callid, rc);
989 return rc;
990
991miss:
992 rc = exfat_directory_close(&di);
993 if (rc!=EOK)
994 goto err;
995 rc = exfat_node_put(fn);
996 async_answer_0(callid, rc != EOK ? rc : ENOENT);
997 *rbytes = 0;
998 return rc != EOK ? rc : ENOENT;
999
1000hit:
1001 pos = di.pos;
1002 rc = exfat_directory_close(&di);
1003 if (rc!=EOK)
1004 goto err;
1005 (void) async_data_read_finalize(callid, name, str_size(name) + 1);
1006 bytes = (pos - spos)+1;
1007 }
1008
1009 rc = exfat_node_put(fn);
1010 *rbytes = bytes;
1011 return rc;
1012}
1013
1014static int exfat_close(devmap_handle_t devmap_handle, fs_index_t index)
1015{
1016 return EOK;
1017}
1018
1019static int exfat_sync(devmap_handle_t devmap_handle, fs_index_t index)
1020{
1021 fs_node_t *fn;
1022 int rc = exfat_node_get(&fn, devmap_handle, index);
1023 if (rc != EOK)
1024 return rc;
1025 if (!fn)
1026 return ENOENT;
1027
1028 exfat_node_t *nodep = EXFAT_NODE(fn);
1029
1030 nodep->dirty = true;
1031 rc = exfat_node_sync(nodep);
1032
1033 exfat_node_put(fn);
1034 return rc;
1035}
1036
1037static int
1038exfat_write(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t pos,
1039 size_t *wbytes, aoff64_t *nsize)
1040{
1041 /* TODO */
1042 return EOK;
1043}
1044
1045static int
1046exfat_truncate(devmap_handle_t devmap_handle, fs_index_t index, aoff64_t size)
1047{
1048 /* TODO */
1049 return EOK;
1050}
1051static int exfat_destroy(devmap_handle_t devmap_handle, fs_index_t index)
1052{
1053 /* TODO */
1054 return EOK;
1055}
1056
1057vfs_out_ops_t exfat_ops = {
1058 .mounted = exfat_mounted,
1059 .unmounted = exfat_unmounted,
1060 .read = exfat_read,
1061 .write = exfat_write,
1062 .truncate = exfat_truncate,
1063 .close = exfat_close,
1064 .destroy = exfat_destroy,
1065 .sync = exfat_sync,
1066};
1067
1068
1069/**
1070 * @}
1071 */
Note: See TracBrowser for help on using the repository browser.