xref: /netbsd/sys/ufs/lfs/lfs.h (revision 6550d01e)
1 /*	$NetBSD: lfs.h,v 1.133 2010/02/16 23:20:30 mlelstv Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Konrad E. Schroder <perseant@hhhh.org>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*-
32  * Copyright (c) 1991, 1993
33  *	The Regents of the University of California.  All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  *	@(#)lfs.h	8.9 (Berkeley) 5/8/95
60  */
61 
62 #ifndef _UFS_LFS_LFS_H_
63 #define _UFS_LFS_LFS_H_
64 
65 #include <sys/rwlock.h>
66 #include <sys/mutex.h>
67 #include <sys/queue.h>
68 #include <sys/condvar.h>
69 
70 /*
71  * Compile-time options for LFS.
72  */
73 #define LFS_IFIND_RETRIES  16
74 #define LFS_LOGLENGTH      1024 /* size of debugging log */
75 #define LFS_MAX_ACTIVE	   10	/* Dirty segments before ckp forced */
76 
77 /*
78  * Fixed filesystem layout parameters
79  */
80 #define	LFS_LABELPAD	8192		/* LFS label size */
81 #define	LFS_SBPAD	8192		/* LFS superblock size */
82 
83 #define	LFS_UNUSED_INUM	0		/* 0: out of band inode number */
84 #define	LFS_IFILE_INUM	1		/* 1: IFILE inode number */
85 					/* 2: Root inode number */
86 #define	LOSTFOUNDINO	3		/* 3: lost+found inode number */
87 #define	LFS_FIRST_INUM	4		/* 4: first free inode number */
88 
89 #define	LFS_V1_SUMMARY_SIZE	512     /* V1 fixed summary size */
90 #define	LFS_DFL_SUMMARY_SIZE	512	/* Default summary size */
91 
92 #define LFS_MAX_DADDR	0x7fffffff	/* Highest addressable fsb */
93 
94 #define LFS_MAXNAMLEN	255		/* maximum name length in a dir */
95 
96 /* Adjustable filesystem parameters */
97 #define MIN_FREE_SEGS	20
98 #define MIN_RESV_SEGS	15
99 #ifndef LFS_ATIME_IFILE
100 # define LFS_ATIME_IFILE 0 /* Store atime info in ifile (optional in LFSv1) */
101 #endif
102 #define LFS_MARKV_MAXBLKCNT	65536	/* Max block count for lfs_markv() */
103 
104 /* Misc. definitions */
105 #define BW_CLEAN	1		/* Flag for lfs_bwrite_ext() */
106 #define PG_DELWRI	PG_PAGER1	/* Local def for delayed pageout */
107 
108 /* Resource limits */
109 #define	LFS_MAX_RESOURCE(x, u)	(((x) >> 2) - 10 * (u))
110 #define	LFS_WAIT_RESOURCE(x, u)	(((x) >> 1) - ((x) >> 3) - 10 * (u))
111 #define	LFS_INVERSE_MAX_RESOURCE(x, u)	(((x) + 10 * (u)) << 2)
112 #define LFS_MAX_BUFS	    LFS_MAX_RESOURCE(nbuf, 1)
113 #define LFS_WAIT_BUFS	    LFS_WAIT_RESOURCE(nbuf, 1)
114 #define LFS_INVERSE_MAX_BUFS(n)	LFS_INVERSE_MAX_RESOURCE(n, 1)
115 #define LFS_MAX_BYTES	    LFS_MAX_RESOURCE(bufmem_lowater, PAGE_SIZE)
116 #define LFS_INVERSE_MAX_BYTES(n) LFS_INVERSE_MAX_RESOURCE(n, PAGE_SIZE)
117 #define LFS_WAIT_BYTES	    LFS_WAIT_RESOURCE(bufmem_lowater, PAGE_SIZE)
118 #define LFS_MAX_DIROP	    ((desiredvnodes >> 2) + (desiredvnodes >> 3))
119 #define SIZEOF_DIROP(fs)	(2 * ((fs)->lfs_bsize + DINODE1_SIZE))
120 #define LFS_MAX_FSDIROP(fs)						\
121 	((fs)->lfs_nclean <= (fs)->lfs_resvseg ? 0 :			\
122 	 (((fs)->lfs_nclean - (fs)->lfs_resvseg) * (fs)->lfs_ssize) /	\
123           (2 * SIZEOF_DIROP(fs)))
124 #define LFS_MAX_PAGES	lfs_max_pages()
125 #define LFS_WAIT_PAGES	lfs_wait_pages()
126 #define LFS_BUFWAIT	    2	/* How long to wait if over *_WAIT_* */
127 
128 #ifdef _KERNEL
129 int lfs_wait_pages(void);
130 int lfs_max_pages(void);
131 #endif /* _KERNEL */
132 
133 /* How starved can we be before we start holding back page writes */
134 #define LFS_STARVED_FOR_SEGS(fs) ((fs)->lfs_nclean < (fs)->lfs_resvseg)
135 
136 /*
137  * Reserved blocks for lfs_malloc
138  */
139 
140 /* Structure to keep reserved blocks */
141 typedef struct lfs_res_blk {
142 	void *p;
143 	LIST_ENTRY(lfs_res_blk) res;
144 	int size;
145 	char inuse;
146 } res_t;
147 
148 /* Types for lfs_newbuf and lfs_malloc */
149 #define LFS_NB_UNKNOWN -1
150 #define LFS_NB_SUMMARY	0
151 #define LFS_NB_SBLOCK	1
152 #define LFS_NB_IBLOCK	2
153 #define LFS_NB_CLUSTER	3
154 #define LFS_NB_CLEAN	4
155 #define LFS_NB_BLKIOV	5
156 #define LFS_NB_COUNT	6 /* always last */
157 
158 /* Number of reserved memory blocks of each type */
159 #define LFS_N_SUMMARIES 2
160 #define LFS_N_SBLOCKS	1   /* Always 1, to throttle superblock writes */
161 #define LFS_N_IBLOCKS	16  /* In theory ssize/bsize; in practice around 2 */
162 #define LFS_N_CLUSTERS	16  /* In theory ssize/MAXPHYS */
163 #define LFS_N_CLEAN	0
164 #define LFS_N_BLKIOV	1
165 
166 /* Total count of "large" (non-pool) types */
167 #define LFS_N_TOTAL (LFS_N_SUMMARIES + LFS_N_SBLOCKS + LFS_N_IBLOCKS +	\
168 		     LFS_N_CLUSTERS + LFS_N_CLEAN + LFS_N_BLKIOV)
169 
170 /* Counts for pool types */
171 #define LFS_N_CL	LFS_N_CLUSTERS
172 #define LFS_N_BPP	2
173 #define LFS_N_SEG	2
174 
175 /*
176  * "struct buf" associated definitions
177  */
178 
179 /* Unassigned disk addresses. */
180 #define	UNASSIGNED	-1
181 #define UNWRITTEN	-2
182 
183 /* Unused logical block number */
184 #define LFS_UNUSED_LBN	-1
185 
186 /* Determine if a buffer belongs to the ifile */
187 #define IS_IFILE(bp)	(VTOI(bp->b_vp)->i_number == LFS_IFILE_INUM)
188 
189 # define LFS_LOCK_BUF(bp) do {						\
190 	if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) {	\
191 		mutex_enter(&lfs_lock);					\
192 		++locked_queue_count;					\
193 		locked_queue_bytes += bp->b_bufsize;			\
194 		mutex_exit(&lfs_lock);					\
195 	}								\
196 	(bp)->b_flags |= B_LOCKED;					\
197 } while (0)
198 
199 # define LFS_UNLOCK_BUF(bp) do {					\
200 	if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) {	\
201 		mutex_enter(&lfs_lock);					\
202 		--locked_queue_count;					\
203 		locked_queue_bytes -= bp->b_bufsize;			\
204 		if (locked_queue_count < LFS_WAIT_BUFS &&		\
205 		    locked_queue_bytes < LFS_WAIT_BYTES)		\
206 			cv_broadcast(&locked_queue_cv);			\
207 		mutex_exit(&lfs_lock);					\
208 	}								\
209 	(bp)->b_flags &= ~B_LOCKED;					\
210 } while (0)
211 
212 #ifdef _KERNEL
213 
214 extern u_long bufmem_lowater, bufmem_hiwater; /* XXX */
215 
216 # define LFS_IS_MALLOC_BUF(bp) ((bp)->b_iodone == lfs_callback)
217 
218 # ifdef DEBUG
219 #  define LFS_DEBUG_COUNTLOCKED(m) do {					\
220 	if (lfs_debug_log_subsys[DLOG_LLIST]) {				\
221 		lfs_countlocked(&locked_queue_count, &locked_queue_bytes, (m)); \
222 		cv_broadcast(&locked_queue_cv);				\
223 	}								\
224 } while (0)
225 # else
226 #  define LFS_DEBUG_COUNTLOCKED(m)
227 # endif
228 
229 /* log for debugging writes to the Ifile */
230 # ifdef DEBUG
231 struct lfs_log_entry {
232 	const char *op;
233 	const char *file;
234 	int pid;
235 	int line;
236 	daddr_t block;
237 	unsigned long flags;
238 };
239 extern int lfs_lognum;
240 extern struct lfs_log_entry lfs_log[LFS_LOGLENGTH];
241 #  define LFS_BWRITE_LOG(bp) lfs_bwrite_log((bp), __FILE__, __LINE__)
242 #  define LFS_ENTER_LOG(theop, thefile, theline, lbn, theflags, thepid) do {\
243 	int _s;								\
244 									\
245 	mutex_enter(&lfs_lock);						\
246 	_s = splbio();							\
247 	lfs_log[lfs_lognum].op = theop;					\
248 	lfs_log[lfs_lognum].file = thefile;				\
249 	lfs_log[lfs_lognum].line = (theline);				\
250 	lfs_log[lfs_lognum].pid = (thepid);				\
251 	lfs_log[lfs_lognum].block = (lbn);				\
252 	lfs_log[lfs_lognum].flags = (theflags);				\
253 	lfs_lognum = (lfs_lognum + 1) % LFS_LOGLENGTH;			\
254 	splx(_s);							\
255 	mutex_exit(&lfs_lock);						\
256 } while (0)
257 
258 #  define LFS_BCLEAN_LOG(fs, bp) do {					\
259 	if ((bp)->b_vp == (fs)->lfs_ivnode)				\
260 		LFS_ENTER_LOG("clear", __FILE__, __LINE__,		\
261 			      bp->b_lblkno, bp->b_flags, curproc->p_pid);\
262 } while (0)
263 
264 /* Must match list in lfs_vfsops.c ! */
265 #  define DLOG_RF     0  /* roll forward */
266 #  define DLOG_ALLOC  1  /* inode alloc */
267 #  define DLOG_AVAIL  2  /* lfs_{,r,f}avail */
268 #  define DLOG_FLUSH  3  /* flush */
269 #  define DLOG_LLIST  4  /* locked list accounting */
270 #  define DLOG_WVNODE 5  /* vflush/writevnodes verbose */
271 #  define DLOG_VNODE  6  /* vflush/writevnodes */
272 #  define DLOG_SEG    7  /* segwrite */
273 #  define DLOG_SU     8  /* seguse accounting */
274 #  define DLOG_CLEAN  9  /* cleaner routines */
275 #  define DLOG_MOUNT  10 /* mount/unmount */
276 #  define DLOG_PAGE   11 /* putpages/gop_write */
277 #  define DLOG_DIROP  12 /* dirop accounting */
278 #  define DLOG_MALLOC 13 /* lfs_malloc accounting */
279 #  define DLOG_MAX    14 /* The terminator */
280 #  define DLOG(a) lfs_debug_log a
281 # else /* ! DEBUG */
282 #  define LFS_BCLEAN_LOG(fs, bp)
283 #  define LFS_BWRITE_LOG(bp)		VOP_BWRITE((bp))
284 #  define DLOG(a)
285 # endif /* ! DEBUG */
286 #else /* ! _KERNEL */
287 # define LFS_BWRITE_LOG(bp)		VOP_BWRITE((bp))
288 #endif /* _KERNEL */
289 
290 #ifdef _KERNEL
291 /* Filehandle structure for exported LFSes */
292 struct lfid {
293 	struct ufid lfid_ufid;
294 #define lfid_len lfid_ufid.ufid_len
295 #define lfid_ino lfid_ufid.ufid_ino
296 #define lfid_gen lfid_ufid.ufid_gen
297 	uint32_t lfid_ident;
298 };
299 #endif /* _KERNEL */
300 
301 /*
302  * "struct inode" associated definitions
303  */
304 
305 /* Address calculations for metadata located in the inode */
306 #define	S_INDIR(fs)	-NDADDR
307 #define	D_INDIR(fs)	(S_INDIR(fs) - NINDIR(fs) - 1)
308 #define	T_INDIR(fs)	(D_INDIR(fs) - NINDIR(fs) * NINDIR(fs) - 1)
309 
310 /* For convenience */
311 #define IN_ALLMOD (IN_MODIFIED|IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY|IN_ACCESSED|IN_CLEANING)
312 
313 #define LFS_SET_UINO(ip, flags) do {					\
314 	if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED))	\
315 		++(ip)->i_lfs->lfs_uinodes;				\
316 	if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING))	\
317 		++(ip)->i_lfs->lfs_uinodes;				\
318 	if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED))	\
319 		++(ip)->i_lfs->lfs_uinodes;				\
320 	(ip)->i_flag |= (flags);					\
321 } while (0)
322 
323 #define LFS_CLR_UINO(ip, flags) do {					\
324 	if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED))	\
325 		--(ip)->i_lfs->lfs_uinodes;				\
326 	if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING))	\
327 		--(ip)->i_lfs->lfs_uinodes;				\
328 	if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED))	\
329 		--(ip)->i_lfs->lfs_uinodes;				\
330 	(ip)->i_flag &= ~(flags);					\
331 	if ((ip)->i_lfs->lfs_uinodes < 0) {				\
332 		panic("lfs_uinodes < 0");				\
333 	}								\
334 } while (0)
335 
336 #define LFS_ITIMES(ip, acc, mod, cre) \
337 	while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
338 		lfs_itimes(ip, acc, mod, cre)
339 
340 /*
341  * "struct vnode" associated definitions
342  */
343 
344 /* Heuristic emptiness measure */
345 #define VPISEMPTY(vp)	 (LIST_EMPTY(&(vp)->v_dirtyblkhd) && 		\
346 			  !(vp->v_type == VREG && (vp)->v_iflag & VI_ONWORKLST) &&\
347 			  VTOI(vp)->i_lfs_nbtree == 0)
348 
349 #define WRITEINPROG(vp) ((vp)->v_numoutput > 0 ||			\
350 	(!LIST_EMPTY(&(vp)->v_dirtyblkhd) &&				\
351 	 !(VTOI(vp)->i_flag & (IN_MODIFIED | IN_ACCESSED | IN_CLEANING))))
352 
353 
354 /*
355  * On-disk and in-memory checkpoint segment usage structure.
356  */
357 typedef struct segusage SEGUSE;
358 struct segusage {
359 	u_int32_t su_nbytes;		/* 0: number of live bytes */
360 	u_int32_t su_olastmod;		/* 4: SEGUSE last modified timestamp */
361 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
362 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
363 
364 #define	SEGUSE_ACTIVE		0x01	/*  segment currently being written */
365 #define	SEGUSE_DIRTY		0x02	/*  segment has data in it */
366 #define	SEGUSE_SUPERBLOCK	0x04	/*  segment contains a superblock */
367 #define SEGUSE_ERROR		0x08	/*  cleaner: do not clean segment */
368 #define SEGUSE_EMPTY		0x10	/*  segment is empty */
369 #define SEGUSE_INVAL		0x20	/*  segment is invalid */
370 	u_int32_t su_flags;		/* 12: segment flags */
371 	u_int64_t su_lastmod;		/* 16: last modified timestamp */
372 };
373 
374 typedef struct segusage_v1 SEGUSE_V1;
375 struct segusage_v1 {
376 	u_int32_t su_nbytes;		/* 0: number of live bytes */
377 	u_int32_t su_lastmod;		/* 4: SEGUSE last modified timestamp */
378 	u_int16_t su_nsums;		/* 8: number of summaries in segment */
379 	u_int16_t su_ninos;		/* 10: number of inode blocks in seg */
380 	u_int32_t su_flags;		/* 12: segment flags  */
381 };
382 
383 #define	SEGUPB(fs)	(fs->lfs_sepb)
384 #define	SEGTABSIZE_SU(fs)						\
385 	(((fs)->lfs_nseg + SEGUPB(fs) - 1) / (fs)->lfs_sepb)
386 
387 #ifdef _KERNEL
388 # define SHARE_IFLOCK(F) 						\
389   do {									\
390 	rw_enter(&(F)->lfs_iflock, RW_READER);				\
391   } while(0)
392 # define UNSHARE_IFLOCK(F)						\
393   do {									\
394 	rw_exit(&(F)->lfs_iflock);					\
395   } while(0)
396 #else /* ! _KERNEL */
397 # define SHARE_IFLOCK(F)
398 # define UNSHARE_IFLOCK(F)
399 #endif /* ! _KERNEL */
400 
401 /* Read in the block with a specific segment usage entry from the ifile. */
402 #define	LFS_SEGENTRY(SP, F, IN, BP) do {				\
403 	int _e;								\
404 	SHARE_IFLOCK(F);						\
405 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
406 	if ((_e = bread((F)->lfs_ivnode,				\
407 	    ((IN) / (F)->lfs_sepb) + (F)->lfs_cleansz,			\
408 	    (F)->lfs_bsize, NOCRED, 0, &(BP))) != 0)			\
409 		panic("lfs: ifile read: %d", _e);			\
410 	if ((F)->lfs_version == 1)					\
411 		(SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data +		\
412 			((IN) & ((F)->lfs_sepb - 1)));			\
413 	else								\
414 		(SP) = (SEGUSE *)(BP)->b_data + ((IN) % (F)->lfs_sepb);	\
415 	UNSHARE_IFLOCK(F);						\
416 } while (0)
417 
418 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do {				\
419 	if ((SP)->su_nbytes == 0)					\
420 		(SP)->su_flags |= SEGUSE_EMPTY;				\
421 	else								\
422 		(SP)->su_flags &= ~SEGUSE_EMPTY;			\
423 	(F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags;	\
424 	LFS_BWRITE_LOG(BP);						\
425 } while (0)
426 
427 /*
428  * On-disk file information.  One per file with data blocks in the segment.
429  */
430 typedef struct finfo FINFO;
431 struct finfo {
432 	u_int32_t fi_nblocks;		/* number of blocks */
433 	u_int32_t fi_version;		/* version number */
434 	u_int32_t fi_ino;		/* inode number */
435 	u_int32_t fi_lastlength;	/* length of last block in array */
436 	int32_t	  fi_blocks[1];		/* array of logical block numbers */
437 };
438 /* sizeof FINFO except fi_blocks */
439 #define	FINFOSIZE	(sizeof(FINFO) - sizeof(int32_t))
440 
441 /*
442  * Index file inode entries.
443  */
444 typedef struct ifile IFILE;
445 struct ifile {
446 	u_int32_t if_version;		/* inode version number */
447 #define	LFS_UNUSED_DADDR	0	/* out-of-band daddr */
448 	int32_t	  if_daddr;		/* inode disk address */
449 #define LFS_ORPHAN_NEXTFREE	(~(u_int32_t)0) /* indicate orphaned file */
450 	u_int32_t if_nextfree;		/* next-unallocated inode */
451 	u_int32_t if_atime_sec;		/* Last access time, seconds */
452 	u_int32_t if_atime_nsec;	/* and nanoseconds */
453 };
454 
455 typedef struct ifile_v1 IFILE_V1;
456 struct ifile_v1 {
457 	u_int32_t if_version;		/* inode version number */
458 	int32_t	  if_daddr;		/* inode disk address */
459 	u_int32_t if_nextfree;		/* next-unallocated inode */
460 #if LFS_ATIME_IFILE
461 	struct timespec if_atime;	/* Last access time */
462 #endif
463 };
464 
465 /*
466  * LFSv1 compatibility code is not allowed to touch if_atime, since it
467  * may not be mapped!
468  */
469 /* Read in the block with a specific inode from the ifile. */
470 #define	LFS_IENTRY(IP, F, IN, BP) do {					\
471 	int _e;								\
472 	SHARE_IFLOCK(F);						\
473 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
474 	if ((_e = bread((F)->lfs_ivnode,				\
475 	(IN) / (F)->lfs_ifpb + (F)->lfs_cleansz + (F)->lfs_segtabsz,	\
476 	(F)->lfs_bsize, NOCRED, 0, &(BP))) != 0)			\
477 		panic("lfs: ifile ino %d read %d", (int)(IN), _e);	\
478 	if ((F)->lfs_version == 1)					\
479 		(IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data +		\
480 				 (IN) % (F)->lfs_ifpb);			\
481 	else								\
482 		(IP) = (IFILE *)(BP)->b_data + (IN) % (F)->lfs_ifpb;	\
483 	UNSHARE_IFLOCK(F);						\
484 } while (0)
485 
486 /*
487  * Cleaner information structure.  This resides in the ifile and is used
488  * to pass information from the kernel to the cleaner.
489  */
490 typedef struct _cleanerinfo {
491 	u_int32_t clean;		/* number of clean segments */
492 	u_int32_t dirty;		/* number of dirty segments */
493 	int32_t   bfree;		/* disk blocks free */
494 	int32_t	  avail;		/* disk blocks available */
495 	u_int32_t free_head;		/* head of the inode free list */
496 	u_int32_t free_tail;		/* tail of the inode free list */
497 #define LFS_CLEANER_MUST_CLEAN	0x01
498 	u_int32_t flags;		/* status word from the kernel */
499 } CLEANERINFO;
500 
501 #define	CLEANSIZE_SU(fs)						\
502 	((sizeof(CLEANERINFO) + (fs)->lfs_bsize - 1) >> (fs)->lfs_bshift)
503 
504 /* Read in the block with the cleaner info from the ifile. */
505 #define LFS_CLEANERINFO(CP, F, BP) do {					\
506 	SHARE_IFLOCK(F);						\
507 	VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS;			\
508 	if (bread((F)->lfs_ivnode,					\
509 	    (daddr_t)0, (F)->lfs_bsize, NOCRED, 0, &(BP)))		\
510 		panic("lfs: ifile read");				\
511 	(CP) = (CLEANERINFO *)(BP)->b_data;				\
512 	UNSHARE_IFLOCK(F);						\
513 } while (0)
514 
515 /*
516  * Synchronize the Ifile cleaner info with current avail and bfree.
517  */
518 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do {		 	\
519     mutex_enter(&lfs_lock);						\
520     if ((w) || (cip)->bfree != (fs)->lfs_bfree ||		 	\
521 	(cip)->avail != (fs)->lfs_avail - (fs)->lfs_ravail - 		\
522 	(fs)->lfs_favail) {	 					\
523 	(cip)->bfree = (fs)->lfs_bfree;				 	\
524 	(cip)->avail = (fs)->lfs_avail - (fs)->lfs_ravail -		\
525 		(fs)->lfs_favail;				 	\
526 	if (((bp)->b_flags & B_GATHERED) == 0) {		 	\
527 		(fs)->lfs_flags |= LFS_IFDIRTY;			 	\
528 	}								\
529 	mutex_exit(&lfs_lock);						\
530 	(void) LFS_BWRITE_LOG(bp); /* Ifile */			 	\
531     } else {							 	\
532 	mutex_exit(&lfs_lock);						\
533 	brelse(bp, 0);						 	\
534     }									\
535 } while (0)
536 
537 /*
538  * Get the head of the inode free list.
539  * Always called with the segment lock held.
540  */
541 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do {			\
542 	if ((FS)->lfs_version > 1) {					\
543 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
544 		(FS)->lfs_freehd = (CIP)->free_head;			\
545 		brelse(BP, 0);						\
546 	}								\
547 	*(FREEP) = (FS)->lfs_freehd;					\
548 } while (0)
549 
550 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do {				\
551 	(FS)->lfs_freehd = (VAL);					\
552 	if ((FS)->lfs_version > 1) {					\
553 		LFS_CLEANERINFO((CIP), (FS), (BP));			\
554 		(CIP)->free_head = (VAL);				\
555 		LFS_BWRITE_LOG(BP);					\
556 		mutex_enter(&lfs_lock);					\
557 		(FS)->lfs_flags |= LFS_IFDIRTY;				\
558 		mutex_exit(&lfs_lock);					\
559 	}								\
560 } while (0)
561 
562 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do {			\
563 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
564 	*(FREEP) = (CIP)->free_tail;					\
565 	brelse(BP, 0);							\
566 } while (0)
567 
568 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do {				\
569 	LFS_CLEANERINFO((CIP), (FS), (BP));				\
570 	(CIP)->free_tail = (VAL);					\
571 	LFS_BWRITE_LOG(BP);						\
572 	mutex_enter(&lfs_lock);						\
573 	(FS)->lfs_flags |= LFS_IFDIRTY;					\
574 	mutex_exit(&lfs_lock);						\
575 } while (0)
576 
577 /*
578  * On-disk segment summary information
579  */
580 typedef struct segsum_v1 SEGSUM_V1;
581 struct segsum_v1 {
582 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
583 	u_int32_t ss_datasum;		/* 4: check sum of data */
584 	u_int32_t ss_magic;		/* 8: segment summary magic number */
585 #define SS_MAGIC	0x061561
586 	int32_t	  ss_next;		/* 12: next segment */
587 	u_int32_t ss_create;		/* 16: creation time stamp */
588 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
589 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
590 
591 #define	SS_DIROP	0x01		/* segment begins a dirop */
592 #define	SS_CONT		0x02		/* more partials to finish this write*/
593 #define	SS_CLEAN	0x04		/* written by the cleaner */
594 #define	SS_RFW		0x08		/* written by the roll-forward agent */
595 	u_int16_t ss_flags;		/* 24: used for directory operations */
596 	u_int16_t ss_pad;		/* 26: extra space */
597 	/* FINFO's and inode daddr's... */
598 };
599 
600 typedef struct segsum SEGSUM;
601 struct segsum {
602 	u_int32_t ss_sumsum;		/* 0: check sum of summary block */
603 	u_int32_t ss_datasum;		/* 4: check sum of data */
604 	u_int32_t ss_magic;		/* 8: segment summary magic number */
605 	int32_t	  ss_next;		/* 12: next segment */
606 	u_int32_t ss_ident;		/* 16: roll-forward fsid */
607 #define ss_ocreate ss_ident /* ident is where create was in v1 */
608 	u_int16_t ss_nfinfo;		/* 20: number of file info structures */
609 	u_int16_t ss_ninos;		/* 22: number of inodes in summary */
610 	u_int16_t ss_flags;		/* 24: used for directory operations */
611 	u_int8_t  ss_pad[6];		/* 26: extra space */
612 	u_int64_t ss_serial;		/* 32: serial number */
613 	u_int64_t ss_create;		/* 40: time stamp */
614 	/* FINFO's and inode daddr's... */
615 };
616 
617 #define SEGSUM_SIZE(fs) ((fs)->lfs_version == 1 ? sizeof(SEGSUM_V1) : sizeof(SEGSUM))
618 
619 
620 /*
621  * On-disk super block.
622  */
623 struct dlfs {
624 #define	       LFS_MAGIC       0x070162
625 	u_int32_t dlfs_magic;	  /* 0: magic number */
626 #define	       LFS_VERSION     2
627 	u_int32_t dlfs_version;	  /* 4: version number */
628 
629 	u_int32_t dlfs_size;	  /* 8: number of blocks in fs (v1) */
630 				  /*	number of frags in fs (v2) */
631 	u_int32_t dlfs_ssize;	  /* 12: number of blocks per segment (v1) */
632 				  /*	 number of bytes per segment (v2) */
633 	u_int32_t dlfs_dsize;	  /* 16: number of disk blocks in fs */
634 	u_int32_t dlfs_bsize;	  /* 20: file system block size */
635 	u_int32_t dlfs_fsize;	  /* 24: size of frag blocks in fs */
636 	u_int32_t dlfs_frag;	  /* 28: number of frags in a block in fs */
637 
638 /* Checkpoint region. */
639 	u_int32_t dlfs_freehd;	  /* 32: start of the free list */
640 	int32_t   dlfs_bfree;	  /* 36: number of free disk blocks */
641 	u_int32_t dlfs_nfiles;	  /* 40: number of allocated inodes */
642 	int32_t	  dlfs_avail;	  /* 44: blocks available for writing */
643 	int32_t	  dlfs_uinodes;	  /* 48: inodes in cache not yet on disk */
644 	int32_t	  dlfs_idaddr;	  /* 52: inode file disk address */
645 	u_int32_t dlfs_ifile;	  /* 56: inode file inode number */
646 	int32_t	  dlfs_lastseg;	  /* 60: address of last segment written */
647 	int32_t	  dlfs_nextseg;	  /* 64: address of next segment to write */
648 	int32_t	  dlfs_curseg;	  /* 68: current segment being written */
649 	int32_t	  dlfs_offset;	  /* 72: offset in curseg for next partial */
650 	int32_t	  dlfs_lastpseg;  /* 76: address of last partial written */
651 	u_int32_t dlfs_inopf;	  /* 80: v1: time stamp; v2: inodes per frag */
652 #define dlfs_otstamp dlfs_inopf
653 
654 /* These are configuration parameters. */
655 	u_int32_t dlfs_minfree;	  /* 84: minimum percentage of free blocks */
656 
657 /* These fields can be computed from the others. */
658 	u_int64_t dlfs_maxfilesize; /* 88: maximum representable file size */
659 	u_int32_t dlfs_fsbpseg;	    /* 96: fsb per segment */
660 	u_int32_t dlfs_inopb;	  /* 100: inodes per block */
661 	u_int32_t dlfs_ifpb;	  /* 104: IFILE entries per block */
662 	u_int32_t dlfs_sepb;	  /* 108: SEGUSE entries per block */
663 	u_int32_t dlfs_nindir;	  /* 112: indirect pointers per block */
664 	u_int32_t dlfs_nseg;	  /* 116: number of segments */
665 	u_int32_t dlfs_nspf;	  /* 120: number of sectors per fragment */
666 	u_int32_t dlfs_cleansz;	  /* 124: cleaner info size in blocks */
667 	u_int32_t dlfs_segtabsz;  /* 128: segment table size in blocks */
668 	u_int32_t dlfs_segmask;	  /* 132: calculate offset within a segment */
669 	u_int32_t dlfs_segshift;  /* 136: fast mult/div for segments */
670 	u_int32_t dlfs_bshift;	  /* 140: calc block number from file offset */
671 	u_int32_t dlfs_ffshift;	  /* 144: fast mult/div for frag from file */
672 	u_int32_t dlfs_fbshift;	  /* 148: fast mult/div for frag from block */
673 	u_int64_t dlfs_bmask;	  /* 152: calc block offset from file offset */
674 	u_int64_t dlfs_ffmask;	  /* 160: calc frag offset from file offset */
675 	u_int64_t dlfs_fbmask;	  /* 168: calc frag offset from block offset */
676 	u_int32_t dlfs_blktodb;	  /* 176: blktodb and dbtoblk shift constant */
677 	u_int32_t dlfs_sushift;	  /* 180: fast mult/div for segusage table */
678 
679 	int32_t	  dlfs_maxsymlinklen; /* 184: max length of an internal symlink */
680 #define LFS_MIN_SBINTERVAL     5  /* minimum superblock segment spacing */
681 #define LFS_MAXNUMSB	       10 /* 188: superblock disk offsets */
682 	int32_t	   dlfs_sboffs[LFS_MAXNUMSB];
683 
684 	u_int32_t dlfs_nclean;	  /* 228: Number of clean segments */
685 	u_char	  dlfs_fsmnt[MNAMELEN];	 /* 232: name mounted on */
686 #define LFS_PF_CLEAN 0x1
687 	u_int16_t dlfs_pflags;	  /* 322: file system persistent flags */
688 	int32_t	  dlfs_dmeta;	  /* 324: total number of dirty summaries */
689 	u_int32_t dlfs_minfreeseg; /* 328: segments not counted in bfree */
690 	u_int32_t dlfs_sumsize;	  /* 332: size of summary blocks */
691 	u_int64_t dlfs_serial;	  /* 336: serial number */
692 	u_int32_t dlfs_ibsize;	  /* 344: size of inode blocks */
693 	int32_t	  dlfs_start;	  /* 348: start of segment 0 */
694 	u_int64_t dlfs_tstamp;	  /* 352: time stamp */
695 #define LFS_44INODEFMT 0
696 #define LFS_MAXINODEFMT 0
697 	u_int32_t dlfs_inodefmt;  /* 360: inode format version */
698 	u_int32_t dlfs_interleave; /* 364: segment interleave */
699 	u_int32_t dlfs_ident;	  /* 368: per-fs identifier */
700 	u_int32_t dlfs_fsbtodb;	  /* 372: fsbtodb abd dbtodsb shift constant */
701 	u_int32_t dlfs_resvseg;   /* 376: segments reserved for the cleaner */
702 	int8_t	  dlfs_pad[128];  /* 380: round to 512 bytes */
703 /* Checksum -- last valid disk field. */
704 	u_int32_t dlfs_cksum;	  /* 508: checksum for superblock checking */
705 };
706 
707 /* Type used for the inode bitmap */
708 typedef u_int32_t lfs_bm_t;
709 
710 /*
711  * Linked list of segments whose byte count needs updating following a
712  * file truncation.
713  */
714 struct segdelta {
715 	long segnum;
716 	size_t num;
717 	LIST_ENTRY(segdelta) list;
718 };
719 
720 /*
721  * In-memory super block.
722  */
723 struct lfs {
724 	struct dlfs lfs_dlfs;		/* on-disk parameters */
725 #define lfs_magic lfs_dlfs.dlfs_magic
726 #define lfs_version lfs_dlfs.dlfs_version
727 #define lfs_size lfs_dlfs.dlfs_size
728 #define lfs_ssize lfs_dlfs.dlfs_ssize
729 #define lfs_dsize lfs_dlfs.dlfs_dsize
730 #define lfs_bsize lfs_dlfs.dlfs_bsize
731 #define lfs_fsize lfs_dlfs.dlfs_fsize
732 #define lfs_frag lfs_dlfs.dlfs_frag
733 #define lfs_freehd lfs_dlfs.dlfs_freehd
734 #define lfs_bfree lfs_dlfs.dlfs_bfree
735 #define lfs_nfiles lfs_dlfs.dlfs_nfiles
736 #define lfs_avail lfs_dlfs.dlfs_avail
737 #define lfs_uinodes lfs_dlfs.dlfs_uinodes
738 #define lfs_idaddr lfs_dlfs.dlfs_idaddr
739 #define lfs_ifile lfs_dlfs.dlfs_ifile
740 #define lfs_lastseg lfs_dlfs.dlfs_lastseg
741 #define lfs_nextseg lfs_dlfs.dlfs_nextseg
742 #define lfs_curseg lfs_dlfs.dlfs_curseg
743 #define lfs_offset lfs_dlfs.dlfs_offset
744 #define lfs_lastpseg lfs_dlfs.dlfs_lastpseg
745 #define lfs_otstamp lfs_dlfs.dlfs_inopf
746 #define lfs_inopf lfs_dlfs.dlfs_inopf
747 #define lfs_minfree lfs_dlfs.dlfs_minfree
748 #define lfs_maxfilesize lfs_dlfs.dlfs_maxfilesize
749 #define lfs_fsbpseg lfs_dlfs.dlfs_fsbpseg
750 #define lfs_inopb lfs_dlfs.dlfs_inopb
751 #define lfs_ifpb lfs_dlfs.dlfs_ifpb
752 #define lfs_sepb lfs_dlfs.dlfs_sepb
753 #define lfs_nindir lfs_dlfs.dlfs_nindir
754 #define lfs_nseg lfs_dlfs.dlfs_nseg
755 #define lfs_nspf lfs_dlfs.dlfs_nspf
756 #define lfs_cleansz lfs_dlfs.dlfs_cleansz
757 #define lfs_segtabsz lfs_dlfs.dlfs_segtabsz
758 #define lfs_segmask lfs_dlfs.dlfs_segmask
759 #define lfs_segshift lfs_dlfs.dlfs_segshift
760 #define lfs_bmask lfs_dlfs.dlfs_bmask
761 #define lfs_bshift lfs_dlfs.dlfs_bshift
762 #define lfs_ffmask lfs_dlfs.dlfs_ffmask
763 #define lfs_ffshift lfs_dlfs.dlfs_ffshift
764 #define lfs_fbmask lfs_dlfs.dlfs_fbmask
765 #define lfs_fbshift lfs_dlfs.dlfs_fbshift
766 #define lfs_blktodb lfs_dlfs.dlfs_blktodb
767 #define lfs_fsbtodb lfs_dlfs.dlfs_fsbtodb
768 #define lfs_sushift lfs_dlfs.dlfs_sushift
769 #define lfs_maxsymlinklen lfs_dlfs.dlfs_maxsymlinklen
770 #define lfs_sboffs lfs_dlfs.dlfs_sboffs
771 #define lfs_cksum lfs_dlfs.dlfs_cksum
772 #define lfs_pflags lfs_dlfs.dlfs_pflags
773 #define lfs_fsmnt lfs_dlfs.dlfs_fsmnt
774 #define lfs_nclean lfs_dlfs.dlfs_nclean
775 #define lfs_dmeta lfs_dlfs.dlfs_dmeta
776 #define lfs_minfreeseg lfs_dlfs.dlfs_minfreeseg
777 #define lfs_sumsize lfs_dlfs.dlfs_sumsize
778 #define lfs_serial lfs_dlfs.dlfs_serial
779 #define lfs_ibsize lfs_dlfs.dlfs_ibsize
780 #define lfs_start lfs_dlfs.dlfs_start
781 #define lfs_tstamp lfs_dlfs.dlfs_tstamp
782 #define lfs_inodefmt lfs_dlfs.dlfs_inodefmt
783 #define lfs_interleave lfs_dlfs.dlfs_interleave
784 #define lfs_ident lfs_dlfs.dlfs_ident
785 #define lfs_resvseg lfs_dlfs.dlfs_resvseg
786 
787 /* These fields are set at mount time and are meaningless on disk. */
788 	struct segment *lfs_sp;		/* current segment being written */
789 	struct vnode *lfs_ivnode;	/* vnode for the ifile */
790 	u_int32_t  lfs_seglock;		/* single-thread the segment writer */
791 	pid_t	  lfs_lockpid;		/* pid of lock holder */
792 	lwpid_t	  lfs_locklwp;		/* lwp of lock holder */
793 	u_int32_t lfs_iocount;		/* number of ios pending */
794 	u_int32_t lfs_writer;		/* don't allow any dirops to start */
795 	u_int32_t lfs_dirops;		/* count of active directory ops */
796 	u_int32_t lfs_dirvcount;	/* count of VDIROP nodes in this fs */
797 	u_int32_t lfs_doifile;		/* Write ifile blocks on next write */
798 	u_int32_t lfs_nactive;		/* Number of segments since last ckp */
799 	int8_t	  lfs_fmod;		/* super block modified flag */
800 	int8_t	  lfs_ronly;		/* mounted read-only flag */
801 #define LFS_NOTYET  0x01
802 #define LFS_IFDIRTY 0x02
803 #define LFS_WARNED  0x04
804 #define LFS_UNDIROP 0x08
805 	int8_t	  lfs_flags;		/* currently unused flag */
806 	u_int16_t lfs_activesb;		/* toggle between superblocks */
807 	daddr_t	  lfs_sbactive;		/* disk address of current sb write */
808 	struct vnode *lfs_flushvp;	/* vnode being flushed */
809 	int lfs_flushvp_fakevref;	/* fake vref count for flushvp */
810 	struct vnode *lfs_unlockvp;	/* being inactivated in lfs_segunlock */
811 	u_int32_t lfs_diropwait;	/* # procs waiting on dirop flush */
812 	size_t lfs_devbsize;		/* Device block size */
813 	size_t lfs_devbshift;		/* Device block shift */
814 	krwlock_t lfs_fraglock;
815 	krwlock_t lfs_iflock;		/* Ifile lock */
816 	kcondvar_t lfs_stopcv;		/* Wrap lock */
817 	struct lwp *lfs_stoplwp;
818 	pid_t lfs_rfpid;		/* Process ID of roll-forward agent */
819 	int	  lfs_nadirop;		/* number of active dirop nodes */
820 	long	  lfs_ravail;		/* blocks pre-reserved for writing */
821 	long	  lfs_favail;		/* blocks pre-reserved for writing */
822 	res_t *lfs_resblk;		/* Reserved memory for pageout */
823 	TAILQ_HEAD(, inode) lfs_dchainhd; /* dirop vnodes */
824 	TAILQ_HEAD(, inode) lfs_pchainhd; /* paging vnodes */
825 #define LFS_RESHASH_WIDTH 17
826 	LIST_HEAD(, lfs_res_blk) lfs_reshash[LFS_RESHASH_WIDTH];
827 	int	  lfs_pdflush;		 /* pagedaemon wants us to flush */
828 	u_int32_t **lfs_suflags;	/* Segment use flags */
829 #ifdef _KERNEL
830 	struct pool lfs_clpool;		/* Pool for struct lfs_cluster */
831 	struct pool lfs_bpppool;	/* Pool for bpp */
832 	struct pool lfs_segpool;	/* Pool for struct segment */
833 #endif /* _KERNEL */
834 #define LFS_MAX_CLEANIND 64
835 	int32_t  lfs_cleanint[LFS_MAX_CLEANIND]; /* Active cleaning intervals */
836 	int 	 lfs_cleanind;		/* Index into intervals */
837 	int lfs_sleepers;		/* # procs sleeping this fs */
838 	int lfs_pages;			/* dirty pages blaming this fs */
839 	lfs_bm_t *lfs_ino_bitmap;	/* Inuse inodes bitmap */
840 	int lfs_nowrap;			/* Suspend log wrap */
841 	int lfs_wrappass;		/* Allow first log wrap requester to pass */
842 	int lfs_wrapstatus;		/* Wrap status */
843 	LIST_HEAD(, segdelta) lfs_segdhd;	/* List of pending trunc accounting events */
844 };
845 
846 /* NINDIR is the number of indirects in a file system block. */
847 #define	NINDIR(fs)	((fs)->lfs_nindir)
848 
849 /* INOPB is the number of inodes in a secondary storage block. */
850 #define	INOPB(fs)	((fs)->lfs_inopb)
851 /* INOPF is the number of inodes in a fragment. */
852 #define INOPF(fs)	((fs)->lfs_inopf)
853 
854 #define	blksize(fs, ip, lbn) \
855 	(((lbn) >= NDADDR || (ip)->i_ffs1_size >= ((lbn) + 1) << (fs)->lfs_bshift) \
856 	    ? (fs)->lfs_bsize \
857 	    : (fragroundup(fs, blkoff(fs, (ip)->i_ffs1_size))))
858 #define	blkoff(fs, loc)		((int)((loc) & (fs)->lfs_bmask))
859 #define fragoff(fs, loc)    /* calculates (loc % fs->lfs_fsize) */ \
860     ((int)((loc) & (fs)->lfs_ffmask))
861 
862 #if defined (_KERNEL)
863 #define	fsbtodb(fs, b)		((b) << ((fs)->lfs_ffshift - DEV_BSHIFT))
864 #define	dbtofsb(fs, b)		((b) >> ((fs)->lfs_ffshift - DEV_BSHIFT))
865 #else
866 #define	fsbtodb(fs, b)		((b) << (fs)->lfs_fsbtodb)
867 #define	dbtofsb(fs, b)		((b) >> (fs)->lfs_fsbtodb)
868 #endif
869 
870 #define	lblkno(fs, loc)		((loc) >> (fs)->lfs_bshift)
871 #define	lblktosize(fs, blk)	((blk) << (fs)->lfs_bshift)
872 
873 #define fsbtob(fs, b)		((b) << (fs)->lfs_ffshift)
874 #define btofsb(fs, b)		((b) >> (fs)->lfs_ffshift)
875 
876 #define numfrags(fs, loc)	/* calculates (loc / fs->lfs_fsize) */	\
877 	((loc) >> (fs)->lfs_ffshift)
878 #define blkroundup(fs, size)	/* calculates roundup(size, fs->lfs_bsize) */ \
879 	((off_t)(((size) + (fs)->lfs_bmask) & (~(fs)->lfs_bmask)))
880 #define fragroundup(fs, size)	/* calculates roundup(size, fs->lfs_fsize) */ \
881 	((off_t)(((size) + (fs)->lfs_ffmask) & (~(fs)->lfs_ffmask)))
882 #define fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
883 	((frags) >> (fs)->lfs_fbshift)
884 #define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
885 	((blks) << (fs)->lfs_fbshift)
886 #define fragnum(fs, fsb)	/* calculates (fsb % fs->lfs_frag) */	\
887 	((fsb) & ((fs)->lfs_frag - 1))
888 #define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->lfs_frag) */ \
889 	((fsb) &~ ((fs)->lfs_frag - 1))
890 #define dblksize(fs, dp, lbn) \
891 	(((lbn) >= NDADDR || (dp)->di_size >= ((lbn) + 1) << (fs)->lfs_bshift)\
892 	    ? (fs)->lfs_bsize \
893 	    : (fragroundup(fs, blkoff(fs, (dp)->di_size))))
894 
895 #define	segsize(fs)	((fs)->lfs_version == 1 ?	     		\
896 			   lblktosize((fs), (fs)->lfs_ssize) :		\
897 			   (fs)->lfs_ssize)
898 #define segtod(fs, seg) (((fs)->lfs_version == 1     ?	     		\
899 			   (fs)->lfs_ssize << (fs)->lfs_blktodb :	\
900 			   btofsb((fs), (fs)->lfs_ssize)) * (seg))
901 #define	dtosn(fs, daddr)	/* block address to segment number */	\
902 	((uint32_t)(((daddr) - (fs)->lfs_start) / segtod((fs), 1)))
903 #define sntod(fs, sn)		/* segment number to disk address */	\
904 	((daddr_t)(segtod((fs), (sn)) + (fs)->lfs_start))
905 
906 /*
907  * Structures used by lfs_bmapv and lfs_markv to communicate information
908  * about inodes and data blocks.
909  */
910 typedef struct block_info {
911 	u_int32_t bi_inode;		/* inode # */
912 	int32_t	bi_lbn;			/* logical block w/in file */
913 	int32_t	bi_daddr;		/* disk address of block */
914 	u_int64_t bi_segcreate;		/* origin segment create time */
915 	int	bi_version;		/* file version number */
916 	void	*bi_bp;			/* data buffer */
917 	int	bi_size;		/* size of the block (if fragment) */
918 } BLOCK_INFO;
919 
920 /* Compatibility for 1.5 binaries */
921 typedef struct block_info_15 {
922 	u_int32_t bi_inode;		/* inode # */
923 	int32_t	bi_lbn;			/* logical block w/in file */
924 	int32_t	bi_daddr;		/* disk address of block */
925 	u_int32_t bi_segcreate;		/* origin segment create time */
926 	int	bi_version;		/* file version number */
927 	void	*bi_bp;			/* data buffer */
928 	int	bi_size;		/* size of the block (if fragment) */
929 } BLOCK_INFO_15;
930 
931 /* In-memory description of a segment about to be written. */
932 struct segment {
933 	struct lfs	 *fs;		/* file system pointer */
934 	struct buf	**bpp;		/* pointer to buffer array */
935 	struct buf	**cbpp;		/* pointer to next available bp */
936 	struct buf	**start_bpp;	/* pointer to first bp in this set */
937 	struct buf	 *ibp;		/* buffer pointer to inode page */
938 	struct ufs1_dinode    *idp;          /* pointer to ifile dinode */
939 	struct finfo	 *fip;		/* current fileinfo pointer */
940 	struct vnode	 *vp;		/* vnode being gathered */
941 	void	 *segsum;		/* segment summary info */
942 	u_int32_t ninodes;		/* number of inodes in this segment */
943 	int32_t seg_bytes_left;		/* bytes left in segment */
944 	int32_t sum_bytes_left;		/* bytes left in summary block */
945 	u_int32_t seg_number;		/* number of this segment */
946 	int32_t *start_lbp;		/* beginning lbn for this set */
947 
948 #define	SEGM_CKP	0x01		/* doing a checkpoint */
949 #define	SEGM_CLEAN	0x02		/* cleaner call; don't sort */
950 #define	SEGM_SYNC	0x04		/* wait for segment */
951 #define	SEGM_PROT	0x08		/* don't inactivate at segunlock */
952 #define SEGM_PAGEDAEMON	0x10		/* pagedaemon called us */
953 #define SEGM_WRITERD	0x20		/* LFS writed called us */
954 #define SEGM_FORCE_CKP	0x40		/* Force checkpoint right away */
955 	u_int16_t seg_flags;		/* run-time flags for this segment */
956 	u_int32_t seg_iocount;		/* number of ios pending */
957 	int	  ndupino;		/* number of duplicate inodes */
958 };
959 
960 #ifdef _KERNEL
961 struct lfs_cluster {
962 	size_t bufsize;	       /* Size of kept data */
963 	struct buf **bpp;      /* Array of kept buffers */
964 	int bufcount;	       /* Number of kept buffers */
965 #define LFS_CL_MALLOC	0x00000001
966 #define LFS_CL_SHIFT	0x00000002
967 #define LFS_CL_SYNC	0x00000004
968 	u_int32_t flags;       /* Flags */
969 	struct lfs *fs;	       /* LFS that this belongs to */
970 	struct segment *seg;   /* Segment structure, for LFS_CL_SYNC */
971 };
972 
973 /*
974  * Splay tree containing block numbers allocated through lfs_balloc.
975  */
976 struct lbnentry {
977 	SPLAY_ENTRY(lbnentry) entry;
978 	daddr_t lbn;
979 };
980 #endif /* _KERNEL */
981 
982 /*
983  * LFS inode extensions.
984  */
985 struct lfs_inode_ext {
986 	off_t	  lfs_osize;		/* size of file on disk */
987 	u_int32_t lfs_effnblocks;  /* number of blocks when i/o completes */
988 	size_t	  lfs_fragsize[NDADDR]; /* size of on-disk direct blocks */
989 	TAILQ_ENTRY(inode) lfs_dchain;  /* Dirop chain. */
990 	TAILQ_ENTRY(inode) lfs_pchain;  /* Paging chain. */
991 #define LFSI_NO_GOP_WRITE 0x01
992 #define LFSI_DELETED      0x02
993 #define LFSI_WRAPBLOCK    0x04
994 #define LFSI_WRAPWAIT     0x08
995 	u_int32_t lfs_iflags;           /* Inode flags */
996 	daddr_t   lfs_hiblk;		/* Highest lbn held by inode */
997 #ifdef _KERNEL
998 	SPLAY_HEAD(lfs_splay, lbnentry) lfs_lbtree; /* Tree of balloc'd lbns */
999 	int	  lfs_nbtree;		/* Size of tree */
1000 	LIST_HEAD(, segdelta) lfs_segdhd;
1001 #endif
1002 	int16_t	  lfs_odnlink;		/* on-disk nlink count for cleaner */
1003 };
1004 #define i_lfs_osize		inode_ext.lfs->lfs_osize
1005 #define i_lfs_effnblks		inode_ext.lfs->lfs_effnblocks
1006 #define i_lfs_fragsize		inode_ext.lfs->lfs_fragsize
1007 #define i_lfs_dchain		inode_ext.lfs->lfs_dchain
1008 #define i_lfs_pchain		inode_ext.lfs->lfs_pchain
1009 #define i_lfs_iflags		inode_ext.lfs->lfs_iflags
1010 #define i_lfs_hiblk		inode_ext.lfs->lfs_hiblk
1011 #define i_lfs_lbtree		inode_ext.lfs->lfs_lbtree
1012 #define i_lfs_nbtree		inode_ext.lfs->lfs_nbtree
1013 #define i_lfs_segdhd		inode_ext.lfs->lfs_segdhd
1014 #define i_lfs_odnlink		inode_ext.lfs->lfs_odnlink
1015 
1016 /*
1017  * Macros for determining free space on the disk, with the variable metadata
1018  * of segment summaries and inode blocks taken into account.
1019  */
1020 /* Estimate number of clean blocks not available for writing */
1021 #define LFS_EST_CMETA(F) (int32_t)((((F)->lfs_dmeta *			     \
1022 				     (int64_t)(F)->lfs_nclean) /	     \
1023 				      ((F)->lfs_nseg - (F)->lfs_nclean)))
1024 
1025 /* Estimate total size of the disk not including metadata */
1026 #define LFS_EST_NONMETA(F) ((F)->lfs_dsize - (F)->lfs_dmeta - LFS_EST_CMETA(F))
1027 
1028 /* Estimate number of blocks actually available for writing */
1029 #define LFS_EST_BFREE(F) ((F)->lfs_bfree > LFS_EST_CMETA(F) ?		     \
1030 			  (F)->lfs_bfree - LFS_EST_CMETA(F) : 0)
1031 
1032 /* Amount of non-meta space not available to mortal man */
1033 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) *			     \
1034 				   (u_int64_t)(F)->lfs_minfree) /	     \
1035 				  100)
1036 
1037 /* Can credential C write BB blocks */
1038 #define ISSPACE(F, BB, C)						\
1039 	((((C) == NOCRED || kauth_cred_geteuid(C) == 0) &&		\
1040 	  LFS_EST_BFREE(F) >= (BB)) ||					\
1041 	 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
1042 
1043 /* Can an ordinary user write BB blocks */
1044 #define IS_FREESPACE(F, BB)						\
1045 	  (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
1046 
1047 /*
1048  * The minimum number of blocks to create a new inode.  This is:
1049  * directory direct block (1) + NIADDR indirect blocks + inode block (1) +
1050  * ifile direct block (1) + NIADDR indirect blocks = 3 + 2 * NIADDR blocks.
1051  */
1052 #define LFS_NRESERVE(F) (btofsb((F), (2 * NIADDR + 3) << (F)->lfs_bshift))
1053 
1054 /* Statistics Counters */
1055 struct lfs_stats {	/* Must match sysctl list in lfs_vfsops.h ! */
1056 	u_int	segsused;
1057 	u_int	psegwrites;
1058 	u_int	psyncwrites;
1059 	u_int	pcleanwrites;
1060 	u_int	blocktot;
1061 	u_int	cleanblocks;
1062 	u_int	ncheckpoints;
1063 	u_int	nwrites;
1064 	u_int	nsync_writes;
1065 	u_int	wait_exceeded;
1066 	u_int	write_exceeded;
1067 	u_int	flush_invoked;
1068 	u_int	vflush_invoked;
1069 	u_int	clean_inlocked;
1070 	u_int	clean_vnlocked;
1071 	u_int   segs_reclaimed;
1072 };
1073 #ifdef _KERNEL
1074 extern struct lfs_stats lfs_stats;
1075 #endif
1076 
1077 /* Fcntls to take the place of the lfs syscalls */
1078 struct lfs_fcntl_markv {
1079 	BLOCK_INFO *blkiov;	/* blocks to relocate */
1080 	int blkcnt;		/* number of blocks */
1081 };
1082 
1083 #define LFCNSEGWAITALL	_FCNR_FSPRIV('L', 14, struct timeval)
1084 #define LFCNSEGWAIT	_FCNR_FSPRIV('L', 15, struct timeval)
1085 #define LFCNBMAPV	_FCNRW_FSPRIV('L', 2, struct lfs_fcntl_markv)
1086 #define LFCNMARKV	_FCNRW_FSPRIV('L', 3, struct lfs_fcntl_markv)
1087 #define LFCNRECLAIM	 _FCNO_FSPRIV('L', 4)
1088 
1089 struct lfs_fhandle {
1090 	char space[28];	/* FHANDLE_SIZE_COMPAT (but used from userland too) */
1091 };
1092 #define LFCNREWIND       _FCNR_FSPRIV('L', 6, int)
1093 #define LFCNINVAL        _FCNR_FSPRIV('L', 7, int)
1094 #define LFCNRESIZE       _FCNR_FSPRIV('L', 8, int)
1095 #define LFCNWRAPSTOP	 _FCNR_FSPRIV('L', 9, int)
1096 #define LFCNWRAPGO	 _FCNR_FSPRIV('L', 10, int)
1097 #define LFCNIFILEFH	 _FCNW_FSPRIV('L', 11, struct lfs_fhandle)
1098 #define LFCNWRAPPASS	 _FCNR_FSPRIV('L', 12, int)
1099 # define LFS_WRAP_GOING   0x0
1100 # define LFS_WRAP_WAITING 0x1
1101 #define LFCNWRAPSTATUS	 _FCNW_FSPRIV('L', 13, int)
1102 
1103 /*
1104  * Compat.  Defined for kernel only.  Userland always uses
1105  * "the one true version".
1106  */
1107 #ifdef _KERNEL
1108 #include <compat/sys/time_types.h>
1109 
1110 #define LFCNSEGWAITALL_COMPAT	 _FCNW_FSPRIV('L', 0, struct timeval50)
1111 #define LFCNSEGWAIT_COMPAT	 _FCNW_FSPRIV('L', 1, struct timeval50)
1112 #define LFCNIFILEFH_COMPAT	 _FCNW_FSPRIV('L', 5, struct lfs_fhandle)
1113 #define LFCNIFILEFH_COMPAT2	 _FCN_FSPRIV(F_FSOUT, 'L', 11, 32)
1114 #define LFCNWRAPSTOP_COMPAT	 _FCNO_FSPRIV('L', 9)
1115 #define LFCNWRAPGO_COMPAT	 _FCNO_FSPRIV('L', 10)
1116 #define LFCNSEGWAITALL_COMPAT_50 _FCNR_FSPRIV('L', 0, struct timeval50)
1117 #define LFCNSEGWAIT_COMPAT_50	 _FCNR_FSPRIV('L', 1, struct timeval50)
1118 #endif
1119 
1120 #ifdef _KERNEL
1121 /* XXX MP */
1122 #define	LFS_SEGLOCK_HELD(fs) \
1123 	((fs)->lfs_seglock != 0 &&					\
1124 	 (fs)->lfs_lockpid == curproc->p_pid &&				\
1125 	 (fs)->lfs_locklwp == curlwp->l_lid)
1126 #endif /* _KERNEL */
1127 
1128 /* Debug segment lock */
1129 #ifdef notyet
1130 # define ASSERT_SEGLOCK(fs) KASSERT(LFS_SEGLOCK_HELD(fs))
1131 # define ASSERT_NO_SEGLOCK(fs) KASSERT(!LFS_SEGLOCK_HELD(fs))
1132 # define ASSERT_DUNNO_SEGLOCK(fs)
1133 # define ASSERT_MAYBE_SEGLOCK(fs)
1134 #else /* !notyet */
1135 # define ASSERT_DUNNO_SEGLOCK(fs) \
1136 	DLOG((DLOG_SEG, "lfs func %s seglock wrong (%d)\n", __func__, \
1137 		LFS_SEGLOCK_HELD(fs)))
1138 # define ASSERT_SEGLOCK(fs) do {					\
1139 	if (!LFS_SEGLOCK_HELD(fs)) {					\
1140 		DLOG((DLOG_SEG, "lfs func %s seglock wrong (0)\n", __func__)); \
1141 	}								\
1142 } while(0)
1143 # define ASSERT_NO_SEGLOCK(fs) do {					\
1144 	if (LFS_SEGLOCK_HELD(fs)) {					\
1145 		DLOG((DLOG_SEG, "lfs func %s seglock wrong (1)\n", __func__)); \
1146 	}								\
1147 } while(0)
1148 # define ASSERT_MAYBE_SEGLOCK(x)
1149 #endif /* !notyet */
1150 
1151 __BEGIN_DECLS
1152 void lfs_itimes(struct inode *, const struct timespec *,
1153     const struct timespec *, const struct timespec *);
1154 __END_DECLS
1155 
1156 #endif /* !_UFS_LFS_LFS_H_ */
1157