source: mainline/uspace/srv/fs/fat/fat_ops.c@ 4ba3535

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

Minor fixes for update

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