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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 38fc00b was 4e00f87, checked in by Jakub Jermar <jakub@…>, 13 years ago

Use NULL instead of 0 as a hash_table_ops_t member initializer.

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