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