| 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 <block.h>
|
|---|
| 39 | #include <libfs.h>
|
|---|
| 40 | #include <adt/list.h>
|
|---|
| 41 | #include <malloc.h>
|
|---|
| 42 | #include <mem.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <assert.h>
|
|---|
| 46 | #include <stdbool.h>
|
|---|
| 47 | #include "../../vfs/vfs.h"
|
|---|
| 48 |
|
|---|
| 49 | #define NAME "mfs"
|
|---|
| 50 |
|
|---|
| 51 | /* #define DEBUG_MODE */
|
|---|
| 52 |
|
|---|
| 53 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
|---|
| 54 |
|
|---|
| 55 | #ifdef DEBUG_MODE
|
|---|
| 56 | #define mfsdebug(...) printf(__VA_ARGS__)
|
|---|
| 57 | #else
|
|---|
| 58 | #define mfsdebug(...)
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | #define MFS_BMAP_START_BLOCK(sbi, bid) \
|
|---|
| 62 | ((bid) == BMAP_ZONE ? 2 + (sbi)->ibmap_blocks : 2)
|
|---|
| 63 |
|
|---|
| 64 | #define MFS_BMAP_SIZE_BITS(sbi, bid) \
|
|---|
| 65 | ((bid) == BMAP_ZONE ? (sbi)->nzones - (sbi)->firstdatazone - 1 : \
|
|---|
| 66 | (sbi)->ninodes - 1)
|
|---|
| 67 |
|
|---|
| 68 | #define MFS_BMAP_SIZE_BLOCKS(sbi, bid) \
|
|---|
| 69 | ((bid) == BMAP_ZONE ? (sbi)->zbmap_blocks : (sbi)->ibmap_blocks)
|
|---|
| 70 |
|
|---|
| 71 | typedef uint32_t bitchunk_t;
|
|---|
| 72 |
|
|---|
| 73 | typedef enum {
|
|---|
| 74 | BMAP_ZONE,
|
|---|
| 75 | BMAP_INODE
|
|---|
| 76 | } bmap_id_t;
|
|---|
| 77 |
|
|---|
| 78 | typedef enum {
|
|---|
| 79 | MFS_VERSION_V1 = 1,
|
|---|
| 80 | MFS_VERSION_V2,
|
|---|
| 81 | MFS_VERSION_V3
|
|---|
| 82 | } mfs_version_t;
|
|---|
| 83 |
|
|---|
| 84 | /* Generic MinixFS superblock */
|
|---|
| 85 | struct mfs_sb_info {
|
|---|
| 86 | uint32_t ninodes;
|
|---|
| 87 | uint32_t nzones;
|
|---|
| 88 | unsigned long ibmap_blocks;
|
|---|
| 89 | unsigned long zbmap_blocks;
|
|---|
| 90 | unsigned long firstdatazone;
|
|---|
| 91 | int log2_zone_size;
|
|---|
| 92 | int block_size;
|
|---|
| 93 | uint32_t max_file_size;
|
|---|
| 94 | uint16_t magic;
|
|---|
| 95 | uint16_t state;
|
|---|
| 96 |
|
|---|
| 97 | /* The following fields do not exist on disk but only in memory */
|
|---|
| 98 | unsigned long itable_size;
|
|---|
| 99 | mfs_version_t fs_version;
|
|---|
| 100 | int ino_per_block;
|
|---|
| 101 | size_t dirsize;
|
|---|
| 102 | int itable_off;
|
|---|
| 103 | unsigned max_name_len;
|
|---|
| 104 | bool long_names;
|
|---|
| 105 | bool native;
|
|---|
| 106 | unsigned isearch;
|
|---|
| 107 | unsigned zsearch;
|
|---|
| 108 |
|
|---|
| 109 | /* Indicates wether if the cached number of free zones
|
|---|
| 110 | * is to be considered valid or not.
|
|---|
| 111 | */
|
|---|
| 112 | bool nfree_zones_valid;
|
|---|
| 113 | /* Cached number of free zones, used to avoid to scan
|
|---|
| 114 | * the whole bitmap every time the mfs_free_block_count()
|
|---|
| 115 | * is invoked.
|
|---|
| 116 | */
|
|---|
| 117 | unsigned nfree_zones;
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | /* Generic MinixFS inode */
|
|---|
| 121 | struct mfs_ino_info {
|
|---|
| 122 | uint16_t i_mode;
|
|---|
| 123 | uint16_t i_nlinks;
|
|---|
| 124 | int16_t i_uid;
|
|---|
| 125 | uint16_t i_gid;
|
|---|
| 126 | size_t i_size;
|
|---|
| 127 | int32_t i_atime;
|
|---|
| 128 | int32_t i_mtime;
|
|---|
| 129 | int32_t i_ctime;
|
|---|
| 130 | /* Block numbers for direct zones */
|
|---|
| 131 | uint32_t i_dzone[V2_NR_DIRECT_ZONES];
|
|---|
| 132 | /* Block numbers for indirect zones */
|
|---|
| 133 | uint32_t i_izone[V2_NR_INDIRECT_ZONES];
|
|---|
| 134 |
|
|---|
| 135 | /* The following fields do not exist on disk but only in memory */
|
|---|
| 136 | bool dirty;
|
|---|
| 137 | fs_index_t index;
|
|---|
| 138 | };
|
|---|
| 139 |
|
|---|
| 140 | /* Generic MFS directory entry */
|
|---|
| 141 | struct mfs_dentry_info {
|
|---|
| 142 | uint32_t d_inum;
|
|---|
| 143 | char d_name[MFS3_MAX_NAME_LEN + 1];
|
|---|
| 144 |
|
|---|
| 145 | /* The following fields do not exist on disk but only in memory */
|
|---|
| 146 |
|
|---|
| 147 | /* Index of the dentry in the list */
|
|---|
| 148 | unsigned index;
|
|---|
| 149 | /* Pointer to the node at witch the dentry belongs */
|
|---|
| 150 | struct mfs_node *node;
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | struct mfs_instance {
|
|---|
| 154 | service_id_t service_id;
|
|---|
| 155 | struct mfs_sb_info *sbi;
|
|---|
| 156 | unsigned open_nodes_cnt;
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 | /* MinixFS node in core */
|
|---|
| 160 | struct mfs_node {
|
|---|
| 161 | struct mfs_ino_info *ino_i;
|
|---|
| 162 | struct mfs_instance *instance;
|
|---|
| 163 | unsigned refcnt;
|
|---|
| 164 | fs_node_t *fsnode;
|
|---|
| 165 | ht_link_t link;
|
|---|
| 166 | };
|
|---|
| 167 |
|
|---|
| 168 | /* mfs_ops.c */
|
|---|
| 169 | extern vfs_out_ops_t mfs_ops;
|
|---|
| 170 | extern libfs_ops_t mfs_libfs_ops;
|
|---|
| 171 |
|
|---|
| 172 | extern int
|
|---|
| 173 | mfs_global_init(void);
|
|---|
| 174 |
|
|---|
| 175 | /* mfs_inode.c */
|
|---|
| 176 | extern int
|
|---|
| 177 | mfs_get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
|
|---|
| 178 | fs_index_t index);
|
|---|
| 179 |
|
|---|
| 180 | extern int
|
|---|
| 181 | mfs_put_inode(struct mfs_node *mnode);
|
|---|
| 182 |
|
|---|
| 183 | extern int
|
|---|
| 184 | mfs_inode_shrink(struct mfs_node *mnode, size_t size_shrink);
|
|---|
| 185 |
|
|---|
| 186 | /* mfs_rw.c */
|
|---|
| 187 | extern int
|
|---|
| 188 | mfs_read_map(uint32_t *b, const struct mfs_node *mnode, const uint32_t pos);
|
|---|
| 189 |
|
|---|
| 190 | extern int
|
|---|
| 191 | mfs_write_map(struct mfs_node *mnode, uint32_t pos, uint32_t new_zone,
|
|---|
| 192 | uint32_t *old_zone);
|
|---|
| 193 |
|
|---|
| 194 | extern int
|
|---|
| 195 | mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size);
|
|---|
| 196 |
|
|---|
| 197 | /* mfs_dentry.c */
|
|---|
| 198 | extern int
|
|---|
| 199 | mfs_read_dentry(struct mfs_node *mnode,
|
|---|
| 200 | struct mfs_dentry_info *d_info, unsigned index);
|
|---|
| 201 |
|
|---|
| 202 | extern int
|
|---|
| 203 | mfs_write_dentry(struct mfs_dentry_info *d_info);
|
|---|
| 204 |
|
|---|
| 205 | extern int
|
|---|
| 206 | mfs_remove_dentry(struct mfs_node *mnode, const char *d_name);
|
|---|
| 207 |
|
|---|
| 208 | extern int
|
|---|
| 209 | mfs_insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum);
|
|---|
| 210 |
|
|---|
| 211 | /* mfs_balloc.c */
|
|---|
| 212 | extern int
|
|---|
| 213 | mfs_alloc_inode(struct mfs_instance *inst, uint32_t *inum);
|
|---|
| 214 |
|
|---|
| 215 | extern int
|
|---|
| 216 | mfs_free_inode(struct mfs_instance *inst, uint32_t inum);
|
|---|
| 217 |
|
|---|
| 218 | extern int
|
|---|
| 219 | mfs_alloc_zone(struct mfs_instance *inst, uint32_t *zone);
|
|---|
| 220 |
|
|---|
| 221 | extern int
|
|---|
| 222 | mfs_free_zone(struct mfs_instance *inst, uint32_t zone);
|
|---|
| 223 |
|
|---|
| 224 | extern int
|
|---|
| 225 | mfs_count_free_zones(struct mfs_instance *inst, uint32_t *zones);
|
|---|
| 226 |
|
|---|
| 227 | extern int
|
|---|
| 228 | mfs_count_free_inodes(struct mfs_instance *inst, uint32_t *inodes);
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | /* mfs_utils.c */
|
|---|
| 232 | extern uint16_t
|
|---|
| 233 | conv16(bool native, uint16_t n);
|
|---|
| 234 |
|
|---|
| 235 | extern uint32_t
|
|---|
| 236 | conv32(bool native, uint32_t n);
|
|---|
| 237 |
|
|---|
| 238 | extern uint64_t
|
|---|
| 239 | conv64(bool native, uint64_t n);
|
|---|
| 240 |
|
|---|
| 241 | #endif
|
|---|
| 242 |
|
|---|
| 243 | /**
|
|---|
| 244 | * @}
|
|---|
| 245 | */
|
|---|
| 246 |
|
|---|