fs.h revision 48225
112115Sdyson/* 212115Sdyson * modified for EXT2FS support in Lites 1.1 312115Sdyson * 412115Sdyson * Aug 1995, Godmar Back (gback@cs.utah.edu) 512115Sdyson * University of Utah, Department of Computer Science 612115Sdyson */ 712115Sdyson/* 812115Sdyson * Copyright (c) 1982, 1986, 1993 912115Sdyson * The Regents of the University of California. All rights reserved. 1012115Sdyson * 1112115Sdyson * Redistribution and use in source and binary forms, with or without 1212115Sdyson * modification, are permitted provided that the following conditions 1312115Sdyson * are met: 1412115Sdyson * 1. Redistributions of source code must retain the above copyright 1512115Sdyson * notice, this list of conditions and the following disclaimer. 1612115Sdyson * 2. Redistributions in binary form must reproduce the above copyright 1712115Sdyson * notice, this list of conditions and the following disclaimer in the 1812115Sdyson * documentation and/or other materials provided with the distribution. 1912115Sdyson * 3. All advertising materials mentioning features or use of this software 2012115Sdyson * must display the following acknowledgement: 2112115Sdyson * This product includes software developed by the University of 2212115Sdyson * California, Berkeley and its contributors. 2312115Sdyson * 4. Neither the name of the University nor the names of its contributors 2412115Sdyson * may be used to endorse or promote products derived from this software 2512115Sdyson * without specific prior written permission. 2612115Sdyson * 2712115Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2812115Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2912115Sdyson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3012115Sdyson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3112115Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3212115Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3312115Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3412115Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3512115Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3612115Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3712115Sdyson * SUCH DAMAGE. 3812115Sdyson * 3912115Sdyson * @(#)fs.h 8.7 (Berkeley) 4/19/94 4012115Sdyson */ 4112115Sdyson 4212115Sdyson/* 4312115Sdyson * Each disk drive contains some number of file systems. 4412115Sdyson * A file system consists of a number of cylinder groups. 4512115Sdyson * Each cylinder group has inodes and data. 4612115Sdyson * 4712115Sdyson * A file system is described by its super-block, which in turn 4812115Sdyson * describes the cylinder groups. The super-block is critical 4912115Sdyson * data and is replicated in each cylinder group to protect against 5012115Sdyson * catastrophic loss. This is done at `newfs' time and the critical 5112115Sdyson * super-block data does not change, so the copies need not be 5212115Sdyson * referenced further unless disaster strikes. 5312115Sdyson * 5412115Sdyson * The first boot and super blocks are given in absolute disk addresses. 5512115Sdyson * The byte-offset forms are preferred, as they don't imply a sector size. 5612115Sdyson */ 5712115Sdyson#define BBSIZE 1024 5812115Sdyson#define SBSIZE 1024 5912115Sdyson#define BBOFF ((off_t)(0)) 6012115Sdyson#define SBOFF ((off_t)(BBOFF + BBSIZE)) 6112115Sdyson#define BBLOCK ((daddr_t)(0)) 6212115Sdyson#define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE)) 6312115Sdyson 6412115Sdyson/* 6512115Sdyson * The path name on which the file system is mounted is maintained 6612115Sdyson * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 6712115Sdyson * the super block for this name. 6812115Sdyson */ 6912115Sdyson#define MAXMNTLEN 512 7012115Sdyson 7112115Sdyson/* 7212115Sdyson * Macros for access to superblock array structures 7312115Sdyson */ 7412115Sdyson 7512115Sdyson/* 7612115Sdyson * Convert cylinder group to base address of its global summary info. 7712115Sdyson */ 7812115Sdyson#define fs_cs(fs, cgindx) (((struct ext2_group_desc *) \ 7912115Sdyson (fs->s_group_desc[cgindx / EXT2_DESC_PER_BLOCK(fs)]->b_data)) \ 8012115Sdyson [cgindx % EXT2_DESC_PER_BLOCK(fs)]) 8112115Sdyson 8212115Sdyson/* 8312115Sdyson * Turn file system block numbers into disk block addresses. 8412115Sdyson * This maps file system blocks to device size blocks. 8512115Sdyson */ 8612115Sdyson#define fsbtodb(fs, b) ((b) << ((fs)->s_fsbtodb)) 8712115Sdyson#define dbtofsb(fs, b) ((b) >> ((fs)->s_fsbtodb)) 8812115Sdyson 8912115Sdyson/* get group containing inode */ 9012115Sdyson#define ino_to_cg(fs, x) (((x) - 1) / EXT2_INODES_PER_GROUP(fs)) 9112115Sdyson 9212115Sdyson/* get block containing inode from its number x */ 9312115Sdyson#define ino_to_fsba(fs, x) fs_cs(fs, ino_to_cg(fs, x)).bg_inode_table + \ 9412115Sdyson (((x)-1) % EXT2_INODES_PER_GROUP(fs))/EXT2_INODES_PER_BLOCK(fs) 9512115Sdyson 9612115Sdyson/* get offset for inode in block */ 9712115Sdyson#define ino_to_fsbo(fs, x) ((x-1) % EXT2_INODES_PER_BLOCK(fs)) 9812115Sdyson 9912115Sdyson/* 10012115Sdyson * Give cylinder group number for a file system block. 10112115Sdyson * Give cylinder group block number for a file system block. 10212115Sdyson */ 10312115Sdyson#define dtog(fs, d) (((d) - fs->s_es->s_first_data_block) / \ 10412115Sdyson EXT2_BLOCKS_PER_GROUP(fs)) 10512115Sdyson#define dtogd(fs, d) (((d) - fs->s_es->s_first_data_block) % \ 10612115Sdyson EXT2_BLOCKS_PER_GROUP(fs)) 10712115Sdyson 10812115Sdyson/* 10912115Sdyson * The following macros optimize certain frequently calculated 11012115Sdyson * quantities by using shifts and masks in place of divisions 11112115Sdyson * modulos and multiplications. 11212115Sdyson */ 11312115Sdyson#define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \ 11412115Sdyson ((loc) & (fs)->s_qbmask) 11512115Sdyson 11612115Sdyson#define lblktosize(fs, blk) /* calculates (blk * fs->fs_bsize) */ \ 11712115Sdyson ((blk) << (fs->s_bshift)) 11812115Sdyson 11912115Sdyson#define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \ 12012115Sdyson ((loc) >> (fs->s_bshift)) 12112115Sdyson 12212115Sdyson/* no fragments -> logical block number equal # of frags */ 12312115Sdyson#define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \ 12412115Sdyson ((loc) >> (fs->s_bshift)) 12512115Sdyson 12612115Sdyson#define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \ 12712115Sdyson roundup(size, fs->s_frag_size) 12812115Sdyson /* was (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask) */ 12912115Sdyson 13012115Sdyson/* 13112115Sdyson * Determining the size of a file block in the file system. 13212115Sdyson * easy w/o fragments 13312115Sdyson */ 13412115Sdyson#define blksize(fs, ip, lbn) ((fs)->s_frag_size) 13512115Sdyson 13612115Sdyson/* 13712115Sdyson * INOPB is the number of inodes in a secondary storage block. 13812115Sdyson */ 13912115Sdyson#define INOPB(fs) EXT2_INODES_PER_BLOCK(fs) 14012115Sdyson 14112115Sdyson/* 14212115Sdyson * NINDIR is the number of indirects in a file system block. 14312115Sdyson */ 14412115Sdyson#define NINDIR(fs) (EXT2_ADDR_PER_BLOCK(fs)) 14512115Sdyson 14612115Sdysonextern int inside[], around[]; 14712115Sdysonextern u_char *fragtbl[]; 14812115Sdyson 14912115Sdyson/* a few remarks about superblock locking/unlocking 15012115Sdyson * Linux provides special routines for doing so 15112115Sdyson * I haven't figured out yet what BSD does 15212115Sdyson * I think I'll try a VOP_LOCK/VOP_UNLOCK on the device vnode 15312115Sdyson */ 15412115Sdyson#define DEVVP(inode) (VFSTOUFS(ITOV(inode)->v_mount)->um_devvp) 15522521Sdyson#define lock_super(devvp) vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, curproc) 15622521Sdyson#define unlock_super(devvp) VOP_UNLOCK(devvp, 0, curproc) 15712115Sdyson 15827881Sdyson/* 15927881Sdyson * To lock a buffer, set the B_LOCKED flag and then brelse() it. To unlock, 16027881Sdyson * reset the B_LOCKED flag and brelse() the buffer back on the LRU list 16127881Sdyson */ 16227881Sdyson#define LCK_BUF(bp) { \ 16327881Sdyson int s; \ 16427881Sdyson s = splbio(); \ 16527881Sdyson (bp)->b_flags |= B_LOCKED; \ 16627881Sdyson splx(s); \ 16727881Sdyson brelse(bp); \ 16827881Sdyson} 16927881Sdyson 17027881Sdyson#define ULCK_BUF(bp) { \ 17139924Sbde long flags; \ 17227881Sdyson int s; \ 17327881Sdyson s = splbio(); \ 17439924Sbde flags = (bp)->b_flags; \ 17539924Sbde (bp)->b_flags &= ~(B_DIRTY | B_LOCKED); \ 17648225Smckusick BUF_LOCK(bp, LK_EXCLUSIVE); \ 17739924Sbde bremfree(bp); \ 17827881Sdyson splx(s); \ 17939924Sbde if (flags & B_DIRTY) \ 18039924Sbde bdwrite(bp); \ 18139924Sbde else \ 18239924Sbde brelse(bp); \ 18327881Sdyson} 184