fs.h revision 68568
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 4068568Sbde * $FreeBSD: head/sys/gnu/fs/ext2fs/fs.h 68568 2000-11-10 14:54:15Z bde $ 4112115Sdyson */ 4212115Sdyson 4312115Sdyson/* 4412115Sdyson * Each disk drive contains some number of file systems. 4512115Sdyson * A file system consists of a number of cylinder groups. 4612115Sdyson * Each cylinder group has inodes and data. 4712115Sdyson * 4812115Sdyson * A file system is described by its super-block, which in turn 4912115Sdyson * describes the cylinder groups. The super-block is critical 5012115Sdyson * data and is replicated in each cylinder group to protect against 5112115Sdyson * catastrophic loss. This is done at `newfs' time and the critical 5212115Sdyson * super-block data does not change, so the copies need not be 5312115Sdyson * referenced further unless disaster strikes. 5412115Sdyson * 5512115Sdyson * The first boot and super blocks are given in absolute disk addresses. 5612115Sdyson * The byte-offset forms are preferred, as they don't imply a sector size. 5712115Sdyson */ 5812115Sdyson#define BBSIZE 1024 5912115Sdyson#define SBSIZE 1024 6012115Sdyson#define BBOFF ((off_t)(0)) 6112115Sdyson#define SBOFF ((off_t)(BBOFF + BBSIZE)) 6212115Sdyson#define BBLOCK ((daddr_t)(0)) 6312115Sdyson#define SBLOCK ((daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE)) 6412115Sdyson 6512115Sdyson/* 6612115Sdyson * The path name on which the file system is mounted is maintained 6712115Sdyson * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in 6812115Sdyson * the super block for this name. 6912115Sdyson */ 7012115Sdyson#define MAXMNTLEN 512 7112115Sdyson 7212115Sdyson/* 7312115Sdyson * Macros for access to superblock array structures 7412115Sdyson */ 7512115Sdyson 7612115Sdyson/* 7712115Sdyson * Convert cylinder group to base address of its global summary info. 7812115Sdyson */ 7912115Sdyson#define fs_cs(fs, cgindx) (((struct ext2_group_desc *) \ 8012115Sdyson (fs->s_group_desc[cgindx / EXT2_DESC_PER_BLOCK(fs)]->b_data)) \ 8112115Sdyson [cgindx % EXT2_DESC_PER_BLOCK(fs)]) 8212115Sdyson 8312115Sdyson/* 8412115Sdyson * Turn file system block numbers into disk block addresses. 8512115Sdyson * This maps file system blocks to device size blocks. 8612115Sdyson */ 8712115Sdyson#define fsbtodb(fs, b) ((b) << ((fs)->s_fsbtodb)) 8812115Sdyson#define dbtofsb(fs, b) ((b) >> ((fs)->s_fsbtodb)) 8912115Sdyson 9012115Sdyson/* get group containing inode */ 9112115Sdyson#define ino_to_cg(fs, x) (((x) - 1) / EXT2_INODES_PER_GROUP(fs)) 9212115Sdyson 9312115Sdyson/* get block containing inode from its number x */ 9412115Sdyson#define ino_to_fsba(fs, x) fs_cs(fs, ino_to_cg(fs, x)).bg_inode_table + \ 9512115Sdyson (((x)-1) % EXT2_INODES_PER_GROUP(fs))/EXT2_INODES_PER_BLOCK(fs) 9612115Sdyson 9712115Sdyson/* get offset for inode in block */ 9812115Sdyson#define ino_to_fsbo(fs, x) ((x-1) % EXT2_INODES_PER_BLOCK(fs)) 9912115Sdyson 10012115Sdyson/* 10112115Sdyson * Give cylinder group number for a file system block. 10212115Sdyson * Give cylinder group block number for a file system block. 10312115Sdyson */ 10412115Sdyson#define dtog(fs, d) (((d) - fs->s_es->s_first_data_block) / \ 10512115Sdyson EXT2_BLOCKS_PER_GROUP(fs)) 10612115Sdyson#define dtogd(fs, d) (((d) - fs->s_es->s_first_data_block) % \ 10712115Sdyson EXT2_BLOCKS_PER_GROUP(fs)) 10812115Sdyson 10912115Sdyson/* 11012115Sdyson * The following macros optimize certain frequently calculated 11112115Sdyson * quantities by using shifts and masks in place of divisions 11212115Sdyson * modulos and multiplications. 11312115Sdyson */ 11412115Sdyson#define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \ 11512115Sdyson ((loc) & (fs)->s_qbmask) 11612115Sdyson 11712115Sdyson#define lblktosize(fs, blk) /* calculates (blk * fs->fs_bsize) */ \ 11812115Sdyson ((blk) << (fs->s_bshift)) 11912115Sdyson 12012115Sdyson#define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \ 12112115Sdyson ((loc) >> (fs->s_bshift)) 12212115Sdyson 12312115Sdyson/* no fragments -> logical block number equal # of frags */ 12412115Sdyson#define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \ 12512115Sdyson ((loc) >> (fs->s_bshift)) 12612115Sdyson 12712115Sdyson#define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \ 12812115Sdyson roundup(size, fs->s_frag_size) 12912115Sdyson /* was (((size) + (fs)->fs_qfmask) & (fs)->fs_fmask) */ 13012115Sdyson 13112115Sdyson/* 13212115Sdyson * Determining the size of a file block in the file system. 13312115Sdyson * easy w/o fragments 13412115Sdyson */ 13512115Sdyson#define blksize(fs, ip, lbn) ((fs)->s_frag_size) 13612115Sdyson 13712115Sdyson/* 13812115Sdyson * INOPB is the number of inodes in a secondary storage block. 13912115Sdyson */ 14012115Sdyson#define INOPB(fs) EXT2_INODES_PER_BLOCK(fs) 14112115Sdyson 14212115Sdyson/* 14312115Sdyson * NINDIR is the number of indirects in a file system block. 14412115Sdyson */ 14512115Sdyson#define NINDIR(fs) (EXT2_ADDR_PER_BLOCK(fs)) 14612115Sdyson 14712115Sdysonextern int inside[], around[]; 14812115Sdysonextern u_char *fragtbl[]; 14912115Sdyson 15012115Sdyson/* a few remarks about superblock locking/unlocking 15112115Sdyson * Linux provides special routines for doing so 15212115Sdyson * I haven't figured out yet what BSD does 15312115Sdyson * I think I'll try a VOP_LOCK/VOP_UNLOCK on the device vnode 15412115Sdyson */ 15512115Sdyson#define DEVVP(inode) (VFSTOUFS(ITOV(inode)->v_mount)->um_devvp) 15622521Sdyson#define lock_super(devvp) vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, curproc) 15722521Sdyson#define unlock_super(devvp) VOP_UNLOCK(devvp, 0, curproc) 15812115Sdyson 15927881Sdyson/* 16027881Sdyson * To lock a buffer, set the B_LOCKED flag and then brelse() it. To unlock, 16127881Sdyson * reset the B_LOCKED flag and brelse() the buffer back on the LRU list 16227881Sdyson */ 16327881Sdyson#define LCK_BUF(bp) { \ 16427881Sdyson int s; \ 16527881Sdyson s = splbio(); \ 16627881Sdyson (bp)->b_flags |= B_LOCKED; \ 16727881Sdyson splx(s); \ 16827881Sdyson brelse(bp); \ 16927881Sdyson} 17027881Sdyson 17127881Sdyson#define ULCK_BUF(bp) { \ 17239924Sbde long flags; \ 17327881Sdyson int s; \ 17427881Sdyson s = splbio(); \ 17539924Sbde flags = (bp)->b_flags; \ 17639924Sbde (bp)->b_flags &= ~(B_DIRTY | B_LOCKED); \ 17748225Smckusick BUF_LOCK(bp, LK_EXCLUSIVE); \ 17839924Sbde bremfree(bp); \ 17927881Sdyson splx(s); \ 18039924Sbde if (flags & B_DIRTY) \ 18168568Sbde bwrite(bp); \ 18239924Sbde else \ 18339924Sbde brelse(bp); \ 18427881Sdyson} 185