Home | History | Annotate | Line # | Download | only in lfs
lfs.h revision 1.150
      1 /*	$NetBSD: lfs.h,v 1.150 2013/06/08 22:23:52 dholland Exp $	*/
      2 
      3 /*  from NetBSD: dinode.h,v 1.22 2013/01/22 09:39:18 dholland Exp  */
      4 /*  from NetBSD: dir.h,v 1.21 2009/07/22 04:49:19 dholland Exp  */
      5 
      6 /*-
      7  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Konrad E. Schroder <perseant (at) hhhh.org>.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 /*-
     35  * Copyright (c) 1991, 1993
     36  *	The Regents of the University of California.  All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  *
     62  *	@(#)lfs.h	8.9 (Berkeley) 5/8/95
     63  */
     64 /*
     65  * Copyright (c) 2002 Networks Associates Technology, Inc.
     66  * All rights reserved.
     67  *
     68  * This software was developed for the FreeBSD Project by Marshall
     69  * Kirk McKusick and Network Associates Laboratories, the Security
     70  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     71  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     72  * research program
     73  *
     74  * Copyright (c) 1982, 1989, 1993
     75  *	The Regents of the University of California.  All rights reserved.
     76  * (c) UNIX System Laboratories, Inc.
     77  * All or some portions of this file are derived from material licensed
     78  * to the University of California by American Telephone and Telegraph
     79  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     80  * the permission of UNIX System Laboratories, Inc.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. Neither the name of the University nor the names of its contributors
     91  *    may be used to endorse or promote products derived from this software
     92  *    without specific prior written permission.
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    104  * SUCH DAMAGE.
    105  *
    106  *	@(#)dinode.h	8.9 (Berkeley) 3/29/95
    107  */
    108 /*
    109  * Copyright (c) 1982, 1986, 1989, 1993
    110  *	The Regents of the University of California.  All rights reserved.
    111  * (c) UNIX System Laboratories, Inc.
    112  * All or some portions of this file are derived from material licensed
    113  * to the University of California by American Telephone and Telegraph
    114  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    115  * the permission of UNIX System Laboratories, Inc.
    116  *
    117  * Redistribution and use in source and binary forms, with or without
    118  * modification, are permitted provided that the following conditions
    119  * are met:
    120  * 1. Redistributions of source code must retain the above copyright
    121  *    notice, this list of conditions and the following disclaimer.
    122  * 2. Redistributions in binary form must reproduce the above copyright
    123  *    notice, this list of conditions and the following disclaimer in the
    124  *    documentation and/or other materials provided with the distribution.
    125  * 3. Neither the name of the University nor the names of its contributors
    126  *    may be used to endorse or promote products derived from this software
    127  *    without specific prior written permission.
    128  *
    129  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    130  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    131  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    132  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    133  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    134  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    135  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    136  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    137  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    138  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    139  * SUCH DAMAGE.
    140  *
    141  *	@(#)dir.h	8.5 (Berkeley) 4/27/95
    142  */
    143 
    144 /*
    145  * NOTE: COORDINATE ON-DISK FORMAT CHANGES WITH THE FREEBSD PROJECT.
    146  */
    147 
    148 #ifndef _UFS_LFS_LFS_H_
    149 #define _UFS_LFS_LFS_H_
    150 
    151 #include <sys/rwlock.h>
    152 #include <sys/mutex.h>
    153 #include <sys/queue.h>
    154 #include <sys/condvar.h>
    155 #include <sys/mount.h>
    156 #include <sys/pool.h>
    157 
    158 /*
    159  * Compile-time options for LFS.
    160  */
    161 #define LFS_IFIND_RETRIES  16
    162 #define LFS_LOGLENGTH      1024 /* size of debugging log */
    163 #define LFS_MAX_ACTIVE	   10	/* Dirty segments before ckp forced */
    164 
    165 /*
    166  * Fixed filesystem layout parameters
    167  */
    168 #define	LFS_LABELPAD	8192		/* LFS label size */
    169 #define	LFS_SBPAD	8192		/* LFS superblock size */
    170 
    171 #define	LFS_UNUSED_INUM	0		/* 0: out of band inode number */
    172 #define	LFS_IFILE_INUM	1		/* 1: IFILE inode number */
    173 					/* 2: Root inode number */
    174 #define	LFS_LOSTFOUNDINO 3		/* 3: lost+found inode number */
    175 #define	LFS_FIRST_INUM	4		/* 4: first free inode number */
    176 
    177 /*
    178  * The root inode is the root of the file system.  Inode 0 can't be used for
    179  * normal purposes and historically bad blocks were linked to inode 1, thus
    180  * the root inode is 2.  (Inode 1 is no longer used for this purpose, however
    181  * numerous dump tapes make this assumption, so we are stuck with it).
    182  */
    183 #define	ULFS_ROOTINO	((ino_t)2)
    184 
    185 /*
    186  * The Whiteout inode# is a dummy non-zero inode number which will
    187  * never be allocated to a real file.  It is used as a place holder
    188  * in the directory entry which has been tagged as a LFS_DT_WHT entry.
    189  * See the comments about ULFS_ROOTINO above.
    190  */
    191 #define	ULFS_WINO	((ino_t)1)
    192 
    193 
    194 #define	LFS_V1_SUMMARY_SIZE	512     /* V1 fixed summary size */
    195 #define	LFS_DFL_SUMMARY_SIZE	512	/* Default summary size */
    196 
    197 #define LFS_MAX_DADDR	0x7fffffff	/* Highest addressable fsb */
    198 
    199 #define LFS_MAXNAMLEN	255		/* maximum name length in a dir */
    200 
    201 #define ULFS_NXADDR	2
    202 #define	ULFS_NDADDR	12		/* Direct addresses in inode. */
    203 #define	ULFS_NIADDR	3		/* Indirect addresses in inode. */
    204 
    205 /*
    206  * Adjustable filesystem parameters
    207  */
    208 #define MIN_FREE_SEGS	20
    209 #define MIN_RESV_SEGS	15
    210 #ifndef LFS_ATIME_IFILE
    211 # define LFS_ATIME_IFILE 0 /* Store atime info in ifile (optional in LFSv1) */
    212 #endif
    213 #define LFS_MARKV_MAXBLKCNT	65536	/* Max block count for lfs_markv() */
    214 
    215 /* Misc. definitions */
    216 #define BW_CLEAN	1		/* Flag for lfs_bwrite_ext() */
    217 #define PG_DELWRI	PG_PAGER1	/* Local def for delayed pageout */
    218 
    219 /* Resource limits */
    220 #define	LFS_MAX_RESOURCE(x, u)	(((x) >> 2) - 10 * (u))
    221 #define	LFS_WAIT_RESOURCE(x, u)	(((x) >> 1) - ((x) >> 3) - 10 * (u))
    222 #define	LFS_INVERSE_MAX_RESOURCE(x, u)	(((x) + 10 * (u)) << 2)
    223 #define LFS_MAX_BUFS	    LFS_MAX_RESOURCE(nbuf, 1)
    224 #define LFS_WAIT_BUFS	    LFS_WAIT_RESOURCE(nbuf, 1)
    225 #define LFS_INVERSE_MAX_BUFS(n)	LFS_INVERSE_MAX_RESOURCE(n, 1)
    226 #define LFS_MAX_BYTES	    LFS_MAX_RESOURCE(bufmem_lowater, PAGE_SIZE)
    227 #define LFS_INVERSE_MAX_BYTES(n) LFS_INVERSE_MAX_RESOURCE(n, PAGE_SIZE)
    228 #define LFS_WAIT_BYTES	    LFS_WAIT_RESOURCE(bufmem_lowater, PAGE_SIZE)
    229 #define LFS_MAX_DIROP	    ((desiredvnodes >> 2) + (desiredvnodes >> 3))
    230 #define SIZEOF_DIROP(fs)	(2 * ((fs)->lfs_bsize + LFS_DINODE1_SIZE))
    231 #define LFS_MAX_FSDIROP(fs)						\
    232 	((fs)->lfs_nclean <= (fs)->lfs_resvseg ? 0 :			\
    233 	 (((fs)->lfs_nclean - (fs)->lfs_resvseg) * (fs)->lfs_ssize) /	\
    234           (2 * SIZEOF_DIROP(fs)))
    235 #define LFS_MAX_PAGES	lfs_max_pages()
    236 #define LFS_WAIT_PAGES	lfs_wait_pages()
    237 #define LFS_BUFWAIT	    2	/* How long to wait if over *_WAIT_* */
    238 
    239 #ifdef _KERNEL
    240 int lfs_wait_pages(void);
    241 int lfs_max_pages(void);
    242 #endif /* _KERNEL */
    243 
    244 /* How starved can we be before we start holding back page writes */
    245 #define LFS_STARVED_FOR_SEGS(fs) ((fs)->lfs_nclean < (fs)->lfs_resvseg)
    246 
    247 /*
    248  * Reserved blocks for lfs_malloc
    249  */
    250 
    251 /* Structure to keep reserved blocks */
    252 typedef struct lfs_res_blk {
    253 	void *p;
    254 	LIST_ENTRY(lfs_res_blk) res;
    255 	int size;
    256 	char inuse;
    257 } res_t;
    258 
    259 /* Types for lfs_newbuf and lfs_malloc */
    260 #define LFS_NB_UNKNOWN -1
    261 #define LFS_NB_SUMMARY	0
    262 #define LFS_NB_SBLOCK	1
    263 #define LFS_NB_IBLOCK	2
    264 #define LFS_NB_CLUSTER	3
    265 #define LFS_NB_CLEAN	4
    266 #define LFS_NB_BLKIOV	5
    267 #define LFS_NB_COUNT	6 /* always last */
    268 
    269 /* Number of reserved memory blocks of each type */
    270 #define LFS_N_SUMMARIES 2
    271 #define LFS_N_SBLOCKS	1   /* Always 1, to throttle superblock writes */
    272 #define LFS_N_IBLOCKS	16  /* In theory ssize/bsize; in practice around 2 */
    273 #define LFS_N_CLUSTERS	16  /* In theory ssize/MAXPHYS */
    274 #define LFS_N_CLEAN	0
    275 #define LFS_N_BLKIOV	1
    276 
    277 /* Total count of "large" (non-pool) types */
    278 #define LFS_N_TOTAL (LFS_N_SUMMARIES + LFS_N_SBLOCKS + LFS_N_IBLOCKS +	\
    279 		     LFS_N_CLUSTERS + LFS_N_CLEAN + LFS_N_BLKIOV)
    280 
    281 /* Counts for pool types */
    282 #define LFS_N_CL	LFS_N_CLUSTERS
    283 #define LFS_N_BPP	2
    284 #define LFS_N_SEG	2
    285 
    286 /*
    287  * Directories
    288  */
    289 
    290 /*
    291  * A directory consists of some number of blocks of LFS_DIRBLKSIZ
    292  * bytes, where LFS_DIRBLKSIZ is chosen such that it can be transferred
    293  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
    294  *
    295  * Each LFS_DIRBLKSIZ byte block contains some number of directory entry
    296  * structures, which are of variable length.  Each directory entry has
    297  * a struct lfs_direct at the front of it, containing its inode number,
    298  * the length of the entry, and the length of the name contained in
    299  * the entry.  These are followed by the name padded to a 4 byte boundary.
    300  * All names are guaranteed null terminated.
    301  * The maximum length of a name in a directory is LFS_MAXNAMLEN.
    302  *
    303  * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
    304  * a directory entry.  Free space in a directory is represented by
    305  * entries which have dp->d_reclen > DIRSIZ(fmt, dp).  All LFS_DIRBLKSIZ bytes
    306  * in a directory block are claimed by the directory entries.  This
    307  * usually results in the last entry in a directory having a large
    308  * dp->d_reclen.  When entries are deleted from a directory, the
    309  * space is returned to the previous entry in the same directory
    310  * block by increasing its dp->d_reclen.  If the first entry of
    311  * a directory block is free, then its dp->d_ino is set to 0.
    312  * Entries other than the first in a directory do not normally have
    313  * dp->d_ino set to 0.
    314  */
    315 
    316 /*
    317  * Directory block size.
    318  */
    319 #undef	LFS_DIRBLKSIZ
    320 #define	LFS_DIRBLKSIZ	DEV_BSIZE
    321 
    322 /*
    323  * Convert between stat structure types and directory types.
    324  */
    325 #define	LFS_IFTODT(mode)	(((mode) & 0170000) >> 12)
    326 #define	LFS_DTTOIF(dirtype)	((dirtype) << 12)
    327 
    328 /*
    329  * The LFS_DIRSIZ macro gives the minimum record length which will hold
    330  * the directory entry.  This requires the amount of space in struct lfs_direct
    331  * without the d_name field, plus enough space for the name with a terminating
    332  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
    333  */
    334 #define	LFS_DIRECTSIZ(namlen) \
    335 	((sizeof(struct lfs_direct) - (LFS_MAXNAMLEN+1)) + (((namlen)+1 + 3) &~ 3))
    336 
    337 #if (BYTE_ORDER == LITTLE_ENDIAN)
    338 #define LFS_DIRSIZ(oldfmt, dp, needswap)	\
    339     (((oldfmt) && !(needswap)) ?		\
    340     LFS_DIRECTSIZ((dp)->d_type) : LFS_DIRECTSIZ((dp)->d_namlen))
    341 #else
    342 #define LFS_DIRSIZ(oldfmt, dp, needswap)	\
    343     (((oldfmt) && (needswap)) ?			\
    344     LFS_DIRECTSIZ((dp)->d_type) : LFS_DIRECTSIZ((dp)->d_namlen))
    345 #endif
    346 
    347 /* Constants for the first argument of LFS_DIRSIZ */
    348 #define LFS_OLDDIRFMT	1
    349 #define LFS_NEWDIRFMT	0
    350 
    351 /*
    352  * Theoretically, directories can be more than 2Gb in length; however, in
    353  * practice this seems unlikely. So, we define the type doff_t as a 32-bit
    354  * quantity to keep down the cost of doing lookup on a 32-bit machine.
    355  */
    356 #define	doff_t		int32_t
    357 #define	lfs_doff_t	int32_t
    358 #define	MAXDIRSIZE	(0x7fffffff)
    359 
    360 #define	LFS_MAXNAMLEN	255
    361 
    362 /*
    363  * File types for d_type
    364  */
    365 #define	LFS_DT_UNKNOWN	 0
    366 #define	LFS_DT_FIFO	 1
    367 #define	LFS_DT_CHR	 2
    368 #define	LFS_DT_DIR	 4
    369 #define	LFS_DT_BLK	 6
    370 #define	LFS_DT_REG	 8
    371 #define	LFS_DT_LNK	10
    372 #define	LFS_DT_SOCK	12
    373 #define	LFS_DT_WHT	14
    374 
    375 /*
    376  * (See notes above)
    377  */
    378 #define d_ino d_fileno
    379 struct lfs_direct {
    380 	u_int32_t d_fileno;		/* inode number of entry */
    381 	u_int16_t d_reclen;		/* length of this record */
    382 	u_int8_t  d_type; 		/* file type, see below */
    383 	u_int8_t  d_namlen;		/* length of string in d_name */
    384 	char	  d_name[LFS_MAXNAMLEN + 1];/* name with length <= LFS_MAXNAMLEN */
    385 };
    386 
    387 /*
    388  * Template for manipulating directories.  Should use struct lfs_direct's,
    389  * but the name field is LFS_MAXNAMLEN - 1, and this just won't do.
    390  */
    391 struct lfs_dirtemplate {
    392 	u_int32_t	dot_ino;
    393 	int16_t		dot_reclen;
    394 	u_int8_t	dot_type;
    395 	u_int8_t	dot_namlen;
    396 	char		dot_name[4];	/* must be multiple of 4 */
    397 	u_int32_t	dotdot_ino;
    398 	int16_t		dotdot_reclen;
    399 	u_int8_t	dotdot_type;
    400 	u_int8_t	dotdot_namlen;
    401 	char		dotdot_name[4];	/* ditto */
    402 };
    403 
    404 /*
    405  * This is the old format of directories, sans type element.
    406  */
    407 struct lfs_odirtemplate {
    408 	u_int32_t	dot_ino;
    409 	int16_t		dot_reclen;
    410 	u_int16_t	dot_namlen;
    411 	char		dot_name[4];	/* must be multiple of 4 */
    412 	u_int32_t	dotdot_ino;
    413 	int16_t		dotdot_reclen;
    414 	u_int16_t	dotdot_namlen;
    415 	char		dotdot_name[4];	/* ditto */
    416 };
    417 
    418 /*
    419  * Inodes
    420  */
    421 
    422 /*
    423  * A dinode contains all the meta-data associated with a ULFS file.
    424  * This structure defines the on-disk format of a dinode. Since
    425  * this structure describes an on-disk structure, all its fields
    426  * are defined by types with precise widths.
    427  */
    428 
    429 struct ulfs1_dinode {
    430 	u_int16_t	di_mode;	/*   0: IFMT, permissions; see below. */
    431 	int16_t		di_nlink;	/*   2: File link count. */
    432 	union {
    433 		u_int16_t oldids[2];	/*   4: Ffs: old user and group ids. */
    434 		u_int32_t inumber;	/*   4: Lfs: inode number. */
    435 	} di_u;
    436 	u_int64_t	di_size;	/*   8: File byte count. */
    437 	int32_t		di_atime;	/*  16: Last access time. */
    438 	int32_t		di_atimensec;	/*  20: Last access time. */
    439 	int32_t		di_mtime;	/*  24: Last modified time. */
    440 	int32_t		di_mtimensec;	/*  28: Last modified time. */
    441 	int32_t		di_ctime;	/*  32: Last inode change time. */
    442 	int32_t		di_ctimensec;	/*  36: Last inode change time. */
    443 	int32_t		di_db[ULFS_NDADDR]; /*  40: Direct disk blocks. */
    444 	int32_t		di_ib[ULFS_NIADDR]; /*  88: Indirect disk blocks. */
    445 	u_int32_t	di_flags;	/* 100: Status flags (chflags). */
    446 	u_int32_t	di_blocks;	/* 104: Blocks actually held. */
    447 	int32_t		di_gen;		/* 108: Generation number. */
    448 	u_int32_t	di_uid;		/* 112: File owner. */
    449 	u_int32_t	di_gid;		/* 116: File group. */
    450 	u_int64_t	di_modrev;	/* 120: i_modrev for NFSv4 */
    451 };
    452 
    453 struct ulfs2_dinode {
    454 	u_int16_t	di_mode;	/*   0: IFMT, permissions; see below. */
    455 	int16_t		di_nlink;	/*   2: File link count. */
    456 	u_int32_t	di_uid;		/*   4: File owner. */
    457 	u_int32_t	di_gid;		/*   8: File group. */
    458 	u_int32_t	di_blksize;	/*  12: Inode blocksize. */
    459 	u_int64_t	di_size;	/*  16: File byte count. */
    460 	u_int64_t	di_blocks;	/*  24: Bytes actually held. */
    461 	int64_t		di_atime;	/*  32: Last access time. */
    462 	int64_t		di_mtime;	/*  40: Last modified time. */
    463 	int64_t		di_ctime;	/*  48: Last inode change time. */
    464 	int64_t		di_birthtime;	/*  56: Inode creation time. */
    465 	int32_t		di_mtimensec;	/*  64: Last modified time. */
    466 	int32_t		di_atimensec;	/*  68: Last access time. */
    467 	int32_t		di_ctimensec;	/*  72: Last inode change time. */
    468 	int32_t		di_birthnsec;	/*  76: Inode creation time. */
    469 	int32_t		di_gen;		/*  80: Generation number. */
    470 	u_int32_t	di_kernflags;	/*  84: Kernel flags. */
    471 	u_int32_t	di_flags;	/*  88: Status flags (chflags). */
    472 	int32_t		di_extsize;	/*  92: External attributes block. */
    473 	int64_t		di_extb[ULFS_NXADDR];/* 96: External attributes block. */
    474 	int64_t		di_db[ULFS_NDADDR]; /* 112: Direct disk blocks. */
    475 	int64_t		di_ib[ULFS_NIADDR]; /* 208: Indirect disk blocks. */
    476 	u_int64_t	di_modrev;	/* 232: i_modrev for NFSv4 */
    477 	int64_t		di_spare[2];	/* 240: Reserved; currently unused */
    478 };
    479 
    480 /*
    481  * The di_db fields may be overlaid with other information for
    482  * file types that do not have associated disk storage. Block
    483  * and character devices overlay the first data block with their
    484  * dev_t value. Short symbolic links place their path in the
    485  * di_db area.
    486  */
    487 #define	di_inumber	di_u.inumber
    488 #define	di_ogid		di_u.oldids[1]
    489 #define	di_ouid		di_u.oldids[0]
    490 #define	di_rdev		di_db[0]
    491 
    492 /* Size of the on-disk inode. */
    493 #define	LFS_DINODE1_SIZE	(sizeof(struct ulfs1_dinode))	/* 128 */
    494 #define	LFS_DINODE2_SIZE	(sizeof(struct ulfs2_dinode))
    495 
    496 /* File types, found in the upper bits of di_mode. */
    497 #define	LFS_IFMT	0170000		/* Mask of file type. */
    498 #define	LFS_IFIFO	0010000		/* Named pipe (fifo). */
    499 #define	LFS_IFCHR	0020000		/* Character device. */
    500 #define	LFS_IFDIR	0040000		/* Directory file. */
    501 #define	LFS_IFBLK	0060000		/* Block device. */
    502 #define	LFS_IFREG	0100000		/* Regular file. */
    503 #define	LFS_IFLNK	0120000		/* Symbolic link. */
    504 #define	LFS_IFSOCK	0140000		/* UNIX domain socket. */
    505 #define	LFS_IFWHT	0160000		/* Whiteout. */
    506 
    507 /*
    508  * Maximum length of a symlink that can be stored within the inode.
    509  */
    510 #define ULFS1_MAXSYMLINKLEN	((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int32_t))
    511 #define ULFS2_MAXSYMLINKLEN	((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int64_t))
    512 
    513 #define ULFS_MAXSYMLINKLEN(ip) \
    514 	((ip)->i_ump->um_fstype == ULFS1) ? \
    515 	ULFS1_MAXSYMLINKLEN : ULFS2_MAXSYMLINKLEN
    516 
    517 /*
    518  * "struct buf" associated definitions
    519  */
    520 
    521 /* Unassigned disk addresses. */
    522 #define	UNASSIGNED	-1
    523 #define UNWRITTEN	-2
    524 
    525 /* Unused logical block number */
    526 #define LFS_UNUSED_LBN	-1
    527 
    528 /* Determine if a buffer belongs to the ifile */
    529 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
    530 
    531 # define LFS_LOCK_BUF(bp) do {						\
    532 	if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) {	\
    533 		mutex_enter(&lfs_lock);					\
    534 		++locked_queue_count;					\
    535 		locked_queue_bytes += bp->b_bufsize;			\
    536 		mutex_exit(&lfs_lock);					\
    537 	}								\
    538 	(bp)->b_flags |= B_LOCKED;					\
    539 } while (0)
    540 
    541 # define LFS_UNLOCK_BUF(bp) do {					\
    542 	if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {	\
    543 		mutex_enter(&lfs_lock);					\
    544 		--locked_queue_count;					\
    545 		locked_queue_bytes -= bp->b_bufsize;			\
    546 		if (locked_queue_count < LFS_WAIT_BUFS &&		\
    547 		    locked_queue_bytes < LFS_WAIT_BYTES)		\
    548 			cv_broadcast(&locked_queue_cv);			\
    549 		mutex_exit(&lfs_lock);					\
    550 	}								\
    551 	(bp)->b_flags &= ~B_LOCKED;					\
    552 } while (0)
    553 
    554 #ifdef _KERNEL
    555 
    556 extern u_long bufmem_lowater, bufmem_hiwater; /* XXX */
    557 
    558 # define LFS_IS_MALLOC_BUF(bp) ((bp)->b_iodone == lfs_callback)
    559 
    560 # ifdef DEBUG
    561 #  define LFS_DEBUG_COUNTLOCKED(m) do {					\
    562 	if (lfs_debug_log_subsys[DLOG_LLIST]) {				\
    563 		lfs_countlocked(&locked_queue_count, &locked_queue_bytes, (m)); \
    564 		cv_broadcast(&locked_queue_cv);				\
    565 	}								\
    566 } while (0)
    567 # else
    568 #  define LFS_DEBUG_COUNTLOCKED(m)
    569 # endif
    570 
    571 /* log for debugging writes to the Ifile */
    572 # ifdef DEBUG
    573 struct lfs_log_entry {
    574 	const char *op;
    575 	const char *file;
    576 	int pid;
    577 	int line;
    578 	daddr_t block;
    579 	unsigned long flags;
    580 };
    581 extern int lfs_lognum;
    582 extern struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
    583 #  define LFS_BWRITE_LOG(bp) lfs_bwrite_log((bp), __FILE__, __LINE__)
    584 #  define LFS_ENTER_LOG(theop, thefile, theline, lbn, theflags, thepid) do {\
    585 	int _s;								\
    586 									\
    587 	mutex_enter(&lfs_lock);						\
    588 	_s = splbio();							\
    589 	lfs_log[lfs_lognum].op = theop;					\
    590 	lfs_log[lfs_lognum].file = thefile;				\
    591 	lfs_log[lfs_lognum].line = (theline);				\
    592 	lfs_log[lfs_lognum].pid = (thepid);				\
    593 	lfs_log[lfs_lognum].block = (lbn);				\
    594 	lfs_log[lfs_lognum].flags = (theflags);				\
    595 	lfs_lognum = (lfs_lognum + 1) % LFS_LOGLENGTH;			\
    596 	splx(_s);							\
    597 	mutex_exit(&lfs_lock);						\
    598 } while (0)
    599 
    600 #  define LFS_BCLEAN_LOG(fs, bp) do {					\
    601 	if ((bp)->b_vp == (fs)->lfs_ivnode)				\
    602 		LFS_ENTER_LOG("clear", __FILE__, __LINE__,		\
    603 			      bp->b_lblkno, bp->b_flags, curproc->p_pid);\
    604 } while (0)
    605 
    606 /* Must match list in lfs_vfsops.c ! */
    607 #  define DLOG_RF     0  /* roll forward */
    608 #  define DLOG_ALLOC  1  /* inode alloc */
    609 #  define DLOG_AVAIL  2  /* lfs_{,r,f}avail */
    610 #  define DLOG_FLUSH  3  /* flush */
    611 #  define DLOG_LLIST  4  /* locked list accounting */
    612 #  define DLOG_WVNODE 5  /* vflush/writevnodes verbose */
    613 #  define DLOG_VNODE  6  /* vflush/writevnodes */
    614 #  define DLOG_SEG    7  /* segwrite */
    615 #  define DLOG_SU     8  /* seguse accounting */
    616 #  define DLOG_CLEAN  9  /* cleaner routines */
    617 #  define DLOG_MOUNT  10 /* mount/unmount */
    618 #  define DLOG_PAGE   11 /* putpages/gop_write */
    619 #  define DLOG_DIROP  12 /* dirop accounting */
    620 #  define DLOG_MALLOC 13 /* lfs_malloc accounting */
    621 #  define DLOG_MAX    14 /* The terminator */
    622 #  define DLOG(a) lfs_debug_log a
    623 # else /* ! DEBUG */
    624 #  define LFS_BCLEAN_LOG(fs, bp)
    625 #  define LFS_BWRITE_LOG(bp)		VOP_BWRITE((bp)->b_vp, (bp))
    626 #  define DLOG(a)
    627 # endif /* ! DEBUG */
    628 #else /* ! _KERNEL */
    629 # define LFS_BWRITE_LOG(bp)		VOP_BWRITE((bp))
    630 #endif /* _KERNEL */
    631 
    632 #ifdef _KERNEL
    633 /* This overlays the fid structure (see fstypes.h). */
    634 struct ulfs_ufid {
    635 	u_int16_t ufid_len;	/* Length of structure. */
    636 	u_int16_t ufid_pad;	/* Force 32-bit alignment. */
    637 	u_int32_t ufid_ino;	/* File number (ino). */
    638 	int32_t	  ufid_gen;	/* Generation number. */
    639 };
    640 /* Filehandle structure for exported LFSes */
    641 struct lfid {
    642 	struct ulfs_ufid lfid_ufid;
    643 #define lfid_len lfid_ufid.ufid_len
    644 #define lfid_ino lfid_ufid.ufid_ino
    645 #define lfid_gen lfid_ufid.ufid_gen
    646 	uint32_t lfid_ident;
    647 };
    648 #endif /* _KERNEL */
    649 
    650 /*
    651  * "struct inode" associated definitions
    652  */
    653 
    654 /* Address calculations for metadata located in the inode */
    655 #define	S_INDIR(fs)	-ULFS_NDADDR
    656 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
    657 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
    658 
    659 /* For convenience */
    660 #define IN_ALLMOD (IN_MODIFIED|IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY|IN_ACCESSED|IN_CLEANING)
    661 
    662 #define LFS_SET_UINO(ip, flags) do {					\
    663 	if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED))	\
    664 		++(ip)->i_lfs->lfs_uinodes;				\
    665 	if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING))	\
    666 		++(ip)->i_lfs->lfs_uinodes;				\
    667 	if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED))	\
    668 		++(ip)->i_lfs->lfs_uinodes;				\
    669 	(ip)->i_flag |= (flags);					\
    670 } while (0)
    671 
    672 #define LFS_CLR_UINO(ip, flags) do {					\
    673 	if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED))	\
    674 		--(ip)->i_lfs->lfs_uinodes;				\
    675 	if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING))	\
    676 		--(ip)->i_lfs->lfs_uinodes;				\
    677 	if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED))	\
    678 		--(ip)->i_lfs->lfs_uinodes;				\
    679 	(ip)->i_flag &= ~(flags);					\
    680 	if ((ip)->i_lfs->lfs_uinodes < 0) {				\
    681 		panic("lfs_uinodes < 0");				\
    682 	}								\
    683 } while (0)
    684 
    685 #define LFS_ITIMES(ip, acc, mod, cre) \
    686 	while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
    687 		lfs_itimes(ip, acc, mod, cre)
    688 
    689 /*
    690  * "struct vnode" associated definitions
    691  */
    692 
    693 /* Heuristic emptiness measure */
    694 #define VPISEMPTY(vp)	 (LIST_EMPTY(&(vp)->v_dirtyblkhd) && 		\
    695 			  !(vp->v_type == VREG && (vp)->v_iflag & VI_ONWORKLST) &&\
    696 			  VTOI(vp)->i_lfs_nbtree == 0)
    697 
    698 #define WRITEINPROG(vp) ((vp)->v_numoutput > 0 ||			\
    699 	(!LIST_EMPTY(&(vp)->v_dirtyblkhd) &&				\
    700 	 !(VTOI(vp)->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING))))
    701 
    702 
    703 /*
    704  * On-disk and in-memory checkpoint segment usage structure.
    705  */
    706 typedef struct segusage SEGUSE;
    707 struct segusage {
    708 	u_int32_t su_nbytes;		/* 0: number of live bytes */
    709 	u_int32_t su_olastmod;		/* 4: SEGUSE last modified timestamp */
    710 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
    711 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
    712 
    713 #define	SEGUSE_ACTIVE		0x01	/*  segment currently being written */
    714 #define	SEGUSE_DIRTY		0x02	/*  segment has data in it */
    715 #define	SEGUSE_SUPERBLOCK	0x04	/*  segment contains a superblock */
    716 #define SEGUSE_ERROR		0x08	/*  cleaner: do not clean segment */
    717 #define SEGUSE_EMPTY		0x10	/*  segment is empty */
    718 #define SEGUSE_INVAL		0x20	/*  segment is invalid */
    719 	u_int32_t su_flags;		/* 12: segment flags */
    720 	u_int64_t su_lastmod;		/* 16: last modified timestamp */
    721 };
    722 
    723 typedef struct segusage_v1 SEGUSE_V1;
    724 struct segusage_v1 {
    725 	u_int32_t su_nbytes;		/* 0: number of live bytes */
    726 	u_int32_t su_lastmod;		/* 4: SEGUSE last modified timestamp */
    727 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
    728 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
    729 	u_int32_t su_flags;		/* 12: segment flags  */
    730 };
    731 
    732 #define	SEGUPB(fs)	(fs->lfs_sepb)
    733 #define	SEGTABSIZE_SU(fs)						\
    734 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) / (fs)->lfs_sepb)
    735 
    736 #ifdef _KERNEL
    737 # define SHARE_IFLOCK(F) 						\
    738   do {									\
    739 	rw_enter(&(F)->lfs_iflock, RW_READER);				\
    740   } while(0)
    741 # define UNSHARE_IFLOCK(F)						\
    742   do {									\
    743 	rw_exit(&(F)->lfs_iflock);					\
    744   } while(0)
    745 #else /* ! _KERNEL */
    746 # define SHARE_IFLOCK(F)
    747 # define UNSHARE_IFLOCK(F)
    748 #endif /* ! _KERNEL */
    749 
    750 /* Read in the block with a specific segment usage entry from the ifile. */
    751 #define	LFS_SEGENTRY(SP, F, IN, BP) do {				\
    752 	int _e;								\
    753 	SHARE_IFLOCK(F);						\
    754 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    755 	if ((_e = bread((F)->lfs_ivnode,				\
    756 	    ((IN) / (F)->lfs_sepb) + (F)->lfs_cleansz,			\
    757 	    (F)->lfs_bsize, NOCRED, 0, &(BP))) != 0)			\
    758 		panic("lfs: ifile read: %d", _e);			\
    759 	if ((F)->lfs_version == 1)					\
    760 		(SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data +		\
    761 			((IN) & ((F)->lfs_sepb - 1)));			\
    762 	else								\
    763 		(SP) = (SEGUSE *)(BP)->b_data + ((IN) % (F)->lfs_sepb);	\
    764 	UNSHARE_IFLOCK(F);						\
    765 } while (0)
    766 
    767 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do {				\
    768 	if ((SP)->su_nbytes == 0)					\
    769 		(SP)->su_flags |= SEGUSE_EMPTY;				\
    770 	else								\
    771 		(SP)->su_flags &= ~SEGUSE_EMPTY;			\
    772 	(F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags;	\
    773 	LFS_BWRITE_LOG(BP);						\
    774 } while (0)
    775 
    776 /*
    777  * On-disk file information.  One per file with data blocks in the segment.
    778  */
    779 typedef struct finfo FINFO;
    780 struct finfo {
    781 	u_int32_t fi_nblocks;		/* number of blocks */
    782 	u_int32_t fi_version;		/* version number */
    783 	u_int32_t fi_ino;		/* inode number */
    784 	u_int32_t fi_lastlength;	/* length of last block in array */
    785 	int32_t	  fi_blocks[1];		/* array of logical block numbers */
    786 };
    787 /* sizeof FINFO except fi_blocks */
    788 #define	FINFOSIZE	(sizeof(FINFO) - sizeof(int32_t))
    789 
    790 /*
    791  * Index file inode entries.
    792  */
    793 typedef struct ifile IFILE;
    794 struct ifile {
    795 	u_int32_t if_version;		/* inode version number */
    796 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
    797 	int32_t	  if_daddr;		/* inode disk address */
    798 #define LFS_ORPHAN_NEXTFREE	(~(u_int32_t)0) /* indicate orphaned file */
    799 	u_int32_t if_nextfree;		/* next-unallocated inode */
    800 	u_int32_t if_atime_sec;		/* Last access time, seconds */
    801 	u_int32_t if_atime_nsec;	/* and nanoseconds */
    802 };
    803 
    804 typedef struct ifile_v1 IFILE_V1;
    805 struct ifile_v1 {
    806 	u_int32_t if_version;		/* inode version number */
    807 	int32_t	  if_daddr;		/* inode disk address */
    808 	u_int32_t if_nextfree;		/* next-unallocated inode */
    809 #if LFS_ATIME_IFILE
    810 	struct timespec if_atime;	/* Last access time */
    811 #endif
    812 };
    813 
    814 /*
    815  * LFSv1 compatibility code is not allowed to touch if_atime, since it
    816  * may not be mapped!
    817  */
    818 /* Read in the block with a specific inode from the ifile. */
    819 #define	LFS_IENTRY(IP, F, IN, BP) do {					\
    820 	int _e;								\
    821 	SHARE_IFLOCK(F);						\
    822 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    823 	if ((_e = bread((F)->lfs_ivnode,				\
    824 	(IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,	\
    825 	(F)->lfs_bsize, NOCRED, 0, &(BP))) != 0)			\
    826 		panic("lfs: ifile ino %d read %d", (int)(IN), _e);	\
    827 	if ((F)->lfs_version == 1)					\
    828 		(IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data +		\
    829 				 (IN) % (F)->lfs_ifpb);			\
    830 	else								\
    831 		(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;	\
    832 	UNSHARE_IFLOCK(F);						\
    833 } while (0)
    834 
    835 /*
    836  * Cleaner information structure.  This resides in the ifile and is used
    837  * to pass information from the kernel to the cleaner.
    838  */
    839 typedef struct _cleanerinfo {
    840 	u_int32_t clean;		/* number of clean segments */
    841 	u_int32_t dirty;		/* number of dirty segments */
    842 	int32_t   bfree;		/* disk blocks free */
    843 	int32_t	  avail;		/* disk blocks available */
    844 	u_int32_t free_head;		/* head of the inode free list */
    845 	u_int32_t free_tail;		/* tail of the inode free list */
    846 #define LFS_CLEANER_MUST_CLEAN	0x01
    847 	u_int32_t flags;		/* status word from the kernel */
    848 } CLEANERINFO;
    849 
    850 #define	CLEANSIZE_SU(fs)						\
    851 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
    852 
    853 /* Read in the block with the cleaner info from the ifile. */
    854 #define LFS_CLEANERINFO(CP, F, BP) do {					\
    855 	SHARE_IFLOCK(F);						\
    856 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    857 	if (bread((F)->lfs_ivnode,					\
    858 	    (daddr_t)0, (F)->lfs_bsize, NOCRED, 0, &(BP)))		\
    859 		panic("lfs: ifile read");				\
    860 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    861 	UNSHARE_IFLOCK(F);						\
    862 } while (0)
    863 
    864 /*
    865  * Synchronize the Ifile cleaner info with current avail and bfree.
    866  */
    867 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do {		 	\
    868     mutex_enter(&lfs_lock);						\
    869     if ((w) || (cip)->bfree != (fs)->lfs_bfree ||		 	\
    870 	(cip)->avail != (fs)->lfs_avail - (fs)->lfs_ravail - 		\
    871 	(fs)->lfs_favail) {	 					\
    872 	(cip)->bfree = (fs)->lfs_bfree;				 	\
    873 	(cip)->avail = (fs)->lfs_avail - (fs)->lfs_ravail -		\
    874 		(fs)->lfs_favail;				 	\
    875 	if (((bp)->b_flags & B_GATHERED) == 0) {		 	\
    876 		(fs)->lfs_flags |= LFS_IFDIRTY;			 	\
    877 	}								\
    878 	mutex_exit(&lfs_lock);						\
    879 	(void) LFS_BWRITE_LOG(bp); /* Ifile */			 	\
    880     } else {							 	\
    881 	mutex_exit(&lfs_lock);						\
    882 	brelse(bp, 0);						 	\
    883     }									\
    884 } while (0)
    885 
    886 /*
    887  * Get the head of the inode free list.
    888  * Always called with the segment lock held.
    889  */
    890 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do {			\
    891 	if ((FS)->lfs_version > 1) {					\
    892 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    893 		(FS)->lfs_freehd = (CIP)->free_head;			\
    894 		brelse(BP, 0);						\
    895 	}								\
    896 	*(FREEP) = (FS)->lfs_freehd;					\
    897 } while (0)
    898 
    899 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do {				\
    900 	(FS)->lfs_freehd = (VAL);					\
    901 	if ((FS)->lfs_version > 1) {					\
    902 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    903 		(CIP)->free_head = (VAL);				\
    904 		LFS_BWRITE_LOG(BP);					\
    905 		mutex_enter(&lfs_lock);					\
    906 		(FS)->lfs_flags |= LFS_IFDIRTY;				\
    907 		mutex_exit(&lfs_lock);					\
    908 	}								\
    909 } while (0)
    910 
    911 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do {			\
    912 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    913 	*(FREEP) = (CIP)->free_tail;					\
    914 	brelse(BP, 0);							\
    915 } while (0)
    916 
    917 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do {				\
    918 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    919 	(CIP)->free_tail = (VAL);					\
    920 	LFS_BWRITE_LOG(BP);						\
    921 	mutex_enter(&lfs_lock);						\
    922 	(FS)->lfs_flags |= LFS_IFDIRTY;					\
    923 	mutex_exit(&lfs_lock);						\
    924 } while (0)
    925 
    926 /*
    927  * On-disk segment summary information
    928  */
    929 typedef struct segsum_v1 SEGSUM_V1;
    930 struct segsum_v1 {
    931 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
    932 	u_int32_t ss_datasum;		/* 4: check sum of data */
    933 	u_int32_t ss_magic;		/* 8: segment summary magic number */
    934 #define SS_MAGIC	0x061561
    935 	int32_t	  ss_next;		/* 12: next segment */
    936 	u_int32_t ss_create;		/* 16: creation time stamp */
    937 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
    938 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
    939 
    940 #define	SS_DIROP	0x01		/* segment begins a dirop */
    941 #define	SS_CONT		0x02		/* more partials to finish this write*/
    942 #define	SS_CLEAN	0x04		/* written by the cleaner */
    943 #define	SS_RFW		0x08		/* written by the roll-forward agent */
    944 #define	SS_RECLAIM	0x10		/* written by the roll-forward agent */
    945 	u_int16_t ss_flags;		/* 24: used for directory operations */
    946 	u_int16_t ss_pad;		/* 26: extra space */
    947 	/* FINFO's and inode daddr's... */
    948 };
    949 
    950 typedef struct segsum SEGSUM;
    951 struct segsum {
    952 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
    953 	u_int32_t ss_datasum;		/* 4: check sum of data */
    954 	u_int32_t ss_magic;		/* 8: segment summary magic number */
    955 	int32_t	  ss_next;		/* 12: next segment */
    956 	u_int32_t ss_ident;		/* 16: roll-forward fsid */
    957 #define ss_ocreate ss_ident /* ident is where create was in v1 */
    958 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
    959 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
    960 	u_int16_t ss_flags;		/* 24: used for directory operations */
    961 	u_int8_t  ss_pad[2];		/* 26: extra space */
    962 	u_int32_t ss_reclino;           /* 28: inode being reclaimed */
    963 	u_int64_t ss_serial;		/* 32: serial number */
    964 	u_int64_t ss_create;		/* 40: time stamp */
    965 	/* FINFO's and inode daddr's... */
    966 };
    967 
    968 #define SEGSUM_SIZE(fs) ((fs)->lfs_version == 1 ? sizeof(SEGSUM_V1) : sizeof(SEGSUM))
    969 
    970 
    971 /*
    972  * On-disk super block.
    973  */
    974 struct dlfs {
    975 #define	       LFS_MAGIC       0x070162
    976 	u_int32_t dlfs_magic;	  /* 0: magic number */
    977 #define	       LFS_VERSION     2
    978 	u_int32_t dlfs_version;	  /* 4: version number */
    979 
    980 	u_int32_t dlfs_size;	  /* 8: number of blocks in fs (v1) */
    981 				  /*	number of frags in fs (v2) */
    982 	u_int32_t dlfs_ssize;	  /* 12: number of blocks per segment (v1) */
    983 				  /*	 number of bytes per segment (v2) */
    984 	u_int32_t dlfs_dsize;	  /* 16: number of disk blocks in fs */
    985 	u_int32_t dlfs_bsize;	  /* 20: file system block size */
    986 	u_int32_t dlfs_fsize;	  /* 24: size of frag blocks in fs */
    987 	u_int32_t dlfs_frag;	  /* 28: number of frags in a block in fs */
    988 
    989 /* Checkpoint region. */
    990 	u_int32_t dlfs_freehd;	  /* 32: start of the free list */
    991 	int32_t   dlfs_bfree;	  /* 36: number of free disk blocks */
    992 	u_int32_t dlfs_nfiles;	  /* 40: number of allocated inodes */
    993 	int32_t	  dlfs_avail;	  /* 44: blocks available for writing */
    994 	int32_t	  dlfs_uinodes;	  /* 48: inodes in cache not yet on disk */
    995 	int32_t	  dlfs_idaddr;	  /* 52: inode file disk address */
    996 	u_int32_t dlfs_ifile;	  /* 56: inode file inode number */
    997 	int32_t	  dlfs_lastseg;	  /* 60: address of last segment written */
    998 	int32_t	  dlfs_nextseg;	  /* 64: address of next segment to write */
    999 	int32_t	  dlfs_curseg;	  /* 68: current segment being written */
   1000 	int32_t	  dlfs_offset;	  /* 72: offset in curseg for next partial */
   1001 	int32_t	  dlfs_lastpseg;  /* 76: address of last partial written */
   1002 	u_int32_t dlfs_inopf;	  /* 80: v1: time stamp; v2: inodes per frag */
   1003 #define dlfs_otstamp dlfs_inopf
   1004 
   1005 /* These are configuration parameters. */
   1006 	u_int32_t dlfs_minfree;	  /* 84: minimum percentage of free blocks */
   1007 
   1008 /* These fields can be computed from the others. */
   1009 	u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
   1010 	u_int32_t dlfs_fsbpseg;	    /* 96: fsb per segment */
   1011 	u_int32_t dlfs_inopb;	  /* 100: inodes per block */
   1012 	u_int32_t dlfs_ifpb;	  /* 104: IFILE entries per block */
   1013 	u_int32_t dlfs_sepb;	  /* 108: SEGUSE entries per block */
   1014 	u_int32_t dlfs_nindir;	  /* 112: indirect pointers per block */
   1015 	u_int32_t dlfs_nseg;	  /* 116: number of segments */
   1016 	u_int32_t dlfs_nspf;	  /* 120: number of sectors per fragment */
   1017 	u_int32_t dlfs_cleansz;	  /* 124: cleaner info size in blocks */
   1018 	u_int32_t dlfs_segtabsz;  /* 128: segment table size in blocks */
   1019 	u_int32_t dlfs_segmask;	  /* 132: calculate offset within a segment */
   1020 	u_int32_t dlfs_segshift;  /* 136: fast mult/div for segments */
   1021 	u_int32_t dlfs_bshift;	  /* 140: calc block number from file offset */
   1022 	u_int32_t dlfs_ffshift;	  /* 144: fast mult/div for frag from file */
   1023 	u_int32_t dlfs_fbshift;	  /* 148: fast mult/div for frag from block */
   1024 	u_int64_t dlfs_bmask;	  /* 152: calc block offset from file offset */
   1025 	u_int64_t dlfs_ffmask;	  /* 160: calc frag offset from file offset */
   1026 	u_int64_t dlfs_fbmask;	  /* 168: calc frag offset from block offset */
   1027 	u_int32_t dlfs_blktodb;	  /* 176: blktodb and dbtoblk shift constant */
   1028 	u_int32_t dlfs_sushift;	  /* 180: fast mult/div for segusage table */
   1029 
   1030 	int32_t	  dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
   1031 #define LFS_MIN_SBINTERVAL     5  /* minimum superblock segment spacing */
   1032 #define LFS_MAXNUMSB	       10 /* 188: superblock disk offsets */
   1033 	int32_t	   dlfs_sboffs[LFS_MAXNUMSB];
   1034 
   1035 	u_int32_t dlfs_nclean;	  /* 228: Number of clean segments */
   1036 	u_char	  dlfs_fsmnt[MNAMELEN];	 /* 232: name mounted on */
   1037 #define LFS_PF_CLEAN 0x1
   1038 	u_int16_t dlfs_pflags;	  /* 322: file system persistent flags */
   1039 	int32_t	  dlfs_dmeta;	  /* 324: total number of dirty summaries */
   1040 	u_int32_t dlfs_minfreeseg; /* 328: segments not counted in bfree */
   1041 	u_int32_t dlfs_sumsize;	  /* 332: size of summary blocks */
   1042 	u_int64_t dlfs_serial;	  /* 336: serial number */
   1043 	u_int32_t dlfs_ibsize;	  /* 344: size of inode blocks */
   1044 	int32_t	  dlfs_start;	  /* 348: start of segment 0 */
   1045 	u_int64_t dlfs_tstamp;	  /* 352: time stamp */
   1046 #define LFS_44INODEFMT 0
   1047 #define LFS_MAXINODEFMT 0
   1048 	u_int32_t dlfs_inodefmt;  /* 360: inode format version */
   1049 	u_int32_t dlfs_interleave; /* 364: segment interleave */
   1050 	u_int32_t dlfs_ident;	  /* 368: per-fs identifier */
   1051 	u_int32_t dlfs_fsbtodb;	  /* 372: fsbtodb abd dbtodsb shift constant */
   1052 	u_int32_t dlfs_resvseg;   /* 376: segments reserved for the cleaner */
   1053 	int8_t	  dlfs_pad[128];  /* 380: round to 512 bytes */
   1054 /* Checksum -- last valid disk field. */
   1055 	u_int32_t dlfs_cksum;	  /* 508: checksum for superblock checking */
   1056 };
   1057 
   1058 /* Type used for the inode bitmap */
   1059 typedef u_int32_t lfs_bm_t;
   1060 
   1061 /*
   1062  * Linked list of segments whose byte count needs updating following a
   1063  * file truncation.
   1064  */
   1065 struct segdelta {
   1066 	long segnum;
   1067 	size_t num;
   1068 	LIST_ENTRY(segdelta) list;
   1069 };
   1070 
   1071 /*
   1072  * In-memory super block.
   1073  */
   1074 struct lfs {
   1075 	struct dlfs lfs_dlfs;		/* on-disk parameters */
   1076 #define lfs_magic lfs_dlfs.dlfs_magic
   1077 #define lfs_version lfs_dlfs.dlfs_version
   1078 #define lfs_size lfs_dlfs.dlfs_size
   1079 #define lfs_ssize lfs_dlfs.dlfs_ssize
   1080 #define lfs_dsize lfs_dlfs.dlfs_dsize
   1081 #define lfs_bsize lfs_dlfs.dlfs_bsize
   1082 #define lfs_fsize lfs_dlfs.dlfs_fsize
   1083 #define lfs_frag lfs_dlfs.dlfs_frag
   1084 #define lfs_freehd lfs_dlfs.dlfs_freehd
   1085 #define lfs_bfree lfs_dlfs.dlfs_bfree
   1086 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
   1087 #define lfs_avail lfs_dlfs.dlfs_avail
   1088 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
   1089 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
   1090 #define lfs_ifile lfs_dlfs.dlfs_ifile
   1091 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
   1092 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
   1093 #define lfs_curseg lfs_dlfs.dlfs_curseg
   1094 #define lfs_offset lfs_dlfs.dlfs_offset
   1095 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
   1096 #define lfs_otstamp lfs_dlfs.dlfs_inopf
   1097 #define lfs_inopf lfs_dlfs.dlfs_inopf
   1098 #define lfs_minfree lfs_dlfs.dlfs_minfree
   1099 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
   1100 #define lfs_fsbpseg lfs_dlfs.dlfs_fsbpseg
   1101 #define lfs_inopb lfs_dlfs.dlfs_inopb
   1102 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
   1103 #define lfs_sepb lfs_dlfs.dlfs_sepb
   1104 #define lfs_nindir lfs_dlfs.dlfs_nindir
   1105 #define lfs_nseg lfs_dlfs.dlfs_nseg
   1106 #define lfs_nspf lfs_dlfs.dlfs_nspf
   1107 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
   1108 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
   1109 #define lfs_segmask lfs_dlfs.dlfs_segmask
   1110 #define lfs_segshift lfs_dlfs.dlfs_segshift
   1111 #define lfs_bmask lfs_dlfs.dlfs_bmask
   1112 #define lfs_bshift lfs_dlfs.dlfs_bshift
   1113 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
   1114 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
   1115 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
   1116 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
   1117 #define lfs_blktodb lfs_dlfs.dlfs_blktodb
   1118 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
   1119 #define lfs_sushift lfs_dlfs.dlfs_sushift
   1120 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
   1121 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
   1122 #define lfs_cksum lfs_dlfs.dlfs_cksum
   1123 #define lfs_pflags lfs_dlfs.dlfs_pflags
   1124 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
   1125 #define lfs_nclean lfs_dlfs.dlfs_nclean
   1126 #define lfs_dmeta lfs_dlfs.dlfs_dmeta
   1127 #define lfs_minfreeseg lfs_dlfs.dlfs_minfreeseg
   1128 #define lfs_sumsize lfs_dlfs.dlfs_sumsize
   1129 #define lfs_serial lfs_dlfs.dlfs_serial
   1130 #define lfs_ibsize lfs_dlfs.dlfs_ibsize
   1131 #define lfs_start lfs_dlfs.dlfs_start
   1132 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
   1133 #define lfs_inodefmt lfs_dlfs.dlfs_inodefmt
   1134 #define lfs_interleave lfs_dlfs.dlfs_interleave
   1135 #define lfs_ident lfs_dlfs.dlfs_ident
   1136 #define lfs_resvseg lfs_dlfs.dlfs_resvseg
   1137 
   1138 /* These fields are set at mount time and are meaningless on disk. */
   1139 	struct segment *lfs_sp;		/* current segment being written */
   1140 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
   1141 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
   1142 	pid_t	  lfs_lockpid;		/* pid of lock holder */
   1143 	lwpid_t	  lfs_locklwp;		/* lwp of lock holder */
   1144 	u_int32_t lfs_iocount;		/* number of ios pending */
   1145 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
   1146 	u_int32_t lfs_dirops;		/* count of active directory ops */
   1147 	u_int32_t lfs_dirvcount;	/* count of VDIROP nodes in this fs */
   1148 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
   1149 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
   1150 	int8_t	  lfs_fmod;		/* super block modified flag */
   1151 	int8_t	  lfs_ronly;		/* mounted read-only flag */
   1152 #define LFS_NOTYET  0x01
   1153 #define LFS_IFDIRTY 0x02
   1154 #define LFS_WARNED  0x04
   1155 #define LFS_UNDIROP 0x08
   1156 	int8_t	  lfs_flags;		/* currently unused flag */
   1157 	u_int16_t lfs_activesb;		/* toggle between superblocks */
   1158 	daddr_t	  lfs_sbactive;		/* disk address of current sb write */
   1159 	struct vnode *lfs_flushvp;	/* vnode being flushed */
   1160 	int lfs_flushvp_fakevref;	/* fake vref count for flushvp */
   1161 	struct vnode *lfs_unlockvp;	/* being inactivated in lfs_segunlock */
   1162 	u_int32_t lfs_diropwait;	/* # procs waiting on dirop flush */
   1163 	size_t lfs_devbsize;		/* Device block size */
   1164 	size_t lfs_devbshift;		/* Device block shift */
   1165 	krwlock_t lfs_fraglock;
   1166 	krwlock_t lfs_iflock;		/* Ifile lock */
   1167 	kcondvar_t lfs_stopcv;		/* Wrap lock */
   1168 	struct lwp *lfs_stoplwp;
   1169 	pid_t lfs_rfpid;		/* Process ID of roll-forward agent */
   1170 	int	  lfs_nadirop;		/* number of active dirop nodes */
   1171 	long	  lfs_ravail;		/* blocks pre-reserved for writing */
   1172 	long	  lfs_favail;		/* blocks pre-reserved for writing */
   1173 	res_t *lfs_resblk;		/* Reserved memory for pageout */
   1174 	TAILQ_HEAD(, inode) lfs_dchainhd; /* dirop vnodes */
   1175 	TAILQ_HEAD(, inode) lfs_pchainhd; /* paging vnodes */
   1176 #define LFS_RESHASH_WIDTH 17
   1177 	LIST_HEAD(, lfs_res_blk) lfs_reshash[LFS_RESHASH_WIDTH];
   1178 	int	  lfs_pdflush;		 /* pagedaemon wants us to flush */
   1179 	u_int32_t **lfs_suflags;	/* Segment use flags */
   1180 #ifdef _KERNEL
   1181 	struct pool lfs_clpool;		/* Pool for struct lfs_cluster */
   1182 	struct pool lfs_bpppool;	/* Pool for bpp */
   1183 	struct pool lfs_segpool;	/* Pool for struct segment */
   1184 #endif /* _KERNEL */
   1185 #define LFS_MAX_CLEANIND 64
   1186 	int32_t  lfs_cleanint[LFS_MAX_CLEANIND]; /* Active cleaning intervals */
   1187 	int 	 lfs_cleanind;		/* Index into intervals */
   1188 	int lfs_sleepers;		/* # procs sleeping this fs */
   1189 	int lfs_pages;			/* dirty pages blaming this fs */
   1190 	lfs_bm_t *lfs_ino_bitmap;	/* Inuse inodes bitmap */
   1191 	int lfs_nowrap;			/* Suspend log wrap */
   1192 	int lfs_wrappass;		/* Allow first log wrap requester to pass */
   1193 	int lfs_wrapstatus;		/* Wrap status */
   1194 	int lfs_reclino;		/* Inode being reclaimed */
   1195 	int lfs_startseg;               /* Segment we started writing at */
   1196 	LIST_HEAD(, segdelta) lfs_segdhd;	/* List of pending trunc accounting events */
   1197 };
   1198 
   1199 /* NINDIR is the number of indirects in a file system block. */
   1200 #define	NINDIR(fs)	((fs)->lfs_nindir)
   1201 
   1202 /* INOPB is the number of inodes in a secondary storage block. */
   1203 #define	INOPB(fs)	((fs)->lfs_inopb)
   1204 /* INOPF is the number of inodes in a fragment. */
   1205 #define INOPF(fs)	((fs)->lfs_inopf)
   1206 
   1207 #define	blksize(fs, ip, lbn) \
   1208 	(((lbn) >= ULFS_NDADDR || (ip)->i_ffs1_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
   1209 	    ? (fs)->lfs_bsize \
   1210 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs1_size))))
   1211 #define	blkoff(fs, loc)		((int)((loc) & (fs)->lfs_bmask))
   1212 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
   1213     ((int)((loc) & (fs)->lfs_ffmask))
   1214 
   1215 #if defined (_KERNEL)
   1216 #define	fsbtodb(fs, b)		((b) << ((fs)->lfs_ffshift - DEV_BSHIFT))
   1217 #define	dbtofsb(fs, b)		((b) >> ((fs)->lfs_ffshift - DEV_BSHIFT))
   1218 #else
   1219 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
   1220 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
   1221 #endif
   1222 
   1223 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
   1224 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
   1225 
   1226 #define fsbtob(fs, b)		((b) << (fs)->lfs_ffshift)
   1227 #define btofsb(fs, b)		((b) >> (fs)->lfs_ffshift)
   1228 
   1229 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
   1230 	((loc) >> (fs)->lfs_ffshift)
   1231 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
   1232 	((off_t)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
   1233 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
   1234 	((off_t)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
   1235 #define fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
   1236 	((frags) >> (fs)->lfs_fbshift)
   1237 #define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
   1238 	((blks) << (fs)->lfs_fbshift)
   1239 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */	\
   1240 	((fsb) & ((fs)->lfs_frag - 1))
   1241 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
   1242 	((fsb) &~ ((fs)->lfs_frag - 1))
   1243 #define dblksize(fs, dp, lbn) \
   1244 	(((lbn) >= ULFS_NDADDR || (dp)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
   1245 	    ? (fs)->lfs_bsize \
   1246 	    : (fragroundup(fs, blkoff(fs, (dp)->di_size))))
   1247 
   1248 #define	segsize(fs)	((fs)->lfs_version == 1 ?	     		\
   1249 			   lblktosize((fs), (fs)->lfs_ssize) :		\
   1250 			   (fs)->lfs_ssize)
   1251 #define segtod(fs, seg) (((fs)->lfs_version == 1     ?	     		\
   1252 			   (fs)->lfs_ssize << (fs)->lfs_blktodb :	\
   1253 			   btofsb((fs), (fs)->lfs_ssize)) * (seg))
   1254 #define	dtosn(fs, daddr)	/* block address to segment number */	\
   1255 	((uint32_t)(((daddr) - (fs)->lfs_start) / segtod((fs), 1)))
   1256 #define sntod(fs, sn)		/* segment number to disk address */	\
   1257 	((daddr_t)(segtod((fs), (sn)) + (fs)->lfs_start))
   1258 
   1259 /*
   1260  * Structures used by lfs_bmapv and lfs_markv to communicate information
   1261  * about inodes and data blocks.
   1262  */
   1263 typedef struct block_info {
   1264 	u_int32_t bi_inode;		/* inode # */
   1265 	int32_t	bi_lbn;			/* logical block w/in file */
   1266 	int32_t	bi_daddr;		/* disk address of block */
   1267 	u_int64_t bi_segcreate;		/* origin segment create time */
   1268 	int	bi_version;		/* file version number */
   1269 	void	*bi_bp;			/* data buffer */
   1270 	int	bi_size;		/* size of the block (if fragment) */
   1271 } BLOCK_INFO;
   1272 
   1273 /* Compatibility for 1.5 binaries */
   1274 typedef struct block_info_15 {
   1275 	u_int32_t bi_inode;		/* inode # */
   1276 	int32_t	bi_lbn;			/* logical block w/in file */
   1277 	int32_t	bi_daddr;		/* disk address of block */
   1278 	u_int32_t bi_segcreate;		/* origin segment create time */
   1279 	int	bi_version;		/* file version number */
   1280 	void	*bi_bp;			/* data buffer */
   1281 	int	bi_size;		/* size of the block (if fragment) */
   1282 } BLOCK_INFO_15;
   1283 
   1284 /* In-memory description of a segment about to be written. */
   1285 struct segment {
   1286 	struct lfs	 *fs;		/* file system pointer */
   1287 	struct buf	**bpp;		/* pointer to buffer array */
   1288 	struct buf	**cbpp;		/* pointer to next available bp */
   1289 	struct buf	**start_bpp;	/* pointer to first bp in this set */
   1290 	struct buf	 *ibp;		/* buffer pointer to inode page */
   1291 	struct ulfs1_dinode    *idp;          /* pointer to ifile dinode */
   1292 	struct finfo	 *fip;		/* current fileinfo pointer */
   1293 	struct vnode	 *vp;		/* vnode being gathered */
   1294 	void	 *segsum;		/* segment summary info */
   1295 	u_int32_t ninodes;		/* number of inodes in this segment */
   1296 	int32_t seg_bytes_left;		/* bytes left in segment */
   1297 	int32_t sum_bytes_left;		/* bytes left in summary block */
   1298 	u_int32_t seg_number;		/* number of this segment */
   1299 	int32_t *start_lbp;		/* beginning lbn for this set */
   1300 
   1301 #define SEGM_CKP	0x0001		/* doing a checkpoint */
   1302 #define SEGM_CLEAN	0x0002		/* cleaner call; don't sort */
   1303 #define SEGM_SYNC	0x0004		/* wait for segment */
   1304 #define SEGM_PROT	0x0008		/* don't inactivate at segunlock */
   1305 #define SEGM_PAGEDAEMON	0x0010		/* pagedaemon called us */
   1306 #define SEGM_WRITERD	0x0020		/* LFS writed called us */
   1307 #define SEGM_FORCE_CKP	0x0040		/* Force checkpoint right away */
   1308 #define SEGM_RECLAIM	0x0080		/* Writing to reclaim vnode */
   1309 #define SEGM_SINGLE	0x0100		/* Opportunistic writevnodes */
   1310 	u_int16_t seg_flags;		/* run-time flags for this segment */
   1311 	u_int32_t seg_iocount;		/* number of ios pending */
   1312 	int	  ndupino;		/* number of duplicate inodes */
   1313 };
   1314 
   1315 #ifdef _KERNEL
   1316 struct lfs_cluster {
   1317 	size_t bufsize;	       /* Size of kept data */
   1318 	struct buf **bpp;      /* Array of kept buffers */
   1319 	int bufcount;	       /* Number of kept buffers */
   1320 #define LFS_CL_MALLOC	0x00000001
   1321 #define LFS_CL_SHIFT	0x00000002
   1322 #define LFS_CL_SYNC	0x00000004
   1323 	u_int32_t flags;       /* Flags */
   1324 	struct lfs *fs;	       /* LFS that this belongs to */
   1325 	struct segment *seg;   /* Segment structure, for LFS_CL_SYNC */
   1326 };
   1327 
   1328 /*
   1329  * Splay tree containing block numbers allocated through lfs_balloc.
   1330  */
   1331 struct lbnentry {
   1332 	SPLAY_ENTRY(lbnentry) entry;
   1333 	daddr_t lbn;
   1334 };
   1335 #endif /* _KERNEL */
   1336 
   1337 /*
   1338  * LFS inode extensions.
   1339  */
   1340 struct lfs_inode_ext {
   1341 	off_t	  lfs_osize;		/* size of file on disk */
   1342 	u_int32_t lfs_effnblocks;  /* number of blocks when i/o completes */
   1343 	size_t	  lfs_fragsize[ULFS_NDADDR]; /* size of on-disk direct blocks */
   1344 	TAILQ_ENTRY(inode) lfs_dchain;  /* Dirop chain. */
   1345 	TAILQ_ENTRY(inode) lfs_pchain;  /* Paging chain. */
   1346 #define LFSI_NO_GOP_WRITE 0x01
   1347 #define LFSI_DELETED      0x02
   1348 #define LFSI_WRAPBLOCK    0x04
   1349 #define LFSI_WRAPWAIT     0x08
   1350 #define LFSI_BMAP         0x10
   1351 	u_int32_t lfs_iflags;           /* Inode flags */
   1352 	daddr_t   lfs_hiblk;		/* Highest lbn held by inode */
   1353 #ifdef _KERNEL
   1354 	SPLAY_HEAD(lfs_splay, lbnentry) lfs_lbtree; /* Tree of balloc'd lbns */
   1355 	int	  lfs_nbtree;		/* Size of tree */
   1356 	LIST_HEAD(, segdelta) lfs_segdhd;
   1357 #endif
   1358 	int16_t	  lfs_odnlink;		/* on-disk nlink count for cleaner */
   1359 };
   1360 #define i_lfs_osize		inode_ext.lfs->lfs_osize
   1361 #define i_lfs_effnblks		inode_ext.lfs->lfs_effnblocks
   1362 #define i_lfs_fragsize		inode_ext.lfs->lfs_fragsize
   1363 #define i_lfs_dchain		inode_ext.lfs->lfs_dchain
   1364 #define i_lfs_pchain		inode_ext.lfs->lfs_pchain
   1365 #define i_lfs_iflags		inode_ext.lfs->lfs_iflags
   1366 #define i_lfs_hiblk		inode_ext.lfs->lfs_hiblk
   1367 #define i_lfs_lbtree		inode_ext.lfs->lfs_lbtree
   1368 #define i_lfs_nbtree		inode_ext.lfs->lfs_nbtree
   1369 #define i_lfs_segdhd		inode_ext.lfs->lfs_segdhd
   1370 #define i_lfs_odnlink		inode_ext.lfs->lfs_odnlink
   1371 
   1372 /*
   1373  * Macros for determining free space on the disk, with the variable metadata
   1374  * of segment summaries and inode blocks taken into account.
   1375  */
   1376 /*
   1377  * Estimate number of clean blocks not available for writing because
   1378  * they will contain metadata or overhead.  This is calculated as
   1379  *
   1380  *		E = ((C * M / D) * D + (0) * (T - D)) / T
   1381  * or more simply
   1382  *		E = (C * M) / T
   1383  *
   1384  * where
   1385  * C is the clean space,
   1386  * D is the dirty space,
   1387  * M is the dirty metadata, and
   1388  * T = C + D is the total space on disk.
   1389  *
   1390  * This approximates the old formula of E = C * M / D when D is close to T,
   1391  * but avoids falsely reporting "disk full" when the sample size (D) is small.
   1392  */
   1393 #define LFS_EST_CMETA(F) (int32_t)((					\
   1394 	((F)->lfs_dmeta * (int64_t)(F)->lfs_nclean) / 			\
   1395 	((F)->lfs_nseg)))
   1396 
   1397 /* Estimate total size of the disk not including metadata */
   1398 #define LFS_EST_NONMETA(F) ((F)->lfs_dsize - (F)->lfs_dmeta - LFS_EST_CMETA(F))
   1399 
   1400 /* Estimate number of blocks actually available for writing */
   1401 #define LFS_EST_BFREE(F) ((F)->lfs_bfree > LFS_EST_CMETA(F) ?		     \
   1402 			  (F)->lfs_bfree - LFS_EST_CMETA(F) : 0)
   1403 
   1404 /* Amount of non-meta space not available to mortal man */
   1405 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) *			     \
   1406 				   (u_int64_t)(F)->lfs_minfree) /	     \
   1407 				  100)
   1408 
   1409 /* Can credential C write BB blocks */
   1410 #define ISSPACE(F, BB, C)						\
   1411 	((((C) == NOCRED || kauth_cred_geteuid(C) == 0) &&		\
   1412 	  LFS_EST_BFREE(F) >= (BB)) ||					\
   1413 	 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
   1414 
   1415 /* Can an ordinary user write BB blocks */
   1416 #define IS_FREESPACE(F, BB)						\
   1417 	  (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
   1418 
   1419 /*
   1420  * The minimum number of blocks to create a new inode.  This is:
   1421  * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
   1422  * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
   1423  */
   1424 #define LFS_NRESERVE(F) (btofsb((F), (2 * ULFS_NIADDR + 3) << (F)->lfs_bshift))
   1425 
   1426 /* Statistics Counters */
   1427 struct lfs_stats {	/* Must match sysctl list in lfs_vfsops.h ! */
   1428 	u_int	segsused;
   1429 	u_int	psegwrites;
   1430 	u_int	psyncwrites;
   1431 	u_int	pcleanwrites;
   1432 	u_int	blocktot;
   1433 	u_int	cleanblocks;
   1434 	u_int	ncheckpoints;
   1435 	u_int	nwrites;
   1436 	u_int	nsync_writes;
   1437 	u_int	wait_exceeded;
   1438 	u_int	write_exceeded;
   1439 	u_int	flush_invoked;
   1440 	u_int	vflush_invoked;
   1441 	u_int	clean_inlocked;
   1442 	u_int	clean_vnlocked;
   1443 	u_int   segs_reclaimed;
   1444 };
   1445 #ifdef _KERNEL
   1446 extern struct lfs_stats lfs_stats;
   1447 #endif
   1448 
   1449 /* Fcntls to take the place of the lfs syscalls */
   1450 struct lfs_fcntl_markv {
   1451 	BLOCK_INFO *blkiov;	/* blocks to relocate */
   1452 	int blkcnt;		/* number of blocks */
   1453 };
   1454 
   1455 #define LFCNSEGWAITALL	_FCNR_FSPRIV('L', 14, struct timeval)
   1456 #define LFCNSEGWAIT	_FCNR_FSPRIV('L', 15, struct timeval)
   1457 #define LFCNBMAPV	_FCNRW_FSPRIV('L', 2, struct lfs_fcntl_markv)
   1458 #define LFCNMARKV	_FCNRW_FSPRIV('L', 3, struct lfs_fcntl_markv)
   1459 #define LFCNRECLAIM	 _FCNO_FSPRIV('L', 4)
   1460 
   1461 struct lfs_fhandle {
   1462 	char space[28];	/* FHANDLE_SIZE_COMPAT (but used from userland too) */
   1463 };
   1464 #define LFCNREWIND       _FCNR_FSPRIV('L', 6, int)
   1465 #define LFCNINVAL        _FCNR_FSPRIV('L', 7, int)
   1466 #define LFCNRESIZE       _FCNR_FSPRIV('L', 8, int)
   1467 #define LFCNWRAPSTOP	 _FCNR_FSPRIV('L', 9, int)
   1468 #define LFCNWRAPGO	 _FCNR_FSPRIV('L', 10, int)
   1469 #define LFCNIFILEFH	 _FCNW_FSPRIV('L', 11, struct lfs_fhandle)
   1470 #define LFCNWRAPPASS	 _FCNR_FSPRIV('L', 12, int)
   1471 # define LFS_WRAP_GOING   0x0
   1472 # define LFS_WRAP_WAITING 0x1
   1473 #define LFCNWRAPSTATUS	 _FCNW_FSPRIV('L', 13, int)
   1474 
   1475 /*
   1476  * Compat.  Defined for kernel only.  Userland always uses
   1477  * "the one true version".
   1478  */
   1479 #ifdef _KERNEL
   1480 #include <compat/sys/time_types.h>
   1481 
   1482 #define LFCNSEGWAITALL_COMPAT	 _FCNW_FSPRIV('L', 0, struct timeval50)
   1483 #define LFCNSEGWAIT_COMPAT	 _FCNW_FSPRIV('L', 1, struct timeval50)
   1484 #define LFCNIFILEFH_COMPAT	 _FCNW_FSPRIV('L', 5, struct lfs_fhandle)
   1485 #define LFCNIFILEFH_COMPAT2	 _FCN_FSPRIV(F_FSOUT, 'L', 11, 32)
   1486 #define LFCNWRAPSTOP_COMPAT	 _FCNO_FSPRIV('L', 9)
   1487 #define LFCNWRAPGO_COMPAT	 _FCNO_FSPRIV('L', 10)
   1488 #define LFCNSEGWAITALL_COMPAT_50 _FCNR_FSPRIV('L', 0, struct timeval50)
   1489 #define LFCNSEGWAIT_COMPAT_50	 _FCNR_FSPRIV('L', 1, struct timeval50)
   1490 #endif
   1491 
   1492 #ifdef _KERNEL
   1493 /* XXX MP */
   1494 #define	LFS_SEGLOCK_HELD(fs) \
   1495 	((fs)->lfs_seglock != 0 &&					\
   1496 	 (fs)->lfs_lockpid == curproc->p_pid &&				\
   1497 	 (fs)->lfs_locklwp == curlwp->l_lid)
   1498 #endif /* _KERNEL */
   1499 
   1500 /* Debug segment lock */
   1501 #ifdef notyet
   1502 # define ASSERT_SEGLOCK(fs) KASSERT(LFS_SEGLOCK_HELD(fs))
   1503 # define ASSERT_NO_SEGLOCK(fs) KASSERT(!LFS_SEGLOCK_HELD(fs))
   1504 # define ASSERT_DUNNO_SEGLOCK(fs)
   1505 # define ASSERT_MAYBE_SEGLOCK(fs)
   1506 #else /* !notyet */
   1507 # define ASSERT_DUNNO_SEGLOCK(fs) \
   1508 	DLOG((DLOG_SEG, "lfs func %s seglock wrong (%d)\n", __func__, \
   1509 		LFS_SEGLOCK_HELD(fs)))
   1510 # define ASSERT_SEGLOCK(fs) do {					\
   1511 	if (!LFS_SEGLOCK_HELD(fs)) {					\
   1512 		DLOG((DLOG_SEG, "lfs func %s seglock wrong (0)\n", __func__)); \
   1513 	}								\
   1514 } while(0)
   1515 # define ASSERT_NO_SEGLOCK(fs) do {					\
   1516 	if (LFS_SEGLOCK_HELD(fs)) {					\
   1517 		DLOG((DLOG_SEG, "lfs func %s seglock wrong (1)\n", __func__)); \
   1518 	}								\
   1519 } while(0)
   1520 # define ASSERT_MAYBE_SEGLOCK(x)
   1521 #endif /* !notyet */
   1522 
   1523 /*
   1524  * Arguments to mount LFS filesystems
   1525  */
   1526 struct ulfs_args {
   1527 	char	*fspec;			/* block special device to mount */
   1528 };
   1529 
   1530 __BEGIN_DECLS
   1531 void lfs_itimes(struct inode *, const struct timespec *,
   1532     const struct timespec *, const struct timespec *);
   1533 __END_DECLS
   1534 
   1535 #endif /* !_UFS_LFS_LFS_H_ */
   1536