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

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

exFAT:

  1. exfat_bitmap_get and exfat_uctable_get for access to special exFAT objects
  2. Implement exfat_node_get_new
  3. Draft iimplementation of exfat_node_get_core
  4. Implement exfat_node_get
  • Property mode set to 100644
File size: 16.1 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 "../../vfs/vfs.h"
43#include <libfs.h>
44#include <libblock.h>
45#include <ipc/services.h>
46#include <ipc/devmap.h>
47#include <macros.h>
48#include <async.h>
49#include <errno.h>
50#include <str.h>
51#include <byteorder.h>
52#include <adt/hash_table.h>
53#include <adt/list.h>
54#include <assert.h>
55#include <fibril_synch.h>
56#include <sys/mman.h>
57#include <align.h>
58#include <malloc.h>
59#include <stdio.h>
60
61#define EXFAT_NODE(node) ((node) ? (exfat_node_t *) (node)->data : NULL)
62#define FS_NODE(node) ((node) ? (node)->bp : NULL)
63
64
65/** Mutex protecting the list of cached free FAT nodes. */
66static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
67
68/** List of cached free FAT nodes. */
69static LIST_INITIALIZE(ffn_head);
70
71/*
72 * Forward declarations of FAT libfs operations.
73 */
74/*
75static int exfat_bitmap_get(fs_node_t **, devmap_handle_t);
76static int exfat_uctable_get(fs_node_t **, devmap_handle_t);
77*/
78static int exfat_root_get(fs_node_t **, devmap_handle_t);
79static int exfat_match(fs_node_t **, fs_node_t *, const char *);
80static int exfat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
81static int exfat_node_open(fs_node_t *);
82static int exfat_node_put(fs_node_t *);
83static int exfat_create_node(fs_node_t **, devmap_handle_t, int);
84static int exfat_destroy_node(fs_node_t *);
85static int exfat_link(fs_node_t *, fs_node_t *, const char *);
86static int exfat_unlink(fs_node_t *, fs_node_t *, const char *);
87static int exfat_has_children(bool *, fs_node_t *);
88static fs_index_t exfat_index_get(fs_node_t *);
89static aoff64_t exfat_size_get(fs_node_t *);
90static unsigned exfat_lnkcnt_get(fs_node_t *);
91static char exfat_plb_get_char(unsigned);
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 link_t *lnk;
126 exfat_node_t *nodep;
127 int rc;
128
129 /*
130 * We are called from fat_unmounted() and assume that there are already
131 * no nodes belonging to this instance with non-zero refcount. Therefore
132 * it is sufficient to clean up only the FAT free node list.
133 */
134
135restart:
136 fibril_mutex_lock(&ffn_mutex);
137 for (lnk = ffn_head.next; lnk != &ffn_head; lnk = lnk->next) {
138 nodep = list_get_instance(lnk, exfat_node_t, ffn_link);
139 if (!fibril_mutex_trylock(&nodep->lock)) {
140 fibril_mutex_unlock(&ffn_mutex);
141 goto restart;
142 }
143 if (!fibril_mutex_trylock(&nodep->idx->lock)) {
144 fibril_mutex_unlock(&nodep->lock);
145 fibril_mutex_unlock(&ffn_mutex);
146 goto restart;
147 }
148 if (nodep->idx->devmap_handle != devmap_handle) {
149 fibril_mutex_unlock(&nodep->idx->lock);
150 fibril_mutex_unlock(&nodep->lock);
151 continue;
152 }
153
154 list_remove(&nodep->ffn_link);
155 fibril_mutex_unlock(&ffn_mutex);
156
157 /*
158 * We can unlock the node and its index structure because we are
159 * the last player on this playground and VFS is preventing new
160 * players from entering.
161 */
162 fibril_mutex_unlock(&nodep->idx->lock);
163 fibril_mutex_unlock(&nodep->lock);
164
165 if (nodep->dirty) {
166 rc = exfat_node_sync(nodep);
167 if (rc != EOK)
168 return rc;
169 }
170 nodep->idx->nodep = NULL;
171 free(nodep->bp);
172 free(nodep);
173
174 /* Need to restart because we changed the ffn_head list. */
175 goto restart;
176 }
177 fibril_mutex_unlock(&ffn_mutex);
178
179 return EOK;
180}
181
182static int exfat_node_get_new(exfat_node_t **nodepp)
183{
184 fs_node_t *fn;
185 exfat_node_t *nodep;
186 int rc;
187
188 fibril_mutex_lock(&ffn_mutex);
189 if (!list_empty(&ffn_head)) {
190 /* Try to use a cached free node structure. */
191 exfat_idx_t *idxp_tmp;
192 nodep = list_get_instance(ffn_head.next, exfat_node_t, 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
239/** Internal version of exfat_node_get().
240 *
241 * @param idxp Locked index structure.
242 */
243static int exfat_node_get_core(exfat_node_t **nodepp, exfat_idx_t *idxp)
244{
245 block_t *b=NULL;
246 //exfat_bs_t *bs;
247 //exfat_dentry_t *d;
248 exfat_node_t *nodep = NULL;
249 int rc;
250
251 if (idxp->nodep) {
252 /*
253 * We are lucky.
254 * The node is already instantiated in memory.
255 */
256 fibril_mutex_lock(&idxp->nodep->lock);
257 if (!idxp->nodep->refcnt++) {
258 fibril_mutex_lock(&ffn_mutex);
259 list_remove(&idxp->nodep->ffn_link);
260 fibril_mutex_unlock(&ffn_mutex);
261 }
262 fibril_mutex_unlock(&idxp->nodep->lock);
263 *nodepp = idxp->nodep;
264 return EOK;
265 }
266
267 /*
268 * We must instantiate the node from the file system.
269 */
270
271 assert(idxp->pfc);
272
273 rc = exfat_node_get_new(&nodep);
274 if (rc != EOK)
275 return rc;
276
277 //bs = block_bb_get(idxp->devmap_handle);
278
279 /* Access to exFAT directory and read two entries:
280 * file entry and stream entry
281 */
282 /*
283 exfat_directory_t di;
284 exfat_dentry_t *de;
285 exfat_directory_open(&di, ???);
286 exfat_directory_seek(&di, idxp->pdi);
287 exfat_directory_get(&di, &de);
288
289 switch (exfat_classify_dentry(de)) {
290 case EXFAT_DENTRY_FILE:
291 nodep->type = (de->file.attr & EXFAT_ATTR_SUBDIR)?
292 EXFAT_DIRECTORY : EXFAT_FILE;
293 exfat_directory_next(&di);
294 exfat_directory_get(&di, &de);
295 nodep->firtsc = de->stream.firstc;
296 nodep->size = de->stream.data_size;
297 nodep->fragmented = (de->stream.flags & 0x02) == 0;
298 break;
299 case EXFAT_DENTRY_BITMAP:
300 nodep->type = EXFAT_BITMAP;
301 nodep->firstc = de->bitmap.firstc;
302 nodep->size = de->bitmap.size;
303 nodep->fragmented = false;
304 break;
305 case EXFAT_DENTRY_UCTABLE:
306 nodep->type = EXFAT_UCTABLE;
307 nodep->firstc = de->uctable.firstc;
308 nodep->size = de->uctable.size;
309 nodep->fragmented = false;
310 break;
311 default:
312 case EXFAT_DENTRY_SKIP:
313 case EXFAT_DENTRY_LAST:
314 case EXFAT_DENTRY_FREE:
315 case EXFAT_DENTRY_VOLLABEL:
316 case EXFAT_DENTRY_GUID:
317 case EXFAT_DENTRY_STREAM:
318 case EXFAT_DENTRY_NAME:
319 (void) block_put(b);
320 (void) fat_node_put(FS_NODE(nodep));
321 return ENOENT;
322 }
323 */
324
325 /* Read the block that contains the dentry of interest. */
326 /*
327 rc = _fat_block_get(&b, bs, idxp->devmap_handle, idxp->pfc, NULL,
328 (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
329 if (rc != EOK) {
330 (void) fat_node_put(FS_NODE(nodep));
331 return rc;
332 }
333
334 d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
335 */
336
337 nodep->lnkcnt = 1;
338 nodep->refcnt = 1;
339
340 rc = block_put(b);
341 if (rc != EOK) {
342 (void) exfat_node_put(FS_NODE(nodep));
343 return rc;
344 }
345
346 /* Link the idx structure with the node structure. */
347 nodep->idx = idxp;
348 idxp->nodep = nodep;
349
350 *nodepp = nodep;
351 return EOK;
352}
353
354
355
356/*
357 * EXFAT libfs operations.
358 */
359
360int exfat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
361{
362 return exfat_node_get(rfn, devmap_handle, EXFAT_ROOT_IDX);
363}
364
365/*
366int exfat_bitmap_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
367{
368 return exfat_node_get(rfn, devmap_handle, EXFAT_BITMAP_IDX);
369}
370
371int exfat_uctable_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
372{
373 return exfat_node_get(rfn, devmap_handle, EXFAT_UCTABLE_IDX);
374}
375*/
376
377int exfat_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
378{
379 *rfn = NULL;
380 return EOK;
381}
382
383/** Instantiate a exFAT in-core node. */
384int exfat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
385{
386 exfat_node_t *nodep;
387 exfat_idx_t *idxp;
388 int rc;
389
390 idxp = exfat_idx_get_by_index(devmap_handle, index);
391 if (!idxp) {
392 *rfn = NULL;
393 return EOK;
394 }
395 /* idxp->lock held */
396 rc = exfat_node_get_core(&nodep, idxp);
397 fibril_mutex_unlock(&idxp->lock);
398 if (rc == EOK)
399 *rfn = FS_NODE(nodep);
400 return rc;
401}
402
403int exfat_node_open(fs_node_t *fn)
404{
405 /*
406 * Opening a file is stateless, nothing
407 * to be done here.
408 */
409 return EOK;
410}
411
412int exfat_node_put(fs_node_t *fn)
413{
414 return EOK;
415}
416
417int exfat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
418{
419 *rfn = NULL;
420 return EOK;
421}
422
423int exfat_destroy_node(fs_node_t *fn)
424{
425 return EOK;
426}
427
428int exfat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
429{
430 return EOK;
431}
432
433int exfat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
434{
435 return EOK;
436}
437
438int exfat_has_children(bool *has_children, fs_node_t *fn)
439{
440 *has_children = false;
441 return EOK;
442}
443
444
445fs_index_t exfat_index_get(fs_node_t *fn)
446{
447 return EXFAT_NODE(fn)->idx->index;
448}
449
450aoff64_t exfat_size_get(fs_node_t *fn)
451{
452 return EXFAT_NODE(fn)->size;
453}
454
455unsigned exfat_lnkcnt_get(fs_node_t *fn)
456{
457 return EXFAT_NODE(fn)->lnkcnt;
458}
459
460char exfat_plb_get_char(unsigned pos)
461{
462 return exfat_reg.plb_ro[pos % PLB_SIZE];
463}
464
465bool exfat_is_directory(fs_node_t *fn)
466{
467 return EXFAT_NODE(fn)->type == EXFAT_DIRECTORY;
468}
469
470bool exfat_is_file(fs_node_t *fn)
471{
472 return EXFAT_NODE(fn)->type == EXFAT_FILE;
473}
474
475devmap_handle_t exfat_device_get(fs_node_t *node)
476{
477 return 0;
478}
479
480
481/** libfs operations */
482libfs_ops_t exfat_libfs_ops = {
483 .root_get = exfat_root_get,
484 .match = exfat_match,
485 .node_get = exfat_node_get,
486 .node_open = exfat_node_open,
487 .node_put = exfat_node_put,
488 .create = exfat_create_node,
489 .destroy = exfat_destroy_node,
490 .link = exfat_link,
491 .unlink = exfat_unlink,
492 .has_children = exfat_has_children,
493 .index_get = exfat_index_get,
494 .size_get = exfat_size_get,
495 .lnkcnt_get = exfat_lnkcnt_get,
496 .plb_get_char = exfat_plb_get_char,
497 .is_directory = exfat_is_directory,
498 .is_file = exfat_is_file,
499 .device_get = exfat_device_get
500};
501
502
503/*
504 * VFS operations.
505 */
506
507void exfat_mounted(ipc_callid_t rid, ipc_call_t *request)
508{
509 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
510 enum cache_mode cmode;
511 exfat_bs_t *bs;
512
513 /* Accept the mount options */
514 char *opts;
515 int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
516
517 if (rc != EOK) {
518 async_answer_0(rid, rc);
519 return;
520 }
521
522 /* Check for option enabling write through. */
523 if (str_cmp(opts, "wtcache") == 0)
524 cmode = CACHE_MODE_WT;
525 else
526 cmode = CACHE_MODE_WB;
527
528 free(opts);
529
530 /* initialize libblock */
531 rc = block_init(devmap_handle, BS_SIZE);
532 if (rc != EOK) {
533 async_answer_0(rid, rc);
534 return;
535 }
536
537 /* prepare the boot block */
538 rc = block_bb_read(devmap_handle, BS_BLOCK);
539 if (rc != EOK) {
540 block_fini(devmap_handle);
541 async_answer_0(rid, rc);
542 return;
543 }
544
545 /* get the buffer with the boot sector */
546 bs = block_bb_get(devmap_handle);
547
548 (void) bs;
549
550 /* Initialize the block cache */
551 rc = block_cache_init(devmap_handle, BS_SIZE, 0 /* XXX */, cmode);
552 if (rc != EOK) {
553 block_fini(devmap_handle);
554 async_answer_0(rid, rc);
555 return;
556 }
557
558 /* Do some simple sanity checks on the file system. */
559 rc = exfat_sanity_check(bs, devmap_handle);
560 if (rc != EOK) {
561 (void) block_cache_fini(devmap_handle);
562 block_fini(devmap_handle);
563 async_answer_0(rid, rc);
564 return;
565 }
566
567 rc = exfat_idx_init_by_devmap_handle(devmap_handle);
568 if (rc != EOK) {
569 (void) block_cache_fini(devmap_handle);
570 block_fini(devmap_handle);
571 async_answer_0(rid, rc);
572 return;
573 }
574
575 /* Initialize the root node. */
576 fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
577 if (!rfn) {
578 (void) block_cache_fini(devmap_handle);
579 block_fini(devmap_handle);
580 exfat_idx_fini_by_devmap_handle(devmap_handle);
581 async_answer_0(rid, ENOMEM);
582 return;
583 }
584
585 fs_node_initialize(rfn);
586 exfat_node_t *rootp = (exfat_node_t *)malloc(sizeof(exfat_node_t));
587 if (!rootp) {
588 free(rfn);
589 (void) block_cache_fini(devmap_handle);
590 block_fini(devmap_handle);
591 exfat_idx_fini_by_devmap_handle(devmap_handle);
592 async_answer_0(rid, ENOMEM);
593 return;
594 }
595
596 exfat_node_initialize(rootp);
597
598 exfat_idx_t *ridxp = exfat_idx_get_by_pos(devmap_handle, EXFAT_ROOT_PAR, 0);
599 if (!ridxp) {
600 free(rfn);
601 free(rootp);
602 (void) block_cache_fini(devmap_handle);
603 block_fini(devmap_handle);
604 exfat_idx_fini_by_devmap_handle(devmap_handle);
605 async_answer_0(rid, ENOMEM);
606 return;
607 }
608 assert(ridxp->index == 0);
609 /* ridxp->lock held */
610
611 rootp->type = EXFAT_DIRECTORY;
612 rootp->firstc = ROOT_ST(bs);
613 rootp->refcnt = 1;
614 rootp->lnkcnt = 0; /* FS root is not linked */
615 rootp->idx = ridxp;
616 ridxp->nodep = rootp;
617 rootp->bp = rfn;
618 rfn->data = rootp;
619
620 fibril_mutex_unlock(&ridxp->lock);
621
622 /* TODO */
623 /* We should intitalize bitmap and uctable nodes next to the root node */
624 /* HERE!!! */
625
626 /* async_answer_0(rid, EOK); */
627 async_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
628}
629
630void exfat_mount(ipc_callid_t rid, ipc_call_t *request)
631{
632 libfs_mount(&exfat_libfs_ops, exfat_reg.fs_handle, rid, request);
633}
634
635void exfat_unmounted(ipc_callid_t rid, ipc_call_t *request)
636{
637 devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
638 fs_node_t *fn;
639 exfat_node_t *nodep;
640 int rc;
641
642 rc = exfat_root_get(&fn, devmap_handle);
643 if (rc != EOK) {
644 async_answer_0(rid, rc);
645 return;
646 }
647 nodep = EXFAT_NODE(fn);
648
649 /*
650 * We expect exactly two references on the root node. One for the
651 * fat_root_get() above and one created in fat_mounted().
652 */
653 if (nodep->refcnt != 2) {
654 (void) exfat_node_put(fn);
655 async_answer_0(rid, EBUSY);
656 return;
657 }
658
659 /*
660 * Put the root node and force it to the FAT free node list.
661 */
662 (void) exfat_node_put(fn);
663 (void) exfat_node_put(fn);
664
665 /*
666 * Perform cleanup of the node structures, index structures and
667 * associated data. Write back this file system's dirty blocks and
668 * stop using libblock for this instance.
669 */
670 (void) exfat_node_fini_by_devmap_handle(devmap_handle);
671 exfat_idx_fini_by_devmap_handle(devmap_handle);
672 (void) block_cache_fini(devmap_handle);
673 block_fini(devmap_handle);
674
675 async_answer_0(rid, EOK);
676}
677
678void exfat_unmount(ipc_callid_t rid, ipc_call_t *request)
679{
680 libfs_unmount(&exfat_libfs_ops, rid, request);
681}
682
683
684/**
685 * @}
686 */
Note: See TracBrowser for help on using the repository browser.