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