xref: /freebsd/sys/ufs/ffs/softdep.h (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /*
2  * Copyright 1998, 2000 Marshall Kirk McKusick. All Rights Reserved.
3  *
4  * The soft updates code is derived from the appendix of a University
5  * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
6  * "Soft Updates: A Solution to the Metadata Update Problem in File
7  * Systems", CSE-TR-254-95, August 1995).
8  *
9  * Further information about soft updates can be obtained from:
10  *
11  *	Marshall Kirk McKusick		http://www.mckusick.com/softdep/
12  *	1614 Oxford Street		mckusick@mckusick.com
13  *	Berkeley, CA 94709-1608		+1-510-843-9542
14  *	USA
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED.  IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
30  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)softdep.h	9.7 (McKusick) 6/21/00
39  * $FreeBSD$
40  */
41 
42 #include <sys/queue.h>
43 
44 /*
45  * Allocation dependencies are handled with undo/redo on the in-memory
46  * copy of the data. A particular data dependency is eliminated when
47  * it is ALLCOMPLETE: that is ATTACHED, DEPCOMPLETE, and COMPLETE.
48  *
49  * ATTACHED means that the data is not currently being written to
50  * disk. UNDONE means that the data has been rolled back to a safe
51  * state for writing to the disk. When the I/O completes, the data is
52  * restored to its current form and the state reverts to ATTACHED.
53  * The data must be locked throughout the rollback, I/O, and roll
54  * forward so that the rolled back information is never visible to
55  * user processes. The COMPLETE flag indicates that the item has been
56  * written. For example, a dependency that requires that an inode be
57  * written will be marked COMPLETE after the inode has been written
58  * to disk. The DEPCOMPLETE flag indicates the completion of any other
59  * dependencies such as the writing of a cylinder group map has been
60  * completed. A dependency structure may be freed only when both it
61  * and its dependencies have completed and any rollbacks that are in
62  * progress have finished as indicated by the set of ALLCOMPLETE flags
63  * all being set. The two MKDIR flags indicate additional dependencies
64  * that must be done when creating a new directory. MKDIR_BODY is
65  * cleared when the directory data block containing the "." and ".."
66  * entries has been written. MKDIR_PARENT is cleared when the parent
67  * inode with the increased link count for ".." has been written. When
68  * both MKDIR flags have been cleared, the DEPCOMPLETE flag is set to
69  * indicate that the directory dependencies have been completed. The
70  * writing of the directory inode itself sets the COMPLETE flag which
71  * then allows the directory entry for the new directory to be written
72  * to disk. The RMDIR flag marks a dirrem structure as representing
73  * the removal of a directory rather than a file. When the removal
74  * dependencies are completed, additional work needs to be done
75  * (truncation of the "." and ".." entries, an additional decrement
76  * of the associated inode, and a decrement of the parent inode). The
77  * DIRCHG flag marks a diradd structure as representing the changing
78  * of an existing entry rather than the addition of a new one. When
79  * the update is complete the dirrem associated with the inode for
80  * the old name must be added to the worklist to do the necessary
81  * reference count decrement. The GOINGAWAY flag indicates that the
82  * data structure is frozen from further change until its dependencies
83  * have been completed and its resources freed after which it will be
84  * discarded. The IOSTARTED flag prevents multiple calls to the I/O
85  * start routine from doing multiple rollbacks. The SPACECOUNTED flag
86  * says that the files space has been accounted to the pending free
87  * space count. The NEWBLOCK flag marks pagedep structures that have
88  * just been allocated, so must be claimed by the inode before all
89  * dependencies are complete. The ONWORKLIST flag shows whether the
90  * structure is currently linked onto a worklist.
91  */
92 #define	ATTACHED	0x0001
93 #define	UNDONE		0x0002
94 #define	COMPLETE	0x0004
95 #define	DEPCOMPLETE	0x0008
96 #define	MKDIR_PARENT	0x0010	/* diradd & mkdir only */
97 #define	MKDIR_BODY	0x0020	/* diradd & mkdir only */
98 #define	RMDIR		0x0040	/* dirrem only */
99 #define	DIRCHG		0x0080	/* diradd & dirrem only */
100 #define	GOINGAWAY	0x0100	/* indirdep only */
101 #define	IOSTARTED	0x0200	/* inodedep & pagedep only */
102 #define	SPACECOUNTED	0x0400	/* inodedep only */
103 #define	NEWBLOCK	0x0800	/* pagedep only */
104 #define ONWORKLIST	0x8000
105 
106 #define	ALLCOMPLETE	(ATTACHED | COMPLETE | DEPCOMPLETE)
107 
108 /*
109  * The workitem queue.
110  *
111  * It is sometimes useful and/or necessary to clean up certain dependencies
112  * in the background rather than during execution of an application process
113  * or interrupt service routine. To realize this, we append dependency
114  * structures corresponding to such tasks to a "workitem" queue. In a soft
115  * updates implementation, most pending workitems should not wait for more
116  * than a couple of seconds, so the filesystem syncer process awakens once
117  * per second to process the items on the queue.
118  */
119 
120 /* LIST_HEAD(workhead, worklist);	-- declared in buf.h */
121 
122 /*
123  * Each request can be linked onto a work queue through its worklist structure.
124  * To avoid the need for a pointer to the structure itself, this structure
125  * MUST be declared FIRST in each type in which it appears! If more than one
126  * worklist is needed in the structure, then a wk_data field must be added
127  * and the macros below changed to use it.
128  */
129 struct worklist {
130 	LIST_ENTRY(worklist)	wk_list;	/* list of work requests */
131 	unsigned short		wk_type;	/* type of request */
132 	unsigned short		wk_state;	/* state flags */
133 };
134 #define WK_DATA(wk) ((void *)(wk))
135 #define WK_PAGEDEP(wk) ((struct pagedep *)(wk))
136 #define WK_INODEDEP(wk) ((struct inodedep *)(wk))
137 #define WK_NEWBLK(wk) ((struct newblk *)(wk))
138 #define WK_BMSAFEMAP(wk) ((struct bmsafemap *)(wk))
139 #define WK_ALLOCDIRECT(wk) ((struct allocdirect *)(wk))
140 #define WK_INDIRDEP(wk) ((struct indirdep *)(wk))
141 #define WK_ALLOCINDIR(wk) ((struct allocindir *)(wk))
142 #define WK_FREEFRAG(wk) ((struct freefrag *)(wk))
143 #define WK_FREEBLKS(wk) ((struct freeblks *)(wk))
144 #define WK_FREEFILE(wk) ((struct freefile *)(wk))
145 #define WK_DIRADD(wk) ((struct diradd *)(wk))
146 #define WK_MKDIR(wk) ((struct mkdir *)(wk))
147 #define WK_DIRREM(wk) ((struct dirrem *)(wk))
148 #define WK_NEWDIRBLK(wk) ((struct newdirblk *)(wk))
149 
150 /*
151  * Various types of lists
152  */
153 LIST_HEAD(dirremhd, dirrem);
154 LIST_HEAD(diraddhd, diradd);
155 LIST_HEAD(newblkhd, newblk);
156 LIST_HEAD(inodedephd, inodedep);
157 LIST_HEAD(allocindirhd, allocindir);
158 LIST_HEAD(allocdirecthd, allocdirect);
159 TAILQ_HEAD(allocdirectlst, allocdirect);
160 
161 /*
162  * The "pagedep" structure tracks the various dependencies related to
163  * a particular directory page. If a directory page has any dependencies,
164  * it will have a pagedep linked to its associated buffer. The
165  * pd_dirremhd list holds the list of dirrem requests which decrement
166  * inode reference counts. These requests are processed after the
167  * directory page with the corresponding zero'ed entries has been
168  * written. The pd_diraddhd list maintains the list of diradd requests
169  * which cannot be committed until their corresponding inode has been
170  * written to disk. Because a directory may have many new entries
171  * being created, several lists are maintained hashed on bits of the
172  * offset of the entry into the directory page to keep the lists from
173  * getting too long. Once a new directory entry has been cleared to
174  * be written, it is moved to the pd_pendinghd list. After the new
175  * entry has been written to disk it is removed from the pd_pendinghd
176  * list, any removed operations are done, and the dependency structure
177  * is freed.
178  */
179 #define DAHASHSZ 6
180 #define DIRADDHASH(offset) (((offset) >> 2) % DAHASHSZ)
181 struct pagedep {
182 	struct	worklist pd_list;	/* page buffer */
183 #	define	pd_state pd_list.wk_state /* check for multiple I/O starts */
184 	LIST_ENTRY(pagedep) pd_hash;	/* hashed lookup */
185 	struct	mount *pd_mnt;		/* associated mount point */
186 	ino_t	pd_ino;			/* associated file */
187 	ufs_lbn_t pd_lbn;		/* block within file */
188 	struct	dirremhd pd_dirremhd;	/* dirrem's waiting for page */
189 	struct	diraddhd pd_diraddhd[DAHASHSZ]; /* diradd dir entry updates */
190 	struct	diraddhd pd_pendinghd;	/* directory entries awaiting write */
191 };
192 
193 /*
194  * The "inodedep" structure tracks the set of dependencies associated
195  * with an inode. One task that it must manage is delayed operations
196  * (i.e., work requests that must be held until the inodedep's associated
197  * inode has been written to disk). Getting an inode from its incore
198  * state to the disk requires two steps to be taken by the filesystem
199  * in this order: first the inode must be copied to its disk buffer by
200  * the VOP_UPDATE operation; second the inode's buffer must be written
201  * to disk. To ensure that both operations have happened in the required
202  * order, the inodedep maintains two lists. Delayed operations are
203  * placed on the id_inowait list. When the VOP_UPDATE is done, all
204  * operations on the id_inowait list are moved to the id_bufwait list.
205  * When the buffer is written, the items on the id_bufwait list can be
206  * safely moved to the work queue to be processed. A second task of the
207  * inodedep structure is to track the status of block allocation within
208  * the inode.  Each block that is allocated is represented by an
209  * "allocdirect" structure (see below). It is linked onto the id_newinoupdt
210  * list until both its contents and its allocation in the cylinder
211  * group map have been written to disk. Once these dependencies have been
212  * satisfied, it is removed from the id_newinoupdt list and any followup
213  * actions such as releasing the previous block or fragment are placed
214  * on the id_inowait list. When an inode is updated (a VOP_UPDATE is
215  * done), the "inodedep" structure is linked onto the buffer through
216  * its worklist. Thus, it will be notified when the buffer is about
217  * to be written and when it is done. At the update time, all the
218  * elements on the id_newinoupdt list are moved to the id_inoupdt list
219  * since those changes are now relevant to the copy of the inode in the
220  * buffer. Also at update time, the tasks on the id_inowait list are
221  * moved to the id_bufwait list so that they will be executed when
222  * the updated inode has been written to disk. When the buffer containing
223  * the inode is written to disk, any updates listed on the id_inoupdt
224  * list are rolled back as they are not yet safe. Following the write,
225  * the changes are once again rolled forward and any actions on the
226  * id_bufwait list are processed (since those actions are now safe).
227  * The entries on the id_inoupdt and id_newinoupdt lists must be kept
228  * sorted by logical block number to speed the calculation of the size
229  * of the rolled back inode (see explanation in initiate_write_inodeblock).
230  * When a directory entry is created, it is represented by a diradd.
231  * The diradd is added to the id_inowait list as it cannot be safely
232  * written to disk until the inode that it represents is on disk. After
233  * the inode is written, the id_bufwait list is processed and the diradd
234  * entries are moved to the id_pendinghd list where they remain until
235  * the directory block containing the name has been written to disk.
236  * The purpose of keeping the entries on the id_pendinghd list is so that
237  * the softdep_fsync function can find and push the inode's directory
238  * name(s) as part of the fsync operation for that file.
239  */
240 struct inodedep {
241 	struct	worklist id_list;	/* buffer holding inode block */
242 #	define	id_state id_list.wk_state /* inode dependency state */
243 	LIST_ENTRY(inodedep) id_hash;	/* hashed lookup */
244 	struct	fs *id_fs;		/* associated filesystem */
245 	ino_t	id_ino;			/* dependent inode */
246 	nlink_t	id_nlinkdelta;		/* saved effective link count */
247 	struct	dinode *id_savedino;	/* saved dinode contents */
248 	LIST_ENTRY(inodedep) id_deps;	/* bmsafemap's list of inodedep's */
249 	struct	buf *id_buf;		/* related bmsafemap (if pending) */
250 	off_t	id_savedsize;		/* file size saved during rollback */
251 	struct	workhead id_pendinghd;	/* entries awaiting directory write */
252 	struct	workhead id_bufwait;	/* operations after inode written */
253 	struct	workhead id_inowait;	/* operations waiting inode update */
254 	struct	allocdirectlst id_inoupdt; /* updates before inode written */
255 	struct	allocdirectlst id_newinoupdt; /* updates when inode written */
256 };
257 
258 /*
259  * A "newblk" structure is attached to a bmsafemap structure when a block
260  * or fragment is allocated from a cylinder group. Its state is set to
261  * DEPCOMPLETE when its cylinder group map is written. It is consumed by
262  * an associated allocdirect or allocindir allocation which will attach
263  * themselves to the bmsafemap structure if the newblk's DEPCOMPLETE flag
264  * is not set (i.e., its cylinder group map has not been written).
265  */
266 struct newblk {
267 	LIST_ENTRY(newblk) nb_hash;	/* hashed lookup */
268 	struct	fs *nb_fs;		/* associated filesystem */
269 	ufs_daddr_t nb_newblkno;	/* allocated block number */
270 	int	nb_state;		/* state of bitmap dependency */
271 	LIST_ENTRY(newblk) nb_deps;	/* bmsafemap's list of newblk's */
272 	struct	bmsafemap *nb_bmsafemap; /* associated bmsafemap */
273 };
274 
275 /*
276  * A "bmsafemap" structure maintains a list of dependency structures
277  * that depend on the update of a particular cylinder group map.
278  * It has lists for newblks, allocdirects, allocindirs, and inodedeps.
279  * It is attached to the buffer of a cylinder group block when any of
280  * these things are allocated from the cylinder group. It is freed
281  * after the cylinder group map is written and the state of its
282  * dependencies are updated with DEPCOMPLETE to indicate that it has
283  * been processed.
284  */
285 struct bmsafemap {
286 	struct	worklist sm_list;	/* cylgrp buffer */
287 	struct	buf *sm_buf;		/* associated buffer */
288 	struct	allocdirecthd sm_allocdirecthd; /* allocdirect deps */
289 	struct	allocindirhd sm_allocindirhd; /* allocindir deps */
290 	struct	inodedephd sm_inodedephd; /* inodedep deps */
291 	struct	newblkhd sm_newblkhd;	/* newblk deps */
292 };
293 
294 /*
295  * An "allocdirect" structure is attached to an "inodedep" when a new block
296  * or fragment is allocated and pointed to by the inode described by
297  * "inodedep". The worklist is linked to the buffer that holds the block.
298  * When the block is first allocated, it is linked to the bmsafemap
299  * structure associated with the buffer holding the cylinder group map
300  * from which it was allocated. When the cylinder group map is written
301  * to disk, ad_state has the DEPCOMPLETE flag set. When the block itself
302  * is written, the COMPLETE flag is set. Once both the cylinder group map
303  * and the data itself have been written, it is safe to write the inode
304  * that claims the block. If there was a previous fragment that had been
305  * allocated before the file was increased in size, the old fragment may
306  * be freed once the inode claiming the new block is written to disk.
307  * This ad_fragfree request is attached to the id_inowait list of the
308  * associated inodedep (pointed to by ad_inodedep) for processing after
309  * the inode is written. When a block is allocated to a directory, an
310  * fsync of a file whose name is within that block must ensure not only
311  * that the block containing the file name has been written, but also
312  * that the on-disk inode references that block. When a new directory
313  * block is created, we allocate a newdirblk structure which is linked
314  * to the associated allocdirect (on its ad_newdirblk list). When the
315  * allocdirect has been satisfied, the newdirblk structure is moved to
316  * the inodedep id_bufwait list of its directory to await the inode
317  * being written. When the inode is written, the directory entries are
318  * fully committed and can be deleted from their pagedep->id_pendinghd
319  * and inodedep->id_pendinghd lists.
320  */
321 struct allocdirect {
322 	struct	worklist ad_list;	/* buffer holding block */
323 #	define	ad_state ad_list.wk_state /* block pointer state */
324 	TAILQ_ENTRY(allocdirect) ad_next; /* inodedep's list of allocdirect's */
325 	ufs_lbn_t ad_lbn;		/* block within file */
326 	ufs_daddr_t ad_newblkno;	/* new value of block pointer */
327 	ufs_daddr_t ad_oldblkno;	/* old value of block pointer */
328 	long	ad_newsize;		/* size of new block */
329 	long	ad_oldsize;		/* size of old block */
330 	LIST_ENTRY(allocdirect) ad_deps; /* bmsafemap's list of allocdirect's */
331 	struct	buf *ad_buf;		/* cylgrp buffer (if pending) */
332 	struct	inodedep *ad_inodedep;	/* associated inodedep */
333 	struct	freefrag *ad_freefrag;	/* fragment to be freed (if any) */
334 	struct	workhead ad_newdirblk;	/* dir block to notify when written */
335 };
336 
337 /*
338  * A single "indirdep" structure manages all allocation dependencies for
339  * pointers in an indirect block. The up-to-date state of the indirect
340  * block is stored in ir_savedata. The set of pointers that may be safely
341  * written to the disk is stored in ir_safecopy. The state field is used
342  * only to track whether the buffer is currently being written (in which
343  * case it is not safe to update ir_safecopy). Ir_deplisthd contains the
344  * list of allocindir structures, one for each block that needs to be
345  * written to disk. Once the block and its bitmap allocation have been
346  * written the safecopy can be updated to reflect the allocation and the
347  * allocindir structure freed. If ir_state indicates that an I/O on the
348  * indirect block is in progress when ir_safecopy is to be updated, the
349  * update is deferred by placing the allocindir on the ir_donehd list.
350  * When the I/O on the indirect block completes, the entries on the
351  * ir_donehd list are processed by updating their corresponding ir_safecopy
352  * pointers and then freeing the allocindir structure.
353  */
354 struct indirdep {
355 	struct	worklist ir_list;	/* buffer holding indirect block */
356 #	define	ir_state ir_list.wk_state /* indirect block pointer state */
357 	caddr_t ir_saveddata;		/* buffer cache contents */
358 	struct	buf *ir_savebp;		/* buffer holding safe copy */
359 	struct	allocindirhd ir_donehd;	/* done waiting to update safecopy */
360 	struct	allocindirhd ir_deplisthd; /* allocindir deps for this block */
361 };
362 
363 /*
364  * An "allocindir" structure is attached to an "indirdep" when a new block
365  * is allocated and pointed to by the indirect block described by the
366  * "indirdep". The worklist is linked to the buffer that holds the new block.
367  * When the block is first allocated, it is linked to the bmsafemap
368  * structure associated with the buffer holding the cylinder group map
369  * from which it was allocated. When the cylinder group map is written
370  * to disk, ai_state has the DEPCOMPLETE flag set. When the block itself
371  * is written, the COMPLETE flag is set. Once both the cylinder group map
372  * and the data itself have been written, it is safe to write the entry in
373  * the indirect block that claims the block; the "allocindir" dependency
374  * can then be freed as it is no longer applicable.
375  */
376 struct allocindir {
377 	struct	worklist ai_list;	/* buffer holding indirect block */
378 #	define	ai_state ai_list.wk_state /* indirect block pointer state */
379 	LIST_ENTRY(allocindir) ai_next;	/* indirdep's list of allocindir's */
380 	int	ai_offset;		/* pointer offset in indirect block */
381 	ufs_daddr_t ai_newblkno;	/* new block pointer value */
382 	ufs_daddr_t ai_oldblkno;	/* old block pointer value */
383 	struct	freefrag *ai_freefrag;	/* block to be freed when complete */
384 	struct	indirdep *ai_indirdep;	/* address of associated indirdep */
385 	LIST_ENTRY(allocindir) ai_deps;	/* bmsafemap's list of allocindir's */
386 	struct	buf *ai_buf;		/* cylgrp buffer (if pending) */
387 };
388 
389 /*
390  * A "freefrag" structure is attached to an "inodedep" when a previously
391  * allocated fragment is replaced with a larger fragment, rather than extended.
392  * The "freefrag" structure is constructed and attached when the replacement
393  * block is first allocated. It is processed after the inode claiming the
394  * bigger block that replaces it has been written to disk. Note that the
395  * ff_state field is is used to store the uid, so may lose data. However,
396  * the uid is used only in printing an error message, so is not critical.
397  * Keeping it in a short keeps the data structure down to 32 bytes.
398  */
399 struct freefrag {
400 	struct	worklist ff_list;	/* id_inowait or delayed worklist */
401 #	define	ff_state ff_list.wk_state /* owning user; should be uid_t */
402 	struct	vnode *ff_devvp;	/* filesystem device vnode */
403 	struct	mount *ff_mnt;		/* associated mount point */
404 	ufs_daddr_t ff_blkno;		/* fragment physical block number */
405 	long	ff_fragsize;		/* size of fragment being deleted */
406 	ino_t	ff_inum;		/* owning inode number */
407 };
408 
409 /*
410  * A "freeblks" structure is attached to an "inodedep" when the
411  * corresponding file's length is reduced to zero. It records all
412  * the information needed to free the blocks of a file after its
413  * zero'ed inode has been written to disk.
414  */
415 struct freeblks {
416 	struct	worklist fb_list;	/* id_inowait or delayed worklist */
417 	ino_t	fb_previousinum;	/* inode of previous owner of blocks */
418 	struct	vnode *fb_devvp;	/* filesystem device vnode */
419 	struct	mount *fb_mnt;		/* associated mount point */
420 	off_t	fb_oldsize;		/* previous file size */
421 	off_t	fb_newsize;		/* new file size */
422 	int	fb_chkcnt;		/* used to check cnt of blks released */
423 	uid_t	fb_uid;			/* uid of previous owner of blocks */
424 	ufs_daddr_t fb_dblks[NDADDR];	/* direct blk ptrs to deallocate */
425 	ufs_daddr_t fb_iblks[NIADDR];	/* indirect blk ptrs to deallocate */
426 };
427 
428 /*
429  * A "freefile" structure is attached to an inode when its
430  * link count is reduced to zero. It marks the inode as free in
431  * the cylinder group map after the zero'ed inode has been written
432  * to disk and any associated blocks and fragments have been freed.
433  */
434 struct freefile {
435 	struct	worklist fx_list;	/* id_inowait or delayed worklist */
436 	mode_t	fx_mode;		/* mode of inode */
437 	ino_t	fx_oldinum;		/* inum of the unlinked file */
438 	struct	vnode *fx_devvp;	/* filesystem device vnode */
439 	struct	mount *fx_mnt;		/* associated mount point */
440 };
441 
442 /*
443  * A "diradd" structure is linked to an "inodedep" id_inowait list when a
444  * new directory entry is allocated that references the inode described
445  * by "inodedep". When the inode itself is written (either the initial
446  * allocation for new inodes or with the increased link count for
447  * existing inodes), the COMPLETE flag is set in da_state. If the entry
448  * is for a newly allocated inode, the "inodedep" structure is associated
449  * with a bmsafemap which prevents the inode from being written to disk
450  * until the cylinder group has been updated. Thus the da_state COMPLETE
451  * flag cannot be set until the inode bitmap dependency has been removed.
452  * When creating a new file, it is safe to write the directory entry that
453  * claims the inode once the referenced inode has been written. Since
454  * writing the inode clears the bitmap dependencies, the DEPCOMPLETE flag
455  * in the diradd can be set unconditionally when creating a file. When
456  * creating a directory, there are two additional dependencies described by
457  * mkdir structures (see their description below). When these dependencies
458  * are resolved the DEPCOMPLETE flag is set in the diradd structure.
459  * If there are multiple links created to the same inode, there will be
460  * a separate diradd structure created for each link. The diradd is
461  * linked onto the pg_diraddhd list of the pagedep for the directory
462  * page that contains the entry. When a directory page is written,
463  * the pg_diraddhd list is traversed to rollback any entries that are
464  * not yet ready to be written to disk. If a directory entry is being
465  * changed (by rename) rather than added, the DIRCHG flag is set and
466  * the da_previous entry points to the entry that will be "removed"
467  * once the new entry has been committed. During rollback, entries
468  * with da_previous are replaced with the previous inode number rather
469  * than zero.
470  *
471  * The overlaying of da_pagedep and da_previous is done to keep the
472  * structure down to 32 bytes in size on a 32-bit machine. If a
473  * da_previous entry is present, the pointer to its pagedep is available
474  * in the associated dirrem entry. If the DIRCHG flag is set, the
475  * da_previous entry is valid; if not set the da_pagedep entry is valid.
476  * The DIRCHG flag never changes; it is set when the structure is created
477  * if appropriate and is never cleared.
478  */
479 struct diradd {
480 	struct	worklist da_list;	/* id_inowait or id_pendinghd list */
481 #	define	da_state da_list.wk_state /* state of the new directory entry */
482 	LIST_ENTRY(diradd) da_pdlist;	/* pagedep holding directory block */
483 	doff_t	da_offset;		/* offset of new dir entry in dir blk */
484 	ino_t	da_newinum;		/* inode number for the new dir entry */
485 	union {
486 	struct	dirrem *dau_previous;	/* entry being replaced in dir change */
487 	struct	pagedep *dau_pagedep;	/* pagedep dependency for addition */
488 	} da_un;
489 };
490 #define da_previous da_un.dau_previous
491 #define da_pagedep da_un.dau_pagedep
492 
493 /*
494  * Two "mkdir" structures are needed to track the additional dependencies
495  * associated with creating a new directory entry. Normally a directory
496  * addition can be committed as soon as the newly referenced inode has been
497  * written to disk with its increased link count. When a directory is
498  * created there are two additional dependencies: writing the directory
499  * data block containing the "." and ".." entries (MKDIR_BODY) and writing
500  * the parent inode with the increased link count for ".." (MKDIR_PARENT).
501  * These additional dependencies are tracked by two mkdir structures that
502  * reference the associated "diradd" structure. When they have completed,
503  * they set the DEPCOMPLETE flag on the diradd so that it knows that its
504  * extra dependencies have been completed. The md_state field is used only
505  * to identify which type of dependency the mkdir structure is tracking.
506  * It is not used in the mainline code for any purpose other than consistency
507  * checking. All the mkdir structures in the system are linked together on
508  * a list. This list is needed so that a diradd can find its associated
509  * mkdir structures and deallocate them if it is prematurely freed (as for
510  * example if a mkdir is immediately followed by a rmdir of the same directory).
511  * Here, the free of the diradd must traverse the list to find the associated
512  * mkdir structures that reference it. The deletion would be faster if the
513  * diradd structure were simply augmented to have two pointers that referenced
514  * the associated mkdir's. However, this would increase the size of the diradd
515  * structure from 32 to 64-bits to speed a very infrequent operation.
516  */
517 struct mkdir {
518 	struct	worklist md_list;	/* id_inowait or buffer holding dir */
519 #	define	md_state md_list.wk_state /* type: MKDIR_PARENT or MKDIR_BODY */
520 	struct	diradd *md_diradd;	/* associated diradd */
521 	struct	buf *md_buf;		/* MKDIR_BODY: buffer holding dir */
522 	LIST_ENTRY(mkdir) md_mkdirs;	/* list of all mkdirs */
523 };
524 LIST_HEAD(mkdirlist, mkdir) mkdirlisthd;
525 
526 /*
527  * A "dirrem" structure describes an operation to decrement the link
528  * count on an inode. The dirrem structure is attached to the pg_dirremhd
529  * list of the pagedep for the directory page that contains the entry.
530  * It is processed after the directory page with the deleted entry has
531  * been written to disk.
532  *
533  * The overlaying of dm_pagedep and dm_dirinum is done to keep the
534  * structure down to 32 bytes in size on a 32-bit machine. It works
535  * because they are never used concurrently.
536  */
537 struct dirrem {
538 	struct	worklist dm_list;	/* delayed worklist */
539 #	define	dm_state dm_list.wk_state /* state of the old directory entry */
540 	LIST_ENTRY(dirrem) dm_next;	/* pagedep's list of dirrem's */
541 	struct	mount *dm_mnt;		/* associated mount point */
542 	ino_t	dm_oldinum;		/* inum of the removed dir entry */
543 	union {
544 	struct	pagedep *dmu_pagedep;	/* pagedep dependency for remove */
545 	ino_t	dmu_dirinum;		/* parent inode number (for rmdir) */
546 	} dm_un;
547 };
548 #define dm_pagedep dm_un.dmu_pagedep
549 #define dm_dirinum dm_un.dmu_dirinum
550 
551 /*
552  * A "newdirblk" structure tracks the progress of a newly allocated
553  * directory block from its creation until it is claimed by its on-disk
554  * inode. When a block is allocated to a directory, an fsync of a file
555  * whose name is within that block must ensure not only that the block
556  * containing the file name has been written, but also that the on-disk
557  * inode references that block. When a new directory block is created,
558  * we allocate a newdirblk structure which is linked to the associated
559  * allocdirect (on its ad_newdirblk list). When the allocdirect has been
560  * satisfied, the newdirblk structure is moved to the inodedep id_bufwait
561  * list of its directory to await the inode being written. When the inode
562  * is written, the directory entries are fully committed and can be
563  * deleted from their pagedep->id_pendinghd and inodedep->id_pendinghd
564  * lists. Note that we could track directory blocks allocated to indirect
565  * blocks using a similar scheme with the allocindir structures. Rather
566  * than adding this level of complexity, we simply write those newly
567  * allocated indirect blocks synchronously as such allocations are rare.
568  */
569 struct newdirblk {
570 	struct	worklist db_list;	/* id_inowait or pg_newdirblk */
571 #	define	db_state db_list.wk_state /* unused */
572 	struct	pagedep *db_pagedep;	/* associated pagedep */
573 };
574