There is no need to check wether the inst pointer is NULL or not
[helenos.git] / uspace / srv / fs / mfs / mfs_ops.c
blob82923b8b4258fd708260b11da06c353d93660985
1 /*
2 * Copyright (c) 2011 Maurizio Lombardi
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup fs
30 * @{
33 #include <stdlib.h>
34 #include <fibril_synch.h>
35 #include <align.h>
36 #include <adt/hash_table.h>
37 #include <adt/hash.h>
38 #include "mfs.h"
41 static bool check_magic_number(uint16_t magic, bool *native,
42 mfs_version_t *version, bool *longfilenames);
43 static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
44 fs_index_t index);
45 static int mfs_node_put(fs_node_t *fsnode);
46 static int mfs_node_open(fs_node_t *fsnode);
47 static fs_index_t mfs_index_get(fs_node_t *fsnode);
48 static unsigned mfs_lnkcnt_get(fs_node_t *fsnode);
49 static bool mfs_is_directory(fs_node_t *fsnode);
50 static bool mfs_is_file(fs_node_t *fsnode);
51 static int mfs_has_children(bool *has_children, fs_node_t *fsnode);
52 static int mfs_root_get(fs_node_t **rfn, service_id_t service_id);
53 static service_id_t mfs_service_get(fs_node_t *fsnode);
54 static aoff64_t mfs_size_get(fs_node_t *node);
55 static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component);
56 static int mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags);
57 static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name);
58 static int mfs_unlink(fs_node_t *, fs_node_t *, const char *name);
59 static int mfs_destroy_node(fs_node_t *fn);
60 static int mfs_node_get(fs_node_t **rfn, service_id_t service_id,
61 fs_index_t index);
62 static int mfs_instance_get(service_id_t service_id,
63 struct mfs_instance **instance);
64 static int mfs_check_sanity(struct mfs_sb_info *sbi);
65 static bool is_power_of_two(uint32_t n);
66 static int mfs_size_block(service_id_t service_id, uint32_t *size);
67 static int mfs_total_block_count(service_id_t service_id, uint64_t *count);
68 static int mfs_free_block_count(service_id_t service_id, uint64_t *count);
70 static hash_table_t open_nodes;
71 static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
73 libfs_ops_t mfs_libfs_ops = {
74 .size_get = mfs_size_get,
75 .root_get = mfs_root_get,
76 .service_get = mfs_service_get,
77 .is_directory = mfs_is_directory,
78 .is_file = mfs_is_file,
79 .node_get = mfs_node_get,
80 .node_put = mfs_node_put,
81 .node_open = mfs_node_open,
82 .index_get = mfs_index_get,
83 .match = mfs_match,
84 .create = mfs_create_node,
85 .link = mfs_link,
86 .unlink = mfs_unlink,
87 .destroy = mfs_destroy_node,
88 .has_children = mfs_has_children,
89 .lnkcnt_get = mfs_lnkcnt_get,
90 .size_block = mfs_size_block,
91 .total_block_count = mfs_total_block_count,
92 .free_block_count = mfs_free_block_count
95 /* Hash table interface for open nodes hash table */
96 typedef struct {
97 service_id_t service_id;
98 fs_index_t index;
99 } node_key_t;
101 static size_t
102 open_nodes_key_hash(void *key)
104 node_key_t *node_key = (node_key_t*)key;
105 return hash_combine(node_key->service_id, node_key->index);
108 static size_t
109 open_nodes_hash(const ht_link_t *item)
111 struct mfs_node *m = hash_table_get_inst(item, struct mfs_node, link);
112 return hash_combine(m->instance->service_id, m->ino_i->index);
115 static bool
116 open_nodes_key_equal(void *key, const ht_link_t *item)
118 node_key_t *node_key = (node_key_t*)key;
119 struct mfs_node *mnode = hash_table_get_inst(item, struct mfs_node, link);
121 return node_key->service_id == mnode->instance->service_id
122 && node_key->index == mnode->ino_i->index;
125 static hash_table_ops_t open_nodes_ops = {
126 .hash = open_nodes_hash,
127 .key_hash = open_nodes_key_hash,
128 .key_equal = open_nodes_key_equal,
129 .equal = NULL,
130 .remove_callback = NULL,
134 mfs_global_init(void)
136 if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops)) {
137 return ENOMEM;
139 return EOK;
142 static int
143 mfs_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
144 aoff64_t *size, unsigned *linkcnt)
146 enum cache_mode cmode;
147 struct mfs_superblock *sb = NULL;
148 struct mfs3_superblock *sb3 = NULL;
149 struct mfs_sb_info *sbi = NULL;
150 struct mfs_instance *instance = NULL;
151 bool native, longnames;
152 mfs_version_t version;
153 uint16_t magic;
154 int rc;
156 /* Check for option enabling write through. */
157 if (str_cmp(opts, "wtcache") == 0)
158 cmode = CACHE_MODE_WT;
159 else
160 cmode = CACHE_MODE_WB;
162 /* initialize libblock */
163 rc = block_init(EXCHANGE_SERIALIZE, service_id, 4096);
164 if (rc != EOK)
165 return rc;
167 /* Allocate space for generic MFS superblock */
168 sbi = malloc(sizeof(*sbi));
169 if (!sbi) {
170 rc = ENOMEM;
171 goto out_error;
174 /* Allocate space for filesystem instance */
175 instance = malloc(sizeof(*instance));
176 if (!instance) {
177 rc = ENOMEM;
178 goto out_error;
181 sb = malloc(MFS_SUPERBLOCK_SIZE);
182 if (!sb) {
183 rc = ENOMEM;
184 goto out_error;
187 /* Read the superblock */
188 rc = block_read_direct(service_id, MFS_SUPERBLOCK << 1, 2, sb);
189 if (rc != EOK)
190 goto out_error;
192 sb3 = (struct mfs3_superblock *) sb;
194 if (check_magic_number(sb->s_magic, &native, &version, &longnames)) {
195 /* This is a V1 or V2 Minix filesystem */
196 magic = sb->s_magic;
197 } else if (check_magic_number(sb3->s_magic, &native,
198 &version, &longnames)) {
199 /* This is a V3 Minix filesystem */
200 magic = sb3->s_magic;
201 } else {
202 /* Not recognized */
203 mfsdebug("magic number not recognized\n");
204 rc = ENOTSUP;
205 goto out_error;
208 mfsdebug("magic number recognized = %04x\n", magic);
210 /* Fill superblock info structure */
212 sbi->fs_version = version;
213 sbi->long_names = longnames;
214 sbi->native = native;
215 sbi->magic = magic;
216 sbi->isearch = 0;
217 sbi->zsearch = 0;
219 if (version == MFS_VERSION_V3) {
220 sbi->ninodes = conv32(native, sb3->s_ninodes);
221 sbi->ibmap_blocks = conv16(native, sb3->s_ibmap_blocks);
222 sbi->zbmap_blocks = conv16(native, sb3->s_zbmap_blocks);
223 sbi->firstdatazone = conv16(native, sb3->s_first_data_zone);
224 sbi->log2_zone_size = conv16(native, sb3->s_log2_zone_size);
225 sbi->max_file_size = conv32(native, sb3->s_max_file_size);
226 sbi->nzones = conv32(native, sb3->s_nzones);
227 sbi->block_size = conv16(native, sb3->s_block_size);
228 sbi->ino_per_block = V3_INODES_PER_BLOCK(sbi->block_size);
229 sbi->dirsize = MFS3_DIRSIZE;
230 sbi->max_name_len = MFS3_MAX_NAME_LEN;
231 } else {
232 sbi->ninodes = conv16(native, sb->s_ninodes);
233 sbi->ibmap_blocks = conv16(native, sb->s_ibmap_blocks);
234 sbi->zbmap_blocks = conv16(native, sb->s_zbmap_blocks);
235 sbi->firstdatazone = conv16(native, sb->s_first_data_zone);
236 sbi->log2_zone_size = conv16(native, sb->s_log2_zone_size);
237 sbi->max_file_size = conv32(native, sb->s_max_file_size);
238 sbi->block_size = MFS_BLOCKSIZE;
239 if (version == MFS_VERSION_V2) {
240 sbi->nzones = conv32(native, sb->s_nzones2);
241 sbi->ino_per_block = V2_INODES_PER_BLOCK;
242 } else {
243 sbi->nzones = conv16(native, sb->s_nzones);
244 sbi->ino_per_block = V1_INODES_PER_BLOCK;
246 sbi->dirsize = longnames ? MFSL_DIRSIZE : MFS_DIRSIZE;
247 sbi->max_name_len = longnames ? MFS_L_MAX_NAME_LEN :
248 MFS_MAX_NAME_LEN;
251 if (sbi->log2_zone_size != 0) {
252 /* In MFS, file space is allocated per zones.
253 * Zones are a collection of consecutive blocks on disk.
255 * The current MFS implementation supports only filesystems
256 * where the size of a zone is equal to the
257 * size of a block.
259 rc = ENOTSUP;
260 goto out_error;
263 sbi->itable_off = 2 + sbi->ibmap_blocks + sbi->zbmap_blocks;
264 if ((rc = mfs_check_sanity(sbi)) != EOK) {
265 fprintf(stderr, "Filesystem corrupted, invalid superblock");
266 goto out_error;
269 rc = block_cache_init(service_id, sbi->block_size, 0, cmode);
270 if (rc != EOK) {
271 mfsdebug("block cache initialization failed\n");
272 rc = EINVAL;
273 goto out_error;
276 /* Initialize the instance structure and remember it */
277 instance->service_id = service_id;
278 instance->sbi = sbi;
279 instance->open_nodes_cnt = 0;
280 rc = fs_instance_create(service_id, instance);
281 if (rc != EOK) {
282 block_cache_fini(service_id);
283 mfsdebug("fs instance creation failed\n");
284 goto out_error;
287 mfsdebug("mount successful\n");
289 fs_node_t *fn;
290 mfs_node_get(&fn, service_id, MFS_ROOT_INO);
292 struct mfs_node *mroot = fn->data;
294 *index = mroot->ino_i->index;
295 *size = mroot->ino_i->i_size;
296 *linkcnt = 1;
298 free(sb);
300 return mfs_node_put(fn);
302 out_error:
303 block_fini(service_id);
304 if (sb)
305 free(sb);
306 if (sbi)
307 free(sbi);
308 if(instance)
309 free(instance);
310 return rc;
313 static int
314 mfs_unmounted(service_id_t service_id)
316 struct mfs_instance *inst;
318 mfsdebug("%s()\n", __FUNCTION__);
320 int r = mfs_instance_get(service_id, &inst);
321 if (r != EOK)
322 return r;
324 if (inst->open_nodes_cnt != 0)
325 return EBUSY;
327 (void) block_cache_fini(service_id);
328 block_fini(service_id);
330 /* Remove and destroy the instance */
331 (void) fs_instance_destroy(service_id);
332 free(inst->sbi);
333 free(inst);
334 return EOK;
337 service_id_t
338 mfs_service_get(fs_node_t *fsnode)
340 struct mfs_node *node = fsnode->data;
341 return node->instance->service_id;
344 static int
345 mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
347 int r;
348 struct mfs_instance *inst;
349 struct mfs_node *mnode;
350 fs_node_t *fsnode;
351 uint32_t inum;
353 r = mfs_instance_get(service_id, &inst);
354 if (r != EOK)
355 return r;
357 /* Alloc a new inode */
358 r = mfs_alloc_inode(inst, &inum);
359 if (r != EOK)
360 return r;
362 struct mfs_ino_info *ino_i;
364 ino_i = malloc(sizeof(*ino_i));
365 if (!ino_i) {
366 r = ENOMEM;
367 goto out_err;
370 mnode = malloc(sizeof(*mnode));
371 if (!mnode) {
372 r = ENOMEM;
373 goto out_err_1;
376 fsnode = malloc(sizeof(fs_node_t));
377 if (!fsnode) {
378 r = ENOMEM;
379 goto out_err_2;
382 if (flags & L_DIRECTORY) {
383 ino_i->i_mode = S_IFDIR;
384 ino_i->i_nlinks = 1; /* This accounts for the '.' dentry */
385 } else {
386 ino_i->i_mode = S_IFREG;
387 ino_i->i_nlinks = 0;
390 ino_i->i_uid = 0;
391 ino_i->i_gid = 0;
392 ino_i->i_size = 0;
393 ino_i->i_atime = 0;
394 ino_i->i_mtime = 0;
395 ino_i->i_ctime = 0;
397 memset(ino_i->i_dzone, 0, sizeof(uint32_t) * V2_NR_DIRECT_ZONES);
398 memset(ino_i->i_izone, 0, sizeof(uint32_t) * V2_NR_INDIRECT_ZONES);
400 mfsdebug("new node idx = %d\n", (int) inum);
402 ino_i->index = inum;
403 ino_i->dirty = true;
404 mnode->ino_i = ino_i;
405 mnode->instance = inst;
406 mnode->refcnt = 1;
408 fibril_mutex_lock(&open_nodes_lock);
409 hash_table_insert(&open_nodes, &mnode->link);
410 fibril_mutex_unlock(&open_nodes_lock);
411 inst->open_nodes_cnt++;
413 mnode->ino_i->dirty = true;
415 fs_node_initialize(fsnode);
416 fsnode->data = mnode;
417 mnode->fsnode = fsnode;
418 *rfn = fsnode;
420 return EOK;
422 out_err_2:
423 free(mnode);
424 out_err_1:
425 free(ino_i);
426 out_err:
427 mfs_free_inode(inst, inum);
428 return r;
431 static int
432 mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
434 struct mfs_node *mnode = pfn->data;
435 struct mfs_ino_info *ino_i = mnode->ino_i;
436 struct mfs_dentry_info d_info;
437 int r;
439 if (!S_ISDIR(ino_i->i_mode))
440 return ENOTDIR;
442 struct mfs_sb_info *sbi = mnode->instance->sbi;
443 const size_t comp_size = str_size(component);
445 unsigned i;
446 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
447 r = mfs_read_dentry(mnode, &d_info, i);
448 if (r != EOK)
449 return r;
451 if (!d_info.d_inum) {
452 /* This entry is not used */
453 continue;
456 const size_t dentry_name_size = str_size(d_info.d_name);
458 if (comp_size == dentry_name_size &&
459 memcmp(component, d_info.d_name, dentry_name_size) == 0) {
460 /* Hit! */
461 mfs_node_core_get(rfn, mnode->instance,
462 d_info.d_inum);
463 goto found;
466 *rfn = NULL;
467 found:
468 return EOK;
471 static aoff64_t
472 mfs_size_get(fs_node_t *node)
474 const struct mfs_node *mnode = node->data;
475 return mnode->ino_i->i_size;
478 static int
479 mfs_node_get(fs_node_t **rfn, service_id_t service_id,
480 fs_index_t index)
482 int rc;
483 struct mfs_instance *instance;
485 rc = mfs_instance_get(service_id, &instance);
486 if (rc != EOK)
487 return rc;
489 return mfs_node_core_get(rfn, instance, index);
492 static int
493 mfs_node_put(fs_node_t *fsnode)
495 int rc = EOK;
496 struct mfs_node *mnode = fsnode->data;
498 fibril_mutex_lock(&open_nodes_lock);
500 assert(mnode->refcnt > 0);
501 mnode->refcnt--;
502 if (mnode->refcnt == 0) {
503 hash_table_remove_item(&open_nodes, &mnode->link);
504 assert(mnode->instance->open_nodes_cnt > 0);
505 mnode->instance->open_nodes_cnt--;
506 rc = mfs_put_inode(mnode);
507 free(mnode->ino_i);
508 free(mnode);
509 free(fsnode);
512 fibril_mutex_unlock(&open_nodes_lock);
513 return rc;
516 static int
517 mfs_node_open(fs_node_t *fsnode)
520 * Opening a file is stateless, nothing
521 * to be done here.
523 return EOK;
526 static fs_index_t
527 mfs_index_get(fs_node_t *fsnode)
529 struct mfs_node *mnode = fsnode->data;
530 return mnode->ino_i->index;
533 static unsigned
534 mfs_lnkcnt_get(fs_node_t *fsnode)
536 struct mfs_node *mnode = fsnode->data;
538 mfsdebug("%s() %d\n", __FUNCTION__, mnode->ino_i->i_nlinks);
540 if (S_ISDIR(mnode->ino_i->i_mode)) {
541 if (mnode->ino_i->i_nlinks > 1)
542 return 1;
543 else
544 return 0;
545 } else
546 return mnode->ino_i->i_nlinks;
549 static int
550 mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
551 fs_index_t index)
553 fs_node_t *node = NULL;
554 struct mfs_node *mnode = NULL;
555 int rc;
557 fibril_mutex_lock(&open_nodes_lock);
559 /* Check if the node is not already open */
560 node_key_t key = {
561 .service_id = inst->service_id,
562 .index = index
565 ht_link_t *already_open = hash_table_find(&open_nodes, &key);
567 if (already_open) {
568 mnode = hash_table_get_inst(already_open, struct mfs_node, link);
570 *rfn = mnode->fsnode;
571 mnode->refcnt++;
573 fibril_mutex_unlock(&open_nodes_lock);
574 return EOK;
577 node = malloc(sizeof(fs_node_t));
578 if (!node) {
579 rc = ENOMEM;
580 goto out_err;
583 fs_node_initialize(node);
585 mnode = malloc(sizeof(*mnode));
586 if (!mnode) {
587 rc = ENOMEM;
588 goto out_err;
591 struct mfs_ino_info *ino_i;
593 rc = mfs_get_inode(inst, &ino_i, index);
594 if (rc != EOK)
595 goto out_err;
597 ino_i->index = index;
598 mnode->ino_i = ino_i;
599 mnode->refcnt = 1;
601 mnode->instance = inst;
602 node->data = mnode;
603 mnode->fsnode = node;
604 *rfn = node;
606 hash_table_insert(&open_nodes, &mnode->link);
607 inst->open_nodes_cnt++;
609 fibril_mutex_unlock(&open_nodes_lock);
611 return EOK;
613 out_err:
614 if (node)
615 free(node);
616 if (mnode)
617 free(mnode);
618 fibril_mutex_unlock(&open_nodes_lock);
619 return rc;
622 static bool
623 mfs_is_directory(fs_node_t *fsnode)
625 const struct mfs_node *node = fsnode->data;
626 return S_ISDIR(node->ino_i->i_mode);
629 static bool
630 mfs_is_file(fs_node_t *fsnode)
632 struct mfs_node *node = fsnode->data;
633 return S_ISREG(node->ino_i->i_mode);
636 static int
637 mfs_root_get(fs_node_t **rfn, service_id_t service_id)
639 int rc = mfs_node_get(rfn, service_id, MFS_ROOT_INO);
640 return rc;
643 static int
644 mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
646 struct mfs_node *parent = pfn->data;
647 struct mfs_node *child = cfn->data;
648 struct mfs_sb_info *sbi = parent->instance->sbi;
649 bool destroy_dentry = false;
651 if (str_size(name) > sbi->max_name_len)
652 return ENAMETOOLONG;
654 int r = mfs_insert_dentry(parent, name, child->ino_i->index);
655 if (r != EOK)
656 return r;
658 if (S_ISDIR(child->ino_i->i_mode)) {
659 if (child->ino_i->i_nlinks != 1) {
660 /* It's not possible to hardlink directories in MFS */
661 destroy_dentry = true;
662 r = EMLINK;
663 goto exit;
665 r = mfs_insert_dentry(child, ".", child->ino_i->index);
666 if (r != EOK) {
667 destroy_dentry = true;
668 goto exit;
671 r = mfs_insert_dentry(child, "..", parent->ino_i->index);
672 if (r != EOK) {
673 mfs_remove_dentry(child, ".");
674 destroy_dentry = true;
675 goto exit;
678 parent->ino_i->i_nlinks++;
679 parent->ino_i->dirty = true;
682 exit:
683 if (destroy_dentry) {
684 int r2 = mfs_remove_dentry(parent, name);
685 if (r2 != EOK)
686 r = r2;
687 } else {
688 child->ino_i->i_nlinks++;
689 child->ino_i->dirty = true;
691 return r;
694 static int
695 mfs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
697 struct mfs_node *parent = pfn->data;
698 struct mfs_node *child = cfn->data;
699 bool has_children;
700 int r;
702 if (!parent)
703 return EBUSY;
705 r = mfs_has_children(&has_children, cfn);
706 if (r != EOK)
707 return r;
709 if (has_children)
710 return ENOTEMPTY;
712 r = mfs_remove_dentry(parent, name);
713 if (r != EOK)
714 return r;
716 struct mfs_ino_info *chino = child->ino_i;
718 assert(chino->i_nlinks >= 1);
719 chino->i_nlinks--;
720 mfsdebug("Links: %d\n", chino->i_nlinks);
722 if (chino->i_nlinks <= 1 && S_ISDIR(chino->i_mode)) {
723 /* The child directory will be destroyed, decrease the
724 * parent hard links counter.
726 parent->ino_i->i_nlinks--;
727 parent->ino_i->dirty = true;
730 chino->dirty = true;
732 return r;
735 static int
736 mfs_has_children(bool *has_children, fs_node_t *fsnode)
738 struct mfs_node *mnode = fsnode->data;
739 struct mfs_sb_info *sbi = mnode->instance->sbi;
740 int r;
742 *has_children = false;
744 if (!S_ISDIR(mnode->ino_i->i_mode))
745 goto out;
747 struct mfs_dentry_info d_info;
749 /* The first two dentries are always . and .. */
750 unsigned i;
751 for (i = 2; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
752 r = mfs_read_dentry(mnode, &d_info, i);
753 if (r != EOK)
754 return r;
756 if (d_info.d_inum) {
757 /* A valid entry has been found */
758 *has_children = true;
759 break;
762 out:
764 return EOK;
767 static int
768 mfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
769 size_t *rbytes)
771 int rc;
772 fs_node_t *fn = NULL;
774 rc = mfs_node_get(&fn, service_id, index);
775 if (rc != EOK)
776 return rc;
777 if (!fn)
778 return ENOENT;
780 struct mfs_node *mnode;
781 struct mfs_ino_info *ino_i;
782 size_t len, bytes = 0;
783 ipc_callid_t callid;
785 mnode = fn->data;
786 ino_i = mnode->ino_i;
788 if (!async_data_read_receive(&callid, &len)) {
789 rc = EINVAL;
790 goto out_error;
793 if (S_ISDIR(ino_i->i_mode)) {
794 aoff64_t spos = pos;
795 struct mfs_dentry_info d_info;
796 struct mfs_sb_info *sbi = mnode->instance->sbi;
798 if (pos < 2) {
799 /* Skip the first two dentries ('.' and '..') */
800 pos = 2;
803 for (; pos < mnode->ino_i->i_size / sbi->dirsize; ++pos) {
804 rc = mfs_read_dentry(mnode, &d_info, pos);
805 if (rc != EOK)
806 goto out_error;
808 if (d_info.d_inum) {
809 /* Dentry found! */
810 goto found;
814 rc = mfs_node_put(fn);
815 async_answer_0(callid, rc != EOK ? rc : ENOENT);
816 return rc;
817 found:
818 async_data_read_finalize(callid, d_info.d_name,
819 str_size(d_info.d_name) + 1);
820 bytes = ((pos - spos) + 1);
821 } else {
822 struct mfs_sb_info *sbi = mnode->instance->sbi;
824 if (pos >= (size_t) ino_i->i_size) {
825 /* Trying to read beyond the end of file */
826 bytes = 0;
827 (void) async_data_read_finalize(callid, NULL, 0);
828 goto out_success;
831 bytes = min(len, sbi->block_size - pos % sbi->block_size);
832 bytes = min(bytes, ino_i->i_size - pos);
834 uint32_t zone;
835 block_t *b;
837 rc = mfs_read_map(&zone, mnode, pos);
838 if (rc != EOK)
839 goto out_error;
841 if (zone == 0) {
842 /* sparse file */
843 uint8_t *buf = malloc(sbi->block_size);
844 if (!buf) {
845 rc = ENOMEM;
846 goto out_error;
848 memset(buf, 0, sizeof(sbi->block_size));
849 async_data_read_finalize(callid,
850 buf + pos % sbi->block_size, bytes);
851 free(buf);
852 goto out_success;
855 rc = block_get(&b, service_id, zone, BLOCK_FLAGS_NONE);
856 if (rc != EOK)
857 goto out_error;
859 async_data_read_finalize(callid, b->data +
860 pos % sbi->block_size, bytes);
862 rc = block_put(b);
863 if (rc != EOK) {
864 mfs_node_put(fn);
865 return rc;
868 out_success:
869 rc = mfs_node_put(fn);
870 *rbytes = bytes;
871 return rc;
872 out_error:
874 int tmp = mfs_node_put(fn);
875 async_answer_0(callid, tmp != EOK ? tmp : rc);
876 return tmp != EOK ? tmp : rc;
879 static int
880 mfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
881 size_t *wbytes, aoff64_t *nsize)
883 fs_node_t *fn;
884 int r;
885 int flags = BLOCK_FLAGS_NONE;
887 r = mfs_node_get(&fn, service_id, index);
888 if (r != EOK)
889 return r;
890 if (!fn)
891 return ENOENT;
893 ipc_callid_t callid;
894 size_t len;
896 if (!async_data_write_receive(&callid, &len)) {
897 r = EINVAL;
898 goto out_err;
901 struct mfs_node *mnode = fn->data;
902 struct mfs_sb_info *sbi = mnode->instance->sbi;
903 struct mfs_ino_info *ino_i = mnode->ino_i;
904 const size_t bs = sbi->block_size;
905 size_t bytes = min(len, bs - (pos % bs));
906 uint32_t block;
908 if (bytes == bs)
909 flags = BLOCK_FLAGS_NOREAD;
911 r = mfs_read_map(&block, mnode, pos);
912 if (r != EOK)
913 goto out_err;
915 if (block == 0) {
916 uint32_t dummy;
918 r = mfs_alloc_zone(mnode->instance, &block);
919 if (r != EOK)
920 goto out_err;
922 r = mfs_write_map(mnode, pos, block, &dummy);
923 if (r != EOK) {
924 mfs_free_zone(mnode->instance, block);
925 goto out_err;
928 flags = BLOCK_FLAGS_NOREAD;
931 block_t *b;
932 r = block_get(&b, service_id, block, flags);
933 if (r != EOK)
934 goto out_err;
936 if (flags == BLOCK_FLAGS_NOREAD)
937 memset(b->data, 0, sbi->block_size);
939 async_data_write_finalize(callid, b->data + (pos % bs), bytes);
940 b->dirty = true;
942 r = block_put(b);
943 if (r != EOK) {
944 mfs_node_put(fn);
945 return r;
948 if (pos + bytes > ino_i->i_size) {
949 ino_i->i_size = pos + bytes;
950 ino_i->dirty = true;
952 r = mfs_node_put(fn);
953 *nsize = ino_i->i_size;
954 *wbytes = bytes;
955 return r;
957 out_err:
958 mfs_node_put(fn);
959 async_answer_0(callid, r);
960 return r;
963 static int
964 mfs_destroy(service_id_t service_id, fs_index_t index)
966 fs_node_t *fn = NULL;
967 int r;
969 r = mfs_node_get(&fn, service_id, index);
970 if (r != EOK)
971 return r;
972 if (!fn)
973 return ENOENT;
975 /* Destroy the inode */
976 return mfs_destroy_node(fn);
979 static int
980 mfs_destroy_node(fs_node_t *fn)
982 struct mfs_node *mnode = fn->data;
983 bool has_children;
984 int r;
986 mfsdebug("mfs_destroy_node %d\n", mnode->ino_i->index);
988 r = mfs_has_children(&has_children, fn);
989 if (r != EOK)
990 goto out;
992 assert(!has_children);
994 /* Free the entire inode content */
995 r = mfs_inode_shrink(mnode, mnode->ino_i->i_size);
996 if (r != EOK)
997 goto out;
999 /* Mark the inode as free in the bitmap */
1000 r = mfs_free_inode(mnode->instance, mnode->ino_i->index);
1002 out:
1003 mfs_node_put(fn);
1004 return r;
1007 static int
1008 mfs_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
1010 fs_node_t *fn;
1011 int r;
1013 r = mfs_node_get(&fn, service_id, index);
1014 if (r != EOK)
1015 return r;
1016 if (!fn)
1017 return r;
1019 struct mfs_node *mnode = fn->data;
1020 struct mfs_ino_info *ino_i = mnode->ino_i;
1022 if (ino_i->i_size == size)
1023 r = EOK;
1024 else
1025 r = mfs_inode_shrink(mnode, ino_i->i_size - size);
1027 mfs_node_put(fn);
1028 return r;
1031 static int
1032 mfs_instance_get(service_id_t service_id, struct mfs_instance **instance)
1034 void *data;
1035 int rc;
1037 rc = fs_instance_get(service_id, &data);
1038 if (rc == EOK)
1039 *instance = (struct mfs_instance *) data;
1040 else {
1041 mfsdebug("instance not found\n");
1044 return rc;
1047 static bool
1048 check_magic_number(uint16_t magic, bool *native,
1049 mfs_version_t *version, bool *longfilenames)
1051 bool rc = true;
1052 *longfilenames = false;
1054 if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
1055 *native = magic == MFS_MAGIC_V1;
1056 *version = MFS_VERSION_V1;
1057 } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
1058 *native = magic == MFS_MAGIC_V1L;
1059 *version = MFS_VERSION_V1;
1060 *longfilenames = true;
1061 } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
1062 *native = magic == MFS_MAGIC_V2;
1063 *version = MFS_VERSION_V2;
1064 } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
1065 *native = magic == MFS_MAGIC_V2L;
1066 *version = MFS_VERSION_V2;
1067 *longfilenames = true;
1068 } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
1069 *native = magic == MFS_MAGIC_V3;
1070 *version = MFS_VERSION_V3;
1071 } else
1072 rc = false;
1074 return rc;
1077 /** Filesystem sanity check
1079 * @param Pointer to the MFS superblock.
1081 * @return EOK on success, ENOTSUP otherwise.
1083 static int
1084 mfs_check_sanity(struct mfs_sb_info *sbi)
1086 if (!is_power_of_two(sbi->block_size) ||
1087 sbi->block_size < MFS_MIN_BLOCKSIZE ||
1088 sbi->block_size > MFS_MAX_BLOCKSIZE)
1089 return ENOTSUP;
1090 else if (sbi->ibmap_blocks == 0 || sbi->zbmap_blocks == 0)
1091 return ENOTSUP;
1092 else if (sbi->ninodes == 0 || sbi->nzones == 0)
1093 return ENOTSUP;
1094 else if (sbi->firstdatazone == 0)
1095 return ENOTSUP;
1097 return EOK;
1100 static int
1101 mfs_close(service_id_t service_id, fs_index_t index)
1103 return 0;
1106 static int
1107 mfs_sync(service_id_t service_id, fs_index_t index)
1109 fs_node_t *fn = NULL;
1110 int rc = mfs_node_get(&fn, service_id, index);
1111 if (rc != EOK)
1112 return rc;
1113 if (!fn)
1114 return ENOENT;
1116 struct mfs_node *mnode = fn->data;
1117 mnode->ino_i->dirty = true;
1119 return mfs_node_put(fn);
1122 /** Check if a given number is a power of two.
1124 * @param n The number to check.
1126 * @return true if it is a power of two, false otherwise.
1128 static bool
1129 is_power_of_two(uint32_t n)
1131 if (n == 0)
1132 return false;
1134 return (n & (n - 1)) == 0;
1137 static int
1138 mfs_size_block(service_id_t service_id, uint32_t *size)
1140 struct mfs_instance *inst;
1141 int rc;
1143 rc = mfs_instance_get(service_id, &inst);
1144 if (rc != EOK)
1145 return rc;
1147 if (NULL == inst)
1148 return ENOENT;
1150 *size = inst->sbi->block_size;
1152 return EOK;
1155 static int
1156 mfs_total_block_count(service_id_t service_id, uint64_t *count)
1158 struct mfs_instance *inst;
1159 int rc;
1161 rc = mfs_instance_get(service_id, &inst);
1162 if (rc != EOK)
1163 return rc;
1165 if (NULL == inst)
1166 return ENOENT;
1168 *count = (uint64_t) MFS_BMAP_SIZE_BITS(inst->sbi, BMAP_ZONE);
1170 return EOK;
1173 static int
1174 mfs_free_block_count(service_id_t service_id, uint64_t *count)
1176 uint32_t block_free;
1178 struct mfs_instance *inst;
1179 int rc = mfs_instance_get(service_id, &inst);
1180 if (rc != EOK)
1181 return rc;
1183 rc = mfs_count_free_zones(inst, &block_free);
1184 *count = block_free;
1186 return rc;
1189 vfs_out_ops_t mfs_ops = {
1190 .mounted = mfs_mounted,
1191 .unmounted = mfs_unmounted,
1192 .read = mfs_read,
1193 .write = mfs_write,
1194 .truncate = mfs_truncate,
1195 .close = mfs_close,
1196 .destroy = mfs_destroy,
1197 .sync = mfs_sync,
1201 * @}