xref: /freebsd/sys/ufs/ffs/softdep.h (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 1998, 2000 Marshall Kirk McKusick. All Rights Reserved.
5  *
6  * The soft updates code is derived from the appendix of a University
7  * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
8  * "Soft Updates: A Solution to the Metadata Update Problem in File
9  * Systems", CSE-TR-254-95, August 1995).
10  *
11  * Further information about soft updates can be obtained from:
12  *
13  *	Marshall Kirk McKusick		http://www.mckusick.com/softdep/
14  *	1614 Oxford Street		mckusick@mckusick.com
15  *	Berkeley, CA 94709-1608		+1-510-843-9542
16  *	USA
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  *
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
29  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED.  IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)softdep.h	9.7 (McKusick) 6/21/00
41  */
42 
43 #include <sys/queue.h>
44 
45 /*
46  * Allocation dependencies are handled with undo/redo on the in-memory
47  * copy of the data. A particular data dependency is eliminated when
48  * it is ALLCOMPLETE: that is ATTACHED, DEPCOMPLETE, and COMPLETE.
49  *
50  * The ATTACHED flag means that the data is not currently being written
51  * to disk.
52  *
53  * The UNDONE flag means that the data has been rolled back to a safe
54  * state for writing to the disk. When the I/O completes, the data is
55  * restored to its current form and the state reverts to ATTACHED.
56  * The data must be locked throughout the rollback, I/O, and roll
57  * forward so that the rolled back information is never visible to
58  * user processes.
59  *
60  * The COMPLETE flag indicates that the item has been written. For example,
61  * a dependency that requires that an inode be written will be marked
62  * COMPLETE after the inode has been written to disk.
63  *
64  * The DEPCOMPLETE flag indicates the completion of any other
65  * dependencies such as the writing of a cylinder group map has been
66  * completed. A dependency structure may be freed only when both it
67  * and its dependencies have completed and any rollbacks that are in
68  * progress have finished as indicated by the set of ALLCOMPLETE flags
69  * all being set.
70  *
71  * The two MKDIR flags indicate additional dependencies that must be done
72  * when creating a new directory. MKDIR_BODY is cleared when the directory
73  * data block containing the "." and ".." entries has been written.
74  * MKDIR_PARENT is cleared when the parent inode with the increased link
75  * count for ".." has been written. When both MKDIR flags have been
76  * cleared, the DEPCOMPLETE flag is set to indicate that the directory
77  * dependencies have been completed. The writing of the directory inode
78  * itself sets the COMPLETE flag which then allows the directory entry for
79  * the new directory to be written to disk. The RMDIR flag marks a dirrem
80  * structure as representing the removal of a directory rather than a
81  * file. When the removal dependencies are completed, additional work needs
82  * to be done* (an additional decrement of the associated inode, and a
83  * decrement of the parent inode).
84  *
85  * The DIRCHG flag marks a diradd structure as representing the changing
86  * of an existing entry rather than the addition of a new one. When
87  * the update is complete the dirrem associated with the inode for
88  * the old name must be added to the worklist to do the necessary
89  * reference count decrement.
90  *
91  * The GOINGAWAY flag indicates that the data structure is frozen from
92  * further change until its dependencies have been completed and its
93  * resources freed after which it will be discarded.
94  *
95  * The IOSTARTED flag prevents multiple calls to the I/O start routine from
96  * doing multiple rollbacks.
97  *
98  * The NEWBLOCK flag marks pagedep structures that have just been allocated,
99  * so must be claimed by the inode before all dependencies are complete.
100  *
101  * The INPROGRESS flag marks worklist structures that are still on the
102  * worklist, but are being considered for action by some process.
103  *
104  * The UFS1FMT flag indicates that the inode being processed is a ufs1 format.
105  *
106  * The EXTDATA flag indicates that the allocdirect describes an
107  * extended-attributes dependency.
108  *
109  * The ONWORKLIST flag shows whether the structure is currently linked
110  * onto a worklist.
111  *
112  * The UNLINK* flags track the progress of updating the on-disk linked
113  * list of active but unlinked inodes. When an inode is first unlinked
114  * it is marked as UNLINKED. When its on-disk di_freelink has been
115  * written its UNLINKNEXT flags is set. When its predecessor in the
116  * list has its di_freelink pointing at us its UNLINKPREV is set.
117  * When the on-disk list can reach it from the superblock, its
118  * UNLINKONLIST flag is set. Once all of these flags are set, it
119  * is safe to let its last name be removed.
120  */
121 #define	ATTACHED	0x000001
122 #define	UNDONE		0x000002
123 #define	COMPLETE	0x000004
124 #define	DEPCOMPLETE	0x000008
125 #define	MKDIR_PARENT	0x000010 /* diradd, mkdir, jaddref, jsegdep only */
126 #define	MKDIR_BODY	0x000020 /* diradd, mkdir, jaddref only */
127 #define	RMDIR		0x000040 /* dirrem only */
128 #define	DIRCHG		0x000080 /* diradd, dirrem only */
129 #define	GOINGAWAY	0x000100 /* indirdep, jremref only */
130 #define	IOSTARTED	0x000200 /* inodedep, pagedep, bmsafemap only */
131 #define	DELAYEDFREE	0x000400 /* allocindirect free delayed. */
132 #define	NEWBLOCK	0x000800 /* pagedep, jaddref only */
133 #define	INPROGRESS	0x001000 /* dirrem, freeblks, freefrag, freefile only */
134 #define	UFS1FMT		0x002000 /* indirdep only */
135 #define	EXTDATA		0x004000 /* allocdirect only */
136 #define	ONWORKLIST	0x008000
137 #define	IOWAITING	0x010000 /* Thread is waiting for IO to complete. */
138 #define	ONDEPLIST	0x020000 /* Structure is on a dependency list. */
139 #define	UNLINKED	0x040000 /* inodedep has been unlinked. */
140 #define	UNLINKNEXT	0x080000 /* inodedep has valid di_freelink */
141 #define	UNLINKPREV	0x100000 /* inodedep is pointed at in the unlink list */
142 #define	UNLINKONLIST	0x200000 /* inodedep is in the unlinked list on disk */
143 #define	UNLINKLINKS	(UNLINKNEXT | UNLINKPREV)
144 #define	WRITESUCCEEDED	0x400000 /* the disk write completed successfully */
145 
146 #define	ALLCOMPLETE	(ATTACHED | COMPLETE | DEPCOMPLETE)
147 
148 #define PRINT_SOFTDEP_FLAGS "\20\27writesucceeded\26unlinkonlist" \
149 	"\25unlinkprev\24unlinknext\23unlinked\22ondeplist\21iowaiting" \
150 	"\20onworklist\17extdata\16ufs1fmt\15inprogress\14newblock" \
151 	"\13delayedfree\12iostarted\11goingaway\10dirchg\7rmdir\6mkdir_body" \
152 	"\5mkdir_parent\4depcomplete\3complete\2undone\1attached"
153 
154 /*
155  * Values for each of the soft dependency types.
156  */
157 #define	D_UNUSED	0
158 #define	D_FIRST		D_PAGEDEP
159 #define	D_PAGEDEP	1
160 #define	D_INODEDEP	2
161 #define	D_BMSAFEMAP	3
162 #define	D_NEWBLK	4
163 #define	D_ALLOCDIRECT	5
164 #define	D_INDIRDEP	6
165 #define	D_ALLOCINDIR	7
166 #define	D_FREEFRAG	8
167 #define	D_FREEBLKS	9
168 #define	D_FREEFILE	10
169 #define	D_DIRADD	11
170 #define	D_MKDIR		12
171 #define	D_DIRREM	13
172 #define	D_NEWDIRBLK	14
173 #define	D_FREEWORK	15
174 #define	D_FREEDEP	16
175 #define	D_JADDREF	17
176 #define	D_JREMREF	18
177 #define	D_JMVREF	19
178 #define	D_JNEWBLK	20
179 #define	D_JFREEBLK	21
180 #define	D_JFREEFRAG	22
181 #define	D_JSEG		23
182 #define	D_JSEGDEP	24
183 #define	D_SBDEP		25
184 #define	D_JTRUNC	26
185 #define	D_JFSYNC	27
186 #define	D_SENTINEL	28
187 #define	D_LAST		D_SENTINEL
188 
189 /*
190  * The workitem queue.
191  *
192  * It is sometimes useful and/or necessary to clean up certain dependencies
193  * in the background rather than during execution of an application process
194  * or interrupt service routine. To realize this, we append dependency
195  * structures corresponding to such tasks to a "workitem" queue. In a soft
196  * updates implementation, most pending workitems should not wait for more
197  * than a couple of seconds, so the filesystem syncer process awakens once
198  * per second to process the items on the queue.
199  */
200 
201 /* LIST_HEAD(workhead, worklist);	-- declared in buf.h */
202 
203 /*
204  * Each request can be linked onto a work queue through its worklist structure.
205  * To avoid the need for a pointer to the structure itself, this structure
206  * MUST be declared FIRST in each type in which it appears! If more than one
207  * worklist is needed in the structure, then a wk_data field must be added
208  * and the macros below changed to use it.
209  */
210 struct worklist {
211 	LIST_ENTRY(worklist)	wk_list;	/* list of work requests */
212 	struct mount		*wk_mp;		/* Mount we live in */
213 	unsigned int		wk_type:8,	/* type of request */
214 				wk_state:24;	/* state flags */
215 	LIST_ENTRY(worklist)	wk_all;		/* list of deps of this type */
216 #ifdef INVARIANTS
217 	const char		*wk_func;	/* func where added / removed */
218 	int			wk_line;	/* line where added / removed */
219 #endif
220 };
221 #define	WK_DATA(wk) ((void *)(wk))
222 #define	WK_PAGEDEP(wk) ((struct pagedep *)(wk))
223 #define	WK_INODEDEP(wk) ((struct inodedep *)(wk))
224 #define	WK_BMSAFEMAP(wk) ((struct bmsafemap *)(wk))
225 #define	WK_NEWBLK(wk)  ((struct newblk *)(wk))
226 #define	WK_ALLOCDIRECT(wk) ((struct allocdirect *)(wk))
227 #define	WK_INDIRDEP(wk) ((struct indirdep *)(wk))
228 #define	WK_ALLOCINDIR(wk) ((struct allocindir *)(wk))
229 #define	WK_FREEFRAG(wk) ((struct freefrag *)(wk))
230 #define	WK_FREEBLKS(wk) ((struct freeblks *)(wk))
231 #define	WK_FREEWORK(wk) ((struct freework *)(wk))
232 #define	WK_FREEFILE(wk) ((struct freefile *)(wk))
233 #define	WK_DIRADD(wk) ((struct diradd *)(wk))
234 #define	WK_MKDIR(wk) ((struct mkdir *)(wk))
235 #define	WK_DIRREM(wk) ((struct dirrem *)(wk))
236 #define	WK_NEWDIRBLK(wk) ((struct newdirblk *)(wk))
237 #define	WK_JADDREF(wk) ((struct jaddref *)(wk))
238 #define	WK_JREMREF(wk) ((struct jremref *)(wk))
239 #define	WK_JMVREF(wk) ((struct jmvref *)(wk))
240 #define	WK_JSEGDEP(wk) ((struct jsegdep *)(wk))
241 #define	WK_JSEG(wk) ((struct jseg *)(wk))
242 #define	WK_JNEWBLK(wk) ((struct jnewblk *)(wk))
243 #define	WK_JFREEBLK(wk) ((struct jfreeblk *)(wk))
244 #define	WK_FREEDEP(wk) ((struct freedep *)(wk))
245 #define	WK_JFREEFRAG(wk) ((struct jfreefrag *)(wk))
246 #define	WK_SBDEP(wk) ((struct sbdep *)(wk))
247 #define	WK_JTRUNC(wk) ((struct jtrunc *)(wk))
248 #define	WK_JFSYNC(wk) ((struct jfsync *)(wk))
249 
250 /*
251  * Various types of lists
252  */
253 LIST_HEAD(dirremhd, dirrem);
254 LIST_HEAD(diraddhd, diradd);
255 LIST_HEAD(newblkhd, newblk);
256 LIST_HEAD(inodedephd, inodedep);
257 LIST_HEAD(allocindirhd, allocindir);
258 LIST_HEAD(allocdirecthd, allocdirect);
259 TAILQ_HEAD(allocdirectlst, allocdirect);
260 LIST_HEAD(indirdephd, indirdep);
261 LIST_HEAD(jaddrefhd, jaddref);
262 LIST_HEAD(jremrefhd, jremref);
263 LIST_HEAD(jmvrefhd, jmvref);
264 LIST_HEAD(jnewblkhd, jnewblk);
265 LIST_HEAD(jblkdephd, jblkdep);
266 LIST_HEAD(freeworkhd, freework);
267 TAILQ_HEAD(freeworklst, freework);
268 TAILQ_HEAD(jseglst, jseg);
269 TAILQ_HEAD(inoreflst, inoref);
270 TAILQ_HEAD(freeblklst, freeblks);
271 
272 /*
273  * The "pagedep" structure tracks the various dependencies related to
274  * a particular directory page. If a directory page has any dependencies,
275  * it will have a pagedep linked to its associated buffer. The
276  * pd_dirremhd list holds the list of dirrem requests which decrement
277  * inode reference counts. These requests are processed after the
278  * directory page with the corresponding zero'ed entries has been
279  * written. The pd_diraddhd list maintains the list of diradd requests
280  * which cannot be committed until their corresponding inode has been
281  * written to disk. Because a directory may have many new entries
282  * being created, several lists are maintained hashed on bits of the
283  * offset of the entry into the directory page to keep the lists from
284  * getting too long. Once a new directory entry has been cleared to
285  * be written, it is moved to the pd_pendinghd list. After the new
286  * entry has been written to disk it is removed from the pd_pendinghd
287  * list, any removed operations are done, and the dependency structure
288  * is freed.
289  */
290 #define	DAHASHSZ 5
291 #define	DIRADDHASH(offset) (((offset) >> 2) % DAHASHSZ)
292 struct pagedep {
293 	struct	worklist pd_list;	/* page buffer */
294 #	define	pd_state pd_list.wk_state /* check for multiple I/O starts */
295 	LIST_ENTRY(pagedep) pd_hash;	/* hashed lookup */
296 	ino_t	pd_ino;			/* associated file */
297 	ufs_lbn_t pd_lbn;		/* block within file */
298 	struct	newdirblk *pd_newdirblk; /* associated newdirblk if NEWBLOCK */
299 	struct	dirremhd pd_dirremhd;	/* dirrem's waiting for page */
300 	struct	diraddhd pd_diraddhd[DAHASHSZ]; /* diradd dir entry updates */
301 	struct	diraddhd pd_pendinghd;	/* directory entries awaiting write */
302 	struct	jmvrefhd pd_jmvrefhd;	/* Dependent journal writes. */
303 };
304 
305 /*
306  * The "inodedep" structure tracks the set of dependencies associated
307  * with an inode. One task that it must manage is delayed operations
308  * (i.e., work requests that must be held until the inodedep's associated
309  * inode has been written to disk). Getting an inode from its incore
310  * state to the disk requires two steps to be taken by the filesystem
311  * in this order: first the inode must be copied to its disk buffer by
312  * the VOP_UPDATE operation; second the inode's buffer must be written
313  * to disk. To ensure that both operations have happened in the required
314  * order, the inodedep maintains two lists. Delayed operations are
315  * placed on the id_inowait list. When the VOP_UPDATE is done, all
316  * operations on the id_inowait list are moved to the id_bufwait list.
317  * When the buffer is written, the items on the id_bufwait list can be
318  * safely moved to the work queue to be processed. A second task of the
319  * inodedep structure is to track the status of block allocation within
320  * the inode.  Each block that is allocated is represented by an
321  * "allocdirect" structure (see below). It is linked onto the id_newinoupdt
322  * list until both its contents and its allocation in the cylinder
323  * group map have been written to disk. Once these dependencies have been
324  * satisfied, it is removed from the id_newinoupdt list and any followup
325  * actions such as releasing the previous block or fragment are placed
326  * on the id_inowait list. When an inode is updated (a VOP_UPDATE is
327  * done), the "inodedep" structure is linked onto the buffer through
328  * its worklist. Thus, it will be notified when the buffer is about
329  * to be written and when it is done. At the update time, all the
330  * elements on the id_newinoupdt list are moved to the id_inoupdt list
331  * since those changes are now relevant to the copy of the inode in the
332  * buffer. Also at update time, the tasks on the id_inowait list are
333  * moved to the id_bufwait list so that they will be executed when
334  * the updated inode has been written to disk. When the buffer containing
335  * the inode is written to disk, any updates listed on the id_inoupdt
336  * list are rolled back as they are not yet safe. Following the write,
337  * the changes are once again rolled forward and any actions on the
338  * id_bufwait list are processed (since those actions are now safe).
339  * The entries on the id_inoupdt and id_newinoupdt lists must be kept
340  * sorted by logical block number to speed the calculation of the size
341  * of the rolled back inode (see explanation in initiate_write_inodeblock).
342  * When a directory entry is created, it is represented by a diradd.
343  * The diradd is added to the id_inowait list as it cannot be safely
344  * written to disk until the inode that it represents is on disk. After
345  * the inode is written, the id_bufwait list is processed and the diradd
346  * entries are moved to the id_pendinghd list where they remain until
347  * the directory block containing the name has been written to disk.
348  * The purpose of keeping the entries on the id_pendinghd list is so that
349  * the softdep_fsync function can find and push the inode's directory
350  * name(s) as part of the fsync operation for that file.
351  */
352 struct inodedep {
353 	struct	worklist id_list;	/* buffer holding inode block */
354 #	define	id_state id_list.wk_state /* inode dependency state */
355 	LIST_ENTRY(inodedep) id_hash;	/* hashed lookup */
356 	TAILQ_ENTRY(inodedep) id_unlinked;	/* Unlinked but ref'd inodes */
357 	struct	fs *id_fs;		/* associated filesystem */
358 	ino_t	id_ino;			/* dependent inode */
359 	nlink_t	id_nlinkdelta;		/* saved effective link count */
360 	nlink_t	id_nlinkwrote;		/* i_nlink that we wrote to disk */
361 	nlink_t	id_savednlink;		/* Link saved during rollback */
362 	LIST_ENTRY(inodedep) id_deps;	/* bmsafemap's list of inodedep's */
363 	struct	bmsafemap *id_bmsafemap; /* related bmsafemap (if pending) */
364 	struct	diradd *id_mkdiradd;	/* diradd for a mkdir. */
365 	struct	inoreflst id_inoreflst;	/* Inode reference adjustments. */
366 	long	id_savedextsize;	/* ext size saved during rollback */
367 	off_t	id_savedsize;		/* file size saved during rollback */
368 	struct	dirremhd id_dirremhd;	/* Removals pending. */
369 	struct	workhead id_pendinghd;	/* entries awaiting directory write */
370 	struct	workhead id_bufwait;	/* operations after inode written */
371 	struct	workhead id_inowait;	/* operations waiting inode update */
372 	struct	allocdirectlst id_inoupdt; /* updates before inode written */
373 	struct	allocdirectlst id_newinoupdt; /* updates when inode written */
374 	struct	allocdirectlst id_extupdt; /* extdata updates pre-inode write */
375 	struct	allocdirectlst id_newextupdt; /* extdata updates at ino write */
376 	struct	freeblklst id_freeblklst; /* List of partial truncates. */
377 	union {
378 	struct	ufs1_dinode *idu_savedino1; /* saved ufs1_dinode contents */
379 	struct	ufs2_dinode *idu_savedino2; /* saved ufs2_dinode contents */
380 	} id_un;
381 };
382 #define	id_savedino1 id_un.idu_savedino1
383 #define	id_savedino2 id_un.idu_savedino2
384 
385 /*
386  * A "bmsafemap" structure maintains a list of dependency structures
387  * that depend on the update of a particular cylinder group map.
388  * It has lists for newblks, allocdirects, allocindirs, and inodedeps.
389  * It is attached to the buffer of a cylinder group block when any of
390  * these things are allocated from the cylinder group. It is freed
391  * after the cylinder group map is written and the state of its
392  * dependencies are updated with DEPCOMPLETE to indicate that it has
393  * been processed.
394  */
395 struct bmsafemap {
396 	struct	worklist sm_list;	/* cylgrp buffer */
397 #	define	sm_state sm_list.wk_state
398 	LIST_ENTRY(bmsafemap) sm_hash;	/* Hash links. */
399 	LIST_ENTRY(bmsafemap) sm_next;	/* Mount list. */
400 	int	sm_cg;
401 	struct	buf *sm_buf;		/* associated buffer */
402 	struct	allocdirecthd sm_allocdirecthd; /* allocdirect deps */
403 	struct	allocdirecthd sm_allocdirectwr; /* writing allocdirect deps */
404 	struct	allocindirhd sm_allocindirhd; /* allocindir deps */
405 	struct	allocindirhd sm_allocindirwr; /* writing allocindir deps */
406 	struct	inodedephd sm_inodedephd; /* inodedep deps */
407 	struct	inodedephd sm_inodedepwr; /* writing inodedep deps */
408 	struct	newblkhd sm_newblkhd;	/* newblk deps */
409 	struct	newblkhd sm_newblkwr;	/* writing newblk deps */
410 	struct	jaddrefhd sm_jaddrefhd;	/* Pending inode allocations. */
411 	struct	jnewblkhd sm_jnewblkhd;	/* Pending block allocations. */
412 	struct	workhead sm_freehd;	/* Freedep deps. */
413 	struct	workhead sm_freewr;	/* Written freedeps. */
414 };
415 
416 /*
417  * A "newblk" structure is attached to a bmsafemap structure when a block
418  * or fragment is allocated from a cylinder group. Its state is set to
419  * DEPCOMPLETE when its cylinder group map is written. It is converted to
420  * an allocdirect or allocindir allocation once the allocator calls the
421  * appropriate setup function. It will initially be linked onto a bmsafemap
422  * list. Once converted it can be linked onto the lists described for
423  * allocdirect or allocindir as described below.
424  */
425 struct newblk {
426 	struct	worklist nb_list;	/* See comment above. */
427 #	define	nb_state nb_list.wk_state
428 	LIST_ENTRY(newblk) nb_hash;	/* Hashed lookup. */
429 	LIST_ENTRY(newblk) nb_deps;	/* Bmsafemap's list of newblks. */
430 	struct	jnewblk *nb_jnewblk;	/* New block journal entry. */
431 	struct	bmsafemap *nb_bmsafemap;/* Cylgrp dep (if pending). */
432 	struct	freefrag *nb_freefrag;	/* Fragment to be freed (if any). */
433 	struct	indirdephd nb_indirdeps; /* Children indirect blocks. */
434 	struct	workhead nb_newdirblk;	/* Dir block to notify when written. */
435 	struct	workhead nb_jwork;	/* Journal work pending. */
436 	ufs2_daddr_t	nb_newblkno;	/* New value of block pointer. */
437 };
438 
439 /*
440  * An "allocdirect" structure is attached to an "inodedep" when a new block
441  * or fragment is allocated and pointed to by the inode described by
442  * "inodedep". The worklist is linked to the buffer that holds the block.
443  * When the block is first allocated, it is linked to the bmsafemap
444  * structure associated with the buffer holding the cylinder group map
445  * from which it was allocated. When the cylinder group map is written
446  * to disk, ad_state has the DEPCOMPLETE flag set. When the block itself
447  * is written, the COMPLETE flag is set. Once both the cylinder group map
448  * and the data itself have been written, it is safe to write the inode
449  * that claims the block. If there was a previous fragment that had been
450  * allocated before the file was increased in size, the old fragment may
451  * be freed once the inode claiming the new block is written to disk.
452  * This ad_fragfree request is attached to the id_inowait list of the
453  * associated inodedep (pointed to by ad_inodedep) for processing after
454  * the inode is written. When a block is allocated to a directory, an
455  * fsync of a file whose name is within that block must ensure not only
456  * that the block containing the file name has been written, but also
457  * that the on-disk inode references that block. When a new directory
458  * block is created, we allocate a newdirblk structure which is linked
459  * to the associated allocdirect (on its ad_newdirblk list). When the
460  * allocdirect has been satisfied, the newdirblk structure is moved to
461  * the inodedep id_bufwait list of its directory to await the inode
462  * being written. When the inode is written, the directory entries are
463  * fully committed and can be deleted from their pagedep->id_pendinghd
464  * and inodedep->id_pendinghd lists.
465  */
466 struct allocdirect {
467 	struct	newblk ad_block;	/* Common block logic */
468 #	define	ad_list ad_block.nb_list /* block pointer worklist */
469 #	define	ad_state ad_list.wk_state /* block pointer state */
470 	TAILQ_ENTRY(allocdirect) ad_next; /* inodedep's list of allocdirect's */
471 	struct	inodedep *ad_inodedep;	/* associated inodedep */
472 	ufs2_daddr_t	ad_oldblkno;	/* old value of block pointer */
473 	int		ad_offset;	/* Pointer offset in parent. */
474 	long		ad_newsize;	/* size of new block */
475 	long		ad_oldsize;	/* size of old block */
476 };
477 #define	ad_newblkno	ad_block.nb_newblkno
478 #define	ad_freefrag	ad_block.nb_freefrag
479 #define	ad_newdirblk	ad_block.nb_newdirblk
480 
481 /*
482  * A single "indirdep" structure manages all allocation dependencies for
483  * pointers in an indirect block. The up-to-date state of the indirect
484  * block is stored in ir_savedata. The set of pointers that may be safely
485  * written to the disk is stored in ir_savebp. The state field is used
486  * only to track whether the buffer is currently being written (in which
487  * case it is not safe to update ir_savebp). Ir_deplisthd contains the
488  * list of allocindir structures, one for each block that needs to be
489  * written to disk. Once the block and its bitmap allocation have been
490  * written the safecopy can be updated to reflect the allocation and the
491  * allocindir structure freed. If ir_state indicates that an I/O on the
492  * indirect block is in progress when ir_savebp is to be updated, the
493  * update is deferred by placing the allocindir on the ir_donehd list.
494  * When the I/O on the indirect block completes, the entries on the
495  * ir_donehd list are processed by updating their corresponding ir_savebp
496  * pointers and then freeing the allocindir structure.
497  */
498 struct indirdep {
499 	struct	worklist ir_list;	/* buffer holding indirect block */
500 #	define	ir_state ir_list.wk_state /* indirect block pointer state */
501 	LIST_ENTRY(indirdep) ir_next;	/* alloc{direct,indir} list */
502 	TAILQ_HEAD(, freework) ir_trunc;	/* List of truncations. */
503 	caddr_t	ir_saveddata;		/* buffer cache contents */
504 	struct	buf *ir_savebp;		/* buffer holding safe copy */
505 	struct	buf *ir_bp;		/* buffer holding live copy */
506 	struct	allocindirhd ir_completehd; /* waiting for indirdep complete */
507 	struct	allocindirhd ir_writehd; /* Waiting for the pointer write. */
508 	struct	allocindirhd ir_donehd;	/* done waiting to update safecopy */
509 	struct	allocindirhd ir_deplisthd; /* allocindir deps for this block */
510 	struct	freeblks *ir_freeblks;	/* Freeblks that frees this indir. */
511 };
512 
513 /*
514  * An "allocindir" structure is attached to an "indirdep" when a new block
515  * is allocated and pointed to by the indirect block described by the
516  * "indirdep". The worklist is linked to the buffer that holds the new block.
517  * When the block is first allocated, it is linked to the bmsafemap
518  * structure associated with the buffer holding the cylinder group map
519  * from which it was allocated. When the cylinder group map is written
520  * to disk, ai_state has the DEPCOMPLETE flag set. When the block itself
521  * is written, the COMPLETE flag is set. Once both the cylinder group map
522  * and the data itself have been written, it is safe to write the entry in
523  * the indirect block that claims the block; the "allocindir" dependency
524  * can then be freed as it is no longer applicable.
525  */
526 struct allocindir {
527 	struct	newblk ai_block;	/* Common block area */
528 #	define	ai_state ai_block.nb_list.wk_state /* indirect pointer state */
529 	LIST_ENTRY(allocindir) ai_next;	/* indirdep's list of allocindir's */
530 	struct	indirdep *ai_indirdep;	/* address of associated indirdep */
531 	ufs2_daddr_t	ai_oldblkno;	/* old value of block pointer */
532 	ufs_lbn_t	ai_lbn;		/* Logical block number. */
533 	int		ai_offset;	/* Pointer offset in parent. */
534 };
535 #define	ai_newblkno	ai_block.nb_newblkno
536 #define	ai_freefrag	ai_block.nb_freefrag
537 #define	ai_newdirblk	ai_block.nb_newdirblk
538 
539 /*
540  * The allblk union is used to size the newblk structure on allocation so
541  * that it may be any one of three types.
542  */
543 union allblk {
544 	struct	allocindir ab_allocindir;
545 	struct	allocdirect ab_allocdirect;
546 	struct	newblk	ab_newblk;
547 };
548 
549 /*
550  * A "freefrag" structure is attached to an "inodedep" when a previously
551  * allocated fragment is replaced with a larger fragment, rather than extended.
552  * The "freefrag" structure is constructed and attached when the replacement
553  * block is first allocated. It is processed after the inode claiming the
554  * bigger block that replaces it has been written to disk.
555  */
556 struct freefrag {
557 	struct	worklist ff_list;	/* id_inowait or delayed worklist */
558 #	define	ff_state ff_list.wk_state
559 	struct	worklist *ff_jdep;	/* Associated journal entry. */
560 	struct	workhead ff_jwork;	/* Journal work pending. */
561 	ufs2_daddr_t ff_blkno;		/* fragment physical block number */
562 	long	ff_fragsize;		/* size of fragment being deleted */
563 	ino_t	ff_inum;		/* owning inode number */
564 	__enum_uint8(vtype) ff_vtype;	/* owning inode's file type */
565 	int	ff_key;			/* trim key when deleted */
566 };
567 
568 /*
569  * A "freeblks" structure is attached to an "inodedep" when the
570  * corresponding file's length is reduced to zero. It records all
571  * the information needed to free the blocks of a file after its
572  * zero'ed inode has been written to disk.  The actual work is done
573  * by child freework structures which are responsible for individual
574  * inode pointers while freeblks is responsible for retiring the
575  * entire operation when it is complete and holding common members.
576  */
577 struct freeblks {
578 	struct	worklist fb_list;	/* id_inowait or delayed worklist */
579 #	define	fb_state fb_list.wk_state /* inode and dirty block state */
580 	TAILQ_ENTRY(freeblks) fb_next;	/* List of inode truncates. */
581 	struct	jblkdephd fb_jblkdephd;	/* Journal entries pending */
582 	struct	workhead fb_freeworkhd;	/* Work items pending */
583 	struct	workhead fb_jwork;	/* Journal work pending */
584 	struct	vnode *fb_devvp;	/* filesystem device vnode */
585 #ifdef QUOTA
586 	struct	dquot *fb_quota[MAXQUOTAS]; /* quotas to be adjusted */
587 #endif
588 	uint64_t fb_modrev;		/* Inode revision at start of trunc. */
589 	off_t	fb_len;			/* Length we're truncating to. */
590 	ufs2_daddr_t fb_chkcnt;		/* Blocks released. */
591 	ino_t	fb_inum;		/* inode owner of blocks */
592 	__enum_uint8(vtype) fb_vtype;	/* inode owner's file type */
593 	uid_t	fb_uid;			/* uid of previous owner of blocks */
594 	int	fb_ref;			/* Children outstanding. */
595 	int	fb_cgwait;		/* cg writes outstanding. */
596 };
597 
598 /*
599  * A "freework" structure handles the release of a tree of blocks or a single
600  * block.  Each indirect block in a tree is allocated its own freework
601  * structure so that the indirect block may be freed only when all of its
602  * children are freed.  In this way we enforce the rule that an allocated
603  * block must have a valid path to a root that is journaled.  Each child
604  * block acquires a reference and when the ref hits zero the parent ref
605  * is decremented.  If there is no parent the freeblks ref is decremented.
606  */
607 struct freework {
608 	struct	worklist fw_list;		/* Delayed worklist. */
609 #	define	fw_state fw_list.wk_state
610 	LIST_ENTRY(freework) fw_segs;		/* Seg list. */
611 	TAILQ_ENTRY(freework) fw_next;		/* Hash/Trunc list. */
612 	struct	jnewblk	 *fw_jnewblk;		/* Journal entry to cancel. */
613 	struct	freeblks *fw_freeblks;		/* Root of operation. */
614 	struct	freework *fw_parent;		/* Parent indirect. */
615 	struct	indirdep *fw_indir;		/* indirect block. */
616 	ufs2_daddr_t	 fw_blkno;		/* Our block #. */
617 	ufs_lbn_t	 fw_lbn;		/* Original lbn before free. */
618 	uint16_t	 fw_frags;		/* Number of frags. */
619 	uint16_t	 fw_ref;		/* Number of children out. */
620 	uint16_t	 fw_off;		/* Current working position. */
621 	uint16_t	 fw_start;		/* Start of partial truncate. */
622 };
623 
624 /*
625  * A "freedep" structure is allocated to track the completion of a bitmap
626  * write for a freework.  One freedep may cover many freed blocks so long
627  * as they reside in the same cylinder group.  When the cg is written
628  * the freedep decrements the ref on the freework which may permit it
629  * to be freed as well.
630  */
631 struct freedep {
632 	struct	worklist fd_list;	/* Delayed worklist. */
633 	struct	freework *fd_freework;	/* Parent freework. */
634 };
635 
636 /*
637  * A "freefile" structure is attached to an inode when its
638  * link count is reduced to zero. It marks the inode as free in
639  * the cylinder group map after the zero'ed inode has been written
640  * to disk and any associated blocks and fragments have been freed.
641  */
642 struct freefile {
643 	struct	worklist fx_list;	/* id_inowait or delayed worklist */
644 	mode_t	fx_mode;		/* mode of inode */
645 	ino_t	fx_oldinum;		/* inum of the unlinked file */
646 	struct	vnode *fx_devvp;	/* filesystem device vnode */
647 	struct	workhead fx_jwork;	/* journal work pending. */
648 };
649 
650 /*
651  * A "diradd" structure is linked to an "inodedep" id_inowait list when a
652  * new directory entry is allocated that references the inode described
653  * by "inodedep". When the inode itself is written (either the initial
654  * allocation for new inodes or with the increased link count for
655  * existing inodes), the COMPLETE flag is set in da_state. If the entry
656  * is for a newly allocated inode, the "inodedep" structure is associated
657  * with a bmsafemap which prevents the inode from being written to disk
658  * until the cylinder group has been updated. Thus the da_state COMPLETE
659  * flag cannot be set until the inode bitmap dependency has been removed.
660  * When creating a new file, it is safe to write the directory entry that
661  * claims the inode once the referenced inode has been written. Since
662  * writing the inode clears the bitmap dependencies, the DEPCOMPLETE flag
663  * in the diradd can be set unconditionally when creating a file. When
664  * creating a directory, there are two additional dependencies described by
665  * mkdir structures (see their description below). When these dependencies
666  * are resolved the DEPCOMPLETE flag is set in the diradd structure.
667  * If there are multiple links created to the same inode, there will be
668  * a separate diradd structure created for each link. The diradd is
669  * linked onto the pg_diraddhd list of the pagedep for the directory
670  * page that contains the entry. When a directory page is written,
671  * the pg_diraddhd list is traversed to rollback any entries that are
672  * not yet ready to be written to disk. If a directory entry is being
673  * changed (by rename) rather than added, the DIRCHG flag is set and
674  * the da_previous entry points to the entry that will be "removed"
675  * once the new entry has been committed. During rollback, entries
676  * with da_previous are replaced with the previous inode number rather
677  * than zero.
678  *
679  * The overlaying of da_pagedep and da_previous is done to keep the
680  * structure down. If a da_previous entry is present, the pointer to its
681  * pagedep is available in the associated dirrem entry. If the DIRCHG flag
682  * is set, the da_previous entry is valid; if not set the da_pagedep entry
683  * is valid. The DIRCHG flag never changes; it is set when the structure
684  * is created if appropriate and is never cleared.
685  */
686 struct diradd {
687 	struct	worklist da_list;	/* id_inowait or id_pendinghd list */
688 #	define	da_state da_list.wk_state /* state of the new directory entry */
689 	LIST_ENTRY(diradd) da_pdlist;	/* pagedep holding directory block */
690 	doff_t	da_offset;		/* offset of new dir entry in dir blk */
691 	ino_t	da_newinum;		/* inode number for the new dir entry */
692 	union {
693 	struct	dirrem *dau_previous;	/* entry being replaced in dir change */
694 	struct	pagedep *dau_pagedep;	/* pagedep dependency for addition */
695 	} da_un;
696 	struct workhead da_jwork;	/* Journal work awaiting completion. */
697 };
698 #define	da_previous da_un.dau_previous
699 #define	da_pagedep da_un.dau_pagedep
700 
701 /*
702  * Two "mkdir" structures are needed to track the additional dependencies
703  * associated with creating a new directory entry. Normally a directory
704  * addition can be committed as soon as the newly referenced inode has been
705  * written to disk with its increased link count. When a directory is
706  * created there are two additional dependencies: writing the directory
707  * data block containing the "." and ".." entries (MKDIR_BODY) and writing
708  * the parent inode with the increased link count for ".." (MKDIR_PARENT).
709  * These additional dependencies are tracked by two mkdir structures that
710  * reference the associated "diradd" structure. When they have completed,
711  * they set the DEPCOMPLETE flag on the diradd so that it knows that its
712  * extra dependencies have been completed. The md_state field is used only
713  * to identify which type of dependency the mkdir structure is tracking.
714  * It is not used in the mainline code for any purpose other than consistency
715  * checking. All the mkdir structures in the system are linked together on
716  * a list. This list is needed so that a diradd can find its associated
717  * mkdir structures and deallocate them if it is prematurely freed (as for
718  * example if a mkdir is immediately followed by a rmdir of the same directory).
719  * Here, the free of the diradd must traverse the list to find the associated
720  * mkdir structures that reference it. The deletion would be faster if the
721  * diradd structure were simply augmented to have two pointers that referenced
722  * the associated mkdir's. However, this would increase the size of the diradd
723  * structure to speed a very infrequent operation.
724  */
725 struct mkdir {
726 	struct	worklist md_list;	/* id_inowait or buffer holding dir */
727 #	define	md_state md_list.wk_state /* type: MKDIR_PARENT or MKDIR_BODY */
728 	struct	diradd *md_diradd;	/* associated diradd */
729 	struct	jaddref *md_jaddref;	/* dependent jaddref. */
730 	struct	buf *md_buf;		/* MKDIR_BODY: buffer holding dir */
731 	LIST_ENTRY(mkdir) md_mkdirs;	/* list of all mkdirs */
732 };
733 
734 /*
735  * A "dirrem" structure describes an operation to decrement the link
736  * count on an inode. The dirrem structure is attached to the pg_dirremhd
737  * list of the pagedep for the directory page that contains the entry.
738  * It is processed after the directory page with the deleted entry has
739  * been written to disk.
740  */
741 struct dirrem {
742 	struct	worklist dm_list;	/* delayed worklist */
743 #	define	dm_state dm_list.wk_state /* state of the old directory entry */
744 	LIST_ENTRY(dirrem) dm_next;	/* pagedep's list of dirrem's */
745 	LIST_ENTRY(dirrem) dm_inonext;	/* inodedep's list of dirrem's */
746 	struct	jremrefhd dm_jremrefhd;	/* Pending remove reference deps. */
747 	ino_t	dm_oldinum;		/* inum of the removed dir entry */
748 	doff_t	dm_offset;		/* offset of removed dir entry in blk */
749 	union {
750 	struct	pagedep *dmu_pagedep;	/* pagedep dependency for remove */
751 	ino_t	dmu_dirinum;		/* parent inode number (for rmdir) */
752 	} dm_un;
753 	struct workhead dm_jwork;	/* Journal work awaiting completion. */
754 };
755 #define	dm_pagedep dm_un.dmu_pagedep
756 #define	dm_dirinum dm_un.dmu_dirinum
757 
758 /*
759  * A "newdirblk" structure tracks the progress of a newly allocated
760  * directory block from its creation until it is claimed by its on-disk
761  * inode. When a block is allocated to a directory, an fsync of a file
762  * whose name is within that block must ensure not only that the block
763  * containing the file name has been written, but also that the on-disk
764  * inode references that block. When a new directory block is created,
765  * we allocate a newdirblk structure which is linked to the associated
766  * allocdirect (on its ad_newdirblk list). When the allocdirect has been
767  * satisfied, the newdirblk structure is moved to the inodedep id_bufwait
768  * list of its directory to await the inode being written. When the inode
769  * is written, the directory entries are fully committed and can be
770  * deleted from their pagedep->id_pendinghd and inodedep->id_pendinghd
771  * lists. Note that we could track directory blocks allocated to indirect
772  * blocks using a similar scheme with the allocindir structures. Rather
773  * than adding this level of complexity, we simply write those newly
774  * allocated indirect blocks synchronously as such allocations are rare.
775  * In the case of a new directory the . and .. links are tracked with
776  * a mkdir rather than a pagedep.  In this case we track the mkdir
777  * so it can be released when it is written.  A workhead is used
778  * to simplify canceling a mkdir that is removed by a subsequent dirrem.
779  */
780 struct newdirblk {
781 	struct	worklist db_list;	/* id_inowait or pg_newdirblk */
782 #	define	db_state db_list.wk_state
783 	struct	pagedep *db_pagedep;	/* associated pagedep */
784 	struct	workhead db_mkdir;
785 };
786 
787 /*
788  * The inoref structure holds the elements common to jaddref and jremref
789  * so they may easily be queued in-order on the inodedep.
790  */
791 struct inoref {
792 	struct	worklist if_list;	/* Journal pending or jseg entries. */
793 #	define	if_state if_list.wk_state
794 	TAILQ_ENTRY(inoref) if_deps;	/* Links for inodedep. */
795 	struct	jsegdep	*if_jsegdep;	/* Will track our journal record. */
796 	off_t		if_diroff;	/* Directory offset. */
797 	ino_t		if_ino;		/* Inode number. */
798 	ino_t		if_parent;	/* Parent inode number. */
799 	nlink_t		if_nlink;	/* nlink before addition. */
800 	uint16_t	if_mode;	/* File mode, needed for IFMT. */
801 };
802 
803 /*
804  * A "jaddref" structure tracks a new reference (link count) on an inode
805  * and prevents the link count increase and bitmap allocation until a
806  * journal entry can be written.  Once the journal entry is written,
807  * the inode is put on the pendinghd of the bmsafemap and a diradd or
808  * mkdir entry is placed on the bufwait list of the inode.  The DEPCOMPLETE
809  * flag is used to indicate that all of the required information for writing
810  * the journal entry is present.  MKDIR_BODY and MKDIR_PARENT are used to
811  * differentiate . and .. links from regular file names.  NEWBLOCK indicates
812  * a bitmap is still pending.  If a new reference is canceled by a delete
813  * prior to writing the journal the jaddref write is canceled and the
814  * structure persists to prevent any disk-visible changes until it is
815  * ultimately released when the file is freed or the link is dropped again.
816  */
817 struct jaddref {
818 	struct	inoref	ja_ref;		/* see inoref above. */
819 #	define	ja_list	ja_ref.if_list	/* Jrnl pending, id_inowait, dm_jwork.*/
820 #	define	ja_state ja_ref.if_list.wk_state
821 	LIST_ENTRY(jaddref) ja_bmdeps;	/* Links for bmsafemap. */
822 	union {
823 		struct	diradd	*jau_diradd;	/* Pending diradd. */
824 		struct	mkdir	*jau_mkdir;	/* MKDIR_{PARENT,BODY} */
825 	} ja_un;
826 };
827 #define	ja_diradd	ja_un.jau_diradd
828 #define	ja_mkdir	ja_un.jau_mkdir
829 #define	ja_diroff	ja_ref.if_diroff
830 #define	ja_ino		ja_ref.if_ino
831 #define	ja_parent	ja_ref.if_parent
832 #define	ja_mode		ja_ref.if_mode
833 
834 /*
835  * A "jremref" structure tracks a removed reference (unlink) on an
836  * inode and prevents the directory remove from proceeding until the
837  * journal entry is written.  Once the journal has been written the remove
838  * may proceed as normal.
839  */
840 struct jremref {
841 	struct	inoref	jr_ref;		/* see inoref above. */
842 #	define	jr_list	jr_ref.if_list	/* Linked to softdep_journal_pending. */
843 #	define	jr_state jr_ref.if_list.wk_state
844 	LIST_ENTRY(jremref) jr_deps;	/* Links for dirrem. */
845 	struct	dirrem	*jr_dirrem;	/* Back pointer to dirrem. */
846 };
847 
848 /*
849  * A "jmvref" structure tracks a name relocations within the same
850  * directory block that occur as a result of directory compaction.
851  * It prevents the updated directory entry from being written to disk
852  * until the journal entry is written. Once the journal has been
853  * written the compacted directory may be written to disk.
854  */
855 struct jmvref {
856 	struct	worklist jm_list;	/* Linked to softdep_journal_pending. */
857 	LIST_ENTRY(jmvref) jm_deps;	/* Jmvref on pagedep. */
858 	struct pagedep	*jm_pagedep;	/* Back pointer to pagedep. */
859 	ino_t		jm_parent;	/* Containing directory inode number. */
860 	ino_t		jm_ino;		/* Inode number of our entry. */
861 	off_t		jm_oldoff;	/* Our old offset in directory. */
862 	off_t		jm_newoff;	/* Our new offset in directory. */
863 };
864 
865 /*
866  * A "jnewblk" structure tracks a newly allocated block or fragment and
867  * prevents the direct or indirect block pointer as well as the cg bitmap
868  * from being written until it is logged.  After it is logged the jsegdep
869  * is attached to the allocdirect or allocindir until the operation is
870  * completed or reverted.  If the operation is reverted prior to the journal
871  * write the jnewblk structure is maintained to prevent the bitmaps from
872  * reaching the disk.  Ultimately the jnewblk structure will be passed
873  * to the free routine as the in memory cg is modified back to the free
874  * state at which time it can be released. It may be held on any of the
875  * fx_jwork, fw_jwork, fb_jwork, ff_jwork, nb_jwork, or ir_jwork lists.
876  */
877 struct jnewblk {
878 	struct	worklist jn_list;	/* See lists above. */
879 #	define	jn_state jn_list.wk_state
880 	struct	jsegdep	*jn_jsegdep;	/* Will track our journal record. */
881 	LIST_ENTRY(jnewblk) jn_deps;	/* Jnewblks on sm_jnewblkhd. */
882 	struct	worklist *jn_dep;	/* Dependency to ref completed seg. */
883 	ufs_lbn_t	jn_lbn;		/* Lbn to which allocated. */
884 	ufs2_daddr_t	jn_blkno;	/* Blkno allocated */
885 	ino_t		jn_ino;		/* Ino to which allocated. */
886 	int		jn_oldfrags;	/* Previous fragments when extended. */
887 	int		jn_frags;	/* Number of fragments. */
888 };
889 
890 /*
891  * A "jblkdep" structure tracks jfreeblk and jtrunc records attached to a
892  * freeblks structure.
893  */
894 struct jblkdep {
895 	struct	worklist jb_list;	/* For softdep journal pending. */
896 	struct	jsegdep *jb_jsegdep;	/* Reference to the jseg. */
897 	struct	freeblks *jb_freeblks;	/* Back pointer to freeblks. */
898 	LIST_ENTRY(jblkdep) jb_deps;	/* Dep list on freeblks. */
899 
900 };
901 
902 /*
903  * A "jfreeblk" structure tracks the journal write for freeing a block
904  * or tree of blocks.  The block pointer must not be cleared in the inode
905  * or indirect prior to the jfreeblk being written to the journal.
906  */
907 struct jfreeblk {
908 	struct	jblkdep	jf_dep;		/* freeblks linkage. */
909 	ufs_lbn_t	jf_lbn;		/* Lbn from which blocks freed. */
910 	ufs2_daddr_t	jf_blkno;	/* Blkno being freed. */
911 	ino_t		jf_ino;		/* Ino from which blocks freed. */
912 	int		jf_frags;	/* Number of frags being freed. */
913 };
914 
915 /*
916  * A "jfreefrag" tracks the freeing of a single block when a fragment is
917  * extended or an indirect page is replaced.  It is not part of a larger
918  * freeblks operation.
919  */
920 struct jfreefrag {
921 	struct	worklist fr_list;	/* Linked to softdep_journal_pending. */
922 #	define	fr_state fr_list.wk_state
923 	struct	jsegdep	*fr_jsegdep;	/* Will track our journal record. */
924 	struct freefrag	*fr_freefrag;	/* Back pointer to freefrag. */
925 	ufs_lbn_t	fr_lbn;		/* Lbn from which frag freed. */
926 	ufs2_daddr_t	fr_blkno;	/* Blkno being freed. */
927 	ino_t		fr_ino;		/* Ino from which frag freed. */
928 	int		fr_frags;	/* Size of frag being freed. */
929 };
930 
931 /*
932  * A "jtrunc" journals the intent to truncate an inode's data or extent area.
933  */
934 struct jtrunc {
935 	struct	jblkdep	jt_dep;		/* freeblks linkage. */
936 	off_t		jt_size;	/* Final file size. */
937 	int		jt_extsize;	/* Final extent size. */
938 	ino_t		jt_ino;		/* Ino being truncated. */
939 };
940 
941 /*
942  * A "jfsync" journals the completion of an fsync which invalidates earlier
943  * jtrunc records in the journal.
944  */
945 struct jfsync {
946 	struct worklist	jfs_list;	/* For softdep journal pending. */
947 	off_t		jfs_size;	/* Sync file size. */
948 	int		jfs_extsize;	/* Sync extent size. */
949 	ino_t		jfs_ino;	/* ino being synced. */
950 };
951 
952 /*
953  * A "jsegdep" structure tracks a single reference to a written journal
954  * segment so the journal space can be reclaimed when all dependencies
955  * have been written. It can hang off of id_inowait, dm_jwork, da_jwork,
956  * nb_jwork, ff_jwork, or fb_jwork lists.
957  */
958 struct jsegdep {
959 	struct	worklist jd_list;	/* See above for lists. */
960 #	define	jd_state jd_list.wk_state
961 	struct	jseg	*jd_seg;	/* Our journal record. */
962 };
963 
964 /*
965  * A "jseg" structure contains all of the journal records written in a
966  * single disk write.  The jaddref and jremref structures are linked into
967  * js_entries so thay may be completed when the write completes.  The
968  * js_entries also include the write dependency structures: jmvref,
969  * jnewblk, jfreeblk, jfreefrag, and jtrunc.  The js_refs field counts
970  * the number of entries on the js_entries list. Thus there is a single
971  * jseg entry to describe each journal write.
972  */
973 struct jseg {
974 	struct	worklist js_list;	/* b_deps link for journal */
975 #	define	js_state js_list.wk_state
976 	struct	workhead js_entries;	/* Entries awaiting write */
977 	LIST_HEAD(, freework) js_indirs;/* List of indirects in this seg. */
978 	TAILQ_ENTRY(jseg) js_next;	/* List of all unfinished segments. */
979 	struct	jblocks *js_jblocks;	/* Back pointer to block/seg list */
980 	struct	buf *js_buf;		/* Buffer while unwritten */
981 	uint64_t js_seq;		/* Journal record sequence number. */
982 	uint64_t js_oldseq;		/* Oldest valid sequence number. */
983 	int	js_size;		/* Size of journal record in bytes. */
984 	int	js_cnt;			/* Total items allocated. */
985 	int	js_refs;		/* Count of js_entries items. */
986 };
987 
988 /*
989  * A 'sbdep' structure tracks the head of the free inode list and
990  * superblock writes.  This makes sure the superblock is always pointing at
991  * the first possible unlinked inode for the suj recovery process.  If a
992  * block write completes and we discover a new head is available the buf
993  * is dirtied and the dep is kept. See the description of the UNLINK*
994  * flags above for more details.
995  */
996 struct sbdep {
997 	struct	worklist sb_list;	/* b_dep linkage */
998 	struct	fs	*sb_fs;		/* Filesystem pointer within buf. */
999 	struct	ufsmount *sb_ump;	/* Our mount structure */
1000 };
1001 
1002 /*
1003  * Private journaling structures.
1004  */
1005 struct jblocks {
1006 	struct jseglst	jb_segs;	/* TAILQ of current segments. */
1007 	struct jseg	*jb_writeseg;	/* Next write to complete. */
1008 	struct jseg	*jb_oldestseg;	/* Oldest segment with valid entries. */
1009 	struct jextent	*jb_extent;	/* Extent array. */
1010 	uint64_t	jb_nextseq;	/* Next sequence number. */
1011 	uint64_t	jb_oldestwrseq;	/* Oldest written sequence number. */
1012 	uint8_t		jb_needseg;	/* Need a forced segment. */
1013 	uint8_t		jb_suspended;	/* Did journal suspend writes? */
1014 	int		jb_avail;	/* Available extents. */
1015 	int		jb_used;	/* Last used extent. */
1016 	int		jb_head;	/* Allocator head. */
1017 	int		jb_off;		/* Allocator extent offset. */
1018 	int		jb_blocks;	/* Total disk blocks covered. */
1019 	int		jb_free;	/* Total disk blocks free. */
1020 	int		jb_min;		/* Minimum free space. */
1021 	int		jb_low;		/* Low on space. */
1022 	int		jb_age;		/* Insertion time of oldest rec. */
1023 };
1024 
1025 struct jextent {
1026 	ufs2_daddr_t	je_daddr;	/* Disk block address. */
1027 	int		je_blocks;	/* Disk block count. */
1028 };
1029 
1030 /*
1031  * Hash table declarations.
1032  */
1033 LIST_HEAD(mkdirlist, mkdir);
1034 LIST_HEAD(pagedep_hashhead, pagedep);
1035 LIST_HEAD(inodedep_hashhead, inodedep);
1036 LIST_HEAD(newblk_hashhead, newblk);
1037 LIST_HEAD(bmsafemap_hashhead, bmsafemap);
1038 TAILQ_HEAD(indir_hashhead, freework);
1039 
1040 /*
1041  * Per-filesystem soft dependency data.
1042  * Allocated at mount and freed at unmount.
1043  */
1044 struct mount_softdeps {
1045 	struct	rwlock sd_fslock;		/* softdep lock */
1046 	struct	workhead sd_workitem_pending;	/* softdep work queue */
1047 	struct	worklist *sd_worklist_tail;	/* Tail pointer for above */
1048 	struct	workhead sd_journal_pending;	/* journal work queue */
1049 	struct	worklist *sd_journal_tail;	/* Tail pointer for above */
1050 	struct	jblocks *sd_jblocks;		/* Journal block information */
1051 	struct	inodedeplst sd_unlinked;	/* Unlinked inodes */
1052 	struct	bmsafemaphd sd_dirtycg;		/* Dirty CGs */
1053 	struct	mkdirlist sd_mkdirlisthd;	/* Track mkdirs */
1054 	struct	pagedep_hashhead *sd_pdhash;	/* pagedep hash table */
1055 	u_long	sd_pdhashsize;			/* pagedep hash table size-1 */
1056 	long	sd_pdnextclean;			/* next hash bucket to clean */
1057 	struct	inodedep_hashhead *sd_idhash;	/* inodedep hash table */
1058 	u_long	sd_idhashsize;			/* inodedep hash table size-1 */
1059 	long	sd_idnextclean;			/* next hash bucket to clean */
1060 	struct	newblk_hashhead *sd_newblkhash;	/* newblk hash table */
1061 	u_long	sd_newblkhashsize;		/* newblk hash table size-1 */
1062 	struct	bmsafemap_hashhead *sd_bmhash;	/* bmsafemap hash table */
1063 	u_long	sd_bmhashsize;			/* bmsafemap hash table size-1*/
1064 	struct	indir_hashhead *sd_indirhash;	/* indir hash table */
1065 	uint64_t sd_indirhashsize;		/* indir hash table size-1 */
1066 	int	sd_on_journal;			/* Items on the journal list */
1067 	int	sd_on_worklist;			/* Items on the worklist */
1068 	int	sd_deps;			/* Total dependency count */
1069 	int	sd_accdeps;			/* accumulated dep count */
1070 	int	sd_req;				/* Wakeup when deps hits 0. */
1071 	int	sd_flags;			/* comm with flushing thread */
1072 	int	sd_cleanups;			/* Calls to cleanup */
1073 	struct	thread *sd_flushtd;		/* thread handling flushing */
1074 	TAILQ_ENTRY(mount_softdeps) sd_next;	/* List of softdep filesystem */
1075 	struct	ufsmount *sd_ump;		/* our ufsmount structure */
1076 	uint64_t sd_curdeps[D_LAST + 1];	/* count of current deps */
1077 	struct	workhead sd_alldeps[D_LAST + 1];/* Lists of all deps */
1078 };
1079 /*
1080  * Flags for communicating with the syncer thread.
1081  */
1082 #define FLUSH_EXIT	0x0001	/* time to exit */
1083 #define FLUSH_CLEANUP	0x0002	/* need to clear out softdep structures */
1084 #define	FLUSH_STARTING	0x0004	/* flush thread not yet started */
1085 #define	FLUSH_RC_ACTIVE	0x0008	/* a thread is flushing the mount point */
1086 #define	FLUSH_DI_ACTIVE	0x0010	/* a thread is processing delayed
1087 				   inactivations */
1088 
1089 /*
1090  * Keep the old names from when these were in the ufsmount structure.
1091  */
1092 #define	softdep_workitem_pending	um_softdep->sd_workitem_pending
1093 #define	softdep_worklist_tail		um_softdep->sd_worklist_tail
1094 #define	softdep_journal_pending		um_softdep->sd_journal_pending
1095 #define	softdep_journal_tail		um_softdep->sd_journal_tail
1096 #define	softdep_jblocks			um_softdep->sd_jblocks
1097 #define	softdep_unlinked		um_softdep->sd_unlinked
1098 #define	softdep_dirtycg			um_softdep->sd_dirtycg
1099 #define	softdep_mkdirlisthd		um_softdep->sd_mkdirlisthd
1100 #define	pagedep_hashtbl			um_softdep->sd_pdhash
1101 #define	pagedep_hash_size		um_softdep->sd_pdhashsize
1102 #define	pagedep_nextclean		um_softdep->sd_pdnextclean
1103 #define	inodedep_hashtbl		um_softdep->sd_idhash
1104 #define	inodedep_hash_size		um_softdep->sd_idhashsize
1105 #define	inodedep_nextclean		um_softdep->sd_idnextclean
1106 #define	newblk_hashtbl			um_softdep->sd_newblkhash
1107 #define	newblk_hash_size		um_softdep->sd_newblkhashsize
1108 #define	bmsafemap_hashtbl		um_softdep->sd_bmhash
1109 #define	bmsafemap_hash_size		um_softdep->sd_bmhashsize
1110 #define	indir_hashtbl			um_softdep->sd_indirhash
1111 #define	indir_hash_size			um_softdep->sd_indirhashsize
1112 #define	softdep_on_journal		um_softdep->sd_on_journal
1113 #define	softdep_on_worklist		um_softdep->sd_on_worklist
1114 #define	softdep_deps			um_softdep->sd_deps
1115 #define	softdep_accdeps			um_softdep->sd_accdeps
1116 #define	softdep_req			um_softdep->sd_req
1117 #define	softdep_flags			um_softdep->sd_flags
1118 #define	softdep_flushtd			um_softdep->sd_flushtd
1119 #define	softdep_curdeps			um_softdep->sd_curdeps
1120 #define	softdep_alldeps			um_softdep->sd_alldeps
1121