665435b214b1d939de438f1840f8ba9a0c8935cd
[dragonfly.git] / sys / vfs / gnu / ext2fs / fs.h
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 /*
8  * Copyright (c) 1982, 1986, 1993
9  *      The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)fs.h        8.7 (Berkeley) 4/19/94
40  * $FreeBSD: src/sys/gnu/ext2fs/fs.h,v 1.5.2.1 2000/11/11 13:12:45 bde Exp $
41  * $DragonFly: src/sys/vfs/gnu/ext2fs/fs.h,v 1.10 2006/05/05 21:15:09 dillon Exp $
42  */
43
44 /*
45  * Each disk drive contains some number of file systems.
46  * A file system consists of a number of cylinder groups.
47  * Each cylinder group has inodes and data.
48  *
49  * A file system is described by its super-block, which in turn
50  * describes the cylinder groups.  The super-block is critical
51  * data and is replicated in each cylinder group to protect against
52  * catastrophic loss.  This is done at `newfs' time and the critical
53  * super-block data does not change, so the copies need not be
54  * referenced further unless disaster strikes.
55  *
56  * The first boot and super blocks are given in absolute disk addresses.
57  * The byte-offset forms are preferred, as they don't imply a sector size.
58  */
59 #define BBSIZE          1024
60 #define SBSIZE          1024
61 #define BBOFF           ((off_t)(0))
62 #define SBOFF           ((off_t)(BBOFF + BBSIZE))
63 #define BBLOCK          ((daddr_t)(0))
64 #define SBLOCK          ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
65
66 /*
67  * The path name on which the file system is mounted is maintained
68  * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 
69  * the super block for this name.
70  */
71 #define MAXMNTLEN 512
72
73 /*
74  * Macros for access to superblock array structures
75  */
76
77 /*
78  * Convert cylinder group to base address of its global summary info.
79  */
80 #define fs_cs(fs, cgindx)      (((struct ext2_group_desc *) \
81         (fs->s_group_desc[cgindx / EXT2_DESC_PER_BLOCK(fs)]->b_data)) \
82                 [cgindx % EXT2_DESC_PER_BLOCK(fs)])
83
84 /*
85  * Turn file system block numbers into disk block addresses.
86  * This maps file system blocks to device size blocks.
87  */
88 #define fsbtodb(fs, b)  ((b) << ((fs)->s_fsbtodb))
89 #define dbtofsb(fs, b)  ((b) >> ((fs)->s_fsbtodb))
90
91 /* get group containing inode */
92 #define ino_to_cg(fs, x)        (((x) - 1) / EXT2_INODES_PER_GROUP(fs))
93
94 /* get block containing inode from its number x */
95 #define ino_to_fsba(fs, x)      (fs_cs(fs, ino_to_cg(fs, x)).bg_inode_table + \
96         (((x)-1) % EXT2_INODES_PER_GROUP(fs))/EXT2_INODES_PER_BLOCK(fs))
97
98 /* get offset for inode in block */
99 #define ino_to_fsbo(fs, x)      ((x-1) % EXT2_INODES_PER_BLOCK(fs))
100
101 /*
102  * Give cylinder group number for a file system block.
103  * Give cylinder group block number for a file system block.
104  */
105 #define dtog(fs, d)     (((d) - fs->s_es->s_first_data_block) / \
106                         EXT2_BLOCKS_PER_GROUP(fs))
107 #define dtogd(fs, d)    (((d) - fs->s_es->s_first_data_block) % \
108                         EXT2_BLOCKS_PER_GROUP(fs))
109
110 /*
111  * The following macros optimize certain frequently calculated
112  * quantities by using shifts and masks in place of divisions
113  * modulos and multiplications.
114  */
115 #define blkoff(fs, loc)         /* calculates (loc % fs->fs_bsize) */ \
116         ((loc) & (fs)->s_qbmask)
117
118 #define lblktosize(fs, blk)     /* calculates (blk * fs->fs_bsize) */ \
119         ((blk) << (fs->s_bshift))
120
121 /*
122  * Used when converting logical blocks to offsets for getblk, bread,
123  * etc.
124  */
125 #define lblktodoff(fs, blk)     /* calculates (blk * fs->fs_bsize) */ \
126         ((off_t)(blk) << (fs)->s_bshift)
127 #define fsbtodoff(fs, blk)      /* calculates (blk * fs->fs_fsize) */ \
128         ((off_t)(blk) * (fs)->s_frag_size)
129 #define dofftofsb(fs, blk)      /* calculates blk / fs->fs_fsize */   \
130         ((daddr_t)((blk) / (fs)->s_frag_size))
131 #define dbtodoff(fs, b)         /* calculates diskblk * sectorsize */ \
132         ((off_t)(b) * ((fs)->s_frag_size >> (fs)->s_fsbtodb))
133
134 #define lblkno(fs, loc)         /* calculates (loc / fs->fs_bsize) */ \
135         ((loc) >> (fs->s_bshift))
136
137 /* no fragments -> logical block number equal # of frags */
138 #define numfrags(fs, loc)       /* calculates (loc / fs->fs_fsize) */ \
139         ((loc) >> (fs->s_bshift))
140
141 #define fragroundup(fs, size)   /* calculates roundup(size, fs->fs_fsize) */ \
142         roundup(size, fs->s_frag_size)
143         /* was (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask) */
144
145 /*
146  * Determining the size of a file block in the file system.
147  * easy w/o fragments
148  */
149 #define blksize(fs, ip, lbn) ((fs)->s_frag_size)
150
151 /*
152  * INOPB is the number of inodes in a secondary storage block.
153  */
154 #define INOPB(fs)       EXT2_INODES_PER_BLOCK(fs)
155
156 /*
157  * NINDIR is the number of indirects in a file system block.
158  */
159 #define NINDIR(fs)      (EXT2_ADDR_PER_BLOCK(fs))
160
161 extern int inside[], around[];
162 extern u_char *fragtbl[];
163
164 /* a few remarks about superblock locking/unlocking
165  * Linux provides special routines for doing so
166  * I haven't figured out yet what BSD does
167  * I think I'll try a VOP_LOCK/VOP_UNLOCK on the device vnode
168  */
169 #define  DEVVP(inode)           (VFSTOEXT2(ITOV(inode)->v_mount)->um_devvp)
170 #define  lock_super(devvp)      vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY)
171 #define  unlock_super(devvp)    VOP_UNLOCK(devvp, 0)
172
173 /*
174  * To lock a buffer, set the B_LOCKED flag and then brelse() it. To unlock,
175  * reset the B_LOCKED flag and brelse() the buffer back on the LRU list
176  */
177 #define LCK_BUF(bp) { \
178         crit_enter(); \
179         (bp)->b_flags |= B_LOCKED; \
180         crit_exit(); \
181         brelse(bp); \
182 }
183
184 #define ULCK_BUF(bp) { \
185         long flags; \
186         crit_enter(); \
187         flags = (bp)->b_flags; \
188         (bp)->b_flags &= ~(B_DIRTY | B_LOCKED); \
189         BUF_LOCK(bp, LK_EXCLUSIVE); \
190         bremfree(bp); \
191         crit_exit(); \
192         if (flags & B_DIRTY) \
193                 bwrite(bp); \
194         else \
195                 brelse(bp); \
196 }