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