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