xref: /freebsd/sys/ufs/ffs/ffs_softdep.c (revision f15e18a642cb3f7ebc747f8e9cdf11274140107d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 1998, 2000 Marshall Kirk McKusick.
5  * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
6  * All rights reserved.
7  *
8  * The soft updates code is derived from the appendix of a University
9  * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
10  * "Soft Updates: A Solution to the Metadata Update Problem in File
11  * Systems", CSE-TR-254-95, August 1995).
12  *
13  * Further information about soft updates can be obtained from:
14  *
15  *	Marshall Kirk McKusick		http://www.mckusick.com/softdep/
16  *	1614 Oxford Street		mckusick@mckusick.com
17  *	Berkeley, CA 94709-1608		+1-510-843-9542
18  *	USA
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  * 1. Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in the
28  *    documentation and/or other materials provided with the distribution.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
36  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  *	from: @(#)ffs_softdep.c	9.59 (McKusick) 6/21/00
42  */
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include "opt_ffs.h"
48 #include "opt_quota.h"
49 #include "opt_ddb.h"
50 
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/kdb.h>
57 #include <sys/kthread.h>
58 #include <sys/ktr.h>
59 #include <sys/limits.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
65 #include <sys/priv.h>
66 #include <sys/proc.h>
67 #include <sys/racct.h>
68 #include <sys/rwlock.h>
69 #include <sys/stat.h>
70 #include <sys/sysctl.h>
71 #include <sys/syslog.h>
72 #include <sys/vnode.h>
73 #include <sys/conf.h>
74 
75 #include <ufs/ufs/dir.h>
76 #include <ufs/ufs/extattr.h>
77 #include <ufs/ufs/quota.h>
78 #include <ufs/ufs/inode.h>
79 #include <ufs/ufs/ufsmount.h>
80 #include <ufs/ffs/fs.h>
81 #include <ufs/ffs/softdep.h>
82 #include <ufs/ffs/ffs_extern.h>
83 #include <ufs/ufs/ufs_extern.h>
84 
85 #include <vm/vm.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_object.h>
88 
89 #include <geom/geom.h>
90 #include <geom/geom_vfs.h>
91 
92 #include <ddb/ddb.h>
93 
94 #define	KTR_SUJ	0	/* Define to KTR_SPARE. */
95 
96 #ifndef SOFTUPDATES
97 
98 int
99 softdep_flushfiles(oldmnt, flags, td)
100 	struct mount *oldmnt;
101 	int flags;
102 	struct thread *td;
103 {
104 
105 	panic("softdep_flushfiles called");
106 }
107 
108 int
109 softdep_mount(devvp, mp, fs, cred)
110 	struct vnode *devvp;
111 	struct mount *mp;
112 	struct fs *fs;
113 	struct ucred *cred;
114 {
115 
116 	return (0);
117 }
118 
119 void
120 softdep_initialize()
121 {
122 
123 	return;
124 }
125 
126 void
127 softdep_uninitialize()
128 {
129 
130 	return;
131 }
132 
133 void
134 softdep_unmount(mp)
135 	struct mount *mp;
136 {
137 
138 	panic("softdep_unmount called");
139 }
140 
141 void
142 softdep_setup_sbupdate(ump, fs, bp)
143 	struct ufsmount *ump;
144 	struct fs *fs;
145 	struct buf *bp;
146 {
147 
148 	panic("softdep_setup_sbupdate called");
149 }
150 
151 void
152 softdep_setup_inomapdep(bp, ip, newinum, mode)
153 	struct buf *bp;
154 	struct inode *ip;
155 	ino_t newinum;
156 	int mode;
157 {
158 
159 	panic("softdep_setup_inomapdep called");
160 }
161 
162 void
163 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
164 	struct buf *bp;
165 	struct mount *mp;
166 	ufs2_daddr_t newblkno;
167 	int frags;
168 	int oldfrags;
169 {
170 
171 	panic("softdep_setup_blkmapdep called");
172 }
173 
174 void
175 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
176 	struct inode *ip;
177 	ufs_lbn_t lbn;
178 	ufs2_daddr_t newblkno;
179 	ufs2_daddr_t oldblkno;
180 	long newsize;
181 	long oldsize;
182 	struct buf *bp;
183 {
184 
185 	panic("softdep_setup_allocdirect called");
186 }
187 
188 void
189 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
190 	struct inode *ip;
191 	ufs_lbn_t lbn;
192 	ufs2_daddr_t newblkno;
193 	ufs2_daddr_t oldblkno;
194 	long newsize;
195 	long oldsize;
196 	struct buf *bp;
197 {
198 
199 	panic("softdep_setup_allocext called");
200 }
201 
202 void
203 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
204 	struct inode *ip;
205 	ufs_lbn_t lbn;
206 	struct buf *bp;
207 	int ptrno;
208 	ufs2_daddr_t newblkno;
209 	ufs2_daddr_t oldblkno;
210 	struct buf *nbp;
211 {
212 
213 	panic("softdep_setup_allocindir_page called");
214 }
215 
216 void
217 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
218 	struct buf *nbp;
219 	struct inode *ip;
220 	struct buf *bp;
221 	int ptrno;
222 	ufs2_daddr_t newblkno;
223 {
224 
225 	panic("softdep_setup_allocindir_meta called");
226 }
227 
228 void
229 softdep_journal_freeblocks(ip, cred, length, flags)
230 	struct inode *ip;
231 	struct ucred *cred;
232 	off_t length;
233 	int flags;
234 {
235 
236 	panic("softdep_journal_freeblocks called");
237 }
238 
239 void
240 softdep_journal_fsync(ip)
241 	struct inode *ip;
242 {
243 
244 	panic("softdep_journal_fsync called");
245 }
246 
247 void
248 softdep_setup_freeblocks(ip, length, flags)
249 	struct inode *ip;
250 	off_t length;
251 	int flags;
252 {
253 
254 	panic("softdep_setup_freeblocks called");
255 }
256 
257 void
258 softdep_freefile(pvp, ino, mode)
259 		struct vnode *pvp;
260 		ino_t ino;
261 		int mode;
262 {
263 
264 	panic("softdep_freefile called");
265 }
266 
267 int
268 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
269 	struct buf *bp;
270 	struct inode *dp;
271 	off_t diroffset;
272 	ino_t newinum;
273 	struct buf *newdirbp;
274 	int isnewblk;
275 {
276 
277 	panic("softdep_setup_directory_add called");
278 }
279 
280 void
281 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
282 	struct buf *bp;
283 	struct inode *dp;
284 	caddr_t base;
285 	caddr_t oldloc;
286 	caddr_t newloc;
287 	int entrysize;
288 {
289 
290 	panic("softdep_change_directoryentry_offset called");
291 }
292 
293 void
294 softdep_setup_remove(bp, dp, ip, isrmdir)
295 	struct buf *bp;
296 	struct inode *dp;
297 	struct inode *ip;
298 	int isrmdir;
299 {
300 
301 	panic("softdep_setup_remove called");
302 }
303 
304 void
305 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
306 	struct buf *bp;
307 	struct inode *dp;
308 	struct inode *ip;
309 	ino_t newinum;
310 	int isrmdir;
311 {
312 
313 	panic("softdep_setup_directory_change called");
314 }
315 
316 void
317 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
318 	struct mount *mp;
319 	struct buf *bp;
320 	ufs2_daddr_t blkno;
321 	int frags;
322 	struct workhead *wkhd;
323 {
324 
325 	panic("%s called", __FUNCTION__);
326 }
327 
328 void
329 softdep_setup_inofree(mp, bp, ino, wkhd)
330 	struct mount *mp;
331 	struct buf *bp;
332 	ino_t ino;
333 	struct workhead *wkhd;
334 {
335 
336 	panic("%s called", __FUNCTION__);
337 }
338 
339 void
340 softdep_setup_unlink(dp, ip)
341 	struct inode *dp;
342 	struct inode *ip;
343 {
344 
345 	panic("%s called", __FUNCTION__);
346 }
347 
348 void
349 softdep_setup_link(dp, ip)
350 	struct inode *dp;
351 	struct inode *ip;
352 {
353 
354 	panic("%s called", __FUNCTION__);
355 }
356 
357 void
358 softdep_revert_link(dp, ip)
359 	struct inode *dp;
360 	struct inode *ip;
361 {
362 
363 	panic("%s called", __FUNCTION__);
364 }
365 
366 void
367 softdep_setup_rmdir(dp, ip)
368 	struct inode *dp;
369 	struct inode *ip;
370 {
371 
372 	panic("%s called", __FUNCTION__);
373 }
374 
375 void
376 softdep_revert_rmdir(dp, ip)
377 	struct inode *dp;
378 	struct inode *ip;
379 {
380 
381 	panic("%s called", __FUNCTION__);
382 }
383 
384 void
385 softdep_setup_create(dp, ip)
386 	struct inode *dp;
387 	struct inode *ip;
388 {
389 
390 	panic("%s called", __FUNCTION__);
391 }
392 
393 void
394 softdep_revert_create(dp, ip)
395 	struct inode *dp;
396 	struct inode *ip;
397 {
398 
399 	panic("%s called", __FUNCTION__);
400 }
401 
402 void
403 softdep_setup_mkdir(dp, ip)
404 	struct inode *dp;
405 	struct inode *ip;
406 {
407 
408 	panic("%s called", __FUNCTION__);
409 }
410 
411 void
412 softdep_revert_mkdir(dp, ip)
413 	struct inode *dp;
414 	struct inode *ip;
415 {
416 
417 	panic("%s called", __FUNCTION__);
418 }
419 
420 void
421 softdep_setup_dotdot_link(dp, ip)
422 	struct inode *dp;
423 	struct inode *ip;
424 {
425 
426 	panic("%s called", __FUNCTION__);
427 }
428 
429 int
430 softdep_prealloc(vp, waitok)
431 	struct vnode *vp;
432 	int waitok;
433 {
434 
435 	panic("%s called", __FUNCTION__);
436 }
437 
438 int
439 softdep_journal_lookup(mp, vpp)
440 	struct mount *mp;
441 	struct vnode **vpp;
442 {
443 
444 	return (ENOENT);
445 }
446 
447 void
448 softdep_change_linkcnt(ip)
449 	struct inode *ip;
450 {
451 
452 	panic("softdep_change_linkcnt called");
453 }
454 
455 void
456 softdep_load_inodeblock(ip)
457 	struct inode *ip;
458 {
459 
460 	panic("softdep_load_inodeblock called");
461 }
462 
463 void
464 softdep_update_inodeblock(ip, bp, waitfor)
465 	struct inode *ip;
466 	struct buf *bp;
467 	int waitfor;
468 {
469 
470 	panic("softdep_update_inodeblock called");
471 }
472 
473 int
474 softdep_fsync(vp)
475 	struct vnode *vp;	/* the "in_core" copy of the inode */
476 {
477 
478 	return (0);
479 }
480 
481 void
482 softdep_fsync_mountdev(vp)
483 	struct vnode *vp;
484 {
485 
486 	return;
487 }
488 
489 int
490 softdep_flushworklist(oldmnt, countp, td)
491 	struct mount *oldmnt;
492 	int *countp;
493 	struct thread *td;
494 {
495 
496 	*countp = 0;
497 	return (0);
498 }
499 
500 int
501 softdep_sync_metadata(struct vnode *vp)
502 {
503 
504 	panic("softdep_sync_metadata called");
505 }
506 
507 int
508 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
509 {
510 
511 	panic("softdep_sync_buf called");
512 }
513 
514 int
515 softdep_slowdown(vp)
516 	struct vnode *vp;
517 {
518 
519 	panic("softdep_slowdown called");
520 }
521 
522 int
523 softdep_request_cleanup(fs, vp, cred, resource)
524 	struct fs *fs;
525 	struct vnode *vp;
526 	struct ucred *cred;
527 	int resource;
528 {
529 
530 	return (0);
531 }
532 
533 int
534 softdep_check_suspend(struct mount *mp,
535 		      struct vnode *devvp,
536 		      int softdep_depcnt,
537 		      int softdep_accdepcnt,
538 		      int secondary_writes,
539 		      int secondary_accwrites)
540 {
541 	struct bufobj *bo;
542 	int error;
543 
544 	(void) softdep_depcnt,
545 	(void) softdep_accdepcnt;
546 
547 	bo = &devvp->v_bufobj;
548 	ASSERT_BO_WLOCKED(bo);
549 
550 	MNT_ILOCK(mp);
551 	while (mp->mnt_secondary_writes != 0) {
552 		BO_UNLOCK(bo);
553 		msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
554 		    (PUSER - 1) | PDROP, "secwr", 0);
555 		BO_LOCK(bo);
556 		MNT_ILOCK(mp);
557 	}
558 
559 	/*
560 	 * Reasons for needing more work before suspend:
561 	 * - Dirty buffers on devvp.
562 	 * - Secondary writes occurred after start of vnode sync loop
563 	 */
564 	error = 0;
565 	if (bo->bo_numoutput > 0 ||
566 	    bo->bo_dirty.bv_cnt > 0 ||
567 	    secondary_writes != 0 ||
568 	    mp->mnt_secondary_writes != 0 ||
569 	    secondary_accwrites != mp->mnt_secondary_accwrites)
570 		error = EAGAIN;
571 	BO_UNLOCK(bo);
572 	return (error);
573 }
574 
575 void
576 softdep_get_depcounts(struct mount *mp,
577 		      int *softdepactivep,
578 		      int *softdepactiveaccp)
579 {
580 	(void) mp;
581 	*softdepactivep = 0;
582 	*softdepactiveaccp = 0;
583 }
584 
585 void
586 softdep_buf_append(bp, wkhd)
587 	struct buf *bp;
588 	struct workhead *wkhd;
589 {
590 
591 	panic("softdep_buf_appendwork called");
592 }
593 
594 void
595 softdep_inode_append(ip, cred, wkhd)
596 	struct inode *ip;
597 	struct ucred *cred;
598 	struct workhead *wkhd;
599 {
600 
601 	panic("softdep_inode_appendwork called");
602 }
603 
604 void
605 softdep_freework(wkhd)
606 	struct workhead *wkhd;
607 {
608 
609 	panic("softdep_freework called");
610 }
611 
612 int
613 softdep_prerename(fdvp, fvp, tdvp, tvp)
614 	struct vnode *fdvp;
615 	struct vnode *fvp;
616 	struct vnode *tdvp;
617 	struct vnode *tvp;
618 {
619 
620 	panic("softdep_prerename called");
621 }
622 
623 int
624 softdep_prelink(dvp, vp)
625 	struct vnode *dvp;
626 	struct vnode *vp;
627 {
628 
629 	panic("softdep_prelink called");
630 }
631 
632 #else
633 
634 FEATURE(softupdates, "FFS soft-updates support");
635 
636 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
637     "soft updates stats");
638 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total,
639     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
640     "total dependencies allocated");
641 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse,
642     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
643     "high use dependencies allocated");
644 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current,
645     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
646     "current dependencies allocated");
647 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write,
648     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
649     "current dependencies written");
650 
651 unsigned long dep_current[D_LAST + 1];
652 unsigned long dep_highuse[D_LAST + 1];
653 unsigned long dep_total[D_LAST + 1];
654 unsigned long dep_write[D_LAST + 1];
655 
656 #define	SOFTDEP_TYPE(type, str, long)					\
657     static MALLOC_DEFINE(M_ ## type, #str, long);			\
658     SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD,	\
659 	&dep_total[D_ ## type], 0, "");					\
660     SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, 	\
661 	&dep_current[D_ ## type], 0, "");				\
662     SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, 	\
663 	&dep_highuse[D_ ## type], 0, "");				\
664     SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, 	\
665 	&dep_write[D_ ## type], 0, "");
666 
667 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
668 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
669 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
670     "Block or frag allocated from cyl group map");
671 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
672 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
673 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
674 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
675 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
676 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
677 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
678 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
679 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
680 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
681 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
682 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
683 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
684 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
685 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
686 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
687 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
688 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
689 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
690 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
691 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
692 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
693 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
694 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
695 
696 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
697 
698 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
699 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
700 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
701 
702 #define M_SOFTDEP_FLAGS	(M_WAITOK)
703 
704 /*
705  * translate from workitem type to memory type
706  * MUST match the defines above, such that memtype[D_XXX] == M_XXX
707  */
708 static struct malloc_type *memtype[] = {
709 	NULL,
710 	M_PAGEDEP,
711 	M_INODEDEP,
712 	M_BMSAFEMAP,
713 	M_NEWBLK,
714 	M_ALLOCDIRECT,
715 	M_INDIRDEP,
716 	M_ALLOCINDIR,
717 	M_FREEFRAG,
718 	M_FREEBLKS,
719 	M_FREEFILE,
720 	M_DIRADD,
721 	M_MKDIR,
722 	M_DIRREM,
723 	M_NEWDIRBLK,
724 	M_FREEWORK,
725 	M_FREEDEP,
726 	M_JADDREF,
727 	M_JREMREF,
728 	M_JMVREF,
729 	M_JNEWBLK,
730 	M_JFREEBLK,
731 	M_JFREEFRAG,
732 	M_JSEG,
733 	M_JSEGDEP,
734 	M_SBDEP,
735 	M_JTRUNC,
736 	M_JFSYNC,
737 	M_SENTINEL
738 };
739 
740 #define DtoM(type) (memtype[type])
741 
742 /*
743  * Names of malloc types.
744  */
745 #define TYPENAME(type)  \
746 	((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
747 	memtype[type]->ks_shortdesc : "???")
748 /*
749  * End system adaptation definitions.
750  */
751 
752 #define	DOTDOT_OFFSET	offsetof(struct dirtemplate, dotdot_ino)
753 #define	DOT_OFFSET	offsetof(struct dirtemplate, dot_ino)
754 
755 /*
756  * Internal function prototypes.
757  */
758 static	void check_clear_deps(struct mount *);
759 static	void softdep_error(char *, int);
760 static	int softdep_prerename_vnode(struct ufsmount *, struct vnode *);
761 static	int softdep_process_worklist(struct mount *, int);
762 static	int softdep_waitidle(struct mount *, int);
763 static	void drain_output(struct vnode *);
764 static	struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
765 static	int check_inodedep_free(struct inodedep *);
766 static	void clear_remove(struct mount *);
767 static	void clear_inodedeps(struct mount *);
768 static	void unlinked_inodedep(struct mount *, struct inodedep *);
769 static	void clear_unlinked_inodedep(struct inodedep *);
770 static	struct inodedep *first_unlinked_inodedep(struct ufsmount *);
771 static	int flush_pagedep_deps(struct vnode *, struct mount *,
772 	    struct diraddhd *, struct buf *);
773 static	int free_pagedep(struct pagedep *);
774 static	int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
775 static	int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
776 static	int flush_deplist(struct allocdirectlst *, int, int *);
777 static	int sync_cgs(struct mount *, int);
778 static	int handle_written_filepage(struct pagedep *, struct buf *, int);
779 static	int handle_written_sbdep(struct sbdep *, struct buf *);
780 static	void initiate_write_sbdep(struct sbdep *);
781 static	void diradd_inode_written(struct diradd *, struct inodedep *);
782 static	int handle_written_indirdep(struct indirdep *, struct buf *,
783 	    struct buf**, int);
784 static	int handle_written_inodeblock(struct inodedep *, struct buf *, int);
785 static	int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
786 	    uint8_t *);
787 static	int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
788 static	void handle_written_jaddref(struct jaddref *);
789 static	void handle_written_jremref(struct jremref *);
790 static	void handle_written_jseg(struct jseg *, struct buf *);
791 static	void handle_written_jnewblk(struct jnewblk *);
792 static	void handle_written_jblkdep(struct jblkdep *);
793 static	void handle_written_jfreefrag(struct jfreefrag *);
794 static	void complete_jseg(struct jseg *);
795 static	void complete_jsegs(struct jseg *);
796 static	void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
797 static	void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
798 static	void jremref_write(struct jremref *, struct jseg *, uint8_t *);
799 static	void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
800 static	void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
801 static	void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
802 static	void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
803 static	void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
804 static	void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
805 static	inline void inoref_write(struct inoref *, struct jseg *,
806 	    struct jrefrec *);
807 static	void handle_allocdirect_partdone(struct allocdirect *,
808 	    struct workhead *);
809 static	struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
810 	    struct workhead *);
811 static	void indirdep_complete(struct indirdep *);
812 static	int indirblk_lookup(struct mount *, ufs2_daddr_t);
813 static	void indirblk_insert(struct freework *);
814 static	void indirblk_remove(struct freework *);
815 static	void handle_allocindir_partdone(struct allocindir *);
816 static	void initiate_write_filepage(struct pagedep *, struct buf *);
817 static	void initiate_write_indirdep(struct indirdep*, struct buf *);
818 static	void handle_written_mkdir(struct mkdir *, int);
819 static	int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
820 	    uint8_t *);
821 static	void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
822 static	void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
823 static	void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
824 static	void handle_workitem_freefile(struct freefile *);
825 static	int handle_workitem_remove(struct dirrem *, int);
826 static	struct dirrem *newdirrem(struct buf *, struct inode *,
827 	    struct inode *, int, struct dirrem **);
828 static	struct indirdep *indirdep_lookup(struct mount *, struct inode *,
829 	    struct buf *);
830 static	void cancel_indirdep(struct indirdep *, struct buf *,
831 	    struct freeblks *);
832 static	void free_indirdep(struct indirdep *);
833 static	void free_diradd(struct diradd *, struct workhead *);
834 static	void merge_diradd(struct inodedep *, struct diradd *);
835 static	void complete_diradd(struct diradd *);
836 static	struct diradd *diradd_lookup(struct pagedep *, int);
837 static	struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
838 	    struct jremref *);
839 static	struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
840 	    struct jremref *);
841 static	void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
842 	    struct jremref *, struct jremref *);
843 static	void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
844 	    struct jremref *);
845 static	void cancel_allocindir(struct allocindir *, struct buf *bp,
846 	    struct freeblks *, int);
847 static	int setup_trunc_indir(struct freeblks *, struct inode *,
848 	    ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
849 static	void complete_trunc_indir(struct freework *);
850 static	void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
851 	    int);
852 static	void complete_mkdir(struct mkdir *);
853 static	void free_newdirblk(struct newdirblk *);
854 static	void free_jremref(struct jremref *);
855 static	void free_jaddref(struct jaddref *);
856 static	void free_jsegdep(struct jsegdep *);
857 static	void free_jsegs(struct jblocks *);
858 static	void rele_jseg(struct jseg *);
859 static	void free_jseg(struct jseg *, struct jblocks *);
860 static	void free_jnewblk(struct jnewblk *);
861 static	void free_jblkdep(struct jblkdep *);
862 static	void free_jfreefrag(struct jfreefrag *);
863 static	void free_freedep(struct freedep *);
864 static	void journal_jremref(struct dirrem *, struct jremref *,
865 	    struct inodedep *);
866 static	void cancel_jnewblk(struct jnewblk *, struct workhead *);
867 static	int cancel_jaddref(struct jaddref *, struct inodedep *,
868 	    struct workhead *);
869 static	void cancel_jfreefrag(struct jfreefrag *);
870 static	inline void setup_freedirect(struct freeblks *, struct inode *,
871 	    int, int);
872 static	inline void setup_freeext(struct freeblks *, struct inode *, int, int);
873 static	inline void setup_freeindir(struct freeblks *, struct inode *, int,
874 	    ufs_lbn_t, int);
875 static	inline struct freeblks *newfreeblks(struct mount *, struct inode *);
876 static	void freeblks_free(struct ufsmount *, struct freeblks *, int);
877 static	void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
878 static	ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
879 static	int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
880 static	void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
881 	    int, int);
882 static	void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
883 static 	int cancel_pagedep(struct pagedep *, struct freeblks *, int);
884 static	int deallocate_dependencies(struct buf *, struct freeblks *, int);
885 static	void newblk_freefrag(struct newblk*);
886 static	void free_newblk(struct newblk *);
887 static	void cancel_allocdirect(struct allocdirectlst *,
888 	    struct allocdirect *, struct freeblks *);
889 static	int check_inode_unwritten(struct inodedep *);
890 static	int free_inodedep(struct inodedep *);
891 static	void freework_freeblock(struct freework *, u_long);
892 static	void freework_enqueue(struct freework *);
893 static	int handle_workitem_freeblocks(struct freeblks *, int);
894 static	int handle_complete_freeblocks(struct freeblks *, int);
895 static	void handle_workitem_indirblk(struct freework *);
896 static	void handle_written_freework(struct freework *);
897 static	void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
898 static	struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
899 	    struct workhead *);
900 static	struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
901 	    struct inodedep *, struct allocindir *, ufs_lbn_t);
902 static	struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
903 	    ufs2_daddr_t, ufs_lbn_t);
904 static	void handle_workitem_freefrag(struct freefrag *);
905 static	struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
906 	    ufs_lbn_t, u_long);
907 static	void allocdirect_merge(struct allocdirectlst *,
908 	    struct allocdirect *, struct allocdirect *);
909 static	struct freefrag *allocindir_merge(struct allocindir *,
910 	    struct allocindir *);
911 static	int bmsafemap_find(struct bmsafemap_hashhead *, int,
912 	    struct bmsafemap **);
913 static	struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
914 	    int cg, struct bmsafemap *);
915 static	int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
916 	    struct newblk **);
917 static	int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
918 static	int inodedep_find(struct inodedep_hashhead *, ino_t,
919 	    struct inodedep **);
920 static	int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
921 static	int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
922 	    int, struct pagedep **);
923 static	int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
924 	    struct pagedep **);
925 static	void pause_timer(void *);
926 static	int request_cleanup(struct mount *, int);
927 static	int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
928 static	void schedule_cleanup(struct mount *);
929 static void softdep_ast_cleanup_proc(struct thread *);
930 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
931 static	int process_worklist_item(struct mount *, int, int);
932 static	void process_removes(struct vnode *);
933 static	void process_truncates(struct vnode *);
934 static	void jwork_move(struct workhead *, struct workhead *);
935 static	void jwork_insert(struct workhead *, struct jsegdep *);
936 static	void add_to_worklist(struct worklist *, int);
937 static	void wake_worklist(struct worklist *);
938 static	void wait_worklist(struct worklist *, char *);
939 static	void remove_from_worklist(struct worklist *);
940 static	void softdep_flush(void *);
941 static	void softdep_flushjournal(struct mount *);
942 static	int softdep_speedup(struct ufsmount *);
943 static	void worklist_speedup(struct mount *);
944 static	int journal_mount(struct mount *, struct fs *, struct ucred *);
945 static	void journal_unmount(struct ufsmount *);
946 static	int journal_space(struct ufsmount *, int);
947 static	void journal_suspend(struct ufsmount *);
948 static	int journal_unsuspend(struct ufsmount *ump);
949 static	void add_to_journal(struct worklist *);
950 static	void remove_from_journal(struct worklist *);
951 static	bool softdep_excess_items(struct ufsmount *, int);
952 static	void softdep_process_journal(struct mount *, struct worklist *, int);
953 static	struct jremref *newjremref(struct dirrem *, struct inode *,
954 	    struct inode *ip, off_t, nlink_t);
955 static	struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
956 	    uint16_t);
957 static	inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
958 	    uint16_t);
959 static	inline struct jsegdep *inoref_jseg(struct inoref *);
960 static	struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
961 static	struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
962 	    ufs2_daddr_t, int);
963 static	void adjust_newfreework(struct freeblks *, int);
964 static	struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
965 static	void move_newblock_dep(struct jaddref *, struct inodedep *);
966 static	void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
967 static	struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
968 	    ufs2_daddr_t, long, ufs_lbn_t);
969 static	struct freework *newfreework(struct ufsmount *, struct freeblks *,
970 	    struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
971 static	int jwait(struct worklist *, int);
972 static	struct inodedep *inodedep_lookup_ip(struct inode *);
973 static	int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
974 static	struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
975 static	void handle_jwork(struct workhead *);
976 static	struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
977 	    struct mkdir **);
978 static	struct jblocks *jblocks_create(void);
979 static	ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
980 static	void jblocks_free(struct jblocks *, struct mount *, int);
981 static	void jblocks_destroy(struct jblocks *);
982 static	void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
983 
984 /*
985  * Exported softdep operations.
986  */
987 static	void softdep_disk_io_initiation(struct buf *);
988 static	void softdep_disk_write_complete(struct buf *);
989 static	void softdep_deallocate_dependencies(struct buf *);
990 static	int softdep_count_dependencies(struct buf *bp, int);
991 
992 /*
993  * Global lock over all of soft updates.
994  */
995 static struct mtx lk;
996 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF);
997 
998 #define ACQUIRE_GBLLOCK(lk)	mtx_lock(lk)
999 #define FREE_GBLLOCK(lk)	mtx_unlock(lk)
1000 #define GBLLOCK_OWNED(lk)	mtx_assert((lk), MA_OWNED)
1001 
1002 /*
1003  * Per-filesystem soft-updates locking.
1004  */
1005 #define LOCK_PTR(ump)		(&(ump)->um_softdep->sd_fslock)
1006 #define TRY_ACQUIRE_LOCK(ump)	rw_try_wlock(&(ump)->um_softdep->sd_fslock)
1007 #define ACQUIRE_LOCK(ump)	rw_wlock(&(ump)->um_softdep->sd_fslock)
1008 #define FREE_LOCK(ump)		rw_wunlock(&(ump)->um_softdep->sd_fslock)
1009 #define LOCK_OWNED(ump)		rw_assert(&(ump)->um_softdep->sd_fslock, \
1010 				    RA_WLOCKED)
1011 
1012 #define	BUF_AREC(bp)		lockallowrecurse(&(bp)->b_lock)
1013 #define	BUF_NOREC(bp)		lockdisablerecurse(&(bp)->b_lock)
1014 
1015 /*
1016  * Worklist queue management.
1017  * These routines require that the lock be held.
1018  */
1019 #ifndef /* NOT */ INVARIANTS
1020 #define WORKLIST_INSERT(head, item) do {	\
1021 	(item)->wk_state |= ONWORKLIST;		\
1022 	LIST_INSERT_HEAD(head, item, wk_list);	\
1023 } while (0)
1024 #define WORKLIST_REMOVE(item) do {		\
1025 	(item)->wk_state &= ~ONWORKLIST;	\
1026 	LIST_REMOVE(item, wk_list);		\
1027 } while (0)
1028 #define WORKLIST_INSERT_UNLOCKED	WORKLIST_INSERT
1029 #define WORKLIST_REMOVE_UNLOCKED	WORKLIST_REMOVE
1030 
1031 #else /* INVARIANTS */
1032 static	void worklist_insert(struct workhead *, struct worklist *, int,
1033 	const char *, int);
1034 static	void worklist_remove(struct worklist *, int, const char *, int);
1035 
1036 #define WORKLIST_INSERT(head, item) \
1037 	worklist_insert(head, item, 1, __func__, __LINE__)
1038 #define WORKLIST_INSERT_UNLOCKED(head, item)\
1039 	worklist_insert(head, item, 0, __func__, __LINE__)
1040 #define WORKLIST_REMOVE(item)\
1041 	worklist_remove(item, 1, __func__, __LINE__)
1042 #define WORKLIST_REMOVE_UNLOCKED(item)\
1043 	worklist_remove(item, 0, __func__, __LINE__)
1044 
1045 static void
1046 worklist_insert(head, item, locked, func, line)
1047 	struct workhead *head;
1048 	struct worklist *item;
1049 	int locked;
1050 	const char *func;
1051 	int line;
1052 {
1053 
1054 	if (locked)
1055 		LOCK_OWNED(VFSTOUFS(item->wk_mp));
1056 	if (item->wk_state & ONWORKLIST)
1057 		panic("worklist_insert: %p %s(0x%X) already on list, "
1058 		    "added in function %s at line %d",
1059 		    item, TYPENAME(item->wk_type), item->wk_state,
1060 		    item->wk_func, item->wk_line);
1061 	item->wk_state |= ONWORKLIST;
1062 	item->wk_func = func;
1063 	item->wk_line = line;
1064 	LIST_INSERT_HEAD(head, item, wk_list);
1065 }
1066 
1067 static void
1068 worklist_remove(item, locked, func, line)
1069 	struct worklist *item;
1070 	int locked;
1071 	const char *func;
1072 	int line;
1073 {
1074 
1075 	if (locked)
1076 		LOCK_OWNED(VFSTOUFS(item->wk_mp));
1077 	if ((item->wk_state & ONWORKLIST) == 0)
1078 		panic("worklist_remove: %p %s(0x%X) not on list, "
1079 		    "removed in function %s at line %d",
1080 		    item, TYPENAME(item->wk_type), item->wk_state,
1081 		    item->wk_func, item->wk_line);
1082 	item->wk_state &= ~ONWORKLIST;
1083 	item->wk_func = func;
1084 	item->wk_line = line;
1085 	LIST_REMOVE(item, wk_list);
1086 }
1087 #endif /* INVARIANTS */
1088 
1089 /*
1090  * Merge two jsegdeps keeping only the oldest one as newer references
1091  * can't be discarded until after older references.
1092  */
1093 static inline struct jsegdep *
1094 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1095 {
1096 	struct jsegdep *swp;
1097 
1098 	if (two == NULL)
1099 		return (one);
1100 
1101 	if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1102 		swp = one;
1103 		one = two;
1104 		two = swp;
1105 	}
1106 	WORKLIST_REMOVE(&two->jd_list);
1107 	free_jsegdep(two);
1108 
1109 	return (one);
1110 }
1111 
1112 /*
1113  * If two freedeps are compatible free one to reduce list size.
1114  */
1115 static inline struct freedep *
1116 freedep_merge(struct freedep *one, struct freedep *two)
1117 {
1118 	if (two == NULL)
1119 		return (one);
1120 
1121 	if (one->fd_freework == two->fd_freework) {
1122 		WORKLIST_REMOVE(&two->fd_list);
1123 		free_freedep(two);
1124 	}
1125 	return (one);
1126 }
1127 
1128 /*
1129  * Move journal work from one list to another.  Duplicate freedeps and
1130  * jsegdeps are coalesced to keep the lists as small as possible.
1131  */
1132 static void
1133 jwork_move(dst, src)
1134 	struct workhead *dst;
1135 	struct workhead *src;
1136 {
1137 	struct freedep *freedep;
1138 	struct jsegdep *jsegdep;
1139 	struct worklist *wkn;
1140 	struct worklist *wk;
1141 
1142 	KASSERT(dst != src,
1143 	    ("jwork_move: dst == src"));
1144 	freedep = NULL;
1145 	jsegdep = NULL;
1146 	LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1147 		if (wk->wk_type == D_JSEGDEP)
1148 			jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1149 		else if (wk->wk_type == D_FREEDEP)
1150 			freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1151 	}
1152 
1153 	while ((wk = LIST_FIRST(src)) != NULL) {
1154 		WORKLIST_REMOVE(wk);
1155 		WORKLIST_INSERT(dst, wk);
1156 		if (wk->wk_type == D_JSEGDEP) {
1157 			jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1158 			continue;
1159 		}
1160 		if (wk->wk_type == D_FREEDEP)
1161 			freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1162 	}
1163 }
1164 
1165 static void
1166 jwork_insert(dst, jsegdep)
1167 	struct workhead *dst;
1168 	struct jsegdep *jsegdep;
1169 {
1170 	struct jsegdep *jsegdepn;
1171 	struct worklist *wk;
1172 
1173 	LIST_FOREACH(wk, dst, wk_list)
1174 		if (wk->wk_type == D_JSEGDEP)
1175 			break;
1176 	if (wk == NULL) {
1177 		WORKLIST_INSERT(dst, &jsegdep->jd_list);
1178 		return;
1179 	}
1180 	jsegdepn = WK_JSEGDEP(wk);
1181 	if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1182 		WORKLIST_REMOVE(wk);
1183 		free_jsegdep(jsegdepn);
1184 		WORKLIST_INSERT(dst, &jsegdep->jd_list);
1185 	} else
1186 		free_jsegdep(jsegdep);
1187 }
1188 
1189 /*
1190  * Routines for tracking and managing workitems.
1191  */
1192 static	void workitem_free(struct worklist *, int);
1193 static	void workitem_alloc(struct worklist *, int, struct mount *);
1194 static	void workitem_reassign(struct worklist *, int);
1195 
1196 #define	WORKITEM_FREE(item, type) \
1197 	workitem_free((struct worklist *)(item), (type))
1198 #define	WORKITEM_REASSIGN(item, type) \
1199 	workitem_reassign((struct worklist *)(item), (type))
1200 
1201 static void
1202 workitem_free(item, type)
1203 	struct worklist *item;
1204 	int type;
1205 {
1206 	struct ufsmount *ump;
1207 
1208 #ifdef INVARIANTS
1209 	if (item->wk_state & ONWORKLIST)
1210 		panic("workitem_free: %s(0x%X) still on list, "
1211 		    "added in function %s at line %d",
1212 		    TYPENAME(item->wk_type), item->wk_state,
1213 		    item->wk_func, item->wk_line);
1214 	if (item->wk_type != type && type != D_NEWBLK)
1215 		panic("workitem_free: type mismatch %s != %s",
1216 		    TYPENAME(item->wk_type), TYPENAME(type));
1217 #endif
1218 	if (item->wk_state & IOWAITING)
1219 		wakeup(item);
1220 	ump = VFSTOUFS(item->wk_mp);
1221 	LOCK_OWNED(ump);
1222 	KASSERT(ump->softdep_deps > 0,
1223 	    ("workitem_free: %s: softdep_deps going negative",
1224 	    ump->um_fs->fs_fsmnt));
1225 	if (--ump->softdep_deps == 0 && ump->softdep_req)
1226 		wakeup(&ump->softdep_deps);
1227 	KASSERT(dep_current[item->wk_type] > 0,
1228 	    ("workitem_free: %s: dep_current[%s] going negative",
1229 	    ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1230 	KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1231 	    ("workitem_free: %s: softdep_curdeps[%s] going negative",
1232 	    ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1233 	atomic_subtract_long(&dep_current[item->wk_type], 1);
1234 	ump->softdep_curdeps[item->wk_type] -= 1;
1235 #ifdef INVARIANTS
1236 	LIST_REMOVE(item, wk_all);
1237 #endif
1238 	free(item, DtoM(type));
1239 }
1240 
1241 static void
1242 workitem_alloc(item, type, mp)
1243 	struct worklist *item;
1244 	int type;
1245 	struct mount *mp;
1246 {
1247 	struct ufsmount *ump;
1248 
1249 	item->wk_type = type;
1250 	item->wk_mp = mp;
1251 	item->wk_state = 0;
1252 
1253 	ump = VFSTOUFS(mp);
1254 	ACQUIRE_GBLLOCK(&lk);
1255 	dep_current[type]++;
1256 	if (dep_current[type] > dep_highuse[type])
1257 		dep_highuse[type] = dep_current[type];
1258 	dep_total[type]++;
1259 	FREE_GBLLOCK(&lk);
1260 	ACQUIRE_LOCK(ump);
1261 	ump->softdep_curdeps[type] += 1;
1262 	ump->softdep_deps++;
1263 	ump->softdep_accdeps++;
1264 #ifdef INVARIANTS
1265 	LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all);
1266 #endif
1267 	FREE_LOCK(ump);
1268 }
1269 
1270 static void
1271 workitem_reassign(item, newtype)
1272 	struct worklist *item;
1273 	int newtype;
1274 {
1275 	struct ufsmount *ump;
1276 
1277 	ump = VFSTOUFS(item->wk_mp);
1278 	LOCK_OWNED(ump);
1279 	KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1280 	    ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1281 	    VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1282 	ump->softdep_curdeps[item->wk_type] -= 1;
1283 	ump->softdep_curdeps[newtype] += 1;
1284 	KASSERT(dep_current[item->wk_type] > 0,
1285 	    ("workitem_reassign: %s: dep_current[%s] going negative",
1286 	    VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1287 	ACQUIRE_GBLLOCK(&lk);
1288 	dep_current[newtype]++;
1289 	dep_current[item->wk_type]--;
1290 	if (dep_current[newtype] > dep_highuse[newtype])
1291 		dep_highuse[newtype] = dep_current[newtype];
1292 	dep_total[newtype]++;
1293 	FREE_GBLLOCK(&lk);
1294 	item->wk_type = newtype;
1295 }
1296 
1297 /*
1298  * Workitem queue management
1299  */
1300 static int max_softdeps;	/* maximum number of structs before slowdown */
1301 static int tickdelay = 2;	/* number of ticks to pause during slowdown */
1302 static int proc_waiting;	/* tracks whether we have a timeout posted */
1303 static int *stat_countp;	/* statistic to count in proc_waiting timeout */
1304 static struct callout softdep_callout;
1305 static int req_clear_inodedeps;	/* syncer process flush some inodedeps */
1306 static int req_clear_remove;	/* syncer process flush some freeblks */
1307 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1308 
1309 /*
1310  * runtime statistics
1311  */
1312 static int stat_flush_threads;	/* number of softdep flushing threads */
1313 static int stat_worklist_push;	/* number of worklist cleanups */
1314 static int stat_delayed_inact;	/* number of delayed inactivation cleanups */
1315 static int stat_blk_limit_push;	/* number of times block limit neared */
1316 static int stat_ino_limit_push;	/* number of times inode limit neared */
1317 static int stat_blk_limit_hit;	/* number of times block slowdown imposed */
1318 static int stat_ino_limit_hit;	/* number of times inode slowdown imposed */
1319 static int stat_sync_limit_hit;	/* number of synchronous slowdowns imposed */
1320 static int stat_indir_blk_ptrs;	/* bufs redirtied as indir ptrs not written */
1321 static int stat_inode_bitmap;	/* bufs redirtied as inode bitmap not written */
1322 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1323 static int stat_dir_entry;	/* bufs redirtied as dir entry cannot write */
1324 static int stat_jaddref;	/* bufs redirtied as ino bitmap can not write */
1325 static int stat_jnewblk;	/* bufs redirtied as blk bitmap can not write */
1326 static int stat_journal_min;	/* Times hit journal min threshold */
1327 static int stat_journal_low;	/* Times hit journal low threshold */
1328 static int stat_journal_wait;	/* Times blocked in jwait(). */
1329 static int stat_jwait_filepage;	/* Times blocked in jwait() for filepage. */
1330 static int stat_jwait_freeblks;	/* Times blocked in jwait() for freeblks. */
1331 static int stat_jwait_inode;	/* Times blocked in jwait() for inodes. */
1332 static int stat_jwait_newblk;	/* Times blocked in jwait() for newblks. */
1333 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1334 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1335 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1336 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1337 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1338 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1339 
1340 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1341     &max_softdeps, 0, "");
1342 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1343     &tickdelay, 0, "");
1344 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1345     &stat_flush_threads, 0, "");
1346 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push,
1347     CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,"");
1348 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD,
1349     &stat_delayed_inact, 0, "");
1350 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push,
1351     CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,"");
1352 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push,
1353     CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,"");
1354 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit,
1355     CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, "");
1356 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit,
1357     CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, "");
1358 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit,
1359     CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, "");
1360 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs,
1361     CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, "");
1362 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap,
1363     CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, "");
1364 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs,
1365     CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, "");
1366 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry,
1367     CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, "");
1368 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback,
1369     CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, "");
1370 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback,
1371     CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, "");
1372 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low,
1373     CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, "");
1374 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min,
1375     CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, "");
1376 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait,
1377     CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, "");
1378 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage,
1379     CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, "");
1380 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks,
1381     CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, "");
1382 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode,
1383     CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, "");
1384 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk,
1385     CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, "");
1386 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests,
1387     CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, "");
1388 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests,
1389     CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, "");
1390 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay,
1391     CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, "");
1392 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries,
1393     CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, "");
1394 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures,
1395     CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, "");
1396 
1397 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1398     &softdep_flushcache, 0, "");
1399 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1400     &stat_emptyjblocks, 0, "");
1401 
1402 SYSCTL_DECL(_vfs_ffs);
1403 
1404 /* Whether to recompute the summary at mount time */
1405 static int compute_summary_at_mount = 0;
1406 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1407 	   &compute_summary_at_mount, 0, "Recompute summary at mount");
1408 static int print_threads = 0;
1409 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1410     &print_threads, 0, "Notify flusher thread start/stop");
1411 
1412 /* List of all filesystems mounted with soft updates */
1413 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1414 
1415 static void
1416 get_parent_vp_unlock_bp(struct mount *mp, struct buf *bp,
1417     struct diraddhd *diraddhdp, struct diraddhd *unfinishedp)
1418 {
1419 	struct diradd *dap;
1420 
1421 	/*
1422 	 * Requeue unfinished dependencies before
1423 	 * unlocking buffer, which could make
1424 	 * diraddhdp invalid.
1425 	 */
1426 	ACQUIRE_LOCK(VFSTOUFS(mp));
1427 	while ((dap = LIST_FIRST(unfinishedp)) != NULL) {
1428 		LIST_REMOVE(dap, da_pdlist);
1429 		LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
1430 	}
1431 	FREE_LOCK(VFSTOUFS(mp));
1432 
1433 	bp->b_vflags &= ~BV_SCANNED;
1434 	BUF_NOREC(bp);
1435 	BUF_UNLOCK(bp);
1436 }
1437 
1438 /*
1439  * This function fetches inode inum on mount point mp.  We already
1440  * hold a locked vnode vp, and might have a locked buffer bp belonging
1441  * to vp.
1442 
1443  * We must not block on acquiring the new inode lock as we will get
1444  * into a lock-order reversal with the buffer lock and possibly get a
1445  * deadlock.  Thus if we cannot instantiate the requested vnode
1446  * without sleeping on its lock, we must unlock the vnode and the
1447  * buffer before doing a blocking on the vnode lock.  We return
1448  * ERELOOKUP if we have had to unlock either the vnode or the buffer so
1449  * that the caller can reassess its state.
1450  *
1451  * Top-level VFS code (for syscalls and other consumers, e.g. callers
1452  * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe
1453  * point.
1454  *
1455  * Since callers expect to operate on fully constructed vnode, we also
1456  * recheck v_data after relock, and return ENOENT if NULL.
1457  *
1458  * If unlocking bp, we must unroll dequeueing its unfinished
1459  * dependencies, and clear scan flag, before unlocking.  If unlocking
1460  * vp while it is under deactivation, we re-queue deactivation.
1461  */
1462 static int
1463 get_parent_vp(struct vnode *vp, struct mount *mp, ino_t inum, struct buf *bp,
1464     struct diraddhd *diraddhdp, struct diraddhd *unfinishedp,
1465     struct vnode **rvp)
1466 {
1467 	struct vnode *pvp;
1468 	int error;
1469 	bool bplocked;
1470 
1471 	ASSERT_VOP_ELOCKED(vp, "child vnode must be locked");
1472 	for (bplocked = true, pvp = NULL;;) {
1473 		error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp,
1474 		    FFSV_FORCEINSMQ);
1475 		if (error == 0) {
1476 			/*
1477 			 * Since we could have unlocked vp, the inode
1478 			 * number could no longer indicate a
1479 			 * constructed node.  In this case, we must
1480 			 * restart the syscall.
1481 			 */
1482 			if (VTOI(pvp)->i_mode == 0 || !bplocked) {
1483 				if (bp != NULL && bplocked)
1484 					get_parent_vp_unlock_bp(mp, bp,
1485 					    diraddhdp, unfinishedp);
1486 				if (VTOI(pvp)->i_mode == 0)
1487 					vgone(pvp);
1488 				error = ERELOOKUP;
1489 				goto out2;
1490 			}
1491 			goto out1;
1492 		}
1493 		if (bp != NULL && bplocked) {
1494 			get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp);
1495 			bplocked = false;
1496 		}
1497 
1498 		/*
1499 		 * Do not drop vnode lock while inactivating during
1500 		 * vunref.  This would result in leaks of the VI flags
1501 		 * and reclaiming of non-truncated vnode.  Instead,
1502 		 * re-schedule inactivation hoping that we would be
1503 		 * able to sync inode later.
1504 		 */
1505 		if ((vp->v_iflag & VI_DOINGINACT) != 0 &&
1506 		    (vp->v_vflag & VV_UNREF) != 0) {
1507 			VI_LOCK(vp);
1508 			vp->v_iflag |= VI_OWEINACT;
1509 			VI_UNLOCK(vp);
1510 			return (ERELOOKUP);
1511 		}
1512 
1513 		VOP_UNLOCK(vp);
1514 		error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp,
1515 		    FFSV_FORCEINSMQ);
1516 		if (error != 0) {
1517 			MPASS(error != ERELOOKUP);
1518 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1519 			break;
1520 		}
1521 		if (VTOI(pvp)->i_mode == 0) {
1522 			vgone(pvp);
1523 			vput(pvp);
1524 			pvp = NULL;
1525 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1526 			error = ERELOOKUP;
1527 			break;
1528 		}
1529 		error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
1530 		if (error == 0)
1531 			break;
1532 		vput(pvp);
1533 		pvp = NULL;
1534 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1535 		if (vp->v_data == NULL) {
1536 			error = ENOENT;
1537 			break;
1538 		}
1539 	}
1540 	if (bp != NULL) {
1541 		MPASS(!bplocked);
1542 		error = ERELOOKUP;
1543 	}
1544 out2:
1545 	if (error != 0 && pvp != NULL) {
1546 		vput(pvp);
1547 		pvp = NULL;
1548 	}
1549 out1:
1550 	*rvp = pvp;
1551 	ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return");
1552 	return (error);
1553 }
1554 
1555 /*
1556  * This function cleans the worklist for a filesystem.
1557  * Each filesystem running with soft dependencies gets its own
1558  * thread to run in this function. The thread is started up in
1559  * softdep_mount and shutdown in softdep_unmount. They show up
1560  * as part of the kernel "bufdaemon" process whose process
1561  * entry is available in bufdaemonproc.
1562  */
1563 static int searchfailed;
1564 extern struct proc *bufdaemonproc;
1565 static void
1566 softdep_flush(addr)
1567 	void *addr;
1568 {
1569 	struct mount *mp;
1570 	struct thread *td;
1571 	struct ufsmount *ump;
1572 
1573 	td = curthread;
1574 	td->td_pflags |= TDP_NORUNNINGBUF;
1575 	mp = (struct mount *)addr;
1576 	ump = VFSTOUFS(mp);
1577 	atomic_add_int(&stat_flush_threads, 1);
1578 	ACQUIRE_LOCK(ump);
1579 	ump->softdep_flags &= ~FLUSH_STARTING;
1580 	wakeup(&ump->softdep_flushtd);
1581 	FREE_LOCK(ump);
1582 	if (print_threads) {
1583 		if (stat_flush_threads == 1)
1584 			printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1585 			    bufdaemonproc->p_pid);
1586 		printf("Start thread %s\n", td->td_name);
1587 	}
1588 	for (;;) {
1589 		while (softdep_process_worklist(mp, 0) > 0 ||
1590 		    (MOUNTEDSUJ(mp) &&
1591 		    VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1592 			kthread_suspend_check();
1593 		ACQUIRE_LOCK(ump);
1594 		if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1595 			msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1596 			    "sdflush", hz / 2);
1597 		ump->softdep_flags &= ~FLUSH_CLEANUP;
1598 		/*
1599 		 * Check to see if we are done and need to exit.
1600 		 */
1601 		if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1602 			FREE_LOCK(ump);
1603 			continue;
1604 		}
1605 		ump->softdep_flags &= ~FLUSH_EXIT;
1606 		FREE_LOCK(ump);
1607 		wakeup(&ump->softdep_flags);
1608 		if (print_threads)
1609 			printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups);
1610 		atomic_subtract_int(&stat_flush_threads, 1);
1611 		kthread_exit();
1612 		panic("kthread_exit failed\n");
1613 	}
1614 }
1615 
1616 static void
1617 worklist_speedup(mp)
1618 	struct mount *mp;
1619 {
1620 	struct ufsmount *ump;
1621 
1622 	ump = VFSTOUFS(mp);
1623 	LOCK_OWNED(ump);
1624 	if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1625 		ump->softdep_flags |= FLUSH_CLEANUP;
1626 	wakeup(&ump->softdep_flushtd);
1627 }
1628 
1629 static void
1630 softdep_send_speedup(struct ufsmount *ump, off_t shortage, u_int flags)
1631 {
1632 	struct buf *bp;
1633 
1634 	if ((ump->um_flags & UM_CANSPEEDUP) == 0)
1635 		return;
1636 
1637 	bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO);
1638 	bp->b_iocmd = BIO_SPEEDUP;
1639 	bp->b_ioflags = flags;
1640 	bp->b_bcount = omin(shortage, LONG_MAX);
1641 	g_vfs_strategy(ump->um_bo, bp);
1642 	bufwait(bp);
1643 	free(bp, M_TRIM);
1644 }
1645 
1646 static int
1647 softdep_speedup(ump)
1648 	struct ufsmount *ump;
1649 {
1650 	struct ufsmount *altump;
1651 	struct mount_softdeps *sdp;
1652 
1653 	LOCK_OWNED(ump);
1654 	worklist_speedup(ump->um_mountp);
1655 	bd_speedup();
1656 	/*
1657 	 * If we have global shortages, then we need other
1658 	 * filesystems to help with the cleanup. Here we wakeup a
1659 	 * flusher thread for a filesystem that is over its fair
1660 	 * share of resources.
1661 	 */
1662 	if (req_clear_inodedeps || req_clear_remove) {
1663 		ACQUIRE_GBLLOCK(&lk);
1664 		TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1665 			if ((altump = sdp->sd_ump) == ump)
1666 				continue;
1667 			if (((req_clear_inodedeps &&
1668 			    altump->softdep_curdeps[D_INODEDEP] >
1669 			    max_softdeps / stat_flush_threads) ||
1670 			    (req_clear_remove &&
1671 			    altump->softdep_curdeps[D_DIRREM] >
1672 			    (max_softdeps / 2) / stat_flush_threads)) &&
1673 			    TRY_ACQUIRE_LOCK(altump))
1674 				break;
1675 		}
1676 		if (sdp == NULL) {
1677 			searchfailed++;
1678 			FREE_GBLLOCK(&lk);
1679 		} else {
1680 			/*
1681 			 * Move to the end of the list so we pick a
1682 			 * different one on out next try.
1683 			 */
1684 			TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1685 			TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1686 			FREE_GBLLOCK(&lk);
1687 			if ((altump->softdep_flags &
1688 			    (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1689 				altump->softdep_flags |= FLUSH_CLEANUP;
1690 			altump->um_softdep->sd_cleanups++;
1691 			wakeup(&altump->softdep_flushtd);
1692 			FREE_LOCK(altump);
1693 		}
1694 	}
1695 	return (speedup_syncer());
1696 }
1697 
1698 /*
1699  * Add an item to the end of the work queue.
1700  * This routine requires that the lock be held.
1701  * This is the only routine that adds items to the list.
1702  * The following routine is the only one that removes items
1703  * and does so in order from first to last.
1704  */
1705 
1706 #define	WK_HEAD		0x0001	/* Add to HEAD. */
1707 #define	WK_NODELAY	0x0002	/* Process immediately. */
1708 
1709 static void
1710 add_to_worklist(wk, flags)
1711 	struct worklist *wk;
1712 	int flags;
1713 {
1714 	struct ufsmount *ump;
1715 
1716 	ump = VFSTOUFS(wk->wk_mp);
1717 	LOCK_OWNED(ump);
1718 	if (wk->wk_state & ONWORKLIST)
1719 		panic("add_to_worklist: %s(0x%X) already on list",
1720 		    TYPENAME(wk->wk_type), wk->wk_state);
1721 	wk->wk_state |= ONWORKLIST;
1722 	if (ump->softdep_on_worklist == 0) {
1723 		LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1724 		ump->softdep_worklist_tail = wk;
1725 	} else if (flags & WK_HEAD) {
1726 		LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1727 	} else {
1728 		LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1729 		ump->softdep_worklist_tail = wk;
1730 	}
1731 	ump->softdep_on_worklist += 1;
1732 	if (flags & WK_NODELAY)
1733 		worklist_speedup(wk->wk_mp);
1734 }
1735 
1736 /*
1737  * Remove the item to be processed. If we are removing the last
1738  * item on the list, we need to recalculate the tail pointer.
1739  */
1740 static void
1741 remove_from_worklist(wk)
1742 	struct worklist *wk;
1743 {
1744 	struct ufsmount *ump;
1745 
1746 	ump = VFSTOUFS(wk->wk_mp);
1747 	if (ump->softdep_worklist_tail == wk)
1748 		ump->softdep_worklist_tail =
1749 		    (struct worklist *)wk->wk_list.le_prev;
1750 	WORKLIST_REMOVE(wk);
1751 	ump->softdep_on_worklist -= 1;
1752 }
1753 
1754 static void
1755 wake_worklist(wk)
1756 	struct worklist *wk;
1757 {
1758 	if (wk->wk_state & IOWAITING) {
1759 		wk->wk_state &= ~IOWAITING;
1760 		wakeup(wk);
1761 	}
1762 }
1763 
1764 static void
1765 wait_worklist(wk, wmesg)
1766 	struct worklist *wk;
1767 	char *wmesg;
1768 {
1769 	struct ufsmount *ump;
1770 
1771 	ump = VFSTOUFS(wk->wk_mp);
1772 	wk->wk_state |= IOWAITING;
1773 	msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1774 }
1775 
1776 /*
1777  * Process that runs once per second to handle items in the background queue.
1778  *
1779  * Note that we ensure that everything is done in the order in which they
1780  * appear in the queue. The code below depends on this property to ensure
1781  * that blocks of a file are freed before the inode itself is freed. This
1782  * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1783  * until all the old ones have been purged from the dependency lists.
1784  */
1785 static int
1786 softdep_process_worklist(mp, full)
1787 	struct mount *mp;
1788 	int full;
1789 {
1790 	int cnt, matchcnt;
1791 	struct ufsmount *ump;
1792 	long starttime;
1793 
1794 	KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1795 	if (MOUNTEDSOFTDEP(mp) == 0)
1796 		return (0);
1797 	matchcnt = 0;
1798 	ump = VFSTOUFS(mp);
1799 	ACQUIRE_LOCK(ump);
1800 	starttime = time_second;
1801 	softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1802 	check_clear_deps(mp);
1803 	while (ump->softdep_on_worklist > 0) {
1804 		if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1805 			break;
1806 		else
1807 			matchcnt += cnt;
1808 		check_clear_deps(mp);
1809 		/*
1810 		 * We do not generally want to stop for buffer space, but if
1811 		 * we are really being a buffer hog, we will stop and wait.
1812 		 */
1813 		if (should_yield()) {
1814 			FREE_LOCK(ump);
1815 			kern_yield(PRI_USER);
1816 			bwillwrite();
1817 			ACQUIRE_LOCK(ump);
1818 		}
1819 		/*
1820 		 * Never allow processing to run for more than one
1821 		 * second. This gives the syncer thread the opportunity
1822 		 * to pause if appropriate.
1823 		 */
1824 		if (!full && starttime != time_second)
1825 			break;
1826 	}
1827 	if (full == 0)
1828 		journal_unsuspend(ump);
1829 	FREE_LOCK(ump);
1830 	return (matchcnt);
1831 }
1832 
1833 /*
1834  * Process all removes associated with a vnode if we are running out of
1835  * journal space.  Any other process which attempts to flush these will
1836  * be unable as we have the vnodes locked.
1837  */
1838 static void
1839 process_removes(vp)
1840 	struct vnode *vp;
1841 {
1842 	struct inodedep *inodedep;
1843 	struct dirrem *dirrem;
1844 	struct ufsmount *ump;
1845 	struct mount *mp;
1846 	ino_t inum;
1847 
1848 	mp = vp->v_mount;
1849 	ump = VFSTOUFS(mp);
1850 	LOCK_OWNED(ump);
1851 	inum = VTOI(vp)->i_number;
1852 	for (;;) {
1853 top:
1854 		if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1855 			return;
1856 		LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1857 			/*
1858 			 * If another thread is trying to lock this vnode
1859 			 * it will fail but we must wait for it to do so
1860 			 * before we can proceed.
1861 			 */
1862 			if (dirrem->dm_state & INPROGRESS) {
1863 				wait_worklist(&dirrem->dm_list, "pwrwait");
1864 				goto top;
1865 			}
1866 			if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1867 			    (COMPLETE | ONWORKLIST))
1868 				break;
1869 		}
1870 		if (dirrem == NULL)
1871 			return;
1872 		remove_from_worklist(&dirrem->dm_list);
1873 		FREE_LOCK(ump);
1874 		if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1875 			panic("process_removes: suspended filesystem");
1876 		handle_workitem_remove(dirrem, 0);
1877 		vn_finished_secondary_write(mp);
1878 		ACQUIRE_LOCK(ump);
1879 	}
1880 }
1881 
1882 /*
1883  * Process all truncations associated with a vnode if we are running out
1884  * of journal space.  This is called when the vnode lock is already held
1885  * and no other process can clear the truncation.  This function returns
1886  * a value greater than zero if it did any work.
1887  */
1888 static void
1889 process_truncates(vp)
1890 	struct vnode *vp;
1891 {
1892 	struct inodedep *inodedep;
1893 	struct freeblks *freeblks;
1894 	struct ufsmount *ump;
1895 	struct mount *mp;
1896 	ino_t inum;
1897 	int cgwait;
1898 
1899 	mp = vp->v_mount;
1900 	ump = VFSTOUFS(mp);
1901 	LOCK_OWNED(ump);
1902 	inum = VTOI(vp)->i_number;
1903 	for (;;) {
1904 		if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1905 			return;
1906 		cgwait = 0;
1907 		TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1908 			/* Journal entries not yet written.  */
1909 			if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1910 				jwait(&LIST_FIRST(
1911 				    &freeblks->fb_jblkdephd)->jb_list,
1912 				    MNT_WAIT);
1913 				break;
1914 			}
1915 			/* Another thread is executing this item. */
1916 			if (freeblks->fb_state & INPROGRESS) {
1917 				wait_worklist(&freeblks->fb_list, "ptrwait");
1918 				break;
1919 			}
1920 			/* Freeblks is waiting on a inode write. */
1921 			if ((freeblks->fb_state & COMPLETE) == 0) {
1922 				FREE_LOCK(ump);
1923 				ffs_update(vp, 1);
1924 				ACQUIRE_LOCK(ump);
1925 				break;
1926 			}
1927 			if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1928 			    (ALLCOMPLETE | ONWORKLIST)) {
1929 				remove_from_worklist(&freeblks->fb_list);
1930 				freeblks->fb_state |= INPROGRESS;
1931 				FREE_LOCK(ump);
1932 				if (vn_start_secondary_write(NULL, &mp,
1933 				    V_NOWAIT))
1934 					panic("process_truncates: "
1935 					    "suspended filesystem");
1936 				handle_workitem_freeblocks(freeblks, 0);
1937 				vn_finished_secondary_write(mp);
1938 				ACQUIRE_LOCK(ump);
1939 				break;
1940 			}
1941 			if (freeblks->fb_cgwait)
1942 				cgwait++;
1943 		}
1944 		if (cgwait) {
1945 			FREE_LOCK(ump);
1946 			sync_cgs(mp, MNT_WAIT);
1947 			ffs_sync_snap(mp, MNT_WAIT);
1948 			ACQUIRE_LOCK(ump);
1949 			continue;
1950 		}
1951 		if (freeblks == NULL)
1952 			break;
1953 	}
1954 	return;
1955 }
1956 
1957 /*
1958  * Process one item on the worklist.
1959  */
1960 static int
1961 process_worklist_item(mp, target, flags)
1962 	struct mount *mp;
1963 	int target;
1964 	int flags;
1965 {
1966 	struct worklist sentinel;
1967 	struct worklist *wk;
1968 	struct ufsmount *ump;
1969 	int matchcnt;
1970 	int error;
1971 
1972 	KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1973 	/*
1974 	 * If we are being called because of a process doing a
1975 	 * copy-on-write, then it is not safe to write as we may
1976 	 * recurse into the copy-on-write routine.
1977 	 */
1978 	if (curthread->td_pflags & TDP_COWINPROGRESS)
1979 		return (-1);
1980 	PHOLD(curproc);	/* Don't let the stack go away. */
1981 	ump = VFSTOUFS(mp);
1982 	LOCK_OWNED(ump);
1983 	matchcnt = 0;
1984 	sentinel.wk_mp = NULL;
1985 	sentinel.wk_type = D_SENTINEL;
1986 	LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1987 	for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1988 	    wk = LIST_NEXT(&sentinel, wk_list)) {
1989 		if (wk->wk_type == D_SENTINEL) {
1990 			LIST_REMOVE(&sentinel, wk_list);
1991 			LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1992 			continue;
1993 		}
1994 		if (wk->wk_state & INPROGRESS)
1995 			panic("process_worklist_item: %p already in progress.",
1996 			    wk);
1997 		wk->wk_state |= INPROGRESS;
1998 		remove_from_worklist(wk);
1999 		FREE_LOCK(ump);
2000 		if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
2001 			panic("process_worklist_item: suspended filesystem");
2002 		switch (wk->wk_type) {
2003 		case D_DIRREM:
2004 			/* removal of a directory entry */
2005 			error = handle_workitem_remove(WK_DIRREM(wk), flags);
2006 			break;
2007 
2008 		case D_FREEBLKS:
2009 			/* releasing blocks and/or fragments from a file */
2010 			error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
2011 			    flags);
2012 			break;
2013 
2014 		case D_FREEFRAG:
2015 			/* releasing a fragment when replaced as a file grows */
2016 			handle_workitem_freefrag(WK_FREEFRAG(wk));
2017 			error = 0;
2018 			break;
2019 
2020 		case D_FREEFILE:
2021 			/* releasing an inode when its link count drops to 0 */
2022 			handle_workitem_freefile(WK_FREEFILE(wk));
2023 			error = 0;
2024 			break;
2025 
2026 		default:
2027 			panic("%s_process_worklist: Unknown type %s",
2028 			    "softdep", TYPENAME(wk->wk_type));
2029 			/* NOTREACHED */
2030 		}
2031 		vn_finished_secondary_write(mp);
2032 		ACQUIRE_LOCK(ump);
2033 		if (error == 0) {
2034 			if (++matchcnt == target)
2035 				break;
2036 			continue;
2037 		}
2038 		/*
2039 		 * We have to retry the worklist item later.  Wake up any
2040 		 * waiters who may be able to complete it immediately and
2041 		 * add the item back to the head so we don't try to execute
2042 		 * it again.
2043 		 */
2044 		wk->wk_state &= ~INPROGRESS;
2045 		wake_worklist(wk);
2046 		add_to_worklist(wk, WK_HEAD);
2047 	}
2048 	/* Sentinal could've become the tail from remove_from_worklist. */
2049 	if (ump->softdep_worklist_tail == &sentinel)
2050 		ump->softdep_worklist_tail =
2051 		    (struct worklist *)sentinel.wk_list.le_prev;
2052 	LIST_REMOVE(&sentinel, wk_list);
2053 	PRELE(curproc);
2054 	return (matchcnt);
2055 }
2056 
2057 /*
2058  * Move dependencies from one buffer to another.
2059  */
2060 int
2061 softdep_move_dependencies(oldbp, newbp)
2062 	struct buf *oldbp;
2063 	struct buf *newbp;
2064 {
2065 	struct worklist *wk, *wktail;
2066 	struct ufsmount *ump;
2067 	int dirty;
2068 
2069 	if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
2070 		return (0);
2071 	KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
2072 	    ("softdep_move_dependencies called on non-softdep filesystem"));
2073 	dirty = 0;
2074 	wktail = NULL;
2075 	ump = VFSTOUFS(wk->wk_mp);
2076 	ACQUIRE_LOCK(ump);
2077 	while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
2078 		LIST_REMOVE(wk, wk_list);
2079 		if (wk->wk_type == D_BMSAFEMAP &&
2080 		    bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
2081 			dirty = 1;
2082 		if (wktail == NULL)
2083 			LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
2084 		else
2085 			LIST_INSERT_AFTER(wktail, wk, wk_list);
2086 		wktail = wk;
2087 	}
2088 	FREE_LOCK(ump);
2089 
2090 	return (dirty);
2091 }
2092 
2093 /*
2094  * Purge the work list of all items associated with a particular mount point.
2095  */
2096 int
2097 softdep_flushworklist(oldmnt, countp, td)
2098 	struct mount *oldmnt;
2099 	int *countp;
2100 	struct thread *td;
2101 {
2102 	struct vnode *devvp;
2103 	struct ufsmount *ump;
2104 	int count, error;
2105 
2106 	/*
2107 	 * Alternately flush the block device associated with the mount
2108 	 * point and process any dependencies that the flushing
2109 	 * creates. We continue until no more worklist dependencies
2110 	 * are found.
2111 	 */
2112 	*countp = 0;
2113 	error = 0;
2114 	ump = VFSTOUFS(oldmnt);
2115 	devvp = ump->um_devvp;
2116 	while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
2117 		*countp += count;
2118 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2119 		error = VOP_FSYNC(devvp, MNT_WAIT, td);
2120 		VOP_UNLOCK(devvp);
2121 		if (error != 0)
2122 			break;
2123 	}
2124 	return (error);
2125 }
2126 
2127 #define	SU_WAITIDLE_RETRIES	20
2128 static int
2129 softdep_waitidle(struct mount *mp, int flags __unused)
2130 {
2131 	struct ufsmount *ump;
2132 	struct vnode *devvp;
2133 	struct thread *td;
2134 	int error, i;
2135 
2136 	ump = VFSTOUFS(mp);
2137 	devvp = ump->um_devvp;
2138 	td = curthread;
2139 	error = 0;
2140 	ACQUIRE_LOCK(ump);
2141 	for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
2142 		ump->softdep_req = 1;
2143 		KASSERT((flags & FORCECLOSE) == 0 ||
2144 		    ump->softdep_on_worklist == 0,
2145 		    ("softdep_waitidle: work added after flush"));
2146 		msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
2147 		    "softdeps", 10 * hz);
2148 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2149 		error = VOP_FSYNC(devvp, MNT_WAIT, td);
2150 		VOP_UNLOCK(devvp);
2151 		ACQUIRE_LOCK(ump);
2152 		if (error != 0)
2153 			break;
2154 	}
2155 	ump->softdep_req = 0;
2156 	if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
2157 		error = EBUSY;
2158 		printf("softdep_waitidle: Failed to flush worklist for %p\n",
2159 		    mp);
2160 	}
2161 	FREE_LOCK(ump);
2162 	return (error);
2163 }
2164 
2165 /*
2166  * Flush all vnodes and worklist items associated with a specified mount point.
2167  */
2168 int
2169 softdep_flushfiles(oldmnt, flags, td)
2170 	struct mount *oldmnt;
2171 	int flags;
2172 	struct thread *td;
2173 {
2174 #ifdef QUOTA
2175 	struct ufsmount *ump;
2176 	int i;
2177 #endif
2178 	int error, early, depcount, loopcnt, retry_flush_count, retry;
2179 	int morework;
2180 
2181 	KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0,
2182 	    ("softdep_flushfiles called on non-softdep filesystem"));
2183 	loopcnt = 10;
2184 	retry_flush_count = 3;
2185 retry_flush:
2186 	error = 0;
2187 
2188 	/*
2189 	 * Alternately flush the vnodes associated with the mount
2190 	 * point and process any dependencies that the flushing
2191 	 * creates. In theory, this loop can happen at most twice,
2192 	 * but we give it a few extra just to be sure.
2193 	 */
2194 	for (; loopcnt > 0; loopcnt--) {
2195 		/*
2196 		 * Do another flush in case any vnodes were brought in
2197 		 * as part of the cleanup operations.
2198 		 */
2199 		early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2200 		    MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2201 		if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2202 			break;
2203 		if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2204 		    depcount == 0)
2205 			break;
2206 	}
2207 	/*
2208 	 * If we are unmounting then it is an error to fail. If we
2209 	 * are simply trying to downgrade to read-only, then filesystem
2210 	 * activity can keep us busy forever, so we just fail with EBUSY.
2211 	 */
2212 	if (loopcnt == 0) {
2213 		if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2214 			panic("softdep_flushfiles: looping");
2215 		error = EBUSY;
2216 	}
2217 	if (!error)
2218 		error = softdep_waitidle(oldmnt, flags);
2219 	if (!error) {
2220 		if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2221 			retry = 0;
2222 			MNT_ILOCK(oldmnt);
2223 			morework = oldmnt->mnt_nvnodelistsize > 0;
2224 #ifdef QUOTA
2225 			ump = VFSTOUFS(oldmnt);
2226 			UFS_LOCK(ump);
2227 			for (i = 0; i < MAXQUOTAS; i++) {
2228 				if (ump->um_quotas[i] != NULLVP)
2229 					morework = 1;
2230 			}
2231 			UFS_UNLOCK(ump);
2232 #endif
2233 			if (morework) {
2234 				if (--retry_flush_count > 0) {
2235 					retry = 1;
2236 					loopcnt = 3;
2237 				} else
2238 					error = EBUSY;
2239 			}
2240 			MNT_IUNLOCK(oldmnt);
2241 			if (retry)
2242 				goto retry_flush;
2243 		}
2244 	}
2245 	return (error);
2246 }
2247 
2248 /*
2249  * Structure hashing.
2250  *
2251  * There are four types of structures that can be looked up:
2252  *	1) pagedep structures identified by mount point, inode number,
2253  *	   and logical block.
2254  *	2) inodedep structures identified by mount point and inode number.
2255  *	3) newblk structures identified by mount point and
2256  *	   physical block number.
2257  *	4) bmsafemap structures identified by mount point and
2258  *	   cylinder group number.
2259  *
2260  * The "pagedep" and "inodedep" dependency structures are hashed
2261  * separately from the file blocks and inodes to which they correspond.
2262  * This separation helps when the in-memory copy of an inode or
2263  * file block must be replaced. It also obviates the need to access
2264  * an inode or file page when simply updating (or de-allocating)
2265  * dependency structures. Lookup of newblk structures is needed to
2266  * find newly allocated blocks when trying to associate them with
2267  * their allocdirect or allocindir structure.
2268  *
2269  * The lookup routines optionally create and hash a new instance when
2270  * an existing entry is not found. The bmsafemap lookup routine always
2271  * allocates a new structure if an existing one is not found.
2272  */
2273 #define DEPALLOC	0x0001	/* allocate structure if lookup fails */
2274 
2275 /*
2276  * Structures and routines associated with pagedep caching.
2277  */
2278 #define	PAGEDEP_HASH(ump, inum, lbn) \
2279 	(&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2280 
2281 static int
2282 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2283 	struct pagedep_hashhead *pagedephd;
2284 	ino_t ino;
2285 	ufs_lbn_t lbn;
2286 	struct pagedep **pagedeppp;
2287 {
2288 	struct pagedep *pagedep;
2289 
2290 	LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2291 		if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2292 			*pagedeppp = pagedep;
2293 			return (1);
2294 		}
2295 	}
2296 	*pagedeppp = NULL;
2297 	return (0);
2298 }
2299 /*
2300  * Look up a pagedep. Return 1 if found, 0 otherwise.
2301  * If not found, allocate if DEPALLOC flag is passed.
2302  * Found or allocated entry is returned in pagedeppp.
2303  */
2304 static int
2305 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2306 	struct mount *mp;
2307 	struct buf *bp;
2308 	ino_t ino;
2309 	ufs_lbn_t lbn;
2310 	int flags;
2311 	struct pagedep **pagedeppp;
2312 {
2313 	struct pagedep *pagedep;
2314 	struct pagedep_hashhead *pagedephd;
2315 	struct worklist *wk;
2316 	struct ufsmount *ump;
2317 	int ret;
2318 	int i;
2319 
2320 	ump = VFSTOUFS(mp);
2321 	LOCK_OWNED(ump);
2322 	if (bp) {
2323 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2324 			if (wk->wk_type == D_PAGEDEP) {
2325 				*pagedeppp = WK_PAGEDEP(wk);
2326 				return (1);
2327 			}
2328 		}
2329 	}
2330 	pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2331 	ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2332 	if (ret) {
2333 		if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2334 			WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2335 		return (1);
2336 	}
2337 	if ((flags & DEPALLOC) == 0)
2338 		return (0);
2339 	FREE_LOCK(ump);
2340 	pagedep = malloc(sizeof(struct pagedep),
2341 	    M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2342 	workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2343 	ACQUIRE_LOCK(ump);
2344 	ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2345 	if (*pagedeppp) {
2346 		/*
2347 		 * This should never happen since we only create pagedeps
2348 		 * with the vnode lock held.  Could be an assert.
2349 		 */
2350 		WORKITEM_FREE(pagedep, D_PAGEDEP);
2351 		return (ret);
2352 	}
2353 	pagedep->pd_ino = ino;
2354 	pagedep->pd_lbn = lbn;
2355 	LIST_INIT(&pagedep->pd_dirremhd);
2356 	LIST_INIT(&pagedep->pd_pendinghd);
2357 	for (i = 0; i < DAHASHSZ; i++)
2358 		LIST_INIT(&pagedep->pd_diraddhd[i]);
2359 	LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2360 	WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2361 	*pagedeppp = pagedep;
2362 	return (0);
2363 }
2364 
2365 /*
2366  * Structures and routines associated with inodedep caching.
2367  */
2368 #define	INODEDEP_HASH(ump, inum) \
2369       (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2370 
2371 static int
2372 inodedep_find(inodedephd, inum, inodedeppp)
2373 	struct inodedep_hashhead *inodedephd;
2374 	ino_t inum;
2375 	struct inodedep **inodedeppp;
2376 {
2377 	struct inodedep *inodedep;
2378 
2379 	LIST_FOREACH(inodedep, inodedephd, id_hash)
2380 		if (inum == inodedep->id_ino)
2381 			break;
2382 	if (inodedep) {
2383 		*inodedeppp = inodedep;
2384 		return (1);
2385 	}
2386 	*inodedeppp = NULL;
2387 
2388 	return (0);
2389 }
2390 /*
2391  * Look up an inodedep. Return 1 if found, 0 if not found.
2392  * If not found, allocate if DEPALLOC flag is passed.
2393  * Found or allocated entry is returned in inodedeppp.
2394  */
2395 static int
2396 inodedep_lookup(mp, inum, flags, inodedeppp)
2397 	struct mount *mp;
2398 	ino_t inum;
2399 	int flags;
2400 	struct inodedep **inodedeppp;
2401 {
2402 	struct inodedep *inodedep;
2403 	struct inodedep_hashhead *inodedephd;
2404 	struct ufsmount *ump;
2405 	struct fs *fs;
2406 
2407 	ump = VFSTOUFS(mp);
2408 	LOCK_OWNED(ump);
2409 	fs = ump->um_fs;
2410 	inodedephd = INODEDEP_HASH(ump, inum);
2411 
2412 	if (inodedep_find(inodedephd, inum, inodedeppp))
2413 		return (1);
2414 	if ((flags & DEPALLOC) == 0)
2415 		return (0);
2416 	/*
2417 	 * If the system is over its limit and our filesystem is
2418 	 * responsible for more than our share of that usage and
2419 	 * we are not in a rush, request some inodedep cleanup.
2420 	 */
2421 	if (softdep_excess_items(ump, D_INODEDEP))
2422 		schedule_cleanup(mp);
2423 	else
2424 		FREE_LOCK(ump);
2425 	inodedep = malloc(sizeof(struct inodedep),
2426 		M_INODEDEP, M_SOFTDEP_FLAGS);
2427 	workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2428 	ACQUIRE_LOCK(ump);
2429 	if (inodedep_find(inodedephd, inum, inodedeppp)) {
2430 		WORKITEM_FREE(inodedep, D_INODEDEP);
2431 		return (1);
2432 	}
2433 	inodedep->id_fs = fs;
2434 	inodedep->id_ino = inum;
2435 	inodedep->id_state = ALLCOMPLETE;
2436 	inodedep->id_nlinkdelta = 0;
2437 	inodedep->id_nlinkwrote = -1;
2438 	inodedep->id_savedino1 = NULL;
2439 	inodedep->id_savedsize = -1;
2440 	inodedep->id_savedextsize = -1;
2441 	inodedep->id_savednlink = -1;
2442 	inodedep->id_bmsafemap = NULL;
2443 	inodedep->id_mkdiradd = NULL;
2444 	LIST_INIT(&inodedep->id_dirremhd);
2445 	LIST_INIT(&inodedep->id_pendinghd);
2446 	LIST_INIT(&inodedep->id_inowait);
2447 	LIST_INIT(&inodedep->id_bufwait);
2448 	TAILQ_INIT(&inodedep->id_inoreflst);
2449 	TAILQ_INIT(&inodedep->id_inoupdt);
2450 	TAILQ_INIT(&inodedep->id_newinoupdt);
2451 	TAILQ_INIT(&inodedep->id_extupdt);
2452 	TAILQ_INIT(&inodedep->id_newextupdt);
2453 	TAILQ_INIT(&inodedep->id_freeblklst);
2454 	LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2455 	*inodedeppp = inodedep;
2456 	return (0);
2457 }
2458 
2459 /*
2460  * Structures and routines associated with newblk caching.
2461  */
2462 #define	NEWBLK_HASH(ump, inum) \
2463 	(&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2464 
2465 static int
2466 newblk_find(newblkhd, newblkno, flags, newblkpp)
2467 	struct newblk_hashhead *newblkhd;
2468 	ufs2_daddr_t newblkno;
2469 	int flags;
2470 	struct newblk **newblkpp;
2471 {
2472 	struct newblk *newblk;
2473 
2474 	LIST_FOREACH(newblk, newblkhd, nb_hash) {
2475 		if (newblkno != newblk->nb_newblkno)
2476 			continue;
2477 		/*
2478 		 * If we're creating a new dependency don't match those that
2479 		 * have already been converted to allocdirects.  This is for
2480 		 * a frag extend.
2481 		 */
2482 		if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2483 			continue;
2484 		break;
2485 	}
2486 	if (newblk) {
2487 		*newblkpp = newblk;
2488 		return (1);
2489 	}
2490 	*newblkpp = NULL;
2491 	return (0);
2492 }
2493 
2494 /*
2495  * Look up a newblk. Return 1 if found, 0 if not found.
2496  * If not found, allocate if DEPALLOC flag is passed.
2497  * Found or allocated entry is returned in newblkpp.
2498  */
2499 static int
2500 newblk_lookup(mp, newblkno, flags, newblkpp)
2501 	struct mount *mp;
2502 	ufs2_daddr_t newblkno;
2503 	int flags;
2504 	struct newblk **newblkpp;
2505 {
2506 	struct newblk *newblk;
2507 	struct newblk_hashhead *newblkhd;
2508 	struct ufsmount *ump;
2509 
2510 	ump = VFSTOUFS(mp);
2511 	LOCK_OWNED(ump);
2512 	newblkhd = NEWBLK_HASH(ump, newblkno);
2513 	if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2514 		return (1);
2515 	if ((flags & DEPALLOC) == 0)
2516 		return (0);
2517 	if (softdep_excess_items(ump, D_NEWBLK) ||
2518 	    softdep_excess_items(ump, D_ALLOCDIRECT) ||
2519 	    softdep_excess_items(ump, D_ALLOCINDIR))
2520 		schedule_cleanup(mp);
2521 	else
2522 		FREE_LOCK(ump);
2523 	newblk = malloc(sizeof(union allblk), M_NEWBLK,
2524 	    M_SOFTDEP_FLAGS | M_ZERO);
2525 	workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2526 	ACQUIRE_LOCK(ump);
2527 	if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2528 		WORKITEM_FREE(newblk, D_NEWBLK);
2529 		return (1);
2530 	}
2531 	newblk->nb_freefrag = NULL;
2532 	LIST_INIT(&newblk->nb_indirdeps);
2533 	LIST_INIT(&newblk->nb_newdirblk);
2534 	LIST_INIT(&newblk->nb_jwork);
2535 	newblk->nb_state = ATTACHED;
2536 	newblk->nb_newblkno = newblkno;
2537 	LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2538 	*newblkpp = newblk;
2539 	return (0);
2540 }
2541 
2542 /*
2543  * Structures and routines associated with freed indirect block caching.
2544  */
2545 #define	INDIR_HASH(ump, blkno) \
2546 	(&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2547 
2548 /*
2549  * Lookup an indirect block in the indir hash table.  The freework is
2550  * removed and potentially freed.  The caller must do a blocking journal
2551  * write before writing to the blkno.
2552  */
2553 static int
2554 indirblk_lookup(mp, blkno)
2555 	struct mount *mp;
2556 	ufs2_daddr_t blkno;
2557 {
2558 	struct freework *freework;
2559 	struct indir_hashhead *wkhd;
2560 	struct ufsmount *ump;
2561 
2562 	ump = VFSTOUFS(mp);
2563 	wkhd = INDIR_HASH(ump, blkno);
2564 	TAILQ_FOREACH(freework, wkhd, fw_next) {
2565 		if (freework->fw_blkno != blkno)
2566 			continue;
2567 		indirblk_remove(freework);
2568 		return (1);
2569 	}
2570 	return (0);
2571 }
2572 
2573 /*
2574  * Insert an indirect block represented by freework into the indirblk
2575  * hash table so that it may prevent the block from being re-used prior
2576  * to the journal being written.
2577  */
2578 static void
2579 indirblk_insert(freework)
2580 	struct freework *freework;
2581 {
2582 	struct jblocks *jblocks;
2583 	struct jseg *jseg;
2584 	struct ufsmount *ump;
2585 
2586 	ump = VFSTOUFS(freework->fw_list.wk_mp);
2587 	jblocks = ump->softdep_jblocks;
2588 	jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2589 	if (jseg == NULL)
2590 		return;
2591 
2592 	LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2593 	TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2594 	    fw_next);
2595 	freework->fw_state &= ~DEPCOMPLETE;
2596 }
2597 
2598 static void
2599 indirblk_remove(freework)
2600 	struct freework *freework;
2601 {
2602 	struct ufsmount *ump;
2603 
2604 	ump = VFSTOUFS(freework->fw_list.wk_mp);
2605 	LIST_REMOVE(freework, fw_segs);
2606 	TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2607 	freework->fw_state |= DEPCOMPLETE;
2608 	if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2609 		WORKITEM_FREE(freework, D_FREEWORK);
2610 }
2611 
2612 /*
2613  * Executed during filesystem system initialization before
2614  * mounting any filesystems.
2615  */
2616 void
2617 softdep_initialize()
2618 {
2619 
2620 	TAILQ_INIT(&softdepmounts);
2621 #ifdef __LP64__
2622 	max_softdeps = desiredvnodes * 4;
2623 #else
2624 	max_softdeps = desiredvnodes * 2;
2625 #endif
2626 
2627 	/* initialise bioops hack */
2628 	bioops.io_start = softdep_disk_io_initiation;
2629 	bioops.io_complete = softdep_disk_write_complete;
2630 	bioops.io_deallocate = softdep_deallocate_dependencies;
2631 	bioops.io_countdeps = softdep_count_dependencies;
2632 	softdep_ast_cleanup = softdep_ast_cleanup_proc;
2633 
2634 	/* Initialize the callout with an mtx. */
2635 	callout_init_mtx(&softdep_callout, &lk, 0);
2636 }
2637 
2638 /*
2639  * Executed after all filesystems have been unmounted during
2640  * filesystem module unload.
2641  */
2642 void
2643 softdep_uninitialize()
2644 {
2645 
2646 	/* clear bioops hack */
2647 	bioops.io_start = NULL;
2648 	bioops.io_complete = NULL;
2649 	bioops.io_deallocate = NULL;
2650 	bioops.io_countdeps = NULL;
2651 	softdep_ast_cleanup = NULL;
2652 
2653 	callout_drain(&softdep_callout);
2654 }
2655 
2656 /*
2657  * Called at mount time to notify the dependency code that a
2658  * filesystem wishes to use it.
2659  */
2660 int
2661 softdep_mount(devvp, mp, fs, cred)
2662 	struct vnode *devvp;
2663 	struct mount *mp;
2664 	struct fs *fs;
2665 	struct ucred *cred;
2666 {
2667 	struct csum_total cstotal;
2668 	struct mount_softdeps *sdp;
2669 	struct ufsmount *ump;
2670 	struct cg *cgp;
2671 	struct buf *bp;
2672 	u_int cyl, i;
2673 	int error;
2674 
2675 	sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2676 	    M_WAITOK | M_ZERO);
2677 	MNT_ILOCK(mp);
2678 	mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2679 	if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2680 		mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2681 			MNTK_SOFTDEP | MNTK_NOASYNC;
2682 	}
2683 	ump = VFSTOUFS(mp);
2684 	ump->um_softdep = sdp;
2685 	MNT_IUNLOCK(mp);
2686 	rw_init(LOCK_PTR(ump), "per-fs softdep");
2687 	sdp->sd_ump = ump;
2688 	LIST_INIT(&ump->softdep_workitem_pending);
2689 	LIST_INIT(&ump->softdep_journal_pending);
2690 	TAILQ_INIT(&ump->softdep_unlinked);
2691 	LIST_INIT(&ump->softdep_dirtycg);
2692 	ump->softdep_worklist_tail = NULL;
2693 	ump->softdep_on_worklist = 0;
2694 	ump->softdep_deps = 0;
2695 	LIST_INIT(&ump->softdep_mkdirlisthd);
2696 	ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
2697 	    &ump->pagedep_hash_size);
2698 	ump->pagedep_nextclean = 0;
2699 	ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP,
2700 	    &ump->inodedep_hash_size);
2701 	ump->inodedep_nextclean = 0;
2702 	ump->newblk_hashtbl = hashinit(max_softdeps / 2,  M_NEWBLK,
2703 	    &ump->newblk_hash_size);
2704 	ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP,
2705 	    &ump->bmsafemap_hash_size);
2706 	i = 1 << (ffs(desiredvnodes / 10) - 1);
2707 	ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead),
2708 	    M_FREEWORK, M_WAITOK);
2709 	ump->indir_hash_size = i - 1;
2710 	for (i = 0; i <= ump->indir_hash_size; i++)
2711 		TAILQ_INIT(&ump->indir_hashtbl[i]);
2712 #ifdef INVARIANTS
2713 	for (i = 0; i <= D_LAST; i++)
2714 		LIST_INIT(&ump->softdep_alldeps[i]);
2715 #endif
2716 	ACQUIRE_GBLLOCK(&lk);
2717 	TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2718 	FREE_GBLLOCK(&lk);
2719 	if ((fs->fs_flags & FS_SUJ) &&
2720 	    (error = journal_mount(mp, fs, cred)) != 0) {
2721 		printf("Failed to start journal: %d\n", error);
2722 		softdep_unmount(mp);
2723 		return (error);
2724 	}
2725 	/*
2726 	 * Start our flushing thread in the bufdaemon process.
2727 	 */
2728 	ACQUIRE_LOCK(ump);
2729 	ump->softdep_flags |= FLUSH_STARTING;
2730 	FREE_LOCK(ump);
2731 	kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2732 	    &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2733 	    mp->mnt_stat.f_mntonname);
2734 	ACQUIRE_LOCK(ump);
2735 	while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2736 		msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2737 		    hz / 2);
2738 	}
2739 	FREE_LOCK(ump);
2740 	/*
2741 	 * When doing soft updates, the counters in the
2742 	 * superblock may have gotten out of sync. Recomputation
2743 	 * can take a long time and can be deferred for background
2744 	 * fsck.  However, the old behavior of scanning the cylinder
2745 	 * groups and recalculating them at mount time is available
2746 	 * by setting vfs.ffs.compute_summary_at_mount to one.
2747 	 */
2748 	if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2749 		return (0);
2750 	bzero(&cstotal, sizeof cstotal);
2751 	for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2752 		if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2753 		    fs->fs_cgsize, cred, &bp)) != 0) {
2754 			brelse(bp);
2755 			softdep_unmount(mp);
2756 			return (error);
2757 		}
2758 		cgp = (struct cg *)bp->b_data;
2759 		cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2760 		cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2761 		cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2762 		cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2763 		fs->fs_cs(fs, cyl) = cgp->cg_cs;
2764 		brelse(bp);
2765 	}
2766 #ifdef INVARIANTS
2767 	if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2768 		printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2769 #endif
2770 	bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2771 	return (0);
2772 }
2773 
2774 void
2775 softdep_unmount(mp)
2776 	struct mount *mp;
2777 {
2778 	struct ufsmount *ump;
2779 #ifdef INVARIANTS
2780 	int i;
2781 #endif
2782 
2783 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
2784 	    ("softdep_unmount called on non-softdep filesystem"));
2785 	ump = VFSTOUFS(mp);
2786 	MNT_ILOCK(mp);
2787 	mp->mnt_flag &= ~MNT_SOFTDEP;
2788 	if (MOUNTEDSUJ(mp) == 0) {
2789 		MNT_IUNLOCK(mp);
2790 	} else {
2791 		mp->mnt_flag &= ~MNT_SUJ;
2792 		MNT_IUNLOCK(mp);
2793 		journal_unmount(ump);
2794 	}
2795 	/*
2796 	 * Shut down our flushing thread. Check for NULL is if
2797 	 * softdep_mount errors out before the thread has been created.
2798 	 */
2799 	if (ump->softdep_flushtd != NULL) {
2800 		ACQUIRE_LOCK(ump);
2801 		ump->softdep_flags |= FLUSH_EXIT;
2802 		wakeup(&ump->softdep_flushtd);
2803 		msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP,
2804 		    "sdwait", 0);
2805 		KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2806 		    ("Thread shutdown failed"));
2807 	}
2808 	/*
2809 	 * Free up our resources.
2810 	 */
2811 	ACQUIRE_GBLLOCK(&lk);
2812 	TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next);
2813 	FREE_GBLLOCK(&lk);
2814 	rw_destroy(LOCK_PTR(ump));
2815 	hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size);
2816 	hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size);
2817 	hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size);
2818 	hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP,
2819 	    ump->bmsafemap_hash_size);
2820 	free(ump->indir_hashtbl, M_FREEWORK);
2821 #ifdef INVARIANTS
2822 	for (i = 0; i <= D_LAST; i++) {
2823 		KASSERT(ump->softdep_curdeps[i] == 0,
2824 		    ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2825 		    TYPENAME(i), ump->softdep_curdeps[i]));
2826 		KASSERT(LIST_EMPTY(&ump->softdep_alldeps[i]),
2827 		    ("Unmount %s: Dep type %s not empty (%p)", ump->um_fs->fs_fsmnt,
2828 		    TYPENAME(i), LIST_FIRST(&ump->softdep_alldeps[i])));
2829 	}
2830 #endif
2831 	free(ump->um_softdep, M_MOUNTDATA);
2832 }
2833 
2834 static struct jblocks *
2835 jblocks_create(void)
2836 {
2837 	struct jblocks *jblocks;
2838 
2839 	jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2840 	TAILQ_INIT(&jblocks->jb_segs);
2841 	jblocks->jb_avail = 10;
2842 	jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2843 	    M_JBLOCKS, M_WAITOK | M_ZERO);
2844 
2845 	return (jblocks);
2846 }
2847 
2848 static ufs2_daddr_t
2849 jblocks_alloc(jblocks, bytes, actual)
2850 	struct jblocks *jblocks;
2851 	int bytes;
2852 	int *actual;
2853 {
2854 	ufs2_daddr_t daddr;
2855 	struct jextent *jext;
2856 	int freecnt;
2857 	int blocks;
2858 
2859 	blocks = bytes / DEV_BSIZE;
2860 	jext = &jblocks->jb_extent[jblocks->jb_head];
2861 	freecnt = jext->je_blocks - jblocks->jb_off;
2862 	if (freecnt == 0) {
2863 		jblocks->jb_off = 0;
2864 		if (++jblocks->jb_head > jblocks->jb_used)
2865 			jblocks->jb_head = 0;
2866 		jext = &jblocks->jb_extent[jblocks->jb_head];
2867 		freecnt = jext->je_blocks;
2868 	}
2869 	if (freecnt > blocks)
2870 		freecnt = blocks;
2871 	*actual = freecnt * DEV_BSIZE;
2872 	daddr = jext->je_daddr + jblocks->jb_off;
2873 	jblocks->jb_off += freecnt;
2874 	jblocks->jb_free -= freecnt;
2875 
2876 	return (daddr);
2877 }
2878 
2879 static void
2880 jblocks_free(jblocks, mp, bytes)
2881 	struct jblocks *jblocks;
2882 	struct mount *mp;
2883 	int bytes;
2884 {
2885 
2886 	LOCK_OWNED(VFSTOUFS(mp));
2887 	jblocks->jb_free += bytes / DEV_BSIZE;
2888 	if (jblocks->jb_suspended)
2889 		worklist_speedup(mp);
2890 	wakeup(jblocks);
2891 }
2892 
2893 static void
2894 jblocks_destroy(jblocks)
2895 	struct jblocks *jblocks;
2896 {
2897 
2898 	if (jblocks->jb_extent)
2899 		free(jblocks->jb_extent, M_JBLOCKS);
2900 	free(jblocks, M_JBLOCKS);
2901 }
2902 
2903 static void
2904 jblocks_add(jblocks, daddr, blocks)
2905 	struct jblocks *jblocks;
2906 	ufs2_daddr_t daddr;
2907 	int blocks;
2908 {
2909 	struct jextent *jext;
2910 
2911 	jblocks->jb_blocks += blocks;
2912 	jblocks->jb_free += blocks;
2913 	jext = &jblocks->jb_extent[jblocks->jb_used];
2914 	/* Adding the first block. */
2915 	if (jext->je_daddr == 0) {
2916 		jext->je_daddr = daddr;
2917 		jext->je_blocks = blocks;
2918 		return;
2919 	}
2920 	/* Extending the last extent. */
2921 	if (jext->je_daddr + jext->je_blocks == daddr) {
2922 		jext->je_blocks += blocks;
2923 		return;
2924 	}
2925 	/* Adding a new extent. */
2926 	if (++jblocks->jb_used == jblocks->jb_avail) {
2927 		jblocks->jb_avail *= 2;
2928 		jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2929 		    M_JBLOCKS, M_WAITOK | M_ZERO);
2930 		memcpy(jext, jblocks->jb_extent,
2931 		    sizeof(struct jextent) * jblocks->jb_used);
2932 		free(jblocks->jb_extent, M_JBLOCKS);
2933 		jblocks->jb_extent = jext;
2934 	}
2935 	jext = &jblocks->jb_extent[jblocks->jb_used];
2936 	jext->je_daddr = daddr;
2937 	jext->je_blocks = blocks;
2938 	return;
2939 }
2940 
2941 int
2942 softdep_journal_lookup(mp, vpp)
2943 	struct mount *mp;
2944 	struct vnode **vpp;
2945 {
2946 	struct componentname cnp;
2947 	struct vnode *dvp;
2948 	ino_t sujournal;
2949 	int error;
2950 
2951 	error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2952 	if (error)
2953 		return (error);
2954 	bzero(&cnp, sizeof(cnp));
2955 	cnp.cn_nameiop = LOOKUP;
2956 	cnp.cn_flags = ISLASTCN;
2957 	cnp.cn_thread = curthread;
2958 	cnp.cn_cred = curthread->td_ucred;
2959 	cnp.cn_pnbuf = SUJ_FILE;
2960 	cnp.cn_nameptr = SUJ_FILE;
2961 	cnp.cn_namelen = strlen(SUJ_FILE);
2962 	error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2963 	vput(dvp);
2964 	if (error != 0)
2965 		return (error);
2966 	error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2967 	return (error);
2968 }
2969 
2970 /*
2971  * Open and verify the journal file.
2972  */
2973 static int
2974 journal_mount(mp, fs, cred)
2975 	struct mount *mp;
2976 	struct fs *fs;
2977 	struct ucred *cred;
2978 {
2979 	struct jblocks *jblocks;
2980 	struct ufsmount *ump;
2981 	struct vnode *vp;
2982 	struct inode *ip;
2983 	ufs2_daddr_t blkno;
2984 	int bcount;
2985 	int error;
2986 	int i;
2987 
2988 	ump = VFSTOUFS(mp);
2989 	ump->softdep_journal_tail = NULL;
2990 	ump->softdep_on_journal = 0;
2991 	ump->softdep_accdeps = 0;
2992 	ump->softdep_req = 0;
2993 	ump->softdep_jblocks = NULL;
2994 	error = softdep_journal_lookup(mp, &vp);
2995 	if (error != 0) {
2996 		printf("Failed to find journal.  Use tunefs to create one\n");
2997 		return (error);
2998 	}
2999 	ip = VTOI(vp);
3000 	if (ip->i_size < SUJ_MIN) {
3001 		error = ENOSPC;
3002 		goto out;
3003 	}
3004 	bcount = lblkno(fs, ip->i_size);	/* Only use whole blocks. */
3005 	jblocks = jblocks_create();
3006 	for (i = 0; i < bcount; i++) {
3007 		error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
3008 		if (error)
3009 			break;
3010 		jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
3011 	}
3012 	if (error) {
3013 		jblocks_destroy(jblocks);
3014 		goto out;
3015 	}
3016 	jblocks->jb_low = jblocks->jb_free / 3;	/* Reserve 33%. */
3017 	jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
3018 	ump->softdep_jblocks = jblocks;
3019 out:
3020 	if (error == 0) {
3021 		MNT_ILOCK(mp);
3022 		mp->mnt_flag |= MNT_SUJ;
3023 		mp->mnt_flag &= ~MNT_SOFTDEP;
3024 		MNT_IUNLOCK(mp);
3025 		/*
3026 		 * Only validate the journal contents if the
3027 		 * filesystem is clean, otherwise we write the logs
3028 		 * but they'll never be used.  If the filesystem was
3029 		 * still dirty when we mounted it the journal is
3030 		 * invalid and a new journal can only be valid if it
3031 		 * starts from a clean mount.
3032 		 */
3033 		if (fs->fs_clean) {
3034 			DIP_SET(ip, i_modrev, fs->fs_mtime);
3035 			ip->i_flags |= IN_MODIFIED;
3036 			ffs_update(vp, 1);
3037 		}
3038 	}
3039 	vput(vp);
3040 	return (error);
3041 }
3042 
3043 static void
3044 journal_unmount(ump)
3045 	struct ufsmount *ump;
3046 {
3047 
3048 	if (ump->softdep_jblocks)
3049 		jblocks_destroy(ump->softdep_jblocks);
3050 	ump->softdep_jblocks = NULL;
3051 }
3052 
3053 /*
3054  * Called when a journal record is ready to be written.  Space is allocated
3055  * and the journal entry is created when the journal is flushed to stable
3056  * store.
3057  */
3058 static void
3059 add_to_journal(wk)
3060 	struct worklist *wk;
3061 {
3062 	struct ufsmount *ump;
3063 
3064 	ump = VFSTOUFS(wk->wk_mp);
3065 	LOCK_OWNED(ump);
3066 	if (wk->wk_state & ONWORKLIST)
3067 		panic("add_to_journal: %s(0x%X) already on list",
3068 		    TYPENAME(wk->wk_type), wk->wk_state);
3069 	wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
3070 	if (LIST_EMPTY(&ump->softdep_journal_pending)) {
3071 		ump->softdep_jblocks->jb_age = ticks;
3072 		LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
3073 	} else
3074 		LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
3075 	ump->softdep_journal_tail = wk;
3076 	ump->softdep_on_journal += 1;
3077 }
3078 
3079 /*
3080  * Remove an arbitrary item for the journal worklist maintain the tail
3081  * pointer.  This happens when a new operation obviates the need to
3082  * journal an old operation.
3083  */
3084 static void
3085 remove_from_journal(wk)
3086 	struct worklist *wk;
3087 {
3088 	struct ufsmount *ump;
3089 
3090 	ump = VFSTOUFS(wk->wk_mp);
3091 	LOCK_OWNED(ump);
3092 #ifdef INVARIANTS
3093 	{
3094 		struct worklist *wkn;
3095 
3096 		LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
3097 			if (wkn == wk)
3098 				break;
3099 		if (wkn == NULL)
3100 			panic("remove_from_journal: %p is not in journal", wk);
3101 	}
3102 #endif
3103 	/*
3104 	 * We emulate a TAILQ to save space in most structures which do not
3105 	 * require TAILQ semantics.  Here we must update the tail position
3106 	 * when removing the tail which is not the final entry. This works
3107 	 * only if the worklist linkage are at the beginning of the structure.
3108 	 */
3109 	if (ump->softdep_journal_tail == wk)
3110 		ump->softdep_journal_tail =
3111 		    (struct worklist *)wk->wk_list.le_prev;
3112 	WORKLIST_REMOVE(wk);
3113 	ump->softdep_on_journal -= 1;
3114 }
3115 
3116 /*
3117  * Check for journal space as well as dependency limits so the prelink
3118  * code can throttle both journaled and non-journaled filesystems.
3119  * Threshold is 0 for low and 1 for min.
3120  */
3121 static int
3122 journal_space(ump, thresh)
3123 	struct ufsmount *ump;
3124 	int thresh;
3125 {
3126 	struct jblocks *jblocks;
3127 	int limit, avail;
3128 
3129 	jblocks = ump->softdep_jblocks;
3130 	if (jblocks == NULL)
3131 		return (1);
3132 	/*
3133 	 * We use a tighter restriction here to prevent request_cleanup()
3134 	 * running in threads from running into locks we currently hold.
3135 	 * We have to be over the limit and our filesystem has to be
3136 	 * responsible for more than our share of that usage.
3137 	 */
3138 	limit = (max_softdeps / 10) * 9;
3139 	if (dep_current[D_INODEDEP] > limit &&
3140 	    ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
3141 		return (0);
3142 	if (thresh)
3143 		thresh = jblocks->jb_min;
3144 	else
3145 		thresh = jblocks->jb_low;
3146 	avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
3147 	avail = jblocks->jb_free - avail;
3148 
3149 	return (avail > thresh);
3150 }
3151 
3152 static void
3153 journal_suspend(ump)
3154 	struct ufsmount *ump;
3155 {
3156 	struct jblocks *jblocks;
3157 	struct mount *mp;
3158 	bool set;
3159 
3160 	mp = UFSTOVFS(ump);
3161 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0)
3162 		return;
3163 
3164 	jblocks = ump->softdep_jblocks;
3165 	vfs_op_enter(mp);
3166 	set = false;
3167 	MNT_ILOCK(mp);
3168 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
3169 		stat_journal_min++;
3170 		mp->mnt_kern_flag |= MNTK_SUSPEND;
3171 		mp->mnt_susp_owner = ump->softdep_flushtd;
3172 		set = true;
3173 	}
3174 	jblocks->jb_suspended = 1;
3175 	MNT_IUNLOCK(mp);
3176 	if (!set)
3177 		vfs_op_exit(mp);
3178 }
3179 
3180 static int
3181 journal_unsuspend(struct ufsmount *ump)
3182 {
3183 	struct jblocks *jblocks;
3184 	struct mount *mp;
3185 
3186 	mp = UFSTOVFS(ump);
3187 	jblocks = ump->softdep_jblocks;
3188 
3189 	if (jblocks != NULL && jblocks->jb_suspended &&
3190 	    journal_space(ump, jblocks->jb_min)) {
3191 		jblocks->jb_suspended = 0;
3192 		FREE_LOCK(ump);
3193 		mp->mnt_susp_owner = curthread;
3194 		vfs_write_resume(mp, 0);
3195 		ACQUIRE_LOCK(ump);
3196 		return (1);
3197 	}
3198 	return (0);
3199 }
3200 
3201 /*
3202  * Called before any allocation function to be certain that there is
3203  * sufficient space in the journal prior to creating any new records.
3204  * Since in the case of block allocation we may have multiple locked
3205  * buffers at the time of the actual allocation we can not block
3206  * when the journal records are created.  Doing so would create a deadlock
3207  * if any of these buffers needed to be flushed to reclaim space.  Instead
3208  * we require a sufficiently large amount of available space such that
3209  * each thread in the system could have passed this allocation check and
3210  * still have sufficient free space.  With 20% of a minimum journal size
3211  * of 1MB we have 6553 records available.
3212  */
3213 int
3214 softdep_prealloc(vp, waitok)
3215 	struct vnode *vp;
3216 	int waitok;
3217 {
3218 	struct ufsmount *ump;
3219 
3220 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3221 	    ("softdep_prealloc called on non-softdep filesystem"));
3222 	/*
3223 	 * Nothing to do if we are not running journaled soft updates.
3224 	 * If we currently hold the snapshot lock, we must avoid
3225 	 * handling other resources that could cause deadlock.  Do not
3226 	 * touch quotas vnode since it is typically recursed with
3227 	 * other vnode locks held.
3228 	 */
3229 	if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3230 	    (vp->v_vflag & VV_SYSTEM) != 0)
3231 		return (0);
3232 	ump = VFSTOUFS(vp->v_mount);
3233 	ACQUIRE_LOCK(ump);
3234 	if (journal_space(ump, 0)) {
3235 		FREE_LOCK(ump);
3236 		return (0);
3237 	}
3238 	stat_journal_low++;
3239 	FREE_LOCK(ump);
3240 	if (waitok == MNT_NOWAIT)
3241 		return (ENOSPC);
3242 	/*
3243 	 * Attempt to sync this vnode once to flush any journal
3244 	 * work attached to it.
3245 	 */
3246 	if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3247 		ffs_syncvnode(vp, waitok, 0);
3248 	ACQUIRE_LOCK(ump);
3249 	process_removes(vp);
3250 	process_truncates(vp);
3251 	if (journal_space(ump, 0) == 0) {
3252 		softdep_speedup(ump);
3253 		if (journal_space(ump, 1) == 0)
3254 			journal_suspend(ump);
3255 	}
3256 	FREE_LOCK(ump);
3257 
3258 	return (0);
3259 }
3260 
3261 /*
3262  * Try hard to sync all data and metadata for the vnode, and workitems
3263  * flushing which might conflict with the vnode lock.  This is a
3264  * helper for softdep_prerename().
3265  */
3266 static int
3267 softdep_prerename_vnode(ump, vp)
3268 	struct ufsmount *ump;
3269 	struct vnode *vp;
3270 {
3271 	int error;
3272 
3273 	ASSERT_VOP_ELOCKED(vp, "prehandle");
3274 	if (vp->v_data == NULL)
3275 		return (0);
3276 	error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3277 	if (error != 0)
3278 		return (error);
3279 	ACQUIRE_LOCK(ump);
3280 	process_removes(vp);
3281 	process_truncates(vp);
3282 	FREE_LOCK(ump);
3283 	return (0);
3284 }
3285 
3286 /*
3287  * Must be called from VOP_RENAME() after all vnodes are locked.
3288  * Ensures that there is enough journal space for rename.  It is
3289  * sufficiently different from softdep_prelink() by having to handle
3290  * four vnodes.
3291  */
3292 int
3293 softdep_prerename(fdvp, fvp, tdvp, tvp)
3294 	struct vnode *fdvp;
3295 	struct vnode *fvp;
3296 	struct vnode *tdvp;
3297 	struct vnode *tvp;
3298 {
3299 	struct ufsmount *ump;
3300 	int error;
3301 
3302 	ump = VFSTOUFS(fdvp->v_mount);
3303 
3304 	if (journal_space(ump, 0))
3305 		return (0);
3306 
3307 	VOP_UNLOCK(tdvp);
3308 	VOP_UNLOCK(fvp);
3309 	if (tvp != NULL && tvp != tdvp)
3310 		VOP_UNLOCK(tvp);
3311 
3312 	error = softdep_prerename_vnode(ump, fdvp);
3313 	VOP_UNLOCK(fdvp);
3314 	if (error != 0)
3315 		return (error);
3316 
3317 	VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY);
3318 	error = softdep_prerename_vnode(ump, fvp);
3319 	VOP_UNLOCK(fvp);
3320 	if (error != 0)
3321 		return (error);
3322 
3323 	if (tdvp != fdvp) {
3324 		VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY);
3325 		error = softdep_prerename_vnode(ump, tdvp);
3326 		VOP_UNLOCK(tdvp);
3327 		if (error != 0)
3328 			return (error);
3329 	}
3330 
3331 	if (tvp != fvp && tvp != NULL) {
3332 		VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY);
3333 		error = softdep_prerename_vnode(ump, tvp);
3334 		VOP_UNLOCK(tvp);
3335 		if (error != 0)
3336 			return (error);
3337 	}
3338 
3339 	ACQUIRE_LOCK(ump);
3340 	softdep_speedup(ump);
3341 	process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3342 	if (journal_space(ump, 0) == 0) {
3343 		softdep_speedup(ump);
3344 		if (journal_space(ump, 1) == 0)
3345 			journal_suspend(ump);
3346 	}
3347 	FREE_LOCK(ump);
3348 	return (ERELOOKUP);
3349 }
3350 
3351 /*
3352  * Before adjusting a link count on a vnode verify that we have sufficient
3353  * journal space.  If not, process operations that depend on the currently
3354  * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3355  * and softdep flush threads can not acquire these locks to reclaim space.
3356  *
3357  * Returns 0 if all owned locks are still valid and were not dropped
3358  * in the process, in other case it returns either an error from sync,
3359  * or ERELOOKUP if any of the locks were re-acquired.  In the later
3360  * case, the state of the vnodes cannot be relied upon and our VFS
3361  * syscall must be restarted at top level from the lookup.
3362  */
3363 int
3364 softdep_prelink(dvp, vp)
3365 	struct vnode *dvp;
3366 	struct vnode *vp;
3367 {
3368 	struct ufsmount *ump;
3369 
3370 	ASSERT_VOP_ELOCKED(dvp, "prelink dvp");
3371 	if (vp != NULL)
3372 		ASSERT_VOP_ELOCKED(vp, "prelink vp");
3373 	ump = VFSTOUFS(dvp->v_mount);
3374 
3375 	/*
3376 	 * Nothing to do if we have sufficient journal space.  We skip
3377 	 * flushing when vp is a snapshot to avoid deadlock where
3378 	 * another thread is trying to update the inodeblock for dvp
3379 	 * and is waiting on snaplk that vp holds.
3380 	 */
3381 	if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp))))
3382 		return (0);
3383 
3384 	stat_journal_low++;
3385 	if (vp != NULL) {
3386 		VOP_UNLOCK(dvp);
3387 		ffs_syncvnode(vp, MNT_NOWAIT, 0);
3388 		vn_lock_pair(dvp, false, vp, true);
3389 		if (dvp->v_data == NULL)
3390 			return (ERELOOKUP);
3391 	}
3392 	if (vp != NULL)
3393 		VOP_UNLOCK(vp);
3394 	ffs_syncvnode(dvp, MNT_WAIT, 0);
3395 	VOP_UNLOCK(dvp);
3396 
3397 	/* Process vp before dvp as it may create .. removes. */
3398 	if (vp != NULL) {
3399 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3400 		if (vp->v_data == NULL) {
3401 			vn_lock_pair(dvp, false, vp, true);
3402 			return (ERELOOKUP);
3403 		}
3404 		ACQUIRE_LOCK(ump);
3405 		process_removes(vp);
3406 		process_truncates(vp);
3407 		FREE_LOCK(ump);
3408 		VOP_UNLOCK(vp);
3409 	}
3410 
3411 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
3412 	if (dvp->v_data == NULL) {
3413 		vn_lock_pair(dvp, true, vp, false);
3414 		return (ERELOOKUP);
3415 	}
3416 
3417 	ACQUIRE_LOCK(ump);
3418 	process_removes(dvp);
3419 	process_truncates(dvp);
3420 	VOP_UNLOCK(dvp);
3421 	softdep_speedup(ump);
3422 
3423 	process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3424 	if (journal_space(ump, 0) == 0) {
3425 		softdep_speedup(ump);
3426 		if (journal_space(ump, 1) == 0)
3427 			journal_suspend(ump);
3428 	}
3429 	FREE_LOCK(ump);
3430 
3431 	vn_lock_pair(dvp, false, vp, false);
3432 	return (ERELOOKUP);
3433 }
3434 
3435 static void
3436 jseg_write(ump, jseg, data)
3437 	struct ufsmount *ump;
3438 	struct jseg *jseg;
3439 	uint8_t *data;
3440 {
3441 	struct jsegrec *rec;
3442 
3443 	rec = (struct jsegrec *)data;
3444 	rec->jsr_seq = jseg->js_seq;
3445 	rec->jsr_oldest = jseg->js_oldseq;
3446 	rec->jsr_cnt = jseg->js_cnt;
3447 	rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3448 	rec->jsr_crc = 0;
3449 	rec->jsr_time = ump->um_fs->fs_mtime;
3450 }
3451 
3452 static inline void
3453 inoref_write(inoref, jseg, rec)
3454 	struct inoref *inoref;
3455 	struct jseg *jseg;
3456 	struct jrefrec *rec;
3457 {
3458 
3459 	inoref->if_jsegdep->jd_seg = jseg;
3460 	rec->jr_ino = inoref->if_ino;
3461 	rec->jr_parent = inoref->if_parent;
3462 	rec->jr_nlink = inoref->if_nlink;
3463 	rec->jr_mode = inoref->if_mode;
3464 	rec->jr_diroff = inoref->if_diroff;
3465 }
3466 
3467 static void
3468 jaddref_write(jaddref, jseg, data)
3469 	struct jaddref *jaddref;
3470 	struct jseg *jseg;
3471 	uint8_t *data;
3472 {
3473 	struct jrefrec *rec;
3474 
3475 	rec = (struct jrefrec *)data;
3476 	rec->jr_op = JOP_ADDREF;
3477 	inoref_write(&jaddref->ja_ref, jseg, rec);
3478 }
3479 
3480 static void
3481 jremref_write(jremref, jseg, data)
3482 	struct jremref *jremref;
3483 	struct jseg *jseg;
3484 	uint8_t *data;
3485 {
3486 	struct jrefrec *rec;
3487 
3488 	rec = (struct jrefrec *)data;
3489 	rec->jr_op = JOP_REMREF;
3490 	inoref_write(&jremref->jr_ref, jseg, rec);
3491 }
3492 
3493 static void
3494 jmvref_write(jmvref, jseg, data)
3495 	struct jmvref *jmvref;
3496 	struct jseg *jseg;
3497 	uint8_t *data;
3498 {
3499 	struct jmvrec *rec;
3500 
3501 	rec = (struct jmvrec *)data;
3502 	rec->jm_op = JOP_MVREF;
3503 	rec->jm_ino = jmvref->jm_ino;
3504 	rec->jm_parent = jmvref->jm_parent;
3505 	rec->jm_oldoff = jmvref->jm_oldoff;
3506 	rec->jm_newoff = jmvref->jm_newoff;
3507 }
3508 
3509 static void
3510 jnewblk_write(jnewblk, jseg, data)
3511 	struct jnewblk *jnewblk;
3512 	struct jseg *jseg;
3513 	uint8_t *data;
3514 {
3515 	struct jblkrec *rec;
3516 
3517 	jnewblk->jn_jsegdep->jd_seg = jseg;
3518 	rec = (struct jblkrec *)data;
3519 	rec->jb_op = JOP_NEWBLK;
3520 	rec->jb_ino = jnewblk->jn_ino;
3521 	rec->jb_blkno = jnewblk->jn_blkno;
3522 	rec->jb_lbn = jnewblk->jn_lbn;
3523 	rec->jb_frags = jnewblk->jn_frags;
3524 	rec->jb_oldfrags = jnewblk->jn_oldfrags;
3525 }
3526 
3527 static void
3528 jfreeblk_write(jfreeblk, jseg, data)
3529 	struct jfreeblk *jfreeblk;
3530 	struct jseg *jseg;
3531 	uint8_t *data;
3532 {
3533 	struct jblkrec *rec;
3534 
3535 	jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3536 	rec = (struct jblkrec *)data;
3537 	rec->jb_op = JOP_FREEBLK;
3538 	rec->jb_ino = jfreeblk->jf_ino;
3539 	rec->jb_blkno = jfreeblk->jf_blkno;
3540 	rec->jb_lbn = jfreeblk->jf_lbn;
3541 	rec->jb_frags = jfreeblk->jf_frags;
3542 	rec->jb_oldfrags = 0;
3543 }
3544 
3545 static void
3546 jfreefrag_write(jfreefrag, jseg, data)
3547 	struct jfreefrag *jfreefrag;
3548 	struct jseg *jseg;
3549 	uint8_t *data;
3550 {
3551 	struct jblkrec *rec;
3552 
3553 	jfreefrag->fr_jsegdep->jd_seg = jseg;
3554 	rec = (struct jblkrec *)data;
3555 	rec->jb_op = JOP_FREEBLK;
3556 	rec->jb_ino = jfreefrag->fr_ino;
3557 	rec->jb_blkno = jfreefrag->fr_blkno;
3558 	rec->jb_lbn = jfreefrag->fr_lbn;
3559 	rec->jb_frags = jfreefrag->fr_frags;
3560 	rec->jb_oldfrags = 0;
3561 }
3562 
3563 static void
3564 jtrunc_write(jtrunc, jseg, data)
3565 	struct jtrunc *jtrunc;
3566 	struct jseg *jseg;
3567 	uint8_t *data;
3568 {
3569 	struct jtrncrec *rec;
3570 
3571 	jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3572 	rec = (struct jtrncrec *)data;
3573 	rec->jt_op = JOP_TRUNC;
3574 	rec->jt_ino = jtrunc->jt_ino;
3575 	rec->jt_size = jtrunc->jt_size;
3576 	rec->jt_extsize = jtrunc->jt_extsize;
3577 }
3578 
3579 static void
3580 jfsync_write(jfsync, jseg, data)
3581 	struct jfsync *jfsync;
3582 	struct jseg *jseg;
3583 	uint8_t *data;
3584 {
3585 	struct jtrncrec *rec;
3586 
3587 	rec = (struct jtrncrec *)data;
3588 	rec->jt_op = JOP_SYNC;
3589 	rec->jt_ino = jfsync->jfs_ino;
3590 	rec->jt_size = jfsync->jfs_size;
3591 	rec->jt_extsize = jfsync->jfs_extsize;
3592 }
3593 
3594 static void
3595 softdep_flushjournal(mp)
3596 	struct mount *mp;
3597 {
3598 	struct jblocks *jblocks;
3599 	struct ufsmount *ump;
3600 
3601 	if (MOUNTEDSUJ(mp) == 0)
3602 		return;
3603 	ump = VFSTOUFS(mp);
3604 	jblocks = ump->softdep_jblocks;
3605 	ACQUIRE_LOCK(ump);
3606 	while (ump->softdep_on_journal) {
3607 		jblocks->jb_needseg = 1;
3608 		softdep_process_journal(mp, NULL, MNT_WAIT);
3609 	}
3610 	FREE_LOCK(ump);
3611 }
3612 
3613 static void softdep_synchronize_completed(struct bio *);
3614 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3615 
3616 static void
3617 softdep_synchronize_completed(bp)
3618         struct bio *bp;
3619 {
3620 	struct jseg *oldest;
3621 	struct jseg *jseg;
3622 	struct ufsmount *ump;
3623 
3624 	/*
3625 	 * caller1 marks the last segment written before we issued the
3626 	 * synchronize cache.
3627 	 */
3628 	jseg = bp->bio_caller1;
3629 	if (jseg == NULL) {
3630 		g_destroy_bio(bp);
3631 		return;
3632 	}
3633 	ump = VFSTOUFS(jseg->js_list.wk_mp);
3634 	ACQUIRE_LOCK(ump);
3635 	oldest = NULL;
3636 	/*
3637 	 * Mark all the journal entries waiting on the synchronize cache
3638 	 * as completed so they may continue on.
3639 	 */
3640 	while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3641 		jseg->js_state |= COMPLETE;
3642 		oldest = jseg;
3643 		jseg = TAILQ_PREV(jseg, jseglst, js_next);
3644 	}
3645 	/*
3646 	 * Restart deferred journal entry processing from the oldest
3647 	 * completed jseg.
3648 	 */
3649 	if (oldest)
3650 		complete_jsegs(oldest);
3651 
3652 	FREE_LOCK(ump);
3653 	g_destroy_bio(bp);
3654 }
3655 
3656 /*
3657  * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3658  * barriers.  The journal must be written prior to any blocks that depend
3659  * on it and the journal can not be released until the blocks have be
3660  * written.  This code handles both barriers simultaneously.
3661  */
3662 static void
3663 softdep_synchronize(bp, ump, caller1)
3664 	struct bio *bp;
3665 	struct ufsmount *ump;
3666 	void *caller1;
3667 {
3668 
3669 	bp->bio_cmd = BIO_FLUSH;
3670 	bp->bio_flags |= BIO_ORDERED;
3671 	bp->bio_data = NULL;
3672 	bp->bio_offset = ump->um_cp->provider->mediasize;
3673 	bp->bio_length = 0;
3674 	bp->bio_done = softdep_synchronize_completed;
3675 	bp->bio_caller1 = caller1;
3676 	g_io_request(bp, ump->um_cp);
3677 }
3678 
3679 /*
3680  * Flush some journal records to disk.
3681  */
3682 static void
3683 softdep_process_journal(mp, needwk, flags)
3684 	struct mount *mp;
3685 	struct worklist *needwk;
3686 	int flags;
3687 {
3688 	struct jblocks *jblocks;
3689 	struct ufsmount *ump;
3690 	struct worklist *wk;
3691 	struct jseg *jseg;
3692 	struct buf *bp;
3693 	struct bio *bio;
3694 	uint8_t *data;
3695 	struct fs *fs;
3696 	int shouldflush;
3697 	int segwritten;
3698 	int jrecmin;	/* Minimum records per block. */
3699 	int jrecmax;	/* Maximum records per block. */
3700 	int size;
3701 	int cnt;
3702 	int off;
3703 	int devbsize;
3704 
3705 	if (MOUNTEDSUJ(mp) == 0)
3706 		return;
3707 	shouldflush = softdep_flushcache;
3708 	bio = NULL;
3709 	jseg = NULL;
3710 	ump = VFSTOUFS(mp);
3711 	LOCK_OWNED(ump);
3712 	fs = ump->um_fs;
3713 	jblocks = ump->softdep_jblocks;
3714 	devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3715 	/*
3716 	 * We write anywhere between a disk block and fs block.  The upper
3717 	 * bound is picked to prevent buffer cache fragmentation and limit
3718 	 * processing time per I/O.
3719 	 */
3720 	jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3721 	jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3722 	segwritten = 0;
3723 	for (;;) {
3724 		cnt = ump->softdep_on_journal;
3725 		/*
3726 		 * Criteria for writing a segment:
3727 		 * 1) We have a full block.
3728 		 * 2) We're called from jwait() and haven't found the
3729 		 *    journal item yet.
3730 		 * 3) Always write if needseg is set.
3731 		 * 4) If we are called from process_worklist and have
3732 		 *    not yet written anything we write a partial block
3733 		 *    to enforce a 1 second maximum latency on journal
3734 		 *    entries.
3735 		 */
3736 		if (cnt < (jrecmax - 1) && needwk == NULL &&
3737 		    jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3738 			break;
3739 		cnt++;
3740 		/*
3741 		 * Verify some free journal space.  softdep_prealloc() should
3742 		 * guarantee that we don't run out so this is indicative of
3743 		 * a problem with the flow control.  Try to recover
3744 		 * gracefully in any event.
3745 		 */
3746 		while (jblocks->jb_free == 0) {
3747 			if (flags != MNT_WAIT)
3748 				break;
3749 			printf("softdep: Out of journal space!\n");
3750 			softdep_speedup(ump);
3751 			msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3752 		}
3753 		FREE_LOCK(ump);
3754 		jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3755 		workitem_alloc(&jseg->js_list, D_JSEG, mp);
3756 		LIST_INIT(&jseg->js_entries);
3757 		LIST_INIT(&jseg->js_indirs);
3758 		jseg->js_state = ATTACHED;
3759 		if (shouldflush == 0)
3760 			jseg->js_state |= COMPLETE;
3761 		else if (bio == NULL)
3762 			bio = g_alloc_bio();
3763 		jseg->js_jblocks = jblocks;
3764 		bp = geteblk(fs->fs_bsize, 0);
3765 		ACQUIRE_LOCK(ump);
3766 		/*
3767 		 * If there was a race while we were allocating the block
3768 		 * and jseg the entry we care about was likely written.
3769 		 * We bail out in both the WAIT and NOWAIT case and assume
3770 		 * the caller will loop if the entry it cares about is
3771 		 * not written.
3772 		 */
3773 		cnt = ump->softdep_on_journal;
3774 		if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3775 			bp->b_flags |= B_INVAL | B_NOCACHE;
3776 			WORKITEM_FREE(jseg, D_JSEG);
3777 			FREE_LOCK(ump);
3778 			brelse(bp);
3779 			ACQUIRE_LOCK(ump);
3780 			break;
3781 		}
3782 		/*
3783 		 * Calculate the disk block size required for the available
3784 		 * records rounded to the min size.
3785 		 */
3786 		if (cnt == 0)
3787 			size = devbsize;
3788 		else if (cnt < jrecmax)
3789 			size = howmany(cnt, jrecmin) * devbsize;
3790 		else
3791 			size = fs->fs_bsize;
3792 		/*
3793 		 * Allocate a disk block for this journal data and account
3794 		 * for truncation of the requested size if enough contiguous
3795 		 * space was not available.
3796 		 */
3797 		bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3798 		bp->b_lblkno = bp->b_blkno;
3799 		bp->b_offset = bp->b_blkno * DEV_BSIZE;
3800 		bp->b_bcount = size;
3801 		bp->b_flags &= ~B_INVAL;
3802 		bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3803 		/*
3804 		 * Initialize our jseg with cnt records.  Assign the next
3805 		 * sequence number to it and link it in-order.
3806 		 */
3807 		cnt = MIN(cnt, (size / devbsize) * jrecmin);
3808 		jseg->js_buf = bp;
3809 		jseg->js_cnt = cnt;
3810 		jseg->js_refs = cnt + 1;	/* Self ref. */
3811 		jseg->js_size = size;
3812 		jseg->js_seq = jblocks->jb_nextseq++;
3813 		if (jblocks->jb_oldestseg == NULL)
3814 			jblocks->jb_oldestseg = jseg;
3815 		jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3816 		TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3817 		if (jblocks->jb_writeseg == NULL)
3818 			jblocks->jb_writeseg = jseg;
3819 		/*
3820 		 * Start filling in records from the pending list.
3821 		 */
3822 		data = bp->b_data;
3823 		off = 0;
3824 
3825 		/*
3826 		 * Always put a header on the first block.
3827 		 * XXX As with below, there might not be a chance to get
3828 		 * into the loop.  Ensure that something valid is written.
3829 		 */
3830 		jseg_write(ump, jseg, data);
3831 		off += JREC_SIZE;
3832 		data = bp->b_data + off;
3833 
3834 		/*
3835 		 * XXX Something is wrong here.  There's no work to do,
3836 		 * but we need to perform and I/O and allow it to complete
3837 		 * anyways.
3838 		 */
3839 		if (LIST_EMPTY(&ump->softdep_journal_pending))
3840 			stat_emptyjblocks++;
3841 
3842 		while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3843 		    != NULL) {
3844 			if (cnt == 0)
3845 				break;
3846 			/* Place a segment header on every device block. */
3847 			if ((off % devbsize) == 0) {
3848 				jseg_write(ump, jseg, data);
3849 				off += JREC_SIZE;
3850 				data = bp->b_data + off;
3851 			}
3852 			if (wk == needwk)
3853 				needwk = NULL;
3854 			remove_from_journal(wk);
3855 			wk->wk_state |= INPROGRESS;
3856 			WORKLIST_INSERT(&jseg->js_entries, wk);
3857 			switch (wk->wk_type) {
3858 			case D_JADDREF:
3859 				jaddref_write(WK_JADDREF(wk), jseg, data);
3860 				break;
3861 			case D_JREMREF:
3862 				jremref_write(WK_JREMREF(wk), jseg, data);
3863 				break;
3864 			case D_JMVREF:
3865 				jmvref_write(WK_JMVREF(wk), jseg, data);
3866 				break;
3867 			case D_JNEWBLK:
3868 				jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3869 				break;
3870 			case D_JFREEBLK:
3871 				jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3872 				break;
3873 			case D_JFREEFRAG:
3874 				jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3875 				break;
3876 			case D_JTRUNC:
3877 				jtrunc_write(WK_JTRUNC(wk), jseg, data);
3878 				break;
3879 			case D_JFSYNC:
3880 				jfsync_write(WK_JFSYNC(wk), jseg, data);
3881 				break;
3882 			default:
3883 				panic("process_journal: Unknown type %s",
3884 				    TYPENAME(wk->wk_type));
3885 				/* NOTREACHED */
3886 			}
3887 			off += JREC_SIZE;
3888 			data = bp->b_data + off;
3889 			cnt--;
3890 		}
3891 
3892 		/* Clear any remaining space so we don't leak kernel data */
3893 		if (size > off)
3894 			bzero(data, size - off);
3895 
3896 		/*
3897 		 * Write this one buffer and continue.
3898 		 */
3899 		segwritten = 1;
3900 		jblocks->jb_needseg = 0;
3901 		WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3902 		FREE_LOCK(ump);
3903 		bp->b_xflags |= BX_CVTENXIO;
3904 		pbgetvp(ump->um_devvp, bp);
3905 		/*
3906 		 * We only do the blocking wait once we find the journal
3907 		 * entry we're looking for.
3908 		 */
3909 		if (needwk == NULL && flags == MNT_WAIT)
3910 			bwrite(bp);
3911 		else
3912 			bawrite(bp);
3913 		ACQUIRE_LOCK(ump);
3914 	}
3915 	/*
3916 	 * If we wrote a segment issue a synchronize cache so the journal
3917 	 * is reflected on disk before the data is written.  Since reclaiming
3918 	 * journal space also requires writing a journal record this
3919 	 * process also enforces a barrier before reclamation.
3920 	 */
3921 	if (segwritten && shouldflush) {
3922 		softdep_synchronize(bio, ump,
3923 		    TAILQ_LAST(&jblocks->jb_segs, jseglst));
3924 	} else if (bio)
3925 		g_destroy_bio(bio);
3926 	/*
3927 	 * If we've suspended the filesystem because we ran out of journal
3928 	 * space either try to sync it here to make some progress or
3929 	 * unsuspend it if we already have.
3930 	 */
3931 	if (flags == 0 && jblocks->jb_suspended) {
3932 		if (journal_unsuspend(ump))
3933 			return;
3934 		FREE_LOCK(ump);
3935 		VFS_SYNC(mp, MNT_NOWAIT);
3936 		ffs_sbupdate(ump, MNT_WAIT, 0);
3937 		ACQUIRE_LOCK(ump);
3938 	}
3939 }
3940 
3941 /*
3942  * Complete a jseg, allowing all dependencies awaiting journal writes
3943  * to proceed.  Each journal dependency also attaches a jsegdep to dependent
3944  * structures so that the journal segment can be freed to reclaim space.
3945  */
3946 static void
3947 complete_jseg(jseg)
3948 	struct jseg *jseg;
3949 {
3950 	struct worklist *wk;
3951 	struct jmvref *jmvref;
3952 #ifdef INVARIANTS
3953 	int i = 0;
3954 #endif
3955 
3956 	while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3957 		WORKLIST_REMOVE(wk);
3958 		wk->wk_state &= ~INPROGRESS;
3959 		wk->wk_state |= COMPLETE;
3960 		KASSERT(i++ < jseg->js_cnt,
3961 		    ("handle_written_jseg: overflow %d >= %d",
3962 		    i - 1, jseg->js_cnt));
3963 		switch (wk->wk_type) {
3964 		case D_JADDREF:
3965 			handle_written_jaddref(WK_JADDREF(wk));
3966 			break;
3967 		case D_JREMREF:
3968 			handle_written_jremref(WK_JREMREF(wk));
3969 			break;
3970 		case D_JMVREF:
3971 			rele_jseg(jseg);	/* No jsegdep. */
3972 			jmvref = WK_JMVREF(wk);
3973 			LIST_REMOVE(jmvref, jm_deps);
3974 			if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3975 				free_pagedep(jmvref->jm_pagedep);
3976 			WORKITEM_FREE(jmvref, D_JMVREF);
3977 			break;
3978 		case D_JNEWBLK:
3979 			handle_written_jnewblk(WK_JNEWBLK(wk));
3980 			break;
3981 		case D_JFREEBLK:
3982 			handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3983 			break;
3984 		case D_JTRUNC:
3985 			handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3986 			break;
3987 		case D_JFSYNC:
3988 			rele_jseg(jseg);	/* No jsegdep. */
3989 			WORKITEM_FREE(wk, D_JFSYNC);
3990 			break;
3991 		case D_JFREEFRAG:
3992 			handle_written_jfreefrag(WK_JFREEFRAG(wk));
3993 			break;
3994 		default:
3995 			panic("handle_written_jseg: Unknown type %s",
3996 			    TYPENAME(wk->wk_type));
3997 			/* NOTREACHED */
3998 		}
3999 	}
4000 	/* Release the self reference so the structure may be freed. */
4001 	rele_jseg(jseg);
4002 }
4003 
4004 /*
4005  * Determine which jsegs are ready for completion processing.  Waits for
4006  * synchronize cache to complete as well as forcing in-order completion
4007  * of journal entries.
4008  */
4009 static void
4010 complete_jsegs(jseg)
4011 	struct jseg *jseg;
4012 {
4013 	struct jblocks *jblocks;
4014 	struct jseg *jsegn;
4015 
4016 	jblocks = jseg->js_jblocks;
4017 	/*
4018 	 * Don't allow out of order completions.  If this isn't the first
4019 	 * block wait for it to write before we're done.
4020 	 */
4021 	if (jseg != jblocks->jb_writeseg)
4022 		return;
4023 	/* Iterate through available jsegs processing their entries. */
4024 	while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
4025 		jblocks->jb_oldestwrseq = jseg->js_oldseq;
4026 		jsegn = TAILQ_NEXT(jseg, js_next);
4027 		complete_jseg(jseg);
4028 		jseg = jsegn;
4029 	}
4030 	jblocks->jb_writeseg = jseg;
4031 	/*
4032 	 * Attempt to free jsegs now that oldestwrseq may have advanced.
4033 	 */
4034 	free_jsegs(jblocks);
4035 }
4036 
4037 /*
4038  * Mark a jseg as DEPCOMPLETE and throw away the buffer.  Attempt to handle
4039  * the final completions.
4040  */
4041 static void
4042 handle_written_jseg(jseg, bp)
4043 	struct jseg *jseg;
4044 	struct buf *bp;
4045 {
4046 
4047 	if (jseg->js_refs == 0)
4048 		panic("handle_written_jseg: No self-reference on %p", jseg);
4049 	jseg->js_state |= DEPCOMPLETE;
4050 	/*
4051 	 * We'll never need this buffer again, set flags so it will be
4052 	 * discarded.
4053 	 */
4054 	bp->b_flags |= B_INVAL | B_NOCACHE;
4055 	pbrelvp(bp);
4056 	complete_jsegs(jseg);
4057 }
4058 
4059 static inline struct jsegdep *
4060 inoref_jseg(inoref)
4061 	struct inoref *inoref;
4062 {
4063 	struct jsegdep *jsegdep;
4064 
4065 	jsegdep = inoref->if_jsegdep;
4066 	inoref->if_jsegdep = NULL;
4067 
4068 	return (jsegdep);
4069 }
4070 
4071 /*
4072  * Called once a jremref has made it to stable store.  The jremref is marked
4073  * complete and we attempt to free it.  Any pagedeps writes sleeping waiting
4074  * for the jremref to complete will be awoken by free_jremref.
4075  */
4076 static void
4077 handle_written_jremref(jremref)
4078 	struct jremref *jremref;
4079 {
4080 	struct inodedep *inodedep;
4081 	struct jsegdep *jsegdep;
4082 	struct dirrem *dirrem;
4083 
4084 	/* Grab the jsegdep. */
4085 	jsegdep = inoref_jseg(&jremref->jr_ref);
4086 	/*
4087 	 * Remove us from the inoref list.
4088 	 */
4089 	if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
4090 	    0, &inodedep) == 0)
4091 		panic("handle_written_jremref: Lost inodedep");
4092 	TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
4093 	/*
4094 	 * Complete the dirrem.
4095 	 */
4096 	dirrem = jremref->jr_dirrem;
4097 	jremref->jr_dirrem = NULL;
4098 	LIST_REMOVE(jremref, jr_deps);
4099 	jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
4100 	jwork_insert(&dirrem->dm_jwork, jsegdep);
4101 	if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
4102 	    (dirrem->dm_state & COMPLETE) != 0)
4103 		add_to_worklist(&dirrem->dm_list, 0);
4104 	free_jremref(jremref);
4105 }
4106 
4107 /*
4108  * Called once a jaddref has made it to stable store.  The dependency is
4109  * marked complete and any dependent structures are added to the inode
4110  * bufwait list to be completed as soon as it is written.  If a bitmap write
4111  * depends on this entry we move the inode into the inodedephd of the
4112  * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
4113  */
4114 static void
4115 handle_written_jaddref(jaddref)
4116 	struct jaddref *jaddref;
4117 {
4118 	struct jsegdep *jsegdep;
4119 	struct inodedep *inodedep;
4120 	struct diradd *diradd;
4121 	struct mkdir *mkdir;
4122 
4123 	/* Grab the jsegdep. */
4124 	jsegdep = inoref_jseg(&jaddref->ja_ref);
4125 	mkdir = NULL;
4126 	diradd = NULL;
4127 	if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4128 	    0, &inodedep) == 0)
4129 		panic("handle_written_jaddref: Lost inodedep.");
4130 	if (jaddref->ja_diradd == NULL)
4131 		panic("handle_written_jaddref: No dependency");
4132 	if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
4133 		diradd = jaddref->ja_diradd;
4134 		WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
4135 	} else if (jaddref->ja_state & MKDIR_PARENT) {
4136 		mkdir = jaddref->ja_mkdir;
4137 		WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
4138 	} else if (jaddref->ja_state & MKDIR_BODY)
4139 		mkdir = jaddref->ja_mkdir;
4140 	else
4141 		panic("handle_written_jaddref: Unknown dependency %p",
4142 		    jaddref->ja_diradd);
4143 	jaddref->ja_diradd = NULL;	/* also clears ja_mkdir */
4144 	/*
4145 	 * Remove us from the inode list.
4146 	 */
4147 	TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
4148 	/*
4149 	 * The mkdir may be waiting on the jaddref to clear before freeing.
4150 	 */
4151 	if (mkdir) {
4152 		KASSERT(mkdir->md_list.wk_type == D_MKDIR,
4153 		    ("handle_written_jaddref: Incorrect type for mkdir %s",
4154 		    TYPENAME(mkdir->md_list.wk_type)));
4155 		mkdir->md_jaddref = NULL;
4156 		diradd = mkdir->md_diradd;
4157 		mkdir->md_state |= DEPCOMPLETE;
4158 		complete_mkdir(mkdir);
4159 	}
4160 	jwork_insert(&diradd->da_jwork, jsegdep);
4161 	if (jaddref->ja_state & NEWBLOCK) {
4162 		inodedep->id_state |= ONDEPLIST;
4163 		LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
4164 		    inodedep, id_deps);
4165 	}
4166 	free_jaddref(jaddref);
4167 }
4168 
4169 /*
4170  * Called once a jnewblk journal is written.  The allocdirect or allocindir
4171  * is placed in the bmsafemap to await notification of a written bitmap.  If
4172  * the operation was canceled we add the segdep to the appropriate
4173  * dependency to free the journal space once the canceling operation
4174  * completes.
4175  */
4176 static void
4177 handle_written_jnewblk(jnewblk)
4178 	struct jnewblk *jnewblk;
4179 {
4180 	struct bmsafemap *bmsafemap;
4181 	struct freefrag *freefrag;
4182 	struct freework *freework;
4183 	struct jsegdep *jsegdep;
4184 	struct newblk *newblk;
4185 
4186 	/* Grab the jsegdep. */
4187 	jsegdep = jnewblk->jn_jsegdep;
4188 	jnewblk->jn_jsegdep = NULL;
4189 	if (jnewblk->jn_dep == NULL)
4190 		panic("handle_written_jnewblk: No dependency for the segdep.");
4191 	switch (jnewblk->jn_dep->wk_type) {
4192 	case D_NEWBLK:
4193 	case D_ALLOCDIRECT:
4194 	case D_ALLOCINDIR:
4195 		/*
4196 		 * Add the written block to the bmsafemap so it can
4197 		 * be notified when the bitmap is on disk.
4198 		 */
4199 		newblk = WK_NEWBLK(jnewblk->jn_dep);
4200 		newblk->nb_jnewblk = NULL;
4201 		if ((newblk->nb_state & GOINGAWAY) == 0) {
4202 			bmsafemap = newblk->nb_bmsafemap;
4203 			newblk->nb_state |= ONDEPLIST;
4204 			LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
4205 			    nb_deps);
4206 		}
4207 		jwork_insert(&newblk->nb_jwork, jsegdep);
4208 		break;
4209 	case D_FREEFRAG:
4210 		/*
4211 		 * A newblock being removed by a freefrag when replaced by
4212 		 * frag extension.
4213 		 */
4214 		freefrag = WK_FREEFRAG(jnewblk->jn_dep);
4215 		freefrag->ff_jdep = NULL;
4216 		jwork_insert(&freefrag->ff_jwork, jsegdep);
4217 		break;
4218 	case D_FREEWORK:
4219 		/*
4220 		 * A direct block was removed by truncate.
4221 		 */
4222 		freework = WK_FREEWORK(jnewblk->jn_dep);
4223 		freework->fw_jnewblk = NULL;
4224 		jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
4225 		break;
4226 	default:
4227 		panic("handle_written_jnewblk: Unknown type %d.",
4228 		    jnewblk->jn_dep->wk_type);
4229 	}
4230 	jnewblk->jn_dep = NULL;
4231 	free_jnewblk(jnewblk);
4232 }
4233 
4234 /*
4235  * Cancel a jfreefrag that won't be needed, probably due to colliding with
4236  * an in-flight allocation that has not yet been committed.  Divorce us
4237  * from the freefrag and mark it DEPCOMPLETE so that it may be added
4238  * to the worklist.
4239  */
4240 static void
4241 cancel_jfreefrag(jfreefrag)
4242 	struct jfreefrag *jfreefrag;
4243 {
4244 	struct freefrag *freefrag;
4245 
4246 	if (jfreefrag->fr_jsegdep) {
4247 		free_jsegdep(jfreefrag->fr_jsegdep);
4248 		jfreefrag->fr_jsegdep = NULL;
4249 	}
4250 	freefrag = jfreefrag->fr_freefrag;
4251 	jfreefrag->fr_freefrag = NULL;
4252 	free_jfreefrag(jfreefrag);
4253 	freefrag->ff_state |= DEPCOMPLETE;
4254 	CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
4255 }
4256 
4257 /*
4258  * Free a jfreefrag when the parent freefrag is rendered obsolete.
4259  */
4260 static void
4261 free_jfreefrag(jfreefrag)
4262 	struct jfreefrag *jfreefrag;
4263 {
4264 
4265 	if (jfreefrag->fr_state & INPROGRESS)
4266 		WORKLIST_REMOVE(&jfreefrag->fr_list);
4267 	else if (jfreefrag->fr_state & ONWORKLIST)
4268 		remove_from_journal(&jfreefrag->fr_list);
4269 	if (jfreefrag->fr_freefrag != NULL)
4270 		panic("free_jfreefrag:  Still attached to a freefrag.");
4271 	WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
4272 }
4273 
4274 /*
4275  * Called when the journal write for a jfreefrag completes.  The parent
4276  * freefrag is added to the worklist if this completes its dependencies.
4277  */
4278 static void
4279 handle_written_jfreefrag(jfreefrag)
4280 	struct jfreefrag *jfreefrag;
4281 {
4282 	struct jsegdep *jsegdep;
4283 	struct freefrag *freefrag;
4284 
4285 	/* Grab the jsegdep. */
4286 	jsegdep = jfreefrag->fr_jsegdep;
4287 	jfreefrag->fr_jsegdep = NULL;
4288 	freefrag = jfreefrag->fr_freefrag;
4289 	if (freefrag == NULL)
4290 		panic("handle_written_jfreefrag: No freefrag.");
4291 	freefrag->ff_state |= DEPCOMPLETE;
4292 	freefrag->ff_jdep = NULL;
4293 	jwork_insert(&freefrag->ff_jwork, jsegdep);
4294 	if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
4295 		add_to_worklist(&freefrag->ff_list, 0);
4296 	jfreefrag->fr_freefrag = NULL;
4297 	free_jfreefrag(jfreefrag);
4298 }
4299 
4300 /*
4301  * Called when the journal write for a jfreeblk completes.  The jfreeblk
4302  * is removed from the freeblks list of pending journal writes and the
4303  * jsegdep is moved to the freeblks jwork to be completed when all blocks
4304  * have been reclaimed.
4305  */
4306 static void
4307 handle_written_jblkdep(jblkdep)
4308 	struct jblkdep *jblkdep;
4309 {
4310 	struct freeblks *freeblks;
4311 	struct jsegdep *jsegdep;
4312 
4313 	/* Grab the jsegdep. */
4314 	jsegdep = jblkdep->jb_jsegdep;
4315 	jblkdep->jb_jsegdep = NULL;
4316 	freeblks = jblkdep->jb_freeblks;
4317 	LIST_REMOVE(jblkdep, jb_deps);
4318 	jwork_insert(&freeblks->fb_jwork, jsegdep);
4319 	/*
4320 	 * If the freeblks is all journaled, we can add it to the worklist.
4321 	 */
4322 	if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4323 	    (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4324 		add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4325 
4326 	free_jblkdep(jblkdep);
4327 }
4328 
4329 static struct jsegdep *
4330 newjsegdep(struct worklist *wk)
4331 {
4332 	struct jsegdep *jsegdep;
4333 
4334 	jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4335 	workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4336 	jsegdep->jd_seg = NULL;
4337 
4338 	return (jsegdep);
4339 }
4340 
4341 static struct jmvref *
4342 newjmvref(dp, ino, oldoff, newoff)
4343 	struct inode *dp;
4344 	ino_t ino;
4345 	off_t oldoff;
4346 	off_t newoff;
4347 {
4348 	struct jmvref *jmvref;
4349 
4350 	jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4351 	workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4352 	jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4353 	jmvref->jm_parent = dp->i_number;
4354 	jmvref->jm_ino = ino;
4355 	jmvref->jm_oldoff = oldoff;
4356 	jmvref->jm_newoff = newoff;
4357 
4358 	return (jmvref);
4359 }
4360 
4361 /*
4362  * Allocate a new jremref that tracks the removal of ip from dp with the
4363  * directory entry offset of diroff.  Mark the entry as ATTACHED and
4364  * DEPCOMPLETE as we have all the information required for the journal write
4365  * and the directory has already been removed from the buffer.  The caller
4366  * is responsible for linking the jremref into the pagedep and adding it
4367  * to the journal to write.  The MKDIR_PARENT flag is set if we're doing
4368  * a DOTDOT addition so handle_workitem_remove() can properly assign
4369  * the jsegdep when we're done.
4370  */
4371 static struct jremref *
4372 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4373     off_t diroff, nlink_t nlink)
4374 {
4375 	struct jremref *jremref;
4376 
4377 	jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4378 	workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4379 	jremref->jr_state = ATTACHED;
4380 	newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4381 	   nlink, ip->i_mode);
4382 	jremref->jr_dirrem = dirrem;
4383 
4384 	return (jremref);
4385 }
4386 
4387 static inline void
4388 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4389     nlink_t nlink, uint16_t mode)
4390 {
4391 
4392 	inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4393 	inoref->if_diroff = diroff;
4394 	inoref->if_ino = ino;
4395 	inoref->if_parent = parent;
4396 	inoref->if_nlink = nlink;
4397 	inoref->if_mode = mode;
4398 }
4399 
4400 /*
4401  * Allocate a new jaddref to track the addition of ino to dp at diroff.  The
4402  * directory offset may not be known until later.  The caller is responsible
4403  * adding the entry to the journal when this information is available.  nlink
4404  * should be the link count prior to the addition and mode is only required
4405  * to have the correct FMT.
4406  */
4407 static struct jaddref *
4408 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4409     uint16_t mode)
4410 {
4411 	struct jaddref *jaddref;
4412 
4413 	jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4414 	workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4415 	jaddref->ja_state = ATTACHED;
4416 	jaddref->ja_mkdir = NULL;
4417 	newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4418 
4419 	return (jaddref);
4420 }
4421 
4422 /*
4423  * Create a new free dependency for a freework.  The caller is responsible
4424  * for adjusting the reference count when it has the lock held.  The freedep
4425  * will track an outstanding bitmap write that will ultimately clear the
4426  * freework to continue.
4427  */
4428 static struct freedep *
4429 newfreedep(struct freework *freework)
4430 {
4431 	struct freedep *freedep;
4432 
4433 	freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4434 	workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4435 	freedep->fd_freework = freework;
4436 
4437 	return (freedep);
4438 }
4439 
4440 /*
4441  * Free a freedep structure once the buffer it is linked to is written.  If
4442  * this is the last reference to the freework schedule it for completion.
4443  */
4444 static void
4445 free_freedep(freedep)
4446 	struct freedep *freedep;
4447 {
4448 	struct freework *freework;
4449 
4450 	freework = freedep->fd_freework;
4451 	freework->fw_freeblks->fb_cgwait--;
4452 	if (--freework->fw_ref == 0)
4453 		freework_enqueue(freework);
4454 	WORKITEM_FREE(freedep, D_FREEDEP);
4455 }
4456 
4457 /*
4458  * Allocate a new freework structure that may be a level in an indirect
4459  * when parent is not NULL or a top level block when it is.  The top level
4460  * freework structures are allocated without the per-filesystem lock held
4461  * and before the freeblks is visible outside of softdep_setup_freeblocks().
4462  */
4463 static struct freework *
4464 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4465 	struct ufsmount *ump;
4466 	struct freeblks *freeblks;
4467 	struct freework *parent;
4468 	ufs_lbn_t lbn;
4469 	ufs2_daddr_t nb;
4470 	int frags;
4471 	int off;
4472 	int journal;
4473 {
4474 	struct freework *freework;
4475 
4476 	freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4477 	workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4478 	freework->fw_state = ATTACHED;
4479 	freework->fw_jnewblk = NULL;
4480 	freework->fw_freeblks = freeblks;
4481 	freework->fw_parent = parent;
4482 	freework->fw_lbn = lbn;
4483 	freework->fw_blkno = nb;
4484 	freework->fw_frags = frags;
4485 	freework->fw_indir = NULL;
4486 	freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4487 	    lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4488 	freework->fw_start = freework->fw_off = off;
4489 	if (journal)
4490 		newjfreeblk(freeblks, lbn, nb, frags);
4491 	if (parent == NULL) {
4492 		ACQUIRE_LOCK(ump);
4493 		WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4494 		freeblks->fb_ref++;
4495 		FREE_LOCK(ump);
4496 	}
4497 
4498 	return (freework);
4499 }
4500 
4501 /*
4502  * Eliminate a jfreeblk for a block that does not need journaling.
4503  */
4504 static void
4505 cancel_jfreeblk(freeblks, blkno)
4506 	struct freeblks *freeblks;
4507 	ufs2_daddr_t blkno;
4508 {
4509 	struct jfreeblk *jfreeblk;
4510 	struct jblkdep *jblkdep;
4511 
4512 	LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4513 		if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4514 			continue;
4515 		jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4516 		if (jfreeblk->jf_blkno == blkno)
4517 			break;
4518 	}
4519 	if (jblkdep == NULL)
4520 		return;
4521 	CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4522 	free_jsegdep(jblkdep->jb_jsegdep);
4523 	LIST_REMOVE(jblkdep, jb_deps);
4524 	WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4525 }
4526 
4527 /*
4528  * Allocate a new jfreeblk to journal top level block pointer when truncating
4529  * a file.  The caller must add this to the worklist when the per-filesystem
4530  * lock is held.
4531  */
4532 static struct jfreeblk *
4533 newjfreeblk(freeblks, lbn, blkno, frags)
4534 	struct freeblks *freeblks;
4535 	ufs_lbn_t lbn;
4536 	ufs2_daddr_t blkno;
4537 	int frags;
4538 {
4539 	struct jfreeblk *jfreeblk;
4540 
4541 	jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4542 	workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4543 	    freeblks->fb_list.wk_mp);
4544 	jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4545 	jfreeblk->jf_dep.jb_freeblks = freeblks;
4546 	jfreeblk->jf_ino = freeblks->fb_inum;
4547 	jfreeblk->jf_lbn = lbn;
4548 	jfreeblk->jf_blkno = blkno;
4549 	jfreeblk->jf_frags = frags;
4550 	LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4551 
4552 	return (jfreeblk);
4553 }
4554 
4555 /*
4556  * The journal is only prepared to handle full-size block numbers, so we
4557  * have to adjust the record to reflect the change to a full-size block.
4558  * For example, suppose we have a block made up of fragments 8-15 and
4559  * want to free its last two fragments. We are given a request that says:
4560  *     FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4561  * where frags are the number of fragments to free and oldfrags are the
4562  * number of fragments to keep. To block align it, we have to change it to
4563  * have a valid full-size blkno, so it becomes:
4564  *     FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4565  */
4566 static void
4567 adjust_newfreework(freeblks, frag_offset)
4568 	struct freeblks *freeblks;
4569 	int frag_offset;
4570 {
4571 	struct jfreeblk *jfreeblk;
4572 
4573 	KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4574 	    LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4575 	    ("adjust_newfreework: Missing freeblks dependency"));
4576 
4577 	jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4578 	jfreeblk->jf_blkno -= frag_offset;
4579 	jfreeblk->jf_frags += frag_offset;
4580 }
4581 
4582 /*
4583  * Allocate a new jtrunc to track a partial truncation.
4584  */
4585 static struct jtrunc *
4586 newjtrunc(freeblks, size, extsize)
4587 	struct freeblks *freeblks;
4588 	off_t size;
4589 	int extsize;
4590 {
4591 	struct jtrunc *jtrunc;
4592 
4593 	jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4594 	workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4595 	    freeblks->fb_list.wk_mp);
4596 	jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4597 	jtrunc->jt_dep.jb_freeblks = freeblks;
4598 	jtrunc->jt_ino = freeblks->fb_inum;
4599 	jtrunc->jt_size = size;
4600 	jtrunc->jt_extsize = extsize;
4601 	LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4602 
4603 	return (jtrunc);
4604 }
4605 
4606 /*
4607  * If we're canceling a new bitmap we have to search for another ref
4608  * to move into the bmsafemap dep.  This might be better expressed
4609  * with another structure.
4610  */
4611 static void
4612 move_newblock_dep(jaddref, inodedep)
4613 	struct jaddref *jaddref;
4614 	struct inodedep *inodedep;
4615 {
4616 	struct inoref *inoref;
4617 	struct jaddref *jaddrefn;
4618 
4619 	jaddrefn = NULL;
4620 	for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4621 	    inoref = TAILQ_NEXT(inoref, if_deps)) {
4622 		if ((jaddref->ja_state & NEWBLOCK) &&
4623 		    inoref->if_list.wk_type == D_JADDREF) {
4624 			jaddrefn = (struct jaddref *)inoref;
4625 			break;
4626 		}
4627 	}
4628 	if (jaddrefn == NULL)
4629 		return;
4630 	jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4631 	jaddrefn->ja_state |= jaddref->ja_state &
4632 	    (ATTACHED | UNDONE | NEWBLOCK);
4633 	jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4634 	jaddref->ja_state |= ATTACHED;
4635 	LIST_REMOVE(jaddref, ja_bmdeps);
4636 	LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4637 	    ja_bmdeps);
4638 }
4639 
4640 /*
4641  * Cancel a jaddref either before it has been written or while it is being
4642  * written.  This happens when a link is removed before the add reaches
4643  * the disk.  The jaddref dependency is kept linked into the bmsafemap
4644  * and inode to prevent the link count or bitmap from reaching the disk
4645  * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4646  * required.
4647  *
4648  * Returns 1 if the canceled addref requires journaling of the remove and
4649  * 0 otherwise.
4650  */
4651 static int
4652 cancel_jaddref(jaddref, inodedep, wkhd)
4653 	struct jaddref *jaddref;
4654 	struct inodedep *inodedep;
4655 	struct workhead *wkhd;
4656 {
4657 	struct inoref *inoref;
4658 	struct jsegdep *jsegdep;
4659 	int needsj;
4660 
4661 	KASSERT((jaddref->ja_state & COMPLETE) == 0,
4662 	    ("cancel_jaddref: Canceling complete jaddref"));
4663 	if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4664 		needsj = 1;
4665 	else
4666 		needsj = 0;
4667 	if (inodedep == NULL)
4668 		if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4669 		    0, &inodedep) == 0)
4670 			panic("cancel_jaddref: Lost inodedep");
4671 	/*
4672 	 * We must adjust the nlink of any reference operation that follows
4673 	 * us so that it is consistent with the in-memory reference.  This
4674 	 * ensures that inode nlink rollbacks always have the correct link.
4675 	 */
4676 	if (needsj == 0) {
4677 		for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4678 		    inoref = TAILQ_NEXT(inoref, if_deps)) {
4679 			if (inoref->if_state & GOINGAWAY)
4680 				break;
4681 			inoref->if_nlink--;
4682 		}
4683 	}
4684 	jsegdep = inoref_jseg(&jaddref->ja_ref);
4685 	if (jaddref->ja_state & NEWBLOCK)
4686 		move_newblock_dep(jaddref, inodedep);
4687 	wake_worklist(&jaddref->ja_list);
4688 	jaddref->ja_mkdir = NULL;
4689 	if (jaddref->ja_state & INPROGRESS) {
4690 		jaddref->ja_state &= ~INPROGRESS;
4691 		WORKLIST_REMOVE(&jaddref->ja_list);
4692 		jwork_insert(wkhd, jsegdep);
4693 	} else {
4694 		free_jsegdep(jsegdep);
4695 		if (jaddref->ja_state & DEPCOMPLETE)
4696 			remove_from_journal(&jaddref->ja_list);
4697 	}
4698 	jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4699 	/*
4700 	 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4701 	 * can arrange for them to be freed with the bitmap.  Otherwise we
4702 	 * no longer need this addref attached to the inoreflst and it
4703 	 * will incorrectly adjust nlink if we leave it.
4704 	 */
4705 	if ((jaddref->ja_state & NEWBLOCK) == 0) {
4706 		TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4707 		    if_deps);
4708 		jaddref->ja_state |= COMPLETE;
4709 		free_jaddref(jaddref);
4710 		return (needsj);
4711 	}
4712 	/*
4713 	 * Leave the head of the list for jsegdeps for fast merging.
4714 	 */
4715 	if (LIST_FIRST(wkhd) != NULL) {
4716 		jaddref->ja_state |= ONWORKLIST;
4717 		LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4718 	} else
4719 		WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4720 
4721 	return (needsj);
4722 }
4723 
4724 /*
4725  * Attempt to free a jaddref structure when some work completes.  This
4726  * should only succeed once the entry is written and all dependencies have
4727  * been notified.
4728  */
4729 static void
4730 free_jaddref(jaddref)
4731 	struct jaddref *jaddref;
4732 {
4733 
4734 	if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4735 		return;
4736 	if (jaddref->ja_ref.if_jsegdep)
4737 		panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4738 		    jaddref, jaddref->ja_state);
4739 	if (jaddref->ja_state & NEWBLOCK)
4740 		LIST_REMOVE(jaddref, ja_bmdeps);
4741 	if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4742 		panic("free_jaddref: Bad state %p(0x%X)",
4743 		    jaddref, jaddref->ja_state);
4744 	if (jaddref->ja_mkdir != NULL)
4745 		panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4746 	WORKITEM_FREE(jaddref, D_JADDREF);
4747 }
4748 
4749 /*
4750  * Free a jremref structure once it has been written or discarded.
4751  */
4752 static void
4753 free_jremref(jremref)
4754 	struct jremref *jremref;
4755 {
4756 
4757 	if (jremref->jr_ref.if_jsegdep)
4758 		free_jsegdep(jremref->jr_ref.if_jsegdep);
4759 	if (jremref->jr_state & INPROGRESS)
4760 		panic("free_jremref: IO still pending");
4761 	WORKITEM_FREE(jremref, D_JREMREF);
4762 }
4763 
4764 /*
4765  * Free a jnewblk structure.
4766  */
4767 static void
4768 free_jnewblk(jnewblk)
4769 	struct jnewblk *jnewblk;
4770 {
4771 
4772 	if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4773 		return;
4774 	LIST_REMOVE(jnewblk, jn_deps);
4775 	if (jnewblk->jn_dep != NULL)
4776 		panic("free_jnewblk: Dependency still attached.");
4777 	WORKITEM_FREE(jnewblk, D_JNEWBLK);
4778 }
4779 
4780 /*
4781  * Cancel a jnewblk which has been been made redundant by frag extension.
4782  */
4783 static void
4784 cancel_jnewblk(jnewblk, wkhd)
4785 	struct jnewblk *jnewblk;
4786 	struct workhead *wkhd;
4787 {
4788 	struct jsegdep *jsegdep;
4789 
4790 	CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4791 	jsegdep = jnewblk->jn_jsegdep;
4792 	if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4793 		panic("cancel_jnewblk: Invalid state");
4794 	jnewblk->jn_jsegdep  = NULL;
4795 	jnewblk->jn_dep = NULL;
4796 	jnewblk->jn_state |= GOINGAWAY;
4797 	if (jnewblk->jn_state & INPROGRESS) {
4798 		jnewblk->jn_state &= ~INPROGRESS;
4799 		WORKLIST_REMOVE(&jnewblk->jn_list);
4800 		jwork_insert(wkhd, jsegdep);
4801 	} else {
4802 		free_jsegdep(jsegdep);
4803 		remove_from_journal(&jnewblk->jn_list);
4804 	}
4805 	wake_worklist(&jnewblk->jn_list);
4806 	WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4807 }
4808 
4809 static void
4810 free_jblkdep(jblkdep)
4811 	struct jblkdep *jblkdep;
4812 {
4813 
4814 	if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4815 		WORKITEM_FREE(jblkdep, D_JFREEBLK);
4816 	else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4817 		WORKITEM_FREE(jblkdep, D_JTRUNC);
4818 	else
4819 		panic("free_jblkdep: Unexpected type %s",
4820 		    TYPENAME(jblkdep->jb_list.wk_type));
4821 }
4822 
4823 /*
4824  * Free a single jseg once it is no longer referenced in memory or on
4825  * disk.  Reclaim journal blocks and dependencies waiting for the segment
4826  * to disappear.
4827  */
4828 static void
4829 free_jseg(jseg, jblocks)
4830 	struct jseg *jseg;
4831 	struct jblocks *jblocks;
4832 {
4833 	struct freework *freework;
4834 
4835 	/*
4836 	 * Free freework structures that were lingering to indicate freed
4837 	 * indirect blocks that forced journal write ordering on reallocate.
4838 	 */
4839 	while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4840 		indirblk_remove(freework);
4841 	if (jblocks->jb_oldestseg == jseg)
4842 		jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4843 	TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4844 	jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4845 	KASSERT(LIST_EMPTY(&jseg->js_entries),
4846 	    ("free_jseg: Freed jseg has valid entries."));
4847 	WORKITEM_FREE(jseg, D_JSEG);
4848 }
4849 
4850 /*
4851  * Free all jsegs that meet the criteria for being reclaimed and update
4852  * oldestseg.
4853  */
4854 static void
4855 free_jsegs(jblocks)
4856 	struct jblocks *jblocks;
4857 {
4858 	struct jseg *jseg;
4859 
4860 	/*
4861 	 * Free only those jsegs which have none allocated before them to
4862 	 * preserve the journal space ordering.
4863 	 */
4864 	while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4865 		/*
4866 		 * Only reclaim space when nothing depends on this journal
4867 		 * set and another set has written that it is no longer
4868 		 * valid.
4869 		 */
4870 		if (jseg->js_refs != 0) {
4871 			jblocks->jb_oldestseg = jseg;
4872 			return;
4873 		}
4874 		if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4875 			break;
4876 		if (jseg->js_seq > jblocks->jb_oldestwrseq)
4877 			break;
4878 		/*
4879 		 * We can free jsegs that didn't write entries when
4880 		 * oldestwrseq == js_seq.
4881 		 */
4882 		if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4883 		    jseg->js_cnt != 0)
4884 			break;
4885 		free_jseg(jseg, jblocks);
4886 	}
4887 	/*
4888 	 * If we exited the loop above we still must discover the
4889 	 * oldest valid segment.
4890 	 */
4891 	if (jseg)
4892 		for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4893 		     jseg = TAILQ_NEXT(jseg, js_next))
4894 			if (jseg->js_refs != 0)
4895 				break;
4896 	jblocks->jb_oldestseg = jseg;
4897 	/*
4898 	 * The journal has no valid records but some jsegs may still be
4899 	 * waiting on oldestwrseq to advance.  We force a small record
4900 	 * out to permit these lingering records to be reclaimed.
4901 	 */
4902 	if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4903 		jblocks->jb_needseg = 1;
4904 }
4905 
4906 /*
4907  * Release one reference to a jseg and free it if the count reaches 0.  This
4908  * should eventually reclaim journal space as well.
4909  */
4910 static void
4911 rele_jseg(jseg)
4912 	struct jseg *jseg;
4913 {
4914 
4915 	KASSERT(jseg->js_refs > 0,
4916 	    ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4917 	if (--jseg->js_refs != 0)
4918 		return;
4919 	free_jsegs(jseg->js_jblocks);
4920 }
4921 
4922 /*
4923  * Release a jsegdep and decrement the jseg count.
4924  */
4925 static void
4926 free_jsegdep(jsegdep)
4927 	struct jsegdep *jsegdep;
4928 {
4929 
4930 	if (jsegdep->jd_seg)
4931 		rele_jseg(jsegdep->jd_seg);
4932 	WORKITEM_FREE(jsegdep, D_JSEGDEP);
4933 }
4934 
4935 /*
4936  * Wait for a journal item to make it to disk.  Initiate journal processing
4937  * if required.
4938  */
4939 static int
4940 jwait(wk, waitfor)
4941 	struct worklist *wk;
4942 	int waitfor;
4943 {
4944 
4945 	LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4946 	/*
4947 	 * Blocking journal waits cause slow synchronous behavior.  Record
4948 	 * stats on the frequency of these blocking operations.
4949 	 */
4950 	if (waitfor == MNT_WAIT) {
4951 		stat_journal_wait++;
4952 		switch (wk->wk_type) {
4953 		case D_JREMREF:
4954 		case D_JMVREF:
4955 			stat_jwait_filepage++;
4956 			break;
4957 		case D_JTRUNC:
4958 		case D_JFREEBLK:
4959 			stat_jwait_freeblks++;
4960 			break;
4961 		case D_JNEWBLK:
4962 			stat_jwait_newblk++;
4963 			break;
4964 		case D_JADDREF:
4965 			stat_jwait_inode++;
4966 			break;
4967 		default:
4968 			break;
4969 		}
4970 	}
4971 	/*
4972 	 * If IO has not started we process the journal.  We can't mark the
4973 	 * worklist item as IOWAITING because we drop the lock while
4974 	 * processing the journal and the worklist entry may be freed after
4975 	 * this point.  The caller may call back in and re-issue the request.
4976 	 */
4977 	if ((wk->wk_state & INPROGRESS) == 0) {
4978 		softdep_process_journal(wk->wk_mp, wk, waitfor);
4979 		if (waitfor != MNT_WAIT)
4980 			return (EBUSY);
4981 		return (0);
4982 	}
4983 	if (waitfor != MNT_WAIT)
4984 		return (EBUSY);
4985 	wait_worklist(wk, "jwait");
4986 	return (0);
4987 }
4988 
4989 /*
4990  * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4991  * appropriate.  This is a convenience function to reduce duplicate code
4992  * for the setup and revert functions below.
4993  */
4994 static struct inodedep *
4995 inodedep_lookup_ip(ip)
4996 	struct inode *ip;
4997 {
4998 	struct inodedep *inodedep;
4999 
5000 	KASSERT(ip->i_nlink >= ip->i_effnlink,
5001 	    ("inodedep_lookup_ip: bad delta"));
5002 	(void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
5003 	    &inodedep);
5004 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
5005 	KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
5006 
5007 	return (inodedep);
5008 }
5009 
5010 /*
5011  * Called prior to creating a new inode and linking it to a directory.  The
5012  * jaddref structure must already be allocated by softdep_setup_inomapdep
5013  * and it is discovered here so we can initialize the mode and update
5014  * nlinkdelta.
5015  */
5016 void
5017 softdep_setup_create(dp, ip)
5018 	struct inode *dp;
5019 	struct inode *ip;
5020 {
5021 	struct inodedep *inodedep;
5022 	struct jaddref *jaddref;
5023 	struct vnode *dvp;
5024 
5025 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5026 	    ("softdep_setup_create called on non-softdep filesystem"));
5027 	KASSERT(ip->i_nlink == 1,
5028 	    ("softdep_setup_create: Invalid link count."));
5029 	dvp = ITOV(dp);
5030 	ACQUIRE_LOCK(ITOUMP(dp));
5031 	inodedep = inodedep_lookup_ip(ip);
5032 	if (DOINGSUJ(dvp)) {
5033 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5034 		    inoreflst);
5035 		KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
5036 		    ("softdep_setup_create: No addref structure present."));
5037 	}
5038 	FREE_LOCK(ITOUMP(dp));
5039 }
5040 
5041 /*
5042  * Create a jaddref structure to track the addition of a DOTDOT link when
5043  * we are reparenting an inode as part of a rename.  This jaddref will be
5044  * found by softdep_setup_directory_change.  Adjusts nlinkdelta for
5045  * non-journaling softdep.
5046  */
5047 void
5048 softdep_setup_dotdot_link(dp, ip)
5049 	struct inode *dp;
5050 	struct inode *ip;
5051 {
5052 	struct inodedep *inodedep;
5053 	struct jaddref *jaddref;
5054 	struct vnode *dvp;
5055 
5056 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5057 	    ("softdep_setup_dotdot_link called on non-softdep filesystem"));
5058 	dvp = ITOV(dp);
5059 	jaddref = NULL;
5060 	/*
5061 	 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
5062 	 * is used as a normal link would be.
5063 	 */
5064 	if (DOINGSUJ(dvp))
5065 		jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5066 		    dp->i_effnlink - 1, dp->i_mode);
5067 	ACQUIRE_LOCK(ITOUMP(dp));
5068 	inodedep = inodedep_lookup_ip(dp);
5069 	if (jaddref)
5070 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5071 		    if_deps);
5072 	FREE_LOCK(ITOUMP(dp));
5073 }
5074 
5075 /*
5076  * Create a jaddref structure to track a new link to an inode.  The directory
5077  * offset is not known until softdep_setup_directory_add or
5078  * softdep_setup_directory_change.  Adjusts nlinkdelta for non-journaling
5079  * softdep.
5080  */
5081 void
5082 softdep_setup_link(dp, ip)
5083 	struct inode *dp;
5084 	struct inode *ip;
5085 {
5086 	struct inodedep *inodedep;
5087 	struct jaddref *jaddref;
5088 	struct vnode *dvp;
5089 
5090 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5091 	    ("softdep_setup_link called on non-softdep filesystem"));
5092 	dvp = ITOV(dp);
5093 	jaddref = NULL;
5094 	if (DOINGSUJ(dvp))
5095 		jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
5096 		    ip->i_mode);
5097 	ACQUIRE_LOCK(ITOUMP(dp));
5098 	inodedep = inodedep_lookup_ip(ip);
5099 	if (jaddref)
5100 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5101 		    if_deps);
5102 	FREE_LOCK(ITOUMP(dp));
5103 }
5104 
5105 /*
5106  * Called to create the jaddref structures to track . and .. references as
5107  * well as lookup and further initialize the incomplete jaddref created
5108  * by softdep_setup_inomapdep when the inode was allocated.  Adjusts
5109  * nlinkdelta for non-journaling softdep.
5110  */
5111 void
5112 softdep_setup_mkdir(dp, ip)
5113 	struct inode *dp;
5114 	struct inode *ip;
5115 {
5116 	struct inodedep *inodedep;
5117 	struct jaddref *dotdotaddref;
5118 	struct jaddref *dotaddref;
5119 	struct jaddref *jaddref;
5120 	struct vnode *dvp;
5121 
5122 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5123 	    ("softdep_setup_mkdir called on non-softdep filesystem"));
5124 	dvp = ITOV(dp);
5125 	dotaddref = dotdotaddref = NULL;
5126 	if (DOINGSUJ(dvp)) {
5127 		dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
5128 		    ip->i_mode);
5129 		dotaddref->ja_state |= MKDIR_BODY;
5130 		dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5131 		    dp->i_effnlink - 1, dp->i_mode);
5132 		dotdotaddref->ja_state |= MKDIR_PARENT;
5133 	}
5134 	ACQUIRE_LOCK(ITOUMP(dp));
5135 	inodedep = inodedep_lookup_ip(ip);
5136 	if (DOINGSUJ(dvp)) {
5137 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5138 		    inoreflst);
5139 		KASSERT(jaddref != NULL,
5140 		    ("softdep_setup_mkdir: No addref structure present."));
5141 		KASSERT(jaddref->ja_parent == dp->i_number,
5142 		    ("softdep_setup_mkdir: bad parent %ju",
5143 		    (uintmax_t)jaddref->ja_parent));
5144 		TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
5145 		    if_deps);
5146 	}
5147 	inodedep = inodedep_lookup_ip(dp);
5148 	if (DOINGSUJ(dvp))
5149 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
5150 		    &dotdotaddref->ja_ref, if_deps);
5151 	FREE_LOCK(ITOUMP(dp));
5152 }
5153 
5154 /*
5155  * Called to track nlinkdelta of the inode and parent directories prior to
5156  * unlinking a directory.
5157  */
5158 void
5159 softdep_setup_rmdir(dp, ip)
5160 	struct inode *dp;
5161 	struct inode *ip;
5162 {
5163 	struct vnode *dvp;
5164 
5165 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5166 	    ("softdep_setup_rmdir called on non-softdep filesystem"));
5167 	dvp = ITOV(dp);
5168 	ACQUIRE_LOCK(ITOUMP(dp));
5169 	(void) inodedep_lookup_ip(ip);
5170 	(void) inodedep_lookup_ip(dp);
5171 	FREE_LOCK(ITOUMP(dp));
5172 }
5173 
5174 /*
5175  * Called to track nlinkdelta of the inode and parent directories prior to
5176  * unlink.
5177  */
5178 void
5179 softdep_setup_unlink(dp, ip)
5180 	struct inode *dp;
5181 	struct inode *ip;
5182 {
5183 	struct vnode *dvp;
5184 
5185 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5186 	    ("softdep_setup_unlink called on non-softdep filesystem"));
5187 	dvp = ITOV(dp);
5188 	ACQUIRE_LOCK(ITOUMP(dp));
5189 	(void) inodedep_lookup_ip(ip);
5190 	(void) inodedep_lookup_ip(dp);
5191 	FREE_LOCK(ITOUMP(dp));
5192 }
5193 
5194 /*
5195  * Called to release the journal structures created by a failed non-directory
5196  * creation.  Adjusts nlinkdelta for non-journaling softdep.
5197  */
5198 void
5199 softdep_revert_create(dp, ip)
5200 	struct inode *dp;
5201 	struct inode *ip;
5202 {
5203 	struct inodedep *inodedep;
5204 	struct jaddref *jaddref;
5205 	struct vnode *dvp;
5206 
5207 	KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
5208 	    ("softdep_revert_create called on non-softdep filesystem"));
5209 	dvp = ITOV(dp);
5210 	ACQUIRE_LOCK(ITOUMP(dp));
5211 	inodedep = inodedep_lookup_ip(ip);
5212 	if (DOINGSUJ(dvp)) {
5213 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5214 		    inoreflst);
5215 		KASSERT(jaddref->ja_parent == dp->i_number,
5216 		    ("softdep_revert_create: addref parent mismatch"));
5217 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5218 	}
5219 	FREE_LOCK(ITOUMP(dp));
5220 }
5221 
5222 /*
5223  * Called to release the journal structures created by a failed link
5224  * addition.  Adjusts nlinkdelta for non-journaling softdep.
5225  */
5226 void
5227 softdep_revert_link(dp, ip)
5228 	struct inode *dp;
5229 	struct inode *ip;
5230 {
5231 	struct inodedep *inodedep;
5232 	struct jaddref *jaddref;
5233 	struct vnode *dvp;
5234 
5235 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5236 	    ("softdep_revert_link called on non-softdep filesystem"));
5237 	dvp = ITOV(dp);
5238 	ACQUIRE_LOCK(ITOUMP(dp));
5239 	inodedep = inodedep_lookup_ip(ip);
5240 	if (DOINGSUJ(dvp)) {
5241 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5242 		    inoreflst);
5243 		KASSERT(jaddref->ja_parent == dp->i_number,
5244 		    ("softdep_revert_link: addref parent mismatch"));
5245 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5246 	}
5247 	FREE_LOCK(ITOUMP(dp));
5248 }
5249 
5250 /*
5251  * Called to release the journal structures created by a failed mkdir
5252  * attempt.  Adjusts nlinkdelta for non-journaling softdep.
5253  */
5254 void
5255 softdep_revert_mkdir(dp, ip)
5256 	struct inode *dp;
5257 	struct inode *ip;
5258 {
5259 	struct inodedep *inodedep;
5260 	struct jaddref *jaddref;
5261 	struct jaddref *dotaddref;
5262 	struct vnode *dvp;
5263 
5264 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5265 	    ("softdep_revert_mkdir called on non-softdep filesystem"));
5266 	dvp = ITOV(dp);
5267 
5268 	ACQUIRE_LOCK(ITOUMP(dp));
5269 	inodedep = inodedep_lookup_ip(dp);
5270 	if (DOINGSUJ(dvp)) {
5271 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5272 		    inoreflst);
5273 		KASSERT(jaddref->ja_parent == ip->i_number,
5274 		    ("softdep_revert_mkdir: dotdot addref parent mismatch"));
5275 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5276 	}
5277 	inodedep = inodedep_lookup_ip(ip);
5278 	if (DOINGSUJ(dvp)) {
5279 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5280 		    inoreflst);
5281 		KASSERT(jaddref->ja_parent == dp->i_number,
5282 		    ("softdep_revert_mkdir: addref parent mismatch"));
5283 		dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
5284 		    inoreflst, if_deps);
5285 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5286 		KASSERT(dotaddref->ja_parent == ip->i_number,
5287 		    ("softdep_revert_mkdir: dot addref parent mismatch"));
5288 		cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
5289 	}
5290 	FREE_LOCK(ITOUMP(dp));
5291 }
5292 
5293 /*
5294  * Called to correct nlinkdelta after a failed rmdir.
5295  */
5296 void
5297 softdep_revert_rmdir(dp, ip)
5298 	struct inode *dp;
5299 	struct inode *ip;
5300 {
5301 
5302 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5303 	    ("softdep_revert_rmdir called on non-softdep filesystem"));
5304 	ACQUIRE_LOCK(ITOUMP(dp));
5305 	(void) inodedep_lookup_ip(ip);
5306 	(void) inodedep_lookup_ip(dp);
5307 	FREE_LOCK(ITOUMP(dp));
5308 }
5309 
5310 /*
5311  * Protecting the freemaps (or bitmaps).
5312  *
5313  * To eliminate the need to execute fsck before mounting a filesystem
5314  * after a power failure, one must (conservatively) guarantee that the
5315  * on-disk copy of the bitmaps never indicate that a live inode or block is
5316  * free.  So, when a block or inode is allocated, the bitmap should be
5317  * updated (on disk) before any new pointers.  When a block or inode is
5318  * freed, the bitmap should not be updated until all pointers have been
5319  * reset.  The latter dependency is handled by the delayed de-allocation
5320  * approach described below for block and inode de-allocation.  The former
5321  * dependency is handled by calling the following procedure when a block or
5322  * inode is allocated. When an inode is allocated an "inodedep" is created
5323  * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5324  * Each "inodedep" is also inserted into the hash indexing structure so
5325  * that any additional link additions can be made dependent on the inode
5326  * allocation.
5327  *
5328  * The ufs filesystem maintains a number of free block counts (e.g., per
5329  * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5330  * in addition to the bitmaps.  These counts are used to improve efficiency
5331  * during allocation and therefore must be consistent with the bitmaps.
5332  * There is no convenient way to guarantee post-crash consistency of these
5333  * counts with simple update ordering, for two main reasons: (1) The counts
5334  * and bitmaps for a single cylinder group block are not in the same disk
5335  * sector.  If a disk write is interrupted (e.g., by power failure), one may
5336  * be written and the other not.  (2) Some of the counts are located in the
5337  * superblock rather than the cylinder group block. So, we focus our soft
5338  * updates implementation on protecting the bitmaps. When mounting a
5339  * filesystem, we recompute the auxiliary counts from the bitmaps.
5340  */
5341 
5342 /*
5343  * Called just after updating the cylinder group block to allocate an inode.
5344  */
5345 void
5346 softdep_setup_inomapdep(bp, ip, newinum, mode)
5347 	struct buf *bp;		/* buffer for cylgroup block with inode map */
5348 	struct inode *ip;	/* inode related to allocation */
5349 	ino_t newinum;		/* new inode number being allocated */
5350 	int mode;
5351 {
5352 	struct inodedep *inodedep;
5353 	struct bmsafemap *bmsafemap;
5354 	struct jaddref *jaddref;
5355 	struct mount *mp;
5356 	struct fs *fs;
5357 
5358 	mp = ITOVFS(ip);
5359 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5360 	    ("softdep_setup_inomapdep called on non-softdep filesystem"));
5361 	fs = VFSTOUFS(mp)->um_fs;
5362 	jaddref = NULL;
5363 
5364 	/*
5365 	 * Allocate the journal reference add structure so that the bitmap
5366 	 * can be dependent on it.
5367 	 */
5368 	if (MOUNTEDSUJ(mp)) {
5369 		jaddref = newjaddref(ip, newinum, 0, 0, mode);
5370 		jaddref->ja_state |= NEWBLOCK;
5371 	}
5372 
5373 	/*
5374 	 * Create a dependency for the newly allocated inode.
5375 	 * Panic if it already exists as something is seriously wrong.
5376 	 * Otherwise add it to the dependency list for the buffer holding
5377 	 * the cylinder group map from which it was allocated.
5378 	 *
5379 	 * We have to preallocate a bmsafemap entry in case it is needed
5380 	 * in bmsafemap_lookup since once we allocate the inodedep, we
5381 	 * have to finish initializing it before we can FREE_LOCK().
5382 	 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5383 	 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5384 	 * creating the inodedep as it can be freed during the time
5385 	 * that we FREE_LOCK() while allocating the inodedep. We must
5386 	 * call workitem_alloc() before entering the locked section as
5387 	 * it also acquires the lock and we must avoid trying doing so
5388 	 * recursively.
5389 	 */
5390 	bmsafemap = malloc(sizeof(struct bmsafemap),
5391 	    M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5392 	workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5393 	ACQUIRE_LOCK(ITOUMP(ip));
5394 	if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5395 		panic("softdep_setup_inomapdep: dependency %p for new"
5396 		    "inode already exists", inodedep);
5397 	bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5398 	if (jaddref) {
5399 		LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5400 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5401 		    if_deps);
5402 	} else {
5403 		inodedep->id_state |= ONDEPLIST;
5404 		LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5405 	}
5406 	inodedep->id_bmsafemap = bmsafemap;
5407 	inodedep->id_state &= ~DEPCOMPLETE;
5408 	FREE_LOCK(ITOUMP(ip));
5409 }
5410 
5411 /*
5412  * Called just after updating the cylinder group block to
5413  * allocate block or fragment.
5414  */
5415 void
5416 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5417 	struct buf *bp;		/* buffer for cylgroup block with block map */
5418 	struct mount *mp;	/* filesystem doing allocation */
5419 	ufs2_daddr_t newblkno;	/* number of newly allocated block */
5420 	int frags;		/* Number of fragments. */
5421 	int oldfrags;		/* Previous number of fragments for extend. */
5422 {
5423 	struct newblk *newblk;
5424 	struct bmsafemap *bmsafemap;
5425 	struct jnewblk *jnewblk;
5426 	struct ufsmount *ump;
5427 	struct fs *fs;
5428 
5429 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5430 	    ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5431 	ump = VFSTOUFS(mp);
5432 	fs = ump->um_fs;
5433 	jnewblk = NULL;
5434 	/*
5435 	 * Create a dependency for the newly allocated block.
5436 	 * Add it to the dependency list for the buffer holding
5437 	 * the cylinder group map from which it was allocated.
5438 	 */
5439 	if (MOUNTEDSUJ(mp)) {
5440 		jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5441 		workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5442 		jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5443 		jnewblk->jn_state = ATTACHED;
5444 		jnewblk->jn_blkno = newblkno;
5445 		jnewblk->jn_frags = frags;
5446 		jnewblk->jn_oldfrags = oldfrags;
5447 #ifdef INVARIANTS
5448 		{
5449 			struct cg *cgp;
5450 			uint8_t *blksfree;
5451 			long bno;
5452 			int i;
5453 
5454 			cgp = (struct cg *)bp->b_data;
5455 			blksfree = cg_blksfree(cgp);
5456 			bno = dtogd(fs, jnewblk->jn_blkno);
5457 			for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5458 			    i++) {
5459 				if (isset(blksfree, bno + i))
5460 					panic("softdep_setup_blkmapdep: "
5461 					    "free fragment %d from %d-%d "
5462 					    "state 0x%X dep %p", i,
5463 					    jnewblk->jn_oldfrags,
5464 					    jnewblk->jn_frags,
5465 					    jnewblk->jn_state,
5466 					    jnewblk->jn_dep);
5467 			}
5468 		}
5469 #endif
5470 	}
5471 
5472 	CTR3(KTR_SUJ,
5473 	    "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5474 	    newblkno, frags, oldfrags);
5475 	ACQUIRE_LOCK(ump);
5476 	if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5477 		panic("softdep_setup_blkmapdep: found block");
5478 	newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5479 	    dtog(fs, newblkno), NULL);
5480 	if (jnewblk) {
5481 		jnewblk->jn_dep = (struct worklist *)newblk;
5482 		LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5483 	} else {
5484 		newblk->nb_state |= ONDEPLIST;
5485 		LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5486 	}
5487 	newblk->nb_bmsafemap = bmsafemap;
5488 	newblk->nb_jnewblk = jnewblk;
5489 	FREE_LOCK(ump);
5490 }
5491 
5492 #define	BMSAFEMAP_HASH(ump, cg) \
5493       (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5494 
5495 static int
5496 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5497 	struct bmsafemap_hashhead *bmsafemaphd;
5498 	int cg;
5499 	struct bmsafemap **bmsafemapp;
5500 {
5501 	struct bmsafemap *bmsafemap;
5502 
5503 	LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5504 		if (bmsafemap->sm_cg == cg)
5505 			break;
5506 	if (bmsafemap) {
5507 		*bmsafemapp = bmsafemap;
5508 		return (1);
5509 	}
5510 	*bmsafemapp = NULL;
5511 
5512 	return (0);
5513 }
5514 
5515 /*
5516  * Find the bmsafemap associated with a cylinder group buffer.
5517  * If none exists, create one. The buffer must be locked when
5518  * this routine is called and this routine must be called with
5519  * the softdep lock held. To avoid giving up the lock while
5520  * allocating a new bmsafemap, a preallocated bmsafemap may be
5521  * provided. If it is provided but not needed, it is freed.
5522  */
5523 static struct bmsafemap *
5524 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5525 	struct mount *mp;
5526 	struct buf *bp;
5527 	int cg;
5528 	struct bmsafemap *newbmsafemap;
5529 {
5530 	struct bmsafemap_hashhead *bmsafemaphd;
5531 	struct bmsafemap *bmsafemap, *collision;
5532 	struct worklist *wk;
5533 	struct ufsmount *ump;
5534 
5535 	ump = VFSTOUFS(mp);
5536 	LOCK_OWNED(ump);
5537 	KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5538 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5539 		if (wk->wk_type == D_BMSAFEMAP) {
5540 			if (newbmsafemap)
5541 				WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5542 			return (WK_BMSAFEMAP(wk));
5543 		}
5544 	}
5545 	bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5546 	if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5547 		if (newbmsafemap)
5548 			WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5549 		return (bmsafemap);
5550 	}
5551 	if (newbmsafemap) {
5552 		bmsafemap = newbmsafemap;
5553 	} else {
5554 		FREE_LOCK(ump);
5555 		bmsafemap = malloc(sizeof(struct bmsafemap),
5556 			M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5557 		workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5558 		ACQUIRE_LOCK(ump);
5559 	}
5560 	bmsafemap->sm_buf = bp;
5561 	LIST_INIT(&bmsafemap->sm_inodedephd);
5562 	LIST_INIT(&bmsafemap->sm_inodedepwr);
5563 	LIST_INIT(&bmsafemap->sm_newblkhd);
5564 	LIST_INIT(&bmsafemap->sm_newblkwr);
5565 	LIST_INIT(&bmsafemap->sm_jaddrefhd);
5566 	LIST_INIT(&bmsafemap->sm_jnewblkhd);
5567 	LIST_INIT(&bmsafemap->sm_freehd);
5568 	LIST_INIT(&bmsafemap->sm_freewr);
5569 	if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5570 		WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5571 		return (collision);
5572 	}
5573 	bmsafemap->sm_cg = cg;
5574 	LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5575 	LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5576 	WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5577 	return (bmsafemap);
5578 }
5579 
5580 /*
5581  * Direct block allocation dependencies.
5582  *
5583  * When a new block is allocated, the corresponding disk locations must be
5584  * initialized (with zeros or new data) before the on-disk inode points to
5585  * them.  Also, the freemap from which the block was allocated must be
5586  * updated (on disk) before the inode's pointer. These two dependencies are
5587  * independent of each other and are needed for all file blocks and indirect
5588  * blocks that are pointed to directly by the inode.  Just before the
5589  * "in-core" version of the inode is updated with a newly allocated block
5590  * number, a procedure (below) is called to setup allocation dependency
5591  * structures.  These structures are removed when the corresponding
5592  * dependencies are satisfied or when the block allocation becomes obsolete
5593  * (i.e., the file is deleted, the block is de-allocated, or the block is a
5594  * fragment that gets upgraded).  All of these cases are handled in
5595  * procedures described later.
5596  *
5597  * When a file extension causes a fragment to be upgraded, either to a larger
5598  * fragment or to a full block, the on-disk location may change (if the
5599  * previous fragment could not simply be extended). In this case, the old
5600  * fragment must be de-allocated, but not until after the inode's pointer has
5601  * been updated. In most cases, this is handled by later procedures, which
5602  * will construct a "freefrag" structure to be added to the workitem queue
5603  * when the inode update is complete (or obsolete).  The main exception to
5604  * this is when an allocation occurs while a pending allocation dependency
5605  * (for the same block pointer) remains.  This case is handled in the main
5606  * allocation dependency setup procedure by immediately freeing the
5607  * unreferenced fragments.
5608  */
5609 void
5610 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5611 	struct inode *ip;	/* inode to which block is being added */
5612 	ufs_lbn_t off;		/* block pointer within inode */
5613 	ufs2_daddr_t newblkno;	/* disk block number being added */
5614 	ufs2_daddr_t oldblkno;	/* previous block number, 0 unless frag */
5615 	long newsize;		/* size of new block */
5616 	long oldsize;		/* size of new block */
5617 	struct buf *bp;		/* bp for allocated block */
5618 {
5619 	struct allocdirect *adp, *oldadp;
5620 	struct allocdirectlst *adphead;
5621 	struct freefrag *freefrag;
5622 	struct inodedep *inodedep;
5623 	struct pagedep *pagedep;
5624 	struct jnewblk *jnewblk;
5625 	struct newblk *newblk;
5626 	struct mount *mp;
5627 	ufs_lbn_t lbn;
5628 
5629 	lbn = bp->b_lblkno;
5630 	mp = ITOVFS(ip);
5631 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5632 	    ("softdep_setup_allocdirect called on non-softdep filesystem"));
5633 	if (oldblkno && oldblkno != newblkno)
5634 		/*
5635 		 * The usual case is that a smaller fragment that
5636 		 * was just allocated has been replaced with a bigger
5637 		 * fragment or a full-size block. If it is marked as
5638 		 * B_DELWRI, the current contents have not been written
5639 		 * to disk. It is possible that the block was written
5640 		 * earlier, but very uncommon. If the block has never
5641 		 * been written, there is no need to send a BIO_DELETE
5642 		 * for it when it is freed. The gain from avoiding the
5643 		 * TRIMs for the common case of unwritten blocks far
5644 		 * exceeds the cost of the write amplification for the
5645 		 * uncommon case of failing to send a TRIM for a block
5646 		 * that had been written.
5647 		 */
5648 		freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5649 		    (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5650 	else
5651 		freefrag = NULL;
5652 
5653 	CTR6(KTR_SUJ,
5654 	    "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5655 	    "off %jd newsize %ld oldsize %d",
5656 	    ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5657 	ACQUIRE_LOCK(ITOUMP(ip));
5658 	if (off >= UFS_NDADDR) {
5659 		if (lbn > 0)
5660 			panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5661 			    lbn, off);
5662 		/* allocating an indirect block */
5663 		if (oldblkno != 0)
5664 			panic("softdep_setup_allocdirect: non-zero indir");
5665 	} else {
5666 		if (off != lbn)
5667 			panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5668 			    lbn, off);
5669 		/*
5670 		 * Allocating a direct block.
5671 		 *
5672 		 * If we are allocating a directory block, then we must
5673 		 * allocate an associated pagedep to track additions and
5674 		 * deletions.
5675 		 */
5676 		if ((ip->i_mode & IFMT) == IFDIR)
5677 			pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5678 			    &pagedep);
5679 	}
5680 	if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5681 		panic("softdep_setup_allocdirect: lost block");
5682 	KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5683 	    ("softdep_setup_allocdirect: newblk already initialized"));
5684 	/*
5685 	 * Convert the newblk to an allocdirect.
5686 	 */
5687 	WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5688 	adp = (struct allocdirect *)newblk;
5689 	newblk->nb_freefrag = freefrag;
5690 	adp->ad_offset = off;
5691 	adp->ad_oldblkno = oldblkno;
5692 	adp->ad_newsize = newsize;
5693 	adp->ad_oldsize = oldsize;
5694 
5695 	/*
5696 	 * Finish initializing the journal.
5697 	 */
5698 	if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5699 		jnewblk->jn_ino = ip->i_number;
5700 		jnewblk->jn_lbn = lbn;
5701 		add_to_journal(&jnewblk->jn_list);
5702 	}
5703 	if (freefrag && freefrag->ff_jdep != NULL &&
5704 	    freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5705 		add_to_journal(freefrag->ff_jdep);
5706 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5707 	adp->ad_inodedep = inodedep;
5708 
5709 	WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5710 	/*
5711 	 * The list of allocdirects must be kept in sorted and ascending
5712 	 * order so that the rollback routines can quickly determine the
5713 	 * first uncommitted block (the size of the file stored on disk
5714 	 * ends at the end of the lowest committed fragment, or if there
5715 	 * are no fragments, at the end of the highest committed block).
5716 	 * Since files generally grow, the typical case is that the new
5717 	 * block is to be added at the end of the list. We speed this
5718 	 * special case by checking against the last allocdirect in the
5719 	 * list before laboriously traversing the list looking for the
5720 	 * insertion point.
5721 	 */
5722 	adphead = &inodedep->id_newinoupdt;
5723 	oldadp = TAILQ_LAST(adphead, allocdirectlst);
5724 	if (oldadp == NULL || oldadp->ad_offset <= off) {
5725 		/* insert at end of list */
5726 		TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5727 		if (oldadp != NULL && oldadp->ad_offset == off)
5728 			allocdirect_merge(adphead, adp, oldadp);
5729 		FREE_LOCK(ITOUMP(ip));
5730 		return;
5731 	}
5732 	TAILQ_FOREACH(oldadp, adphead, ad_next) {
5733 		if (oldadp->ad_offset >= off)
5734 			break;
5735 	}
5736 	if (oldadp == NULL)
5737 		panic("softdep_setup_allocdirect: lost entry");
5738 	/* insert in middle of list */
5739 	TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5740 	if (oldadp->ad_offset == off)
5741 		allocdirect_merge(adphead, adp, oldadp);
5742 
5743 	FREE_LOCK(ITOUMP(ip));
5744 }
5745 
5746 /*
5747  * Merge a newer and older journal record to be stored either in a
5748  * newblock or freefrag.  This handles aggregating journal records for
5749  * fragment allocation into a second record as well as replacing a
5750  * journal free with an aborted journal allocation.  A segment for the
5751  * oldest record will be placed on wkhd if it has been written.  If not
5752  * the segment for the newer record will suffice.
5753  */
5754 static struct worklist *
5755 jnewblk_merge(new, old, wkhd)
5756 	struct worklist *new;
5757 	struct worklist *old;
5758 	struct workhead *wkhd;
5759 {
5760 	struct jnewblk *njnewblk;
5761 	struct jnewblk *jnewblk;
5762 
5763 	/* Handle NULLs to simplify callers. */
5764 	if (new == NULL)
5765 		return (old);
5766 	if (old == NULL)
5767 		return (new);
5768 	/* Replace a jfreefrag with a jnewblk. */
5769 	if (new->wk_type == D_JFREEFRAG) {
5770 		if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5771 			panic("jnewblk_merge: blkno mismatch: %p, %p",
5772 			    old, new);
5773 		cancel_jfreefrag(WK_JFREEFRAG(new));
5774 		return (old);
5775 	}
5776 	if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5777 		panic("jnewblk_merge: Bad type: old %d new %d\n",
5778 		    old->wk_type, new->wk_type);
5779 	/*
5780 	 * Handle merging of two jnewblk records that describe
5781 	 * different sets of fragments in the same block.
5782 	 */
5783 	jnewblk = WK_JNEWBLK(old);
5784 	njnewblk = WK_JNEWBLK(new);
5785 	if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5786 		panic("jnewblk_merge: Merging disparate blocks.");
5787 	/*
5788 	 * The record may be rolled back in the cg.
5789 	 */
5790 	if (jnewblk->jn_state & UNDONE) {
5791 		jnewblk->jn_state &= ~UNDONE;
5792 		njnewblk->jn_state |= UNDONE;
5793 		njnewblk->jn_state &= ~ATTACHED;
5794 	}
5795 	/*
5796 	 * We modify the newer addref and free the older so that if neither
5797 	 * has been written the most up-to-date copy will be on disk.  If
5798 	 * both have been written but rolled back we only temporarily need
5799 	 * one of them to fix the bits when the cg write completes.
5800 	 */
5801 	jnewblk->jn_state |= ATTACHED | COMPLETE;
5802 	njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5803 	cancel_jnewblk(jnewblk, wkhd);
5804 	WORKLIST_REMOVE(&jnewblk->jn_list);
5805 	free_jnewblk(jnewblk);
5806 	return (new);
5807 }
5808 
5809 /*
5810  * Replace an old allocdirect dependency with a newer one.
5811  */
5812 static void
5813 allocdirect_merge(adphead, newadp, oldadp)
5814 	struct allocdirectlst *adphead;	/* head of list holding allocdirects */
5815 	struct allocdirect *newadp;	/* allocdirect being added */
5816 	struct allocdirect *oldadp;	/* existing allocdirect being checked */
5817 {
5818 	struct worklist *wk;
5819 	struct freefrag *freefrag;
5820 
5821 	freefrag = NULL;
5822 	LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5823 	if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5824 	    newadp->ad_oldsize != oldadp->ad_newsize ||
5825 	    newadp->ad_offset >= UFS_NDADDR)
5826 		panic("%s %jd != new %jd || old size %ld != new %ld",
5827 		    "allocdirect_merge: old blkno",
5828 		    (intmax_t)newadp->ad_oldblkno,
5829 		    (intmax_t)oldadp->ad_newblkno,
5830 		    newadp->ad_oldsize, oldadp->ad_newsize);
5831 	newadp->ad_oldblkno = oldadp->ad_oldblkno;
5832 	newadp->ad_oldsize = oldadp->ad_oldsize;
5833 	/*
5834 	 * If the old dependency had a fragment to free or had never
5835 	 * previously had a block allocated, then the new dependency
5836 	 * can immediately post its freefrag and adopt the old freefrag.
5837 	 * This action is done by swapping the freefrag dependencies.
5838 	 * The new dependency gains the old one's freefrag, and the
5839 	 * old one gets the new one and then immediately puts it on
5840 	 * the worklist when it is freed by free_newblk. It is
5841 	 * not possible to do this swap when the old dependency had a
5842 	 * non-zero size but no previous fragment to free. This condition
5843 	 * arises when the new block is an extension of the old block.
5844 	 * Here, the first part of the fragment allocated to the new
5845 	 * dependency is part of the block currently claimed on disk by
5846 	 * the old dependency, so cannot legitimately be freed until the
5847 	 * conditions for the new dependency are fulfilled.
5848 	 */
5849 	freefrag = newadp->ad_freefrag;
5850 	if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5851 		newadp->ad_freefrag = oldadp->ad_freefrag;
5852 		oldadp->ad_freefrag = freefrag;
5853 	}
5854 	/*
5855 	 * If we are tracking a new directory-block allocation,
5856 	 * move it from the old allocdirect to the new allocdirect.
5857 	 */
5858 	if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5859 		WORKLIST_REMOVE(wk);
5860 		if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5861 			panic("allocdirect_merge: extra newdirblk");
5862 		WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5863 	}
5864 	TAILQ_REMOVE(adphead, oldadp, ad_next);
5865 	/*
5866 	 * We need to move any journal dependencies over to the freefrag
5867 	 * that releases this block if it exists.  Otherwise we are
5868 	 * extending an existing block and we'll wait until that is
5869 	 * complete to release the journal space and extend the
5870 	 * new journal to cover this old space as well.
5871 	 */
5872 	if (freefrag == NULL) {
5873 		if (oldadp->ad_newblkno != newadp->ad_newblkno)
5874 			panic("allocdirect_merge: %jd != %jd",
5875 			    oldadp->ad_newblkno, newadp->ad_newblkno);
5876 		newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5877 		    jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5878 		    &oldadp->ad_block.nb_jnewblk->jn_list,
5879 		    &newadp->ad_block.nb_jwork);
5880 		oldadp->ad_block.nb_jnewblk = NULL;
5881 		cancel_newblk(&oldadp->ad_block, NULL,
5882 		    &newadp->ad_block.nb_jwork);
5883 	} else {
5884 		wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5885 		    &freefrag->ff_list, &freefrag->ff_jwork);
5886 		freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5887 		    &freefrag->ff_jwork);
5888 	}
5889 	free_newblk(&oldadp->ad_block);
5890 }
5891 
5892 /*
5893  * Allocate a jfreefrag structure to journal a single block free.
5894  */
5895 static struct jfreefrag *
5896 newjfreefrag(freefrag, ip, blkno, size, lbn)
5897 	struct freefrag *freefrag;
5898 	struct inode *ip;
5899 	ufs2_daddr_t blkno;
5900 	long size;
5901 	ufs_lbn_t lbn;
5902 {
5903 	struct jfreefrag *jfreefrag;
5904 	struct fs *fs;
5905 
5906 	fs = ITOFS(ip);
5907 	jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5908 	    M_SOFTDEP_FLAGS);
5909 	workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5910 	jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5911 	jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5912 	jfreefrag->fr_ino = ip->i_number;
5913 	jfreefrag->fr_lbn = lbn;
5914 	jfreefrag->fr_blkno = blkno;
5915 	jfreefrag->fr_frags = numfrags(fs, size);
5916 	jfreefrag->fr_freefrag = freefrag;
5917 
5918 	return (jfreefrag);
5919 }
5920 
5921 /*
5922  * Allocate a new freefrag structure.
5923  */
5924 static struct freefrag *
5925 newfreefrag(ip, blkno, size, lbn, key)
5926 	struct inode *ip;
5927 	ufs2_daddr_t blkno;
5928 	long size;
5929 	ufs_lbn_t lbn;
5930 	u_long key;
5931 {
5932 	struct freefrag *freefrag;
5933 	struct ufsmount *ump;
5934 	struct fs *fs;
5935 
5936 	CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5937 	    ip->i_number, blkno, size, lbn);
5938 	ump = ITOUMP(ip);
5939 	fs = ump->um_fs;
5940 	if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5941 		panic("newfreefrag: frag size");
5942 	freefrag = malloc(sizeof(struct freefrag),
5943 	    M_FREEFRAG, M_SOFTDEP_FLAGS);
5944 	workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5945 	freefrag->ff_state = ATTACHED;
5946 	LIST_INIT(&freefrag->ff_jwork);
5947 	freefrag->ff_inum = ip->i_number;
5948 	freefrag->ff_vtype = ITOV(ip)->v_type;
5949 	freefrag->ff_blkno = blkno;
5950 	freefrag->ff_fragsize = size;
5951 	freefrag->ff_key = key;
5952 
5953 	if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5954 		freefrag->ff_jdep = (struct worklist *)
5955 		    newjfreefrag(freefrag, ip, blkno, size, lbn);
5956 	} else {
5957 		freefrag->ff_state |= DEPCOMPLETE;
5958 		freefrag->ff_jdep = NULL;
5959 	}
5960 
5961 	return (freefrag);
5962 }
5963 
5964 /*
5965  * This workitem de-allocates fragments that were replaced during
5966  * file block allocation.
5967  */
5968 static void
5969 handle_workitem_freefrag(freefrag)
5970 	struct freefrag *freefrag;
5971 {
5972 	struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5973 	struct workhead wkhd;
5974 
5975 	CTR3(KTR_SUJ,
5976 	    "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5977 	    freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5978 	/*
5979 	 * It would be illegal to add new completion items to the
5980 	 * freefrag after it was schedule to be done so it must be
5981 	 * safe to modify the list head here.
5982 	 */
5983 	LIST_INIT(&wkhd);
5984 	ACQUIRE_LOCK(ump);
5985 	LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5986 	/*
5987 	 * If the journal has not been written we must cancel it here.
5988 	 */
5989 	if (freefrag->ff_jdep) {
5990 		if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5991 			panic("handle_workitem_freefrag: Unexpected type %d\n",
5992 			    freefrag->ff_jdep->wk_type);
5993 		cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5994 	}
5995 	FREE_LOCK(ump);
5996 	ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5997 	   freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
5998 	   &wkhd, freefrag->ff_key);
5999 	ACQUIRE_LOCK(ump);
6000 	WORKITEM_FREE(freefrag, D_FREEFRAG);
6001 	FREE_LOCK(ump);
6002 }
6003 
6004 /*
6005  * Set up a dependency structure for an external attributes data block.
6006  * This routine follows much of the structure of softdep_setup_allocdirect.
6007  * See the description of softdep_setup_allocdirect above for details.
6008  */
6009 void
6010 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
6011 	struct inode *ip;
6012 	ufs_lbn_t off;
6013 	ufs2_daddr_t newblkno;
6014 	ufs2_daddr_t oldblkno;
6015 	long newsize;
6016 	long oldsize;
6017 	struct buf *bp;
6018 {
6019 	struct allocdirect *adp, *oldadp;
6020 	struct allocdirectlst *adphead;
6021 	struct freefrag *freefrag;
6022 	struct inodedep *inodedep;
6023 	struct jnewblk *jnewblk;
6024 	struct newblk *newblk;
6025 	struct mount *mp;
6026 	struct ufsmount *ump;
6027 	ufs_lbn_t lbn;
6028 
6029 	mp = ITOVFS(ip);
6030 	ump = VFSTOUFS(mp);
6031 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6032 	    ("softdep_setup_allocext called on non-softdep filesystem"));
6033 	KASSERT(off < UFS_NXADDR,
6034 	    ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
6035 
6036 	lbn = bp->b_lblkno;
6037 	if (oldblkno && oldblkno != newblkno)
6038 		/*
6039 		 * The usual case is that a smaller fragment that
6040 		 * was just allocated has been replaced with a bigger
6041 		 * fragment or a full-size block. If it is marked as
6042 		 * B_DELWRI, the current contents have not been written
6043 		 * to disk. It is possible that the block was written
6044 		 * earlier, but very uncommon. If the block has never
6045 		 * been written, there is no need to send a BIO_DELETE
6046 		 * for it when it is freed. The gain from avoiding the
6047 		 * TRIMs for the common case of unwritten blocks far
6048 		 * exceeds the cost of the write amplification for the
6049 		 * uncommon case of failing to send a TRIM for a block
6050 		 * that had been written.
6051 		 */
6052 		freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
6053 		    (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
6054 	else
6055 		freefrag = NULL;
6056 
6057 	ACQUIRE_LOCK(ump);
6058 	if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
6059 		panic("softdep_setup_allocext: lost block");
6060 	KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6061 	    ("softdep_setup_allocext: newblk already initialized"));
6062 	/*
6063 	 * Convert the newblk to an allocdirect.
6064 	 */
6065 	WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
6066 	adp = (struct allocdirect *)newblk;
6067 	newblk->nb_freefrag = freefrag;
6068 	adp->ad_offset = off;
6069 	adp->ad_oldblkno = oldblkno;
6070 	adp->ad_newsize = newsize;
6071 	adp->ad_oldsize = oldsize;
6072 	adp->ad_state |=  EXTDATA;
6073 
6074 	/*
6075 	 * Finish initializing the journal.
6076 	 */
6077 	if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6078 		jnewblk->jn_ino = ip->i_number;
6079 		jnewblk->jn_lbn = lbn;
6080 		add_to_journal(&jnewblk->jn_list);
6081 	}
6082 	if (freefrag && freefrag->ff_jdep != NULL &&
6083 	    freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6084 		add_to_journal(freefrag->ff_jdep);
6085 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6086 	adp->ad_inodedep = inodedep;
6087 
6088 	WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
6089 	/*
6090 	 * The list of allocdirects must be kept in sorted and ascending
6091 	 * order so that the rollback routines can quickly determine the
6092 	 * first uncommitted block (the size of the file stored on disk
6093 	 * ends at the end of the lowest committed fragment, or if there
6094 	 * are no fragments, at the end of the highest committed block).
6095 	 * Since files generally grow, the typical case is that the new
6096 	 * block is to be added at the end of the list. We speed this
6097 	 * special case by checking against the last allocdirect in the
6098 	 * list before laboriously traversing the list looking for the
6099 	 * insertion point.
6100 	 */
6101 	adphead = &inodedep->id_newextupdt;
6102 	oldadp = TAILQ_LAST(adphead, allocdirectlst);
6103 	if (oldadp == NULL || oldadp->ad_offset <= off) {
6104 		/* insert at end of list */
6105 		TAILQ_INSERT_TAIL(adphead, adp, ad_next);
6106 		if (oldadp != NULL && oldadp->ad_offset == off)
6107 			allocdirect_merge(adphead, adp, oldadp);
6108 		FREE_LOCK(ump);
6109 		return;
6110 	}
6111 	TAILQ_FOREACH(oldadp, adphead, ad_next) {
6112 		if (oldadp->ad_offset >= off)
6113 			break;
6114 	}
6115 	if (oldadp == NULL)
6116 		panic("softdep_setup_allocext: lost entry");
6117 	/* insert in middle of list */
6118 	TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
6119 	if (oldadp->ad_offset == off)
6120 		allocdirect_merge(adphead, adp, oldadp);
6121 	FREE_LOCK(ump);
6122 }
6123 
6124 /*
6125  * Indirect block allocation dependencies.
6126  *
6127  * The same dependencies that exist for a direct block also exist when
6128  * a new block is allocated and pointed to by an entry in a block of
6129  * indirect pointers. The undo/redo states described above are also
6130  * used here. Because an indirect block contains many pointers that
6131  * may have dependencies, a second copy of the entire in-memory indirect
6132  * block is kept. The buffer cache copy is always completely up-to-date.
6133  * The second copy, which is used only as a source for disk writes,
6134  * contains only the safe pointers (i.e., those that have no remaining
6135  * update dependencies). The second copy is freed when all pointers
6136  * are safe. The cache is not allowed to replace indirect blocks with
6137  * pending update dependencies. If a buffer containing an indirect
6138  * block with dependencies is written, these routines will mark it
6139  * dirty again. It can only be successfully written once all the
6140  * dependencies are removed. The ffs_fsync routine in conjunction with
6141  * softdep_sync_metadata work together to get all the dependencies
6142  * removed so that a file can be successfully written to disk. Three
6143  * procedures are used when setting up indirect block pointer
6144  * dependencies. The division is necessary because of the organization
6145  * of the "balloc" routine and because of the distinction between file
6146  * pages and file metadata blocks.
6147  */
6148 
6149 /*
6150  * Allocate a new allocindir structure.
6151  */
6152 static struct allocindir *
6153 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
6154 	struct inode *ip;	/* inode for file being extended */
6155 	int ptrno;		/* offset of pointer in indirect block */
6156 	ufs2_daddr_t newblkno;	/* disk block number being added */
6157 	ufs2_daddr_t oldblkno;	/* previous block number, 0 if none */
6158 	ufs_lbn_t lbn;
6159 {
6160 	struct newblk *newblk;
6161 	struct allocindir *aip;
6162 	struct freefrag *freefrag;
6163 	struct jnewblk *jnewblk;
6164 
6165 	if (oldblkno)
6166 		freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
6167 		    SINGLETON_KEY);
6168 	else
6169 		freefrag = NULL;
6170 	ACQUIRE_LOCK(ITOUMP(ip));
6171 	if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
6172 		panic("new_allocindir: lost block");
6173 	KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6174 	    ("newallocindir: newblk already initialized"));
6175 	WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
6176 	newblk->nb_freefrag = freefrag;
6177 	aip = (struct allocindir *)newblk;
6178 	aip->ai_offset = ptrno;
6179 	aip->ai_oldblkno = oldblkno;
6180 	aip->ai_lbn = lbn;
6181 	if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6182 		jnewblk->jn_ino = ip->i_number;
6183 		jnewblk->jn_lbn = lbn;
6184 		add_to_journal(&jnewblk->jn_list);
6185 	}
6186 	if (freefrag && freefrag->ff_jdep != NULL &&
6187 	    freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6188 		add_to_journal(freefrag->ff_jdep);
6189 	return (aip);
6190 }
6191 
6192 /*
6193  * Called just before setting an indirect block pointer
6194  * to a newly allocated file page.
6195  */
6196 void
6197 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
6198 	struct inode *ip;	/* inode for file being extended */
6199 	ufs_lbn_t lbn;		/* allocated block number within file */
6200 	struct buf *bp;		/* buffer with indirect blk referencing page */
6201 	int ptrno;		/* offset of pointer in indirect block */
6202 	ufs2_daddr_t newblkno;	/* disk block number being added */
6203 	ufs2_daddr_t oldblkno;	/* previous block number, 0 if none */
6204 	struct buf *nbp;	/* buffer holding allocated page */
6205 {
6206 	struct inodedep *inodedep;
6207 	struct freefrag *freefrag;
6208 	struct allocindir *aip;
6209 	struct pagedep *pagedep;
6210 	struct mount *mp;
6211 	struct ufsmount *ump;
6212 
6213 	mp = ITOVFS(ip);
6214 	ump = VFSTOUFS(mp);
6215 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6216 	    ("softdep_setup_allocindir_page called on non-softdep filesystem"));
6217 	KASSERT(lbn == nbp->b_lblkno,
6218 	    ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
6219 	    lbn, bp->b_lblkno));
6220 	CTR4(KTR_SUJ,
6221 	    "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
6222 	    "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
6223 	ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
6224 	aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
6225 	(void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6226 	/*
6227 	 * If we are allocating a directory page, then we must
6228 	 * allocate an associated pagedep to track additions and
6229 	 * deletions.
6230 	 */
6231 	if ((ip->i_mode & IFMT) == IFDIR)
6232 		pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
6233 	WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6234 	freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
6235 	FREE_LOCK(ump);
6236 	if (freefrag)
6237 		handle_workitem_freefrag(freefrag);
6238 }
6239 
6240 /*
6241  * Called just before setting an indirect block pointer to a
6242  * newly allocated indirect block.
6243  */
6244 void
6245 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
6246 	struct buf *nbp;	/* newly allocated indirect block */
6247 	struct inode *ip;	/* inode for file being extended */
6248 	struct buf *bp;		/* indirect block referencing allocated block */
6249 	int ptrno;		/* offset of pointer in indirect block */
6250 	ufs2_daddr_t newblkno;	/* disk block number being added */
6251 {
6252 	struct inodedep *inodedep;
6253 	struct allocindir *aip;
6254 	struct ufsmount *ump;
6255 	ufs_lbn_t lbn;
6256 
6257 	ump = ITOUMP(ip);
6258 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6259 	    ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
6260 	CTR3(KTR_SUJ,
6261 	    "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
6262 	    ip->i_number, newblkno, ptrno);
6263 	lbn = nbp->b_lblkno;
6264 	ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
6265 	aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
6266 	inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
6267 	WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6268 	if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
6269 		panic("softdep_setup_allocindir_meta: Block already existed");
6270 	FREE_LOCK(ump);
6271 }
6272 
6273 static void
6274 indirdep_complete(indirdep)
6275 	struct indirdep *indirdep;
6276 {
6277 	struct allocindir *aip;
6278 
6279 	LIST_REMOVE(indirdep, ir_next);
6280 	indirdep->ir_state |= DEPCOMPLETE;
6281 
6282 	while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
6283 		LIST_REMOVE(aip, ai_next);
6284 		free_newblk(&aip->ai_block);
6285 	}
6286 	/*
6287 	 * If this indirdep is not attached to a buf it was simply waiting
6288 	 * on completion to clear completehd.  free_indirdep() asserts
6289 	 * that nothing is dangling.
6290 	 */
6291 	if ((indirdep->ir_state & ONWORKLIST) == 0)
6292 		free_indirdep(indirdep);
6293 }
6294 
6295 static struct indirdep *
6296 indirdep_lookup(mp, ip, bp)
6297 	struct mount *mp;
6298 	struct inode *ip;
6299 	struct buf *bp;
6300 {
6301 	struct indirdep *indirdep, *newindirdep;
6302 	struct newblk *newblk;
6303 	struct ufsmount *ump;
6304 	struct worklist *wk;
6305 	struct fs *fs;
6306 	ufs2_daddr_t blkno;
6307 
6308 	ump = VFSTOUFS(mp);
6309 	LOCK_OWNED(ump);
6310 	indirdep = NULL;
6311 	newindirdep = NULL;
6312 	fs = ump->um_fs;
6313 	for (;;) {
6314 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6315 			if (wk->wk_type != D_INDIRDEP)
6316 				continue;
6317 			indirdep = WK_INDIRDEP(wk);
6318 			break;
6319 		}
6320 		/* Found on the buffer worklist, no new structure to free. */
6321 		if (indirdep != NULL && newindirdep == NULL)
6322 			return (indirdep);
6323 		if (indirdep != NULL && newindirdep != NULL)
6324 			panic("indirdep_lookup: simultaneous create");
6325 		/* None found on the buffer and a new structure is ready. */
6326 		if (indirdep == NULL && newindirdep != NULL)
6327 			break;
6328 		/* None found and no new structure available. */
6329 		FREE_LOCK(ump);
6330 		newindirdep = malloc(sizeof(struct indirdep),
6331 		    M_INDIRDEP, M_SOFTDEP_FLAGS);
6332 		workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6333 		newindirdep->ir_state = ATTACHED;
6334 		if (I_IS_UFS1(ip))
6335 			newindirdep->ir_state |= UFS1FMT;
6336 		TAILQ_INIT(&newindirdep->ir_trunc);
6337 		newindirdep->ir_saveddata = NULL;
6338 		LIST_INIT(&newindirdep->ir_deplisthd);
6339 		LIST_INIT(&newindirdep->ir_donehd);
6340 		LIST_INIT(&newindirdep->ir_writehd);
6341 		LIST_INIT(&newindirdep->ir_completehd);
6342 		if (bp->b_blkno == bp->b_lblkno) {
6343 			ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6344 			    NULL, NULL);
6345 			bp->b_blkno = blkno;
6346 		}
6347 		newindirdep->ir_freeblks = NULL;
6348 		newindirdep->ir_savebp =
6349 		    getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6350 		newindirdep->ir_bp = bp;
6351 		BUF_KERNPROC(newindirdep->ir_savebp);
6352 		bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6353 		ACQUIRE_LOCK(ump);
6354 	}
6355 	indirdep = newindirdep;
6356 	WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6357 	/*
6358 	 * If the block is not yet allocated we don't set DEPCOMPLETE so
6359 	 * that we don't free dependencies until the pointers are valid.
6360 	 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6361 	 * than using the hash.
6362 	 */
6363 	if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6364 		LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6365 	else
6366 		indirdep->ir_state |= DEPCOMPLETE;
6367 	return (indirdep);
6368 }
6369 
6370 /*
6371  * Called to finish the allocation of the "aip" allocated
6372  * by one of the two routines above.
6373  */
6374 static struct freefrag *
6375 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
6376 	struct buf *bp;		/* in-memory copy of the indirect block */
6377 	struct inode *ip;	/* inode for file being extended */
6378 	struct inodedep *inodedep; /* Inodedep for ip */
6379 	struct allocindir *aip;	/* allocindir allocated by the above routines */
6380 	ufs_lbn_t lbn;		/* Logical block number for this block. */
6381 {
6382 	struct fs *fs;
6383 	struct indirdep *indirdep;
6384 	struct allocindir *oldaip;
6385 	struct freefrag *freefrag;
6386 	struct mount *mp;
6387 	struct ufsmount *ump;
6388 
6389 	mp = ITOVFS(ip);
6390 	ump = VFSTOUFS(mp);
6391 	LOCK_OWNED(ump);
6392 	fs = ump->um_fs;
6393 	if (bp->b_lblkno >= 0)
6394 		panic("setup_allocindir_phase2: not indir blk");
6395 	KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6396 	    ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6397 	indirdep = indirdep_lookup(mp, ip, bp);
6398 	KASSERT(indirdep->ir_savebp != NULL,
6399 	    ("setup_allocindir_phase2 NULL ir_savebp"));
6400 	aip->ai_indirdep = indirdep;
6401 	/*
6402 	 * Check for an unwritten dependency for this indirect offset.  If
6403 	 * there is, merge the old dependency into the new one.  This happens
6404 	 * as a result of reallocblk only.
6405 	 */
6406 	freefrag = NULL;
6407 	if (aip->ai_oldblkno != 0) {
6408 		LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6409 			if (oldaip->ai_offset == aip->ai_offset) {
6410 				freefrag = allocindir_merge(aip, oldaip);
6411 				goto done;
6412 			}
6413 		}
6414 		LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6415 			if (oldaip->ai_offset == aip->ai_offset) {
6416 				freefrag = allocindir_merge(aip, oldaip);
6417 				goto done;
6418 			}
6419 		}
6420 	}
6421 done:
6422 	LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6423 	return (freefrag);
6424 }
6425 
6426 /*
6427  * Merge two allocindirs which refer to the same block.  Move newblock
6428  * dependencies and setup the freefrags appropriately.
6429  */
6430 static struct freefrag *
6431 allocindir_merge(aip, oldaip)
6432 	struct allocindir *aip;
6433 	struct allocindir *oldaip;
6434 {
6435 	struct freefrag *freefrag;
6436 	struct worklist *wk;
6437 
6438 	if (oldaip->ai_newblkno != aip->ai_oldblkno)
6439 		panic("allocindir_merge: blkno");
6440 	aip->ai_oldblkno = oldaip->ai_oldblkno;
6441 	freefrag = aip->ai_freefrag;
6442 	aip->ai_freefrag = oldaip->ai_freefrag;
6443 	oldaip->ai_freefrag = NULL;
6444 	KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6445 	/*
6446 	 * If we are tracking a new directory-block allocation,
6447 	 * move it from the old allocindir to the new allocindir.
6448 	 */
6449 	if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6450 		WORKLIST_REMOVE(wk);
6451 		if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6452 			panic("allocindir_merge: extra newdirblk");
6453 		WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6454 	}
6455 	/*
6456 	 * We can skip journaling for this freefrag and just complete
6457 	 * any pending journal work for the allocindir that is being
6458 	 * removed after the freefrag completes.
6459 	 */
6460 	if (freefrag->ff_jdep)
6461 		cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6462 	LIST_REMOVE(oldaip, ai_next);
6463 	freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6464 	    &freefrag->ff_list, &freefrag->ff_jwork);
6465 	free_newblk(&oldaip->ai_block);
6466 
6467 	return (freefrag);
6468 }
6469 
6470 static inline void
6471 setup_freedirect(freeblks, ip, i, needj)
6472 	struct freeblks *freeblks;
6473 	struct inode *ip;
6474 	int i;
6475 	int needj;
6476 {
6477 	struct ufsmount *ump;
6478 	ufs2_daddr_t blkno;
6479 	int frags;
6480 
6481 	blkno = DIP(ip, i_db[i]);
6482 	if (blkno == 0)
6483 		return;
6484 	DIP_SET(ip, i_db[i], 0);
6485 	ump = ITOUMP(ip);
6486 	frags = sblksize(ump->um_fs, ip->i_size, i);
6487 	frags = numfrags(ump->um_fs, frags);
6488 	newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6489 }
6490 
6491 static inline void
6492 setup_freeext(freeblks, ip, i, needj)
6493 	struct freeblks *freeblks;
6494 	struct inode *ip;
6495 	int i;
6496 	int needj;
6497 {
6498 	struct ufsmount *ump;
6499 	ufs2_daddr_t blkno;
6500 	int frags;
6501 
6502 	blkno = ip->i_din2->di_extb[i];
6503 	if (blkno == 0)
6504 		return;
6505 	ip->i_din2->di_extb[i] = 0;
6506 	ump = ITOUMP(ip);
6507 	frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6508 	frags = numfrags(ump->um_fs, frags);
6509 	newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6510 }
6511 
6512 static inline void
6513 setup_freeindir(freeblks, ip, i, lbn, needj)
6514 	struct freeblks *freeblks;
6515 	struct inode *ip;
6516 	int i;
6517 	ufs_lbn_t lbn;
6518 	int needj;
6519 {
6520 	struct ufsmount *ump;
6521 	ufs2_daddr_t blkno;
6522 
6523 	blkno = DIP(ip, i_ib[i]);
6524 	if (blkno == 0)
6525 		return;
6526 	DIP_SET(ip, i_ib[i], 0);
6527 	ump = ITOUMP(ip);
6528 	newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6529 	    0, needj);
6530 }
6531 
6532 static inline struct freeblks *
6533 newfreeblks(mp, ip)
6534 	struct mount *mp;
6535 	struct inode *ip;
6536 {
6537 	struct freeblks *freeblks;
6538 
6539 	freeblks = malloc(sizeof(struct freeblks),
6540 		M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6541 	workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6542 	LIST_INIT(&freeblks->fb_jblkdephd);
6543 	LIST_INIT(&freeblks->fb_jwork);
6544 	freeblks->fb_ref = 0;
6545 	freeblks->fb_cgwait = 0;
6546 	freeblks->fb_state = ATTACHED;
6547 	freeblks->fb_uid = ip->i_uid;
6548 	freeblks->fb_inum = ip->i_number;
6549 	freeblks->fb_vtype = ITOV(ip)->v_type;
6550 	freeblks->fb_modrev = DIP(ip, i_modrev);
6551 	freeblks->fb_devvp = ITODEVVP(ip);
6552 	freeblks->fb_chkcnt = 0;
6553 	freeblks->fb_len = 0;
6554 
6555 	return (freeblks);
6556 }
6557 
6558 static void
6559 trunc_indirdep(indirdep, freeblks, bp, off)
6560 	struct indirdep *indirdep;
6561 	struct freeblks *freeblks;
6562 	struct buf *bp;
6563 	int off;
6564 {
6565 	struct allocindir *aip, *aipn;
6566 
6567 	/*
6568 	 * The first set of allocindirs won't be in savedbp.
6569 	 */
6570 	LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6571 		if (aip->ai_offset > off)
6572 			cancel_allocindir(aip, bp, freeblks, 1);
6573 	LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6574 		if (aip->ai_offset > off)
6575 			cancel_allocindir(aip, bp, freeblks, 1);
6576 	/*
6577 	 * These will exist in savedbp.
6578 	 */
6579 	LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6580 		if (aip->ai_offset > off)
6581 			cancel_allocindir(aip, NULL, freeblks, 0);
6582 	LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6583 		if (aip->ai_offset > off)
6584 			cancel_allocindir(aip, NULL, freeblks, 0);
6585 }
6586 
6587 /*
6588  * Follow the chain of indirects down to lastlbn creating a freework
6589  * structure for each.  This will be used to start indir_trunc() at
6590  * the right offset and create the journal records for the parrtial
6591  * truncation.  A second step will handle the truncated dependencies.
6592  */
6593 static int
6594 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6595 	struct freeblks *freeblks;
6596 	struct inode *ip;
6597 	ufs_lbn_t lbn;
6598 	ufs_lbn_t lastlbn;
6599 	ufs2_daddr_t blkno;
6600 {
6601 	struct indirdep *indirdep;
6602 	struct indirdep *indirn;
6603 	struct freework *freework;
6604 	struct newblk *newblk;
6605 	struct mount *mp;
6606 	struct ufsmount *ump;
6607 	struct buf *bp;
6608 	uint8_t *start;
6609 	uint8_t *end;
6610 	ufs_lbn_t lbnadd;
6611 	int level;
6612 	int error;
6613 	int off;
6614 
6615 	freework = NULL;
6616 	if (blkno == 0)
6617 		return (0);
6618 	mp = freeblks->fb_list.wk_mp;
6619 	ump = VFSTOUFS(mp);
6620 	/*
6621 	 * Here, calls to VOP_BMAP() will fail.  However, we already have
6622 	 * the on-disk address, so we just pass it to bread() instead of
6623 	 * having bread() attempt to calculate it using VOP_BMAP().
6624 	 */
6625 	error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno),
6626 	    (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6627 	if (error)
6628 		return (error);
6629 	level = lbn_level(lbn);
6630 	lbnadd = lbn_offset(ump->um_fs, level);
6631 	/*
6632 	 * Compute the offset of the last block we want to keep.  Store
6633 	 * in the freework the first block we want to completely free.
6634 	 */
6635 	off = (lastlbn - -(lbn + level)) / lbnadd;
6636 	if (off + 1 == NINDIR(ump->um_fs))
6637 		goto nowork;
6638 	freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6639 	/*
6640 	 * Link the freework into the indirdep.  This will prevent any new
6641 	 * allocations from proceeding until we are finished with the
6642 	 * truncate and the block is written.
6643 	 */
6644 	ACQUIRE_LOCK(ump);
6645 	indirdep = indirdep_lookup(mp, ip, bp);
6646 	if (indirdep->ir_freeblks)
6647 		panic("setup_trunc_indir: indirdep already truncated.");
6648 	TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6649 	freework->fw_indir = indirdep;
6650 	/*
6651 	 * Cancel any allocindirs that will not make it to disk.
6652 	 * We have to do this for all copies of the indirdep that
6653 	 * live on this newblk.
6654 	 */
6655 	if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6656 		if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6657 		    &newblk) == 0)
6658 			panic("setup_trunc_indir: lost block");
6659 		LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6660 			trunc_indirdep(indirn, freeblks, bp, off);
6661 	} else
6662 		trunc_indirdep(indirdep, freeblks, bp, off);
6663 	FREE_LOCK(ump);
6664 	/*
6665 	 * Creation is protected by the buf lock. The saveddata is only
6666 	 * needed if a full truncation follows a partial truncation but it
6667 	 * is difficult to allocate in that case so we fetch it anyway.
6668 	 */
6669 	if (indirdep->ir_saveddata == NULL)
6670 		indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6671 		    M_SOFTDEP_FLAGS);
6672 nowork:
6673 	/* Fetch the blkno of the child and the zero start offset. */
6674 	if (I_IS_UFS1(ip)) {
6675 		blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6676 		start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6677 	} else {
6678 		blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6679 		start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6680 	}
6681 	if (freework) {
6682 		/* Zero the truncated pointers. */
6683 		end = bp->b_data + bp->b_bcount;
6684 		bzero(start, end - start);
6685 		bdwrite(bp);
6686 	} else
6687 		bqrelse(bp);
6688 	if (level == 0)
6689 		return (0);
6690 	lbn++; /* adjust level */
6691 	lbn -= (off * lbnadd);
6692 	return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6693 }
6694 
6695 /*
6696  * Complete the partial truncation of an indirect block setup by
6697  * setup_trunc_indir().  This zeros the truncated pointers in the saved
6698  * copy and writes them to disk before the freeblks is allowed to complete.
6699  */
6700 static void
6701 complete_trunc_indir(freework)
6702 	struct freework *freework;
6703 {
6704 	struct freework *fwn;
6705 	struct indirdep *indirdep;
6706 	struct ufsmount *ump;
6707 	struct buf *bp;
6708 	uintptr_t start;
6709 	int count;
6710 
6711 	ump = VFSTOUFS(freework->fw_list.wk_mp);
6712 	LOCK_OWNED(ump);
6713 	indirdep = freework->fw_indir;
6714 	for (;;) {
6715 		bp = indirdep->ir_bp;
6716 		/* See if the block was discarded. */
6717 		if (bp == NULL)
6718 			break;
6719 		/* Inline part of getdirtybuf().  We dont want bremfree. */
6720 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6721 			break;
6722 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6723 		    LOCK_PTR(ump)) == 0)
6724 			BUF_UNLOCK(bp);
6725 		ACQUIRE_LOCK(ump);
6726 	}
6727 	freework->fw_state |= DEPCOMPLETE;
6728 	TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6729 	/*
6730 	 * Zero the pointers in the saved copy.
6731 	 */
6732 	if (indirdep->ir_state & UFS1FMT)
6733 		start = sizeof(ufs1_daddr_t);
6734 	else
6735 		start = sizeof(ufs2_daddr_t);
6736 	start *= freework->fw_start;
6737 	count = indirdep->ir_savebp->b_bcount - start;
6738 	start += (uintptr_t)indirdep->ir_savebp->b_data;
6739 	bzero((char *)start, count);
6740 	/*
6741 	 * We need to start the next truncation in the list if it has not
6742 	 * been started yet.
6743 	 */
6744 	fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6745 	if (fwn != NULL) {
6746 		if (fwn->fw_freeblks == indirdep->ir_freeblks)
6747 			TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6748 		if ((fwn->fw_state & ONWORKLIST) == 0)
6749 			freework_enqueue(fwn);
6750 	}
6751 	/*
6752 	 * If bp is NULL the block was fully truncated, restore
6753 	 * the saved block list otherwise free it if it is no
6754 	 * longer needed.
6755 	 */
6756 	if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6757 		if (bp == NULL)
6758 			bcopy(indirdep->ir_saveddata,
6759 			    indirdep->ir_savebp->b_data,
6760 			    indirdep->ir_savebp->b_bcount);
6761 		free(indirdep->ir_saveddata, M_INDIRDEP);
6762 		indirdep->ir_saveddata = NULL;
6763 	}
6764 	/*
6765 	 * When bp is NULL there is a full truncation pending.  We
6766 	 * must wait for this full truncation to be journaled before
6767 	 * we can release this freework because the disk pointers will
6768 	 * never be written as zero.
6769 	 */
6770 	if (bp == NULL)  {
6771 		if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6772 			handle_written_freework(freework);
6773 		else
6774 			WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6775 			   &freework->fw_list);
6776 		if (fwn == NULL) {
6777 			freework->fw_indir = (void *)0x0000deadbeef0000;
6778 			bp = indirdep->ir_savebp;
6779 			indirdep->ir_savebp = NULL;
6780 			free_indirdep(indirdep);
6781 			FREE_LOCK(ump);
6782 			brelse(bp);
6783 			ACQUIRE_LOCK(ump);
6784 		}
6785 	} else {
6786 		/* Complete when the real copy is written. */
6787 		WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6788 		BUF_UNLOCK(bp);
6789 	}
6790 }
6791 
6792 /*
6793  * Calculate the number of blocks we are going to release where datablocks
6794  * is the current total and length is the new file size.
6795  */
6796 static ufs2_daddr_t
6797 blkcount(fs, datablocks, length)
6798 	struct fs *fs;
6799 	ufs2_daddr_t datablocks;
6800 	off_t length;
6801 {
6802 	off_t totblks, numblks;
6803 
6804 	totblks = 0;
6805 	numblks = howmany(length, fs->fs_bsize);
6806 	if (numblks <= UFS_NDADDR) {
6807 		totblks = howmany(length, fs->fs_fsize);
6808 		goto out;
6809 	}
6810         totblks = blkstofrags(fs, numblks);
6811 	numblks -= UFS_NDADDR;
6812 	/*
6813 	 * Count all single, then double, then triple indirects required.
6814 	 * Subtracting one indirects worth of blocks for each pass
6815 	 * acknowledges one of each pointed to by the inode.
6816 	 */
6817 	for (;;) {
6818 		totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6819 		numblks -= NINDIR(fs);
6820 		if (numblks <= 0)
6821 			break;
6822 		numblks = howmany(numblks, NINDIR(fs));
6823 	}
6824 out:
6825 	totblks = fsbtodb(fs, totblks);
6826 	/*
6827 	 * Handle sparse files.  We can't reclaim more blocks than the inode
6828 	 * references.  We will correct it later in handle_complete_freeblks()
6829 	 * when we know the real count.
6830 	 */
6831 	if (totblks > datablocks)
6832 		return (0);
6833 	return (datablocks - totblks);
6834 }
6835 
6836 /*
6837  * Handle freeblocks for journaled softupdate filesystems.
6838  *
6839  * Contrary to normal softupdates, we must preserve the block pointers in
6840  * indirects until their subordinates are free.  This is to avoid journaling
6841  * every block that is freed which may consume more space than the journal
6842  * itself.  The recovery program will see the free block journals at the
6843  * base of the truncated area and traverse them to reclaim space.  The
6844  * pointers in the inode may be cleared immediately after the journal
6845  * records are written because each direct and indirect pointer in the
6846  * inode is recorded in a journal.  This permits full truncation to proceed
6847  * asynchronously.  The write order is journal -> inode -> cgs -> indirects.
6848  *
6849  * The algorithm is as follows:
6850  * 1) Traverse the in-memory state and create journal entries to release
6851  *    the relevant blocks and full indirect trees.
6852  * 2) Traverse the indirect block chain adding partial truncation freework
6853  *    records to indirects in the path to lastlbn.  The freework will
6854  *    prevent new allocation dependencies from being satisfied in this
6855  *    indirect until the truncation completes.
6856  * 3) Read and lock the inode block, performing an update with the new size
6857  *    and pointers.  This prevents truncated data from becoming valid on
6858  *    disk through step 4.
6859  * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6860  *    eliminate journal work for those records that do not require it.
6861  * 5) Schedule the journal records to be written followed by the inode block.
6862  * 6) Allocate any necessary frags for the end of file.
6863  * 7) Zero any partially truncated blocks.
6864  *
6865  * From this truncation proceeds asynchronously using the freework and
6866  * indir_trunc machinery.  The file will not be extended again into a
6867  * partially truncated indirect block until all work is completed but
6868  * the normal dependency mechanism ensures that it is rolled back/forward
6869  * as appropriate.  Further truncation may occur without delay and is
6870  * serialized in indir_trunc().
6871  */
6872 void
6873 softdep_journal_freeblocks(ip, cred, length, flags)
6874 	struct inode *ip;	/* The inode whose length is to be reduced */
6875 	struct ucred *cred;
6876 	off_t length;		/* The new length for the file */
6877 	int flags;		/* IO_EXT and/or IO_NORMAL */
6878 {
6879 	struct freeblks *freeblks, *fbn;
6880 	struct worklist *wk, *wkn;
6881 	struct inodedep *inodedep;
6882 	struct jblkdep *jblkdep;
6883 	struct allocdirect *adp, *adpn;
6884 	struct ufsmount *ump;
6885 	struct fs *fs;
6886 	struct buf *bp;
6887 	struct vnode *vp;
6888 	struct mount *mp;
6889 	daddr_t dbn;
6890 	ufs2_daddr_t extblocks, datablocks;
6891 	ufs_lbn_t tmpval, lbn, lastlbn;
6892 	int frags, lastoff, iboff, allocblock, needj, error, i;
6893 
6894 	ump = ITOUMP(ip);
6895 	mp = UFSTOVFS(ump);
6896 	fs = ump->um_fs;
6897 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6898 	    ("softdep_journal_freeblocks called on non-softdep filesystem"));
6899 	vp = ITOV(ip);
6900 	needj = 1;
6901 	iboff = -1;
6902 	allocblock = 0;
6903 	extblocks = 0;
6904 	datablocks = 0;
6905 	frags = 0;
6906 	freeblks = newfreeblks(mp, ip);
6907 	ACQUIRE_LOCK(ump);
6908 	/*
6909 	 * If we're truncating a removed file that will never be written
6910 	 * we don't need to journal the block frees.  The canceled journals
6911 	 * for the allocations will suffice.
6912 	 */
6913 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6914 	if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6915 	    length == 0)
6916 		needj = 0;
6917 	CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6918 	    ip->i_number, length, needj);
6919 	FREE_LOCK(ump);
6920 	/*
6921 	 * Calculate the lbn that we are truncating to.  This results in -1
6922 	 * if we're truncating the 0 bytes.  So it is the last lbn we want
6923 	 * to keep, not the first lbn we want to truncate.
6924 	 */
6925 	lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6926 	lastoff = blkoff(fs, length);
6927 	/*
6928 	 * Compute frags we are keeping in lastlbn.  0 means all.
6929 	 */
6930 	if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6931 		frags = fragroundup(fs, lastoff);
6932 		/* adp offset of last valid allocdirect. */
6933 		iboff = lastlbn;
6934 	} else if (lastlbn > 0)
6935 		iboff = UFS_NDADDR;
6936 	if (fs->fs_magic == FS_UFS2_MAGIC)
6937 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6938 	/*
6939 	 * Handle normal data blocks and indirects.  This section saves
6940 	 * values used after the inode update to complete frag and indirect
6941 	 * truncation.
6942 	 */
6943 	if ((flags & IO_NORMAL) != 0) {
6944 		/*
6945 		 * Handle truncation of whole direct and indirect blocks.
6946 		 */
6947 		for (i = iboff + 1; i < UFS_NDADDR; i++)
6948 			setup_freedirect(freeblks, ip, i, needj);
6949 		for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6950 		    i < UFS_NIADDR;
6951 		    i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6952 			/* Release a whole indirect tree. */
6953 			if (lbn > lastlbn) {
6954 				setup_freeindir(freeblks, ip, i, -lbn -i,
6955 				    needj);
6956 				continue;
6957 			}
6958 			iboff = i + UFS_NDADDR;
6959 			/*
6960 			 * Traverse partially truncated indirect tree.
6961 			 */
6962 			if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6963 				setup_trunc_indir(freeblks, ip, -lbn - i,
6964 				    lastlbn, DIP(ip, i_ib[i]));
6965 		}
6966 		/*
6967 		 * Handle partial truncation to a frag boundary.
6968 		 */
6969 		if (frags) {
6970 			ufs2_daddr_t blkno;
6971 			long oldfrags;
6972 
6973 			oldfrags = blksize(fs, ip, lastlbn);
6974 			blkno = DIP(ip, i_db[lastlbn]);
6975 			if (blkno && oldfrags != frags) {
6976 				oldfrags -= frags;
6977 				oldfrags = numfrags(fs, oldfrags);
6978 				blkno += numfrags(fs, frags);
6979 				newfreework(ump, freeblks, NULL, lastlbn,
6980 				    blkno, oldfrags, 0, needj);
6981 				if (needj)
6982 					adjust_newfreework(freeblks,
6983 					    numfrags(fs, frags));
6984 			} else if (blkno == 0)
6985 				allocblock = 1;
6986 		}
6987 		/*
6988 		 * Add a journal record for partial truncate if we are
6989 		 * handling indirect blocks.  Non-indirects need no extra
6990 		 * journaling.
6991 		 */
6992 		if (length != 0 && lastlbn >= UFS_NDADDR) {
6993 			UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
6994 			newjtrunc(freeblks, length, 0);
6995 		}
6996 		ip->i_size = length;
6997 		DIP_SET(ip, i_size, ip->i_size);
6998 		UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6999 		datablocks = DIP(ip, i_blocks) - extblocks;
7000 		if (length != 0)
7001 			datablocks = blkcount(fs, datablocks, length);
7002 		freeblks->fb_len = length;
7003 	}
7004 	if ((flags & IO_EXT) != 0) {
7005 		for (i = 0; i < UFS_NXADDR; i++)
7006 			setup_freeext(freeblks, ip, i, needj);
7007 		ip->i_din2->di_extsize = 0;
7008 		datablocks += extblocks;
7009 		UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7010 	}
7011 #ifdef QUOTA
7012 	/* Reference the quotas in case the block count is wrong in the end. */
7013 	quotaref(vp, freeblks->fb_quota);
7014 	(void) chkdq(ip, -datablocks, NOCRED, FORCE);
7015 #endif
7016 	freeblks->fb_chkcnt = -datablocks;
7017 	UFS_LOCK(ump);
7018 	fs->fs_pendingblocks += datablocks;
7019 	UFS_UNLOCK(ump);
7020 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7021 	/*
7022 	 * Handle truncation of incomplete alloc direct dependencies.  We
7023 	 * hold the inode block locked to prevent incomplete dependencies
7024 	 * from reaching the disk while we are eliminating those that
7025 	 * have been truncated.  This is a partially inlined ffs_update().
7026 	 */
7027 	ufs_itimes(vp);
7028 	ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
7029 	dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
7030 	error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
7031 	    NULL, NULL, 0, cred, 0, NULL, &bp);
7032 	if (error) {
7033 		softdep_error("softdep_journal_freeblocks", error);
7034 		return;
7035 	}
7036 	if (bp->b_bufsize == fs->fs_bsize)
7037 		bp->b_flags |= B_CLUSTEROK;
7038 	softdep_update_inodeblock(ip, bp, 0);
7039 	if (ump->um_fstype == UFS1) {
7040 		*((struct ufs1_dinode *)bp->b_data +
7041 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
7042 	} else {
7043 		ffs_update_dinode_ckhash(fs, ip->i_din2);
7044 		*((struct ufs2_dinode *)bp->b_data +
7045 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
7046 	}
7047 	ACQUIRE_LOCK(ump);
7048 	(void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7049 	if ((inodedep->id_state & IOSTARTED) != 0)
7050 		panic("softdep_setup_freeblocks: inode busy");
7051 	/*
7052 	 * Add the freeblks structure to the list of operations that
7053 	 * must await the zero'ed inode being written to disk. If we
7054 	 * still have a bitmap dependency (needj), then the inode
7055 	 * has never been written to disk, so we can process the
7056 	 * freeblks below once we have deleted the dependencies.
7057 	 */
7058 	if (needj)
7059 		WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7060 	else
7061 		freeblks->fb_state |= COMPLETE;
7062 	if ((flags & IO_NORMAL) != 0) {
7063 		TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
7064 			if (adp->ad_offset > iboff)
7065 				cancel_allocdirect(&inodedep->id_inoupdt, adp,
7066 				    freeblks);
7067 			/*
7068 			 * Truncate the allocdirect.  We could eliminate
7069 			 * or modify journal records as well.
7070 			 */
7071 			else if (adp->ad_offset == iboff && frags)
7072 				adp->ad_newsize = frags;
7073 		}
7074 	}
7075 	if ((flags & IO_EXT) != 0)
7076 		while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7077 			cancel_allocdirect(&inodedep->id_extupdt, adp,
7078 			    freeblks);
7079 	/*
7080 	 * Scan the bufwait list for newblock dependencies that will never
7081 	 * make it to disk.
7082 	 */
7083 	LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
7084 		if (wk->wk_type != D_ALLOCDIRECT)
7085 			continue;
7086 		adp = WK_ALLOCDIRECT(wk);
7087 		if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
7088 		    ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
7089 			cancel_jfreeblk(freeblks, adp->ad_newblkno);
7090 			cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
7091 			WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7092 		}
7093 	}
7094 	/*
7095 	 * Add journal work.
7096 	 */
7097 	LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
7098 		add_to_journal(&jblkdep->jb_list);
7099 	FREE_LOCK(ump);
7100 	bdwrite(bp);
7101 	/*
7102 	 * Truncate dependency structures beyond length.
7103 	 */
7104 	trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
7105 	/*
7106 	 * This is only set when we need to allocate a fragment because
7107 	 * none existed at the end of a frag-sized file.  It handles only
7108 	 * allocating a new, zero filled block.
7109 	 */
7110 	if (allocblock) {
7111 		ip->i_size = length - lastoff;
7112 		DIP_SET(ip, i_size, ip->i_size);
7113 		error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
7114 		if (error != 0) {
7115 			softdep_error("softdep_journal_freeblks", error);
7116 			return;
7117 		}
7118 		ip->i_size = length;
7119 		DIP_SET(ip, i_size, length);
7120 		UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
7121 		allocbuf(bp, frags);
7122 		ffs_update(vp, 0);
7123 		bawrite(bp);
7124 	} else if (lastoff != 0 && vp->v_type != VDIR) {
7125 		int size;
7126 
7127 		/*
7128 		 * Zero the end of a truncated frag or block.
7129 		 */
7130 		size = sblksize(fs, length, lastlbn);
7131 		error = bread(vp, lastlbn, size, cred, &bp);
7132 		if (error == 0) {
7133 			bzero((char *)bp->b_data + lastoff, size - lastoff);
7134 			bawrite(bp);
7135 		} else if (!ffs_fsfail_cleanup(ump, error)) {
7136 			softdep_error("softdep_journal_freeblks", error);
7137 			return;
7138 		}
7139 	}
7140 	ACQUIRE_LOCK(ump);
7141 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7142 	TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
7143 	freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
7144 	/*
7145 	 * We zero earlier truncations so they don't erroneously
7146 	 * update i_blocks.
7147 	 */
7148 	if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
7149 		TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
7150 			fbn->fb_len = 0;
7151 	if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
7152 	    LIST_EMPTY(&freeblks->fb_jblkdephd))
7153 		freeblks->fb_state |= INPROGRESS;
7154 	else
7155 		freeblks = NULL;
7156 	FREE_LOCK(ump);
7157 	if (freeblks)
7158 		handle_workitem_freeblocks(freeblks, 0);
7159 	trunc_pages(ip, length, extblocks, flags);
7160 
7161 }
7162 
7163 /*
7164  * Flush a JOP_SYNC to the journal.
7165  */
7166 void
7167 softdep_journal_fsync(ip)
7168 	struct inode *ip;
7169 {
7170 	struct jfsync *jfsync;
7171 	struct ufsmount *ump;
7172 
7173 	ump = ITOUMP(ip);
7174 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7175 	    ("softdep_journal_fsync called on non-softdep filesystem"));
7176 	if ((ip->i_flag & IN_TRUNCATED) == 0)
7177 		return;
7178 	ip->i_flag &= ~IN_TRUNCATED;
7179 	jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
7180 	workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
7181 	jfsync->jfs_size = ip->i_size;
7182 	jfsync->jfs_ino = ip->i_number;
7183 	ACQUIRE_LOCK(ump);
7184 	add_to_journal(&jfsync->jfs_list);
7185 	jwait(&jfsync->jfs_list, MNT_WAIT);
7186 	FREE_LOCK(ump);
7187 }
7188 
7189 /*
7190  * Block de-allocation dependencies.
7191  *
7192  * When blocks are de-allocated, the on-disk pointers must be nullified before
7193  * the blocks are made available for use by other files.  (The true
7194  * requirement is that old pointers must be nullified before new on-disk
7195  * pointers are set.  We chose this slightly more stringent requirement to
7196  * reduce complexity.) Our implementation handles this dependency by updating
7197  * the inode (or indirect block) appropriately but delaying the actual block
7198  * de-allocation (i.e., freemap and free space count manipulation) until
7199  * after the updated versions reach stable storage.  After the disk is
7200  * updated, the blocks can be safely de-allocated whenever it is convenient.
7201  * This implementation handles only the common case of reducing a file's
7202  * length to zero. Other cases are handled by the conventional synchronous
7203  * write approach.
7204  *
7205  * The ffs implementation with which we worked double-checks
7206  * the state of the block pointers and file size as it reduces
7207  * a file's length.  Some of this code is replicated here in our
7208  * soft updates implementation.  The freeblks->fb_chkcnt field is
7209  * used to transfer a part of this information to the procedure
7210  * that eventually de-allocates the blocks.
7211  *
7212  * This routine should be called from the routine that shortens
7213  * a file's length, before the inode's size or block pointers
7214  * are modified. It will save the block pointer information for
7215  * later release and zero the inode so that the calling routine
7216  * can release it.
7217  */
7218 void
7219 softdep_setup_freeblocks(ip, length, flags)
7220 	struct inode *ip;	/* The inode whose length is to be reduced */
7221 	off_t length;		/* The new length for the file */
7222 	int flags;		/* IO_EXT and/or IO_NORMAL */
7223 {
7224 	struct ufs1_dinode *dp1;
7225 	struct ufs2_dinode *dp2;
7226 	struct freeblks *freeblks;
7227 	struct inodedep *inodedep;
7228 	struct allocdirect *adp;
7229 	struct ufsmount *ump;
7230 	struct buf *bp;
7231 	struct fs *fs;
7232 	ufs2_daddr_t extblocks, datablocks;
7233 	struct mount *mp;
7234 	int i, delay, error;
7235 	ufs_lbn_t tmpval;
7236 	ufs_lbn_t lbn;
7237 
7238 	ump = ITOUMP(ip);
7239 	mp = UFSTOVFS(ump);
7240 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
7241 	    ("softdep_setup_freeblocks called on non-softdep filesystem"));
7242 	CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
7243 	    ip->i_number, length);
7244 	KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
7245 	fs = ump->um_fs;
7246 	if ((error = bread(ump->um_devvp,
7247 	    fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
7248 	    (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
7249 		if (!ffs_fsfail_cleanup(ump, error))
7250 			softdep_error("softdep_setup_freeblocks", error);
7251 		return;
7252 	}
7253 	freeblks = newfreeblks(mp, ip);
7254 	extblocks = 0;
7255 	datablocks = 0;
7256 	if (fs->fs_magic == FS_UFS2_MAGIC)
7257 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
7258 	if ((flags & IO_NORMAL) != 0) {
7259 		for (i = 0; i < UFS_NDADDR; i++)
7260 			setup_freedirect(freeblks, ip, i, 0);
7261 		for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
7262 		    i < UFS_NIADDR;
7263 		    i++, lbn += tmpval, tmpval *= NINDIR(fs))
7264 			setup_freeindir(freeblks, ip, i, -lbn -i, 0);
7265 		ip->i_size = 0;
7266 		DIP_SET(ip, i_size, 0);
7267 		UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7268 		datablocks = DIP(ip, i_blocks) - extblocks;
7269 	}
7270 	if ((flags & IO_EXT) != 0) {
7271 		for (i = 0; i < UFS_NXADDR; i++)
7272 			setup_freeext(freeblks, ip, i, 0);
7273 		ip->i_din2->di_extsize = 0;
7274 		datablocks += extblocks;
7275 		UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7276 	}
7277 #ifdef QUOTA
7278 	/* Reference the quotas in case the block count is wrong in the end. */
7279 	quotaref(ITOV(ip), freeblks->fb_quota);
7280 	(void) chkdq(ip, -datablocks, NOCRED, FORCE);
7281 #endif
7282 	freeblks->fb_chkcnt = -datablocks;
7283 	UFS_LOCK(ump);
7284 	fs->fs_pendingblocks += datablocks;
7285 	UFS_UNLOCK(ump);
7286 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7287 	/*
7288 	 * Push the zero'ed inode to its disk buffer so that we are free
7289 	 * to delete its dependencies below. Once the dependencies are gone
7290 	 * the buffer can be safely released.
7291 	 */
7292 	if (ump->um_fstype == UFS1) {
7293 		dp1 = ((struct ufs1_dinode *)bp->b_data +
7294 		    ino_to_fsbo(fs, ip->i_number));
7295 		ip->i_din1->di_freelink = dp1->di_freelink;
7296 		*dp1 = *ip->i_din1;
7297 	} else {
7298 		dp2 = ((struct ufs2_dinode *)bp->b_data +
7299 		    ino_to_fsbo(fs, ip->i_number));
7300 		ip->i_din2->di_freelink = dp2->di_freelink;
7301 		ffs_update_dinode_ckhash(fs, ip->i_din2);
7302 		*dp2 = *ip->i_din2;
7303 	}
7304 	/*
7305 	 * Find and eliminate any inode dependencies.
7306 	 */
7307 	ACQUIRE_LOCK(ump);
7308 	(void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7309 	if ((inodedep->id_state & IOSTARTED) != 0)
7310 		panic("softdep_setup_freeblocks: inode busy");
7311 	/*
7312 	 * Add the freeblks structure to the list of operations that
7313 	 * must await the zero'ed inode being written to disk. If we
7314 	 * still have a bitmap dependency (delay == 0), then the inode
7315 	 * has never been written to disk, so we can process the
7316 	 * freeblks below once we have deleted the dependencies.
7317 	 */
7318 	delay = (inodedep->id_state & DEPCOMPLETE);
7319 	if (delay)
7320 		WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7321 	else
7322 		freeblks->fb_state |= COMPLETE;
7323 	/*
7324 	 * Because the file length has been truncated to zero, any
7325 	 * pending block allocation dependency structures associated
7326 	 * with this inode are obsolete and can simply be de-allocated.
7327 	 * We must first merge the two dependency lists to get rid of
7328 	 * any duplicate freefrag structures, then purge the merged list.
7329 	 * If we still have a bitmap dependency, then the inode has never
7330 	 * been written to disk, so we can free any fragments without delay.
7331 	 */
7332 	if (flags & IO_NORMAL) {
7333 		merge_inode_lists(&inodedep->id_newinoupdt,
7334 		    &inodedep->id_inoupdt);
7335 		while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7336 			cancel_allocdirect(&inodedep->id_inoupdt, adp,
7337 			    freeblks);
7338 	}
7339 	if (flags & IO_EXT) {
7340 		merge_inode_lists(&inodedep->id_newextupdt,
7341 		    &inodedep->id_extupdt);
7342 		while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7343 			cancel_allocdirect(&inodedep->id_extupdt, adp,
7344 			    freeblks);
7345 	}
7346 	FREE_LOCK(ump);
7347 	bdwrite(bp);
7348 	trunc_dependencies(ip, freeblks, -1, 0, flags);
7349 	ACQUIRE_LOCK(ump);
7350 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7351 		(void) free_inodedep(inodedep);
7352 	freeblks->fb_state |= DEPCOMPLETE;
7353 	/*
7354 	 * If the inode with zeroed block pointers is now on disk
7355 	 * we can start freeing blocks.
7356 	 */
7357 	if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7358 		freeblks->fb_state |= INPROGRESS;
7359 	else
7360 		freeblks = NULL;
7361 	FREE_LOCK(ump);
7362 	if (freeblks)
7363 		handle_workitem_freeblocks(freeblks, 0);
7364 	trunc_pages(ip, length, extblocks, flags);
7365 }
7366 
7367 /*
7368  * Eliminate pages from the page cache that back parts of this inode and
7369  * adjust the vnode pager's idea of our size.  This prevents stale data
7370  * from hanging around in the page cache.
7371  */
7372 static void
7373 trunc_pages(ip, length, extblocks, flags)
7374 	struct inode *ip;
7375 	off_t length;
7376 	ufs2_daddr_t extblocks;
7377 	int flags;
7378 {
7379 	struct vnode *vp;
7380 	struct fs *fs;
7381 	ufs_lbn_t lbn;
7382 	off_t end, extend;
7383 
7384 	vp = ITOV(ip);
7385 	fs = ITOFS(ip);
7386 	extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7387 	if ((flags & IO_EXT) != 0)
7388 		vn_pages_remove(vp, extend, 0);
7389 	if ((flags & IO_NORMAL) == 0)
7390 		return;
7391 	BO_LOCK(&vp->v_bufobj);
7392 	drain_output(vp);
7393 	BO_UNLOCK(&vp->v_bufobj);
7394 	/*
7395 	 * The vnode pager eliminates file pages we eliminate indirects
7396 	 * below.
7397 	 */
7398 	vnode_pager_setsize(vp, length);
7399 	/*
7400 	 * Calculate the end based on the last indirect we want to keep.  If
7401 	 * the block extends into indirects we can just use the negative of
7402 	 * its lbn.  Doubles and triples exist at lower numbers so we must
7403 	 * be careful not to remove those, if they exist.  double and triple
7404 	 * indirect lbns do not overlap with others so it is not important
7405 	 * to verify how many levels are required.
7406 	 */
7407 	lbn = lblkno(fs, length);
7408 	if (lbn >= UFS_NDADDR) {
7409 		/* Calculate the virtual lbn of the triple indirect. */
7410 		lbn = -lbn - (UFS_NIADDR - 1);
7411 		end = OFF_TO_IDX(lblktosize(fs, lbn));
7412 	} else
7413 		end = extend;
7414 	vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7415 }
7416 
7417 /*
7418  * See if the buf bp is in the range eliminated by truncation.
7419  */
7420 static int
7421 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7422 	struct buf *bp;
7423 	int *blkoffp;
7424 	ufs_lbn_t lastlbn;
7425 	int lastoff;
7426 	int flags;
7427 {
7428 	ufs_lbn_t lbn;
7429 
7430 	*blkoffp = 0;
7431 	/* Only match ext/normal blocks as appropriate. */
7432 	if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7433 	    ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7434 		return (0);
7435 	/* ALTDATA is always a full truncation. */
7436 	if ((bp->b_xflags & BX_ALTDATA) != 0)
7437 		return (1);
7438 	/* -1 is full truncation. */
7439 	if (lastlbn == -1)
7440 		return (1);
7441 	/*
7442 	 * If this is a partial truncate we only want those
7443 	 * blocks and indirect blocks that cover the range
7444 	 * we're after.
7445 	 */
7446 	lbn = bp->b_lblkno;
7447 	if (lbn < 0)
7448 		lbn = -(lbn + lbn_level(lbn));
7449 	if (lbn < lastlbn)
7450 		return (0);
7451 	/* Here we only truncate lblkno if it's partial. */
7452 	if (lbn == lastlbn) {
7453 		if (lastoff == 0)
7454 			return (0);
7455 		*blkoffp = lastoff;
7456 	}
7457 	return (1);
7458 }
7459 
7460 /*
7461  * Eliminate any dependencies that exist in memory beyond lblkno:off
7462  */
7463 static void
7464 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7465 	struct inode *ip;
7466 	struct freeblks *freeblks;
7467 	ufs_lbn_t lastlbn;
7468 	int lastoff;
7469 	int flags;
7470 {
7471 	struct bufobj *bo;
7472 	struct vnode *vp;
7473 	struct buf *bp;
7474 	int blkoff;
7475 
7476 	/*
7477 	 * We must wait for any I/O in progress to finish so that
7478 	 * all potential buffers on the dirty list will be visible.
7479 	 * Once they are all there, walk the list and get rid of
7480 	 * any dependencies.
7481 	 */
7482 	vp = ITOV(ip);
7483 	bo = &vp->v_bufobj;
7484 	BO_LOCK(bo);
7485 	drain_output(vp);
7486 	TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7487 		bp->b_vflags &= ~BV_SCANNED;
7488 restart:
7489 	TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7490 		if (bp->b_vflags & BV_SCANNED)
7491 			continue;
7492 		if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7493 			bp->b_vflags |= BV_SCANNED;
7494 			continue;
7495 		}
7496 		KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7497 		if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7498 			goto restart;
7499 		BO_UNLOCK(bo);
7500 		if (deallocate_dependencies(bp, freeblks, blkoff))
7501 			bqrelse(bp);
7502 		else
7503 			brelse(bp);
7504 		BO_LOCK(bo);
7505 		goto restart;
7506 	}
7507 	/*
7508 	 * Now do the work of vtruncbuf while also matching indirect blocks.
7509 	 */
7510 	TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7511 		bp->b_vflags &= ~BV_SCANNED;
7512 cleanrestart:
7513 	TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7514 		if (bp->b_vflags & BV_SCANNED)
7515 			continue;
7516 		if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7517 			bp->b_vflags |= BV_SCANNED;
7518 			continue;
7519 		}
7520 		if (BUF_LOCK(bp,
7521 		    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7522 		    BO_LOCKPTR(bo)) == ENOLCK) {
7523 			BO_LOCK(bo);
7524 			goto cleanrestart;
7525 		}
7526 		bp->b_vflags |= BV_SCANNED;
7527 		bremfree(bp);
7528 		if (blkoff != 0) {
7529 			allocbuf(bp, blkoff);
7530 			bqrelse(bp);
7531 		} else {
7532 			bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7533 			brelse(bp);
7534 		}
7535 		BO_LOCK(bo);
7536 		goto cleanrestart;
7537 	}
7538 	drain_output(vp);
7539 	BO_UNLOCK(bo);
7540 }
7541 
7542 static int
7543 cancel_pagedep(pagedep, freeblks, blkoff)
7544 	struct pagedep *pagedep;
7545 	struct freeblks *freeblks;
7546 	int blkoff;
7547 {
7548 	struct jremref *jremref;
7549 	struct jmvref *jmvref;
7550 	struct dirrem *dirrem, *tmp;
7551 	int i;
7552 
7553 	/*
7554 	 * Copy any directory remove dependencies to the list
7555 	 * to be processed after the freeblks proceeds.  If
7556 	 * directory entry never made it to disk they
7557 	 * can be dumped directly onto the work list.
7558 	 */
7559 	LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7560 		/* Skip this directory removal if it is intended to remain. */
7561 		if (dirrem->dm_offset < blkoff)
7562 			continue;
7563 		/*
7564 		 * If there are any dirrems we wait for the journal write
7565 		 * to complete and then restart the buf scan as the lock
7566 		 * has been dropped.
7567 		 */
7568 		while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7569 			jwait(&jremref->jr_list, MNT_WAIT);
7570 			return (ERESTART);
7571 		}
7572 		LIST_REMOVE(dirrem, dm_next);
7573 		dirrem->dm_dirinum = pagedep->pd_ino;
7574 		WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7575 	}
7576 	while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7577 		jwait(&jmvref->jm_list, MNT_WAIT);
7578 		return (ERESTART);
7579 	}
7580 	/*
7581 	 * When we're partially truncating a pagedep we just want to flush
7582 	 * journal entries and return.  There can not be any adds in the
7583 	 * truncated portion of the directory and newblk must remain if
7584 	 * part of the block remains.
7585 	 */
7586 	if (blkoff != 0) {
7587 		struct diradd *dap;
7588 
7589 		LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7590 			if (dap->da_offset > blkoff)
7591 				panic("cancel_pagedep: diradd %p off %d > %d",
7592 				    dap, dap->da_offset, blkoff);
7593 		for (i = 0; i < DAHASHSZ; i++)
7594 			LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7595 				if (dap->da_offset > blkoff)
7596 					panic("cancel_pagedep: diradd %p off %d > %d",
7597 					    dap, dap->da_offset, blkoff);
7598 		return (0);
7599 	}
7600 	/*
7601 	 * There should be no directory add dependencies present
7602 	 * as the directory could not be truncated until all
7603 	 * children were removed.
7604 	 */
7605 	KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7606 	    ("deallocate_dependencies: pendinghd != NULL"));
7607 	for (i = 0; i < DAHASHSZ; i++)
7608 		KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7609 		    ("deallocate_dependencies: diraddhd != NULL"));
7610 	if ((pagedep->pd_state & NEWBLOCK) != 0)
7611 		free_newdirblk(pagedep->pd_newdirblk);
7612 	if (free_pagedep(pagedep) == 0)
7613 		panic("Failed to free pagedep %p", pagedep);
7614 	return (0);
7615 }
7616 
7617 /*
7618  * Reclaim any dependency structures from a buffer that is about to
7619  * be reallocated to a new vnode. The buffer must be locked, thus,
7620  * no I/O completion operations can occur while we are manipulating
7621  * its associated dependencies. The mutex is held so that other I/O's
7622  * associated with related dependencies do not occur.
7623  */
7624 static int
7625 deallocate_dependencies(bp, freeblks, off)
7626 	struct buf *bp;
7627 	struct freeblks *freeblks;
7628 	int off;
7629 {
7630 	struct indirdep *indirdep;
7631 	struct pagedep *pagedep;
7632 	struct worklist *wk, *wkn;
7633 	struct ufsmount *ump;
7634 
7635 	ump = softdep_bp_to_mp(bp);
7636 	if (ump == NULL)
7637 		goto done;
7638 	ACQUIRE_LOCK(ump);
7639 	LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7640 		switch (wk->wk_type) {
7641 		case D_INDIRDEP:
7642 			indirdep = WK_INDIRDEP(wk);
7643 			if (bp->b_lblkno >= 0 ||
7644 			    bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7645 				panic("deallocate_dependencies: not indir");
7646 			cancel_indirdep(indirdep, bp, freeblks);
7647 			continue;
7648 
7649 		case D_PAGEDEP:
7650 			pagedep = WK_PAGEDEP(wk);
7651 			if (cancel_pagedep(pagedep, freeblks, off)) {
7652 				FREE_LOCK(ump);
7653 				return (ERESTART);
7654 			}
7655 			continue;
7656 
7657 		case D_ALLOCINDIR:
7658 			/*
7659 			 * Simply remove the allocindir, we'll find it via
7660 			 * the indirdep where we can clear pointers if
7661 			 * needed.
7662 			 */
7663 			WORKLIST_REMOVE(wk);
7664 			continue;
7665 
7666 		case D_FREEWORK:
7667 			/*
7668 			 * A truncation is waiting for the zero'd pointers
7669 			 * to be written.  It can be freed when the freeblks
7670 			 * is journaled.
7671 			 */
7672 			WORKLIST_REMOVE(wk);
7673 			wk->wk_state |= ONDEPLIST;
7674 			WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7675 			break;
7676 
7677 		case D_ALLOCDIRECT:
7678 			if (off != 0)
7679 				continue;
7680 			/* FALLTHROUGH */
7681 		default:
7682 			panic("deallocate_dependencies: Unexpected type %s",
7683 			    TYPENAME(wk->wk_type));
7684 			/* NOTREACHED */
7685 		}
7686 	}
7687 	FREE_LOCK(ump);
7688 done:
7689 	/*
7690 	 * Don't throw away this buf, we were partially truncating and
7691 	 * some deps may always remain.
7692 	 */
7693 	if (off) {
7694 		allocbuf(bp, off);
7695 		bp->b_vflags |= BV_SCANNED;
7696 		return (EBUSY);
7697 	}
7698 	bp->b_flags |= B_INVAL | B_NOCACHE;
7699 
7700 	return (0);
7701 }
7702 
7703 /*
7704  * An allocdirect is being canceled due to a truncate.  We must make sure
7705  * the journal entry is released in concert with the blkfree that releases
7706  * the storage.  Completed journal entries must not be released until the
7707  * space is no longer pointed to by the inode or in the bitmap.
7708  */
7709 static void
7710 cancel_allocdirect(adphead, adp, freeblks)
7711 	struct allocdirectlst *adphead;
7712 	struct allocdirect *adp;
7713 	struct freeblks *freeblks;
7714 {
7715 	struct freework *freework;
7716 	struct newblk *newblk;
7717 	struct worklist *wk;
7718 
7719 	TAILQ_REMOVE(adphead, adp, ad_next);
7720 	newblk = (struct newblk *)adp;
7721 	freework = NULL;
7722 	/*
7723 	 * Find the correct freework structure.
7724 	 */
7725 	LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7726 		if (wk->wk_type != D_FREEWORK)
7727 			continue;
7728 		freework = WK_FREEWORK(wk);
7729 		if (freework->fw_blkno == newblk->nb_newblkno)
7730 			break;
7731 	}
7732 	if (freework == NULL)
7733 		panic("cancel_allocdirect: Freework not found");
7734 	/*
7735 	 * If a newblk exists at all we still have the journal entry that
7736 	 * initiated the allocation so we do not need to journal the free.
7737 	 */
7738 	cancel_jfreeblk(freeblks, freework->fw_blkno);
7739 	/*
7740 	 * If the journal hasn't been written the jnewblk must be passed
7741 	 * to the call to ffs_blkfree that reclaims the space.  We accomplish
7742 	 * this by linking the journal dependency into the freework to be
7743 	 * freed when freework_freeblock() is called.  If the journal has
7744 	 * been written we can simply reclaim the journal space when the
7745 	 * freeblks work is complete.
7746 	 */
7747 	freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7748 	    &freeblks->fb_jwork);
7749 	WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7750 }
7751 
7752 /*
7753  * Cancel a new block allocation.  May be an indirect or direct block.  We
7754  * remove it from various lists and return any journal record that needs to
7755  * be resolved by the caller.
7756  *
7757  * A special consideration is made for indirects which were never pointed
7758  * at on disk and will never be found once this block is released.
7759  */
7760 static struct jnewblk *
7761 cancel_newblk(newblk, wk, wkhd)
7762 	struct newblk *newblk;
7763 	struct worklist *wk;
7764 	struct workhead *wkhd;
7765 {
7766 	struct jnewblk *jnewblk;
7767 
7768 	CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7769 
7770 	newblk->nb_state |= GOINGAWAY;
7771 	/*
7772 	 * Previously we traversed the completedhd on each indirdep
7773 	 * attached to this newblk to cancel them and gather journal
7774 	 * work.  Since we need only the oldest journal segment and
7775 	 * the lowest point on the tree will always have the oldest
7776 	 * journal segment we are free to release the segments
7777 	 * of any subordinates and may leave the indirdep list to
7778 	 * indirdep_complete() when this newblk is freed.
7779 	 */
7780 	if (newblk->nb_state & ONDEPLIST) {
7781 		newblk->nb_state &= ~ONDEPLIST;
7782 		LIST_REMOVE(newblk, nb_deps);
7783 	}
7784 	if (newblk->nb_state & ONWORKLIST)
7785 		WORKLIST_REMOVE(&newblk->nb_list);
7786 	/*
7787 	 * If the journal entry hasn't been written we save a pointer to
7788 	 * the dependency that frees it until it is written or the
7789 	 * superseding operation completes.
7790 	 */
7791 	jnewblk = newblk->nb_jnewblk;
7792 	if (jnewblk != NULL && wk != NULL) {
7793 		newblk->nb_jnewblk = NULL;
7794 		jnewblk->jn_dep = wk;
7795 	}
7796 	if (!LIST_EMPTY(&newblk->nb_jwork))
7797 		jwork_move(wkhd, &newblk->nb_jwork);
7798 	/*
7799 	 * When truncating we must free the newdirblk early to remove
7800 	 * the pagedep from the hash before returning.
7801 	 */
7802 	if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7803 		free_newdirblk(WK_NEWDIRBLK(wk));
7804 	if (!LIST_EMPTY(&newblk->nb_newdirblk))
7805 		panic("cancel_newblk: extra newdirblk");
7806 
7807 	return (jnewblk);
7808 }
7809 
7810 /*
7811  * Schedule the freefrag associated with a newblk to be released once
7812  * the pointers are written and the previous block is no longer needed.
7813  */
7814 static void
7815 newblk_freefrag(newblk)
7816 	struct newblk *newblk;
7817 {
7818 	struct freefrag *freefrag;
7819 
7820 	if (newblk->nb_freefrag == NULL)
7821 		return;
7822 	freefrag = newblk->nb_freefrag;
7823 	newblk->nb_freefrag = NULL;
7824 	freefrag->ff_state |= COMPLETE;
7825 	if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7826 		add_to_worklist(&freefrag->ff_list, 0);
7827 }
7828 
7829 /*
7830  * Free a newblk. Generate a new freefrag work request if appropriate.
7831  * This must be called after the inode pointer and any direct block pointers
7832  * are valid or fully removed via truncate or frag extension.
7833  */
7834 static void
7835 free_newblk(newblk)
7836 	struct newblk *newblk;
7837 {
7838 	struct indirdep *indirdep;
7839 	struct worklist *wk;
7840 
7841 	KASSERT(newblk->nb_jnewblk == NULL,
7842 	    ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7843 	KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7844 	    ("free_newblk: unclaimed newblk"));
7845 	LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7846 	newblk_freefrag(newblk);
7847 	if (newblk->nb_state & ONDEPLIST)
7848 		LIST_REMOVE(newblk, nb_deps);
7849 	if (newblk->nb_state & ONWORKLIST)
7850 		WORKLIST_REMOVE(&newblk->nb_list);
7851 	LIST_REMOVE(newblk, nb_hash);
7852 	if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7853 		free_newdirblk(WK_NEWDIRBLK(wk));
7854 	if (!LIST_EMPTY(&newblk->nb_newdirblk))
7855 		panic("free_newblk: extra newdirblk");
7856 	while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7857 		indirdep_complete(indirdep);
7858 	handle_jwork(&newblk->nb_jwork);
7859 	WORKITEM_FREE(newblk, D_NEWBLK);
7860 }
7861 
7862 /*
7863  * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7864  */
7865 static void
7866 free_newdirblk(newdirblk)
7867 	struct newdirblk *newdirblk;
7868 {
7869 	struct pagedep *pagedep;
7870 	struct diradd *dap;
7871 	struct worklist *wk;
7872 
7873 	LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7874 	WORKLIST_REMOVE(&newdirblk->db_list);
7875 	/*
7876 	 * If the pagedep is still linked onto the directory buffer
7877 	 * dependency chain, then some of the entries on the
7878 	 * pd_pendinghd list may not be committed to disk yet. In
7879 	 * this case, we will simply clear the NEWBLOCK flag and
7880 	 * let the pd_pendinghd list be processed when the pagedep
7881 	 * is next written. If the pagedep is no longer on the buffer
7882 	 * dependency chain, then all the entries on the pd_pending
7883 	 * list are committed to disk and we can free them here.
7884 	 */
7885 	pagedep = newdirblk->db_pagedep;
7886 	pagedep->pd_state &= ~NEWBLOCK;
7887 	if ((pagedep->pd_state & ONWORKLIST) == 0) {
7888 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7889 			free_diradd(dap, NULL);
7890 		/*
7891 		 * If no dependencies remain, the pagedep will be freed.
7892 		 */
7893 		free_pagedep(pagedep);
7894 	}
7895 	/* Should only ever be one item in the list. */
7896 	while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7897 		WORKLIST_REMOVE(wk);
7898 		handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7899 	}
7900 	WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7901 }
7902 
7903 /*
7904  * Prepare an inode to be freed. The actual free operation is not
7905  * done until the zero'ed inode has been written to disk.
7906  */
7907 void
7908 softdep_freefile(pvp, ino, mode)
7909 	struct vnode *pvp;
7910 	ino_t ino;
7911 	int mode;
7912 {
7913 	struct inode *ip = VTOI(pvp);
7914 	struct inodedep *inodedep;
7915 	struct freefile *freefile;
7916 	struct freeblks *freeblks;
7917 	struct ufsmount *ump;
7918 
7919 	ump = ITOUMP(ip);
7920 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7921 	    ("softdep_freefile called on non-softdep filesystem"));
7922 	/*
7923 	 * This sets up the inode de-allocation dependency.
7924 	 */
7925 	freefile = malloc(sizeof(struct freefile),
7926 		M_FREEFILE, M_SOFTDEP_FLAGS);
7927 	workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7928 	freefile->fx_mode = mode;
7929 	freefile->fx_oldinum = ino;
7930 	freefile->fx_devvp = ump->um_devvp;
7931 	LIST_INIT(&freefile->fx_jwork);
7932 	UFS_LOCK(ump);
7933 	ump->um_fs->fs_pendinginodes += 1;
7934 	UFS_UNLOCK(ump);
7935 
7936 	/*
7937 	 * If the inodedep does not exist, then the zero'ed inode has
7938 	 * been written to disk. If the allocated inode has never been
7939 	 * written to disk, then the on-disk inode is zero'ed. In either
7940 	 * case we can free the file immediately.  If the journal was
7941 	 * canceled before being written the inode will never make it to
7942 	 * disk and we must send the canceled journal entrys to
7943 	 * ffs_freefile() to be cleared in conjunction with the bitmap.
7944 	 * Any blocks waiting on the inode to write can be safely freed
7945 	 * here as it will never been written.
7946 	 */
7947 	ACQUIRE_LOCK(ump);
7948 	inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7949 	if (inodedep) {
7950 		/*
7951 		 * Clear out freeblks that no longer need to reference
7952 		 * this inode.
7953 		 */
7954 		while ((freeblks =
7955 		    TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7956 			TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7957 			    fb_next);
7958 			freeblks->fb_state &= ~ONDEPLIST;
7959 		}
7960 		/*
7961 		 * Remove this inode from the unlinked list.
7962 		 */
7963 		if (inodedep->id_state & UNLINKED) {
7964 			/*
7965 			 * Save the journal work to be freed with the bitmap
7966 			 * before we clear UNLINKED.  Otherwise it can be lost
7967 			 * if the inode block is written.
7968 			 */
7969 			handle_bufwait(inodedep, &freefile->fx_jwork);
7970 			clear_unlinked_inodedep(inodedep);
7971 			/*
7972 			 * Re-acquire inodedep as we've dropped the
7973 			 * per-filesystem lock in clear_unlinked_inodedep().
7974 			 */
7975 			inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7976 		}
7977 	}
7978 	if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7979 		FREE_LOCK(ump);
7980 		handle_workitem_freefile(freefile);
7981 		return;
7982 	}
7983 	if ((inodedep->id_state & DEPCOMPLETE) == 0)
7984 		inodedep->id_state |= GOINGAWAY;
7985 	WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7986 	FREE_LOCK(ump);
7987 	if (ip->i_number == ino)
7988 		UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
7989 }
7990 
7991 /*
7992  * Check to see if an inode has never been written to disk. If
7993  * so free the inodedep and return success, otherwise return failure.
7994  *
7995  * If we still have a bitmap dependency, then the inode has never
7996  * been written to disk. Drop the dependency as it is no longer
7997  * necessary since the inode is being deallocated. We set the
7998  * ALLCOMPLETE flags since the bitmap now properly shows that the
7999  * inode is not allocated. Even if the inode is actively being
8000  * written, it has been rolled back to its zero'ed state, so we
8001  * are ensured that a zero inode is what is on the disk. For short
8002  * lived files, this change will usually result in removing all the
8003  * dependencies from the inode so that it can be freed immediately.
8004  */
8005 static int
8006 check_inode_unwritten(inodedep)
8007 	struct inodedep *inodedep;
8008 {
8009 
8010 	LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8011 
8012 	if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
8013 	    !LIST_EMPTY(&inodedep->id_dirremhd) ||
8014 	    !LIST_EMPTY(&inodedep->id_pendinghd) ||
8015 	    !LIST_EMPTY(&inodedep->id_bufwait) ||
8016 	    !LIST_EMPTY(&inodedep->id_inowait) ||
8017 	    !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
8018 	    !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
8019 	    !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
8020 	    !TAILQ_EMPTY(&inodedep->id_extupdt) ||
8021 	    !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
8022 	    !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
8023 	    inodedep->id_mkdiradd != NULL ||
8024 	    inodedep->id_nlinkdelta != 0)
8025 		return (0);
8026 	/*
8027 	 * Another process might be in initiate_write_inodeblock_ufs[12]
8028 	 * trying to allocate memory without holding "Softdep Lock".
8029 	 */
8030 	if ((inodedep->id_state & IOSTARTED) != 0 &&
8031 	    inodedep->id_savedino1 == NULL)
8032 		return (0);
8033 
8034 	if (inodedep->id_state & ONDEPLIST)
8035 		LIST_REMOVE(inodedep, id_deps);
8036 	inodedep->id_state &= ~ONDEPLIST;
8037 	inodedep->id_state |= ALLCOMPLETE;
8038 	inodedep->id_bmsafemap = NULL;
8039 	if (inodedep->id_state & ONWORKLIST)
8040 		WORKLIST_REMOVE(&inodedep->id_list);
8041 	if (inodedep->id_savedino1 != NULL) {
8042 		free(inodedep->id_savedino1, M_SAVEDINO);
8043 		inodedep->id_savedino1 = NULL;
8044 	}
8045 	if (free_inodedep(inodedep) == 0)
8046 		panic("check_inode_unwritten: busy inode");
8047 	return (1);
8048 }
8049 
8050 static int
8051 check_inodedep_free(inodedep)
8052 	struct inodedep *inodedep;
8053 {
8054 
8055 	LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8056 	if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
8057 	    !LIST_EMPTY(&inodedep->id_dirremhd) ||
8058 	    !LIST_EMPTY(&inodedep->id_pendinghd) ||
8059 	    !LIST_EMPTY(&inodedep->id_bufwait) ||
8060 	    !LIST_EMPTY(&inodedep->id_inowait) ||
8061 	    !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
8062 	    !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
8063 	    !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
8064 	    !TAILQ_EMPTY(&inodedep->id_extupdt) ||
8065 	    !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
8066 	    !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
8067 	    inodedep->id_mkdiradd != NULL ||
8068 	    inodedep->id_nlinkdelta != 0 ||
8069 	    inodedep->id_savedino1 != NULL)
8070 		return (0);
8071 	return (1);
8072 }
8073 
8074 /*
8075  * Try to free an inodedep structure. Return 1 if it could be freed.
8076  */
8077 static int
8078 free_inodedep(inodedep)
8079 	struct inodedep *inodedep;
8080 {
8081 
8082 	LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8083 	if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
8084 	    !check_inodedep_free(inodedep))
8085 		return (0);
8086 	if (inodedep->id_state & ONDEPLIST)
8087 		LIST_REMOVE(inodedep, id_deps);
8088 	LIST_REMOVE(inodedep, id_hash);
8089 	WORKITEM_FREE(inodedep, D_INODEDEP);
8090 	return (1);
8091 }
8092 
8093 /*
8094  * Free the block referenced by a freework structure.  The parent freeblks
8095  * structure is released and completed when the final cg bitmap reaches
8096  * the disk.  This routine may be freeing a jnewblk which never made it to
8097  * disk in which case we do not have to wait as the operation is undone
8098  * in memory immediately.
8099  */
8100 static void
8101 freework_freeblock(freework, key)
8102 	struct freework *freework;
8103 	u_long key;
8104 {
8105 	struct freeblks *freeblks;
8106 	struct jnewblk *jnewblk;
8107 	struct ufsmount *ump;
8108 	struct workhead wkhd;
8109 	struct fs *fs;
8110 	int bsize;
8111 	int needj;
8112 
8113 	ump = VFSTOUFS(freework->fw_list.wk_mp);
8114 	LOCK_OWNED(ump);
8115 	/*
8116 	 * Handle partial truncate separately.
8117 	 */
8118 	if (freework->fw_indir) {
8119 		complete_trunc_indir(freework);
8120 		return;
8121 	}
8122 	freeblks = freework->fw_freeblks;
8123 	fs = ump->um_fs;
8124 	needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
8125 	bsize = lfragtosize(fs, freework->fw_frags);
8126 	LIST_INIT(&wkhd);
8127 	/*
8128 	 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
8129 	 * on the indirblk hashtable and prevents premature freeing.
8130 	 */
8131 	freework->fw_state |= DEPCOMPLETE;
8132 	/*
8133 	 * SUJ needs to wait for the segment referencing freed indirect
8134 	 * blocks to expire so that we know the checker will not confuse
8135 	 * a re-allocated indirect block with its old contents.
8136 	 */
8137 	if (needj && freework->fw_lbn <= -UFS_NDADDR)
8138 		indirblk_insert(freework);
8139 	/*
8140 	 * If we are canceling an existing jnewblk pass it to the free
8141 	 * routine, otherwise pass the freeblk which will ultimately
8142 	 * release the freeblks.  If we're not journaling, we can just
8143 	 * free the freeblks immediately.
8144 	 */
8145 	jnewblk = freework->fw_jnewblk;
8146 	if (jnewblk != NULL) {
8147 		cancel_jnewblk(jnewblk, &wkhd);
8148 		needj = 0;
8149 	} else if (needj) {
8150 		freework->fw_state |= DELAYEDFREE;
8151 		freeblks->fb_cgwait++;
8152 		WORKLIST_INSERT(&wkhd, &freework->fw_list);
8153 	}
8154 	FREE_LOCK(ump);
8155 	freeblks_free(ump, freeblks, btodb(bsize));
8156 	CTR4(KTR_SUJ,
8157 	    "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
8158 	    freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
8159 	ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
8160 	    freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
8161 	ACQUIRE_LOCK(ump);
8162 	/*
8163 	 * The jnewblk will be discarded and the bits in the map never
8164 	 * made it to disk.  We can immediately free the freeblk.
8165 	 */
8166 	if (needj == 0)
8167 		handle_written_freework(freework);
8168 }
8169 
8170 /*
8171  * We enqueue freework items that need processing back on the freeblks and
8172  * add the freeblks to the worklist.  This makes it easier to find all work
8173  * required to flush a truncation in process_truncates().
8174  */
8175 static void
8176 freework_enqueue(freework)
8177 	struct freework *freework;
8178 {
8179 	struct freeblks *freeblks;
8180 
8181 	freeblks = freework->fw_freeblks;
8182 	if ((freework->fw_state & INPROGRESS) == 0)
8183 		WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
8184 	if ((freeblks->fb_state &
8185 	    (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
8186 	    LIST_EMPTY(&freeblks->fb_jblkdephd))
8187 		add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8188 }
8189 
8190 /*
8191  * Start, continue, or finish the process of freeing an indirect block tree.
8192  * The free operation may be paused at any point with fw_off containing the
8193  * offset to restart from.  This enables us to implement some flow control
8194  * for large truncates which may fan out and generate a huge number of
8195  * dependencies.
8196  */
8197 static void
8198 handle_workitem_indirblk(freework)
8199 	struct freework *freework;
8200 {
8201 	struct freeblks *freeblks;
8202 	struct ufsmount *ump;
8203 	struct fs *fs;
8204 
8205 	freeblks = freework->fw_freeblks;
8206 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8207 	fs = ump->um_fs;
8208 	if (freework->fw_state & DEPCOMPLETE) {
8209 		handle_written_freework(freework);
8210 		return;
8211 	}
8212 	if (freework->fw_off == NINDIR(fs)) {
8213 		freework_freeblock(freework, SINGLETON_KEY);
8214 		return;
8215 	}
8216 	freework->fw_state |= INPROGRESS;
8217 	FREE_LOCK(ump);
8218 	indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
8219 	    freework->fw_lbn);
8220 	ACQUIRE_LOCK(ump);
8221 }
8222 
8223 /*
8224  * Called when a freework structure attached to a cg buf is written.  The
8225  * ref on either the parent or the freeblks structure is released and
8226  * the freeblks is added back to the worklist if there is more work to do.
8227  */
8228 static void
8229 handle_written_freework(freework)
8230 	struct freework *freework;
8231 {
8232 	struct freeblks *freeblks;
8233 	struct freework *parent;
8234 
8235 	freeblks = freework->fw_freeblks;
8236 	parent = freework->fw_parent;
8237 	if (freework->fw_state & DELAYEDFREE)
8238 		freeblks->fb_cgwait--;
8239 	freework->fw_state |= COMPLETE;
8240 	if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
8241 		WORKITEM_FREE(freework, D_FREEWORK);
8242 	if (parent) {
8243 		if (--parent->fw_ref == 0)
8244 			freework_enqueue(parent);
8245 		return;
8246 	}
8247 	if (--freeblks->fb_ref != 0)
8248 		return;
8249 	if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
8250 	    ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
8251 		add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8252 }
8253 
8254 /*
8255  * This workitem routine performs the block de-allocation.
8256  * The workitem is added to the pending list after the updated
8257  * inode block has been written to disk.  As mentioned above,
8258  * checks regarding the number of blocks de-allocated (compared
8259  * to the number of blocks allocated for the file) are also
8260  * performed in this function.
8261  */
8262 static int
8263 handle_workitem_freeblocks(freeblks, flags)
8264 	struct freeblks *freeblks;
8265 	int flags;
8266 {
8267 	struct freework *freework;
8268 	struct newblk *newblk;
8269 	struct allocindir *aip;
8270 	struct ufsmount *ump;
8271 	struct worklist *wk;
8272 	u_long key;
8273 
8274 	KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
8275 	    ("handle_workitem_freeblocks: Journal entries not written."));
8276 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8277 	key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8278 	ACQUIRE_LOCK(ump);
8279 	while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
8280 		WORKLIST_REMOVE(wk);
8281 		switch (wk->wk_type) {
8282 		case D_DIRREM:
8283 			wk->wk_state |= COMPLETE;
8284 			add_to_worklist(wk, 0);
8285 			continue;
8286 
8287 		case D_ALLOCDIRECT:
8288 			free_newblk(WK_NEWBLK(wk));
8289 			continue;
8290 
8291 		case D_ALLOCINDIR:
8292 			aip = WK_ALLOCINDIR(wk);
8293 			freework = NULL;
8294 			if (aip->ai_state & DELAYEDFREE) {
8295 				FREE_LOCK(ump);
8296 				freework = newfreework(ump, freeblks, NULL,
8297 				    aip->ai_lbn, aip->ai_newblkno,
8298 				    ump->um_fs->fs_frag, 0, 0);
8299 				ACQUIRE_LOCK(ump);
8300 			}
8301 			newblk = WK_NEWBLK(wk);
8302 			if (newblk->nb_jnewblk) {
8303 				freework->fw_jnewblk = newblk->nb_jnewblk;
8304 				newblk->nb_jnewblk->jn_dep = &freework->fw_list;
8305 				newblk->nb_jnewblk = NULL;
8306 			}
8307 			free_newblk(newblk);
8308 			continue;
8309 
8310 		case D_FREEWORK:
8311 			freework = WK_FREEWORK(wk);
8312 			if (freework->fw_lbn <= -UFS_NDADDR)
8313 				handle_workitem_indirblk(freework);
8314 			else
8315 				freework_freeblock(freework, key);
8316 			continue;
8317 		default:
8318 			panic("handle_workitem_freeblocks: Unknown type %s",
8319 			    TYPENAME(wk->wk_type));
8320 		}
8321 	}
8322 	if (freeblks->fb_ref != 0) {
8323 		freeblks->fb_state &= ~INPROGRESS;
8324 		wake_worklist(&freeblks->fb_list);
8325 		freeblks = NULL;
8326 	}
8327 	FREE_LOCK(ump);
8328 	ffs_blkrelease_finish(ump, key);
8329 	if (freeblks)
8330 		return handle_complete_freeblocks(freeblks, flags);
8331 	return (0);
8332 }
8333 
8334 /*
8335  * Handle completion of block free via truncate.  This allows fs_pending
8336  * to track the actual free block count more closely than if we only updated
8337  * it at the end.  We must be careful to handle cases where the block count
8338  * on free was incorrect.
8339  */
8340 static void
8341 freeblks_free(ump, freeblks, blocks)
8342 	struct ufsmount *ump;
8343 	struct freeblks *freeblks;
8344 	int blocks;
8345 {
8346 	struct fs *fs;
8347 	ufs2_daddr_t remain;
8348 
8349 	UFS_LOCK(ump);
8350 	remain = -freeblks->fb_chkcnt;
8351 	freeblks->fb_chkcnt += blocks;
8352 	if (remain > 0) {
8353 		if (remain < blocks)
8354 			blocks = remain;
8355 		fs = ump->um_fs;
8356 		fs->fs_pendingblocks -= blocks;
8357 	}
8358 	UFS_UNLOCK(ump);
8359 }
8360 
8361 /*
8362  * Once all of the freework workitems are complete we can retire the
8363  * freeblocks dependency and any journal work awaiting completion.  This
8364  * can not be called until all other dependencies are stable on disk.
8365  */
8366 static int
8367 handle_complete_freeblocks(freeblks, flags)
8368 	struct freeblks *freeblks;
8369 	int flags;
8370 {
8371 	struct inodedep *inodedep;
8372 	struct inode *ip;
8373 	struct vnode *vp;
8374 	struct fs *fs;
8375 	struct ufsmount *ump;
8376 	ufs2_daddr_t spare;
8377 
8378 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8379 	fs = ump->um_fs;
8380 	flags = LK_EXCLUSIVE | flags;
8381 	spare = freeblks->fb_chkcnt;
8382 
8383 	/*
8384 	 * If we did not release the expected number of blocks we may have
8385 	 * to adjust the inode block count here.  Only do so if it wasn't
8386 	 * a truncation to zero and the modrev still matches.
8387 	 */
8388 	if (spare && freeblks->fb_len != 0) {
8389 		if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8390 		    flags, &vp, FFSV_FORCEINSMQ) != 0)
8391 			return (EBUSY);
8392 		ip = VTOI(vp);
8393 		if (ip->i_mode == 0) {
8394 			vgone(vp);
8395 		} else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8396 			DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8397 			UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8398 			/*
8399 			 * We must wait so this happens before the
8400 			 * journal is reclaimed.
8401 			 */
8402 			ffs_update(vp, 1);
8403 		}
8404 		vput(vp);
8405 	}
8406 	if (spare < 0) {
8407 		UFS_LOCK(ump);
8408 		fs->fs_pendingblocks += spare;
8409 		UFS_UNLOCK(ump);
8410 	}
8411 #ifdef QUOTA
8412 	/* Handle spare. */
8413 	if (spare)
8414 		quotaadj(freeblks->fb_quota, ump, -spare);
8415 	quotarele(freeblks->fb_quota);
8416 #endif
8417 	ACQUIRE_LOCK(ump);
8418 	if (freeblks->fb_state & ONDEPLIST) {
8419 		inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8420 		    0, &inodedep);
8421 		TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8422 		freeblks->fb_state &= ~ONDEPLIST;
8423 		if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8424 			free_inodedep(inodedep);
8425 	}
8426 	/*
8427 	 * All of the freeblock deps must be complete prior to this call
8428 	 * so it's now safe to complete earlier outstanding journal entries.
8429 	 */
8430 	handle_jwork(&freeblks->fb_jwork);
8431 	WORKITEM_FREE(freeblks, D_FREEBLKS);
8432 	FREE_LOCK(ump);
8433 	return (0);
8434 }
8435 
8436 /*
8437  * Release blocks associated with the freeblks and stored in the indirect
8438  * block dbn. If level is greater than SINGLE, the block is an indirect block
8439  * and recursive calls to indirtrunc must be used to cleanse other indirect
8440  * blocks.
8441  *
8442  * This handles partial and complete truncation of blocks.  Partial is noted
8443  * with goingaway == 0.  In this case the freework is completed after the
8444  * zero'd indirects are written to disk.  For full truncation the freework
8445  * is completed after the block is freed.
8446  */
8447 static void
8448 indir_trunc(freework, dbn, lbn)
8449 	struct freework *freework;
8450 	ufs2_daddr_t dbn;
8451 	ufs_lbn_t lbn;
8452 {
8453 	struct freework *nfreework;
8454 	struct workhead wkhd;
8455 	struct freeblks *freeblks;
8456 	struct buf *bp;
8457 	struct fs *fs;
8458 	struct indirdep *indirdep;
8459 	struct mount *mp;
8460 	struct ufsmount *ump;
8461 	ufs1_daddr_t *bap1;
8462 	ufs2_daddr_t nb, nnb, *bap2;
8463 	ufs_lbn_t lbnadd, nlbn;
8464 	u_long key;
8465 	int nblocks, ufs1fmt, freedblocks;
8466 	int goingaway, freedeps, needj, level, cnt, i, error;
8467 
8468 	freeblks = freework->fw_freeblks;
8469 	mp = freeblks->fb_list.wk_mp;
8470 	ump = VFSTOUFS(mp);
8471 	fs = ump->um_fs;
8472 	/*
8473 	 * Get buffer of block pointers to be freed.  There are three cases:
8474 	 *
8475 	 * 1) Partial truncate caches the indirdep pointer in the freework
8476 	 *    which provides us a back copy to the save bp which holds the
8477 	 *    pointers we want to clear.  When this completes the zero
8478 	 *    pointers are written to the real copy.
8479 	 * 2) The indirect is being completely truncated, cancel_indirdep()
8480 	 *    eliminated the real copy and placed the indirdep on the saved
8481 	 *    copy.  The indirdep and buf are discarded when this completes.
8482 	 * 3) The indirect was not in memory, we read a copy off of the disk
8483 	 *    using the devvp and drop and invalidate the buffer when we're
8484 	 *    done.
8485 	 */
8486 	goingaway = 1;
8487 	indirdep = NULL;
8488 	if (freework->fw_indir != NULL) {
8489 		goingaway = 0;
8490 		indirdep = freework->fw_indir;
8491 		bp = indirdep->ir_savebp;
8492 		if (bp == NULL || bp->b_blkno != dbn)
8493 			panic("indir_trunc: Bad saved buf %p blkno %jd",
8494 			    bp, (intmax_t)dbn);
8495 	} else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8496 		/*
8497 		 * The lock prevents the buf dep list from changing and
8498 	 	 * indirects on devvp should only ever have one dependency.
8499 		 */
8500 		indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8501 		if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8502 			panic("indir_trunc: Bad indirdep %p from buf %p",
8503 			    indirdep, bp);
8504 	} else {
8505 		error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn,
8506 		    (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
8507 		if (error)
8508 			return;
8509 	}
8510 	ACQUIRE_LOCK(ump);
8511 	/* Protects against a race with complete_trunc_indir(). */
8512 	freework->fw_state &= ~INPROGRESS;
8513 	/*
8514 	 * If we have an indirdep we need to enforce the truncation order
8515 	 * and discard it when it is complete.
8516 	 */
8517 	if (indirdep) {
8518 		if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8519 		    !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8520 			/*
8521 			 * Add the complete truncate to the list on the
8522 			 * indirdep to enforce in-order processing.
8523 			 */
8524 			if (freework->fw_indir == NULL)
8525 				TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8526 				    freework, fw_next);
8527 			FREE_LOCK(ump);
8528 			return;
8529 		}
8530 		/*
8531 		 * If we're goingaway, free the indirdep.  Otherwise it will
8532 		 * linger until the write completes.
8533 		 */
8534 		if (goingaway) {
8535 			KASSERT(indirdep->ir_savebp == bp,
8536 			    ("indir_trunc: losing ir_savebp %p",
8537 			    indirdep->ir_savebp));
8538 			indirdep->ir_savebp = NULL;
8539 			free_indirdep(indirdep);
8540 		}
8541 	}
8542 	FREE_LOCK(ump);
8543 	/* Initialize pointers depending on block size. */
8544 	if (ump->um_fstype == UFS1) {
8545 		bap1 = (ufs1_daddr_t *)bp->b_data;
8546 		nb = bap1[freework->fw_off];
8547 		ufs1fmt = 1;
8548 		bap2 = NULL;
8549 	} else {
8550 		bap2 = (ufs2_daddr_t *)bp->b_data;
8551 		nb = bap2[freework->fw_off];
8552 		ufs1fmt = 0;
8553 		bap1 = NULL;
8554 	}
8555 	level = lbn_level(lbn);
8556 	needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8557 	lbnadd = lbn_offset(fs, level);
8558 	nblocks = btodb(fs->fs_bsize);
8559 	nfreework = freework;
8560 	freedeps = 0;
8561 	cnt = 0;
8562 	/*
8563 	 * Reclaim blocks.  Traverses into nested indirect levels and
8564 	 * arranges for the current level to be freed when subordinates
8565 	 * are free when journaling.
8566 	 */
8567 	key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8568 	for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8569 		if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8570 		    fs->fs_bsize) != 0)
8571 			nb = 0;
8572 		if (i != NINDIR(fs) - 1) {
8573 			if (ufs1fmt)
8574 				nnb = bap1[i+1];
8575 			else
8576 				nnb = bap2[i+1];
8577 		} else
8578 			nnb = 0;
8579 		if (nb == 0)
8580 			continue;
8581 		cnt++;
8582 		if (level != 0) {
8583 			nlbn = (lbn + 1) - (i * lbnadd);
8584 			if (needj != 0) {
8585 				nfreework = newfreework(ump, freeblks, freework,
8586 				    nlbn, nb, fs->fs_frag, 0, 0);
8587 				freedeps++;
8588 			}
8589 			indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8590 		} else {
8591 			struct freedep *freedep;
8592 
8593 			/*
8594 			 * Attempt to aggregate freedep dependencies for
8595 			 * all blocks being released to the same CG.
8596 			 */
8597 			LIST_INIT(&wkhd);
8598 			if (needj != 0 &&
8599 			    (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8600 				freedep = newfreedep(freework);
8601 				WORKLIST_INSERT_UNLOCKED(&wkhd,
8602 				    &freedep->fd_list);
8603 				freedeps++;
8604 			}
8605 			CTR3(KTR_SUJ,
8606 			    "indir_trunc: ino %jd blkno %jd size %d",
8607 			    freeblks->fb_inum, nb, fs->fs_bsize);
8608 			ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8609 			    fs->fs_bsize, freeblks->fb_inum,
8610 			    freeblks->fb_vtype, &wkhd, key);
8611 		}
8612 	}
8613 	ffs_blkrelease_finish(ump, key);
8614 	if (goingaway) {
8615 		bp->b_flags |= B_INVAL | B_NOCACHE;
8616 		brelse(bp);
8617 	}
8618 	freedblocks = 0;
8619 	if (level == 0)
8620 		freedblocks = (nblocks * cnt);
8621 	if (needj == 0)
8622 		freedblocks += nblocks;
8623 	freeblks_free(ump, freeblks, freedblocks);
8624 	/*
8625 	 * If we are journaling set up the ref counts and offset so this
8626 	 * indirect can be completed when its children are free.
8627 	 */
8628 	if (needj) {
8629 		ACQUIRE_LOCK(ump);
8630 		freework->fw_off = i;
8631 		freework->fw_ref += freedeps;
8632 		freework->fw_ref -= NINDIR(fs) + 1;
8633 		if (level == 0)
8634 			freeblks->fb_cgwait += freedeps;
8635 		if (freework->fw_ref == 0)
8636 			freework_freeblock(freework, SINGLETON_KEY);
8637 		FREE_LOCK(ump);
8638 		return;
8639 	}
8640 	/*
8641 	 * If we're not journaling we can free the indirect now.
8642 	 */
8643 	dbn = dbtofsb(fs, dbn);
8644 	CTR3(KTR_SUJ,
8645 	    "indir_trunc 2: ino %jd blkno %jd size %d",
8646 	    freeblks->fb_inum, dbn, fs->fs_bsize);
8647 	ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8648 	    freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8649 	/* Non SUJ softdep does single-threaded truncations. */
8650 	if (freework->fw_blkno == dbn) {
8651 		freework->fw_state |= ALLCOMPLETE;
8652 		ACQUIRE_LOCK(ump);
8653 		handle_written_freework(freework);
8654 		FREE_LOCK(ump);
8655 	}
8656 	return;
8657 }
8658 
8659 /*
8660  * Cancel an allocindir when it is removed via truncation.  When bp is not
8661  * NULL the indirect never appeared on disk and is scheduled to be freed
8662  * independently of the indir so we can more easily track journal work.
8663  */
8664 static void
8665 cancel_allocindir(aip, bp, freeblks, trunc)
8666 	struct allocindir *aip;
8667 	struct buf *bp;
8668 	struct freeblks *freeblks;
8669 	int trunc;
8670 {
8671 	struct indirdep *indirdep;
8672 	struct freefrag *freefrag;
8673 	struct newblk *newblk;
8674 
8675 	newblk = (struct newblk *)aip;
8676 	LIST_REMOVE(aip, ai_next);
8677 	/*
8678 	 * We must eliminate the pointer in bp if it must be freed on its
8679 	 * own due to partial truncate or pending journal work.
8680 	 */
8681 	if (bp && (trunc || newblk->nb_jnewblk)) {
8682 		/*
8683 		 * Clear the pointer and mark the aip to be freed
8684 		 * directly if it never existed on disk.
8685 		 */
8686 		aip->ai_state |= DELAYEDFREE;
8687 		indirdep = aip->ai_indirdep;
8688 		if (indirdep->ir_state & UFS1FMT)
8689 			((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8690 		else
8691 			((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8692 	}
8693 	/*
8694 	 * When truncating the previous pointer will be freed via
8695 	 * savedbp.  Eliminate the freefrag which would dup free.
8696 	 */
8697 	if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8698 		newblk->nb_freefrag = NULL;
8699 		if (freefrag->ff_jdep)
8700 			cancel_jfreefrag(
8701 			    WK_JFREEFRAG(freefrag->ff_jdep));
8702 		jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8703 		WORKITEM_FREE(freefrag, D_FREEFRAG);
8704 	}
8705 	/*
8706 	 * If the journal hasn't been written the jnewblk must be passed
8707 	 * to the call to ffs_blkfree that reclaims the space.  We accomplish
8708 	 * this by leaving the journal dependency on the newblk to be freed
8709 	 * when a freework is created in handle_workitem_freeblocks().
8710 	 */
8711 	cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8712 	WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8713 }
8714 
8715 /*
8716  * Create the mkdir dependencies for . and .. in a new directory.  Link them
8717  * in to a newdirblk so any subsequent additions are tracked properly.  The
8718  * caller is responsible for adding the mkdir1 dependency to the journal
8719  * and updating id_mkdiradd.  This function returns with the per-filesystem
8720  * lock held.
8721  */
8722 static struct mkdir *
8723 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8724 	struct diradd *dap;
8725 	ino_t newinum;
8726 	ino_t dinum;
8727 	struct buf *newdirbp;
8728 	struct mkdir **mkdirp;
8729 {
8730 	struct newblk *newblk;
8731 	struct pagedep *pagedep;
8732 	struct inodedep *inodedep;
8733 	struct newdirblk *newdirblk;
8734 	struct mkdir *mkdir1, *mkdir2;
8735 	struct worklist *wk;
8736 	struct jaddref *jaddref;
8737 	struct ufsmount *ump;
8738 	struct mount *mp;
8739 
8740 	mp = dap->da_list.wk_mp;
8741 	ump = VFSTOUFS(mp);
8742 	newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8743 	    M_SOFTDEP_FLAGS);
8744 	workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8745 	LIST_INIT(&newdirblk->db_mkdir);
8746 	mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8747 	workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8748 	mkdir1->md_state = ATTACHED | MKDIR_BODY;
8749 	mkdir1->md_diradd = dap;
8750 	mkdir1->md_jaddref = NULL;
8751 	mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8752 	workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8753 	mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8754 	mkdir2->md_diradd = dap;
8755 	mkdir2->md_jaddref = NULL;
8756 	if (MOUNTEDSUJ(mp) == 0) {
8757 		mkdir1->md_state |= DEPCOMPLETE;
8758 		mkdir2->md_state |= DEPCOMPLETE;
8759 	}
8760 	/*
8761 	 * Dependency on "." and ".." being written to disk.
8762 	 */
8763 	mkdir1->md_buf = newdirbp;
8764 	ACQUIRE_LOCK(VFSTOUFS(mp));
8765 	LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8766 	/*
8767 	 * We must link the pagedep, allocdirect, and newdirblk for
8768 	 * the initial file page so the pointer to the new directory
8769 	 * is not written until the directory contents are live and
8770 	 * any subsequent additions are not marked live until the
8771 	 * block is reachable via the inode.
8772 	 */
8773 	if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8774 		panic("setup_newdir: lost pagedep");
8775 	LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8776 		if (wk->wk_type == D_ALLOCDIRECT)
8777 			break;
8778 	if (wk == NULL)
8779 		panic("setup_newdir: lost allocdirect");
8780 	if (pagedep->pd_state & NEWBLOCK)
8781 		panic("setup_newdir: NEWBLOCK already set");
8782 	newblk = WK_NEWBLK(wk);
8783 	pagedep->pd_state |= NEWBLOCK;
8784 	pagedep->pd_newdirblk = newdirblk;
8785 	newdirblk->db_pagedep = pagedep;
8786 	WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8787 	WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8788 	/*
8789 	 * Look up the inodedep for the parent directory so that we
8790 	 * can link mkdir2 into the pending dotdot jaddref or
8791 	 * the inode write if there is none.  If the inode is
8792 	 * ALLCOMPLETE and no jaddref is present all dependencies have
8793 	 * been satisfied and mkdir2 can be freed.
8794 	 */
8795 	inodedep_lookup(mp, dinum, 0, &inodedep);
8796 	if (MOUNTEDSUJ(mp)) {
8797 		if (inodedep == NULL)
8798 			panic("setup_newdir: Lost parent.");
8799 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8800 		    inoreflst);
8801 		KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8802 		    (jaddref->ja_state & MKDIR_PARENT),
8803 		    ("setup_newdir: bad dotdot jaddref %p", jaddref));
8804 		LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8805 		mkdir2->md_jaddref = jaddref;
8806 		jaddref->ja_mkdir = mkdir2;
8807 	} else if (inodedep == NULL ||
8808 	    (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8809 		dap->da_state &= ~MKDIR_PARENT;
8810 		WORKITEM_FREE(mkdir2, D_MKDIR);
8811 		mkdir2 = NULL;
8812 	} else {
8813 		LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8814 		WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8815 	}
8816 	*mkdirp = mkdir2;
8817 
8818 	return (mkdir1);
8819 }
8820 
8821 /*
8822  * Directory entry addition dependencies.
8823  *
8824  * When adding a new directory entry, the inode (with its incremented link
8825  * count) must be written to disk before the directory entry's pointer to it.
8826  * Also, if the inode is newly allocated, the corresponding freemap must be
8827  * updated (on disk) before the directory entry's pointer. These requirements
8828  * are met via undo/redo on the directory entry's pointer, which consists
8829  * simply of the inode number.
8830  *
8831  * As directory entries are added and deleted, the free space within a
8832  * directory block can become fragmented.  The ufs filesystem will compact
8833  * a fragmented directory block to make space for a new entry. When this
8834  * occurs, the offsets of previously added entries change. Any "diradd"
8835  * dependency structures corresponding to these entries must be updated with
8836  * the new offsets.
8837  */
8838 
8839 /*
8840  * This routine is called after the in-memory inode's link
8841  * count has been incremented, but before the directory entry's
8842  * pointer to the inode has been set.
8843  */
8844 int
8845 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8846 	struct buf *bp;		/* buffer containing directory block */
8847 	struct inode *dp;	/* inode for directory */
8848 	off_t diroffset;	/* offset of new entry in directory */
8849 	ino_t newinum;		/* inode referenced by new directory entry */
8850 	struct buf *newdirbp;	/* non-NULL => contents of new mkdir */
8851 	int isnewblk;		/* entry is in a newly allocated block */
8852 {
8853 	int offset;		/* offset of new entry within directory block */
8854 	ufs_lbn_t lbn;		/* block in directory containing new entry */
8855 	struct fs *fs;
8856 	struct diradd *dap;
8857 	struct newblk *newblk;
8858 	struct pagedep *pagedep;
8859 	struct inodedep *inodedep;
8860 	struct newdirblk *newdirblk;
8861 	struct mkdir *mkdir1, *mkdir2;
8862 	struct jaddref *jaddref;
8863 	struct ufsmount *ump;
8864 	struct mount *mp;
8865 	int isindir;
8866 
8867 	mp = ITOVFS(dp);
8868 	ump = VFSTOUFS(mp);
8869 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8870 	    ("softdep_setup_directory_add called on non-softdep filesystem"));
8871 	/*
8872 	 * Whiteouts have no dependencies.
8873 	 */
8874 	if (newinum == UFS_WINO) {
8875 		if (newdirbp != NULL)
8876 			bdwrite(newdirbp);
8877 		return (0);
8878 	}
8879 	jaddref = NULL;
8880 	mkdir1 = mkdir2 = NULL;
8881 	fs = ump->um_fs;
8882 	lbn = lblkno(fs, diroffset);
8883 	offset = blkoff(fs, diroffset);
8884 	dap = malloc(sizeof(struct diradd), M_DIRADD,
8885 		M_SOFTDEP_FLAGS|M_ZERO);
8886 	workitem_alloc(&dap->da_list, D_DIRADD, mp);
8887 	dap->da_offset = offset;
8888 	dap->da_newinum = newinum;
8889 	dap->da_state = ATTACHED;
8890 	LIST_INIT(&dap->da_jwork);
8891 	isindir = bp->b_lblkno >= UFS_NDADDR;
8892 	newdirblk = NULL;
8893 	if (isnewblk &&
8894 	    (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8895 		newdirblk = malloc(sizeof(struct newdirblk),
8896 		    M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8897 		workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8898 		LIST_INIT(&newdirblk->db_mkdir);
8899 	}
8900 	/*
8901 	 * If we're creating a new directory setup the dependencies and set
8902 	 * the dap state to wait for them.  Otherwise it's COMPLETE and
8903 	 * we can move on.
8904 	 */
8905 	if (newdirbp == NULL) {
8906 		dap->da_state |= DEPCOMPLETE;
8907 		ACQUIRE_LOCK(ump);
8908 	} else {
8909 		dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8910 		mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8911 		    &mkdir2);
8912 	}
8913 	/*
8914 	 * Link into parent directory pagedep to await its being written.
8915 	 */
8916 	pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8917 #ifdef INVARIANTS
8918 	if (diradd_lookup(pagedep, offset) != NULL)
8919 		panic("softdep_setup_directory_add: %p already at off %d\n",
8920 		    diradd_lookup(pagedep, offset), offset);
8921 #endif
8922 	dap->da_pagedep = pagedep;
8923 	LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8924 	    da_pdlist);
8925 	inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8926 	/*
8927 	 * If we're journaling, link the diradd into the jaddref so it
8928 	 * may be completed after the journal entry is written.  Otherwise,
8929 	 * link the diradd into its inodedep.  If the inode is not yet
8930 	 * written place it on the bufwait list, otherwise do the post-inode
8931 	 * write processing to put it on the id_pendinghd list.
8932 	 */
8933 	if (MOUNTEDSUJ(mp)) {
8934 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8935 		    inoreflst);
8936 		KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8937 		    ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8938 		jaddref->ja_diroff = diroffset;
8939 		jaddref->ja_diradd = dap;
8940 		add_to_journal(&jaddref->ja_list);
8941 	} else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8942 		diradd_inode_written(dap, inodedep);
8943 	else
8944 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8945 	/*
8946 	 * Add the journal entries for . and .. links now that the primary
8947 	 * link is written.
8948 	 */
8949 	if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8950 		jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8951 		    inoreflst, if_deps);
8952 		KASSERT(jaddref != NULL &&
8953 		    jaddref->ja_ino == jaddref->ja_parent &&
8954 		    (jaddref->ja_state & MKDIR_BODY),
8955 		    ("softdep_setup_directory_add: bad dot jaddref %p",
8956 		    jaddref));
8957 		mkdir1->md_jaddref = jaddref;
8958 		jaddref->ja_mkdir = mkdir1;
8959 		/*
8960 		 * It is important that the dotdot journal entry
8961 		 * is added prior to the dot entry since dot writes
8962 		 * both the dot and dotdot links.  These both must
8963 		 * be added after the primary link for the journal
8964 		 * to remain consistent.
8965 		 */
8966 		add_to_journal(&mkdir2->md_jaddref->ja_list);
8967 		add_to_journal(&jaddref->ja_list);
8968 	}
8969 	/*
8970 	 * If we are adding a new directory remember this diradd so that if
8971 	 * we rename it we can keep the dot and dotdot dependencies.  If
8972 	 * we are adding a new name for an inode that has a mkdiradd we
8973 	 * must be in rename and we have to move the dot and dotdot
8974 	 * dependencies to this new name.  The old name is being orphaned
8975 	 * soon.
8976 	 */
8977 	if (mkdir1 != NULL) {
8978 		if (inodedep->id_mkdiradd != NULL)
8979 			panic("softdep_setup_directory_add: Existing mkdir");
8980 		inodedep->id_mkdiradd = dap;
8981 	} else if (inodedep->id_mkdiradd)
8982 		merge_diradd(inodedep, dap);
8983 	if (newdirblk != NULL) {
8984 		/*
8985 		 * There is nothing to do if we are already tracking
8986 		 * this block.
8987 		 */
8988 		if ((pagedep->pd_state & NEWBLOCK) != 0) {
8989 			WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8990 			FREE_LOCK(ump);
8991 			return (0);
8992 		}
8993 		if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8994 		    == 0)
8995 			panic("softdep_setup_directory_add: lost entry");
8996 		WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8997 		pagedep->pd_state |= NEWBLOCK;
8998 		pagedep->pd_newdirblk = newdirblk;
8999 		newdirblk->db_pagedep = pagedep;
9000 		FREE_LOCK(ump);
9001 		/*
9002 		 * If we extended into an indirect signal direnter to sync.
9003 		 */
9004 		if (isindir)
9005 			return (1);
9006 		return (0);
9007 	}
9008 	FREE_LOCK(ump);
9009 	return (0);
9010 }
9011 
9012 /*
9013  * This procedure is called to change the offset of a directory
9014  * entry when compacting a directory block which must be owned
9015  * exclusively by the caller. Note that the actual entry movement
9016  * must be done in this procedure to ensure that no I/O completions
9017  * occur while the move is in progress.
9018  */
9019 void
9020 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
9021 	struct buf *bp;		/* Buffer holding directory block. */
9022 	struct inode *dp;	/* inode for directory */
9023 	caddr_t base;		/* address of dp->i_offset */
9024 	caddr_t oldloc;		/* address of old directory location */
9025 	caddr_t newloc;		/* address of new directory location */
9026 	int entrysize;		/* size of directory entry */
9027 {
9028 	int offset, oldoffset, newoffset;
9029 	struct pagedep *pagedep;
9030 	struct jmvref *jmvref;
9031 	struct diradd *dap;
9032 	struct direct *de;
9033 	struct mount *mp;
9034 	struct ufsmount *ump;
9035 	ufs_lbn_t lbn;
9036 	int flags;
9037 
9038 	mp = ITOVFS(dp);
9039 	ump = VFSTOUFS(mp);
9040 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9041 	    ("softdep_change_directoryentry_offset called on "
9042 	     "non-softdep filesystem"));
9043 	de = (struct direct *)oldloc;
9044 	jmvref = NULL;
9045 	flags = 0;
9046 	/*
9047 	 * Moves are always journaled as it would be too complex to
9048 	 * determine if any affected adds or removes are present in the
9049 	 * journal.
9050 	 */
9051 	if (MOUNTEDSUJ(mp)) {
9052 		flags = DEPALLOC;
9053 		jmvref = newjmvref(dp, de->d_ino,
9054 		    I_OFFSET(dp) + (oldloc - base),
9055 		    I_OFFSET(dp) + (newloc - base));
9056 	}
9057 	lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9058 	offset = blkoff(ump->um_fs, I_OFFSET(dp));
9059 	oldoffset = offset + (oldloc - base);
9060 	newoffset = offset + (newloc - base);
9061 	ACQUIRE_LOCK(ump);
9062 	if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
9063 		goto done;
9064 	dap = diradd_lookup(pagedep, oldoffset);
9065 	if (dap) {
9066 		dap->da_offset = newoffset;
9067 		newoffset = DIRADDHASH(newoffset);
9068 		oldoffset = DIRADDHASH(oldoffset);
9069 		if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
9070 		    newoffset != oldoffset) {
9071 			LIST_REMOVE(dap, da_pdlist);
9072 			LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
9073 			    dap, da_pdlist);
9074 		}
9075 	}
9076 done:
9077 	if (jmvref) {
9078 		jmvref->jm_pagedep = pagedep;
9079 		LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
9080 		add_to_journal(&jmvref->jm_list);
9081 	}
9082 	bcopy(oldloc, newloc, entrysize);
9083 	FREE_LOCK(ump);
9084 }
9085 
9086 /*
9087  * Move the mkdir dependencies and journal work from one diradd to another
9088  * when renaming a directory.  The new name must depend on the mkdir deps
9089  * completing as the old name did.  Directories can only have one valid link
9090  * at a time so one must be canonical.
9091  */
9092 static void
9093 merge_diradd(inodedep, newdap)
9094 	struct inodedep *inodedep;
9095 	struct diradd *newdap;
9096 {
9097 	struct diradd *olddap;
9098 	struct mkdir *mkdir, *nextmd;
9099 	struct ufsmount *ump;
9100 	short state;
9101 
9102 	olddap = inodedep->id_mkdiradd;
9103 	inodedep->id_mkdiradd = newdap;
9104 	if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9105 		newdap->da_state &= ~DEPCOMPLETE;
9106 		ump = VFSTOUFS(inodedep->id_list.wk_mp);
9107 		for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9108 		     mkdir = nextmd) {
9109 			nextmd = LIST_NEXT(mkdir, md_mkdirs);
9110 			if (mkdir->md_diradd != olddap)
9111 				continue;
9112 			mkdir->md_diradd = newdap;
9113 			state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
9114 			newdap->da_state |= state;
9115 			olddap->da_state &= ~state;
9116 			if ((olddap->da_state &
9117 			    (MKDIR_PARENT | MKDIR_BODY)) == 0)
9118 				break;
9119 		}
9120 		if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9121 			panic("merge_diradd: unfound ref");
9122 	}
9123 	/*
9124 	 * Any mkdir related journal items are not safe to be freed until
9125 	 * the new name is stable.
9126 	 */
9127 	jwork_move(&newdap->da_jwork, &olddap->da_jwork);
9128 	olddap->da_state |= DEPCOMPLETE;
9129 	complete_diradd(olddap);
9130 }
9131 
9132 /*
9133  * Move the diradd to the pending list when all diradd dependencies are
9134  * complete.
9135  */
9136 static void
9137 complete_diradd(dap)
9138 	struct diradd *dap;
9139 {
9140 	struct pagedep *pagedep;
9141 
9142 	if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
9143 		if (dap->da_state & DIRCHG)
9144 			pagedep = dap->da_previous->dm_pagedep;
9145 		else
9146 			pagedep = dap->da_pagedep;
9147 		LIST_REMOVE(dap, da_pdlist);
9148 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9149 	}
9150 }
9151 
9152 /*
9153  * Cancel a diradd when a dirrem overlaps with it.  We must cancel the journal
9154  * add entries and conditonally journal the remove.
9155  */
9156 static void
9157 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
9158 	struct diradd *dap;
9159 	struct dirrem *dirrem;
9160 	struct jremref *jremref;
9161 	struct jremref *dotremref;
9162 	struct jremref *dotdotremref;
9163 {
9164 	struct inodedep *inodedep;
9165 	struct jaddref *jaddref;
9166 	struct inoref *inoref;
9167 	struct ufsmount *ump;
9168 	struct mkdir *mkdir;
9169 
9170 	/*
9171 	 * If no remove references were allocated we're on a non-journaled
9172 	 * filesystem and can skip the cancel step.
9173 	 */
9174 	if (jremref == NULL) {
9175 		free_diradd(dap, NULL);
9176 		return;
9177 	}
9178 	/*
9179 	 * Cancel the primary name an free it if it does not require
9180 	 * journaling.
9181 	 */
9182 	if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
9183 	    0, &inodedep) != 0) {
9184 		/* Abort the addref that reference this diradd.  */
9185 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
9186 			if (inoref->if_list.wk_type != D_JADDREF)
9187 				continue;
9188 			jaddref = (struct jaddref *)inoref;
9189 			if (jaddref->ja_diradd != dap)
9190 				continue;
9191 			if (cancel_jaddref(jaddref, inodedep,
9192 			    &dirrem->dm_jwork) == 0) {
9193 				free_jremref(jremref);
9194 				jremref = NULL;
9195 			}
9196 			break;
9197 		}
9198 	}
9199 	/*
9200 	 * Cancel subordinate names and free them if they do not require
9201 	 * journaling.
9202 	 */
9203 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9204 		ump = VFSTOUFS(dap->da_list.wk_mp);
9205 		LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
9206 			if (mkdir->md_diradd != dap)
9207 				continue;
9208 			if ((jaddref = mkdir->md_jaddref) == NULL)
9209 				continue;
9210 			mkdir->md_jaddref = NULL;
9211 			if (mkdir->md_state & MKDIR_PARENT) {
9212 				if (cancel_jaddref(jaddref, NULL,
9213 				    &dirrem->dm_jwork) == 0) {
9214 					free_jremref(dotdotremref);
9215 					dotdotremref = NULL;
9216 				}
9217 			} else {
9218 				if (cancel_jaddref(jaddref, inodedep,
9219 				    &dirrem->dm_jwork) == 0) {
9220 					free_jremref(dotremref);
9221 					dotremref = NULL;
9222 				}
9223 			}
9224 		}
9225 	}
9226 
9227 	if (jremref)
9228 		journal_jremref(dirrem, jremref, inodedep);
9229 	if (dotremref)
9230 		journal_jremref(dirrem, dotremref, inodedep);
9231 	if (dotdotremref)
9232 		journal_jremref(dirrem, dotdotremref, NULL);
9233 	jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
9234 	free_diradd(dap, &dirrem->dm_jwork);
9235 }
9236 
9237 /*
9238  * Free a diradd dependency structure.
9239  */
9240 static void
9241 free_diradd(dap, wkhd)
9242 	struct diradd *dap;
9243 	struct workhead *wkhd;
9244 {
9245 	struct dirrem *dirrem;
9246 	struct pagedep *pagedep;
9247 	struct inodedep *inodedep;
9248 	struct mkdir *mkdir, *nextmd;
9249 	struct ufsmount *ump;
9250 
9251 	ump = VFSTOUFS(dap->da_list.wk_mp);
9252 	LOCK_OWNED(ump);
9253 	LIST_REMOVE(dap, da_pdlist);
9254 	if (dap->da_state & ONWORKLIST)
9255 		WORKLIST_REMOVE(&dap->da_list);
9256 	if ((dap->da_state & DIRCHG) == 0) {
9257 		pagedep = dap->da_pagedep;
9258 	} else {
9259 		dirrem = dap->da_previous;
9260 		pagedep = dirrem->dm_pagedep;
9261 		dirrem->dm_dirinum = pagedep->pd_ino;
9262 		dirrem->dm_state |= COMPLETE;
9263 		if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9264 			add_to_worklist(&dirrem->dm_list, 0);
9265 	}
9266 	if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
9267 	    0, &inodedep) != 0)
9268 		if (inodedep->id_mkdiradd == dap)
9269 			inodedep->id_mkdiradd = NULL;
9270 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9271 		for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9272 		     mkdir = nextmd) {
9273 			nextmd = LIST_NEXT(mkdir, md_mkdirs);
9274 			if (mkdir->md_diradd != dap)
9275 				continue;
9276 			dap->da_state &=
9277 			    ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
9278 			LIST_REMOVE(mkdir, md_mkdirs);
9279 			if (mkdir->md_state & ONWORKLIST)
9280 				WORKLIST_REMOVE(&mkdir->md_list);
9281 			if (mkdir->md_jaddref != NULL)
9282 				panic("free_diradd: Unexpected jaddref");
9283 			WORKITEM_FREE(mkdir, D_MKDIR);
9284 			if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
9285 				break;
9286 		}
9287 		if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9288 			panic("free_diradd: unfound ref");
9289 	}
9290 	if (inodedep)
9291 		free_inodedep(inodedep);
9292 	/*
9293 	 * Free any journal segments waiting for the directory write.
9294 	 */
9295 	handle_jwork(&dap->da_jwork);
9296 	WORKITEM_FREE(dap, D_DIRADD);
9297 }
9298 
9299 /*
9300  * Directory entry removal dependencies.
9301  *
9302  * When removing a directory entry, the entry's inode pointer must be
9303  * zero'ed on disk before the corresponding inode's link count is decremented
9304  * (possibly freeing the inode for re-use). This dependency is handled by
9305  * updating the directory entry but delaying the inode count reduction until
9306  * after the directory block has been written to disk. After this point, the
9307  * inode count can be decremented whenever it is convenient.
9308  */
9309 
9310 /*
9311  * This routine should be called immediately after removing
9312  * a directory entry.  The inode's link count should not be
9313  * decremented by the calling procedure -- the soft updates
9314  * code will do this task when it is safe.
9315  */
9316 void
9317 softdep_setup_remove(bp, dp, ip, isrmdir)
9318 	struct buf *bp;		/* buffer containing directory block */
9319 	struct inode *dp;	/* inode for the directory being modified */
9320 	struct inode *ip;	/* inode for directory entry being removed */
9321 	int isrmdir;		/* indicates if doing RMDIR */
9322 {
9323 	struct dirrem *dirrem, *prevdirrem;
9324 	struct inodedep *inodedep;
9325 	struct ufsmount *ump;
9326 	int direct;
9327 
9328 	ump = ITOUMP(ip);
9329 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9330 	    ("softdep_setup_remove called on non-softdep filesystem"));
9331 	/*
9332 	 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.  We want
9333 	 * newdirrem() to setup the full directory remove which requires
9334 	 * isrmdir > 1.
9335 	 */
9336 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9337 	/*
9338 	 * Add the dirrem to the inodedep's pending remove list for quick
9339 	 * discovery later.
9340 	 */
9341 	if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9342 		panic("softdep_setup_remove: Lost inodedep.");
9343 	KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9344 	dirrem->dm_state |= ONDEPLIST;
9345 	LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9346 
9347 	/*
9348 	 * If the COMPLETE flag is clear, then there were no active
9349 	 * entries and we want to roll back to a zeroed entry until
9350 	 * the new inode is committed to disk. If the COMPLETE flag is
9351 	 * set then we have deleted an entry that never made it to
9352 	 * disk. If the entry we deleted resulted from a name change,
9353 	 * then the old name still resides on disk. We cannot delete
9354 	 * its inode (returned to us in prevdirrem) until the zeroed
9355 	 * directory entry gets to disk. The new inode has never been
9356 	 * referenced on the disk, so can be deleted immediately.
9357 	 */
9358 	if ((dirrem->dm_state & COMPLETE) == 0) {
9359 		LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9360 		    dm_next);
9361 		FREE_LOCK(ump);
9362 	} else {
9363 		if (prevdirrem != NULL)
9364 			LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9365 			    prevdirrem, dm_next);
9366 		dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9367 		direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9368 		FREE_LOCK(ump);
9369 		if (direct)
9370 			handle_workitem_remove(dirrem, 0);
9371 	}
9372 }
9373 
9374 /*
9375  * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9376  * pd_pendinghd list of a pagedep.
9377  */
9378 static struct diradd *
9379 diradd_lookup(pagedep, offset)
9380 	struct pagedep *pagedep;
9381 	int offset;
9382 {
9383 	struct diradd *dap;
9384 
9385 	LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9386 		if (dap->da_offset == offset)
9387 			return (dap);
9388 	LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9389 		if (dap->da_offset == offset)
9390 			return (dap);
9391 	return (NULL);
9392 }
9393 
9394 /*
9395  * Search for a .. diradd dependency in a directory that is being removed.
9396  * If the directory was renamed to a new parent we have a diradd rather
9397  * than a mkdir for the .. entry.  We need to cancel it now before
9398  * it is found in truncate().
9399  */
9400 static struct jremref *
9401 cancel_diradd_dotdot(ip, dirrem, jremref)
9402 	struct inode *ip;
9403 	struct dirrem *dirrem;
9404 	struct jremref *jremref;
9405 {
9406 	struct pagedep *pagedep;
9407 	struct diradd *dap;
9408 	struct worklist *wk;
9409 
9410 	if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9411 		return (jremref);
9412 	dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9413 	if (dap == NULL)
9414 		return (jremref);
9415 	cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9416 	/*
9417 	 * Mark any journal work as belonging to the parent so it is freed
9418 	 * with the .. reference.
9419 	 */
9420 	LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9421 		wk->wk_state |= MKDIR_PARENT;
9422 	return (NULL);
9423 }
9424 
9425 /*
9426  * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9427  * replace it with a dirrem/diradd pair as a result of re-parenting a
9428  * directory.  This ensures that we don't simultaneously have a mkdir and
9429  * a diradd for the same .. entry.
9430  */
9431 static struct jremref *
9432 cancel_mkdir_dotdot(ip, dirrem, jremref)
9433 	struct inode *ip;
9434 	struct dirrem *dirrem;
9435 	struct jremref *jremref;
9436 {
9437 	struct inodedep *inodedep;
9438 	struct jaddref *jaddref;
9439 	struct ufsmount *ump;
9440 	struct mkdir *mkdir;
9441 	struct diradd *dap;
9442 	struct mount *mp;
9443 
9444 	mp = ITOVFS(ip);
9445 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9446 		return (jremref);
9447 	dap = inodedep->id_mkdiradd;
9448 	if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9449 		return (jremref);
9450 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
9451 	for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9452 	    mkdir = LIST_NEXT(mkdir, md_mkdirs))
9453 		if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9454 			break;
9455 	if (mkdir == NULL)
9456 		panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9457 	if ((jaddref = mkdir->md_jaddref) != NULL) {
9458 		mkdir->md_jaddref = NULL;
9459 		jaddref->ja_state &= ~MKDIR_PARENT;
9460 		if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9461 			panic("cancel_mkdir_dotdot: Lost parent inodedep");
9462 		if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9463 			journal_jremref(dirrem, jremref, inodedep);
9464 			jremref = NULL;
9465 		}
9466 	}
9467 	if (mkdir->md_state & ONWORKLIST)
9468 		WORKLIST_REMOVE(&mkdir->md_list);
9469 	mkdir->md_state |= ALLCOMPLETE;
9470 	complete_mkdir(mkdir);
9471 	return (jremref);
9472 }
9473 
9474 static void
9475 journal_jremref(dirrem, jremref, inodedep)
9476 	struct dirrem *dirrem;
9477 	struct jremref *jremref;
9478 	struct inodedep *inodedep;
9479 {
9480 
9481 	if (inodedep == NULL)
9482 		if (inodedep_lookup(jremref->jr_list.wk_mp,
9483 		    jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9484 			panic("journal_jremref: Lost inodedep");
9485 	LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9486 	TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9487 	add_to_journal(&jremref->jr_list);
9488 }
9489 
9490 static void
9491 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9492 	struct dirrem *dirrem;
9493 	struct jremref *jremref;
9494 	struct jremref *dotremref;
9495 	struct jremref *dotdotremref;
9496 {
9497 	struct inodedep *inodedep;
9498 
9499 	if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9500 	    &inodedep) == 0)
9501 		panic("dirrem_journal: Lost inodedep");
9502 	journal_jremref(dirrem, jremref, inodedep);
9503 	if (dotremref)
9504 		journal_jremref(dirrem, dotremref, inodedep);
9505 	if (dotdotremref)
9506 		journal_jremref(dirrem, dotdotremref, NULL);
9507 }
9508 
9509 /*
9510  * Allocate a new dirrem if appropriate and return it along with
9511  * its associated pagedep. Called without a lock, returns with lock.
9512  */
9513 static struct dirrem *
9514 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9515 	struct buf *bp;		/* buffer containing directory block */
9516 	struct inode *dp;	/* inode for the directory being modified */
9517 	struct inode *ip;	/* inode for directory entry being removed */
9518 	int isrmdir;		/* indicates if doing RMDIR */
9519 	struct dirrem **prevdirremp; /* previously referenced inode, if any */
9520 {
9521 	int offset;
9522 	ufs_lbn_t lbn;
9523 	struct diradd *dap;
9524 	struct dirrem *dirrem;
9525 	struct pagedep *pagedep;
9526 	struct jremref *jremref;
9527 	struct jremref *dotremref;
9528 	struct jremref *dotdotremref;
9529 	struct vnode *dvp;
9530 	struct ufsmount *ump;
9531 
9532 	/*
9533 	 * Whiteouts have no deletion dependencies.
9534 	 */
9535 	if (ip == NULL)
9536 		panic("newdirrem: whiteout");
9537 	dvp = ITOV(dp);
9538 	ump = ITOUMP(dp);
9539 
9540 	/*
9541 	 * If the system is over its limit and our filesystem is
9542 	 * responsible for more than our share of that usage and
9543 	 * we are not a snapshot, request some inodedep cleanup.
9544 	 * Limiting the number of dirrem structures will also limit
9545 	 * the number of freefile and freeblks structures.
9546 	 */
9547 	ACQUIRE_LOCK(ump);
9548 	if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9549 		schedule_cleanup(UFSTOVFS(ump));
9550 	else
9551 		FREE_LOCK(ump);
9552 	dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9553 	    M_ZERO);
9554 	workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9555 	LIST_INIT(&dirrem->dm_jremrefhd);
9556 	LIST_INIT(&dirrem->dm_jwork);
9557 	dirrem->dm_state = isrmdir ? RMDIR : 0;
9558 	dirrem->dm_oldinum = ip->i_number;
9559 	*prevdirremp = NULL;
9560 	/*
9561 	 * Allocate remove reference structures to track journal write
9562 	 * dependencies.  We will always have one for the link and
9563 	 * when doing directories we will always have one more for dot.
9564 	 * When renaming a directory we skip the dotdot link change so
9565 	 * this is not needed.
9566 	 */
9567 	jremref = dotremref = dotdotremref = NULL;
9568 	if (DOINGSUJ(dvp)) {
9569 		if (isrmdir) {
9570 			jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9571 			    ip->i_effnlink + 2);
9572 			dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9573 			    ip->i_effnlink + 1);
9574 			dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9575 			    dp->i_effnlink + 1);
9576 			dotdotremref->jr_state |= MKDIR_PARENT;
9577 		} else
9578 			jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9579 			    ip->i_effnlink + 1);
9580 	}
9581 	ACQUIRE_LOCK(ump);
9582 	lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9583 	offset = blkoff(ump->um_fs, I_OFFSET(dp));
9584 	pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9585 	    &pagedep);
9586 	dirrem->dm_pagedep = pagedep;
9587 	dirrem->dm_offset = offset;
9588 	/*
9589 	 * If we're renaming a .. link to a new directory, cancel any
9590 	 * existing MKDIR_PARENT mkdir.  If it has already been canceled
9591 	 * the jremref is preserved for any potential diradd in this
9592 	 * location.  This can not coincide with a rmdir.
9593 	 */
9594 	if (I_OFFSET(dp) == DOTDOT_OFFSET) {
9595 		if (isrmdir)
9596 			panic("newdirrem: .. directory change during remove?");
9597 		jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9598 	}
9599 	/*
9600 	 * If we're removing a directory search for the .. dependency now and
9601 	 * cancel it.  Any pending journal work will be added to the dirrem
9602 	 * to be completed when the workitem remove completes.
9603 	 */
9604 	if (isrmdir)
9605 		dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9606 	/*
9607 	 * Check for a diradd dependency for the same directory entry.
9608 	 * If present, then both dependencies become obsolete and can
9609 	 * be de-allocated.
9610 	 */
9611 	dap = diradd_lookup(pagedep, offset);
9612 	if (dap == NULL) {
9613 		/*
9614 		 * Link the jremref structures into the dirrem so they are
9615 		 * written prior to the pagedep.
9616 		 */
9617 		if (jremref)
9618 			dirrem_journal(dirrem, jremref, dotremref,
9619 			    dotdotremref);
9620 		return (dirrem);
9621 	}
9622 	/*
9623 	 * Must be ATTACHED at this point.
9624 	 */
9625 	if ((dap->da_state & ATTACHED) == 0)
9626 		panic("newdirrem: not ATTACHED");
9627 	if (dap->da_newinum != ip->i_number)
9628 		panic("newdirrem: inum %ju should be %ju",
9629 		    (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9630 	/*
9631 	 * If we are deleting a changed name that never made it to disk,
9632 	 * then return the dirrem describing the previous inode (which
9633 	 * represents the inode currently referenced from this entry on disk).
9634 	 */
9635 	if ((dap->da_state & DIRCHG) != 0) {
9636 		*prevdirremp = dap->da_previous;
9637 		dap->da_state &= ~DIRCHG;
9638 		dap->da_pagedep = pagedep;
9639 	}
9640 	/*
9641 	 * We are deleting an entry that never made it to disk.
9642 	 * Mark it COMPLETE so we can delete its inode immediately.
9643 	 */
9644 	dirrem->dm_state |= COMPLETE;
9645 	cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9646 #ifdef INVARIANTS
9647 	if (isrmdir == 0) {
9648 		struct worklist *wk;
9649 
9650 		LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9651 			if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9652 				panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9653 	}
9654 #endif
9655 
9656 	return (dirrem);
9657 }
9658 
9659 /*
9660  * Directory entry change dependencies.
9661  *
9662  * Changing an existing directory entry requires that an add operation
9663  * be completed first followed by a deletion. The semantics for the addition
9664  * are identical to the description of adding a new entry above except
9665  * that the rollback is to the old inode number rather than zero. Once
9666  * the addition dependency is completed, the removal is done as described
9667  * in the removal routine above.
9668  */
9669 
9670 /*
9671  * This routine should be called immediately after changing
9672  * a directory entry.  The inode's link count should not be
9673  * decremented by the calling procedure -- the soft updates
9674  * code will perform this task when it is safe.
9675  */
9676 void
9677 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9678 	struct buf *bp;		/* buffer containing directory block */
9679 	struct inode *dp;	/* inode for the directory being modified */
9680 	struct inode *ip;	/* inode for directory entry being removed */
9681 	ino_t newinum;		/* new inode number for changed entry */
9682 	int isrmdir;		/* indicates if doing RMDIR */
9683 {
9684 	int offset;
9685 	struct diradd *dap = NULL;
9686 	struct dirrem *dirrem, *prevdirrem;
9687 	struct pagedep *pagedep;
9688 	struct inodedep *inodedep;
9689 	struct jaddref *jaddref;
9690 	struct mount *mp;
9691 	struct ufsmount *ump;
9692 
9693 	mp = ITOVFS(dp);
9694 	ump = VFSTOUFS(mp);
9695 	offset = blkoff(ump->um_fs, I_OFFSET(dp));
9696 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9697 	   ("softdep_setup_directory_change called on non-softdep filesystem"));
9698 
9699 	/*
9700 	 * Whiteouts do not need diradd dependencies.
9701 	 */
9702 	if (newinum != UFS_WINO) {
9703 		dap = malloc(sizeof(struct diradd),
9704 		    M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9705 		workitem_alloc(&dap->da_list, D_DIRADD, mp);
9706 		dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9707 		dap->da_offset = offset;
9708 		dap->da_newinum = newinum;
9709 		LIST_INIT(&dap->da_jwork);
9710 	}
9711 
9712 	/*
9713 	 * Allocate a new dirrem and ACQUIRE_LOCK.
9714 	 */
9715 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9716 	pagedep = dirrem->dm_pagedep;
9717 	/*
9718 	 * The possible values for isrmdir:
9719 	 *	0 - non-directory file rename
9720 	 *	1 - directory rename within same directory
9721 	 *   inum - directory rename to new directory of given inode number
9722 	 * When renaming to a new directory, we are both deleting and
9723 	 * creating a new directory entry, so the link count on the new
9724 	 * directory should not change. Thus we do not need the followup
9725 	 * dirrem which is usually done in handle_workitem_remove. We set
9726 	 * the DIRCHG flag to tell handle_workitem_remove to skip the
9727 	 * followup dirrem.
9728 	 */
9729 	if (isrmdir > 1)
9730 		dirrem->dm_state |= DIRCHG;
9731 
9732 	/*
9733 	 * Whiteouts have no additional dependencies,
9734 	 * so just put the dirrem on the correct list.
9735 	 */
9736 	if (newinum == UFS_WINO) {
9737 		if ((dirrem->dm_state & COMPLETE) == 0) {
9738 			LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9739 			    dm_next);
9740 		} else {
9741 			dirrem->dm_dirinum = pagedep->pd_ino;
9742 			if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9743 				add_to_worklist(&dirrem->dm_list, 0);
9744 		}
9745 		FREE_LOCK(ump);
9746 		return;
9747 	}
9748 	/*
9749 	 * Add the dirrem to the inodedep's pending remove list for quick
9750 	 * discovery later.  A valid nlinkdelta ensures that this lookup
9751 	 * will not fail.
9752 	 */
9753 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9754 		panic("softdep_setup_directory_change: Lost inodedep.");
9755 	dirrem->dm_state |= ONDEPLIST;
9756 	LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9757 
9758 	/*
9759 	 * If the COMPLETE flag is clear, then there were no active
9760 	 * entries and we want to roll back to the previous inode until
9761 	 * the new inode is committed to disk. If the COMPLETE flag is
9762 	 * set, then we have deleted an entry that never made it to disk.
9763 	 * If the entry we deleted resulted from a name change, then the old
9764 	 * inode reference still resides on disk. Any rollback that we do
9765 	 * needs to be to that old inode (returned to us in prevdirrem). If
9766 	 * the entry we deleted resulted from a create, then there is
9767 	 * no entry on the disk, so we want to roll back to zero rather
9768 	 * than the uncommitted inode. In either of the COMPLETE cases we
9769 	 * want to immediately free the unwritten and unreferenced inode.
9770 	 */
9771 	if ((dirrem->dm_state & COMPLETE) == 0) {
9772 		dap->da_previous = dirrem;
9773 	} else {
9774 		if (prevdirrem != NULL) {
9775 			dap->da_previous = prevdirrem;
9776 		} else {
9777 			dap->da_state &= ~DIRCHG;
9778 			dap->da_pagedep = pagedep;
9779 		}
9780 		dirrem->dm_dirinum = pagedep->pd_ino;
9781 		if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9782 			add_to_worklist(&dirrem->dm_list, 0);
9783 	}
9784 	/*
9785 	 * Lookup the jaddref for this journal entry.  We must finish
9786 	 * initializing it and make the diradd write dependent on it.
9787 	 * If we're not journaling, put it on the id_bufwait list if the
9788 	 * inode is not yet written. If it is written, do the post-inode
9789 	 * write processing to put it on the id_pendinghd list.
9790 	 */
9791 	inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9792 	if (MOUNTEDSUJ(mp)) {
9793 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9794 		    inoreflst);
9795 		KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9796 		    ("softdep_setup_directory_change: bad jaddref %p",
9797 		    jaddref));
9798 		jaddref->ja_diroff = I_OFFSET(dp);
9799 		jaddref->ja_diradd = dap;
9800 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9801 		    dap, da_pdlist);
9802 		add_to_journal(&jaddref->ja_list);
9803 	} else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9804 		dap->da_state |= COMPLETE;
9805 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9806 		WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9807 	} else {
9808 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9809 		    dap, da_pdlist);
9810 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9811 	}
9812 	/*
9813 	 * If we're making a new name for a directory that has not been
9814 	 * committed when need to move the dot and dotdot references to
9815 	 * this new name.
9816 	 */
9817 	if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET)
9818 		merge_diradd(inodedep, dap);
9819 	FREE_LOCK(ump);
9820 }
9821 
9822 /*
9823  * Called whenever the link count on an inode is changed.
9824  * It creates an inode dependency so that the new reference(s)
9825  * to the inode cannot be committed to disk until the updated
9826  * inode has been written.
9827  */
9828 void
9829 softdep_change_linkcnt(ip)
9830 	struct inode *ip;	/* the inode with the increased link count */
9831 {
9832 	struct inodedep *inodedep;
9833 	struct ufsmount *ump;
9834 
9835 	ump = ITOUMP(ip);
9836 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9837 	    ("softdep_change_linkcnt called on non-softdep filesystem"));
9838 	ACQUIRE_LOCK(ump);
9839 	inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9840 	if (ip->i_nlink < ip->i_effnlink)
9841 		panic("softdep_change_linkcnt: bad delta");
9842 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9843 	FREE_LOCK(ump);
9844 }
9845 
9846 /*
9847  * Attach a sbdep dependency to the superblock buf so that we can keep
9848  * track of the head of the linked list of referenced but unlinked inodes.
9849  */
9850 void
9851 softdep_setup_sbupdate(ump, fs, bp)
9852 	struct ufsmount *ump;
9853 	struct fs *fs;
9854 	struct buf *bp;
9855 {
9856 	struct sbdep *sbdep;
9857 	struct worklist *wk;
9858 
9859 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9860 	    ("softdep_setup_sbupdate called on non-softdep filesystem"));
9861 	LIST_FOREACH(wk, &bp->b_dep, wk_list)
9862 		if (wk->wk_type == D_SBDEP)
9863 			break;
9864 	if (wk != NULL)
9865 		return;
9866 	sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9867 	workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9868 	sbdep->sb_fs = fs;
9869 	sbdep->sb_ump = ump;
9870 	ACQUIRE_LOCK(ump);
9871 	WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9872 	FREE_LOCK(ump);
9873 }
9874 
9875 /*
9876  * Return the first unlinked inodedep which is ready to be the head of the
9877  * list.  The inodedep and all those after it must have valid next pointers.
9878  */
9879 static struct inodedep *
9880 first_unlinked_inodedep(ump)
9881 	struct ufsmount *ump;
9882 {
9883 	struct inodedep *inodedep;
9884 	struct inodedep *idp;
9885 
9886 	LOCK_OWNED(ump);
9887 	for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9888 	    inodedep; inodedep = idp) {
9889 		if ((inodedep->id_state & UNLINKNEXT) == 0)
9890 			return (NULL);
9891 		idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9892 		if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9893 			break;
9894 		if ((inodedep->id_state & UNLINKPREV) == 0)
9895 			break;
9896 	}
9897 	return (inodedep);
9898 }
9899 
9900 /*
9901  * Set the sujfree unlinked head pointer prior to writing a superblock.
9902  */
9903 static void
9904 initiate_write_sbdep(sbdep)
9905 	struct sbdep *sbdep;
9906 {
9907 	struct inodedep *inodedep;
9908 	struct fs *bpfs;
9909 	struct fs *fs;
9910 
9911 	bpfs = sbdep->sb_fs;
9912 	fs = sbdep->sb_ump->um_fs;
9913 	inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9914 	if (inodedep) {
9915 		fs->fs_sujfree = inodedep->id_ino;
9916 		inodedep->id_state |= UNLINKPREV;
9917 	} else
9918 		fs->fs_sujfree = 0;
9919 	bpfs->fs_sujfree = fs->fs_sujfree;
9920 	/*
9921 	 * Because we have made changes to the superblock, we need to
9922 	 * recompute its check-hash.
9923 	 */
9924 	bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9925 }
9926 
9927 /*
9928  * After a superblock is written determine whether it must be written again
9929  * due to a changing unlinked list head.
9930  */
9931 static int
9932 handle_written_sbdep(sbdep, bp)
9933 	struct sbdep *sbdep;
9934 	struct buf *bp;
9935 {
9936 	struct inodedep *inodedep;
9937 	struct fs *fs;
9938 
9939 	LOCK_OWNED(sbdep->sb_ump);
9940 	fs = sbdep->sb_fs;
9941 	/*
9942 	 * If the superblock doesn't match the in-memory list start over.
9943 	 */
9944 	inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9945 	if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9946 	    (inodedep == NULL && fs->fs_sujfree != 0)) {
9947 		bdirty(bp);
9948 		return (1);
9949 	}
9950 	WORKITEM_FREE(sbdep, D_SBDEP);
9951 	if (fs->fs_sujfree == 0)
9952 		return (0);
9953 	/*
9954 	 * Now that we have a record of this inode in stable store allow it
9955 	 * to be written to free up pending work.  Inodes may see a lot of
9956 	 * write activity after they are unlinked which we must not hold up.
9957 	 */
9958 	for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9959 		if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9960 			panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9961 			    inodedep, inodedep->id_state);
9962 		if (inodedep->id_state & UNLINKONLIST)
9963 			break;
9964 		inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9965 	}
9966 
9967 	return (0);
9968 }
9969 
9970 /*
9971  * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9972  */
9973 static void
9974 unlinked_inodedep(mp, inodedep)
9975 	struct mount *mp;
9976 	struct inodedep *inodedep;
9977 {
9978 	struct ufsmount *ump;
9979 
9980 	ump = VFSTOUFS(mp);
9981 	LOCK_OWNED(ump);
9982 	if (MOUNTEDSUJ(mp) == 0)
9983 		return;
9984 	ump->um_fs->fs_fmod = 1;
9985 	if (inodedep->id_state & UNLINKED)
9986 		panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9987 	inodedep->id_state |= UNLINKED;
9988 	TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9989 }
9990 
9991 /*
9992  * Remove an inodedep from the unlinked inodedep list.  This may require
9993  * disk writes if the inode has made it that far.
9994  */
9995 static void
9996 clear_unlinked_inodedep(inodedep)
9997 	struct inodedep *inodedep;
9998 {
9999 	struct ufs2_dinode *dip;
10000 	struct ufsmount *ump;
10001 	struct inodedep *idp;
10002 	struct inodedep *idn;
10003 	struct fs *fs, *bpfs;
10004 	struct buf *bp;
10005 	daddr_t dbn;
10006 	ino_t ino;
10007 	ino_t nino;
10008 	ino_t pino;
10009 	int error;
10010 
10011 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
10012 	fs = ump->um_fs;
10013 	ino = inodedep->id_ino;
10014 	error = 0;
10015 	for (;;) {
10016 		LOCK_OWNED(ump);
10017 		KASSERT((inodedep->id_state & UNLINKED) != 0,
10018 		    ("clear_unlinked_inodedep: inodedep %p not unlinked",
10019 		    inodedep));
10020 		/*
10021 		 * If nothing has yet been written simply remove us from
10022 		 * the in memory list and return.  This is the most common
10023 		 * case where handle_workitem_remove() loses the final
10024 		 * reference.
10025 		 */
10026 		if ((inodedep->id_state & UNLINKLINKS) == 0)
10027 			break;
10028 		/*
10029 		 * If we have a NEXT pointer and no PREV pointer we can simply
10030 		 * clear NEXT's PREV and remove ourselves from the list.  Be
10031 		 * careful not to clear PREV if the superblock points at
10032 		 * next as well.
10033 		 */
10034 		idn = TAILQ_NEXT(inodedep, id_unlinked);
10035 		if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
10036 			if (idn && fs->fs_sujfree != idn->id_ino)
10037 				idn->id_state &= ~UNLINKPREV;
10038 			break;
10039 		}
10040 		/*
10041 		 * Here we have an inodedep which is actually linked into
10042 		 * the list.  We must remove it by forcing a write to the
10043 		 * link before us, whether it be the superblock or an inode.
10044 		 * Unfortunately the list may change while we're waiting
10045 		 * on the buf lock for either resource so we must loop until
10046 		 * we lock the right one.  If both the superblock and an
10047 		 * inode point to this inode we must clear the inode first
10048 		 * followed by the superblock.
10049 		 */
10050 		idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
10051 		pino = 0;
10052 		if (idp && (idp->id_state & UNLINKNEXT))
10053 			pino = idp->id_ino;
10054 		FREE_LOCK(ump);
10055 		if (pino == 0) {
10056 			bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
10057 			    (int)fs->fs_sbsize, 0, 0, 0);
10058 		} else {
10059 			dbn = fsbtodb(fs, ino_to_fsba(fs, pino));
10060 			error = ffs_breadz(ump, ump->um_devvp, dbn, dbn,
10061 			    (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL,
10062 			    &bp);
10063 		}
10064 		ACQUIRE_LOCK(ump);
10065 		if (error)
10066 			break;
10067 		/* If the list has changed restart the loop. */
10068 		idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
10069 		nino = 0;
10070 		if (idp && (idp->id_state & UNLINKNEXT))
10071 			nino = idp->id_ino;
10072 		if (nino != pino ||
10073 		    (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
10074 			FREE_LOCK(ump);
10075 			brelse(bp);
10076 			ACQUIRE_LOCK(ump);
10077 			continue;
10078 		}
10079 		nino = 0;
10080 		idn = TAILQ_NEXT(inodedep, id_unlinked);
10081 		if (idn)
10082 			nino = idn->id_ino;
10083 		/*
10084 		 * Remove us from the in memory list.  After this we cannot
10085 		 * access the inodedep.
10086 		 */
10087 		KASSERT((inodedep->id_state & UNLINKED) != 0,
10088 		    ("clear_unlinked_inodedep: inodedep %p not unlinked",
10089 		    inodedep));
10090 		inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
10091 		TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
10092 		FREE_LOCK(ump);
10093 		/*
10094 		 * The predecessor's next pointer is manually updated here
10095 		 * so that the NEXT flag is never cleared for an element
10096 		 * that is in the list.
10097 		 */
10098 		if (pino == 0) {
10099 			bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
10100 			bpfs = (struct fs *)bp->b_data;
10101 			ffs_oldfscompat_write(bpfs, ump);
10102 			softdep_setup_sbupdate(ump, bpfs, bp);
10103 			/*
10104 			 * Because we may have made changes to the superblock,
10105 			 * we need to recompute its check-hash.
10106 			 */
10107 			bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
10108 		} else if (fs->fs_magic == FS_UFS1_MAGIC) {
10109 			((struct ufs1_dinode *)bp->b_data +
10110 			    ino_to_fsbo(fs, pino))->di_freelink = nino;
10111 		} else {
10112 			dip = (struct ufs2_dinode *)bp->b_data +
10113 			    ino_to_fsbo(fs, pino);
10114 			dip->di_freelink = nino;
10115 			ffs_update_dinode_ckhash(fs, dip);
10116 		}
10117 		/*
10118 		 * If the bwrite fails we have no recourse to recover.  The
10119 		 * filesystem is corrupted already.
10120 		 */
10121 		bwrite(bp);
10122 		ACQUIRE_LOCK(ump);
10123 		/*
10124 		 * If the superblock pointer still needs to be cleared force
10125 		 * a write here.
10126 		 */
10127 		if (fs->fs_sujfree == ino) {
10128 			FREE_LOCK(ump);
10129 			bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
10130 			    (int)fs->fs_sbsize, 0, 0, 0);
10131 			bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
10132 			bpfs = (struct fs *)bp->b_data;
10133 			ffs_oldfscompat_write(bpfs, ump);
10134 			softdep_setup_sbupdate(ump, bpfs, bp);
10135 			/*
10136 			 * Because we may have made changes to the superblock,
10137 			 * we need to recompute its check-hash.
10138 			 */
10139 			bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
10140 			bwrite(bp);
10141 			ACQUIRE_LOCK(ump);
10142 		}
10143 
10144 		if (fs->fs_sujfree != ino)
10145 			return;
10146 		panic("clear_unlinked_inodedep: Failed to clear free head");
10147 	}
10148 	if (inodedep->id_ino == fs->fs_sujfree)
10149 		panic("clear_unlinked_inodedep: Freeing head of free list");
10150 	inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
10151 	TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
10152 	return;
10153 }
10154 
10155 /*
10156  * This workitem decrements the inode's link count.
10157  * If the link count reaches zero, the file is removed.
10158  */
10159 static int
10160 handle_workitem_remove(dirrem, flags)
10161 	struct dirrem *dirrem;
10162 	int flags;
10163 {
10164 	struct inodedep *inodedep;
10165 	struct workhead dotdotwk;
10166 	struct worklist *wk;
10167 	struct ufsmount *ump;
10168 	struct mount *mp;
10169 	struct vnode *vp;
10170 	struct inode *ip;
10171 	ino_t oldinum;
10172 
10173 	if (dirrem->dm_state & ONWORKLIST)
10174 		panic("handle_workitem_remove: dirrem %p still on worklist",
10175 		    dirrem);
10176 	oldinum = dirrem->dm_oldinum;
10177 	mp = dirrem->dm_list.wk_mp;
10178 	ump = VFSTOUFS(mp);
10179 	flags |= LK_EXCLUSIVE;
10180 	if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0)
10181 		return (EBUSY);
10182 	ip = VTOI(vp);
10183 	MPASS(ip->i_mode != 0);
10184 	ACQUIRE_LOCK(ump);
10185 	if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
10186 		panic("handle_workitem_remove: lost inodedep");
10187 	if (dirrem->dm_state & ONDEPLIST)
10188 		LIST_REMOVE(dirrem, dm_inonext);
10189 	KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
10190 	    ("handle_workitem_remove:  Journal entries not written."));
10191 
10192 	/*
10193 	 * Move all dependencies waiting on the remove to complete
10194 	 * from the dirrem to the inode inowait list to be completed
10195 	 * after the inode has been updated and written to disk.
10196 	 *
10197 	 * Any marked MKDIR_PARENT are saved to be completed when the
10198 	 * dotdot ref is removed unless DIRCHG is specified.  For
10199 	 * directory change operations there will be no further
10200 	 * directory writes and the jsegdeps need to be moved along
10201 	 * with the rest to be completed when the inode is free or
10202 	 * stable in the inode free list.
10203 	 */
10204 	LIST_INIT(&dotdotwk);
10205 	while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
10206 		WORKLIST_REMOVE(wk);
10207 		if ((dirrem->dm_state & DIRCHG) == 0 &&
10208 		    wk->wk_state & MKDIR_PARENT) {
10209 			wk->wk_state &= ~MKDIR_PARENT;
10210 			WORKLIST_INSERT(&dotdotwk, wk);
10211 			continue;
10212 		}
10213 		WORKLIST_INSERT(&inodedep->id_inowait, wk);
10214 	}
10215 	LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
10216 	/*
10217 	 * Normal file deletion.
10218 	 */
10219 	if ((dirrem->dm_state & RMDIR) == 0) {
10220 		ip->i_nlink--;
10221 		KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
10222 		    "%ju negative i_nlink %d", (intmax_t)ip->i_number,
10223 		    ip->i_nlink));
10224 		DIP_SET(ip, i_nlink, ip->i_nlink);
10225 		UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10226 		if (ip->i_nlink < ip->i_effnlink)
10227 			panic("handle_workitem_remove: bad file delta");
10228 		if (ip->i_nlink == 0)
10229 			unlinked_inodedep(mp, inodedep);
10230 		inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10231 		KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10232 		    ("handle_workitem_remove: worklist not empty. %s",
10233 		    TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
10234 		WORKITEM_FREE(dirrem, D_DIRREM);
10235 		FREE_LOCK(ump);
10236 		goto out;
10237 	}
10238 	/*
10239 	 * Directory deletion. Decrement reference count for both the
10240 	 * just deleted parent directory entry and the reference for ".".
10241 	 * Arrange to have the reference count on the parent decremented
10242 	 * to account for the loss of "..".
10243 	 */
10244 	ip->i_nlink -= 2;
10245 	KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
10246 	    "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
10247 	DIP_SET(ip, i_nlink, ip->i_nlink);
10248 	UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10249 	if (ip->i_nlink < ip->i_effnlink)
10250 		panic("handle_workitem_remove: bad dir delta");
10251 	if (ip->i_nlink == 0)
10252 		unlinked_inodedep(mp, inodedep);
10253 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10254 	/*
10255 	 * Rename a directory to a new parent. Since, we are both deleting
10256 	 * and creating a new directory entry, the link count on the new
10257 	 * directory should not change. Thus we skip the followup dirrem.
10258 	 */
10259 	if (dirrem->dm_state & DIRCHG) {
10260 		KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10261 		    ("handle_workitem_remove: DIRCHG and worklist not empty."));
10262 		WORKITEM_FREE(dirrem, D_DIRREM);
10263 		FREE_LOCK(ump);
10264 		goto out;
10265 	}
10266 	dirrem->dm_state = ONDEPLIST;
10267 	dirrem->dm_oldinum = dirrem->dm_dirinum;
10268 	/*
10269 	 * Place the dirrem on the parent's diremhd list.
10270 	 */
10271 	if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
10272 		panic("handle_workitem_remove: lost dir inodedep");
10273 	LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
10274 	/*
10275 	 * If the allocated inode has never been written to disk, then
10276 	 * the on-disk inode is zero'ed and we can remove the file
10277 	 * immediately.  When journaling if the inode has been marked
10278 	 * unlinked and not DEPCOMPLETE we know it can never be written.
10279 	 */
10280 	inodedep_lookup(mp, oldinum, 0, &inodedep);
10281 	if (inodedep == NULL ||
10282 	    (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
10283 	    check_inode_unwritten(inodedep)) {
10284 		FREE_LOCK(ump);
10285 		vput(vp);
10286 		return handle_workitem_remove(dirrem, flags);
10287 	}
10288 	WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
10289 	FREE_LOCK(ump);
10290 	UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10291 out:
10292 	ffs_update(vp, 0);
10293 	vput(vp);
10294 	return (0);
10295 }
10296 
10297 /*
10298  * Inode de-allocation dependencies.
10299  *
10300  * When an inode's link count is reduced to zero, it can be de-allocated. We
10301  * found it convenient to postpone de-allocation until after the inode is
10302  * written to disk with its new link count (zero).  At this point, all of the
10303  * on-disk inode's block pointers are nullified and, with careful dependency
10304  * list ordering, all dependencies related to the inode will be satisfied and
10305  * the corresponding dependency structures de-allocated.  So, if/when the
10306  * inode is reused, there will be no mixing of old dependencies with new
10307  * ones.  This artificial dependency is set up by the block de-allocation
10308  * procedure above (softdep_setup_freeblocks) and completed by the
10309  * following procedure.
10310  */
10311 static void
10312 handle_workitem_freefile(freefile)
10313 	struct freefile *freefile;
10314 {
10315 	struct workhead wkhd;
10316 	struct fs *fs;
10317 	struct ufsmount *ump;
10318 	int error;
10319 #ifdef INVARIANTS
10320 	struct inodedep *idp;
10321 #endif
10322 
10323 	ump = VFSTOUFS(freefile->fx_list.wk_mp);
10324 	fs = ump->um_fs;
10325 #ifdef INVARIANTS
10326 	ACQUIRE_LOCK(ump);
10327 	error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10328 	FREE_LOCK(ump);
10329 	if (error)
10330 		panic("handle_workitem_freefile: inodedep %p survived", idp);
10331 #endif
10332 	UFS_LOCK(ump);
10333 	fs->fs_pendinginodes -= 1;
10334 	UFS_UNLOCK(ump);
10335 	LIST_INIT(&wkhd);
10336 	LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10337 	if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10338 	    freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10339 		softdep_error("handle_workitem_freefile", error);
10340 	ACQUIRE_LOCK(ump);
10341 	WORKITEM_FREE(freefile, D_FREEFILE);
10342 	FREE_LOCK(ump);
10343 }
10344 
10345 /*
10346  * Helper function which unlinks marker element from work list and returns
10347  * the next element on the list.
10348  */
10349 static __inline struct worklist *
10350 markernext(struct worklist *marker)
10351 {
10352 	struct worklist *next;
10353 
10354 	next = LIST_NEXT(marker, wk_list);
10355 	LIST_REMOVE(marker, wk_list);
10356 	return next;
10357 }
10358 
10359 /*
10360  * Disk writes.
10361  *
10362  * The dependency structures constructed above are most actively used when file
10363  * system blocks are written to disk.  No constraints are placed on when a
10364  * block can be written, but unsatisfied update dependencies are made safe by
10365  * modifying (or replacing) the source memory for the duration of the disk
10366  * write.  When the disk write completes, the memory block is again brought
10367  * up-to-date.
10368  *
10369  * In-core inode structure reclamation.
10370  *
10371  * Because there are a finite number of "in-core" inode structures, they are
10372  * reused regularly.  By transferring all inode-related dependencies to the
10373  * in-memory inode block and indexing them separately (via "inodedep"s), we
10374  * can allow "in-core" inode structures to be reused at any time and avoid
10375  * any increase in contention.
10376  *
10377  * Called just before entering the device driver to initiate a new disk I/O.
10378  * The buffer must be locked, thus, no I/O completion operations can occur
10379  * while we are manipulating its associated dependencies.
10380  */
10381 static void
10382 softdep_disk_io_initiation(bp)
10383 	struct buf *bp;		/* structure describing disk write to occur */
10384 {
10385 	struct worklist *wk;
10386 	struct worklist marker;
10387 	struct inodedep *inodedep;
10388 	struct freeblks *freeblks;
10389 	struct jblkdep *jblkdep;
10390 	struct newblk *newblk;
10391 	struct ufsmount *ump;
10392 
10393 	/*
10394 	 * We only care about write operations. There should never
10395 	 * be dependencies for reads.
10396 	 */
10397 	if (bp->b_iocmd != BIO_WRITE)
10398 		panic("softdep_disk_io_initiation: not write");
10399 
10400 	if (bp->b_vflags & BV_BKGRDINPROG)
10401 		panic("softdep_disk_io_initiation: Writing buffer with "
10402 		    "background write in progress: %p", bp);
10403 
10404 	ump = softdep_bp_to_mp(bp);
10405 	if (ump == NULL)
10406 		return;
10407 
10408 	marker.wk_type = D_LAST + 1;	/* Not a normal workitem */
10409 	PHOLD(curproc);			/* Don't swap out kernel stack */
10410 	ACQUIRE_LOCK(ump);
10411 	/*
10412 	 * Do any necessary pre-I/O processing.
10413 	 */
10414 	for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10415 	     wk = markernext(&marker)) {
10416 		LIST_INSERT_AFTER(wk, &marker, wk_list);
10417 		switch (wk->wk_type) {
10418 		case D_PAGEDEP:
10419 			initiate_write_filepage(WK_PAGEDEP(wk), bp);
10420 			continue;
10421 
10422 		case D_INODEDEP:
10423 			inodedep = WK_INODEDEP(wk);
10424 			if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10425 				initiate_write_inodeblock_ufs1(inodedep, bp);
10426 			else
10427 				initiate_write_inodeblock_ufs2(inodedep, bp);
10428 			continue;
10429 
10430 		case D_INDIRDEP:
10431 			initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10432 			continue;
10433 
10434 		case D_BMSAFEMAP:
10435 			initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10436 			continue;
10437 
10438 		case D_JSEG:
10439 			WK_JSEG(wk)->js_buf = NULL;
10440 			continue;
10441 
10442 		case D_FREEBLKS:
10443 			freeblks = WK_FREEBLKS(wk);
10444 			jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10445 			/*
10446 			 * We have to wait for the freeblks to be journaled
10447 			 * before we can write an inodeblock with updated
10448 			 * pointers.  Be careful to arrange the marker so
10449 			 * we revisit the freeblks if it's not removed by
10450 			 * the first jwait().
10451 			 */
10452 			if (jblkdep != NULL) {
10453 				LIST_REMOVE(&marker, wk_list);
10454 				LIST_INSERT_BEFORE(wk, &marker, wk_list);
10455 				jwait(&jblkdep->jb_list, MNT_WAIT);
10456 			}
10457 			continue;
10458 		case D_ALLOCDIRECT:
10459 		case D_ALLOCINDIR:
10460 			/*
10461 			 * We have to wait for the jnewblk to be journaled
10462 			 * before we can write to a block if the contents
10463 			 * may be confused with an earlier file's indirect
10464 			 * at recovery time.  Handle the marker as described
10465 			 * above.
10466 			 */
10467 			newblk = WK_NEWBLK(wk);
10468 			if (newblk->nb_jnewblk != NULL &&
10469 			    indirblk_lookup(newblk->nb_list.wk_mp,
10470 			    newblk->nb_newblkno)) {
10471 				LIST_REMOVE(&marker, wk_list);
10472 				LIST_INSERT_BEFORE(wk, &marker, wk_list);
10473 				jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10474 			}
10475 			continue;
10476 
10477 		case D_SBDEP:
10478 			initiate_write_sbdep(WK_SBDEP(wk));
10479 			continue;
10480 
10481 		case D_MKDIR:
10482 		case D_FREEWORK:
10483 		case D_FREEDEP:
10484 		case D_JSEGDEP:
10485 			continue;
10486 
10487 		default:
10488 			panic("handle_disk_io_initiation: Unexpected type %s",
10489 			    TYPENAME(wk->wk_type));
10490 			/* NOTREACHED */
10491 		}
10492 	}
10493 	FREE_LOCK(ump);
10494 	PRELE(curproc);			/* Allow swapout of kernel stack */
10495 }
10496 
10497 /*
10498  * Called from within the procedure above to deal with unsatisfied
10499  * allocation dependencies in a directory. The buffer must be locked,
10500  * thus, no I/O completion operations can occur while we are
10501  * manipulating its associated dependencies.
10502  */
10503 static void
10504 initiate_write_filepage(pagedep, bp)
10505 	struct pagedep *pagedep;
10506 	struct buf *bp;
10507 {
10508 	struct jremref *jremref;
10509 	struct jmvref *jmvref;
10510 	struct dirrem *dirrem;
10511 	struct diradd *dap;
10512 	struct direct *ep;
10513 	int i;
10514 
10515 	if (pagedep->pd_state & IOSTARTED) {
10516 		/*
10517 		 * This can only happen if there is a driver that does not
10518 		 * understand chaining. Here biodone will reissue the call
10519 		 * to strategy for the incomplete buffers.
10520 		 */
10521 		printf("initiate_write_filepage: already started\n");
10522 		return;
10523 	}
10524 	pagedep->pd_state |= IOSTARTED;
10525 	/*
10526 	 * Wait for all journal remove dependencies to hit the disk.
10527 	 * We can not allow any potentially conflicting directory adds
10528 	 * to be visible before removes and rollback is too difficult.
10529 	 * The per-filesystem lock may be dropped and re-acquired, however
10530 	 * we hold the buf locked so the dependency can not go away.
10531 	 */
10532 	LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10533 		while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10534 			jwait(&jremref->jr_list, MNT_WAIT);
10535 	while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10536 		jwait(&jmvref->jm_list, MNT_WAIT);
10537 	for (i = 0; i < DAHASHSZ; i++) {
10538 		LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10539 			ep = (struct direct *)
10540 			    ((char *)bp->b_data + dap->da_offset);
10541 			if (ep->d_ino != dap->da_newinum)
10542 				panic("%s: dir inum %ju != new %ju",
10543 				    "initiate_write_filepage",
10544 				    (uintmax_t)ep->d_ino,
10545 				    (uintmax_t)dap->da_newinum);
10546 			if (dap->da_state & DIRCHG)
10547 				ep->d_ino = dap->da_previous->dm_oldinum;
10548 			else
10549 				ep->d_ino = 0;
10550 			dap->da_state &= ~ATTACHED;
10551 			dap->da_state |= UNDONE;
10552 		}
10553 	}
10554 }
10555 
10556 /*
10557  * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10558  * Note that any bug fixes made to this routine must be done in the
10559  * version found below.
10560  *
10561  * Called from within the procedure above to deal with unsatisfied
10562  * allocation dependencies in an inodeblock. The buffer must be
10563  * locked, thus, no I/O completion operations can occur while we
10564  * are manipulating its associated dependencies.
10565  */
10566 static void
10567 initiate_write_inodeblock_ufs1(inodedep, bp)
10568 	struct inodedep *inodedep;
10569 	struct buf *bp;			/* The inode block */
10570 {
10571 	struct allocdirect *adp, *lastadp;
10572 	struct ufs1_dinode *dp;
10573 	struct ufs1_dinode *sip;
10574 	struct inoref *inoref;
10575 	struct ufsmount *ump;
10576 	struct fs *fs;
10577 	ufs_lbn_t i;
10578 #ifdef INVARIANTS
10579 	ufs_lbn_t prevlbn = 0;
10580 #endif
10581 	int deplist;
10582 
10583 	if (inodedep->id_state & IOSTARTED)
10584 		panic("initiate_write_inodeblock_ufs1: already started");
10585 	inodedep->id_state |= IOSTARTED;
10586 	fs = inodedep->id_fs;
10587 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
10588 	LOCK_OWNED(ump);
10589 	dp = (struct ufs1_dinode *)bp->b_data +
10590 	    ino_to_fsbo(fs, inodedep->id_ino);
10591 
10592 	/*
10593 	 * If we're on the unlinked list but have not yet written our
10594 	 * next pointer initialize it here.
10595 	 */
10596 	if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10597 		struct inodedep *inon;
10598 
10599 		inon = TAILQ_NEXT(inodedep, id_unlinked);
10600 		dp->di_freelink = inon ? inon->id_ino : 0;
10601 	}
10602 	/*
10603 	 * If the bitmap is not yet written, then the allocated
10604 	 * inode cannot be written to disk.
10605 	 */
10606 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10607 		if (inodedep->id_savedino1 != NULL)
10608 			panic("initiate_write_inodeblock_ufs1: I/O underway");
10609 		FREE_LOCK(ump);
10610 		sip = malloc(sizeof(struct ufs1_dinode),
10611 		    M_SAVEDINO, M_SOFTDEP_FLAGS);
10612 		ACQUIRE_LOCK(ump);
10613 		inodedep->id_savedino1 = sip;
10614 		*inodedep->id_savedino1 = *dp;
10615 		bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10616 		dp->di_gen = inodedep->id_savedino1->di_gen;
10617 		dp->di_freelink = inodedep->id_savedino1->di_freelink;
10618 		return;
10619 	}
10620 	/*
10621 	 * If no dependencies, then there is nothing to roll back.
10622 	 */
10623 	inodedep->id_savedsize = dp->di_size;
10624 	inodedep->id_savedextsize = 0;
10625 	inodedep->id_savednlink = dp->di_nlink;
10626 	if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10627 	    TAILQ_EMPTY(&inodedep->id_inoreflst))
10628 		return;
10629 	/*
10630 	 * Revert the link count to that of the first unwritten journal entry.
10631 	 */
10632 	inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10633 	if (inoref)
10634 		dp->di_nlink = inoref->if_nlink;
10635 	/*
10636 	 * Set the dependencies to busy.
10637 	 */
10638 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10639 	     adp = TAILQ_NEXT(adp, ad_next)) {
10640 #ifdef INVARIANTS
10641 		if (deplist != 0 && prevlbn >= adp->ad_offset)
10642 			panic("softdep_write_inodeblock: lbn order");
10643 		prevlbn = adp->ad_offset;
10644 		if (adp->ad_offset < UFS_NDADDR &&
10645 		    dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10646 			panic("initiate_write_inodeblock_ufs1: "
10647 			    "direct pointer #%jd mismatch %d != %jd",
10648 			    (intmax_t)adp->ad_offset,
10649 			    dp->di_db[adp->ad_offset],
10650 			    (intmax_t)adp->ad_newblkno);
10651 		if (adp->ad_offset >= UFS_NDADDR &&
10652 		    dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10653 			panic("initiate_write_inodeblock_ufs1: "
10654 			    "indirect pointer #%jd mismatch %d != %jd",
10655 			    (intmax_t)adp->ad_offset - UFS_NDADDR,
10656 			    dp->di_ib[adp->ad_offset - UFS_NDADDR],
10657 			    (intmax_t)adp->ad_newblkno);
10658 		deplist |= 1 << adp->ad_offset;
10659 		if ((adp->ad_state & ATTACHED) == 0)
10660 			panic("initiate_write_inodeblock_ufs1: "
10661 			    "Unknown state 0x%x", adp->ad_state);
10662 #endif /* INVARIANTS */
10663 		adp->ad_state &= ~ATTACHED;
10664 		adp->ad_state |= UNDONE;
10665 	}
10666 	/*
10667 	 * The on-disk inode cannot claim to be any larger than the last
10668 	 * fragment that has been written. Otherwise, the on-disk inode
10669 	 * might have fragments that were not the last block in the file
10670 	 * which would corrupt the filesystem.
10671 	 */
10672 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10673 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10674 		if (adp->ad_offset >= UFS_NDADDR)
10675 			break;
10676 		dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10677 		/* keep going until hitting a rollback to a frag */
10678 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10679 			continue;
10680 		dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10681 		for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10682 #ifdef INVARIANTS
10683 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10684 				panic("initiate_write_inodeblock_ufs1: "
10685 				    "lost dep1");
10686 #endif /* INVARIANTS */
10687 			dp->di_db[i] = 0;
10688 		}
10689 		for (i = 0; i < UFS_NIADDR; i++) {
10690 #ifdef INVARIANTS
10691 			if (dp->di_ib[i] != 0 &&
10692 			    (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10693 				panic("initiate_write_inodeblock_ufs1: "
10694 				    "lost dep2");
10695 #endif /* INVARIANTS */
10696 			dp->di_ib[i] = 0;
10697 		}
10698 		return;
10699 	}
10700 	/*
10701 	 * If we have zero'ed out the last allocated block of the file,
10702 	 * roll back the size to the last currently allocated block.
10703 	 * We know that this last allocated block is a full-sized as
10704 	 * we already checked for fragments in the loop above.
10705 	 */
10706 	if (lastadp != NULL &&
10707 	    dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10708 		for (i = lastadp->ad_offset; i >= 0; i--)
10709 			if (dp->di_db[i] != 0)
10710 				break;
10711 		dp->di_size = (i + 1) * fs->fs_bsize;
10712 	}
10713 	/*
10714 	 * The only dependencies are for indirect blocks.
10715 	 *
10716 	 * The file size for indirect block additions is not guaranteed.
10717 	 * Such a guarantee would be non-trivial to achieve. The conventional
10718 	 * synchronous write implementation also does not make this guarantee.
10719 	 * Fsck should catch and fix discrepancies. Arguably, the file size
10720 	 * can be over-estimated without destroying integrity when the file
10721 	 * moves into the indirect blocks (i.e., is large). If we want to
10722 	 * postpone fsck, we are stuck with this argument.
10723 	 */
10724 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10725 		dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10726 }
10727 
10728 /*
10729  * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10730  * Note that any bug fixes made to this routine must be done in the
10731  * version found above.
10732  *
10733  * Called from within the procedure above to deal with unsatisfied
10734  * allocation dependencies in an inodeblock. The buffer must be
10735  * locked, thus, no I/O completion operations can occur while we
10736  * are manipulating its associated dependencies.
10737  */
10738 static void
10739 initiate_write_inodeblock_ufs2(inodedep, bp)
10740 	struct inodedep *inodedep;
10741 	struct buf *bp;			/* The inode block */
10742 {
10743 	struct allocdirect *adp, *lastadp;
10744 	struct ufs2_dinode *dp;
10745 	struct ufs2_dinode *sip;
10746 	struct inoref *inoref;
10747 	struct ufsmount *ump;
10748 	struct fs *fs;
10749 	ufs_lbn_t i;
10750 #ifdef INVARIANTS
10751 	ufs_lbn_t prevlbn = 0;
10752 #endif
10753 	int deplist;
10754 
10755 	if (inodedep->id_state & IOSTARTED)
10756 		panic("initiate_write_inodeblock_ufs2: already started");
10757 	inodedep->id_state |= IOSTARTED;
10758 	fs = inodedep->id_fs;
10759 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
10760 	LOCK_OWNED(ump);
10761 	dp = (struct ufs2_dinode *)bp->b_data +
10762 	    ino_to_fsbo(fs, inodedep->id_ino);
10763 
10764 	/*
10765 	 * If we're on the unlinked list but have not yet written our
10766 	 * next pointer initialize it here.
10767 	 */
10768 	if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10769 		struct inodedep *inon;
10770 
10771 		inon = TAILQ_NEXT(inodedep, id_unlinked);
10772 		dp->di_freelink = inon ? inon->id_ino : 0;
10773 		ffs_update_dinode_ckhash(fs, dp);
10774 	}
10775 	/*
10776 	 * If the bitmap is not yet written, then the allocated
10777 	 * inode cannot be written to disk.
10778 	 */
10779 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10780 		if (inodedep->id_savedino2 != NULL)
10781 			panic("initiate_write_inodeblock_ufs2: I/O underway");
10782 		FREE_LOCK(ump);
10783 		sip = malloc(sizeof(struct ufs2_dinode),
10784 		    M_SAVEDINO, M_SOFTDEP_FLAGS);
10785 		ACQUIRE_LOCK(ump);
10786 		inodedep->id_savedino2 = sip;
10787 		*inodedep->id_savedino2 = *dp;
10788 		bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10789 		dp->di_gen = inodedep->id_savedino2->di_gen;
10790 		dp->di_freelink = inodedep->id_savedino2->di_freelink;
10791 		return;
10792 	}
10793 	/*
10794 	 * If no dependencies, then there is nothing to roll back.
10795 	 */
10796 	inodedep->id_savedsize = dp->di_size;
10797 	inodedep->id_savedextsize = dp->di_extsize;
10798 	inodedep->id_savednlink = dp->di_nlink;
10799 	if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10800 	    TAILQ_EMPTY(&inodedep->id_extupdt) &&
10801 	    TAILQ_EMPTY(&inodedep->id_inoreflst))
10802 		return;
10803 	/*
10804 	 * Revert the link count to that of the first unwritten journal entry.
10805 	 */
10806 	inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10807 	if (inoref)
10808 		dp->di_nlink = inoref->if_nlink;
10809 
10810 	/*
10811 	 * Set the ext data dependencies to busy.
10812 	 */
10813 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10814 	     adp = TAILQ_NEXT(adp, ad_next)) {
10815 #ifdef INVARIANTS
10816 		if (deplist != 0 && prevlbn >= adp->ad_offset)
10817 			panic("initiate_write_inodeblock_ufs2: lbn order");
10818 		prevlbn = adp->ad_offset;
10819 		if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10820 			panic("initiate_write_inodeblock_ufs2: "
10821 			    "ext pointer #%jd mismatch %jd != %jd",
10822 			    (intmax_t)adp->ad_offset,
10823 			    (intmax_t)dp->di_extb[adp->ad_offset],
10824 			    (intmax_t)adp->ad_newblkno);
10825 		deplist |= 1 << adp->ad_offset;
10826 		if ((adp->ad_state & ATTACHED) == 0)
10827 			panic("initiate_write_inodeblock_ufs2: Unknown "
10828 			    "state 0x%x", adp->ad_state);
10829 #endif /* INVARIANTS */
10830 		adp->ad_state &= ~ATTACHED;
10831 		adp->ad_state |= UNDONE;
10832 	}
10833 	/*
10834 	 * The on-disk inode cannot claim to be any larger than the last
10835 	 * fragment that has been written. Otherwise, the on-disk inode
10836 	 * might have fragments that were not the last block in the ext
10837 	 * data which would corrupt the filesystem.
10838 	 */
10839 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10840 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10841 		dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10842 		/* keep going until hitting a rollback to a frag */
10843 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10844 			continue;
10845 		dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10846 		for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10847 #ifdef INVARIANTS
10848 			if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10849 				panic("initiate_write_inodeblock_ufs2: "
10850 				    "lost dep1");
10851 #endif /* INVARIANTS */
10852 			dp->di_extb[i] = 0;
10853 		}
10854 		lastadp = NULL;
10855 		break;
10856 	}
10857 	/*
10858 	 * If we have zero'ed out the last allocated block of the ext
10859 	 * data, roll back the size to the last currently allocated block.
10860 	 * We know that this last allocated block is a full-sized as
10861 	 * we already checked for fragments in the loop above.
10862 	 */
10863 	if (lastadp != NULL &&
10864 	    dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10865 		for (i = lastadp->ad_offset; i >= 0; i--)
10866 			if (dp->di_extb[i] != 0)
10867 				break;
10868 		dp->di_extsize = (i + 1) * fs->fs_bsize;
10869 	}
10870 	/*
10871 	 * Set the file data dependencies to busy.
10872 	 */
10873 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10874 	     adp = TAILQ_NEXT(adp, ad_next)) {
10875 #ifdef INVARIANTS
10876 		if (deplist != 0 && prevlbn >= adp->ad_offset)
10877 			panic("softdep_write_inodeblock: lbn order");
10878 		if ((adp->ad_state & ATTACHED) == 0)
10879 			panic("inodedep %p and adp %p not attached", inodedep, adp);
10880 		prevlbn = adp->ad_offset;
10881 		if (!ffs_fsfail_cleanup(ump, 0) &&
10882 		    adp->ad_offset < UFS_NDADDR &&
10883 		    dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10884 			panic("initiate_write_inodeblock_ufs2: "
10885 			    "direct pointer #%jd mismatch %jd != %jd",
10886 			    (intmax_t)adp->ad_offset,
10887 			    (intmax_t)dp->di_db[adp->ad_offset],
10888 			    (intmax_t)adp->ad_newblkno);
10889 		if (!ffs_fsfail_cleanup(ump, 0) &&
10890 		    adp->ad_offset >= UFS_NDADDR &&
10891 		    dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10892 			panic("initiate_write_inodeblock_ufs2: "
10893 			    "indirect pointer #%jd mismatch %jd != %jd",
10894 			    (intmax_t)adp->ad_offset - UFS_NDADDR,
10895 			    (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10896 			    (intmax_t)adp->ad_newblkno);
10897 		deplist |= 1 << adp->ad_offset;
10898 		if ((adp->ad_state & ATTACHED) == 0)
10899 			panic("initiate_write_inodeblock_ufs2: Unknown "
10900 			     "state 0x%x", adp->ad_state);
10901 #endif /* INVARIANTS */
10902 		adp->ad_state &= ~ATTACHED;
10903 		adp->ad_state |= UNDONE;
10904 	}
10905 	/*
10906 	 * The on-disk inode cannot claim to be any larger than the last
10907 	 * fragment that has been written. Otherwise, the on-disk inode
10908 	 * might have fragments that were not the last block in the file
10909 	 * which would corrupt the filesystem.
10910 	 */
10911 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10912 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10913 		if (adp->ad_offset >= UFS_NDADDR)
10914 			break;
10915 		dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10916 		/* keep going until hitting a rollback to a frag */
10917 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10918 			continue;
10919 		dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10920 		for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10921 #ifdef INVARIANTS
10922 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10923 				panic("initiate_write_inodeblock_ufs2: "
10924 				    "lost dep2");
10925 #endif /* INVARIANTS */
10926 			dp->di_db[i] = 0;
10927 		}
10928 		for (i = 0; i < UFS_NIADDR; i++) {
10929 #ifdef INVARIANTS
10930 			if (dp->di_ib[i] != 0 &&
10931 			    (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10932 				panic("initiate_write_inodeblock_ufs2: "
10933 				    "lost dep3");
10934 #endif /* INVARIANTS */
10935 			dp->di_ib[i] = 0;
10936 		}
10937 		ffs_update_dinode_ckhash(fs, dp);
10938 		return;
10939 	}
10940 	/*
10941 	 * If we have zero'ed out the last allocated block of the file,
10942 	 * roll back the size to the last currently allocated block.
10943 	 * We know that this last allocated block is a full-sized as
10944 	 * we already checked for fragments in the loop above.
10945 	 */
10946 	if (lastadp != NULL &&
10947 	    dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10948 		for (i = lastadp->ad_offset; i >= 0; i--)
10949 			if (dp->di_db[i] != 0)
10950 				break;
10951 		dp->di_size = (i + 1) * fs->fs_bsize;
10952 	}
10953 	/*
10954 	 * The only dependencies are for indirect blocks.
10955 	 *
10956 	 * The file size for indirect block additions is not guaranteed.
10957 	 * Such a guarantee would be non-trivial to achieve. The conventional
10958 	 * synchronous write implementation also does not make this guarantee.
10959 	 * Fsck should catch and fix discrepancies. Arguably, the file size
10960 	 * can be over-estimated without destroying integrity when the file
10961 	 * moves into the indirect blocks (i.e., is large). If we want to
10962 	 * postpone fsck, we are stuck with this argument.
10963 	 */
10964 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10965 		dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10966 	ffs_update_dinode_ckhash(fs, dp);
10967 }
10968 
10969 /*
10970  * Cancel an indirdep as a result of truncation.  Release all of the
10971  * children allocindirs and place their journal work on the appropriate
10972  * list.
10973  */
10974 static void
10975 cancel_indirdep(indirdep, bp, freeblks)
10976 	struct indirdep *indirdep;
10977 	struct buf *bp;
10978 	struct freeblks *freeblks;
10979 {
10980 	struct allocindir *aip;
10981 
10982 	/*
10983 	 * None of the indirect pointers will ever be visible,
10984 	 * so they can simply be tossed. GOINGAWAY ensures
10985 	 * that allocated pointers will be saved in the buffer
10986 	 * cache until they are freed. Note that they will
10987 	 * only be able to be found by their physical address
10988 	 * since the inode mapping the logical address will
10989 	 * be gone. The save buffer used for the safe copy
10990 	 * was allocated in setup_allocindir_phase2 using
10991 	 * the physical address so it could be used for this
10992 	 * purpose. Hence we swap the safe copy with the real
10993 	 * copy, allowing the safe copy to be freed and holding
10994 	 * on to the real copy for later use in indir_trunc.
10995 	 */
10996 	if (indirdep->ir_state & GOINGAWAY)
10997 		panic("cancel_indirdep: already gone");
10998 	if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10999 		indirdep->ir_state |= DEPCOMPLETE;
11000 		LIST_REMOVE(indirdep, ir_next);
11001 	}
11002 	indirdep->ir_state |= GOINGAWAY;
11003 	/*
11004 	 * Pass in bp for blocks still have journal writes
11005 	 * pending so we can cancel them on their own.
11006 	 */
11007 	while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
11008 		cancel_allocindir(aip, bp, freeblks, 0);
11009 	while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
11010 		cancel_allocindir(aip, NULL, freeblks, 0);
11011 	while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
11012 		cancel_allocindir(aip, NULL, freeblks, 0);
11013 	while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
11014 		cancel_allocindir(aip, NULL, freeblks, 0);
11015 	/*
11016 	 * If there are pending partial truncations we need to keep the
11017 	 * old block copy around until they complete.  This is because
11018 	 * the current b_data is not a perfect superset of the available
11019 	 * blocks.
11020 	 */
11021 	if (TAILQ_EMPTY(&indirdep->ir_trunc))
11022 		bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
11023 	else
11024 		bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
11025 	WORKLIST_REMOVE(&indirdep->ir_list);
11026 	WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
11027 	indirdep->ir_bp = NULL;
11028 	indirdep->ir_freeblks = freeblks;
11029 }
11030 
11031 /*
11032  * Free an indirdep once it no longer has new pointers to track.
11033  */
11034 static void
11035 free_indirdep(indirdep)
11036 	struct indirdep *indirdep;
11037 {
11038 
11039 	KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
11040 	    ("free_indirdep: Indir trunc list not empty."));
11041 	KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
11042 	    ("free_indirdep: Complete head not empty."));
11043 	KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
11044 	    ("free_indirdep: write head not empty."));
11045 	KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
11046 	    ("free_indirdep: done head not empty."));
11047 	KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
11048 	    ("free_indirdep: deplist head not empty."));
11049 	KASSERT((indirdep->ir_state & DEPCOMPLETE),
11050 	    ("free_indirdep: %p still on newblk list.", indirdep));
11051 	KASSERT(indirdep->ir_saveddata == NULL,
11052 	    ("free_indirdep: %p still has saved data.", indirdep));
11053 	KASSERT(indirdep->ir_savebp == NULL,
11054 	    ("free_indirdep: %p still has savebp buffer.", indirdep));
11055 	if (indirdep->ir_state & ONWORKLIST)
11056 		WORKLIST_REMOVE(&indirdep->ir_list);
11057 	WORKITEM_FREE(indirdep, D_INDIRDEP);
11058 }
11059 
11060 /*
11061  * Called before a write to an indirdep.  This routine is responsible for
11062  * rolling back pointers to a safe state which includes only those
11063  * allocindirs which have been completed.
11064  */
11065 static void
11066 initiate_write_indirdep(indirdep, bp)
11067 	struct indirdep *indirdep;
11068 	struct buf *bp;
11069 {
11070 	struct ufsmount *ump;
11071 
11072 	indirdep->ir_state |= IOSTARTED;
11073 	if (indirdep->ir_state & GOINGAWAY)
11074 		panic("disk_io_initiation: indirdep gone");
11075 	/*
11076 	 * If there are no remaining dependencies, this will be writing
11077 	 * the real pointers.
11078 	 */
11079 	if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
11080 	    TAILQ_EMPTY(&indirdep->ir_trunc))
11081 		return;
11082 	/*
11083 	 * Replace up-to-date version with safe version.
11084 	 */
11085 	if (indirdep->ir_saveddata == NULL) {
11086 		ump = VFSTOUFS(indirdep->ir_list.wk_mp);
11087 		LOCK_OWNED(ump);
11088 		FREE_LOCK(ump);
11089 		indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
11090 		    M_SOFTDEP_FLAGS);
11091 		ACQUIRE_LOCK(ump);
11092 	}
11093 	indirdep->ir_state &= ~ATTACHED;
11094 	indirdep->ir_state |= UNDONE;
11095 	bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
11096 	bcopy(indirdep->ir_savebp->b_data, bp->b_data,
11097 	    bp->b_bcount);
11098 }
11099 
11100 /*
11101  * Called when an inode has been cleared in a cg bitmap.  This finally
11102  * eliminates any canceled jaddrefs
11103  */
11104 void
11105 softdep_setup_inofree(mp, bp, ino, wkhd)
11106 	struct mount *mp;
11107 	struct buf *bp;
11108 	ino_t ino;
11109 	struct workhead *wkhd;
11110 {
11111 	struct worklist *wk, *wkn;
11112 	struct inodedep *inodedep;
11113 	struct ufsmount *ump;
11114 	uint8_t *inosused;
11115 	struct cg *cgp;
11116 	struct fs *fs;
11117 
11118 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
11119 	    ("softdep_setup_inofree called on non-softdep filesystem"));
11120 	ump = VFSTOUFS(mp);
11121 	ACQUIRE_LOCK(ump);
11122 	if (!ffs_fsfail_cleanup(ump, 0)) {
11123 		fs = ump->um_fs;
11124 		cgp = (struct cg *)bp->b_data;
11125 		inosused = cg_inosused(cgp);
11126 		if (isset(inosused, ino % fs->fs_ipg))
11127 			panic("softdep_setup_inofree: inode %ju not freed.",
11128 			    (uintmax_t)ino);
11129 	}
11130 	if (inodedep_lookup(mp, ino, 0, &inodedep))
11131 		panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
11132 		    (uintmax_t)ino, inodedep);
11133 	if (wkhd) {
11134 		LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
11135 			if (wk->wk_type != D_JADDREF)
11136 				continue;
11137 			WORKLIST_REMOVE(wk);
11138 			/*
11139 			 * We can free immediately even if the jaddref
11140 			 * isn't attached in a background write as now
11141 			 * the bitmaps are reconciled.
11142 			 */
11143 			wk->wk_state |= COMPLETE | ATTACHED;
11144 			free_jaddref(WK_JADDREF(wk));
11145 		}
11146 		jwork_move(&bp->b_dep, wkhd);
11147 	}
11148 	FREE_LOCK(ump);
11149 }
11150 
11151 /*
11152  * Called via ffs_blkfree() after a set of frags has been cleared from a cg
11153  * map.  Any dependencies waiting for the write to clear are added to the
11154  * buf's list and any jnewblks that are being canceled are discarded
11155  * immediately.
11156  */
11157 void
11158 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
11159 	struct mount *mp;
11160 	struct buf *bp;
11161 	ufs2_daddr_t blkno;
11162 	int frags;
11163 	struct workhead *wkhd;
11164 {
11165 	struct bmsafemap *bmsafemap;
11166 	struct jnewblk *jnewblk;
11167 	struct ufsmount *ump;
11168 	struct worklist *wk;
11169 	struct fs *fs;
11170 #ifdef INVARIANTS
11171 	uint8_t *blksfree;
11172 	struct cg *cgp;
11173 	ufs2_daddr_t jstart;
11174 	ufs2_daddr_t jend;
11175 	ufs2_daddr_t end;
11176 	long bno;
11177 	int i;
11178 #endif
11179 
11180 	CTR3(KTR_SUJ,
11181 	    "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
11182 	    blkno, frags, wkhd);
11183 
11184 	ump = VFSTOUFS(mp);
11185 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
11186 	    ("softdep_setup_blkfree called on non-softdep filesystem"));
11187 	ACQUIRE_LOCK(ump);
11188 	/* Lookup the bmsafemap so we track when it is dirty. */
11189 	fs = ump->um_fs;
11190 	bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11191 	/*
11192 	 * Detach any jnewblks which have been canceled.  They must linger
11193 	 * until the bitmap is cleared again by ffs_blkfree() to prevent
11194 	 * an unjournaled allocation from hitting the disk.
11195 	 */
11196 	if (wkhd) {
11197 		while ((wk = LIST_FIRST(wkhd)) != NULL) {
11198 			CTR2(KTR_SUJ,
11199 			    "softdep_setup_blkfree: blkno %jd wk type %d",
11200 			    blkno, wk->wk_type);
11201 			WORKLIST_REMOVE(wk);
11202 			if (wk->wk_type != D_JNEWBLK) {
11203 				WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
11204 				continue;
11205 			}
11206 			jnewblk = WK_JNEWBLK(wk);
11207 			KASSERT(jnewblk->jn_state & GOINGAWAY,
11208 			    ("softdep_setup_blkfree: jnewblk not canceled."));
11209 #ifdef INVARIANTS
11210 			/*
11211 			 * Assert that this block is free in the bitmap
11212 			 * before we discard the jnewblk.
11213 			 */
11214 			cgp = (struct cg *)bp->b_data;
11215 			blksfree = cg_blksfree(cgp);
11216 			bno = dtogd(fs, jnewblk->jn_blkno);
11217 			for (i = jnewblk->jn_oldfrags;
11218 			    i < jnewblk->jn_frags; i++) {
11219 				if (isset(blksfree, bno + i))
11220 					continue;
11221 				panic("softdep_setup_blkfree: not free");
11222 			}
11223 #endif
11224 			/*
11225 			 * Even if it's not attached we can free immediately
11226 			 * as the new bitmap is correct.
11227 			 */
11228 			wk->wk_state |= COMPLETE | ATTACHED;
11229 			free_jnewblk(jnewblk);
11230 		}
11231 	}
11232 
11233 #ifdef INVARIANTS
11234 	/*
11235 	 * Assert that we are not freeing a block which has an outstanding
11236 	 * allocation dependency.
11237 	 */
11238 	fs = VFSTOUFS(mp)->um_fs;
11239 	bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11240 	end = blkno + frags;
11241 	LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11242 		/*
11243 		 * Don't match against blocks that will be freed when the
11244 		 * background write is done.
11245 		 */
11246 		if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
11247 		    (COMPLETE | DEPCOMPLETE))
11248 			continue;
11249 		jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
11250 		jend = jnewblk->jn_blkno + jnewblk->jn_frags;
11251 		if ((blkno >= jstart && blkno < jend) ||
11252 		    (end > jstart && end <= jend)) {
11253 			printf("state 0x%X %jd - %d %d dep %p\n",
11254 			    jnewblk->jn_state, jnewblk->jn_blkno,
11255 			    jnewblk->jn_oldfrags, jnewblk->jn_frags,
11256 			    jnewblk->jn_dep);
11257 			panic("softdep_setup_blkfree: "
11258 			    "%jd-%jd(%d) overlaps with %jd-%jd",
11259 			    blkno, end, frags, jstart, jend);
11260 		}
11261 	}
11262 #endif
11263 	FREE_LOCK(ump);
11264 }
11265 
11266 /*
11267  * Revert a block allocation when the journal record that describes it
11268  * is not yet written.
11269  */
11270 static int
11271 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
11272 	struct jnewblk *jnewblk;
11273 	struct fs *fs;
11274 	struct cg *cgp;
11275 	uint8_t *blksfree;
11276 {
11277 	ufs1_daddr_t fragno;
11278 	long cgbno, bbase;
11279 	int frags, blk;
11280 	int i;
11281 
11282 	frags = 0;
11283 	cgbno = dtogd(fs, jnewblk->jn_blkno);
11284 	/*
11285 	 * We have to test which frags need to be rolled back.  We may
11286 	 * be operating on a stale copy when doing background writes.
11287 	 */
11288 	for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
11289 		if (isclr(blksfree, cgbno + i))
11290 			frags++;
11291 	if (frags == 0)
11292 		return (0);
11293 	/*
11294 	 * This is mostly ffs_blkfree() sans some validation and
11295 	 * superblock updates.
11296 	 */
11297 	if (frags == fs->fs_frag) {
11298 		fragno = fragstoblks(fs, cgbno);
11299 		ffs_setblock(fs, blksfree, fragno);
11300 		ffs_clusteracct(fs, cgp, fragno, 1);
11301 		cgp->cg_cs.cs_nbfree++;
11302 	} else {
11303 		cgbno += jnewblk->jn_oldfrags;
11304 		bbase = cgbno - fragnum(fs, cgbno);
11305 		/* Decrement the old frags.  */
11306 		blk = blkmap(fs, blksfree, bbase);
11307 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11308 		/* Deallocate the fragment */
11309 		for (i = 0; i < frags; i++)
11310 			setbit(blksfree, cgbno + i);
11311 		cgp->cg_cs.cs_nffree += frags;
11312 		/* Add back in counts associated with the new frags */
11313 		blk = blkmap(fs, blksfree, bbase);
11314 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11315 		/* If a complete block has been reassembled, account for it. */
11316 		fragno = fragstoblks(fs, bbase);
11317 		if (ffs_isblock(fs, blksfree, fragno)) {
11318 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
11319 			ffs_clusteracct(fs, cgp, fragno, 1);
11320 			cgp->cg_cs.cs_nbfree++;
11321 		}
11322 	}
11323 	stat_jnewblk++;
11324 	jnewblk->jn_state &= ~ATTACHED;
11325 	jnewblk->jn_state |= UNDONE;
11326 
11327 	return (frags);
11328 }
11329 
11330 static void
11331 initiate_write_bmsafemap(bmsafemap, bp)
11332 	struct bmsafemap *bmsafemap;
11333 	struct buf *bp;			/* The cg block. */
11334 {
11335 	struct jaddref *jaddref;
11336 	struct jnewblk *jnewblk;
11337 	uint8_t *inosused;
11338 	uint8_t *blksfree;
11339 	struct cg *cgp;
11340 	struct fs *fs;
11341 	ino_t ino;
11342 
11343 	/*
11344 	 * If this is a background write, we did this at the time that
11345 	 * the copy was made, so do not need to do it again.
11346 	 */
11347 	if (bmsafemap->sm_state & IOSTARTED)
11348 		return;
11349 	bmsafemap->sm_state |= IOSTARTED;
11350 	/*
11351 	 * Clear any inode allocations which are pending journal writes.
11352 	 */
11353 	if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11354 		cgp = (struct cg *)bp->b_data;
11355 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11356 		inosused = cg_inosused(cgp);
11357 		LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11358 			ino = jaddref->ja_ino % fs->fs_ipg;
11359 			if (isset(inosused, ino)) {
11360 				if ((jaddref->ja_mode & IFMT) == IFDIR)
11361 					cgp->cg_cs.cs_ndir--;
11362 				cgp->cg_cs.cs_nifree++;
11363 				clrbit(inosused, ino);
11364 				jaddref->ja_state &= ~ATTACHED;
11365 				jaddref->ja_state |= UNDONE;
11366 				stat_jaddref++;
11367 			} else
11368 				panic("initiate_write_bmsafemap: inode %ju "
11369 				    "marked free", (uintmax_t)jaddref->ja_ino);
11370 		}
11371 	}
11372 	/*
11373 	 * Clear any block allocations which are pending journal writes.
11374 	 */
11375 	if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11376 		cgp = (struct cg *)bp->b_data;
11377 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11378 		blksfree = cg_blksfree(cgp);
11379 		LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11380 			if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11381 				continue;
11382 			panic("initiate_write_bmsafemap: block %jd "
11383 			    "marked free", jnewblk->jn_blkno);
11384 		}
11385 	}
11386 	/*
11387 	 * Move allocation lists to the written lists so they can be
11388 	 * cleared once the block write is complete.
11389 	 */
11390 	LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11391 	    inodedep, id_deps);
11392 	LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11393 	    newblk, nb_deps);
11394 	LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11395 	    wk_list);
11396 }
11397 
11398 void
11399 softdep_handle_error(struct buf *bp)
11400 {
11401 	struct ufsmount *ump;
11402 
11403 	ump = softdep_bp_to_mp(bp);
11404 	if (ump == NULL)
11405 		return;
11406 
11407 	if (ffs_fsfail_cleanup(ump, bp->b_error)) {
11408 		/*
11409 		 * No future writes will succeed, so the on-disk image is safe.
11410 		 * Pretend that this write succeeded so that the softdep state
11411 		 * will be cleaned up naturally.
11412 		 */
11413 		bp->b_ioflags &= ~BIO_ERROR;
11414 		bp->b_error = 0;
11415 	}
11416 }
11417 
11418 /*
11419  * This routine is called during the completion interrupt
11420  * service routine for a disk write (from the procedure called
11421  * by the device driver to inform the filesystem caches of
11422  * a request completion).  It should be called early in this
11423  * procedure, before the block is made available to other
11424  * processes or other routines are called.
11425  *
11426  */
11427 static void
11428 softdep_disk_write_complete(bp)
11429 	struct buf *bp;		/* describes the completed disk write */
11430 {
11431 	struct worklist *wk;
11432 	struct worklist *owk;
11433 	struct ufsmount *ump;
11434 	struct workhead reattach;
11435 	struct freeblks *freeblks;
11436 	struct buf *sbp;
11437 
11438 	ump = softdep_bp_to_mp(bp);
11439 	KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11440 	    ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11441 	     "with outstanding dependencies for buffer %p", bp));
11442 	if (ump == NULL)
11443 		return;
11444 	if ((bp->b_ioflags & BIO_ERROR) != 0)
11445 		softdep_handle_error(bp);
11446 	/*
11447 	 * If an error occurred while doing the write, then the data
11448 	 * has not hit the disk and the dependencies cannot be processed.
11449 	 * But we do have to go through and roll forward any dependencies
11450 	 * that were rolled back before the disk write.
11451 	 */
11452 	sbp = NULL;
11453 	ACQUIRE_LOCK(ump);
11454 	if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11455 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11456 			switch (wk->wk_type) {
11457 			case D_PAGEDEP:
11458 				handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11459 				continue;
11460 
11461 			case D_INODEDEP:
11462 				handle_written_inodeblock(WK_INODEDEP(wk),
11463 				    bp, 0);
11464 				continue;
11465 
11466 			case D_BMSAFEMAP:
11467 				handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11468 				    bp, 0);
11469 				continue;
11470 
11471 			case D_INDIRDEP:
11472 				handle_written_indirdep(WK_INDIRDEP(wk),
11473 				    bp, &sbp, 0);
11474 				continue;
11475 			default:
11476 				/* nothing to roll forward */
11477 				continue;
11478 			}
11479 		}
11480 		FREE_LOCK(ump);
11481 		if (sbp)
11482 			brelse(sbp);
11483 		return;
11484 	}
11485 	LIST_INIT(&reattach);
11486 
11487 	/*
11488 	 * Ump SU lock must not be released anywhere in this code segment.
11489 	 */
11490 	owk = NULL;
11491 	while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11492 		WORKLIST_REMOVE(wk);
11493 		atomic_add_long(&dep_write[wk->wk_type], 1);
11494 		if (wk == owk)
11495 			panic("duplicate worklist: %p\n", wk);
11496 		owk = wk;
11497 		switch (wk->wk_type) {
11498 		case D_PAGEDEP:
11499 			if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11500 			    WRITESUCCEEDED))
11501 				WORKLIST_INSERT(&reattach, wk);
11502 			continue;
11503 
11504 		case D_INODEDEP:
11505 			if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11506 			    WRITESUCCEEDED))
11507 				WORKLIST_INSERT(&reattach, wk);
11508 			continue;
11509 
11510 		case D_BMSAFEMAP:
11511 			if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11512 			    WRITESUCCEEDED))
11513 				WORKLIST_INSERT(&reattach, wk);
11514 			continue;
11515 
11516 		case D_MKDIR:
11517 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11518 			continue;
11519 
11520 		case D_ALLOCDIRECT:
11521 			wk->wk_state |= COMPLETE;
11522 			handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11523 			continue;
11524 
11525 		case D_ALLOCINDIR:
11526 			wk->wk_state |= COMPLETE;
11527 			handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11528 			continue;
11529 
11530 		case D_INDIRDEP:
11531 			if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11532 			    WRITESUCCEEDED))
11533 				WORKLIST_INSERT(&reattach, wk);
11534 			continue;
11535 
11536 		case D_FREEBLKS:
11537 			wk->wk_state |= COMPLETE;
11538 			freeblks = WK_FREEBLKS(wk);
11539 			if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11540 			    LIST_EMPTY(&freeblks->fb_jblkdephd))
11541 				add_to_worklist(wk, WK_NODELAY);
11542 			continue;
11543 
11544 		case D_FREEWORK:
11545 			handle_written_freework(WK_FREEWORK(wk));
11546 			break;
11547 
11548 		case D_JSEGDEP:
11549 			free_jsegdep(WK_JSEGDEP(wk));
11550 			continue;
11551 
11552 		case D_JSEG:
11553 			handle_written_jseg(WK_JSEG(wk), bp);
11554 			continue;
11555 
11556 		case D_SBDEP:
11557 			if (handle_written_sbdep(WK_SBDEP(wk), bp))
11558 				WORKLIST_INSERT(&reattach, wk);
11559 			continue;
11560 
11561 		case D_FREEDEP:
11562 			free_freedep(WK_FREEDEP(wk));
11563 			continue;
11564 
11565 		default:
11566 			panic("handle_disk_write_complete: Unknown type %s",
11567 			    TYPENAME(wk->wk_type));
11568 			/* NOTREACHED */
11569 		}
11570 	}
11571 	/*
11572 	 * Reattach any requests that must be redone.
11573 	 */
11574 	while ((wk = LIST_FIRST(&reattach)) != NULL) {
11575 		WORKLIST_REMOVE(wk);
11576 		WORKLIST_INSERT(&bp->b_dep, wk);
11577 	}
11578 	FREE_LOCK(ump);
11579 	if (sbp)
11580 		brelse(sbp);
11581 }
11582 
11583 /*
11584  * Called from within softdep_disk_write_complete above.
11585  */
11586 static void
11587 handle_allocdirect_partdone(adp, wkhd)
11588 	struct allocdirect *adp;	/* the completed allocdirect */
11589 	struct workhead *wkhd;		/* Work to do when inode is writtne. */
11590 {
11591 	struct allocdirectlst *listhead;
11592 	struct allocdirect *listadp;
11593 	struct inodedep *inodedep;
11594 	long bsize;
11595 
11596 	LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11597 	if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11598 		return;
11599 	/*
11600 	 * The on-disk inode cannot claim to be any larger than the last
11601 	 * fragment that has been written. Otherwise, the on-disk inode
11602 	 * might have fragments that were not the last block in the file
11603 	 * which would corrupt the filesystem. Thus, we cannot free any
11604 	 * allocdirects after one whose ad_oldblkno claims a fragment as
11605 	 * these blocks must be rolled back to zero before writing the inode.
11606 	 * We check the currently active set of allocdirects in id_inoupdt
11607 	 * or id_extupdt as appropriate.
11608 	 */
11609 	inodedep = adp->ad_inodedep;
11610 	bsize = inodedep->id_fs->fs_bsize;
11611 	if (adp->ad_state & EXTDATA)
11612 		listhead = &inodedep->id_extupdt;
11613 	else
11614 		listhead = &inodedep->id_inoupdt;
11615 	TAILQ_FOREACH(listadp, listhead, ad_next) {
11616 		/* found our block */
11617 		if (listadp == adp)
11618 			break;
11619 		/* continue if ad_oldlbn is not a fragment */
11620 		if (listadp->ad_oldsize == 0 ||
11621 		    listadp->ad_oldsize == bsize)
11622 			continue;
11623 		/* hit a fragment */
11624 		return;
11625 	}
11626 	/*
11627 	 * If we have reached the end of the current list without
11628 	 * finding the just finished dependency, then it must be
11629 	 * on the future dependency list. Future dependencies cannot
11630 	 * be freed until they are moved to the current list.
11631 	 */
11632 	if (listadp == NULL) {
11633 #ifdef INVARIANTS
11634 		if (adp->ad_state & EXTDATA)
11635 			listhead = &inodedep->id_newextupdt;
11636 		else
11637 			listhead = &inodedep->id_newinoupdt;
11638 		TAILQ_FOREACH(listadp, listhead, ad_next)
11639 			/* found our block */
11640 			if (listadp == adp)
11641 				break;
11642 		if (listadp == NULL)
11643 			panic("handle_allocdirect_partdone: lost dep");
11644 #endif /* INVARIANTS */
11645 		return;
11646 	}
11647 	/*
11648 	 * If we have found the just finished dependency, then queue
11649 	 * it along with anything that follows it that is complete.
11650 	 * Since the pointer has not yet been written in the inode
11651 	 * as the dependency prevents it, place the allocdirect on the
11652 	 * bufwait list where it will be freed once the pointer is
11653 	 * valid.
11654 	 */
11655 	if (wkhd == NULL)
11656 		wkhd = &inodedep->id_bufwait;
11657 	for (; adp; adp = listadp) {
11658 		listadp = TAILQ_NEXT(adp, ad_next);
11659 		if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11660 			return;
11661 		TAILQ_REMOVE(listhead, adp, ad_next);
11662 		WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11663 	}
11664 }
11665 
11666 /*
11667  * Called from within softdep_disk_write_complete above.  This routine
11668  * completes successfully written allocindirs.
11669  */
11670 static void
11671 handle_allocindir_partdone(aip)
11672 	struct allocindir *aip;		/* the completed allocindir */
11673 {
11674 	struct indirdep *indirdep;
11675 
11676 	if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11677 		return;
11678 	indirdep = aip->ai_indirdep;
11679 	LIST_REMOVE(aip, ai_next);
11680 	/*
11681 	 * Don't set a pointer while the buffer is undergoing IO or while
11682 	 * we have active truncations.
11683 	 */
11684 	if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11685 		LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11686 		return;
11687 	}
11688 	if (indirdep->ir_state & UFS1FMT)
11689 		((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11690 		    aip->ai_newblkno;
11691 	else
11692 		((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11693 		    aip->ai_newblkno;
11694 	/*
11695 	 * Await the pointer write before freeing the allocindir.
11696 	 */
11697 	LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11698 }
11699 
11700 /*
11701  * Release segments held on a jwork list.
11702  */
11703 static void
11704 handle_jwork(wkhd)
11705 	struct workhead *wkhd;
11706 {
11707 	struct worklist *wk;
11708 
11709 	while ((wk = LIST_FIRST(wkhd)) != NULL) {
11710 		WORKLIST_REMOVE(wk);
11711 		switch (wk->wk_type) {
11712 		case D_JSEGDEP:
11713 			free_jsegdep(WK_JSEGDEP(wk));
11714 			continue;
11715 		case D_FREEDEP:
11716 			free_freedep(WK_FREEDEP(wk));
11717 			continue;
11718 		case D_FREEFRAG:
11719 			rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11720 			WORKITEM_FREE(wk, D_FREEFRAG);
11721 			continue;
11722 		case D_FREEWORK:
11723 			handle_written_freework(WK_FREEWORK(wk));
11724 			continue;
11725 		default:
11726 			panic("handle_jwork: Unknown type %s\n",
11727 			    TYPENAME(wk->wk_type));
11728 		}
11729 	}
11730 }
11731 
11732 /*
11733  * Handle the bufwait list on an inode when it is safe to release items
11734  * held there.  This normally happens after an inode block is written but
11735  * may be delayed and handled later if there are pending journal items that
11736  * are not yet safe to be released.
11737  */
11738 static struct freefile *
11739 handle_bufwait(inodedep, refhd)
11740 	struct inodedep *inodedep;
11741 	struct workhead *refhd;
11742 {
11743 	struct jaddref *jaddref;
11744 	struct freefile *freefile;
11745 	struct worklist *wk;
11746 
11747 	freefile = NULL;
11748 	while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11749 		WORKLIST_REMOVE(wk);
11750 		switch (wk->wk_type) {
11751 		case D_FREEFILE:
11752 			/*
11753 			 * We defer adding freefile to the worklist
11754 			 * until all other additions have been made to
11755 			 * ensure that it will be done after all the
11756 			 * old blocks have been freed.
11757 			 */
11758 			if (freefile != NULL)
11759 				panic("handle_bufwait: freefile");
11760 			freefile = WK_FREEFILE(wk);
11761 			continue;
11762 
11763 		case D_MKDIR:
11764 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11765 			continue;
11766 
11767 		case D_DIRADD:
11768 			diradd_inode_written(WK_DIRADD(wk), inodedep);
11769 			continue;
11770 
11771 		case D_FREEFRAG:
11772 			wk->wk_state |= COMPLETE;
11773 			if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11774 				add_to_worklist(wk, 0);
11775 			continue;
11776 
11777 		case D_DIRREM:
11778 			wk->wk_state |= COMPLETE;
11779 			add_to_worklist(wk, 0);
11780 			continue;
11781 
11782 		case D_ALLOCDIRECT:
11783 		case D_ALLOCINDIR:
11784 			free_newblk(WK_NEWBLK(wk));
11785 			continue;
11786 
11787 		case D_JNEWBLK:
11788 			wk->wk_state |= COMPLETE;
11789 			free_jnewblk(WK_JNEWBLK(wk));
11790 			continue;
11791 
11792 		/*
11793 		 * Save freed journal segments and add references on
11794 		 * the supplied list which will delay their release
11795 		 * until the cg bitmap is cleared on disk.
11796 		 */
11797 		case D_JSEGDEP:
11798 			if (refhd == NULL)
11799 				free_jsegdep(WK_JSEGDEP(wk));
11800 			else
11801 				WORKLIST_INSERT(refhd, wk);
11802 			continue;
11803 
11804 		case D_JADDREF:
11805 			jaddref = WK_JADDREF(wk);
11806 			TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11807 			    if_deps);
11808 			/*
11809 			 * Transfer any jaddrefs to the list to be freed with
11810 			 * the bitmap if we're handling a removed file.
11811 			 */
11812 			if (refhd == NULL) {
11813 				wk->wk_state |= COMPLETE;
11814 				free_jaddref(jaddref);
11815 			} else
11816 				WORKLIST_INSERT(refhd, wk);
11817 			continue;
11818 
11819 		default:
11820 			panic("handle_bufwait: Unknown type %p(%s)",
11821 			    wk, TYPENAME(wk->wk_type));
11822 			/* NOTREACHED */
11823 		}
11824 	}
11825 	return (freefile);
11826 }
11827 /*
11828  * Called from within softdep_disk_write_complete above to restore
11829  * in-memory inode block contents to their most up-to-date state. Note
11830  * that this routine is always called from interrupt level with further
11831  * interrupts from this device blocked.
11832  *
11833  * If the write did not succeed, we will do all the roll-forward
11834  * operations, but we will not take the actions that will allow its
11835  * dependencies to be processed.
11836  */
11837 static int
11838 handle_written_inodeblock(inodedep, bp, flags)
11839 	struct inodedep *inodedep;
11840 	struct buf *bp;		/* buffer containing the inode block */
11841 	int flags;
11842 {
11843 	struct freefile *freefile;
11844 	struct allocdirect *adp, *nextadp;
11845 	struct ufs1_dinode *dp1 = NULL;
11846 	struct ufs2_dinode *dp2 = NULL;
11847 	struct workhead wkhd;
11848 	int hadchanges, fstype;
11849 	ino_t freelink;
11850 
11851 	LIST_INIT(&wkhd);
11852 	hadchanges = 0;
11853 	freefile = NULL;
11854 	if ((inodedep->id_state & IOSTARTED) == 0)
11855 		panic("handle_written_inodeblock: not started");
11856 	inodedep->id_state &= ~IOSTARTED;
11857 	if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11858 		fstype = UFS1;
11859 		dp1 = (struct ufs1_dinode *)bp->b_data +
11860 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11861 		freelink = dp1->di_freelink;
11862 	} else {
11863 		fstype = UFS2;
11864 		dp2 = (struct ufs2_dinode *)bp->b_data +
11865 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11866 		freelink = dp2->di_freelink;
11867 	}
11868 	/*
11869 	 * Leave this inodeblock dirty until it's in the list.
11870 	 */
11871 	if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11872 	    (flags & WRITESUCCEEDED)) {
11873 		struct inodedep *inon;
11874 
11875 		inon = TAILQ_NEXT(inodedep, id_unlinked);
11876 		if ((inon == NULL && freelink == 0) ||
11877 		    (inon && inon->id_ino == freelink)) {
11878 			if (inon)
11879 				inon->id_state |= UNLINKPREV;
11880 			inodedep->id_state |= UNLINKNEXT;
11881 		}
11882 		hadchanges = 1;
11883 	}
11884 	/*
11885 	 * If we had to rollback the inode allocation because of
11886 	 * bitmaps being incomplete, then simply restore it.
11887 	 * Keep the block dirty so that it will not be reclaimed until
11888 	 * all associated dependencies have been cleared and the
11889 	 * corresponding updates written to disk.
11890 	 */
11891 	if (inodedep->id_savedino1 != NULL) {
11892 		hadchanges = 1;
11893 		if (fstype == UFS1)
11894 			*dp1 = *inodedep->id_savedino1;
11895 		else
11896 			*dp2 = *inodedep->id_savedino2;
11897 		free(inodedep->id_savedino1, M_SAVEDINO);
11898 		inodedep->id_savedino1 = NULL;
11899 		if ((bp->b_flags & B_DELWRI) == 0)
11900 			stat_inode_bitmap++;
11901 		bdirty(bp);
11902 		/*
11903 		 * If the inode is clear here and GOINGAWAY it will never
11904 		 * be written.  Process the bufwait and clear any pending
11905 		 * work which may include the freefile.
11906 		 */
11907 		if (inodedep->id_state & GOINGAWAY)
11908 			goto bufwait;
11909 		return (1);
11910 	}
11911 	if (flags & WRITESUCCEEDED)
11912 		inodedep->id_state |= COMPLETE;
11913 	/*
11914 	 * Roll forward anything that had to be rolled back before
11915 	 * the inode could be updated.
11916 	 */
11917 	for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11918 		nextadp = TAILQ_NEXT(adp, ad_next);
11919 		if (adp->ad_state & ATTACHED)
11920 			panic("handle_written_inodeblock: new entry");
11921 		if (fstype == UFS1) {
11922 			if (adp->ad_offset < UFS_NDADDR) {
11923 				if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11924 					panic("%s %s #%jd mismatch %d != %jd",
11925 					    "handle_written_inodeblock:",
11926 					    "direct pointer",
11927 					    (intmax_t)adp->ad_offset,
11928 					    dp1->di_db[adp->ad_offset],
11929 					    (intmax_t)adp->ad_oldblkno);
11930 				dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11931 			} else {
11932 				if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11933 				    0)
11934 					panic("%s: %s #%jd allocated as %d",
11935 					    "handle_written_inodeblock",
11936 					    "indirect pointer",
11937 					    (intmax_t)adp->ad_offset -
11938 					    UFS_NDADDR,
11939 					    dp1->di_ib[adp->ad_offset -
11940 					    UFS_NDADDR]);
11941 				dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11942 				    adp->ad_newblkno;
11943 			}
11944 		} else {
11945 			if (adp->ad_offset < UFS_NDADDR) {
11946 				if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11947 					panic("%s: %s #%jd %s %jd != %jd",
11948 					    "handle_written_inodeblock",
11949 					    "direct pointer",
11950 					    (intmax_t)adp->ad_offset, "mismatch",
11951 					    (intmax_t)dp2->di_db[adp->ad_offset],
11952 					    (intmax_t)adp->ad_oldblkno);
11953 				dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11954 			} else {
11955 				if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11956 				    0)
11957 					panic("%s: %s #%jd allocated as %jd",
11958 					    "handle_written_inodeblock",
11959 					    "indirect pointer",
11960 					    (intmax_t)adp->ad_offset -
11961 					    UFS_NDADDR,
11962 					    (intmax_t)
11963 					    dp2->di_ib[adp->ad_offset -
11964 					    UFS_NDADDR]);
11965 				dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11966 				    adp->ad_newblkno;
11967 			}
11968 		}
11969 		adp->ad_state &= ~UNDONE;
11970 		adp->ad_state |= ATTACHED;
11971 		hadchanges = 1;
11972 	}
11973 	for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11974 		nextadp = TAILQ_NEXT(adp, ad_next);
11975 		if (adp->ad_state & ATTACHED)
11976 			panic("handle_written_inodeblock: new entry");
11977 		if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11978 			panic("%s: direct pointers #%jd %s %jd != %jd",
11979 			    "handle_written_inodeblock",
11980 			    (intmax_t)adp->ad_offset, "mismatch",
11981 			    (intmax_t)dp2->di_extb[adp->ad_offset],
11982 			    (intmax_t)adp->ad_oldblkno);
11983 		dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11984 		adp->ad_state &= ~UNDONE;
11985 		adp->ad_state |= ATTACHED;
11986 		hadchanges = 1;
11987 	}
11988 	if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11989 		stat_direct_blk_ptrs++;
11990 	/*
11991 	 * Reset the file size to its most up-to-date value.
11992 	 */
11993 	if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11994 		panic("handle_written_inodeblock: bad size");
11995 	if (inodedep->id_savednlink > UFS_LINK_MAX)
11996 		panic("handle_written_inodeblock: Invalid link count "
11997 		    "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11998 		    inodedep);
11999 	if (fstype == UFS1) {
12000 		if (dp1->di_nlink != inodedep->id_savednlink) {
12001 			dp1->di_nlink = inodedep->id_savednlink;
12002 			hadchanges = 1;
12003 		}
12004 		if (dp1->di_size != inodedep->id_savedsize) {
12005 			dp1->di_size = inodedep->id_savedsize;
12006 			hadchanges = 1;
12007 		}
12008 	} else {
12009 		if (dp2->di_nlink != inodedep->id_savednlink) {
12010 			dp2->di_nlink = inodedep->id_savednlink;
12011 			hadchanges = 1;
12012 		}
12013 		if (dp2->di_size != inodedep->id_savedsize) {
12014 			dp2->di_size = inodedep->id_savedsize;
12015 			hadchanges = 1;
12016 		}
12017 		if (dp2->di_extsize != inodedep->id_savedextsize) {
12018 			dp2->di_extsize = inodedep->id_savedextsize;
12019 			hadchanges = 1;
12020 		}
12021 	}
12022 	inodedep->id_savedsize = -1;
12023 	inodedep->id_savedextsize = -1;
12024 	inodedep->id_savednlink = -1;
12025 	/*
12026 	 * If there were any rollbacks in the inode block, then it must be
12027 	 * marked dirty so that its will eventually get written back in
12028 	 * its correct form.
12029 	 */
12030 	if (hadchanges) {
12031 		if (fstype == UFS2)
12032 			ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
12033 		bdirty(bp);
12034 	}
12035 bufwait:
12036 	/*
12037 	 * If the write did not succeed, we have done all the roll-forward
12038 	 * operations, but we cannot take the actions that will allow its
12039 	 * dependencies to be processed.
12040 	 */
12041 	if ((flags & WRITESUCCEEDED) == 0)
12042 		return (hadchanges);
12043 	/*
12044 	 * Process any allocdirects that completed during the update.
12045 	 */
12046 	if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
12047 		handle_allocdirect_partdone(adp, &wkhd);
12048 	if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
12049 		handle_allocdirect_partdone(adp, &wkhd);
12050 	/*
12051 	 * Process deallocations that were held pending until the
12052 	 * inode had been written to disk. Freeing of the inode
12053 	 * is delayed until after all blocks have been freed to
12054 	 * avoid creation of new <vfsid, inum, lbn> triples
12055 	 * before the old ones have been deleted.  Completely
12056 	 * unlinked inodes are not processed until the unlinked
12057 	 * inode list is written or the last reference is removed.
12058 	 */
12059 	if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
12060 		freefile = handle_bufwait(inodedep, NULL);
12061 		if (freefile && !LIST_EMPTY(&wkhd)) {
12062 			WORKLIST_INSERT(&wkhd, &freefile->fx_list);
12063 			freefile = NULL;
12064 		}
12065 	}
12066 	/*
12067 	 * Move rolled forward dependency completions to the bufwait list
12068 	 * now that those that were already written have been processed.
12069 	 */
12070 	if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
12071 		panic("handle_written_inodeblock: bufwait but no changes");
12072 	jwork_move(&inodedep->id_bufwait, &wkhd);
12073 
12074 	if (freefile != NULL) {
12075 		/*
12076 		 * If the inode is goingaway it was never written.  Fake up
12077 		 * the state here so free_inodedep() can succeed.
12078 		 */
12079 		if (inodedep->id_state & GOINGAWAY)
12080 			inodedep->id_state |= COMPLETE | DEPCOMPLETE;
12081 		if (free_inodedep(inodedep) == 0)
12082 			panic("handle_written_inodeblock: live inodedep %p",
12083 			    inodedep);
12084 		add_to_worklist(&freefile->fx_list, 0);
12085 		return (0);
12086 	}
12087 
12088 	/*
12089 	 * If no outstanding dependencies, free it.
12090 	 */
12091 	if (free_inodedep(inodedep) ||
12092 	    (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
12093 	     TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
12094 	     TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
12095 	     LIST_FIRST(&inodedep->id_bufwait) == 0))
12096 		return (0);
12097 	return (hadchanges);
12098 }
12099 
12100 /*
12101  * Perform needed roll-forwards and kick off any dependencies that
12102  * can now be processed.
12103  *
12104  * If the write did not succeed, we will do all the roll-forward
12105  * operations, but we will not take the actions that will allow its
12106  * dependencies to be processed.
12107  */
12108 static int
12109 handle_written_indirdep(indirdep, bp, bpp, flags)
12110 	struct indirdep *indirdep;
12111 	struct buf *bp;
12112 	struct buf **bpp;
12113 	int flags;
12114 {
12115 	struct allocindir *aip;
12116 	struct buf *sbp;
12117 	int chgs;
12118 
12119 	if (indirdep->ir_state & GOINGAWAY)
12120 		panic("handle_written_indirdep: indirdep gone");
12121 	if ((indirdep->ir_state & IOSTARTED) == 0)
12122 		panic("handle_written_indirdep: IO not started");
12123 	chgs = 0;
12124 	/*
12125 	 * If there were rollbacks revert them here.
12126 	 */
12127 	if (indirdep->ir_saveddata) {
12128 		bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
12129 		if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12130 			free(indirdep->ir_saveddata, M_INDIRDEP);
12131 			indirdep->ir_saveddata = NULL;
12132 		}
12133 		chgs = 1;
12134 	}
12135 	indirdep->ir_state &= ~(UNDONE | IOSTARTED);
12136 	indirdep->ir_state |= ATTACHED;
12137 	/*
12138 	 * If the write did not succeed, we have done all the roll-forward
12139 	 * operations, but we cannot take the actions that will allow its
12140 	 * dependencies to be processed.
12141 	 */
12142 	if ((flags & WRITESUCCEEDED) == 0) {
12143 		stat_indir_blk_ptrs++;
12144 		bdirty(bp);
12145 		return (1);
12146 	}
12147 	/*
12148 	 * Move allocindirs with written pointers to the completehd if
12149 	 * the indirdep's pointer is not yet written.  Otherwise
12150 	 * free them here.
12151 	 */
12152 	while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
12153 		LIST_REMOVE(aip, ai_next);
12154 		if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
12155 			LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
12156 			    ai_next);
12157 			newblk_freefrag(&aip->ai_block);
12158 			continue;
12159 		}
12160 		free_newblk(&aip->ai_block);
12161 	}
12162 	/*
12163 	 * Move allocindirs that have finished dependency processing from
12164 	 * the done list to the write list after updating the pointers.
12165 	 */
12166 	if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12167 		while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
12168 			handle_allocindir_partdone(aip);
12169 			if (aip == LIST_FIRST(&indirdep->ir_donehd))
12170 				panic("disk_write_complete: not gone");
12171 			chgs = 1;
12172 		}
12173 	}
12174 	/*
12175 	 * Preserve the indirdep if there were any changes or if it is not
12176 	 * yet valid on disk.
12177 	 */
12178 	if (chgs) {
12179 		stat_indir_blk_ptrs++;
12180 		bdirty(bp);
12181 		return (1);
12182 	}
12183 	/*
12184 	 * If there were no changes we can discard the savedbp and detach
12185 	 * ourselves from the buf.  We are only carrying completed pointers
12186 	 * in this case.
12187 	 */
12188 	sbp = indirdep->ir_savebp;
12189 	sbp->b_flags |= B_INVAL | B_NOCACHE;
12190 	indirdep->ir_savebp = NULL;
12191 	indirdep->ir_bp = NULL;
12192 	if (*bpp != NULL)
12193 		panic("handle_written_indirdep: bp already exists.");
12194 	*bpp = sbp;
12195 	/*
12196 	 * The indirdep may not be freed until its parent points at it.
12197 	 */
12198 	if (indirdep->ir_state & DEPCOMPLETE)
12199 		free_indirdep(indirdep);
12200 
12201 	return (0);
12202 }
12203 
12204 /*
12205  * Process a diradd entry after its dependent inode has been written.
12206  */
12207 static void
12208 diradd_inode_written(dap, inodedep)
12209 	struct diradd *dap;
12210 	struct inodedep *inodedep;
12211 {
12212 
12213 	LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
12214 	dap->da_state |= COMPLETE;
12215 	complete_diradd(dap);
12216 	WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
12217 }
12218 
12219 /*
12220  * Returns true if the bmsafemap will have rollbacks when written.  Must only
12221  * be called with the per-filesystem lock and the buf lock on the cg held.
12222  */
12223 static int
12224 bmsafemap_backgroundwrite(bmsafemap, bp)
12225 	struct bmsafemap *bmsafemap;
12226 	struct buf *bp;
12227 {
12228 	int dirty;
12229 
12230 	LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
12231 	dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
12232 	    !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
12233 	/*
12234 	 * If we're initiating a background write we need to process the
12235 	 * rollbacks as they exist now, not as they exist when IO starts.
12236 	 * No other consumers will look at the contents of the shadowed
12237 	 * buf so this is safe to do here.
12238 	 */
12239 	if (bp->b_xflags & BX_BKGRDMARKER)
12240 		initiate_write_bmsafemap(bmsafemap, bp);
12241 
12242 	return (dirty);
12243 }
12244 
12245 /*
12246  * Re-apply an allocation when a cg write is complete.
12247  */
12248 static int
12249 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
12250 	struct jnewblk *jnewblk;
12251 	struct fs *fs;
12252 	struct cg *cgp;
12253 	uint8_t *blksfree;
12254 {
12255 	ufs1_daddr_t fragno;
12256 	ufs2_daddr_t blkno;
12257 	long cgbno, bbase;
12258 	int frags, blk;
12259 	int i;
12260 
12261 	frags = 0;
12262 	cgbno = dtogd(fs, jnewblk->jn_blkno);
12263 	for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
12264 		if (isclr(blksfree, cgbno + i))
12265 			panic("jnewblk_rollforward: re-allocated fragment");
12266 		frags++;
12267 	}
12268 	if (frags == fs->fs_frag) {
12269 		blkno = fragstoblks(fs, cgbno);
12270 		ffs_clrblock(fs, blksfree, (long)blkno);
12271 		ffs_clusteracct(fs, cgp, blkno, -1);
12272 		cgp->cg_cs.cs_nbfree--;
12273 	} else {
12274 		bbase = cgbno - fragnum(fs, cgbno);
12275 		cgbno += jnewblk->jn_oldfrags;
12276                 /* If a complete block had been reassembled, account for it. */
12277 		fragno = fragstoblks(fs, bbase);
12278 		if (ffs_isblock(fs, blksfree, fragno)) {
12279 			cgp->cg_cs.cs_nffree += fs->fs_frag;
12280 			ffs_clusteracct(fs, cgp, fragno, -1);
12281 			cgp->cg_cs.cs_nbfree--;
12282 		}
12283 		/* Decrement the old frags.  */
12284 		blk = blkmap(fs, blksfree, bbase);
12285 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12286 		/* Allocate the fragment */
12287 		for (i = 0; i < frags; i++)
12288 			clrbit(blksfree, cgbno + i);
12289 		cgp->cg_cs.cs_nffree -= frags;
12290 		/* Add back in counts associated with the new frags */
12291 		blk = blkmap(fs, blksfree, bbase);
12292 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12293 	}
12294 	return (frags);
12295 }
12296 
12297 /*
12298  * Complete a write to a bmsafemap structure.  Roll forward any bitmap
12299  * changes if it's not a background write.  Set all written dependencies
12300  * to DEPCOMPLETE and free the structure if possible.
12301  *
12302  * If the write did not succeed, we will do all the roll-forward
12303  * operations, but we will not take the actions that will allow its
12304  * dependencies to be processed.
12305  */
12306 static int
12307 handle_written_bmsafemap(bmsafemap, bp, flags)
12308 	struct bmsafemap *bmsafemap;
12309 	struct buf *bp;
12310 	int flags;
12311 {
12312 	struct newblk *newblk;
12313 	struct inodedep *inodedep;
12314 	struct jaddref *jaddref, *jatmp;
12315 	struct jnewblk *jnewblk, *jntmp;
12316 	struct ufsmount *ump;
12317 	uint8_t *inosused;
12318 	uint8_t *blksfree;
12319 	struct cg *cgp;
12320 	struct fs *fs;
12321 	ino_t ino;
12322 	int foreground;
12323 	int chgs;
12324 
12325 	if ((bmsafemap->sm_state & IOSTARTED) == 0)
12326 		panic("handle_written_bmsafemap: Not started\n");
12327 	ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
12328 	chgs = 0;
12329 	bmsafemap->sm_state &= ~IOSTARTED;
12330 	foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
12331 	/*
12332 	 * If write was successful, release journal work that was waiting
12333 	 * on the write. Otherwise move the work back.
12334 	 */
12335 	if (flags & WRITESUCCEEDED)
12336 		handle_jwork(&bmsafemap->sm_freewr);
12337 	else
12338 		LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12339 		    worklist, wk_list);
12340 
12341 	/*
12342 	 * Restore unwritten inode allocation pending jaddref writes.
12343 	 */
12344 	if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12345 		cgp = (struct cg *)bp->b_data;
12346 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12347 		inosused = cg_inosused(cgp);
12348 		LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12349 		    ja_bmdeps, jatmp) {
12350 			if ((jaddref->ja_state & UNDONE) == 0)
12351 				continue;
12352 			ino = jaddref->ja_ino % fs->fs_ipg;
12353 			if (isset(inosused, ino))
12354 				panic("handle_written_bmsafemap: "
12355 				    "re-allocated inode");
12356 			/* Do the roll-forward only if it's a real copy. */
12357 			if (foreground) {
12358 				if ((jaddref->ja_mode & IFMT) == IFDIR)
12359 					cgp->cg_cs.cs_ndir++;
12360 				cgp->cg_cs.cs_nifree--;
12361 				setbit(inosused, ino);
12362 				chgs = 1;
12363 			}
12364 			jaddref->ja_state &= ~UNDONE;
12365 			jaddref->ja_state |= ATTACHED;
12366 			free_jaddref(jaddref);
12367 		}
12368 	}
12369 	/*
12370 	 * Restore any block allocations which are pending journal writes.
12371 	 */
12372 	if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12373 		cgp = (struct cg *)bp->b_data;
12374 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12375 		blksfree = cg_blksfree(cgp);
12376 		LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12377 		    jntmp) {
12378 			if ((jnewblk->jn_state & UNDONE) == 0)
12379 				continue;
12380 			/* Do the roll-forward only if it's a real copy. */
12381 			if (foreground &&
12382 			    jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12383 				chgs = 1;
12384 			jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12385 			jnewblk->jn_state |= ATTACHED;
12386 			free_jnewblk(jnewblk);
12387 		}
12388 	}
12389 	/*
12390 	 * If the write did not succeed, we have done all the roll-forward
12391 	 * operations, but we cannot take the actions that will allow its
12392 	 * dependencies to be processed.
12393 	 */
12394 	if ((flags & WRITESUCCEEDED) == 0) {
12395 		LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12396 		    newblk, nb_deps);
12397 		LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12398 		    worklist, wk_list);
12399 		if (foreground)
12400 			bdirty(bp);
12401 		return (1);
12402 	}
12403 	while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12404 		newblk->nb_state |= DEPCOMPLETE;
12405 		newblk->nb_state &= ~ONDEPLIST;
12406 		newblk->nb_bmsafemap = NULL;
12407 		LIST_REMOVE(newblk, nb_deps);
12408 		if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12409 			handle_allocdirect_partdone(
12410 			    WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12411 		else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12412 			handle_allocindir_partdone(
12413 			    WK_ALLOCINDIR(&newblk->nb_list));
12414 		else if (newblk->nb_list.wk_type != D_NEWBLK)
12415 			panic("handle_written_bmsafemap: Unexpected type: %s",
12416 			    TYPENAME(newblk->nb_list.wk_type));
12417 	}
12418 	while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12419 		inodedep->id_state |= DEPCOMPLETE;
12420 		inodedep->id_state &= ~ONDEPLIST;
12421 		LIST_REMOVE(inodedep, id_deps);
12422 		inodedep->id_bmsafemap = NULL;
12423 	}
12424 	LIST_REMOVE(bmsafemap, sm_next);
12425 	if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12426 	    LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12427 	    LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12428 	    LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12429 	    LIST_EMPTY(&bmsafemap->sm_freehd)) {
12430 		LIST_REMOVE(bmsafemap, sm_hash);
12431 		WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12432 		return (0);
12433 	}
12434 	LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12435 	if (foreground)
12436 		bdirty(bp);
12437 	return (1);
12438 }
12439 
12440 /*
12441  * Try to free a mkdir dependency.
12442  */
12443 static void
12444 complete_mkdir(mkdir)
12445 	struct mkdir *mkdir;
12446 {
12447 	struct diradd *dap;
12448 
12449 	if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12450 		return;
12451 	LIST_REMOVE(mkdir, md_mkdirs);
12452 	dap = mkdir->md_diradd;
12453 	dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12454 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12455 		dap->da_state |= DEPCOMPLETE;
12456 		complete_diradd(dap);
12457 	}
12458 	WORKITEM_FREE(mkdir, D_MKDIR);
12459 }
12460 
12461 /*
12462  * Handle the completion of a mkdir dependency.
12463  */
12464 static void
12465 handle_written_mkdir(mkdir, type)
12466 	struct mkdir *mkdir;
12467 	int type;
12468 {
12469 
12470 	if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12471 		panic("handle_written_mkdir: bad type");
12472 	mkdir->md_state |= COMPLETE;
12473 	complete_mkdir(mkdir);
12474 }
12475 
12476 static int
12477 free_pagedep(pagedep)
12478 	struct pagedep *pagedep;
12479 {
12480 	int i;
12481 
12482 	if (pagedep->pd_state & NEWBLOCK)
12483 		return (0);
12484 	if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12485 		return (0);
12486 	for (i = 0; i < DAHASHSZ; i++)
12487 		if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12488 			return (0);
12489 	if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12490 		return (0);
12491 	if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12492 		return (0);
12493 	if (pagedep->pd_state & ONWORKLIST)
12494 		WORKLIST_REMOVE(&pagedep->pd_list);
12495 	LIST_REMOVE(pagedep, pd_hash);
12496 	WORKITEM_FREE(pagedep, D_PAGEDEP);
12497 
12498 	return (1);
12499 }
12500 
12501 /*
12502  * Called from within softdep_disk_write_complete above.
12503  * A write operation was just completed. Removed inodes can
12504  * now be freed and associated block pointers may be committed.
12505  * Note that this routine is always called from interrupt level
12506  * with further interrupts from this device blocked.
12507  *
12508  * If the write did not succeed, we will do all the roll-forward
12509  * operations, but we will not take the actions that will allow its
12510  * dependencies to be processed.
12511  */
12512 static int
12513 handle_written_filepage(pagedep, bp, flags)
12514 	struct pagedep *pagedep;
12515 	struct buf *bp;		/* buffer containing the written page */
12516 	int flags;
12517 {
12518 	struct dirrem *dirrem;
12519 	struct diradd *dap, *nextdap;
12520 	struct direct *ep;
12521 	int i, chgs;
12522 
12523 	if ((pagedep->pd_state & IOSTARTED) == 0)
12524 		panic("handle_written_filepage: not started");
12525 	pagedep->pd_state &= ~IOSTARTED;
12526 	if ((flags & WRITESUCCEEDED) == 0)
12527 		goto rollforward;
12528 	/*
12529 	 * Process any directory removals that have been committed.
12530 	 */
12531 	while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12532 		LIST_REMOVE(dirrem, dm_next);
12533 		dirrem->dm_state |= COMPLETE;
12534 		dirrem->dm_dirinum = pagedep->pd_ino;
12535 		KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12536 		    ("handle_written_filepage: Journal entries not written."));
12537 		add_to_worklist(&dirrem->dm_list, 0);
12538 	}
12539 	/*
12540 	 * Free any directory additions that have been committed.
12541 	 * If it is a newly allocated block, we have to wait until
12542 	 * the on-disk directory inode claims the new block.
12543 	 */
12544 	if ((pagedep->pd_state & NEWBLOCK) == 0)
12545 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12546 			free_diradd(dap, NULL);
12547 rollforward:
12548 	/*
12549 	 * Uncommitted directory entries must be restored.
12550 	 */
12551 	for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12552 		for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12553 		     dap = nextdap) {
12554 			nextdap = LIST_NEXT(dap, da_pdlist);
12555 			if (dap->da_state & ATTACHED)
12556 				panic("handle_written_filepage: attached");
12557 			ep = (struct direct *)
12558 			    ((char *)bp->b_data + dap->da_offset);
12559 			ep->d_ino = dap->da_newinum;
12560 			dap->da_state &= ~UNDONE;
12561 			dap->da_state |= ATTACHED;
12562 			chgs = 1;
12563 			/*
12564 			 * If the inode referenced by the directory has
12565 			 * been written out, then the dependency can be
12566 			 * moved to the pending list.
12567 			 */
12568 			if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12569 				LIST_REMOVE(dap, da_pdlist);
12570 				LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12571 				    da_pdlist);
12572 			}
12573 		}
12574 	}
12575 	/*
12576 	 * If there were any rollbacks in the directory, then it must be
12577 	 * marked dirty so that its will eventually get written back in
12578 	 * its correct form.
12579 	 */
12580 	if (chgs || (flags & WRITESUCCEEDED) == 0) {
12581 		if ((bp->b_flags & B_DELWRI) == 0)
12582 			stat_dir_entry++;
12583 		bdirty(bp);
12584 		return (1);
12585 	}
12586 	/*
12587 	 * If we are not waiting for a new directory block to be
12588 	 * claimed by its inode, then the pagedep will be freed.
12589 	 * Otherwise it will remain to track any new entries on
12590 	 * the page in case they are fsync'ed.
12591 	 */
12592 	free_pagedep(pagedep);
12593 	return (0);
12594 }
12595 
12596 /*
12597  * Writing back in-core inode structures.
12598  *
12599  * The filesystem only accesses an inode's contents when it occupies an
12600  * "in-core" inode structure.  These "in-core" structures are separate from
12601  * the page frames used to cache inode blocks.  Only the latter are
12602  * transferred to/from the disk.  So, when the updated contents of the
12603  * "in-core" inode structure are copied to the corresponding in-memory inode
12604  * block, the dependencies are also transferred.  The following procedure is
12605  * called when copying a dirty "in-core" inode to a cached inode block.
12606  */
12607 
12608 /*
12609  * Called when an inode is loaded from disk. If the effective link count
12610  * differed from the actual link count when it was last flushed, then we
12611  * need to ensure that the correct effective link count is put back.
12612  */
12613 void
12614 softdep_load_inodeblock(ip)
12615 	struct inode *ip;	/* the "in_core" copy of the inode */
12616 {
12617 	struct inodedep *inodedep;
12618 	struct ufsmount *ump;
12619 
12620 	ump = ITOUMP(ip);
12621 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12622 	    ("softdep_load_inodeblock called on non-softdep filesystem"));
12623 	/*
12624 	 * Check for alternate nlink count.
12625 	 */
12626 	ip->i_effnlink = ip->i_nlink;
12627 	ACQUIRE_LOCK(ump);
12628 	if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12629 		FREE_LOCK(ump);
12630 		return;
12631 	}
12632 	if (ip->i_nlink != inodedep->id_nlinkwrote &&
12633 	    inodedep->id_nlinkwrote != -1) {
12634 		KASSERT(ip->i_nlink == 0 &&
12635 		    (ump->um_flags & UM_FSFAIL_CLEANUP) != 0,
12636 		    ("read bad i_nlink value"));
12637 		ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote;
12638 	}
12639 	ip->i_effnlink -= inodedep->id_nlinkdelta;
12640 	KASSERT(ip->i_effnlink >= 0,
12641 	    ("softdep_load_inodeblock: negative i_effnlink"));
12642 	FREE_LOCK(ump);
12643 }
12644 
12645 /*
12646  * This routine is called just before the "in-core" inode
12647  * information is to be copied to the in-memory inode block.
12648  * Recall that an inode block contains several inodes. If
12649  * the force flag is set, then the dependencies will be
12650  * cleared so that the update can always be made. Note that
12651  * the buffer is locked when this routine is called, so we
12652  * will never be in the middle of writing the inode block
12653  * to disk.
12654  */
12655 void
12656 softdep_update_inodeblock(ip, bp, waitfor)
12657 	struct inode *ip;	/* the "in_core" copy of the inode */
12658 	struct buf *bp;		/* the buffer containing the inode block */
12659 	int waitfor;		/* nonzero => update must be allowed */
12660 {
12661 	struct inodedep *inodedep;
12662 	struct inoref *inoref;
12663 	struct ufsmount *ump;
12664 	struct worklist *wk;
12665 	struct mount *mp;
12666 	struct buf *ibp;
12667 	struct fs *fs;
12668 	int error;
12669 
12670 	ump = ITOUMP(ip);
12671 	mp = UFSTOVFS(ump);
12672 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12673 	    ("softdep_update_inodeblock called on non-softdep filesystem"));
12674 	fs = ump->um_fs;
12675 	/*
12676 	 * Preserve the freelink that is on disk.  clear_unlinked_inodedep()
12677 	 * does not have access to the in-core ip so must write directly into
12678 	 * the inode block buffer when setting freelink.
12679 	 */
12680 	if (fs->fs_magic == FS_UFS1_MAGIC)
12681 		DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12682 		    ino_to_fsbo(fs, ip->i_number))->di_freelink);
12683 	else
12684 		DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12685 		    ino_to_fsbo(fs, ip->i_number))->di_freelink);
12686 	/*
12687 	 * If the effective link count is not equal to the actual link
12688 	 * count, then we must track the difference in an inodedep while
12689 	 * the inode is (potentially) tossed out of the cache. Otherwise,
12690 	 * if there is no existing inodedep, then there are no dependencies
12691 	 * to track.
12692 	 */
12693 	ACQUIRE_LOCK(ump);
12694 again:
12695 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12696 		FREE_LOCK(ump);
12697 		if (ip->i_effnlink != ip->i_nlink)
12698 			panic("softdep_update_inodeblock: bad link count");
12699 		return;
12700 	}
12701 	KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta,
12702 	    ("softdep_update_inodeblock inconsistent ip %p i_nlink %d "
12703 	    "inodedep %p id_nlinkdelta %jd",
12704 	    ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta));
12705 	inodedep->id_nlinkwrote = ip->i_nlink;
12706 	if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12707 		panic("softdep_update_inodeblock: bad delta");
12708 	/*
12709 	 * If we're flushing all dependencies we must also move any waiting
12710 	 * for journal writes onto the bufwait list prior to I/O.
12711 	 */
12712 	if (waitfor) {
12713 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12714 			if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12715 			    == DEPCOMPLETE) {
12716 				jwait(&inoref->if_list, MNT_WAIT);
12717 				goto again;
12718 			}
12719 		}
12720 	}
12721 	/*
12722 	 * Changes have been initiated. Anything depending on these
12723 	 * changes cannot occur until this inode has been written.
12724 	 */
12725 	inodedep->id_state &= ~COMPLETE;
12726 	if ((inodedep->id_state & ONWORKLIST) == 0)
12727 		WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12728 	/*
12729 	 * Any new dependencies associated with the incore inode must
12730 	 * now be moved to the list associated with the buffer holding
12731 	 * the in-memory copy of the inode. Once merged process any
12732 	 * allocdirects that are completed by the merger.
12733 	 */
12734 	merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12735 	if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12736 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12737 		    NULL);
12738 	merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12739 	if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12740 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12741 		    NULL);
12742 	/*
12743 	 * Now that the inode has been pushed into the buffer, the
12744 	 * operations dependent on the inode being written to disk
12745 	 * can be moved to the id_bufwait so that they will be
12746 	 * processed when the buffer I/O completes.
12747 	 */
12748 	while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12749 		WORKLIST_REMOVE(wk);
12750 		WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12751 	}
12752 	/*
12753 	 * Newly allocated inodes cannot be written until the bitmap
12754 	 * that allocates them have been written (indicated by
12755 	 * DEPCOMPLETE being set in id_state). If we are doing a
12756 	 * forced sync (e.g., an fsync on a file), we force the bitmap
12757 	 * to be written so that the update can be done.
12758 	 */
12759 	if (waitfor == 0) {
12760 		FREE_LOCK(ump);
12761 		return;
12762 	}
12763 retry:
12764 	if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12765 		FREE_LOCK(ump);
12766 		return;
12767 	}
12768 	ibp = inodedep->id_bmsafemap->sm_buf;
12769 	ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12770 	if (ibp == NULL) {
12771 		/*
12772 		 * If ibp came back as NULL, the dependency could have been
12773 		 * freed while we slept.  Look it up again, and check to see
12774 		 * that it has completed.
12775 		 */
12776 		if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12777 			goto retry;
12778 		FREE_LOCK(ump);
12779 		return;
12780 	}
12781 	FREE_LOCK(ump);
12782 	if ((error = bwrite(ibp)) != 0)
12783 		softdep_error("softdep_update_inodeblock: bwrite", error);
12784 }
12785 
12786 /*
12787  * Merge the a new inode dependency list (such as id_newinoupdt) into an
12788  * old inode dependency list (such as id_inoupdt).
12789  */
12790 static void
12791 merge_inode_lists(newlisthead, oldlisthead)
12792 	struct allocdirectlst *newlisthead;
12793 	struct allocdirectlst *oldlisthead;
12794 {
12795 	struct allocdirect *listadp, *newadp;
12796 
12797 	newadp = TAILQ_FIRST(newlisthead);
12798 	if (newadp != NULL)
12799 		LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12800 	for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12801 		if (listadp->ad_offset < newadp->ad_offset) {
12802 			listadp = TAILQ_NEXT(listadp, ad_next);
12803 			continue;
12804 		}
12805 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
12806 		TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12807 		if (listadp->ad_offset == newadp->ad_offset) {
12808 			allocdirect_merge(oldlisthead, newadp,
12809 			    listadp);
12810 			listadp = newadp;
12811 		}
12812 		newadp = TAILQ_FIRST(newlisthead);
12813 	}
12814 	while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12815 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
12816 		TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12817 	}
12818 }
12819 
12820 /*
12821  * If we are doing an fsync, then we must ensure that any directory
12822  * entries for the inode have been written after the inode gets to disk.
12823  */
12824 int
12825 softdep_fsync(vp)
12826 	struct vnode *vp;	/* the "in_core" copy of the inode */
12827 {
12828 	struct inodedep *inodedep;
12829 	struct pagedep *pagedep;
12830 	struct inoref *inoref;
12831 	struct ufsmount *ump;
12832 	struct worklist *wk;
12833 	struct diradd *dap;
12834 	struct mount *mp;
12835 	struct vnode *pvp;
12836 	struct inode *ip;
12837 	struct buf *bp;
12838 	struct fs *fs;
12839 	struct thread *td = curthread;
12840 	int error, flushparent, pagedep_new_block;
12841 	ino_t parentino;
12842 	ufs_lbn_t lbn;
12843 
12844 	ip = VTOI(vp);
12845 	mp = vp->v_mount;
12846 	ump = VFSTOUFS(mp);
12847 	fs = ump->um_fs;
12848 	if (MOUNTEDSOFTDEP(mp) == 0)
12849 		return (0);
12850 	ACQUIRE_LOCK(ump);
12851 restart:
12852 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12853 		FREE_LOCK(ump);
12854 		return (0);
12855 	}
12856 	TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12857 		if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12858 		    == DEPCOMPLETE) {
12859 			jwait(&inoref->if_list, MNT_WAIT);
12860 			goto restart;
12861 		}
12862 	}
12863 	if (!LIST_EMPTY(&inodedep->id_inowait) ||
12864 	    !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12865 	    !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12866 	    !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12867 	    !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12868 		panic("softdep_fsync: pending ops %p", inodedep);
12869 	for (error = 0, flushparent = 0; ; ) {
12870 		if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12871 			break;
12872 		if (wk->wk_type != D_DIRADD)
12873 			panic("softdep_fsync: Unexpected type %s",
12874 			    TYPENAME(wk->wk_type));
12875 		dap = WK_DIRADD(wk);
12876 		/*
12877 		 * Flush our parent if this directory entry has a MKDIR_PARENT
12878 		 * dependency or is contained in a newly allocated block.
12879 		 */
12880 		if (dap->da_state & DIRCHG)
12881 			pagedep = dap->da_previous->dm_pagedep;
12882 		else
12883 			pagedep = dap->da_pagedep;
12884 		parentino = pagedep->pd_ino;
12885 		lbn = pagedep->pd_lbn;
12886 		if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12887 			panic("softdep_fsync: dirty");
12888 		if ((dap->da_state & MKDIR_PARENT) ||
12889 		    (pagedep->pd_state & NEWBLOCK))
12890 			flushparent = 1;
12891 		else
12892 			flushparent = 0;
12893 		/*
12894 		 * If we are being fsync'ed as part of vgone'ing this vnode,
12895 		 * then we will not be able to release and recover the
12896 		 * vnode below, so we just have to give up on writing its
12897 		 * directory entry out. It will eventually be written, just
12898 		 * not now, but then the user was not asking to have it
12899 		 * written, so we are not breaking any promises.
12900 		 */
12901 		if (VN_IS_DOOMED(vp))
12902 			break;
12903 		/*
12904 		 * We prevent deadlock by always fetching inodes from the
12905 		 * root, moving down the directory tree. Thus, when fetching
12906 		 * our parent directory, we first try to get the lock. If
12907 		 * that fails, we must unlock ourselves before requesting
12908 		 * the lock on our parent. See the comment in ufs_lookup
12909 		 * for details on possible races.
12910 		 */
12911 		FREE_LOCK(ump);
12912 		error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL,
12913 		    &pvp);
12914 		if (error == ERELOOKUP)
12915 			error = 0;
12916 		if (error != 0)
12917 			return (error);
12918 		/*
12919 		 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12920 		 * that are contained in direct blocks will be resolved by
12921 		 * doing a ffs_update. Pagedeps contained in indirect blocks
12922 		 * may require a complete sync'ing of the directory. So, we
12923 		 * try the cheap and fast ffs_update first, and if that fails,
12924 		 * then we do the slower ffs_syncvnode of the directory.
12925 		 */
12926 		if (flushparent) {
12927 			int locked;
12928 
12929 			if ((error = ffs_update(pvp, 1)) != 0) {
12930 				vput(pvp);
12931 				return (error);
12932 			}
12933 			ACQUIRE_LOCK(ump);
12934 			locked = 1;
12935 			if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12936 				if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12937 					if (wk->wk_type != D_DIRADD)
12938 						panic("softdep_fsync: Unexpected type %s",
12939 						      TYPENAME(wk->wk_type));
12940 					dap = WK_DIRADD(wk);
12941 					if (dap->da_state & DIRCHG)
12942 						pagedep = dap->da_previous->dm_pagedep;
12943 					else
12944 						pagedep = dap->da_pagedep;
12945 					pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12946 					FREE_LOCK(ump);
12947 					locked = 0;
12948 					if (pagedep_new_block && (error =
12949 					    ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12950 						vput(pvp);
12951 						return (error);
12952 					}
12953 				}
12954 			}
12955 			if (locked)
12956 				FREE_LOCK(ump);
12957 		}
12958 		/*
12959 		 * Flush directory page containing the inode's name.
12960 		 */
12961 		error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12962 		    &bp);
12963 		if (error == 0)
12964 			error = bwrite(bp);
12965 		else
12966 			brelse(bp);
12967 		vput(pvp);
12968 		if (!ffs_fsfail_cleanup(ump, error))
12969 			return (error);
12970 		ACQUIRE_LOCK(ump);
12971 		if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12972 			break;
12973 	}
12974 	FREE_LOCK(ump);
12975 	return (0);
12976 }
12977 
12978 /*
12979  * Flush all the dirty bitmaps associated with the block device
12980  * before flushing the rest of the dirty blocks so as to reduce
12981  * the number of dependencies that will have to be rolled back.
12982  *
12983  * XXX Unused?
12984  */
12985 void
12986 softdep_fsync_mountdev(vp)
12987 	struct vnode *vp;
12988 {
12989 	struct buf *bp, *nbp;
12990 	struct worklist *wk;
12991 	struct bufobj *bo;
12992 
12993 	if (!vn_isdisk(vp))
12994 		panic("softdep_fsync_mountdev: vnode not a disk");
12995 	bo = &vp->v_bufobj;
12996 restart:
12997 	BO_LOCK(bo);
12998 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12999 		/*
13000 		 * If it is already scheduled, skip to the next buffer.
13001 		 */
13002 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
13003 			continue;
13004 
13005 		if ((bp->b_flags & B_DELWRI) == 0)
13006 			panic("softdep_fsync_mountdev: not dirty");
13007 		/*
13008 		 * We are only interested in bitmaps with outstanding
13009 		 * dependencies.
13010 		 */
13011 		if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
13012 		    wk->wk_type != D_BMSAFEMAP ||
13013 		    (bp->b_vflags & BV_BKGRDINPROG)) {
13014 			BUF_UNLOCK(bp);
13015 			continue;
13016 		}
13017 		BO_UNLOCK(bo);
13018 		bremfree(bp);
13019 		(void) bawrite(bp);
13020 		goto restart;
13021 	}
13022 	drain_output(vp);
13023 	BO_UNLOCK(bo);
13024 }
13025 
13026 /*
13027  * Sync all cylinder groups that were dirty at the time this function is
13028  * called.  Newly dirtied cgs will be inserted before the sentinel.  This
13029  * is used to flush freedep activity that may be holding up writes to a
13030  * indirect block.
13031  */
13032 static int
13033 sync_cgs(mp, waitfor)
13034 	struct mount *mp;
13035 	int waitfor;
13036 {
13037 	struct bmsafemap *bmsafemap;
13038 	struct bmsafemap *sentinel;
13039 	struct ufsmount *ump;
13040 	struct buf *bp;
13041 	int error;
13042 
13043 	sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
13044 	sentinel->sm_cg = -1;
13045 	ump = VFSTOUFS(mp);
13046 	error = 0;
13047 	ACQUIRE_LOCK(ump);
13048 	LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
13049 	for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
13050 	    bmsafemap = LIST_NEXT(sentinel, sm_next)) {
13051 		/* Skip sentinels and cgs with no work to release. */
13052 		if (bmsafemap->sm_cg == -1 ||
13053 		    (LIST_EMPTY(&bmsafemap->sm_freehd) &&
13054 		    LIST_EMPTY(&bmsafemap->sm_freewr))) {
13055 			LIST_REMOVE(sentinel, sm_next);
13056 			LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
13057 			continue;
13058 		}
13059 		/*
13060 		 * If we don't get the lock and we're waiting try again, if
13061 		 * not move on to the next buf and try to sync it.
13062 		 */
13063 		bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
13064 		if (bp == NULL && waitfor == MNT_WAIT)
13065 			continue;
13066 		LIST_REMOVE(sentinel, sm_next);
13067 		LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
13068 		if (bp == NULL)
13069 			continue;
13070 		FREE_LOCK(ump);
13071 		if (waitfor == MNT_NOWAIT)
13072 			bawrite(bp);
13073 		else
13074 			error = bwrite(bp);
13075 		ACQUIRE_LOCK(ump);
13076 		if (error)
13077 			break;
13078 	}
13079 	LIST_REMOVE(sentinel, sm_next);
13080 	FREE_LOCK(ump);
13081 	free(sentinel, M_BMSAFEMAP);
13082 	return (error);
13083 }
13084 
13085 /*
13086  * This routine is called when we are trying to synchronously flush a
13087  * file. This routine must eliminate any filesystem metadata dependencies
13088  * so that the syncing routine can succeed.
13089  */
13090 int
13091 softdep_sync_metadata(struct vnode *vp)
13092 {
13093 	struct inode *ip;
13094 	int error;
13095 
13096 	ip = VTOI(vp);
13097 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13098 	    ("softdep_sync_metadata called on non-softdep filesystem"));
13099 	/*
13100 	 * Ensure that any direct block dependencies have been cleared,
13101 	 * truncations are started, and inode references are journaled.
13102 	 */
13103 	ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
13104 	/*
13105 	 * Write all journal records to prevent rollbacks on devvp.
13106 	 */
13107 	if (vp->v_type == VCHR)
13108 		softdep_flushjournal(vp->v_mount);
13109 	error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
13110 	/*
13111 	 * Ensure that all truncates are written so we won't find deps on
13112 	 * indirect blocks.
13113 	 */
13114 	process_truncates(vp);
13115 	FREE_LOCK(VFSTOUFS(vp->v_mount));
13116 
13117 	return (error);
13118 }
13119 
13120 /*
13121  * This routine is called when we are attempting to sync a buf with
13122  * dependencies.  If waitfor is MNT_NOWAIT it attempts to schedule any
13123  * other IO it can but returns EBUSY if the buffer is not yet able to
13124  * be written.  Dependencies which will not cause rollbacks will always
13125  * return 0.
13126  */
13127 int
13128 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
13129 {
13130 	struct indirdep *indirdep;
13131 	struct pagedep *pagedep;
13132 	struct allocindir *aip;
13133 	struct newblk *newblk;
13134 	struct ufsmount *ump;
13135 	struct buf *nbp;
13136 	struct worklist *wk;
13137 	int i, error;
13138 
13139 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13140 	    ("softdep_sync_buf called on non-softdep filesystem"));
13141 	/*
13142 	 * For VCHR we just don't want to force flush any dependencies that
13143 	 * will cause rollbacks.
13144 	 */
13145 	if (vp->v_type == VCHR) {
13146 		if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
13147 			return (EBUSY);
13148 		return (0);
13149 	}
13150 	ump = VFSTOUFS(vp->v_mount);
13151 	ACQUIRE_LOCK(ump);
13152 	/*
13153 	 * As we hold the buffer locked, none of its dependencies
13154 	 * will disappear.
13155 	 */
13156 	error = 0;
13157 top:
13158 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
13159 		switch (wk->wk_type) {
13160 		case D_ALLOCDIRECT:
13161 		case D_ALLOCINDIR:
13162 			newblk = WK_NEWBLK(wk);
13163 			if (newblk->nb_jnewblk != NULL) {
13164 				if (waitfor == MNT_NOWAIT) {
13165 					error = EBUSY;
13166 					goto out_unlock;
13167 				}
13168 				jwait(&newblk->nb_jnewblk->jn_list, waitfor);
13169 				goto top;
13170 			}
13171 			if (newblk->nb_state & DEPCOMPLETE ||
13172 			    waitfor == MNT_NOWAIT)
13173 				continue;
13174 			nbp = newblk->nb_bmsafemap->sm_buf;
13175 			nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13176 			if (nbp == NULL)
13177 				goto top;
13178 			FREE_LOCK(ump);
13179 			if ((error = bwrite(nbp)) != 0)
13180 				goto out;
13181 			ACQUIRE_LOCK(ump);
13182 			continue;
13183 
13184 		case D_INDIRDEP:
13185 			indirdep = WK_INDIRDEP(wk);
13186 			if (waitfor == MNT_NOWAIT) {
13187 				if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
13188 				    !LIST_EMPTY(&indirdep->ir_deplisthd)) {
13189 					error = EBUSY;
13190 					goto out_unlock;
13191 				}
13192 			}
13193 			if (!TAILQ_EMPTY(&indirdep->ir_trunc))
13194 				panic("softdep_sync_buf: truncation pending.");
13195 		restart:
13196 			LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
13197 				newblk = (struct newblk *)aip;
13198 				if (newblk->nb_jnewblk != NULL) {
13199 					jwait(&newblk->nb_jnewblk->jn_list,
13200 					    waitfor);
13201 					goto restart;
13202 				}
13203 				if (newblk->nb_state & DEPCOMPLETE)
13204 					continue;
13205 				nbp = newblk->nb_bmsafemap->sm_buf;
13206 				nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13207 				if (nbp == NULL)
13208 					goto restart;
13209 				FREE_LOCK(ump);
13210 				if ((error = bwrite(nbp)) != 0)
13211 					goto out;
13212 				ACQUIRE_LOCK(ump);
13213 				goto restart;
13214 			}
13215 			continue;
13216 
13217 		case D_PAGEDEP:
13218 			/*
13219 			 * Only flush directory entries in synchronous passes.
13220 			 */
13221 			if (waitfor != MNT_WAIT) {
13222 				error = EBUSY;
13223 				goto out_unlock;
13224 			}
13225 			/*
13226 			 * While syncing snapshots, we must allow recursive
13227 			 * lookups.
13228 			 */
13229 			BUF_AREC(bp);
13230 			/*
13231 			 * We are trying to sync a directory that may
13232 			 * have dependencies on both its own metadata
13233 			 * and/or dependencies on the inodes of any
13234 			 * recently allocated files. We walk its diradd
13235 			 * lists pushing out the associated inode.
13236 			 */
13237 			pagedep = WK_PAGEDEP(wk);
13238 			for (i = 0; i < DAHASHSZ; i++) {
13239 				if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
13240 					continue;
13241 				error = flush_pagedep_deps(vp, wk->wk_mp,
13242 				    &pagedep->pd_diraddhd[i], bp);
13243 				if (error != 0) {
13244 					if (error != ERELOOKUP)
13245 						BUF_NOREC(bp);
13246 					goto out_unlock;
13247 				}
13248 			}
13249 			BUF_NOREC(bp);
13250 			continue;
13251 
13252 		case D_FREEWORK:
13253 		case D_FREEDEP:
13254 		case D_JSEGDEP:
13255 		case D_JNEWBLK:
13256 			continue;
13257 
13258 		default:
13259 			panic("softdep_sync_buf: Unknown type %s",
13260 			    TYPENAME(wk->wk_type));
13261 			/* NOTREACHED */
13262 		}
13263 	}
13264 out_unlock:
13265 	FREE_LOCK(ump);
13266 out:
13267 	return (error);
13268 }
13269 
13270 /*
13271  * Flush the dependencies associated with an inodedep.
13272  */
13273 static int
13274 flush_inodedep_deps(vp, mp, ino)
13275 	struct vnode *vp;
13276 	struct mount *mp;
13277 	ino_t ino;
13278 {
13279 	struct inodedep *inodedep;
13280 	struct inoref *inoref;
13281 	struct ufsmount *ump;
13282 	int error, waitfor;
13283 
13284 	/*
13285 	 * This work is done in two passes. The first pass grabs most
13286 	 * of the buffers and begins asynchronously writing them. The
13287 	 * only way to wait for these asynchronous writes is to sleep
13288 	 * on the filesystem vnode which may stay busy for a long time
13289 	 * if the filesystem is active. So, instead, we make a second
13290 	 * pass over the dependencies blocking on each write. In the
13291 	 * usual case we will be blocking against a write that we
13292 	 * initiated, so when it is done the dependency will have been
13293 	 * resolved. Thus the second pass is expected to end quickly.
13294 	 * We give a brief window at the top of the loop to allow
13295 	 * any pending I/O to complete.
13296 	 */
13297 	ump = VFSTOUFS(mp);
13298 	LOCK_OWNED(ump);
13299 	for (error = 0, waitfor = MNT_NOWAIT; ; ) {
13300 		if (error)
13301 			return (error);
13302 		FREE_LOCK(ump);
13303 		ACQUIRE_LOCK(ump);
13304 restart:
13305 		if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13306 			return (0);
13307 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13308 			if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13309 			    == DEPCOMPLETE) {
13310 				jwait(&inoref->if_list, MNT_WAIT);
13311 				goto restart;
13312 			}
13313 		}
13314 		if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
13315 		    flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
13316 		    flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
13317 		    flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
13318 			continue;
13319 		/*
13320 		 * If pass2, we are done, otherwise do pass 2.
13321 		 */
13322 		if (waitfor == MNT_WAIT)
13323 			break;
13324 		waitfor = MNT_WAIT;
13325 	}
13326 	/*
13327 	 * Try freeing inodedep in case all dependencies have been removed.
13328 	 */
13329 	if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
13330 		(void) free_inodedep(inodedep);
13331 	return (0);
13332 }
13333 
13334 /*
13335  * Flush an inode dependency list.
13336  */
13337 static int
13338 flush_deplist(listhead, waitfor, errorp)
13339 	struct allocdirectlst *listhead;
13340 	int waitfor;
13341 	int *errorp;
13342 {
13343 	struct allocdirect *adp;
13344 	struct newblk *newblk;
13345 	struct ufsmount *ump;
13346 	struct buf *bp;
13347 
13348 	if ((adp = TAILQ_FIRST(listhead)) == NULL)
13349 		return (0);
13350 	ump = VFSTOUFS(adp->ad_list.wk_mp);
13351 	LOCK_OWNED(ump);
13352 	TAILQ_FOREACH(adp, listhead, ad_next) {
13353 		newblk = (struct newblk *)adp;
13354 		if (newblk->nb_jnewblk != NULL) {
13355 			jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13356 			return (1);
13357 		}
13358 		if (newblk->nb_state & DEPCOMPLETE)
13359 			continue;
13360 		bp = newblk->nb_bmsafemap->sm_buf;
13361 		bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13362 		if (bp == NULL) {
13363 			if (waitfor == MNT_NOWAIT)
13364 				continue;
13365 			return (1);
13366 		}
13367 		FREE_LOCK(ump);
13368 		if (waitfor == MNT_NOWAIT)
13369 			bawrite(bp);
13370 		else
13371 			*errorp = bwrite(bp);
13372 		ACQUIRE_LOCK(ump);
13373 		return (1);
13374 	}
13375 	return (0);
13376 }
13377 
13378 /*
13379  * Flush dependencies associated with an allocdirect block.
13380  */
13381 static int
13382 flush_newblk_dep(vp, mp, lbn)
13383 	struct vnode *vp;
13384 	struct mount *mp;
13385 	ufs_lbn_t lbn;
13386 {
13387 	struct newblk *newblk;
13388 	struct ufsmount *ump;
13389 	struct bufobj *bo;
13390 	struct inode *ip;
13391 	struct buf *bp;
13392 	ufs2_daddr_t blkno;
13393 	int error;
13394 
13395 	error = 0;
13396 	bo = &vp->v_bufobj;
13397 	ip = VTOI(vp);
13398 	blkno = DIP(ip, i_db[lbn]);
13399 	if (blkno == 0)
13400 		panic("flush_newblk_dep: Missing block");
13401 	ump = VFSTOUFS(mp);
13402 	ACQUIRE_LOCK(ump);
13403 	/*
13404 	 * Loop until all dependencies related to this block are satisfied.
13405 	 * We must be careful to restart after each sleep in case a write
13406 	 * completes some part of this process for us.
13407 	 */
13408 	for (;;) {
13409 		if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13410 			FREE_LOCK(ump);
13411 			break;
13412 		}
13413 		if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13414 			panic("flush_newblk_dep: Bad newblk %p", newblk);
13415 		/*
13416 		 * Flush the journal.
13417 		 */
13418 		if (newblk->nb_jnewblk != NULL) {
13419 			jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13420 			continue;
13421 		}
13422 		/*
13423 		 * Write the bitmap dependency.
13424 		 */
13425 		if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13426 			bp = newblk->nb_bmsafemap->sm_buf;
13427 			bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13428 			if (bp == NULL)
13429 				continue;
13430 			FREE_LOCK(ump);
13431 			error = bwrite(bp);
13432 			if (error)
13433 				break;
13434 			ACQUIRE_LOCK(ump);
13435 			continue;
13436 		}
13437 		/*
13438 		 * Write the buffer.
13439 		 */
13440 		FREE_LOCK(ump);
13441 		BO_LOCK(bo);
13442 		bp = gbincore(bo, lbn);
13443 		if (bp != NULL) {
13444 			error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13445 			    LK_INTERLOCK, BO_LOCKPTR(bo));
13446 			if (error == ENOLCK) {
13447 				ACQUIRE_LOCK(ump);
13448 				error = 0;
13449 				continue; /* Slept, retry */
13450 			}
13451 			if (error != 0)
13452 				break;	/* Failed */
13453 			if (bp->b_flags & B_DELWRI) {
13454 				bremfree(bp);
13455 				error = bwrite(bp);
13456 				if (error)
13457 					break;
13458 			} else
13459 				BUF_UNLOCK(bp);
13460 		} else
13461 			BO_UNLOCK(bo);
13462 		/*
13463 		 * We have to wait for the direct pointers to
13464 		 * point at the newdirblk before the dependency
13465 		 * will go away.
13466 		 */
13467 		error = ffs_update(vp, 1);
13468 		if (error)
13469 			break;
13470 		ACQUIRE_LOCK(ump);
13471 	}
13472 	return (error);
13473 }
13474 
13475 /*
13476  * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13477  */
13478 static int
13479 flush_pagedep_deps(pvp, mp, diraddhdp, locked_bp)
13480 	struct vnode *pvp;
13481 	struct mount *mp;
13482 	struct diraddhd *diraddhdp;
13483 	struct buf *locked_bp;
13484 {
13485 	struct inodedep *inodedep;
13486 	struct inoref *inoref;
13487 	struct ufsmount *ump;
13488 	struct diradd *dap;
13489 	struct vnode *vp;
13490 	int error = 0;
13491 	struct buf *bp;
13492 	ino_t inum;
13493 	struct diraddhd unfinished;
13494 
13495 	LIST_INIT(&unfinished);
13496 	ump = VFSTOUFS(mp);
13497 	LOCK_OWNED(ump);
13498 restart:
13499 	while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13500 		/*
13501 		 * Flush ourselves if this directory entry
13502 		 * has a MKDIR_PARENT dependency.
13503 		 */
13504 		if (dap->da_state & MKDIR_PARENT) {
13505 			FREE_LOCK(ump);
13506 			if ((error = ffs_update(pvp, 1)) != 0)
13507 				break;
13508 			ACQUIRE_LOCK(ump);
13509 			/*
13510 			 * If that cleared dependencies, go on to next.
13511 			 */
13512 			if (dap != LIST_FIRST(diraddhdp))
13513 				continue;
13514 			/*
13515 			 * All MKDIR_PARENT dependencies and all the
13516 			 * NEWBLOCK pagedeps that are contained in direct
13517 			 * blocks were resolved by doing above ffs_update.
13518 			 * Pagedeps contained in indirect blocks may
13519 			 * require a complete sync'ing of the directory.
13520 			 * We are in the midst of doing a complete sync,
13521 			 * so if they are not resolved in this pass we
13522 			 * defer them for now as they will be sync'ed by
13523 			 * our caller shortly.
13524 			 */
13525 			LIST_REMOVE(dap, da_pdlist);
13526 			LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13527 			continue;
13528 		}
13529 		/*
13530 		 * A newly allocated directory must have its "." and
13531 		 * ".." entries written out before its name can be
13532 		 * committed in its parent.
13533 		 */
13534 		inum = dap->da_newinum;
13535 		if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13536 			panic("flush_pagedep_deps: lost inode1");
13537 		/*
13538 		 * Wait for any pending journal adds to complete so we don't
13539 		 * cause rollbacks while syncing.
13540 		 */
13541 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13542 			if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13543 			    == DEPCOMPLETE) {
13544 				jwait(&inoref->if_list, MNT_WAIT);
13545 				goto restart;
13546 			}
13547 		}
13548 		if (dap->da_state & MKDIR_BODY) {
13549 			FREE_LOCK(ump);
13550 			error = get_parent_vp(pvp, mp, inum, locked_bp,
13551 			    diraddhdp, &unfinished, &vp);
13552 			if (error != 0)
13553 				break;
13554 			error = flush_newblk_dep(vp, mp, 0);
13555 			/*
13556 			 * If we still have the dependency we might need to
13557 			 * update the vnode to sync the new link count to
13558 			 * disk.
13559 			 */
13560 			if (error == 0 && dap == LIST_FIRST(diraddhdp))
13561 				error = ffs_update(vp, 1);
13562 			vput(vp);
13563 			if (error != 0)
13564 				break;
13565 			ACQUIRE_LOCK(ump);
13566 			/*
13567 			 * If that cleared dependencies, go on to next.
13568 			 */
13569 			if (dap != LIST_FIRST(diraddhdp))
13570 				continue;
13571 			if (dap->da_state & MKDIR_BODY) {
13572 				inodedep_lookup(UFSTOVFS(ump), inum, 0,
13573 				    &inodedep);
13574 				panic("flush_pagedep_deps: MKDIR_BODY "
13575 				    "inodedep %p dap %p vp %p",
13576 				    inodedep, dap, vp);
13577 			}
13578 		}
13579 		/*
13580 		 * Flush the inode on which the directory entry depends.
13581 		 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13582 		 * the only remaining dependency is that the updated inode
13583 		 * count must get pushed to disk. The inode has already
13584 		 * been pushed into its inode buffer (via VOP_UPDATE) at
13585 		 * the time of the reference count change. So we need only
13586 		 * locate that buffer, ensure that there will be no rollback
13587 		 * caused by a bitmap dependency, then write the inode buffer.
13588 		 */
13589 retry:
13590 		if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13591 			panic("flush_pagedep_deps: lost inode");
13592 		/*
13593 		 * If the inode still has bitmap dependencies,
13594 		 * push them to disk.
13595 		 */
13596 		if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13597 			bp = inodedep->id_bmsafemap->sm_buf;
13598 			bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13599 			if (bp == NULL)
13600 				goto retry;
13601 			FREE_LOCK(ump);
13602 			if ((error = bwrite(bp)) != 0)
13603 				break;
13604 			ACQUIRE_LOCK(ump);
13605 			if (dap != LIST_FIRST(diraddhdp))
13606 				continue;
13607 		}
13608 		/*
13609 		 * If the inode is still sitting in a buffer waiting
13610 		 * to be written or waiting for the link count to be
13611 		 * adjusted update it here to flush it to disk.
13612 		 */
13613 		if (dap == LIST_FIRST(diraddhdp)) {
13614 			FREE_LOCK(ump);
13615 			error = get_parent_vp(pvp, mp, inum, locked_bp,
13616 			    diraddhdp, &unfinished, &vp);
13617 			if (error != 0)
13618 				break;
13619 			error = ffs_update(vp, 1);
13620 			vput(vp);
13621 			if (error)
13622 				break;
13623 			ACQUIRE_LOCK(ump);
13624 		}
13625 		/*
13626 		 * If we have failed to get rid of all the dependencies
13627 		 * then something is seriously wrong.
13628 		 */
13629 		if (dap == LIST_FIRST(diraddhdp)) {
13630 			inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13631 			panic("flush_pagedep_deps: failed to flush "
13632 			    "inodedep %p ino %ju dap %p",
13633 			    inodedep, (uintmax_t)inum, dap);
13634 		}
13635 	}
13636 	if (error)
13637 		ACQUIRE_LOCK(ump);
13638 	while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13639 		LIST_REMOVE(dap, da_pdlist);
13640 		LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13641 	}
13642 	return (error);
13643 }
13644 
13645 /*
13646  * A large burst of file addition or deletion activity can drive the
13647  * memory load excessively high. First attempt to slow things down
13648  * using the techniques below. If that fails, this routine requests
13649  * the offending operations to fall back to running synchronously
13650  * until the memory load returns to a reasonable level.
13651  */
13652 int
13653 softdep_slowdown(vp)
13654 	struct vnode *vp;
13655 {
13656 	struct ufsmount *ump;
13657 	int jlow;
13658 	int max_softdeps_hard;
13659 
13660 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13661 	    ("softdep_slowdown called on non-softdep filesystem"));
13662 	ump = VFSTOUFS(vp->v_mount);
13663 	ACQUIRE_LOCK(ump);
13664 	jlow = 0;
13665 	/*
13666 	 * Check for journal space if needed.
13667 	 */
13668 	if (DOINGSUJ(vp)) {
13669 		if (journal_space(ump, 0) == 0)
13670 			jlow = 1;
13671 	}
13672 	/*
13673 	 * If the system is under its limits and our filesystem is
13674 	 * not responsible for more than our share of the usage and
13675 	 * we are not low on journal space, then no need to slow down.
13676 	 */
13677 	max_softdeps_hard = max_softdeps * 11 / 10;
13678 	if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13679 	    dep_current[D_INODEDEP] < max_softdeps_hard &&
13680 	    dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13681 	    dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13682 	    ump->softdep_curdeps[D_DIRREM] <
13683 	    (max_softdeps_hard / 2) / stat_flush_threads &&
13684 	    ump->softdep_curdeps[D_INODEDEP] <
13685 	    max_softdeps_hard / stat_flush_threads &&
13686 	    ump->softdep_curdeps[D_INDIRDEP] <
13687 	    (max_softdeps_hard / 1000) / stat_flush_threads &&
13688 	    ump->softdep_curdeps[D_FREEBLKS] <
13689 	    max_softdeps_hard / stat_flush_threads) {
13690 		FREE_LOCK(ump);
13691   		return (0);
13692 	}
13693 	/*
13694 	 * If the journal is low or our filesystem is over its limit
13695 	 * then speedup the cleanup.
13696 	 */
13697 	if (ump->softdep_curdeps[D_INDIRDEP] <
13698 	    (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13699 		softdep_speedup(ump);
13700 	stat_sync_limit_hit += 1;
13701 	FREE_LOCK(ump);
13702 	/*
13703 	 * We only slow down the rate at which new dependencies are
13704 	 * generated if we are not using journaling. With journaling,
13705 	 * the cleanup should always be sufficient to keep things
13706 	 * under control.
13707 	 */
13708 	if (DOINGSUJ(vp))
13709 		return (0);
13710 	return (1);
13711 }
13712 
13713 static int
13714 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused)
13715 {
13716 	return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 &&
13717 	    ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0));
13718 }
13719 
13720 static void
13721 softdep_request_cleanup_inactivate(struct mount *mp)
13722 {
13723 	struct vnode *vp, *mvp;
13724 	int error;
13725 
13726 	MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter,
13727 	    NULL) {
13728 		vholdl(vp);
13729 		vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
13730 		VI_LOCK(vp);
13731 		if (vp->v_data != NULL && vp->v_usecount == 0) {
13732 			while ((vp->v_iflag & VI_OWEINACT) != 0) {
13733 				error = vinactive(vp);
13734 				if (error != 0 && error != ERELOOKUP)
13735 					break;
13736 			}
13737 			atomic_add_int(&stat_delayed_inact, 1);
13738 		}
13739 		VOP_UNLOCK(vp);
13740 		vdropl(vp);
13741 	}
13742 }
13743 
13744 /*
13745  * Called by the allocation routines when they are about to fail
13746  * in the hope that we can free up the requested resource (inodes
13747  * or disk space).
13748  *
13749  * First check to see if the work list has anything on it. If it has,
13750  * clean up entries until we successfully free the requested resource.
13751  * Because this process holds inodes locked, we cannot handle any remove
13752  * requests that might block on a locked inode as that could lead to
13753  * deadlock. If the worklist yields none of the requested resource,
13754  * start syncing out vnodes to free up the needed space.
13755  */
13756 int
13757 softdep_request_cleanup(fs, vp, cred, resource)
13758 	struct fs *fs;
13759 	struct vnode *vp;
13760 	struct ucred *cred;
13761 	int resource;
13762 {
13763 	struct ufsmount *ump;
13764 	struct mount *mp;
13765 	long starttime;
13766 	ufs2_daddr_t needed;
13767 	int error, failed_vnode;
13768 
13769 	/*
13770 	 * If we are being called because of a process doing a
13771 	 * copy-on-write, then it is not safe to process any
13772 	 * worklist items as we will recurse into the copyonwrite
13773 	 * routine.  This will result in an incoherent snapshot.
13774 	 * If the vnode that we hold is a snapshot, we must avoid
13775 	 * handling other resources that could cause deadlock.
13776 	 */
13777 	if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13778 		return (0);
13779 
13780 	if (resource == FLUSH_BLOCKS_WAIT)
13781 		stat_cleanup_blkrequests += 1;
13782 	else
13783 		stat_cleanup_inorequests += 1;
13784 
13785 	mp = vp->v_mount;
13786 	ump = VFSTOUFS(mp);
13787 	mtx_assert(UFS_MTX(ump), MA_OWNED);
13788 	UFS_UNLOCK(ump);
13789 	error = ffs_update(vp, 1);
13790 	if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13791 		UFS_LOCK(ump);
13792 		return (0);
13793 	}
13794 	/*
13795 	 * If we are in need of resources, start by cleaning up
13796 	 * any block removals associated with our inode.
13797 	 */
13798 	ACQUIRE_LOCK(ump);
13799 	process_removes(vp);
13800 	process_truncates(vp);
13801 	FREE_LOCK(ump);
13802 	/*
13803 	 * Now clean up at least as many resources as we will need.
13804 	 *
13805 	 * When requested to clean up inodes, the number that are needed
13806 	 * is set by the number of simultaneous writers (mnt_writeopcount)
13807 	 * plus a bit of slop (2) in case some more writers show up while
13808 	 * we are cleaning.
13809 	 *
13810 	 * When requested to free up space, the amount of space that
13811 	 * we need is enough blocks to allocate a full-sized segment
13812 	 * (fs_contigsumsize). The number of such segments that will
13813 	 * be needed is set by the number of simultaneous writers
13814 	 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13815 	 * writers show up while we are cleaning.
13816 	 *
13817 	 * Additionally, if we are unpriviledged and allocating space,
13818 	 * we need to ensure that we clean up enough blocks to get the
13819 	 * needed number of blocks over the threshold of the minimum
13820 	 * number of blocks required to be kept free by the filesystem
13821 	 * (fs_minfree).
13822 	 */
13823 	if (resource == FLUSH_INODES_WAIT) {
13824 		needed = vfs_mount_fetch_counter(vp->v_mount,
13825 		    MNT_COUNT_WRITEOPCOUNT) + 2;
13826 	} else if (resource == FLUSH_BLOCKS_WAIT) {
13827 		needed = (vfs_mount_fetch_counter(vp->v_mount,
13828 		    MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13829 		if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13830 			needed += fragstoblks(fs,
13831 			    roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13832 			    fs->fs_cstotal.cs_nffree, fs->fs_frag));
13833 	} else {
13834 		printf("softdep_request_cleanup: Unknown resource type %d\n",
13835 		    resource);
13836 		UFS_LOCK(ump);
13837 		return (0);
13838 	}
13839 	starttime = time_second;
13840 retry:
13841 	if (resource == FLUSH_BLOCKS_WAIT &&
13842 	    fs->fs_cstotal.cs_nbfree <= needed)
13843 		softdep_send_speedup(ump, needed * fs->fs_bsize,
13844 		    BIO_SPEEDUP_TRIM);
13845 	if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13846 	    fs->fs_cstotal.cs_nbfree <= needed) ||
13847 	    (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13848 	    fs->fs_cstotal.cs_nifree <= needed)) {
13849 		ACQUIRE_LOCK(ump);
13850 		if (ump->softdep_on_worklist > 0 &&
13851 		    process_worklist_item(UFSTOVFS(ump),
13852 		    ump->softdep_on_worklist, LK_NOWAIT) != 0)
13853 			stat_worklist_push += 1;
13854 		FREE_LOCK(ump);
13855 	}
13856 
13857 	/*
13858 	 * Check that there are vnodes pending inactivation.  As they
13859 	 * have been unlinked, inactivating them will free up their
13860 	 * inodes.
13861 	 */
13862 	ACQUIRE_LOCK(ump);
13863 	if (resource == FLUSH_INODES_WAIT &&
13864 	    fs->fs_cstotal.cs_nifree <= needed &&
13865 	    fs->fs_pendinginodes <= needed) {
13866 		if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) {
13867 			ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE;
13868 			FREE_LOCK(ump);
13869 			softdep_request_cleanup_inactivate(mp);
13870 			ACQUIRE_LOCK(ump);
13871 			ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE;
13872 			wakeup(&ump->um_softdep->sd_flags);
13873 		} else {
13874 			while ((ump->um_softdep->sd_flags &
13875 			    FLUSH_DI_ACTIVE) != 0) {
13876 				msleep(&ump->um_softdep->sd_flags,
13877 				    LOCK_PTR(ump), PVM, "ffsvina", hz);
13878 			}
13879 		}
13880 	}
13881 	FREE_LOCK(ump);
13882 
13883 	/*
13884 	 * If we still need resources and there are no more worklist
13885 	 * entries to process to obtain them, we have to start flushing
13886 	 * the dirty vnodes to force the release of additional requests
13887 	 * to the worklist that we can then process to reap addition
13888 	 * resources. We walk the vnodes associated with the mount point
13889 	 * until we get the needed worklist requests that we can reap.
13890 	 *
13891 	 * If there are several threads all needing to clean the same
13892 	 * mount point, only one is allowed to walk the mount list.
13893 	 * When several threads all try to walk the same mount list,
13894 	 * they end up competing with each other and often end up in
13895 	 * livelock. This approach ensures that forward progress is
13896 	 * made at the cost of occational ENOSPC errors being returned
13897 	 * that might otherwise have been avoided.
13898 	 */
13899 	error = 1;
13900 	if ((resource == FLUSH_BLOCKS_WAIT &&
13901 	     fs->fs_cstotal.cs_nbfree <= needed) ||
13902 	    (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13903 	     fs->fs_cstotal.cs_nifree <= needed)) {
13904 		ACQUIRE_LOCK(ump);
13905 		if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13906 			ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13907 			FREE_LOCK(ump);
13908 			failed_vnode = softdep_request_cleanup_flush(mp, ump);
13909 			ACQUIRE_LOCK(ump);
13910 			ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13911 			wakeup(&ump->um_softdep->sd_flags);
13912 			FREE_LOCK(ump);
13913 			if (ump->softdep_on_worklist > 0) {
13914 				stat_cleanup_retries += 1;
13915 				if (!failed_vnode)
13916 					goto retry;
13917 			}
13918 		} else {
13919 			while ((ump->um_softdep->sd_flags &
13920 			    FLUSH_RC_ACTIVE) != 0) {
13921 				msleep(&ump->um_softdep->sd_flags,
13922 				    LOCK_PTR(ump), PVM, "ffsrca", hz);
13923 			}
13924 			FREE_LOCK(ump);
13925 			error = 0;
13926 		}
13927 		stat_cleanup_failures += 1;
13928 	}
13929 	if (time_second - starttime > stat_cleanup_high_delay)
13930 		stat_cleanup_high_delay = time_second - starttime;
13931 	UFS_LOCK(ump);
13932 	return (error);
13933 }
13934 
13935 /*
13936  * Scan the vnodes for the specified mount point flushing out any
13937  * vnodes that can be locked without waiting. Finally, try to flush
13938  * the device associated with the mount point if it can be locked
13939  * without waiting.
13940  *
13941  * We return 0 if we were able to lock every vnode in our scan.
13942  * If we had to skip one or more vnodes, we return 1.
13943  */
13944 static int
13945 softdep_request_cleanup_flush(mp, ump)
13946 	struct mount *mp;
13947 	struct ufsmount *ump;
13948 {
13949 	struct thread *td;
13950 	struct vnode *lvp, *mvp;
13951 	int failed_vnode;
13952 
13953 	failed_vnode = 0;
13954 	td = curthread;
13955 	MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13956 		if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13957 			VI_UNLOCK(lvp);
13958 			continue;
13959 		}
13960 		if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) {
13961 			failed_vnode = 1;
13962 			continue;
13963 		}
13964 		if (lvp->v_vflag & VV_NOSYNC) {	/* unlinked */
13965 			vput(lvp);
13966 			continue;
13967 		}
13968 		(void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13969 		vput(lvp);
13970 	}
13971 	lvp = ump->um_devvp;
13972 	if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13973 		VOP_FSYNC(lvp, MNT_NOWAIT, td);
13974 		VOP_UNLOCK(lvp);
13975 	}
13976 	return (failed_vnode);
13977 }
13978 
13979 static bool
13980 softdep_excess_items(struct ufsmount *ump, int item)
13981 {
13982 
13983 	KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13984 	return (dep_current[item] > max_softdeps &&
13985 	    ump->softdep_curdeps[item] > max_softdeps /
13986 	    stat_flush_threads);
13987 }
13988 
13989 static void
13990 schedule_cleanup(struct mount *mp)
13991 {
13992 	struct ufsmount *ump;
13993 	struct thread *td;
13994 
13995 	ump = VFSTOUFS(mp);
13996 	LOCK_OWNED(ump);
13997 	FREE_LOCK(ump);
13998 	td = curthread;
13999 	if ((td->td_pflags & TDP_KTHREAD) != 0 &&
14000 	    (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
14001 		/*
14002 		 * No ast is delivered to kernel threads, so nobody
14003 		 * would deref the mp.  Some kernel threads
14004 		 * explicitely check for AST, e.g. NFS daemon does
14005 		 * this in the serving loop.
14006 		 */
14007 		return;
14008 	}
14009 	if (td->td_su != NULL)
14010 		vfs_rel(td->td_su);
14011 	vfs_ref(mp);
14012 	td->td_su = mp;
14013 	thread_lock(td);
14014 	td->td_flags |= TDF_ASTPENDING;
14015 	thread_unlock(td);
14016 }
14017 
14018 static void
14019 softdep_ast_cleanup_proc(struct thread *td)
14020 {
14021 	struct mount *mp;
14022 	struct ufsmount *ump;
14023 	int error;
14024 	bool req;
14025 
14026 	while ((mp = td->td_su) != NULL) {
14027 		td->td_su = NULL;
14028 		error = vfs_busy(mp, MBF_NOWAIT);
14029 		vfs_rel(mp);
14030 		if (error != 0)
14031 			return;
14032 		if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
14033 			ump = VFSTOUFS(mp);
14034 			for (;;) {
14035 				req = false;
14036 				ACQUIRE_LOCK(ump);
14037 				if (softdep_excess_items(ump, D_INODEDEP)) {
14038 					req = true;
14039 					request_cleanup(mp, FLUSH_INODES);
14040 				}
14041 				if (softdep_excess_items(ump, D_DIRREM)) {
14042 					req = true;
14043 					request_cleanup(mp, FLUSH_BLOCKS);
14044 				}
14045 				FREE_LOCK(ump);
14046 				if (softdep_excess_items(ump, D_NEWBLK) ||
14047 				    softdep_excess_items(ump, D_ALLOCDIRECT) ||
14048 				    softdep_excess_items(ump, D_ALLOCINDIR)) {
14049 					error = vn_start_write(NULL, &mp,
14050 					    V_WAIT);
14051 					if (error == 0) {
14052 						req = true;
14053 						VFS_SYNC(mp, MNT_WAIT);
14054 						vn_finished_write(mp);
14055 					}
14056 				}
14057 				if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
14058 					break;
14059 			}
14060 		}
14061 		vfs_unbusy(mp);
14062 	}
14063 	if ((mp = td->td_su) != NULL) {
14064 		td->td_su = NULL;
14065 		vfs_rel(mp);
14066 	}
14067 }
14068 
14069 /*
14070  * If memory utilization has gotten too high, deliberately slow things
14071  * down and speed up the I/O processing.
14072  */
14073 static int
14074 request_cleanup(mp, resource)
14075 	struct mount *mp;
14076 	int resource;
14077 {
14078 	struct thread *td = curthread;
14079 	struct ufsmount *ump;
14080 
14081 	ump = VFSTOUFS(mp);
14082 	LOCK_OWNED(ump);
14083 	/*
14084 	 * We never hold up the filesystem syncer or buf daemon.
14085 	 */
14086 	if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
14087 		return (0);
14088 	/*
14089 	 * First check to see if the work list has gotten backlogged.
14090 	 * If it has, co-opt this process to help clean up two entries.
14091 	 * Because this process may hold inodes locked, we cannot
14092 	 * handle any remove requests that might block on a locked
14093 	 * inode as that could lead to deadlock.  We set TDP_SOFTDEP
14094 	 * to avoid recursively processing the worklist.
14095 	 */
14096 	if (ump->softdep_on_worklist > max_softdeps / 10) {
14097 		td->td_pflags |= TDP_SOFTDEP;
14098 		process_worklist_item(mp, 2, LK_NOWAIT);
14099 		td->td_pflags &= ~TDP_SOFTDEP;
14100 		stat_worklist_push += 2;
14101 		return(1);
14102 	}
14103 	/*
14104 	 * Next, we attempt to speed up the syncer process. If that
14105 	 * is successful, then we allow the process to continue.
14106 	 */
14107 	if (softdep_speedup(ump) &&
14108 	    resource != FLUSH_BLOCKS_WAIT &&
14109 	    resource != FLUSH_INODES_WAIT)
14110 		return(0);
14111 	/*
14112 	 * If we are resource constrained on inode dependencies, try
14113 	 * flushing some dirty inodes. Otherwise, we are constrained
14114 	 * by file deletions, so try accelerating flushes of directories
14115 	 * with removal dependencies. We would like to do the cleanup
14116 	 * here, but we probably hold an inode locked at this point and
14117 	 * that might deadlock against one that we try to clean. So,
14118 	 * the best that we can do is request the syncer daemon to do
14119 	 * the cleanup for us.
14120 	 */
14121 	switch (resource) {
14122 	case FLUSH_INODES:
14123 	case FLUSH_INODES_WAIT:
14124 		ACQUIRE_GBLLOCK(&lk);
14125 		stat_ino_limit_push += 1;
14126 		req_clear_inodedeps += 1;
14127 		FREE_GBLLOCK(&lk);
14128 		stat_countp = &stat_ino_limit_hit;
14129 		break;
14130 
14131 	case FLUSH_BLOCKS:
14132 	case FLUSH_BLOCKS_WAIT:
14133 		ACQUIRE_GBLLOCK(&lk);
14134 		stat_blk_limit_push += 1;
14135 		req_clear_remove += 1;
14136 		FREE_GBLLOCK(&lk);
14137 		stat_countp = &stat_blk_limit_hit;
14138 		break;
14139 
14140 	default:
14141 		panic("request_cleanup: unknown type");
14142 	}
14143 	/*
14144 	 * Hopefully the syncer daemon will catch up and awaken us.
14145 	 * We wait at most tickdelay before proceeding in any case.
14146 	 */
14147 	ACQUIRE_GBLLOCK(&lk);
14148 	FREE_LOCK(ump);
14149 	proc_waiting += 1;
14150 	if (callout_pending(&softdep_callout) == FALSE)
14151 		callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
14152 		    pause_timer, 0);
14153 
14154 	if ((td->td_pflags & TDP_KTHREAD) == 0)
14155 		msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
14156 	proc_waiting -= 1;
14157 	FREE_GBLLOCK(&lk);
14158 	ACQUIRE_LOCK(ump);
14159 	return (1);
14160 }
14161 
14162 /*
14163  * Awaken processes pausing in request_cleanup and clear proc_waiting
14164  * to indicate that there is no longer a timer running. Pause_timer
14165  * will be called with the global softdep mutex (&lk) locked.
14166  */
14167 static void
14168 pause_timer(arg)
14169 	void *arg;
14170 {
14171 
14172 	GBLLOCK_OWNED(&lk);
14173 	/*
14174 	 * The callout_ API has acquired mtx and will hold it around this
14175 	 * function call.
14176 	 */
14177 	*stat_countp += proc_waiting;
14178 	wakeup(&proc_waiting);
14179 }
14180 
14181 /*
14182  * If requested, try removing inode or removal dependencies.
14183  */
14184 static void
14185 check_clear_deps(mp)
14186 	struct mount *mp;
14187 {
14188 	struct ufsmount *ump;
14189 	bool suj_susp;
14190 
14191 	/*
14192 	 * Tell the lower layers that any TRIM or WRITE transactions that have
14193 	 * been delayed for performance reasons should proceed to help alleviate
14194 	 * the shortage faster. The race between checking req_* and the softdep
14195 	 * mutex (lk) is fine since this is an advisory operation that at most
14196 	 * causes deferred work to be done sooner.
14197 	 */
14198 	ump = VFSTOUFS(mp);
14199 	suj_susp = MOUNTEDSUJ(mp) && ump->softdep_jblocks->jb_suspended;
14200 	if (req_clear_remove || req_clear_inodedeps || suj_susp) {
14201 		FREE_LOCK(ump);
14202 		softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
14203 		ACQUIRE_LOCK(ump);
14204 	}
14205 
14206 	/*
14207 	 * If we are suspended, it may be because of our using
14208 	 * too many inodedeps, so help clear them out.
14209 	 */
14210 	if (suj_susp)
14211 		clear_inodedeps(mp);
14212 
14213 	/*
14214 	 * General requests for cleanup of backed up dependencies
14215 	 */
14216 	ACQUIRE_GBLLOCK(&lk);
14217 	if (req_clear_inodedeps) {
14218 		req_clear_inodedeps -= 1;
14219 		FREE_GBLLOCK(&lk);
14220 		clear_inodedeps(mp);
14221 		ACQUIRE_GBLLOCK(&lk);
14222 		wakeup(&proc_waiting);
14223 	}
14224 	if (req_clear_remove) {
14225 		req_clear_remove -= 1;
14226 		FREE_GBLLOCK(&lk);
14227 		clear_remove(mp);
14228 		ACQUIRE_GBLLOCK(&lk);
14229 		wakeup(&proc_waiting);
14230 	}
14231 	FREE_GBLLOCK(&lk);
14232 }
14233 
14234 /*
14235  * Flush out a directory with at least one removal dependency in an effort to
14236  * reduce the number of dirrem, freefile, and freeblks dependency structures.
14237  */
14238 static void
14239 clear_remove(mp)
14240 	struct mount *mp;
14241 {
14242 	struct pagedep_hashhead *pagedephd;
14243 	struct pagedep *pagedep;
14244 	struct ufsmount *ump;
14245 	struct vnode *vp;
14246 	struct bufobj *bo;
14247 	int error, cnt;
14248 	ino_t ino;
14249 
14250 	ump = VFSTOUFS(mp);
14251 	LOCK_OWNED(ump);
14252 
14253 	for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
14254 		pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
14255 		if (ump->pagedep_nextclean > ump->pagedep_hash_size)
14256 			ump->pagedep_nextclean = 0;
14257 		LIST_FOREACH(pagedep, pagedephd, pd_hash) {
14258 			if (LIST_EMPTY(&pagedep->pd_dirremhd))
14259 				continue;
14260 			ino = pagedep->pd_ino;
14261 			if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14262 				continue;
14263 			FREE_LOCK(ump);
14264 
14265 			/*
14266 			 * Let unmount clear deps
14267 			 */
14268 			error = vfs_busy(mp, MBF_NOWAIT);
14269 			if (error != 0)
14270 				goto finish_write;
14271 			error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14272 			     FFSV_FORCEINSMQ);
14273 			vfs_unbusy(mp);
14274 			if (error != 0) {
14275 				softdep_error("clear_remove: vget", error);
14276 				goto finish_write;
14277 			}
14278 			MPASS(VTOI(vp)->i_mode != 0);
14279 			if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14280 				softdep_error("clear_remove: fsync", error);
14281 			bo = &vp->v_bufobj;
14282 			BO_LOCK(bo);
14283 			drain_output(vp);
14284 			BO_UNLOCK(bo);
14285 			vput(vp);
14286 		finish_write:
14287 			vn_finished_write(mp);
14288 			ACQUIRE_LOCK(ump);
14289 			return;
14290 		}
14291 	}
14292 }
14293 
14294 /*
14295  * Clear out a block of dirty inodes in an effort to reduce
14296  * the number of inodedep dependency structures.
14297  */
14298 static void
14299 clear_inodedeps(mp)
14300 	struct mount *mp;
14301 {
14302 	struct inodedep_hashhead *inodedephd;
14303 	struct inodedep *inodedep;
14304 	struct ufsmount *ump;
14305 	struct vnode *vp;
14306 	struct fs *fs;
14307 	int error, cnt;
14308 	ino_t firstino, lastino, ino;
14309 
14310 	ump = VFSTOUFS(mp);
14311 	fs = ump->um_fs;
14312 	LOCK_OWNED(ump);
14313 	/*
14314 	 * Pick a random inode dependency to be cleared.
14315 	 * We will then gather up all the inodes in its block
14316 	 * that have dependencies and flush them out.
14317 	 */
14318 	for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
14319 		inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
14320 		if (ump->inodedep_nextclean > ump->inodedep_hash_size)
14321 			ump->inodedep_nextclean = 0;
14322 		if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
14323 			break;
14324 	}
14325 	if (inodedep == NULL)
14326 		return;
14327 	/*
14328 	 * Find the last inode in the block with dependencies.
14329 	 */
14330 	firstino = rounddown2(inodedep->id_ino, INOPB(fs));
14331 	for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
14332 		if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
14333 			break;
14334 	/*
14335 	 * Asynchronously push all but the last inode with dependencies.
14336 	 * Synchronously push the last inode with dependencies to ensure
14337 	 * that the inode block gets written to free up the inodedeps.
14338 	 */
14339 	for (ino = firstino; ino <= lastino; ino++) {
14340 		if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
14341 			continue;
14342 		if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14343 			continue;
14344 		FREE_LOCK(ump);
14345 		error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
14346 		if (error != 0) {
14347 			vn_finished_write(mp);
14348 			ACQUIRE_LOCK(ump);
14349 			return;
14350 		}
14351 		if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14352 		    FFSV_FORCEINSMQ)) != 0) {
14353 			softdep_error("clear_inodedeps: vget", error);
14354 			vfs_unbusy(mp);
14355 			vn_finished_write(mp);
14356 			ACQUIRE_LOCK(ump);
14357 			return;
14358 		}
14359 		vfs_unbusy(mp);
14360 		if (VTOI(vp)->i_mode == 0) {
14361 			vgone(vp);
14362 		} else if (ino == lastino) {
14363 			do {
14364 				error = ffs_syncvnode(vp, MNT_WAIT, 0);
14365 			} while (error == ERELOOKUP);
14366 			if (error != 0)
14367 				softdep_error("clear_inodedeps: fsync1", error);
14368 		} else {
14369 			if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14370 				softdep_error("clear_inodedeps: fsync2", error);
14371 			BO_LOCK(&vp->v_bufobj);
14372 			drain_output(vp);
14373 			BO_UNLOCK(&vp->v_bufobj);
14374 		}
14375 		vput(vp);
14376 		vn_finished_write(mp);
14377 		ACQUIRE_LOCK(ump);
14378 	}
14379 }
14380 
14381 void
14382 softdep_buf_append(bp, wkhd)
14383 	struct buf *bp;
14384 	struct workhead *wkhd;
14385 {
14386 	struct worklist *wk;
14387 	struct ufsmount *ump;
14388 
14389 	if ((wk = LIST_FIRST(wkhd)) == NULL)
14390 		return;
14391 	KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14392 	    ("softdep_buf_append called on non-softdep filesystem"));
14393 	ump = VFSTOUFS(wk->wk_mp);
14394 	ACQUIRE_LOCK(ump);
14395 	while ((wk = LIST_FIRST(wkhd)) != NULL) {
14396 		WORKLIST_REMOVE(wk);
14397 		WORKLIST_INSERT(&bp->b_dep, wk);
14398 	}
14399 	FREE_LOCK(ump);
14400 
14401 }
14402 
14403 void
14404 softdep_inode_append(ip, cred, wkhd)
14405 	struct inode *ip;
14406 	struct ucred *cred;
14407 	struct workhead *wkhd;
14408 {
14409 	struct buf *bp;
14410 	struct fs *fs;
14411 	struct ufsmount *ump;
14412 	int error;
14413 
14414 	ump = ITOUMP(ip);
14415 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14416 	    ("softdep_inode_append called on non-softdep filesystem"));
14417 	fs = ump->um_fs;
14418 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14419 	    (int)fs->fs_bsize, cred, &bp);
14420 	if (error) {
14421 		bqrelse(bp);
14422 		softdep_freework(wkhd);
14423 		return;
14424 	}
14425 	softdep_buf_append(bp, wkhd);
14426 	bqrelse(bp);
14427 }
14428 
14429 void
14430 softdep_freework(wkhd)
14431 	struct workhead *wkhd;
14432 {
14433 	struct worklist *wk;
14434 	struct ufsmount *ump;
14435 
14436 	if ((wk = LIST_FIRST(wkhd)) == NULL)
14437 		return;
14438 	KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14439 	    ("softdep_freework called on non-softdep filesystem"));
14440 	ump = VFSTOUFS(wk->wk_mp);
14441 	ACQUIRE_LOCK(ump);
14442 	handle_jwork(wkhd);
14443 	FREE_LOCK(ump);
14444 }
14445 
14446 static struct ufsmount *
14447 softdep_bp_to_mp(bp)
14448 	struct buf *bp;
14449 {
14450 	struct mount *mp;
14451 	struct vnode *vp;
14452 
14453 	if (LIST_EMPTY(&bp->b_dep))
14454 		return (NULL);
14455 	vp = bp->b_vp;
14456 	KASSERT(vp != NULL,
14457 	    ("%s, buffer with dependencies lacks vnode", __func__));
14458 
14459 	/*
14460 	 * The ump mount point is stable after we get a correct
14461 	 * pointer, since bp is locked and this prevents unmount from
14462 	 * proceeding.  But to get to it, we cannot dereference bp->b_dep
14463 	 * head wk_mp, because we do not yet own SU ump lock and
14464 	 * workitem might be freed while dereferenced.
14465 	 */
14466 retry:
14467 	switch (vp->v_type) {
14468 	case VCHR:
14469 		VI_LOCK(vp);
14470 		mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14471 		VI_UNLOCK(vp);
14472 		if (mp == NULL)
14473 			goto retry;
14474 		break;
14475 	case VREG:
14476 	case VDIR:
14477 	case VLNK:
14478 	case VFIFO:
14479 	case VSOCK:
14480 		mp = vp->v_mount;
14481 		break;
14482 	case VBLK:
14483 		vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14484 		/* FALLTHROUGH */
14485 	case VNON:
14486 	case VBAD:
14487 	case VMARKER:
14488 		mp = NULL;
14489 		break;
14490 	default:
14491 		vn_printf(vp, "unknown vnode type");
14492 		mp = NULL;
14493 		break;
14494 	}
14495 	return (VFSTOUFS(mp));
14496 }
14497 
14498 /*
14499  * Function to determine if the buffer has outstanding dependencies
14500  * that will cause a roll-back if the buffer is written. If wantcount
14501  * is set, return number of dependencies, otherwise just yes or no.
14502  */
14503 static int
14504 softdep_count_dependencies(bp, wantcount)
14505 	struct buf *bp;
14506 	int wantcount;
14507 {
14508 	struct worklist *wk;
14509 	struct ufsmount *ump;
14510 	struct bmsafemap *bmsafemap;
14511 	struct freework *freework;
14512 	struct inodedep *inodedep;
14513 	struct indirdep *indirdep;
14514 	struct freeblks *freeblks;
14515 	struct allocindir *aip;
14516 	struct pagedep *pagedep;
14517 	struct dirrem *dirrem;
14518 	struct newblk *newblk;
14519 	struct mkdir *mkdir;
14520 	struct diradd *dap;
14521 	int i, retval;
14522 
14523 	ump = softdep_bp_to_mp(bp);
14524 	if (ump == NULL)
14525 		return (0);
14526 	retval = 0;
14527 	ACQUIRE_LOCK(ump);
14528 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14529 		switch (wk->wk_type) {
14530 		case D_INODEDEP:
14531 			inodedep = WK_INODEDEP(wk);
14532 			if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14533 				/* bitmap allocation dependency */
14534 				retval += 1;
14535 				if (!wantcount)
14536 					goto out;
14537 			}
14538 			if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14539 				/* direct block pointer dependency */
14540 				retval += 1;
14541 				if (!wantcount)
14542 					goto out;
14543 			}
14544 			if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14545 				/* direct block pointer dependency */
14546 				retval += 1;
14547 				if (!wantcount)
14548 					goto out;
14549 			}
14550 			if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14551 				/* Add reference dependency. */
14552 				retval += 1;
14553 				if (!wantcount)
14554 					goto out;
14555 			}
14556 			continue;
14557 
14558 		case D_INDIRDEP:
14559 			indirdep = WK_INDIRDEP(wk);
14560 
14561 			TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14562 				/* indirect truncation dependency */
14563 				retval += 1;
14564 				if (!wantcount)
14565 					goto out;
14566 			}
14567 
14568 			LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14569 				/* indirect block pointer dependency */
14570 				retval += 1;
14571 				if (!wantcount)
14572 					goto out;
14573 			}
14574 			continue;
14575 
14576 		case D_PAGEDEP:
14577 			pagedep = WK_PAGEDEP(wk);
14578 			LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14579 				if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14580 					/* Journal remove ref dependency. */
14581 					retval += 1;
14582 					if (!wantcount)
14583 						goto out;
14584 				}
14585 			}
14586 			for (i = 0; i < DAHASHSZ; i++) {
14587 				LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14588 					/* directory entry dependency */
14589 					retval += 1;
14590 					if (!wantcount)
14591 						goto out;
14592 				}
14593 			}
14594 			continue;
14595 
14596 		case D_BMSAFEMAP:
14597 			bmsafemap = WK_BMSAFEMAP(wk);
14598 			if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14599 				/* Add reference dependency. */
14600 				retval += 1;
14601 				if (!wantcount)
14602 					goto out;
14603 			}
14604 			if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14605 				/* Allocate block dependency. */
14606 				retval += 1;
14607 				if (!wantcount)
14608 					goto out;
14609 			}
14610 			continue;
14611 
14612 		case D_FREEBLKS:
14613 			freeblks = WK_FREEBLKS(wk);
14614 			if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14615 				/* Freeblk journal dependency. */
14616 				retval += 1;
14617 				if (!wantcount)
14618 					goto out;
14619 			}
14620 			continue;
14621 
14622 		case D_ALLOCDIRECT:
14623 		case D_ALLOCINDIR:
14624 			newblk = WK_NEWBLK(wk);
14625 			if (newblk->nb_jnewblk) {
14626 				/* Journal allocate dependency. */
14627 				retval += 1;
14628 				if (!wantcount)
14629 					goto out;
14630 			}
14631 			continue;
14632 
14633 		case D_MKDIR:
14634 			mkdir = WK_MKDIR(wk);
14635 			if (mkdir->md_jaddref) {
14636 				/* Journal reference dependency. */
14637 				retval += 1;
14638 				if (!wantcount)
14639 					goto out;
14640 			}
14641 			continue;
14642 
14643 		case D_FREEWORK:
14644 		case D_FREEDEP:
14645 		case D_JSEGDEP:
14646 		case D_JSEG:
14647 		case D_SBDEP:
14648 			/* never a dependency on these blocks */
14649 			continue;
14650 
14651 		default:
14652 			panic("softdep_count_dependencies: Unexpected type %s",
14653 			    TYPENAME(wk->wk_type));
14654 			/* NOTREACHED */
14655 		}
14656 	}
14657 out:
14658 	FREE_LOCK(ump);
14659 	return (retval);
14660 }
14661 
14662 /*
14663  * Acquire exclusive access to a buffer.
14664  * Must be called with a locked mtx parameter.
14665  * Return acquired buffer or NULL on failure.
14666  */
14667 static struct buf *
14668 getdirtybuf(bp, lock, waitfor)
14669 	struct buf *bp;
14670 	struct rwlock *lock;
14671 	int waitfor;
14672 {
14673 	int error;
14674 
14675 	if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14676 		if (waitfor != MNT_WAIT)
14677 			return (NULL);
14678 		error = BUF_LOCK(bp,
14679 		    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14680 		/*
14681 		 * Even if we successfully acquire bp here, we have dropped
14682 		 * lock, which may violates our guarantee.
14683 		 */
14684 		if (error == 0)
14685 			BUF_UNLOCK(bp);
14686 		else if (error != ENOLCK)
14687 			panic("getdirtybuf: inconsistent lock: %d", error);
14688 		rw_wlock(lock);
14689 		return (NULL);
14690 	}
14691 	if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14692 		if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14693 			rw_wunlock(lock);
14694 			BO_LOCK(bp->b_bufobj);
14695 			BUF_UNLOCK(bp);
14696 			if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14697 				bp->b_vflags |= BV_BKGRDWAIT;
14698 				msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14699 				       PRIBIO | PDROP, "getbuf", 0);
14700 			} else
14701 				BO_UNLOCK(bp->b_bufobj);
14702 			rw_wlock(lock);
14703 			return (NULL);
14704 		}
14705 		BUF_UNLOCK(bp);
14706 		if (waitfor != MNT_WAIT)
14707 			return (NULL);
14708 #ifdef DEBUG_VFS_LOCKS
14709 		if (bp->b_vp->v_type != VCHR)
14710 			ASSERT_BO_WLOCKED(bp->b_bufobj);
14711 #endif
14712 		bp->b_vflags |= BV_BKGRDWAIT;
14713 		rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14714 		return (NULL);
14715 	}
14716 	if ((bp->b_flags & B_DELWRI) == 0) {
14717 		BUF_UNLOCK(bp);
14718 		return (NULL);
14719 	}
14720 	bremfree(bp);
14721 	return (bp);
14722 }
14723 
14724 /*
14725  * Check if it is safe to suspend the file system now.  On entry,
14726  * the vnode interlock for devvp should be held.  Return 0 with
14727  * the mount interlock held if the file system can be suspended now,
14728  * otherwise return EAGAIN with the mount interlock held.
14729  */
14730 int
14731 softdep_check_suspend(struct mount *mp,
14732 		      struct vnode *devvp,
14733 		      int softdep_depcnt,
14734 		      int softdep_accdepcnt,
14735 		      int secondary_writes,
14736 		      int secondary_accwrites)
14737 {
14738 	struct bufobj *bo;
14739 	struct ufsmount *ump;
14740 	struct inodedep *inodedep;
14741 	int error, unlinked;
14742 
14743 	bo = &devvp->v_bufobj;
14744 	ASSERT_BO_WLOCKED(bo);
14745 
14746 	/*
14747 	 * If we are not running with soft updates, then we need only
14748 	 * deal with secondary writes as we try to suspend.
14749 	 */
14750 	if (MOUNTEDSOFTDEP(mp) == 0) {
14751 		MNT_ILOCK(mp);
14752 		while (mp->mnt_secondary_writes != 0) {
14753 			BO_UNLOCK(bo);
14754 			msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14755 			    (PUSER - 1) | PDROP, "secwr", 0);
14756 			BO_LOCK(bo);
14757 			MNT_ILOCK(mp);
14758 		}
14759 
14760 		/*
14761 		 * Reasons for needing more work before suspend:
14762 		 * - Dirty buffers on devvp.
14763 		 * - Secondary writes occurred after start of vnode sync loop
14764 		 */
14765 		error = 0;
14766 		if (bo->bo_numoutput > 0 ||
14767 		    bo->bo_dirty.bv_cnt > 0 ||
14768 		    secondary_writes != 0 ||
14769 		    mp->mnt_secondary_writes != 0 ||
14770 		    secondary_accwrites != mp->mnt_secondary_accwrites)
14771 			error = EAGAIN;
14772 		BO_UNLOCK(bo);
14773 		return (error);
14774 	}
14775 
14776 	/*
14777 	 * If we are running with soft updates, then we need to coordinate
14778 	 * with them as we try to suspend.
14779 	 */
14780 	ump = VFSTOUFS(mp);
14781 	for (;;) {
14782 		if (!TRY_ACQUIRE_LOCK(ump)) {
14783 			BO_UNLOCK(bo);
14784 			ACQUIRE_LOCK(ump);
14785 			FREE_LOCK(ump);
14786 			BO_LOCK(bo);
14787 			continue;
14788 		}
14789 		MNT_ILOCK(mp);
14790 		if (mp->mnt_secondary_writes != 0) {
14791 			FREE_LOCK(ump);
14792 			BO_UNLOCK(bo);
14793 			msleep(&mp->mnt_secondary_writes,
14794 			       MNT_MTX(mp),
14795 			       (PUSER - 1) | PDROP, "secwr", 0);
14796 			BO_LOCK(bo);
14797 			continue;
14798 		}
14799 		break;
14800 	}
14801 
14802 	unlinked = 0;
14803 	if (MOUNTEDSUJ(mp)) {
14804 		for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14805 		    inodedep != NULL;
14806 		    inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14807 			if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14808 			    UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14809 			    UNLINKONLIST) ||
14810 			    !check_inodedep_free(inodedep))
14811 				continue;
14812 			unlinked++;
14813 		}
14814 	}
14815 
14816 	/*
14817 	 * Reasons for needing more work before suspend:
14818 	 * - Dirty buffers on devvp.
14819 	 * - Softdep activity occurred after start of vnode sync loop
14820 	 * - Secondary writes occurred after start of vnode sync loop
14821 	 */
14822 	error = 0;
14823 	if (bo->bo_numoutput > 0 ||
14824 	    bo->bo_dirty.bv_cnt > 0 ||
14825 	    softdep_depcnt != unlinked ||
14826 	    ump->softdep_deps != unlinked ||
14827 	    softdep_accdepcnt != ump->softdep_accdeps ||
14828 	    secondary_writes != 0 ||
14829 	    mp->mnt_secondary_writes != 0 ||
14830 	    secondary_accwrites != mp->mnt_secondary_accwrites)
14831 		error = EAGAIN;
14832 	FREE_LOCK(ump);
14833 	BO_UNLOCK(bo);
14834 	return (error);
14835 }
14836 
14837 /*
14838  * Get the number of dependency structures for the file system, both
14839  * the current number and the total number allocated.  These will
14840  * later be used to detect that softdep processing has occurred.
14841  */
14842 void
14843 softdep_get_depcounts(struct mount *mp,
14844 		      int *softdep_depsp,
14845 		      int *softdep_accdepsp)
14846 {
14847 	struct ufsmount *ump;
14848 
14849 	if (MOUNTEDSOFTDEP(mp) == 0) {
14850 		*softdep_depsp = 0;
14851 		*softdep_accdepsp = 0;
14852 		return;
14853 	}
14854 	ump = VFSTOUFS(mp);
14855 	ACQUIRE_LOCK(ump);
14856 	*softdep_depsp = ump->softdep_deps;
14857 	*softdep_accdepsp = ump->softdep_accdeps;
14858 	FREE_LOCK(ump);
14859 }
14860 
14861 /*
14862  * Wait for pending output on a vnode to complete.
14863  */
14864 static void
14865 drain_output(vp)
14866 	struct vnode *vp;
14867 {
14868 
14869 	ASSERT_VOP_LOCKED(vp, "drain_output");
14870 	(void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14871 }
14872 
14873 /*
14874  * Called whenever a buffer that is being invalidated or reallocated
14875  * contains dependencies. This should only happen if an I/O error has
14876  * occurred. The routine is called with the buffer locked.
14877  */
14878 static void
14879 softdep_deallocate_dependencies(bp)
14880 	struct buf *bp;
14881 {
14882 
14883 	if ((bp->b_ioflags & BIO_ERROR) == 0)
14884 		panic("softdep_deallocate_dependencies: dangling deps");
14885 	if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14886 		softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14887 	else
14888 		printf("softdep_deallocate_dependencies: "
14889 		    "got error %d while accessing filesystem\n", bp->b_error);
14890 	if (bp->b_error != ENXIO)
14891 		panic("softdep_deallocate_dependencies: unrecovered I/O error");
14892 }
14893 
14894 /*
14895  * Function to handle asynchronous write errors in the filesystem.
14896  */
14897 static void
14898 softdep_error(func, error)
14899 	char *func;
14900 	int error;
14901 {
14902 
14903 	/* XXX should do something better! */
14904 	printf("%s: got error %d while accessing filesystem\n", func, error);
14905 }
14906 
14907 #ifdef DDB
14908 
14909 /* exported to ffs_vfsops.c */
14910 extern void db_print_ffs(struct ufsmount *ump);
14911 void
14912 db_print_ffs(struct ufsmount *ump)
14913 {
14914 	db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14915 	    ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14916 	db_printf("    fs %p su_wl %d su_deps %d su_req %d\n",
14917 	    ump->um_fs, ump->softdep_on_worklist,
14918 	    ump->softdep_deps, ump->softdep_req);
14919 }
14920 
14921 static void
14922 worklist_print(struct worklist *wk, int verbose)
14923 {
14924 
14925 	if (!verbose) {
14926 		db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14927 		    (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
14928 		return;
14929 	}
14930 	db_printf("worklist: %p type %s state 0x%b next %p\n    ", wk,
14931 	    TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
14932 	    LIST_NEXT(wk, wk_list));
14933 	db_print_ffs(VFSTOUFS(wk->wk_mp));
14934 }
14935 
14936 static void
14937 inodedep_print(struct inodedep *inodedep, int verbose)
14938 {
14939 
14940 	worklist_print(&inodedep->id_list, 0);
14941 	db_printf("    fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14942 	    inodedep->id_fs,
14943 	    (intmax_t)inodedep->id_ino,
14944 	    (intmax_t)fsbtodb(inodedep->id_fs,
14945 	        ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14946 	    (intmax_t)inodedep->id_nlinkdelta,
14947 	    (intmax_t)inodedep->id_savednlink);
14948 
14949 	if (verbose == 0)
14950 		return;
14951 
14952 	db_printf("    bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14953 	    inodedep->id_bmsafemap,
14954 	    inodedep->id_mkdiradd,
14955 	    TAILQ_FIRST(&inodedep->id_inoreflst));
14956 	db_printf("    dirremhd %p, pendinghd %p, bufwait %p\n",
14957 	    LIST_FIRST(&inodedep->id_dirremhd),
14958 	    LIST_FIRST(&inodedep->id_pendinghd),
14959 	    LIST_FIRST(&inodedep->id_bufwait));
14960 	db_printf("    inowait %p, inoupdt %p, newinoupdt %p\n",
14961 	    LIST_FIRST(&inodedep->id_inowait),
14962 	    TAILQ_FIRST(&inodedep->id_inoupdt),
14963 	    TAILQ_FIRST(&inodedep->id_newinoupdt));
14964 	db_printf("    extupdt %p, newextupdt %p, freeblklst %p\n",
14965 	    TAILQ_FIRST(&inodedep->id_extupdt),
14966 	    TAILQ_FIRST(&inodedep->id_newextupdt),
14967 	    TAILQ_FIRST(&inodedep->id_freeblklst));
14968 	db_printf("    saveino %p, savedsize %jd, savedextsize %jd\n",
14969 	    inodedep->id_savedino1,
14970 	    (intmax_t)inodedep->id_savedsize,
14971 	    (intmax_t)inodedep->id_savedextsize);
14972 }
14973 
14974 static void
14975 newblk_print(struct newblk *nbp)
14976 {
14977 
14978 	worklist_print(&nbp->nb_list, 0);
14979 	db_printf("    newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14980 	db_printf("    jnewblk %p, bmsafemap %p, freefrag %p\n",
14981 	    &nbp->nb_jnewblk,
14982 	    &nbp->nb_bmsafemap,
14983 	    &nbp->nb_freefrag);
14984 	db_printf("    indirdeps %p, newdirblk %p, jwork %p\n",
14985 	    LIST_FIRST(&nbp->nb_indirdeps),
14986 	    LIST_FIRST(&nbp->nb_newdirblk),
14987 	    LIST_FIRST(&nbp->nb_jwork));
14988 }
14989 
14990 static void
14991 allocdirect_print(struct allocdirect *adp)
14992 {
14993 
14994 	newblk_print(&adp->ad_block);
14995 	db_printf("    oldblkno %jd, oldsize %ld, newsize %ld\n",
14996 	    adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14997 	db_printf("    offset %d, inodedep %p\n",
14998 	    adp->ad_offset, adp->ad_inodedep);
14999 }
15000 
15001 static void
15002 allocindir_print(struct allocindir *aip)
15003 {
15004 
15005 	newblk_print(&aip->ai_block);
15006 	db_printf("    oldblkno %jd, lbn %jd\n",
15007 	    (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
15008 	db_printf("    offset %d, indirdep %p\n",
15009 	    aip->ai_offset, aip->ai_indirdep);
15010 }
15011 
15012 static void
15013 mkdir_print(struct mkdir *mkdir)
15014 {
15015 
15016 	worklist_print(&mkdir->md_list, 0);
15017 	db_printf("    diradd %p, jaddref %p, buf %p\n",
15018 		mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
15019 }
15020 
15021 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
15022 {
15023 
15024 	if (have_addr == 0) {
15025 		db_printf("inodedep address required\n");
15026 		return;
15027 	}
15028 	inodedep_print((struct inodedep*)addr, 1);
15029 }
15030 
15031 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
15032 {
15033 	struct inodedep_hashhead *inodedephd;
15034 	struct inodedep *inodedep;
15035 	struct ufsmount *ump;
15036 	int cnt;
15037 
15038 	if (have_addr == 0) {
15039 		db_printf("ufsmount address required\n");
15040 		return;
15041 	}
15042 	ump = (struct ufsmount *)addr;
15043 	for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
15044 		inodedephd = &ump->inodedep_hashtbl[cnt];
15045 		LIST_FOREACH(inodedep, inodedephd, id_hash) {
15046 			inodedep_print(inodedep, 0);
15047 		}
15048 	}
15049 }
15050 
15051 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
15052 {
15053 
15054 	if (have_addr == 0) {
15055 		db_printf("worklist address required\n");
15056 		return;
15057 	}
15058 	worklist_print((struct worklist *)addr, 1);
15059 }
15060 
15061 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
15062 {
15063 	struct worklist *wk;
15064 	struct workhead *wkhd;
15065 
15066 	if (have_addr == 0) {
15067 		db_printf("worklist address required "
15068 		    "(for example value in bp->b_dep)\n");
15069 		return;
15070 	}
15071 	/*
15072 	 * We often do not have the address of the worklist head but
15073 	 * instead a pointer to its first entry (e.g., we have the
15074 	 * contents of bp->b_dep rather than &bp->b_dep). But the back
15075 	 * pointer of bp->b_dep will point at the head of the list, so
15076 	 * we cheat and use that instead. If we are in the middle of
15077 	 * a list we will still get the same result, so nothing
15078 	 * unexpected will result.
15079 	 */
15080 	wk = (struct worklist *)addr;
15081 	if (wk == NULL)
15082 		return;
15083 	wkhd = (struct workhead *)wk->wk_list.le_prev;
15084 	LIST_FOREACH(wk, wkhd, wk_list) {
15085 		switch(wk->wk_type) {
15086 		case D_INODEDEP:
15087 			inodedep_print(WK_INODEDEP(wk), 0);
15088 			continue;
15089 		case D_ALLOCDIRECT:
15090 			allocdirect_print(WK_ALLOCDIRECT(wk));
15091 			continue;
15092 		case D_ALLOCINDIR:
15093 			allocindir_print(WK_ALLOCINDIR(wk));
15094 			continue;
15095 		case D_MKDIR:
15096 			mkdir_print(WK_MKDIR(wk));
15097 			continue;
15098 		default:
15099 			worklist_print(wk, 0);
15100 			continue;
15101 		}
15102 	}
15103 }
15104 
15105 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
15106 {
15107 	if (have_addr == 0) {
15108 		db_printf("mkdir address required\n");
15109 		return;
15110 	}
15111 	mkdir_print((struct mkdir *)addr);
15112 }
15113 
15114 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
15115 {
15116 	struct mkdirlist *mkdirlisthd;
15117 	struct mkdir *mkdir;
15118 
15119 	if (have_addr == 0) {
15120 		db_printf("mkdir listhead address required\n");
15121 		return;
15122 	}
15123 	mkdirlisthd = (struct mkdirlist *)addr;
15124 	LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
15125 		mkdir_print(mkdir);
15126 		if (mkdir->md_diradd != NULL) {
15127 			db_printf("    ");
15128 			worklist_print(&mkdir->md_diradd->da_list, 0);
15129 		}
15130 		if (mkdir->md_jaddref != NULL) {
15131 			db_printf("    ");
15132 			worklist_print(&mkdir->md_jaddref->ja_list, 0);
15133 		}
15134 	}
15135 }
15136 
15137 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
15138 {
15139 	if (have_addr == 0) {
15140 		db_printf("allocdirect address required\n");
15141 		return;
15142 	}
15143 	allocdirect_print((struct allocdirect *)addr);
15144 }
15145 
15146 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
15147 {
15148 	if (have_addr == 0) {
15149 		db_printf("allocindir address required\n");
15150 		return;
15151 	}
15152 	allocindir_print((struct allocindir *)addr);
15153 }
15154 
15155 #endif /* DDB */
15156 
15157 #endif /* SOFTUPDATES */
15158