1 /*- 2 * modified for EXT2FS support in Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 * 7 * $FreeBSD$ 8 */ 9 /*- 10 * Copyright (c) 2009 Aditya Sarawgi 11 * All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * 35 */ 36 37 #ifndef _FS_EXT2FS_EXT2_FS_H 38 #define _FS_EXT2FS_EXT2_FS_H 39 40 #include <sys/types.h> 41 42 /* 43 * Special inode numbers 44 */ 45 #define EXT2_BAD_INO 1 /* Bad blocks inode */ 46 #define EXT2_ROOT_INO 2 /* Root inode */ 47 #define EXT2_BOOT_LOADER_INO 5 /* Boot loader inode */ 48 #define EXT2_UNDEL_DIR_INO 6 /* Undelete directory inode */ 49 50 /* First non-reserved inode for old ext2 filesystems */ 51 #define E2FS_REV0_FIRST_INO 11 52 53 /* 54 * The second extended file system magic number 55 */ 56 #define E2FS_MAGIC 0xEF53 57 58 #if defined(_KERNEL) 59 /* 60 * FreeBSD passes the pointer to the in-core struct with relevant 61 * fields to EXT2_SB macro when accessing superblock fields. 62 */ 63 #define EXT2_SB(sb) (sb) 64 #else 65 /* Assume that user mode programs are passing in an ext2fs superblock, not 66 * a kernel struct super_block. This will allow us to call the feature-test 67 * macros from user land. */ 68 #define EXT2_SB(sb) (sb) 69 #endif 70 71 /* 72 * Maximal count of links to a file 73 */ 74 #define EXT2_LINK_MAX 32000 75 76 /* 77 * Constants relative to the data blocks 78 */ 79 #define EXT2_NDIR_BLOCKS 12 80 #define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS 81 #define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1) 82 #define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1) 83 #define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1) 84 #define EXT2_MAXSYMLINKLEN (EXT2_N_BLOCKS * sizeof (uint32_t)) 85 86 /* 87 * The path name on which the file system is mounted is maintained 88 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 89 * the super block for this name. 90 */ 91 #define MAXMNTLEN 512 92 93 /* 94 * Super block for an ext2fs file system. 95 */ 96 struct ext2fs { 97 u_int32_t e2fs_icount; /* Inode count */ 98 u_int32_t e2fs_bcount; /* blocks count */ 99 u_int32_t e2fs_rbcount; /* reserved blocks count */ 100 u_int32_t e2fs_fbcount; /* free blocks count */ 101 u_int32_t e2fs_ficount; /* free inodes count */ 102 u_int32_t e2fs_first_dblock; /* first data block */ 103 u_int32_t e2fs_log_bsize; /* block size = 1024*(2^e2fs_log_bsize) */ 104 u_int32_t e2fs_log_fsize; /* fragment size */ 105 u_int32_t e2fs_bpg; /* blocks per group */ 106 u_int32_t e2fs_fpg; /* frags per group */ 107 u_int32_t e2fs_ipg; /* inodes per group */ 108 u_int32_t e2fs_mtime; /* mount time */ 109 u_int32_t e2fs_wtime; /* write time */ 110 u_int16_t e2fs_mnt_count; /* mount count */ 111 u_int16_t e2fs_max_mnt_count; /* max mount count */ 112 u_int16_t e2fs_magic; /* magic number */ 113 u_int16_t e2fs_state; /* file system state */ 114 u_int16_t e2fs_beh; /* behavior on errors */ 115 u_int16_t e2fs_minrev; /* minor revision level */ 116 u_int32_t e2fs_lastfsck; /* time of last fsck */ 117 u_int32_t e2fs_fsckintv; /* max time between fscks */ 118 u_int32_t e2fs_creator; /* creator OS */ 119 u_int32_t e2fs_rev; /* revision level */ 120 u_int16_t e2fs_ruid; /* default uid for reserved blocks */ 121 u_int16_t e2fs_rgid; /* default gid for reserved blocks */ 122 /* EXT2_DYNAMIC_REV superblocks */ 123 u_int32_t e2fs_first_ino; /* first non-reserved inode */ 124 u_int16_t e2fs_inode_size; /* size of inode structure */ 125 u_int16_t e2fs_block_group_nr; /* block grp number of this sblk*/ 126 u_int32_t e2fs_features_compat; /* compatible feature set */ 127 u_int32_t e2fs_features_incompat; /* incompatible feature set */ 128 u_int32_t e2fs_features_rocompat; /* RO-compatible feature set */ 129 u_int8_t e2fs_uuid[16]; /* 128-bit uuid for volume */ 130 char e2fs_vname[16]; /* volume name */ 131 char e2fs_fsmnt[64]; /* name mounted on */ 132 u_int32_t e2fs_algo; /* For comcate for dir */ 133 u_int16_t e2fs_reserved_ngdb; /* # of reserved gd blocks for resize */ 134 u_int32_t reserved2[204]; 135 }; 136 137 138 /* Assume that user mode programs are passing in an ext2fs superblock, not 139 * a kernel struct super_block. This will allow us to call the feature-test 140 * macros from user land. */ 141 #define EXT2_SB(sb) (sb) 142 143 /* 144 * In-Memory Superblock 145 */ 146 147 struct m_ext2fs { 148 struct ext2fs * e2fs; 149 char e2fs_fsmnt[MAXMNTLEN];/* name mounted on */ 150 char e2fs_ronly; /* mounted read-only flag */ 151 char e2fs_fmod; /* super block modified flag */ 152 uint32_t e2fs_bsize; /* Block size */ 153 uint32_t e2fs_bshift; /* calc of logical block no */ 154 int32_t e2fs_bmask; /* calc of block offset */ 155 int32_t e2fs_bpg; /* Number of blocks per group */ 156 int64_t e2fs_qbmask; /* = s_blocksize -1 */ 157 uint32_t e2fs_fsbtodb; /* Shift to get disk block */ 158 uint32_t e2fs_ipg; /* Number of inodes per group */ 159 uint32_t e2fs_ipb; /* Number of inodes per block */ 160 uint32_t e2fs_itpg; /* Number of inode table per group */ 161 uint32_t e2fs_fsize; /* Size of fragments per block */ 162 uint32_t e2fs_fpb; /* Number of fragments per block */ 163 uint32_t e2fs_fpg; /* Number of fragments per group */ 164 uint32_t e2fs_dbpg; /* Number of descriptor blocks per group */ 165 uint32_t e2fs_descpb; /* Number of group descriptors per block */ 166 uint32_t e2fs_gdbcount; /* Number of group descriptors */ 167 uint32_t e2fs_gcount; /* Number of groups */ 168 uint32_t e2fs_first_inode;/* First inode on fs */ 169 int32_t e2fs_isize; /* Size of inode */ 170 uint32_t e2fs_mount_opt; 171 uint32_t e2fs_blocksize_bits; 172 uint32_t e2fs_total_dir; /* Total number of directories */ 173 uint8_t *e2fs_contigdirs; 174 char e2fs_wasvalid; /* valid at mount time */ 175 off_t e2fs_maxfilesize; 176 struct ext2_gd *e2fs_gd; /* Group Descriptors */ 177 }; 178 179 /* 180 * The second extended file system version 181 */ 182 #define E2FS_DATE "95/08/09" 183 #define E2FS_VERSION "0.5b" 184 185 /* 186 * Revision levels 187 */ 188 #define E2FS_REV0 0 /* The good old (original) format */ 189 #define E2FS_REV1 1 /* V2 format w/ dynamic inode sizes */ 190 191 #define E2FS_CURRENT_REV E2FS_REV0 192 #define E2FS_MAX_SUPP_REV E2FS_REV1 193 194 #define E2FS_REV0_INODE_SIZE 128 195 196 /* 197 * compatible/incompatible features 198 */ 199 #define EXT2F_COMPAT_PREALLOC 0x0001 200 #define EXT2F_COMPAT_RESIZE 0x0010 201 202 #define EXT2F_ROCOMPAT_SPARSESUPER 0x0001 203 #define EXT2F_ROCOMPAT_LARGEFILE 0x0002 204 #define EXT2F_ROCOMPAT_BTREE_DIR 0x0004 205 206 #define EXT2F_INCOMPAT_COMP 0x0001 207 #define EXT2F_INCOMPAT_FTYPE 0x0002 208 209 /* 210 * Features supported in this implementation 211 * 212 * We support the following REV1 features: 213 * - EXT2F_ROCOMPAT_SPARSESUPER 214 * - EXT2F_ROCOMPAT_LARGEFILE 215 * - EXT2F_INCOMPAT_FTYPE 216 */ 217 #define EXT2F_COMPAT_SUPP 0x0000 218 #define EXT2F_ROCOMPAT_SUPP (EXT2F_ROCOMPAT_SPARSESUPER \ 219 | EXT2F_ROCOMPAT_LARGEFILE) 220 #define EXT2F_INCOMPAT_SUPP EXT2F_INCOMPAT_FTYPE 221 222 /* 223 * Feature set definitions 224 */ 225 #define EXT2_HAS_COMPAT_FEATURE(sb,mask) \ 226 ( EXT2_SB(sb)->e2fs->e2fs_features_compat & htole32(mask) ) 227 #define EXT2_HAS_RO_COMPAT_FEATURE(sb,mask) \ 228 ( EXT2_SB(sb)->e2fs->e2fs_features_rocompat & htole32(mask) ) 229 #define EXT2_HAS_INCOMPAT_FEATURE(sb,mask) \ 230 ( EXT2_SB(sb)->e2fs->e2fs_features_incompat & htole32(mask) ) 231 232 /* 233 * Definitions of behavior on errors 234 */ 235 #define E2FS_BEH_CONTINUE 1 /* continue operation */ 236 #define E2FS_BEH_READONLY 2 /* remount fs read only */ 237 #define E2FS_BEH_PANIC 3 /* cause panic */ 238 #define E2FS_BEH_DEFAULT E2FS_BEH_CONTINUE 239 240 /* 241 * OS identification 242 */ 243 #define E2FS_OS_LINUX 0 244 #define E2FS_OS_HURD 1 245 #define E2FS_OS_MASIX 2 246 #define E2FS_OS_FREEBSD 3 247 #define E2FS_OS_LITES 4 248 249 /* 250 * File clean flags 251 */ 252 #define E2FS_ISCLEAN 0x0001 /* Unmounted cleanly */ 253 #define E2FS_ERRORS 0x0002 /* Errors detected */ 254 255 /* ext2 file system block group descriptor */ 256 257 struct ext2_gd { 258 u_int32_t ext2bgd_b_bitmap; /* blocks bitmap block */ 259 u_int32_t ext2bgd_i_bitmap; /* inodes bitmap block */ 260 u_int32_t ext2bgd_i_tables; /* inodes table block */ 261 u_int16_t ext2bgd_nbfree; /* number of free blocks */ 262 u_int16_t ext2bgd_nifree; /* number of free inodes */ 263 u_int16_t ext2bgd_ndirs; /* number of directories */ 264 u_int16_t reserved; 265 u_int32_t reserved2[3]; 266 }; 267 268 /* EXT2FS metadatas are stored in little-endian byte order. These macros 269 * helps reading these metadatas 270 */ 271 272 #define e2fs_cgload(old, new, size) memcpy((new), (old), (size)); 273 #define e2fs_cgsave(old, new, size) memcpy((new), (old), (size)); 274 /* 275 * Macro-instructions used to manage several block sizes 276 */ 277 #define EXT2_MIN_BLOCK_SIZE 1024 278 #define EXT2_MAX_BLOCK_SIZE 4096 279 #define EXT2_MIN_BLOCK_LOG_SIZE 10 280 #if defined(_KERNEL) 281 # define EXT2_BLOCK_SIZE(s) ((s)->e2fs_bsize) 282 #else 283 # define EXT2_BLOCK_SIZE(s) (EXT2_MIN_BLOCK_SIZE << (s)->e2fs_log_bsize) 284 #endif 285 #define EXT2_ADDR_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (uint32_t)) 286 #if defined(_KERNEL) 287 # define EXT2_BLOCK_SIZE_BITS(s) ((s)->e2fs_blocksize_bits) 288 #else 289 # define EXT2_BLOCK_SIZE_BITS(s) ((s)->e2fs_log_bsize + 10) 290 #endif 291 #if defined(_KERNEL) 292 #define EXT2_ADDR_PER_BLOCK_BITS(s) (EXT2_SB(s)->s_addr_per_block_bits) 293 #define EXT2_INODE_SIZE(s) (EXT2_SB(s)->e2fs_isize) 294 #define EXT2_FIRST_INO(s) (EXT2_SB(s)->e2fs_first_inode) 295 #else 296 #define EXT2_INODE_SIZE(s) (((s)->s_rev_level == E2FS_REV0) ? \ 297 E2FS_REV0 : (s)->s_inode_size) 298 #define EXT2_FIRST_INO(s) (((s)->s_rev_level == E2FS_REV0) ? \ 299 E2FS_REV0 : (s)->e2fs_first_ino) 300 #endif 301 302 /* 303 * Macro-instructions used to manage fragments 304 */ 305 #define EXT2_MIN_FRAG_SIZE 1024 306 #define EXT2_MAX_FRAG_SIZE 4096 307 #define EXT2_MIN_FRAG_LOG_SIZE 10 308 #if defined(_KERNEL) 309 # define EXT2_FRAG_SIZE(s) (EXT2_SB(s)->e2fs_fsize) 310 # define EXT2_FRAGS_PER_BLOCK(s) (EXT2_SB(s)->e2fs_fpb) 311 #else 312 # define EXT2_FRAG_SIZE(s) (EXT2_MIN_FRAG_SIZE << (s)->e2fs_log_fsize) 313 # define EXT2_FRAGS_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / EXT2_FRAG_SIZE(s)) 314 #endif 315 316 /* 317 * Macro-instructions used to manage group descriptors 318 */ 319 #if defined(_KERNEL) 320 # define EXT2_BLOCKS_PER_GROUP(s) (EXT2_SB(s)->e2fs_bpg) 321 # define EXT2_DESC_PER_BLOCK(s) (EXT2_SB(s)->e2fs_descpb) 322 # define EXT2_DESC_PER_BLOCK_BITS(s) (EXT2_SB(s)->s_desc_per_block_bits) 323 #else 324 # define EXT2_BLOCKS_PER_GROUP(s) ((s)->e2fs_bpg) 325 # define EXT2_DESC_PER_BLOCK(s) (EXT2_BLOCK_SIZE(s) / sizeof (struct ext2_gd)) 326 327 #endif 328 329 #endif /* _LINUX_EXT2_FS_H */ 330