xref: /freebsd/sys/ufs/ffs/ffs_softdep.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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(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(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(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(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(ip, i_blocks) += freeblks->fb_chkcnt - blocksreleased;
2514 		ip->i_flag |= IN_CHANGE;
2515 		vput(vp);
2516 	}
2517 
2518 #ifdef DIAGNOSTIC
2519 	if (freeblks->fb_chkcnt != blocksreleased &&
2520 	    ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0))
2521 		printf("handle_workitem_freeblocks: block count\n");
2522 	if (allerror)
2523 		softdep_error("handle_workitem_freeblks", allerror);
2524 #endif /* DIAGNOSTIC */
2525 
2526 	WORKITEM_FREE(freeblks, D_FREEBLKS);
2527 }
2528 
2529 /*
2530  * Release blocks associated with the inode ip and stored in the indirect
2531  * block dbn. If level is greater than SINGLE, the block is an indirect block
2532  * and recursive calls to indirtrunc must be used to cleanse other indirect
2533  * blocks.
2534  */
2535 static int
2536 indir_trunc(freeblks, dbn, level, lbn, countp)
2537 	struct freeblks *freeblks;
2538 	ufs2_daddr_t dbn;
2539 	int level;
2540 	ufs_lbn_t lbn;
2541 	ufs2_daddr_t *countp;
2542 {
2543 	struct buf *bp;
2544 	struct fs *fs;
2545 	struct worklist *wk;
2546 	struct indirdep *indirdep;
2547 	ufs1_daddr_t *bap1 = 0;
2548 	ufs2_daddr_t nb, *bap2 = 0;
2549 	ufs_lbn_t lbnadd;
2550 	int i, nblocks, ufs1fmt;
2551 	int error, allerror = 0;
2552 
2553 	fs = VFSTOUFS(freeblks->fb_mnt)->um_fs;
2554 	lbnadd = 1;
2555 	for (i = level; i > 0; i--)
2556 		lbnadd *= NINDIR(fs);
2557 	/*
2558 	 * Get buffer of block pointers to be freed. This routine is not
2559 	 * called until the zero'ed inode has been written, so it is safe
2560 	 * to free blocks as they are encountered. Because the inode has
2561 	 * been zero'ed, calls to bmap on these blocks will fail. So, we
2562 	 * have to use the on-disk address and the block device for the
2563 	 * filesystem to look them up. If the file was deleted before its
2564 	 * indirect blocks were all written to disk, the routine that set
2565 	 * us up (deallocate_dependencies) will have arranged to leave
2566 	 * a complete copy of the indirect block in memory for our use.
2567 	 * Otherwise we have to read the blocks in from the disk.
2568 	 */
2569 #ifdef notyet
2570 	bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0,
2571 	    GB_NOCREAT);
2572 #else
2573 	bp = incore(freeblks->fb_devvp, dbn);
2574 #endif
2575 	ACQUIRE_LOCK(&lk);
2576 	if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2577 		if (wk->wk_type != D_INDIRDEP ||
2578 		    (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2579 		    (indirdep->ir_state & GOINGAWAY) == 0) {
2580 			FREE_LOCK(&lk);
2581 			panic("indir_trunc: lost indirdep");
2582 		}
2583 		WORKLIST_REMOVE(wk);
2584 		WORKITEM_FREE(indirdep, D_INDIRDEP);
2585 		if (LIST_FIRST(&bp->b_dep) != NULL) {
2586 			FREE_LOCK(&lk);
2587 			panic("indir_trunc: dangling dep");
2588 		}
2589 		VFSTOUFS(freeblks->fb_mnt)->um_numindirdeps -= 1;
2590 		FREE_LOCK(&lk);
2591 	} else {
2592 #ifdef notyet
2593 		if (bp)
2594 			brelse(bp);
2595 #endif
2596 		FREE_LOCK(&lk);
2597 		error = bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
2598 		    NOCRED, &bp);
2599 		if (error) {
2600 			brelse(bp);
2601 			return (error);
2602 		}
2603 	}
2604 	/*
2605 	 * Recursively free indirect blocks.
2606 	 */
2607 	if (VFSTOUFS(freeblks->fb_mnt)->um_fstype == UFS1) {
2608 		ufs1fmt = 1;
2609 		bap1 = (ufs1_daddr_t *)bp->b_data;
2610 	} else {
2611 		ufs1fmt = 0;
2612 		bap2 = (ufs2_daddr_t *)bp->b_data;
2613 	}
2614 	nblocks = btodb(fs->fs_bsize);
2615 	for (i = NINDIR(fs) - 1; i >= 0; i--) {
2616 		if (ufs1fmt)
2617 			nb = bap1[i];
2618 		else
2619 			nb = bap2[i];
2620 		if (nb == 0)
2621 			continue;
2622 		if (level != 0) {
2623 			if ((error = indir_trunc(freeblks, fsbtodb(fs, nb),
2624 			     level - 1, lbn + (i * lbnadd), countp)) != 0)
2625 				allerror = error;
2626 		}
2627 		ffs_blkfree(fs, freeblks->fb_devvp, nb, fs->fs_bsize,
2628 		    freeblks->fb_previousinum);
2629 		fs->fs_pendingblocks -= nblocks;
2630 		*countp += nblocks;
2631 	}
2632 	bp->b_flags |= B_INVAL | B_NOCACHE;
2633 	brelse(bp);
2634 	return (allerror);
2635 }
2636 
2637 /*
2638  * Free an allocindir.
2639  * This routine must be called with splbio interrupts blocked.
2640  */
2641 static void
2642 free_allocindir(aip, inodedep)
2643 	struct allocindir *aip;
2644 	struct inodedep *inodedep;
2645 {
2646 	struct freefrag *freefrag;
2647 
2648 #ifdef DEBUG
2649 	if (lk.lkt_held == NOHOLDER)
2650 		panic("free_allocindir: lock not held");
2651 #endif
2652 	if ((aip->ai_state & DEPCOMPLETE) == 0)
2653 		LIST_REMOVE(aip, ai_deps);
2654 	if (aip->ai_state & ONWORKLIST)
2655 		WORKLIST_REMOVE(&aip->ai_list);
2656 	LIST_REMOVE(aip, ai_next);
2657 	if ((freefrag = aip->ai_freefrag) != NULL) {
2658 		if (inodedep == NULL)
2659 			add_to_worklist(&freefrag->ff_list);
2660 		else
2661 			WORKLIST_INSERT(&inodedep->id_bufwait,
2662 			    &freefrag->ff_list);
2663 	}
2664 	WORKITEM_FREE(aip, D_ALLOCINDIR);
2665 }
2666 
2667 /*
2668  * Directory entry addition dependencies.
2669  *
2670  * When adding a new directory entry, the inode (with its incremented link
2671  * count) must be written to disk before the directory entry's pointer to it.
2672  * Also, if the inode is newly allocated, the corresponding freemap must be
2673  * updated (on disk) before the directory entry's pointer. These requirements
2674  * are met via undo/redo on the directory entry's pointer, which consists
2675  * simply of the inode number.
2676  *
2677  * As directory entries are added and deleted, the free space within a
2678  * directory block can become fragmented.  The ufs filesystem will compact
2679  * a fragmented directory block to make space for a new entry. When this
2680  * occurs, the offsets of previously added entries change. Any "diradd"
2681  * dependency structures corresponding to these entries must be updated with
2682  * the new offsets.
2683  */
2684 
2685 /*
2686  * This routine is called after the in-memory inode's link
2687  * count has been incremented, but before the directory entry's
2688  * pointer to the inode has been set.
2689  */
2690 int
2691 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
2692 	struct buf *bp;		/* buffer containing directory block */
2693 	struct inode *dp;	/* inode for directory */
2694 	off_t diroffset;	/* offset of new entry in directory */
2695 	ino_t newinum;		/* inode referenced by new directory entry */
2696 	struct buf *newdirbp;	/* non-NULL => contents of new mkdir */
2697 	int isnewblk;		/* entry is in a newly allocated block */
2698 {
2699 	int offset;		/* offset of new entry within directory block */
2700 	ufs_lbn_t lbn;		/* block in directory containing new entry */
2701 	struct fs *fs;
2702 	struct diradd *dap;
2703 	struct allocdirect *adp;
2704 	struct pagedep *pagedep;
2705 	struct inodedep *inodedep;
2706 	struct newdirblk *newdirblk = 0;
2707 	struct mkdir *mkdir1, *mkdir2;
2708 
2709 	/*
2710 	 * Whiteouts have no dependencies.
2711 	 */
2712 	if (newinum == WINO) {
2713 		if (newdirbp != NULL)
2714 			bdwrite(newdirbp);
2715 		return (0);
2716 	}
2717 
2718 	fs = dp->i_fs;
2719 	lbn = lblkno(fs, diroffset);
2720 	offset = blkoff(fs, diroffset);
2721 	MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2722 		M_SOFTDEP_FLAGS|M_ZERO);
2723 	dap->da_list.wk_type = D_DIRADD;
2724 	dap->da_offset = offset;
2725 	dap->da_newinum = newinum;
2726 	dap->da_state = ATTACHED;
2727 	if (isnewblk && lbn < NDADDR && fragoff(fs, diroffset) == 0) {
2728 		MALLOC(newdirblk, struct newdirblk *, sizeof(struct newdirblk),
2729 		    M_NEWDIRBLK, M_SOFTDEP_FLAGS);
2730 		newdirblk->db_list.wk_type = D_NEWDIRBLK;
2731 		newdirblk->db_state = 0;
2732 	}
2733 	if (newdirbp == NULL) {
2734 		dap->da_state |= DEPCOMPLETE;
2735 		ACQUIRE_LOCK(&lk);
2736 	} else {
2737 		dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
2738 		MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2739 		    M_SOFTDEP_FLAGS);
2740 		mkdir1->md_list.wk_type = D_MKDIR;
2741 		mkdir1->md_state = MKDIR_BODY;
2742 		mkdir1->md_diradd = dap;
2743 		MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2744 		    M_SOFTDEP_FLAGS);
2745 		mkdir2->md_list.wk_type = D_MKDIR;
2746 		mkdir2->md_state = MKDIR_PARENT;
2747 		mkdir2->md_diradd = dap;
2748 		/*
2749 		 * Dependency on "." and ".." being written to disk.
2750 		 */
2751 		mkdir1->md_buf = newdirbp;
2752 		ACQUIRE_LOCK(&lk);
2753 		LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
2754 		WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
2755 		FREE_LOCK(&lk);
2756 		bdwrite(newdirbp);
2757 		/*
2758 		 * Dependency on link count increase for parent directory
2759 		 */
2760 		ACQUIRE_LOCK(&lk);
2761 		if (inodedep_lookup(fs, dp->i_number, 0, &inodedep) == 0
2762 		    || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2763 			dap->da_state &= ~MKDIR_PARENT;
2764 			WORKITEM_FREE(mkdir2, D_MKDIR);
2765 		} else {
2766 			LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
2767 			WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
2768 		}
2769 	}
2770 	/*
2771 	 * Link into parent directory pagedep to await its being written.
2772 	 */
2773 	if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2774 		WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2775 	dap->da_pagedep = pagedep;
2776 	LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
2777 	    da_pdlist);
2778 	/*
2779 	 * Link into its inodedep. Put it on the id_bufwait list if the inode
2780 	 * is not yet written. If it is written, do the post-inode write
2781 	 * processing to put it on the id_pendinghd list.
2782 	 */
2783 	(void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep);
2784 	if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
2785 		diradd_inode_written(dap, inodedep);
2786 	else
2787 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2788 	if (isnewblk) {
2789 		/*
2790 		 * Directories growing into indirect blocks are rare
2791 		 * enough and the frequency of new block allocation
2792 		 * in those cases even more rare, that we choose not
2793 		 * to bother tracking them. Rather we simply force the
2794 		 * new directory entry to disk.
2795 		 */
2796 		if (lbn >= NDADDR) {
2797 			FREE_LOCK(&lk);
2798 			/*
2799 			 * We only have a new allocation when at the
2800 			 * beginning of a new block, not when we are
2801 			 * expanding into an existing block.
2802 			 */
2803 			if (blkoff(fs, diroffset) == 0)
2804 				return (1);
2805 			return (0);
2806 		}
2807 		/*
2808 		 * We only have a new allocation when at the beginning
2809 		 * of a new fragment, not when we are expanding into an
2810 		 * existing fragment. Also, there is nothing to do if we
2811 		 * are already tracking this block.
2812 		 */
2813 		if (fragoff(fs, diroffset) != 0) {
2814 			FREE_LOCK(&lk);
2815 			return (0);
2816 		}
2817 		if ((pagedep->pd_state & NEWBLOCK) != 0) {
2818 			WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2819 			FREE_LOCK(&lk);
2820 			return (0);
2821 		}
2822 		/*
2823 		 * Find our associated allocdirect and have it track us.
2824 		 */
2825 		if (inodedep_lookup(fs, dp->i_number, 0, &inodedep) == 0)
2826 			panic("softdep_setup_directory_add: lost inodedep");
2827 		adp = TAILQ_LAST(&inodedep->id_newinoupdt, allocdirectlst);
2828 		if (adp == NULL || adp->ad_lbn != lbn) {
2829 			FREE_LOCK(&lk);
2830 			panic("softdep_setup_directory_add: lost entry");
2831 		}
2832 		pagedep->pd_state |= NEWBLOCK;
2833 		newdirblk->db_pagedep = pagedep;
2834 		WORKLIST_INSERT(&adp->ad_newdirblk, &newdirblk->db_list);
2835 	}
2836 	FREE_LOCK(&lk);
2837 	return (0);
2838 }
2839 
2840 /*
2841  * This procedure is called to change the offset of a directory
2842  * entry when compacting a directory block which must be owned
2843  * exclusively by the caller. Note that the actual entry movement
2844  * must be done in this procedure to ensure that no I/O completions
2845  * occur while the move is in progress.
2846  */
2847 void
2848 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
2849 	struct inode *dp;	/* inode for directory */
2850 	caddr_t base;		/* address of dp->i_offset */
2851 	caddr_t oldloc;		/* address of old directory location */
2852 	caddr_t newloc;		/* address of new directory location */
2853 	int entrysize;		/* size of directory entry */
2854 {
2855 	int offset, oldoffset, newoffset;
2856 	struct pagedep *pagedep;
2857 	struct diradd *dap;
2858 	ufs_lbn_t lbn;
2859 
2860 	ACQUIRE_LOCK(&lk);
2861 	lbn = lblkno(dp->i_fs, dp->i_offset);
2862 	offset = blkoff(dp->i_fs, dp->i_offset);
2863 	if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
2864 		goto done;
2865 	oldoffset = offset + (oldloc - base);
2866 	newoffset = offset + (newloc - base);
2867 
2868 	LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
2869 		if (dap->da_offset != oldoffset)
2870 			continue;
2871 		dap->da_offset = newoffset;
2872 		if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
2873 			break;
2874 		LIST_REMOVE(dap, da_pdlist);
2875 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
2876 		    dap, da_pdlist);
2877 		break;
2878 	}
2879 	if (dap == NULL) {
2880 
2881 		LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
2882 			if (dap->da_offset == oldoffset) {
2883 				dap->da_offset = newoffset;
2884 				break;
2885 			}
2886 		}
2887 	}
2888 done:
2889 	bcopy(oldloc, newloc, entrysize);
2890 	FREE_LOCK(&lk);
2891 }
2892 
2893 /*
2894  * Free a diradd dependency structure. This routine must be called
2895  * with splbio interrupts blocked.
2896  */
2897 static void
2898 free_diradd(dap)
2899 	struct diradd *dap;
2900 {
2901 	struct dirrem *dirrem;
2902 	struct pagedep *pagedep;
2903 	struct inodedep *inodedep;
2904 	struct mkdir *mkdir, *nextmd;
2905 
2906 #ifdef DEBUG
2907 	if (lk.lkt_held == NOHOLDER)
2908 		panic("free_diradd: lock not held");
2909 #endif
2910 	WORKLIST_REMOVE(&dap->da_list);
2911 	LIST_REMOVE(dap, da_pdlist);
2912 	if ((dap->da_state & DIRCHG) == 0) {
2913 		pagedep = dap->da_pagedep;
2914 	} else {
2915 		dirrem = dap->da_previous;
2916 		pagedep = dirrem->dm_pagedep;
2917 		dirrem->dm_dirinum = pagedep->pd_ino;
2918 		add_to_worklist(&dirrem->dm_list);
2919 	}
2920 	if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum,
2921 	    0, &inodedep) != 0)
2922 		(void) free_inodedep(inodedep);
2923 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2924 		for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
2925 			nextmd = LIST_NEXT(mkdir, md_mkdirs);
2926 			if (mkdir->md_diradd != dap)
2927 				continue;
2928 			dap->da_state &= ~mkdir->md_state;
2929 			WORKLIST_REMOVE(&mkdir->md_list);
2930 			LIST_REMOVE(mkdir, md_mkdirs);
2931 			WORKITEM_FREE(mkdir, D_MKDIR);
2932 		}
2933 		if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2934 			FREE_LOCK(&lk);
2935 			panic("free_diradd: unfound ref");
2936 		}
2937 	}
2938 	WORKITEM_FREE(dap, D_DIRADD);
2939 }
2940 
2941 /*
2942  * Directory entry removal dependencies.
2943  *
2944  * When removing a directory entry, the entry's inode pointer must be
2945  * zero'ed on disk before the corresponding inode's link count is decremented
2946  * (possibly freeing the inode for re-use). This dependency is handled by
2947  * updating the directory entry but delaying the inode count reduction until
2948  * after the directory block has been written to disk. After this point, the
2949  * inode count can be decremented whenever it is convenient.
2950  */
2951 
2952 /*
2953  * This routine should be called immediately after removing
2954  * a directory entry.  The inode's link count should not be
2955  * decremented by the calling procedure -- the soft updates
2956  * code will do this task when it is safe.
2957  */
2958 void
2959 softdep_setup_remove(bp, dp, ip, isrmdir)
2960 	struct buf *bp;		/* buffer containing directory block */
2961 	struct inode *dp;	/* inode for the directory being modified */
2962 	struct inode *ip;	/* inode for directory entry being removed */
2963 	int isrmdir;		/* indicates if doing RMDIR */
2964 {
2965 	struct dirrem *dirrem, *prevdirrem;
2966 
2967 	/*
2968 	 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
2969 	 */
2970 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2971 
2972 	/*
2973 	 * If the COMPLETE flag is clear, then there were no active
2974 	 * entries and we want to roll back to a zeroed entry until
2975 	 * the new inode is committed to disk. If the COMPLETE flag is
2976 	 * set then we have deleted an entry that never made it to
2977 	 * disk. If the entry we deleted resulted from a name change,
2978 	 * then the old name still resides on disk. We cannot delete
2979 	 * its inode (returned to us in prevdirrem) until the zeroed
2980 	 * directory entry gets to disk. The new inode has never been
2981 	 * referenced on the disk, so can be deleted immediately.
2982 	 */
2983 	if ((dirrem->dm_state & COMPLETE) == 0) {
2984 		LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
2985 		    dm_next);
2986 		FREE_LOCK(&lk);
2987 	} else {
2988 		if (prevdirrem != NULL)
2989 			LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
2990 			    prevdirrem, dm_next);
2991 		dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
2992 		FREE_LOCK(&lk);
2993 		handle_workitem_remove(dirrem, NULL);
2994 	}
2995 }
2996 
2997 /*
2998  * Allocate a new dirrem if appropriate and return it along with
2999  * its associated pagedep. Called without a lock, returns with lock.
3000  */
3001 static long num_dirrem;		/* number of dirrem allocated */
3002 static struct dirrem *
3003 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
3004 	struct buf *bp;		/* buffer containing directory block */
3005 	struct inode *dp;	/* inode for the directory being modified */
3006 	struct inode *ip;	/* inode for directory entry being removed */
3007 	int isrmdir;		/* indicates if doing RMDIR */
3008 	struct dirrem **prevdirremp; /* previously referenced inode, if any */
3009 {
3010 	int offset;
3011 	ufs_lbn_t lbn;
3012 	struct diradd *dap;
3013 	struct dirrem *dirrem;
3014 	struct pagedep *pagedep;
3015 
3016 	/*
3017 	 * Whiteouts have no deletion dependencies.
3018 	 */
3019 	if (ip == NULL)
3020 		panic("newdirrem: whiteout");
3021 	/*
3022 	 * If we are over our limit, try to improve the situation.
3023 	 * Limiting the number of dirrem structures will also limit
3024 	 * the number of freefile and freeblks structures.
3025 	 */
3026 	if (num_dirrem > max_softdeps / 2)
3027 		(void) request_cleanup(FLUSH_REMOVE, 0);
3028 	num_dirrem += 1;
3029 	MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
3030 		M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO);
3031 	dirrem->dm_list.wk_type = D_DIRREM;
3032 	dirrem->dm_state = isrmdir ? RMDIR : 0;
3033 	dirrem->dm_mnt = ITOV(ip)->v_mount;
3034 	dirrem->dm_oldinum = ip->i_number;
3035 	*prevdirremp = NULL;
3036 
3037 	ACQUIRE_LOCK(&lk);
3038 	lbn = lblkno(dp->i_fs, dp->i_offset);
3039 	offset = blkoff(dp->i_fs, dp->i_offset);
3040 	if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3041 		WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3042 	dirrem->dm_pagedep = pagedep;
3043 	/*
3044 	 * Check for a diradd dependency for the same directory entry.
3045 	 * If present, then both dependencies become obsolete and can
3046 	 * be de-allocated. Check for an entry on both the pd_dirraddhd
3047 	 * list and the pd_pendinghd list.
3048 	 */
3049 
3050 	LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
3051 		if (dap->da_offset == offset)
3052 			break;
3053 	if (dap == NULL) {
3054 
3055 		LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
3056 			if (dap->da_offset == offset)
3057 				break;
3058 		if (dap == NULL)
3059 			return (dirrem);
3060 	}
3061 	/*
3062 	 * Must be ATTACHED at this point.
3063 	 */
3064 	if ((dap->da_state & ATTACHED) == 0) {
3065 		FREE_LOCK(&lk);
3066 		panic("newdirrem: not ATTACHED");
3067 	}
3068 	if (dap->da_newinum != ip->i_number) {
3069 		FREE_LOCK(&lk);
3070 		panic("newdirrem: inum %d should be %d",
3071 		    ip->i_number, dap->da_newinum);
3072 	}
3073 	/*
3074 	 * If we are deleting a changed name that never made it to disk,
3075 	 * then return the dirrem describing the previous inode (which
3076 	 * represents the inode currently referenced from this entry on disk).
3077 	 */
3078 	if ((dap->da_state & DIRCHG) != 0) {
3079 		*prevdirremp = dap->da_previous;
3080 		dap->da_state &= ~DIRCHG;
3081 		dap->da_pagedep = pagedep;
3082 	}
3083 	/*
3084 	 * We are deleting an entry that never made it to disk.
3085 	 * Mark it COMPLETE so we can delete its inode immediately.
3086 	 */
3087 	dirrem->dm_state |= COMPLETE;
3088 	free_diradd(dap);
3089 	return (dirrem);
3090 }
3091 
3092 /*
3093  * Directory entry change dependencies.
3094  *
3095  * Changing an existing directory entry requires that an add operation
3096  * be completed first followed by a deletion. The semantics for the addition
3097  * are identical to the description of adding a new entry above except
3098  * that the rollback is to the old inode number rather than zero. Once
3099  * the addition dependency is completed, the removal is done as described
3100  * in the removal routine above.
3101  */
3102 
3103 /*
3104  * This routine should be called immediately after changing
3105  * a directory entry.  The inode's link count should not be
3106  * decremented by the calling procedure -- the soft updates
3107  * code will perform this task when it is safe.
3108  */
3109 void
3110 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
3111 	struct buf *bp;		/* buffer containing directory block */
3112 	struct inode *dp;	/* inode for the directory being modified */
3113 	struct inode *ip;	/* inode for directory entry being removed */
3114 	ino_t newinum;		/* new inode number for changed entry */
3115 	int isrmdir;		/* indicates if doing RMDIR */
3116 {
3117 	int offset;
3118 	struct diradd *dap = NULL;
3119 	struct dirrem *dirrem, *prevdirrem;
3120 	struct pagedep *pagedep;
3121 	struct inodedep *inodedep;
3122 
3123 	offset = blkoff(dp->i_fs, dp->i_offset);
3124 
3125 	/*
3126 	 * Whiteouts do not need diradd dependencies.
3127 	 */
3128 	if (newinum != WINO) {
3129 		MALLOC(dap, struct diradd *, sizeof(struct diradd),
3130 		    M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
3131 		dap->da_list.wk_type = D_DIRADD;
3132 		dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
3133 		dap->da_offset = offset;
3134 		dap->da_newinum = newinum;
3135 	}
3136 
3137 	/*
3138 	 * Allocate a new dirrem and ACQUIRE_LOCK.
3139 	 */
3140 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3141 	pagedep = dirrem->dm_pagedep;
3142 	/*
3143 	 * The possible values for isrmdir:
3144 	 *	0 - non-directory file rename
3145 	 *	1 - directory rename within same directory
3146 	 *   inum - directory rename to new directory of given inode number
3147 	 * When renaming to a new directory, we are both deleting and
3148 	 * creating a new directory entry, so the link count on the new
3149 	 * directory should not change. Thus we do not need the followup
3150 	 * dirrem which is usually done in handle_workitem_remove. We set
3151 	 * the DIRCHG flag to tell handle_workitem_remove to skip the
3152 	 * followup dirrem.
3153 	 */
3154 	if (isrmdir > 1)
3155 		dirrem->dm_state |= DIRCHG;
3156 
3157 	/*
3158 	 * Whiteouts have no additional dependencies,
3159 	 * so just put the dirrem on the correct list.
3160 	 */
3161 	if (newinum == WINO) {
3162 		if ((dirrem->dm_state & COMPLETE) == 0) {
3163 			LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
3164 			    dm_next);
3165 		} else {
3166 			dirrem->dm_dirinum = pagedep->pd_ino;
3167 			add_to_worklist(&dirrem->dm_list);
3168 		}
3169 		FREE_LOCK(&lk);
3170 		return;
3171 	}
3172 
3173 	/*
3174 	 * If the COMPLETE flag is clear, then there were no active
3175 	 * entries and we want to roll back to the previous inode until
3176 	 * the new inode is committed to disk. If the COMPLETE flag is
3177 	 * set, then we have deleted an entry that never made it to disk.
3178 	 * If the entry we deleted resulted from a name change, then the old
3179 	 * inode reference still resides on disk. Any rollback that we do
3180 	 * needs to be to that old inode (returned to us in prevdirrem). If
3181 	 * the entry we deleted resulted from a create, then there is
3182 	 * no entry on the disk, so we want to roll back to zero rather
3183 	 * than the uncommitted inode. In either of the COMPLETE cases we
3184 	 * want to immediately free the unwritten and unreferenced inode.
3185 	 */
3186 	if ((dirrem->dm_state & COMPLETE) == 0) {
3187 		dap->da_previous = dirrem;
3188 	} else {
3189 		if (prevdirrem != NULL) {
3190 			dap->da_previous = prevdirrem;
3191 		} else {
3192 			dap->da_state &= ~DIRCHG;
3193 			dap->da_pagedep = pagedep;
3194 		}
3195 		dirrem->dm_dirinum = pagedep->pd_ino;
3196 		add_to_worklist(&dirrem->dm_list);
3197 	}
3198 	/*
3199 	 * Link into its inodedep. Put it on the id_bufwait list if the inode
3200 	 * is not yet written. If it is written, do the post-inode write
3201 	 * processing to put it on the id_pendinghd list.
3202 	 */
3203 	if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 ||
3204 	    (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3205 		dap->da_state |= COMPLETE;
3206 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3207 		WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3208 	} else {
3209 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
3210 		    dap, da_pdlist);
3211 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3212 	}
3213 	FREE_LOCK(&lk);
3214 }
3215 
3216 /*
3217  * Called whenever the link count on an inode is changed.
3218  * It creates an inode dependency so that the new reference(s)
3219  * to the inode cannot be committed to disk until the updated
3220  * inode has been written.
3221  */
3222 void
3223 softdep_change_linkcnt(ip)
3224 	struct inode *ip;	/* the inode with the increased link count */
3225 {
3226 	struct inodedep *inodedep;
3227 
3228 	ACQUIRE_LOCK(&lk);
3229 	(void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep);
3230 	if (ip->i_nlink < ip->i_effnlink) {
3231 		FREE_LOCK(&lk);
3232 		panic("softdep_change_linkcnt: bad delta");
3233 	}
3234 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3235 	FREE_LOCK(&lk);
3236 }
3237 
3238 /*
3239  * Called when the effective link count and the reference count
3240  * on an inode drops to zero. At this point there are no names
3241  * referencing the file in the filesystem and no active file
3242  * references. The space associated with the file will be freed
3243  * as soon as the necessary soft dependencies are cleared.
3244  */
3245 void
3246 softdep_releasefile(ip)
3247 	struct inode *ip;	/* inode with the zero effective link count */
3248 {
3249 	struct inodedep *inodedep;
3250 	struct fs *fs;
3251 	int extblocks;
3252 
3253 	if (ip->i_effnlink > 0)
3254 		panic("softdep_filerelease: file still referenced");
3255 	/*
3256 	 * We may be called several times as the real reference count
3257 	 * drops to zero. We only want to account for the space once.
3258 	 */
3259 	if (ip->i_flag & IN_SPACECOUNTED)
3260 		return;
3261 	/*
3262 	 * We have to deactivate a snapshot otherwise copyonwrites may
3263 	 * add blocks and the cleanup may remove blocks after we have
3264 	 * tried to account for them.
3265 	 */
3266 	if ((ip->i_flags & SF_SNAPSHOT) != 0)
3267 		ffs_snapremove(ITOV(ip));
3268 	/*
3269 	 * If we are tracking an nlinkdelta, we have to also remember
3270 	 * whether we accounted for the freed space yet.
3271 	 */
3272 	ACQUIRE_LOCK(&lk);
3273 	if ((inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep)))
3274 		inodedep->id_state |= SPACECOUNTED;
3275 	FREE_LOCK(&lk);
3276 	fs = ip->i_fs;
3277 	extblocks = 0;
3278 	if (fs->fs_magic == FS_UFS2_MAGIC)
3279 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
3280 	ip->i_fs->fs_pendingblocks += DIP(ip, i_blocks) - extblocks;
3281 	ip->i_fs->fs_pendinginodes += 1;
3282 	ip->i_flag |= IN_SPACECOUNTED;
3283 }
3284 
3285 /*
3286  * This workitem decrements the inode's link count.
3287  * If the link count reaches zero, the file is removed.
3288  */
3289 static void
3290 handle_workitem_remove(dirrem, xp)
3291 	struct dirrem *dirrem;
3292 	struct vnode *xp;
3293 {
3294 	struct thread *td = curthread;
3295 	struct inodedep *inodedep;
3296 	struct vnode *vp;
3297 	struct inode *ip;
3298 	ino_t oldinum;
3299 	int error;
3300 
3301 	if ((vp = xp) == NULL &&
3302 	    (error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, LK_EXCLUSIVE,
3303 	     &vp)) != 0) {
3304 		softdep_error("handle_workitem_remove: vget", error);
3305 		return;
3306 	}
3307 	ip = VTOI(vp);
3308 	ACQUIRE_LOCK(&lk);
3309 	if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){
3310 		FREE_LOCK(&lk);
3311 		panic("handle_workitem_remove: lost inodedep");
3312 	}
3313 	/*
3314 	 * Normal file deletion.
3315 	 */
3316 	if ((dirrem->dm_state & RMDIR) == 0) {
3317 		ip->i_nlink--;
3318 		DIP(ip, i_nlink) = ip->i_nlink;
3319 		ip->i_flag |= IN_CHANGE;
3320 		if (ip->i_nlink < ip->i_effnlink) {
3321 			FREE_LOCK(&lk);
3322 			panic("handle_workitem_remove: bad file delta");
3323 		}
3324 		inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3325 		FREE_LOCK(&lk);
3326 		vput(vp);
3327 		num_dirrem -= 1;
3328 		WORKITEM_FREE(dirrem, D_DIRREM);
3329 		return;
3330 	}
3331 	/*
3332 	 * Directory deletion. Decrement reference count for both the
3333 	 * just deleted parent directory entry and the reference for ".".
3334 	 * Next truncate the directory to length zero. When the
3335 	 * truncation completes, arrange to have the reference count on
3336 	 * the parent decremented to account for the loss of "..".
3337 	 */
3338 	ip->i_nlink -= 2;
3339 	DIP(ip, i_nlink) = ip->i_nlink;
3340 	ip->i_flag |= IN_CHANGE;
3341 	if (ip->i_nlink < ip->i_effnlink) {
3342 		FREE_LOCK(&lk);
3343 		panic("handle_workitem_remove: bad dir delta");
3344 	}
3345 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3346 	FREE_LOCK(&lk);
3347 	if ((error = UFS_TRUNCATE(vp, (off_t)0, 0, td->td_ucred, td)) != 0)
3348 		softdep_error("handle_workitem_remove: truncate", error);
3349 	/*
3350 	 * Rename a directory to a new parent. Since, we are both deleting
3351 	 * and creating a new directory entry, the link count on the new
3352 	 * directory should not change. Thus we skip the followup dirrem.
3353 	 */
3354 	if (dirrem->dm_state & DIRCHG) {
3355 		vput(vp);
3356 		num_dirrem -= 1;
3357 		WORKITEM_FREE(dirrem, D_DIRREM);
3358 		return;
3359 	}
3360 	/*
3361 	 * If the inodedep does not exist, then the zero'ed inode has
3362 	 * been written to disk. If the allocated inode has never been
3363 	 * written to disk, then the on-disk inode is zero'ed. In either
3364 	 * case we can remove the file immediately.
3365 	 */
3366 	ACQUIRE_LOCK(&lk);
3367 	dirrem->dm_state = 0;
3368 	oldinum = dirrem->dm_oldinum;
3369 	dirrem->dm_oldinum = dirrem->dm_dirinum;
3370 	if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 ||
3371 	    check_inode_unwritten(inodedep)) {
3372 		FREE_LOCK(&lk);
3373 		vput(vp);
3374 		handle_workitem_remove(dirrem, NULL);
3375 		return;
3376 	}
3377 	WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
3378 	FREE_LOCK(&lk);
3379 	vput(vp);
3380 }
3381 
3382 /*
3383  * Inode de-allocation dependencies.
3384  *
3385  * When an inode's link count is reduced to zero, it can be de-allocated. We
3386  * found it convenient to postpone de-allocation until after the inode is
3387  * written to disk with its new link count (zero).  At this point, all of the
3388  * on-disk inode's block pointers are nullified and, with careful dependency
3389  * list ordering, all dependencies related to the inode will be satisfied and
3390  * the corresponding dependency structures de-allocated.  So, if/when the
3391  * inode is reused, there will be no mixing of old dependencies with new
3392  * ones.  This artificial dependency is set up by the block de-allocation
3393  * procedure above (softdep_setup_freeblocks) and completed by the
3394  * following procedure.
3395  */
3396 static void
3397 handle_workitem_freefile(freefile)
3398 	struct freefile *freefile;
3399 {
3400 	struct fs *fs;
3401 	struct inodedep *idp;
3402 	int error;
3403 
3404 	fs = VFSTOUFS(freefile->fx_mnt)->um_fs;
3405 #ifdef DEBUG
3406 	ACQUIRE_LOCK(&lk);
3407 	error = inodedep_lookup(fs, freefile->fx_oldinum, 0, &idp);
3408 	FREE_LOCK(&lk);
3409 	if (error)
3410 		panic("handle_workitem_freefile: inodedep survived");
3411 #endif
3412 	fs->fs_pendinginodes -= 1;
3413 	if ((error = ffs_freefile(fs, freefile->fx_devvp, freefile->fx_oldinum,
3414 	     freefile->fx_mode)) != 0)
3415 		softdep_error("handle_workitem_freefile", error);
3416 	WORKITEM_FREE(freefile, D_FREEFILE);
3417 }
3418 
3419 static int
3420 softdep_disk_prewrite(struct vnode *vp, struct buf *bp)
3421 {
3422 	int error;
3423 
3424 	if (bp->b_iocmd != BIO_WRITE)
3425 		return (0);
3426 	if ((bp->b_flags & B_VALIDSUSPWRT) == 0 &&
3427 	    bp->b_vp != NULL && bp->b_vp->v_mount != NULL &&
3428 	    (bp->b_vp->v_mount->mnt_kern_flag & MNTK_SUSPENDED) != 0)
3429 		panic("softdep_disk_prewrite: bad I/O");
3430 	bp->b_flags &= ~B_VALIDSUSPWRT;
3431 	if (LIST_FIRST(&bp->b_dep) != NULL)
3432 		buf_start(bp);
3433 	mp_fixme("This should require the vnode lock.");
3434 	if ((vp->v_vflag & VV_COPYONWRITE) &&
3435 	    vp->v_rdev->si_copyonwrite &&
3436 	    (error = (*vp->v_rdev->si_copyonwrite)(vp, bp)) != 0 &&
3437 	    error != EOPNOTSUPP) {
3438 		bp->b_io.bio_error = error;
3439 		bp->b_io.bio_flags |= BIO_ERROR;
3440 		biodone(&bp->b_io);
3441 		return (1);
3442 	}
3443 	return (0);
3444 }
3445 
3446 
3447 /*
3448  * Disk writes.
3449  *
3450  * The dependency structures constructed above are most actively used when file
3451  * system blocks are written to disk.  No constraints are placed on when a
3452  * block can be written, but unsatisfied update dependencies are made safe by
3453  * modifying (or replacing) the source memory for the duration of the disk
3454  * write.  When the disk write completes, the memory block is again brought
3455  * up-to-date.
3456  *
3457  * In-core inode structure reclamation.
3458  *
3459  * Because there are a finite number of "in-core" inode structures, they are
3460  * reused regularly.  By transferring all inode-related dependencies to the
3461  * in-memory inode block and indexing them separately (via "inodedep"s), we
3462  * can allow "in-core" inode structures to be reused at any time and avoid
3463  * any increase in contention.
3464  *
3465  * Called just before entering the device driver to initiate a new disk I/O.
3466  * The buffer must be locked, thus, no I/O completion operations can occur
3467  * while we are manipulating its associated dependencies.
3468  */
3469 static void
3470 softdep_disk_io_initiation(bp)
3471 	struct buf *bp;		/* structure describing disk write to occur */
3472 {
3473 	struct worklist *wk, *nextwk;
3474 	struct indirdep *indirdep;
3475 	struct inodedep *inodedep;
3476 
3477 	/*
3478 	 * We only care about write operations. There should never
3479 	 * be dependencies for reads.
3480 	 */
3481 	if (bp->b_iocmd == BIO_READ)
3482 		panic("softdep_disk_io_initiation: read");
3483 	/*
3484 	 * Do any necessary pre-I/O processing.
3485 	 */
3486 	for (wk = LIST_FIRST(&bp->b_dep); wk; wk = nextwk) {
3487 		nextwk = LIST_NEXT(wk, wk_list);
3488 		switch (wk->wk_type) {
3489 
3490 		case D_PAGEDEP:
3491 			initiate_write_filepage(WK_PAGEDEP(wk), bp);
3492 			continue;
3493 
3494 		case D_INODEDEP:
3495 			inodedep = WK_INODEDEP(wk);
3496 			if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
3497 				initiate_write_inodeblock_ufs1(inodedep, bp);
3498 			else
3499 				initiate_write_inodeblock_ufs2(inodedep, bp);
3500 			continue;
3501 
3502 		case D_INDIRDEP:
3503 			indirdep = WK_INDIRDEP(wk);
3504 			if (indirdep->ir_state & GOINGAWAY)
3505 				panic("disk_io_initiation: indirdep gone");
3506 			/*
3507 			 * If there are no remaining dependencies, this
3508 			 * will be writing the real pointers, so the
3509 			 * dependency can be freed.
3510 			 */
3511 			if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) {
3512 				indirdep->ir_savebp->b_flags |=
3513 				    B_INVAL | B_NOCACHE;
3514 				brelse(indirdep->ir_savebp);
3515 				/* inline expand WORKLIST_REMOVE(wk); */
3516 				wk->wk_state &= ~ONWORKLIST;
3517 				LIST_REMOVE(wk, wk_list);
3518 				WORKITEM_FREE(indirdep, D_INDIRDEP);
3519 				continue;
3520 			}
3521 			/*
3522 			 * Replace up-to-date version with safe version.
3523 			 */
3524 			MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
3525 			    M_INDIRDEP, M_SOFTDEP_FLAGS);
3526 			ACQUIRE_LOCK(&lk);
3527 			indirdep->ir_state &= ~ATTACHED;
3528 			indirdep->ir_state |= UNDONE;
3529 			bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3530 			bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3531 			    bp->b_bcount);
3532 			FREE_LOCK(&lk);
3533 			continue;
3534 
3535 		case D_MKDIR:
3536 		case D_BMSAFEMAP:
3537 		case D_ALLOCDIRECT:
3538 		case D_ALLOCINDIR:
3539 			continue;
3540 
3541 		default:
3542 			panic("handle_disk_io_initiation: Unexpected type %s",
3543 			    TYPENAME(wk->wk_type));
3544 			/* NOTREACHED */
3545 		}
3546 	}
3547 }
3548 
3549 /*
3550  * Called from within the procedure above to deal with unsatisfied
3551  * allocation dependencies in a directory. The buffer must be locked,
3552  * thus, no I/O completion operations can occur while we are
3553  * manipulating its associated dependencies.
3554  */
3555 static void
3556 initiate_write_filepage(pagedep, bp)
3557 	struct pagedep *pagedep;
3558 	struct buf *bp;
3559 {
3560 	struct diradd *dap;
3561 	struct direct *ep;
3562 	int i;
3563 
3564 	if (pagedep->pd_state & IOSTARTED) {
3565 		/*
3566 		 * This can only happen if there is a driver that does not
3567 		 * understand chaining. Here biodone will reissue the call
3568 		 * to strategy for the incomplete buffers.
3569 		 */
3570 		printf("initiate_write_filepage: already started\n");
3571 		return;
3572 	}
3573 	pagedep->pd_state |= IOSTARTED;
3574 	ACQUIRE_LOCK(&lk);
3575 	for (i = 0; i < DAHASHSZ; i++) {
3576 		LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3577 			ep = (struct direct *)
3578 			    ((char *)bp->b_data + dap->da_offset);
3579 			if (ep->d_ino != dap->da_newinum) {
3580 				FREE_LOCK(&lk);
3581 				panic("%s: dir inum %d != new %d",
3582 				    "initiate_write_filepage",
3583 				    ep->d_ino, dap->da_newinum);
3584 			}
3585 			if (dap->da_state & DIRCHG)
3586 				ep->d_ino = dap->da_previous->dm_oldinum;
3587 			else
3588 				ep->d_ino = 0;
3589 			dap->da_state &= ~ATTACHED;
3590 			dap->da_state |= UNDONE;
3591 		}
3592 	}
3593 	FREE_LOCK(&lk);
3594 }
3595 
3596 /*
3597  * Version of initiate_write_inodeblock that handles UFS1 dinodes.
3598  * Note that any bug fixes made to this routine must be done in the
3599  * version found below.
3600  *
3601  * Called from within the procedure above to deal with unsatisfied
3602  * allocation dependencies in an inodeblock. The buffer must be
3603  * locked, thus, no I/O completion operations can occur while we
3604  * are manipulating its associated dependencies.
3605  */
3606 static void
3607 initiate_write_inodeblock_ufs1(inodedep, bp)
3608 	struct inodedep *inodedep;
3609 	struct buf *bp;			/* The inode block */
3610 {
3611 	struct allocdirect *adp, *lastadp;
3612 	struct ufs1_dinode *dp;
3613 	struct fs *fs;
3614 	ufs_lbn_t i, prevlbn = 0;
3615 	int deplist;
3616 
3617 	if (inodedep->id_state & IOSTARTED)
3618 		panic("initiate_write_inodeblock_ufs1: already started");
3619 	inodedep->id_state |= IOSTARTED;
3620 	fs = inodedep->id_fs;
3621 	dp = (struct ufs1_dinode *)bp->b_data +
3622 	    ino_to_fsbo(fs, inodedep->id_ino);
3623 	/*
3624 	 * If the bitmap is not yet written, then the allocated
3625 	 * inode cannot be written to disk.
3626 	 */
3627 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3628 		if (inodedep->id_savedino1 != NULL)
3629 			panic("initiate_write_inodeblock_ufs1: I/O underway");
3630 		MALLOC(inodedep->id_savedino1, struct ufs1_dinode *,
3631 		    sizeof(struct ufs1_dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3632 		*inodedep->id_savedino1 = *dp;
3633 		bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
3634 		return;
3635 	}
3636 	/*
3637 	 * If no dependencies, then there is nothing to roll back.
3638 	 */
3639 	inodedep->id_savedsize = dp->di_size;
3640 	inodedep->id_savedextsize = 0;
3641 	if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL)
3642 		return;
3643 	/*
3644 	 * Set the dependencies to busy.
3645 	 */
3646 	ACQUIRE_LOCK(&lk);
3647 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3648 	     adp = TAILQ_NEXT(adp, ad_next)) {
3649 #ifdef DIAGNOSTIC
3650 		if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3651 			FREE_LOCK(&lk);
3652 			panic("softdep_write_inodeblock: lbn order");
3653 		}
3654 		prevlbn = adp->ad_lbn;
3655 		if (adp->ad_lbn < NDADDR &&
3656 		    dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3657 			FREE_LOCK(&lk);
3658 			panic("%s: direct pointer #%jd mismatch %d != %jd",
3659 			    "softdep_write_inodeblock",
3660 			    (intmax_t)adp->ad_lbn,
3661 			    dp->di_db[adp->ad_lbn],
3662 			    (intmax_t)adp->ad_newblkno);
3663 		}
3664 		if (adp->ad_lbn >= NDADDR &&
3665 		    dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3666 			FREE_LOCK(&lk);
3667 			panic("%s: indirect pointer #%jd mismatch %d != %jd",
3668 			    "softdep_write_inodeblock",
3669 			    (intmax_t)adp->ad_lbn - NDADDR,
3670 			    dp->di_ib[adp->ad_lbn - NDADDR],
3671 			    (intmax_t)adp->ad_newblkno);
3672 		}
3673 		deplist |= 1 << adp->ad_lbn;
3674 		if ((adp->ad_state & ATTACHED) == 0) {
3675 			FREE_LOCK(&lk);
3676 			panic("softdep_write_inodeblock: Unknown state 0x%x",
3677 			    adp->ad_state);
3678 		}
3679 #endif /* DIAGNOSTIC */
3680 		adp->ad_state &= ~ATTACHED;
3681 		adp->ad_state |= UNDONE;
3682 	}
3683 	/*
3684 	 * The on-disk inode cannot claim to be any larger than the last
3685 	 * fragment that has been written. Otherwise, the on-disk inode
3686 	 * might have fragments that were not the last block in the file
3687 	 * which would corrupt the filesystem.
3688 	 */
3689 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3690 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3691 		if (adp->ad_lbn >= NDADDR)
3692 			break;
3693 		dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3694 		/* keep going until hitting a rollback to a frag */
3695 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3696 			continue;
3697 		dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3698 		for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3699 #ifdef DIAGNOSTIC
3700 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3701 				FREE_LOCK(&lk);
3702 				panic("softdep_write_inodeblock: lost dep1");
3703 			}
3704 #endif /* DIAGNOSTIC */
3705 			dp->di_db[i] = 0;
3706 		}
3707 		for (i = 0; i < NIADDR; i++) {
3708 #ifdef DIAGNOSTIC
3709 			if (dp->di_ib[i] != 0 &&
3710 			    (deplist & ((1 << NDADDR) << i)) == 0) {
3711 				FREE_LOCK(&lk);
3712 				panic("softdep_write_inodeblock: lost dep2");
3713 			}
3714 #endif /* DIAGNOSTIC */
3715 			dp->di_ib[i] = 0;
3716 		}
3717 		FREE_LOCK(&lk);
3718 		return;
3719 	}
3720 	/*
3721 	 * If we have zero'ed out the last allocated block of the file,
3722 	 * roll back the size to the last currently allocated block.
3723 	 * We know that this last allocated block is a full-sized as
3724 	 * we already checked for fragments in the loop above.
3725 	 */
3726 	if (lastadp != NULL &&
3727 	    dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3728 		for (i = lastadp->ad_lbn; i >= 0; i--)
3729 			if (dp->di_db[i] != 0)
3730 				break;
3731 		dp->di_size = (i + 1) * fs->fs_bsize;
3732 	}
3733 	/*
3734 	 * The only dependencies are for indirect blocks.
3735 	 *
3736 	 * The file size for indirect block additions is not guaranteed.
3737 	 * Such a guarantee would be non-trivial to achieve. The conventional
3738 	 * synchronous write implementation also does not make this guarantee.
3739 	 * Fsck should catch and fix discrepancies. Arguably, the file size
3740 	 * can be over-estimated without destroying integrity when the file
3741 	 * moves into the indirect blocks (i.e., is large). If we want to
3742 	 * postpone fsck, we are stuck with this argument.
3743 	 */
3744 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3745 		dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3746 	FREE_LOCK(&lk);
3747 }
3748 
3749 /*
3750  * Version of initiate_write_inodeblock that handles UFS2 dinodes.
3751  * Note that any bug fixes made to this routine must be done in the
3752  * version found above.
3753  *
3754  * Called from within the procedure above to deal with unsatisfied
3755  * allocation dependencies in an inodeblock. The buffer must be
3756  * locked, thus, no I/O completion operations can occur while we
3757  * are manipulating its associated dependencies.
3758  */
3759 static void
3760 initiate_write_inodeblock_ufs2(inodedep, bp)
3761 	struct inodedep *inodedep;
3762 	struct buf *bp;			/* The inode block */
3763 {
3764 	struct allocdirect *adp, *lastadp;
3765 	struct ufs2_dinode *dp;
3766 	struct fs *fs;
3767 	ufs_lbn_t i, prevlbn = 0;
3768 	int deplist;
3769 
3770 	if (inodedep->id_state & IOSTARTED)
3771 		panic("initiate_write_inodeblock_ufs2: already started");
3772 	inodedep->id_state |= IOSTARTED;
3773 	fs = inodedep->id_fs;
3774 	dp = (struct ufs2_dinode *)bp->b_data +
3775 	    ino_to_fsbo(fs, inodedep->id_ino);
3776 	/*
3777 	 * If the bitmap is not yet written, then the allocated
3778 	 * inode cannot be written to disk.
3779 	 */
3780 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3781 		if (inodedep->id_savedino2 != NULL)
3782 			panic("initiate_write_inodeblock_ufs2: I/O underway");
3783 		MALLOC(inodedep->id_savedino2, struct ufs2_dinode *,
3784 		    sizeof(struct ufs2_dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3785 		*inodedep->id_savedino2 = *dp;
3786 		bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
3787 		return;
3788 	}
3789 	/*
3790 	 * If no dependencies, then there is nothing to roll back.
3791 	 */
3792 	inodedep->id_savedsize = dp->di_size;
3793 	inodedep->id_savedextsize = dp->di_extsize;
3794 	if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL &&
3795 	    TAILQ_FIRST(&inodedep->id_extupdt) == NULL)
3796 		return;
3797 	/*
3798 	 * Set the ext data dependencies to busy.
3799 	 */
3800 	ACQUIRE_LOCK(&lk);
3801 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
3802 	     adp = TAILQ_NEXT(adp, ad_next)) {
3803 #ifdef DIAGNOSTIC
3804 		if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3805 			FREE_LOCK(&lk);
3806 			panic("softdep_write_inodeblock: lbn order");
3807 		}
3808 		prevlbn = adp->ad_lbn;
3809 		if (dp->di_extb[adp->ad_lbn] != adp->ad_newblkno) {
3810 			FREE_LOCK(&lk);
3811 			panic("%s: direct pointer #%jd mismatch %jd != %jd",
3812 			    "softdep_write_inodeblock",
3813 			    (intmax_t)adp->ad_lbn,
3814 			    (intmax_t)dp->di_extb[adp->ad_lbn],
3815 			    (intmax_t)adp->ad_newblkno);
3816 		}
3817 		deplist |= 1 << adp->ad_lbn;
3818 		if ((adp->ad_state & ATTACHED) == 0) {
3819 			FREE_LOCK(&lk);
3820 			panic("softdep_write_inodeblock: Unknown state 0x%x",
3821 			    adp->ad_state);
3822 		}
3823 #endif /* DIAGNOSTIC */
3824 		adp->ad_state &= ~ATTACHED;
3825 		adp->ad_state |= UNDONE;
3826 	}
3827 	/*
3828 	 * The on-disk inode cannot claim to be any larger than the last
3829 	 * fragment that has been written. Otherwise, the on-disk inode
3830 	 * might have fragments that were not the last block in the ext
3831 	 * data which would corrupt the filesystem.
3832 	 */
3833 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
3834 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3835 		dp->di_extb[adp->ad_lbn] = adp->ad_oldblkno;
3836 		/* keep going until hitting a rollback to a frag */
3837 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3838 			continue;
3839 		dp->di_extsize = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3840 		for (i = adp->ad_lbn + 1; i < NXADDR; i++) {
3841 #ifdef DIAGNOSTIC
3842 			if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) {
3843 				FREE_LOCK(&lk);
3844 				panic("softdep_write_inodeblock: lost dep1");
3845 			}
3846 #endif /* DIAGNOSTIC */
3847 			dp->di_extb[i] = 0;
3848 		}
3849 		lastadp = NULL;
3850 		break;
3851 	}
3852 	/*
3853 	 * If we have zero'ed out the last allocated block of the ext
3854 	 * data, roll back the size to the last currently allocated block.
3855 	 * We know that this last allocated block is a full-sized as
3856 	 * we already checked for fragments in the loop above.
3857 	 */
3858 	if (lastadp != NULL &&
3859 	    dp->di_extsize <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3860 		for (i = lastadp->ad_lbn; i >= 0; i--)
3861 			if (dp->di_extb[i] != 0)
3862 				break;
3863 		dp->di_extsize = (i + 1) * fs->fs_bsize;
3864 	}
3865 	/*
3866 	 * Set the file data dependencies to busy.
3867 	 */
3868 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3869 	     adp = TAILQ_NEXT(adp, ad_next)) {
3870 #ifdef DIAGNOSTIC
3871 		if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3872 			FREE_LOCK(&lk);
3873 			panic("softdep_write_inodeblock: lbn order");
3874 		}
3875 		prevlbn = adp->ad_lbn;
3876 		if (adp->ad_lbn < NDADDR &&
3877 		    dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3878 			FREE_LOCK(&lk);
3879 			panic("%s: direct pointer #%jd mismatch %jd != %jd",
3880 			    "softdep_write_inodeblock",
3881 			    (intmax_t)adp->ad_lbn,
3882 			    (intmax_t)dp->di_db[adp->ad_lbn],
3883 			    (intmax_t)adp->ad_newblkno);
3884 		}
3885 		if (adp->ad_lbn >= NDADDR &&
3886 		    dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3887 			FREE_LOCK(&lk);
3888 			panic("%s indirect pointer #%jd mismatch %jd != %jd",
3889 			    "softdep_write_inodeblock:",
3890 			    (intmax_t)adp->ad_lbn - NDADDR,
3891 			    (intmax_t)dp->di_ib[adp->ad_lbn - NDADDR],
3892 			    (intmax_t)adp->ad_newblkno);
3893 		}
3894 		deplist |= 1 << adp->ad_lbn;
3895 		if ((adp->ad_state & ATTACHED) == 0) {
3896 			FREE_LOCK(&lk);
3897 			panic("softdep_write_inodeblock: Unknown state 0x%x",
3898 			    adp->ad_state);
3899 		}
3900 #endif /* DIAGNOSTIC */
3901 		adp->ad_state &= ~ATTACHED;
3902 		adp->ad_state |= UNDONE;
3903 	}
3904 	/*
3905 	 * The on-disk inode cannot claim to be any larger than the last
3906 	 * fragment that has been written. Otherwise, the on-disk inode
3907 	 * might have fragments that were not the last block in the file
3908 	 * which would corrupt the filesystem.
3909 	 */
3910 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3911 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3912 		if (adp->ad_lbn >= NDADDR)
3913 			break;
3914 		dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3915 		/* keep going until hitting a rollback to a frag */
3916 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3917 			continue;
3918 		dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3919 		for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3920 #ifdef DIAGNOSTIC
3921 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3922 				FREE_LOCK(&lk);
3923 				panic("softdep_write_inodeblock: lost dep2");
3924 			}
3925 #endif /* DIAGNOSTIC */
3926 			dp->di_db[i] = 0;
3927 		}
3928 		for (i = 0; i < NIADDR; i++) {
3929 #ifdef DIAGNOSTIC
3930 			if (dp->di_ib[i] != 0 &&
3931 			    (deplist & ((1 << NDADDR) << i)) == 0) {
3932 				FREE_LOCK(&lk);
3933 				panic("softdep_write_inodeblock: lost dep3");
3934 			}
3935 #endif /* DIAGNOSTIC */
3936 			dp->di_ib[i] = 0;
3937 		}
3938 		FREE_LOCK(&lk);
3939 		return;
3940 	}
3941 	/*
3942 	 * If we have zero'ed out the last allocated block of the file,
3943 	 * roll back the size to the last currently allocated block.
3944 	 * We know that this last allocated block is a full-sized as
3945 	 * we already checked for fragments in the loop above.
3946 	 */
3947 	if (lastadp != NULL &&
3948 	    dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3949 		for (i = lastadp->ad_lbn; i >= 0; i--)
3950 			if (dp->di_db[i] != 0)
3951 				break;
3952 		dp->di_size = (i + 1) * fs->fs_bsize;
3953 	}
3954 	/*
3955 	 * The only dependencies are for indirect blocks.
3956 	 *
3957 	 * The file size for indirect block additions is not guaranteed.
3958 	 * Such a guarantee would be non-trivial to achieve. The conventional
3959 	 * synchronous write implementation also does not make this guarantee.
3960 	 * Fsck should catch and fix discrepancies. Arguably, the file size
3961 	 * can be over-estimated without destroying integrity when the file
3962 	 * moves into the indirect blocks (i.e., is large). If we want to
3963 	 * postpone fsck, we are stuck with this argument.
3964 	 */
3965 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3966 		dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3967 	FREE_LOCK(&lk);
3968 }
3969 
3970 /*
3971  * This routine is called during the completion interrupt
3972  * service routine for a disk write (from the procedure called
3973  * by the device driver to inform the filesystem caches of
3974  * a request completion).  It should be called early in this
3975  * procedure, before the block is made available to other
3976  * processes or other routines are called.
3977  */
3978 static void
3979 softdep_disk_write_complete(bp)
3980 	struct buf *bp;		/* describes the completed disk write */
3981 {
3982 	struct worklist *wk;
3983 	struct workhead reattach;
3984 	struct newblk *newblk;
3985 	struct allocindir *aip;
3986 	struct allocdirect *adp;
3987 	struct indirdep *indirdep;
3988 	struct inodedep *inodedep;
3989 	struct bmsafemap *bmsafemap;
3990 
3991 	/*
3992 	 * If an error occurred while doing the write, then the data
3993 	 * has not hit the disk and the dependencies cannot be unrolled.
3994 	 */
3995 	if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0)
3996 		return;
3997 #ifdef DEBUG
3998 	if (lk.lkt_held != NOHOLDER)
3999 		panic("softdep_disk_write_complete: lock is held");
4000 	lk.lkt_held = SPECIAL_FLAG;
4001 #endif
4002 	LIST_INIT(&reattach);
4003 	while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
4004 		WORKLIST_REMOVE(wk);
4005 		switch (wk->wk_type) {
4006 
4007 		case D_PAGEDEP:
4008 			if (handle_written_filepage(WK_PAGEDEP(wk), bp))
4009 				WORKLIST_INSERT(&reattach, wk);
4010 			continue;
4011 
4012 		case D_INODEDEP:
4013 			if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
4014 				WORKLIST_INSERT(&reattach, wk);
4015 			continue;
4016 
4017 		case D_BMSAFEMAP:
4018 			bmsafemap = WK_BMSAFEMAP(wk);
4019 			while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
4020 				newblk->nb_state |= DEPCOMPLETE;
4021 				newblk->nb_bmsafemap = NULL;
4022 				LIST_REMOVE(newblk, nb_deps);
4023 			}
4024 			while ((adp =
4025 			   LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
4026 				adp->ad_state |= DEPCOMPLETE;
4027 				adp->ad_buf = NULL;
4028 				LIST_REMOVE(adp, ad_deps);
4029 				handle_allocdirect_partdone(adp);
4030 			}
4031 			while ((aip =
4032 			    LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
4033 				aip->ai_state |= DEPCOMPLETE;
4034 				aip->ai_buf = NULL;
4035 				LIST_REMOVE(aip, ai_deps);
4036 				handle_allocindir_partdone(aip);
4037 			}
4038 			while ((inodedep =
4039 			     LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
4040 				inodedep->id_state |= DEPCOMPLETE;
4041 				LIST_REMOVE(inodedep, id_deps);
4042 				inodedep->id_buf = NULL;
4043 			}
4044 			WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
4045 			continue;
4046 
4047 		case D_MKDIR:
4048 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
4049 			continue;
4050 
4051 		case D_ALLOCDIRECT:
4052 			adp = WK_ALLOCDIRECT(wk);
4053 			adp->ad_state |= COMPLETE;
4054 			handle_allocdirect_partdone(adp);
4055 			continue;
4056 
4057 		case D_ALLOCINDIR:
4058 			aip = WK_ALLOCINDIR(wk);
4059 			aip->ai_state |= COMPLETE;
4060 			handle_allocindir_partdone(aip);
4061 			continue;
4062 
4063 		case D_INDIRDEP:
4064 			indirdep = WK_INDIRDEP(wk);
4065 			if (indirdep->ir_state & GOINGAWAY) {
4066 				lk.lkt_held = NOHOLDER;
4067 				panic("disk_write_complete: indirdep gone");
4068 			}
4069 			bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
4070 			FREE(indirdep->ir_saveddata, M_INDIRDEP);
4071 			indirdep->ir_saveddata = 0;
4072 			indirdep->ir_state &= ~UNDONE;
4073 			indirdep->ir_state |= ATTACHED;
4074 			while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
4075 				handle_allocindir_partdone(aip);
4076 				if (aip == LIST_FIRST(&indirdep->ir_donehd)) {
4077 					lk.lkt_held = NOHOLDER;
4078 					panic("disk_write_complete: not gone");
4079 				}
4080 			}
4081 			WORKLIST_INSERT(&reattach, wk);
4082 			if ((bp->b_flags & B_DELWRI) == 0)
4083 				stat_indir_blk_ptrs++;
4084 			bdirty(bp);
4085 			continue;
4086 
4087 		default:
4088 			lk.lkt_held = NOHOLDER;
4089 			panic("handle_disk_write_complete: Unknown type %s",
4090 			    TYPENAME(wk->wk_type));
4091 			/* NOTREACHED */
4092 		}
4093 	}
4094 	/*
4095 	 * Reattach any requests that must be redone.
4096 	 */
4097 	while ((wk = LIST_FIRST(&reattach)) != NULL) {
4098 		WORKLIST_REMOVE(wk);
4099 		WORKLIST_INSERT(&bp->b_dep, wk);
4100 	}
4101 #ifdef DEBUG
4102 	if (lk.lkt_held != SPECIAL_FLAG)
4103 		panic("softdep_disk_write_complete: lock lost");
4104 	lk.lkt_held = NOHOLDER;
4105 #endif
4106 }
4107 
4108 /*
4109  * Called from within softdep_disk_write_complete above. Note that
4110  * this routine is always called from interrupt level with further
4111  * splbio interrupts blocked.
4112  */
4113 static void
4114 handle_allocdirect_partdone(adp)
4115 	struct allocdirect *adp;	/* the completed allocdirect */
4116 {
4117 	struct allocdirectlst *listhead;
4118 	struct allocdirect *listadp;
4119 	struct inodedep *inodedep;
4120 	long bsize, delay;
4121 
4122 	if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4123 		return;
4124 	if (adp->ad_buf != NULL) {
4125 		lk.lkt_held = NOHOLDER;
4126 		panic("handle_allocdirect_partdone: dangling dep");
4127 	}
4128 	/*
4129 	 * The on-disk inode cannot claim to be any larger than the last
4130 	 * fragment that has been written. Otherwise, the on-disk inode
4131 	 * might have fragments that were not the last block in the file
4132 	 * which would corrupt the filesystem. Thus, we cannot free any
4133 	 * allocdirects after one whose ad_oldblkno claims a fragment as
4134 	 * these blocks must be rolled back to zero before writing the inode.
4135 	 * We check the currently active set of allocdirects in id_inoupdt
4136 	 * or id_extupdt as appropriate.
4137 	 */
4138 	inodedep = adp->ad_inodedep;
4139 	bsize = inodedep->id_fs->fs_bsize;
4140 	if (adp->ad_state & EXTDATA)
4141 		listhead = &inodedep->id_extupdt;
4142 	else
4143 		listhead = &inodedep->id_inoupdt;
4144 	TAILQ_FOREACH(listadp, listhead, ad_next) {
4145 		/* found our block */
4146 		if (listadp == adp)
4147 			break;
4148 		/* continue if ad_oldlbn is not a fragment */
4149 		if (listadp->ad_oldsize == 0 ||
4150 		    listadp->ad_oldsize == bsize)
4151 			continue;
4152 		/* hit a fragment */
4153 		return;
4154 	}
4155 	/*
4156 	 * If we have reached the end of the current list without
4157 	 * finding the just finished dependency, then it must be
4158 	 * on the future dependency list. Future dependencies cannot
4159 	 * be freed until they are moved to the current list.
4160 	 */
4161 	if (listadp == NULL) {
4162 #ifdef DEBUG
4163 		if (adp->ad_state & EXTDATA)
4164 			listhead = &inodedep->id_newextupdt;
4165 		else
4166 			listhead = &inodedep->id_newinoupdt;
4167 		TAILQ_FOREACH(listadp, listhead, ad_next)
4168 			/* found our block */
4169 			if (listadp == adp)
4170 				break;
4171 		if (listadp == NULL) {
4172 			lk.lkt_held = NOHOLDER;
4173 			panic("handle_allocdirect_partdone: lost dep");
4174 		}
4175 #endif /* DEBUG */
4176 		return;
4177 	}
4178 	/*
4179 	 * If we have found the just finished dependency, then free
4180 	 * it along with anything that follows it that is complete.
4181 	 * If the inode still has a bitmap dependency, then it has
4182 	 * never been written to disk, hence the on-disk inode cannot
4183 	 * reference the old fragment so we can free it without delay.
4184 	 */
4185 	delay = (inodedep->id_state & DEPCOMPLETE);
4186 	for (; adp; adp = listadp) {
4187 		listadp = TAILQ_NEXT(adp, ad_next);
4188 		if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4189 			return;
4190 		free_allocdirect(listhead, adp, delay);
4191 	}
4192 }
4193 
4194 /*
4195  * Called from within softdep_disk_write_complete above. Note that
4196  * this routine is always called from interrupt level with further
4197  * splbio interrupts blocked.
4198  */
4199 static void
4200 handle_allocindir_partdone(aip)
4201 	struct allocindir *aip;		/* the completed allocindir */
4202 {
4203 	struct indirdep *indirdep;
4204 
4205 	if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
4206 		return;
4207 	if (aip->ai_buf != NULL) {
4208 		lk.lkt_held = NOHOLDER;
4209 		panic("handle_allocindir_partdone: dangling dependency");
4210 	}
4211 	indirdep = aip->ai_indirdep;
4212 	if (indirdep->ir_state & UNDONE) {
4213 		LIST_REMOVE(aip, ai_next);
4214 		LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
4215 		return;
4216 	}
4217 	if (indirdep->ir_state & UFS1FMT)
4218 		((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4219 		    aip->ai_newblkno;
4220 	else
4221 		((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4222 		    aip->ai_newblkno;
4223 	LIST_REMOVE(aip, ai_next);
4224 	if (aip->ai_freefrag != NULL)
4225 		add_to_worklist(&aip->ai_freefrag->ff_list);
4226 	WORKITEM_FREE(aip, D_ALLOCINDIR);
4227 }
4228 
4229 /*
4230  * Called from within softdep_disk_write_complete above to restore
4231  * in-memory inode block contents to their most up-to-date state. Note
4232  * that this routine is always called from interrupt level with further
4233  * splbio interrupts blocked.
4234  */
4235 static int
4236 handle_written_inodeblock(inodedep, bp)
4237 	struct inodedep *inodedep;
4238 	struct buf *bp;		/* buffer containing the inode block */
4239 {
4240 	struct worklist *wk, *filefree;
4241 	struct allocdirect *adp, *nextadp;
4242 	struct ufs1_dinode *dp1 = NULL;
4243 	struct ufs2_dinode *dp2 = NULL;
4244 	int hadchanges, fstype;
4245 
4246 	if ((inodedep->id_state & IOSTARTED) == 0) {
4247 		lk.lkt_held = NOHOLDER;
4248 		panic("handle_written_inodeblock: not started");
4249 	}
4250 	inodedep->id_state &= ~IOSTARTED;
4251 	inodedep->id_state |= COMPLETE;
4252 	if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
4253 		fstype = UFS1;
4254 		dp1 = (struct ufs1_dinode *)bp->b_data +
4255 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4256 	} else {
4257 		fstype = UFS2;
4258 		dp2 = (struct ufs2_dinode *)bp->b_data +
4259 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4260 	}
4261 	/*
4262 	 * If we had to rollback the inode allocation because of
4263 	 * bitmaps being incomplete, then simply restore it.
4264 	 * Keep the block dirty so that it will not be reclaimed until
4265 	 * all associated dependencies have been cleared and the
4266 	 * corresponding updates written to disk.
4267 	 */
4268 	if (inodedep->id_savedino1 != NULL) {
4269 		if (fstype == UFS1)
4270 			*dp1 = *inodedep->id_savedino1;
4271 		else
4272 			*dp2 = *inodedep->id_savedino2;
4273 		FREE(inodedep->id_savedino1, M_INODEDEP);
4274 		inodedep->id_savedino1 = NULL;
4275 		if ((bp->b_flags & B_DELWRI) == 0)
4276 			stat_inode_bitmap++;
4277 		bdirty(bp);
4278 		return (1);
4279 	}
4280 	/*
4281 	 * Roll forward anything that had to be rolled back before
4282 	 * the inode could be updated.
4283 	 */
4284 	hadchanges = 0;
4285 	for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
4286 		nextadp = TAILQ_NEXT(adp, ad_next);
4287 		if (adp->ad_state & ATTACHED) {
4288 			lk.lkt_held = NOHOLDER;
4289 			panic("handle_written_inodeblock: new entry");
4290 		}
4291 		if (fstype == UFS1) {
4292 			if (adp->ad_lbn < NDADDR) {
4293 				if (dp1->di_db[adp->ad_lbn]!=adp->ad_oldblkno) {
4294 					lk.lkt_held = NOHOLDER;
4295 					panic("%s %s #%jd mismatch %d != %jd",
4296 					    "handle_written_inodeblock:",
4297 					    "direct pointer",
4298 					    (intmax_t)adp->ad_lbn,
4299 					    dp1->di_db[adp->ad_lbn],
4300 					    (intmax_t)adp->ad_oldblkno);
4301 				}
4302 				dp1->di_db[adp->ad_lbn] = adp->ad_newblkno;
4303 			} else {
4304 				if (dp1->di_ib[adp->ad_lbn - NDADDR] != 0) {
4305 					lk.lkt_held = NOHOLDER;
4306 					panic("%s: %s #%jd allocated as %d",
4307 					    "handle_written_inodeblock",
4308 					    "indirect pointer",
4309 					    (intmax_t)adp->ad_lbn - NDADDR,
4310 					    dp1->di_ib[adp->ad_lbn - NDADDR]);
4311 				}
4312 				dp1->di_ib[adp->ad_lbn - NDADDR] =
4313 				    adp->ad_newblkno;
4314 			}
4315 		} else {
4316 			if (adp->ad_lbn < NDADDR) {
4317 				if (dp2->di_db[adp->ad_lbn]!=adp->ad_oldblkno) {
4318 					lk.lkt_held = NOHOLDER;
4319 					panic("%s: %s #%jd %s %jd != %jd",
4320 					    "handle_written_inodeblock",
4321 					    "direct pointer",
4322 					    (intmax_t)adp->ad_lbn, "mismatch",
4323 					    (intmax_t)dp2->di_db[adp->ad_lbn],
4324 					    (intmax_t)adp->ad_oldblkno);
4325 				}
4326 				dp2->di_db[adp->ad_lbn] = adp->ad_newblkno;
4327 			} else {
4328 				if (dp2->di_ib[adp->ad_lbn - NDADDR] != 0) {
4329 					lk.lkt_held = NOHOLDER;
4330 					panic("%s: %s #%jd allocated as %jd",
4331 					    "handle_written_inodeblock",
4332 					    "indirect pointer",
4333 					    (intmax_t)adp->ad_lbn - NDADDR,
4334 					    (intmax_t)
4335 					    dp2->di_ib[adp->ad_lbn - NDADDR]);
4336 				}
4337 				dp2->di_ib[adp->ad_lbn - NDADDR] =
4338 				    adp->ad_newblkno;
4339 			}
4340 		}
4341 		adp->ad_state &= ~UNDONE;
4342 		adp->ad_state |= ATTACHED;
4343 		hadchanges = 1;
4344 	}
4345 	for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
4346 		nextadp = TAILQ_NEXT(adp, ad_next);
4347 		if (adp->ad_state & ATTACHED) {
4348 			lk.lkt_held = NOHOLDER;
4349 			panic("handle_written_inodeblock: new entry");
4350 		}
4351 		if (dp2->di_extb[adp->ad_lbn] != adp->ad_oldblkno) {
4352 			lk.lkt_held = NOHOLDER;
4353 			panic("%s: direct pointers #%jd %s %jd != %jd",
4354 			    "handle_written_inodeblock",
4355 			    (intmax_t)adp->ad_lbn, "mismatch",
4356 			    (intmax_t)dp2->di_extb[adp->ad_lbn],
4357 			    (intmax_t)adp->ad_oldblkno);
4358 		}
4359 		dp2->di_extb[adp->ad_lbn] = adp->ad_newblkno;
4360 		adp->ad_state &= ~UNDONE;
4361 		adp->ad_state |= ATTACHED;
4362 		hadchanges = 1;
4363 	}
4364 	if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
4365 		stat_direct_blk_ptrs++;
4366 	/*
4367 	 * Reset the file size to its most up-to-date value.
4368 	 */
4369 	if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) {
4370 		lk.lkt_held = NOHOLDER;
4371 		panic("handle_written_inodeblock: bad size");
4372 	}
4373 	if (fstype == UFS1) {
4374 		if (dp1->di_size != inodedep->id_savedsize) {
4375 			dp1->di_size = inodedep->id_savedsize;
4376 			hadchanges = 1;
4377 		}
4378 	} else {
4379 		if (dp2->di_size != inodedep->id_savedsize) {
4380 			dp2->di_size = inodedep->id_savedsize;
4381 			hadchanges = 1;
4382 		}
4383 		if (dp2->di_extsize != inodedep->id_savedextsize) {
4384 			dp2->di_extsize = inodedep->id_savedextsize;
4385 			hadchanges = 1;
4386 		}
4387 	}
4388 	inodedep->id_savedsize = -1;
4389 	inodedep->id_savedextsize = -1;
4390 	/*
4391 	 * If there were any rollbacks in the inode block, then it must be
4392 	 * marked dirty so that its will eventually get written back in
4393 	 * its correct form.
4394 	 */
4395 	if (hadchanges)
4396 		bdirty(bp);
4397 	/*
4398 	 * Process any allocdirects that completed during the update.
4399 	 */
4400 	if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
4401 		handle_allocdirect_partdone(adp);
4402 	if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
4403 		handle_allocdirect_partdone(adp);
4404 	/*
4405 	 * Process deallocations that were held pending until the
4406 	 * inode had been written to disk. Freeing of the inode
4407 	 * is delayed until after all blocks have been freed to
4408 	 * avoid creation of new <vfsid, inum, lbn> triples
4409 	 * before the old ones have been deleted.
4410 	 */
4411 	filefree = NULL;
4412 	while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
4413 		WORKLIST_REMOVE(wk);
4414 		switch (wk->wk_type) {
4415 
4416 		case D_FREEFILE:
4417 			/*
4418 			 * We defer adding filefree to the worklist until
4419 			 * all other additions have been made to ensure
4420 			 * that it will be done after all the old blocks
4421 			 * have been freed.
4422 			 */
4423 			if (filefree != NULL) {
4424 				lk.lkt_held = NOHOLDER;
4425 				panic("handle_written_inodeblock: filefree");
4426 			}
4427 			filefree = wk;
4428 			continue;
4429 
4430 		case D_MKDIR:
4431 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
4432 			continue;
4433 
4434 		case D_DIRADD:
4435 			diradd_inode_written(WK_DIRADD(wk), inodedep);
4436 			continue;
4437 
4438 		case D_FREEBLKS:
4439 		case D_FREEFRAG:
4440 		case D_DIRREM:
4441 			add_to_worklist(wk);
4442 			continue;
4443 
4444 		case D_NEWDIRBLK:
4445 			free_newdirblk(WK_NEWDIRBLK(wk));
4446 			continue;
4447 
4448 		default:
4449 			lk.lkt_held = NOHOLDER;
4450 			panic("handle_written_inodeblock: Unknown type %s",
4451 			    TYPENAME(wk->wk_type));
4452 			/* NOTREACHED */
4453 		}
4454 	}
4455 	if (filefree != NULL) {
4456 		if (free_inodedep(inodedep) == 0) {
4457 			lk.lkt_held = NOHOLDER;
4458 			panic("handle_written_inodeblock: live inodedep");
4459 		}
4460 		add_to_worklist(filefree);
4461 		return (0);
4462 	}
4463 
4464 	/*
4465 	 * If no outstanding dependencies, free it.
4466 	 */
4467 	if (free_inodedep(inodedep) ||
4468 	    (TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
4469 	     TAILQ_FIRST(&inodedep->id_extupdt) == 0))
4470 		return (0);
4471 	return (hadchanges);
4472 }
4473 
4474 /*
4475  * Process a diradd entry after its dependent inode has been written.
4476  * This routine must be called with splbio interrupts blocked.
4477  */
4478 static void
4479 diradd_inode_written(dap, inodedep)
4480 	struct diradd *dap;
4481 	struct inodedep *inodedep;
4482 {
4483 	struct pagedep *pagedep;
4484 
4485 	dap->da_state |= COMPLETE;
4486 	if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4487 		if (dap->da_state & DIRCHG)
4488 			pagedep = dap->da_previous->dm_pagedep;
4489 		else
4490 			pagedep = dap->da_pagedep;
4491 		LIST_REMOVE(dap, da_pdlist);
4492 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4493 	}
4494 	WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
4495 }
4496 
4497 /*
4498  * Handle the completion of a mkdir dependency.
4499  */
4500 static void
4501 handle_written_mkdir(mkdir, type)
4502 	struct mkdir *mkdir;
4503 	int type;
4504 {
4505 	struct diradd *dap;
4506 	struct pagedep *pagedep;
4507 
4508 	if (mkdir->md_state != type) {
4509 		lk.lkt_held = NOHOLDER;
4510 		panic("handle_written_mkdir: bad type");
4511 	}
4512 	dap = mkdir->md_diradd;
4513 	dap->da_state &= ~type;
4514 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
4515 		dap->da_state |= DEPCOMPLETE;
4516 	if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4517 		if (dap->da_state & DIRCHG)
4518 			pagedep = dap->da_previous->dm_pagedep;
4519 		else
4520 			pagedep = dap->da_pagedep;
4521 		LIST_REMOVE(dap, da_pdlist);
4522 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4523 	}
4524 	LIST_REMOVE(mkdir, md_mkdirs);
4525 	WORKITEM_FREE(mkdir, D_MKDIR);
4526 }
4527 
4528 /*
4529  * Called from within softdep_disk_write_complete above.
4530  * A write operation was just completed. Removed inodes can
4531  * now be freed and associated block pointers may be committed.
4532  * Note that this routine is always called from interrupt level
4533  * with further splbio interrupts blocked.
4534  */
4535 static int
4536 handle_written_filepage(pagedep, bp)
4537 	struct pagedep *pagedep;
4538 	struct buf *bp;		/* buffer containing the written page */
4539 {
4540 	struct dirrem *dirrem;
4541 	struct diradd *dap, *nextdap;
4542 	struct direct *ep;
4543 	int i, chgs;
4544 
4545 	if ((pagedep->pd_state & IOSTARTED) == 0) {
4546 		lk.lkt_held = NOHOLDER;
4547 		panic("handle_written_filepage: not started");
4548 	}
4549 	pagedep->pd_state &= ~IOSTARTED;
4550 	/*
4551 	 * Process any directory removals that have been committed.
4552 	 */
4553 	while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
4554 		LIST_REMOVE(dirrem, dm_next);
4555 		dirrem->dm_dirinum = pagedep->pd_ino;
4556 		add_to_worklist(&dirrem->dm_list);
4557 	}
4558 	/*
4559 	 * Free any directory additions that have been committed.
4560 	 * If it is a newly allocated block, we have to wait until
4561 	 * the on-disk directory inode claims the new block.
4562 	 */
4563 	if ((pagedep->pd_state & NEWBLOCK) == 0)
4564 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
4565 			free_diradd(dap);
4566 	/*
4567 	 * Uncommitted directory entries must be restored.
4568 	 */
4569 	for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
4570 		for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
4571 		     dap = nextdap) {
4572 			nextdap = LIST_NEXT(dap, da_pdlist);
4573 			if (dap->da_state & ATTACHED) {
4574 				lk.lkt_held = NOHOLDER;
4575 				panic("handle_written_filepage: attached");
4576 			}
4577 			ep = (struct direct *)
4578 			    ((char *)bp->b_data + dap->da_offset);
4579 			ep->d_ino = dap->da_newinum;
4580 			dap->da_state &= ~UNDONE;
4581 			dap->da_state |= ATTACHED;
4582 			chgs = 1;
4583 			/*
4584 			 * If the inode referenced by the directory has
4585 			 * been written out, then the dependency can be
4586 			 * moved to the pending list.
4587 			 */
4588 			if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4589 				LIST_REMOVE(dap, da_pdlist);
4590 				LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
4591 				    da_pdlist);
4592 			}
4593 		}
4594 	}
4595 	/*
4596 	 * If there were any rollbacks in the directory, then it must be
4597 	 * marked dirty so that its will eventually get written back in
4598 	 * its correct form.
4599 	 */
4600 	if (chgs) {
4601 		if ((bp->b_flags & B_DELWRI) == 0)
4602 			stat_dir_entry++;
4603 		bdirty(bp);
4604 		return (1);
4605 	}
4606 	/*
4607 	 * If we are not waiting for a new directory block to be
4608 	 * claimed by its inode, then the pagedep will be freed.
4609 	 * Otherwise it will remain to track any new entries on
4610 	 * the page in case they are fsync'ed.
4611 	 */
4612 	if ((pagedep->pd_state & NEWBLOCK) == 0) {
4613 		LIST_REMOVE(pagedep, pd_hash);
4614 		WORKITEM_FREE(pagedep, D_PAGEDEP);
4615 	}
4616 	return (0);
4617 }
4618 
4619 /*
4620  * Writing back in-core inode structures.
4621  *
4622  * The filesystem only accesses an inode's contents when it occupies an
4623  * "in-core" inode structure.  These "in-core" structures are separate from
4624  * the page frames used to cache inode blocks.  Only the latter are
4625  * transferred to/from the disk.  So, when the updated contents of the
4626  * "in-core" inode structure are copied to the corresponding in-memory inode
4627  * block, the dependencies are also transferred.  The following procedure is
4628  * called when copying a dirty "in-core" inode to a cached inode block.
4629  */
4630 
4631 /*
4632  * Called when an inode is loaded from disk. If the effective link count
4633  * differed from the actual link count when it was last flushed, then we
4634  * need to ensure that the correct effective link count is put back.
4635  */
4636 void
4637 softdep_load_inodeblock(ip)
4638 	struct inode *ip;	/* the "in_core" copy of the inode */
4639 {
4640 	struct inodedep *inodedep;
4641 
4642 	/*
4643 	 * Check for alternate nlink count.
4644 	 */
4645 	ip->i_effnlink = ip->i_nlink;
4646 	ACQUIRE_LOCK(&lk);
4647 	if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
4648 		FREE_LOCK(&lk);
4649 		return;
4650 	}
4651 	ip->i_effnlink -= inodedep->id_nlinkdelta;
4652 	if (inodedep->id_state & SPACECOUNTED)
4653 		ip->i_flag |= IN_SPACECOUNTED;
4654 	FREE_LOCK(&lk);
4655 }
4656 
4657 /*
4658  * This routine is called just before the "in-core" inode
4659  * information is to be copied to the in-memory inode block.
4660  * Recall that an inode block contains several inodes. If
4661  * the force flag is set, then the dependencies will be
4662  * cleared so that the update can always be made. Note that
4663  * the buffer is locked when this routine is called, so we
4664  * will never be in the middle of writing the inode block
4665  * to disk.
4666  */
4667 void
4668 softdep_update_inodeblock(ip, bp, waitfor)
4669 	struct inode *ip;	/* the "in_core" copy of the inode */
4670 	struct buf *bp;		/* the buffer containing the inode block */
4671 	int waitfor;		/* nonzero => update must be allowed */
4672 {
4673 	struct inodedep *inodedep;
4674 	struct worklist *wk;
4675 	struct buf *ibp;
4676 	int error;
4677 
4678 	/*
4679 	 * If the effective link count is not equal to the actual link
4680 	 * count, then we must track the difference in an inodedep while
4681 	 * the inode is (potentially) tossed out of the cache. Otherwise,
4682 	 * if there is no existing inodedep, then there are no dependencies
4683 	 * to track.
4684 	 */
4685 	ACQUIRE_LOCK(&lk);
4686 	if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
4687 		FREE_LOCK(&lk);
4688 		if (ip->i_effnlink != ip->i_nlink)
4689 			panic("softdep_update_inodeblock: bad link count");
4690 		return;
4691 	}
4692 	if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) {
4693 		FREE_LOCK(&lk);
4694 		panic("softdep_update_inodeblock: bad delta");
4695 	}
4696 	/*
4697 	 * Changes have been initiated. Anything depending on these
4698 	 * changes cannot occur until this inode has been written.
4699 	 */
4700 	inodedep->id_state &= ~COMPLETE;
4701 	if ((inodedep->id_state & ONWORKLIST) == 0)
4702 		WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
4703 	/*
4704 	 * Any new dependencies associated with the incore inode must
4705 	 * now be moved to the list associated with the buffer holding
4706 	 * the in-memory copy of the inode. Once merged process any
4707 	 * allocdirects that are completed by the merger.
4708 	 */
4709 	merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
4710 	if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL)
4711 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
4712 	merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
4713 	if (TAILQ_FIRST(&inodedep->id_extupdt) != NULL)
4714 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt));
4715 	/*
4716 	 * Now that the inode has been pushed into the buffer, the
4717 	 * operations dependent on the inode being written to disk
4718 	 * can be moved to the id_bufwait so that they will be
4719 	 * processed when the buffer I/O completes.
4720 	 */
4721 	while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
4722 		WORKLIST_REMOVE(wk);
4723 		WORKLIST_INSERT(&inodedep->id_bufwait, wk);
4724 	}
4725 	/*
4726 	 * Newly allocated inodes cannot be written until the bitmap
4727 	 * that allocates them have been written (indicated by
4728 	 * DEPCOMPLETE being set in id_state). If we are doing a
4729 	 * forced sync (e.g., an fsync on a file), we force the bitmap
4730 	 * to be written so that the update can be done.
4731 	 */
4732 	if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) {
4733 		FREE_LOCK(&lk);
4734 		return;
4735 	}
4736 	ibp = inodedep->id_buf;
4737 	ibp = getdirtybuf(&ibp, NULL, MNT_WAIT);
4738 	FREE_LOCK(&lk);
4739 	if (ibp && (error = bwrite(ibp)) != 0)
4740 		softdep_error("softdep_update_inodeblock: bwrite", error);
4741 	if ((inodedep->id_state & DEPCOMPLETE) == 0)
4742 		panic("softdep_update_inodeblock: update failed");
4743 }
4744 
4745 /*
4746  * Merge the a new inode dependency list (such as id_newinoupdt) into an
4747  * old inode dependency list (such as id_inoupdt). This routine must be
4748  * called with splbio interrupts blocked.
4749  */
4750 static void
4751 merge_inode_lists(newlisthead, oldlisthead)
4752 	struct allocdirectlst *newlisthead;
4753 	struct allocdirectlst *oldlisthead;
4754 {
4755 	struct allocdirect *listadp, *newadp;
4756 
4757 	newadp = TAILQ_FIRST(newlisthead);
4758 	for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
4759 		if (listadp->ad_lbn < newadp->ad_lbn) {
4760 			listadp = TAILQ_NEXT(listadp, ad_next);
4761 			continue;
4762 		}
4763 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
4764 		TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
4765 		if (listadp->ad_lbn == newadp->ad_lbn) {
4766 			allocdirect_merge(oldlisthead, newadp,
4767 			    listadp);
4768 			listadp = newadp;
4769 		}
4770 		newadp = TAILQ_FIRST(newlisthead);
4771 	}
4772 	while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
4773 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
4774 		TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
4775 	}
4776 }
4777 
4778 /*
4779  * If we are doing an fsync, then we must ensure that any directory
4780  * entries for the inode have been written after the inode gets to disk.
4781  */
4782 int
4783 softdep_fsync(vp)
4784 	struct vnode *vp;	/* the "in_core" copy of the inode */
4785 {
4786 	struct inodedep *inodedep;
4787 	struct pagedep *pagedep;
4788 	struct worklist *wk;
4789 	struct diradd *dap;
4790 	struct mount *mnt;
4791 	struct vnode *pvp;
4792 	struct inode *ip;
4793 	struct buf *bp;
4794 	struct fs *fs;
4795 	struct thread *td = curthread;
4796 	int error, flushparent;
4797 	ino_t parentino;
4798 	ufs_lbn_t lbn;
4799 
4800 	ip = VTOI(vp);
4801 	fs = ip->i_fs;
4802 	ACQUIRE_LOCK(&lk);
4803 	if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) {
4804 		FREE_LOCK(&lk);
4805 		return (0);
4806 	}
4807 	if (LIST_FIRST(&inodedep->id_inowait) != NULL ||
4808 	    LIST_FIRST(&inodedep->id_bufwait) != NULL ||
4809 	    TAILQ_FIRST(&inodedep->id_extupdt) != NULL ||
4810 	    TAILQ_FIRST(&inodedep->id_newextupdt) != NULL ||
4811 	    TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
4812 	    TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) {
4813 		FREE_LOCK(&lk);
4814 		panic("softdep_fsync: pending ops");
4815 	}
4816 	for (error = 0, flushparent = 0; ; ) {
4817 		if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
4818 			break;
4819 		if (wk->wk_type != D_DIRADD) {
4820 			FREE_LOCK(&lk);
4821 			panic("softdep_fsync: Unexpected type %s",
4822 			    TYPENAME(wk->wk_type));
4823 		}
4824 		dap = WK_DIRADD(wk);
4825 		/*
4826 		 * Flush our parent if this directory entry has a MKDIR_PARENT
4827 		 * dependency or is contained in a newly allocated block.
4828 		 */
4829 		if (dap->da_state & DIRCHG)
4830 			pagedep = dap->da_previous->dm_pagedep;
4831 		else
4832 			pagedep = dap->da_pagedep;
4833 		mnt = pagedep->pd_mnt;
4834 		parentino = pagedep->pd_ino;
4835 		lbn = pagedep->pd_lbn;
4836 		if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) {
4837 			FREE_LOCK(&lk);
4838 			panic("softdep_fsync: dirty");
4839 		}
4840 		if ((dap->da_state & MKDIR_PARENT) ||
4841 		    (pagedep->pd_state & NEWBLOCK))
4842 			flushparent = 1;
4843 		else
4844 			flushparent = 0;
4845 		/*
4846 		 * If we are being fsync'ed as part of vgone'ing this vnode,
4847 		 * then we will not be able to release and recover the
4848 		 * vnode below, so we just have to give up on writing its
4849 		 * directory entry out. It will eventually be written, just
4850 		 * not now, but then the user was not asking to have it
4851 		 * written, so we are not breaking any promises.
4852 		 */
4853 		if (vp->v_iflag & VI_XLOCK)
4854 			break;
4855 		/*
4856 		 * We prevent deadlock by always fetching inodes from the
4857 		 * root, moving down the directory tree. Thus, when fetching
4858 		 * our parent directory, we first try to get the lock. If
4859 		 * that fails, we must unlock ourselves before requesting
4860 		 * the lock on our parent. See the comment in ufs_lookup
4861 		 * for details on possible races.
4862 		 */
4863 		FREE_LOCK(&lk);
4864 		if (VFS_VGET(mnt, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp)) {
4865 			VOP_UNLOCK(vp, 0, td);
4866 			error = VFS_VGET(mnt, parentino, LK_EXCLUSIVE, &pvp);
4867 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4868 			if (error != 0)
4869 				return (error);
4870 		}
4871 		/*
4872 		 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
4873 		 * that are contained in direct blocks will be resolved by
4874 		 * doing a UFS_UPDATE. Pagedeps contained in indirect blocks
4875 		 * may require a complete sync'ing of the directory. So, we
4876 		 * try the cheap and fast UFS_UPDATE first, and if that fails,
4877 		 * then we do the slower VOP_FSYNC of the directory.
4878 		 */
4879 		if (flushparent) {
4880 			if ((error = UFS_UPDATE(pvp, 1)) != 0) {
4881 				vput(pvp);
4882 				return (error);
4883 			}
4884 			if ((pagedep->pd_state & NEWBLOCK) &&
4885 			    (error = VOP_FSYNC(pvp, td->td_ucred, MNT_WAIT, td))) {
4886 				vput(pvp);
4887 				return (error);
4888 			}
4889 		}
4890 		/*
4891 		 * Flush directory page containing the inode's name.
4892 		 */
4893 		error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
4894 		    &bp);
4895 		if (error == 0)
4896 			error = bwrite(bp);
4897 		else
4898 			brelse(bp);
4899 		vput(pvp);
4900 		if (error != 0)
4901 			return (error);
4902 		ACQUIRE_LOCK(&lk);
4903 		if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0)
4904 			break;
4905 	}
4906 	FREE_LOCK(&lk);
4907 	return (0);
4908 }
4909 
4910 /*
4911  * Flush all the dirty bitmaps associated with the block device
4912  * before flushing the rest of the dirty blocks so as to reduce
4913  * the number of dependencies that will have to be rolled back.
4914  */
4915 void
4916 softdep_fsync_mountdev(vp)
4917 	struct vnode *vp;
4918 {
4919 	struct buf *bp, *nbp;
4920 	struct worklist *wk;
4921 
4922 	if (!vn_isdisk(vp, NULL))
4923 		panic("softdep_fsync_mountdev: vnode not a disk");
4924 	ACQUIRE_LOCK(&lk);
4925 	VI_LOCK(vp);
4926 	for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
4927 		nbp = TAILQ_NEXT(bp, b_vnbufs);
4928 		/*
4929 		 * If it is already scheduled, skip to the next buffer.
4930 		 */
4931 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
4932 			continue;
4933 
4934 		if ((bp->b_flags & B_DELWRI) == 0) {
4935 			FREE_LOCK(&lk);
4936 			panic("softdep_fsync_mountdev: not dirty");
4937 		}
4938 		/*
4939 		 * We are only interested in bitmaps with outstanding
4940 		 * dependencies.
4941 		 */
4942 		if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
4943 		    wk->wk_type != D_BMSAFEMAP ||
4944 		    (bp->b_vflags & BV_BKGRDINPROG)) {
4945 			BUF_UNLOCK(bp);
4946 			continue;
4947 		}
4948 		VI_UNLOCK(vp);
4949 		bremfree(bp);
4950 		FREE_LOCK(&lk);
4951 		(void) bawrite(bp);
4952 		ACQUIRE_LOCK(&lk);
4953 		/*
4954 		 * Since we may have slept during the I/O, we need
4955 		 * to start from a known point.
4956 		 */
4957 		VI_LOCK(vp);
4958 		nbp = TAILQ_FIRST(&vp->v_dirtyblkhd);
4959 	}
4960 	drain_output(vp, 1);
4961 	VI_UNLOCK(vp);
4962 	FREE_LOCK(&lk);
4963 }
4964 
4965 /*
4966  * This routine is called when we are trying to synchronously flush a
4967  * file. This routine must eliminate any filesystem metadata dependencies
4968  * so that the syncing routine can succeed by pushing the dirty blocks
4969  * associated with the file. If any I/O errors occur, they are returned.
4970  */
4971 int
4972 softdep_sync_metadata(ap)
4973 	struct vop_fsync_args /* {
4974 		struct vnode *a_vp;
4975 		struct ucred *a_cred;
4976 		int a_waitfor;
4977 		struct thread *a_td;
4978 	} */ *ap;
4979 {
4980 	struct vnode *vp = ap->a_vp;
4981 	struct pagedep *pagedep;
4982 	struct allocdirect *adp;
4983 	struct allocindir *aip;
4984 	struct buf *bp, *nbp;
4985 	struct worklist *wk;
4986 	int i, error, waitfor;
4987 
4988 	/*
4989 	 * Check whether this vnode is involved in a filesystem
4990 	 * that is doing soft dependency processing.
4991 	 */
4992 	if (!vn_isdisk(vp, NULL)) {
4993 		if (!DOINGSOFTDEP(vp))
4994 			return (0);
4995 	} else
4996 		if (vp->v_rdev->si_mountpoint == NULL ||
4997 		    (vp->v_rdev->si_mountpoint->mnt_flag & MNT_SOFTDEP) == 0)
4998 			return (0);
4999 	/*
5000 	 * Ensure that any direct block dependencies have been cleared.
5001 	 */
5002 	ACQUIRE_LOCK(&lk);
5003 	if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) {
5004 		FREE_LOCK(&lk);
5005 		return (error);
5006 	}
5007 	/*
5008 	 * For most files, the only metadata dependencies are the
5009 	 * cylinder group maps that allocate their inode or blocks.
5010 	 * The block allocation dependencies can be found by traversing
5011 	 * the dependency lists for any buffers that remain on their
5012 	 * dirty buffer list. The inode allocation dependency will
5013 	 * be resolved when the inode is updated with MNT_WAIT.
5014 	 * This work is done in two passes. The first pass grabs most
5015 	 * of the buffers and begins asynchronously writing them. The
5016 	 * only way to wait for these asynchronous writes is to sleep
5017 	 * on the filesystem vnode which may stay busy for a long time
5018 	 * if the filesystem is active. So, instead, we make a second
5019 	 * pass over the dependencies blocking on each write. In the
5020 	 * usual case we will be blocking against a write that we
5021 	 * initiated, so when it is done the dependency will have been
5022 	 * resolved. Thus the second pass is expected to end quickly.
5023 	 */
5024 	waitfor = MNT_NOWAIT;
5025 top:
5026 	/*
5027 	 * We must wait for any I/O in progress to finish so that
5028 	 * all potential buffers on the dirty list will be visible.
5029 	 */
5030 	VI_LOCK(vp);
5031 	drain_output(vp, 1);
5032 	bp = getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd),
5033 	    VI_MTX(vp), MNT_WAIT);
5034 	if (bp == NULL) {
5035 		VI_UNLOCK(vp);
5036 		FREE_LOCK(&lk);
5037 		return (0);
5038 	}
5039 	/* While syncing snapshots, we must allow recursive lookups */
5040 	bp->b_lock.lk_flags |= LK_CANRECURSE;
5041 loop:
5042 	/*
5043 	 * As we hold the buffer locked, none of its dependencies
5044 	 * will disappear.
5045 	 */
5046 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5047 		switch (wk->wk_type) {
5048 
5049 		case D_ALLOCDIRECT:
5050 			adp = WK_ALLOCDIRECT(wk);
5051 			if (adp->ad_state & DEPCOMPLETE)
5052 				continue;
5053 			nbp = adp->ad_buf;
5054 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5055 			if (nbp == NULL)
5056 				continue;
5057 			FREE_LOCK(&lk);
5058 			if (waitfor == MNT_NOWAIT) {
5059 				bawrite(nbp);
5060 			} else if ((error = bwrite(nbp)) != 0) {
5061 				break;
5062 			}
5063 			ACQUIRE_LOCK(&lk);
5064 			continue;
5065 
5066 		case D_ALLOCINDIR:
5067 			aip = WK_ALLOCINDIR(wk);
5068 			if (aip->ai_state & DEPCOMPLETE)
5069 				continue;
5070 			nbp = aip->ai_buf;
5071 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5072 			if (nbp == NULL)
5073 				continue;
5074 			FREE_LOCK(&lk);
5075 			if (waitfor == MNT_NOWAIT) {
5076 				bawrite(nbp);
5077 			} else if ((error = bwrite(nbp)) != 0) {
5078 				break;
5079 			}
5080 			ACQUIRE_LOCK(&lk);
5081 			continue;
5082 
5083 		case D_INDIRDEP:
5084 		restart:
5085 
5086 			LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
5087 				if (aip->ai_state & DEPCOMPLETE)
5088 					continue;
5089 				nbp = aip->ai_buf;
5090 				nbp = getdirtybuf(&nbp, NULL, MNT_WAIT);
5091 				if (nbp == NULL)
5092 					goto restart;
5093 				FREE_LOCK(&lk);
5094 				if ((error = bwrite(nbp)) != 0) {
5095 					break;
5096 				}
5097 				ACQUIRE_LOCK(&lk);
5098 				goto restart;
5099 			}
5100 			continue;
5101 
5102 		case D_INODEDEP:
5103 			if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs,
5104 			    WK_INODEDEP(wk)->id_ino)) != 0) {
5105 				FREE_LOCK(&lk);
5106 				break;
5107 			}
5108 			continue;
5109 
5110 		case D_PAGEDEP:
5111 			/*
5112 			 * We are trying to sync a directory that may
5113 			 * have dependencies on both its own metadata
5114 			 * and/or dependencies on the inodes of any
5115 			 * recently allocated files. We walk its diradd
5116 			 * lists pushing out the associated inode.
5117 			 */
5118 			pagedep = WK_PAGEDEP(wk);
5119 			for (i = 0; i < DAHASHSZ; i++) {
5120 				if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
5121 					continue;
5122 				if ((error =
5123 				    flush_pagedep_deps(vp, pagedep->pd_mnt,
5124 						&pagedep->pd_diraddhd[i]))) {
5125 					FREE_LOCK(&lk);
5126 					break;
5127 				}
5128 			}
5129 			continue;
5130 
5131 		case D_MKDIR:
5132 			/*
5133 			 * This case should never happen if the vnode has
5134 			 * been properly sync'ed. However, if this function
5135 			 * is used at a place where the vnode has not yet
5136 			 * been sync'ed, this dependency can show up. So,
5137 			 * rather than panic, just flush it.
5138 			 */
5139 			nbp = WK_MKDIR(wk)->md_buf;
5140 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5141 			if (nbp == NULL)
5142 				continue;
5143 			FREE_LOCK(&lk);
5144 			if (waitfor == MNT_NOWAIT) {
5145 				bawrite(nbp);
5146 			} else if ((error = bwrite(nbp)) != 0) {
5147 				break;
5148 			}
5149 			ACQUIRE_LOCK(&lk);
5150 			continue;
5151 
5152 		case D_BMSAFEMAP:
5153 			/*
5154 			 * This case should never happen if the vnode has
5155 			 * been properly sync'ed. However, if this function
5156 			 * is used at a place where the vnode has not yet
5157 			 * been sync'ed, this dependency can show up. So,
5158 			 * rather than panic, just flush it.
5159 			 */
5160 			nbp = WK_BMSAFEMAP(wk)->sm_buf;
5161 			nbp = getdirtybuf(&nbp, NULL, waitfor);
5162 			if (nbp == NULL)
5163 				continue;
5164 			FREE_LOCK(&lk);
5165 			if (waitfor == MNT_NOWAIT) {
5166 				bawrite(nbp);
5167 			} else if ((error = bwrite(nbp)) != 0) {
5168 				break;
5169 			}
5170 			ACQUIRE_LOCK(&lk);
5171 			continue;
5172 
5173 		default:
5174 			FREE_LOCK(&lk);
5175 			panic("softdep_sync_metadata: Unknown type %s",
5176 			    TYPENAME(wk->wk_type));
5177 			/* NOTREACHED */
5178 		}
5179 		/* We reach here only in error and unlocked */
5180 		if (error == 0)
5181 			panic("softdep_sync_metadata: zero error");
5182 		bp->b_lock.lk_flags &= ~LK_CANRECURSE;
5183 		bawrite(bp);
5184 		return (error);
5185 	}
5186 	VI_LOCK(vp);
5187 	nbp = getdirtybuf(&TAILQ_NEXT(bp, b_vnbufs), VI_MTX(vp), MNT_WAIT);
5188 	if (nbp == NULL)
5189 		VI_UNLOCK(vp);
5190 	FREE_LOCK(&lk);
5191 	bp->b_lock.lk_flags &= ~LK_CANRECURSE;
5192 	bawrite(bp);
5193 	ACQUIRE_LOCK(&lk);
5194 	if (nbp != NULL) {
5195 		bp = nbp;
5196 		goto loop;
5197 	}
5198 	/*
5199 	 * The brief unlock is to allow any pent up dependency
5200 	 * processing to be done. Then proceed with the second pass.
5201 	 */
5202 	if (waitfor == MNT_NOWAIT) {
5203 		waitfor = MNT_WAIT;
5204 		FREE_LOCK(&lk);
5205 		ACQUIRE_LOCK(&lk);
5206 		goto top;
5207 	}
5208 
5209 	/*
5210 	 * If we have managed to get rid of all the dirty buffers,
5211 	 * then we are done. For certain directories and block
5212 	 * devices, we may need to do further work.
5213 	 *
5214 	 * We must wait for any I/O in progress to finish so that
5215 	 * all potential buffers on the dirty list will be visible.
5216 	 */
5217 	VI_LOCK(vp);
5218 	drain_output(vp, 1);
5219 	if (TAILQ_FIRST(&vp->v_dirtyblkhd) == NULL) {
5220 		VI_UNLOCK(vp);
5221 		FREE_LOCK(&lk);
5222 		return (0);
5223 	}
5224 	VI_UNLOCK(vp);
5225 
5226 	FREE_LOCK(&lk);
5227 	/*
5228 	 * If we are trying to sync a block device, some of its buffers may
5229 	 * contain metadata that cannot be written until the contents of some
5230 	 * partially written files have been written to disk. The only easy
5231 	 * way to accomplish this is to sync the entire filesystem (luckily
5232 	 * this happens rarely).
5233 	 */
5234 	if (vn_isdisk(vp, NULL) &&
5235 	    vp->v_rdev->si_mountpoint && !VOP_ISLOCKED(vp, NULL) &&
5236 	    (error = VFS_SYNC(vp->v_rdev->si_mountpoint, MNT_WAIT, ap->a_cred,
5237 	     ap->a_td)) != 0)
5238 		return (error);
5239 	return (0);
5240 }
5241 
5242 /*
5243  * Flush the dependencies associated with an inodedep.
5244  * Called with splbio blocked.
5245  */
5246 static int
5247 flush_inodedep_deps(fs, ino)
5248 	struct fs *fs;
5249 	ino_t ino;
5250 {
5251 	struct inodedep *inodedep;
5252 	int error, waitfor;
5253 
5254 	/*
5255 	 * This work is done in two passes. The first pass grabs most
5256 	 * of the buffers and begins asynchronously writing them. The
5257 	 * only way to wait for these asynchronous writes is to sleep
5258 	 * on the filesystem vnode which may stay busy for a long time
5259 	 * if the filesystem is active. So, instead, we make a second
5260 	 * pass over the dependencies blocking on each write. In the
5261 	 * usual case we will be blocking against a write that we
5262 	 * initiated, so when it is done the dependency will have been
5263 	 * resolved. Thus the second pass is expected to end quickly.
5264 	 * We give a brief window at the top of the loop to allow
5265 	 * any pending I/O to complete.
5266 	 */
5267 	for (error = 0, waitfor = MNT_NOWAIT; ; ) {
5268 		if (error)
5269 			return (error);
5270 		FREE_LOCK(&lk);
5271 		ACQUIRE_LOCK(&lk);
5272 		if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
5273 			return (0);
5274 		if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
5275 		    flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
5276 		    flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
5277 		    flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
5278 			continue;
5279 		/*
5280 		 * If pass2, we are done, otherwise do pass 2.
5281 		 */
5282 		if (waitfor == MNT_WAIT)
5283 			break;
5284 		waitfor = MNT_WAIT;
5285 	}
5286 	/*
5287 	 * Try freeing inodedep in case all dependencies have been removed.
5288 	 */
5289 	if (inodedep_lookup(fs, ino, 0, &inodedep) != 0)
5290 		(void) free_inodedep(inodedep);
5291 	return (0);
5292 }
5293 
5294 /*
5295  * Flush an inode dependency list.
5296  * Called with splbio blocked.
5297  */
5298 static int
5299 flush_deplist(listhead, waitfor, errorp)
5300 	struct allocdirectlst *listhead;
5301 	int waitfor;
5302 	int *errorp;
5303 {
5304 	struct allocdirect *adp;
5305 	struct buf *bp;
5306 
5307 	TAILQ_FOREACH(adp, listhead, ad_next) {
5308 		if (adp->ad_state & DEPCOMPLETE)
5309 			continue;
5310 		bp = adp->ad_buf;
5311 		bp = getdirtybuf(&bp, NULL, waitfor);
5312 		if (bp == NULL) {
5313 			if (waitfor == MNT_NOWAIT)
5314 				continue;
5315 			return (1);
5316 		}
5317 		FREE_LOCK(&lk);
5318 		if (waitfor == MNT_NOWAIT) {
5319 			bawrite(bp);
5320 		} else if ((*errorp = bwrite(bp)) != 0) {
5321 			ACQUIRE_LOCK(&lk);
5322 			return (1);
5323 		}
5324 		ACQUIRE_LOCK(&lk);
5325 		return (1);
5326 	}
5327 	return (0);
5328 }
5329 
5330 /*
5331  * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
5332  * Called with splbio blocked.
5333  */
5334 static int
5335 flush_pagedep_deps(pvp, mp, diraddhdp)
5336 	struct vnode *pvp;
5337 	struct mount *mp;
5338 	struct diraddhd *diraddhdp;
5339 {
5340 	struct thread *td = curthread;
5341 	struct inodedep *inodedep;
5342 	struct ufsmount *ump;
5343 	struct diradd *dap;
5344 	struct vnode *vp;
5345 	int error = 0;
5346 	struct buf *bp;
5347 	ino_t inum;
5348 
5349 	ump = VFSTOUFS(mp);
5350 	while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
5351 		/*
5352 		 * Flush ourselves if this directory entry
5353 		 * has a MKDIR_PARENT dependency.
5354 		 */
5355 		if (dap->da_state & MKDIR_PARENT) {
5356 			FREE_LOCK(&lk);
5357 			if ((error = UFS_UPDATE(pvp, 1)) != 0)
5358 				break;
5359 			ACQUIRE_LOCK(&lk);
5360 			/*
5361 			 * If that cleared dependencies, go on to next.
5362 			 */
5363 			if (dap != LIST_FIRST(diraddhdp))
5364 				continue;
5365 			if (dap->da_state & MKDIR_PARENT) {
5366 				FREE_LOCK(&lk);
5367 				panic("flush_pagedep_deps: MKDIR_PARENT");
5368 			}
5369 		}
5370 		/*
5371 		 * A newly allocated directory must have its "." and
5372 		 * ".." entries written out before its name can be
5373 		 * committed in its parent. We do not want or need
5374 		 * the full semantics of a synchronous VOP_FSYNC as
5375 		 * that may end up here again, once for each directory
5376 		 * level in the filesystem. Instead, we push the blocks
5377 		 * and wait for them to clear. We have to fsync twice
5378 		 * because the first call may choose to defer blocks
5379 		 * that still have dependencies, but deferral will
5380 		 * happen at most once.
5381 		 */
5382 		inum = dap->da_newinum;
5383 		if (dap->da_state & MKDIR_BODY) {
5384 			FREE_LOCK(&lk);
5385 			if ((error = VFS_VGET(mp, inum, LK_EXCLUSIVE, &vp)))
5386 				break;
5387 			if ((error=VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td)) ||
5388 			    (error=VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td))) {
5389 				vput(vp);
5390 				break;
5391 			}
5392 			VI_LOCK(vp);
5393 			drain_output(vp, 0);
5394 			VI_UNLOCK(vp);
5395 			vput(vp);
5396 			ACQUIRE_LOCK(&lk);
5397 			/*
5398 			 * If that cleared dependencies, go on to next.
5399 			 */
5400 			if (dap != LIST_FIRST(diraddhdp))
5401 				continue;
5402 			if (dap->da_state & MKDIR_BODY) {
5403 				FREE_LOCK(&lk);
5404 				panic("flush_pagedep_deps: MKDIR_BODY");
5405 			}
5406 		}
5407 		/*
5408 		 * Flush the inode on which the directory entry depends.
5409 		 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
5410 		 * the only remaining dependency is that the updated inode
5411 		 * count must get pushed to disk. The inode has already
5412 		 * been pushed into its inode buffer (via VOP_UPDATE) at
5413 		 * the time of the reference count change. So we need only
5414 		 * locate that buffer, ensure that there will be no rollback
5415 		 * caused by a bitmap dependency, then write the inode buffer.
5416 		 */
5417 		if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0) {
5418 			FREE_LOCK(&lk);
5419 			panic("flush_pagedep_deps: lost inode");
5420 		}
5421 		/*
5422 		 * If the inode still has bitmap dependencies,
5423 		 * push them to disk.
5424 		 */
5425 		if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5426 			bp = inodedep->id_buf;
5427 			bp = getdirtybuf(&bp, NULL, MNT_WAIT);
5428 			FREE_LOCK(&lk);
5429 			if (bp && (error = bwrite(bp)) != 0)
5430 				break;
5431 			ACQUIRE_LOCK(&lk);
5432 			if (dap != LIST_FIRST(diraddhdp))
5433 				continue;
5434 		}
5435 		/*
5436 		 * If the inode is still sitting in a buffer waiting
5437 		 * to be written, push it to disk.
5438 		 */
5439 		FREE_LOCK(&lk);
5440 		if ((error = bread(ump->um_devvp,
5441 		    fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
5442 		    (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) {
5443 			brelse(bp);
5444 			break;
5445 		}
5446 		if ((error = bwrite(bp)) != 0)
5447 			break;
5448 		ACQUIRE_LOCK(&lk);
5449 		/*
5450 		 * If we have failed to get rid of all the dependencies
5451 		 * then something is seriously wrong.
5452 		 */
5453 		if (dap == LIST_FIRST(diraddhdp)) {
5454 			FREE_LOCK(&lk);
5455 			panic("flush_pagedep_deps: flush failed");
5456 		}
5457 	}
5458 	if (error)
5459 		ACQUIRE_LOCK(&lk);
5460 	return (error);
5461 }
5462 
5463 /*
5464  * A large burst of file addition or deletion activity can drive the
5465  * memory load excessively high. First attempt to slow things down
5466  * using the techniques below. If that fails, this routine requests
5467  * the offending operations to fall back to running synchronously
5468  * until the memory load returns to a reasonable level.
5469  */
5470 int
5471 softdep_slowdown(vp)
5472 	struct vnode *vp;
5473 {
5474 	int max_softdeps_hard;
5475 
5476 	max_softdeps_hard = max_softdeps * 11 / 10;
5477 	if (num_dirrem < max_softdeps_hard / 2 &&
5478 	    num_inodedep < max_softdeps_hard &&
5479 	    VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps)
5480   		return (0);
5481 	if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps)
5482 		speedup_syncer();
5483 	stat_sync_limit_hit += 1;
5484 	return (1);
5485 }
5486 
5487 /*
5488  * Called by the allocation routines when they are about to fail
5489  * in the hope that we can free up some disk space.
5490  *
5491  * First check to see if the work list has anything on it. If it has,
5492  * clean up entries until we successfully free some space. Because this
5493  * process holds inodes locked, we cannot handle any remove requests
5494  * that might block on a locked inode as that could lead to deadlock.
5495  * If the worklist yields no free space, encourage the syncer daemon
5496  * to help us. In no event will we try for longer than tickdelay seconds.
5497  */
5498 int
5499 softdep_request_cleanup(fs, vp)
5500 	struct fs *fs;
5501 	struct vnode *vp;
5502 {
5503 	long starttime;
5504 	ufs2_daddr_t needed;
5505 
5506 	needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize;
5507 	starttime = time_second + tickdelay;
5508 	/*
5509 	 * If we are being called because of a process doing a
5510 	 * copy-on-write, then it is not safe to update the vnode
5511 	 * as we may recurse into the copy-on-write routine.
5512 	 */
5513 	if (!(curthread->td_pflags & TDP_COWINPROGRESS) &&
5514 	    UFS_UPDATE(vp, 1) != 0)
5515 		return (0);
5516 	while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) {
5517 		if (time_second > starttime)
5518 			return (0);
5519 		if (num_on_worklist > 0 &&
5520 		    process_worklist_item(NULL, LK_NOWAIT) != -1) {
5521 			stat_worklist_push += 1;
5522 			continue;
5523 		}
5524 		request_cleanup(FLUSH_REMOVE_WAIT, 0);
5525 	}
5526 	return (1);
5527 }
5528 
5529 /*
5530  * If memory utilization has gotten too high, deliberately slow things
5531  * down and speed up the I/O processing.
5532  */
5533 static int
5534 request_cleanup(resource, islocked)
5535 	int resource;
5536 	int islocked;
5537 {
5538 	struct thread *td = curthread;
5539 
5540 	/*
5541 	 * We never hold up the filesystem syncer process.
5542 	 */
5543 	if (td == filesys_syncer)
5544 		return (0);
5545 	/*
5546 	 * First check to see if the work list has gotten backlogged.
5547 	 * If it has, co-opt this process to help clean up two entries.
5548 	 * Because this process may hold inodes locked, we cannot
5549 	 * handle any remove requests that might block on a locked
5550 	 * inode as that could lead to deadlock.
5551 	 */
5552 	if (num_on_worklist > max_softdeps / 10) {
5553 		if (islocked)
5554 			FREE_LOCK(&lk);
5555 		process_worklist_item(NULL, LK_NOWAIT);
5556 		process_worklist_item(NULL, LK_NOWAIT);
5557 		stat_worklist_push += 2;
5558 		if (islocked)
5559 			ACQUIRE_LOCK(&lk);
5560 		return(1);
5561 	}
5562 	/*
5563 	 * Next, we attempt to speed up the syncer process. If that
5564 	 * is successful, then we allow the process to continue.
5565 	 */
5566 	if (speedup_syncer() && resource != FLUSH_REMOVE_WAIT)
5567 		return(0);
5568 	/*
5569 	 * If we are resource constrained on inode dependencies, try
5570 	 * flushing some dirty inodes. Otherwise, we are constrained
5571 	 * by file deletions, so try accelerating flushes of directories
5572 	 * with removal dependencies. We would like to do the cleanup
5573 	 * here, but we probably hold an inode locked at this point and
5574 	 * that might deadlock against one that we try to clean. So,
5575 	 * the best that we can do is request the syncer daemon to do
5576 	 * the cleanup for us.
5577 	 */
5578 	switch (resource) {
5579 
5580 	case FLUSH_INODES:
5581 		stat_ino_limit_push += 1;
5582 		req_clear_inodedeps += 1;
5583 		stat_countp = &stat_ino_limit_hit;
5584 		break;
5585 
5586 	case FLUSH_REMOVE:
5587 	case FLUSH_REMOVE_WAIT:
5588 		stat_blk_limit_push += 1;
5589 		req_clear_remove += 1;
5590 		stat_countp = &stat_blk_limit_hit;
5591 		break;
5592 
5593 	default:
5594 		if (islocked)
5595 			FREE_LOCK(&lk);
5596 		panic("request_cleanup: unknown type");
5597 	}
5598 	/*
5599 	 * Hopefully the syncer daemon will catch up and awaken us.
5600 	 * We wait at most tickdelay before proceeding in any case.
5601 	 */
5602 	if (islocked == 0)
5603 		ACQUIRE_LOCK(&lk);
5604 	proc_waiting += 1;
5605 	if (handle.callout == NULL)
5606 		handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
5607 	interlocked_sleep(&lk, SLEEP, (caddr_t)&proc_waiting, NULL, PPAUSE,
5608 	    "softupdate", 0);
5609 	proc_waiting -= 1;
5610 	if (islocked == 0)
5611 		FREE_LOCK(&lk);
5612 	return (1);
5613 }
5614 
5615 /*
5616  * Awaken processes pausing in request_cleanup and clear proc_waiting
5617  * to indicate that there is no longer a timer running.
5618  */
5619 static void
5620 pause_timer(arg)
5621 	void *arg;
5622 {
5623 
5624 	*stat_countp += 1;
5625 	wakeup_one(&proc_waiting);
5626 	if (proc_waiting > 0)
5627 		handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
5628 	else
5629 		handle.callout = NULL;
5630 }
5631 
5632 /*
5633  * Flush out a directory with at least one removal dependency in an effort to
5634  * reduce the number of dirrem, freefile, and freeblks dependency structures.
5635  */
5636 static void
5637 clear_remove(td)
5638 	struct thread *td;
5639 {
5640 	struct pagedep_hashhead *pagedephd;
5641 	struct pagedep *pagedep;
5642 	static int next = 0;
5643 	struct mount *mp;
5644 	struct vnode *vp;
5645 	int error, cnt;
5646 	ino_t ino;
5647 
5648 	ACQUIRE_LOCK(&lk);
5649 	for (cnt = 0; cnt < pagedep_hash; cnt++) {
5650 		pagedephd = &pagedep_hashtbl[next++];
5651 		if (next >= pagedep_hash)
5652 			next = 0;
5653 		LIST_FOREACH(pagedep, pagedephd, pd_hash) {
5654 			if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL)
5655 				continue;
5656 			mp = pagedep->pd_mnt;
5657 			ino = pagedep->pd_ino;
5658 			if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5659 				continue;
5660 			FREE_LOCK(&lk);
5661 			if ((error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &vp))) {
5662 				softdep_error("clear_remove: vget", error);
5663 				vn_finished_write(mp);
5664 				return;
5665 			}
5666 			if ((error = VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td)))
5667 				softdep_error("clear_remove: fsync", error);
5668 			VI_LOCK(vp);
5669 			drain_output(vp, 0);
5670 			VI_UNLOCK(vp);
5671 			vput(vp);
5672 			vn_finished_write(mp);
5673 			return;
5674 		}
5675 	}
5676 	FREE_LOCK(&lk);
5677 }
5678 
5679 /*
5680  * Clear out a block of dirty inodes in an effort to reduce
5681  * the number of inodedep dependency structures.
5682  */
5683 static void
5684 clear_inodedeps(td)
5685 	struct thread *td;
5686 {
5687 	struct inodedep_hashhead *inodedephd;
5688 	struct inodedep *inodedep;
5689 	static int next = 0;
5690 	struct mount *mp;
5691 	struct vnode *vp;
5692 	struct fs *fs;
5693 	int error, cnt;
5694 	ino_t firstino, lastino, ino;
5695 
5696 	ACQUIRE_LOCK(&lk);
5697 	/*
5698 	 * Pick a random inode dependency to be cleared.
5699 	 * We will then gather up all the inodes in its block
5700 	 * that have dependencies and flush them out.
5701 	 */
5702 	for (cnt = 0; cnt < inodedep_hash; cnt++) {
5703 		inodedephd = &inodedep_hashtbl[next++];
5704 		if (next >= inodedep_hash)
5705 			next = 0;
5706 		if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
5707 			break;
5708 	}
5709 	if (inodedep == NULL)
5710 		return;
5711 	/*
5712 	 * Ugly code to find mount point given pointer to superblock.
5713 	 */
5714 	fs = inodedep->id_fs;
5715 	TAILQ_FOREACH(mp, &mountlist, mnt_list)
5716 		if ((mp->mnt_flag & MNT_SOFTDEP) && fs == VFSTOUFS(mp)->um_fs)
5717 			break;
5718 	/*
5719 	 * Find the last inode in the block with dependencies.
5720 	 */
5721 	firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
5722 	for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
5723 		if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
5724 			break;
5725 	/*
5726 	 * Asynchronously push all but the last inode with dependencies.
5727 	 * Synchronously push the last inode with dependencies to ensure
5728 	 * that the inode block gets written to free up the inodedeps.
5729 	 */
5730 	for (ino = firstino; ino <= lastino; ino++) {
5731 		if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
5732 			continue;
5733 		if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5734 			continue;
5735 		FREE_LOCK(&lk);
5736 		if ((error = VFS_VGET(mp, ino, LK_EXCLUSIVE, &vp)) != 0) {
5737 			softdep_error("clear_inodedeps: vget", error);
5738 			vn_finished_write(mp);
5739 			return;
5740 		}
5741 		if (ino == lastino) {
5742 			if ((error = VOP_FSYNC(vp, td->td_ucred, MNT_WAIT, td)))
5743 				softdep_error("clear_inodedeps: fsync1", error);
5744 		} else {
5745 			if ((error = VOP_FSYNC(vp, td->td_ucred, MNT_NOWAIT, td)))
5746 				softdep_error("clear_inodedeps: fsync2", error);
5747 			VI_LOCK(vp);
5748 			drain_output(vp, 0);
5749 			VI_UNLOCK(vp);
5750 		}
5751 		vput(vp);
5752 		vn_finished_write(mp);
5753 		ACQUIRE_LOCK(&lk);
5754 	}
5755 	FREE_LOCK(&lk);
5756 }
5757 
5758 /*
5759  * Function to determine if the buffer has outstanding dependencies
5760  * that will cause a roll-back if the buffer is written. If wantcount
5761  * is set, return number of dependencies, otherwise just yes or no.
5762  */
5763 static int
5764 softdep_count_dependencies(bp, wantcount)
5765 	struct buf *bp;
5766 	int wantcount;
5767 {
5768 	struct worklist *wk;
5769 	struct inodedep *inodedep;
5770 	struct indirdep *indirdep;
5771 	struct allocindir *aip;
5772 	struct pagedep *pagedep;
5773 	struct diradd *dap;
5774 	int i, retval;
5775 
5776 	retval = 0;
5777 	ACQUIRE_LOCK(&lk);
5778 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5779 		switch (wk->wk_type) {
5780 
5781 		case D_INODEDEP:
5782 			inodedep = WK_INODEDEP(wk);
5783 			if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5784 				/* bitmap allocation dependency */
5785 				retval += 1;
5786 				if (!wantcount)
5787 					goto out;
5788 			}
5789 			if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
5790 				/* direct block pointer dependency */
5791 				retval += 1;
5792 				if (!wantcount)
5793 					goto out;
5794 			}
5795 			if (TAILQ_FIRST(&inodedep->id_extupdt)) {
5796 				/* direct block pointer dependency */
5797 				retval += 1;
5798 				if (!wantcount)
5799 					goto out;
5800 			}
5801 			continue;
5802 
5803 		case D_INDIRDEP:
5804 			indirdep = WK_INDIRDEP(wk);
5805 
5806 			LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
5807 				/* indirect block pointer dependency */
5808 				retval += 1;
5809 				if (!wantcount)
5810 					goto out;
5811 			}
5812 			continue;
5813 
5814 		case D_PAGEDEP:
5815 			pagedep = WK_PAGEDEP(wk);
5816 			for (i = 0; i < DAHASHSZ; i++) {
5817 
5818 				LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
5819 					/* directory entry dependency */
5820 					retval += 1;
5821 					if (!wantcount)
5822 						goto out;
5823 				}
5824 			}
5825 			continue;
5826 
5827 		case D_BMSAFEMAP:
5828 		case D_ALLOCDIRECT:
5829 		case D_ALLOCINDIR:
5830 		case D_MKDIR:
5831 			/* never a dependency on these blocks */
5832 			continue;
5833 
5834 		default:
5835 			FREE_LOCK(&lk);
5836 			panic("softdep_check_for_rollback: Unexpected type %s",
5837 			    TYPENAME(wk->wk_type));
5838 			/* NOTREACHED */
5839 		}
5840 	}
5841 out:
5842 	FREE_LOCK(&lk);
5843 	return retval;
5844 }
5845 
5846 /*
5847  * Acquire exclusive access to a buffer.
5848  * Must be called with splbio blocked.
5849  * Return acquired buffer or NULL on failure.  mtx, if provided, will be
5850  * released on success but held on failure.
5851  */
5852 static struct buf *
5853 getdirtybuf(bpp, mtx, waitfor)
5854 	struct buf **bpp;
5855 	struct mtx *mtx;
5856 	int waitfor;
5857 {
5858 	struct buf *bp;
5859 	int error;
5860 
5861 	/*
5862 	 * XXX This code and the code that calls it need to be reviewed to
5863 	 * verify its use of the vnode interlock.
5864 	 */
5865 
5866 	for (;;) {
5867 		if ((bp = *bpp) == NULL)
5868 			return (0);
5869 		if (bp->b_vp == NULL)
5870 			kdb_backtrace();
5871 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
5872 			if ((bp->b_vflags & BV_BKGRDINPROG) == 0)
5873 				break;
5874 			BUF_UNLOCK(bp);
5875 			if (waitfor != MNT_WAIT)
5876 				return (NULL);
5877 			/*
5878 			 * The mtx argument must be bp->b_vp's mutex in
5879 			 * this case.
5880 			 */
5881 #ifdef	DEBUG_VFS_LOCKS
5882 			if (bp->b_vp->v_type != VCHR)
5883 				ASSERT_VI_LOCKED(bp->b_vp, "getdirtybuf");
5884 #endif
5885 			bp->b_vflags |= BV_BKGRDWAIT;
5886 			interlocked_sleep(&lk, SLEEP, &bp->b_xflags, mtx,
5887 			    PRIBIO, "getbuf", 0);
5888 			continue;
5889 		}
5890 		if (waitfor != MNT_WAIT)
5891 			return (NULL);
5892 		if (mtx) {
5893 			error = interlocked_sleep(&lk, LOCKBUF, bp, mtx,
5894 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 0, 0);
5895 			mtx_lock(mtx);
5896 		} else
5897 			error = interlocked_sleep(&lk, LOCKBUF, bp, NULL,
5898 			    LK_EXCLUSIVE | LK_SLEEPFAIL, 0, 0);
5899 		if (error != ENOLCK) {
5900 			FREE_LOCK(&lk);
5901 			panic("getdirtybuf: inconsistent lock");
5902 		}
5903 	}
5904 	if ((bp->b_flags & B_DELWRI) == 0) {
5905 		BUF_UNLOCK(bp);
5906 		return (NULL);
5907 	}
5908 	if (mtx)
5909 		mtx_unlock(mtx);
5910 	bremfree(bp);
5911 	return (bp);
5912 }
5913 
5914 /*
5915  * Wait for pending output on a vnode to complete.
5916  * Must be called with vnode lock and interlock locked.
5917  */
5918 static void
5919 drain_output(vp, islocked)
5920 	struct vnode *vp;
5921 	int islocked;
5922 {
5923 	ASSERT_VOP_LOCKED(vp, "drain_output");
5924 	ASSERT_VI_LOCKED(vp, "drain_output");
5925 
5926 	if (!islocked)
5927 		ACQUIRE_LOCK(&lk);
5928 	while (vp->v_numoutput) {
5929 		vp->v_iflag |= VI_BWAIT;
5930 		interlocked_sleep(&lk, SLEEP, (caddr_t)&vp->v_numoutput,
5931 		    VI_MTX(vp), PRIBIO + 1, "drainvp", 0);
5932 	}
5933 	if (!islocked)
5934 		FREE_LOCK(&lk);
5935 }
5936 
5937 /*
5938  * Called whenever a buffer that is being invalidated or reallocated
5939  * contains dependencies. This should only happen if an I/O error has
5940  * occurred. The routine is called with the buffer locked.
5941  */
5942 static void
5943 softdep_deallocate_dependencies(bp)
5944 	struct buf *bp;
5945 {
5946 
5947 	if ((bp->b_ioflags & BIO_ERROR) == 0)
5948 		panic("softdep_deallocate_dependencies: dangling deps");
5949 	softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
5950 	panic("softdep_deallocate_dependencies: unrecovered I/O error");
5951 }
5952 
5953 /*
5954  * Function to handle asynchronous write errors in the filesystem.
5955  */
5956 static void
5957 softdep_error(func, error)
5958 	char *func;
5959 	int error;
5960 {
5961 
5962 	/* XXX should do something better! */
5963 	printf("%s: got error %d while accessing filesystem\n", func, error);
5964 }
5965