2 * modified for EXT2FS support in Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1982, 1986, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)fs.h 8.7 (Berkeley) 4/19/94
40 * Each disk drive contains some number of file systems.
41 * A file system consists of a number of cylinder groups.
42 * Each cylinder group has inodes and data.
44 * A file system is described by its super-block, which in turn
45 * describes the cylinder groups. The super-block is critical
46 * data and is replicated in each cylinder group to protect against
47 * catastrophic loss. This is done at `newfs' time and the critical
48 * super-block data does not change, so the copies need not be
49 * referenced further unless disaster strikes.
51 * The first boot and super blocks are given in absolute disk addresses.
52 * The byte-offset forms are preferred, as they don't imply a sector size.
58 * The path name on which the file system is mounted is maintained
59 * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
60 * the super block for this name.
65 * Macros for access to superblock array structures
69 * Convert cylinder group to base address of its global summary info.
71 #define fs_cs(fs, cgindx) (((struct ext2_group_desc *) \
72 (fs->s_group_desc[cgindx / EXT2_DESC_PER_BLOCK(fs)]->b_data)) \
73 [cgindx % EXT2_DESC_PER_BLOCK(fs)])
76 * Turn file system block numbers into disk block addresses.
77 * This maps file system blocks to device size blocks.
79 #define fsbtodb(fs, b) ((b) << ((fs)->s_fsbtodb))
80 #define dbtofsb(fs, b) ((b) >> ((fs)->s_fsbtodb))
82 /* get group containing inode */
83 #define ino_to_cg(fs, x) (((x) - 1) / EXT2_INODES_PER_GROUP(fs))
85 /* get block containing inode from its number x */
86 #define ino_to_fsba(fs, x) fs_cs(fs, ino_to_cg(fs, x)).bg_inode_table + \
87 (((x)-1) % EXT2_INODES_PER_GROUP(fs))/EXT2_INODES_PER_BLOCK(fs)
89 /* get offset for inode in block */
90 #define ino_to_fsbo(fs, x) ((x-1) % EXT2_INODES_PER_BLOCK(fs))
93 * Give cylinder group number for a file system block.
94 * Give cylinder group block number for a file system block.
96 #define dtog(fs, d) (((d) - fs->s_es->s_first_data_block) / \
97 EXT2_BLOCKS_PER_GROUP(fs))
98 #define dtogd(fs, d) (((d) - fs->s_es->s_first_data_block) % \
99 EXT2_BLOCKS_PER_GROUP(fs))
102 * The following macros optimize certain frequently calculated
103 * quantities by using shifts and masks in place of divisions
104 * modulos and multiplications.
106 #define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
107 ((loc) & (fs)->s_qbmask)
109 #define lblktosize(fs, blk) /* calculates (blk * fs->fs_bsize) */ \
110 ((blk) << (fs->s_bshift))
112 #define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
113 ((loc) >> (fs->s_bshift))
115 /* no fragments -> logical block number equal # of frags */
116 #define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
117 ((loc) >> (fs->s_bshift))
119 #define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
120 roundup(size, fs->s_frag_size)
121 /* was (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask) */
124 * Determining the size of a file block in the file system.
127 #define blksize(fs, ip, lbn) ((fs)->s_frag_size)
130 * INOPB is the number of inodes in a secondary storage block.
132 #define INOPB(fs) EXT2_INODES_PER_BLOCK(fs)
135 * NINDIR is the number of indirects in a file system block.
137 #define NINDIR(fs) (EXT2_ADDR_PER_BLOCK(fs))
139 extern int inside[], around[];
140 extern u_char *fragtbl[];
142 /* a few remarks about superblock locking/unlocking
143 * Linux provides special routines for doing so
144 * I haven't figured out yet what BSD does
145 * I think I'll try a VOP_LOCK/VOP_UNLOCK on the device vnode
147 #define DEVVP(inode) (VFSTOEXT2(ITOV(inode)->v_mount)->um_devvp)
148 #define lock_super(devvp) vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, curthread)
149 #define unlock_super(devvp) VOP_UNLOCK(devvp, 0, curthread)
152 * Historically, ext2fs kept it's metadata buffers on the LOCKED queue. Now,
153 * we change the lock owner to kern so that we may use it from contexts other
154 * than the one that originally locked it. When we are finished with the
155 * buffer, we release it, writing it first if it was dirty.
157 #define LCK_BUF(bp) { \
158 (bp)->b_flags |= B_PERSISTENT; \
162 #define ULCK_BUF(bp) { \
164 flags = (bp)->b_flags; \
165 (bp)->b_flags &= ~(B_DIRTY | B_PERSISTENT); \
166 if (flags & B_DIRTY) \