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>
90 #include <geom/geom_vfs.h>
94 #define KTR_SUJ 0 /* Define to KTR_SPARE. */
99 softdep_flushfiles(oldmnt, flags, td)
100 struct mount *oldmnt;
105 panic("softdep_flushfiles called");
109 softdep_mount(devvp, mp, fs, cred)
127 softdep_uninitialize()
138 panic("softdep_unmount called");
142 softdep_setup_sbupdate(ump, fs, bp)
143 struct ufsmount *ump;
148 panic("softdep_setup_sbupdate called");
152 softdep_setup_inomapdep(bp, ip, newinum, mode)
159 panic("softdep_setup_inomapdep called");
163 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
166 ufs2_daddr_t newblkno;
171 panic("softdep_setup_blkmapdep called");
175 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
178 ufs2_daddr_t newblkno;
179 ufs2_daddr_t oldblkno;
185 panic("softdep_setup_allocdirect called");
189 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
192 ufs2_daddr_t newblkno;
193 ufs2_daddr_t oldblkno;
199 panic("softdep_setup_allocext called");
203 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
208 ufs2_daddr_t newblkno;
209 ufs2_daddr_t oldblkno;
213 panic("softdep_setup_allocindir_page called");
217 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
222 ufs2_daddr_t newblkno;
225 panic("softdep_setup_allocindir_meta called");
229 softdep_journal_freeblocks(ip, cred, length, flags)
236 panic("softdep_journal_freeblocks called");
240 softdep_journal_fsync(ip)
244 panic("softdep_journal_fsync called");
248 softdep_setup_freeblocks(ip, length, flags)
254 panic("softdep_setup_freeblocks called");
258 softdep_freefile(pvp, ino, mode)
264 panic("softdep_freefile called");
268 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
273 struct buf *newdirbp;
277 panic("softdep_setup_directory_add called");
281 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
290 panic("softdep_change_directoryentry_offset called");
294 softdep_setup_remove(bp, dp, ip, isrmdir)
301 panic("softdep_setup_remove called");
305 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
313 panic("softdep_setup_directory_change called");
317 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
322 struct workhead *wkhd;
325 panic("%s called", __FUNCTION__);
329 softdep_setup_inofree(mp, bp, ino, wkhd)
333 struct workhead *wkhd;
336 panic("%s called", __FUNCTION__);
340 softdep_setup_unlink(dp, ip)
345 panic("%s called", __FUNCTION__);
349 softdep_setup_link(dp, ip)
354 panic("%s called", __FUNCTION__);
358 softdep_revert_link(dp, ip)
363 panic("%s called", __FUNCTION__);
367 softdep_setup_rmdir(dp, ip)
372 panic("%s called", __FUNCTION__);
376 softdep_revert_rmdir(dp, ip)
381 panic("%s called", __FUNCTION__);
385 softdep_setup_create(dp, ip)
390 panic("%s called", __FUNCTION__);
394 softdep_revert_create(dp, ip)
399 panic("%s called", __FUNCTION__);
403 softdep_setup_mkdir(dp, ip)
408 panic("%s called", __FUNCTION__);
412 softdep_revert_mkdir(dp, ip)
417 panic("%s called", __FUNCTION__);
421 softdep_setup_dotdot_link(dp, ip)
426 panic("%s called", __FUNCTION__);
430 softdep_prealloc(vp, waitok)
435 panic("%s called", __FUNCTION__);
439 softdep_journal_lookup(mp, vpp)
448 softdep_change_linkcnt(ip)
452 panic("softdep_change_linkcnt called");
456 softdep_load_inodeblock(ip)
460 panic("softdep_load_inodeblock called");
464 softdep_update_inodeblock(ip, bp, waitfor)
470 panic("softdep_update_inodeblock called");
475 struct vnode *vp; /* the "in_core" copy of the inode */
482 softdep_fsync_mountdev(vp)
490 softdep_flushworklist(oldmnt, countp, td)
491 struct mount *oldmnt;
501 softdep_sync_metadata(struct vnode *vp)
504 panic("softdep_sync_metadata called");
508 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
511 panic("softdep_sync_buf called");
519 panic("softdep_slowdown called");
523 softdep_request_cleanup(fs, vp, cred, resource)
534 softdep_check_suspend(struct mount *mp,
537 int softdep_accdepcnt,
538 int secondary_writes,
539 int secondary_accwrites)
544 (void) softdep_depcnt,
545 (void) softdep_accdepcnt;
547 bo = &devvp->v_bufobj;
548 ASSERT_BO_WLOCKED(bo);
551 while (mp->mnt_secondary_writes != 0) {
553 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
554 (PUSER - 1) | PDROP, "secwr", 0);
560 * Reasons for needing more work before suspend:
561 * - Dirty buffers on devvp.
562 * - Secondary writes occurred after start of vnode sync loop
565 if (bo->bo_numoutput > 0 ||
566 bo->bo_dirty.bv_cnt > 0 ||
567 secondary_writes != 0 ||
568 mp->mnt_secondary_writes != 0 ||
569 secondary_accwrites != mp->mnt_secondary_accwrites)
576 softdep_get_depcounts(struct mount *mp,
578 int *softdepactiveaccp)
582 *softdepactiveaccp = 0;
586 softdep_buf_append(bp, wkhd)
588 struct workhead *wkhd;
591 panic("softdep_buf_appendwork called");
595 softdep_inode_append(ip, cred, wkhd)
598 struct workhead *wkhd;
601 panic("softdep_inode_appendwork called");
605 softdep_freework(wkhd)
606 struct workhead *wkhd;
609 panic("softdep_freework called");
613 softdep_prerename(fdvp, fvp, tdvp, tvp)
620 panic("softdep_prerename called");
624 softdep_prelink(dvp, vp, cnp)
627 struct componentname *cnp;
630 panic("softdep_prelink called");
635 FEATURE(softupdates, "FFS soft-updates support");
637 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
638 "soft updates stats");
639 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total,
640 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
641 "total dependencies allocated");
642 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse,
643 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
644 "high use dependencies allocated");
645 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current,
646 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
647 "current dependencies allocated");
648 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write,
649 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
650 "current dependencies written");
652 unsigned long dep_current[D_LAST + 1];
653 unsigned long dep_highuse[D_LAST + 1];
654 unsigned long dep_total[D_LAST + 1];
655 unsigned long dep_write[D_LAST + 1];
657 #define SOFTDEP_TYPE(type, str, long) \
658 static MALLOC_DEFINE(M_ ## type, #str, long); \
659 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \
660 &dep_total[D_ ## type], 0, ""); \
661 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \
662 &dep_current[D_ ## type], 0, ""); \
663 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \
664 &dep_highuse[D_ ## type], 0, ""); \
665 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \
666 &dep_write[D_ ## type], 0, "");
668 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
669 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
670 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
671 "Block or frag allocated from cyl group map");
672 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
673 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
674 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
675 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
676 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
677 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
678 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
679 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
680 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
681 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
682 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
683 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
684 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
685 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
686 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
687 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
688 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
689 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
690 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
691 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
692 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
693 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
694 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
695 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
697 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
699 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
700 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
701 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
703 #define M_SOFTDEP_FLAGS (M_WAITOK)
706 * translate from workitem type to memory type
707 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
709 static struct malloc_type *memtype[] = {
741 #define DtoM(type) (memtype[type])
744 * Names of malloc types.
746 #define TYPENAME(type) \
747 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
748 memtype[type]->ks_shortdesc : "???")
750 * End system adaptation definitions.
753 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
754 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
757 * Internal function prototypes.
759 static void check_clear_deps(struct mount *);
760 static void softdep_error(char *, int);
761 static int softdep_prerename_vnode(struct ufsmount *, struct vnode *);
762 static int softdep_process_worklist(struct mount *, int);
763 static int softdep_waitidle(struct mount *, int);
764 static void drain_output(struct vnode *);
765 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
766 static int check_inodedep_free(struct inodedep *);
767 static void clear_remove(struct mount *);
768 static void clear_inodedeps(struct mount *);
769 static void unlinked_inodedep(struct mount *, struct inodedep *);
770 static void clear_unlinked_inodedep(struct inodedep *);
771 static struct inodedep *first_unlinked_inodedep(struct ufsmount *);
772 static int flush_pagedep_deps(struct vnode *, struct mount *,
773 struct diraddhd *, struct buf *);
774 static int free_pagedep(struct pagedep *);
775 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
776 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
777 static int flush_deplist(struct allocdirectlst *, int, int *);
778 static int sync_cgs(struct mount *, int);
779 static int handle_written_filepage(struct pagedep *, struct buf *, int);
780 static int handle_written_sbdep(struct sbdep *, struct buf *);
781 static void initiate_write_sbdep(struct sbdep *);
782 static void diradd_inode_written(struct diradd *, struct inodedep *);
783 static int handle_written_indirdep(struct indirdep *, struct buf *,
785 static int handle_written_inodeblock(struct inodedep *, struct buf *, int);
786 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
788 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
789 static void handle_written_jaddref(struct jaddref *);
790 static void handle_written_jremref(struct jremref *);
791 static void handle_written_jseg(struct jseg *, struct buf *);
792 static void handle_written_jnewblk(struct jnewblk *);
793 static void handle_written_jblkdep(struct jblkdep *);
794 static void handle_written_jfreefrag(struct jfreefrag *);
795 static void complete_jseg(struct jseg *);
796 static void complete_jsegs(struct jseg *);
797 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
798 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
799 static void jremref_write(struct jremref *, struct jseg *, uint8_t *);
800 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
801 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
802 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
803 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
804 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
805 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
806 static inline void inoref_write(struct inoref *, struct jseg *,
808 static void handle_allocdirect_partdone(struct allocdirect *,
810 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
812 static void indirdep_complete(struct indirdep *);
813 static int indirblk_lookup(struct mount *, ufs2_daddr_t);
814 static void indirblk_insert(struct freework *);
815 static void indirblk_remove(struct freework *);
816 static void handle_allocindir_partdone(struct allocindir *);
817 static void initiate_write_filepage(struct pagedep *, struct buf *);
818 static void initiate_write_indirdep(struct indirdep*, struct buf *);
819 static void handle_written_mkdir(struct mkdir *, int);
820 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
822 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
823 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
824 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
825 static void handle_workitem_freefile(struct freefile *);
826 static int handle_workitem_remove(struct dirrem *, int);
827 static struct dirrem *newdirrem(struct buf *, struct inode *,
828 struct inode *, int, struct dirrem **);
829 static struct indirdep *indirdep_lookup(struct mount *, struct inode *,
831 static void cancel_indirdep(struct indirdep *, struct buf *,
833 static void free_indirdep(struct indirdep *);
834 static void free_diradd(struct diradd *, struct workhead *);
835 static void merge_diradd(struct inodedep *, struct diradd *);
836 static void complete_diradd(struct diradd *);
837 static struct diradd *diradd_lookup(struct pagedep *, int);
838 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
840 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
842 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
843 struct jremref *, struct jremref *);
844 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
846 static void cancel_allocindir(struct allocindir *, struct buf *bp,
847 struct freeblks *, int);
848 static int setup_trunc_indir(struct freeblks *, struct inode *,
849 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
850 static void complete_trunc_indir(struct freework *);
851 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
853 static void complete_mkdir(struct mkdir *);
854 static void free_newdirblk(struct newdirblk *);
855 static void free_jremref(struct jremref *);
856 static void free_jaddref(struct jaddref *);
857 static void free_jsegdep(struct jsegdep *);
858 static void free_jsegs(struct jblocks *);
859 static void rele_jseg(struct jseg *);
860 static void free_jseg(struct jseg *, struct jblocks *);
861 static void free_jnewblk(struct jnewblk *);
862 static void free_jblkdep(struct jblkdep *);
863 static void free_jfreefrag(struct jfreefrag *);
864 static void free_freedep(struct freedep *);
865 static void journal_jremref(struct dirrem *, struct jremref *,
867 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
868 static int cancel_jaddref(struct jaddref *, struct inodedep *,
870 static void cancel_jfreefrag(struct jfreefrag *);
871 static inline void setup_freedirect(struct freeblks *, struct inode *,
873 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
874 static inline void setup_freeindir(struct freeblks *, struct inode *, int,
876 static inline struct freeblks *newfreeblks(struct mount *, struct inode *);
877 static void freeblks_free(struct ufsmount *, struct freeblks *, int);
878 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
879 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
880 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
881 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
883 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
884 static int cancel_pagedep(struct pagedep *, struct freeblks *, int);
885 static int deallocate_dependencies(struct buf *, struct freeblks *, int);
886 static void newblk_freefrag(struct newblk*);
887 static void free_newblk(struct newblk *);
888 static void cancel_allocdirect(struct allocdirectlst *,
889 struct allocdirect *, struct freeblks *);
890 static int check_inode_unwritten(struct inodedep *);
891 static int free_inodedep(struct inodedep *);
892 static void freework_freeblock(struct freework *, u_long);
893 static void freework_enqueue(struct freework *);
894 static int handle_workitem_freeblocks(struct freeblks *, int);
895 static int handle_complete_freeblocks(struct freeblks *, int);
896 static void handle_workitem_indirblk(struct freework *);
897 static void handle_written_freework(struct freework *);
898 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
899 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
901 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
902 struct inodedep *, struct allocindir *, ufs_lbn_t);
903 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
904 ufs2_daddr_t, ufs_lbn_t);
905 static void handle_workitem_freefrag(struct freefrag *);
906 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
908 static void allocdirect_merge(struct allocdirectlst *,
909 struct allocdirect *, struct allocdirect *);
910 static struct freefrag *allocindir_merge(struct allocindir *,
911 struct allocindir *);
912 static int bmsafemap_find(struct bmsafemap_hashhead *, int,
913 struct bmsafemap **);
914 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
915 int cg, struct bmsafemap *);
916 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
918 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
919 static int inodedep_find(struct inodedep_hashhead *, ino_t,
921 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
922 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
923 int, struct pagedep **);
924 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
926 static void pause_timer(void *);
927 static int request_cleanup(struct mount *, int);
928 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
929 static void schedule_cleanup(struct mount *);
930 static void softdep_ast_cleanup_proc(struct thread *);
931 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
932 static int process_worklist_item(struct mount *, int, int);
933 static void process_removes(struct vnode *);
934 static void process_truncates(struct vnode *);
935 static void jwork_move(struct workhead *, struct workhead *);
936 static void jwork_insert(struct workhead *, struct jsegdep *);
937 static void add_to_worklist(struct worklist *, int);
938 static void wake_worklist(struct worklist *);
939 static void wait_worklist(struct worklist *, char *);
940 static void remove_from_worklist(struct worklist *);
941 static void softdep_flush(void *);
942 static void softdep_flushjournal(struct mount *);
943 static int softdep_speedup(struct ufsmount *);
944 static void worklist_speedup(struct mount *);
945 static int journal_mount(struct mount *, struct fs *, struct ucred *);
946 static void journal_unmount(struct ufsmount *);
947 static int journal_space(struct ufsmount *, int);
948 static void journal_suspend(struct ufsmount *);
949 static int journal_unsuspend(struct ufsmount *ump);
950 static void add_to_journal(struct worklist *);
951 static void remove_from_journal(struct worklist *);
952 static bool softdep_excess_items(struct ufsmount *, int);
953 static void softdep_process_journal(struct mount *, struct worklist *, int);
954 static struct jremref *newjremref(struct dirrem *, struct inode *,
955 struct inode *ip, off_t, nlink_t);
956 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
958 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
960 static inline struct jsegdep *inoref_jseg(struct inoref *);
961 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
962 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
964 static void adjust_newfreework(struct freeblks *, int);
965 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
966 static void move_newblock_dep(struct jaddref *, struct inodedep *);
967 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
968 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
969 ufs2_daddr_t, long, ufs_lbn_t);
970 static struct freework *newfreework(struct ufsmount *, struct freeblks *,
971 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
972 static int jwait(struct worklist *, int);
973 static struct inodedep *inodedep_lookup_ip(struct inode *);
974 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
975 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
976 static void handle_jwork(struct workhead *);
977 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
979 static struct jblocks *jblocks_create(void);
980 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
981 static void jblocks_free(struct jblocks *, struct mount *, int);
982 static void jblocks_destroy(struct jblocks *);
983 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
986 * Exported softdep operations.
988 static void softdep_disk_io_initiation(struct buf *);
989 static void softdep_disk_write_complete(struct buf *);
990 static void softdep_deallocate_dependencies(struct buf *);
991 static int softdep_count_dependencies(struct buf *bp, int);
994 * Global lock over all of soft updates.
996 static struct mtx lk;
997 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF);
999 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk)
1000 #define FREE_GBLLOCK(lk) mtx_unlock(lk)
1001 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED)
1004 * Per-filesystem soft-updates locking.
1006 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock)
1007 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock)
1008 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock)
1009 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock)
1010 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \
1013 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock)
1014 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock)
1017 * Worklist queue management.
1018 * These routines require that the lock be held.
1020 #ifndef /* NOT */ INVARIANTS
1021 #define WORKLIST_INSERT(head, item) do { \
1022 (item)->wk_state |= ONWORKLIST; \
1023 LIST_INSERT_HEAD(head, item, wk_list); \
1025 #define WORKLIST_REMOVE(item) do { \
1026 (item)->wk_state &= ~ONWORKLIST; \
1027 LIST_REMOVE(item, wk_list); \
1029 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT
1030 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE
1032 #else /* INVARIANTS */
1033 static void worklist_insert(struct workhead *, struct worklist *, int,
1035 static void worklist_remove(struct worklist *, int, const char *, int);
1037 #define WORKLIST_INSERT(head, item) \
1038 worklist_insert(head, item, 1, __func__, __LINE__)
1039 #define WORKLIST_INSERT_UNLOCKED(head, item)\
1040 worklist_insert(head, item, 0, __func__, __LINE__)
1041 #define WORKLIST_REMOVE(item)\
1042 worklist_remove(item, 1, __func__, __LINE__)
1043 #define WORKLIST_REMOVE_UNLOCKED(item)\
1044 worklist_remove(item, 0, __func__, __LINE__)
1047 worklist_insert(head, item, locked, func, line)
1048 struct workhead *head;
1049 struct worklist *item;
1056 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1057 if (item->wk_state & ONWORKLIST)
1058 panic("worklist_insert: %p %s(0x%X) already on list, "
1059 "added in function %s at line %d",
1060 item, TYPENAME(item->wk_type), item->wk_state,
1061 item->wk_func, item->wk_line);
1062 item->wk_state |= ONWORKLIST;
1063 item->wk_func = func;
1064 item->wk_line = line;
1065 LIST_INSERT_HEAD(head, item, wk_list);
1069 worklist_remove(item, locked, func, line)
1070 struct worklist *item;
1077 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1078 if ((item->wk_state & ONWORKLIST) == 0)
1079 panic("worklist_remove: %p %s(0x%X) not on list, "
1080 "removed in function %s at line %d",
1081 item, TYPENAME(item->wk_type), item->wk_state,
1082 item->wk_func, item->wk_line);
1083 item->wk_state &= ~ONWORKLIST;
1084 item->wk_func = func;
1085 item->wk_line = line;
1086 LIST_REMOVE(item, wk_list);
1088 #endif /* INVARIANTS */
1091 * Merge two jsegdeps keeping only the oldest one as newer references
1092 * can't be discarded until after older references.
1094 static inline struct jsegdep *
1095 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1097 struct jsegdep *swp;
1102 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1107 WORKLIST_REMOVE(&two->jd_list);
1114 * If two freedeps are compatible free one to reduce list size.
1116 static inline struct freedep *
1117 freedep_merge(struct freedep *one, struct freedep *two)
1122 if (one->fd_freework == two->fd_freework) {
1123 WORKLIST_REMOVE(&two->fd_list);
1130 * Move journal work from one list to another. Duplicate freedeps and
1131 * jsegdeps are coalesced to keep the lists as small as possible.
1134 jwork_move(dst, src)
1135 struct workhead *dst;
1136 struct workhead *src;
1138 struct freedep *freedep;
1139 struct jsegdep *jsegdep;
1140 struct worklist *wkn;
1141 struct worklist *wk;
1144 ("jwork_move: dst == src"));
1147 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1148 if (wk->wk_type == D_JSEGDEP)
1149 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1150 else if (wk->wk_type == D_FREEDEP)
1151 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1154 while ((wk = LIST_FIRST(src)) != NULL) {
1155 WORKLIST_REMOVE(wk);
1156 WORKLIST_INSERT(dst, wk);
1157 if (wk->wk_type == D_JSEGDEP) {
1158 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1161 if (wk->wk_type == D_FREEDEP)
1162 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1167 jwork_insert(dst, jsegdep)
1168 struct workhead *dst;
1169 struct jsegdep *jsegdep;
1171 struct jsegdep *jsegdepn;
1172 struct worklist *wk;
1174 LIST_FOREACH(wk, dst, wk_list)
1175 if (wk->wk_type == D_JSEGDEP)
1178 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1181 jsegdepn = WK_JSEGDEP(wk);
1182 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1183 WORKLIST_REMOVE(wk);
1184 free_jsegdep(jsegdepn);
1185 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1187 free_jsegdep(jsegdep);
1191 * Routines for tracking and managing workitems.
1193 static void workitem_free(struct worklist *, int);
1194 static void workitem_alloc(struct worklist *, int, struct mount *);
1195 static void workitem_reassign(struct worklist *, int);
1197 #define WORKITEM_FREE(item, type) \
1198 workitem_free((struct worklist *)(item), (type))
1199 #define WORKITEM_REASSIGN(item, type) \
1200 workitem_reassign((struct worklist *)(item), (type))
1203 workitem_free(item, type)
1204 struct worklist *item;
1207 struct ufsmount *ump;
1210 if (item->wk_state & ONWORKLIST)
1211 panic("workitem_free: %s(0x%X) still on list, "
1212 "added in function %s at line %d",
1213 TYPENAME(item->wk_type), item->wk_state,
1214 item->wk_func, item->wk_line);
1215 if (item->wk_type != type && type != D_NEWBLK)
1216 panic("workitem_free: type mismatch %s != %s",
1217 TYPENAME(item->wk_type), TYPENAME(type));
1219 if (item->wk_state & IOWAITING)
1221 ump = VFSTOUFS(item->wk_mp);
1223 KASSERT(ump->softdep_deps > 0,
1224 ("workitem_free: %s: softdep_deps going negative",
1225 ump->um_fs->fs_fsmnt));
1226 if (--ump->softdep_deps == 0 && ump->softdep_req)
1227 wakeup(&ump->softdep_deps);
1228 KASSERT(dep_current[item->wk_type] > 0,
1229 ("workitem_free: %s: dep_current[%s] going negative",
1230 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1231 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1232 ("workitem_free: %s: softdep_curdeps[%s] going negative",
1233 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1234 atomic_subtract_long(&dep_current[item->wk_type], 1);
1235 ump->softdep_curdeps[item->wk_type] -= 1;
1237 LIST_REMOVE(item, wk_all);
1239 free(item, DtoM(type));
1243 workitem_alloc(item, type, mp)
1244 struct worklist *item;
1248 struct ufsmount *ump;
1250 item->wk_type = type;
1255 ACQUIRE_GBLLOCK(&lk);
1256 dep_current[type]++;
1257 if (dep_current[type] > dep_highuse[type])
1258 dep_highuse[type] = dep_current[type];
1262 ump->softdep_curdeps[type] += 1;
1263 ump->softdep_deps++;
1264 ump->softdep_accdeps++;
1266 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all);
1272 workitem_reassign(item, newtype)
1273 struct worklist *item;
1276 struct ufsmount *ump;
1278 ump = VFSTOUFS(item->wk_mp);
1280 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1281 ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1282 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1283 ump->softdep_curdeps[item->wk_type] -= 1;
1284 ump->softdep_curdeps[newtype] += 1;
1285 KASSERT(dep_current[item->wk_type] > 0,
1286 ("workitem_reassign: %s: dep_current[%s] going negative",
1287 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1288 ACQUIRE_GBLLOCK(&lk);
1289 dep_current[newtype]++;
1290 dep_current[item->wk_type]--;
1291 if (dep_current[newtype] > dep_highuse[newtype])
1292 dep_highuse[newtype] = dep_current[newtype];
1293 dep_total[newtype]++;
1295 item->wk_type = newtype;
1299 * Workitem queue management
1301 static int max_softdeps; /* maximum number of structs before slowdown */
1302 static int tickdelay = 2; /* number of ticks to pause during slowdown */
1303 static int proc_waiting; /* tracks whether we have a timeout posted */
1304 static int *stat_countp; /* statistic to count in proc_waiting timeout */
1305 static struct callout softdep_callout;
1306 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
1307 static int req_clear_remove; /* syncer process flush some freeblks */
1308 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1311 * runtime statistics
1313 static int stat_flush_threads; /* number of softdep flushing threads */
1314 static int stat_worklist_push; /* number of worklist cleanups */
1315 static int stat_delayed_inact; /* number of delayed inactivation cleanups */
1316 static int stat_blk_limit_push; /* number of times block limit neared */
1317 static int stat_ino_limit_push; /* number of times inode limit neared */
1318 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
1319 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
1320 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
1321 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
1322 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
1323 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1324 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
1325 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */
1326 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */
1327 static int stat_journal_min; /* Times hit journal min threshold */
1328 static int stat_journal_low; /* Times hit journal low threshold */
1329 static int stat_journal_wait; /* Times blocked in jwait(). */
1330 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */
1331 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */
1332 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */
1333 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */
1334 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1335 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1336 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1337 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1338 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1339 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1341 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1342 &max_softdeps, 0, "");
1343 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1345 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1346 &stat_flush_threads, 0, "");
1347 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push,
1348 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,"");
1349 SYSCTL_INT(_debug_softdep, OID_AUTO, delayed_inactivations, CTLFLAG_RD,
1350 &stat_delayed_inact, 0, "");
1351 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push,
1352 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,"");
1353 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push,
1354 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,"");
1355 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit,
1356 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, "");
1357 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit,
1358 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, "");
1359 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit,
1360 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, "");
1361 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs,
1362 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, "");
1363 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap,
1364 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, "");
1365 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs,
1366 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, "");
1367 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry,
1368 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, "");
1369 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback,
1370 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, "");
1371 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback,
1372 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, "");
1373 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low,
1374 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, "");
1375 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min,
1376 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, "");
1377 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait,
1378 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, "");
1379 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage,
1380 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, "");
1381 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks,
1382 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, "");
1383 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode,
1384 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, "");
1385 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk,
1386 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, "");
1387 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests,
1388 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, "");
1389 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests,
1390 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, "");
1391 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay,
1392 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, "");
1393 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries,
1394 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, "");
1395 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures,
1396 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, "");
1398 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1399 &softdep_flushcache, 0, "");
1400 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1401 &stat_emptyjblocks, 0, "");
1403 SYSCTL_DECL(_vfs_ffs);
1405 /* Whether to recompute the summary at mount time */
1406 static int compute_summary_at_mount = 0;
1407 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1408 &compute_summary_at_mount, 0, "Recompute summary at mount");
1409 static int print_threads = 0;
1410 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1411 &print_threads, 0, "Notify flusher thread start/stop");
1413 /* List of all filesystems mounted with soft updates */
1414 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1417 get_parent_vp_unlock_bp(struct mount *mp, struct buf *bp,
1418 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp)
1423 * Requeue unfinished dependencies before
1424 * unlocking buffer, which could make
1425 * diraddhdp invalid.
1427 ACQUIRE_LOCK(VFSTOUFS(mp));
1428 while ((dap = LIST_FIRST(unfinishedp)) != NULL) {
1429 LIST_REMOVE(dap, da_pdlist);
1430 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
1432 FREE_LOCK(VFSTOUFS(mp));
1434 bp->b_vflags &= ~BV_SCANNED;
1440 * This function fetches inode inum on mount point mp. We already
1441 * hold a locked vnode vp, and might have a locked buffer bp belonging
1444 * We must not block on acquiring the new inode lock as we will get
1445 * into a lock-order reversal with the buffer lock and possibly get a
1446 * deadlock. Thus if we cannot instantiate the requested vnode
1447 * without sleeping on its lock, we must unlock the vnode and the
1448 * buffer before doing a blocking on the vnode lock. We return
1449 * ERELOOKUP if we have had to unlock either the vnode or the buffer so
1450 * that the caller can reassess its state.
1452 * Top-level VFS code (for syscalls and other consumers, e.g. callers
1453 * of VOP_FSYNC() in syncer) check for ERELOOKUP and restart at safe
1456 * Since callers expect to operate on fully constructed vnode, we also
1457 * recheck v_data after relock, and return ENOENT if NULL.
1459 * If unlocking bp, we must unroll dequeueing its unfinished
1460 * dependencies, and clear scan flag, before unlocking. If unlocking
1461 * vp while it is under deactivation, we re-queue deactivation.
1464 get_parent_vp(struct vnode *vp, struct mount *mp, ino_t inum, struct buf *bp,
1465 struct diraddhd *diraddhdp, struct diraddhd *unfinishedp,
1472 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked");
1473 for (bplocked = true, pvp = NULL;;) {
1474 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE | LK_NOWAIT, &pvp,
1475 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
1478 * Since we could have unlocked vp, the inode
1479 * number could no longer indicate a
1480 * constructed node. In this case, we must
1481 * restart the syscall.
1483 if (VTOI(pvp)->i_mode == 0 || !bplocked) {
1484 if (bp != NULL && bplocked)
1485 get_parent_vp_unlock_bp(mp, bp,
1486 diraddhdp, unfinishedp);
1487 if (VTOI(pvp)->i_mode == 0)
1494 if (bp != NULL && bplocked) {
1495 get_parent_vp_unlock_bp(mp, bp, diraddhdp, unfinishedp);
1500 * Do not drop vnode lock while inactivating during
1501 * vunref. This would result in leaks of the VI flags
1502 * and reclaiming of non-truncated vnode. Instead,
1503 * re-schedule inactivation hoping that we would be
1504 * able to sync inode later.
1506 if ((vp->v_iflag & VI_DOINGINACT) != 0 &&
1507 (vp->v_vflag & VV_UNREF) != 0) {
1509 vp->v_iflag |= VI_OWEINACT;
1515 error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &pvp,
1516 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
1518 MPASS(error != ERELOOKUP);
1519 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1522 if (VTOI(pvp)->i_mode == 0) {
1526 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1530 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
1535 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1536 if (vp->v_data == NULL) {
1546 if (error != 0 && pvp != NULL) {
1552 ASSERT_VOP_ELOCKED(vp, "child vnode must be locked on return");
1557 * This function cleans the worklist for a filesystem.
1558 * Each filesystem running with soft dependencies gets its own
1559 * thread to run in this function. The thread is started up in
1560 * softdep_mount and shutdown in softdep_unmount. They show up
1561 * as part of the kernel "bufdaemon" process whose process
1562 * entry is available in bufdaemonproc.
1564 static int searchfailed;
1565 extern struct proc *bufdaemonproc;
1572 struct ufsmount *ump;
1576 td->td_pflags |= TDP_NORUNNINGBUF;
1577 mp = (struct mount *)addr;
1579 atomic_add_int(&stat_flush_threads, 1);
1581 ump->softdep_flags &= ~FLUSH_STARTING;
1582 wakeup(&ump->softdep_flushtd);
1584 if (print_threads) {
1585 if (stat_flush_threads == 1)
1586 printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1587 bufdaemonproc->p_pid);
1588 printf("Start thread %s\n", td->td_name);
1591 while (softdep_process_worklist(mp, 0) > 0 ||
1593 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1594 kthread_suspend_check();
1596 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1597 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1599 ump->softdep_flags &= ~FLUSH_CLEANUP;
1601 * Check to see if we are done and need to exit.
1603 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1607 ump->softdep_flags &= ~FLUSH_EXIT;
1608 cleanups = ump->um_softdep->sd_cleanups;
1610 wakeup(&ump->softdep_flags);
1611 if (print_threads) {
1612 printf("Stop thread %s: searchfailed %d, "
1613 "did cleanups %d\n",
1614 td->td_name, searchfailed, cleanups);
1616 atomic_subtract_int(&stat_flush_threads, 1);
1618 panic("kthread_exit failed\n");
1623 worklist_speedup(mp)
1626 struct ufsmount *ump;
1630 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1631 ump->softdep_flags |= FLUSH_CLEANUP;
1632 wakeup(&ump->softdep_flushtd);
1636 softdep_send_speedup(struct ufsmount *ump, off_t shortage, u_int flags)
1640 if ((ump->um_flags & UM_CANSPEEDUP) == 0)
1643 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO);
1644 bp->b_iocmd = BIO_SPEEDUP;
1645 bp->b_ioflags = flags;
1646 bp->b_bcount = omin(shortage, LONG_MAX);
1647 g_vfs_strategy(ump->um_bo, bp);
1653 softdep_speedup(ump)
1654 struct ufsmount *ump;
1656 struct ufsmount *altump;
1657 struct mount_softdeps *sdp;
1660 worklist_speedup(ump->um_mountp);
1663 * If we have global shortages, then we need other
1664 * filesystems to help with the cleanup. Here we wakeup a
1665 * flusher thread for a filesystem that is over its fair
1666 * share of resources.
1668 if (req_clear_inodedeps || req_clear_remove) {
1669 ACQUIRE_GBLLOCK(&lk);
1670 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1671 if ((altump = sdp->sd_ump) == ump)
1673 if (((req_clear_inodedeps &&
1674 altump->softdep_curdeps[D_INODEDEP] >
1675 max_softdeps / stat_flush_threads) ||
1676 (req_clear_remove &&
1677 altump->softdep_curdeps[D_DIRREM] >
1678 (max_softdeps / 2) / stat_flush_threads)) &&
1679 TRY_ACQUIRE_LOCK(altump))
1687 * Move to the end of the list so we pick a
1688 * different one on out next try.
1690 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1691 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1693 if ((altump->softdep_flags &
1694 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1695 altump->softdep_flags |= FLUSH_CLEANUP;
1696 altump->um_softdep->sd_cleanups++;
1697 wakeup(&altump->softdep_flushtd);
1701 return (speedup_syncer());
1705 * Add an item to the end of the work queue.
1706 * This routine requires that the lock be held.
1707 * This is the only routine that adds items to the list.
1708 * The following routine is the only one that removes items
1709 * and does so in order from first to last.
1712 #define WK_HEAD 0x0001 /* Add to HEAD. */
1713 #define WK_NODELAY 0x0002 /* Process immediately. */
1716 add_to_worklist(wk, flags)
1717 struct worklist *wk;
1720 struct ufsmount *ump;
1722 ump = VFSTOUFS(wk->wk_mp);
1724 if (wk->wk_state & ONWORKLIST)
1725 panic("add_to_worklist: %s(0x%X) already on list",
1726 TYPENAME(wk->wk_type), wk->wk_state);
1727 wk->wk_state |= ONWORKLIST;
1728 if (ump->softdep_on_worklist == 0) {
1729 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1730 ump->softdep_worklist_tail = wk;
1731 } else if (flags & WK_HEAD) {
1732 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1734 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1735 ump->softdep_worklist_tail = wk;
1737 ump->softdep_on_worklist += 1;
1738 if (flags & WK_NODELAY)
1739 worklist_speedup(wk->wk_mp);
1743 * Remove the item to be processed. If we are removing the last
1744 * item on the list, we need to recalculate the tail pointer.
1747 remove_from_worklist(wk)
1748 struct worklist *wk;
1750 struct ufsmount *ump;
1752 ump = VFSTOUFS(wk->wk_mp);
1753 if (ump->softdep_worklist_tail == wk)
1754 ump->softdep_worklist_tail =
1755 (struct worklist *)wk->wk_list.le_prev;
1756 WORKLIST_REMOVE(wk);
1757 ump->softdep_on_worklist -= 1;
1762 struct worklist *wk;
1764 if (wk->wk_state & IOWAITING) {
1765 wk->wk_state &= ~IOWAITING;
1771 wait_worklist(wk, wmesg)
1772 struct worklist *wk;
1775 struct ufsmount *ump;
1777 ump = VFSTOUFS(wk->wk_mp);
1778 wk->wk_state |= IOWAITING;
1779 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1783 * Process that runs once per second to handle items in the background queue.
1785 * Note that we ensure that everything is done in the order in which they
1786 * appear in the queue. The code below depends on this property to ensure
1787 * that blocks of a file are freed before the inode itself is freed. This
1788 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1789 * until all the old ones have been purged from the dependency lists.
1792 softdep_process_worklist(mp, full)
1797 struct ufsmount *ump;
1800 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1802 if (ump->um_softdep == NULL)
1806 starttime = time_second;
1807 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1808 check_clear_deps(mp);
1809 while (ump->softdep_on_worklist > 0) {
1810 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1814 check_clear_deps(mp);
1816 * We do not generally want to stop for buffer space, but if
1817 * we are really being a buffer hog, we will stop and wait.
1819 if (should_yield()) {
1821 kern_yield(PRI_USER);
1826 * Never allow processing to run for more than one
1827 * second. This gives the syncer thread the opportunity
1828 * to pause if appropriate.
1830 if (!full && starttime != time_second)
1834 journal_unsuspend(ump);
1840 * Process all removes associated with a vnode if we are running out of
1841 * journal space. Any other process which attempts to flush these will
1842 * be unable as we have the vnodes locked.
1848 struct inodedep *inodedep;
1849 struct dirrem *dirrem;
1850 struct ufsmount *ump;
1857 inum = VTOI(vp)->i_number;
1860 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1862 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1864 * If another thread is trying to lock this vnode
1865 * it will fail but we must wait for it to do so
1866 * before we can proceed.
1868 if (dirrem->dm_state & INPROGRESS) {
1869 wait_worklist(&dirrem->dm_list, "pwrwait");
1872 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1873 (COMPLETE | ONWORKLIST))
1878 remove_from_worklist(&dirrem->dm_list);
1880 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1881 panic("process_removes: suspended filesystem");
1882 handle_workitem_remove(dirrem, 0);
1883 vn_finished_secondary_write(mp);
1889 * Process all truncations associated with a vnode if we are running out
1890 * of journal space. This is called when the vnode lock is already held
1891 * and no other process can clear the truncation. This function returns
1892 * a value greater than zero if it did any work.
1895 process_truncates(vp)
1898 struct inodedep *inodedep;
1899 struct freeblks *freeblks;
1900 struct ufsmount *ump;
1908 inum = VTOI(vp)->i_number;
1910 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1913 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1914 /* Journal entries not yet written. */
1915 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1917 &freeblks->fb_jblkdephd)->jb_list,
1921 /* Another thread is executing this item. */
1922 if (freeblks->fb_state & INPROGRESS) {
1923 wait_worklist(&freeblks->fb_list, "ptrwait");
1926 /* Freeblks is waiting on a inode write. */
1927 if ((freeblks->fb_state & COMPLETE) == 0) {
1933 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1934 (ALLCOMPLETE | ONWORKLIST)) {
1935 remove_from_worklist(&freeblks->fb_list);
1936 freeblks->fb_state |= INPROGRESS;
1938 if (vn_start_secondary_write(NULL, &mp,
1940 panic("process_truncates: "
1941 "suspended filesystem");
1942 handle_workitem_freeblocks(freeblks, 0);
1943 vn_finished_secondary_write(mp);
1947 if (freeblks->fb_cgwait)
1952 sync_cgs(mp, MNT_WAIT);
1953 ffs_sync_snap(mp, MNT_WAIT);
1957 if (freeblks == NULL)
1964 * Process one item on the worklist.
1967 process_worklist_item(mp, target, flags)
1972 struct worklist sentinel;
1973 struct worklist *wk;
1974 struct ufsmount *ump;
1978 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1980 * If we are being called because of a process doing a
1981 * copy-on-write, then it is not safe to write as we may
1982 * recurse into the copy-on-write routine.
1984 if (curthread->td_pflags & TDP_COWINPROGRESS)
1986 PHOLD(curproc); /* Don't let the stack go away. */
1990 sentinel.wk_mp = NULL;
1991 sentinel.wk_type = D_SENTINEL;
1992 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1993 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1994 wk = LIST_NEXT(&sentinel, wk_list)) {
1995 if (wk->wk_type == D_SENTINEL) {
1996 LIST_REMOVE(&sentinel, wk_list);
1997 LIST_INSERT_AFTER(wk, &sentinel, wk_list);
2000 if (wk->wk_state & INPROGRESS)
2001 panic("process_worklist_item: %p already in progress.",
2003 wk->wk_state |= INPROGRESS;
2004 remove_from_worklist(wk);
2006 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
2007 panic("process_worklist_item: suspended filesystem");
2008 switch (wk->wk_type) {
2010 /* removal of a directory entry */
2011 error = handle_workitem_remove(WK_DIRREM(wk), flags);
2015 /* releasing blocks and/or fragments from a file */
2016 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
2021 /* releasing a fragment when replaced as a file grows */
2022 handle_workitem_freefrag(WK_FREEFRAG(wk));
2027 /* releasing an inode when its link count drops to 0 */
2028 handle_workitem_freefile(WK_FREEFILE(wk));
2033 panic("%s_process_worklist: Unknown type %s",
2034 "softdep", TYPENAME(wk->wk_type));
2037 vn_finished_secondary_write(mp);
2040 if (++matchcnt == target)
2045 * We have to retry the worklist item later. Wake up any
2046 * waiters who may be able to complete it immediately and
2047 * add the item back to the head so we don't try to execute
2050 wk->wk_state &= ~INPROGRESS;
2052 add_to_worklist(wk, WK_HEAD);
2054 /* Sentinal could've become the tail from remove_from_worklist. */
2055 if (ump->softdep_worklist_tail == &sentinel)
2056 ump->softdep_worklist_tail =
2057 (struct worklist *)sentinel.wk_list.le_prev;
2058 LIST_REMOVE(&sentinel, wk_list);
2064 * Move dependencies from one buffer to another.
2067 softdep_move_dependencies(oldbp, newbp)
2071 struct worklist *wk, *wktail;
2072 struct ufsmount *ump;
2075 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
2077 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
2078 ("softdep_move_dependencies called on non-softdep filesystem"));
2081 ump = VFSTOUFS(wk->wk_mp);
2083 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
2084 LIST_REMOVE(wk, wk_list);
2085 if (wk->wk_type == D_BMSAFEMAP &&
2086 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
2089 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
2091 LIST_INSERT_AFTER(wktail, wk, wk_list);
2100 * Purge the work list of all items associated with a particular mount point.
2103 softdep_flushworklist(oldmnt, countp, td)
2104 struct mount *oldmnt;
2108 struct vnode *devvp;
2109 struct ufsmount *ump;
2113 * Alternately flush the block device associated with the mount
2114 * point and process any dependencies that the flushing
2115 * creates. We continue until no more worklist dependencies
2120 ump = VFSTOUFS(oldmnt);
2121 devvp = ump->um_devvp;
2122 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
2124 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2125 error = VOP_FSYNC(devvp, MNT_WAIT, td);
2133 #define SU_WAITIDLE_RETRIES 20
2135 softdep_waitidle(struct mount *mp, int flags __unused)
2137 struct ufsmount *ump;
2138 struct vnode *devvp;
2143 KASSERT(ump->um_softdep != NULL,
2144 ("softdep_waitidle called on non-softdep filesystem"));
2145 devvp = ump->um_devvp;
2149 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
2150 ump->softdep_req = 1;
2151 KASSERT((flags & FORCECLOSE) == 0 ||
2152 ump->softdep_on_worklist == 0,
2153 ("softdep_waitidle: work added after flush"));
2154 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
2155 "softdeps", 10 * hz);
2156 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2157 error = VOP_FSYNC(devvp, MNT_WAIT, td);
2163 ump->softdep_req = 0;
2164 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
2166 printf("softdep_waitidle: Failed to flush worklist for %p\n",
2174 * Flush all vnodes and worklist items associated with a specified mount point.
2177 softdep_flushfiles(oldmnt, flags, td)
2178 struct mount *oldmnt;
2182 struct ufsmount *ump;
2186 int error, early, depcount, loopcnt, retry_flush_count, retry;
2189 ump = VFSTOUFS(oldmnt);
2190 KASSERT(ump->um_softdep != NULL,
2191 ("softdep_flushfiles called on non-softdep filesystem"));
2193 retry_flush_count = 3;
2198 * Alternately flush the vnodes associated with the mount
2199 * point and process any dependencies that the flushing
2200 * creates. In theory, this loop can happen at most twice,
2201 * but we give it a few extra just to be sure.
2203 for (; loopcnt > 0; loopcnt--) {
2205 * Do another flush in case any vnodes were brought in
2206 * as part of the cleanup operations.
2208 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2209 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2210 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2212 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2217 * If we are unmounting then it is an error to fail. If we
2218 * are simply trying to downgrade to read-only, then filesystem
2219 * activity can keep us busy forever, so we just fail with EBUSY.
2222 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2223 panic("softdep_flushfiles: looping");
2227 error = softdep_waitidle(oldmnt, flags);
2229 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2232 morework = oldmnt->mnt_nvnodelistsize > 0;
2235 for (i = 0; i < MAXQUOTAS; i++) {
2236 if (ump->um_quotas[i] != NULLVP)
2242 if (--retry_flush_count > 0) {
2248 MNT_IUNLOCK(oldmnt);
2257 * Structure hashing.
2259 * There are four types of structures that can be looked up:
2260 * 1) pagedep structures identified by mount point, inode number,
2261 * and logical block.
2262 * 2) inodedep structures identified by mount point and inode number.
2263 * 3) newblk structures identified by mount point and
2264 * physical block number.
2265 * 4) bmsafemap structures identified by mount point and
2266 * cylinder group number.
2268 * The "pagedep" and "inodedep" dependency structures are hashed
2269 * separately from the file blocks and inodes to which they correspond.
2270 * This separation helps when the in-memory copy of an inode or
2271 * file block must be replaced. It also obviates the need to access
2272 * an inode or file page when simply updating (or de-allocating)
2273 * dependency structures. Lookup of newblk structures is needed to
2274 * find newly allocated blocks when trying to associate them with
2275 * their allocdirect or allocindir structure.
2277 * The lookup routines optionally create and hash a new instance when
2278 * an existing entry is not found. The bmsafemap lookup routine always
2279 * allocates a new structure if an existing one is not found.
2281 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2284 * Structures and routines associated with pagedep caching.
2286 #define PAGEDEP_HASH(ump, inum, lbn) \
2287 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2290 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2291 struct pagedep_hashhead *pagedephd;
2294 struct pagedep **pagedeppp;
2296 struct pagedep *pagedep;
2298 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2299 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2300 *pagedeppp = pagedep;
2308 * Look up a pagedep. Return 1 if found, 0 otherwise.
2309 * If not found, allocate if DEPALLOC flag is passed.
2310 * Found or allocated entry is returned in pagedeppp.
2313 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2319 struct pagedep **pagedeppp;
2321 struct pagedep *pagedep;
2322 struct pagedep_hashhead *pagedephd;
2323 struct worklist *wk;
2324 struct ufsmount *ump;
2331 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2332 if (wk->wk_type == D_PAGEDEP) {
2333 *pagedeppp = WK_PAGEDEP(wk);
2338 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2339 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2341 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2342 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2345 if ((flags & DEPALLOC) == 0)
2348 pagedep = malloc(sizeof(struct pagedep),
2349 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2350 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2352 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2355 * This should never happen since we only create pagedeps
2356 * with the vnode lock held. Could be an assert.
2358 WORKITEM_FREE(pagedep, D_PAGEDEP);
2361 pagedep->pd_ino = ino;
2362 pagedep->pd_lbn = lbn;
2363 LIST_INIT(&pagedep->pd_dirremhd);
2364 LIST_INIT(&pagedep->pd_pendinghd);
2365 for (i = 0; i < DAHASHSZ; i++)
2366 LIST_INIT(&pagedep->pd_diraddhd[i]);
2367 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2368 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2369 *pagedeppp = pagedep;
2374 * Structures and routines associated with inodedep caching.
2376 #define INODEDEP_HASH(ump, inum) \
2377 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2380 inodedep_find(inodedephd, inum, inodedeppp)
2381 struct inodedep_hashhead *inodedephd;
2383 struct inodedep **inodedeppp;
2385 struct inodedep *inodedep;
2387 LIST_FOREACH(inodedep, inodedephd, id_hash)
2388 if (inum == inodedep->id_ino)
2391 *inodedeppp = inodedep;
2399 * Look up an inodedep. Return 1 if found, 0 if not found.
2400 * If not found, allocate if DEPALLOC flag is passed.
2401 * Found or allocated entry is returned in inodedeppp.
2404 inodedep_lookup(mp, inum, flags, inodedeppp)
2408 struct inodedep **inodedeppp;
2410 struct inodedep *inodedep;
2411 struct inodedep_hashhead *inodedephd;
2412 struct ufsmount *ump;
2418 inodedephd = INODEDEP_HASH(ump, inum);
2420 if (inodedep_find(inodedephd, inum, inodedeppp))
2422 if ((flags & DEPALLOC) == 0)
2425 * If the system is over its limit and our filesystem is
2426 * responsible for more than our share of that usage and
2427 * we are not in a rush, request some inodedep cleanup.
2429 if (softdep_excess_items(ump, D_INODEDEP))
2430 schedule_cleanup(mp);
2433 inodedep = malloc(sizeof(struct inodedep),
2434 M_INODEDEP, M_SOFTDEP_FLAGS);
2435 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2437 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2438 WORKITEM_FREE(inodedep, D_INODEDEP);
2441 inodedep->id_fs = fs;
2442 inodedep->id_ino = inum;
2443 inodedep->id_state = ALLCOMPLETE;
2444 inodedep->id_nlinkdelta = 0;
2445 inodedep->id_nlinkwrote = -1;
2446 inodedep->id_savedino1 = NULL;
2447 inodedep->id_savedsize = -1;
2448 inodedep->id_savedextsize = -1;
2449 inodedep->id_savednlink = -1;
2450 inodedep->id_bmsafemap = NULL;
2451 inodedep->id_mkdiradd = NULL;
2452 LIST_INIT(&inodedep->id_dirremhd);
2453 LIST_INIT(&inodedep->id_pendinghd);
2454 LIST_INIT(&inodedep->id_inowait);
2455 LIST_INIT(&inodedep->id_bufwait);
2456 TAILQ_INIT(&inodedep->id_inoreflst);
2457 TAILQ_INIT(&inodedep->id_inoupdt);
2458 TAILQ_INIT(&inodedep->id_newinoupdt);
2459 TAILQ_INIT(&inodedep->id_extupdt);
2460 TAILQ_INIT(&inodedep->id_newextupdt);
2461 TAILQ_INIT(&inodedep->id_freeblklst);
2462 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2463 *inodedeppp = inodedep;
2468 * Structures and routines associated with newblk caching.
2470 #define NEWBLK_HASH(ump, inum) \
2471 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2474 newblk_find(newblkhd, newblkno, flags, newblkpp)
2475 struct newblk_hashhead *newblkhd;
2476 ufs2_daddr_t newblkno;
2478 struct newblk **newblkpp;
2480 struct newblk *newblk;
2482 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2483 if (newblkno != newblk->nb_newblkno)
2486 * If we're creating a new dependency don't match those that
2487 * have already been converted to allocdirects. This is for
2490 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2503 * Look up a newblk. Return 1 if found, 0 if not found.
2504 * If not found, allocate if DEPALLOC flag is passed.
2505 * Found or allocated entry is returned in newblkpp.
2508 newblk_lookup(mp, newblkno, flags, newblkpp)
2510 ufs2_daddr_t newblkno;
2512 struct newblk **newblkpp;
2514 struct newblk *newblk;
2515 struct newblk_hashhead *newblkhd;
2516 struct ufsmount *ump;
2520 newblkhd = NEWBLK_HASH(ump, newblkno);
2521 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2523 if ((flags & DEPALLOC) == 0)
2525 if (softdep_excess_items(ump, D_NEWBLK) ||
2526 softdep_excess_items(ump, D_ALLOCDIRECT) ||
2527 softdep_excess_items(ump, D_ALLOCINDIR))
2528 schedule_cleanup(mp);
2531 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2532 M_SOFTDEP_FLAGS | M_ZERO);
2533 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2535 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2536 WORKITEM_FREE(newblk, D_NEWBLK);
2539 newblk->nb_freefrag = NULL;
2540 LIST_INIT(&newblk->nb_indirdeps);
2541 LIST_INIT(&newblk->nb_newdirblk);
2542 LIST_INIT(&newblk->nb_jwork);
2543 newblk->nb_state = ATTACHED;
2544 newblk->nb_newblkno = newblkno;
2545 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2551 * Structures and routines associated with freed indirect block caching.
2553 #define INDIR_HASH(ump, blkno) \
2554 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2557 * Lookup an indirect block in the indir hash table. The freework is
2558 * removed and potentially freed. The caller must do a blocking journal
2559 * write before writing to the blkno.
2562 indirblk_lookup(mp, blkno)
2566 struct freework *freework;
2567 struct indir_hashhead *wkhd;
2568 struct ufsmount *ump;
2571 wkhd = INDIR_HASH(ump, blkno);
2572 TAILQ_FOREACH(freework, wkhd, fw_next) {
2573 if (freework->fw_blkno != blkno)
2575 indirblk_remove(freework);
2582 * Insert an indirect block represented by freework into the indirblk
2583 * hash table so that it may prevent the block from being re-used prior
2584 * to the journal being written.
2587 indirblk_insert(freework)
2588 struct freework *freework;
2590 struct jblocks *jblocks;
2592 struct ufsmount *ump;
2594 ump = VFSTOUFS(freework->fw_list.wk_mp);
2595 jblocks = ump->softdep_jblocks;
2596 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2600 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2601 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2603 freework->fw_state &= ~DEPCOMPLETE;
2607 indirblk_remove(freework)
2608 struct freework *freework;
2610 struct ufsmount *ump;
2612 ump = VFSTOUFS(freework->fw_list.wk_mp);
2613 LIST_REMOVE(freework, fw_segs);
2614 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2615 freework->fw_state |= DEPCOMPLETE;
2616 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2617 WORKITEM_FREE(freework, D_FREEWORK);
2621 * Executed during filesystem system initialization before
2622 * mounting any filesystems.
2625 softdep_initialize()
2628 TAILQ_INIT(&softdepmounts);
2630 max_softdeps = desiredvnodes * 4;
2632 max_softdeps = desiredvnodes * 2;
2635 /* initialise bioops hack */
2636 bioops.io_start = softdep_disk_io_initiation;
2637 bioops.io_complete = softdep_disk_write_complete;
2638 bioops.io_deallocate = softdep_deallocate_dependencies;
2639 bioops.io_countdeps = softdep_count_dependencies;
2640 softdep_ast_cleanup = softdep_ast_cleanup_proc;
2642 /* Initialize the callout with an mtx. */
2643 callout_init_mtx(&softdep_callout, &lk, 0);
2647 * Executed after all filesystems have been unmounted during
2648 * filesystem module unload.
2651 softdep_uninitialize()
2654 /* clear bioops hack */
2655 bioops.io_start = NULL;
2656 bioops.io_complete = NULL;
2657 bioops.io_deallocate = NULL;
2658 bioops.io_countdeps = NULL;
2659 softdep_ast_cleanup = NULL;
2661 callout_drain(&softdep_callout);
2665 * Called at mount time to notify the dependency code that a
2666 * filesystem wishes to use it.
2669 softdep_mount(devvp, mp, fs, cred)
2670 struct vnode *devvp;
2675 struct csum_total cstotal;
2676 struct mount_softdeps *sdp;
2677 struct ufsmount *ump;
2685 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2687 rw_init(&sdp->sd_fslock, "SUrw");
2689 LIST_INIT(&sdp->sd_workitem_pending);
2690 LIST_INIT(&sdp->sd_journal_pending);
2691 TAILQ_INIT(&sdp->sd_unlinked);
2692 LIST_INIT(&sdp->sd_dirtycg);
2693 sdp->sd_worklist_tail = NULL;
2694 sdp->sd_on_worklist = 0;
2696 LIST_INIT(&sdp->sd_mkdirlisthd);
2697 sdp->sd_pdhash = hashinit(desiredvnodes / 5, M_PAGEDEP,
2698 &sdp->sd_pdhashsize);
2699 sdp->sd_pdnextclean = 0;
2700 sdp->sd_idhash = hashinit(desiredvnodes, M_INODEDEP,
2701 &sdp->sd_idhashsize);
2702 sdp->sd_idnextclean = 0;
2703 sdp->sd_newblkhash = hashinit(max_softdeps / 2, M_NEWBLK,
2704 &sdp->sd_newblkhashsize);
2705 sdp->sd_bmhash = hashinit(1024, M_BMSAFEMAP, &sdp->sd_bmhashsize);
2706 i = 1 << (ffs(desiredvnodes / 10) - 1);
2707 sdp->sd_indirhash = malloc(i * sizeof(struct indir_hashhead),
2708 M_FREEWORK, M_WAITOK);
2709 sdp->sd_indirhashsize = i - 1;
2710 for (i = 0; i <= sdp->sd_indirhashsize; i++)
2711 TAILQ_INIT(&sdp->sd_indirhash[i]);
2713 for (i = 0; i <= D_LAST; i++)
2714 LIST_INIT(&sdp->sd_alldeps[i]);
2716 ACQUIRE_GBLLOCK(&lk);
2717 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2720 ump->um_softdep = sdp;
2722 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2723 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2724 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2725 MNTK_SOFTDEP | MNTK_NOASYNC;
2729 if ((fs->fs_flags & FS_SUJ) &&
2730 (error = journal_mount(mp, fs, cred)) != 0) {
2731 printf("Failed to start journal: %d\n", error);
2732 softdep_unmount(mp);
2736 * Start our flushing thread in the bufdaemon process.
2739 ump->softdep_flags |= FLUSH_STARTING;
2741 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2742 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2743 mp->mnt_stat.f_mntonname);
2745 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2746 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2751 * When doing soft updates, the counters in the
2752 * superblock may have gotten out of sync. Recomputation
2753 * can take a long time and can be deferred for background
2754 * fsck. However, the old behavior of scanning the cylinder
2755 * groups and recalculating them at mount time is available
2756 * by setting vfs.ffs.compute_summary_at_mount to one.
2758 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2760 bzero(&cstotal, sizeof cstotal);
2761 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2762 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2763 fs->fs_cgsize, cred, &bp)) != 0) {
2765 softdep_unmount(mp);
2768 cgp = (struct cg *)bp->b_data;
2769 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2770 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2771 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2772 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2773 fs->fs_cs(fs, cyl) = cgp->cg_cs;
2777 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2778 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2780 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2788 struct ufsmount *ump;
2789 struct mount_softdeps *ums;
2792 KASSERT(ump->um_softdep != NULL,
2793 ("softdep_unmount called on non-softdep filesystem"));
2795 mp->mnt_flag &= ~MNT_SOFTDEP;
2796 if ((mp->mnt_flag & MNT_SUJ) == 0) {
2799 mp->mnt_flag &= ~MNT_SUJ;
2801 journal_unmount(ump);
2804 * Shut down our flushing thread. Check for NULL is if
2805 * softdep_mount errors out before the thread has been created.
2807 if (ump->softdep_flushtd != NULL) {
2809 ump->softdep_flags |= FLUSH_EXIT;
2810 wakeup(&ump->softdep_flushtd);
2811 while ((ump->softdep_flags & FLUSH_EXIT) != 0) {
2812 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM,
2815 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2816 ("Thread shutdown failed"));
2821 * We are no longer have softdep structure attached to ump.
2823 ums = ump->um_softdep;
2824 ACQUIRE_GBLLOCK(&lk);
2825 TAILQ_REMOVE(&softdepmounts, ums, sd_next);
2827 ump->um_softdep = NULL;
2829 KASSERT(ums->sd_on_journal == 0,
2830 ("ump %p ums %p on_journal %d", ump, ums, ums->sd_on_journal));
2831 KASSERT(ums->sd_on_worklist == 0,
2832 ("ump %p ums %p on_worklist %d", ump, ums, ums->sd_on_worklist));
2833 KASSERT(ums->sd_deps == 0,
2834 ("ump %p ums %p deps %d", ump, ums, ums->sd_deps));
2837 * Free up our resources.
2839 rw_destroy(&ums->sd_fslock);
2840 hashdestroy(ums->sd_pdhash, M_PAGEDEP, ums->sd_pdhashsize);
2841 hashdestroy(ums->sd_idhash, M_INODEDEP, ums->sd_idhashsize);
2842 hashdestroy(ums->sd_newblkhash, M_NEWBLK, ums->sd_newblkhashsize);
2843 hashdestroy(ums->sd_bmhash, M_BMSAFEMAP, ums->sd_bmhashsize);
2844 free(ums->sd_indirhash, M_FREEWORK);
2846 for (int i = 0; i <= D_LAST; i++) {
2847 KASSERT(ums->sd_curdeps[i] == 0,
2848 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2849 TYPENAME(i), ums->sd_curdeps[i]));
2850 KASSERT(LIST_EMPTY(&ums->sd_alldeps[i]),
2851 ("Unmount %s: Dep type %s not empty (%p)",
2852 ump->um_fs->fs_fsmnt,
2853 TYPENAME(i), LIST_FIRST(&ums->sd_alldeps[i])));
2856 free(ums, M_MOUNTDATA);
2859 static struct jblocks *
2860 jblocks_create(void)
2862 struct jblocks *jblocks;
2864 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2865 TAILQ_INIT(&jblocks->jb_segs);
2866 jblocks->jb_avail = 10;
2867 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2868 M_JBLOCKS, M_WAITOK | M_ZERO);
2874 jblocks_alloc(jblocks, bytes, actual)
2875 struct jblocks *jblocks;
2880 struct jextent *jext;
2884 blocks = bytes / DEV_BSIZE;
2885 jext = &jblocks->jb_extent[jblocks->jb_head];
2886 freecnt = jext->je_blocks - jblocks->jb_off;
2888 jblocks->jb_off = 0;
2889 if (++jblocks->jb_head > jblocks->jb_used)
2890 jblocks->jb_head = 0;
2891 jext = &jblocks->jb_extent[jblocks->jb_head];
2892 freecnt = jext->je_blocks;
2894 if (freecnt > blocks)
2896 *actual = freecnt * DEV_BSIZE;
2897 daddr = jext->je_daddr + jblocks->jb_off;
2898 jblocks->jb_off += freecnt;
2899 jblocks->jb_free -= freecnt;
2905 jblocks_free(jblocks, mp, bytes)
2906 struct jblocks *jblocks;
2911 LOCK_OWNED(VFSTOUFS(mp));
2912 jblocks->jb_free += bytes / DEV_BSIZE;
2913 if (jblocks->jb_suspended)
2914 worklist_speedup(mp);
2919 jblocks_destroy(jblocks)
2920 struct jblocks *jblocks;
2923 if (jblocks->jb_extent)
2924 free(jblocks->jb_extent, M_JBLOCKS);
2925 free(jblocks, M_JBLOCKS);
2929 jblocks_add(jblocks, daddr, blocks)
2930 struct jblocks *jblocks;
2934 struct jextent *jext;
2936 jblocks->jb_blocks += blocks;
2937 jblocks->jb_free += blocks;
2938 jext = &jblocks->jb_extent[jblocks->jb_used];
2939 /* Adding the first block. */
2940 if (jext->je_daddr == 0) {
2941 jext->je_daddr = daddr;
2942 jext->je_blocks = blocks;
2945 /* Extending the last extent. */
2946 if (jext->je_daddr + jext->je_blocks == daddr) {
2947 jext->je_blocks += blocks;
2950 /* Adding a new extent. */
2951 if (++jblocks->jb_used == jblocks->jb_avail) {
2952 jblocks->jb_avail *= 2;
2953 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2954 M_JBLOCKS, M_WAITOK | M_ZERO);
2955 memcpy(jext, jblocks->jb_extent,
2956 sizeof(struct jextent) * jblocks->jb_used);
2957 free(jblocks->jb_extent, M_JBLOCKS);
2958 jblocks->jb_extent = jext;
2960 jext = &jblocks->jb_extent[jblocks->jb_used];
2961 jext->je_daddr = daddr;
2962 jext->je_blocks = blocks;
2967 softdep_journal_lookup(mp, vpp)
2971 struct componentname cnp;
2976 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2979 bzero(&cnp, sizeof(cnp));
2980 cnp.cn_nameiop = LOOKUP;
2981 cnp.cn_flags = ISLASTCN;
2982 cnp.cn_thread = curthread;
2983 cnp.cn_cred = curthread->td_ucred;
2984 cnp.cn_pnbuf = SUJ_FILE;
2985 cnp.cn_nameptr = SUJ_FILE;
2986 cnp.cn_namelen = strlen(SUJ_FILE);
2987 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2991 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2996 * Open and verify the journal file.
2999 journal_mount(mp, fs, cred)
3004 struct jblocks *jblocks;
3005 struct ufsmount *ump;
3014 ump->softdep_journal_tail = NULL;
3015 ump->softdep_on_journal = 0;
3016 ump->softdep_accdeps = 0;
3017 ump->softdep_req = 0;
3018 ump->softdep_jblocks = NULL;
3019 error = softdep_journal_lookup(mp, &vp);
3021 printf("Failed to find journal. Use tunefs to create one\n");
3025 if (ip->i_size < SUJ_MIN) {
3029 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */
3030 jblocks = jblocks_create();
3031 for (i = 0; i < bcount; i++) {
3032 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
3035 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
3038 jblocks_destroy(jblocks);
3041 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */
3042 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
3043 ump->softdep_jblocks = jblocks;
3046 mp->mnt_flag |= MNT_SUJ;
3050 * Only validate the journal contents if the
3051 * filesystem is clean, otherwise we write the logs
3052 * but they'll never be used. If the filesystem was
3053 * still dirty when we mounted it the journal is
3054 * invalid and a new journal can only be valid if it
3055 * starts from a clean mount.
3058 DIP_SET(ip, i_modrev, fs->fs_mtime);
3059 ip->i_flags |= IN_MODIFIED;
3068 journal_unmount(ump)
3069 struct ufsmount *ump;
3072 if (ump->softdep_jblocks)
3073 jblocks_destroy(ump->softdep_jblocks);
3074 ump->softdep_jblocks = NULL;
3078 * Called when a journal record is ready to be written. Space is allocated
3079 * and the journal entry is created when the journal is flushed to stable
3084 struct worklist *wk;
3086 struct ufsmount *ump;
3088 ump = VFSTOUFS(wk->wk_mp);
3090 if (wk->wk_state & ONWORKLIST)
3091 panic("add_to_journal: %s(0x%X) already on list",
3092 TYPENAME(wk->wk_type), wk->wk_state);
3093 wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
3094 if (LIST_EMPTY(&ump->softdep_journal_pending)) {
3095 ump->softdep_jblocks->jb_age = ticks;
3096 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
3098 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
3099 ump->softdep_journal_tail = wk;
3100 ump->softdep_on_journal += 1;
3104 * Remove an arbitrary item for the journal worklist maintain the tail
3105 * pointer. This happens when a new operation obviates the need to
3106 * journal an old operation.
3109 remove_from_journal(wk)
3110 struct worklist *wk;
3112 struct ufsmount *ump;
3114 ump = VFSTOUFS(wk->wk_mp);
3118 struct worklist *wkn;
3120 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
3124 panic("remove_from_journal: %p is not in journal", wk);
3128 * We emulate a TAILQ to save space in most structures which do not
3129 * require TAILQ semantics. Here we must update the tail position
3130 * when removing the tail which is not the final entry. This works
3131 * only if the worklist linkage are at the beginning of the structure.
3133 if (ump->softdep_journal_tail == wk)
3134 ump->softdep_journal_tail =
3135 (struct worklist *)wk->wk_list.le_prev;
3136 WORKLIST_REMOVE(wk);
3137 ump->softdep_on_journal -= 1;
3141 * Check for journal space as well as dependency limits so the prelink
3142 * code can throttle both journaled and non-journaled filesystems.
3143 * Threshold is 0 for low and 1 for min.
3146 journal_space(ump, thresh)
3147 struct ufsmount *ump;
3150 struct jblocks *jblocks;
3153 jblocks = ump->softdep_jblocks;
3154 if (jblocks == NULL)
3157 * We use a tighter restriction here to prevent request_cleanup()
3158 * running in threads from running into locks we currently hold.
3159 * We have to be over the limit and our filesystem has to be
3160 * responsible for more than our share of that usage.
3162 limit = (max_softdeps / 10) * 9;
3163 if (dep_current[D_INODEDEP] > limit &&
3164 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
3167 thresh = jblocks->jb_min;
3169 thresh = jblocks->jb_low;
3170 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
3171 avail = jblocks->jb_free - avail;
3173 return (avail > thresh);
3177 journal_suspend(ump)
3178 struct ufsmount *ump;
3180 struct jblocks *jblocks;
3185 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0)
3188 jblocks = ump->softdep_jblocks;
3192 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
3194 mp->mnt_kern_flag |= MNTK_SUSPEND;
3195 mp->mnt_susp_owner = ump->softdep_flushtd;
3198 jblocks->jb_suspended = 1;
3205 journal_unsuspend(struct ufsmount *ump)
3207 struct jblocks *jblocks;
3211 jblocks = ump->softdep_jblocks;
3213 if (jblocks != NULL && jblocks->jb_suspended &&
3214 journal_space(ump, jblocks->jb_min)) {
3215 jblocks->jb_suspended = 0;
3217 mp->mnt_susp_owner = curthread;
3218 vfs_write_resume(mp, 0);
3226 journal_check_space(ump)
3227 struct ufsmount *ump;
3233 if (journal_space(ump, 0) == 0) {
3234 softdep_speedup(ump);
3237 VFS_SYNC(mp, MNT_NOWAIT);
3238 ffs_sbupdate(ump, MNT_WAIT, 0);
3240 if (journal_space(ump, 1) == 0)
3241 journal_suspend(ump);
3246 * Called before any allocation function to be certain that there is
3247 * sufficient space in the journal prior to creating any new records.
3248 * Since in the case of block allocation we may have multiple locked
3249 * buffers at the time of the actual allocation we can not block
3250 * when the journal records are created. Doing so would create a deadlock
3251 * if any of these buffers needed to be flushed to reclaim space. Instead
3252 * we require a sufficiently large amount of available space such that
3253 * each thread in the system could have passed this allocation check and
3254 * still have sufficient free space. With 20% of a minimum journal size
3255 * of 1MB we have 6553 records available.
3258 softdep_prealloc(vp, waitok)
3262 struct ufsmount *ump;
3264 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3265 ("softdep_prealloc called on non-softdep filesystem"));
3267 * Nothing to do if we are not running journaled soft updates.
3268 * If we currently hold the snapshot lock, we must avoid
3269 * handling other resources that could cause deadlock. Do not
3270 * touch quotas vnode since it is typically recursed with
3271 * other vnode locks held.
3273 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3274 (vp->v_vflag & VV_SYSTEM) != 0)
3276 ump = VFSTOUFS(vp->v_mount);
3278 if (journal_space(ump, 0)) {
3284 if (waitok == MNT_NOWAIT)
3287 * Attempt to sync this vnode once to flush any journal
3288 * work attached to it.
3290 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3291 ffs_syncvnode(vp, waitok, 0);
3293 process_removes(vp);
3294 process_truncates(vp);
3295 journal_check_space(ump);
3302 * Try hard to sync all data and metadata for the vnode, and workitems
3303 * flushing which might conflict with the vnode lock. This is a
3304 * helper for softdep_prerename().
3307 softdep_prerename_vnode(ump, vp)
3308 struct ufsmount *ump;
3313 ASSERT_VOP_ELOCKED(vp, "prehandle");
3314 if (vp->v_data == NULL)
3316 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3320 process_removes(vp);
3321 process_truncates(vp);
3327 * Must be called from VOP_RENAME() after all vnodes are locked.
3328 * Ensures that there is enough journal space for rename. It is
3329 * sufficiently different from softdep_prelink() by having to handle
3333 softdep_prerename(fdvp, fvp, tdvp, tvp)
3339 struct ufsmount *ump;
3342 ump = VFSTOUFS(fdvp->v_mount);
3344 if (journal_space(ump, 0))
3349 if (tvp != NULL && tvp != tdvp)
3352 error = softdep_prerename_vnode(ump, fdvp);
3357 VOP_LOCK(fvp, LK_EXCLUSIVE | LK_RETRY);
3358 error = softdep_prerename_vnode(ump, fvp);
3364 VOP_LOCK(tdvp, LK_EXCLUSIVE | LK_RETRY);
3365 error = softdep_prerename_vnode(ump, tdvp);
3371 if (tvp != fvp && tvp != NULL) {
3372 VOP_LOCK(tvp, LK_EXCLUSIVE | LK_RETRY);
3373 error = softdep_prerename_vnode(ump, tvp);
3380 softdep_speedup(ump);
3381 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3382 journal_check_space(ump);
3388 * Before adjusting a link count on a vnode verify that we have sufficient
3389 * journal space. If not, process operations that depend on the currently
3390 * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3391 * and softdep flush threads can not acquire these locks to reclaim space.
3393 * Returns 0 if all owned locks are still valid and were not dropped
3394 * in the process, in other case it returns either an error from sync,
3395 * or ERELOOKUP if any of the locks were re-acquired. In the later
3396 * case, the state of the vnodes cannot be relied upon and our VFS
3397 * syscall must be restarted at top level from the lookup.
3400 softdep_prelink(dvp, vp, cnp)
3403 struct componentname *cnp;
3405 struct ufsmount *ump;
3406 struct nameidata *ndp;
3408 ASSERT_VOP_ELOCKED(dvp, "prelink dvp");
3410 ASSERT_VOP_ELOCKED(vp, "prelink vp");
3411 ump = VFSTOUFS(dvp->v_mount);
3414 * Nothing to do if we have sufficient journal space. We skip
3415 * flushing when vp is a snapshot to avoid deadlock where
3416 * another thread is trying to update the inodeblock for dvp
3417 * and is waiting on snaplk that vp holds.
3419 if (journal_space(ump, 0) || (vp != NULL && IS_SNAPSHOT(VTOI(vp))))
3423 * Check if the journal space consumption can in theory be
3424 * accounted on dvp and vp. If the vnodes metadata was not
3425 * changed comparing with the previous round-trip into
3426 * softdep_prelink(), as indicated by the seqc generation
3427 * recorded in the nameidata, then there is no point in
3428 * starting the sync.
3430 ndp = __containerof(cnp, struct nameidata, ni_cnd);
3431 if (!seqc_in_modify(ndp->ni_dvp_seqc) &&
3432 vn_seqc_consistent(dvp, ndp->ni_dvp_seqc) &&
3433 (vp == NULL || (!seqc_in_modify(ndp->ni_vp_seqc) &&
3434 vn_seqc_consistent(vp, ndp->ni_vp_seqc))))
3440 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3441 vn_lock_pair(dvp, false, vp, true);
3442 if (dvp->v_data == NULL)
3447 ffs_syncvnode(dvp, MNT_WAIT, 0);
3448 /* Process vp before dvp as it may create .. removes. */
3451 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3452 if (vp->v_data == NULL) {
3453 vn_lock_pair(dvp, false, vp, true);
3457 process_removes(vp);
3458 process_truncates(vp);
3461 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
3462 if (dvp->v_data == NULL) {
3463 vn_lock_pair(dvp, true, vp, false);
3469 process_removes(dvp);
3470 process_truncates(dvp);
3472 softdep_speedup(ump);
3474 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3475 journal_check_space(ump);
3478 vn_lock_pair(dvp, false, vp, false);
3480 ndp->ni_dvp_seqc = vn_seqc_read_any(dvp);
3482 ndp->ni_vp_seqc = vn_seqc_read_any(vp);
3487 jseg_write(ump, jseg, data)
3488 struct ufsmount *ump;
3492 struct jsegrec *rec;
3494 rec = (struct jsegrec *)data;
3495 rec->jsr_seq = jseg->js_seq;
3496 rec->jsr_oldest = jseg->js_oldseq;
3497 rec->jsr_cnt = jseg->js_cnt;
3498 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3500 rec->jsr_time = ump->um_fs->fs_mtime;
3504 inoref_write(inoref, jseg, rec)
3505 struct inoref *inoref;
3507 struct jrefrec *rec;
3510 inoref->if_jsegdep->jd_seg = jseg;
3511 rec->jr_ino = inoref->if_ino;
3512 rec->jr_parent = inoref->if_parent;
3513 rec->jr_nlink = inoref->if_nlink;
3514 rec->jr_mode = inoref->if_mode;
3515 rec->jr_diroff = inoref->if_diroff;
3519 jaddref_write(jaddref, jseg, data)
3520 struct jaddref *jaddref;
3524 struct jrefrec *rec;
3526 rec = (struct jrefrec *)data;
3527 rec->jr_op = JOP_ADDREF;
3528 inoref_write(&jaddref->ja_ref, jseg, rec);
3532 jremref_write(jremref, jseg, data)
3533 struct jremref *jremref;
3537 struct jrefrec *rec;
3539 rec = (struct jrefrec *)data;
3540 rec->jr_op = JOP_REMREF;
3541 inoref_write(&jremref->jr_ref, jseg, rec);
3545 jmvref_write(jmvref, jseg, data)
3546 struct jmvref *jmvref;
3552 rec = (struct jmvrec *)data;
3553 rec->jm_op = JOP_MVREF;
3554 rec->jm_ino = jmvref->jm_ino;
3555 rec->jm_parent = jmvref->jm_parent;
3556 rec->jm_oldoff = jmvref->jm_oldoff;
3557 rec->jm_newoff = jmvref->jm_newoff;
3561 jnewblk_write(jnewblk, jseg, data)
3562 struct jnewblk *jnewblk;
3566 struct jblkrec *rec;
3568 jnewblk->jn_jsegdep->jd_seg = jseg;
3569 rec = (struct jblkrec *)data;
3570 rec->jb_op = JOP_NEWBLK;
3571 rec->jb_ino = jnewblk->jn_ino;
3572 rec->jb_blkno = jnewblk->jn_blkno;
3573 rec->jb_lbn = jnewblk->jn_lbn;
3574 rec->jb_frags = jnewblk->jn_frags;
3575 rec->jb_oldfrags = jnewblk->jn_oldfrags;
3579 jfreeblk_write(jfreeblk, jseg, data)
3580 struct jfreeblk *jfreeblk;
3584 struct jblkrec *rec;
3586 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3587 rec = (struct jblkrec *)data;
3588 rec->jb_op = JOP_FREEBLK;
3589 rec->jb_ino = jfreeblk->jf_ino;
3590 rec->jb_blkno = jfreeblk->jf_blkno;
3591 rec->jb_lbn = jfreeblk->jf_lbn;
3592 rec->jb_frags = jfreeblk->jf_frags;
3593 rec->jb_oldfrags = 0;
3597 jfreefrag_write(jfreefrag, jseg, data)
3598 struct jfreefrag *jfreefrag;
3602 struct jblkrec *rec;
3604 jfreefrag->fr_jsegdep->jd_seg = jseg;
3605 rec = (struct jblkrec *)data;
3606 rec->jb_op = JOP_FREEBLK;
3607 rec->jb_ino = jfreefrag->fr_ino;
3608 rec->jb_blkno = jfreefrag->fr_blkno;
3609 rec->jb_lbn = jfreefrag->fr_lbn;
3610 rec->jb_frags = jfreefrag->fr_frags;
3611 rec->jb_oldfrags = 0;
3615 jtrunc_write(jtrunc, jseg, data)
3616 struct jtrunc *jtrunc;
3620 struct jtrncrec *rec;
3622 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3623 rec = (struct jtrncrec *)data;
3624 rec->jt_op = JOP_TRUNC;
3625 rec->jt_ino = jtrunc->jt_ino;
3626 rec->jt_size = jtrunc->jt_size;
3627 rec->jt_extsize = jtrunc->jt_extsize;
3631 jfsync_write(jfsync, jseg, data)
3632 struct jfsync *jfsync;
3636 struct jtrncrec *rec;
3638 rec = (struct jtrncrec *)data;
3639 rec->jt_op = JOP_SYNC;
3640 rec->jt_ino = jfsync->jfs_ino;
3641 rec->jt_size = jfsync->jfs_size;
3642 rec->jt_extsize = jfsync->jfs_extsize;
3646 softdep_flushjournal(mp)
3649 struct jblocks *jblocks;
3650 struct ufsmount *ump;
3652 if (MOUNTEDSUJ(mp) == 0)
3655 jblocks = ump->softdep_jblocks;
3657 while (ump->softdep_on_journal) {
3658 jblocks->jb_needseg = 1;
3659 softdep_process_journal(mp, NULL, MNT_WAIT);
3664 static void softdep_synchronize_completed(struct bio *);
3665 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3668 softdep_synchronize_completed(bp)
3671 struct jseg *oldest;
3673 struct ufsmount *ump;
3676 * caller1 marks the last segment written before we issued the
3677 * synchronize cache.
3679 jseg = bp->bio_caller1;
3684 ump = VFSTOUFS(jseg->js_list.wk_mp);
3688 * Mark all the journal entries waiting on the synchronize cache
3689 * as completed so they may continue on.
3691 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3692 jseg->js_state |= COMPLETE;
3694 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3697 * Restart deferred journal entry processing from the oldest
3701 complete_jsegs(oldest);
3708 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3709 * barriers. The journal must be written prior to any blocks that depend
3710 * on it and the journal can not be released until the blocks have be
3711 * written. This code handles both barriers simultaneously.
3714 softdep_synchronize(bp, ump, caller1)
3716 struct ufsmount *ump;
3720 bp->bio_cmd = BIO_FLUSH;
3721 bp->bio_flags |= BIO_ORDERED;
3722 bp->bio_data = NULL;
3723 bp->bio_offset = ump->um_cp->provider->mediasize;
3725 bp->bio_done = softdep_synchronize_completed;
3726 bp->bio_caller1 = caller1;
3727 g_io_request(bp, ump->um_cp);
3731 * Flush some journal records to disk.
3734 softdep_process_journal(mp, needwk, flags)
3736 struct worklist *needwk;
3739 struct jblocks *jblocks;
3740 struct ufsmount *ump;
3741 struct worklist *wk;
3749 int jrecmin; /* Minimum records per block. */
3750 int jrecmax; /* Maximum records per block. */
3757 if (ump->um_softdep == NULL || ump->um_softdep->sd_jblocks == NULL)
3759 shouldflush = softdep_flushcache;
3764 jblocks = ump->softdep_jblocks;
3765 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3767 * We write anywhere between a disk block and fs block. The upper
3768 * bound is picked to prevent buffer cache fragmentation and limit
3769 * processing time per I/O.
3771 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3772 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3775 cnt = ump->softdep_on_journal;
3777 * Criteria for writing a segment:
3778 * 1) We have a full block.
3779 * 2) We're called from jwait() and haven't found the
3781 * 3) Always write if needseg is set.
3782 * 4) If we are called from process_worklist and have
3783 * not yet written anything we write a partial block
3784 * to enforce a 1 second maximum latency on journal
3787 if (cnt < (jrecmax - 1) && needwk == NULL &&
3788 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3792 * Verify some free journal space. softdep_prealloc() should
3793 * guarantee that we don't run out so this is indicative of
3794 * a problem with the flow control. Try to recover
3795 * gracefully in any event.
3797 while (jblocks->jb_free == 0) {
3798 if (flags != MNT_WAIT)
3800 printf("softdep: Out of journal space!\n");
3801 softdep_speedup(ump);
3802 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3805 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3806 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3807 LIST_INIT(&jseg->js_entries);
3808 LIST_INIT(&jseg->js_indirs);
3809 jseg->js_state = ATTACHED;
3810 if (shouldflush == 0)
3811 jseg->js_state |= COMPLETE;
3812 else if (bio == NULL)
3813 bio = g_alloc_bio();
3814 jseg->js_jblocks = jblocks;
3815 bp = geteblk(fs->fs_bsize, 0);
3818 * If there was a race while we were allocating the block
3819 * and jseg the entry we care about was likely written.
3820 * We bail out in both the WAIT and NOWAIT case and assume
3821 * the caller will loop if the entry it cares about is
3824 cnt = ump->softdep_on_journal;
3825 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3826 bp->b_flags |= B_INVAL | B_NOCACHE;
3827 WORKITEM_FREE(jseg, D_JSEG);
3834 * Calculate the disk block size required for the available
3835 * records rounded to the min size.
3839 else if (cnt < jrecmax)
3840 size = howmany(cnt, jrecmin) * devbsize;
3842 size = fs->fs_bsize;
3844 * Allocate a disk block for this journal data and account
3845 * for truncation of the requested size if enough contiguous
3846 * space was not available.
3848 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3849 bp->b_lblkno = bp->b_blkno;
3850 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3851 bp->b_bcount = size;
3852 bp->b_flags &= ~B_INVAL;
3853 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3855 * Initialize our jseg with cnt records. Assign the next
3856 * sequence number to it and link it in-order.
3858 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3861 jseg->js_refs = cnt + 1; /* Self ref. */
3862 jseg->js_size = size;
3863 jseg->js_seq = jblocks->jb_nextseq++;
3864 if (jblocks->jb_oldestseg == NULL)
3865 jblocks->jb_oldestseg = jseg;
3866 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3867 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3868 if (jblocks->jb_writeseg == NULL)
3869 jblocks->jb_writeseg = jseg;
3871 * Start filling in records from the pending list.
3877 * Always put a header on the first block.
3878 * XXX As with below, there might not be a chance to get
3879 * into the loop. Ensure that something valid is written.
3881 jseg_write(ump, jseg, data);
3883 data = bp->b_data + off;
3886 * XXX Something is wrong here. There's no work to do,
3887 * but we need to perform and I/O and allow it to complete
3890 if (LIST_EMPTY(&ump->softdep_journal_pending))
3891 stat_emptyjblocks++;
3893 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3897 /* Place a segment header on every device block. */
3898 if ((off % devbsize) == 0) {
3899 jseg_write(ump, jseg, data);
3901 data = bp->b_data + off;
3905 remove_from_journal(wk);
3906 wk->wk_state |= INPROGRESS;
3907 WORKLIST_INSERT(&jseg->js_entries, wk);
3908 switch (wk->wk_type) {
3910 jaddref_write(WK_JADDREF(wk), jseg, data);
3913 jremref_write(WK_JREMREF(wk), jseg, data);
3916 jmvref_write(WK_JMVREF(wk), jseg, data);
3919 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3922 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3925 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3928 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3931 jfsync_write(WK_JFSYNC(wk), jseg, data);
3934 panic("process_journal: Unknown type %s",
3935 TYPENAME(wk->wk_type));
3939 data = bp->b_data + off;
3943 /* Clear any remaining space so we don't leak kernel data */
3945 bzero(data, size - off);
3948 * Write this one buffer and continue.
3951 jblocks->jb_needseg = 0;
3952 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3954 bp->b_xflags |= BX_CVTENXIO;
3955 pbgetvp(ump->um_devvp, bp);
3957 * We only do the blocking wait once we find the journal
3958 * entry we're looking for.
3960 if (needwk == NULL && flags == MNT_WAIT)
3967 * If we wrote a segment issue a synchronize cache so the journal
3968 * is reflected on disk before the data is written. Since reclaiming
3969 * journal space also requires writing a journal record this
3970 * process also enforces a barrier before reclamation.
3972 if (segwritten && shouldflush) {
3973 softdep_synchronize(bio, ump,
3974 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3978 * If we've suspended the filesystem because we ran out of journal
3979 * space either try to sync it here to make some progress or
3980 * unsuspend it if we already have.
3982 if (flags == 0 && jblocks->jb_suspended) {
3983 if (journal_unsuspend(ump))
3986 VFS_SYNC(mp, MNT_NOWAIT);
3987 ffs_sbupdate(ump, MNT_WAIT, 0);
3993 * Complete a jseg, allowing all dependencies awaiting journal writes
3994 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3995 * structures so that the journal segment can be freed to reclaim space.
4001 struct worklist *wk;
4002 struct jmvref *jmvref;
4007 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
4008 WORKLIST_REMOVE(wk);
4009 wk->wk_state &= ~INPROGRESS;
4010 wk->wk_state |= COMPLETE;
4011 KASSERT(i++ < jseg->js_cnt,
4012 ("handle_written_jseg: overflow %d >= %d",
4013 i - 1, jseg->js_cnt));
4014 switch (wk->wk_type) {
4016 handle_written_jaddref(WK_JADDREF(wk));
4019 handle_written_jremref(WK_JREMREF(wk));
4022 rele_jseg(jseg); /* No jsegdep. */
4023 jmvref = WK_JMVREF(wk);
4024 LIST_REMOVE(jmvref, jm_deps);
4025 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
4026 free_pagedep(jmvref->jm_pagedep);
4027 WORKITEM_FREE(jmvref, D_JMVREF);
4030 handle_written_jnewblk(WK_JNEWBLK(wk));
4033 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
4036 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
4039 rele_jseg(jseg); /* No jsegdep. */
4040 WORKITEM_FREE(wk, D_JFSYNC);
4043 handle_written_jfreefrag(WK_JFREEFRAG(wk));
4046 panic("handle_written_jseg: Unknown type %s",
4047 TYPENAME(wk->wk_type));
4051 /* Release the self reference so the structure may be freed. */
4056 * Determine which jsegs are ready for completion processing. Waits for
4057 * synchronize cache to complete as well as forcing in-order completion
4058 * of journal entries.
4061 complete_jsegs(jseg)
4064 struct jblocks *jblocks;
4067 jblocks = jseg->js_jblocks;
4069 * Don't allow out of order completions. If this isn't the first
4070 * block wait for it to write before we're done.
4072 if (jseg != jblocks->jb_writeseg)
4074 /* Iterate through available jsegs processing their entries. */
4075 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
4076 jblocks->jb_oldestwrseq = jseg->js_oldseq;
4077 jsegn = TAILQ_NEXT(jseg, js_next);
4078 complete_jseg(jseg);
4081 jblocks->jb_writeseg = jseg;
4083 * Attempt to free jsegs now that oldestwrseq may have advanced.
4085 free_jsegs(jblocks);
4089 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
4090 * the final completions.
4093 handle_written_jseg(jseg, bp)
4098 if (jseg->js_refs == 0)
4099 panic("handle_written_jseg: No self-reference on %p", jseg);
4100 jseg->js_state |= DEPCOMPLETE;
4102 * We'll never need this buffer again, set flags so it will be
4105 bp->b_flags |= B_INVAL | B_NOCACHE;
4107 complete_jsegs(jseg);
4110 static inline struct jsegdep *
4112 struct inoref *inoref;
4114 struct jsegdep *jsegdep;
4116 jsegdep = inoref->if_jsegdep;
4117 inoref->if_jsegdep = NULL;
4123 * Called once a jremref has made it to stable store. The jremref is marked
4124 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
4125 * for the jremref to complete will be awoken by free_jremref.
4128 handle_written_jremref(jremref)
4129 struct jremref *jremref;
4131 struct inodedep *inodedep;
4132 struct jsegdep *jsegdep;
4133 struct dirrem *dirrem;
4135 /* Grab the jsegdep. */
4136 jsegdep = inoref_jseg(&jremref->jr_ref);
4138 * Remove us from the inoref list.
4140 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
4142 panic("handle_written_jremref: Lost inodedep");
4143 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
4145 * Complete the dirrem.
4147 dirrem = jremref->jr_dirrem;
4148 jremref->jr_dirrem = NULL;
4149 LIST_REMOVE(jremref, jr_deps);
4150 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
4151 jwork_insert(&dirrem->dm_jwork, jsegdep);
4152 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
4153 (dirrem->dm_state & COMPLETE) != 0)
4154 add_to_worklist(&dirrem->dm_list, 0);
4155 free_jremref(jremref);
4159 * Called once a jaddref has made it to stable store. The dependency is
4160 * marked complete and any dependent structures are added to the inode
4161 * bufwait list to be completed as soon as it is written. If a bitmap write
4162 * depends on this entry we move the inode into the inodedephd of the
4163 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
4166 handle_written_jaddref(jaddref)
4167 struct jaddref *jaddref;
4169 struct jsegdep *jsegdep;
4170 struct inodedep *inodedep;
4171 struct diradd *diradd;
4172 struct mkdir *mkdir;
4174 /* Grab the jsegdep. */
4175 jsegdep = inoref_jseg(&jaddref->ja_ref);
4178 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4180 panic("handle_written_jaddref: Lost inodedep.");
4181 if (jaddref->ja_diradd == NULL)
4182 panic("handle_written_jaddref: No dependency");
4183 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
4184 diradd = jaddref->ja_diradd;
4185 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
4186 } else if (jaddref->ja_state & MKDIR_PARENT) {
4187 mkdir = jaddref->ja_mkdir;
4188 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
4189 } else if (jaddref->ja_state & MKDIR_BODY)
4190 mkdir = jaddref->ja_mkdir;
4192 panic("handle_written_jaddref: Unknown dependency %p",
4193 jaddref->ja_diradd);
4194 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
4196 * Remove us from the inode list.
4198 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
4200 * The mkdir may be waiting on the jaddref to clear before freeing.
4203 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
4204 ("handle_written_jaddref: Incorrect type for mkdir %s",
4205 TYPENAME(mkdir->md_list.wk_type)));
4206 mkdir->md_jaddref = NULL;
4207 diradd = mkdir->md_diradd;
4208 mkdir->md_state |= DEPCOMPLETE;
4209 complete_mkdir(mkdir);
4211 jwork_insert(&diradd->da_jwork, jsegdep);
4212 if (jaddref->ja_state & NEWBLOCK) {
4213 inodedep->id_state |= ONDEPLIST;
4214 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
4217 free_jaddref(jaddref);
4221 * Called once a jnewblk journal is written. The allocdirect or allocindir
4222 * is placed in the bmsafemap to await notification of a written bitmap. If
4223 * the operation was canceled we add the segdep to the appropriate
4224 * dependency to free the journal space once the canceling operation
4228 handle_written_jnewblk(jnewblk)
4229 struct jnewblk *jnewblk;
4231 struct bmsafemap *bmsafemap;
4232 struct freefrag *freefrag;
4233 struct freework *freework;
4234 struct jsegdep *jsegdep;
4235 struct newblk *newblk;
4237 /* Grab the jsegdep. */
4238 jsegdep = jnewblk->jn_jsegdep;
4239 jnewblk->jn_jsegdep = NULL;
4240 if (jnewblk->jn_dep == NULL)
4241 panic("handle_written_jnewblk: No dependency for the segdep.");
4242 switch (jnewblk->jn_dep->wk_type) {
4247 * Add the written block to the bmsafemap so it can
4248 * be notified when the bitmap is on disk.
4250 newblk = WK_NEWBLK(jnewblk->jn_dep);
4251 newblk->nb_jnewblk = NULL;
4252 if ((newblk->nb_state & GOINGAWAY) == 0) {
4253 bmsafemap = newblk->nb_bmsafemap;
4254 newblk->nb_state |= ONDEPLIST;
4255 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
4258 jwork_insert(&newblk->nb_jwork, jsegdep);
4262 * A newblock being removed by a freefrag when replaced by
4265 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
4266 freefrag->ff_jdep = NULL;
4267 jwork_insert(&freefrag->ff_jwork, jsegdep);
4271 * A direct block was removed by truncate.
4273 freework = WK_FREEWORK(jnewblk->jn_dep);
4274 freework->fw_jnewblk = NULL;
4275 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
4278 panic("handle_written_jnewblk: Unknown type %d.",
4279 jnewblk->jn_dep->wk_type);
4281 jnewblk->jn_dep = NULL;
4282 free_jnewblk(jnewblk);
4286 * Cancel a jfreefrag that won't be needed, probably due to colliding with
4287 * an in-flight allocation that has not yet been committed. Divorce us
4288 * from the freefrag and mark it DEPCOMPLETE so that it may be added
4292 cancel_jfreefrag(jfreefrag)
4293 struct jfreefrag *jfreefrag;
4295 struct freefrag *freefrag;
4297 if (jfreefrag->fr_jsegdep) {
4298 free_jsegdep(jfreefrag->fr_jsegdep);
4299 jfreefrag->fr_jsegdep = NULL;
4301 freefrag = jfreefrag->fr_freefrag;
4302 jfreefrag->fr_freefrag = NULL;
4303 free_jfreefrag(jfreefrag);
4304 freefrag->ff_state |= DEPCOMPLETE;
4305 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
4309 * Free a jfreefrag when the parent freefrag is rendered obsolete.
4312 free_jfreefrag(jfreefrag)
4313 struct jfreefrag *jfreefrag;
4316 if (jfreefrag->fr_state & INPROGRESS)
4317 WORKLIST_REMOVE(&jfreefrag->fr_list);
4318 else if (jfreefrag->fr_state & ONWORKLIST)
4319 remove_from_journal(&jfreefrag->fr_list);
4320 if (jfreefrag->fr_freefrag != NULL)
4321 panic("free_jfreefrag: Still attached to a freefrag.");
4322 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
4326 * Called when the journal write for a jfreefrag completes. The parent
4327 * freefrag is added to the worklist if this completes its dependencies.
4330 handle_written_jfreefrag(jfreefrag)
4331 struct jfreefrag *jfreefrag;
4333 struct jsegdep *jsegdep;
4334 struct freefrag *freefrag;
4336 /* Grab the jsegdep. */
4337 jsegdep = jfreefrag->fr_jsegdep;
4338 jfreefrag->fr_jsegdep = NULL;
4339 freefrag = jfreefrag->fr_freefrag;
4340 if (freefrag == NULL)
4341 panic("handle_written_jfreefrag: No freefrag.");
4342 freefrag->ff_state |= DEPCOMPLETE;
4343 freefrag->ff_jdep = NULL;
4344 jwork_insert(&freefrag->ff_jwork, jsegdep);
4345 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
4346 add_to_worklist(&freefrag->ff_list, 0);
4347 jfreefrag->fr_freefrag = NULL;
4348 free_jfreefrag(jfreefrag);
4352 * Called when the journal write for a jfreeblk completes. The jfreeblk
4353 * is removed from the freeblks list of pending journal writes and the
4354 * jsegdep is moved to the freeblks jwork to be completed when all blocks
4355 * have been reclaimed.
4358 handle_written_jblkdep(jblkdep)
4359 struct jblkdep *jblkdep;
4361 struct freeblks *freeblks;
4362 struct jsegdep *jsegdep;
4364 /* Grab the jsegdep. */
4365 jsegdep = jblkdep->jb_jsegdep;
4366 jblkdep->jb_jsegdep = NULL;
4367 freeblks = jblkdep->jb_freeblks;
4368 LIST_REMOVE(jblkdep, jb_deps);
4369 jwork_insert(&freeblks->fb_jwork, jsegdep);
4371 * If the freeblks is all journaled, we can add it to the worklist.
4373 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4374 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4375 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4377 free_jblkdep(jblkdep);
4380 static struct jsegdep *
4381 newjsegdep(struct worklist *wk)
4383 struct jsegdep *jsegdep;
4385 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4386 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4387 jsegdep->jd_seg = NULL;
4392 static struct jmvref *
4393 newjmvref(dp, ino, oldoff, newoff)
4399 struct jmvref *jmvref;
4401 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4402 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4403 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4404 jmvref->jm_parent = dp->i_number;
4405 jmvref->jm_ino = ino;
4406 jmvref->jm_oldoff = oldoff;
4407 jmvref->jm_newoff = newoff;
4413 * Allocate a new jremref that tracks the removal of ip from dp with the
4414 * directory entry offset of diroff. Mark the entry as ATTACHED and
4415 * DEPCOMPLETE as we have all the information required for the journal write
4416 * and the directory has already been removed from the buffer. The caller
4417 * is responsible for linking the jremref into the pagedep and adding it
4418 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4419 * a DOTDOT addition so handle_workitem_remove() can properly assign
4420 * the jsegdep when we're done.
4422 static struct jremref *
4423 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4424 off_t diroff, nlink_t nlink)
4426 struct jremref *jremref;
4428 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4429 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4430 jremref->jr_state = ATTACHED;
4431 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4433 jremref->jr_dirrem = dirrem;
4439 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4440 nlink_t nlink, uint16_t mode)
4443 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4444 inoref->if_diroff = diroff;
4445 inoref->if_ino = ino;
4446 inoref->if_parent = parent;
4447 inoref->if_nlink = nlink;
4448 inoref->if_mode = mode;
4452 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4453 * directory offset may not be known until later. The caller is responsible
4454 * adding the entry to the journal when this information is available. nlink
4455 * should be the link count prior to the addition and mode is only required
4456 * to have the correct FMT.
4458 static struct jaddref *
4459 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4462 struct jaddref *jaddref;
4464 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4465 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4466 jaddref->ja_state = ATTACHED;
4467 jaddref->ja_mkdir = NULL;
4468 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4474 * Create a new free dependency for a freework. The caller is responsible
4475 * for adjusting the reference count when it has the lock held. The freedep
4476 * will track an outstanding bitmap write that will ultimately clear the
4477 * freework to continue.
4479 static struct freedep *
4480 newfreedep(struct freework *freework)
4482 struct freedep *freedep;
4484 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4485 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4486 freedep->fd_freework = freework;
4492 * Free a freedep structure once the buffer it is linked to is written. If
4493 * this is the last reference to the freework schedule it for completion.
4496 free_freedep(freedep)
4497 struct freedep *freedep;
4499 struct freework *freework;
4501 freework = freedep->fd_freework;
4502 freework->fw_freeblks->fb_cgwait--;
4503 if (--freework->fw_ref == 0)
4504 freework_enqueue(freework);
4505 WORKITEM_FREE(freedep, D_FREEDEP);
4509 * Allocate a new freework structure that may be a level in an indirect
4510 * when parent is not NULL or a top level block when it is. The top level
4511 * freework structures are allocated without the per-filesystem lock held
4512 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4514 static struct freework *
4515 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4516 struct ufsmount *ump;
4517 struct freeblks *freeblks;
4518 struct freework *parent;
4525 struct freework *freework;
4527 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4528 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4529 freework->fw_state = ATTACHED;
4530 freework->fw_jnewblk = NULL;
4531 freework->fw_freeblks = freeblks;
4532 freework->fw_parent = parent;
4533 freework->fw_lbn = lbn;
4534 freework->fw_blkno = nb;
4535 freework->fw_frags = frags;
4536 freework->fw_indir = NULL;
4537 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4538 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4539 freework->fw_start = freework->fw_off = off;
4541 newjfreeblk(freeblks, lbn, nb, frags);
4542 if (parent == NULL) {
4544 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4553 * Eliminate a jfreeblk for a block that does not need journaling.
4556 cancel_jfreeblk(freeblks, blkno)
4557 struct freeblks *freeblks;
4560 struct jfreeblk *jfreeblk;
4561 struct jblkdep *jblkdep;
4563 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4564 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4566 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4567 if (jfreeblk->jf_blkno == blkno)
4570 if (jblkdep == NULL)
4572 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4573 free_jsegdep(jblkdep->jb_jsegdep);
4574 LIST_REMOVE(jblkdep, jb_deps);
4575 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4579 * Allocate a new jfreeblk to journal top level block pointer when truncating
4580 * a file. The caller must add this to the worklist when the per-filesystem
4583 static struct jfreeblk *
4584 newjfreeblk(freeblks, lbn, blkno, frags)
4585 struct freeblks *freeblks;
4590 struct jfreeblk *jfreeblk;
4592 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4593 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4594 freeblks->fb_list.wk_mp);
4595 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4596 jfreeblk->jf_dep.jb_freeblks = freeblks;
4597 jfreeblk->jf_ino = freeblks->fb_inum;
4598 jfreeblk->jf_lbn = lbn;
4599 jfreeblk->jf_blkno = blkno;
4600 jfreeblk->jf_frags = frags;
4601 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4607 * The journal is only prepared to handle full-size block numbers, so we
4608 * have to adjust the record to reflect the change to a full-size block.
4609 * For example, suppose we have a block made up of fragments 8-15 and
4610 * want to free its last two fragments. We are given a request that says:
4611 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4612 * where frags are the number of fragments to free and oldfrags are the
4613 * number of fragments to keep. To block align it, we have to change it to
4614 * have a valid full-size blkno, so it becomes:
4615 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4618 adjust_newfreework(freeblks, frag_offset)
4619 struct freeblks *freeblks;
4622 struct jfreeblk *jfreeblk;
4624 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4625 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4626 ("adjust_newfreework: Missing freeblks dependency"));
4628 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4629 jfreeblk->jf_blkno -= frag_offset;
4630 jfreeblk->jf_frags += frag_offset;
4634 * Allocate a new jtrunc to track a partial truncation.
4636 static struct jtrunc *
4637 newjtrunc(freeblks, size, extsize)
4638 struct freeblks *freeblks;
4642 struct jtrunc *jtrunc;
4644 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4645 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4646 freeblks->fb_list.wk_mp);
4647 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4648 jtrunc->jt_dep.jb_freeblks = freeblks;
4649 jtrunc->jt_ino = freeblks->fb_inum;
4650 jtrunc->jt_size = size;
4651 jtrunc->jt_extsize = extsize;
4652 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4658 * If we're canceling a new bitmap we have to search for another ref
4659 * to move into the bmsafemap dep. This might be better expressed
4660 * with another structure.
4663 move_newblock_dep(jaddref, inodedep)
4664 struct jaddref *jaddref;
4665 struct inodedep *inodedep;
4667 struct inoref *inoref;
4668 struct jaddref *jaddrefn;
4671 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4672 inoref = TAILQ_NEXT(inoref, if_deps)) {
4673 if ((jaddref->ja_state & NEWBLOCK) &&
4674 inoref->if_list.wk_type == D_JADDREF) {
4675 jaddrefn = (struct jaddref *)inoref;
4679 if (jaddrefn == NULL)
4681 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4682 jaddrefn->ja_state |= jaddref->ja_state &
4683 (ATTACHED | UNDONE | NEWBLOCK);
4684 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4685 jaddref->ja_state |= ATTACHED;
4686 LIST_REMOVE(jaddref, ja_bmdeps);
4687 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4692 * Cancel a jaddref either before it has been written or while it is being
4693 * written. This happens when a link is removed before the add reaches
4694 * the disk. The jaddref dependency is kept linked into the bmsafemap
4695 * and inode to prevent the link count or bitmap from reaching the disk
4696 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4699 * Returns 1 if the canceled addref requires journaling of the remove and
4703 cancel_jaddref(jaddref, inodedep, wkhd)
4704 struct jaddref *jaddref;
4705 struct inodedep *inodedep;
4706 struct workhead *wkhd;
4708 struct inoref *inoref;
4709 struct jsegdep *jsegdep;
4712 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4713 ("cancel_jaddref: Canceling complete jaddref"));
4714 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4718 if (inodedep == NULL)
4719 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4721 panic("cancel_jaddref: Lost inodedep");
4723 * We must adjust the nlink of any reference operation that follows
4724 * us so that it is consistent with the in-memory reference. This
4725 * ensures that inode nlink rollbacks always have the correct link.
4728 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4729 inoref = TAILQ_NEXT(inoref, if_deps)) {
4730 if (inoref->if_state & GOINGAWAY)
4735 jsegdep = inoref_jseg(&jaddref->ja_ref);
4736 if (jaddref->ja_state & NEWBLOCK)
4737 move_newblock_dep(jaddref, inodedep);
4738 wake_worklist(&jaddref->ja_list);
4739 jaddref->ja_mkdir = NULL;
4740 if (jaddref->ja_state & INPROGRESS) {
4741 jaddref->ja_state &= ~INPROGRESS;
4742 WORKLIST_REMOVE(&jaddref->ja_list);
4743 jwork_insert(wkhd, jsegdep);
4745 free_jsegdep(jsegdep);
4746 if (jaddref->ja_state & DEPCOMPLETE)
4747 remove_from_journal(&jaddref->ja_list);
4749 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4751 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4752 * can arrange for them to be freed with the bitmap. Otherwise we
4753 * no longer need this addref attached to the inoreflst and it
4754 * will incorrectly adjust nlink if we leave it.
4756 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4757 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4759 jaddref->ja_state |= COMPLETE;
4760 free_jaddref(jaddref);
4764 * Leave the head of the list for jsegdeps for fast merging.
4766 if (LIST_FIRST(wkhd) != NULL) {
4767 jaddref->ja_state |= ONWORKLIST;
4768 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4770 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4776 * Attempt to free a jaddref structure when some work completes. This
4777 * should only succeed once the entry is written and all dependencies have
4781 free_jaddref(jaddref)
4782 struct jaddref *jaddref;
4785 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4787 if (jaddref->ja_ref.if_jsegdep)
4788 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4789 jaddref, jaddref->ja_state);
4790 if (jaddref->ja_state & NEWBLOCK)
4791 LIST_REMOVE(jaddref, ja_bmdeps);
4792 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4793 panic("free_jaddref: Bad state %p(0x%X)",
4794 jaddref, jaddref->ja_state);
4795 if (jaddref->ja_mkdir != NULL)
4796 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4797 WORKITEM_FREE(jaddref, D_JADDREF);
4801 * Free a jremref structure once it has been written or discarded.
4804 free_jremref(jremref)
4805 struct jremref *jremref;
4808 if (jremref->jr_ref.if_jsegdep)
4809 free_jsegdep(jremref->jr_ref.if_jsegdep);
4810 if (jremref->jr_state & INPROGRESS)
4811 panic("free_jremref: IO still pending");
4812 WORKITEM_FREE(jremref, D_JREMREF);
4816 * Free a jnewblk structure.
4819 free_jnewblk(jnewblk)
4820 struct jnewblk *jnewblk;
4823 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4825 LIST_REMOVE(jnewblk, jn_deps);
4826 if (jnewblk->jn_dep != NULL)
4827 panic("free_jnewblk: Dependency still attached.");
4828 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4832 * Cancel a jnewblk which has been been made redundant by frag extension.
4835 cancel_jnewblk(jnewblk, wkhd)
4836 struct jnewblk *jnewblk;
4837 struct workhead *wkhd;
4839 struct jsegdep *jsegdep;
4841 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4842 jsegdep = jnewblk->jn_jsegdep;
4843 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4844 panic("cancel_jnewblk: Invalid state");
4845 jnewblk->jn_jsegdep = NULL;
4846 jnewblk->jn_dep = NULL;
4847 jnewblk->jn_state |= GOINGAWAY;
4848 if (jnewblk->jn_state & INPROGRESS) {
4849 jnewblk->jn_state &= ~INPROGRESS;
4850 WORKLIST_REMOVE(&jnewblk->jn_list);
4851 jwork_insert(wkhd, jsegdep);
4853 free_jsegdep(jsegdep);
4854 remove_from_journal(&jnewblk->jn_list);
4856 wake_worklist(&jnewblk->jn_list);
4857 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4861 free_jblkdep(jblkdep)
4862 struct jblkdep *jblkdep;
4865 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4866 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4867 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4868 WORKITEM_FREE(jblkdep, D_JTRUNC);
4870 panic("free_jblkdep: Unexpected type %s",
4871 TYPENAME(jblkdep->jb_list.wk_type));
4875 * Free a single jseg once it is no longer referenced in memory or on
4876 * disk. Reclaim journal blocks and dependencies waiting for the segment
4880 free_jseg(jseg, jblocks)
4882 struct jblocks *jblocks;
4884 struct freework *freework;
4887 * Free freework structures that were lingering to indicate freed
4888 * indirect blocks that forced journal write ordering on reallocate.
4890 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4891 indirblk_remove(freework);
4892 if (jblocks->jb_oldestseg == jseg)
4893 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4894 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4895 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4896 KASSERT(LIST_EMPTY(&jseg->js_entries),
4897 ("free_jseg: Freed jseg has valid entries."));
4898 WORKITEM_FREE(jseg, D_JSEG);
4902 * Free all jsegs that meet the criteria for being reclaimed and update
4907 struct jblocks *jblocks;
4912 * Free only those jsegs which have none allocated before them to
4913 * preserve the journal space ordering.
4915 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4917 * Only reclaim space when nothing depends on this journal
4918 * set and another set has written that it is no longer
4921 if (jseg->js_refs != 0) {
4922 jblocks->jb_oldestseg = jseg;
4925 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4927 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4930 * We can free jsegs that didn't write entries when
4931 * oldestwrseq == js_seq.
4933 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4936 free_jseg(jseg, jblocks);
4939 * If we exited the loop above we still must discover the
4940 * oldest valid segment.
4943 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4944 jseg = TAILQ_NEXT(jseg, js_next))
4945 if (jseg->js_refs != 0)
4947 jblocks->jb_oldestseg = jseg;
4949 * The journal has no valid records but some jsegs may still be
4950 * waiting on oldestwrseq to advance. We force a small record
4951 * out to permit these lingering records to be reclaimed.
4953 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4954 jblocks->jb_needseg = 1;
4958 * Release one reference to a jseg and free it if the count reaches 0. This
4959 * should eventually reclaim journal space as well.
4966 KASSERT(jseg->js_refs > 0,
4967 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4968 if (--jseg->js_refs != 0)
4970 free_jsegs(jseg->js_jblocks);
4974 * Release a jsegdep and decrement the jseg count.
4977 free_jsegdep(jsegdep)
4978 struct jsegdep *jsegdep;
4981 if (jsegdep->jd_seg)
4982 rele_jseg(jsegdep->jd_seg);
4983 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4987 * Wait for a journal item to make it to disk. Initiate journal processing
4992 struct worklist *wk;
4996 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4998 * Blocking journal waits cause slow synchronous behavior. Record
4999 * stats on the frequency of these blocking operations.
5001 if (waitfor == MNT_WAIT) {
5002 stat_journal_wait++;
5003 switch (wk->wk_type) {
5006 stat_jwait_filepage++;
5010 stat_jwait_freeblks++;
5013 stat_jwait_newblk++;
5023 * If IO has not started we process the journal. We can't mark the
5024 * worklist item as IOWAITING because we drop the lock while
5025 * processing the journal and the worklist entry may be freed after
5026 * this point. The caller may call back in and re-issue the request.
5028 if ((wk->wk_state & INPROGRESS) == 0) {
5029 softdep_process_journal(wk->wk_mp, wk, waitfor);
5030 if (waitfor != MNT_WAIT)
5034 if (waitfor != MNT_WAIT)
5036 wait_worklist(wk, "jwait");
5041 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
5042 * appropriate. This is a convenience function to reduce duplicate code
5043 * for the setup and revert functions below.
5045 static struct inodedep *
5046 inodedep_lookup_ip(ip)
5049 struct inodedep *inodedep;
5051 KASSERT(ip->i_nlink >= ip->i_effnlink,
5052 ("inodedep_lookup_ip: bad delta"));
5053 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
5055 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
5056 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
5062 * Called prior to creating a new inode and linking it to a directory. The
5063 * jaddref structure must already be allocated by softdep_setup_inomapdep
5064 * and it is discovered here so we can initialize the mode and update
5068 softdep_setup_create(dp, ip)
5072 struct inodedep *inodedep;
5073 struct jaddref *jaddref;
5076 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5077 ("softdep_setup_create called on non-softdep filesystem"));
5078 KASSERT(ip->i_nlink == 1,
5079 ("softdep_setup_create: Invalid link count."));
5081 ACQUIRE_LOCK(ITOUMP(dp));
5082 inodedep = inodedep_lookup_ip(ip);
5083 if (DOINGSUJ(dvp)) {
5084 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5086 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
5087 ("softdep_setup_create: No addref structure present."));
5089 FREE_LOCK(ITOUMP(dp));
5093 * Create a jaddref structure to track the addition of a DOTDOT link when
5094 * we are reparenting an inode as part of a rename. This jaddref will be
5095 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
5096 * non-journaling softdep.
5099 softdep_setup_dotdot_link(dp, ip)
5103 struct inodedep *inodedep;
5104 struct jaddref *jaddref;
5107 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5108 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
5112 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
5113 * is used as a normal link would be.
5116 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5117 dp->i_effnlink - 1, dp->i_mode);
5118 ACQUIRE_LOCK(ITOUMP(dp));
5119 inodedep = inodedep_lookup_ip(dp);
5121 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5123 FREE_LOCK(ITOUMP(dp));
5127 * Create a jaddref structure to track a new link to an inode. The directory
5128 * offset is not known until softdep_setup_directory_add or
5129 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
5133 softdep_setup_link(dp, ip)
5137 struct inodedep *inodedep;
5138 struct jaddref *jaddref;
5141 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5142 ("softdep_setup_link called on non-softdep filesystem"));
5146 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
5148 ACQUIRE_LOCK(ITOUMP(dp));
5149 inodedep = inodedep_lookup_ip(ip);
5151 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5153 FREE_LOCK(ITOUMP(dp));
5157 * Called to create the jaddref structures to track . and .. references as
5158 * well as lookup and further initialize the incomplete jaddref created
5159 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
5160 * nlinkdelta for non-journaling softdep.
5163 softdep_setup_mkdir(dp, ip)
5167 struct inodedep *inodedep;
5168 struct jaddref *dotdotaddref;
5169 struct jaddref *dotaddref;
5170 struct jaddref *jaddref;
5173 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5174 ("softdep_setup_mkdir called on non-softdep filesystem"));
5176 dotaddref = dotdotaddref = NULL;
5177 if (DOINGSUJ(dvp)) {
5178 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
5180 dotaddref->ja_state |= MKDIR_BODY;
5181 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
5182 dp->i_effnlink - 1, dp->i_mode);
5183 dotdotaddref->ja_state |= MKDIR_PARENT;
5185 ACQUIRE_LOCK(ITOUMP(dp));
5186 inodedep = inodedep_lookup_ip(ip);
5187 if (DOINGSUJ(dvp)) {
5188 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5190 KASSERT(jaddref != NULL,
5191 ("softdep_setup_mkdir: No addref structure present."));
5192 KASSERT(jaddref->ja_parent == dp->i_number,
5193 ("softdep_setup_mkdir: bad parent %ju",
5194 (uintmax_t)jaddref->ja_parent));
5195 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
5198 inodedep = inodedep_lookup_ip(dp);
5200 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
5201 &dotdotaddref->ja_ref, if_deps);
5202 FREE_LOCK(ITOUMP(dp));
5206 * Called to track nlinkdelta of the inode and parent directories prior to
5207 * unlinking a directory.
5210 softdep_setup_rmdir(dp, ip)
5216 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5217 ("softdep_setup_rmdir called on non-softdep filesystem"));
5219 ACQUIRE_LOCK(ITOUMP(dp));
5220 (void) inodedep_lookup_ip(ip);
5221 (void) inodedep_lookup_ip(dp);
5222 FREE_LOCK(ITOUMP(dp));
5226 * Called to track nlinkdelta of the inode and parent directories prior to
5230 softdep_setup_unlink(dp, ip)
5236 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5237 ("softdep_setup_unlink called on non-softdep filesystem"));
5239 ACQUIRE_LOCK(ITOUMP(dp));
5240 (void) inodedep_lookup_ip(ip);
5241 (void) inodedep_lookup_ip(dp);
5242 FREE_LOCK(ITOUMP(dp));
5246 * Called to release the journal structures created by a failed non-directory
5247 * creation. Adjusts nlinkdelta for non-journaling softdep.
5250 softdep_revert_create(dp, ip)
5254 struct inodedep *inodedep;
5255 struct jaddref *jaddref;
5258 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
5259 ("softdep_revert_create called on non-softdep filesystem"));
5261 ACQUIRE_LOCK(ITOUMP(dp));
5262 inodedep = inodedep_lookup_ip(ip);
5263 if (DOINGSUJ(dvp)) {
5264 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5266 KASSERT(jaddref->ja_parent == dp->i_number,
5267 ("softdep_revert_create: addref parent mismatch"));
5268 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5270 FREE_LOCK(ITOUMP(dp));
5274 * Called to release the journal structures created by a failed link
5275 * addition. Adjusts nlinkdelta for non-journaling softdep.
5278 softdep_revert_link(dp, ip)
5282 struct inodedep *inodedep;
5283 struct jaddref *jaddref;
5286 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5287 ("softdep_revert_link called on non-softdep filesystem"));
5289 ACQUIRE_LOCK(ITOUMP(dp));
5290 inodedep = inodedep_lookup_ip(ip);
5291 if (DOINGSUJ(dvp)) {
5292 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5294 KASSERT(jaddref->ja_parent == dp->i_number,
5295 ("softdep_revert_link: addref parent mismatch"));
5296 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5298 FREE_LOCK(ITOUMP(dp));
5302 * Called to release the journal structures created by a failed mkdir
5303 * attempt. Adjusts nlinkdelta for non-journaling softdep.
5306 softdep_revert_mkdir(dp, ip)
5310 struct inodedep *inodedep;
5311 struct jaddref *jaddref;
5312 struct jaddref *dotaddref;
5315 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5316 ("softdep_revert_mkdir called on non-softdep filesystem"));
5319 ACQUIRE_LOCK(ITOUMP(dp));
5320 inodedep = inodedep_lookup_ip(dp);
5321 if (DOINGSUJ(dvp)) {
5322 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5324 KASSERT(jaddref->ja_parent == ip->i_number,
5325 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
5326 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5328 inodedep = inodedep_lookup_ip(ip);
5329 if (DOINGSUJ(dvp)) {
5330 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
5332 KASSERT(jaddref->ja_parent == dp->i_number,
5333 ("softdep_revert_mkdir: addref parent mismatch"));
5334 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
5335 inoreflst, if_deps);
5336 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
5337 KASSERT(dotaddref->ja_parent == ip->i_number,
5338 ("softdep_revert_mkdir: dot addref parent mismatch"));
5339 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
5341 FREE_LOCK(ITOUMP(dp));
5345 * Called to correct nlinkdelta after a failed rmdir.
5348 softdep_revert_rmdir(dp, ip)
5353 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5354 ("softdep_revert_rmdir called on non-softdep filesystem"));
5355 ACQUIRE_LOCK(ITOUMP(dp));
5356 (void) inodedep_lookup_ip(ip);
5357 (void) inodedep_lookup_ip(dp);
5358 FREE_LOCK(ITOUMP(dp));
5362 * Protecting the freemaps (or bitmaps).
5364 * To eliminate the need to execute fsck before mounting a filesystem
5365 * after a power failure, one must (conservatively) guarantee that the
5366 * on-disk copy of the bitmaps never indicate that a live inode or block is
5367 * free. So, when a block or inode is allocated, the bitmap should be
5368 * updated (on disk) before any new pointers. When a block or inode is
5369 * freed, the bitmap should not be updated until all pointers have been
5370 * reset. The latter dependency is handled by the delayed de-allocation
5371 * approach described below for block and inode de-allocation. The former
5372 * dependency is handled by calling the following procedure when a block or
5373 * inode is allocated. When an inode is allocated an "inodedep" is created
5374 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5375 * Each "inodedep" is also inserted into the hash indexing structure so
5376 * that any additional link additions can be made dependent on the inode
5379 * The ufs filesystem maintains a number of free block counts (e.g., per
5380 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5381 * in addition to the bitmaps. These counts are used to improve efficiency
5382 * during allocation and therefore must be consistent with the bitmaps.
5383 * There is no convenient way to guarantee post-crash consistency of these
5384 * counts with simple update ordering, for two main reasons: (1) The counts
5385 * and bitmaps for a single cylinder group block are not in the same disk
5386 * sector. If a disk write is interrupted (e.g., by power failure), one may
5387 * be written and the other not. (2) Some of the counts are located in the
5388 * superblock rather than the cylinder group block. So, we focus our soft
5389 * updates implementation on protecting the bitmaps. When mounting a
5390 * filesystem, we recompute the auxiliary counts from the bitmaps.
5394 * Called just after updating the cylinder group block to allocate an inode.
5397 softdep_setup_inomapdep(bp, ip, newinum, mode)
5398 struct buf *bp; /* buffer for cylgroup block with inode map */
5399 struct inode *ip; /* inode related to allocation */
5400 ino_t newinum; /* new inode number being allocated */
5403 struct inodedep *inodedep;
5404 struct bmsafemap *bmsafemap;
5405 struct jaddref *jaddref;
5410 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5411 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5412 fs = VFSTOUFS(mp)->um_fs;
5416 * Allocate the journal reference add structure so that the bitmap
5417 * can be dependent on it.
5419 if (MOUNTEDSUJ(mp)) {
5420 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5421 jaddref->ja_state |= NEWBLOCK;
5425 * Create a dependency for the newly allocated inode.
5426 * Panic if it already exists as something is seriously wrong.
5427 * Otherwise add it to the dependency list for the buffer holding
5428 * the cylinder group map from which it was allocated.
5430 * We have to preallocate a bmsafemap entry in case it is needed
5431 * in bmsafemap_lookup since once we allocate the inodedep, we
5432 * have to finish initializing it before we can FREE_LOCK().
5433 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5434 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5435 * creating the inodedep as it can be freed during the time
5436 * that we FREE_LOCK() while allocating the inodedep. We must
5437 * call workitem_alloc() before entering the locked section as
5438 * it also acquires the lock and we must avoid trying doing so
5441 bmsafemap = malloc(sizeof(struct bmsafemap),
5442 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5443 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5444 ACQUIRE_LOCK(ITOUMP(ip));
5445 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5446 panic("softdep_setup_inomapdep: dependency %p for new"
5447 "inode already exists", inodedep);
5448 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5450 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5451 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5454 inodedep->id_state |= ONDEPLIST;
5455 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5457 inodedep->id_bmsafemap = bmsafemap;
5458 inodedep->id_state &= ~DEPCOMPLETE;
5459 FREE_LOCK(ITOUMP(ip));
5463 * Called just after updating the cylinder group block to
5464 * allocate block or fragment.
5467 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5468 struct buf *bp; /* buffer for cylgroup block with block map */
5469 struct mount *mp; /* filesystem doing allocation */
5470 ufs2_daddr_t newblkno; /* number of newly allocated block */
5471 int frags; /* Number of fragments. */
5472 int oldfrags; /* Previous number of fragments for extend. */
5474 struct newblk *newblk;
5475 struct bmsafemap *bmsafemap;
5476 struct jnewblk *jnewblk;
5477 struct ufsmount *ump;
5480 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5481 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5486 * Create a dependency for the newly allocated block.
5487 * Add it to the dependency list for the buffer holding
5488 * the cylinder group map from which it was allocated.
5490 if (MOUNTEDSUJ(mp)) {
5491 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5492 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5493 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5494 jnewblk->jn_state = ATTACHED;
5495 jnewblk->jn_blkno = newblkno;
5496 jnewblk->jn_frags = frags;
5497 jnewblk->jn_oldfrags = oldfrags;
5505 cgp = (struct cg *)bp->b_data;
5506 blksfree = cg_blksfree(cgp);
5507 bno = dtogd(fs, jnewblk->jn_blkno);
5508 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5510 if (isset(blksfree, bno + i))
5511 panic("softdep_setup_blkmapdep: "
5512 "free fragment %d from %d-%d "
5513 "state 0x%X dep %p", i,
5514 jnewblk->jn_oldfrags,
5524 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5525 newblkno, frags, oldfrags);
5527 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5528 panic("softdep_setup_blkmapdep: found block");
5529 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5530 dtog(fs, newblkno), NULL);
5532 jnewblk->jn_dep = (struct worklist *)newblk;
5533 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5535 newblk->nb_state |= ONDEPLIST;
5536 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5538 newblk->nb_bmsafemap = bmsafemap;
5539 newblk->nb_jnewblk = jnewblk;
5543 #define BMSAFEMAP_HASH(ump, cg) \
5544 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5547 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5548 struct bmsafemap_hashhead *bmsafemaphd;
5550 struct bmsafemap **bmsafemapp;
5552 struct bmsafemap *bmsafemap;
5554 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5555 if (bmsafemap->sm_cg == cg)
5558 *bmsafemapp = bmsafemap;
5567 * Find the bmsafemap associated with a cylinder group buffer.
5568 * If none exists, create one. The buffer must be locked when
5569 * this routine is called and this routine must be called with
5570 * the softdep lock held. To avoid giving up the lock while
5571 * allocating a new bmsafemap, a preallocated bmsafemap may be
5572 * provided. If it is provided but not needed, it is freed.
5574 static struct bmsafemap *
5575 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5579 struct bmsafemap *newbmsafemap;
5581 struct bmsafemap_hashhead *bmsafemaphd;
5582 struct bmsafemap *bmsafemap, *collision;
5583 struct worklist *wk;
5584 struct ufsmount *ump;
5588 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5589 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5590 if (wk->wk_type == D_BMSAFEMAP) {
5592 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5593 return (WK_BMSAFEMAP(wk));
5596 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5597 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5599 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5603 bmsafemap = newbmsafemap;
5606 bmsafemap = malloc(sizeof(struct bmsafemap),
5607 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5608 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5611 bmsafemap->sm_buf = bp;
5612 LIST_INIT(&bmsafemap->sm_inodedephd);
5613 LIST_INIT(&bmsafemap->sm_inodedepwr);
5614 LIST_INIT(&bmsafemap->sm_newblkhd);
5615 LIST_INIT(&bmsafemap->sm_newblkwr);
5616 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5617 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5618 LIST_INIT(&bmsafemap->sm_freehd);
5619 LIST_INIT(&bmsafemap->sm_freewr);
5620 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5621 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5624 bmsafemap->sm_cg = cg;
5625 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5626 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5627 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5632 * Direct block allocation dependencies.
5634 * When a new block is allocated, the corresponding disk locations must be
5635 * initialized (with zeros or new data) before the on-disk inode points to
5636 * them. Also, the freemap from which the block was allocated must be
5637 * updated (on disk) before the inode's pointer. These two dependencies are
5638 * independent of each other and are needed for all file blocks and indirect
5639 * blocks that are pointed to directly by the inode. Just before the
5640 * "in-core" version of the inode is updated with a newly allocated block
5641 * number, a procedure (below) is called to setup allocation dependency
5642 * structures. These structures are removed when the corresponding
5643 * dependencies are satisfied or when the block allocation becomes obsolete
5644 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5645 * fragment that gets upgraded). All of these cases are handled in
5646 * procedures described later.
5648 * When a file extension causes a fragment to be upgraded, either to a larger
5649 * fragment or to a full block, the on-disk location may change (if the
5650 * previous fragment could not simply be extended). In this case, the old
5651 * fragment must be de-allocated, but not until after the inode's pointer has
5652 * been updated. In most cases, this is handled by later procedures, which
5653 * will construct a "freefrag" structure to be added to the workitem queue
5654 * when the inode update is complete (or obsolete). The main exception to
5655 * this is when an allocation occurs while a pending allocation dependency
5656 * (for the same block pointer) remains. This case is handled in the main
5657 * allocation dependency setup procedure by immediately freeing the
5658 * unreferenced fragments.
5661 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5662 struct inode *ip; /* inode to which block is being added */
5663 ufs_lbn_t off; /* block pointer within inode */
5664 ufs2_daddr_t newblkno; /* disk block number being added */
5665 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
5666 long newsize; /* size of new block */
5667 long oldsize; /* size of new block */
5668 struct buf *bp; /* bp for allocated block */
5670 struct allocdirect *adp, *oldadp;
5671 struct allocdirectlst *adphead;
5672 struct freefrag *freefrag;
5673 struct inodedep *inodedep;
5674 struct pagedep *pagedep;
5675 struct jnewblk *jnewblk;
5676 struct newblk *newblk;
5682 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5683 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5684 if (oldblkno && oldblkno != newblkno)
5686 * The usual case is that a smaller fragment that
5687 * was just allocated has been replaced with a bigger
5688 * fragment or a full-size block. If it is marked as
5689 * B_DELWRI, the current contents have not been written
5690 * to disk. It is possible that the block was written
5691 * earlier, but very uncommon. If the block has never
5692 * been written, there is no need to send a BIO_DELETE
5693 * for it when it is freed. The gain from avoiding the
5694 * TRIMs for the common case of unwritten blocks far
5695 * exceeds the cost of the write amplification for the
5696 * uncommon case of failing to send a TRIM for a block
5697 * that had been written.
5699 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5700 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5705 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5706 "off %jd newsize %ld oldsize %d",
5707 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5708 ACQUIRE_LOCK(ITOUMP(ip));
5709 if (off >= UFS_NDADDR) {
5711 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5713 /* allocating an indirect block */
5715 panic("softdep_setup_allocdirect: non-zero indir");
5718 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5721 * Allocating a direct block.
5723 * If we are allocating a directory block, then we must
5724 * allocate an associated pagedep to track additions and
5727 if ((ip->i_mode & IFMT) == IFDIR)
5728 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5731 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5732 panic("softdep_setup_allocdirect: lost block");
5733 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5734 ("softdep_setup_allocdirect: newblk already initialized"));
5736 * Convert the newblk to an allocdirect.
5738 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5739 adp = (struct allocdirect *)newblk;
5740 newblk->nb_freefrag = freefrag;
5741 adp->ad_offset = off;
5742 adp->ad_oldblkno = oldblkno;
5743 adp->ad_newsize = newsize;
5744 adp->ad_oldsize = oldsize;
5747 * Finish initializing the journal.
5749 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5750 jnewblk->jn_ino = ip->i_number;
5751 jnewblk->jn_lbn = lbn;
5752 add_to_journal(&jnewblk->jn_list);
5754 if (freefrag && freefrag->ff_jdep != NULL &&
5755 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5756 add_to_journal(freefrag->ff_jdep);
5757 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5758 adp->ad_inodedep = inodedep;
5760 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5762 * The list of allocdirects must be kept in sorted and ascending
5763 * order so that the rollback routines can quickly determine the
5764 * first uncommitted block (the size of the file stored on disk
5765 * ends at the end of the lowest committed fragment, or if there
5766 * are no fragments, at the end of the highest committed block).
5767 * Since files generally grow, the typical case is that the new
5768 * block is to be added at the end of the list. We speed this
5769 * special case by checking against the last allocdirect in the
5770 * list before laboriously traversing the list looking for the
5773 adphead = &inodedep->id_newinoupdt;
5774 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5775 if (oldadp == NULL || oldadp->ad_offset <= off) {
5776 /* insert at end of list */
5777 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5778 if (oldadp != NULL && oldadp->ad_offset == off)
5779 allocdirect_merge(adphead, adp, oldadp);
5780 FREE_LOCK(ITOUMP(ip));
5783 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5784 if (oldadp->ad_offset >= off)
5788 panic("softdep_setup_allocdirect: lost entry");
5789 /* insert in middle of list */
5790 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5791 if (oldadp->ad_offset == off)
5792 allocdirect_merge(adphead, adp, oldadp);
5794 FREE_LOCK(ITOUMP(ip));
5798 * Merge a newer and older journal record to be stored either in a
5799 * newblock or freefrag. This handles aggregating journal records for
5800 * fragment allocation into a second record as well as replacing a
5801 * journal free with an aborted journal allocation. A segment for the
5802 * oldest record will be placed on wkhd if it has been written. If not
5803 * the segment for the newer record will suffice.
5805 static struct worklist *
5806 jnewblk_merge(new, old, wkhd)
5807 struct worklist *new;
5808 struct worklist *old;
5809 struct workhead *wkhd;
5811 struct jnewblk *njnewblk;
5812 struct jnewblk *jnewblk;
5814 /* Handle NULLs to simplify callers. */
5819 /* Replace a jfreefrag with a jnewblk. */
5820 if (new->wk_type == D_JFREEFRAG) {
5821 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5822 panic("jnewblk_merge: blkno mismatch: %p, %p",
5824 cancel_jfreefrag(WK_JFREEFRAG(new));
5827 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5828 panic("jnewblk_merge: Bad type: old %d new %d\n",
5829 old->wk_type, new->wk_type);
5831 * Handle merging of two jnewblk records that describe
5832 * different sets of fragments in the same block.
5834 jnewblk = WK_JNEWBLK(old);
5835 njnewblk = WK_JNEWBLK(new);
5836 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5837 panic("jnewblk_merge: Merging disparate blocks.");
5839 * The record may be rolled back in the cg.
5841 if (jnewblk->jn_state & UNDONE) {
5842 jnewblk->jn_state &= ~UNDONE;
5843 njnewblk->jn_state |= UNDONE;
5844 njnewblk->jn_state &= ~ATTACHED;
5847 * We modify the newer addref and free the older so that if neither
5848 * has been written the most up-to-date copy will be on disk. If
5849 * both have been written but rolled back we only temporarily need
5850 * one of them to fix the bits when the cg write completes.
5852 jnewblk->jn_state |= ATTACHED | COMPLETE;
5853 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5854 cancel_jnewblk(jnewblk, wkhd);
5855 WORKLIST_REMOVE(&jnewblk->jn_list);
5856 free_jnewblk(jnewblk);
5861 * Replace an old allocdirect dependency with a newer one.
5864 allocdirect_merge(adphead, newadp, oldadp)
5865 struct allocdirectlst *adphead; /* head of list holding allocdirects */
5866 struct allocdirect *newadp; /* allocdirect being added */
5867 struct allocdirect *oldadp; /* existing allocdirect being checked */
5869 struct worklist *wk;
5870 struct freefrag *freefrag;
5873 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5874 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5875 newadp->ad_oldsize != oldadp->ad_newsize ||
5876 newadp->ad_offset >= UFS_NDADDR)
5877 panic("%s %jd != new %jd || old size %ld != new %ld",
5878 "allocdirect_merge: old blkno",
5879 (intmax_t)newadp->ad_oldblkno,
5880 (intmax_t)oldadp->ad_newblkno,
5881 newadp->ad_oldsize, oldadp->ad_newsize);
5882 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5883 newadp->ad_oldsize = oldadp->ad_oldsize;
5885 * If the old dependency had a fragment to free or had never
5886 * previously had a block allocated, then the new dependency
5887 * can immediately post its freefrag and adopt the old freefrag.
5888 * This action is done by swapping the freefrag dependencies.
5889 * The new dependency gains the old one's freefrag, and the
5890 * old one gets the new one and then immediately puts it on
5891 * the worklist when it is freed by free_newblk. It is
5892 * not possible to do this swap when the old dependency had a
5893 * non-zero size but no previous fragment to free. This condition
5894 * arises when the new block is an extension of the old block.
5895 * Here, the first part of the fragment allocated to the new
5896 * dependency is part of the block currently claimed on disk by
5897 * the old dependency, so cannot legitimately be freed until the
5898 * conditions for the new dependency are fulfilled.
5900 freefrag = newadp->ad_freefrag;
5901 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5902 newadp->ad_freefrag = oldadp->ad_freefrag;
5903 oldadp->ad_freefrag = freefrag;
5906 * If we are tracking a new directory-block allocation,
5907 * move it from the old allocdirect to the new allocdirect.
5909 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5910 WORKLIST_REMOVE(wk);
5911 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5912 panic("allocdirect_merge: extra newdirblk");
5913 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5915 TAILQ_REMOVE(adphead, oldadp, ad_next);
5917 * We need to move any journal dependencies over to the freefrag
5918 * that releases this block if it exists. Otherwise we are
5919 * extending an existing block and we'll wait until that is
5920 * complete to release the journal space and extend the
5921 * new journal to cover this old space as well.
5923 if (freefrag == NULL) {
5924 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5925 panic("allocdirect_merge: %jd != %jd",
5926 oldadp->ad_newblkno, newadp->ad_newblkno);
5927 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5928 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5929 &oldadp->ad_block.nb_jnewblk->jn_list,
5930 &newadp->ad_block.nb_jwork);
5931 oldadp->ad_block.nb_jnewblk = NULL;
5932 cancel_newblk(&oldadp->ad_block, NULL,
5933 &newadp->ad_block.nb_jwork);
5935 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5936 &freefrag->ff_list, &freefrag->ff_jwork);
5937 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5938 &freefrag->ff_jwork);
5940 free_newblk(&oldadp->ad_block);
5944 * Allocate a jfreefrag structure to journal a single block free.
5946 static struct jfreefrag *
5947 newjfreefrag(freefrag, ip, blkno, size, lbn)
5948 struct freefrag *freefrag;
5954 struct jfreefrag *jfreefrag;
5958 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5960 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5961 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5962 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5963 jfreefrag->fr_ino = ip->i_number;
5964 jfreefrag->fr_lbn = lbn;
5965 jfreefrag->fr_blkno = blkno;
5966 jfreefrag->fr_frags = numfrags(fs, size);
5967 jfreefrag->fr_freefrag = freefrag;
5973 * Allocate a new freefrag structure.
5975 static struct freefrag *
5976 newfreefrag(ip, blkno, size, lbn, key)
5983 struct freefrag *freefrag;
5984 struct ufsmount *ump;
5987 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5988 ip->i_number, blkno, size, lbn);
5991 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5992 panic("newfreefrag: frag size");
5993 freefrag = malloc(sizeof(struct freefrag),
5994 M_FREEFRAG, M_SOFTDEP_FLAGS);
5995 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5996 freefrag->ff_state = ATTACHED;
5997 LIST_INIT(&freefrag->ff_jwork);
5998 freefrag->ff_inum = ip->i_number;
5999 freefrag->ff_vtype = ITOV(ip)->v_type;
6000 freefrag->ff_blkno = blkno;
6001 freefrag->ff_fragsize = size;
6002 freefrag->ff_key = key;
6004 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
6005 freefrag->ff_jdep = (struct worklist *)
6006 newjfreefrag(freefrag, ip, blkno, size, lbn);
6008 freefrag->ff_state |= DEPCOMPLETE;
6009 freefrag->ff_jdep = NULL;
6016 * This workitem de-allocates fragments that were replaced during
6017 * file block allocation.
6020 handle_workitem_freefrag(freefrag)
6021 struct freefrag *freefrag;
6023 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
6024 struct workhead wkhd;
6027 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
6028 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
6030 * It would be illegal to add new completion items to the
6031 * freefrag after it was schedule to be done so it must be
6032 * safe to modify the list head here.
6036 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
6038 * If the journal has not been written we must cancel it here.
6040 if (freefrag->ff_jdep) {
6041 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
6042 panic("handle_workitem_freefrag: Unexpected type %d\n",
6043 freefrag->ff_jdep->wk_type);
6044 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
6047 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
6048 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
6049 &wkhd, freefrag->ff_key);
6051 WORKITEM_FREE(freefrag, D_FREEFRAG);
6056 * Set up a dependency structure for an external attributes data block.
6057 * This routine follows much of the structure of softdep_setup_allocdirect.
6058 * See the description of softdep_setup_allocdirect above for details.
6061 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
6064 ufs2_daddr_t newblkno;
6065 ufs2_daddr_t oldblkno;
6070 struct allocdirect *adp, *oldadp;
6071 struct allocdirectlst *adphead;
6072 struct freefrag *freefrag;
6073 struct inodedep *inodedep;
6074 struct jnewblk *jnewblk;
6075 struct newblk *newblk;
6077 struct ufsmount *ump;
6082 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6083 ("softdep_setup_allocext called on non-softdep filesystem"));
6084 KASSERT(off < UFS_NXADDR,
6085 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
6088 if (oldblkno && oldblkno != newblkno)
6090 * The usual case is that a smaller fragment that
6091 * was just allocated has been replaced with a bigger
6092 * fragment or a full-size block. If it is marked as
6093 * B_DELWRI, the current contents have not been written
6094 * to disk. It is possible that the block was written
6095 * earlier, but very uncommon. If the block has never
6096 * been written, there is no need to send a BIO_DELETE
6097 * for it when it is freed. The gain from avoiding the
6098 * TRIMs for the common case of unwritten blocks far
6099 * exceeds the cost of the write amplification for the
6100 * uncommon case of failing to send a TRIM for a block
6101 * that had been written.
6103 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
6104 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
6109 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
6110 panic("softdep_setup_allocext: lost block");
6111 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6112 ("softdep_setup_allocext: newblk already initialized"));
6114 * Convert the newblk to an allocdirect.
6116 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
6117 adp = (struct allocdirect *)newblk;
6118 newblk->nb_freefrag = freefrag;
6119 adp->ad_offset = off;
6120 adp->ad_oldblkno = oldblkno;
6121 adp->ad_newsize = newsize;
6122 adp->ad_oldsize = oldsize;
6123 adp->ad_state |= EXTDATA;
6126 * Finish initializing the journal.
6128 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6129 jnewblk->jn_ino = ip->i_number;
6130 jnewblk->jn_lbn = lbn;
6131 add_to_journal(&jnewblk->jn_list);
6133 if (freefrag && freefrag->ff_jdep != NULL &&
6134 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6135 add_to_journal(freefrag->ff_jdep);
6136 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6137 adp->ad_inodedep = inodedep;
6139 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
6141 * The list of allocdirects must be kept in sorted and ascending
6142 * order so that the rollback routines can quickly determine the
6143 * first uncommitted block (the size of the file stored on disk
6144 * ends at the end of the lowest committed fragment, or if there
6145 * are no fragments, at the end of the highest committed block).
6146 * Since files generally grow, the typical case is that the new
6147 * block is to be added at the end of the list. We speed this
6148 * special case by checking against the last allocdirect in the
6149 * list before laboriously traversing the list looking for the
6152 adphead = &inodedep->id_newextupdt;
6153 oldadp = TAILQ_LAST(adphead, allocdirectlst);
6154 if (oldadp == NULL || oldadp->ad_offset <= off) {
6155 /* insert at end of list */
6156 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
6157 if (oldadp != NULL && oldadp->ad_offset == off)
6158 allocdirect_merge(adphead, adp, oldadp);
6162 TAILQ_FOREACH(oldadp, adphead, ad_next) {
6163 if (oldadp->ad_offset >= off)
6167 panic("softdep_setup_allocext: lost entry");
6168 /* insert in middle of list */
6169 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
6170 if (oldadp->ad_offset == off)
6171 allocdirect_merge(adphead, adp, oldadp);
6176 * Indirect block allocation dependencies.
6178 * The same dependencies that exist for a direct block also exist when
6179 * a new block is allocated and pointed to by an entry in a block of
6180 * indirect pointers. The undo/redo states described above are also
6181 * used here. Because an indirect block contains many pointers that
6182 * may have dependencies, a second copy of the entire in-memory indirect
6183 * block is kept. The buffer cache copy is always completely up-to-date.
6184 * The second copy, which is used only as a source for disk writes,
6185 * contains only the safe pointers (i.e., those that have no remaining
6186 * update dependencies). The second copy is freed when all pointers
6187 * are safe. The cache is not allowed to replace indirect blocks with
6188 * pending update dependencies. If a buffer containing an indirect
6189 * block with dependencies is written, these routines will mark it
6190 * dirty again. It can only be successfully written once all the
6191 * dependencies are removed. The ffs_fsync routine in conjunction with
6192 * softdep_sync_metadata work together to get all the dependencies
6193 * removed so that a file can be successfully written to disk. Three
6194 * procedures are used when setting up indirect block pointer
6195 * dependencies. The division is necessary because of the organization
6196 * of the "balloc" routine and because of the distinction between file
6197 * pages and file metadata blocks.
6201 * Allocate a new allocindir structure.
6203 static struct allocindir *
6204 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
6205 struct inode *ip; /* inode for file being extended */
6206 int ptrno; /* offset of pointer in indirect block */
6207 ufs2_daddr_t newblkno; /* disk block number being added */
6208 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
6211 struct newblk *newblk;
6212 struct allocindir *aip;
6213 struct freefrag *freefrag;
6214 struct jnewblk *jnewblk;
6217 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
6221 ACQUIRE_LOCK(ITOUMP(ip));
6222 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
6223 panic("new_allocindir: lost block");
6224 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
6225 ("newallocindir: newblk already initialized"));
6226 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
6227 newblk->nb_freefrag = freefrag;
6228 aip = (struct allocindir *)newblk;
6229 aip->ai_offset = ptrno;
6230 aip->ai_oldblkno = oldblkno;
6232 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
6233 jnewblk->jn_ino = ip->i_number;
6234 jnewblk->jn_lbn = lbn;
6235 add_to_journal(&jnewblk->jn_list);
6237 if (freefrag && freefrag->ff_jdep != NULL &&
6238 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
6239 add_to_journal(freefrag->ff_jdep);
6244 * Called just before setting an indirect block pointer
6245 * to a newly allocated file page.
6248 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
6249 struct inode *ip; /* inode for file being extended */
6250 ufs_lbn_t lbn; /* allocated block number within file */
6251 struct buf *bp; /* buffer with indirect blk referencing page */
6252 int ptrno; /* offset of pointer in indirect block */
6253 ufs2_daddr_t newblkno; /* disk block number being added */
6254 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
6255 struct buf *nbp; /* buffer holding allocated page */
6257 struct inodedep *inodedep;
6258 struct freefrag *freefrag;
6259 struct allocindir *aip;
6260 struct pagedep *pagedep;
6262 struct ufsmount *ump;
6266 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6267 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
6268 KASSERT(lbn == nbp->b_lblkno,
6269 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
6270 lbn, bp->b_lblkno));
6272 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
6273 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
6274 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
6275 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
6276 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6278 * If we are allocating a directory page, then we must
6279 * allocate an associated pagedep to track additions and
6282 if ((ip->i_mode & IFMT) == IFDIR)
6283 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
6284 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6285 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
6288 handle_workitem_freefrag(freefrag);
6292 * Called just before setting an indirect block pointer to a
6293 * newly allocated indirect block.
6296 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
6297 struct buf *nbp; /* newly allocated indirect block */
6298 struct inode *ip; /* inode for file being extended */
6299 struct buf *bp; /* indirect block referencing allocated block */
6300 int ptrno; /* offset of pointer in indirect block */
6301 ufs2_daddr_t newblkno; /* disk block number being added */
6303 struct inodedep *inodedep;
6304 struct allocindir *aip;
6305 struct ufsmount *ump;
6309 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6310 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
6312 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
6313 ip->i_number, newblkno, ptrno);
6314 lbn = nbp->b_lblkno;
6315 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
6316 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
6317 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
6318 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
6319 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
6320 panic("softdep_setup_allocindir_meta: Block already existed");
6325 indirdep_complete(indirdep)
6326 struct indirdep *indirdep;
6328 struct allocindir *aip;
6330 LIST_REMOVE(indirdep, ir_next);
6331 indirdep->ir_state |= DEPCOMPLETE;
6333 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
6334 LIST_REMOVE(aip, ai_next);
6335 free_newblk(&aip->ai_block);
6338 * If this indirdep is not attached to a buf it was simply waiting
6339 * on completion to clear completehd. free_indirdep() asserts
6340 * that nothing is dangling.
6342 if ((indirdep->ir_state & ONWORKLIST) == 0)
6343 free_indirdep(indirdep);
6346 static struct indirdep *
6347 indirdep_lookup(mp, ip, bp)
6352 struct indirdep *indirdep, *newindirdep;
6353 struct newblk *newblk;
6354 struct ufsmount *ump;
6355 struct worklist *wk;
6365 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6366 if (wk->wk_type != D_INDIRDEP)
6368 indirdep = WK_INDIRDEP(wk);
6371 /* Found on the buffer worklist, no new structure to free. */
6372 if (indirdep != NULL && newindirdep == NULL)
6374 if (indirdep != NULL && newindirdep != NULL)
6375 panic("indirdep_lookup: simultaneous create");
6376 /* None found on the buffer and a new structure is ready. */
6377 if (indirdep == NULL && newindirdep != NULL)
6379 /* None found and no new structure available. */
6381 newindirdep = malloc(sizeof(struct indirdep),
6382 M_INDIRDEP, M_SOFTDEP_FLAGS);
6383 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6384 newindirdep->ir_state = ATTACHED;
6386 newindirdep->ir_state |= UFS1FMT;
6387 TAILQ_INIT(&newindirdep->ir_trunc);
6388 newindirdep->ir_saveddata = NULL;
6389 LIST_INIT(&newindirdep->ir_deplisthd);
6390 LIST_INIT(&newindirdep->ir_donehd);
6391 LIST_INIT(&newindirdep->ir_writehd);
6392 LIST_INIT(&newindirdep->ir_completehd);
6393 if (bp->b_blkno == bp->b_lblkno) {
6394 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6396 bp->b_blkno = blkno;
6398 newindirdep->ir_freeblks = NULL;
6399 newindirdep->ir_savebp =
6400 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6401 newindirdep->ir_bp = bp;
6402 BUF_KERNPROC(newindirdep->ir_savebp);
6403 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6406 indirdep = newindirdep;
6407 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6409 * If the block is not yet allocated we don't set DEPCOMPLETE so
6410 * that we don't free dependencies until the pointers are valid.
6411 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6412 * than using the hash.
6414 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6415 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6417 indirdep->ir_state |= DEPCOMPLETE;
6422 * Called to finish the allocation of the "aip" allocated
6423 * by one of the two routines above.
6425 static struct freefrag *
6426 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
6427 struct buf *bp; /* in-memory copy of the indirect block */
6428 struct inode *ip; /* inode for file being extended */
6429 struct inodedep *inodedep; /* Inodedep for ip */
6430 struct allocindir *aip; /* allocindir allocated by the above routines */
6431 ufs_lbn_t lbn; /* Logical block number for this block. */
6434 struct indirdep *indirdep;
6435 struct allocindir *oldaip;
6436 struct freefrag *freefrag;
6438 struct ufsmount *ump;
6444 if (bp->b_lblkno >= 0)
6445 panic("setup_allocindir_phase2: not indir blk");
6446 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6447 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6448 indirdep = indirdep_lookup(mp, ip, bp);
6449 KASSERT(indirdep->ir_savebp != NULL,
6450 ("setup_allocindir_phase2 NULL ir_savebp"));
6451 aip->ai_indirdep = indirdep;
6453 * Check for an unwritten dependency for this indirect offset. If
6454 * there is, merge the old dependency into the new one. This happens
6455 * as a result of reallocblk only.
6458 if (aip->ai_oldblkno != 0) {
6459 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6460 if (oldaip->ai_offset == aip->ai_offset) {
6461 freefrag = allocindir_merge(aip, oldaip);
6465 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6466 if (oldaip->ai_offset == aip->ai_offset) {
6467 freefrag = allocindir_merge(aip, oldaip);
6473 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6478 * Merge two allocindirs which refer to the same block. Move newblock
6479 * dependencies and setup the freefrags appropriately.
6481 static struct freefrag *
6482 allocindir_merge(aip, oldaip)
6483 struct allocindir *aip;
6484 struct allocindir *oldaip;
6486 struct freefrag *freefrag;
6487 struct worklist *wk;
6489 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6490 panic("allocindir_merge: blkno");
6491 aip->ai_oldblkno = oldaip->ai_oldblkno;
6492 freefrag = aip->ai_freefrag;
6493 aip->ai_freefrag = oldaip->ai_freefrag;
6494 oldaip->ai_freefrag = NULL;
6495 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6497 * If we are tracking a new directory-block allocation,
6498 * move it from the old allocindir to the new allocindir.
6500 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6501 WORKLIST_REMOVE(wk);
6502 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6503 panic("allocindir_merge: extra newdirblk");
6504 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6507 * We can skip journaling for this freefrag and just complete
6508 * any pending journal work for the allocindir that is being
6509 * removed after the freefrag completes.
6511 if (freefrag->ff_jdep)
6512 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6513 LIST_REMOVE(oldaip, ai_next);
6514 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6515 &freefrag->ff_list, &freefrag->ff_jwork);
6516 free_newblk(&oldaip->ai_block);
6522 setup_freedirect(freeblks, ip, i, needj)
6523 struct freeblks *freeblks;
6528 struct ufsmount *ump;
6532 blkno = DIP(ip, i_db[i]);
6535 DIP_SET(ip, i_db[i], 0);
6537 frags = sblksize(ump->um_fs, ip->i_size, i);
6538 frags = numfrags(ump->um_fs, frags);
6539 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6543 setup_freeext(freeblks, ip, i, needj)
6544 struct freeblks *freeblks;
6549 struct ufsmount *ump;
6553 blkno = ip->i_din2->di_extb[i];
6556 ip->i_din2->di_extb[i] = 0;
6558 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6559 frags = numfrags(ump->um_fs, frags);
6560 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6564 setup_freeindir(freeblks, ip, i, lbn, needj)
6565 struct freeblks *freeblks;
6571 struct ufsmount *ump;
6574 blkno = DIP(ip, i_ib[i]);
6577 DIP_SET(ip, i_ib[i], 0);
6579 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6583 static inline struct freeblks *
6588 struct freeblks *freeblks;
6590 freeblks = malloc(sizeof(struct freeblks),
6591 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6592 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6593 LIST_INIT(&freeblks->fb_jblkdephd);
6594 LIST_INIT(&freeblks->fb_jwork);
6595 freeblks->fb_ref = 0;
6596 freeblks->fb_cgwait = 0;
6597 freeblks->fb_state = ATTACHED;
6598 freeblks->fb_uid = ip->i_uid;
6599 freeblks->fb_inum = ip->i_number;
6600 freeblks->fb_vtype = ITOV(ip)->v_type;
6601 freeblks->fb_modrev = DIP(ip, i_modrev);
6602 freeblks->fb_devvp = ITODEVVP(ip);
6603 freeblks->fb_chkcnt = 0;
6604 freeblks->fb_len = 0;
6610 trunc_indirdep(indirdep, freeblks, bp, off)
6611 struct indirdep *indirdep;
6612 struct freeblks *freeblks;
6616 struct allocindir *aip, *aipn;
6619 * The first set of allocindirs won't be in savedbp.
6621 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6622 if (aip->ai_offset > off)
6623 cancel_allocindir(aip, bp, freeblks, 1);
6624 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6625 if (aip->ai_offset > off)
6626 cancel_allocindir(aip, bp, freeblks, 1);
6628 * These will exist in savedbp.
6630 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6631 if (aip->ai_offset > off)
6632 cancel_allocindir(aip, NULL, freeblks, 0);
6633 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6634 if (aip->ai_offset > off)
6635 cancel_allocindir(aip, NULL, freeblks, 0);
6639 * Follow the chain of indirects down to lastlbn creating a freework
6640 * structure for each. This will be used to start indir_trunc() at
6641 * the right offset and create the journal records for the parrtial
6642 * truncation. A second step will handle the truncated dependencies.
6645 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6646 struct freeblks *freeblks;
6652 struct indirdep *indirdep;
6653 struct indirdep *indirn;
6654 struct freework *freework;
6655 struct newblk *newblk;
6657 struct ufsmount *ump;
6669 mp = freeblks->fb_list.wk_mp;
6672 * Here, calls to VOP_BMAP() will fail. However, we already have
6673 * the on-disk address, so we just pass it to bread() instead of
6674 * having bread() attempt to calculate it using VOP_BMAP().
6676 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno),
6677 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6680 level = lbn_level(lbn);
6681 lbnadd = lbn_offset(ump->um_fs, level);
6683 * Compute the offset of the last block we want to keep. Store
6684 * in the freework the first block we want to completely free.
6686 off = (lastlbn - -(lbn + level)) / lbnadd;
6687 if (off + 1 == NINDIR(ump->um_fs))
6689 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6691 * Link the freework into the indirdep. This will prevent any new
6692 * allocations from proceeding until we are finished with the
6693 * truncate and the block is written.
6696 indirdep = indirdep_lookup(mp, ip, bp);
6697 if (indirdep->ir_freeblks)
6698 panic("setup_trunc_indir: indirdep already truncated.");
6699 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6700 freework->fw_indir = indirdep;
6702 * Cancel any allocindirs that will not make it to disk.
6703 * We have to do this for all copies of the indirdep that
6704 * live on this newblk.
6706 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6707 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6709 panic("setup_trunc_indir: lost block");
6710 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6711 trunc_indirdep(indirn, freeblks, bp, off);
6713 trunc_indirdep(indirdep, freeblks, bp, off);
6716 * Creation is protected by the buf lock. The saveddata is only
6717 * needed if a full truncation follows a partial truncation but it
6718 * is difficult to allocate in that case so we fetch it anyway.
6720 if (indirdep->ir_saveddata == NULL)
6721 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6724 /* Fetch the blkno of the child and the zero start offset. */
6725 if (I_IS_UFS1(ip)) {
6726 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6727 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6729 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6730 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6733 /* Zero the truncated pointers. */
6734 end = bp->b_data + bp->b_bcount;
6735 bzero(start, end - start);
6741 lbn++; /* adjust level */
6742 lbn -= (off * lbnadd);
6743 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6747 * Complete the partial truncation of an indirect block setup by
6748 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6749 * copy and writes them to disk before the freeblks is allowed to complete.
6752 complete_trunc_indir(freework)
6753 struct freework *freework;
6755 struct freework *fwn;
6756 struct indirdep *indirdep;
6757 struct ufsmount *ump;
6762 ump = VFSTOUFS(freework->fw_list.wk_mp);
6764 indirdep = freework->fw_indir;
6766 bp = indirdep->ir_bp;
6767 /* See if the block was discarded. */
6770 /* Inline part of getdirtybuf(). We dont want bremfree. */
6771 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6773 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6774 LOCK_PTR(ump)) == 0)
6778 freework->fw_state |= DEPCOMPLETE;
6779 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6781 * Zero the pointers in the saved copy.
6783 if (indirdep->ir_state & UFS1FMT)
6784 start = sizeof(ufs1_daddr_t);
6786 start = sizeof(ufs2_daddr_t);
6787 start *= freework->fw_start;
6788 count = indirdep->ir_savebp->b_bcount - start;
6789 start += (uintptr_t)indirdep->ir_savebp->b_data;
6790 bzero((char *)start, count);
6792 * We need to start the next truncation in the list if it has not
6795 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6797 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6798 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6799 if ((fwn->fw_state & ONWORKLIST) == 0)
6800 freework_enqueue(fwn);
6803 * If bp is NULL the block was fully truncated, restore
6804 * the saved block list otherwise free it if it is no
6807 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6809 bcopy(indirdep->ir_saveddata,
6810 indirdep->ir_savebp->b_data,
6811 indirdep->ir_savebp->b_bcount);
6812 free(indirdep->ir_saveddata, M_INDIRDEP);
6813 indirdep->ir_saveddata = NULL;
6816 * When bp is NULL there is a full truncation pending. We
6817 * must wait for this full truncation to be journaled before
6818 * we can release this freework because the disk pointers will
6819 * never be written as zero.
6822 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6823 handle_written_freework(freework);
6825 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6826 &freework->fw_list);
6828 freework->fw_indir = (void *)0x0000deadbeef0000;
6829 bp = indirdep->ir_savebp;
6830 indirdep->ir_savebp = NULL;
6831 free_indirdep(indirdep);
6837 /* Complete when the real copy is written. */
6838 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6844 * Calculate the number of blocks we are going to release where datablocks
6845 * is the current total and length is the new file size.
6848 blkcount(fs, datablocks, length)
6850 ufs2_daddr_t datablocks;
6853 off_t totblks, numblks;
6856 numblks = howmany(length, fs->fs_bsize);
6857 if (numblks <= UFS_NDADDR) {
6858 totblks = howmany(length, fs->fs_fsize);
6861 totblks = blkstofrags(fs, numblks);
6862 numblks -= UFS_NDADDR;
6864 * Count all single, then double, then triple indirects required.
6865 * Subtracting one indirects worth of blocks for each pass
6866 * acknowledges one of each pointed to by the inode.
6869 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6870 numblks -= NINDIR(fs);
6873 numblks = howmany(numblks, NINDIR(fs));
6876 totblks = fsbtodb(fs, totblks);
6878 * Handle sparse files. We can't reclaim more blocks than the inode
6879 * references. We will correct it later in handle_complete_freeblks()
6880 * when we know the real count.
6882 if (totblks > datablocks)
6884 return (datablocks - totblks);
6888 * Handle freeblocks for journaled softupdate filesystems.
6890 * Contrary to normal softupdates, we must preserve the block pointers in
6891 * indirects until their subordinates are free. This is to avoid journaling
6892 * every block that is freed which may consume more space than the journal
6893 * itself. The recovery program will see the free block journals at the
6894 * base of the truncated area and traverse them to reclaim space. The
6895 * pointers in the inode may be cleared immediately after the journal
6896 * records are written because each direct and indirect pointer in the
6897 * inode is recorded in a journal. This permits full truncation to proceed
6898 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6900 * The algorithm is as follows:
6901 * 1) Traverse the in-memory state and create journal entries to release
6902 * the relevant blocks and full indirect trees.
6903 * 2) Traverse the indirect block chain adding partial truncation freework
6904 * records to indirects in the path to lastlbn. The freework will
6905 * prevent new allocation dependencies from being satisfied in this
6906 * indirect until the truncation completes.
6907 * 3) Read and lock the inode block, performing an update with the new size
6908 * and pointers. This prevents truncated data from becoming valid on
6909 * disk through step 4.
6910 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6911 * eliminate journal work for those records that do not require it.
6912 * 5) Schedule the journal records to be written followed by the inode block.
6913 * 6) Allocate any necessary frags for the end of file.
6914 * 7) Zero any partially truncated blocks.
6916 * From this truncation proceeds asynchronously using the freework and
6917 * indir_trunc machinery. The file will not be extended again into a
6918 * partially truncated indirect block until all work is completed but
6919 * the normal dependency mechanism ensures that it is rolled back/forward
6920 * as appropriate. Further truncation may occur without delay and is
6921 * serialized in indir_trunc().
6924 softdep_journal_freeblocks(ip, cred, length, flags)
6925 struct inode *ip; /* The inode whose length is to be reduced */
6927 off_t length; /* The new length for the file */
6928 int flags; /* IO_EXT and/or IO_NORMAL */
6930 struct freeblks *freeblks, *fbn;
6931 struct worklist *wk, *wkn;
6932 struct inodedep *inodedep;
6933 struct jblkdep *jblkdep;
6934 struct allocdirect *adp, *adpn;
6935 struct ufsmount *ump;
6941 ufs2_daddr_t extblocks, datablocks;
6942 ufs_lbn_t tmpval, lbn, lastlbn;
6943 int frags, lastoff, iboff, allocblock, needj, error, i;
6948 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6949 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6957 freeblks = newfreeblks(mp, ip);
6960 * If we're truncating a removed file that will never be written
6961 * we don't need to journal the block frees. The canceled journals
6962 * for the allocations will suffice.
6964 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6965 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6968 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6969 ip->i_number, length, needj);
6972 * Calculate the lbn that we are truncating to. This results in -1
6973 * if we're truncating the 0 bytes. So it is the last lbn we want
6974 * to keep, not the first lbn we want to truncate.
6976 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6977 lastoff = blkoff(fs, length);
6979 * Compute frags we are keeping in lastlbn. 0 means all.
6981 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6982 frags = fragroundup(fs, lastoff);
6983 /* adp offset of last valid allocdirect. */
6985 } else if (lastlbn > 0)
6987 if (fs->fs_magic == FS_UFS2_MAGIC)
6988 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6990 * Handle normal data blocks and indirects. This section saves
6991 * values used after the inode update to complete frag and indirect
6994 if ((flags & IO_NORMAL) != 0) {
6996 * Handle truncation of whole direct and indirect blocks.
6998 for (i = iboff + 1; i < UFS_NDADDR; i++)
6999 setup_freedirect(freeblks, ip, i, needj);
7000 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
7002 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
7003 /* Release a whole indirect tree. */
7004 if (lbn > lastlbn) {
7005 setup_freeindir(freeblks, ip, i, -lbn -i,
7009 iboff = i + UFS_NDADDR;
7011 * Traverse partially truncated indirect tree.
7013 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
7014 setup_trunc_indir(freeblks, ip, -lbn - i,
7015 lastlbn, DIP(ip, i_ib[i]));
7018 * Handle partial truncation to a frag boundary.
7024 oldfrags = blksize(fs, ip, lastlbn);
7025 blkno = DIP(ip, i_db[lastlbn]);
7026 if (blkno && oldfrags != frags) {
7028 oldfrags = numfrags(fs, oldfrags);
7029 blkno += numfrags(fs, frags);
7030 newfreework(ump, freeblks, NULL, lastlbn,
7031 blkno, oldfrags, 0, needj);
7033 adjust_newfreework(freeblks,
7034 numfrags(fs, frags));
7035 } else if (blkno == 0)
7039 * Add a journal record for partial truncate if we are
7040 * handling indirect blocks. Non-indirects need no extra
7043 if (length != 0 && lastlbn >= UFS_NDADDR) {
7044 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
7045 newjtrunc(freeblks, length, 0);
7047 ip->i_size = length;
7048 DIP_SET(ip, i_size, ip->i_size);
7049 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7050 datablocks = DIP(ip, i_blocks) - extblocks;
7052 datablocks = blkcount(fs, datablocks, length);
7053 freeblks->fb_len = length;
7055 if ((flags & IO_EXT) != 0) {
7056 for (i = 0; i < UFS_NXADDR; i++)
7057 setup_freeext(freeblks, ip, i, needj);
7058 ip->i_din2->di_extsize = 0;
7059 datablocks += extblocks;
7060 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7063 /* Reference the quotas in case the block count is wrong in the end. */
7064 quotaref(vp, freeblks->fb_quota);
7065 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
7067 freeblks->fb_chkcnt = -datablocks;
7069 fs->fs_pendingblocks += datablocks;
7071 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7073 * Handle truncation of incomplete alloc direct dependencies. We
7074 * hold the inode block locked to prevent incomplete dependencies
7075 * from reaching the disk while we are eliminating those that
7076 * have been truncated. This is a partially inlined ffs_update().
7079 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
7080 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
7081 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
7082 NULL, NULL, 0, cred, 0, NULL, &bp);
7084 softdep_error("softdep_journal_freeblocks", error);
7087 if (bp->b_bufsize == fs->fs_bsize)
7088 bp->b_flags |= B_CLUSTEROK;
7089 softdep_update_inodeblock(ip, bp, 0);
7090 if (ump->um_fstype == UFS1) {
7091 *((struct ufs1_dinode *)bp->b_data +
7092 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
7094 ffs_update_dinode_ckhash(fs, ip->i_din2);
7095 *((struct ufs2_dinode *)bp->b_data +
7096 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
7099 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7100 if ((inodedep->id_state & IOSTARTED) != 0)
7101 panic("softdep_setup_freeblocks: inode busy");
7103 * Add the freeblks structure to the list of operations that
7104 * must await the zero'ed inode being written to disk. If we
7105 * still have a bitmap dependency (needj), then the inode
7106 * has never been written to disk, so we can process the
7107 * freeblks below once we have deleted the dependencies.
7110 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7112 freeblks->fb_state |= COMPLETE;
7113 if ((flags & IO_NORMAL) != 0) {
7114 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
7115 if (adp->ad_offset > iboff)
7116 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7119 * Truncate the allocdirect. We could eliminate
7120 * or modify journal records as well.
7122 else if (adp->ad_offset == iboff && frags)
7123 adp->ad_newsize = frags;
7126 if ((flags & IO_EXT) != 0)
7127 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7128 cancel_allocdirect(&inodedep->id_extupdt, adp,
7131 * Scan the bufwait list for newblock dependencies that will never
7134 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
7135 if (wk->wk_type != D_ALLOCDIRECT)
7137 adp = WK_ALLOCDIRECT(wk);
7138 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
7139 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
7140 cancel_jfreeblk(freeblks, adp->ad_newblkno);
7141 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
7142 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7148 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
7149 add_to_journal(&jblkdep->jb_list);
7153 * Truncate dependency structures beyond length.
7155 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
7157 * This is only set when we need to allocate a fragment because
7158 * none existed at the end of a frag-sized file. It handles only
7159 * allocating a new, zero filled block.
7162 ip->i_size = length - lastoff;
7163 DIP_SET(ip, i_size, ip->i_size);
7164 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
7166 softdep_error("softdep_journal_freeblks", error);
7169 ip->i_size = length;
7170 DIP_SET(ip, i_size, length);
7171 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
7172 allocbuf(bp, frags);
7175 } else if (lastoff != 0 && vp->v_type != VDIR) {
7179 * Zero the end of a truncated frag or block.
7181 size = sblksize(fs, length, lastlbn);
7182 error = bread(vp, lastlbn, size, cred, &bp);
7184 bzero((char *)bp->b_data + lastoff, size - lastoff);
7186 } else if (!ffs_fsfail_cleanup(ump, error)) {
7187 softdep_error("softdep_journal_freeblks", error);
7192 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7193 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
7194 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
7196 * We zero earlier truncations so they don't erroneously
7199 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
7200 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
7202 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
7203 LIST_EMPTY(&freeblks->fb_jblkdephd))
7204 freeblks->fb_state |= INPROGRESS;
7209 handle_workitem_freeblocks(freeblks, 0);
7210 trunc_pages(ip, length, extblocks, flags);
7215 * Flush a JOP_SYNC to the journal.
7218 softdep_journal_fsync(ip)
7221 struct jfsync *jfsync;
7222 struct ufsmount *ump;
7225 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7226 ("softdep_journal_fsync called on non-softdep filesystem"));
7227 if ((ip->i_flag & IN_TRUNCATED) == 0)
7229 ip->i_flag &= ~IN_TRUNCATED;
7230 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
7231 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
7232 jfsync->jfs_size = ip->i_size;
7233 jfsync->jfs_ino = ip->i_number;
7235 add_to_journal(&jfsync->jfs_list);
7236 jwait(&jfsync->jfs_list, MNT_WAIT);
7241 * Block de-allocation dependencies.
7243 * When blocks are de-allocated, the on-disk pointers must be nullified before
7244 * the blocks are made available for use by other files. (The true
7245 * requirement is that old pointers must be nullified before new on-disk
7246 * pointers are set. We chose this slightly more stringent requirement to
7247 * reduce complexity.) Our implementation handles this dependency by updating
7248 * the inode (or indirect block) appropriately but delaying the actual block
7249 * de-allocation (i.e., freemap and free space count manipulation) until
7250 * after the updated versions reach stable storage. After the disk is
7251 * updated, the blocks can be safely de-allocated whenever it is convenient.
7252 * This implementation handles only the common case of reducing a file's
7253 * length to zero. Other cases are handled by the conventional synchronous
7256 * The ffs implementation with which we worked double-checks
7257 * the state of the block pointers and file size as it reduces
7258 * a file's length. Some of this code is replicated here in our
7259 * soft updates implementation. The freeblks->fb_chkcnt field is
7260 * used to transfer a part of this information to the procedure
7261 * that eventually de-allocates the blocks.
7263 * This routine should be called from the routine that shortens
7264 * a file's length, before the inode's size or block pointers
7265 * are modified. It will save the block pointer information for
7266 * later release and zero the inode so that the calling routine
7270 softdep_setup_freeblocks(ip, length, flags)
7271 struct inode *ip; /* The inode whose length is to be reduced */
7272 off_t length; /* The new length for the file */
7273 int flags; /* IO_EXT and/or IO_NORMAL */
7275 struct ufs1_dinode *dp1;
7276 struct ufs2_dinode *dp2;
7277 struct freeblks *freeblks;
7278 struct inodedep *inodedep;
7279 struct allocdirect *adp;
7280 struct ufsmount *ump;
7283 ufs2_daddr_t extblocks, datablocks;
7285 int i, delay, error;
7291 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
7292 ("softdep_setup_freeblocks called on non-softdep filesystem"));
7293 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
7294 ip->i_number, length);
7295 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
7297 if ((error = bread(ump->um_devvp,
7298 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
7299 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
7300 if (!ffs_fsfail_cleanup(ump, error))
7301 softdep_error("softdep_setup_freeblocks", error);
7304 freeblks = newfreeblks(mp, ip);
7307 if (fs->fs_magic == FS_UFS2_MAGIC)
7308 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
7309 if ((flags & IO_NORMAL) != 0) {
7310 for (i = 0; i < UFS_NDADDR; i++)
7311 setup_freedirect(freeblks, ip, i, 0);
7312 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
7314 i++, lbn += tmpval, tmpval *= NINDIR(fs))
7315 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
7317 DIP_SET(ip, i_size, 0);
7318 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7319 datablocks = DIP(ip, i_blocks) - extblocks;
7321 if ((flags & IO_EXT) != 0) {
7322 for (i = 0; i < UFS_NXADDR; i++)
7323 setup_freeext(freeblks, ip, i, 0);
7324 ip->i_din2->di_extsize = 0;
7325 datablocks += extblocks;
7326 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
7329 /* Reference the quotas in case the block count is wrong in the end. */
7330 quotaref(ITOV(ip), freeblks->fb_quota);
7331 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
7333 freeblks->fb_chkcnt = -datablocks;
7335 fs->fs_pendingblocks += datablocks;
7337 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7339 * Push the zero'ed inode to its disk buffer so that we are free
7340 * to delete its dependencies below. Once the dependencies are gone
7341 * the buffer can be safely released.
7343 if (ump->um_fstype == UFS1) {
7344 dp1 = ((struct ufs1_dinode *)bp->b_data +
7345 ino_to_fsbo(fs, ip->i_number));
7346 ip->i_din1->di_freelink = dp1->di_freelink;
7349 dp2 = ((struct ufs2_dinode *)bp->b_data +
7350 ino_to_fsbo(fs, ip->i_number));
7351 ip->i_din2->di_freelink = dp2->di_freelink;
7352 ffs_update_dinode_ckhash(fs, ip->i_din2);
7356 * Find and eliminate any inode dependencies.
7359 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7360 if ((inodedep->id_state & IOSTARTED) != 0)
7361 panic("softdep_setup_freeblocks: inode busy");
7363 * Add the freeblks structure to the list of operations that
7364 * must await the zero'ed inode being written to disk. If we
7365 * still have a bitmap dependency (delay == 0), then the inode
7366 * has never been written to disk, so we can process the
7367 * freeblks below once we have deleted the dependencies.
7369 delay = (inodedep->id_state & DEPCOMPLETE);
7371 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7373 freeblks->fb_state |= COMPLETE;
7375 * Because the file length has been truncated to zero, any
7376 * pending block allocation dependency structures associated
7377 * with this inode are obsolete and can simply be de-allocated.
7378 * We must first merge the two dependency lists to get rid of
7379 * any duplicate freefrag structures, then purge the merged list.
7380 * If we still have a bitmap dependency, then the inode has never
7381 * been written to disk, so we can free any fragments without delay.
7383 if (flags & IO_NORMAL) {
7384 merge_inode_lists(&inodedep->id_newinoupdt,
7385 &inodedep->id_inoupdt);
7386 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7387 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7390 if (flags & IO_EXT) {
7391 merge_inode_lists(&inodedep->id_newextupdt,
7392 &inodedep->id_extupdt);
7393 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7394 cancel_allocdirect(&inodedep->id_extupdt, adp,
7399 trunc_dependencies(ip, freeblks, -1, 0, flags);
7401 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7402 (void) free_inodedep(inodedep);
7403 freeblks->fb_state |= DEPCOMPLETE;
7405 * If the inode with zeroed block pointers is now on disk
7406 * we can start freeing blocks.
7408 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7409 freeblks->fb_state |= INPROGRESS;
7414 handle_workitem_freeblocks(freeblks, 0);
7415 trunc_pages(ip, length, extblocks, flags);
7419 * Eliminate pages from the page cache that back parts of this inode and
7420 * adjust the vnode pager's idea of our size. This prevents stale data
7421 * from hanging around in the page cache.
7424 trunc_pages(ip, length, extblocks, flags)
7427 ufs2_daddr_t extblocks;
7437 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7438 if ((flags & IO_EXT) != 0)
7439 vn_pages_remove(vp, extend, 0);
7440 if ((flags & IO_NORMAL) == 0)
7442 BO_LOCK(&vp->v_bufobj);
7444 BO_UNLOCK(&vp->v_bufobj);
7446 * The vnode pager eliminates file pages we eliminate indirects
7449 vnode_pager_setsize(vp, length);
7451 * Calculate the end based on the last indirect we want to keep. If
7452 * the block extends into indirects we can just use the negative of
7453 * its lbn. Doubles and triples exist at lower numbers so we must
7454 * be careful not to remove those, if they exist. double and triple
7455 * indirect lbns do not overlap with others so it is not important
7456 * to verify how many levels are required.
7458 lbn = lblkno(fs, length);
7459 if (lbn >= UFS_NDADDR) {
7460 /* Calculate the virtual lbn of the triple indirect. */
7461 lbn = -lbn - (UFS_NIADDR - 1);
7462 end = OFF_TO_IDX(lblktosize(fs, lbn));
7465 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7469 * See if the buf bp is in the range eliminated by truncation.
7472 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7482 /* Only match ext/normal blocks as appropriate. */
7483 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7484 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7486 /* ALTDATA is always a full truncation. */
7487 if ((bp->b_xflags & BX_ALTDATA) != 0)
7489 /* -1 is full truncation. */
7493 * If this is a partial truncate we only want those
7494 * blocks and indirect blocks that cover the range
7499 lbn = -(lbn + lbn_level(lbn));
7502 /* Here we only truncate lblkno if it's partial. */
7503 if (lbn == lastlbn) {
7512 * Eliminate any dependencies that exist in memory beyond lblkno:off
7515 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7517 struct freeblks *freeblks;
7528 * We must wait for any I/O in progress to finish so that
7529 * all potential buffers on the dirty list will be visible.
7530 * Once they are all there, walk the list and get rid of
7537 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7538 bp->b_vflags &= ~BV_SCANNED;
7540 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7541 if (bp->b_vflags & BV_SCANNED)
7543 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7544 bp->b_vflags |= BV_SCANNED;
7547 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7548 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7551 if (deallocate_dependencies(bp, freeblks, blkoff))
7559 * Now do the work of vtruncbuf while also matching indirect blocks.
7561 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7562 bp->b_vflags &= ~BV_SCANNED;
7564 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7565 if (bp->b_vflags & BV_SCANNED)
7567 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7568 bp->b_vflags |= BV_SCANNED;
7572 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7573 BO_LOCKPTR(bo)) == ENOLCK) {
7578 bp->b_vflags |= BV_SCANNED;
7582 allocbuf(bp, blkoff);
7585 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7596 cancel_pagedep(pagedep, freeblks, blkoff)
7597 struct pagedep *pagedep;
7598 struct freeblks *freeblks;
7601 struct jremref *jremref;
7602 struct jmvref *jmvref;
7603 struct dirrem *dirrem, *tmp;
7607 * Copy any directory remove dependencies to the list
7608 * to be processed after the freeblks proceeds. If
7609 * directory entry never made it to disk they
7610 * can be dumped directly onto the work list.
7612 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7613 /* Skip this directory removal if it is intended to remain. */
7614 if (dirrem->dm_offset < blkoff)
7617 * If there are any dirrems we wait for the journal write
7618 * to complete and then restart the buf scan as the lock
7621 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7622 jwait(&jremref->jr_list, MNT_WAIT);
7625 LIST_REMOVE(dirrem, dm_next);
7626 dirrem->dm_dirinum = pagedep->pd_ino;
7627 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7629 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7630 jwait(&jmvref->jm_list, MNT_WAIT);
7634 * When we're partially truncating a pagedep we just want to flush
7635 * journal entries and return. There can not be any adds in the
7636 * truncated portion of the directory and newblk must remain if
7637 * part of the block remains.
7642 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7643 if (dap->da_offset > blkoff)
7644 panic("cancel_pagedep: diradd %p off %d > %d",
7645 dap, dap->da_offset, blkoff);
7646 for (i = 0; i < DAHASHSZ; i++)
7647 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7648 if (dap->da_offset > blkoff)
7649 panic("cancel_pagedep: diradd %p off %d > %d",
7650 dap, dap->da_offset, blkoff);
7654 * There should be no directory add dependencies present
7655 * as the directory could not be truncated until all
7656 * children were removed.
7658 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7659 ("deallocate_dependencies: pendinghd != NULL"));
7660 for (i = 0; i < DAHASHSZ; i++)
7661 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7662 ("deallocate_dependencies: diraddhd != NULL"));
7663 if ((pagedep->pd_state & NEWBLOCK) != 0)
7664 free_newdirblk(pagedep->pd_newdirblk);
7665 if (free_pagedep(pagedep) == 0)
7666 panic("Failed to free pagedep %p", pagedep);
7671 * Reclaim any dependency structures from a buffer that is about to
7672 * be reallocated to a new vnode. The buffer must be locked, thus,
7673 * no I/O completion operations can occur while we are manipulating
7674 * its associated dependencies. The mutex is held so that other I/O's
7675 * associated with related dependencies do not occur.
7678 deallocate_dependencies(bp, freeblks, off)
7680 struct freeblks *freeblks;
7683 struct indirdep *indirdep;
7684 struct pagedep *pagedep;
7685 struct worklist *wk, *wkn;
7686 struct ufsmount *ump;
7688 ump = softdep_bp_to_mp(bp);
7692 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7693 switch (wk->wk_type) {
7695 indirdep = WK_INDIRDEP(wk);
7696 if (bp->b_lblkno >= 0 ||
7697 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7698 panic("deallocate_dependencies: not indir");
7699 cancel_indirdep(indirdep, bp, freeblks);
7703 pagedep = WK_PAGEDEP(wk);
7704 if (cancel_pagedep(pagedep, freeblks, off)) {
7712 * Simply remove the allocindir, we'll find it via
7713 * the indirdep where we can clear pointers if
7716 WORKLIST_REMOVE(wk);
7721 * A truncation is waiting for the zero'd pointers
7722 * to be written. It can be freed when the freeblks
7725 WORKLIST_REMOVE(wk);
7726 wk->wk_state |= ONDEPLIST;
7727 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7735 panic("deallocate_dependencies: Unexpected type %s",
7736 TYPENAME(wk->wk_type));
7743 * Don't throw away this buf, we were partially truncating and
7744 * some deps may always remain.
7748 bp->b_vflags |= BV_SCANNED;
7751 bp->b_flags |= B_INVAL | B_NOCACHE;
7757 * An allocdirect is being canceled due to a truncate. We must make sure
7758 * the journal entry is released in concert with the blkfree that releases
7759 * the storage. Completed journal entries must not be released until the
7760 * space is no longer pointed to by the inode or in the bitmap.
7763 cancel_allocdirect(adphead, adp, freeblks)
7764 struct allocdirectlst *adphead;
7765 struct allocdirect *adp;
7766 struct freeblks *freeblks;
7768 struct freework *freework;
7769 struct newblk *newblk;
7770 struct worklist *wk;
7772 TAILQ_REMOVE(adphead, adp, ad_next);
7773 newblk = (struct newblk *)adp;
7776 * Find the correct freework structure.
7778 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7779 if (wk->wk_type != D_FREEWORK)
7781 freework = WK_FREEWORK(wk);
7782 if (freework->fw_blkno == newblk->nb_newblkno)
7785 if (freework == NULL)
7786 panic("cancel_allocdirect: Freework not found");
7788 * If a newblk exists at all we still have the journal entry that
7789 * initiated the allocation so we do not need to journal the free.
7791 cancel_jfreeblk(freeblks, freework->fw_blkno);
7793 * If the journal hasn't been written the jnewblk must be passed
7794 * to the call to ffs_blkfree that reclaims the space. We accomplish
7795 * this by linking the journal dependency into the freework to be
7796 * freed when freework_freeblock() is called. If the journal has
7797 * been written we can simply reclaim the journal space when the
7798 * freeblks work is complete.
7800 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7801 &freeblks->fb_jwork);
7802 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7806 * Cancel a new block allocation. May be an indirect or direct block. We
7807 * remove it from various lists and return any journal record that needs to
7808 * be resolved by the caller.
7810 * A special consideration is made for indirects which were never pointed
7811 * at on disk and will never be found once this block is released.
7813 static struct jnewblk *
7814 cancel_newblk(newblk, wk, wkhd)
7815 struct newblk *newblk;
7816 struct worklist *wk;
7817 struct workhead *wkhd;
7819 struct jnewblk *jnewblk;
7821 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7823 newblk->nb_state |= GOINGAWAY;
7825 * Previously we traversed the completedhd on each indirdep
7826 * attached to this newblk to cancel them and gather journal
7827 * work. Since we need only the oldest journal segment and
7828 * the lowest point on the tree will always have the oldest
7829 * journal segment we are free to release the segments
7830 * of any subordinates and may leave the indirdep list to
7831 * indirdep_complete() when this newblk is freed.
7833 if (newblk->nb_state & ONDEPLIST) {
7834 newblk->nb_state &= ~ONDEPLIST;
7835 LIST_REMOVE(newblk, nb_deps);
7837 if (newblk->nb_state & ONWORKLIST)
7838 WORKLIST_REMOVE(&newblk->nb_list);
7840 * If the journal entry hasn't been written we save a pointer to
7841 * the dependency that frees it until it is written or the
7842 * superseding operation completes.
7844 jnewblk = newblk->nb_jnewblk;
7845 if (jnewblk != NULL && wk != NULL) {
7846 newblk->nb_jnewblk = NULL;
7847 jnewblk->jn_dep = wk;
7849 if (!LIST_EMPTY(&newblk->nb_jwork))
7850 jwork_move(wkhd, &newblk->nb_jwork);
7852 * When truncating we must free the newdirblk early to remove
7853 * the pagedep from the hash before returning.
7855 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7856 free_newdirblk(WK_NEWDIRBLK(wk));
7857 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7858 panic("cancel_newblk: extra newdirblk");
7864 * Schedule the freefrag associated with a newblk to be released once
7865 * the pointers are written and the previous block is no longer needed.
7868 newblk_freefrag(newblk)
7869 struct newblk *newblk;
7871 struct freefrag *freefrag;
7873 if (newblk->nb_freefrag == NULL)
7875 freefrag = newblk->nb_freefrag;
7876 newblk->nb_freefrag = NULL;
7877 freefrag->ff_state |= COMPLETE;
7878 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7879 add_to_worklist(&freefrag->ff_list, 0);
7883 * Free a newblk. Generate a new freefrag work request if appropriate.
7884 * This must be called after the inode pointer and any direct block pointers
7885 * are valid or fully removed via truncate or frag extension.
7889 struct newblk *newblk;
7891 struct indirdep *indirdep;
7892 struct worklist *wk;
7894 KASSERT(newblk->nb_jnewblk == NULL,
7895 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7896 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7897 ("free_newblk: unclaimed newblk"));
7898 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7899 newblk_freefrag(newblk);
7900 if (newblk->nb_state & ONDEPLIST)
7901 LIST_REMOVE(newblk, nb_deps);
7902 if (newblk->nb_state & ONWORKLIST)
7903 WORKLIST_REMOVE(&newblk->nb_list);
7904 LIST_REMOVE(newblk, nb_hash);
7905 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7906 free_newdirblk(WK_NEWDIRBLK(wk));
7907 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7908 panic("free_newblk: extra newdirblk");
7909 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7910 indirdep_complete(indirdep);
7911 handle_jwork(&newblk->nb_jwork);
7912 WORKITEM_FREE(newblk, D_NEWBLK);
7916 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7919 free_newdirblk(newdirblk)
7920 struct newdirblk *newdirblk;
7922 struct pagedep *pagedep;
7924 struct worklist *wk;
7926 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7927 WORKLIST_REMOVE(&newdirblk->db_list);
7929 * If the pagedep is still linked onto the directory buffer
7930 * dependency chain, then some of the entries on the
7931 * pd_pendinghd list may not be committed to disk yet. In
7932 * this case, we will simply clear the NEWBLOCK flag and
7933 * let the pd_pendinghd list be processed when the pagedep
7934 * is next written. If the pagedep is no longer on the buffer
7935 * dependency chain, then all the entries on the pd_pending
7936 * list are committed to disk and we can free them here.
7938 pagedep = newdirblk->db_pagedep;
7939 pagedep->pd_state &= ~NEWBLOCK;
7940 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7941 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7942 free_diradd(dap, NULL);
7944 * If no dependencies remain, the pagedep will be freed.
7946 free_pagedep(pagedep);
7948 /* Should only ever be one item in the list. */
7949 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7950 WORKLIST_REMOVE(wk);
7951 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7953 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7957 * Prepare an inode to be freed. The actual free operation is not
7958 * done until the zero'ed inode has been written to disk.
7961 softdep_freefile(pvp, ino, mode)
7966 struct inode *ip = VTOI(pvp);
7967 struct inodedep *inodedep;
7968 struct freefile *freefile;
7969 struct freeblks *freeblks;
7970 struct ufsmount *ump;
7973 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7974 ("softdep_freefile called on non-softdep filesystem"));
7976 * This sets up the inode de-allocation dependency.
7978 freefile = malloc(sizeof(struct freefile),
7979 M_FREEFILE, M_SOFTDEP_FLAGS);
7980 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7981 freefile->fx_mode = mode;
7982 freefile->fx_oldinum = ino;
7983 freefile->fx_devvp = ump->um_devvp;
7984 LIST_INIT(&freefile->fx_jwork);
7986 ump->um_fs->fs_pendinginodes += 1;
7990 * If the inodedep does not exist, then the zero'ed inode has
7991 * been written to disk. If the allocated inode has never been
7992 * written to disk, then the on-disk inode is zero'ed. In either
7993 * case we can free the file immediately. If the journal was
7994 * canceled before being written the inode will never make it to
7995 * disk and we must send the canceled journal entrys to
7996 * ffs_freefile() to be cleared in conjunction with the bitmap.
7997 * Any blocks waiting on the inode to write can be safely freed
7998 * here as it will never been written.
8001 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
8004 * Clear out freeblks that no longer need to reference
8008 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
8009 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
8011 freeblks->fb_state &= ~ONDEPLIST;
8014 * Remove this inode from the unlinked list.
8016 if (inodedep->id_state & UNLINKED) {
8018 * Save the journal work to be freed with the bitmap
8019 * before we clear UNLINKED. Otherwise it can be lost
8020 * if the inode block is written.
8022 handle_bufwait(inodedep, &freefile->fx_jwork);
8023 clear_unlinked_inodedep(inodedep);
8025 * Re-acquire inodedep as we've dropped the
8026 * per-filesystem lock in clear_unlinked_inodedep().
8028 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
8031 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
8033 handle_workitem_freefile(freefile);
8036 if ((inodedep->id_state & DEPCOMPLETE) == 0)
8037 inodedep->id_state |= GOINGAWAY;
8038 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
8040 if (ip->i_number == ino)
8041 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
8045 * Check to see if an inode has never been written to disk. If
8046 * so free the inodedep and return success, otherwise return failure.
8048 * If we still have a bitmap dependency, then the inode has never
8049 * been written to disk. Drop the dependency as it is no longer
8050 * necessary since the inode is being deallocated. We set the
8051 * ALLCOMPLETE flags since the bitmap now properly shows that the
8052 * inode is not allocated. Even if the inode is actively being
8053 * written, it has been rolled back to its zero'ed state, so we
8054 * are ensured that a zero inode is what is on the disk. For short
8055 * lived files, this change will usually result in removing all the
8056 * dependencies from the inode so that it can be freed immediately.
8059 check_inode_unwritten(inodedep)
8060 struct inodedep *inodedep;
8063 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8065 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
8066 !LIST_EMPTY(&inodedep->id_dirremhd) ||
8067 !LIST_EMPTY(&inodedep->id_pendinghd) ||
8068 !LIST_EMPTY(&inodedep->id_bufwait) ||
8069 !LIST_EMPTY(&inodedep->id_inowait) ||
8070 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
8071 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
8072 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
8073 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
8074 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
8075 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
8076 inodedep->id_mkdiradd != NULL ||
8077 inodedep->id_nlinkdelta != 0)
8080 * Another process might be in initiate_write_inodeblock_ufs[12]
8081 * trying to allocate memory without holding "Softdep Lock".
8083 if ((inodedep->id_state & IOSTARTED) != 0 &&
8084 inodedep->id_savedino1 == NULL)
8087 if (inodedep->id_state & ONDEPLIST)
8088 LIST_REMOVE(inodedep, id_deps);
8089 inodedep->id_state &= ~ONDEPLIST;
8090 inodedep->id_state |= ALLCOMPLETE;
8091 inodedep->id_bmsafemap = NULL;
8092 if (inodedep->id_state & ONWORKLIST)
8093 WORKLIST_REMOVE(&inodedep->id_list);
8094 if (inodedep->id_savedino1 != NULL) {
8095 free(inodedep->id_savedino1, M_SAVEDINO);
8096 inodedep->id_savedino1 = NULL;
8098 if (free_inodedep(inodedep) == 0)
8099 panic("check_inode_unwritten: busy inode");
8104 check_inodedep_free(inodedep)
8105 struct inodedep *inodedep;
8108 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8109 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
8110 !LIST_EMPTY(&inodedep->id_dirremhd) ||
8111 !LIST_EMPTY(&inodedep->id_pendinghd) ||
8112 !LIST_EMPTY(&inodedep->id_bufwait) ||
8113 !LIST_EMPTY(&inodedep->id_inowait) ||
8114 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
8115 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
8116 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
8117 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
8118 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
8119 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
8120 inodedep->id_mkdiradd != NULL ||
8121 inodedep->id_nlinkdelta != 0 ||
8122 inodedep->id_savedino1 != NULL)
8128 * Try to free an inodedep structure. Return 1 if it could be freed.
8131 free_inodedep(inodedep)
8132 struct inodedep *inodedep;
8135 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
8136 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
8137 !check_inodedep_free(inodedep))
8139 if (inodedep->id_state & ONDEPLIST)
8140 LIST_REMOVE(inodedep, id_deps);
8141 LIST_REMOVE(inodedep, id_hash);
8142 WORKITEM_FREE(inodedep, D_INODEDEP);
8147 * Free the block referenced by a freework structure. The parent freeblks
8148 * structure is released and completed when the final cg bitmap reaches
8149 * the disk. This routine may be freeing a jnewblk which never made it to
8150 * disk in which case we do not have to wait as the operation is undone
8151 * in memory immediately.
8154 freework_freeblock(freework, key)
8155 struct freework *freework;
8158 struct freeblks *freeblks;
8159 struct jnewblk *jnewblk;
8160 struct ufsmount *ump;
8161 struct workhead wkhd;
8166 ump = VFSTOUFS(freework->fw_list.wk_mp);
8169 * Handle partial truncate separately.
8171 if (freework->fw_indir) {
8172 complete_trunc_indir(freework);
8175 freeblks = freework->fw_freeblks;
8177 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
8178 bsize = lfragtosize(fs, freework->fw_frags);
8181 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
8182 * on the indirblk hashtable and prevents premature freeing.
8184 freework->fw_state |= DEPCOMPLETE;
8186 * SUJ needs to wait for the segment referencing freed indirect
8187 * blocks to expire so that we know the checker will not confuse
8188 * a re-allocated indirect block with its old contents.
8190 if (needj && freework->fw_lbn <= -UFS_NDADDR)
8191 indirblk_insert(freework);
8193 * If we are canceling an existing jnewblk pass it to the free
8194 * routine, otherwise pass the freeblk which will ultimately
8195 * release the freeblks. If we're not journaling, we can just
8196 * free the freeblks immediately.
8198 jnewblk = freework->fw_jnewblk;
8199 if (jnewblk != NULL) {
8200 cancel_jnewblk(jnewblk, &wkhd);
8203 freework->fw_state |= DELAYEDFREE;
8204 freeblks->fb_cgwait++;
8205 WORKLIST_INSERT(&wkhd, &freework->fw_list);
8208 freeblks_free(ump, freeblks, btodb(bsize));
8210 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
8211 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
8212 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
8213 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
8216 * The jnewblk will be discarded and the bits in the map never
8217 * made it to disk. We can immediately free the freeblk.
8220 handle_written_freework(freework);
8224 * We enqueue freework items that need processing back on the freeblks and
8225 * add the freeblks to the worklist. This makes it easier to find all work
8226 * required to flush a truncation in process_truncates().
8229 freework_enqueue(freework)
8230 struct freework *freework;
8232 struct freeblks *freeblks;
8234 freeblks = freework->fw_freeblks;
8235 if ((freework->fw_state & INPROGRESS) == 0)
8236 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
8237 if ((freeblks->fb_state &
8238 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
8239 LIST_EMPTY(&freeblks->fb_jblkdephd))
8240 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8244 * Start, continue, or finish the process of freeing an indirect block tree.
8245 * The free operation may be paused at any point with fw_off containing the
8246 * offset to restart from. This enables us to implement some flow control
8247 * for large truncates which may fan out and generate a huge number of
8251 handle_workitem_indirblk(freework)
8252 struct freework *freework;
8254 struct freeblks *freeblks;
8255 struct ufsmount *ump;
8258 freeblks = freework->fw_freeblks;
8259 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8261 if (freework->fw_state & DEPCOMPLETE) {
8262 handle_written_freework(freework);
8265 if (freework->fw_off == NINDIR(fs)) {
8266 freework_freeblock(freework, SINGLETON_KEY);
8269 freework->fw_state |= INPROGRESS;
8271 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
8277 * Called when a freework structure attached to a cg buf is written. The
8278 * ref on either the parent or the freeblks structure is released and
8279 * the freeblks is added back to the worklist if there is more work to do.
8282 handle_written_freework(freework)
8283 struct freework *freework;
8285 struct freeblks *freeblks;
8286 struct freework *parent;
8288 freeblks = freework->fw_freeblks;
8289 parent = freework->fw_parent;
8290 if (freework->fw_state & DELAYEDFREE)
8291 freeblks->fb_cgwait--;
8292 freework->fw_state |= COMPLETE;
8293 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
8294 WORKITEM_FREE(freework, D_FREEWORK);
8296 if (--parent->fw_ref == 0)
8297 freework_enqueue(parent);
8300 if (--freeblks->fb_ref != 0)
8302 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
8303 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
8304 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
8308 * This workitem routine performs the block de-allocation.
8309 * The workitem is added to the pending list after the updated
8310 * inode block has been written to disk. As mentioned above,
8311 * checks regarding the number of blocks de-allocated (compared
8312 * to the number of blocks allocated for the file) are also
8313 * performed in this function.
8316 handle_workitem_freeblocks(freeblks, flags)
8317 struct freeblks *freeblks;
8320 struct freework *freework;
8321 struct newblk *newblk;
8322 struct allocindir *aip;
8323 struct ufsmount *ump;
8324 struct worklist *wk;
8327 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
8328 ("handle_workitem_freeblocks: Journal entries not written."));
8329 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8330 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8332 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
8333 WORKLIST_REMOVE(wk);
8334 switch (wk->wk_type) {
8336 wk->wk_state |= COMPLETE;
8337 add_to_worklist(wk, 0);
8341 free_newblk(WK_NEWBLK(wk));
8345 aip = WK_ALLOCINDIR(wk);
8347 if (aip->ai_state & DELAYEDFREE) {
8349 freework = newfreework(ump, freeblks, NULL,
8350 aip->ai_lbn, aip->ai_newblkno,
8351 ump->um_fs->fs_frag, 0, 0);
8354 newblk = WK_NEWBLK(wk);
8355 if (newblk->nb_jnewblk) {
8356 freework->fw_jnewblk = newblk->nb_jnewblk;
8357 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
8358 newblk->nb_jnewblk = NULL;
8360 free_newblk(newblk);
8364 freework = WK_FREEWORK(wk);
8365 if (freework->fw_lbn <= -UFS_NDADDR)
8366 handle_workitem_indirblk(freework);
8368 freework_freeblock(freework, key);
8371 panic("handle_workitem_freeblocks: Unknown type %s",
8372 TYPENAME(wk->wk_type));
8375 if (freeblks->fb_ref != 0) {
8376 freeblks->fb_state &= ~INPROGRESS;
8377 wake_worklist(&freeblks->fb_list);
8381 ffs_blkrelease_finish(ump, key);
8383 return handle_complete_freeblocks(freeblks, flags);
8388 * Handle completion of block free via truncate. This allows fs_pending
8389 * to track the actual free block count more closely than if we only updated
8390 * it at the end. We must be careful to handle cases where the block count
8391 * on free was incorrect.
8394 freeblks_free(ump, freeblks, blocks)
8395 struct ufsmount *ump;
8396 struct freeblks *freeblks;
8400 ufs2_daddr_t remain;
8403 remain = -freeblks->fb_chkcnt;
8404 freeblks->fb_chkcnt += blocks;
8406 if (remain < blocks)
8409 fs->fs_pendingblocks -= blocks;
8415 * Once all of the freework workitems are complete we can retire the
8416 * freeblocks dependency and any journal work awaiting completion. This
8417 * can not be called until all other dependencies are stable on disk.
8420 handle_complete_freeblocks(freeblks, flags)
8421 struct freeblks *freeblks;
8424 struct inodedep *inodedep;
8428 struct ufsmount *ump;
8431 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8433 flags = LK_EXCLUSIVE | flags;
8434 spare = freeblks->fb_chkcnt;
8437 * If we did not release the expected number of blocks we may have
8438 * to adjust the inode block count here. Only do so if it wasn't
8439 * a truncation to zero and the modrev still matches.
8441 if (spare && freeblks->fb_len != 0) {
8442 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8443 flags, &vp, FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP) != 0)
8446 if (ip->i_mode == 0) {
8448 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8449 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8450 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8452 * We must wait so this happens before the
8453 * journal is reclaimed.
8461 fs->fs_pendingblocks += spare;
8467 quotaadj(freeblks->fb_quota, ump, -spare);
8468 quotarele(freeblks->fb_quota);
8471 if (freeblks->fb_state & ONDEPLIST) {
8472 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8474 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8475 freeblks->fb_state &= ~ONDEPLIST;
8476 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8477 free_inodedep(inodedep);
8480 * All of the freeblock deps must be complete prior to this call
8481 * so it's now safe to complete earlier outstanding journal entries.
8483 handle_jwork(&freeblks->fb_jwork);
8484 WORKITEM_FREE(freeblks, D_FREEBLKS);
8490 * Release blocks associated with the freeblks and stored in the indirect
8491 * block dbn. If level is greater than SINGLE, the block is an indirect block
8492 * and recursive calls to indirtrunc must be used to cleanse other indirect
8495 * This handles partial and complete truncation of blocks. Partial is noted
8496 * with goingaway == 0. In this case the freework is completed after the
8497 * zero'd indirects are written to disk. For full truncation the freework
8498 * is completed after the block is freed.
8501 indir_trunc(freework, dbn, lbn)
8502 struct freework *freework;
8506 struct freework *nfreework;
8507 struct workhead wkhd;
8508 struct freeblks *freeblks;
8511 struct indirdep *indirdep;
8513 struct ufsmount *ump;
8515 ufs2_daddr_t nb, nnb, *bap2;
8516 ufs_lbn_t lbnadd, nlbn;
8518 int nblocks, ufs1fmt, freedblocks;
8519 int goingaway, freedeps, needj, level, cnt, i, error;
8521 freeblks = freework->fw_freeblks;
8522 mp = freeblks->fb_list.wk_mp;
8526 * Get buffer of block pointers to be freed. There are three cases:
8528 * 1) Partial truncate caches the indirdep pointer in the freework
8529 * which provides us a back copy to the save bp which holds the
8530 * pointers we want to clear. When this completes the zero
8531 * pointers are written to the real copy.
8532 * 2) The indirect is being completely truncated, cancel_indirdep()
8533 * eliminated the real copy and placed the indirdep on the saved
8534 * copy. The indirdep and buf are discarded when this completes.
8535 * 3) The indirect was not in memory, we read a copy off of the disk
8536 * using the devvp and drop and invalidate the buffer when we're
8541 if (freework->fw_indir != NULL) {
8543 indirdep = freework->fw_indir;
8544 bp = indirdep->ir_savebp;
8545 if (bp == NULL || bp->b_blkno != dbn)
8546 panic("indir_trunc: Bad saved buf %p blkno %jd",
8548 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8550 * The lock prevents the buf dep list from changing and
8551 * indirects on devvp should only ever have one dependency.
8553 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8554 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8555 panic("indir_trunc: Bad indirdep %p from buf %p",
8558 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn,
8559 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
8564 /* Protects against a race with complete_trunc_indir(). */
8565 freework->fw_state &= ~INPROGRESS;
8567 * If we have an indirdep we need to enforce the truncation order
8568 * and discard it when it is complete.
8571 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8572 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8574 * Add the complete truncate to the list on the
8575 * indirdep to enforce in-order processing.
8577 if (freework->fw_indir == NULL)
8578 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8584 * If we're goingaway, free the indirdep. Otherwise it will
8585 * linger until the write completes.
8588 KASSERT(indirdep->ir_savebp == bp,
8589 ("indir_trunc: losing ir_savebp %p",
8590 indirdep->ir_savebp));
8591 indirdep->ir_savebp = NULL;
8592 free_indirdep(indirdep);
8596 /* Initialize pointers depending on block size. */
8597 if (ump->um_fstype == UFS1) {
8598 bap1 = (ufs1_daddr_t *)bp->b_data;
8599 nb = bap1[freework->fw_off];
8603 bap2 = (ufs2_daddr_t *)bp->b_data;
8604 nb = bap2[freework->fw_off];
8608 level = lbn_level(lbn);
8609 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8610 lbnadd = lbn_offset(fs, level);
8611 nblocks = btodb(fs->fs_bsize);
8612 nfreework = freework;
8616 * Reclaim blocks. Traverses into nested indirect levels and
8617 * arranges for the current level to be freed when subordinates
8618 * are free when journaling.
8620 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8621 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8622 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8625 if (i != NINDIR(fs) - 1) {
8636 nlbn = (lbn + 1) - (i * lbnadd);
8638 nfreework = newfreework(ump, freeblks, freework,
8639 nlbn, nb, fs->fs_frag, 0, 0);
8642 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8644 struct freedep *freedep;
8647 * Attempt to aggregate freedep dependencies for
8648 * all blocks being released to the same CG.
8652 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8653 freedep = newfreedep(freework);
8654 WORKLIST_INSERT_UNLOCKED(&wkhd,
8659 "indir_trunc: ino %jd blkno %jd size %d",
8660 freeblks->fb_inum, nb, fs->fs_bsize);
8661 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8662 fs->fs_bsize, freeblks->fb_inum,
8663 freeblks->fb_vtype, &wkhd, key);
8666 ffs_blkrelease_finish(ump, key);
8668 bp->b_flags |= B_INVAL | B_NOCACHE;
8673 freedblocks = (nblocks * cnt);
8675 freedblocks += nblocks;
8676 freeblks_free(ump, freeblks, freedblocks);
8678 * If we are journaling set up the ref counts and offset so this
8679 * indirect can be completed when its children are free.
8683 freework->fw_off = i;
8684 freework->fw_ref += freedeps;
8685 freework->fw_ref -= NINDIR(fs) + 1;
8687 freeblks->fb_cgwait += freedeps;
8688 if (freework->fw_ref == 0)
8689 freework_freeblock(freework, SINGLETON_KEY);
8694 * If we're not journaling we can free the indirect now.
8696 dbn = dbtofsb(fs, dbn);
8698 "indir_trunc 2: ino %jd blkno %jd size %d",
8699 freeblks->fb_inum, dbn, fs->fs_bsize);
8700 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8701 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8702 /* Non SUJ softdep does single-threaded truncations. */
8703 if (freework->fw_blkno == dbn) {
8704 freework->fw_state |= ALLCOMPLETE;
8706 handle_written_freework(freework);
8713 * Cancel an allocindir when it is removed via truncation. When bp is not
8714 * NULL the indirect never appeared on disk and is scheduled to be freed
8715 * independently of the indir so we can more easily track journal work.
8718 cancel_allocindir(aip, bp, freeblks, trunc)
8719 struct allocindir *aip;
8721 struct freeblks *freeblks;
8724 struct indirdep *indirdep;
8725 struct freefrag *freefrag;
8726 struct newblk *newblk;
8728 newblk = (struct newblk *)aip;
8729 LIST_REMOVE(aip, ai_next);
8731 * We must eliminate the pointer in bp if it must be freed on its
8732 * own due to partial truncate or pending journal work.
8734 if (bp && (trunc || newblk->nb_jnewblk)) {
8736 * Clear the pointer and mark the aip to be freed
8737 * directly if it never existed on disk.
8739 aip->ai_state |= DELAYEDFREE;
8740 indirdep = aip->ai_indirdep;
8741 if (indirdep->ir_state & UFS1FMT)
8742 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8744 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8747 * When truncating the previous pointer will be freed via
8748 * savedbp. Eliminate the freefrag which would dup free.
8750 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8751 newblk->nb_freefrag = NULL;
8752 if (freefrag->ff_jdep)
8754 WK_JFREEFRAG(freefrag->ff_jdep));
8755 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8756 WORKITEM_FREE(freefrag, D_FREEFRAG);
8759 * If the journal hasn't been written the jnewblk must be passed
8760 * to the call to ffs_blkfree that reclaims the space. We accomplish
8761 * this by leaving the journal dependency on the newblk to be freed
8762 * when a freework is created in handle_workitem_freeblocks().
8764 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8765 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8769 * Create the mkdir dependencies for . and .. in a new directory. Link them
8770 * in to a newdirblk so any subsequent additions are tracked properly. The
8771 * caller is responsible for adding the mkdir1 dependency to the journal
8772 * and updating id_mkdiradd. This function returns with the per-filesystem
8775 static struct mkdir *
8776 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8780 struct buf *newdirbp;
8781 struct mkdir **mkdirp;
8783 struct newblk *newblk;
8784 struct pagedep *pagedep;
8785 struct inodedep *inodedep;
8786 struct newdirblk *newdirblk;
8787 struct mkdir *mkdir1, *mkdir2;
8788 struct worklist *wk;
8789 struct jaddref *jaddref;
8790 struct ufsmount *ump;
8793 mp = dap->da_list.wk_mp;
8795 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8797 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8798 LIST_INIT(&newdirblk->db_mkdir);
8799 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8800 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8801 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8802 mkdir1->md_diradd = dap;
8803 mkdir1->md_jaddref = NULL;
8804 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8805 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8806 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8807 mkdir2->md_diradd = dap;
8808 mkdir2->md_jaddref = NULL;
8809 if (MOUNTEDSUJ(mp) == 0) {
8810 mkdir1->md_state |= DEPCOMPLETE;
8811 mkdir2->md_state |= DEPCOMPLETE;
8814 * Dependency on "." and ".." being written to disk.
8816 mkdir1->md_buf = newdirbp;
8817 ACQUIRE_LOCK(VFSTOUFS(mp));
8818 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8820 * We must link the pagedep, allocdirect, and newdirblk for
8821 * the initial file page so the pointer to the new directory
8822 * is not written until the directory contents are live and
8823 * any subsequent additions are not marked live until the
8824 * block is reachable via the inode.
8826 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8827 panic("setup_newdir: lost pagedep");
8828 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8829 if (wk->wk_type == D_ALLOCDIRECT)
8832 panic("setup_newdir: lost allocdirect");
8833 if (pagedep->pd_state & NEWBLOCK)
8834 panic("setup_newdir: NEWBLOCK already set");
8835 newblk = WK_NEWBLK(wk);
8836 pagedep->pd_state |= NEWBLOCK;
8837 pagedep->pd_newdirblk = newdirblk;
8838 newdirblk->db_pagedep = pagedep;
8839 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8840 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8842 * Look up the inodedep for the parent directory so that we
8843 * can link mkdir2 into the pending dotdot jaddref or
8844 * the inode write if there is none. If the inode is
8845 * ALLCOMPLETE and no jaddref is present all dependencies have
8846 * been satisfied and mkdir2 can be freed.
8848 inodedep_lookup(mp, dinum, 0, &inodedep);
8849 if (MOUNTEDSUJ(mp)) {
8850 if (inodedep == NULL)
8851 panic("setup_newdir: Lost parent.");
8852 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8854 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8855 (jaddref->ja_state & MKDIR_PARENT),
8856 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8857 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8858 mkdir2->md_jaddref = jaddref;
8859 jaddref->ja_mkdir = mkdir2;
8860 } else if (inodedep == NULL ||
8861 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8862 dap->da_state &= ~MKDIR_PARENT;
8863 WORKITEM_FREE(mkdir2, D_MKDIR);
8866 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8867 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8875 * Directory entry addition dependencies.
8877 * When adding a new directory entry, the inode (with its incremented link
8878 * count) must be written to disk before the directory entry's pointer to it.
8879 * Also, if the inode is newly allocated, the corresponding freemap must be
8880 * updated (on disk) before the directory entry's pointer. These requirements
8881 * are met via undo/redo on the directory entry's pointer, which consists
8882 * simply of the inode number.
8884 * As directory entries are added and deleted, the free space within a
8885 * directory block can become fragmented. The ufs filesystem will compact
8886 * a fragmented directory block to make space for a new entry. When this
8887 * occurs, the offsets of previously added entries change. Any "diradd"
8888 * dependency structures corresponding to these entries must be updated with
8893 * This routine is called after the in-memory inode's link
8894 * count has been incremented, but before the directory entry's
8895 * pointer to the inode has been set.
8898 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8899 struct buf *bp; /* buffer containing directory block */
8900 struct inode *dp; /* inode for directory */
8901 off_t diroffset; /* offset of new entry in directory */
8902 ino_t newinum; /* inode referenced by new directory entry */
8903 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
8904 int isnewblk; /* entry is in a newly allocated block */
8906 int offset; /* offset of new entry within directory block */
8907 ufs_lbn_t lbn; /* block in directory containing new entry */
8910 struct newblk *newblk;
8911 struct pagedep *pagedep;
8912 struct inodedep *inodedep;
8913 struct newdirblk *newdirblk;
8914 struct mkdir *mkdir1, *mkdir2;
8915 struct jaddref *jaddref;
8916 struct ufsmount *ump;
8922 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8923 ("softdep_setup_directory_add called on non-softdep filesystem"));
8925 * Whiteouts have no dependencies.
8927 if (newinum == UFS_WINO) {
8928 if (newdirbp != NULL)
8933 mkdir1 = mkdir2 = NULL;
8935 lbn = lblkno(fs, diroffset);
8936 offset = blkoff(fs, diroffset);
8937 dap = malloc(sizeof(struct diradd), M_DIRADD,
8938 M_SOFTDEP_FLAGS|M_ZERO);
8939 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8940 dap->da_offset = offset;
8941 dap->da_newinum = newinum;
8942 dap->da_state = ATTACHED;
8943 LIST_INIT(&dap->da_jwork);
8944 isindir = bp->b_lblkno >= UFS_NDADDR;
8947 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8948 newdirblk = malloc(sizeof(struct newdirblk),
8949 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8950 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8951 LIST_INIT(&newdirblk->db_mkdir);
8954 * If we're creating a new directory setup the dependencies and set
8955 * the dap state to wait for them. Otherwise it's COMPLETE and
8958 if (newdirbp == NULL) {
8959 dap->da_state |= DEPCOMPLETE;
8962 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8963 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8967 * Link into parent directory pagedep to await its being written.
8969 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8971 if (diradd_lookup(pagedep, offset) != NULL)
8972 panic("softdep_setup_directory_add: %p already at off %d\n",
8973 diradd_lookup(pagedep, offset), offset);
8975 dap->da_pagedep = pagedep;
8976 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8978 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8980 * If we're journaling, link the diradd into the jaddref so it
8981 * may be completed after the journal entry is written. Otherwise,
8982 * link the diradd into its inodedep. If the inode is not yet
8983 * written place it on the bufwait list, otherwise do the post-inode
8984 * write processing to put it on the id_pendinghd list.
8986 if (MOUNTEDSUJ(mp)) {
8987 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8989 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8990 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8991 jaddref->ja_diroff = diroffset;
8992 jaddref->ja_diradd = dap;
8993 add_to_journal(&jaddref->ja_list);
8994 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8995 diradd_inode_written(dap, inodedep);
8997 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8999 * Add the journal entries for . and .. links now that the primary
9002 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
9003 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
9004 inoreflst, if_deps);
9005 KASSERT(jaddref != NULL &&
9006 jaddref->ja_ino == jaddref->ja_parent &&
9007 (jaddref->ja_state & MKDIR_BODY),
9008 ("softdep_setup_directory_add: bad dot jaddref %p",
9010 mkdir1->md_jaddref = jaddref;
9011 jaddref->ja_mkdir = mkdir1;
9013 * It is important that the dotdot journal entry
9014 * is added prior to the dot entry since dot writes
9015 * both the dot and dotdot links. These both must
9016 * be added after the primary link for the journal
9017 * to remain consistent.
9019 add_to_journal(&mkdir2->md_jaddref->ja_list);
9020 add_to_journal(&jaddref->ja_list);
9023 * If we are adding a new directory remember this diradd so that if
9024 * we rename it we can keep the dot and dotdot dependencies. If
9025 * we are adding a new name for an inode that has a mkdiradd we
9026 * must be in rename and we have to move the dot and dotdot
9027 * dependencies to this new name. The old name is being orphaned
9030 if (mkdir1 != NULL) {
9031 if (inodedep->id_mkdiradd != NULL)
9032 panic("softdep_setup_directory_add: Existing mkdir");
9033 inodedep->id_mkdiradd = dap;
9034 } else if (inodedep->id_mkdiradd)
9035 merge_diradd(inodedep, dap);
9036 if (newdirblk != NULL) {
9038 * There is nothing to do if we are already tracking
9041 if ((pagedep->pd_state & NEWBLOCK) != 0) {
9042 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
9046 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
9048 panic("softdep_setup_directory_add: lost entry");
9049 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
9050 pagedep->pd_state |= NEWBLOCK;
9051 pagedep->pd_newdirblk = newdirblk;
9052 newdirblk->db_pagedep = pagedep;
9055 * If we extended into an indirect signal direnter to sync.
9066 * This procedure is called to change the offset of a directory
9067 * entry when compacting a directory block which must be owned
9068 * exclusively by the caller. Note that the actual entry movement
9069 * must be done in this procedure to ensure that no I/O completions
9070 * occur while the move is in progress.
9073 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
9074 struct buf *bp; /* Buffer holding directory block. */
9075 struct inode *dp; /* inode for directory */
9076 caddr_t base; /* address of dp->i_offset */
9077 caddr_t oldloc; /* address of old directory location */
9078 caddr_t newloc; /* address of new directory location */
9079 int entrysize; /* size of directory entry */
9081 int offset, oldoffset, newoffset;
9082 struct pagedep *pagedep;
9083 struct jmvref *jmvref;
9087 struct ufsmount *ump;
9093 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9094 ("softdep_change_directoryentry_offset called on "
9095 "non-softdep filesystem"));
9096 de = (struct direct *)oldloc;
9100 * Moves are always journaled as it would be too complex to
9101 * determine if any affected adds or removes are present in the
9104 if (MOUNTEDSUJ(mp)) {
9106 jmvref = newjmvref(dp, de->d_ino,
9107 I_OFFSET(dp) + (oldloc - base),
9108 I_OFFSET(dp) + (newloc - base));
9110 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9111 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9112 oldoffset = offset + (oldloc - base);
9113 newoffset = offset + (newloc - base);
9115 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
9117 dap = diradd_lookup(pagedep, oldoffset);
9119 dap->da_offset = newoffset;
9120 newoffset = DIRADDHASH(newoffset);
9121 oldoffset = DIRADDHASH(oldoffset);
9122 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
9123 newoffset != oldoffset) {
9124 LIST_REMOVE(dap, da_pdlist);
9125 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
9131 jmvref->jm_pagedep = pagedep;
9132 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
9133 add_to_journal(&jmvref->jm_list);
9135 bcopy(oldloc, newloc, entrysize);
9140 * Move the mkdir dependencies and journal work from one diradd to another
9141 * when renaming a directory. The new name must depend on the mkdir deps
9142 * completing as the old name did. Directories can only have one valid link
9143 * at a time so one must be canonical.
9146 merge_diradd(inodedep, newdap)
9147 struct inodedep *inodedep;
9148 struct diradd *newdap;
9150 struct diradd *olddap;
9151 struct mkdir *mkdir, *nextmd;
9152 struct ufsmount *ump;
9155 olddap = inodedep->id_mkdiradd;
9156 inodedep->id_mkdiradd = newdap;
9157 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9158 newdap->da_state &= ~DEPCOMPLETE;
9159 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9160 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9162 nextmd = LIST_NEXT(mkdir, md_mkdirs);
9163 if (mkdir->md_diradd != olddap)
9165 mkdir->md_diradd = newdap;
9166 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
9167 newdap->da_state |= state;
9168 olddap->da_state &= ~state;
9169 if ((olddap->da_state &
9170 (MKDIR_PARENT | MKDIR_BODY)) == 0)
9173 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9174 panic("merge_diradd: unfound ref");
9177 * Any mkdir related journal items are not safe to be freed until
9178 * the new name is stable.
9180 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
9181 olddap->da_state |= DEPCOMPLETE;
9182 complete_diradd(olddap);
9186 * Move the diradd to the pending list when all diradd dependencies are
9190 complete_diradd(dap)
9193 struct pagedep *pagedep;
9195 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
9196 if (dap->da_state & DIRCHG)
9197 pagedep = dap->da_previous->dm_pagedep;
9199 pagedep = dap->da_pagedep;
9200 LIST_REMOVE(dap, da_pdlist);
9201 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9206 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
9207 * add entries and conditonally journal the remove.
9210 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
9212 struct dirrem *dirrem;
9213 struct jremref *jremref;
9214 struct jremref *dotremref;
9215 struct jremref *dotdotremref;
9217 struct inodedep *inodedep;
9218 struct jaddref *jaddref;
9219 struct inoref *inoref;
9220 struct ufsmount *ump;
9221 struct mkdir *mkdir;
9224 * If no remove references were allocated we're on a non-journaled
9225 * filesystem and can skip the cancel step.
9227 if (jremref == NULL) {
9228 free_diradd(dap, NULL);
9232 * Cancel the primary name an free it if it does not require
9235 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
9236 0, &inodedep) != 0) {
9237 /* Abort the addref that reference this diradd. */
9238 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
9239 if (inoref->if_list.wk_type != D_JADDREF)
9241 jaddref = (struct jaddref *)inoref;
9242 if (jaddref->ja_diradd != dap)
9244 if (cancel_jaddref(jaddref, inodedep,
9245 &dirrem->dm_jwork) == 0) {
9246 free_jremref(jremref);
9253 * Cancel subordinate names and free them if they do not require
9256 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9257 ump = VFSTOUFS(dap->da_list.wk_mp);
9258 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
9259 if (mkdir->md_diradd != dap)
9261 if ((jaddref = mkdir->md_jaddref) == NULL)
9263 mkdir->md_jaddref = NULL;
9264 if (mkdir->md_state & MKDIR_PARENT) {
9265 if (cancel_jaddref(jaddref, NULL,
9266 &dirrem->dm_jwork) == 0) {
9267 free_jremref(dotdotremref);
9268 dotdotremref = NULL;
9271 if (cancel_jaddref(jaddref, inodedep,
9272 &dirrem->dm_jwork) == 0) {
9273 free_jremref(dotremref);
9281 journal_jremref(dirrem, jremref, inodedep);
9283 journal_jremref(dirrem, dotremref, inodedep);
9285 journal_jremref(dirrem, dotdotremref, NULL);
9286 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
9287 free_diradd(dap, &dirrem->dm_jwork);
9291 * Free a diradd dependency structure.
9294 free_diradd(dap, wkhd)
9296 struct workhead *wkhd;
9298 struct dirrem *dirrem;
9299 struct pagedep *pagedep;
9300 struct inodedep *inodedep;
9301 struct mkdir *mkdir, *nextmd;
9302 struct ufsmount *ump;
9304 ump = VFSTOUFS(dap->da_list.wk_mp);
9306 LIST_REMOVE(dap, da_pdlist);
9307 if (dap->da_state & ONWORKLIST)
9308 WORKLIST_REMOVE(&dap->da_list);
9309 if ((dap->da_state & DIRCHG) == 0) {
9310 pagedep = dap->da_pagedep;
9312 dirrem = dap->da_previous;
9313 pagedep = dirrem->dm_pagedep;
9314 dirrem->dm_dirinum = pagedep->pd_ino;
9315 dirrem->dm_state |= COMPLETE;
9316 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9317 add_to_worklist(&dirrem->dm_list, 0);
9319 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
9321 if (inodedep->id_mkdiradd == dap)
9322 inodedep->id_mkdiradd = NULL;
9323 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
9324 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9326 nextmd = LIST_NEXT(mkdir, md_mkdirs);
9327 if (mkdir->md_diradd != dap)
9330 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
9331 LIST_REMOVE(mkdir, md_mkdirs);
9332 if (mkdir->md_state & ONWORKLIST)
9333 WORKLIST_REMOVE(&mkdir->md_list);
9334 if (mkdir->md_jaddref != NULL)
9335 panic("free_diradd: Unexpected jaddref");
9336 WORKITEM_FREE(mkdir, D_MKDIR);
9337 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
9340 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9341 panic("free_diradd: unfound ref");
9344 free_inodedep(inodedep);
9346 * Free any journal segments waiting for the directory write.
9348 handle_jwork(&dap->da_jwork);
9349 WORKITEM_FREE(dap, D_DIRADD);
9353 * Directory entry removal dependencies.
9355 * When removing a directory entry, the entry's inode pointer must be
9356 * zero'ed on disk before the corresponding inode's link count is decremented
9357 * (possibly freeing the inode for re-use). This dependency is handled by
9358 * updating the directory entry but delaying the inode count reduction until
9359 * after the directory block has been written to disk. After this point, the
9360 * inode count can be decremented whenever it is convenient.
9364 * This routine should be called immediately after removing
9365 * a directory entry. The inode's link count should not be
9366 * decremented by the calling procedure -- the soft updates
9367 * code will do this task when it is safe.
9370 softdep_setup_remove(bp, dp, ip, isrmdir)
9371 struct buf *bp; /* buffer containing directory block */
9372 struct inode *dp; /* inode for the directory being modified */
9373 struct inode *ip; /* inode for directory entry being removed */
9374 int isrmdir; /* indicates if doing RMDIR */
9376 struct dirrem *dirrem, *prevdirrem;
9377 struct inodedep *inodedep;
9378 struct ufsmount *ump;
9382 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9383 ("softdep_setup_remove called on non-softdep filesystem"));
9385 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
9386 * newdirrem() to setup the full directory remove which requires
9389 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9391 * Add the dirrem to the inodedep's pending remove list for quick
9394 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9395 panic("softdep_setup_remove: Lost inodedep.");
9396 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9397 dirrem->dm_state |= ONDEPLIST;
9398 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9401 * If the COMPLETE flag is clear, then there were no active
9402 * entries and we want to roll back to a zeroed entry until
9403 * the new inode is committed to disk. If the COMPLETE flag is
9404 * set then we have deleted an entry that never made it to
9405 * disk. If the entry we deleted resulted from a name change,
9406 * then the old name still resides on disk. We cannot delete
9407 * its inode (returned to us in prevdirrem) until the zeroed
9408 * directory entry gets to disk. The new inode has never been
9409 * referenced on the disk, so can be deleted immediately.
9411 if ((dirrem->dm_state & COMPLETE) == 0) {
9412 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9416 if (prevdirrem != NULL)
9417 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9418 prevdirrem, dm_next);
9419 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9420 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9423 handle_workitem_remove(dirrem, 0);
9428 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9429 * pd_pendinghd list of a pagedep.
9431 static struct diradd *
9432 diradd_lookup(pagedep, offset)
9433 struct pagedep *pagedep;
9438 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9439 if (dap->da_offset == offset)
9441 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9442 if (dap->da_offset == offset)
9448 * Search for a .. diradd dependency in a directory that is being removed.
9449 * If the directory was renamed to a new parent we have a diradd rather
9450 * than a mkdir for the .. entry. We need to cancel it now before
9451 * it is found in truncate().
9453 static struct jremref *
9454 cancel_diradd_dotdot(ip, dirrem, jremref)
9456 struct dirrem *dirrem;
9457 struct jremref *jremref;
9459 struct pagedep *pagedep;
9461 struct worklist *wk;
9463 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9465 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9468 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9470 * Mark any journal work as belonging to the parent so it is freed
9471 * with the .. reference.
9473 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9474 wk->wk_state |= MKDIR_PARENT;
9479 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9480 * replace it with a dirrem/diradd pair as a result of re-parenting a
9481 * directory. This ensures that we don't simultaneously have a mkdir and
9482 * a diradd for the same .. entry.
9484 static struct jremref *
9485 cancel_mkdir_dotdot(ip, dirrem, jremref)
9487 struct dirrem *dirrem;
9488 struct jremref *jremref;
9490 struct inodedep *inodedep;
9491 struct jaddref *jaddref;
9492 struct ufsmount *ump;
9493 struct mkdir *mkdir;
9498 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9500 dap = inodedep->id_mkdiradd;
9501 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9503 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9504 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9505 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9506 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9509 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9510 if ((jaddref = mkdir->md_jaddref) != NULL) {
9511 mkdir->md_jaddref = NULL;
9512 jaddref->ja_state &= ~MKDIR_PARENT;
9513 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9514 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9515 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9516 journal_jremref(dirrem, jremref, inodedep);
9520 if (mkdir->md_state & ONWORKLIST)
9521 WORKLIST_REMOVE(&mkdir->md_list);
9522 mkdir->md_state |= ALLCOMPLETE;
9523 complete_mkdir(mkdir);
9528 journal_jremref(dirrem, jremref, inodedep)
9529 struct dirrem *dirrem;
9530 struct jremref *jremref;
9531 struct inodedep *inodedep;
9534 if (inodedep == NULL)
9535 if (inodedep_lookup(jremref->jr_list.wk_mp,
9536 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9537 panic("journal_jremref: Lost inodedep");
9538 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9539 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9540 add_to_journal(&jremref->jr_list);
9544 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9545 struct dirrem *dirrem;
9546 struct jremref *jremref;
9547 struct jremref *dotremref;
9548 struct jremref *dotdotremref;
9550 struct inodedep *inodedep;
9552 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9554 panic("dirrem_journal: Lost inodedep");
9555 journal_jremref(dirrem, jremref, inodedep);
9557 journal_jremref(dirrem, dotremref, inodedep);
9559 journal_jremref(dirrem, dotdotremref, NULL);
9563 * Allocate a new dirrem if appropriate and return it along with
9564 * its associated pagedep. Called without a lock, returns with lock.
9566 static struct dirrem *
9567 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9568 struct buf *bp; /* buffer containing directory block */
9569 struct inode *dp; /* inode for the directory being modified */
9570 struct inode *ip; /* inode for directory entry being removed */
9571 int isrmdir; /* indicates if doing RMDIR */
9572 struct dirrem **prevdirremp; /* previously referenced inode, if any */
9577 struct dirrem *dirrem;
9578 struct pagedep *pagedep;
9579 struct jremref *jremref;
9580 struct jremref *dotremref;
9581 struct jremref *dotdotremref;
9583 struct ufsmount *ump;
9586 * Whiteouts have no deletion dependencies.
9589 panic("newdirrem: whiteout");
9594 * If the system is over its limit and our filesystem is
9595 * responsible for more than our share of that usage and
9596 * we are not a snapshot, request some inodedep cleanup.
9597 * Limiting the number of dirrem structures will also limit
9598 * the number of freefile and freeblks structures.
9601 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9602 schedule_cleanup(UFSTOVFS(ump));
9605 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9607 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9608 LIST_INIT(&dirrem->dm_jremrefhd);
9609 LIST_INIT(&dirrem->dm_jwork);
9610 dirrem->dm_state = isrmdir ? RMDIR : 0;
9611 dirrem->dm_oldinum = ip->i_number;
9612 *prevdirremp = NULL;
9614 * Allocate remove reference structures to track journal write
9615 * dependencies. We will always have one for the link and
9616 * when doing directories we will always have one more for dot.
9617 * When renaming a directory we skip the dotdot link change so
9618 * this is not needed.
9620 jremref = dotremref = dotdotremref = NULL;
9621 if (DOINGSUJ(dvp)) {
9623 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9624 ip->i_effnlink + 2);
9625 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9626 ip->i_effnlink + 1);
9627 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9628 dp->i_effnlink + 1);
9629 dotdotremref->jr_state |= MKDIR_PARENT;
9631 jremref = newjremref(dirrem, dp, ip, I_OFFSET(dp),
9632 ip->i_effnlink + 1);
9635 lbn = lblkno(ump->um_fs, I_OFFSET(dp));
9636 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9637 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9639 dirrem->dm_pagedep = pagedep;
9640 dirrem->dm_offset = offset;
9642 * If we're renaming a .. link to a new directory, cancel any
9643 * existing MKDIR_PARENT mkdir. If it has already been canceled
9644 * the jremref is preserved for any potential diradd in this
9645 * location. This can not coincide with a rmdir.
9647 if (I_OFFSET(dp) == DOTDOT_OFFSET) {
9649 panic("newdirrem: .. directory change during remove?");
9650 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9653 * If we're removing a directory search for the .. dependency now and
9654 * cancel it. Any pending journal work will be added to the dirrem
9655 * to be completed when the workitem remove completes.
9658 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9660 * Check for a diradd dependency for the same directory entry.
9661 * If present, then both dependencies become obsolete and can
9664 dap = diradd_lookup(pagedep, offset);
9667 * Link the jremref structures into the dirrem so they are
9668 * written prior to the pagedep.
9671 dirrem_journal(dirrem, jremref, dotremref,
9676 * Must be ATTACHED at this point.
9678 if ((dap->da_state & ATTACHED) == 0)
9679 panic("newdirrem: not ATTACHED");
9680 if (dap->da_newinum != ip->i_number)
9681 panic("newdirrem: inum %ju should be %ju",
9682 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9684 * If we are deleting a changed name that never made it to disk,
9685 * then return the dirrem describing the previous inode (which
9686 * represents the inode currently referenced from this entry on disk).
9688 if ((dap->da_state & DIRCHG) != 0) {
9689 *prevdirremp = dap->da_previous;
9690 dap->da_state &= ~DIRCHG;
9691 dap->da_pagedep = pagedep;
9694 * We are deleting an entry that never made it to disk.
9695 * Mark it COMPLETE so we can delete its inode immediately.
9697 dirrem->dm_state |= COMPLETE;
9698 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9701 struct worklist *wk;
9703 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9704 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9705 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9713 * Directory entry change dependencies.
9715 * Changing an existing directory entry requires that an add operation
9716 * be completed first followed by a deletion. The semantics for the addition
9717 * are identical to the description of adding a new entry above except
9718 * that the rollback is to the old inode number rather than zero. Once
9719 * the addition dependency is completed, the removal is done as described
9720 * in the removal routine above.
9724 * This routine should be called immediately after changing
9725 * a directory entry. The inode's link count should not be
9726 * decremented by the calling procedure -- the soft updates
9727 * code will perform this task when it is safe.
9730 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9731 struct buf *bp; /* buffer containing directory block */
9732 struct inode *dp; /* inode for the directory being modified */
9733 struct inode *ip; /* inode for directory entry being removed */
9734 ino_t newinum; /* new inode number for changed entry */
9735 int isrmdir; /* indicates if doing RMDIR */
9738 struct diradd *dap = NULL;
9739 struct dirrem *dirrem, *prevdirrem;
9740 struct pagedep *pagedep;
9741 struct inodedep *inodedep;
9742 struct jaddref *jaddref;
9744 struct ufsmount *ump;
9748 offset = blkoff(ump->um_fs, I_OFFSET(dp));
9749 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9750 ("softdep_setup_directory_change called on non-softdep filesystem"));
9753 * Whiteouts do not need diradd dependencies.
9755 if (newinum != UFS_WINO) {
9756 dap = malloc(sizeof(struct diradd),
9757 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9758 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9759 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9760 dap->da_offset = offset;
9761 dap->da_newinum = newinum;
9762 LIST_INIT(&dap->da_jwork);
9766 * Allocate a new dirrem and ACQUIRE_LOCK.
9768 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9769 pagedep = dirrem->dm_pagedep;
9771 * The possible values for isrmdir:
9772 * 0 - non-directory file rename
9773 * 1 - directory rename within same directory
9774 * inum - directory rename to new directory of given inode number
9775 * When renaming to a new directory, we are both deleting and
9776 * creating a new directory entry, so the link count on the new
9777 * directory should not change. Thus we do not need the followup
9778 * dirrem which is usually done in handle_workitem_remove. We set
9779 * the DIRCHG flag to tell handle_workitem_remove to skip the
9783 dirrem->dm_state |= DIRCHG;
9786 * Whiteouts have no additional dependencies,
9787 * so just put the dirrem on the correct list.
9789 if (newinum == UFS_WINO) {
9790 if ((dirrem->dm_state & COMPLETE) == 0) {
9791 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9794 dirrem->dm_dirinum = pagedep->pd_ino;
9795 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9796 add_to_worklist(&dirrem->dm_list, 0);
9802 * Add the dirrem to the inodedep's pending remove list for quick
9803 * discovery later. A valid nlinkdelta ensures that this lookup
9806 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9807 panic("softdep_setup_directory_change: Lost inodedep.");
9808 dirrem->dm_state |= ONDEPLIST;
9809 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9812 * If the COMPLETE flag is clear, then there were no active
9813 * entries and we want to roll back to the previous inode until
9814 * the new inode is committed to disk. If the COMPLETE flag is
9815 * set, then we have deleted an entry that never made it to disk.
9816 * If the entry we deleted resulted from a name change, then the old
9817 * inode reference still resides on disk. Any rollback that we do
9818 * needs to be to that old inode (returned to us in prevdirrem). If
9819 * the entry we deleted resulted from a create, then there is
9820 * no entry on the disk, so we want to roll back to zero rather
9821 * than the uncommitted inode. In either of the COMPLETE cases we
9822 * want to immediately free the unwritten and unreferenced inode.
9824 if ((dirrem->dm_state & COMPLETE) == 0) {
9825 dap->da_previous = dirrem;
9827 if (prevdirrem != NULL) {
9828 dap->da_previous = prevdirrem;
9830 dap->da_state &= ~DIRCHG;
9831 dap->da_pagedep = pagedep;
9833 dirrem->dm_dirinum = pagedep->pd_ino;
9834 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9835 add_to_worklist(&dirrem->dm_list, 0);
9838 * Lookup the jaddref for this journal entry. We must finish
9839 * initializing it and make the diradd write dependent on it.
9840 * If we're not journaling, put it on the id_bufwait list if the
9841 * inode is not yet written. If it is written, do the post-inode
9842 * write processing to put it on the id_pendinghd list.
9844 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9845 if (MOUNTEDSUJ(mp)) {
9846 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9848 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9849 ("softdep_setup_directory_change: bad jaddref %p",
9851 jaddref->ja_diroff = I_OFFSET(dp);
9852 jaddref->ja_diradd = dap;
9853 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9855 add_to_journal(&jaddref->ja_list);
9856 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9857 dap->da_state |= COMPLETE;
9858 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9859 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9861 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9863 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9866 * If we're making a new name for a directory that has not been
9867 * committed when need to move the dot and dotdot references to
9870 if (inodedep->id_mkdiradd && I_OFFSET(dp) != DOTDOT_OFFSET)
9871 merge_diradd(inodedep, dap);
9876 * Called whenever the link count on an inode is changed.
9877 * It creates an inode dependency so that the new reference(s)
9878 * to the inode cannot be committed to disk until the updated
9879 * inode has been written.
9882 softdep_change_linkcnt(ip)
9883 struct inode *ip; /* the inode with the increased link count */
9885 struct inodedep *inodedep;
9886 struct ufsmount *ump;
9889 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9890 ("softdep_change_linkcnt called on non-softdep filesystem"));
9892 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9893 if (ip->i_nlink < ip->i_effnlink)
9894 panic("softdep_change_linkcnt: bad delta");
9895 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9900 * Attach a sbdep dependency to the superblock buf so that we can keep
9901 * track of the head of the linked list of referenced but unlinked inodes.
9904 softdep_setup_sbupdate(ump, fs, bp)
9905 struct ufsmount *ump;
9909 struct sbdep *sbdep;
9910 struct worklist *wk;
9912 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9913 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9914 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9915 if (wk->wk_type == D_SBDEP)
9919 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9920 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9922 sbdep->sb_ump = ump;
9924 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9929 * Return the first unlinked inodedep which is ready to be the head of the
9930 * list. The inodedep and all those after it must have valid next pointers.
9932 static struct inodedep *
9933 first_unlinked_inodedep(ump)
9934 struct ufsmount *ump;
9936 struct inodedep *inodedep;
9937 struct inodedep *idp;
9940 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9941 inodedep; inodedep = idp) {
9942 if ((inodedep->id_state & UNLINKNEXT) == 0)
9944 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9945 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9947 if ((inodedep->id_state & UNLINKPREV) == 0)
9954 * Set the sujfree unlinked head pointer prior to writing a superblock.
9957 initiate_write_sbdep(sbdep)
9958 struct sbdep *sbdep;
9960 struct inodedep *inodedep;
9964 bpfs = sbdep->sb_fs;
9965 fs = sbdep->sb_ump->um_fs;
9966 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9968 fs->fs_sujfree = inodedep->id_ino;
9969 inodedep->id_state |= UNLINKPREV;
9972 bpfs->fs_sujfree = fs->fs_sujfree;
9974 * Because we have made changes to the superblock, we need to
9975 * recompute its check-hash.
9977 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9981 * After a superblock is written determine whether it must be written again
9982 * due to a changing unlinked list head.
9985 handle_written_sbdep(sbdep, bp)
9986 struct sbdep *sbdep;
9989 struct inodedep *inodedep;
9992 LOCK_OWNED(sbdep->sb_ump);
9995 * If the superblock doesn't match the in-memory list start over.
9997 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9998 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9999 (inodedep == NULL && fs->fs_sujfree != 0)) {
10003 WORKITEM_FREE(sbdep, D_SBDEP);
10004 if (fs->fs_sujfree == 0)
10007 * Now that we have a record of this inode in stable store allow it
10008 * to be written to free up pending work. Inodes may see a lot of
10009 * write activity after they are unlinked which we must not hold up.
10011 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
10012 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
10013 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
10014 inodedep, inodedep->id_state);
10015 if (inodedep->id_state & UNLINKONLIST)
10017 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
10024 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
10027 unlinked_inodedep(mp, inodedep)
10029 struct inodedep *inodedep;
10031 struct ufsmount *ump;
10033 ump = VFSTOUFS(mp);
10035 if (MOUNTEDSUJ(mp) == 0)
10037 ump->um_fs->fs_fmod = 1;
10038 if (inodedep->id_state & UNLINKED)
10039 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
10040 inodedep->id_state |= UNLINKED;
10041 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
10045 * Remove an inodedep from the unlinked inodedep list. This may require
10046 * disk writes if the inode has made it that far.
10049 clear_unlinked_inodedep(inodedep)
10050 struct inodedep *inodedep;
10052 struct ufs2_dinode *dip;
10053 struct ufsmount *ump;
10054 struct inodedep *idp;
10055 struct inodedep *idn;
10056 struct fs *fs, *bpfs;
10064 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10066 ino = inodedep->id_ino;
10070 KASSERT((inodedep->id_state & UNLINKED) != 0,
10071 ("clear_unlinked_inodedep: inodedep %p not unlinked",
10074 * If nothing has yet been written simply remove us from
10075 * the in memory list and return. This is the most common
10076 * case where handle_workitem_remove() loses the final
10079 if ((inodedep->id_state & UNLINKLINKS) == 0)
10082 * If we have a NEXT pointer and no PREV pointer we can simply
10083 * clear NEXT's PREV and remove ourselves from the list. Be
10084 * careful not to clear PREV if the superblock points at
10087 idn = TAILQ_NEXT(inodedep, id_unlinked);
10088 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
10089 if (idn && fs->fs_sujfree != idn->id_ino)
10090 idn->id_state &= ~UNLINKPREV;
10094 * Here we have an inodedep which is actually linked into
10095 * the list. We must remove it by forcing a write to the
10096 * link before us, whether it be the superblock or an inode.
10097 * Unfortunately the list may change while we're waiting
10098 * on the buf lock for either resource so we must loop until
10099 * we lock the right one. If both the superblock and an
10100 * inode point to this inode we must clear the inode first
10101 * followed by the superblock.
10103 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
10105 if (idp && (idp->id_state & UNLINKNEXT))
10106 pino = idp->id_ino;
10109 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
10110 (int)fs->fs_sbsize, 0, 0, 0);
10112 dbn = fsbtodb(fs, ino_to_fsba(fs, pino));
10113 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn,
10114 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL,
10120 /* If the list has changed restart the loop. */
10121 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
10123 if (idp && (idp->id_state & UNLINKNEXT))
10124 nino = idp->id_ino;
10125 if (nino != pino ||
10126 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
10133 idn = TAILQ_NEXT(inodedep, id_unlinked);
10135 nino = idn->id_ino;
10137 * Remove us from the in memory list. After this we cannot
10138 * access the inodedep.
10140 KASSERT((inodedep->id_state & UNLINKED) != 0,
10141 ("clear_unlinked_inodedep: inodedep %p not unlinked",
10143 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
10144 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
10147 * The predecessor's next pointer is manually updated here
10148 * so that the NEXT flag is never cleared for an element
10149 * that is in the list.
10152 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
10153 bpfs = (struct fs *)bp->b_data;
10154 ffs_oldfscompat_write(bpfs, ump);
10155 softdep_setup_sbupdate(ump, bpfs, bp);
10157 * Because we may have made changes to the superblock,
10158 * we need to recompute its check-hash.
10160 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
10161 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
10162 ((struct ufs1_dinode *)bp->b_data +
10163 ino_to_fsbo(fs, pino))->di_freelink = nino;
10165 dip = (struct ufs2_dinode *)bp->b_data +
10166 ino_to_fsbo(fs, pino);
10167 dip->di_freelink = nino;
10168 ffs_update_dinode_ckhash(fs, dip);
10171 * If the bwrite fails we have no recourse to recover. The
10172 * filesystem is corrupted already.
10177 * If the superblock pointer still needs to be cleared force
10180 if (fs->fs_sujfree == ino) {
10182 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
10183 (int)fs->fs_sbsize, 0, 0, 0);
10184 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
10185 bpfs = (struct fs *)bp->b_data;
10186 ffs_oldfscompat_write(bpfs, ump);
10187 softdep_setup_sbupdate(ump, bpfs, bp);
10189 * Because we may have made changes to the superblock,
10190 * we need to recompute its check-hash.
10192 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
10197 if (fs->fs_sujfree != ino)
10199 panic("clear_unlinked_inodedep: Failed to clear free head");
10201 if (inodedep->id_ino == fs->fs_sujfree)
10202 panic("clear_unlinked_inodedep: Freeing head of free list");
10203 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
10204 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
10209 * This workitem decrements the inode's link count.
10210 * If the link count reaches zero, the file is removed.
10213 handle_workitem_remove(dirrem, flags)
10214 struct dirrem *dirrem;
10217 struct inodedep *inodedep;
10218 struct workhead dotdotwk;
10219 struct worklist *wk;
10220 struct ufsmount *ump;
10226 if (dirrem->dm_state & ONWORKLIST)
10227 panic("handle_workitem_remove: dirrem %p still on worklist",
10229 oldinum = dirrem->dm_oldinum;
10230 mp = dirrem->dm_list.wk_mp;
10231 ump = VFSTOUFS(mp);
10232 flags |= LK_EXCLUSIVE;
10233 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ |
10234 FFSV_FORCEINODEDEP) != 0)
10237 MPASS(ip->i_mode != 0);
10239 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
10240 panic("handle_workitem_remove: lost inodedep");
10241 if (dirrem->dm_state & ONDEPLIST)
10242 LIST_REMOVE(dirrem, dm_inonext);
10243 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
10244 ("handle_workitem_remove: Journal entries not written."));
10247 * Move all dependencies waiting on the remove to complete
10248 * from the dirrem to the inode inowait list to be completed
10249 * after the inode has been updated and written to disk.
10251 * Any marked MKDIR_PARENT are saved to be completed when the
10252 * dotdot ref is removed unless DIRCHG is specified. For
10253 * directory change operations there will be no further
10254 * directory writes and the jsegdeps need to be moved along
10255 * with the rest to be completed when the inode is free or
10256 * stable in the inode free list.
10258 LIST_INIT(&dotdotwk);
10259 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
10260 WORKLIST_REMOVE(wk);
10261 if ((dirrem->dm_state & DIRCHG) == 0 &&
10262 wk->wk_state & MKDIR_PARENT) {
10263 wk->wk_state &= ~MKDIR_PARENT;
10264 WORKLIST_INSERT(&dotdotwk, wk);
10267 WORKLIST_INSERT(&inodedep->id_inowait, wk);
10269 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
10271 * Normal file deletion.
10273 if ((dirrem->dm_state & RMDIR) == 0) {
10275 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
10276 "%ju negative i_nlink %d", (intmax_t)ip->i_number,
10278 DIP_SET(ip, i_nlink, ip->i_nlink);
10279 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10280 if (ip->i_nlink < ip->i_effnlink)
10281 panic("handle_workitem_remove: bad file delta");
10282 if (ip->i_nlink == 0)
10283 unlinked_inodedep(mp, inodedep);
10284 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10285 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10286 ("handle_workitem_remove: worklist not empty. %s",
10287 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
10288 WORKITEM_FREE(dirrem, D_DIRREM);
10293 * Directory deletion. Decrement reference count for both the
10294 * just deleted parent directory entry and the reference for ".".
10295 * Arrange to have the reference count on the parent decremented
10296 * to account for the loss of "..".
10299 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
10300 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
10301 DIP_SET(ip, i_nlink, ip->i_nlink);
10302 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10303 if (ip->i_nlink < ip->i_effnlink)
10304 panic("handle_workitem_remove: bad dir delta");
10305 if (ip->i_nlink == 0)
10306 unlinked_inodedep(mp, inodedep);
10307 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
10309 * Rename a directory to a new parent. Since, we are both deleting
10310 * and creating a new directory entry, the link count on the new
10311 * directory should not change. Thus we skip the followup dirrem.
10313 if (dirrem->dm_state & DIRCHG) {
10314 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
10315 ("handle_workitem_remove: DIRCHG and worklist not empty."));
10316 WORKITEM_FREE(dirrem, D_DIRREM);
10320 dirrem->dm_state = ONDEPLIST;
10321 dirrem->dm_oldinum = dirrem->dm_dirinum;
10323 * Place the dirrem on the parent's diremhd list.
10325 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
10326 panic("handle_workitem_remove: lost dir inodedep");
10327 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
10329 * If the allocated inode has never been written to disk, then
10330 * the on-disk inode is zero'ed and we can remove the file
10331 * immediately. When journaling if the inode has been marked
10332 * unlinked and not DEPCOMPLETE we know it can never be written.
10334 inodedep_lookup(mp, oldinum, 0, &inodedep);
10335 if (inodedep == NULL ||
10336 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
10337 check_inode_unwritten(inodedep)) {
10340 return handle_workitem_remove(dirrem, flags);
10342 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
10344 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10352 * Inode de-allocation dependencies.
10354 * When an inode's link count is reduced to zero, it can be de-allocated. We
10355 * found it convenient to postpone de-allocation until after the inode is
10356 * written to disk with its new link count (zero). At this point, all of the
10357 * on-disk inode's block pointers are nullified and, with careful dependency
10358 * list ordering, all dependencies related to the inode will be satisfied and
10359 * the corresponding dependency structures de-allocated. So, if/when the
10360 * inode is reused, there will be no mixing of old dependencies with new
10361 * ones. This artificial dependency is set up by the block de-allocation
10362 * procedure above (softdep_setup_freeblocks) and completed by the
10363 * following procedure.
10366 handle_workitem_freefile(freefile)
10367 struct freefile *freefile;
10369 struct workhead wkhd;
10371 struct ufsmount *ump;
10374 struct inodedep *idp;
10377 ump = VFSTOUFS(freefile->fx_list.wk_mp);
10381 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10384 panic("handle_workitem_freefile: inodedep %p survived", idp);
10387 fs->fs_pendinginodes -= 1;
10390 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10391 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10392 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10393 softdep_error("handle_workitem_freefile", error);
10395 WORKITEM_FREE(freefile, D_FREEFILE);
10400 * Helper function which unlinks marker element from work list and returns
10401 * the next element on the list.
10403 static __inline struct worklist *
10404 markernext(struct worklist *marker)
10406 struct worklist *next;
10408 next = LIST_NEXT(marker, wk_list);
10409 LIST_REMOVE(marker, wk_list);
10416 * The dependency structures constructed above are most actively used when file
10417 * system blocks are written to disk. No constraints are placed on when a
10418 * block can be written, but unsatisfied update dependencies are made safe by
10419 * modifying (or replacing) the source memory for the duration of the disk
10420 * write. When the disk write completes, the memory block is again brought
10423 * In-core inode structure reclamation.
10425 * Because there are a finite number of "in-core" inode structures, they are
10426 * reused regularly. By transferring all inode-related dependencies to the
10427 * in-memory inode block and indexing them separately (via "inodedep"s), we
10428 * can allow "in-core" inode structures to be reused at any time and avoid
10429 * any increase in contention.
10431 * Called just before entering the device driver to initiate a new disk I/O.
10432 * The buffer must be locked, thus, no I/O completion operations can occur
10433 * while we are manipulating its associated dependencies.
10436 softdep_disk_io_initiation(bp)
10437 struct buf *bp; /* structure describing disk write to occur */
10439 struct worklist *wk;
10440 struct worklist marker;
10441 struct inodedep *inodedep;
10442 struct freeblks *freeblks;
10443 struct jblkdep *jblkdep;
10444 struct newblk *newblk;
10445 struct ufsmount *ump;
10448 * We only care about write operations. There should never
10449 * be dependencies for reads.
10451 if (bp->b_iocmd != BIO_WRITE)
10452 panic("softdep_disk_io_initiation: not write");
10454 if (bp->b_vflags & BV_BKGRDINPROG)
10455 panic("softdep_disk_io_initiation: Writing buffer with "
10456 "background write in progress: %p", bp);
10458 ump = softdep_bp_to_mp(bp);
10462 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10463 PHOLD(curproc); /* Don't swap out kernel stack */
10466 * Do any necessary pre-I/O processing.
10468 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10469 wk = markernext(&marker)) {
10470 LIST_INSERT_AFTER(wk, &marker, wk_list);
10471 switch (wk->wk_type) {
10473 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10477 inodedep = WK_INODEDEP(wk);
10478 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10479 initiate_write_inodeblock_ufs1(inodedep, bp);
10481 initiate_write_inodeblock_ufs2(inodedep, bp);
10485 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10489 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10493 WK_JSEG(wk)->js_buf = NULL;
10497 freeblks = WK_FREEBLKS(wk);
10498 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10500 * We have to wait for the freeblks to be journaled
10501 * before we can write an inodeblock with updated
10502 * pointers. Be careful to arrange the marker so
10503 * we revisit the freeblks if it's not removed by
10504 * the first jwait().
10506 if (jblkdep != NULL) {
10507 LIST_REMOVE(&marker, wk_list);
10508 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10509 jwait(&jblkdep->jb_list, MNT_WAIT);
10512 case D_ALLOCDIRECT:
10515 * We have to wait for the jnewblk to be journaled
10516 * before we can write to a block if the contents
10517 * may be confused with an earlier file's indirect
10518 * at recovery time. Handle the marker as described
10521 newblk = WK_NEWBLK(wk);
10522 if (newblk->nb_jnewblk != NULL &&
10523 indirblk_lookup(newblk->nb_list.wk_mp,
10524 newblk->nb_newblkno)) {
10525 LIST_REMOVE(&marker, wk_list);
10526 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10527 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10532 initiate_write_sbdep(WK_SBDEP(wk));
10542 panic("handle_disk_io_initiation: Unexpected type %s",
10543 TYPENAME(wk->wk_type));
10548 PRELE(curproc); /* Allow swapout of kernel stack */
10552 * Called from within the procedure above to deal with unsatisfied
10553 * allocation dependencies in a directory. The buffer must be locked,
10554 * thus, no I/O completion operations can occur while we are
10555 * manipulating its associated dependencies.
10558 initiate_write_filepage(pagedep, bp)
10559 struct pagedep *pagedep;
10562 struct jremref *jremref;
10563 struct jmvref *jmvref;
10564 struct dirrem *dirrem;
10565 struct diradd *dap;
10569 if (pagedep->pd_state & IOSTARTED) {
10571 * This can only happen if there is a driver that does not
10572 * understand chaining. Here biodone will reissue the call
10573 * to strategy for the incomplete buffers.
10575 printf("initiate_write_filepage: already started\n");
10578 pagedep->pd_state |= IOSTARTED;
10580 * Wait for all journal remove dependencies to hit the disk.
10581 * We can not allow any potentially conflicting directory adds
10582 * to be visible before removes and rollback is too difficult.
10583 * The per-filesystem lock may be dropped and re-acquired, however
10584 * we hold the buf locked so the dependency can not go away.
10586 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10587 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10588 jwait(&jremref->jr_list, MNT_WAIT);
10589 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10590 jwait(&jmvref->jm_list, MNT_WAIT);
10591 for (i = 0; i < DAHASHSZ; i++) {
10592 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10593 ep = (struct direct *)
10594 ((char *)bp->b_data + dap->da_offset);
10595 if (ep->d_ino != dap->da_newinum)
10596 panic("%s: dir inum %ju != new %ju",
10597 "initiate_write_filepage",
10598 (uintmax_t)ep->d_ino,
10599 (uintmax_t)dap->da_newinum);
10600 if (dap->da_state & DIRCHG)
10601 ep->d_ino = dap->da_previous->dm_oldinum;
10604 dap->da_state &= ~ATTACHED;
10605 dap->da_state |= UNDONE;
10611 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10612 * Note that any bug fixes made to this routine must be done in the
10613 * version found below.
10615 * Called from within the procedure above to deal with unsatisfied
10616 * allocation dependencies in an inodeblock. The buffer must be
10617 * locked, thus, no I/O completion operations can occur while we
10618 * are manipulating its associated dependencies.
10621 initiate_write_inodeblock_ufs1(inodedep, bp)
10622 struct inodedep *inodedep;
10623 struct buf *bp; /* The inode block */
10625 struct allocdirect *adp, *lastadp;
10626 struct ufs1_dinode *dp;
10627 struct ufs1_dinode *sip;
10628 struct inoref *inoref;
10629 struct ufsmount *ump;
10633 ufs_lbn_t prevlbn = 0;
10637 if (inodedep->id_state & IOSTARTED)
10638 panic("initiate_write_inodeblock_ufs1: already started");
10639 inodedep->id_state |= IOSTARTED;
10640 fs = inodedep->id_fs;
10641 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10643 dp = (struct ufs1_dinode *)bp->b_data +
10644 ino_to_fsbo(fs, inodedep->id_ino);
10647 * If we're on the unlinked list but have not yet written our
10648 * next pointer initialize it here.
10650 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10651 struct inodedep *inon;
10653 inon = TAILQ_NEXT(inodedep, id_unlinked);
10654 dp->di_freelink = inon ? inon->id_ino : 0;
10657 * If the bitmap is not yet written, then the allocated
10658 * inode cannot be written to disk.
10660 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10661 if (inodedep->id_savedino1 != NULL)
10662 panic("initiate_write_inodeblock_ufs1: I/O underway");
10664 sip = malloc(sizeof(struct ufs1_dinode),
10665 M_SAVEDINO, M_SOFTDEP_FLAGS);
10667 inodedep->id_savedino1 = sip;
10668 *inodedep->id_savedino1 = *dp;
10669 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10670 dp->di_gen = inodedep->id_savedino1->di_gen;
10671 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10675 * If no dependencies, then there is nothing to roll back.
10677 inodedep->id_savedsize = dp->di_size;
10678 inodedep->id_savedextsize = 0;
10679 inodedep->id_savednlink = dp->di_nlink;
10680 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10681 TAILQ_EMPTY(&inodedep->id_inoreflst))
10684 * Revert the link count to that of the first unwritten journal entry.
10686 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10688 dp->di_nlink = inoref->if_nlink;
10690 * Set the dependencies to busy.
10692 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10693 adp = TAILQ_NEXT(adp, ad_next)) {
10695 if (deplist != 0 && prevlbn >= adp->ad_offset)
10696 panic("softdep_write_inodeblock: lbn order");
10697 prevlbn = adp->ad_offset;
10698 if (adp->ad_offset < UFS_NDADDR &&
10699 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10700 panic("initiate_write_inodeblock_ufs1: "
10701 "direct pointer #%jd mismatch %d != %jd",
10702 (intmax_t)adp->ad_offset,
10703 dp->di_db[adp->ad_offset],
10704 (intmax_t)adp->ad_newblkno);
10705 if (adp->ad_offset >= UFS_NDADDR &&
10706 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10707 panic("initiate_write_inodeblock_ufs1: "
10708 "indirect pointer #%jd mismatch %d != %jd",
10709 (intmax_t)adp->ad_offset - UFS_NDADDR,
10710 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10711 (intmax_t)adp->ad_newblkno);
10712 deplist |= 1 << adp->ad_offset;
10713 if ((adp->ad_state & ATTACHED) == 0)
10714 panic("initiate_write_inodeblock_ufs1: "
10715 "Unknown state 0x%x", adp->ad_state);
10716 #endif /* INVARIANTS */
10717 adp->ad_state &= ~ATTACHED;
10718 adp->ad_state |= UNDONE;
10721 * The on-disk inode cannot claim to be any larger than the last
10722 * fragment that has been written. Otherwise, the on-disk inode
10723 * might have fragments that were not the last block in the file
10724 * which would corrupt the filesystem.
10726 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10727 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10728 if (adp->ad_offset >= UFS_NDADDR)
10730 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10731 /* keep going until hitting a rollback to a frag */
10732 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10734 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10735 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10737 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10738 panic("initiate_write_inodeblock_ufs1: "
10740 #endif /* INVARIANTS */
10743 for (i = 0; i < UFS_NIADDR; i++) {
10745 if (dp->di_ib[i] != 0 &&
10746 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10747 panic("initiate_write_inodeblock_ufs1: "
10749 #endif /* INVARIANTS */
10755 * If we have zero'ed out the last allocated block of the file,
10756 * roll back the size to the last currently allocated block.
10757 * We know that this last allocated block is a full-sized as
10758 * we already checked for fragments in the loop above.
10760 if (lastadp != NULL &&
10761 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10762 for (i = lastadp->ad_offset; i >= 0; i--)
10763 if (dp->di_db[i] != 0)
10765 dp->di_size = (i + 1) * fs->fs_bsize;
10768 * The only dependencies are for indirect blocks.
10770 * The file size for indirect block additions is not guaranteed.
10771 * Such a guarantee would be non-trivial to achieve. The conventional
10772 * synchronous write implementation also does not make this guarantee.
10773 * Fsck should catch and fix discrepancies. Arguably, the file size
10774 * can be over-estimated without destroying integrity when the file
10775 * moves into the indirect blocks (i.e., is large). If we want to
10776 * postpone fsck, we are stuck with this argument.
10778 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10779 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10783 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10784 * Note that any bug fixes made to this routine must be done in the
10785 * version found above.
10787 * Called from within the procedure above to deal with unsatisfied
10788 * allocation dependencies in an inodeblock. The buffer must be
10789 * locked, thus, no I/O completion operations can occur while we
10790 * are manipulating its associated dependencies.
10793 initiate_write_inodeblock_ufs2(inodedep, bp)
10794 struct inodedep *inodedep;
10795 struct buf *bp; /* The inode block */
10797 struct allocdirect *adp, *lastadp;
10798 struct ufs2_dinode *dp;
10799 struct ufs2_dinode *sip;
10800 struct inoref *inoref;
10801 struct ufsmount *ump;
10805 ufs_lbn_t prevlbn = 0;
10809 if (inodedep->id_state & IOSTARTED)
10810 panic("initiate_write_inodeblock_ufs2: already started");
10811 inodedep->id_state |= IOSTARTED;
10812 fs = inodedep->id_fs;
10813 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10815 dp = (struct ufs2_dinode *)bp->b_data +
10816 ino_to_fsbo(fs, inodedep->id_ino);
10819 * If we're on the unlinked list but have not yet written our
10820 * next pointer initialize it here.
10822 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10823 struct inodedep *inon;
10825 inon = TAILQ_NEXT(inodedep, id_unlinked);
10826 dp->di_freelink = inon ? inon->id_ino : 0;
10827 ffs_update_dinode_ckhash(fs, dp);
10830 * If the bitmap is not yet written, then the allocated
10831 * inode cannot be written to disk.
10833 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10834 if (inodedep->id_savedino2 != NULL)
10835 panic("initiate_write_inodeblock_ufs2: I/O underway");
10837 sip = malloc(sizeof(struct ufs2_dinode),
10838 M_SAVEDINO, M_SOFTDEP_FLAGS);
10840 inodedep->id_savedino2 = sip;
10841 *inodedep->id_savedino2 = *dp;
10842 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10843 dp->di_gen = inodedep->id_savedino2->di_gen;
10844 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10848 * If no dependencies, then there is nothing to roll back.
10850 inodedep->id_savedsize = dp->di_size;
10851 inodedep->id_savedextsize = dp->di_extsize;
10852 inodedep->id_savednlink = dp->di_nlink;
10853 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10854 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10855 TAILQ_EMPTY(&inodedep->id_inoreflst))
10858 * Revert the link count to that of the first unwritten journal entry.
10860 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10862 dp->di_nlink = inoref->if_nlink;
10865 * Set the ext data dependencies to busy.
10867 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10868 adp = TAILQ_NEXT(adp, ad_next)) {
10870 if (deplist != 0 && prevlbn >= adp->ad_offset)
10871 panic("initiate_write_inodeblock_ufs2: lbn order");
10872 prevlbn = adp->ad_offset;
10873 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10874 panic("initiate_write_inodeblock_ufs2: "
10875 "ext pointer #%jd mismatch %jd != %jd",
10876 (intmax_t)adp->ad_offset,
10877 (intmax_t)dp->di_extb[adp->ad_offset],
10878 (intmax_t)adp->ad_newblkno);
10879 deplist |= 1 << adp->ad_offset;
10880 if ((adp->ad_state & ATTACHED) == 0)
10881 panic("initiate_write_inodeblock_ufs2: Unknown "
10882 "state 0x%x", adp->ad_state);
10883 #endif /* INVARIANTS */
10884 adp->ad_state &= ~ATTACHED;
10885 adp->ad_state |= UNDONE;
10888 * The on-disk inode cannot claim to be any larger than the last
10889 * fragment that has been written. Otherwise, the on-disk inode
10890 * might have fragments that were not the last block in the ext
10891 * data which would corrupt the filesystem.
10893 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10894 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10895 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10896 /* keep going until hitting a rollback to a frag */
10897 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10899 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10900 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10902 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10903 panic("initiate_write_inodeblock_ufs2: "
10905 #endif /* INVARIANTS */
10906 dp->di_extb[i] = 0;
10912 * If we have zero'ed out the last allocated block of the ext
10913 * data, roll back the size to the last currently allocated block.
10914 * We know that this last allocated block is a full-sized as
10915 * we already checked for fragments in the loop above.
10917 if (lastadp != NULL &&
10918 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10919 for (i = lastadp->ad_offset; i >= 0; i--)
10920 if (dp->di_extb[i] != 0)
10922 dp->di_extsize = (i + 1) * fs->fs_bsize;
10925 * Set the file data dependencies to busy.
10927 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10928 adp = TAILQ_NEXT(adp, ad_next)) {
10930 if (deplist != 0 && prevlbn >= adp->ad_offset)
10931 panic("softdep_write_inodeblock: lbn order");
10932 if ((adp->ad_state & ATTACHED) == 0)
10933 panic("inodedep %p and adp %p not attached", inodedep, adp);
10934 prevlbn = adp->ad_offset;
10935 if (!ffs_fsfail_cleanup(ump, 0) &&
10936 adp->ad_offset < UFS_NDADDR &&
10937 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10938 panic("initiate_write_inodeblock_ufs2: "
10939 "direct pointer #%jd mismatch %jd != %jd",
10940 (intmax_t)adp->ad_offset,
10941 (intmax_t)dp->di_db[adp->ad_offset],
10942 (intmax_t)adp->ad_newblkno);
10943 if (!ffs_fsfail_cleanup(ump, 0) &&
10944 adp->ad_offset >= UFS_NDADDR &&
10945 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10946 panic("initiate_write_inodeblock_ufs2: "
10947 "indirect pointer #%jd mismatch %jd != %jd",
10948 (intmax_t)adp->ad_offset - UFS_NDADDR,
10949 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10950 (intmax_t)adp->ad_newblkno);
10951 deplist |= 1 << adp->ad_offset;
10952 if ((adp->ad_state & ATTACHED) == 0)
10953 panic("initiate_write_inodeblock_ufs2: Unknown "
10954 "state 0x%x", adp->ad_state);
10955 #endif /* INVARIANTS */
10956 adp->ad_state &= ~ATTACHED;
10957 adp->ad_state |= UNDONE;
10960 * The on-disk inode cannot claim to be any larger than the last
10961 * fragment that has been written. Otherwise, the on-disk inode
10962 * might have fragments that were not the last block in the file
10963 * which would corrupt the filesystem.
10965 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10966 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10967 if (adp->ad_offset >= UFS_NDADDR)
10969 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10970 /* keep going until hitting a rollback to a frag */
10971 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10973 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10974 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10976 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10977 panic("initiate_write_inodeblock_ufs2: "
10979 #endif /* INVARIANTS */
10982 for (i = 0; i < UFS_NIADDR; i++) {
10984 if (dp->di_ib[i] != 0 &&
10985 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10986 panic("initiate_write_inodeblock_ufs2: "
10988 #endif /* INVARIANTS */
10991 ffs_update_dinode_ckhash(fs, dp);
10995 * If we have zero'ed out the last allocated block of the file,
10996 * roll back the size to the last currently allocated block.
10997 * We know that this last allocated block is a full-sized as
10998 * we already checked for fragments in the loop above.
11000 if (lastadp != NULL &&
11001 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
11002 for (i = lastadp->ad_offset; i >= 0; i--)
11003 if (dp->di_db[i] != 0)
11005 dp->di_size = (i + 1) * fs->fs_bsize;
11008 * The only dependencies are for indirect blocks.
11010 * The file size for indirect block additions is not guaranteed.
11011 * Such a guarantee would be non-trivial to achieve. The conventional
11012 * synchronous write implementation also does not make this guarantee.
11013 * Fsck should catch and fix discrepancies. Arguably, the file size
11014 * can be over-estimated without destroying integrity when the file
11015 * moves into the indirect blocks (i.e., is large). If we want to
11016 * postpone fsck, we are stuck with this argument.
11018 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
11019 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
11020 ffs_update_dinode_ckhash(fs, dp);
11024 * Cancel an indirdep as a result of truncation. Release all of the
11025 * children allocindirs and place their journal work on the appropriate
11029 cancel_indirdep(indirdep, bp, freeblks)
11030 struct indirdep *indirdep;
11032 struct freeblks *freeblks;
11034 struct allocindir *aip;
11037 * None of the indirect pointers will ever be visible,
11038 * so they can simply be tossed. GOINGAWAY ensures
11039 * that allocated pointers will be saved in the buffer
11040 * cache until they are freed. Note that they will
11041 * only be able to be found by their physical address
11042 * since the inode mapping the logical address will
11043 * be gone. The save buffer used for the safe copy
11044 * was allocated in setup_allocindir_phase2 using
11045 * the physical address so it could be used for this
11046 * purpose. Hence we swap the safe copy with the real
11047 * copy, allowing the safe copy to be freed and holding
11048 * on to the real copy for later use in indir_trunc.
11050 if (indirdep->ir_state & GOINGAWAY)
11051 panic("cancel_indirdep: already gone");
11052 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11053 indirdep->ir_state |= DEPCOMPLETE;
11054 LIST_REMOVE(indirdep, ir_next);
11056 indirdep->ir_state |= GOINGAWAY;
11058 * Pass in bp for blocks still have journal writes
11059 * pending so we can cancel them on their own.
11061 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
11062 cancel_allocindir(aip, bp, freeblks, 0);
11063 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
11064 cancel_allocindir(aip, NULL, freeblks, 0);
11065 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
11066 cancel_allocindir(aip, NULL, freeblks, 0);
11067 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
11068 cancel_allocindir(aip, NULL, freeblks, 0);
11070 * If there are pending partial truncations we need to keep the
11071 * old block copy around until they complete. This is because
11072 * the current b_data is not a perfect superset of the available
11075 if (TAILQ_EMPTY(&indirdep->ir_trunc))
11076 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
11078 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
11079 WORKLIST_REMOVE(&indirdep->ir_list);
11080 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
11081 indirdep->ir_bp = NULL;
11082 indirdep->ir_freeblks = freeblks;
11086 * Free an indirdep once it no longer has new pointers to track.
11089 free_indirdep(indirdep)
11090 struct indirdep *indirdep;
11093 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
11094 ("free_indirdep: Indir trunc list not empty."));
11095 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
11096 ("free_indirdep: Complete head not empty."));
11097 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
11098 ("free_indirdep: write head not empty."));
11099 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
11100 ("free_indirdep: done head not empty."));
11101 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
11102 ("free_indirdep: deplist head not empty."));
11103 KASSERT((indirdep->ir_state & DEPCOMPLETE),
11104 ("free_indirdep: %p still on newblk list.", indirdep));
11105 KASSERT(indirdep->ir_saveddata == NULL,
11106 ("free_indirdep: %p still has saved data.", indirdep));
11107 KASSERT(indirdep->ir_savebp == NULL,
11108 ("free_indirdep: %p still has savebp buffer.", indirdep));
11109 if (indirdep->ir_state & ONWORKLIST)
11110 WORKLIST_REMOVE(&indirdep->ir_list);
11111 WORKITEM_FREE(indirdep, D_INDIRDEP);
11115 * Called before a write to an indirdep. This routine is responsible for
11116 * rolling back pointers to a safe state which includes only those
11117 * allocindirs which have been completed.
11120 initiate_write_indirdep(indirdep, bp)
11121 struct indirdep *indirdep;
11124 struct ufsmount *ump;
11126 indirdep->ir_state |= IOSTARTED;
11127 if (indirdep->ir_state & GOINGAWAY)
11128 panic("disk_io_initiation: indirdep gone");
11130 * If there are no remaining dependencies, this will be writing
11131 * the real pointers.
11133 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
11134 TAILQ_EMPTY(&indirdep->ir_trunc))
11137 * Replace up-to-date version with safe version.
11139 if (indirdep->ir_saveddata == NULL) {
11140 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
11143 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
11147 indirdep->ir_state &= ~ATTACHED;
11148 indirdep->ir_state |= UNDONE;
11149 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
11150 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
11155 * Called when an inode has been cleared in a cg bitmap. This finally
11156 * eliminates any canceled jaddrefs
11159 softdep_setup_inofree(mp, bp, ino, wkhd)
11163 struct workhead *wkhd;
11165 struct worklist *wk, *wkn;
11166 struct inodedep *inodedep;
11167 struct ufsmount *ump;
11172 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
11173 ("softdep_setup_inofree called on non-softdep filesystem"));
11174 ump = VFSTOUFS(mp);
11176 if (!ffs_fsfail_cleanup(ump, 0)) {
11178 cgp = (struct cg *)bp->b_data;
11179 inosused = cg_inosused(cgp);
11180 if (isset(inosused, ino % fs->fs_ipg))
11181 panic("softdep_setup_inofree: inode %ju not freed.",
11184 if (inodedep_lookup(mp, ino, 0, &inodedep))
11185 panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
11186 (uintmax_t)ino, inodedep);
11188 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
11189 if (wk->wk_type != D_JADDREF)
11191 WORKLIST_REMOVE(wk);
11193 * We can free immediately even if the jaddref
11194 * isn't attached in a background write as now
11195 * the bitmaps are reconciled.
11197 wk->wk_state |= COMPLETE | ATTACHED;
11198 free_jaddref(WK_JADDREF(wk));
11200 jwork_move(&bp->b_dep, wkhd);
11206 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
11207 * map. Any dependencies waiting for the write to clear are added to the
11208 * buf's list and any jnewblks that are being canceled are discarded
11212 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
11215 ufs2_daddr_t blkno;
11217 struct workhead *wkhd;
11219 struct bmsafemap *bmsafemap;
11220 struct jnewblk *jnewblk;
11221 struct ufsmount *ump;
11222 struct worklist *wk;
11227 ufs2_daddr_t jstart;
11235 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
11236 blkno, frags, wkhd);
11238 ump = VFSTOUFS(mp);
11239 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
11240 ("softdep_setup_blkfree called on non-softdep filesystem"));
11242 /* Lookup the bmsafemap so we track when it is dirty. */
11244 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11246 * Detach any jnewblks which have been canceled. They must linger
11247 * until the bitmap is cleared again by ffs_blkfree() to prevent
11248 * an unjournaled allocation from hitting the disk.
11251 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11253 "softdep_setup_blkfree: blkno %jd wk type %d",
11254 blkno, wk->wk_type);
11255 WORKLIST_REMOVE(wk);
11256 if (wk->wk_type != D_JNEWBLK) {
11257 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
11260 jnewblk = WK_JNEWBLK(wk);
11261 KASSERT(jnewblk->jn_state & GOINGAWAY,
11262 ("softdep_setup_blkfree: jnewblk not canceled."));
11265 * Assert that this block is free in the bitmap
11266 * before we discard the jnewblk.
11268 cgp = (struct cg *)bp->b_data;
11269 blksfree = cg_blksfree(cgp);
11270 bno = dtogd(fs, jnewblk->jn_blkno);
11271 for (i = jnewblk->jn_oldfrags;
11272 i < jnewblk->jn_frags; i++) {
11273 if (isset(blksfree, bno + i))
11275 panic("softdep_setup_blkfree: not free");
11279 * Even if it's not attached we can free immediately
11280 * as the new bitmap is correct.
11282 wk->wk_state |= COMPLETE | ATTACHED;
11283 free_jnewblk(jnewblk);
11289 * Assert that we are not freeing a block which has an outstanding
11290 * allocation dependency.
11292 fs = VFSTOUFS(mp)->um_fs;
11293 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
11294 end = blkno + frags;
11295 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11297 * Don't match against blocks that will be freed when the
11298 * background write is done.
11300 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
11301 (COMPLETE | DEPCOMPLETE))
11303 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
11304 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
11305 if ((blkno >= jstart && blkno < jend) ||
11306 (end > jstart && end <= jend)) {
11307 printf("state 0x%X %jd - %d %d dep %p\n",
11308 jnewblk->jn_state, jnewblk->jn_blkno,
11309 jnewblk->jn_oldfrags, jnewblk->jn_frags,
11311 panic("softdep_setup_blkfree: "
11312 "%jd-%jd(%d) overlaps with %jd-%jd",
11313 blkno, end, frags, jstart, jend);
11321 * Revert a block allocation when the journal record that describes it
11322 * is not yet written.
11325 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
11326 struct jnewblk *jnewblk;
11331 ufs1_daddr_t fragno;
11337 cgbno = dtogd(fs, jnewblk->jn_blkno);
11339 * We have to test which frags need to be rolled back. We may
11340 * be operating on a stale copy when doing background writes.
11342 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
11343 if (isclr(blksfree, cgbno + i))
11348 * This is mostly ffs_blkfree() sans some validation and
11349 * superblock updates.
11351 if (frags == fs->fs_frag) {
11352 fragno = fragstoblks(fs, cgbno);
11353 ffs_setblock(fs, blksfree, fragno);
11354 ffs_clusteracct(fs, cgp, fragno, 1);
11355 cgp->cg_cs.cs_nbfree++;
11357 cgbno += jnewblk->jn_oldfrags;
11358 bbase = cgbno - fragnum(fs, cgbno);
11359 /* Decrement the old frags. */
11360 blk = blkmap(fs, blksfree, bbase);
11361 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11362 /* Deallocate the fragment */
11363 for (i = 0; i < frags; i++)
11364 setbit(blksfree, cgbno + i);
11365 cgp->cg_cs.cs_nffree += frags;
11366 /* Add back in counts associated with the new frags */
11367 blk = blkmap(fs, blksfree, bbase);
11368 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11369 /* If a complete block has been reassembled, account for it. */
11370 fragno = fragstoblks(fs, bbase);
11371 if (ffs_isblock(fs, blksfree, fragno)) {
11372 cgp->cg_cs.cs_nffree -= fs->fs_frag;
11373 ffs_clusteracct(fs, cgp, fragno, 1);
11374 cgp->cg_cs.cs_nbfree++;
11378 jnewblk->jn_state &= ~ATTACHED;
11379 jnewblk->jn_state |= UNDONE;
11385 initiate_write_bmsafemap(bmsafemap, bp)
11386 struct bmsafemap *bmsafemap;
11387 struct buf *bp; /* The cg block. */
11389 struct jaddref *jaddref;
11390 struct jnewblk *jnewblk;
11398 * If this is a background write, we did this at the time that
11399 * the copy was made, so do not need to do it again.
11401 if (bmsafemap->sm_state & IOSTARTED)
11403 bmsafemap->sm_state |= IOSTARTED;
11405 * Clear any inode allocations which are pending journal writes.
11407 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11408 cgp = (struct cg *)bp->b_data;
11409 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11410 inosused = cg_inosused(cgp);
11411 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11412 ino = jaddref->ja_ino % fs->fs_ipg;
11413 if (isset(inosused, ino)) {
11414 if ((jaddref->ja_mode & IFMT) == IFDIR)
11415 cgp->cg_cs.cs_ndir--;
11416 cgp->cg_cs.cs_nifree++;
11417 clrbit(inosused, ino);
11418 jaddref->ja_state &= ~ATTACHED;
11419 jaddref->ja_state |= UNDONE;
11422 panic("initiate_write_bmsafemap: inode %ju "
11423 "marked free", (uintmax_t)jaddref->ja_ino);
11427 * Clear any block allocations which are pending journal writes.
11429 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11430 cgp = (struct cg *)bp->b_data;
11431 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11432 blksfree = cg_blksfree(cgp);
11433 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11434 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11436 panic("initiate_write_bmsafemap: block %jd "
11437 "marked free", jnewblk->jn_blkno);
11441 * Move allocation lists to the written lists so they can be
11442 * cleared once the block write is complete.
11444 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11445 inodedep, id_deps);
11446 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11448 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11453 softdep_handle_error(struct buf *bp)
11455 struct ufsmount *ump;
11457 ump = softdep_bp_to_mp(bp);
11461 if (ffs_fsfail_cleanup(ump, bp->b_error)) {
11463 * No future writes will succeed, so the on-disk image is safe.
11464 * Pretend that this write succeeded so that the softdep state
11465 * will be cleaned up naturally.
11467 bp->b_ioflags &= ~BIO_ERROR;
11473 * This routine is called during the completion interrupt
11474 * service routine for a disk write (from the procedure called
11475 * by the device driver to inform the filesystem caches of
11476 * a request completion). It should be called early in this
11477 * procedure, before the block is made available to other
11478 * processes or other routines are called.
11482 softdep_disk_write_complete(bp)
11483 struct buf *bp; /* describes the completed disk write */
11485 struct worklist *wk;
11486 struct worklist *owk;
11487 struct ufsmount *ump;
11488 struct workhead reattach;
11489 struct freeblks *freeblks;
11492 ump = softdep_bp_to_mp(bp);
11493 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11494 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11495 "with outstanding dependencies for buffer %p", bp));
11498 if ((bp->b_ioflags & BIO_ERROR) != 0)
11499 softdep_handle_error(bp);
11501 * If an error occurred while doing the write, then the data
11502 * has not hit the disk and the dependencies cannot be processed.
11503 * But we do have to go through and roll forward any dependencies
11504 * that were rolled back before the disk write.
11508 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11509 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11510 switch (wk->wk_type) {
11512 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11516 handle_written_inodeblock(WK_INODEDEP(wk),
11521 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11526 handle_written_indirdep(WK_INDIRDEP(wk),
11530 /* nothing to roll forward */
11539 LIST_INIT(&reattach);
11542 * Ump SU lock must not be released anywhere in this code segment.
11545 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11546 WORKLIST_REMOVE(wk);
11547 atomic_add_long(&dep_write[wk->wk_type], 1);
11549 panic("duplicate worklist: %p\n", wk);
11551 switch (wk->wk_type) {
11553 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11555 WORKLIST_INSERT(&reattach, wk);
11559 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11561 WORKLIST_INSERT(&reattach, wk);
11565 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11567 WORKLIST_INSERT(&reattach, wk);
11571 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11574 case D_ALLOCDIRECT:
11575 wk->wk_state |= COMPLETE;
11576 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11580 wk->wk_state |= COMPLETE;
11581 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11585 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11587 WORKLIST_INSERT(&reattach, wk);
11591 wk->wk_state |= COMPLETE;
11592 freeblks = WK_FREEBLKS(wk);
11593 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11594 LIST_EMPTY(&freeblks->fb_jblkdephd))
11595 add_to_worklist(wk, WK_NODELAY);
11599 handle_written_freework(WK_FREEWORK(wk));
11603 free_jsegdep(WK_JSEGDEP(wk));
11607 handle_written_jseg(WK_JSEG(wk), bp);
11611 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11612 WORKLIST_INSERT(&reattach, wk);
11616 free_freedep(WK_FREEDEP(wk));
11620 panic("handle_disk_write_complete: Unknown type %s",
11621 TYPENAME(wk->wk_type));
11626 * Reattach any requests that must be redone.
11628 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11629 WORKLIST_REMOVE(wk);
11630 WORKLIST_INSERT(&bp->b_dep, wk);
11638 * Called from within softdep_disk_write_complete above.
11641 handle_allocdirect_partdone(adp, wkhd)
11642 struct allocdirect *adp; /* the completed allocdirect */
11643 struct workhead *wkhd; /* Work to do when inode is writtne. */
11645 struct allocdirectlst *listhead;
11646 struct allocdirect *listadp;
11647 struct inodedep *inodedep;
11650 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11651 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11654 * The on-disk inode cannot claim to be any larger than the last
11655 * fragment that has been written. Otherwise, the on-disk inode
11656 * might have fragments that were not the last block in the file
11657 * which would corrupt the filesystem. Thus, we cannot free any
11658 * allocdirects after one whose ad_oldblkno claims a fragment as
11659 * these blocks must be rolled back to zero before writing the inode.
11660 * We check the currently active set of allocdirects in id_inoupdt
11661 * or id_extupdt as appropriate.
11663 inodedep = adp->ad_inodedep;
11664 bsize = inodedep->id_fs->fs_bsize;
11665 if (adp->ad_state & EXTDATA)
11666 listhead = &inodedep->id_extupdt;
11668 listhead = &inodedep->id_inoupdt;
11669 TAILQ_FOREACH(listadp, listhead, ad_next) {
11670 /* found our block */
11671 if (listadp == adp)
11673 /* continue if ad_oldlbn is not a fragment */
11674 if (listadp->ad_oldsize == 0 ||
11675 listadp->ad_oldsize == bsize)
11677 /* hit a fragment */
11681 * If we have reached the end of the current list without
11682 * finding the just finished dependency, then it must be
11683 * on the future dependency list. Future dependencies cannot
11684 * be freed until they are moved to the current list.
11686 if (listadp == NULL) {
11688 if (adp->ad_state & EXTDATA)
11689 listhead = &inodedep->id_newextupdt;
11691 listhead = &inodedep->id_newinoupdt;
11692 TAILQ_FOREACH(listadp, listhead, ad_next)
11693 /* found our block */
11694 if (listadp == adp)
11696 if (listadp == NULL)
11697 panic("handle_allocdirect_partdone: lost dep");
11698 #endif /* INVARIANTS */
11702 * If we have found the just finished dependency, then queue
11703 * it along with anything that follows it that is complete.
11704 * Since the pointer has not yet been written in the inode
11705 * as the dependency prevents it, place the allocdirect on the
11706 * bufwait list where it will be freed once the pointer is
11710 wkhd = &inodedep->id_bufwait;
11711 for (; adp; adp = listadp) {
11712 listadp = TAILQ_NEXT(adp, ad_next);
11713 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11715 TAILQ_REMOVE(listhead, adp, ad_next);
11716 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11721 * Called from within softdep_disk_write_complete above. This routine
11722 * completes successfully written allocindirs.
11725 handle_allocindir_partdone(aip)
11726 struct allocindir *aip; /* the completed allocindir */
11728 struct indirdep *indirdep;
11730 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11732 indirdep = aip->ai_indirdep;
11733 LIST_REMOVE(aip, ai_next);
11735 * Don't set a pointer while the buffer is undergoing IO or while
11736 * we have active truncations.
11738 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11739 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11742 if (indirdep->ir_state & UFS1FMT)
11743 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11746 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11749 * Await the pointer write before freeing the allocindir.
11751 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11755 * Release segments held on a jwork list.
11759 struct workhead *wkhd;
11761 struct worklist *wk;
11763 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11764 WORKLIST_REMOVE(wk);
11765 switch (wk->wk_type) {
11767 free_jsegdep(WK_JSEGDEP(wk));
11770 free_freedep(WK_FREEDEP(wk));
11773 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11774 WORKITEM_FREE(wk, D_FREEFRAG);
11777 handle_written_freework(WK_FREEWORK(wk));
11780 panic("handle_jwork: Unknown type %s\n",
11781 TYPENAME(wk->wk_type));
11787 * Handle the bufwait list on an inode when it is safe to release items
11788 * held there. This normally happens after an inode block is written but
11789 * may be delayed and handled later if there are pending journal items that
11790 * are not yet safe to be released.
11792 static struct freefile *
11793 handle_bufwait(inodedep, refhd)
11794 struct inodedep *inodedep;
11795 struct workhead *refhd;
11797 struct jaddref *jaddref;
11798 struct freefile *freefile;
11799 struct worklist *wk;
11802 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11803 WORKLIST_REMOVE(wk);
11804 switch (wk->wk_type) {
11807 * We defer adding freefile to the worklist
11808 * until all other additions have been made to
11809 * ensure that it will be done after all the
11810 * old blocks have been freed.
11812 if (freefile != NULL)
11813 panic("handle_bufwait: freefile");
11814 freefile = WK_FREEFILE(wk);
11818 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11822 diradd_inode_written(WK_DIRADD(wk), inodedep);
11826 wk->wk_state |= COMPLETE;
11827 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11828 add_to_worklist(wk, 0);
11832 wk->wk_state |= COMPLETE;
11833 add_to_worklist(wk, 0);
11836 case D_ALLOCDIRECT:
11838 free_newblk(WK_NEWBLK(wk));
11842 wk->wk_state |= COMPLETE;
11843 free_jnewblk(WK_JNEWBLK(wk));
11847 * Save freed journal segments and add references on
11848 * the supplied list which will delay their release
11849 * until the cg bitmap is cleared on disk.
11853 free_jsegdep(WK_JSEGDEP(wk));
11855 WORKLIST_INSERT(refhd, wk);
11859 jaddref = WK_JADDREF(wk);
11860 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11863 * Transfer any jaddrefs to the list to be freed with
11864 * the bitmap if we're handling a removed file.
11866 if (refhd == NULL) {
11867 wk->wk_state |= COMPLETE;
11868 free_jaddref(jaddref);
11870 WORKLIST_INSERT(refhd, wk);
11874 panic("handle_bufwait: Unknown type %p(%s)",
11875 wk, TYPENAME(wk->wk_type));
11882 * Called from within softdep_disk_write_complete above to restore
11883 * in-memory inode block contents to their most up-to-date state. Note
11884 * that this routine is always called from interrupt level with further
11885 * interrupts from this device blocked.
11887 * If the write did not succeed, we will do all the roll-forward
11888 * operations, but we will not take the actions that will allow its
11889 * dependencies to be processed.
11892 handle_written_inodeblock(inodedep, bp, flags)
11893 struct inodedep *inodedep;
11894 struct buf *bp; /* buffer containing the inode block */
11897 struct freefile *freefile;
11898 struct allocdirect *adp, *nextadp;
11899 struct ufs1_dinode *dp1 = NULL;
11900 struct ufs2_dinode *dp2 = NULL;
11901 struct workhead wkhd;
11902 int hadchanges, fstype;
11908 if ((inodedep->id_state & IOSTARTED) == 0)
11909 panic("handle_written_inodeblock: not started");
11910 inodedep->id_state &= ~IOSTARTED;
11911 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11913 dp1 = (struct ufs1_dinode *)bp->b_data +
11914 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11915 freelink = dp1->di_freelink;
11918 dp2 = (struct ufs2_dinode *)bp->b_data +
11919 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11920 freelink = dp2->di_freelink;
11923 * Leave this inodeblock dirty until it's in the list.
11925 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11926 (flags & WRITESUCCEEDED)) {
11927 struct inodedep *inon;
11929 inon = TAILQ_NEXT(inodedep, id_unlinked);
11930 if ((inon == NULL && freelink == 0) ||
11931 (inon && inon->id_ino == freelink)) {
11933 inon->id_state |= UNLINKPREV;
11934 inodedep->id_state |= UNLINKNEXT;
11939 * If we had to rollback the inode allocation because of
11940 * bitmaps being incomplete, then simply restore it.
11941 * Keep the block dirty so that it will not be reclaimed until
11942 * all associated dependencies have been cleared and the
11943 * corresponding updates written to disk.
11945 if (inodedep->id_savedino1 != NULL) {
11947 if (fstype == UFS1)
11948 *dp1 = *inodedep->id_savedino1;
11950 *dp2 = *inodedep->id_savedino2;
11951 free(inodedep->id_savedino1, M_SAVEDINO);
11952 inodedep->id_savedino1 = NULL;
11953 if ((bp->b_flags & B_DELWRI) == 0)
11954 stat_inode_bitmap++;
11957 * If the inode is clear here and GOINGAWAY it will never
11958 * be written. Process the bufwait and clear any pending
11959 * work which may include the freefile.
11961 if (inodedep->id_state & GOINGAWAY)
11965 if (flags & WRITESUCCEEDED)
11966 inodedep->id_state |= COMPLETE;
11968 * Roll forward anything that had to be rolled back before
11969 * the inode could be updated.
11971 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11972 nextadp = TAILQ_NEXT(adp, ad_next);
11973 if (adp->ad_state & ATTACHED)
11974 panic("handle_written_inodeblock: new entry");
11975 if (fstype == UFS1) {
11976 if (adp->ad_offset < UFS_NDADDR) {
11977 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11978 panic("%s %s #%jd mismatch %d != %jd",
11979 "handle_written_inodeblock:",
11981 (intmax_t)adp->ad_offset,
11982 dp1->di_db[adp->ad_offset],
11983 (intmax_t)adp->ad_oldblkno);
11984 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11986 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11988 panic("%s: %s #%jd allocated as %d",
11989 "handle_written_inodeblock",
11990 "indirect pointer",
11991 (intmax_t)adp->ad_offset -
11993 dp1->di_ib[adp->ad_offset -
11995 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11999 if (adp->ad_offset < UFS_NDADDR) {
12000 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
12001 panic("%s: %s #%jd %s %jd != %jd",
12002 "handle_written_inodeblock",
12004 (intmax_t)adp->ad_offset, "mismatch",
12005 (intmax_t)dp2->di_db[adp->ad_offset],
12006 (intmax_t)adp->ad_oldblkno);
12007 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
12009 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
12011 panic("%s: %s #%jd allocated as %jd",
12012 "handle_written_inodeblock",
12013 "indirect pointer",
12014 (intmax_t)adp->ad_offset -
12017 dp2->di_ib[adp->ad_offset -
12019 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
12023 adp->ad_state &= ~UNDONE;
12024 adp->ad_state |= ATTACHED;
12027 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
12028 nextadp = TAILQ_NEXT(adp, ad_next);
12029 if (adp->ad_state & ATTACHED)
12030 panic("handle_written_inodeblock: new entry");
12031 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
12032 panic("%s: direct pointers #%jd %s %jd != %jd",
12033 "handle_written_inodeblock",
12034 (intmax_t)adp->ad_offset, "mismatch",
12035 (intmax_t)dp2->di_extb[adp->ad_offset],
12036 (intmax_t)adp->ad_oldblkno);
12037 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
12038 adp->ad_state &= ~UNDONE;
12039 adp->ad_state |= ATTACHED;
12042 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
12043 stat_direct_blk_ptrs++;
12045 * Reset the file size to its most up-to-date value.
12047 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
12048 panic("handle_written_inodeblock: bad size");
12049 if (inodedep->id_savednlink > UFS_LINK_MAX)
12050 panic("handle_written_inodeblock: Invalid link count "
12051 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
12053 if (fstype == UFS1) {
12054 if (dp1->di_nlink != inodedep->id_savednlink) {
12055 dp1->di_nlink = inodedep->id_savednlink;
12058 if (dp1->di_size != inodedep->id_savedsize) {
12059 dp1->di_size = inodedep->id_savedsize;
12063 if (dp2->di_nlink != inodedep->id_savednlink) {
12064 dp2->di_nlink = inodedep->id_savednlink;
12067 if (dp2->di_size != inodedep->id_savedsize) {
12068 dp2->di_size = inodedep->id_savedsize;
12071 if (dp2->di_extsize != inodedep->id_savedextsize) {
12072 dp2->di_extsize = inodedep->id_savedextsize;
12076 inodedep->id_savedsize = -1;
12077 inodedep->id_savedextsize = -1;
12078 inodedep->id_savednlink = -1;
12080 * If there were any rollbacks in the inode block, then it must be
12081 * marked dirty so that its will eventually get written back in
12082 * its correct form.
12085 if (fstype == UFS2)
12086 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
12091 * If the write did not succeed, we have done all the roll-forward
12092 * operations, but we cannot take the actions that will allow its
12093 * dependencies to be processed.
12095 if ((flags & WRITESUCCEEDED) == 0)
12096 return (hadchanges);
12098 * Process any allocdirects that completed during the update.
12100 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
12101 handle_allocdirect_partdone(adp, &wkhd);
12102 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
12103 handle_allocdirect_partdone(adp, &wkhd);
12105 * Process deallocations that were held pending until the
12106 * inode had been written to disk. Freeing of the inode
12107 * is delayed until after all blocks have been freed to
12108 * avoid creation of new <vfsid, inum, lbn> triples
12109 * before the old ones have been deleted. Completely
12110 * unlinked inodes are not processed until the unlinked
12111 * inode list is written or the last reference is removed.
12113 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
12114 freefile = handle_bufwait(inodedep, NULL);
12115 if (freefile && !LIST_EMPTY(&wkhd)) {
12116 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
12121 * Move rolled forward dependency completions to the bufwait list
12122 * now that those that were already written have been processed.
12124 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
12125 panic("handle_written_inodeblock: bufwait but no changes");
12126 jwork_move(&inodedep->id_bufwait, &wkhd);
12128 if (freefile != NULL) {
12130 * If the inode is goingaway it was never written. Fake up
12131 * the state here so free_inodedep() can succeed.
12133 if (inodedep->id_state & GOINGAWAY)
12134 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
12135 if (free_inodedep(inodedep) == 0)
12136 panic("handle_written_inodeblock: live inodedep %p",
12138 add_to_worklist(&freefile->fx_list, 0);
12143 * If no outstanding dependencies, free it.
12145 if (free_inodedep(inodedep) ||
12146 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
12147 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
12148 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
12149 LIST_FIRST(&inodedep->id_bufwait) == 0))
12151 return (hadchanges);
12155 * Perform needed roll-forwards and kick off any dependencies that
12156 * can now be processed.
12158 * If the write did not succeed, we will do all the roll-forward
12159 * operations, but we will not take the actions that will allow its
12160 * dependencies to be processed.
12163 handle_written_indirdep(indirdep, bp, bpp, flags)
12164 struct indirdep *indirdep;
12169 struct allocindir *aip;
12173 if (indirdep->ir_state & GOINGAWAY)
12174 panic("handle_written_indirdep: indirdep gone");
12175 if ((indirdep->ir_state & IOSTARTED) == 0)
12176 panic("handle_written_indirdep: IO not started");
12179 * If there were rollbacks revert them here.
12181 if (indirdep->ir_saveddata) {
12182 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
12183 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12184 free(indirdep->ir_saveddata, M_INDIRDEP);
12185 indirdep->ir_saveddata = NULL;
12189 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
12190 indirdep->ir_state |= ATTACHED;
12192 * If the write did not succeed, we have done all the roll-forward
12193 * operations, but we cannot take the actions that will allow its
12194 * dependencies to be processed.
12196 if ((flags & WRITESUCCEEDED) == 0) {
12197 stat_indir_blk_ptrs++;
12202 * Move allocindirs with written pointers to the completehd if
12203 * the indirdep's pointer is not yet written. Otherwise
12206 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
12207 LIST_REMOVE(aip, ai_next);
12208 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
12209 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
12211 newblk_freefrag(&aip->ai_block);
12214 free_newblk(&aip->ai_block);
12217 * Move allocindirs that have finished dependency processing from
12218 * the done list to the write list after updating the pointers.
12220 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
12221 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
12222 handle_allocindir_partdone(aip);
12223 if (aip == LIST_FIRST(&indirdep->ir_donehd))
12224 panic("disk_write_complete: not gone");
12229 * Preserve the indirdep if there were any changes or if it is not
12230 * yet valid on disk.
12233 stat_indir_blk_ptrs++;
12238 * If there were no changes we can discard the savedbp and detach
12239 * ourselves from the buf. We are only carrying completed pointers
12242 sbp = indirdep->ir_savebp;
12243 sbp->b_flags |= B_INVAL | B_NOCACHE;
12244 indirdep->ir_savebp = NULL;
12245 indirdep->ir_bp = NULL;
12247 panic("handle_written_indirdep: bp already exists.");
12250 * The indirdep may not be freed until its parent points at it.
12252 if (indirdep->ir_state & DEPCOMPLETE)
12253 free_indirdep(indirdep);
12259 * Process a diradd entry after its dependent inode has been written.
12262 diradd_inode_written(dap, inodedep)
12263 struct diradd *dap;
12264 struct inodedep *inodedep;
12267 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
12268 dap->da_state |= COMPLETE;
12269 complete_diradd(dap);
12270 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
12274 * Returns true if the bmsafemap will have rollbacks when written. Must only
12275 * be called with the per-filesystem lock and the buf lock on the cg held.
12278 bmsafemap_backgroundwrite(bmsafemap, bp)
12279 struct bmsafemap *bmsafemap;
12284 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
12285 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
12286 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
12288 * If we're initiating a background write we need to process the
12289 * rollbacks as they exist now, not as they exist when IO starts.
12290 * No other consumers will look at the contents of the shadowed
12291 * buf so this is safe to do here.
12293 if (bp->b_xflags & BX_BKGRDMARKER)
12294 initiate_write_bmsafemap(bmsafemap, bp);
12300 * Re-apply an allocation when a cg write is complete.
12303 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
12304 struct jnewblk *jnewblk;
12309 ufs1_daddr_t fragno;
12310 ufs2_daddr_t blkno;
12316 cgbno = dtogd(fs, jnewblk->jn_blkno);
12317 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
12318 if (isclr(blksfree, cgbno + i))
12319 panic("jnewblk_rollforward: re-allocated fragment");
12322 if (frags == fs->fs_frag) {
12323 blkno = fragstoblks(fs, cgbno);
12324 ffs_clrblock(fs, blksfree, (long)blkno);
12325 ffs_clusteracct(fs, cgp, blkno, -1);
12326 cgp->cg_cs.cs_nbfree--;
12328 bbase = cgbno - fragnum(fs, cgbno);
12329 cgbno += jnewblk->jn_oldfrags;
12330 /* If a complete block had been reassembled, account for it. */
12331 fragno = fragstoblks(fs, bbase);
12332 if (ffs_isblock(fs, blksfree, fragno)) {
12333 cgp->cg_cs.cs_nffree += fs->fs_frag;
12334 ffs_clusteracct(fs, cgp, fragno, -1);
12335 cgp->cg_cs.cs_nbfree--;
12337 /* Decrement the old frags. */
12338 blk = blkmap(fs, blksfree, bbase);
12339 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12340 /* Allocate the fragment */
12341 for (i = 0; i < frags; i++)
12342 clrbit(blksfree, cgbno + i);
12343 cgp->cg_cs.cs_nffree -= frags;
12344 /* Add back in counts associated with the new frags */
12345 blk = blkmap(fs, blksfree, bbase);
12346 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12352 * Complete a write to a bmsafemap structure. Roll forward any bitmap
12353 * changes if it's not a background write. Set all written dependencies
12354 * to DEPCOMPLETE and free the structure if possible.
12356 * If the write did not succeed, we will do all the roll-forward
12357 * operations, but we will not take the actions that will allow its
12358 * dependencies to be processed.
12361 handle_written_bmsafemap(bmsafemap, bp, flags)
12362 struct bmsafemap *bmsafemap;
12366 struct newblk *newblk;
12367 struct inodedep *inodedep;
12368 struct jaddref *jaddref, *jatmp;
12369 struct jnewblk *jnewblk, *jntmp;
12370 struct ufsmount *ump;
12379 if ((bmsafemap->sm_state & IOSTARTED) == 0)
12380 panic("handle_written_bmsafemap: Not started\n");
12381 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
12383 bmsafemap->sm_state &= ~IOSTARTED;
12384 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
12386 * If write was successful, release journal work that was waiting
12387 * on the write. Otherwise move the work back.
12389 if (flags & WRITESUCCEEDED)
12390 handle_jwork(&bmsafemap->sm_freewr);
12392 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12393 worklist, wk_list);
12396 * Restore unwritten inode allocation pending jaddref writes.
12398 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12399 cgp = (struct cg *)bp->b_data;
12400 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12401 inosused = cg_inosused(cgp);
12402 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12403 ja_bmdeps, jatmp) {
12404 if ((jaddref->ja_state & UNDONE) == 0)
12406 ino = jaddref->ja_ino % fs->fs_ipg;
12407 if (isset(inosused, ino))
12408 panic("handle_written_bmsafemap: "
12409 "re-allocated inode");
12410 /* Do the roll-forward only if it's a real copy. */
12412 if ((jaddref->ja_mode & IFMT) == IFDIR)
12413 cgp->cg_cs.cs_ndir++;
12414 cgp->cg_cs.cs_nifree--;
12415 setbit(inosused, ino);
12418 jaddref->ja_state &= ~UNDONE;
12419 jaddref->ja_state |= ATTACHED;
12420 free_jaddref(jaddref);
12424 * Restore any block allocations which are pending journal writes.
12426 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12427 cgp = (struct cg *)bp->b_data;
12428 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12429 blksfree = cg_blksfree(cgp);
12430 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12432 if ((jnewblk->jn_state & UNDONE) == 0)
12434 /* Do the roll-forward only if it's a real copy. */
12436 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12438 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12439 jnewblk->jn_state |= ATTACHED;
12440 free_jnewblk(jnewblk);
12444 * If the write did not succeed, we have done all the roll-forward
12445 * operations, but we cannot take the actions that will allow its
12446 * dependencies to be processed.
12448 if ((flags & WRITESUCCEEDED) == 0) {
12449 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12451 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12452 worklist, wk_list);
12457 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12458 newblk->nb_state |= DEPCOMPLETE;
12459 newblk->nb_state &= ~ONDEPLIST;
12460 newblk->nb_bmsafemap = NULL;
12461 LIST_REMOVE(newblk, nb_deps);
12462 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12463 handle_allocdirect_partdone(
12464 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12465 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12466 handle_allocindir_partdone(
12467 WK_ALLOCINDIR(&newblk->nb_list));
12468 else if (newblk->nb_list.wk_type != D_NEWBLK)
12469 panic("handle_written_bmsafemap: Unexpected type: %s",
12470 TYPENAME(newblk->nb_list.wk_type));
12472 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12473 inodedep->id_state |= DEPCOMPLETE;
12474 inodedep->id_state &= ~ONDEPLIST;
12475 LIST_REMOVE(inodedep, id_deps);
12476 inodedep->id_bmsafemap = NULL;
12478 LIST_REMOVE(bmsafemap, sm_next);
12479 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12480 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12481 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12482 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12483 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12484 LIST_REMOVE(bmsafemap, sm_hash);
12485 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12488 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12495 * Try to free a mkdir dependency.
12498 complete_mkdir(mkdir)
12499 struct mkdir *mkdir;
12501 struct diradd *dap;
12503 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12505 LIST_REMOVE(mkdir, md_mkdirs);
12506 dap = mkdir->md_diradd;
12507 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12508 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12509 dap->da_state |= DEPCOMPLETE;
12510 complete_diradd(dap);
12512 WORKITEM_FREE(mkdir, D_MKDIR);
12516 * Handle the completion of a mkdir dependency.
12519 handle_written_mkdir(mkdir, type)
12520 struct mkdir *mkdir;
12524 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12525 panic("handle_written_mkdir: bad type");
12526 mkdir->md_state |= COMPLETE;
12527 complete_mkdir(mkdir);
12531 free_pagedep(pagedep)
12532 struct pagedep *pagedep;
12536 if (pagedep->pd_state & NEWBLOCK)
12538 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12540 for (i = 0; i < DAHASHSZ; i++)
12541 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12543 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12545 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12547 if (pagedep->pd_state & ONWORKLIST)
12548 WORKLIST_REMOVE(&pagedep->pd_list);
12549 LIST_REMOVE(pagedep, pd_hash);
12550 WORKITEM_FREE(pagedep, D_PAGEDEP);
12556 * Called from within softdep_disk_write_complete above.
12557 * A write operation was just completed. Removed inodes can
12558 * now be freed and associated block pointers may be committed.
12559 * Note that this routine is always called from interrupt level
12560 * with further interrupts from this device blocked.
12562 * If the write did not succeed, we will do all the roll-forward
12563 * operations, but we will not take the actions that will allow its
12564 * dependencies to be processed.
12567 handle_written_filepage(pagedep, bp, flags)
12568 struct pagedep *pagedep;
12569 struct buf *bp; /* buffer containing the written page */
12572 struct dirrem *dirrem;
12573 struct diradd *dap, *nextdap;
12577 if ((pagedep->pd_state & IOSTARTED) == 0)
12578 panic("handle_written_filepage: not started");
12579 pagedep->pd_state &= ~IOSTARTED;
12580 if ((flags & WRITESUCCEEDED) == 0)
12583 * Process any directory removals that have been committed.
12585 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12586 LIST_REMOVE(dirrem, dm_next);
12587 dirrem->dm_state |= COMPLETE;
12588 dirrem->dm_dirinum = pagedep->pd_ino;
12589 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12590 ("handle_written_filepage: Journal entries not written."));
12591 add_to_worklist(&dirrem->dm_list, 0);
12594 * Free any directory additions that have been committed.
12595 * If it is a newly allocated block, we have to wait until
12596 * the on-disk directory inode claims the new block.
12598 if ((pagedep->pd_state & NEWBLOCK) == 0)
12599 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12600 free_diradd(dap, NULL);
12603 * Uncommitted directory entries must be restored.
12605 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12606 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12608 nextdap = LIST_NEXT(dap, da_pdlist);
12609 if (dap->da_state & ATTACHED)
12610 panic("handle_written_filepage: attached");
12611 ep = (struct direct *)
12612 ((char *)bp->b_data + dap->da_offset);
12613 ep->d_ino = dap->da_newinum;
12614 dap->da_state &= ~UNDONE;
12615 dap->da_state |= ATTACHED;
12618 * If the inode referenced by the directory has
12619 * been written out, then the dependency can be
12620 * moved to the pending list.
12622 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12623 LIST_REMOVE(dap, da_pdlist);
12624 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12630 * If there were any rollbacks in the directory, then it must be
12631 * marked dirty so that its will eventually get written back in
12632 * its correct form.
12634 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12635 if ((bp->b_flags & B_DELWRI) == 0)
12641 * If we are not waiting for a new directory block to be
12642 * claimed by its inode, then the pagedep will be freed.
12643 * Otherwise it will remain to track any new entries on
12644 * the page in case they are fsync'ed.
12646 free_pagedep(pagedep);
12651 * Writing back in-core inode structures.
12653 * The filesystem only accesses an inode's contents when it occupies an
12654 * "in-core" inode structure. These "in-core" structures are separate from
12655 * the page frames used to cache inode blocks. Only the latter are
12656 * transferred to/from the disk. So, when the updated contents of the
12657 * "in-core" inode structure are copied to the corresponding in-memory inode
12658 * block, the dependencies are also transferred. The following procedure is
12659 * called when copying a dirty "in-core" inode to a cached inode block.
12663 * Called when an inode is loaded from disk. If the effective link count
12664 * differed from the actual link count when it was last flushed, then we
12665 * need to ensure that the correct effective link count is put back.
12668 softdep_load_inodeblock(ip)
12669 struct inode *ip; /* the "in_core" copy of the inode */
12671 struct inodedep *inodedep;
12672 struct ufsmount *ump;
12675 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12676 ("softdep_load_inodeblock called on non-softdep filesystem"));
12678 * Check for alternate nlink count.
12680 ip->i_effnlink = ip->i_nlink;
12682 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12686 if (ip->i_nlink != inodedep->id_nlinkwrote &&
12687 inodedep->id_nlinkwrote != -1) {
12688 KASSERT(ip->i_nlink == 0 &&
12689 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0,
12690 ("read bad i_nlink value"));
12691 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote;
12693 ip->i_effnlink -= inodedep->id_nlinkdelta;
12694 KASSERT(ip->i_effnlink >= 0,
12695 ("softdep_load_inodeblock: negative i_effnlink"));
12700 * This routine is called just before the "in-core" inode
12701 * information is to be copied to the in-memory inode block.
12702 * Recall that an inode block contains several inodes. If
12703 * the force flag is set, then the dependencies will be
12704 * cleared so that the update can always be made. Note that
12705 * the buffer is locked when this routine is called, so we
12706 * will never be in the middle of writing the inode block
12710 softdep_update_inodeblock(ip, bp, waitfor)
12711 struct inode *ip; /* the "in_core" copy of the inode */
12712 struct buf *bp; /* the buffer containing the inode block */
12713 int waitfor; /* nonzero => update must be allowed */
12715 struct inodedep *inodedep;
12716 struct inoref *inoref;
12717 struct ufsmount *ump;
12718 struct worklist *wk;
12725 mp = UFSTOVFS(ump);
12726 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12727 ("softdep_update_inodeblock called on non-softdep filesystem"));
12730 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12731 * does not have access to the in-core ip so must write directly into
12732 * the inode block buffer when setting freelink.
12734 if (fs->fs_magic == FS_UFS1_MAGIC)
12735 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12736 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12738 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12739 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12741 * If the effective link count is not equal to the actual link
12742 * count, then we must track the difference in an inodedep while
12743 * the inode is (potentially) tossed out of the cache. Otherwise,
12744 * if there is no existing inodedep, then there are no dependencies
12749 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12751 if (ip->i_effnlink != ip->i_nlink)
12752 panic("softdep_update_inodeblock: bad link count");
12755 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta,
12756 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d "
12757 "inodedep %p id_nlinkdelta %jd",
12758 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta));
12759 inodedep->id_nlinkwrote = ip->i_nlink;
12760 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12761 panic("softdep_update_inodeblock: bad delta");
12763 * If we're flushing all dependencies we must also move any waiting
12764 * for journal writes onto the bufwait list prior to I/O.
12767 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12768 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12770 jwait(&inoref->if_list, MNT_WAIT);
12776 * Changes have been initiated. Anything depending on these
12777 * changes cannot occur until this inode has been written.
12779 inodedep->id_state &= ~COMPLETE;
12780 if ((inodedep->id_state & ONWORKLIST) == 0)
12781 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12783 * Any new dependencies associated with the incore inode must
12784 * now be moved to the list associated with the buffer holding
12785 * the in-memory copy of the inode. Once merged process any
12786 * allocdirects that are completed by the merger.
12788 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12789 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12790 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12792 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12793 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12794 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12797 * Now that the inode has been pushed into the buffer, the
12798 * operations dependent on the inode being written to disk
12799 * can be moved to the id_bufwait so that they will be
12800 * processed when the buffer I/O completes.
12802 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12803 WORKLIST_REMOVE(wk);
12804 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12807 * Newly allocated inodes cannot be written until the bitmap
12808 * that allocates them have been written (indicated by
12809 * DEPCOMPLETE being set in id_state). If we are doing a
12810 * forced sync (e.g., an fsync on a file), we force the bitmap
12811 * to be written so that the update can be done.
12813 if (waitfor == 0) {
12818 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12822 ibp = inodedep->id_bmsafemap->sm_buf;
12823 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12826 * If ibp came back as NULL, the dependency could have been
12827 * freed while we slept. Look it up again, and check to see
12828 * that it has completed.
12830 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12836 if ((error = bwrite(ibp)) != 0)
12837 softdep_error("softdep_update_inodeblock: bwrite", error);
12841 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12842 * old inode dependency list (such as id_inoupdt).
12845 merge_inode_lists(newlisthead, oldlisthead)
12846 struct allocdirectlst *newlisthead;
12847 struct allocdirectlst *oldlisthead;
12849 struct allocdirect *listadp, *newadp;
12851 newadp = TAILQ_FIRST(newlisthead);
12852 if (newadp != NULL)
12853 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12854 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12855 if (listadp->ad_offset < newadp->ad_offset) {
12856 listadp = TAILQ_NEXT(listadp, ad_next);
12859 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12860 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12861 if (listadp->ad_offset == newadp->ad_offset) {
12862 allocdirect_merge(oldlisthead, newadp,
12866 newadp = TAILQ_FIRST(newlisthead);
12868 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12869 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12870 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12875 * If we are doing an fsync, then we must ensure that any directory
12876 * entries for the inode have been written after the inode gets to disk.
12880 struct vnode *vp; /* the "in_core" copy of the inode */
12882 struct inodedep *inodedep;
12883 struct pagedep *pagedep;
12884 struct inoref *inoref;
12885 struct ufsmount *ump;
12886 struct worklist *wk;
12887 struct diradd *dap;
12893 struct thread *td = curthread;
12894 int error, flushparent, pagedep_new_block;
12900 ump = VFSTOUFS(mp);
12902 if (MOUNTEDSOFTDEP(mp) == 0)
12906 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12910 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12911 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12913 jwait(&inoref->if_list, MNT_WAIT);
12917 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12918 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12919 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12920 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12921 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12922 panic("softdep_fsync: pending ops %p", inodedep);
12923 for (error = 0, flushparent = 0; ; ) {
12924 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12926 if (wk->wk_type != D_DIRADD)
12927 panic("softdep_fsync: Unexpected type %s",
12928 TYPENAME(wk->wk_type));
12929 dap = WK_DIRADD(wk);
12931 * Flush our parent if this directory entry has a MKDIR_PARENT
12932 * dependency or is contained in a newly allocated block.
12934 if (dap->da_state & DIRCHG)
12935 pagedep = dap->da_previous->dm_pagedep;
12937 pagedep = dap->da_pagedep;
12938 parentino = pagedep->pd_ino;
12939 lbn = pagedep->pd_lbn;
12940 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12941 panic("softdep_fsync: dirty");
12942 if ((dap->da_state & MKDIR_PARENT) ||
12943 (pagedep->pd_state & NEWBLOCK))
12948 * If we are being fsync'ed as part of vgone'ing this vnode,
12949 * then we will not be able to release and recover the
12950 * vnode below, so we just have to give up on writing its
12951 * directory entry out. It will eventually be written, just
12952 * not now, but then the user was not asking to have it
12953 * written, so we are not breaking any promises.
12955 if (VN_IS_DOOMED(vp))
12958 * We prevent deadlock by always fetching inodes from the
12959 * root, moving down the directory tree. Thus, when fetching
12960 * our parent directory, we first try to get the lock. If
12961 * that fails, we must unlock ourselves before requesting
12962 * the lock on our parent. See the comment in ufs_lookup
12963 * for details on possible races.
12966 error = get_parent_vp(vp, mp, parentino, NULL, NULL, NULL,
12968 if (error == ERELOOKUP)
12973 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12974 * that are contained in direct blocks will be resolved by
12975 * doing a ffs_update. Pagedeps contained in indirect blocks
12976 * may require a complete sync'ing of the directory. So, we
12977 * try the cheap and fast ffs_update first, and if that fails,
12978 * then we do the slower ffs_syncvnode of the directory.
12983 if ((error = ffs_update(pvp, 1)) != 0) {
12989 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12990 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12991 if (wk->wk_type != D_DIRADD)
12992 panic("softdep_fsync: Unexpected type %s",
12993 TYPENAME(wk->wk_type));
12994 dap = WK_DIRADD(wk);
12995 if (dap->da_state & DIRCHG)
12996 pagedep = dap->da_previous->dm_pagedep;
12998 pagedep = dap->da_pagedep;
12999 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
13002 if (pagedep_new_block && (error =
13003 ffs_syncvnode(pvp, MNT_WAIT, 0))) {
13013 * Flush directory page containing the inode's name.
13015 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
13018 error = bwrite(bp);
13022 if (!ffs_fsfail_cleanup(ump, error))
13025 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
13033 * Flush all the dirty bitmaps associated with the block device
13034 * before flushing the rest of the dirty blocks so as to reduce
13035 * the number of dependencies that will have to be rolled back.
13040 softdep_fsync_mountdev(vp)
13043 struct buf *bp, *nbp;
13044 struct worklist *wk;
13047 if (!vn_isdisk(vp))
13048 panic("softdep_fsync_mountdev: vnode not a disk");
13049 bo = &vp->v_bufobj;
13052 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
13054 * If it is already scheduled, skip to the next buffer.
13056 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
13059 if ((bp->b_flags & B_DELWRI) == 0)
13060 panic("softdep_fsync_mountdev: not dirty");
13062 * We are only interested in bitmaps with outstanding
13065 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
13066 wk->wk_type != D_BMSAFEMAP ||
13067 (bp->b_vflags & BV_BKGRDINPROG)) {
13073 (void) bawrite(bp);
13081 * Sync all cylinder groups that were dirty at the time this function is
13082 * called. Newly dirtied cgs will be inserted before the sentinel. This
13083 * is used to flush freedep activity that may be holding up writes to a
13087 sync_cgs(mp, waitfor)
13091 struct bmsafemap *bmsafemap;
13092 struct bmsafemap *sentinel;
13093 struct ufsmount *ump;
13097 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
13098 sentinel->sm_cg = -1;
13099 ump = VFSTOUFS(mp);
13102 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
13103 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
13104 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
13105 /* Skip sentinels and cgs with no work to release. */
13106 if (bmsafemap->sm_cg == -1 ||
13107 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
13108 LIST_EMPTY(&bmsafemap->sm_freewr))) {
13109 LIST_REMOVE(sentinel, sm_next);
13110 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
13114 * If we don't get the lock and we're waiting try again, if
13115 * not move on to the next buf and try to sync it.
13117 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
13118 if (bp == NULL && waitfor == MNT_WAIT)
13120 LIST_REMOVE(sentinel, sm_next);
13121 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
13125 if (waitfor == MNT_NOWAIT)
13128 error = bwrite(bp);
13133 LIST_REMOVE(sentinel, sm_next);
13135 free(sentinel, M_BMSAFEMAP);
13140 * This routine is called when we are trying to synchronously flush a
13141 * file. This routine must eliminate any filesystem metadata dependencies
13142 * so that the syncing routine can succeed.
13145 softdep_sync_metadata(struct vnode *vp)
13151 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13152 ("softdep_sync_metadata called on non-softdep filesystem"));
13154 * Ensure that any direct block dependencies have been cleared,
13155 * truncations are started, and inode references are journaled.
13157 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
13159 * Write all journal records to prevent rollbacks on devvp.
13161 if (vp->v_type == VCHR)
13162 softdep_flushjournal(vp->v_mount);
13163 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
13165 * Ensure that all truncates are written so we won't find deps on
13168 process_truncates(vp);
13169 FREE_LOCK(VFSTOUFS(vp->v_mount));
13175 * This routine is called when we are attempting to sync a buf with
13176 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
13177 * other IO it can but returns EBUSY if the buffer is not yet able to
13178 * be written. Dependencies which will not cause rollbacks will always
13182 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
13184 struct indirdep *indirdep;
13185 struct pagedep *pagedep;
13186 struct allocindir *aip;
13187 struct newblk *newblk;
13188 struct ufsmount *ump;
13190 struct worklist *wk;
13193 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13194 ("softdep_sync_buf called on non-softdep filesystem"));
13196 * For VCHR we just don't want to force flush any dependencies that
13197 * will cause rollbacks.
13199 if (vp->v_type == VCHR) {
13200 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
13204 ump = VFSTOUFS(vp->v_mount);
13207 * As we hold the buffer locked, none of its dependencies
13212 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
13213 switch (wk->wk_type) {
13214 case D_ALLOCDIRECT:
13216 newblk = WK_NEWBLK(wk);
13217 if (newblk->nb_jnewblk != NULL) {
13218 if (waitfor == MNT_NOWAIT) {
13222 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
13225 if (newblk->nb_state & DEPCOMPLETE ||
13226 waitfor == MNT_NOWAIT)
13228 nbp = newblk->nb_bmsafemap->sm_buf;
13229 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13233 if ((error = bwrite(nbp)) != 0)
13239 indirdep = WK_INDIRDEP(wk);
13240 if (waitfor == MNT_NOWAIT) {
13241 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
13242 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
13247 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
13248 panic("softdep_sync_buf: truncation pending.");
13250 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
13251 newblk = (struct newblk *)aip;
13252 if (newblk->nb_jnewblk != NULL) {
13253 jwait(&newblk->nb_jnewblk->jn_list,
13257 if (newblk->nb_state & DEPCOMPLETE)
13259 nbp = newblk->nb_bmsafemap->sm_buf;
13260 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
13264 if ((error = bwrite(nbp)) != 0)
13273 * Only flush directory entries in synchronous passes.
13275 if (waitfor != MNT_WAIT) {
13280 * While syncing snapshots, we must allow recursive
13285 * We are trying to sync a directory that may
13286 * have dependencies on both its own metadata
13287 * and/or dependencies on the inodes of any
13288 * recently allocated files. We walk its diradd
13289 * lists pushing out the associated inode.
13291 pagedep = WK_PAGEDEP(wk);
13292 for (i = 0; i < DAHASHSZ; i++) {
13293 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
13295 error = flush_pagedep_deps(vp, wk->wk_mp,
13296 &pagedep->pd_diraddhd[i], bp);
13298 if (error != ERELOOKUP)
13313 panic("softdep_sync_buf: Unknown type %s",
13314 TYPENAME(wk->wk_type));
13325 * Flush the dependencies associated with an inodedep.
13328 flush_inodedep_deps(vp, mp, ino)
13333 struct inodedep *inodedep;
13334 struct inoref *inoref;
13335 struct ufsmount *ump;
13336 int error, waitfor;
13339 * This work is done in two passes. The first pass grabs most
13340 * of the buffers and begins asynchronously writing them. The
13341 * only way to wait for these asynchronous writes is to sleep
13342 * on the filesystem vnode which may stay busy for a long time
13343 * if the filesystem is active. So, instead, we make a second
13344 * pass over the dependencies blocking on each write. In the
13345 * usual case we will be blocking against a write that we
13346 * initiated, so when it is done the dependency will have been
13347 * resolved. Thus the second pass is expected to end quickly.
13348 * We give a brief window at the top of the loop to allow
13349 * any pending I/O to complete.
13351 ump = VFSTOUFS(mp);
13353 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
13359 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13361 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13362 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13364 jwait(&inoref->if_list, MNT_WAIT);
13368 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
13369 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
13370 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
13371 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
13374 * If pass2, we are done, otherwise do pass 2.
13376 if (waitfor == MNT_WAIT)
13378 waitfor = MNT_WAIT;
13381 * Try freeing inodedep in case all dependencies have been removed.
13383 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
13384 (void) free_inodedep(inodedep);
13389 * Flush an inode dependency list.
13392 flush_deplist(listhead, waitfor, errorp)
13393 struct allocdirectlst *listhead;
13397 struct allocdirect *adp;
13398 struct newblk *newblk;
13399 struct ufsmount *ump;
13402 if ((adp = TAILQ_FIRST(listhead)) == NULL)
13404 ump = VFSTOUFS(adp->ad_list.wk_mp);
13406 TAILQ_FOREACH(adp, listhead, ad_next) {
13407 newblk = (struct newblk *)adp;
13408 if (newblk->nb_jnewblk != NULL) {
13409 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13412 if (newblk->nb_state & DEPCOMPLETE)
13414 bp = newblk->nb_bmsafemap->sm_buf;
13415 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13417 if (waitfor == MNT_NOWAIT)
13422 if (waitfor == MNT_NOWAIT)
13425 *errorp = bwrite(bp);
13433 * Flush dependencies associated with an allocdirect block.
13436 flush_newblk_dep(vp, mp, lbn)
13441 struct newblk *newblk;
13442 struct ufsmount *ump;
13446 ufs2_daddr_t blkno;
13450 bo = &vp->v_bufobj;
13452 blkno = DIP(ip, i_db[lbn]);
13454 panic("flush_newblk_dep: Missing block");
13455 ump = VFSTOUFS(mp);
13458 * Loop until all dependencies related to this block are satisfied.
13459 * We must be careful to restart after each sleep in case a write
13460 * completes some part of this process for us.
13463 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13467 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13468 panic("flush_newblk_dep: Bad newblk %p", newblk);
13470 * Flush the journal.
13472 if (newblk->nb_jnewblk != NULL) {
13473 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13477 * Write the bitmap dependency.
13479 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13480 bp = newblk->nb_bmsafemap->sm_buf;
13481 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13485 error = bwrite(bp);
13492 * Write the buffer.
13496 bp = gbincore(bo, lbn);
13498 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13499 LK_INTERLOCK, BO_LOCKPTR(bo));
13500 if (error == ENOLCK) {
13503 continue; /* Slept, retry */
13506 break; /* Failed */
13507 if (bp->b_flags & B_DELWRI) {
13509 error = bwrite(bp);
13517 * We have to wait for the direct pointers to
13518 * point at the newdirblk before the dependency
13521 error = ffs_update(vp, 1);
13530 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13533 flush_pagedep_deps(pvp, mp, diraddhdp, locked_bp)
13536 struct diraddhd *diraddhdp;
13537 struct buf *locked_bp;
13539 struct inodedep *inodedep;
13540 struct inoref *inoref;
13541 struct ufsmount *ump;
13542 struct diradd *dap;
13547 struct diraddhd unfinished;
13549 LIST_INIT(&unfinished);
13550 ump = VFSTOUFS(mp);
13553 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13555 * Flush ourselves if this directory entry
13556 * has a MKDIR_PARENT dependency.
13558 if (dap->da_state & MKDIR_PARENT) {
13560 if ((error = ffs_update(pvp, 1)) != 0)
13564 * If that cleared dependencies, go on to next.
13566 if (dap != LIST_FIRST(diraddhdp))
13569 * All MKDIR_PARENT dependencies and all the
13570 * NEWBLOCK pagedeps that are contained in direct
13571 * blocks were resolved by doing above ffs_update.
13572 * Pagedeps contained in indirect blocks may
13573 * require a complete sync'ing of the directory.
13574 * We are in the midst of doing a complete sync,
13575 * so if they are not resolved in this pass we
13576 * defer them for now as they will be sync'ed by
13577 * our caller shortly.
13579 LIST_REMOVE(dap, da_pdlist);
13580 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13584 * A newly allocated directory must have its "." and
13585 * ".." entries written out before its name can be
13586 * committed in its parent.
13588 inum = dap->da_newinum;
13589 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13590 panic("flush_pagedep_deps: lost inode1");
13592 * Wait for any pending journal adds to complete so we don't
13593 * cause rollbacks while syncing.
13595 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13596 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13598 jwait(&inoref->if_list, MNT_WAIT);
13602 if (dap->da_state & MKDIR_BODY) {
13604 error = get_parent_vp(pvp, mp, inum, locked_bp,
13605 diraddhdp, &unfinished, &vp);
13608 error = flush_newblk_dep(vp, mp, 0);
13610 * If we still have the dependency we might need to
13611 * update the vnode to sync the new link count to
13614 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13615 error = ffs_update(vp, 1);
13621 * If that cleared dependencies, go on to next.
13623 if (dap != LIST_FIRST(diraddhdp))
13625 if (dap->da_state & MKDIR_BODY) {
13626 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13628 panic("flush_pagedep_deps: MKDIR_BODY "
13629 "inodedep %p dap %p vp %p",
13630 inodedep, dap, vp);
13634 * Flush the inode on which the directory entry depends.
13635 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13636 * the only remaining dependency is that the updated inode
13637 * count must get pushed to disk. The inode has already
13638 * been pushed into its inode buffer (via VOP_UPDATE) at
13639 * the time of the reference count change. So we need only
13640 * locate that buffer, ensure that there will be no rollback
13641 * caused by a bitmap dependency, then write the inode buffer.
13644 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13645 panic("flush_pagedep_deps: lost inode");
13647 * If the inode still has bitmap dependencies,
13648 * push them to disk.
13650 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13651 bp = inodedep->id_bmsafemap->sm_buf;
13652 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13656 if ((error = bwrite(bp)) != 0)
13659 if (dap != LIST_FIRST(diraddhdp))
13663 * If the inode is still sitting in a buffer waiting
13664 * to be written or waiting for the link count to be
13665 * adjusted update it here to flush it to disk.
13667 if (dap == LIST_FIRST(diraddhdp)) {
13669 error = get_parent_vp(pvp, mp, inum, locked_bp,
13670 diraddhdp, &unfinished, &vp);
13673 error = ffs_update(vp, 1);
13680 * If we have failed to get rid of all the dependencies
13681 * then something is seriously wrong.
13683 if (dap == LIST_FIRST(diraddhdp)) {
13684 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13685 panic("flush_pagedep_deps: failed to flush "
13686 "inodedep %p ino %ju dap %p",
13687 inodedep, (uintmax_t)inum, dap);
13692 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13693 LIST_REMOVE(dap, da_pdlist);
13694 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13700 * A large burst of file addition or deletion activity can drive the
13701 * memory load excessively high. First attempt to slow things down
13702 * using the techniques below. If that fails, this routine requests
13703 * the offending operations to fall back to running synchronously
13704 * until the memory load returns to a reasonable level.
13707 softdep_slowdown(vp)
13710 struct ufsmount *ump;
13712 int max_softdeps_hard;
13714 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13715 ("softdep_slowdown called on non-softdep filesystem"));
13716 ump = VFSTOUFS(vp->v_mount);
13720 * Check for journal space if needed.
13722 if (DOINGSUJ(vp)) {
13723 if (journal_space(ump, 0) == 0)
13727 * If the system is under its limits and our filesystem is
13728 * not responsible for more than our share of the usage and
13729 * we are not low on journal space, then no need to slow down.
13731 max_softdeps_hard = max_softdeps * 11 / 10;
13732 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13733 dep_current[D_INODEDEP] < max_softdeps_hard &&
13734 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13735 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13736 ump->softdep_curdeps[D_DIRREM] <
13737 (max_softdeps_hard / 2) / stat_flush_threads &&
13738 ump->softdep_curdeps[D_INODEDEP] <
13739 max_softdeps_hard / stat_flush_threads &&
13740 ump->softdep_curdeps[D_INDIRDEP] <
13741 (max_softdeps_hard / 1000) / stat_flush_threads &&
13742 ump->softdep_curdeps[D_FREEBLKS] <
13743 max_softdeps_hard / stat_flush_threads) {
13748 * If the journal is low or our filesystem is over its limit
13749 * then speedup the cleanup.
13751 if (ump->softdep_curdeps[D_INDIRDEP] <
13752 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13753 softdep_speedup(ump);
13754 stat_sync_limit_hit += 1;
13757 * We only slow down the rate at which new dependencies are
13758 * generated if we are not using journaling. With journaling,
13759 * the cleanup should always be sufficient to keep things
13768 softdep_request_cleanup_filter(struct vnode *vp, void *arg __unused)
13770 return ((vp->v_iflag & VI_OWEINACT) != 0 && vp->v_usecount == 0 &&
13771 ((vp->v_vflag & VV_NOSYNC) != 0 || VTOI(vp)->i_effnlink == 0));
13775 softdep_request_cleanup_inactivate(struct mount *mp)
13777 struct vnode *vp, *mvp;
13780 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, softdep_request_cleanup_filter,
13783 vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
13785 if (vp->v_data != NULL && vp->v_usecount == 0) {
13786 while ((vp->v_iflag & VI_OWEINACT) != 0) {
13787 error = vinactive(vp);
13788 if (error != 0 && error != ERELOOKUP)
13791 atomic_add_int(&stat_delayed_inact, 1);
13799 * Called by the allocation routines when they are about to fail
13800 * in the hope that we can free up the requested resource (inodes
13803 * First check to see if the work list has anything on it. If it has,
13804 * clean up entries until we successfully free the requested resource.
13805 * Because this process holds inodes locked, we cannot handle any remove
13806 * requests that might block on a locked inode as that could lead to
13807 * deadlock. If the worklist yields none of the requested resource,
13808 * start syncing out vnodes to free up the needed space.
13811 softdep_request_cleanup(fs, vp, cred, resource)
13814 struct ucred *cred;
13817 struct ufsmount *ump;
13820 ufs2_daddr_t needed;
13821 int error, failed_vnode;
13824 * If we are being called because of a process doing a
13825 * copy-on-write, then it is not safe to process any
13826 * worklist items as we will recurse into the copyonwrite
13827 * routine. This will result in an incoherent snapshot.
13828 * If the vnode that we hold is a snapshot, we must avoid
13829 * handling other resources that could cause deadlock.
13831 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13834 if (resource == FLUSH_BLOCKS_WAIT)
13835 stat_cleanup_blkrequests += 1;
13837 stat_cleanup_inorequests += 1;
13840 ump = VFSTOUFS(mp);
13841 mtx_assert(UFS_MTX(ump), MA_OWNED);
13843 error = ffs_update(vp, 1);
13844 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13849 * If we are in need of resources, start by cleaning up
13850 * any block removals associated with our inode.
13853 process_removes(vp);
13854 process_truncates(vp);
13857 * Now clean up at least as many resources as we will need.
13859 * When requested to clean up inodes, the number that are needed
13860 * is set by the number of simultaneous writers (mnt_writeopcount)
13861 * plus a bit of slop (2) in case some more writers show up while
13864 * When requested to free up space, the amount of space that
13865 * we need is enough blocks to allocate a full-sized segment
13866 * (fs_contigsumsize). The number of such segments that will
13867 * be needed is set by the number of simultaneous writers
13868 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13869 * writers show up while we are cleaning.
13871 * Additionally, if we are unpriviledged and allocating space,
13872 * we need to ensure that we clean up enough blocks to get the
13873 * needed number of blocks over the threshold of the minimum
13874 * number of blocks required to be kept free by the filesystem
13877 if (resource == FLUSH_INODES_WAIT) {
13878 needed = vfs_mount_fetch_counter(vp->v_mount,
13879 MNT_COUNT_WRITEOPCOUNT) + 2;
13880 } else if (resource == FLUSH_BLOCKS_WAIT) {
13881 needed = (vfs_mount_fetch_counter(vp->v_mount,
13882 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13883 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13884 needed += fragstoblks(fs,
13885 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13886 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13888 printf("softdep_request_cleanup: Unknown resource type %d\n",
13893 starttime = time_second;
13895 if (resource == FLUSH_BLOCKS_WAIT &&
13896 fs->fs_cstotal.cs_nbfree <= needed)
13897 softdep_send_speedup(ump, needed * fs->fs_bsize,
13899 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13900 fs->fs_cstotal.cs_nbfree <= needed) ||
13901 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13902 fs->fs_cstotal.cs_nifree <= needed)) {
13904 if (ump->softdep_on_worklist > 0 &&
13905 process_worklist_item(UFSTOVFS(ump),
13906 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13907 stat_worklist_push += 1;
13912 * Check that there are vnodes pending inactivation. As they
13913 * have been unlinked, inactivating them will free up their
13917 if (resource == FLUSH_INODES_WAIT &&
13918 fs->fs_cstotal.cs_nifree <= needed &&
13919 fs->fs_pendinginodes <= needed) {
13920 if ((ump->um_softdep->sd_flags & FLUSH_DI_ACTIVE) == 0) {
13921 ump->um_softdep->sd_flags |= FLUSH_DI_ACTIVE;
13923 softdep_request_cleanup_inactivate(mp);
13925 ump->um_softdep->sd_flags &= ~FLUSH_DI_ACTIVE;
13926 wakeup(&ump->um_softdep->sd_flags);
13928 while ((ump->um_softdep->sd_flags &
13929 FLUSH_DI_ACTIVE) != 0) {
13930 msleep(&ump->um_softdep->sd_flags,
13931 LOCK_PTR(ump), PVM, "ffsvina", hz);
13938 * If we still need resources and there are no more worklist
13939 * entries to process to obtain them, we have to start flushing
13940 * the dirty vnodes to force the release of additional requests
13941 * to the worklist that we can then process to reap addition
13942 * resources. We walk the vnodes associated with the mount point
13943 * until we get the needed worklist requests that we can reap.
13945 * If there are several threads all needing to clean the same
13946 * mount point, only one is allowed to walk the mount list.
13947 * When several threads all try to walk the same mount list,
13948 * they end up competing with each other and often end up in
13949 * livelock. This approach ensures that forward progress is
13950 * made at the cost of occational ENOSPC errors being returned
13951 * that might otherwise have been avoided.
13954 if ((resource == FLUSH_BLOCKS_WAIT &&
13955 fs->fs_cstotal.cs_nbfree <= needed) ||
13956 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13957 fs->fs_cstotal.cs_nifree <= needed)) {
13959 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13960 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13962 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13964 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13965 wakeup(&ump->um_softdep->sd_flags);
13967 if (ump->softdep_on_worklist > 0) {
13968 stat_cleanup_retries += 1;
13973 while ((ump->um_softdep->sd_flags &
13974 FLUSH_RC_ACTIVE) != 0) {
13975 msleep(&ump->um_softdep->sd_flags,
13976 LOCK_PTR(ump), PVM, "ffsrca", hz);
13981 stat_cleanup_failures += 1;
13983 if (time_second - starttime > stat_cleanup_high_delay)
13984 stat_cleanup_high_delay = time_second - starttime;
13990 * Scan the vnodes for the specified mount point flushing out any
13991 * vnodes that can be locked without waiting. Finally, try to flush
13992 * the device associated with the mount point if it can be locked
13995 * We return 0 if we were able to lock every vnode in our scan.
13996 * If we had to skip one or more vnodes, we return 1.
13999 softdep_request_cleanup_flush(mp, ump)
14001 struct ufsmount *ump;
14004 struct vnode *lvp, *mvp;
14009 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
14010 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
14014 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) {
14018 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
14022 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
14025 lvp = ump->um_devvp;
14026 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
14027 VOP_FSYNC(lvp, MNT_NOWAIT, td);
14030 return (failed_vnode);
14034 softdep_excess_items(struct ufsmount *ump, int item)
14037 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
14038 return (dep_current[item] > max_softdeps &&
14039 ump->softdep_curdeps[item] > max_softdeps /
14040 stat_flush_threads);
14044 schedule_cleanup(struct mount *mp)
14046 struct ufsmount *ump;
14049 ump = VFSTOUFS(mp);
14053 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
14054 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
14056 * No ast is delivered to kernel threads, so nobody
14057 * would deref the mp. Some kernel threads
14058 * explicitely check for AST, e.g. NFS daemon does
14059 * this in the serving loop.
14063 if (td->td_su != NULL)
14064 vfs_rel(td->td_su);
14068 td->td_flags |= TDF_ASTPENDING;
14073 softdep_ast_cleanup_proc(struct thread *td)
14076 struct ufsmount *ump;
14080 while ((mp = td->td_su) != NULL) {
14082 error = vfs_busy(mp, MBF_NOWAIT);
14086 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
14087 ump = VFSTOUFS(mp);
14091 if (softdep_excess_items(ump, D_INODEDEP)) {
14093 request_cleanup(mp, FLUSH_INODES);
14095 if (softdep_excess_items(ump, D_DIRREM)) {
14097 request_cleanup(mp, FLUSH_BLOCKS);
14100 if (softdep_excess_items(ump, D_NEWBLK) ||
14101 softdep_excess_items(ump, D_ALLOCDIRECT) ||
14102 softdep_excess_items(ump, D_ALLOCINDIR)) {
14103 error = vn_start_write(NULL, &mp,
14107 VFS_SYNC(mp, MNT_WAIT);
14108 vn_finished_write(mp);
14111 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
14117 if ((mp = td->td_su) != NULL) {
14124 * If memory utilization has gotten too high, deliberately slow things
14125 * down and speed up the I/O processing.
14128 request_cleanup(mp, resource)
14132 struct thread *td = curthread;
14133 struct ufsmount *ump;
14135 ump = VFSTOUFS(mp);
14138 * We never hold up the filesystem syncer or buf daemon.
14140 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
14143 * First check to see if the work list has gotten backlogged.
14144 * If it has, co-opt this process to help clean up two entries.
14145 * Because this process may hold inodes locked, we cannot
14146 * handle any remove requests that might block on a locked
14147 * inode as that could lead to deadlock. We set TDP_SOFTDEP
14148 * to avoid recursively processing the worklist.
14150 if (ump->softdep_on_worklist > max_softdeps / 10) {
14151 td->td_pflags |= TDP_SOFTDEP;
14152 process_worklist_item(mp, 2, LK_NOWAIT);
14153 td->td_pflags &= ~TDP_SOFTDEP;
14154 stat_worklist_push += 2;
14158 * Next, we attempt to speed up the syncer process. If that
14159 * is successful, then we allow the process to continue.
14161 if (softdep_speedup(ump) &&
14162 resource != FLUSH_BLOCKS_WAIT &&
14163 resource != FLUSH_INODES_WAIT)
14166 * If we are resource constrained on inode dependencies, try
14167 * flushing some dirty inodes. Otherwise, we are constrained
14168 * by file deletions, so try accelerating flushes of directories
14169 * with removal dependencies. We would like to do the cleanup
14170 * here, but we probably hold an inode locked at this point and
14171 * that might deadlock against one that we try to clean. So,
14172 * the best that we can do is request the syncer daemon to do
14173 * the cleanup for us.
14175 switch (resource) {
14177 case FLUSH_INODES_WAIT:
14178 ACQUIRE_GBLLOCK(&lk);
14179 stat_ino_limit_push += 1;
14180 req_clear_inodedeps += 1;
14182 stat_countp = &stat_ino_limit_hit;
14186 case FLUSH_BLOCKS_WAIT:
14187 ACQUIRE_GBLLOCK(&lk);
14188 stat_blk_limit_push += 1;
14189 req_clear_remove += 1;
14191 stat_countp = &stat_blk_limit_hit;
14195 panic("request_cleanup: unknown type");
14198 * Hopefully the syncer daemon will catch up and awaken us.
14199 * We wait at most tickdelay before proceeding in any case.
14201 ACQUIRE_GBLLOCK(&lk);
14204 if (callout_pending(&softdep_callout) == FALSE)
14205 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
14208 if ((td->td_pflags & TDP_KTHREAD) == 0)
14209 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
14217 * Awaken processes pausing in request_cleanup and clear proc_waiting
14218 * to indicate that there is no longer a timer running. Pause_timer
14219 * will be called with the global softdep mutex (&lk) locked.
14226 GBLLOCK_OWNED(&lk);
14228 * The callout_ API has acquired mtx and will hold it around this
14231 *stat_countp += proc_waiting;
14232 wakeup(&proc_waiting);
14236 * If requested, try removing inode or removal dependencies.
14239 check_clear_deps(mp)
14242 struct ufsmount *ump;
14246 * Tell the lower layers that any TRIM or WRITE transactions that have
14247 * been delayed for performance reasons should proceed to help alleviate
14248 * the shortage faster. The race between checking req_* and the softdep
14249 * mutex (lk) is fine since this is an advisory operation that at most
14250 * causes deferred work to be done sooner.
14252 ump = VFSTOUFS(mp);
14253 suj_susp = ump->um_softdep->sd_jblocks != NULL &&
14254 ump->softdep_jblocks->jb_suspended;
14255 if (req_clear_remove || req_clear_inodedeps || suj_susp) {
14257 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
14262 * If we are suspended, it may be because of our using
14263 * too many inodedeps, so help clear them out.
14266 clear_inodedeps(mp);
14269 * General requests for cleanup of backed up dependencies
14271 ACQUIRE_GBLLOCK(&lk);
14272 if (req_clear_inodedeps) {
14273 req_clear_inodedeps -= 1;
14275 clear_inodedeps(mp);
14276 ACQUIRE_GBLLOCK(&lk);
14277 wakeup(&proc_waiting);
14279 if (req_clear_remove) {
14280 req_clear_remove -= 1;
14283 ACQUIRE_GBLLOCK(&lk);
14284 wakeup(&proc_waiting);
14290 * Flush out a directory with at least one removal dependency in an effort to
14291 * reduce the number of dirrem, freefile, and freeblks dependency structures.
14297 struct pagedep_hashhead *pagedephd;
14298 struct pagedep *pagedep;
14299 struct ufsmount *ump;
14305 ump = VFSTOUFS(mp);
14308 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
14309 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
14310 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
14311 ump->pagedep_nextclean = 0;
14312 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
14313 if (LIST_EMPTY(&pagedep->pd_dirremhd))
14315 ino = pagedep->pd_ino;
14316 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14321 * Let unmount clear deps
14323 error = vfs_busy(mp, MBF_NOWAIT);
14326 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14327 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP);
14330 softdep_error("clear_remove: vget", error);
14333 MPASS(VTOI(vp)->i_mode != 0);
14334 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14335 softdep_error("clear_remove: fsync", error);
14336 bo = &vp->v_bufobj;
14342 vn_finished_write(mp);
14350 * Clear out a block of dirty inodes in an effort to reduce
14351 * the number of inodedep dependency structures.
14354 clear_inodedeps(mp)
14357 struct inodedep_hashhead *inodedephd;
14358 struct inodedep *inodedep;
14359 struct ufsmount *ump;
14363 ino_t firstino, lastino, ino;
14365 ump = VFSTOUFS(mp);
14369 * Pick a random inode dependency to be cleared.
14370 * We will then gather up all the inodes in its block
14371 * that have dependencies and flush them out.
14373 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
14374 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
14375 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
14376 ump->inodedep_nextclean = 0;
14377 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
14380 if (inodedep == NULL)
14383 * Find the last inode in the block with dependencies.
14385 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
14386 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
14387 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
14390 * Asynchronously push all but the last inode with dependencies.
14391 * Synchronously push the last inode with dependencies to ensure
14392 * that the inode block gets written to free up the inodedeps.
14394 for (ino = firstino; ino <= lastino; ino++) {
14395 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
14397 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14400 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
14402 vn_finished_write(mp);
14406 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14407 FFSV_FORCEINSMQ | FFSV_FORCEINODEDEP)) != 0) {
14408 softdep_error("clear_inodedeps: vget", error);
14410 vn_finished_write(mp);
14415 if (VTOI(vp)->i_mode == 0) {
14417 } else if (ino == lastino) {
14419 error = ffs_syncvnode(vp, MNT_WAIT, 0);
14420 } while (error == ERELOOKUP);
14422 softdep_error("clear_inodedeps: fsync1", error);
14424 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14425 softdep_error("clear_inodedeps: fsync2", error);
14426 BO_LOCK(&vp->v_bufobj);
14428 BO_UNLOCK(&vp->v_bufobj);
14431 vn_finished_write(mp);
14437 softdep_buf_append(bp, wkhd)
14439 struct workhead *wkhd;
14441 struct worklist *wk;
14442 struct ufsmount *ump;
14444 if ((wk = LIST_FIRST(wkhd)) == NULL)
14446 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14447 ("softdep_buf_append called on non-softdep filesystem"));
14448 ump = VFSTOUFS(wk->wk_mp);
14450 while ((wk = LIST_FIRST(wkhd)) != NULL) {
14451 WORKLIST_REMOVE(wk);
14452 WORKLIST_INSERT(&bp->b_dep, wk);
14459 softdep_inode_append(ip, cred, wkhd)
14461 struct ucred *cred;
14462 struct workhead *wkhd;
14466 struct ufsmount *ump;
14470 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14471 ("softdep_inode_append called on non-softdep filesystem"));
14473 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14474 (int)fs->fs_bsize, cred, &bp);
14477 softdep_freework(wkhd);
14480 softdep_buf_append(bp, wkhd);
14485 softdep_freework(wkhd)
14486 struct workhead *wkhd;
14488 struct worklist *wk;
14489 struct ufsmount *ump;
14491 if ((wk = LIST_FIRST(wkhd)) == NULL)
14493 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14494 ("softdep_freework called on non-softdep filesystem"));
14495 ump = VFSTOUFS(wk->wk_mp);
14497 handle_jwork(wkhd);
14501 static struct ufsmount *
14502 softdep_bp_to_mp(bp)
14508 if (LIST_EMPTY(&bp->b_dep))
14511 KASSERT(vp != NULL,
14512 ("%s, buffer with dependencies lacks vnode", __func__));
14515 * The ump mount point is stable after we get a correct
14516 * pointer, since bp is locked and this prevents unmount from
14517 * proceeding. But to get to it, we cannot dereference bp->b_dep
14518 * head wk_mp, because we do not yet own SU ump lock and
14519 * workitem might be freed while dereferenced.
14522 switch (vp->v_type) {
14525 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14538 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14546 vn_printf(vp, "unknown vnode type");
14550 return (VFSTOUFS(mp));
14554 * Function to determine if the buffer has outstanding dependencies
14555 * that will cause a roll-back if the buffer is written. If wantcount
14556 * is set, return number of dependencies, otherwise just yes or no.
14559 softdep_count_dependencies(bp, wantcount)
14563 struct worklist *wk;
14564 struct ufsmount *ump;
14565 struct bmsafemap *bmsafemap;
14566 struct freework *freework;
14567 struct inodedep *inodedep;
14568 struct indirdep *indirdep;
14569 struct freeblks *freeblks;
14570 struct allocindir *aip;
14571 struct pagedep *pagedep;
14572 struct dirrem *dirrem;
14573 struct newblk *newblk;
14574 struct mkdir *mkdir;
14575 struct diradd *dap;
14578 ump = softdep_bp_to_mp(bp);
14583 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14584 switch (wk->wk_type) {
14586 inodedep = WK_INODEDEP(wk);
14587 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14588 /* bitmap allocation dependency */
14593 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14594 /* direct block pointer dependency */
14599 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14600 /* direct block pointer dependency */
14605 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14606 /* Add reference dependency. */
14614 indirdep = WK_INDIRDEP(wk);
14616 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14617 /* indirect truncation dependency */
14623 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14624 /* indirect block pointer dependency */
14632 pagedep = WK_PAGEDEP(wk);
14633 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14634 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14635 /* Journal remove ref dependency. */
14641 for (i = 0; i < DAHASHSZ; i++) {
14642 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14643 /* directory entry dependency */
14652 bmsafemap = WK_BMSAFEMAP(wk);
14653 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14654 /* Add reference dependency. */
14659 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14660 /* Allocate block dependency. */
14668 freeblks = WK_FREEBLKS(wk);
14669 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14670 /* Freeblk journal dependency. */
14677 case D_ALLOCDIRECT:
14679 newblk = WK_NEWBLK(wk);
14680 if (newblk->nb_jnewblk) {
14681 /* Journal allocate dependency. */
14689 mkdir = WK_MKDIR(wk);
14690 if (mkdir->md_jaddref) {
14691 /* Journal reference dependency. */
14703 /* never a dependency on these blocks */
14707 panic("softdep_count_dependencies: Unexpected type %s",
14708 TYPENAME(wk->wk_type));
14718 * Acquire exclusive access to a buffer.
14719 * Must be called with a locked mtx parameter.
14720 * Return acquired buffer or NULL on failure.
14722 static struct buf *
14723 getdirtybuf(bp, lock, waitfor)
14725 struct rwlock *lock;
14730 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14731 if (waitfor != MNT_WAIT)
14733 error = BUF_LOCK(bp,
14734 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14736 * Even if we successfully acquire bp here, we have dropped
14737 * lock, which may violates our guarantee.
14741 else if (error != ENOLCK)
14742 panic("getdirtybuf: inconsistent lock: %d", error);
14746 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14747 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14749 BO_LOCK(bp->b_bufobj);
14751 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14752 bp->b_vflags |= BV_BKGRDWAIT;
14753 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14754 PRIBIO | PDROP, "getbuf", 0);
14756 BO_UNLOCK(bp->b_bufobj);
14761 if (waitfor != MNT_WAIT)
14763 #ifdef DEBUG_VFS_LOCKS
14764 if (bp->b_vp->v_type != VCHR)
14765 ASSERT_BO_WLOCKED(bp->b_bufobj);
14767 bp->b_vflags |= BV_BKGRDWAIT;
14768 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14771 if ((bp->b_flags & B_DELWRI) == 0) {
14780 * Check if it is safe to suspend the file system now. On entry,
14781 * the vnode interlock for devvp should be held. Return 0 with
14782 * the mount interlock held if the file system can be suspended now,
14783 * otherwise return EAGAIN with the mount interlock held.
14786 softdep_check_suspend(struct mount *mp,
14787 struct vnode *devvp,
14788 int softdep_depcnt,
14789 int softdep_accdepcnt,
14790 int secondary_writes,
14791 int secondary_accwrites)
14794 struct ufsmount *ump;
14795 struct inodedep *inodedep;
14796 int error, unlinked;
14798 bo = &devvp->v_bufobj;
14799 ASSERT_BO_WLOCKED(bo);
14802 * If we are not running with soft updates, then we need only
14803 * deal with secondary writes as we try to suspend.
14805 if (MOUNTEDSOFTDEP(mp) == 0) {
14807 while (mp->mnt_secondary_writes != 0) {
14809 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14810 (PUSER - 1) | PDROP, "secwr", 0);
14816 * Reasons for needing more work before suspend:
14817 * - Dirty buffers on devvp.
14818 * - Secondary writes occurred after start of vnode sync loop
14821 if (bo->bo_numoutput > 0 ||
14822 bo->bo_dirty.bv_cnt > 0 ||
14823 secondary_writes != 0 ||
14824 mp->mnt_secondary_writes != 0 ||
14825 secondary_accwrites != mp->mnt_secondary_accwrites)
14832 * If we are running with soft updates, then we need to coordinate
14833 * with them as we try to suspend.
14835 ump = VFSTOUFS(mp);
14837 if (!TRY_ACQUIRE_LOCK(ump)) {
14845 if (mp->mnt_secondary_writes != 0) {
14848 msleep(&mp->mnt_secondary_writes,
14850 (PUSER - 1) | PDROP, "secwr", 0);
14858 if (MOUNTEDSUJ(mp)) {
14859 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14861 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14862 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14863 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14865 !check_inodedep_free(inodedep))
14872 * Reasons for needing more work before suspend:
14873 * - Dirty buffers on devvp.
14874 * - Softdep activity occurred after start of vnode sync loop
14875 * - Secondary writes occurred after start of vnode sync loop
14878 if (bo->bo_numoutput > 0 ||
14879 bo->bo_dirty.bv_cnt > 0 ||
14880 softdep_depcnt != unlinked ||
14881 ump->softdep_deps != unlinked ||
14882 softdep_accdepcnt != ump->softdep_accdeps ||
14883 secondary_writes != 0 ||
14884 mp->mnt_secondary_writes != 0 ||
14885 secondary_accwrites != mp->mnt_secondary_accwrites)
14893 * Get the number of dependency structures for the file system, both
14894 * the current number and the total number allocated. These will
14895 * later be used to detect that softdep processing has occurred.
14898 softdep_get_depcounts(struct mount *mp,
14899 int *softdep_depsp,
14900 int *softdep_accdepsp)
14902 struct ufsmount *ump;
14904 if (MOUNTEDSOFTDEP(mp) == 0) {
14905 *softdep_depsp = 0;
14906 *softdep_accdepsp = 0;
14909 ump = VFSTOUFS(mp);
14911 *softdep_depsp = ump->softdep_deps;
14912 *softdep_accdepsp = ump->softdep_accdeps;
14917 * Wait for pending output on a vnode to complete.
14924 ASSERT_VOP_LOCKED(vp, "drain_output");
14925 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14929 * Called whenever a buffer that is being invalidated or reallocated
14930 * contains dependencies. This should only happen if an I/O error has
14931 * occurred. The routine is called with the buffer locked.
14934 softdep_deallocate_dependencies(bp)
14938 if ((bp->b_ioflags & BIO_ERROR) == 0)
14939 panic("softdep_deallocate_dependencies: dangling deps");
14940 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14941 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14943 printf("softdep_deallocate_dependencies: "
14944 "got error %d while accessing filesystem\n", bp->b_error);
14945 if (bp->b_error != ENXIO)
14946 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14950 * Function to handle asynchronous write errors in the filesystem.
14953 softdep_error(func, error)
14958 /* XXX should do something better! */
14959 printf("%s: got error %d while accessing filesystem\n", func, error);
14964 /* exported to ffs_vfsops.c */
14965 extern void db_print_ffs(struct ufsmount *ump);
14967 db_print_ffs(struct ufsmount *ump)
14969 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14970 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14971 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n",
14972 ump->um_fs, ump->softdep_on_worklist,
14973 ump->softdep_deps, ump->softdep_req);
14977 worklist_print(struct worklist *wk, int verbose)
14981 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14982 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
14985 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
14986 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
14987 LIST_NEXT(wk, wk_list));
14988 db_print_ffs(VFSTOUFS(wk->wk_mp));
14992 inodedep_print(struct inodedep *inodedep, int verbose)
14995 worklist_print(&inodedep->id_list, 0);
14996 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14998 (intmax_t)inodedep->id_ino,
14999 (intmax_t)fsbtodb(inodedep->id_fs,
15000 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
15001 (intmax_t)inodedep->id_nlinkdelta,
15002 (intmax_t)inodedep->id_savednlink);
15007 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
15008 inodedep->id_bmsafemap,
15009 inodedep->id_mkdiradd,
15010 TAILQ_FIRST(&inodedep->id_inoreflst));
15011 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
15012 LIST_FIRST(&inodedep->id_dirremhd),
15013 LIST_FIRST(&inodedep->id_pendinghd),
15014 LIST_FIRST(&inodedep->id_bufwait));
15015 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
15016 LIST_FIRST(&inodedep->id_inowait),
15017 TAILQ_FIRST(&inodedep->id_inoupdt),
15018 TAILQ_FIRST(&inodedep->id_newinoupdt));
15019 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
15020 TAILQ_FIRST(&inodedep->id_extupdt),
15021 TAILQ_FIRST(&inodedep->id_newextupdt),
15022 TAILQ_FIRST(&inodedep->id_freeblklst));
15023 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
15024 inodedep->id_savedino1,
15025 (intmax_t)inodedep->id_savedsize,
15026 (intmax_t)inodedep->id_savedextsize);
15030 newblk_print(struct newblk *nbp)
15033 worklist_print(&nbp->nb_list, 0);
15034 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
15035 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
15037 &nbp->nb_bmsafemap,
15038 &nbp->nb_freefrag);
15039 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
15040 LIST_FIRST(&nbp->nb_indirdeps),
15041 LIST_FIRST(&nbp->nb_newdirblk),
15042 LIST_FIRST(&nbp->nb_jwork));
15046 allocdirect_print(struct allocdirect *adp)
15049 newblk_print(&adp->ad_block);
15050 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
15051 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
15052 db_printf(" offset %d, inodedep %p\n",
15053 adp->ad_offset, adp->ad_inodedep);
15057 allocindir_print(struct allocindir *aip)
15060 newblk_print(&aip->ai_block);
15061 db_printf(" oldblkno %jd, lbn %jd\n",
15062 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
15063 db_printf(" offset %d, indirdep %p\n",
15064 aip->ai_offset, aip->ai_indirdep);
15068 mkdir_print(struct mkdir *mkdir)
15071 worklist_print(&mkdir->md_list, 0);
15072 db_printf(" diradd %p, jaddref %p, buf %p\n",
15073 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
15076 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
15079 if (have_addr == 0) {
15080 db_printf("inodedep address required\n");
15083 inodedep_print((struct inodedep*)addr, 1);
15086 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
15088 struct inodedep_hashhead *inodedephd;
15089 struct inodedep *inodedep;
15090 struct ufsmount *ump;
15093 if (have_addr == 0) {
15094 db_printf("ufsmount address required\n");
15097 ump = (struct ufsmount *)addr;
15098 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
15099 inodedephd = &ump->inodedep_hashtbl[cnt];
15100 LIST_FOREACH(inodedep, inodedephd, id_hash) {
15101 inodedep_print(inodedep, 0);
15106 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
15109 if (have_addr == 0) {
15110 db_printf("worklist address required\n");
15113 worklist_print((struct worklist *)addr, 1);
15116 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
15118 struct worklist *wk;
15119 struct workhead *wkhd;
15121 if (have_addr == 0) {
15122 db_printf("worklist address required "
15123 "(for example value in bp->b_dep)\n");
15127 * We often do not have the address of the worklist head but
15128 * instead a pointer to its first entry (e.g., we have the
15129 * contents of bp->b_dep rather than &bp->b_dep). But the back
15130 * pointer of bp->b_dep will point at the head of the list, so
15131 * we cheat and use that instead. If we are in the middle of
15132 * a list we will still get the same result, so nothing
15133 * unexpected will result.
15135 wk = (struct worklist *)addr;
15138 wkhd = (struct workhead *)wk->wk_list.le_prev;
15139 LIST_FOREACH(wk, wkhd, wk_list) {
15140 switch(wk->wk_type) {
15142 inodedep_print(WK_INODEDEP(wk), 0);
15144 case D_ALLOCDIRECT:
15145 allocdirect_print(WK_ALLOCDIRECT(wk));
15148 allocindir_print(WK_ALLOCINDIR(wk));
15151 mkdir_print(WK_MKDIR(wk));
15154 worklist_print(wk, 0);
15160 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
15162 if (have_addr == 0) {
15163 db_printf("mkdir address required\n");
15166 mkdir_print((struct mkdir *)addr);
15169 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
15171 struct mkdirlist *mkdirlisthd;
15172 struct mkdir *mkdir;
15174 if (have_addr == 0) {
15175 db_printf("mkdir listhead address required\n");
15178 mkdirlisthd = (struct mkdirlist *)addr;
15179 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
15180 mkdir_print(mkdir);
15181 if (mkdir->md_diradd != NULL) {
15183 worklist_print(&mkdir->md_diradd->da_list, 0);
15185 if (mkdir->md_jaddref != NULL) {
15187 worklist_print(&mkdir->md_jaddref->ja_list, 0);
15192 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
15194 if (have_addr == 0) {
15195 db_printf("allocdirect address required\n");
15198 allocdirect_print((struct allocdirect *)addr);
15201 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
15203 if (have_addr == 0) {
15204 db_printf("allocindir address required\n");
15207 allocindir_print((struct allocindir *)addr);
15212 #endif /* SOFTUPDATES */