| 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 | #ifndef _MFS_H_
|
|---|
| 34 | #define _MFS_H_
|
|---|
| 35 |
|
|---|
| 36 | #include <minix.h>
|
|---|
| 37 | #include <macros.h>
|
|---|
| 38 | #include <libblock.h>
|
|---|
| 39 | #include <libfs.h>
|
|---|
| 40 | #include <adt/list.h>
|
|---|
| 41 | #include "../../vfs/vfs.h"
|
|---|
| 42 |
|
|---|
| 43 | #define NAME "mfs"
|
|---|
| 44 |
|
|---|
| 45 | #define DEBUG_MODE
|
|---|
| 46 |
|
|---|
| 47 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
|---|
| 48 |
|
|---|
| 49 | #ifdef DEBUG_MODE
|
|---|
| 50 | #define mfsdebug(...) printf(__VA_ARGS__)
|
|---|
| 51 | #else
|
|---|
| 52 | #define mfsdebug(...)
|
|---|
| 53 | #endif
|
|---|
| 54 |
|
|---|
| 55 | #ifdef _MAIN
|
|---|
| 56 | #define GLOBAL
|
|---|
| 57 | #else
|
|---|
| 58 | #define GLOBAL extern
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | GLOBAL fs_reg_t mfs_reg;
|
|---|
| 62 |
|
|---|
| 63 | typedef uint32_t bitchunk_t;
|
|---|
| 64 |
|
|---|
| 65 | typedef enum {
|
|---|
| 66 | BMAP_ZONE,
|
|---|
| 67 | BMAP_INODE
|
|---|
| 68 | } bmap_id_t;
|
|---|
| 69 |
|
|---|
| 70 | typedef enum {
|
|---|
| 71 | MFS_VERSION_V1 = 1,
|
|---|
| 72 | MFS_VERSION_V2,
|
|---|
| 73 | MFS_VERSION_V3
|
|---|
| 74 | } mfs_version_t;
|
|---|
| 75 |
|
|---|
| 76 | /*Generic MinixFS superblock*/
|
|---|
| 77 | struct mfs_sb_info {
|
|---|
| 78 | uint32_t ninodes;
|
|---|
| 79 | uint32_t nzones;
|
|---|
| 80 | unsigned long ibmap_blocks;
|
|---|
| 81 | unsigned long zbmap_blocks;
|
|---|
| 82 | unsigned long firstdatazone;
|
|---|
| 83 | int log2_zone_size;
|
|---|
| 84 | int block_size;
|
|---|
| 85 | uint32_t max_file_size;
|
|---|
| 86 | uint16_t magic;
|
|---|
| 87 | uint16_t state;
|
|---|
| 88 |
|
|---|
| 89 | /*The following fields do not exist on disk but only in memory*/
|
|---|
| 90 | unsigned long itable_size;
|
|---|
| 91 | mfs_version_t fs_version;
|
|---|
| 92 | int ino_per_block;
|
|---|
| 93 | size_t dirsize;
|
|---|
| 94 | int itable_off;
|
|---|
| 95 | unsigned max_name_len;
|
|---|
| 96 | bool long_names;
|
|---|
| 97 | bool native;
|
|---|
| 98 | unsigned isearch;
|
|---|
| 99 | unsigned zsearch;
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | /*Generic MinixFS inode*/
|
|---|
| 103 | struct mfs_ino_info {
|
|---|
| 104 | uint16_t i_mode;
|
|---|
| 105 | uint16_t i_nlinks;
|
|---|
| 106 | int16_t i_uid;
|
|---|
| 107 | uint16_t i_gid;
|
|---|
| 108 | size_t i_size;
|
|---|
| 109 | int32_t i_atime;
|
|---|
| 110 | int32_t i_mtime;
|
|---|
| 111 | int32_t i_ctime;
|
|---|
| 112 | /*Block numbers for direct zones*/
|
|---|
| 113 | uint32_t i_dzone[V2_NR_DIRECT_ZONES];
|
|---|
| 114 | /*Block numbers for indirect zones*/
|
|---|
| 115 | uint32_t i_izone[V2_NR_INDIRECT_ZONES];
|
|---|
| 116 |
|
|---|
| 117 | /*The following fields do not exist on disk but only in memory*/
|
|---|
| 118 | bool dirty;
|
|---|
| 119 | fs_index_t index;
|
|---|
| 120 | };
|
|---|
| 121 |
|
|---|
| 122 | /*Generic MFS directory entry*/
|
|---|
| 123 | struct mfs_dentry_info {
|
|---|
| 124 | uint32_t d_inum;
|
|---|
| 125 | char d_name[MFS3_MAX_NAME_LEN + 1];
|
|---|
| 126 |
|
|---|
| 127 | /*The following fields do not exist on disk but only in memory*/
|
|---|
| 128 |
|
|---|
| 129 | /*Index of the dentry in the list*/
|
|---|
| 130 | unsigned index;
|
|---|
| 131 | /*Pointer to the node at witch the dentry belongs*/
|
|---|
| 132 | struct mfs_node *node;
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | struct mfs_instance {
|
|---|
| 136 | link_t link;
|
|---|
| 137 | devmap_handle_t handle;
|
|---|
| 138 | struct mfs_sb_info *sbi;
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | /*MinixFS node in core*/
|
|---|
| 142 | struct mfs_node {
|
|---|
| 143 | struct mfs_ino_info *ino_i;
|
|---|
| 144 | struct mfs_instance *instance;
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | /*mfs_ops.c*/
|
|---|
| 148 | extern void mfs_mounted(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 149 | extern void mfs_mount(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 150 | extern void mfs_lookup(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 151 | extern int mfs_instance_get(devmap_handle_t handle,
|
|---|
| 152 | struct mfs_instance **instance);
|
|---|
| 153 |
|
|---|
| 154 | extern void mfs_stat(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 155 | extern void mfs_close(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 156 | extern void mfs_open_node(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 157 |
|
|---|
| 158 | extern void
|
|---|
| 159 | mfs_read(ipc_callid_t rid, ipc_call_t *request);
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | /*mfs_inode.c*/
|
|---|
| 163 | int
|
|---|
| 164 | get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
|
|---|
| 165 | fs_index_t index);
|
|---|
| 166 |
|
|---|
| 167 | extern int
|
|---|
| 168 | put_inode(struct mfs_node *mnode);
|
|---|
| 169 |
|
|---|
| 170 | int
|
|---|
| 171 | inode_grow(struct mfs_node *mnode, size_t size_grow);
|
|---|
| 172 |
|
|---|
| 173 | /*mfs_rw.c*/
|
|---|
| 174 | extern int
|
|---|
| 175 | read_map(uint32_t *b, const struct mfs_node *mnode, const uint32_t pos);
|
|---|
| 176 |
|
|---|
| 177 | extern int
|
|---|
| 178 | write_map(struct mfs_node *mnode, uint32_t pos, uint32_t new_zone,
|
|---|
| 179 | uint32_t *old_zone);
|
|---|
| 180 |
|
|---|
| 181 | extern int
|
|---|
| 182 | free_zone(struct mfs_node *mnode, const uint32_t zone);
|
|---|
| 183 |
|
|---|
| 184 | /*mfs_dentry.c*/
|
|---|
| 185 | extern int
|
|---|
| 186 | read_directory_entry(struct mfs_node *mnode,
|
|---|
| 187 | struct mfs_dentry_info **d_info, unsigned index);
|
|---|
| 188 |
|
|---|
| 189 | extern int
|
|---|
| 190 | write_dentry(struct mfs_dentry_info *d_info);
|
|---|
| 191 |
|
|---|
| 192 | int
|
|---|
| 193 | insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum);
|
|---|
| 194 |
|
|---|
| 195 | /*mfs_balloc.c*/
|
|---|
| 196 | extern int
|
|---|
| 197 | mfs_alloc_bit(struct mfs_instance *inst, uint32_t *idx, bmap_id_t bid);
|
|---|
| 198 |
|
|---|
| 199 | extern int
|
|---|
| 200 | mfs_free_bit(struct mfs_instance *inst, uint32_t idx, bmap_id_t bid);
|
|---|
| 201 |
|
|---|
| 202 | #endif
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * @}
|
|---|
| 206 | */
|
|---|
| 207 |
|
|---|