Home | History | Annotate | Line # | Download | only in lfs
lfs.h revision 1.37
      1 /*	$NetBSD: lfs.h,v 1.37 2001/07/13 20:30:22 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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_DEBUG_RFW            /* print roll-forward debugging info */
     79 
     80 /* #define DEBUG_LFS */              /* Intensive debugging of LFS subsystem */
     81 
     82 /*
     83  * Parameters and generic definitions
     84  */
     85 #define BW_CLEAN	1
     86 #define MIN_FREE_SEGS	2
     87 #define LFS_MAX_ACTIVE	10
     88 #define LFS_MAXDIROP	(desiredvnodes>>2)
     89 #ifndef LFS_ATIME_IFILE
     90 # define LFS_ATIME_IFILE 0
     91 #endif
     92 
     93 /*
     94  * #define WRITE_THRESHHOLD    ((nbuf >> 1) - 10)
     95  * #define WAIT_THRESHHOLD     (nbuf - (nbuf >> 2) - 10)
     96  */
     97 #define LFS_MAX_BUFS        ((nbuf >> 2) - 10)
     98 #define LFS_WAIT_BUFS       ((nbuf >> 1) - (nbuf >> 3) - 10)
     99 /* These are new ... is LFS taking up too much memory in its buffers? */
    100 #define LFS_MAX_BYTES       (((bufpages >> 2) - 10) * NBPG)
    101 #define LFS_WAIT_BYTES      (((bufpages >> 1) - (bufpages >> 3) - 10) * NBPG)
    102 #define LFS_BUFWAIT         2
    103 
    104 #define LFS_LOCK_BUF(bp) do {						\
    105 	if (((bp)->b_flags & (B_LOCKED | B_CALL)) == 0) {		\
    106 		++locked_queue_count;       				\
    107 		locked_queue_bytes += bp->b_bufsize;			\
    108 	}								\
    109 	(bp)->b_flags |= B_LOCKED;					\
    110 } while(0)
    111 
    112 #define LFS_UNLOCK_BUF(bp) do {						\
    113 	if (((bp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED) {	\
    114 		--locked_queue_count;       				\
    115 		locked_queue_bytes -= bp->b_bufsize;			\
    116 		if (locked_queue_count < LFS_WAIT_BUFS &&		\
    117 		    locked_queue_bytes < LFS_WAIT_BYTES)		\
    118 			wakeup(&locked_queue_count);			\
    119 	}								\
    120 	(bp)->b_flags &= ~B_LOCKED;					\
    121 } while(0)
    122 
    123 /* For convenience */
    124 #define IN_ALLMOD (IN_MODIFIED|IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_ACCESSED|IN_CLEANING)
    125 
    126 #define LFS_SET_UINO(ip, flags) do {                                    \
    127         if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED))   \
    128                 ++(ip)->i_lfs->lfs_uinodes;                             \
    129         if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING))   \
    130                 ++(ip)->i_lfs->lfs_uinodes;                             \
    131         if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED))   \
    132                 ++(ip)->i_lfs->lfs_uinodes;                             \
    133         (ip)->i_flag |= (flags);                                        \
    134 } while(0)
    135 
    136 #define LFS_CLR_UINO(ip, flags) do {                                    \
    137         if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED))    \
    138                 --(ip)->i_lfs->lfs_uinodes;                             \
    139         if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING))    \
    140                 --(ip)->i_lfs->lfs_uinodes;                             \
    141         if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED))    \
    142                 --(ip)->i_lfs->lfs_uinodes;                             \
    143         (ip)->i_flag &= ~(flags);                                       \
    144 	if ((ip)->i_lfs->lfs_uinodes < 0) {                             \
    145 		panic("lfs_uinodes < 0");                               \
    146 	}                                                               \
    147 } while(0)
    148 
    149 #define LFS_ITIMES(ip, acc, mod, cre)  do {				\
    150        	if ((ip)->i_flag & IN_ACCESS) {                        		\
    151 		(ip)->i_ffs_atime = (acc)->tv_sec;			\
    152 		(ip)->i_ffs_atimensec = (acc)->tv_nsec;			\
    153 		if ((ip)->i_lfs->lfs_version > 1) {			\
    154 			struct buf *ibp;				\
    155 			IFILE *ifp;					\
    156 									\
    157 			LFS_IENTRY(ifp, ip->i_lfs, ip->i_number, ibp);	\
    158 			ifp->if_atime_sec = (acc)->tv_sec;		\
    159 			ifp->if_atime_nsec = (acc)->tv_nsec;		\
    160 			VOP_BWRITE(ibp);				\
    161 		} else {						\
    162 			LFS_SET_UINO(ip, IN_ACCESSED);			\
    163 		}                                              		\
    164 	}								\
    165 	if ((ip)->i_flag & (IN_CHANGE | IN_UPDATE)) {			\
    166 		if ((ip)->i_flag & IN_UPDATE) {				\
    167 			(ip)->i_ffs_mtime = (mod)->tv_sec;		\
    168 			(ip)->i_ffs_mtimensec = (mod)->tv_nsec;		\
    169 			(ip)->i_modrev++;				\
    170 		}							\
    171 		if ((ip)->i_flag & IN_CHANGE) {				\
    172 			(ip)->i_ffs_ctime = (cre)->tv_sec;		\
    173 			(ip)->i_ffs_ctimensec = (cre)->tv_nsec;		\
    174 		}							\
    175 		LFS_SET_UINO(ip, IN_MODIFIED);				\
    176 	}								\
    177 	(ip)->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);		\
    178 } while(0)
    179 
    180 #define WRITEINPROG(vp) (vp->v_dirtyblkhd.lh_first && !(VTOI(vp)->i_flag & \
    181 				(IN_MODIFIED | IN_ACCESSED | IN_CLEANING)))
    182 
    183 /* Here begins the berkeley code */
    184 
    185 #define	LFS_LABELPAD	8192		/* LFS label size */
    186 #define	LFS_SBPAD	8192		/* LFS superblock size */
    187 
    188 /* On-disk and in-memory checkpoint segment usage structure. */
    189 typedef struct segusage SEGUSE;
    190 struct segusage {
    191 	u_int32_t su_nbytes;		/* 0: number of live bytes */
    192 	u_int32_t su_olastmod;		/* 4: SEGUSE last modified timestamp */
    193 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
    194 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
    195 
    196 #define	SEGUSE_ACTIVE		0x01	/*  segment currently being written */
    197 #define	SEGUSE_DIRTY		0x02	/*  segment has data in it */
    198 #define	SEGUSE_SUPERBLOCK	0x04	/*  segment contains a superblock */
    199 #define SEGUSE_ERROR            0x08    /*  cleaner: do not clean segment */
    200 	u_int32_t su_flags;		/* 12: segment flags */
    201 	u_int64_t su_lastmod;		/* 16: last modified timestamp */
    202 };
    203 
    204 typedef struct segusage_v1 SEGUSE_V1;
    205 struct segusage_v1 {
    206 	u_int32_t su_nbytes;		/* 0: number of live bytes */
    207 	u_int32_t su_lastmod;		/* 4: SEGUSE last modified timestamp */
    208 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
    209 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
    210 	u_int32_t su_flags;             /* 12: segment flags  */
    211 };
    212 
    213 #define	SEGUPB(fs)	(fs->lfs_sepb)
    214 #define	SEGTABSIZE_SU(fs)						\
    215 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) / (fs)->lfs_sepb)
    216 
    217 /* On-disk file information.  One per file with data blocks in the segment. */
    218 typedef struct finfo FINFO;
    219 struct finfo {
    220 	u_int32_t fi_nblocks;		/* number of blocks */
    221 	u_int32_t fi_version;		/* version number */
    222 	u_int32_t fi_ino;		/* inode number */
    223 	u_int32_t fi_lastlength;	/* length of last block in array */
    224 	ufs_daddr_t fi_blocks[1];	/* array of logical block numbers */
    225 };
    226 
    227 
    228 /* On-disk super block. */
    229 struct dlfs {
    230 #define        LFS_MAGIC       0x070162
    231         u_int32_t dlfs_magic;     /* 0: magic number */
    232 #define        LFS_VERSION     2
    233         u_int32_t dlfs_version;   /* 4: version number */
    234 
    235         u_int32_t dlfs_size;      /* 8: number of blocks in fs (v1) */
    236 				  /*    number of frags in fs (v2) */
    237         u_int32_t dlfs_ssize;     /* 12: number of blocks per segment (v1) */
    238 	                          /*     number of bytes per segment (v2) */
    239         u_int32_t dlfs_dsize;     /* 16: number of disk blocks in fs */
    240         u_int32_t dlfs_bsize;     /* 20: file system block size */
    241         u_int32_t dlfs_fsize;     /* 24: size of frag blocks in fs */
    242         u_int32_t dlfs_frag;      /* 28: number of frags in a block in fs */
    243 
    244 /* Checkpoint region. */
    245         u_int32_t dlfs_free;      /* 32: start of the free list */
    246         u_int32_t dlfs_bfree;     /* 36: number of free disk blocks */
    247         u_int32_t dlfs_nfiles;    /* 40: number of allocated inodes */
    248         int32_t   dlfs_avail;     /* 44: blocks available for writing */
    249         int32_t   dlfs_uinodes;   /* 48: inodes in cache not yet on disk */
    250         ufs_daddr_t  dlfs_idaddr; /* 52: inode file disk address */
    251         u_int32_t dlfs_ifile;     /* 56: inode file inode number */
    252         ufs_daddr_t  dlfs_lastseg; /* 60: address of last segment written */
    253         ufs_daddr_t  dlfs_nextseg; /* 64: address of next segment to write */
    254         ufs_daddr_t  dlfs_curseg; /* 68: current segment being written */
    255         ufs_daddr_t  dlfs_offset; /* 72: offset in curseg for next partial */
    256         ufs_daddr_t  dlfs_lastpseg; /* 76: address of last partial written */
    257 	u_int32_t dlfs_inopf;     /* 80: v1: time stamp; v2: inodes per frag */
    258 #define dlfs_otstamp dlfs_inopf
    259 
    260 /* These are configuration parameters. */
    261         u_int32_t dlfs_minfree;   /* 84: minimum percentage of free blocks */
    262 
    263 /* These fields can be computed from the others. */
    264         u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
    265         u_int32_t dlfs_fsbpseg;     /* 96: fsb per segment */
    266         u_int32_t dlfs_inopb;     /* 100: inodes per block */
    267         u_int32_t dlfs_ifpb;      /* 104: IFILE entries per block */
    268         u_int32_t dlfs_sepb;      /* 108: SEGUSE entries per block */
    269         u_int32_t dlfs_nindir;    /* 112: indirect pointers per block */
    270         u_int32_t dlfs_nseg;      /* 116: number of segments */
    271         u_int32_t dlfs_nspf;      /* 120: number of sectors per fragment */
    272         u_int32_t dlfs_cleansz;   /* 124: cleaner info size in blocks */
    273         u_int32_t dlfs_segtabsz;  /* 128: segment table size in blocks */
    274         u_int32_t dlfs_segmask;   /* 132: calculate offset within a segment */
    275         u_int32_t dlfs_segshift;  /* 136: fast mult/div for segments */
    276         u_int32_t dlfs_bshift;    /* 140: calc block number from file offset */
    277         u_int32_t dlfs_ffshift;   /* 144: fast mult/div for frag from file */
    278         u_int32_t dlfs_fbshift;   /* 148: fast mult/div for frag from block */
    279         u_int64_t dlfs_bmask;     /* 152: calc block offset from file offset */
    280         u_int64_t dlfs_ffmask;    /* 160: calc frag offset from file offset */
    281         u_int64_t dlfs_fbmask;    /* 168: calc frag offset from block offset */
    282         u_int32_t dlfs_blktodb;   /* 176: blktodb and dbtoblk shift constant */
    283         u_int32_t dlfs_sushift;   /* 180: fast mult/div for segusage table */
    284 
    285         int32_t   dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
    286 #define LFS_MIN_SBINTERVAL      5  /* minimum superblock segment spacing */
    287 #define LFS_MAXNUMSB            10 /* 188: superblock disk offsets */
    288         ufs_daddr_t       dlfs_sboffs[LFS_MAXNUMSB];
    289 
    290 	u_int32_t dlfs_nclean;    /* 228: Number of clean segments */
    291 	u_char	  dlfs_fsmnt[MNAMELEN];	 /* 232: name mounted on */
    292 #define LFS_PF_CLEAN 0x1
    293 	u_int16_t dlfs_pflags;    /* 322: file system persistent flags */
    294 	int32_t   dlfs_dmeta;     /* 324: total number of dirty summaries */
    295 	u_int32_t dlfs_minfreeseg; /* 328: segs reserved for cleaner */
    296 	u_int32_t dlfs_sumsize;   /* 332: size of summary blocks */
    297 	u_int64_t dlfs_serial;    /* 336: serial number */
    298 	u_int32_t dlfs_ibsize;    /* 344: size of inode blocks */
    299 	ufs_daddr_t dlfs_start;   /* 348: start of segment 0 */
    300 	u_int64_t dlfs_tstamp;    /* 352: time stamp */
    301 #define LFS_44INODEFMT 0
    302 #define LFS_MAXINODEFMT 0
    303 	u_int32_t dlfs_inodefmt;  /* 360: inode format version */
    304 	u_int32_t dlfs_interleave; /* 364: segment interleave */
    305 	u_int32_t dlfs_ident;     /* 368: per-fs identifier */
    306 	u_int32_t dlfs_fsbtodb;   /* 372: fsbtodb abd dbtodsb shift constant */
    307 	int8_t    dlfs_pad[132];  /* 376: round to 512 bytes */
    308 /* Checksum -- last valid disk field. */
    309 	u_int32_t dlfs_cksum;     /* 508: checksum for superblock checking */
    310 };
    311 
    312 /* Maximum number of io's we can have pending at once */
    313 #define LFS_THROTTLE  16 /* XXX should be better paramtrized - ? */
    314 
    315 /* In-memory super block. */
    316 struct lfs {
    317         struct dlfs lfs_dlfs;           /* on-disk parameters */
    318 #define lfs_magic lfs_dlfs.dlfs_magic
    319 #define lfs_version lfs_dlfs.dlfs_version
    320 #define lfs_size lfs_dlfs.dlfs_size
    321 #define lfs_ssize lfs_dlfs.dlfs_ssize
    322 #define lfs_dsize lfs_dlfs.dlfs_dsize
    323 #define lfs_bsize lfs_dlfs.dlfs_bsize
    324 #define lfs_fsize lfs_dlfs.dlfs_fsize
    325 #define lfs_frag lfs_dlfs.dlfs_frag
    326 #define lfs_free lfs_dlfs.dlfs_free
    327 #define lfs_bfree lfs_dlfs.dlfs_bfree
    328 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
    329 #define lfs_avail lfs_dlfs.dlfs_avail
    330 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
    331 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
    332 #define lfs_ifile lfs_dlfs.dlfs_ifile
    333 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
    334 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
    335 #define lfs_curseg lfs_dlfs.dlfs_curseg
    336 #define lfs_offset lfs_dlfs.dlfs_offset
    337 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
    338 #define lfs_otstamp lfs_dlfs.dlfs_inopf
    339 #define lfs_inopf lfs_dlfs.dlfs_inopf
    340 #define lfs_minfree lfs_dlfs.dlfs_minfree
    341 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
    342 #define lfs_fsbpseg lfs_dlfs.dlfs_fsbpseg
    343 #define lfs_inopb lfs_dlfs.dlfs_inopb
    344 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
    345 #define lfs_sepb lfs_dlfs.dlfs_sepb
    346 #define lfs_nindir lfs_dlfs.dlfs_nindir
    347 #define lfs_nseg lfs_dlfs.dlfs_nseg
    348 #define lfs_nspf lfs_dlfs.dlfs_nspf
    349 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
    350 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
    351 #define lfs_segmask lfs_dlfs.dlfs_segmask
    352 #define lfs_segshift lfs_dlfs.dlfs_segshift
    353 #define lfs_bmask lfs_dlfs.dlfs_bmask
    354 #define lfs_bshift lfs_dlfs.dlfs_bshift
    355 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
    356 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
    357 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
    358 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
    359 #define lfs_blktodb lfs_dlfs.dlfs_blktodb
    360 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
    361 #define lfs_sushift lfs_dlfs.dlfs_sushift
    362 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
    363 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
    364 #define lfs_cksum lfs_dlfs.dlfs_cksum
    365 #define lfs_pflags lfs_dlfs.dlfs_pflags
    366 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
    367 #define lfs_nclean lfs_dlfs.dlfs_nclean
    368 #define lfs_dmeta lfs_dlfs.dlfs_dmeta
    369 #define lfs_minfreeseg lfs_dlfs.dlfs_minfreeseg
    370 #define lfs_sumsize lfs_dlfs.dlfs_sumsize
    371 #define lfs_serial lfs_dlfs.dlfs_serial
    372 #define lfs_ibsize lfs_dlfs.dlfs_ibsize
    373 #define lfs_start lfs_dlfs.dlfs_start
    374 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
    375 #define lfs_inodefmt lfs_dlfs.dlfs_inodefmt
    376 #define lfs_interleave lfs_dlfs.dlfs_interleave
    377 #define lfs_ident lfs_dlfs.dlfs_ident
    378 
    379 /* These fields are set at mount time and are meaningless on disk. */
    380 	struct segment *lfs_sp;		/* current segment being written */
    381 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
    382 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
    383 	pid_t	  lfs_lockpid;		/* pid of lock holder */
    384 	u_int32_t lfs_iocount;		/* number of ios pending */
    385 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
    386 	u_int32_t lfs_dirops;		/* count of active directory ops */
    387 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
    388 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
    389 	int8_t	  lfs_fmod;		/* super block modified flag */
    390 	int8_t	  lfs_ronly;		/* mounted read-only flag */
    391 #define LFS_NOTYET 0x01
    392 	int8_t	  lfs_flags;		/* currently unused flag */
    393 	u_int16_t lfs_activesb;         /* toggle between superblocks */
    394 #ifdef LFS_TRACK_IOS
    395 	daddr_t   lfs_pending[LFS_THROTTLE]; /* daddrs of pending writes */
    396 #endif /* LFS_TRACK_IOS */
    397 	daddr_t   lfs_sbactive;         /* disk address of in-progress sb write */
    398 	struct vnode *lfs_flushvp;      /* vnode being flushed */
    399 	struct vnode *lfs_unlockvp;     /* being inactivated in lfs_segunlock */
    400 	u_int32_t lfs_diropwait;	/* # procs waiting on dirop flush */
    401 	size_t lfs_devbsize;		/* Device block size */
    402 	size_t lfs_devbshift;		/* Device block shift */
    403 	struct lock lfs_freelock;
    404 	pid_t lfs_rfpid;		/* Process ID of roll-forward agent */
    405 	int       lfs_nadirop;		/* number of active dirop nodes */
    406 	long      lfs_ravail;           /* blocks pre-reserved for writing */
    407 };
    408 
    409 /*
    410  * Inode 0:	out-of-band inode number
    411  * Inode 1:	IFILE inode number
    412  * Inode 2:	root inode
    413  * Inode 3:	lost+found inode number
    414  */
    415 #define	LFS_UNUSED_INUM	0		/* out of band inode number */
    416 #define	LFS_IFILE_INUM	1		/* IFILE inode number */
    417 #define	LOSTFOUNDINO	3		/* lost+found inode number */
    418 #define	LFS_FIRST_INUM	4		/* first free inode number */
    419 
    420 /* Address calculations for metadata located in the inode */
    421 #define	S_INDIR(fs)	-NDADDR
    422 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
    423 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
    424 
    425 /* Unassigned disk addresses. */
    426 #define	UNASSIGNED	-1
    427 #define UNWRITTEN       -2
    428 
    429 /* Unused logical block number */
    430 #define LFS_UNUSED_LBN	-1
    431 
    432 typedef struct ifile IFILE;
    433 struct ifile {
    434 	u_int32_t if_version;		/* inode version number */
    435 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
    436 	ufs_daddr_t if_daddr;		/* inode disk address */
    437 	ino_t	  if_nextfree;		/* next-unallocated inode */
    438 	/* XXX - when inode format changes, this changes too */
    439 	u_int32_t if_atime_sec;		/* Last access time, seconds */
    440 	u_int32_t if_atime_nsec;	/* and nanoseconds */
    441 };
    442 
    443 typedef struct ifile_v1 IFILE_V1;
    444 struct ifile_v1 {
    445 	u_int32_t if_version;		/* inode version number */
    446 	ufs_daddr_t if_daddr;		/* inode disk address */
    447 	ino_t	  if_nextfree;		/* next-unallocated inode */
    448 #if LFS_ATIME_IFILE
    449 	struct timespec if_atime;	/* Last access time */
    450 #endif
    451 };
    452 
    453 /*
    454  * Cleaner information structure.  This resides in the ifile and is used
    455  * to pass information between the cleaner and the kernel.
    456  */
    457 typedef struct _cleanerinfo {
    458 	u_int32_t clean;		/* number of clean segments */
    459 	u_int32_t dirty;		/* number of dirty segments */
    460 	u_int32_t bfree;		/* disk blocks free */
    461 	int32_t   avail;		/* disk blocks available */
    462 	u_int32_t free_head;            /* head of the inode free list */
    463 	u_int32_t free_tail;            /* tail of the inode free list */
    464 } CLEANERINFO;
    465 
    466 #define	CLEANSIZE_SU(fs)						\
    467 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
    468 
    469 /*
    470  * All summary blocks are the same size, so we can always read a summary
    471  * block easily from a segment.
    472  */
    473 #define	LFS_V1_SUMMARY_SIZE	512
    474 #define	LFS_DFL_SUMMARY_SIZE	512
    475 
    476 /* On-disk segment summary information */
    477 typedef struct segsum_v1 SEGSUM_V1;
    478 struct segsum_v1 {
    479 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
    480 	u_int32_t ss_datasum;		/* 4: check sum of data */
    481 	u_int32_t ss_magic;		/* 8: segment summary magic number */
    482 #define SS_MAGIC	0x061561
    483 	ufs_daddr_t ss_next;		/* 12: next segment */
    484 	u_int32_t ss_create;		/* 16: creation time stamp */
    485 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
    486 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
    487 
    488 #define	SS_DIROP	0x01		/* segment begins a dirop */
    489 #define	SS_CONT		0x02		/* more partials to finish this write*/
    490 	u_int16_t ss_flags;		/* 24: used for directory operations */
    491 	u_int16_t ss_pad;		/* 26: extra space */
    492 	/* FINFO's and inode daddr's... */
    493 };
    494 
    495 typedef struct segsum SEGSUM;
    496 struct segsum {
    497 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
    498 	u_int32_t ss_datasum;		/* 4: check sum of data */
    499 	u_int32_t ss_magic;		/* 8: segment summary magic number */
    500 	ufs_daddr_t ss_next;		/* 12: next segment */
    501 	u_int32_t ss_ident;		/* 16: roll-forward fsid */
    502 #define ss_ocreate ss_ident /* ident is where create was in v1 */
    503 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
    504 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
    505 	u_int16_t ss_flags;		/* 24: used for directory operations */
    506 	u_int8_t  ss_pad[6];		/* 26: extra space */
    507 	u_int64_t ss_serial;		/* 32: serial number */
    508 	u_int64_t ss_create;		/* 40: time stamp */
    509 	/* FINFO's and inode daddr's... */
    510 };
    511 
    512 #define SEGSUM_SIZE(fs) ((fs)->lfs_version == 1 ? sizeof(SEGSUM_V1) : sizeof(SEGSUM))
    513 
    514 /* NINDIR is the number of indirects in a file system block. */
    515 #define	NINDIR(fs)	((fs)->lfs_nindir)
    516 
    517 /* INOPB is the number of inodes in a secondary storage block. */
    518 #define	INOPB(fs)	((fs)->lfs_inopb)
    519 /* INOPF is the number of inodes in a fragment. */
    520 #define INOPF(fs)       ((fs)->lfs_inopf)
    521 
    522 #define	blksize(fs, ip, lbn) \
    523 	(((lbn) >= NDADDR || (ip)->i_ffs_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
    524 	    ? (fs)->lfs_bsize \
    525 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs_size))))
    526 #define	blkoff(fs, loc)		((int)(loc) & (fs)->lfs_bmask)
    527 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
    528     ((int)((loc) & (fs)->lfs_ffmask))
    529 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
    530 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
    531 #define fragstodb(fs, b)	((b) << ((fs)->lfs_blktodb - (fs)->lfs_fbshift))
    532 #define dbtofrags(fs, b)	((b) >> ((fs)->lfs_blktodb - (fs)->lfs_fbshift))
    533 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
    534 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
    535 /* Same as above, but named like dbtob(), btodb() */
    536 #define fsbtob(fs, b)		((b) << ((fs)->lfs_bshift - \
    537 				(fs)->lfs_blktodb + (fs)->lfs_fsbtodb))
    538 #define btofsb(fs, b)		((b) >> ((fs)->lfs_bshift - \
    539 				(fs)->lfs_blktodb + (fs)->lfs_fsbtodb))
    540 #define fsbtofrags(fs, b)	((b) >> ((fs)->lfs_blktodb - (fs)->lfs_fbshift - \
    541 				(fs)->lfs_fsbtodb))
    542 #define fragstofsb(fs, b)	((b) << ((fs)->lfs_blktodb - (fs)->lfs_fbshift - \
    543 				(fs)->lfs_fsbtodb))
    544 #define btofrags(fs, b)		((b) >> (fs)->lfs_ffshift)
    545 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
    546 	((loc) >> (fs)->lfs_ffshift)
    547 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
    548 	((int)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
    549 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
    550 	((int)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
    551 #define fragstoblks(fs, frags)	/* calculates (frags / fs->lfs_frag) */ \
    552 	((frags) >> (fs)->lfs_fbshift)
    553 #define blkstofrags(fs, blks)	/* calculates (blks * fs->lfs_frag) */ \
    554 	((blks) << (fs)->lfs_fbshift)
    555 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */ \
    556 	((fsb) & ((fs)->lfs_frag - 1))
    557 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
    558 	((fsb) &~ ((fs)->lfs_frag - 1))
    559 #define dblksize(fs, dip, lbn) \
    560 	(((lbn) >= NDADDR || (dip)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
    561 	    ? (fs)->lfs_bsize \
    562 	    : (fragroundup(fs, blkoff(fs, (dip)->di_size))))
    563 
    564 #define segtod(fs, seg) (((fs)->lfs_version == 1     ?       \
    565 			   (fs)->lfs_ssize << (fs)->lfs_blktodb :       \
    566 			   btofsb((fs), (fs)->lfs_ssize)) * (seg))
    567 #define	dtosn(fs, daddr)	/* block address to segment number */	\
    568 	(((daddr) - (fs)->lfs_start) / segtod((fs), 1))
    569 #define sntod(fs, sn) 		/* segment number to disk address */	\
    570 	((ufs_daddr_t)(segtod((fs), (sn)) + (fs)->lfs_start))
    571 
    572 /* Read in the block with the cleaner info from the ifile. */
    573 #define LFS_CLEANERINFO(CP, F, BP) {					\
    574 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    575 	if (bread((F)->lfs_ivnode,					\
    576 	    (ufs_daddr_t)0, (F)->lfs_bsize, NOCRED, &(BP)))		\
    577 		panic("lfs: ifile read");				\
    578 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    579 }
    580 
    581 /* Synchronize the Ifile cleaner info with current avail and bfree */
    582 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do {                \
    583     if ((w) || (cip)->bfree != (fs)->lfs_bfree ||                \
    584         (cip)->avail != (fs)->lfs_avail - (fs)->lfs_ravail) {    \
    585 	(cip)->bfree = (fs)->lfs_bfree;                          \
    586         (cip)->avail = (fs)->lfs_avail - (fs)->lfs_ravail;       \
    587 	(void) VOP_BWRITE(bp); /* Ifile */                       \
    588     } else                                                       \
    589 	brelse(bp);                                              \
    590 } while(0)
    591 
    592 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do {                       \
    593 	if ((FS)->lfs_version > 1) {                                    \
    594 		LFS_CLEANERINFO((CIP), (FS), (BP));                     \
    595 		(FS)->lfs_free = (CIP)->free_head;			\
    596 		brelse(BP);                                             \
    597 	}								\
    598 	*(FREEP) = (FS)->lfs_free;					\
    599 } while (0)
    600 
    601 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do {                         \
    602 	(FS)->lfs_free = (VAL);						\
    603 	if ((FS)->lfs_version > 1) {                                    \
    604 		LFS_CLEANERINFO((CIP), (FS), (BP));                     \
    605 		(CIP)->free_head = (VAL);                 		\
    606 		VOP_BWRITE(BP);                                         \
    607 	}                                                               \
    608 } while (0)
    609 
    610 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do {                       \
    611 	LFS_CLEANERINFO((CIP), (FS), (BP));                     	\
    612 	*(FREEP) = (CIP)->free_tail;					\
    613 	brelse(BP);                                             	\
    614 } while (0)
    615 
    616 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do {                         \
    617 	LFS_CLEANERINFO((CIP), (FS), (BP));                     	\
    618 	(CIP)->free_tail = (VAL);                 			\
    619 	VOP_BWRITE(BP);                                         	\
    620 } while (0)
    621 
    622 /*
    623  * XXX - v1 compatibility code is not allowed to touch if_atime, since it
    624  * may not be mapped!
    625  */
    626 /* Read in the block with a specific inode from the ifile. */
    627 #define	LFS_IENTRY(IP, F, IN, BP) {					\
    628 	int _e;								\
    629 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    630 	if ((_e = bread((F)->lfs_ivnode,				\
    631     	(IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,	\
    632     	(F)->lfs_bsize, NOCRED, &(BP))) != 0)				\
    633 		panic("lfs: ifile read %d", _e);			\
    634 	if((F)->lfs_version == 1)					\
    635 		(IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data + (IN) % (F)->lfs_ifpb); \
    636 	else								\
    637 		(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;	\
    638 }
    639 
    640 /* Read in the block with a specific segment usage entry from the ifile. */
    641 #define	LFS_SEGENTRY(SP, F, IN, BP) {					\
    642 	int _e;								\
    643 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    644 	if ((_e = bread((F)->lfs_ivnode,				\
    645 	    ((IN) / (F)->lfs_sepb) + (F)->lfs_cleansz,			\
    646 	    (F)->lfs_bsize, NOCRED, &(BP))) != 0)			\
    647 		panic("lfs: ifile read: %d", _e);			\
    648 	if ((F)->lfs_version == 1)					\
    649 		(SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data +		\
    650 			((IN) & ((F)->lfs_sepb - 1)));			\
    651 	else								\
    652 		(SP) = (SEGUSE *)(BP)->b_data + ((IN) % (F)->lfs_sepb);	\
    653 }
    654 
    655 /* Determine if a buffer belongs to the ifile */
    656 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
    657 
    658 /*
    659  * Structures used by lfs_bmapv and lfs_markv to communicate information
    660  * about inodes and data blocks.
    661  */
    662 typedef struct block_info {
    663 	ino_t	bi_inode;		/* inode # */
    664 	ufs_daddr_t bi_lbn;		/* logical block w/in file */
    665 	ufs_daddr_t bi_daddr;		/* disk address of block */
    666 	u_int64_t   bi_segcreate;	/* origin segment create time */
    667 	int	bi_version;		/* file version number */
    668 	void	*bi_bp;			/* data buffer */
    669 	int     bi_size;		/* size of the block (if fragment) */
    670 } BLOCK_INFO;
    671 
    672 /* Compatibility for 1.5 binaries */
    673 typedef struct block_info_15 {
    674 	ino_t	bi_inode;		/* inode # */
    675 	ufs_daddr_t bi_lbn;		/* logical block w/in file */
    676 	ufs_daddr_t bi_daddr;		/* disk address of block */
    677 	u_int32_t   bi_segcreate;	/* origin segment create time */
    678 	int	bi_version;		/* file version number */
    679 	void	*bi_bp;			/* data buffer */
    680 	int     bi_size;		/* size of the block (if fragment) */
    681 } BLOCK_INFO_15;
    682 
    683 /* In-memory description of a segment about to be written. */
    684 struct segment {
    685 	struct lfs	 *fs;		/* file system pointer */
    686 	struct buf	**bpp;		/* pointer to buffer array */
    687 	struct buf	**cbpp;		/* pointer to next available bp */
    688 	struct buf	**start_bpp;	/* pointer to first bp in this set */
    689 	struct buf	 *ibp;		/* buffer pointer to inode page */
    690 	struct dinode    *idp;          /* pointer to ifile dinode */
    691 	struct finfo	 *fip;		/* current fileinfo pointer */
    692 	struct vnode	 *vp;		/* vnode being gathered */
    693 	void	 *segsum;		/* segment summary info */
    694 	u_int32_t ninodes;		/* number of inodes in this segment */
    695 	u_int32_t seg_bytes_left;	/* bytes left in segment */
    696 	u_int32_t sum_bytes_left;	/* bytes left in summary block */
    697 	u_int32_t seg_number;		/* number of this segment */
    698 	ufs_daddr_t *start_lbp;		/* beginning lbn for this set */
    699 
    700 #define	SEGM_CKP	0x01		/* doing a checkpoint */
    701 #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
    702 #define	SEGM_SYNC	0x04		/* wait for segment */
    703 #define	SEGM_PROT	0x08		/* don't inactivate at segunlock */
    704 	u_int16_t seg_flags;		/* run-time flags for this segment */
    705 };
    706 
    707 /*
    708  * Macros for determining free space on the disk, with the variable metadata
    709  * of segment summaries and inode blocks taken into account.
    710  */
    711 /* Estimate number of clean blocks not available for writing */
    712 #define LFS_EST_CMETA(F) (int32_t)((((F)->lfs_dmeta *                        \
    713 				     (int64_t)(F)->lfs_nclean) /             \
    714 				      ((F)->lfs_nseg - (F)->lfs_nclean)))
    715 
    716 /* Estimate total size of the disk not including metadata */
    717 #define LFS_EST_NONMETA(F) ((F)->lfs_dsize - (F)->lfs_dmeta - LFS_EST_CMETA(F))
    718 
    719 /* Estimate number of blocks actually available for writing */
    720 #define LFS_EST_BFREE(F) ((F)->lfs_bfree - LFS_EST_CMETA(F) - (F)->lfs_dmeta)
    721 
    722 /* Amount of non-meta space not available to mortal man */
    723 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) *                      \
    724                                    (u_int64_t)(F)->lfs_minfree) /            \
    725 			          100)
    726 
    727 /* Can credential C write BB blocks */
    728 #define ISSPACE(F, BB, C)						\
    729 	((((C) == NOCRED || (C)->cr_uid == 0) &&			\
    730           LFS_EST_BFREE(F) >= (BB)) ||					\
    731 	 ((C)->cr_uid != 0 && IS_FREESPACE(F, BB)))
    732 
    733 /* Can an ordinary user write BB blocks */
    734 #define IS_FREESPACE(F, BB)						\
    735           (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
    736 
    737 /* Statistics Counters */
    738 struct lfs_stats {
    739 	u_int	segsused;
    740 	u_int	psegwrites;
    741 	u_int	psyncwrites;
    742 	u_int	pcleanwrites;
    743 	u_int	blocktot;
    744 	u_int	cleanblocks;
    745 	u_int	ncheckpoints;
    746 	u_int	nwrites;
    747 	u_int	nsync_writes;
    748 	u_int	wait_exceeded;
    749 	u_int	write_exceeded;
    750 	u_int	flush_invoked;
    751 	u_int	vflush_invoked;
    752 };
    753 extern struct lfs_stats lfs_stats;
    754