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

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

Cleaning error path in mfs_mounted()

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