xref: /freebsd/sys/ufs/ffs/ffs_softdep.c (revision c37420b0d5b3b6ef875fbf0b84a13f6f09be56d6)
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  *	from: @(#)ffs_softdep.c	9.59 (McKusick) 6/21/00
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 /*
45  * For now we want the safety net that the DIAGNOSTIC and DEBUG flags provide.
46  */
47 #ifndef DIAGNOSTIC
48 #define DIAGNOSTIC
49 #endif
50 #ifndef DEBUG
51 #define DEBUG
52 #endif
53 
54 #include <sys/param.h>
55 #include <sys/kernel.h>
56 #include <sys/systm.h>
57 #include <sys/bio.h>
58 #include <sys/buf.h>
59 #include <sys/kdb.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/proc.h>
63 #include <sys/stat.h>
64 #include <sys/syslog.h>
65 #include <sys/vnode.h>
66 #include <sys/conf.h>
67 #include <ufs/ufs/dir.h>
68 #include <ufs/ufs/extattr.h>
69 #include <ufs/ufs/quota.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufsmount.h>
72 #include <ufs/ffs/fs.h>
73 #include <ufs/ffs/softdep.h>
74 #include <ufs/ffs/ffs_extern.h>
75 #include <ufs/ufs/ufs_extern.h>
76 
77 /*
78  * These definitions need to be adapted to the system to which
79  * this file is being ported.
80  */
81 /*
82  * malloc types defined for the softdep system.
83  */
84 static MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies");
85 static MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies");
86 static MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation");
87 static MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map");
88 static MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode");
89 static MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies");
90 static MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block");
91 static MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode");
92 static MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode");
93 static MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated");
94 static MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry");
95 static MALLOC_DEFINE(M_MKDIR, "mkdir","New directory");
96 static MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted");
97 static MALLOC_DEFINE(M_NEWDIRBLK, "newdirblk","Unclaimed new directory block");
98 
99 #define M_SOFTDEP_FLAGS	(M_WAITOK | M_USE_RESERVE)
100 
101 #define	D_PAGEDEP	0
102 #define	D_INODEDEP	1
103 #define	D_NEWBLK	2
104 #define	D_BMSAFEMAP	3
105 #define	D_ALLOCDIRECT	4
106 #define	D_INDIRDEP	5
107 #define	D_ALLOCINDIR	6
108 #define	D_FREEFRAG	7
109 #define	D_FREEBLKS	8
110 #define	D_FREEFILE	9
111 #define	D_DIRADD	10
112 #define	D_MKDIR		11
113 #define	D_DIRREM	12
114 #define	D_NEWDIRBLK	13
115 #define	D_LAST		D_NEWDIRBLK
116 
117 /*
118  * translate from workitem type to memory type
119  * MUST match the defines above, such that memtype[D_XXX] == M_XXX
120  */
121 static struct malloc_type *memtype[] = {
122 	M_PAGEDEP,
123 	M_INODEDEP,
124 	M_NEWBLK,
125 	M_BMSAFEMAP,
126 	M_ALLOCDIRECT,
127 	M_INDIRDEP,
128 	M_ALLOCINDIR,
129 	M_FREEFRAG,
130 	M_FREEBLKS,
131 	M_FREEFILE,
132 	M_DIRADD,
133 	M_MKDIR,
134 	M_DIRREM,
135 	M_NEWDIRBLK
136 };
137 
138 #define DtoM(type) (memtype[type])
139 
140 /*
141  * Names of malloc types.
142  */
143 #define TYPENAME(type)  \
144 	((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???")
145 /*
146  * End system adaptaion definitions.
147  */
148 
149 /*
150  * Internal function prototypes.
151  */
152 static	void softdep_error(char *, int);
153 static	void drain_output(struct vnode *, int);
154 static	struct buf *getdirtybuf(struct buf **, struct mtx *, int);
155 static	void clear_remove(struct thread *);
156 static	void clear_inodedeps(struct thread *);
157 static	int flush_pagedep_deps(struct vnode *, struct mount *,
158 	    struct diraddhd *);
159 static	int flush_inodedep_deps(struct fs *, ino_t);
160 static	int flush_deplist(struct allocdirectlst *, int, int *);
161 static	int handle_written_filepage(struct pagedep *, struct buf *);
162 static  void diradd_inode_written(struct diradd *, struct inodedep *);
163 static	int handle_written_inodeblock(struct inodedep *, struct buf *);
164 static	void handle_allocdirect_partdone(struct allocdirect *);
165 static	void handle_allocindir_partdone(struct allocindir *);
166 static	void initiate_write_filepage(struct pagedep *, struct buf *);
167 static	void handle_written_mkdir(struct mkdir *, int);
168 static	void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
169 static	void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
170 static	void handle_workitem_freefile(struct freefile *);
171 static	void handle_workitem_remove(struct dirrem *, struct vnode *);
172 static	struct dirrem *newdirrem(struct buf *, struct inode *,
173 	    struct inode *, int, struct dirrem **);
174 static	void free_diradd(struct diradd *);
175 static	void free_allocindir(struct allocindir *, struct inodedep *);
176 static	void free_newdirblk(struct newdirblk *);
177 static	int indir_trunc(struct freeblks *, ufs2_daddr_t, int, ufs_lbn_t,
178 	    ufs2_daddr_t *);
179 static	void deallocate_dependencies(struct buf *, struct inodedep *);
180 static	void free_allocdirect(struct allocdirectlst *,
181 	    struct allocdirect *, int);
182 static	int check_inode_unwritten(struct inodedep *);
183 static	int free_inodedep(struct inodedep *);
184 static	void handle_workitem_freeblocks(struct freeblks *, int);
185 static	void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
186 static	void setup_allocindir_phase2(struct buf *, struct inode *,
187 	    struct allocindir *);
188 static	struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
189 	    ufs2_daddr_t);
190 static	void handle_workitem_freefrag(struct freefrag *);
191 static	struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long);
192 static	void allocdirect_merge(struct allocdirectlst *,
193 	    struct allocdirect *, struct allocdirect *);
194 static	struct bmsafemap *bmsafemap_lookup(struct buf *);
195 static	int newblk_lookup(struct fs *, ufs2_daddr_t, int, struct newblk **);
196 static	int inodedep_lookup(struct fs *, ino_t, int, struct inodedep **);
197 static	int pagedep_lookup(struct inode *, ufs_lbn_t, int, struct pagedep **);
198 static	void pause_timer(void *);
199 static	int request_cleanup(int, int);
200 static	int process_worklist_item(struct mount *, int);
201 static	void add_to_worklist(struct worklist *);
202 
203 /*
204  * Exported softdep operations.
205  */
206 static	int softdep_disk_prewrite(struct vnode *vp, struct buf *bp);
207 static	void softdep_disk_io_initiation(struct buf *);
208 static	void softdep_disk_write_complete(struct buf *);
209 static	void softdep_deallocate_dependencies(struct buf *);
210 static	void softdep_move_dependencies(struct buf *, struct buf *);
211 static	int softdep_count_dependencies(struct buf *bp, int);
212 
213 /*
214  * Locking primitives.
215  *
216  * For a uniprocessor, all we need to do is protect against disk
217  * interrupts. For a multiprocessor, this lock would have to be
218  * a mutex. A single mutex is used throughout this file, though
219  * finer grain locking could be used if contention warranted it.
220  *
221  * For a multiprocessor, the sleep call would accept a lock and
222  * release it after the sleep processing was complete. In a uniprocessor
223  * implementation there is no such interlock, so we simple mark
224  * the places where it needs to be done with the `interlocked' form
225  * of the lock calls. Since the uniprocessor sleep already interlocks
226  * the spl, there is nothing that really needs to be done.
227  */
228 #ifndef /* NOT */ DEBUG
229 static struct lockit {
230 	int	lkt_spl;
231 } lk = { 0 };
232 #define ACQUIRE_LOCK(lk)		(lk)->lkt_spl = splbio()
233 #define FREE_LOCK(lk)			splx((lk)->lkt_spl)
234 
235 #else /* DEBUG */
236 #define NOHOLDER	((struct thread *)-1)
237 #define SPECIAL_FLAG	((struct thread *)-2)
238 static struct lockit {
239 	int	lkt_spl;
240 	struct	thread *lkt_held;
241 } lk = { 0, NOHOLDER };
242 
243 static	void acquire_lock(struct lockit *);
244 static	void free_lock(struct lockit *);
245 void	softdep_panic(char *);
246 
247 #define ACQUIRE_LOCK(lk)		acquire_lock(lk)
248 #define FREE_LOCK(lk)			free_lock(lk)
249 
250 static void
251 acquire_lock(lk)
252 	struct lockit *lk;
253 {
254 	struct thread *holder;
255 
256 	if (lk->lkt_held != NOHOLDER) {
257 		holder = lk->lkt_held;
258 		FREE_LOCK(lk);
259 		if (holder == curthread)
260 			panic("softdep_lock: locking against myself");
261 		else
262 			panic("softdep_lock: lock held by %p", holder);
263 	}
264 	lk->lkt_spl = splbio();
265 	lk->lkt_held = curthread;
266 }
267 
268 static void
269 free_lock(lk)
270 	struct lockit *lk;
271 {
272 
273 	if (lk->lkt_held == NOHOLDER)
274 		panic("softdep_unlock: lock not held");
275 	lk->lkt_held = NOHOLDER;
276 	splx(lk->lkt_spl);
277 }
278 
279 /*
280  * Function to release soft updates lock and panic.
281  */
282 void
283 softdep_panic(msg)
284 	char *msg;
285 {
286 
287 	if (lk.lkt_held != NOHOLDER)
288 		FREE_LOCK(&lk);
289 	panic(msg);
290 }
291 #endif /* DEBUG */
292 
293 static	int interlocked_sleep(struct lockit *, int, void *, struct mtx *, int,
294 	    const char *, int);
295 
296 /*
297  * When going to sleep, we must save our SPL so that it does
298  * not get lost if some other process uses the lock while we
299  * are sleeping. We restore it after we have slept. This routine
300  * wraps the interlocking with functions that sleep. The list
301  * below enumerates the available set of operations.
302  */
303 #define	UNKNOWN		0
304 #define	SLEEP		1
305 #define	LOCKBUF		2
306 
307 static int
308 interlocked_sleep(lk, op, ident, mtx, flags, wmesg, timo)
309 	struct lockit *lk;
310 	int op;
311 	void *ident;
312 	struct mtx *mtx;
313 	int flags;
314 	const char *wmesg;
315 	int timo;
316 {
317 	struct thread *holder;
318 	int s, retval;
319 
320 	s = lk->lkt_spl;
321 #	ifdef DEBUG
322 	if (lk->lkt_held == NOHOLDER)
323 		panic("interlocked_sleep: lock not held");
324 	lk->lkt_held = NOHOLDER;
325 #	endif /* DEBUG */
326 	switch (op) {
327 	case SLEEP:
328 		retval = msleep(ident, mtx, flags, wmesg, timo);
329 		break;
330 	case LOCKBUF:
331 		retval = BUF_LOCK((struct buf *)ident, flags, mtx);
332 		break;
333 	default:
334 		panic("interlocked_sleep: unknown operation");
335 	}
336 #	ifdef DEBUG
337 	if (lk->lkt_held != NOHOLDER) {
338 		holder = lk->lkt_held;
339 		FREE_LOCK(lk);
340 		if (holder == curthread)
341 			panic("interlocked_sleep: locking against self");
342 		else
343 			panic("interlocked_sleep: lock held by %p", holder);
344 	}
345 	lk->lkt_held = curthread;
346 #	endif /* DEBUG */
347 	lk->lkt_spl = s;
348 	return (retval);
349 }
350 
351 /*
352  * Place holder for real semaphores.
353  */
354 struct sema {
355 	int	value;
356 	struct	thread *holder;
357 	char	*name;
358 	int	prio;
359 	int	timo;
360 };
361 static	void sema_init(struct sema *, char *, int, int);
362 static	int sema_get(struct sema *, struct lockit *);
363 static	void sema_release(struct sema *);
364 
365 static void
366 sema_init(semap, name, prio, timo)
367 	struct sema *semap;
368 	char *name;
369 	int prio, timo;
370 {
371 
372 	semap->holder = NOHOLDER;
373 	semap->value = 0;
374 	semap->name = name;
375 	semap->prio = prio;
376 	semap->timo = timo;
377 }
378 
379 static int
380 sema_get(semap, interlock)
381 	struct sema *semap;
382 	struct lockit *interlock;
383 {
384 
385 	if (semap->value++ > 0) {
386 		if (interlock != NULL) {
387 			interlocked_sleep(interlock, SLEEP, (caddr_t)semap,
388 			    NULL, semap->prio, semap->name,
389 			    semap->timo);
390 			FREE_LOCK(interlock);
391 		} else {
392 			tsleep(semap, semap->prio, semap->name,
393 			    semap->timo);
394 		}
395 		return (0);
396 	}
397 	semap->holder = curthread;
398 	if (interlock != NULL)
399 		FREE_LOCK(interlock);
400 	return (1);
401 }
402 
403 static void
404 sema_release(semap)
405 	struct sema *semap;
406 {
407 
408 	if (semap->value <= 0 || semap->holder != curthread) {
409 		if (lk.lkt_held != NOHOLDER)
410 			FREE_LOCK(&lk);
411 		panic("sema_release: not held");
412 	}
413 	if (--semap->value > 0) {
414 		semap->value = 0;
415 		wakeup(semap);
416 	}
417 	semap->holder = NOHOLDER;
418 }
419 
420 /*
421  * Worklist queue management.
422  * These routines require that the lock be held.
423  */
424 #ifndef /* NOT */ DEBUG
425 #define WORKLIST_INSERT(head, item) do {	\
426 	(item)->wk_state |= ONWORKLIST;		\
427 	LIST_INSERT_HEAD(head, item, wk_list);	\
428 } while (0)
429 #define WORKLIST_REMOVE(item) do {		\
430 	(item)->wk_state &= ~ONWORKLIST;	\
431 	LIST_REMOVE(item, wk_list);		\
432 } while (0)
433 #define WORKITEM_FREE(item, type) FREE(item, DtoM(type))
434 
435 #else /* DEBUG */
436 static	void worklist_insert(struct workhead *, struct worklist *);
437 static	void worklist_remove(struct worklist *);
438 static	void workitem_free(struct worklist *, int);
439 
440 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
441 #define WORKLIST_REMOVE(item) worklist_remove(item)
442 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)item, type)
443 
444 static void
445 worklist_insert(head, item)
446 	struct workhead *head;
447 	struct worklist *item;
448 {
449 
450 	if (lk.lkt_held == NOHOLDER)
451 		panic("worklist_insert: lock not held");
452 	if (item->wk_state & ONWORKLIST) {
453 		FREE_LOCK(&lk);
454 		panic("worklist_insert: already on list");
455 	}
456 	item->wk_state |= ONWORKLIST;
457 	LIST_INSERT_HEAD(head, item, wk_list);
458 }
459 
460 static void
461 worklist_remove(item)
462 	struct worklist *item;
463 {
464 
465 	if (lk.lkt_held == NOHOLDER)
466 		panic("worklist_remove: lock not held");
467 	if ((item->wk_state & ONWORKLIST) == 0) {
468 		FREE_LOCK(&lk);
469 		panic("worklist_remove: not on list");
470 	}
471 	item->wk_state &= ~ONWORKLIST;
472 	LIST_REMOVE(item, wk_list);
473 }
474 
475 static void
476 workitem_free(item, type)
477 	struct worklist *item;
478 	int type;
479 {
480 
481 	if (item->wk_state & ONWORKLIST) {
482 		if (lk.lkt_held != NOHOLDER)
483 			FREE_LOCK(&lk);
484 		panic("workitem_free: still on list");
485 	}
486 	if (item->wk_type != type) {
487 		if (lk.lkt_held != NOHOLDER)
488 			FREE_LOCK(&lk);
489 		panic("workitem_free: type mismatch");
490 	}
491 	FREE(item, DtoM(type));
492 }
493 #endif /* DEBUG */
494 
495 /*
496  * Workitem queue management
497  */
498 static struct workhead softdep_workitem_pending;
499 static struct worklist *worklist_tail;
500 static int num_on_worklist;	/* number of worklist items to be processed */
501 static int softdep_worklist_busy; /* 1 => trying to do unmount */
502 static int softdep_worklist_req; /* serialized waiters */
503 static int max_softdeps;	/* maximum number of structs before slowdown */
504 static int maxindirdeps = 50;	/* max number of indirdeps before slowdown */
505 static int tickdelay = 2;	/* number of ticks to pause during slowdown */
506 static int proc_waiting;	/* tracks whether we have a timeout posted */
507 static int *stat_countp;	/* statistic to count in proc_waiting timeout */
508 static struct callout_handle handle; /* handle on posted proc_waiting timeout */
509 static struct thread *filesys_syncer; /* proc of filesystem syncer process */
510 static int req_clear_inodedeps;	/* syncer process flush some inodedeps */
511 #define FLUSH_INODES		1
512 static int req_clear_remove;	/* syncer process flush some freeblks */
513 #define FLUSH_REMOVE		2
514 #define FLUSH_REMOVE_WAIT	3
515 /*
516  * runtime statistics
517  */
518 static int stat_worklist_push;	/* number of worklist cleanups */
519 static int stat_blk_limit_push;	/* number of times block limit neared */
520 static int stat_ino_limit_push;	/* number of times inode limit neared */
521 static int stat_blk_limit_hit;	/* number of times block slowdown imposed */
522 static int stat_ino_limit_hit;	/* number of times inode slowdown imposed */
523 static int stat_sync_limit_hit;	/* number of synchronous slowdowns imposed */
524 static int stat_indir_blk_ptrs;	/* bufs redirtied as indir ptrs not written */
525 static int stat_inode_bitmap;	/* bufs redirtied as inode bitmap not written */
526 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
527 static int stat_dir_entry;	/* bufs redirtied as dir entry cannot write */
528 #ifdef DEBUG
529 #include <vm/vm.h>
530 #include <sys/sysctl.h>
531 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
532 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
533 SYSCTL_INT(_debug, OID_AUTO, maxindirdeps, CTLFLAG_RW, &maxindirdeps, 0, "");
534 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
535 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
536 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
537 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
538 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
539 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
540 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
541 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
542 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
543 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
544 #endif /* DEBUG */
545 
546 /*
547  * Add an item to the end of the work queue.
548  * This routine requires that the lock be held.
549  * This is the only routine that adds items to the list.
550  * The following routine is the only one that removes items
551  * and does so in order from first to last.
552  */
553 static void
554 add_to_worklist(wk)
555 	struct worklist *wk;
556 {
557 
558 	if (wk->wk_state & ONWORKLIST) {
559 		if (lk.lkt_held != NOHOLDER)
560 			FREE_LOCK(&lk);
561 		panic("add_to_worklist: already on list");
562 	}
563 	wk->wk_state |= ONWORKLIST;
564 	if (LIST_FIRST(&softdep_workitem_pending) == NULL)
565 		LIST_INSERT_HEAD(&softdep_workitem_pending, wk, wk_list);
566 	else
567 		LIST_INSERT_AFTER(worklist_tail, wk, wk_list);
568 	worklist_tail = wk;
569 	num_on_worklist += 1;
570 }
571 
572 /*
573  * Process that runs once per second to handle items in the background queue.
574  *
575  * Note that we ensure that everything is done in the order in which they
576  * appear in the queue. The code below depends on this property to ensure
577  * that blocks of a file are freed before the inode itself is freed. This
578  * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
579  * until all the old ones have been purged from the dependency lists.
580  */
581 int
582 softdep_process_worklist(matchmnt)
583 	struct mount *matchmnt;
584 {
585 	struct thread *td = curthread;
586 	int cnt, matchcnt, loopcount;
587 	long starttime;
588 
589 	/*
590 	 * Record the process identifier of our caller so that we can give
591 	 * this process preferential treatment in request_cleanup below.
592 	 */
593 	filesys_syncer = td;
594 	matchcnt = 0;
595 
596 	/*
597 	 * There is no danger of having multiple processes run this
598 	 * code, but we have to single-thread it when softdep_flushfiles()
599 	 * is in operation to get an accurate count of the number of items
600 	 * related to its mount point that are in the list.
601 	 */
602 	if (matchmnt == NULL) {
603 		if (softdep_worklist_busy < 0)
604 			return(-1);
605 		softdep_worklist_busy += 1;
606 	}
607 
608 	/*
609 	 * If requested, try removing inode or removal dependencies.
610 	 */
611 	if (req_clear_inodedeps) {
612 		clear_inodedeps(td);
613 		req_clear_inodedeps -= 1;
614 		wakeup_one(&proc_waiting);
615 	}
616 	if (req_clear_remove) {
617 		clear_remove(td);
618 		req_clear_remove -= 1;
619 		wakeup_one(&proc_waiting);
620 	}
621 	loopcount = 1;
622 	starttime = time_second;
623 	while (num_on_worklist > 0) {
624 		if ((cnt = process_worklist_item(matchmnt, 0)) == -1)
625 			break;
626 		else
627 			matchcnt += cnt;
628 
629 		/*
630 		 * If a umount operation wants to run the worklist
631 		 * accurately, abort.
632 		 */
633 		if (softdep_worklist_req && matchmnt == NULL) {
634 			matchcnt = -1;
635 			break;
636 		}
637 
638 		/*
639 		 * If requested, try removing inode or removal dependencies.
640 		 */
641 		if (req_clear_inodedeps) {
642 			clear_inodedeps(td);
643 			req_clear_inodedeps -= 1;
644 			wakeup_one(&proc_waiting);
645 		}
646 		if (req_clear_remove) {
647 			clear_remove(td);
648 			req_clear_remove -= 1;
649 			wakeup_one(&proc_waiting);
650 		}
651 		/*
652 		 * We do not generally want to stop for buffer space, but if
653 		 * we are really being a buffer hog, we will stop and wait.
654 		 */
655 		if (loopcount++ % 128 == 0)
656 			bwillwrite();
657 		/*
658 		 * Never allow processing to run for more than one
659 		 * second. Otherwise the other syncer tasks may get
660 		 * excessively backlogged.
661 		 */
662 		if (starttime != time_second && matchmnt == NULL) {
663 			matchcnt = -1;
664 			break;
665 		}
666 	}
667 	if (matchmnt == NULL) {
668 		softdep_worklist_busy -= 1;
669 		if (softdep_worklist_req && softdep_worklist_busy == 0)
670 			wakeup(&softdep_worklist_req);
671 	}
672 	return (matchcnt);
673 }
674 
675 /*
676  * Process one item on the worklist.
677  */
678 static int
679 process_worklist_item(matchmnt, flags)
680 	struct mount *matchmnt;
681 	int flags;
682 {
683 	struct worklist *wk, *wkend;
684 	struct mount *mp;
685 	struct vnode *vp;
686 	int matchcnt = 0;
687 
688 	/*
689 	 * If we are being called because of a process doing a
690 	 * copy-on-write, then it is not safe to write as we may
691 	 * recurse into the copy-on-write routine.
692 	 */
693 	if (curthread->td_pflags & TDP_COWINPROGRESS)
694 		return (-1);
695 	ACQUIRE_LOCK(&lk);
696 	/*
697 	 * Normally we just process each item on the worklist in order.
698 	 * However, if we are in a situation where we cannot lock any
699 	 * inodes, we have to skip over any dirrem requests whose
700 	 * vnodes are resident and locked.
701 	 */
702 	vp = NULL;
703 	LIST_FOREACH(wk, &softdep_workitem_pending, wk_list) {
704 		if (wk->wk_state & INPROGRESS)
705 			continue;
706 		if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
707 			break;
708 		wk->wk_state |= INPROGRESS;
709 		FREE_LOCK(&lk);
710 		VFS_VGET(WK_DIRREM(wk)->dm_mnt, WK_DIRREM(wk)->dm_oldinum,
711 		    LK_NOWAIT | LK_EXCLUSIVE, &vp);
712 		ACQUIRE_LOCK(&lk);
713 		wk->wk_state &= ~INPROGRESS;
714 		if (vp != NULL)
715 			break;
716 	}
717 	if (wk == 0) {
718 		FREE_LOCK(&lk);
719 		return (-1);
720 	}
721 	/*
722 	 * Remove the item to be processed. If we are removing the last
723 	 * item on the list, we need to recalculate the tail pointer.
724 	 * As this happens rarely and usually when the list is short,
725 	 * we just run down the list to find it rather than tracking it
726 	 * in the above loop.
727 	 */
728 	WORKLIST_REMOVE(wk);
729 	if (wk == worklist_tail) {
730 		LIST_FOREACH(wkend, &softdep_workitem_pending, wk_list)
731 			if (LIST_NEXT(wkend, wk_list) == NULL)
732 				break;
733 		worklist_tail = wkend;
734 	}
735 	num_on_worklist -= 1;
736 	FREE_LOCK(&lk);
737 	switch (wk->wk_type) {
738 
739 	case D_DIRREM:
740 		/* removal of a directory entry */
741 		mp = WK_DIRREM(wk)->dm_mnt;
742 		if (vn_write_suspend_wait(NULL, mp, V_NOWAIT))
743 			panic("%s: dirrem on suspended filesystem",
744 				"process_worklist_item");
745 		if (mp == matchmnt)
746 			matchcnt += 1;
747 		handle_workitem_remove(WK_DIRREM(wk), vp);
748 		break;
749 
750 	case D_FREEBLKS:
751 		/* releasing blocks and/or fragments from a file */
752 		mp = WK_FREEBLKS(wk)->fb_mnt;
753 		if (vn_write_suspend_wait(NULL, mp, V_NOWAIT))
754 			panic("%s: freeblks on suspended filesystem",
755 				"process_worklist_item");
756 		if (mp == matchmnt)
757 			matchcnt += 1;
758 		handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT);
759 		break;
760 
761 	case D_FREEFRAG:
762 		/* releasing a fragment when replaced as a file grows */
763 		mp = WK_FREEFRAG(wk)->ff_mnt;
764 		if (vn_write_suspend_wait(NULL, mp, V_NOWAIT))
765 			panic("%s: freefrag on suspended filesystem",
766 				"process_worklist_item");
767 		if (mp == matchmnt)
768 			matchcnt += 1;
769 		handle_workitem_freefrag(WK_FREEFRAG(wk));
770 		break;
771 
772 	case D_FREEFILE:
773 		/* releasing an inode when its link count drops to 0 */
774 		mp = WK_FREEFILE(wk)->fx_mnt;
775 		if (vn_write_suspend_wait(NULL, mp, V_NOWAIT))
776 			panic("%s: freefile on suspended filesystem",
777 				"process_worklist_item");
778 		if (mp == matchmnt)
779 			matchcnt += 1;
780 		handle_workitem_freefile(WK_FREEFILE(wk));
781 		break;
782 
783 	default:
784 		panic("%s_process_worklist: Unknown type %s",
785 		    "softdep", TYPENAME(wk->wk_type));
786 		/* NOTREACHED */
787 	}
788 	return (matchcnt);
789 }
790 
791 /*
792  * Move dependencies from one buffer to another.
793  */
794 static void
795 softdep_move_dependencies(oldbp, newbp)
796 	struct buf *oldbp;
797 	struct buf *newbp;
798 {
799 	struct worklist *wk, *wktail;
800 
801 	if (LIST_FIRST(&newbp->b_dep) != NULL)
802 		panic("softdep_move_dependencies: need merge code");
803 	wktail = 0;
804 	ACQUIRE_LOCK(&lk);
805 	while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
806 		LIST_REMOVE(wk, wk_list);
807 		if (wktail == 0)
808 			LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
809 		else
810 			LIST_INSERT_AFTER(wktail, wk, wk_list);
811 		wktail = wk;
812 	}
813 	FREE_LOCK(&lk);
814 }
815 
816 /*
817  * Purge the work list of all items associated with a particular mount point.
818  */
819 int
820 softdep_flushworklist(oldmnt, countp, td)
821 	struct mount *oldmnt;
822 	int *countp;
823 	struct thread *td;
824 {
825 	struct vnode *devvp;
826 	int count, error = 0;
827 
828 	/*
829 	 * Await our turn to clear out the queue, then serialize access.
830 	 */
831 	while (softdep_worklist_busy) {
832 		softdep_worklist_req += 1;
833 		tsleep(&softdep_worklist_req, PRIBIO, "softflush", 0);
834 		softdep_worklist_req -= 1;
835 	}
836 	softdep_worklist_busy = -1;
837 	/*
838 	 * Alternately flush the block device associated with the mount
839 	 * point and process any dependencies that the flushing
840 	 * creates. We continue until no more worklist dependencies
841 	 * are found.
842 	 */
843 	*countp = 0;
844 	devvp = VFSTOUFS(oldmnt)->um_devvp;
845 	while ((count = softdep_process_worklist(oldmnt)) > 0) {
846 		*countp += count;
847 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
848 		error = VOP_FSYNC(devvp, td->td_ucred, MNT_WAIT, td);
849 		VOP_UNLOCK(devvp, 0, td);
850 		if (error)
851 			break;
852 	}
853 	softdep_worklist_busy = 0;
854 	if (softdep_worklist_req)
855 		wakeup(&softdep_worklist_req);
856 	return (error);
857 }
858 
859 /*
860  * Flush all vnodes and worklist items associated with a specified mount point.
861  */
862 int
863 softdep_flushfiles(oldmnt, flags, td)
864 	struct mount *oldmnt;
865 	int flags;
866 	struct thread *td;
867 {
868 	int error, count, loopcnt;
869 
870 	error = 0;
871 
872 	/*
873 	 * Alternately flush the vnodes associated with the mount
874 	 * point and process any dependencies that the flushing
875 	 * creates. In theory, this loop can happen at most twice,
876 	 * but we give it a few extra just to be sure.
877 	 */
878 	for (loopcnt = 10; loopcnt > 0; loopcnt--) {
879 		/*
880 		 * Do another flush in case any vnodes were brought in
881 		 * as part of the cleanup operations.
882 		 */
883 		if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
884 			break;
885 		if ((error = softdep_flushworklist(oldmnt, &count, td)) != 0 ||
886 		    count == 0)
887 			break;
888 	}
889 	/*
890 	 * If we are unmounting then it is an error to fail. If we
891 	 * are simply trying to downgrade to read-only, then filesystem
892 	 * activity can keep us busy forever, so we just fail with EBUSY.
893 	 */
894 	if (loopcnt == 0) {
895 		if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
896 			panic("softdep_flushfiles: looping");
897 		error = EBUSY;
898 	}
899 	return (error);
900 }
901 
902 /*
903  * Structure hashing.
904  *
905  * There are three types of structures that can be looked up:
906  *	1) pagedep structures identified by mount point, inode number,
907  *	   and logical block.
908  *	2) inodedep structures identified by mount point and inode number.
909  *	3) newblk structures identified by mount point and
910  *	   physical block number.
911  *
912  * The "pagedep" and "inodedep" dependency structures are hashed
913  * separately from the file blocks and inodes to which they correspond.
914  * This separation helps when the in-memory copy of an inode or
915  * file block must be replaced. It also obviates the need to access
916  * an inode or file page when simply updating (or de-allocating)
917  * dependency structures. Lookup of newblk structures is needed to
918  * find newly allocated blocks when trying to associate them with
919  * their allocdirect or allocindir structure.
920  *
921  * The lookup routines optionally create and hash a new instance when
922  * an existing entry is not found.
923  */
924 #define DEPALLOC	0x0001	/* allocate structure if lookup fails */
925 #define NODELAY		0x0002	/* cannot do background work */
926 
927 /*
928  * Structures and routines associated with pagedep caching.
929  */
930 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
931 u_long	pagedep_hash;		/* size of hash table - 1 */
932 #define	PAGEDEP_HASH(mp, inum, lbn) \
933 	(&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
934 	    pagedep_hash])
935 static struct sema pagedep_in_progress;
936 
937 /*
938  * Look up a pagedep. Return 1 if found, 0 if not found or found
939  * when asked to allocate but not associated with any buffer.
940  * If not found, allocate if DEPALLOC flag is passed.
941  * Found or allocated entry is returned in pagedeppp.
942  * This routine must be called with splbio interrupts blocked.
943  */
944 static int
945 pagedep_lookup(ip, lbn, flags, pagedeppp)
946 	struct inode *ip;
947 	ufs_lbn_t lbn;
948 	int flags;
949 	struct pagedep **pagedeppp;
950 {
951 	struct pagedep *pagedep;
952 	struct pagedep_hashhead *pagedephd;
953 	struct mount *mp;
954 	int i;
955 
956 #ifdef DEBUG
957 	if (lk.lkt_held == NOHOLDER)
958 		panic("pagedep_lookup: lock not held");
959 #endif
960 	mp = ITOV(ip)->v_mount;
961 	pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
962 top:
963 	LIST_FOREACH(pagedep, pagedephd, pd_hash)
964 		if (ip->i_number == pagedep->pd_ino &&
965 		    lbn == pagedep->pd_lbn &&
966 		    mp == pagedep->pd_mnt)
967 			break;
968 	if (pagedep) {
969 		*pagedeppp = pagedep;
970 		if ((flags & DEPALLOC) != 0 &&
971 		    (pagedep->pd_state & ONWORKLIST) == 0)
972 			return (0);
973 		return (1);
974 	}
975 	if ((flags & DEPALLOC) == 0) {
976 		*pagedeppp = NULL;
977 		return (0);
978 	}
979 	if (sema_get(&pagedep_in_progress, &lk) == 0) {
980 		ACQUIRE_LOCK(&lk);
981 		goto top;
982 	}
983 	MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep), M_PAGEDEP,
984 		M_SOFTDEP_FLAGS|M_ZERO);
985 	pagedep->pd_list.wk_type = D_PAGEDEP;
986 	pagedep->pd_mnt = mp;
987 	pagedep->pd_ino = ip->i_number;
988 	pagedep->pd_lbn = lbn;
989 	LIST_INIT(&pagedep->pd_dirremhd);
990 	LIST_INIT(&pagedep->pd_pendinghd);
991 	for (i = 0; i < DAHASHSZ; i++)
992 		LIST_INIT(&pagedep->pd_diraddhd[i]);
993 	ACQUIRE_LOCK(&lk);
994 	LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
995 	sema_release(&pagedep_in_progress);
996 	*pagedeppp = pagedep;
997 	return (0);
998 }
999 
1000 /*
1001  * Structures and routines associated with inodedep caching.
1002  */
1003 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
1004 static u_long	inodedep_hash;	/* size of hash table - 1 */
1005 static long	num_inodedep;	/* number of inodedep allocated */
1006 #define	INODEDEP_HASH(fs, inum) \
1007       (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
1008 static struct sema inodedep_in_progress;
1009 
1010 /*
1011  * Look up an inodedep. Return 1 if found, 0 if not found.
1012  * If not found, allocate if DEPALLOC flag is passed.
1013  * Found or allocated entry is returned in inodedeppp.
1014  * This routine must be called with splbio interrupts blocked.
1015  */
1016 static int
1017 inodedep_lookup(fs, inum, flags, inodedeppp)
1018 	struct fs *fs;
1019 	ino_t inum;
1020 	int flags;
1021 	struct inodedep **inodedeppp;
1022 {
1023 	struct inodedep *inodedep;
1024 	struct inodedep_hashhead *inodedephd;
1025 	int firsttry;
1026 
1027 #ifdef DEBUG
1028 	if (lk.lkt_held == NOHOLDER)
1029 		panic("inodedep_lookup: lock not held");
1030 #endif
1031 	firsttry = 1;
1032 	inodedephd = INODEDEP_HASH(fs, inum);
1033 top:
1034 	LIST_FOREACH(inodedep, inodedephd, id_hash)
1035 		if (inum == inodedep->id_ino && fs == inodedep->id_fs)
1036 			break;
1037 	if (inodedep) {
1038 		*inodedeppp = inodedep;
1039 		return (1);
1040 	}
1041 	if ((flags & DEPALLOC) == 0) {
1042 		*inodedeppp = NULL;
1043 		return (0);
1044 	}
1045 	/*
1046 	 * If we are over our limit, try to improve the situation.
1047 	 */
1048 	if (num_inodedep > max_softdeps && firsttry && (flags & NODELAY) == 0 &&
1049 	    request_cleanup(FLUSH_INODES, 1)) {
1050 		firsttry = 0;
1051 		goto top;
1052 	}
1053 	if (sema_get(&inodedep_in_progress, &lk) == 0) {
1054 		ACQUIRE_LOCK(&lk);
1055 		goto top;
1056 	}
1057 	num_inodedep += 1;
1058 	MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep),
1059 		M_INODEDEP, M_SOFTDEP_FLAGS);
1060 	inodedep->id_list.wk_type = D_INODEDEP;
1061 	inodedep->id_fs = fs;
1062 	inodedep->id_ino = inum;
1063 	inodedep->id_state = ALLCOMPLETE;
1064 	inodedep->id_nlinkdelta = 0;
1065 	inodedep->id_savedino1 = NULL;
1066 	inodedep->id_savedsize = -1;
1067 	inodedep->id_savedextsize = -1;
1068 	inodedep->id_buf = NULL;
1069 	LIST_INIT(&inodedep->id_pendinghd);
1070 	LIST_INIT(&inodedep->id_inowait);
1071 	LIST_INIT(&inodedep->id_bufwait);
1072 	TAILQ_INIT(&inodedep->id_inoupdt);
1073 	TAILQ_INIT(&inodedep->id_newinoupdt);
1074 	TAILQ_INIT(&inodedep->id_extupdt);
1075 	TAILQ_INIT(&inodedep->id_newextupdt);
1076 	ACQUIRE_LOCK(&lk);
1077 	LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1078 	sema_release(&inodedep_in_progress);
1079 	*inodedeppp = inodedep;
1080 	return (0);
1081 }
1082 
1083 /*
1084  * Structures and routines associated with newblk caching.
1085  */
1086 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1087 u_long	newblk_hash;		/* size of hash table - 1 */
1088 #define	NEWBLK_HASH(fs, inum) \
1089 	(&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1090 static struct sema newblk_in_progress;
1091 
1092 /*
1093  * Look up a newblk. Return 1 if found, 0 if not found.
1094  * If not found, allocate if DEPALLOC flag is passed.
1095  * Found or allocated entry is returned in newblkpp.
1096  */
1097 static int
1098 newblk_lookup(fs, newblkno, flags, newblkpp)
1099 	struct fs *fs;
1100 	ufs2_daddr_t newblkno;
1101 	int flags;
1102 	struct newblk **newblkpp;
1103 {
1104 	struct newblk *newblk;
1105 	struct newblk_hashhead *newblkhd;
1106 
1107 	newblkhd = NEWBLK_HASH(fs, newblkno);
1108 top:
1109 	LIST_FOREACH(newblk, newblkhd, nb_hash)
1110 		if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1111 			break;
1112 	if (newblk) {
1113 		*newblkpp = newblk;
1114 		return (1);
1115 	}
1116 	if ((flags & DEPALLOC) == 0) {
1117 		*newblkpp = NULL;
1118 		return (0);
1119 	}
1120 	if (sema_get(&newblk_in_progress, 0) == 0)
1121 		goto top;
1122 	MALLOC(newblk, struct newblk *, sizeof(struct newblk),
1123 		M_NEWBLK, M_SOFTDEP_FLAGS);
1124 	newblk->nb_state = 0;
1125 	newblk->nb_fs = fs;
1126 	newblk->nb_newblkno = newblkno;
1127 	LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1128 	sema_release(&newblk_in_progress);
1129 	*newblkpp = newblk;
1130 	return (0);
1131 }
1132 
1133 /*
1134  * Executed during filesystem system initialization before
1135  * mounting any filesystems.
1136  */
1137 void
1138 softdep_initialize()
1139 {
1140 
1141 	LIST_INIT(&mkdirlisthd);
1142 	LIST_INIT(&softdep_workitem_pending);
1143 	max_softdeps = desiredvnodes * 4;
1144 	pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1145 	    &pagedep_hash);
1146 	sema_init(&pagedep_in_progress, "pagedep", PRIBIO, 0);
1147 	inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1148 	sema_init(&inodedep_in_progress, "inodedep", PRIBIO, 0);
1149 	newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1150 	sema_init(&newblk_in_progress, "newblk", PRIBIO, 0);
1151 
1152 	/* hooks through which the main kernel code calls us */
1153 	softdep_process_worklist_hook = softdep_process_worklist;
1154 	softdep_fsync_hook = softdep_fsync;
1155 
1156 	/* initialise bioops hack */
1157 	bioops.io_prewrite = softdep_disk_prewrite;
1158 	bioops.io_start = softdep_disk_io_initiation;
1159 	bioops.io_complete = softdep_disk_write_complete;
1160 	bioops.io_deallocate = softdep_deallocate_dependencies;
1161 	bioops.io_movedeps = softdep_move_dependencies;
1162 	bioops.io_countdeps = softdep_count_dependencies;
1163 }
1164 
1165 /*
1166  * Executed after all filesystems have been unmounted during
1167  * filesystem module unload.
1168  */
1169 void
1170 softdep_uninitialize()
1171 {
1172 
1173 	softdep_process_worklist_hook = NULL;
1174 	softdep_fsync_hook = NULL;
1175 	hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash);
1176 	hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash);
1177 	hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash);
1178 }
1179 
1180 /*
1181  * Called at mount time to notify the dependency code that a
1182  * filesystem wishes to use it.
1183  */
1184 int
1185 softdep_mount(devvp, mp, fs, cred)
1186 	struct vnode *devvp;
1187 	struct mount *mp;
1188 	struct fs *fs;
1189 	struct ucred *cred;
1190 {
1191 	struct csum_total cstotal;
1192 	struct cg *cgp;
1193 	struct buf *bp;
1194 	int error, cyl;
1195 
1196 	mp->mnt_flag &= ~MNT_ASYNC;
1197 	mp->mnt_flag |= MNT_SOFTDEP;
1198 	/*
1199 	 * When doing soft updates, the counters in the
1200 	 * superblock may have gotten out of sync, so we have
1201 	 * to scan the cylinder groups and recalculate them.
1202 	 */
1203 	if (fs->fs_clean != 0)
1204 		return (0);
1205 	bzero(&cstotal, sizeof cstotal);
1206 	for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1207 		if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1208 		    fs->fs_cgsize, cred, &bp)) != 0) {
1209 			brelse(bp);
1210 			return (error);
1211 		}
1212 		cgp = (struct cg *)bp->b_data;
1213 		cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1214 		cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1215 		cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1216 		cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1217 		fs->fs_cs(fs, cyl) = cgp->cg_cs;
1218 		brelse(bp);
1219 	}
1220 #ifdef DEBUG
1221 	if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1222 		printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
1223 #endif
1224 	bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1225 	return (0);
1226 }
1227 
1228 /*
1229  * Protecting the freemaps (or bitmaps).
1230  *
1231  * To eliminate the need to execute fsck before mounting a filesystem
1232  * after a power failure, one must (conservatively) guarantee that the
1233  * on-disk copy of the bitmaps never indicate that a live inode or block is
1234  * free.  So, when a block or inode is allocated, the bitmap should be
1235  * updated (on disk) before any new pointers.  When a block or inode is
1236  * freed, the bitmap should not be updated until all pointers have been
1237  * reset.  The latter dependency is handled by the delayed de-allocation
1238  * approach described below for block and inode de-allocation.  The former
1239  * dependency is handled by calling the following procedure when a block or
1240  * inode is allocated. When an inode is allocated an "inodedep" is created
1241  * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1242  * Each "inodedep" is also inserted into the hash indexing structure so
1243  * that any additional link additions can be made dependent on the inode
1244  * allocation.
1245  *
1246  * The ufs filesystem maintains a number of free block counts (e.g., per
1247  * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1248  * in addition to the bitmaps.  These counts are used to improve efficiency
1249  * during allocation and therefore must be consistent with the bitmaps.
1250  * There is no convenient way to guarantee post-crash consistency of these
1251  * counts with simple update ordering, for two main reasons: (1) The counts
1252  * and bitmaps for a single cylinder group block are not in the same disk
1253  * sector.  If a disk write is interrupted (e.g., by power failure), one may
1254  * be written and the other not.  (2) Some of the counts are located in the
1255  * superblock rather than the cylinder group block. So, we focus our soft
1256  * updates implementation on protecting the bitmaps. When mounting a
1257  * filesystem, we recompute the auxiliary counts from the bitmaps.
1258  */
1259 
1260 /*
1261  * Called just after updating the cylinder group block to allocate an inode.
1262  */
1263 void
1264 softdep_setup_inomapdep(bp, ip, newinum)
1265 	struct buf *bp;		/* buffer for cylgroup block with inode map */
1266 	struct inode *ip;	/* inode related to allocation */
1267 	ino_t newinum;		/* new inode number being allocated */
1268 {
1269 	struct inodedep *inodedep;
1270 	struct bmsafemap *bmsafemap;
1271 
1272 	/*
1273 	 * Create a dependency for the newly allocated inode.
1274 	 * Panic if it already exists as something is seriously wrong.
1275 	 * Otherwise add it to the dependency list for the buffer holding
1276 	 * the cylinder group map from which it was allocated.
1277 	 */
1278 	ACQUIRE_LOCK(&lk);
1279 	if ((inodedep_lookup(ip->i_fs, newinum, DEPALLOC|NODELAY, &inodedep))) {
1280 		FREE_LOCK(&lk);
1281 		panic("softdep_setup_inomapdep: found inode");
1282 	}
1283 	inodedep->id_buf = bp;
1284 	inodedep->id_state &= ~DEPCOMPLETE;
1285 	bmsafemap = bmsafemap_lookup(bp);
1286 	LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1287 	FREE_LOCK(&lk);
1288 }
1289 
1290 /*
1291  * Called just after updating the cylinder group block to
1292  * allocate block or fragment.
1293  */
1294 void
1295 softdep_setup_blkmapdep(bp, fs, newblkno)
1296 	struct buf *bp;		/* buffer for cylgroup block with block map */
1297 	struct fs *fs;		/* filesystem doing allocation */
1298 	ufs2_daddr_t newblkno;	/* number of newly allocated block */
1299 {
1300 	struct newblk *newblk;
1301 	struct bmsafemap *bmsafemap;
1302 
1303 	/*
1304 	 * Create a dependency for the newly allocated block.
1305 	 * Add it to the dependency list for the buffer holding
1306 	 * the cylinder group map from which it was allocated.
1307 	 */
1308 	if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1309 		panic("softdep_setup_blkmapdep: found block");
1310 	ACQUIRE_LOCK(&lk);
1311 	newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp);
1312 	LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1313 	FREE_LOCK(&lk);
1314 }
1315 
1316 /*
1317  * Find the bmsafemap associated with a cylinder group buffer.
1318  * If none exists, create one. The buffer must be locked when
1319  * this routine is called and this routine must be called with
1320  * splbio interrupts blocked.
1321  */
1322 static struct bmsafemap *
1323 bmsafemap_lookup(bp)
1324 	struct buf *bp;
1325 {
1326 	struct bmsafemap *bmsafemap;
1327 	struct worklist *wk;
1328 
1329 #ifdef DEBUG
1330 	if (lk.lkt_held == NOHOLDER)
1331 		panic("bmsafemap_lookup: lock not held");
1332 #endif
1333 	LIST_FOREACH(wk, &bp->b_dep, wk_list)
1334 		if (wk->wk_type == D_BMSAFEMAP)
1335 			return (WK_BMSAFEMAP(wk));
1336 	FREE_LOCK(&lk);
1337 	MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1338 		M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1339 	bmsafemap->sm_list.wk_type = D_BMSAFEMAP;
1340 	bmsafemap->sm_list.wk_state = 0;
1341 	bmsafemap->sm_buf = bp;
1342 	LIST_INIT(&bmsafemap->sm_allocdirecthd);
1343 	LIST_INIT(&bmsafemap->sm_allocindirhd);
1344 	LIST_INIT(&bmsafemap->sm_inodedephd);
1345 	LIST_INIT(&bmsafemap->sm_newblkhd);
1346 	ACQUIRE_LOCK(&lk);
1347 	WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1348 	return (bmsafemap);
1349 }
1350 
1351 /*
1352  * Direct block allocation dependencies.
1353  *
1354  * When a new block is allocated, the corresponding disk locations must be
1355  * initialized (with zeros or new data) before the on-disk inode points to
1356  * them.  Also, the freemap from which the block was allocated must be
1357  * updated (on disk) before the inode's pointer. These two dependencies are
1358  * independent of each other and are needed for all file blocks and indirect
1359  * blocks that are pointed to directly by the inode.  Just before the
1360  * "in-core" version of the inode is updated with a newly allocated block
1361  * number, a procedure (below) is called to setup allocation dependency
1362  * structures.  These structures are removed when the corresponding
1363  * dependencies are satisfied or when the block allocation becomes obsolete
1364  * (i.e., the file is deleted, the block is de-allocated, or the block is a
1365  * fragment that gets upgraded).  All of these cases are handled in
1366  * procedures described later.
1367  *
1368  * When a file extension causes a fragment to be upgraded, either to a larger
1369  * fragment or to a full block, the on-disk location may change (if the
1370  * previous fragment could not simply be extended). In this case, the old
1371  * fragment must be de-allocated, but not until after the inode's pointer has
1372  * been updated. In most cases, this is handled by later procedures, which
1373  * will construct a "freefrag" structure to be added to the workitem queue
1374  * when the inode update is complete (or obsolete).  The main exception to
1375  * this is when an allocation occurs while a pending allocation dependency
1376  * (for the same block pointer) remains.  This case is handled in the main
1377  * allocation dependency setup procedure by immediately freeing the
1378  * unreferenced fragments.
1379  */
1380 void
1381 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1382 	struct inode *ip;	/* inode to which block is being added */
1383 	ufs_lbn_t lbn;		/* block pointer within inode */
1384 	ufs2_daddr_t newblkno;	/* disk block number being added */
1385 	ufs2_daddr_t oldblkno;	/* previous block number, 0 unless frag */
1386 	long newsize;		/* size of new block */
1387 	long oldsize;		/* size of new block */
1388 	struct buf *bp;		/* bp for allocated block */
1389 {
1390 	struct allocdirect *adp, *oldadp;
1391 	struct allocdirectlst *adphead;
1392 	struct bmsafemap *bmsafemap;
1393 	struct inodedep *inodedep;
1394 	struct pagedep *pagedep;
1395 	struct newblk *newblk;
1396 
1397 	MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1398 		M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1399 	adp->ad_list.wk_type = D_ALLOCDIRECT;
1400 	adp->ad_lbn = lbn;
1401 	adp->ad_newblkno = newblkno;
1402 	adp->ad_oldblkno = oldblkno;
1403 	adp->ad_newsize = newsize;
1404 	adp->ad_oldsize = oldsize;
1405 	adp->ad_state = ATTACHED;
1406 	LIST_INIT(&adp->ad_newdirblk);
1407 	if (newblkno == oldblkno)
1408 		adp->ad_freefrag = NULL;
1409 	else
1410 		adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1411 
1412 	if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1413 		panic("softdep_setup_allocdirect: lost block");
1414 
1415 	ACQUIRE_LOCK(&lk);
1416 	inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1417 	adp->ad_inodedep = inodedep;
1418 
1419 	if (newblk->nb_state == DEPCOMPLETE) {
1420 		adp->ad_state |= DEPCOMPLETE;
1421 		adp->ad_buf = NULL;
1422 	} else {
1423 		bmsafemap = newblk->nb_bmsafemap;
1424 		adp->ad_buf = bmsafemap->sm_buf;
1425 		LIST_REMOVE(newblk, nb_deps);
1426 		LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1427 	}
1428 	LIST_REMOVE(newblk, nb_hash);
1429 	FREE(newblk, M_NEWBLK);
1430 
1431 	WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1432 	if (lbn >= NDADDR) {
1433 		/* allocating an indirect block */
1434 		if (oldblkno != 0) {
1435 			FREE_LOCK(&lk);
1436 			panic("softdep_setup_allocdirect: non-zero indir");
1437 		}
1438 	} else {
1439 		/*
1440 		 * Allocating a direct block.
1441 		 *
1442 		 * If we are allocating a directory block, then we must
1443 		 * allocate an associated pagedep to track additions and
1444 		 * deletions.
1445 		 */
1446 		if ((ip->i_mode & IFMT) == IFDIR &&
1447 		    pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1448 			WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1449 	}
1450 	/*
1451 	 * The list of allocdirects must be kept in sorted and ascending
1452 	 * order so that the rollback routines can quickly determine the
1453 	 * first uncommitted block (the size of the file stored on disk
1454 	 * ends at the end of the lowest committed fragment, or if there
1455 	 * are no fragments, at the end of the highest committed block).
1456 	 * Since files generally grow, the typical case is that the new
1457 	 * block is to be added at the end of the list. We speed this
1458 	 * special case by checking against the last allocdirect in the
1459 	 * list before laboriously traversing the list looking for the
1460 	 * insertion point.
1461 	 */
1462 	adphead = &inodedep->id_newinoupdt;
1463 	oldadp = TAILQ_LAST(adphead, allocdirectlst);
1464 	if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1465 		/* insert at end of list */
1466 		TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1467 		if (oldadp != NULL && oldadp->ad_lbn == lbn)
1468 			allocdirect_merge(adphead, adp, oldadp);
1469 		FREE_LOCK(&lk);
1470 		return;
1471 	}
1472 	TAILQ_FOREACH(oldadp, adphead, ad_next) {
1473 		if (oldadp->ad_lbn >= lbn)
1474 			break;
1475 	}
1476 	if (oldadp == NULL) {
1477 		FREE_LOCK(&lk);
1478 		panic("softdep_setup_allocdirect: lost entry");
1479 	}
1480 	/* insert in middle of list */
1481 	TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1482 	if (oldadp->ad_lbn == lbn)
1483 		allocdirect_merge(adphead, adp, oldadp);
1484 	FREE_LOCK(&lk);
1485 }
1486 
1487 /*
1488  * Replace an old allocdirect dependency with a newer one.
1489  * This routine must be called with splbio interrupts blocked.
1490  */
1491 static void
1492 allocdirect_merge(adphead, newadp, oldadp)
1493 	struct allocdirectlst *adphead;	/* head of list holding allocdirects */
1494 	struct allocdirect *newadp;	/* allocdirect being added */
1495 	struct allocdirect *oldadp;	/* existing allocdirect being checked */
1496 {
1497 	struct worklist *wk;
1498 	struct freefrag *freefrag;
1499 	struct newdirblk *newdirblk;
1500 
1501 #ifdef DEBUG
1502 	if (lk.lkt_held == NOHOLDER)
1503 		panic("allocdirect_merge: lock not held");
1504 #endif
1505 	if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1506 	    newadp->ad_oldsize != oldadp->ad_newsize ||
1507 	    newadp->ad_lbn >= NDADDR) {
1508 		FREE_LOCK(&lk);
1509 		panic("%s %jd != new %jd || old size %ld != new %ld",
1510 		    "allocdirect_merge: old blkno",
1511 		    (intmax_t)newadp->ad_oldblkno,
1512 		    (intmax_t)oldadp->ad_newblkno,
1513 		    newadp->ad_oldsize, oldadp->ad_newsize);
1514 	}
1515 	newadp->ad_oldblkno = oldadp->ad_oldblkno;
1516 	newadp->ad_oldsize = oldadp->ad_oldsize;
1517 	/*
1518 	 * If the old dependency had a fragment to free or had never
1519 	 * previously had a block allocated, then the new dependency
1520 	 * can immediately post its freefrag and adopt the old freefrag.
1521 	 * This action is done by swapping the freefrag dependencies.
1522 	 * The new dependency gains the old one's freefrag, and the
1523 	 * old one gets the new one and then immediately puts it on
1524 	 * the worklist when it is freed by free_allocdirect. It is
1525 	 * not possible to do this swap when the old dependency had a
1526 	 * non-zero size but no previous fragment to free. This condition
1527 	 * arises when the new block is an extension of the old block.
1528 	 * Here, the first part of the fragment allocated to the new
1529 	 * dependency is part of the block currently claimed on disk by
1530 	 * the old dependency, so cannot legitimately be freed until the
1531 	 * conditions for the new dependency are fulfilled.
1532 	 */
1533 	if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1534 		freefrag = newadp->ad_freefrag;
1535 		newadp->ad_freefrag = oldadp->ad_freefrag;
1536 		oldadp->ad_freefrag = freefrag;
1537 	}
1538 	/*
1539 	 * If we are tracking a new directory-block allocation,
1540 	 * move it from the old allocdirect to the new allocdirect.
1541 	 */
1542 	if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
1543 		newdirblk = WK_NEWDIRBLK(wk);
1544 		WORKLIST_REMOVE(&newdirblk->db_list);
1545 		if (LIST_FIRST(&oldadp->ad_newdirblk) != NULL)
1546 			panic("allocdirect_merge: extra newdirblk");
1547 		WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list);
1548 	}
1549 	free_allocdirect(adphead, oldadp, 0);
1550 }
1551 
1552 /*
1553  * Allocate a new freefrag structure if needed.
1554  */
1555 static struct freefrag *
1556 newfreefrag(ip, blkno, size)
1557 	struct inode *ip;
1558 	ufs2_daddr_t blkno;
1559 	long size;
1560 {
1561 	struct freefrag *freefrag;
1562 	struct fs *fs;
1563 
1564 	if (blkno == 0)
1565 		return (NULL);
1566 	fs = ip->i_fs;
1567 	if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1568 		panic("newfreefrag: frag size");
1569 	MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1570 		M_FREEFRAG, M_SOFTDEP_FLAGS);
1571 	freefrag->ff_list.wk_type = D_FREEFRAG;
1572 	freefrag->ff_state = 0;
1573 	freefrag->ff_inum = ip->i_number;
1574 	freefrag->ff_mnt = ITOV(ip)->v_mount;
1575 	freefrag->ff_blkno = blkno;
1576 	freefrag->ff_fragsize = size;
1577 	return (freefrag);
1578 }
1579 
1580 /*
1581  * This workitem de-allocates fragments that were replaced during
1582  * file block allocation.
1583  */
1584 static void
1585 handle_workitem_freefrag(freefrag)
1586 	struct freefrag *freefrag;
1587 {
1588 	struct ufsmount *ump = VFSTOUFS(freefrag->ff_mnt);
1589 
1590 	ffs_blkfree(ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
1591 	    freefrag->ff_fragsize, freefrag->ff_inum);
1592 	FREE(freefrag, M_FREEFRAG);
1593 }
1594 
1595 /*
1596  * Set up a dependency structure for an external attributes data block.
1597  * This routine follows much of the structure of softdep_setup_allocdirect.
1598  * See the description of softdep_setup_allocdirect above for details.
1599  */
1600 void
1601 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1602 	struct inode *ip;
1603 	ufs_lbn_t lbn;
1604 	ufs2_daddr_t newblkno;
1605 	ufs2_daddr_t oldblkno;
1606 	long newsize;
1607 	long oldsize;
1608 	struct buf *bp;
1609 {
1610 	struct allocdirect *adp, *oldadp;
1611 	struct allocdirectlst *adphead;
1612 	struct bmsafemap *bmsafemap;
1613 	struct inodedep *inodedep;
1614 	struct newblk *newblk;
1615 
1616 	MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1617 		M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1618 	adp->ad_list.wk_type = D_ALLOCDIRECT;
1619 	adp->ad_lbn = lbn;
1620 	adp->ad_newblkno = newblkno;
1621 	adp->ad_oldblkno = oldblkno;
1622 	adp->ad_newsize = newsize;
1623 	adp->ad_oldsize = oldsize;
1624 	adp->ad_state = ATTACHED | EXTDATA;
1625 	LIST_INIT(&adp->ad_newdirblk);
1626 	if (newblkno == oldblkno)
1627 		adp->ad_freefrag = NULL;
1628 	else
1629 		adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1630 
1631 	if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1632 		panic("softdep_setup_allocext: lost block");
1633 
1634 	ACQUIRE_LOCK(&lk);
1635 	inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1636 	adp->ad_inodedep = inodedep;
1637 
1638 	if (newblk->nb_state == DEPCOMPLETE) {
1639 		adp->ad_state |= DEPCOMPLETE;
1640 		adp->ad_buf = NULL;
1641 	} else {
1642 		bmsafemap = newblk->nb_bmsafemap;
1643 		adp->ad_buf = bmsafemap->sm_buf;
1644 		LIST_REMOVE(newblk, nb_deps);
1645 		LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1646 	}
1647 	LIST_REMOVE(newblk, nb_hash);
1648 	FREE(newblk, M_NEWBLK);
1649 
1650 	WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1651 	if (lbn >= NXADDR) {
1652 		FREE_LOCK(&lk);
1653 		panic("softdep_setup_allocext: lbn %lld > NXADDR",
1654 		    (long long)lbn);
1655 	}
1656 	/*
1657 	 * The list of allocdirects must be kept in sorted and ascending
1658 	 * order so that the rollback routines can quickly determine the
1659 	 * first uncommitted block (the size of the file stored on disk
1660 	 * ends at the end of the lowest committed fragment, or if there
1661 	 * are no fragments, at the end of the highest committed block).
1662 	 * Since files generally grow, the typical case is that the new
1663 	 * block is to be added at the end of the list. We speed this
1664 	 * special case by checking against the last allocdirect in the
1665 	 * list before laboriously traversing the list looking for the
1666 	 * insertion point.
1667 	 */
1668 	adphead = &inodedep->id_newextupdt;
1669 	oldadp = TAILQ_LAST(adphead, allocdirectlst);
1670 	if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1671 		/* insert at end of list */
1672 		TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1673 		if (oldadp != NULL && oldadp->ad_lbn == lbn)
1674 			allocdirect_merge(adphead, adp, oldadp);
1675 		FREE_LOCK(&lk);
1676 		return;
1677 	}
1678 	TAILQ_FOREACH(oldadp, adphead, ad_next) {
1679 		if (oldadp->ad_lbn >= lbn)
1680 			break;
1681 	}
1682 	if (oldadp == NULL) {
1683 		FREE_LOCK(&lk);
1684 		panic("softdep_setup_allocext: lost entry");
1685 	}
1686 	/* insert in middle of list */
1687 	TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1688 	if (oldadp->ad_lbn == lbn)
1689 		allocdirect_merge(adphead, adp, oldadp);
1690 	FREE_LOCK(&lk);
1691 }
1692 
1693 /*
1694  * Indirect block allocation dependencies.
1695  *
1696  * The same dependencies that exist for a direct block also exist when
1697  * a new block is allocated and pointed to by an entry in a block of
1698  * indirect pointers. The undo/redo states described above are also
1699  * used here. Because an indirect block contains many pointers that
1700  * may have dependencies, a second copy of the entire in-memory indirect
1701  * block is kept. The buffer cache copy is always completely up-to-date.
1702  * The second copy, which is used only as a source for disk writes,
1703  * contains only the safe pointers (i.e., those that have no remaining
1704  * update dependencies). The second copy is freed when all pointers
1705  * are safe. The cache is not allowed to replace indirect blocks with
1706  * pending update dependencies. If a buffer containing an indirect
1707  * block with dependencies is written, these routines will mark it
1708  * dirty again. It can only be successfully written once all the
1709  * dependencies are removed. The ffs_fsync routine in conjunction with
1710  * softdep_sync_metadata work together to get all the dependencies
1711  * removed so that a file can be successfully written to disk. Three
1712  * procedures are used when setting up indirect block pointer
1713  * dependencies. The division is necessary because of the organization
1714  * of the "balloc" routine and because of the distinction between file
1715  * pages and file metadata blocks.
1716  */
1717 
1718 /*
1719  * Allocate a new allocindir structure.
1720  */
1721 static struct allocindir *
1722 newallocindir(ip, ptrno, newblkno, oldblkno)
1723 	struct inode *ip;	/* inode for file being extended */
1724 	int ptrno;		/* offset of pointer in indirect block */
1725 	ufs2_daddr_t newblkno;	/* disk block number being added */
1726 	ufs2_daddr_t oldblkno;	/* previous block number, 0 if none */
1727 {
1728 	struct allocindir *aip;
1729 
1730 	MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1731 		M_ALLOCINDIR, M_SOFTDEP_FLAGS|M_ZERO);
1732 	aip->ai_list.wk_type = D_ALLOCINDIR;
1733 	aip->ai_state = ATTACHED;
1734 	aip->ai_offset = ptrno;
1735 	aip->ai_newblkno = newblkno;
1736 	aip->ai_oldblkno = oldblkno;
1737 	aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1738 	return (aip);
1739 }
1740 
1741 /*
1742  * Called just before setting an indirect block pointer
1743  * to a newly allocated file page.
1744  */
1745 void
1746 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
1747 	struct inode *ip;	/* inode for file being extended */
1748 	ufs_lbn_t lbn;		/* allocated block number within file */
1749 	struct buf *bp;		/* buffer with indirect blk referencing page */
1750 	int ptrno;		/* offset of pointer in indirect block */
1751 	ufs2_daddr_t newblkno;	/* disk block number being added */
1752 	ufs2_daddr_t oldblkno;	/* previous block number, 0 if none */
1753 	struct buf *nbp;	/* buffer holding allocated page */
1754 {
1755 	struct allocindir *aip;
1756 	struct pagedep *pagedep;
1757 
1758 	aip = newallocindir(ip, ptrno, newblkno, oldblkno);
1759 	ACQUIRE_LOCK(&lk);
1760 	/*
1761 	 * If we are allocating a directory page, then we must
1762 	 * allocate an associated pagedep to track additions and
1763 	 * deletions.
1764 	 */
1765 	if ((ip->i_mode & IFMT) == IFDIR &&
1766 	    pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1767 		WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
1768 	WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1769 	FREE_LOCK(&lk);
1770 	setup_allocindir_phase2(bp, ip, aip);
1771 }
1772 
1773 /*
1774  * Called just before setting an indirect block pointer to a
1775  * newly allocated indirect block.
1776  */
1777 void
1778 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
1779 	struct buf *nbp;	/* newly allocated indirect block */
1780 	struct inode *ip;	/* inode for file being extended */
1781 	struct buf *bp;		/* indirect block referencing allocated block */
1782 	int ptrno;		/* offset of pointer in indirect block */
1783 	ufs2_daddr_t newblkno;	/* disk block number being added */
1784 {
1785 	struct allocindir *aip;
1786 
1787 	aip = newallocindir(ip, ptrno, newblkno, 0);
1788 	ACQUIRE_LOCK(&lk);
1789 	WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1790 	FREE_LOCK(&lk);
1791 	setup_allocindir_phase2(bp, ip, aip);
1792 }
1793 
1794 /*
1795  * Called to finish the allocation of the "aip" allocated
1796  * by one of the two routines above.
1797  */
1798 static void
1799 setup_allocindir_phase2(bp, ip, aip)
1800 	struct buf *bp;		/* in-memory copy of the indirect block */
1801 	struct inode *ip;	/* inode for file being extended */
1802 	struct allocindir *aip;	/* allocindir allocated by the above routines */
1803 {
1804 	struct worklist *wk;
1805 	struct indirdep *indirdep, *newindirdep;
1806 	struct bmsafemap *bmsafemap;
1807 	struct allocindir *oldaip;
1808 	struct freefrag *freefrag;
1809 	struct newblk *newblk;
1810 	ufs2_daddr_t blkno;
1811 
1812 	if (bp->b_lblkno >= 0)
1813 		panic("setup_allocindir_phase2: not indir blk");
1814 	for (indirdep = NULL, newindirdep = NULL; ; ) {
1815 		ACQUIRE_LOCK(&lk);
1816 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
1817 			if (wk->wk_type != D_INDIRDEP)
1818 				continue;
1819 			indirdep = WK_INDIRDEP(wk);
1820 			break;
1821 		}
1822 		if (indirdep == NULL && newindirdep) {
1823 			indirdep = newindirdep;
1824 			WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
1825 			newindirdep = NULL;
1826 		}
1827 		FREE_LOCK(&lk);
1828 		if (indirdep) {
1829 			if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
1830 			    &newblk) == 0)
1831 				panic("setup_allocindir: lost block");
1832 			ACQUIRE_LOCK(&lk);
1833 			if (newblk->nb_state == DEPCOMPLETE) {
1834 				aip->ai_state |= DEPCOMPLETE;
1835 				aip->ai_buf = NULL;
1836 			} else {
1837 				bmsafemap = newblk->nb_bmsafemap;
1838 				aip->ai_buf = bmsafemap->sm_buf;
1839 				LIST_REMOVE(newblk, nb_deps);
1840 				LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
1841 				    aip, ai_deps);
1842 			}
1843 			LIST_REMOVE(newblk, nb_hash);
1844 			FREE(newblk, M_NEWBLK);
1845 			aip->ai_indirdep = indirdep;
1846 			/*
1847 			 * Check to see if there is an existing dependency
1848 			 * for this block. If there is, merge the old
1849 			 * dependency into the new one.
1850 			 */
1851 			if (aip->ai_oldblkno == 0)
1852 				oldaip = NULL;
1853 			else
1854 
1855 				LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
1856 					if (oldaip->ai_offset == aip->ai_offset)
1857 						break;
1858 			freefrag = NULL;
1859 			if (oldaip != NULL) {
1860 				if (oldaip->ai_newblkno != aip->ai_oldblkno) {
1861 					FREE_LOCK(&lk);
1862 					panic("setup_allocindir_phase2: blkno");
1863 				}
1864 				aip->ai_oldblkno = oldaip->ai_oldblkno;
1865 				freefrag = aip->ai_freefrag;
1866 				aip->ai_freefrag = oldaip->ai_freefrag;
1867 				oldaip->ai_freefrag = NULL;
1868 				free_allocindir(oldaip, NULL);
1869 			}
1870 			LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
1871 			if (ip->i_ump->um_fstype == UFS1)
1872 				((ufs1_daddr_t *)indirdep->ir_savebp->b_data)
1873 				    [aip->ai_offset] = aip->ai_oldblkno;
1874 			else
1875 				((ufs2_daddr_t *)indirdep->ir_savebp->b_data)
1876 				    [aip->ai_offset] = aip->ai_oldblkno;
1877 			FREE_LOCK(&lk);
1878 			if (freefrag != NULL)
1879 				handle_workitem_freefrag(freefrag);
1880 		}
1881 		if (newindirdep) {
1882 			brelse(newindirdep->ir_savebp);
1883 			WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
1884 		}
1885 		if (indirdep)
1886 			break;
1887 		MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
1888 			M_INDIRDEP, M_SOFTDEP_FLAGS);
1889 		newindirdep->ir_list.wk_type = D_INDIRDEP;
1890 		newindirdep->ir_state = ATTACHED;
1891 		if (ip->i_ump->um_fstype == UFS1)
1892 			newindirdep->ir_state |= UFS1FMT;
1893 		LIST_INIT(&newindirdep->ir_deplisthd);
1894 		LIST_INIT(&newindirdep->ir_donehd);
1895 		if (bp->b_blkno == bp->b_lblkno) {
1896 			ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
1897 			    NULL, NULL);
1898 			bp->b_blkno = blkno;
1899 		}
1900 		newindirdep->ir_savebp =
1901 		    getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
1902 		BUF_KERNPROC(newindirdep->ir_savebp);
1903 		bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
1904 	}
1905 }
1906 
1907 /*
1908  * Block de-allocation dependencies.
1909  *
1910  * When blocks are de-allocated, the on-disk pointers must be nullified before
1911  * the blocks are made available for use by other files.  (The true
1912  * requirement is that old pointers must be nullified before new on-disk
1913  * pointers are set.  We chose this slightly more stringent requirement to
1914  * reduce complexity.) Our implementation handles this dependency by updating
1915  * the inode (or indirect block) appropriately but delaying the actual block
1916  * de-allocation (i.e., freemap and free space count manipulation) until
1917  * after the updated versions reach stable storage.  After the disk is
1918  * updated, the blocks can be safely de-allocated whenever it is convenient.
1919  * This implementation handles only the common case of reducing a file's
1920  * length to zero. Other cases are handled by the conventional synchronous
1921  * write approach.
1922  *
1923  * The ffs implementation with which we worked double-checks
1924  * the state of the block pointers and file size as it reduces
1925  * a file's length.  Some of this code is replicated here in our
1926  * soft updates implementation.  The freeblks->fb_chkcnt field is
1927  * used to transfer a part of this information to the procedure
1928  * that eventually de-allocates the blocks.
1929  *
1930  * This routine should be called from the routine that shortens
1931  * a file's length, before the inode's size or block pointers
1932  * are modified. It will save the block pointer information for
1933  * later release and zero the inode so that the calling routine
1934  * can release it.
1935  */
1936 void
1937 softdep_setup_freeblocks(ip, length, flags)
1938 	struct inode *ip;	/* The inode whose length is to be reduced */
1939 	off_t length;		/* The new length for the file */
1940 	int flags;		/* IO_EXT and/or IO_NORMAL */
1941 {
1942 	struct freeblks *freeblks;
1943 	struct inodedep *inodedep;
1944 	struct allocdirect *adp;
1945 	struct vnode *vp;
1946 	struct buf *bp;
1947 	struct fs *fs;
1948 	ufs2_daddr_t extblocks, datablocks;
1949 	int i, delay, error;
1950 
1951 	fs = ip->i_fs;
1952 	if (length != 0)
1953 		panic("softdep_setup_freeblocks: non-zero length");
1954 	MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
1955 		M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
1956 	freeblks->fb_list.wk_type = D_FREEBLKS;
1957 	freeblks->fb_uid = ip->i_uid;
1958 	freeblks->fb_previousinum = ip->i_number;
1959 	freeblks->fb_devvp = ip->i_devvp;
1960 	freeblks->fb_mnt = ITOV(ip)->v_mount;
1961 	extblocks = 0;
1962 	if (fs->fs_magic == FS_UFS2_MAGIC)
1963 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
1964 	datablocks = DIP(ip, i_blocks) - extblocks;
1965 	if ((flags & IO_NORMAL) == 0) {
1966 		freeblks->fb_oldsize = 0;
1967 		freeblks->fb_chkcnt = 0;
1968 	} else {
1969 		freeblks->fb_oldsize = ip->i_size;
1970 		ip->i_size = 0;
1971 		DIP_SET(ip, i_size, 0);
1972 		freeblks->fb_chkcnt = datablocks;
1973 		for (i = 0; i < NDADDR; i++) {
1974 			freeblks->fb_dblks[i] = DIP(ip, i_db[i]);
1975 			DIP_SET(ip, i_db[i], 0);
1976 		}
1977 		for (i = 0; i < NIADDR; i++) {
1978 			freeblks->fb_iblks[i] = DIP(ip, i_ib[i]);
1979 			DIP_SET(ip, i_ib[i], 0);
1980 		}
1981 		/*
1982 		 * If the file was removed, then the space being freed was
1983 		 * accounted for then (see softdep_filereleased()). If the
1984 		 * file is merely being truncated, then we account for it now.
1985 		 */
1986 		if ((ip->i_flag & IN_SPACECOUNTED) == 0)
1987 			fs->fs_pendingblocks += datablocks;
1988 	}
1989 	if ((flags & IO_EXT) == 0) {
1990 		freeblks->fb_oldextsize = 0;
1991 	} else {
1992 		freeblks->fb_oldextsize = ip->i_din2->di_extsize;
1993 		ip->i_din2->di_extsize = 0;
1994 		freeblks->fb_chkcnt += extblocks;
1995 		for (i = 0; i < NXADDR; i++) {
1996 			freeblks->fb_eblks[i] = ip->i_din2->di_extb[i];
1997 			ip->i_din2->di_extb[i] = 0;
1998 		}
1999 	}
2000 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt);
2001 	/*
2002 	 * Push the zero'ed inode to to its disk buffer so that we are free
2003 	 * to delete its dependencies below. Once the dependencies are gone
2004 	 * the buffer can be safely released.
2005 	 */
2006 	if ((error = bread(ip->i_devvp,
2007 	    fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
2008 	    (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
2009 		brelse(bp);
2010 		softdep_error("softdep_setup_freeblocks", error);
2011 	}
2012 	if (ip->i_ump->um_fstype == UFS1)
2013 		*((struct ufs1_dinode *)bp->b_data +
2014 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
2015 	else
2016 		*((struct ufs2_dinode *)bp->b_data +
2017 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
2018 	/*
2019 	 * Find and eliminate any inode dependencies.
2020 	 */
2021 	ACQUIRE_LOCK(&lk);
2022 	(void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep);
2023 	if ((inodedep->id_state & IOSTARTED) != 0) {
2024 		FREE_LOCK(&lk);
2025 		panic("softdep_setup_freeblocks: inode busy");
2026 	}
2027 	/*
2028 	 * Add the freeblks structure to the list of operations that
2029 	 * must await the zero'ed inode being written to disk. If we
2030 	 * still have a bitmap dependency (delay == 0), then the inode
2031 	 * has never been written to disk, so we can process the
2032 	 * freeblks below once we have deleted the dependencies.
2033 	 */
2034 	delay = (inodedep->id_state & DEPCOMPLETE);
2035 	if (delay)
2036 		WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
2037 	/*
2038 	 * Because the file length has been truncated to zero, any
2039 	 * pending block allocation dependency structures associated
2040 	 * with this inode are obsolete and can simply be de-allocated.
2041 	 * We must first merge the two dependency lists to get rid of
2042 	 * any duplicate freefrag structures, then purge the merged list.
2043 	 * If we still have a bitmap dependency, then the inode has never
2044 	 * been written to disk, so we can free any fragments without delay.
2045 	 */
2046 	if (flags & IO_NORMAL) {
2047 		merge_inode_lists(&inodedep->id_newinoupdt,
2048 		    &inodedep->id_inoupdt);
2049 		while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
2050 			free_allocdirect(&inodedep->id_inoupdt, adp, delay);
2051 	}
2052 	if (flags & IO_EXT) {
2053 		merge_inode_lists(&inodedep->id_newextupdt,
2054 		    &inodedep->id_extupdt);
2055 		while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
2056 			free_allocdirect(&inodedep->id_extupdt, adp, delay);
2057 	}
2058 	FREE_LOCK(&lk);
2059 	bdwrite(bp);
2060 	/*
2061 	 * We must wait for any I/O in progress to finish so that
2062 	 * all potential buffers on the dirty list will be visible.
2063 	 * Once they are all there, walk the list and get rid of
2064 	 * any dependencies.
2065 	 */
2066 	vp = ITOV(ip);
2067 	ACQUIRE_LOCK(&lk);
2068 	VI_LOCK(vp);
2069 	drain_output(vp, 1);
2070 restart:
2071 	TAILQ_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
2072 		if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
2073 		    ((flags & IO_NORMAL) == 0 &&
2074 		      (bp->b_xflags & BX_ALTDATA) == 0))
2075 			continue;
2076 		if ((bp = getdirtybuf(&bp, VI_MTX(vp), MNT_WAIT)) == NULL)
2077 			goto restart;
2078 		(void) inodedep_lookup(fs, ip->i_number, 0, &inodedep);
2079 		deallocate_dependencies(bp, inodedep);
2080 		bp->b_flags |= B_INVAL | B_NOCACHE;
2081 		FREE_LOCK(&lk);
2082 		brelse(bp);
2083 		ACQUIRE_LOCK(&lk);
2084 		VI_LOCK(vp);
2085 		goto restart;
2086 	}
2087 	VI_UNLOCK(vp);
2088 	if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) != 0)
2089 		(void) free_inodedep(inodedep);
2090 	FREE_LOCK(&lk);
2091 	/*
2092 	 * If the inode has never been written to disk (delay == 0),
2093 	 * then we can process the freeblks now that we have deleted
2094 	 * the dependencies.
2095 	 */
2096 	if (!delay)
2097 		handle_workitem_freeblocks(freeblks, 0);
2098 }
2099 
2100 /*
2101  * Reclaim any dependency structures from a buffer that is about to
2102  * be reallocated to a new vnode. The buffer must be locked, thus,
2103  * no I/O completion operations can occur while we are manipulating
2104  * its associated dependencies. The mutex is held so that other I/O's
2105  * associated with related dependencies do not occur.
2106  */
2107 static void
2108 deallocate_dependencies(bp, inodedep)
2109 	struct buf *bp;
2110 	struct inodedep *inodedep;
2111 {
2112 	struct worklist *wk;
2113 	struct indirdep *indirdep;
2114 	struct allocindir *aip;
2115 	struct pagedep *pagedep;
2116 	struct dirrem *dirrem;
2117 	struct diradd *dap;
2118 	int i;
2119 
2120 	while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2121 		switch (wk->wk_type) {
2122 
2123 		case D_INDIRDEP:
2124 			indirdep = WK_INDIRDEP(wk);
2125 			/*
2126 			 * None of the indirect pointers will ever be visible,
2127 			 * so they can simply be tossed. GOINGAWAY ensures
2128 			 * that allocated pointers will be saved in the buffer
2129 			 * cache until they are freed. Note that they will
2130 			 * only be able to be found by their physical address
2131 			 * since the inode mapping the logical address will
2132 			 * be gone. The save buffer used for the safe copy
2133 			 * was allocated in setup_allocindir_phase2 using
2134 			 * the physical address so it could be used for this
2135 			 * purpose. Hence we swap the safe copy with the real
2136 			 * copy, allowing the safe copy to be freed and holding
2137 			 * on to the real copy for later use in indir_trunc.
2138 			 */
2139 			if (indirdep->ir_state & GOINGAWAY) {
2140 				FREE_LOCK(&lk);
2141 				panic("deallocate_dependencies: already gone");
2142 			}
2143 			indirdep->ir_state |= GOINGAWAY;
2144 			VFSTOUFS(bp->b_vp->v_mount)->um_numindirdeps += 1;
2145 			while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
2146 				free_allocindir(aip, inodedep);
2147 			if (bp->b_lblkno >= 0 ||
2148 			    bp->b_blkno != indirdep->ir_savebp->b_lblkno) {
2149 				FREE_LOCK(&lk);
2150 				panic("deallocate_dependencies: not indir");
2151 			}
2152 			bcopy(bp->b_data, indirdep->ir_savebp->b_data,
2153 			    bp->b_bcount);
2154 			WORKLIST_REMOVE(wk);
2155 			WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
2156 			continue;
2157 
2158 		case D_PAGEDEP:
2159 			pagedep = WK_PAGEDEP(wk);
2160 			/*
2161 			 * None of the directory additions will ever be
2162 			 * visible, so they can simply be tossed.
2163 			 */
2164 			for (i = 0; i < DAHASHSZ; i++)
2165 				while ((dap =
2166 				    LIST_FIRST(&pagedep->pd_diraddhd[i])))
2167 					free_diradd(dap);
2168 			while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
2169 				free_diradd(dap);
2170 			/*
2171 			 * Copy any directory remove dependencies to the list
2172 			 * to be processed after the zero'ed inode is written.
2173 			 * If the inode has already been written, then they
2174 			 * can be dumped directly onto the work list.
2175 			 */
2176 			LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
2177 				LIST_REMOVE(dirrem, dm_next);
2178 				dirrem->dm_dirinum = pagedep->pd_ino;
2179 				if (inodedep == NULL ||
2180 				    (inodedep->id_state & ALLCOMPLETE) ==
2181 				     ALLCOMPLETE)
2182 					add_to_worklist(&dirrem->dm_list);
2183 				else
2184 					WORKLIST_INSERT(&inodedep->id_bufwait,
2185 					    &dirrem->dm_list);
2186 			}
2187 			if ((pagedep->pd_state & NEWBLOCK) != 0) {
2188 				LIST_FOREACH(wk, &inodedep->id_bufwait, wk_list)
2189 					if (wk->wk_type == D_NEWDIRBLK &&
2190 					    WK_NEWDIRBLK(wk)->db_pagedep ==
2191 					      pagedep)
2192 						break;
2193 				if (wk != NULL) {
2194 					WORKLIST_REMOVE(wk);
2195 					free_newdirblk(WK_NEWDIRBLK(wk));
2196 				} else {
2197 					FREE_LOCK(&lk);
2198 					panic("deallocate_dependencies: "
2199 					      "lost pagedep");
2200 				}
2201 			}
2202 			WORKLIST_REMOVE(&pagedep->pd_list);
2203 			LIST_REMOVE(pagedep, pd_hash);
2204 			WORKITEM_FREE(pagedep, D_PAGEDEP);
2205 			continue;
2206 
2207 		case D_ALLOCINDIR:
2208 			free_allocindir(WK_ALLOCINDIR(wk), inodedep);
2209 			continue;
2210 
2211 		case D_ALLOCDIRECT:
2212 		case D_INODEDEP:
2213 			FREE_LOCK(&lk);
2214 			panic("deallocate_dependencies: Unexpected type %s",
2215 			    TYPENAME(wk->wk_type));
2216 			/* NOTREACHED */
2217 
2218 		default:
2219 			FREE_LOCK(&lk);
2220 			panic("deallocate_dependencies: Unknown type %s",
2221 			    TYPENAME(wk->wk_type));
2222 			/* NOTREACHED */
2223 		}
2224 	}
2225 }
2226 
2227 /*
2228  * Free an allocdirect. Generate a new freefrag work request if appropriate.
2229  * This routine must be called with splbio interrupts blocked.
2230  */
2231 static void
2232 free_allocdirect(adphead, adp, delay)
2233 	struct allocdirectlst *adphead;
2234 	struct allocdirect *adp;
2235 	int delay;
2236 {
2237 	struct newdirblk *newdirblk;
2238 	struct worklist *wk;
2239 
2240 #ifdef DEBUG
2241 	if (lk.lkt_held == NOHOLDER)
2242 		panic("free_allocdirect: lock not held");
2243 #endif
2244 	if ((adp->ad_state & DEPCOMPLETE) == 0)
2245 		LIST_REMOVE(adp, ad_deps);
2246 	TAILQ_REMOVE(adphead, adp, ad_next);
2247 	if ((adp->ad_state & COMPLETE) == 0)
2248 		WORKLIST_REMOVE(&adp->ad_list);
2249 	if (adp->ad_freefrag != NULL) {
2250 		if (delay)
2251 			WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2252 			    &adp->ad_freefrag->ff_list);
2253 		else
2254 			add_to_worklist(&adp->ad_freefrag->ff_list);
2255 	}
2256 	if ((wk = LIST_FIRST(&adp->ad_newdirblk)) != NULL) {
2257 		newdirblk = WK_NEWDIRBLK(wk);
2258 		WORKLIST_REMOVE(&newdirblk->db_list);
2259 		if (LIST_FIRST(&adp->ad_newdirblk) != NULL)
2260 			panic("free_allocdirect: extra newdirblk");
2261 		if (delay)
2262 			WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2263 			    &newdirblk->db_list);
2264 		else
2265 			free_newdirblk(newdirblk);
2266 	}
2267 	WORKITEM_FREE(adp, D_ALLOCDIRECT);
2268 }
2269 
2270 /*
2271  * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
2272  * This routine must be called with splbio interrupts blocked.
2273  */
2274 static void
2275 free_newdirblk(newdirblk)
2276 	struct newdirblk *newdirblk;
2277 {
2278 	struct pagedep *pagedep;
2279 	struct diradd *dap;
2280 	int i;
2281 
2282 #ifdef DEBUG
2283 	if (lk.lkt_held == NOHOLDER)
2284 		panic("free_newdirblk: lock not held");
2285 #endif
2286 	/*
2287 	 * If the pagedep is still linked onto the directory buffer
2288 	 * dependency chain, then some of the entries on the
2289 	 * pd_pendinghd list may not be committed to disk yet. In
2290 	 * this case, we will simply clear the NEWBLOCK flag and
2291 	 * let the pd_pendinghd list be processed when the pagedep
2292 	 * is next written. If the pagedep is no longer on the buffer
2293 	 * dependency chain, then all the entries on the pd_pending
2294 	 * list are committed to disk and we can free them here.
2295 	 */
2296 	pagedep = newdirblk->db_pagedep;
2297 	pagedep->pd_state &= ~NEWBLOCK;
2298 	if ((pagedep->pd_state & ONWORKLIST) == 0)
2299 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
2300 			free_diradd(dap);
2301 	/*
2302 	 * If no dependencies remain, the pagedep will be freed.
2303 	 */
2304 	for (i = 0; i < DAHASHSZ; i++)
2305 		if (LIST_FIRST(&pagedep->pd_diraddhd[i]) != NULL)
2306 			break;
2307 	if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0) {
2308 		LIST_REMOVE(pagedep, pd_hash);
2309 		WORKITEM_FREE(pagedep, D_PAGEDEP);
2310 	}
2311 	WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2312 }
2313 
2314 /*
2315  * Prepare an inode to be freed. The actual free operation is not
2316  * done until the zero'ed inode has been written to disk.
2317  */
2318 void
2319 softdep_freefile(pvp, ino, mode)
2320 	struct vnode *pvp;
2321 	ino_t ino;
2322 	int mode;
2323 {
2324 	struct inode *ip = VTOI(pvp);
2325 	struct inodedep *inodedep;
2326 	struct freefile *freefile;
2327 
2328 	/*
2329 	 * This sets up the inode de-allocation dependency.
2330 	 */
2331 	MALLOC(freefile, struct freefile *, sizeof(struct freefile),
2332 		M_FREEFILE, M_SOFTDEP_FLAGS);
2333 	freefile->fx_list.wk_type = D_FREEFILE;
2334 	freefile->fx_list.wk_state = 0;
2335 	freefile->fx_mode = mode;
2336 	freefile->fx_oldinum = ino;
2337 	freefile->fx_devvp = ip->i_devvp;
2338 	freefile->fx_mnt = ITOV(ip)->v_mount;
2339 	if ((ip->i_flag & IN_SPACECOUNTED) == 0)
2340 		ip->i_fs->fs_pendinginodes += 1;
2341 
2342 	/*
2343 	 * If the inodedep does not exist, then the zero'ed inode has
2344 	 * been written to disk. If the allocated inode has never been
2345 	 * written to disk, then the on-disk inode is zero'ed. In either
2346 	 * case we can free the file immediately.
2347 	 */
2348 	ACQUIRE_LOCK(&lk);
2349 	if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0 ||
2350 	    check_inode_unwritten(inodedep)) {
2351 		FREE_LOCK(&lk);
2352 		handle_workitem_freefile(freefile);
2353 		return;
2354 	}
2355 	WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2356 	FREE_LOCK(&lk);
2357 }
2358 
2359 /*
2360  * Check to see if an inode has never been written to disk. If
2361  * so free the inodedep and return success, otherwise return failure.
2362  * This routine must be called with splbio interrupts blocked.
2363  *
2364  * If we still have a bitmap dependency, then the inode has never
2365  * been written to disk. Drop the dependency as it is no longer
2366  * necessary since the inode is being deallocated. We set the
2367  * ALLCOMPLETE flags since the bitmap now properly shows that the
2368  * inode is not allocated. Even if the inode is actively being
2369  * written, it has been rolled back to its zero'ed state, so we
2370  * are ensured that a zero inode is what is on the disk. For short
2371  * lived files, this change will usually result in removing all the
2372  * dependencies from the inode so that it can be freed immediately.
2373  */
2374 static int
2375 check_inode_unwritten(inodedep)
2376 	struct inodedep *inodedep;
2377 {
2378 
2379 	if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2380 	    LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2381 	    LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2382 	    LIST_FIRST(&inodedep->id_inowait) != NULL ||
2383 	    TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2384 	    TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2385 	    TAILQ_FIRST(&inodedep->id_extupdt) != NULL ||
2386 	    TAILQ_FIRST(&inodedep->id_newextupdt) != NULL ||
2387 	    inodedep->id_nlinkdelta != 0)
2388 		return (0);
2389 	inodedep->id_state |= ALLCOMPLETE;
2390 	LIST_REMOVE(inodedep, id_deps);
2391 	inodedep->id_buf = NULL;
2392 	if (inodedep->id_state & ONWORKLIST)
2393 		WORKLIST_REMOVE(&inodedep->id_list);
2394 	if (inodedep->id_savedino1 != NULL) {
2395 		FREE(inodedep->id_savedino1, M_INODEDEP);
2396 		inodedep->id_savedino1 = NULL;
2397 	}
2398 	if (free_inodedep(inodedep) == 0) {
2399 		FREE_LOCK(&lk);
2400 		panic("check_inode_unwritten: busy inode");
2401 	}
2402 	return (1);
2403 }
2404 
2405 /*
2406  * Try to free an inodedep structure. Return 1 if it could be freed.
2407  */
2408 static int
2409 free_inodedep(inodedep)
2410 	struct inodedep *inodedep;
2411 {
2412 
2413 	if ((inodedep->id_state & ONWORKLIST) != 0 ||
2414 	    (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2415 	    LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2416 	    LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2417 	    LIST_FIRST(&inodedep->id_inowait) != NULL ||
2418 	    TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2419 	    TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2420 	    TAILQ_FIRST(&inodedep->id_extupdt) != NULL ||
2421 	    TAILQ_FIRST(&inodedep->id_newextupdt) != NULL ||
2422 	    inodedep->id_nlinkdelta != 0 || inodedep->id_savedino1 != NULL)
2423 		return (0);
2424 	LIST_REMOVE(inodedep, id_hash);
2425 	WORKITEM_FREE(inodedep, D_INODEDEP);
2426 	num_inodedep -= 1;
2427 	return (1);
2428 }
2429 
2430 /*
2431  * This workitem routine performs the block de-allocation.
2432  * The workitem is added to the pending list after the updated
2433  * inode block has been written to disk.  As mentioned above,
2434  * checks regarding the number of blocks de-allocated (compared
2435  * to the number of blocks allocated for the file) are also
2436  * performed in this function.
2437  */
2438 static void
2439 handle_workitem_freeblocks(freeblks, flags)
2440 	struct freeblks *freeblks;
2441 	int flags;
2442 {
2443 	struct inode *ip;
2444 	struct vnode *vp;
2445 	struct fs *fs;
2446 	int i, nblocks, level, bsize;
2447 	ufs2_daddr_t bn, blocksreleased = 0;
2448 	int error, allerror = 0;
2449 	ufs_lbn_t baselbns[NIADDR], tmpval;
2450 
2451 	fs = VFSTOUFS(freeblks->fb_mnt)->um_fs;
2452 	tmpval = 1;
2453 	baselbns[0] = NDADDR;
2454 	for (i = 1; i < NIADDR; i++) {
2455 		tmpval *= NINDIR(fs);
2456 		baselbns[i] = baselbns[i - 1] + tmpval;
2457 	}
2458 	nblocks = btodb(fs->fs_bsize);
2459 	blocksreleased = 0;
2460 	/*
2461 	 * Release all extended attribute blocks or frags.
2462 	 */
2463 	if (freeblks->fb_oldextsize > 0) {
2464 		for (i = (NXADDR - 1); i >= 0; i--) {
2465 			if ((bn = freeblks->fb_eblks[i]) == 0)
2466 				continue;
2467 			bsize = sblksize(fs, freeblks->fb_oldextsize, i);
2468 			ffs_blkfree(fs, freeblks->fb_devvp, bn, bsize,
2469 			    freeblks->fb_previousinum);
2470 			blocksreleased += btodb(bsize);
2471 		}
2472 	}
2473 	/*
2474 	 * Release all data blocks or frags.
2475 	 */
2476 	if (freeblks->fb_oldsize > 0) {
2477 		/*
2478 		 * Indirect blocks first.
2479 		 */
2480 		for (level = (NIADDR - 1); level >= 0; level--) {
2481 			if ((bn = freeblks->fb_iblks[level]) == 0)
2482 				continue;
2483 			if ((error = indir_trunc(freeblks, fsbtodb(fs, bn),
2484 			    level, baselbns[level], &blocksreleased)) == 0)
2485 				allerror = error;
2486 			ffs_blkfree(fs, freeblks->fb_devvp, bn, fs->fs_bsize,
2487 			    freeblks->fb_previousinum);
2488 			fs->fs_pendingblocks -= nblocks;
2489 			blocksreleased += nblocks;
2490 		}
2491 		/*
2492 		 * All direct blocks or frags.
2493 		 */
2494 		for (i = (NDADDR - 1); i >= 0; i--) {
2495 			if ((bn = freeblks->fb_dblks[i]) == 0)
2496 				continue;
2497 			bsize = sblksize(fs, freeblks->fb_oldsize, i);
2498 			ffs_blkfree(fs, freeblks->fb_devvp, bn, bsize,
2499 			    freeblks->fb_previousinum);
2500 			fs->fs_pendingblocks -= btodb(bsize);
2501 			blocksreleased += btodb(bsize);
2502 		}
2503 	}
2504 	/*
2505 	 * If we still have not finished background cleanup, then check
2506 	 * to see if the block count needs to be adjusted.
2507 	 */
2508 	if (freeblks->fb_chkcnt != blocksreleased &&
2509 	    (fs->fs_flags & FS_UNCLEAN) != 0 &&
2510 	    VFS_VGET(freeblks->fb_mnt, freeblks->fb_previousinum,
2511 	    (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp) == 0) {
2512 		ip = VTOI(vp);
2513 		DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + \
2514 		    freeblks->fb_chkcnt - blocksreleased);
2515 		ip->i_flag |= IN_CHANGE;
2516 		vput(vp);
2517 	}
2518 
2519 #ifdef DIAGNOSTIC
2520 	if (freeblks->fb_chkcnt != blocksreleased &&
2521 	    ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0))
2522 		printf("handle_workitem_freeblocks: block count\n");
2523 	if (allerror)
2524 		softdep_error("handle_workitem_freeblks", allerror);
2525 #endif /* DIAGNOSTIC */
2526 
2527 	WORKITEM_FREE(freeblks, D_FREEBLKS);
2528 }
2529 
2530 /*
2531  * Release blocks associated with the inode ip and stored in the indirect
2532  * block dbn. If level is greater than SINGLE, the block is an indirect block
2533  * and recursive calls to indirtrunc must be used to cleanse other indirect
2534  * blocks.
2535  */
2536 static int
2537 indir_trunc(freeblks, dbn, level, lbn, countp)
2538 	struct freeblks *freeblks;
2539 	ufs2_daddr_t dbn;
2540 	int level;
2541 	ufs_lbn_t lbn;
2542 	ufs2_daddr_t *countp;
2543 {
2544 	struct buf *bp;
2545 	struct fs *fs;
2546 	struct worklist *wk;
2547 	struct indirdep *indirdep;
2548 	ufs1_daddr_t *bap1 = 0;
2549 	ufs2_daddr_t nb, *bap2 = 0;
2550 	ufs_lbn_t lbnadd;
2551 	int i, nblocks, ufs1fmt;
2552 	int error, allerror = 0;
2553 
2554 	fs = VFSTOUFS(freeblks->fb_mnt)->um_fs;
2555 	lbnadd = 1;
2556 	for (i = level; i > 0; i--)
2557 		lbnadd *= NINDIR(fs);
2558 	/*
2559 	 * Get buffer of block pointers to be freed. This routine is not
2560 	 * called until the zero'ed inode has been written, so it is safe
2561 	 * to free blocks as they are encountered. Because the inode has
2562 	 * been zero'ed, calls to bmap on these blocks will fail. So, we
2563 	 * have to use the on-disk address and the block device for the
2564 	 * filesystem to look them up. If the file was deleted before its
2565 	 * indirect blocks were all written to disk, the routine that set
2566 	 * us up (deallocate_dependencies) will have arranged to leave
2567 	 * a complete copy of the indirect block in memory for our use.
2568 	 * Otherwise we have to read the blocks in from the disk.
2569 	 */
2570 #ifdef notyet
2571 	bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0,
2572 	    GB_NOCREAT);
2573 #else
2574 	bp = incore(freeblks->fb_devvp, dbn);
2575 #endif
2576 	ACQUIRE_LOCK(&lk);
2577 	if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2578 		if (wk->wk_type != D_INDIRDEP ||
2579 		    (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2580 		    (indirdep->ir_state & GOINGAWAY) == 0) {
2581 			FREE_LOCK(&lk);
2582 			panic("indir_trunc: lost indirdep");
2583 		}
2584 		WORKLIST_REMOVE(wk);
2585 		WORKITEM_FREE(indirdep, D_INDIRDEP);
2586 		if (LIST_FIRST(&bp->b_dep) != NULL) {
2587 			FREE_LOCK(&lk);
2588 			panic("indir_trunc: dangling dep");
2589 		}
2590 		VFSTOUFS(freeblks->fb_mnt)->um_numindirdeps -= 1;
2591 		FREE_LOCK(&lk);
2592 	} else {
2593 #ifdef notyet
2594 		if (bp)
2595 			brelse(bp);
2596 #endif
2597 		FREE_LOCK(&lk);
2598 		error = bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
2599 		    NOCRED, &bp);
2600 		if (error) {
2601 			brelse(bp);
2602 			return (error);
2603 		}
2604 	}
2605 	/*
2606 	 * Recursively free indirect blocks.
2607 	 */
2608 	if (VFSTOUFS(freeblks->fb_mnt)->um_fstype == UFS1) {
2609 		ufs1fmt = 1;
2610 		bap1 = (ufs1_daddr_t *)bp->b_data;
2611 	} else {
2612 		ufs1fmt = 0;
2613 		bap2 = (ufs2_daddr_t *)bp->b_data;
2614 	}
2615 	nblocks = btodb(fs->fs_bsize);
2616 	for (i = NINDIR(fs) - 1; i >= 0; i--) {
2617 		if (ufs1fmt)
2618 			nb = bap1[i];
2619 		else
2620 			nb = bap2[i];
2621 		if (nb == 0)
2622 			continue;
2623 		if (level != 0) {
2624 			if ((error = indir_trunc(freeblks, fsbtodb(fs, nb),
2625 			     level - 1, lbn + (i * lbnadd), countp)) != 0)
2626 				allerror = error;
2627 		}
2628 		ffs_blkfree(fs, freeblks->fb_devvp, nb, fs->fs_bsize,
2629 		    freeblks->fb_previousinum);
2630 		fs->fs_pendingblocks -= nblocks;
2631 		*countp += nblocks;
2632 	}
2633 	bp->b_flags |= B_INVAL | B_NOCACHE;
2634 	brelse(bp);
2635 	return (allerror);
2636 }
2637 
2638 /*
2639  * Free an allocindir.
2640  * This routine must be called with splbio interrupts blocked.
2641  */
2642 static void
2643 free_allocindir(aip, inodedep)
2644 	struct allocindir *aip;
2645 	struct inodedep *inodedep;
2646 {
2647 	struct freefrag *freefrag;
2648 
2649 #ifdef DEBUG
2650 	if (lk.lkt_held == NOHOLDER)
2651 		panic("free_allocindir: lock not held");
2652 #endif
2653 	if ((aip->ai_state & DEPCOMPLETE) == 0)
2654 		LIST_REMOVE(aip, ai_deps);
2655 	if (aip->ai_state & ONWORKLIST)
2656 		WORKLIST_REMOVE(&aip->ai_list);
2657 	LIST_REMOVE(aip, ai_next);
2658 	if ((freefrag = aip->ai_freefrag) != NULL) {
2659 		if (inodedep == NULL)
2660 			add_to_worklist(&freefrag->ff_list);
2661 		else
2662 			WORKLIST_INSERT(&inodedep->id_bufwait,
2663 			    &freefrag->ff_list);
2664 	}
2665 	WORKITEM_FREE(aip, D_ALLOCINDIR);
2666 }
2667 
2668 /*
2669  * Directory entry addition dependencies.
2670  *
2671  * When adding a new directory entry, the inode (with its incremented link
2672  * count) must be written to disk before the directory entry's pointer to it.
2673  * Also, if the inode is newly allocated, the corresponding freemap must be
2674  * updated (on disk) before the directory entry's pointer. These requirements
2675  * are met via undo/redo on the directory entry's pointer, which consists
2676  * simply of the inode number.
2677  *
2678  * As directory entries are added and deleted, the free space within a
2679  * directory block can become fragmented.  The ufs filesystem will compact
2680  * a fragmented directory block to make space for a new entry. When this
2681  * occurs, the offsets of previously added entries change. Any "diradd"
2682  * dependency structures corresponding to these entries must be updated with
2683  * the new offsets.
2684  */
2685 
2686 /*
2687  * This routine is called after the in-memory inode's link
2688  * count has been incremented, but before the directory entry's
2689  * pointer to the inode has been set.
2690  */
2691 int
2692 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
2693 	struct buf *bp;		/* buffer containing directory block */
2694 	struct inode *dp;	/* inode for directory */
2695 	off_t diroffset;	/* offset of new entry in directory */
2696 	ino_t newinum;		/* inode referenced by new directory entry */
2697 	struct buf *newdirbp;	/* non-NULL => contents of new mkdir */
2698 	int isnewblk;		/* entry is in a newly allocated block */
2699 {
2700 	int offset;		/* offset of new entry within directory block */
2701 	ufs_lbn_t lbn;		/* block in directory containing new entry */
2702 	struct fs *fs;
2703 	struct diradd *dap;
2704 	struct allocdirect *adp;
2705 	struct pagedep *pagedep;
2706 	struct inodedep *inodedep;
2707 	struct newdirblk *newdirblk = 0;
2708 	struct mkdir *mkdir1, *mkdir2;
2709 
2710 	/*
2711 	 * Whiteouts have no dependencies.
2712 	 */
2713 	if (newinum == WINO) {
2714 		if (newdirbp != NULL)
2715 			bdwrite(newdirbp);
2716 		return (0);
2717 	}
2718 
2719 	fs = dp->i_fs;
2720 	lbn = lblkno(fs, diroffset);
2721 	offset = blkoff(fs, diroffset);
2722 	MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2723 		M_SOFTDEP_FLAGS|M_ZERO);
2724 	dap->da_list.wk_type = D_DIRADD;
2725 	dap->da_offset = offset;
2726 	dap->da_newinum = newinum;
2727 	dap->da_state = ATTACHED;
2728 	if (isnewblk && lbn < NDADDR && fragoff(fs, diroffset) == 0) {
2729 		MALLOC(newdirblk, struct newdirblk *, sizeof(struct newdirblk),
2730 		    M_NEWDIRBLK, M_SOFTDEP_FLAGS);
2731 		newdirblk->db_list.wk_type = D_NEWDIRBLK;
2732 		newdirblk->db_state = 0;
2733 	}
2734 	if (newdirbp == NULL) {
2735 		dap->da_state |= DEPCOMPLETE;
2736 		ACQUIRE_LOCK(&lk);
2737 	} else {
2738 		dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
2739 		MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2740 		    M_SOFTDEP_FLAGS);
2741 		mkdir1->md_list.wk_type = D_MKDIR;
2742 		mkdir1->md_state = MKDIR_BODY;
2743 		mkdir1->md_diradd = dap;
2744 		MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2745 		    M_SOFTDEP_FLAGS);
2746 		mkdir2->md_list.wk_type = D_MKDIR;
2747 		mkdir2->md_state = MKDIR_PARENT;
2748 		mkdir2->md_diradd = dap;
2749 		/*
2750 		 * Dependency on "." and ".." being written to disk.
2751 		 */
2752 		mkdir1->md_buf = newdirbp;
2753 		ACQUIRE_LOCK(&lk);
2754 		LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
2755 		WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
2756 		FREE_LOCK(&lk);
2757 		bdwrite(newdirbp);
2758 		/*
2759 		 * Dependency on link count increase for parent directory
2760 		 */
2761 		ACQUIRE_LOCK(&lk);
2762 		if (inodedep_lookup(fs, dp->i_number, 0, &inodedep) == 0
2763 		    || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2764 			dap->da_state &= ~MKDIR_PARENT;
2765 			WORKITEM_FREE(mkdir2, D_MKDIR);
2766 		} else {
2767 			LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
2768 			WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
2769 		}
2770 	}
2771 	/*
2772 	 * Link into parent directory pagedep to await its being written.
2773 	 */
2774 	if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2775 		WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2776 	dap->da_pagedep = pagedep;
2777 	LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
2778 	    da_pdlist);
2779 	/*
2780 	 * Link into its inodedep. Put it on the id_bufwait list if the inode
2781 	 * is not yet written. If it is written, do the post-inode write
2782 	 * processing to put it on the id_pendinghd list.
2783 	 */
2784 	(void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep);
2785 	if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
2786 		diradd_inode_written(dap, inodedep);
2787 	else
2788 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2789 	if (isnewblk) {
2790 		/*
2791 		 * Directories growing into indirect blocks are rare
2792 		 * enough and the frequency of new block allocation
2793 		 * in those cases even more rare, that we choose not
2794 		 * to bother tracking them. Rather we simply force the
2795 		 * new directory entry to disk.
2796 		 */
2797 		if (lbn >= NDADDR) {
2798 			FREE_LOCK(&lk);
2799 			/*
2800 			 * We only have a new allocation when at the
2801 			 * beginning of a new block, not when we are
2802 			 * expanding into an existing block.
2803 			 */
2804 			if (blkoff(fs, diroffset) == 0)
2805 				return (1);
2806 			return (0);
2807 		}
2808 		/*
2809 		 * We only have a new allocation when at the beginning
2810 		 * of a new fragment, not when we are expanding into an
2811 		 * existing fragment. Also, there is nothing to do if we
2812 		 * are already tracking this block.
2813 		 */
2814 		if (fragoff(fs, diroffset) != 0) {
2815 			FREE_LOCK(&lk);
2816 			return (0);
2817 		}
2818 		if ((pagedep->pd_state & NEWBLOCK) != 0) {
2819 			WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2820 			FREE_LOCK(&lk);
2821 			return (0);
2822 		}
2823 		/*
2824 		 * Find our associated allocdirect and have it track us.
2825 		 */
2826 		if (inodedep_lookup(fs, dp->i_number, 0, &inodedep) == 0)
2827 			panic("softdep_setup_directory_add: lost inodedep");
2828 		adp = TAILQ_LAST(&inodedep->id_newinoupdt, allocdirectlst);
2829 		if (adp == NULL || adp->ad_lbn != lbn) {
2830 			FREE_LOCK(&lk);
2831 			panic("softdep_setup_directory_add: lost entry");
2832 		}
2833 		pagedep->pd_state |= NEWBLOCK;
2834 		newdirblk->db_pagedep = pagedep;
2835 		WORKLIST_INSERT(&adp->ad_newdirblk, &newdirblk->db_list);
2836 	}
2837 	FREE_LOCK(&lk);
2838 	return (0);
2839 }
2840 
2841 /*
2842  * This procedure is called to change the offset of a directory
2843  * entry when compacting a directory block which must be owned
2844  * exclusively by the caller. Note that the actual entry movement
2845  * must be done in this procedure to ensure that no I/O completions
2846  * occur while the move is in progress.
2847  */
2848 void
2849 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
2850 	struct inode *dp;	/* inode for directory */
2851 	caddr_t base;		/* address of dp->i_offset */
2852 	caddr_t oldloc;		/* address of old directory location */
2853 	caddr_t newloc;		/* address of new directory location */
2854 	int entrysize;		/* size of directory entry */
2855 {
2856 	int offset, oldoffset, newoffset;
2857 	struct pagedep *pagedep;
2858 	struct diradd *dap;
2859 	ufs_lbn_t lbn;
2860 
2861 	ACQUIRE_LOCK(&lk);
2862 	lbn = lblkno(dp->i_fs, dp->i_offset);
2863 	offset = blkoff(dp->i_fs, dp->i_offset);
2864 	if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
2865 		goto done;
2866 	oldoffset = offset + (oldloc - base);
2867 	newoffset = offset + (newloc - base);
2868 
2869 	LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
2870 		if (dap->da_offset != oldoffset)
2871 			continue;
2872 		dap->da_offset = newoffset;
2873 		if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
2874 			break;
2875 		LIST_REMOVE(dap, da_pdlist);
2876 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
2877 		    dap, da_pdlist);
2878 		break;
2879 	}
2880 	if (dap == NULL) {
2881 
2882 		LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
2883 			if (dap->da_offset == oldoffset) {
2884 				dap->da_offset = newoffset;
2885 				break;
2886 			}
2887 		}
2888 	}
2889 done:
2890 	bcopy(oldloc, newloc, entrysize);
2891 	FREE_LOCK(&lk);
2892 }
2893 
2894 /*
2895  * Free a diradd dependency structure. This routine must be called
2896  * with splbio interrupts blocked.
2897  */
2898 static void
2899 free_diradd(dap)
2900 	struct diradd *dap;
2901 {
2902 	struct dirrem *dirrem;
2903 	struct pagedep *pagedep;
2904 	struct inodedep *inodedep;
2905 	struct mkdir *mkdir, *nextmd;
2906 
2907 #ifdef DEBUG
2908 	if (lk.lkt_held == NOHOLDER)
2909 		panic("free_diradd: lock not held");
2910 #endif
2911 	WORKLIST_REMOVE(&dap->da_list);
2912 	LIST_REMOVE(dap, da_pdlist);
2913 	if ((dap->da_state & DIRCHG) == 0) {
2914 		pagedep = dap->da_pagedep;
2915 	} else {
2916 		dirrem = dap->da_previous;
2917 		pagedep = dirrem->dm_pagedep;
2918 		dirrem->dm_dirinum = pagedep->pd_ino;
2919 		add_to_worklist(&dirrem->dm_list);
2920 	}
2921 	if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum,
2922 	    0, &inodedep) != 0)
2923 		(void) free_inodedep(inodedep);
2924 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2925 		for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
2926 			nextmd = LIST_NEXT(mkdir, md_mkdirs);
2927 			if (mkdir->md_diradd != dap)
2928 				continue;
2929 			dap->da_state &= ~mkdir->md_state;
2930 			WORKLIST_REMOVE(&mkdir->md_list);
2931 			LIST_REMOVE(mkdir, md_mkdirs);
2932 			WORKITEM_FREE(mkdir, D_MKDIR);
2933 		}
2934 		if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2935 			FREE_LOCK(&lk);
2936 			panic("free_diradd: unfound ref");
2937 		}
2938 	}
2939 	WORKITEM_FREE(dap, D_DIRADD);
2940 }
2941 
2942 /*
2943  * Directory entry removal dependencies.
2944  *
2945  * When removing a directory entry, the entry's inode pointer must be
2946  * zero'ed on disk before the corresponding inode's link count is decremented
2947  * (possibly freeing the inode for re-use). This dependency is handled by
2948  * updating the directory entry but delaying the inode count reduction until
2949  * after the directory block has been written to disk. After this point, the
2950  * inode count can be decremented whenever it is convenient.
2951  */
2952 
2953 /*
2954  * This routine should be called immediately after removing
2955  * a directory entry.  The inode's link count should not be
2956  * decremented by the calling procedure -- the soft updates
2957  * code will do this task when it is safe.
2958  */
2959 void
2960 softdep_setup_remove(bp, dp, ip, isrmdir)
2961 	struct buf *bp;		/* buffer containing directory block */
2962 	struct inode *dp;	/* inode for the directory being modified */
2963 	struct inode *ip;	/* inode for directory entry being removed */
2964 	int isrmdir;		/* indicates if doing RMDIR */
2965 {
2966 	struct dirrem *dirrem, *prevdirrem;
2967 
2968 	/*
2969 	 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
2970 	 */
2971 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2972 
2973 	/*
2974 	 * If the COMPLETE flag is clear, then there were no active
2975 	 * entries and we want to roll back to a zeroed entry until
2976 	 * the new inode is committed to disk. If the COMPLETE flag is
2977 	 * set then we have deleted an entry that never made it to
2978 	 * disk. If the entry we deleted resulted from a name change,
2979 	 * then the old name still resides on disk. We cannot delete
2980 	 * its inode (returned to us in prevdirrem) until the zeroed
2981 	 * directory entry gets to disk. The new inode has never been
2982 	 * referenced on the disk, so can be deleted immediately.
2983 	 */
2984 	if ((dirrem->dm_state & COMPLETE) == 0) {
2985 		LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
2986 		    dm_next);
2987 		FREE_LOCK(&lk);
2988 	} else {
2989 		if (prevdirrem != NULL)
2990 			LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
2991 			    prevdirrem, dm_next);
2992 		dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
2993 		FREE_LOCK(&lk);
2994 		handle_workitem_remove(dirrem, NULL);
2995 	}
2996 }
2997 
2998 /*
2999  * Allocate a new dirrem if appropriate and return it along with
3000  * its associated pagedep. Called without a lock, returns with lock.
3001  */
3002 static long num_dirrem;		/* number of dirrem allocated */
3003 static struct dirrem *
3004 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
3005 	struct buf *bp;		/* buffer containing directory block */
3006 	struct inode *dp;	/* inode for the directory being modified */
3007 	struct inode *ip;	/* inode for directory entry being removed */
3008 	int isrmdir;		/* indicates if doing RMDIR */
3009 	struct dirrem **prevdirremp; /* previously referenced inode, if any */
3010 {
3011 	int offset;
3012 	ufs_lbn_t lbn;
3013 	struct diradd *dap;
3014 	struct dirrem *dirrem;
3015 	struct pagedep *pagedep;
3016 
3017 	/*
3018 	 * Whiteouts have no deletion dependencies.
3019 	 */
3020 	if (ip == NULL)
3021 		panic("newdirrem: whiteout");
3022 	/*
3023 	 * If we are over our limit, try to improve the situation.
3024 	 * Limiting the number of dirrem structures will also limit
3025 	 * the number of freefile and freeblks structures.
3026 	 */
3027 	if (num_dirrem > max_softdeps / 2)
3028 		(void) request_cleanup(FLUSH_REMOVE, 0);
3029 	num_dirrem += 1;
3030 	MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
3031 		M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO);
3032 	dirrem->dm_list.wk_type = D_DIRREM;
3033 	dirrem->dm_state = isrmdir ? RMDIR : 0;
3034 	dirrem->dm_mnt = ITOV(ip)->v_mount;
3035 	dirrem->dm_oldinum = ip->i_number;
3036 	*prevdirremp = NULL;
3037 
3038 	ACQUIRE_LOCK(&lk);
3039 	lbn = lblkno(dp->i_fs, dp->i_offset);
3040 	offset = blkoff(dp->i_fs, dp->i_offset);
3041 	if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3042 		WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3043 	dirrem->dm_pagedep = pagedep;
3044 	/*
3045 	 * Check for a diradd dependency for the same directory entry.
3046 	 * If present, then both dependencies become obsolete and can
3047 	 * be de-allocated. Check for an entry on both the pd_dirraddhd
3048 	 * list and the pd_pendinghd list.
3049 	 */
3050 
3051 	LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
3052 		if (dap->da_offset == offset)
3053 			break;
3054 	if (dap == NULL) {
3055 
3056 		LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
3057 			if (dap->da_offset == offset)
3058 				break;
3059 		if (dap == NULL)
3060 			return (dirrem);
3061 	}
3062 	/*
3063 	 * Must be ATTACHED at this point.
3064 	 */
3065 	if ((dap->da_state & ATTACHED) == 0) {
3066 		FREE_LOCK(&lk);
3067 		panic("newdirrem: not ATTACHED");
3068 	}
3069 	if (dap->da_newinum != ip->i_number) {
3070 		FREE_LOCK(&lk);
3071 		panic("newdirrem: inum %d should be %d",
3072 		    ip->i_number, dap->da_newinum);
3073 	}
3074 	/*
3075 	 * If we are deleting a changed name that never made it to disk,
3076 	 * then return the dirrem describing the previous inode (which
3077 	 * represents the inode currently referenced from this entry on disk).
3078 	 */
3079 	if ((dap->da_state & DIRCHG) != 0) {
3080 		*prevdirremp = dap->da_previous;
3081 		dap->da_state &= ~DIRCHG;
3082 		dap->da_pagedep = pagedep;
3083 	}
3084 	/*
3085 	 * We are deleting an entry that never made it to disk.
3086 	 * Mark it COMPLETE so we can delete its inode immediately.
3087 	 */
3088 	dirrem->dm_state |= COMPLETE;
3089 	free_diradd(dap);
3090 	return (dirrem);
3091 }
3092 
3093 /*
3094  * Directory entry change dependencies.
3095  *
3096  * Changing an existing directory entry requires that an add operation
3097  * be completed first followed by a deletion. The semantics for the addition
3098  * are identical to the description of adding a new entry above except
3099  * that the rollback is to the old inode number rather than zero. Once
3100  * the addition dependency is completed, the removal is done as described
3101  * in the removal routine above.
3102  */
3103 
3104 /*
3105  * This routine should be called immediately after changing
3106  * a directory entry.  The inode's link count should not be
3107  * decremented by the calling procedure -- the soft updates
3108  * code will perform this task when it is safe.
3109  */
3110 void
3111 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
3112 	struct buf *bp;		/* buffer containing directory block */
3113 	struct inode *dp;	/* inode for the directory being modified */
3114 	struct inode *ip;	/* inode for directory entry being removed */
3115 	ino_t newinum;		/* new inode number for changed entry */
3116 	int isrmdir;		/* indicates if doing RMDIR */
3117 {
3118 	int offset;
3119 	struct diradd *dap = NULL;
3120 	struct dirrem *dirrem, *prevdirrem;
3121 	struct pagedep *pagedep;
3122 	struct inodedep *inodedep;
3123 
3124 	offset = blkoff(dp->i_fs, dp->i_offset);
3125 
3126 	/*
3127 	 * Whiteouts do not need diradd dependencies.
3128 	 */
3129 	if (newinum != WINO) {
3130 		MALLOC(dap, struct diradd *, sizeof(struct diradd),
3131 		    M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
3132 		dap->da_list.wk_type = D_DIRADD;
3133 		dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
3134 		dap->da_offset = offset;
3135 		dap->da_newinum = newinum;
3136 	}
3137 
3138 	/*
3139 	 * Allocate a new dirrem and ACQUIRE_LOCK.
3140 	 */
3141 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3142 	pagedep = dirrem->dm_pagedep;
3143 	/*
3144 	 * The possible values for isrmdir:
3145 	 *	0 - non-directory file rename
3146 	 *	1 - directory rename within same directory
3147 	 *   inum - directory rename to new directory of given inode number
3148 	 * When renaming to a new directory, we are both deleting and
3149 	 * creating a new directory entry, so the link count on the new
3150 	 * directory should not change. Thus we do not need the followup
3151 	 * dirrem which is usually done in handle_workitem_remove. We set
3152 	 * the DIRCHG flag to tell handle_workitem_remove to skip the
3153 	 * followup dirrem.
3154 	 */
3155 	if (isrmdir > 1)
3156 		dirrem->dm_state |= DIRCHG;
3157 
3158 	/*
3159 	 * Whiteouts have no additional dependencies,
3160 	 * so just put the dirrem on the correct list.
3161 	 */
3162 	if (newinum == WINO) {
3163 		if ((dirrem->dm_state & COMPLETE) == 0) {
3164 			LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
3165 			    dm_next);
3166 		} else {
3167 			dirrem->dm_dirinum = pagedep->pd_ino;
3168 			add_to_worklist(&dirrem->dm_list);
3169 		}
3170 		FREE_LOCK(&lk);
3171 		return;
3172 	}
3173 
3174 	/*
3175 	 * If the COMPLETE flag is clear, then there were no active
3176 	 * entries and we want to roll back to the previous inode until
3177 	 * the new inode is committed to disk. If the COMPLETE flag is
3178 	 * set, then we have deleted an entry that never made it to disk.
3179 	 * If the entry we deleted resulted from a name change, then the old
3180 	 * inode reference still resides on disk. Any rollback that we do
3181 	 * needs to be to that old inode (returned to us in prevdirrem). If
3182 	 * the entry we deleted resulted from a create, then there is
3183 	 * no entry on the disk, so we want to roll back to zero rather
3184 	 * than the uncommitted inode. In either of the COMPLETE cases we
3185 	 * want to immediately free the unwritten and unreferenced inode.
3186 	 */
3187 	if ((dirrem->dm_state & COMPLETE) == 0) {
3188 		dap->da_previous = dirrem;
3189 	} else {
3190 		if (prevdirrem != NULL) {
3191 			dap->da_previous = prevdirrem;
3192 		} else {
3193 			dap->da_state &= ~DIRCHG;
3194 			dap->da_pagedep = pagedep;
3195 		}
3196 		dirrem->dm_dirinum = pagedep->pd_ino;
3197 		add_to_worklist(&dirrem->dm_list);
3198 	}
3199 	/*
3200 	 * Link into its inodedep. Put it on the id_bufwait list if the inode
3201 	 * is not yet written. If it is written, do the post-inode write
3202 	 * processing to put it on the id_pendinghd list.
3203 	 */
3204 	if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 ||
3205 	    (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3206 		dap->da_state |= COMPLETE;
3207 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3208 		WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3209 	} else {
3210 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
3211 		    dap, da_pdlist);
3212 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3213 	}
3214 	FREE_LOCK(&lk);
3215 }
3216 
3217 /*
3218  * Called whenever the link count on an inode is changed.
3219  * It creates an inode dependency so that the new reference(s)
3220  * to the inode cannot be committed to disk until the updated
3221  * inode has been written.
3222  */
3223 void
3224 softdep_change_linkcnt(ip)
3225 	struct inode *ip;	/* the inode with the increased link count */
3226 {
3227 	struct inodedep *inodedep;
3228 
3229 	ACQUIRE_LOCK(&lk);
3230 	(void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep);
3231 	if (ip->i_nlink < ip->i_effnlink) {
3232 		FREE_LOCK(&lk);
3233 		panic("softdep_change_linkcnt: bad delta");
3234 	}
3235 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3236 	FREE_LOCK(&lk);
3237 }
3238 
3239 /*
3240  * Called when the effective link count and the reference count
3241  * on an inode drops to zero. At this point there are no names
3242  * referencing the file in the filesystem and no active file
3243  * references. The space associated with the file will be freed
3244  * as soon as the necessary soft dependencies are cleared.
3245  */
3246 void
3247 softdep_releasefile(ip)
3248 	struct inode *ip;	/* inode with the zero effective link count */
3249 {
3250 	struct inodedep *inodedep;
3251 	struct fs *fs;
3252 	int extblocks;
3253 
3254 	if (ip->i_effnlink > 0)
3255 		panic("softdep_filerelease: file still referenced");
3256 	/*
3257 	 * We may be called several times as the real reference count
3258 	 * drops to zero. We only want to account for the space once.
3259 	 */
3260 	if (ip->i_flag & IN_SPACECOUNTED)
3261 		return;
3262 	/*
3263 	 * We have to deactivate a snapshot otherwise copyonwrites may
3264 	 * add blocks and the cleanup may remove blocks after we have
3265 	 * tried to account for them.
3266 	 */
3267 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
3268 		ffs_snapremove(ITOV(ip));
3269 	/*
3270 	 * If we are tracking an nlinkdelta, we have to also remember
3271 	 * whether we accounted for the freed space yet.
3272 	 */
3273 	ACQUIRE_LOCK(&lk);
3274 	if ((inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep)))
3275 		inodedep->id_state |= SPACECOUNTED;
3276 	FREE_LOCK(&lk);
3277 	fs = ip->i_fs;
3278 	extblocks = 0;
3279 	if (fs->fs_magic == FS_UFS2_MAGIC)
3280 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
3281 	ip->i_fs->fs_pendingblocks += DIP(ip, i_blocks) - extblocks;
3282 	ip->i_fs->fs_pendinginodes += 1;
3283 	ip->i_flag |= IN_SPACECOUNTED;
3284 }
3285 
3286 /*
3287  * This workitem decrements the inode's link count.
3288  * If the link count reaches zero, the file is removed.
3289  */
3290 static void
3291 handle_workitem_remove(dirrem, xp)
3292 	struct dirrem *dirrem;
3293 	struct vnode *xp;
3294 {
3295 	struct thread *td = curthread;
3296 	struct inodedep *inodedep;
3297 	struct vnode *vp;
3298 	struct inode *ip;
3299 	ino_t oldinum;
3300 	int error;
3301 
3302 	if ((vp = xp) == NULL &&
3303 	    (error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, LK_EXCLUSIVE,
3304 	     &vp)) != 0) {
3305 		softdep_error("handle_workitem_remove: vget", error);
3306 		return;
3307 	}
3308 	ip = VTOI(vp);
3309 	ACQUIRE_LOCK(&lk);
3310 	if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){
3311 		FREE_LOCK(&lk);
3312 		panic("handle_workitem_remove: lost inodedep");
3313 	}
3314 	/*
3315 	 * Normal file deletion.
3316 	 */
3317 	if ((dirrem->dm_state & RMDIR) == 0) {
3318 		ip->i_nlink--;
3319 		DIP_SET(ip, i_nlink, ip->i_nlink);
3320 		ip->i_flag |= IN_CHANGE;
3321 		if (ip->i_nlink < ip->i_effnlink) {
3322 			FREE_LOCK(&lk);
3323 			panic("handle_workitem_remove: bad file delta");
3324 		}
3325 		inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3326 		FREE_LOCK(&lk);
3327 		vput(vp);
3328 		num_dirrem -= 1;
3329 		WORKITEM_FREE(dirrem, D_DIRREM);
3330 		return;
3331 	}
3332 	/*
3333 	 * Directory deletion. Decrement reference count for both the
3334 	 * just deleted parent directory entry and the reference for ".".
3335 	 * Next truncate the directory to length zero. When the
3336 	 * truncation completes, arrange to have the reference count on
3337 	 * the parent decremented to account for the loss of "..".
3338 	 */
3339 	ip->i_nlink -= 2;
3340 	DIP_SET(ip, i_nlink, ip->i_nlink);
3341 	ip->i_flag |= IN_CHANGE;
3342 	if (ip->i_nlink < ip->i_effnlink) {
3343 		FREE_LOCK(&lk);
3344 		panic("handle_workitem_remove: bad dir delta");
3345 	}
3346 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3347 	FREE_LOCK(&lk);
3348 	if ((error = UFS_TRUNCATE(vp, (off_t)0, 0, td->td_ucred, td)) != 0)
3349 		softdep_error("handle_workitem_remove: truncate", error);
3350 	/*
3351 	 * Rename a directory to a new parent. Since, we are both deleting
3352 	 * and creating a new directory entry, the link count on the new
3353 	 * directory should not change. Thus we skip the followup dirrem.
3354 	 */
3355 	if (dirrem->dm_state & DIRCHG) {
3356 		vput(vp);
3357 		num_dirrem -= 1;
3358 		WORKITEM_FREE(dirrem, D_DIRREM);
3359 		return;
3360 	}
3361 	/*
3362 	 * If the inodedep does not exist, then the zero'ed inode has
3363 	 * been written to disk. If the allocated inode has never been
3364 	 * written to disk, then the on-disk inode is zero'ed. In either
3365 	 * case we can remove the file immediately.
3366 	 */
3367 	ACQUIRE_LOCK(&lk);
3368 	dirrem->dm_state = 0;
3369 	oldinum = dirrem->dm_oldinum;
3370 	dirrem->dm_oldinum = dirrem->dm_dirinum;
3371 	if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 ||
3372 	    check_inode_unwritten(inodedep)) {
3373 		FREE_LOCK(&lk);
3374 		vput(vp);
3375 		handle_workitem_remove(dirrem, NULL);
3376 		return;
3377 	}
3378 	WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
3379 	FREE_LOCK(&lk);
3380 	vput(vp);
3381 }
3382 
3383 /*
3384  * Inode de-allocation dependencies.
3385  *
3386  * When an inode's link count is reduced to zero, it can be de-allocated. We
3387  * found it convenient to postpone de-allocation until after the inode is
3388  * written to disk with its new link count (zero).  At this point, all of the
3389  * on-disk inode's block pointers are nullified and, with careful dependency
3390  * list ordering, all dependencies related to the inode will be satisfied and
3391  * the corresponding dependency structures de-allocated.  So, if/when the
3392  * inode is reused, there will be no mixing of old dependencies with new
3393  * ones.  This artificial dependency is set up by the block de-allocation
3394  * procedure above (softdep_setup_freeblocks) and completed by the
3395  * following procedure.
3396  */
3397 static void
3398 handle_workitem_freefile(freefile)
3399 	struct freefile *freefile;
3400 {
3401 	struct fs *fs;
3402 	struct inodedep *idp;
3403 	int error;
3404 
3405 	fs = VFSTOUFS(freefile->fx_mnt)->um_fs;
3406 #ifdef DEBUG
3407 	ACQUIRE_LOCK(&lk);
3408 	error = inodedep_lookup(fs, freefile->fx_oldinum, 0, &idp);
3409 	FREE_LOCK(&lk);
3410 	if (error)
3411 		panic("handle_workitem_freefile: inodedep survived");
3412 #endif
3413 	fs->fs_pendinginodes -= 1;
3414 	if ((error = ffs_freefile(fs, freefile->fx_devvp, freefile->fx_oldinum,
3415 	     freefile->fx_mode)) != 0)
3416 		softdep_error("handle_workitem_freefile", error);
3417 	WORKITEM_FREE(freefile, D_FREEFILE);
3418 }
3419 
3420 static int
3421 softdep_disk_prewrite(struct vnode *vp, struct buf *bp)
3422 {
3423 	int error;
3424 
3425 	if (bp->b_iocmd != BIO_WRITE)
3426 		return (0);
3427 	if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
3428 	    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
3429 	    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
3430 		panic("softdep_disk_prewrite: bad I/O");
3431 	bp->b_flags &= ~B_VALIDSUSPWRT;
3432 	if (LIST_FIRST(&bp->b_dep) != NULL)
3433 		buf_start(bp);
3434 	mp_fixme("This should require the vnode lock.");
3435 	if ((vp->v_vflag & VV_COPYONWRITE) &&
3436 	    vp->v_rdev->si_copyonwrite &&
3437 	    (error = (*vp->v_rdev->si_copyonwrite)(vp, bp)) != 0 &&
3438 	    error != EOPNOTSUPP) {
3439 		bp->b_error = error;
3440 		bp->b_ioflags |= BIO_ERROR;
3441 		bufdone(bp);
3442 		return (1);
3443 	}
3444 	return (0);
3445 }
3446 
3447 
3448 /*
3449  * Disk writes.
3450  *
3451  * The dependency structures constructed above are most actively used when file
3452  * system blocks are written to disk.  No constraints are placed on when a
3453  * block can be written, but unsatisfied update dependencies are made safe by
3454  * modifying (or replacing) the source memory for the duration of the disk
3455  * write.  When the disk write completes, the memory block is again brought
3456  * up-to-date.
3457  *
3458  * In-core inode structure reclamation.
3459  *
3460  * Because there are a finite number of "in-core" inode structures, they are
3461  * reused regularly.  By transferring all inode-related dependencies to the
3462  * in-memory inode block and indexing them separately (via "inodedep"s), we
3463  * can allow "in-core" inode structures to be reused at any time and avoid
3464  * any increase in contention.
3465  *
3466  * Called just before entering the device driver to initiate a new disk I/O.
3467  * The buffer must be locked, thus, no I/O completion operations can occur
3468  * while we are manipulating its associated dependencies.
3469  */
3470 static void
3471 softdep_disk_io_initiation(bp)
3472 	struct buf *bp;		/* structure describing disk write to occur */
3473 {
3474 	struct worklist *wk, *nextwk;
3475 	struct indirdep *indirdep;
3476 	struct inodedep *inodedep;
3477 
3478 	/*
3479 	 * We only care about write operations. There should never
3480 	 * be dependencies for reads.
3481 	 */
3482 	if (bp->b_iocmd == BIO_READ)
3483 		panic("softdep_disk_io_initiation: read");
3484 	/*
3485 	 * Do any necessary pre-I/O processing.
3486 	 */
3487 	for (wk = LIST_FIRST(&bp->b_dep); wk; wk = nextwk) {
3488 		nextwk = LIST_NEXT(wk, wk_list);
3489 		switch (wk->wk_type) {
3490 
3491 		case D_PAGEDEP:
3492 			initiate_write_filepage(WK_PAGEDEP(wk), bp);
3493 			continue;
3494 
3495 		case D_INODEDEP:
3496 			inodedep = WK_INODEDEP(wk);
3497 			if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
3498 				initiate_write_inodeblock_ufs1(inodedep, bp);
3499 			else
3500 				initiate_write_inodeblock_ufs2(inodedep, bp);
3501 			continue;
3502 
3503 		case D_INDIRDEP:
3504 			indirdep = WK_INDIRDEP(wk);
3505 			if (indirdep->ir_state & GOINGAWAY)
3506 				panic("disk_io_initiation: indirdep gone");
3507 			/*
3508 			 * If there are no remaining dependencies, this
3509 			 * will be writing the real pointers, so the
3510 			 * dependency can be freed.
3511 			 */
3512 			if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) {
3513 				indirdep->ir_savebp->b_flags |=
3514 				    B_INVAL | B_NOCACHE;
3515 				brelse(indirdep->ir_savebp);
3516 				/* inline expand WORKLIST_REMOVE(wk); */
3517 				wk->wk_state &= ~ONWORKLIST;
3518 				LIST_REMOVE(wk, wk_list);
3519 				WORKITEM_FREE(indirdep, D_INDIRDEP);
3520 				continue;
3521 			}
3522 			/*
3523 			 * Replace up-to-date version with safe version.
3524 			 */
3525 			MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
3526 			    M_INDIRDEP, M_SOFTDEP_FLAGS);
3527 			ACQUIRE_LOCK(&lk);
3528 			indirdep->ir_state &= ~ATTACHED;
3529 			indirdep->ir_state |= UNDONE;
3530 			bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3531 			bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3532 			    bp->b_bcount);
3533 			FREE_LOCK(&lk);
3534 			continue;
3535 
3536 		case D_MKDIR:
3537 		case D_BMSAFEMAP:
3538 		case D_ALLOCDIRECT:
3539 		case D_ALLOCINDIR:
3540 			continue;
3541 
3542 		default:
3543 			panic("handle_disk_io_initiation: Unexpected type %s",
3544 			    TYPENAME(wk->wk_type));
3545 			/* NOTREACHED */
3546 		}
3547 	}
3548 }
3549 
3550 /*
3551  * Called from within the procedure above to deal with unsatisfied
3552  * allocation dependencies in a directory. The buffer must be locked,
3553  * thus, no I/O completion operations can occur while we are
3554  * manipulating its associated dependencies.
3555  */
3556 static void
3557 initiate_write_filepage(pagedep, bp)
3558 	struct pagedep *pagedep;
3559 	struct buf *bp;
3560 {
3561 	struct diradd *dap;
3562 	struct direct *ep;
3563 	int i;
3564 
3565 	if (pagedep->pd_state & IOSTARTED) {
3566 		/*
3567 		 * This can only happen if there is a driver that does not
3568 		 * understand chaining. Here biodone will reissue the call
3569 		 * to strategy for the incomplete buffers.
3570 		 */
3571 		printf("initiate_write_filepage: already started\n");
3572 		return;
3573 	}
3574 	pagedep->pd_state |= IOSTARTED;
3575 	ACQUIRE_LOCK(&lk);
3576 	for (i = 0; i < DAHASHSZ; i++) {
3577 		LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3578 			ep = (struct direct *)
3579 			    ((char *)bp->b_data + dap->da_offset);
3580 			if (ep->d_ino != dap->da_newinum) {
3581 				FREE_LOCK(&lk);
3582 				panic("%s: dir inum %d != new %d",
3583 				    "initiate_write_filepage",
3584 				    ep->d_ino, dap->da_newinum);
3585 			}
3586 			if (dap->da_state & DIRCHG)
3587 				ep->d_ino = dap->da_previous->dm_oldinum;
3588 			else
3589 				ep->d_ino = 0;
3590 			dap->da_state &= ~ATTACHED;
3591 			dap->da_state |= UNDONE;
3592 		}
3593 	}
3594 	FREE_LOCK(&lk);
3595 }
3596 
3597 /*
3598  * Version of initiate_write_inodeblock that handles UFS1 dinodes.
3599  * Note that any bug fixes made to this routine must be done in the
3600  * version found below.
3601  *
3602  * Called from within the procedure above to deal with unsatisfied
3603  * allocation dependencies in an inodeblock. The buffer must be
3604  * locked, thus, no I/O completion operations can occur while we
3605  * are manipulating its associated dependencies.
3606  */
3607 static void
3608 initiate_write_inodeblock_ufs1(inodedep, bp)
3609 	struct inodedep *inodedep;
3610 	struct buf *bp;			/* The inode block */
3611 {
3612 	struct allocdirect *adp, *lastadp;
3613 	struct ufs1_dinode *dp;
3614 	struct fs *fs;
3615 	ufs_lbn_t i, prevlbn = 0;
3616 	int deplist;
3617 
3618 	if (inodedep->id_state & IOSTARTED)
3619 		panic("initiate_write_inodeblock_ufs1: already started");
3620 	inodedep->id_state |= IOSTARTED;
3621 	fs = inodedep->id_fs;
3622 	dp = (struct ufs1_dinode *)bp->b_data +
3623 	    ino_to_fsbo(fs, inodedep->id_ino);
3624 	/*
3625 	 * If the bitmap is not yet written, then the allocated
3626 	 * inode cannot be written to disk.
3627 	 */
3628 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3629 		if (inodedep->id_savedino1 != NULL)
3630 			panic("initiate_write_inodeblock_ufs1: I/O underway");
3631 		MALLOC(inodedep->id_savedino1, struct ufs1_dinode *,
3632 		    sizeof(struct ufs1_dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3633 		*inodedep->id_savedino1 = *dp;
3634 		bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
3635 		return;
3636 	}
3637 	/*
3638 	 * If no dependencies, then there is nothing to roll back.
3639 	 */
3640 	inodedep->id_savedsize = dp->di_size;
3641 	inodedep->id_savedextsize = 0;
3642 	if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL)
3643 		return;
3644 	/*
3645 	 * Set the dependencies to busy.
3646 	 */
3647 	ACQUIRE_LOCK(&lk);
3648 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3649 	     adp = TAILQ_NEXT(adp, ad_next)) {
3650 #ifdef DIAGNOSTIC
3651 		if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3652 			FREE_LOCK(&lk);
3653 			panic("softdep_write_inodeblock: lbn order");
3654 		}
3655 		prevlbn = adp->ad_lbn;
3656 		if (adp->ad_lbn < NDADDR &&
3657 		    dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3658 			FREE_LOCK(&lk);
3659 			panic("%s: direct pointer #%jd mismatch %d != %jd",
3660 			    "softdep_write_inodeblock",
3661 			    (intmax_t)adp->ad_lbn,
3662 			    dp->di_db[adp->ad_lbn],
3663 			    (intmax_t)adp->ad_newblkno);
3664 		}
3665 		if (adp->ad_lbn >= NDADDR &&
3666 		    dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3667 			FREE_LOCK(&lk);
3668 			panic("%s: indirect pointer #%jd mismatch %d != %jd",
3669 			    "softdep_write_inodeblock",
3670 			    (intmax_t)adp->ad_lbn - NDADDR,
3671 			    dp->di_ib[adp->ad_lbn - NDADDR],
3672 			    (intmax_t)adp->ad_newblkno);
3673 		}
3674 		deplist |= 1 << adp->ad_lbn;
3675 		if ((adp->ad_state & ATTACHED) == 0) {
3676 			FREE_LOCK(&lk);
3677 			panic("softdep_write_inodeblock: Unknown state 0x%x",
3678 			    adp->ad_state);
3679 		}
3680 #endif /* DIAGNOSTIC */
3681 		adp->ad_state &= ~ATTACHED;
3682 		adp->ad_state |= UNDONE;
3683 	}
3684 	/*
3685 	 * The on-disk inode cannot claim to be any larger than the last
3686 	 * fragment that has been written. Otherwise, the on-disk inode
3687 	 * might have fragments that were not the last block in the file
3688 	 * which would corrupt the filesystem.
3689 	 */
3690 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3691 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3692 		if (adp->ad_lbn >= NDADDR)
3693 			break;
3694 		dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3695 		/* keep going until hitting a rollback to a frag */
3696 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3697 			continue;
3698 		dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3699 		for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3700 #ifdef DIAGNOSTIC
3701 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3702 				FREE_LOCK(&lk);
3703 				panic("softdep_write_inodeblock: lost dep1");
3704 			}
3705 #endif /* DIAGNOSTIC */
3706 			dp->di_db[i] = 0;
3707 		}
3708 		for (i = 0; i < NIADDR; i++) {
3709 #ifdef DIAGNOSTIC
3710 			if (dp->di_ib[i] != 0 &&
3711 			    (deplist & ((1 << NDADDR) << i)) == 0) {
3712 				FREE_LOCK(&lk);
3713 				panic("softdep_write_inodeblock: lost dep2");
3714 			}
3715 #endif /* DIAGNOSTIC */
3716 			dp->di_ib[i] = 0;
3717 		}
3718 		FREE_LOCK(&lk);
3719 		return;
3720 	}
3721 	/*
3722 	 * If we have zero'ed out the last allocated block of the file,
3723 	 * roll back the size to the last currently allocated block.
3724 	 * We know that this last allocated block is a full-sized as
3725 	 * we already checked for fragments in the loop above.
3726 	 */
3727 	if (lastadp != NULL &&
3728 	    dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3729 		for (i = lastadp->ad_lbn; i >= 0; i--)
3730 			if (dp->di_db[i] != 0)
3731 				break;
3732 		dp->di_size = (i + 1) * fs->fs_bsize;
3733 	}
3734 	/*
3735 	 * The only dependencies are for indirect blocks.
3736 	 *
3737 	 * The file size for indirect block additions is not guaranteed.
3738 	 * Such a guarantee would be non-trivial to achieve. The conventional
3739 	 * synchronous write implementation also does not make this guarantee.
3740 	 * Fsck should catch and fix discrepancies. Arguably, the file size
3741 	 * can be over-estimated without destroying integrity when the file
3742 	 * moves into the indirect blocks (i.e., is large). If we want to
3743 	 * postpone fsck, we are stuck with this argument.
3744 	 */
3745 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3746 		dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3747 	FREE_LOCK(&lk);
3748 }
3749 
3750 /*
3751  * Version of initiate_write_inodeblock that handles UFS2 dinodes.
3752  * Note that any bug fixes made to this routine must be done in the
3753  * version found above.
3754  *
3755  * Called from within the procedure above to deal with unsatisfied
3756  * allocation dependencies in an inodeblock. The buffer must be
3757  * locked, thus, no I/O completion operations can occur while we
3758  * are manipulating its associated dependencies.
3759  */
3760 static void
3761 initiate_write_inodeblock_ufs2(inodedep, bp)
3762 	struct inodedep *inodedep;
3763 	struct buf *bp;			/* The inode block */
3764 {
3765 	struct allocdirect *adp, *lastadp;
3766 	struct ufs2_dinode *dp;
3767 	struct fs *fs;
3768 	ufs_lbn_t i, prevlbn = 0;
3769 	int deplist;
3770 
3771 	if (inodedep->id_state & IOSTARTED)
3772 		panic("initiate_write_inodeblock_ufs2: already started");
3773 	inodedep->id_state |= IOSTARTED;
3774 	fs = inodedep->id_fs;
3775 	dp = (struct ufs2_dinode *)bp->b_data +
3776 	    ino_to_fsbo(fs, inodedep->id_ino);
3777 	/*
3778 	 * If the bitmap is not yet written, then the allocated
3779 	 * inode cannot be written to disk.
3780 	 */
3781 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3782 		if (inodedep->id_savedino2 != NULL)
3783 			panic("initiate_write_inodeblock_ufs2: I/O underway");
3784 		MALLOC(inodedep->id_savedino2, struct ufs2_dinode *,
3785 		    sizeof(struct ufs2_dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3786 		*inodedep->id_savedino2 = *dp;
3787 		bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
3788 		return;
3789 	}
3790 	/*
3791 	 * If no dependencies, then there is nothing to roll back.
3792 	 */
3793 	inodedep->id_savedsize = dp->di_size;
3794 	inodedep->id_savedextsize = dp->di_extsize;
3795 	if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL &&
3796 	    TAILQ_FIRST(&inodedep->id_extupdt) == NULL)
3797 		return;
3798 	/*
3799 	 * Set the ext data dependencies to busy.
3800 	 */
3801 	ACQUIRE_LOCK(&lk);
3802 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
3803 	     adp = TAILQ_NEXT(adp, ad_next)) {
3804 #ifdef DIAGNOSTIC
3805 		if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3806 			FREE_LOCK(&lk);
3807 			panic("softdep_write_inodeblock: lbn order");
3808 		}
3809 		prevlbn = adp->ad_lbn;
3810 		if (dp->di_extb[adp->ad_lbn] != adp->ad_newblkno) {
3811 			FREE_LOCK(&lk);
3812 			panic("%s: direct pointer #%jd mismatch %jd != %jd",
3813 			    "softdep_write_inodeblock",
3814 			    (intmax_t)adp->ad_lbn,
3815 			    (intmax_t)dp->di_extb[adp->ad_lbn],
3816 			    (intmax_t)adp->ad_newblkno);
3817 		}
3818 		deplist |= 1 << adp->ad_lbn;
3819 		if ((adp->ad_state & ATTACHED) == 0) {
3820 			FREE_LOCK(&lk);
3821 			panic("softdep_write_inodeblock: Unknown state 0x%x",
3822 			    adp->ad_state);
3823 		}
3824 #endif /* DIAGNOSTIC */
3825 		adp->ad_state &= ~ATTACHED;
3826 		adp->ad_state |= UNDONE;
3827 	}
3828 	/*
3829 	 * The on-disk inode cannot claim to be any larger than the last
3830 	 * fragment that has been written. Otherwise, the on-disk inode
3831 	 * might have fragments that were not the last block in the ext
3832 	 * data which would corrupt the filesystem.
3833 	 */
3834 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
3835 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3836 		dp->di_extb[adp->ad_lbn] = adp->ad_oldblkno;
3837 		/* keep going until hitting a rollback to a frag */
3838 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3839 			continue;
3840 		dp->di_extsize = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3841 		for (i = adp->ad_lbn + 1; i < NXADDR; i++) {
3842 #ifdef DIAGNOSTIC
3843 			if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) {
3844 				FREE_LOCK(&lk);
3845 				panic("softdep_write_inodeblock: lost dep1");
3846 			}
3847 #endif /* DIAGNOSTIC */
3848 			dp->di_extb[i] = 0;
3849 		}
3850 		lastadp = NULL;
3851 		break;
3852 	}
3853 	/*
3854 	 * If we have zero'ed out the last allocated block of the ext
3855 	 * data, roll back the size to the last currently allocated block.
3856 	 * We know that this last allocated block is a full-sized as
3857 	 * we already checked for fragments in the loop above.
3858 	 */
3859 	if (lastadp != NULL &&
3860 	    dp->di_extsize <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3861 		for (i = lastadp->ad_lbn; i >= 0; i--)
3862 			if (dp->di_extb[i] != 0)
3863 				break;
3864 		dp->di_extsize = (i + 1) * fs->fs_bsize;
3865 	}
3866 	/*
3867 	 * Set the file data dependencies to busy.
3868 	 */
3869 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3870 	     adp = TAILQ_NEXT(adp, ad_next)) {
3871 #ifdef DIAGNOSTIC
3872 		if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3873 			FREE_LOCK(&lk);
3874 			panic("softdep_write_inodeblock: lbn order");
3875 		}
3876 		prevlbn = adp->ad_lbn;
3877 		if (adp->ad_lbn < NDADDR &&
3878 		    dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3879 			FREE_LOCK(&lk);
3880 			panic("%s: direct pointer #%jd mismatch %jd != %jd",
3881 			    "softdep_write_inodeblock",
3882 			    (intmax_t)adp->ad_lbn,
3883 			    (intmax_t)dp->di_db[adp->ad_lbn],
3884 			    (intmax_t)adp->ad_newblkno);
3885 		}
3886 		if (adp->ad_lbn >= NDADDR &&
3887 		    dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3888 			FREE_LOCK(&lk);
3889 			panic("%s indirect pointer #%jd mismatch %jd != %jd",
3890 			    "softdep_write_inodeblock:",
3891 			    (intmax_t)adp->ad_lbn - NDADDR,
3892 			    (intmax_t)dp->di_ib[adp->ad_lbn - NDADDR],
3893 			    (intmax_t)adp->ad_newblkno);
3894 		}
3895 		deplist |= 1 << adp->ad_lbn;
3896 		if ((adp->ad_state & ATTACHED) == 0) {
3897 			FREE_LOCK(&lk);
3898 			panic("softdep_write_inodeblock: Unknown state 0x%x",
3899 			    adp->ad_state);
3900 		}
3901 #endif /* DIAGNOSTIC */
3902 		adp->ad_state &= ~ATTACHED;
3903 		adp->ad_state |= UNDONE;
3904 	}
3905 	/*
3906 	 * The on-disk inode cannot claim to be any larger than the last
3907 	 * fragment that has been written. Otherwise, the on-disk inode
3908 	 * might have fragments that were not the last block in the file
3909 	 * which would corrupt the filesystem.
3910 	 */
3911 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3912 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3913 		if (adp->ad_lbn >= NDADDR)
3914 			break;
3915 		dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3916 		/* keep going until hitting a rollback to a frag */
3917 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3918 			continue;
3919 		dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3920 		for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3921 #ifdef DIAGNOSTIC
3922 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3923 				FREE_LOCK(&lk);
3924 				panic("softdep_write_inodeblock: lost dep2");
3925 			}
3926 #endif /* DIAGNOSTIC */
3927 			dp->di_db[i] = 0;
3928 		}
3929 		for (i = 0; i < NIADDR; i++) {
3930 #ifdef DIAGNOSTIC
3931 			if (dp->di_ib[i] != 0 &&
3932 			    (deplist & ((1 << NDADDR) << i)) == 0) {
3933 				FREE_LOCK(&lk);
3934 				panic("softdep_write_inodeblock: lost dep3");
3935 			}
3936 #endif /* DIAGNOSTIC */
3937 			dp->di_ib[i] = 0;
3938 		}
3939 		FREE_LOCK(&lk);
3940 		return;
3941 	}
3942 	/*
3943 	 * If we have zero'ed out the last allocated block of the file,
3944 	 * roll back the size to the last currently allocated block.
3945 	 * We know that this last allocated block is a full-sized as
3946 	 * we already checked for fragments in the loop above.
3947 	 */
3948 	if (lastadp != NULL &&
3949 	    dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3950 		for (i = lastadp->ad_lbn; i >= 0; i--)
3951 			if (dp->di_db[i] != 0)
3952 				break;
3953 		dp->di_size = (i + 1) * fs->fs_bsize;
3954 	}
3955 	/*
3956 	 * The only dependencies are for indirect blocks.
3957 	 *
3958 	 * The file size for indirect block additions is not guaranteed.
3959 	 * Such a guarantee would be non-trivial to achieve. The conventional
3960 	 * synchronous write implementation also does not make this guarantee.
3961 	 * Fsck should catch and fix discrepancies. Arguably, the file size
3962 	 * can be over-estimated without destroying integrity when the file
3963 	 * moves into the indirect blocks (i.e., is large). If we want to
3964 	 * postpone fsck, we are stuck with this argument.
3965 	 */
3966 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3967 		dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3968 	FREE_LOCK(&lk);
3969 }
3970 
3971 /*
3972  * This routine is called during the completion interrupt
3973  * service routine for a disk write (from the procedure called
3974  * by the device driver to inform the filesystem caches of
3975  * a request completion).  It should be called early in this
3976  * procedure, before the block is made available to other
3977  * processes or other routines are called.
3978  */
3979 static void
3980 softdep_disk_write_complete(bp)
3981 	struct buf *bp;		/* describes the completed disk write */
3982 {
3983 	struct worklist *wk;
3984 	struct workhead reattach;
3985 	struct newblk *newblk;
3986 	struct allocindir *aip;
3987 	struct allocdirect *adp;
3988 	struct indirdep *indirdep;
3989 	struct inodedep *inodedep;
3990 	struct bmsafemap *bmsafemap;
3991 
3992 	/*
3993 	 * If an error occurred while doing the write, then the data
3994 	 * has not hit the disk and the dependencies cannot be unrolled.
3995 	 */
3996 	if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0)
3997 		return;
3998 #ifdef DEBUG
3999 	if (lk.lkt_held != NOHOLDER)
4000 		panic("softdep_disk_write_complete: lock is held");
4001 	lk.lkt_held = SPECIAL_FLAG;
4002 #endif
4003 	LIST_INIT(&reattach);
4004 	while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
4005 		WORKLIST_REMOVE(wk);
4006 		switch (wk->wk_type) {
4007 
4008 		case D_PAGEDEP:
4009 			if (handle_written_filepage(WK_PAGEDEP(wk), bp))
4010 				WORKLIST_INSERT(&reattach, wk);
4011 			continue;
4012 
4013 		case D_INODEDEP:
4014 			if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
4015 				WORKLIST_INSERT(&reattach, wk);
4016 			continue;
4017 
4018 		case D_BMSAFEMAP:
4019 			bmsafemap = WK_BMSAFEMAP(wk);
4020 			while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
4021 				newblk->nb_state |= DEPCOMPLETE;
4022 				newblk->nb_bmsafemap = NULL;
4023 				LIST_REMOVE(newblk, nb_deps);
4024 			}
4025 			while ((adp =
4026 			   LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
4027 				adp->ad_state |= DEPCOMPLETE;
4028 				adp->ad_buf = NULL;
4029 				LIST_REMOVE(adp, ad_deps);
4030 				handle_allocdirect_partdone(adp);
4031 			}
4032 			while ((aip =
4033 			    LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
4034 				aip->ai_state |= DEPCOMPLETE;
4035 				aip->ai_buf = NULL;
4036 				LIST_REMOVE(aip, ai_deps);
4037 				handle_allocindir_partdone(aip);
4038 			}
4039 			while ((inodedep =
4040 			     LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
4041 				inodedep->id_state |= DEPCOMPLETE;
4042 				LIST_REMOVE(inodedep, id_deps);
4043 				inodedep->id_buf = NULL;
4044 			}
4045 			WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
4046 			continue;
4047 
4048 		case D_MKDIR:
4049 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
4050 			continue;
4051 
4052 		case D_ALLOCDIRECT:
4053 			adp = WK_ALLOCDIRECT(wk);
4054 			adp->ad_state |= COMPLETE;
4055 			handle_allocdirect_partdone(adp);
4056 			continue;
4057 
4058 		case D_ALLOCINDIR:
4059 			aip = WK_ALLOCINDIR(wk);
4060 			aip->ai_state |= COMPLETE;
4061 			handle_allocindir_partdone(aip);
4062 			continue;
4063 
4064 		case D_INDIRDEP:
4065 			indirdep = WK_INDIRDEP(wk);
4066 			if (indirdep->ir_state & GOINGAWAY) {
4067 				lk.lkt_held = NOHOLDER;
4068 				panic("disk_write_complete: indirdep gone");
4069 			}
4070 			bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
4071 			FREE(indirdep->ir_saveddata, M_INDIRDEP);
4072 			indirdep->ir_saveddata = 0;
4073 			indirdep->ir_state &= ~UNDONE;
4074 			indirdep->ir_state |= ATTACHED;
4075 			while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
4076 				handle_allocindir_partdone(aip);
4077 				if (aip == LIST_FIRST(&indirdep->ir_donehd)) {
4078 					lk.lkt_held = NOHOLDER;
4079 					panic("disk_write_complete: not gone");
4080 				}
4081 			}
4082 			WORKLIST_INSERT(&reattach, wk);
4083 			if ((bp->b_flags & B_DELWRI) == 0)
4084 				stat_indir_blk_ptrs++;
4085 			bdirty(bp);
4086 			continue;
4087 
4088 		default:
4089 			lk.lkt_held = NOHOLDER;
4090 			panic("handle_disk_write_complete: Unknown type %s",
4091 			    TYPENAME(wk->wk_type));
4092 			/* NOTREACHED */
4093 		}
4094 	}
4095 	/*
4096 	 * Reattach any requests that must be redone.
4097 	 */
4098 	while ((wk = LIST_FIRST(&reattach)) != NULL) {
4099 		WORKLIST_REMOVE(wk);
4100 		WORKLIST_INSERT(&bp->b_dep, wk);
4101 	}
4102 #ifdef DEBUG
4103 	if (lk.lkt_held != SPECIAL_FLAG)
4104 		panic("softdep_disk_write_complete: lock lost");
4105 	lk.lkt_held = NOHOLDER;
4106 #endif
4107 }
4108 
4109 /*
4110  * Called from within softdep_disk_write_complete above. Note that
4111  * this routine is always called from interrupt level with further
4112  * splbio interrupts blocked.
4113  */
4114 static void
4115 handle_allocdirect_partdone(adp)
4116 	struct allocdirect *adp;	/* the completed allocdirect */
4117 {
4118 	struct allocdirectlst *listhead;
4119 	struct allocdirect *listadp;
4120 	struct inodedep *inodedep;
4121 	long bsize, delay;
4122 
4123 	if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4124 		return;
4125 	if (adp->ad_buf != NULL) {
4126 		lk.lkt_held = NOHOLDER;
4127 		panic("handle_allocdirect_partdone: dangling dep");
4128 	}
4129 	/*
4130 	 * The on-disk inode cannot claim to be any larger than the last
4131 	 * fragment that has been written. Otherwise, the on-disk inode
4132 	 * might have fragments that were not the last block in the file
4133 	 * which would corrupt the filesystem. Thus, we cannot free any
4134 	 * allocdirects after one whose ad_oldblkno claims a fragment as
4135 	 * these blocks must be rolled back to zero before writing the inode.
4136 	 * We check the currently active set of allocdirects in id_inoupdt
4137 	 * or id_extupdt as appropriate.
4138 	 */
4139 	inodedep = adp->ad_inodedep;
4140 	bsize = inodedep->id_fs->fs_bsize;
4141 	if (adp->ad_state & EXTDATA)
4142 		listhead = &inodedep->id_extupdt;
4143 	else
4144 		listhead = &inodedep->id_inoupdt;
4145 	TAILQ_FOREACH(listadp, listhead, ad_next) {
4146 		/* found our block */
4147 		if (listadp == adp)
4148 			break;
4149 		/* continue if ad_oldlbn is not a fragment */
4150 		if (listadp->ad_oldsize == 0 ||
4151 		    listadp->ad_oldsize == bsize)
4152 			continue;
4153 		/* hit a fragment */
4154 		return;
4155 	}
4156 	/*
4157 	 * If we have reached the end of the current list without
4158 	 * finding the just finished dependency, then it must be
4159 	 * on the future dependency list. Future dependencies cannot
4160 	 * be freed until they are moved to the current list.
4161 	 */
4162 	if (listadp == NULL) {
4163 #ifdef DEBUG
4164 		if (adp->ad_state & EXTDATA)
4165 			listhead = &inodedep->id_newextupdt;
4166 		else
4167 			listhead = &inodedep->id_newinoupdt;
4168 		TAILQ_FOREACH(listadp, listhead, ad_next)
4169 			/* found our block */
4170 			if (listadp == adp)
4171 				break;
4172 		if (listadp == NULL) {
4173 			lk.lkt_held = NOHOLDER;
4174 			panic("handle_allocdirect_partdone: lost dep");
4175 		}
4176 #endif /* DEBUG */
4177 		return;
4178 	}
4179 	/*
4180 	 * If we have found the just finished dependency, then free
4181 	 * it along with anything that follows it that is complete.
4182 	 * If the inode still has a bitmap dependency, then it has
4183 	 * never been written to disk, hence the on-disk inode cannot
4184 	 * reference the old fragment so we can free it without delay.
4185 	 */
4186 	delay = (inodedep->id_state & DEPCOMPLETE);
4187 	for (; adp; adp = listadp) {
4188 		listadp = TAILQ_NEXT(adp, ad_next);
4189 		if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4190 			return;
4191 		free_allocdirect(listhead, adp, delay);
4192 	}
4193 }
4194 
4195 /*
4196  * Called from within softdep_disk_write_complete above. Note that
4197  * this routine is always called from interrupt level with further
4198  * splbio interrupts blocked.
4199  */
4200 static void
4201 handle_allocindir_partdone(aip)
4202 	struct allocindir *aip;		/* the completed allocindir */
4203 {
4204 	struct indirdep *indirdep;
4205 
4206 	if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
4207 		return;
4208 	if (aip->ai_buf != NULL) {
4209 		lk.lkt_held = NOHOLDER;
4210 		panic("handle_allocindir_partdone: dangling dependency");
4211 	}
4212 	indirdep = aip->ai_indirdep;
4213 	if (indirdep->ir_state & UNDONE) {
4214 		LIST_REMOVE(aip, ai_next);
4215 		LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
4216 		return;
4217 	}
4218 	if (indirdep->ir_state & UFS1FMT)
4219 		((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4220 		    aip->ai_newblkno;
4221 	else
4222 		((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4223 		    aip->ai_newblkno;
4224 	LIST_REMOVE(aip, ai_next);
4225 	if (aip->ai_freefrag != NULL)
4226 		add_to_worklist(&aip->ai_freefrag->ff_list);
4227 	WORKITEM_FREE(aip, D_ALLOCINDIR);
4228 }
4229 
4230 /*
4231  * Called from within softdep_disk_write_complete above to restore
4232  * in-memory inode block contents to their most up-to-date state. Note
4233  * that this routine is always called from interrupt level with further
4234  * splbio interrupts blocked.
4235  */
4236 static int
4237 handle_written_inodeblock(inodedep, bp)
4238 	struct inodedep *inodedep;
4239 	struct buf *bp;		/* buffer containing the inode block */
4240 {
4241 	struct worklist *wk, *filefree;
4242 	struct allocdirect *adp, *nextadp;
4243 	struct ufs1_dinode *dp1 = NULL;
4244 	struct ufs2_dinode *dp2 = NULL;
4245 	int hadchanges, fstype;
4246 
4247 	if ((inodedep->id_state & IOSTARTED) == 0) {
4248 		lk.lkt_held = NOHOLDER;
4249 		panic("handle_written_inodeblock: not started");
4250 	}
4251 	inodedep->id_state &= ~IOSTARTED;
4252 	inodedep->id_state |= COMPLETE;
4253 	if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
4254 		fstype = UFS1;
4255 		dp1 = (struct ufs1_dinode *)bp->b_data +
4256 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4257 	} else {
4258 		fstype = UFS2;
4259 		dp2 = (struct ufs2_dinode *)bp->b_data +
4260 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4261 	}
4262 	/*
4263 	 * If we had to rollback the inode allocation because of
4264 	 * bitmaps being incomplete, then simply restore it.
4265 	 * Keep the block dirty so that it will not be reclaimed until
4266 	 * all associated dependencies have been cleared and the
4267 	 * corresponding updates written to disk.
4268 	 */
4269 	if (inodedep->id_savedino1 != NULL) {
4270 		if (fstype == UFS1)
4271 			*dp1 = *inodedep->id_savedino1;
4272 		else
4273 			*dp2 = *inodedep->id_savedino2;
4274 		FREE(inodedep->id_savedino1, M_INODEDEP);
4275 		inodedep->id_savedino1 = NULL;
4276 		if ((bp->b_flags & B_DELWRI) == 0)
4277 			stat_inode_bitmap++;
4278 		bdirty(bp);
4279 		return (1);
4280 	}
4281 	/*
4282 	 * Roll forward anything that had to be rolled back before
4283 	 * the inode could be updated.
4284 	 */
4285 	hadchanges = 0;
4286 	for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
4287 		nextadp = TAILQ_NEXT(adp, ad_next);
4288 		if (adp->ad_state & ATTACHED) {
4289 			lk.lkt_held = NOHOLDER;
4290 			panic("handle_written_inodeblock: new entry");
4291 		}
4292 		if (fstype == UFS1) {
4293 			if (adp->ad_lbn < NDADDR) {
4294 				if (dp1->di_db[adp->ad_lbn]!=adp->ad_oldblkno) {
4295 					lk.lkt_held = NOHOLDER;
4296 					panic("%s %s #%jd mismatch %d != %jd",
4297 					    "handle_written_inodeblock:",
4298 					    "direct pointer",
4299 					    (intmax_t)adp->ad_lbn,
4300 					    dp1->di_db[adp->ad_lbn],
4301 					    (intmax_t)adp->ad_oldblkno);
4302 				}
4303 				dp1->di_db[adp->ad_lbn] = adp->ad_newblkno;
4304 			} else {
4305 				if (dp1->di_ib[adp->ad_lbn - NDADDR] != 0) {
4306 					lk.lkt_held = NOHOLDER;
4307 					panic("%s: %s #%jd allocated as %d",
4308 					    "handle_written_inodeblock",
4309 					    "indirect pointer",
4310 					    (intmax_t)adp->ad_lbn - NDADDR,
4311 					    dp1->di_ib[adp->ad_lbn - NDADDR]);
4312 				}
4313 				dp1->di_ib[adp->ad_lbn - NDADDR] =
4314 				    adp->ad_newblkno;
4315 			}
4316 		} else {
4317 			if (adp->ad_lbn < NDADDR) {
4318 				if (dp2->di_db[adp->ad_lbn]!=adp->ad_oldblkno) {
4319 					lk.lkt_held = NOHOLDER;
4320 					panic("%s: %s #%jd %s %jd != %jd",
4321 					    "handle_written_inodeblock",
4322 					    "direct pointer",
4323 					    (intmax_t)adp->ad_lbn, "mismatch",
4324 					    (intmax_t)dp2->di_db[adp->ad_lbn],
4325 					    (intmax_t)adp->ad_oldblkno);
4326 				}
4327 				dp2->di_db[adp->ad_lbn] = adp->ad_newblkno;
4328 			} else {
4329 				if (dp2->di_ib[adp->ad_lbn - NDADDR] != 0) {
4330 					lk.lkt_held = NOHOLDER;
4331 					panic("%s: %s #%jd allocated as %jd",
4332 					    "handle_written_inodeblock",
4333 					    "indirect pointer",
4334 					    (intmax_t)adp->ad_lbn - NDADDR,
4335 					    (intmax_t)
4336 					    dp2->di_ib[adp->ad_lbn - NDADDR]);
4337 				}
4338 				dp2->di_ib[adp->ad_lbn - NDADDR] =
4339 				    adp->ad_newblkno;
4340 			}
4341 		}
4342 		adp->ad_state &= ~UNDONE;
4343 		adp->ad_state |= ATTACHED;
4344 		hadchanges = 1;
4345 	}
4346 	for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
4347 		nextadp = TAILQ_NEXT(adp, ad_next);
4348 		if (adp->ad_state & ATTACHED) {
4349 			lk.lkt_held = NOHOLDER;
4350 			panic("handle_written_inodeblock: new entry");
4351 		}
4352 		if (dp2->di_extb[adp->ad_lbn] != adp->ad_oldblkno) {
4353 			lk.lkt_held = NOHOLDER;
4354 			panic("%s: direct pointers #%jd %s %jd != %jd",
4355 			    "handle_written_inodeblock",
4356 			    (intmax_t)adp->ad_lbn, "mismatch",
4357 			    (intmax_t)dp2->di_extb[adp->ad_lbn],
4358 			    (intmax_t)adp->ad_oldblkno);
4359 		}
4360 		dp2->di_extb[adp->ad_lbn] = adp->ad_newblkno;
4361 		adp->ad_state &= ~UNDONE;
4362 		adp->ad_state |= ATTACHED;
4363 		hadchanges = 1;
4364 	}
4365 	if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
4366 		stat_direct_blk_ptrs++;
4367 	/*
4368 	 * Reset the file size to its most up-to-date value.
4369 	 */
4370 	if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) {
4371 		lk.lkt_held = NOHOLDER;
4372 		panic("handle_written_inodeblock: bad size");
4373 	}
4374 	if (fstype == UFS1) {
4375 		if (dp1->di_size != inodedep->id_savedsize) {
4376 			dp1->di_size = inodedep->id_savedsize;
4377 			hadchanges = 1;
4378 		}
4379 	} else {
4380 		if (dp2->di_size != inodedep->id_savedsize) {
4381 			dp2->di_size = inodedep->id_savedsize;
4382 			hadchanges = 1;
4383 		}
4384 		if (dp2->di_extsize != inodedep->id_savedextsize) {
4385 			dp2->di_extsize = inodedep->id_savedextsize;
4386 			hadchanges = 1;
4387 		}
4388 	}
4389 	inodedep->id_savedsize = -1;
4390 	inodedep->id_savedextsize = -1;
4391 	/*
4392 	 * If there were any rollbacks in the inode block, then it must be
4393 	 * marked dirty so that its will eventually get written back in
4394 	 * its correct form.
4395 	 */
4396 	if (hadchanges)
4397 		bdirty(bp);
4398 	/*
4399 	 * Process any allocdirects that completed during the update.
4400 	 */
4401 	if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
4402 		handle_allocdirect_partdone(adp);
4403 	if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
4404 		handle_allocdirect_partdone(adp);
4405 	/*
4406 	 * Process deallocations that were held pending until the
4407 	 * inode had been written to disk. Freeing of the inode
4408 	 * is delayed until after all blocks have been freed to
4409 	 * avoid creation of new <vfsid, inum, lbn> triples
4410 	 * before the old ones have been deleted.
4411 	 */
4412 	filefree = NULL;
4413 	while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
4414 		WORKLIST_REMOVE(wk);
4415 		switch (wk->wk_type) {
4416 
4417 		case D_FREEFILE:
4418 			/*
4419 			 * We defer adding filefree to the worklist until
4420 			 * all other additions have been made to ensure
4421 			 * that it will be done after all the old blocks
4422 			 * have been freed.
4423 			 */
4424 			if (filefree != NULL) {
4425 				lk.lkt_held = NOHOLDER;
4426 				panic("handle_written_inodeblock: filefree");
4427 			}
4428 			filefree = wk;
4429 			continue;
4430 
4431 		case D_MKDIR:
4432 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
4433 			continue;
4434 
4435 		case D_DIRADD:
4436 			diradd_inode_written(WK_DIRADD(wk), inodedep);
4437 			continue;
4438 
4439 		case D_FREEBLKS:
4440 		case D_FREEFRAG:
4441 		case D_DIRREM:
4442 			add_to_worklist(wk);
4443 			continue;
4444 
4445 		case D_NEWDIRBLK:
4446 			free_newdirblk(WK_NEWDIRBLK(wk));
4447 			continue;
4448 
4449 		default:
4450 			lk.lkt_held = NOHOLDER;
4451 			panic("handle_written_inodeblock: Unknown type %s",
4452 			    TYPENAME(wk->wk_type));
4453 			/* NOTREACHED */
4454 		}
4455 	}
4456 	if (filefree != NULL) {
4457 		if (free_inodedep(inodedep) == 0) {
4458 			lk.lkt_held = NOHOLDER;
4459 			panic("handle_written_inodeblock: live inodedep");
4460 		}
4461 		add_to_worklist(filefree);
4462 		return (0);
4463 	}
4464 
4465 	/*
4466 	 * If no outstanding dependencies, free it.
4467 	 */
4468 	if (free_inodedep(inodedep) ||
4469 	    (TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
4470 	     TAILQ_FIRST(&inodedep->id_extupdt) == 0))
4471 		return (0);
4472 	return (hadchanges);
4473 }
4474 
4475 /*
4476  * Process a diradd entry after its dependent inode has been written.
4477  * This routine must be called with splbio interrupts blocked.
4478  */
4479 static void
4480 diradd_inode_written(dap, inodedep)
4481 	struct diradd *dap;
4482 	struct inodedep *inodedep;
4483 {
4484 	struct pagedep *pagedep;
4485 
4486 	dap->da_state |= COMPLETE;
4487 	if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4488 		if (dap->da_state & DIRCHG)
4489 			pagedep = dap->da_previous->dm_pagedep;
4490 		else
4491 			pagedep = dap->da_pagedep;
4492 		LIST_REMOVE(dap, da_pdlist);
4493 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4494 	}
4495 	WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
4496 }
4497 
4498 /*
4499  * Handle the completion of a mkdir dependency.
4500  */
4501 static void
4502 handle_written_mkdir(mkdir, type)
4503 	struct mkdir *mkdir;
4504 	int type;
4505 {
4506 	struct diradd *dap;
4507 	struct pagedep *pagedep;
4508 
4509 	if (mkdir->md_state != type) {
4510 		lk.lkt_held = NOHOLDER;
4511 		panic("handle_written_mkdir: bad type");
4512 	}
4513 	dap = mkdir->md_diradd;
4514 	dap->da_state &= ~type;
4515 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
4516 		dap->da_state |= DEPCOMPLETE;
4517 	if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4518 		if (dap->da_state & DIRCHG)
4519 			pagedep = dap->da_previous->dm_pagedep;
4520 		else
4521 			pagedep = dap->da_pagedep;
4522 		LIST_REMOVE(dap, da_pdlist);
4523 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4524 	}
4525 	LIST_REMOVE(mkdir, md_mkdirs);
4526 	WORKITEM_FREE(mkdir, D_MKDIR);
4527 }
4528 
4529 /*
4530  * Called from within softdep_disk_write_complete above.
4531  * A write operation was just completed. Removed inodes can
4532  * now be freed and associated block pointers may be committed.
4533  * Note that this routine is always called from interrupt level
4534  * with further splbio interrupts blocked.
4535  */
4536 static int
4537 handle_written_filepage(pagedep, bp)
4538 	struct pagedep *pagedep;
4539 	struct buf *bp;		/* buffer containing the written page */
4540 {
4541 	struct dirrem *dirrem;
4542 	struct diradd *dap, *nextdap;
4543 	struct direct *ep;
4544 	int i, chgs;
4545 
4546 	if ((pagedep->pd_state & IOSTARTED) == 0) {
4547 		lk.lkt_held = NOHOLDER;
4548 		panic("handle_written_filepage: not started");
4549 	}
4550 	pagedep->pd_state &= ~IOSTARTED;
4551 	/*
4552 	 * Process any directory removals that have been committed.
4553 	 */
4554 	while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
4555 		LIST_REMOVE(dirrem, dm_next);
4556 		dirrem->dm_dirinum = pagedep->pd_ino;
4557 		add_to_worklist(&dirrem->dm_list);
4558 	}
4559 	/*
4560 	 * Free any directory additions that have been committed.
4561 	 * If it is a newly allocated block, we have to wait until
4562 	 * the on-disk directory inode claims the new block.
4563 	 */
4564 	if ((pagedep->pd_state & NEWBLOCK) == 0)
4565 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
4566 			free_diradd(dap);
4567 	/*
4568 	 * Uncommitted directory entries must be restored.
4569 	 */
4570 	for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
4571 		for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
4572 		     dap = nextdap) {
4573 			nextdap = LIST_NEXT(dap, da_pdlist);
4574 			if (dap->da_state & ATTACHED) {
4575 				lk.lkt_held = NOHOLDER;
4576 				panic("handle_written_filepage: attached");
4577 			}
4578 			ep = (struct direct *)
4579 			    ((char *)bp->b_data + dap->da_offset);
4580 			ep->d_ino = dap->da_newinum;
4581 			dap->da_state &= ~UNDONE;
4582 			dap->da_state |= ATTACHED;
4583 			chgs = 1;
4584 			/*
4585 			 * If the inode referenced by the directory has
4586 			 * been written out, then the dependency can be
4587 			 * moved to the pending list.
4588 			 */
4589 			if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4590 				LIST_REMOVE(dap, da_pdlist);
4591 				LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
4592 				    da_pdlist);
4593 			}
4594 		}
4595 	}
4596 	/*
4597 	 * If there were any rollbacks in the directory, then it must be
4598 	 * marked dirty so that its will eventually get written back in
4599 	 * its correct form.
4600 	 */
4601 	if (chgs) {
4602 		if ((bp->b_flags & B_DELWRI) == 0)
4603 			stat_dir_entry++;
4604 		bdirty(bp);
4605 		return (1);
4606 	}
4607 	/*
4608 	 * If we are not waiting for a new directory block to be
4609 	 * claimed by its inode, then the pagedep will be freed.
4610 	 * Otherwise it will remain to track any new entries on
4611 	 * the page in case they are fsync'ed.
4612 	 */
4613 	if ((pagedep->pd_state & NEWBLOCK) == 0) {
4614 		LIST_REMOVE(pagedep, pd_hash);
4615 		WORKITEM_FREE(pagedep, D_PAGEDEP);
4616 	}
4617 	return (0);
4618 }
4619 
4620 /*
4621  * Writing back in-core inode structures.
4622  *
4623  * The filesystem only accesses an inode's contents when it occupies an
4624  * "in-core" inode structure.  These "in-core" structures are separate from
4625  * the page frames used to cache inode blocks.  Only the latter are
4626  * transferred to/from the disk.  So, when the updated contents of the
4627  * "in-core" inode structure are copied to the corresponding in-memory inode
4628  * block, the dependencies are also transferred.  The following procedure is
4629  * called when copying a dirty "in-core" inode to a cached inode block.
4630  */
4631 
4632 /*
4633  * Called when an inode is loaded from disk. If the effective link count
4634  * differed from the actual link count when it was last flushed, then we
4635  * need to ensure that the correct effective link count is put back.
4636  */
4637 void
4638 softdep_load_inodeblock(ip)
4639 	struct inode *ip;	/* the "in_core" copy of the inode */
4640 {
4641 	struct inodedep *inodedep;
4642 
4643 	/*
4644 	 * Check for alternate nlink count.
4645 	 */
4646 	ip->i_effnlink = ip->i_nlink;
4647 	ACQUIRE_LOCK(&lk);
4648 	if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
4649 		FREE_LOCK(&lk);
4650 		return;
4651 	}
4652 	ip->i_effnlink -= inodedep->id_nlinkdelta;
4653 	if (inodedep->id_state & SPACECOUNTED)
4654 		ip->i_flag |= IN_SPACECOUNTED;
4655 	FREE_LOCK(&lk);
4656 }
4657 
4658 /*
4659  * This routine is called just before the "in-core" inode
4660  * information is to be copied to the in-memory inode block.
4661  * Recall that an inode block contains several inodes. If
4662  * the force flag is set, then the dependencies will be
4663  * cleared so that the update can always be made. Note that
4664  * the buffer is locked when this routine is called, so we
4665  * will never be in the middle of writing the inode block
4666  * to disk.
4667  */
4668 void
4669 softdep_update_inodeblock(ip, bp, waitfor)
4670 	struct inode *ip;	/* the "in_core" copy of the inode */
4671 	struct buf *bp;		/* the buffer containing the inode block */
4672 	int waitfor;		/* nonzero => update must be allowed */
4673 {
4674 	struct inodedep *inodedep;
4675 	struct worklist *wk;
4676 	struct buf *ibp;
4677 	int error;
4678 
4679 	/*
4680 	 * If the effective link count is not equal to the actual link
4681 	 * count, then we must track the difference in an inodedep while
4682 	 * the inode is (potentially) tossed out of the cache. Otherwise,
4683 	 * if there is no existing inodedep, then there are no dependencies
4684 	 * to track.
4685 	 */
4686 	ACQUIRE_LOCK(&lk);
4687 	if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
4688 		FREE_LOCK(&lk);
4689 		if (ip->i_effnlink != ip->i_nlink)
4690 			panic("softdep_update_inodeblock: bad link count");
4691 		return;
4692 	}
4693 	if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) {
4694 		FREE_LOCK(&lk);
4695 		panic("softdep_update_inodeblock: bad delta");
4696 	}
4697 	/*
4698 	 * Changes have been initiated. Anything depending on these
4699 	 * changes cannot occur until this inode has been written.
4700 	 */
4701 	inodedep->id_state &= ~COMPLETE;
4702 	if ((inodedep->id_state & ONWORKLIST) == 0)
4703 		WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
4704 	/*
4705 	 * Any new dependencies associated with the incore inode must
4706 	 * now be moved to the list associated with the buffer holding
4707 	 * the in-memory copy of the inode. Once merged process any
4708 	 * allocdirects that are completed by the merger.
4709 	 */
4710 	merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
4711 	if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL)
4712 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
4713 	merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
4714 	if (TAILQ_FIRST(&inodedep->id_extupdt) != NULL)
4715 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt));
4716 	/*
4717 	 * Now that the inode has been pushed into the buffer, the
4718 	 * operations dependent on the inode being written to disk
4719 	 * can be moved to the id_bufwait so that they will be
4720 	 * processed when the buffer I/O completes.
4721 	 */
4722 	while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
4723 		WORKLIST_REMOVE(wk);
4724 		WORKLIST_INSERT(&inodedep->id_bufwait, wk);
4725 	}
4726 	/*
4727 	 * Newly allocated inodes cannot be written until the bitmap
4728 	 * that allocates them have been written (indicated by
4729 	 * DEPCOMPLETE being set in id_state). If we are doing a
4730 	 * forced sync (e.g., an fsync on a file), we force the bitmap
4731 	 * to be written so that the update can be done.
4732 	 */
4733 	if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) {
4734 		FREE_LOCK(&lk);
4735 		return;
4736 	}
4737 	ibp = inodedep->id_buf;
4738 	ibp = getdirtybuf(&ibp, NULL, MNT_WAIT);
4739 	FREE_LOCK(&lk);
4740 	if (ibp && (error = bwrite(ibp)) != 0)
4741 		softdep_error("softdep_update_inodeblock: bwrite", error);
4742 	if ((inodedep->id_state & DEPCOMPLETE) == 0)
4743 		panic("softdep_update_inodeblock: update failed");
4744 }
4745 
4746 /*
4747  * Merge the a new inode dependency list (such as id_newinoupdt) into an
4748  * old inode dependency list (such as id_inoupdt). This routine must be
4749  * called with splbio interrupts blocked.
4750  */
4751 static void
4752 merge_inode_lists(newlisthead, oldlisthead)
4753 	struct allocdirectlst *newlisthead;
4754 	struct allocdirectlst *oldlisthead;
4755 {
4756 	struct allocdirect *listadp, *newadp;
4757 
4758 	newadp = TAILQ_FIRST(newlisthead);
4759 	for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
4760 		if (listadp->ad_lbn < newadp->ad_lbn) {
4761 			listadp = TAILQ_NEXT(listadp, ad_next);
4762 			continue;
4763 		}
4764 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
4765 		TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
4766 		if (listadp->ad_lbn == newadp->ad_lbn) {
4767 			allocdirect_merge(oldlisthead, newadp,
4768 			    listadp);
4769 			listadp = newadp;
4770 		}
4771 		newadp = TAILQ_FIRST(newlisthead);
4772 	}
4773 	while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
4774 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
4775 		TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
4776 	}
4777 }
4778 
4779 /*
4780  * If we are doing an fsync, then we must ensure that any directory
4781  * entries for the inode have been written after the inode gets to disk.
4782  */
4783 int
4784 softdep_fsync(vp)
4785 	struct vnode *vp;	/* the "in_core" copy of the inode */
4786 {
4787 	struct inodedep *inodedep;
4788 	struct pagedep *pagedep;
4789 	struct worklist *wk;
4790 	struct diradd *dap;
4791 	struct mount *mnt;
4792 	struct vnode *pvp;
4793 	struct inode *ip;
4794 	struct buf *bp;
4795 	struct fs *fs;
4796 	struct thread *td = curthread;
4797 	int error, flushparent;
4798 	ino_t parentino;
4799 	ufs_lbn_t lbn;
4800 
4801 	ip = VTOI(vp);
4802 	fs = ip->i_fs;
4803 	ACQUIRE_LOCK(&lk);
4804 	if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) {
4805 		FREE_LOCK(&lk);
4806 		return (0);
4807 	}
4808 	if (LIST_FIRST(&inodedep->id_inowait) != NULL ||
4809 	    LIST_FIRST(&inodedep->id_bufwait) != NULL ||
4810 	    TAILQ_FIRST(&inodedep->id_extupdt) != NULL ||
4811 	    TAILQ_FIRST(&inodedep->id_newextupdt) != NULL ||
4812 	    TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
4813 	    TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) {
4814 		FREE_LOCK(&lk);
4815 		panic("softdep_fsync: pending ops");
4816 	}
4817 	for (error = 0, flushparent = 0; ; ) {
4818 		if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
4819 			break;
4820 		if (wk->wk_type != D_DIRADD) {
4821 			FREE_LOCK(&lk);
4822 			panic("softdep_fsync: Unexpected type %s",
4823 			    TYPENAME(wk->wk_type));
4824 		}
4825 		dap = WK_DIRADD(wk);
4826 		/*
4827 		 * Flush our parent if this directory entry has a MKDIR_PARENT
4828 		 * dependency or is contained in a newly allocated block.
4829 		 */
4830 		if (dap->da_state & DIRCHG)
4831 			pagedep = dap->da_previous->dm_pagedep;
4832 		else
4833 			pagedep = dap->da_pagedep;
4834 		mnt = pagedep->pd_mnt;
4835 		parentino = pagedep->pd_ino;
4836 		lbn = pagedep->pd_lbn;
4837 		if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) {
4838 			FREE_LOCK(&lk);
4839 			panic("softdep_fsync: dirty");
4840 		}
4841 		if ((dap->da_state & MKDIR_PARENT) ||
4842 		    (pagedep->pd_state & NEWBLOCK))
4843 			flushparent = 1;
4844 		else
4845 			flushparent = 0;
4846 		/*
4847 		 * If we are being fsync'ed as part of vgone'ing this vnode,
4848 		 * then we will not be able to release and recover the
4849 		 * vnode below, so we just have to give up on writing its
4850 		 * directory entry out. It will eventually be written, just
4851 		 * not now, but then the user was not asking to have it
4852 		 * written, so we are not breaking any promises.
4853 		 */
4854 		if (vp->v_iflag & VI_XLOCK)
4855 			break;
4856 		/*
4857 		 * We prevent deadlock by always fetching inodes from the
4858 		 * root, moving down the directory tree. Thus, when fetching
4859 		 * our parent directory, we first try to get the lock. If
4860 		 * that fails, we must unlock ourselves before requesting
4861 		 * the lock on our parent. See the comment in ufs_lookup
4862 		 * for details on possible races.
4863 		 */
4864 		FREE_LOCK(&lk);
4865 		if (VFS_VGET(mnt, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp)) {
4866 			VOP_UNLOCK(vp, 0, td);
4867 			error = VFS_VGET(mnt, parentino, LK_EXCLUSIVE, &pvp);
4868 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4869 			if (error != 0)
4870 				return (error);
4871 		}
4872 		/*
4873 		 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
4874 		 * that are contained in direct blocks will be resolved by
4875 		 * doing a UFS_UPDATE. Pagedeps contained in indirect blocks
4876 		 * may require a complete sync'ing of the directory. So, we
4877 		 * try the cheap and fast UFS_UPDATE first, and if that fails,
4878 		 * then we do the slower VOP_FSYNC of the directory.
4879 		 */
4880 		if (flushparent) {
4881 			if ((error = UFS_UPDATE(pvp, 1)) != 0) {
4882 				vput(pvp);
4883 				return (error);
4884 			}
4885 			if ((pagedep->pd_state & NEWBLOCK) &&
4886 			    (error = VOP_FSYNC(pvp, td->td_ucred, MNT_WAIT, td))) {
4887 				vput(pvp);
4888 				return (error);
4889 			}
4890 		}
4891 		/*
4892 		 * Flush directory page containing the inode's name.
4893 		 */
4894 		error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
4895 		    &bp);
4896 		if (error == 0)
4897 			error = bwrite(bp);
4898 		else
4899 			brelse(bp);
4900 		vput(pvp);
4901 		if (error != 0)
4902 			return (error);
4903 		ACQUIRE_LOCK(&lk);
4904 		if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0)
4905 			break;
4906 	}
4907 	FREE_LOCK(&lk);
4908 	return (0);
4909 }
4910 
4911 /*
4912  * Flush all the dirty bitmaps associated with the block device
4913  * before flushing the rest of the dirty blocks so as to reduce
4914  * the number of dependencies that will have to be rolled back.
4915  */
4916 void
4917 softdep_fsync_mountdev(vp)
4918 	struct vnode *vp;
4919 {
4920 	struct buf *bp, *nbp;
4921 	struct worklist *wk;
4922 
4923 	if (!vn_isdisk(vp, NULL))
4924 		panic("softdep_fsync_mountdev: vnode not a disk");
4925 	ACQUIRE_LOCK(&lk);
4926 	VI_LOCK(vp);
4927 	for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
4928 		nbp = TAILQ_NEXT(bp, b_vnbufs);
4929 		/*
4930 		 * If it is already scheduled, skip to the next buffer.
4931 		 */
4932 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
4933 			continue;
4934 
4935 		if ((bp->b_flags & B_DELWRI) == 0) {
4936 			FREE_LOCK(&lk);
4937 			panic("softdep_fsync_mountdev: not dirty");
4938 		}
4939 		/*
4940 		 * We are only interested in bitmaps with outstanding
4941 		 * dependencies.
4942 		 */
4943 		if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
4944 		    wk->wk_type != D_BMSAFEMAP ||
4945 		    (bp->b_vflags & BV_BKGRDINPROG)) {
4946 			BUF_UNLOCK(bp);
4947 			continue;
4948 		}
4949 		VI_UNLOCK(vp);
4950 		bremfree(bp);
4951 		FREE_LOCK(&lk);
4952 		(void) bawrite(bp);
4953 		ACQUIRE_LOCK(&lk);
4954 		/*
4955 		 * Since we may have slept during the I/O, we need
4956 		 * to start from a known point.
4957 		 */
4958 		VI_LOCK(vp);
4959 		nbp = TAILQ_FIRST(&vp->v_dirtyblkhd);
4960 	}
4961 	drain_output(vp, 1);
4962 	VI_UNLOCK(vp);
4963 	FREE_LOCK(&lk);
4964 }
4965 
4966 /*
4967  * This routine is called when we are trying to synchronously flush a
4968  * file. This routine must eliminate any filesystem metadata dependencies
4969  * so that the syncing routine can succeed by pushing the dirty blocks
4970  * associated with the file. If any I/O errors occur, they are returned.
4971  */
4972 int
4973 softdep_sync_metadata(ap)
4974 	struct vop_fsync_args /* {
4975 		struct vnode *a_vp;
4976 		struct ucred *a_cred;
4977 		int a_waitfor;
4978 		struct thread *a_td;
4979 	} */ *ap;
4980 {
4981 	struct vnode *vp = ap->a_vp;
4982 	struct pagedep *pagedep;
4983 	struct allocdirect *adp;
4984 	struct allocindir *aip;
4985 	struct buf *bp, *nbp;
4986 	struct worklist *wk;
4987 	int i, error, waitfor;
4988 
4989 	/*
4990 	 * Check whether this vnode is involved in a filesystem
4991 	 * that is doing soft dependency processing.
4992 	 */
4993 	if (!vn_isdisk(vp, NULL)) {
4994 		if (!DOINGSOFTDEP(vp))
4995 			return (0);
4996 	} else
4997 		if (vp->v_rdev->si_mountpoint == NULL ||
4998 		    (vp->v_rdev->si_mountpoint->mnt_flag & MNT_SOFTDEP) == 0)
4999 			return (0);
5000 	/*
5001 	 * Ensure that any direct block dependencies have been cleared.
5002 	 */
5003 	ACQUIRE_LOCK(&lk);
5004 	if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) {
5005 		FREE_LOCK(&lk);
5006 		return (error);
5007 	}
5008 	/*
5009 	 * For most files, the only metadata dependencies are the
5010 	 * cylinder group maps that allocate their inode or blocks.
5011 	 * The block allocation dependencies can be found by traversing
5012 	 * the dependency lists for any buffers that remain on their
5013 	 * dirty buffer list. The inode allocation dependency will
5014 	 * be resolved when the inode is updated with MNT_WAIT.
5015 	 * This work is done in two passes. The first pass grabs most
5016 	 * of the buffers and begins asynchronously writing them. The
5017 	 * only way to wait for these asynchronous writes is to sleep
5018 	 * on the filesystem vnode which may stay busy for a long time
5019 	 * if the filesystem is active. So, instead, we make a second
5020 	 * pass over the dependencies blocking on each write. In the
5021 	 * usual case we will be blocking against a write that we
5022 	 * initiated, so when it is done the dependency will have been
5023 	 * resolved. Thus the second pass is expected to end quickly.
5024 	 */
5025 	waitfor = MNT_NOWAIT;
5026 top:
5027 	/*
5028 	 * We must wait for any I/O in progress to finish so that
5029 	 * all potential buffers on the dirty list will be visible.
5030 	 */
5031 	VI_LOCK(vp);
5032 	drain_output(vp, 1);
5033 	bp = getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd),
5034 	    VI_MTX(vp), MNT_WAIT);
5035 	if (bp == NULL) {
5036 		VI_UNLOCK(vp);
5037 		FREE_LOCK(&lk);
5038 		return (0);
5039 	}
5040 	/* While syncing snapshots, we must allow recursive lookups */
5041 	bp->b_lock.lk_flags |= LK_CANRECURSE;
5042 loop:
5043 	/*
5044 	 * As we hold the buffer locked, none of its dependencies
5045 	 * will disappear.
5046 	 */
5047 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5048 		switch (wk->wk_type) {
5049 
5050 		case D_ALLOCDIRECT:
5051 			adp = WK_ALLOCDIRECT(wk);
5052 			if (adp->ad_state & DEPCOMPLETE)
5053 				continue;
5054 			nbp = adp->ad_buf;
5055 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5056 			if (nbp == NULL)
5057 				continue;
5058 			FREE_LOCK(&lk);
5059 			if (waitfor == MNT_NOWAIT) {
5060 				bawrite(nbp);
5061 			} else if ((error = bwrite(nbp)) != 0) {
5062 				break;
5063 			}
5064 			ACQUIRE_LOCK(&lk);
5065 			continue;
5066 
5067 		case D_ALLOCINDIR:
5068 			aip = WK_ALLOCINDIR(wk);
5069 			if (aip->ai_state & DEPCOMPLETE)
5070 				continue;
5071 			nbp = aip->ai_buf;
5072 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5073 			if (nbp == NULL)
5074 				continue;
5075 			FREE_LOCK(&lk);
5076 			if (waitfor == MNT_NOWAIT) {
5077 				bawrite(nbp);
5078 			} else if ((error = bwrite(nbp)) != 0) {
5079 				break;
5080 			}
5081 			ACQUIRE_LOCK(&lk);
5082 			continue;
5083 
5084 		case D_INDIRDEP:
5085 		restart:
5086 
5087 			LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
5088 				if (aip->ai_state & DEPCOMPLETE)
5089 					continue;
5090 				nbp = aip->ai_buf;
5091 				nbp = getdirtybuf(&nbp, NULL, MNT_WAIT);
5092 				if (nbp == NULL)
5093 					goto restart;
5094 				FREE_LOCK(&lk);
5095 				if ((error = bwrite(nbp)) != 0) {
5096 					break;
5097 				}
5098 				ACQUIRE_LOCK(&lk);
5099 				goto restart;
5100 			}
5101 			continue;
5102 
5103 		case D_INODEDEP:
5104 			if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs,
5105 			    WK_INODEDEP(wk)->id_ino)) != 0) {
5106 				FREE_LOCK(&lk);
5107 				break;
5108 			}
5109 			continue;
5110 
5111 		case D_PAGEDEP:
5112 			/*
5113 			 * We are trying to sync a directory that may
5114 			 * have dependencies on both its own metadata
5115 			 * and/or dependencies on the inodes of any
5116 			 * recently allocated files. We walk its diradd
5117 			 * lists pushing out the associated inode.
5118 			 */
5119 			pagedep = WK_PAGEDEP(wk);
5120 			for (i = 0; i < DAHASHSZ; i++) {
5121 				if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
5122 					continue;
5123 				if ((error =
5124 				    flush_pagedep_deps(vp, pagedep->pd_mnt,
5125 						&pagedep->pd_diraddhd[i]))) {
5126 					FREE_LOCK(&lk);
5127 					break;
5128 				}
5129 			}
5130 			continue;
5131 
5132 		case D_MKDIR:
5133 			/*
5134 			 * This case should never happen if the vnode has
5135 			 * been properly sync'ed. However, if this function
5136 			 * is used at a place where the vnode has not yet
5137 			 * been sync'ed, this dependency can show up. So,
5138 			 * rather than panic, just flush it.
5139 			 */
5140 			nbp = WK_MKDIR(wk)->md_buf;
5141 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5142 			if (nbp == NULL)
5143 				continue;
5144 			FREE_LOCK(&lk);
5145 			if (waitfor == MNT_NOWAIT) {
5146 				bawrite(nbp);
5147 			} else if ((error = bwrite(nbp)) != 0) {
5148 				break;
5149 			}
5150 			ACQUIRE_LOCK(&lk);
5151 			continue;
5152 
5153 		case D_BMSAFEMAP:
5154 			/*
5155 			 * This case should never happen if the vnode has
5156 			 * been properly sync'ed. However, if this function
5157 			 * is used at a place where the vnode has not yet
5158 			 * been sync'ed, this dependency can show up. So,
5159 			 * rather than panic, just flush it.
5160 			 */
5161 			nbp = WK_BMSAFEMAP(wk)->sm_buf;
5162 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5163 			if (nbp == NULL)
5164 				continue;
5165 			FREE_LOCK(&lk);
5166 			if (waitfor == MNT_NOWAIT) {
5167 				bawrite(nbp);
5168 			} else if ((error = bwrite(nbp)) != 0) {
5169 				break;
5170 			}
5171 			ACQUIRE_LOCK(&lk);
5172 			continue;
5173 
5174 		default:
5175 			FREE_LOCK(&lk);
5176 			panic("softdep_sync_metadata: Unknown type %s",
5177 			    TYPENAME(wk->wk_type));
5178 			/* NOTREACHED */
5179 		}
5180 		/* We reach here only in error and unlocked */
5181 		if (error == 0)
5182 			panic("softdep_sync_metadata: zero error");
5183 		bp->b_lock.lk_flags &= ~LK_CANRECURSE;
5184 		bawrite(bp);
5185 		return (error);
5186 	}
5187 	VI_LOCK(vp);
5188 	nbp = getdirtybuf(&TAILQ_NEXT(bp, b_vnbufs), VI_MTX(vp), MNT_WAIT);
5189 	if (nbp == NULL)
5190 		VI_UNLOCK(vp);
5191 	FREE_LOCK(&lk);
5192 	bp->b_lock.lk_flags &= ~LK_CANRECURSE;
5193 	bawrite(bp);
5194 	ACQUIRE_LOCK(&lk);
5195 	if (nbp != NULL) {
5196 		bp = nbp;
5197 		goto loop;
5198 	}
5199 	/*
5200 	 * The brief unlock is to allow any pent up dependency
5201 	 * processing to be done. Then proceed with the second pass.
5202 	 */
5203 	if (waitfor == MNT_NOWAIT) {
5204 		waitfor = MNT_WAIT;
5205 		FREE_LOCK(&lk);
5206 		ACQUIRE_LOCK(&lk);
5207 		goto top;
5208 	}
5209 
5210 	/*
5211 	 * If we have managed to get rid of all the dirty buffers,
5212 	 * then we are done. For certain directories and block
5213 	 * devices, we may need to do further work.
5214 	 *
5215 	 * We must wait for any I/O in progress to finish so that
5216 	 * all potential buffers on the dirty list will be visible.
5217 	 */
5218 	VI_LOCK(vp);
5219 	drain_output(vp, 1);
5220 	if (TAILQ_FIRST(&vp->v_dirtyblkhd) == NULL) {
5221 		VI_UNLOCK(vp);
5222 		FREE_LOCK(&lk);
5223 		return (0);
5224 	}
5225 	VI_UNLOCK(vp);
5226 
5227 	FREE_LOCK(&lk);
5228 	/*
5229 	 * If we are trying to sync a block device, some of its buffers may
5230 	 * contain metadata that cannot be written until the contents of some
5231 	 * partially written files have been written to disk. The only easy
5232 	 * way to accomplish this is to sync the entire filesystem (luckily
5233 	 * this happens rarely).
5234 	 */
5235 	if (vn_isdisk(vp, NULL) &&
5236 	    vp->v_rdev->si_mountpoint && !VOP_ISLOCKED(vp, NULL) &&
5237 	    (error = VFS_SYNC(vp->v_rdev->si_mountpoint, MNT_WAIT, ap->a_cred,
5238 	     ap->a_td)) != 0)
5239 		return (error);
5240 	return (0);
5241 }
5242 
5243 /*
5244  * Flush the dependencies associated with an inodedep.
5245  * Called with splbio blocked.
5246  */
5247 static int
5248 flush_inodedep_deps(fs, ino)
5249 	struct fs *fs;
5250 	ino_t ino;
5251 {
5252 	struct inodedep *inodedep;
5253 	int error, waitfor;
5254 
5255 	/*
5256 	 * This work is done in two passes. The first pass grabs most
5257 	 * of the buffers and begins asynchronously writing them. The
5258 	 * only way to wait for these asynchronous writes is to sleep
5259 	 * on the filesystem vnode which may stay busy for a long time
5260 	 * if the filesystem is active. So, instead, we make a second
5261 	 * pass over the dependencies blocking on each write. In the
5262 	 * usual case we will be blocking against a write that we
5263 	 * initiated, so when it is done the dependency will have been
5264 	 * resolved. Thus the second pass is expected to end quickly.
5265 	 * We give a brief window at the top of the loop to allow
5266 	 * any pending I/O to complete.
5267 	 */
5268 	for (error = 0, waitfor = MNT_NOWAIT; ; ) {
5269 		if (error)
5270 			return (error);
5271 		FREE_LOCK(&lk);
5272 		ACQUIRE_LOCK(&lk);
5273 		if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
5274 			return (0);
5275 		if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
5276 		    flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
5277 		    flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
5278 		    flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
5279 			continue;
5280 		/*
5281 		 * If pass2, we are done, otherwise do pass 2.
5282 		 */
5283 		if (waitfor == MNT_WAIT)
5284 			break;
5285 		waitfor = MNT_WAIT;
5286 	}
5287 	/*
5288 	 * Try freeing inodedep in case all dependencies have been removed.
5289 	 */
5290 	if (inodedep_lookup(fs, ino, 0, &inodedep) != 0)
5291 		(void) free_inodedep(inodedep);
5292 	return (0);
5293 }
5294 
5295 /*
5296  * Flush an inode dependency list.
5297  * Called with splbio blocked.
5298  */
5299 static int
5300 flush_deplist(listhead, waitfor, errorp)
5301 	struct allocdirectlst *listhead;
5302 	int waitfor;
5303 	int *errorp;
5304 {
5305 	struct allocdirect *adp;
5306 	struct buf *bp;
5307 
5308 	TAILQ_FOREACH(adp, listhead, ad_next) {
5309 		if (adp->ad_state & DEPCOMPLETE)
5310 			continue;
5311 		bp = adp->ad_buf;
5312 		bp = getdirtybuf(&bp, NULL, waitfor);
5313 		if (bp == NULL) {
5314 			if (waitfor == MNT_NOWAIT)
5315 				continue;
5316 			return (1);
5317 		}
5318 		FREE_LOCK(&lk);
5319 		if (waitfor == MNT_NOWAIT) {
5320 			bawrite(bp);
5321 		} else if ((*errorp = bwrite(bp)) != 0) {
5322 			ACQUIRE_LOCK(&lk);
5323 			return (1);
5324 		}
5325 		ACQUIRE_LOCK(&lk);
5326 		return (1);
5327 	}
5328 	return (0);
5329 }
5330 
5331 /*
5332  * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
5333  * Called with splbio blocked.
5334  */
5335 static int
5336 flush_pagedep_deps(pvp, mp, diraddhdp)
5337 	struct vnode *pvp;
5338 	struct mount *mp;
5339 	struct diraddhd *diraddhdp;
5340 {
5341 	struct thread *td = curthread;
5342 	struct inodedep *inodedep;
5343 	struct ufsmount *ump;
5344 	struct diradd *dap;
5345 	struct vnode *vp;
5346 	int error = 0;
5347 	struct buf *bp;
5348 	ino_t inum;
5349 
5350 	ump = VFSTOUFS(mp);
5351 	while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
5352 		/*
5353 		 * Flush ourselves if this directory entry
5354 		 * has a MKDIR_PARENT dependency.
5355 		 */
5356 		if (dap->da_state & MKDIR_PARENT) {
5357 			FREE_LOCK(&lk);
5358 			if ((error = UFS_UPDATE(pvp, 1)) != 0)
5359 				break;
5360 			ACQUIRE_LOCK(&lk);
5361 			/*
5362 			 * If that cleared dependencies, go on to next.
5363 			 */
5364 			if (dap != LIST_FIRST(diraddhdp))
5365 				continue;
5366 			if (dap->da_state & MKDIR_PARENT) {
5367 				FREE_LOCK(&lk);
5368 				panic("flush_pagedep_deps: MKDIR_PARENT");
5369 			}
5370 		}
5371 		/*
5372 		 * A newly allocated directory must have its "." and
5373 		 * ".." entries written out before its name can be
5374 		 * committed in its parent. We do not want or need
5375 		 * the full semantics of a synchronous VOP_FSYNC as
5376 		 * that may end up here again, once for each directory
5377 		 * level in the filesystem. Instead, we push the blocks
5378 		 * and wait for them to clear. We have to fsync twice
5379 		 * because the first call may choose to defer blocks
5380 		 * that still have dependencies, but deferral will
5381 		 * happen at most once.
5382 		 */
5383 		inum = dap->da_newinum;
5384 		if (dap->da_state & MKDIR_BODY) {
5385 			FREE_LOCK(&lk);
5386 			if ((error = VFS_VGET(mp, inum, LK_EXCLUSIVE, &vp)))
5387 				break;
5388 			if ((error=VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td)) ||
5389 			    (error=VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td))) {
5390 				vput(vp);
5391 				break;
5392 			}
5393 			VI_LOCK(vp);
5394 			drain_output(vp, 0);
5395 			VI_UNLOCK(vp);
5396 			vput(vp);
5397 			ACQUIRE_LOCK(&lk);
5398 			/*
5399 			 * If that cleared dependencies, go on to next.
5400 			 */
5401 			if (dap != LIST_FIRST(diraddhdp))
5402 				continue;
5403 			if (dap->da_state & MKDIR_BODY) {
5404 				FREE_LOCK(&lk);
5405 				panic("flush_pagedep_deps: MKDIR_BODY");
5406 			}
5407 		}
5408 		/*
5409 		 * Flush the inode on which the directory entry depends.
5410 		 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
5411 		 * the only remaining dependency is that the updated inode
5412 		 * count must get pushed to disk. The inode has already
5413 		 * been pushed into its inode buffer (via VOP_UPDATE) at
5414 		 * the time of the reference count change. So we need only
5415 		 * locate that buffer, ensure that there will be no rollback
5416 		 * caused by a bitmap dependency, then write the inode buffer.
5417 		 */
5418 		if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0) {
5419 			FREE_LOCK(&lk);
5420 			panic("flush_pagedep_deps: lost inode");
5421 		}
5422 		/*
5423 		 * If the inode still has bitmap dependencies,
5424 		 * push them to disk.
5425 		 */
5426 		if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5427 			bp = inodedep->id_buf;
5428 			bp = getdirtybuf(&bp, NULL, MNT_WAIT);
5429 			FREE_LOCK(&lk);
5430 			if (bp && (error = bwrite(bp)) != 0)
5431 				break;
5432 			ACQUIRE_LOCK(&lk);
5433 			if (dap != LIST_FIRST(diraddhdp))
5434 				continue;
5435 		}
5436 		/*
5437 		 * If the inode is still sitting in a buffer waiting
5438 		 * to be written, push it to disk.
5439 		 */
5440 		FREE_LOCK(&lk);
5441 		if ((error = bread(ump->um_devvp,
5442 		    fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
5443 		    (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) {
5444 			brelse(bp);
5445 			break;
5446 		}
5447 		if ((error = bwrite(bp)) != 0)
5448 			break;
5449 		ACQUIRE_LOCK(&lk);
5450 		/*
5451 		 * If we have failed to get rid of all the dependencies
5452 		 * then something is seriously wrong.
5453 		 */
5454 		if (dap == LIST_FIRST(diraddhdp)) {
5455 			FREE_LOCK(&lk);
5456 			panic("flush_pagedep_deps: flush failed");
5457 		}
5458 	}
5459 	if (error)
5460 		ACQUIRE_LOCK(&lk);
5461 	return (error);
5462 }
5463 
5464 /*
5465  * A large burst of file addition or deletion activity can drive the
5466  * memory load excessively high. First attempt to slow things down
5467  * using the techniques below. If that fails, this routine requests
5468  * the offending operations to fall back to running synchronously
5469  * until the memory load returns to a reasonable level.
5470  */
5471 int
5472 softdep_slowdown(vp)
5473 	struct vnode *vp;
5474 {
5475 	int max_softdeps_hard;
5476 
5477 	max_softdeps_hard = max_softdeps * 11 / 10;
5478 	if (num_dirrem < max_softdeps_hard / 2 &&
5479 	    num_inodedep < max_softdeps_hard &&
5480 	    VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps)
5481   		return (0);
5482 	if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps)
5483 		speedup_syncer();
5484 	stat_sync_limit_hit += 1;
5485 	return (1);
5486 }
5487 
5488 /*
5489  * Called by the allocation routines when they are about to fail
5490  * in the hope that we can free up some disk space.
5491  *
5492  * First check to see if the work list has anything on it. If it has,
5493  * clean up entries until we successfully free some space. Because this
5494  * process holds inodes locked, we cannot handle any remove requests
5495  * that might block on a locked inode as that could lead to deadlock.
5496  * If the worklist yields no free space, encourage the syncer daemon
5497  * to help us. In no event will we try for longer than tickdelay seconds.
5498  */
5499 int
5500 softdep_request_cleanup(fs, vp)
5501 	struct fs *fs;
5502 	struct vnode *vp;
5503 {
5504 	long starttime;
5505 	ufs2_daddr_t needed;
5506 
5507 	needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize;
5508 	starttime = time_second + tickdelay;
5509 	/*
5510 	 * If we are being called because of a process doing a
5511 	 * copy-on-write, then it is not safe to update the vnode
5512 	 * as we may recurse into the copy-on-write routine.
5513 	 */
5514 	if (!(curthread->td_pflags & TDP_COWINPROGRESS) &&
5515 	    UFS_UPDATE(vp, 1) != 0)
5516 		return (0);
5517 	while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) {
5518 		if (time_second > starttime)
5519 			return (0);
5520 		if (num_on_worklist > 0 &&
5521 		    process_worklist_item(NULL, LK_NOWAIT) != -1) {
5522 			stat_worklist_push += 1;
5523 			continue;
5524 		}
5525 		request_cleanup(FLUSH_REMOVE_WAIT, 0);
5526 	}
5527 	return (1);
5528 }
5529 
5530 /*
5531  * If memory utilization has gotten too high, deliberately slow things
5532  * down and speed up the I/O processing.
5533  */
5534 static int
5535 request_cleanup(resource, islocked)
5536 	int resource;
5537 	int islocked;
5538 {
5539 	struct thread *td = curthread;
5540 
5541 	/*
5542 	 * We never hold up the filesystem syncer process.
5543 	 */
5544 	if (td == filesys_syncer)
5545 		return (0);
5546 	/*
5547 	 * First check to see if the work list has gotten backlogged.
5548 	 * If it has, co-opt this process to help clean up two entries.
5549 	 * Because this process may hold inodes locked, we cannot
5550 	 * handle any remove requests that might block on a locked
5551 	 * inode as that could lead to deadlock.
5552 	 */
5553 	if (num_on_worklist > max_softdeps / 10) {
5554 		if (islocked)
5555 			FREE_LOCK(&lk);
5556 		process_worklist_item(NULL, LK_NOWAIT);
5557 		process_worklist_item(NULL, LK_NOWAIT);
5558 		stat_worklist_push += 2;
5559 		if (islocked)
5560 			ACQUIRE_LOCK(&lk);
5561 		return(1);
5562 	}
5563 	/*
5564 	 * Next, we attempt to speed up the syncer process. If that
5565 	 * is successful, then we allow the process to continue.
5566 	 */
5567 	if (speedup_syncer() && resource != FLUSH_REMOVE_WAIT)
5568 		return(0);
5569 	/*
5570 	 * If we are resource constrained on inode dependencies, try
5571 	 * flushing some dirty inodes. Otherwise, we are constrained
5572 	 * by file deletions, so try accelerating flushes of directories
5573 	 * with removal dependencies. We would like to do the cleanup
5574 	 * here, but we probably hold an inode locked at this point and
5575 	 * that might deadlock against one that we try to clean. So,
5576 	 * the best that we can do is request the syncer daemon to do
5577 	 * the cleanup for us.
5578 	 */
5579 	switch (resource) {
5580 
5581 	case FLUSH_INODES:
5582 		stat_ino_limit_push += 1;
5583 		req_clear_inodedeps += 1;
5584 		stat_countp = &stat_ino_limit_hit;
5585 		break;
5586 
5587 	case FLUSH_REMOVE:
5588 	case FLUSH_REMOVE_WAIT:
5589 		stat_blk_limit_push += 1;
5590 		req_clear_remove += 1;
5591 		stat_countp = &stat_blk_limit_hit;
5592 		break;
5593 
5594 	default:
5595 		if (islocked)
5596 			FREE_LOCK(&lk);
5597 		panic("request_cleanup: unknown type");
5598 	}
5599 	/*
5600 	 * Hopefully the syncer daemon will catch up and awaken us.
5601 	 * We wait at most tickdelay before proceeding in any case.
5602 	 */
5603 	if (islocked == 0)
5604 		ACQUIRE_LOCK(&lk);
5605 	proc_waiting += 1;
5606 	if (handle.callout == NULL)
5607 		handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
5608 	interlocked_sleep(&lk, SLEEP, (caddr_t)&proc_waiting, NULL, PPAUSE,
5609 	    "softupdate", 0);
5610 	proc_waiting -= 1;
5611 	if (islocked == 0)
5612 		FREE_LOCK(&lk);
5613 	return (1);
5614 }
5615 
5616 /*
5617  * Awaken processes pausing in request_cleanup and clear proc_waiting
5618  * to indicate that there is no longer a timer running.
5619  */
5620 static void
5621 pause_timer(arg)
5622 	void *arg;
5623 {
5624 
5625 	*stat_countp += 1;
5626 	wakeup_one(&proc_waiting);
5627 	if (proc_waiting > 0)
5628 		handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
5629 	else
5630 		handle.callout = NULL;
5631 }
5632 
5633 /*
5634  * Flush out a directory with at least one removal dependency in an effort to
5635  * reduce the number of dirrem, freefile, and freeblks dependency structures.
5636  */
5637 static void
5638 clear_remove(td)
5639 	struct thread *td;
5640 {
5641 	struct pagedep_hashhead *pagedephd;
5642 	struct pagedep *pagedep;
5643 	static int next = 0;
5644 	struct mount *mp;
5645 	struct vnode *vp;
5646 	int error, cnt;
5647 	ino_t ino;
5648 
5649 	ACQUIRE_LOCK(&lk);
5650 	for (cnt = 0; cnt < pagedep_hash; cnt++) {
5651 		pagedephd = &pagedep_hashtbl[next++];
5652 		if (next >= pagedep_hash)
5653 			next = 0;
5654 		LIST_FOREACH(pagedep, pagedephd, pd_hash) {
5655 			if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL)
5656 				continue;
5657 			mp = pagedep->pd_mnt;
5658 			ino = pagedep->pd_ino;
5659 			if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5660 				continue;
5661 			FREE_LOCK(&lk);
5662 			if ((error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &vp))) {
5663 				softdep_error("clear_remove: vget", error);
5664 				vn_finished_write(mp);
5665 				return;
5666 			}
5667 			if ((error = VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td)))
5668 				softdep_error("clear_remove: fsync", error);
5669 			VI_LOCK(vp);
5670 			drain_output(vp, 0);
5671 			VI_UNLOCK(vp);
5672 			vput(vp);
5673 			vn_finished_write(mp);
5674 			return;
5675 		}
5676 	}
5677 	FREE_LOCK(&lk);
5678 }
5679 
5680 /*
5681  * Clear out a block of dirty inodes in an effort to reduce
5682  * the number of inodedep dependency structures.
5683  */
5684 static void
5685 clear_inodedeps(td)
5686 	struct thread *td;
5687 {
5688 	struct inodedep_hashhead *inodedephd;
5689 	struct inodedep *inodedep;
5690 	static int next = 0;
5691 	struct mount *mp;
5692 	struct vnode *vp;
5693 	struct fs *fs;
5694 	int error, cnt;
5695 	ino_t firstino, lastino, ino;
5696 
5697 	ACQUIRE_LOCK(&lk);
5698 	/*
5699 	 * Pick a random inode dependency to be cleared.
5700 	 * We will then gather up all the inodes in its block
5701 	 * that have dependencies and flush them out.
5702 	 */
5703 	for (cnt = 0; cnt < inodedep_hash; cnt++) {
5704 		inodedephd = &inodedep_hashtbl[next++];
5705 		if (next >= inodedep_hash)
5706 			next = 0;
5707 		if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
5708 			break;
5709 	}
5710 	if (inodedep == NULL)
5711 		return;
5712 	/*
5713 	 * Ugly code to find mount point given pointer to superblock.
5714 	 */
5715 	fs = inodedep->id_fs;
5716 	TAILQ_FOREACH(mp, &mountlist, mnt_list)
5717 		if ((mp->mnt_flag & MNT_SOFTDEP) && fs == VFSTOUFS(mp)->um_fs)
5718 			break;
5719 	/*
5720 	 * Find the last inode in the block with dependencies.
5721 	 */
5722 	firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
5723 	for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
5724 		if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
5725 			break;
5726 	/*
5727 	 * Asynchronously push all but the last inode with dependencies.
5728 	 * Synchronously push the last inode with dependencies to ensure
5729 	 * that the inode block gets written to free up the inodedeps.
5730 	 */
5731 	for (ino = firstino; ino <= lastino; ino++) {
5732 		if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
5733 			continue;
5734 		if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5735 			continue;
5736 		FREE_LOCK(&lk);
5737 		if ((error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &vp)) != 0) {
5738 			softdep_error("clear_inodedeps: vget", error);
5739 			vn_finished_write(mp);
5740 			return;
5741 		}
5742 		if (ino == lastino) {
5743 			if ((error = VOP_FSYNC(vp, td->td_ucred, MNT_WAIT, td)))
5744 				softdep_error("clear_inodedeps: fsync1", error);
5745 		} else {
5746 			if ((error = VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td)))
5747 				softdep_error("clear_inodedeps: fsync2", error);
5748 			VI_LOCK(vp);
5749 			drain_output(vp, 0);
5750 			VI_UNLOCK(vp);
5751 		}
5752 		vput(vp);
5753 		vn_finished_write(mp);
5754 		ACQUIRE_LOCK(&lk);
5755 	}
5756 	FREE_LOCK(&lk);
5757 }
5758 
5759 /*
5760  * Function to determine if the buffer has outstanding dependencies
5761  * that will cause a roll-back if the buffer is written. If wantcount
5762  * is set, return number of dependencies, otherwise just yes or no.
5763  */
5764 static int
5765 softdep_count_dependencies(bp, wantcount)
5766 	struct buf *bp;
5767 	int wantcount;
5768 {
5769 	struct worklist *wk;
5770 	struct inodedep *inodedep;
5771 	struct indirdep *indirdep;
5772 	struct allocindir *aip;
5773 	struct pagedep *pagedep;
5774 	struct diradd *dap;
5775 	int i, retval;
5776 
5777 	retval = 0;
5778 	ACQUIRE_LOCK(&lk);
5779 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5780 		switch (wk->wk_type) {
5781 
5782 		case D_INODEDEP:
5783 			inodedep = WK_INODEDEP(wk);
5784 			if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5785 				/* bitmap allocation dependency */
5786 				retval += 1;
5787 				if (!wantcount)
5788 					goto out;
5789 			}
5790 			if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
5791 				/* direct block pointer dependency */
5792 				retval += 1;
5793 				if (!wantcount)
5794 					goto out;
5795 			}
5796 			if (TAILQ_FIRST(&inodedep->id_extupdt)) {
5797 				/* direct block pointer dependency */
5798 				retval += 1;
5799 				if (!wantcount)
5800 					goto out;
5801 			}
5802 			continue;
5803 
5804 		case D_INDIRDEP:
5805 			indirdep = WK_INDIRDEP(wk);
5806 
5807 			LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
5808 				/* indirect block pointer dependency */
5809 				retval += 1;
5810 				if (!wantcount)
5811 					goto out;
5812 			}
5813 			continue;
5814 
5815 		case D_PAGEDEP:
5816 			pagedep = WK_PAGEDEP(wk);
5817 			for (i = 0; i < DAHASHSZ; i++) {
5818 
5819 				LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
5820 					/* directory entry dependency */
5821 					retval += 1;
5822 					if (!wantcount)
5823 						goto out;
5824 				}
5825 			}
5826 			continue;
5827 
5828 		case D_BMSAFEMAP:
5829 		case D_ALLOCDIRECT:
5830 		case D_ALLOCINDIR:
5831 		case D_MKDIR:
5832 			/* never a dependency on these blocks */
5833 			continue;
5834 
5835 		default:
5836 			FREE_LOCK(&lk);
5837 			panic("softdep_check_for_rollback: Unexpected type %s",
5838 			    TYPENAME(wk->wk_type));
5839 			/* NOTREACHED */
5840 		}
5841 	}
5842 out:
5843 	FREE_LOCK(&lk);
5844 	return retval;
5845 }
5846 
5847 /*
5848  * Acquire exclusive access to a buffer.
5849  * Must be called with splbio blocked.
5850  * Return acquired buffer or NULL on failure.  mtx, if provided, will be
5851  * released on success but held on failure.
5852  */
5853 static struct buf *
5854 getdirtybuf(bpp, mtx, waitfor)
5855 	struct buf **bpp;
5856 	struct mtx *mtx;
5857 	int waitfor;
5858 {
5859 	struct buf *bp;
5860 	int error;
5861 
5862 	/*
5863 	 * XXX This code and the code that calls it need to be reviewed to
5864 	 * verify its use of the vnode interlock.
5865 	 */
5866 
5867 	for (;;) {
5868 		if ((bp = *bpp) == NULL)
5869 			return (0);
5870 		if (bp->b_vp == NULL)
5871 			kdb_backtrace();
5872 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
5873 			if ((bp->b_vflags & BV_BKGRDINPROG) == 0)
5874 				break;
5875 			BUF_UNLOCK(bp);
5876 			if (waitfor != MNT_WAIT)
5877 				return (NULL);
5878 			/*
5879 			 * The mtx argument must be bp->b_vp's mutex in
5880 			 * this case.
5881 			 */
5882 #ifdef	DEBUG_VFS_LOCKS
5883 			if (bp->b_vp->v_type != VCHR)
5884 				ASSERT_VI_LOCKED(bp->b_vp, "getdirtybuf");
5885 #endif
5886 			bp->b_vflags |= BV_BKGRDWAIT;
5887 			interlocked_sleep(&lk, SLEEP, &bp->b_xflags, mtx,
5888 			    PRIBIO, "getbuf", 0);
5889 			continue;
5890 		}
5891 		if (waitfor != MNT_WAIT)
5892 			return (NULL);
5893 		if (mtx) {
5894 			error = interlocked_sleep(&lk, LOCKBUF, bp, mtx,
5895 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 0, 0);
5896 			mtx_lock(mtx);
5897 		} else
5898 			error = interlocked_sleep(&lk, LOCKBUF, bp, NULL,
5899 			    LK_EXCLUSIVE | LK_SLEEPFAIL, 0, 0);
5900 		if (error != ENOLCK) {
5901 			FREE_LOCK(&lk);
5902 			panic("getdirtybuf: inconsistent lock");
5903 		}
5904 	}
5905 	if ((bp->b_flags & B_DELWRI) == 0) {
5906 		BUF_UNLOCK(bp);
5907 		return (NULL);
5908 	}
5909 	if (mtx)
5910 		mtx_unlock(mtx);
5911 	bremfree(bp);
5912 	return (bp);
5913 }
5914 
5915 /*
5916  * Wait for pending output on a vnode to complete.
5917  * Must be called with vnode lock and interlock locked.
5918  */
5919 static void
5920 drain_output(vp, islocked)
5921 	struct vnode *vp;
5922 	int islocked;
5923 {
5924 	ASSERT_VOP_LOCKED(vp, "drain_output");
5925 	ASSERT_VI_LOCKED(vp, "drain_output");
5926 
5927 	if (!islocked)
5928 		ACQUIRE_LOCK(&lk);
5929 	while (vp->v_numoutput) {
5930 		vp->v_iflag |= VI_BWAIT;
5931 		interlocked_sleep(&lk, SLEEP, (caddr_t)&vp->v_numoutput,
5932 		    VI_MTX(vp), PRIBIO + 1, "drainvp", 0);
5933 	}
5934 	if (!islocked)
5935 		FREE_LOCK(&lk);
5936 }
5937 
5938 /*
5939  * Called whenever a buffer that is being invalidated or reallocated
5940  * contains dependencies. This should only happen if an I/O error has
5941  * occurred. The routine is called with the buffer locked.
5942  */
5943 static void
5944 softdep_deallocate_dependencies(bp)
5945 	struct buf *bp;
5946 {
5947 
5948 	if ((bp->b_ioflags & BIO_ERROR) == 0)
5949 		panic("softdep_deallocate_dependencies: dangling deps");
5950 	softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
5951 	panic("softdep_deallocate_dependencies: unrecovered I/O error");
5952 }
5953 
5954 /*
5955  * Function to handle asynchronous write errors in the filesystem.
5956  */
5957 static void
5958 softdep_error(func, error)
5959 	char *func;
5960 	int error;
5961 {
5962 
5963 	/* XXX should do something better! */
5964 	printf("%s: got error %d while accessing filesystem\n", func, error);
5965 }
5966