Home | History | Annotate | Line # | Download | only in lfs
lfs.h revision 1.151
      1 /*	$NetBSD: lfs.h,v 1.151 2013/06/08 23:04:49 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 LFS 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 	u_int32_t	di_inumber;	/*   4: Inode number. */
    433 	u_int64_t	di_size;	/*   8: File byte count. */
    434 	int32_t		di_atime;	/*  16: Last access time. */
    435 	int32_t		di_atimensec;	/*  20: Last access time. */
    436 	int32_t		di_mtime;	/*  24: Last modified time. */
    437 	int32_t		di_mtimensec;	/*  28: Last modified time. */
    438 	int32_t		di_ctime;	/*  32: Last inode change time. */
    439 	int32_t		di_ctimensec;	/*  36: Last inode change time. */
    440 	int32_t		di_db[ULFS_NDADDR]; /*  40: Direct disk blocks. */
    441 	int32_t		di_ib[ULFS_NIADDR]; /*  88: Indirect disk blocks. */
    442 	u_int32_t	di_flags;	/* 100: Status flags (chflags). */
    443 	u_int32_t	di_blocks;	/* 104: Blocks actually held. */
    444 	int32_t		di_gen;		/* 108: Generation number. */
    445 	u_int32_t	di_uid;		/* 112: File owner. */
    446 	u_int32_t	di_gid;		/* 116: File group. */
    447 	u_int64_t	di_modrev;	/* 120: i_modrev for NFSv4 */
    448 };
    449 
    450 struct ulfs2_dinode {
    451 	u_int16_t	di_mode;	/*   0: IFMT, permissions; see below. */
    452 	int16_t		di_nlink;	/*   2: File link count. */
    453 	u_int32_t	di_uid;		/*   4: File owner. */
    454 	u_int32_t	di_gid;		/*   8: File group. */
    455 	u_int32_t	di_blksize;	/*  12: Inode blocksize. */
    456 	u_int64_t	di_size;	/*  16: File byte count. */
    457 	u_int64_t	di_blocks;	/*  24: Bytes actually held. */
    458 	int64_t		di_atime;	/*  32: Last access time. */
    459 	int64_t		di_mtime;	/*  40: Last modified time. */
    460 	int64_t		di_ctime;	/*  48: Last inode change time. */
    461 	int64_t		di_birthtime;	/*  56: Inode creation time. */
    462 	int32_t		di_mtimensec;	/*  64: Last modified time. */
    463 	int32_t		di_atimensec;	/*  68: Last access time. */
    464 	int32_t		di_ctimensec;	/*  72: Last inode change time. */
    465 	int32_t		di_birthnsec;	/*  76: Inode creation time. */
    466 	int32_t		di_gen;		/*  80: Generation number. */
    467 	u_int32_t	di_kernflags;	/*  84: Kernel flags. */
    468 	u_int32_t	di_flags;	/*  88: Status flags (chflags). */
    469 	int32_t		di_extsize;	/*  92: External attributes block. */
    470 	int64_t		di_extb[ULFS_NXADDR];/* 96: External attributes block. */
    471 	int64_t		di_db[ULFS_NDADDR]; /* 112: Direct disk blocks. */
    472 	int64_t		di_ib[ULFS_NIADDR]; /* 208: Indirect disk blocks. */
    473 	u_int64_t	di_modrev;	/* 232: i_modrev for NFSv4 */
    474 	int64_t		di_spare[2];	/* 240: Reserved; currently unused */
    475 };
    476 
    477 /*
    478  * The di_db fields may be overlaid with other information for
    479  * file types that do not have associated disk storage. Block
    480  * and character devices overlay the first data block with their
    481  * dev_t value. Short symbolic links place their path in the
    482  * di_db area.
    483  */
    484 #define	di_rdev		di_db[0]
    485 
    486 /* Size of the on-disk inode. */
    487 #define	LFS_DINODE1_SIZE	(sizeof(struct ulfs1_dinode))	/* 128 */
    488 #define	LFS_DINODE2_SIZE	(sizeof(struct ulfs2_dinode))
    489 
    490 /* File types, found in the upper bits of di_mode. */
    491 #define	LFS_IFMT	0170000		/* Mask of file type. */
    492 #define	LFS_IFIFO	0010000		/* Named pipe (fifo). */
    493 #define	LFS_IFCHR	0020000		/* Character device. */
    494 #define	LFS_IFDIR	0040000		/* Directory file. */
    495 #define	LFS_IFBLK	0060000		/* Block device. */
    496 #define	LFS_IFREG	0100000		/* Regular file. */
    497 #define	LFS_IFLNK	0120000		/* Symbolic link. */
    498 #define	LFS_IFSOCK	0140000		/* UNIX domain socket. */
    499 #define	LFS_IFWHT	0160000		/* Whiteout. */
    500 
    501 /*
    502  * Maximum length of a symlink that can be stored within the inode.
    503  */
    504 #define ULFS1_MAXSYMLINKLEN	((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int32_t))
    505 #define ULFS2_MAXSYMLINKLEN	((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int64_t))
    506 
    507 #define ULFS_MAXSYMLINKLEN(ip) \
    508 	((ip)->i_ump->um_fstype == ULFS1) ? \
    509 	ULFS1_MAXSYMLINKLEN : ULFS2_MAXSYMLINKLEN
    510 
    511 /*
    512  * "struct buf" associated definitions
    513  */
    514 
    515 /* Unassigned disk addresses. */
    516 #define	UNASSIGNED	-1
    517 #define UNWRITTEN	-2
    518 
    519 /* Unused logical block number */
    520 #define LFS_UNUSED_LBN	-1
    521 
    522 /* Determine if a buffer belongs to the ifile */
    523 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
    524 
    525 # define LFS_LOCK_BUF(bp) do {						\
    526 	if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) {	\
    527 		mutex_enter(&lfs_lock);					\
    528 		++locked_queue_count;					\
    529 		locked_queue_bytes += bp->b_bufsize;			\
    530 		mutex_exit(&lfs_lock);					\
    531 	}								\
    532 	(bp)->b_flags |= B_LOCKED;					\
    533 } while (0)
    534 
    535 # define LFS_UNLOCK_BUF(bp) do {					\
    536 	if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {	\
    537 		mutex_enter(&lfs_lock);					\
    538 		--locked_queue_count;					\
    539 		locked_queue_bytes -= bp->b_bufsize;			\
    540 		if (locked_queue_count < LFS_WAIT_BUFS &&		\
    541 		    locked_queue_bytes < LFS_WAIT_BYTES)		\
    542 			cv_broadcast(&locked_queue_cv);			\
    543 		mutex_exit(&lfs_lock);					\
    544 	}								\
    545 	(bp)->b_flags &= ~B_LOCKED;					\
    546 } while (0)
    547 
    548 #ifdef _KERNEL
    549 
    550 extern u_long bufmem_lowater, bufmem_hiwater; /* XXX */
    551 
    552 # define LFS_IS_MALLOC_BUF(bp) ((bp)->b_iodone == lfs_callback)
    553 
    554 # ifdef DEBUG
    555 #  define LFS_DEBUG_COUNTLOCKED(m) do {					\
    556 	if (lfs_debug_log_subsys[DLOG_LLIST]) {				\
    557 		lfs_countlocked(&locked_queue_count, &locked_queue_bytes, (m)); \
    558 		cv_broadcast(&locked_queue_cv);				\
    559 	}								\
    560 } while (0)
    561 # else
    562 #  define LFS_DEBUG_COUNTLOCKED(m)
    563 # endif
    564 
    565 /* log for debugging writes to the Ifile */
    566 # ifdef DEBUG
    567 struct lfs_log_entry {
    568 	const char *op;
    569 	const char *file;
    570 	int pid;
    571 	int line;
    572 	daddr_t block;
    573 	unsigned long flags;
    574 };
    575 extern int lfs_lognum;
    576 extern struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
    577 #  define LFS_BWRITE_LOG(bp) lfs_bwrite_log((bp), __FILE__, __LINE__)
    578 #  define LFS_ENTER_LOG(theop, thefile, theline, lbn, theflags, thepid) do {\
    579 	int _s;								\
    580 									\
    581 	mutex_enter(&lfs_lock);						\
    582 	_s = splbio();							\
    583 	lfs_log[lfs_lognum].op = theop;					\
    584 	lfs_log[lfs_lognum].file = thefile;				\
    585 	lfs_log[lfs_lognum].line = (theline);				\
    586 	lfs_log[lfs_lognum].pid = (thepid);				\
    587 	lfs_log[lfs_lognum].block = (lbn);				\
    588 	lfs_log[lfs_lognum].flags = (theflags);				\
    589 	lfs_lognum = (lfs_lognum + 1) % LFS_LOGLENGTH;			\
    590 	splx(_s);							\
    591 	mutex_exit(&lfs_lock);						\
    592 } while (0)
    593 
    594 #  define LFS_BCLEAN_LOG(fs, bp) do {					\
    595 	if ((bp)->b_vp == (fs)->lfs_ivnode)				\
    596 		LFS_ENTER_LOG("clear", __FILE__, __LINE__,		\
    597 			      bp->b_lblkno, bp->b_flags, curproc->p_pid);\
    598 } while (0)
    599 
    600 /* Must match list in lfs_vfsops.c ! */
    601 #  define DLOG_RF     0  /* roll forward */
    602 #  define DLOG_ALLOC  1  /* inode alloc */
    603 #  define DLOG_AVAIL  2  /* lfs_{,r,f}avail */
    604 #  define DLOG_FLUSH  3  /* flush */
    605 #  define DLOG_LLIST  4  /* locked list accounting */
    606 #  define DLOG_WVNODE 5  /* vflush/writevnodes verbose */
    607 #  define DLOG_VNODE  6  /* vflush/writevnodes */
    608 #  define DLOG_SEG    7  /* segwrite */
    609 #  define DLOG_SU     8  /* seguse accounting */
    610 #  define DLOG_CLEAN  9  /* cleaner routines */
    611 #  define DLOG_MOUNT  10 /* mount/unmount */
    612 #  define DLOG_PAGE   11 /* putpages/gop_write */
    613 #  define DLOG_DIROP  12 /* dirop accounting */
    614 #  define DLOG_MALLOC 13 /* lfs_malloc accounting */
    615 #  define DLOG_MAX    14 /* The terminator */
    616 #  define DLOG(a) lfs_debug_log a
    617 # else /* ! DEBUG */
    618 #  define LFS_BCLEAN_LOG(fs, bp)
    619 #  define LFS_BWRITE_LOG(bp)		VOP_BWRITE((bp)->b_vp, (bp))
    620 #  define DLOG(a)
    621 # endif /* ! DEBUG */
    622 #else /* ! _KERNEL */
    623 # define LFS_BWRITE_LOG(bp)		VOP_BWRITE((bp))
    624 #endif /* _KERNEL */
    625 
    626 #ifdef _KERNEL
    627 /* This overlays the fid structure (see fstypes.h). */
    628 struct ulfs_ufid {
    629 	u_int16_t ufid_len;	/* Length of structure. */
    630 	u_int16_t ufid_pad;	/* Force 32-bit alignment. */
    631 	u_int32_t ufid_ino;	/* File number (ino). */
    632 	int32_t	  ufid_gen;	/* Generation number. */
    633 };
    634 /* Filehandle structure for exported LFSes */
    635 struct lfid {
    636 	struct ulfs_ufid lfid_ufid;
    637 #define lfid_len lfid_ufid.ufid_len
    638 #define lfid_ino lfid_ufid.ufid_ino
    639 #define lfid_gen lfid_ufid.ufid_gen
    640 	uint32_t lfid_ident;
    641 };
    642 #endif /* _KERNEL */
    643 
    644 /*
    645  * "struct inode" associated definitions
    646  */
    647 
    648 /* Address calculations for metadata located in the inode */
    649 #define	S_INDIR(fs)	-ULFS_NDADDR
    650 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
    651 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
    652 
    653 /* For convenience */
    654 #define IN_ALLMOD (IN_MODIFIED|IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY|IN_ACCESSED|IN_CLEANING)
    655 
    656 #define LFS_SET_UINO(ip, flags) do {					\
    657 	if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED))	\
    658 		++(ip)->i_lfs->lfs_uinodes;				\
    659 	if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING))	\
    660 		++(ip)->i_lfs->lfs_uinodes;				\
    661 	if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED))	\
    662 		++(ip)->i_lfs->lfs_uinodes;				\
    663 	(ip)->i_flag |= (flags);					\
    664 } while (0)
    665 
    666 #define LFS_CLR_UINO(ip, flags) do {					\
    667 	if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED))	\
    668 		--(ip)->i_lfs->lfs_uinodes;				\
    669 	if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING))	\
    670 		--(ip)->i_lfs->lfs_uinodes;				\
    671 	if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED))	\
    672 		--(ip)->i_lfs->lfs_uinodes;				\
    673 	(ip)->i_flag &= ~(flags);					\
    674 	if ((ip)->i_lfs->lfs_uinodes < 0) {				\
    675 		panic("lfs_uinodes < 0");				\
    676 	}								\
    677 } while (0)
    678 
    679 #define LFS_ITIMES(ip, acc, mod, cre) \
    680 	while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
    681 		lfs_itimes(ip, acc, mod, cre)
    682 
    683 /*
    684  * "struct vnode" associated definitions
    685  */
    686 
    687 /* Heuristic emptiness measure */
    688 #define VPISEMPTY(vp)	 (LIST_EMPTY(&(vp)->v_dirtyblkhd) && 		\
    689 			  !(vp->v_type == VREG && (vp)->v_iflag & VI_ONWORKLST) &&\
    690 			  VTOI(vp)->i_lfs_nbtree == 0)
    691 
    692 #define WRITEINPROG(vp) ((vp)->v_numoutput > 0 ||			\
    693 	(!LIST_EMPTY(&(vp)->v_dirtyblkhd) &&				\
    694 	 !(VTOI(vp)->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING))))
    695 
    696 
    697 /*
    698  * On-disk and in-memory checkpoint segment usage structure.
    699  */
    700 typedef struct segusage SEGUSE;
    701 struct segusage {
    702 	u_int32_t su_nbytes;		/* 0: number of live bytes */
    703 	u_int32_t su_olastmod;		/* 4: SEGUSE last modified timestamp */
    704 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
    705 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
    706 
    707 #define	SEGUSE_ACTIVE		0x01	/*  segment currently being written */
    708 #define	SEGUSE_DIRTY		0x02	/*  segment has data in it */
    709 #define	SEGUSE_SUPERBLOCK	0x04	/*  segment contains a superblock */
    710 #define SEGUSE_ERROR		0x08	/*  cleaner: do not clean segment */
    711 #define SEGUSE_EMPTY		0x10	/*  segment is empty */
    712 #define SEGUSE_INVAL		0x20	/*  segment is invalid */
    713 	u_int32_t su_flags;		/* 12: segment flags */
    714 	u_int64_t su_lastmod;		/* 16: last modified timestamp */
    715 };
    716 
    717 typedef struct segusage_v1 SEGUSE_V1;
    718 struct segusage_v1 {
    719 	u_int32_t su_nbytes;		/* 0: number of live bytes */
    720 	u_int32_t su_lastmod;		/* 4: SEGUSE last modified timestamp */
    721 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
    722 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
    723 	u_int32_t su_flags;		/* 12: segment flags  */
    724 };
    725 
    726 #define	SEGUPB(fs)	(fs->lfs_sepb)
    727 #define	SEGTABSIZE_SU(fs)						\
    728 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) / (fs)->lfs_sepb)
    729 
    730 #ifdef _KERNEL
    731 # define SHARE_IFLOCK(F) 						\
    732   do {									\
    733 	rw_enter(&(F)->lfs_iflock, RW_READER);				\
    734   } while(0)
    735 # define UNSHARE_IFLOCK(F)						\
    736   do {									\
    737 	rw_exit(&(F)->lfs_iflock);					\
    738   } while(0)
    739 #else /* ! _KERNEL */
    740 # define SHARE_IFLOCK(F)
    741 # define UNSHARE_IFLOCK(F)
    742 #endif /* ! _KERNEL */
    743 
    744 /* Read in the block with a specific segment usage entry from the ifile. */
    745 #define	LFS_SEGENTRY(SP, F, IN, BP) do {				\
    746 	int _e;								\
    747 	SHARE_IFLOCK(F);						\
    748 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    749 	if ((_e = bread((F)->lfs_ivnode,				\
    750 	    ((IN) / (F)->lfs_sepb) + (F)->lfs_cleansz,			\
    751 	    (F)->lfs_bsize, NOCRED, 0, &(BP))) != 0)			\
    752 		panic("lfs: ifile read: %d", _e);			\
    753 	if ((F)->lfs_version == 1)					\
    754 		(SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data +		\
    755 			((IN) & ((F)->lfs_sepb - 1)));			\
    756 	else								\
    757 		(SP) = (SEGUSE *)(BP)->b_data + ((IN) % (F)->lfs_sepb);	\
    758 	UNSHARE_IFLOCK(F);						\
    759 } while (0)
    760 
    761 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do {				\
    762 	if ((SP)->su_nbytes == 0)					\
    763 		(SP)->su_flags |= SEGUSE_EMPTY;				\
    764 	else								\
    765 		(SP)->su_flags &= ~SEGUSE_EMPTY;			\
    766 	(F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags;	\
    767 	LFS_BWRITE_LOG(BP);						\
    768 } while (0)
    769 
    770 /*
    771  * On-disk file information.  One per file with data blocks in the segment.
    772  */
    773 typedef struct finfo FINFO;
    774 struct finfo {
    775 	u_int32_t fi_nblocks;		/* number of blocks */
    776 	u_int32_t fi_version;		/* version number */
    777 	u_int32_t fi_ino;		/* inode number */
    778 	u_int32_t fi_lastlength;	/* length of last block in array */
    779 	int32_t	  fi_blocks[1];		/* array of logical block numbers */
    780 };
    781 /* sizeof FINFO except fi_blocks */
    782 #define	FINFOSIZE	(sizeof(FINFO) - sizeof(int32_t))
    783 
    784 /*
    785  * Index file inode entries.
    786  */
    787 typedef struct ifile IFILE;
    788 struct ifile {
    789 	u_int32_t if_version;		/* inode version number */
    790 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
    791 	int32_t	  if_daddr;		/* inode disk address */
    792 #define LFS_ORPHAN_NEXTFREE	(~(u_int32_t)0) /* indicate orphaned file */
    793 	u_int32_t if_nextfree;		/* next-unallocated inode */
    794 	u_int32_t if_atime_sec;		/* Last access time, seconds */
    795 	u_int32_t if_atime_nsec;	/* and nanoseconds */
    796 };
    797 
    798 typedef struct ifile_v1 IFILE_V1;
    799 struct ifile_v1 {
    800 	u_int32_t if_version;		/* inode version number */
    801 	int32_t	  if_daddr;		/* inode disk address */
    802 	u_int32_t if_nextfree;		/* next-unallocated inode */
    803 #if LFS_ATIME_IFILE
    804 	struct timespec if_atime;	/* Last access time */
    805 #endif
    806 };
    807 
    808 /*
    809  * LFSv1 compatibility code is not allowed to touch if_atime, since it
    810  * may not be mapped!
    811  */
    812 /* Read in the block with a specific inode from the ifile. */
    813 #define	LFS_IENTRY(IP, F, IN, BP) do {					\
    814 	int _e;								\
    815 	SHARE_IFLOCK(F);						\
    816 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    817 	if ((_e = bread((F)->lfs_ivnode,				\
    818 	(IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,	\
    819 	(F)->lfs_bsize, NOCRED, 0, &(BP))) != 0)			\
    820 		panic("lfs: ifile ino %d read %d", (int)(IN), _e);	\
    821 	if ((F)->lfs_version == 1)					\
    822 		(IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data +		\
    823 				 (IN) % (F)->lfs_ifpb);			\
    824 	else								\
    825 		(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;	\
    826 	UNSHARE_IFLOCK(F);						\
    827 } while (0)
    828 
    829 /*
    830  * Cleaner information structure.  This resides in the ifile and is used
    831  * to pass information from the kernel to the cleaner.
    832  */
    833 typedef struct _cleanerinfo {
    834 	u_int32_t clean;		/* number of clean segments */
    835 	u_int32_t dirty;		/* number of dirty segments */
    836 	int32_t   bfree;		/* disk blocks free */
    837 	int32_t	  avail;		/* disk blocks available */
    838 	u_int32_t free_head;		/* head of the inode free list */
    839 	u_int32_t free_tail;		/* tail of the inode free list */
    840 #define LFS_CLEANER_MUST_CLEAN	0x01
    841 	u_int32_t flags;		/* status word from the kernel */
    842 } CLEANERINFO;
    843 
    844 #define	CLEANSIZE_SU(fs)						\
    845 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
    846 
    847 /* Read in the block with the cleaner info from the ifile. */
    848 #define LFS_CLEANERINFO(CP, F, BP) do {					\
    849 	SHARE_IFLOCK(F);						\
    850 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
    851 	if (bread((F)->lfs_ivnode,					\
    852 	    (daddr_t)0, (F)->lfs_bsize, NOCRED, 0, &(BP)))		\
    853 		panic("lfs: ifile read");				\
    854 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
    855 	UNSHARE_IFLOCK(F);						\
    856 } while (0)
    857 
    858 /*
    859  * Synchronize the Ifile cleaner info with current avail and bfree.
    860  */
    861 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do {		 	\
    862     mutex_enter(&lfs_lock);						\
    863     if ((w) || (cip)->bfree != (fs)->lfs_bfree ||		 	\
    864 	(cip)->avail != (fs)->lfs_avail - (fs)->lfs_ravail - 		\
    865 	(fs)->lfs_favail) {	 					\
    866 	(cip)->bfree = (fs)->lfs_bfree;				 	\
    867 	(cip)->avail = (fs)->lfs_avail - (fs)->lfs_ravail -		\
    868 		(fs)->lfs_favail;				 	\
    869 	if (((bp)->b_flags & B_GATHERED) == 0) {		 	\
    870 		(fs)->lfs_flags |= LFS_IFDIRTY;			 	\
    871 	}								\
    872 	mutex_exit(&lfs_lock);						\
    873 	(void) LFS_BWRITE_LOG(bp); /* Ifile */			 	\
    874     } else {							 	\
    875 	mutex_exit(&lfs_lock);						\
    876 	brelse(bp, 0);						 	\
    877     }									\
    878 } while (0)
    879 
    880 /*
    881  * Get the head of the inode free list.
    882  * Always called with the segment lock held.
    883  */
    884 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do {			\
    885 	if ((FS)->lfs_version > 1) {					\
    886 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    887 		(FS)->lfs_freehd = (CIP)->free_head;			\
    888 		brelse(BP, 0);						\
    889 	}								\
    890 	*(FREEP) = (FS)->lfs_freehd;					\
    891 } while (0)
    892 
    893 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do {				\
    894 	(FS)->lfs_freehd = (VAL);					\
    895 	if ((FS)->lfs_version > 1) {					\
    896 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
    897 		(CIP)->free_head = (VAL);				\
    898 		LFS_BWRITE_LOG(BP);					\
    899 		mutex_enter(&lfs_lock);					\
    900 		(FS)->lfs_flags |= LFS_IFDIRTY;				\
    901 		mutex_exit(&lfs_lock);					\
    902 	}								\
    903 } while (0)
    904 
    905 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do {			\
    906 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    907 	*(FREEP) = (CIP)->free_tail;					\
    908 	brelse(BP, 0);							\
    909 } while (0)
    910 
    911 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do {				\
    912 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
    913 	(CIP)->free_tail = (VAL);					\
    914 	LFS_BWRITE_LOG(BP);						\
    915 	mutex_enter(&lfs_lock);						\
    916 	(FS)->lfs_flags |= LFS_IFDIRTY;					\
    917 	mutex_exit(&lfs_lock);						\
    918 } while (0)
    919 
    920 /*
    921  * On-disk segment summary information
    922  */
    923 typedef struct segsum_v1 SEGSUM_V1;
    924 struct segsum_v1 {
    925 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
    926 	u_int32_t ss_datasum;		/* 4: check sum of data */
    927 	u_int32_t ss_magic;		/* 8: segment summary magic number */
    928 #define SS_MAGIC	0x061561
    929 	int32_t	  ss_next;		/* 12: next segment */
    930 	u_int32_t ss_create;		/* 16: creation time stamp */
    931 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
    932 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
    933 
    934 #define	SS_DIROP	0x01		/* segment begins a dirop */
    935 #define	SS_CONT		0x02		/* more partials to finish this write*/
    936 #define	SS_CLEAN	0x04		/* written by the cleaner */
    937 #define	SS_RFW		0x08		/* written by the roll-forward agent */
    938 #define	SS_RECLAIM	0x10		/* written by the roll-forward agent */
    939 	u_int16_t ss_flags;		/* 24: used for directory operations */
    940 	u_int16_t ss_pad;		/* 26: extra space */
    941 	/* FINFO's and inode daddr's... */
    942 };
    943 
    944 typedef struct segsum SEGSUM;
    945 struct segsum {
    946 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
    947 	u_int32_t ss_datasum;		/* 4: check sum of data */
    948 	u_int32_t ss_magic;		/* 8: segment summary magic number */
    949 	int32_t	  ss_next;		/* 12: next segment */
    950 	u_int32_t ss_ident;		/* 16: roll-forward fsid */
    951 #define ss_ocreate ss_ident /* ident is where create was in v1 */
    952 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
    953 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
    954 	u_int16_t ss_flags;		/* 24: used for directory operations */
    955 	u_int8_t  ss_pad[2];		/* 26: extra space */
    956 	u_int32_t ss_reclino;           /* 28: inode being reclaimed */
    957 	u_int64_t ss_serial;		/* 32: serial number */
    958 	u_int64_t ss_create;		/* 40: time stamp */
    959 	/* FINFO's and inode daddr's... */
    960 };
    961 
    962 #define SEGSUM_SIZE(fs) ((fs)->lfs_version == 1 ? sizeof(SEGSUM_V1) : sizeof(SEGSUM))
    963 
    964 
    965 /*
    966  * On-disk super block.
    967  */
    968 struct dlfs {
    969 #define	       LFS_MAGIC       0x070162
    970 	u_int32_t dlfs_magic;	  /* 0: magic number */
    971 #define	       LFS_VERSION     2
    972 	u_int32_t dlfs_version;	  /* 4: version number */
    973 
    974 	u_int32_t dlfs_size;	  /* 8: number of blocks in fs (v1) */
    975 				  /*	number of frags in fs (v2) */
    976 	u_int32_t dlfs_ssize;	  /* 12: number of blocks per segment (v1) */
    977 				  /*	 number of bytes per segment (v2) */
    978 	u_int32_t dlfs_dsize;	  /* 16: number of disk blocks in fs */
    979 	u_int32_t dlfs_bsize;	  /* 20: file system block size */
    980 	u_int32_t dlfs_fsize;	  /* 24: size of frag blocks in fs */
    981 	u_int32_t dlfs_frag;	  /* 28: number of frags in a block in fs */
    982 
    983 /* Checkpoint region. */
    984 	u_int32_t dlfs_freehd;	  /* 32: start of the free list */
    985 	int32_t   dlfs_bfree;	  /* 36: number of free disk blocks */
    986 	u_int32_t dlfs_nfiles;	  /* 40: number of allocated inodes */
    987 	int32_t	  dlfs_avail;	  /* 44: blocks available for writing */
    988 	int32_t	  dlfs_uinodes;	  /* 48: inodes in cache not yet on disk */
    989 	int32_t	  dlfs_idaddr;	  /* 52: inode file disk address */
    990 	u_int32_t dlfs_ifile;	  /* 56: inode file inode number */
    991 	int32_t	  dlfs_lastseg;	  /* 60: address of last segment written */
    992 	int32_t	  dlfs_nextseg;	  /* 64: address of next segment to write */
    993 	int32_t	  dlfs_curseg;	  /* 68: current segment being written */
    994 	int32_t	  dlfs_offset;	  /* 72: offset in curseg for next partial */
    995 	int32_t	  dlfs_lastpseg;  /* 76: address of last partial written */
    996 	u_int32_t dlfs_inopf;	  /* 80: v1: time stamp; v2: inodes per frag */
    997 #define dlfs_otstamp dlfs_inopf
    998 
    999 /* These are configuration parameters. */
   1000 	u_int32_t dlfs_minfree;	  /* 84: minimum percentage of free blocks */
   1001 
   1002 /* These fields can be computed from the others. */
   1003 	u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
   1004 	u_int32_t dlfs_fsbpseg;	    /* 96: fsb per segment */
   1005 	u_int32_t dlfs_inopb;	  /* 100: inodes per block */
   1006 	u_int32_t dlfs_ifpb;	  /* 104: IFILE entries per block */
   1007 	u_int32_t dlfs_sepb;	  /* 108: SEGUSE entries per block */
   1008 	u_int32_t dlfs_nindir;	  /* 112: indirect pointers per block */
   1009 	u_int32_t dlfs_nseg;	  /* 116: number of segments */
   1010 	u_int32_t dlfs_nspf;	  /* 120: number of sectors per fragment */
   1011 	u_int32_t dlfs_cleansz;	  /* 124: cleaner info size in blocks */
   1012 	u_int32_t dlfs_segtabsz;  /* 128: segment table size in blocks */
   1013 	u_int32_t dlfs_segmask;	  /* 132: calculate offset within a segment */
   1014 	u_int32_t dlfs_segshift;  /* 136: fast mult/div for segments */
   1015 	u_int32_t dlfs_bshift;	  /* 140: calc block number from file offset */
   1016 	u_int32_t dlfs_ffshift;	  /* 144: fast mult/div for frag from file */
   1017 	u_int32_t dlfs_fbshift;	  /* 148: fast mult/div for frag from block */
   1018 	u_int64_t dlfs_bmask;	  /* 152: calc block offset from file offset */
   1019 	u_int64_t dlfs_ffmask;	  /* 160: calc frag offset from file offset */
   1020 	u_int64_t dlfs_fbmask;	  /* 168: calc frag offset from block offset */
   1021 	u_int32_t dlfs_blktodb;	  /* 176: blktodb and dbtoblk shift constant */
   1022 	u_int32_t dlfs_sushift;	  /* 180: fast mult/div for segusage table */
   1023 
   1024 	int32_t	  dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
   1025 #define LFS_MIN_SBINTERVAL     5  /* minimum superblock segment spacing */
   1026 #define LFS_MAXNUMSB	       10 /* 188: superblock disk offsets */
   1027 	int32_t	   dlfs_sboffs[LFS_MAXNUMSB];
   1028 
   1029 	u_int32_t dlfs_nclean;	  /* 228: Number of clean segments */
   1030 	u_char	  dlfs_fsmnt[MNAMELEN];	 /* 232: name mounted on */
   1031 #define LFS_PF_CLEAN 0x1
   1032 	u_int16_t dlfs_pflags;	  /* 322: file system persistent flags */
   1033 	int32_t	  dlfs_dmeta;	  /* 324: total number of dirty summaries */
   1034 	u_int32_t dlfs_minfreeseg; /* 328: segments not counted in bfree */
   1035 	u_int32_t dlfs_sumsize;	  /* 332: size of summary blocks */
   1036 	u_int64_t dlfs_serial;	  /* 336: serial number */
   1037 	u_int32_t dlfs_ibsize;	  /* 344: size of inode blocks */
   1038 	int32_t	  dlfs_start;	  /* 348: start of segment 0 */
   1039 	u_int64_t dlfs_tstamp;	  /* 352: time stamp */
   1040 #define LFS_44INODEFMT 0
   1041 #define LFS_MAXINODEFMT 0
   1042 	u_int32_t dlfs_inodefmt;  /* 360: inode format version */
   1043 	u_int32_t dlfs_interleave; /* 364: segment interleave */
   1044 	u_int32_t dlfs_ident;	  /* 368: per-fs identifier */
   1045 	u_int32_t dlfs_fsbtodb;	  /* 372: fsbtodb abd dbtodsb shift constant */
   1046 	u_int32_t dlfs_resvseg;   /* 376: segments reserved for the cleaner */
   1047 	int8_t	  dlfs_pad[128];  /* 380: round to 512 bytes */
   1048 /* Checksum -- last valid disk field. */
   1049 	u_int32_t dlfs_cksum;	  /* 508: checksum for superblock checking */
   1050 };
   1051 
   1052 /* Type used for the inode bitmap */
   1053 typedef u_int32_t lfs_bm_t;
   1054 
   1055 /*
   1056  * Linked list of segments whose byte count needs updating following a
   1057  * file truncation.
   1058  */
   1059 struct segdelta {
   1060 	long segnum;
   1061 	size_t num;
   1062 	LIST_ENTRY(segdelta) list;
   1063 };
   1064 
   1065 /*
   1066  * In-memory super block.
   1067  */
   1068 struct lfs {
   1069 	struct dlfs lfs_dlfs;		/* on-disk parameters */
   1070 #define lfs_magic lfs_dlfs.dlfs_magic
   1071 #define lfs_version lfs_dlfs.dlfs_version
   1072 #define lfs_size lfs_dlfs.dlfs_size
   1073 #define lfs_ssize lfs_dlfs.dlfs_ssize
   1074 #define lfs_dsize lfs_dlfs.dlfs_dsize
   1075 #define lfs_bsize lfs_dlfs.dlfs_bsize
   1076 #define lfs_fsize lfs_dlfs.dlfs_fsize
   1077 #define lfs_frag lfs_dlfs.dlfs_frag
   1078 #define lfs_freehd lfs_dlfs.dlfs_freehd
   1079 #define lfs_bfree lfs_dlfs.dlfs_bfree
   1080 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
   1081 #define lfs_avail lfs_dlfs.dlfs_avail
   1082 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
   1083 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
   1084 #define lfs_ifile lfs_dlfs.dlfs_ifile
   1085 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
   1086 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
   1087 #define lfs_curseg lfs_dlfs.dlfs_curseg
   1088 #define lfs_offset lfs_dlfs.dlfs_offset
   1089 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
   1090 #define lfs_otstamp lfs_dlfs.dlfs_inopf
   1091 #define lfs_inopf lfs_dlfs.dlfs_inopf
   1092 #define lfs_minfree lfs_dlfs.dlfs_minfree
   1093 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
   1094 #define lfs_fsbpseg lfs_dlfs.dlfs_fsbpseg
   1095 #define lfs_inopb lfs_dlfs.dlfs_inopb
   1096 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
   1097 #define lfs_sepb lfs_dlfs.dlfs_sepb
   1098 #define lfs_nindir lfs_dlfs.dlfs_nindir
   1099 #define lfs_nseg lfs_dlfs.dlfs_nseg
   1100 #define lfs_nspf lfs_dlfs.dlfs_nspf
   1101 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
   1102 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
   1103 #define lfs_segmask lfs_dlfs.dlfs_segmask
   1104 #define lfs_segshift lfs_dlfs.dlfs_segshift
   1105 #define lfs_bmask lfs_dlfs.dlfs_bmask
   1106 #define lfs_bshift lfs_dlfs.dlfs_bshift
   1107 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
   1108 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
   1109 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
   1110 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
   1111 #define lfs_blktodb lfs_dlfs.dlfs_blktodb
   1112 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
   1113 #define lfs_sushift lfs_dlfs.dlfs_sushift
   1114 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
   1115 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
   1116 #define lfs_cksum lfs_dlfs.dlfs_cksum
   1117 #define lfs_pflags lfs_dlfs.dlfs_pflags
   1118 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
   1119 #define lfs_nclean lfs_dlfs.dlfs_nclean
   1120 #define lfs_dmeta lfs_dlfs.dlfs_dmeta
   1121 #define lfs_minfreeseg lfs_dlfs.dlfs_minfreeseg
   1122 #define lfs_sumsize lfs_dlfs.dlfs_sumsize
   1123 #define lfs_serial lfs_dlfs.dlfs_serial
   1124 #define lfs_ibsize lfs_dlfs.dlfs_ibsize
   1125 #define lfs_start lfs_dlfs.dlfs_start
   1126 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
   1127 #define lfs_inodefmt lfs_dlfs.dlfs_inodefmt
   1128 #define lfs_interleave lfs_dlfs.dlfs_interleave
   1129 #define lfs_ident lfs_dlfs.dlfs_ident
   1130 #define lfs_resvseg lfs_dlfs.dlfs_resvseg
   1131 
   1132 /* These fields are set at mount time and are meaningless on disk. */
   1133 	struct segment *lfs_sp;		/* current segment being written */
   1134 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
   1135 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
   1136 	pid_t	  lfs_lockpid;		/* pid of lock holder */
   1137 	lwpid_t	  lfs_locklwp;		/* lwp of lock holder */
   1138 	u_int32_t lfs_iocount;		/* number of ios pending */
   1139 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
   1140 	u_int32_t lfs_dirops;		/* count of active directory ops */
   1141 	u_int32_t lfs_dirvcount;	/* count of VDIROP nodes in this fs */
   1142 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
   1143 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
   1144 	int8_t	  lfs_fmod;		/* super block modified flag */
   1145 	int8_t	  lfs_ronly;		/* mounted read-only flag */
   1146 #define LFS_NOTYET  0x01
   1147 #define LFS_IFDIRTY 0x02
   1148 #define LFS_WARNED  0x04
   1149 #define LFS_UNDIROP 0x08
   1150 	int8_t	  lfs_flags;		/* currently unused flag */
   1151 	u_int16_t lfs_activesb;		/* toggle between superblocks */
   1152 	daddr_t	  lfs_sbactive;		/* disk address of current sb write */
   1153 	struct vnode *lfs_flushvp;	/* vnode being flushed */
   1154 	int lfs_flushvp_fakevref;	/* fake vref count for flushvp */
   1155 	struct vnode *lfs_unlockvp;	/* being inactivated in lfs_segunlock */
   1156 	u_int32_t lfs_diropwait;	/* # procs waiting on dirop flush */
   1157 	size_t lfs_devbsize;		/* Device block size */
   1158 	size_t lfs_devbshift;		/* Device block shift */
   1159 	krwlock_t lfs_fraglock;
   1160 	krwlock_t lfs_iflock;		/* Ifile lock */
   1161 	kcondvar_t lfs_stopcv;		/* Wrap lock */
   1162 	struct lwp *lfs_stoplwp;
   1163 	pid_t lfs_rfpid;		/* Process ID of roll-forward agent */
   1164 	int	  lfs_nadirop;		/* number of active dirop nodes */
   1165 	long	  lfs_ravail;		/* blocks pre-reserved for writing */
   1166 	long	  lfs_favail;		/* blocks pre-reserved for writing */
   1167 	res_t *lfs_resblk;		/* Reserved memory for pageout */
   1168 	TAILQ_HEAD(, inode) lfs_dchainhd; /* dirop vnodes */
   1169 	TAILQ_HEAD(, inode) lfs_pchainhd; /* paging vnodes */
   1170 #define LFS_RESHASH_WIDTH 17
   1171 	LIST_HEAD(, lfs_res_blk) lfs_reshash[LFS_RESHASH_WIDTH];
   1172 	int	  lfs_pdflush;		 /* pagedaemon wants us to flush */
   1173 	u_int32_t **lfs_suflags;	/* Segment use flags */
   1174 #ifdef _KERNEL
   1175 	struct pool lfs_clpool;		/* Pool for struct lfs_cluster */
   1176 	struct pool lfs_bpppool;	/* Pool for bpp */
   1177 	struct pool lfs_segpool;	/* Pool for struct segment */
   1178 #endif /* _KERNEL */
   1179 #define LFS_MAX_CLEANIND 64
   1180 	int32_t  lfs_cleanint[LFS_MAX_CLEANIND]; /* Active cleaning intervals */
   1181 	int 	 lfs_cleanind;		/* Index into intervals */
   1182 	int lfs_sleepers;		/* # procs sleeping this fs */
   1183 	int lfs_pages;			/* dirty pages blaming this fs */
   1184 	lfs_bm_t *lfs_ino_bitmap;	/* Inuse inodes bitmap */
   1185 	int lfs_nowrap;			/* Suspend log wrap */
   1186 	int lfs_wrappass;		/* Allow first log wrap requester to pass */
   1187 	int lfs_wrapstatus;		/* Wrap status */
   1188 	int lfs_reclino;		/* Inode being reclaimed */
   1189 	int lfs_startseg;               /* Segment we started writing at */
   1190 	LIST_HEAD(, segdelta) lfs_segdhd;	/* List of pending trunc accounting events */
   1191 };
   1192 
   1193 /* NINDIR is the number of indirects in a file system block. */
   1194 #define	NINDIR(fs)	((fs)->lfs_nindir)
   1195 
   1196 /* INOPB is the number of inodes in a secondary storage block. */
   1197 #define	INOPB(fs)	((fs)->lfs_inopb)
   1198 /* INOPF is the number of inodes in a fragment. */
   1199 #define INOPF(fs)	((fs)->lfs_inopf)
   1200 
   1201 #define	blksize(fs, ip, lbn) \
   1202 	(((lbn) >= ULFS_NDADDR || (ip)->i_ffs1_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
   1203 	    ? (fs)->lfs_bsize \
   1204 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs1_size))))
   1205 #define	blkoff(fs, loc)		((int)((loc) & (fs)->lfs_bmask))
   1206 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
   1207     ((int)((loc) & (fs)->lfs_ffmask))
   1208 
   1209 #if defined (_KERNEL)
   1210 #define	fsbtodb(fs, b)		((b) << ((fs)->lfs_ffshift - DEV_BSHIFT))
   1211 #define	dbtofsb(fs, b)		((b) >> ((fs)->lfs_ffshift - DEV_BSHIFT))
   1212 #else
   1213 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
   1214 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
   1215 #endif
   1216 
   1217 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
   1218 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
   1219 
   1220 #define fsbtob(fs, b)		((b) << (fs)->lfs_ffshift)
   1221 #define btofsb(fs, b)		((b) >> (fs)->lfs_ffshift)
   1222 
   1223 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
   1224 	((loc) >> (fs)->lfs_ffshift)
   1225 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
   1226 	((off_t)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
   1227 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
   1228 	((off_t)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
   1229 #define fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
   1230 	((frags) >> (fs)->lfs_fbshift)
   1231 #define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
   1232 	((blks) << (fs)->lfs_fbshift)
   1233 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */	\
   1234 	((fsb) & ((fs)->lfs_frag - 1))
   1235 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
   1236 	((fsb) &~ ((fs)->lfs_frag - 1))
   1237 #define dblksize(fs, dp, lbn) \
   1238 	(((lbn) >= ULFS_NDADDR || (dp)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
   1239 	    ? (fs)->lfs_bsize \
   1240 	    : (fragroundup(fs, blkoff(fs, (dp)->di_size))))
   1241 
   1242 #define	segsize(fs)	((fs)->lfs_version == 1 ?	     		\
   1243 			   lblktosize((fs), (fs)->lfs_ssize) :		\
   1244 			   (fs)->lfs_ssize)
   1245 #define segtod(fs, seg) (((fs)->lfs_version == 1     ?	     		\
   1246 			   (fs)->lfs_ssize << (fs)->lfs_blktodb :	\
   1247 			   btofsb((fs), (fs)->lfs_ssize)) * (seg))
   1248 #define	dtosn(fs, daddr)	/* block address to segment number */	\
   1249 	((uint32_t)(((daddr) - (fs)->lfs_start) / segtod((fs), 1)))
   1250 #define sntod(fs, sn)		/* segment number to disk address */	\
   1251 	((daddr_t)(segtod((fs), (sn)) + (fs)->lfs_start))
   1252 
   1253 /*
   1254  * Structures used by lfs_bmapv and lfs_markv to communicate information
   1255  * about inodes and data blocks.
   1256  */
   1257 typedef struct block_info {
   1258 	u_int32_t bi_inode;		/* inode # */
   1259 	int32_t	bi_lbn;			/* logical block w/in file */
   1260 	int32_t	bi_daddr;		/* disk address of block */
   1261 	u_int64_t bi_segcreate;		/* origin segment create time */
   1262 	int	bi_version;		/* file version number */
   1263 	void	*bi_bp;			/* data buffer */
   1264 	int	bi_size;		/* size of the block (if fragment) */
   1265 } BLOCK_INFO;
   1266 
   1267 /* Compatibility for 1.5 binaries */
   1268 typedef struct block_info_15 {
   1269 	u_int32_t bi_inode;		/* inode # */
   1270 	int32_t	bi_lbn;			/* logical block w/in file */
   1271 	int32_t	bi_daddr;		/* disk address of block */
   1272 	u_int32_t bi_segcreate;		/* origin segment create time */
   1273 	int	bi_version;		/* file version number */
   1274 	void	*bi_bp;			/* data buffer */
   1275 	int	bi_size;		/* size of the block (if fragment) */
   1276 } BLOCK_INFO_15;
   1277 
   1278 /* In-memory description of a segment about to be written. */
   1279 struct segment {
   1280 	struct lfs	 *fs;		/* file system pointer */
   1281 	struct buf	**bpp;		/* pointer to buffer array */
   1282 	struct buf	**cbpp;		/* pointer to next available bp */
   1283 	struct buf	**start_bpp;	/* pointer to first bp in this set */
   1284 	struct buf	 *ibp;		/* buffer pointer to inode page */
   1285 	struct ulfs1_dinode    *idp;          /* pointer to ifile dinode */
   1286 	struct finfo	 *fip;		/* current fileinfo pointer */
   1287 	struct vnode	 *vp;		/* vnode being gathered */
   1288 	void	 *segsum;		/* segment summary info */
   1289 	u_int32_t ninodes;		/* number of inodes in this segment */
   1290 	int32_t seg_bytes_left;		/* bytes left in segment */
   1291 	int32_t sum_bytes_left;		/* bytes left in summary block */
   1292 	u_int32_t seg_number;		/* number of this segment */
   1293 	int32_t *start_lbp;		/* beginning lbn for this set */
   1294 
   1295 #define SEGM_CKP	0x0001		/* doing a checkpoint */
   1296 #define SEGM_CLEAN	0x0002		/* cleaner call; don't sort */
   1297 #define SEGM_SYNC	0x0004		/* wait for segment */
   1298 #define SEGM_PROT	0x0008		/* don't inactivate at segunlock */
   1299 #define SEGM_PAGEDAEMON	0x0010		/* pagedaemon called us */
   1300 #define SEGM_WRITERD	0x0020		/* LFS writed called us */
   1301 #define SEGM_FORCE_CKP	0x0040		/* Force checkpoint right away */
   1302 #define SEGM_RECLAIM	0x0080		/* Writing to reclaim vnode */
   1303 #define SEGM_SINGLE	0x0100		/* Opportunistic writevnodes */
   1304 	u_int16_t seg_flags;		/* run-time flags for this segment */
   1305 	u_int32_t seg_iocount;		/* number of ios pending */
   1306 	int	  ndupino;		/* number of duplicate inodes */
   1307 };
   1308 
   1309 #ifdef _KERNEL
   1310 struct lfs_cluster {
   1311 	size_t bufsize;	       /* Size of kept data */
   1312 	struct buf **bpp;      /* Array of kept buffers */
   1313 	int bufcount;	       /* Number of kept buffers */
   1314 #define LFS_CL_MALLOC	0x00000001
   1315 #define LFS_CL_SHIFT	0x00000002
   1316 #define LFS_CL_SYNC	0x00000004
   1317 	u_int32_t flags;       /* Flags */
   1318 	struct lfs *fs;	       /* LFS that this belongs to */
   1319 	struct segment *seg;   /* Segment structure, for LFS_CL_SYNC */
   1320 };
   1321 
   1322 /*
   1323  * Splay tree containing block numbers allocated through lfs_balloc.
   1324  */
   1325 struct lbnentry {
   1326 	SPLAY_ENTRY(lbnentry) entry;
   1327 	daddr_t lbn;
   1328 };
   1329 #endif /* _KERNEL */
   1330 
   1331 /*
   1332  * LFS inode extensions.
   1333  */
   1334 struct lfs_inode_ext {
   1335 	off_t	  lfs_osize;		/* size of file on disk */
   1336 	u_int32_t lfs_effnblocks;  /* number of blocks when i/o completes */
   1337 	size_t	  lfs_fragsize[ULFS_NDADDR]; /* size of on-disk direct blocks */
   1338 	TAILQ_ENTRY(inode) lfs_dchain;  /* Dirop chain. */
   1339 	TAILQ_ENTRY(inode) lfs_pchain;  /* Paging chain. */
   1340 #define LFSI_NO_GOP_WRITE 0x01
   1341 #define LFSI_DELETED      0x02
   1342 #define LFSI_WRAPBLOCK    0x04
   1343 #define LFSI_WRAPWAIT     0x08
   1344 #define LFSI_BMAP         0x10
   1345 	u_int32_t lfs_iflags;           /* Inode flags */
   1346 	daddr_t   lfs_hiblk;		/* Highest lbn held by inode */
   1347 #ifdef _KERNEL
   1348 	SPLAY_HEAD(lfs_splay, lbnentry) lfs_lbtree; /* Tree of balloc'd lbns */
   1349 	int	  lfs_nbtree;		/* Size of tree */
   1350 	LIST_HEAD(, segdelta) lfs_segdhd;
   1351 #endif
   1352 	int16_t	  lfs_odnlink;		/* on-disk nlink count for cleaner */
   1353 };
   1354 #define i_lfs_osize		inode_ext.lfs->lfs_osize
   1355 #define i_lfs_effnblks		inode_ext.lfs->lfs_effnblocks
   1356 #define i_lfs_fragsize		inode_ext.lfs->lfs_fragsize
   1357 #define i_lfs_dchain		inode_ext.lfs->lfs_dchain
   1358 #define i_lfs_pchain		inode_ext.lfs->lfs_pchain
   1359 #define i_lfs_iflags		inode_ext.lfs->lfs_iflags
   1360 #define i_lfs_hiblk		inode_ext.lfs->lfs_hiblk
   1361 #define i_lfs_lbtree		inode_ext.lfs->lfs_lbtree
   1362 #define i_lfs_nbtree		inode_ext.lfs->lfs_nbtree
   1363 #define i_lfs_segdhd		inode_ext.lfs->lfs_segdhd
   1364 #define i_lfs_odnlink		inode_ext.lfs->lfs_odnlink
   1365 
   1366 /*
   1367  * Macros for determining free space on the disk, with the variable metadata
   1368  * of segment summaries and inode blocks taken into account.
   1369  */
   1370 /*
   1371  * Estimate number of clean blocks not available for writing because
   1372  * they will contain metadata or overhead.  This is calculated as
   1373  *
   1374  *		E = ((C * M / D) * D + (0) * (T - D)) / T
   1375  * or more simply
   1376  *		E = (C * M) / T
   1377  *
   1378  * where
   1379  * C is the clean space,
   1380  * D is the dirty space,
   1381  * M is the dirty metadata, and
   1382  * T = C + D is the total space on disk.
   1383  *
   1384  * This approximates the old formula of E = C * M / D when D is close to T,
   1385  * but avoids falsely reporting "disk full" when the sample size (D) is small.
   1386  */
   1387 #define LFS_EST_CMETA(F) (int32_t)((					\
   1388 	((F)->lfs_dmeta * (int64_t)(F)->lfs_nclean) / 			\
   1389 	((F)->lfs_nseg)))
   1390 
   1391 /* Estimate total size of the disk not including metadata */
   1392 #define LFS_EST_NONMETA(F) ((F)->lfs_dsize - (F)->lfs_dmeta - LFS_EST_CMETA(F))
   1393 
   1394 /* Estimate number of blocks actually available for writing */
   1395 #define LFS_EST_BFREE(F) ((F)->lfs_bfree > LFS_EST_CMETA(F) ?		     \
   1396 			  (F)->lfs_bfree - LFS_EST_CMETA(F) : 0)
   1397 
   1398 /* Amount of non-meta space not available to mortal man */
   1399 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) *			     \
   1400 				   (u_int64_t)(F)->lfs_minfree) /	     \
   1401 				  100)
   1402 
   1403 /* Can credential C write BB blocks */
   1404 #define ISSPACE(F, BB, C)						\
   1405 	((((C) == NOCRED || kauth_cred_geteuid(C) == 0) &&		\
   1406 	  LFS_EST_BFREE(F) >= (BB)) ||					\
   1407 	 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
   1408 
   1409 /* Can an ordinary user write BB blocks */
   1410 #define IS_FREESPACE(F, BB)						\
   1411 	  (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
   1412 
   1413 /*
   1414  * The minimum number of blocks to create a new inode.  This is:
   1415  * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
   1416  * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
   1417  */
   1418 #define LFS_NRESERVE(F) (btofsb((F), (2 * ULFS_NIADDR + 3) << (F)->lfs_bshift))
   1419 
   1420 /* Statistics Counters */
   1421 struct lfs_stats {	/* Must match sysctl list in lfs_vfsops.h ! */
   1422 	u_int	segsused;
   1423 	u_int	psegwrites;
   1424 	u_int	psyncwrites;
   1425 	u_int	pcleanwrites;
   1426 	u_int	blocktot;
   1427 	u_int	cleanblocks;
   1428 	u_int	ncheckpoints;
   1429 	u_int	nwrites;
   1430 	u_int	nsync_writes;
   1431 	u_int	wait_exceeded;
   1432 	u_int	write_exceeded;
   1433 	u_int	flush_invoked;
   1434 	u_int	vflush_invoked;
   1435 	u_int	clean_inlocked;
   1436 	u_int	clean_vnlocked;
   1437 	u_int   segs_reclaimed;
   1438 };
   1439 #ifdef _KERNEL
   1440 extern struct lfs_stats lfs_stats;
   1441 #endif
   1442 
   1443 /* Fcntls to take the place of the lfs syscalls */
   1444 struct lfs_fcntl_markv {
   1445 	BLOCK_INFO *blkiov;	/* blocks to relocate */
   1446 	int blkcnt;		/* number of blocks */
   1447 };
   1448 
   1449 #define LFCNSEGWAITALL	_FCNR_FSPRIV('L', 14, struct timeval)
   1450 #define LFCNSEGWAIT	_FCNR_FSPRIV('L', 15, struct timeval)
   1451 #define LFCNBMAPV	_FCNRW_FSPRIV('L', 2, struct lfs_fcntl_markv)
   1452 #define LFCNMARKV	_FCNRW_FSPRIV('L', 3, struct lfs_fcntl_markv)
   1453 #define LFCNRECLAIM	 _FCNO_FSPRIV('L', 4)
   1454 
   1455 struct lfs_fhandle {
   1456 	char space[28];	/* FHANDLE_SIZE_COMPAT (but used from userland too) */
   1457 };
   1458 #define LFCNREWIND       _FCNR_FSPRIV('L', 6, int)
   1459 #define LFCNINVAL        _FCNR_FSPRIV('L', 7, int)
   1460 #define LFCNRESIZE       _FCNR_FSPRIV('L', 8, int)
   1461 #define LFCNWRAPSTOP	 _FCNR_FSPRIV('L', 9, int)
   1462 #define LFCNWRAPGO	 _FCNR_FSPRIV('L', 10, int)
   1463 #define LFCNIFILEFH	 _FCNW_FSPRIV('L', 11, struct lfs_fhandle)
   1464 #define LFCNWRAPPASS	 _FCNR_FSPRIV('L', 12, int)
   1465 # define LFS_WRAP_GOING   0x0
   1466 # define LFS_WRAP_WAITING 0x1
   1467 #define LFCNWRAPSTATUS	 _FCNW_FSPRIV('L', 13, int)
   1468 
   1469 /*
   1470  * Compat.  Defined for kernel only.  Userland always uses
   1471  * "the one true version".
   1472  */
   1473 #ifdef _KERNEL
   1474 #include <compat/sys/time_types.h>
   1475 
   1476 #define LFCNSEGWAITALL_COMPAT	 _FCNW_FSPRIV('L', 0, struct timeval50)
   1477 #define LFCNSEGWAIT_COMPAT	 _FCNW_FSPRIV('L', 1, struct timeval50)
   1478 #define LFCNIFILEFH_COMPAT	 _FCNW_FSPRIV('L', 5, struct lfs_fhandle)
   1479 #define LFCNIFILEFH_COMPAT2	 _FCN_FSPRIV(F_FSOUT, 'L', 11, 32)
   1480 #define LFCNWRAPSTOP_COMPAT	 _FCNO_FSPRIV('L', 9)
   1481 #define LFCNWRAPGO_COMPAT	 _FCNO_FSPRIV('L', 10)
   1482 #define LFCNSEGWAITALL_COMPAT_50 _FCNR_FSPRIV('L', 0, struct timeval50)
   1483 #define LFCNSEGWAIT_COMPAT_50	 _FCNR_FSPRIV('L', 1, struct timeval50)
   1484 #endif
   1485 
   1486 #ifdef _KERNEL
   1487 /* XXX MP */
   1488 #define	LFS_SEGLOCK_HELD(fs) \
   1489 	((fs)->lfs_seglock != 0 &&					\
   1490 	 (fs)->lfs_lockpid == curproc->p_pid &&				\
   1491 	 (fs)->lfs_locklwp == curlwp->l_lid)
   1492 #endif /* _KERNEL */
   1493 
   1494 /* Debug segment lock */
   1495 #ifdef notyet
   1496 # define ASSERT_SEGLOCK(fs) KASSERT(LFS_SEGLOCK_HELD(fs))
   1497 # define ASSERT_NO_SEGLOCK(fs) KASSERT(!LFS_SEGLOCK_HELD(fs))
   1498 # define ASSERT_DUNNO_SEGLOCK(fs)
   1499 # define ASSERT_MAYBE_SEGLOCK(fs)
   1500 #else /* !notyet */
   1501 # define ASSERT_DUNNO_SEGLOCK(fs) \
   1502 	DLOG((DLOG_SEG, "lfs func %s seglock wrong (%d)\n", __func__, \
   1503 		LFS_SEGLOCK_HELD(fs)))
   1504 # define ASSERT_SEGLOCK(fs) do {					\
   1505 	if (!LFS_SEGLOCK_HELD(fs)) {					\
   1506 		DLOG((DLOG_SEG, "lfs func %s seglock wrong (0)\n", __func__)); \
   1507 	}								\
   1508 } while(0)
   1509 # define ASSERT_NO_SEGLOCK(fs) do {					\
   1510 	if (LFS_SEGLOCK_HELD(fs)) {					\
   1511 		DLOG((DLOG_SEG, "lfs func %s seglock wrong (1)\n", __func__)); \
   1512 	}								\
   1513 } while(0)
   1514 # define ASSERT_MAYBE_SEGLOCK(x)
   1515 #endif /* !notyet */
   1516 
   1517 /*
   1518  * Arguments to mount LFS filesystems
   1519  */
   1520 struct ulfs_args {
   1521 	char	*fspec;			/* block special device to mount */
   1522 };
   1523 
   1524 __BEGIN_DECLS
   1525 void lfs_itimes(struct inode *, const struct timespec *,
   1526     const struct timespec *, const struct timespec *);
   1527 __END_DECLS
   1528 
   1529 #endif /* !_UFS_LFS_LFS_H_ */
   1530