source: mainline/uspace/srv/fs/mfs/mfs_ops.c@ 1e6dc5b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1e6dc5b was df3caec5, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

Add basic MINIX filesystem sanity check

  • Property mode set to 100644
File size: 25.2 KB
Line 
1/*
2 * Copyright (c) 2011 Maurizio Lombardi
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33#include <stdlib.h>
34#include <fibril_synch.h>
35#include <align.h>
36#include <adt/hash_table.h>
37#include "mfs.h"
38
39#define OPEN_NODES_KEYS 2
40#define OPEN_NODES_SERVICE_KEY 0
41#define OPEN_NODES_INODE_KEY 1
42#define OPEN_NODES_BUCKETS 256
43
44static bool check_magic_number(uint16_t magic, bool *native,
45 mfs_version_t *version, bool *longfilenames);
46static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
47 fs_index_t index);
48static int mfs_node_put(fs_node_t *fsnode);
49static int mfs_node_open(fs_node_t *fsnode);
50static fs_index_t mfs_index_get(fs_node_t *fsnode);
51static unsigned mfs_lnkcnt_get(fs_node_t *fsnode);
52static bool mfs_is_directory(fs_node_t *fsnode);
53static bool mfs_is_file(fs_node_t *fsnode);
54static int mfs_has_children(bool *has_children, fs_node_t *fsnode);
55static int mfs_root_get(fs_node_t **rfn, service_id_t service_id);
56static service_id_t mfs_service_get(fs_node_t *fsnode);
57static aoff64_t mfs_size_get(fs_node_t *node);
58static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component);
59static int mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags);
60static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name);
61static int mfs_unlink(fs_node_t *, fs_node_t *, const char *name);
62static int mfs_destroy_node(fs_node_t *fn);
63static hash_index_t open_nodes_hash(unsigned long key[]);
64static int open_nodes_compare(unsigned long key[], hash_count_t keys,
65 link_t *item);
66static void open_nodes_remove_cb(link_t *link);
67static int mfs_node_get(fs_node_t **rfn, service_id_t service_id,
68 fs_index_t index);
69static int mfs_instance_get(service_id_t service_id,
70 struct mfs_instance **instance);
71static int mfs_check_sanity(struct mfs_sb_info *sbi);
72static bool is_power_of_two(uint32_t n);
73
74static hash_table_t open_nodes;
75static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
76
77libfs_ops_t mfs_libfs_ops = {
78 .size_get = mfs_size_get,
79 .root_get = mfs_root_get,
80 .service_get = mfs_service_get,
81 .is_directory = mfs_is_directory,
82 .is_file = mfs_is_file,
83 .node_get = mfs_node_get,
84 .node_put = mfs_node_put,
85 .node_open = mfs_node_open,
86 .index_get = mfs_index_get,
87 .match = mfs_match,
88 .create = mfs_create_node,
89 .link = mfs_link,
90 .unlink = mfs_unlink,
91 .destroy = mfs_destroy_node,
92 .has_children = mfs_has_children,
93 .lnkcnt_get = mfs_lnkcnt_get
94};
95
96/* Hash table interface for open nodes hash table */
97static hash_index_t
98open_nodes_hash(unsigned long key[])
99{
100 /* TODO: This is very simple and probably can be improved */
101 return key[OPEN_NODES_INODE_KEY] % OPEN_NODES_BUCKETS;
102}
103
104static int
105open_nodes_compare(unsigned long key[], hash_count_t keys,
106 link_t *item)
107{
108 struct mfs_node *mnode = hash_table_get_instance(item, struct mfs_node, link);
109 assert(keys > 0);
110 if (mnode->instance->service_id !=
111 ((service_id_t) key[OPEN_NODES_SERVICE_KEY])) {
112 return false;
113 }
114 if (keys == 1) {
115 return true;
116 }
117 assert(keys == 2);
118 return (mnode->ino_i->index == key[OPEN_NODES_INODE_KEY]);
119}
120
121static void
122open_nodes_remove_cb(link_t *link)
123{
124 /* We don't use remove callback for this hash table */
125}
126
127static hash_table_operations_t open_nodes_ops = {
128 .hash = open_nodes_hash,
129 .compare = open_nodes_compare,
130 .remove_callback = open_nodes_remove_cb,
131};
132
133int
134mfs_global_init(void)
135{
136 if (!hash_table_create(&open_nodes, OPEN_NODES_BUCKETS,
137 OPEN_NODES_KEYS, &open_nodes_ops)) {
138 return ENOMEM;
139 }
140 return EOK;
141}
142
143static int
144mfs_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
145 aoff64_t *size, unsigned *linkcnt)
146{
147 enum cache_mode cmode;
148 struct mfs_superblock *sb = NULL;
149 struct mfs3_superblock *sb3 = NULL;
150 struct mfs_sb_info *sbi = NULL;
151 struct mfs_instance *instance = NULL;
152 bool native, longnames;
153 mfs_version_t version;
154 uint16_t magic;
155 int rc;
156
157 /* Check for option enabling write through. */
158 if (str_cmp(opts, "wtcache") == 0)
159 cmode = CACHE_MODE_WT;
160 else
161 cmode = CACHE_MODE_WB;
162
163 /* initialize libblock */
164 rc = block_init(EXCHANGE_SERIALIZE, service_id, 4096);
165 if (rc != EOK)
166 return rc;
167
168 /* Allocate space for generic MFS superblock */
169 sbi = malloc(sizeof(*sbi));
170 if (!sbi) {
171 rc = ENOMEM;
172 goto out_error;
173 }
174
175 /* Allocate space for filesystem instance */
176 instance = malloc(sizeof(*instance));
177 if (!instance) {
178 rc = ENOMEM;
179 goto out_error;
180 }
181
182 sb = malloc(MFS_SUPERBLOCK_SIZE);
183 if (!sb) {
184 rc = ENOMEM;
185 goto out_error;
186 }
187
188 /* Read the superblock */
189 rc = block_read_direct(service_id, MFS_SUPERBLOCK << 1, 2, sb);
190 if (rc != EOK)
191 goto out_error;
192
193 sb3 = (struct mfs3_superblock *) sb;
194
195 if (check_magic_number(sb->s_magic, &native, &version, &longnames)) {
196 /* This is a V1 or V2 Minix filesystem */
197 magic = sb->s_magic;
198 } else if (check_magic_number(sb3->s_magic, &native, &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;
206 }
207
208 mfsdebug("magic number recognized = %04x\n", magic);
209
210 /* Fill superblock info structure */
211
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;
218
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;
245 }
246 sbi->dirsize = longnames ? MFSL_DIRSIZE : MFS_DIRSIZE;
247 sbi->max_name_len = longnames ? MFS_L_MAX_NAME_LEN :
248 MFS_MAX_NAME_LEN;
249 }
250
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.
254 *
255 * The current MFS implementation supports only filesystems
256 * where the size of a zone is equal to the
257 * size of a block.
258 */
259 rc = ENOTSUP;
260 goto out_error;
261 }
262
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;
267 }
268
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;
274 }
275
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;
285 }
286
287 mfsdebug("mount successful\n");
288
289 fs_node_t *fn;
290 mfs_node_get(&fn, service_id, MFS_ROOT_INO);
291
292 struct mfs_node *mroot = fn->data;
293
294 *index = mroot->ino_i->index;
295 *size = mroot->ino_i->i_size;
296 *linkcnt = 1;
297
298 free(sb);
299
300 return mfs_node_put(fn);
301
302out_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;
311}
312
313static int
314mfs_unmounted(service_id_t service_id)
315{
316 struct mfs_instance *inst;
317
318 mfsdebug("%s()\n", __FUNCTION__);
319
320 int r = mfs_instance_get(service_id, &inst);
321 if (r != EOK)
322 return r;
323
324 if (inst->open_nodes_cnt != 0)
325 return EBUSY;
326
327 (void) block_cache_fini(service_id);
328 block_fini(service_id);
329
330 /* Remove and destroy the instance */
331 (void) fs_instance_destroy(service_id);
332 free(inst->sbi);
333 free(inst);
334 return EOK;
335}
336
337service_id_t
338mfs_service_get(fs_node_t *fsnode)
339{
340 struct mfs_node *node = fsnode->data;
341 return node->instance->service_id;
342}
343
344static int
345mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
346{
347 int r;
348 struct mfs_instance *inst;
349 struct mfs_node *mnode;
350 fs_node_t *fsnode;
351 uint32_t inum;
352
353 mfsdebug("%s()\n", __FUNCTION__);
354
355 r = mfs_instance_get(service_id, &inst);
356 if (r != EOK)
357 return r;
358
359 /* Alloc a new inode */
360 r = mfs_alloc_inode(inst, &inum);
361 if (r != EOK)
362 return r;
363
364 struct mfs_ino_info *ino_i;
365
366 ino_i = malloc(sizeof(*ino_i));
367 if (!ino_i) {
368 r = ENOMEM;
369 goto out_err;
370 }
371
372 mnode = malloc(sizeof(*mnode));
373 if (!mnode) {
374 r = ENOMEM;
375 goto out_err_1;
376 }
377
378 fsnode = malloc(sizeof(fs_node_t));
379 if (!fsnode) {
380 r = ENOMEM;
381 goto out_err_2;
382 }
383
384 if (flags & L_DIRECTORY) {
385 ino_i->i_mode = S_IFDIR;
386 ino_i->i_nlinks = 2; /* This accounts for the '.' dentry */
387 } else {
388 ino_i->i_mode = S_IFREG;
389 ino_i->i_nlinks = 1;
390 }
391
392 ino_i->i_uid = 0;
393 ino_i->i_gid = 0;
394 ino_i->i_size = 0;
395 ino_i->i_atime = 0;
396 ino_i->i_mtime = 0;
397 ino_i->i_ctime = 0;
398
399 memset(ino_i->i_dzone, 0, sizeof(uint32_t) * V2_NR_DIRECT_ZONES);
400 memset(ino_i->i_izone, 0, sizeof(uint32_t) * V2_NR_INDIRECT_ZONES);
401
402 mfsdebug("new node idx = %d\n", (int) inum);
403
404 ino_i->index = inum;
405 ino_i->dirty = true;
406 mnode->ino_i = ino_i;
407 mnode->instance = inst;
408 mnode->refcnt = 1;
409
410 link_initialize(&mnode->link);
411
412 unsigned long key[] = {
413 [OPEN_NODES_SERVICE_KEY] = inst->service_id,
414 [OPEN_NODES_INODE_KEY] = inum,
415 };
416
417 fibril_mutex_lock(&open_nodes_lock);
418 hash_table_insert(&open_nodes, key, &mnode->link);
419 fibril_mutex_unlock(&open_nodes_lock);
420 inst->open_nodes_cnt++;
421
422 mnode->ino_i->dirty = true;
423
424 fs_node_initialize(fsnode);
425 fsnode->data = mnode;
426 mnode->fsnode = fsnode;
427 *rfn = fsnode;
428
429 return EOK;
430
431out_err_2:
432 free(mnode);
433out_err_1:
434 free(ino_i);
435out_err:
436 return r;
437}
438
439static int
440mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
441{
442 struct mfs_node *mnode = pfn->data;
443 struct mfs_ino_info *ino_i = mnode->ino_i;
444 struct mfs_dentry_info d_info;
445 int r;
446
447 mfsdebug("%s()\n", __FUNCTION__);
448
449 if (!S_ISDIR(ino_i->i_mode))
450 return ENOTDIR;
451
452 struct mfs_sb_info *sbi = mnode->instance->sbi;
453 const size_t comp_size = str_size(component);
454
455 unsigned i;
456 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
457 r = mfs_read_dentry(mnode, &d_info, i);
458 if (r != EOK)
459 return r;
460
461 if (!d_info.d_inum) {
462 /* This entry is not used */
463 continue;
464 }
465
466 const size_t dentry_name_size = str_size(d_info.d_name);
467
468 if (comp_size == dentry_name_size &&
469 !bcmp(component, d_info.d_name, dentry_name_size)) {
470 /* Hit! */
471 mfs_node_core_get(rfn, mnode->instance,
472 d_info.d_inum);
473 goto found;
474 }
475 }
476 *rfn = NULL;
477found:
478 return EOK;
479}
480
481static aoff64_t
482mfs_size_get(fs_node_t *node)
483{
484 const struct mfs_node *mnode = node->data;
485 return mnode->ino_i->i_size;
486}
487
488static int
489mfs_node_get(fs_node_t **rfn, service_id_t service_id,
490 fs_index_t index)
491{
492 int rc;
493 struct mfs_instance *instance;
494
495 mfsdebug("%s()\n", __FUNCTION__);
496
497 rc = mfs_instance_get(service_id, &instance);
498 if (rc != EOK)
499 return rc;
500
501 return mfs_node_core_get(rfn, instance, index);
502}
503
504static int
505mfs_node_put(fs_node_t *fsnode)
506{
507 int rc = EOK;
508 struct mfs_node *mnode = fsnode->data;
509
510 mfsdebug("%s()\n", __FUNCTION__);
511
512 fibril_mutex_lock(&open_nodes_lock);
513
514 assert(mnode->refcnt > 0);
515 mnode->refcnt--;
516 if (mnode->refcnt == 0) {
517 unsigned long key[] = {
518 [OPEN_NODES_SERVICE_KEY] = mnode->instance->service_id,
519 [OPEN_NODES_INODE_KEY] = mnode->ino_i->index
520 };
521 hash_table_remove(&open_nodes, key, OPEN_NODES_KEYS);
522 assert(mnode->instance->open_nodes_cnt > 0);
523 mnode->instance->open_nodes_cnt--;
524 rc = mfs_put_inode(mnode);
525 free(mnode->ino_i);
526 free(mnode);
527 free(fsnode);
528 }
529
530 fibril_mutex_unlock(&open_nodes_lock);
531 return rc;
532}
533
534static int
535mfs_node_open(fs_node_t *fsnode)
536{
537 /*
538 * Opening a file is stateless, nothing
539 * to be done here.
540 */
541 return EOK;
542}
543
544static fs_index_t
545mfs_index_get(fs_node_t *fsnode)
546{
547 struct mfs_node *mnode = fsnode->data;
548 return mnode->ino_i->index;
549}
550
551static unsigned
552mfs_lnkcnt_get(fs_node_t *fsnode)
553{
554 struct mfs_node *mnode = fsnode->data;
555
556 mfsdebug("%s() %d\n", __FUNCTION__, mnode->ino_i->i_nlinks);
557
558 if (S_ISDIR(mnode->ino_i->i_mode)) {
559 if (mnode->ino_i->i_nlinks > 1)
560 return 1;
561 else
562 return 0;
563 } else
564 return mnode->ino_i->i_nlinks;
565}
566
567static int
568mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
569 fs_index_t index)
570{
571 fs_node_t *node = NULL;
572 struct mfs_node *mnode = NULL;
573 int rc;
574
575 mfsdebug("%s()\n", __FUNCTION__);
576
577 fibril_mutex_lock(&open_nodes_lock);
578
579 /* Check if the node is not already open */
580 unsigned long key[] = {
581 [OPEN_NODES_SERVICE_KEY] = inst->service_id,
582 [OPEN_NODES_INODE_KEY] = index,
583 };
584 link_t *already_open = hash_table_find(&open_nodes, key);
585
586 if (already_open) {
587 mnode = hash_table_get_instance(already_open, struct mfs_node, link);
588 *rfn = mnode->fsnode;
589 mnode->refcnt++;
590
591 fibril_mutex_unlock(&open_nodes_lock);
592 return EOK;
593 }
594
595 node = malloc(sizeof(fs_node_t));
596 if (!node) {
597 rc = ENOMEM;
598 goto out_err;
599 }
600
601 fs_node_initialize(node);
602
603 mnode = malloc(sizeof(*mnode));
604 if (!mnode) {
605 rc = ENOMEM;
606 goto out_err;
607 }
608
609 struct mfs_ino_info *ino_i;
610
611 rc = mfs_get_inode(inst, &ino_i, index);
612 if (rc != EOK)
613 goto out_err;
614
615 ino_i->index = index;
616 mnode->ino_i = ino_i;
617 mnode->refcnt = 1;
618 link_initialize(&mnode->link);
619
620 mnode->instance = inst;
621 node->data = mnode;
622 mnode->fsnode = node;
623 *rfn = node;
624
625 hash_table_insert(&open_nodes, key, &mnode->link);
626 inst->open_nodes_cnt++;
627
628 fibril_mutex_unlock(&open_nodes_lock);
629
630 return EOK;
631
632out_err:
633 if (node)
634 free(node);
635 if (mnode)
636 free(mnode);
637 fibril_mutex_unlock(&open_nodes_lock);
638 return rc;
639}
640
641static bool
642mfs_is_directory(fs_node_t *fsnode)
643{
644 const struct mfs_node *node = fsnode->data;
645 return S_ISDIR(node->ino_i->i_mode);
646}
647
648static bool
649mfs_is_file(fs_node_t *fsnode)
650{
651 struct mfs_node *node = fsnode->data;
652 return S_ISREG(node->ino_i->i_mode);
653}
654
655static int
656mfs_root_get(fs_node_t **rfn, service_id_t service_id)
657{
658 int rc = mfs_node_get(rfn, service_id, MFS_ROOT_INO);
659 return rc;
660}
661
662static int
663mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
664{
665 struct mfs_node *parent = pfn->data;
666 struct mfs_node *child = cfn->data;
667 struct mfs_sb_info *sbi = parent->instance->sbi;
668
669 mfsdebug("%s()\n", __FUNCTION__);
670
671 if (str_size(name) > sbi->max_name_len)
672 return ENAMETOOLONG;
673
674 int r = mfs_insert_dentry(parent, name, child->ino_i->index);
675 if (r != EOK)
676 goto exit_error;
677
678 if (S_ISDIR(child->ino_i->i_mode)) {
679 r = mfs_insert_dentry(child, ".", child->ino_i->index);
680 if (r != EOK)
681 goto exit_error;
682
683 r = mfs_insert_dentry(child, "..", parent->ino_i->index);
684 if (r != EOK)
685 goto exit_error;
686
687 parent->ino_i->i_nlinks++;
688 parent->ino_i->dirty = true;
689 }
690
691exit_error:
692 return r;
693}
694
695static int
696mfs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
697{
698 struct mfs_node *parent = pfn->data;
699 struct mfs_node *child = cfn->data;
700 bool has_children;
701 int r;
702
703 mfsdebug("%s()\n", __FUNCTION__);
704
705 if (!parent)
706 return EBUSY;
707
708 r = mfs_has_children(&has_children, cfn);
709 if (r != EOK)
710 return r;
711
712 if (has_children)
713 return ENOTEMPTY;
714
715 r = mfs_remove_dentry(parent, name);
716 if (r != EOK)
717 return r;
718
719 struct mfs_ino_info *chino = child->ino_i;
720
721 assert(chino->i_nlinks >= 1);
722 chino->i_nlinks--;
723 mfsdebug("Links: %d\n", chino->i_nlinks);
724
725 if (chino->i_nlinks <= 1 && S_ISDIR(chino->i_mode)) {
726 /* The child directory will be destroyed, decrease the
727 * parent hard links counter.
728 */
729 parent->ino_i->i_nlinks--;
730 parent->ino_i->dirty = true;
731 }
732
733 chino->dirty = true;
734
735 return r;
736}
737
738static int
739mfs_has_children(bool *has_children, fs_node_t *fsnode)
740{
741 struct mfs_node *mnode = fsnode->data;
742 struct mfs_sb_info *sbi = mnode->instance->sbi;
743 int r;
744
745 *has_children = false;
746
747 if (!S_ISDIR(mnode->ino_i->i_mode))
748 goto out;
749
750 struct mfs_dentry_info d_info;
751
752 /* The first two dentries are always . and .. */
753 unsigned i;
754 for (i = 2; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
755 r = mfs_read_dentry(mnode, &d_info, i);
756 if (r != EOK)
757 return r;
758
759 if (d_info.d_inum) {
760 /* A valid entry has been found */
761 *has_children = true;
762 break;
763 }
764 }
765out:
766
767 return EOK;
768}
769
770static int
771mfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
772 size_t *rbytes)
773{
774 int rc;
775 fs_node_t *fn;
776
777 rc = mfs_node_get(&fn, service_id, index);
778 if (rc != EOK)
779 return rc;
780 if (!fn)
781 return ENOENT;
782
783 struct mfs_node *mnode;
784 struct mfs_ino_info *ino_i;
785 size_t len, bytes = 0;
786 ipc_callid_t callid;
787
788 mnode = fn->data;
789 ino_i = mnode->ino_i;
790
791 if (!async_data_read_receive(&callid, &len)) {
792 rc = EINVAL;
793 goto out_error;
794 }
795
796 if (S_ISDIR(ino_i->i_mode)) {
797 aoff64_t spos = pos;
798 struct mfs_dentry_info d_info;
799 struct mfs_sb_info *sbi = mnode->instance->sbi;
800
801 if (pos < 2) {
802 /* Skip the first two dentries ('.' and '..') */
803 pos = 2;
804 }
805
806 for (; pos < mnode->ino_i->i_size / sbi->dirsize; ++pos) {
807 rc = mfs_read_dentry(mnode, &d_info, pos);
808 if (rc != EOK)
809 goto out_error;
810
811 if (d_info.d_inum) {
812 /* Dentry found! */
813 goto found;
814 }
815 }
816
817 rc = mfs_node_put(fn);
818 async_answer_0(callid, rc != EOK ? rc : ENOENT);
819 return rc;
820found:
821 async_data_read_finalize(callid, d_info.d_name,
822 str_size(d_info.d_name) + 1);
823 bytes = ((pos - spos) + 1);
824 } else {
825 struct mfs_sb_info *sbi = mnode->instance->sbi;
826
827 if (pos >= (size_t) ino_i->i_size) {
828 /* Trying to read beyond the end of file */
829 bytes = 0;
830 (void) async_data_read_finalize(callid, NULL, 0);
831 goto out_success;
832 }
833
834 bytes = min(len, sbi->block_size - pos % sbi->block_size);
835 bytes = min(bytes, ino_i->i_size - pos);
836
837 uint32_t zone;
838 block_t *b;
839
840 rc = mfs_read_map(&zone, mnode, pos);
841 if (rc != EOK)
842 goto out_error;
843
844 if (zone == 0) {
845 /* sparse file */
846 uint8_t *buf = malloc(sbi->block_size);
847 if (!buf) {
848 rc = ENOMEM;
849 goto out_error;
850 }
851 memset(buf, 0, sizeof(sbi->block_size));
852 async_data_read_finalize(callid,
853 buf + pos % sbi->block_size, bytes);
854 free(buf);
855 goto out_success;
856 }
857
858 rc = block_get(&b, service_id, zone, BLOCK_FLAGS_NONE);
859 if (rc != EOK)
860 goto out_error;
861
862 async_data_read_finalize(callid, b->data +
863 pos % sbi->block_size, bytes);
864
865 rc = block_put(b);
866 if (rc != EOK) {
867 mfs_node_put(fn);
868 return rc;
869 }
870 }
871out_success:
872 rc = mfs_node_put(fn);
873 *rbytes = bytes;
874 return rc;
875out_error:
876 ;
877 int tmp = mfs_node_put(fn);
878 async_answer_0(callid, tmp != EOK ? tmp : rc);
879 return tmp != EOK ? tmp : rc;
880}
881
882static int
883mfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
884 size_t *wbytes, aoff64_t *nsize)
885{
886 fs_node_t *fn;
887 int r;
888 int flags = BLOCK_FLAGS_NONE;
889
890 r = mfs_node_get(&fn, service_id, index);
891 if (r != EOK)
892 return r;
893 if (!fn)
894 return ENOENT;
895
896 ipc_callid_t callid;
897 size_t len;
898
899 if (!async_data_write_receive(&callid, &len)) {
900 r = EINVAL;
901 goto out_err;
902 }
903
904 struct mfs_node *mnode = fn->data;
905 struct mfs_sb_info *sbi = mnode->instance->sbi;
906 struct mfs_ino_info *ino_i = mnode->ino_i;
907 const size_t bs = sbi->block_size;
908 size_t bytes = min(len, bs - (pos % bs));
909 uint32_t block;
910
911 if (bytes == bs)
912 flags = BLOCK_FLAGS_NOREAD;
913
914 r = mfs_read_map(&block, mnode, pos);
915 if (r != EOK)
916 goto out_err;
917
918 if (block == 0) {
919 uint32_t dummy;
920
921 r = mfs_alloc_zone(mnode->instance, &block);
922 if (r != EOK)
923 goto out_err;
924
925 r = mfs_write_map(mnode, pos, block, &dummy);
926 if (r != EOK)
927 goto out_err;
928
929 flags = BLOCK_FLAGS_NOREAD;
930 }
931
932 block_t *b;
933 r = block_get(&b, service_id, block, flags);
934 if (r != EOK)
935 goto out_err;
936
937 if (flags == BLOCK_FLAGS_NOREAD)
938 memset(b->data, 0, sbi->block_size);
939
940 async_data_write_finalize(callid, b->data + (pos % bs), bytes);
941 b->dirty = true;
942
943 r = block_put(b);
944 if (r != EOK) {
945 mfs_node_put(fn);
946 return r;
947 }
948
949 if (pos + bytes > ino_i->i_size) {
950 ino_i->i_size = pos + bytes;
951 ino_i->dirty = true;
952 }
953 r = mfs_node_put(fn);
954 *nsize = ino_i->i_size;
955 *wbytes = bytes;
956 return r;
957
958out_err:
959 mfs_node_put(fn);
960 async_answer_0(callid, r);
961 return r;
962}
963
964static int
965mfs_destroy(service_id_t service_id, fs_index_t index)
966{
967 fs_node_t *fn;
968 int r;
969
970 r = mfs_node_get(&fn, service_id, index);
971 if (r != EOK)
972 return r;
973 if (!fn)
974 return ENOENT;
975
976 /* Destroy the inode */
977 return mfs_destroy_node(fn);
978}
979
980static int
981mfs_destroy_node(fs_node_t *fn)
982{
983 struct mfs_node *mnode = fn->data;
984 bool has_children;
985 int r;
986
987 mfsdebug("mfs_destroy_node %d\n", mnode->ino_i->index);
988
989 r = mfs_has_children(&has_children, fn);
990 if (r != EOK)
991 goto out;
992
993 assert(!has_children);
994
995 /* Free the entire inode content */
996 r = mfs_inode_shrink(mnode, mnode->ino_i->i_size);
997 if (r != EOK)
998 goto out;
999
1000 /* Mark the inode as free in the bitmap */
1001 r = mfs_free_inode(mnode->instance, mnode->ino_i->index);
1002
1003out:
1004 mfs_node_put(fn);
1005 return r;
1006}
1007
1008static int
1009mfs_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
1010{
1011 fs_node_t *fn;
1012 int r;
1013
1014 r = mfs_node_get(&fn, service_id, index);
1015 if (r != EOK)
1016 return r;
1017 if (!fn)
1018 return r;
1019
1020 struct mfs_node *mnode = fn->data;
1021 struct mfs_ino_info *ino_i = mnode->ino_i;
1022
1023 if (ino_i->i_size == size)
1024 r = EOK;
1025 else
1026 r = mfs_inode_shrink(mnode, ino_i->i_size - size);
1027
1028 mfs_node_put(fn);
1029 return r;
1030}
1031
1032static int
1033mfs_instance_get(service_id_t service_id, struct mfs_instance **instance)
1034{
1035 void *data;
1036 int rc;
1037
1038 rc = fs_instance_get(service_id, &data);
1039 if (rc == EOK)
1040 *instance = (struct mfs_instance *) data;
1041 else {
1042 mfsdebug("instance not found\n");
1043 }
1044
1045 return rc;
1046}
1047
1048static bool
1049check_magic_number(uint16_t magic, bool *native,
1050 mfs_version_t *version, bool *longfilenames)
1051{
1052 bool rc = true;
1053 *longfilenames = false;
1054
1055 if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
1056 *native = magic == MFS_MAGIC_V1;
1057 *version = MFS_VERSION_V1;
1058 } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
1059 *native = magic == MFS_MAGIC_V1L;
1060 *version = MFS_VERSION_V1;
1061 *longfilenames = true;
1062 } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
1063 *native = magic == MFS_MAGIC_V2;
1064 *version = MFS_VERSION_V2;
1065 } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
1066 *native = magic == MFS_MAGIC_V2L;
1067 *version = MFS_VERSION_V2;
1068 *longfilenames = true;
1069 } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
1070 *native = magic == MFS_MAGIC_V3;
1071 *version = MFS_VERSION_V3;
1072 } else
1073 rc = false;
1074
1075 return rc;
1076}
1077
1078/** Filesystem sanity check
1079 *
1080 * @param Pointer to the MFS superblock.
1081 *
1082 * @return EOK on success, ENOTSUP otherwise.
1083 */
1084static int
1085mfs_check_sanity(struct mfs_sb_info *sbi)
1086{
1087 if (!is_power_of_two(sbi->block_size) ||
1088 sbi->block_size < MFS_MIN_BLOCKSIZE ||
1089 sbi->block_size > MFS_MAX_BLOCKSIZE)
1090 return ENOTSUP;
1091 else if (sbi->ibmap_blocks == 0 || sbi->zbmap_blocks == 0)
1092 return ENOTSUP;
1093 else if (sbi->ninodes == 0 || sbi->nzones == 0)
1094 return ENOTSUP;
1095 else if (sbi->firstdatazone == 0)
1096 return ENOTSUP;
1097
1098 return EOK;
1099}
1100
1101static int
1102mfs_close(service_id_t service_id, fs_index_t index)
1103{
1104 return 0;
1105}
1106
1107static int
1108mfs_sync(service_id_t service_id, fs_index_t index)
1109{
1110 fs_node_t *fn;
1111 int rc = mfs_node_get(&fn, service_id, index);
1112 if (rc != EOK)
1113 return rc;
1114 if (!fn)
1115 return ENOENT;
1116
1117 struct mfs_node *mnode = fn->data;
1118 mnode->ino_i->dirty = true;
1119
1120 return mfs_node_put(fn);
1121}
1122
1123/** Check if a given number is a power of two.
1124 *
1125 * @param n The number to check.
1126 *
1127 * @return true if it is a power of two, false otherwise.
1128 */
1129static bool
1130is_power_of_two(uint32_t n)
1131{
1132 if (n == 0)
1133 return false;
1134
1135 return (n & (n - 1)) == 0;
1136}
1137
1138vfs_out_ops_t mfs_ops = {
1139 .mounted = mfs_mounted,
1140 .unmounted = mfs_unmounted,
1141 .read = mfs_read,
1142 .write = mfs_write,
1143 .truncate = mfs_truncate,
1144 .close = mfs_close,
1145 .destroy = mfs_destroy,
1146 .sync = mfs_sync,
1147};
1148
1149/**
1150 * @}
1151 */
1152
Note: See TracBrowser for help on using the repository browser.