Home | History | Annotate | Line # | Download | only in lfs
lfs.h revision 1.11
      1 /*	$NetBSD: lfs.h,v 1.11 1999/03/10 00:20:00 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*-
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs.h	8.9 (Berkeley) 5/8/95
     71  */
     72 
     73 /*
     74  * Compile-time options for LFS.
     75  */
     76 #define LFS_EAGAIN_FAIL          /* markv fail with EAGAIN if ino is locked */
     77 #define LFS_CONSERVATIVE_LOCK    /* lock ifile ino in lfs_segwrite, if safe */
     78 
     79 #define LFS_TOGGLE_SB           /* toggle between first 2 sbs for checkpoint */
     80 #define LFS_TRACK_IOS           /* attempt to avoid cleaning segments not yet fully written to disk */
     81 #define LFS_USEDIROP            /* use VDIROP segregation */
     82 #define LFS_STINGY_CLEAN        /* write only cleaned inodes when cleaning */
     83 #define LFS_STINGY_BLOCKS       /* write only cleaned blocks when cleaning */
     84 #define LFS_CANNOT_ROLLFW       /* No roll-forward agent exists */
     85 
     86 /* #define DEBUG_LFS */              /* Intensive debugging of LFS subsystem */
     87 
     88 /* #define LFS_ATIME_IFILE */         /* Store atime in Ifile, don't push */
     89 /* #define LFS_HONOR_RDONLY */        /* Don't write blocks if mounted ro */
     90 
     91 /*
     92  * Parameters and generic definitions
     93  */
     94 #define BW_CLEAN 1
     95 #define MIN_FREE_SEGS 4
     96 
     97 #ifndef LFS_ATIME_IFILE
     98 # define LFS_ITIMES(ip, acc, mod, cre) FFS_ITIMES((ip),(acc),(mod),(cre))
     99 #else
    100 # define LFS_ITIMES(ip, acc, mod, cre) {                                \
    101 	struct buf *ibp;						\
    102 	IFILE *ifp;							\
    103 									\
    104         if ((ip)->i_flag & IN_ACCESS) {                         	\
    105 	LFS_IENTRY(ifp, ip->i_lfs, ip->i_number, ibp);			\
    106        		ifp->if_atime = (mod);					\
    107        		VOP_BWRITE(bp);						\
    108 		(ip)->i_flag &= ~IN_ACCESS;				\
    109         }                                                       	\
    110         if ((ip)->i_flag & (IN_CHANGE | IN_UPDATE)) {       		\
    111                 (ip)->i_flag |= IN_MODIFIED;                            \
    112                 if ((ip)->i_flag & IN_UPDATE) {                         \
    113                         (ip)->i_ffs_mtime = (mod)->tv_sec;		\
    114                         (ip)->i_ffs_mtimensec = (mod)->tv_nsec;         \
    115                         (ip)->i_modrev++;                               \
    116                 }                                                       \
    117                 if ((ip)->i_flag & IN_CHANGE) {                         \
    118                         (ip)->i_ffs_ctime = (cre)->tv_sec;		\
    119                         (ip)->i_ffs_ctimensec = (cre)->tv_nsec;         \
    120                 }                                                       \
    121                 (ip)->i_flag &= ~(IN_CHANGE | IN_UPDATE);   		\
    122         }                                                               \
    123 }
    124 #endif
    125 
    126 #define WRITEINPROG(vp) (vp->v_dirtyblkhd.lh_first && !(VTOI(vp)->i_flag & (IN_MODIFIED|IN_CLEANING)))
    127 
    128 /* Here begins the berkeley code */
    129 
    130 #define	LFS_LABELPAD	8192		/* LFS label size */
    131 #define	LFS_SBPAD	8192		/* LFS superblock size */
    132 
    133 /*
    134  * XXX
    135  * This is a kluge and NEEDS to go away.
    136  *
    137  * Right now, ufs code handles most of the calls for directory operations
    138  * such as create, mkdir, link, etc.  As a result VOP_UPDATE is being
    139  * called with waitfor set (since ffs does these things synchronously).
    140  * Since LFS does not want to do these synchronously, we treat the last
    141  * argument to lfs_update as a set of flags.  If LFS_SYNC is set, then
    142  * the update should be synchronous, if not, do it asynchronously.
    143  * Unfortunately, this means that LFS won't work with NFS yet because
    144  * NFS goes through paths that will make normal calls to ufs which will
    145  * call lfs with a last argument of 1.
    146  */
    147 #define	LFS_SYNC	0x02
    148 
    149 /* On-disk and in-memory checkpoint segment usage structure. */
    150 typedef struct segusage SEGUSE;
    151 struct segusage {
    152 	u_int32_t su_nbytes;		/* number of live bytes */
    153 	u_int32_t su_lastmod;		/* SEGUSE last modified timestamp */
    154 	u_int16_t su_nsums;		/* number of summaries in segment */
    155 	u_int16_t su_ninos;		/* number of inode blocks in seg */
    156 
    157 #define	SEGUSE_ACTIVE		0x01	/* segment is currently being written */
    158 #define	SEGUSE_DIRTY		0x02	/* segment has data in it */
    159 #define	SEGUSE_SUPERBLOCK	0x04	/* segment contains a superblock */
    160 	u_int32_t su_flags;
    161 };
    162 
    163 #define	SEGUPB(fs)	(1 << (fs)->lfs_sushift)
    164 #define	SEGTABSIZE_SU(fs)						\
    165 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift)
    166 
    167 /* On-disk file information.  One per file with data blocks in the segment. */
    168 typedef struct finfo FINFO;
    169 struct finfo {
    170 	u_int32_t fi_nblocks;		/* number of blocks */
    171 	u_int32_t fi_version;		/* version number */
    172 	u_int32_t fi_ino;		/* inode number */
    173 	u_int32_t fi_lastlength;	/* length of last block in array */
    174 	ufs_daddr_t	  fi_blocks[1];		/* array of logical block numbers */
    175 };
    176 
    177 /* On-disk super block. */
    178 struct dlfs {
    179 #define	LFS_MAGIC	0x070162
    180         u_int32_t dlfs_magic;     /* 0: magic number */
    181 #define	LFS_VERSION	1
    182         u_int32_t dlfs_version;   /* 4: version number */
    183 
    184         u_int32_t dlfs_size;      /* 8: number of blocks in fs */
    185         u_int32_t dlfs_ssize;     /* 12: number of blocks per segment */
    186         u_int32_t dlfs_dsize;     /* 16: number of disk blocks in fs */
    187         u_int32_t dlfs_bsize;     /* 20: file system block size */
    188         u_int32_t dlfs_fsize;     /* 24: size of frag blocks in fs */
    189         u_int32_t dlfs_frag;      /* 28: number of frags in a block in fs */
    190 
    191 /* Checkpoint region. */
    192         ino_t     dlfs_free;      /* 32: start of the free list */
    193         u_int32_t dlfs_bfree;     /* 36: number of free disk blocks */
    194         u_int32_t dlfs_nfiles;    /* 40: number of allocated inodes */
    195         int32_t   dlfs_avail;     /* 44: blocks available for writing */
    196         u_int32_t dlfs_uinodes;   /* 48: inodes in cache not yet on disk */
    197         ufs_daddr_t  dlfs_idaddr; /* 52: inode file disk address */
    198         ino_t     dlfs_ifile;     /* 56: inode file inode number */
    199         ufs_daddr_t  dlfs_lastseg; /* 60: address of last segment written */
    200         ufs_daddr_t  dlfs_nextseg; /* 64: address of next segment to write */
    201         ufs_daddr_t  dlfs_curseg; /* 68: current segment being written */
    202         ufs_daddr_t  dlfs_offset; /* 72: offset in curseg for next partial */
    203         ufs_daddr_t  dlfs_lastpseg; /* 76: address of last partial written */
    204         u_int32_t dlfs_tstamp;    /* 80: time stamp */
    205 
    206 /* These are configuration parameters. */
    207         u_int32_t dlfs_minfree;   /* 84: minimum percentage of free blocks */
    208 
    209 /* These fields can be computed from the others. */
    210         u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
    211         u_int32_t dlfs_dbpseg;    /* 96: disk blocks per segment */
    212         u_int32_t dlfs_inopb;     /* 100: inodes per block */
    213         u_int32_t dlfs_ifpb;      /* 104: IFILE entries per block */
    214         u_int32_t dlfs_sepb;      /* 108: SEGUSE entries per block */
    215         u_int32_t dlfs_nindir;    /* 112: indirect pointers per block */
    216         u_int32_t dlfs_nseg;      /* 116: number of segments */
    217         u_int32_t dlfs_nspf;      /* 120: number of sectors per fragment */
    218         u_int32_t dlfs_cleansz;   /* 124: cleaner info size in blocks */
    219         u_int32_t dlfs_segtabsz;  /* 128: segment table size in blocks */
    220         u_int32_t dlfs_segmask;   /* 132: calculate offset within a segment */
    221         u_int32_t dlfs_segshift;  /* 136: fast mult/div for segments */
    222         u_int32_t dlfs_bshift;    /* 140: calc block number from file offset */
    223         u_int32_t dlfs_ffshift;   /* 144: fast mult/div for frag from file */
    224         u_int32_t dlfs_fbshift;   /* 148: fast mult/div for frag from block */
    225         u_int64_t dlfs_bmask;     /* 152: calc block offset from file offset */
    226         u_int64_t dlfs_ffmask;    /* 160: calc frag offset from file offset */
    227         u_int64_t dlfs_fbmask;    /* 168: calc frag offset from block offset */
    228         u_int32_t dlfs_fsbtodb;   /* 176: fsbtodb and dbtofsb shift constant */
    229         u_int32_t dlfs_sushift;   /* 180: fast mult/div for segusage table */
    230 
    231         int32_t   dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
    232 
    233 #define	LFS_MIN_SBINTERVAL	5  /* minimum superblock segment spacing */
    234 #define LFS_MAXNUMSB            10 /* 188: superblock disk offsets */
    235         ufs_daddr_t       dlfs_sboffs[LFS_MAXNUMSB];
    236 
    237 	u_int32_t dlfs_nclean;    /* 228: Number of clean segments */
    238 	u_char	  dlfs_fsmnt[MNAMELEN];	 /* 232: name mounted on */
    239 	/* XXX this is 2 bytes only to pad to a quad boundary */
    240 	u_int16_t dlfs_clean;     /* 322: file system is clean flag */
    241 #define LFS_MAX_ACTIVE          10
    242 /* Checksum -- last valid disk field. */
    243         u_int32_t dlfs_cksum;     /* 324: checksum for superblock checking */
    244         int8_t    dlfs_pad[181];  /* 328: round to 512 bytes */
    245 };
    246 
    247 /* Maximum number of io's we can have pending at once */
    248 #define LFS_THROTTLE  16 /* XXX should be better paramtrized - ? */
    249 
    250 /* In-memory super block. */
    251 struct lfs {
    252         struct dlfs lfs_dlfs;           /* on-disk parameters */
    253 #define lfs_magic lfs_dlfs.dlfs_magic
    254 #define lfs_version lfs_dlfs.dlfs_version
    255 #define lfs_size lfs_dlfs.dlfs_size
    256 #define lfs_ssize lfs_dlfs.dlfs_ssize
    257 #define lfs_dsize lfs_dlfs.dlfs_dsize
    258 #define lfs_bsize lfs_dlfs.dlfs_bsize
    259 #define lfs_fsize lfs_dlfs.dlfs_fsize
    260 #define lfs_frag lfs_dlfs.dlfs_frag
    261 #define lfs_free lfs_dlfs.dlfs_free
    262 #define lfs_bfree lfs_dlfs.dlfs_bfree
    263 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
    264 #define lfs_avail lfs_dlfs.dlfs_avail
    265 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
    266 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
    267 #define lfs_ifile lfs_dlfs.dlfs_ifile
    268 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
    269 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
    270 #define lfs_curseg lfs_dlfs.dlfs_curseg
    271 #define lfs_offset lfs_dlfs.dlfs_offset
    272 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
    273 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
    274 #define lfs_minfree lfs_dlfs.dlfs_minfree
    275 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
    276 #define lfs_dbpseg lfs_dlfs.dlfs_dbpseg
    277 #define lfs_inopb lfs_dlfs.dlfs_inopb
    278 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
    279 #define lfs_sepb lfs_dlfs.dlfs_sepb
    280 #define lfs_nindir lfs_dlfs.dlfs_nindir
    281 #define lfs_nseg lfs_dlfs.dlfs_nseg
    282 #define lfs_nspf lfs_dlfs.dlfs_nspf
    283 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
    284 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
    285 #define lfs_segmask lfs_dlfs.dlfs_segmask
    286 #define lfs_segshift lfs_dlfs.dlfs_segshift
    287 #define lfs_bmask lfs_dlfs.dlfs_bmask
    288 #define lfs_bshift lfs_dlfs.dlfs_bshift
    289 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
    290 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
    291 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
    292 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
    293 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
    294 #define lfs_sushift lfs_dlfs.dlfs_sushift
    295 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
    296 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
    297 #define lfs_cksum lfs_dlfs.dlfs_cksum
    298 #define lfs_clean lfs_dlfs.dlfs_clean
    299 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
    300 #define lfs_nclean lfs_dlfs.dlfs_nclean
    301 /* These fields are set at mount time and are meaningless on disk. */
    302 	struct segment *lfs_sp;		/* current segment being written */
    303 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
    304 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
    305 	pid_t	  lfs_lockpid;		/* pid of lock holder */
    306 	u_int32_t lfs_iocount;		/* number of ios pending */
    307 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
    308 	u_int32_t lfs_dirops;		/* count of active directory ops */
    309 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
    310 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
    311 	int8_t	  lfs_fmod;		/* super block modified flag */
    312 	int8_t	  lfs_ronly;		/* mounted read-only flag */
    313 	int8_t	  lfs_flags;		/* currently unused flag */
    314 #ifdef LFS_TOGGLE_SB
    315 	u_int16_t lfs_activesb;         /* toggle between superblocks */
    316 #endif /* LFS_TOGGLE_SB */
    317 #ifdef LFS_TRACK_IOS
    318 	daddr_t   lfs_pending[LFS_THROTTLE]; /* daddrs of pending writes */
    319 #endif /* LFS_TRACK_IOS */
    320 #ifdef LFS_USEDIROP
    321 # define LFS_MAXDIROP 32
    322 	int       lfs_dirvcount;        /* number of VDIROP-marked vnodes */
    323 #endif /* LFS_USEDIROP */
    324 #ifdef LFS_CANNOT_ROLLFW
    325 	daddr_t   lfs_sbactive;         /* disk address of in-progress sb write */
    326 #endif
    327 	struct vnode *lfs_flushvp;      /* vnode being flushed */
    328 	int       lfs_loanedbytes;      /* Temp. byte loans for removed inodes */
    329 };
    330 
    331 /*
    332  * Inode 0:	out-of-band inode number
    333  * Inode 1:	IFILE inode number
    334  * Inode 2:	root inode
    335  * Inode 3:	lost+found inode number
    336  */
    337 #define	LFS_UNUSED_INUM	0		/* out of band inode number */
    338 #define	LFS_IFILE_INUM	1		/* IFILE inode number */
    339 #define	LOSTFOUNDINO	3		/* lost+found inode number */
    340 #define	LFS_FIRST_INUM	4		/* first free inode number */
    341 
    342 /* Address calculations for metadata located in the inode */
    343 #define	S_INDIR(fs)	-NDADDR
    344 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
    345 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
    346 
    347 /* Unassigned disk address. */
    348 #define	UNASSIGNED	-1
    349 
    350 /* Unused logical block number */
    351 #define LFS_UNUSED_LBN	-1
    352 
    353 typedef struct ifile IFILE;
    354 struct ifile {
    355 	u_int32_t if_version;		/* inode version number */
    356 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
    357 	ufs_daddr_t if_daddr;		/* inode disk address */
    358 	ino_t	  if_nextfree;		/* next-unallocated inode */
    359 #ifdef LFS_ATIME_IFILE
    360 	struct timespec if_atime;	/* Last access time */
    361 #endif
    362 };
    363 
    364 /*
    365  * Cleaner information structure.  This resides in the ifile and is used
    366  * to pass information between the cleaner and the kernel.
    367  */
    368 typedef struct _cleanerinfo {
    369 	u_int32_t clean;		/* K: number of clean segments */
    370 	u_int32_t dirty;		/* K: number of dirty segments */
    371 } CLEANERINFO;
    372 
    373 #define	CLEANSIZE_SU(fs)						\
    374 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
    375 
    376 /*
    377  * All summary blocks are the same size, so we can always read a summary
    378  * block easily from a segment.
    379  */
    380 #define	LFS_SUMMARY_SIZE	512
    381 
    382 /* On-disk segment summary information */
    383 typedef struct segsum SEGSUM;
    384 struct segsum {
    385 	u_int32_t ss_sumsum;		/* check sum of summary block */
    386 	u_int32_t ss_datasum;		/* check sum of data */
    387 	u_int32_t ss_magic;		/* segment summary magic number */
    388 #define SS_MAGIC	0x061561
    389 	ufs_daddr_t ss_next;		/* next segment */
    390 	u_int32_t ss_create;		/* creation time stamp */
    391 	u_int16_t ss_nfinfo;		/* number of file info structures */
    392 	u_int16_t ss_ninos;		/* number of inodes in summary */
    393 
    394 #define	SS_DIROP	0x01		/* segment begins a dirop */
    395 #define	SS_CONT		0x02		/* more partials to finish this write*/
    396 	u_int16_t ss_flags;		/* used for directory operations */
    397 	u_int16_t ss_pad;		/* extra space */
    398 	/* FINFO's and inode daddr's... */
    399 };
    400 
    401 /* NINDIR is the number of indirects in a file system block. */
    402 #define	NINDIR(fs)	((fs)->lfs_nindir)
    403 
    404 /* INOPB is the number of inodes in a secondary storage block. */
    405 #define	INOPB(fs)	((fs)->lfs_inopb)
    406 
    407 #define	blksize(fs, ip, lbn) \
    408 	(((lbn) >= NDADDR || (ip)->i_ffs_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
    409 	    ? (fs)->lfs_bsize \
    410 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs_size))))
    411 #define	blkoff(fs, loc)		((int)(loc) & (fs)->lfs_bmask)
    412 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
    413     ((int)((loc) & (fs)->lfs_ffmask))
    414 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
    415 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
    416 #define fragstodb(fs, b)	((b) << ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
    417 #define dbtofrags(fs, b)	((b) >> ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
    418 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
    419 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
    420 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
    421 	((loc) >> (fs)->lfs_ffshift)
    422 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
    423 	((int)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
    424 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
    425 	((int)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
    426 #define fragstoblks(fs, frags)	/* calculates (frags / fs->lfs_frag) */ \
    427 	((frags) >> (fs)->lfs_fbshift)
    428 #define blkstofrags(fs, blks)	/* calculates (blks * fs->lfs_frag) */ \
    429 	((blks) << (fs)->lfs_fbshift)
    430 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */ \
    431 	((fsb) & ((fs)->lfs_frag - 1))
    432 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
    433 	((fsb) &~ ((fs)->lfs_frag - 1))
    434 #define dblksize(fs, dip, lbn) \
    435 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
    436 	    ? (fs)->lfs_bsize \
    437 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
    438 #define	datosn(fs, daddr)	/* disk address to segment number */	\
    439 	(((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
    440 #define sntoda(fs, sn) 		/* segment number to disk address */	\
    441 	((ufs_daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) +	\
    442 	    (fs)->lfs_sboffs[0]))
    443 
    444 /* Read in the block with the cleaner info from the ifile. */
    445 #define LFS_CLEANERINFO(CP, F, BP) {					\
    446 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    447 	if (bread((F)->lfs_ivnode,					\
    448 	    (ufs_daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP)))		\
    449 		panic("lfs: ifile read");				\
    450 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    451 }
    452 
    453 /* Read in the block with a specific inode from the ifile. */
    454 #define	LFS_IENTRY(IP, F, IN, BP) {					\
    455 	int _e;								\
    456 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    457 	if ((_e = bread((F)->lfs_ivnode,				\
    458 	    (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
    459 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    460 		panic("lfs: ifile read %d", _e);			\
    461 	(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;		\
    462 }
    463 
    464 /* Read in the block with a specific segment usage entry from the ifile. */
    465 #define	LFS_SEGENTRY(SP, F, IN, BP) {					\
    466 	int _e;								\
    467 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    468 	if ((_e = bread((F)->lfs_ivnode,				\
    469 	    ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz,		\
    470 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    471 		panic("lfs: ifile read: %d", _e);			\
    472 	(SP) = (SEGUSE *)(BP)->b_data + ((IN) & ((F)->lfs_sepb - 1));	\
    473 }
    474 
    475 /* Determine if a buffer belongs to the ifile */
    476 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
    477 
    478 /*
    479  * Structures used by lfs_bmapv and lfs_markv to communicate information
    480  * about inodes and data blocks.
    481  */
    482 typedef struct block_info {
    483 	ino_t	bi_inode;		/* inode # */
    484 	ufs_daddr_t bi_lbn;		/* logical block w/in file */
    485 	ufs_daddr_t bi_daddr;		/* disk address of block */
    486 	time_t	bi_segcreate;		/* origin segment create time */
    487 	int	bi_version;		/* file version number */
    488 	void	*bi_bp;			/* data buffer */
    489 	int     bi_size;		/* size of the block (if fragment) */
    490 } BLOCK_INFO;
    491 
    492 /* In-memory description of a segment about to be written. */
    493 struct segment {
    494 	struct lfs	 *fs;		/* file system pointer */
    495 	struct buf	**bpp;		/* pointer to buffer array */
    496 	struct buf	**cbpp;		/* pointer to next available bp */
    497 	struct buf	**start_bpp;	/* pointer to first bp in this set */
    498 	struct buf	 *ibp;		/* buffer pointer to inode page */
    499 	struct finfo	 *fip;		/* current fileinfo pointer */
    500 	struct vnode	 *vp;		/* vnode being gathered */
    501 	void	 *segsum;		/* segment summary info */
    502 	u_int32_t ninodes;		/* number of inodes in this segment */
    503 	u_int32_t seg_bytes_left;	/* bytes left in segment */
    504 	u_int32_t sum_bytes_left;	/* bytes left in summary block */
    505 	u_int32_t seg_number;		/* number of this segment */
    506 	ufs_daddr_t *start_lbp;		/* beginning lbn for this set */
    507 
    508 #define	SEGM_CKP	0x01		/* doing a checkpoint */
    509 #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
    510 #define	SEGM_SYNC	0x04		/* wait for segment */
    511 	u_int16_t seg_flags;		/* run-time flags for this segment */
    512 };
    513 
    514 #define ISSPACE(F, BB, C)						\
    515 	(((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) ||		\
    516 	((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
    517 
    518 #define IS_FREESPACE(F, BB)						\
    519 	((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB)))
    520 
    521 #define ISSPACE_XXX(F, BB)						\
    522 	((F)->lfs_bfree >= (BB))
    523 
    524 /* Statistics Counters */
    525 struct lfs_stats {
    526 	u_int	segsused;
    527 	u_int	psegwrites;
    528 	u_int	psyncwrites;
    529 	u_int	pcleanwrites;
    530 	u_int	blocktot;
    531 	u_int	cleanblocks;
    532 	u_int	ncheckpoints;
    533 	u_int	nwrites;
    534 	u_int	nsync_writes;
    535 	u_int	wait_exceeded;
    536 	u_int	write_exceeded;
    537 	u_int	flush_invoked;
    538 	u_int	vflush_invoked;
    539 };
    540 extern struct lfs_stats lfs_stats;
    541