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

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

Merge mainline changes

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