Home | History | Annotate | Line # | Download | only in lfs
lfs.h revision 1.25
      1 /*	$NetBSD: lfs.h,v 1.25 2000/06/06 20:19:14 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_TRACK_IOS            /* attempt to avoid cleaning segments not yet fully written to disk */
     78 #define LFS_CANNOT_ROLLFW       /* No roll-forward agent exists */
     79 
     80 /* #define DEBUG_LFS */              /* Intensive debugging of LFS subsystem */
     81 
     82 /* #define LFS_ATIME_IFILE */         /* Store atime in Ifile, don't push */
     83 
     84 /*
     85  * Parameters and generic definitions
     86  */
     87 #define BW_CLEAN	1
     88 #define MIN_FREE_SEGS	4
     89 #define LFS_MAX_ACTIVE	10
     90 #define LFS_MAXDIROP	(desiredvnodes>>2)
     91 
     92 /* For convenience */
     93 #define IN_ALLMOD (IN_MODIFIED|IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_ACCESSED|IN_CLEANING)
     94 
     95 #ifndef LFS_ATIME_IFILE
     96 # define LFS_ITIMES(ip, acc, mod, cre) FFS_ITIMES((ip),(acc),(mod),(cre))
     97 #else
     98 # define LFS_ITIMES(ip, acc, mod, cre) {                                \
     99 	struct buf *ibp;						\
    100 	IFILE *ifp;							\
    101 									\
    102         if ((ip)->i_flag & IN_ACCESS) {                         	\
    103 	LFS_IENTRY(ifp, ip->i_lfs, ip->i_number, ibp);			\
    104        		ifp->if_atime = (mod);					\
    105        		VOP_BWRITE(bp);						\
    106 		(ip)->i_flag &= ~IN_ACCESS;				\
    107         }                                                       	\
    108         if ((ip)->i_flag & (IN_CHANGE | IN_UPDATE)) {       		\
    109                 (ip)->i_flag |= IN_MODIFIED;                            \
    110                 if ((ip)->i_flag & IN_UPDATE) {                         \
    111                         (ip)->i_ffs_mtime = (mod)->tv_sec;		\
    112                         (ip)->i_ffs_mtimensec = (mod)->tv_nsec;         \
    113                         (ip)->i_modrev++;                               \
    114                 }                                                       \
    115                 if ((ip)->i_flag & IN_CHANGE) {                         \
    116                         (ip)->i_ffs_ctime = (cre)->tv_sec;		\
    117                         (ip)->i_ffs_ctimensec = (cre)->tv_nsec;         \
    118                 }                                                       \
    119                 (ip)->i_flag &= ~(IN_CHANGE | IN_UPDATE);   		\
    120         }                                                               \
    121 }
    122 #endif
    123 
    124 #define WRITEINPROG(vp) (vp->v_dirtyblkhd.lh_first && !(VTOI(vp)->i_flag & (IN_MODIFIED|IN_CLEANING)))
    125 
    126 /* Here begins the berkeley code */
    127 
    128 #define	LFS_LABELPAD	8192		/* LFS label size */
    129 #define	LFS_SBPAD	8192		/* LFS superblock size */
    130 
    131 /* On-disk and in-memory checkpoint segment usage structure. */
    132 typedef struct segusage SEGUSE;
    133 struct segusage {
    134 	u_int32_t su_nbytes;		/* number of live bytes */
    135 	u_int32_t su_lastmod;		/* SEGUSE last modified timestamp */
    136 	u_int16_t su_nsums;		/* number of summaries in segment */
    137 	u_int16_t su_ninos;		/* number of inode blocks in seg */
    138 
    139 #define	SEGUSE_ACTIVE		0x01	/* segment is currently being written */
    140 #define	SEGUSE_DIRTY		0x02	/* segment has data in it */
    141 #define	SEGUSE_SUPERBLOCK	0x04	/* segment contains a superblock */
    142 	u_int32_t su_flags;
    143 };
    144 
    145 #define	SEGUPB(fs)	(1 << (fs)->lfs_sushift)
    146 #define	SEGTABSIZE_SU(fs)						\
    147 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) >> (fs)->lfs_sushift)
    148 
    149 /* On-disk file information.  One per file with data blocks in the segment. */
    150 typedef struct finfo FINFO;
    151 struct finfo {
    152 	u_int32_t fi_nblocks;		/* number of blocks */
    153 	u_int32_t fi_version;		/* version number */
    154 	u_int32_t fi_ino;		/* inode number */
    155 	u_int32_t fi_lastlength;	/* length of last block in array */
    156 	ufs_daddr_t	  fi_blocks[1];		/* array of logical block numbers */
    157 };
    158 
    159 /* On-disk super block. */
    160 struct dlfs {
    161 #define	LFS_MAGIC	0x070162
    162         u_int32_t dlfs_magic;     /* 0: magic number */
    163 #define	LFS_VERSION	1
    164         u_int32_t dlfs_version;   /* 4: version number */
    165 
    166         u_int32_t dlfs_size;      /* 8: number of blocks in fs */
    167         u_int32_t dlfs_ssize;     /* 12: number of blocks per segment */
    168         u_int32_t dlfs_dsize;     /* 16: number of disk blocks in fs */
    169         u_int32_t dlfs_bsize;     /* 20: file system block size */
    170         u_int32_t dlfs_fsize;     /* 24: size of frag blocks in fs */
    171         u_int32_t dlfs_frag;      /* 28: number of frags in a block in fs */
    172 
    173 /* Checkpoint region. */
    174         u_int32_t dlfs_free;      /* 32: start of the free list */
    175         u_int32_t dlfs_bfree;     /* 36: number of free disk blocks */
    176         u_int32_t dlfs_nfiles;    /* 40: number of allocated inodes */
    177         int32_t   dlfs_avail;     /* 44: blocks available for writing */
    178         u_int32_t dlfs_uinodes;   /* 48: inodes in cache not yet on disk */
    179         ufs_daddr_t  dlfs_idaddr; /* 52: inode file disk address */
    180         u_int32_t dlfs_ifile;     /* 56: inode file inode number */
    181         ufs_daddr_t  dlfs_lastseg; /* 60: address of last segment written */
    182         ufs_daddr_t  dlfs_nextseg; /* 64: address of next segment to write */
    183         ufs_daddr_t  dlfs_curseg; /* 68: current segment being written */
    184         ufs_daddr_t  dlfs_offset; /* 72: offset in curseg for next partial */
    185         ufs_daddr_t  dlfs_lastpseg; /* 76: address of last partial written */
    186         u_int32_t dlfs_tstamp;    /* 80: time stamp */
    187 
    188 /* These are configuration parameters. */
    189         u_int32_t dlfs_minfree;   /* 84: minimum percentage of free blocks */
    190 
    191 /* These fields can be computed from the others. */
    192         u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
    193         u_int32_t dlfs_dbpseg;    /* 96: disk blocks per segment */
    194         u_int32_t dlfs_inopb;     /* 100: inodes per block */
    195         u_int32_t dlfs_ifpb;      /* 104: IFILE entries per block */
    196         u_int32_t dlfs_sepb;      /* 108: SEGUSE entries per block */
    197         u_int32_t dlfs_nindir;    /* 112: indirect pointers per block */
    198         u_int32_t dlfs_nseg;      /* 116: number of segments */
    199         u_int32_t dlfs_nspf;      /* 120: number of sectors per fragment */
    200         u_int32_t dlfs_cleansz;   /* 124: cleaner info size in blocks */
    201         u_int32_t dlfs_segtabsz;  /* 128: segment table size in blocks */
    202         u_int32_t dlfs_segmask;   /* 132: calculate offset within a segment */
    203         u_int32_t dlfs_segshift;  /* 136: fast mult/div for segments */
    204         u_int32_t dlfs_bshift;    /* 140: calc block number from file offset */
    205         u_int32_t dlfs_ffshift;   /* 144: fast mult/div for frag from file */
    206         u_int32_t dlfs_fbshift;   /* 148: fast mult/div for frag from block */
    207         u_int64_t dlfs_bmask;     /* 152: calc block offset from file offset */
    208         u_int64_t dlfs_ffmask;    /* 160: calc frag offset from file offset */
    209         u_int64_t dlfs_fbmask;    /* 168: calc frag offset from block offset */
    210         u_int32_t dlfs_fsbtodb;   /* 176: fsbtodb and dbtofsb shift constant */
    211         u_int32_t dlfs_sushift;   /* 180: fast mult/div for segusage table */
    212 
    213         int32_t   dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
    214 
    215 #define	LFS_MIN_SBINTERVAL	5  /* minimum superblock segment spacing */
    216 #define LFS_MAXNUMSB            10 /* 188: superblock disk offsets */
    217         ufs_daddr_t       dlfs_sboffs[LFS_MAXNUMSB];
    218 
    219 	u_int32_t dlfs_nclean;    /* 228: Number of clean segments */
    220 	u_char	  dlfs_fsmnt[MNAMELEN];	 /* 232: name mounted on */
    221 	/* XXX this is 2 bytes only to pad to a quad boundary */
    222 	u_int16_t dlfs_clean;     /* 322: file system is clean flag */
    223         int8_t    dlfs_pad[184];  /* 324: round to 512 bytes */
    224 /* Checksum -- last valid disk field. */
    225         u_int32_t dlfs_cksum;     /* 328: checksum for superblock checking */
    226 };
    227 
    228 /* Maximum number of io's we can have pending at once */
    229 #define LFS_THROTTLE  16 /* XXX should be better paramtrized - ? */
    230 
    231 /* In-memory super block. */
    232 struct lfs {
    233         struct dlfs lfs_dlfs;           /* on-disk parameters */
    234 #define lfs_magic lfs_dlfs.dlfs_magic
    235 #define lfs_version lfs_dlfs.dlfs_version
    236 #define lfs_size lfs_dlfs.dlfs_size
    237 #define lfs_ssize lfs_dlfs.dlfs_ssize
    238 #define lfs_dsize lfs_dlfs.dlfs_dsize
    239 #define lfs_bsize lfs_dlfs.dlfs_bsize
    240 #define lfs_fsize lfs_dlfs.dlfs_fsize
    241 #define lfs_frag lfs_dlfs.dlfs_frag
    242 #define lfs_free lfs_dlfs.dlfs_free
    243 #define lfs_bfree lfs_dlfs.dlfs_bfree
    244 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
    245 #define lfs_avail lfs_dlfs.dlfs_avail
    246 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
    247 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
    248 #define lfs_ifile lfs_dlfs.dlfs_ifile
    249 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
    250 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
    251 #define lfs_curseg lfs_dlfs.dlfs_curseg
    252 #define lfs_offset lfs_dlfs.dlfs_offset
    253 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
    254 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
    255 #define lfs_minfree lfs_dlfs.dlfs_minfree
    256 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
    257 #define lfs_dbpseg lfs_dlfs.dlfs_dbpseg
    258 #define lfs_inopb lfs_dlfs.dlfs_inopb
    259 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
    260 #define lfs_sepb lfs_dlfs.dlfs_sepb
    261 #define lfs_nindir lfs_dlfs.dlfs_nindir
    262 #define lfs_nseg lfs_dlfs.dlfs_nseg
    263 #define lfs_nspf lfs_dlfs.dlfs_nspf
    264 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
    265 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
    266 #define lfs_segmask lfs_dlfs.dlfs_segmask
    267 #define lfs_segshift lfs_dlfs.dlfs_segshift
    268 #define lfs_bmask lfs_dlfs.dlfs_bmask
    269 #define lfs_bshift lfs_dlfs.dlfs_bshift
    270 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
    271 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
    272 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
    273 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
    274 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
    275 #define lfs_sushift lfs_dlfs.dlfs_sushift
    276 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
    277 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
    278 #define lfs_cksum lfs_dlfs.dlfs_cksum
    279 #define lfs_clean lfs_dlfs.dlfs_clean
    280 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
    281 #define lfs_nclean lfs_dlfs.dlfs_nclean
    282 /* These fields are set at mount time and are meaningless on disk. */
    283 	struct segment *lfs_sp;		/* current segment being written */
    284 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
    285 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
    286 	pid_t	  lfs_lockpid;		/* pid of lock holder */
    287 	u_int32_t lfs_iocount;		/* number of ios pending */
    288 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
    289 	u_int32_t lfs_dirops;		/* count of active directory ops */
    290 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
    291 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
    292 	int8_t	  lfs_fmod;		/* super block modified flag */
    293 	int8_t	  lfs_ronly;		/* mounted read-only flag */
    294 #define LFS_NOTYET 0x01
    295 	int8_t	  lfs_flags;		/* currently unused flag */
    296 	u_int16_t lfs_activesb;         /* toggle between superblocks */
    297 #ifdef LFS_TRACK_IOS
    298 	daddr_t   lfs_pending[LFS_THROTTLE]; /* daddrs of pending writes */
    299 #endif /* LFS_TRACK_IOS */
    300 #ifdef LFS_CANNOT_ROLLFW
    301 	daddr_t   lfs_sbactive;         /* disk address of in-progress sb write */
    302 #endif
    303 	struct vnode *lfs_flushvp;      /* vnode being flushed */
    304 	struct vnode *lfs_unlockvp;     /* being inactivated in lfs_segunlock */
    305 	u_int32_t lfs_diropwait;	/* # procs waiting on dirop flush */
    306 	struct lock lfs_freelock;
    307 	pid_t lfs_rfpid;		/* Process ID of roll-forward agent */
    308 	int       lfs_nadirop;		/* number of active dirop nodes */
    309 };
    310 
    311 /*
    312  * Inode 0:	out-of-band inode number
    313  * Inode 1:	IFILE inode number
    314  * Inode 2:	root inode
    315  * Inode 3:	lost+found inode number
    316  */
    317 #define	LFS_UNUSED_INUM	0		/* out of band inode number */
    318 #define	LFS_IFILE_INUM	1		/* IFILE inode number */
    319 #define	LOSTFOUNDINO	3		/* lost+found inode number */
    320 #define	LFS_FIRST_INUM	4		/* first free inode number */
    321 
    322 /* Address calculations for metadata located in the inode */
    323 #define	S_INDIR(fs)	-NDADDR
    324 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
    325 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
    326 
    327 /* Unassigned disk addresses. */
    328 #define	UNASSIGNED	-1
    329 #define UNWRITTEN       -2
    330 
    331 /* Unused logical block number */
    332 #define LFS_UNUSED_LBN	-1
    333 
    334 typedef struct ifile IFILE;
    335 struct ifile {
    336 	u_int32_t if_version;		/* inode version number */
    337 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
    338 	ufs_daddr_t if_daddr;		/* inode disk address */
    339 	ino_t	  if_nextfree;		/* next-unallocated inode */
    340 #ifdef LFS_ATIME_IFILE
    341 	struct timespec if_atime;	/* Last access time */
    342 #endif
    343 };
    344 
    345 /*
    346  * Cleaner information structure.  This resides in the ifile and is used
    347  * to pass information between the cleaner and the kernel.
    348  */
    349 typedef struct _cleanerinfo {
    350 	u_int32_t clean;		/* K: number of clean segments */
    351 	u_int32_t dirty;		/* K: number of dirty segments */
    352 } CLEANERINFO;
    353 
    354 #define	CLEANSIZE_SU(fs)						\
    355 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
    356 
    357 /*
    358  * All summary blocks are the same size, so we can always read a summary
    359  * block easily from a segment.
    360  */
    361 #define	LFS_SUMMARY_SIZE	512
    362 
    363 /* On-disk segment summary information */
    364 typedef struct segsum SEGSUM;
    365 struct segsum {
    366 	u_int32_t ss_sumsum;		/* check sum of summary block */
    367 	u_int32_t ss_datasum;		/* check sum of data */
    368 	u_int32_t ss_magic;		/* segment summary magic number */
    369 #define SS_MAGIC	0x061561
    370 	ufs_daddr_t ss_next;		/* next segment */
    371 	u_int32_t ss_create;		/* creation time stamp */
    372 	u_int16_t ss_nfinfo;		/* number of file info structures */
    373 	u_int16_t ss_ninos;		/* number of inodes in summary */
    374 
    375 #define	SS_DIROP	0x01		/* segment begins a dirop */
    376 #define	SS_CONT		0x02		/* more partials to finish this write*/
    377 	u_int16_t ss_flags;		/* used for directory operations */
    378 	u_int16_t ss_pad;		/* extra space */
    379 	/* FINFO's and inode daddr's... */
    380 };
    381 
    382 /* NINDIR is the number of indirects in a file system block. */
    383 #define	NINDIR(fs)	((fs)->lfs_nindir)
    384 
    385 /* INOPB is the number of inodes in a secondary storage block. */
    386 #define	INOPB(fs)	((fs)->lfs_inopb)
    387 
    388 #define	blksize(fs, ip, lbn) \
    389 	(((lbn) >= NDADDR || (ip)->i_ffs_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
    390 	    ? (fs)->lfs_bsize \
    391 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs_size))))
    392 #define	blkoff(fs, loc)		((int)(loc) & (fs)->lfs_bmask)
    393 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
    394     ((int)((loc) & (fs)->lfs_ffmask))
    395 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
    396 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
    397 #define fragstodb(fs, b)	((b) << ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
    398 #define dbtofrags(fs, b)	((b) >> ((fs)->lfs_fsbtodb - (fs)->lfs_fbshift))
    399 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
    400 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
    401 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
    402 	((loc) >> (fs)->lfs_ffshift)
    403 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
    404 	((int)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
    405 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
    406 	((int)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
    407 #define fragstoblks(fs, frags)	/* calculates (frags / fs->lfs_frag) */ \
    408 	((frags) >> (fs)->lfs_fbshift)
    409 #define blkstofrags(fs, blks)	/* calculates (blks * fs->lfs_frag) */ \
    410 	((blks) << (fs)->lfs_fbshift)
    411 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */ \
    412 	((fsb) & ((fs)->lfs_frag - 1))
    413 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
    414 	((fsb) &~ ((fs)->lfs_frag - 1))
    415 #define dblksize(fs, dip, lbn) \
    416 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
    417 	    ? (fs)->lfs_bsize \
    418 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
    419 #define	datosn(fs, daddr)	/* disk address to segment number */	\
    420 	(((daddr) - (fs)->lfs_sboffs[0]) / fsbtodb((fs), (fs)->lfs_ssize))
    421 #define sntoda(fs, sn) 		/* segment number to disk address */	\
    422 	((ufs_daddr_t)((sn) * ((fs)->lfs_ssize << (fs)->lfs_fsbtodb) +	\
    423 	    (fs)->lfs_sboffs[0]))
    424 
    425 /* Read in the block with the cleaner info from the ifile. */
    426 #define LFS_CLEANERINFO(CP, F, BP) {					\
    427 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    428 	if (bread((F)->lfs_ivnode,					\
    429 	    (ufs_daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP)))		\
    430 		panic("lfs: ifile read");				\
    431 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    432 }
    433 
    434 /* Read in the block with a specific inode from the ifile. */
    435 #define	LFS_IENTRY(IP, F, IN, BP) {					\
    436 	int _e;								\
    437 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    438 	if ((_e = bread((F)->lfs_ivnode,				\
    439 	    (IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,\
    440 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    441 		panic("lfs: ifile read %d", _e);			\
    442 	(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;		\
    443 }
    444 
    445 /* Read in the block with a specific segment usage entry from the ifile. */
    446 #define	LFS_SEGENTRY(SP, F, IN, BP) {					\
    447 	int _e;								\
    448 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    449 	if ((_e = bread((F)->lfs_ivnode,				\
    450 	    ((IN) >> (F)->lfs_sushift) + (F)->lfs_cleansz,		\
    451 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    452 		panic("lfs: ifile read: %d", _e);			\
    453 	(SP) = (SEGUSE *)(BP)->b_data + ((IN) & ((F)->lfs_sepb - 1));	\
    454 }
    455 
    456 /* Determine if a buffer belongs to the ifile */
    457 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
    458 
    459 /*
    460  * Structures used by lfs_bmapv and lfs_markv to communicate information
    461  * about inodes and data blocks.
    462  */
    463 typedef struct block_info {
    464 	ino_t	bi_inode;		/* inode # */
    465 	ufs_daddr_t bi_lbn;		/* logical block w/in file */
    466 	ufs_daddr_t bi_daddr;		/* disk address of block */
    467 	time_t	bi_segcreate;		/* origin segment create time */
    468 	int	bi_version;		/* file version number */
    469 	void	*bi_bp;			/* data buffer */
    470 	int     bi_size;		/* size of the block (if fragment) */
    471 } BLOCK_INFO;
    472 
    473 /* In-memory description of a segment about to be written. */
    474 struct segment {
    475 	struct lfs	 *fs;		/* file system pointer */
    476 	struct buf	**bpp;		/* pointer to buffer array */
    477 	struct buf	**cbpp;		/* pointer to next available bp */
    478 	struct buf	**start_bpp;	/* pointer to first bp in this set */
    479 	struct buf	 *ibp;		/* buffer pointer to inode page */
    480 	struct dinode    *idp;          /* pointer to ifile dinode */
    481 	struct finfo	 *fip;		/* current fileinfo pointer */
    482 	struct vnode	 *vp;		/* vnode being gathered */
    483 	void	 *segsum;		/* segment summary info */
    484 	u_int32_t ninodes;		/* number of inodes in this segment */
    485 	u_int32_t seg_bytes_left;	/* bytes left in segment */
    486 	u_int32_t sum_bytes_left;	/* bytes left in summary block */
    487 	u_int32_t seg_number;		/* number of this segment */
    488 	ufs_daddr_t *start_lbp;		/* beginning lbn for this set */
    489 
    490 #define	SEGM_CKP	0x01		/* doing a checkpoint */
    491 #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
    492 #define	SEGM_SYNC	0x04		/* wait for segment */
    493 #define	SEGM_PROT	0x08		/* don't inactivate at segunlock */
    494 	u_int16_t seg_flags;		/* run-time flags for this segment */
    495 };
    496 
    497 #define ISSPACE(F, BB, C)						\
    498 	(((C)->cr_uid == 0 && (F)->lfs_bfree >= (BB)) ||		\
    499 	((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
    500 
    501 #define IS_FREESPACE(F, BB)						\
    502 	((F)->lfs_bfree > ((F)->lfs_dsize * (F)->lfs_minfree / 100 + (BB)))
    503 
    504 #define ISSPACE_XXX(F, BB)						\
    505 	((F)->lfs_bfree >= (BB))
    506 
    507 /* Statistics Counters */
    508 struct lfs_stats {
    509 	u_int	segsused;
    510 	u_int	psegwrites;
    511 	u_int	psyncwrites;
    512 	u_int	pcleanwrites;
    513 	u_int	blocktot;
    514 	u_int	cleanblocks;
    515 	u_int	ncheckpoints;
    516 	u_int	nwrites;
    517 	u_int	nsync_writes;
    518 	u_int	wait_exceeded;
    519 	u_int	write_exceeded;
    520 	u_int	flush_invoked;
    521 	u_int	vflush_invoked;
    522 };
    523 extern struct lfs_stats lfs_stats;
    524