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