xref: /freebsd/sys/ufs/ffs/ffs_softdep.c (revision c4e127e24dc9f1322ebe7ade0991de7022010bf1)
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 
91 #include <ddb/ddb.h>
92 
93 #define	KTR_SUJ	0	/* Define to KTR_SPARE. */
94 
95 #ifndef SOFTUPDATES
96 
97 int
98 softdep_flushfiles(oldmnt, flags, td)
99 	struct mount *oldmnt;
100 	int flags;
101 	struct thread *td;
102 {
103 
104 	panic("softdep_flushfiles called");
105 }
106 
107 int
108 softdep_mount(devvp, mp, fs, cred)
109 	struct vnode *devvp;
110 	struct mount *mp;
111 	struct fs *fs;
112 	struct ucred *cred;
113 {
114 
115 	return (0);
116 }
117 
118 void
119 softdep_initialize()
120 {
121 
122 	return;
123 }
124 
125 void
126 softdep_uninitialize()
127 {
128 
129 	return;
130 }
131 
132 void
133 softdep_unmount(mp)
134 	struct mount *mp;
135 {
136 
137 	panic("softdep_unmount called");
138 }
139 
140 void
141 softdep_setup_sbupdate(ump, fs, bp)
142 	struct ufsmount *ump;
143 	struct fs *fs;
144 	struct buf *bp;
145 {
146 
147 	panic("softdep_setup_sbupdate called");
148 }
149 
150 void
151 softdep_setup_inomapdep(bp, ip, newinum, mode)
152 	struct buf *bp;
153 	struct inode *ip;
154 	ino_t newinum;
155 	int mode;
156 {
157 
158 	panic("softdep_setup_inomapdep called");
159 }
160 
161 void
162 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
163 	struct buf *bp;
164 	struct mount *mp;
165 	ufs2_daddr_t newblkno;
166 	int frags;
167 	int oldfrags;
168 {
169 
170 	panic("softdep_setup_blkmapdep called");
171 }
172 
173 void
174 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
175 	struct inode *ip;
176 	ufs_lbn_t lbn;
177 	ufs2_daddr_t newblkno;
178 	ufs2_daddr_t oldblkno;
179 	long newsize;
180 	long oldsize;
181 	struct buf *bp;
182 {
183 
184 	panic("softdep_setup_allocdirect called");
185 }
186 
187 void
188 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
189 	struct inode *ip;
190 	ufs_lbn_t lbn;
191 	ufs2_daddr_t newblkno;
192 	ufs2_daddr_t oldblkno;
193 	long newsize;
194 	long oldsize;
195 	struct buf *bp;
196 {
197 
198 	panic("softdep_setup_allocext called");
199 }
200 
201 void
202 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
203 	struct inode *ip;
204 	ufs_lbn_t lbn;
205 	struct buf *bp;
206 	int ptrno;
207 	ufs2_daddr_t newblkno;
208 	ufs2_daddr_t oldblkno;
209 	struct buf *nbp;
210 {
211 
212 	panic("softdep_setup_allocindir_page called");
213 }
214 
215 void
216 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
217 	struct buf *nbp;
218 	struct inode *ip;
219 	struct buf *bp;
220 	int ptrno;
221 	ufs2_daddr_t newblkno;
222 {
223 
224 	panic("softdep_setup_allocindir_meta called");
225 }
226 
227 void
228 softdep_journal_freeblocks(ip, cred, length, flags)
229 	struct inode *ip;
230 	struct ucred *cred;
231 	off_t length;
232 	int flags;
233 {
234 
235 	panic("softdep_journal_freeblocks called");
236 }
237 
238 void
239 softdep_journal_fsync(ip)
240 	struct inode *ip;
241 {
242 
243 	panic("softdep_journal_fsync called");
244 }
245 
246 void
247 softdep_setup_freeblocks(ip, length, flags)
248 	struct inode *ip;
249 	off_t length;
250 	int flags;
251 {
252 
253 	panic("softdep_setup_freeblocks called");
254 }
255 
256 void
257 softdep_freefile(pvp, ino, mode)
258 		struct vnode *pvp;
259 		ino_t ino;
260 		int mode;
261 {
262 
263 	panic("softdep_freefile called");
264 }
265 
266 int
267 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
268 	struct buf *bp;
269 	struct inode *dp;
270 	off_t diroffset;
271 	ino_t newinum;
272 	struct buf *newdirbp;
273 	int isnewblk;
274 {
275 
276 	panic("softdep_setup_directory_add called");
277 }
278 
279 void
280 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
281 	struct buf *bp;
282 	struct inode *dp;
283 	caddr_t base;
284 	caddr_t oldloc;
285 	caddr_t newloc;
286 	int entrysize;
287 {
288 
289 	panic("softdep_change_directoryentry_offset called");
290 }
291 
292 void
293 softdep_setup_remove(bp, dp, ip, isrmdir)
294 	struct buf *bp;
295 	struct inode *dp;
296 	struct inode *ip;
297 	int isrmdir;
298 {
299 
300 	panic("softdep_setup_remove called");
301 }
302 
303 void
304 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
305 	struct buf *bp;
306 	struct inode *dp;
307 	struct inode *ip;
308 	ino_t newinum;
309 	int isrmdir;
310 {
311 
312 	panic("softdep_setup_directory_change called");
313 }
314 
315 void
316 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
317 	struct mount *mp;
318 	struct buf *bp;
319 	ufs2_daddr_t blkno;
320 	int frags;
321 	struct workhead *wkhd;
322 {
323 
324 	panic("%s called", __FUNCTION__);
325 }
326 
327 void
328 softdep_setup_inofree(mp, bp, ino, wkhd)
329 	struct mount *mp;
330 	struct buf *bp;
331 	ino_t ino;
332 	struct workhead *wkhd;
333 {
334 
335 	panic("%s called", __FUNCTION__);
336 }
337 
338 void
339 softdep_setup_unlink(dp, ip)
340 	struct inode *dp;
341 	struct inode *ip;
342 {
343 
344 	panic("%s called", __FUNCTION__);
345 }
346 
347 void
348 softdep_setup_link(dp, ip)
349 	struct inode *dp;
350 	struct inode *ip;
351 {
352 
353 	panic("%s called", __FUNCTION__);
354 }
355 
356 void
357 softdep_revert_link(dp, ip)
358 	struct inode *dp;
359 	struct inode *ip;
360 {
361 
362 	panic("%s called", __FUNCTION__);
363 }
364 
365 void
366 softdep_setup_rmdir(dp, ip)
367 	struct inode *dp;
368 	struct inode *ip;
369 {
370 
371 	panic("%s called", __FUNCTION__);
372 }
373 
374 void
375 softdep_revert_rmdir(dp, ip)
376 	struct inode *dp;
377 	struct inode *ip;
378 {
379 
380 	panic("%s called", __FUNCTION__);
381 }
382 
383 void
384 softdep_setup_create(dp, ip)
385 	struct inode *dp;
386 	struct inode *ip;
387 {
388 
389 	panic("%s called", __FUNCTION__);
390 }
391 
392 void
393 softdep_revert_create(dp, ip)
394 	struct inode *dp;
395 	struct inode *ip;
396 {
397 
398 	panic("%s called", __FUNCTION__);
399 }
400 
401 void
402 softdep_setup_mkdir(dp, ip)
403 	struct inode *dp;
404 	struct inode *ip;
405 {
406 
407 	panic("%s called", __FUNCTION__);
408 }
409 
410 void
411 softdep_revert_mkdir(dp, ip)
412 	struct inode *dp;
413 	struct inode *ip;
414 {
415 
416 	panic("%s called", __FUNCTION__);
417 }
418 
419 void
420 softdep_setup_dotdot_link(dp, ip)
421 	struct inode *dp;
422 	struct inode *ip;
423 {
424 
425 	panic("%s called", __FUNCTION__);
426 }
427 
428 int
429 softdep_prealloc(vp, waitok)
430 	struct vnode *vp;
431 	int waitok;
432 {
433 
434 	panic("%s called", __FUNCTION__);
435 }
436 
437 int
438 softdep_journal_lookup(mp, vpp)
439 	struct mount *mp;
440 	struct vnode **vpp;
441 {
442 
443 	return (ENOENT);
444 }
445 
446 void
447 softdep_change_linkcnt(ip)
448 	struct inode *ip;
449 {
450 
451 	panic("softdep_change_linkcnt called");
452 }
453 
454 void
455 softdep_load_inodeblock(ip)
456 	struct inode *ip;
457 {
458 
459 	panic("softdep_load_inodeblock called");
460 }
461 
462 void
463 softdep_update_inodeblock(ip, bp, waitfor)
464 	struct inode *ip;
465 	struct buf *bp;
466 	int waitfor;
467 {
468 
469 	panic("softdep_update_inodeblock called");
470 }
471 
472 int
473 softdep_fsync(vp)
474 	struct vnode *vp;	/* the "in_core" copy of the inode */
475 {
476 
477 	return (0);
478 }
479 
480 void
481 softdep_fsync_mountdev(vp)
482 	struct vnode *vp;
483 {
484 
485 	return;
486 }
487 
488 int
489 softdep_flushworklist(oldmnt, countp, td)
490 	struct mount *oldmnt;
491 	int *countp;
492 	struct thread *td;
493 {
494 
495 	*countp = 0;
496 	return (0);
497 }
498 
499 int
500 softdep_sync_metadata(struct vnode *vp)
501 {
502 
503 	panic("softdep_sync_metadata called");
504 }
505 
506 int
507 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
508 {
509 
510 	panic("softdep_sync_buf called");
511 }
512 
513 int
514 softdep_slowdown(vp)
515 	struct vnode *vp;
516 {
517 
518 	panic("softdep_slowdown called");
519 }
520 
521 int
522 softdep_request_cleanup(fs, vp, cred, resource)
523 	struct fs *fs;
524 	struct vnode *vp;
525 	struct ucred *cred;
526 	int resource;
527 {
528 
529 	return (0);
530 }
531 
532 int
533 softdep_check_suspend(struct mount *mp,
534 		      struct vnode *devvp,
535 		      int softdep_depcnt,
536 		      int softdep_accdepcnt,
537 		      int secondary_writes,
538 		      int secondary_accwrites)
539 {
540 	struct bufobj *bo;
541 	int error;
542 
543 	(void) softdep_depcnt,
544 	(void) softdep_accdepcnt;
545 
546 	bo = &devvp->v_bufobj;
547 	ASSERT_BO_WLOCKED(bo);
548 
549 	MNT_ILOCK(mp);
550 	while (mp->mnt_secondary_writes != 0) {
551 		BO_UNLOCK(bo);
552 		msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
553 		    (PUSER - 1) | PDROP, "secwr", 0);
554 		BO_LOCK(bo);
555 		MNT_ILOCK(mp);
556 	}
557 
558 	/*
559 	 * Reasons for needing more work before suspend:
560 	 * - Dirty buffers on devvp.
561 	 * - Secondary writes occurred after start of vnode sync loop
562 	 */
563 	error = 0;
564 	if (bo->bo_numoutput > 0 ||
565 	    bo->bo_dirty.bv_cnt > 0 ||
566 	    secondary_writes != 0 ||
567 	    mp->mnt_secondary_writes != 0 ||
568 	    secondary_accwrites != mp->mnt_secondary_accwrites)
569 		error = EAGAIN;
570 	BO_UNLOCK(bo);
571 	return (error);
572 }
573 
574 void
575 softdep_get_depcounts(struct mount *mp,
576 		      int *softdepactivep,
577 		      int *softdepactiveaccp)
578 {
579 	(void) mp;
580 	*softdepactivep = 0;
581 	*softdepactiveaccp = 0;
582 }
583 
584 void
585 softdep_buf_append(bp, wkhd)
586 	struct buf *bp;
587 	struct workhead *wkhd;
588 {
589 
590 	panic("softdep_buf_appendwork called");
591 }
592 
593 void
594 softdep_inode_append(ip, cred, wkhd)
595 	struct inode *ip;
596 	struct ucred *cred;
597 	struct workhead *wkhd;
598 {
599 
600 	panic("softdep_inode_appendwork called");
601 }
602 
603 void
604 softdep_freework(wkhd)
605 	struct workhead *wkhd;
606 {
607 
608 	panic("softdep_freework called");
609 }
610 
611 #else
612 
613 FEATURE(softupdates, "FFS soft-updates support");
614 
615 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0,
616     "soft updates stats");
617 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0,
618     "total dependencies allocated");
619 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0,
620     "high use dependencies allocated");
621 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0,
622     "current dependencies allocated");
623 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0,
624     "current dependencies written");
625 
626 unsigned long dep_current[D_LAST + 1];
627 unsigned long dep_highuse[D_LAST + 1];
628 unsigned long dep_total[D_LAST + 1];
629 unsigned long dep_write[D_LAST + 1];
630 
631 #define	SOFTDEP_TYPE(type, str, long)					\
632     static MALLOC_DEFINE(M_ ## type, #str, long);			\
633     SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD,	\
634 	&dep_total[D_ ## type], 0, "");					\
635     SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, 	\
636 	&dep_current[D_ ## type], 0, "");				\
637     SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, 	\
638 	&dep_highuse[D_ ## type], 0, "");				\
639     SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, 	\
640 	&dep_write[D_ ## type], 0, "");
641 
642 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
643 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
644 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
645     "Block or frag allocated from cyl group map");
646 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
647 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
648 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
649 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
650 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
651 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
652 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
653 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
654 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
655 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
656 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
657 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
658 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
659 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
660 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
661 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
662 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
663 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
664 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
665 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
666 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
667 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
668 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
669 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
670 
671 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
672 
673 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
674 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
675 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
676 
677 #define M_SOFTDEP_FLAGS	(M_WAITOK)
678 
679 /*
680  * translate from workitem type to memory type
681  * MUST match the defines above, such that memtype[D_XXX] == M_XXX
682  */
683 static struct malloc_type *memtype[] = {
684 	NULL,
685 	M_PAGEDEP,
686 	M_INODEDEP,
687 	M_BMSAFEMAP,
688 	M_NEWBLK,
689 	M_ALLOCDIRECT,
690 	M_INDIRDEP,
691 	M_ALLOCINDIR,
692 	M_FREEFRAG,
693 	M_FREEBLKS,
694 	M_FREEFILE,
695 	M_DIRADD,
696 	M_MKDIR,
697 	M_DIRREM,
698 	M_NEWDIRBLK,
699 	M_FREEWORK,
700 	M_FREEDEP,
701 	M_JADDREF,
702 	M_JREMREF,
703 	M_JMVREF,
704 	M_JNEWBLK,
705 	M_JFREEBLK,
706 	M_JFREEFRAG,
707 	M_JSEG,
708 	M_JSEGDEP,
709 	M_SBDEP,
710 	M_JTRUNC,
711 	M_JFSYNC,
712 	M_SENTINEL
713 };
714 
715 #define DtoM(type) (memtype[type])
716 
717 /*
718  * Names of malloc types.
719  */
720 #define TYPENAME(type)  \
721 	((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
722 	memtype[type]->ks_shortdesc : "???")
723 /*
724  * End system adaptation definitions.
725  */
726 
727 #define	DOTDOT_OFFSET	offsetof(struct dirtemplate, dotdot_ino)
728 #define	DOT_OFFSET	offsetof(struct dirtemplate, dot_ino)
729 
730 /*
731  * Internal function prototypes.
732  */
733 static	void check_clear_deps(struct mount *);
734 static	void softdep_error(char *, int);
735 static	int softdep_process_worklist(struct mount *, int);
736 static	int softdep_waitidle(struct mount *, int);
737 static	void drain_output(struct vnode *);
738 static	struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
739 static	int check_inodedep_free(struct inodedep *);
740 static	void clear_remove(struct mount *);
741 static	void clear_inodedeps(struct mount *);
742 static	void unlinked_inodedep(struct mount *, struct inodedep *);
743 static	void clear_unlinked_inodedep(struct inodedep *);
744 static	struct inodedep *first_unlinked_inodedep(struct ufsmount *);
745 static	int flush_pagedep_deps(struct vnode *, struct mount *,
746 	    struct diraddhd *);
747 static	int free_pagedep(struct pagedep *);
748 static	int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
749 static	int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
750 static	int flush_deplist(struct allocdirectlst *, int, int *);
751 static	int sync_cgs(struct mount *, int);
752 static	int handle_written_filepage(struct pagedep *, struct buf *, int);
753 static	int handle_written_sbdep(struct sbdep *, struct buf *);
754 static	void initiate_write_sbdep(struct sbdep *);
755 static	void diradd_inode_written(struct diradd *, struct inodedep *);
756 static	int handle_written_indirdep(struct indirdep *, struct buf *,
757 	    struct buf**, int);
758 static	int handle_written_inodeblock(struct inodedep *, struct buf *, int);
759 static	int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
760 	    uint8_t *);
761 static	int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
762 static	void handle_written_jaddref(struct jaddref *);
763 static	void handle_written_jremref(struct jremref *);
764 static	void handle_written_jseg(struct jseg *, struct buf *);
765 static	void handle_written_jnewblk(struct jnewblk *);
766 static	void handle_written_jblkdep(struct jblkdep *);
767 static	void handle_written_jfreefrag(struct jfreefrag *);
768 static	void complete_jseg(struct jseg *);
769 static	void complete_jsegs(struct jseg *);
770 static	void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
771 static	void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
772 static	void jremref_write(struct jremref *, struct jseg *, uint8_t *);
773 static	void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
774 static	void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
775 static	void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
776 static	void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
777 static	void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
778 static	void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
779 static	inline void inoref_write(struct inoref *, struct jseg *,
780 	    struct jrefrec *);
781 static	void handle_allocdirect_partdone(struct allocdirect *,
782 	    struct workhead *);
783 static	struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
784 	    struct workhead *);
785 static	void indirdep_complete(struct indirdep *);
786 static	int indirblk_lookup(struct mount *, ufs2_daddr_t);
787 static	void indirblk_insert(struct freework *);
788 static	void indirblk_remove(struct freework *);
789 static	void handle_allocindir_partdone(struct allocindir *);
790 static	void initiate_write_filepage(struct pagedep *, struct buf *);
791 static	void initiate_write_indirdep(struct indirdep*, struct buf *);
792 static	void handle_written_mkdir(struct mkdir *, int);
793 static	int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
794 	    uint8_t *);
795 static	void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
796 static	void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
797 static	void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
798 static	void handle_workitem_freefile(struct freefile *);
799 static	int handle_workitem_remove(struct dirrem *, int);
800 static	struct dirrem *newdirrem(struct buf *, struct inode *,
801 	    struct inode *, int, struct dirrem **);
802 static	struct indirdep *indirdep_lookup(struct mount *, struct inode *,
803 	    struct buf *);
804 static	void cancel_indirdep(struct indirdep *, struct buf *,
805 	    struct freeblks *);
806 static	void free_indirdep(struct indirdep *);
807 static	void free_diradd(struct diradd *, struct workhead *);
808 static	void merge_diradd(struct inodedep *, struct diradd *);
809 static	void complete_diradd(struct diradd *);
810 static	struct diradd *diradd_lookup(struct pagedep *, int);
811 static	struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
812 	    struct jremref *);
813 static	struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
814 	    struct jremref *);
815 static	void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
816 	    struct jremref *, struct jremref *);
817 static	void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
818 	    struct jremref *);
819 static	void cancel_allocindir(struct allocindir *, struct buf *bp,
820 	    struct freeblks *, int);
821 static	int setup_trunc_indir(struct freeblks *, struct inode *,
822 	    ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
823 static	void complete_trunc_indir(struct freework *);
824 static	void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
825 	    int);
826 static	void complete_mkdir(struct mkdir *);
827 static	void free_newdirblk(struct newdirblk *);
828 static	void free_jremref(struct jremref *);
829 static	void free_jaddref(struct jaddref *);
830 static	void free_jsegdep(struct jsegdep *);
831 static	void free_jsegs(struct jblocks *);
832 static	void rele_jseg(struct jseg *);
833 static	void free_jseg(struct jseg *, struct jblocks *);
834 static	void free_jnewblk(struct jnewblk *);
835 static	void free_jblkdep(struct jblkdep *);
836 static	void free_jfreefrag(struct jfreefrag *);
837 static	void free_freedep(struct freedep *);
838 static	void journal_jremref(struct dirrem *, struct jremref *,
839 	    struct inodedep *);
840 static	void cancel_jnewblk(struct jnewblk *, struct workhead *);
841 static	int cancel_jaddref(struct jaddref *, struct inodedep *,
842 	    struct workhead *);
843 static	void cancel_jfreefrag(struct jfreefrag *);
844 static	inline void setup_freedirect(struct freeblks *, struct inode *,
845 	    int, int);
846 static	inline void setup_freeext(struct freeblks *, struct inode *, int, int);
847 static	inline void setup_freeindir(struct freeblks *, struct inode *, int,
848 	    ufs_lbn_t, int);
849 static	inline struct freeblks *newfreeblks(struct mount *, struct inode *);
850 static	void freeblks_free(struct ufsmount *, struct freeblks *, int);
851 static	void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
852 static	ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
853 static	int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
854 static	void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
855 	    int, int);
856 static	void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
857 static 	int cancel_pagedep(struct pagedep *, struct freeblks *, int);
858 static	int deallocate_dependencies(struct buf *, struct freeblks *, int);
859 static	void newblk_freefrag(struct newblk*);
860 static	void free_newblk(struct newblk *);
861 static	void cancel_allocdirect(struct allocdirectlst *,
862 	    struct allocdirect *, struct freeblks *);
863 static	int check_inode_unwritten(struct inodedep *);
864 static	int free_inodedep(struct inodedep *);
865 static	void freework_freeblock(struct freework *, u_long);
866 static	void freework_enqueue(struct freework *);
867 static	int handle_workitem_freeblocks(struct freeblks *, int);
868 static	int handle_complete_freeblocks(struct freeblks *, int);
869 static	void handle_workitem_indirblk(struct freework *);
870 static	void handle_written_freework(struct freework *);
871 static	void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
872 static	struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
873 	    struct workhead *);
874 static	struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
875 	    struct inodedep *, struct allocindir *, ufs_lbn_t);
876 static	struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
877 	    ufs2_daddr_t, ufs_lbn_t);
878 static	void handle_workitem_freefrag(struct freefrag *);
879 static	struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
880 	    ufs_lbn_t, u_long);
881 static	void allocdirect_merge(struct allocdirectlst *,
882 	    struct allocdirect *, struct allocdirect *);
883 static	struct freefrag *allocindir_merge(struct allocindir *,
884 	    struct allocindir *);
885 static	int bmsafemap_find(struct bmsafemap_hashhead *, int,
886 	    struct bmsafemap **);
887 static	struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
888 	    int cg, struct bmsafemap *);
889 static	int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
890 	    struct newblk **);
891 static	int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
892 static	int inodedep_find(struct inodedep_hashhead *, ino_t,
893 	    struct inodedep **);
894 static	int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
895 static	int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
896 	    int, struct pagedep **);
897 static	int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
898 	    struct pagedep **);
899 static	void pause_timer(void *);
900 static	int request_cleanup(struct mount *, int);
901 static	int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
902 static	void schedule_cleanup(struct mount *);
903 static void softdep_ast_cleanup_proc(struct thread *);
904 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
905 static	int process_worklist_item(struct mount *, int, int);
906 static	void process_removes(struct vnode *);
907 static	void process_truncates(struct vnode *);
908 static	void jwork_move(struct workhead *, struct workhead *);
909 static	void jwork_insert(struct workhead *, struct jsegdep *);
910 static	void add_to_worklist(struct worklist *, int);
911 static	void wake_worklist(struct worklist *);
912 static	void wait_worklist(struct worklist *, char *);
913 static	void remove_from_worklist(struct worklist *);
914 static	void softdep_flush(void *);
915 static	void softdep_flushjournal(struct mount *);
916 static	int softdep_speedup(struct ufsmount *);
917 static	void worklist_speedup(struct mount *);
918 static	int journal_mount(struct mount *, struct fs *, struct ucred *);
919 static	void journal_unmount(struct ufsmount *);
920 static	int journal_space(struct ufsmount *, int);
921 static	void journal_suspend(struct ufsmount *);
922 static	int journal_unsuspend(struct ufsmount *ump);
923 static	void softdep_prelink(struct vnode *, struct vnode *);
924 static	void add_to_journal(struct worklist *);
925 static	void remove_from_journal(struct worklist *);
926 static	bool softdep_excess_items(struct ufsmount *, int);
927 static	void softdep_process_journal(struct mount *, struct worklist *, int);
928 static	struct jremref *newjremref(struct dirrem *, struct inode *,
929 	    struct inode *ip, off_t, nlink_t);
930 static	struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
931 	    uint16_t);
932 static	inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
933 	    uint16_t);
934 static	inline struct jsegdep *inoref_jseg(struct inoref *);
935 static	struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
936 static	struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
937 	    ufs2_daddr_t, int);
938 static	void adjust_newfreework(struct freeblks *, int);
939 static	struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
940 static	void move_newblock_dep(struct jaddref *, struct inodedep *);
941 static	void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
942 static	struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
943 	    ufs2_daddr_t, long, ufs_lbn_t);
944 static	struct freework *newfreework(struct ufsmount *, struct freeblks *,
945 	    struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
946 static	int jwait(struct worklist *, int);
947 static	struct inodedep *inodedep_lookup_ip(struct inode *);
948 static	int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
949 static	struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
950 static	void handle_jwork(struct workhead *);
951 static	struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
952 	    struct mkdir **);
953 static	struct jblocks *jblocks_create(void);
954 static	ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
955 static	void jblocks_free(struct jblocks *, struct mount *, int);
956 static	void jblocks_destroy(struct jblocks *);
957 static	void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
958 
959 /*
960  * Exported softdep operations.
961  */
962 static	void softdep_disk_io_initiation(struct buf *);
963 static	void softdep_disk_write_complete(struct buf *);
964 static	void softdep_deallocate_dependencies(struct buf *);
965 static	int softdep_count_dependencies(struct buf *bp, int);
966 
967 /*
968  * Global lock over all of soft updates.
969  */
970 static struct mtx lk;
971 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF);
972 
973 #define ACQUIRE_GBLLOCK(lk)	mtx_lock(lk)
974 #define FREE_GBLLOCK(lk)	mtx_unlock(lk)
975 #define GBLLOCK_OWNED(lk)	mtx_assert((lk), MA_OWNED)
976 
977 /*
978  * Per-filesystem soft-updates locking.
979  */
980 #define LOCK_PTR(ump)		(&(ump)->um_softdep->sd_fslock)
981 #define TRY_ACQUIRE_LOCK(ump)	rw_try_wlock(&(ump)->um_softdep->sd_fslock)
982 #define ACQUIRE_LOCK(ump)	rw_wlock(&(ump)->um_softdep->sd_fslock)
983 #define FREE_LOCK(ump)		rw_wunlock(&(ump)->um_softdep->sd_fslock)
984 #define LOCK_OWNED(ump)		rw_assert(&(ump)->um_softdep->sd_fslock, \
985 				    RA_WLOCKED)
986 
987 #define	BUF_AREC(bp)		lockallowrecurse(&(bp)->b_lock)
988 #define	BUF_NOREC(bp)		lockdisablerecurse(&(bp)->b_lock)
989 
990 /*
991  * Worklist queue management.
992  * These routines require that the lock be held.
993  */
994 #ifndef /* NOT */ INVARIANTS
995 #define WORKLIST_INSERT(head, item) do {	\
996 	(item)->wk_state |= ONWORKLIST;		\
997 	LIST_INSERT_HEAD(head, item, wk_list);	\
998 } while (0)
999 #define WORKLIST_REMOVE(item) do {		\
1000 	(item)->wk_state &= ~ONWORKLIST;	\
1001 	LIST_REMOVE(item, wk_list);		\
1002 } while (0)
1003 #define WORKLIST_INSERT_UNLOCKED	WORKLIST_INSERT
1004 #define WORKLIST_REMOVE_UNLOCKED	WORKLIST_REMOVE
1005 
1006 #else /* INVARIANTS */
1007 static	void worklist_insert(struct workhead *, struct worklist *, int,
1008 	const char *, int);
1009 static	void worklist_remove(struct worklist *, int, const char *, int);
1010 
1011 #define WORKLIST_INSERT(head, item) \
1012 	worklist_insert(head, item, 1, __func__, __LINE__)
1013 #define WORKLIST_INSERT_UNLOCKED(head, item)\
1014 	worklist_insert(head, item, 0, __func__, __LINE__)
1015 #define WORKLIST_REMOVE(item)\
1016 	worklist_remove(item, 1, __func__, __LINE__)
1017 #define WORKLIST_REMOVE_UNLOCKED(item)\
1018 	worklist_remove(item, 0, __func__, __LINE__)
1019 
1020 static void
1021 worklist_insert(head, item, locked, func, line)
1022 	struct workhead *head;
1023 	struct worklist *item;
1024 	int locked;
1025 	const char *func;
1026 	int line;
1027 {
1028 
1029 	if (locked)
1030 		LOCK_OWNED(VFSTOUFS(item->wk_mp));
1031 	if (item->wk_state & ONWORKLIST)
1032 		panic("worklist_insert: %p %s(0x%X) already on list, "
1033 		    "added in function %s at line %d",
1034 		    item, TYPENAME(item->wk_type), item->wk_state,
1035 		    item->wk_func, item->wk_line);
1036 	item->wk_state |= ONWORKLIST;
1037 	item->wk_func = func;
1038 	item->wk_line = line;
1039 	LIST_INSERT_HEAD(head, item, wk_list);
1040 }
1041 
1042 static void
1043 worklist_remove(item, locked, func, line)
1044 	struct worklist *item;
1045 	int locked;
1046 	const char *func;
1047 	int line;
1048 {
1049 
1050 	if (locked)
1051 		LOCK_OWNED(VFSTOUFS(item->wk_mp));
1052 	if ((item->wk_state & ONWORKLIST) == 0)
1053 		panic("worklist_remove: %p %s(0x%X) not on list, "
1054 		    "removed in function %s at line %d",
1055 		    item, TYPENAME(item->wk_type), item->wk_state,
1056 		    item->wk_func, item->wk_line);
1057 	item->wk_state &= ~ONWORKLIST;
1058 	item->wk_func = func;
1059 	item->wk_line = line;
1060 	LIST_REMOVE(item, wk_list);
1061 }
1062 #endif /* INVARIANTS */
1063 
1064 /*
1065  * Merge two jsegdeps keeping only the oldest one as newer references
1066  * can't be discarded until after older references.
1067  */
1068 static inline struct jsegdep *
1069 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1070 {
1071 	struct jsegdep *swp;
1072 
1073 	if (two == NULL)
1074 		return (one);
1075 
1076 	if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1077 		swp = one;
1078 		one = two;
1079 		two = swp;
1080 	}
1081 	WORKLIST_REMOVE(&two->jd_list);
1082 	free_jsegdep(two);
1083 
1084 	return (one);
1085 }
1086 
1087 /*
1088  * If two freedeps are compatible free one to reduce list size.
1089  */
1090 static inline struct freedep *
1091 freedep_merge(struct freedep *one, struct freedep *two)
1092 {
1093 	if (two == NULL)
1094 		return (one);
1095 
1096 	if (one->fd_freework == two->fd_freework) {
1097 		WORKLIST_REMOVE(&two->fd_list);
1098 		free_freedep(two);
1099 	}
1100 	return (one);
1101 }
1102 
1103 /*
1104  * Move journal work from one list to another.  Duplicate freedeps and
1105  * jsegdeps are coalesced to keep the lists as small as possible.
1106  */
1107 static void
1108 jwork_move(dst, src)
1109 	struct workhead *dst;
1110 	struct workhead *src;
1111 {
1112 	struct freedep *freedep;
1113 	struct jsegdep *jsegdep;
1114 	struct worklist *wkn;
1115 	struct worklist *wk;
1116 
1117 	KASSERT(dst != src,
1118 	    ("jwork_move: dst == src"));
1119 	freedep = NULL;
1120 	jsegdep = NULL;
1121 	LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1122 		if (wk->wk_type == D_JSEGDEP)
1123 			jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1124 		else if (wk->wk_type == D_FREEDEP)
1125 			freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1126 	}
1127 
1128 	while ((wk = LIST_FIRST(src)) != NULL) {
1129 		WORKLIST_REMOVE(wk);
1130 		WORKLIST_INSERT(dst, wk);
1131 		if (wk->wk_type == D_JSEGDEP) {
1132 			jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1133 			continue;
1134 		}
1135 		if (wk->wk_type == D_FREEDEP)
1136 			freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1137 	}
1138 }
1139 
1140 static void
1141 jwork_insert(dst, jsegdep)
1142 	struct workhead *dst;
1143 	struct jsegdep *jsegdep;
1144 {
1145 	struct jsegdep *jsegdepn;
1146 	struct worklist *wk;
1147 
1148 	LIST_FOREACH(wk, dst, wk_list)
1149 		if (wk->wk_type == D_JSEGDEP)
1150 			break;
1151 	if (wk == NULL) {
1152 		WORKLIST_INSERT(dst, &jsegdep->jd_list);
1153 		return;
1154 	}
1155 	jsegdepn = WK_JSEGDEP(wk);
1156 	if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1157 		WORKLIST_REMOVE(wk);
1158 		free_jsegdep(jsegdepn);
1159 		WORKLIST_INSERT(dst, &jsegdep->jd_list);
1160 	} else
1161 		free_jsegdep(jsegdep);
1162 }
1163 
1164 /*
1165  * Routines for tracking and managing workitems.
1166  */
1167 static	void workitem_free(struct worklist *, int);
1168 static	void workitem_alloc(struct worklist *, int, struct mount *);
1169 static	void workitem_reassign(struct worklist *, int);
1170 
1171 #define	WORKITEM_FREE(item, type) \
1172 	workitem_free((struct worklist *)(item), (type))
1173 #define	WORKITEM_REASSIGN(item, type) \
1174 	workitem_reassign((struct worklist *)(item), (type))
1175 
1176 static void
1177 workitem_free(item, type)
1178 	struct worklist *item;
1179 	int type;
1180 {
1181 	struct ufsmount *ump;
1182 
1183 #ifdef INVARIANTS
1184 	if (item->wk_state & ONWORKLIST)
1185 		panic("workitem_free: %s(0x%X) still on list, "
1186 		    "added in function %s at line %d",
1187 		    TYPENAME(item->wk_type), item->wk_state,
1188 		    item->wk_func, item->wk_line);
1189 	if (item->wk_type != type && type != D_NEWBLK)
1190 		panic("workitem_free: type mismatch %s != %s",
1191 		    TYPENAME(item->wk_type), TYPENAME(type));
1192 #endif
1193 	if (item->wk_state & IOWAITING)
1194 		wakeup(item);
1195 	ump = VFSTOUFS(item->wk_mp);
1196 	LOCK_OWNED(ump);
1197 	KASSERT(ump->softdep_deps > 0,
1198 	    ("workitem_free: %s: softdep_deps going negative",
1199 	    ump->um_fs->fs_fsmnt));
1200 	if (--ump->softdep_deps == 0 && ump->softdep_req)
1201 		wakeup(&ump->softdep_deps);
1202 	KASSERT(dep_current[item->wk_type] > 0,
1203 	    ("workitem_free: %s: dep_current[%s] going negative",
1204 	    ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1205 	KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1206 	    ("workitem_free: %s: softdep_curdeps[%s] going negative",
1207 	    ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1208 	atomic_subtract_long(&dep_current[item->wk_type], 1);
1209 	ump->softdep_curdeps[item->wk_type] -= 1;
1210 	free(item, DtoM(type));
1211 }
1212 
1213 static void
1214 workitem_alloc(item, type, mp)
1215 	struct worklist *item;
1216 	int type;
1217 	struct mount *mp;
1218 {
1219 	struct ufsmount *ump;
1220 
1221 	item->wk_type = type;
1222 	item->wk_mp = mp;
1223 	item->wk_state = 0;
1224 
1225 	ump = VFSTOUFS(mp);
1226 	ACQUIRE_GBLLOCK(&lk);
1227 	dep_current[type]++;
1228 	if (dep_current[type] > dep_highuse[type])
1229 		dep_highuse[type] = dep_current[type];
1230 	dep_total[type]++;
1231 	FREE_GBLLOCK(&lk);
1232 	ACQUIRE_LOCK(ump);
1233 	ump->softdep_curdeps[type] += 1;
1234 	ump->softdep_deps++;
1235 	ump->softdep_accdeps++;
1236 	FREE_LOCK(ump);
1237 }
1238 
1239 static void
1240 workitem_reassign(item, newtype)
1241 	struct worklist *item;
1242 	int newtype;
1243 {
1244 	struct ufsmount *ump;
1245 
1246 	ump = VFSTOUFS(item->wk_mp);
1247 	LOCK_OWNED(ump);
1248 	KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1249 	    ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1250 	    VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1251 	ump->softdep_curdeps[item->wk_type] -= 1;
1252 	ump->softdep_curdeps[newtype] += 1;
1253 	KASSERT(dep_current[item->wk_type] > 0,
1254 	    ("workitem_reassign: %s: dep_current[%s] going negative",
1255 	    VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1256 	ACQUIRE_GBLLOCK(&lk);
1257 	dep_current[newtype]++;
1258 	dep_current[item->wk_type]--;
1259 	if (dep_current[newtype] > dep_highuse[newtype])
1260 		dep_highuse[newtype] = dep_current[newtype];
1261 	dep_total[newtype]++;
1262 	FREE_GBLLOCK(&lk);
1263 	item->wk_type = newtype;
1264 }
1265 
1266 /*
1267  * Workitem queue management
1268  */
1269 static int max_softdeps;	/* maximum number of structs before slowdown */
1270 static int tickdelay = 2;	/* number of ticks to pause during slowdown */
1271 static int proc_waiting;	/* tracks whether we have a timeout posted */
1272 static int *stat_countp;	/* statistic to count in proc_waiting timeout */
1273 static struct callout softdep_callout;
1274 static int req_clear_inodedeps;	/* syncer process flush some inodedeps */
1275 static int req_clear_remove;	/* syncer process flush some freeblks */
1276 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1277 
1278 /*
1279  * runtime statistics
1280  */
1281 static int stat_flush_threads;	/* number of softdep flushing threads */
1282 static int stat_worklist_push;	/* number of worklist cleanups */
1283 static int stat_blk_limit_push;	/* number of times block limit neared */
1284 static int stat_ino_limit_push;	/* number of times inode limit neared */
1285 static int stat_blk_limit_hit;	/* number of times block slowdown imposed */
1286 static int stat_ino_limit_hit;	/* number of times inode slowdown imposed */
1287 static int stat_sync_limit_hit;	/* number of synchronous slowdowns imposed */
1288 static int stat_indir_blk_ptrs;	/* bufs redirtied as indir ptrs not written */
1289 static int stat_inode_bitmap;	/* bufs redirtied as inode bitmap not written */
1290 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1291 static int stat_dir_entry;	/* bufs redirtied as dir entry cannot write */
1292 static int stat_jaddref;	/* bufs redirtied as ino bitmap can not write */
1293 static int stat_jnewblk;	/* bufs redirtied as blk bitmap can not write */
1294 static int stat_journal_min;	/* Times hit journal min threshold */
1295 static int stat_journal_low;	/* Times hit journal low threshold */
1296 static int stat_journal_wait;	/* Times blocked in jwait(). */
1297 static int stat_jwait_filepage;	/* Times blocked in jwait() for filepage. */
1298 static int stat_jwait_freeblks;	/* Times blocked in jwait() for freeblks. */
1299 static int stat_jwait_inode;	/* Times blocked in jwait() for inodes. */
1300 static int stat_jwait_newblk;	/* Times blocked in jwait() for newblks. */
1301 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1302 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1303 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1304 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1305 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1306 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1307 
1308 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1309     &max_softdeps, 0, "");
1310 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1311     &tickdelay, 0, "");
1312 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1313     &stat_flush_threads, 0, "");
1314 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW,
1315     &stat_worklist_push, 0,"");
1316 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW,
1317     &stat_blk_limit_push, 0,"");
1318 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW,
1319     &stat_ino_limit_push, 0,"");
1320 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW,
1321     &stat_blk_limit_hit, 0, "");
1322 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW,
1323     &stat_ino_limit_hit, 0, "");
1324 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW,
1325     &stat_sync_limit_hit, 0, "");
1326 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW,
1327     &stat_indir_blk_ptrs, 0, "");
1328 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW,
1329     &stat_inode_bitmap, 0, "");
1330 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW,
1331     &stat_direct_blk_ptrs, 0, "");
1332 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW,
1333     &stat_dir_entry, 0, "");
1334 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW,
1335     &stat_jaddref, 0, "");
1336 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW,
1337     &stat_jnewblk, 0, "");
1338 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW,
1339     &stat_journal_low, 0, "");
1340 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW,
1341     &stat_journal_min, 0, "");
1342 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW,
1343     &stat_journal_wait, 0, "");
1344 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW,
1345     &stat_jwait_filepage, 0, "");
1346 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW,
1347     &stat_jwait_freeblks, 0, "");
1348 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW,
1349     &stat_jwait_inode, 0, "");
1350 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW,
1351     &stat_jwait_newblk, 0, "");
1352 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW,
1353     &stat_cleanup_blkrequests, 0, "");
1354 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW,
1355     &stat_cleanup_inorequests, 0, "");
1356 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW,
1357     &stat_cleanup_high_delay, 0, "");
1358 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW,
1359     &stat_cleanup_retries, 0, "");
1360 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW,
1361     &stat_cleanup_failures, 0, "");
1362 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1363     &softdep_flushcache, 0, "");
1364 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1365     &stat_emptyjblocks, 0, "");
1366 
1367 SYSCTL_DECL(_vfs_ffs);
1368 
1369 /* Whether to recompute the summary at mount time */
1370 static int compute_summary_at_mount = 0;
1371 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1372 	   &compute_summary_at_mount, 0, "Recompute summary at mount");
1373 static int print_threads = 0;
1374 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1375     &print_threads, 0, "Notify flusher thread start/stop");
1376 
1377 /* List of all filesystems mounted with soft updates */
1378 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1379 
1380 /*
1381  * This function cleans the worklist for a filesystem.
1382  * Each filesystem running with soft dependencies gets its own
1383  * thread to run in this function. The thread is started up in
1384  * softdep_mount and shutdown in softdep_unmount. They show up
1385  * as part of the kernel "bufdaemon" process whose process
1386  * entry is available in bufdaemonproc.
1387  */
1388 static int searchfailed;
1389 extern struct proc *bufdaemonproc;
1390 static void
1391 softdep_flush(addr)
1392 	void *addr;
1393 {
1394 	struct mount *mp;
1395 	struct thread *td;
1396 	struct ufsmount *ump;
1397 
1398 	td = curthread;
1399 	td->td_pflags |= TDP_NORUNNINGBUF;
1400 	mp = (struct mount *)addr;
1401 	ump = VFSTOUFS(mp);
1402 	atomic_add_int(&stat_flush_threads, 1);
1403 	ACQUIRE_LOCK(ump);
1404 	ump->softdep_flags &= ~FLUSH_STARTING;
1405 	wakeup(&ump->softdep_flushtd);
1406 	FREE_LOCK(ump);
1407 	if (print_threads) {
1408 		if (stat_flush_threads == 1)
1409 			printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1410 			    bufdaemonproc->p_pid);
1411 		printf("Start thread %s\n", td->td_name);
1412 	}
1413 	for (;;) {
1414 		while (softdep_process_worklist(mp, 0) > 0 ||
1415 		    (MOUNTEDSUJ(mp) &&
1416 		    VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1417 			kthread_suspend_check();
1418 		ACQUIRE_LOCK(ump);
1419 		if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1420 			msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1421 			    "sdflush", hz / 2);
1422 		ump->softdep_flags &= ~FLUSH_CLEANUP;
1423 		/*
1424 		 * Check to see if we are done and need to exit.
1425 		 */
1426 		if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1427 			FREE_LOCK(ump);
1428 			continue;
1429 		}
1430 		ump->softdep_flags &= ~FLUSH_EXIT;
1431 		FREE_LOCK(ump);
1432 		wakeup(&ump->softdep_flags);
1433 		if (print_threads)
1434 			printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups);
1435 		atomic_subtract_int(&stat_flush_threads, 1);
1436 		kthread_exit();
1437 		panic("kthread_exit failed\n");
1438 	}
1439 }
1440 
1441 static void
1442 worklist_speedup(mp)
1443 	struct mount *mp;
1444 {
1445 	struct ufsmount *ump;
1446 
1447 	ump = VFSTOUFS(mp);
1448 	LOCK_OWNED(ump);
1449 	if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1450 		ump->softdep_flags |= FLUSH_CLEANUP;
1451 	wakeup(&ump->softdep_flushtd);
1452 }
1453 
1454 static int
1455 softdep_speedup(ump)
1456 	struct ufsmount *ump;
1457 {
1458 	struct ufsmount *altump;
1459 	struct mount_softdeps *sdp;
1460 
1461 	LOCK_OWNED(ump);
1462 	worklist_speedup(ump->um_mountp);
1463 	bd_speedup();
1464 	/*
1465 	 * If we have global shortages, then we need other
1466 	 * filesystems to help with the cleanup. Here we wakeup a
1467 	 * flusher thread for a filesystem that is over its fair
1468 	 * share of resources.
1469 	 */
1470 	if (req_clear_inodedeps || req_clear_remove) {
1471 		ACQUIRE_GBLLOCK(&lk);
1472 		TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1473 			if ((altump = sdp->sd_ump) == ump)
1474 				continue;
1475 			if (((req_clear_inodedeps &&
1476 			    altump->softdep_curdeps[D_INODEDEP] >
1477 			    max_softdeps / stat_flush_threads) ||
1478 			    (req_clear_remove &&
1479 			    altump->softdep_curdeps[D_DIRREM] >
1480 			    (max_softdeps / 2) / stat_flush_threads)) &&
1481 			    TRY_ACQUIRE_LOCK(altump))
1482 				break;
1483 		}
1484 		if (sdp == NULL) {
1485 			searchfailed++;
1486 			FREE_GBLLOCK(&lk);
1487 		} else {
1488 			/*
1489 			 * Move to the end of the list so we pick a
1490 			 * different one on out next try.
1491 			 */
1492 			TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1493 			TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1494 			FREE_GBLLOCK(&lk);
1495 			if ((altump->softdep_flags &
1496 			    (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1497 				altump->softdep_flags |= FLUSH_CLEANUP;
1498 			altump->um_softdep->sd_cleanups++;
1499 			wakeup(&altump->softdep_flushtd);
1500 			FREE_LOCK(altump);
1501 		}
1502 	}
1503 	return (speedup_syncer());
1504 }
1505 
1506 /*
1507  * Add an item to the end of the work queue.
1508  * This routine requires that the lock be held.
1509  * This is the only routine that adds items to the list.
1510  * The following routine is the only one that removes items
1511  * and does so in order from first to last.
1512  */
1513 
1514 #define	WK_HEAD		0x0001	/* Add to HEAD. */
1515 #define	WK_NODELAY	0x0002	/* Process immediately. */
1516 
1517 static void
1518 add_to_worklist(wk, flags)
1519 	struct worklist *wk;
1520 	int flags;
1521 {
1522 	struct ufsmount *ump;
1523 
1524 	ump = VFSTOUFS(wk->wk_mp);
1525 	LOCK_OWNED(ump);
1526 	if (wk->wk_state & ONWORKLIST)
1527 		panic("add_to_worklist: %s(0x%X) already on list",
1528 		    TYPENAME(wk->wk_type), wk->wk_state);
1529 	wk->wk_state |= ONWORKLIST;
1530 	if (ump->softdep_on_worklist == 0) {
1531 		LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1532 		ump->softdep_worklist_tail = wk;
1533 	} else if (flags & WK_HEAD) {
1534 		LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1535 	} else {
1536 		LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1537 		ump->softdep_worklist_tail = wk;
1538 	}
1539 	ump->softdep_on_worklist += 1;
1540 	if (flags & WK_NODELAY)
1541 		worklist_speedup(wk->wk_mp);
1542 }
1543 
1544 /*
1545  * Remove the item to be processed. If we are removing the last
1546  * item on the list, we need to recalculate the tail pointer.
1547  */
1548 static void
1549 remove_from_worklist(wk)
1550 	struct worklist *wk;
1551 {
1552 	struct ufsmount *ump;
1553 
1554 	ump = VFSTOUFS(wk->wk_mp);
1555 	if (ump->softdep_worklist_tail == wk)
1556 		ump->softdep_worklist_tail =
1557 		    (struct worklist *)wk->wk_list.le_prev;
1558 	WORKLIST_REMOVE(wk);
1559 	ump->softdep_on_worklist -= 1;
1560 }
1561 
1562 static void
1563 wake_worklist(wk)
1564 	struct worklist *wk;
1565 {
1566 	if (wk->wk_state & IOWAITING) {
1567 		wk->wk_state &= ~IOWAITING;
1568 		wakeup(wk);
1569 	}
1570 }
1571 
1572 static void
1573 wait_worklist(wk, wmesg)
1574 	struct worklist *wk;
1575 	char *wmesg;
1576 {
1577 	struct ufsmount *ump;
1578 
1579 	ump = VFSTOUFS(wk->wk_mp);
1580 	wk->wk_state |= IOWAITING;
1581 	msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1582 }
1583 
1584 /*
1585  * Process that runs once per second to handle items in the background queue.
1586  *
1587  * Note that we ensure that everything is done in the order in which they
1588  * appear in the queue. The code below depends on this property to ensure
1589  * that blocks of a file are freed before the inode itself is freed. This
1590  * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1591  * until all the old ones have been purged from the dependency lists.
1592  */
1593 static int
1594 softdep_process_worklist(mp, full)
1595 	struct mount *mp;
1596 	int full;
1597 {
1598 	int cnt, matchcnt;
1599 	struct ufsmount *ump;
1600 	long starttime;
1601 
1602 	KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1603 	if (MOUNTEDSOFTDEP(mp) == 0)
1604 		return (0);
1605 	matchcnt = 0;
1606 	ump = VFSTOUFS(mp);
1607 	ACQUIRE_LOCK(ump);
1608 	starttime = time_second;
1609 	softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1610 	check_clear_deps(mp);
1611 	while (ump->softdep_on_worklist > 0) {
1612 		if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1613 			break;
1614 		else
1615 			matchcnt += cnt;
1616 		check_clear_deps(mp);
1617 		/*
1618 		 * We do not generally want to stop for buffer space, but if
1619 		 * we are really being a buffer hog, we will stop and wait.
1620 		 */
1621 		if (should_yield()) {
1622 			FREE_LOCK(ump);
1623 			kern_yield(PRI_USER);
1624 			bwillwrite();
1625 			ACQUIRE_LOCK(ump);
1626 		}
1627 		/*
1628 		 * Never allow processing to run for more than one
1629 		 * second. This gives the syncer thread the opportunity
1630 		 * to pause if appropriate.
1631 		 */
1632 		if (!full && starttime != time_second)
1633 			break;
1634 	}
1635 	if (full == 0)
1636 		journal_unsuspend(ump);
1637 	FREE_LOCK(ump);
1638 	return (matchcnt);
1639 }
1640 
1641 /*
1642  * Process all removes associated with a vnode if we are running out of
1643  * journal space.  Any other process which attempts to flush these will
1644  * be unable as we have the vnodes locked.
1645  */
1646 static void
1647 process_removes(vp)
1648 	struct vnode *vp;
1649 {
1650 	struct inodedep *inodedep;
1651 	struct dirrem *dirrem;
1652 	struct ufsmount *ump;
1653 	struct mount *mp;
1654 	ino_t inum;
1655 
1656 	mp = vp->v_mount;
1657 	ump = VFSTOUFS(mp);
1658 	LOCK_OWNED(ump);
1659 	inum = VTOI(vp)->i_number;
1660 	for (;;) {
1661 top:
1662 		if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1663 			return;
1664 		LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1665 			/*
1666 			 * If another thread is trying to lock this vnode
1667 			 * it will fail but we must wait for it to do so
1668 			 * before we can proceed.
1669 			 */
1670 			if (dirrem->dm_state & INPROGRESS) {
1671 				wait_worklist(&dirrem->dm_list, "pwrwait");
1672 				goto top;
1673 			}
1674 			if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1675 			    (COMPLETE | ONWORKLIST))
1676 				break;
1677 		}
1678 		if (dirrem == NULL)
1679 			return;
1680 		remove_from_worklist(&dirrem->dm_list);
1681 		FREE_LOCK(ump);
1682 		if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1683 			panic("process_removes: suspended filesystem");
1684 		handle_workitem_remove(dirrem, 0);
1685 		vn_finished_secondary_write(mp);
1686 		ACQUIRE_LOCK(ump);
1687 	}
1688 }
1689 
1690 /*
1691  * Process all truncations associated with a vnode if we are running out
1692  * of journal space.  This is called when the vnode lock is already held
1693  * and no other process can clear the truncation.  This function returns
1694  * a value greater than zero if it did any work.
1695  */
1696 static void
1697 process_truncates(vp)
1698 	struct vnode *vp;
1699 {
1700 	struct inodedep *inodedep;
1701 	struct freeblks *freeblks;
1702 	struct ufsmount *ump;
1703 	struct mount *mp;
1704 	ino_t inum;
1705 	int cgwait;
1706 
1707 	mp = vp->v_mount;
1708 	ump = VFSTOUFS(mp);
1709 	LOCK_OWNED(ump);
1710 	inum = VTOI(vp)->i_number;
1711 	for (;;) {
1712 		if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1713 			return;
1714 		cgwait = 0;
1715 		TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1716 			/* Journal entries not yet written.  */
1717 			if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1718 				jwait(&LIST_FIRST(
1719 				    &freeblks->fb_jblkdephd)->jb_list,
1720 				    MNT_WAIT);
1721 				break;
1722 			}
1723 			/* Another thread is executing this item. */
1724 			if (freeblks->fb_state & INPROGRESS) {
1725 				wait_worklist(&freeblks->fb_list, "ptrwait");
1726 				break;
1727 			}
1728 			/* Freeblks is waiting on a inode write. */
1729 			if ((freeblks->fb_state & COMPLETE) == 0) {
1730 				FREE_LOCK(ump);
1731 				ffs_update(vp, 1);
1732 				ACQUIRE_LOCK(ump);
1733 				break;
1734 			}
1735 			if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1736 			    (ALLCOMPLETE | ONWORKLIST)) {
1737 				remove_from_worklist(&freeblks->fb_list);
1738 				freeblks->fb_state |= INPROGRESS;
1739 				FREE_LOCK(ump);
1740 				if (vn_start_secondary_write(NULL, &mp,
1741 				    V_NOWAIT))
1742 					panic("process_truncates: "
1743 					    "suspended filesystem");
1744 				handle_workitem_freeblocks(freeblks, 0);
1745 				vn_finished_secondary_write(mp);
1746 				ACQUIRE_LOCK(ump);
1747 				break;
1748 			}
1749 			if (freeblks->fb_cgwait)
1750 				cgwait++;
1751 		}
1752 		if (cgwait) {
1753 			FREE_LOCK(ump);
1754 			sync_cgs(mp, MNT_WAIT);
1755 			ffs_sync_snap(mp, MNT_WAIT);
1756 			ACQUIRE_LOCK(ump);
1757 			continue;
1758 		}
1759 		if (freeblks == NULL)
1760 			break;
1761 	}
1762 	return;
1763 }
1764 
1765 /*
1766  * Process one item on the worklist.
1767  */
1768 static int
1769 process_worklist_item(mp, target, flags)
1770 	struct mount *mp;
1771 	int target;
1772 	int flags;
1773 {
1774 	struct worklist sentinel;
1775 	struct worklist *wk;
1776 	struct ufsmount *ump;
1777 	int matchcnt;
1778 	int error;
1779 
1780 	KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1781 	/*
1782 	 * If we are being called because of a process doing a
1783 	 * copy-on-write, then it is not safe to write as we may
1784 	 * recurse into the copy-on-write routine.
1785 	 */
1786 	if (curthread->td_pflags & TDP_COWINPROGRESS)
1787 		return (-1);
1788 	PHOLD(curproc);	/* Don't let the stack go away. */
1789 	ump = VFSTOUFS(mp);
1790 	LOCK_OWNED(ump);
1791 	matchcnt = 0;
1792 	sentinel.wk_mp = NULL;
1793 	sentinel.wk_type = D_SENTINEL;
1794 	LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1795 	for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1796 	    wk = LIST_NEXT(&sentinel, wk_list)) {
1797 		if (wk->wk_type == D_SENTINEL) {
1798 			LIST_REMOVE(&sentinel, wk_list);
1799 			LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1800 			continue;
1801 		}
1802 		if (wk->wk_state & INPROGRESS)
1803 			panic("process_worklist_item: %p already in progress.",
1804 			    wk);
1805 		wk->wk_state |= INPROGRESS;
1806 		remove_from_worklist(wk);
1807 		FREE_LOCK(ump);
1808 		if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1809 			panic("process_worklist_item: suspended filesystem");
1810 		switch (wk->wk_type) {
1811 		case D_DIRREM:
1812 			/* removal of a directory entry */
1813 			error = handle_workitem_remove(WK_DIRREM(wk), flags);
1814 			break;
1815 
1816 		case D_FREEBLKS:
1817 			/* releasing blocks and/or fragments from a file */
1818 			error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
1819 			    flags);
1820 			break;
1821 
1822 		case D_FREEFRAG:
1823 			/* releasing a fragment when replaced as a file grows */
1824 			handle_workitem_freefrag(WK_FREEFRAG(wk));
1825 			error = 0;
1826 			break;
1827 
1828 		case D_FREEFILE:
1829 			/* releasing an inode when its link count drops to 0 */
1830 			handle_workitem_freefile(WK_FREEFILE(wk));
1831 			error = 0;
1832 			break;
1833 
1834 		default:
1835 			panic("%s_process_worklist: Unknown type %s",
1836 			    "softdep", TYPENAME(wk->wk_type));
1837 			/* NOTREACHED */
1838 		}
1839 		vn_finished_secondary_write(mp);
1840 		ACQUIRE_LOCK(ump);
1841 		if (error == 0) {
1842 			if (++matchcnt == target)
1843 				break;
1844 			continue;
1845 		}
1846 		/*
1847 		 * We have to retry the worklist item later.  Wake up any
1848 		 * waiters who may be able to complete it immediately and
1849 		 * add the item back to the head so we don't try to execute
1850 		 * it again.
1851 		 */
1852 		wk->wk_state &= ~INPROGRESS;
1853 		wake_worklist(wk);
1854 		add_to_worklist(wk, WK_HEAD);
1855 	}
1856 	/* Sentinal could've become the tail from remove_from_worklist. */
1857 	if (ump->softdep_worklist_tail == &sentinel)
1858 		ump->softdep_worklist_tail =
1859 		    (struct worklist *)sentinel.wk_list.le_prev;
1860 	LIST_REMOVE(&sentinel, wk_list);
1861 	PRELE(curproc);
1862 	return (matchcnt);
1863 }
1864 
1865 /*
1866  * Move dependencies from one buffer to another.
1867  */
1868 int
1869 softdep_move_dependencies(oldbp, newbp)
1870 	struct buf *oldbp;
1871 	struct buf *newbp;
1872 {
1873 	struct worklist *wk, *wktail;
1874 	struct ufsmount *ump;
1875 	int dirty;
1876 
1877 	if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
1878 		return (0);
1879 	KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
1880 	    ("softdep_move_dependencies called on non-softdep filesystem"));
1881 	dirty = 0;
1882 	wktail = NULL;
1883 	ump = VFSTOUFS(wk->wk_mp);
1884 	ACQUIRE_LOCK(ump);
1885 	while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
1886 		LIST_REMOVE(wk, wk_list);
1887 		if (wk->wk_type == D_BMSAFEMAP &&
1888 		    bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
1889 			dirty = 1;
1890 		if (wktail == NULL)
1891 			LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1892 		else
1893 			LIST_INSERT_AFTER(wktail, wk, wk_list);
1894 		wktail = wk;
1895 	}
1896 	FREE_LOCK(ump);
1897 
1898 	return (dirty);
1899 }
1900 
1901 /*
1902  * Purge the work list of all items associated with a particular mount point.
1903  */
1904 int
1905 softdep_flushworklist(oldmnt, countp, td)
1906 	struct mount *oldmnt;
1907 	int *countp;
1908 	struct thread *td;
1909 {
1910 	struct vnode *devvp;
1911 	struct ufsmount *ump;
1912 	int count, error;
1913 
1914 	/*
1915 	 * Alternately flush the block device associated with the mount
1916 	 * point and process any dependencies that the flushing
1917 	 * creates. We continue until no more worklist dependencies
1918 	 * are found.
1919 	 */
1920 	*countp = 0;
1921 	error = 0;
1922 	ump = VFSTOUFS(oldmnt);
1923 	devvp = ump->um_devvp;
1924 	while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1925 		*countp += count;
1926 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1927 		error = VOP_FSYNC(devvp, MNT_WAIT, td);
1928 		VOP_UNLOCK(devvp, 0);
1929 		if (error != 0)
1930 			break;
1931 	}
1932 	return (error);
1933 }
1934 
1935 #define	SU_WAITIDLE_RETRIES	20
1936 static int
1937 softdep_waitidle(struct mount *mp, int flags __unused)
1938 {
1939 	struct ufsmount *ump;
1940 	struct vnode *devvp;
1941 	struct thread *td;
1942 	int error, i;
1943 
1944 	ump = VFSTOUFS(mp);
1945 	devvp = ump->um_devvp;
1946 	td = curthread;
1947 	error = 0;
1948 	ACQUIRE_LOCK(ump);
1949 	for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
1950 		ump->softdep_req = 1;
1951 		KASSERT((flags & FORCECLOSE) == 0 ||
1952 		    ump->softdep_on_worklist == 0,
1953 		    ("softdep_waitidle: work added after flush"));
1954 		msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
1955 		    "softdeps", 10 * hz);
1956 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1957 		error = VOP_FSYNC(devvp, MNT_WAIT, td);
1958 		VOP_UNLOCK(devvp, 0);
1959 		ACQUIRE_LOCK(ump);
1960 		if (error != 0)
1961 			break;
1962 	}
1963 	ump->softdep_req = 0;
1964 	if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
1965 		error = EBUSY;
1966 		printf("softdep_waitidle: Failed to flush worklist for %p\n",
1967 		    mp);
1968 	}
1969 	FREE_LOCK(ump);
1970 	return (error);
1971 }
1972 
1973 /*
1974  * Flush all vnodes and worklist items associated with a specified mount point.
1975  */
1976 int
1977 softdep_flushfiles(oldmnt, flags, td)
1978 	struct mount *oldmnt;
1979 	int flags;
1980 	struct thread *td;
1981 {
1982 #ifdef QUOTA
1983 	struct ufsmount *ump;
1984 	int i;
1985 #endif
1986 	int error, early, depcount, loopcnt, retry_flush_count, retry;
1987 	int morework;
1988 
1989 	KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0,
1990 	    ("softdep_flushfiles called on non-softdep filesystem"));
1991 	loopcnt = 10;
1992 	retry_flush_count = 3;
1993 retry_flush:
1994 	error = 0;
1995 
1996 	/*
1997 	 * Alternately flush the vnodes associated with the mount
1998 	 * point and process any dependencies that the flushing
1999 	 * creates. In theory, this loop can happen at most twice,
2000 	 * but we give it a few extra just to be sure.
2001 	 */
2002 	for (; loopcnt > 0; loopcnt--) {
2003 		/*
2004 		 * Do another flush in case any vnodes were brought in
2005 		 * as part of the cleanup operations.
2006 		 */
2007 		early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2008 		    MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2009 		if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2010 			break;
2011 		if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2012 		    depcount == 0)
2013 			break;
2014 	}
2015 	/*
2016 	 * If we are unmounting then it is an error to fail. If we
2017 	 * are simply trying to downgrade to read-only, then filesystem
2018 	 * activity can keep us busy forever, so we just fail with EBUSY.
2019 	 */
2020 	if (loopcnt == 0) {
2021 		if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2022 			panic("softdep_flushfiles: looping");
2023 		error = EBUSY;
2024 	}
2025 	if (!error)
2026 		error = softdep_waitidle(oldmnt, flags);
2027 	if (!error) {
2028 		if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2029 			retry = 0;
2030 			MNT_ILOCK(oldmnt);
2031 			KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0,
2032 			    ("softdep_flushfiles: !MNTK_NOINSMNTQ"));
2033 			morework = oldmnt->mnt_nvnodelistsize > 0;
2034 #ifdef QUOTA
2035 			ump = VFSTOUFS(oldmnt);
2036 			UFS_LOCK(ump);
2037 			for (i = 0; i < MAXQUOTAS; i++) {
2038 				if (ump->um_quotas[i] != NULLVP)
2039 					morework = 1;
2040 			}
2041 			UFS_UNLOCK(ump);
2042 #endif
2043 			if (morework) {
2044 				if (--retry_flush_count > 0) {
2045 					retry = 1;
2046 					loopcnt = 3;
2047 				} else
2048 					error = EBUSY;
2049 			}
2050 			MNT_IUNLOCK(oldmnt);
2051 			if (retry)
2052 				goto retry_flush;
2053 		}
2054 	}
2055 	return (error);
2056 }
2057 
2058 /*
2059  * Structure hashing.
2060  *
2061  * There are four types of structures that can be looked up:
2062  *	1) pagedep structures identified by mount point, inode number,
2063  *	   and logical block.
2064  *	2) inodedep structures identified by mount point and inode number.
2065  *	3) newblk structures identified by mount point and
2066  *	   physical block number.
2067  *	4) bmsafemap structures identified by mount point and
2068  *	   cylinder group number.
2069  *
2070  * The "pagedep" and "inodedep" dependency structures are hashed
2071  * separately from the file blocks and inodes to which they correspond.
2072  * This separation helps when the in-memory copy of an inode or
2073  * file block must be replaced. It also obviates the need to access
2074  * an inode or file page when simply updating (or de-allocating)
2075  * dependency structures. Lookup of newblk structures is needed to
2076  * find newly allocated blocks when trying to associate them with
2077  * their allocdirect or allocindir structure.
2078  *
2079  * The lookup routines optionally create and hash a new instance when
2080  * an existing entry is not found. The bmsafemap lookup routine always
2081  * allocates a new structure if an existing one is not found.
2082  */
2083 #define DEPALLOC	0x0001	/* allocate structure if lookup fails */
2084 
2085 /*
2086  * Structures and routines associated with pagedep caching.
2087  */
2088 #define	PAGEDEP_HASH(ump, inum, lbn) \
2089 	(&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2090 
2091 static int
2092 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2093 	struct pagedep_hashhead *pagedephd;
2094 	ino_t ino;
2095 	ufs_lbn_t lbn;
2096 	struct pagedep **pagedeppp;
2097 {
2098 	struct pagedep *pagedep;
2099 
2100 	LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2101 		if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2102 			*pagedeppp = pagedep;
2103 			return (1);
2104 		}
2105 	}
2106 	*pagedeppp = NULL;
2107 	return (0);
2108 }
2109 /*
2110  * Look up a pagedep. Return 1 if found, 0 otherwise.
2111  * If not found, allocate if DEPALLOC flag is passed.
2112  * Found or allocated entry is returned in pagedeppp.
2113  */
2114 static int
2115 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2116 	struct mount *mp;
2117 	struct buf *bp;
2118 	ino_t ino;
2119 	ufs_lbn_t lbn;
2120 	int flags;
2121 	struct pagedep **pagedeppp;
2122 {
2123 	struct pagedep *pagedep;
2124 	struct pagedep_hashhead *pagedephd;
2125 	struct worklist *wk;
2126 	struct ufsmount *ump;
2127 	int ret;
2128 	int i;
2129 
2130 	ump = VFSTOUFS(mp);
2131 	LOCK_OWNED(ump);
2132 	if (bp) {
2133 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2134 			if (wk->wk_type == D_PAGEDEP) {
2135 				*pagedeppp = WK_PAGEDEP(wk);
2136 				return (1);
2137 			}
2138 		}
2139 	}
2140 	pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2141 	ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2142 	if (ret) {
2143 		if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2144 			WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2145 		return (1);
2146 	}
2147 	if ((flags & DEPALLOC) == 0)
2148 		return (0);
2149 	FREE_LOCK(ump);
2150 	pagedep = malloc(sizeof(struct pagedep),
2151 	    M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2152 	workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2153 	ACQUIRE_LOCK(ump);
2154 	ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2155 	if (*pagedeppp) {
2156 		/*
2157 		 * This should never happen since we only create pagedeps
2158 		 * with the vnode lock held.  Could be an assert.
2159 		 */
2160 		WORKITEM_FREE(pagedep, D_PAGEDEP);
2161 		return (ret);
2162 	}
2163 	pagedep->pd_ino = ino;
2164 	pagedep->pd_lbn = lbn;
2165 	LIST_INIT(&pagedep->pd_dirremhd);
2166 	LIST_INIT(&pagedep->pd_pendinghd);
2167 	for (i = 0; i < DAHASHSZ; i++)
2168 		LIST_INIT(&pagedep->pd_diraddhd[i]);
2169 	LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2170 	WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2171 	*pagedeppp = pagedep;
2172 	return (0);
2173 }
2174 
2175 /*
2176  * Structures and routines associated with inodedep caching.
2177  */
2178 #define	INODEDEP_HASH(ump, inum) \
2179       (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2180 
2181 static int
2182 inodedep_find(inodedephd, inum, inodedeppp)
2183 	struct inodedep_hashhead *inodedephd;
2184 	ino_t inum;
2185 	struct inodedep **inodedeppp;
2186 {
2187 	struct inodedep *inodedep;
2188 
2189 	LIST_FOREACH(inodedep, inodedephd, id_hash)
2190 		if (inum == inodedep->id_ino)
2191 			break;
2192 	if (inodedep) {
2193 		*inodedeppp = inodedep;
2194 		return (1);
2195 	}
2196 	*inodedeppp = NULL;
2197 
2198 	return (0);
2199 }
2200 /*
2201  * Look up an inodedep. Return 1 if found, 0 if not found.
2202  * If not found, allocate if DEPALLOC flag is passed.
2203  * Found or allocated entry is returned in inodedeppp.
2204  */
2205 static int
2206 inodedep_lookup(mp, inum, flags, inodedeppp)
2207 	struct mount *mp;
2208 	ino_t inum;
2209 	int flags;
2210 	struct inodedep **inodedeppp;
2211 {
2212 	struct inodedep *inodedep;
2213 	struct inodedep_hashhead *inodedephd;
2214 	struct ufsmount *ump;
2215 	struct fs *fs;
2216 
2217 	ump = VFSTOUFS(mp);
2218 	LOCK_OWNED(ump);
2219 	fs = ump->um_fs;
2220 	inodedephd = INODEDEP_HASH(ump, inum);
2221 
2222 	if (inodedep_find(inodedephd, inum, inodedeppp))
2223 		return (1);
2224 	if ((flags & DEPALLOC) == 0)
2225 		return (0);
2226 	/*
2227 	 * If the system is over its limit and our filesystem is
2228 	 * responsible for more than our share of that usage and
2229 	 * we are not in a rush, request some inodedep cleanup.
2230 	 */
2231 	if (softdep_excess_items(ump, D_INODEDEP))
2232 		schedule_cleanup(mp);
2233 	else
2234 		FREE_LOCK(ump);
2235 	inodedep = malloc(sizeof(struct inodedep),
2236 		M_INODEDEP, M_SOFTDEP_FLAGS);
2237 	workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2238 	ACQUIRE_LOCK(ump);
2239 	if (inodedep_find(inodedephd, inum, inodedeppp)) {
2240 		WORKITEM_FREE(inodedep, D_INODEDEP);
2241 		return (1);
2242 	}
2243 	inodedep->id_fs = fs;
2244 	inodedep->id_ino = inum;
2245 	inodedep->id_state = ALLCOMPLETE;
2246 	inodedep->id_nlinkdelta = 0;
2247 	inodedep->id_savedino1 = NULL;
2248 	inodedep->id_savedsize = -1;
2249 	inodedep->id_savedextsize = -1;
2250 	inodedep->id_savednlink = -1;
2251 	inodedep->id_bmsafemap = NULL;
2252 	inodedep->id_mkdiradd = NULL;
2253 	LIST_INIT(&inodedep->id_dirremhd);
2254 	LIST_INIT(&inodedep->id_pendinghd);
2255 	LIST_INIT(&inodedep->id_inowait);
2256 	LIST_INIT(&inodedep->id_bufwait);
2257 	TAILQ_INIT(&inodedep->id_inoreflst);
2258 	TAILQ_INIT(&inodedep->id_inoupdt);
2259 	TAILQ_INIT(&inodedep->id_newinoupdt);
2260 	TAILQ_INIT(&inodedep->id_extupdt);
2261 	TAILQ_INIT(&inodedep->id_newextupdt);
2262 	TAILQ_INIT(&inodedep->id_freeblklst);
2263 	LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2264 	*inodedeppp = inodedep;
2265 	return (0);
2266 }
2267 
2268 /*
2269  * Structures and routines associated with newblk caching.
2270  */
2271 #define	NEWBLK_HASH(ump, inum) \
2272 	(&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2273 
2274 static int
2275 newblk_find(newblkhd, newblkno, flags, newblkpp)
2276 	struct newblk_hashhead *newblkhd;
2277 	ufs2_daddr_t newblkno;
2278 	int flags;
2279 	struct newblk **newblkpp;
2280 {
2281 	struct newblk *newblk;
2282 
2283 	LIST_FOREACH(newblk, newblkhd, nb_hash) {
2284 		if (newblkno != newblk->nb_newblkno)
2285 			continue;
2286 		/*
2287 		 * If we're creating a new dependency don't match those that
2288 		 * have already been converted to allocdirects.  This is for
2289 		 * a frag extend.
2290 		 */
2291 		if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2292 			continue;
2293 		break;
2294 	}
2295 	if (newblk) {
2296 		*newblkpp = newblk;
2297 		return (1);
2298 	}
2299 	*newblkpp = NULL;
2300 	return (0);
2301 }
2302 
2303 /*
2304  * Look up a newblk. Return 1 if found, 0 if not found.
2305  * If not found, allocate if DEPALLOC flag is passed.
2306  * Found or allocated entry is returned in newblkpp.
2307  */
2308 static int
2309 newblk_lookup(mp, newblkno, flags, newblkpp)
2310 	struct mount *mp;
2311 	ufs2_daddr_t newblkno;
2312 	int flags;
2313 	struct newblk **newblkpp;
2314 {
2315 	struct newblk *newblk;
2316 	struct newblk_hashhead *newblkhd;
2317 	struct ufsmount *ump;
2318 
2319 	ump = VFSTOUFS(mp);
2320 	LOCK_OWNED(ump);
2321 	newblkhd = NEWBLK_HASH(ump, newblkno);
2322 	if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2323 		return (1);
2324 	if ((flags & DEPALLOC) == 0)
2325 		return (0);
2326 	if (softdep_excess_items(ump, D_NEWBLK) ||
2327 	    softdep_excess_items(ump, D_ALLOCDIRECT) ||
2328 	    softdep_excess_items(ump, D_ALLOCINDIR))
2329 		schedule_cleanup(mp);
2330 	else
2331 		FREE_LOCK(ump);
2332 	newblk = malloc(sizeof(union allblk), M_NEWBLK,
2333 	    M_SOFTDEP_FLAGS | M_ZERO);
2334 	workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2335 	ACQUIRE_LOCK(ump);
2336 	if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2337 		WORKITEM_FREE(newblk, D_NEWBLK);
2338 		return (1);
2339 	}
2340 	newblk->nb_freefrag = NULL;
2341 	LIST_INIT(&newblk->nb_indirdeps);
2342 	LIST_INIT(&newblk->nb_newdirblk);
2343 	LIST_INIT(&newblk->nb_jwork);
2344 	newblk->nb_state = ATTACHED;
2345 	newblk->nb_newblkno = newblkno;
2346 	LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2347 	*newblkpp = newblk;
2348 	return (0);
2349 }
2350 
2351 /*
2352  * Structures and routines associated with freed indirect block caching.
2353  */
2354 #define	INDIR_HASH(ump, blkno) \
2355 	(&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2356 
2357 /*
2358  * Lookup an indirect block in the indir hash table.  The freework is
2359  * removed and potentially freed.  The caller must do a blocking journal
2360  * write before writing to the blkno.
2361  */
2362 static int
2363 indirblk_lookup(mp, blkno)
2364 	struct mount *mp;
2365 	ufs2_daddr_t blkno;
2366 {
2367 	struct freework *freework;
2368 	struct indir_hashhead *wkhd;
2369 	struct ufsmount *ump;
2370 
2371 	ump = VFSTOUFS(mp);
2372 	wkhd = INDIR_HASH(ump, blkno);
2373 	TAILQ_FOREACH(freework, wkhd, fw_next) {
2374 		if (freework->fw_blkno != blkno)
2375 			continue;
2376 		indirblk_remove(freework);
2377 		return (1);
2378 	}
2379 	return (0);
2380 }
2381 
2382 /*
2383  * Insert an indirect block represented by freework into the indirblk
2384  * hash table so that it may prevent the block from being re-used prior
2385  * to the journal being written.
2386  */
2387 static void
2388 indirblk_insert(freework)
2389 	struct freework *freework;
2390 {
2391 	struct jblocks *jblocks;
2392 	struct jseg *jseg;
2393 	struct ufsmount *ump;
2394 
2395 	ump = VFSTOUFS(freework->fw_list.wk_mp);
2396 	jblocks = ump->softdep_jblocks;
2397 	jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2398 	if (jseg == NULL)
2399 		return;
2400 
2401 	LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2402 	TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2403 	    fw_next);
2404 	freework->fw_state &= ~DEPCOMPLETE;
2405 }
2406 
2407 static void
2408 indirblk_remove(freework)
2409 	struct freework *freework;
2410 {
2411 	struct ufsmount *ump;
2412 
2413 	ump = VFSTOUFS(freework->fw_list.wk_mp);
2414 	LIST_REMOVE(freework, fw_segs);
2415 	TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2416 	freework->fw_state |= DEPCOMPLETE;
2417 	if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2418 		WORKITEM_FREE(freework, D_FREEWORK);
2419 }
2420 
2421 /*
2422  * Executed during filesystem system initialization before
2423  * mounting any filesystems.
2424  */
2425 void
2426 softdep_initialize()
2427 {
2428 
2429 	TAILQ_INIT(&softdepmounts);
2430 #ifdef __LP64__
2431 	max_softdeps = desiredvnodes * 4;
2432 #else
2433 	max_softdeps = desiredvnodes * 2;
2434 #endif
2435 
2436 	/* initialise bioops hack */
2437 	bioops.io_start = softdep_disk_io_initiation;
2438 	bioops.io_complete = softdep_disk_write_complete;
2439 	bioops.io_deallocate = softdep_deallocate_dependencies;
2440 	bioops.io_countdeps = softdep_count_dependencies;
2441 	softdep_ast_cleanup = softdep_ast_cleanup_proc;
2442 
2443 	/* Initialize the callout with an mtx. */
2444 	callout_init_mtx(&softdep_callout, &lk, 0);
2445 }
2446 
2447 /*
2448  * Executed after all filesystems have been unmounted during
2449  * filesystem module unload.
2450  */
2451 void
2452 softdep_uninitialize()
2453 {
2454 
2455 	/* clear bioops hack */
2456 	bioops.io_start = NULL;
2457 	bioops.io_complete = NULL;
2458 	bioops.io_deallocate = NULL;
2459 	bioops.io_countdeps = NULL;
2460 	softdep_ast_cleanup = NULL;
2461 
2462 	callout_drain(&softdep_callout);
2463 }
2464 
2465 /*
2466  * Called at mount time to notify the dependency code that a
2467  * filesystem wishes to use it.
2468  */
2469 int
2470 softdep_mount(devvp, mp, fs, cred)
2471 	struct vnode *devvp;
2472 	struct mount *mp;
2473 	struct fs *fs;
2474 	struct ucred *cred;
2475 {
2476 	struct csum_total cstotal;
2477 	struct mount_softdeps *sdp;
2478 	struct ufsmount *ump;
2479 	struct cg *cgp;
2480 	struct buf *bp;
2481 	u_int cyl, i;
2482 	int error;
2483 
2484 	sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2485 	    M_WAITOK | M_ZERO);
2486 	MNT_ILOCK(mp);
2487 	mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2488 	if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2489 		mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2490 			MNTK_SOFTDEP | MNTK_NOASYNC;
2491 	}
2492 	ump = VFSTOUFS(mp);
2493 	ump->um_softdep = sdp;
2494 	MNT_IUNLOCK(mp);
2495 	rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock");
2496 	sdp->sd_ump = ump;
2497 	LIST_INIT(&ump->softdep_workitem_pending);
2498 	LIST_INIT(&ump->softdep_journal_pending);
2499 	TAILQ_INIT(&ump->softdep_unlinked);
2500 	LIST_INIT(&ump->softdep_dirtycg);
2501 	ump->softdep_worklist_tail = NULL;
2502 	ump->softdep_on_worklist = 0;
2503 	ump->softdep_deps = 0;
2504 	LIST_INIT(&ump->softdep_mkdirlisthd);
2505 	ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
2506 	    &ump->pagedep_hash_size);
2507 	ump->pagedep_nextclean = 0;
2508 	ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP,
2509 	    &ump->inodedep_hash_size);
2510 	ump->inodedep_nextclean = 0;
2511 	ump->newblk_hashtbl = hashinit(max_softdeps / 2,  M_NEWBLK,
2512 	    &ump->newblk_hash_size);
2513 	ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP,
2514 	    &ump->bmsafemap_hash_size);
2515 	i = 1 << (ffs(desiredvnodes / 10) - 1);
2516 	ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead),
2517 	    M_FREEWORK, M_WAITOK);
2518 	ump->indir_hash_size = i - 1;
2519 	for (i = 0; i <= ump->indir_hash_size; i++)
2520 		TAILQ_INIT(&ump->indir_hashtbl[i]);
2521 	ACQUIRE_GBLLOCK(&lk);
2522 	TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2523 	FREE_GBLLOCK(&lk);
2524 	if ((fs->fs_flags & FS_SUJ) &&
2525 	    (error = journal_mount(mp, fs, cred)) != 0) {
2526 		printf("Failed to start journal: %d\n", error);
2527 		softdep_unmount(mp);
2528 		return (error);
2529 	}
2530 	/*
2531 	 * Start our flushing thread in the bufdaemon process.
2532 	 */
2533 	ACQUIRE_LOCK(ump);
2534 	ump->softdep_flags |= FLUSH_STARTING;
2535 	FREE_LOCK(ump);
2536 	kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2537 	    &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2538 	    mp->mnt_stat.f_mntonname);
2539 	ACQUIRE_LOCK(ump);
2540 	while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2541 		msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2542 		    hz / 2);
2543 	}
2544 	FREE_LOCK(ump);
2545 	/*
2546 	 * When doing soft updates, the counters in the
2547 	 * superblock may have gotten out of sync. Recomputation
2548 	 * can take a long time and can be deferred for background
2549 	 * fsck.  However, the old behavior of scanning the cylinder
2550 	 * groups and recalculating them at mount time is available
2551 	 * by setting vfs.ffs.compute_summary_at_mount to one.
2552 	 */
2553 	if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2554 		return (0);
2555 	bzero(&cstotal, sizeof cstotal);
2556 	for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2557 		if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2558 		    fs->fs_cgsize, cred, &bp)) != 0) {
2559 			brelse(bp);
2560 			softdep_unmount(mp);
2561 			return (error);
2562 		}
2563 		cgp = (struct cg *)bp->b_data;
2564 		cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2565 		cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2566 		cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2567 		cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2568 		fs->fs_cs(fs, cyl) = cgp->cg_cs;
2569 		brelse(bp);
2570 	}
2571 #ifdef INVARIANTS
2572 	if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2573 		printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2574 #endif
2575 	bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2576 	return (0);
2577 }
2578 
2579 void
2580 softdep_unmount(mp)
2581 	struct mount *mp;
2582 {
2583 	struct ufsmount *ump;
2584 #ifdef INVARIANTS
2585 	int i;
2586 #endif
2587 
2588 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
2589 	    ("softdep_unmount called on non-softdep filesystem"));
2590 	ump = VFSTOUFS(mp);
2591 	MNT_ILOCK(mp);
2592 	mp->mnt_flag &= ~MNT_SOFTDEP;
2593 	if (MOUNTEDSUJ(mp) == 0) {
2594 		MNT_IUNLOCK(mp);
2595 	} else {
2596 		mp->mnt_flag &= ~MNT_SUJ;
2597 		MNT_IUNLOCK(mp);
2598 		journal_unmount(ump);
2599 	}
2600 	/*
2601 	 * Shut down our flushing thread. Check for NULL is if
2602 	 * softdep_mount errors out before the thread has been created.
2603 	 */
2604 	if (ump->softdep_flushtd != NULL) {
2605 		ACQUIRE_LOCK(ump);
2606 		ump->softdep_flags |= FLUSH_EXIT;
2607 		wakeup(&ump->softdep_flushtd);
2608 		msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP,
2609 		    "sdwait", 0);
2610 		KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2611 		    ("Thread shutdown failed"));
2612 	}
2613 	/*
2614 	 * Free up our resources.
2615 	 */
2616 	ACQUIRE_GBLLOCK(&lk);
2617 	TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next);
2618 	FREE_GBLLOCK(&lk);
2619 	rw_destroy(LOCK_PTR(ump));
2620 	hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size);
2621 	hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size);
2622 	hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size);
2623 	hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP,
2624 	    ump->bmsafemap_hash_size);
2625 	free(ump->indir_hashtbl, M_FREEWORK);
2626 #ifdef INVARIANTS
2627 	for (i = 0; i <= D_LAST; i++)
2628 		KASSERT(ump->softdep_curdeps[i] == 0,
2629 		    ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2630 		    TYPENAME(i), ump->softdep_curdeps[i]));
2631 #endif
2632 	free(ump->um_softdep, M_MOUNTDATA);
2633 }
2634 
2635 static struct jblocks *
2636 jblocks_create(void)
2637 {
2638 	struct jblocks *jblocks;
2639 
2640 	jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2641 	TAILQ_INIT(&jblocks->jb_segs);
2642 	jblocks->jb_avail = 10;
2643 	jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2644 	    M_JBLOCKS, M_WAITOK | M_ZERO);
2645 
2646 	return (jblocks);
2647 }
2648 
2649 static ufs2_daddr_t
2650 jblocks_alloc(jblocks, bytes, actual)
2651 	struct jblocks *jblocks;
2652 	int bytes;
2653 	int *actual;
2654 {
2655 	ufs2_daddr_t daddr;
2656 	struct jextent *jext;
2657 	int freecnt;
2658 	int blocks;
2659 
2660 	blocks = bytes / DEV_BSIZE;
2661 	jext = &jblocks->jb_extent[jblocks->jb_head];
2662 	freecnt = jext->je_blocks - jblocks->jb_off;
2663 	if (freecnt == 0) {
2664 		jblocks->jb_off = 0;
2665 		if (++jblocks->jb_head > jblocks->jb_used)
2666 			jblocks->jb_head = 0;
2667 		jext = &jblocks->jb_extent[jblocks->jb_head];
2668 		freecnt = jext->je_blocks;
2669 	}
2670 	if (freecnt > blocks)
2671 		freecnt = blocks;
2672 	*actual = freecnt * DEV_BSIZE;
2673 	daddr = jext->je_daddr + jblocks->jb_off;
2674 	jblocks->jb_off += freecnt;
2675 	jblocks->jb_free -= freecnt;
2676 
2677 	return (daddr);
2678 }
2679 
2680 static void
2681 jblocks_free(jblocks, mp, bytes)
2682 	struct jblocks *jblocks;
2683 	struct mount *mp;
2684 	int bytes;
2685 {
2686 
2687 	LOCK_OWNED(VFSTOUFS(mp));
2688 	jblocks->jb_free += bytes / DEV_BSIZE;
2689 	if (jblocks->jb_suspended)
2690 		worklist_speedup(mp);
2691 	wakeup(jblocks);
2692 }
2693 
2694 static void
2695 jblocks_destroy(jblocks)
2696 	struct jblocks *jblocks;
2697 {
2698 
2699 	if (jblocks->jb_extent)
2700 		free(jblocks->jb_extent, M_JBLOCKS);
2701 	free(jblocks, M_JBLOCKS);
2702 }
2703 
2704 static void
2705 jblocks_add(jblocks, daddr, blocks)
2706 	struct jblocks *jblocks;
2707 	ufs2_daddr_t daddr;
2708 	int blocks;
2709 {
2710 	struct jextent *jext;
2711 
2712 	jblocks->jb_blocks += blocks;
2713 	jblocks->jb_free += blocks;
2714 	jext = &jblocks->jb_extent[jblocks->jb_used];
2715 	/* Adding the first block. */
2716 	if (jext->je_daddr == 0) {
2717 		jext->je_daddr = daddr;
2718 		jext->je_blocks = blocks;
2719 		return;
2720 	}
2721 	/* Extending the last extent. */
2722 	if (jext->je_daddr + jext->je_blocks == daddr) {
2723 		jext->je_blocks += blocks;
2724 		return;
2725 	}
2726 	/* Adding a new extent. */
2727 	if (++jblocks->jb_used == jblocks->jb_avail) {
2728 		jblocks->jb_avail *= 2;
2729 		jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2730 		    M_JBLOCKS, M_WAITOK | M_ZERO);
2731 		memcpy(jext, jblocks->jb_extent,
2732 		    sizeof(struct jextent) * jblocks->jb_used);
2733 		free(jblocks->jb_extent, M_JBLOCKS);
2734 		jblocks->jb_extent = jext;
2735 	}
2736 	jext = &jblocks->jb_extent[jblocks->jb_used];
2737 	jext->je_daddr = daddr;
2738 	jext->je_blocks = blocks;
2739 	return;
2740 }
2741 
2742 int
2743 softdep_journal_lookup(mp, vpp)
2744 	struct mount *mp;
2745 	struct vnode **vpp;
2746 {
2747 	struct componentname cnp;
2748 	struct vnode *dvp;
2749 	ino_t sujournal;
2750 	int error;
2751 
2752 	error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2753 	if (error)
2754 		return (error);
2755 	bzero(&cnp, sizeof(cnp));
2756 	cnp.cn_nameiop = LOOKUP;
2757 	cnp.cn_flags = ISLASTCN;
2758 	cnp.cn_thread = curthread;
2759 	cnp.cn_cred = curthread->td_ucred;
2760 	cnp.cn_pnbuf = SUJ_FILE;
2761 	cnp.cn_nameptr = SUJ_FILE;
2762 	cnp.cn_namelen = strlen(SUJ_FILE);
2763 	error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2764 	vput(dvp);
2765 	if (error != 0)
2766 		return (error);
2767 	error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2768 	return (error);
2769 }
2770 
2771 /*
2772  * Open and verify the journal file.
2773  */
2774 static int
2775 journal_mount(mp, fs, cred)
2776 	struct mount *mp;
2777 	struct fs *fs;
2778 	struct ucred *cred;
2779 {
2780 	struct jblocks *jblocks;
2781 	struct ufsmount *ump;
2782 	struct vnode *vp;
2783 	struct inode *ip;
2784 	ufs2_daddr_t blkno;
2785 	int bcount;
2786 	int error;
2787 	int i;
2788 
2789 	ump = VFSTOUFS(mp);
2790 	ump->softdep_journal_tail = NULL;
2791 	ump->softdep_on_journal = 0;
2792 	ump->softdep_accdeps = 0;
2793 	ump->softdep_req = 0;
2794 	ump->softdep_jblocks = NULL;
2795 	error = softdep_journal_lookup(mp, &vp);
2796 	if (error != 0) {
2797 		printf("Failed to find journal.  Use tunefs to create one\n");
2798 		return (error);
2799 	}
2800 	ip = VTOI(vp);
2801 	if (ip->i_size < SUJ_MIN) {
2802 		error = ENOSPC;
2803 		goto out;
2804 	}
2805 	bcount = lblkno(fs, ip->i_size);	/* Only use whole blocks. */
2806 	jblocks = jblocks_create();
2807 	for (i = 0; i < bcount; i++) {
2808 		error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
2809 		if (error)
2810 			break;
2811 		jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
2812 	}
2813 	if (error) {
2814 		jblocks_destroy(jblocks);
2815 		goto out;
2816 	}
2817 	jblocks->jb_low = jblocks->jb_free / 3;	/* Reserve 33%. */
2818 	jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
2819 	ump->softdep_jblocks = jblocks;
2820 out:
2821 	if (error == 0) {
2822 		MNT_ILOCK(mp);
2823 		mp->mnt_flag |= MNT_SUJ;
2824 		mp->mnt_flag &= ~MNT_SOFTDEP;
2825 		MNT_IUNLOCK(mp);
2826 		/*
2827 		 * Only validate the journal contents if the
2828 		 * filesystem is clean, otherwise we write the logs
2829 		 * but they'll never be used.  If the filesystem was
2830 		 * still dirty when we mounted it the journal is
2831 		 * invalid and a new journal can only be valid if it
2832 		 * starts from a clean mount.
2833 		 */
2834 		if (fs->fs_clean) {
2835 			DIP_SET(ip, i_modrev, fs->fs_mtime);
2836 			ip->i_flags |= IN_MODIFIED;
2837 			ffs_update(vp, 1);
2838 		}
2839 	}
2840 	vput(vp);
2841 	return (error);
2842 }
2843 
2844 static void
2845 journal_unmount(ump)
2846 	struct ufsmount *ump;
2847 {
2848 
2849 	if (ump->softdep_jblocks)
2850 		jblocks_destroy(ump->softdep_jblocks);
2851 	ump->softdep_jblocks = NULL;
2852 }
2853 
2854 /*
2855  * Called when a journal record is ready to be written.  Space is allocated
2856  * and the journal entry is created when the journal is flushed to stable
2857  * store.
2858  */
2859 static void
2860 add_to_journal(wk)
2861 	struct worklist *wk;
2862 {
2863 	struct ufsmount *ump;
2864 
2865 	ump = VFSTOUFS(wk->wk_mp);
2866 	LOCK_OWNED(ump);
2867 	if (wk->wk_state & ONWORKLIST)
2868 		panic("add_to_journal: %s(0x%X) already on list",
2869 		    TYPENAME(wk->wk_type), wk->wk_state);
2870 	wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
2871 	if (LIST_EMPTY(&ump->softdep_journal_pending)) {
2872 		ump->softdep_jblocks->jb_age = ticks;
2873 		LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
2874 	} else
2875 		LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
2876 	ump->softdep_journal_tail = wk;
2877 	ump->softdep_on_journal += 1;
2878 }
2879 
2880 /*
2881  * Remove an arbitrary item for the journal worklist maintain the tail
2882  * pointer.  This happens when a new operation obviates the need to
2883  * journal an old operation.
2884  */
2885 static void
2886 remove_from_journal(wk)
2887 	struct worklist *wk;
2888 {
2889 	struct ufsmount *ump;
2890 
2891 	ump = VFSTOUFS(wk->wk_mp);
2892 	LOCK_OWNED(ump);
2893 #ifdef INVARIANTS
2894 	{
2895 		struct worklist *wkn;
2896 
2897 		LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
2898 			if (wkn == wk)
2899 				break;
2900 		if (wkn == NULL)
2901 			panic("remove_from_journal: %p is not in journal", wk);
2902 	}
2903 #endif
2904 	/*
2905 	 * We emulate a TAILQ to save space in most structures which do not
2906 	 * require TAILQ semantics.  Here we must update the tail position
2907 	 * when removing the tail which is not the final entry. This works
2908 	 * only if the worklist linkage are at the beginning of the structure.
2909 	 */
2910 	if (ump->softdep_journal_tail == wk)
2911 		ump->softdep_journal_tail =
2912 		    (struct worklist *)wk->wk_list.le_prev;
2913 	WORKLIST_REMOVE(wk);
2914 	ump->softdep_on_journal -= 1;
2915 }
2916 
2917 /*
2918  * Check for journal space as well as dependency limits so the prelink
2919  * code can throttle both journaled and non-journaled filesystems.
2920  * Threshold is 0 for low and 1 for min.
2921  */
2922 static int
2923 journal_space(ump, thresh)
2924 	struct ufsmount *ump;
2925 	int thresh;
2926 {
2927 	struct jblocks *jblocks;
2928 	int limit, avail;
2929 
2930 	jblocks = ump->softdep_jblocks;
2931 	if (jblocks == NULL)
2932 		return (1);
2933 	/*
2934 	 * We use a tighter restriction here to prevent request_cleanup()
2935 	 * running in threads from running into locks we currently hold.
2936 	 * We have to be over the limit and our filesystem has to be
2937 	 * responsible for more than our share of that usage.
2938 	 */
2939 	limit = (max_softdeps / 10) * 9;
2940 	if (dep_current[D_INODEDEP] > limit &&
2941 	    ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
2942 		return (0);
2943 	if (thresh)
2944 		thresh = jblocks->jb_min;
2945 	else
2946 		thresh = jblocks->jb_low;
2947 	avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
2948 	avail = jblocks->jb_free - avail;
2949 
2950 	return (avail > thresh);
2951 }
2952 
2953 static void
2954 journal_suspend(ump)
2955 	struct ufsmount *ump;
2956 {
2957 	struct jblocks *jblocks;
2958 	struct mount *mp;
2959 
2960 	mp = UFSTOVFS(ump);
2961 	jblocks = ump->softdep_jblocks;
2962 	MNT_ILOCK(mp);
2963 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
2964 		stat_journal_min++;
2965 		mp->mnt_kern_flag |= MNTK_SUSPEND;
2966 		mp->mnt_susp_owner = ump->softdep_flushtd;
2967 	}
2968 	jblocks->jb_suspended = 1;
2969 	MNT_IUNLOCK(mp);
2970 }
2971 
2972 static int
2973 journal_unsuspend(struct ufsmount *ump)
2974 {
2975 	struct jblocks *jblocks;
2976 	struct mount *mp;
2977 
2978 	mp = UFSTOVFS(ump);
2979 	jblocks = ump->softdep_jblocks;
2980 
2981 	if (jblocks != NULL && jblocks->jb_suspended &&
2982 	    journal_space(ump, jblocks->jb_min)) {
2983 		jblocks->jb_suspended = 0;
2984 		FREE_LOCK(ump);
2985 		mp->mnt_susp_owner = curthread;
2986 		vfs_write_resume(mp, 0);
2987 		ACQUIRE_LOCK(ump);
2988 		return (1);
2989 	}
2990 	return (0);
2991 }
2992 
2993 /*
2994  * Called before any allocation function to be certain that there is
2995  * sufficient space in the journal prior to creating any new records.
2996  * Since in the case of block allocation we may have multiple locked
2997  * buffers at the time of the actual allocation we can not block
2998  * when the journal records are created.  Doing so would create a deadlock
2999  * if any of these buffers needed to be flushed to reclaim space.  Instead
3000  * we require a sufficiently large amount of available space such that
3001  * each thread in the system could have passed this allocation check and
3002  * still have sufficient free space.  With 20% of a minimum journal size
3003  * of 1MB we have 6553 records available.
3004  */
3005 int
3006 softdep_prealloc(vp, waitok)
3007 	struct vnode *vp;
3008 	int waitok;
3009 {
3010 	struct ufsmount *ump;
3011 
3012 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3013 	    ("softdep_prealloc called on non-softdep filesystem"));
3014 	/*
3015 	 * Nothing to do if we are not running journaled soft updates.
3016 	 * If we currently hold the snapshot lock, we must avoid
3017 	 * handling other resources that could cause deadlock.  Do not
3018 	 * touch quotas vnode since it is typically recursed with
3019 	 * other vnode locks held.
3020 	 */
3021 	if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3022 	    (vp->v_vflag & VV_SYSTEM) != 0)
3023 		return (0);
3024 	ump = VFSTOUFS(vp->v_mount);
3025 	ACQUIRE_LOCK(ump);
3026 	if (journal_space(ump, 0)) {
3027 		FREE_LOCK(ump);
3028 		return (0);
3029 	}
3030 	stat_journal_low++;
3031 	FREE_LOCK(ump);
3032 	if (waitok == MNT_NOWAIT)
3033 		return (ENOSPC);
3034 	/*
3035 	 * Attempt to sync this vnode once to flush any journal
3036 	 * work attached to it.
3037 	 */
3038 	if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3039 		ffs_syncvnode(vp, waitok, 0);
3040 	ACQUIRE_LOCK(ump);
3041 	process_removes(vp);
3042 	process_truncates(vp);
3043 	if (journal_space(ump, 0) == 0) {
3044 		softdep_speedup(ump);
3045 		if (journal_space(ump, 1) == 0)
3046 			journal_suspend(ump);
3047 	}
3048 	FREE_LOCK(ump);
3049 
3050 	return (0);
3051 }
3052 
3053 /*
3054  * Before adjusting a link count on a vnode verify that we have sufficient
3055  * journal space.  If not, process operations that depend on the currently
3056  * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3057  * and softdep flush threads can not acquire these locks to reclaim space.
3058  */
3059 static void
3060 softdep_prelink(dvp, vp)
3061 	struct vnode *dvp;
3062 	struct vnode *vp;
3063 {
3064 	struct ufsmount *ump;
3065 
3066 	ump = VFSTOUFS(dvp->v_mount);
3067 	LOCK_OWNED(ump);
3068 	/*
3069 	 * Nothing to do if we have sufficient journal space.
3070 	 * If we currently hold the snapshot lock, we must avoid
3071 	 * handling other resources that could cause deadlock.
3072 	 */
3073 	if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp))))
3074 		return;
3075 	stat_journal_low++;
3076 	FREE_LOCK(ump);
3077 	if (vp)
3078 		ffs_syncvnode(vp, MNT_NOWAIT, 0);
3079 	ffs_syncvnode(dvp, MNT_WAIT, 0);
3080 	ACQUIRE_LOCK(ump);
3081 	/* Process vp before dvp as it may create .. removes. */
3082 	if (vp) {
3083 		process_removes(vp);
3084 		process_truncates(vp);
3085 	}
3086 	process_removes(dvp);
3087 	process_truncates(dvp);
3088 	softdep_speedup(ump);
3089 	process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3090 	if (journal_space(ump, 0) == 0) {
3091 		softdep_speedup(ump);
3092 		if (journal_space(ump, 1) == 0)
3093 			journal_suspend(ump);
3094 	}
3095 }
3096 
3097 static void
3098 jseg_write(ump, jseg, data)
3099 	struct ufsmount *ump;
3100 	struct jseg *jseg;
3101 	uint8_t *data;
3102 {
3103 	struct jsegrec *rec;
3104 
3105 	rec = (struct jsegrec *)data;
3106 	rec->jsr_seq = jseg->js_seq;
3107 	rec->jsr_oldest = jseg->js_oldseq;
3108 	rec->jsr_cnt = jseg->js_cnt;
3109 	rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3110 	rec->jsr_crc = 0;
3111 	rec->jsr_time = ump->um_fs->fs_mtime;
3112 }
3113 
3114 static inline void
3115 inoref_write(inoref, jseg, rec)
3116 	struct inoref *inoref;
3117 	struct jseg *jseg;
3118 	struct jrefrec *rec;
3119 {
3120 
3121 	inoref->if_jsegdep->jd_seg = jseg;
3122 	rec->jr_ino = inoref->if_ino;
3123 	rec->jr_parent = inoref->if_parent;
3124 	rec->jr_nlink = inoref->if_nlink;
3125 	rec->jr_mode = inoref->if_mode;
3126 	rec->jr_diroff = inoref->if_diroff;
3127 }
3128 
3129 static void
3130 jaddref_write(jaddref, jseg, data)
3131 	struct jaddref *jaddref;
3132 	struct jseg *jseg;
3133 	uint8_t *data;
3134 {
3135 	struct jrefrec *rec;
3136 
3137 	rec = (struct jrefrec *)data;
3138 	rec->jr_op = JOP_ADDREF;
3139 	inoref_write(&jaddref->ja_ref, jseg, rec);
3140 }
3141 
3142 static void
3143 jremref_write(jremref, jseg, data)
3144 	struct jremref *jremref;
3145 	struct jseg *jseg;
3146 	uint8_t *data;
3147 {
3148 	struct jrefrec *rec;
3149 
3150 	rec = (struct jrefrec *)data;
3151 	rec->jr_op = JOP_REMREF;
3152 	inoref_write(&jremref->jr_ref, jseg, rec);
3153 }
3154 
3155 static void
3156 jmvref_write(jmvref, jseg, data)
3157 	struct jmvref *jmvref;
3158 	struct jseg *jseg;
3159 	uint8_t *data;
3160 {
3161 	struct jmvrec *rec;
3162 
3163 	rec = (struct jmvrec *)data;
3164 	rec->jm_op = JOP_MVREF;
3165 	rec->jm_ino = jmvref->jm_ino;
3166 	rec->jm_parent = jmvref->jm_parent;
3167 	rec->jm_oldoff = jmvref->jm_oldoff;
3168 	rec->jm_newoff = jmvref->jm_newoff;
3169 }
3170 
3171 static void
3172 jnewblk_write(jnewblk, jseg, data)
3173 	struct jnewblk *jnewblk;
3174 	struct jseg *jseg;
3175 	uint8_t *data;
3176 {
3177 	struct jblkrec *rec;
3178 
3179 	jnewblk->jn_jsegdep->jd_seg = jseg;
3180 	rec = (struct jblkrec *)data;
3181 	rec->jb_op = JOP_NEWBLK;
3182 	rec->jb_ino = jnewblk->jn_ino;
3183 	rec->jb_blkno = jnewblk->jn_blkno;
3184 	rec->jb_lbn = jnewblk->jn_lbn;
3185 	rec->jb_frags = jnewblk->jn_frags;
3186 	rec->jb_oldfrags = jnewblk->jn_oldfrags;
3187 }
3188 
3189 static void
3190 jfreeblk_write(jfreeblk, jseg, data)
3191 	struct jfreeblk *jfreeblk;
3192 	struct jseg *jseg;
3193 	uint8_t *data;
3194 {
3195 	struct jblkrec *rec;
3196 
3197 	jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3198 	rec = (struct jblkrec *)data;
3199 	rec->jb_op = JOP_FREEBLK;
3200 	rec->jb_ino = jfreeblk->jf_ino;
3201 	rec->jb_blkno = jfreeblk->jf_blkno;
3202 	rec->jb_lbn = jfreeblk->jf_lbn;
3203 	rec->jb_frags = jfreeblk->jf_frags;
3204 	rec->jb_oldfrags = 0;
3205 }
3206 
3207 static void
3208 jfreefrag_write(jfreefrag, jseg, data)
3209 	struct jfreefrag *jfreefrag;
3210 	struct jseg *jseg;
3211 	uint8_t *data;
3212 {
3213 	struct jblkrec *rec;
3214 
3215 	jfreefrag->fr_jsegdep->jd_seg = jseg;
3216 	rec = (struct jblkrec *)data;
3217 	rec->jb_op = JOP_FREEBLK;
3218 	rec->jb_ino = jfreefrag->fr_ino;
3219 	rec->jb_blkno = jfreefrag->fr_blkno;
3220 	rec->jb_lbn = jfreefrag->fr_lbn;
3221 	rec->jb_frags = jfreefrag->fr_frags;
3222 	rec->jb_oldfrags = 0;
3223 }
3224 
3225 static void
3226 jtrunc_write(jtrunc, jseg, data)
3227 	struct jtrunc *jtrunc;
3228 	struct jseg *jseg;
3229 	uint8_t *data;
3230 {
3231 	struct jtrncrec *rec;
3232 
3233 	jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3234 	rec = (struct jtrncrec *)data;
3235 	rec->jt_op = JOP_TRUNC;
3236 	rec->jt_ino = jtrunc->jt_ino;
3237 	rec->jt_size = jtrunc->jt_size;
3238 	rec->jt_extsize = jtrunc->jt_extsize;
3239 }
3240 
3241 static void
3242 jfsync_write(jfsync, jseg, data)
3243 	struct jfsync *jfsync;
3244 	struct jseg *jseg;
3245 	uint8_t *data;
3246 {
3247 	struct jtrncrec *rec;
3248 
3249 	rec = (struct jtrncrec *)data;
3250 	rec->jt_op = JOP_SYNC;
3251 	rec->jt_ino = jfsync->jfs_ino;
3252 	rec->jt_size = jfsync->jfs_size;
3253 	rec->jt_extsize = jfsync->jfs_extsize;
3254 }
3255 
3256 static void
3257 softdep_flushjournal(mp)
3258 	struct mount *mp;
3259 {
3260 	struct jblocks *jblocks;
3261 	struct ufsmount *ump;
3262 
3263 	if (MOUNTEDSUJ(mp) == 0)
3264 		return;
3265 	ump = VFSTOUFS(mp);
3266 	jblocks = ump->softdep_jblocks;
3267 	ACQUIRE_LOCK(ump);
3268 	while (ump->softdep_on_journal) {
3269 		jblocks->jb_needseg = 1;
3270 		softdep_process_journal(mp, NULL, MNT_WAIT);
3271 	}
3272 	FREE_LOCK(ump);
3273 }
3274 
3275 static void softdep_synchronize_completed(struct bio *);
3276 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3277 
3278 static void
3279 softdep_synchronize_completed(bp)
3280         struct bio *bp;
3281 {
3282 	struct jseg *oldest;
3283 	struct jseg *jseg;
3284 	struct ufsmount *ump;
3285 
3286 	/*
3287 	 * caller1 marks the last segment written before we issued the
3288 	 * synchronize cache.
3289 	 */
3290 	jseg = bp->bio_caller1;
3291 	if (jseg == NULL) {
3292 		g_destroy_bio(bp);
3293 		return;
3294 	}
3295 	ump = VFSTOUFS(jseg->js_list.wk_mp);
3296 	ACQUIRE_LOCK(ump);
3297 	oldest = NULL;
3298 	/*
3299 	 * Mark all the journal entries waiting on the synchronize cache
3300 	 * as completed so they may continue on.
3301 	 */
3302 	while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3303 		jseg->js_state |= COMPLETE;
3304 		oldest = jseg;
3305 		jseg = TAILQ_PREV(jseg, jseglst, js_next);
3306 	}
3307 	/*
3308 	 * Restart deferred journal entry processing from the oldest
3309 	 * completed jseg.
3310 	 */
3311 	if (oldest)
3312 		complete_jsegs(oldest);
3313 
3314 	FREE_LOCK(ump);
3315 	g_destroy_bio(bp);
3316 }
3317 
3318 /*
3319  * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3320  * barriers.  The journal must be written prior to any blocks that depend
3321  * on it and the journal can not be released until the blocks have be
3322  * written.  This code handles both barriers simultaneously.
3323  */
3324 static void
3325 softdep_synchronize(bp, ump, caller1)
3326 	struct bio *bp;
3327 	struct ufsmount *ump;
3328 	void *caller1;
3329 {
3330 
3331 	bp->bio_cmd = BIO_FLUSH;
3332 	bp->bio_flags |= BIO_ORDERED;
3333 	bp->bio_data = NULL;
3334 	bp->bio_offset = ump->um_cp->provider->mediasize;
3335 	bp->bio_length = 0;
3336 	bp->bio_done = softdep_synchronize_completed;
3337 	bp->bio_caller1 = caller1;
3338 	g_io_request(bp,
3339 	    (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private);
3340 }
3341 
3342 /*
3343  * Flush some journal records to disk.
3344  */
3345 static void
3346 softdep_process_journal(mp, needwk, flags)
3347 	struct mount *mp;
3348 	struct worklist *needwk;
3349 	int flags;
3350 {
3351 	struct jblocks *jblocks;
3352 	struct ufsmount *ump;
3353 	struct worklist *wk;
3354 	struct jseg *jseg;
3355 	struct buf *bp;
3356 	struct bio *bio;
3357 	uint8_t *data;
3358 	struct fs *fs;
3359 	int shouldflush;
3360 	int segwritten;
3361 	int jrecmin;	/* Minimum records per block. */
3362 	int jrecmax;	/* Maximum records per block. */
3363 	int size;
3364 	int cnt;
3365 	int off;
3366 	int devbsize;
3367 
3368 	if (MOUNTEDSUJ(mp) == 0)
3369 		return;
3370 	shouldflush = softdep_flushcache;
3371 	bio = NULL;
3372 	jseg = NULL;
3373 	ump = VFSTOUFS(mp);
3374 	LOCK_OWNED(ump);
3375 	fs = ump->um_fs;
3376 	jblocks = ump->softdep_jblocks;
3377 	devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3378 	/*
3379 	 * We write anywhere between a disk block and fs block.  The upper
3380 	 * bound is picked to prevent buffer cache fragmentation and limit
3381 	 * processing time per I/O.
3382 	 */
3383 	jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3384 	jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3385 	segwritten = 0;
3386 	for (;;) {
3387 		cnt = ump->softdep_on_journal;
3388 		/*
3389 		 * Criteria for writing a segment:
3390 		 * 1) We have a full block.
3391 		 * 2) We're called from jwait() and haven't found the
3392 		 *    journal item yet.
3393 		 * 3) Always write if needseg is set.
3394 		 * 4) If we are called from process_worklist and have
3395 		 *    not yet written anything we write a partial block
3396 		 *    to enforce a 1 second maximum latency on journal
3397 		 *    entries.
3398 		 */
3399 		if (cnt < (jrecmax - 1) && needwk == NULL &&
3400 		    jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3401 			break;
3402 		cnt++;
3403 		/*
3404 		 * Verify some free journal space.  softdep_prealloc() should
3405 		 * guarantee that we don't run out so this is indicative of
3406 		 * a problem with the flow control.  Try to recover
3407 		 * gracefully in any event.
3408 		 */
3409 		while (jblocks->jb_free == 0) {
3410 			if (flags != MNT_WAIT)
3411 				break;
3412 			printf("softdep: Out of journal space!\n");
3413 			softdep_speedup(ump);
3414 			msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3415 		}
3416 		FREE_LOCK(ump);
3417 		jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3418 		workitem_alloc(&jseg->js_list, D_JSEG, mp);
3419 		LIST_INIT(&jseg->js_entries);
3420 		LIST_INIT(&jseg->js_indirs);
3421 		jseg->js_state = ATTACHED;
3422 		if (shouldflush == 0)
3423 			jseg->js_state |= COMPLETE;
3424 		else if (bio == NULL)
3425 			bio = g_alloc_bio();
3426 		jseg->js_jblocks = jblocks;
3427 		bp = geteblk(fs->fs_bsize, 0);
3428 		ACQUIRE_LOCK(ump);
3429 		/*
3430 		 * If there was a race while we were allocating the block
3431 		 * and jseg the entry we care about was likely written.
3432 		 * We bail out in both the WAIT and NOWAIT case and assume
3433 		 * the caller will loop if the entry it cares about is
3434 		 * not written.
3435 		 */
3436 		cnt = ump->softdep_on_journal;
3437 		if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3438 			bp->b_flags |= B_INVAL | B_NOCACHE;
3439 			WORKITEM_FREE(jseg, D_JSEG);
3440 			FREE_LOCK(ump);
3441 			brelse(bp);
3442 			ACQUIRE_LOCK(ump);
3443 			break;
3444 		}
3445 		/*
3446 		 * Calculate the disk block size required for the available
3447 		 * records rounded to the min size.
3448 		 */
3449 		if (cnt == 0)
3450 			size = devbsize;
3451 		else if (cnt < jrecmax)
3452 			size = howmany(cnt, jrecmin) * devbsize;
3453 		else
3454 			size = fs->fs_bsize;
3455 		/*
3456 		 * Allocate a disk block for this journal data and account
3457 		 * for truncation of the requested size if enough contiguous
3458 		 * space was not available.
3459 		 */
3460 		bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3461 		bp->b_lblkno = bp->b_blkno;
3462 		bp->b_offset = bp->b_blkno * DEV_BSIZE;
3463 		bp->b_bcount = size;
3464 		bp->b_flags &= ~B_INVAL;
3465 		bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3466 		/*
3467 		 * Initialize our jseg with cnt records.  Assign the next
3468 		 * sequence number to it and link it in-order.
3469 		 */
3470 		cnt = MIN(cnt, (size / devbsize) * jrecmin);
3471 		jseg->js_buf = bp;
3472 		jseg->js_cnt = cnt;
3473 		jseg->js_refs = cnt + 1;	/* Self ref. */
3474 		jseg->js_size = size;
3475 		jseg->js_seq = jblocks->jb_nextseq++;
3476 		if (jblocks->jb_oldestseg == NULL)
3477 			jblocks->jb_oldestseg = jseg;
3478 		jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3479 		TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3480 		if (jblocks->jb_writeseg == NULL)
3481 			jblocks->jb_writeseg = jseg;
3482 		/*
3483 		 * Start filling in records from the pending list.
3484 		 */
3485 		data = bp->b_data;
3486 		off = 0;
3487 
3488 		/*
3489 		 * Always put a header on the first block.
3490 		 * XXX As with below, there might not be a chance to get
3491 		 * into the loop.  Ensure that something valid is written.
3492 		 */
3493 		jseg_write(ump, jseg, data);
3494 		off += JREC_SIZE;
3495 		data = bp->b_data + off;
3496 
3497 		/*
3498 		 * XXX Something is wrong here.  There's no work to do,
3499 		 * but we need to perform and I/O and allow it to complete
3500 		 * anyways.
3501 		 */
3502 		if (LIST_EMPTY(&ump->softdep_journal_pending))
3503 			stat_emptyjblocks++;
3504 
3505 		while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3506 		    != NULL) {
3507 			if (cnt == 0)
3508 				break;
3509 			/* Place a segment header on every device block. */
3510 			if ((off % devbsize) == 0) {
3511 				jseg_write(ump, jseg, data);
3512 				off += JREC_SIZE;
3513 				data = bp->b_data + off;
3514 			}
3515 			if (wk == needwk)
3516 				needwk = NULL;
3517 			remove_from_journal(wk);
3518 			wk->wk_state |= INPROGRESS;
3519 			WORKLIST_INSERT(&jseg->js_entries, wk);
3520 			switch (wk->wk_type) {
3521 			case D_JADDREF:
3522 				jaddref_write(WK_JADDREF(wk), jseg, data);
3523 				break;
3524 			case D_JREMREF:
3525 				jremref_write(WK_JREMREF(wk), jseg, data);
3526 				break;
3527 			case D_JMVREF:
3528 				jmvref_write(WK_JMVREF(wk), jseg, data);
3529 				break;
3530 			case D_JNEWBLK:
3531 				jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3532 				break;
3533 			case D_JFREEBLK:
3534 				jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3535 				break;
3536 			case D_JFREEFRAG:
3537 				jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3538 				break;
3539 			case D_JTRUNC:
3540 				jtrunc_write(WK_JTRUNC(wk), jseg, data);
3541 				break;
3542 			case D_JFSYNC:
3543 				jfsync_write(WK_JFSYNC(wk), jseg, data);
3544 				break;
3545 			default:
3546 				panic("process_journal: Unknown type %s",
3547 				    TYPENAME(wk->wk_type));
3548 				/* NOTREACHED */
3549 			}
3550 			off += JREC_SIZE;
3551 			data = bp->b_data + off;
3552 			cnt--;
3553 		}
3554 
3555 		/* Clear any remaining space so we don't leak kernel data */
3556 		if (size > off)
3557 			bzero(data, size - off);
3558 
3559 		/*
3560 		 * Write this one buffer and continue.
3561 		 */
3562 		segwritten = 1;
3563 		jblocks->jb_needseg = 0;
3564 		WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3565 		FREE_LOCK(ump);
3566 		pbgetvp(ump->um_devvp, bp);
3567 		/*
3568 		 * We only do the blocking wait once we find the journal
3569 		 * entry we're looking for.
3570 		 */
3571 		if (needwk == NULL && flags == MNT_WAIT)
3572 			bwrite(bp);
3573 		else
3574 			bawrite(bp);
3575 		ACQUIRE_LOCK(ump);
3576 	}
3577 	/*
3578 	 * If we wrote a segment issue a synchronize cache so the journal
3579 	 * is reflected on disk before the data is written.  Since reclaiming
3580 	 * journal space also requires writing a journal record this
3581 	 * process also enforces a barrier before reclamation.
3582 	 */
3583 	if (segwritten && shouldflush) {
3584 		softdep_synchronize(bio, ump,
3585 		    TAILQ_LAST(&jblocks->jb_segs, jseglst));
3586 	} else if (bio)
3587 		g_destroy_bio(bio);
3588 	/*
3589 	 * If we've suspended the filesystem because we ran out of journal
3590 	 * space either try to sync it here to make some progress or
3591 	 * unsuspend it if we already have.
3592 	 */
3593 	if (flags == 0 && jblocks->jb_suspended) {
3594 		if (journal_unsuspend(ump))
3595 			return;
3596 		FREE_LOCK(ump);
3597 		VFS_SYNC(mp, MNT_NOWAIT);
3598 		ffs_sbupdate(ump, MNT_WAIT, 0);
3599 		ACQUIRE_LOCK(ump);
3600 	}
3601 }
3602 
3603 /*
3604  * Complete a jseg, allowing all dependencies awaiting journal writes
3605  * to proceed.  Each journal dependency also attaches a jsegdep to dependent
3606  * structures so that the journal segment can be freed to reclaim space.
3607  */
3608 static void
3609 complete_jseg(jseg)
3610 	struct jseg *jseg;
3611 {
3612 	struct worklist *wk;
3613 	struct jmvref *jmvref;
3614 #ifdef INVARIANTS
3615 	int i = 0;
3616 #endif
3617 
3618 	while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3619 		WORKLIST_REMOVE(wk);
3620 		wk->wk_state &= ~INPROGRESS;
3621 		wk->wk_state |= COMPLETE;
3622 		KASSERT(i++ < jseg->js_cnt,
3623 		    ("handle_written_jseg: overflow %d >= %d",
3624 		    i - 1, jseg->js_cnt));
3625 		switch (wk->wk_type) {
3626 		case D_JADDREF:
3627 			handle_written_jaddref(WK_JADDREF(wk));
3628 			break;
3629 		case D_JREMREF:
3630 			handle_written_jremref(WK_JREMREF(wk));
3631 			break;
3632 		case D_JMVREF:
3633 			rele_jseg(jseg);	/* No jsegdep. */
3634 			jmvref = WK_JMVREF(wk);
3635 			LIST_REMOVE(jmvref, jm_deps);
3636 			if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3637 				free_pagedep(jmvref->jm_pagedep);
3638 			WORKITEM_FREE(jmvref, D_JMVREF);
3639 			break;
3640 		case D_JNEWBLK:
3641 			handle_written_jnewblk(WK_JNEWBLK(wk));
3642 			break;
3643 		case D_JFREEBLK:
3644 			handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3645 			break;
3646 		case D_JTRUNC:
3647 			handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3648 			break;
3649 		case D_JFSYNC:
3650 			rele_jseg(jseg);	/* No jsegdep. */
3651 			WORKITEM_FREE(wk, D_JFSYNC);
3652 			break;
3653 		case D_JFREEFRAG:
3654 			handle_written_jfreefrag(WK_JFREEFRAG(wk));
3655 			break;
3656 		default:
3657 			panic("handle_written_jseg: Unknown type %s",
3658 			    TYPENAME(wk->wk_type));
3659 			/* NOTREACHED */
3660 		}
3661 	}
3662 	/* Release the self reference so the structure may be freed. */
3663 	rele_jseg(jseg);
3664 }
3665 
3666 /*
3667  * Determine which jsegs are ready for completion processing.  Waits for
3668  * synchronize cache to complete as well as forcing in-order completion
3669  * of journal entries.
3670  */
3671 static void
3672 complete_jsegs(jseg)
3673 	struct jseg *jseg;
3674 {
3675 	struct jblocks *jblocks;
3676 	struct jseg *jsegn;
3677 
3678 	jblocks = jseg->js_jblocks;
3679 	/*
3680 	 * Don't allow out of order completions.  If this isn't the first
3681 	 * block wait for it to write before we're done.
3682 	 */
3683 	if (jseg != jblocks->jb_writeseg)
3684 		return;
3685 	/* Iterate through available jsegs processing their entries. */
3686 	while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
3687 		jblocks->jb_oldestwrseq = jseg->js_oldseq;
3688 		jsegn = TAILQ_NEXT(jseg, js_next);
3689 		complete_jseg(jseg);
3690 		jseg = jsegn;
3691 	}
3692 	jblocks->jb_writeseg = jseg;
3693 	/*
3694 	 * Attempt to free jsegs now that oldestwrseq may have advanced.
3695 	 */
3696 	free_jsegs(jblocks);
3697 }
3698 
3699 /*
3700  * Mark a jseg as DEPCOMPLETE and throw away the buffer.  Attempt to handle
3701  * the final completions.
3702  */
3703 static void
3704 handle_written_jseg(jseg, bp)
3705 	struct jseg *jseg;
3706 	struct buf *bp;
3707 {
3708 
3709 	if (jseg->js_refs == 0)
3710 		panic("handle_written_jseg: No self-reference on %p", jseg);
3711 	jseg->js_state |= DEPCOMPLETE;
3712 	/*
3713 	 * We'll never need this buffer again, set flags so it will be
3714 	 * discarded.
3715 	 */
3716 	bp->b_flags |= B_INVAL | B_NOCACHE;
3717 	pbrelvp(bp);
3718 	complete_jsegs(jseg);
3719 }
3720 
3721 static inline struct jsegdep *
3722 inoref_jseg(inoref)
3723 	struct inoref *inoref;
3724 {
3725 	struct jsegdep *jsegdep;
3726 
3727 	jsegdep = inoref->if_jsegdep;
3728 	inoref->if_jsegdep = NULL;
3729 
3730 	return (jsegdep);
3731 }
3732 
3733 /*
3734  * Called once a jremref has made it to stable store.  The jremref is marked
3735  * complete and we attempt to free it.  Any pagedeps writes sleeping waiting
3736  * for the jremref to complete will be awoken by free_jremref.
3737  */
3738 static void
3739 handle_written_jremref(jremref)
3740 	struct jremref *jremref;
3741 {
3742 	struct inodedep *inodedep;
3743 	struct jsegdep *jsegdep;
3744 	struct dirrem *dirrem;
3745 
3746 	/* Grab the jsegdep. */
3747 	jsegdep = inoref_jseg(&jremref->jr_ref);
3748 	/*
3749 	 * Remove us from the inoref list.
3750 	 */
3751 	if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
3752 	    0, &inodedep) == 0)
3753 		panic("handle_written_jremref: Lost inodedep");
3754 	TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
3755 	/*
3756 	 * Complete the dirrem.
3757 	 */
3758 	dirrem = jremref->jr_dirrem;
3759 	jremref->jr_dirrem = NULL;
3760 	LIST_REMOVE(jremref, jr_deps);
3761 	jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
3762 	jwork_insert(&dirrem->dm_jwork, jsegdep);
3763 	if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
3764 	    (dirrem->dm_state & COMPLETE) != 0)
3765 		add_to_worklist(&dirrem->dm_list, 0);
3766 	free_jremref(jremref);
3767 }
3768 
3769 /*
3770  * Called once a jaddref has made it to stable store.  The dependency is
3771  * marked complete and any dependent structures are added to the inode
3772  * bufwait list to be completed as soon as it is written.  If a bitmap write
3773  * depends on this entry we move the inode into the inodedephd of the
3774  * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
3775  */
3776 static void
3777 handle_written_jaddref(jaddref)
3778 	struct jaddref *jaddref;
3779 {
3780 	struct jsegdep *jsegdep;
3781 	struct inodedep *inodedep;
3782 	struct diradd *diradd;
3783 	struct mkdir *mkdir;
3784 
3785 	/* Grab the jsegdep. */
3786 	jsegdep = inoref_jseg(&jaddref->ja_ref);
3787 	mkdir = NULL;
3788 	diradd = NULL;
3789 	if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
3790 	    0, &inodedep) == 0)
3791 		panic("handle_written_jaddref: Lost inodedep.");
3792 	if (jaddref->ja_diradd == NULL)
3793 		panic("handle_written_jaddref: No dependency");
3794 	if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
3795 		diradd = jaddref->ja_diradd;
3796 		WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
3797 	} else if (jaddref->ja_state & MKDIR_PARENT) {
3798 		mkdir = jaddref->ja_mkdir;
3799 		WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
3800 	} else if (jaddref->ja_state & MKDIR_BODY)
3801 		mkdir = jaddref->ja_mkdir;
3802 	else
3803 		panic("handle_written_jaddref: Unknown dependency %p",
3804 		    jaddref->ja_diradd);
3805 	jaddref->ja_diradd = NULL;	/* also clears ja_mkdir */
3806 	/*
3807 	 * Remove us from the inode list.
3808 	 */
3809 	TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
3810 	/*
3811 	 * The mkdir may be waiting on the jaddref to clear before freeing.
3812 	 */
3813 	if (mkdir) {
3814 		KASSERT(mkdir->md_list.wk_type == D_MKDIR,
3815 		    ("handle_written_jaddref: Incorrect type for mkdir %s",
3816 		    TYPENAME(mkdir->md_list.wk_type)));
3817 		mkdir->md_jaddref = NULL;
3818 		diradd = mkdir->md_diradd;
3819 		mkdir->md_state |= DEPCOMPLETE;
3820 		complete_mkdir(mkdir);
3821 	}
3822 	jwork_insert(&diradd->da_jwork, jsegdep);
3823 	if (jaddref->ja_state & NEWBLOCK) {
3824 		inodedep->id_state |= ONDEPLIST;
3825 		LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
3826 		    inodedep, id_deps);
3827 	}
3828 	free_jaddref(jaddref);
3829 }
3830 
3831 /*
3832  * Called once a jnewblk journal is written.  The allocdirect or allocindir
3833  * is placed in the bmsafemap to await notification of a written bitmap.  If
3834  * the operation was canceled we add the segdep to the appropriate
3835  * dependency to free the journal space once the canceling operation
3836  * completes.
3837  */
3838 static void
3839 handle_written_jnewblk(jnewblk)
3840 	struct jnewblk *jnewblk;
3841 {
3842 	struct bmsafemap *bmsafemap;
3843 	struct freefrag *freefrag;
3844 	struct freework *freework;
3845 	struct jsegdep *jsegdep;
3846 	struct newblk *newblk;
3847 
3848 	/* Grab the jsegdep. */
3849 	jsegdep = jnewblk->jn_jsegdep;
3850 	jnewblk->jn_jsegdep = NULL;
3851 	if (jnewblk->jn_dep == NULL)
3852 		panic("handle_written_jnewblk: No dependency for the segdep.");
3853 	switch (jnewblk->jn_dep->wk_type) {
3854 	case D_NEWBLK:
3855 	case D_ALLOCDIRECT:
3856 	case D_ALLOCINDIR:
3857 		/*
3858 		 * Add the written block to the bmsafemap so it can
3859 		 * be notified when the bitmap is on disk.
3860 		 */
3861 		newblk = WK_NEWBLK(jnewblk->jn_dep);
3862 		newblk->nb_jnewblk = NULL;
3863 		if ((newblk->nb_state & GOINGAWAY) == 0) {
3864 			bmsafemap = newblk->nb_bmsafemap;
3865 			newblk->nb_state |= ONDEPLIST;
3866 			LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
3867 			    nb_deps);
3868 		}
3869 		jwork_insert(&newblk->nb_jwork, jsegdep);
3870 		break;
3871 	case D_FREEFRAG:
3872 		/*
3873 		 * A newblock being removed by a freefrag when replaced by
3874 		 * frag extension.
3875 		 */
3876 		freefrag = WK_FREEFRAG(jnewblk->jn_dep);
3877 		freefrag->ff_jdep = NULL;
3878 		jwork_insert(&freefrag->ff_jwork, jsegdep);
3879 		break;
3880 	case D_FREEWORK:
3881 		/*
3882 		 * A direct block was removed by truncate.
3883 		 */
3884 		freework = WK_FREEWORK(jnewblk->jn_dep);
3885 		freework->fw_jnewblk = NULL;
3886 		jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
3887 		break;
3888 	default:
3889 		panic("handle_written_jnewblk: Unknown type %d.",
3890 		    jnewblk->jn_dep->wk_type);
3891 	}
3892 	jnewblk->jn_dep = NULL;
3893 	free_jnewblk(jnewblk);
3894 }
3895 
3896 /*
3897  * Cancel a jfreefrag that won't be needed, probably due to colliding with
3898  * an in-flight allocation that has not yet been committed.  Divorce us
3899  * from the freefrag and mark it DEPCOMPLETE so that it may be added
3900  * to the worklist.
3901  */
3902 static void
3903 cancel_jfreefrag(jfreefrag)
3904 	struct jfreefrag *jfreefrag;
3905 {
3906 	struct freefrag *freefrag;
3907 
3908 	if (jfreefrag->fr_jsegdep) {
3909 		free_jsegdep(jfreefrag->fr_jsegdep);
3910 		jfreefrag->fr_jsegdep = NULL;
3911 	}
3912 	freefrag = jfreefrag->fr_freefrag;
3913 	jfreefrag->fr_freefrag = NULL;
3914 	free_jfreefrag(jfreefrag);
3915 	freefrag->ff_state |= DEPCOMPLETE;
3916 	CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
3917 }
3918 
3919 /*
3920  * Free a jfreefrag when the parent freefrag is rendered obsolete.
3921  */
3922 static void
3923 free_jfreefrag(jfreefrag)
3924 	struct jfreefrag *jfreefrag;
3925 {
3926 
3927 	if (jfreefrag->fr_state & INPROGRESS)
3928 		WORKLIST_REMOVE(&jfreefrag->fr_list);
3929 	else if (jfreefrag->fr_state & ONWORKLIST)
3930 		remove_from_journal(&jfreefrag->fr_list);
3931 	if (jfreefrag->fr_freefrag != NULL)
3932 		panic("free_jfreefrag:  Still attached to a freefrag.");
3933 	WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
3934 }
3935 
3936 /*
3937  * Called when the journal write for a jfreefrag completes.  The parent
3938  * freefrag is added to the worklist if this completes its dependencies.
3939  */
3940 static void
3941 handle_written_jfreefrag(jfreefrag)
3942 	struct jfreefrag *jfreefrag;
3943 {
3944 	struct jsegdep *jsegdep;
3945 	struct freefrag *freefrag;
3946 
3947 	/* Grab the jsegdep. */
3948 	jsegdep = jfreefrag->fr_jsegdep;
3949 	jfreefrag->fr_jsegdep = NULL;
3950 	freefrag = jfreefrag->fr_freefrag;
3951 	if (freefrag == NULL)
3952 		panic("handle_written_jfreefrag: No freefrag.");
3953 	freefrag->ff_state |= DEPCOMPLETE;
3954 	freefrag->ff_jdep = NULL;
3955 	jwork_insert(&freefrag->ff_jwork, jsegdep);
3956 	if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
3957 		add_to_worklist(&freefrag->ff_list, 0);
3958 	jfreefrag->fr_freefrag = NULL;
3959 	free_jfreefrag(jfreefrag);
3960 }
3961 
3962 /*
3963  * Called when the journal write for a jfreeblk completes.  The jfreeblk
3964  * is removed from the freeblks list of pending journal writes and the
3965  * jsegdep is moved to the freeblks jwork to be completed when all blocks
3966  * have been reclaimed.
3967  */
3968 static void
3969 handle_written_jblkdep(jblkdep)
3970 	struct jblkdep *jblkdep;
3971 {
3972 	struct freeblks *freeblks;
3973 	struct jsegdep *jsegdep;
3974 
3975 	/* Grab the jsegdep. */
3976 	jsegdep = jblkdep->jb_jsegdep;
3977 	jblkdep->jb_jsegdep = NULL;
3978 	freeblks = jblkdep->jb_freeblks;
3979 	LIST_REMOVE(jblkdep, jb_deps);
3980 	jwork_insert(&freeblks->fb_jwork, jsegdep);
3981 	/*
3982 	 * If the freeblks is all journaled, we can add it to the worklist.
3983 	 */
3984 	if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
3985 	    (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
3986 		add_to_worklist(&freeblks->fb_list, WK_NODELAY);
3987 
3988 	free_jblkdep(jblkdep);
3989 }
3990 
3991 static struct jsegdep *
3992 newjsegdep(struct worklist *wk)
3993 {
3994 	struct jsegdep *jsegdep;
3995 
3996 	jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
3997 	workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
3998 	jsegdep->jd_seg = NULL;
3999 
4000 	return (jsegdep);
4001 }
4002 
4003 static struct jmvref *
4004 newjmvref(dp, ino, oldoff, newoff)
4005 	struct inode *dp;
4006 	ino_t ino;
4007 	off_t oldoff;
4008 	off_t newoff;
4009 {
4010 	struct jmvref *jmvref;
4011 
4012 	jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4013 	workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4014 	jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4015 	jmvref->jm_parent = dp->i_number;
4016 	jmvref->jm_ino = ino;
4017 	jmvref->jm_oldoff = oldoff;
4018 	jmvref->jm_newoff = newoff;
4019 
4020 	return (jmvref);
4021 }
4022 
4023 /*
4024  * Allocate a new jremref that tracks the removal of ip from dp with the
4025  * directory entry offset of diroff.  Mark the entry as ATTACHED and
4026  * DEPCOMPLETE as we have all the information required for the journal write
4027  * and the directory has already been removed from the buffer.  The caller
4028  * is responsible for linking the jremref into the pagedep and adding it
4029  * to the journal to write.  The MKDIR_PARENT flag is set if we're doing
4030  * a DOTDOT addition so handle_workitem_remove() can properly assign
4031  * the jsegdep when we're done.
4032  */
4033 static struct jremref *
4034 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4035     off_t diroff, nlink_t nlink)
4036 {
4037 	struct jremref *jremref;
4038 
4039 	jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4040 	workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4041 	jremref->jr_state = ATTACHED;
4042 	newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4043 	   nlink, ip->i_mode);
4044 	jremref->jr_dirrem = dirrem;
4045 
4046 	return (jremref);
4047 }
4048 
4049 static inline void
4050 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4051     nlink_t nlink, uint16_t mode)
4052 {
4053 
4054 	inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4055 	inoref->if_diroff = diroff;
4056 	inoref->if_ino = ino;
4057 	inoref->if_parent = parent;
4058 	inoref->if_nlink = nlink;
4059 	inoref->if_mode = mode;
4060 }
4061 
4062 /*
4063  * Allocate a new jaddref to track the addition of ino to dp at diroff.  The
4064  * directory offset may not be known until later.  The caller is responsible
4065  * adding the entry to the journal when this information is available.  nlink
4066  * should be the link count prior to the addition and mode is only required
4067  * to have the correct FMT.
4068  */
4069 static struct jaddref *
4070 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4071     uint16_t mode)
4072 {
4073 	struct jaddref *jaddref;
4074 
4075 	jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4076 	workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4077 	jaddref->ja_state = ATTACHED;
4078 	jaddref->ja_mkdir = NULL;
4079 	newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4080 
4081 	return (jaddref);
4082 }
4083 
4084 /*
4085  * Create a new free dependency for a freework.  The caller is responsible
4086  * for adjusting the reference count when it has the lock held.  The freedep
4087  * will track an outstanding bitmap write that will ultimately clear the
4088  * freework to continue.
4089  */
4090 static struct freedep *
4091 newfreedep(struct freework *freework)
4092 {
4093 	struct freedep *freedep;
4094 
4095 	freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4096 	workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4097 	freedep->fd_freework = freework;
4098 
4099 	return (freedep);
4100 }
4101 
4102 /*
4103  * Free a freedep structure once the buffer it is linked to is written.  If
4104  * this is the last reference to the freework schedule it for completion.
4105  */
4106 static void
4107 free_freedep(freedep)
4108 	struct freedep *freedep;
4109 {
4110 	struct freework *freework;
4111 
4112 	freework = freedep->fd_freework;
4113 	freework->fw_freeblks->fb_cgwait--;
4114 	if (--freework->fw_ref == 0)
4115 		freework_enqueue(freework);
4116 	WORKITEM_FREE(freedep, D_FREEDEP);
4117 }
4118 
4119 /*
4120  * Allocate a new freework structure that may be a level in an indirect
4121  * when parent is not NULL or a top level block when it is.  The top level
4122  * freework structures are allocated without the per-filesystem lock held
4123  * and before the freeblks is visible outside of softdep_setup_freeblocks().
4124  */
4125 static struct freework *
4126 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4127 	struct ufsmount *ump;
4128 	struct freeblks *freeblks;
4129 	struct freework *parent;
4130 	ufs_lbn_t lbn;
4131 	ufs2_daddr_t nb;
4132 	int frags;
4133 	int off;
4134 	int journal;
4135 {
4136 	struct freework *freework;
4137 
4138 	freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4139 	workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4140 	freework->fw_state = ATTACHED;
4141 	freework->fw_jnewblk = NULL;
4142 	freework->fw_freeblks = freeblks;
4143 	freework->fw_parent = parent;
4144 	freework->fw_lbn = lbn;
4145 	freework->fw_blkno = nb;
4146 	freework->fw_frags = frags;
4147 	freework->fw_indir = NULL;
4148 	freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4149 	    lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4150 	freework->fw_start = freework->fw_off = off;
4151 	if (journal)
4152 		newjfreeblk(freeblks, lbn, nb, frags);
4153 	if (parent == NULL) {
4154 		ACQUIRE_LOCK(ump);
4155 		WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4156 		freeblks->fb_ref++;
4157 		FREE_LOCK(ump);
4158 	}
4159 
4160 	return (freework);
4161 }
4162 
4163 /*
4164  * Eliminate a jfreeblk for a block that does not need journaling.
4165  */
4166 static void
4167 cancel_jfreeblk(freeblks, blkno)
4168 	struct freeblks *freeblks;
4169 	ufs2_daddr_t blkno;
4170 {
4171 	struct jfreeblk *jfreeblk;
4172 	struct jblkdep *jblkdep;
4173 
4174 	LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4175 		if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4176 			continue;
4177 		jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4178 		if (jfreeblk->jf_blkno == blkno)
4179 			break;
4180 	}
4181 	if (jblkdep == NULL)
4182 		return;
4183 	CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4184 	free_jsegdep(jblkdep->jb_jsegdep);
4185 	LIST_REMOVE(jblkdep, jb_deps);
4186 	WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4187 }
4188 
4189 /*
4190  * Allocate a new jfreeblk to journal top level block pointer when truncating
4191  * a file.  The caller must add this to the worklist when the per-filesystem
4192  * lock is held.
4193  */
4194 static struct jfreeblk *
4195 newjfreeblk(freeblks, lbn, blkno, frags)
4196 	struct freeblks *freeblks;
4197 	ufs_lbn_t lbn;
4198 	ufs2_daddr_t blkno;
4199 	int frags;
4200 {
4201 	struct jfreeblk *jfreeblk;
4202 
4203 	jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4204 	workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4205 	    freeblks->fb_list.wk_mp);
4206 	jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4207 	jfreeblk->jf_dep.jb_freeblks = freeblks;
4208 	jfreeblk->jf_ino = freeblks->fb_inum;
4209 	jfreeblk->jf_lbn = lbn;
4210 	jfreeblk->jf_blkno = blkno;
4211 	jfreeblk->jf_frags = frags;
4212 	LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4213 
4214 	return (jfreeblk);
4215 }
4216 
4217 /*
4218  * The journal is only prepared to handle full-size block numbers, so we
4219  * have to adjust the record to reflect the change to a full-size block.
4220  * For example, suppose we have a block made up of fragments 8-15 and
4221  * want to free its last two fragments. We are given a request that says:
4222  *     FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4223  * where frags are the number of fragments to free and oldfrags are the
4224  * number of fragments to keep. To block align it, we have to change it to
4225  * have a valid full-size blkno, so it becomes:
4226  *     FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4227  */
4228 static void
4229 adjust_newfreework(freeblks, frag_offset)
4230 	struct freeblks *freeblks;
4231 	int frag_offset;
4232 {
4233 	struct jfreeblk *jfreeblk;
4234 
4235 	KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4236 	    LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4237 	    ("adjust_newfreework: Missing freeblks dependency"));
4238 
4239 	jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4240 	jfreeblk->jf_blkno -= frag_offset;
4241 	jfreeblk->jf_frags += frag_offset;
4242 }
4243 
4244 /*
4245  * Allocate a new jtrunc to track a partial truncation.
4246  */
4247 static struct jtrunc *
4248 newjtrunc(freeblks, size, extsize)
4249 	struct freeblks *freeblks;
4250 	off_t size;
4251 	int extsize;
4252 {
4253 	struct jtrunc *jtrunc;
4254 
4255 	jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4256 	workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4257 	    freeblks->fb_list.wk_mp);
4258 	jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4259 	jtrunc->jt_dep.jb_freeblks = freeblks;
4260 	jtrunc->jt_ino = freeblks->fb_inum;
4261 	jtrunc->jt_size = size;
4262 	jtrunc->jt_extsize = extsize;
4263 	LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4264 
4265 	return (jtrunc);
4266 }
4267 
4268 /*
4269  * If we're canceling a new bitmap we have to search for another ref
4270  * to move into the bmsafemap dep.  This might be better expressed
4271  * with another structure.
4272  */
4273 static void
4274 move_newblock_dep(jaddref, inodedep)
4275 	struct jaddref *jaddref;
4276 	struct inodedep *inodedep;
4277 {
4278 	struct inoref *inoref;
4279 	struct jaddref *jaddrefn;
4280 
4281 	jaddrefn = NULL;
4282 	for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4283 	    inoref = TAILQ_NEXT(inoref, if_deps)) {
4284 		if ((jaddref->ja_state & NEWBLOCK) &&
4285 		    inoref->if_list.wk_type == D_JADDREF) {
4286 			jaddrefn = (struct jaddref *)inoref;
4287 			break;
4288 		}
4289 	}
4290 	if (jaddrefn == NULL)
4291 		return;
4292 	jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4293 	jaddrefn->ja_state |= jaddref->ja_state &
4294 	    (ATTACHED | UNDONE | NEWBLOCK);
4295 	jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4296 	jaddref->ja_state |= ATTACHED;
4297 	LIST_REMOVE(jaddref, ja_bmdeps);
4298 	LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4299 	    ja_bmdeps);
4300 }
4301 
4302 /*
4303  * Cancel a jaddref either before it has been written or while it is being
4304  * written.  This happens when a link is removed before the add reaches
4305  * the disk.  The jaddref dependency is kept linked into the bmsafemap
4306  * and inode to prevent the link count or bitmap from reaching the disk
4307  * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4308  * required.
4309  *
4310  * Returns 1 if the canceled addref requires journaling of the remove and
4311  * 0 otherwise.
4312  */
4313 static int
4314 cancel_jaddref(jaddref, inodedep, wkhd)
4315 	struct jaddref *jaddref;
4316 	struct inodedep *inodedep;
4317 	struct workhead *wkhd;
4318 {
4319 	struct inoref *inoref;
4320 	struct jsegdep *jsegdep;
4321 	int needsj;
4322 
4323 	KASSERT((jaddref->ja_state & COMPLETE) == 0,
4324 	    ("cancel_jaddref: Canceling complete jaddref"));
4325 	if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4326 		needsj = 1;
4327 	else
4328 		needsj = 0;
4329 	if (inodedep == NULL)
4330 		if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4331 		    0, &inodedep) == 0)
4332 			panic("cancel_jaddref: Lost inodedep");
4333 	/*
4334 	 * We must adjust the nlink of any reference operation that follows
4335 	 * us so that it is consistent with the in-memory reference.  This
4336 	 * ensures that inode nlink rollbacks always have the correct link.
4337 	 */
4338 	if (needsj == 0) {
4339 		for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4340 		    inoref = TAILQ_NEXT(inoref, if_deps)) {
4341 			if (inoref->if_state & GOINGAWAY)
4342 				break;
4343 			inoref->if_nlink--;
4344 		}
4345 	}
4346 	jsegdep = inoref_jseg(&jaddref->ja_ref);
4347 	if (jaddref->ja_state & NEWBLOCK)
4348 		move_newblock_dep(jaddref, inodedep);
4349 	wake_worklist(&jaddref->ja_list);
4350 	jaddref->ja_mkdir = NULL;
4351 	if (jaddref->ja_state & INPROGRESS) {
4352 		jaddref->ja_state &= ~INPROGRESS;
4353 		WORKLIST_REMOVE(&jaddref->ja_list);
4354 		jwork_insert(wkhd, jsegdep);
4355 	} else {
4356 		free_jsegdep(jsegdep);
4357 		if (jaddref->ja_state & DEPCOMPLETE)
4358 			remove_from_journal(&jaddref->ja_list);
4359 	}
4360 	jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4361 	/*
4362 	 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4363 	 * can arrange for them to be freed with the bitmap.  Otherwise we
4364 	 * no longer need this addref attached to the inoreflst and it
4365 	 * will incorrectly adjust nlink if we leave it.
4366 	 */
4367 	if ((jaddref->ja_state & NEWBLOCK) == 0) {
4368 		TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4369 		    if_deps);
4370 		jaddref->ja_state |= COMPLETE;
4371 		free_jaddref(jaddref);
4372 		return (needsj);
4373 	}
4374 	/*
4375 	 * Leave the head of the list for jsegdeps for fast merging.
4376 	 */
4377 	if (LIST_FIRST(wkhd) != NULL) {
4378 		jaddref->ja_state |= ONWORKLIST;
4379 		LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4380 	} else
4381 		WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4382 
4383 	return (needsj);
4384 }
4385 
4386 /*
4387  * Attempt to free a jaddref structure when some work completes.  This
4388  * should only succeed once the entry is written and all dependencies have
4389  * been notified.
4390  */
4391 static void
4392 free_jaddref(jaddref)
4393 	struct jaddref *jaddref;
4394 {
4395 
4396 	if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4397 		return;
4398 	if (jaddref->ja_ref.if_jsegdep)
4399 		panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4400 		    jaddref, jaddref->ja_state);
4401 	if (jaddref->ja_state & NEWBLOCK)
4402 		LIST_REMOVE(jaddref, ja_bmdeps);
4403 	if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4404 		panic("free_jaddref: Bad state %p(0x%X)",
4405 		    jaddref, jaddref->ja_state);
4406 	if (jaddref->ja_mkdir != NULL)
4407 		panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4408 	WORKITEM_FREE(jaddref, D_JADDREF);
4409 }
4410 
4411 /*
4412  * Free a jremref structure once it has been written or discarded.
4413  */
4414 static void
4415 free_jremref(jremref)
4416 	struct jremref *jremref;
4417 {
4418 
4419 	if (jremref->jr_ref.if_jsegdep)
4420 		free_jsegdep(jremref->jr_ref.if_jsegdep);
4421 	if (jremref->jr_state & INPROGRESS)
4422 		panic("free_jremref: IO still pending");
4423 	WORKITEM_FREE(jremref, D_JREMREF);
4424 }
4425 
4426 /*
4427  * Free a jnewblk structure.
4428  */
4429 static void
4430 free_jnewblk(jnewblk)
4431 	struct jnewblk *jnewblk;
4432 {
4433 
4434 	if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4435 		return;
4436 	LIST_REMOVE(jnewblk, jn_deps);
4437 	if (jnewblk->jn_dep != NULL)
4438 		panic("free_jnewblk: Dependency still attached.");
4439 	WORKITEM_FREE(jnewblk, D_JNEWBLK);
4440 }
4441 
4442 /*
4443  * Cancel a jnewblk which has been been made redundant by frag extension.
4444  */
4445 static void
4446 cancel_jnewblk(jnewblk, wkhd)
4447 	struct jnewblk *jnewblk;
4448 	struct workhead *wkhd;
4449 {
4450 	struct jsegdep *jsegdep;
4451 
4452 	CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4453 	jsegdep = jnewblk->jn_jsegdep;
4454 	if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4455 		panic("cancel_jnewblk: Invalid state");
4456 	jnewblk->jn_jsegdep  = NULL;
4457 	jnewblk->jn_dep = NULL;
4458 	jnewblk->jn_state |= GOINGAWAY;
4459 	if (jnewblk->jn_state & INPROGRESS) {
4460 		jnewblk->jn_state &= ~INPROGRESS;
4461 		WORKLIST_REMOVE(&jnewblk->jn_list);
4462 		jwork_insert(wkhd, jsegdep);
4463 	} else {
4464 		free_jsegdep(jsegdep);
4465 		remove_from_journal(&jnewblk->jn_list);
4466 	}
4467 	wake_worklist(&jnewblk->jn_list);
4468 	WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4469 }
4470 
4471 static void
4472 free_jblkdep(jblkdep)
4473 	struct jblkdep *jblkdep;
4474 {
4475 
4476 	if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4477 		WORKITEM_FREE(jblkdep, D_JFREEBLK);
4478 	else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4479 		WORKITEM_FREE(jblkdep, D_JTRUNC);
4480 	else
4481 		panic("free_jblkdep: Unexpected type %s",
4482 		    TYPENAME(jblkdep->jb_list.wk_type));
4483 }
4484 
4485 /*
4486  * Free a single jseg once it is no longer referenced in memory or on
4487  * disk.  Reclaim journal blocks and dependencies waiting for the segment
4488  * to disappear.
4489  */
4490 static void
4491 free_jseg(jseg, jblocks)
4492 	struct jseg *jseg;
4493 	struct jblocks *jblocks;
4494 {
4495 	struct freework *freework;
4496 
4497 	/*
4498 	 * Free freework structures that were lingering to indicate freed
4499 	 * indirect blocks that forced journal write ordering on reallocate.
4500 	 */
4501 	while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4502 		indirblk_remove(freework);
4503 	if (jblocks->jb_oldestseg == jseg)
4504 		jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4505 	TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4506 	jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4507 	KASSERT(LIST_EMPTY(&jseg->js_entries),
4508 	    ("free_jseg: Freed jseg has valid entries."));
4509 	WORKITEM_FREE(jseg, D_JSEG);
4510 }
4511 
4512 /*
4513  * Free all jsegs that meet the criteria for being reclaimed and update
4514  * oldestseg.
4515  */
4516 static void
4517 free_jsegs(jblocks)
4518 	struct jblocks *jblocks;
4519 {
4520 	struct jseg *jseg;
4521 
4522 	/*
4523 	 * Free only those jsegs which have none allocated before them to
4524 	 * preserve the journal space ordering.
4525 	 */
4526 	while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4527 		/*
4528 		 * Only reclaim space when nothing depends on this journal
4529 		 * set and another set has written that it is no longer
4530 		 * valid.
4531 		 */
4532 		if (jseg->js_refs != 0) {
4533 			jblocks->jb_oldestseg = jseg;
4534 			return;
4535 		}
4536 		if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4537 			break;
4538 		if (jseg->js_seq > jblocks->jb_oldestwrseq)
4539 			break;
4540 		/*
4541 		 * We can free jsegs that didn't write entries when
4542 		 * oldestwrseq == js_seq.
4543 		 */
4544 		if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4545 		    jseg->js_cnt != 0)
4546 			break;
4547 		free_jseg(jseg, jblocks);
4548 	}
4549 	/*
4550 	 * If we exited the loop above we still must discover the
4551 	 * oldest valid segment.
4552 	 */
4553 	if (jseg)
4554 		for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4555 		     jseg = TAILQ_NEXT(jseg, js_next))
4556 			if (jseg->js_refs != 0)
4557 				break;
4558 	jblocks->jb_oldestseg = jseg;
4559 	/*
4560 	 * The journal has no valid records but some jsegs may still be
4561 	 * waiting on oldestwrseq to advance.  We force a small record
4562 	 * out to permit these lingering records to be reclaimed.
4563 	 */
4564 	if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4565 		jblocks->jb_needseg = 1;
4566 }
4567 
4568 /*
4569  * Release one reference to a jseg and free it if the count reaches 0.  This
4570  * should eventually reclaim journal space as well.
4571  */
4572 static void
4573 rele_jseg(jseg)
4574 	struct jseg *jseg;
4575 {
4576 
4577 	KASSERT(jseg->js_refs > 0,
4578 	    ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4579 	if (--jseg->js_refs != 0)
4580 		return;
4581 	free_jsegs(jseg->js_jblocks);
4582 }
4583 
4584 /*
4585  * Release a jsegdep and decrement the jseg count.
4586  */
4587 static void
4588 free_jsegdep(jsegdep)
4589 	struct jsegdep *jsegdep;
4590 {
4591 
4592 	if (jsegdep->jd_seg)
4593 		rele_jseg(jsegdep->jd_seg);
4594 	WORKITEM_FREE(jsegdep, D_JSEGDEP);
4595 }
4596 
4597 /*
4598  * Wait for a journal item to make it to disk.  Initiate journal processing
4599  * if required.
4600  */
4601 static int
4602 jwait(wk, waitfor)
4603 	struct worklist *wk;
4604 	int waitfor;
4605 {
4606 
4607 	LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4608 	/*
4609 	 * Blocking journal waits cause slow synchronous behavior.  Record
4610 	 * stats on the frequency of these blocking operations.
4611 	 */
4612 	if (waitfor == MNT_WAIT) {
4613 		stat_journal_wait++;
4614 		switch (wk->wk_type) {
4615 		case D_JREMREF:
4616 		case D_JMVREF:
4617 			stat_jwait_filepage++;
4618 			break;
4619 		case D_JTRUNC:
4620 		case D_JFREEBLK:
4621 			stat_jwait_freeblks++;
4622 			break;
4623 		case D_JNEWBLK:
4624 			stat_jwait_newblk++;
4625 			break;
4626 		case D_JADDREF:
4627 			stat_jwait_inode++;
4628 			break;
4629 		default:
4630 			break;
4631 		}
4632 	}
4633 	/*
4634 	 * If IO has not started we process the journal.  We can't mark the
4635 	 * worklist item as IOWAITING because we drop the lock while
4636 	 * processing the journal and the worklist entry may be freed after
4637 	 * this point.  The caller may call back in and re-issue the request.
4638 	 */
4639 	if ((wk->wk_state & INPROGRESS) == 0) {
4640 		softdep_process_journal(wk->wk_mp, wk, waitfor);
4641 		if (waitfor != MNT_WAIT)
4642 			return (EBUSY);
4643 		return (0);
4644 	}
4645 	if (waitfor != MNT_WAIT)
4646 		return (EBUSY);
4647 	wait_worklist(wk, "jwait");
4648 	return (0);
4649 }
4650 
4651 /*
4652  * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4653  * appropriate.  This is a convenience function to reduce duplicate code
4654  * for the setup and revert functions below.
4655  */
4656 static struct inodedep *
4657 inodedep_lookup_ip(ip)
4658 	struct inode *ip;
4659 {
4660 	struct inodedep *inodedep;
4661 
4662 	KASSERT(ip->i_nlink >= ip->i_effnlink,
4663 	    ("inodedep_lookup_ip: bad delta"));
4664 	(void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
4665 	    &inodedep);
4666 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4667 	KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
4668 
4669 	return (inodedep);
4670 }
4671 
4672 /*
4673  * Called prior to creating a new inode and linking it to a directory.  The
4674  * jaddref structure must already be allocated by softdep_setup_inomapdep
4675  * and it is discovered here so we can initialize the mode and update
4676  * nlinkdelta.
4677  */
4678 void
4679 softdep_setup_create(dp, ip)
4680 	struct inode *dp;
4681 	struct inode *ip;
4682 {
4683 	struct inodedep *inodedep;
4684 	struct jaddref *jaddref;
4685 	struct vnode *dvp;
4686 
4687 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4688 	    ("softdep_setup_create called on non-softdep filesystem"));
4689 	KASSERT(ip->i_nlink == 1,
4690 	    ("softdep_setup_create: Invalid link count."));
4691 	dvp = ITOV(dp);
4692 	ACQUIRE_LOCK(ITOUMP(dp));
4693 	inodedep = inodedep_lookup_ip(ip);
4694 	if (DOINGSUJ(dvp)) {
4695 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4696 		    inoreflst);
4697 		KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4698 		    ("softdep_setup_create: No addref structure present."));
4699 	}
4700 	softdep_prelink(dvp, NULL);
4701 	FREE_LOCK(ITOUMP(dp));
4702 }
4703 
4704 /*
4705  * Create a jaddref structure to track the addition of a DOTDOT link when
4706  * we are reparenting an inode as part of a rename.  This jaddref will be
4707  * found by softdep_setup_directory_change.  Adjusts nlinkdelta for
4708  * non-journaling softdep.
4709  */
4710 void
4711 softdep_setup_dotdot_link(dp, ip)
4712 	struct inode *dp;
4713 	struct inode *ip;
4714 {
4715 	struct inodedep *inodedep;
4716 	struct jaddref *jaddref;
4717 	struct vnode *dvp;
4718 
4719 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4720 	    ("softdep_setup_dotdot_link called on non-softdep filesystem"));
4721 	dvp = ITOV(dp);
4722 	jaddref = NULL;
4723 	/*
4724 	 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
4725 	 * is used as a normal link would be.
4726 	 */
4727 	if (DOINGSUJ(dvp))
4728 		jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4729 		    dp->i_effnlink - 1, dp->i_mode);
4730 	ACQUIRE_LOCK(ITOUMP(dp));
4731 	inodedep = inodedep_lookup_ip(dp);
4732 	if (jaddref)
4733 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4734 		    if_deps);
4735 	softdep_prelink(dvp, ITOV(ip));
4736 	FREE_LOCK(ITOUMP(dp));
4737 }
4738 
4739 /*
4740  * Create a jaddref structure to track a new link to an inode.  The directory
4741  * offset is not known until softdep_setup_directory_add or
4742  * softdep_setup_directory_change.  Adjusts nlinkdelta for non-journaling
4743  * softdep.
4744  */
4745 void
4746 softdep_setup_link(dp, ip)
4747 	struct inode *dp;
4748 	struct inode *ip;
4749 {
4750 	struct inodedep *inodedep;
4751 	struct jaddref *jaddref;
4752 	struct vnode *dvp;
4753 
4754 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4755 	    ("softdep_setup_link called on non-softdep filesystem"));
4756 	dvp = ITOV(dp);
4757 	jaddref = NULL;
4758 	if (DOINGSUJ(dvp))
4759 		jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
4760 		    ip->i_mode);
4761 	ACQUIRE_LOCK(ITOUMP(dp));
4762 	inodedep = inodedep_lookup_ip(ip);
4763 	if (jaddref)
4764 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4765 		    if_deps);
4766 	softdep_prelink(dvp, ITOV(ip));
4767 	FREE_LOCK(ITOUMP(dp));
4768 }
4769 
4770 /*
4771  * Called to create the jaddref structures to track . and .. references as
4772  * well as lookup and further initialize the incomplete jaddref created
4773  * by softdep_setup_inomapdep when the inode was allocated.  Adjusts
4774  * nlinkdelta for non-journaling softdep.
4775  */
4776 void
4777 softdep_setup_mkdir(dp, ip)
4778 	struct inode *dp;
4779 	struct inode *ip;
4780 {
4781 	struct inodedep *inodedep;
4782 	struct jaddref *dotdotaddref;
4783 	struct jaddref *dotaddref;
4784 	struct jaddref *jaddref;
4785 	struct vnode *dvp;
4786 
4787 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4788 	    ("softdep_setup_mkdir called on non-softdep filesystem"));
4789 	dvp = ITOV(dp);
4790 	dotaddref = dotdotaddref = NULL;
4791 	if (DOINGSUJ(dvp)) {
4792 		dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
4793 		    ip->i_mode);
4794 		dotaddref->ja_state |= MKDIR_BODY;
4795 		dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4796 		    dp->i_effnlink - 1, dp->i_mode);
4797 		dotdotaddref->ja_state |= MKDIR_PARENT;
4798 	}
4799 	ACQUIRE_LOCK(ITOUMP(dp));
4800 	inodedep = inodedep_lookup_ip(ip);
4801 	if (DOINGSUJ(dvp)) {
4802 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4803 		    inoreflst);
4804 		KASSERT(jaddref != NULL,
4805 		    ("softdep_setup_mkdir: No addref structure present."));
4806 		KASSERT(jaddref->ja_parent == dp->i_number,
4807 		    ("softdep_setup_mkdir: bad parent %ju",
4808 		    (uintmax_t)jaddref->ja_parent));
4809 		TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
4810 		    if_deps);
4811 	}
4812 	inodedep = inodedep_lookup_ip(dp);
4813 	if (DOINGSUJ(dvp))
4814 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
4815 		    &dotdotaddref->ja_ref, if_deps);
4816 	softdep_prelink(ITOV(dp), NULL);
4817 	FREE_LOCK(ITOUMP(dp));
4818 }
4819 
4820 /*
4821  * Called to track nlinkdelta of the inode and parent directories prior to
4822  * unlinking a directory.
4823  */
4824 void
4825 softdep_setup_rmdir(dp, ip)
4826 	struct inode *dp;
4827 	struct inode *ip;
4828 {
4829 	struct vnode *dvp;
4830 
4831 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4832 	    ("softdep_setup_rmdir called on non-softdep filesystem"));
4833 	dvp = ITOV(dp);
4834 	ACQUIRE_LOCK(ITOUMP(dp));
4835 	(void) inodedep_lookup_ip(ip);
4836 	(void) inodedep_lookup_ip(dp);
4837 	softdep_prelink(dvp, ITOV(ip));
4838 	FREE_LOCK(ITOUMP(dp));
4839 }
4840 
4841 /*
4842  * Called to track nlinkdelta of the inode and parent directories prior to
4843  * unlink.
4844  */
4845 void
4846 softdep_setup_unlink(dp, ip)
4847 	struct inode *dp;
4848 	struct inode *ip;
4849 {
4850 	struct vnode *dvp;
4851 
4852 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4853 	    ("softdep_setup_unlink called on non-softdep filesystem"));
4854 	dvp = ITOV(dp);
4855 	ACQUIRE_LOCK(ITOUMP(dp));
4856 	(void) inodedep_lookup_ip(ip);
4857 	(void) inodedep_lookup_ip(dp);
4858 	softdep_prelink(dvp, ITOV(ip));
4859 	FREE_LOCK(ITOUMP(dp));
4860 }
4861 
4862 /*
4863  * Called to release the journal structures created by a failed non-directory
4864  * creation.  Adjusts nlinkdelta for non-journaling softdep.
4865  */
4866 void
4867 softdep_revert_create(dp, ip)
4868 	struct inode *dp;
4869 	struct inode *ip;
4870 {
4871 	struct inodedep *inodedep;
4872 	struct jaddref *jaddref;
4873 	struct vnode *dvp;
4874 
4875 	KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
4876 	    ("softdep_revert_create called on non-softdep filesystem"));
4877 	dvp = ITOV(dp);
4878 	ACQUIRE_LOCK(ITOUMP(dp));
4879 	inodedep = inodedep_lookup_ip(ip);
4880 	if (DOINGSUJ(dvp)) {
4881 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4882 		    inoreflst);
4883 		KASSERT(jaddref->ja_parent == dp->i_number,
4884 		    ("softdep_revert_create: addref parent mismatch"));
4885 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4886 	}
4887 	FREE_LOCK(ITOUMP(dp));
4888 }
4889 
4890 /*
4891  * Called to release the journal structures created by a failed link
4892  * addition.  Adjusts nlinkdelta for non-journaling softdep.
4893  */
4894 void
4895 softdep_revert_link(dp, ip)
4896 	struct inode *dp;
4897 	struct inode *ip;
4898 {
4899 	struct inodedep *inodedep;
4900 	struct jaddref *jaddref;
4901 	struct vnode *dvp;
4902 
4903 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4904 	    ("softdep_revert_link called on non-softdep filesystem"));
4905 	dvp = ITOV(dp);
4906 	ACQUIRE_LOCK(ITOUMP(dp));
4907 	inodedep = inodedep_lookup_ip(ip);
4908 	if (DOINGSUJ(dvp)) {
4909 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4910 		    inoreflst);
4911 		KASSERT(jaddref->ja_parent == dp->i_number,
4912 		    ("softdep_revert_link: addref parent mismatch"));
4913 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4914 	}
4915 	FREE_LOCK(ITOUMP(dp));
4916 }
4917 
4918 /*
4919  * Called to release the journal structures created by a failed mkdir
4920  * attempt.  Adjusts nlinkdelta for non-journaling softdep.
4921  */
4922 void
4923 softdep_revert_mkdir(dp, ip)
4924 	struct inode *dp;
4925 	struct inode *ip;
4926 {
4927 	struct inodedep *inodedep;
4928 	struct jaddref *jaddref;
4929 	struct jaddref *dotaddref;
4930 	struct vnode *dvp;
4931 
4932 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4933 	    ("softdep_revert_mkdir called on non-softdep filesystem"));
4934 	dvp = ITOV(dp);
4935 
4936 	ACQUIRE_LOCK(ITOUMP(dp));
4937 	inodedep = inodedep_lookup_ip(dp);
4938 	if (DOINGSUJ(dvp)) {
4939 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4940 		    inoreflst);
4941 		KASSERT(jaddref->ja_parent == ip->i_number,
4942 		    ("softdep_revert_mkdir: dotdot addref parent mismatch"));
4943 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4944 	}
4945 	inodedep = inodedep_lookup_ip(ip);
4946 	if (DOINGSUJ(dvp)) {
4947 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4948 		    inoreflst);
4949 		KASSERT(jaddref->ja_parent == dp->i_number,
4950 		    ("softdep_revert_mkdir: addref parent mismatch"));
4951 		dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
4952 		    inoreflst, if_deps);
4953 		cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4954 		KASSERT(dotaddref->ja_parent == ip->i_number,
4955 		    ("softdep_revert_mkdir: dot addref parent mismatch"));
4956 		cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
4957 	}
4958 	FREE_LOCK(ITOUMP(dp));
4959 }
4960 
4961 /*
4962  * Called to correct nlinkdelta after a failed rmdir.
4963  */
4964 void
4965 softdep_revert_rmdir(dp, ip)
4966 	struct inode *dp;
4967 	struct inode *ip;
4968 {
4969 
4970 	KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4971 	    ("softdep_revert_rmdir called on non-softdep filesystem"));
4972 	ACQUIRE_LOCK(ITOUMP(dp));
4973 	(void) inodedep_lookup_ip(ip);
4974 	(void) inodedep_lookup_ip(dp);
4975 	FREE_LOCK(ITOUMP(dp));
4976 }
4977 
4978 /*
4979  * Protecting the freemaps (or bitmaps).
4980  *
4981  * To eliminate the need to execute fsck before mounting a filesystem
4982  * after a power failure, one must (conservatively) guarantee that the
4983  * on-disk copy of the bitmaps never indicate that a live inode or block is
4984  * free.  So, when a block or inode is allocated, the bitmap should be
4985  * updated (on disk) before any new pointers.  When a block or inode is
4986  * freed, the bitmap should not be updated until all pointers have been
4987  * reset.  The latter dependency is handled by the delayed de-allocation
4988  * approach described below for block and inode de-allocation.  The former
4989  * dependency is handled by calling the following procedure when a block or
4990  * inode is allocated. When an inode is allocated an "inodedep" is created
4991  * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
4992  * Each "inodedep" is also inserted into the hash indexing structure so
4993  * that any additional link additions can be made dependent on the inode
4994  * allocation.
4995  *
4996  * The ufs filesystem maintains a number of free block counts (e.g., per
4997  * cylinder group, per cylinder and per <cylinder, rotational position> pair)
4998  * in addition to the bitmaps.  These counts are used to improve efficiency
4999  * during allocation and therefore must be consistent with the bitmaps.
5000  * There is no convenient way to guarantee post-crash consistency of these
5001  * counts with simple update ordering, for two main reasons: (1) The counts
5002  * and bitmaps for a single cylinder group block are not in the same disk
5003  * sector.  If a disk write is interrupted (e.g., by power failure), one may
5004  * be written and the other not.  (2) Some of the counts are located in the
5005  * superblock rather than the cylinder group block. So, we focus our soft
5006  * updates implementation on protecting the bitmaps. When mounting a
5007  * filesystem, we recompute the auxiliary counts from the bitmaps.
5008  */
5009 
5010 /*
5011  * Called just after updating the cylinder group block to allocate an inode.
5012  */
5013 void
5014 softdep_setup_inomapdep(bp, ip, newinum, mode)
5015 	struct buf *bp;		/* buffer for cylgroup block with inode map */
5016 	struct inode *ip;	/* inode related to allocation */
5017 	ino_t newinum;		/* new inode number being allocated */
5018 	int mode;
5019 {
5020 	struct inodedep *inodedep;
5021 	struct bmsafemap *bmsafemap;
5022 	struct jaddref *jaddref;
5023 	struct mount *mp;
5024 	struct fs *fs;
5025 
5026 	mp = ITOVFS(ip);
5027 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5028 	    ("softdep_setup_inomapdep called on non-softdep filesystem"));
5029 	fs = VFSTOUFS(mp)->um_fs;
5030 	jaddref = NULL;
5031 
5032 	/*
5033 	 * Allocate the journal reference add structure so that the bitmap
5034 	 * can be dependent on it.
5035 	 */
5036 	if (MOUNTEDSUJ(mp)) {
5037 		jaddref = newjaddref(ip, newinum, 0, 0, mode);
5038 		jaddref->ja_state |= NEWBLOCK;
5039 	}
5040 
5041 	/*
5042 	 * Create a dependency for the newly allocated inode.
5043 	 * Panic if it already exists as something is seriously wrong.
5044 	 * Otherwise add it to the dependency list for the buffer holding
5045 	 * the cylinder group map from which it was allocated.
5046 	 *
5047 	 * We have to preallocate a bmsafemap entry in case it is needed
5048 	 * in bmsafemap_lookup since once we allocate the inodedep, we
5049 	 * have to finish initializing it before we can FREE_LOCK().
5050 	 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5051 	 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5052 	 * creating the inodedep as it can be freed during the time
5053 	 * that we FREE_LOCK() while allocating the inodedep. We must
5054 	 * call workitem_alloc() before entering the locked section as
5055 	 * it also acquires the lock and we must avoid trying doing so
5056 	 * recursively.
5057 	 */
5058 	bmsafemap = malloc(sizeof(struct bmsafemap),
5059 	    M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5060 	workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5061 	ACQUIRE_LOCK(ITOUMP(ip));
5062 	if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5063 		panic("softdep_setup_inomapdep: dependency %p for new"
5064 		    "inode already exists", inodedep);
5065 	bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5066 	if (jaddref) {
5067 		LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5068 		TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5069 		    if_deps);
5070 	} else {
5071 		inodedep->id_state |= ONDEPLIST;
5072 		LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5073 	}
5074 	inodedep->id_bmsafemap = bmsafemap;
5075 	inodedep->id_state &= ~DEPCOMPLETE;
5076 	FREE_LOCK(ITOUMP(ip));
5077 }
5078 
5079 /*
5080  * Called just after updating the cylinder group block to
5081  * allocate block or fragment.
5082  */
5083 void
5084 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5085 	struct buf *bp;		/* buffer for cylgroup block with block map */
5086 	struct mount *mp;	/* filesystem doing allocation */
5087 	ufs2_daddr_t newblkno;	/* number of newly allocated block */
5088 	int frags;		/* Number of fragments. */
5089 	int oldfrags;		/* Previous number of fragments for extend. */
5090 {
5091 	struct newblk *newblk;
5092 	struct bmsafemap *bmsafemap;
5093 	struct jnewblk *jnewblk;
5094 	struct ufsmount *ump;
5095 	struct fs *fs;
5096 
5097 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5098 	    ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5099 	ump = VFSTOUFS(mp);
5100 	fs = ump->um_fs;
5101 	jnewblk = NULL;
5102 	/*
5103 	 * Create a dependency for the newly allocated block.
5104 	 * Add it to the dependency list for the buffer holding
5105 	 * the cylinder group map from which it was allocated.
5106 	 */
5107 	if (MOUNTEDSUJ(mp)) {
5108 		jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5109 		workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5110 		jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5111 		jnewblk->jn_state = ATTACHED;
5112 		jnewblk->jn_blkno = newblkno;
5113 		jnewblk->jn_frags = frags;
5114 		jnewblk->jn_oldfrags = oldfrags;
5115 #ifdef INVARIANTS
5116 		{
5117 			struct cg *cgp;
5118 			uint8_t *blksfree;
5119 			long bno;
5120 			int i;
5121 
5122 			cgp = (struct cg *)bp->b_data;
5123 			blksfree = cg_blksfree(cgp);
5124 			bno = dtogd(fs, jnewblk->jn_blkno);
5125 			for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5126 			    i++) {
5127 				if (isset(blksfree, bno + i))
5128 					panic("softdep_setup_blkmapdep: "
5129 					    "free fragment %d from %d-%d "
5130 					    "state 0x%X dep %p", i,
5131 					    jnewblk->jn_oldfrags,
5132 					    jnewblk->jn_frags,
5133 					    jnewblk->jn_state,
5134 					    jnewblk->jn_dep);
5135 			}
5136 		}
5137 #endif
5138 	}
5139 
5140 	CTR3(KTR_SUJ,
5141 	    "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5142 	    newblkno, frags, oldfrags);
5143 	ACQUIRE_LOCK(ump);
5144 	if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5145 		panic("softdep_setup_blkmapdep: found block");
5146 	newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5147 	    dtog(fs, newblkno), NULL);
5148 	if (jnewblk) {
5149 		jnewblk->jn_dep = (struct worklist *)newblk;
5150 		LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5151 	} else {
5152 		newblk->nb_state |= ONDEPLIST;
5153 		LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5154 	}
5155 	newblk->nb_bmsafemap = bmsafemap;
5156 	newblk->nb_jnewblk = jnewblk;
5157 	FREE_LOCK(ump);
5158 }
5159 
5160 #define	BMSAFEMAP_HASH(ump, cg) \
5161       (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5162 
5163 static int
5164 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5165 	struct bmsafemap_hashhead *bmsafemaphd;
5166 	int cg;
5167 	struct bmsafemap **bmsafemapp;
5168 {
5169 	struct bmsafemap *bmsafemap;
5170 
5171 	LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5172 		if (bmsafemap->sm_cg == cg)
5173 			break;
5174 	if (bmsafemap) {
5175 		*bmsafemapp = bmsafemap;
5176 		return (1);
5177 	}
5178 	*bmsafemapp = NULL;
5179 
5180 	return (0);
5181 }
5182 
5183 /*
5184  * Find the bmsafemap associated with a cylinder group buffer.
5185  * If none exists, create one. The buffer must be locked when
5186  * this routine is called and this routine must be called with
5187  * the softdep lock held. To avoid giving up the lock while
5188  * allocating a new bmsafemap, a preallocated bmsafemap may be
5189  * provided. If it is provided but not needed, it is freed.
5190  */
5191 static struct bmsafemap *
5192 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5193 	struct mount *mp;
5194 	struct buf *bp;
5195 	int cg;
5196 	struct bmsafemap *newbmsafemap;
5197 {
5198 	struct bmsafemap_hashhead *bmsafemaphd;
5199 	struct bmsafemap *bmsafemap, *collision;
5200 	struct worklist *wk;
5201 	struct ufsmount *ump;
5202 
5203 	ump = VFSTOUFS(mp);
5204 	LOCK_OWNED(ump);
5205 	KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5206 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5207 		if (wk->wk_type == D_BMSAFEMAP) {
5208 			if (newbmsafemap)
5209 				WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5210 			return (WK_BMSAFEMAP(wk));
5211 		}
5212 	}
5213 	bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5214 	if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5215 		if (newbmsafemap)
5216 			WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5217 		return (bmsafemap);
5218 	}
5219 	if (newbmsafemap) {
5220 		bmsafemap = newbmsafemap;
5221 	} else {
5222 		FREE_LOCK(ump);
5223 		bmsafemap = malloc(sizeof(struct bmsafemap),
5224 			M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5225 		workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5226 		ACQUIRE_LOCK(ump);
5227 	}
5228 	bmsafemap->sm_buf = bp;
5229 	LIST_INIT(&bmsafemap->sm_inodedephd);
5230 	LIST_INIT(&bmsafemap->sm_inodedepwr);
5231 	LIST_INIT(&bmsafemap->sm_newblkhd);
5232 	LIST_INIT(&bmsafemap->sm_newblkwr);
5233 	LIST_INIT(&bmsafemap->sm_jaddrefhd);
5234 	LIST_INIT(&bmsafemap->sm_jnewblkhd);
5235 	LIST_INIT(&bmsafemap->sm_freehd);
5236 	LIST_INIT(&bmsafemap->sm_freewr);
5237 	if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5238 		WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5239 		return (collision);
5240 	}
5241 	bmsafemap->sm_cg = cg;
5242 	LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5243 	LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5244 	WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5245 	return (bmsafemap);
5246 }
5247 
5248 /*
5249  * Direct block allocation dependencies.
5250  *
5251  * When a new block is allocated, the corresponding disk locations must be
5252  * initialized (with zeros or new data) before the on-disk inode points to
5253  * them.  Also, the freemap from which the block was allocated must be
5254  * updated (on disk) before the inode's pointer. These two dependencies are
5255  * independent of each other and are needed for all file blocks and indirect
5256  * blocks that are pointed to directly by the inode.  Just before the
5257  * "in-core" version of the inode is updated with a newly allocated block
5258  * number, a procedure (below) is called to setup allocation dependency
5259  * structures.  These structures are removed when the corresponding
5260  * dependencies are satisfied or when the block allocation becomes obsolete
5261  * (i.e., the file is deleted, the block is de-allocated, or the block is a
5262  * fragment that gets upgraded).  All of these cases are handled in
5263  * procedures described later.
5264  *
5265  * When a file extension causes a fragment to be upgraded, either to a larger
5266  * fragment or to a full block, the on-disk location may change (if the
5267  * previous fragment could not simply be extended). In this case, the old
5268  * fragment must be de-allocated, but not until after the inode's pointer has
5269  * been updated. In most cases, this is handled by later procedures, which
5270  * will construct a "freefrag" structure to be added to the workitem queue
5271  * when the inode update is complete (or obsolete).  The main exception to
5272  * this is when an allocation occurs while a pending allocation dependency
5273  * (for the same block pointer) remains.  This case is handled in the main
5274  * allocation dependency setup procedure by immediately freeing the
5275  * unreferenced fragments.
5276  */
5277 void
5278 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5279 	struct inode *ip;	/* inode to which block is being added */
5280 	ufs_lbn_t off;		/* block pointer within inode */
5281 	ufs2_daddr_t newblkno;	/* disk block number being added */
5282 	ufs2_daddr_t oldblkno;	/* previous block number, 0 unless frag */
5283 	long newsize;		/* size of new block */
5284 	long oldsize;		/* size of new block */
5285 	struct buf *bp;		/* bp for allocated block */
5286 {
5287 	struct allocdirect *adp, *oldadp;
5288 	struct allocdirectlst *adphead;
5289 	struct freefrag *freefrag;
5290 	struct inodedep *inodedep;
5291 	struct pagedep *pagedep;
5292 	struct jnewblk *jnewblk;
5293 	struct newblk *newblk;
5294 	struct mount *mp;
5295 	ufs_lbn_t lbn;
5296 
5297 	lbn = bp->b_lblkno;
5298 	mp = ITOVFS(ip);
5299 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5300 	    ("softdep_setup_allocdirect called on non-softdep filesystem"));
5301 	if (oldblkno && oldblkno != newblkno)
5302 		/*
5303 		 * The usual case is that a smaller fragment that
5304 		 * was just allocated has been replaced with a bigger
5305 		 * fragment or a full-size block. If it is marked as
5306 		 * B_DELWRI, the current contents have not been written
5307 		 * to disk. It is possible that the block was written
5308 		 * earlier, but very uncommon. If the block has never
5309 		 * been written, there is no need to send a BIO_DELETE
5310 		 * for it when it is freed. The gain from avoiding the
5311 		 * TRIMs for the common case of unwritten blocks far
5312 		 * exceeds the cost of the write amplification for the
5313 		 * uncommon case of failing to send a TRIM for a block
5314 		 * that had been written.
5315 		 */
5316 		freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5317 		    (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5318 	else
5319 		freefrag = NULL;
5320 
5321 	CTR6(KTR_SUJ,
5322 	    "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5323 	    "off %jd newsize %ld oldsize %d",
5324 	    ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5325 	ACQUIRE_LOCK(ITOUMP(ip));
5326 	if (off >= UFS_NDADDR) {
5327 		if (lbn > 0)
5328 			panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5329 			    lbn, off);
5330 		/* allocating an indirect block */
5331 		if (oldblkno != 0)
5332 			panic("softdep_setup_allocdirect: non-zero indir");
5333 	} else {
5334 		if (off != lbn)
5335 			panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5336 			    lbn, off);
5337 		/*
5338 		 * Allocating a direct block.
5339 		 *
5340 		 * If we are allocating a directory block, then we must
5341 		 * allocate an associated pagedep to track additions and
5342 		 * deletions.
5343 		 */
5344 		if ((ip->i_mode & IFMT) == IFDIR)
5345 			pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5346 			    &pagedep);
5347 	}
5348 	if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5349 		panic("softdep_setup_allocdirect: lost block");
5350 	KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5351 	    ("softdep_setup_allocdirect: newblk already initialized"));
5352 	/*
5353 	 * Convert the newblk to an allocdirect.
5354 	 */
5355 	WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5356 	adp = (struct allocdirect *)newblk;
5357 	newblk->nb_freefrag = freefrag;
5358 	adp->ad_offset = off;
5359 	adp->ad_oldblkno = oldblkno;
5360 	adp->ad_newsize = newsize;
5361 	adp->ad_oldsize = oldsize;
5362 
5363 	/*
5364 	 * Finish initializing the journal.
5365 	 */
5366 	if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5367 		jnewblk->jn_ino = ip->i_number;
5368 		jnewblk->jn_lbn = lbn;
5369 		add_to_journal(&jnewblk->jn_list);
5370 	}
5371 	if (freefrag && freefrag->ff_jdep != NULL &&
5372 	    freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5373 		add_to_journal(freefrag->ff_jdep);
5374 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5375 	adp->ad_inodedep = inodedep;
5376 
5377 	WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5378 	/*
5379 	 * The list of allocdirects must be kept in sorted and ascending
5380 	 * order so that the rollback routines can quickly determine the
5381 	 * first uncommitted block (the size of the file stored on disk
5382 	 * ends at the end of the lowest committed fragment, or if there
5383 	 * are no fragments, at the end of the highest committed block).
5384 	 * Since files generally grow, the typical case is that the new
5385 	 * block is to be added at the end of the list. We speed this
5386 	 * special case by checking against the last allocdirect in the
5387 	 * list before laboriously traversing the list looking for the
5388 	 * insertion point.
5389 	 */
5390 	adphead = &inodedep->id_newinoupdt;
5391 	oldadp = TAILQ_LAST(adphead, allocdirectlst);
5392 	if (oldadp == NULL || oldadp->ad_offset <= off) {
5393 		/* insert at end of list */
5394 		TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5395 		if (oldadp != NULL && oldadp->ad_offset == off)
5396 			allocdirect_merge(adphead, adp, oldadp);
5397 		FREE_LOCK(ITOUMP(ip));
5398 		return;
5399 	}
5400 	TAILQ_FOREACH(oldadp, adphead, ad_next) {
5401 		if (oldadp->ad_offset >= off)
5402 			break;
5403 	}
5404 	if (oldadp == NULL)
5405 		panic("softdep_setup_allocdirect: lost entry");
5406 	/* insert in middle of list */
5407 	TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5408 	if (oldadp->ad_offset == off)
5409 		allocdirect_merge(adphead, adp, oldadp);
5410 
5411 	FREE_LOCK(ITOUMP(ip));
5412 }
5413 
5414 /*
5415  * Merge a newer and older journal record to be stored either in a
5416  * newblock or freefrag.  This handles aggregating journal records for
5417  * fragment allocation into a second record as well as replacing a
5418  * journal free with an aborted journal allocation.  A segment for the
5419  * oldest record will be placed on wkhd if it has been written.  If not
5420  * the segment for the newer record will suffice.
5421  */
5422 static struct worklist *
5423 jnewblk_merge(new, old, wkhd)
5424 	struct worklist *new;
5425 	struct worklist *old;
5426 	struct workhead *wkhd;
5427 {
5428 	struct jnewblk *njnewblk;
5429 	struct jnewblk *jnewblk;
5430 
5431 	/* Handle NULLs to simplify callers. */
5432 	if (new == NULL)
5433 		return (old);
5434 	if (old == NULL)
5435 		return (new);
5436 	/* Replace a jfreefrag with a jnewblk. */
5437 	if (new->wk_type == D_JFREEFRAG) {
5438 		if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5439 			panic("jnewblk_merge: blkno mismatch: %p, %p",
5440 			    old, new);
5441 		cancel_jfreefrag(WK_JFREEFRAG(new));
5442 		return (old);
5443 	}
5444 	if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5445 		panic("jnewblk_merge: Bad type: old %d new %d\n",
5446 		    old->wk_type, new->wk_type);
5447 	/*
5448 	 * Handle merging of two jnewblk records that describe
5449 	 * different sets of fragments in the same block.
5450 	 */
5451 	jnewblk = WK_JNEWBLK(old);
5452 	njnewblk = WK_JNEWBLK(new);
5453 	if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5454 		panic("jnewblk_merge: Merging disparate blocks.");
5455 	/*
5456 	 * The record may be rolled back in the cg.
5457 	 */
5458 	if (jnewblk->jn_state & UNDONE) {
5459 		jnewblk->jn_state &= ~UNDONE;
5460 		njnewblk->jn_state |= UNDONE;
5461 		njnewblk->jn_state &= ~ATTACHED;
5462 	}
5463 	/*
5464 	 * We modify the newer addref and free the older so that if neither
5465 	 * has been written the most up-to-date copy will be on disk.  If
5466 	 * both have been written but rolled back we only temporarily need
5467 	 * one of them to fix the bits when the cg write completes.
5468 	 */
5469 	jnewblk->jn_state |= ATTACHED | COMPLETE;
5470 	njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5471 	cancel_jnewblk(jnewblk, wkhd);
5472 	WORKLIST_REMOVE(&jnewblk->jn_list);
5473 	free_jnewblk(jnewblk);
5474 	return (new);
5475 }
5476 
5477 /*
5478  * Replace an old allocdirect dependency with a newer one.
5479  */
5480 static void
5481 allocdirect_merge(adphead, newadp, oldadp)
5482 	struct allocdirectlst *adphead;	/* head of list holding allocdirects */
5483 	struct allocdirect *newadp;	/* allocdirect being added */
5484 	struct allocdirect *oldadp;	/* existing allocdirect being checked */
5485 {
5486 	struct worklist *wk;
5487 	struct freefrag *freefrag;
5488 
5489 	freefrag = NULL;
5490 	LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5491 	if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5492 	    newadp->ad_oldsize != oldadp->ad_newsize ||
5493 	    newadp->ad_offset >= UFS_NDADDR)
5494 		panic("%s %jd != new %jd || old size %ld != new %ld",
5495 		    "allocdirect_merge: old blkno",
5496 		    (intmax_t)newadp->ad_oldblkno,
5497 		    (intmax_t)oldadp->ad_newblkno,
5498 		    newadp->ad_oldsize, oldadp->ad_newsize);
5499 	newadp->ad_oldblkno = oldadp->ad_oldblkno;
5500 	newadp->ad_oldsize = oldadp->ad_oldsize;
5501 	/*
5502 	 * If the old dependency had a fragment to free or had never
5503 	 * previously had a block allocated, then the new dependency
5504 	 * can immediately post its freefrag and adopt the old freefrag.
5505 	 * This action is done by swapping the freefrag dependencies.
5506 	 * The new dependency gains the old one's freefrag, and the
5507 	 * old one gets the new one and then immediately puts it on
5508 	 * the worklist when it is freed by free_newblk. It is
5509 	 * not possible to do this swap when the old dependency had a
5510 	 * non-zero size but no previous fragment to free. This condition
5511 	 * arises when the new block is an extension of the old block.
5512 	 * Here, the first part of the fragment allocated to the new
5513 	 * dependency is part of the block currently claimed on disk by
5514 	 * the old dependency, so cannot legitimately be freed until the
5515 	 * conditions for the new dependency are fulfilled.
5516 	 */
5517 	freefrag = newadp->ad_freefrag;
5518 	if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5519 		newadp->ad_freefrag = oldadp->ad_freefrag;
5520 		oldadp->ad_freefrag = freefrag;
5521 	}
5522 	/*
5523 	 * If we are tracking a new directory-block allocation,
5524 	 * move it from the old allocdirect to the new allocdirect.
5525 	 */
5526 	if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5527 		WORKLIST_REMOVE(wk);
5528 		if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5529 			panic("allocdirect_merge: extra newdirblk");
5530 		WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5531 	}
5532 	TAILQ_REMOVE(adphead, oldadp, ad_next);
5533 	/*
5534 	 * We need to move any journal dependencies over to the freefrag
5535 	 * that releases this block if it exists.  Otherwise we are
5536 	 * extending an existing block and we'll wait until that is
5537 	 * complete to release the journal space and extend the
5538 	 * new journal to cover this old space as well.
5539 	 */
5540 	if (freefrag == NULL) {
5541 		if (oldadp->ad_newblkno != newadp->ad_newblkno)
5542 			panic("allocdirect_merge: %jd != %jd",
5543 			    oldadp->ad_newblkno, newadp->ad_newblkno);
5544 		newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5545 		    jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5546 		    &oldadp->ad_block.nb_jnewblk->jn_list,
5547 		    &newadp->ad_block.nb_jwork);
5548 		oldadp->ad_block.nb_jnewblk = NULL;
5549 		cancel_newblk(&oldadp->ad_block, NULL,
5550 		    &newadp->ad_block.nb_jwork);
5551 	} else {
5552 		wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5553 		    &freefrag->ff_list, &freefrag->ff_jwork);
5554 		freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5555 		    &freefrag->ff_jwork);
5556 	}
5557 	free_newblk(&oldadp->ad_block);
5558 }
5559 
5560 /*
5561  * Allocate a jfreefrag structure to journal a single block free.
5562  */
5563 static struct jfreefrag *
5564 newjfreefrag(freefrag, ip, blkno, size, lbn)
5565 	struct freefrag *freefrag;
5566 	struct inode *ip;
5567 	ufs2_daddr_t blkno;
5568 	long size;
5569 	ufs_lbn_t lbn;
5570 {
5571 	struct jfreefrag *jfreefrag;
5572 	struct fs *fs;
5573 
5574 	fs = ITOFS(ip);
5575 	jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5576 	    M_SOFTDEP_FLAGS);
5577 	workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5578 	jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5579 	jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5580 	jfreefrag->fr_ino = ip->i_number;
5581 	jfreefrag->fr_lbn = lbn;
5582 	jfreefrag->fr_blkno = blkno;
5583 	jfreefrag->fr_frags = numfrags(fs, size);
5584 	jfreefrag->fr_freefrag = freefrag;
5585 
5586 	return (jfreefrag);
5587 }
5588 
5589 /*
5590  * Allocate a new freefrag structure.
5591  */
5592 static struct freefrag *
5593 newfreefrag(ip, blkno, size, lbn, key)
5594 	struct inode *ip;
5595 	ufs2_daddr_t blkno;
5596 	long size;
5597 	ufs_lbn_t lbn;
5598 	u_long key;
5599 {
5600 	struct freefrag *freefrag;
5601 	struct ufsmount *ump;
5602 	struct fs *fs;
5603 
5604 	CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5605 	    ip->i_number, blkno, size, lbn);
5606 	ump = ITOUMP(ip);
5607 	fs = ump->um_fs;
5608 	if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5609 		panic("newfreefrag: frag size");
5610 	freefrag = malloc(sizeof(struct freefrag),
5611 	    M_FREEFRAG, M_SOFTDEP_FLAGS);
5612 	workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5613 	freefrag->ff_state = ATTACHED;
5614 	LIST_INIT(&freefrag->ff_jwork);
5615 	freefrag->ff_inum = ip->i_number;
5616 	freefrag->ff_vtype = ITOV(ip)->v_type;
5617 	freefrag->ff_blkno = blkno;
5618 	freefrag->ff_fragsize = size;
5619 	freefrag->ff_key = key;
5620 
5621 	if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5622 		freefrag->ff_jdep = (struct worklist *)
5623 		    newjfreefrag(freefrag, ip, blkno, size, lbn);
5624 	} else {
5625 		freefrag->ff_state |= DEPCOMPLETE;
5626 		freefrag->ff_jdep = NULL;
5627 	}
5628 
5629 	return (freefrag);
5630 }
5631 
5632 /*
5633  * This workitem de-allocates fragments that were replaced during
5634  * file block allocation.
5635  */
5636 static void
5637 handle_workitem_freefrag(freefrag)
5638 	struct freefrag *freefrag;
5639 {
5640 	struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5641 	struct workhead wkhd;
5642 
5643 	CTR3(KTR_SUJ,
5644 	    "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5645 	    freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5646 	/*
5647 	 * It would be illegal to add new completion items to the
5648 	 * freefrag after it was schedule to be done so it must be
5649 	 * safe to modify the list head here.
5650 	 */
5651 	LIST_INIT(&wkhd);
5652 	ACQUIRE_LOCK(ump);
5653 	LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5654 	/*
5655 	 * If the journal has not been written we must cancel it here.
5656 	 */
5657 	if (freefrag->ff_jdep) {
5658 		if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5659 			panic("handle_workitem_freefrag: Unexpected type %d\n",
5660 			    freefrag->ff_jdep->wk_type);
5661 		cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5662 	}
5663 	FREE_LOCK(ump);
5664 	ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5665 	   freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
5666 	   &wkhd, freefrag->ff_key);
5667 	ACQUIRE_LOCK(ump);
5668 	WORKITEM_FREE(freefrag, D_FREEFRAG);
5669 	FREE_LOCK(ump);
5670 }
5671 
5672 /*
5673  * Set up a dependency structure for an external attributes data block.
5674  * This routine follows much of the structure of softdep_setup_allocdirect.
5675  * See the description of softdep_setup_allocdirect above for details.
5676  */
5677 void
5678 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5679 	struct inode *ip;
5680 	ufs_lbn_t off;
5681 	ufs2_daddr_t newblkno;
5682 	ufs2_daddr_t oldblkno;
5683 	long newsize;
5684 	long oldsize;
5685 	struct buf *bp;
5686 {
5687 	struct allocdirect *adp, *oldadp;
5688 	struct allocdirectlst *adphead;
5689 	struct freefrag *freefrag;
5690 	struct inodedep *inodedep;
5691 	struct jnewblk *jnewblk;
5692 	struct newblk *newblk;
5693 	struct mount *mp;
5694 	struct ufsmount *ump;
5695 	ufs_lbn_t lbn;
5696 
5697 	mp = ITOVFS(ip);
5698 	ump = VFSTOUFS(mp);
5699 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5700 	    ("softdep_setup_allocext called on non-softdep filesystem"));
5701 	KASSERT(off < UFS_NXADDR,
5702 	    ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
5703 
5704 	lbn = bp->b_lblkno;
5705 	if (oldblkno && oldblkno != newblkno)
5706 		/*
5707 		 * The usual case is that a smaller fragment that
5708 		 * was just allocated has been replaced with a bigger
5709 		 * fragment or a full-size block. If it is marked as
5710 		 * B_DELWRI, the current contents have not been written
5711 		 * to disk. It is possible that the block was written
5712 		 * earlier, but very uncommon. If the block has never
5713 		 * been written, there is no need to send a BIO_DELETE
5714 		 * for it when it is freed. The gain from avoiding the
5715 		 * TRIMs for the common case of unwritten blocks far
5716 		 * exceeds the cost of the write amplification for the
5717 		 * uncommon case of failing to send a TRIM for a block
5718 		 * that had been written.
5719 		 */
5720 		freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5721 		    (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5722 	else
5723 		freefrag = NULL;
5724 
5725 	ACQUIRE_LOCK(ump);
5726 	if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5727 		panic("softdep_setup_allocext: lost block");
5728 	KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5729 	    ("softdep_setup_allocext: newblk already initialized"));
5730 	/*
5731 	 * Convert the newblk to an allocdirect.
5732 	 */
5733 	WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5734 	adp = (struct allocdirect *)newblk;
5735 	newblk->nb_freefrag = freefrag;
5736 	adp->ad_offset = off;
5737 	adp->ad_oldblkno = oldblkno;
5738 	adp->ad_newsize = newsize;
5739 	adp->ad_oldsize = oldsize;
5740 	adp->ad_state |=  EXTDATA;
5741 
5742 	/*
5743 	 * Finish initializing the journal.
5744 	 */
5745 	if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5746 		jnewblk->jn_ino = ip->i_number;
5747 		jnewblk->jn_lbn = lbn;
5748 		add_to_journal(&jnewblk->jn_list);
5749 	}
5750 	if (freefrag && freefrag->ff_jdep != NULL &&
5751 	    freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5752 		add_to_journal(freefrag->ff_jdep);
5753 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5754 	adp->ad_inodedep = inodedep;
5755 
5756 	WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5757 	/*
5758 	 * The list of allocdirects must be kept in sorted and ascending
5759 	 * order so that the rollback routines can quickly determine the
5760 	 * first uncommitted block (the size of the file stored on disk
5761 	 * ends at the end of the lowest committed fragment, or if there
5762 	 * are no fragments, at the end of the highest committed block).
5763 	 * Since files generally grow, the typical case is that the new
5764 	 * block is to be added at the end of the list. We speed this
5765 	 * special case by checking against the last allocdirect in the
5766 	 * list before laboriously traversing the list looking for the
5767 	 * insertion point.
5768 	 */
5769 	adphead = &inodedep->id_newextupdt;
5770 	oldadp = TAILQ_LAST(adphead, allocdirectlst);
5771 	if (oldadp == NULL || oldadp->ad_offset <= off) {
5772 		/* insert at end of list */
5773 		TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5774 		if (oldadp != NULL && oldadp->ad_offset == off)
5775 			allocdirect_merge(adphead, adp, oldadp);
5776 		FREE_LOCK(ump);
5777 		return;
5778 	}
5779 	TAILQ_FOREACH(oldadp, adphead, ad_next) {
5780 		if (oldadp->ad_offset >= off)
5781 			break;
5782 	}
5783 	if (oldadp == NULL)
5784 		panic("softdep_setup_allocext: lost entry");
5785 	/* insert in middle of list */
5786 	TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5787 	if (oldadp->ad_offset == off)
5788 		allocdirect_merge(adphead, adp, oldadp);
5789 	FREE_LOCK(ump);
5790 }
5791 
5792 /*
5793  * Indirect block allocation dependencies.
5794  *
5795  * The same dependencies that exist for a direct block also exist when
5796  * a new block is allocated and pointed to by an entry in a block of
5797  * indirect pointers. The undo/redo states described above are also
5798  * used here. Because an indirect block contains many pointers that
5799  * may have dependencies, a second copy of the entire in-memory indirect
5800  * block is kept. The buffer cache copy is always completely up-to-date.
5801  * The second copy, which is used only as a source for disk writes,
5802  * contains only the safe pointers (i.e., those that have no remaining
5803  * update dependencies). The second copy is freed when all pointers
5804  * are safe. The cache is not allowed to replace indirect blocks with
5805  * pending update dependencies. If a buffer containing an indirect
5806  * block with dependencies is written, these routines will mark it
5807  * dirty again. It can only be successfully written once all the
5808  * dependencies are removed. The ffs_fsync routine in conjunction with
5809  * softdep_sync_metadata work together to get all the dependencies
5810  * removed so that a file can be successfully written to disk. Three
5811  * procedures are used when setting up indirect block pointer
5812  * dependencies. The division is necessary because of the organization
5813  * of the "balloc" routine and because of the distinction between file
5814  * pages and file metadata blocks.
5815  */
5816 
5817 /*
5818  * Allocate a new allocindir structure.
5819  */
5820 static struct allocindir *
5821 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
5822 	struct inode *ip;	/* inode for file being extended */
5823 	int ptrno;		/* offset of pointer in indirect block */
5824 	ufs2_daddr_t newblkno;	/* disk block number being added */
5825 	ufs2_daddr_t oldblkno;	/* previous block number, 0 if none */
5826 	ufs_lbn_t lbn;
5827 {
5828 	struct newblk *newblk;
5829 	struct allocindir *aip;
5830 	struct freefrag *freefrag;
5831 	struct jnewblk *jnewblk;
5832 
5833 	if (oldblkno)
5834 		freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
5835 		    SINGLETON_KEY);
5836 	else
5837 		freefrag = NULL;
5838 	ACQUIRE_LOCK(ITOUMP(ip));
5839 	if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
5840 		panic("new_allocindir: lost block");
5841 	KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5842 	    ("newallocindir: newblk already initialized"));
5843 	WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
5844 	newblk->nb_freefrag = freefrag;
5845 	aip = (struct allocindir *)newblk;
5846 	aip->ai_offset = ptrno;
5847 	aip->ai_oldblkno = oldblkno;
5848 	aip->ai_lbn = lbn;
5849 	if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5850 		jnewblk->jn_ino = ip->i_number;
5851 		jnewblk->jn_lbn = lbn;
5852 		add_to_journal(&jnewblk->jn_list);
5853 	}
5854 	if (freefrag && freefrag->ff_jdep != NULL &&
5855 	    freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5856 		add_to_journal(freefrag->ff_jdep);
5857 	return (aip);
5858 }
5859 
5860 /*
5861  * Called just before setting an indirect block pointer
5862  * to a newly allocated file page.
5863  */
5864 void
5865 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
5866 	struct inode *ip;	/* inode for file being extended */
5867 	ufs_lbn_t lbn;		/* allocated block number within file */
5868 	struct buf *bp;		/* buffer with indirect blk referencing page */
5869 	int ptrno;		/* offset of pointer in indirect block */
5870 	ufs2_daddr_t newblkno;	/* disk block number being added */
5871 	ufs2_daddr_t oldblkno;	/* previous block number, 0 if none */
5872 	struct buf *nbp;	/* buffer holding allocated page */
5873 {
5874 	struct inodedep *inodedep;
5875 	struct freefrag *freefrag;
5876 	struct allocindir *aip;
5877 	struct pagedep *pagedep;
5878 	struct mount *mp;
5879 	struct ufsmount *ump;
5880 
5881 	mp = ITOVFS(ip);
5882 	ump = VFSTOUFS(mp);
5883 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5884 	    ("softdep_setup_allocindir_page called on non-softdep filesystem"));
5885 	KASSERT(lbn == nbp->b_lblkno,
5886 	    ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
5887 	    lbn, bp->b_lblkno));
5888 	CTR4(KTR_SUJ,
5889 	    "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
5890 	    "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
5891 	ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
5892 	aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
5893 	(void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5894 	/*
5895 	 * If we are allocating a directory page, then we must
5896 	 * allocate an associated pagedep to track additions and
5897 	 * deletions.
5898 	 */
5899 	if ((ip->i_mode & IFMT) == IFDIR)
5900 		pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
5901 	WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5902 	freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
5903 	FREE_LOCK(ump);
5904 	if (freefrag)
5905 		handle_workitem_freefrag(freefrag);
5906 }
5907 
5908 /*
5909  * Called just before setting an indirect block pointer to a
5910  * newly allocated indirect block.
5911  */
5912 void
5913 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
5914 	struct buf *nbp;	/* newly allocated indirect block */
5915 	struct inode *ip;	/* inode for file being extended */
5916 	struct buf *bp;		/* indirect block referencing allocated block */
5917 	int ptrno;		/* offset of pointer in indirect block */
5918 	ufs2_daddr_t newblkno;	/* disk block number being added */
5919 {
5920 	struct inodedep *inodedep;
5921 	struct allocindir *aip;
5922 	struct ufsmount *ump;
5923 	ufs_lbn_t lbn;
5924 
5925 	ump = ITOUMP(ip);
5926 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
5927 	    ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
5928 	CTR3(KTR_SUJ,
5929 	    "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
5930 	    ip->i_number, newblkno, ptrno);
5931 	lbn = nbp->b_lblkno;
5932 	ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
5933 	aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
5934 	inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
5935 	WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5936 	if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
5937 		panic("softdep_setup_allocindir_meta: Block already existed");
5938 	FREE_LOCK(ump);
5939 }
5940 
5941 static void
5942 indirdep_complete(indirdep)
5943 	struct indirdep *indirdep;
5944 {
5945 	struct allocindir *aip;
5946 
5947 	LIST_REMOVE(indirdep, ir_next);
5948 	indirdep->ir_state |= DEPCOMPLETE;
5949 
5950 	while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
5951 		LIST_REMOVE(aip, ai_next);
5952 		free_newblk(&aip->ai_block);
5953 	}
5954 	/*
5955 	 * If this indirdep is not attached to a buf it was simply waiting
5956 	 * on completion to clear completehd.  free_indirdep() asserts
5957 	 * that nothing is dangling.
5958 	 */
5959 	if ((indirdep->ir_state & ONWORKLIST) == 0)
5960 		free_indirdep(indirdep);
5961 }
5962 
5963 static struct indirdep *
5964 indirdep_lookup(mp, ip, bp)
5965 	struct mount *mp;
5966 	struct inode *ip;
5967 	struct buf *bp;
5968 {
5969 	struct indirdep *indirdep, *newindirdep;
5970 	struct newblk *newblk;
5971 	struct ufsmount *ump;
5972 	struct worklist *wk;
5973 	struct fs *fs;
5974 	ufs2_daddr_t blkno;
5975 
5976 	ump = VFSTOUFS(mp);
5977 	LOCK_OWNED(ump);
5978 	indirdep = NULL;
5979 	newindirdep = NULL;
5980 	fs = ump->um_fs;
5981 	for (;;) {
5982 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5983 			if (wk->wk_type != D_INDIRDEP)
5984 				continue;
5985 			indirdep = WK_INDIRDEP(wk);
5986 			break;
5987 		}
5988 		/* Found on the buffer worklist, no new structure to free. */
5989 		if (indirdep != NULL && newindirdep == NULL)
5990 			return (indirdep);
5991 		if (indirdep != NULL && newindirdep != NULL)
5992 			panic("indirdep_lookup: simultaneous create");
5993 		/* None found on the buffer and a new structure is ready. */
5994 		if (indirdep == NULL && newindirdep != NULL)
5995 			break;
5996 		/* None found and no new structure available. */
5997 		FREE_LOCK(ump);
5998 		newindirdep = malloc(sizeof(struct indirdep),
5999 		    M_INDIRDEP, M_SOFTDEP_FLAGS);
6000 		workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6001 		newindirdep->ir_state = ATTACHED;
6002 		if (I_IS_UFS1(ip))
6003 			newindirdep->ir_state |= UFS1FMT;
6004 		TAILQ_INIT(&newindirdep->ir_trunc);
6005 		newindirdep->ir_saveddata = NULL;
6006 		LIST_INIT(&newindirdep->ir_deplisthd);
6007 		LIST_INIT(&newindirdep->ir_donehd);
6008 		LIST_INIT(&newindirdep->ir_writehd);
6009 		LIST_INIT(&newindirdep->ir_completehd);
6010 		if (bp->b_blkno == bp->b_lblkno) {
6011 			ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6012 			    NULL, NULL);
6013 			bp->b_blkno = blkno;
6014 		}
6015 		newindirdep->ir_freeblks = NULL;
6016 		newindirdep->ir_savebp =
6017 		    getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6018 		newindirdep->ir_bp = bp;
6019 		BUF_KERNPROC(newindirdep->ir_savebp);
6020 		bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6021 		ACQUIRE_LOCK(ump);
6022 	}
6023 	indirdep = newindirdep;
6024 	WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6025 	/*
6026 	 * If the block is not yet allocated we don't set DEPCOMPLETE so
6027 	 * that we don't free dependencies until the pointers are valid.
6028 	 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6029 	 * than using the hash.
6030 	 */
6031 	if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6032 		LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6033 	else
6034 		indirdep->ir_state |= DEPCOMPLETE;
6035 	return (indirdep);
6036 }
6037 
6038 /*
6039  * Called to finish the allocation of the "aip" allocated
6040  * by one of the two routines above.
6041  */
6042 static struct freefrag *
6043 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
6044 	struct buf *bp;		/* in-memory copy of the indirect block */
6045 	struct inode *ip;	/* inode for file being extended */
6046 	struct inodedep *inodedep; /* Inodedep for ip */
6047 	struct allocindir *aip;	/* allocindir allocated by the above routines */
6048 	ufs_lbn_t lbn;		/* Logical block number for this block. */
6049 {
6050 	struct fs *fs;
6051 	struct indirdep *indirdep;
6052 	struct allocindir *oldaip;
6053 	struct freefrag *freefrag;
6054 	struct mount *mp;
6055 	struct ufsmount *ump;
6056 
6057 	mp = ITOVFS(ip);
6058 	ump = VFSTOUFS(mp);
6059 	LOCK_OWNED(ump);
6060 	fs = ump->um_fs;
6061 	if (bp->b_lblkno >= 0)
6062 		panic("setup_allocindir_phase2: not indir blk");
6063 	KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6064 	    ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6065 	indirdep = indirdep_lookup(mp, ip, bp);
6066 	KASSERT(indirdep->ir_savebp != NULL,
6067 	    ("setup_allocindir_phase2 NULL ir_savebp"));
6068 	aip->ai_indirdep = indirdep;
6069 	/*
6070 	 * Check for an unwritten dependency for this indirect offset.  If
6071 	 * there is, merge the old dependency into the new one.  This happens
6072 	 * as a result of reallocblk only.
6073 	 */
6074 	freefrag = NULL;
6075 	if (aip->ai_oldblkno != 0) {
6076 		LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6077 			if (oldaip->ai_offset == aip->ai_offset) {
6078 				freefrag = allocindir_merge(aip, oldaip);
6079 				goto done;
6080 			}
6081 		}
6082 		LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6083 			if (oldaip->ai_offset == aip->ai_offset) {
6084 				freefrag = allocindir_merge(aip, oldaip);
6085 				goto done;
6086 			}
6087 		}
6088 	}
6089 done:
6090 	LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6091 	return (freefrag);
6092 }
6093 
6094 /*
6095  * Merge two allocindirs which refer to the same block.  Move newblock
6096  * dependencies and setup the freefrags appropriately.
6097  */
6098 static struct freefrag *
6099 allocindir_merge(aip, oldaip)
6100 	struct allocindir *aip;
6101 	struct allocindir *oldaip;
6102 {
6103 	struct freefrag *freefrag;
6104 	struct worklist *wk;
6105 
6106 	if (oldaip->ai_newblkno != aip->ai_oldblkno)
6107 		panic("allocindir_merge: blkno");
6108 	aip->ai_oldblkno = oldaip->ai_oldblkno;
6109 	freefrag = aip->ai_freefrag;
6110 	aip->ai_freefrag = oldaip->ai_freefrag;
6111 	oldaip->ai_freefrag = NULL;
6112 	KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6113 	/*
6114 	 * If we are tracking a new directory-block allocation,
6115 	 * move it from the old allocindir to the new allocindir.
6116 	 */
6117 	if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6118 		WORKLIST_REMOVE(wk);
6119 		if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6120 			panic("allocindir_merge: extra newdirblk");
6121 		WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6122 	}
6123 	/*
6124 	 * We can skip journaling for this freefrag and just complete
6125 	 * any pending journal work for the allocindir that is being
6126 	 * removed after the freefrag completes.
6127 	 */
6128 	if (freefrag->ff_jdep)
6129 		cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6130 	LIST_REMOVE(oldaip, ai_next);
6131 	freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6132 	    &freefrag->ff_list, &freefrag->ff_jwork);
6133 	free_newblk(&oldaip->ai_block);
6134 
6135 	return (freefrag);
6136 }
6137 
6138 static inline void
6139 setup_freedirect(freeblks, ip, i, needj)
6140 	struct freeblks *freeblks;
6141 	struct inode *ip;
6142 	int i;
6143 	int needj;
6144 {
6145 	struct ufsmount *ump;
6146 	ufs2_daddr_t blkno;
6147 	int frags;
6148 
6149 	blkno = DIP(ip, i_db[i]);
6150 	if (blkno == 0)
6151 		return;
6152 	DIP_SET(ip, i_db[i], 0);
6153 	ump = ITOUMP(ip);
6154 	frags = sblksize(ump->um_fs, ip->i_size, i);
6155 	frags = numfrags(ump->um_fs, frags);
6156 	newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6157 }
6158 
6159 static inline void
6160 setup_freeext(freeblks, ip, i, needj)
6161 	struct freeblks *freeblks;
6162 	struct inode *ip;
6163 	int i;
6164 	int needj;
6165 {
6166 	struct ufsmount *ump;
6167 	ufs2_daddr_t blkno;
6168 	int frags;
6169 
6170 	blkno = ip->i_din2->di_extb[i];
6171 	if (blkno == 0)
6172 		return;
6173 	ip->i_din2->di_extb[i] = 0;
6174 	ump = ITOUMP(ip);
6175 	frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6176 	frags = numfrags(ump->um_fs, frags);
6177 	newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6178 }
6179 
6180 static inline void
6181 setup_freeindir(freeblks, ip, i, lbn, needj)
6182 	struct freeblks *freeblks;
6183 	struct inode *ip;
6184 	int i;
6185 	ufs_lbn_t lbn;
6186 	int needj;
6187 {
6188 	struct ufsmount *ump;
6189 	ufs2_daddr_t blkno;
6190 
6191 	blkno = DIP(ip, i_ib[i]);
6192 	if (blkno == 0)
6193 		return;
6194 	DIP_SET(ip, i_ib[i], 0);
6195 	ump = ITOUMP(ip);
6196 	newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6197 	    0, needj);
6198 }
6199 
6200 static inline struct freeblks *
6201 newfreeblks(mp, ip)
6202 	struct mount *mp;
6203 	struct inode *ip;
6204 {
6205 	struct freeblks *freeblks;
6206 
6207 	freeblks = malloc(sizeof(struct freeblks),
6208 		M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6209 	workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6210 	LIST_INIT(&freeblks->fb_jblkdephd);
6211 	LIST_INIT(&freeblks->fb_jwork);
6212 	freeblks->fb_ref = 0;
6213 	freeblks->fb_cgwait = 0;
6214 	freeblks->fb_state = ATTACHED;
6215 	freeblks->fb_uid = ip->i_uid;
6216 	freeblks->fb_inum = ip->i_number;
6217 	freeblks->fb_vtype = ITOV(ip)->v_type;
6218 	freeblks->fb_modrev = DIP(ip, i_modrev);
6219 	freeblks->fb_devvp = ITODEVVP(ip);
6220 	freeblks->fb_chkcnt = 0;
6221 	freeblks->fb_len = 0;
6222 
6223 	return (freeblks);
6224 }
6225 
6226 static void
6227 trunc_indirdep(indirdep, freeblks, bp, off)
6228 	struct indirdep *indirdep;
6229 	struct freeblks *freeblks;
6230 	struct buf *bp;
6231 	int off;
6232 {
6233 	struct allocindir *aip, *aipn;
6234 
6235 	/*
6236 	 * The first set of allocindirs won't be in savedbp.
6237 	 */
6238 	LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6239 		if (aip->ai_offset > off)
6240 			cancel_allocindir(aip, bp, freeblks, 1);
6241 	LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6242 		if (aip->ai_offset > off)
6243 			cancel_allocindir(aip, bp, freeblks, 1);
6244 	/*
6245 	 * These will exist in savedbp.
6246 	 */
6247 	LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6248 		if (aip->ai_offset > off)
6249 			cancel_allocindir(aip, NULL, freeblks, 0);
6250 	LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6251 		if (aip->ai_offset > off)
6252 			cancel_allocindir(aip, NULL, freeblks, 0);
6253 }
6254 
6255 /*
6256  * Follow the chain of indirects down to lastlbn creating a freework
6257  * structure for each.  This will be used to start indir_trunc() at
6258  * the right offset and create the journal records for the parrtial
6259  * truncation.  A second step will handle the truncated dependencies.
6260  */
6261 static int
6262 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6263 	struct freeblks *freeblks;
6264 	struct inode *ip;
6265 	ufs_lbn_t lbn;
6266 	ufs_lbn_t lastlbn;
6267 	ufs2_daddr_t blkno;
6268 {
6269 	struct indirdep *indirdep;
6270 	struct indirdep *indirn;
6271 	struct freework *freework;
6272 	struct newblk *newblk;
6273 	struct mount *mp;
6274 	struct ufsmount *ump;
6275 	struct buf *bp;
6276 	uint8_t *start;
6277 	uint8_t *end;
6278 	ufs_lbn_t lbnadd;
6279 	int level;
6280 	int error;
6281 	int off;
6282 
6283 
6284 	freework = NULL;
6285 	if (blkno == 0)
6286 		return (0);
6287 	mp = freeblks->fb_list.wk_mp;
6288 	ump = VFSTOUFS(mp);
6289 	bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0);
6290 	if ((bp->b_flags & B_CACHE) == 0) {
6291 		bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno);
6292 		bp->b_iocmd = BIO_READ;
6293 		bp->b_flags &= ~B_INVAL;
6294 		bp->b_ioflags &= ~BIO_ERROR;
6295 		vfs_busy_pages(bp, 0);
6296 		bp->b_iooffset = dbtob(bp->b_blkno);
6297 		bstrategy(bp);
6298 #ifdef RACCT
6299 		if (racct_enable) {
6300 			PROC_LOCK(curproc);
6301 			racct_add_buf(curproc, bp, 0);
6302 			PROC_UNLOCK(curproc);
6303 		}
6304 #endif /* RACCT */
6305 		curthread->td_ru.ru_inblock++;
6306 		error = bufwait(bp);
6307 		if (error) {
6308 			brelse(bp);
6309 			return (error);
6310 		}
6311 	}
6312 	level = lbn_level(lbn);
6313 	lbnadd = lbn_offset(ump->um_fs, level);
6314 	/*
6315 	 * Compute the offset of the last block we want to keep.  Store
6316 	 * in the freework the first block we want to completely free.
6317 	 */
6318 	off = (lastlbn - -(lbn + level)) / lbnadd;
6319 	if (off + 1 == NINDIR(ump->um_fs))
6320 		goto nowork;
6321 	freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6322 	/*
6323 	 * Link the freework into the indirdep.  This will prevent any new
6324 	 * allocations from proceeding until we are finished with the
6325 	 * truncate and the block is written.
6326 	 */
6327 	ACQUIRE_LOCK(ump);
6328 	indirdep = indirdep_lookup(mp, ip, bp);
6329 	if (indirdep->ir_freeblks)
6330 		panic("setup_trunc_indir: indirdep already truncated.");
6331 	TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6332 	freework->fw_indir = indirdep;
6333 	/*
6334 	 * Cancel any allocindirs that will not make it to disk.
6335 	 * We have to do this for all copies of the indirdep that
6336 	 * live on this newblk.
6337 	 */
6338 	if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6339 		if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6340 		    &newblk) == 0)
6341 			panic("setup_trunc_indir: lost block");
6342 		LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6343 			trunc_indirdep(indirn, freeblks, bp, off);
6344 	} else
6345 		trunc_indirdep(indirdep, freeblks, bp, off);
6346 	FREE_LOCK(ump);
6347 	/*
6348 	 * Creation is protected by the buf lock. The saveddata is only
6349 	 * needed if a full truncation follows a partial truncation but it
6350 	 * is difficult to allocate in that case so we fetch it anyway.
6351 	 */
6352 	if (indirdep->ir_saveddata == NULL)
6353 		indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6354 		    M_SOFTDEP_FLAGS);
6355 nowork:
6356 	/* Fetch the blkno of the child and the zero start offset. */
6357 	if (I_IS_UFS1(ip)) {
6358 		blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6359 		start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6360 	} else {
6361 		blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6362 		start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6363 	}
6364 	if (freework) {
6365 		/* Zero the truncated pointers. */
6366 		end = bp->b_data + bp->b_bcount;
6367 		bzero(start, end - start);
6368 		bdwrite(bp);
6369 	} else
6370 		bqrelse(bp);
6371 	if (level == 0)
6372 		return (0);
6373 	lbn++; /* adjust level */
6374 	lbn -= (off * lbnadd);
6375 	return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6376 }
6377 
6378 /*
6379  * Complete the partial truncation of an indirect block setup by
6380  * setup_trunc_indir().  This zeros the truncated pointers in the saved
6381  * copy and writes them to disk before the freeblks is allowed to complete.
6382  */
6383 static void
6384 complete_trunc_indir(freework)
6385 	struct freework *freework;
6386 {
6387 	struct freework *fwn;
6388 	struct indirdep *indirdep;
6389 	struct ufsmount *ump;
6390 	struct buf *bp;
6391 	uintptr_t start;
6392 	int count;
6393 
6394 	ump = VFSTOUFS(freework->fw_list.wk_mp);
6395 	LOCK_OWNED(ump);
6396 	indirdep = freework->fw_indir;
6397 	for (;;) {
6398 		bp = indirdep->ir_bp;
6399 		/* See if the block was discarded. */
6400 		if (bp == NULL)
6401 			break;
6402 		/* Inline part of getdirtybuf().  We dont want bremfree. */
6403 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6404 			break;
6405 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6406 		    LOCK_PTR(ump)) == 0)
6407 			BUF_UNLOCK(bp);
6408 		ACQUIRE_LOCK(ump);
6409 	}
6410 	freework->fw_state |= DEPCOMPLETE;
6411 	TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6412 	/*
6413 	 * Zero the pointers in the saved copy.
6414 	 */
6415 	if (indirdep->ir_state & UFS1FMT)
6416 		start = sizeof(ufs1_daddr_t);
6417 	else
6418 		start = sizeof(ufs2_daddr_t);
6419 	start *= freework->fw_start;
6420 	count = indirdep->ir_savebp->b_bcount - start;
6421 	start += (uintptr_t)indirdep->ir_savebp->b_data;
6422 	bzero((char *)start, count);
6423 	/*
6424 	 * We need to start the next truncation in the list if it has not
6425 	 * been started yet.
6426 	 */
6427 	fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6428 	if (fwn != NULL) {
6429 		if (fwn->fw_freeblks == indirdep->ir_freeblks)
6430 			TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6431 		if ((fwn->fw_state & ONWORKLIST) == 0)
6432 			freework_enqueue(fwn);
6433 	}
6434 	/*
6435 	 * If bp is NULL the block was fully truncated, restore
6436 	 * the saved block list otherwise free it if it is no
6437 	 * longer needed.
6438 	 */
6439 	if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6440 		if (bp == NULL)
6441 			bcopy(indirdep->ir_saveddata,
6442 			    indirdep->ir_savebp->b_data,
6443 			    indirdep->ir_savebp->b_bcount);
6444 		free(indirdep->ir_saveddata, M_INDIRDEP);
6445 		indirdep->ir_saveddata = NULL;
6446 	}
6447 	/*
6448 	 * When bp is NULL there is a full truncation pending.  We
6449 	 * must wait for this full truncation to be journaled before
6450 	 * we can release this freework because the disk pointers will
6451 	 * never be written as zero.
6452 	 */
6453 	if (bp == NULL)  {
6454 		if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6455 			handle_written_freework(freework);
6456 		else
6457 			WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6458 			   &freework->fw_list);
6459 	} else {
6460 		/* Complete when the real copy is written. */
6461 		WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6462 		BUF_UNLOCK(bp);
6463 	}
6464 }
6465 
6466 /*
6467  * Calculate the number of blocks we are going to release where datablocks
6468  * is the current total and length is the new file size.
6469  */
6470 static ufs2_daddr_t
6471 blkcount(fs, datablocks, length)
6472 	struct fs *fs;
6473 	ufs2_daddr_t datablocks;
6474 	off_t length;
6475 {
6476 	off_t totblks, numblks;
6477 
6478 	totblks = 0;
6479 	numblks = howmany(length, fs->fs_bsize);
6480 	if (numblks <= UFS_NDADDR) {
6481 		totblks = howmany(length, fs->fs_fsize);
6482 		goto out;
6483 	}
6484         totblks = blkstofrags(fs, numblks);
6485 	numblks -= UFS_NDADDR;
6486 	/*
6487 	 * Count all single, then double, then triple indirects required.
6488 	 * Subtracting one indirects worth of blocks for each pass
6489 	 * acknowledges one of each pointed to by the inode.
6490 	 */
6491 	for (;;) {
6492 		totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6493 		numblks -= NINDIR(fs);
6494 		if (numblks <= 0)
6495 			break;
6496 		numblks = howmany(numblks, NINDIR(fs));
6497 	}
6498 out:
6499 	totblks = fsbtodb(fs, totblks);
6500 	/*
6501 	 * Handle sparse files.  We can't reclaim more blocks than the inode
6502 	 * references.  We will correct it later in handle_complete_freeblks()
6503 	 * when we know the real count.
6504 	 */
6505 	if (totblks > datablocks)
6506 		return (0);
6507 	return (datablocks - totblks);
6508 }
6509 
6510 /*
6511  * Handle freeblocks for journaled softupdate filesystems.
6512  *
6513  * Contrary to normal softupdates, we must preserve the block pointers in
6514  * indirects until their subordinates are free.  This is to avoid journaling
6515  * every block that is freed which may consume more space than the journal
6516  * itself.  The recovery program will see the free block journals at the
6517  * base of the truncated area and traverse them to reclaim space.  The
6518  * pointers in the inode may be cleared immediately after the journal
6519  * records are written because each direct and indirect pointer in the
6520  * inode is recorded in a journal.  This permits full truncation to proceed
6521  * asynchronously.  The write order is journal -> inode -> cgs -> indirects.
6522  *
6523  * The algorithm is as follows:
6524  * 1) Traverse the in-memory state and create journal entries to release
6525  *    the relevant blocks and full indirect trees.
6526  * 2) Traverse the indirect block chain adding partial truncation freework
6527  *    records to indirects in the path to lastlbn.  The freework will
6528  *    prevent new allocation dependencies from being satisfied in this
6529  *    indirect until the truncation completes.
6530  * 3) Read and lock the inode block, performing an update with the new size
6531  *    and pointers.  This prevents truncated data from becoming valid on
6532  *    disk through step 4.
6533  * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6534  *    eliminate journal work for those records that do not require it.
6535  * 5) Schedule the journal records to be written followed by the inode block.
6536  * 6) Allocate any necessary frags for the end of file.
6537  * 7) Zero any partially truncated blocks.
6538  *
6539  * From this truncation proceeds asynchronously using the freework and
6540  * indir_trunc machinery.  The file will not be extended again into a
6541  * partially truncated indirect block until all work is completed but
6542  * the normal dependency mechanism ensures that it is rolled back/forward
6543  * as appropriate.  Further truncation may occur without delay and is
6544  * serialized in indir_trunc().
6545  */
6546 void
6547 softdep_journal_freeblocks(ip, cred, length, flags)
6548 	struct inode *ip;	/* The inode whose length is to be reduced */
6549 	struct ucred *cred;
6550 	off_t length;		/* The new length for the file */
6551 	int flags;		/* IO_EXT and/or IO_NORMAL */
6552 {
6553 	struct freeblks *freeblks, *fbn;
6554 	struct worklist *wk, *wkn;
6555 	struct inodedep *inodedep;
6556 	struct jblkdep *jblkdep;
6557 	struct allocdirect *adp, *adpn;
6558 	struct ufsmount *ump;
6559 	struct fs *fs;
6560 	struct buf *bp;
6561 	struct vnode *vp;
6562 	struct mount *mp;
6563 	ufs2_daddr_t extblocks, datablocks;
6564 	ufs_lbn_t tmpval, lbn, lastlbn;
6565 	int frags, lastoff, iboff, allocblock, needj, error, i;
6566 
6567 	ump = ITOUMP(ip);
6568 	mp = UFSTOVFS(ump);
6569 	fs = ump->um_fs;
6570 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6571 	    ("softdep_journal_freeblocks called on non-softdep filesystem"));
6572 	vp = ITOV(ip);
6573 	needj = 1;
6574 	iboff = -1;
6575 	allocblock = 0;
6576 	extblocks = 0;
6577 	datablocks = 0;
6578 	frags = 0;
6579 	freeblks = newfreeblks(mp, ip);
6580 	ACQUIRE_LOCK(ump);
6581 	/*
6582 	 * If we're truncating a removed file that will never be written
6583 	 * we don't need to journal the block frees.  The canceled journals
6584 	 * for the allocations will suffice.
6585 	 */
6586 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6587 	if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6588 	    length == 0)
6589 		needj = 0;
6590 	CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6591 	    ip->i_number, length, needj);
6592 	FREE_LOCK(ump);
6593 	/*
6594 	 * Calculate the lbn that we are truncating to.  This results in -1
6595 	 * if we're truncating the 0 bytes.  So it is the last lbn we want
6596 	 * to keep, not the first lbn we want to truncate.
6597 	 */
6598 	lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6599 	lastoff = blkoff(fs, length);
6600 	/*
6601 	 * Compute frags we are keeping in lastlbn.  0 means all.
6602 	 */
6603 	if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6604 		frags = fragroundup(fs, lastoff);
6605 		/* adp offset of last valid allocdirect. */
6606 		iboff = lastlbn;
6607 	} else if (lastlbn > 0)
6608 		iboff = UFS_NDADDR;
6609 	if (fs->fs_magic == FS_UFS2_MAGIC)
6610 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6611 	/*
6612 	 * Handle normal data blocks and indirects.  This section saves
6613 	 * values used after the inode update to complete frag and indirect
6614 	 * truncation.
6615 	 */
6616 	if ((flags & IO_NORMAL) != 0) {
6617 		/*
6618 		 * Handle truncation of whole direct and indirect blocks.
6619 		 */
6620 		for (i = iboff + 1; i < UFS_NDADDR; i++)
6621 			setup_freedirect(freeblks, ip, i, needj);
6622 		for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6623 		    i < UFS_NIADDR;
6624 		    i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6625 			/* Release a whole indirect tree. */
6626 			if (lbn > lastlbn) {
6627 				setup_freeindir(freeblks, ip, i, -lbn -i,
6628 				    needj);
6629 				continue;
6630 			}
6631 			iboff = i + UFS_NDADDR;
6632 			/*
6633 			 * Traverse partially truncated indirect tree.
6634 			 */
6635 			if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6636 				setup_trunc_indir(freeblks, ip, -lbn - i,
6637 				    lastlbn, DIP(ip, i_ib[i]));
6638 		}
6639 		/*
6640 		 * Handle partial truncation to a frag boundary.
6641 		 */
6642 		if (frags) {
6643 			ufs2_daddr_t blkno;
6644 			long oldfrags;
6645 
6646 			oldfrags = blksize(fs, ip, lastlbn);
6647 			blkno = DIP(ip, i_db[lastlbn]);
6648 			if (blkno && oldfrags != frags) {
6649 				oldfrags -= frags;
6650 				oldfrags = numfrags(fs, oldfrags);
6651 				blkno += numfrags(fs, frags);
6652 				newfreework(ump, freeblks, NULL, lastlbn,
6653 				    blkno, oldfrags, 0, needj);
6654 				if (needj)
6655 					adjust_newfreework(freeblks,
6656 					    numfrags(fs, frags));
6657 			} else if (blkno == 0)
6658 				allocblock = 1;
6659 		}
6660 		/*
6661 		 * Add a journal record for partial truncate if we are
6662 		 * handling indirect blocks.  Non-indirects need no extra
6663 		 * journaling.
6664 		 */
6665 		if (length != 0 && lastlbn >= UFS_NDADDR) {
6666 			ip->i_flag |= IN_TRUNCATED;
6667 			newjtrunc(freeblks, length, 0);
6668 		}
6669 		ip->i_size = length;
6670 		DIP_SET(ip, i_size, ip->i_size);
6671 		datablocks = DIP(ip, i_blocks) - extblocks;
6672 		if (length != 0)
6673 			datablocks = blkcount(fs, datablocks, length);
6674 		freeblks->fb_len = length;
6675 	}
6676 	if ((flags & IO_EXT) != 0) {
6677 		for (i = 0; i < UFS_NXADDR; i++)
6678 			setup_freeext(freeblks, ip, i, needj);
6679 		ip->i_din2->di_extsize = 0;
6680 		datablocks += extblocks;
6681 	}
6682 #ifdef QUOTA
6683 	/* Reference the quotas in case the block count is wrong in the end. */
6684 	quotaref(vp, freeblks->fb_quota);
6685 	(void) chkdq(ip, -datablocks, NOCRED, 0);
6686 #endif
6687 	freeblks->fb_chkcnt = -datablocks;
6688 	UFS_LOCK(ump);
6689 	fs->fs_pendingblocks += datablocks;
6690 	UFS_UNLOCK(ump);
6691 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6692 	/*
6693 	 * Handle truncation of incomplete alloc direct dependencies.  We
6694 	 * hold the inode block locked to prevent incomplete dependencies
6695 	 * from reaching the disk while we are eliminating those that
6696 	 * have been truncated.  This is a partially inlined ffs_update().
6697 	 */
6698 	ufs_itimes(vp);
6699 	ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
6700 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6701 	    (int)fs->fs_bsize, cred, &bp);
6702 	if (error) {
6703 		brelse(bp);
6704 		softdep_error("softdep_journal_freeblocks", error);
6705 		return;
6706 	}
6707 	if (bp->b_bufsize == fs->fs_bsize)
6708 		bp->b_flags |= B_CLUSTEROK;
6709 	softdep_update_inodeblock(ip, bp, 0);
6710 	if (ump->um_fstype == UFS1) {
6711 		*((struct ufs1_dinode *)bp->b_data +
6712 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
6713 	} else {
6714 		ffs_update_dinode_ckhash(fs, ip->i_din2);
6715 		*((struct ufs2_dinode *)bp->b_data +
6716 		    ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
6717 	}
6718 	ACQUIRE_LOCK(ump);
6719 	(void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6720 	if ((inodedep->id_state & IOSTARTED) != 0)
6721 		panic("softdep_setup_freeblocks: inode busy");
6722 	/*
6723 	 * Add the freeblks structure to the list of operations that
6724 	 * must await the zero'ed inode being written to disk. If we
6725 	 * still have a bitmap dependency (needj), then the inode
6726 	 * has never been written to disk, so we can process the
6727 	 * freeblks below once we have deleted the dependencies.
6728 	 */
6729 	if (needj)
6730 		WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6731 	else
6732 		freeblks->fb_state |= COMPLETE;
6733 	if ((flags & IO_NORMAL) != 0) {
6734 		TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
6735 			if (adp->ad_offset > iboff)
6736 				cancel_allocdirect(&inodedep->id_inoupdt, adp,
6737 				    freeblks);
6738 			/*
6739 			 * Truncate the allocdirect.  We could eliminate
6740 			 * or modify journal records as well.
6741 			 */
6742 			else if (adp->ad_offset == iboff && frags)
6743 				adp->ad_newsize = frags;
6744 		}
6745 	}
6746 	if ((flags & IO_EXT) != 0)
6747 		while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
6748 			cancel_allocdirect(&inodedep->id_extupdt, adp,
6749 			    freeblks);
6750 	/*
6751 	 * Scan the bufwait list for newblock dependencies that will never
6752 	 * make it to disk.
6753 	 */
6754 	LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6755 		if (wk->wk_type != D_ALLOCDIRECT)
6756 			continue;
6757 		adp = WK_ALLOCDIRECT(wk);
6758 		if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
6759 		    ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
6760 			cancel_jfreeblk(freeblks, adp->ad_newblkno);
6761 			cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
6762 			WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
6763 		}
6764 	}
6765 	/*
6766 	 * Add journal work.
6767 	 */
6768 	LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6769 		add_to_journal(&jblkdep->jb_list);
6770 	FREE_LOCK(ump);
6771 	bdwrite(bp);
6772 	/*
6773 	 * Truncate dependency structures beyond length.
6774 	 */
6775 	trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
6776 	/*
6777 	 * This is only set when we need to allocate a fragment because
6778 	 * none existed at the end of a frag-sized file.  It handles only
6779 	 * allocating a new, zero filled block.
6780 	 */
6781 	if (allocblock) {
6782 		ip->i_size = length - lastoff;
6783 		DIP_SET(ip, i_size, ip->i_size);
6784 		error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
6785 		if (error != 0) {
6786 			softdep_error("softdep_journal_freeblks", error);
6787 			return;
6788 		}
6789 		ip->i_size = length;
6790 		DIP_SET(ip, i_size, length);
6791 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
6792 		allocbuf(bp, frags);
6793 		ffs_update(vp, 0);
6794 		bawrite(bp);
6795 	} else if (lastoff != 0 && vp->v_type != VDIR) {
6796 		int size;
6797 
6798 		/*
6799 		 * Zero the end of a truncated frag or block.
6800 		 */
6801 		size = sblksize(fs, length, lastlbn);
6802 		error = bread(vp, lastlbn, size, cred, &bp);
6803 		if (error) {
6804 			softdep_error("softdep_journal_freeblks", error);
6805 			return;
6806 		}
6807 		bzero((char *)bp->b_data + lastoff, size - lastoff);
6808 		bawrite(bp);
6809 
6810 	}
6811 	ACQUIRE_LOCK(ump);
6812 	inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6813 	TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
6814 	freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
6815 	/*
6816 	 * We zero earlier truncations so they don't erroneously
6817 	 * update i_blocks.
6818 	 */
6819 	if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
6820 		TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
6821 			fbn->fb_len = 0;
6822 	if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
6823 	    LIST_EMPTY(&freeblks->fb_jblkdephd))
6824 		freeblks->fb_state |= INPROGRESS;
6825 	else
6826 		freeblks = NULL;
6827 	FREE_LOCK(ump);
6828 	if (freeblks)
6829 		handle_workitem_freeblocks(freeblks, 0);
6830 	trunc_pages(ip, length, extblocks, flags);
6831 
6832 }
6833 
6834 /*
6835  * Flush a JOP_SYNC to the journal.
6836  */
6837 void
6838 softdep_journal_fsync(ip)
6839 	struct inode *ip;
6840 {
6841 	struct jfsync *jfsync;
6842 	struct ufsmount *ump;
6843 
6844 	ump = ITOUMP(ip);
6845 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6846 	    ("softdep_journal_fsync called on non-softdep filesystem"));
6847 	if ((ip->i_flag & IN_TRUNCATED) == 0)
6848 		return;
6849 	ip->i_flag &= ~IN_TRUNCATED;
6850 	jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
6851 	workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
6852 	jfsync->jfs_size = ip->i_size;
6853 	jfsync->jfs_ino = ip->i_number;
6854 	ACQUIRE_LOCK(ump);
6855 	add_to_journal(&jfsync->jfs_list);
6856 	jwait(&jfsync->jfs_list, MNT_WAIT);
6857 	FREE_LOCK(ump);
6858 }
6859 
6860 /*
6861  * Block de-allocation dependencies.
6862  *
6863  * When blocks are de-allocated, the on-disk pointers must be nullified before
6864  * the blocks are made available for use by other files.  (The true
6865  * requirement is that old pointers must be nullified before new on-disk
6866  * pointers are set.  We chose this slightly more stringent requirement to
6867  * reduce complexity.) Our implementation handles this dependency by updating
6868  * the inode (or indirect block) appropriately but delaying the actual block
6869  * de-allocation (i.e., freemap and free space count manipulation) until
6870  * after the updated versions reach stable storage.  After the disk is
6871  * updated, the blocks can be safely de-allocated whenever it is convenient.
6872  * This implementation handles only the common case of reducing a file's
6873  * length to zero. Other cases are handled by the conventional synchronous
6874  * write approach.
6875  *
6876  * The ffs implementation with which we worked double-checks
6877  * the state of the block pointers and file size as it reduces
6878  * a file's length.  Some of this code is replicated here in our
6879  * soft updates implementation.  The freeblks->fb_chkcnt field is
6880  * used to transfer a part of this information to the procedure
6881  * that eventually de-allocates the blocks.
6882  *
6883  * This routine should be called from the routine that shortens
6884  * a file's length, before the inode's size or block pointers
6885  * are modified. It will save the block pointer information for
6886  * later release and zero the inode so that the calling routine
6887  * can release it.
6888  */
6889 void
6890 softdep_setup_freeblocks(ip, length, flags)
6891 	struct inode *ip;	/* The inode whose length is to be reduced */
6892 	off_t length;		/* The new length for the file */
6893 	int flags;		/* IO_EXT and/or IO_NORMAL */
6894 {
6895 	struct ufs1_dinode *dp1;
6896 	struct ufs2_dinode *dp2;
6897 	struct freeblks *freeblks;
6898 	struct inodedep *inodedep;
6899 	struct allocdirect *adp;
6900 	struct ufsmount *ump;
6901 	struct buf *bp;
6902 	struct fs *fs;
6903 	ufs2_daddr_t extblocks, datablocks;
6904 	struct mount *mp;
6905 	int i, delay, error;
6906 	ufs_lbn_t tmpval;
6907 	ufs_lbn_t lbn;
6908 
6909 	ump = ITOUMP(ip);
6910 	mp = UFSTOVFS(ump);
6911 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6912 	    ("softdep_setup_freeblocks called on non-softdep filesystem"));
6913 	CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
6914 	    ip->i_number, length);
6915 	KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
6916 	fs = ump->um_fs;
6917 	if ((error = bread(ump->um_devvp,
6918 	    fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6919 	    (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
6920 		brelse(bp);
6921 		softdep_error("softdep_setup_freeblocks", error);
6922 		return;
6923 	}
6924 	freeblks = newfreeblks(mp, ip);
6925 	extblocks = 0;
6926 	datablocks = 0;
6927 	if (fs->fs_magic == FS_UFS2_MAGIC)
6928 		extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6929 	if ((flags & IO_NORMAL) != 0) {
6930 		for (i = 0; i < UFS_NDADDR; i++)
6931 			setup_freedirect(freeblks, ip, i, 0);
6932 		for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6933 		    i < UFS_NIADDR;
6934 		    i++, lbn += tmpval, tmpval *= NINDIR(fs))
6935 			setup_freeindir(freeblks, ip, i, -lbn -i, 0);
6936 		ip->i_size = 0;
6937 		DIP_SET(ip, i_size, 0);
6938 		datablocks = DIP(ip, i_blocks) - extblocks;
6939 	}
6940 	if ((flags & IO_EXT) != 0) {
6941 		for (i = 0; i < UFS_NXADDR; i++)
6942 			setup_freeext(freeblks, ip, i, 0);
6943 		ip->i_din2->di_extsize = 0;
6944 		datablocks += extblocks;
6945 	}
6946 #ifdef QUOTA
6947 	/* Reference the quotas in case the block count is wrong in the end. */
6948 	quotaref(ITOV(ip), freeblks->fb_quota);
6949 	(void) chkdq(ip, -datablocks, NOCRED, 0);
6950 #endif
6951 	freeblks->fb_chkcnt = -datablocks;
6952 	UFS_LOCK(ump);
6953 	fs->fs_pendingblocks += datablocks;
6954 	UFS_UNLOCK(ump);
6955 	DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6956 	/*
6957 	 * Push the zero'ed inode to its disk buffer so that we are free
6958 	 * to delete its dependencies below. Once the dependencies are gone
6959 	 * the buffer can be safely released.
6960 	 */
6961 	if (ump->um_fstype == UFS1) {
6962 		dp1 = ((struct ufs1_dinode *)bp->b_data +
6963 		    ino_to_fsbo(fs, ip->i_number));
6964 		ip->i_din1->di_freelink = dp1->di_freelink;
6965 		*dp1 = *ip->i_din1;
6966 	} else {
6967 		dp2 = ((struct ufs2_dinode *)bp->b_data +
6968 		    ino_to_fsbo(fs, ip->i_number));
6969 		ip->i_din2->di_freelink = dp2->di_freelink;
6970 		ffs_update_dinode_ckhash(fs, ip->i_din2);
6971 		*dp2 = *ip->i_din2;
6972 	}
6973 	/*
6974 	 * Find and eliminate any inode dependencies.
6975 	 */
6976 	ACQUIRE_LOCK(ump);
6977 	(void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6978 	if ((inodedep->id_state & IOSTARTED) != 0)
6979 		panic("softdep_setup_freeblocks: inode busy");
6980 	/*
6981 	 * Add the freeblks structure to the list of operations that
6982 	 * must await the zero'ed inode being written to disk. If we
6983 	 * still have a bitmap dependency (delay == 0), then the inode
6984 	 * has never been written to disk, so we can process the
6985 	 * freeblks below once we have deleted the dependencies.
6986 	 */
6987 	delay = (inodedep->id_state & DEPCOMPLETE);
6988 	if (delay)
6989 		WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6990 	else
6991 		freeblks->fb_state |= COMPLETE;
6992 	/*
6993 	 * Because the file length has been truncated to zero, any
6994 	 * pending block allocation dependency structures associated
6995 	 * with this inode are obsolete and can simply be de-allocated.
6996 	 * We must first merge the two dependency lists to get rid of
6997 	 * any duplicate freefrag structures, then purge the merged list.
6998 	 * If we still have a bitmap dependency, then the inode has never
6999 	 * been written to disk, so we can free any fragments without delay.
7000 	 */
7001 	if (flags & IO_NORMAL) {
7002 		merge_inode_lists(&inodedep->id_newinoupdt,
7003 		    &inodedep->id_inoupdt);
7004 		while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7005 			cancel_allocdirect(&inodedep->id_inoupdt, adp,
7006 			    freeblks);
7007 	}
7008 	if (flags & IO_EXT) {
7009 		merge_inode_lists(&inodedep->id_newextupdt,
7010 		    &inodedep->id_extupdt);
7011 		while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7012 			cancel_allocdirect(&inodedep->id_extupdt, adp,
7013 			    freeblks);
7014 	}
7015 	FREE_LOCK(ump);
7016 	bdwrite(bp);
7017 	trunc_dependencies(ip, freeblks, -1, 0, flags);
7018 	ACQUIRE_LOCK(ump);
7019 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7020 		(void) free_inodedep(inodedep);
7021 	freeblks->fb_state |= DEPCOMPLETE;
7022 	/*
7023 	 * If the inode with zeroed block pointers is now on disk
7024 	 * we can start freeing blocks.
7025 	 */
7026 	if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7027 		freeblks->fb_state |= INPROGRESS;
7028 	else
7029 		freeblks = NULL;
7030 	FREE_LOCK(ump);
7031 	if (freeblks)
7032 		handle_workitem_freeblocks(freeblks, 0);
7033 	trunc_pages(ip, length, extblocks, flags);
7034 }
7035 
7036 /*
7037  * Eliminate pages from the page cache that back parts of this inode and
7038  * adjust the vnode pager's idea of our size.  This prevents stale data
7039  * from hanging around in the page cache.
7040  */
7041 static void
7042 trunc_pages(ip, length, extblocks, flags)
7043 	struct inode *ip;
7044 	off_t length;
7045 	ufs2_daddr_t extblocks;
7046 	int flags;
7047 {
7048 	struct vnode *vp;
7049 	struct fs *fs;
7050 	ufs_lbn_t lbn;
7051 	off_t end, extend;
7052 
7053 	vp = ITOV(ip);
7054 	fs = ITOFS(ip);
7055 	extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7056 	if ((flags & IO_EXT) != 0)
7057 		vn_pages_remove(vp, extend, 0);
7058 	if ((flags & IO_NORMAL) == 0)
7059 		return;
7060 	BO_LOCK(&vp->v_bufobj);
7061 	drain_output(vp);
7062 	BO_UNLOCK(&vp->v_bufobj);
7063 	/*
7064 	 * The vnode pager eliminates file pages we eliminate indirects
7065 	 * below.
7066 	 */
7067 	vnode_pager_setsize(vp, length);
7068 	/*
7069 	 * Calculate the end based on the last indirect we want to keep.  If
7070 	 * the block extends into indirects we can just use the negative of
7071 	 * its lbn.  Doubles and triples exist at lower numbers so we must
7072 	 * be careful not to remove those, if they exist.  double and triple
7073 	 * indirect lbns do not overlap with others so it is not important
7074 	 * to verify how many levels are required.
7075 	 */
7076 	lbn = lblkno(fs, length);
7077 	if (lbn >= UFS_NDADDR) {
7078 		/* Calculate the virtual lbn of the triple indirect. */
7079 		lbn = -lbn - (UFS_NIADDR - 1);
7080 		end = OFF_TO_IDX(lblktosize(fs, lbn));
7081 	} else
7082 		end = extend;
7083 	vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7084 }
7085 
7086 /*
7087  * See if the buf bp is in the range eliminated by truncation.
7088  */
7089 static int
7090 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7091 	struct buf *bp;
7092 	int *blkoffp;
7093 	ufs_lbn_t lastlbn;
7094 	int lastoff;
7095 	int flags;
7096 {
7097 	ufs_lbn_t lbn;
7098 
7099 	*blkoffp = 0;
7100 	/* Only match ext/normal blocks as appropriate. */
7101 	if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7102 	    ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7103 		return (0);
7104 	/* ALTDATA is always a full truncation. */
7105 	if ((bp->b_xflags & BX_ALTDATA) != 0)
7106 		return (1);
7107 	/* -1 is full truncation. */
7108 	if (lastlbn == -1)
7109 		return (1);
7110 	/*
7111 	 * If this is a partial truncate we only want those
7112 	 * blocks and indirect blocks that cover the range
7113 	 * we're after.
7114 	 */
7115 	lbn = bp->b_lblkno;
7116 	if (lbn < 0)
7117 		lbn = -(lbn + lbn_level(lbn));
7118 	if (lbn < lastlbn)
7119 		return (0);
7120 	/* Here we only truncate lblkno if it's partial. */
7121 	if (lbn == lastlbn) {
7122 		if (lastoff == 0)
7123 			return (0);
7124 		*blkoffp = lastoff;
7125 	}
7126 	return (1);
7127 }
7128 
7129 /*
7130  * Eliminate any dependencies that exist in memory beyond lblkno:off
7131  */
7132 static void
7133 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7134 	struct inode *ip;
7135 	struct freeblks *freeblks;
7136 	ufs_lbn_t lastlbn;
7137 	int lastoff;
7138 	int flags;
7139 {
7140 	struct bufobj *bo;
7141 	struct vnode *vp;
7142 	struct buf *bp;
7143 	int blkoff;
7144 
7145 	/*
7146 	 * We must wait for any I/O in progress to finish so that
7147 	 * all potential buffers on the dirty list will be visible.
7148 	 * Once they are all there, walk the list and get rid of
7149 	 * any dependencies.
7150 	 */
7151 	vp = ITOV(ip);
7152 	bo = &vp->v_bufobj;
7153 	BO_LOCK(bo);
7154 	drain_output(vp);
7155 	TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7156 		bp->b_vflags &= ~BV_SCANNED;
7157 restart:
7158 	TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7159 		if (bp->b_vflags & BV_SCANNED)
7160 			continue;
7161 		if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7162 			bp->b_vflags |= BV_SCANNED;
7163 			continue;
7164 		}
7165 		KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7166 		if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7167 			goto restart;
7168 		BO_UNLOCK(bo);
7169 		if (deallocate_dependencies(bp, freeblks, blkoff))
7170 			bqrelse(bp);
7171 		else
7172 			brelse(bp);
7173 		BO_LOCK(bo);
7174 		goto restart;
7175 	}
7176 	/*
7177 	 * Now do the work of vtruncbuf while also matching indirect blocks.
7178 	 */
7179 	TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7180 		bp->b_vflags &= ~BV_SCANNED;
7181 cleanrestart:
7182 	TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7183 		if (bp->b_vflags & BV_SCANNED)
7184 			continue;
7185 		if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7186 			bp->b_vflags |= BV_SCANNED;
7187 			continue;
7188 		}
7189 		if (BUF_LOCK(bp,
7190 		    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7191 		    BO_LOCKPTR(bo)) == ENOLCK) {
7192 			BO_LOCK(bo);
7193 			goto cleanrestart;
7194 		}
7195 		bp->b_vflags |= BV_SCANNED;
7196 		bremfree(bp);
7197 		if (blkoff != 0) {
7198 			allocbuf(bp, blkoff);
7199 			bqrelse(bp);
7200 		} else {
7201 			bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7202 			brelse(bp);
7203 		}
7204 		BO_LOCK(bo);
7205 		goto cleanrestart;
7206 	}
7207 	drain_output(vp);
7208 	BO_UNLOCK(bo);
7209 }
7210 
7211 static int
7212 cancel_pagedep(pagedep, freeblks, blkoff)
7213 	struct pagedep *pagedep;
7214 	struct freeblks *freeblks;
7215 	int blkoff;
7216 {
7217 	struct jremref *jremref;
7218 	struct jmvref *jmvref;
7219 	struct dirrem *dirrem, *tmp;
7220 	int i;
7221 
7222 	/*
7223 	 * Copy any directory remove dependencies to the list
7224 	 * to be processed after the freeblks proceeds.  If
7225 	 * directory entry never made it to disk they
7226 	 * can be dumped directly onto the work list.
7227 	 */
7228 	LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7229 		/* Skip this directory removal if it is intended to remain. */
7230 		if (dirrem->dm_offset < blkoff)
7231 			continue;
7232 		/*
7233 		 * If there are any dirrems we wait for the journal write
7234 		 * to complete and then restart the buf scan as the lock
7235 		 * has been dropped.
7236 		 */
7237 		while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7238 			jwait(&jremref->jr_list, MNT_WAIT);
7239 			return (ERESTART);
7240 		}
7241 		LIST_REMOVE(dirrem, dm_next);
7242 		dirrem->dm_dirinum = pagedep->pd_ino;
7243 		WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7244 	}
7245 	while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7246 		jwait(&jmvref->jm_list, MNT_WAIT);
7247 		return (ERESTART);
7248 	}
7249 	/*
7250 	 * When we're partially truncating a pagedep we just want to flush
7251 	 * journal entries and return.  There can not be any adds in the
7252 	 * truncated portion of the directory and newblk must remain if
7253 	 * part of the block remains.
7254 	 */
7255 	if (blkoff != 0) {
7256 		struct diradd *dap;
7257 
7258 		LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7259 			if (dap->da_offset > blkoff)
7260 				panic("cancel_pagedep: diradd %p off %d > %d",
7261 				    dap, dap->da_offset, blkoff);
7262 		for (i = 0; i < DAHASHSZ; i++)
7263 			LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7264 				if (dap->da_offset > blkoff)
7265 					panic("cancel_pagedep: diradd %p off %d > %d",
7266 					    dap, dap->da_offset, blkoff);
7267 		return (0);
7268 	}
7269 	/*
7270 	 * There should be no directory add dependencies present
7271 	 * as the directory could not be truncated until all
7272 	 * children were removed.
7273 	 */
7274 	KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7275 	    ("deallocate_dependencies: pendinghd != NULL"));
7276 	for (i = 0; i < DAHASHSZ; i++)
7277 		KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7278 		    ("deallocate_dependencies: diraddhd != NULL"));
7279 	if ((pagedep->pd_state & NEWBLOCK) != 0)
7280 		free_newdirblk(pagedep->pd_newdirblk);
7281 	if (free_pagedep(pagedep) == 0)
7282 		panic("Failed to free pagedep %p", pagedep);
7283 	return (0);
7284 }
7285 
7286 /*
7287  * Reclaim any dependency structures from a buffer that is about to
7288  * be reallocated to a new vnode. The buffer must be locked, thus,
7289  * no I/O completion operations can occur while we are manipulating
7290  * its associated dependencies. The mutex is held so that other I/O's
7291  * associated with related dependencies do not occur.
7292  */
7293 static int
7294 deallocate_dependencies(bp, freeblks, off)
7295 	struct buf *bp;
7296 	struct freeblks *freeblks;
7297 	int off;
7298 {
7299 	struct indirdep *indirdep;
7300 	struct pagedep *pagedep;
7301 	struct worklist *wk, *wkn;
7302 	struct ufsmount *ump;
7303 
7304 	ump = softdep_bp_to_mp(bp);
7305 	if (ump == NULL)
7306 		goto done;
7307 	ACQUIRE_LOCK(ump);
7308 	LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7309 		switch (wk->wk_type) {
7310 		case D_INDIRDEP:
7311 			indirdep = WK_INDIRDEP(wk);
7312 			if (bp->b_lblkno >= 0 ||
7313 			    bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7314 				panic("deallocate_dependencies: not indir");
7315 			cancel_indirdep(indirdep, bp, freeblks);
7316 			continue;
7317 
7318 		case D_PAGEDEP:
7319 			pagedep = WK_PAGEDEP(wk);
7320 			if (cancel_pagedep(pagedep, freeblks, off)) {
7321 				FREE_LOCK(ump);
7322 				return (ERESTART);
7323 			}
7324 			continue;
7325 
7326 		case D_ALLOCINDIR:
7327 			/*
7328 			 * Simply remove the allocindir, we'll find it via
7329 			 * the indirdep where we can clear pointers if
7330 			 * needed.
7331 			 */
7332 			WORKLIST_REMOVE(wk);
7333 			continue;
7334 
7335 		case D_FREEWORK:
7336 			/*
7337 			 * A truncation is waiting for the zero'd pointers
7338 			 * to be written.  It can be freed when the freeblks
7339 			 * is journaled.
7340 			 */
7341 			WORKLIST_REMOVE(wk);
7342 			wk->wk_state |= ONDEPLIST;
7343 			WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7344 			break;
7345 
7346 		case D_ALLOCDIRECT:
7347 			if (off != 0)
7348 				continue;
7349 			/* FALLTHROUGH */
7350 		default:
7351 			panic("deallocate_dependencies: Unexpected type %s",
7352 			    TYPENAME(wk->wk_type));
7353 			/* NOTREACHED */
7354 		}
7355 	}
7356 	FREE_LOCK(ump);
7357 done:
7358 	/*
7359 	 * Don't throw away this buf, we were partially truncating and
7360 	 * some deps may always remain.
7361 	 */
7362 	if (off) {
7363 		allocbuf(bp, off);
7364 		bp->b_vflags |= BV_SCANNED;
7365 		return (EBUSY);
7366 	}
7367 	bp->b_flags |= B_INVAL | B_NOCACHE;
7368 
7369 	return (0);
7370 }
7371 
7372 /*
7373  * An allocdirect is being canceled due to a truncate.  We must make sure
7374  * the journal entry is released in concert with the blkfree that releases
7375  * the storage.  Completed journal entries must not be released until the
7376  * space is no longer pointed to by the inode or in the bitmap.
7377  */
7378 static void
7379 cancel_allocdirect(adphead, adp, freeblks)
7380 	struct allocdirectlst *adphead;
7381 	struct allocdirect *adp;
7382 	struct freeblks *freeblks;
7383 {
7384 	struct freework *freework;
7385 	struct newblk *newblk;
7386 	struct worklist *wk;
7387 
7388 	TAILQ_REMOVE(adphead, adp, ad_next);
7389 	newblk = (struct newblk *)adp;
7390 	freework = NULL;
7391 	/*
7392 	 * Find the correct freework structure.
7393 	 */
7394 	LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7395 		if (wk->wk_type != D_FREEWORK)
7396 			continue;
7397 		freework = WK_FREEWORK(wk);
7398 		if (freework->fw_blkno == newblk->nb_newblkno)
7399 			break;
7400 	}
7401 	if (freework == NULL)
7402 		panic("cancel_allocdirect: Freework not found");
7403 	/*
7404 	 * If a newblk exists at all we still have the journal entry that
7405 	 * initiated the allocation so we do not need to journal the free.
7406 	 */
7407 	cancel_jfreeblk(freeblks, freework->fw_blkno);
7408 	/*
7409 	 * If the journal hasn't been written the jnewblk must be passed
7410 	 * to the call to ffs_blkfree that reclaims the space.  We accomplish
7411 	 * this by linking the journal dependency into the freework to be
7412 	 * freed when freework_freeblock() is called.  If the journal has
7413 	 * been written we can simply reclaim the journal space when the
7414 	 * freeblks work is complete.
7415 	 */
7416 	freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7417 	    &freeblks->fb_jwork);
7418 	WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7419 }
7420 
7421 
7422 /*
7423  * Cancel a new block allocation.  May be an indirect or direct block.  We
7424  * remove it from various lists and return any journal record that needs to
7425  * be resolved by the caller.
7426  *
7427  * A special consideration is made for indirects which were never pointed
7428  * at on disk and will never be found once this block is released.
7429  */
7430 static struct jnewblk *
7431 cancel_newblk(newblk, wk, wkhd)
7432 	struct newblk *newblk;
7433 	struct worklist *wk;
7434 	struct workhead *wkhd;
7435 {
7436 	struct jnewblk *jnewblk;
7437 
7438 	CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7439 
7440 	newblk->nb_state |= GOINGAWAY;
7441 	/*
7442 	 * Previously we traversed the completedhd on each indirdep
7443 	 * attached to this newblk to cancel them and gather journal
7444 	 * work.  Since we need only the oldest journal segment and
7445 	 * the lowest point on the tree will always have the oldest
7446 	 * journal segment we are free to release the segments
7447 	 * of any subordinates and may leave the indirdep list to
7448 	 * indirdep_complete() when this newblk is freed.
7449 	 */
7450 	if (newblk->nb_state & ONDEPLIST) {
7451 		newblk->nb_state &= ~ONDEPLIST;
7452 		LIST_REMOVE(newblk, nb_deps);
7453 	}
7454 	if (newblk->nb_state & ONWORKLIST)
7455 		WORKLIST_REMOVE(&newblk->nb_list);
7456 	/*
7457 	 * If the journal entry hasn't been written we save a pointer to
7458 	 * the dependency that frees it until it is written or the
7459 	 * superseding operation completes.
7460 	 */
7461 	jnewblk = newblk->nb_jnewblk;
7462 	if (jnewblk != NULL && wk != NULL) {
7463 		newblk->nb_jnewblk = NULL;
7464 		jnewblk->jn_dep = wk;
7465 	}
7466 	if (!LIST_EMPTY(&newblk->nb_jwork))
7467 		jwork_move(wkhd, &newblk->nb_jwork);
7468 	/*
7469 	 * When truncating we must free the newdirblk early to remove
7470 	 * the pagedep from the hash before returning.
7471 	 */
7472 	if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7473 		free_newdirblk(WK_NEWDIRBLK(wk));
7474 	if (!LIST_EMPTY(&newblk->nb_newdirblk))
7475 		panic("cancel_newblk: extra newdirblk");
7476 
7477 	return (jnewblk);
7478 }
7479 
7480 /*
7481  * Schedule the freefrag associated with a newblk to be released once
7482  * the pointers are written and the previous block is no longer needed.
7483  */
7484 static void
7485 newblk_freefrag(newblk)
7486 	struct newblk *newblk;
7487 {
7488 	struct freefrag *freefrag;
7489 
7490 	if (newblk->nb_freefrag == NULL)
7491 		return;
7492 	freefrag = newblk->nb_freefrag;
7493 	newblk->nb_freefrag = NULL;
7494 	freefrag->ff_state |= COMPLETE;
7495 	if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7496 		add_to_worklist(&freefrag->ff_list, 0);
7497 }
7498 
7499 /*
7500  * Free a newblk. Generate a new freefrag work request if appropriate.
7501  * This must be called after the inode pointer and any direct block pointers
7502  * are valid or fully removed via truncate or frag extension.
7503  */
7504 static void
7505 free_newblk(newblk)
7506 	struct newblk *newblk;
7507 {
7508 	struct indirdep *indirdep;
7509 	struct worklist *wk;
7510 
7511 	KASSERT(newblk->nb_jnewblk == NULL,
7512 	    ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7513 	KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7514 	    ("free_newblk: unclaimed newblk"));
7515 	LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7516 	newblk_freefrag(newblk);
7517 	if (newblk->nb_state & ONDEPLIST)
7518 		LIST_REMOVE(newblk, nb_deps);
7519 	if (newblk->nb_state & ONWORKLIST)
7520 		WORKLIST_REMOVE(&newblk->nb_list);
7521 	LIST_REMOVE(newblk, nb_hash);
7522 	if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7523 		free_newdirblk(WK_NEWDIRBLK(wk));
7524 	if (!LIST_EMPTY(&newblk->nb_newdirblk))
7525 		panic("free_newblk: extra newdirblk");
7526 	while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7527 		indirdep_complete(indirdep);
7528 	handle_jwork(&newblk->nb_jwork);
7529 	WORKITEM_FREE(newblk, D_NEWBLK);
7530 }
7531 
7532 /*
7533  * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7534  */
7535 static void
7536 free_newdirblk(newdirblk)
7537 	struct newdirblk *newdirblk;
7538 {
7539 	struct pagedep *pagedep;
7540 	struct diradd *dap;
7541 	struct worklist *wk;
7542 
7543 	LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7544 	WORKLIST_REMOVE(&newdirblk->db_list);
7545 	/*
7546 	 * If the pagedep is still linked onto the directory buffer
7547 	 * dependency chain, then some of the entries on the
7548 	 * pd_pendinghd list may not be committed to disk yet. In
7549 	 * this case, we will simply clear the NEWBLOCK flag and
7550 	 * let the pd_pendinghd list be processed when the pagedep
7551 	 * is next written. If the pagedep is no longer on the buffer
7552 	 * dependency chain, then all the entries on the pd_pending
7553 	 * list are committed to disk and we can free them here.
7554 	 */
7555 	pagedep = newdirblk->db_pagedep;
7556 	pagedep->pd_state &= ~NEWBLOCK;
7557 	if ((pagedep->pd_state & ONWORKLIST) == 0) {
7558 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7559 			free_diradd(dap, NULL);
7560 		/*
7561 		 * If no dependencies remain, the pagedep will be freed.
7562 		 */
7563 		free_pagedep(pagedep);
7564 	}
7565 	/* Should only ever be one item in the list. */
7566 	while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7567 		WORKLIST_REMOVE(wk);
7568 		handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7569 	}
7570 	WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7571 }
7572 
7573 /*
7574  * Prepare an inode to be freed. The actual free operation is not
7575  * done until the zero'ed inode has been written to disk.
7576  */
7577 void
7578 softdep_freefile(pvp, ino, mode)
7579 	struct vnode *pvp;
7580 	ino_t ino;
7581 	int mode;
7582 {
7583 	struct inode *ip = VTOI(pvp);
7584 	struct inodedep *inodedep;
7585 	struct freefile *freefile;
7586 	struct freeblks *freeblks;
7587 	struct ufsmount *ump;
7588 
7589 	ump = ITOUMP(ip);
7590 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7591 	    ("softdep_freefile called on non-softdep filesystem"));
7592 	/*
7593 	 * This sets up the inode de-allocation dependency.
7594 	 */
7595 	freefile = malloc(sizeof(struct freefile),
7596 		M_FREEFILE, M_SOFTDEP_FLAGS);
7597 	workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7598 	freefile->fx_mode = mode;
7599 	freefile->fx_oldinum = ino;
7600 	freefile->fx_devvp = ump->um_devvp;
7601 	LIST_INIT(&freefile->fx_jwork);
7602 	UFS_LOCK(ump);
7603 	ump->um_fs->fs_pendinginodes += 1;
7604 	UFS_UNLOCK(ump);
7605 
7606 	/*
7607 	 * If the inodedep does not exist, then the zero'ed inode has
7608 	 * been written to disk. If the allocated inode has never been
7609 	 * written to disk, then the on-disk inode is zero'ed. In either
7610 	 * case we can free the file immediately.  If the journal was
7611 	 * canceled before being written the inode will never make it to
7612 	 * disk and we must send the canceled journal entrys to
7613 	 * ffs_freefile() to be cleared in conjunction with the bitmap.
7614 	 * Any blocks waiting on the inode to write can be safely freed
7615 	 * here as it will never been written.
7616 	 */
7617 	ACQUIRE_LOCK(ump);
7618 	inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7619 	if (inodedep) {
7620 		/*
7621 		 * Clear out freeblks that no longer need to reference
7622 		 * this inode.
7623 		 */
7624 		while ((freeblks =
7625 		    TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7626 			TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7627 			    fb_next);
7628 			freeblks->fb_state &= ~ONDEPLIST;
7629 		}
7630 		/*
7631 		 * Remove this inode from the unlinked list.
7632 		 */
7633 		if (inodedep->id_state & UNLINKED) {
7634 			/*
7635 			 * Save the journal work to be freed with the bitmap
7636 			 * before we clear UNLINKED.  Otherwise it can be lost
7637 			 * if the inode block is written.
7638 			 */
7639 			handle_bufwait(inodedep, &freefile->fx_jwork);
7640 			clear_unlinked_inodedep(inodedep);
7641 			/*
7642 			 * Re-acquire inodedep as we've dropped the
7643 			 * per-filesystem lock in clear_unlinked_inodedep().
7644 			 */
7645 			inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7646 		}
7647 	}
7648 	if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7649 		FREE_LOCK(ump);
7650 		handle_workitem_freefile(freefile);
7651 		return;
7652 	}
7653 	if ((inodedep->id_state & DEPCOMPLETE) == 0)
7654 		inodedep->id_state |= GOINGAWAY;
7655 	WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7656 	FREE_LOCK(ump);
7657 	if (ip->i_number == ino)
7658 		ip->i_flag |= IN_MODIFIED;
7659 }
7660 
7661 /*
7662  * Check to see if an inode has never been written to disk. If
7663  * so free the inodedep and return success, otherwise return failure.
7664  *
7665  * If we still have a bitmap dependency, then the inode has never
7666  * been written to disk. Drop the dependency as it is no longer
7667  * necessary since the inode is being deallocated. We set the
7668  * ALLCOMPLETE flags since the bitmap now properly shows that the
7669  * inode is not allocated. Even if the inode is actively being
7670  * written, it has been rolled back to its zero'ed state, so we
7671  * are ensured that a zero inode is what is on the disk. For short
7672  * lived files, this change will usually result in removing all the
7673  * dependencies from the inode so that it can be freed immediately.
7674  */
7675 static int
7676 check_inode_unwritten(inodedep)
7677 	struct inodedep *inodedep;
7678 {
7679 
7680 	LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7681 
7682 	if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
7683 	    !LIST_EMPTY(&inodedep->id_dirremhd) ||
7684 	    !LIST_EMPTY(&inodedep->id_pendinghd) ||
7685 	    !LIST_EMPTY(&inodedep->id_bufwait) ||
7686 	    !LIST_EMPTY(&inodedep->id_inowait) ||
7687 	    !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7688 	    !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7689 	    !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7690 	    !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7691 	    !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7692 	    !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7693 	    inodedep->id_mkdiradd != NULL ||
7694 	    inodedep->id_nlinkdelta != 0)
7695 		return (0);
7696 	/*
7697 	 * Another process might be in initiate_write_inodeblock_ufs[12]
7698 	 * trying to allocate memory without holding "Softdep Lock".
7699 	 */
7700 	if ((inodedep->id_state & IOSTARTED) != 0 &&
7701 	    inodedep->id_savedino1 == NULL)
7702 		return (0);
7703 
7704 	if (inodedep->id_state & ONDEPLIST)
7705 		LIST_REMOVE(inodedep, id_deps);
7706 	inodedep->id_state &= ~ONDEPLIST;
7707 	inodedep->id_state |= ALLCOMPLETE;
7708 	inodedep->id_bmsafemap = NULL;
7709 	if (inodedep->id_state & ONWORKLIST)
7710 		WORKLIST_REMOVE(&inodedep->id_list);
7711 	if (inodedep->id_savedino1 != NULL) {
7712 		free(inodedep->id_savedino1, M_SAVEDINO);
7713 		inodedep->id_savedino1 = NULL;
7714 	}
7715 	if (free_inodedep(inodedep) == 0)
7716 		panic("check_inode_unwritten: busy inode");
7717 	return (1);
7718 }
7719 
7720 static int
7721 check_inodedep_free(inodedep)
7722 	struct inodedep *inodedep;
7723 {
7724 
7725 	LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7726 	if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
7727 	    !LIST_EMPTY(&inodedep->id_dirremhd) ||
7728 	    !LIST_EMPTY(&inodedep->id_pendinghd) ||
7729 	    !LIST_EMPTY(&inodedep->id_bufwait) ||
7730 	    !LIST_EMPTY(&inodedep->id_inowait) ||
7731 	    !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7732 	    !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7733 	    !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7734 	    !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7735 	    !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7736 	    !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7737 	    inodedep->id_mkdiradd != NULL ||
7738 	    inodedep->id_nlinkdelta != 0 ||
7739 	    inodedep->id_savedino1 != NULL)
7740 		return (0);
7741 	return (1);
7742 }
7743 
7744 /*
7745  * Try to free an inodedep structure. Return 1 if it could be freed.
7746  */
7747 static int
7748 free_inodedep(inodedep)
7749 	struct inodedep *inodedep;
7750 {
7751 
7752 	LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7753 	if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7754 	    !check_inodedep_free(inodedep))
7755 		return (0);
7756 	if (inodedep->id_state & ONDEPLIST)
7757 		LIST_REMOVE(inodedep, id_deps);
7758 	LIST_REMOVE(inodedep, id_hash);
7759 	WORKITEM_FREE(inodedep, D_INODEDEP);
7760 	return (1);
7761 }
7762 
7763 /*
7764  * Free the block referenced by a freework structure.  The parent freeblks
7765  * structure is released and completed when the final cg bitmap reaches
7766  * the disk.  This routine may be freeing a jnewblk which never made it to
7767  * disk in which case we do not have to wait as the operation is undone
7768  * in memory immediately.
7769  */
7770 static void
7771 freework_freeblock(freework, key)
7772 	struct freework *freework;
7773 	u_long key;
7774 {
7775 	struct freeblks *freeblks;
7776 	struct jnewblk *jnewblk;
7777 	struct ufsmount *ump;
7778 	struct workhead wkhd;
7779 	struct fs *fs;
7780 	int bsize;
7781 	int needj;
7782 
7783 	ump = VFSTOUFS(freework->fw_list.wk_mp);
7784 	LOCK_OWNED(ump);
7785 	/*
7786 	 * Handle partial truncate separately.
7787 	 */
7788 	if (freework->fw_indir) {
7789 		complete_trunc_indir(freework);
7790 		return;
7791 	}
7792 	freeblks = freework->fw_freeblks;
7793 	fs = ump->um_fs;
7794 	needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7795 	bsize = lfragtosize(fs, freework->fw_frags);
7796 	LIST_INIT(&wkhd);
7797 	/*
7798 	 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7799 	 * on the indirblk hashtable and prevents premature freeing.
7800 	 */
7801 	freework->fw_state |= DEPCOMPLETE;
7802 	/*
7803 	 * SUJ needs to wait for the segment referencing freed indirect
7804 	 * blocks to expire so that we know the checker will not confuse
7805 	 * a re-allocated indirect block with its old contents.
7806 	 */
7807 	if (needj && freework->fw_lbn <= -UFS_NDADDR)
7808 		indirblk_insert(freework);
7809 	/*
7810 	 * If we are canceling an existing jnewblk pass it to the free
7811 	 * routine, otherwise pass the freeblk which will ultimately
7812 	 * release the freeblks.  If we're not journaling, we can just
7813 	 * free the freeblks immediately.
7814 	 */
7815 	jnewblk = freework->fw_jnewblk;
7816 	if (jnewblk != NULL) {
7817 		cancel_jnewblk(jnewblk, &wkhd);
7818 		needj = 0;
7819 	} else if (needj) {
7820 		freework->fw_state |= DELAYEDFREE;
7821 		freeblks->fb_cgwait++;
7822 		WORKLIST_INSERT(&wkhd, &freework->fw_list);
7823 	}
7824 	FREE_LOCK(ump);
7825 	freeblks_free(ump, freeblks, btodb(bsize));
7826 	CTR4(KTR_SUJ,
7827 	    "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
7828 	    freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
7829 	ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
7830 	    freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
7831 	ACQUIRE_LOCK(ump);
7832 	/*
7833 	 * The jnewblk will be discarded and the bits in the map never
7834 	 * made it to disk.  We can immediately free the freeblk.
7835 	 */
7836 	if (needj == 0)
7837 		handle_written_freework(freework);
7838 }
7839 
7840 /*
7841  * We enqueue freework items that need processing back on the freeblks and
7842  * add the freeblks to the worklist.  This makes it easier to find all work
7843  * required to flush a truncation in process_truncates().
7844  */
7845 static void
7846 freework_enqueue(freework)
7847 	struct freework *freework;
7848 {
7849 	struct freeblks *freeblks;
7850 
7851 	freeblks = freework->fw_freeblks;
7852 	if ((freework->fw_state & INPROGRESS) == 0)
7853 		WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
7854 	if ((freeblks->fb_state &
7855 	    (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
7856 	    LIST_EMPTY(&freeblks->fb_jblkdephd))
7857 		add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7858 }
7859 
7860 /*
7861  * Start, continue, or finish the process of freeing an indirect block tree.
7862  * The free operation may be paused at any point with fw_off containing the
7863  * offset to restart from.  This enables us to implement some flow control
7864  * for large truncates which may fan out and generate a huge number of
7865  * dependencies.
7866  */
7867 static void
7868 handle_workitem_indirblk(freework)
7869 	struct freework *freework;
7870 {
7871 	struct freeblks *freeblks;
7872 	struct ufsmount *ump;
7873 	struct fs *fs;
7874 
7875 	freeblks = freework->fw_freeblks;
7876 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7877 	fs = ump->um_fs;
7878 	if (freework->fw_state & DEPCOMPLETE) {
7879 		handle_written_freework(freework);
7880 		return;
7881 	}
7882 	if (freework->fw_off == NINDIR(fs)) {
7883 		freework_freeblock(freework, SINGLETON_KEY);
7884 		return;
7885 	}
7886 	freework->fw_state |= INPROGRESS;
7887 	FREE_LOCK(ump);
7888 	indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
7889 	    freework->fw_lbn);
7890 	ACQUIRE_LOCK(ump);
7891 }
7892 
7893 /*
7894  * Called when a freework structure attached to a cg buf is written.  The
7895  * ref on either the parent or the freeblks structure is released and
7896  * the freeblks is added back to the worklist if there is more work to do.
7897  */
7898 static void
7899 handle_written_freework(freework)
7900 	struct freework *freework;
7901 {
7902 	struct freeblks *freeblks;
7903 	struct freework *parent;
7904 
7905 	freeblks = freework->fw_freeblks;
7906 	parent = freework->fw_parent;
7907 	if (freework->fw_state & DELAYEDFREE)
7908 		freeblks->fb_cgwait--;
7909 	freework->fw_state |= COMPLETE;
7910 	if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
7911 		WORKITEM_FREE(freework, D_FREEWORK);
7912 	if (parent) {
7913 		if (--parent->fw_ref == 0)
7914 			freework_enqueue(parent);
7915 		return;
7916 	}
7917 	if (--freeblks->fb_ref != 0)
7918 		return;
7919 	if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
7920 	    ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
7921 		add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7922 }
7923 
7924 /*
7925  * This workitem routine performs the block de-allocation.
7926  * The workitem is added to the pending list after the updated
7927  * inode block has been written to disk.  As mentioned above,
7928  * checks regarding the number of blocks de-allocated (compared
7929  * to the number of blocks allocated for the file) are also
7930  * performed in this function.
7931  */
7932 static int
7933 handle_workitem_freeblocks(freeblks, flags)
7934 	struct freeblks *freeblks;
7935 	int flags;
7936 {
7937 	struct freework *freework;
7938 	struct newblk *newblk;
7939 	struct allocindir *aip;
7940 	struct ufsmount *ump;
7941 	struct worklist *wk;
7942 	u_long key;
7943 
7944 	KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
7945 	    ("handle_workitem_freeblocks: Journal entries not written."));
7946 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7947 	key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
7948 	ACQUIRE_LOCK(ump);
7949 	while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
7950 		WORKLIST_REMOVE(wk);
7951 		switch (wk->wk_type) {
7952 		case D_DIRREM:
7953 			wk->wk_state |= COMPLETE;
7954 			add_to_worklist(wk, 0);
7955 			continue;
7956 
7957 		case D_ALLOCDIRECT:
7958 			free_newblk(WK_NEWBLK(wk));
7959 			continue;
7960 
7961 		case D_ALLOCINDIR:
7962 			aip = WK_ALLOCINDIR(wk);
7963 			freework = NULL;
7964 			if (aip->ai_state & DELAYEDFREE) {
7965 				FREE_LOCK(ump);
7966 				freework = newfreework(ump, freeblks, NULL,
7967 				    aip->ai_lbn, aip->ai_newblkno,
7968 				    ump->um_fs->fs_frag, 0, 0);
7969 				ACQUIRE_LOCK(ump);
7970 			}
7971 			newblk = WK_NEWBLK(wk);
7972 			if (newblk->nb_jnewblk) {
7973 				freework->fw_jnewblk = newblk->nb_jnewblk;
7974 				newblk->nb_jnewblk->jn_dep = &freework->fw_list;
7975 				newblk->nb_jnewblk = NULL;
7976 			}
7977 			free_newblk(newblk);
7978 			continue;
7979 
7980 		case D_FREEWORK:
7981 			freework = WK_FREEWORK(wk);
7982 			if (freework->fw_lbn <= -UFS_NDADDR)
7983 				handle_workitem_indirblk(freework);
7984 			else
7985 				freework_freeblock(freework, key);
7986 			continue;
7987 		default:
7988 			panic("handle_workitem_freeblocks: Unknown type %s",
7989 			    TYPENAME(wk->wk_type));
7990 		}
7991 	}
7992 	if (freeblks->fb_ref != 0) {
7993 		freeblks->fb_state &= ~INPROGRESS;
7994 		wake_worklist(&freeblks->fb_list);
7995 		freeblks = NULL;
7996 	}
7997 	FREE_LOCK(ump);
7998 	ffs_blkrelease_finish(ump, key);
7999 	if (freeblks)
8000 		return handle_complete_freeblocks(freeblks, flags);
8001 	return (0);
8002 }
8003 
8004 /*
8005  * Handle completion of block free via truncate.  This allows fs_pending
8006  * to track the actual free block count more closely than if we only updated
8007  * it at the end.  We must be careful to handle cases where the block count
8008  * on free was incorrect.
8009  */
8010 static void
8011 freeblks_free(ump, freeblks, blocks)
8012 	struct ufsmount *ump;
8013 	struct freeblks *freeblks;
8014 	int blocks;
8015 {
8016 	struct fs *fs;
8017 	ufs2_daddr_t remain;
8018 
8019 	UFS_LOCK(ump);
8020 	remain = -freeblks->fb_chkcnt;
8021 	freeblks->fb_chkcnt += blocks;
8022 	if (remain > 0) {
8023 		if (remain < blocks)
8024 			blocks = remain;
8025 		fs = ump->um_fs;
8026 		fs->fs_pendingblocks -= blocks;
8027 	}
8028 	UFS_UNLOCK(ump);
8029 }
8030 
8031 /*
8032  * Once all of the freework workitems are complete we can retire the
8033  * freeblocks dependency and any journal work awaiting completion.  This
8034  * can not be called until all other dependencies are stable on disk.
8035  */
8036 static int
8037 handle_complete_freeblocks(freeblks, flags)
8038 	struct freeblks *freeblks;
8039 	int flags;
8040 {
8041 	struct inodedep *inodedep;
8042 	struct inode *ip;
8043 	struct vnode *vp;
8044 	struct fs *fs;
8045 	struct ufsmount *ump;
8046 	ufs2_daddr_t spare;
8047 
8048 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8049 	fs = ump->um_fs;
8050 	flags = LK_EXCLUSIVE | flags;
8051 	spare = freeblks->fb_chkcnt;
8052 
8053 	/*
8054 	 * If we did not release the expected number of blocks we may have
8055 	 * to adjust the inode block count here.  Only do so if it wasn't
8056 	 * a truncation to zero and the modrev still matches.
8057 	 */
8058 	if (spare && freeblks->fb_len != 0) {
8059 		if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8060 		    flags, &vp, FFSV_FORCEINSMQ) != 0)
8061 			return (EBUSY);
8062 		ip = VTOI(vp);
8063 		if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8064 			DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8065 			ip->i_flag |= IN_CHANGE;
8066 			/*
8067 			 * We must wait so this happens before the
8068 			 * journal is reclaimed.
8069 			 */
8070 			ffs_update(vp, 1);
8071 		}
8072 		vput(vp);
8073 	}
8074 	if (spare < 0) {
8075 		UFS_LOCK(ump);
8076 		fs->fs_pendingblocks += spare;
8077 		UFS_UNLOCK(ump);
8078 	}
8079 #ifdef QUOTA
8080 	/* Handle spare. */
8081 	if (spare)
8082 		quotaadj(freeblks->fb_quota, ump, -spare);
8083 	quotarele(freeblks->fb_quota);
8084 #endif
8085 	ACQUIRE_LOCK(ump);
8086 	if (freeblks->fb_state & ONDEPLIST) {
8087 		inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8088 		    0, &inodedep);
8089 		TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8090 		freeblks->fb_state &= ~ONDEPLIST;
8091 		if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8092 			free_inodedep(inodedep);
8093 	}
8094 	/*
8095 	 * All of the freeblock deps must be complete prior to this call
8096 	 * so it's now safe to complete earlier outstanding journal entries.
8097 	 */
8098 	handle_jwork(&freeblks->fb_jwork);
8099 	WORKITEM_FREE(freeblks, D_FREEBLKS);
8100 	FREE_LOCK(ump);
8101 	return (0);
8102 }
8103 
8104 /*
8105  * Release blocks associated with the freeblks and stored in the indirect
8106  * block dbn. If level is greater than SINGLE, the block is an indirect block
8107  * and recursive calls to indirtrunc must be used to cleanse other indirect
8108  * blocks.
8109  *
8110  * This handles partial and complete truncation of blocks.  Partial is noted
8111  * with goingaway == 0.  In this case the freework is completed after the
8112  * zero'd indirects are written to disk.  For full truncation the freework
8113  * is completed after the block is freed.
8114  */
8115 static void
8116 indir_trunc(freework, dbn, lbn)
8117 	struct freework *freework;
8118 	ufs2_daddr_t dbn;
8119 	ufs_lbn_t lbn;
8120 {
8121 	struct freework *nfreework;
8122 	struct workhead wkhd;
8123 	struct freeblks *freeblks;
8124 	struct buf *bp;
8125 	struct fs *fs;
8126 	struct indirdep *indirdep;
8127 	struct ufsmount *ump;
8128 	ufs1_daddr_t *bap1;
8129 	ufs2_daddr_t nb, nnb, *bap2;
8130 	ufs_lbn_t lbnadd, nlbn;
8131 	u_long key;
8132 	int nblocks, ufs1fmt, freedblocks;
8133 	int goingaway, freedeps, needj, level, cnt, i;
8134 
8135 	freeblks = freework->fw_freeblks;
8136 	ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8137 	fs = ump->um_fs;
8138 	/*
8139 	 * Get buffer of block pointers to be freed.  There are three cases:
8140 	 *
8141 	 * 1) Partial truncate caches the indirdep pointer in the freework
8142 	 *    which provides us a back copy to the save bp which holds the
8143 	 *    pointers we want to clear.  When this completes the zero
8144 	 *    pointers are written to the real copy.
8145 	 * 2) The indirect is being completely truncated, cancel_indirdep()
8146 	 *    eliminated the real copy and placed the indirdep on the saved
8147 	 *    copy.  The indirdep and buf are discarded when this completes.
8148 	 * 3) The indirect was not in memory, we read a copy off of the disk
8149 	 *    using the devvp and drop and invalidate the buffer when we're
8150 	 *    done.
8151 	 */
8152 	goingaway = 1;
8153 	indirdep = NULL;
8154 	if (freework->fw_indir != NULL) {
8155 		goingaway = 0;
8156 		indirdep = freework->fw_indir;
8157 		bp = indirdep->ir_savebp;
8158 		if (bp == NULL || bp->b_blkno != dbn)
8159 			panic("indir_trunc: Bad saved buf %p blkno %jd",
8160 			    bp, (intmax_t)dbn);
8161 	} else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8162 		/*
8163 		 * The lock prevents the buf dep list from changing and
8164 	 	 * indirects on devvp should only ever have one dependency.
8165 		 */
8166 		indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8167 		if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8168 			panic("indir_trunc: Bad indirdep %p from buf %p",
8169 			    indirdep, bp);
8170 	} else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
8171 	    NOCRED, &bp) != 0) {
8172 		brelse(bp);
8173 		return;
8174 	}
8175 	ACQUIRE_LOCK(ump);
8176 	/* Protects against a race with complete_trunc_indir(). */
8177 	freework->fw_state &= ~INPROGRESS;
8178 	/*
8179 	 * If we have an indirdep we need to enforce the truncation order
8180 	 * and discard it when it is complete.
8181 	 */
8182 	if (indirdep) {
8183 		if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8184 		    !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8185 			/*
8186 			 * Add the complete truncate to the list on the
8187 			 * indirdep to enforce in-order processing.
8188 			 */
8189 			if (freework->fw_indir == NULL)
8190 				TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8191 				    freework, fw_next);
8192 			FREE_LOCK(ump);
8193 			return;
8194 		}
8195 		/*
8196 		 * If we're goingaway, free the indirdep.  Otherwise it will
8197 		 * linger until the write completes.
8198 		 */
8199 		if (goingaway)
8200 			free_indirdep(indirdep);
8201 	}
8202 	FREE_LOCK(ump);
8203 	/* Initialize pointers depending on block size. */
8204 	if (ump->um_fstype == UFS1) {
8205 		bap1 = (ufs1_daddr_t *)bp->b_data;
8206 		nb = bap1[freework->fw_off];
8207 		ufs1fmt = 1;
8208 		bap2 = NULL;
8209 	} else {
8210 		bap2 = (ufs2_daddr_t *)bp->b_data;
8211 		nb = bap2[freework->fw_off];
8212 		ufs1fmt = 0;
8213 		bap1 = NULL;
8214 	}
8215 	level = lbn_level(lbn);
8216 	needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8217 	lbnadd = lbn_offset(fs, level);
8218 	nblocks = btodb(fs->fs_bsize);
8219 	nfreework = freework;
8220 	freedeps = 0;
8221 	cnt = 0;
8222 	/*
8223 	 * Reclaim blocks.  Traverses into nested indirect levels and
8224 	 * arranges for the current level to be freed when subordinates
8225 	 * are free when journaling.
8226 	 */
8227 	key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8228 	for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8229 		if (i != NINDIR(fs) - 1) {
8230 			if (ufs1fmt)
8231 				nnb = bap1[i+1];
8232 			else
8233 				nnb = bap2[i+1];
8234 		} else
8235 			nnb = 0;
8236 		if (nb == 0)
8237 			continue;
8238 		cnt++;
8239 		if (level != 0) {
8240 			nlbn = (lbn + 1) - (i * lbnadd);
8241 			if (needj != 0) {
8242 				nfreework = newfreework(ump, freeblks, freework,
8243 				    nlbn, nb, fs->fs_frag, 0, 0);
8244 				freedeps++;
8245 			}
8246 			indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8247 		} else {
8248 			struct freedep *freedep;
8249 
8250 			/*
8251 			 * Attempt to aggregate freedep dependencies for
8252 			 * all blocks being released to the same CG.
8253 			 */
8254 			LIST_INIT(&wkhd);
8255 			if (needj != 0 &&
8256 			    (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8257 				freedep = newfreedep(freework);
8258 				WORKLIST_INSERT_UNLOCKED(&wkhd,
8259 				    &freedep->fd_list);
8260 				freedeps++;
8261 			}
8262 			CTR3(KTR_SUJ,
8263 			    "indir_trunc: ino %jd blkno %jd size %d",
8264 			    freeblks->fb_inum, nb, fs->fs_bsize);
8265 			ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8266 			    fs->fs_bsize, freeblks->fb_inum,
8267 			    freeblks->fb_vtype, &wkhd, key);
8268 		}
8269 	}
8270 	ffs_blkrelease_finish(ump, key);
8271 	if (goingaway) {
8272 		bp->b_flags |= B_INVAL | B_NOCACHE;
8273 		brelse(bp);
8274 	}
8275 	freedblocks = 0;
8276 	if (level == 0)
8277 		freedblocks = (nblocks * cnt);
8278 	if (needj == 0)
8279 		freedblocks += nblocks;
8280 	freeblks_free(ump, freeblks, freedblocks);
8281 	/*
8282 	 * If we are journaling set up the ref counts and offset so this
8283 	 * indirect can be completed when its children are free.
8284 	 */
8285 	if (needj) {
8286 		ACQUIRE_LOCK(ump);
8287 		freework->fw_off = i;
8288 		freework->fw_ref += freedeps;
8289 		freework->fw_ref -= NINDIR(fs) + 1;
8290 		if (level == 0)
8291 			freeblks->fb_cgwait += freedeps;
8292 		if (freework->fw_ref == 0)
8293 			freework_freeblock(freework, SINGLETON_KEY);
8294 		FREE_LOCK(ump);
8295 		return;
8296 	}
8297 	/*
8298 	 * If we're not journaling we can free the indirect now.
8299 	 */
8300 	dbn = dbtofsb(fs, dbn);
8301 	CTR3(KTR_SUJ,
8302 	    "indir_trunc 2: ino %jd blkno %jd size %d",
8303 	    freeblks->fb_inum, dbn, fs->fs_bsize);
8304 	ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8305 	    freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8306 	/* Non SUJ softdep does single-threaded truncations. */
8307 	if (freework->fw_blkno == dbn) {
8308 		freework->fw_state |= ALLCOMPLETE;
8309 		ACQUIRE_LOCK(ump);
8310 		handle_written_freework(freework);
8311 		FREE_LOCK(ump);
8312 	}
8313 	return;
8314 }
8315 
8316 /*
8317  * Cancel an allocindir when it is removed via truncation.  When bp is not
8318  * NULL the indirect never appeared on disk and is scheduled to be freed
8319  * independently of the indir so we can more easily track journal work.
8320  */
8321 static void
8322 cancel_allocindir(aip, bp, freeblks, trunc)
8323 	struct allocindir *aip;
8324 	struct buf *bp;
8325 	struct freeblks *freeblks;
8326 	int trunc;
8327 {
8328 	struct indirdep *indirdep;
8329 	struct freefrag *freefrag;
8330 	struct newblk *newblk;
8331 
8332 	newblk = (struct newblk *)aip;
8333 	LIST_REMOVE(aip, ai_next);
8334 	/*
8335 	 * We must eliminate the pointer in bp if it must be freed on its
8336 	 * own due to partial truncate or pending journal work.
8337 	 */
8338 	if (bp && (trunc || newblk->nb_jnewblk)) {
8339 		/*
8340 		 * Clear the pointer and mark the aip to be freed
8341 		 * directly if it never existed on disk.
8342 		 */
8343 		aip->ai_state |= DELAYEDFREE;
8344 		indirdep = aip->ai_indirdep;
8345 		if (indirdep->ir_state & UFS1FMT)
8346 			((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8347 		else
8348 			((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8349 	}
8350 	/*
8351 	 * When truncating the previous pointer will be freed via
8352 	 * savedbp.  Eliminate the freefrag which would dup free.
8353 	 */
8354 	if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8355 		newblk->nb_freefrag = NULL;
8356 		if (freefrag->ff_jdep)
8357 			cancel_jfreefrag(
8358 			    WK_JFREEFRAG(freefrag->ff_jdep));
8359 		jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8360 		WORKITEM_FREE(freefrag, D_FREEFRAG);
8361 	}
8362 	/*
8363 	 * If the journal hasn't been written the jnewblk must be passed
8364 	 * to the call to ffs_blkfree that reclaims the space.  We accomplish
8365 	 * this by leaving the journal dependency on the newblk to be freed
8366 	 * when a freework is created in handle_workitem_freeblocks().
8367 	 */
8368 	cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8369 	WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8370 }
8371 
8372 /*
8373  * Create the mkdir dependencies for . and .. in a new directory.  Link them
8374  * in to a newdirblk so any subsequent additions are tracked properly.  The
8375  * caller is responsible for adding the mkdir1 dependency to the journal
8376  * and updating id_mkdiradd.  This function returns with the per-filesystem
8377  * lock held.
8378  */
8379 static struct mkdir *
8380 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8381 	struct diradd *dap;
8382 	ino_t newinum;
8383 	ino_t dinum;
8384 	struct buf *newdirbp;
8385 	struct mkdir **mkdirp;
8386 {
8387 	struct newblk *newblk;
8388 	struct pagedep *pagedep;
8389 	struct inodedep *inodedep;
8390 	struct newdirblk *newdirblk;
8391 	struct mkdir *mkdir1, *mkdir2;
8392 	struct worklist *wk;
8393 	struct jaddref *jaddref;
8394 	struct ufsmount *ump;
8395 	struct mount *mp;
8396 
8397 	mp = dap->da_list.wk_mp;
8398 	ump = VFSTOUFS(mp);
8399 	newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8400 	    M_SOFTDEP_FLAGS);
8401 	workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8402 	LIST_INIT(&newdirblk->db_mkdir);
8403 	mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8404 	workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8405 	mkdir1->md_state = ATTACHED | MKDIR_BODY;
8406 	mkdir1->md_diradd = dap;
8407 	mkdir1->md_jaddref = NULL;
8408 	mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8409 	workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8410 	mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8411 	mkdir2->md_diradd = dap;
8412 	mkdir2->md_jaddref = NULL;
8413 	if (MOUNTEDSUJ(mp) == 0) {
8414 		mkdir1->md_state |= DEPCOMPLETE;
8415 		mkdir2->md_state |= DEPCOMPLETE;
8416 	}
8417 	/*
8418 	 * Dependency on "." and ".." being written to disk.
8419 	 */
8420 	mkdir1->md_buf = newdirbp;
8421 	ACQUIRE_LOCK(VFSTOUFS(mp));
8422 	LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8423 	/*
8424 	 * We must link the pagedep, allocdirect, and newdirblk for
8425 	 * the initial file page so the pointer to the new directory
8426 	 * is not written until the directory contents are live and
8427 	 * any subsequent additions are not marked live until the
8428 	 * block is reachable via the inode.
8429 	 */
8430 	if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8431 		panic("setup_newdir: lost pagedep");
8432 	LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8433 		if (wk->wk_type == D_ALLOCDIRECT)
8434 			break;
8435 	if (wk == NULL)
8436 		panic("setup_newdir: lost allocdirect");
8437 	if (pagedep->pd_state & NEWBLOCK)
8438 		panic("setup_newdir: NEWBLOCK already set");
8439 	newblk = WK_NEWBLK(wk);
8440 	pagedep->pd_state |= NEWBLOCK;
8441 	pagedep->pd_newdirblk = newdirblk;
8442 	newdirblk->db_pagedep = pagedep;
8443 	WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8444 	WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8445 	/*
8446 	 * Look up the inodedep for the parent directory so that we
8447 	 * can link mkdir2 into the pending dotdot jaddref or
8448 	 * the inode write if there is none.  If the inode is
8449 	 * ALLCOMPLETE and no jaddref is present all dependencies have
8450 	 * been satisfied and mkdir2 can be freed.
8451 	 */
8452 	inodedep_lookup(mp, dinum, 0, &inodedep);
8453 	if (MOUNTEDSUJ(mp)) {
8454 		if (inodedep == NULL)
8455 			panic("setup_newdir: Lost parent.");
8456 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8457 		    inoreflst);
8458 		KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8459 		    (jaddref->ja_state & MKDIR_PARENT),
8460 		    ("setup_newdir: bad dotdot jaddref %p", jaddref));
8461 		LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8462 		mkdir2->md_jaddref = jaddref;
8463 		jaddref->ja_mkdir = mkdir2;
8464 	} else if (inodedep == NULL ||
8465 	    (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8466 		dap->da_state &= ~MKDIR_PARENT;
8467 		WORKITEM_FREE(mkdir2, D_MKDIR);
8468 		mkdir2 = NULL;
8469 	} else {
8470 		LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8471 		WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8472 	}
8473 	*mkdirp = mkdir2;
8474 
8475 	return (mkdir1);
8476 }
8477 
8478 /*
8479  * Directory entry addition dependencies.
8480  *
8481  * When adding a new directory entry, the inode (with its incremented link
8482  * count) must be written to disk before the directory entry's pointer to it.
8483  * Also, if the inode is newly allocated, the corresponding freemap must be
8484  * updated (on disk) before the directory entry's pointer. These requirements
8485  * are met via undo/redo on the directory entry's pointer, which consists
8486  * simply of the inode number.
8487  *
8488  * As directory entries are added and deleted, the free space within a
8489  * directory block can become fragmented.  The ufs filesystem will compact
8490  * a fragmented directory block to make space for a new entry. When this
8491  * occurs, the offsets of previously added entries change. Any "diradd"
8492  * dependency structures corresponding to these entries must be updated with
8493  * the new offsets.
8494  */
8495 
8496 /*
8497  * This routine is called after the in-memory inode's link
8498  * count has been incremented, but before the directory entry's
8499  * pointer to the inode has been set.
8500  */
8501 int
8502 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8503 	struct buf *bp;		/* buffer containing directory block */
8504 	struct inode *dp;	/* inode for directory */
8505 	off_t diroffset;	/* offset of new entry in directory */
8506 	ino_t newinum;		/* inode referenced by new directory entry */
8507 	struct buf *newdirbp;	/* non-NULL => contents of new mkdir */
8508 	int isnewblk;		/* entry is in a newly allocated block */
8509 {
8510 	int offset;		/* offset of new entry within directory block */
8511 	ufs_lbn_t lbn;		/* block in directory containing new entry */
8512 	struct fs *fs;
8513 	struct diradd *dap;
8514 	struct newblk *newblk;
8515 	struct pagedep *pagedep;
8516 	struct inodedep *inodedep;
8517 	struct newdirblk *newdirblk;
8518 	struct mkdir *mkdir1, *mkdir2;
8519 	struct jaddref *jaddref;
8520 	struct ufsmount *ump;
8521 	struct mount *mp;
8522 	int isindir;
8523 
8524 	mp = ITOVFS(dp);
8525 	ump = VFSTOUFS(mp);
8526 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8527 	    ("softdep_setup_directory_add called on non-softdep filesystem"));
8528 	/*
8529 	 * Whiteouts have no dependencies.
8530 	 */
8531 	if (newinum == UFS_WINO) {
8532 		if (newdirbp != NULL)
8533 			bdwrite(newdirbp);
8534 		return (0);
8535 	}
8536 	jaddref = NULL;
8537 	mkdir1 = mkdir2 = NULL;
8538 	fs = ump->um_fs;
8539 	lbn = lblkno(fs, diroffset);
8540 	offset = blkoff(fs, diroffset);
8541 	dap = malloc(sizeof(struct diradd), M_DIRADD,
8542 		M_SOFTDEP_FLAGS|M_ZERO);
8543 	workitem_alloc(&dap->da_list, D_DIRADD, mp);
8544 	dap->da_offset = offset;
8545 	dap->da_newinum = newinum;
8546 	dap->da_state = ATTACHED;
8547 	LIST_INIT(&dap->da_jwork);
8548 	isindir = bp->b_lblkno >= UFS_NDADDR;
8549 	newdirblk = NULL;
8550 	if (isnewblk &&
8551 	    (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8552 		newdirblk = malloc(sizeof(struct newdirblk),
8553 		    M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8554 		workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8555 		LIST_INIT(&newdirblk->db_mkdir);
8556 	}
8557 	/*
8558 	 * If we're creating a new directory setup the dependencies and set
8559 	 * the dap state to wait for them.  Otherwise it's COMPLETE and
8560 	 * we can move on.
8561 	 */
8562 	if (newdirbp == NULL) {
8563 		dap->da_state |= DEPCOMPLETE;
8564 		ACQUIRE_LOCK(ump);
8565 	} else {
8566 		dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8567 		mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8568 		    &mkdir2);
8569 	}
8570 	/*
8571 	 * Link into parent directory pagedep to await its being written.
8572 	 */
8573 	pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8574 #ifdef INVARIANTS
8575 	if (diradd_lookup(pagedep, offset) != NULL)
8576 		panic("softdep_setup_directory_add: %p already at off %d\n",
8577 		    diradd_lookup(pagedep, offset), offset);
8578 #endif
8579 	dap->da_pagedep = pagedep;
8580 	LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8581 	    da_pdlist);
8582 	inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8583 	/*
8584 	 * If we're journaling, link the diradd into the jaddref so it
8585 	 * may be completed after the journal entry is written.  Otherwise,
8586 	 * link the diradd into its inodedep.  If the inode is not yet
8587 	 * written place it on the bufwait list, otherwise do the post-inode
8588 	 * write processing to put it on the id_pendinghd list.
8589 	 */
8590 	if (MOUNTEDSUJ(mp)) {
8591 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8592 		    inoreflst);
8593 		KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8594 		    ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8595 		jaddref->ja_diroff = diroffset;
8596 		jaddref->ja_diradd = dap;
8597 		add_to_journal(&jaddref->ja_list);
8598 	} else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8599 		diradd_inode_written(dap, inodedep);
8600 	else
8601 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8602 	/*
8603 	 * Add the journal entries for . and .. links now that the primary
8604 	 * link is written.
8605 	 */
8606 	if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8607 		jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8608 		    inoreflst, if_deps);
8609 		KASSERT(jaddref != NULL &&
8610 		    jaddref->ja_ino == jaddref->ja_parent &&
8611 		    (jaddref->ja_state & MKDIR_BODY),
8612 		    ("softdep_setup_directory_add: bad dot jaddref %p",
8613 		    jaddref));
8614 		mkdir1->md_jaddref = jaddref;
8615 		jaddref->ja_mkdir = mkdir1;
8616 		/*
8617 		 * It is important that the dotdot journal entry
8618 		 * is added prior to the dot entry since dot writes
8619 		 * both the dot and dotdot links.  These both must
8620 		 * be added after the primary link for the journal
8621 		 * to remain consistent.
8622 		 */
8623 		add_to_journal(&mkdir2->md_jaddref->ja_list);
8624 		add_to_journal(&jaddref->ja_list);
8625 	}
8626 	/*
8627 	 * If we are adding a new directory remember this diradd so that if
8628 	 * we rename it we can keep the dot and dotdot dependencies.  If
8629 	 * we are adding a new name for an inode that has a mkdiradd we
8630 	 * must be in rename and we have to move the dot and dotdot
8631 	 * dependencies to this new name.  The old name is being orphaned
8632 	 * soon.
8633 	 */
8634 	if (mkdir1 != NULL) {
8635 		if (inodedep->id_mkdiradd != NULL)
8636 			panic("softdep_setup_directory_add: Existing mkdir");
8637 		inodedep->id_mkdiradd = dap;
8638 	} else if (inodedep->id_mkdiradd)
8639 		merge_diradd(inodedep, dap);
8640 	if (newdirblk != NULL) {
8641 		/*
8642 		 * There is nothing to do if we are already tracking
8643 		 * this block.
8644 		 */
8645 		if ((pagedep->pd_state & NEWBLOCK) != 0) {
8646 			WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8647 			FREE_LOCK(ump);
8648 			return (0);
8649 		}
8650 		if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8651 		    == 0)
8652 			panic("softdep_setup_directory_add: lost entry");
8653 		WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8654 		pagedep->pd_state |= NEWBLOCK;
8655 		pagedep->pd_newdirblk = newdirblk;
8656 		newdirblk->db_pagedep = pagedep;
8657 		FREE_LOCK(ump);
8658 		/*
8659 		 * If we extended into an indirect signal direnter to sync.
8660 		 */
8661 		if (isindir)
8662 			return (1);
8663 		return (0);
8664 	}
8665 	FREE_LOCK(ump);
8666 	return (0);
8667 }
8668 
8669 /*
8670  * This procedure is called to change the offset of a directory
8671  * entry when compacting a directory block which must be owned
8672  * exclusively by the caller. Note that the actual entry movement
8673  * must be done in this procedure to ensure that no I/O completions
8674  * occur while the move is in progress.
8675  */
8676 void
8677 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
8678 	struct buf *bp;		/* Buffer holding directory block. */
8679 	struct inode *dp;	/* inode for directory */
8680 	caddr_t base;		/* address of dp->i_offset */
8681 	caddr_t oldloc;		/* address of old directory location */
8682 	caddr_t newloc;		/* address of new directory location */
8683 	int entrysize;		/* size of directory entry */
8684 {
8685 	int offset, oldoffset, newoffset;
8686 	struct pagedep *pagedep;
8687 	struct jmvref *jmvref;
8688 	struct diradd *dap;
8689 	struct direct *de;
8690 	struct mount *mp;
8691 	struct ufsmount *ump;
8692 	ufs_lbn_t lbn;
8693 	int flags;
8694 
8695 	mp = ITOVFS(dp);
8696 	ump = VFSTOUFS(mp);
8697 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8698 	    ("softdep_change_directoryentry_offset called on "
8699 	     "non-softdep filesystem"));
8700 	de = (struct direct *)oldloc;
8701 	jmvref = NULL;
8702 	flags = 0;
8703 	/*
8704 	 * Moves are always journaled as it would be too complex to
8705 	 * determine if any affected adds or removes are present in the
8706 	 * journal.
8707 	 */
8708 	if (MOUNTEDSUJ(mp)) {
8709 		flags = DEPALLOC;
8710 		jmvref = newjmvref(dp, de->d_ino,
8711 		    dp->i_offset + (oldloc - base),
8712 		    dp->i_offset + (newloc - base));
8713 	}
8714 	lbn = lblkno(ump->um_fs, dp->i_offset);
8715 	offset = blkoff(ump->um_fs, dp->i_offset);
8716 	oldoffset = offset + (oldloc - base);
8717 	newoffset = offset + (newloc - base);
8718 	ACQUIRE_LOCK(ump);
8719 	if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8720 		goto done;
8721 	dap = diradd_lookup(pagedep, oldoffset);
8722 	if (dap) {
8723 		dap->da_offset = newoffset;
8724 		newoffset = DIRADDHASH(newoffset);
8725 		oldoffset = DIRADDHASH(oldoffset);
8726 		if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8727 		    newoffset != oldoffset) {
8728 			LIST_REMOVE(dap, da_pdlist);
8729 			LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8730 			    dap, da_pdlist);
8731 		}
8732 	}
8733 done:
8734 	if (jmvref) {
8735 		jmvref->jm_pagedep = pagedep;
8736 		LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8737 		add_to_journal(&jmvref->jm_list);
8738 	}
8739 	bcopy(oldloc, newloc, entrysize);
8740 	FREE_LOCK(ump);
8741 }
8742 
8743 /*
8744  * Move the mkdir dependencies and journal work from one diradd to another
8745  * when renaming a directory.  The new name must depend on the mkdir deps
8746  * completing as the old name did.  Directories can only have one valid link
8747  * at a time so one must be canonical.
8748  */
8749 static void
8750 merge_diradd(inodedep, newdap)
8751 	struct inodedep *inodedep;
8752 	struct diradd *newdap;
8753 {
8754 	struct diradd *olddap;
8755 	struct mkdir *mkdir, *nextmd;
8756 	struct ufsmount *ump;
8757 	short state;
8758 
8759 	olddap = inodedep->id_mkdiradd;
8760 	inodedep->id_mkdiradd = newdap;
8761 	if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8762 		newdap->da_state &= ~DEPCOMPLETE;
8763 		ump = VFSTOUFS(inodedep->id_list.wk_mp);
8764 		for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8765 		     mkdir = nextmd) {
8766 			nextmd = LIST_NEXT(mkdir, md_mkdirs);
8767 			if (mkdir->md_diradd != olddap)
8768 				continue;
8769 			mkdir->md_diradd = newdap;
8770 			state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8771 			newdap->da_state |= state;
8772 			olddap->da_state &= ~state;
8773 			if ((olddap->da_state &
8774 			    (MKDIR_PARENT | MKDIR_BODY)) == 0)
8775 				break;
8776 		}
8777 		if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8778 			panic("merge_diradd: unfound ref");
8779 	}
8780 	/*
8781 	 * Any mkdir related journal items are not safe to be freed until
8782 	 * the new name is stable.
8783 	 */
8784 	jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8785 	olddap->da_state |= DEPCOMPLETE;
8786 	complete_diradd(olddap);
8787 }
8788 
8789 /*
8790  * Move the diradd to the pending list when all diradd dependencies are
8791  * complete.
8792  */
8793 static void
8794 complete_diradd(dap)
8795 	struct diradd *dap;
8796 {
8797 	struct pagedep *pagedep;
8798 
8799 	if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
8800 		if (dap->da_state & DIRCHG)
8801 			pagedep = dap->da_previous->dm_pagedep;
8802 		else
8803 			pagedep = dap->da_pagedep;
8804 		LIST_REMOVE(dap, da_pdlist);
8805 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
8806 	}
8807 }
8808 
8809 /*
8810  * Cancel a diradd when a dirrem overlaps with it.  We must cancel the journal
8811  * add entries and conditonally journal the remove.
8812  */
8813 static void
8814 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
8815 	struct diradd *dap;
8816 	struct dirrem *dirrem;
8817 	struct jremref *jremref;
8818 	struct jremref *dotremref;
8819 	struct jremref *dotdotremref;
8820 {
8821 	struct inodedep *inodedep;
8822 	struct jaddref *jaddref;
8823 	struct inoref *inoref;
8824 	struct ufsmount *ump;
8825 	struct mkdir *mkdir;
8826 
8827 	/*
8828 	 * If no remove references were allocated we're on a non-journaled
8829 	 * filesystem and can skip the cancel step.
8830 	 */
8831 	if (jremref == NULL) {
8832 		free_diradd(dap, NULL);
8833 		return;
8834 	}
8835 	/*
8836 	 * Cancel the primary name an free it if it does not require
8837 	 * journaling.
8838 	 */
8839 	if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
8840 	    0, &inodedep) != 0) {
8841 		/* Abort the addref that reference this diradd.  */
8842 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
8843 			if (inoref->if_list.wk_type != D_JADDREF)
8844 				continue;
8845 			jaddref = (struct jaddref *)inoref;
8846 			if (jaddref->ja_diradd != dap)
8847 				continue;
8848 			if (cancel_jaddref(jaddref, inodedep,
8849 			    &dirrem->dm_jwork) == 0) {
8850 				free_jremref(jremref);
8851 				jremref = NULL;
8852 			}
8853 			break;
8854 		}
8855 	}
8856 	/*
8857 	 * Cancel subordinate names and free them if they do not require
8858 	 * journaling.
8859 	 */
8860 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8861 		ump = VFSTOUFS(dap->da_list.wk_mp);
8862 		LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
8863 			if (mkdir->md_diradd != dap)
8864 				continue;
8865 			if ((jaddref = mkdir->md_jaddref) == NULL)
8866 				continue;
8867 			mkdir->md_jaddref = NULL;
8868 			if (mkdir->md_state & MKDIR_PARENT) {
8869 				if (cancel_jaddref(jaddref, NULL,
8870 				    &dirrem->dm_jwork) == 0) {
8871 					free_jremref(dotdotremref);
8872 					dotdotremref = NULL;
8873 				}
8874 			} else {
8875 				if (cancel_jaddref(jaddref, inodedep,
8876 				    &dirrem->dm_jwork) == 0) {
8877 					free_jremref(dotremref);
8878 					dotremref = NULL;
8879 				}
8880 			}
8881 		}
8882 	}
8883 
8884 	if (jremref)
8885 		journal_jremref(dirrem, jremref, inodedep);
8886 	if (dotremref)
8887 		journal_jremref(dirrem, dotremref, inodedep);
8888 	if (dotdotremref)
8889 		journal_jremref(dirrem, dotdotremref, NULL);
8890 	jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
8891 	free_diradd(dap, &dirrem->dm_jwork);
8892 }
8893 
8894 /*
8895  * Free a diradd dependency structure.
8896  */
8897 static void
8898 free_diradd(dap, wkhd)
8899 	struct diradd *dap;
8900 	struct workhead *wkhd;
8901 {
8902 	struct dirrem *dirrem;
8903 	struct pagedep *pagedep;
8904 	struct inodedep *inodedep;
8905 	struct mkdir *mkdir, *nextmd;
8906 	struct ufsmount *ump;
8907 
8908 	ump = VFSTOUFS(dap->da_list.wk_mp);
8909 	LOCK_OWNED(ump);
8910 	LIST_REMOVE(dap, da_pdlist);
8911 	if (dap->da_state & ONWORKLIST)
8912 		WORKLIST_REMOVE(&dap->da_list);
8913 	if ((dap->da_state & DIRCHG) == 0) {
8914 		pagedep = dap->da_pagedep;
8915 	} else {
8916 		dirrem = dap->da_previous;
8917 		pagedep = dirrem->dm_pagedep;
8918 		dirrem->dm_dirinum = pagedep->pd_ino;
8919 		dirrem->dm_state |= COMPLETE;
8920 		if (LIST_EMPTY(&dirrem->dm_jremrefhd))
8921 			add_to_worklist(&dirrem->dm_list, 0);
8922 	}
8923 	if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
8924 	    0, &inodedep) != 0)
8925 		if (inodedep->id_mkdiradd == dap)
8926 			inodedep->id_mkdiradd = NULL;
8927 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8928 		for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8929 		     mkdir = nextmd) {
8930 			nextmd = LIST_NEXT(mkdir, md_mkdirs);
8931 			if (mkdir->md_diradd != dap)
8932 				continue;
8933 			dap->da_state &=
8934 			    ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
8935 			LIST_REMOVE(mkdir, md_mkdirs);
8936 			if (mkdir->md_state & ONWORKLIST)
8937 				WORKLIST_REMOVE(&mkdir->md_list);
8938 			if (mkdir->md_jaddref != NULL)
8939 				panic("free_diradd: Unexpected jaddref");
8940 			WORKITEM_FREE(mkdir, D_MKDIR);
8941 			if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
8942 				break;
8943 		}
8944 		if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8945 			panic("free_diradd: unfound ref");
8946 	}
8947 	if (inodedep)
8948 		free_inodedep(inodedep);
8949 	/*
8950 	 * Free any journal segments waiting for the directory write.
8951 	 */
8952 	handle_jwork(&dap->da_jwork);
8953 	WORKITEM_FREE(dap, D_DIRADD);
8954 }
8955 
8956 /*
8957  * Directory entry removal dependencies.
8958  *
8959  * When removing a directory entry, the entry's inode pointer must be
8960  * zero'ed on disk before the corresponding inode's link count is decremented
8961  * (possibly freeing the inode for re-use). This dependency is handled by
8962  * updating the directory entry but delaying the inode count reduction until
8963  * after the directory block has been written to disk. After this point, the
8964  * inode count can be decremented whenever it is convenient.
8965  */
8966 
8967 /*
8968  * This routine should be called immediately after removing
8969  * a directory entry.  The inode's link count should not be
8970  * decremented by the calling procedure -- the soft updates
8971  * code will do this task when it is safe.
8972  */
8973 void
8974 softdep_setup_remove(bp, dp, ip, isrmdir)
8975 	struct buf *bp;		/* buffer containing directory block */
8976 	struct inode *dp;	/* inode for the directory being modified */
8977 	struct inode *ip;	/* inode for directory entry being removed */
8978 	int isrmdir;		/* indicates if doing RMDIR */
8979 {
8980 	struct dirrem *dirrem, *prevdirrem;
8981 	struct inodedep *inodedep;
8982 	struct ufsmount *ump;
8983 	int direct;
8984 
8985 	ump = ITOUMP(ip);
8986 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
8987 	    ("softdep_setup_remove called on non-softdep filesystem"));
8988 	/*
8989 	 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.  We want
8990 	 * newdirrem() to setup the full directory remove which requires
8991 	 * isrmdir > 1.
8992 	 */
8993 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
8994 	/*
8995 	 * Add the dirrem to the inodedep's pending remove list for quick
8996 	 * discovery later.
8997 	 */
8998 	if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
8999 		panic("softdep_setup_remove: Lost inodedep.");
9000 	KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9001 	dirrem->dm_state |= ONDEPLIST;
9002 	LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9003 
9004 	/*
9005 	 * If the COMPLETE flag is clear, then there were no active
9006 	 * entries and we want to roll back to a zeroed entry until
9007 	 * the new inode is committed to disk. If the COMPLETE flag is
9008 	 * set then we have deleted an entry that never made it to
9009 	 * disk. If the entry we deleted resulted from a name change,
9010 	 * then the old name still resides on disk. We cannot delete
9011 	 * its inode (returned to us in prevdirrem) until the zeroed
9012 	 * directory entry gets to disk. The new inode has never been
9013 	 * referenced on the disk, so can be deleted immediately.
9014 	 */
9015 	if ((dirrem->dm_state & COMPLETE) == 0) {
9016 		LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9017 		    dm_next);
9018 		FREE_LOCK(ump);
9019 	} else {
9020 		if (prevdirrem != NULL)
9021 			LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9022 			    prevdirrem, dm_next);
9023 		dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9024 		direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9025 		FREE_LOCK(ump);
9026 		if (direct)
9027 			handle_workitem_remove(dirrem, 0);
9028 	}
9029 }
9030 
9031 /*
9032  * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9033  * pd_pendinghd list of a pagedep.
9034  */
9035 static struct diradd *
9036 diradd_lookup(pagedep, offset)
9037 	struct pagedep *pagedep;
9038 	int offset;
9039 {
9040 	struct diradd *dap;
9041 
9042 	LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9043 		if (dap->da_offset == offset)
9044 			return (dap);
9045 	LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9046 		if (dap->da_offset == offset)
9047 			return (dap);
9048 	return (NULL);
9049 }
9050 
9051 /*
9052  * Search for a .. diradd dependency in a directory that is being removed.
9053  * If the directory was renamed to a new parent we have a diradd rather
9054  * than a mkdir for the .. entry.  We need to cancel it now before
9055  * it is found in truncate().
9056  */
9057 static struct jremref *
9058 cancel_diradd_dotdot(ip, dirrem, jremref)
9059 	struct inode *ip;
9060 	struct dirrem *dirrem;
9061 	struct jremref *jremref;
9062 {
9063 	struct pagedep *pagedep;
9064 	struct diradd *dap;
9065 	struct worklist *wk;
9066 
9067 	if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9068 		return (jremref);
9069 	dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9070 	if (dap == NULL)
9071 		return (jremref);
9072 	cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9073 	/*
9074 	 * Mark any journal work as belonging to the parent so it is freed
9075 	 * with the .. reference.
9076 	 */
9077 	LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9078 		wk->wk_state |= MKDIR_PARENT;
9079 	return (NULL);
9080 }
9081 
9082 /*
9083  * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9084  * replace it with a dirrem/diradd pair as a result of re-parenting a
9085  * directory.  This ensures that we don't simultaneously have a mkdir and
9086  * a diradd for the same .. entry.
9087  */
9088 static struct jremref *
9089 cancel_mkdir_dotdot(ip, dirrem, jremref)
9090 	struct inode *ip;
9091 	struct dirrem *dirrem;
9092 	struct jremref *jremref;
9093 {
9094 	struct inodedep *inodedep;
9095 	struct jaddref *jaddref;
9096 	struct ufsmount *ump;
9097 	struct mkdir *mkdir;
9098 	struct diradd *dap;
9099 	struct mount *mp;
9100 
9101 	mp = ITOVFS(ip);
9102 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9103 		return (jremref);
9104 	dap = inodedep->id_mkdiradd;
9105 	if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9106 		return (jremref);
9107 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
9108 	for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9109 	    mkdir = LIST_NEXT(mkdir, md_mkdirs))
9110 		if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9111 			break;
9112 	if (mkdir == NULL)
9113 		panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9114 	if ((jaddref = mkdir->md_jaddref) != NULL) {
9115 		mkdir->md_jaddref = NULL;
9116 		jaddref->ja_state &= ~MKDIR_PARENT;
9117 		if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9118 			panic("cancel_mkdir_dotdot: Lost parent inodedep");
9119 		if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9120 			journal_jremref(dirrem, jremref, inodedep);
9121 			jremref = NULL;
9122 		}
9123 	}
9124 	if (mkdir->md_state & ONWORKLIST)
9125 		WORKLIST_REMOVE(&mkdir->md_list);
9126 	mkdir->md_state |= ALLCOMPLETE;
9127 	complete_mkdir(mkdir);
9128 	return (jremref);
9129 }
9130 
9131 static void
9132 journal_jremref(dirrem, jremref, inodedep)
9133 	struct dirrem *dirrem;
9134 	struct jremref *jremref;
9135 	struct inodedep *inodedep;
9136 {
9137 
9138 	if (inodedep == NULL)
9139 		if (inodedep_lookup(jremref->jr_list.wk_mp,
9140 		    jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9141 			panic("journal_jremref: Lost inodedep");
9142 	LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9143 	TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9144 	add_to_journal(&jremref->jr_list);
9145 }
9146 
9147 static void
9148 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9149 	struct dirrem *dirrem;
9150 	struct jremref *jremref;
9151 	struct jremref *dotremref;
9152 	struct jremref *dotdotremref;
9153 {
9154 	struct inodedep *inodedep;
9155 
9156 
9157 	if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9158 	    &inodedep) == 0)
9159 		panic("dirrem_journal: Lost inodedep");
9160 	journal_jremref(dirrem, jremref, inodedep);
9161 	if (dotremref)
9162 		journal_jremref(dirrem, dotremref, inodedep);
9163 	if (dotdotremref)
9164 		journal_jremref(dirrem, dotdotremref, NULL);
9165 }
9166 
9167 /*
9168  * Allocate a new dirrem if appropriate and return it along with
9169  * its associated pagedep. Called without a lock, returns with lock.
9170  */
9171 static struct dirrem *
9172 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9173 	struct buf *bp;		/* buffer containing directory block */
9174 	struct inode *dp;	/* inode for the directory being modified */
9175 	struct inode *ip;	/* inode for directory entry being removed */
9176 	int isrmdir;		/* indicates if doing RMDIR */
9177 	struct dirrem **prevdirremp; /* previously referenced inode, if any */
9178 {
9179 	int offset;
9180 	ufs_lbn_t lbn;
9181 	struct diradd *dap;
9182 	struct dirrem *dirrem;
9183 	struct pagedep *pagedep;
9184 	struct jremref *jremref;
9185 	struct jremref *dotremref;
9186 	struct jremref *dotdotremref;
9187 	struct vnode *dvp;
9188 	struct ufsmount *ump;
9189 
9190 	/*
9191 	 * Whiteouts have no deletion dependencies.
9192 	 */
9193 	if (ip == NULL)
9194 		panic("newdirrem: whiteout");
9195 	dvp = ITOV(dp);
9196 	ump = ITOUMP(dp);
9197 
9198 	/*
9199 	 * If the system is over its limit and our filesystem is
9200 	 * responsible for more than our share of that usage and
9201 	 * we are not a snapshot, request some inodedep cleanup.
9202 	 * Limiting the number of dirrem structures will also limit
9203 	 * the number of freefile and freeblks structures.
9204 	 */
9205 	ACQUIRE_LOCK(ump);
9206 	if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9207 		schedule_cleanup(UFSTOVFS(ump));
9208 	else
9209 		FREE_LOCK(ump);
9210 	dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9211 	    M_ZERO);
9212 	workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9213 	LIST_INIT(&dirrem->dm_jremrefhd);
9214 	LIST_INIT(&dirrem->dm_jwork);
9215 	dirrem->dm_state = isrmdir ? RMDIR : 0;
9216 	dirrem->dm_oldinum = ip->i_number;
9217 	*prevdirremp = NULL;
9218 	/*
9219 	 * Allocate remove reference structures to track journal write
9220 	 * dependencies.  We will always have one for the link and
9221 	 * when doing directories we will always have one more for dot.
9222 	 * When renaming a directory we skip the dotdot link change so
9223 	 * this is not needed.
9224 	 */
9225 	jremref = dotremref = dotdotremref = NULL;
9226 	if (DOINGSUJ(dvp)) {
9227 		if (isrmdir) {
9228 			jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9229 			    ip->i_effnlink + 2);
9230 			dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9231 			    ip->i_effnlink + 1);
9232 			dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9233 			    dp->i_effnlink + 1);
9234 			dotdotremref->jr_state |= MKDIR_PARENT;
9235 		} else
9236 			jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9237 			    ip->i_effnlink + 1);
9238 	}
9239 	ACQUIRE_LOCK(ump);
9240 	lbn = lblkno(ump->um_fs, dp->i_offset);
9241 	offset = blkoff(ump->um_fs, dp->i_offset);
9242 	pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9243 	    &pagedep);
9244 	dirrem->dm_pagedep = pagedep;
9245 	dirrem->dm_offset = offset;
9246 	/*
9247 	 * If we're renaming a .. link to a new directory, cancel any
9248 	 * existing MKDIR_PARENT mkdir.  If it has already been canceled
9249 	 * the jremref is preserved for any potential diradd in this
9250 	 * location.  This can not coincide with a rmdir.
9251 	 */
9252 	if (dp->i_offset == DOTDOT_OFFSET) {
9253 		if (isrmdir)
9254 			panic("newdirrem: .. directory change during remove?");
9255 		jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9256 	}
9257 	/*
9258 	 * If we're removing a directory search for the .. dependency now and
9259 	 * cancel it.  Any pending journal work will be added to the dirrem
9260 	 * to be completed when the workitem remove completes.
9261 	 */
9262 	if (isrmdir)
9263 		dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9264 	/*
9265 	 * Check for a diradd dependency for the same directory entry.
9266 	 * If present, then both dependencies become obsolete and can
9267 	 * be de-allocated.
9268 	 */
9269 	dap = diradd_lookup(pagedep, offset);
9270 	if (dap == NULL) {
9271 		/*
9272 		 * Link the jremref structures into the dirrem so they are
9273 		 * written prior to the pagedep.
9274 		 */
9275 		if (jremref)
9276 			dirrem_journal(dirrem, jremref, dotremref,
9277 			    dotdotremref);
9278 		return (dirrem);
9279 	}
9280 	/*
9281 	 * Must be ATTACHED at this point.
9282 	 */
9283 	if ((dap->da_state & ATTACHED) == 0)
9284 		panic("newdirrem: not ATTACHED");
9285 	if (dap->da_newinum != ip->i_number)
9286 		panic("newdirrem: inum %ju should be %ju",
9287 		    (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9288 	/*
9289 	 * If we are deleting a changed name that never made it to disk,
9290 	 * then return the dirrem describing the previous inode (which
9291 	 * represents the inode currently referenced from this entry on disk).
9292 	 */
9293 	if ((dap->da_state & DIRCHG) != 0) {
9294 		*prevdirremp = dap->da_previous;
9295 		dap->da_state &= ~DIRCHG;
9296 		dap->da_pagedep = pagedep;
9297 	}
9298 	/*
9299 	 * We are deleting an entry that never made it to disk.
9300 	 * Mark it COMPLETE so we can delete its inode immediately.
9301 	 */
9302 	dirrem->dm_state |= COMPLETE;
9303 	cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9304 #ifdef INVARIANTS
9305 	if (isrmdir == 0) {
9306 		struct worklist *wk;
9307 
9308 		LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9309 			if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9310 				panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9311 	}
9312 #endif
9313 
9314 	return (dirrem);
9315 }
9316 
9317 /*
9318  * Directory entry change dependencies.
9319  *
9320  * Changing an existing directory entry requires that an add operation
9321  * be completed first followed by a deletion. The semantics for the addition
9322  * are identical to the description of adding a new entry above except
9323  * that the rollback is to the old inode number rather than zero. Once
9324  * the addition dependency is completed, the removal is done as described
9325  * in the removal routine above.
9326  */
9327 
9328 /*
9329  * This routine should be called immediately after changing
9330  * a directory entry.  The inode's link count should not be
9331  * decremented by the calling procedure -- the soft updates
9332  * code will perform this task when it is safe.
9333  */
9334 void
9335 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9336 	struct buf *bp;		/* buffer containing directory block */
9337 	struct inode *dp;	/* inode for the directory being modified */
9338 	struct inode *ip;	/* inode for directory entry being removed */
9339 	ino_t newinum;		/* new inode number for changed entry */
9340 	int isrmdir;		/* indicates if doing RMDIR */
9341 {
9342 	int offset;
9343 	struct diradd *dap = NULL;
9344 	struct dirrem *dirrem, *prevdirrem;
9345 	struct pagedep *pagedep;
9346 	struct inodedep *inodedep;
9347 	struct jaddref *jaddref;
9348 	struct mount *mp;
9349 	struct ufsmount *ump;
9350 
9351 	mp = ITOVFS(dp);
9352 	ump = VFSTOUFS(mp);
9353 	offset = blkoff(ump->um_fs, dp->i_offset);
9354 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9355 	   ("softdep_setup_directory_change called on non-softdep filesystem"));
9356 
9357 	/*
9358 	 * Whiteouts do not need diradd dependencies.
9359 	 */
9360 	if (newinum != UFS_WINO) {
9361 		dap = malloc(sizeof(struct diradd),
9362 		    M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9363 		workitem_alloc(&dap->da_list, D_DIRADD, mp);
9364 		dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9365 		dap->da_offset = offset;
9366 		dap->da_newinum = newinum;
9367 		LIST_INIT(&dap->da_jwork);
9368 	}
9369 
9370 	/*
9371 	 * Allocate a new dirrem and ACQUIRE_LOCK.
9372 	 */
9373 	dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9374 	pagedep = dirrem->dm_pagedep;
9375 	/*
9376 	 * The possible values for isrmdir:
9377 	 *	0 - non-directory file rename
9378 	 *	1 - directory rename within same directory
9379 	 *   inum - directory rename to new directory of given inode number
9380 	 * When renaming to a new directory, we are both deleting and
9381 	 * creating a new directory entry, so the link count on the new
9382 	 * directory should not change. Thus we do not need the followup
9383 	 * dirrem which is usually done in handle_workitem_remove. We set
9384 	 * the DIRCHG flag to tell handle_workitem_remove to skip the
9385 	 * followup dirrem.
9386 	 */
9387 	if (isrmdir > 1)
9388 		dirrem->dm_state |= DIRCHG;
9389 
9390 	/*
9391 	 * Whiteouts have no additional dependencies,
9392 	 * so just put the dirrem on the correct list.
9393 	 */
9394 	if (newinum == UFS_WINO) {
9395 		if ((dirrem->dm_state & COMPLETE) == 0) {
9396 			LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9397 			    dm_next);
9398 		} else {
9399 			dirrem->dm_dirinum = pagedep->pd_ino;
9400 			if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9401 				add_to_worklist(&dirrem->dm_list, 0);
9402 		}
9403 		FREE_LOCK(ump);
9404 		return;
9405 	}
9406 	/*
9407 	 * Add the dirrem to the inodedep's pending remove list for quick
9408 	 * discovery later.  A valid nlinkdelta ensures that this lookup
9409 	 * will not fail.
9410 	 */
9411 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9412 		panic("softdep_setup_directory_change: Lost inodedep.");
9413 	dirrem->dm_state |= ONDEPLIST;
9414 	LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9415 
9416 	/*
9417 	 * If the COMPLETE flag is clear, then there were no active
9418 	 * entries and we want to roll back to the previous inode until
9419 	 * the new inode is committed to disk. If the COMPLETE flag is
9420 	 * set, then we have deleted an entry that never made it to disk.
9421 	 * If the entry we deleted resulted from a name change, then the old
9422 	 * inode reference still resides on disk. Any rollback that we do
9423 	 * needs to be to that old inode (returned to us in prevdirrem). If
9424 	 * the entry we deleted resulted from a create, then there is
9425 	 * no entry on the disk, so we want to roll back to zero rather
9426 	 * than the uncommitted inode. In either of the COMPLETE cases we
9427 	 * want to immediately free the unwritten and unreferenced inode.
9428 	 */
9429 	if ((dirrem->dm_state & COMPLETE) == 0) {
9430 		dap->da_previous = dirrem;
9431 	} else {
9432 		if (prevdirrem != NULL) {
9433 			dap->da_previous = prevdirrem;
9434 		} else {
9435 			dap->da_state &= ~DIRCHG;
9436 			dap->da_pagedep = pagedep;
9437 		}
9438 		dirrem->dm_dirinum = pagedep->pd_ino;
9439 		if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9440 			add_to_worklist(&dirrem->dm_list, 0);
9441 	}
9442 	/*
9443 	 * Lookup the jaddref for this journal entry.  We must finish
9444 	 * initializing it and make the diradd write dependent on it.
9445 	 * If we're not journaling, put it on the id_bufwait list if the
9446 	 * inode is not yet written. If it is written, do the post-inode
9447 	 * write processing to put it on the id_pendinghd list.
9448 	 */
9449 	inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9450 	if (MOUNTEDSUJ(mp)) {
9451 		jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9452 		    inoreflst);
9453 		KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9454 		    ("softdep_setup_directory_change: bad jaddref %p",
9455 		    jaddref));
9456 		jaddref->ja_diroff = dp->i_offset;
9457 		jaddref->ja_diradd = dap;
9458 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9459 		    dap, da_pdlist);
9460 		add_to_journal(&jaddref->ja_list);
9461 	} else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9462 		dap->da_state |= COMPLETE;
9463 		LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9464 		WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9465 	} else {
9466 		LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9467 		    dap, da_pdlist);
9468 		WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9469 	}
9470 	/*
9471 	 * If we're making a new name for a directory that has not been
9472 	 * committed when need to move the dot and dotdot references to
9473 	 * this new name.
9474 	 */
9475 	if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET)
9476 		merge_diradd(inodedep, dap);
9477 	FREE_LOCK(ump);
9478 }
9479 
9480 /*
9481  * Called whenever the link count on an inode is changed.
9482  * It creates an inode dependency so that the new reference(s)
9483  * to the inode cannot be committed to disk until the updated
9484  * inode has been written.
9485  */
9486 void
9487 softdep_change_linkcnt(ip)
9488 	struct inode *ip;	/* the inode with the increased link count */
9489 {
9490 	struct inodedep *inodedep;
9491 	struct ufsmount *ump;
9492 
9493 	ump = ITOUMP(ip);
9494 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9495 	    ("softdep_change_linkcnt called on non-softdep filesystem"));
9496 	ACQUIRE_LOCK(ump);
9497 	inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9498 	if (ip->i_nlink < ip->i_effnlink)
9499 		panic("softdep_change_linkcnt: bad delta");
9500 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9501 	FREE_LOCK(ump);
9502 }
9503 
9504 /*
9505  * Attach a sbdep dependency to the superblock buf so that we can keep
9506  * track of the head of the linked list of referenced but unlinked inodes.
9507  */
9508 void
9509 softdep_setup_sbupdate(ump, fs, bp)
9510 	struct ufsmount *ump;
9511 	struct fs *fs;
9512 	struct buf *bp;
9513 {
9514 	struct sbdep *sbdep;
9515 	struct worklist *wk;
9516 
9517 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9518 	    ("softdep_setup_sbupdate called on non-softdep filesystem"));
9519 	LIST_FOREACH(wk, &bp->b_dep, wk_list)
9520 		if (wk->wk_type == D_SBDEP)
9521 			break;
9522 	if (wk != NULL)
9523 		return;
9524 	sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9525 	workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9526 	sbdep->sb_fs = fs;
9527 	sbdep->sb_ump = ump;
9528 	ACQUIRE_LOCK(ump);
9529 	WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9530 	FREE_LOCK(ump);
9531 }
9532 
9533 /*
9534  * Return the first unlinked inodedep which is ready to be the head of the
9535  * list.  The inodedep and all those after it must have valid next pointers.
9536  */
9537 static struct inodedep *
9538 first_unlinked_inodedep(ump)
9539 	struct ufsmount *ump;
9540 {
9541 	struct inodedep *inodedep;
9542 	struct inodedep *idp;
9543 
9544 	LOCK_OWNED(ump);
9545 	for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9546 	    inodedep; inodedep = idp) {
9547 		if ((inodedep->id_state & UNLINKNEXT) == 0)
9548 			return (NULL);
9549 		idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9550 		if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9551 			break;
9552 		if ((inodedep->id_state & UNLINKPREV) == 0)
9553 			break;
9554 	}
9555 	return (inodedep);
9556 }
9557 
9558 /*
9559  * Set the sujfree unlinked head pointer prior to writing a superblock.
9560  */
9561 static void
9562 initiate_write_sbdep(sbdep)
9563 	struct sbdep *sbdep;
9564 {
9565 	struct inodedep *inodedep;
9566 	struct fs *bpfs;
9567 	struct fs *fs;
9568 
9569 	bpfs = sbdep->sb_fs;
9570 	fs = sbdep->sb_ump->um_fs;
9571 	inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9572 	if (inodedep) {
9573 		fs->fs_sujfree = inodedep->id_ino;
9574 		inodedep->id_state |= UNLINKPREV;
9575 	} else
9576 		fs->fs_sujfree = 0;
9577 	bpfs->fs_sujfree = fs->fs_sujfree;
9578 }
9579 
9580 /*
9581  * After a superblock is written determine whether it must be written again
9582  * due to a changing unlinked list head.
9583  */
9584 static int
9585 handle_written_sbdep(sbdep, bp)
9586 	struct sbdep *sbdep;
9587 	struct buf *bp;
9588 {
9589 	struct inodedep *inodedep;
9590 	struct fs *fs;
9591 
9592 	LOCK_OWNED(sbdep->sb_ump);
9593 	fs = sbdep->sb_fs;
9594 	/*
9595 	 * If the superblock doesn't match the in-memory list start over.
9596 	 */
9597 	inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9598 	if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9599 	    (inodedep == NULL && fs->fs_sujfree != 0)) {
9600 		bdirty(bp);
9601 		return (1);
9602 	}
9603 	WORKITEM_FREE(sbdep, D_SBDEP);
9604 	if (fs->fs_sujfree == 0)
9605 		return (0);
9606 	/*
9607 	 * Now that we have a record of this inode in stable store allow it
9608 	 * to be written to free up pending work.  Inodes may see a lot of
9609 	 * write activity after they are unlinked which we must not hold up.
9610 	 */
9611 	for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9612 		if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9613 			panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9614 			    inodedep, inodedep->id_state);
9615 		if (inodedep->id_state & UNLINKONLIST)
9616 			break;
9617 		inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9618 	}
9619 
9620 	return (0);
9621 }
9622 
9623 /*
9624  * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9625  */
9626 static void
9627 unlinked_inodedep(mp, inodedep)
9628 	struct mount *mp;
9629 	struct inodedep *inodedep;
9630 {
9631 	struct ufsmount *ump;
9632 
9633 	ump = VFSTOUFS(mp);
9634 	LOCK_OWNED(ump);
9635 	if (MOUNTEDSUJ(mp) == 0)
9636 		return;
9637 	ump->um_fs->fs_fmod = 1;
9638 	if (inodedep->id_state & UNLINKED)
9639 		panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9640 	inodedep->id_state |= UNLINKED;
9641 	TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9642 }
9643 
9644 /*
9645  * Remove an inodedep from the unlinked inodedep list.  This may require
9646  * disk writes if the inode has made it that far.
9647  */
9648 static void
9649 clear_unlinked_inodedep(inodedep)
9650 	struct inodedep *inodedep;
9651 {
9652 	struct ufs2_dinode *dip;
9653 	struct ufsmount *ump;
9654 	struct inodedep *idp;
9655 	struct inodedep *idn;
9656 	struct fs *fs;
9657 	struct buf *bp;
9658 	ino_t ino;
9659 	ino_t nino;
9660 	ino_t pino;
9661 	int error;
9662 
9663 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
9664 	fs = ump->um_fs;
9665 	ino = inodedep->id_ino;
9666 	error = 0;
9667 	for (;;) {
9668 		LOCK_OWNED(ump);
9669 		KASSERT((inodedep->id_state & UNLINKED) != 0,
9670 		    ("clear_unlinked_inodedep: inodedep %p not unlinked",
9671 		    inodedep));
9672 		/*
9673 		 * If nothing has yet been written simply remove us from
9674 		 * the in memory list and return.  This is the most common
9675 		 * case where handle_workitem_remove() loses the final
9676 		 * reference.
9677 		 */
9678 		if ((inodedep->id_state & UNLINKLINKS) == 0)
9679 			break;
9680 		/*
9681 		 * If we have a NEXT pointer and no PREV pointer we can simply
9682 		 * clear NEXT's PREV and remove ourselves from the list.  Be
9683 		 * careful not to clear PREV if the superblock points at
9684 		 * next as well.
9685 		 */
9686 		idn = TAILQ_NEXT(inodedep, id_unlinked);
9687 		if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9688 			if (idn && fs->fs_sujfree != idn->id_ino)
9689 				idn->id_state &= ~UNLINKPREV;
9690 			break;
9691 		}
9692 		/*
9693 		 * Here we have an inodedep which is actually linked into
9694 		 * the list.  We must remove it by forcing a write to the
9695 		 * link before us, whether it be the superblock or an inode.
9696 		 * Unfortunately the list may change while we're waiting
9697 		 * on the buf lock for either resource so we must loop until
9698 		 * we lock the right one.  If both the superblock and an
9699 		 * inode point to this inode we must clear the inode first
9700 		 * followed by the superblock.
9701 		 */
9702 		idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9703 		pino = 0;
9704 		if (idp && (idp->id_state & UNLINKNEXT))
9705 			pino = idp->id_ino;
9706 		FREE_LOCK(ump);
9707 		if (pino == 0) {
9708 			bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9709 			    (int)fs->fs_sbsize, 0, 0, 0);
9710 		} else {
9711 			error = bread(ump->um_devvp,
9712 			    fsbtodb(fs, ino_to_fsba(fs, pino)),
9713 			    (int)fs->fs_bsize, NOCRED, &bp);
9714 			if (error)
9715 				brelse(bp);
9716 		}
9717 		ACQUIRE_LOCK(ump);
9718 		if (error)
9719 			break;
9720 		/* If the list has changed restart the loop. */
9721 		idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9722 		nino = 0;
9723 		if (idp && (idp->id_state & UNLINKNEXT))
9724 			nino = idp->id_ino;
9725 		if (nino != pino ||
9726 		    (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9727 			FREE_LOCK(ump);
9728 			brelse(bp);
9729 			ACQUIRE_LOCK(ump);
9730 			continue;
9731 		}
9732 		nino = 0;
9733 		idn = TAILQ_NEXT(inodedep, id_unlinked);
9734 		if (idn)
9735 			nino = idn->id_ino;
9736 		/*
9737 		 * Remove us from the in memory list.  After this we cannot
9738 		 * access the inodedep.
9739 		 */
9740 		KASSERT((inodedep->id_state & UNLINKED) != 0,
9741 		    ("clear_unlinked_inodedep: inodedep %p not unlinked",
9742 		    inodedep));
9743 		inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9744 		TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9745 		FREE_LOCK(ump);
9746 		/*
9747 		 * The predecessor's next pointer is manually updated here
9748 		 * so that the NEXT flag is never cleared for an element
9749 		 * that is in the list.
9750 		 */
9751 		if (pino == 0) {
9752 			bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9753 			ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
9754 			softdep_setup_sbupdate(ump, (struct fs *)bp->b_data,
9755 			    bp);
9756 		} else if (fs->fs_magic == FS_UFS1_MAGIC) {
9757 			((struct ufs1_dinode *)bp->b_data +
9758 			    ino_to_fsbo(fs, pino))->di_freelink = nino;
9759 		} else {
9760 			dip = (struct ufs2_dinode *)bp->b_data +
9761 			    ino_to_fsbo(fs, pino);
9762 			dip->di_freelink = nino;
9763 			ffs_update_dinode_ckhash(fs, dip);
9764 		}
9765 		/*
9766 		 * If the bwrite fails we have no recourse to recover.  The
9767 		 * filesystem is corrupted already.
9768 		 */
9769 		bwrite(bp);
9770 		ACQUIRE_LOCK(ump);
9771 		/*
9772 		 * If the superblock pointer still needs to be cleared force
9773 		 * a write here.
9774 		 */
9775 		if (fs->fs_sujfree == ino) {
9776 			FREE_LOCK(ump);
9777 			bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9778 			    (int)fs->fs_sbsize, 0, 0, 0);
9779 			bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9780 			ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
9781 			softdep_setup_sbupdate(ump, (struct fs *)bp->b_data,
9782 			    bp);
9783 			bwrite(bp);
9784 			ACQUIRE_LOCK(ump);
9785 		}
9786 
9787 		if (fs->fs_sujfree != ino)
9788 			return;
9789 		panic("clear_unlinked_inodedep: Failed to clear free head");
9790 	}
9791 	if (inodedep->id_ino == fs->fs_sujfree)
9792 		panic("clear_unlinked_inodedep: Freeing head of free list");
9793 	inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9794 	TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9795 	return;
9796 }
9797 
9798 /*
9799  * This workitem decrements the inode's link count.
9800  * If the link count reaches zero, the file is removed.
9801  */
9802 static int
9803 handle_workitem_remove(dirrem, flags)
9804 	struct dirrem *dirrem;
9805 	int flags;
9806 {
9807 	struct inodedep *inodedep;
9808 	struct workhead dotdotwk;
9809 	struct worklist *wk;
9810 	struct ufsmount *ump;
9811 	struct mount *mp;
9812 	struct vnode *vp;
9813 	struct inode *ip;
9814 	ino_t oldinum;
9815 
9816 	if (dirrem->dm_state & ONWORKLIST)
9817 		panic("handle_workitem_remove: dirrem %p still on worklist",
9818 		    dirrem);
9819 	oldinum = dirrem->dm_oldinum;
9820 	mp = dirrem->dm_list.wk_mp;
9821 	ump = VFSTOUFS(mp);
9822 	flags |= LK_EXCLUSIVE;
9823 	if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0)
9824 		return (EBUSY);
9825 	ip = VTOI(vp);
9826 	ACQUIRE_LOCK(ump);
9827 	if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
9828 		panic("handle_workitem_remove: lost inodedep");
9829 	if (dirrem->dm_state & ONDEPLIST)
9830 		LIST_REMOVE(dirrem, dm_inonext);
9831 	KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
9832 	    ("handle_workitem_remove:  Journal entries not written."));
9833 
9834 	/*
9835 	 * Move all dependencies waiting on the remove to complete
9836 	 * from the dirrem to the inode inowait list to be completed
9837 	 * after the inode has been updated and written to disk.  Any
9838 	 * marked MKDIR_PARENT are saved to be completed when the .. ref
9839 	 * is removed.
9840 	 */
9841 	LIST_INIT(&dotdotwk);
9842 	while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
9843 		WORKLIST_REMOVE(wk);
9844 		if (wk->wk_state & MKDIR_PARENT) {
9845 			wk->wk_state &= ~MKDIR_PARENT;
9846 			WORKLIST_INSERT(&dotdotwk, wk);
9847 			continue;
9848 		}
9849 		WORKLIST_INSERT(&inodedep->id_inowait, wk);
9850 	}
9851 	LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
9852 	/*
9853 	 * Normal file deletion.
9854 	 */
9855 	if ((dirrem->dm_state & RMDIR) == 0) {
9856 		ip->i_nlink--;
9857 		DIP_SET(ip, i_nlink, ip->i_nlink);
9858 		ip->i_flag |= IN_CHANGE;
9859 		if (ip->i_nlink < ip->i_effnlink)
9860 			panic("handle_workitem_remove: bad file delta");
9861 		if (ip->i_nlink == 0)
9862 			unlinked_inodedep(mp, inodedep);
9863 		inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9864 		KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9865 		    ("handle_workitem_remove: worklist not empty. %s",
9866 		    TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
9867 		WORKITEM_FREE(dirrem, D_DIRREM);
9868 		FREE_LOCK(ump);
9869 		goto out;
9870 	}
9871 	/*
9872 	 * Directory deletion. Decrement reference count for both the
9873 	 * just deleted parent directory entry and the reference for ".".
9874 	 * Arrange to have the reference count on the parent decremented
9875 	 * to account for the loss of "..".
9876 	 */
9877 	ip->i_nlink -= 2;
9878 	DIP_SET(ip, i_nlink, ip->i_nlink);
9879 	ip->i_flag |= IN_CHANGE;
9880 	if (ip->i_nlink < ip->i_effnlink)
9881 		panic("handle_workitem_remove: bad dir delta");
9882 	if (ip->i_nlink == 0)
9883 		unlinked_inodedep(mp, inodedep);
9884 	inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9885 	/*
9886 	 * Rename a directory to a new parent. Since, we are both deleting
9887 	 * and creating a new directory entry, the link count on the new
9888 	 * directory should not change. Thus we skip the followup dirrem.
9889 	 */
9890 	if (dirrem->dm_state & DIRCHG) {
9891 		KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9892 		    ("handle_workitem_remove: DIRCHG and worklist not empty."));
9893 		WORKITEM_FREE(dirrem, D_DIRREM);
9894 		FREE_LOCK(ump);
9895 		goto out;
9896 	}
9897 	dirrem->dm_state = ONDEPLIST;
9898 	dirrem->dm_oldinum = dirrem->dm_dirinum;
9899 	/*
9900 	 * Place the dirrem on the parent's diremhd list.
9901 	 */
9902 	if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
9903 		panic("handle_workitem_remove: lost dir inodedep");
9904 	LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9905 	/*
9906 	 * If the allocated inode has never been written to disk, then
9907 	 * the on-disk inode is zero'ed and we can remove the file
9908 	 * immediately.  When journaling if the inode has been marked
9909 	 * unlinked and not DEPCOMPLETE we know it can never be written.
9910 	 */
9911 	inodedep_lookup(mp, oldinum, 0, &inodedep);
9912 	if (inodedep == NULL ||
9913 	    (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
9914 	    check_inode_unwritten(inodedep)) {
9915 		FREE_LOCK(ump);
9916 		vput(vp);
9917 		return handle_workitem_remove(dirrem, flags);
9918 	}
9919 	WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
9920 	FREE_LOCK(ump);
9921 	ip->i_flag |= IN_CHANGE;
9922 out:
9923 	ffs_update(vp, 0);
9924 	vput(vp);
9925 	return (0);
9926 }
9927 
9928 /*
9929  * Inode de-allocation dependencies.
9930  *
9931  * When an inode's link count is reduced to zero, it can be de-allocated. We
9932  * found it convenient to postpone de-allocation until after the inode is
9933  * written to disk with its new link count (zero).  At this point, all of the
9934  * on-disk inode's block pointers are nullified and, with careful dependency
9935  * list ordering, all dependencies related to the inode will be satisfied and
9936  * the corresponding dependency structures de-allocated.  So, if/when the
9937  * inode is reused, there will be no mixing of old dependencies with new
9938  * ones.  This artificial dependency is set up by the block de-allocation
9939  * procedure above (softdep_setup_freeblocks) and completed by the
9940  * following procedure.
9941  */
9942 static void
9943 handle_workitem_freefile(freefile)
9944 	struct freefile *freefile;
9945 {
9946 	struct workhead wkhd;
9947 	struct fs *fs;
9948 	struct ufsmount *ump;
9949 	int error;
9950 #ifdef INVARIANTS
9951 	struct inodedep *idp;
9952 #endif
9953 
9954 	ump = VFSTOUFS(freefile->fx_list.wk_mp);
9955 	fs = ump->um_fs;
9956 #ifdef INVARIANTS
9957 	ACQUIRE_LOCK(ump);
9958 	error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
9959 	FREE_LOCK(ump);
9960 	if (error)
9961 		panic("handle_workitem_freefile: inodedep %p survived", idp);
9962 #endif
9963 	UFS_LOCK(ump);
9964 	fs->fs_pendinginodes -= 1;
9965 	UFS_UNLOCK(ump);
9966 	LIST_INIT(&wkhd);
9967 	LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
9968 	if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
9969 	    freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
9970 		softdep_error("handle_workitem_freefile", error);
9971 	ACQUIRE_LOCK(ump);
9972 	WORKITEM_FREE(freefile, D_FREEFILE);
9973 	FREE_LOCK(ump);
9974 }
9975 
9976 
9977 /*
9978  * Helper function which unlinks marker element from work list and returns
9979  * the next element on the list.
9980  */
9981 static __inline struct worklist *
9982 markernext(struct worklist *marker)
9983 {
9984 	struct worklist *next;
9985 
9986 	next = LIST_NEXT(marker, wk_list);
9987 	LIST_REMOVE(marker, wk_list);
9988 	return next;
9989 }
9990 
9991 /*
9992  * Disk writes.
9993  *
9994  * The dependency structures constructed above are most actively used when file
9995  * system blocks are written to disk.  No constraints are placed on when a
9996  * block can be written, but unsatisfied update dependencies are made safe by
9997  * modifying (or replacing) the source memory for the duration of the disk
9998  * write.  When the disk write completes, the memory block is again brought
9999  * up-to-date.
10000  *
10001  * In-core inode structure reclamation.
10002  *
10003  * Because there are a finite number of "in-core" inode structures, they are
10004  * reused regularly.  By transferring all inode-related dependencies to the
10005  * in-memory inode block and indexing them separately (via "inodedep"s), we
10006  * can allow "in-core" inode structures to be reused at any time and avoid
10007  * any increase in contention.
10008  *
10009  * Called just before entering the device driver to initiate a new disk I/O.
10010  * The buffer must be locked, thus, no I/O completion operations can occur
10011  * while we are manipulating its associated dependencies.
10012  */
10013 static void
10014 softdep_disk_io_initiation(bp)
10015 	struct buf *bp;		/* structure describing disk write to occur */
10016 {
10017 	struct worklist *wk;
10018 	struct worklist marker;
10019 	struct inodedep *inodedep;
10020 	struct freeblks *freeblks;
10021 	struct jblkdep *jblkdep;
10022 	struct newblk *newblk;
10023 	struct ufsmount *ump;
10024 
10025 	/*
10026 	 * We only care about write operations. There should never
10027 	 * be dependencies for reads.
10028 	 */
10029 	if (bp->b_iocmd != BIO_WRITE)
10030 		panic("softdep_disk_io_initiation: not write");
10031 
10032 	if (bp->b_vflags & BV_BKGRDINPROG)
10033 		panic("softdep_disk_io_initiation: Writing buffer with "
10034 		    "background write in progress: %p", bp);
10035 
10036 	ump = softdep_bp_to_mp(bp);
10037 	if (ump == NULL)
10038 		return;
10039 
10040 	marker.wk_type = D_LAST + 1;	/* Not a normal workitem */
10041 	PHOLD(curproc);			/* Don't swap out kernel stack */
10042 	ACQUIRE_LOCK(ump);
10043 	/*
10044 	 * Do any necessary pre-I/O processing.
10045 	 */
10046 	for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10047 	     wk = markernext(&marker)) {
10048 		LIST_INSERT_AFTER(wk, &marker, wk_list);
10049 		switch (wk->wk_type) {
10050 
10051 		case D_PAGEDEP:
10052 			initiate_write_filepage(WK_PAGEDEP(wk), bp);
10053 			continue;
10054 
10055 		case D_INODEDEP:
10056 			inodedep = WK_INODEDEP(wk);
10057 			if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10058 				initiate_write_inodeblock_ufs1(inodedep, bp);
10059 			else
10060 				initiate_write_inodeblock_ufs2(inodedep, bp);
10061 			continue;
10062 
10063 		case D_INDIRDEP:
10064 			initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10065 			continue;
10066 
10067 		case D_BMSAFEMAP:
10068 			initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10069 			continue;
10070 
10071 		case D_JSEG:
10072 			WK_JSEG(wk)->js_buf = NULL;
10073 			continue;
10074 
10075 		case D_FREEBLKS:
10076 			freeblks = WK_FREEBLKS(wk);
10077 			jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10078 			/*
10079 			 * We have to wait for the freeblks to be journaled
10080 			 * before we can write an inodeblock with updated
10081 			 * pointers.  Be careful to arrange the marker so
10082 			 * we revisit the freeblks if it's not removed by
10083 			 * the first jwait().
10084 			 */
10085 			if (jblkdep != NULL) {
10086 				LIST_REMOVE(&marker, wk_list);
10087 				LIST_INSERT_BEFORE(wk, &marker, wk_list);
10088 				jwait(&jblkdep->jb_list, MNT_WAIT);
10089 			}
10090 			continue;
10091 		case D_ALLOCDIRECT:
10092 		case D_ALLOCINDIR:
10093 			/*
10094 			 * We have to wait for the jnewblk to be journaled
10095 			 * before we can write to a block if the contents
10096 			 * may be confused with an earlier file's indirect
10097 			 * at recovery time.  Handle the marker as described
10098 			 * above.
10099 			 */
10100 			newblk = WK_NEWBLK(wk);
10101 			if (newblk->nb_jnewblk != NULL &&
10102 			    indirblk_lookup(newblk->nb_list.wk_mp,
10103 			    newblk->nb_newblkno)) {
10104 				LIST_REMOVE(&marker, wk_list);
10105 				LIST_INSERT_BEFORE(wk, &marker, wk_list);
10106 				jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10107 			}
10108 			continue;
10109 
10110 		case D_SBDEP:
10111 			initiate_write_sbdep(WK_SBDEP(wk));
10112 			continue;
10113 
10114 		case D_MKDIR:
10115 		case D_FREEWORK:
10116 		case D_FREEDEP:
10117 		case D_JSEGDEP:
10118 			continue;
10119 
10120 		default:
10121 			panic("handle_disk_io_initiation: Unexpected type %s",
10122 			    TYPENAME(wk->wk_type));
10123 			/* NOTREACHED */
10124 		}
10125 	}
10126 	FREE_LOCK(ump);
10127 	PRELE(curproc);			/* Allow swapout of kernel stack */
10128 }
10129 
10130 /*
10131  * Called from within the procedure above to deal with unsatisfied
10132  * allocation dependencies in a directory. The buffer must be locked,
10133  * thus, no I/O completion operations can occur while we are
10134  * manipulating its associated dependencies.
10135  */
10136 static void
10137 initiate_write_filepage(pagedep, bp)
10138 	struct pagedep *pagedep;
10139 	struct buf *bp;
10140 {
10141 	struct jremref *jremref;
10142 	struct jmvref *jmvref;
10143 	struct dirrem *dirrem;
10144 	struct diradd *dap;
10145 	struct direct *ep;
10146 	int i;
10147 
10148 	if (pagedep->pd_state & IOSTARTED) {
10149 		/*
10150 		 * This can only happen if there is a driver that does not
10151 		 * understand chaining. Here biodone will reissue the call
10152 		 * to strategy for the incomplete buffers.
10153 		 */
10154 		printf("initiate_write_filepage: already started\n");
10155 		return;
10156 	}
10157 	pagedep->pd_state |= IOSTARTED;
10158 	/*
10159 	 * Wait for all journal remove dependencies to hit the disk.
10160 	 * We can not allow any potentially conflicting directory adds
10161 	 * to be visible before removes and rollback is too difficult.
10162 	 * The per-filesystem lock may be dropped and re-acquired, however
10163 	 * we hold the buf locked so the dependency can not go away.
10164 	 */
10165 	LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10166 		while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10167 			jwait(&jremref->jr_list, MNT_WAIT);
10168 	while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10169 		jwait(&jmvref->jm_list, MNT_WAIT);
10170 	for (i = 0; i < DAHASHSZ; i++) {
10171 		LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10172 			ep = (struct direct *)
10173 			    ((char *)bp->b_data + dap->da_offset);
10174 			if (ep->d_ino != dap->da_newinum)
10175 				panic("%s: dir inum %ju != new %ju",
10176 				    "initiate_write_filepage",
10177 				    (uintmax_t)ep->d_ino,
10178 				    (uintmax_t)dap->da_newinum);
10179 			if (dap->da_state & DIRCHG)
10180 				ep->d_ino = dap->da_previous->dm_oldinum;
10181 			else
10182 				ep->d_ino = 0;
10183 			dap->da_state &= ~ATTACHED;
10184 			dap->da_state |= UNDONE;
10185 		}
10186 	}
10187 }
10188 
10189 /*
10190  * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10191  * Note that any bug fixes made to this routine must be done in the
10192  * version found below.
10193  *
10194  * Called from within the procedure above to deal with unsatisfied
10195  * allocation dependencies in an inodeblock. The buffer must be
10196  * locked, thus, no I/O completion operations can occur while we
10197  * are manipulating its associated dependencies.
10198  */
10199 static void
10200 initiate_write_inodeblock_ufs1(inodedep, bp)
10201 	struct inodedep *inodedep;
10202 	struct buf *bp;			/* The inode block */
10203 {
10204 	struct allocdirect *adp, *lastadp;
10205 	struct ufs1_dinode *dp;
10206 	struct ufs1_dinode *sip;
10207 	struct inoref *inoref;
10208 	struct ufsmount *ump;
10209 	struct fs *fs;
10210 	ufs_lbn_t i;
10211 #ifdef INVARIANTS
10212 	ufs_lbn_t prevlbn = 0;
10213 #endif
10214 	int deplist;
10215 
10216 	if (inodedep->id_state & IOSTARTED)
10217 		panic("initiate_write_inodeblock_ufs1: already started");
10218 	inodedep->id_state |= IOSTARTED;
10219 	fs = inodedep->id_fs;
10220 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
10221 	LOCK_OWNED(ump);
10222 	dp = (struct ufs1_dinode *)bp->b_data +
10223 	    ino_to_fsbo(fs, inodedep->id_ino);
10224 
10225 	/*
10226 	 * If we're on the unlinked list but have not yet written our
10227 	 * next pointer initialize it here.
10228 	 */
10229 	if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10230 		struct inodedep *inon;
10231 
10232 		inon = TAILQ_NEXT(inodedep, id_unlinked);
10233 		dp->di_freelink = inon ? inon->id_ino : 0;
10234 	}
10235 	/*
10236 	 * If the bitmap is not yet written, then the allocated
10237 	 * inode cannot be written to disk.
10238 	 */
10239 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10240 		if (inodedep->id_savedino1 != NULL)
10241 			panic("initiate_write_inodeblock_ufs1: I/O underway");
10242 		FREE_LOCK(ump);
10243 		sip = malloc(sizeof(struct ufs1_dinode),
10244 		    M_SAVEDINO, M_SOFTDEP_FLAGS);
10245 		ACQUIRE_LOCK(ump);
10246 		inodedep->id_savedino1 = sip;
10247 		*inodedep->id_savedino1 = *dp;
10248 		bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10249 		dp->di_gen = inodedep->id_savedino1->di_gen;
10250 		dp->di_freelink = inodedep->id_savedino1->di_freelink;
10251 		return;
10252 	}
10253 	/*
10254 	 * If no dependencies, then there is nothing to roll back.
10255 	 */
10256 	inodedep->id_savedsize = dp->di_size;
10257 	inodedep->id_savedextsize = 0;
10258 	inodedep->id_savednlink = dp->di_nlink;
10259 	if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10260 	    TAILQ_EMPTY(&inodedep->id_inoreflst))
10261 		return;
10262 	/*
10263 	 * Revert the link count to that of the first unwritten journal entry.
10264 	 */
10265 	inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10266 	if (inoref)
10267 		dp->di_nlink = inoref->if_nlink;
10268 	/*
10269 	 * Set the dependencies to busy.
10270 	 */
10271 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10272 	     adp = TAILQ_NEXT(adp, ad_next)) {
10273 #ifdef INVARIANTS
10274 		if (deplist != 0 && prevlbn >= adp->ad_offset)
10275 			panic("softdep_write_inodeblock: lbn order");
10276 		prevlbn = adp->ad_offset;
10277 		if (adp->ad_offset < UFS_NDADDR &&
10278 		    dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10279 			panic("initiate_write_inodeblock_ufs1: "
10280 			    "direct pointer #%jd mismatch %d != %jd",
10281 			    (intmax_t)adp->ad_offset,
10282 			    dp->di_db[adp->ad_offset],
10283 			    (intmax_t)adp->ad_newblkno);
10284 		if (adp->ad_offset >= UFS_NDADDR &&
10285 		    dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10286 			panic("initiate_write_inodeblock_ufs1: "
10287 			    "indirect pointer #%jd mismatch %d != %jd",
10288 			    (intmax_t)adp->ad_offset - UFS_NDADDR,
10289 			    dp->di_ib[adp->ad_offset - UFS_NDADDR],
10290 			    (intmax_t)adp->ad_newblkno);
10291 		deplist |= 1 << adp->ad_offset;
10292 		if ((adp->ad_state & ATTACHED) == 0)
10293 			panic("initiate_write_inodeblock_ufs1: "
10294 			    "Unknown state 0x%x", adp->ad_state);
10295 #endif /* INVARIANTS */
10296 		adp->ad_state &= ~ATTACHED;
10297 		adp->ad_state |= UNDONE;
10298 	}
10299 	/*
10300 	 * The on-disk inode cannot claim to be any larger than the last
10301 	 * fragment that has been written. Otherwise, the on-disk inode
10302 	 * might have fragments that were not the last block in the file
10303 	 * which would corrupt the filesystem.
10304 	 */
10305 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10306 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10307 		if (adp->ad_offset >= UFS_NDADDR)
10308 			break;
10309 		dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10310 		/* keep going until hitting a rollback to a frag */
10311 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10312 			continue;
10313 		dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10314 		for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10315 #ifdef INVARIANTS
10316 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10317 				panic("initiate_write_inodeblock_ufs1: "
10318 				    "lost dep1");
10319 #endif /* INVARIANTS */
10320 			dp->di_db[i] = 0;
10321 		}
10322 		for (i = 0; i < UFS_NIADDR; i++) {
10323 #ifdef INVARIANTS
10324 			if (dp->di_ib[i] != 0 &&
10325 			    (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10326 				panic("initiate_write_inodeblock_ufs1: "
10327 				    "lost dep2");
10328 #endif /* INVARIANTS */
10329 			dp->di_ib[i] = 0;
10330 		}
10331 		return;
10332 	}
10333 	/*
10334 	 * If we have zero'ed out the last allocated block of the file,
10335 	 * roll back the size to the last currently allocated block.
10336 	 * We know that this last allocated block is a full-sized as
10337 	 * we already checked for fragments in the loop above.
10338 	 */
10339 	if (lastadp != NULL &&
10340 	    dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10341 		for (i = lastadp->ad_offset; i >= 0; i--)
10342 			if (dp->di_db[i] != 0)
10343 				break;
10344 		dp->di_size = (i + 1) * fs->fs_bsize;
10345 	}
10346 	/*
10347 	 * The only dependencies are for indirect blocks.
10348 	 *
10349 	 * The file size for indirect block additions is not guaranteed.
10350 	 * Such a guarantee would be non-trivial to achieve. The conventional
10351 	 * synchronous write implementation also does not make this guarantee.
10352 	 * Fsck should catch and fix discrepancies. Arguably, the file size
10353 	 * can be over-estimated without destroying integrity when the file
10354 	 * moves into the indirect blocks (i.e., is large). If we want to
10355 	 * postpone fsck, we are stuck with this argument.
10356 	 */
10357 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10358 		dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10359 }
10360 
10361 /*
10362  * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10363  * Note that any bug fixes made to this routine must be done in the
10364  * version found above.
10365  *
10366  * Called from within the procedure above to deal with unsatisfied
10367  * allocation dependencies in an inodeblock. The buffer must be
10368  * locked, thus, no I/O completion operations can occur while we
10369  * are manipulating its associated dependencies.
10370  */
10371 static void
10372 initiate_write_inodeblock_ufs2(inodedep, bp)
10373 	struct inodedep *inodedep;
10374 	struct buf *bp;			/* The inode block */
10375 {
10376 	struct allocdirect *adp, *lastadp;
10377 	struct ufs2_dinode *dp;
10378 	struct ufs2_dinode *sip;
10379 	struct inoref *inoref;
10380 	struct ufsmount *ump;
10381 	struct fs *fs;
10382 	ufs_lbn_t i;
10383 #ifdef INVARIANTS
10384 	ufs_lbn_t prevlbn = 0;
10385 #endif
10386 	int deplist;
10387 
10388 	if (inodedep->id_state & IOSTARTED)
10389 		panic("initiate_write_inodeblock_ufs2: already started");
10390 	inodedep->id_state |= IOSTARTED;
10391 	fs = inodedep->id_fs;
10392 	ump = VFSTOUFS(inodedep->id_list.wk_mp);
10393 	LOCK_OWNED(ump);
10394 	dp = (struct ufs2_dinode *)bp->b_data +
10395 	    ino_to_fsbo(fs, inodedep->id_ino);
10396 
10397 	/*
10398 	 * If we're on the unlinked list but have not yet written our
10399 	 * next pointer initialize it here.
10400 	 */
10401 	if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10402 		struct inodedep *inon;
10403 
10404 		inon = TAILQ_NEXT(inodedep, id_unlinked);
10405 		dp->di_freelink = inon ? inon->id_ino : 0;
10406 		ffs_update_dinode_ckhash(fs, dp);
10407 	}
10408 	/*
10409 	 * If the bitmap is not yet written, then the allocated
10410 	 * inode cannot be written to disk.
10411 	 */
10412 	if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10413 		if (inodedep->id_savedino2 != NULL)
10414 			panic("initiate_write_inodeblock_ufs2: I/O underway");
10415 		FREE_LOCK(ump);
10416 		sip = malloc(sizeof(struct ufs2_dinode),
10417 		    M_SAVEDINO, M_SOFTDEP_FLAGS);
10418 		ACQUIRE_LOCK(ump);
10419 		inodedep->id_savedino2 = sip;
10420 		*inodedep->id_savedino2 = *dp;
10421 		bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10422 		dp->di_gen = inodedep->id_savedino2->di_gen;
10423 		dp->di_freelink = inodedep->id_savedino2->di_freelink;
10424 		return;
10425 	}
10426 	/*
10427 	 * If no dependencies, then there is nothing to roll back.
10428 	 */
10429 	inodedep->id_savedsize = dp->di_size;
10430 	inodedep->id_savedextsize = dp->di_extsize;
10431 	inodedep->id_savednlink = dp->di_nlink;
10432 	if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10433 	    TAILQ_EMPTY(&inodedep->id_extupdt) &&
10434 	    TAILQ_EMPTY(&inodedep->id_inoreflst))
10435 		return;
10436 	/*
10437 	 * Revert the link count to that of the first unwritten journal entry.
10438 	 */
10439 	inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10440 	if (inoref)
10441 		dp->di_nlink = inoref->if_nlink;
10442 
10443 	/*
10444 	 * Set the ext data dependencies to busy.
10445 	 */
10446 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10447 	     adp = TAILQ_NEXT(adp, ad_next)) {
10448 #ifdef INVARIANTS
10449 		if (deplist != 0 && prevlbn >= adp->ad_offset)
10450 			panic("initiate_write_inodeblock_ufs2: lbn order");
10451 		prevlbn = adp->ad_offset;
10452 		if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10453 			panic("initiate_write_inodeblock_ufs2: "
10454 			    "ext pointer #%jd mismatch %jd != %jd",
10455 			    (intmax_t)adp->ad_offset,
10456 			    (intmax_t)dp->di_extb[adp->ad_offset],
10457 			    (intmax_t)adp->ad_newblkno);
10458 		deplist |= 1 << adp->ad_offset;
10459 		if ((adp->ad_state & ATTACHED) == 0)
10460 			panic("initiate_write_inodeblock_ufs2: Unknown "
10461 			    "state 0x%x", adp->ad_state);
10462 #endif /* INVARIANTS */
10463 		adp->ad_state &= ~ATTACHED;
10464 		adp->ad_state |= UNDONE;
10465 	}
10466 	/*
10467 	 * The on-disk inode cannot claim to be any larger than the last
10468 	 * fragment that has been written. Otherwise, the on-disk inode
10469 	 * might have fragments that were not the last block in the ext
10470 	 * data which would corrupt the filesystem.
10471 	 */
10472 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10473 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10474 		dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10475 		/* keep going until hitting a rollback to a frag */
10476 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10477 			continue;
10478 		dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10479 		for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10480 #ifdef INVARIANTS
10481 			if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10482 				panic("initiate_write_inodeblock_ufs2: "
10483 				    "lost dep1");
10484 #endif /* INVARIANTS */
10485 			dp->di_extb[i] = 0;
10486 		}
10487 		lastadp = NULL;
10488 		break;
10489 	}
10490 	/*
10491 	 * If we have zero'ed out the last allocated block of the ext
10492 	 * data, roll back the size to the last currently allocated block.
10493 	 * We know that this last allocated block is a full-sized as
10494 	 * we already checked for fragments in the loop above.
10495 	 */
10496 	if (lastadp != NULL &&
10497 	    dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10498 		for (i = lastadp->ad_offset; i >= 0; i--)
10499 			if (dp->di_extb[i] != 0)
10500 				break;
10501 		dp->di_extsize = (i + 1) * fs->fs_bsize;
10502 	}
10503 	/*
10504 	 * Set the file data dependencies to busy.
10505 	 */
10506 	for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10507 	     adp = TAILQ_NEXT(adp, ad_next)) {
10508 #ifdef INVARIANTS
10509 		if (deplist != 0 && prevlbn >= adp->ad_offset)
10510 			panic("softdep_write_inodeblock: lbn order");
10511 		if ((adp->ad_state & ATTACHED) == 0)
10512 			panic("inodedep %p and adp %p not attached", inodedep, adp);
10513 		prevlbn = adp->ad_offset;
10514 		if (adp->ad_offset < UFS_NDADDR &&
10515 		    dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10516 			panic("initiate_write_inodeblock_ufs2: "
10517 			    "direct pointer #%jd mismatch %jd != %jd",
10518 			    (intmax_t)adp->ad_offset,
10519 			    (intmax_t)dp->di_db[adp->ad_offset],
10520 			    (intmax_t)adp->ad_newblkno);
10521 		if (adp->ad_offset >= UFS_NDADDR &&
10522 		    dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10523 			panic("initiate_write_inodeblock_ufs2: "
10524 			    "indirect pointer #%jd mismatch %jd != %jd",
10525 			    (intmax_t)adp->ad_offset - UFS_NDADDR,
10526 			    (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10527 			    (intmax_t)adp->ad_newblkno);
10528 		deplist |= 1 << adp->ad_offset;
10529 		if ((adp->ad_state & ATTACHED) == 0)
10530 			panic("initiate_write_inodeblock_ufs2: Unknown "
10531 			     "state 0x%x", adp->ad_state);
10532 #endif /* INVARIANTS */
10533 		adp->ad_state &= ~ATTACHED;
10534 		adp->ad_state |= UNDONE;
10535 	}
10536 	/*
10537 	 * The on-disk inode cannot claim to be any larger than the last
10538 	 * fragment that has been written. Otherwise, the on-disk inode
10539 	 * might have fragments that were not the last block in the file
10540 	 * which would corrupt the filesystem.
10541 	 */
10542 	for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10543 	     lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10544 		if (adp->ad_offset >= UFS_NDADDR)
10545 			break;
10546 		dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10547 		/* keep going until hitting a rollback to a frag */
10548 		if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10549 			continue;
10550 		dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10551 		for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10552 #ifdef INVARIANTS
10553 			if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10554 				panic("initiate_write_inodeblock_ufs2: "
10555 				    "lost dep2");
10556 #endif /* INVARIANTS */
10557 			dp->di_db[i] = 0;
10558 		}
10559 		for (i = 0; i < UFS_NIADDR; i++) {
10560 #ifdef INVARIANTS
10561 			if (dp->di_ib[i] != 0 &&
10562 			    (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10563 				panic("initiate_write_inodeblock_ufs2: "
10564 				    "lost dep3");
10565 #endif /* INVARIANTS */
10566 			dp->di_ib[i] = 0;
10567 		}
10568 		ffs_update_dinode_ckhash(fs, dp);
10569 		return;
10570 	}
10571 	/*
10572 	 * If we have zero'ed out the last allocated block of the file,
10573 	 * roll back the size to the last currently allocated block.
10574 	 * We know that this last allocated block is a full-sized as
10575 	 * we already checked for fragments in the loop above.
10576 	 */
10577 	if (lastadp != NULL &&
10578 	    dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10579 		for (i = lastadp->ad_offset; i >= 0; i--)
10580 			if (dp->di_db[i] != 0)
10581 				break;
10582 		dp->di_size = (i + 1) * fs->fs_bsize;
10583 	}
10584 	/*
10585 	 * The only dependencies are for indirect blocks.
10586 	 *
10587 	 * The file size for indirect block additions is not guaranteed.
10588 	 * Such a guarantee would be non-trivial to achieve. The conventional
10589 	 * synchronous write implementation also does not make this guarantee.
10590 	 * Fsck should catch and fix discrepancies. Arguably, the file size
10591 	 * can be over-estimated without destroying integrity when the file
10592 	 * moves into the indirect blocks (i.e., is large). If we want to
10593 	 * postpone fsck, we are stuck with this argument.
10594 	 */
10595 	for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10596 		dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10597 	ffs_update_dinode_ckhash(fs, dp);
10598 }
10599 
10600 /*
10601  * Cancel an indirdep as a result of truncation.  Release all of the
10602  * children allocindirs and place their journal work on the appropriate
10603  * list.
10604  */
10605 static void
10606 cancel_indirdep(indirdep, bp, freeblks)
10607 	struct indirdep *indirdep;
10608 	struct buf *bp;
10609 	struct freeblks *freeblks;
10610 {
10611 	struct allocindir *aip;
10612 
10613 	/*
10614 	 * None of the indirect pointers will ever be visible,
10615 	 * so they can simply be tossed. GOINGAWAY ensures
10616 	 * that allocated pointers will be saved in the buffer
10617 	 * cache until they are freed. Note that they will
10618 	 * only be able to be found by their physical address
10619 	 * since the inode mapping the logical address will
10620 	 * be gone. The save buffer used for the safe copy
10621 	 * was allocated in setup_allocindir_phase2 using
10622 	 * the physical address so it could be used for this
10623 	 * purpose. Hence we swap the safe copy with the real
10624 	 * copy, allowing the safe copy to be freed and holding
10625 	 * on to the real copy for later use in indir_trunc.
10626 	 */
10627 	if (indirdep->ir_state & GOINGAWAY)
10628 		panic("cancel_indirdep: already gone");
10629 	if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10630 		indirdep->ir_state |= DEPCOMPLETE;
10631 		LIST_REMOVE(indirdep, ir_next);
10632 	}
10633 	indirdep->ir_state |= GOINGAWAY;
10634 	/*
10635 	 * Pass in bp for blocks still have journal writes
10636 	 * pending so we can cancel them on their own.
10637 	 */
10638 	while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
10639 		cancel_allocindir(aip, bp, freeblks, 0);
10640 	while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
10641 		cancel_allocindir(aip, NULL, freeblks, 0);
10642 	while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
10643 		cancel_allocindir(aip, NULL, freeblks, 0);
10644 	while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
10645 		cancel_allocindir(aip, NULL, freeblks, 0);
10646 	/*
10647 	 * If there are pending partial truncations we need to keep the
10648 	 * old block copy around until they complete.  This is because
10649 	 * the current b_data is not a perfect superset of the available
10650 	 * blocks.
10651 	 */
10652 	if (TAILQ_EMPTY(&indirdep->ir_trunc))
10653 		bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10654 	else
10655 		bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10656 	WORKLIST_REMOVE(&indirdep->ir_list);
10657 	WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10658 	indirdep->ir_bp = NULL;
10659 	indirdep->ir_freeblks = freeblks;
10660 }
10661 
10662 /*
10663  * Free an indirdep once it no longer has new pointers to track.
10664  */
10665 static void
10666 free_indirdep(indirdep)
10667 	struct indirdep *indirdep;
10668 {
10669 
10670 	KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10671 	    ("free_indirdep: Indir trunc list not empty."));
10672 	KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10673 	    ("free_indirdep: Complete head not empty."));
10674 	KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10675 	    ("free_indirdep: write head not empty."));
10676 	KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10677 	    ("free_indirdep: done head not empty."));
10678 	KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10679 	    ("free_indirdep: deplist head not empty."));
10680 	KASSERT((indirdep->ir_state & DEPCOMPLETE),
10681 	    ("free_indirdep: %p still on newblk list.", indirdep));
10682 	KASSERT(indirdep->ir_saveddata == NULL,
10683 	    ("free_indirdep: %p still has saved data.", indirdep));
10684 	if (indirdep->ir_state & ONWORKLIST)
10685 		WORKLIST_REMOVE(&indirdep->ir_list);
10686 	WORKITEM_FREE(indirdep, D_INDIRDEP);
10687 }
10688 
10689 /*
10690  * Called before a write to an indirdep.  This routine is responsible for
10691  * rolling back pointers to a safe state which includes only those
10692  * allocindirs which have been completed.
10693  */
10694 static void
10695 initiate_write_indirdep(indirdep, bp)
10696 	struct indirdep *indirdep;
10697 	struct buf *bp;
10698 {
10699 	struct ufsmount *ump;
10700 
10701 	indirdep->ir_state |= IOSTARTED;
10702 	if (indirdep->ir_state & GOINGAWAY)
10703 		panic("disk_io_initiation: indirdep gone");
10704 	/*
10705 	 * If there are no remaining dependencies, this will be writing
10706 	 * the real pointers.
10707 	 */
10708 	if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10709 	    TAILQ_EMPTY(&indirdep->ir_trunc))
10710 		return;
10711 	/*
10712 	 * Replace up-to-date version with safe version.
10713 	 */
10714 	if (indirdep->ir_saveddata == NULL) {
10715 		ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10716 		LOCK_OWNED(ump);
10717 		FREE_LOCK(ump);
10718 		indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10719 		    M_SOFTDEP_FLAGS);
10720 		ACQUIRE_LOCK(ump);
10721 	}
10722 	indirdep->ir_state &= ~ATTACHED;
10723 	indirdep->ir_state |= UNDONE;
10724 	bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10725 	bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10726 	    bp->b_bcount);
10727 }
10728 
10729 /*
10730  * Called when an inode has been cleared in a cg bitmap.  This finally
10731  * eliminates any canceled jaddrefs
10732  */
10733 void
10734 softdep_setup_inofree(mp, bp, ino, wkhd)
10735 	struct mount *mp;
10736 	struct buf *bp;
10737 	ino_t ino;
10738 	struct workhead *wkhd;
10739 {
10740 	struct worklist *wk, *wkn;
10741 	struct inodedep *inodedep;
10742 	struct ufsmount *ump;
10743 	uint8_t *inosused;
10744 	struct cg *cgp;
10745 	struct fs *fs;
10746 
10747 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10748 	    ("softdep_setup_inofree called on non-softdep filesystem"));
10749 	ump = VFSTOUFS(mp);
10750 	ACQUIRE_LOCK(ump);
10751 	fs = ump->um_fs;
10752 	cgp = (struct cg *)bp->b_data;
10753 	inosused = cg_inosused(cgp);
10754 	if (isset(inosused, ino % fs->fs_ipg))
10755 		panic("softdep_setup_inofree: inode %ju not freed.",
10756 		    (uintmax_t)ino);
10757 	if (inodedep_lookup(mp, ino, 0, &inodedep))
10758 		panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
10759 		    (uintmax_t)ino, inodedep);
10760 	if (wkhd) {
10761 		LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10762 			if (wk->wk_type != D_JADDREF)
10763 				continue;
10764 			WORKLIST_REMOVE(wk);
10765 			/*
10766 			 * We can free immediately even if the jaddref
10767 			 * isn't attached in a background write as now
10768 			 * the bitmaps are reconciled.
10769 			 */
10770 			wk->wk_state |= COMPLETE | ATTACHED;
10771 			free_jaddref(WK_JADDREF(wk));
10772 		}
10773 		jwork_move(&bp->b_dep, wkhd);
10774 	}
10775 	FREE_LOCK(ump);
10776 }
10777 
10778 
10779 /*
10780  * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10781  * map.  Any dependencies waiting for the write to clear are added to the
10782  * buf's list and any jnewblks that are being canceled are discarded
10783  * immediately.
10784  */
10785 void
10786 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
10787 	struct mount *mp;
10788 	struct buf *bp;
10789 	ufs2_daddr_t blkno;
10790 	int frags;
10791 	struct workhead *wkhd;
10792 {
10793 	struct bmsafemap *bmsafemap;
10794 	struct jnewblk *jnewblk;
10795 	struct ufsmount *ump;
10796 	struct worklist *wk;
10797 	struct fs *fs;
10798 #ifdef INVARIANTS
10799 	uint8_t *blksfree;
10800 	struct cg *cgp;
10801 	ufs2_daddr_t jstart;
10802 	ufs2_daddr_t jend;
10803 	ufs2_daddr_t end;
10804 	long bno;
10805 	int i;
10806 #endif
10807 
10808 	CTR3(KTR_SUJ,
10809 	    "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
10810 	    blkno, frags, wkhd);
10811 
10812 	ump = VFSTOUFS(mp);
10813 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
10814 	    ("softdep_setup_blkfree called on non-softdep filesystem"));
10815 	ACQUIRE_LOCK(ump);
10816 	/* Lookup the bmsafemap so we track when it is dirty. */
10817 	fs = ump->um_fs;
10818 	bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10819 	/*
10820 	 * Detach any jnewblks which have been canceled.  They must linger
10821 	 * until the bitmap is cleared again by ffs_blkfree() to prevent
10822 	 * an unjournaled allocation from hitting the disk.
10823 	 */
10824 	if (wkhd) {
10825 		while ((wk = LIST_FIRST(wkhd)) != NULL) {
10826 			CTR2(KTR_SUJ,
10827 			    "softdep_setup_blkfree: blkno %jd wk type %d",
10828 			    blkno, wk->wk_type);
10829 			WORKLIST_REMOVE(wk);
10830 			if (wk->wk_type != D_JNEWBLK) {
10831 				WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
10832 				continue;
10833 			}
10834 			jnewblk = WK_JNEWBLK(wk);
10835 			KASSERT(jnewblk->jn_state & GOINGAWAY,
10836 			    ("softdep_setup_blkfree: jnewblk not canceled."));
10837 #ifdef INVARIANTS
10838 			/*
10839 			 * Assert that this block is free in the bitmap
10840 			 * before we discard the jnewblk.
10841 			 */
10842 			cgp = (struct cg *)bp->b_data;
10843 			blksfree = cg_blksfree(cgp);
10844 			bno = dtogd(fs, jnewblk->jn_blkno);
10845 			for (i = jnewblk->jn_oldfrags;
10846 			    i < jnewblk->jn_frags; i++) {
10847 				if (isset(blksfree, bno + i))
10848 					continue;
10849 				panic("softdep_setup_blkfree: not free");
10850 			}
10851 #endif
10852 			/*
10853 			 * Even if it's not attached we can free immediately
10854 			 * as the new bitmap is correct.
10855 			 */
10856 			wk->wk_state |= COMPLETE | ATTACHED;
10857 			free_jnewblk(jnewblk);
10858 		}
10859 	}
10860 
10861 #ifdef INVARIANTS
10862 	/*
10863 	 * Assert that we are not freeing a block which has an outstanding
10864 	 * allocation dependency.
10865 	 */
10866 	fs = VFSTOUFS(mp)->um_fs;
10867 	bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10868 	end = blkno + frags;
10869 	LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
10870 		/*
10871 		 * Don't match against blocks that will be freed when the
10872 		 * background write is done.
10873 		 */
10874 		if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
10875 		    (COMPLETE | DEPCOMPLETE))
10876 			continue;
10877 		jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
10878 		jend = jnewblk->jn_blkno + jnewblk->jn_frags;
10879 		if ((blkno >= jstart && blkno < jend) ||
10880 		    (end > jstart && end <= jend)) {
10881 			printf("state 0x%X %jd - %d %d dep %p\n",
10882 			    jnewblk->jn_state, jnewblk->jn_blkno,
10883 			    jnewblk->jn_oldfrags, jnewblk->jn_frags,
10884 			    jnewblk->jn_dep);
10885 			panic("softdep_setup_blkfree: "
10886 			    "%jd-%jd(%d) overlaps with %jd-%jd",
10887 			    blkno, end, frags, jstart, jend);
10888 		}
10889 	}
10890 #endif
10891 	FREE_LOCK(ump);
10892 }
10893 
10894 /*
10895  * Revert a block allocation when the journal record that describes it
10896  * is not yet written.
10897  */
10898 static int
10899 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
10900 	struct jnewblk *jnewblk;
10901 	struct fs *fs;
10902 	struct cg *cgp;
10903 	uint8_t *blksfree;
10904 {
10905 	ufs1_daddr_t fragno;
10906 	long cgbno, bbase;
10907 	int frags, blk;
10908 	int i;
10909 
10910 	frags = 0;
10911 	cgbno = dtogd(fs, jnewblk->jn_blkno);
10912 	/*
10913 	 * We have to test which frags need to be rolled back.  We may
10914 	 * be operating on a stale copy when doing background writes.
10915 	 */
10916 	for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
10917 		if (isclr(blksfree, cgbno + i))
10918 			frags++;
10919 	if (frags == 0)
10920 		return (0);
10921 	/*
10922 	 * This is mostly ffs_blkfree() sans some validation and
10923 	 * superblock updates.
10924 	 */
10925 	if (frags == fs->fs_frag) {
10926 		fragno = fragstoblks(fs, cgbno);
10927 		ffs_setblock(fs, blksfree, fragno);
10928 		ffs_clusteracct(fs, cgp, fragno, 1);
10929 		cgp->cg_cs.cs_nbfree++;
10930 	} else {
10931 		cgbno += jnewblk->jn_oldfrags;
10932 		bbase = cgbno - fragnum(fs, cgbno);
10933 		/* Decrement the old frags.  */
10934 		blk = blkmap(fs, blksfree, bbase);
10935 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
10936 		/* Deallocate the fragment */
10937 		for (i = 0; i < frags; i++)
10938 			setbit(blksfree, cgbno + i);
10939 		cgp->cg_cs.cs_nffree += frags;
10940 		/* Add back in counts associated with the new frags */
10941 		blk = blkmap(fs, blksfree, bbase);
10942 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
10943 		/* If a complete block has been reassembled, account for it. */
10944 		fragno = fragstoblks(fs, bbase);
10945 		if (ffs_isblock(fs, blksfree, fragno)) {
10946 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
10947 			ffs_clusteracct(fs, cgp, fragno, 1);
10948 			cgp->cg_cs.cs_nbfree++;
10949 		}
10950 	}
10951 	stat_jnewblk++;
10952 	jnewblk->jn_state &= ~ATTACHED;
10953 	jnewblk->jn_state |= UNDONE;
10954 
10955 	return (frags);
10956 }
10957 
10958 static void
10959 initiate_write_bmsafemap(bmsafemap, bp)
10960 	struct bmsafemap *bmsafemap;
10961 	struct buf *bp;			/* The cg block. */
10962 {
10963 	struct jaddref *jaddref;
10964 	struct jnewblk *jnewblk;
10965 	uint8_t *inosused;
10966 	uint8_t *blksfree;
10967 	struct cg *cgp;
10968 	struct fs *fs;
10969 	ino_t ino;
10970 
10971 	/*
10972 	 * If this is a background write, we did this at the time that
10973 	 * the copy was made, so do not need to do it again.
10974 	 */
10975 	if (bmsafemap->sm_state & IOSTARTED)
10976 		return;
10977 	bmsafemap->sm_state |= IOSTARTED;
10978 	/*
10979 	 * Clear any inode allocations which are pending journal writes.
10980 	 */
10981 	if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
10982 		cgp = (struct cg *)bp->b_data;
10983 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
10984 		inosused = cg_inosused(cgp);
10985 		LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
10986 			ino = jaddref->ja_ino % fs->fs_ipg;
10987 			if (isset(inosused, ino)) {
10988 				if ((jaddref->ja_mode & IFMT) == IFDIR)
10989 					cgp->cg_cs.cs_ndir--;
10990 				cgp->cg_cs.cs_nifree++;
10991 				clrbit(inosused, ino);
10992 				jaddref->ja_state &= ~ATTACHED;
10993 				jaddref->ja_state |= UNDONE;
10994 				stat_jaddref++;
10995 			} else
10996 				panic("initiate_write_bmsafemap: inode %ju "
10997 				    "marked free", (uintmax_t)jaddref->ja_ino);
10998 		}
10999 	}
11000 	/*
11001 	 * Clear any block allocations which are pending journal writes.
11002 	 */
11003 	if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11004 		cgp = (struct cg *)bp->b_data;
11005 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11006 		blksfree = cg_blksfree(cgp);
11007 		LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11008 			if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11009 				continue;
11010 			panic("initiate_write_bmsafemap: block %jd "
11011 			    "marked free", jnewblk->jn_blkno);
11012 		}
11013 	}
11014 	/*
11015 	 * Move allocation lists to the written lists so they can be
11016 	 * cleared once the block write is complete.
11017 	 */
11018 	LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11019 	    inodedep, id_deps);
11020 	LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11021 	    newblk, nb_deps);
11022 	LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11023 	    wk_list);
11024 }
11025 
11026 /*
11027  * This routine is called during the completion interrupt
11028  * service routine for a disk write (from the procedure called
11029  * by the device driver to inform the filesystem caches of
11030  * a request completion).  It should be called early in this
11031  * procedure, before the block is made available to other
11032  * processes or other routines are called.
11033  *
11034  */
11035 static void
11036 softdep_disk_write_complete(bp)
11037 	struct buf *bp;		/* describes the completed disk write */
11038 {
11039 	struct worklist *wk;
11040 	struct worklist *owk;
11041 	struct ufsmount *ump;
11042 	struct workhead reattach;
11043 	struct freeblks *freeblks;
11044 	struct buf *sbp;
11045 
11046 	ump = softdep_bp_to_mp(bp);
11047 	KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11048 	    ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11049 	     "with outstanding dependencies for buffer %p", bp));
11050 	if (ump == NULL)
11051 		return;
11052 	/*
11053 	 * If an error occurred while doing the write, then the data
11054 	 * has not hit the disk and the dependencies cannot be processed.
11055 	 * But we do have to go through and roll forward any dependencies
11056 	 * that were rolled back before the disk write.
11057 	 */
11058 	sbp = NULL;
11059 	ACQUIRE_LOCK(ump);
11060 	if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11061 		LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11062 			switch (wk->wk_type) {
11063 
11064 			case D_PAGEDEP:
11065 				handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11066 				continue;
11067 
11068 			case D_INODEDEP:
11069 				handle_written_inodeblock(WK_INODEDEP(wk),
11070 				    bp, 0);
11071 				continue;
11072 
11073 			case D_BMSAFEMAP:
11074 				handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11075 				    bp, 0);
11076 				continue;
11077 
11078 			case D_INDIRDEP:
11079 				handle_written_indirdep(WK_INDIRDEP(wk),
11080 				    bp, &sbp, 0);
11081 				continue;
11082 			default:
11083 				/* nothing to roll forward */
11084 				continue;
11085 			}
11086 		}
11087 		FREE_LOCK(ump);
11088 		if (sbp)
11089 			brelse(sbp);
11090 		return;
11091 	}
11092 	LIST_INIT(&reattach);
11093 
11094 	/*
11095 	 * Ump SU lock must not be released anywhere in this code segment.
11096 	 */
11097 	owk = NULL;
11098 	while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11099 		WORKLIST_REMOVE(wk);
11100 		atomic_add_long(&dep_write[wk->wk_type], 1);
11101 		if (wk == owk)
11102 			panic("duplicate worklist: %p\n", wk);
11103 		owk = wk;
11104 		switch (wk->wk_type) {
11105 
11106 		case D_PAGEDEP:
11107 			if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11108 			    WRITESUCCEEDED))
11109 				WORKLIST_INSERT(&reattach, wk);
11110 			continue;
11111 
11112 		case D_INODEDEP:
11113 			if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11114 			    WRITESUCCEEDED))
11115 				WORKLIST_INSERT(&reattach, wk);
11116 			continue;
11117 
11118 		case D_BMSAFEMAP:
11119 			if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11120 			    WRITESUCCEEDED))
11121 				WORKLIST_INSERT(&reattach, wk);
11122 			continue;
11123 
11124 		case D_MKDIR:
11125 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11126 			continue;
11127 
11128 		case D_ALLOCDIRECT:
11129 			wk->wk_state |= COMPLETE;
11130 			handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11131 			continue;
11132 
11133 		case D_ALLOCINDIR:
11134 			wk->wk_state |= COMPLETE;
11135 			handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11136 			continue;
11137 
11138 		case D_INDIRDEP:
11139 			if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11140 			    WRITESUCCEEDED))
11141 				WORKLIST_INSERT(&reattach, wk);
11142 			continue;
11143 
11144 		case D_FREEBLKS:
11145 			wk->wk_state |= COMPLETE;
11146 			freeblks = WK_FREEBLKS(wk);
11147 			if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11148 			    LIST_EMPTY(&freeblks->fb_jblkdephd))
11149 				add_to_worklist(wk, WK_NODELAY);
11150 			continue;
11151 
11152 		case D_FREEWORK:
11153 			handle_written_freework(WK_FREEWORK(wk));
11154 			break;
11155 
11156 		case D_JSEGDEP:
11157 			free_jsegdep(WK_JSEGDEP(wk));
11158 			continue;
11159 
11160 		case D_JSEG:
11161 			handle_written_jseg(WK_JSEG(wk), bp);
11162 			continue;
11163 
11164 		case D_SBDEP:
11165 			if (handle_written_sbdep(WK_SBDEP(wk), bp))
11166 				WORKLIST_INSERT(&reattach, wk);
11167 			continue;
11168 
11169 		case D_FREEDEP:
11170 			free_freedep(WK_FREEDEP(wk));
11171 			continue;
11172 
11173 		default:
11174 			panic("handle_disk_write_complete: Unknown type %s",
11175 			    TYPENAME(wk->wk_type));
11176 			/* NOTREACHED */
11177 		}
11178 	}
11179 	/*
11180 	 * Reattach any requests that must be redone.
11181 	 */
11182 	while ((wk = LIST_FIRST(&reattach)) != NULL) {
11183 		WORKLIST_REMOVE(wk);
11184 		WORKLIST_INSERT(&bp->b_dep, wk);
11185 	}
11186 	FREE_LOCK(ump);
11187 	if (sbp)
11188 		brelse(sbp);
11189 }
11190 
11191 /*
11192  * Called from within softdep_disk_write_complete above.
11193  */
11194 static void
11195 handle_allocdirect_partdone(adp, wkhd)
11196 	struct allocdirect *adp;	/* the completed allocdirect */
11197 	struct workhead *wkhd;		/* Work to do when inode is writtne. */
11198 {
11199 	struct allocdirectlst *listhead;
11200 	struct allocdirect *listadp;
11201 	struct inodedep *inodedep;
11202 	long bsize;
11203 
11204 	LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11205 	if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11206 		return;
11207 	/*
11208 	 * The on-disk inode cannot claim to be any larger than the last
11209 	 * fragment that has been written. Otherwise, the on-disk inode
11210 	 * might have fragments that were not the last block in the file
11211 	 * which would corrupt the filesystem. Thus, we cannot free any
11212 	 * allocdirects after one whose ad_oldblkno claims a fragment as
11213 	 * these blocks must be rolled back to zero before writing the inode.
11214 	 * We check the currently active set of allocdirects in id_inoupdt
11215 	 * or id_extupdt as appropriate.
11216 	 */
11217 	inodedep = adp->ad_inodedep;
11218 	bsize = inodedep->id_fs->fs_bsize;
11219 	if (adp->ad_state & EXTDATA)
11220 		listhead = &inodedep->id_extupdt;
11221 	else
11222 		listhead = &inodedep->id_inoupdt;
11223 	TAILQ_FOREACH(listadp, listhead, ad_next) {
11224 		/* found our block */
11225 		if (listadp == adp)
11226 			break;
11227 		/* continue if ad_oldlbn is not a fragment */
11228 		if (listadp->ad_oldsize == 0 ||
11229 		    listadp->ad_oldsize == bsize)
11230 			continue;
11231 		/* hit a fragment */
11232 		return;
11233 	}
11234 	/*
11235 	 * If we have reached the end of the current list without
11236 	 * finding the just finished dependency, then it must be
11237 	 * on the future dependency list. Future dependencies cannot
11238 	 * be freed until they are moved to the current list.
11239 	 */
11240 	if (listadp == NULL) {
11241 #ifdef INVARIANTS
11242 		if (adp->ad_state & EXTDATA)
11243 			listhead = &inodedep->id_newextupdt;
11244 		else
11245 			listhead = &inodedep->id_newinoupdt;
11246 		TAILQ_FOREACH(listadp, listhead, ad_next)
11247 			/* found our block */
11248 			if (listadp == adp)
11249 				break;
11250 		if (listadp == NULL)
11251 			panic("handle_allocdirect_partdone: lost dep");
11252 #endif /* INVARIANTS */
11253 		return;
11254 	}
11255 	/*
11256 	 * If we have found the just finished dependency, then queue
11257 	 * it along with anything that follows it that is complete.
11258 	 * Since the pointer has not yet been written in the inode
11259 	 * as the dependency prevents it, place the allocdirect on the
11260 	 * bufwait list where it will be freed once the pointer is
11261 	 * valid.
11262 	 */
11263 	if (wkhd == NULL)
11264 		wkhd = &inodedep->id_bufwait;
11265 	for (; adp; adp = listadp) {
11266 		listadp = TAILQ_NEXT(adp, ad_next);
11267 		if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11268 			return;
11269 		TAILQ_REMOVE(listhead, adp, ad_next);
11270 		WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11271 	}
11272 }
11273 
11274 /*
11275  * Called from within softdep_disk_write_complete above.  This routine
11276  * completes successfully written allocindirs.
11277  */
11278 static void
11279 handle_allocindir_partdone(aip)
11280 	struct allocindir *aip;		/* the completed allocindir */
11281 {
11282 	struct indirdep *indirdep;
11283 
11284 	if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11285 		return;
11286 	indirdep = aip->ai_indirdep;
11287 	LIST_REMOVE(aip, ai_next);
11288 	/*
11289 	 * Don't set a pointer while the buffer is undergoing IO or while
11290 	 * we have active truncations.
11291 	 */
11292 	if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11293 		LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11294 		return;
11295 	}
11296 	if (indirdep->ir_state & UFS1FMT)
11297 		((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11298 		    aip->ai_newblkno;
11299 	else
11300 		((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11301 		    aip->ai_newblkno;
11302 	/*
11303 	 * Await the pointer write before freeing the allocindir.
11304 	 */
11305 	LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11306 }
11307 
11308 /*
11309  * Release segments held on a jwork list.
11310  */
11311 static void
11312 handle_jwork(wkhd)
11313 	struct workhead *wkhd;
11314 {
11315 	struct worklist *wk;
11316 
11317 	while ((wk = LIST_FIRST(wkhd)) != NULL) {
11318 		WORKLIST_REMOVE(wk);
11319 		switch (wk->wk_type) {
11320 		case D_JSEGDEP:
11321 			free_jsegdep(WK_JSEGDEP(wk));
11322 			continue;
11323 		case D_FREEDEP:
11324 			free_freedep(WK_FREEDEP(wk));
11325 			continue;
11326 		case D_FREEFRAG:
11327 			rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11328 			WORKITEM_FREE(wk, D_FREEFRAG);
11329 			continue;
11330 		case D_FREEWORK:
11331 			handle_written_freework(WK_FREEWORK(wk));
11332 			continue;
11333 		default:
11334 			panic("handle_jwork: Unknown type %s\n",
11335 			    TYPENAME(wk->wk_type));
11336 		}
11337 	}
11338 }
11339 
11340 /*
11341  * Handle the bufwait list on an inode when it is safe to release items
11342  * held there.  This normally happens after an inode block is written but
11343  * may be delayed and handled later if there are pending journal items that
11344  * are not yet safe to be released.
11345  */
11346 static struct freefile *
11347 handle_bufwait(inodedep, refhd)
11348 	struct inodedep *inodedep;
11349 	struct workhead *refhd;
11350 {
11351 	struct jaddref *jaddref;
11352 	struct freefile *freefile;
11353 	struct worklist *wk;
11354 
11355 	freefile = NULL;
11356 	while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11357 		WORKLIST_REMOVE(wk);
11358 		switch (wk->wk_type) {
11359 		case D_FREEFILE:
11360 			/*
11361 			 * We defer adding freefile to the worklist
11362 			 * until all other additions have been made to
11363 			 * ensure that it will be done after all the
11364 			 * old blocks have been freed.
11365 			 */
11366 			if (freefile != NULL)
11367 				panic("handle_bufwait: freefile");
11368 			freefile = WK_FREEFILE(wk);
11369 			continue;
11370 
11371 		case D_MKDIR:
11372 			handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11373 			continue;
11374 
11375 		case D_DIRADD:
11376 			diradd_inode_written(WK_DIRADD(wk), inodedep);
11377 			continue;
11378 
11379 		case D_FREEFRAG:
11380 			wk->wk_state |= COMPLETE;
11381 			if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11382 				add_to_worklist(wk, 0);
11383 			continue;
11384 
11385 		case D_DIRREM:
11386 			wk->wk_state |= COMPLETE;
11387 			add_to_worklist(wk, 0);
11388 			continue;
11389 
11390 		case D_ALLOCDIRECT:
11391 		case D_ALLOCINDIR:
11392 			free_newblk(WK_NEWBLK(wk));
11393 			continue;
11394 
11395 		case D_JNEWBLK:
11396 			wk->wk_state |= COMPLETE;
11397 			free_jnewblk(WK_JNEWBLK(wk));
11398 			continue;
11399 
11400 		/*
11401 		 * Save freed journal segments and add references on
11402 		 * the supplied list which will delay their release
11403 		 * until the cg bitmap is cleared on disk.
11404 		 */
11405 		case D_JSEGDEP:
11406 			if (refhd == NULL)
11407 				free_jsegdep(WK_JSEGDEP(wk));
11408 			else
11409 				WORKLIST_INSERT(refhd, wk);
11410 			continue;
11411 
11412 		case D_JADDREF:
11413 			jaddref = WK_JADDREF(wk);
11414 			TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11415 			    if_deps);
11416 			/*
11417 			 * Transfer any jaddrefs to the list to be freed with
11418 			 * the bitmap if we're handling a removed file.
11419 			 */
11420 			if (refhd == NULL) {
11421 				wk->wk_state |= COMPLETE;
11422 				free_jaddref(jaddref);
11423 			} else
11424 				WORKLIST_INSERT(refhd, wk);
11425 			continue;
11426 
11427 		default:
11428 			panic("handle_bufwait: Unknown type %p(%s)",
11429 			    wk, TYPENAME(wk->wk_type));
11430 			/* NOTREACHED */
11431 		}
11432 	}
11433 	return (freefile);
11434 }
11435 /*
11436  * Called from within softdep_disk_write_complete above to restore
11437  * in-memory inode block contents to their most up-to-date state. Note
11438  * that this routine is always called from interrupt level with further
11439  * interrupts from this device blocked.
11440  *
11441  * If the write did not succeed, we will do all the roll-forward
11442  * operations, but we will not take the actions that will allow its
11443  * dependencies to be processed.
11444  */
11445 static int
11446 handle_written_inodeblock(inodedep, bp, flags)
11447 	struct inodedep *inodedep;
11448 	struct buf *bp;		/* buffer containing the inode block */
11449 	int flags;
11450 {
11451 	struct freefile *freefile;
11452 	struct allocdirect *adp, *nextadp;
11453 	struct ufs1_dinode *dp1 = NULL;
11454 	struct ufs2_dinode *dp2 = NULL;
11455 	struct workhead wkhd;
11456 	int hadchanges, fstype;
11457 	ino_t freelink;
11458 
11459 	LIST_INIT(&wkhd);
11460 	hadchanges = 0;
11461 	freefile = NULL;
11462 	if ((inodedep->id_state & IOSTARTED) == 0)
11463 		panic("handle_written_inodeblock: not started");
11464 	inodedep->id_state &= ~IOSTARTED;
11465 	if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11466 		fstype = UFS1;
11467 		dp1 = (struct ufs1_dinode *)bp->b_data +
11468 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11469 		freelink = dp1->di_freelink;
11470 	} else {
11471 		fstype = UFS2;
11472 		dp2 = (struct ufs2_dinode *)bp->b_data +
11473 		    ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11474 		freelink = dp2->di_freelink;
11475 	}
11476 	/*
11477 	 * Leave this inodeblock dirty until it's in the list.
11478 	 */
11479 	if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11480 	    (flags & WRITESUCCEEDED)) {
11481 		struct inodedep *inon;
11482 
11483 		inon = TAILQ_NEXT(inodedep, id_unlinked);
11484 		if ((inon == NULL && freelink == 0) ||
11485 		    (inon && inon->id_ino == freelink)) {
11486 			if (inon)
11487 				inon->id_state |= UNLINKPREV;
11488 			inodedep->id_state |= UNLINKNEXT;
11489 		}
11490 		hadchanges = 1;
11491 	}
11492 	/*
11493 	 * If we had to rollback the inode allocation because of
11494 	 * bitmaps being incomplete, then simply restore it.
11495 	 * Keep the block dirty so that it will not be reclaimed until
11496 	 * all associated dependencies have been cleared and the
11497 	 * corresponding updates written to disk.
11498 	 */
11499 	if (inodedep->id_savedino1 != NULL) {
11500 		hadchanges = 1;
11501 		if (fstype == UFS1)
11502 			*dp1 = *inodedep->id_savedino1;
11503 		else
11504 			*dp2 = *inodedep->id_savedino2;
11505 		free(inodedep->id_savedino1, M_SAVEDINO);
11506 		inodedep->id_savedino1 = NULL;
11507 		if ((bp->b_flags & B_DELWRI) == 0)
11508 			stat_inode_bitmap++;
11509 		bdirty(bp);
11510 		/*
11511 		 * If the inode is clear here and GOINGAWAY it will never
11512 		 * be written.  Process the bufwait and clear any pending
11513 		 * work which may include the freefile.
11514 		 */
11515 		if (inodedep->id_state & GOINGAWAY)
11516 			goto bufwait;
11517 		return (1);
11518 	}
11519 	if (flags & WRITESUCCEEDED)
11520 		inodedep->id_state |= COMPLETE;
11521 	/*
11522 	 * Roll forward anything that had to be rolled back before
11523 	 * the inode could be updated.
11524 	 */
11525 	for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11526 		nextadp = TAILQ_NEXT(adp, ad_next);
11527 		if (adp->ad_state & ATTACHED)
11528 			panic("handle_written_inodeblock: new entry");
11529 		if (fstype == UFS1) {
11530 			if (adp->ad_offset < UFS_NDADDR) {
11531 				if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11532 					panic("%s %s #%jd mismatch %d != %jd",
11533 					    "handle_written_inodeblock:",
11534 					    "direct pointer",
11535 					    (intmax_t)adp->ad_offset,
11536 					    dp1->di_db[adp->ad_offset],
11537 					    (intmax_t)adp->ad_oldblkno);
11538 				dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11539 			} else {
11540 				if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11541 				    0)
11542 					panic("%s: %s #%jd allocated as %d",
11543 					    "handle_written_inodeblock",
11544 					    "indirect pointer",
11545 					    (intmax_t)adp->ad_offset -
11546 					    UFS_NDADDR,
11547 					    dp1->di_ib[adp->ad_offset -
11548 					    UFS_NDADDR]);
11549 				dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11550 				    adp->ad_newblkno;
11551 			}
11552 		} else {
11553 			if (adp->ad_offset < UFS_NDADDR) {
11554 				if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11555 					panic("%s: %s #%jd %s %jd != %jd",
11556 					    "handle_written_inodeblock",
11557 					    "direct pointer",
11558 					    (intmax_t)adp->ad_offset, "mismatch",
11559 					    (intmax_t)dp2->di_db[adp->ad_offset],
11560 					    (intmax_t)adp->ad_oldblkno);
11561 				dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11562 			} else {
11563 				if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11564 				    0)
11565 					panic("%s: %s #%jd allocated as %jd",
11566 					    "handle_written_inodeblock",
11567 					    "indirect pointer",
11568 					    (intmax_t)adp->ad_offset -
11569 					    UFS_NDADDR,
11570 					    (intmax_t)
11571 					    dp2->di_ib[adp->ad_offset -
11572 					    UFS_NDADDR]);
11573 				dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11574 				    adp->ad_newblkno;
11575 			}
11576 		}
11577 		adp->ad_state &= ~UNDONE;
11578 		adp->ad_state |= ATTACHED;
11579 		hadchanges = 1;
11580 	}
11581 	for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11582 		nextadp = TAILQ_NEXT(adp, ad_next);
11583 		if (adp->ad_state & ATTACHED)
11584 			panic("handle_written_inodeblock: new entry");
11585 		if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11586 			panic("%s: direct pointers #%jd %s %jd != %jd",
11587 			    "handle_written_inodeblock",
11588 			    (intmax_t)adp->ad_offset, "mismatch",
11589 			    (intmax_t)dp2->di_extb[adp->ad_offset],
11590 			    (intmax_t)adp->ad_oldblkno);
11591 		dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11592 		adp->ad_state &= ~UNDONE;
11593 		adp->ad_state |= ATTACHED;
11594 		hadchanges = 1;
11595 	}
11596 	if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11597 		stat_direct_blk_ptrs++;
11598 	/*
11599 	 * Reset the file size to its most up-to-date value.
11600 	 */
11601 	if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11602 		panic("handle_written_inodeblock: bad size");
11603 	if (inodedep->id_savednlink > UFS_LINK_MAX)
11604 		panic("handle_written_inodeblock: Invalid link count "
11605 		    "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11606 		    inodedep);
11607 	if (fstype == UFS1) {
11608 		if (dp1->di_nlink != inodedep->id_savednlink) {
11609 			dp1->di_nlink = inodedep->id_savednlink;
11610 			hadchanges = 1;
11611 		}
11612 		if (dp1->di_size != inodedep->id_savedsize) {
11613 			dp1->di_size = inodedep->id_savedsize;
11614 			hadchanges = 1;
11615 		}
11616 	} else {
11617 		if (dp2->di_nlink != inodedep->id_savednlink) {
11618 			dp2->di_nlink = inodedep->id_savednlink;
11619 			hadchanges = 1;
11620 		}
11621 		if (dp2->di_size != inodedep->id_savedsize) {
11622 			dp2->di_size = inodedep->id_savedsize;
11623 			hadchanges = 1;
11624 		}
11625 		if (dp2->di_extsize != inodedep->id_savedextsize) {
11626 			dp2->di_extsize = inodedep->id_savedextsize;
11627 			hadchanges = 1;
11628 		}
11629 	}
11630 	inodedep->id_savedsize = -1;
11631 	inodedep->id_savedextsize = -1;
11632 	inodedep->id_savednlink = -1;
11633 	/*
11634 	 * If there were any rollbacks in the inode block, then it must be
11635 	 * marked dirty so that its will eventually get written back in
11636 	 * its correct form.
11637 	 */
11638 	if (hadchanges) {
11639 		if (fstype == UFS2)
11640 			ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
11641 		bdirty(bp);
11642 	}
11643 bufwait:
11644 	/*
11645 	 * If the write did not succeed, we have done all the roll-forward
11646 	 * operations, but we cannot take the actions that will allow its
11647 	 * dependencies to be processed.
11648 	 */
11649 	if ((flags & WRITESUCCEEDED) == 0)
11650 		return (hadchanges);
11651 	/*
11652 	 * Process any allocdirects that completed during the update.
11653 	 */
11654 	if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11655 		handle_allocdirect_partdone(adp, &wkhd);
11656 	if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11657 		handle_allocdirect_partdone(adp, &wkhd);
11658 	/*
11659 	 * Process deallocations that were held pending until the
11660 	 * inode had been written to disk. Freeing of the inode
11661 	 * is delayed until after all blocks have been freed to
11662 	 * avoid creation of new <vfsid, inum, lbn> triples
11663 	 * before the old ones have been deleted.  Completely
11664 	 * unlinked inodes are not processed until the unlinked
11665 	 * inode list is written or the last reference is removed.
11666 	 */
11667 	if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11668 		freefile = handle_bufwait(inodedep, NULL);
11669 		if (freefile && !LIST_EMPTY(&wkhd)) {
11670 			WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11671 			freefile = NULL;
11672 		}
11673 	}
11674 	/*
11675 	 * Move rolled forward dependency completions to the bufwait list
11676 	 * now that those that were already written have been processed.
11677 	 */
11678 	if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11679 		panic("handle_written_inodeblock: bufwait but no changes");
11680 	jwork_move(&inodedep->id_bufwait, &wkhd);
11681 
11682 	if (freefile != NULL) {
11683 		/*
11684 		 * If the inode is goingaway it was never written.  Fake up
11685 		 * the state here so free_inodedep() can succeed.
11686 		 */
11687 		if (inodedep->id_state & GOINGAWAY)
11688 			inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11689 		if (free_inodedep(inodedep) == 0)
11690 			panic("handle_written_inodeblock: live inodedep %p",
11691 			    inodedep);
11692 		add_to_worklist(&freefile->fx_list, 0);
11693 		return (0);
11694 	}
11695 
11696 	/*
11697 	 * If no outstanding dependencies, free it.
11698 	 */
11699 	if (free_inodedep(inodedep) ||
11700 	    (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11701 	     TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11702 	     TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11703 	     LIST_FIRST(&inodedep->id_bufwait) == 0))
11704 		return (0);
11705 	return (hadchanges);
11706 }
11707 
11708 /*
11709  * Perform needed roll-forwards and kick off any dependencies that
11710  * can now be processed.
11711  *
11712  * If the write did not succeed, we will do all the roll-forward
11713  * operations, but we will not take the actions that will allow its
11714  * dependencies to be processed.
11715  */
11716 static int
11717 handle_written_indirdep(indirdep, bp, bpp, flags)
11718 	struct indirdep *indirdep;
11719 	struct buf *bp;
11720 	struct buf **bpp;
11721 	int flags;
11722 {
11723 	struct allocindir *aip;
11724 	struct buf *sbp;
11725 	int chgs;
11726 
11727 	if (indirdep->ir_state & GOINGAWAY)
11728 		panic("handle_written_indirdep: indirdep gone");
11729 	if ((indirdep->ir_state & IOSTARTED) == 0)
11730 		panic("handle_written_indirdep: IO not started");
11731 	chgs = 0;
11732 	/*
11733 	 * If there were rollbacks revert them here.
11734 	 */
11735 	if (indirdep->ir_saveddata) {
11736 		bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11737 		if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11738 			free(indirdep->ir_saveddata, M_INDIRDEP);
11739 			indirdep->ir_saveddata = NULL;
11740 		}
11741 		chgs = 1;
11742 	}
11743 	indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11744 	indirdep->ir_state |= ATTACHED;
11745 	/*
11746 	 * If the write did not succeed, we have done all the roll-forward
11747 	 * operations, but we cannot take the actions that will allow its
11748 	 * dependencies to be processed.
11749 	 */
11750 	if ((flags & WRITESUCCEEDED) == 0) {
11751 		stat_indir_blk_ptrs++;
11752 		bdirty(bp);
11753 		return (1);
11754 	}
11755 	/*
11756 	 * Move allocindirs with written pointers to the completehd if
11757 	 * the indirdep's pointer is not yet written.  Otherwise
11758 	 * free them here.
11759 	 */
11760 	while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
11761 		LIST_REMOVE(aip, ai_next);
11762 		if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11763 			LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11764 			    ai_next);
11765 			newblk_freefrag(&aip->ai_block);
11766 			continue;
11767 		}
11768 		free_newblk(&aip->ai_block);
11769 	}
11770 	/*
11771 	 * Move allocindirs that have finished dependency processing from
11772 	 * the done list to the write list after updating the pointers.
11773 	 */
11774 	if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11775 		while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
11776 			handle_allocindir_partdone(aip);
11777 			if (aip == LIST_FIRST(&indirdep->ir_donehd))
11778 				panic("disk_write_complete: not gone");
11779 			chgs = 1;
11780 		}
11781 	}
11782 	/*
11783 	 * Preserve the indirdep if there were any changes or if it is not
11784 	 * yet valid on disk.
11785 	 */
11786 	if (chgs) {
11787 		stat_indir_blk_ptrs++;
11788 		bdirty(bp);
11789 		return (1);
11790 	}
11791 	/*
11792 	 * If there were no changes we can discard the savedbp and detach
11793 	 * ourselves from the buf.  We are only carrying completed pointers
11794 	 * in this case.
11795 	 */
11796 	sbp = indirdep->ir_savebp;
11797 	sbp->b_flags |= B_INVAL | B_NOCACHE;
11798 	indirdep->ir_savebp = NULL;
11799 	indirdep->ir_bp = NULL;
11800 	if (*bpp != NULL)
11801 		panic("handle_written_indirdep: bp already exists.");
11802 	*bpp = sbp;
11803 	/*
11804 	 * The indirdep may not be freed until its parent points at it.
11805 	 */
11806 	if (indirdep->ir_state & DEPCOMPLETE)
11807 		free_indirdep(indirdep);
11808 
11809 	return (0);
11810 }
11811 
11812 /*
11813  * Process a diradd entry after its dependent inode has been written.
11814  */
11815 static void
11816 diradd_inode_written(dap, inodedep)
11817 	struct diradd *dap;
11818 	struct inodedep *inodedep;
11819 {
11820 
11821 	LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
11822 	dap->da_state |= COMPLETE;
11823 	complete_diradd(dap);
11824 	WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
11825 }
11826 
11827 /*
11828  * Returns true if the bmsafemap will have rollbacks when written.  Must only
11829  * be called with the per-filesystem lock and the buf lock on the cg held.
11830  */
11831 static int
11832 bmsafemap_backgroundwrite(bmsafemap, bp)
11833 	struct bmsafemap *bmsafemap;
11834 	struct buf *bp;
11835 {
11836 	int dirty;
11837 
11838 	LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
11839 	dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
11840 	    !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
11841 	/*
11842 	 * If we're initiating a background write we need to process the
11843 	 * rollbacks as they exist now, not as they exist when IO starts.
11844 	 * No other consumers will look at the contents of the shadowed
11845 	 * buf so this is safe to do here.
11846 	 */
11847 	if (bp->b_xflags & BX_BKGRDMARKER)
11848 		initiate_write_bmsafemap(bmsafemap, bp);
11849 
11850 	return (dirty);
11851 }
11852 
11853 /*
11854  * Re-apply an allocation when a cg write is complete.
11855  */
11856 static int
11857 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
11858 	struct jnewblk *jnewblk;
11859 	struct fs *fs;
11860 	struct cg *cgp;
11861 	uint8_t *blksfree;
11862 {
11863 	ufs1_daddr_t fragno;
11864 	ufs2_daddr_t blkno;
11865 	long cgbno, bbase;
11866 	int frags, blk;
11867 	int i;
11868 
11869 	frags = 0;
11870 	cgbno = dtogd(fs, jnewblk->jn_blkno);
11871 	for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
11872 		if (isclr(blksfree, cgbno + i))
11873 			panic("jnewblk_rollforward: re-allocated fragment");
11874 		frags++;
11875 	}
11876 	if (frags == fs->fs_frag) {
11877 		blkno = fragstoblks(fs, cgbno);
11878 		ffs_clrblock(fs, blksfree, (long)blkno);
11879 		ffs_clusteracct(fs, cgp, blkno, -1);
11880 		cgp->cg_cs.cs_nbfree--;
11881 	} else {
11882 		bbase = cgbno - fragnum(fs, cgbno);
11883 		cgbno += jnewblk->jn_oldfrags;
11884                 /* If a complete block had been reassembled, account for it. */
11885 		fragno = fragstoblks(fs, bbase);
11886 		if (ffs_isblock(fs, blksfree, fragno)) {
11887 			cgp->cg_cs.cs_nffree += fs->fs_frag;
11888 			ffs_clusteracct(fs, cgp, fragno, -1);
11889 			cgp->cg_cs.cs_nbfree--;
11890 		}
11891 		/* Decrement the old frags.  */
11892 		blk = blkmap(fs, blksfree, bbase);
11893 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11894 		/* Allocate the fragment */
11895 		for (i = 0; i < frags; i++)
11896 			clrbit(blksfree, cgbno + i);
11897 		cgp->cg_cs.cs_nffree -= frags;
11898 		/* Add back in counts associated with the new frags */
11899 		blk = blkmap(fs, blksfree, bbase);
11900 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11901 	}
11902 	return (frags);
11903 }
11904 
11905 /*
11906  * Complete a write to a bmsafemap structure.  Roll forward any bitmap
11907  * changes if it's not a background write.  Set all written dependencies
11908  * to DEPCOMPLETE and free the structure if possible.
11909  *
11910  * If the write did not succeed, we will do all the roll-forward
11911  * operations, but we will not take the actions that will allow its
11912  * dependencies to be processed.
11913  */
11914 static int
11915 handle_written_bmsafemap(bmsafemap, bp, flags)
11916 	struct bmsafemap *bmsafemap;
11917 	struct buf *bp;
11918 	int flags;
11919 {
11920 	struct newblk *newblk;
11921 	struct inodedep *inodedep;
11922 	struct jaddref *jaddref, *jatmp;
11923 	struct jnewblk *jnewblk, *jntmp;
11924 	struct ufsmount *ump;
11925 	uint8_t *inosused;
11926 	uint8_t *blksfree;
11927 	struct cg *cgp;
11928 	struct fs *fs;
11929 	ino_t ino;
11930 	int foreground;
11931 	int chgs;
11932 
11933 	if ((bmsafemap->sm_state & IOSTARTED) == 0)
11934 		panic("handle_written_bmsafemap: Not started\n");
11935 	ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
11936 	chgs = 0;
11937 	bmsafemap->sm_state &= ~IOSTARTED;
11938 	foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
11939 	/*
11940 	 * If write was successful, release journal work that was waiting
11941 	 * on the write. Otherwise move the work back.
11942 	 */
11943 	if (flags & WRITESUCCEEDED)
11944 		handle_jwork(&bmsafemap->sm_freewr);
11945 	else
11946 		LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
11947 		    worklist, wk_list);
11948 
11949 	/*
11950 	 * Restore unwritten inode allocation pending jaddref writes.
11951 	 */
11952 	if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
11953 		cgp = (struct cg *)bp->b_data;
11954 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11955 		inosused = cg_inosused(cgp);
11956 		LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
11957 		    ja_bmdeps, jatmp) {
11958 			if ((jaddref->ja_state & UNDONE) == 0)
11959 				continue;
11960 			ino = jaddref->ja_ino % fs->fs_ipg;
11961 			if (isset(inosused, ino))
11962 				panic("handle_written_bmsafemap: "
11963 				    "re-allocated inode");
11964 			/* Do the roll-forward only if it's a real copy. */
11965 			if (foreground) {
11966 				if ((jaddref->ja_mode & IFMT) == IFDIR)
11967 					cgp->cg_cs.cs_ndir++;
11968 				cgp->cg_cs.cs_nifree--;
11969 				setbit(inosused, ino);
11970 				chgs = 1;
11971 			}
11972 			jaddref->ja_state &= ~UNDONE;
11973 			jaddref->ja_state |= ATTACHED;
11974 			free_jaddref(jaddref);
11975 		}
11976 	}
11977 	/*
11978 	 * Restore any block allocations which are pending journal writes.
11979 	 */
11980 	if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11981 		cgp = (struct cg *)bp->b_data;
11982 		fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11983 		blksfree = cg_blksfree(cgp);
11984 		LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
11985 		    jntmp) {
11986 			if ((jnewblk->jn_state & UNDONE) == 0)
11987 				continue;
11988 			/* Do the roll-forward only if it's a real copy. */
11989 			if (foreground &&
11990 			    jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
11991 				chgs = 1;
11992 			jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
11993 			jnewblk->jn_state |= ATTACHED;
11994 			free_jnewblk(jnewblk);
11995 		}
11996 	}
11997 	/*
11998 	 * If the write did not succeed, we have done all the roll-forward
11999 	 * operations, but we cannot take the actions that will allow its
12000 	 * dependencies to be processed.
12001 	 */
12002 	if ((flags & WRITESUCCEEDED) == 0) {
12003 		LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12004 		    newblk, nb_deps);
12005 		LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12006 		    worklist, wk_list);
12007 		if (foreground)
12008 			bdirty(bp);
12009 		return (1);
12010 	}
12011 	while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12012 		newblk->nb_state |= DEPCOMPLETE;
12013 		newblk->nb_state &= ~ONDEPLIST;
12014 		newblk->nb_bmsafemap = NULL;
12015 		LIST_REMOVE(newblk, nb_deps);
12016 		if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12017 			handle_allocdirect_partdone(
12018 			    WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12019 		else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12020 			handle_allocindir_partdone(
12021 			    WK_ALLOCINDIR(&newblk->nb_list));
12022 		else if (newblk->nb_list.wk_type != D_NEWBLK)
12023 			panic("handle_written_bmsafemap: Unexpected type: %s",
12024 			    TYPENAME(newblk->nb_list.wk_type));
12025 	}
12026 	while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12027 		inodedep->id_state |= DEPCOMPLETE;
12028 		inodedep->id_state &= ~ONDEPLIST;
12029 		LIST_REMOVE(inodedep, id_deps);
12030 		inodedep->id_bmsafemap = NULL;
12031 	}
12032 	LIST_REMOVE(bmsafemap, sm_next);
12033 	if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12034 	    LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12035 	    LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12036 	    LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12037 	    LIST_EMPTY(&bmsafemap->sm_freehd)) {
12038 		LIST_REMOVE(bmsafemap, sm_hash);
12039 		WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12040 		return (0);
12041 	}
12042 	LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12043 	if (foreground)
12044 		bdirty(bp);
12045 	return (1);
12046 }
12047 
12048 /*
12049  * Try to free a mkdir dependency.
12050  */
12051 static void
12052 complete_mkdir(mkdir)
12053 	struct mkdir *mkdir;
12054 {
12055 	struct diradd *dap;
12056 
12057 	if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12058 		return;
12059 	LIST_REMOVE(mkdir, md_mkdirs);
12060 	dap = mkdir->md_diradd;
12061 	dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12062 	if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12063 		dap->da_state |= DEPCOMPLETE;
12064 		complete_diradd(dap);
12065 	}
12066 	WORKITEM_FREE(mkdir, D_MKDIR);
12067 }
12068 
12069 /*
12070  * Handle the completion of a mkdir dependency.
12071  */
12072 static void
12073 handle_written_mkdir(mkdir, type)
12074 	struct mkdir *mkdir;
12075 	int type;
12076 {
12077 
12078 	if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12079 		panic("handle_written_mkdir: bad type");
12080 	mkdir->md_state |= COMPLETE;
12081 	complete_mkdir(mkdir);
12082 }
12083 
12084 static int
12085 free_pagedep(pagedep)
12086 	struct pagedep *pagedep;
12087 {
12088 	int i;
12089 
12090 	if (pagedep->pd_state & NEWBLOCK)
12091 		return (0);
12092 	if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12093 		return (0);
12094 	for (i = 0; i < DAHASHSZ; i++)
12095 		if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12096 			return (0);
12097 	if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12098 		return (0);
12099 	if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12100 		return (0);
12101 	if (pagedep->pd_state & ONWORKLIST)
12102 		WORKLIST_REMOVE(&pagedep->pd_list);
12103 	LIST_REMOVE(pagedep, pd_hash);
12104 	WORKITEM_FREE(pagedep, D_PAGEDEP);
12105 
12106 	return (1);
12107 }
12108 
12109 /*
12110  * Called from within softdep_disk_write_complete above.
12111  * A write operation was just completed. Removed inodes can
12112  * now be freed and associated block pointers may be committed.
12113  * Note that this routine is always called from interrupt level
12114  * with further interrupts from this device blocked.
12115  *
12116  * If the write did not succeed, we will do all the roll-forward
12117  * operations, but we will not take the actions that will allow its
12118  * dependencies to be processed.
12119  */
12120 static int
12121 handle_written_filepage(pagedep, bp, flags)
12122 	struct pagedep *pagedep;
12123 	struct buf *bp;		/* buffer containing the written page */
12124 	int flags;
12125 {
12126 	struct dirrem *dirrem;
12127 	struct diradd *dap, *nextdap;
12128 	struct direct *ep;
12129 	int i, chgs;
12130 
12131 	if ((pagedep->pd_state & IOSTARTED) == 0)
12132 		panic("handle_written_filepage: not started");
12133 	pagedep->pd_state &= ~IOSTARTED;
12134 	if ((flags & WRITESUCCEEDED) == 0)
12135 		goto rollforward;
12136 	/*
12137 	 * Process any directory removals that have been committed.
12138 	 */
12139 	while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12140 		LIST_REMOVE(dirrem, dm_next);
12141 		dirrem->dm_state |= COMPLETE;
12142 		dirrem->dm_dirinum = pagedep->pd_ino;
12143 		KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12144 		    ("handle_written_filepage: Journal entries not written."));
12145 		add_to_worklist(&dirrem->dm_list, 0);
12146 	}
12147 	/*
12148 	 * Free any directory additions that have been committed.
12149 	 * If it is a newly allocated block, we have to wait until
12150 	 * the on-disk directory inode claims the new block.
12151 	 */
12152 	if ((pagedep->pd_state & NEWBLOCK) == 0)
12153 		while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12154 			free_diradd(dap, NULL);
12155 rollforward:
12156 	/*
12157 	 * Uncommitted directory entries must be restored.
12158 	 */
12159 	for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12160 		for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12161 		     dap = nextdap) {
12162 			nextdap = LIST_NEXT(dap, da_pdlist);
12163 			if (dap->da_state & ATTACHED)
12164 				panic("handle_written_filepage: attached");
12165 			ep = (struct direct *)
12166 			    ((char *)bp->b_data + dap->da_offset);
12167 			ep->d_ino = dap->da_newinum;
12168 			dap->da_state &= ~UNDONE;
12169 			dap->da_state |= ATTACHED;
12170 			chgs = 1;
12171 			/*
12172 			 * If the inode referenced by the directory has
12173 			 * been written out, then the dependency can be
12174 			 * moved to the pending list.
12175 			 */
12176 			if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12177 				LIST_REMOVE(dap, da_pdlist);
12178 				LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12179 				    da_pdlist);
12180 			}
12181 		}
12182 	}
12183 	/*
12184 	 * If there were any rollbacks in the directory, then it must be
12185 	 * marked dirty so that its will eventually get written back in
12186 	 * its correct form.
12187 	 */
12188 	if (chgs || (flags & WRITESUCCEEDED) == 0) {
12189 		if ((bp->b_flags & B_DELWRI) == 0)
12190 			stat_dir_entry++;
12191 		bdirty(bp);
12192 		return (1);
12193 	}
12194 	/*
12195 	 * If we are not waiting for a new directory block to be
12196 	 * claimed by its inode, then the pagedep will be freed.
12197 	 * Otherwise it will remain to track any new entries on
12198 	 * the page in case they are fsync'ed.
12199 	 */
12200 	free_pagedep(pagedep);
12201 	return (0);
12202 }
12203 
12204 /*
12205  * Writing back in-core inode structures.
12206  *
12207  * The filesystem only accesses an inode's contents when it occupies an
12208  * "in-core" inode structure.  These "in-core" structures are separate from
12209  * the page frames used to cache inode blocks.  Only the latter are
12210  * transferred to/from the disk.  So, when the updated contents of the
12211  * "in-core" inode structure are copied to the corresponding in-memory inode
12212  * block, the dependencies are also transferred.  The following procedure is
12213  * called when copying a dirty "in-core" inode to a cached inode block.
12214  */
12215 
12216 /*
12217  * Called when an inode is loaded from disk. If the effective link count
12218  * differed from the actual link count when it was last flushed, then we
12219  * need to ensure that the correct effective link count is put back.
12220  */
12221 void
12222 softdep_load_inodeblock(ip)
12223 	struct inode *ip;	/* the "in_core" copy of the inode */
12224 {
12225 	struct inodedep *inodedep;
12226 	struct ufsmount *ump;
12227 
12228 	ump = ITOUMP(ip);
12229 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12230 	    ("softdep_load_inodeblock called on non-softdep filesystem"));
12231 	/*
12232 	 * Check for alternate nlink count.
12233 	 */
12234 	ip->i_effnlink = ip->i_nlink;
12235 	ACQUIRE_LOCK(ump);
12236 	if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12237 		FREE_LOCK(ump);
12238 		return;
12239 	}
12240 	ip->i_effnlink -= inodedep->id_nlinkdelta;
12241 	FREE_LOCK(ump);
12242 }
12243 
12244 /*
12245  * This routine is called just before the "in-core" inode
12246  * information is to be copied to the in-memory inode block.
12247  * Recall that an inode block contains several inodes. If
12248  * the force flag is set, then the dependencies will be
12249  * cleared so that the update can always be made. Note that
12250  * the buffer is locked when this routine is called, so we
12251  * will never be in the middle of writing the inode block
12252  * to disk.
12253  */
12254 void
12255 softdep_update_inodeblock(ip, bp, waitfor)
12256 	struct inode *ip;	/* the "in_core" copy of the inode */
12257 	struct buf *bp;		/* the buffer containing the inode block */
12258 	int waitfor;		/* nonzero => update must be allowed */
12259 {
12260 	struct inodedep *inodedep;
12261 	struct inoref *inoref;
12262 	struct ufsmount *ump;
12263 	struct worklist *wk;
12264 	struct mount *mp;
12265 	struct buf *ibp;
12266 	struct fs *fs;
12267 	int error;
12268 
12269 	ump = ITOUMP(ip);
12270 	mp = UFSTOVFS(ump);
12271 	KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12272 	    ("softdep_update_inodeblock called on non-softdep filesystem"));
12273 	fs = ump->um_fs;
12274 	/*
12275 	 * Preserve the freelink that is on disk.  clear_unlinked_inodedep()
12276 	 * does not have access to the in-core ip so must write directly into
12277 	 * the inode block buffer when setting freelink.
12278 	 */
12279 	if (fs->fs_magic == FS_UFS1_MAGIC)
12280 		DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12281 		    ino_to_fsbo(fs, ip->i_number))->di_freelink);
12282 	else
12283 		DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12284 		    ino_to_fsbo(fs, ip->i_number))->di_freelink);
12285 	/*
12286 	 * If the effective link count is not equal to the actual link
12287 	 * count, then we must track the difference in an inodedep while
12288 	 * the inode is (potentially) tossed out of the cache. Otherwise,
12289 	 * if there is no existing inodedep, then there are no dependencies
12290 	 * to track.
12291 	 */
12292 	ACQUIRE_LOCK(ump);
12293 again:
12294 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12295 		FREE_LOCK(ump);
12296 		if (ip->i_effnlink != ip->i_nlink)
12297 			panic("softdep_update_inodeblock: bad link count");
12298 		return;
12299 	}
12300 	if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12301 		panic("softdep_update_inodeblock: bad delta");
12302 	/*
12303 	 * If we're flushing all dependencies we must also move any waiting
12304 	 * for journal writes onto the bufwait list prior to I/O.
12305 	 */
12306 	if (waitfor) {
12307 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12308 			if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12309 			    == DEPCOMPLETE) {
12310 				jwait(&inoref->if_list, MNT_WAIT);
12311 				goto again;
12312 			}
12313 		}
12314 	}
12315 	/*
12316 	 * Changes have been initiated. Anything depending on these
12317 	 * changes cannot occur until this inode has been written.
12318 	 */
12319 	inodedep->id_state &= ~COMPLETE;
12320 	if ((inodedep->id_state & ONWORKLIST) == 0)
12321 		WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12322 	/*
12323 	 * Any new dependencies associated with the incore inode must
12324 	 * now be moved to the list associated with the buffer holding
12325 	 * the in-memory copy of the inode. Once merged process any
12326 	 * allocdirects that are completed by the merger.
12327 	 */
12328 	merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12329 	if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12330 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12331 		    NULL);
12332 	merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12333 	if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12334 		handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12335 		    NULL);
12336 	/*
12337 	 * Now that the inode has been pushed into the buffer, the
12338 	 * operations dependent on the inode being written to disk
12339 	 * can be moved to the id_bufwait so that they will be
12340 	 * processed when the buffer I/O completes.
12341 	 */
12342 	while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12343 		WORKLIST_REMOVE(wk);
12344 		WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12345 	}
12346 	/*
12347 	 * Newly allocated inodes cannot be written until the bitmap
12348 	 * that allocates them have been written (indicated by
12349 	 * DEPCOMPLETE being set in id_state). If we are doing a
12350 	 * forced sync (e.g., an fsync on a file), we force the bitmap
12351 	 * to be written so that the update can be done.
12352 	 */
12353 	if (waitfor == 0) {
12354 		FREE_LOCK(ump);
12355 		return;
12356 	}
12357 retry:
12358 	if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12359 		FREE_LOCK(ump);
12360 		return;
12361 	}
12362 	ibp = inodedep->id_bmsafemap->sm_buf;
12363 	ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12364 	if (ibp == NULL) {
12365 		/*
12366 		 * If ibp came back as NULL, the dependency could have been
12367 		 * freed while we slept.  Look it up again, and check to see
12368 		 * that it has completed.
12369 		 */
12370 		if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12371 			goto retry;
12372 		FREE_LOCK(ump);
12373 		return;
12374 	}
12375 	FREE_LOCK(ump);
12376 	if ((error = bwrite(ibp)) != 0)
12377 		softdep_error("softdep_update_inodeblock: bwrite", error);
12378 }
12379 
12380 /*
12381  * Merge the a new inode dependency list (such as id_newinoupdt) into an
12382  * old inode dependency list (such as id_inoupdt).
12383  */
12384 static void
12385 merge_inode_lists(newlisthead, oldlisthead)
12386 	struct allocdirectlst *newlisthead;
12387 	struct allocdirectlst *oldlisthead;
12388 {
12389 	struct allocdirect *listadp, *newadp;
12390 
12391 	newadp = TAILQ_FIRST(newlisthead);
12392 	if (newadp != NULL)
12393 		LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12394 	for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12395 		if (listadp->ad_offset < newadp->ad_offset) {
12396 			listadp = TAILQ_NEXT(listadp, ad_next);
12397 			continue;
12398 		}
12399 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
12400 		TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12401 		if (listadp->ad_offset == newadp->ad_offset) {
12402 			allocdirect_merge(oldlisthead, newadp,
12403 			    listadp);
12404 			listadp = newadp;
12405 		}
12406 		newadp = TAILQ_FIRST(newlisthead);
12407 	}
12408 	while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12409 		TAILQ_REMOVE(newlisthead, newadp, ad_next);
12410 		TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12411 	}
12412 }
12413 
12414 /*
12415  * If we are doing an fsync, then we must ensure that any directory
12416  * entries for the inode have been written after the inode gets to disk.
12417  */
12418 int
12419 softdep_fsync(vp)
12420 	struct vnode *vp;	/* the "in_core" copy of the inode */
12421 {
12422 	struct inodedep *inodedep;
12423 	struct pagedep *pagedep;
12424 	struct inoref *inoref;
12425 	struct ufsmount *ump;
12426 	struct worklist *wk;
12427 	struct diradd *dap;
12428 	struct mount *mp;
12429 	struct vnode *pvp;
12430 	struct inode *ip;
12431 	struct buf *bp;
12432 	struct fs *fs;
12433 	struct thread *td = curthread;
12434 	int error, flushparent, pagedep_new_block;
12435 	ino_t parentino;
12436 	ufs_lbn_t lbn;
12437 
12438 	ip = VTOI(vp);
12439 	mp = vp->v_mount;
12440 	ump = VFSTOUFS(mp);
12441 	fs = ump->um_fs;
12442 	if (MOUNTEDSOFTDEP(mp) == 0)
12443 		return (0);
12444 	ACQUIRE_LOCK(ump);
12445 restart:
12446 	if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12447 		FREE_LOCK(ump);
12448 		return (0);
12449 	}
12450 	TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12451 		if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12452 		    == DEPCOMPLETE) {
12453 			jwait(&inoref->if_list, MNT_WAIT);
12454 			goto restart;
12455 		}
12456 	}
12457 	if (!LIST_EMPTY(&inodedep->id_inowait) ||
12458 	    !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12459 	    !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12460 	    !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12461 	    !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12462 		panic("softdep_fsync: pending ops %p", inodedep);
12463 	for (error = 0, flushparent = 0; ; ) {
12464 		if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12465 			break;
12466 		if (wk->wk_type != D_DIRADD)
12467 			panic("softdep_fsync: Unexpected type %s",
12468 			    TYPENAME(wk->wk_type));
12469 		dap = WK_DIRADD(wk);
12470 		/*
12471 		 * Flush our parent if this directory entry has a MKDIR_PARENT
12472 		 * dependency or is contained in a newly allocated block.
12473 		 */
12474 		if (dap->da_state & DIRCHG)
12475 			pagedep = dap->da_previous->dm_pagedep;
12476 		else
12477 			pagedep = dap->da_pagedep;
12478 		parentino = pagedep->pd_ino;
12479 		lbn = pagedep->pd_lbn;
12480 		if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12481 			panic("softdep_fsync: dirty");
12482 		if ((dap->da_state & MKDIR_PARENT) ||
12483 		    (pagedep->pd_state & NEWBLOCK))
12484 			flushparent = 1;
12485 		else
12486 			flushparent = 0;
12487 		/*
12488 		 * If we are being fsync'ed as part of vgone'ing this vnode,
12489 		 * then we will not be able to release and recover the
12490 		 * vnode below, so we just have to give up on writing its
12491 		 * directory entry out. It will eventually be written, just
12492 		 * not now, but then the user was not asking to have it
12493 		 * written, so we are not breaking any promises.
12494 		 */
12495 		if (vp->v_iflag & VI_DOOMED)
12496 			break;
12497 		/*
12498 		 * We prevent deadlock by always fetching inodes from the
12499 		 * root, moving down the directory tree. Thus, when fetching
12500 		 * our parent directory, we first try to get the lock. If
12501 		 * that fails, we must unlock ourselves before requesting
12502 		 * the lock on our parent. See the comment in ufs_lookup
12503 		 * for details on possible races.
12504 		 */
12505 		FREE_LOCK(ump);
12506 		if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
12507 		    FFSV_FORCEINSMQ)) {
12508 			error = vfs_busy(mp, MBF_NOWAIT);
12509 			if (error != 0) {
12510 				vfs_ref(mp);
12511 				VOP_UNLOCK(vp, 0);
12512 				error = vfs_busy(mp, 0);
12513 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12514 				vfs_rel(mp);
12515 				if (error != 0)
12516 					return (ENOENT);
12517 				if (vp->v_iflag & VI_DOOMED) {
12518 					vfs_unbusy(mp);
12519 					return (ENOENT);
12520 				}
12521 			}
12522 			VOP_UNLOCK(vp, 0);
12523 			error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
12524 			    &pvp, FFSV_FORCEINSMQ);
12525 			vfs_unbusy(mp);
12526 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12527 			if (vp->v_iflag & VI_DOOMED) {
12528 				if (error == 0)
12529 					vput(pvp);
12530 				error = ENOENT;
12531 			}
12532 			if (error != 0)
12533 				return (error);
12534 		}
12535 		/*
12536 		 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12537 		 * that are contained in direct blocks will be resolved by
12538 		 * doing a ffs_update. Pagedeps contained in indirect blocks
12539 		 * may require a complete sync'ing of the directory. So, we
12540 		 * try the cheap and fast ffs_update first, and if that fails,
12541 		 * then we do the slower ffs_syncvnode of the directory.
12542 		 */
12543 		if (flushparent) {
12544 			int locked;
12545 
12546 			if ((error = ffs_update(pvp, 1)) != 0) {
12547 				vput(pvp);
12548 				return (error);
12549 			}
12550 			ACQUIRE_LOCK(ump);
12551 			locked = 1;
12552 			if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12553 				if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12554 					if (wk->wk_type != D_DIRADD)
12555 						panic("softdep_fsync: Unexpected type %s",
12556 						      TYPENAME(wk->wk_type));
12557 					dap = WK_DIRADD(wk);
12558 					if (dap->da_state & DIRCHG)
12559 						pagedep = dap->da_previous->dm_pagedep;
12560 					else
12561 						pagedep = dap->da_pagedep;
12562 					pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12563 					FREE_LOCK(ump);
12564 					locked = 0;
12565 					if (pagedep_new_block && (error =
12566 					    ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12567 						vput(pvp);
12568 						return (error);
12569 					}
12570 				}
12571 			}
12572 			if (locked)
12573 				FREE_LOCK(ump);
12574 		}
12575 		/*
12576 		 * Flush directory page containing the inode's name.
12577 		 */
12578 		error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12579 		    &bp);
12580 		if (error == 0)
12581 			error = bwrite(bp);
12582 		else
12583 			brelse(bp);
12584 		vput(pvp);
12585 		if (error != 0)
12586 			return (error);
12587 		ACQUIRE_LOCK(ump);
12588 		if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12589 			break;
12590 	}
12591 	FREE_LOCK(ump);
12592 	return (0);
12593 }
12594 
12595 /*
12596  * Flush all the dirty bitmaps associated with the block device
12597  * before flushing the rest of the dirty blocks so as to reduce
12598  * the number of dependencies that will have to be rolled back.
12599  *
12600  * XXX Unused?
12601  */
12602 void
12603 softdep_fsync_mountdev(vp)
12604 	struct vnode *vp;
12605 {
12606 	struct buf *bp, *nbp;
12607 	struct worklist *wk;
12608 	struct bufobj *bo;
12609 
12610 	if (!vn_isdisk(vp, NULL))
12611 		panic("softdep_fsync_mountdev: vnode not a disk");
12612 	bo = &vp->v_bufobj;
12613 restart:
12614 	BO_LOCK(bo);
12615 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12616 		/*
12617 		 * If it is already scheduled, skip to the next buffer.
12618 		 */
12619 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12620 			continue;
12621 
12622 		if ((bp->b_flags & B_DELWRI) == 0)
12623 			panic("softdep_fsync_mountdev: not dirty");
12624 		/*
12625 		 * We are only interested in bitmaps with outstanding
12626 		 * dependencies.
12627 		 */
12628 		if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12629 		    wk->wk_type != D_BMSAFEMAP ||
12630 		    (bp->b_vflags & BV_BKGRDINPROG)) {
12631 			BUF_UNLOCK(bp);
12632 			continue;
12633 		}
12634 		BO_UNLOCK(bo);
12635 		bremfree(bp);
12636 		(void) bawrite(bp);
12637 		goto restart;
12638 	}
12639 	drain_output(vp);
12640 	BO_UNLOCK(bo);
12641 }
12642 
12643 /*
12644  * Sync all cylinder groups that were dirty at the time this function is
12645  * called.  Newly dirtied cgs will be inserted before the sentinel.  This
12646  * is used to flush freedep activity that may be holding up writes to a
12647  * indirect block.
12648  */
12649 static int
12650 sync_cgs(mp, waitfor)
12651 	struct mount *mp;
12652 	int waitfor;
12653 {
12654 	struct bmsafemap *bmsafemap;
12655 	struct bmsafemap *sentinel;
12656 	struct ufsmount *ump;
12657 	struct buf *bp;
12658 	int error;
12659 
12660 	sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12661 	sentinel->sm_cg = -1;
12662 	ump = VFSTOUFS(mp);
12663 	error = 0;
12664 	ACQUIRE_LOCK(ump);
12665 	LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12666 	for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12667 	    bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12668 		/* Skip sentinels and cgs with no work to release. */
12669 		if (bmsafemap->sm_cg == -1 ||
12670 		    (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12671 		    LIST_EMPTY(&bmsafemap->sm_freewr))) {
12672 			LIST_REMOVE(sentinel, sm_next);
12673 			LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12674 			continue;
12675 		}
12676 		/*
12677 		 * If we don't get the lock and we're waiting try again, if
12678 		 * not move on to the next buf and try to sync it.
12679 		 */
12680 		bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12681 		if (bp == NULL && waitfor == MNT_WAIT)
12682 			continue;
12683 		LIST_REMOVE(sentinel, sm_next);
12684 		LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12685 		if (bp == NULL)
12686 			continue;
12687 		FREE_LOCK(ump);
12688 		if (waitfor == MNT_NOWAIT)
12689 			bawrite(bp);
12690 		else
12691 			error = bwrite(bp);
12692 		ACQUIRE_LOCK(ump);
12693 		if (error)
12694 			break;
12695 	}
12696 	LIST_REMOVE(sentinel, sm_next);
12697 	FREE_LOCK(ump);
12698 	free(sentinel, M_BMSAFEMAP);
12699 	return (error);
12700 }
12701 
12702 /*
12703  * This routine is called when we are trying to synchronously flush a
12704  * file. This routine must eliminate any filesystem metadata dependencies
12705  * so that the syncing routine can succeed.
12706  */
12707 int
12708 softdep_sync_metadata(struct vnode *vp)
12709 {
12710 	struct inode *ip;
12711 	int error;
12712 
12713 	ip = VTOI(vp);
12714 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12715 	    ("softdep_sync_metadata called on non-softdep filesystem"));
12716 	/*
12717 	 * Ensure that any direct block dependencies have been cleared,
12718 	 * truncations are started, and inode references are journaled.
12719 	 */
12720 	ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
12721 	/*
12722 	 * Write all journal records to prevent rollbacks on devvp.
12723 	 */
12724 	if (vp->v_type == VCHR)
12725 		softdep_flushjournal(vp->v_mount);
12726 	error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12727 	/*
12728 	 * Ensure that all truncates are written so we won't find deps on
12729 	 * indirect blocks.
12730 	 */
12731 	process_truncates(vp);
12732 	FREE_LOCK(VFSTOUFS(vp->v_mount));
12733 
12734 	return (error);
12735 }
12736 
12737 /*
12738  * This routine is called when we are attempting to sync a buf with
12739  * dependencies.  If waitfor is MNT_NOWAIT it attempts to schedule any
12740  * other IO it can but returns EBUSY if the buffer is not yet able to
12741  * be written.  Dependencies which will not cause rollbacks will always
12742  * return 0.
12743  */
12744 int
12745 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
12746 {
12747 	struct indirdep *indirdep;
12748 	struct pagedep *pagedep;
12749 	struct allocindir *aip;
12750 	struct newblk *newblk;
12751 	struct ufsmount *ump;
12752 	struct buf *nbp;
12753 	struct worklist *wk;
12754 	int i, error;
12755 
12756 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12757 	    ("softdep_sync_buf called on non-softdep filesystem"));
12758 	/*
12759 	 * For VCHR we just don't want to force flush any dependencies that
12760 	 * will cause rollbacks.
12761 	 */
12762 	if (vp->v_type == VCHR) {
12763 		if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12764 			return (EBUSY);
12765 		return (0);
12766 	}
12767 	ump = VFSTOUFS(vp->v_mount);
12768 	ACQUIRE_LOCK(ump);
12769 	/*
12770 	 * As we hold the buffer locked, none of its dependencies
12771 	 * will disappear.
12772 	 */
12773 	error = 0;
12774 top:
12775 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12776 		switch (wk->wk_type) {
12777 
12778 		case D_ALLOCDIRECT:
12779 		case D_ALLOCINDIR:
12780 			newblk = WK_NEWBLK(wk);
12781 			if (newblk->nb_jnewblk != NULL) {
12782 				if (waitfor == MNT_NOWAIT) {
12783 					error = EBUSY;
12784 					goto out_unlock;
12785 				}
12786 				jwait(&newblk->nb_jnewblk->jn_list, waitfor);
12787 				goto top;
12788 			}
12789 			if (newblk->nb_state & DEPCOMPLETE ||
12790 			    waitfor == MNT_NOWAIT)
12791 				continue;
12792 			nbp = newblk->nb_bmsafemap->sm_buf;
12793 			nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12794 			if (nbp == NULL)
12795 				goto top;
12796 			FREE_LOCK(ump);
12797 			if ((error = bwrite(nbp)) != 0)
12798 				goto out;
12799 			ACQUIRE_LOCK(ump);
12800 			continue;
12801 
12802 		case D_INDIRDEP:
12803 			indirdep = WK_INDIRDEP(wk);
12804 			if (waitfor == MNT_NOWAIT) {
12805 				if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
12806 				    !LIST_EMPTY(&indirdep->ir_deplisthd)) {
12807 					error = EBUSY;
12808 					goto out_unlock;
12809 				}
12810 			}
12811 			if (!TAILQ_EMPTY(&indirdep->ir_trunc))
12812 				panic("softdep_sync_buf: truncation pending.");
12813 		restart:
12814 			LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
12815 				newblk = (struct newblk *)aip;
12816 				if (newblk->nb_jnewblk != NULL) {
12817 					jwait(&newblk->nb_jnewblk->jn_list,
12818 					    waitfor);
12819 					goto restart;
12820 				}
12821 				if (newblk->nb_state & DEPCOMPLETE)
12822 					continue;
12823 				nbp = newblk->nb_bmsafemap->sm_buf;
12824 				nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12825 				if (nbp == NULL)
12826 					goto restart;
12827 				FREE_LOCK(ump);
12828 				if ((error = bwrite(nbp)) != 0)
12829 					goto out;
12830 				ACQUIRE_LOCK(ump);
12831 				goto restart;
12832 			}
12833 			continue;
12834 
12835 		case D_PAGEDEP:
12836 			/*
12837 			 * Only flush directory entries in synchronous passes.
12838 			 */
12839 			if (waitfor != MNT_WAIT) {
12840 				error = EBUSY;
12841 				goto out_unlock;
12842 			}
12843 			/*
12844 			 * While syncing snapshots, we must allow recursive
12845 			 * lookups.
12846 			 */
12847 			BUF_AREC(bp);
12848 			/*
12849 			 * We are trying to sync a directory that may
12850 			 * have dependencies on both its own metadata
12851 			 * and/or dependencies on the inodes of any
12852 			 * recently allocated files. We walk its diradd
12853 			 * lists pushing out the associated inode.
12854 			 */
12855 			pagedep = WK_PAGEDEP(wk);
12856 			for (i = 0; i < DAHASHSZ; i++) {
12857 				if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
12858 					continue;
12859 				if ((error = flush_pagedep_deps(vp, wk->wk_mp,
12860 				    &pagedep->pd_diraddhd[i]))) {
12861 					BUF_NOREC(bp);
12862 					goto out_unlock;
12863 				}
12864 			}
12865 			BUF_NOREC(bp);
12866 			continue;
12867 
12868 		case D_FREEWORK:
12869 		case D_FREEDEP:
12870 		case D_JSEGDEP:
12871 		case D_JNEWBLK:
12872 			continue;
12873 
12874 		default:
12875 			panic("softdep_sync_buf: Unknown type %s",
12876 			    TYPENAME(wk->wk_type));
12877 			/* NOTREACHED */
12878 		}
12879 	}
12880 out_unlock:
12881 	FREE_LOCK(ump);
12882 out:
12883 	return (error);
12884 }
12885 
12886 /*
12887  * Flush the dependencies associated with an inodedep.
12888  */
12889 static int
12890 flush_inodedep_deps(vp, mp, ino)
12891 	struct vnode *vp;
12892 	struct mount *mp;
12893 	ino_t ino;
12894 {
12895 	struct inodedep *inodedep;
12896 	struct inoref *inoref;
12897 	struct ufsmount *ump;
12898 	int error, waitfor;
12899 
12900 	/*
12901 	 * This work is done in two passes. The first pass grabs most
12902 	 * of the buffers and begins asynchronously writing them. The
12903 	 * only way to wait for these asynchronous writes is to sleep
12904 	 * on the filesystem vnode which may stay busy for a long time
12905 	 * if the filesystem is active. So, instead, we make a second
12906 	 * pass over the dependencies blocking on each write. In the
12907 	 * usual case we will be blocking against a write that we
12908 	 * initiated, so when it is done the dependency will have been
12909 	 * resolved. Thus the second pass is expected to end quickly.
12910 	 * We give a brief window at the top of the loop to allow
12911 	 * any pending I/O to complete.
12912 	 */
12913 	ump = VFSTOUFS(mp);
12914 	LOCK_OWNED(ump);
12915 	for (error = 0, waitfor = MNT_NOWAIT; ; ) {
12916 		if (error)
12917 			return (error);
12918 		FREE_LOCK(ump);
12919 		ACQUIRE_LOCK(ump);
12920 restart:
12921 		if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
12922 			return (0);
12923 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12924 			if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12925 			    == DEPCOMPLETE) {
12926 				jwait(&inoref->if_list, MNT_WAIT);
12927 				goto restart;
12928 			}
12929 		}
12930 		if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
12931 		    flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
12932 		    flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
12933 		    flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
12934 			continue;
12935 		/*
12936 		 * If pass2, we are done, otherwise do pass 2.
12937 		 */
12938 		if (waitfor == MNT_WAIT)
12939 			break;
12940 		waitfor = MNT_WAIT;
12941 	}
12942 	/*
12943 	 * Try freeing inodedep in case all dependencies have been removed.
12944 	 */
12945 	if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
12946 		(void) free_inodedep(inodedep);
12947 	return (0);
12948 }
12949 
12950 /*
12951  * Flush an inode dependency list.
12952  */
12953 static int
12954 flush_deplist(listhead, waitfor, errorp)
12955 	struct allocdirectlst *listhead;
12956 	int waitfor;
12957 	int *errorp;
12958 {
12959 	struct allocdirect *adp;
12960 	struct newblk *newblk;
12961 	struct ufsmount *ump;
12962 	struct buf *bp;
12963 
12964 	if ((adp = TAILQ_FIRST(listhead)) == NULL)
12965 		return (0);
12966 	ump = VFSTOUFS(adp->ad_list.wk_mp);
12967 	LOCK_OWNED(ump);
12968 	TAILQ_FOREACH(adp, listhead, ad_next) {
12969 		newblk = (struct newblk *)adp;
12970 		if (newblk->nb_jnewblk != NULL) {
12971 			jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
12972 			return (1);
12973 		}
12974 		if (newblk->nb_state & DEPCOMPLETE)
12975 			continue;
12976 		bp = newblk->nb_bmsafemap->sm_buf;
12977 		bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
12978 		if (bp == NULL) {
12979 			if (waitfor == MNT_NOWAIT)
12980 				continue;
12981 			return (1);
12982 		}
12983 		FREE_LOCK(ump);
12984 		if (waitfor == MNT_NOWAIT)
12985 			bawrite(bp);
12986 		else
12987 			*errorp = bwrite(bp);
12988 		ACQUIRE_LOCK(ump);
12989 		return (1);
12990 	}
12991 	return (0);
12992 }
12993 
12994 /*
12995  * Flush dependencies associated with an allocdirect block.
12996  */
12997 static int
12998 flush_newblk_dep(vp, mp, lbn)
12999 	struct vnode *vp;
13000 	struct mount *mp;
13001 	ufs_lbn_t lbn;
13002 {
13003 	struct newblk *newblk;
13004 	struct ufsmount *ump;
13005 	struct bufobj *bo;
13006 	struct inode *ip;
13007 	struct buf *bp;
13008 	ufs2_daddr_t blkno;
13009 	int error;
13010 
13011 	error = 0;
13012 	bo = &vp->v_bufobj;
13013 	ip = VTOI(vp);
13014 	blkno = DIP(ip, i_db[lbn]);
13015 	if (blkno == 0)
13016 		panic("flush_newblk_dep: Missing block");
13017 	ump = VFSTOUFS(mp);
13018 	ACQUIRE_LOCK(ump);
13019 	/*
13020 	 * Loop until all dependencies related to this block are satisfied.
13021 	 * We must be careful to restart after each sleep in case a write
13022 	 * completes some part of this process for us.
13023 	 */
13024 	for (;;) {
13025 		if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13026 			FREE_LOCK(ump);
13027 			break;
13028 		}
13029 		if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13030 			panic("flush_newblk_dep: Bad newblk %p", newblk);
13031 		/*
13032 		 * Flush the journal.
13033 		 */
13034 		if (newblk->nb_jnewblk != NULL) {
13035 			jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13036 			continue;
13037 		}
13038 		/*
13039 		 * Write the bitmap dependency.
13040 		 */
13041 		if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13042 			bp = newblk->nb_bmsafemap->sm_buf;
13043 			bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13044 			if (bp == NULL)
13045 				continue;
13046 			FREE_LOCK(ump);
13047 			error = bwrite(bp);
13048 			if (error)
13049 				break;
13050 			ACQUIRE_LOCK(ump);
13051 			continue;
13052 		}
13053 		/*
13054 		 * Write the buffer.
13055 		 */
13056 		FREE_LOCK(ump);
13057 		BO_LOCK(bo);
13058 		bp = gbincore(bo, lbn);
13059 		if (bp != NULL) {
13060 			error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13061 			    LK_INTERLOCK, BO_LOCKPTR(bo));
13062 			if (error == ENOLCK) {
13063 				ACQUIRE_LOCK(ump);
13064 				error = 0;
13065 				continue; /* Slept, retry */
13066 			}
13067 			if (error != 0)
13068 				break;	/* Failed */
13069 			if (bp->b_flags & B_DELWRI) {
13070 				bremfree(bp);
13071 				error = bwrite(bp);
13072 				if (error)
13073 					break;
13074 			} else
13075 				BUF_UNLOCK(bp);
13076 		} else
13077 			BO_UNLOCK(bo);
13078 		/*
13079 		 * We have to wait for the direct pointers to
13080 		 * point at the newdirblk before the dependency
13081 		 * will go away.
13082 		 */
13083 		error = ffs_update(vp, 1);
13084 		if (error)
13085 			break;
13086 		ACQUIRE_LOCK(ump);
13087 	}
13088 	return (error);
13089 }
13090 
13091 /*
13092  * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13093  */
13094 static int
13095 flush_pagedep_deps(pvp, mp, diraddhdp)
13096 	struct vnode *pvp;
13097 	struct mount *mp;
13098 	struct diraddhd *diraddhdp;
13099 {
13100 	struct inodedep *inodedep;
13101 	struct inoref *inoref;
13102 	struct ufsmount *ump;
13103 	struct diradd *dap;
13104 	struct vnode *vp;
13105 	int error = 0;
13106 	struct buf *bp;
13107 	ino_t inum;
13108 	struct diraddhd unfinished;
13109 
13110 	LIST_INIT(&unfinished);
13111 	ump = VFSTOUFS(mp);
13112 	LOCK_OWNED(ump);
13113 restart:
13114 	while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13115 		/*
13116 		 * Flush ourselves if this directory entry
13117 		 * has a MKDIR_PARENT dependency.
13118 		 */
13119 		if (dap->da_state & MKDIR_PARENT) {
13120 			FREE_LOCK(ump);
13121 			if ((error = ffs_update(pvp, 1)) != 0)
13122 				break;
13123 			ACQUIRE_LOCK(ump);
13124 			/*
13125 			 * If that cleared dependencies, go on to next.
13126 			 */
13127 			if (dap != LIST_FIRST(diraddhdp))
13128 				continue;
13129 			/*
13130 			 * All MKDIR_PARENT dependencies and all the
13131 			 * NEWBLOCK pagedeps that are contained in direct
13132 			 * blocks were resolved by doing above ffs_update.
13133 			 * Pagedeps contained in indirect blocks may
13134 			 * require a complete sync'ing of the directory.
13135 			 * We are in the midst of doing a complete sync,
13136 			 * so if they are not resolved in this pass we
13137 			 * defer them for now as they will be sync'ed by
13138 			 * our caller shortly.
13139 			 */
13140 			LIST_REMOVE(dap, da_pdlist);
13141 			LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13142 			continue;
13143 		}
13144 		/*
13145 		 * A newly allocated directory must have its "." and
13146 		 * ".." entries written out before its name can be
13147 		 * committed in its parent.
13148 		 */
13149 		inum = dap->da_newinum;
13150 		if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13151 			panic("flush_pagedep_deps: lost inode1");
13152 		/*
13153 		 * Wait for any pending journal adds to complete so we don't
13154 		 * cause rollbacks while syncing.
13155 		 */
13156 		TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13157 			if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13158 			    == DEPCOMPLETE) {
13159 				jwait(&inoref->if_list, MNT_WAIT);
13160 				goto restart;
13161 			}
13162 		}
13163 		if (dap->da_state & MKDIR_BODY) {
13164 			FREE_LOCK(ump);
13165 			if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13166 			    FFSV_FORCEINSMQ)))
13167 				break;
13168 			error = flush_newblk_dep(vp, mp, 0);
13169 			/*
13170 			 * If we still have the dependency we might need to
13171 			 * update the vnode to sync the new link count to
13172 			 * disk.
13173 			 */
13174 			if (error == 0 && dap == LIST_FIRST(diraddhdp))
13175 				error = ffs_update(vp, 1);
13176 			vput(vp);
13177 			if (error != 0)
13178 				break;
13179 			ACQUIRE_LOCK(ump);
13180 			/*
13181 			 * If that cleared dependencies, go on to next.
13182 			 */
13183 			if (dap != LIST_FIRST(diraddhdp))
13184 				continue;
13185 			if (dap->da_state & MKDIR_BODY) {
13186 				inodedep_lookup(UFSTOVFS(ump), inum, 0,
13187 				    &inodedep);
13188 				panic("flush_pagedep_deps: MKDIR_BODY "
13189 				    "inodedep %p dap %p vp %p",
13190 				    inodedep, dap, vp);
13191 			}
13192 		}
13193 		/*
13194 		 * Flush the inode on which the directory entry depends.
13195 		 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13196 		 * the only remaining dependency is that the updated inode
13197 		 * count must get pushed to disk. The inode has already
13198 		 * been pushed into its inode buffer (via VOP_UPDATE) at
13199 		 * the time of the reference count change. So we need only
13200 		 * locate that buffer, ensure that there will be no rollback
13201 		 * caused by a bitmap dependency, then write the inode buffer.
13202 		 */
13203 retry:
13204 		if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13205 			panic("flush_pagedep_deps: lost inode");
13206 		/*
13207 		 * If the inode still has bitmap dependencies,
13208 		 * push them to disk.
13209 		 */
13210 		if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13211 			bp = inodedep->id_bmsafemap->sm_buf;
13212 			bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13213 			if (bp == NULL)
13214 				goto retry;
13215 			FREE_LOCK(ump);
13216 			if ((error = bwrite(bp)) != 0)
13217 				break;
13218 			ACQUIRE_LOCK(ump);
13219 			if (dap != LIST_FIRST(diraddhdp))
13220 				continue;
13221 		}
13222 		/*
13223 		 * If the inode is still sitting in a buffer waiting
13224 		 * to be written or waiting for the link count to be
13225 		 * adjusted update it here to flush it to disk.
13226 		 */
13227 		if (dap == LIST_FIRST(diraddhdp)) {
13228 			FREE_LOCK(ump);
13229 			if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13230 			    FFSV_FORCEINSMQ)))
13231 				break;
13232 			error = ffs_update(vp, 1);
13233 			vput(vp);
13234 			if (error)
13235 				break;
13236 			ACQUIRE_LOCK(ump);
13237 		}
13238 		/*
13239 		 * If we have failed to get rid of all the dependencies
13240 		 * then something is seriously wrong.
13241 		 */
13242 		if (dap == LIST_FIRST(diraddhdp)) {
13243 			inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13244 			panic("flush_pagedep_deps: failed to flush "
13245 			    "inodedep %p ino %ju dap %p",
13246 			    inodedep, (uintmax_t)inum, dap);
13247 		}
13248 	}
13249 	if (error)
13250 		ACQUIRE_LOCK(ump);
13251 	while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13252 		LIST_REMOVE(dap, da_pdlist);
13253 		LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13254 	}
13255 	return (error);
13256 }
13257 
13258 /*
13259  * A large burst of file addition or deletion activity can drive the
13260  * memory load excessively high. First attempt to slow things down
13261  * using the techniques below. If that fails, this routine requests
13262  * the offending operations to fall back to running synchronously
13263  * until the memory load returns to a reasonable level.
13264  */
13265 int
13266 softdep_slowdown(vp)
13267 	struct vnode *vp;
13268 {
13269 	struct ufsmount *ump;
13270 	int jlow;
13271 	int max_softdeps_hard;
13272 
13273 	KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13274 	    ("softdep_slowdown called on non-softdep filesystem"));
13275 	ump = VFSTOUFS(vp->v_mount);
13276 	ACQUIRE_LOCK(ump);
13277 	jlow = 0;
13278 	/*
13279 	 * Check for journal space if needed.
13280 	 */
13281 	if (DOINGSUJ(vp)) {
13282 		if (journal_space(ump, 0) == 0)
13283 			jlow = 1;
13284 	}
13285 	/*
13286 	 * If the system is under its limits and our filesystem is
13287 	 * not responsible for more than our share of the usage and
13288 	 * we are not low on journal space, then no need to slow down.
13289 	 */
13290 	max_softdeps_hard = max_softdeps * 11 / 10;
13291 	if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13292 	    dep_current[D_INODEDEP] < max_softdeps_hard &&
13293 	    dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13294 	    dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13295 	    ump->softdep_curdeps[D_DIRREM] <
13296 	    (max_softdeps_hard / 2) / stat_flush_threads &&
13297 	    ump->softdep_curdeps[D_INODEDEP] <
13298 	    max_softdeps_hard / stat_flush_threads &&
13299 	    ump->softdep_curdeps[D_INDIRDEP] <
13300 	    (max_softdeps_hard / 1000) / stat_flush_threads &&
13301 	    ump->softdep_curdeps[D_FREEBLKS] <
13302 	    max_softdeps_hard / stat_flush_threads) {
13303 		FREE_LOCK(ump);
13304   		return (0);
13305 	}
13306 	/*
13307 	 * If the journal is low or our filesystem is over its limit
13308 	 * then speedup the cleanup.
13309 	 */
13310 	if (ump->softdep_curdeps[D_INDIRDEP] <
13311 	    (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13312 		softdep_speedup(ump);
13313 	stat_sync_limit_hit += 1;
13314 	FREE_LOCK(ump);
13315 	/*
13316 	 * We only slow down the rate at which new dependencies are
13317 	 * generated if we are not using journaling. With journaling,
13318 	 * the cleanup should always be sufficient to keep things
13319 	 * under control.
13320 	 */
13321 	if (DOINGSUJ(vp))
13322 		return (0);
13323 	return (1);
13324 }
13325 
13326 /*
13327  * Called by the allocation routines when they are about to fail
13328  * in the hope that we can free up the requested resource (inodes
13329  * or disk space).
13330  *
13331  * First check to see if the work list has anything on it. If it has,
13332  * clean up entries until we successfully free the requested resource.
13333  * Because this process holds inodes locked, we cannot handle any remove
13334  * requests that might block on a locked inode as that could lead to
13335  * deadlock. If the worklist yields none of the requested resource,
13336  * start syncing out vnodes to free up the needed space.
13337  */
13338 int
13339 softdep_request_cleanup(fs, vp, cred, resource)
13340 	struct fs *fs;
13341 	struct vnode *vp;
13342 	struct ucred *cred;
13343 	int resource;
13344 {
13345 	struct ufsmount *ump;
13346 	struct mount *mp;
13347 	long starttime;
13348 	ufs2_daddr_t needed;
13349 	int error, failed_vnode;
13350 
13351 	/*
13352 	 * If we are being called because of a process doing a
13353 	 * copy-on-write, then it is not safe to process any
13354 	 * worklist items as we will recurse into the copyonwrite
13355 	 * routine.  This will result in an incoherent snapshot.
13356 	 * If the vnode that we hold is a snapshot, we must avoid
13357 	 * handling other resources that could cause deadlock.
13358 	 */
13359 	if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13360 		return (0);
13361 
13362 	if (resource == FLUSH_BLOCKS_WAIT)
13363 		stat_cleanup_blkrequests += 1;
13364 	else
13365 		stat_cleanup_inorequests += 1;
13366 
13367 	mp = vp->v_mount;
13368 	ump = VFSTOUFS(mp);
13369 	mtx_assert(UFS_MTX(ump), MA_OWNED);
13370 	UFS_UNLOCK(ump);
13371 	error = ffs_update(vp, 1);
13372 	if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13373 		UFS_LOCK(ump);
13374 		return (0);
13375 	}
13376 	/*
13377 	 * If we are in need of resources, start by cleaning up
13378 	 * any block removals associated with our inode.
13379 	 */
13380 	ACQUIRE_LOCK(ump);
13381 	process_removes(vp);
13382 	process_truncates(vp);
13383 	FREE_LOCK(ump);
13384 	/*
13385 	 * Now clean up at least as many resources as we will need.
13386 	 *
13387 	 * When requested to clean up inodes, the number that are needed
13388 	 * is set by the number of simultaneous writers (mnt_writeopcount)
13389 	 * plus a bit of slop (2) in case some more writers show up while
13390 	 * we are cleaning.
13391 	 *
13392 	 * When requested to free up space, the amount of space that
13393 	 * we need is enough blocks to allocate a full-sized segment
13394 	 * (fs_contigsumsize). The number of such segments that will
13395 	 * be needed is set by the number of simultaneous writers
13396 	 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13397 	 * writers show up while we are cleaning.
13398 	 *
13399 	 * Additionally, if we are unpriviledged and allocating space,
13400 	 * we need to ensure that we clean up enough blocks to get the
13401 	 * needed number of blocks over the threshold of the minimum
13402 	 * number of blocks required to be kept free by the filesystem
13403 	 * (fs_minfree).
13404 	 */
13405 	if (resource == FLUSH_INODES_WAIT) {
13406 		needed = vp->v_mount->mnt_writeopcount + 2;
13407 	} else if (resource == FLUSH_BLOCKS_WAIT) {
13408 		needed = (vp->v_mount->mnt_writeopcount + 2) *
13409 		    fs->fs_contigsumsize;
13410 		if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13411 			needed += fragstoblks(fs,
13412 			    roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13413 			    fs->fs_cstotal.cs_nffree, fs->fs_frag));
13414 	} else {
13415 		UFS_LOCK(ump);
13416 		printf("softdep_request_cleanup: Unknown resource type %d\n",
13417 		    resource);
13418 		return (0);
13419 	}
13420 	starttime = time_second;
13421 retry:
13422 	if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13423 	    fs->fs_cstotal.cs_nbfree <= needed) ||
13424 	    (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13425 	    fs->fs_cstotal.cs_nifree <= needed)) {
13426 		ACQUIRE_LOCK(ump);
13427 		if (ump->softdep_on_worklist > 0 &&
13428 		    process_worklist_item(UFSTOVFS(ump),
13429 		    ump->softdep_on_worklist, LK_NOWAIT) != 0)
13430 			stat_worklist_push += 1;
13431 		FREE_LOCK(ump);
13432 	}
13433 	/*
13434 	 * If we still need resources and there are no more worklist
13435 	 * entries to process to obtain them, we have to start flushing
13436 	 * the dirty vnodes to force the release of additional requests
13437 	 * to the worklist that we can then process to reap addition
13438 	 * resources. We walk the vnodes associated with the mount point
13439 	 * until we get the needed worklist requests that we can reap.
13440 	 *
13441 	 * If there are several threads all needing to clean the same
13442 	 * mount point, only one is allowed to walk the mount list.
13443 	 * When several threads all try to walk the same mount list,
13444 	 * they end up competing with each other and often end up in
13445 	 * livelock. This approach ensures that forward progress is
13446 	 * made at the cost of occational ENOSPC errors being returned
13447 	 * that might otherwise have been avoided.
13448 	 */
13449 	error = 1;
13450 	if ((resource == FLUSH_BLOCKS_WAIT &&
13451 	     fs->fs_cstotal.cs_nbfree <= needed) ||
13452 	    (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13453 	     fs->fs_cstotal.cs_nifree <= needed)) {
13454 		ACQUIRE_LOCK(ump);
13455 		if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13456 			ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13457 			FREE_LOCK(ump);
13458 			failed_vnode = softdep_request_cleanup_flush(mp, ump);
13459 			ACQUIRE_LOCK(ump);
13460 			ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13461 			FREE_LOCK(ump);
13462 			if (ump->softdep_on_worklist > 0) {
13463 				stat_cleanup_retries += 1;
13464 				if (!failed_vnode)
13465 					goto retry;
13466 			}
13467 		} else {
13468 			FREE_LOCK(ump);
13469 			error = 0;
13470 		}
13471 		stat_cleanup_failures += 1;
13472 	}
13473 	if (time_second - starttime > stat_cleanup_high_delay)
13474 		stat_cleanup_high_delay = time_second - starttime;
13475 	UFS_LOCK(ump);
13476 	return (error);
13477 }
13478 
13479 /*
13480  * Scan the vnodes for the specified mount point flushing out any
13481  * vnodes that can be locked without waiting. Finally, try to flush
13482  * the device associated with the mount point if it can be locked
13483  * without waiting.
13484  *
13485  * We return 0 if we were able to lock every vnode in our scan.
13486  * If we had to skip one or more vnodes, we return 1.
13487  */
13488 static int
13489 softdep_request_cleanup_flush(mp, ump)
13490 	struct mount *mp;
13491 	struct ufsmount *ump;
13492 {
13493 	struct thread *td;
13494 	struct vnode *lvp, *mvp;
13495 	int failed_vnode;
13496 
13497 	failed_vnode = 0;
13498 	td = curthread;
13499 	MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13500 		if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13501 			VI_UNLOCK(lvp);
13502 			continue;
13503 		}
13504 		if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT,
13505 		    td) != 0) {
13506 			failed_vnode = 1;
13507 			continue;
13508 		}
13509 		if (lvp->v_vflag & VV_NOSYNC) {	/* unlinked */
13510 			vput(lvp);
13511 			continue;
13512 		}
13513 		(void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13514 		vput(lvp);
13515 	}
13516 	lvp = ump->um_devvp;
13517 	if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13518 		VOP_FSYNC(lvp, MNT_NOWAIT, td);
13519 		VOP_UNLOCK(lvp, 0);
13520 	}
13521 	return (failed_vnode);
13522 }
13523 
13524 static bool
13525 softdep_excess_items(struct ufsmount *ump, int item)
13526 {
13527 
13528 	KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13529 	return (dep_current[item] > max_softdeps &&
13530 	    ump->softdep_curdeps[item] > max_softdeps /
13531 	    stat_flush_threads);
13532 }
13533 
13534 static void
13535 schedule_cleanup(struct mount *mp)
13536 {
13537 	struct ufsmount *ump;
13538 	struct thread *td;
13539 
13540 	ump = VFSTOUFS(mp);
13541 	LOCK_OWNED(ump);
13542 	FREE_LOCK(ump);
13543 	td = curthread;
13544 	if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13545 	    (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13546 		/*
13547 		 * No ast is delivered to kernel threads, so nobody
13548 		 * would deref the mp.  Some kernel threads
13549 		 * explicitely check for AST, e.g. NFS daemon does
13550 		 * this in the serving loop.
13551 		 */
13552 		return;
13553 	}
13554 	if (td->td_su != NULL)
13555 		vfs_rel(td->td_su);
13556 	vfs_ref(mp);
13557 	td->td_su = mp;
13558 	thread_lock(td);
13559 	td->td_flags |= TDF_ASTPENDING;
13560 	thread_unlock(td);
13561 }
13562 
13563 static void
13564 softdep_ast_cleanup_proc(struct thread *td)
13565 {
13566 	struct mount *mp;
13567 	struct ufsmount *ump;
13568 	int error;
13569 	bool req;
13570 
13571 	while ((mp = td->td_su) != NULL) {
13572 		td->td_su = NULL;
13573 		error = vfs_busy(mp, MBF_NOWAIT);
13574 		vfs_rel(mp);
13575 		if (error != 0)
13576 			return;
13577 		if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13578 			ump = VFSTOUFS(mp);
13579 			for (;;) {
13580 				req = false;
13581 				ACQUIRE_LOCK(ump);
13582 				if (softdep_excess_items(ump, D_INODEDEP)) {
13583 					req = true;
13584 					request_cleanup(mp, FLUSH_INODES);
13585 				}
13586 				if (softdep_excess_items(ump, D_DIRREM)) {
13587 					req = true;
13588 					request_cleanup(mp, FLUSH_BLOCKS);
13589 				}
13590 				FREE_LOCK(ump);
13591 				if (softdep_excess_items(ump, D_NEWBLK) ||
13592 				    softdep_excess_items(ump, D_ALLOCDIRECT) ||
13593 				    softdep_excess_items(ump, D_ALLOCINDIR)) {
13594 					error = vn_start_write(NULL, &mp,
13595 					    V_WAIT);
13596 					if (error == 0) {
13597 						req = true;
13598 						VFS_SYNC(mp, MNT_WAIT);
13599 						vn_finished_write(mp);
13600 					}
13601 				}
13602 				if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13603 					break;
13604 			}
13605 		}
13606 		vfs_unbusy(mp);
13607 	}
13608 	if ((mp = td->td_su) != NULL) {
13609 		td->td_su = NULL;
13610 		vfs_rel(mp);
13611 	}
13612 }
13613 
13614 /*
13615  * If memory utilization has gotten too high, deliberately slow things
13616  * down and speed up the I/O processing.
13617  */
13618 static int
13619 request_cleanup(mp, resource)
13620 	struct mount *mp;
13621 	int resource;
13622 {
13623 	struct thread *td = curthread;
13624 	struct ufsmount *ump;
13625 
13626 	ump = VFSTOUFS(mp);
13627 	LOCK_OWNED(ump);
13628 	/*
13629 	 * We never hold up the filesystem syncer or buf daemon.
13630 	 */
13631 	if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13632 		return (0);
13633 	/*
13634 	 * First check to see if the work list has gotten backlogged.
13635 	 * If it has, co-opt this process to help clean up two entries.
13636 	 * Because this process may hold inodes locked, we cannot
13637 	 * handle any remove requests that might block on a locked
13638 	 * inode as that could lead to deadlock.  We set TDP_SOFTDEP
13639 	 * to avoid recursively processing the worklist.
13640 	 */
13641 	if (ump->softdep_on_worklist > max_softdeps / 10) {
13642 		td->td_pflags |= TDP_SOFTDEP;
13643 		process_worklist_item(mp, 2, LK_NOWAIT);
13644 		td->td_pflags &= ~TDP_SOFTDEP;
13645 		stat_worklist_push += 2;
13646 		return(1);
13647 	}
13648 	/*
13649 	 * Next, we attempt to speed up the syncer process. If that
13650 	 * is successful, then we allow the process to continue.
13651 	 */
13652 	if (softdep_speedup(ump) &&
13653 	    resource != FLUSH_BLOCKS_WAIT &&
13654 	    resource != FLUSH_INODES_WAIT)
13655 		return(0);
13656 	/*
13657 	 * If we are resource constrained on inode dependencies, try
13658 	 * flushing some dirty inodes. Otherwise, we are constrained
13659 	 * by file deletions, so try accelerating flushes of directories
13660 	 * with removal dependencies. We would like to do the cleanup
13661 	 * here, but we probably hold an inode locked at this point and
13662 	 * that might deadlock against one that we try to clean. So,
13663 	 * the best that we can do is request the syncer daemon to do
13664 	 * the cleanup for us.
13665 	 */
13666 	switch (resource) {
13667 
13668 	case FLUSH_INODES:
13669 	case FLUSH_INODES_WAIT:
13670 		ACQUIRE_GBLLOCK(&lk);
13671 		stat_ino_limit_push += 1;
13672 		req_clear_inodedeps += 1;
13673 		FREE_GBLLOCK(&lk);
13674 		stat_countp = &stat_ino_limit_hit;
13675 		break;
13676 
13677 	case FLUSH_BLOCKS:
13678 	case FLUSH_BLOCKS_WAIT:
13679 		ACQUIRE_GBLLOCK(&lk);
13680 		stat_blk_limit_push += 1;
13681 		req_clear_remove += 1;
13682 		FREE_GBLLOCK(&lk);
13683 		stat_countp = &stat_blk_limit_hit;
13684 		break;
13685 
13686 	default:
13687 		panic("request_cleanup: unknown type");
13688 	}
13689 	/*
13690 	 * Hopefully the syncer daemon will catch up and awaken us.
13691 	 * We wait at most tickdelay before proceeding in any case.
13692 	 */
13693 	ACQUIRE_GBLLOCK(&lk);
13694 	FREE_LOCK(ump);
13695 	proc_waiting += 1;
13696 	if (callout_pending(&softdep_callout) == FALSE)
13697 		callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13698 		    pause_timer, 0);
13699 
13700 	if ((td->td_pflags & TDP_KTHREAD) == 0)
13701 		msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13702 	proc_waiting -= 1;
13703 	FREE_GBLLOCK(&lk);
13704 	ACQUIRE_LOCK(ump);
13705 	return (1);
13706 }
13707 
13708 /*
13709  * Awaken processes pausing in request_cleanup and clear proc_waiting
13710  * to indicate that there is no longer a timer running. Pause_timer
13711  * will be called with the global softdep mutex (&lk) locked.
13712  */
13713 static void
13714 pause_timer(arg)
13715 	void *arg;
13716 {
13717 
13718 	GBLLOCK_OWNED(&lk);
13719 	/*
13720 	 * The callout_ API has acquired mtx and will hold it around this
13721 	 * function call.
13722 	 */
13723 	*stat_countp += proc_waiting;
13724 	wakeup(&proc_waiting);
13725 }
13726 
13727 /*
13728  * If requested, try removing inode or removal dependencies.
13729  */
13730 static void
13731 check_clear_deps(mp)
13732 	struct mount *mp;
13733 {
13734 
13735 	/*
13736 	 * If we are suspended, it may be because of our using
13737 	 * too many inodedeps, so help clear them out.
13738 	 */
13739 	if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended)
13740 		clear_inodedeps(mp);
13741 	/*
13742 	 * General requests for cleanup of backed up dependencies
13743 	 */
13744 	ACQUIRE_GBLLOCK(&lk);
13745 	if (req_clear_inodedeps) {
13746 		req_clear_inodedeps -= 1;
13747 		FREE_GBLLOCK(&lk);
13748 		clear_inodedeps(mp);
13749 		ACQUIRE_GBLLOCK(&lk);
13750 		wakeup(&proc_waiting);
13751 	}
13752 	if (req_clear_remove) {
13753 		req_clear_remove -= 1;
13754 		FREE_GBLLOCK(&lk);
13755 		clear_remove(mp);
13756 		ACQUIRE_GBLLOCK(&lk);
13757 		wakeup(&proc_waiting);
13758 	}
13759 	FREE_GBLLOCK(&lk);
13760 }
13761 
13762 /*
13763  * Flush out a directory with at least one removal dependency in an effort to
13764  * reduce the number of dirrem, freefile, and freeblks dependency structures.
13765  */
13766 static void
13767 clear_remove(mp)
13768 	struct mount *mp;
13769 {
13770 	struct pagedep_hashhead *pagedephd;
13771 	struct pagedep *pagedep;
13772 	struct ufsmount *ump;
13773 	struct vnode *vp;
13774 	struct bufobj *bo;
13775 	int error, cnt;
13776 	ino_t ino;
13777 
13778 	ump = VFSTOUFS(mp);
13779 	LOCK_OWNED(ump);
13780 
13781 	for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
13782 		pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
13783 		if (ump->pagedep_nextclean > ump->pagedep_hash_size)
13784 			ump->pagedep_nextclean = 0;
13785 		LIST_FOREACH(pagedep, pagedephd, pd_hash) {
13786 			if (LIST_EMPTY(&pagedep->pd_dirremhd))
13787 				continue;
13788 			ino = pagedep->pd_ino;
13789 			if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13790 				continue;
13791 			FREE_LOCK(ump);
13792 
13793 			/*
13794 			 * Let unmount clear deps
13795 			 */
13796 			error = vfs_busy(mp, MBF_NOWAIT);
13797 			if (error != 0)
13798 				goto finish_write;
13799 			error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13800 			     FFSV_FORCEINSMQ);
13801 			vfs_unbusy(mp);
13802 			if (error != 0) {
13803 				softdep_error("clear_remove: vget", error);
13804 				goto finish_write;
13805 			}
13806 			if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13807 				softdep_error("clear_remove: fsync", error);
13808 			bo = &vp->v_bufobj;
13809 			BO_LOCK(bo);
13810 			drain_output(vp);
13811 			BO_UNLOCK(bo);
13812 			vput(vp);
13813 		finish_write:
13814 			vn_finished_write(mp);
13815 			ACQUIRE_LOCK(ump);
13816 			return;
13817 		}
13818 	}
13819 }
13820 
13821 /*
13822  * Clear out a block of dirty inodes in an effort to reduce
13823  * the number of inodedep dependency structures.
13824  */
13825 static void
13826 clear_inodedeps(mp)
13827 	struct mount *mp;
13828 {
13829 	struct inodedep_hashhead *inodedephd;
13830 	struct inodedep *inodedep;
13831 	struct ufsmount *ump;
13832 	struct vnode *vp;
13833 	struct fs *fs;
13834 	int error, cnt;
13835 	ino_t firstino, lastino, ino;
13836 
13837 	ump = VFSTOUFS(mp);
13838 	fs = ump->um_fs;
13839 	LOCK_OWNED(ump);
13840 	/*
13841 	 * Pick a random inode dependency to be cleared.
13842 	 * We will then gather up all the inodes in its block
13843 	 * that have dependencies and flush them out.
13844 	 */
13845 	for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
13846 		inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
13847 		if (ump->inodedep_nextclean > ump->inodedep_hash_size)
13848 			ump->inodedep_nextclean = 0;
13849 		if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
13850 			break;
13851 	}
13852 	if (inodedep == NULL)
13853 		return;
13854 	/*
13855 	 * Find the last inode in the block with dependencies.
13856 	 */
13857 	firstino = rounddown2(inodedep->id_ino, INOPB(fs));
13858 	for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
13859 		if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
13860 			break;
13861 	/*
13862 	 * Asynchronously push all but the last inode with dependencies.
13863 	 * Synchronously push the last inode with dependencies to ensure
13864 	 * that the inode block gets written to free up the inodedeps.
13865 	 */
13866 	for (ino = firstino; ino <= lastino; ino++) {
13867 		if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13868 			continue;
13869 		if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13870 			continue;
13871 		FREE_LOCK(ump);
13872 		error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
13873 		if (error != 0) {
13874 			vn_finished_write(mp);
13875 			ACQUIRE_LOCK(ump);
13876 			return;
13877 		}
13878 		if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13879 		    FFSV_FORCEINSMQ)) != 0) {
13880 			softdep_error("clear_inodedeps: vget", error);
13881 			vfs_unbusy(mp);
13882 			vn_finished_write(mp);
13883 			ACQUIRE_LOCK(ump);
13884 			return;
13885 		}
13886 		vfs_unbusy(mp);
13887 		if (ino == lastino) {
13888 			if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)))
13889 				softdep_error("clear_inodedeps: fsync1", error);
13890 		} else {
13891 			if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13892 				softdep_error("clear_inodedeps: fsync2", error);
13893 			BO_LOCK(&vp->v_bufobj);
13894 			drain_output(vp);
13895 			BO_UNLOCK(&vp->v_bufobj);
13896 		}
13897 		vput(vp);
13898 		vn_finished_write(mp);
13899 		ACQUIRE_LOCK(ump);
13900 	}
13901 }
13902 
13903 void
13904 softdep_buf_append(bp, wkhd)
13905 	struct buf *bp;
13906 	struct workhead *wkhd;
13907 {
13908 	struct worklist *wk;
13909 	struct ufsmount *ump;
13910 
13911 	if ((wk = LIST_FIRST(wkhd)) == NULL)
13912 		return;
13913 	KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13914 	    ("softdep_buf_append called on non-softdep filesystem"));
13915 	ump = VFSTOUFS(wk->wk_mp);
13916 	ACQUIRE_LOCK(ump);
13917 	while ((wk = LIST_FIRST(wkhd)) != NULL) {
13918 		WORKLIST_REMOVE(wk);
13919 		WORKLIST_INSERT(&bp->b_dep, wk);
13920 	}
13921 	FREE_LOCK(ump);
13922 
13923 }
13924 
13925 void
13926 softdep_inode_append(ip, cred, wkhd)
13927 	struct inode *ip;
13928 	struct ucred *cred;
13929 	struct workhead *wkhd;
13930 {
13931 	struct buf *bp;
13932 	struct fs *fs;
13933 	struct ufsmount *ump;
13934 	int error;
13935 
13936 	ump = ITOUMP(ip);
13937 	KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
13938 	    ("softdep_inode_append called on non-softdep filesystem"));
13939 	fs = ump->um_fs;
13940 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
13941 	    (int)fs->fs_bsize, cred, &bp);
13942 	if (error) {
13943 		bqrelse(bp);
13944 		softdep_freework(wkhd);
13945 		return;
13946 	}
13947 	softdep_buf_append(bp, wkhd);
13948 	bqrelse(bp);
13949 }
13950 
13951 void
13952 softdep_freework(wkhd)
13953 	struct workhead *wkhd;
13954 {
13955 	struct worklist *wk;
13956 	struct ufsmount *ump;
13957 
13958 	if ((wk = LIST_FIRST(wkhd)) == NULL)
13959 		return;
13960 	KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13961 	    ("softdep_freework called on non-softdep filesystem"));
13962 	ump = VFSTOUFS(wk->wk_mp);
13963 	ACQUIRE_LOCK(ump);
13964 	handle_jwork(wkhd);
13965 	FREE_LOCK(ump);
13966 }
13967 
13968 static struct ufsmount *
13969 softdep_bp_to_mp(bp)
13970 	struct buf *bp;
13971 {
13972 	struct mount *mp;
13973 	struct vnode *vp;
13974 
13975 	if (LIST_EMPTY(&bp->b_dep))
13976 		return (NULL);
13977 	vp = bp->b_vp;
13978 	KASSERT(vp != NULL,
13979 	    ("%s, buffer with dependencies lacks vnode", __func__));
13980 
13981 	/*
13982 	 * The ump mount point is stable after we get a correct
13983 	 * pointer, since bp is locked and this prevents unmount from
13984 	 * proceeding.  But to get to it, we cannot dereference bp->b_dep
13985 	 * head wk_mp, because we do not yet own SU ump lock and
13986 	 * workitem might be freed while dereferenced.
13987 	 */
13988 retry:
13989 	switch (vp->v_type) {
13990 	case VCHR:
13991 		VI_LOCK(vp);
13992 		mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
13993 		VI_UNLOCK(vp);
13994 		if (mp == NULL)
13995 			goto retry;
13996 		break;
13997 	case VREG:
13998 	case VDIR:
13999 	case VLNK:
14000 	case VFIFO:
14001 	case VSOCK:
14002 		mp = vp->v_mount;
14003 		break;
14004 	case VBLK:
14005 		vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14006 		/* FALLTHROUGH */
14007 	case VNON:
14008 	case VBAD:
14009 	case VMARKER:
14010 		mp = NULL;
14011 		break;
14012 	default:
14013 		vn_printf(vp, "unknown vnode type");
14014 		mp = NULL;
14015 		break;
14016 	}
14017 	return (VFSTOUFS(mp));
14018 }
14019 
14020 /*
14021  * Function to determine if the buffer has outstanding dependencies
14022  * that will cause a roll-back if the buffer is written. If wantcount
14023  * is set, return number of dependencies, otherwise just yes or no.
14024  */
14025 static int
14026 softdep_count_dependencies(bp, wantcount)
14027 	struct buf *bp;
14028 	int wantcount;
14029 {
14030 	struct worklist *wk;
14031 	struct ufsmount *ump;
14032 	struct bmsafemap *bmsafemap;
14033 	struct freework *freework;
14034 	struct inodedep *inodedep;
14035 	struct indirdep *indirdep;
14036 	struct freeblks *freeblks;
14037 	struct allocindir *aip;
14038 	struct pagedep *pagedep;
14039 	struct dirrem *dirrem;
14040 	struct newblk *newblk;
14041 	struct mkdir *mkdir;
14042 	struct diradd *dap;
14043 	int i, retval;
14044 
14045 	ump = softdep_bp_to_mp(bp);
14046 	if (ump == NULL)
14047 		return (0);
14048 	retval = 0;
14049 	ACQUIRE_LOCK(ump);
14050 	LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14051 		switch (wk->wk_type) {
14052 
14053 		case D_INODEDEP:
14054 			inodedep = WK_INODEDEP(wk);
14055 			if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14056 				/* bitmap allocation dependency */
14057 				retval += 1;
14058 				if (!wantcount)
14059 					goto out;
14060 			}
14061 			if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14062 				/* direct block pointer dependency */
14063 				retval += 1;
14064 				if (!wantcount)
14065 					goto out;
14066 			}
14067 			if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14068 				/* direct block pointer dependency */
14069 				retval += 1;
14070 				if (!wantcount)
14071 					goto out;
14072 			}
14073 			if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14074 				/* Add reference dependency. */
14075 				retval += 1;
14076 				if (!wantcount)
14077 					goto out;
14078 			}
14079 			continue;
14080 
14081 		case D_INDIRDEP:
14082 			indirdep = WK_INDIRDEP(wk);
14083 
14084 			TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14085 				/* indirect truncation dependency */
14086 				retval += 1;
14087 				if (!wantcount)
14088 					goto out;
14089 			}
14090 
14091 			LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14092 				/* indirect block pointer dependency */
14093 				retval += 1;
14094 				if (!wantcount)
14095 					goto out;
14096 			}
14097 			continue;
14098 
14099 		case D_PAGEDEP:
14100 			pagedep = WK_PAGEDEP(wk);
14101 			LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14102 				if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14103 					/* Journal remove ref dependency. */
14104 					retval += 1;
14105 					if (!wantcount)
14106 						goto out;
14107 				}
14108 			}
14109 			for (i = 0; i < DAHASHSZ; i++) {
14110 
14111 				LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14112 					/* directory entry dependency */
14113 					retval += 1;
14114 					if (!wantcount)
14115 						goto out;
14116 				}
14117 			}
14118 			continue;
14119 
14120 		case D_BMSAFEMAP:
14121 			bmsafemap = WK_BMSAFEMAP(wk);
14122 			if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14123 				/* Add reference dependency. */
14124 				retval += 1;
14125 				if (!wantcount)
14126 					goto out;
14127 			}
14128 			if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14129 				/* Allocate block dependency. */
14130 				retval += 1;
14131 				if (!wantcount)
14132 					goto out;
14133 			}
14134 			continue;
14135 
14136 		case D_FREEBLKS:
14137 			freeblks = WK_FREEBLKS(wk);
14138 			if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14139 				/* Freeblk journal dependency. */
14140 				retval += 1;
14141 				if (!wantcount)
14142 					goto out;
14143 			}
14144 			continue;
14145 
14146 		case D_ALLOCDIRECT:
14147 		case D_ALLOCINDIR:
14148 			newblk = WK_NEWBLK(wk);
14149 			if (newblk->nb_jnewblk) {
14150 				/* Journal allocate dependency. */
14151 				retval += 1;
14152 				if (!wantcount)
14153 					goto out;
14154 			}
14155 			continue;
14156 
14157 		case D_MKDIR:
14158 			mkdir = WK_MKDIR(wk);
14159 			if (mkdir->md_jaddref) {
14160 				/* Journal reference dependency. */
14161 				retval += 1;
14162 				if (!wantcount)
14163 					goto out;
14164 			}
14165 			continue;
14166 
14167 		case D_FREEWORK:
14168 		case D_FREEDEP:
14169 		case D_JSEGDEP:
14170 		case D_JSEG:
14171 		case D_SBDEP:
14172 			/* never a dependency on these blocks */
14173 			continue;
14174 
14175 		default:
14176 			panic("softdep_count_dependencies: Unexpected type %s",
14177 			    TYPENAME(wk->wk_type));
14178 			/* NOTREACHED */
14179 		}
14180 	}
14181 out:
14182 	FREE_LOCK(ump);
14183 	return (retval);
14184 }
14185 
14186 /*
14187  * Acquire exclusive access to a buffer.
14188  * Must be called with a locked mtx parameter.
14189  * Return acquired buffer or NULL on failure.
14190  */
14191 static struct buf *
14192 getdirtybuf(bp, lock, waitfor)
14193 	struct buf *bp;
14194 	struct rwlock *lock;
14195 	int waitfor;
14196 {
14197 	int error;
14198 
14199 	if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14200 		if (waitfor != MNT_WAIT)
14201 			return (NULL);
14202 		error = BUF_LOCK(bp,
14203 		    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14204 		/*
14205 		 * Even if we successfully acquire bp here, we have dropped
14206 		 * lock, which may violates our guarantee.
14207 		 */
14208 		if (error == 0)
14209 			BUF_UNLOCK(bp);
14210 		else if (error != ENOLCK)
14211 			panic("getdirtybuf: inconsistent lock: %d", error);
14212 		rw_wlock(lock);
14213 		return (NULL);
14214 	}
14215 	if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14216 		if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14217 			rw_wunlock(lock);
14218 			BO_LOCK(bp->b_bufobj);
14219 			BUF_UNLOCK(bp);
14220 			if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14221 				bp->b_vflags |= BV_BKGRDWAIT;
14222 				msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14223 				       PRIBIO | PDROP, "getbuf", 0);
14224 			} else
14225 				BO_UNLOCK(bp->b_bufobj);
14226 			rw_wlock(lock);
14227 			return (NULL);
14228 		}
14229 		BUF_UNLOCK(bp);
14230 		if (waitfor != MNT_WAIT)
14231 			return (NULL);
14232 #ifdef DEBUG_VFS_LOCKS
14233 		if (bp->b_vp->v_type != VCHR)
14234 			ASSERT_BO_WLOCKED(bp->b_bufobj);
14235 #endif
14236 		bp->b_vflags |= BV_BKGRDWAIT;
14237 		rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14238 		return (NULL);
14239 	}
14240 	if ((bp->b_flags & B_DELWRI) == 0) {
14241 		BUF_UNLOCK(bp);
14242 		return (NULL);
14243 	}
14244 	bremfree(bp);
14245 	return (bp);
14246 }
14247 
14248 
14249 /*
14250  * Check if it is safe to suspend the file system now.  On entry,
14251  * the vnode interlock for devvp should be held.  Return 0 with
14252  * the mount interlock held if the file system can be suspended now,
14253  * otherwise return EAGAIN with the mount interlock held.
14254  */
14255 int
14256 softdep_check_suspend(struct mount *mp,
14257 		      struct vnode *devvp,
14258 		      int softdep_depcnt,
14259 		      int softdep_accdepcnt,
14260 		      int secondary_writes,
14261 		      int secondary_accwrites)
14262 {
14263 	struct bufobj *bo;
14264 	struct ufsmount *ump;
14265 	struct inodedep *inodedep;
14266 	int error, unlinked;
14267 
14268 	bo = &devvp->v_bufobj;
14269 	ASSERT_BO_WLOCKED(bo);
14270 
14271 	/*
14272 	 * If we are not running with soft updates, then we need only
14273 	 * deal with secondary writes as we try to suspend.
14274 	 */
14275 	if (MOUNTEDSOFTDEP(mp) == 0) {
14276 		MNT_ILOCK(mp);
14277 		while (mp->mnt_secondary_writes != 0) {
14278 			BO_UNLOCK(bo);
14279 			msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14280 			    (PUSER - 1) | PDROP, "secwr", 0);
14281 			BO_LOCK(bo);
14282 			MNT_ILOCK(mp);
14283 		}
14284 
14285 		/*
14286 		 * Reasons for needing more work before suspend:
14287 		 * - Dirty buffers on devvp.
14288 		 * - Secondary writes occurred after start of vnode sync loop
14289 		 */
14290 		error = 0;
14291 		if (bo->bo_numoutput > 0 ||
14292 		    bo->bo_dirty.bv_cnt > 0 ||
14293 		    secondary_writes != 0 ||
14294 		    mp->mnt_secondary_writes != 0 ||
14295 		    secondary_accwrites != mp->mnt_secondary_accwrites)
14296 			error = EAGAIN;
14297 		BO_UNLOCK(bo);
14298 		return (error);
14299 	}
14300 
14301 	/*
14302 	 * If we are running with soft updates, then we need to coordinate
14303 	 * with them as we try to suspend.
14304 	 */
14305 	ump = VFSTOUFS(mp);
14306 	for (;;) {
14307 		if (!TRY_ACQUIRE_LOCK(ump)) {
14308 			BO_UNLOCK(bo);
14309 			ACQUIRE_LOCK(ump);
14310 			FREE_LOCK(ump);
14311 			BO_LOCK(bo);
14312 			continue;
14313 		}
14314 		MNT_ILOCK(mp);
14315 		if (mp->mnt_secondary_writes != 0) {
14316 			FREE_LOCK(ump);
14317 			BO_UNLOCK(bo);
14318 			msleep(&mp->mnt_secondary_writes,
14319 			       MNT_MTX(mp),
14320 			       (PUSER - 1) | PDROP, "secwr", 0);
14321 			BO_LOCK(bo);
14322 			continue;
14323 		}
14324 		break;
14325 	}
14326 
14327 	unlinked = 0;
14328 	if (MOUNTEDSUJ(mp)) {
14329 		for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14330 		    inodedep != NULL;
14331 		    inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14332 			if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14333 			    UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14334 			    UNLINKONLIST) ||
14335 			    !check_inodedep_free(inodedep))
14336 				continue;
14337 			unlinked++;
14338 		}
14339 	}
14340 
14341 	/*
14342 	 * Reasons for needing more work before suspend:
14343 	 * - Dirty buffers on devvp.
14344 	 * - Softdep activity occurred after start of vnode sync loop
14345 	 * - Secondary writes occurred after start of vnode sync loop
14346 	 */
14347 	error = 0;
14348 	if (bo->bo_numoutput > 0 ||
14349 	    bo->bo_dirty.bv_cnt > 0 ||
14350 	    softdep_depcnt != unlinked ||
14351 	    ump->softdep_deps != unlinked ||
14352 	    softdep_accdepcnt != ump->softdep_accdeps ||
14353 	    secondary_writes != 0 ||
14354 	    mp->mnt_secondary_writes != 0 ||
14355 	    secondary_accwrites != mp->mnt_secondary_accwrites)
14356 		error = EAGAIN;
14357 	FREE_LOCK(ump);
14358 	BO_UNLOCK(bo);
14359 	return (error);
14360 }
14361 
14362 
14363 /*
14364  * Get the number of dependency structures for the file system, both
14365  * the current number and the total number allocated.  These will
14366  * later be used to detect that softdep processing has occurred.
14367  */
14368 void
14369 softdep_get_depcounts(struct mount *mp,
14370 		      int *softdep_depsp,
14371 		      int *softdep_accdepsp)
14372 {
14373 	struct ufsmount *ump;
14374 
14375 	if (MOUNTEDSOFTDEP(mp) == 0) {
14376 		*softdep_depsp = 0;
14377 		*softdep_accdepsp = 0;
14378 		return;
14379 	}
14380 	ump = VFSTOUFS(mp);
14381 	ACQUIRE_LOCK(ump);
14382 	*softdep_depsp = ump->softdep_deps;
14383 	*softdep_accdepsp = ump->softdep_accdeps;
14384 	FREE_LOCK(ump);
14385 }
14386 
14387 /*
14388  * Wait for pending output on a vnode to complete.
14389  */
14390 static void
14391 drain_output(vp)
14392 	struct vnode *vp;
14393 {
14394 
14395 	ASSERT_VOP_LOCKED(vp, "drain_output");
14396 	(void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14397 }
14398 
14399 /*
14400  * Called whenever a buffer that is being invalidated or reallocated
14401  * contains dependencies. This should only happen if an I/O error has
14402  * occurred. The routine is called with the buffer locked.
14403  */
14404 static void
14405 softdep_deallocate_dependencies(bp)
14406 	struct buf *bp;
14407 {
14408 
14409 	if ((bp->b_ioflags & BIO_ERROR) == 0)
14410 		panic("softdep_deallocate_dependencies: dangling deps");
14411 	if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14412 		softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14413 	else
14414 		printf("softdep_deallocate_dependencies: "
14415 		    "got error %d while accessing filesystem\n", bp->b_error);
14416 	if (bp->b_error != ENXIO)
14417 		panic("softdep_deallocate_dependencies: unrecovered I/O error");
14418 }
14419 
14420 /*
14421  * Function to handle asynchronous write errors in the filesystem.
14422  */
14423 static void
14424 softdep_error(func, error)
14425 	char *func;
14426 	int error;
14427 {
14428 
14429 	/* XXX should do something better! */
14430 	printf("%s: got error %d while accessing filesystem\n", func, error);
14431 }
14432 
14433 #ifdef DDB
14434 
14435 /* exported to ffs_vfsops.c */
14436 extern void db_print_ffs(struct ufsmount *ump);
14437 void
14438 db_print_ffs(struct ufsmount *ump)
14439 {
14440 	db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14441 	    ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14442 	db_printf("    fs %p su_wl %d su_deps %d su_req %d\n",
14443 	    ump->um_fs, ump->softdep_on_worklist,
14444 	    ump->softdep_deps, ump->softdep_req);
14445 }
14446 
14447 static void
14448 worklist_print(struct worklist *wk, int verbose)
14449 {
14450 
14451 	if (!verbose) {
14452 		db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14453 		    (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
14454 		return;
14455 	}
14456 	db_printf("worklist: %p type %s state 0x%b next %p\n    ", wk,
14457 	    TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
14458 	    LIST_NEXT(wk, wk_list));
14459 	db_print_ffs(VFSTOUFS(wk->wk_mp));
14460 }
14461 
14462 static void
14463 inodedep_print(struct inodedep *inodedep, int verbose)
14464 {
14465 
14466 	worklist_print(&inodedep->id_list, 0);
14467 	db_printf("    fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14468 	    inodedep->id_fs,
14469 	    (intmax_t)inodedep->id_ino,
14470 	    (intmax_t)fsbtodb(inodedep->id_fs,
14471 	        ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14472 	    (intmax_t)inodedep->id_nlinkdelta,
14473 	    (intmax_t)inodedep->id_savednlink);
14474 
14475 	if (verbose == 0)
14476 		return;
14477 
14478 	db_printf("    bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14479 	    inodedep->id_bmsafemap,
14480 	    inodedep->id_mkdiradd,
14481 	    TAILQ_FIRST(&inodedep->id_inoreflst));
14482 	db_printf("    dirremhd %p, pendinghd %p, bufwait %p\n",
14483 	    LIST_FIRST(&inodedep->id_dirremhd),
14484 	    LIST_FIRST(&inodedep->id_pendinghd),
14485 	    LIST_FIRST(&inodedep->id_bufwait));
14486 	db_printf("    inowait %p, inoupdt %p, newinoupdt %p\n",
14487 	    LIST_FIRST(&inodedep->id_inowait),
14488 	    TAILQ_FIRST(&inodedep->id_inoupdt),
14489 	    TAILQ_FIRST(&inodedep->id_newinoupdt));
14490 	db_printf("    extupdt %p, newextupdt %p, freeblklst %p\n",
14491 	    TAILQ_FIRST(&inodedep->id_extupdt),
14492 	    TAILQ_FIRST(&inodedep->id_newextupdt),
14493 	    TAILQ_FIRST(&inodedep->id_freeblklst));
14494 	db_printf("    saveino %p, savedsize %jd, savedextsize %jd\n",
14495 	    inodedep->id_savedino1,
14496 	    (intmax_t)inodedep->id_savedsize,
14497 	    (intmax_t)inodedep->id_savedextsize);
14498 }
14499 
14500 static void
14501 newblk_print(struct newblk *nbp)
14502 {
14503 
14504 	worklist_print(&nbp->nb_list, 0);
14505 	db_printf("    newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14506 	db_printf("    jnewblk %p, bmsafemap %p, freefrag %p\n",
14507 	    &nbp->nb_jnewblk,
14508 	    &nbp->nb_bmsafemap,
14509 	    &nbp->nb_freefrag);
14510 	db_printf("    indirdeps %p, newdirblk %p, jwork %p\n",
14511 	    LIST_FIRST(&nbp->nb_indirdeps),
14512 	    LIST_FIRST(&nbp->nb_newdirblk),
14513 	    LIST_FIRST(&nbp->nb_jwork));
14514 }
14515 
14516 static void
14517 allocdirect_print(struct allocdirect *adp)
14518 {
14519 
14520 	newblk_print(&adp->ad_block);
14521 	db_printf("    oldblkno %jd, oldsize %ld, newsize %ld\n",
14522 	    adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14523 	db_printf("    offset %d, inodedep %p\n",
14524 	    adp->ad_offset, adp->ad_inodedep);
14525 }
14526 
14527 static void
14528 allocindir_print(struct allocindir *aip)
14529 {
14530 
14531 	newblk_print(&aip->ai_block);
14532 	db_printf("    oldblkno %jd, lbn %jd\n",
14533 	    (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
14534 	db_printf("    offset %d, indirdep %p\n",
14535 	    aip->ai_offset, aip->ai_indirdep);
14536 }
14537 
14538 static void
14539 mkdir_print(struct mkdir *mkdir)
14540 {
14541 
14542 	worklist_print(&mkdir->md_list, 0);
14543 	db_printf("    diradd %p, jaddref %p, buf %p\n",
14544 		mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
14545 }
14546 
14547 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
14548 {
14549 
14550 	if (have_addr == 0) {
14551 		db_printf("inodedep address required\n");
14552 		return;
14553 	}
14554 	inodedep_print((struct inodedep*)addr, 1);
14555 }
14556 
14557 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
14558 {
14559 	struct inodedep_hashhead *inodedephd;
14560 	struct inodedep *inodedep;
14561 	struct ufsmount *ump;
14562 	int cnt;
14563 
14564 	if (have_addr == 0) {
14565 		db_printf("ufsmount address required\n");
14566 		return;
14567 	}
14568 	ump = (struct ufsmount *)addr;
14569 	for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14570 		inodedephd = &ump->inodedep_hashtbl[cnt];
14571 		LIST_FOREACH(inodedep, inodedephd, id_hash) {
14572 			inodedep_print(inodedep, 0);
14573 		}
14574 	}
14575 }
14576 
14577 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
14578 {
14579 
14580 	if (have_addr == 0) {
14581 		db_printf("worklist address required\n");
14582 		return;
14583 	}
14584 	worklist_print((struct worklist *)addr, 1);
14585 }
14586 
14587 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
14588 {
14589 	struct worklist *wk;
14590 	struct workhead *wkhd;
14591 
14592 	if (have_addr == 0) {
14593 		db_printf("worklist address required "
14594 		    "(for example value in bp->b_dep)\n");
14595 		return;
14596 	}
14597 	/*
14598 	 * We often do not have the address of the worklist head but
14599 	 * instead a pointer to its first entry (e.g., we have the
14600 	 * contents of bp->b_dep rather than &bp->b_dep). But the back
14601 	 * pointer of bp->b_dep will point at the head of the list, so
14602 	 * we cheat and use that instead. If we are in the middle of
14603 	 * a list we will still get the same result, so nothing
14604 	 * unexpected will result.
14605 	 */
14606 	wk = (struct worklist *)addr;
14607 	if (wk == NULL)
14608 		return;
14609 	wkhd = (struct workhead *)wk->wk_list.le_prev;
14610 	LIST_FOREACH(wk, wkhd, wk_list) {
14611 		switch(wk->wk_type) {
14612 		case D_INODEDEP:
14613 			inodedep_print(WK_INODEDEP(wk), 0);
14614 			continue;
14615 		case D_ALLOCDIRECT:
14616 			allocdirect_print(WK_ALLOCDIRECT(wk));
14617 			continue;
14618 		case D_ALLOCINDIR:
14619 			allocindir_print(WK_ALLOCINDIR(wk));
14620 			continue;
14621 		case D_MKDIR:
14622 			mkdir_print(WK_MKDIR(wk));
14623 			continue;
14624 		default:
14625 			worklist_print(wk, 0);
14626 			continue;
14627 		}
14628 	}
14629 }
14630 
14631 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
14632 {
14633 	if (have_addr == 0) {
14634 		db_printf("mkdir address required\n");
14635 		return;
14636 	}
14637 	mkdir_print((struct mkdir *)addr);
14638 }
14639 
14640 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
14641 {
14642 	struct mkdirlist *mkdirlisthd;
14643 	struct mkdir *mkdir;
14644 
14645 	if (have_addr == 0) {
14646 		db_printf("mkdir listhead address required\n");
14647 		return;
14648 	}
14649 	mkdirlisthd = (struct mkdirlist *)addr;
14650 	LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14651 		mkdir_print(mkdir);
14652 		if (mkdir->md_diradd != NULL) {
14653 			db_printf("    ");
14654 			worklist_print(&mkdir->md_diradd->da_list, 0);
14655 		}
14656 		if (mkdir->md_jaddref != NULL) {
14657 			db_printf("    ");
14658 			worklist_print(&mkdir->md_jaddref->ja_list, 0);
14659 		}
14660 	}
14661 }
14662 
14663 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
14664 {
14665 	if (have_addr == 0) {
14666 		db_printf("allocdirect address required\n");
14667 		return;
14668 	}
14669 	allocdirect_print((struct allocdirect *)addr);
14670 }
14671 
14672 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
14673 {
14674 	if (have_addr == 0) {
14675 		db_printf("allocindir address required\n");
14676 		return;
14677 	}
14678 	allocindir_print((struct allocindir *)addr);
14679 }
14680 
14681 #endif /* DDB */
14682 
14683 #endif /* SOFTUPDATES */
14684