1N/A/*
1N/A * Mach Operating System
1N/A * Copyright (c) 1991,1990 Carnegie Mellon University
1N/A * All Rights Reserved.
1N/A *
1N/A * Permission to use, copy, modify and distribute this software and its
1N/A * documentation is hereby granted, provided that both the copyright
1N/A * notice and this permission notice appear in all copies of the
1N/A * software, derivative works or modified versions, and any portions
1N/A * thereof, and that both notices appear in supporting documentation.
1N/A *
1N/A * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1N/A * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1N/A * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1N/A *
1N/A * Carnegie Mellon requests users of this software to return to
1N/A *
1N/A * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
1N/A * School of Computer Science
1N/A * Carnegie Mellon University
1N/A * Pittsburgh PA 15213-3890
1N/A *
1N/A * any improvements or extensions that they make and grant Carnegie Mellon
1N/A * the rights to redistribute these changes.
1N/A */
1N/A/*
1N/A * Copyright (c) 1982, 1986 Regents of the University of California.
1N/A * All rights reserved.
1N/A *
1N/A * Redistribution and use in source and binary forms are permitted
1N/A * provided that the above copyright notice and this paragraph are
1N/A * duplicated in all such forms and that any documentation,
1N/A * advertising materials, and other materials related to such
1N/A * distribution and use acknowledge that the software was developed
1N/A * by the University of California, Berkeley. The name of the
1N/A * University may not be used to endorse or promote products derived
1N/A * from this software without specific prior written permission.
1N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1N/A * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1N/A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1N/A *
1N/A * @(#)fs.h 7.7 (Berkeley) 5/9/89
1N/A */
1N/A
1N/A/*
1N/A * Each disk drive contains some number of file systems.
1N/A * A file system consists of a number of cylinder groups.
1N/A * Each cylinder group has inodes and data.
1N/A *
1N/A * A file system is described by its super-block, which in turn
1N/A * describes the cylinder groups. The super-block is critical
1N/A * data and is replicated in each cylinder group to protect against
1N/A * catastrophic loss. This is done at `newfs' time and the critical
1N/A * super-block data does not change, so the copies need not be
1N/A * referenced further unless disaster strikes.
1N/A *
1N/A * For file system fs, the offsets of the various blocks of interest
1N/A * are given in the super block as:
1N/A * [fs->fs_sblkno] Super-block
1N/A * [fs->fs_cblkno] Cylinder group block
1N/A * [fs->fs_iblkno] Inode blocks
1N/A * [fs->fs_dblkno] Data blocks
1N/A * The beginning of cylinder group cg in fs, is given by
1N/A * the ``cgbase(fs, cg)'' macro.
1N/A *
1N/A * The first boot and super blocks are given in absolute disk addresses.
1N/A * The byte-offset forms are preferred, as they don't imply a sector size.
1N/A */
1N/A#define BBSIZE 8192
1N/A#define SBSIZE 8192
1N/A#define BBOFF ((mach_off_t)(0))
1N/A#define SBOFF ((mach_off_t)(BBOFF + BBSIZE))
1N/A#define BBLOCK ((mach_daddr_t)(0))
1N/A#define SBLOCK ((mach_daddr_t)(BBLOCK + BBSIZE / DEV_BSIZE))
1N/A
1N/A/*
1N/A * Addresses stored in inodes are capable of addressing fragments
1N/A * of `blocks'. File system blocks of at most size MAXBSIZE can
1N/A * be optionally broken into 2, 4, or 8 pieces, each of which is
1N/A * addressible; these pieces may be DEV_BSIZE, or some multiple of
1N/A * a DEV_BSIZE unit.
1N/A *
1N/A * Large files consist of exclusively large data blocks. To avoid
1N/A * undue wasted disk space, the last data block of a small file may be
1N/A * allocated as only as many fragments of a large block as are
1N/A * necessary. The file system format retains only a single pointer
1N/A * to such a fragment, which is a piece of a single large block that
1N/A * has been divided. The size of such a fragment is determinable from
1N/A * information in the inode, using the ``blksize(fs, ip, lbn)'' macro.
1N/A *
1N/A * The file system records space availability at the fragment level;
1N/A * to determine block availability, aligned fragments are examined.
1N/A *
1N/A * The root inode is the root of the file system.
1N/A * Inode 0 can't be used for normal purposes and
1N/A * historically bad blocks were linked to inode 1,
1N/A * thus the root inode is 2. (inode 1 is no longer used for
1N/A * this purpose, however numerous dump tapes make this
1N/A * assumption, so we are stuck with it)
1N/A */
1N/A#define ROOTINO ((mach_ino_t)2) /* i number of all roots */
1N/A
1N/A/*
1N/A * MINBSIZE is the smallest allowable block size.
1N/A * In order to insure that it is possible to create files of size
1N/A * 2^32 with only two levels of indirection, MINBSIZE is set to 4096.
1N/A * MINBSIZE must be big enough to hold a cylinder group block,
1N/A * thus changes to (struct cg) must keep its size within MINBSIZE.
1N/A * Note that super blocks are always of size SBSIZE,
1N/A * and that both SBSIZE and MAXBSIZE must be >= MINBSIZE.
1N/A */
1N/A#define MINBSIZE 4096
1N/A
1N/A/*
1N/A * The path name on which the file system is mounted is maintained
1N/A * in fs_fsmnt. MAXMNTLEN defines the amount of space allocated in
1N/A * the super block for this name.
1N/A * The limit on the amount of summary information per file system
1N/A * is defined by MAXCSBUFS. It is currently parameterized for a
1N/A * maximum of two million cylinders.
1N/A */
1N/A#define MAXMNTLEN 512
1N/A#define MAXCSBUFS 32
1N/A
1N/A/*
1N/A * Per cylinder group information; summarized in blocks allocated
1N/A * from first cylinder group data blocks. These blocks have to be
1N/A * read in from fs_csaddr (size fs_cssize) in addition to the
1N/A * super block.
1N/A *
1N/A * N.B. sizeof(struct csum) must be a power of two in order for
1N/A * the ``fs_cs'' macro to work (see below).
1N/A */
1N/Astruct csum
1N/A {
1N/A int cs_ndir; /* number of directories */
1N/A int cs_nbfree; /* number of free blocks */
1N/A int cs_nifree; /* number of free inodes */
1N/A int cs_nffree; /* number of free frags */
1N/A };
1N/A
1N/A/*
1N/A * Super block for a file system.
1N/A */
1N/A#define FS_MAGIC 0x011954
1N/Astruct fs
1N/A {
1N/A int xxx1; /* struct fs *fs_link; */
1N/A int xxx2; /* struct fs *fs_rlink; */
1N/A mach_daddr_t fs_sblkno; /* addr of super-block in filesys */
1N/A mach_daddr_t fs_cblkno; /* offset of cyl-block in filesys */
1N/A mach_daddr_t fs_iblkno; /* offset of inode-blocks in filesys */
1N/A mach_daddr_t fs_dblkno; /* offset of first data after cg */
1N/A int fs_cgoffset; /* cylinder group offset in cylinder */
1N/A int fs_cgmask; /* used to calc mod fs_ntrak */
1N/A mach_time_t fs_time; /* last time written */
1N/A int fs_size; /* number of blocks in fs */
1N/A int fs_dsize; /* number of data blocks in fs */
1N/A int fs_ncg; /* number of cylinder groups */
1N/A int fs_bsize; /* size of basic blocks in fs */
1N/A int fs_fsize; /* size of frag blocks in fs */
1N/A int fs_frag; /* number of frags in a block in fs */
1N/A/* these are configuration parameters */
1N/A int fs_minfree; /* minimum percentage of free blocks */
1N/A int fs_rotdelay; /* num of ms for optimal next block */
1N/A int fs_rps; /* disk revolutions per second */
1N/A/* these fields can be computed from the others */
1N/A int fs_bmask; /* ``blkoff'' calc of blk offsets */
1N/A int fs_fmask; /* ``fragoff'' calc of frag offsets */
1N/A int fs_bshift; /* ``lblkno'' calc of logical blkno */
1N/A int fs_fshift; /* ``numfrags'' calc number of frags */
1N/A/* these are configuration parameters */
1N/A int fs_maxcontig; /* max number of contiguous blks */
1N/A int fs_maxbpg; /* max number of blks per cyl group */
1N/A/* these fields can be computed from the others */
1N/A int fs_fragshift; /* block to frag shift */
1N/A int fs_fsbtodb; /* fsbtodb and dbtofsb shift constant */
1N/A int fs_sbsize; /* actual size of super block */
1N/A int fs_csmask; /* csum block offset */
1N/A int fs_csshift; /* csum block number */
1N/A int fs_nindir; /* value of NINDIR */
1N/A int fs_inopb; /* value of INOPB */
1N/A int fs_nspf; /* value of NSPF */
1N/A/* yet another configuration parameter */
1N/A int fs_optim; /* optimization preference, see below */
1N/A/* these fields are derived from the hardware */
1N/A int fs_npsect; /* # sectors/track including spares */
1N/A int fs_interleave; /* hardware sector interleave */
1N/A int fs_trackskew; /* sector 0 skew, per track */
1N/A int fs_headswitch; /* head switch time, usec */
1N/A int fs_trkseek; /* track-to-track seek, usec */
1N/A/* sizes determined by number of cylinder groups and their sizes */
1N/A mach_daddr_t fs_csaddr; /* blk addr of cyl grp summary area */
1N/A int fs_cssize; /* size of cyl grp summary area */
1N/A int fs_cgsize; /* cylinder group size */
1N/A/* these fields are derived from the hardware */
1N/A int fs_ntrak; /* tracks per cylinder */
1N/A int fs_nsect; /* sectors per track */
1N/A int fs_spc; /* sectors per cylinder */
1N/A/* this comes from the disk driver partitioning */
1N/A int fs_ncyl; /* cylinders in file system */
1N/A/* these fields can be computed from the others */
1N/A int fs_cpg; /* cylinders per group */
1N/A int fs_ipg; /* inodes per group */
1N/A int fs_fpg; /* blocks per group * fs_frag */
1N/A/* this data must be re-computed after crashes */
1N/A struct csum fs_cstotal; /* cylinder summary information */
1N/A/* these fields are cleared at mount time */
1N/A char fs_fmod; /* super block modified flag */
1N/A char fs_clean; /* file system is clean flag */
1N/A char fs_ronly; /* mounted read-only flag */
1N/A char fs_flags; /* currently unused flag */
1N/A char fs_fsmnt[MAXMNTLEN]; /* name mounted on */
1N/A/* these fields retain the current block allocation info */
1N/A int fs_cgrotor; /* last cg searched */
1N/A#if 1
1N/A int was_fs_csp[MAXCSBUFS];
1N/A#else
1N/A struct csum *fs_csp[MAXCSBUFS]; /* list of fs_cs info buffers */
1N/A#endif
1N/A int fs_cpc; /* cyl per cycle in postbl */
1N/A short fs_opostbl[16][8]; /* old rotation block list head */
1N/A long fs_sparecon[50]; /* reserved for future constants */
1N/A long fs_contigsumsize; /* size of cluster summary array */
1N/A long fs_maxsymlinklen; /* max length of an internal symlink */
1N/A long fs_inodefmt; /* format of on-disk inodes */
1N/A quad fs_maxfilesize; /* maximum representable file size */
1N/A quad fs_qbmask; /* ~fs_bmask - for use with quad size */
1N/A quad fs_qfmask; /* ~fs_fmask - for use with quad size */
1N/A long fs_state; /* validate fs_clean field */
1N/A int fs_postblformat; /* format of positional layout tables */
1N/A int fs_nrpos; /* number of rotaional positions */
1N/A int fs_postbloff; /* (short) rotation block list head */
1N/A int fs_rotbloff; /* (u_char) blocks for each rotation */
1N/A int fs_magic; /* magic number */
1N/A u_char fs_space[1]; /* list of blocks for each rotation */
1N/A/* actually longer */
1N/A };
1N/A/*
1N/A * Preference for optimization.
1N/A */
1N/A#define FS_OPTTIME 0 /* minimize allocation time */
1N/A#define FS_OPTSPACE 1 /* minimize disk fragmentation */
1N/A
1N/A/*
1N/A * Rotational layout table format types
1N/A */
1N/A#define FS_42POSTBLFMT -1 /* 4.2BSD rotational table format */
1N/A#define FS_DYNAMICPOSTBLFMT 1 /* dynamic rotational table format */
1N/A/*
1N/A * Macros for access to superblock array structures
1N/A */
1N/A#define fs_postbl(fs, cylno) \
1N/A (((fs)->fs_postblformat == FS_42POSTBLFMT) \
1N/A ? ((fs)->fs_opostbl[cylno]) \
1N/A : ((short *)((char *)(fs) + (fs)->fs_postbloff) + (cylno) * (fs)->fs_nrpos))
1N/A#define fs_rotbl(fs) \
1N/A (((fs)->fs_postblformat == FS_42POSTBLFMT) \
1N/A ? ((fs)->fs_space) \
1N/A : ((u_char *)((char *)(fs) + (fs)->fs_rotbloff)))
1N/A
1N/A/*
1N/A * Convert cylinder group to base address of its global summary info.
1N/A *
1N/A * N.B. This macro assumes that sizeof(struct csum) is a power of two.
1N/A */
1N/A#define fs_cs(fs, indx) \
1N/A fs_csp[(indx) >> (fs)->fs_csshift][(indx) & ~(fs)->fs_csmask]
1N/A
1N/A/*
1N/A * Cylinder group block for a file system.
1N/A */
1N/A#define CG_MAGIC 0x090255
1N/Astruct cg
1N/A {
1N/A int xxx1; /* struct cg *cg_link; */
1N/A int cg_magic; /* magic number */
1N/A mach_time_t cg_time; /* time last written */
1N/A int cg_cgx; /* we are the cgx'th cylinder group */
1N/A short cg_ncyl; /* number of cyl's this cg */
1N/A short cg_niblk; /* number of inode blocks this cg */
1N/A int cg_ndblk; /* number of data blocks this cg */
1N/A struct csum cg_cs; /* cylinder summary information */
1N/A int cg_rotor; /* position of last used block */
1N/A int cg_frotor; /* position of last used frag */
1N/A int cg_irotor; /* position of last used inode */
1N/A int cg_frsum[MAXFRAG]; /* counts of available frags */
1N/A int cg_btotoff; /* (long) block totals per cylinder */
1N/A int cg_boff; /* (short) free block positions */
1N/A int cg_iusedoff; /* (char) used inode map */
1N/A int cg_freeoff; /* (u_char) free block map */
1N/A int cg_nextfreeoff; /* (u_char) next available space */
1N/A int cg_sparecon[16]; /* reserved for future use */
1N/A u_char cg_space[1]; /* space for cylinder group maps */
1N/A/* actually longer */
1N/A };
1N/A/*
1N/A * Macros for access to cylinder group array structures
1N/A */
1N/A#define cg_blktot(cgp) \
1N/A (((cgp)->cg_magic != CG_MAGIC) \
1N/A ? (((struct ocg *)(cgp))->cg_btot) \
1N/A : ((int *)((char *)(cgp) + (cgp)->cg_btotoff)))
1N/A#define cg_blks(fs, cgp, cylno) \
1N/A (((cgp)->cg_magic != CG_MAGIC) \
1N/A ? (((struct ocg *)(cgp))->cg_b[cylno]) \
1N/A : ((short *)((char *)(cgp) + (cgp)->cg_boff) + (cylno) * (fs)->fs_nrpos))
1N/A#define cg_inosused(cgp) \
1N/A (((cgp)->cg_magic != CG_MAGIC) \
1N/A ? (((struct ocg *)(cgp))->cg_iused) \
1N/A : ((char *)((char *)(cgp) + (cgp)->cg_iusedoff)))
1N/A#define cg_blksfree(cgp) \
1N/A (((cgp)->cg_magic != CG_MAGIC) \
1N/A ? (((struct ocg *)(cgp))->cg_free) \
1N/A : ((u_char *)((char *)(cgp) + (cgp)->cg_freeoff)))
1N/A#define cg_chkmagic(cgp) \
1N/A ((cgp)->cg_magic == CG_MAGIC || ((struct ocg *)(cgp))->cg_magic == CG_MAGIC)
1N/A
1N/A/*
1N/A * The following structure is defined
1N/A * for compatibility with old file systems.
1N/A */
1N/Astruct ocg
1N/A {
1N/A int xxx1; /* struct ocg *cg_link; */
1N/A int xxx2; /* struct ocg *cg_rlink; */
1N/A mach_time_t cg_time; /* time last written */
1N/A int cg_cgx; /* we are the cgx'th cylinder group */
1N/A short cg_ncyl; /* number of cyl's this cg */
1N/A short cg_niblk; /* number of inode blocks this cg */
1N/A int cg_ndblk; /* number of data blocks this cg */
1N/A struct csum cg_cs; /* cylinder summary information */
1N/A int cg_rotor; /* position of last used block */
1N/A int cg_frotor; /* position of last used frag */
1N/A int cg_irotor; /* position of last used inode */
1N/A int cg_frsum[8]; /* counts of available frags */
1N/A int cg_btot[32]; /* block totals per cylinder */
1N/A short cg_b[32][8]; /* positions of free blocks */
1N/A char cg_iused[256]; /* used inode map */
1N/A int cg_magic; /* magic number */
1N/A u_char cg_free[1]; /* free block map */
1N/A/* actually longer */
1N/A };
1N/A
1N/A/*
1N/A * Turn file system block numbers into disk block addresses.
1N/A * This maps file system blocks to device size blocks.
1N/A */
1N/A#define fsbtodb(fs, b) ((b) << (fs)->fs_fsbtodb)
1N/A#define dbtofsb(fs, b) ((b) >> (fs)->fs_fsbtodb)
1N/A
1N/A/*
1N/A * Cylinder group macros to locate things in cylinder groups.
1N/A * They calc file system addresses of cylinder group data structures.
1N/A */
1N/A#define cgbase(fs, c) ((mach_daddr_t)((fs)->fs_fpg * (c)))
1N/A#define cgstart(fs, c) \
1N/A (cgbase(fs, c) + (fs)->fs_cgoffset * ((c) & ~((fs)->fs_cgmask)))
1N/A#define cgsblock(fs, c) (cgstart(fs, c) + (fs)->fs_sblkno) /* super blk */
1N/A#define cgtod(fs, c) (cgstart(fs, c) + (fs)->fs_cblkno) /* cg block */
1N/A#define cgimin(fs, c) (cgstart(fs, c) + (fs)->fs_iblkno) /* inode blk */
1N/A#define cgdmin(fs, c) (cgstart(fs, c) + (fs)->fs_dblkno) /* 1st data */
1N/A
1N/A/*
1N/A * Macros for handling inode numbers:
1N/A * inode number to file system block offset.
1N/A * inode number to cylinder group number.
1N/A * inode number to file system block address.
1N/A */
1N/A#define itoo(fs, x) ((x) % INOPB(fs))
1N/A#define itog(fs, x) ((x) / (fs)->fs_ipg)
1N/A#define itod(fs, x) \
1N/A ((mach_daddr_t)(cgimin(fs, itog(fs, x)) + \
1N/A (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
1N/A
1N/A/*
1N/A * Give cylinder group number for a file system block.
1N/A * Give cylinder group block number for a file system block.
1N/A */
1N/A#define dtog(fs, d) ((d) / (fs)->fs_fpg)
1N/A#define dtogd(fs, d) ((d) % (fs)->fs_fpg)
1N/A
1N/A/*
1N/A * Extract the bits for a block from a map.
1N/A * Compute the cylinder and rotational position of a cyl block addr.
1N/A */
1N/A#define blkmap(fs, map, loc) \
1N/A (((map)[(loc) / NBBY] >> ((loc) % NBBY)) & (0xff >> (NBBY - (fs)->fs_frag)))
1N/A#define cbtocylno(fs, bno) \
1N/A ((bno) * NSPF(fs) / (fs)->fs_spc)
1N/A#define cbtorpos(fs, bno) \
1N/A (((bno) * NSPF(fs) % (fs)->fs_spc / (fs)->fs_nsect * (fs)->fs_trackskew + \
1N/A (bno) * NSPF(fs) % (fs)->fs_spc % (fs)->fs_nsect * (fs)->fs_interleave) % \
1N/A (fs)->fs_nsect * (fs)->fs_nrpos / (fs)->fs_npsect)
1N/A
1N/A/*
1N/A * The following macros optimize certain frequently calculated
1N/A * quantities by using shifts and masks in place of divisions
1N/A * modulos and multiplications.
1N/A */
1N/A#define blkoff(fs, loc) /* calculates (loc % fs->fs_bsize) */ \
1N/A ((loc) & ~(fs)->fs_bmask)
1N/A#define fragoff(fs, loc) /* calculates (loc % fs->fs_fsize) */ \
1N/A ((loc) & ~(fs)->fs_fmask)
1N/A#define lblkno(fs, loc) /* calculates (loc / fs->fs_bsize) */ \
1N/A ((loc) >> (fs)->fs_bshift)
1N/A#define numfrags(fs, loc) /* calculates (loc / fs->fs_fsize) */ \
1N/A ((loc) >> (fs)->fs_fshift)
1N/A#define blkroundup(fs, size) /* calculates roundup(size, fs->fs_bsize) */ \
1N/A (((size) + (fs)->fs_bsize - 1) & (fs)->fs_bmask)
1N/A#define fragroundup(fs, size) /* calculates roundup(size, fs->fs_fsize) */ \
1N/A (((size) + (fs)->fs_fsize - 1) & (fs)->fs_fmask)
1N/A#define fragstoblks(fs, frags) /* calculates (frags / fs->fs_frag) */ \
1N/A ((frags) >> (fs)->fs_fragshift)
1N/A#define blkstofrags(fs, blks) /* calculates (blks * fs->fs_frag) */ \
1N/A ((blks) << (fs)->fs_fragshift)
1N/A#define fragnum(fs, fsb) /* calculates (fsb % fs->fs_frag) */ \
1N/A ((fsb) & ((fs)->fs_frag - 1))
1N/A#define blknum(fs, fsb) /* calculates rounddown(fsb, fs->fs_frag) */ \
1N/A ((fsb) &~ ((fs)->fs_frag - 1))
1N/A
1N/A/*
1N/A * Determine the number of available frags given a
1N/A * percentage to hold in reserve
1N/A */
1N/A#define freespace(fs, percentreserved) \
1N/A (blkstofrags((fs), (fs)->fs_cstotal.cs_nbfree) + \
1N/A (fs)->fs_cstotal.cs_nffree - ((fs)->fs_dsize * (percentreserved) / 100))
1N/A
1N/A/*
1N/A * Determining the size of a file block in the file system.
1N/A */
1N/A#define blksize(fs, ip, lbn) \
1N/A (((lbn) >= NDADDR || (ip)->i_size >= ((lbn) + 1) << (fs)->fs_bshift) \
1N/A ? (fs)->fs_bsize \
1N/A : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
1N/A#define dblksize(fs, dip, lbn) \
1N/A (((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->fs_bshift) \
1N/A ? (fs)->fs_bsize \
1N/A : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
1N/A
1N/A/*
1N/A * Number of disk sectors per block; assumes DEV_BSIZE byte sector size.
1N/A */
1N/A#define NSPB(fs) ((fs)->fs_nspf << (fs)->fs_fragshift)
1N/A#define NSPF(fs) ((fs)->fs_nspf)
1N/A
1N/A/*
1N/A * INOPB is the number of inodes in a secondary storage block.
1N/A */
1N/A#define INOPB(fs) ((fs)->fs_inopb)
1N/A#define INOPF(fs) ((fs)->fs_inopb >> (fs)->fs_fragshift)
1N/A
1N/A/*
1N/A * NINDIR is the number of indirects in a file system block.
1N/A */
1N/A#define NINDIR(fs) ((fs)->fs_nindir)