| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Maurizio Lombardi
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <stdlib.h>
|
|---|
| 35 | #include <assert.h>
|
|---|
| 36 | #include <fibril_synch.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include "mfs.h"
|
|---|
| 39 | #include "mfs_utils.h"
|
|---|
| 40 |
|
|---|
| 41 | static bool check_magic_number(uint16_t magic, bool *native,
|
|---|
| 42 | mfs_version_t *version, bool *longfilenames);
|
|---|
| 43 | static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
|
|---|
| 44 | fs_index_t index);
|
|---|
| 45 |
|
|---|
| 46 | static int mfs_node_put(fs_node_t *fsnode);
|
|---|
| 47 | static int mfs_node_open(fs_node_t *fsnode);
|
|---|
| 48 | static fs_index_t mfs_index_get(fs_node_t *fsnode);
|
|---|
| 49 | static unsigned mfs_lnkcnt_get(fs_node_t *fsnode);
|
|---|
| 50 | static char mfs_plb_get_char(unsigned pos);
|
|---|
| 51 | static bool mfs_is_directory(fs_node_t *fsnode);
|
|---|
| 52 | static bool mfs_is_file(fs_node_t *fsnode);
|
|---|
| 53 | static int mfs_has_children(bool *has_children, fs_node_t *fsnode);
|
|---|
| 54 | static int mfs_root_get(fs_node_t **rfn, devmap_handle_t handle);
|
|---|
| 55 | static devmap_handle_t mfs_device_get(fs_node_t *fsnode);
|
|---|
| 56 | static aoff64_t mfs_size_get(fs_node_t *node);
|
|---|
| 57 | static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component);
|
|---|
| 58 | static int mfs_create_node(fs_node_t **rfn, devmap_handle_t handle, int flags);
|
|---|
| 59 | static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name);
|
|---|
| 60 |
|
|---|
| 61 | static int mfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle,
|
|---|
| 62 | fs_index_t index);
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | static LIST_INITIALIZE(inst_list);
|
|---|
| 66 | static FIBRIL_MUTEX_INITIALIZE(inst_list_mutex);
|
|---|
| 67 |
|
|---|
| 68 | libfs_ops_t mfs_libfs_ops = {
|
|---|
| 69 | .size_get = mfs_size_get,
|
|---|
| 70 | .root_get = mfs_root_get,
|
|---|
| 71 | .device_get = mfs_device_get,
|
|---|
| 72 | .is_directory = mfs_is_directory,
|
|---|
| 73 | .is_file = mfs_is_file,
|
|---|
| 74 | .node_get = mfs_node_get,
|
|---|
| 75 | .node_put = mfs_node_put,
|
|---|
| 76 | .node_open = mfs_node_open,
|
|---|
| 77 | .index_get = mfs_index_get,
|
|---|
| 78 | .match = mfs_match,
|
|---|
| 79 | .create = mfs_create_node,
|
|---|
| 80 | .link = mfs_link,
|
|---|
| 81 | .plb_get_char = mfs_plb_get_char,
|
|---|
| 82 | .has_children = mfs_has_children,
|
|---|
| 83 | .lnkcnt_get = mfs_lnkcnt_get
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | void mfs_mounted(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 87 | {
|
|---|
| 88 | devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 89 | enum cache_mode cmode;
|
|---|
| 90 | struct mfs_superblock *sb;
|
|---|
| 91 | struct mfs3_superblock *sb3;
|
|---|
| 92 | struct mfs_sb_info *sbi;
|
|---|
| 93 | struct mfs_instance *instance;
|
|---|
| 94 | bool native, longnames;
|
|---|
| 95 | mfs_version_t version;
|
|---|
| 96 | uint16_t magic;
|
|---|
| 97 |
|
|---|
| 98 | /* Accept the mount options */
|
|---|
| 99 | char *opts;
|
|---|
| 100 | int rc = async_data_write_accept((void **) &opts, true, 0, 0, 0, NULL);
|
|---|
| 101 |
|
|---|
| 102 | if (rc != EOK) {
|
|---|
| 103 | mfsdebug("Can't accept async data write\n");
|
|---|
| 104 | async_answer_0(rid, rc);
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /* Check for option enabling write through. */
|
|---|
| 109 | if (str_cmp(opts, "wtcache") == 0)
|
|---|
| 110 | cmode = CACHE_MODE_WT;
|
|---|
| 111 | else
|
|---|
| 112 | cmode = CACHE_MODE_WB;
|
|---|
| 113 |
|
|---|
| 114 | free(opts);
|
|---|
| 115 |
|
|---|
| 116 | /* initialize libblock */
|
|---|
| 117 | rc = block_init(devmap_handle, 1024);
|
|---|
| 118 | if (rc != EOK) {
|
|---|
| 119 | mfsdebug("libblock initialization failed\n");
|
|---|
| 120 | async_answer_0(rid, rc);
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /*Allocate space for generic MFS superblock*/
|
|---|
| 125 | sbi = malloc(sizeof(*sbi));
|
|---|
| 126 |
|
|---|
| 127 | if (!sbi) {
|
|---|
| 128 | async_answer_0(rid, ENOMEM);
|
|---|
| 129 | return;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /*Allocate space for filesystem instance*/
|
|---|
| 133 | instance = malloc(sizeof(*instance));
|
|---|
| 134 |
|
|---|
| 135 | if (!instance) {
|
|---|
| 136 | async_answer_0(rid, ENOMEM);
|
|---|
| 137 | return;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | sb = malloc(MFS_SUPERBLOCK_SIZE);
|
|---|
| 141 |
|
|---|
| 142 | if (!sb) {
|
|---|
| 143 | async_answer_0(rid, ENOMEM);
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /* Read the superblock */
|
|---|
| 148 | rc = block_read_direct(devmap_handle, MFS_SUPERBLOCK << 1, 1, sb);
|
|---|
| 149 | if (rc != EOK) {
|
|---|
| 150 | block_fini(devmap_handle);
|
|---|
| 151 | async_answer_0(rid, rc);
|
|---|
| 152 | return;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | sb3 = (struct mfs3_superblock *) sb;
|
|---|
| 156 |
|
|---|
| 157 | if (check_magic_number(sb->s_magic, &native, &version, &longnames)) {
|
|---|
| 158 | /*This is a V1 or V2 Minix filesystem*/
|
|---|
| 159 | magic = sb->s_magic;
|
|---|
| 160 | goto recognized;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | if (!check_magic_number(sb3->s_magic, &native, &version, &longnames)) {
|
|---|
| 164 | mfsdebug("magic number not recognized\n");
|
|---|
| 165 | block_fini(devmap_handle);
|
|---|
| 166 | async_answer_0(rid, ENOTSUP);
|
|---|
| 167 | return;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /*This is a V3 Minix filesystem*/
|
|---|
| 171 |
|
|---|
| 172 | magic = sb3->s_magic;
|
|---|
| 173 |
|
|---|
| 174 | recognized:
|
|---|
| 175 |
|
|---|
| 176 | mfsdebug("magic number recognized = %04x\n", magic);
|
|---|
| 177 |
|
|---|
| 178 | /*Fill superblock info structure*/
|
|---|
| 179 |
|
|---|
| 180 | sbi->fs_version = version;
|
|---|
| 181 | sbi->long_names = longnames;
|
|---|
| 182 | sbi->native = native;
|
|---|
| 183 | sbi->magic = magic;
|
|---|
| 184 | sbi->isearch = 0;
|
|---|
| 185 | sbi->zsearch = 0;
|
|---|
| 186 |
|
|---|
| 187 | if (version == MFS_VERSION_V3) {
|
|---|
| 188 | sbi->ninodes = conv32(native, sb3->s_ninodes);
|
|---|
| 189 | sbi->ibmap_blocks = conv16(native, sb3->s_ibmap_blocks);
|
|---|
| 190 | sbi->zbmap_blocks = conv16(native, sb3->s_zbmap_blocks);
|
|---|
| 191 | sbi->firstdatazone = conv16(native, sb3->s_first_data_zone);
|
|---|
| 192 | sbi->log2_zone_size = conv16(native, sb3->s_log2_zone_size);
|
|---|
| 193 | sbi->max_file_size = conv32(native, sb3->s_max_file_size);
|
|---|
| 194 | sbi->nzones = conv32(native, sb3->s_nzones);
|
|---|
| 195 | sbi->block_size = conv16(native, sb3->s_block_size);
|
|---|
| 196 | sbi->ino_per_block = V3_INODES_PER_BLOCK(sbi->block_size);
|
|---|
| 197 | sbi->dirsize = MFS3_DIRSIZE;
|
|---|
| 198 | sbi->max_name_len = MFS3_MAX_NAME_LEN;
|
|---|
| 199 | } else {
|
|---|
| 200 | sbi->ninodes = conv16(native, sb->s_ninodes);
|
|---|
| 201 | sbi->ibmap_blocks = conv16(native, sb->s_ibmap_blocks);
|
|---|
| 202 | sbi->zbmap_blocks = conv16(native, sb->s_zbmap_blocks);
|
|---|
| 203 | sbi->firstdatazone = conv16(native, sb->s_first_data_zone);
|
|---|
| 204 | sbi->log2_zone_size = conv16(native, sb->s_log2_zone_size);
|
|---|
| 205 | sbi->max_file_size = conv32(native, sb->s_max_file_size);
|
|---|
| 206 | sbi->block_size = MFS_BLOCKSIZE;
|
|---|
| 207 | if (version == MFS_VERSION_V2) {
|
|---|
| 208 | sbi->nzones = conv32(native, sb->s_nzones2);
|
|---|
| 209 | sbi->ino_per_block = V2_INODES_PER_BLOCK;
|
|---|
| 210 | } else {
|
|---|
| 211 | sbi->nzones = conv16(native, sb->s_nzones);
|
|---|
| 212 | sbi->ino_per_block = V1_INODES_PER_BLOCK;
|
|---|
| 213 | }
|
|---|
| 214 | sbi->dirsize = longnames ? MFSL_DIRSIZE : MFS_DIRSIZE;
|
|---|
| 215 | sbi->max_name_len = longnames ? MFS_L_MAX_NAME_LEN :
|
|---|
| 216 | MFS_MAX_NAME_LEN;
|
|---|
| 217 | }
|
|---|
| 218 | sbi->itable_off = 2 + sbi->ibmap_blocks + sbi->zbmap_blocks;
|
|---|
| 219 |
|
|---|
| 220 | free(sb);
|
|---|
| 221 |
|
|---|
| 222 | rc = block_cache_init(devmap_handle, sbi->block_size, 0, cmode);
|
|---|
| 223 |
|
|---|
| 224 | if (rc != EOK) {
|
|---|
| 225 | block_fini(devmap_handle);
|
|---|
| 226 | async_answer_0(rid, EINVAL);
|
|---|
| 227 | mfsdebug("block cache initialization failed\n");
|
|---|
| 228 | return;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /*Initialize the instance structure and add it to the list*/
|
|---|
| 232 | link_initialize(&instance->link);
|
|---|
| 233 | instance->handle = devmap_handle;
|
|---|
| 234 | instance->sbi = sbi;
|
|---|
| 235 |
|
|---|
| 236 | fibril_mutex_lock(&inst_list_mutex);
|
|---|
| 237 | list_append(&instance->link, &inst_list);
|
|---|
| 238 | fibril_mutex_unlock(&inst_list_mutex);
|
|---|
| 239 |
|
|---|
| 240 | mfsdebug("mount successful\n");
|
|---|
| 241 |
|
|---|
| 242 | async_answer_0(rid, EOK);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void mfs_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 246 | {
|
|---|
| 247 | libfs_mount(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | devmap_handle_t mfs_device_get(fs_node_t *fsnode)
|
|---|
| 251 | {
|
|---|
| 252 | struct mfs_node *node = fsnode->data;
|
|---|
| 253 | return node->instance->handle;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | static int mfs_create_node(fs_node_t **rfn, devmap_handle_t handle, int flags)
|
|---|
| 257 | {
|
|---|
| 258 | int r;
|
|---|
| 259 | struct mfs_instance *inst;
|
|---|
| 260 | struct mfs_node *mnode;
|
|---|
| 261 | fs_node_t *fsnode;
|
|---|
| 262 | uint32_t inum;
|
|---|
| 263 |
|
|---|
| 264 | mfsdebug("create_node()\n");
|
|---|
| 265 |
|
|---|
| 266 | r = mfs_instance_get(handle, &inst);
|
|---|
| 267 | if (r != EOK)
|
|---|
| 268 | return r;
|
|---|
| 269 |
|
|---|
| 270 | if (flags & L_DIRECTORY) {
|
|---|
| 271 | /*Not yet supported*/
|
|---|
| 272 | return ENOTSUP;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /*Alloc a new inode*/
|
|---|
| 276 | r = mfs_alloc_bit(inst, &inum, BMAP_INODE);
|
|---|
| 277 | if (r != EOK)
|
|---|
| 278 | return r;
|
|---|
| 279 |
|
|---|
| 280 | struct mfs_ino_info *ino_i;
|
|---|
| 281 |
|
|---|
| 282 | ino_i = malloc(sizeof(*ino_i));
|
|---|
| 283 | if (!ino_i) {
|
|---|
| 284 | r = ENOMEM;
|
|---|
| 285 | goto out_err;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | mnode = malloc(sizeof(*mnode));
|
|---|
| 289 | if (!mnode) {
|
|---|
| 290 | r = ENOMEM;
|
|---|
| 291 | goto out_err_1;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | fsnode = malloc(sizeof(fs_node_t));
|
|---|
| 295 | if (!fsnode) {
|
|---|
| 296 | r = ENOMEM;
|
|---|
| 297 | goto out_err_2;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | ino_i->i_mode = S_IFREG;
|
|---|
| 301 | ino_i->i_nlinks = 1;
|
|---|
| 302 | ino_i->i_uid = 0;
|
|---|
| 303 | ino_i->i_gid = 0;
|
|---|
| 304 | ino_i->i_size = 0;
|
|---|
| 305 | ino_i->i_atime = 0;
|
|---|
| 306 | ino_i->i_mtime = 0;
|
|---|
| 307 | ino_i->i_ctime = 0;
|
|---|
| 308 |
|
|---|
| 309 | memset(ino_i->i_dzone, 0, sizeof(uint32_t) * V2_NR_DIRECT_ZONES);
|
|---|
| 310 | memset(ino_i->i_izone, 0, sizeof(uint32_t) * V2_NR_INDIRECT_ZONES);
|
|---|
| 311 |
|
|---|
| 312 | ino_i->index = inum;
|
|---|
| 313 | ino_i->dirty = true;
|
|---|
| 314 | mnode->ino_i = ino_i;
|
|---|
| 315 | mnode->instance = inst;
|
|---|
| 316 |
|
|---|
| 317 | put_inode(mnode);
|
|---|
| 318 |
|
|---|
| 319 | fs_node_initialize(fsnode);
|
|---|
| 320 | fsnode->data = mnode;
|
|---|
| 321 | *rfn = fsnode;
|
|---|
| 322 |
|
|---|
| 323 | return EOK;
|
|---|
| 324 |
|
|---|
| 325 | out_err_2:
|
|---|
| 326 | free(mnode);
|
|---|
| 327 | out_err_1:
|
|---|
| 328 | free(ino_i);
|
|---|
| 329 | out_err:
|
|---|
| 330 | return r;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
|
|---|
| 334 | {
|
|---|
| 335 | struct mfs_node *mnode = pfn->data;
|
|---|
| 336 | struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| 337 | struct mfs_dentry_info *d_info;
|
|---|
| 338 | int r;
|
|---|
| 339 |
|
|---|
| 340 | mfsdebug("mfs_match()\n");
|
|---|
| 341 |
|
|---|
| 342 | if (!S_ISDIR(ino_i->i_mode))
|
|---|
| 343 | return ENOTDIR;
|
|---|
| 344 |
|
|---|
| 345 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 346 | const size_t comp_size = str_size(component);
|
|---|
| 347 |
|
|---|
| 348 | int i = 2;
|
|---|
| 349 | while (1) {
|
|---|
| 350 | r = read_directory_entry(mnode, &d_info, i++);
|
|---|
| 351 | if (r != EOK)
|
|---|
| 352 | return r;
|
|---|
| 353 |
|
|---|
| 354 | if (!d_info) {
|
|---|
| 355 | /*Reached the end of the directory entry list*/
|
|---|
| 356 | break;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (!d_info->d_inum) {
|
|---|
| 360 | /*This entry is not used*/
|
|---|
| 361 | free(d_info);
|
|---|
| 362 | continue;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | if (!bcmp(component, d_info->d_name, min(sbi->max_name_len,
|
|---|
| 366 | comp_size))) {
|
|---|
| 367 | /*Hit!*/
|
|---|
| 368 | mfs_node_core_get(rfn, mnode->instance,
|
|---|
| 369 | d_info->d_inum);
|
|---|
| 370 | free(d_info);
|
|---|
| 371 | goto found;
|
|---|
| 372 | }
|
|---|
| 373 | free(d_info);
|
|---|
| 374 | }
|
|---|
| 375 | *rfn = NULL;
|
|---|
| 376 | return ENOENT;
|
|---|
| 377 | found:
|
|---|
| 378 | return EOK;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | static aoff64_t mfs_size_get(fs_node_t *node)
|
|---|
| 382 | {
|
|---|
| 383 | assert(node);
|
|---|
| 384 |
|
|---|
| 385 | const struct mfs_node *mnode = node->data;
|
|---|
| 386 | assert(mnode);
|
|---|
| 387 | assert(mnode->ino_i);
|
|---|
| 388 |
|
|---|
| 389 | return mnode->ino_i->i_size;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | void mfs_stat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 393 | {
|
|---|
| 394 | mfsdebug("mfs_stat()\n");
|
|---|
| 395 | libfs_stat(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | static int mfs_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle,
|
|---|
| 399 | fs_index_t index)
|
|---|
| 400 | {
|
|---|
| 401 | int rc;
|
|---|
| 402 | struct mfs_instance *instance;
|
|---|
| 403 |
|
|---|
| 404 | rc = mfs_instance_get(devmap_handle, &instance);
|
|---|
| 405 |
|
|---|
| 406 | if (rc != EOK)
|
|---|
| 407 | return rc;
|
|---|
| 408 |
|
|---|
| 409 | return mfs_node_core_get(rfn, instance, index);
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | static int mfs_node_put(fs_node_t *fsnode)
|
|---|
| 413 | {
|
|---|
| 414 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 415 |
|
|---|
| 416 | put_inode(mnode);
|
|---|
| 417 | free(mnode->ino_i);
|
|---|
| 418 | free(mnode);
|
|---|
| 419 |
|
|---|
| 420 | return EOK;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | static int mfs_node_open(fs_node_t *fsnode)
|
|---|
| 424 | {
|
|---|
| 425 | /*
|
|---|
| 426 | * Opening a file is stateless, nothing
|
|---|
| 427 | * to be done here.
|
|---|
| 428 | */
|
|---|
| 429 | return EOK;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | static fs_index_t mfs_index_get(fs_node_t *fsnode)
|
|---|
| 433 | {
|
|---|
| 434 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 435 |
|
|---|
| 436 | assert(mnode->ino_i);
|
|---|
| 437 | return mnode->ino_i->index;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | static unsigned mfs_lnkcnt_get(fs_node_t *fsnode)
|
|---|
| 441 | {
|
|---|
| 442 | unsigned rc;
|
|---|
| 443 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 444 |
|
|---|
| 445 | assert(mnode);
|
|---|
| 446 | assert(mnode->ino_i);
|
|---|
| 447 |
|
|---|
| 448 | rc = mnode->ino_i->i_nlinks;
|
|---|
| 449 | return rc;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
|
|---|
| 453 | fs_index_t index)
|
|---|
| 454 | {
|
|---|
| 455 | fs_node_t *node = NULL;
|
|---|
| 456 | struct mfs_node *mnode = NULL;
|
|---|
| 457 | int rc;
|
|---|
| 458 |
|
|---|
| 459 | node = malloc(sizeof(fs_node_t));
|
|---|
| 460 | if (!node) {
|
|---|
| 461 | rc = ENOMEM;
|
|---|
| 462 | goto out_err;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | fs_node_initialize(node);
|
|---|
| 466 |
|
|---|
| 467 | mnode = malloc(sizeof(*mnode));
|
|---|
| 468 | if (!mnode) {
|
|---|
| 469 | rc = ENOMEM;
|
|---|
| 470 | goto out_err;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | struct mfs_ino_info *ino_i;
|
|---|
| 474 |
|
|---|
| 475 | rc = get_inode(inst, &ino_i, index);
|
|---|
| 476 | if (rc != EOK)
|
|---|
| 477 | goto out_err;
|
|---|
| 478 |
|
|---|
| 479 | ino_i->index = index;
|
|---|
| 480 | mnode->ino_i = ino_i;
|
|---|
| 481 |
|
|---|
| 482 | mnode->instance = inst;
|
|---|
| 483 | node->data = mnode;
|
|---|
| 484 | *rfn = node;
|
|---|
| 485 |
|
|---|
| 486 | mfsdebug("node_get_core(%d) OK\n", (int) index);
|
|---|
| 487 |
|
|---|
| 488 | return EOK;
|
|---|
| 489 |
|
|---|
| 490 | out_err:
|
|---|
| 491 | if (node)
|
|---|
| 492 | free(node);
|
|---|
| 493 | if (mnode)
|
|---|
| 494 | free(mnode);
|
|---|
| 495 | return rc;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | static bool mfs_is_directory(fs_node_t *fsnode)
|
|---|
| 499 | {
|
|---|
| 500 | const struct mfs_node *node = fsnode->data;
|
|---|
| 501 | return S_ISDIR(node->ino_i->i_mode);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | static bool mfs_is_file(fs_node_t *fsnode)
|
|---|
| 505 | {
|
|---|
| 506 | struct mfs_node *node = fsnode->data;
|
|---|
| 507 | return S_ISREG(node->ino_i->i_mode);
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | static int mfs_root_get(fs_node_t **rfn, devmap_handle_t handle)
|
|---|
| 511 | {
|
|---|
| 512 | int rc = mfs_node_get(rfn, handle, MFS_ROOT_INO);
|
|---|
| 513 | return rc;
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | void mfs_lookup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 517 | {
|
|---|
| 518 | libfs_lookup(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | static char mfs_plb_get_char(unsigned pos)
|
|---|
| 522 | {
|
|---|
| 523 | return mfs_reg.plb_ro[pos % PLB_SIZE];
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
|
|---|
| 527 | {
|
|---|
| 528 | struct mfs_node *parent = pfn->data;
|
|---|
| 529 | struct mfs_node *child = cfn->data;
|
|---|
| 530 |
|
|---|
| 531 | mfsdebug("mfs_link()\n");
|
|---|
| 532 |
|
|---|
| 533 | int r = insert_dentry(parent, name, child->ino_i->index);
|
|---|
| 534 |
|
|---|
| 535 | return r;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | static int mfs_has_children(bool *has_children, fs_node_t *fsnode)
|
|---|
| 539 | {
|
|---|
| 540 | struct mfs_node *mnode = fsnode->data;
|
|---|
| 541 | int r;
|
|---|
| 542 |
|
|---|
| 543 | *has_children = false;
|
|---|
| 544 |
|
|---|
| 545 | if (!S_ISDIR(mnode->ino_i->i_mode))
|
|---|
| 546 | goto out;
|
|---|
| 547 |
|
|---|
| 548 | struct mfs_dentry_info *d_info;
|
|---|
| 549 |
|
|---|
| 550 | /* The first two dentries are always . and .. */
|
|---|
| 551 | int i = 2;
|
|---|
| 552 | while (1) {
|
|---|
| 553 | r = read_directory_entry(mnode, &d_info, i++);
|
|---|
| 554 | if (r != EOK)
|
|---|
| 555 | return r;
|
|---|
| 556 |
|
|---|
| 557 | if (!d_info) {
|
|---|
| 558 | /*Reached the end of the dentries list*/
|
|---|
| 559 | break;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | if (d_info->d_inum) {
|
|---|
| 563 | /*A valid entry has been found*/
|
|---|
| 564 | *has_children = true;
|
|---|
| 565 | free(d_info);
|
|---|
| 566 | break;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | free(d_info);
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | out:
|
|---|
| 573 |
|
|---|
| 574 | if (*has_children) {
|
|---|
| 575 | mfsdebug("Has children\n");
|
|---|
| 576 | } else {
|
|---|
| 577 | mfsdebug("Has not children\n");
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | return EOK;
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | void
|
|---|
| 584 | mfs_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 585 | {
|
|---|
| 586 | int rc;
|
|---|
| 587 | devmap_handle_t handle = (devmap_handle_t) IPC_GET_ARG1(*request);
|
|---|
| 588 | fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
|
|---|
| 589 | aoff64_t pos = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request),
|
|---|
| 590 | IPC_GET_ARG4(*request));
|
|---|
| 591 | fs_node_t *fn;
|
|---|
| 592 |
|
|---|
| 593 | rc = mfs_node_get(&fn, handle, index);
|
|---|
| 594 | if (rc != EOK) {
|
|---|
| 595 | async_answer_0(rid, rc);
|
|---|
| 596 | return;
|
|---|
| 597 | }
|
|---|
| 598 | if (!fn) {
|
|---|
| 599 | async_answer_0(rid, ENOENT);
|
|---|
| 600 | return;
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | struct mfs_node *mnode;
|
|---|
| 604 | struct mfs_ino_info *ino_i;
|
|---|
| 605 | size_t len, bytes = 0;
|
|---|
| 606 | ipc_callid_t callid;
|
|---|
| 607 |
|
|---|
| 608 | mnode = fn->data;
|
|---|
| 609 | ino_i = mnode->ino_i;
|
|---|
| 610 |
|
|---|
| 611 | if (!async_data_read_receive(&callid, &len)) {
|
|---|
| 612 | rc = EINVAL;
|
|---|
| 613 | goto out_error;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (S_ISDIR(ino_i->i_mode)) {
|
|---|
| 617 | aoff64_t spos = pos;
|
|---|
| 618 | struct mfs_dentry_info *d_info;
|
|---|
| 619 |
|
|---|
| 620 | while (1) {
|
|---|
| 621 | rc = read_directory_entry(mnode, &d_info, pos);
|
|---|
| 622 | if (rc != EOK)
|
|---|
| 623 | goto out_error;
|
|---|
| 624 |
|
|---|
| 625 | if (!d_info) {
|
|---|
| 626 | /*Reached the end of the dentries list*/
|
|---|
| 627 | break;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | if (d_info->d_inum) {
|
|---|
| 631 | /*Dentry found!*/
|
|---|
| 632 | mfsdebug("DENTRY FOUND %s!!\n", d_info->d_name);
|
|---|
| 633 | goto found;
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| 636 | free(d_info);
|
|---|
| 637 | pos++;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | rc = mfs_node_put(fn);
|
|---|
| 641 | async_answer_0(callid, rc != EOK ? rc : ENOENT);
|
|---|
| 642 | async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
|
|---|
| 643 | return;
|
|---|
| 644 | found:
|
|---|
| 645 | async_data_read_finalize(callid, d_info->d_name,
|
|---|
| 646 | str_size(d_info->d_name) + 1);
|
|---|
| 647 | bytes = ((pos - spos) + 1);
|
|---|
| 648 | } else {
|
|---|
| 649 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 650 |
|
|---|
| 651 | if (pos >= (size_t) ino_i->i_size) {
|
|---|
| 652 | /*Trying to read beyond the end of file*/
|
|---|
| 653 | bytes = 0;
|
|---|
| 654 | (void) async_data_read_finalize(callid, NULL, 0);
|
|---|
| 655 | goto out_success;
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | bytes = min(len, sbi->block_size - pos % sbi->block_size);
|
|---|
| 659 | bytes = min(bytes, ino_i->i_size - pos);
|
|---|
| 660 |
|
|---|
| 661 | uint32_t zone;
|
|---|
| 662 | block_t *b;
|
|---|
| 663 |
|
|---|
| 664 | rc = read_map(&zone, mnode, pos);
|
|---|
| 665 | if (rc != EOK)
|
|---|
| 666 | goto out_error;
|
|---|
| 667 |
|
|---|
| 668 | if (zone == 0) {
|
|---|
| 669 | /*sparse file*/
|
|---|
| 670 | uint8_t *buf = malloc(sbi->block_size);
|
|---|
| 671 | if (!buf) {
|
|---|
| 672 | rc = ENOMEM;
|
|---|
| 673 | goto out_error;
|
|---|
| 674 | }
|
|---|
| 675 | memset(buf, 0, sizeof(sbi->block_size));
|
|---|
| 676 | async_data_read_finalize(callid,
|
|---|
| 677 | buf + pos % sbi->block_size, bytes);
|
|---|
| 678 | free(buf);
|
|---|
| 679 | goto out_success;
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | rc = block_get(&b, handle, zone, BLOCK_FLAGS_NONE);
|
|---|
| 683 | if (rc != EOK)
|
|---|
| 684 | goto out_error;
|
|---|
| 685 |
|
|---|
| 686 | async_data_read_finalize(callid, b->data +
|
|---|
| 687 | pos % sbi->block_size, bytes);
|
|---|
| 688 |
|
|---|
| 689 | rc = block_put(b);
|
|---|
| 690 | if (rc != EOK) {
|
|---|
| 691 | mfs_node_put(fn);
|
|---|
| 692 | async_answer_0(rid, rc);
|
|---|
| 693 | return;
|
|---|
| 694 | }
|
|---|
| 695 | }
|
|---|
| 696 | out_success:
|
|---|
| 697 | rc = mfs_node_put(fn);
|
|---|
| 698 | async_answer_1(rid, rc, (sysarg_t)bytes);
|
|---|
| 699 | return;
|
|---|
| 700 | out_error: ;
|
|---|
| 701 | int tmp = mfs_node_put(fn);
|
|---|
| 702 | async_answer_0(callid, tmp != EOK ? tmp : rc);
|
|---|
| 703 | async_answer_0(rid, tmp != EOK ? tmp : rc);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | int mfs_instance_get(devmap_handle_t handle, struct mfs_instance **instance)
|
|---|
| 707 | {
|
|---|
| 708 | link_t *link;
|
|---|
| 709 | struct mfs_instance *instance_ptr;
|
|---|
| 710 |
|
|---|
| 711 | fibril_mutex_lock(&inst_list_mutex);
|
|---|
| 712 |
|
|---|
| 713 | for (link = inst_list.next; link != &inst_list; link = link->next) {
|
|---|
| 714 | instance_ptr = list_get_instance(link, struct mfs_instance,
|
|---|
| 715 | link);
|
|---|
| 716 |
|
|---|
| 717 | if (instance_ptr->handle == handle) {
|
|---|
| 718 | *instance = instance_ptr;
|
|---|
| 719 | fibril_mutex_unlock(&inst_list_mutex);
|
|---|
| 720 | return EOK;
|
|---|
| 721 | }
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | mfsdebug("Instance not found\n");
|
|---|
| 725 |
|
|---|
| 726 | fibril_mutex_unlock(&inst_list_mutex);
|
|---|
| 727 | return EINVAL;
|
|---|
| 728 | }
|
|---|
| 729 |
|
|---|
| 730 | static bool check_magic_number(uint16_t magic, bool *native,
|
|---|
| 731 | mfs_version_t *version, bool *longfilenames)
|
|---|
| 732 | {
|
|---|
| 733 | bool rc = false;
|
|---|
| 734 | *longfilenames = false;
|
|---|
| 735 |
|
|---|
| 736 | if (magic == MFS_MAGIC_V1 || magic == MFS_MAGIC_V1R) {
|
|---|
| 737 | *native = magic == MFS_MAGIC_V1;
|
|---|
| 738 | *version = MFS_VERSION_V1;
|
|---|
| 739 | rc = true;
|
|---|
| 740 | } else if (magic == MFS_MAGIC_V1L || magic == MFS_MAGIC_V1LR) {
|
|---|
| 741 | *native = magic == MFS_MAGIC_V1L;
|
|---|
| 742 | *version = MFS_VERSION_V1;
|
|---|
| 743 | *longfilenames = true;
|
|---|
| 744 | rc = true;
|
|---|
| 745 | } else if (magic == MFS_MAGIC_V2 || magic == MFS_MAGIC_V2R) {
|
|---|
| 746 | *native = magic == MFS_MAGIC_V2;
|
|---|
| 747 | *version = MFS_VERSION_V2;
|
|---|
| 748 | rc = true;
|
|---|
| 749 | } else if (magic == MFS_MAGIC_V2L || magic == MFS_MAGIC_V2LR) {
|
|---|
| 750 | *native = magic == MFS_MAGIC_V2L;
|
|---|
| 751 | *version = MFS_VERSION_V2;
|
|---|
| 752 | *longfilenames = true;
|
|---|
| 753 | rc = true;
|
|---|
| 754 | } else if (magic == MFS_MAGIC_V3 || magic == MFS_MAGIC_V3R) {
|
|---|
| 755 | *native = magic == MFS_MAGIC_V3;
|
|---|
| 756 | *version = MFS_VERSION_V3;
|
|---|
| 757 | rc = true;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | return rc;
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | void
|
|---|
| 764 | mfs_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 765 | {
|
|---|
| 766 | async_answer_0(rid, EOK);
|
|---|
| 767 | }
|
|---|
| 768 |
|
|---|
| 769 | void
|
|---|
| 770 | mfs_open_node(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 771 | {
|
|---|
| 772 | libfs_open_node(&mfs_libfs_ops, mfs_reg.fs_handle, rid, request);
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | /**
|
|---|
| 776 | * @}
|
|---|
| 777 | */
|
|---|
| 778 |
|
|---|