2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 1998, 2000 Marshall Kirk McKusick.
5 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
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).
13 * Further information about soft updates can be obtained from:
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
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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.
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.
41 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 #include "opt_quota.h"
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
57 #include <sys/kthread.h>
59 #include <sys/limits.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
67 #include <sys/racct.h>
68 #include <sys/rwlock.h>
70 #include <sys/sysctl.h>
71 #include <sys/syslog.h>
72 #include <sys/vnode.h>
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>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_object.h>
89 #include <geom/geom.h>
93 #define KTR_SUJ 0 /* Define to KTR_SPARE. */
98 softdep_flushfiles(oldmnt, flags, td)
104 panic("softdep_flushfiles called");
108 softdep_mount(devvp, mp, fs, cred)
126 softdep_uninitialize()
137 panic("softdep_unmount called");
141 softdep_setup_sbupdate(ump, fs, bp)
142 struct ufsmount *ump;
147 panic("softdep_setup_sbupdate called");
151 softdep_setup_inomapdep(bp, ip, newinum, mode)
158 panic("softdep_setup_inomapdep called");
162 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
165 ufs2_daddr_t newblkno;
170 panic("softdep_setup_blkmapdep called");
174 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
177 ufs2_daddr_t newblkno;
178 ufs2_daddr_t oldblkno;
184 panic("softdep_setup_allocdirect called");
188 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
191 ufs2_daddr_t newblkno;
192 ufs2_daddr_t oldblkno;
198 panic("softdep_setup_allocext called");
202 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
207 ufs2_daddr_t newblkno;
208 ufs2_daddr_t oldblkno;
212 panic("softdep_setup_allocindir_page called");
216 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
221 ufs2_daddr_t newblkno;
224 panic("softdep_setup_allocindir_meta called");
228 softdep_journal_freeblocks(ip, cred, length, flags)
235 panic("softdep_journal_freeblocks called");
239 softdep_journal_fsync(ip)
243 panic("softdep_journal_fsync called");
247 softdep_setup_freeblocks(ip, length, flags)
253 panic("softdep_setup_freeblocks called");
257 softdep_freefile(pvp, ino, mode)
263 panic("softdep_freefile called");
267 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
272 struct buf *newdirbp;
276 panic("softdep_setup_directory_add called");
280 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
289 panic("softdep_change_directoryentry_offset called");
293 softdep_setup_remove(bp, dp, ip, isrmdir)
300 panic("softdep_setup_remove called");
304 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
312 panic("softdep_setup_directory_change called");
316 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
321 struct workhead *wkhd;
324 panic("%s called", __FUNCTION__);
328 softdep_setup_inofree(mp, bp, ino, wkhd)
332 struct workhead *wkhd;
335 panic("%s called", __FUNCTION__);
339 softdep_setup_unlink(dp, ip)
344 panic("%s called", __FUNCTION__);
348 softdep_setup_link(dp, ip)
353 panic("%s called", __FUNCTION__);
357 softdep_revert_link(dp, ip)
362 panic("%s called", __FUNCTION__);
366 softdep_setup_rmdir(dp, ip)
371 panic("%s called", __FUNCTION__);
375 softdep_revert_rmdir(dp, ip)
380 panic("%s called", __FUNCTION__);
384 softdep_setup_create(dp, ip)
389 panic("%s called", __FUNCTION__);
393 softdep_revert_create(dp, ip)
398 panic("%s called", __FUNCTION__);
402 softdep_setup_mkdir(dp, ip)
407 panic("%s called", __FUNCTION__);
411 softdep_revert_mkdir(dp, ip)
416 panic("%s called", __FUNCTION__);
420 softdep_setup_dotdot_link(dp, ip)
425 panic("%s called", __FUNCTION__);
429 softdep_prealloc(vp, waitok)
434 panic("%s called", __FUNCTION__);
438 softdep_journal_lookup(mp, vpp)
447 softdep_change_linkcnt(ip)
451 panic("softdep_change_linkcnt called");
455 softdep_load_inodeblock(ip)
459 panic("softdep_load_inodeblock called");
463 softdep_update_inodeblock(ip, bp, waitfor)
469 panic("softdep_update_inodeblock called");
474 struct vnode *vp; /* the "in_core" copy of the inode */
481 softdep_fsync_mountdev(vp)
489 softdep_flushworklist(oldmnt, countp, td)
490 struct mount *oldmnt;
500 softdep_sync_metadata(struct vnode *vp)
503 panic("softdep_sync_metadata called");
507 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
510 panic("softdep_sync_buf called");
518 panic("softdep_slowdown called");
522 softdep_request_cleanup(fs, vp, cred, resource)
533 softdep_check_suspend(struct mount *mp,
536 int softdep_accdepcnt,
537 int secondary_writes,
538 int secondary_accwrites)
543 (void) softdep_depcnt,
544 (void) softdep_accdepcnt;
546 bo = &devvp->v_bufobj;
547 ASSERT_BO_WLOCKED(bo);
550 while (mp->mnt_secondary_writes != 0) {
552 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
553 (PUSER - 1) | PDROP, "secwr", 0);
559 * Reasons for needing more work before suspend:
560 * - Dirty buffers on devvp.
561 * - Secondary writes occurred after start of vnode sync loop
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)
575 softdep_get_depcounts(struct mount *mp,
577 int *softdepactiveaccp)
581 *softdepactiveaccp = 0;
585 softdep_buf_append(bp, wkhd)
587 struct workhead *wkhd;
590 panic("softdep_buf_appendwork called");
594 softdep_inode_append(ip, cred, wkhd)
597 struct workhead *wkhd;
600 panic("softdep_inode_appendwork called");
604 softdep_freework(wkhd)
605 struct workhead *wkhd;
608 panic("softdep_freework called");
613 FEATURE(softupdates, "FFS soft-updates support");
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");
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];
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, "");
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");
671 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
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");
677 #define M_SOFTDEP_FLAGS (M_WAITOK)
680 * translate from workitem type to memory type
681 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
683 static struct malloc_type *memtype[] = {
715 #define DtoM(type) (memtype[type])
718 * Names of malloc types.
720 #define TYPENAME(type) \
721 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
722 memtype[type]->ks_shortdesc : "???")
724 * End system adaptation definitions.
727 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
728 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
731 * Internal function prototypes.
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 *,
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 *,
758 static int handle_written_inodeblock(struct inodedep *, struct buf *, int);
759 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
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 *,
781 static void handle_allocdirect_partdone(struct allocdirect *,
783 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
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 *,
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 *,
804 static void cancel_indirdep(struct indirdep *, struct buf *,
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 *,
813 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
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 *,
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 *,
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 *,
840 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
841 static int cancel_jaddref(struct jaddref *, struct inodedep *,
843 static void cancel_jfreefrag(struct jfreefrag *);
844 static inline void setup_freedirect(struct freeblks *, struct inode *,
846 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
847 static inline void setup_freeindir(struct freeblks *, struct inode *, 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,
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 *,
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,
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,
891 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
892 static int inodedep_find(struct inodedep_hashhead *, ino_t,
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,
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,
932 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_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,
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 *,
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);
960 * Exported softdep operations.
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);
968 * Global lock over all of soft updates.
970 static struct mtx lk;
971 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF);
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)
978 * Per-filesystem soft-updates locking.
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, \
987 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock)
988 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock)
991 * Worklist queue management.
992 * These routines require that the lock be held.
994 #ifndef /* NOT */ INVARIANTS
995 #define WORKLIST_INSERT(head, item) do { \
996 (item)->wk_state |= ONWORKLIST; \
997 LIST_INSERT_HEAD(head, item, wk_list); \
999 #define WORKLIST_REMOVE(item) do { \
1000 (item)->wk_state &= ~ONWORKLIST; \
1001 LIST_REMOVE(item, wk_list); \
1003 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT
1004 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE
1006 #else /* INVARIANTS */
1007 static void worklist_insert(struct workhead *, struct worklist *, int,
1009 static void worklist_remove(struct worklist *, int, const char *, int);
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__)
1021 worklist_insert(head, item, locked, func, line)
1022 struct workhead *head;
1023 struct worklist *item;
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);
1043 worklist_remove(item, locked, func, line)
1044 struct worklist *item;
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);
1062 #endif /* INVARIANTS */
1065 * Merge two jsegdeps keeping only the oldest one as newer references
1066 * can't be discarded until after older references.
1068 static inline struct jsegdep *
1069 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1071 struct jsegdep *swp;
1076 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1081 WORKLIST_REMOVE(&two->jd_list);
1088 * If two freedeps are compatible free one to reduce list size.
1090 static inline struct freedep *
1091 freedep_merge(struct freedep *one, struct freedep *two)
1096 if (one->fd_freework == two->fd_freework) {
1097 WORKLIST_REMOVE(&two->fd_list);
1104 * Move journal work from one list to another. Duplicate freedeps and
1105 * jsegdeps are coalesced to keep the lists as small as possible.
1108 jwork_move(dst, src)
1109 struct workhead *dst;
1110 struct workhead *src;
1112 struct freedep *freedep;
1113 struct jsegdep *jsegdep;
1114 struct worklist *wkn;
1115 struct worklist *wk;
1118 ("jwork_move: dst == src"));
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);
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);
1135 if (wk->wk_type == D_FREEDEP)
1136 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1141 jwork_insert(dst, jsegdep)
1142 struct workhead *dst;
1143 struct jsegdep *jsegdep;
1145 struct jsegdep *jsegdepn;
1146 struct worklist *wk;
1148 LIST_FOREACH(wk, dst, wk_list)
1149 if (wk->wk_type == D_JSEGDEP)
1152 WORKLIST_INSERT(dst, &jsegdep->jd_list);
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);
1161 free_jsegdep(jsegdep);
1165 * Routines for tracking and managing workitems.
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);
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))
1177 workitem_free(item, type)
1178 struct worklist *item;
1181 struct ufsmount *ump;
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));
1193 if (item->wk_state & IOWAITING)
1195 ump = VFSTOUFS(item->wk_mp);
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));
1214 workitem_alloc(item, type, mp)
1215 struct worklist *item;
1219 struct ufsmount *ump;
1221 item->wk_type = type;
1226 ACQUIRE_GBLLOCK(&lk);
1227 dep_current[type]++;
1228 if (dep_current[type] > dep_highuse[type])
1229 dep_highuse[type] = dep_current[type];
1233 ump->softdep_curdeps[type] += 1;
1234 ump->softdep_deps++;
1235 ump->softdep_accdeps++;
1240 workitem_reassign(item, newtype)
1241 struct worklist *item;
1244 struct ufsmount *ump;
1246 ump = VFSTOUFS(item->wk_mp);
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]++;
1263 item->wk_type = newtype;
1267 * Workitem queue management
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? */
1279 * runtime statistics
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 */
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,
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, "");
1367 SYSCTL_DECL(_vfs_ffs);
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");
1377 /* List of all filesystems mounted with soft updates */
1378 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
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.
1388 static int searchfailed;
1389 extern struct proc *bufdaemonproc;
1396 struct ufsmount *ump;
1399 td->td_pflags |= TDP_NORUNNINGBUF;
1400 mp = (struct mount *)addr;
1402 atomic_add_int(&stat_flush_threads, 1);
1404 ump->softdep_flags &= ~FLUSH_STARTING;
1405 wakeup(&ump->softdep_flushtd);
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);
1414 while (softdep_process_worklist(mp, 0) > 0 ||
1416 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1417 kthread_suspend_check();
1419 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1420 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1422 ump->softdep_flags &= ~FLUSH_CLEANUP;
1424 * Check to see if we are done and need to exit.
1426 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1430 ump->softdep_flags &= ~FLUSH_EXIT;
1432 wakeup(&ump->softdep_flags);
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);
1437 panic("kthread_exit failed\n");
1442 worklist_speedup(mp)
1445 struct ufsmount *ump;
1449 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1450 ump->softdep_flags |= FLUSH_CLEANUP;
1451 wakeup(&ump->softdep_flushtd);
1455 softdep_speedup(ump)
1456 struct ufsmount *ump;
1458 struct ufsmount *altump;
1459 struct mount_softdeps *sdp;
1462 worklist_speedup(ump->um_mountp);
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.
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)
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))
1489 * Move to the end of the list so we pick a
1490 * different one on out next try.
1492 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1493 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
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);
1503 return (speedup_syncer());
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.
1514 #define WK_HEAD 0x0001 /* Add to HEAD. */
1515 #define WK_NODELAY 0x0002 /* Process immediately. */
1518 add_to_worklist(wk, flags)
1519 struct worklist *wk;
1522 struct ufsmount *ump;
1524 ump = VFSTOUFS(wk->wk_mp);
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);
1536 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1537 ump->softdep_worklist_tail = wk;
1539 ump->softdep_on_worklist += 1;
1540 if (flags & WK_NODELAY)
1541 worklist_speedup(wk->wk_mp);
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.
1549 remove_from_worklist(wk)
1550 struct worklist *wk;
1552 struct ufsmount *ump;
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;
1564 struct worklist *wk;
1566 if (wk->wk_state & IOWAITING) {
1567 wk->wk_state &= ~IOWAITING;
1573 wait_worklist(wk, wmesg)
1574 struct worklist *wk;
1577 struct ufsmount *ump;
1579 ump = VFSTOUFS(wk->wk_mp);
1580 wk->wk_state |= IOWAITING;
1581 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1585 * Process that runs once per second to handle items in the background queue.
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.
1594 softdep_process_worklist(mp, full)
1599 struct ufsmount *ump;
1602 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1603 if (MOUNTEDSOFTDEP(mp) == 0)
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)
1616 check_clear_deps(mp);
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.
1621 if (should_yield()) {
1623 kern_yield(PRI_USER);
1628 * Never allow processing to run for more than one
1629 * second. This gives the syncer thread the opportunity
1630 * to pause if appropriate.
1632 if (!full && starttime != time_second)
1636 journal_unsuspend(ump);
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.
1650 struct inodedep *inodedep;
1651 struct dirrem *dirrem;
1652 struct ufsmount *ump;
1659 inum = VTOI(vp)->i_number;
1662 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1664 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
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.
1670 if (dirrem->dm_state & INPROGRESS) {
1671 wait_worklist(&dirrem->dm_list, "pwrwait");
1674 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1675 (COMPLETE | ONWORKLIST))
1680 remove_from_worklist(&dirrem->dm_list);
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);
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.
1697 process_truncates(vp)
1700 struct inodedep *inodedep;
1701 struct freeblks *freeblks;
1702 struct ufsmount *ump;
1710 inum = VTOI(vp)->i_number;
1712 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1715 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1716 /* Journal entries not yet written. */
1717 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1719 &freeblks->fb_jblkdephd)->jb_list,
1723 /* Another thread is executing this item. */
1724 if (freeblks->fb_state & INPROGRESS) {
1725 wait_worklist(&freeblks->fb_list, "ptrwait");
1728 /* Freeblks is waiting on a inode write. */
1729 if ((freeblks->fb_state & COMPLETE) == 0) {
1735 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1736 (ALLCOMPLETE | ONWORKLIST)) {
1737 remove_from_worklist(&freeblks->fb_list);
1738 freeblks->fb_state |= INPROGRESS;
1740 if (vn_start_secondary_write(NULL, &mp,
1742 panic("process_truncates: "
1743 "suspended filesystem");
1744 handle_workitem_freeblocks(freeblks, 0);
1745 vn_finished_secondary_write(mp);
1749 if (freeblks->fb_cgwait)
1754 sync_cgs(mp, MNT_WAIT);
1755 ffs_sync_snap(mp, MNT_WAIT);
1759 if (freeblks == NULL)
1766 * Process one item on the worklist.
1769 process_worklist_item(mp, target, flags)
1774 struct worklist sentinel;
1775 struct worklist *wk;
1776 struct ufsmount *ump;
1780 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
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.
1786 if (curthread->td_pflags & TDP_COWINPROGRESS)
1788 PHOLD(curproc); /* Don't let the stack go away. */
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);
1802 if (wk->wk_state & INPROGRESS)
1803 panic("process_worklist_item: %p already in progress.",
1805 wk->wk_state |= INPROGRESS;
1806 remove_from_worklist(wk);
1808 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1809 panic("process_worklist_item: suspended filesystem");
1810 switch (wk->wk_type) {
1812 /* removal of a directory entry */
1813 error = handle_workitem_remove(WK_DIRREM(wk), flags);
1817 /* releasing blocks and/or fragments from a file */
1818 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
1823 /* releasing a fragment when replaced as a file grows */
1824 handle_workitem_freefrag(WK_FREEFRAG(wk));
1829 /* releasing an inode when its link count drops to 0 */
1830 handle_workitem_freefile(WK_FREEFILE(wk));
1835 panic("%s_process_worklist: Unknown type %s",
1836 "softdep", TYPENAME(wk->wk_type));
1839 vn_finished_secondary_write(mp);
1842 if (++matchcnt == target)
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
1852 wk->wk_state &= ~INPROGRESS;
1854 add_to_worklist(wk, WK_HEAD);
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);
1866 * Move dependencies from one buffer to another.
1869 softdep_move_dependencies(oldbp, newbp)
1873 struct worklist *wk, *wktail;
1874 struct ufsmount *ump;
1877 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
1879 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
1880 ("softdep_move_dependencies called on non-softdep filesystem"));
1883 ump = VFSTOUFS(wk->wk_mp);
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))
1891 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1893 LIST_INSERT_AFTER(wktail, wk, wk_list);
1902 * Purge the work list of all items associated with a particular mount point.
1905 softdep_flushworklist(oldmnt, countp, td)
1906 struct mount *oldmnt;
1910 struct vnode *devvp;
1911 struct ufsmount *ump;
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
1922 ump = VFSTOUFS(oldmnt);
1923 devvp = ump->um_devvp;
1924 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1926 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1927 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1928 VOP_UNLOCK(devvp, 0);
1935 #define SU_WAITIDLE_RETRIES 20
1937 softdep_waitidle(struct mount *mp, int flags __unused)
1939 struct ufsmount *ump;
1940 struct vnode *devvp;
1945 devvp = ump->um_devvp;
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);
1963 ump->softdep_req = 0;
1964 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
1966 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1974 * Flush all vnodes and worklist items associated with a specified mount point.
1977 softdep_flushfiles(oldmnt, flags, td)
1978 struct mount *oldmnt;
1983 struct ufsmount *ump;
1986 int error, early, depcount, loopcnt, retry_flush_count, retry;
1989 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0,
1990 ("softdep_flushfiles called on non-softdep filesystem"));
1992 retry_flush_count = 3;
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.
2002 for (; loopcnt > 0; loopcnt--) {
2004 * Do another flush in case any vnodes were brought in
2005 * as part of the cleanup operations.
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)
2011 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
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.
2021 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2022 panic("softdep_flushfiles: looping");
2026 error = softdep_waitidle(oldmnt, flags);
2028 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2031 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0,
2032 ("softdep_flushfiles: !MNTK_NOINSMNTQ"));
2033 morework = oldmnt->mnt_nvnodelistsize > 0;
2035 ump = VFSTOUFS(oldmnt);
2037 for (i = 0; i < MAXQUOTAS; i++) {
2038 if (ump->um_quotas[i] != NULLVP)
2044 if (--retry_flush_count > 0) {
2050 MNT_IUNLOCK(oldmnt);
2059 * Structure hashing.
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.
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.
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.
2083 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2086 * Structures and routines associated with pagedep caching.
2088 #define PAGEDEP_HASH(ump, inum, lbn) \
2089 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2092 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2093 struct pagedep_hashhead *pagedephd;
2096 struct pagedep **pagedeppp;
2098 struct pagedep *pagedep;
2100 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2101 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2102 *pagedeppp = pagedep;
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.
2115 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2121 struct pagedep **pagedeppp;
2123 struct pagedep *pagedep;
2124 struct pagedep_hashhead *pagedephd;
2125 struct worklist *wk;
2126 struct ufsmount *ump;
2133 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2134 if (wk->wk_type == D_PAGEDEP) {
2135 *pagedeppp = WK_PAGEDEP(wk);
2140 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2141 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2143 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2144 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2147 if ((flags & DEPALLOC) == 0)
2150 pagedep = malloc(sizeof(struct pagedep),
2151 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2152 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2154 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2157 * This should never happen since we only create pagedeps
2158 * with the vnode lock held. Could be an assert.
2160 WORKITEM_FREE(pagedep, D_PAGEDEP);
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;
2176 * Structures and routines associated with inodedep caching.
2178 #define INODEDEP_HASH(ump, inum) \
2179 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2182 inodedep_find(inodedephd, inum, inodedeppp)
2183 struct inodedep_hashhead *inodedephd;
2185 struct inodedep **inodedeppp;
2187 struct inodedep *inodedep;
2189 LIST_FOREACH(inodedep, inodedephd, id_hash)
2190 if (inum == inodedep->id_ino)
2193 *inodedeppp = inodedep;
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.
2206 inodedep_lookup(mp, inum, flags, inodedeppp)
2210 struct inodedep **inodedeppp;
2212 struct inodedep *inodedep;
2213 struct inodedep_hashhead *inodedephd;
2214 struct ufsmount *ump;
2220 inodedephd = INODEDEP_HASH(ump, inum);
2222 if (inodedep_find(inodedephd, inum, inodedeppp))
2224 if ((flags & DEPALLOC) == 0)
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.
2231 if (softdep_excess_items(ump, D_INODEDEP))
2232 schedule_cleanup(mp);
2235 inodedep = malloc(sizeof(struct inodedep),
2236 M_INODEDEP, M_SOFTDEP_FLAGS);
2237 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2239 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2240 WORKITEM_FREE(inodedep, D_INODEDEP);
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;
2269 * Structures and routines associated with newblk caching.
2271 #define NEWBLK_HASH(ump, inum) \
2272 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2275 newblk_find(newblkhd, newblkno, flags, newblkpp)
2276 struct newblk_hashhead *newblkhd;
2277 ufs2_daddr_t newblkno;
2279 struct newblk **newblkpp;
2281 struct newblk *newblk;
2283 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2284 if (newblkno != newblk->nb_newblkno)
2287 * If we're creating a new dependency don't match those that
2288 * have already been converted to allocdirects. This is for
2291 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
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.
2309 newblk_lookup(mp, newblkno, flags, newblkpp)
2311 ufs2_daddr_t newblkno;
2313 struct newblk **newblkpp;
2315 struct newblk *newblk;
2316 struct newblk_hashhead *newblkhd;
2317 struct ufsmount *ump;
2321 newblkhd = NEWBLK_HASH(ump, newblkno);
2322 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2324 if ((flags & DEPALLOC) == 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);
2332 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2333 M_SOFTDEP_FLAGS | M_ZERO);
2334 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2336 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2337 WORKITEM_FREE(newblk, D_NEWBLK);
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);
2352 * Structures and routines associated with freed indirect block caching.
2354 #define INDIR_HASH(ump, blkno) \
2355 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
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.
2363 indirblk_lookup(mp, blkno)
2367 struct freework *freework;
2368 struct indir_hashhead *wkhd;
2369 struct ufsmount *ump;
2372 wkhd = INDIR_HASH(ump, blkno);
2373 TAILQ_FOREACH(freework, wkhd, fw_next) {
2374 if (freework->fw_blkno != blkno)
2376 indirblk_remove(freework);
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.
2388 indirblk_insert(freework)
2389 struct freework *freework;
2391 struct jblocks *jblocks;
2393 struct ufsmount *ump;
2395 ump = VFSTOUFS(freework->fw_list.wk_mp);
2396 jblocks = ump->softdep_jblocks;
2397 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2401 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2402 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2404 freework->fw_state &= ~DEPCOMPLETE;
2408 indirblk_remove(freework)
2409 struct freework *freework;
2411 struct ufsmount *ump;
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);
2422 * Executed during filesystem system initialization before
2423 * mounting any filesystems.
2426 softdep_initialize()
2429 TAILQ_INIT(&softdepmounts);
2431 max_softdeps = desiredvnodes * 4;
2433 max_softdeps = desiredvnodes * 2;
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;
2443 /* Initialize the callout with an mtx. */
2444 callout_init_mtx(&softdep_callout, &lk, 0);
2448 * Executed after all filesystems have been unmounted during
2449 * filesystem module unload.
2452 softdep_uninitialize()
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;
2462 callout_drain(&softdep_callout);
2466 * Called at mount time to notify the dependency code that a
2467 * filesystem wishes to use it.
2470 softdep_mount(devvp, mp, fs, cred)
2471 struct vnode *devvp;
2476 struct csum_total cstotal;
2477 struct mount_softdeps *sdp;
2478 struct ufsmount *ump;
2484 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
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;
2493 ump->um_softdep = sdp;
2495 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock");
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);
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);
2531 * Start our flushing thread in the bufdaemon process.
2534 ump->softdep_flags |= FLUSH_STARTING;
2536 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2537 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2538 mp->mnt_stat.f_mntonname);
2540 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2541 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
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.
2553 if (compute_summary_at_mount == 0 || fs->fs_clean != 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) {
2560 softdep_unmount(mp);
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;
2572 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2573 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2575 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2583 struct ufsmount *ump;
2588 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
2589 ("softdep_unmount called on non-softdep filesystem"));
2592 mp->mnt_flag &= ~MNT_SOFTDEP;
2593 if (MOUNTEDSUJ(mp) == 0) {
2596 mp->mnt_flag &= ~MNT_SUJ;
2598 journal_unmount(ump);
2601 * Shut down our flushing thread. Check for NULL is if
2602 * softdep_mount errors out before the thread has been created.
2604 if (ump->softdep_flushtd != NULL) {
2606 ump->softdep_flags |= FLUSH_EXIT;
2607 wakeup(&ump->softdep_flushtd);
2608 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP,
2610 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2611 ("Thread shutdown failed"));
2614 * Free up our resources.
2616 ACQUIRE_GBLLOCK(&lk);
2617 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next);
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);
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]));
2632 free(ump->um_softdep, M_MOUNTDATA);
2635 static struct jblocks *
2636 jblocks_create(void)
2638 struct jblocks *jblocks;
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);
2650 jblocks_alloc(jblocks, bytes, actual)
2651 struct jblocks *jblocks;
2656 struct jextent *jext;
2660 blocks = bytes / DEV_BSIZE;
2661 jext = &jblocks->jb_extent[jblocks->jb_head];
2662 freecnt = jext->je_blocks - jblocks->jb_off;
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;
2670 if (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;
2681 jblocks_free(jblocks, mp, bytes)
2682 struct jblocks *jblocks;
2687 LOCK_OWNED(VFSTOUFS(mp));
2688 jblocks->jb_free += bytes / DEV_BSIZE;
2689 if (jblocks->jb_suspended)
2690 worklist_speedup(mp);
2695 jblocks_destroy(jblocks)
2696 struct jblocks *jblocks;
2699 if (jblocks->jb_extent)
2700 free(jblocks->jb_extent, M_JBLOCKS);
2701 free(jblocks, M_JBLOCKS);
2705 jblocks_add(jblocks, daddr, blocks)
2706 struct jblocks *jblocks;
2710 struct jextent *jext;
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;
2721 /* Extending the last extent. */
2722 if (jext->je_daddr + jext->je_blocks == daddr) {
2723 jext->je_blocks += blocks;
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;
2736 jext = &jblocks->jb_extent[jblocks->jb_used];
2737 jext->je_daddr = daddr;
2738 jext->je_blocks = blocks;
2743 softdep_journal_lookup(mp, vpp)
2747 struct componentname cnp;
2752 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
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);
2767 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2772 * Open and verify the journal file.
2775 journal_mount(mp, fs, cred)
2780 struct jblocks *jblocks;
2781 struct ufsmount *ump;
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);
2797 printf("Failed to find journal. Use tunefs to create one\n");
2801 if (ip->i_size < SUJ_MIN) {
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);
2811 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
2814 jblocks_destroy(jblocks);
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;
2823 mp->mnt_flag |= MNT_SUJ;
2824 mp->mnt_flag &= ~MNT_SOFTDEP;
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.
2835 DIP_SET(ip, i_modrev, fs->fs_mtime);
2836 ip->i_flags |= IN_MODIFIED;
2845 journal_unmount(ump)
2846 struct ufsmount *ump;
2849 if (ump->softdep_jblocks)
2850 jblocks_destroy(ump->softdep_jblocks);
2851 ump->softdep_jblocks = NULL;
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
2861 struct worklist *wk;
2863 struct ufsmount *ump;
2865 ump = VFSTOUFS(wk->wk_mp);
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);
2875 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
2876 ump->softdep_journal_tail = wk;
2877 ump->softdep_on_journal += 1;
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.
2886 remove_from_journal(wk)
2887 struct worklist *wk;
2889 struct ufsmount *ump;
2891 ump = VFSTOUFS(wk->wk_mp);
2895 struct worklist *wkn;
2897 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
2901 panic("remove_from_journal: %p is not in journal", wk);
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.
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;
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.
2923 journal_space(ump, thresh)
2924 struct ufsmount *ump;
2927 struct jblocks *jblocks;
2930 jblocks = ump->softdep_jblocks;
2931 if (jblocks == NULL)
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.
2939 limit = (max_softdeps / 10) * 9;
2940 if (dep_current[D_INODEDEP] > limit &&
2941 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
2944 thresh = jblocks->jb_min;
2946 thresh = jblocks->jb_low;
2947 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
2948 avail = jblocks->jb_free - avail;
2950 return (avail > thresh);
2954 journal_suspend(ump)
2955 struct ufsmount *ump;
2957 struct jblocks *jblocks;
2961 jblocks = ump->softdep_jblocks;
2963 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
2965 mp->mnt_kern_flag |= MNTK_SUSPEND;
2966 mp->mnt_susp_owner = ump->softdep_flushtd;
2968 jblocks->jb_suspended = 1;
2973 journal_unsuspend(struct ufsmount *ump)
2975 struct jblocks *jblocks;
2979 jblocks = ump->softdep_jblocks;
2981 if (jblocks != NULL && jblocks->jb_suspended &&
2982 journal_space(ump, jblocks->jb_min)) {
2983 jblocks->jb_suspended = 0;
2985 mp->mnt_susp_owner = curthread;
2986 vfs_write_resume(mp, 0);
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.
3006 softdep_prealloc(vp, waitok)
3010 struct ufsmount *ump;
3012 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3013 ("softdep_prealloc called on non-softdep filesystem"));
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.
3021 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3022 (vp->v_vflag & VV_SYSTEM) != 0)
3024 ump = VFSTOUFS(vp->v_mount);
3026 if (journal_space(ump, 0)) {
3032 if (waitok == MNT_NOWAIT)
3035 * Attempt to sync this vnode once to flush any journal
3036 * work attached to it.
3038 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3039 ffs_syncvnode(vp, waitok, 0);
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);
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.
3060 softdep_prelink(dvp, vp)
3064 struct ufsmount *ump;
3066 ump = VFSTOUFS(dvp->v_mount);
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.
3073 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp))))
3078 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3079 ffs_syncvnode(dvp, MNT_WAIT, 0);
3081 /* Process vp before dvp as it may create .. removes. */
3083 process_removes(vp);
3084 process_truncates(vp);
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);
3098 jseg_write(ump, jseg, data)
3099 struct ufsmount *ump;
3103 struct jsegrec *rec;
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;
3111 rec->jsr_time = ump->um_fs->fs_mtime;
3115 inoref_write(inoref, jseg, rec)
3116 struct inoref *inoref;
3118 struct jrefrec *rec;
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;
3130 jaddref_write(jaddref, jseg, data)
3131 struct jaddref *jaddref;
3135 struct jrefrec *rec;
3137 rec = (struct jrefrec *)data;
3138 rec->jr_op = JOP_ADDREF;
3139 inoref_write(&jaddref->ja_ref, jseg, rec);
3143 jremref_write(jremref, jseg, data)
3144 struct jremref *jremref;
3148 struct jrefrec *rec;
3150 rec = (struct jrefrec *)data;
3151 rec->jr_op = JOP_REMREF;
3152 inoref_write(&jremref->jr_ref, jseg, rec);
3156 jmvref_write(jmvref, jseg, data)
3157 struct jmvref *jmvref;
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;
3172 jnewblk_write(jnewblk, jseg, data)
3173 struct jnewblk *jnewblk;
3177 struct jblkrec *rec;
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;
3190 jfreeblk_write(jfreeblk, jseg, data)
3191 struct jfreeblk *jfreeblk;
3195 struct jblkrec *rec;
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;
3208 jfreefrag_write(jfreefrag, jseg, data)
3209 struct jfreefrag *jfreefrag;
3213 struct jblkrec *rec;
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;
3226 jtrunc_write(jtrunc, jseg, data)
3227 struct jtrunc *jtrunc;
3231 struct jtrncrec *rec;
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;
3242 jfsync_write(jfsync, jseg, data)
3243 struct jfsync *jfsync;
3247 struct jtrncrec *rec;
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;
3257 softdep_flushjournal(mp)
3260 struct jblocks *jblocks;
3261 struct ufsmount *ump;
3263 if (MOUNTEDSUJ(mp) == 0)
3266 jblocks = ump->softdep_jblocks;
3268 while (ump->softdep_on_journal) {
3269 jblocks->jb_needseg = 1;
3270 softdep_process_journal(mp, NULL, MNT_WAIT);
3275 static void softdep_synchronize_completed(struct bio *);
3276 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3279 softdep_synchronize_completed(bp)
3282 struct jseg *oldest;
3284 struct ufsmount *ump;
3287 * caller1 marks the last segment written before we issued the
3288 * synchronize cache.
3290 jseg = bp->bio_caller1;
3295 ump = VFSTOUFS(jseg->js_list.wk_mp);
3299 * Mark all the journal entries waiting on the synchronize cache
3300 * as completed so they may continue on.
3302 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3303 jseg->js_state |= COMPLETE;
3305 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3308 * Restart deferred journal entry processing from the oldest
3312 complete_jsegs(oldest);
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.
3325 softdep_synchronize(bp, ump, caller1)
3327 struct ufsmount *ump;
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;
3336 bp->bio_done = softdep_synchronize_completed;
3337 bp->bio_caller1 = caller1;
3339 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private);
3343 * Flush some journal records to disk.
3346 softdep_process_journal(mp, needwk, flags)
3348 struct worklist *needwk;
3351 struct jblocks *jblocks;
3352 struct ufsmount *ump;
3353 struct worklist *wk;
3361 int jrecmin; /* Minimum records per block. */
3362 int jrecmax; /* Maximum records per block. */
3368 if (MOUNTEDSUJ(mp) == 0)
3370 shouldflush = softdep_flushcache;
3376 jblocks = ump->softdep_jblocks;
3377 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
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.
3383 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3384 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3387 cnt = ump->softdep_on_journal;
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
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
3399 if (cnt < (jrecmax - 1) && needwk == NULL &&
3400 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
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.
3409 while (jblocks->jb_free == 0) {
3410 if (flags != MNT_WAIT)
3412 printf("softdep: Out of journal space!\n");
3413 softdep_speedup(ump);
3414 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
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);
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
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);
3446 * Calculate the disk block size required for the available
3447 * records rounded to the min size.
3451 else if (cnt < jrecmax)
3452 size = howmany(cnt, jrecmin) * devbsize;
3454 size = fs->fs_bsize;
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.
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;
3467 * Initialize our jseg with cnt records. Assign the next
3468 * sequence number to it and link it in-order.
3470 cnt = MIN(cnt, (size / devbsize) * jrecmin);
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;
3483 * Start filling in records from the pending list.
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.
3493 jseg_write(ump, jseg, data);
3495 data = bp->b_data + off;
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
3502 if (LIST_EMPTY(&ump->softdep_journal_pending))
3503 stat_emptyjblocks++;
3505 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3509 /* Place a segment header on every device block. */
3510 if ((off % devbsize) == 0) {
3511 jseg_write(ump, jseg, data);
3513 data = bp->b_data + off;
3517 remove_from_journal(wk);
3518 wk->wk_state |= INPROGRESS;
3519 WORKLIST_INSERT(&jseg->js_entries, wk);
3520 switch (wk->wk_type) {
3522 jaddref_write(WK_JADDREF(wk), jseg, data);
3525 jremref_write(WK_JREMREF(wk), jseg, data);
3528 jmvref_write(WK_JMVREF(wk), jseg, data);
3531 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3534 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3537 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3540 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3543 jfsync_write(WK_JFSYNC(wk), jseg, data);
3546 panic("process_journal: Unknown type %s",
3547 TYPENAME(wk->wk_type));
3551 data = bp->b_data + off;
3555 /* Clear any remaining space so we don't leak kernel data */
3557 bzero(data, size - off);
3560 * Write this one buffer and continue.
3563 jblocks->jb_needseg = 0;
3564 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3566 pbgetvp(ump->um_devvp, bp);
3568 * We only do the blocking wait once we find the journal
3569 * entry we're looking for.
3571 if (needwk == NULL && flags == MNT_WAIT)
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.
3583 if (segwritten && shouldflush) {
3584 softdep_synchronize(bio, ump,
3585 TAILQ_LAST(&jblocks->jb_segs, jseglst));
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.
3593 if (flags == 0 && jblocks->jb_suspended) {
3594 if (journal_unsuspend(ump))
3597 VFS_SYNC(mp, MNT_NOWAIT);
3598 ffs_sbupdate(ump, MNT_WAIT, 0);
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.
3612 struct worklist *wk;
3613 struct jmvref *jmvref;
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) {
3627 handle_written_jaddref(WK_JADDREF(wk));
3630 handle_written_jremref(WK_JREMREF(wk));
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);
3641 handle_written_jnewblk(WK_JNEWBLK(wk));
3644 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3647 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3650 rele_jseg(jseg); /* No jsegdep. */
3651 WORKITEM_FREE(wk, D_JFSYNC);
3654 handle_written_jfreefrag(WK_JFREEFRAG(wk));
3657 panic("handle_written_jseg: Unknown type %s",
3658 TYPENAME(wk->wk_type));
3662 /* Release the self reference so the structure may be freed. */
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.
3672 complete_jsegs(jseg)
3675 struct jblocks *jblocks;
3678 jblocks = jseg->js_jblocks;
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.
3683 if (jseg != jblocks->jb_writeseg)
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);
3692 jblocks->jb_writeseg = jseg;
3694 * Attempt to free jsegs now that oldestwrseq may have advanced.
3696 free_jsegs(jblocks);
3700 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
3701 * the final completions.
3704 handle_written_jseg(jseg, bp)
3709 if (jseg->js_refs == 0)
3710 panic("handle_written_jseg: No self-reference on %p", jseg);
3711 jseg->js_state |= DEPCOMPLETE;
3713 * We'll never need this buffer again, set flags so it will be
3716 bp->b_flags |= B_INVAL | B_NOCACHE;
3718 complete_jsegs(jseg);
3721 static inline struct jsegdep *
3723 struct inoref *inoref;
3725 struct jsegdep *jsegdep;
3727 jsegdep = inoref->if_jsegdep;
3728 inoref->if_jsegdep = NULL;
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.
3739 handle_written_jremref(jremref)
3740 struct jremref *jremref;
3742 struct inodedep *inodedep;
3743 struct jsegdep *jsegdep;
3744 struct dirrem *dirrem;
3746 /* Grab the jsegdep. */
3747 jsegdep = inoref_jseg(&jremref->jr_ref);
3749 * Remove us from the inoref list.
3751 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
3753 panic("handle_written_jremref: Lost inodedep");
3754 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
3756 * Complete the dirrem.
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);
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.
3777 handle_written_jaddref(jaddref)
3778 struct jaddref *jaddref;
3780 struct jsegdep *jsegdep;
3781 struct inodedep *inodedep;
3782 struct diradd *diradd;
3783 struct mkdir *mkdir;
3785 /* Grab the jsegdep. */
3786 jsegdep = inoref_jseg(&jaddref->ja_ref);
3789 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
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;
3803 panic("handle_written_jaddref: Unknown dependency %p",
3804 jaddref->ja_diradd);
3805 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
3807 * Remove us from the inode list.
3809 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
3811 * The mkdir may be waiting on the jaddref to clear before freeing.
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);
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,
3828 free_jaddref(jaddref);
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
3839 handle_written_jnewblk(jnewblk)
3840 struct jnewblk *jnewblk;
3842 struct bmsafemap *bmsafemap;
3843 struct freefrag *freefrag;
3844 struct freework *freework;
3845 struct jsegdep *jsegdep;
3846 struct newblk *newblk;
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) {
3858 * Add the written block to the bmsafemap so it can
3859 * be notified when the bitmap is on disk.
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,
3869 jwork_insert(&newblk->nb_jwork, jsegdep);
3873 * A newblock being removed by a freefrag when replaced by
3876 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
3877 freefrag->ff_jdep = NULL;
3878 jwork_insert(&freefrag->ff_jwork, jsegdep);
3882 * A direct block was removed by truncate.
3884 freework = WK_FREEWORK(jnewblk->jn_dep);
3885 freework->fw_jnewblk = NULL;
3886 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
3889 panic("handle_written_jnewblk: Unknown type %d.",
3890 jnewblk->jn_dep->wk_type);
3892 jnewblk->jn_dep = NULL;
3893 free_jnewblk(jnewblk);
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
3903 cancel_jfreefrag(jfreefrag)
3904 struct jfreefrag *jfreefrag;
3906 struct freefrag *freefrag;
3908 if (jfreefrag->fr_jsegdep) {
3909 free_jsegdep(jfreefrag->fr_jsegdep);
3910 jfreefrag->fr_jsegdep = NULL;
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);
3920 * Free a jfreefrag when the parent freefrag is rendered obsolete.
3923 free_jfreefrag(jfreefrag)
3924 struct jfreefrag *jfreefrag;
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);
3937 * Called when the journal write for a jfreefrag completes. The parent
3938 * freefrag is added to the worklist if this completes its dependencies.
3941 handle_written_jfreefrag(jfreefrag)
3942 struct jfreefrag *jfreefrag;
3944 struct jsegdep *jsegdep;
3945 struct freefrag *freefrag;
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);
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.
3969 handle_written_jblkdep(jblkdep)
3970 struct jblkdep *jblkdep;
3972 struct freeblks *freeblks;
3973 struct jsegdep *jsegdep;
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);
3982 * If the freeblks is all journaled, we can add it to the worklist.
3984 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
3985 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
3986 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
3988 free_jblkdep(jblkdep);
3991 static struct jsegdep *
3992 newjsegdep(struct worklist *wk)
3994 struct jsegdep *jsegdep;
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;
4003 static struct jmvref *
4004 newjmvref(dp, ino, oldoff, newoff)
4010 struct jmvref *jmvref;
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;
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.
4033 static struct jremref *
4034 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4035 off_t diroff, nlink_t nlink)
4037 struct jremref *jremref;
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,
4044 jremref->jr_dirrem = dirrem;
4050 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4051 nlink_t nlink, uint16_t mode)
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;
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.
4069 static struct jaddref *
4070 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4073 struct jaddref *jaddref;
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);
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.
4090 static struct freedep *
4091 newfreedep(struct freework *freework)
4093 struct freedep *freedep;
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;
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.
4107 free_freedep(freedep)
4108 struct freedep *freedep;
4110 struct freework *freework;
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);
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().
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;
4136 struct freework *freework;
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;
4152 newjfreeblk(freeblks, lbn, nb, frags);
4153 if (parent == NULL) {
4155 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4164 * Eliminate a jfreeblk for a block that does not need journaling.
4167 cancel_jfreeblk(freeblks, blkno)
4168 struct freeblks *freeblks;
4171 struct jfreeblk *jfreeblk;
4172 struct jblkdep *jblkdep;
4174 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4175 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4177 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4178 if (jfreeblk->jf_blkno == blkno)
4181 if (jblkdep == NULL)
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);
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
4194 static struct jfreeblk *
4195 newjfreeblk(freeblks, lbn, blkno, frags)
4196 struct freeblks *freeblks;
4201 struct jfreeblk *jfreeblk;
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);
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
4229 adjust_newfreework(freeblks, frag_offset)
4230 struct freeblks *freeblks;
4233 struct jfreeblk *jfreeblk;
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"));
4239 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4240 jfreeblk->jf_blkno -= frag_offset;
4241 jfreeblk->jf_frags += frag_offset;
4245 * Allocate a new jtrunc to track a partial truncation.
4247 static struct jtrunc *
4248 newjtrunc(freeblks, size, extsize)
4249 struct freeblks *freeblks;
4253 struct jtrunc *jtrunc;
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);
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.
4274 move_newblock_dep(jaddref, inodedep)
4275 struct jaddref *jaddref;
4276 struct inodedep *inodedep;
4278 struct inoref *inoref;
4279 struct jaddref *jaddrefn;
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;
4290 if (jaddrefn == NULL)
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,
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
4310 * Returns 1 if the canceled addref requires journaling of the remove and
4314 cancel_jaddref(jaddref, inodedep, wkhd)
4315 struct jaddref *jaddref;
4316 struct inodedep *inodedep;
4317 struct workhead *wkhd;
4319 struct inoref *inoref;
4320 struct jsegdep *jsegdep;
4323 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4324 ("cancel_jaddref: Canceling complete jaddref"));
4325 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4329 if (inodedep == NULL)
4330 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4332 panic("cancel_jaddref: Lost inodedep");
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.
4339 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4340 inoref = TAILQ_NEXT(inoref, if_deps)) {
4341 if (inoref->if_state & GOINGAWAY)
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);
4356 free_jsegdep(jsegdep);
4357 if (jaddref->ja_state & DEPCOMPLETE)
4358 remove_from_journal(&jaddref->ja_list);
4360 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
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.
4367 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4368 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4370 jaddref->ja_state |= COMPLETE;
4371 free_jaddref(jaddref);
4375 * Leave the head of the list for jsegdeps for fast merging.
4377 if (LIST_FIRST(wkhd) != NULL) {
4378 jaddref->ja_state |= ONWORKLIST;
4379 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4381 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
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
4392 free_jaddref(jaddref)
4393 struct jaddref *jaddref;
4396 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
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);
4412 * Free a jremref structure once it has been written or discarded.
4415 free_jremref(jremref)
4416 struct jremref *jremref;
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);
4427 * Free a jnewblk structure.
4430 free_jnewblk(jnewblk)
4431 struct jnewblk *jnewblk;
4434 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
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);
4443 * Cancel a jnewblk which has been been made redundant by frag extension.
4446 cancel_jnewblk(jnewblk, wkhd)
4447 struct jnewblk *jnewblk;
4448 struct workhead *wkhd;
4450 struct jsegdep *jsegdep;
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);
4464 free_jsegdep(jsegdep);
4465 remove_from_journal(&jnewblk->jn_list);
4467 wake_worklist(&jnewblk->jn_list);
4468 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4472 free_jblkdep(jblkdep)
4473 struct jblkdep *jblkdep;
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);
4481 panic("free_jblkdep: Unexpected type %s",
4482 TYPENAME(jblkdep->jb_list.wk_type));
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
4491 free_jseg(jseg, jblocks)
4493 struct jblocks *jblocks;
4495 struct freework *freework;
4498 * Free freework structures that were lingering to indicate freed
4499 * indirect blocks that forced journal write ordering on reallocate.
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);
4513 * Free all jsegs that meet the criteria for being reclaimed and update
4518 struct jblocks *jblocks;
4523 * Free only those jsegs which have none allocated before them to
4524 * preserve the journal space ordering.
4526 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4528 * Only reclaim space when nothing depends on this journal
4529 * set and another set has written that it is no longer
4532 if (jseg->js_refs != 0) {
4533 jblocks->jb_oldestseg = jseg;
4536 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4538 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4541 * We can free jsegs that didn't write entries when
4542 * oldestwrseq == js_seq.
4544 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4547 free_jseg(jseg, jblocks);
4550 * If we exited the loop above we still must discover the
4551 * oldest valid segment.
4554 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4555 jseg = TAILQ_NEXT(jseg, js_next))
4556 if (jseg->js_refs != 0)
4558 jblocks->jb_oldestseg = jseg;
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.
4564 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4565 jblocks->jb_needseg = 1;
4569 * Release one reference to a jseg and free it if the count reaches 0. This
4570 * should eventually reclaim journal space as well.
4577 KASSERT(jseg->js_refs > 0,
4578 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4579 if (--jseg->js_refs != 0)
4581 free_jsegs(jseg->js_jblocks);
4585 * Release a jsegdep and decrement the jseg count.
4588 free_jsegdep(jsegdep)
4589 struct jsegdep *jsegdep;
4592 if (jsegdep->jd_seg)
4593 rele_jseg(jsegdep->jd_seg);
4594 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4598 * Wait for a journal item to make it to disk. Initiate journal processing
4603 struct worklist *wk;
4607 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4609 * Blocking journal waits cause slow synchronous behavior. Record
4610 * stats on the frequency of these blocking operations.
4612 if (waitfor == MNT_WAIT) {
4613 stat_journal_wait++;
4614 switch (wk->wk_type) {
4617 stat_jwait_filepage++;
4621 stat_jwait_freeblks++;
4624 stat_jwait_newblk++;
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.
4639 if ((wk->wk_state & INPROGRESS) == 0) {
4640 softdep_process_journal(wk->wk_mp, wk, waitfor);
4641 if (waitfor != MNT_WAIT)
4645 if (waitfor != MNT_WAIT)
4647 wait_worklist(wk, "jwait");
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.
4656 static struct inodedep *
4657 inodedep_lookup_ip(ip)
4660 struct inodedep *inodedep;
4662 KASSERT(ip->i_nlink >= ip->i_effnlink,
4663 ("inodedep_lookup_ip: bad delta"));
4664 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
4666 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4667 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
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
4679 softdep_setup_create(dp, ip)
4683 struct inodedep *inodedep;
4684 struct jaddref *jaddref;
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."));
4692 ACQUIRE_LOCK(ITOUMP(dp));
4693 inodedep = inodedep_lookup_ip(ip);
4694 if (DOINGSUJ(dvp)) {
4695 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4697 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4698 ("softdep_setup_create: No addref structure present."));
4700 softdep_prelink(dvp, NULL);
4701 FREE_LOCK(ITOUMP(dp));
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.
4711 softdep_setup_dotdot_link(dp, ip)
4715 struct inodedep *inodedep;
4716 struct jaddref *jaddref;
4719 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4720 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
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.
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);
4733 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4735 softdep_prelink(dvp, ITOV(ip));
4736 FREE_LOCK(ITOUMP(dp));
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
4746 softdep_setup_link(dp, ip)
4750 struct inodedep *inodedep;
4751 struct jaddref *jaddref;
4754 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4755 ("softdep_setup_link called on non-softdep filesystem"));
4759 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
4761 ACQUIRE_LOCK(ITOUMP(dp));
4762 inodedep = inodedep_lookup_ip(ip);
4764 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4766 softdep_prelink(dvp, ITOV(ip));
4767 FREE_LOCK(ITOUMP(dp));
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.
4777 softdep_setup_mkdir(dp, ip)
4781 struct inodedep *inodedep;
4782 struct jaddref *dotdotaddref;
4783 struct jaddref *dotaddref;
4784 struct jaddref *jaddref;
4787 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4788 ("softdep_setup_mkdir called on non-softdep filesystem"));
4790 dotaddref = dotdotaddref = NULL;
4791 if (DOINGSUJ(dvp)) {
4792 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
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;
4799 ACQUIRE_LOCK(ITOUMP(dp));
4800 inodedep = inodedep_lookup_ip(ip);
4801 if (DOINGSUJ(dvp)) {
4802 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_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,
4812 inodedep = inodedep_lookup_ip(dp);
4814 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
4815 &dotdotaddref->ja_ref, if_deps);
4816 softdep_prelink(ITOV(dp), NULL);
4817 FREE_LOCK(ITOUMP(dp));
4821 * Called to track nlinkdelta of the inode and parent directories prior to
4822 * unlinking a directory.
4825 softdep_setup_rmdir(dp, ip)
4831 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4832 ("softdep_setup_rmdir called on non-softdep filesystem"));
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));
4842 * Called to track nlinkdelta of the inode and parent directories prior to
4846 softdep_setup_unlink(dp, ip)
4852 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4853 ("softdep_setup_unlink called on non-softdep filesystem"));
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));
4863 * Called to release the journal structures created by a failed non-directory
4864 * creation. Adjusts nlinkdelta for non-journaling softdep.
4867 softdep_revert_create(dp, ip)
4871 struct inodedep *inodedep;
4872 struct jaddref *jaddref;
4875 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
4876 ("softdep_revert_create called on non-softdep filesystem"));
4878 ACQUIRE_LOCK(ITOUMP(dp));
4879 inodedep = inodedep_lookup_ip(ip);
4880 if (DOINGSUJ(dvp)) {
4881 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4883 KASSERT(jaddref->ja_parent == dp->i_number,
4884 ("softdep_revert_create: addref parent mismatch"));
4885 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4887 FREE_LOCK(ITOUMP(dp));
4891 * Called to release the journal structures created by a failed link
4892 * addition. Adjusts nlinkdelta for non-journaling softdep.
4895 softdep_revert_link(dp, ip)
4899 struct inodedep *inodedep;
4900 struct jaddref *jaddref;
4903 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4904 ("softdep_revert_link called on non-softdep filesystem"));
4906 ACQUIRE_LOCK(ITOUMP(dp));
4907 inodedep = inodedep_lookup_ip(ip);
4908 if (DOINGSUJ(dvp)) {
4909 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4911 KASSERT(jaddref->ja_parent == dp->i_number,
4912 ("softdep_revert_link: addref parent mismatch"));
4913 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4915 FREE_LOCK(ITOUMP(dp));
4919 * Called to release the journal structures created by a failed mkdir
4920 * attempt. Adjusts nlinkdelta for non-journaling softdep.
4923 softdep_revert_mkdir(dp, ip)
4927 struct inodedep *inodedep;
4928 struct jaddref *jaddref;
4929 struct jaddref *dotaddref;
4932 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4933 ("softdep_revert_mkdir called on non-softdep filesystem"));
4936 ACQUIRE_LOCK(ITOUMP(dp));
4937 inodedep = inodedep_lookup_ip(dp);
4938 if (DOINGSUJ(dvp)) {
4939 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_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);
4945 inodedep = inodedep_lookup_ip(ip);
4946 if (DOINGSUJ(dvp)) {
4947 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_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);
4958 FREE_LOCK(ITOUMP(dp));
4962 * Called to correct nlinkdelta after a failed rmdir.
4965 softdep_revert_rmdir(dp, ip)
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));
4979 * Protecting the freemaps (or bitmaps).
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
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.
5011 * Called just after updating the cylinder group block to allocate an inode.
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 */
5020 struct inodedep *inodedep;
5021 struct bmsafemap *bmsafemap;
5022 struct jaddref *jaddref;
5027 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5028 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5029 fs = VFSTOUFS(mp)->um_fs;
5033 * Allocate the journal reference add structure so that the bitmap
5034 * can be dependent on it.
5036 if (MOUNTEDSUJ(mp)) {
5037 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5038 jaddref->ja_state |= NEWBLOCK;
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.
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
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);
5067 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5068 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5071 inodedep->id_state |= ONDEPLIST;
5072 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5074 inodedep->id_bmsafemap = bmsafemap;
5075 inodedep->id_state &= ~DEPCOMPLETE;
5076 FREE_LOCK(ITOUMP(ip));
5080 * Called just after updating the cylinder group block to
5081 * allocate block or fragment.
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. */
5091 struct newblk *newblk;
5092 struct bmsafemap *bmsafemap;
5093 struct jnewblk *jnewblk;
5094 struct ufsmount *ump;
5097 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5098 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
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.
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;
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;
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,
5141 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5142 newblkno, frags, oldfrags);
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);
5149 jnewblk->jn_dep = (struct worklist *)newblk;
5150 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5152 newblk->nb_state |= ONDEPLIST;
5153 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5155 newblk->nb_bmsafemap = bmsafemap;
5156 newblk->nb_jnewblk = jnewblk;
5160 #define BMSAFEMAP_HASH(ump, cg) \
5161 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5164 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5165 struct bmsafemap_hashhead *bmsafemaphd;
5167 struct bmsafemap **bmsafemapp;
5169 struct bmsafemap *bmsafemap;
5171 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5172 if (bmsafemap->sm_cg == cg)
5175 *bmsafemapp = bmsafemap;
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.
5191 static struct bmsafemap *
5192 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5196 struct bmsafemap *newbmsafemap;
5198 struct bmsafemap_hashhead *bmsafemaphd;
5199 struct bmsafemap *bmsafemap, *collision;
5200 struct worklist *wk;
5201 struct ufsmount *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) {
5209 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5210 return (WK_BMSAFEMAP(wk));
5213 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5214 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5216 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5220 bmsafemap = newbmsafemap;
5223 bmsafemap = malloc(sizeof(struct bmsafemap),
5224 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5225 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
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);
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);
5249 * Direct block allocation dependencies.
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.
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.
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 */
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;
5299 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5300 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5301 if (oldblkno && oldblkno != newblkno)
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.
5316 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5317 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
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) {
5328 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5330 /* allocating an indirect block */
5332 panic("softdep_setup_allocdirect: non-zero indir");
5335 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5338 * Allocating a direct block.
5340 * If we are allocating a directory block, then we must
5341 * allocate an associated pagedep to track additions and
5344 if ((ip->i_mode & IFMT) == IFDIR)
5345 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
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"));
5353 * Convert the newblk to an allocdirect.
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;
5364 * Finish initializing the journal.
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);
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;
5377 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
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
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));
5400 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5401 if (oldadp->ad_offset >= off)
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);
5411 FREE_LOCK(ITOUMP(ip));
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.
5422 static struct worklist *
5423 jnewblk_merge(new, old, wkhd)
5424 struct worklist *new;
5425 struct worklist *old;
5426 struct workhead *wkhd;
5428 struct jnewblk *njnewblk;
5429 struct jnewblk *jnewblk;
5431 /* Handle NULLs to simplify callers. */
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",
5441 cancel_jfreefrag(WK_JFREEFRAG(new));
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);
5448 * Handle merging of two jnewblk records that describe
5449 * different sets of fragments in the same block.
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.");
5456 * The record may be rolled back in the cg.
5458 if (jnewblk->jn_state & UNDONE) {
5459 jnewblk->jn_state &= ~UNDONE;
5460 njnewblk->jn_state |= UNDONE;
5461 njnewblk->jn_state &= ~ATTACHED;
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.
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);
5478 * Replace an old allocdirect dependency with a newer one.
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 */
5486 struct worklist *wk;
5487 struct freefrag *freefrag;
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;
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.
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;
5523 * If we are tracking a new directory-block allocation,
5524 * move it from the old allocdirect to the new allocdirect.
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);
5532 TAILQ_REMOVE(adphead, oldadp, ad_next);
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.
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);
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);
5557 free_newblk(&oldadp->ad_block);
5561 * Allocate a jfreefrag structure to journal a single block free.
5563 static struct jfreefrag *
5564 newjfreefrag(freefrag, ip, blkno, size, lbn)
5565 struct freefrag *freefrag;
5571 struct jfreefrag *jfreefrag;
5575 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
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;
5590 * Allocate a new freefrag structure.
5592 static struct freefrag *
5593 newfreefrag(ip, blkno, size, lbn, key)
5600 struct freefrag *freefrag;
5601 struct ufsmount *ump;
5604 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5605 ip->i_number, blkno, size, lbn);
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;
5621 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5622 freefrag->ff_jdep = (struct worklist *)
5623 newjfreefrag(freefrag, ip, blkno, size, lbn);
5625 freefrag->ff_state |= DEPCOMPLETE;
5626 freefrag->ff_jdep = NULL;
5633 * This workitem de-allocates fragments that were replaced during
5634 * file block allocation.
5637 handle_workitem_freefrag(freefrag)
5638 struct freefrag *freefrag;
5640 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5641 struct workhead wkhd;
5644 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5645 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
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.
5653 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5655 * If the journal has not been written we must cancel it here.
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);
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);
5668 WORKITEM_FREE(freefrag, D_FREEFRAG);
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.
5678 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5681 ufs2_daddr_t newblkno;
5682 ufs2_daddr_t oldblkno;
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;
5694 struct ufsmount *ump;
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));
5705 if (oldblkno && oldblkno != newblkno)
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.
5720 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5721 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
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"));
5731 * Convert the newblk to an allocdirect.
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;
5743 * Finish initializing the journal.
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);
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;
5756 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
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
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);
5779 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5780 if (oldadp->ad_offset >= off)
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);
5793 * Indirect block allocation dependencies.
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.
5818 * Allocate a new allocindir structure.
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 */
5828 struct newblk *newblk;
5829 struct allocindir *aip;
5830 struct freefrag *freefrag;
5831 struct jnewblk *jnewblk;
5834 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
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;
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);
5854 if (freefrag && freefrag->ff_jdep != NULL &&
5855 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5856 add_to_journal(freefrag->ff_jdep);
5861 * Called just before setting an indirect block pointer
5862 * to a newly allocated file page.
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 */
5874 struct inodedep *inodedep;
5875 struct freefrag *freefrag;
5876 struct allocindir *aip;
5877 struct pagedep *pagedep;
5879 struct ufsmount *ump;
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));
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);
5895 * If we are allocating a directory page, then we must
5896 * allocate an associated pagedep to track additions and
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);
5905 handle_workitem_freefrag(freefrag);
5909 * Called just before setting an indirect block pointer to a
5910 * newly allocated indirect block.
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 */
5920 struct inodedep *inodedep;
5921 struct allocindir *aip;
5922 struct ufsmount *ump;
5926 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
5927 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
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");
5942 indirdep_complete(indirdep)
5943 struct indirdep *indirdep;
5945 struct allocindir *aip;
5947 LIST_REMOVE(indirdep, ir_next);
5948 indirdep->ir_state |= DEPCOMPLETE;
5950 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
5951 LIST_REMOVE(aip, ai_next);
5952 free_newblk(&aip->ai_block);
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.
5959 if ((indirdep->ir_state & ONWORKLIST) == 0)
5960 free_indirdep(indirdep);
5963 static struct indirdep *
5964 indirdep_lookup(mp, ip, bp)
5969 struct indirdep *indirdep, *newindirdep;
5970 struct newblk *newblk;
5971 struct ufsmount *ump;
5972 struct worklist *wk;
5982 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5983 if (wk->wk_type != D_INDIRDEP)
5985 indirdep = WK_INDIRDEP(wk);
5988 /* Found on the buffer worklist, no new structure to free. */
5989 if (indirdep != NULL && newindirdep == NULL)
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)
5996 /* None found and no new structure available. */
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;
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,
6013 bp->b_blkno = blkno;
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);
6023 indirdep = newindirdep;
6024 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
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.
6031 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6032 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6034 indirdep->ir_state |= DEPCOMPLETE;
6039 * Called to finish the allocation of the "aip" allocated
6040 * by one of the two routines above.
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. */
6051 struct indirdep *indirdep;
6052 struct allocindir *oldaip;
6053 struct freefrag *freefrag;
6055 struct ufsmount *ump;
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;
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.
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);
6082 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6083 if (oldaip->ai_offset == aip->ai_offset) {
6084 freefrag = allocindir_merge(aip, oldaip);
6090 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6095 * Merge two allocindirs which refer to the same block. Move newblock
6096 * dependencies and setup the freefrags appropriately.
6098 static struct freefrag *
6099 allocindir_merge(aip, oldaip)
6100 struct allocindir *aip;
6101 struct allocindir *oldaip;
6103 struct freefrag *freefrag;
6104 struct worklist *wk;
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"));
6114 * If we are tracking a new directory-block allocation,
6115 * move it from the old allocindir to the new allocindir.
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);
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.
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);
6139 setup_freedirect(freeblks, ip, i, needj)
6140 struct freeblks *freeblks;
6145 struct ufsmount *ump;
6149 blkno = DIP(ip, i_db[i]);
6152 DIP_SET(ip, i_db[i], 0);
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);
6160 setup_freeext(freeblks, ip, i, needj)
6161 struct freeblks *freeblks;
6166 struct ufsmount *ump;
6170 blkno = ip->i_din2->di_extb[i];
6173 ip->i_din2->di_extb[i] = 0;
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);
6181 setup_freeindir(freeblks, ip, i, lbn, needj)
6182 struct freeblks *freeblks;
6188 struct ufsmount *ump;
6191 blkno = DIP(ip, i_ib[i]);
6194 DIP_SET(ip, i_ib[i], 0);
6196 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6200 static inline struct freeblks *
6205 struct freeblks *freeblks;
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;
6227 trunc_indirdep(indirdep, freeblks, bp, off)
6228 struct indirdep *indirdep;
6229 struct freeblks *freeblks;
6233 struct allocindir *aip, *aipn;
6236 * The first set of allocindirs won't be in savedbp.
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);
6245 * These will exist in savedbp.
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);
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.
6262 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6263 struct freeblks *freeblks;
6269 struct indirdep *indirdep;
6270 struct indirdep *indirn;
6271 struct freework *freework;
6272 struct newblk *newblk;
6274 struct ufsmount *ump;
6287 mp = freeblks->fb_list.wk_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);
6301 racct_add_buf(curproc, bp, 0);
6302 PROC_UNLOCK(curproc);
6305 curthread->td_ru.ru_inblock++;
6306 error = bufwait(bp);
6312 level = lbn_level(lbn);
6313 lbnadd = lbn_offset(ump->um_fs, level);
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.
6318 off = (lastlbn - -(lbn + level)) / lbnadd;
6319 if (off + 1 == NINDIR(ump->um_fs))
6321 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
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.
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;
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.
6338 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6339 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6341 panic("setup_trunc_indir: lost block");
6342 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6343 trunc_indirdep(indirn, freeblks, bp, off);
6345 trunc_indirdep(indirdep, freeblks, bp, off);
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.
6352 if (indirdep->ir_saveddata == NULL)
6353 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
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];
6361 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6362 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6365 /* Zero the truncated pointers. */
6366 end = bp->b_data + bp->b_bcount;
6367 bzero(start, end - start);
6373 lbn++; /* adjust level */
6374 lbn -= (off * lbnadd);
6375 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
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.
6384 complete_trunc_indir(freework)
6385 struct freework *freework;
6387 struct freework *fwn;
6388 struct indirdep *indirdep;
6389 struct ufsmount *ump;
6394 ump = VFSTOUFS(freework->fw_list.wk_mp);
6396 indirdep = freework->fw_indir;
6398 bp = indirdep->ir_bp;
6399 /* See if the block was discarded. */
6402 /* Inline part of getdirtybuf(). We dont want bremfree. */
6403 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6405 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6406 LOCK_PTR(ump)) == 0)
6410 freework->fw_state |= DEPCOMPLETE;
6411 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6413 * Zero the pointers in the saved copy.
6415 if (indirdep->ir_state & UFS1FMT)
6416 start = sizeof(ufs1_daddr_t);
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);
6424 * We need to start the next truncation in the list if it has not
6427 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
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);
6435 * If bp is NULL the block was fully truncated, restore
6436 * the saved block list otherwise free it if it is no
6439 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
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;
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.
6454 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6455 handle_written_freework(freework);
6457 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6458 &freework->fw_list);
6460 /* Complete when the real copy is written. */
6461 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
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.
6471 blkcount(fs, datablocks, length)
6473 ufs2_daddr_t datablocks;
6476 off_t totblks, numblks;
6479 numblks = howmany(length, fs->fs_bsize);
6480 if (numblks <= UFS_NDADDR) {
6481 totblks = howmany(length, fs->fs_fsize);
6484 totblks = blkstofrags(fs, numblks);
6485 numblks -= UFS_NDADDR;
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.
6492 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6493 numblks -= NINDIR(fs);
6496 numblks = howmany(numblks, NINDIR(fs));
6499 totblks = fsbtodb(fs, totblks);
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.
6505 if (totblks > datablocks)
6507 return (datablocks - totblks);
6511 * Handle freeblocks for journaled softupdate filesystems.
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.
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.
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().
6547 softdep_journal_freeblocks(ip, cred, length, flags)
6548 struct inode *ip; /* The inode whose length is to be reduced */
6550 off_t length; /* The new length for the file */
6551 int flags; /* IO_EXT and/or IO_NORMAL */
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;
6563 ufs2_daddr_t extblocks, datablocks;
6564 ufs_lbn_t tmpval, lbn, lastlbn;
6565 int frags, lastoff, iboff, allocblock, needj, error, i;
6570 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6571 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6579 freeblks = newfreeblks(mp, ip);
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.
6586 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6587 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6590 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6591 ip->i_number, length, needj);
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.
6598 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6599 lastoff = blkoff(fs, length);
6601 * Compute frags we are keeping in lastlbn. 0 means all.
6603 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6604 frags = fragroundup(fs, lastoff);
6605 /* adp offset of last valid allocdirect. */
6607 } else if (lastlbn > 0)
6609 if (fs->fs_magic == FS_UFS2_MAGIC)
6610 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6612 * Handle normal data blocks and indirects. This section saves
6613 * values used after the inode update to complete frag and indirect
6616 if ((flags & IO_NORMAL) != 0) {
6618 * Handle truncation of whole direct and indirect blocks.
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;
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,
6631 iboff = i + UFS_NDADDR;
6633 * Traverse partially truncated indirect tree.
6635 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6636 setup_trunc_indir(freeblks, ip, -lbn - i,
6637 lastlbn, DIP(ip, i_ib[i]));
6640 * Handle partial truncation to a frag boundary.
6646 oldfrags = blksize(fs, ip, lastlbn);
6647 blkno = DIP(ip, i_db[lastlbn]);
6648 if (blkno && oldfrags != frags) {
6650 oldfrags = numfrags(fs, oldfrags);
6651 blkno += numfrags(fs, frags);
6652 newfreework(ump, freeblks, NULL, lastlbn,
6653 blkno, oldfrags, 0, needj);
6655 adjust_newfreework(freeblks,
6656 numfrags(fs, frags));
6657 } else if (blkno == 0)
6661 * Add a journal record for partial truncate if we are
6662 * handling indirect blocks. Non-indirects need no extra
6665 if (length != 0 && lastlbn >= UFS_NDADDR) {
6666 ip->i_flag |= IN_TRUNCATED;
6667 newjtrunc(freeblks, length, 0);
6669 ip->i_size = length;
6670 DIP_SET(ip, i_size, ip->i_size);
6671 datablocks = DIP(ip, i_blocks) - extblocks;
6673 datablocks = blkcount(fs, datablocks, length);
6674 freeblks->fb_len = length;
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;
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, FORCE);
6687 freeblks->fb_chkcnt = -datablocks;
6689 fs->fs_pendingblocks += datablocks;
6691 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
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().
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);
6704 softdep_error("softdep_journal_freeblocks", error);
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;
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;
6719 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6720 if ((inodedep->id_state & IOSTARTED) != 0)
6721 panic("softdep_setup_freeblocks: inode busy");
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.
6730 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
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,
6739 * Truncate the allocdirect. We could eliminate
6740 * or modify journal records as well.
6742 else if (adp->ad_offset == iboff && frags)
6743 adp->ad_newsize = frags;
6746 if ((flags & IO_EXT) != 0)
6747 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
6748 cancel_allocdirect(&inodedep->id_extupdt, adp,
6751 * Scan the bufwait list for newblock dependencies that will never
6754 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6755 if (wk->wk_type != D_ALLOCDIRECT)
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);
6768 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6769 add_to_journal(&jblkdep->jb_list);
6773 * Truncate dependency structures beyond length.
6775 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
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.
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);
6786 softdep_error("softdep_journal_freeblks", error);
6789 ip->i_size = length;
6790 DIP_SET(ip, i_size, length);
6791 ip->i_flag |= IN_CHANGE | IN_UPDATE;
6792 allocbuf(bp, frags);
6795 } else if (lastoff != 0 && vp->v_type != VDIR) {
6799 * Zero the end of a truncated frag or block.
6801 size = sblksize(fs, length, lastlbn);
6802 error = bread(vp, lastlbn, size, cred, &bp);
6804 softdep_error("softdep_journal_freeblks", error);
6807 bzero((char *)bp->b_data + lastoff, size - lastoff);
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;
6816 * We zero earlier truncations so they don't erroneously
6819 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
6820 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
6822 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
6823 LIST_EMPTY(&freeblks->fb_jblkdephd))
6824 freeblks->fb_state |= INPROGRESS;
6829 handle_workitem_freeblocks(freeblks, 0);
6830 trunc_pages(ip, length, extblocks, flags);
6835 * Flush a JOP_SYNC to the journal.
6838 softdep_journal_fsync(ip)
6841 struct jfsync *jfsync;
6842 struct ufsmount *ump;
6845 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6846 ("softdep_journal_fsync called on non-softdep filesystem"));
6847 if ((ip->i_flag & IN_TRUNCATED) == 0)
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;
6855 add_to_journal(&jfsync->jfs_list);
6856 jwait(&jfsync->jfs_list, MNT_WAIT);
6861 * Block de-allocation dependencies.
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
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.
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
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 */
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;
6903 ufs2_daddr_t extblocks, datablocks;
6905 int i, delay, error;
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"));
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) {
6921 softdep_error("softdep_setup_freeblocks", error);
6924 freeblks = newfreeblks(mp, ip);
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;
6934 i++, lbn += tmpval, tmpval *= NINDIR(fs))
6935 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
6937 DIP_SET(ip, i_size, 0);
6938 datablocks = DIP(ip, i_blocks) - extblocks;
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;
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, FORCE);
6951 freeblks->fb_chkcnt = -datablocks;
6953 fs->fs_pendingblocks += datablocks;
6955 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
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.
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;
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);
6974 * Find and eliminate any inode dependencies.
6977 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6978 if ((inodedep->id_state & IOSTARTED) != 0)
6979 panic("softdep_setup_freeblocks: inode busy");
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.
6987 delay = (inodedep->id_state & DEPCOMPLETE);
6989 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6991 freeblks->fb_state |= COMPLETE;
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.
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,
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,
7017 trunc_dependencies(ip, freeblks, -1, 0, flags);
7019 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7020 (void) free_inodedep(inodedep);
7021 freeblks->fb_state |= DEPCOMPLETE;
7023 * If the inode with zeroed block pointers is now on disk
7024 * we can start freeing blocks.
7026 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7027 freeblks->fb_state |= INPROGRESS;
7032 handle_workitem_freeblocks(freeblks, 0);
7033 trunc_pages(ip, length, extblocks, flags);
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.
7042 trunc_pages(ip, length, extblocks, flags)
7045 ufs2_daddr_t extblocks;
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)
7060 BO_LOCK(&vp->v_bufobj);
7062 BO_UNLOCK(&vp->v_bufobj);
7064 * The vnode pager eliminates file pages we eliminate indirects
7067 vnode_pager_setsize(vp, length);
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.
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));
7083 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7087 * See if the buf bp is in the range eliminated by truncation.
7090 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
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))
7104 /* ALTDATA is always a full truncation. */
7105 if ((bp->b_xflags & BX_ALTDATA) != 0)
7107 /* -1 is full truncation. */
7111 * If this is a partial truncate we only want those
7112 * blocks and indirect blocks that cover the range
7117 lbn = -(lbn + lbn_level(lbn));
7120 /* Here we only truncate lblkno if it's partial. */
7121 if (lbn == lastlbn) {
7130 * Eliminate any dependencies that exist in memory beyond lblkno:off
7133 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7135 struct freeblks *freeblks;
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
7155 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7156 bp->b_vflags &= ~BV_SCANNED;
7158 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7159 if (bp->b_vflags & BV_SCANNED)
7161 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7162 bp->b_vflags |= BV_SCANNED;
7165 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7166 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7169 if (deallocate_dependencies(bp, freeblks, blkoff))
7177 * Now do the work of vtruncbuf while also matching indirect blocks.
7179 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7180 bp->b_vflags &= ~BV_SCANNED;
7182 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7183 if (bp->b_vflags & BV_SCANNED)
7185 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7186 bp->b_vflags |= BV_SCANNED;
7190 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7191 BO_LOCKPTR(bo)) == ENOLCK) {
7195 bp->b_vflags |= BV_SCANNED;
7198 allocbuf(bp, blkoff);
7201 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7212 cancel_pagedep(pagedep, freeblks, blkoff)
7213 struct pagedep *pagedep;
7214 struct freeblks *freeblks;
7217 struct jremref *jremref;
7218 struct jmvref *jmvref;
7219 struct dirrem *dirrem, *tmp;
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.
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)
7233 * If there are any dirrems we wait for the journal write
7234 * to complete and then restart the buf scan as the lock
7237 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7238 jwait(&jremref->jr_list, MNT_WAIT);
7241 LIST_REMOVE(dirrem, dm_next);
7242 dirrem->dm_dirinum = pagedep->pd_ino;
7243 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7245 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7246 jwait(&jmvref->jm_list, MNT_WAIT);
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.
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);
7270 * There should be no directory add dependencies present
7271 * as the directory could not be truncated until all
7272 * children were removed.
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);
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.
7294 deallocate_dependencies(bp, freeblks, off)
7296 struct freeblks *freeblks;
7299 struct indirdep *indirdep;
7300 struct pagedep *pagedep;
7301 struct worklist *wk, *wkn;
7302 struct ufsmount *ump;
7304 ump = softdep_bp_to_mp(bp);
7308 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7309 switch (wk->wk_type) {
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);
7319 pagedep = WK_PAGEDEP(wk);
7320 if (cancel_pagedep(pagedep, freeblks, off)) {
7328 * Simply remove the allocindir, we'll find it via
7329 * the indirdep where we can clear pointers if
7332 WORKLIST_REMOVE(wk);
7337 * A truncation is waiting for the zero'd pointers
7338 * to be written. It can be freed when the freeblks
7341 WORKLIST_REMOVE(wk);
7342 wk->wk_state |= ONDEPLIST;
7343 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7351 panic("deallocate_dependencies: Unexpected type %s",
7352 TYPENAME(wk->wk_type));
7359 * Don't throw away this buf, we were partially truncating and
7360 * some deps may always remain.
7364 bp->b_vflags |= BV_SCANNED;
7367 bp->b_flags |= B_INVAL | B_NOCACHE;
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.
7379 cancel_allocdirect(adphead, adp, freeblks)
7380 struct allocdirectlst *adphead;
7381 struct allocdirect *adp;
7382 struct freeblks *freeblks;
7384 struct freework *freework;
7385 struct newblk *newblk;
7386 struct worklist *wk;
7388 TAILQ_REMOVE(adphead, adp, ad_next);
7389 newblk = (struct newblk *)adp;
7392 * Find the correct freework structure.
7394 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7395 if (wk->wk_type != D_FREEWORK)
7397 freework = WK_FREEWORK(wk);
7398 if (freework->fw_blkno == newblk->nb_newblkno)
7401 if (freework == NULL)
7402 panic("cancel_allocdirect: Freework not found");
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.
7407 cancel_jfreeblk(freeblks, freework->fw_blkno);
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.
7416 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7417 &freeblks->fb_jwork);
7418 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
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.
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.
7430 static struct jnewblk *
7431 cancel_newblk(newblk, wk, wkhd)
7432 struct newblk *newblk;
7433 struct worklist *wk;
7434 struct workhead *wkhd;
7436 struct jnewblk *jnewblk;
7438 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7440 newblk->nb_state |= GOINGAWAY;
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.
7450 if (newblk->nb_state & ONDEPLIST) {
7451 newblk->nb_state &= ~ONDEPLIST;
7452 LIST_REMOVE(newblk, nb_deps);
7454 if (newblk->nb_state & ONWORKLIST)
7455 WORKLIST_REMOVE(&newblk->nb_list);
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.
7461 jnewblk = newblk->nb_jnewblk;
7462 if (jnewblk != NULL && wk != NULL) {
7463 newblk->nb_jnewblk = NULL;
7464 jnewblk->jn_dep = wk;
7466 if (!LIST_EMPTY(&newblk->nb_jwork))
7467 jwork_move(wkhd, &newblk->nb_jwork);
7469 * When truncating we must free the newdirblk early to remove
7470 * the pagedep from the hash before returning.
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");
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.
7485 newblk_freefrag(newblk)
7486 struct newblk *newblk;
7488 struct freefrag *freefrag;
7490 if (newblk->nb_freefrag == NULL)
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);
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.
7506 struct newblk *newblk;
7508 struct indirdep *indirdep;
7509 struct worklist *wk;
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);
7533 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7536 free_newdirblk(newdirblk)
7537 struct newdirblk *newdirblk;
7539 struct pagedep *pagedep;
7541 struct worklist *wk;
7543 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7544 WORKLIST_REMOVE(&newdirblk->db_list);
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.
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);
7561 * If no dependencies remain, the pagedep will be freed.
7563 free_pagedep(pagedep);
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);
7570 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
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.
7578 softdep_freefile(pvp, ino, mode)
7583 struct inode *ip = VTOI(pvp);
7584 struct inodedep *inodedep;
7585 struct freefile *freefile;
7586 struct freeblks *freeblks;
7587 struct ufsmount *ump;
7590 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7591 ("softdep_freefile called on non-softdep filesystem"));
7593 * This sets up the inode de-allocation dependency.
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);
7603 ump->um_fs->fs_pendinginodes += 1;
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.
7618 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7621 * Clear out freeblks that no longer need to reference
7625 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7626 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7628 freeblks->fb_state &= ~ONDEPLIST;
7631 * Remove this inode from the unlinked list.
7633 if (inodedep->id_state & UNLINKED) {
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.
7639 handle_bufwait(inodedep, &freefile->fx_jwork);
7640 clear_unlinked_inodedep(inodedep);
7642 * Re-acquire inodedep as we've dropped the
7643 * per-filesystem lock in clear_unlinked_inodedep().
7645 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7648 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7650 handle_workitem_freefile(freefile);
7653 if ((inodedep->id_state & DEPCOMPLETE) == 0)
7654 inodedep->id_state |= GOINGAWAY;
7655 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7657 if (ip->i_number == ino)
7658 ip->i_flag |= IN_MODIFIED;
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.
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.
7676 check_inode_unwritten(inodedep)
7677 struct inodedep *inodedep;
7680 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
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)
7697 * Another process might be in initiate_write_inodeblock_ufs[12]
7698 * trying to allocate memory without holding "Softdep Lock".
7700 if ((inodedep->id_state & IOSTARTED) != 0 &&
7701 inodedep->id_savedino1 == NULL)
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;
7715 if (free_inodedep(inodedep) == 0)
7716 panic("check_inode_unwritten: busy inode");
7721 check_inodedep_free(inodedep)
7722 struct inodedep *inodedep;
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)
7745 * Try to free an inodedep structure. Return 1 if it could be freed.
7748 free_inodedep(inodedep)
7749 struct inodedep *inodedep;
7752 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7753 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7754 !check_inodedep_free(inodedep))
7756 if (inodedep->id_state & ONDEPLIST)
7757 LIST_REMOVE(inodedep, id_deps);
7758 LIST_REMOVE(inodedep, id_hash);
7759 WORKITEM_FREE(inodedep, D_INODEDEP);
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.
7771 freework_freeblock(freework, key)
7772 struct freework *freework;
7775 struct freeblks *freeblks;
7776 struct jnewblk *jnewblk;
7777 struct ufsmount *ump;
7778 struct workhead wkhd;
7783 ump = VFSTOUFS(freework->fw_list.wk_mp);
7786 * Handle partial truncate separately.
7788 if (freework->fw_indir) {
7789 complete_trunc_indir(freework);
7792 freeblks = freework->fw_freeblks;
7794 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7795 bsize = lfragtosize(fs, freework->fw_frags);
7798 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7799 * on the indirblk hashtable and prevents premature freeing.
7801 freework->fw_state |= DEPCOMPLETE;
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.
7807 if (needj && freework->fw_lbn <= -UFS_NDADDR)
7808 indirblk_insert(freework);
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.
7815 jnewblk = freework->fw_jnewblk;
7816 if (jnewblk != NULL) {
7817 cancel_jnewblk(jnewblk, &wkhd);
7820 freework->fw_state |= DELAYEDFREE;
7821 freeblks->fb_cgwait++;
7822 WORKLIST_INSERT(&wkhd, &freework->fw_list);
7825 freeblks_free(ump, freeblks, btodb(bsize));
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);
7833 * The jnewblk will be discarded and the bits in the map never
7834 * made it to disk. We can immediately free the freeblk.
7837 handle_written_freework(freework);
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().
7846 freework_enqueue(freework)
7847 struct freework *freework;
7849 struct freeblks *freeblks;
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);
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
7868 handle_workitem_indirblk(freework)
7869 struct freework *freework;
7871 struct freeblks *freeblks;
7872 struct ufsmount *ump;
7875 freeblks = freework->fw_freeblks;
7876 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7878 if (freework->fw_state & DEPCOMPLETE) {
7879 handle_written_freework(freework);
7882 if (freework->fw_off == NINDIR(fs)) {
7883 freework_freeblock(freework, SINGLETON_KEY);
7886 freework->fw_state |= INPROGRESS;
7888 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
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.
7899 handle_written_freework(freework)
7900 struct freework *freework;
7902 struct freeblks *freeblks;
7903 struct freework *parent;
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);
7913 if (--parent->fw_ref == 0)
7914 freework_enqueue(parent);
7917 if (--freeblks->fb_ref != 0)
7919 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
7920 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
7921 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
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.
7933 handle_workitem_freeblocks(freeblks, flags)
7934 struct freeblks *freeblks;
7937 struct freework *freework;
7938 struct newblk *newblk;
7939 struct allocindir *aip;
7940 struct ufsmount *ump;
7941 struct worklist *wk;
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);
7949 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
7950 WORKLIST_REMOVE(wk);
7951 switch (wk->wk_type) {
7953 wk->wk_state |= COMPLETE;
7954 add_to_worklist(wk, 0);
7958 free_newblk(WK_NEWBLK(wk));
7962 aip = WK_ALLOCINDIR(wk);
7964 if (aip->ai_state & DELAYEDFREE) {
7966 freework = newfreework(ump, freeblks, NULL,
7967 aip->ai_lbn, aip->ai_newblkno,
7968 ump->um_fs->fs_frag, 0, 0);
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;
7977 free_newblk(newblk);
7981 freework = WK_FREEWORK(wk);
7982 if (freework->fw_lbn <= -UFS_NDADDR)
7983 handle_workitem_indirblk(freework);
7985 freework_freeblock(freework, key);
7988 panic("handle_workitem_freeblocks: Unknown type %s",
7989 TYPENAME(wk->wk_type));
7992 if (freeblks->fb_ref != 0) {
7993 freeblks->fb_state &= ~INPROGRESS;
7994 wake_worklist(&freeblks->fb_list);
7998 ffs_blkrelease_finish(ump, key);
8000 return handle_complete_freeblocks(freeblks, flags);
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.
8011 freeblks_free(ump, freeblks, blocks)
8012 struct ufsmount *ump;
8013 struct freeblks *freeblks;
8017 ufs2_daddr_t remain;
8020 remain = -freeblks->fb_chkcnt;
8021 freeblks->fb_chkcnt += blocks;
8023 if (remain < blocks)
8026 fs->fs_pendingblocks -= blocks;
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.
8037 handle_complete_freeblocks(freeblks, flags)
8038 struct freeblks *freeblks;
8041 struct inodedep *inodedep;
8045 struct ufsmount *ump;
8048 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8050 flags = LK_EXCLUSIVE | flags;
8051 spare = freeblks->fb_chkcnt;
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.
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)
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;
8067 * We must wait so this happens before the
8068 * journal is reclaimed.
8076 fs->fs_pendingblocks += spare;
8082 quotaadj(freeblks->fb_quota, ump, -spare);
8083 quotarele(freeblks->fb_quota);
8086 if (freeblks->fb_state & ONDEPLIST) {
8087 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
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);
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.
8098 handle_jwork(&freeblks->fb_jwork);
8099 WORKITEM_FREE(freeblks, D_FREEBLKS);
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
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.
8116 indir_trunc(freework, dbn, lbn)
8117 struct freework *freework;
8121 struct freework *nfreework;
8122 struct workhead wkhd;
8123 struct freeblks *freeblks;
8126 struct indirdep *indirdep;
8128 struct ufsmount *ump;
8130 ufs2_daddr_t nb, nnb, *bap2;
8131 ufs_lbn_t lbnadd, nlbn;
8133 int nblocks, ufs1fmt, freedblocks;
8134 int goingaway, freedeps, needj, level, cnt, i;
8136 freeblks = freework->fw_freeblks;
8137 mp = freeblks->fb_list.wk_mp;
8141 * Get buffer of block pointers to be freed. There are three cases:
8143 * 1) Partial truncate caches the indirdep pointer in the freework
8144 * which provides us a back copy to the save bp which holds the
8145 * pointers we want to clear. When this completes the zero
8146 * pointers are written to the real copy.
8147 * 2) The indirect is being completely truncated, cancel_indirdep()
8148 * eliminated the real copy and placed the indirdep on the saved
8149 * copy. The indirdep and buf are discarded when this completes.
8150 * 3) The indirect was not in memory, we read a copy off of the disk
8151 * using the devvp and drop and invalidate the buffer when we're
8156 if (freework->fw_indir != NULL) {
8158 indirdep = freework->fw_indir;
8159 bp = indirdep->ir_savebp;
8160 if (bp == NULL || bp->b_blkno != dbn)
8161 panic("indir_trunc: Bad saved buf %p blkno %jd",
8163 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8165 * The lock prevents the buf dep list from changing and
8166 * indirects on devvp should only ever have one dependency.
8168 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8169 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8170 panic("indir_trunc: Bad indirdep %p from buf %p",
8172 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
8173 NOCRED, &bp) != 0) {
8178 /* Protects against a race with complete_trunc_indir(). */
8179 freework->fw_state &= ~INPROGRESS;
8181 * If we have an indirdep we need to enforce the truncation order
8182 * and discard it when it is complete.
8185 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8186 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8188 * Add the complete truncate to the list on the
8189 * indirdep to enforce in-order processing.
8191 if (freework->fw_indir == NULL)
8192 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8198 * If we're goingaway, free the indirdep. Otherwise it will
8199 * linger until the write completes.
8202 free_indirdep(indirdep);
8205 /* Initialize pointers depending on block size. */
8206 if (ump->um_fstype == UFS1) {
8207 bap1 = (ufs1_daddr_t *)bp->b_data;
8208 nb = bap1[freework->fw_off];
8212 bap2 = (ufs2_daddr_t *)bp->b_data;
8213 nb = bap2[freework->fw_off];
8217 level = lbn_level(lbn);
8218 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8219 lbnadd = lbn_offset(fs, level);
8220 nblocks = btodb(fs->fs_bsize);
8221 nfreework = freework;
8225 * Reclaim blocks. Traverses into nested indirect levels and
8226 * arranges for the current level to be freed when subordinates
8227 * are free when journaling.
8229 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8230 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8231 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8234 if (i != NINDIR(fs) - 1) {
8245 nlbn = (lbn + 1) - (i * lbnadd);
8247 nfreework = newfreework(ump, freeblks, freework,
8248 nlbn, nb, fs->fs_frag, 0, 0);
8251 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8253 struct freedep *freedep;
8256 * Attempt to aggregate freedep dependencies for
8257 * all blocks being released to the same CG.
8261 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8262 freedep = newfreedep(freework);
8263 WORKLIST_INSERT_UNLOCKED(&wkhd,
8268 "indir_trunc: ino %jd blkno %jd size %d",
8269 freeblks->fb_inum, nb, fs->fs_bsize);
8270 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8271 fs->fs_bsize, freeblks->fb_inum,
8272 freeblks->fb_vtype, &wkhd, key);
8275 ffs_blkrelease_finish(ump, key);
8277 bp->b_flags |= B_INVAL | B_NOCACHE;
8282 freedblocks = (nblocks * cnt);
8284 freedblocks += nblocks;
8285 freeblks_free(ump, freeblks, freedblocks);
8287 * If we are journaling set up the ref counts and offset so this
8288 * indirect can be completed when its children are free.
8292 freework->fw_off = i;
8293 freework->fw_ref += freedeps;
8294 freework->fw_ref -= NINDIR(fs) + 1;
8296 freeblks->fb_cgwait += freedeps;
8297 if (freework->fw_ref == 0)
8298 freework_freeblock(freework, SINGLETON_KEY);
8303 * If we're not journaling we can free the indirect now.
8305 dbn = dbtofsb(fs, dbn);
8307 "indir_trunc 2: ino %jd blkno %jd size %d",
8308 freeblks->fb_inum, dbn, fs->fs_bsize);
8309 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8310 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8311 /* Non SUJ softdep does single-threaded truncations. */
8312 if (freework->fw_blkno == dbn) {
8313 freework->fw_state |= ALLCOMPLETE;
8315 handle_written_freework(freework);
8322 * Cancel an allocindir when it is removed via truncation. When bp is not
8323 * NULL the indirect never appeared on disk and is scheduled to be freed
8324 * independently of the indir so we can more easily track journal work.
8327 cancel_allocindir(aip, bp, freeblks, trunc)
8328 struct allocindir *aip;
8330 struct freeblks *freeblks;
8333 struct indirdep *indirdep;
8334 struct freefrag *freefrag;
8335 struct newblk *newblk;
8337 newblk = (struct newblk *)aip;
8338 LIST_REMOVE(aip, ai_next);
8340 * We must eliminate the pointer in bp if it must be freed on its
8341 * own due to partial truncate or pending journal work.
8343 if (bp && (trunc || newblk->nb_jnewblk)) {
8345 * Clear the pointer and mark the aip to be freed
8346 * directly if it never existed on disk.
8348 aip->ai_state |= DELAYEDFREE;
8349 indirdep = aip->ai_indirdep;
8350 if (indirdep->ir_state & UFS1FMT)
8351 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8353 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8356 * When truncating the previous pointer will be freed via
8357 * savedbp. Eliminate the freefrag which would dup free.
8359 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8360 newblk->nb_freefrag = NULL;
8361 if (freefrag->ff_jdep)
8363 WK_JFREEFRAG(freefrag->ff_jdep));
8364 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8365 WORKITEM_FREE(freefrag, D_FREEFRAG);
8368 * If the journal hasn't been written the jnewblk must be passed
8369 * to the call to ffs_blkfree that reclaims the space. We accomplish
8370 * this by leaving the journal dependency on the newblk to be freed
8371 * when a freework is created in handle_workitem_freeblocks().
8373 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8374 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8378 * Create the mkdir dependencies for . and .. in a new directory. Link them
8379 * in to a newdirblk so any subsequent additions are tracked properly. The
8380 * caller is responsible for adding the mkdir1 dependency to the journal
8381 * and updating id_mkdiradd. This function returns with the per-filesystem
8384 static struct mkdir *
8385 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8389 struct buf *newdirbp;
8390 struct mkdir **mkdirp;
8392 struct newblk *newblk;
8393 struct pagedep *pagedep;
8394 struct inodedep *inodedep;
8395 struct newdirblk *newdirblk;
8396 struct mkdir *mkdir1, *mkdir2;
8397 struct worklist *wk;
8398 struct jaddref *jaddref;
8399 struct ufsmount *ump;
8402 mp = dap->da_list.wk_mp;
8404 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8406 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8407 LIST_INIT(&newdirblk->db_mkdir);
8408 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8409 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8410 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8411 mkdir1->md_diradd = dap;
8412 mkdir1->md_jaddref = NULL;
8413 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8414 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8415 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8416 mkdir2->md_diradd = dap;
8417 mkdir2->md_jaddref = NULL;
8418 if (MOUNTEDSUJ(mp) == 0) {
8419 mkdir1->md_state |= DEPCOMPLETE;
8420 mkdir2->md_state |= DEPCOMPLETE;
8423 * Dependency on "." and ".." being written to disk.
8425 mkdir1->md_buf = newdirbp;
8426 ACQUIRE_LOCK(VFSTOUFS(mp));
8427 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8429 * We must link the pagedep, allocdirect, and newdirblk for
8430 * the initial file page so the pointer to the new directory
8431 * is not written until the directory contents are live and
8432 * any subsequent additions are not marked live until the
8433 * block is reachable via the inode.
8435 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8436 panic("setup_newdir: lost pagedep");
8437 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8438 if (wk->wk_type == D_ALLOCDIRECT)
8441 panic("setup_newdir: lost allocdirect");
8442 if (pagedep->pd_state & NEWBLOCK)
8443 panic("setup_newdir: NEWBLOCK already set");
8444 newblk = WK_NEWBLK(wk);
8445 pagedep->pd_state |= NEWBLOCK;
8446 pagedep->pd_newdirblk = newdirblk;
8447 newdirblk->db_pagedep = pagedep;
8448 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8449 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8451 * Look up the inodedep for the parent directory so that we
8452 * can link mkdir2 into the pending dotdot jaddref or
8453 * the inode write if there is none. If the inode is
8454 * ALLCOMPLETE and no jaddref is present all dependencies have
8455 * been satisfied and mkdir2 can be freed.
8457 inodedep_lookup(mp, dinum, 0, &inodedep);
8458 if (MOUNTEDSUJ(mp)) {
8459 if (inodedep == NULL)
8460 panic("setup_newdir: Lost parent.");
8461 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8463 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8464 (jaddref->ja_state & MKDIR_PARENT),
8465 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8466 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8467 mkdir2->md_jaddref = jaddref;
8468 jaddref->ja_mkdir = mkdir2;
8469 } else if (inodedep == NULL ||
8470 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8471 dap->da_state &= ~MKDIR_PARENT;
8472 WORKITEM_FREE(mkdir2, D_MKDIR);
8475 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8476 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8484 * Directory entry addition dependencies.
8486 * When adding a new directory entry, the inode (with its incremented link
8487 * count) must be written to disk before the directory entry's pointer to it.
8488 * Also, if the inode is newly allocated, the corresponding freemap must be
8489 * updated (on disk) before the directory entry's pointer. These requirements
8490 * are met via undo/redo on the directory entry's pointer, which consists
8491 * simply of the inode number.
8493 * As directory entries are added and deleted, the free space within a
8494 * directory block can become fragmented. The ufs filesystem will compact
8495 * a fragmented directory block to make space for a new entry. When this
8496 * occurs, the offsets of previously added entries change. Any "diradd"
8497 * dependency structures corresponding to these entries must be updated with
8502 * This routine is called after the in-memory inode's link
8503 * count has been incremented, but before the directory entry's
8504 * pointer to the inode has been set.
8507 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8508 struct buf *bp; /* buffer containing directory block */
8509 struct inode *dp; /* inode for directory */
8510 off_t diroffset; /* offset of new entry in directory */
8511 ino_t newinum; /* inode referenced by new directory entry */
8512 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
8513 int isnewblk; /* entry is in a newly allocated block */
8515 int offset; /* offset of new entry within directory block */
8516 ufs_lbn_t lbn; /* block in directory containing new entry */
8519 struct newblk *newblk;
8520 struct pagedep *pagedep;
8521 struct inodedep *inodedep;
8522 struct newdirblk *newdirblk;
8523 struct mkdir *mkdir1, *mkdir2;
8524 struct jaddref *jaddref;
8525 struct ufsmount *ump;
8531 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8532 ("softdep_setup_directory_add called on non-softdep filesystem"));
8534 * Whiteouts have no dependencies.
8536 if (newinum == UFS_WINO) {
8537 if (newdirbp != NULL)
8542 mkdir1 = mkdir2 = NULL;
8544 lbn = lblkno(fs, diroffset);
8545 offset = blkoff(fs, diroffset);
8546 dap = malloc(sizeof(struct diradd), M_DIRADD,
8547 M_SOFTDEP_FLAGS|M_ZERO);
8548 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8549 dap->da_offset = offset;
8550 dap->da_newinum = newinum;
8551 dap->da_state = ATTACHED;
8552 LIST_INIT(&dap->da_jwork);
8553 isindir = bp->b_lblkno >= UFS_NDADDR;
8556 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8557 newdirblk = malloc(sizeof(struct newdirblk),
8558 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8559 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8560 LIST_INIT(&newdirblk->db_mkdir);
8563 * If we're creating a new directory setup the dependencies and set
8564 * the dap state to wait for them. Otherwise it's COMPLETE and
8567 if (newdirbp == NULL) {
8568 dap->da_state |= DEPCOMPLETE;
8571 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8572 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8576 * Link into parent directory pagedep to await its being written.
8578 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8580 if (diradd_lookup(pagedep, offset) != NULL)
8581 panic("softdep_setup_directory_add: %p already at off %d\n",
8582 diradd_lookup(pagedep, offset), offset);
8584 dap->da_pagedep = pagedep;
8585 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8587 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8589 * If we're journaling, link the diradd into the jaddref so it
8590 * may be completed after the journal entry is written. Otherwise,
8591 * link the diradd into its inodedep. If the inode is not yet
8592 * written place it on the bufwait list, otherwise do the post-inode
8593 * write processing to put it on the id_pendinghd list.
8595 if (MOUNTEDSUJ(mp)) {
8596 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8598 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8599 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8600 jaddref->ja_diroff = diroffset;
8601 jaddref->ja_diradd = dap;
8602 add_to_journal(&jaddref->ja_list);
8603 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8604 diradd_inode_written(dap, inodedep);
8606 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8608 * Add the journal entries for . and .. links now that the primary
8611 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8612 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8613 inoreflst, if_deps);
8614 KASSERT(jaddref != NULL &&
8615 jaddref->ja_ino == jaddref->ja_parent &&
8616 (jaddref->ja_state & MKDIR_BODY),
8617 ("softdep_setup_directory_add: bad dot jaddref %p",
8619 mkdir1->md_jaddref = jaddref;
8620 jaddref->ja_mkdir = mkdir1;
8622 * It is important that the dotdot journal entry
8623 * is added prior to the dot entry since dot writes
8624 * both the dot and dotdot links. These both must
8625 * be added after the primary link for the journal
8626 * to remain consistent.
8628 add_to_journal(&mkdir2->md_jaddref->ja_list);
8629 add_to_journal(&jaddref->ja_list);
8632 * If we are adding a new directory remember this diradd so that if
8633 * we rename it we can keep the dot and dotdot dependencies. If
8634 * we are adding a new name for an inode that has a mkdiradd we
8635 * must be in rename and we have to move the dot and dotdot
8636 * dependencies to this new name. The old name is being orphaned
8639 if (mkdir1 != NULL) {
8640 if (inodedep->id_mkdiradd != NULL)
8641 panic("softdep_setup_directory_add: Existing mkdir");
8642 inodedep->id_mkdiradd = dap;
8643 } else if (inodedep->id_mkdiradd)
8644 merge_diradd(inodedep, dap);
8645 if (newdirblk != NULL) {
8647 * There is nothing to do if we are already tracking
8650 if ((pagedep->pd_state & NEWBLOCK) != 0) {
8651 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8655 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8657 panic("softdep_setup_directory_add: lost entry");
8658 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8659 pagedep->pd_state |= NEWBLOCK;
8660 pagedep->pd_newdirblk = newdirblk;
8661 newdirblk->db_pagedep = pagedep;
8664 * If we extended into an indirect signal direnter to sync.
8675 * This procedure is called to change the offset of a directory
8676 * entry when compacting a directory block which must be owned
8677 * exclusively by the caller. Note that the actual entry movement
8678 * must be done in this procedure to ensure that no I/O completions
8679 * occur while the move is in progress.
8682 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
8683 struct buf *bp; /* Buffer holding directory block. */
8684 struct inode *dp; /* inode for directory */
8685 caddr_t base; /* address of dp->i_offset */
8686 caddr_t oldloc; /* address of old directory location */
8687 caddr_t newloc; /* address of new directory location */
8688 int entrysize; /* size of directory entry */
8690 int offset, oldoffset, newoffset;
8691 struct pagedep *pagedep;
8692 struct jmvref *jmvref;
8696 struct ufsmount *ump;
8702 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8703 ("softdep_change_directoryentry_offset called on "
8704 "non-softdep filesystem"));
8705 de = (struct direct *)oldloc;
8709 * Moves are always journaled as it would be too complex to
8710 * determine if any affected adds or removes are present in the
8713 if (MOUNTEDSUJ(mp)) {
8715 jmvref = newjmvref(dp, de->d_ino,
8716 dp->i_offset + (oldloc - base),
8717 dp->i_offset + (newloc - base));
8719 lbn = lblkno(ump->um_fs, dp->i_offset);
8720 offset = blkoff(ump->um_fs, dp->i_offset);
8721 oldoffset = offset + (oldloc - base);
8722 newoffset = offset + (newloc - base);
8724 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8726 dap = diradd_lookup(pagedep, oldoffset);
8728 dap->da_offset = newoffset;
8729 newoffset = DIRADDHASH(newoffset);
8730 oldoffset = DIRADDHASH(oldoffset);
8731 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8732 newoffset != oldoffset) {
8733 LIST_REMOVE(dap, da_pdlist);
8734 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8740 jmvref->jm_pagedep = pagedep;
8741 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8742 add_to_journal(&jmvref->jm_list);
8744 bcopy(oldloc, newloc, entrysize);
8749 * Move the mkdir dependencies and journal work from one diradd to another
8750 * when renaming a directory. The new name must depend on the mkdir deps
8751 * completing as the old name did. Directories can only have one valid link
8752 * at a time so one must be canonical.
8755 merge_diradd(inodedep, newdap)
8756 struct inodedep *inodedep;
8757 struct diradd *newdap;
8759 struct diradd *olddap;
8760 struct mkdir *mkdir, *nextmd;
8761 struct ufsmount *ump;
8764 olddap = inodedep->id_mkdiradd;
8765 inodedep->id_mkdiradd = newdap;
8766 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8767 newdap->da_state &= ~DEPCOMPLETE;
8768 ump = VFSTOUFS(inodedep->id_list.wk_mp);
8769 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8771 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8772 if (mkdir->md_diradd != olddap)
8774 mkdir->md_diradd = newdap;
8775 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8776 newdap->da_state |= state;
8777 olddap->da_state &= ~state;
8778 if ((olddap->da_state &
8779 (MKDIR_PARENT | MKDIR_BODY)) == 0)
8782 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8783 panic("merge_diradd: unfound ref");
8786 * Any mkdir related journal items are not safe to be freed until
8787 * the new name is stable.
8789 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8790 olddap->da_state |= DEPCOMPLETE;
8791 complete_diradd(olddap);
8795 * Move the diradd to the pending list when all diradd dependencies are
8799 complete_diradd(dap)
8802 struct pagedep *pagedep;
8804 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
8805 if (dap->da_state & DIRCHG)
8806 pagedep = dap->da_previous->dm_pagedep;
8808 pagedep = dap->da_pagedep;
8809 LIST_REMOVE(dap, da_pdlist);
8810 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
8815 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
8816 * add entries and conditonally journal the remove.
8819 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
8821 struct dirrem *dirrem;
8822 struct jremref *jremref;
8823 struct jremref *dotremref;
8824 struct jremref *dotdotremref;
8826 struct inodedep *inodedep;
8827 struct jaddref *jaddref;
8828 struct inoref *inoref;
8829 struct ufsmount *ump;
8830 struct mkdir *mkdir;
8833 * If no remove references were allocated we're on a non-journaled
8834 * filesystem and can skip the cancel step.
8836 if (jremref == NULL) {
8837 free_diradd(dap, NULL);
8841 * Cancel the primary name an free it if it does not require
8844 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
8845 0, &inodedep) != 0) {
8846 /* Abort the addref that reference this diradd. */
8847 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
8848 if (inoref->if_list.wk_type != D_JADDREF)
8850 jaddref = (struct jaddref *)inoref;
8851 if (jaddref->ja_diradd != dap)
8853 if (cancel_jaddref(jaddref, inodedep,
8854 &dirrem->dm_jwork) == 0) {
8855 free_jremref(jremref);
8862 * Cancel subordinate names and free them if they do not require
8865 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8866 ump = VFSTOUFS(dap->da_list.wk_mp);
8867 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
8868 if (mkdir->md_diradd != dap)
8870 if ((jaddref = mkdir->md_jaddref) == NULL)
8872 mkdir->md_jaddref = NULL;
8873 if (mkdir->md_state & MKDIR_PARENT) {
8874 if (cancel_jaddref(jaddref, NULL,
8875 &dirrem->dm_jwork) == 0) {
8876 free_jremref(dotdotremref);
8877 dotdotremref = NULL;
8880 if (cancel_jaddref(jaddref, inodedep,
8881 &dirrem->dm_jwork) == 0) {
8882 free_jremref(dotremref);
8890 journal_jremref(dirrem, jremref, inodedep);
8892 journal_jremref(dirrem, dotremref, inodedep);
8894 journal_jremref(dirrem, dotdotremref, NULL);
8895 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
8896 free_diradd(dap, &dirrem->dm_jwork);
8900 * Free a diradd dependency structure.
8903 free_diradd(dap, wkhd)
8905 struct workhead *wkhd;
8907 struct dirrem *dirrem;
8908 struct pagedep *pagedep;
8909 struct inodedep *inodedep;
8910 struct mkdir *mkdir, *nextmd;
8911 struct ufsmount *ump;
8913 ump = VFSTOUFS(dap->da_list.wk_mp);
8915 LIST_REMOVE(dap, da_pdlist);
8916 if (dap->da_state & ONWORKLIST)
8917 WORKLIST_REMOVE(&dap->da_list);
8918 if ((dap->da_state & DIRCHG) == 0) {
8919 pagedep = dap->da_pagedep;
8921 dirrem = dap->da_previous;
8922 pagedep = dirrem->dm_pagedep;
8923 dirrem->dm_dirinum = pagedep->pd_ino;
8924 dirrem->dm_state |= COMPLETE;
8925 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
8926 add_to_worklist(&dirrem->dm_list, 0);
8928 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
8930 if (inodedep->id_mkdiradd == dap)
8931 inodedep->id_mkdiradd = NULL;
8932 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8933 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8935 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8936 if (mkdir->md_diradd != dap)
8939 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
8940 LIST_REMOVE(mkdir, md_mkdirs);
8941 if (mkdir->md_state & ONWORKLIST)
8942 WORKLIST_REMOVE(&mkdir->md_list);
8943 if (mkdir->md_jaddref != NULL)
8944 panic("free_diradd: Unexpected jaddref");
8945 WORKITEM_FREE(mkdir, D_MKDIR);
8946 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
8949 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8950 panic("free_diradd: unfound ref");
8953 free_inodedep(inodedep);
8955 * Free any journal segments waiting for the directory write.
8957 handle_jwork(&dap->da_jwork);
8958 WORKITEM_FREE(dap, D_DIRADD);
8962 * Directory entry removal dependencies.
8964 * When removing a directory entry, the entry's inode pointer must be
8965 * zero'ed on disk before the corresponding inode's link count is decremented
8966 * (possibly freeing the inode for re-use). This dependency is handled by
8967 * updating the directory entry but delaying the inode count reduction until
8968 * after the directory block has been written to disk. After this point, the
8969 * inode count can be decremented whenever it is convenient.
8973 * This routine should be called immediately after removing
8974 * a directory entry. The inode's link count should not be
8975 * decremented by the calling procedure -- the soft updates
8976 * code will do this task when it is safe.
8979 softdep_setup_remove(bp, dp, ip, isrmdir)
8980 struct buf *bp; /* buffer containing directory block */
8981 struct inode *dp; /* inode for the directory being modified */
8982 struct inode *ip; /* inode for directory entry being removed */
8983 int isrmdir; /* indicates if doing RMDIR */
8985 struct dirrem *dirrem, *prevdirrem;
8986 struct inodedep *inodedep;
8987 struct ufsmount *ump;
8991 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
8992 ("softdep_setup_remove called on non-softdep filesystem"));
8994 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
8995 * newdirrem() to setup the full directory remove which requires
8998 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9000 * Add the dirrem to the inodedep's pending remove list for quick
9003 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9004 panic("softdep_setup_remove: Lost inodedep.");
9005 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9006 dirrem->dm_state |= ONDEPLIST;
9007 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9010 * If the COMPLETE flag is clear, then there were no active
9011 * entries and we want to roll back to a zeroed entry until
9012 * the new inode is committed to disk. If the COMPLETE flag is
9013 * set then we have deleted an entry that never made it to
9014 * disk. If the entry we deleted resulted from a name change,
9015 * then the old name still resides on disk. We cannot delete
9016 * its inode (returned to us in prevdirrem) until the zeroed
9017 * directory entry gets to disk. The new inode has never been
9018 * referenced on the disk, so can be deleted immediately.
9020 if ((dirrem->dm_state & COMPLETE) == 0) {
9021 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9025 if (prevdirrem != NULL)
9026 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9027 prevdirrem, dm_next);
9028 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9029 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9032 handle_workitem_remove(dirrem, 0);
9037 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9038 * pd_pendinghd list of a pagedep.
9040 static struct diradd *
9041 diradd_lookup(pagedep, offset)
9042 struct pagedep *pagedep;
9047 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9048 if (dap->da_offset == offset)
9050 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9051 if (dap->da_offset == offset)
9057 * Search for a .. diradd dependency in a directory that is being removed.
9058 * If the directory was renamed to a new parent we have a diradd rather
9059 * than a mkdir for the .. entry. We need to cancel it now before
9060 * it is found in truncate().
9062 static struct jremref *
9063 cancel_diradd_dotdot(ip, dirrem, jremref)
9065 struct dirrem *dirrem;
9066 struct jremref *jremref;
9068 struct pagedep *pagedep;
9070 struct worklist *wk;
9072 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9074 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9077 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9079 * Mark any journal work as belonging to the parent so it is freed
9080 * with the .. reference.
9082 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9083 wk->wk_state |= MKDIR_PARENT;
9088 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9089 * replace it with a dirrem/diradd pair as a result of re-parenting a
9090 * directory. This ensures that we don't simultaneously have a mkdir and
9091 * a diradd for the same .. entry.
9093 static struct jremref *
9094 cancel_mkdir_dotdot(ip, dirrem, jremref)
9096 struct dirrem *dirrem;
9097 struct jremref *jremref;
9099 struct inodedep *inodedep;
9100 struct jaddref *jaddref;
9101 struct ufsmount *ump;
9102 struct mkdir *mkdir;
9107 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9109 dap = inodedep->id_mkdiradd;
9110 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9112 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9113 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9114 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9115 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9118 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9119 if ((jaddref = mkdir->md_jaddref) != NULL) {
9120 mkdir->md_jaddref = NULL;
9121 jaddref->ja_state &= ~MKDIR_PARENT;
9122 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9123 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9124 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9125 journal_jremref(dirrem, jremref, inodedep);
9129 if (mkdir->md_state & ONWORKLIST)
9130 WORKLIST_REMOVE(&mkdir->md_list);
9131 mkdir->md_state |= ALLCOMPLETE;
9132 complete_mkdir(mkdir);
9137 journal_jremref(dirrem, jremref, inodedep)
9138 struct dirrem *dirrem;
9139 struct jremref *jremref;
9140 struct inodedep *inodedep;
9143 if (inodedep == NULL)
9144 if (inodedep_lookup(jremref->jr_list.wk_mp,
9145 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9146 panic("journal_jremref: Lost inodedep");
9147 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9148 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9149 add_to_journal(&jremref->jr_list);
9153 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9154 struct dirrem *dirrem;
9155 struct jremref *jremref;
9156 struct jremref *dotremref;
9157 struct jremref *dotdotremref;
9159 struct inodedep *inodedep;
9162 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9164 panic("dirrem_journal: Lost inodedep");
9165 journal_jremref(dirrem, jremref, inodedep);
9167 journal_jremref(dirrem, dotremref, inodedep);
9169 journal_jremref(dirrem, dotdotremref, NULL);
9173 * Allocate a new dirrem if appropriate and return it along with
9174 * its associated pagedep. Called without a lock, returns with lock.
9176 static struct dirrem *
9177 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9178 struct buf *bp; /* buffer containing directory block */
9179 struct inode *dp; /* inode for the directory being modified */
9180 struct inode *ip; /* inode for directory entry being removed */
9181 int isrmdir; /* indicates if doing RMDIR */
9182 struct dirrem **prevdirremp; /* previously referenced inode, if any */
9187 struct dirrem *dirrem;
9188 struct pagedep *pagedep;
9189 struct jremref *jremref;
9190 struct jremref *dotremref;
9191 struct jremref *dotdotremref;
9193 struct ufsmount *ump;
9196 * Whiteouts have no deletion dependencies.
9199 panic("newdirrem: whiteout");
9204 * If the system is over its limit and our filesystem is
9205 * responsible for more than our share of that usage and
9206 * we are not a snapshot, request some inodedep cleanup.
9207 * Limiting the number of dirrem structures will also limit
9208 * the number of freefile and freeblks structures.
9211 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9212 schedule_cleanup(UFSTOVFS(ump));
9215 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9217 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9218 LIST_INIT(&dirrem->dm_jremrefhd);
9219 LIST_INIT(&dirrem->dm_jwork);
9220 dirrem->dm_state = isrmdir ? RMDIR : 0;
9221 dirrem->dm_oldinum = ip->i_number;
9222 *prevdirremp = NULL;
9224 * Allocate remove reference structures to track journal write
9225 * dependencies. We will always have one for the link and
9226 * when doing directories we will always have one more for dot.
9227 * When renaming a directory we skip the dotdot link change so
9228 * this is not needed.
9230 jremref = dotremref = dotdotremref = NULL;
9231 if (DOINGSUJ(dvp)) {
9233 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9234 ip->i_effnlink + 2);
9235 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9236 ip->i_effnlink + 1);
9237 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9238 dp->i_effnlink + 1);
9239 dotdotremref->jr_state |= MKDIR_PARENT;
9241 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9242 ip->i_effnlink + 1);
9245 lbn = lblkno(ump->um_fs, dp->i_offset);
9246 offset = blkoff(ump->um_fs, dp->i_offset);
9247 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9249 dirrem->dm_pagedep = pagedep;
9250 dirrem->dm_offset = offset;
9252 * If we're renaming a .. link to a new directory, cancel any
9253 * existing MKDIR_PARENT mkdir. If it has already been canceled
9254 * the jremref is preserved for any potential diradd in this
9255 * location. This can not coincide with a rmdir.
9257 if (dp->i_offset == DOTDOT_OFFSET) {
9259 panic("newdirrem: .. directory change during remove?");
9260 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9263 * If we're removing a directory search for the .. dependency now and
9264 * cancel it. Any pending journal work will be added to the dirrem
9265 * to be completed when the workitem remove completes.
9268 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9270 * Check for a diradd dependency for the same directory entry.
9271 * If present, then both dependencies become obsolete and can
9274 dap = diradd_lookup(pagedep, offset);
9277 * Link the jremref structures into the dirrem so they are
9278 * written prior to the pagedep.
9281 dirrem_journal(dirrem, jremref, dotremref,
9286 * Must be ATTACHED at this point.
9288 if ((dap->da_state & ATTACHED) == 0)
9289 panic("newdirrem: not ATTACHED");
9290 if (dap->da_newinum != ip->i_number)
9291 panic("newdirrem: inum %ju should be %ju",
9292 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9294 * If we are deleting a changed name that never made it to disk,
9295 * then return the dirrem describing the previous inode (which
9296 * represents the inode currently referenced from this entry on disk).
9298 if ((dap->da_state & DIRCHG) != 0) {
9299 *prevdirremp = dap->da_previous;
9300 dap->da_state &= ~DIRCHG;
9301 dap->da_pagedep = pagedep;
9304 * We are deleting an entry that never made it to disk.
9305 * Mark it COMPLETE so we can delete its inode immediately.
9307 dirrem->dm_state |= COMPLETE;
9308 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9311 struct worklist *wk;
9313 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9314 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9315 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9323 * Directory entry change dependencies.
9325 * Changing an existing directory entry requires that an add operation
9326 * be completed first followed by a deletion. The semantics for the addition
9327 * are identical to the description of adding a new entry above except
9328 * that the rollback is to the old inode number rather than zero. Once
9329 * the addition dependency is completed, the removal is done as described
9330 * in the removal routine above.
9334 * This routine should be called immediately after changing
9335 * a directory entry. The inode's link count should not be
9336 * decremented by the calling procedure -- the soft updates
9337 * code will perform this task when it is safe.
9340 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9341 struct buf *bp; /* buffer containing directory block */
9342 struct inode *dp; /* inode for the directory being modified */
9343 struct inode *ip; /* inode for directory entry being removed */
9344 ino_t newinum; /* new inode number for changed entry */
9345 int isrmdir; /* indicates if doing RMDIR */
9348 struct diradd *dap = NULL;
9349 struct dirrem *dirrem, *prevdirrem;
9350 struct pagedep *pagedep;
9351 struct inodedep *inodedep;
9352 struct jaddref *jaddref;
9354 struct ufsmount *ump;
9358 offset = blkoff(ump->um_fs, dp->i_offset);
9359 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9360 ("softdep_setup_directory_change called on non-softdep filesystem"));
9363 * Whiteouts do not need diradd dependencies.
9365 if (newinum != UFS_WINO) {
9366 dap = malloc(sizeof(struct diradd),
9367 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9368 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9369 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9370 dap->da_offset = offset;
9371 dap->da_newinum = newinum;
9372 LIST_INIT(&dap->da_jwork);
9376 * Allocate a new dirrem and ACQUIRE_LOCK.
9378 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9379 pagedep = dirrem->dm_pagedep;
9381 * The possible values for isrmdir:
9382 * 0 - non-directory file rename
9383 * 1 - directory rename within same directory
9384 * inum - directory rename to new directory of given inode number
9385 * When renaming to a new directory, we are both deleting and
9386 * creating a new directory entry, so the link count on the new
9387 * directory should not change. Thus we do not need the followup
9388 * dirrem which is usually done in handle_workitem_remove. We set
9389 * the DIRCHG flag to tell handle_workitem_remove to skip the
9393 dirrem->dm_state |= DIRCHG;
9396 * Whiteouts have no additional dependencies,
9397 * so just put the dirrem on the correct list.
9399 if (newinum == UFS_WINO) {
9400 if ((dirrem->dm_state & COMPLETE) == 0) {
9401 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9404 dirrem->dm_dirinum = pagedep->pd_ino;
9405 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9406 add_to_worklist(&dirrem->dm_list, 0);
9412 * Add the dirrem to the inodedep's pending remove list for quick
9413 * discovery later. A valid nlinkdelta ensures that this lookup
9416 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9417 panic("softdep_setup_directory_change: Lost inodedep.");
9418 dirrem->dm_state |= ONDEPLIST;
9419 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9422 * If the COMPLETE flag is clear, then there were no active
9423 * entries and we want to roll back to the previous inode until
9424 * the new inode is committed to disk. If the COMPLETE flag is
9425 * set, then we have deleted an entry that never made it to disk.
9426 * If the entry we deleted resulted from a name change, then the old
9427 * inode reference still resides on disk. Any rollback that we do
9428 * needs to be to that old inode (returned to us in prevdirrem). If
9429 * the entry we deleted resulted from a create, then there is
9430 * no entry on the disk, so we want to roll back to zero rather
9431 * than the uncommitted inode. In either of the COMPLETE cases we
9432 * want to immediately free the unwritten and unreferenced inode.
9434 if ((dirrem->dm_state & COMPLETE) == 0) {
9435 dap->da_previous = dirrem;
9437 if (prevdirrem != NULL) {
9438 dap->da_previous = prevdirrem;
9440 dap->da_state &= ~DIRCHG;
9441 dap->da_pagedep = pagedep;
9443 dirrem->dm_dirinum = pagedep->pd_ino;
9444 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9445 add_to_worklist(&dirrem->dm_list, 0);
9448 * Lookup the jaddref for this journal entry. We must finish
9449 * initializing it and make the diradd write dependent on it.
9450 * If we're not journaling, put it on the id_bufwait list if the
9451 * inode is not yet written. If it is written, do the post-inode
9452 * write processing to put it on the id_pendinghd list.
9454 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9455 if (MOUNTEDSUJ(mp)) {
9456 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9458 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9459 ("softdep_setup_directory_change: bad jaddref %p",
9461 jaddref->ja_diroff = dp->i_offset;
9462 jaddref->ja_diradd = dap;
9463 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9465 add_to_journal(&jaddref->ja_list);
9466 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9467 dap->da_state |= COMPLETE;
9468 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9469 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9471 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9473 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9476 * If we're making a new name for a directory that has not been
9477 * committed when need to move the dot and dotdot references to
9480 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET)
9481 merge_diradd(inodedep, dap);
9486 * Called whenever the link count on an inode is changed.
9487 * It creates an inode dependency so that the new reference(s)
9488 * to the inode cannot be committed to disk until the updated
9489 * inode has been written.
9492 softdep_change_linkcnt(ip)
9493 struct inode *ip; /* the inode with the increased link count */
9495 struct inodedep *inodedep;
9496 struct ufsmount *ump;
9499 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9500 ("softdep_change_linkcnt called on non-softdep filesystem"));
9502 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9503 if (ip->i_nlink < ip->i_effnlink)
9504 panic("softdep_change_linkcnt: bad delta");
9505 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9510 * Attach a sbdep dependency to the superblock buf so that we can keep
9511 * track of the head of the linked list of referenced but unlinked inodes.
9514 softdep_setup_sbupdate(ump, fs, bp)
9515 struct ufsmount *ump;
9519 struct sbdep *sbdep;
9520 struct worklist *wk;
9522 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9523 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9524 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9525 if (wk->wk_type == D_SBDEP)
9529 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9530 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9532 sbdep->sb_ump = ump;
9534 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9539 * Return the first unlinked inodedep which is ready to be the head of the
9540 * list. The inodedep and all those after it must have valid next pointers.
9542 static struct inodedep *
9543 first_unlinked_inodedep(ump)
9544 struct ufsmount *ump;
9546 struct inodedep *inodedep;
9547 struct inodedep *idp;
9550 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9551 inodedep; inodedep = idp) {
9552 if ((inodedep->id_state & UNLINKNEXT) == 0)
9554 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9555 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9557 if ((inodedep->id_state & UNLINKPREV) == 0)
9564 * Set the sujfree unlinked head pointer prior to writing a superblock.
9567 initiate_write_sbdep(sbdep)
9568 struct sbdep *sbdep;
9570 struct inodedep *inodedep;
9574 bpfs = sbdep->sb_fs;
9575 fs = sbdep->sb_ump->um_fs;
9576 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9578 fs->fs_sujfree = inodedep->id_ino;
9579 inodedep->id_state |= UNLINKPREV;
9582 bpfs->fs_sujfree = fs->fs_sujfree;
9586 * After a superblock is written determine whether it must be written again
9587 * due to a changing unlinked list head.
9590 handle_written_sbdep(sbdep, bp)
9591 struct sbdep *sbdep;
9594 struct inodedep *inodedep;
9597 LOCK_OWNED(sbdep->sb_ump);
9600 * If the superblock doesn't match the in-memory list start over.
9602 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9603 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9604 (inodedep == NULL && fs->fs_sujfree != 0)) {
9608 WORKITEM_FREE(sbdep, D_SBDEP);
9609 if (fs->fs_sujfree == 0)
9612 * Now that we have a record of this inode in stable store allow it
9613 * to be written to free up pending work. Inodes may see a lot of
9614 * write activity after they are unlinked which we must not hold up.
9616 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9617 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9618 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9619 inodedep, inodedep->id_state);
9620 if (inodedep->id_state & UNLINKONLIST)
9622 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9629 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9632 unlinked_inodedep(mp, inodedep)
9634 struct inodedep *inodedep;
9636 struct ufsmount *ump;
9640 if (MOUNTEDSUJ(mp) == 0)
9642 ump->um_fs->fs_fmod = 1;
9643 if (inodedep->id_state & UNLINKED)
9644 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9645 inodedep->id_state |= UNLINKED;
9646 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9650 * Remove an inodedep from the unlinked inodedep list. This may require
9651 * disk writes if the inode has made it that far.
9654 clear_unlinked_inodedep(inodedep)
9655 struct inodedep *inodedep;
9657 struct ufs2_dinode *dip;
9658 struct ufsmount *ump;
9659 struct inodedep *idp;
9660 struct inodedep *idn;
9668 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9670 ino = inodedep->id_ino;
9674 KASSERT((inodedep->id_state & UNLINKED) != 0,
9675 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9678 * If nothing has yet been written simply remove us from
9679 * the in memory list and return. This is the most common
9680 * case where handle_workitem_remove() loses the final
9683 if ((inodedep->id_state & UNLINKLINKS) == 0)
9686 * If we have a NEXT pointer and no PREV pointer we can simply
9687 * clear NEXT's PREV and remove ourselves from the list. Be
9688 * careful not to clear PREV if the superblock points at
9691 idn = TAILQ_NEXT(inodedep, id_unlinked);
9692 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9693 if (idn && fs->fs_sujfree != idn->id_ino)
9694 idn->id_state &= ~UNLINKPREV;
9698 * Here we have an inodedep which is actually linked into
9699 * the list. We must remove it by forcing a write to the
9700 * link before us, whether it be the superblock or an inode.
9701 * Unfortunately the list may change while we're waiting
9702 * on the buf lock for either resource so we must loop until
9703 * we lock the right one. If both the superblock and an
9704 * inode point to this inode we must clear the inode first
9705 * followed by the superblock.
9707 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9709 if (idp && (idp->id_state & UNLINKNEXT))
9713 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9714 (int)fs->fs_sbsize, 0, 0, 0);
9716 error = bread(ump->um_devvp,
9717 fsbtodb(fs, ino_to_fsba(fs, pino)),
9718 (int)fs->fs_bsize, NOCRED, &bp);
9725 /* If the list has changed restart the loop. */
9726 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9728 if (idp && (idp->id_state & UNLINKNEXT))
9731 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9738 idn = TAILQ_NEXT(inodedep, id_unlinked);
9742 * Remove us from the in memory list. After this we cannot
9743 * access the inodedep.
9745 KASSERT((inodedep->id_state & UNLINKED) != 0,
9746 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9748 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9749 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9752 * The predecessor's next pointer is manually updated here
9753 * so that the NEXT flag is never cleared for an element
9754 * that is in the list.
9757 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9758 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
9759 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data,
9761 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
9762 ((struct ufs1_dinode *)bp->b_data +
9763 ino_to_fsbo(fs, pino))->di_freelink = nino;
9765 dip = (struct ufs2_dinode *)bp->b_data +
9766 ino_to_fsbo(fs, pino);
9767 dip->di_freelink = nino;
9768 ffs_update_dinode_ckhash(fs, dip);
9771 * If the bwrite fails we have no recourse to recover. The
9772 * filesystem is corrupted already.
9777 * If the superblock pointer still needs to be cleared force
9780 if (fs->fs_sujfree == ino) {
9782 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9783 (int)fs->fs_sbsize, 0, 0, 0);
9784 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9785 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
9786 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data,
9792 if (fs->fs_sujfree != ino)
9794 panic("clear_unlinked_inodedep: Failed to clear free head");
9796 if (inodedep->id_ino == fs->fs_sujfree)
9797 panic("clear_unlinked_inodedep: Freeing head of free list");
9798 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9799 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9804 * This workitem decrements the inode's link count.
9805 * If the link count reaches zero, the file is removed.
9808 handle_workitem_remove(dirrem, flags)
9809 struct dirrem *dirrem;
9812 struct inodedep *inodedep;
9813 struct workhead dotdotwk;
9814 struct worklist *wk;
9815 struct ufsmount *ump;
9821 if (dirrem->dm_state & ONWORKLIST)
9822 panic("handle_workitem_remove: dirrem %p still on worklist",
9824 oldinum = dirrem->dm_oldinum;
9825 mp = dirrem->dm_list.wk_mp;
9827 flags |= LK_EXCLUSIVE;
9828 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0)
9832 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
9833 panic("handle_workitem_remove: lost inodedep");
9834 if (dirrem->dm_state & ONDEPLIST)
9835 LIST_REMOVE(dirrem, dm_inonext);
9836 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
9837 ("handle_workitem_remove: Journal entries not written."));
9840 * Move all dependencies waiting on the remove to complete
9841 * from the dirrem to the inode inowait list to be completed
9842 * after the inode has been updated and written to disk. Any
9843 * marked MKDIR_PARENT are saved to be completed when the .. ref
9846 LIST_INIT(&dotdotwk);
9847 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
9848 WORKLIST_REMOVE(wk);
9849 if (wk->wk_state & MKDIR_PARENT) {
9850 wk->wk_state &= ~MKDIR_PARENT;
9851 WORKLIST_INSERT(&dotdotwk, wk);
9854 WORKLIST_INSERT(&inodedep->id_inowait, wk);
9856 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
9858 * Normal file deletion.
9860 if ((dirrem->dm_state & RMDIR) == 0) {
9862 DIP_SET(ip, i_nlink, ip->i_nlink);
9863 ip->i_flag |= IN_CHANGE;
9864 if (ip->i_nlink < ip->i_effnlink)
9865 panic("handle_workitem_remove: bad file delta");
9866 if (ip->i_nlink == 0)
9867 unlinked_inodedep(mp, inodedep);
9868 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9869 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9870 ("handle_workitem_remove: worklist not empty. %s",
9871 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
9872 WORKITEM_FREE(dirrem, D_DIRREM);
9877 * Directory deletion. Decrement reference count for both the
9878 * just deleted parent directory entry and the reference for ".".
9879 * Arrange to have the reference count on the parent decremented
9880 * to account for the loss of "..".
9883 DIP_SET(ip, i_nlink, ip->i_nlink);
9884 ip->i_flag |= IN_CHANGE;
9885 if (ip->i_nlink < ip->i_effnlink)
9886 panic("handle_workitem_remove: bad dir delta");
9887 if (ip->i_nlink == 0)
9888 unlinked_inodedep(mp, inodedep);
9889 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9891 * Rename a directory to a new parent. Since, we are both deleting
9892 * and creating a new directory entry, the link count on the new
9893 * directory should not change. Thus we skip the followup dirrem.
9895 if (dirrem->dm_state & DIRCHG) {
9896 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9897 ("handle_workitem_remove: DIRCHG and worklist not empty."));
9898 WORKITEM_FREE(dirrem, D_DIRREM);
9902 dirrem->dm_state = ONDEPLIST;
9903 dirrem->dm_oldinum = dirrem->dm_dirinum;
9905 * Place the dirrem on the parent's diremhd list.
9907 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
9908 panic("handle_workitem_remove: lost dir inodedep");
9909 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9911 * If the allocated inode has never been written to disk, then
9912 * the on-disk inode is zero'ed and we can remove the file
9913 * immediately. When journaling if the inode has been marked
9914 * unlinked and not DEPCOMPLETE we know it can never be written.
9916 inodedep_lookup(mp, oldinum, 0, &inodedep);
9917 if (inodedep == NULL ||
9918 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
9919 check_inode_unwritten(inodedep)) {
9922 return handle_workitem_remove(dirrem, flags);
9924 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
9926 ip->i_flag |= IN_CHANGE;
9934 * Inode de-allocation dependencies.
9936 * When an inode's link count is reduced to zero, it can be de-allocated. We
9937 * found it convenient to postpone de-allocation until after the inode is
9938 * written to disk with its new link count (zero). At this point, all of the
9939 * on-disk inode's block pointers are nullified and, with careful dependency
9940 * list ordering, all dependencies related to the inode will be satisfied and
9941 * the corresponding dependency structures de-allocated. So, if/when the
9942 * inode is reused, there will be no mixing of old dependencies with new
9943 * ones. This artificial dependency is set up by the block de-allocation
9944 * procedure above (softdep_setup_freeblocks) and completed by the
9945 * following procedure.
9948 handle_workitem_freefile(freefile)
9949 struct freefile *freefile;
9951 struct workhead wkhd;
9953 struct ufsmount *ump;
9956 struct inodedep *idp;
9959 ump = VFSTOUFS(freefile->fx_list.wk_mp);
9963 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
9966 panic("handle_workitem_freefile: inodedep %p survived", idp);
9969 fs->fs_pendinginodes -= 1;
9972 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
9973 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
9974 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
9975 softdep_error("handle_workitem_freefile", error);
9977 WORKITEM_FREE(freefile, D_FREEFILE);
9983 * Helper function which unlinks marker element from work list and returns
9984 * the next element on the list.
9986 static __inline struct worklist *
9987 markernext(struct worklist *marker)
9989 struct worklist *next;
9991 next = LIST_NEXT(marker, wk_list);
9992 LIST_REMOVE(marker, wk_list);
9999 * The dependency structures constructed above are most actively used when file
10000 * system blocks are written to disk. No constraints are placed on when a
10001 * block can be written, but unsatisfied update dependencies are made safe by
10002 * modifying (or replacing) the source memory for the duration of the disk
10003 * write. When the disk write completes, the memory block is again brought
10006 * In-core inode structure reclamation.
10008 * Because there are a finite number of "in-core" inode structures, they are
10009 * reused regularly. By transferring all inode-related dependencies to the
10010 * in-memory inode block and indexing them separately (via "inodedep"s), we
10011 * can allow "in-core" inode structures to be reused at any time and avoid
10012 * any increase in contention.
10014 * Called just before entering the device driver to initiate a new disk I/O.
10015 * The buffer must be locked, thus, no I/O completion operations can occur
10016 * while we are manipulating its associated dependencies.
10019 softdep_disk_io_initiation(bp)
10020 struct buf *bp; /* structure describing disk write to occur */
10022 struct worklist *wk;
10023 struct worklist marker;
10024 struct inodedep *inodedep;
10025 struct freeblks *freeblks;
10026 struct jblkdep *jblkdep;
10027 struct newblk *newblk;
10028 struct ufsmount *ump;
10031 * We only care about write operations. There should never
10032 * be dependencies for reads.
10034 if (bp->b_iocmd != BIO_WRITE)
10035 panic("softdep_disk_io_initiation: not write");
10037 if (bp->b_vflags & BV_BKGRDINPROG)
10038 panic("softdep_disk_io_initiation: Writing buffer with "
10039 "background write in progress: %p", bp);
10041 ump = softdep_bp_to_mp(bp);
10045 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10046 PHOLD(curproc); /* Don't swap out kernel stack */
10049 * Do any necessary pre-I/O processing.
10051 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10052 wk = markernext(&marker)) {
10053 LIST_INSERT_AFTER(wk, &marker, wk_list);
10054 switch (wk->wk_type) {
10057 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10061 inodedep = WK_INODEDEP(wk);
10062 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10063 initiate_write_inodeblock_ufs1(inodedep, bp);
10065 initiate_write_inodeblock_ufs2(inodedep, bp);
10069 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10073 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10077 WK_JSEG(wk)->js_buf = NULL;
10081 freeblks = WK_FREEBLKS(wk);
10082 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10084 * We have to wait for the freeblks to be journaled
10085 * before we can write an inodeblock with updated
10086 * pointers. Be careful to arrange the marker so
10087 * we revisit the freeblks if it's not removed by
10088 * the first jwait().
10090 if (jblkdep != NULL) {
10091 LIST_REMOVE(&marker, wk_list);
10092 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10093 jwait(&jblkdep->jb_list, MNT_WAIT);
10096 case D_ALLOCDIRECT:
10099 * We have to wait for the jnewblk to be journaled
10100 * before we can write to a block if the contents
10101 * may be confused with an earlier file's indirect
10102 * at recovery time. Handle the marker as described
10105 newblk = WK_NEWBLK(wk);
10106 if (newblk->nb_jnewblk != NULL &&
10107 indirblk_lookup(newblk->nb_list.wk_mp,
10108 newblk->nb_newblkno)) {
10109 LIST_REMOVE(&marker, wk_list);
10110 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10111 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10116 initiate_write_sbdep(WK_SBDEP(wk));
10126 panic("handle_disk_io_initiation: Unexpected type %s",
10127 TYPENAME(wk->wk_type));
10132 PRELE(curproc); /* Allow swapout of kernel stack */
10136 * Called from within the procedure above to deal with unsatisfied
10137 * allocation dependencies in a directory. The buffer must be locked,
10138 * thus, no I/O completion operations can occur while we are
10139 * manipulating its associated dependencies.
10142 initiate_write_filepage(pagedep, bp)
10143 struct pagedep *pagedep;
10146 struct jremref *jremref;
10147 struct jmvref *jmvref;
10148 struct dirrem *dirrem;
10149 struct diradd *dap;
10153 if (pagedep->pd_state & IOSTARTED) {
10155 * This can only happen if there is a driver that does not
10156 * understand chaining. Here biodone will reissue the call
10157 * to strategy for the incomplete buffers.
10159 printf("initiate_write_filepage: already started\n");
10162 pagedep->pd_state |= IOSTARTED;
10164 * Wait for all journal remove dependencies to hit the disk.
10165 * We can not allow any potentially conflicting directory adds
10166 * to be visible before removes and rollback is too difficult.
10167 * The per-filesystem lock may be dropped and re-acquired, however
10168 * we hold the buf locked so the dependency can not go away.
10170 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10171 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10172 jwait(&jremref->jr_list, MNT_WAIT);
10173 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10174 jwait(&jmvref->jm_list, MNT_WAIT);
10175 for (i = 0; i < DAHASHSZ; i++) {
10176 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10177 ep = (struct direct *)
10178 ((char *)bp->b_data + dap->da_offset);
10179 if (ep->d_ino != dap->da_newinum)
10180 panic("%s: dir inum %ju != new %ju",
10181 "initiate_write_filepage",
10182 (uintmax_t)ep->d_ino,
10183 (uintmax_t)dap->da_newinum);
10184 if (dap->da_state & DIRCHG)
10185 ep->d_ino = dap->da_previous->dm_oldinum;
10188 dap->da_state &= ~ATTACHED;
10189 dap->da_state |= UNDONE;
10195 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10196 * Note that any bug fixes made to this routine must be done in the
10197 * version found below.
10199 * Called from within the procedure above to deal with unsatisfied
10200 * allocation dependencies in an inodeblock. The buffer must be
10201 * locked, thus, no I/O completion operations can occur while we
10202 * are manipulating its associated dependencies.
10205 initiate_write_inodeblock_ufs1(inodedep, bp)
10206 struct inodedep *inodedep;
10207 struct buf *bp; /* The inode block */
10209 struct allocdirect *adp, *lastadp;
10210 struct ufs1_dinode *dp;
10211 struct ufs1_dinode *sip;
10212 struct inoref *inoref;
10213 struct ufsmount *ump;
10217 ufs_lbn_t prevlbn = 0;
10221 if (inodedep->id_state & IOSTARTED)
10222 panic("initiate_write_inodeblock_ufs1: already started");
10223 inodedep->id_state |= IOSTARTED;
10224 fs = inodedep->id_fs;
10225 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10227 dp = (struct ufs1_dinode *)bp->b_data +
10228 ino_to_fsbo(fs, inodedep->id_ino);
10231 * If we're on the unlinked list but have not yet written our
10232 * next pointer initialize it here.
10234 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10235 struct inodedep *inon;
10237 inon = TAILQ_NEXT(inodedep, id_unlinked);
10238 dp->di_freelink = inon ? inon->id_ino : 0;
10241 * If the bitmap is not yet written, then the allocated
10242 * inode cannot be written to disk.
10244 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10245 if (inodedep->id_savedino1 != NULL)
10246 panic("initiate_write_inodeblock_ufs1: I/O underway");
10248 sip = malloc(sizeof(struct ufs1_dinode),
10249 M_SAVEDINO, M_SOFTDEP_FLAGS);
10251 inodedep->id_savedino1 = sip;
10252 *inodedep->id_savedino1 = *dp;
10253 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10254 dp->di_gen = inodedep->id_savedino1->di_gen;
10255 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10259 * If no dependencies, then there is nothing to roll back.
10261 inodedep->id_savedsize = dp->di_size;
10262 inodedep->id_savedextsize = 0;
10263 inodedep->id_savednlink = dp->di_nlink;
10264 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10265 TAILQ_EMPTY(&inodedep->id_inoreflst))
10268 * Revert the link count to that of the first unwritten journal entry.
10270 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10272 dp->di_nlink = inoref->if_nlink;
10274 * Set the dependencies to busy.
10276 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10277 adp = TAILQ_NEXT(adp, ad_next)) {
10279 if (deplist != 0 && prevlbn >= adp->ad_offset)
10280 panic("softdep_write_inodeblock: lbn order");
10281 prevlbn = adp->ad_offset;
10282 if (adp->ad_offset < UFS_NDADDR &&
10283 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10284 panic("initiate_write_inodeblock_ufs1: "
10285 "direct pointer #%jd mismatch %d != %jd",
10286 (intmax_t)adp->ad_offset,
10287 dp->di_db[adp->ad_offset],
10288 (intmax_t)adp->ad_newblkno);
10289 if (adp->ad_offset >= UFS_NDADDR &&
10290 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10291 panic("initiate_write_inodeblock_ufs1: "
10292 "indirect pointer #%jd mismatch %d != %jd",
10293 (intmax_t)adp->ad_offset - UFS_NDADDR,
10294 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10295 (intmax_t)adp->ad_newblkno);
10296 deplist |= 1 << adp->ad_offset;
10297 if ((adp->ad_state & ATTACHED) == 0)
10298 panic("initiate_write_inodeblock_ufs1: "
10299 "Unknown state 0x%x", adp->ad_state);
10300 #endif /* INVARIANTS */
10301 adp->ad_state &= ~ATTACHED;
10302 adp->ad_state |= UNDONE;
10305 * The on-disk inode cannot claim to be any larger than the last
10306 * fragment that has been written. Otherwise, the on-disk inode
10307 * might have fragments that were not the last block in the file
10308 * which would corrupt the filesystem.
10310 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10311 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10312 if (adp->ad_offset >= UFS_NDADDR)
10314 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10315 /* keep going until hitting a rollback to a frag */
10316 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10318 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10319 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10321 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10322 panic("initiate_write_inodeblock_ufs1: "
10324 #endif /* INVARIANTS */
10327 for (i = 0; i < UFS_NIADDR; i++) {
10329 if (dp->di_ib[i] != 0 &&
10330 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10331 panic("initiate_write_inodeblock_ufs1: "
10333 #endif /* INVARIANTS */
10339 * If we have zero'ed out the last allocated block of the file,
10340 * roll back the size to the last currently allocated block.
10341 * We know that this last allocated block is a full-sized as
10342 * we already checked for fragments in the loop above.
10344 if (lastadp != NULL &&
10345 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10346 for (i = lastadp->ad_offset; i >= 0; i--)
10347 if (dp->di_db[i] != 0)
10349 dp->di_size = (i + 1) * fs->fs_bsize;
10352 * The only dependencies are for indirect blocks.
10354 * The file size for indirect block additions is not guaranteed.
10355 * Such a guarantee would be non-trivial to achieve. The conventional
10356 * synchronous write implementation also does not make this guarantee.
10357 * Fsck should catch and fix discrepancies. Arguably, the file size
10358 * can be over-estimated without destroying integrity when the file
10359 * moves into the indirect blocks (i.e., is large). If we want to
10360 * postpone fsck, we are stuck with this argument.
10362 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10363 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10367 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10368 * Note that any bug fixes made to this routine must be done in the
10369 * version found above.
10371 * Called from within the procedure above to deal with unsatisfied
10372 * allocation dependencies in an inodeblock. The buffer must be
10373 * locked, thus, no I/O completion operations can occur while we
10374 * are manipulating its associated dependencies.
10377 initiate_write_inodeblock_ufs2(inodedep, bp)
10378 struct inodedep *inodedep;
10379 struct buf *bp; /* The inode block */
10381 struct allocdirect *adp, *lastadp;
10382 struct ufs2_dinode *dp;
10383 struct ufs2_dinode *sip;
10384 struct inoref *inoref;
10385 struct ufsmount *ump;
10389 ufs_lbn_t prevlbn = 0;
10393 if (inodedep->id_state & IOSTARTED)
10394 panic("initiate_write_inodeblock_ufs2: already started");
10395 inodedep->id_state |= IOSTARTED;
10396 fs = inodedep->id_fs;
10397 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10399 dp = (struct ufs2_dinode *)bp->b_data +
10400 ino_to_fsbo(fs, inodedep->id_ino);
10403 * If we're on the unlinked list but have not yet written our
10404 * next pointer initialize it here.
10406 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10407 struct inodedep *inon;
10409 inon = TAILQ_NEXT(inodedep, id_unlinked);
10410 dp->di_freelink = inon ? inon->id_ino : 0;
10411 ffs_update_dinode_ckhash(fs, dp);
10414 * If the bitmap is not yet written, then the allocated
10415 * inode cannot be written to disk.
10417 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10418 if (inodedep->id_savedino2 != NULL)
10419 panic("initiate_write_inodeblock_ufs2: I/O underway");
10421 sip = malloc(sizeof(struct ufs2_dinode),
10422 M_SAVEDINO, M_SOFTDEP_FLAGS);
10424 inodedep->id_savedino2 = sip;
10425 *inodedep->id_savedino2 = *dp;
10426 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10427 dp->di_gen = inodedep->id_savedino2->di_gen;
10428 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10432 * If no dependencies, then there is nothing to roll back.
10434 inodedep->id_savedsize = dp->di_size;
10435 inodedep->id_savedextsize = dp->di_extsize;
10436 inodedep->id_savednlink = dp->di_nlink;
10437 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10438 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10439 TAILQ_EMPTY(&inodedep->id_inoreflst))
10442 * Revert the link count to that of the first unwritten journal entry.
10444 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10446 dp->di_nlink = inoref->if_nlink;
10449 * Set the ext data dependencies to busy.
10451 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10452 adp = TAILQ_NEXT(adp, ad_next)) {
10454 if (deplist != 0 && prevlbn >= adp->ad_offset)
10455 panic("initiate_write_inodeblock_ufs2: lbn order");
10456 prevlbn = adp->ad_offset;
10457 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10458 panic("initiate_write_inodeblock_ufs2: "
10459 "ext pointer #%jd mismatch %jd != %jd",
10460 (intmax_t)adp->ad_offset,
10461 (intmax_t)dp->di_extb[adp->ad_offset],
10462 (intmax_t)adp->ad_newblkno);
10463 deplist |= 1 << adp->ad_offset;
10464 if ((adp->ad_state & ATTACHED) == 0)
10465 panic("initiate_write_inodeblock_ufs2: Unknown "
10466 "state 0x%x", adp->ad_state);
10467 #endif /* INVARIANTS */
10468 adp->ad_state &= ~ATTACHED;
10469 adp->ad_state |= UNDONE;
10472 * The on-disk inode cannot claim to be any larger than the last
10473 * fragment that has been written. Otherwise, the on-disk inode
10474 * might have fragments that were not the last block in the ext
10475 * data which would corrupt the filesystem.
10477 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10478 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10479 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10480 /* keep going until hitting a rollback to a frag */
10481 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10483 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10484 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10486 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10487 panic("initiate_write_inodeblock_ufs2: "
10489 #endif /* INVARIANTS */
10490 dp->di_extb[i] = 0;
10496 * If we have zero'ed out the last allocated block of the ext
10497 * data, roll back the size to the last currently allocated block.
10498 * We know that this last allocated block is a full-sized as
10499 * we already checked for fragments in the loop above.
10501 if (lastadp != NULL &&
10502 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10503 for (i = lastadp->ad_offset; i >= 0; i--)
10504 if (dp->di_extb[i] != 0)
10506 dp->di_extsize = (i + 1) * fs->fs_bsize;
10509 * Set the file data dependencies to busy.
10511 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10512 adp = TAILQ_NEXT(adp, ad_next)) {
10514 if (deplist != 0 && prevlbn >= adp->ad_offset)
10515 panic("softdep_write_inodeblock: lbn order");
10516 if ((adp->ad_state & ATTACHED) == 0)
10517 panic("inodedep %p and adp %p not attached", inodedep, adp);
10518 prevlbn = adp->ad_offset;
10519 if (adp->ad_offset < UFS_NDADDR &&
10520 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10521 panic("initiate_write_inodeblock_ufs2: "
10522 "direct pointer #%jd mismatch %jd != %jd",
10523 (intmax_t)adp->ad_offset,
10524 (intmax_t)dp->di_db[adp->ad_offset],
10525 (intmax_t)adp->ad_newblkno);
10526 if (adp->ad_offset >= UFS_NDADDR &&
10527 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10528 panic("initiate_write_inodeblock_ufs2: "
10529 "indirect pointer #%jd mismatch %jd != %jd",
10530 (intmax_t)adp->ad_offset - UFS_NDADDR,
10531 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10532 (intmax_t)adp->ad_newblkno);
10533 deplist |= 1 << adp->ad_offset;
10534 if ((adp->ad_state & ATTACHED) == 0)
10535 panic("initiate_write_inodeblock_ufs2: Unknown "
10536 "state 0x%x", adp->ad_state);
10537 #endif /* INVARIANTS */
10538 adp->ad_state &= ~ATTACHED;
10539 adp->ad_state |= UNDONE;
10542 * The on-disk inode cannot claim to be any larger than the last
10543 * fragment that has been written. Otherwise, the on-disk inode
10544 * might have fragments that were not the last block in the file
10545 * which would corrupt the filesystem.
10547 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10548 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10549 if (adp->ad_offset >= UFS_NDADDR)
10551 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10552 /* keep going until hitting a rollback to a frag */
10553 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10555 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10556 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10558 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10559 panic("initiate_write_inodeblock_ufs2: "
10561 #endif /* INVARIANTS */
10564 for (i = 0; i < UFS_NIADDR; i++) {
10566 if (dp->di_ib[i] != 0 &&
10567 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10568 panic("initiate_write_inodeblock_ufs2: "
10570 #endif /* INVARIANTS */
10573 ffs_update_dinode_ckhash(fs, dp);
10577 * If we have zero'ed out the last allocated block of the file,
10578 * roll back the size to the last currently allocated block.
10579 * We know that this last allocated block is a full-sized as
10580 * we already checked for fragments in the loop above.
10582 if (lastadp != NULL &&
10583 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10584 for (i = lastadp->ad_offset; i >= 0; i--)
10585 if (dp->di_db[i] != 0)
10587 dp->di_size = (i + 1) * fs->fs_bsize;
10590 * The only dependencies are for indirect blocks.
10592 * The file size for indirect block additions is not guaranteed.
10593 * Such a guarantee would be non-trivial to achieve. The conventional
10594 * synchronous write implementation also does not make this guarantee.
10595 * Fsck should catch and fix discrepancies. Arguably, the file size
10596 * can be over-estimated without destroying integrity when the file
10597 * moves into the indirect blocks (i.e., is large). If we want to
10598 * postpone fsck, we are stuck with this argument.
10600 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10601 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10602 ffs_update_dinode_ckhash(fs, dp);
10606 * Cancel an indirdep as a result of truncation. Release all of the
10607 * children allocindirs and place their journal work on the appropriate
10611 cancel_indirdep(indirdep, bp, freeblks)
10612 struct indirdep *indirdep;
10614 struct freeblks *freeblks;
10616 struct allocindir *aip;
10619 * None of the indirect pointers will ever be visible,
10620 * so they can simply be tossed. GOINGAWAY ensures
10621 * that allocated pointers will be saved in the buffer
10622 * cache until they are freed. Note that they will
10623 * only be able to be found by their physical address
10624 * since the inode mapping the logical address will
10625 * be gone. The save buffer used for the safe copy
10626 * was allocated in setup_allocindir_phase2 using
10627 * the physical address so it could be used for this
10628 * purpose. Hence we swap the safe copy with the real
10629 * copy, allowing the safe copy to be freed and holding
10630 * on to the real copy for later use in indir_trunc.
10632 if (indirdep->ir_state & GOINGAWAY)
10633 panic("cancel_indirdep: already gone");
10634 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10635 indirdep->ir_state |= DEPCOMPLETE;
10636 LIST_REMOVE(indirdep, ir_next);
10638 indirdep->ir_state |= GOINGAWAY;
10640 * Pass in bp for blocks still have journal writes
10641 * pending so we can cancel them on their own.
10643 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
10644 cancel_allocindir(aip, bp, freeblks, 0);
10645 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
10646 cancel_allocindir(aip, NULL, freeblks, 0);
10647 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
10648 cancel_allocindir(aip, NULL, freeblks, 0);
10649 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
10650 cancel_allocindir(aip, NULL, freeblks, 0);
10652 * If there are pending partial truncations we need to keep the
10653 * old block copy around until they complete. This is because
10654 * the current b_data is not a perfect superset of the available
10657 if (TAILQ_EMPTY(&indirdep->ir_trunc))
10658 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10660 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10661 WORKLIST_REMOVE(&indirdep->ir_list);
10662 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10663 indirdep->ir_bp = NULL;
10664 indirdep->ir_freeblks = freeblks;
10668 * Free an indirdep once it no longer has new pointers to track.
10671 free_indirdep(indirdep)
10672 struct indirdep *indirdep;
10675 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10676 ("free_indirdep: Indir trunc list not empty."));
10677 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10678 ("free_indirdep: Complete head not empty."));
10679 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10680 ("free_indirdep: write head not empty."));
10681 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10682 ("free_indirdep: done head not empty."));
10683 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10684 ("free_indirdep: deplist head not empty."));
10685 KASSERT((indirdep->ir_state & DEPCOMPLETE),
10686 ("free_indirdep: %p still on newblk list.", indirdep));
10687 KASSERT(indirdep->ir_saveddata == NULL,
10688 ("free_indirdep: %p still has saved data.", indirdep));
10689 if (indirdep->ir_state & ONWORKLIST)
10690 WORKLIST_REMOVE(&indirdep->ir_list);
10691 WORKITEM_FREE(indirdep, D_INDIRDEP);
10695 * Called before a write to an indirdep. This routine is responsible for
10696 * rolling back pointers to a safe state which includes only those
10697 * allocindirs which have been completed.
10700 initiate_write_indirdep(indirdep, bp)
10701 struct indirdep *indirdep;
10704 struct ufsmount *ump;
10706 indirdep->ir_state |= IOSTARTED;
10707 if (indirdep->ir_state & GOINGAWAY)
10708 panic("disk_io_initiation: indirdep gone");
10710 * If there are no remaining dependencies, this will be writing
10711 * the real pointers.
10713 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10714 TAILQ_EMPTY(&indirdep->ir_trunc))
10717 * Replace up-to-date version with safe version.
10719 if (indirdep->ir_saveddata == NULL) {
10720 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10723 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10727 indirdep->ir_state &= ~ATTACHED;
10728 indirdep->ir_state |= UNDONE;
10729 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10730 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10735 * Called when an inode has been cleared in a cg bitmap. This finally
10736 * eliminates any canceled jaddrefs
10739 softdep_setup_inofree(mp, bp, ino, wkhd)
10743 struct workhead *wkhd;
10745 struct worklist *wk, *wkn;
10746 struct inodedep *inodedep;
10747 struct ufsmount *ump;
10752 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10753 ("softdep_setup_inofree called on non-softdep filesystem"));
10754 ump = VFSTOUFS(mp);
10757 cgp = (struct cg *)bp->b_data;
10758 inosused = cg_inosused(cgp);
10759 if (isset(inosused, ino % fs->fs_ipg))
10760 panic("softdep_setup_inofree: inode %ju not freed.",
10762 if (inodedep_lookup(mp, ino, 0, &inodedep))
10763 panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
10764 (uintmax_t)ino, inodedep);
10766 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10767 if (wk->wk_type != D_JADDREF)
10769 WORKLIST_REMOVE(wk);
10771 * We can free immediately even if the jaddref
10772 * isn't attached in a background write as now
10773 * the bitmaps are reconciled.
10775 wk->wk_state |= COMPLETE | ATTACHED;
10776 free_jaddref(WK_JADDREF(wk));
10778 jwork_move(&bp->b_dep, wkhd);
10785 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10786 * map. Any dependencies waiting for the write to clear are added to the
10787 * buf's list and any jnewblks that are being canceled are discarded
10791 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
10794 ufs2_daddr_t blkno;
10796 struct workhead *wkhd;
10798 struct bmsafemap *bmsafemap;
10799 struct jnewblk *jnewblk;
10800 struct ufsmount *ump;
10801 struct worklist *wk;
10806 ufs2_daddr_t jstart;
10814 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
10815 blkno, frags, wkhd);
10817 ump = VFSTOUFS(mp);
10818 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
10819 ("softdep_setup_blkfree called on non-softdep filesystem"));
10821 /* Lookup the bmsafemap so we track when it is dirty. */
10823 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10825 * Detach any jnewblks which have been canceled. They must linger
10826 * until the bitmap is cleared again by ffs_blkfree() to prevent
10827 * an unjournaled allocation from hitting the disk.
10830 while ((wk = LIST_FIRST(wkhd)) != NULL) {
10832 "softdep_setup_blkfree: blkno %jd wk type %d",
10833 blkno, wk->wk_type);
10834 WORKLIST_REMOVE(wk);
10835 if (wk->wk_type != D_JNEWBLK) {
10836 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
10839 jnewblk = WK_JNEWBLK(wk);
10840 KASSERT(jnewblk->jn_state & GOINGAWAY,
10841 ("softdep_setup_blkfree: jnewblk not canceled."));
10844 * Assert that this block is free in the bitmap
10845 * before we discard the jnewblk.
10847 cgp = (struct cg *)bp->b_data;
10848 blksfree = cg_blksfree(cgp);
10849 bno = dtogd(fs, jnewblk->jn_blkno);
10850 for (i = jnewblk->jn_oldfrags;
10851 i < jnewblk->jn_frags; i++) {
10852 if (isset(blksfree, bno + i))
10854 panic("softdep_setup_blkfree: not free");
10858 * Even if it's not attached we can free immediately
10859 * as the new bitmap is correct.
10861 wk->wk_state |= COMPLETE | ATTACHED;
10862 free_jnewblk(jnewblk);
10868 * Assert that we are not freeing a block which has an outstanding
10869 * allocation dependency.
10871 fs = VFSTOUFS(mp)->um_fs;
10872 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10873 end = blkno + frags;
10874 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
10876 * Don't match against blocks that will be freed when the
10877 * background write is done.
10879 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
10880 (COMPLETE | DEPCOMPLETE))
10882 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
10883 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
10884 if ((blkno >= jstart && blkno < jend) ||
10885 (end > jstart && end <= jend)) {
10886 printf("state 0x%X %jd - %d %d dep %p\n",
10887 jnewblk->jn_state, jnewblk->jn_blkno,
10888 jnewblk->jn_oldfrags, jnewblk->jn_frags,
10890 panic("softdep_setup_blkfree: "
10891 "%jd-%jd(%d) overlaps with %jd-%jd",
10892 blkno, end, frags, jstart, jend);
10900 * Revert a block allocation when the journal record that describes it
10901 * is not yet written.
10904 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
10905 struct jnewblk *jnewblk;
10910 ufs1_daddr_t fragno;
10916 cgbno = dtogd(fs, jnewblk->jn_blkno);
10918 * We have to test which frags need to be rolled back. We may
10919 * be operating on a stale copy when doing background writes.
10921 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
10922 if (isclr(blksfree, cgbno + i))
10927 * This is mostly ffs_blkfree() sans some validation and
10928 * superblock updates.
10930 if (frags == fs->fs_frag) {
10931 fragno = fragstoblks(fs, cgbno);
10932 ffs_setblock(fs, blksfree, fragno);
10933 ffs_clusteracct(fs, cgp, fragno, 1);
10934 cgp->cg_cs.cs_nbfree++;
10936 cgbno += jnewblk->jn_oldfrags;
10937 bbase = cgbno - fragnum(fs, cgbno);
10938 /* Decrement the old frags. */
10939 blk = blkmap(fs, blksfree, bbase);
10940 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
10941 /* Deallocate the fragment */
10942 for (i = 0; i < frags; i++)
10943 setbit(blksfree, cgbno + i);
10944 cgp->cg_cs.cs_nffree += frags;
10945 /* Add back in counts associated with the new frags */
10946 blk = blkmap(fs, blksfree, bbase);
10947 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
10948 /* If a complete block has been reassembled, account for it. */
10949 fragno = fragstoblks(fs, bbase);
10950 if (ffs_isblock(fs, blksfree, fragno)) {
10951 cgp->cg_cs.cs_nffree -= fs->fs_frag;
10952 ffs_clusteracct(fs, cgp, fragno, 1);
10953 cgp->cg_cs.cs_nbfree++;
10957 jnewblk->jn_state &= ~ATTACHED;
10958 jnewblk->jn_state |= UNDONE;
10964 initiate_write_bmsafemap(bmsafemap, bp)
10965 struct bmsafemap *bmsafemap;
10966 struct buf *bp; /* The cg block. */
10968 struct jaddref *jaddref;
10969 struct jnewblk *jnewblk;
10977 * If this is a background write, we did this at the time that
10978 * the copy was made, so do not need to do it again.
10980 if (bmsafemap->sm_state & IOSTARTED)
10982 bmsafemap->sm_state |= IOSTARTED;
10984 * Clear any inode allocations which are pending journal writes.
10986 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
10987 cgp = (struct cg *)bp->b_data;
10988 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
10989 inosused = cg_inosused(cgp);
10990 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
10991 ino = jaddref->ja_ino % fs->fs_ipg;
10992 if (isset(inosused, ino)) {
10993 if ((jaddref->ja_mode & IFMT) == IFDIR)
10994 cgp->cg_cs.cs_ndir--;
10995 cgp->cg_cs.cs_nifree++;
10996 clrbit(inosused, ino);
10997 jaddref->ja_state &= ~ATTACHED;
10998 jaddref->ja_state |= UNDONE;
11001 panic("initiate_write_bmsafemap: inode %ju "
11002 "marked free", (uintmax_t)jaddref->ja_ino);
11006 * Clear any block allocations which are pending journal writes.
11008 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11009 cgp = (struct cg *)bp->b_data;
11010 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11011 blksfree = cg_blksfree(cgp);
11012 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11013 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11015 panic("initiate_write_bmsafemap: block %jd "
11016 "marked free", jnewblk->jn_blkno);
11020 * Move allocation lists to the written lists so they can be
11021 * cleared once the block write is complete.
11023 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11024 inodedep, id_deps);
11025 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11027 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11032 * This routine is called during the completion interrupt
11033 * service routine for a disk write (from the procedure called
11034 * by the device driver to inform the filesystem caches of
11035 * a request completion). It should be called early in this
11036 * procedure, before the block is made available to other
11037 * processes or other routines are called.
11041 softdep_disk_write_complete(bp)
11042 struct buf *bp; /* describes the completed disk write */
11044 struct worklist *wk;
11045 struct worklist *owk;
11046 struct ufsmount *ump;
11047 struct workhead reattach;
11048 struct freeblks *freeblks;
11051 ump = softdep_bp_to_mp(bp);
11052 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11053 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11054 "with outstanding dependencies for buffer %p", bp));
11058 * If an error occurred while doing the write, then the data
11059 * has not hit the disk and the dependencies cannot be processed.
11060 * But we do have to go through and roll forward any dependencies
11061 * that were rolled back before the disk write.
11065 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11066 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11067 switch (wk->wk_type) {
11070 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11074 handle_written_inodeblock(WK_INODEDEP(wk),
11079 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11084 handle_written_indirdep(WK_INDIRDEP(wk),
11088 /* nothing to roll forward */
11097 LIST_INIT(&reattach);
11100 * Ump SU lock must not be released anywhere in this code segment.
11103 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11104 WORKLIST_REMOVE(wk);
11105 atomic_add_long(&dep_write[wk->wk_type], 1);
11107 panic("duplicate worklist: %p\n", wk);
11109 switch (wk->wk_type) {
11112 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11114 WORKLIST_INSERT(&reattach, wk);
11118 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11120 WORKLIST_INSERT(&reattach, wk);
11124 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11126 WORKLIST_INSERT(&reattach, wk);
11130 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11133 case D_ALLOCDIRECT:
11134 wk->wk_state |= COMPLETE;
11135 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11139 wk->wk_state |= COMPLETE;
11140 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11144 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11146 WORKLIST_INSERT(&reattach, wk);
11150 wk->wk_state |= COMPLETE;
11151 freeblks = WK_FREEBLKS(wk);
11152 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11153 LIST_EMPTY(&freeblks->fb_jblkdephd))
11154 add_to_worklist(wk, WK_NODELAY);
11158 handle_written_freework(WK_FREEWORK(wk));
11162 free_jsegdep(WK_JSEGDEP(wk));
11166 handle_written_jseg(WK_JSEG(wk), bp);
11170 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11171 WORKLIST_INSERT(&reattach, wk);
11175 free_freedep(WK_FREEDEP(wk));
11179 panic("handle_disk_write_complete: Unknown type %s",
11180 TYPENAME(wk->wk_type));
11185 * Reattach any requests that must be redone.
11187 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11188 WORKLIST_REMOVE(wk);
11189 WORKLIST_INSERT(&bp->b_dep, wk);
11197 * Called from within softdep_disk_write_complete above.
11200 handle_allocdirect_partdone(adp, wkhd)
11201 struct allocdirect *adp; /* the completed allocdirect */
11202 struct workhead *wkhd; /* Work to do when inode is writtne. */
11204 struct allocdirectlst *listhead;
11205 struct allocdirect *listadp;
11206 struct inodedep *inodedep;
11209 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11210 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11213 * The on-disk inode cannot claim to be any larger than the last
11214 * fragment that has been written. Otherwise, the on-disk inode
11215 * might have fragments that were not the last block in the file
11216 * which would corrupt the filesystem. Thus, we cannot free any
11217 * allocdirects after one whose ad_oldblkno claims a fragment as
11218 * these blocks must be rolled back to zero before writing the inode.
11219 * We check the currently active set of allocdirects in id_inoupdt
11220 * or id_extupdt as appropriate.
11222 inodedep = adp->ad_inodedep;
11223 bsize = inodedep->id_fs->fs_bsize;
11224 if (adp->ad_state & EXTDATA)
11225 listhead = &inodedep->id_extupdt;
11227 listhead = &inodedep->id_inoupdt;
11228 TAILQ_FOREACH(listadp, listhead, ad_next) {
11229 /* found our block */
11230 if (listadp == adp)
11232 /* continue if ad_oldlbn is not a fragment */
11233 if (listadp->ad_oldsize == 0 ||
11234 listadp->ad_oldsize == bsize)
11236 /* hit a fragment */
11240 * If we have reached the end of the current list without
11241 * finding the just finished dependency, then it must be
11242 * on the future dependency list. Future dependencies cannot
11243 * be freed until they are moved to the current list.
11245 if (listadp == NULL) {
11247 if (adp->ad_state & EXTDATA)
11248 listhead = &inodedep->id_newextupdt;
11250 listhead = &inodedep->id_newinoupdt;
11251 TAILQ_FOREACH(listadp, listhead, ad_next)
11252 /* found our block */
11253 if (listadp == adp)
11255 if (listadp == NULL)
11256 panic("handle_allocdirect_partdone: lost dep");
11257 #endif /* INVARIANTS */
11261 * If we have found the just finished dependency, then queue
11262 * it along with anything that follows it that is complete.
11263 * Since the pointer has not yet been written in the inode
11264 * as the dependency prevents it, place the allocdirect on the
11265 * bufwait list where it will be freed once the pointer is
11269 wkhd = &inodedep->id_bufwait;
11270 for (; adp; adp = listadp) {
11271 listadp = TAILQ_NEXT(adp, ad_next);
11272 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11274 TAILQ_REMOVE(listhead, adp, ad_next);
11275 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11280 * Called from within softdep_disk_write_complete above. This routine
11281 * completes successfully written allocindirs.
11284 handle_allocindir_partdone(aip)
11285 struct allocindir *aip; /* the completed allocindir */
11287 struct indirdep *indirdep;
11289 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11291 indirdep = aip->ai_indirdep;
11292 LIST_REMOVE(aip, ai_next);
11294 * Don't set a pointer while the buffer is undergoing IO or while
11295 * we have active truncations.
11297 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11298 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11301 if (indirdep->ir_state & UFS1FMT)
11302 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11305 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11308 * Await the pointer write before freeing the allocindir.
11310 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11314 * Release segments held on a jwork list.
11318 struct workhead *wkhd;
11320 struct worklist *wk;
11322 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11323 WORKLIST_REMOVE(wk);
11324 switch (wk->wk_type) {
11326 free_jsegdep(WK_JSEGDEP(wk));
11329 free_freedep(WK_FREEDEP(wk));
11332 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11333 WORKITEM_FREE(wk, D_FREEFRAG);
11336 handle_written_freework(WK_FREEWORK(wk));
11339 panic("handle_jwork: Unknown type %s\n",
11340 TYPENAME(wk->wk_type));
11346 * Handle the bufwait list on an inode when it is safe to release items
11347 * held there. This normally happens after an inode block is written but
11348 * may be delayed and handled later if there are pending journal items that
11349 * are not yet safe to be released.
11351 static struct freefile *
11352 handle_bufwait(inodedep, refhd)
11353 struct inodedep *inodedep;
11354 struct workhead *refhd;
11356 struct jaddref *jaddref;
11357 struct freefile *freefile;
11358 struct worklist *wk;
11361 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11362 WORKLIST_REMOVE(wk);
11363 switch (wk->wk_type) {
11366 * We defer adding freefile to the worklist
11367 * until all other additions have been made to
11368 * ensure that it will be done after all the
11369 * old blocks have been freed.
11371 if (freefile != NULL)
11372 panic("handle_bufwait: freefile");
11373 freefile = WK_FREEFILE(wk);
11377 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11381 diradd_inode_written(WK_DIRADD(wk), inodedep);
11385 wk->wk_state |= COMPLETE;
11386 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11387 add_to_worklist(wk, 0);
11391 wk->wk_state |= COMPLETE;
11392 add_to_worklist(wk, 0);
11395 case D_ALLOCDIRECT:
11397 free_newblk(WK_NEWBLK(wk));
11401 wk->wk_state |= COMPLETE;
11402 free_jnewblk(WK_JNEWBLK(wk));
11406 * Save freed journal segments and add references on
11407 * the supplied list which will delay their release
11408 * until the cg bitmap is cleared on disk.
11412 free_jsegdep(WK_JSEGDEP(wk));
11414 WORKLIST_INSERT(refhd, wk);
11418 jaddref = WK_JADDREF(wk);
11419 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11422 * Transfer any jaddrefs to the list to be freed with
11423 * the bitmap if we're handling a removed file.
11425 if (refhd == NULL) {
11426 wk->wk_state |= COMPLETE;
11427 free_jaddref(jaddref);
11429 WORKLIST_INSERT(refhd, wk);
11433 panic("handle_bufwait: Unknown type %p(%s)",
11434 wk, TYPENAME(wk->wk_type));
11441 * Called from within softdep_disk_write_complete above to restore
11442 * in-memory inode block contents to their most up-to-date state. Note
11443 * that this routine is always called from interrupt level with further
11444 * interrupts from this device blocked.
11446 * If the write did not succeed, we will do all the roll-forward
11447 * operations, but we will not take the actions that will allow its
11448 * dependencies to be processed.
11451 handle_written_inodeblock(inodedep, bp, flags)
11452 struct inodedep *inodedep;
11453 struct buf *bp; /* buffer containing the inode block */
11456 struct freefile *freefile;
11457 struct allocdirect *adp, *nextadp;
11458 struct ufs1_dinode *dp1 = NULL;
11459 struct ufs2_dinode *dp2 = NULL;
11460 struct workhead wkhd;
11461 int hadchanges, fstype;
11467 if ((inodedep->id_state & IOSTARTED) == 0)
11468 panic("handle_written_inodeblock: not started");
11469 inodedep->id_state &= ~IOSTARTED;
11470 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11472 dp1 = (struct ufs1_dinode *)bp->b_data +
11473 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11474 freelink = dp1->di_freelink;
11477 dp2 = (struct ufs2_dinode *)bp->b_data +
11478 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11479 freelink = dp2->di_freelink;
11482 * Leave this inodeblock dirty until it's in the list.
11484 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11485 (flags & WRITESUCCEEDED)) {
11486 struct inodedep *inon;
11488 inon = TAILQ_NEXT(inodedep, id_unlinked);
11489 if ((inon == NULL && freelink == 0) ||
11490 (inon && inon->id_ino == freelink)) {
11492 inon->id_state |= UNLINKPREV;
11493 inodedep->id_state |= UNLINKNEXT;
11498 * If we had to rollback the inode allocation because of
11499 * bitmaps being incomplete, then simply restore it.
11500 * Keep the block dirty so that it will not be reclaimed until
11501 * all associated dependencies have been cleared and the
11502 * corresponding updates written to disk.
11504 if (inodedep->id_savedino1 != NULL) {
11506 if (fstype == UFS1)
11507 *dp1 = *inodedep->id_savedino1;
11509 *dp2 = *inodedep->id_savedino2;
11510 free(inodedep->id_savedino1, M_SAVEDINO);
11511 inodedep->id_savedino1 = NULL;
11512 if ((bp->b_flags & B_DELWRI) == 0)
11513 stat_inode_bitmap++;
11516 * If the inode is clear here and GOINGAWAY it will never
11517 * be written. Process the bufwait and clear any pending
11518 * work which may include the freefile.
11520 if (inodedep->id_state & GOINGAWAY)
11524 if (flags & WRITESUCCEEDED)
11525 inodedep->id_state |= COMPLETE;
11527 * Roll forward anything that had to be rolled back before
11528 * the inode could be updated.
11530 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11531 nextadp = TAILQ_NEXT(adp, ad_next);
11532 if (adp->ad_state & ATTACHED)
11533 panic("handle_written_inodeblock: new entry");
11534 if (fstype == UFS1) {
11535 if (adp->ad_offset < UFS_NDADDR) {
11536 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11537 panic("%s %s #%jd mismatch %d != %jd",
11538 "handle_written_inodeblock:",
11540 (intmax_t)adp->ad_offset,
11541 dp1->di_db[adp->ad_offset],
11542 (intmax_t)adp->ad_oldblkno);
11543 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11545 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11547 panic("%s: %s #%jd allocated as %d",
11548 "handle_written_inodeblock",
11549 "indirect pointer",
11550 (intmax_t)adp->ad_offset -
11552 dp1->di_ib[adp->ad_offset -
11554 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11558 if (adp->ad_offset < UFS_NDADDR) {
11559 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11560 panic("%s: %s #%jd %s %jd != %jd",
11561 "handle_written_inodeblock",
11563 (intmax_t)adp->ad_offset, "mismatch",
11564 (intmax_t)dp2->di_db[adp->ad_offset],
11565 (intmax_t)adp->ad_oldblkno);
11566 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11568 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11570 panic("%s: %s #%jd allocated as %jd",
11571 "handle_written_inodeblock",
11572 "indirect pointer",
11573 (intmax_t)adp->ad_offset -
11576 dp2->di_ib[adp->ad_offset -
11578 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11582 adp->ad_state &= ~UNDONE;
11583 adp->ad_state |= ATTACHED;
11586 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11587 nextadp = TAILQ_NEXT(adp, ad_next);
11588 if (adp->ad_state & ATTACHED)
11589 panic("handle_written_inodeblock: new entry");
11590 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11591 panic("%s: direct pointers #%jd %s %jd != %jd",
11592 "handle_written_inodeblock",
11593 (intmax_t)adp->ad_offset, "mismatch",
11594 (intmax_t)dp2->di_extb[adp->ad_offset],
11595 (intmax_t)adp->ad_oldblkno);
11596 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11597 adp->ad_state &= ~UNDONE;
11598 adp->ad_state |= ATTACHED;
11601 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11602 stat_direct_blk_ptrs++;
11604 * Reset the file size to its most up-to-date value.
11606 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11607 panic("handle_written_inodeblock: bad size");
11608 if (inodedep->id_savednlink > UFS_LINK_MAX)
11609 panic("handle_written_inodeblock: Invalid link count "
11610 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11612 if (fstype == UFS1) {
11613 if (dp1->di_nlink != inodedep->id_savednlink) {
11614 dp1->di_nlink = inodedep->id_savednlink;
11617 if (dp1->di_size != inodedep->id_savedsize) {
11618 dp1->di_size = inodedep->id_savedsize;
11622 if (dp2->di_nlink != inodedep->id_savednlink) {
11623 dp2->di_nlink = inodedep->id_savednlink;
11626 if (dp2->di_size != inodedep->id_savedsize) {
11627 dp2->di_size = inodedep->id_savedsize;
11630 if (dp2->di_extsize != inodedep->id_savedextsize) {
11631 dp2->di_extsize = inodedep->id_savedextsize;
11635 inodedep->id_savedsize = -1;
11636 inodedep->id_savedextsize = -1;
11637 inodedep->id_savednlink = -1;
11639 * If there were any rollbacks in the inode block, then it must be
11640 * marked dirty so that its will eventually get written back in
11641 * its correct form.
11644 if (fstype == UFS2)
11645 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
11650 * If the write did not succeed, we have done all the roll-forward
11651 * operations, but we cannot take the actions that will allow its
11652 * dependencies to be processed.
11654 if ((flags & WRITESUCCEEDED) == 0)
11655 return (hadchanges);
11657 * Process any allocdirects that completed during the update.
11659 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11660 handle_allocdirect_partdone(adp, &wkhd);
11661 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11662 handle_allocdirect_partdone(adp, &wkhd);
11664 * Process deallocations that were held pending until the
11665 * inode had been written to disk. Freeing of the inode
11666 * is delayed until after all blocks have been freed to
11667 * avoid creation of new <vfsid, inum, lbn> triples
11668 * before the old ones have been deleted. Completely
11669 * unlinked inodes are not processed until the unlinked
11670 * inode list is written or the last reference is removed.
11672 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11673 freefile = handle_bufwait(inodedep, NULL);
11674 if (freefile && !LIST_EMPTY(&wkhd)) {
11675 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11680 * Move rolled forward dependency completions to the bufwait list
11681 * now that those that were already written have been processed.
11683 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11684 panic("handle_written_inodeblock: bufwait but no changes");
11685 jwork_move(&inodedep->id_bufwait, &wkhd);
11687 if (freefile != NULL) {
11689 * If the inode is goingaway it was never written. Fake up
11690 * the state here so free_inodedep() can succeed.
11692 if (inodedep->id_state & GOINGAWAY)
11693 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11694 if (free_inodedep(inodedep) == 0)
11695 panic("handle_written_inodeblock: live inodedep %p",
11697 add_to_worklist(&freefile->fx_list, 0);
11702 * If no outstanding dependencies, free it.
11704 if (free_inodedep(inodedep) ||
11705 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11706 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11707 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11708 LIST_FIRST(&inodedep->id_bufwait) == 0))
11710 return (hadchanges);
11714 * Perform needed roll-forwards and kick off any dependencies that
11715 * can now be processed.
11717 * If the write did not succeed, we will do all the roll-forward
11718 * operations, but we will not take the actions that will allow its
11719 * dependencies to be processed.
11722 handle_written_indirdep(indirdep, bp, bpp, flags)
11723 struct indirdep *indirdep;
11728 struct allocindir *aip;
11732 if (indirdep->ir_state & GOINGAWAY)
11733 panic("handle_written_indirdep: indirdep gone");
11734 if ((indirdep->ir_state & IOSTARTED) == 0)
11735 panic("handle_written_indirdep: IO not started");
11738 * If there were rollbacks revert them here.
11740 if (indirdep->ir_saveddata) {
11741 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11742 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11743 free(indirdep->ir_saveddata, M_INDIRDEP);
11744 indirdep->ir_saveddata = NULL;
11748 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11749 indirdep->ir_state |= ATTACHED;
11751 * If the write did not succeed, we have done all the roll-forward
11752 * operations, but we cannot take the actions that will allow its
11753 * dependencies to be processed.
11755 if ((flags & WRITESUCCEEDED) == 0) {
11756 stat_indir_blk_ptrs++;
11761 * Move allocindirs with written pointers to the completehd if
11762 * the indirdep's pointer is not yet written. Otherwise
11765 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
11766 LIST_REMOVE(aip, ai_next);
11767 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11768 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11770 newblk_freefrag(&aip->ai_block);
11773 free_newblk(&aip->ai_block);
11776 * Move allocindirs that have finished dependency processing from
11777 * the done list to the write list after updating the pointers.
11779 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11780 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
11781 handle_allocindir_partdone(aip);
11782 if (aip == LIST_FIRST(&indirdep->ir_donehd))
11783 panic("disk_write_complete: not gone");
11788 * Preserve the indirdep if there were any changes or if it is not
11789 * yet valid on disk.
11792 stat_indir_blk_ptrs++;
11797 * If there were no changes we can discard the savedbp and detach
11798 * ourselves from the buf. We are only carrying completed pointers
11801 sbp = indirdep->ir_savebp;
11802 sbp->b_flags |= B_INVAL | B_NOCACHE;
11803 indirdep->ir_savebp = NULL;
11804 indirdep->ir_bp = NULL;
11806 panic("handle_written_indirdep: bp already exists.");
11809 * The indirdep may not be freed until its parent points at it.
11811 if (indirdep->ir_state & DEPCOMPLETE)
11812 free_indirdep(indirdep);
11818 * Process a diradd entry after its dependent inode has been written.
11821 diradd_inode_written(dap, inodedep)
11822 struct diradd *dap;
11823 struct inodedep *inodedep;
11826 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
11827 dap->da_state |= COMPLETE;
11828 complete_diradd(dap);
11829 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
11833 * Returns true if the bmsafemap will have rollbacks when written. Must only
11834 * be called with the per-filesystem lock and the buf lock on the cg held.
11837 bmsafemap_backgroundwrite(bmsafemap, bp)
11838 struct bmsafemap *bmsafemap;
11843 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
11844 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
11845 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
11847 * If we're initiating a background write we need to process the
11848 * rollbacks as they exist now, not as they exist when IO starts.
11849 * No other consumers will look at the contents of the shadowed
11850 * buf so this is safe to do here.
11852 if (bp->b_xflags & BX_BKGRDMARKER)
11853 initiate_write_bmsafemap(bmsafemap, bp);
11859 * Re-apply an allocation when a cg write is complete.
11862 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
11863 struct jnewblk *jnewblk;
11868 ufs1_daddr_t fragno;
11869 ufs2_daddr_t blkno;
11875 cgbno = dtogd(fs, jnewblk->jn_blkno);
11876 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
11877 if (isclr(blksfree, cgbno + i))
11878 panic("jnewblk_rollforward: re-allocated fragment");
11881 if (frags == fs->fs_frag) {
11882 blkno = fragstoblks(fs, cgbno);
11883 ffs_clrblock(fs, blksfree, (long)blkno);
11884 ffs_clusteracct(fs, cgp, blkno, -1);
11885 cgp->cg_cs.cs_nbfree--;
11887 bbase = cgbno - fragnum(fs, cgbno);
11888 cgbno += jnewblk->jn_oldfrags;
11889 /* If a complete block had been reassembled, account for it. */
11890 fragno = fragstoblks(fs, bbase);
11891 if (ffs_isblock(fs, blksfree, fragno)) {
11892 cgp->cg_cs.cs_nffree += fs->fs_frag;
11893 ffs_clusteracct(fs, cgp, fragno, -1);
11894 cgp->cg_cs.cs_nbfree--;
11896 /* Decrement the old frags. */
11897 blk = blkmap(fs, blksfree, bbase);
11898 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11899 /* Allocate the fragment */
11900 for (i = 0; i < frags; i++)
11901 clrbit(blksfree, cgbno + i);
11902 cgp->cg_cs.cs_nffree -= frags;
11903 /* Add back in counts associated with the new frags */
11904 blk = blkmap(fs, blksfree, bbase);
11905 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11911 * Complete a write to a bmsafemap structure. Roll forward any bitmap
11912 * changes if it's not a background write. Set all written dependencies
11913 * to DEPCOMPLETE and free the structure if possible.
11915 * If the write did not succeed, we will do all the roll-forward
11916 * operations, but we will not take the actions that will allow its
11917 * dependencies to be processed.
11920 handle_written_bmsafemap(bmsafemap, bp, flags)
11921 struct bmsafemap *bmsafemap;
11925 struct newblk *newblk;
11926 struct inodedep *inodedep;
11927 struct jaddref *jaddref, *jatmp;
11928 struct jnewblk *jnewblk, *jntmp;
11929 struct ufsmount *ump;
11938 if ((bmsafemap->sm_state & IOSTARTED) == 0)
11939 panic("handle_written_bmsafemap: Not started\n");
11940 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
11942 bmsafemap->sm_state &= ~IOSTARTED;
11943 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
11945 * If write was successful, release journal work that was waiting
11946 * on the write. Otherwise move the work back.
11948 if (flags & WRITESUCCEEDED)
11949 handle_jwork(&bmsafemap->sm_freewr);
11951 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
11952 worklist, wk_list);
11955 * Restore unwritten inode allocation pending jaddref writes.
11957 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
11958 cgp = (struct cg *)bp->b_data;
11959 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11960 inosused = cg_inosused(cgp);
11961 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
11962 ja_bmdeps, jatmp) {
11963 if ((jaddref->ja_state & UNDONE) == 0)
11965 ino = jaddref->ja_ino % fs->fs_ipg;
11966 if (isset(inosused, ino))
11967 panic("handle_written_bmsafemap: "
11968 "re-allocated inode");
11969 /* Do the roll-forward only if it's a real copy. */
11971 if ((jaddref->ja_mode & IFMT) == IFDIR)
11972 cgp->cg_cs.cs_ndir++;
11973 cgp->cg_cs.cs_nifree--;
11974 setbit(inosused, ino);
11977 jaddref->ja_state &= ~UNDONE;
11978 jaddref->ja_state |= ATTACHED;
11979 free_jaddref(jaddref);
11983 * Restore any block allocations which are pending journal writes.
11985 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11986 cgp = (struct cg *)bp->b_data;
11987 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11988 blksfree = cg_blksfree(cgp);
11989 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
11991 if ((jnewblk->jn_state & UNDONE) == 0)
11993 /* Do the roll-forward only if it's a real copy. */
11995 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
11997 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
11998 jnewblk->jn_state |= ATTACHED;
11999 free_jnewblk(jnewblk);
12003 * If the write did not succeed, we have done all the roll-forward
12004 * operations, but we cannot take the actions that will allow its
12005 * dependencies to be processed.
12007 if ((flags & WRITESUCCEEDED) == 0) {
12008 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12010 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12011 worklist, wk_list);
12016 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12017 newblk->nb_state |= DEPCOMPLETE;
12018 newblk->nb_state &= ~ONDEPLIST;
12019 newblk->nb_bmsafemap = NULL;
12020 LIST_REMOVE(newblk, nb_deps);
12021 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12022 handle_allocdirect_partdone(
12023 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12024 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12025 handle_allocindir_partdone(
12026 WK_ALLOCINDIR(&newblk->nb_list));
12027 else if (newblk->nb_list.wk_type != D_NEWBLK)
12028 panic("handle_written_bmsafemap: Unexpected type: %s",
12029 TYPENAME(newblk->nb_list.wk_type));
12031 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12032 inodedep->id_state |= DEPCOMPLETE;
12033 inodedep->id_state &= ~ONDEPLIST;
12034 LIST_REMOVE(inodedep, id_deps);
12035 inodedep->id_bmsafemap = NULL;
12037 LIST_REMOVE(bmsafemap, sm_next);
12038 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12039 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12040 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12041 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12042 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12043 LIST_REMOVE(bmsafemap, sm_hash);
12044 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12047 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12054 * Try to free a mkdir dependency.
12057 complete_mkdir(mkdir)
12058 struct mkdir *mkdir;
12060 struct diradd *dap;
12062 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12064 LIST_REMOVE(mkdir, md_mkdirs);
12065 dap = mkdir->md_diradd;
12066 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12067 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12068 dap->da_state |= DEPCOMPLETE;
12069 complete_diradd(dap);
12071 WORKITEM_FREE(mkdir, D_MKDIR);
12075 * Handle the completion of a mkdir dependency.
12078 handle_written_mkdir(mkdir, type)
12079 struct mkdir *mkdir;
12083 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12084 panic("handle_written_mkdir: bad type");
12085 mkdir->md_state |= COMPLETE;
12086 complete_mkdir(mkdir);
12090 free_pagedep(pagedep)
12091 struct pagedep *pagedep;
12095 if (pagedep->pd_state & NEWBLOCK)
12097 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12099 for (i = 0; i < DAHASHSZ; i++)
12100 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12102 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12104 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12106 if (pagedep->pd_state & ONWORKLIST)
12107 WORKLIST_REMOVE(&pagedep->pd_list);
12108 LIST_REMOVE(pagedep, pd_hash);
12109 WORKITEM_FREE(pagedep, D_PAGEDEP);
12115 * Called from within softdep_disk_write_complete above.
12116 * A write operation was just completed. Removed inodes can
12117 * now be freed and associated block pointers may be committed.
12118 * Note that this routine is always called from interrupt level
12119 * with further interrupts from this device blocked.
12121 * If the write did not succeed, we will do all the roll-forward
12122 * operations, but we will not take the actions that will allow its
12123 * dependencies to be processed.
12126 handle_written_filepage(pagedep, bp, flags)
12127 struct pagedep *pagedep;
12128 struct buf *bp; /* buffer containing the written page */
12131 struct dirrem *dirrem;
12132 struct diradd *dap, *nextdap;
12136 if ((pagedep->pd_state & IOSTARTED) == 0)
12137 panic("handle_written_filepage: not started");
12138 pagedep->pd_state &= ~IOSTARTED;
12139 if ((flags & WRITESUCCEEDED) == 0)
12142 * Process any directory removals that have been committed.
12144 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12145 LIST_REMOVE(dirrem, dm_next);
12146 dirrem->dm_state |= COMPLETE;
12147 dirrem->dm_dirinum = pagedep->pd_ino;
12148 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12149 ("handle_written_filepage: Journal entries not written."));
12150 add_to_worklist(&dirrem->dm_list, 0);
12153 * Free any directory additions that have been committed.
12154 * If it is a newly allocated block, we have to wait until
12155 * the on-disk directory inode claims the new block.
12157 if ((pagedep->pd_state & NEWBLOCK) == 0)
12158 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12159 free_diradd(dap, NULL);
12162 * Uncommitted directory entries must be restored.
12164 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12165 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12167 nextdap = LIST_NEXT(dap, da_pdlist);
12168 if (dap->da_state & ATTACHED)
12169 panic("handle_written_filepage: attached");
12170 ep = (struct direct *)
12171 ((char *)bp->b_data + dap->da_offset);
12172 ep->d_ino = dap->da_newinum;
12173 dap->da_state &= ~UNDONE;
12174 dap->da_state |= ATTACHED;
12177 * If the inode referenced by the directory has
12178 * been written out, then the dependency can be
12179 * moved to the pending list.
12181 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12182 LIST_REMOVE(dap, da_pdlist);
12183 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12189 * If there were any rollbacks in the directory, then it must be
12190 * marked dirty so that its will eventually get written back in
12191 * its correct form.
12193 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12194 if ((bp->b_flags & B_DELWRI) == 0)
12200 * If we are not waiting for a new directory block to be
12201 * claimed by its inode, then the pagedep will be freed.
12202 * Otherwise it will remain to track any new entries on
12203 * the page in case they are fsync'ed.
12205 free_pagedep(pagedep);
12210 * Writing back in-core inode structures.
12212 * The filesystem only accesses an inode's contents when it occupies an
12213 * "in-core" inode structure. These "in-core" structures are separate from
12214 * the page frames used to cache inode blocks. Only the latter are
12215 * transferred to/from the disk. So, when the updated contents of the
12216 * "in-core" inode structure are copied to the corresponding in-memory inode
12217 * block, the dependencies are also transferred. The following procedure is
12218 * called when copying a dirty "in-core" inode to a cached inode block.
12222 * Called when an inode is loaded from disk. If the effective link count
12223 * differed from the actual link count when it was last flushed, then we
12224 * need to ensure that the correct effective link count is put back.
12227 softdep_load_inodeblock(ip)
12228 struct inode *ip; /* the "in_core" copy of the inode */
12230 struct inodedep *inodedep;
12231 struct ufsmount *ump;
12234 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12235 ("softdep_load_inodeblock called on non-softdep filesystem"));
12237 * Check for alternate nlink count.
12239 ip->i_effnlink = ip->i_nlink;
12241 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12245 ip->i_effnlink -= inodedep->id_nlinkdelta;
12250 * This routine is called just before the "in-core" inode
12251 * information is to be copied to the in-memory inode block.
12252 * Recall that an inode block contains several inodes. If
12253 * the force flag is set, then the dependencies will be
12254 * cleared so that the update can always be made. Note that
12255 * the buffer is locked when this routine is called, so we
12256 * will never be in the middle of writing the inode block
12260 softdep_update_inodeblock(ip, bp, waitfor)
12261 struct inode *ip; /* the "in_core" copy of the inode */
12262 struct buf *bp; /* the buffer containing the inode block */
12263 int waitfor; /* nonzero => update must be allowed */
12265 struct inodedep *inodedep;
12266 struct inoref *inoref;
12267 struct ufsmount *ump;
12268 struct worklist *wk;
12275 mp = UFSTOVFS(ump);
12276 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12277 ("softdep_update_inodeblock called on non-softdep filesystem"));
12280 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12281 * does not have access to the in-core ip so must write directly into
12282 * the inode block buffer when setting freelink.
12284 if (fs->fs_magic == FS_UFS1_MAGIC)
12285 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12286 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12288 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12289 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12291 * If the effective link count is not equal to the actual link
12292 * count, then we must track the difference in an inodedep while
12293 * the inode is (potentially) tossed out of the cache. Otherwise,
12294 * if there is no existing inodedep, then there are no dependencies
12299 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12301 if (ip->i_effnlink != ip->i_nlink)
12302 panic("softdep_update_inodeblock: bad link count");
12305 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12306 panic("softdep_update_inodeblock: bad delta");
12308 * If we're flushing all dependencies we must also move any waiting
12309 * for journal writes onto the bufwait list prior to I/O.
12312 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12313 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12315 jwait(&inoref->if_list, MNT_WAIT);
12321 * Changes have been initiated. Anything depending on these
12322 * changes cannot occur until this inode has been written.
12324 inodedep->id_state &= ~COMPLETE;
12325 if ((inodedep->id_state & ONWORKLIST) == 0)
12326 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12328 * Any new dependencies associated with the incore inode must
12329 * now be moved to the list associated with the buffer holding
12330 * the in-memory copy of the inode. Once merged process any
12331 * allocdirects that are completed by the merger.
12333 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12334 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12335 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12337 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12338 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12339 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12342 * Now that the inode has been pushed into the buffer, the
12343 * operations dependent on the inode being written to disk
12344 * can be moved to the id_bufwait so that they will be
12345 * processed when the buffer I/O completes.
12347 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12348 WORKLIST_REMOVE(wk);
12349 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12352 * Newly allocated inodes cannot be written until the bitmap
12353 * that allocates them have been written (indicated by
12354 * DEPCOMPLETE being set in id_state). If we are doing a
12355 * forced sync (e.g., an fsync on a file), we force the bitmap
12356 * to be written so that the update can be done.
12358 if (waitfor == 0) {
12363 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12367 ibp = inodedep->id_bmsafemap->sm_buf;
12368 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12371 * If ibp came back as NULL, the dependency could have been
12372 * freed while we slept. Look it up again, and check to see
12373 * that it has completed.
12375 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12381 if ((error = bwrite(ibp)) != 0)
12382 softdep_error("softdep_update_inodeblock: bwrite", error);
12386 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12387 * old inode dependency list (such as id_inoupdt).
12390 merge_inode_lists(newlisthead, oldlisthead)
12391 struct allocdirectlst *newlisthead;
12392 struct allocdirectlst *oldlisthead;
12394 struct allocdirect *listadp, *newadp;
12396 newadp = TAILQ_FIRST(newlisthead);
12397 if (newadp != NULL)
12398 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12399 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12400 if (listadp->ad_offset < newadp->ad_offset) {
12401 listadp = TAILQ_NEXT(listadp, ad_next);
12404 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12405 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12406 if (listadp->ad_offset == newadp->ad_offset) {
12407 allocdirect_merge(oldlisthead, newadp,
12411 newadp = TAILQ_FIRST(newlisthead);
12413 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12414 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12415 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12420 * If we are doing an fsync, then we must ensure that any directory
12421 * entries for the inode have been written after the inode gets to disk.
12425 struct vnode *vp; /* the "in_core" copy of the inode */
12427 struct inodedep *inodedep;
12428 struct pagedep *pagedep;
12429 struct inoref *inoref;
12430 struct ufsmount *ump;
12431 struct worklist *wk;
12432 struct diradd *dap;
12438 struct thread *td = curthread;
12439 int error, flushparent, pagedep_new_block;
12445 ump = VFSTOUFS(mp);
12447 if (MOUNTEDSOFTDEP(mp) == 0)
12451 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12455 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12456 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12458 jwait(&inoref->if_list, MNT_WAIT);
12462 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12463 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12464 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12465 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12466 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12467 panic("softdep_fsync: pending ops %p", inodedep);
12468 for (error = 0, flushparent = 0; ; ) {
12469 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12471 if (wk->wk_type != D_DIRADD)
12472 panic("softdep_fsync: Unexpected type %s",
12473 TYPENAME(wk->wk_type));
12474 dap = WK_DIRADD(wk);
12476 * Flush our parent if this directory entry has a MKDIR_PARENT
12477 * dependency or is contained in a newly allocated block.
12479 if (dap->da_state & DIRCHG)
12480 pagedep = dap->da_previous->dm_pagedep;
12482 pagedep = dap->da_pagedep;
12483 parentino = pagedep->pd_ino;
12484 lbn = pagedep->pd_lbn;
12485 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12486 panic("softdep_fsync: dirty");
12487 if ((dap->da_state & MKDIR_PARENT) ||
12488 (pagedep->pd_state & NEWBLOCK))
12493 * If we are being fsync'ed as part of vgone'ing this vnode,
12494 * then we will not be able to release and recover the
12495 * vnode below, so we just have to give up on writing its
12496 * directory entry out. It will eventually be written, just
12497 * not now, but then the user was not asking to have it
12498 * written, so we are not breaking any promises.
12500 if (vp->v_iflag & VI_DOOMED)
12503 * We prevent deadlock by always fetching inodes from the
12504 * root, moving down the directory tree. Thus, when fetching
12505 * our parent directory, we first try to get the lock. If
12506 * that fails, we must unlock ourselves before requesting
12507 * the lock on our parent. See the comment in ufs_lookup
12508 * for details on possible races.
12511 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
12512 FFSV_FORCEINSMQ)) {
12513 error = vfs_busy(mp, MBF_NOWAIT);
12517 error = vfs_busy(mp, 0);
12518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12522 if (vp->v_iflag & VI_DOOMED) {
12528 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
12529 &pvp, FFSV_FORCEINSMQ);
12531 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12532 if (vp->v_iflag & VI_DOOMED) {
12541 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12542 * that are contained in direct blocks will be resolved by
12543 * doing a ffs_update. Pagedeps contained in indirect blocks
12544 * may require a complete sync'ing of the directory. So, we
12545 * try the cheap and fast ffs_update first, and if that fails,
12546 * then we do the slower ffs_syncvnode of the directory.
12551 if ((error = ffs_update(pvp, 1)) != 0) {
12557 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12558 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12559 if (wk->wk_type != D_DIRADD)
12560 panic("softdep_fsync: Unexpected type %s",
12561 TYPENAME(wk->wk_type));
12562 dap = WK_DIRADD(wk);
12563 if (dap->da_state & DIRCHG)
12564 pagedep = dap->da_previous->dm_pagedep;
12566 pagedep = dap->da_pagedep;
12567 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12570 if (pagedep_new_block && (error =
12571 ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12581 * Flush directory page containing the inode's name.
12583 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12586 error = bwrite(bp);
12593 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12601 * Flush all the dirty bitmaps associated with the block device
12602 * before flushing the rest of the dirty blocks so as to reduce
12603 * the number of dependencies that will have to be rolled back.
12608 softdep_fsync_mountdev(vp)
12611 struct buf *bp, *nbp;
12612 struct worklist *wk;
12615 if (!vn_isdisk(vp, NULL))
12616 panic("softdep_fsync_mountdev: vnode not a disk");
12617 bo = &vp->v_bufobj;
12620 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12622 * If it is already scheduled, skip to the next buffer.
12624 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12627 if ((bp->b_flags & B_DELWRI) == 0)
12628 panic("softdep_fsync_mountdev: not dirty");
12630 * We are only interested in bitmaps with outstanding
12633 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12634 wk->wk_type != D_BMSAFEMAP ||
12635 (bp->b_vflags & BV_BKGRDINPROG)) {
12641 (void) bawrite(bp);
12649 * Sync all cylinder groups that were dirty at the time this function is
12650 * called. Newly dirtied cgs will be inserted before the sentinel. This
12651 * is used to flush freedep activity that may be holding up writes to a
12655 sync_cgs(mp, waitfor)
12659 struct bmsafemap *bmsafemap;
12660 struct bmsafemap *sentinel;
12661 struct ufsmount *ump;
12665 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12666 sentinel->sm_cg = -1;
12667 ump = VFSTOUFS(mp);
12670 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12671 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12672 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12673 /* Skip sentinels and cgs with no work to release. */
12674 if (bmsafemap->sm_cg == -1 ||
12675 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12676 LIST_EMPTY(&bmsafemap->sm_freewr))) {
12677 LIST_REMOVE(sentinel, sm_next);
12678 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12682 * If we don't get the lock and we're waiting try again, if
12683 * not move on to the next buf and try to sync it.
12685 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12686 if (bp == NULL && waitfor == MNT_WAIT)
12688 LIST_REMOVE(sentinel, sm_next);
12689 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12693 if (waitfor == MNT_NOWAIT)
12696 error = bwrite(bp);
12701 LIST_REMOVE(sentinel, sm_next);
12703 free(sentinel, M_BMSAFEMAP);
12708 * This routine is called when we are trying to synchronously flush a
12709 * file. This routine must eliminate any filesystem metadata dependencies
12710 * so that the syncing routine can succeed.
12713 softdep_sync_metadata(struct vnode *vp)
12719 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12720 ("softdep_sync_metadata called on non-softdep filesystem"));
12722 * Ensure that any direct block dependencies have been cleared,
12723 * truncations are started, and inode references are journaled.
12725 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
12727 * Write all journal records to prevent rollbacks on devvp.
12729 if (vp->v_type == VCHR)
12730 softdep_flushjournal(vp->v_mount);
12731 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12733 * Ensure that all truncates are written so we won't find deps on
12736 process_truncates(vp);
12737 FREE_LOCK(VFSTOUFS(vp->v_mount));
12743 * This routine is called when we are attempting to sync a buf with
12744 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
12745 * other IO it can but returns EBUSY if the buffer is not yet able to
12746 * be written. Dependencies which will not cause rollbacks will always
12750 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
12752 struct indirdep *indirdep;
12753 struct pagedep *pagedep;
12754 struct allocindir *aip;
12755 struct newblk *newblk;
12756 struct ufsmount *ump;
12758 struct worklist *wk;
12761 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12762 ("softdep_sync_buf called on non-softdep filesystem"));
12764 * For VCHR we just don't want to force flush any dependencies that
12765 * will cause rollbacks.
12767 if (vp->v_type == VCHR) {
12768 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12772 ump = VFSTOUFS(vp->v_mount);
12775 * As we hold the buffer locked, none of its dependencies
12780 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12781 switch (wk->wk_type) {
12783 case D_ALLOCDIRECT:
12785 newblk = WK_NEWBLK(wk);
12786 if (newblk->nb_jnewblk != NULL) {
12787 if (waitfor == MNT_NOWAIT) {
12791 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
12794 if (newblk->nb_state & DEPCOMPLETE ||
12795 waitfor == MNT_NOWAIT)
12797 nbp = newblk->nb_bmsafemap->sm_buf;
12798 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12802 if ((error = bwrite(nbp)) != 0)
12808 indirdep = WK_INDIRDEP(wk);
12809 if (waitfor == MNT_NOWAIT) {
12810 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
12811 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
12816 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
12817 panic("softdep_sync_buf: truncation pending.");
12819 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
12820 newblk = (struct newblk *)aip;
12821 if (newblk->nb_jnewblk != NULL) {
12822 jwait(&newblk->nb_jnewblk->jn_list,
12826 if (newblk->nb_state & DEPCOMPLETE)
12828 nbp = newblk->nb_bmsafemap->sm_buf;
12829 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12833 if ((error = bwrite(nbp)) != 0)
12842 * Only flush directory entries in synchronous passes.
12844 if (waitfor != MNT_WAIT) {
12849 * While syncing snapshots, we must allow recursive
12854 * We are trying to sync a directory that may
12855 * have dependencies on both its own metadata
12856 * and/or dependencies on the inodes of any
12857 * recently allocated files. We walk its diradd
12858 * lists pushing out the associated inode.
12860 pagedep = WK_PAGEDEP(wk);
12861 for (i = 0; i < DAHASHSZ; i++) {
12862 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
12864 if ((error = flush_pagedep_deps(vp, wk->wk_mp,
12865 &pagedep->pd_diraddhd[i]))) {
12880 panic("softdep_sync_buf: Unknown type %s",
12881 TYPENAME(wk->wk_type));
12892 * Flush the dependencies associated with an inodedep.
12895 flush_inodedep_deps(vp, mp, ino)
12900 struct inodedep *inodedep;
12901 struct inoref *inoref;
12902 struct ufsmount *ump;
12903 int error, waitfor;
12906 * This work is done in two passes. The first pass grabs most
12907 * of the buffers and begins asynchronously writing them. The
12908 * only way to wait for these asynchronous writes is to sleep
12909 * on the filesystem vnode which may stay busy for a long time
12910 * if the filesystem is active. So, instead, we make a second
12911 * pass over the dependencies blocking on each write. In the
12912 * usual case we will be blocking against a write that we
12913 * initiated, so when it is done the dependency will have been
12914 * resolved. Thus the second pass is expected to end quickly.
12915 * We give a brief window at the top of the loop to allow
12916 * any pending I/O to complete.
12918 ump = VFSTOUFS(mp);
12920 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
12926 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
12928 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12929 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12931 jwait(&inoref->if_list, MNT_WAIT);
12935 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
12936 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
12937 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
12938 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
12941 * If pass2, we are done, otherwise do pass 2.
12943 if (waitfor == MNT_WAIT)
12945 waitfor = MNT_WAIT;
12948 * Try freeing inodedep in case all dependencies have been removed.
12950 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
12951 (void) free_inodedep(inodedep);
12956 * Flush an inode dependency list.
12959 flush_deplist(listhead, waitfor, errorp)
12960 struct allocdirectlst *listhead;
12964 struct allocdirect *adp;
12965 struct newblk *newblk;
12966 struct ufsmount *ump;
12969 if ((adp = TAILQ_FIRST(listhead)) == NULL)
12971 ump = VFSTOUFS(adp->ad_list.wk_mp);
12973 TAILQ_FOREACH(adp, listhead, ad_next) {
12974 newblk = (struct newblk *)adp;
12975 if (newblk->nb_jnewblk != NULL) {
12976 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
12979 if (newblk->nb_state & DEPCOMPLETE)
12981 bp = newblk->nb_bmsafemap->sm_buf;
12982 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
12984 if (waitfor == MNT_NOWAIT)
12989 if (waitfor == MNT_NOWAIT)
12992 *errorp = bwrite(bp);
13000 * Flush dependencies associated with an allocdirect block.
13003 flush_newblk_dep(vp, mp, lbn)
13008 struct newblk *newblk;
13009 struct ufsmount *ump;
13013 ufs2_daddr_t blkno;
13017 bo = &vp->v_bufobj;
13019 blkno = DIP(ip, i_db[lbn]);
13021 panic("flush_newblk_dep: Missing block");
13022 ump = VFSTOUFS(mp);
13025 * Loop until all dependencies related to this block are satisfied.
13026 * We must be careful to restart after each sleep in case a write
13027 * completes some part of this process for us.
13030 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13034 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13035 panic("flush_newblk_dep: Bad newblk %p", newblk);
13037 * Flush the journal.
13039 if (newblk->nb_jnewblk != NULL) {
13040 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13044 * Write the bitmap dependency.
13046 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13047 bp = newblk->nb_bmsafemap->sm_buf;
13048 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13052 error = bwrite(bp);
13059 * Write the buffer.
13063 bp = gbincore(bo, lbn);
13065 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13066 LK_INTERLOCK, BO_LOCKPTR(bo));
13067 if (error == ENOLCK) {
13070 continue; /* Slept, retry */
13073 break; /* Failed */
13074 if (bp->b_flags & B_DELWRI) {
13076 error = bwrite(bp);
13084 * We have to wait for the direct pointers to
13085 * point at the newdirblk before the dependency
13088 error = ffs_update(vp, 1);
13097 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13100 flush_pagedep_deps(pvp, mp, diraddhdp)
13103 struct diraddhd *diraddhdp;
13105 struct inodedep *inodedep;
13106 struct inoref *inoref;
13107 struct ufsmount *ump;
13108 struct diradd *dap;
13113 struct diraddhd unfinished;
13115 LIST_INIT(&unfinished);
13116 ump = VFSTOUFS(mp);
13119 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13121 * Flush ourselves if this directory entry
13122 * has a MKDIR_PARENT dependency.
13124 if (dap->da_state & MKDIR_PARENT) {
13126 if ((error = ffs_update(pvp, 1)) != 0)
13130 * If that cleared dependencies, go on to next.
13132 if (dap != LIST_FIRST(diraddhdp))
13135 * All MKDIR_PARENT dependencies and all the
13136 * NEWBLOCK pagedeps that are contained in direct
13137 * blocks were resolved by doing above ffs_update.
13138 * Pagedeps contained in indirect blocks may
13139 * require a complete sync'ing of the directory.
13140 * We are in the midst of doing a complete sync,
13141 * so if they are not resolved in this pass we
13142 * defer them for now as they will be sync'ed by
13143 * our caller shortly.
13145 LIST_REMOVE(dap, da_pdlist);
13146 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13150 * A newly allocated directory must have its "." and
13151 * ".." entries written out before its name can be
13152 * committed in its parent.
13154 inum = dap->da_newinum;
13155 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13156 panic("flush_pagedep_deps: lost inode1");
13158 * Wait for any pending journal adds to complete so we don't
13159 * cause rollbacks while syncing.
13161 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13162 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13164 jwait(&inoref->if_list, MNT_WAIT);
13168 if (dap->da_state & MKDIR_BODY) {
13170 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13173 error = flush_newblk_dep(vp, mp, 0);
13175 * If we still have the dependency we might need to
13176 * update the vnode to sync the new link count to
13179 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13180 error = ffs_update(vp, 1);
13186 * If that cleared dependencies, go on to next.
13188 if (dap != LIST_FIRST(diraddhdp))
13190 if (dap->da_state & MKDIR_BODY) {
13191 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13193 panic("flush_pagedep_deps: MKDIR_BODY "
13194 "inodedep %p dap %p vp %p",
13195 inodedep, dap, vp);
13199 * Flush the inode on which the directory entry depends.
13200 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13201 * the only remaining dependency is that the updated inode
13202 * count must get pushed to disk. The inode has already
13203 * been pushed into its inode buffer (via VOP_UPDATE) at
13204 * the time of the reference count change. So we need only
13205 * locate that buffer, ensure that there will be no rollback
13206 * caused by a bitmap dependency, then write the inode buffer.
13209 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13210 panic("flush_pagedep_deps: lost inode");
13212 * If the inode still has bitmap dependencies,
13213 * push them to disk.
13215 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13216 bp = inodedep->id_bmsafemap->sm_buf;
13217 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13221 if ((error = bwrite(bp)) != 0)
13224 if (dap != LIST_FIRST(diraddhdp))
13228 * If the inode is still sitting in a buffer waiting
13229 * to be written or waiting for the link count to be
13230 * adjusted update it here to flush it to disk.
13232 if (dap == LIST_FIRST(diraddhdp)) {
13234 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13237 error = ffs_update(vp, 1);
13244 * If we have failed to get rid of all the dependencies
13245 * then something is seriously wrong.
13247 if (dap == LIST_FIRST(diraddhdp)) {
13248 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13249 panic("flush_pagedep_deps: failed to flush "
13250 "inodedep %p ino %ju dap %p",
13251 inodedep, (uintmax_t)inum, dap);
13256 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13257 LIST_REMOVE(dap, da_pdlist);
13258 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13264 * A large burst of file addition or deletion activity can drive the
13265 * memory load excessively high. First attempt to slow things down
13266 * using the techniques below. If that fails, this routine requests
13267 * the offending operations to fall back to running synchronously
13268 * until the memory load returns to a reasonable level.
13271 softdep_slowdown(vp)
13274 struct ufsmount *ump;
13276 int max_softdeps_hard;
13278 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13279 ("softdep_slowdown called on non-softdep filesystem"));
13280 ump = VFSTOUFS(vp->v_mount);
13284 * Check for journal space if needed.
13286 if (DOINGSUJ(vp)) {
13287 if (journal_space(ump, 0) == 0)
13291 * If the system is under its limits and our filesystem is
13292 * not responsible for more than our share of the usage and
13293 * we are not low on journal space, then no need to slow down.
13295 max_softdeps_hard = max_softdeps * 11 / 10;
13296 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13297 dep_current[D_INODEDEP] < max_softdeps_hard &&
13298 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13299 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13300 ump->softdep_curdeps[D_DIRREM] <
13301 (max_softdeps_hard / 2) / stat_flush_threads &&
13302 ump->softdep_curdeps[D_INODEDEP] <
13303 max_softdeps_hard / stat_flush_threads &&
13304 ump->softdep_curdeps[D_INDIRDEP] <
13305 (max_softdeps_hard / 1000) / stat_flush_threads &&
13306 ump->softdep_curdeps[D_FREEBLKS] <
13307 max_softdeps_hard / stat_flush_threads) {
13312 * If the journal is low or our filesystem is over its limit
13313 * then speedup the cleanup.
13315 if (ump->softdep_curdeps[D_INDIRDEP] <
13316 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13317 softdep_speedup(ump);
13318 stat_sync_limit_hit += 1;
13321 * We only slow down the rate at which new dependencies are
13322 * generated if we are not using journaling. With journaling,
13323 * the cleanup should always be sufficient to keep things
13332 * Called by the allocation routines when they are about to fail
13333 * in the hope that we can free up the requested resource (inodes
13336 * First check to see if the work list has anything on it. If it has,
13337 * clean up entries until we successfully free the requested resource.
13338 * Because this process holds inodes locked, we cannot handle any remove
13339 * requests that might block on a locked inode as that could lead to
13340 * deadlock. If the worklist yields none of the requested resource,
13341 * start syncing out vnodes to free up the needed space.
13344 softdep_request_cleanup(fs, vp, cred, resource)
13347 struct ucred *cred;
13350 struct ufsmount *ump;
13353 ufs2_daddr_t needed;
13354 int error, failed_vnode;
13357 * If we are being called because of a process doing a
13358 * copy-on-write, then it is not safe to process any
13359 * worklist items as we will recurse into the copyonwrite
13360 * routine. This will result in an incoherent snapshot.
13361 * If the vnode that we hold is a snapshot, we must avoid
13362 * handling other resources that could cause deadlock.
13364 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13367 if (resource == FLUSH_BLOCKS_WAIT)
13368 stat_cleanup_blkrequests += 1;
13370 stat_cleanup_inorequests += 1;
13373 ump = VFSTOUFS(mp);
13374 mtx_assert(UFS_MTX(ump), MA_OWNED);
13376 error = ffs_update(vp, 1);
13377 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13382 * If we are in need of resources, start by cleaning up
13383 * any block removals associated with our inode.
13386 process_removes(vp);
13387 process_truncates(vp);
13390 * Now clean up at least as many resources as we will need.
13392 * When requested to clean up inodes, the number that are needed
13393 * is set by the number of simultaneous writers (mnt_writeopcount)
13394 * plus a bit of slop (2) in case some more writers show up while
13397 * When requested to free up space, the amount of space that
13398 * we need is enough blocks to allocate a full-sized segment
13399 * (fs_contigsumsize). The number of such segments that will
13400 * be needed is set by the number of simultaneous writers
13401 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13402 * writers show up while we are cleaning.
13404 * Additionally, if we are unpriviledged and allocating space,
13405 * we need to ensure that we clean up enough blocks to get the
13406 * needed number of blocks over the threshold of the minimum
13407 * number of blocks required to be kept free by the filesystem
13410 if (resource == FLUSH_INODES_WAIT) {
13411 needed = vp->v_mount->mnt_writeopcount + 2;
13412 } else if (resource == FLUSH_BLOCKS_WAIT) {
13413 needed = (vp->v_mount->mnt_writeopcount + 2) *
13414 fs->fs_contigsumsize;
13415 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13416 needed += fragstoblks(fs,
13417 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13418 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13421 printf("softdep_request_cleanup: Unknown resource type %d\n",
13425 starttime = time_second;
13427 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13428 fs->fs_cstotal.cs_nbfree <= needed) ||
13429 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13430 fs->fs_cstotal.cs_nifree <= needed)) {
13432 if (ump->softdep_on_worklist > 0 &&
13433 process_worklist_item(UFSTOVFS(ump),
13434 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13435 stat_worklist_push += 1;
13439 * If we still need resources and there are no more worklist
13440 * entries to process to obtain them, we have to start flushing
13441 * the dirty vnodes to force the release of additional requests
13442 * to the worklist that we can then process to reap addition
13443 * resources. We walk the vnodes associated with the mount point
13444 * until we get the needed worklist requests that we can reap.
13446 * If there are several threads all needing to clean the same
13447 * mount point, only one is allowed to walk the mount list.
13448 * When several threads all try to walk the same mount list,
13449 * they end up competing with each other and often end up in
13450 * livelock. This approach ensures that forward progress is
13451 * made at the cost of occational ENOSPC errors being returned
13452 * that might otherwise have been avoided.
13455 if ((resource == FLUSH_BLOCKS_WAIT &&
13456 fs->fs_cstotal.cs_nbfree <= needed) ||
13457 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13458 fs->fs_cstotal.cs_nifree <= needed)) {
13460 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13461 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13463 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13465 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13467 if (ump->softdep_on_worklist > 0) {
13468 stat_cleanup_retries += 1;
13476 stat_cleanup_failures += 1;
13478 if (time_second - starttime > stat_cleanup_high_delay)
13479 stat_cleanup_high_delay = time_second - starttime;
13485 * Scan the vnodes for the specified mount point flushing out any
13486 * vnodes that can be locked without waiting. Finally, try to flush
13487 * the device associated with the mount point if it can be locked
13490 * We return 0 if we were able to lock every vnode in our scan.
13491 * If we had to skip one or more vnodes, we return 1.
13494 softdep_request_cleanup_flush(mp, ump)
13496 struct ufsmount *ump;
13499 struct vnode *lvp, *mvp;
13504 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13505 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13509 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT,
13514 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
13518 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13521 lvp = ump->um_devvp;
13522 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13523 VOP_FSYNC(lvp, MNT_NOWAIT, td);
13524 VOP_UNLOCK(lvp, 0);
13526 return (failed_vnode);
13530 softdep_excess_items(struct ufsmount *ump, int item)
13533 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13534 return (dep_current[item] > max_softdeps &&
13535 ump->softdep_curdeps[item] > max_softdeps /
13536 stat_flush_threads);
13540 schedule_cleanup(struct mount *mp)
13542 struct ufsmount *ump;
13545 ump = VFSTOUFS(mp);
13549 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13550 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13552 * No ast is delivered to kernel threads, so nobody
13553 * would deref the mp. Some kernel threads
13554 * explicitely check for AST, e.g. NFS daemon does
13555 * this in the serving loop.
13559 if (td->td_su != NULL)
13560 vfs_rel(td->td_su);
13564 td->td_flags |= TDF_ASTPENDING;
13569 softdep_ast_cleanup_proc(struct thread *td)
13572 struct ufsmount *ump;
13576 while ((mp = td->td_su) != NULL) {
13578 error = vfs_busy(mp, MBF_NOWAIT);
13582 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13583 ump = VFSTOUFS(mp);
13587 if (softdep_excess_items(ump, D_INODEDEP)) {
13589 request_cleanup(mp, FLUSH_INODES);
13591 if (softdep_excess_items(ump, D_DIRREM)) {
13593 request_cleanup(mp, FLUSH_BLOCKS);
13596 if (softdep_excess_items(ump, D_NEWBLK) ||
13597 softdep_excess_items(ump, D_ALLOCDIRECT) ||
13598 softdep_excess_items(ump, D_ALLOCINDIR)) {
13599 error = vn_start_write(NULL, &mp,
13603 VFS_SYNC(mp, MNT_WAIT);
13604 vn_finished_write(mp);
13607 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13613 if ((mp = td->td_su) != NULL) {
13620 * If memory utilization has gotten too high, deliberately slow things
13621 * down and speed up the I/O processing.
13624 request_cleanup(mp, resource)
13628 struct thread *td = curthread;
13629 struct ufsmount *ump;
13631 ump = VFSTOUFS(mp);
13634 * We never hold up the filesystem syncer or buf daemon.
13636 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13639 * First check to see if the work list has gotten backlogged.
13640 * If it has, co-opt this process to help clean up two entries.
13641 * Because this process may hold inodes locked, we cannot
13642 * handle any remove requests that might block on a locked
13643 * inode as that could lead to deadlock. We set TDP_SOFTDEP
13644 * to avoid recursively processing the worklist.
13646 if (ump->softdep_on_worklist > max_softdeps / 10) {
13647 td->td_pflags |= TDP_SOFTDEP;
13648 process_worklist_item(mp, 2, LK_NOWAIT);
13649 td->td_pflags &= ~TDP_SOFTDEP;
13650 stat_worklist_push += 2;
13654 * Next, we attempt to speed up the syncer process. If that
13655 * is successful, then we allow the process to continue.
13657 if (softdep_speedup(ump) &&
13658 resource != FLUSH_BLOCKS_WAIT &&
13659 resource != FLUSH_INODES_WAIT)
13662 * If we are resource constrained on inode dependencies, try
13663 * flushing some dirty inodes. Otherwise, we are constrained
13664 * by file deletions, so try accelerating flushes of directories
13665 * with removal dependencies. We would like to do the cleanup
13666 * here, but we probably hold an inode locked at this point and
13667 * that might deadlock against one that we try to clean. So,
13668 * the best that we can do is request the syncer daemon to do
13669 * the cleanup for us.
13671 switch (resource) {
13674 case FLUSH_INODES_WAIT:
13675 ACQUIRE_GBLLOCK(&lk);
13676 stat_ino_limit_push += 1;
13677 req_clear_inodedeps += 1;
13679 stat_countp = &stat_ino_limit_hit;
13683 case FLUSH_BLOCKS_WAIT:
13684 ACQUIRE_GBLLOCK(&lk);
13685 stat_blk_limit_push += 1;
13686 req_clear_remove += 1;
13688 stat_countp = &stat_blk_limit_hit;
13692 panic("request_cleanup: unknown type");
13695 * Hopefully the syncer daemon will catch up and awaken us.
13696 * We wait at most tickdelay before proceeding in any case.
13698 ACQUIRE_GBLLOCK(&lk);
13701 if (callout_pending(&softdep_callout) == FALSE)
13702 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13705 if ((td->td_pflags & TDP_KTHREAD) == 0)
13706 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13714 * Awaken processes pausing in request_cleanup and clear proc_waiting
13715 * to indicate that there is no longer a timer running. Pause_timer
13716 * will be called with the global softdep mutex (&lk) locked.
13723 GBLLOCK_OWNED(&lk);
13725 * The callout_ API has acquired mtx and will hold it around this
13728 *stat_countp += proc_waiting;
13729 wakeup(&proc_waiting);
13733 * If requested, try removing inode or removal dependencies.
13736 check_clear_deps(mp)
13741 * If we are suspended, it may be because of our using
13742 * too many inodedeps, so help clear them out.
13744 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended)
13745 clear_inodedeps(mp);
13747 * General requests for cleanup of backed up dependencies
13749 ACQUIRE_GBLLOCK(&lk);
13750 if (req_clear_inodedeps) {
13751 req_clear_inodedeps -= 1;
13753 clear_inodedeps(mp);
13754 ACQUIRE_GBLLOCK(&lk);
13755 wakeup(&proc_waiting);
13757 if (req_clear_remove) {
13758 req_clear_remove -= 1;
13761 ACQUIRE_GBLLOCK(&lk);
13762 wakeup(&proc_waiting);
13768 * Flush out a directory with at least one removal dependency in an effort to
13769 * reduce the number of dirrem, freefile, and freeblks dependency structures.
13775 struct pagedep_hashhead *pagedephd;
13776 struct pagedep *pagedep;
13777 struct ufsmount *ump;
13783 ump = VFSTOUFS(mp);
13786 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
13787 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
13788 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
13789 ump->pagedep_nextclean = 0;
13790 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
13791 if (LIST_EMPTY(&pagedep->pd_dirremhd))
13793 ino = pagedep->pd_ino;
13794 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13799 * Let unmount clear deps
13801 error = vfs_busy(mp, MBF_NOWAIT);
13804 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13808 softdep_error("clear_remove: vget", error);
13811 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13812 softdep_error("clear_remove: fsync", error);
13813 bo = &vp->v_bufobj;
13819 vn_finished_write(mp);
13827 * Clear out a block of dirty inodes in an effort to reduce
13828 * the number of inodedep dependency structures.
13831 clear_inodedeps(mp)
13834 struct inodedep_hashhead *inodedephd;
13835 struct inodedep *inodedep;
13836 struct ufsmount *ump;
13840 ino_t firstino, lastino, ino;
13842 ump = VFSTOUFS(mp);
13846 * Pick a random inode dependency to be cleared.
13847 * We will then gather up all the inodes in its block
13848 * that have dependencies and flush them out.
13850 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
13851 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
13852 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
13853 ump->inodedep_nextclean = 0;
13854 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
13857 if (inodedep == NULL)
13860 * Find the last inode in the block with dependencies.
13862 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
13863 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
13864 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
13867 * Asynchronously push all but the last inode with dependencies.
13868 * Synchronously push the last inode with dependencies to ensure
13869 * that the inode block gets written to free up the inodedeps.
13871 for (ino = firstino; ino <= lastino; ino++) {
13872 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13874 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13877 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
13879 vn_finished_write(mp);
13883 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13884 FFSV_FORCEINSMQ)) != 0) {
13885 softdep_error("clear_inodedeps: vget", error);
13887 vn_finished_write(mp);
13892 if (ino == lastino) {
13893 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)))
13894 softdep_error("clear_inodedeps: fsync1", error);
13896 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13897 softdep_error("clear_inodedeps: fsync2", error);
13898 BO_LOCK(&vp->v_bufobj);
13900 BO_UNLOCK(&vp->v_bufobj);
13903 vn_finished_write(mp);
13909 softdep_buf_append(bp, wkhd)
13911 struct workhead *wkhd;
13913 struct worklist *wk;
13914 struct ufsmount *ump;
13916 if ((wk = LIST_FIRST(wkhd)) == NULL)
13918 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13919 ("softdep_buf_append called on non-softdep filesystem"));
13920 ump = VFSTOUFS(wk->wk_mp);
13922 while ((wk = LIST_FIRST(wkhd)) != NULL) {
13923 WORKLIST_REMOVE(wk);
13924 WORKLIST_INSERT(&bp->b_dep, wk);
13931 softdep_inode_append(ip, cred, wkhd)
13933 struct ucred *cred;
13934 struct workhead *wkhd;
13938 struct ufsmount *ump;
13942 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
13943 ("softdep_inode_append called on non-softdep filesystem"));
13945 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
13946 (int)fs->fs_bsize, cred, &bp);
13949 softdep_freework(wkhd);
13952 softdep_buf_append(bp, wkhd);
13957 softdep_freework(wkhd)
13958 struct workhead *wkhd;
13960 struct worklist *wk;
13961 struct ufsmount *ump;
13963 if ((wk = LIST_FIRST(wkhd)) == NULL)
13965 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13966 ("softdep_freework called on non-softdep filesystem"));
13967 ump = VFSTOUFS(wk->wk_mp);
13969 handle_jwork(wkhd);
13973 static struct ufsmount *
13974 softdep_bp_to_mp(bp)
13980 if (LIST_EMPTY(&bp->b_dep))
13983 KASSERT(vp != NULL,
13984 ("%s, buffer with dependencies lacks vnode", __func__));
13987 * The ump mount point is stable after we get a correct
13988 * pointer, since bp is locked and this prevents unmount from
13989 * proceeding. But to get to it, we cannot dereference bp->b_dep
13990 * head wk_mp, because we do not yet own SU ump lock and
13991 * workitem might be freed while dereferenced.
13994 switch (vp->v_type) {
13997 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14010 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14018 vn_printf(vp, "unknown vnode type");
14022 return (VFSTOUFS(mp));
14026 * Function to determine if the buffer has outstanding dependencies
14027 * that will cause a roll-back if the buffer is written. If wantcount
14028 * is set, return number of dependencies, otherwise just yes or no.
14031 softdep_count_dependencies(bp, wantcount)
14035 struct worklist *wk;
14036 struct ufsmount *ump;
14037 struct bmsafemap *bmsafemap;
14038 struct freework *freework;
14039 struct inodedep *inodedep;
14040 struct indirdep *indirdep;
14041 struct freeblks *freeblks;
14042 struct allocindir *aip;
14043 struct pagedep *pagedep;
14044 struct dirrem *dirrem;
14045 struct newblk *newblk;
14046 struct mkdir *mkdir;
14047 struct diradd *dap;
14050 ump = softdep_bp_to_mp(bp);
14055 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14056 switch (wk->wk_type) {
14059 inodedep = WK_INODEDEP(wk);
14060 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14061 /* bitmap allocation dependency */
14066 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14067 /* direct block pointer dependency */
14072 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14073 /* direct block pointer dependency */
14078 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14079 /* Add reference dependency. */
14087 indirdep = WK_INDIRDEP(wk);
14089 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14090 /* indirect truncation dependency */
14096 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14097 /* indirect block pointer dependency */
14105 pagedep = WK_PAGEDEP(wk);
14106 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14107 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14108 /* Journal remove ref dependency. */
14114 for (i = 0; i < DAHASHSZ; i++) {
14116 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14117 /* directory entry dependency */
14126 bmsafemap = WK_BMSAFEMAP(wk);
14127 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14128 /* Add reference dependency. */
14133 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14134 /* Allocate block dependency. */
14142 freeblks = WK_FREEBLKS(wk);
14143 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14144 /* Freeblk journal dependency. */
14151 case D_ALLOCDIRECT:
14153 newblk = WK_NEWBLK(wk);
14154 if (newblk->nb_jnewblk) {
14155 /* Journal allocate dependency. */
14163 mkdir = WK_MKDIR(wk);
14164 if (mkdir->md_jaddref) {
14165 /* Journal reference dependency. */
14177 /* never a dependency on these blocks */
14181 panic("softdep_count_dependencies: Unexpected type %s",
14182 TYPENAME(wk->wk_type));
14192 * Acquire exclusive access to a buffer.
14193 * Must be called with a locked mtx parameter.
14194 * Return acquired buffer or NULL on failure.
14196 static struct buf *
14197 getdirtybuf(bp, lock, waitfor)
14199 struct rwlock *lock;
14204 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14205 if (waitfor != MNT_WAIT)
14207 error = BUF_LOCK(bp,
14208 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14210 * Even if we successfully acquire bp here, we have dropped
14211 * lock, which may violates our guarantee.
14215 else if (error != ENOLCK)
14216 panic("getdirtybuf: inconsistent lock: %d", error);
14220 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14221 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14223 BO_LOCK(bp->b_bufobj);
14225 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14226 bp->b_vflags |= BV_BKGRDWAIT;
14227 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14228 PRIBIO | PDROP, "getbuf", 0);
14230 BO_UNLOCK(bp->b_bufobj);
14235 if (waitfor != MNT_WAIT)
14237 #ifdef DEBUG_VFS_LOCKS
14238 if (bp->b_vp->v_type != VCHR)
14239 ASSERT_BO_WLOCKED(bp->b_bufobj);
14241 bp->b_vflags |= BV_BKGRDWAIT;
14242 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14245 if ((bp->b_flags & B_DELWRI) == 0) {
14255 * Check if it is safe to suspend the file system now. On entry,
14256 * the vnode interlock for devvp should be held. Return 0 with
14257 * the mount interlock held if the file system can be suspended now,
14258 * otherwise return EAGAIN with the mount interlock held.
14261 softdep_check_suspend(struct mount *mp,
14262 struct vnode *devvp,
14263 int softdep_depcnt,
14264 int softdep_accdepcnt,
14265 int secondary_writes,
14266 int secondary_accwrites)
14269 struct ufsmount *ump;
14270 struct inodedep *inodedep;
14271 int error, unlinked;
14273 bo = &devvp->v_bufobj;
14274 ASSERT_BO_WLOCKED(bo);
14277 * If we are not running with soft updates, then we need only
14278 * deal with secondary writes as we try to suspend.
14280 if (MOUNTEDSOFTDEP(mp) == 0) {
14282 while (mp->mnt_secondary_writes != 0) {
14284 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14285 (PUSER - 1) | PDROP, "secwr", 0);
14291 * Reasons for needing more work before suspend:
14292 * - Dirty buffers on devvp.
14293 * - Secondary writes occurred after start of vnode sync loop
14296 if (bo->bo_numoutput > 0 ||
14297 bo->bo_dirty.bv_cnt > 0 ||
14298 secondary_writes != 0 ||
14299 mp->mnt_secondary_writes != 0 ||
14300 secondary_accwrites != mp->mnt_secondary_accwrites)
14307 * If we are running with soft updates, then we need to coordinate
14308 * with them as we try to suspend.
14310 ump = VFSTOUFS(mp);
14312 if (!TRY_ACQUIRE_LOCK(ump)) {
14320 if (mp->mnt_secondary_writes != 0) {
14323 msleep(&mp->mnt_secondary_writes,
14325 (PUSER - 1) | PDROP, "secwr", 0);
14333 if (MOUNTEDSUJ(mp)) {
14334 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14336 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14337 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14338 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14340 !check_inodedep_free(inodedep))
14347 * Reasons for needing more work before suspend:
14348 * - Dirty buffers on devvp.
14349 * - Softdep activity occurred after start of vnode sync loop
14350 * - Secondary writes occurred after start of vnode sync loop
14353 if (bo->bo_numoutput > 0 ||
14354 bo->bo_dirty.bv_cnt > 0 ||
14355 softdep_depcnt != unlinked ||
14356 ump->softdep_deps != unlinked ||
14357 softdep_accdepcnt != ump->softdep_accdeps ||
14358 secondary_writes != 0 ||
14359 mp->mnt_secondary_writes != 0 ||
14360 secondary_accwrites != mp->mnt_secondary_accwrites)
14369 * Get the number of dependency structures for the file system, both
14370 * the current number and the total number allocated. These will
14371 * later be used to detect that softdep processing has occurred.
14374 softdep_get_depcounts(struct mount *mp,
14375 int *softdep_depsp,
14376 int *softdep_accdepsp)
14378 struct ufsmount *ump;
14380 if (MOUNTEDSOFTDEP(mp) == 0) {
14381 *softdep_depsp = 0;
14382 *softdep_accdepsp = 0;
14385 ump = VFSTOUFS(mp);
14387 *softdep_depsp = ump->softdep_deps;
14388 *softdep_accdepsp = ump->softdep_accdeps;
14393 * Wait for pending output on a vnode to complete.
14400 ASSERT_VOP_LOCKED(vp, "drain_output");
14401 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14405 * Called whenever a buffer that is being invalidated or reallocated
14406 * contains dependencies. This should only happen if an I/O error has
14407 * occurred. The routine is called with the buffer locked.
14410 softdep_deallocate_dependencies(bp)
14414 if ((bp->b_ioflags & BIO_ERROR) == 0)
14415 panic("softdep_deallocate_dependencies: dangling deps");
14416 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14417 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14419 printf("softdep_deallocate_dependencies: "
14420 "got error %d while accessing filesystem\n", bp->b_error);
14421 if (bp->b_error != ENXIO)
14422 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14426 * Function to handle asynchronous write errors in the filesystem.
14429 softdep_error(func, error)
14434 /* XXX should do something better! */
14435 printf("%s: got error %d while accessing filesystem\n", func, error);
14440 /* exported to ffs_vfsops.c */
14441 extern void db_print_ffs(struct ufsmount *ump);
14443 db_print_ffs(struct ufsmount *ump)
14445 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14446 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14447 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n",
14448 ump->um_fs, ump->softdep_on_worklist,
14449 ump->softdep_deps, ump->softdep_req);
14453 worklist_print(struct worklist *wk, int verbose)
14457 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14458 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
14461 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
14462 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
14463 LIST_NEXT(wk, wk_list));
14464 db_print_ffs(VFSTOUFS(wk->wk_mp));
14468 inodedep_print(struct inodedep *inodedep, int verbose)
14471 worklist_print(&inodedep->id_list, 0);
14472 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14474 (intmax_t)inodedep->id_ino,
14475 (intmax_t)fsbtodb(inodedep->id_fs,
14476 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14477 (intmax_t)inodedep->id_nlinkdelta,
14478 (intmax_t)inodedep->id_savednlink);
14483 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14484 inodedep->id_bmsafemap,
14485 inodedep->id_mkdiradd,
14486 TAILQ_FIRST(&inodedep->id_inoreflst));
14487 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
14488 LIST_FIRST(&inodedep->id_dirremhd),
14489 LIST_FIRST(&inodedep->id_pendinghd),
14490 LIST_FIRST(&inodedep->id_bufwait));
14491 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
14492 LIST_FIRST(&inodedep->id_inowait),
14493 TAILQ_FIRST(&inodedep->id_inoupdt),
14494 TAILQ_FIRST(&inodedep->id_newinoupdt));
14495 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
14496 TAILQ_FIRST(&inodedep->id_extupdt),
14497 TAILQ_FIRST(&inodedep->id_newextupdt),
14498 TAILQ_FIRST(&inodedep->id_freeblklst));
14499 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
14500 inodedep->id_savedino1,
14501 (intmax_t)inodedep->id_savedsize,
14502 (intmax_t)inodedep->id_savedextsize);
14506 newblk_print(struct newblk *nbp)
14509 worklist_print(&nbp->nb_list, 0);
14510 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14511 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
14513 &nbp->nb_bmsafemap,
14514 &nbp->nb_freefrag);
14515 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
14516 LIST_FIRST(&nbp->nb_indirdeps),
14517 LIST_FIRST(&nbp->nb_newdirblk),
14518 LIST_FIRST(&nbp->nb_jwork));
14522 allocdirect_print(struct allocdirect *adp)
14525 newblk_print(&adp->ad_block);
14526 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
14527 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14528 db_printf(" offset %d, inodedep %p\n",
14529 adp->ad_offset, adp->ad_inodedep);
14533 allocindir_print(struct allocindir *aip)
14536 newblk_print(&aip->ai_block);
14537 db_printf(" oldblkno %jd, lbn %jd\n",
14538 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
14539 db_printf(" offset %d, indirdep %p\n",
14540 aip->ai_offset, aip->ai_indirdep);
14544 mkdir_print(struct mkdir *mkdir)
14547 worklist_print(&mkdir->md_list, 0);
14548 db_printf(" diradd %p, jaddref %p, buf %p\n",
14549 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
14552 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
14555 if (have_addr == 0) {
14556 db_printf("inodedep address required\n");
14559 inodedep_print((struct inodedep*)addr, 1);
14562 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
14564 struct inodedep_hashhead *inodedephd;
14565 struct inodedep *inodedep;
14566 struct ufsmount *ump;
14569 if (have_addr == 0) {
14570 db_printf("ufsmount address required\n");
14573 ump = (struct ufsmount *)addr;
14574 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14575 inodedephd = &ump->inodedep_hashtbl[cnt];
14576 LIST_FOREACH(inodedep, inodedephd, id_hash) {
14577 inodedep_print(inodedep, 0);
14582 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
14585 if (have_addr == 0) {
14586 db_printf("worklist address required\n");
14589 worklist_print((struct worklist *)addr, 1);
14592 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
14594 struct worklist *wk;
14595 struct workhead *wkhd;
14597 if (have_addr == 0) {
14598 db_printf("worklist address required "
14599 "(for example value in bp->b_dep)\n");
14603 * We often do not have the address of the worklist head but
14604 * instead a pointer to its first entry (e.g., we have the
14605 * contents of bp->b_dep rather than &bp->b_dep). But the back
14606 * pointer of bp->b_dep will point at the head of the list, so
14607 * we cheat and use that instead. If we are in the middle of
14608 * a list we will still get the same result, so nothing
14609 * unexpected will result.
14611 wk = (struct worklist *)addr;
14614 wkhd = (struct workhead *)wk->wk_list.le_prev;
14615 LIST_FOREACH(wk, wkhd, wk_list) {
14616 switch(wk->wk_type) {
14618 inodedep_print(WK_INODEDEP(wk), 0);
14620 case D_ALLOCDIRECT:
14621 allocdirect_print(WK_ALLOCDIRECT(wk));
14624 allocindir_print(WK_ALLOCINDIR(wk));
14627 mkdir_print(WK_MKDIR(wk));
14630 worklist_print(wk, 0);
14636 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
14638 if (have_addr == 0) {
14639 db_printf("mkdir address required\n");
14642 mkdir_print((struct mkdir *)addr);
14645 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
14647 struct mkdirlist *mkdirlisthd;
14648 struct mkdir *mkdir;
14650 if (have_addr == 0) {
14651 db_printf("mkdir listhead address required\n");
14654 mkdirlisthd = (struct mkdirlist *)addr;
14655 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14656 mkdir_print(mkdir);
14657 if (mkdir->md_diradd != NULL) {
14659 worklist_print(&mkdir->md_diradd->da_list, 0);
14661 if (mkdir->md_jaddref != NULL) {
14663 worklist_print(&mkdir->md_jaddref->ja_list, 0);
14668 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
14670 if (have_addr == 0) {
14671 db_printf("allocdirect address required\n");
14674 allocdirect_print((struct allocdirect *)addr);
14677 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
14679 if (have_addr == 0) {
14680 db_printf("allocindir address required\n");
14683 allocindir_print((struct allocindir *)addr);
14688 #endif /* SOFTUPDATES */