| [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] | 41 | static bool check_magic_number(uint16_t magic, bool *native,
|
|---|
| [6d4d883] | 42 | mfs_version_t *version, bool *longfilenames);
|
|---|
| [44c0f5b] | 43 | static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
|
|---|
| [6d4d883] | 44 | fs_index_t index);
|
|---|
| [41202a9] | 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);
|
|---|
| [ac28650] | 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);
|
|---|
| [03bc76a] | 52 | static int mfs_root_get(fs_node_t **rfn, service_id_t service_id);
|
|---|
| [b33870b] | 53 | static service_id_t mfs_service_get(fs_node_t *fsnode);
|
|---|
| [ac28650] | 54 | static aoff64_t mfs_size_get(fs_node_t *node);
|
|---|
| [bd64680] | 55 | static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component);
|
|---|
| [03bc76a] | 56 | static int mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags);
|
|---|
| [88ccd8b8] | 57 | static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name);
|
|---|
| [53eb588] | 58 | static int mfs_unlink(fs_node_t *, fs_node_t *, const char *name);
|
|---|
| [d8af1bd] | 59 | static int mfs_destroy_node(fs_node_t *fn);
|
|---|
| [03bc76a] | 60 | static int mfs_node_get(fs_node_t **rfn, service_id_t service_id,
|
|---|
| [6d4d883] | 61 | fs_index_t index);
|
|---|
| 62 | static int mfs_instance_get(service_id_t service_id,
|
|---|
| 63 | struct mfs_instance **instance);
|
|---|
| [df3caec5] | 64 | static int mfs_check_sanity(struct mfs_sb_info *sbi);
|
|---|
| 65 | static bool is_power_of_two(uint32_t n);
|
|---|
| [41202a9] | 66 |
|
|---|
| [ee257b2] | 67 | static hash_table_t open_nodes;
|
|---|
| 68 | static FIBRIL_MUTEX_INITIALIZE(open_nodes_lock);
|
|---|
| [953a823] | 69 |
|
|---|
| [e54ba607] | 70 | libfs_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 | typedef struct {
|
|---|
| 91 | service_id_t service_id;
|
|---|
| 92 | fs_index_t index;
|
|---|
| 93 | } node_key_t;
|
|---|
| 94 |
|
|---|
| 95 | static size_t
|
|---|
| 96 | open_nodes_key_hash(void *key)
|
|---|
| [ee257b2] | 97 | {
|
|---|
| [062d900] | 98 | node_key_t *node_key = (node_key_t*)key;
|
|---|
| 99 | return hash_combine(node_key->service_id, node_key->index);
|
|---|
| [ee257b2] | 100 | }
|
|---|
| 101 |
|
|---|
| [062d900] | 102 | static size_t
|
|---|
| 103 | open_nodes_hash(const ht_link_t *item)
|
|---|
| [ee257b2] | 104 | {
|
|---|
| [062d900] | 105 | struct mfs_node *m = hash_table_get_inst(item, struct mfs_node, link);
|
|---|
| 106 | return hash_combine(m->instance->service_id, m->ino_i->index);
|
|---|
| [ee257b2] | 107 | }
|
|---|
| 108 |
|
|---|
| [062d900] | 109 | static bool
|
|---|
| 110 | open_nodes_key_equal(void *key, const ht_link_t *item)
|
|---|
| [ee257b2] | 111 | {
|
|---|
| [062d900] | 112 | node_key_t *node_key = (node_key_t*)key;
|
|---|
| 113 | struct mfs_node *mnode = hash_table_get_inst(item, struct mfs_node, link);
|
|---|
| 114 |
|
|---|
| 115 | return node_key->service_id == mnode->instance->service_id
|
|---|
| 116 | && node_key->index == mnode->ino_i->index;
|
|---|
| [ee257b2] | 117 | }
|
|---|
| 118 |
|
|---|
| [062d900] | 119 | static hash_table_ops_t open_nodes_ops = {
|
|---|
| [ee257b2] | 120 | .hash = open_nodes_hash,
|
|---|
| [062d900] | 121 | .key_hash = open_nodes_key_hash,
|
|---|
| 122 | .key_equal = open_nodes_key_equal,
|
|---|
| [4e00f87] | 123 | .equal = NULL,
|
|---|
| 124 | .remove_callback = NULL,
|
|---|
| [ee257b2] | 125 | };
|
|---|
| 126 |
|
|---|
| [6d4d883] | 127 | int
|
|---|
| 128 | mfs_global_init(void)
|
|---|
| [ee257b2] | 129 | {
|
|---|
| [062d900] | 130 | if (!hash_table_create(&open_nodes, 0, 0, &open_nodes_ops)) {
|
|---|
| [ee257b2] | 131 | return ENOMEM;
|
|---|
| 132 | }
|
|---|
| 133 | return EOK;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| [03bc76a] | 136 | static int
|
|---|
| 137 | mfs_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
|
|---|
| [6d4d883] | 138 | aoff64_t *size, unsigned *linkcnt)
|
|---|
| [096c8835] | 139 | {
|
|---|
| [44c6091f] | 140 | enum cache_mode cmode;
|
|---|
| [f456ab2] | 141 | struct mfs_superblock *sb = NULL;
|
|---|
| 142 | struct mfs3_superblock *sb3 = NULL;
|
|---|
| 143 | struct mfs_sb_info *sbi = NULL;
|
|---|
| 144 | struct mfs_instance *instance = NULL;
|
|---|
| [8ceba1e] | 145 | bool native, longnames;
|
|---|
| [9e3dc95] | 146 | mfs_version_t version;
|
|---|
| [245eb02d] | 147 | uint16_t magic;
|
|---|
| [03bc76a] | 148 | int rc;
|
|---|
| [096c8835] | 149 |
|
|---|
| 150 | /* Check for option enabling write through. */
|
|---|
| 151 | if (str_cmp(opts, "wtcache") == 0)
|
|---|
| 152 | cmode = CACHE_MODE_WT;
|
|---|
| 153 | else
|
|---|
| 154 | cmode = CACHE_MODE_WB;
|
|---|
| 155 |
|
|---|
| 156 | /* initialize libblock */
|
|---|
| [bbd4c72] | 157 | rc = block_init(EXCHANGE_SERIALIZE, service_id, 4096);
|
|---|
| [03bc76a] | 158 | if (rc != EOK)
|
|---|
| 159 | return rc;
|
|---|
| [096c8835] | 160 |
|
|---|
| [6d4d883] | 161 | /* Allocate space for generic MFS superblock */
|
|---|
| [b438804] | 162 | sbi = malloc(sizeof(*sbi));
|
|---|
| [953a823] | 163 | if (!sbi) {
|
|---|
| [f456ab2] | 164 | rc = ENOMEM;
|
|---|
| 165 | goto out_error;
|
|---|
| [953a823] | 166 | }
|
|---|
| 167 |
|
|---|
| [6d4d883] | 168 | /* Allocate space for filesystem instance */
|
|---|
| [b438804] | 169 | instance = malloc(sizeof(*instance));
|
|---|
| [953a823] | 170 | if (!instance) {
|
|---|
| [f456ab2] | 171 | rc = ENOMEM;
|
|---|
| 172 | goto out_error;
|
|---|
| [953a823] | 173 | }
|
|---|
| 174 |
|
|---|
| [b438804] | 175 | sb = malloc(MFS_SUPERBLOCK_SIZE);
|
|---|
| [953a823] | 176 | if (!sb) {
|
|---|
| [f456ab2] | 177 | rc = ENOMEM;
|
|---|
| 178 | goto out_error;
|
|---|
| [953a823] | 179 | }
|
|---|
| [92dd5c8] | 180 |
|
|---|
| 181 | /* Read the superblock */
|
|---|
| [e03d545] | 182 | rc = block_read_direct(service_id, MFS_SUPERBLOCK << 1, 2, sb);
|
|---|
| [f456ab2] | 183 | if (rc != EOK)
|
|---|
| 184 | goto out_error;
|
|---|
| [096c8835] | 185 |
|
|---|
| [953a823] | 186 | sb3 = (struct mfs3_superblock *) sb;
|
|---|
| 187 |
|
|---|
| 188 | if (check_magic_number(sb->s_magic, &native, &version, &longnames)) {
|
|---|
| [6d4d883] | 189 | /* This is a V1 or V2 Minix filesystem */
|
|---|
| [953a823] | 190 | magic = sb->s_magic;
|
|---|
| [7769ec9] | 191 | } else if (check_magic_number(sb3->s_magic, &native,
|
|---|
| 192 | &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 |
|
|---|
| 296 | out_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] | 307 | static int
|
|---|
| 308 | mfs_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] | 331 | service_id_t
|
|---|
| 332 | mfs_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] | 338 | static int
|
|---|
| 339 | mfs_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 |
|
|---|
| [03bc76a] | 347 | r = mfs_instance_get(service_id, &inst);
|
|---|
| [c699b0c] | 348 | if (r != EOK)
|
|---|
| 349 | return r;
|
|---|
| [88ccd8b8] | 350 |
|
|---|
| [6d4d883] | 351 | /* Alloc a new inode */
|
|---|
| [70ac0af] | 352 | r = mfs_alloc_inode(inst, &inum);
|
|---|
| [c699b0c] | 353 | if (r != EOK)
|
|---|
| 354 | return r;
|
|---|
| [88ccd8b8] | 355 |
|
|---|
| 356 | struct mfs_ino_info *ino_i;
|
|---|
| 357 |
|
|---|
| 358 | ino_i = malloc(sizeof(*ino_i));
|
|---|
| 359 | if (!ino_i) {
|
|---|
| 360 | r = ENOMEM;
|
|---|
| 361 | goto out_err;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | mnode = malloc(sizeof(*mnode));
|
|---|
| 365 | if (!mnode) {
|
|---|
| 366 | r = ENOMEM;
|
|---|
| 367 | goto out_err_1;
|
|---|
| 368 | }
|
|---|
| [44c6091f] | 369 |
|
|---|
| [88ccd8b8] | 370 | fsnode = malloc(sizeof(fs_node_t));
|
|---|
| 371 | if (!fsnode) {
|
|---|
| 372 | r = ENOMEM;
|
|---|
| 373 | goto out_err_2;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| [bbd4c72] | 376 | if (flags & L_DIRECTORY) {
|
|---|
| [13ecdac9] | 377 | ino_i->i_mode = S_IFDIR;
|
|---|
| [4c3ad56] | 378 | ino_i->i_nlinks = 1; /* This accounts for the '.' dentry */
|
|---|
| [1ba6651] | 379 | } else {
|
|---|
| [13ecdac9] | 380 | ino_i->i_mode = S_IFREG;
|
|---|
| [1ba6651] | 381 | ino_i->i_nlinks = 0;
|
|---|
| 382 | }
|
|---|
| [13ecdac9] | 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 |
|
|---|
| 416 | out_err_2:
|
|---|
| 417 | free(mnode);
|
|---|
| 418 | out_err_1:
|
|---|
| 419 | free(ino_i);
|
|---|
| 420 | out_err:
|
|---|
| [64e63ce1] | 421 | mfs_free_inode(inst, inum);
|
|---|
| [88ccd8b8] | 422 | return r;
|
|---|
| [10eb754] | 423 | }
|
|---|
| 424 |
|
|---|
| [6d4d883] | 425 | static int
|
|---|
| 426 | mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| [bd64680] | 427 | {
|
|---|
| 428 | struct mfs_node *mnode = pfn->data;
|
|---|
| 429 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| [e80c2ff] | 430 | struct mfs_dentry_info d_info;
|
|---|
| [488f7ed] | 431 | int r;
|
|---|
| [bd64680] | 432 |
|
|---|
| 433 | if (!S_ISDIR(ino_i->i_mode))
|
|---|
| 434 | return ENOTDIR;
|
|---|
| 435 |
|
|---|
| 436 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 437 | const size_t comp_size = str_size(component);
|
|---|
| 438 |
|
|---|
| [e80c2ff] | 439 | unsigned i;
|
|---|
| 440 | for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
|
|---|
| [3a5ee6c] | 441 | r = mfs_read_dentry(mnode, &d_info, i);
|
|---|
| [c699b0c] | 442 | if (r != EOK)
|
|---|
| 443 | return r;
|
|---|
| [488f7ed] | 444 |
|
|---|
| [e80c2ff] | 445 | if (!d_info.d_inum) {
|
|---|
| [6d4d883] | 446 | /* This entry is not used */
|
|---|
| [bd64680] | 447 | continue;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| [367d67fe] | 450 | const size_t dentry_name_size = str_size(d_info.d_name);
|
|---|
| 451 |
|
|---|
| 452 | if (comp_size == dentry_name_size &&
|
|---|
| [44ecf89] | 453 | memcmp(component, d_info.d_name, dentry_name_size) == 0) {
|
|---|
| [6d4d883] | 454 | /* Hit! */
|
|---|
| [bd64680] | 455 | mfs_node_core_get(rfn, mnode->instance,
|
|---|
| [6d4d883] | 456 | d_info.d_inum);
|
|---|
| [bd64680] | 457 | goto found;
|
|---|
| 458 | }
|
|---|
| 459 | }
|
|---|
| 460 | *rfn = NULL;
|
|---|
| 461 | found:
|
|---|
| 462 | return EOK;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| [6d4d883] | 465 | static aoff64_t
|
|---|
| 466 | mfs_size_get(fs_node_t *node)
|
|---|
| [0d6ab10] | 467 | {
|
|---|
| 468 | const struct mfs_node *mnode = node->data;
|
|---|
| [155f792] | 469 | return mnode->ino_i->i_size;
|
|---|
| [0d6ab10] | 470 | }
|
|---|
| 471 |
|
|---|
| [03bc76a] | 472 | static int
|
|---|
| 473 | mfs_node_get(fs_node_t **rfn, service_id_t service_id,
|
|---|
| [6d4d883] | 474 | fs_index_t index)
|
|---|
| [44c0f5b] | 475 | {
|
|---|
| 476 | int rc;
|
|---|
| 477 | struct mfs_instance *instance;
|
|---|
| 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] | 486 | static int
|
|---|
| 487 | mfs_node_put(fs_node_t *fsnode)
|
|---|
| [41202a9] | 488 | {
|
|---|
| [ee257b2] | 489 | int rc = EOK;
|
|---|
| [41202a9] | 490 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 491 |
|
|---|
| [ee257b2] | 492 | fibril_mutex_lock(&open_nodes_lock);
|
|---|
| 493 |
|
|---|
| 494 | assert(mnode->refcnt > 0);
|
|---|
| 495 | mnode->refcnt--;
|
|---|
| 496 | if (mnode->refcnt == 0) {
|
|---|
| [062d900] | 497 | hash_table_remove_item(&open_nodes, &mnode->link);
|
|---|
| [ee257b2] | 498 | assert(mnode->instance->open_nodes_cnt > 0);
|
|---|
| 499 | mnode->instance->open_nodes_cnt--;
|
|---|
| [5f509cc] | 500 | rc = mfs_put_inode(mnode);
|
|---|
| [ee257b2] | 501 | free(mnode->ino_i);
|
|---|
| 502 | free(mnode);
|
|---|
| 503 | free(fsnode);
|
|---|
| 504 | }
|
|---|
| [41202a9] | 505 |
|
|---|
| [ee257b2] | 506 | fibril_mutex_unlock(&open_nodes_lock);
|
|---|
| 507 | return rc;
|
|---|
| [41202a9] | 508 | }
|
|---|
| 509 |
|
|---|
| [6d4d883] | 510 | static int
|
|---|
| 511 | mfs_node_open(fs_node_t *fsnode)
|
|---|
| [41202a9] | 512 | {
|
|---|
| 513 | /*
|
|---|
| 514 | * Opening a file is stateless, nothing
|
|---|
| 515 | * to be done here.
|
|---|
| 516 | */
|
|---|
| 517 | return EOK;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| [6d4d883] | 520 | static fs_index_t
|
|---|
| 521 | mfs_index_get(fs_node_t *fsnode)
|
|---|
| [41202a9] | 522 | {
|
|---|
| 523 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 524 | return mnode->ino_i->index;
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| [6d4d883] | 527 | static unsigned
|
|---|
| 528 | mfs_lnkcnt_get(fs_node_t *fsnode)
|
|---|
| [41202a9] | 529 | {
|
|---|
| 530 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 531 |
|
|---|
| [75e0f15] | 532 | mfsdebug("%s() %d\n", __FUNCTION__, mnode->ino_i->i_nlinks);
|
|---|
| [ee257b2] | 533 |
|
|---|
| [bbd4c72] | 534 | if (S_ISDIR(mnode->ino_i->i_mode)) {
|
|---|
| 535 | if (mnode->ino_i->i_nlinks > 1)
|
|---|
| 536 | return 1;
|
|---|
| 537 | else
|
|---|
| 538 | return 0;
|
|---|
| [75e0f15] | 539 | } else
|
|---|
| 540 | return mnode->ino_i->i_nlinks;
|
|---|
| [41202a9] | 541 | }
|
|---|
| 542 |
|
|---|
| [6d4d883] | 543 | static int
|
|---|
| 544 | mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
|
|---|
| 545 | fs_index_t index)
|
|---|
| [44c0f5b] | 546 | {
|
|---|
| 547 | fs_node_t *node = NULL;
|
|---|
| 548 | struct mfs_node *mnode = NULL;
|
|---|
| 549 | int rc;
|
|---|
| 550 |
|
|---|
| [ee257b2] | 551 | fibril_mutex_lock(&open_nodes_lock);
|
|---|
| 552 |
|
|---|
| 553 | /* Check if the node is not already open */
|
|---|
| [062d900] | 554 | node_key_t key = {
|
|---|
| 555 | .service_id = inst->service_id,
|
|---|
| 556 | .index = index
|
|---|
| [ee257b2] | 557 | };
|
|---|
| [062d900] | 558 |
|
|---|
| 559 | ht_link_t *already_open = hash_table_find(&open_nodes, &key);
|
|---|
| [ee257b2] | 560 |
|
|---|
| 561 | if (already_open) {
|
|---|
| [062d900] | 562 | mnode = hash_table_get_inst(already_open, struct mfs_node, link);
|
|---|
| [4c53333] | 563 |
|
|---|
| [ee257b2] | 564 | *rfn = mnode->fsnode;
|
|---|
| 565 | mnode->refcnt++;
|
|---|
| 566 |
|
|---|
| 567 | fibril_mutex_unlock(&open_nodes_lock);
|
|---|
| 568 | return EOK;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| [b438804] | 571 | node = malloc(sizeof(fs_node_t));
|
|---|
| [44c0f5b] | 572 | if (!node) {
|
|---|
| 573 | rc = ENOMEM;
|
|---|
| 574 | goto out_err;
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | fs_node_initialize(node);
|
|---|
| 578 |
|
|---|
| [b438804] | 579 | mnode = malloc(sizeof(*mnode));
|
|---|
| [44c0f5b] | 580 | if (!mnode) {
|
|---|
| 581 | rc = ENOMEM;
|
|---|
| 582 | goto out_err;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| [155f792] | 585 | struct mfs_ino_info *ino_i;
|
|---|
| 586 |
|
|---|
| [3a5ee6c] | 587 | rc = mfs_get_inode(inst, &ino_i, index);
|
|---|
| [c699b0c] | 588 | if (rc != EOK)
|
|---|
| 589 | goto out_err;
|
|---|
| [155f792] | 590 |
|
|---|
| [41202a9] | 591 | ino_i->index = index;
|
|---|
| [155f792] | 592 | mnode->ino_i = ino_i;
|
|---|
| [ee257b2] | 593 | mnode->refcnt = 1;
|
|---|
| [155f792] | 594 |
|
|---|
| [44c0f5b] | 595 | mnode->instance = inst;
|
|---|
| 596 | node->data = mnode;
|
|---|
| [ee257b2] | 597 | mnode->fsnode = node;
|
|---|
| [44c0f5b] | 598 | *rfn = node;
|
|---|
| 599 |
|
|---|
| [062d900] | 600 | hash_table_insert(&open_nodes, &mnode->link);
|
|---|
| [ee257b2] | 601 | inst->open_nodes_cnt++;
|
|---|
| 602 |
|
|---|
| 603 | fibril_mutex_unlock(&open_nodes_lock);
|
|---|
| 604 |
|
|---|
| [44c0f5b] | 605 | return EOK;
|
|---|
| 606 |
|
|---|
| 607 | out_err:
|
|---|
| 608 | if (node)
|
|---|
| 609 | free(node);
|
|---|
| 610 | if (mnode)
|
|---|
| 611 | free(mnode);
|
|---|
| [ee257b2] | 612 | fibril_mutex_unlock(&open_nodes_lock);
|
|---|
| [44c0f5b] | 613 | return rc;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| [6d4d883] | 616 | static bool
|
|---|
| 617 | mfs_is_directory(fs_node_t *fsnode)
|
|---|
| [5a58ae2] | 618 | {
|
|---|
| [44c0f5b] | 619 | const struct mfs_node *node = fsnode->data;
|
|---|
| [155f792] | 620 | return S_ISDIR(node->ino_i->i_mode);
|
|---|
| [5a58ae2] | 621 | }
|
|---|
| 622 |
|
|---|
| [6d4d883] | 623 | static bool
|
|---|
| 624 | mfs_is_file(fs_node_t *fsnode)
|
|---|
| [7d04324] | 625 | {
|
|---|
| 626 | struct mfs_node *node = fsnode->data;
|
|---|
| [155f792] | 627 | return S_ISREG(node->ino_i->i_mode);
|
|---|
| [7d04324] | 628 | }
|
|---|
| 629 |
|
|---|
| [6d4d883] | 630 | static int
|
|---|
| 631 | 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 |
|
|---|
| [6d4d883] | 637 | static int
|
|---|
| 638 | mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
|---|
| [88ccd8b8] | 639 | {
|
|---|
| [afd9c3b] | 640 | struct mfs_node *parent = pfn->data;
|
|---|
| 641 | struct mfs_node *child = cfn->data;
|
|---|
| [48de019] | 642 | struct mfs_sb_info *sbi = parent->instance->sbi;
|
|---|
| [48902fa] | 643 | bool destroy_dentry = false;
|
|---|
| [48de019] | 644 |
|
|---|
| 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)
|
|---|
| [48902fa] | 650 | return r;
|
|---|
| [4c3ad56] | 651 |
|
|---|
| [13ecdac9] | 652 | if (S_ISDIR(child->ino_i->i_mode)) {
|
|---|
| [48902fa] | 653 | if (child->ino_i->i_nlinks != 1) {
|
|---|
| 654 | /* It's not possible to hardlink directories in MFS */
|
|---|
| 655 | destroy_dentry = true;
|
|---|
| 656 | r = EMLINK;
|
|---|
| 657 | goto exit;
|
|---|
| 658 | }
|
|---|
| [3a5ee6c] | 659 | r = mfs_insert_dentry(child, ".", child->ino_i->index);
|
|---|
| [48902fa] | 660 | if (r != EOK) {
|
|---|
| 661 | destroy_dentry = true;
|
|---|
| 662 | goto exit;
|
|---|
| 663 | }
|
|---|
| [bbd4c72] | 664 |
|
|---|
| [3a5ee6c] | 665 | r = mfs_insert_dentry(child, "..", parent->ino_i->index);
|
|---|
| [48902fa] | 666 | if (r != EOK) {
|
|---|
| [64e63ce1] | 667 | mfs_remove_dentry(child, ".");
|
|---|
| [48902fa] | 668 | destroy_dentry = true;
|
|---|
| 669 | goto exit;
|
|---|
| 670 | }
|
|---|
| [bbd4c72] | 671 |
|
|---|
| 672 | parent->ino_i->i_nlinks++;
|
|---|
| 673 | parent->ino_i->dirty = true;
|
|---|
| [13ecdac9] | 674 | }
|
|---|
| [afd9c3b] | 675 |
|
|---|
| [48902fa] | 676 | exit:
|
|---|
| 677 | if (destroy_dentry) {
|
|---|
| 678 | int r2 = mfs_remove_dentry(parent, name);
|
|---|
| 679 | if (r2 != EOK)
|
|---|
| 680 | r = r2;
|
|---|
| 681 | } else {
|
|---|
| 682 | child->ino_i->i_nlinks++;
|
|---|
| 683 | child->ino_i->dirty = true;
|
|---|
| 684 | }
|
|---|
| [afd9c3b] | 685 | return r;
|
|---|
| [88ccd8b8] | 686 | }
|
|---|
| 687 |
|
|---|
| [53eb588] | 688 | static int
|
|---|
| 689 | mfs_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
|---|
| 690 | {
|
|---|
| 691 | struct mfs_node *parent = pfn->data;
|
|---|
| 692 | struct mfs_node *child = cfn->data;
|
|---|
| 693 | bool has_children;
|
|---|
| 694 | int r;
|
|---|
| 695 |
|
|---|
| 696 | if (!parent)
|
|---|
| 697 | return EBUSY;
|
|---|
| 698 |
|
|---|
| 699 | r = mfs_has_children(&has_children, cfn);
|
|---|
| [c699b0c] | 700 | if (r != EOK)
|
|---|
| 701 | return r;
|
|---|
| [3f3e5b5] | 702 |
|
|---|
| [53eb588] | 703 | if (has_children)
|
|---|
| 704 | return ENOTEMPTY;
|
|---|
| 705 |
|
|---|
| [3a5ee6c] | 706 | r = mfs_remove_dentry(parent, name);
|
|---|
| [c699b0c] | 707 | if (r != EOK)
|
|---|
| 708 | return r;
|
|---|
| [53eb588] | 709 |
|
|---|
| 710 | struct mfs_ino_info *chino = child->ino_i;
|
|---|
| 711 |
|
|---|
| 712 | assert(chino->i_nlinks >= 1);
|
|---|
| [bbd4c72] | 713 | chino->i_nlinks--;
|
|---|
| 714 | mfsdebug("Links: %d\n", chino->i_nlinks);
|
|---|
| 715 |
|
|---|
| 716 | if (chino->i_nlinks <= 1 && S_ISDIR(chino->i_mode)) {
|
|---|
| 717 | /* The child directory will be destroyed, decrease the
|
|---|
| 718 | * parent hard links counter.
|
|---|
| 719 | */
|
|---|
| [ee257b2] | 720 | parent->ino_i->i_nlinks--;
|
|---|
| 721 | parent->ino_i->dirty = true;
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| [53eb588] | 724 | chino->dirty = true;
|
|---|
| 725 |
|
|---|
| [ee257b2] | 726 | return r;
|
|---|
| [53eb588] | 727 | }
|
|---|
| 728 |
|
|---|
| [6d4d883] | 729 | static int
|
|---|
| 730 | mfs_has_children(bool *has_children, fs_node_t *fsnode)
|
|---|
| [54caa41b] | 731 | {
|
|---|
| 732 | struct mfs_node *mnode = fsnode->data;
|
|---|
| [e80c2ff] | 733 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| [488f7ed] | 734 | int r;
|
|---|
| [54caa41b] | 735 |
|
|---|
| 736 | *has_children = false;
|
|---|
| 737 |
|
|---|
| 738 | if (!S_ISDIR(mnode->ino_i->i_mode))
|
|---|
| 739 | goto out;
|
|---|
| 740 |
|
|---|
| [e80c2ff] | 741 | struct mfs_dentry_info d_info;
|
|---|
| [44c6091f] | 742 |
|
|---|
| [ac28650] | 743 | /* The first two dentries are always . and .. */
|
|---|
| [e80c2ff] | 744 | unsigned i;
|
|---|
| [3f3e5b5] | 745 | for (i = 2; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
|
|---|
| [3a5ee6c] | 746 | r = mfs_read_dentry(mnode, &d_info, i);
|
|---|
| [c699b0c] | 747 | if (r != EOK)
|
|---|
| 748 | return r;
|
|---|
| [54caa41b] | 749 |
|
|---|
| [e80c2ff] | 750 | if (d_info.d_inum) {
|
|---|
| [6d4d883] | 751 | /* A valid entry has been found */
|
|---|
| [54caa41b] | 752 | *has_children = true;
|
|---|
| 753 | break;
|
|---|
| 754 | }
|
|---|
| 755 | }
|
|---|
| [41202a9] | 756 | out:
|
|---|
| 757 |
|
|---|
| [54caa41b] | 758 | return EOK;
|
|---|
| 759 | }
|
|---|
| 760 |
|
|---|
| [03bc76a] | 761 | static int
|
|---|
| 762 | mfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
|
|---|
| [6d4d883] | 763 | size_t *rbytes)
|
|---|
| [668f1949] | 764 | {
|
|---|
| 765 | int rc;
|
|---|
| [5b56dc7] | 766 | fs_node_t *fn = NULL;
|
|---|
| [668f1949] | 767 |
|
|---|
| [03bc76a] | 768 | rc = mfs_node_get(&fn, service_id, index);
|
|---|
| 769 | if (rc != EOK)
|
|---|
| 770 | return rc;
|
|---|
| 771 | if (!fn)
|
|---|
| 772 | return ENOENT;
|
|---|
| [668f1949] | 773 |
|
|---|
| 774 | struct mfs_node *mnode;
|
|---|
| 775 | struct mfs_ino_info *ino_i;
|
|---|
| 776 | size_t len, bytes = 0;
|
|---|
| 777 | ipc_callid_t callid;
|
|---|
| 778 |
|
|---|
| 779 | mnode = fn->data;
|
|---|
| 780 | ino_i = mnode->ino_i;
|
|---|
| 781 |
|
|---|
| 782 | if (!async_data_read_receive(&callid, &len)) {
|
|---|
| [230229de] | 783 | rc = EINVAL;
|
|---|
| 784 | goto out_error;
|
|---|
| [668f1949] | 785 | }
|
|---|
| 786 |
|
|---|
| 787 | if (S_ISDIR(ino_i->i_mode)) {
|
|---|
| 788 | aoff64_t spos = pos;
|
|---|
| [e80c2ff] | 789 | struct mfs_dentry_info d_info;
|
|---|
| 790 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| [668f1949] | 791 |
|
|---|
| [e8b6b6a] | 792 | if (pos < 2) {
|
|---|
| [6d4d883] | 793 | /* Skip the first two dentries ('.' and '..') */
|
|---|
| [e8b6b6a] | 794 | pos = 2;
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| [3f3e5b5] | 797 | for (; pos < mnode->ino_i->i_size / sbi->dirsize; ++pos) {
|
|---|
| [3a5ee6c] | 798 | rc = mfs_read_dentry(mnode, &d_info, pos);
|
|---|
| [c699b0c] | 799 | if (rc != EOK)
|
|---|
| 800 | goto out_error;
|
|---|
| [488f7ed] | 801 |
|
|---|
| [e80c2ff] | 802 | if (d_info.d_inum) {
|
|---|
| [6d4d883] | 803 | /* Dentry found! */
|
|---|
| [668f1949] | 804 | goto found;
|
|---|
| 805 | }
|
|---|
| 806 | }
|
|---|
| 807 |
|
|---|
| 808 | rc = mfs_node_put(fn);
|
|---|
| 809 | async_answer_0(callid, rc != EOK ? rc : ENOENT);
|
|---|
| [03bc76a] | 810 | return rc;
|
|---|
| [668f1949] | 811 | found:
|
|---|
| [e80c2ff] | 812 | async_data_read_finalize(callid, d_info.d_name,
|
|---|
| [c2e50d7] | 813 | str_size(d_info.d_name) + 1);
|
|---|
| [668f1949] | 814 | bytes = ((pos - spos) + 1);
|
|---|
| [230229de] | 815 | } else {
|
|---|
| 816 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 817 |
|
|---|
| 818 | if (pos >= (size_t) ino_i->i_size) {
|
|---|
| [6d4d883] | 819 | /* Trying to read beyond the end of file */
|
|---|
| [230229de] | 820 | bytes = 0;
|
|---|
| 821 | (void) async_data_read_finalize(callid, NULL, 0);
|
|---|
| 822 | goto out_success;
|
|---|
| 823 | }
|
|---|
| 824 |
|
|---|
| 825 | bytes = min(len, sbi->block_size - pos % sbi->block_size);
|
|---|
| 826 | bytes = min(bytes, ino_i->i_size - pos);
|
|---|
| 827 |
|
|---|
| 828 | uint32_t zone;
|
|---|
| 829 | block_t *b;
|
|---|
| [668f1949] | 830 |
|
|---|
| [3a5ee6c] | 831 | rc = mfs_read_map(&zone, mnode, pos);
|
|---|
| [c699b0c] | 832 | if (rc != EOK)
|
|---|
| 833 | goto out_error;
|
|---|
| [230229de] | 834 |
|
|---|
| 835 | if (zone == 0) {
|
|---|
| [6d4d883] | 836 | /* sparse file */
|
|---|
| [230229de] | 837 | uint8_t *buf = malloc(sbi->block_size);
|
|---|
| 838 | if (!buf) {
|
|---|
| 839 | rc = ENOMEM;
|
|---|
| 840 | goto out_error;
|
|---|
| 841 | }
|
|---|
| [6fc5262] | 842 | memset(buf, 0, sizeof(sbi->block_size));
|
|---|
| [230229de] | 843 | async_data_read_finalize(callid,
|
|---|
| [6d4d883] | 844 | buf + pos % sbi->block_size, bytes);
|
|---|
| [230229de] | 845 | free(buf);
|
|---|
| 846 | goto out_success;
|
|---|
| 847 | }
|
|---|
| 848 |
|
|---|
| [03bc76a] | 849 | rc = block_get(&b, service_id, zone, BLOCK_FLAGS_NONE);
|
|---|
| [c699b0c] | 850 | if (rc != EOK)
|
|---|
| 851 | goto out_error;
|
|---|
| [230229de] | 852 |
|
|---|
| 853 | async_data_read_finalize(callid, b->data +
|
|---|
| [6d4d883] | 854 | pos % sbi->block_size, bytes);
|
|---|
| [230229de] | 855 |
|
|---|
| 856 | rc = block_put(b);
|
|---|
| 857 | if (rc != EOK) {
|
|---|
| 858 | mfs_node_put(fn);
|
|---|
| [03bc76a] | 859 | return rc;
|
|---|
| [230229de] | 860 | }
|
|---|
| 861 | }
|
|---|
| 862 | out_success:
|
|---|
| [668f1949] | 863 | rc = mfs_node_put(fn);
|
|---|
| [03bc76a] | 864 | *rbytes = bytes;
|
|---|
| 865 | return rc;
|
|---|
| [44c6091f] | 866 | out_error:
|
|---|
| 867 | ;
|
|---|
| [230229de] | 868 | int tmp = mfs_node_put(fn);
|
|---|
| 869 | async_answer_0(callid, tmp != EOK ? tmp : rc);
|
|---|
| [03bc76a] | 870 | return tmp != EOK ? tmp : rc;
|
|---|
| [668f1949] | 871 | }
|
|---|
| 872 |
|
|---|
| [03bc76a] | 873 | static int
|
|---|
| 874 | mfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
|
|---|
| [6d4d883] | 875 | size_t *wbytes, aoff64_t *nsize)
|
|---|
| [bb7e8382] | 876 | {
|
|---|
| 877 | fs_node_t *fn;
|
|---|
| 878 | int r;
|
|---|
| 879 | int flags = BLOCK_FLAGS_NONE;
|
|---|
| 880 |
|
|---|
| [03bc76a] | 881 | r = mfs_node_get(&fn, service_id, index);
|
|---|
| 882 | if (r != EOK)
|
|---|
| 883 | return r;
|
|---|
| 884 | if (!fn)
|
|---|
| 885 | return ENOENT;
|
|---|
| [bb7e8382] | 886 |
|
|---|
| 887 | ipc_callid_t callid;
|
|---|
| 888 | size_t len;
|
|---|
| 889 |
|
|---|
| 890 | if (!async_data_write_receive(&callid, &len)) {
|
|---|
| 891 | r = EINVAL;
|
|---|
| 892 | goto out_err;
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | struct mfs_node *mnode = fn->data;
|
|---|
| 896 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 897 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| 898 | const size_t bs = sbi->block_size;
|
|---|
| [36cb22f] | 899 | size_t bytes = min(len, bs - (pos % bs));
|
|---|
| [bb7e8382] | 900 | uint32_t block;
|
|---|
| 901 |
|
|---|
| 902 | if (bytes == bs)
|
|---|
| 903 | flags = BLOCK_FLAGS_NOREAD;
|
|---|
| 904 |
|
|---|
| [36cb22f] | 905 | r = mfs_read_map(&block, mnode, pos);
|
|---|
| 906 | if (r != EOK)
|
|---|
| 907 | goto out_err;
|
|---|
| [bb7e8382] | 908 |
|
|---|
| [36cb22f] | 909 | if (block == 0) {
|
|---|
| [bb7e8382] | 910 | uint32_t dummy;
|
|---|
| 911 |
|
|---|
| [70ac0af] | 912 | r = mfs_alloc_zone(mnode->instance, &block);
|
|---|
| [c699b0c] | 913 | if (r != EOK)
|
|---|
| 914 | goto out_err;
|
|---|
| [36cb22f] | 915 |
|
|---|
| [3a5ee6c] | 916 | r = mfs_write_map(mnode, pos, block, &dummy);
|
|---|
| [64e63ce1] | 917 | if (r != EOK) {
|
|---|
| 918 | mfs_free_zone(mnode->instance, block);
|
|---|
| [c699b0c] | 919 | goto out_err;
|
|---|
| [64e63ce1] | 920 | }
|
|---|
| [36cb22f] | 921 |
|
|---|
| 922 | flags = BLOCK_FLAGS_NOREAD;
|
|---|
| [bb7e8382] | 923 | }
|
|---|
| 924 |
|
|---|
| 925 | block_t *b;
|
|---|
| [03bc76a] | 926 | r = block_get(&b, service_id, block, flags);
|
|---|
| [c699b0c] | 927 | if (r != EOK)
|
|---|
| 928 | goto out_err;
|
|---|
| [bb7e8382] | 929 |
|
|---|
| [36cb22f] | 930 | if (flags == BLOCK_FLAGS_NOREAD)
|
|---|
| 931 | memset(b->data, 0, sbi->block_size);
|
|---|
| 932 |
|
|---|
| 933 | async_data_write_finalize(callid, b->data + (pos % bs), bytes);
|
|---|
| [bb7e8382] | 934 | b->dirty = true;
|
|---|
| 935 |
|
|---|
| 936 | r = block_put(b);
|
|---|
| 937 | if (r != EOK) {
|
|---|
| 938 | mfs_node_put(fn);
|
|---|
| [03bc76a] | 939 | return r;
|
|---|
| [bb7e8382] | 940 | }
|
|---|
| 941 |
|
|---|
| [36cb22f] | 942 | if (pos + bytes > ino_i->i_size) {
|
|---|
| 943 | ino_i->i_size = pos + bytes;
|
|---|
| 944 | ino_i->dirty = true;
|
|---|
| 945 | }
|
|---|
| [bb7e8382] | 946 | r = mfs_node_put(fn);
|
|---|
| [36cb22f] | 947 | *nsize = ino_i->i_size;
|
|---|
| [03bc76a] | 948 | *wbytes = bytes;
|
|---|
| 949 | return r;
|
|---|
| [bb7e8382] | 950 |
|
|---|
| 951 | out_err:
|
|---|
| 952 | mfs_node_put(fn);
|
|---|
| 953 | async_answer_0(callid, r);
|
|---|
| [03bc76a] | 954 | return r;
|
|---|
| [bb7e8382] | 955 | }
|
|---|
| 956 |
|
|---|
| [03bc76a] | 957 | static int
|
|---|
| 958 | mfs_destroy(service_id_t service_id, fs_index_t index)
|
|---|
| [d8af1bd] | 959 | {
|
|---|
| [1ba6651] | 960 | fs_node_t *fn = NULL;
|
|---|
| [d8af1bd] | 961 | int r;
|
|---|
| 962 |
|
|---|
| [03bc76a] | 963 | r = mfs_node_get(&fn, service_id, index);
|
|---|
| 964 | if (r != EOK)
|
|---|
| 965 | return r;
|
|---|
| 966 | if (!fn)
|
|---|
| 967 | return ENOENT;
|
|---|
| [d8af1bd] | 968 |
|
|---|
| [6d4d883] | 969 | /* Destroy the inode */
|
|---|
| [e03d545] | 970 | return mfs_destroy_node(fn);
|
|---|
| [d8af1bd] | 971 | }
|
|---|
| 972 |
|
|---|
| 973 | static int
|
|---|
| 974 | mfs_destroy_node(fs_node_t *fn)
|
|---|
| 975 | {
|
|---|
| 976 | struct mfs_node *mnode = fn->data;
|
|---|
| 977 | bool has_children;
|
|---|
| 978 | int r;
|
|---|
| 979 |
|
|---|
| [a5bad72] | 980 | mfsdebug("mfs_destroy_node %d\n", mnode->ino_i->index);
|
|---|
| 981 |
|
|---|
| [d8af1bd] | 982 | r = mfs_has_children(&has_children, fn);
|
|---|
| [c699b0c] | 983 | if (r != EOK)
|
|---|
| 984 | goto out;
|
|---|
| [d8af1bd] | 985 |
|
|---|
| 986 | assert(!has_children);
|
|---|
| 987 |
|
|---|
| [6d4d883] | 988 | /* Free the entire inode content */
|
|---|
| [3a5ee6c] | 989 | r = mfs_inode_shrink(mnode, mnode->ino_i->i_size);
|
|---|
| [c699b0c] | 990 | if (r != EOK)
|
|---|
| 991 | goto out;
|
|---|
| [e03d545] | 992 |
|
|---|
| [6d4d883] | 993 | /* Mark the inode as free in the bitmap */
|
|---|
| [70ac0af] | 994 | r = mfs_free_inode(mnode->instance, mnode->ino_i->index);
|
|---|
| [d8af1bd] | 995 |
|
|---|
| [51db5aeb] | 996 | out:
|
|---|
| [eefb653] | 997 | mfs_node_put(fn);
|
|---|
| [d8af1bd] | 998 | return r;
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| [03bc76a] | 1001 | static int
|
|---|
| 1002 | mfs_truncate(service_id_t service_id, fs_index_t index, aoff64_t size)
|
|---|
| [8a49fed] | 1003 | {
|
|---|
| 1004 | fs_node_t *fn;
|
|---|
| 1005 | int r;
|
|---|
| 1006 |
|
|---|
| [03bc76a] | 1007 | r = mfs_node_get(&fn, service_id, index);
|
|---|
| 1008 | if (r != EOK)
|
|---|
| 1009 | return r;
|
|---|
| 1010 | if (!fn)
|
|---|
| 1011 | return r;
|
|---|
| [8a49fed] | 1012 |
|
|---|
| 1013 | struct mfs_node *mnode = fn->data;
|
|---|
| 1014 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| 1015 |
|
|---|
| 1016 | if (ino_i->i_size == size)
|
|---|
| 1017 | r = EOK;
|
|---|
| 1018 | else
|
|---|
| [3a5ee6c] | 1019 | r = mfs_inode_shrink(mnode, ino_i->i_size - size);
|
|---|
| [8a49fed] | 1020 |
|
|---|
| 1021 | mfs_node_put(fn);
|
|---|
| [03bc76a] | 1022 | return r;
|
|---|
| [8a49fed] | 1023 | }
|
|---|
| 1024 |
|
|---|
| [03bc76a] | 1025 | static int
|
|---|
| 1026 | mfs_instance_get(service_id_t service_id, struct mfs_instance **instance)
|
|---|
| [3b08178] | 1027 | {
|
|---|
| [5bf76c1] | 1028 | void *data;
|
|---|
| 1029 | int rc;
|
|---|
| [44c6091f] | 1030 |
|
|---|
| [5bf76c1] | 1031 | rc = fs_instance_get(service_id, &data);
|
|---|
| [6d4d883] | 1032 | if (rc == EOK)
|
|---|
| [5bf76c1] | 1033 | *instance = (struct mfs_instance *) data;
|
|---|
| [6d4d883] | 1034 | else {
|
|---|
| [5bf76c1] | 1035 | mfsdebug("instance not found\n");
|
|---|
| [3b08178] | 1036 | }
|
|---|
| 1037 |
|
|---|
| [5bf76c1] | 1038 | return rc;
|
|---|
| [3b08178] | 1039 | }
|
|---|
| 1040 |
|
|---|
| [6d4d883] | 1041 | static bool
|
|---|
| 1042 | check_magic_number(uint16_t magic, bool *native,
|
|---|
| 1043 | mfs_version_t *version, bool *longfilenames)
|
|---|
| [9e3dc95] | 1044 | {
|
|---|
| [cfac897] | 1045 | bool rc = true;
|
|---|
| [8ceba1e] | 1046 | *longfilenames = false;
|
|---|
| 1047 |
|
|---|
| [57640e7] | 1048 | if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
|
|---|
| 1049 | *native = magic == MFS_MAGIC_V1;
|
|---|
| [9e3dc95] | 1050 | *version = MFS_VERSION_V1;
|
|---|
| [8ceba1e] | 1051 | } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
|
|---|
| 1052 | *native = magic == MFS_MAGIC_V1L;
|
|---|
| 1053 | *version = MFS_VERSION_V1;
|
|---|
| 1054 | *longfilenames = true;
|
|---|
| [57640e7] | 1055 | } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
|
|---|
| 1056 | *native = magic == MFS_MAGIC_V2;
|
|---|
| [9e3dc95] | 1057 | *version = MFS_VERSION_V2;
|
|---|
| [8ceba1e] | 1058 | } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
|
|---|
| 1059 | *native = magic == MFS_MAGIC_V2L;
|
|---|
| 1060 | *version = MFS_VERSION_V2;
|
|---|
| 1061 | *longfilenames = true;
|
|---|
| [57640e7] | 1062 | } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
|
|---|
| 1063 | *native = magic == MFS_MAGIC_V3;
|
|---|
| [9e3dc95] | 1064 | *version = MFS_VERSION_V3;
|
|---|
| [cfac897] | 1065 | } else
|
|---|
| 1066 | rc = false;
|
|---|
| [9e3dc95] | 1067 |
|
|---|
| [7a96476] | 1068 | return rc;
|
|---|
| [096c8835] | 1069 | }
|
|---|
| 1070 |
|
|---|
| [df3caec5] | 1071 | /** Filesystem sanity check
|
|---|
| 1072 | *
|
|---|
| 1073 | * @param Pointer to the MFS superblock.
|
|---|
| 1074 | *
|
|---|
| 1075 | * @return EOK on success, ENOTSUP otherwise.
|
|---|
| 1076 | */
|
|---|
| 1077 | static int
|
|---|
| 1078 | mfs_check_sanity(struct mfs_sb_info *sbi)
|
|---|
| 1079 | {
|
|---|
| 1080 | if (!is_power_of_two(sbi->block_size) ||
|
|---|
| 1081 | sbi->block_size < MFS_MIN_BLOCKSIZE ||
|
|---|
| 1082 | sbi->block_size > MFS_MAX_BLOCKSIZE)
|
|---|
| 1083 | return ENOTSUP;
|
|---|
| 1084 | else if (sbi->ibmap_blocks == 0 || sbi->zbmap_blocks == 0)
|
|---|
| 1085 | return ENOTSUP;
|
|---|
| 1086 | else if (sbi->ninodes == 0 || sbi->nzones == 0)
|
|---|
| 1087 | return ENOTSUP;
|
|---|
| 1088 | else if (sbi->firstdatazone == 0)
|
|---|
| 1089 | return ENOTSUP;
|
|---|
| 1090 |
|
|---|
| 1091 | return EOK;
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| [03bc76a] | 1094 | static int
|
|---|
| 1095 | mfs_close(service_id_t service_id, fs_index_t index)
|
|---|
| [e700970] | 1096 | {
|
|---|
| [03bc76a] | 1097 | return 0;
|
|---|
| [e700970] | 1098 | }
|
|---|
| 1099 |
|
|---|
| [03bc76a] | 1100 | static int
|
|---|
| 1101 | mfs_sync(service_id_t service_id, fs_index_t index)
|
|---|
| [51db5aeb] | 1102 | {
|
|---|
| [5b56dc7] | 1103 | fs_node_t *fn = NULL;
|
|---|
| [03bc76a] | 1104 | int rc = mfs_node_get(&fn, service_id, index);
|
|---|
| 1105 | if (rc != EOK)
|
|---|
| 1106 | return rc;
|
|---|
| 1107 | if (!fn)
|
|---|
| 1108 | return ENOENT;
|
|---|
| [51db5aeb] | 1109 |
|
|---|
| 1110 | struct mfs_node *mnode = fn->data;
|
|---|
| 1111 | mnode->ino_i->dirty = true;
|
|---|
| 1112 |
|
|---|
| [03bc76a] | 1113 | return mfs_node_put(fn);
|
|---|
| [51db5aeb] | 1114 | }
|
|---|
| 1115 |
|
|---|
| [df3caec5] | 1116 | /** Check if a given number is a power of two.
|
|---|
| 1117 | *
|
|---|
| 1118 | * @param n The number to check.
|
|---|
| 1119 | *
|
|---|
| 1120 | * @return true if it is a power of two, false otherwise.
|
|---|
| 1121 | */
|
|---|
| 1122 | static bool
|
|---|
| 1123 | is_power_of_two(uint32_t n)
|
|---|
| 1124 | {
|
|---|
| 1125 | if (n == 0)
|
|---|
| 1126 | return false;
|
|---|
| 1127 |
|
|---|
| 1128 | return (n & (n - 1)) == 0;
|
|---|
| 1129 | }
|
|---|
| 1130 |
|
|---|
| [03bc76a] | 1131 | vfs_out_ops_t mfs_ops = {
|
|---|
| 1132 | .mounted = mfs_mounted,
|
|---|
| 1133 | .unmounted = mfs_unmounted,
|
|---|
| 1134 | .read = mfs_read,
|
|---|
| 1135 | .write = mfs_write,
|
|---|
| 1136 | .truncate = mfs_truncate,
|
|---|
| 1137 | .close = mfs_close,
|
|---|
| 1138 | .destroy = mfs_destroy,
|
|---|
| 1139 | .sync = mfs_sync,
|
|---|
| 1140 | };
|
|---|
| 1141 |
|
|---|
| [096c8835] | 1142 | /**
|
|---|
| 1143 | * @}
|
|---|
| [44c6091f] | 1144 | */
|
|---|
| [096c8835] | 1145 |
|
|---|