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