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