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");
614 FEATURE(softupdates, "FFS soft-updates support");
616 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
617 "soft updates stats");
618 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total,
619 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
620 "total dependencies allocated");
621 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse,
622 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
623 "high use dependencies allocated");
624 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current,
625 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
626 "current dependencies allocated");
627 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write,
628 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
629 "current dependencies written");
631 unsigned long dep_current[D_LAST + 1];
632 unsigned long dep_highuse[D_LAST + 1];
633 unsigned long dep_total[D_LAST + 1];
634 unsigned long dep_write[D_LAST + 1];
636 #define SOFTDEP_TYPE(type, str, long) \
637 static MALLOC_DEFINE(M_ ## type, #str, long); \
638 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \
639 &dep_total[D_ ## type], 0, ""); \
640 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \
641 &dep_current[D_ ## type], 0, ""); \
642 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \
643 &dep_highuse[D_ ## type], 0, ""); \
644 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \
645 &dep_write[D_ ## type], 0, "");
647 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
648 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
649 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
650 "Block or frag allocated from cyl group map");
651 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
652 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
653 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
654 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
655 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
656 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
657 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
658 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
659 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
660 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
661 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
662 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
663 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
664 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
665 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
666 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
667 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
668 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
669 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
670 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
671 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
672 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
673 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
674 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
676 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
678 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
679 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
680 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
682 #define M_SOFTDEP_FLAGS (M_WAITOK)
685 * translate from workitem type to memory type
686 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
688 static struct malloc_type *memtype[] = {
720 #define DtoM(type) (memtype[type])
723 * Names of malloc types.
725 #define TYPENAME(type) \
726 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
727 memtype[type]->ks_shortdesc : "???")
729 * End system adaptation definitions.
732 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
733 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
736 * Internal function prototypes.
738 static void check_clear_deps(struct mount *);
739 static void softdep_error(char *, int);
740 static int softdep_process_worklist(struct mount *, int);
741 static int softdep_waitidle(struct mount *, int);
742 static void drain_output(struct vnode *);
743 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
744 static int check_inodedep_free(struct inodedep *);
745 static void clear_remove(struct mount *);
746 static void clear_inodedeps(struct mount *);
747 static void unlinked_inodedep(struct mount *, struct inodedep *);
748 static void clear_unlinked_inodedep(struct inodedep *);
749 static struct inodedep *first_unlinked_inodedep(struct ufsmount *);
750 static int flush_pagedep_deps(struct vnode *, struct mount *,
752 static int free_pagedep(struct pagedep *);
753 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
754 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
755 static int flush_deplist(struct allocdirectlst *, int, int *);
756 static int sync_cgs(struct mount *, int);
757 static int handle_written_filepage(struct pagedep *, struct buf *, int);
758 static int handle_written_sbdep(struct sbdep *, struct buf *);
759 static void initiate_write_sbdep(struct sbdep *);
760 static void diradd_inode_written(struct diradd *, struct inodedep *);
761 static int handle_written_indirdep(struct indirdep *, struct buf *,
763 static int handle_written_inodeblock(struct inodedep *, struct buf *, int);
764 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
766 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
767 static void handle_written_jaddref(struct jaddref *);
768 static void handle_written_jremref(struct jremref *);
769 static void handle_written_jseg(struct jseg *, struct buf *);
770 static void handle_written_jnewblk(struct jnewblk *);
771 static void handle_written_jblkdep(struct jblkdep *);
772 static void handle_written_jfreefrag(struct jfreefrag *);
773 static void complete_jseg(struct jseg *);
774 static void complete_jsegs(struct jseg *);
775 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
776 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
777 static void jremref_write(struct jremref *, struct jseg *, uint8_t *);
778 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
779 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
780 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
781 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
782 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
783 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
784 static inline void inoref_write(struct inoref *, struct jseg *,
786 static void handle_allocdirect_partdone(struct allocdirect *,
788 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
790 static void indirdep_complete(struct indirdep *);
791 static int indirblk_lookup(struct mount *, ufs2_daddr_t);
792 static void indirblk_insert(struct freework *);
793 static void indirblk_remove(struct freework *);
794 static void handle_allocindir_partdone(struct allocindir *);
795 static void initiate_write_filepage(struct pagedep *, struct buf *);
796 static void initiate_write_indirdep(struct indirdep*, struct buf *);
797 static void handle_written_mkdir(struct mkdir *, int);
798 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
800 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
801 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
802 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
803 static void handle_workitem_freefile(struct freefile *);
804 static int handle_workitem_remove(struct dirrem *, int);
805 static struct dirrem *newdirrem(struct buf *, struct inode *,
806 struct inode *, int, struct dirrem **);
807 static struct indirdep *indirdep_lookup(struct mount *, struct inode *,
809 static void cancel_indirdep(struct indirdep *, struct buf *,
811 static void free_indirdep(struct indirdep *);
812 static void free_diradd(struct diradd *, struct workhead *);
813 static void merge_diradd(struct inodedep *, struct diradd *);
814 static void complete_diradd(struct diradd *);
815 static struct diradd *diradd_lookup(struct pagedep *, int);
816 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
818 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
820 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
821 struct jremref *, struct jremref *);
822 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
824 static void cancel_allocindir(struct allocindir *, struct buf *bp,
825 struct freeblks *, int);
826 static int setup_trunc_indir(struct freeblks *, struct inode *,
827 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
828 static void complete_trunc_indir(struct freework *);
829 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
831 static void complete_mkdir(struct mkdir *);
832 static void free_newdirblk(struct newdirblk *);
833 static void free_jremref(struct jremref *);
834 static void free_jaddref(struct jaddref *);
835 static void free_jsegdep(struct jsegdep *);
836 static void free_jsegs(struct jblocks *);
837 static void rele_jseg(struct jseg *);
838 static void free_jseg(struct jseg *, struct jblocks *);
839 static void free_jnewblk(struct jnewblk *);
840 static void free_jblkdep(struct jblkdep *);
841 static void free_jfreefrag(struct jfreefrag *);
842 static void free_freedep(struct freedep *);
843 static void journal_jremref(struct dirrem *, struct jremref *,
845 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
846 static int cancel_jaddref(struct jaddref *, struct inodedep *,
848 static void cancel_jfreefrag(struct jfreefrag *);
849 static inline void setup_freedirect(struct freeblks *, struct inode *,
851 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
852 static inline void setup_freeindir(struct freeblks *, struct inode *, int,
854 static inline struct freeblks *newfreeblks(struct mount *, struct inode *);
855 static void freeblks_free(struct ufsmount *, struct freeblks *, int);
856 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
857 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
858 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
859 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
861 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
862 static int cancel_pagedep(struct pagedep *, struct freeblks *, int);
863 static int deallocate_dependencies(struct buf *, struct freeblks *, int);
864 static void newblk_freefrag(struct newblk*);
865 static void free_newblk(struct newblk *);
866 static void cancel_allocdirect(struct allocdirectlst *,
867 struct allocdirect *, struct freeblks *);
868 static int check_inode_unwritten(struct inodedep *);
869 static int free_inodedep(struct inodedep *);
870 static void freework_freeblock(struct freework *, u_long);
871 static void freework_enqueue(struct freework *);
872 static int handle_workitem_freeblocks(struct freeblks *, int);
873 static int handle_complete_freeblocks(struct freeblks *, int);
874 static void handle_workitem_indirblk(struct freework *);
875 static void handle_written_freework(struct freework *);
876 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
877 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
879 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
880 struct inodedep *, struct allocindir *, ufs_lbn_t);
881 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
882 ufs2_daddr_t, ufs_lbn_t);
883 static void handle_workitem_freefrag(struct freefrag *);
884 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
886 static void allocdirect_merge(struct allocdirectlst *,
887 struct allocdirect *, struct allocdirect *);
888 static struct freefrag *allocindir_merge(struct allocindir *,
889 struct allocindir *);
890 static int bmsafemap_find(struct bmsafemap_hashhead *, int,
891 struct bmsafemap **);
892 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
893 int cg, struct bmsafemap *);
894 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
896 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
897 static int inodedep_find(struct inodedep_hashhead *, ino_t,
899 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
900 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
901 int, struct pagedep **);
902 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
904 static void pause_timer(void *);
905 static int request_cleanup(struct mount *, int);
906 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
907 static void schedule_cleanup(struct mount *);
908 static void softdep_ast_cleanup_proc(struct thread *);
909 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
910 static int process_worklist_item(struct mount *, int, int);
911 static void process_removes(struct vnode *);
912 static void process_truncates(struct vnode *);
913 static void jwork_move(struct workhead *, struct workhead *);
914 static void jwork_insert(struct workhead *, struct jsegdep *);
915 static void add_to_worklist(struct worklist *, int);
916 static void wake_worklist(struct worklist *);
917 static void wait_worklist(struct worklist *, char *);
918 static void remove_from_worklist(struct worklist *);
919 static void softdep_flush(void *);
920 static void softdep_flushjournal(struct mount *);
921 static int softdep_speedup(struct ufsmount *);
922 static void worklist_speedup(struct mount *);
923 static int journal_mount(struct mount *, struct fs *, struct ucred *);
924 static void journal_unmount(struct ufsmount *);
925 static int journal_space(struct ufsmount *, int);
926 static void journal_suspend(struct ufsmount *);
927 static int journal_unsuspend(struct ufsmount *ump);
928 static void softdep_prelink(struct vnode *, struct vnode *);
929 static void add_to_journal(struct worklist *);
930 static void remove_from_journal(struct worklist *);
931 static bool softdep_excess_items(struct ufsmount *, int);
932 static void softdep_process_journal(struct mount *, struct worklist *, int);
933 static struct jremref *newjremref(struct dirrem *, struct inode *,
934 struct inode *ip, off_t, nlink_t);
935 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
937 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
939 static inline struct jsegdep *inoref_jseg(struct inoref *);
940 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
941 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
943 static void adjust_newfreework(struct freeblks *, int);
944 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
945 static void move_newblock_dep(struct jaddref *, struct inodedep *);
946 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
947 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
948 ufs2_daddr_t, long, ufs_lbn_t);
949 static struct freework *newfreework(struct ufsmount *, struct freeblks *,
950 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
951 static int jwait(struct worklist *, int);
952 static struct inodedep *inodedep_lookup_ip(struct inode *);
953 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
954 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
955 static void handle_jwork(struct workhead *);
956 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
958 static struct jblocks *jblocks_create(void);
959 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
960 static void jblocks_free(struct jblocks *, struct mount *, int);
961 static void jblocks_destroy(struct jblocks *);
962 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
965 * Exported softdep operations.
967 static void softdep_disk_io_initiation(struct buf *);
968 static void softdep_disk_write_complete(struct buf *);
969 static void softdep_deallocate_dependencies(struct buf *);
970 static int softdep_count_dependencies(struct buf *bp, int);
973 * Global lock over all of soft updates.
975 static struct mtx lk;
976 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF);
978 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk)
979 #define FREE_GBLLOCK(lk) mtx_unlock(lk)
980 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED)
983 * Per-filesystem soft-updates locking.
985 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock)
986 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock)
987 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock)
988 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock)
989 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \
992 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock)
993 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock)
996 * Worklist queue management.
997 * These routines require that the lock be held.
999 #ifndef /* NOT */ INVARIANTS
1000 #define WORKLIST_INSERT(head, item) do { \
1001 (item)->wk_state |= ONWORKLIST; \
1002 LIST_INSERT_HEAD(head, item, wk_list); \
1004 #define WORKLIST_REMOVE(item) do { \
1005 (item)->wk_state &= ~ONWORKLIST; \
1006 LIST_REMOVE(item, wk_list); \
1008 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT
1009 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE
1011 #else /* INVARIANTS */
1012 static void worklist_insert(struct workhead *, struct worklist *, int,
1014 static void worklist_remove(struct worklist *, int, const char *, int);
1016 #define WORKLIST_INSERT(head, item) \
1017 worklist_insert(head, item, 1, __func__, __LINE__)
1018 #define WORKLIST_INSERT_UNLOCKED(head, item)\
1019 worklist_insert(head, item, 0, __func__, __LINE__)
1020 #define WORKLIST_REMOVE(item)\
1021 worklist_remove(item, 1, __func__, __LINE__)
1022 #define WORKLIST_REMOVE_UNLOCKED(item)\
1023 worklist_remove(item, 0, __func__, __LINE__)
1026 worklist_insert(head, item, locked, func, line)
1027 struct workhead *head;
1028 struct worklist *item;
1035 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1036 if (item->wk_state & ONWORKLIST)
1037 panic("worklist_insert: %p %s(0x%X) already on list, "
1038 "added in function %s at line %d",
1039 item, TYPENAME(item->wk_type), item->wk_state,
1040 item->wk_func, item->wk_line);
1041 item->wk_state |= ONWORKLIST;
1042 item->wk_func = func;
1043 item->wk_line = line;
1044 LIST_INSERT_HEAD(head, item, wk_list);
1048 worklist_remove(item, locked, func, line)
1049 struct worklist *item;
1056 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1057 if ((item->wk_state & ONWORKLIST) == 0)
1058 panic("worklist_remove: %p %s(0x%X) not on list, "
1059 "removed 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_REMOVE(item, wk_list);
1067 #endif /* INVARIANTS */
1070 * Merge two jsegdeps keeping only the oldest one as newer references
1071 * can't be discarded until after older references.
1073 static inline struct jsegdep *
1074 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1076 struct jsegdep *swp;
1081 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1086 WORKLIST_REMOVE(&two->jd_list);
1093 * If two freedeps are compatible free one to reduce list size.
1095 static inline struct freedep *
1096 freedep_merge(struct freedep *one, struct freedep *two)
1101 if (one->fd_freework == two->fd_freework) {
1102 WORKLIST_REMOVE(&two->fd_list);
1109 * Move journal work from one list to another. Duplicate freedeps and
1110 * jsegdeps are coalesced to keep the lists as small as possible.
1113 jwork_move(dst, src)
1114 struct workhead *dst;
1115 struct workhead *src;
1117 struct freedep *freedep;
1118 struct jsegdep *jsegdep;
1119 struct worklist *wkn;
1120 struct worklist *wk;
1123 ("jwork_move: dst == src"));
1126 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1127 if (wk->wk_type == D_JSEGDEP)
1128 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1129 else if (wk->wk_type == D_FREEDEP)
1130 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1133 while ((wk = LIST_FIRST(src)) != NULL) {
1134 WORKLIST_REMOVE(wk);
1135 WORKLIST_INSERT(dst, wk);
1136 if (wk->wk_type == D_JSEGDEP) {
1137 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1140 if (wk->wk_type == D_FREEDEP)
1141 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1146 jwork_insert(dst, jsegdep)
1147 struct workhead *dst;
1148 struct jsegdep *jsegdep;
1150 struct jsegdep *jsegdepn;
1151 struct worklist *wk;
1153 LIST_FOREACH(wk, dst, wk_list)
1154 if (wk->wk_type == D_JSEGDEP)
1157 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1160 jsegdepn = WK_JSEGDEP(wk);
1161 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1162 WORKLIST_REMOVE(wk);
1163 free_jsegdep(jsegdepn);
1164 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1166 free_jsegdep(jsegdep);
1170 * Routines for tracking and managing workitems.
1172 static void workitem_free(struct worklist *, int);
1173 static void workitem_alloc(struct worklist *, int, struct mount *);
1174 static void workitem_reassign(struct worklist *, int);
1176 #define WORKITEM_FREE(item, type) \
1177 workitem_free((struct worklist *)(item), (type))
1178 #define WORKITEM_REASSIGN(item, type) \
1179 workitem_reassign((struct worklist *)(item), (type))
1182 workitem_free(item, type)
1183 struct worklist *item;
1186 struct ufsmount *ump;
1189 if (item->wk_state & ONWORKLIST)
1190 panic("workitem_free: %s(0x%X) still on list, "
1191 "added in function %s at line %d",
1192 TYPENAME(item->wk_type), item->wk_state,
1193 item->wk_func, item->wk_line);
1194 if (item->wk_type != type && type != D_NEWBLK)
1195 panic("workitem_free: type mismatch %s != %s",
1196 TYPENAME(item->wk_type), TYPENAME(type));
1198 if (item->wk_state & IOWAITING)
1200 ump = VFSTOUFS(item->wk_mp);
1202 KASSERT(ump->softdep_deps > 0,
1203 ("workitem_free: %s: softdep_deps going negative",
1204 ump->um_fs->fs_fsmnt));
1205 if (--ump->softdep_deps == 0 && ump->softdep_req)
1206 wakeup(&ump->softdep_deps);
1207 KASSERT(dep_current[item->wk_type] > 0,
1208 ("workitem_free: %s: dep_current[%s] going negative",
1209 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1210 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1211 ("workitem_free: %s: softdep_curdeps[%s] going negative",
1212 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1213 atomic_subtract_long(&dep_current[item->wk_type], 1);
1214 ump->softdep_curdeps[item->wk_type] -= 1;
1216 LIST_REMOVE(item, wk_all);
1218 free(item, DtoM(type));
1222 workitem_alloc(item, type, mp)
1223 struct worklist *item;
1227 struct ufsmount *ump;
1229 item->wk_type = type;
1234 ACQUIRE_GBLLOCK(&lk);
1235 dep_current[type]++;
1236 if (dep_current[type] > dep_highuse[type])
1237 dep_highuse[type] = dep_current[type];
1241 ump->softdep_curdeps[type] += 1;
1242 ump->softdep_deps++;
1243 ump->softdep_accdeps++;
1245 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all);
1251 workitem_reassign(item, newtype)
1252 struct worklist *item;
1255 struct ufsmount *ump;
1257 ump = VFSTOUFS(item->wk_mp);
1259 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1260 ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1261 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1262 ump->softdep_curdeps[item->wk_type] -= 1;
1263 ump->softdep_curdeps[newtype] += 1;
1264 KASSERT(dep_current[item->wk_type] > 0,
1265 ("workitem_reassign: %s: dep_current[%s] going negative",
1266 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1267 ACQUIRE_GBLLOCK(&lk);
1268 dep_current[newtype]++;
1269 dep_current[item->wk_type]--;
1270 if (dep_current[newtype] > dep_highuse[newtype])
1271 dep_highuse[newtype] = dep_current[newtype];
1272 dep_total[newtype]++;
1274 item->wk_type = newtype;
1278 * Workitem queue management
1280 static int max_softdeps; /* maximum number of structs before slowdown */
1281 static int tickdelay = 2; /* number of ticks to pause during slowdown */
1282 static int proc_waiting; /* tracks whether we have a timeout posted */
1283 static int *stat_countp; /* statistic to count in proc_waiting timeout */
1284 static struct callout softdep_callout;
1285 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
1286 static int req_clear_remove; /* syncer process flush some freeblks */
1287 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1290 * runtime statistics
1292 static int stat_flush_threads; /* number of softdep flushing threads */
1293 static int stat_worklist_push; /* number of worklist cleanups */
1294 static int stat_blk_limit_push; /* number of times block limit neared */
1295 static int stat_ino_limit_push; /* number of times inode limit neared */
1296 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
1297 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
1298 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
1299 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
1300 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
1301 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1302 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
1303 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */
1304 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */
1305 static int stat_journal_min; /* Times hit journal min threshold */
1306 static int stat_journal_low; /* Times hit journal low threshold */
1307 static int stat_journal_wait; /* Times blocked in jwait(). */
1308 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */
1309 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */
1310 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */
1311 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */
1312 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1313 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1314 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1315 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1316 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1317 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1319 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1320 &max_softdeps, 0, "");
1321 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1323 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1324 &stat_flush_threads, 0, "");
1325 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push,
1326 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,"");
1327 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push,
1328 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,"");
1329 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push,
1330 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,"");
1331 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit,
1332 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, "");
1333 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit,
1334 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, "");
1335 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit,
1336 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, "");
1337 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs,
1338 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, "");
1339 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap,
1340 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, "");
1341 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs,
1342 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, "");
1343 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry,
1344 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, "");
1345 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback,
1346 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, "");
1347 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback,
1348 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, "");
1349 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low,
1350 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, "");
1351 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min,
1352 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, "");
1353 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait,
1354 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, "");
1355 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage,
1356 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, "");
1357 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks,
1358 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, "");
1359 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode,
1360 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, "");
1361 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk,
1362 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, "");
1363 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests,
1364 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, "");
1365 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests,
1366 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, "");
1367 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay,
1368 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, "");
1369 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries,
1370 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, "");
1371 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures,
1372 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, "");
1374 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1375 &softdep_flushcache, 0, "");
1376 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1377 &stat_emptyjblocks, 0, "");
1379 SYSCTL_DECL(_vfs_ffs);
1381 /* Whether to recompute the summary at mount time */
1382 static int compute_summary_at_mount = 0;
1383 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1384 &compute_summary_at_mount, 0, "Recompute summary at mount");
1385 static int print_threads = 0;
1386 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1387 &print_threads, 0, "Notify flusher thread start/stop");
1389 /* List of all filesystems mounted with soft updates */
1390 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1393 * This function cleans the worklist for a filesystem.
1394 * Each filesystem running with soft dependencies gets its own
1395 * thread to run in this function. The thread is started up in
1396 * softdep_mount and shutdown in softdep_unmount. They show up
1397 * as part of the kernel "bufdaemon" process whose process
1398 * entry is available in bufdaemonproc.
1400 static int searchfailed;
1401 extern struct proc *bufdaemonproc;
1408 struct ufsmount *ump;
1411 td->td_pflags |= TDP_NORUNNINGBUF;
1412 mp = (struct mount *)addr;
1414 atomic_add_int(&stat_flush_threads, 1);
1416 ump->softdep_flags &= ~FLUSH_STARTING;
1417 wakeup(&ump->softdep_flushtd);
1419 if (print_threads) {
1420 if (stat_flush_threads == 1)
1421 printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1422 bufdaemonproc->p_pid);
1423 printf("Start thread %s\n", td->td_name);
1426 while (softdep_process_worklist(mp, 0) > 0 ||
1428 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1429 kthread_suspend_check();
1431 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1432 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1434 ump->softdep_flags &= ~FLUSH_CLEANUP;
1436 * Check to see if we are done and need to exit.
1438 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1442 ump->softdep_flags &= ~FLUSH_EXIT;
1444 wakeup(&ump->softdep_flags);
1446 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups);
1447 atomic_subtract_int(&stat_flush_threads, 1);
1449 panic("kthread_exit failed\n");
1454 worklist_speedup(mp)
1457 struct ufsmount *ump;
1461 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1462 ump->softdep_flags |= FLUSH_CLEANUP;
1463 wakeup(&ump->softdep_flushtd);
1467 softdep_send_speedup(struct ufsmount *ump, off_t shortage, u_int flags)
1471 if ((ump->um_flags & UM_CANSPEEDUP) == 0)
1474 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO);
1475 bp->b_iocmd = BIO_SPEEDUP;
1476 bp->b_ioflags = flags;
1477 bp->b_bcount = omin(shortage, LONG_MAX);
1478 g_vfs_strategy(ump->um_bo, bp);
1484 softdep_speedup(ump)
1485 struct ufsmount *ump;
1487 struct ufsmount *altump;
1488 struct mount_softdeps *sdp;
1491 worklist_speedup(ump->um_mountp);
1494 * If we have global shortages, then we need other
1495 * filesystems to help with the cleanup. Here we wakeup a
1496 * flusher thread for a filesystem that is over its fair
1497 * share of resources.
1499 if (req_clear_inodedeps || req_clear_remove) {
1500 ACQUIRE_GBLLOCK(&lk);
1501 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1502 if ((altump = sdp->sd_ump) == ump)
1504 if (((req_clear_inodedeps &&
1505 altump->softdep_curdeps[D_INODEDEP] >
1506 max_softdeps / stat_flush_threads) ||
1507 (req_clear_remove &&
1508 altump->softdep_curdeps[D_DIRREM] >
1509 (max_softdeps / 2) / stat_flush_threads)) &&
1510 TRY_ACQUIRE_LOCK(altump))
1518 * Move to the end of the list so we pick a
1519 * different one on out next try.
1521 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1522 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1524 if ((altump->softdep_flags &
1525 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1526 altump->softdep_flags |= FLUSH_CLEANUP;
1527 altump->um_softdep->sd_cleanups++;
1528 wakeup(&altump->softdep_flushtd);
1532 return (speedup_syncer());
1536 * Add an item to the end of the work queue.
1537 * This routine requires that the lock be held.
1538 * This is the only routine that adds items to the list.
1539 * The following routine is the only one that removes items
1540 * and does so in order from first to last.
1543 #define WK_HEAD 0x0001 /* Add to HEAD. */
1544 #define WK_NODELAY 0x0002 /* Process immediately. */
1547 add_to_worklist(wk, flags)
1548 struct worklist *wk;
1551 struct ufsmount *ump;
1553 ump = VFSTOUFS(wk->wk_mp);
1555 if (wk->wk_state & ONWORKLIST)
1556 panic("add_to_worklist: %s(0x%X) already on list",
1557 TYPENAME(wk->wk_type), wk->wk_state);
1558 wk->wk_state |= ONWORKLIST;
1559 if (ump->softdep_on_worklist == 0) {
1560 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1561 ump->softdep_worklist_tail = wk;
1562 } else if (flags & WK_HEAD) {
1563 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1565 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1566 ump->softdep_worklist_tail = wk;
1568 ump->softdep_on_worklist += 1;
1569 if (flags & WK_NODELAY)
1570 worklist_speedup(wk->wk_mp);
1574 * Remove the item to be processed. If we are removing the last
1575 * item on the list, we need to recalculate the tail pointer.
1578 remove_from_worklist(wk)
1579 struct worklist *wk;
1581 struct ufsmount *ump;
1583 ump = VFSTOUFS(wk->wk_mp);
1584 if (ump->softdep_worklist_tail == wk)
1585 ump->softdep_worklist_tail =
1586 (struct worklist *)wk->wk_list.le_prev;
1587 WORKLIST_REMOVE(wk);
1588 ump->softdep_on_worklist -= 1;
1593 struct worklist *wk;
1595 if (wk->wk_state & IOWAITING) {
1596 wk->wk_state &= ~IOWAITING;
1602 wait_worklist(wk, wmesg)
1603 struct worklist *wk;
1606 struct ufsmount *ump;
1608 ump = VFSTOUFS(wk->wk_mp);
1609 wk->wk_state |= IOWAITING;
1610 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1614 * Process that runs once per second to handle items in the background queue.
1616 * Note that we ensure that everything is done in the order in which they
1617 * appear in the queue. The code below depends on this property to ensure
1618 * that blocks of a file are freed before the inode itself is freed. This
1619 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1620 * until all the old ones have been purged from the dependency lists.
1623 softdep_process_worklist(mp, full)
1628 struct ufsmount *ump;
1631 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1632 if (MOUNTEDSOFTDEP(mp) == 0)
1637 starttime = time_second;
1638 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1639 check_clear_deps(mp);
1640 while (ump->softdep_on_worklist > 0) {
1641 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1645 check_clear_deps(mp);
1647 * We do not generally want to stop for buffer space, but if
1648 * we are really being a buffer hog, we will stop and wait.
1650 if (should_yield()) {
1652 kern_yield(PRI_USER);
1657 * Never allow processing to run for more than one
1658 * second. This gives the syncer thread the opportunity
1659 * to pause if appropriate.
1661 if (!full && starttime != time_second)
1665 journal_unsuspend(ump);
1671 * Process all removes associated with a vnode if we are running out of
1672 * journal space. Any other process which attempts to flush these will
1673 * be unable as we have the vnodes locked.
1679 struct inodedep *inodedep;
1680 struct dirrem *dirrem;
1681 struct ufsmount *ump;
1688 inum = VTOI(vp)->i_number;
1691 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1693 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1695 * If another thread is trying to lock this vnode
1696 * it will fail but we must wait for it to do so
1697 * before we can proceed.
1699 if (dirrem->dm_state & INPROGRESS) {
1700 wait_worklist(&dirrem->dm_list, "pwrwait");
1703 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1704 (COMPLETE | ONWORKLIST))
1709 remove_from_worklist(&dirrem->dm_list);
1711 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1712 panic("process_removes: suspended filesystem");
1713 handle_workitem_remove(dirrem, 0);
1714 vn_finished_secondary_write(mp);
1720 * Process all truncations associated with a vnode if we are running out
1721 * of journal space. This is called when the vnode lock is already held
1722 * and no other process can clear the truncation. This function returns
1723 * a value greater than zero if it did any work.
1726 process_truncates(vp)
1729 struct inodedep *inodedep;
1730 struct freeblks *freeblks;
1731 struct ufsmount *ump;
1739 inum = VTOI(vp)->i_number;
1741 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1744 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1745 /* Journal entries not yet written. */
1746 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1748 &freeblks->fb_jblkdephd)->jb_list,
1752 /* Another thread is executing this item. */
1753 if (freeblks->fb_state & INPROGRESS) {
1754 wait_worklist(&freeblks->fb_list, "ptrwait");
1757 /* Freeblks is waiting on a inode write. */
1758 if ((freeblks->fb_state & COMPLETE) == 0) {
1764 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1765 (ALLCOMPLETE | ONWORKLIST)) {
1766 remove_from_worklist(&freeblks->fb_list);
1767 freeblks->fb_state |= INPROGRESS;
1769 if (vn_start_secondary_write(NULL, &mp,
1771 panic("process_truncates: "
1772 "suspended filesystem");
1773 handle_workitem_freeblocks(freeblks, 0);
1774 vn_finished_secondary_write(mp);
1778 if (freeblks->fb_cgwait)
1783 sync_cgs(mp, MNT_WAIT);
1784 ffs_sync_snap(mp, MNT_WAIT);
1788 if (freeblks == NULL)
1795 * Process one item on the worklist.
1798 process_worklist_item(mp, target, flags)
1803 struct worklist sentinel;
1804 struct worklist *wk;
1805 struct ufsmount *ump;
1809 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1811 * If we are being called because of a process doing a
1812 * copy-on-write, then it is not safe to write as we may
1813 * recurse into the copy-on-write routine.
1815 if (curthread->td_pflags & TDP_COWINPROGRESS)
1817 PHOLD(curproc); /* Don't let the stack go away. */
1821 sentinel.wk_mp = NULL;
1822 sentinel.wk_type = D_SENTINEL;
1823 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1824 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1825 wk = LIST_NEXT(&sentinel, wk_list)) {
1826 if (wk->wk_type == D_SENTINEL) {
1827 LIST_REMOVE(&sentinel, wk_list);
1828 LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1831 if (wk->wk_state & INPROGRESS)
1832 panic("process_worklist_item: %p already in progress.",
1834 wk->wk_state |= INPROGRESS;
1835 remove_from_worklist(wk);
1837 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1838 panic("process_worklist_item: suspended filesystem");
1839 switch (wk->wk_type) {
1841 /* removal of a directory entry */
1842 error = handle_workitem_remove(WK_DIRREM(wk), flags);
1846 /* releasing blocks and/or fragments from a file */
1847 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
1852 /* releasing a fragment when replaced as a file grows */
1853 handle_workitem_freefrag(WK_FREEFRAG(wk));
1858 /* releasing an inode when its link count drops to 0 */
1859 handle_workitem_freefile(WK_FREEFILE(wk));
1864 panic("%s_process_worklist: Unknown type %s",
1865 "softdep", TYPENAME(wk->wk_type));
1868 vn_finished_secondary_write(mp);
1871 if (++matchcnt == target)
1876 * We have to retry the worklist item later. Wake up any
1877 * waiters who may be able to complete it immediately and
1878 * add the item back to the head so we don't try to execute
1881 wk->wk_state &= ~INPROGRESS;
1883 add_to_worklist(wk, WK_HEAD);
1885 /* Sentinal could've become the tail from remove_from_worklist. */
1886 if (ump->softdep_worklist_tail == &sentinel)
1887 ump->softdep_worklist_tail =
1888 (struct worklist *)sentinel.wk_list.le_prev;
1889 LIST_REMOVE(&sentinel, wk_list);
1895 * Move dependencies from one buffer to another.
1898 softdep_move_dependencies(oldbp, newbp)
1902 struct worklist *wk, *wktail;
1903 struct ufsmount *ump;
1906 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
1908 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
1909 ("softdep_move_dependencies called on non-softdep filesystem"));
1912 ump = VFSTOUFS(wk->wk_mp);
1914 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
1915 LIST_REMOVE(wk, wk_list);
1916 if (wk->wk_type == D_BMSAFEMAP &&
1917 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
1920 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1922 LIST_INSERT_AFTER(wktail, wk, wk_list);
1931 * Purge the work list of all items associated with a particular mount point.
1934 softdep_flushworklist(oldmnt, countp, td)
1935 struct mount *oldmnt;
1939 struct vnode *devvp;
1940 struct ufsmount *ump;
1944 * Alternately flush the block device associated with the mount
1945 * point and process any dependencies that the flushing
1946 * creates. We continue until no more worklist dependencies
1951 ump = VFSTOUFS(oldmnt);
1952 devvp = ump->um_devvp;
1953 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1955 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1956 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1964 #define SU_WAITIDLE_RETRIES 20
1966 softdep_waitidle(struct mount *mp, int flags __unused)
1968 struct ufsmount *ump;
1969 struct vnode *devvp;
1974 devvp = ump->um_devvp;
1978 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
1979 ump->softdep_req = 1;
1980 KASSERT((flags & FORCECLOSE) == 0 ||
1981 ump->softdep_on_worklist == 0,
1982 ("softdep_waitidle: work added after flush"));
1983 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
1984 "softdeps", 10 * hz);
1985 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1986 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1992 ump->softdep_req = 0;
1993 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
1995 printf("softdep_waitidle: Failed to flush worklist for %p\n",
2003 * Flush all vnodes and worklist items associated with a specified mount point.
2006 softdep_flushfiles(oldmnt, flags, td)
2007 struct mount *oldmnt;
2012 struct ufsmount *ump;
2015 int error, early, depcount, loopcnt, retry_flush_count, retry;
2018 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0,
2019 ("softdep_flushfiles called on non-softdep filesystem"));
2021 retry_flush_count = 3;
2026 * Alternately flush the vnodes associated with the mount
2027 * point and process any dependencies that the flushing
2028 * creates. In theory, this loop can happen at most twice,
2029 * but we give it a few extra just to be sure.
2031 for (; loopcnt > 0; loopcnt--) {
2033 * Do another flush in case any vnodes were brought in
2034 * as part of the cleanup operations.
2036 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2037 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2038 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2040 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2045 * If we are unmounting then it is an error to fail. If we
2046 * are simply trying to downgrade to read-only, then filesystem
2047 * activity can keep us busy forever, so we just fail with EBUSY.
2050 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2051 panic("softdep_flushfiles: looping");
2055 error = softdep_waitidle(oldmnt, flags);
2057 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2060 morework = oldmnt->mnt_nvnodelistsize > 0;
2062 ump = VFSTOUFS(oldmnt);
2064 for (i = 0; i < MAXQUOTAS; i++) {
2065 if (ump->um_quotas[i] != NULLVP)
2071 if (--retry_flush_count > 0) {
2077 MNT_IUNLOCK(oldmnt);
2086 * Structure hashing.
2088 * There are four types of structures that can be looked up:
2089 * 1) pagedep structures identified by mount point, inode number,
2090 * and logical block.
2091 * 2) inodedep structures identified by mount point and inode number.
2092 * 3) newblk structures identified by mount point and
2093 * physical block number.
2094 * 4) bmsafemap structures identified by mount point and
2095 * cylinder group number.
2097 * The "pagedep" and "inodedep" dependency structures are hashed
2098 * separately from the file blocks and inodes to which they correspond.
2099 * This separation helps when the in-memory copy of an inode or
2100 * file block must be replaced. It also obviates the need to access
2101 * an inode or file page when simply updating (or de-allocating)
2102 * dependency structures. Lookup of newblk structures is needed to
2103 * find newly allocated blocks when trying to associate them with
2104 * their allocdirect or allocindir structure.
2106 * The lookup routines optionally create and hash a new instance when
2107 * an existing entry is not found. The bmsafemap lookup routine always
2108 * allocates a new structure if an existing one is not found.
2110 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2113 * Structures and routines associated with pagedep caching.
2115 #define PAGEDEP_HASH(ump, inum, lbn) \
2116 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2119 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2120 struct pagedep_hashhead *pagedephd;
2123 struct pagedep **pagedeppp;
2125 struct pagedep *pagedep;
2127 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2128 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2129 *pagedeppp = pagedep;
2137 * Look up a pagedep. Return 1 if found, 0 otherwise.
2138 * If not found, allocate if DEPALLOC flag is passed.
2139 * Found or allocated entry is returned in pagedeppp.
2142 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2148 struct pagedep **pagedeppp;
2150 struct pagedep *pagedep;
2151 struct pagedep_hashhead *pagedephd;
2152 struct worklist *wk;
2153 struct ufsmount *ump;
2160 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2161 if (wk->wk_type == D_PAGEDEP) {
2162 *pagedeppp = WK_PAGEDEP(wk);
2167 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2168 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2170 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2171 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2174 if ((flags & DEPALLOC) == 0)
2177 pagedep = malloc(sizeof(struct pagedep),
2178 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2179 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2181 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2184 * This should never happen since we only create pagedeps
2185 * with the vnode lock held. Could be an assert.
2187 WORKITEM_FREE(pagedep, D_PAGEDEP);
2190 pagedep->pd_ino = ino;
2191 pagedep->pd_lbn = lbn;
2192 LIST_INIT(&pagedep->pd_dirremhd);
2193 LIST_INIT(&pagedep->pd_pendinghd);
2194 for (i = 0; i < DAHASHSZ; i++)
2195 LIST_INIT(&pagedep->pd_diraddhd[i]);
2196 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2197 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2198 *pagedeppp = pagedep;
2203 * Structures and routines associated with inodedep caching.
2205 #define INODEDEP_HASH(ump, inum) \
2206 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2209 inodedep_find(inodedephd, inum, inodedeppp)
2210 struct inodedep_hashhead *inodedephd;
2212 struct inodedep **inodedeppp;
2214 struct inodedep *inodedep;
2216 LIST_FOREACH(inodedep, inodedephd, id_hash)
2217 if (inum == inodedep->id_ino)
2220 *inodedeppp = inodedep;
2228 * Look up an inodedep. Return 1 if found, 0 if not found.
2229 * If not found, allocate if DEPALLOC flag is passed.
2230 * Found or allocated entry is returned in inodedeppp.
2233 inodedep_lookup(mp, inum, flags, inodedeppp)
2237 struct inodedep **inodedeppp;
2239 struct inodedep *inodedep;
2240 struct inodedep_hashhead *inodedephd;
2241 struct ufsmount *ump;
2247 inodedephd = INODEDEP_HASH(ump, inum);
2249 if (inodedep_find(inodedephd, inum, inodedeppp))
2251 if ((flags & DEPALLOC) == 0)
2254 * If the system is over its limit and our filesystem is
2255 * responsible for more than our share of that usage and
2256 * we are not in a rush, request some inodedep cleanup.
2258 if (softdep_excess_items(ump, D_INODEDEP))
2259 schedule_cleanup(mp);
2262 inodedep = malloc(sizeof(struct inodedep),
2263 M_INODEDEP, M_SOFTDEP_FLAGS);
2264 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2266 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2267 WORKITEM_FREE(inodedep, D_INODEDEP);
2270 inodedep->id_fs = fs;
2271 inodedep->id_ino = inum;
2272 inodedep->id_state = ALLCOMPLETE;
2273 inodedep->id_nlinkdelta = 0;
2274 inodedep->id_nlinkwrote = -1;
2275 inodedep->id_savedino1 = NULL;
2276 inodedep->id_savedsize = -1;
2277 inodedep->id_savedextsize = -1;
2278 inodedep->id_savednlink = -1;
2279 inodedep->id_bmsafemap = NULL;
2280 inodedep->id_mkdiradd = NULL;
2281 LIST_INIT(&inodedep->id_dirremhd);
2282 LIST_INIT(&inodedep->id_pendinghd);
2283 LIST_INIT(&inodedep->id_inowait);
2284 LIST_INIT(&inodedep->id_bufwait);
2285 TAILQ_INIT(&inodedep->id_inoreflst);
2286 TAILQ_INIT(&inodedep->id_inoupdt);
2287 TAILQ_INIT(&inodedep->id_newinoupdt);
2288 TAILQ_INIT(&inodedep->id_extupdt);
2289 TAILQ_INIT(&inodedep->id_newextupdt);
2290 TAILQ_INIT(&inodedep->id_freeblklst);
2291 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2292 *inodedeppp = inodedep;
2297 * Structures and routines associated with newblk caching.
2299 #define NEWBLK_HASH(ump, inum) \
2300 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2303 newblk_find(newblkhd, newblkno, flags, newblkpp)
2304 struct newblk_hashhead *newblkhd;
2305 ufs2_daddr_t newblkno;
2307 struct newblk **newblkpp;
2309 struct newblk *newblk;
2311 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2312 if (newblkno != newblk->nb_newblkno)
2315 * If we're creating a new dependency don't match those that
2316 * have already been converted to allocdirects. This is for
2319 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2332 * Look up a newblk. Return 1 if found, 0 if not found.
2333 * If not found, allocate if DEPALLOC flag is passed.
2334 * Found or allocated entry is returned in newblkpp.
2337 newblk_lookup(mp, newblkno, flags, newblkpp)
2339 ufs2_daddr_t newblkno;
2341 struct newblk **newblkpp;
2343 struct newblk *newblk;
2344 struct newblk_hashhead *newblkhd;
2345 struct ufsmount *ump;
2349 newblkhd = NEWBLK_HASH(ump, newblkno);
2350 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2352 if ((flags & DEPALLOC) == 0)
2354 if (softdep_excess_items(ump, D_NEWBLK) ||
2355 softdep_excess_items(ump, D_ALLOCDIRECT) ||
2356 softdep_excess_items(ump, D_ALLOCINDIR))
2357 schedule_cleanup(mp);
2360 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2361 M_SOFTDEP_FLAGS | M_ZERO);
2362 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2364 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2365 WORKITEM_FREE(newblk, D_NEWBLK);
2368 newblk->nb_freefrag = NULL;
2369 LIST_INIT(&newblk->nb_indirdeps);
2370 LIST_INIT(&newblk->nb_newdirblk);
2371 LIST_INIT(&newblk->nb_jwork);
2372 newblk->nb_state = ATTACHED;
2373 newblk->nb_newblkno = newblkno;
2374 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2380 * Structures and routines associated with freed indirect block caching.
2382 #define INDIR_HASH(ump, blkno) \
2383 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2386 * Lookup an indirect block in the indir hash table. The freework is
2387 * removed and potentially freed. The caller must do a blocking journal
2388 * write before writing to the blkno.
2391 indirblk_lookup(mp, blkno)
2395 struct freework *freework;
2396 struct indir_hashhead *wkhd;
2397 struct ufsmount *ump;
2400 wkhd = INDIR_HASH(ump, blkno);
2401 TAILQ_FOREACH(freework, wkhd, fw_next) {
2402 if (freework->fw_blkno != blkno)
2404 indirblk_remove(freework);
2411 * Insert an indirect block represented by freework into the indirblk
2412 * hash table so that it may prevent the block from being re-used prior
2413 * to the journal being written.
2416 indirblk_insert(freework)
2417 struct freework *freework;
2419 struct jblocks *jblocks;
2421 struct ufsmount *ump;
2423 ump = VFSTOUFS(freework->fw_list.wk_mp);
2424 jblocks = ump->softdep_jblocks;
2425 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2429 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2430 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2432 freework->fw_state &= ~DEPCOMPLETE;
2436 indirblk_remove(freework)
2437 struct freework *freework;
2439 struct ufsmount *ump;
2441 ump = VFSTOUFS(freework->fw_list.wk_mp);
2442 LIST_REMOVE(freework, fw_segs);
2443 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2444 freework->fw_state |= DEPCOMPLETE;
2445 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2446 WORKITEM_FREE(freework, D_FREEWORK);
2450 * Executed during filesystem system initialization before
2451 * mounting any filesystems.
2454 softdep_initialize()
2457 TAILQ_INIT(&softdepmounts);
2459 max_softdeps = desiredvnodes * 4;
2461 max_softdeps = desiredvnodes * 2;
2464 /* initialise bioops hack */
2465 bioops.io_start = softdep_disk_io_initiation;
2466 bioops.io_complete = softdep_disk_write_complete;
2467 bioops.io_deallocate = softdep_deallocate_dependencies;
2468 bioops.io_countdeps = softdep_count_dependencies;
2469 softdep_ast_cleanup = softdep_ast_cleanup_proc;
2471 /* Initialize the callout with an mtx. */
2472 callout_init_mtx(&softdep_callout, &lk, 0);
2476 * Executed after all filesystems have been unmounted during
2477 * filesystem module unload.
2480 softdep_uninitialize()
2483 /* clear bioops hack */
2484 bioops.io_start = NULL;
2485 bioops.io_complete = NULL;
2486 bioops.io_deallocate = NULL;
2487 bioops.io_countdeps = NULL;
2488 softdep_ast_cleanup = NULL;
2490 callout_drain(&softdep_callout);
2494 * Called at mount time to notify the dependency code that a
2495 * filesystem wishes to use it.
2498 softdep_mount(devvp, mp, fs, cred)
2499 struct vnode *devvp;
2504 struct csum_total cstotal;
2505 struct mount_softdeps *sdp;
2506 struct ufsmount *ump;
2512 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2515 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2516 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2517 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2518 MNTK_SOFTDEP | MNTK_NOASYNC;
2521 ump->um_softdep = sdp;
2523 rw_init(LOCK_PTR(ump), "per-fs softdep");
2525 LIST_INIT(&ump->softdep_workitem_pending);
2526 LIST_INIT(&ump->softdep_journal_pending);
2527 TAILQ_INIT(&ump->softdep_unlinked);
2528 LIST_INIT(&ump->softdep_dirtycg);
2529 ump->softdep_worklist_tail = NULL;
2530 ump->softdep_on_worklist = 0;
2531 ump->softdep_deps = 0;
2532 LIST_INIT(&ump->softdep_mkdirlisthd);
2533 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
2534 &ump->pagedep_hash_size);
2535 ump->pagedep_nextclean = 0;
2536 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP,
2537 &ump->inodedep_hash_size);
2538 ump->inodedep_nextclean = 0;
2539 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK,
2540 &ump->newblk_hash_size);
2541 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP,
2542 &ump->bmsafemap_hash_size);
2543 i = 1 << (ffs(desiredvnodes / 10) - 1);
2544 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead),
2545 M_FREEWORK, M_WAITOK);
2546 ump->indir_hash_size = i - 1;
2547 for (i = 0; i <= ump->indir_hash_size; i++)
2548 TAILQ_INIT(&ump->indir_hashtbl[i]);
2550 for (i = 0; i <= D_LAST; i++)
2551 LIST_INIT(&ump->softdep_alldeps[i]);
2553 ACQUIRE_GBLLOCK(&lk);
2554 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2556 if ((fs->fs_flags & FS_SUJ) &&
2557 (error = journal_mount(mp, fs, cred)) != 0) {
2558 printf("Failed to start journal: %d\n", error);
2559 softdep_unmount(mp);
2563 * Start our flushing thread in the bufdaemon process.
2566 ump->softdep_flags |= FLUSH_STARTING;
2568 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2569 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2570 mp->mnt_stat.f_mntonname);
2572 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2573 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2578 * When doing soft updates, the counters in the
2579 * superblock may have gotten out of sync. Recomputation
2580 * can take a long time and can be deferred for background
2581 * fsck. However, the old behavior of scanning the cylinder
2582 * groups and recalculating them at mount time is available
2583 * by setting vfs.ffs.compute_summary_at_mount to one.
2585 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2587 bzero(&cstotal, sizeof cstotal);
2588 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2589 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2590 fs->fs_cgsize, cred, &bp)) != 0) {
2592 softdep_unmount(mp);
2595 cgp = (struct cg *)bp->b_data;
2596 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2597 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2598 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2599 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2600 fs->fs_cs(fs, cyl) = cgp->cg_cs;
2604 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2605 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2607 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2615 struct ufsmount *ump;
2620 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
2621 ("softdep_unmount called on non-softdep filesystem"));
2624 mp->mnt_flag &= ~MNT_SOFTDEP;
2625 if (MOUNTEDSUJ(mp) == 0) {
2628 mp->mnt_flag &= ~MNT_SUJ;
2630 journal_unmount(ump);
2633 * Shut down our flushing thread. Check for NULL is if
2634 * softdep_mount errors out before the thread has been created.
2636 if (ump->softdep_flushtd != NULL) {
2638 ump->softdep_flags |= FLUSH_EXIT;
2639 wakeup(&ump->softdep_flushtd);
2640 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP,
2642 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2643 ("Thread shutdown failed"));
2646 * Free up our resources.
2648 ACQUIRE_GBLLOCK(&lk);
2649 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next);
2651 rw_destroy(LOCK_PTR(ump));
2652 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size);
2653 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size);
2654 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size);
2655 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP,
2656 ump->bmsafemap_hash_size);
2657 free(ump->indir_hashtbl, M_FREEWORK);
2659 for (i = 0; i <= D_LAST; i++) {
2660 KASSERT(ump->softdep_curdeps[i] == 0,
2661 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2662 TYPENAME(i), ump->softdep_curdeps[i]));
2663 KASSERT(LIST_EMPTY(&ump->softdep_alldeps[i]),
2664 ("Unmount %s: Dep type %s not empty (%p)", ump->um_fs->fs_fsmnt,
2665 TYPENAME(i), LIST_FIRST(&ump->softdep_alldeps[i])));
2668 free(ump->um_softdep, M_MOUNTDATA);
2671 static struct jblocks *
2672 jblocks_create(void)
2674 struct jblocks *jblocks;
2676 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2677 TAILQ_INIT(&jblocks->jb_segs);
2678 jblocks->jb_avail = 10;
2679 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2680 M_JBLOCKS, M_WAITOK | M_ZERO);
2686 jblocks_alloc(jblocks, bytes, actual)
2687 struct jblocks *jblocks;
2692 struct jextent *jext;
2696 blocks = bytes / DEV_BSIZE;
2697 jext = &jblocks->jb_extent[jblocks->jb_head];
2698 freecnt = jext->je_blocks - jblocks->jb_off;
2700 jblocks->jb_off = 0;
2701 if (++jblocks->jb_head > jblocks->jb_used)
2702 jblocks->jb_head = 0;
2703 jext = &jblocks->jb_extent[jblocks->jb_head];
2704 freecnt = jext->je_blocks;
2706 if (freecnt > blocks)
2708 *actual = freecnt * DEV_BSIZE;
2709 daddr = jext->je_daddr + jblocks->jb_off;
2710 jblocks->jb_off += freecnt;
2711 jblocks->jb_free -= freecnt;
2717 jblocks_free(jblocks, mp, bytes)
2718 struct jblocks *jblocks;
2723 LOCK_OWNED(VFSTOUFS(mp));
2724 jblocks->jb_free += bytes / DEV_BSIZE;
2725 if (jblocks->jb_suspended)
2726 worklist_speedup(mp);
2731 jblocks_destroy(jblocks)
2732 struct jblocks *jblocks;
2735 if (jblocks->jb_extent)
2736 free(jblocks->jb_extent, M_JBLOCKS);
2737 free(jblocks, M_JBLOCKS);
2741 jblocks_add(jblocks, daddr, blocks)
2742 struct jblocks *jblocks;
2746 struct jextent *jext;
2748 jblocks->jb_blocks += blocks;
2749 jblocks->jb_free += blocks;
2750 jext = &jblocks->jb_extent[jblocks->jb_used];
2751 /* Adding the first block. */
2752 if (jext->je_daddr == 0) {
2753 jext->je_daddr = daddr;
2754 jext->je_blocks = blocks;
2757 /* Extending the last extent. */
2758 if (jext->je_daddr + jext->je_blocks == daddr) {
2759 jext->je_blocks += blocks;
2762 /* Adding a new extent. */
2763 if (++jblocks->jb_used == jblocks->jb_avail) {
2764 jblocks->jb_avail *= 2;
2765 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2766 M_JBLOCKS, M_WAITOK | M_ZERO);
2767 memcpy(jext, jblocks->jb_extent,
2768 sizeof(struct jextent) * jblocks->jb_used);
2769 free(jblocks->jb_extent, M_JBLOCKS);
2770 jblocks->jb_extent = jext;
2772 jext = &jblocks->jb_extent[jblocks->jb_used];
2773 jext->je_daddr = daddr;
2774 jext->je_blocks = blocks;
2779 softdep_journal_lookup(mp, vpp)
2783 struct componentname cnp;
2788 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2791 bzero(&cnp, sizeof(cnp));
2792 cnp.cn_nameiop = LOOKUP;
2793 cnp.cn_flags = ISLASTCN;
2794 cnp.cn_thread = curthread;
2795 cnp.cn_cred = curthread->td_ucred;
2796 cnp.cn_pnbuf = SUJ_FILE;
2797 cnp.cn_nameptr = SUJ_FILE;
2798 cnp.cn_namelen = strlen(SUJ_FILE);
2799 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2803 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2808 * Open and verify the journal file.
2811 journal_mount(mp, fs, cred)
2816 struct jblocks *jblocks;
2817 struct ufsmount *ump;
2826 ump->softdep_journal_tail = NULL;
2827 ump->softdep_on_journal = 0;
2828 ump->softdep_accdeps = 0;
2829 ump->softdep_req = 0;
2830 ump->softdep_jblocks = NULL;
2831 error = softdep_journal_lookup(mp, &vp);
2833 printf("Failed to find journal. Use tunefs to create one\n");
2837 if (ip->i_size < SUJ_MIN) {
2841 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */
2842 jblocks = jblocks_create();
2843 for (i = 0; i < bcount; i++) {
2844 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
2847 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
2850 jblocks_destroy(jblocks);
2853 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */
2854 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
2855 ump->softdep_jblocks = jblocks;
2859 mp->mnt_flag |= MNT_SUJ;
2860 mp->mnt_flag &= ~MNT_SOFTDEP;
2863 * Only validate the journal contents if the
2864 * filesystem is clean, otherwise we write the logs
2865 * but they'll never be used. If the filesystem was
2866 * still dirty when we mounted it the journal is
2867 * invalid and a new journal can only be valid if it
2868 * starts from a clean mount.
2871 DIP_SET(ip, i_modrev, fs->fs_mtime);
2872 ip->i_flags |= IN_MODIFIED;
2881 journal_unmount(ump)
2882 struct ufsmount *ump;
2885 if (ump->softdep_jblocks)
2886 jblocks_destroy(ump->softdep_jblocks);
2887 ump->softdep_jblocks = NULL;
2891 * Called when a journal record is ready to be written. Space is allocated
2892 * and the journal entry is created when the journal is flushed to stable
2897 struct worklist *wk;
2899 struct ufsmount *ump;
2901 ump = VFSTOUFS(wk->wk_mp);
2903 if (wk->wk_state & ONWORKLIST)
2904 panic("add_to_journal: %s(0x%X) already on list",
2905 TYPENAME(wk->wk_type), wk->wk_state);
2906 wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
2907 if (LIST_EMPTY(&ump->softdep_journal_pending)) {
2908 ump->softdep_jblocks->jb_age = ticks;
2909 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
2911 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
2912 ump->softdep_journal_tail = wk;
2913 ump->softdep_on_journal += 1;
2917 * Remove an arbitrary item for the journal worklist maintain the tail
2918 * pointer. This happens when a new operation obviates the need to
2919 * journal an old operation.
2922 remove_from_journal(wk)
2923 struct worklist *wk;
2925 struct ufsmount *ump;
2927 ump = VFSTOUFS(wk->wk_mp);
2931 struct worklist *wkn;
2933 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
2937 panic("remove_from_journal: %p is not in journal", wk);
2941 * We emulate a TAILQ to save space in most structures which do not
2942 * require TAILQ semantics. Here we must update the tail position
2943 * when removing the tail which is not the final entry. This works
2944 * only if the worklist linkage are at the beginning of the structure.
2946 if (ump->softdep_journal_tail == wk)
2947 ump->softdep_journal_tail =
2948 (struct worklist *)wk->wk_list.le_prev;
2949 WORKLIST_REMOVE(wk);
2950 ump->softdep_on_journal -= 1;
2954 * Check for journal space as well as dependency limits so the prelink
2955 * code can throttle both journaled and non-journaled filesystems.
2956 * Threshold is 0 for low and 1 for min.
2959 journal_space(ump, thresh)
2960 struct ufsmount *ump;
2963 struct jblocks *jblocks;
2966 jblocks = ump->softdep_jblocks;
2967 if (jblocks == NULL)
2970 * We use a tighter restriction here to prevent request_cleanup()
2971 * running in threads from running into locks we currently hold.
2972 * We have to be over the limit and our filesystem has to be
2973 * responsible for more than our share of that usage.
2975 limit = (max_softdeps / 10) * 9;
2976 if (dep_current[D_INODEDEP] > limit &&
2977 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
2980 thresh = jblocks->jb_min;
2982 thresh = jblocks->jb_low;
2983 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
2984 avail = jblocks->jb_free - avail;
2986 return (avail > thresh);
2990 journal_suspend(ump)
2991 struct ufsmount *ump;
2993 struct jblocks *jblocks;
2998 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0)
3001 jblocks = ump->softdep_jblocks;
3005 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
3007 mp->mnt_kern_flag |= MNTK_SUSPEND;
3008 mp->mnt_susp_owner = ump->softdep_flushtd;
3011 jblocks->jb_suspended = 1;
3018 journal_unsuspend(struct ufsmount *ump)
3020 struct jblocks *jblocks;
3024 jblocks = ump->softdep_jblocks;
3026 if (jblocks != NULL && jblocks->jb_suspended &&
3027 journal_space(ump, jblocks->jb_min)) {
3028 jblocks->jb_suspended = 0;
3030 mp->mnt_susp_owner = curthread;
3031 vfs_write_resume(mp, 0);
3039 * Called before any allocation function to be certain that there is
3040 * sufficient space in the journal prior to creating any new records.
3041 * Since in the case of block allocation we may have multiple locked
3042 * buffers at the time of the actual allocation we can not block
3043 * when the journal records are created. Doing so would create a deadlock
3044 * if any of these buffers needed to be flushed to reclaim space. Instead
3045 * we require a sufficiently large amount of available space such that
3046 * each thread in the system could have passed this allocation check and
3047 * still have sufficient free space. With 20% of a minimum journal size
3048 * of 1MB we have 6553 records available.
3051 softdep_prealloc(vp, waitok)
3055 struct ufsmount *ump;
3057 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3058 ("softdep_prealloc called on non-softdep filesystem"));
3060 * Nothing to do if we are not running journaled soft updates.
3061 * If we currently hold the snapshot lock, we must avoid
3062 * handling other resources that could cause deadlock. Do not
3063 * touch quotas vnode since it is typically recursed with
3064 * other vnode locks held.
3066 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3067 (vp->v_vflag & VV_SYSTEM) != 0)
3069 ump = VFSTOUFS(vp->v_mount);
3071 if (journal_space(ump, 0)) {
3077 if (waitok == MNT_NOWAIT)
3080 * Attempt to sync this vnode once to flush any journal
3081 * work attached to it.
3083 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3084 ffs_syncvnode(vp, waitok, 0);
3086 process_removes(vp);
3087 process_truncates(vp);
3088 if (journal_space(ump, 0) == 0) {
3089 softdep_speedup(ump);
3090 if (journal_space(ump, 1) == 0)
3091 journal_suspend(ump);
3099 * Before adjusting a link count on a vnode verify that we have sufficient
3100 * journal space. If not, process operations that depend on the currently
3101 * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3102 * and softdep flush threads can not acquire these locks to reclaim space.
3105 softdep_prelink(dvp, vp)
3109 struct ufsmount *ump;
3111 ump = VFSTOUFS(dvp->v_mount);
3114 * Nothing to do if we have sufficient journal space.
3115 * If we currently hold the snapshot lock, we must avoid
3116 * handling other resources that could cause deadlock.
3118 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp))))
3123 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3124 ffs_syncvnode(dvp, MNT_WAIT, 0);
3126 /* Process vp before dvp as it may create .. removes. */
3128 process_removes(vp);
3129 process_truncates(vp);
3131 process_removes(dvp);
3132 process_truncates(dvp);
3133 softdep_speedup(ump);
3134 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3135 if (journal_space(ump, 0) == 0) {
3136 softdep_speedup(ump);
3137 if (journal_space(ump, 1) == 0)
3138 journal_suspend(ump);
3143 jseg_write(ump, jseg, data)
3144 struct ufsmount *ump;
3148 struct jsegrec *rec;
3150 rec = (struct jsegrec *)data;
3151 rec->jsr_seq = jseg->js_seq;
3152 rec->jsr_oldest = jseg->js_oldseq;
3153 rec->jsr_cnt = jseg->js_cnt;
3154 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3156 rec->jsr_time = ump->um_fs->fs_mtime;
3160 inoref_write(inoref, jseg, rec)
3161 struct inoref *inoref;
3163 struct jrefrec *rec;
3166 inoref->if_jsegdep->jd_seg = jseg;
3167 rec->jr_ino = inoref->if_ino;
3168 rec->jr_parent = inoref->if_parent;
3169 rec->jr_nlink = inoref->if_nlink;
3170 rec->jr_mode = inoref->if_mode;
3171 rec->jr_diroff = inoref->if_diroff;
3175 jaddref_write(jaddref, jseg, data)
3176 struct jaddref *jaddref;
3180 struct jrefrec *rec;
3182 rec = (struct jrefrec *)data;
3183 rec->jr_op = JOP_ADDREF;
3184 inoref_write(&jaddref->ja_ref, jseg, rec);
3188 jremref_write(jremref, jseg, data)
3189 struct jremref *jremref;
3193 struct jrefrec *rec;
3195 rec = (struct jrefrec *)data;
3196 rec->jr_op = JOP_REMREF;
3197 inoref_write(&jremref->jr_ref, jseg, rec);
3201 jmvref_write(jmvref, jseg, data)
3202 struct jmvref *jmvref;
3208 rec = (struct jmvrec *)data;
3209 rec->jm_op = JOP_MVREF;
3210 rec->jm_ino = jmvref->jm_ino;
3211 rec->jm_parent = jmvref->jm_parent;
3212 rec->jm_oldoff = jmvref->jm_oldoff;
3213 rec->jm_newoff = jmvref->jm_newoff;
3217 jnewblk_write(jnewblk, jseg, data)
3218 struct jnewblk *jnewblk;
3222 struct jblkrec *rec;
3224 jnewblk->jn_jsegdep->jd_seg = jseg;
3225 rec = (struct jblkrec *)data;
3226 rec->jb_op = JOP_NEWBLK;
3227 rec->jb_ino = jnewblk->jn_ino;
3228 rec->jb_blkno = jnewblk->jn_blkno;
3229 rec->jb_lbn = jnewblk->jn_lbn;
3230 rec->jb_frags = jnewblk->jn_frags;
3231 rec->jb_oldfrags = jnewblk->jn_oldfrags;
3235 jfreeblk_write(jfreeblk, jseg, data)
3236 struct jfreeblk *jfreeblk;
3240 struct jblkrec *rec;
3242 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3243 rec = (struct jblkrec *)data;
3244 rec->jb_op = JOP_FREEBLK;
3245 rec->jb_ino = jfreeblk->jf_ino;
3246 rec->jb_blkno = jfreeblk->jf_blkno;
3247 rec->jb_lbn = jfreeblk->jf_lbn;
3248 rec->jb_frags = jfreeblk->jf_frags;
3249 rec->jb_oldfrags = 0;
3253 jfreefrag_write(jfreefrag, jseg, data)
3254 struct jfreefrag *jfreefrag;
3258 struct jblkrec *rec;
3260 jfreefrag->fr_jsegdep->jd_seg = jseg;
3261 rec = (struct jblkrec *)data;
3262 rec->jb_op = JOP_FREEBLK;
3263 rec->jb_ino = jfreefrag->fr_ino;
3264 rec->jb_blkno = jfreefrag->fr_blkno;
3265 rec->jb_lbn = jfreefrag->fr_lbn;
3266 rec->jb_frags = jfreefrag->fr_frags;
3267 rec->jb_oldfrags = 0;
3271 jtrunc_write(jtrunc, jseg, data)
3272 struct jtrunc *jtrunc;
3276 struct jtrncrec *rec;
3278 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3279 rec = (struct jtrncrec *)data;
3280 rec->jt_op = JOP_TRUNC;
3281 rec->jt_ino = jtrunc->jt_ino;
3282 rec->jt_size = jtrunc->jt_size;
3283 rec->jt_extsize = jtrunc->jt_extsize;
3287 jfsync_write(jfsync, jseg, data)
3288 struct jfsync *jfsync;
3292 struct jtrncrec *rec;
3294 rec = (struct jtrncrec *)data;
3295 rec->jt_op = JOP_SYNC;
3296 rec->jt_ino = jfsync->jfs_ino;
3297 rec->jt_size = jfsync->jfs_size;
3298 rec->jt_extsize = jfsync->jfs_extsize;
3302 softdep_flushjournal(mp)
3305 struct jblocks *jblocks;
3306 struct ufsmount *ump;
3308 if (MOUNTEDSUJ(mp) == 0)
3311 jblocks = ump->softdep_jblocks;
3313 while (ump->softdep_on_journal) {
3314 jblocks->jb_needseg = 1;
3315 softdep_process_journal(mp, NULL, MNT_WAIT);
3320 static void softdep_synchronize_completed(struct bio *);
3321 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3324 softdep_synchronize_completed(bp)
3327 struct jseg *oldest;
3329 struct ufsmount *ump;
3332 * caller1 marks the last segment written before we issued the
3333 * synchronize cache.
3335 jseg = bp->bio_caller1;
3340 ump = VFSTOUFS(jseg->js_list.wk_mp);
3344 * Mark all the journal entries waiting on the synchronize cache
3345 * as completed so they may continue on.
3347 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3348 jseg->js_state |= COMPLETE;
3350 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3353 * Restart deferred journal entry processing from the oldest
3357 complete_jsegs(oldest);
3364 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3365 * barriers. The journal must be written prior to any blocks that depend
3366 * on it and the journal can not be released until the blocks have be
3367 * written. This code handles both barriers simultaneously.
3370 softdep_synchronize(bp, ump, caller1)
3372 struct ufsmount *ump;
3376 bp->bio_cmd = BIO_FLUSH;
3377 bp->bio_flags |= BIO_ORDERED;
3378 bp->bio_data = NULL;
3379 bp->bio_offset = ump->um_cp->provider->mediasize;
3381 bp->bio_done = softdep_synchronize_completed;
3382 bp->bio_caller1 = caller1;
3383 g_io_request(bp, ump->um_cp);
3387 * Flush some journal records to disk.
3390 softdep_process_journal(mp, needwk, flags)
3392 struct worklist *needwk;
3395 struct jblocks *jblocks;
3396 struct ufsmount *ump;
3397 struct worklist *wk;
3405 int jrecmin; /* Minimum records per block. */
3406 int jrecmax; /* Maximum records per block. */
3412 if (MOUNTEDSUJ(mp) == 0)
3414 shouldflush = softdep_flushcache;
3420 jblocks = ump->softdep_jblocks;
3421 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3423 * We write anywhere between a disk block and fs block. The upper
3424 * bound is picked to prevent buffer cache fragmentation and limit
3425 * processing time per I/O.
3427 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3428 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3431 cnt = ump->softdep_on_journal;
3433 * Criteria for writing a segment:
3434 * 1) We have a full block.
3435 * 2) We're called from jwait() and haven't found the
3437 * 3) Always write if needseg is set.
3438 * 4) If we are called from process_worklist and have
3439 * not yet written anything we write a partial block
3440 * to enforce a 1 second maximum latency on journal
3443 if (cnt < (jrecmax - 1) && needwk == NULL &&
3444 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3448 * Verify some free journal space. softdep_prealloc() should
3449 * guarantee that we don't run out so this is indicative of
3450 * a problem with the flow control. Try to recover
3451 * gracefully in any event.
3453 while (jblocks->jb_free == 0) {
3454 if (flags != MNT_WAIT)
3456 printf("softdep: Out of journal space!\n");
3457 softdep_speedup(ump);
3458 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3461 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3462 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3463 LIST_INIT(&jseg->js_entries);
3464 LIST_INIT(&jseg->js_indirs);
3465 jseg->js_state = ATTACHED;
3466 if (shouldflush == 0)
3467 jseg->js_state |= COMPLETE;
3468 else if (bio == NULL)
3469 bio = g_alloc_bio();
3470 jseg->js_jblocks = jblocks;
3471 bp = geteblk(fs->fs_bsize, 0);
3474 * If there was a race while we were allocating the block
3475 * and jseg the entry we care about was likely written.
3476 * We bail out in both the WAIT and NOWAIT case and assume
3477 * the caller will loop if the entry it cares about is
3480 cnt = ump->softdep_on_journal;
3481 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3482 bp->b_flags |= B_INVAL | B_NOCACHE;
3483 WORKITEM_FREE(jseg, D_JSEG);
3490 * Calculate the disk block size required for the available
3491 * records rounded to the min size.
3495 else if (cnt < jrecmax)
3496 size = howmany(cnt, jrecmin) * devbsize;
3498 size = fs->fs_bsize;
3500 * Allocate a disk block for this journal data and account
3501 * for truncation of the requested size if enough contiguous
3502 * space was not available.
3504 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3505 bp->b_lblkno = bp->b_blkno;
3506 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3507 bp->b_bcount = size;
3508 bp->b_flags &= ~B_INVAL;
3509 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3511 * Initialize our jseg with cnt records. Assign the next
3512 * sequence number to it and link it in-order.
3514 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3517 jseg->js_refs = cnt + 1; /* Self ref. */
3518 jseg->js_size = size;
3519 jseg->js_seq = jblocks->jb_nextseq++;
3520 if (jblocks->jb_oldestseg == NULL)
3521 jblocks->jb_oldestseg = jseg;
3522 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3523 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3524 if (jblocks->jb_writeseg == NULL)
3525 jblocks->jb_writeseg = jseg;
3527 * Start filling in records from the pending list.
3533 * Always put a header on the first block.
3534 * XXX As with below, there might not be a chance to get
3535 * into the loop. Ensure that something valid is written.
3537 jseg_write(ump, jseg, data);
3539 data = bp->b_data + off;
3542 * XXX Something is wrong here. There's no work to do,
3543 * but we need to perform and I/O and allow it to complete
3546 if (LIST_EMPTY(&ump->softdep_journal_pending))
3547 stat_emptyjblocks++;
3549 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3553 /* Place a segment header on every device block. */
3554 if ((off % devbsize) == 0) {
3555 jseg_write(ump, jseg, data);
3557 data = bp->b_data + off;
3561 remove_from_journal(wk);
3562 wk->wk_state |= INPROGRESS;
3563 WORKLIST_INSERT(&jseg->js_entries, wk);
3564 switch (wk->wk_type) {
3566 jaddref_write(WK_JADDREF(wk), jseg, data);
3569 jremref_write(WK_JREMREF(wk), jseg, data);
3572 jmvref_write(WK_JMVREF(wk), jseg, data);
3575 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3578 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3581 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3584 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3587 jfsync_write(WK_JFSYNC(wk), jseg, data);
3590 panic("process_journal: Unknown type %s",
3591 TYPENAME(wk->wk_type));
3595 data = bp->b_data + off;
3599 /* Clear any remaining space so we don't leak kernel data */
3601 bzero(data, size - off);
3604 * Write this one buffer and continue.
3607 jblocks->jb_needseg = 0;
3608 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3610 bp->b_xflags |= BX_CVTENXIO;
3611 pbgetvp(ump->um_devvp, bp);
3613 * We only do the blocking wait once we find the journal
3614 * entry we're looking for.
3616 if (needwk == NULL && flags == MNT_WAIT)
3623 * If we wrote a segment issue a synchronize cache so the journal
3624 * is reflected on disk before the data is written. Since reclaiming
3625 * journal space also requires writing a journal record this
3626 * process also enforces a barrier before reclamation.
3628 if (segwritten && shouldflush) {
3629 softdep_synchronize(bio, ump,
3630 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3634 * If we've suspended the filesystem because we ran out of journal
3635 * space either try to sync it here to make some progress or
3636 * unsuspend it if we already have.
3638 if (flags == 0 && jblocks->jb_suspended) {
3639 if (journal_unsuspend(ump))
3642 VFS_SYNC(mp, MNT_NOWAIT);
3643 ffs_sbupdate(ump, MNT_WAIT, 0);
3649 * Complete a jseg, allowing all dependencies awaiting journal writes
3650 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3651 * structures so that the journal segment can be freed to reclaim space.
3657 struct worklist *wk;
3658 struct jmvref *jmvref;
3663 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3664 WORKLIST_REMOVE(wk);
3665 wk->wk_state &= ~INPROGRESS;
3666 wk->wk_state |= COMPLETE;
3667 KASSERT(i++ < jseg->js_cnt,
3668 ("handle_written_jseg: overflow %d >= %d",
3669 i - 1, jseg->js_cnt));
3670 switch (wk->wk_type) {
3672 handle_written_jaddref(WK_JADDREF(wk));
3675 handle_written_jremref(WK_JREMREF(wk));
3678 rele_jseg(jseg); /* No jsegdep. */
3679 jmvref = WK_JMVREF(wk);
3680 LIST_REMOVE(jmvref, jm_deps);
3681 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3682 free_pagedep(jmvref->jm_pagedep);
3683 WORKITEM_FREE(jmvref, D_JMVREF);
3686 handle_written_jnewblk(WK_JNEWBLK(wk));
3689 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3692 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3695 rele_jseg(jseg); /* No jsegdep. */
3696 WORKITEM_FREE(wk, D_JFSYNC);
3699 handle_written_jfreefrag(WK_JFREEFRAG(wk));
3702 panic("handle_written_jseg: Unknown type %s",
3703 TYPENAME(wk->wk_type));
3707 /* Release the self reference so the structure may be freed. */
3712 * Determine which jsegs are ready for completion processing. Waits for
3713 * synchronize cache to complete as well as forcing in-order completion
3714 * of journal entries.
3717 complete_jsegs(jseg)
3720 struct jblocks *jblocks;
3723 jblocks = jseg->js_jblocks;
3725 * Don't allow out of order completions. If this isn't the first
3726 * block wait for it to write before we're done.
3728 if (jseg != jblocks->jb_writeseg)
3730 /* Iterate through available jsegs processing their entries. */
3731 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
3732 jblocks->jb_oldestwrseq = jseg->js_oldseq;
3733 jsegn = TAILQ_NEXT(jseg, js_next);
3734 complete_jseg(jseg);
3737 jblocks->jb_writeseg = jseg;
3739 * Attempt to free jsegs now that oldestwrseq may have advanced.
3741 free_jsegs(jblocks);
3745 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
3746 * the final completions.
3749 handle_written_jseg(jseg, bp)
3754 if (jseg->js_refs == 0)
3755 panic("handle_written_jseg: No self-reference on %p", jseg);
3756 jseg->js_state |= DEPCOMPLETE;
3758 * We'll never need this buffer again, set flags so it will be
3761 bp->b_flags |= B_INVAL | B_NOCACHE;
3763 complete_jsegs(jseg);
3766 static inline struct jsegdep *
3768 struct inoref *inoref;
3770 struct jsegdep *jsegdep;
3772 jsegdep = inoref->if_jsegdep;
3773 inoref->if_jsegdep = NULL;
3779 * Called once a jremref has made it to stable store. The jremref is marked
3780 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
3781 * for the jremref to complete will be awoken by free_jremref.
3784 handle_written_jremref(jremref)
3785 struct jremref *jremref;
3787 struct inodedep *inodedep;
3788 struct jsegdep *jsegdep;
3789 struct dirrem *dirrem;
3791 /* Grab the jsegdep. */
3792 jsegdep = inoref_jseg(&jremref->jr_ref);
3794 * Remove us from the inoref list.
3796 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
3798 panic("handle_written_jremref: Lost inodedep");
3799 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
3801 * Complete the dirrem.
3803 dirrem = jremref->jr_dirrem;
3804 jremref->jr_dirrem = NULL;
3805 LIST_REMOVE(jremref, jr_deps);
3806 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
3807 jwork_insert(&dirrem->dm_jwork, jsegdep);
3808 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
3809 (dirrem->dm_state & COMPLETE) != 0)
3810 add_to_worklist(&dirrem->dm_list, 0);
3811 free_jremref(jremref);
3815 * Called once a jaddref has made it to stable store. The dependency is
3816 * marked complete and any dependent structures are added to the inode
3817 * bufwait list to be completed as soon as it is written. If a bitmap write
3818 * depends on this entry we move the inode into the inodedephd of the
3819 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
3822 handle_written_jaddref(jaddref)
3823 struct jaddref *jaddref;
3825 struct jsegdep *jsegdep;
3826 struct inodedep *inodedep;
3827 struct diradd *diradd;
3828 struct mkdir *mkdir;
3830 /* Grab the jsegdep. */
3831 jsegdep = inoref_jseg(&jaddref->ja_ref);
3834 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
3836 panic("handle_written_jaddref: Lost inodedep.");
3837 if (jaddref->ja_diradd == NULL)
3838 panic("handle_written_jaddref: No dependency");
3839 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
3840 diradd = jaddref->ja_diradd;
3841 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
3842 } else if (jaddref->ja_state & MKDIR_PARENT) {
3843 mkdir = jaddref->ja_mkdir;
3844 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
3845 } else if (jaddref->ja_state & MKDIR_BODY)
3846 mkdir = jaddref->ja_mkdir;
3848 panic("handle_written_jaddref: Unknown dependency %p",
3849 jaddref->ja_diradd);
3850 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
3852 * Remove us from the inode list.
3854 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
3856 * The mkdir may be waiting on the jaddref to clear before freeing.
3859 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
3860 ("handle_written_jaddref: Incorrect type for mkdir %s",
3861 TYPENAME(mkdir->md_list.wk_type)));
3862 mkdir->md_jaddref = NULL;
3863 diradd = mkdir->md_diradd;
3864 mkdir->md_state |= DEPCOMPLETE;
3865 complete_mkdir(mkdir);
3867 jwork_insert(&diradd->da_jwork, jsegdep);
3868 if (jaddref->ja_state & NEWBLOCK) {
3869 inodedep->id_state |= ONDEPLIST;
3870 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
3873 free_jaddref(jaddref);
3877 * Called once a jnewblk journal is written. The allocdirect or allocindir
3878 * is placed in the bmsafemap to await notification of a written bitmap. If
3879 * the operation was canceled we add the segdep to the appropriate
3880 * dependency to free the journal space once the canceling operation
3884 handle_written_jnewblk(jnewblk)
3885 struct jnewblk *jnewblk;
3887 struct bmsafemap *bmsafemap;
3888 struct freefrag *freefrag;
3889 struct freework *freework;
3890 struct jsegdep *jsegdep;
3891 struct newblk *newblk;
3893 /* Grab the jsegdep. */
3894 jsegdep = jnewblk->jn_jsegdep;
3895 jnewblk->jn_jsegdep = NULL;
3896 if (jnewblk->jn_dep == NULL)
3897 panic("handle_written_jnewblk: No dependency for the segdep.");
3898 switch (jnewblk->jn_dep->wk_type) {
3903 * Add the written block to the bmsafemap so it can
3904 * be notified when the bitmap is on disk.
3906 newblk = WK_NEWBLK(jnewblk->jn_dep);
3907 newblk->nb_jnewblk = NULL;
3908 if ((newblk->nb_state & GOINGAWAY) == 0) {
3909 bmsafemap = newblk->nb_bmsafemap;
3910 newblk->nb_state |= ONDEPLIST;
3911 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
3914 jwork_insert(&newblk->nb_jwork, jsegdep);
3918 * A newblock being removed by a freefrag when replaced by
3921 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
3922 freefrag->ff_jdep = NULL;
3923 jwork_insert(&freefrag->ff_jwork, jsegdep);
3927 * A direct block was removed by truncate.
3929 freework = WK_FREEWORK(jnewblk->jn_dep);
3930 freework->fw_jnewblk = NULL;
3931 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
3934 panic("handle_written_jnewblk: Unknown type %d.",
3935 jnewblk->jn_dep->wk_type);
3937 jnewblk->jn_dep = NULL;
3938 free_jnewblk(jnewblk);
3942 * Cancel a jfreefrag that won't be needed, probably due to colliding with
3943 * an in-flight allocation that has not yet been committed. Divorce us
3944 * from the freefrag and mark it DEPCOMPLETE so that it may be added
3948 cancel_jfreefrag(jfreefrag)
3949 struct jfreefrag *jfreefrag;
3951 struct freefrag *freefrag;
3953 if (jfreefrag->fr_jsegdep) {
3954 free_jsegdep(jfreefrag->fr_jsegdep);
3955 jfreefrag->fr_jsegdep = NULL;
3957 freefrag = jfreefrag->fr_freefrag;
3958 jfreefrag->fr_freefrag = NULL;
3959 free_jfreefrag(jfreefrag);
3960 freefrag->ff_state |= DEPCOMPLETE;
3961 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
3965 * Free a jfreefrag when the parent freefrag is rendered obsolete.
3968 free_jfreefrag(jfreefrag)
3969 struct jfreefrag *jfreefrag;
3972 if (jfreefrag->fr_state & INPROGRESS)
3973 WORKLIST_REMOVE(&jfreefrag->fr_list);
3974 else if (jfreefrag->fr_state & ONWORKLIST)
3975 remove_from_journal(&jfreefrag->fr_list);
3976 if (jfreefrag->fr_freefrag != NULL)
3977 panic("free_jfreefrag: Still attached to a freefrag.");
3978 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
3982 * Called when the journal write for a jfreefrag completes. The parent
3983 * freefrag is added to the worklist if this completes its dependencies.
3986 handle_written_jfreefrag(jfreefrag)
3987 struct jfreefrag *jfreefrag;
3989 struct jsegdep *jsegdep;
3990 struct freefrag *freefrag;
3992 /* Grab the jsegdep. */
3993 jsegdep = jfreefrag->fr_jsegdep;
3994 jfreefrag->fr_jsegdep = NULL;
3995 freefrag = jfreefrag->fr_freefrag;
3996 if (freefrag == NULL)
3997 panic("handle_written_jfreefrag: No freefrag.");
3998 freefrag->ff_state |= DEPCOMPLETE;
3999 freefrag->ff_jdep = NULL;
4000 jwork_insert(&freefrag->ff_jwork, jsegdep);
4001 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
4002 add_to_worklist(&freefrag->ff_list, 0);
4003 jfreefrag->fr_freefrag = NULL;
4004 free_jfreefrag(jfreefrag);
4008 * Called when the journal write for a jfreeblk completes. The jfreeblk
4009 * is removed from the freeblks list of pending journal writes and the
4010 * jsegdep is moved to the freeblks jwork to be completed when all blocks
4011 * have been reclaimed.
4014 handle_written_jblkdep(jblkdep)
4015 struct jblkdep *jblkdep;
4017 struct freeblks *freeblks;
4018 struct jsegdep *jsegdep;
4020 /* Grab the jsegdep. */
4021 jsegdep = jblkdep->jb_jsegdep;
4022 jblkdep->jb_jsegdep = NULL;
4023 freeblks = jblkdep->jb_freeblks;
4024 LIST_REMOVE(jblkdep, jb_deps);
4025 jwork_insert(&freeblks->fb_jwork, jsegdep);
4027 * If the freeblks is all journaled, we can add it to the worklist.
4029 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4030 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4031 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4033 free_jblkdep(jblkdep);
4036 static struct jsegdep *
4037 newjsegdep(struct worklist *wk)
4039 struct jsegdep *jsegdep;
4041 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4042 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4043 jsegdep->jd_seg = NULL;
4048 static struct jmvref *
4049 newjmvref(dp, ino, oldoff, newoff)
4055 struct jmvref *jmvref;
4057 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4058 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4059 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4060 jmvref->jm_parent = dp->i_number;
4061 jmvref->jm_ino = ino;
4062 jmvref->jm_oldoff = oldoff;
4063 jmvref->jm_newoff = newoff;
4069 * Allocate a new jremref that tracks the removal of ip from dp with the
4070 * directory entry offset of diroff. Mark the entry as ATTACHED and
4071 * DEPCOMPLETE as we have all the information required for the journal write
4072 * and the directory has already been removed from the buffer. The caller
4073 * is responsible for linking the jremref into the pagedep and adding it
4074 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4075 * a DOTDOT addition so handle_workitem_remove() can properly assign
4076 * the jsegdep when we're done.
4078 static struct jremref *
4079 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4080 off_t diroff, nlink_t nlink)
4082 struct jremref *jremref;
4084 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4085 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4086 jremref->jr_state = ATTACHED;
4087 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4089 jremref->jr_dirrem = dirrem;
4095 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4096 nlink_t nlink, uint16_t mode)
4099 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4100 inoref->if_diroff = diroff;
4101 inoref->if_ino = ino;
4102 inoref->if_parent = parent;
4103 inoref->if_nlink = nlink;
4104 inoref->if_mode = mode;
4108 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4109 * directory offset may not be known until later. The caller is responsible
4110 * adding the entry to the journal when this information is available. nlink
4111 * should be the link count prior to the addition and mode is only required
4112 * to have the correct FMT.
4114 static struct jaddref *
4115 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4118 struct jaddref *jaddref;
4120 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4121 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4122 jaddref->ja_state = ATTACHED;
4123 jaddref->ja_mkdir = NULL;
4124 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4130 * Create a new free dependency for a freework. The caller is responsible
4131 * for adjusting the reference count when it has the lock held. The freedep
4132 * will track an outstanding bitmap write that will ultimately clear the
4133 * freework to continue.
4135 static struct freedep *
4136 newfreedep(struct freework *freework)
4138 struct freedep *freedep;
4140 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4141 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4142 freedep->fd_freework = freework;
4148 * Free a freedep structure once the buffer it is linked to is written. If
4149 * this is the last reference to the freework schedule it for completion.
4152 free_freedep(freedep)
4153 struct freedep *freedep;
4155 struct freework *freework;
4157 freework = freedep->fd_freework;
4158 freework->fw_freeblks->fb_cgwait--;
4159 if (--freework->fw_ref == 0)
4160 freework_enqueue(freework);
4161 WORKITEM_FREE(freedep, D_FREEDEP);
4165 * Allocate a new freework structure that may be a level in an indirect
4166 * when parent is not NULL or a top level block when it is. The top level
4167 * freework structures are allocated without the per-filesystem lock held
4168 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4170 static struct freework *
4171 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4172 struct ufsmount *ump;
4173 struct freeblks *freeblks;
4174 struct freework *parent;
4181 struct freework *freework;
4183 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4184 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4185 freework->fw_state = ATTACHED;
4186 freework->fw_jnewblk = NULL;
4187 freework->fw_freeblks = freeblks;
4188 freework->fw_parent = parent;
4189 freework->fw_lbn = lbn;
4190 freework->fw_blkno = nb;
4191 freework->fw_frags = frags;
4192 freework->fw_indir = NULL;
4193 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4194 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4195 freework->fw_start = freework->fw_off = off;
4197 newjfreeblk(freeblks, lbn, nb, frags);
4198 if (parent == NULL) {
4200 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4209 * Eliminate a jfreeblk for a block that does not need journaling.
4212 cancel_jfreeblk(freeblks, blkno)
4213 struct freeblks *freeblks;
4216 struct jfreeblk *jfreeblk;
4217 struct jblkdep *jblkdep;
4219 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4220 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4222 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4223 if (jfreeblk->jf_blkno == blkno)
4226 if (jblkdep == NULL)
4228 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4229 free_jsegdep(jblkdep->jb_jsegdep);
4230 LIST_REMOVE(jblkdep, jb_deps);
4231 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4235 * Allocate a new jfreeblk to journal top level block pointer when truncating
4236 * a file. The caller must add this to the worklist when the per-filesystem
4239 static struct jfreeblk *
4240 newjfreeblk(freeblks, lbn, blkno, frags)
4241 struct freeblks *freeblks;
4246 struct jfreeblk *jfreeblk;
4248 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4249 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4250 freeblks->fb_list.wk_mp);
4251 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4252 jfreeblk->jf_dep.jb_freeblks = freeblks;
4253 jfreeblk->jf_ino = freeblks->fb_inum;
4254 jfreeblk->jf_lbn = lbn;
4255 jfreeblk->jf_blkno = blkno;
4256 jfreeblk->jf_frags = frags;
4257 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4263 * The journal is only prepared to handle full-size block numbers, so we
4264 * have to adjust the record to reflect the change to a full-size block.
4265 * For example, suppose we have a block made up of fragments 8-15 and
4266 * want to free its last two fragments. We are given a request that says:
4267 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4268 * where frags are the number of fragments to free and oldfrags are the
4269 * number of fragments to keep. To block align it, we have to change it to
4270 * have a valid full-size blkno, so it becomes:
4271 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4274 adjust_newfreework(freeblks, frag_offset)
4275 struct freeblks *freeblks;
4278 struct jfreeblk *jfreeblk;
4280 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4281 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4282 ("adjust_newfreework: Missing freeblks dependency"));
4284 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4285 jfreeblk->jf_blkno -= frag_offset;
4286 jfreeblk->jf_frags += frag_offset;
4290 * Allocate a new jtrunc to track a partial truncation.
4292 static struct jtrunc *
4293 newjtrunc(freeblks, size, extsize)
4294 struct freeblks *freeblks;
4298 struct jtrunc *jtrunc;
4300 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4301 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4302 freeblks->fb_list.wk_mp);
4303 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4304 jtrunc->jt_dep.jb_freeblks = freeblks;
4305 jtrunc->jt_ino = freeblks->fb_inum;
4306 jtrunc->jt_size = size;
4307 jtrunc->jt_extsize = extsize;
4308 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4314 * If we're canceling a new bitmap we have to search for another ref
4315 * to move into the bmsafemap dep. This might be better expressed
4316 * with another structure.
4319 move_newblock_dep(jaddref, inodedep)
4320 struct jaddref *jaddref;
4321 struct inodedep *inodedep;
4323 struct inoref *inoref;
4324 struct jaddref *jaddrefn;
4327 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4328 inoref = TAILQ_NEXT(inoref, if_deps)) {
4329 if ((jaddref->ja_state & NEWBLOCK) &&
4330 inoref->if_list.wk_type == D_JADDREF) {
4331 jaddrefn = (struct jaddref *)inoref;
4335 if (jaddrefn == NULL)
4337 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4338 jaddrefn->ja_state |= jaddref->ja_state &
4339 (ATTACHED | UNDONE | NEWBLOCK);
4340 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4341 jaddref->ja_state |= ATTACHED;
4342 LIST_REMOVE(jaddref, ja_bmdeps);
4343 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4348 * Cancel a jaddref either before it has been written or while it is being
4349 * written. This happens when a link is removed before the add reaches
4350 * the disk. The jaddref dependency is kept linked into the bmsafemap
4351 * and inode to prevent the link count or bitmap from reaching the disk
4352 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4355 * Returns 1 if the canceled addref requires journaling of the remove and
4359 cancel_jaddref(jaddref, inodedep, wkhd)
4360 struct jaddref *jaddref;
4361 struct inodedep *inodedep;
4362 struct workhead *wkhd;
4364 struct inoref *inoref;
4365 struct jsegdep *jsegdep;
4368 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4369 ("cancel_jaddref: Canceling complete jaddref"));
4370 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4374 if (inodedep == NULL)
4375 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4377 panic("cancel_jaddref: Lost inodedep");
4379 * We must adjust the nlink of any reference operation that follows
4380 * us so that it is consistent with the in-memory reference. This
4381 * ensures that inode nlink rollbacks always have the correct link.
4384 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4385 inoref = TAILQ_NEXT(inoref, if_deps)) {
4386 if (inoref->if_state & GOINGAWAY)
4391 jsegdep = inoref_jseg(&jaddref->ja_ref);
4392 if (jaddref->ja_state & NEWBLOCK)
4393 move_newblock_dep(jaddref, inodedep);
4394 wake_worklist(&jaddref->ja_list);
4395 jaddref->ja_mkdir = NULL;
4396 if (jaddref->ja_state & INPROGRESS) {
4397 jaddref->ja_state &= ~INPROGRESS;
4398 WORKLIST_REMOVE(&jaddref->ja_list);
4399 jwork_insert(wkhd, jsegdep);
4401 free_jsegdep(jsegdep);
4402 if (jaddref->ja_state & DEPCOMPLETE)
4403 remove_from_journal(&jaddref->ja_list);
4405 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4407 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4408 * can arrange for them to be freed with the bitmap. Otherwise we
4409 * no longer need this addref attached to the inoreflst and it
4410 * will incorrectly adjust nlink if we leave it.
4412 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4413 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4415 jaddref->ja_state |= COMPLETE;
4416 free_jaddref(jaddref);
4420 * Leave the head of the list for jsegdeps for fast merging.
4422 if (LIST_FIRST(wkhd) != NULL) {
4423 jaddref->ja_state |= ONWORKLIST;
4424 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4426 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4432 * Attempt to free a jaddref structure when some work completes. This
4433 * should only succeed once the entry is written and all dependencies have
4437 free_jaddref(jaddref)
4438 struct jaddref *jaddref;
4441 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4443 if (jaddref->ja_ref.if_jsegdep)
4444 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4445 jaddref, jaddref->ja_state);
4446 if (jaddref->ja_state & NEWBLOCK)
4447 LIST_REMOVE(jaddref, ja_bmdeps);
4448 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4449 panic("free_jaddref: Bad state %p(0x%X)",
4450 jaddref, jaddref->ja_state);
4451 if (jaddref->ja_mkdir != NULL)
4452 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4453 WORKITEM_FREE(jaddref, D_JADDREF);
4457 * Free a jremref structure once it has been written or discarded.
4460 free_jremref(jremref)
4461 struct jremref *jremref;
4464 if (jremref->jr_ref.if_jsegdep)
4465 free_jsegdep(jremref->jr_ref.if_jsegdep);
4466 if (jremref->jr_state & INPROGRESS)
4467 panic("free_jremref: IO still pending");
4468 WORKITEM_FREE(jremref, D_JREMREF);
4472 * Free a jnewblk structure.
4475 free_jnewblk(jnewblk)
4476 struct jnewblk *jnewblk;
4479 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4481 LIST_REMOVE(jnewblk, jn_deps);
4482 if (jnewblk->jn_dep != NULL)
4483 panic("free_jnewblk: Dependency still attached.");
4484 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4488 * Cancel a jnewblk which has been been made redundant by frag extension.
4491 cancel_jnewblk(jnewblk, wkhd)
4492 struct jnewblk *jnewblk;
4493 struct workhead *wkhd;
4495 struct jsegdep *jsegdep;
4497 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4498 jsegdep = jnewblk->jn_jsegdep;
4499 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4500 panic("cancel_jnewblk: Invalid state");
4501 jnewblk->jn_jsegdep = NULL;
4502 jnewblk->jn_dep = NULL;
4503 jnewblk->jn_state |= GOINGAWAY;
4504 if (jnewblk->jn_state & INPROGRESS) {
4505 jnewblk->jn_state &= ~INPROGRESS;
4506 WORKLIST_REMOVE(&jnewblk->jn_list);
4507 jwork_insert(wkhd, jsegdep);
4509 free_jsegdep(jsegdep);
4510 remove_from_journal(&jnewblk->jn_list);
4512 wake_worklist(&jnewblk->jn_list);
4513 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4517 free_jblkdep(jblkdep)
4518 struct jblkdep *jblkdep;
4521 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4522 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4523 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4524 WORKITEM_FREE(jblkdep, D_JTRUNC);
4526 panic("free_jblkdep: Unexpected type %s",
4527 TYPENAME(jblkdep->jb_list.wk_type));
4531 * Free a single jseg once it is no longer referenced in memory or on
4532 * disk. Reclaim journal blocks and dependencies waiting for the segment
4536 free_jseg(jseg, jblocks)
4538 struct jblocks *jblocks;
4540 struct freework *freework;
4543 * Free freework structures that were lingering to indicate freed
4544 * indirect blocks that forced journal write ordering on reallocate.
4546 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4547 indirblk_remove(freework);
4548 if (jblocks->jb_oldestseg == jseg)
4549 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4550 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4551 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4552 KASSERT(LIST_EMPTY(&jseg->js_entries),
4553 ("free_jseg: Freed jseg has valid entries."));
4554 WORKITEM_FREE(jseg, D_JSEG);
4558 * Free all jsegs that meet the criteria for being reclaimed and update
4563 struct jblocks *jblocks;
4568 * Free only those jsegs which have none allocated before them to
4569 * preserve the journal space ordering.
4571 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4573 * Only reclaim space when nothing depends on this journal
4574 * set and another set has written that it is no longer
4577 if (jseg->js_refs != 0) {
4578 jblocks->jb_oldestseg = jseg;
4581 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4583 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4586 * We can free jsegs that didn't write entries when
4587 * oldestwrseq == js_seq.
4589 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4592 free_jseg(jseg, jblocks);
4595 * If we exited the loop above we still must discover the
4596 * oldest valid segment.
4599 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4600 jseg = TAILQ_NEXT(jseg, js_next))
4601 if (jseg->js_refs != 0)
4603 jblocks->jb_oldestseg = jseg;
4605 * The journal has no valid records but some jsegs may still be
4606 * waiting on oldestwrseq to advance. We force a small record
4607 * out to permit these lingering records to be reclaimed.
4609 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4610 jblocks->jb_needseg = 1;
4614 * Release one reference to a jseg and free it if the count reaches 0. This
4615 * should eventually reclaim journal space as well.
4622 KASSERT(jseg->js_refs > 0,
4623 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4624 if (--jseg->js_refs != 0)
4626 free_jsegs(jseg->js_jblocks);
4630 * Release a jsegdep and decrement the jseg count.
4633 free_jsegdep(jsegdep)
4634 struct jsegdep *jsegdep;
4637 if (jsegdep->jd_seg)
4638 rele_jseg(jsegdep->jd_seg);
4639 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4643 * Wait for a journal item to make it to disk. Initiate journal processing
4648 struct worklist *wk;
4652 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4654 * Blocking journal waits cause slow synchronous behavior. Record
4655 * stats on the frequency of these blocking operations.
4657 if (waitfor == MNT_WAIT) {
4658 stat_journal_wait++;
4659 switch (wk->wk_type) {
4662 stat_jwait_filepage++;
4666 stat_jwait_freeblks++;
4669 stat_jwait_newblk++;
4679 * If IO has not started we process the journal. We can't mark the
4680 * worklist item as IOWAITING because we drop the lock while
4681 * processing the journal and the worklist entry may be freed after
4682 * this point. The caller may call back in and re-issue the request.
4684 if ((wk->wk_state & INPROGRESS) == 0) {
4685 softdep_process_journal(wk->wk_mp, wk, waitfor);
4686 if (waitfor != MNT_WAIT)
4690 if (waitfor != MNT_WAIT)
4692 wait_worklist(wk, "jwait");
4697 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4698 * appropriate. This is a convenience function to reduce duplicate code
4699 * for the setup and revert functions below.
4701 static struct inodedep *
4702 inodedep_lookup_ip(ip)
4705 struct inodedep *inodedep;
4707 KASSERT(ip->i_nlink >= ip->i_effnlink,
4708 ("inodedep_lookup_ip: bad delta"));
4709 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
4711 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4712 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
4718 * Called prior to creating a new inode and linking it to a directory. The
4719 * jaddref structure must already be allocated by softdep_setup_inomapdep
4720 * and it is discovered here so we can initialize the mode and update
4724 softdep_setup_create(dp, ip)
4728 struct inodedep *inodedep;
4729 struct jaddref *jaddref;
4732 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4733 ("softdep_setup_create called on non-softdep filesystem"));
4734 KASSERT(ip->i_nlink == 1,
4735 ("softdep_setup_create: Invalid link count."));
4737 ACQUIRE_LOCK(ITOUMP(dp));
4738 inodedep = inodedep_lookup_ip(ip);
4739 if (DOINGSUJ(dvp)) {
4740 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4742 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4743 ("softdep_setup_create: No addref structure present."));
4745 softdep_prelink(dvp, NULL);
4746 FREE_LOCK(ITOUMP(dp));
4750 * Create a jaddref structure to track the addition of a DOTDOT link when
4751 * we are reparenting an inode as part of a rename. This jaddref will be
4752 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
4753 * non-journaling softdep.
4756 softdep_setup_dotdot_link(dp, ip)
4760 struct inodedep *inodedep;
4761 struct jaddref *jaddref;
4764 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4765 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
4769 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
4770 * is used as a normal link would be.
4773 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4774 dp->i_effnlink - 1, dp->i_mode);
4775 ACQUIRE_LOCK(ITOUMP(dp));
4776 inodedep = inodedep_lookup_ip(dp);
4778 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4780 softdep_prelink(dvp, ITOV(ip));
4781 FREE_LOCK(ITOUMP(dp));
4785 * Create a jaddref structure to track a new link to an inode. The directory
4786 * offset is not known until softdep_setup_directory_add or
4787 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
4791 softdep_setup_link(dp, ip)
4795 struct inodedep *inodedep;
4796 struct jaddref *jaddref;
4799 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4800 ("softdep_setup_link called on non-softdep filesystem"));
4804 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
4806 ACQUIRE_LOCK(ITOUMP(dp));
4807 inodedep = inodedep_lookup_ip(ip);
4809 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4811 softdep_prelink(dvp, ITOV(ip));
4812 FREE_LOCK(ITOUMP(dp));
4816 * Called to create the jaddref structures to track . and .. references as
4817 * well as lookup and further initialize the incomplete jaddref created
4818 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
4819 * nlinkdelta for non-journaling softdep.
4822 softdep_setup_mkdir(dp, ip)
4826 struct inodedep *inodedep;
4827 struct jaddref *dotdotaddref;
4828 struct jaddref *dotaddref;
4829 struct jaddref *jaddref;
4832 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4833 ("softdep_setup_mkdir called on non-softdep filesystem"));
4835 dotaddref = dotdotaddref = NULL;
4836 if (DOINGSUJ(dvp)) {
4837 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
4839 dotaddref->ja_state |= MKDIR_BODY;
4840 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4841 dp->i_effnlink - 1, dp->i_mode);
4842 dotdotaddref->ja_state |= MKDIR_PARENT;
4844 ACQUIRE_LOCK(ITOUMP(dp));
4845 inodedep = inodedep_lookup_ip(ip);
4846 if (DOINGSUJ(dvp)) {
4847 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4849 KASSERT(jaddref != NULL,
4850 ("softdep_setup_mkdir: No addref structure present."));
4851 KASSERT(jaddref->ja_parent == dp->i_number,
4852 ("softdep_setup_mkdir: bad parent %ju",
4853 (uintmax_t)jaddref->ja_parent));
4854 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
4857 inodedep = inodedep_lookup_ip(dp);
4859 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
4860 &dotdotaddref->ja_ref, if_deps);
4861 softdep_prelink(ITOV(dp), NULL);
4862 FREE_LOCK(ITOUMP(dp));
4866 * Called to track nlinkdelta of the inode and parent directories prior to
4867 * unlinking a directory.
4870 softdep_setup_rmdir(dp, ip)
4876 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4877 ("softdep_setup_rmdir called on non-softdep filesystem"));
4879 ACQUIRE_LOCK(ITOUMP(dp));
4880 (void) inodedep_lookup_ip(ip);
4881 (void) inodedep_lookup_ip(dp);
4882 softdep_prelink(dvp, ITOV(ip));
4883 FREE_LOCK(ITOUMP(dp));
4887 * Called to track nlinkdelta of the inode and parent directories prior to
4891 softdep_setup_unlink(dp, ip)
4897 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4898 ("softdep_setup_unlink called on non-softdep filesystem"));
4900 ACQUIRE_LOCK(ITOUMP(dp));
4901 (void) inodedep_lookup_ip(ip);
4902 (void) inodedep_lookup_ip(dp);
4903 softdep_prelink(dvp, ITOV(ip));
4904 FREE_LOCK(ITOUMP(dp));
4908 * Called to release the journal structures created by a failed non-directory
4909 * creation. Adjusts nlinkdelta for non-journaling softdep.
4912 softdep_revert_create(dp, ip)
4916 struct inodedep *inodedep;
4917 struct jaddref *jaddref;
4920 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
4921 ("softdep_revert_create called on non-softdep filesystem"));
4923 ACQUIRE_LOCK(ITOUMP(dp));
4924 inodedep = inodedep_lookup_ip(ip);
4925 if (DOINGSUJ(dvp)) {
4926 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4928 KASSERT(jaddref->ja_parent == dp->i_number,
4929 ("softdep_revert_create: addref parent mismatch"));
4930 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4932 FREE_LOCK(ITOUMP(dp));
4936 * Called to release the journal structures created by a failed link
4937 * addition. Adjusts nlinkdelta for non-journaling softdep.
4940 softdep_revert_link(dp, ip)
4944 struct inodedep *inodedep;
4945 struct jaddref *jaddref;
4948 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4949 ("softdep_revert_link called on non-softdep filesystem"));
4951 ACQUIRE_LOCK(ITOUMP(dp));
4952 inodedep = inodedep_lookup_ip(ip);
4953 if (DOINGSUJ(dvp)) {
4954 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4956 KASSERT(jaddref->ja_parent == dp->i_number,
4957 ("softdep_revert_link: addref parent mismatch"));
4958 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4960 FREE_LOCK(ITOUMP(dp));
4964 * Called to release the journal structures created by a failed mkdir
4965 * attempt. Adjusts nlinkdelta for non-journaling softdep.
4968 softdep_revert_mkdir(dp, ip)
4972 struct inodedep *inodedep;
4973 struct jaddref *jaddref;
4974 struct jaddref *dotaddref;
4977 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4978 ("softdep_revert_mkdir called on non-softdep filesystem"));
4981 ACQUIRE_LOCK(ITOUMP(dp));
4982 inodedep = inodedep_lookup_ip(dp);
4983 if (DOINGSUJ(dvp)) {
4984 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4986 KASSERT(jaddref->ja_parent == ip->i_number,
4987 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
4988 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4990 inodedep = inodedep_lookup_ip(ip);
4991 if (DOINGSUJ(dvp)) {
4992 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4994 KASSERT(jaddref->ja_parent == dp->i_number,
4995 ("softdep_revert_mkdir: addref parent mismatch"));
4996 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
4997 inoreflst, if_deps);
4998 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4999 KASSERT(dotaddref->ja_parent == ip->i_number,
5000 ("softdep_revert_mkdir: dot addref parent mismatch"));
5001 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
5003 FREE_LOCK(ITOUMP(dp));
5007 * Called to correct nlinkdelta after a failed rmdir.
5010 softdep_revert_rmdir(dp, ip)
5015 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5016 ("softdep_revert_rmdir called on non-softdep filesystem"));
5017 ACQUIRE_LOCK(ITOUMP(dp));
5018 (void) inodedep_lookup_ip(ip);
5019 (void) inodedep_lookup_ip(dp);
5020 FREE_LOCK(ITOUMP(dp));
5024 * Protecting the freemaps (or bitmaps).
5026 * To eliminate the need to execute fsck before mounting a filesystem
5027 * after a power failure, one must (conservatively) guarantee that the
5028 * on-disk copy of the bitmaps never indicate that a live inode or block is
5029 * free. So, when a block or inode is allocated, the bitmap should be
5030 * updated (on disk) before any new pointers. When a block or inode is
5031 * freed, the bitmap should not be updated until all pointers have been
5032 * reset. The latter dependency is handled by the delayed de-allocation
5033 * approach described below for block and inode de-allocation. The former
5034 * dependency is handled by calling the following procedure when a block or
5035 * inode is allocated. When an inode is allocated an "inodedep" is created
5036 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5037 * Each "inodedep" is also inserted into the hash indexing structure so
5038 * that any additional link additions can be made dependent on the inode
5041 * The ufs filesystem maintains a number of free block counts (e.g., per
5042 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5043 * in addition to the bitmaps. These counts are used to improve efficiency
5044 * during allocation and therefore must be consistent with the bitmaps.
5045 * There is no convenient way to guarantee post-crash consistency of these
5046 * counts with simple update ordering, for two main reasons: (1) The counts
5047 * and bitmaps for a single cylinder group block are not in the same disk
5048 * sector. If a disk write is interrupted (e.g., by power failure), one may
5049 * be written and the other not. (2) Some of the counts are located in the
5050 * superblock rather than the cylinder group block. So, we focus our soft
5051 * updates implementation on protecting the bitmaps. When mounting a
5052 * filesystem, we recompute the auxiliary counts from the bitmaps.
5056 * Called just after updating the cylinder group block to allocate an inode.
5059 softdep_setup_inomapdep(bp, ip, newinum, mode)
5060 struct buf *bp; /* buffer for cylgroup block with inode map */
5061 struct inode *ip; /* inode related to allocation */
5062 ino_t newinum; /* new inode number being allocated */
5065 struct inodedep *inodedep;
5066 struct bmsafemap *bmsafemap;
5067 struct jaddref *jaddref;
5072 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5073 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5074 fs = VFSTOUFS(mp)->um_fs;
5078 * Allocate the journal reference add structure so that the bitmap
5079 * can be dependent on it.
5081 if (MOUNTEDSUJ(mp)) {
5082 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5083 jaddref->ja_state |= NEWBLOCK;
5087 * Create a dependency for the newly allocated inode.
5088 * Panic if it already exists as something is seriously wrong.
5089 * Otherwise add it to the dependency list for the buffer holding
5090 * the cylinder group map from which it was allocated.
5092 * We have to preallocate a bmsafemap entry in case it is needed
5093 * in bmsafemap_lookup since once we allocate the inodedep, we
5094 * have to finish initializing it before we can FREE_LOCK().
5095 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5096 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5097 * creating the inodedep as it can be freed during the time
5098 * that we FREE_LOCK() while allocating the inodedep. We must
5099 * call workitem_alloc() before entering the locked section as
5100 * it also acquires the lock and we must avoid trying doing so
5103 bmsafemap = malloc(sizeof(struct bmsafemap),
5104 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5105 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5106 ACQUIRE_LOCK(ITOUMP(ip));
5107 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5108 panic("softdep_setup_inomapdep: dependency %p for new"
5109 "inode already exists", inodedep);
5110 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5112 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5113 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5116 inodedep->id_state |= ONDEPLIST;
5117 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5119 inodedep->id_bmsafemap = bmsafemap;
5120 inodedep->id_state &= ~DEPCOMPLETE;
5121 FREE_LOCK(ITOUMP(ip));
5125 * Called just after updating the cylinder group block to
5126 * allocate block or fragment.
5129 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5130 struct buf *bp; /* buffer for cylgroup block with block map */
5131 struct mount *mp; /* filesystem doing allocation */
5132 ufs2_daddr_t newblkno; /* number of newly allocated block */
5133 int frags; /* Number of fragments. */
5134 int oldfrags; /* Previous number of fragments for extend. */
5136 struct newblk *newblk;
5137 struct bmsafemap *bmsafemap;
5138 struct jnewblk *jnewblk;
5139 struct ufsmount *ump;
5142 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5143 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5148 * Create a dependency for the newly allocated block.
5149 * Add it to the dependency list for the buffer holding
5150 * the cylinder group map from which it was allocated.
5152 if (MOUNTEDSUJ(mp)) {
5153 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5154 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5155 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5156 jnewblk->jn_state = ATTACHED;
5157 jnewblk->jn_blkno = newblkno;
5158 jnewblk->jn_frags = frags;
5159 jnewblk->jn_oldfrags = oldfrags;
5167 cgp = (struct cg *)bp->b_data;
5168 blksfree = cg_blksfree(cgp);
5169 bno = dtogd(fs, jnewblk->jn_blkno);
5170 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5172 if (isset(blksfree, bno + i))
5173 panic("softdep_setup_blkmapdep: "
5174 "free fragment %d from %d-%d "
5175 "state 0x%X dep %p", i,
5176 jnewblk->jn_oldfrags,
5186 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5187 newblkno, frags, oldfrags);
5189 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5190 panic("softdep_setup_blkmapdep: found block");
5191 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5192 dtog(fs, newblkno), NULL);
5194 jnewblk->jn_dep = (struct worklist *)newblk;
5195 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5197 newblk->nb_state |= ONDEPLIST;
5198 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5200 newblk->nb_bmsafemap = bmsafemap;
5201 newblk->nb_jnewblk = jnewblk;
5205 #define BMSAFEMAP_HASH(ump, cg) \
5206 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5209 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5210 struct bmsafemap_hashhead *bmsafemaphd;
5212 struct bmsafemap **bmsafemapp;
5214 struct bmsafemap *bmsafemap;
5216 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5217 if (bmsafemap->sm_cg == cg)
5220 *bmsafemapp = bmsafemap;
5229 * Find the bmsafemap associated with a cylinder group buffer.
5230 * If none exists, create one. The buffer must be locked when
5231 * this routine is called and this routine must be called with
5232 * the softdep lock held. To avoid giving up the lock while
5233 * allocating a new bmsafemap, a preallocated bmsafemap may be
5234 * provided. If it is provided but not needed, it is freed.
5236 static struct bmsafemap *
5237 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5241 struct bmsafemap *newbmsafemap;
5243 struct bmsafemap_hashhead *bmsafemaphd;
5244 struct bmsafemap *bmsafemap, *collision;
5245 struct worklist *wk;
5246 struct ufsmount *ump;
5250 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5251 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5252 if (wk->wk_type == D_BMSAFEMAP) {
5254 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5255 return (WK_BMSAFEMAP(wk));
5258 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5259 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5261 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5265 bmsafemap = newbmsafemap;
5268 bmsafemap = malloc(sizeof(struct bmsafemap),
5269 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5270 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5273 bmsafemap->sm_buf = bp;
5274 LIST_INIT(&bmsafemap->sm_inodedephd);
5275 LIST_INIT(&bmsafemap->sm_inodedepwr);
5276 LIST_INIT(&bmsafemap->sm_newblkhd);
5277 LIST_INIT(&bmsafemap->sm_newblkwr);
5278 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5279 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5280 LIST_INIT(&bmsafemap->sm_freehd);
5281 LIST_INIT(&bmsafemap->sm_freewr);
5282 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5283 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5286 bmsafemap->sm_cg = cg;
5287 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5288 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5289 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5294 * Direct block allocation dependencies.
5296 * When a new block is allocated, the corresponding disk locations must be
5297 * initialized (with zeros or new data) before the on-disk inode points to
5298 * them. Also, the freemap from which the block was allocated must be
5299 * updated (on disk) before the inode's pointer. These two dependencies are
5300 * independent of each other and are needed for all file blocks and indirect
5301 * blocks that are pointed to directly by the inode. Just before the
5302 * "in-core" version of the inode is updated with a newly allocated block
5303 * number, a procedure (below) is called to setup allocation dependency
5304 * structures. These structures are removed when the corresponding
5305 * dependencies are satisfied or when the block allocation becomes obsolete
5306 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5307 * fragment that gets upgraded). All of these cases are handled in
5308 * procedures described later.
5310 * When a file extension causes a fragment to be upgraded, either to a larger
5311 * fragment or to a full block, the on-disk location may change (if the
5312 * previous fragment could not simply be extended). In this case, the old
5313 * fragment must be de-allocated, but not until after the inode's pointer has
5314 * been updated. In most cases, this is handled by later procedures, which
5315 * will construct a "freefrag" structure to be added to the workitem queue
5316 * when the inode update is complete (or obsolete). The main exception to
5317 * this is when an allocation occurs while a pending allocation dependency
5318 * (for the same block pointer) remains. This case is handled in the main
5319 * allocation dependency setup procedure by immediately freeing the
5320 * unreferenced fragments.
5323 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5324 struct inode *ip; /* inode to which block is being added */
5325 ufs_lbn_t off; /* block pointer within inode */
5326 ufs2_daddr_t newblkno; /* disk block number being added */
5327 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
5328 long newsize; /* size of new block */
5329 long oldsize; /* size of new block */
5330 struct buf *bp; /* bp for allocated block */
5332 struct allocdirect *adp, *oldadp;
5333 struct allocdirectlst *adphead;
5334 struct freefrag *freefrag;
5335 struct inodedep *inodedep;
5336 struct pagedep *pagedep;
5337 struct jnewblk *jnewblk;
5338 struct newblk *newblk;
5344 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5345 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5346 if (oldblkno && oldblkno != newblkno)
5348 * The usual case is that a smaller fragment that
5349 * was just allocated has been replaced with a bigger
5350 * fragment or a full-size block. If it is marked as
5351 * B_DELWRI, the current contents have not been written
5352 * to disk. It is possible that the block was written
5353 * earlier, but very uncommon. If the block has never
5354 * been written, there is no need to send a BIO_DELETE
5355 * for it when it is freed. The gain from avoiding the
5356 * TRIMs for the common case of unwritten blocks far
5357 * exceeds the cost of the write amplification for the
5358 * uncommon case of failing to send a TRIM for a block
5359 * that had been written.
5361 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5362 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5367 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5368 "off %jd newsize %ld oldsize %d",
5369 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5370 ACQUIRE_LOCK(ITOUMP(ip));
5371 if (off >= UFS_NDADDR) {
5373 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5375 /* allocating an indirect block */
5377 panic("softdep_setup_allocdirect: non-zero indir");
5380 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5383 * Allocating a direct block.
5385 * If we are allocating a directory block, then we must
5386 * allocate an associated pagedep to track additions and
5389 if ((ip->i_mode & IFMT) == IFDIR)
5390 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5393 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5394 panic("softdep_setup_allocdirect: lost block");
5395 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5396 ("softdep_setup_allocdirect: newblk already initialized"));
5398 * Convert the newblk to an allocdirect.
5400 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5401 adp = (struct allocdirect *)newblk;
5402 newblk->nb_freefrag = freefrag;
5403 adp->ad_offset = off;
5404 adp->ad_oldblkno = oldblkno;
5405 adp->ad_newsize = newsize;
5406 adp->ad_oldsize = oldsize;
5409 * Finish initializing the journal.
5411 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5412 jnewblk->jn_ino = ip->i_number;
5413 jnewblk->jn_lbn = lbn;
5414 add_to_journal(&jnewblk->jn_list);
5416 if (freefrag && freefrag->ff_jdep != NULL &&
5417 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5418 add_to_journal(freefrag->ff_jdep);
5419 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5420 adp->ad_inodedep = inodedep;
5422 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5424 * The list of allocdirects must be kept in sorted and ascending
5425 * order so that the rollback routines can quickly determine the
5426 * first uncommitted block (the size of the file stored on disk
5427 * ends at the end of the lowest committed fragment, or if there
5428 * are no fragments, at the end of the highest committed block).
5429 * Since files generally grow, the typical case is that the new
5430 * block is to be added at the end of the list. We speed this
5431 * special case by checking against the last allocdirect in the
5432 * list before laboriously traversing the list looking for the
5435 adphead = &inodedep->id_newinoupdt;
5436 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5437 if (oldadp == NULL || oldadp->ad_offset <= off) {
5438 /* insert at end of list */
5439 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5440 if (oldadp != NULL && oldadp->ad_offset == off)
5441 allocdirect_merge(adphead, adp, oldadp);
5442 FREE_LOCK(ITOUMP(ip));
5445 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5446 if (oldadp->ad_offset >= off)
5450 panic("softdep_setup_allocdirect: lost entry");
5451 /* insert in middle of list */
5452 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5453 if (oldadp->ad_offset == off)
5454 allocdirect_merge(adphead, adp, oldadp);
5456 FREE_LOCK(ITOUMP(ip));
5460 * Merge a newer and older journal record to be stored either in a
5461 * newblock or freefrag. This handles aggregating journal records for
5462 * fragment allocation into a second record as well as replacing a
5463 * journal free with an aborted journal allocation. A segment for the
5464 * oldest record will be placed on wkhd if it has been written. If not
5465 * the segment for the newer record will suffice.
5467 static struct worklist *
5468 jnewblk_merge(new, old, wkhd)
5469 struct worklist *new;
5470 struct worklist *old;
5471 struct workhead *wkhd;
5473 struct jnewblk *njnewblk;
5474 struct jnewblk *jnewblk;
5476 /* Handle NULLs to simplify callers. */
5481 /* Replace a jfreefrag with a jnewblk. */
5482 if (new->wk_type == D_JFREEFRAG) {
5483 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5484 panic("jnewblk_merge: blkno mismatch: %p, %p",
5486 cancel_jfreefrag(WK_JFREEFRAG(new));
5489 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5490 panic("jnewblk_merge: Bad type: old %d new %d\n",
5491 old->wk_type, new->wk_type);
5493 * Handle merging of two jnewblk records that describe
5494 * different sets of fragments in the same block.
5496 jnewblk = WK_JNEWBLK(old);
5497 njnewblk = WK_JNEWBLK(new);
5498 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5499 panic("jnewblk_merge: Merging disparate blocks.");
5501 * The record may be rolled back in the cg.
5503 if (jnewblk->jn_state & UNDONE) {
5504 jnewblk->jn_state &= ~UNDONE;
5505 njnewblk->jn_state |= UNDONE;
5506 njnewblk->jn_state &= ~ATTACHED;
5509 * We modify the newer addref and free the older so that if neither
5510 * has been written the most up-to-date copy will be on disk. If
5511 * both have been written but rolled back we only temporarily need
5512 * one of them to fix the bits when the cg write completes.
5514 jnewblk->jn_state |= ATTACHED | COMPLETE;
5515 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5516 cancel_jnewblk(jnewblk, wkhd);
5517 WORKLIST_REMOVE(&jnewblk->jn_list);
5518 free_jnewblk(jnewblk);
5523 * Replace an old allocdirect dependency with a newer one.
5526 allocdirect_merge(adphead, newadp, oldadp)
5527 struct allocdirectlst *adphead; /* head of list holding allocdirects */
5528 struct allocdirect *newadp; /* allocdirect being added */
5529 struct allocdirect *oldadp; /* existing allocdirect being checked */
5531 struct worklist *wk;
5532 struct freefrag *freefrag;
5535 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5536 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5537 newadp->ad_oldsize != oldadp->ad_newsize ||
5538 newadp->ad_offset >= UFS_NDADDR)
5539 panic("%s %jd != new %jd || old size %ld != new %ld",
5540 "allocdirect_merge: old blkno",
5541 (intmax_t)newadp->ad_oldblkno,
5542 (intmax_t)oldadp->ad_newblkno,
5543 newadp->ad_oldsize, oldadp->ad_newsize);
5544 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5545 newadp->ad_oldsize = oldadp->ad_oldsize;
5547 * If the old dependency had a fragment to free or had never
5548 * previously had a block allocated, then the new dependency
5549 * can immediately post its freefrag and adopt the old freefrag.
5550 * This action is done by swapping the freefrag dependencies.
5551 * The new dependency gains the old one's freefrag, and the
5552 * old one gets the new one and then immediately puts it on
5553 * the worklist when it is freed by free_newblk. It is
5554 * not possible to do this swap when the old dependency had a
5555 * non-zero size but no previous fragment to free. This condition
5556 * arises when the new block is an extension of the old block.
5557 * Here, the first part of the fragment allocated to the new
5558 * dependency is part of the block currently claimed on disk by
5559 * the old dependency, so cannot legitimately be freed until the
5560 * conditions for the new dependency are fulfilled.
5562 freefrag = newadp->ad_freefrag;
5563 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5564 newadp->ad_freefrag = oldadp->ad_freefrag;
5565 oldadp->ad_freefrag = freefrag;
5568 * If we are tracking a new directory-block allocation,
5569 * move it from the old allocdirect to the new allocdirect.
5571 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5572 WORKLIST_REMOVE(wk);
5573 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5574 panic("allocdirect_merge: extra newdirblk");
5575 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5577 TAILQ_REMOVE(adphead, oldadp, ad_next);
5579 * We need to move any journal dependencies over to the freefrag
5580 * that releases this block if it exists. Otherwise we are
5581 * extending an existing block and we'll wait until that is
5582 * complete to release the journal space and extend the
5583 * new journal to cover this old space as well.
5585 if (freefrag == NULL) {
5586 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5587 panic("allocdirect_merge: %jd != %jd",
5588 oldadp->ad_newblkno, newadp->ad_newblkno);
5589 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5590 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5591 &oldadp->ad_block.nb_jnewblk->jn_list,
5592 &newadp->ad_block.nb_jwork);
5593 oldadp->ad_block.nb_jnewblk = NULL;
5594 cancel_newblk(&oldadp->ad_block, NULL,
5595 &newadp->ad_block.nb_jwork);
5597 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5598 &freefrag->ff_list, &freefrag->ff_jwork);
5599 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5600 &freefrag->ff_jwork);
5602 free_newblk(&oldadp->ad_block);
5606 * Allocate a jfreefrag structure to journal a single block free.
5608 static struct jfreefrag *
5609 newjfreefrag(freefrag, ip, blkno, size, lbn)
5610 struct freefrag *freefrag;
5616 struct jfreefrag *jfreefrag;
5620 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5622 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5623 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5624 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5625 jfreefrag->fr_ino = ip->i_number;
5626 jfreefrag->fr_lbn = lbn;
5627 jfreefrag->fr_blkno = blkno;
5628 jfreefrag->fr_frags = numfrags(fs, size);
5629 jfreefrag->fr_freefrag = freefrag;
5635 * Allocate a new freefrag structure.
5637 static struct freefrag *
5638 newfreefrag(ip, blkno, size, lbn, key)
5645 struct freefrag *freefrag;
5646 struct ufsmount *ump;
5649 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5650 ip->i_number, blkno, size, lbn);
5653 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5654 panic("newfreefrag: frag size");
5655 freefrag = malloc(sizeof(struct freefrag),
5656 M_FREEFRAG, M_SOFTDEP_FLAGS);
5657 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5658 freefrag->ff_state = ATTACHED;
5659 LIST_INIT(&freefrag->ff_jwork);
5660 freefrag->ff_inum = ip->i_number;
5661 freefrag->ff_vtype = ITOV(ip)->v_type;
5662 freefrag->ff_blkno = blkno;
5663 freefrag->ff_fragsize = size;
5664 freefrag->ff_key = key;
5666 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5667 freefrag->ff_jdep = (struct worklist *)
5668 newjfreefrag(freefrag, ip, blkno, size, lbn);
5670 freefrag->ff_state |= DEPCOMPLETE;
5671 freefrag->ff_jdep = NULL;
5678 * This workitem de-allocates fragments that were replaced during
5679 * file block allocation.
5682 handle_workitem_freefrag(freefrag)
5683 struct freefrag *freefrag;
5685 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5686 struct workhead wkhd;
5689 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5690 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5692 * It would be illegal to add new completion items to the
5693 * freefrag after it was schedule to be done so it must be
5694 * safe to modify the list head here.
5698 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5700 * If the journal has not been written we must cancel it here.
5702 if (freefrag->ff_jdep) {
5703 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5704 panic("handle_workitem_freefrag: Unexpected type %d\n",
5705 freefrag->ff_jdep->wk_type);
5706 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5709 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5710 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
5711 &wkhd, freefrag->ff_key);
5713 WORKITEM_FREE(freefrag, D_FREEFRAG);
5718 * Set up a dependency structure for an external attributes data block.
5719 * This routine follows much of the structure of softdep_setup_allocdirect.
5720 * See the description of softdep_setup_allocdirect above for details.
5723 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5726 ufs2_daddr_t newblkno;
5727 ufs2_daddr_t oldblkno;
5732 struct allocdirect *adp, *oldadp;
5733 struct allocdirectlst *adphead;
5734 struct freefrag *freefrag;
5735 struct inodedep *inodedep;
5736 struct jnewblk *jnewblk;
5737 struct newblk *newblk;
5739 struct ufsmount *ump;
5744 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5745 ("softdep_setup_allocext called on non-softdep filesystem"));
5746 KASSERT(off < UFS_NXADDR,
5747 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
5750 if (oldblkno && oldblkno != newblkno)
5752 * The usual case is that a smaller fragment that
5753 * was just allocated has been replaced with a bigger
5754 * fragment or a full-size block. If it is marked as
5755 * B_DELWRI, the current contents have not been written
5756 * to disk. It is possible that the block was written
5757 * earlier, but very uncommon. If the block has never
5758 * been written, there is no need to send a BIO_DELETE
5759 * for it when it is freed. The gain from avoiding the
5760 * TRIMs for the common case of unwritten blocks far
5761 * exceeds the cost of the write amplification for the
5762 * uncommon case of failing to send a TRIM for a block
5763 * that had been written.
5765 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5766 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5771 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5772 panic("softdep_setup_allocext: lost block");
5773 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5774 ("softdep_setup_allocext: newblk already initialized"));
5776 * Convert the newblk to an allocdirect.
5778 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5779 adp = (struct allocdirect *)newblk;
5780 newblk->nb_freefrag = freefrag;
5781 adp->ad_offset = off;
5782 adp->ad_oldblkno = oldblkno;
5783 adp->ad_newsize = newsize;
5784 adp->ad_oldsize = oldsize;
5785 adp->ad_state |= EXTDATA;
5788 * Finish initializing the journal.
5790 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5791 jnewblk->jn_ino = ip->i_number;
5792 jnewblk->jn_lbn = lbn;
5793 add_to_journal(&jnewblk->jn_list);
5795 if (freefrag && freefrag->ff_jdep != NULL &&
5796 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5797 add_to_journal(freefrag->ff_jdep);
5798 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5799 adp->ad_inodedep = inodedep;
5801 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5803 * The list of allocdirects must be kept in sorted and ascending
5804 * order so that the rollback routines can quickly determine the
5805 * first uncommitted block (the size of the file stored on disk
5806 * ends at the end of the lowest committed fragment, or if there
5807 * are no fragments, at the end of the highest committed block).
5808 * Since files generally grow, the typical case is that the new
5809 * block is to be added at the end of the list. We speed this
5810 * special case by checking against the last allocdirect in the
5811 * list before laboriously traversing the list looking for the
5814 adphead = &inodedep->id_newextupdt;
5815 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5816 if (oldadp == NULL || oldadp->ad_offset <= off) {
5817 /* insert at end of list */
5818 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5819 if (oldadp != NULL && oldadp->ad_offset == off)
5820 allocdirect_merge(adphead, adp, oldadp);
5824 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5825 if (oldadp->ad_offset >= off)
5829 panic("softdep_setup_allocext: lost entry");
5830 /* insert in middle of list */
5831 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5832 if (oldadp->ad_offset == off)
5833 allocdirect_merge(adphead, adp, oldadp);
5838 * Indirect block allocation dependencies.
5840 * The same dependencies that exist for a direct block also exist when
5841 * a new block is allocated and pointed to by an entry in a block of
5842 * indirect pointers. The undo/redo states described above are also
5843 * used here. Because an indirect block contains many pointers that
5844 * may have dependencies, a second copy of the entire in-memory indirect
5845 * block is kept. The buffer cache copy is always completely up-to-date.
5846 * The second copy, which is used only as a source for disk writes,
5847 * contains only the safe pointers (i.e., those that have no remaining
5848 * update dependencies). The second copy is freed when all pointers
5849 * are safe. The cache is not allowed to replace indirect blocks with
5850 * pending update dependencies. If a buffer containing an indirect
5851 * block with dependencies is written, these routines will mark it
5852 * dirty again. It can only be successfully written once all the
5853 * dependencies are removed. The ffs_fsync routine in conjunction with
5854 * softdep_sync_metadata work together to get all the dependencies
5855 * removed so that a file can be successfully written to disk. Three
5856 * procedures are used when setting up indirect block pointer
5857 * dependencies. The division is necessary because of the organization
5858 * of the "balloc" routine and because of the distinction between file
5859 * pages and file metadata blocks.
5863 * Allocate a new allocindir structure.
5865 static struct allocindir *
5866 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
5867 struct inode *ip; /* inode for file being extended */
5868 int ptrno; /* offset of pointer in indirect block */
5869 ufs2_daddr_t newblkno; /* disk block number being added */
5870 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
5873 struct newblk *newblk;
5874 struct allocindir *aip;
5875 struct freefrag *freefrag;
5876 struct jnewblk *jnewblk;
5879 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
5883 ACQUIRE_LOCK(ITOUMP(ip));
5884 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
5885 panic("new_allocindir: lost block");
5886 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5887 ("newallocindir: newblk already initialized"));
5888 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
5889 newblk->nb_freefrag = freefrag;
5890 aip = (struct allocindir *)newblk;
5891 aip->ai_offset = ptrno;
5892 aip->ai_oldblkno = oldblkno;
5894 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5895 jnewblk->jn_ino = ip->i_number;
5896 jnewblk->jn_lbn = lbn;
5897 add_to_journal(&jnewblk->jn_list);
5899 if (freefrag && freefrag->ff_jdep != NULL &&
5900 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5901 add_to_journal(freefrag->ff_jdep);
5906 * Called just before setting an indirect block pointer
5907 * to a newly allocated file page.
5910 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
5911 struct inode *ip; /* inode for file being extended */
5912 ufs_lbn_t lbn; /* allocated block number within file */
5913 struct buf *bp; /* buffer with indirect blk referencing page */
5914 int ptrno; /* offset of pointer in indirect block */
5915 ufs2_daddr_t newblkno; /* disk block number being added */
5916 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
5917 struct buf *nbp; /* buffer holding allocated page */
5919 struct inodedep *inodedep;
5920 struct freefrag *freefrag;
5921 struct allocindir *aip;
5922 struct pagedep *pagedep;
5924 struct ufsmount *ump;
5928 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5929 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
5930 KASSERT(lbn == nbp->b_lblkno,
5931 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
5932 lbn, bp->b_lblkno));
5934 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
5935 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
5936 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
5937 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
5938 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5940 * If we are allocating a directory page, then we must
5941 * allocate an associated pagedep to track additions and
5944 if ((ip->i_mode & IFMT) == IFDIR)
5945 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
5946 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5947 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
5950 handle_workitem_freefrag(freefrag);
5954 * Called just before setting an indirect block pointer to a
5955 * newly allocated indirect block.
5958 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
5959 struct buf *nbp; /* newly allocated indirect block */
5960 struct inode *ip; /* inode for file being extended */
5961 struct buf *bp; /* indirect block referencing allocated block */
5962 int ptrno; /* offset of pointer in indirect block */
5963 ufs2_daddr_t newblkno; /* disk block number being added */
5965 struct inodedep *inodedep;
5966 struct allocindir *aip;
5967 struct ufsmount *ump;
5971 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
5972 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
5974 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
5975 ip->i_number, newblkno, ptrno);
5976 lbn = nbp->b_lblkno;
5977 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
5978 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
5979 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
5980 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5981 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
5982 panic("softdep_setup_allocindir_meta: Block already existed");
5987 indirdep_complete(indirdep)
5988 struct indirdep *indirdep;
5990 struct allocindir *aip;
5992 LIST_REMOVE(indirdep, ir_next);
5993 indirdep->ir_state |= DEPCOMPLETE;
5995 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
5996 LIST_REMOVE(aip, ai_next);
5997 free_newblk(&aip->ai_block);
6000 * If this indirdep is not attached to a buf it was simply waiting
6001 * on completion to clear completehd. free_indirdep() asserts
6002 * that nothing is dangling.
6004 if ((indirdep->ir_state & ONWORKLIST) == 0)
6005 free_indirdep(indirdep);
6008 static struct indirdep *
6009 indirdep_lookup(mp, ip, bp)
6014 struct indirdep *indirdep, *newindirdep;
6015 struct newblk *newblk;
6016 struct ufsmount *ump;
6017 struct worklist *wk;
6027 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6028 if (wk->wk_type != D_INDIRDEP)
6030 indirdep = WK_INDIRDEP(wk);
6033 /* Found on the buffer worklist, no new structure to free. */
6034 if (indirdep != NULL && newindirdep == NULL)
6036 if (indirdep != NULL && newindirdep != NULL)
6037 panic("indirdep_lookup: simultaneous create");
6038 /* None found on the buffer and a new structure is ready. */
6039 if (indirdep == NULL && newindirdep != NULL)
6041 /* None found and no new structure available. */
6043 newindirdep = malloc(sizeof(struct indirdep),
6044 M_INDIRDEP, M_SOFTDEP_FLAGS);
6045 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6046 newindirdep->ir_state = ATTACHED;
6048 newindirdep->ir_state |= UFS1FMT;
6049 TAILQ_INIT(&newindirdep->ir_trunc);
6050 newindirdep->ir_saveddata = NULL;
6051 LIST_INIT(&newindirdep->ir_deplisthd);
6052 LIST_INIT(&newindirdep->ir_donehd);
6053 LIST_INIT(&newindirdep->ir_writehd);
6054 LIST_INIT(&newindirdep->ir_completehd);
6055 if (bp->b_blkno == bp->b_lblkno) {
6056 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6058 bp->b_blkno = blkno;
6060 newindirdep->ir_freeblks = NULL;
6061 newindirdep->ir_savebp =
6062 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6063 newindirdep->ir_bp = bp;
6064 BUF_KERNPROC(newindirdep->ir_savebp);
6065 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6068 indirdep = newindirdep;
6069 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6071 * If the block is not yet allocated we don't set DEPCOMPLETE so
6072 * that we don't free dependencies until the pointers are valid.
6073 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6074 * than using the hash.
6076 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6077 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6079 indirdep->ir_state |= DEPCOMPLETE;
6084 * Called to finish the allocation of the "aip" allocated
6085 * by one of the two routines above.
6087 static struct freefrag *
6088 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
6089 struct buf *bp; /* in-memory copy of the indirect block */
6090 struct inode *ip; /* inode for file being extended */
6091 struct inodedep *inodedep; /* Inodedep for ip */
6092 struct allocindir *aip; /* allocindir allocated by the above routines */
6093 ufs_lbn_t lbn; /* Logical block number for this block. */
6096 struct indirdep *indirdep;
6097 struct allocindir *oldaip;
6098 struct freefrag *freefrag;
6100 struct ufsmount *ump;
6106 if (bp->b_lblkno >= 0)
6107 panic("setup_allocindir_phase2: not indir blk");
6108 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6109 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6110 indirdep = indirdep_lookup(mp, ip, bp);
6111 KASSERT(indirdep->ir_savebp != NULL,
6112 ("setup_allocindir_phase2 NULL ir_savebp"));
6113 aip->ai_indirdep = indirdep;
6115 * Check for an unwritten dependency for this indirect offset. If
6116 * there is, merge the old dependency into the new one. This happens
6117 * as a result of reallocblk only.
6120 if (aip->ai_oldblkno != 0) {
6121 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6122 if (oldaip->ai_offset == aip->ai_offset) {
6123 freefrag = allocindir_merge(aip, oldaip);
6127 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6128 if (oldaip->ai_offset == aip->ai_offset) {
6129 freefrag = allocindir_merge(aip, oldaip);
6135 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6140 * Merge two allocindirs which refer to the same block. Move newblock
6141 * dependencies and setup the freefrags appropriately.
6143 static struct freefrag *
6144 allocindir_merge(aip, oldaip)
6145 struct allocindir *aip;
6146 struct allocindir *oldaip;
6148 struct freefrag *freefrag;
6149 struct worklist *wk;
6151 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6152 panic("allocindir_merge: blkno");
6153 aip->ai_oldblkno = oldaip->ai_oldblkno;
6154 freefrag = aip->ai_freefrag;
6155 aip->ai_freefrag = oldaip->ai_freefrag;
6156 oldaip->ai_freefrag = NULL;
6157 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6159 * If we are tracking a new directory-block allocation,
6160 * move it from the old allocindir to the new allocindir.
6162 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6163 WORKLIST_REMOVE(wk);
6164 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6165 panic("allocindir_merge: extra newdirblk");
6166 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6169 * We can skip journaling for this freefrag and just complete
6170 * any pending journal work for the allocindir that is being
6171 * removed after the freefrag completes.
6173 if (freefrag->ff_jdep)
6174 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6175 LIST_REMOVE(oldaip, ai_next);
6176 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6177 &freefrag->ff_list, &freefrag->ff_jwork);
6178 free_newblk(&oldaip->ai_block);
6184 setup_freedirect(freeblks, ip, i, needj)
6185 struct freeblks *freeblks;
6190 struct ufsmount *ump;
6194 blkno = DIP(ip, i_db[i]);
6197 DIP_SET(ip, i_db[i], 0);
6199 frags = sblksize(ump->um_fs, ip->i_size, i);
6200 frags = numfrags(ump->um_fs, frags);
6201 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6205 setup_freeext(freeblks, ip, i, needj)
6206 struct freeblks *freeblks;
6211 struct ufsmount *ump;
6215 blkno = ip->i_din2->di_extb[i];
6218 ip->i_din2->di_extb[i] = 0;
6220 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6221 frags = numfrags(ump->um_fs, frags);
6222 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6226 setup_freeindir(freeblks, ip, i, lbn, needj)
6227 struct freeblks *freeblks;
6233 struct ufsmount *ump;
6236 blkno = DIP(ip, i_ib[i]);
6239 DIP_SET(ip, i_ib[i], 0);
6241 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6245 static inline struct freeblks *
6250 struct freeblks *freeblks;
6252 freeblks = malloc(sizeof(struct freeblks),
6253 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6254 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6255 LIST_INIT(&freeblks->fb_jblkdephd);
6256 LIST_INIT(&freeblks->fb_jwork);
6257 freeblks->fb_ref = 0;
6258 freeblks->fb_cgwait = 0;
6259 freeblks->fb_state = ATTACHED;
6260 freeblks->fb_uid = ip->i_uid;
6261 freeblks->fb_inum = ip->i_number;
6262 freeblks->fb_vtype = ITOV(ip)->v_type;
6263 freeblks->fb_modrev = DIP(ip, i_modrev);
6264 freeblks->fb_devvp = ITODEVVP(ip);
6265 freeblks->fb_chkcnt = 0;
6266 freeblks->fb_len = 0;
6272 trunc_indirdep(indirdep, freeblks, bp, off)
6273 struct indirdep *indirdep;
6274 struct freeblks *freeblks;
6278 struct allocindir *aip, *aipn;
6281 * The first set of allocindirs won't be in savedbp.
6283 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6284 if (aip->ai_offset > off)
6285 cancel_allocindir(aip, bp, freeblks, 1);
6286 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6287 if (aip->ai_offset > off)
6288 cancel_allocindir(aip, bp, freeblks, 1);
6290 * These will exist in savedbp.
6292 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6293 if (aip->ai_offset > off)
6294 cancel_allocindir(aip, NULL, freeblks, 0);
6295 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6296 if (aip->ai_offset > off)
6297 cancel_allocindir(aip, NULL, freeblks, 0);
6301 * Follow the chain of indirects down to lastlbn creating a freework
6302 * structure for each. This will be used to start indir_trunc() at
6303 * the right offset and create the journal records for the parrtial
6304 * truncation. A second step will handle the truncated dependencies.
6307 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6308 struct freeblks *freeblks;
6314 struct indirdep *indirdep;
6315 struct indirdep *indirn;
6316 struct freework *freework;
6317 struct newblk *newblk;
6319 struct ufsmount *ump;
6331 mp = freeblks->fb_list.wk_mp;
6334 * Here, calls to VOP_BMAP() will fail. However, we already have
6335 * the on-disk address, so we just pass it to bread() instead of
6336 * having bread() attempt to calculate it using VOP_BMAP().
6338 error = ffs_breadz(ump, ITOV(ip), lbn, blkptrtodb(ump, blkno),
6339 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6342 level = lbn_level(lbn);
6343 lbnadd = lbn_offset(ump->um_fs, level);
6345 * Compute the offset of the last block we want to keep. Store
6346 * in the freework the first block we want to completely free.
6348 off = (lastlbn - -(lbn + level)) / lbnadd;
6349 if (off + 1 == NINDIR(ump->um_fs))
6351 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6353 * Link the freework into the indirdep. This will prevent any new
6354 * allocations from proceeding until we are finished with the
6355 * truncate and the block is written.
6358 indirdep = indirdep_lookup(mp, ip, bp);
6359 if (indirdep->ir_freeblks)
6360 panic("setup_trunc_indir: indirdep already truncated.");
6361 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6362 freework->fw_indir = indirdep;
6364 * Cancel any allocindirs that will not make it to disk.
6365 * We have to do this for all copies of the indirdep that
6366 * live on this newblk.
6368 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6369 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6371 panic("setup_trunc_indir: lost block");
6372 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6373 trunc_indirdep(indirn, freeblks, bp, off);
6375 trunc_indirdep(indirdep, freeblks, bp, off);
6378 * Creation is protected by the buf lock. The saveddata is only
6379 * needed if a full truncation follows a partial truncation but it
6380 * is difficult to allocate in that case so we fetch it anyway.
6382 if (indirdep->ir_saveddata == NULL)
6383 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6386 /* Fetch the blkno of the child and the zero start offset. */
6387 if (I_IS_UFS1(ip)) {
6388 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6389 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6391 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6392 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6395 /* Zero the truncated pointers. */
6396 end = bp->b_data + bp->b_bcount;
6397 bzero(start, end - start);
6403 lbn++; /* adjust level */
6404 lbn -= (off * lbnadd);
6405 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6409 * Complete the partial truncation of an indirect block setup by
6410 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6411 * copy and writes them to disk before the freeblks is allowed to complete.
6414 complete_trunc_indir(freework)
6415 struct freework *freework;
6417 struct freework *fwn;
6418 struct indirdep *indirdep;
6419 struct ufsmount *ump;
6424 ump = VFSTOUFS(freework->fw_list.wk_mp);
6426 indirdep = freework->fw_indir;
6428 bp = indirdep->ir_bp;
6429 /* See if the block was discarded. */
6432 /* Inline part of getdirtybuf(). We dont want bremfree. */
6433 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6435 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6436 LOCK_PTR(ump)) == 0)
6440 freework->fw_state |= DEPCOMPLETE;
6441 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6443 * Zero the pointers in the saved copy.
6445 if (indirdep->ir_state & UFS1FMT)
6446 start = sizeof(ufs1_daddr_t);
6448 start = sizeof(ufs2_daddr_t);
6449 start *= freework->fw_start;
6450 count = indirdep->ir_savebp->b_bcount - start;
6451 start += (uintptr_t)indirdep->ir_savebp->b_data;
6452 bzero((char *)start, count);
6454 * We need to start the next truncation in the list if it has not
6457 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6459 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6460 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6461 if ((fwn->fw_state & ONWORKLIST) == 0)
6462 freework_enqueue(fwn);
6465 * If bp is NULL the block was fully truncated, restore
6466 * the saved block list otherwise free it if it is no
6469 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6471 bcopy(indirdep->ir_saveddata,
6472 indirdep->ir_savebp->b_data,
6473 indirdep->ir_savebp->b_bcount);
6474 free(indirdep->ir_saveddata, M_INDIRDEP);
6475 indirdep->ir_saveddata = NULL;
6478 * When bp is NULL there is a full truncation pending. We
6479 * must wait for this full truncation to be journaled before
6480 * we can release this freework because the disk pointers will
6481 * never be written as zero.
6484 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6485 handle_written_freework(freework);
6487 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6488 &freework->fw_list);
6490 freework->fw_indir = (void *)0x0000deadbeef0000;
6491 bp = indirdep->ir_savebp;
6492 indirdep->ir_savebp = NULL;
6493 free_indirdep(indirdep);
6499 /* Complete when the real copy is written. */
6500 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6506 * Calculate the number of blocks we are going to release where datablocks
6507 * is the current total and length is the new file size.
6510 blkcount(fs, datablocks, length)
6512 ufs2_daddr_t datablocks;
6515 off_t totblks, numblks;
6518 numblks = howmany(length, fs->fs_bsize);
6519 if (numblks <= UFS_NDADDR) {
6520 totblks = howmany(length, fs->fs_fsize);
6523 totblks = blkstofrags(fs, numblks);
6524 numblks -= UFS_NDADDR;
6526 * Count all single, then double, then triple indirects required.
6527 * Subtracting one indirects worth of blocks for each pass
6528 * acknowledges one of each pointed to by the inode.
6531 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6532 numblks -= NINDIR(fs);
6535 numblks = howmany(numblks, NINDIR(fs));
6538 totblks = fsbtodb(fs, totblks);
6540 * Handle sparse files. We can't reclaim more blocks than the inode
6541 * references. We will correct it later in handle_complete_freeblks()
6542 * when we know the real count.
6544 if (totblks > datablocks)
6546 return (datablocks - totblks);
6550 * Handle freeblocks for journaled softupdate filesystems.
6552 * Contrary to normal softupdates, we must preserve the block pointers in
6553 * indirects until their subordinates are free. This is to avoid journaling
6554 * every block that is freed which may consume more space than the journal
6555 * itself. The recovery program will see the free block journals at the
6556 * base of the truncated area and traverse them to reclaim space. The
6557 * pointers in the inode may be cleared immediately after the journal
6558 * records are written because each direct and indirect pointer in the
6559 * inode is recorded in a journal. This permits full truncation to proceed
6560 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6562 * The algorithm is as follows:
6563 * 1) Traverse the in-memory state and create journal entries to release
6564 * the relevant blocks and full indirect trees.
6565 * 2) Traverse the indirect block chain adding partial truncation freework
6566 * records to indirects in the path to lastlbn. The freework will
6567 * prevent new allocation dependencies from being satisfied in this
6568 * indirect until the truncation completes.
6569 * 3) Read and lock the inode block, performing an update with the new size
6570 * and pointers. This prevents truncated data from becoming valid on
6571 * disk through step 4.
6572 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6573 * eliminate journal work for those records that do not require it.
6574 * 5) Schedule the journal records to be written followed by the inode block.
6575 * 6) Allocate any necessary frags for the end of file.
6576 * 7) Zero any partially truncated blocks.
6578 * From this truncation proceeds asynchronously using the freework and
6579 * indir_trunc machinery. The file will not be extended again into a
6580 * partially truncated indirect block until all work is completed but
6581 * the normal dependency mechanism ensures that it is rolled back/forward
6582 * as appropriate. Further truncation may occur without delay and is
6583 * serialized in indir_trunc().
6586 softdep_journal_freeblocks(ip, cred, length, flags)
6587 struct inode *ip; /* The inode whose length is to be reduced */
6589 off_t length; /* The new length for the file */
6590 int flags; /* IO_EXT and/or IO_NORMAL */
6592 struct freeblks *freeblks, *fbn;
6593 struct worklist *wk, *wkn;
6594 struct inodedep *inodedep;
6595 struct jblkdep *jblkdep;
6596 struct allocdirect *adp, *adpn;
6597 struct ufsmount *ump;
6603 ufs2_daddr_t extblocks, datablocks;
6604 ufs_lbn_t tmpval, lbn, lastlbn;
6605 int frags, lastoff, iboff, allocblock, needj, error, i;
6610 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6611 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6619 freeblks = newfreeblks(mp, ip);
6622 * If we're truncating a removed file that will never be written
6623 * we don't need to journal the block frees. The canceled journals
6624 * for the allocations will suffice.
6626 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6627 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6630 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6631 ip->i_number, length, needj);
6634 * Calculate the lbn that we are truncating to. This results in -1
6635 * if we're truncating the 0 bytes. So it is the last lbn we want
6636 * to keep, not the first lbn we want to truncate.
6638 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6639 lastoff = blkoff(fs, length);
6641 * Compute frags we are keeping in lastlbn. 0 means all.
6643 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6644 frags = fragroundup(fs, lastoff);
6645 /* adp offset of last valid allocdirect. */
6647 } else if (lastlbn > 0)
6649 if (fs->fs_magic == FS_UFS2_MAGIC)
6650 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6652 * Handle normal data blocks and indirects. This section saves
6653 * values used after the inode update to complete frag and indirect
6656 if ((flags & IO_NORMAL) != 0) {
6658 * Handle truncation of whole direct and indirect blocks.
6660 for (i = iboff + 1; i < UFS_NDADDR; i++)
6661 setup_freedirect(freeblks, ip, i, needj);
6662 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6664 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6665 /* Release a whole indirect tree. */
6666 if (lbn > lastlbn) {
6667 setup_freeindir(freeblks, ip, i, -lbn -i,
6671 iboff = i + UFS_NDADDR;
6673 * Traverse partially truncated indirect tree.
6675 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6676 setup_trunc_indir(freeblks, ip, -lbn - i,
6677 lastlbn, DIP(ip, i_ib[i]));
6680 * Handle partial truncation to a frag boundary.
6686 oldfrags = blksize(fs, ip, lastlbn);
6687 blkno = DIP(ip, i_db[lastlbn]);
6688 if (blkno && oldfrags != frags) {
6690 oldfrags = numfrags(fs, oldfrags);
6691 blkno += numfrags(fs, frags);
6692 newfreework(ump, freeblks, NULL, lastlbn,
6693 blkno, oldfrags, 0, needj);
6695 adjust_newfreework(freeblks,
6696 numfrags(fs, frags));
6697 } else if (blkno == 0)
6701 * Add a journal record for partial truncate if we are
6702 * handling indirect blocks. Non-indirects need no extra
6705 if (length != 0 && lastlbn >= UFS_NDADDR) {
6706 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
6707 newjtrunc(freeblks, length, 0);
6709 ip->i_size = length;
6710 DIP_SET(ip, i_size, ip->i_size);
6711 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6712 datablocks = DIP(ip, i_blocks) - extblocks;
6714 datablocks = blkcount(fs, datablocks, length);
6715 freeblks->fb_len = length;
6717 if ((flags & IO_EXT) != 0) {
6718 for (i = 0; i < UFS_NXADDR; i++)
6719 setup_freeext(freeblks, ip, i, needj);
6720 ip->i_din2->di_extsize = 0;
6721 datablocks += extblocks;
6722 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6725 /* Reference the quotas in case the block count is wrong in the end. */
6726 quotaref(vp, freeblks->fb_quota);
6727 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
6729 freeblks->fb_chkcnt = -datablocks;
6731 fs->fs_pendingblocks += datablocks;
6733 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6735 * Handle truncation of incomplete alloc direct dependencies. We
6736 * hold the inode block locked to prevent incomplete dependencies
6737 * from reaching the disk while we are eliminating those that
6738 * have been truncated. This is a partially inlined ffs_update().
6741 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
6742 dbn = fsbtodb(fs, ino_to_fsba(fs, ip->i_number));
6743 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn, (int)fs->fs_bsize,
6744 NULL, NULL, 0, cred, 0, NULL, &bp);
6746 softdep_error("softdep_journal_freeblocks", error);
6749 if (bp->b_bufsize == fs->fs_bsize)
6750 bp->b_flags |= B_CLUSTEROK;
6751 softdep_update_inodeblock(ip, bp, 0);
6752 if (ump->um_fstype == UFS1) {
6753 *((struct ufs1_dinode *)bp->b_data +
6754 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
6756 ffs_update_dinode_ckhash(fs, ip->i_din2);
6757 *((struct ufs2_dinode *)bp->b_data +
6758 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
6761 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6762 if ((inodedep->id_state & IOSTARTED) != 0)
6763 panic("softdep_setup_freeblocks: inode busy");
6765 * Add the freeblks structure to the list of operations that
6766 * must await the zero'ed inode being written to disk. If we
6767 * still have a bitmap dependency (needj), then the inode
6768 * has never been written to disk, so we can process the
6769 * freeblks below once we have deleted the dependencies.
6772 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6774 freeblks->fb_state |= COMPLETE;
6775 if ((flags & IO_NORMAL) != 0) {
6776 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
6777 if (adp->ad_offset > iboff)
6778 cancel_allocdirect(&inodedep->id_inoupdt, adp,
6781 * Truncate the allocdirect. We could eliminate
6782 * or modify journal records as well.
6784 else if (adp->ad_offset == iboff && frags)
6785 adp->ad_newsize = frags;
6788 if ((flags & IO_EXT) != 0)
6789 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
6790 cancel_allocdirect(&inodedep->id_extupdt, adp,
6793 * Scan the bufwait list for newblock dependencies that will never
6796 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6797 if (wk->wk_type != D_ALLOCDIRECT)
6799 adp = WK_ALLOCDIRECT(wk);
6800 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
6801 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
6802 cancel_jfreeblk(freeblks, adp->ad_newblkno);
6803 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
6804 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
6810 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6811 add_to_journal(&jblkdep->jb_list);
6815 * Truncate dependency structures beyond length.
6817 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
6819 * This is only set when we need to allocate a fragment because
6820 * none existed at the end of a frag-sized file. It handles only
6821 * allocating a new, zero filled block.
6824 ip->i_size = length - lastoff;
6825 DIP_SET(ip, i_size, ip->i_size);
6826 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
6828 softdep_error("softdep_journal_freeblks", error);
6831 ip->i_size = length;
6832 DIP_SET(ip, i_size, length);
6833 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
6834 allocbuf(bp, frags);
6837 } else if (lastoff != 0 && vp->v_type != VDIR) {
6841 * Zero the end of a truncated frag or block.
6843 size = sblksize(fs, length, lastlbn);
6844 error = bread(vp, lastlbn, size, cred, &bp);
6846 bzero((char *)bp->b_data + lastoff, size - lastoff);
6848 } else if (!ffs_fsfail_cleanup(ump, error)) {
6849 softdep_error("softdep_journal_freeblks", error);
6854 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6855 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
6856 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
6858 * We zero earlier truncations so they don't erroneously
6861 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
6862 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
6864 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
6865 LIST_EMPTY(&freeblks->fb_jblkdephd))
6866 freeblks->fb_state |= INPROGRESS;
6871 handle_workitem_freeblocks(freeblks, 0);
6872 trunc_pages(ip, length, extblocks, flags);
6877 * Flush a JOP_SYNC to the journal.
6880 softdep_journal_fsync(ip)
6883 struct jfsync *jfsync;
6884 struct ufsmount *ump;
6887 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6888 ("softdep_journal_fsync called on non-softdep filesystem"));
6889 if ((ip->i_flag & IN_TRUNCATED) == 0)
6891 ip->i_flag &= ~IN_TRUNCATED;
6892 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
6893 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
6894 jfsync->jfs_size = ip->i_size;
6895 jfsync->jfs_ino = ip->i_number;
6897 add_to_journal(&jfsync->jfs_list);
6898 jwait(&jfsync->jfs_list, MNT_WAIT);
6903 * Block de-allocation dependencies.
6905 * When blocks are de-allocated, the on-disk pointers must be nullified before
6906 * the blocks are made available for use by other files. (The true
6907 * requirement is that old pointers must be nullified before new on-disk
6908 * pointers are set. We chose this slightly more stringent requirement to
6909 * reduce complexity.) Our implementation handles this dependency by updating
6910 * the inode (or indirect block) appropriately but delaying the actual block
6911 * de-allocation (i.e., freemap and free space count manipulation) until
6912 * after the updated versions reach stable storage. After the disk is
6913 * updated, the blocks can be safely de-allocated whenever it is convenient.
6914 * This implementation handles only the common case of reducing a file's
6915 * length to zero. Other cases are handled by the conventional synchronous
6918 * The ffs implementation with which we worked double-checks
6919 * the state of the block pointers and file size as it reduces
6920 * a file's length. Some of this code is replicated here in our
6921 * soft updates implementation. The freeblks->fb_chkcnt field is
6922 * used to transfer a part of this information to the procedure
6923 * that eventually de-allocates the blocks.
6925 * This routine should be called from the routine that shortens
6926 * a file's length, before the inode's size or block pointers
6927 * are modified. It will save the block pointer information for
6928 * later release and zero the inode so that the calling routine
6932 softdep_setup_freeblocks(ip, length, flags)
6933 struct inode *ip; /* The inode whose length is to be reduced */
6934 off_t length; /* The new length for the file */
6935 int flags; /* IO_EXT and/or IO_NORMAL */
6937 struct ufs1_dinode *dp1;
6938 struct ufs2_dinode *dp2;
6939 struct freeblks *freeblks;
6940 struct inodedep *inodedep;
6941 struct allocdirect *adp;
6942 struct ufsmount *ump;
6945 ufs2_daddr_t extblocks, datablocks;
6947 int i, delay, error;
6953 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6954 ("softdep_setup_freeblocks called on non-softdep filesystem"));
6955 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
6956 ip->i_number, length);
6957 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
6959 if ((error = bread(ump->um_devvp,
6960 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6961 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
6962 if (!ffs_fsfail_cleanup(ump, error))
6963 softdep_error("softdep_setup_freeblocks", error);
6966 freeblks = newfreeblks(mp, ip);
6969 if (fs->fs_magic == FS_UFS2_MAGIC)
6970 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6971 if ((flags & IO_NORMAL) != 0) {
6972 for (i = 0; i < UFS_NDADDR; i++)
6973 setup_freedirect(freeblks, ip, i, 0);
6974 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6976 i++, lbn += tmpval, tmpval *= NINDIR(fs))
6977 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
6979 DIP_SET(ip, i_size, 0);
6980 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6981 datablocks = DIP(ip, i_blocks) - extblocks;
6983 if ((flags & IO_EXT) != 0) {
6984 for (i = 0; i < UFS_NXADDR; i++)
6985 setup_freeext(freeblks, ip, i, 0);
6986 ip->i_din2->di_extsize = 0;
6987 datablocks += extblocks;
6988 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
6991 /* Reference the quotas in case the block count is wrong in the end. */
6992 quotaref(ITOV(ip), freeblks->fb_quota);
6993 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
6995 freeblks->fb_chkcnt = -datablocks;
6997 fs->fs_pendingblocks += datablocks;
6999 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
7001 * Push the zero'ed inode to its disk buffer so that we are free
7002 * to delete its dependencies below. Once the dependencies are gone
7003 * the buffer can be safely released.
7005 if (ump->um_fstype == UFS1) {
7006 dp1 = ((struct ufs1_dinode *)bp->b_data +
7007 ino_to_fsbo(fs, ip->i_number));
7008 ip->i_din1->di_freelink = dp1->di_freelink;
7011 dp2 = ((struct ufs2_dinode *)bp->b_data +
7012 ino_to_fsbo(fs, ip->i_number));
7013 ip->i_din2->di_freelink = dp2->di_freelink;
7014 ffs_update_dinode_ckhash(fs, ip->i_din2);
7018 * Find and eliminate any inode dependencies.
7021 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7022 if ((inodedep->id_state & IOSTARTED) != 0)
7023 panic("softdep_setup_freeblocks: inode busy");
7025 * Add the freeblks structure to the list of operations that
7026 * must await the zero'ed inode being written to disk. If we
7027 * still have a bitmap dependency (delay == 0), then the inode
7028 * has never been written to disk, so we can process the
7029 * freeblks below once we have deleted the dependencies.
7031 delay = (inodedep->id_state & DEPCOMPLETE);
7033 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7035 freeblks->fb_state |= COMPLETE;
7037 * Because the file length has been truncated to zero, any
7038 * pending block allocation dependency structures associated
7039 * with this inode are obsolete and can simply be de-allocated.
7040 * We must first merge the two dependency lists to get rid of
7041 * any duplicate freefrag structures, then purge the merged list.
7042 * If we still have a bitmap dependency, then the inode has never
7043 * been written to disk, so we can free any fragments without delay.
7045 if (flags & IO_NORMAL) {
7046 merge_inode_lists(&inodedep->id_newinoupdt,
7047 &inodedep->id_inoupdt);
7048 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7049 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7052 if (flags & IO_EXT) {
7053 merge_inode_lists(&inodedep->id_newextupdt,
7054 &inodedep->id_extupdt);
7055 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7056 cancel_allocdirect(&inodedep->id_extupdt, adp,
7061 trunc_dependencies(ip, freeblks, -1, 0, flags);
7063 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7064 (void) free_inodedep(inodedep);
7065 freeblks->fb_state |= DEPCOMPLETE;
7067 * If the inode with zeroed block pointers is now on disk
7068 * we can start freeing blocks.
7070 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7071 freeblks->fb_state |= INPROGRESS;
7076 handle_workitem_freeblocks(freeblks, 0);
7077 trunc_pages(ip, length, extblocks, flags);
7081 * Eliminate pages from the page cache that back parts of this inode and
7082 * adjust the vnode pager's idea of our size. This prevents stale data
7083 * from hanging around in the page cache.
7086 trunc_pages(ip, length, extblocks, flags)
7089 ufs2_daddr_t extblocks;
7099 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7100 if ((flags & IO_EXT) != 0)
7101 vn_pages_remove(vp, extend, 0);
7102 if ((flags & IO_NORMAL) == 0)
7104 BO_LOCK(&vp->v_bufobj);
7106 BO_UNLOCK(&vp->v_bufobj);
7108 * The vnode pager eliminates file pages we eliminate indirects
7111 vnode_pager_setsize(vp, length);
7113 * Calculate the end based on the last indirect we want to keep. If
7114 * the block extends into indirects we can just use the negative of
7115 * its lbn. Doubles and triples exist at lower numbers so we must
7116 * be careful not to remove those, if they exist. double and triple
7117 * indirect lbns do not overlap with others so it is not important
7118 * to verify how many levels are required.
7120 lbn = lblkno(fs, length);
7121 if (lbn >= UFS_NDADDR) {
7122 /* Calculate the virtual lbn of the triple indirect. */
7123 lbn = -lbn - (UFS_NIADDR - 1);
7124 end = OFF_TO_IDX(lblktosize(fs, lbn));
7127 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7131 * See if the buf bp is in the range eliminated by truncation.
7134 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7144 /* Only match ext/normal blocks as appropriate. */
7145 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7146 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7148 /* ALTDATA is always a full truncation. */
7149 if ((bp->b_xflags & BX_ALTDATA) != 0)
7151 /* -1 is full truncation. */
7155 * If this is a partial truncate we only want those
7156 * blocks and indirect blocks that cover the range
7161 lbn = -(lbn + lbn_level(lbn));
7164 /* Here we only truncate lblkno if it's partial. */
7165 if (lbn == lastlbn) {
7174 * Eliminate any dependencies that exist in memory beyond lblkno:off
7177 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7179 struct freeblks *freeblks;
7190 * We must wait for any I/O in progress to finish so that
7191 * all potential buffers on the dirty list will be visible.
7192 * Once they are all there, walk the list and get rid of
7199 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7200 bp->b_vflags &= ~BV_SCANNED;
7202 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7203 if (bp->b_vflags & BV_SCANNED)
7205 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7206 bp->b_vflags |= BV_SCANNED;
7209 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7210 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7213 if (deallocate_dependencies(bp, freeblks, blkoff))
7221 * Now do the work of vtruncbuf while also matching indirect blocks.
7223 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7224 bp->b_vflags &= ~BV_SCANNED;
7226 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7227 if (bp->b_vflags & BV_SCANNED)
7229 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7230 bp->b_vflags |= BV_SCANNED;
7234 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7235 BO_LOCKPTR(bo)) == ENOLCK) {
7239 bp->b_vflags |= BV_SCANNED;
7242 allocbuf(bp, blkoff);
7245 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7256 cancel_pagedep(pagedep, freeblks, blkoff)
7257 struct pagedep *pagedep;
7258 struct freeblks *freeblks;
7261 struct jremref *jremref;
7262 struct jmvref *jmvref;
7263 struct dirrem *dirrem, *tmp;
7267 * Copy any directory remove dependencies to the list
7268 * to be processed after the freeblks proceeds. If
7269 * directory entry never made it to disk they
7270 * can be dumped directly onto the work list.
7272 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7273 /* Skip this directory removal if it is intended to remain. */
7274 if (dirrem->dm_offset < blkoff)
7277 * If there are any dirrems we wait for the journal write
7278 * to complete and then restart the buf scan as the lock
7281 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7282 jwait(&jremref->jr_list, MNT_WAIT);
7285 LIST_REMOVE(dirrem, dm_next);
7286 dirrem->dm_dirinum = pagedep->pd_ino;
7287 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7289 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7290 jwait(&jmvref->jm_list, MNT_WAIT);
7294 * When we're partially truncating a pagedep we just want to flush
7295 * journal entries and return. There can not be any adds in the
7296 * truncated portion of the directory and newblk must remain if
7297 * part of the block remains.
7302 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7303 if (dap->da_offset > blkoff)
7304 panic("cancel_pagedep: diradd %p off %d > %d",
7305 dap, dap->da_offset, blkoff);
7306 for (i = 0; i < DAHASHSZ; i++)
7307 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7308 if (dap->da_offset > blkoff)
7309 panic("cancel_pagedep: diradd %p off %d > %d",
7310 dap, dap->da_offset, blkoff);
7314 * There should be no directory add dependencies present
7315 * as the directory could not be truncated until all
7316 * children were removed.
7318 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7319 ("deallocate_dependencies: pendinghd != NULL"));
7320 for (i = 0; i < DAHASHSZ; i++)
7321 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7322 ("deallocate_dependencies: diraddhd != NULL"));
7323 if ((pagedep->pd_state & NEWBLOCK) != 0)
7324 free_newdirblk(pagedep->pd_newdirblk);
7325 if (free_pagedep(pagedep) == 0)
7326 panic("Failed to free pagedep %p", pagedep);
7331 * Reclaim any dependency structures from a buffer that is about to
7332 * be reallocated to a new vnode. The buffer must be locked, thus,
7333 * no I/O completion operations can occur while we are manipulating
7334 * its associated dependencies. The mutex is held so that other I/O's
7335 * associated with related dependencies do not occur.
7338 deallocate_dependencies(bp, freeblks, off)
7340 struct freeblks *freeblks;
7343 struct indirdep *indirdep;
7344 struct pagedep *pagedep;
7345 struct worklist *wk, *wkn;
7346 struct ufsmount *ump;
7348 ump = softdep_bp_to_mp(bp);
7352 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7353 switch (wk->wk_type) {
7355 indirdep = WK_INDIRDEP(wk);
7356 if (bp->b_lblkno >= 0 ||
7357 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7358 panic("deallocate_dependencies: not indir");
7359 cancel_indirdep(indirdep, bp, freeblks);
7363 pagedep = WK_PAGEDEP(wk);
7364 if (cancel_pagedep(pagedep, freeblks, off)) {
7372 * Simply remove the allocindir, we'll find it via
7373 * the indirdep where we can clear pointers if
7376 WORKLIST_REMOVE(wk);
7381 * A truncation is waiting for the zero'd pointers
7382 * to be written. It can be freed when the freeblks
7385 WORKLIST_REMOVE(wk);
7386 wk->wk_state |= ONDEPLIST;
7387 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7395 panic("deallocate_dependencies: Unexpected type %s",
7396 TYPENAME(wk->wk_type));
7403 * Don't throw away this buf, we were partially truncating and
7404 * some deps may always remain.
7408 bp->b_vflags |= BV_SCANNED;
7411 bp->b_flags |= B_INVAL | B_NOCACHE;
7417 * An allocdirect is being canceled due to a truncate. We must make sure
7418 * the journal entry is released in concert with the blkfree that releases
7419 * the storage. Completed journal entries must not be released until the
7420 * space is no longer pointed to by the inode or in the bitmap.
7423 cancel_allocdirect(adphead, adp, freeblks)
7424 struct allocdirectlst *adphead;
7425 struct allocdirect *adp;
7426 struct freeblks *freeblks;
7428 struct freework *freework;
7429 struct newblk *newblk;
7430 struct worklist *wk;
7432 TAILQ_REMOVE(adphead, adp, ad_next);
7433 newblk = (struct newblk *)adp;
7436 * Find the correct freework structure.
7438 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7439 if (wk->wk_type != D_FREEWORK)
7441 freework = WK_FREEWORK(wk);
7442 if (freework->fw_blkno == newblk->nb_newblkno)
7445 if (freework == NULL)
7446 panic("cancel_allocdirect: Freework not found");
7448 * If a newblk exists at all we still have the journal entry that
7449 * initiated the allocation so we do not need to journal the free.
7451 cancel_jfreeblk(freeblks, freework->fw_blkno);
7453 * If the journal hasn't been written the jnewblk must be passed
7454 * to the call to ffs_blkfree that reclaims the space. We accomplish
7455 * this by linking the journal dependency into the freework to be
7456 * freed when freework_freeblock() is called. If the journal has
7457 * been written we can simply reclaim the journal space when the
7458 * freeblks work is complete.
7460 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7461 &freeblks->fb_jwork);
7462 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7466 * Cancel a new block allocation. May be an indirect or direct block. We
7467 * remove it from various lists and return any journal record that needs to
7468 * be resolved by the caller.
7470 * A special consideration is made for indirects which were never pointed
7471 * at on disk and will never be found once this block is released.
7473 static struct jnewblk *
7474 cancel_newblk(newblk, wk, wkhd)
7475 struct newblk *newblk;
7476 struct worklist *wk;
7477 struct workhead *wkhd;
7479 struct jnewblk *jnewblk;
7481 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7483 newblk->nb_state |= GOINGAWAY;
7485 * Previously we traversed the completedhd on each indirdep
7486 * attached to this newblk to cancel them and gather journal
7487 * work. Since we need only the oldest journal segment and
7488 * the lowest point on the tree will always have the oldest
7489 * journal segment we are free to release the segments
7490 * of any subordinates and may leave the indirdep list to
7491 * indirdep_complete() when this newblk is freed.
7493 if (newblk->nb_state & ONDEPLIST) {
7494 newblk->nb_state &= ~ONDEPLIST;
7495 LIST_REMOVE(newblk, nb_deps);
7497 if (newblk->nb_state & ONWORKLIST)
7498 WORKLIST_REMOVE(&newblk->nb_list);
7500 * If the journal entry hasn't been written we save a pointer to
7501 * the dependency that frees it until it is written or the
7502 * superseding operation completes.
7504 jnewblk = newblk->nb_jnewblk;
7505 if (jnewblk != NULL && wk != NULL) {
7506 newblk->nb_jnewblk = NULL;
7507 jnewblk->jn_dep = wk;
7509 if (!LIST_EMPTY(&newblk->nb_jwork))
7510 jwork_move(wkhd, &newblk->nb_jwork);
7512 * When truncating we must free the newdirblk early to remove
7513 * the pagedep from the hash before returning.
7515 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7516 free_newdirblk(WK_NEWDIRBLK(wk));
7517 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7518 panic("cancel_newblk: extra newdirblk");
7524 * Schedule the freefrag associated with a newblk to be released once
7525 * the pointers are written and the previous block is no longer needed.
7528 newblk_freefrag(newblk)
7529 struct newblk *newblk;
7531 struct freefrag *freefrag;
7533 if (newblk->nb_freefrag == NULL)
7535 freefrag = newblk->nb_freefrag;
7536 newblk->nb_freefrag = NULL;
7537 freefrag->ff_state |= COMPLETE;
7538 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7539 add_to_worklist(&freefrag->ff_list, 0);
7543 * Free a newblk. Generate a new freefrag work request if appropriate.
7544 * This must be called after the inode pointer and any direct block pointers
7545 * are valid or fully removed via truncate or frag extension.
7549 struct newblk *newblk;
7551 struct indirdep *indirdep;
7552 struct worklist *wk;
7554 KASSERT(newblk->nb_jnewblk == NULL,
7555 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7556 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7557 ("free_newblk: unclaimed newblk"));
7558 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7559 newblk_freefrag(newblk);
7560 if (newblk->nb_state & ONDEPLIST)
7561 LIST_REMOVE(newblk, nb_deps);
7562 if (newblk->nb_state & ONWORKLIST)
7563 WORKLIST_REMOVE(&newblk->nb_list);
7564 LIST_REMOVE(newblk, nb_hash);
7565 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7566 free_newdirblk(WK_NEWDIRBLK(wk));
7567 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7568 panic("free_newblk: extra newdirblk");
7569 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7570 indirdep_complete(indirdep);
7571 handle_jwork(&newblk->nb_jwork);
7572 WORKITEM_FREE(newblk, D_NEWBLK);
7576 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7579 free_newdirblk(newdirblk)
7580 struct newdirblk *newdirblk;
7582 struct pagedep *pagedep;
7584 struct worklist *wk;
7586 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7587 WORKLIST_REMOVE(&newdirblk->db_list);
7589 * If the pagedep is still linked onto the directory buffer
7590 * dependency chain, then some of the entries on the
7591 * pd_pendinghd list may not be committed to disk yet. In
7592 * this case, we will simply clear the NEWBLOCK flag and
7593 * let the pd_pendinghd list be processed when the pagedep
7594 * is next written. If the pagedep is no longer on the buffer
7595 * dependency chain, then all the entries on the pd_pending
7596 * list are committed to disk and we can free them here.
7598 pagedep = newdirblk->db_pagedep;
7599 pagedep->pd_state &= ~NEWBLOCK;
7600 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7601 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7602 free_diradd(dap, NULL);
7604 * If no dependencies remain, the pagedep will be freed.
7606 free_pagedep(pagedep);
7608 /* Should only ever be one item in the list. */
7609 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7610 WORKLIST_REMOVE(wk);
7611 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7613 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7617 * Prepare an inode to be freed. The actual free operation is not
7618 * done until the zero'ed inode has been written to disk.
7621 softdep_freefile(pvp, ino, mode)
7626 struct inode *ip = VTOI(pvp);
7627 struct inodedep *inodedep;
7628 struct freefile *freefile;
7629 struct freeblks *freeblks;
7630 struct ufsmount *ump;
7633 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7634 ("softdep_freefile called on non-softdep filesystem"));
7636 * This sets up the inode de-allocation dependency.
7638 freefile = malloc(sizeof(struct freefile),
7639 M_FREEFILE, M_SOFTDEP_FLAGS);
7640 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7641 freefile->fx_mode = mode;
7642 freefile->fx_oldinum = ino;
7643 freefile->fx_devvp = ump->um_devvp;
7644 LIST_INIT(&freefile->fx_jwork);
7646 ump->um_fs->fs_pendinginodes += 1;
7650 * If the inodedep does not exist, then the zero'ed inode has
7651 * been written to disk. If the allocated inode has never been
7652 * written to disk, then the on-disk inode is zero'ed. In either
7653 * case we can free the file immediately. If the journal was
7654 * canceled before being written the inode will never make it to
7655 * disk and we must send the canceled journal entrys to
7656 * ffs_freefile() to be cleared in conjunction with the bitmap.
7657 * Any blocks waiting on the inode to write can be safely freed
7658 * here as it will never been written.
7661 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7664 * Clear out freeblks that no longer need to reference
7668 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7669 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7671 freeblks->fb_state &= ~ONDEPLIST;
7674 * Remove this inode from the unlinked list.
7676 if (inodedep->id_state & UNLINKED) {
7678 * Save the journal work to be freed with the bitmap
7679 * before we clear UNLINKED. Otherwise it can be lost
7680 * if the inode block is written.
7682 handle_bufwait(inodedep, &freefile->fx_jwork);
7683 clear_unlinked_inodedep(inodedep);
7685 * Re-acquire inodedep as we've dropped the
7686 * per-filesystem lock in clear_unlinked_inodedep().
7688 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7691 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7693 handle_workitem_freefile(freefile);
7696 if ((inodedep->id_state & DEPCOMPLETE) == 0)
7697 inodedep->id_state |= GOINGAWAY;
7698 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7700 if (ip->i_number == ino)
7701 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
7705 * Check to see if an inode has never been written to disk. If
7706 * so free the inodedep and return success, otherwise return failure.
7708 * If we still have a bitmap dependency, then the inode has never
7709 * been written to disk. Drop the dependency as it is no longer
7710 * necessary since the inode is being deallocated. We set the
7711 * ALLCOMPLETE flags since the bitmap now properly shows that the
7712 * inode is not allocated. Even if the inode is actively being
7713 * written, it has been rolled back to its zero'ed state, so we
7714 * are ensured that a zero inode is what is on the disk. For short
7715 * lived files, this change will usually result in removing all the
7716 * dependencies from the inode so that it can be freed immediately.
7719 check_inode_unwritten(inodedep)
7720 struct inodedep *inodedep;
7723 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7725 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
7726 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7727 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7728 !LIST_EMPTY(&inodedep->id_bufwait) ||
7729 !LIST_EMPTY(&inodedep->id_inowait) ||
7730 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7731 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7732 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7733 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7734 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7735 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7736 inodedep->id_mkdiradd != NULL ||
7737 inodedep->id_nlinkdelta != 0)
7740 * Another process might be in initiate_write_inodeblock_ufs[12]
7741 * trying to allocate memory without holding "Softdep Lock".
7743 if ((inodedep->id_state & IOSTARTED) != 0 &&
7744 inodedep->id_savedino1 == NULL)
7747 if (inodedep->id_state & ONDEPLIST)
7748 LIST_REMOVE(inodedep, id_deps);
7749 inodedep->id_state &= ~ONDEPLIST;
7750 inodedep->id_state |= ALLCOMPLETE;
7751 inodedep->id_bmsafemap = NULL;
7752 if (inodedep->id_state & ONWORKLIST)
7753 WORKLIST_REMOVE(&inodedep->id_list);
7754 if (inodedep->id_savedino1 != NULL) {
7755 free(inodedep->id_savedino1, M_SAVEDINO);
7756 inodedep->id_savedino1 = NULL;
7758 if (free_inodedep(inodedep) == 0)
7759 panic("check_inode_unwritten: busy inode");
7764 check_inodedep_free(inodedep)
7765 struct inodedep *inodedep;
7768 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7769 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
7770 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7771 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7772 !LIST_EMPTY(&inodedep->id_bufwait) ||
7773 !LIST_EMPTY(&inodedep->id_inowait) ||
7774 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7775 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7776 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7777 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7778 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7779 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7780 inodedep->id_mkdiradd != NULL ||
7781 inodedep->id_nlinkdelta != 0 ||
7782 inodedep->id_savedino1 != NULL)
7788 * Try to free an inodedep structure. Return 1 if it could be freed.
7791 free_inodedep(inodedep)
7792 struct inodedep *inodedep;
7795 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7796 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7797 !check_inodedep_free(inodedep))
7799 if (inodedep->id_state & ONDEPLIST)
7800 LIST_REMOVE(inodedep, id_deps);
7801 LIST_REMOVE(inodedep, id_hash);
7802 WORKITEM_FREE(inodedep, D_INODEDEP);
7807 * Free the block referenced by a freework structure. The parent freeblks
7808 * structure is released and completed when the final cg bitmap reaches
7809 * the disk. This routine may be freeing a jnewblk which never made it to
7810 * disk in which case we do not have to wait as the operation is undone
7811 * in memory immediately.
7814 freework_freeblock(freework, key)
7815 struct freework *freework;
7818 struct freeblks *freeblks;
7819 struct jnewblk *jnewblk;
7820 struct ufsmount *ump;
7821 struct workhead wkhd;
7826 ump = VFSTOUFS(freework->fw_list.wk_mp);
7829 * Handle partial truncate separately.
7831 if (freework->fw_indir) {
7832 complete_trunc_indir(freework);
7835 freeblks = freework->fw_freeblks;
7837 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7838 bsize = lfragtosize(fs, freework->fw_frags);
7841 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7842 * on the indirblk hashtable and prevents premature freeing.
7844 freework->fw_state |= DEPCOMPLETE;
7846 * SUJ needs to wait for the segment referencing freed indirect
7847 * blocks to expire so that we know the checker will not confuse
7848 * a re-allocated indirect block with its old contents.
7850 if (needj && freework->fw_lbn <= -UFS_NDADDR)
7851 indirblk_insert(freework);
7853 * If we are canceling an existing jnewblk pass it to the free
7854 * routine, otherwise pass the freeblk which will ultimately
7855 * release the freeblks. If we're not journaling, we can just
7856 * free the freeblks immediately.
7858 jnewblk = freework->fw_jnewblk;
7859 if (jnewblk != NULL) {
7860 cancel_jnewblk(jnewblk, &wkhd);
7863 freework->fw_state |= DELAYEDFREE;
7864 freeblks->fb_cgwait++;
7865 WORKLIST_INSERT(&wkhd, &freework->fw_list);
7868 freeblks_free(ump, freeblks, btodb(bsize));
7870 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
7871 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
7872 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
7873 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
7876 * The jnewblk will be discarded and the bits in the map never
7877 * made it to disk. We can immediately free the freeblk.
7880 handle_written_freework(freework);
7884 * We enqueue freework items that need processing back on the freeblks and
7885 * add the freeblks to the worklist. This makes it easier to find all work
7886 * required to flush a truncation in process_truncates().
7889 freework_enqueue(freework)
7890 struct freework *freework;
7892 struct freeblks *freeblks;
7894 freeblks = freework->fw_freeblks;
7895 if ((freework->fw_state & INPROGRESS) == 0)
7896 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
7897 if ((freeblks->fb_state &
7898 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
7899 LIST_EMPTY(&freeblks->fb_jblkdephd))
7900 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7904 * Start, continue, or finish the process of freeing an indirect block tree.
7905 * The free operation may be paused at any point with fw_off containing the
7906 * offset to restart from. This enables us to implement some flow control
7907 * for large truncates which may fan out and generate a huge number of
7911 handle_workitem_indirblk(freework)
7912 struct freework *freework;
7914 struct freeblks *freeblks;
7915 struct ufsmount *ump;
7918 freeblks = freework->fw_freeblks;
7919 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7921 if (freework->fw_state & DEPCOMPLETE) {
7922 handle_written_freework(freework);
7925 if (freework->fw_off == NINDIR(fs)) {
7926 freework_freeblock(freework, SINGLETON_KEY);
7929 freework->fw_state |= INPROGRESS;
7931 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
7937 * Called when a freework structure attached to a cg buf is written. The
7938 * ref on either the parent or the freeblks structure is released and
7939 * the freeblks is added back to the worklist if there is more work to do.
7942 handle_written_freework(freework)
7943 struct freework *freework;
7945 struct freeblks *freeblks;
7946 struct freework *parent;
7948 freeblks = freework->fw_freeblks;
7949 parent = freework->fw_parent;
7950 if (freework->fw_state & DELAYEDFREE)
7951 freeblks->fb_cgwait--;
7952 freework->fw_state |= COMPLETE;
7953 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
7954 WORKITEM_FREE(freework, D_FREEWORK);
7956 if (--parent->fw_ref == 0)
7957 freework_enqueue(parent);
7960 if (--freeblks->fb_ref != 0)
7962 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
7963 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
7964 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7968 * This workitem routine performs the block de-allocation.
7969 * The workitem is added to the pending list after the updated
7970 * inode block has been written to disk. As mentioned above,
7971 * checks regarding the number of blocks de-allocated (compared
7972 * to the number of blocks allocated for the file) are also
7973 * performed in this function.
7976 handle_workitem_freeblocks(freeblks, flags)
7977 struct freeblks *freeblks;
7980 struct freework *freework;
7981 struct newblk *newblk;
7982 struct allocindir *aip;
7983 struct ufsmount *ump;
7984 struct worklist *wk;
7987 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
7988 ("handle_workitem_freeblocks: Journal entries not written."));
7989 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7990 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
7992 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
7993 WORKLIST_REMOVE(wk);
7994 switch (wk->wk_type) {
7996 wk->wk_state |= COMPLETE;
7997 add_to_worklist(wk, 0);
8001 free_newblk(WK_NEWBLK(wk));
8005 aip = WK_ALLOCINDIR(wk);
8007 if (aip->ai_state & DELAYEDFREE) {
8009 freework = newfreework(ump, freeblks, NULL,
8010 aip->ai_lbn, aip->ai_newblkno,
8011 ump->um_fs->fs_frag, 0, 0);
8014 newblk = WK_NEWBLK(wk);
8015 if (newblk->nb_jnewblk) {
8016 freework->fw_jnewblk = newblk->nb_jnewblk;
8017 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
8018 newblk->nb_jnewblk = NULL;
8020 free_newblk(newblk);
8024 freework = WK_FREEWORK(wk);
8025 if (freework->fw_lbn <= -UFS_NDADDR)
8026 handle_workitem_indirblk(freework);
8028 freework_freeblock(freework, key);
8031 panic("handle_workitem_freeblocks: Unknown type %s",
8032 TYPENAME(wk->wk_type));
8035 if (freeblks->fb_ref != 0) {
8036 freeblks->fb_state &= ~INPROGRESS;
8037 wake_worklist(&freeblks->fb_list);
8041 ffs_blkrelease_finish(ump, key);
8043 return handle_complete_freeblocks(freeblks, flags);
8048 * Handle completion of block free via truncate. This allows fs_pending
8049 * to track the actual free block count more closely than if we only updated
8050 * it at the end. We must be careful to handle cases where the block count
8051 * on free was incorrect.
8054 freeblks_free(ump, freeblks, blocks)
8055 struct ufsmount *ump;
8056 struct freeblks *freeblks;
8060 ufs2_daddr_t remain;
8063 remain = -freeblks->fb_chkcnt;
8064 freeblks->fb_chkcnt += blocks;
8066 if (remain < blocks)
8069 fs->fs_pendingblocks -= blocks;
8075 * Once all of the freework workitems are complete we can retire the
8076 * freeblocks dependency and any journal work awaiting completion. This
8077 * can not be called until all other dependencies are stable on disk.
8080 handle_complete_freeblocks(freeblks, flags)
8081 struct freeblks *freeblks;
8084 struct inodedep *inodedep;
8088 struct ufsmount *ump;
8091 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8093 flags = LK_EXCLUSIVE | flags;
8094 spare = freeblks->fb_chkcnt;
8097 * If we did not release the expected number of blocks we may have
8098 * to adjust the inode block count here. Only do so if it wasn't
8099 * a truncation to zero and the modrev still matches.
8101 if (spare && freeblks->fb_len != 0) {
8102 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8103 flags, &vp, FFSV_FORCEINSMQ) != 0)
8106 if (ip->i_mode == 0) {
8108 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8109 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8110 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8112 * We must wait so this happens before the
8113 * journal is reclaimed.
8121 fs->fs_pendingblocks += spare;
8127 quotaadj(freeblks->fb_quota, ump, -spare);
8128 quotarele(freeblks->fb_quota);
8131 if (freeblks->fb_state & ONDEPLIST) {
8132 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8134 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8135 freeblks->fb_state &= ~ONDEPLIST;
8136 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8137 free_inodedep(inodedep);
8140 * All of the freeblock deps must be complete prior to this call
8141 * so it's now safe to complete earlier outstanding journal entries.
8143 handle_jwork(&freeblks->fb_jwork);
8144 WORKITEM_FREE(freeblks, D_FREEBLKS);
8150 * Release blocks associated with the freeblks and stored in the indirect
8151 * block dbn. If level is greater than SINGLE, the block is an indirect block
8152 * and recursive calls to indirtrunc must be used to cleanse other indirect
8155 * This handles partial and complete truncation of blocks. Partial is noted
8156 * with goingaway == 0. In this case the freework is completed after the
8157 * zero'd indirects are written to disk. For full truncation the freework
8158 * is completed after the block is freed.
8161 indir_trunc(freework, dbn, lbn)
8162 struct freework *freework;
8166 struct freework *nfreework;
8167 struct workhead wkhd;
8168 struct freeblks *freeblks;
8171 struct indirdep *indirdep;
8173 struct ufsmount *ump;
8175 ufs2_daddr_t nb, nnb, *bap2;
8176 ufs_lbn_t lbnadd, nlbn;
8178 int nblocks, ufs1fmt, freedblocks;
8179 int goingaway, freedeps, needj, level, cnt, i, error;
8181 freeblks = freework->fw_freeblks;
8182 mp = freeblks->fb_list.wk_mp;
8186 * Get buffer of block pointers to be freed. There are three cases:
8188 * 1) Partial truncate caches the indirdep pointer in the freework
8189 * which provides us a back copy to the save bp which holds the
8190 * pointers we want to clear. When this completes the zero
8191 * pointers are written to the real copy.
8192 * 2) The indirect is being completely truncated, cancel_indirdep()
8193 * eliminated the real copy and placed the indirdep on the saved
8194 * copy. The indirdep and buf are discarded when this completes.
8195 * 3) The indirect was not in memory, we read a copy off of the disk
8196 * using the devvp and drop and invalidate the buffer when we're
8201 if (freework->fw_indir != NULL) {
8203 indirdep = freework->fw_indir;
8204 bp = indirdep->ir_savebp;
8205 if (bp == NULL || bp->b_blkno != dbn)
8206 panic("indir_trunc: Bad saved buf %p blkno %jd",
8208 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8210 * The lock prevents the buf dep list from changing and
8211 * indirects on devvp should only ever have one dependency.
8213 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8214 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8215 panic("indir_trunc: Bad indirdep %p from buf %p",
8218 error = ffs_breadz(ump, freeblks->fb_devvp, dbn, dbn,
8219 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
8224 /* Protects against a race with complete_trunc_indir(). */
8225 freework->fw_state &= ~INPROGRESS;
8227 * If we have an indirdep we need to enforce the truncation order
8228 * and discard it when it is complete.
8231 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8232 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8234 * Add the complete truncate to the list on the
8235 * indirdep to enforce in-order processing.
8237 if (freework->fw_indir == NULL)
8238 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8244 * If we're goingaway, free the indirdep. Otherwise it will
8245 * linger until the write completes.
8248 KASSERT(indirdep->ir_savebp == bp,
8249 ("indir_trunc: losing ir_savebp %p",
8250 indirdep->ir_savebp));
8251 indirdep->ir_savebp = NULL;
8252 free_indirdep(indirdep);
8256 /* Initialize pointers depending on block size. */
8257 if (ump->um_fstype == UFS1) {
8258 bap1 = (ufs1_daddr_t *)bp->b_data;
8259 nb = bap1[freework->fw_off];
8263 bap2 = (ufs2_daddr_t *)bp->b_data;
8264 nb = bap2[freework->fw_off];
8268 level = lbn_level(lbn);
8269 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8270 lbnadd = lbn_offset(fs, level);
8271 nblocks = btodb(fs->fs_bsize);
8272 nfreework = freework;
8276 * Reclaim blocks. Traverses into nested indirect levels and
8277 * arranges for the current level to be freed when subordinates
8278 * are free when journaling.
8280 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8281 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8282 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8285 if (i != NINDIR(fs) - 1) {
8296 nlbn = (lbn + 1) - (i * lbnadd);
8298 nfreework = newfreework(ump, freeblks, freework,
8299 nlbn, nb, fs->fs_frag, 0, 0);
8302 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8304 struct freedep *freedep;
8307 * Attempt to aggregate freedep dependencies for
8308 * all blocks being released to the same CG.
8312 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8313 freedep = newfreedep(freework);
8314 WORKLIST_INSERT_UNLOCKED(&wkhd,
8319 "indir_trunc: ino %jd blkno %jd size %d",
8320 freeblks->fb_inum, nb, fs->fs_bsize);
8321 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8322 fs->fs_bsize, freeblks->fb_inum,
8323 freeblks->fb_vtype, &wkhd, key);
8326 ffs_blkrelease_finish(ump, key);
8328 bp->b_flags |= B_INVAL | B_NOCACHE;
8333 freedblocks = (nblocks * cnt);
8335 freedblocks += nblocks;
8336 freeblks_free(ump, freeblks, freedblocks);
8338 * If we are journaling set up the ref counts and offset so this
8339 * indirect can be completed when its children are free.
8343 freework->fw_off = i;
8344 freework->fw_ref += freedeps;
8345 freework->fw_ref -= NINDIR(fs) + 1;
8347 freeblks->fb_cgwait += freedeps;
8348 if (freework->fw_ref == 0)
8349 freework_freeblock(freework, SINGLETON_KEY);
8354 * If we're not journaling we can free the indirect now.
8356 dbn = dbtofsb(fs, dbn);
8358 "indir_trunc 2: ino %jd blkno %jd size %d",
8359 freeblks->fb_inum, dbn, fs->fs_bsize);
8360 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8361 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8362 /* Non SUJ softdep does single-threaded truncations. */
8363 if (freework->fw_blkno == dbn) {
8364 freework->fw_state |= ALLCOMPLETE;
8366 handle_written_freework(freework);
8373 * Cancel an allocindir when it is removed via truncation. When bp is not
8374 * NULL the indirect never appeared on disk and is scheduled to be freed
8375 * independently of the indir so we can more easily track journal work.
8378 cancel_allocindir(aip, bp, freeblks, trunc)
8379 struct allocindir *aip;
8381 struct freeblks *freeblks;
8384 struct indirdep *indirdep;
8385 struct freefrag *freefrag;
8386 struct newblk *newblk;
8388 newblk = (struct newblk *)aip;
8389 LIST_REMOVE(aip, ai_next);
8391 * We must eliminate the pointer in bp if it must be freed on its
8392 * own due to partial truncate or pending journal work.
8394 if (bp && (trunc || newblk->nb_jnewblk)) {
8396 * Clear the pointer and mark the aip to be freed
8397 * directly if it never existed on disk.
8399 aip->ai_state |= DELAYEDFREE;
8400 indirdep = aip->ai_indirdep;
8401 if (indirdep->ir_state & UFS1FMT)
8402 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8404 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8407 * When truncating the previous pointer will be freed via
8408 * savedbp. Eliminate the freefrag which would dup free.
8410 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8411 newblk->nb_freefrag = NULL;
8412 if (freefrag->ff_jdep)
8414 WK_JFREEFRAG(freefrag->ff_jdep));
8415 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8416 WORKITEM_FREE(freefrag, D_FREEFRAG);
8419 * If the journal hasn't been written the jnewblk must be passed
8420 * to the call to ffs_blkfree that reclaims the space. We accomplish
8421 * this by leaving the journal dependency on the newblk to be freed
8422 * when a freework is created in handle_workitem_freeblocks().
8424 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8425 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8429 * Create the mkdir dependencies for . and .. in a new directory. Link them
8430 * in to a newdirblk so any subsequent additions are tracked properly. The
8431 * caller is responsible for adding the mkdir1 dependency to the journal
8432 * and updating id_mkdiradd. This function returns with the per-filesystem
8435 static struct mkdir *
8436 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8440 struct buf *newdirbp;
8441 struct mkdir **mkdirp;
8443 struct newblk *newblk;
8444 struct pagedep *pagedep;
8445 struct inodedep *inodedep;
8446 struct newdirblk *newdirblk;
8447 struct mkdir *mkdir1, *mkdir2;
8448 struct worklist *wk;
8449 struct jaddref *jaddref;
8450 struct ufsmount *ump;
8453 mp = dap->da_list.wk_mp;
8455 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8457 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8458 LIST_INIT(&newdirblk->db_mkdir);
8459 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8460 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8461 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8462 mkdir1->md_diradd = dap;
8463 mkdir1->md_jaddref = NULL;
8464 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8465 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8466 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8467 mkdir2->md_diradd = dap;
8468 mkdir2->md_jaddref = NULL;
8469 if (MOUNTEDSUJ(mp) == 0) {
8470 mkdir1->md_state |= DEPCOMPLETE;
8471 mkdir2->md_state |= DEPCOMPLETE;
8474 * Dependency on "." and ".." being written to disk.
8476 mkdir1->md_buf = newdirbp;
8477 ACQUIRE_LOCK(VFSTOUFS(mp));
8478 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8480 * We must link the pagedep, allocdirect, and newdirblk for
8481 * the initial file page so the pointer to the new directory
8482 * is not written until the directory contents are live and
8483 * any subsequent additions are not marked live until the
8484 * block is reachable via the inode.
8486 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8487 panic("setup_newdir: lost pagedep");
8488 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8489 if (wk->wk_type == D_ALLOCDIRECT)
8492 panic("setup_newdir: lost allocdirect");
8493 if (pagedep->pd_state & NEWBLOCK)
8494 panic("setup_newdir: NEWBLOCK already set");
8495 newblk = WK_NEWBLK(wk);
8496 pagedep->pd_state |= NEWBLOCK;
8497 pagedep->pd_newdirblk = newdirblk;
8498 newdirblk->db_pagedep = pagedep;
8499 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8500 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8502 * Look up the inodedep for the parent directory so that we
8503 * can link mkdir2 into the pending dotdot jaddref or
8504 * the inode write if there is none. If the inode is
8505 * ALLCOMPLETE and no jaddref is present all dependencies have
8506 * been satisfied and mkdir2 can be freed.
8508 inodedep_lookup(mp, dinum, 0, &inodedep);
8509 if (MOUNTEDSUJ(mp)) {
8510 if (inodedep == NULL)
8511 panic("setup_newdir: Lost parent.");
8512 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8514 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8515 (jaddref->ja_state & MKDIR_PARENT),
8516 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8517 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8518 mkdir2->md_jaddref = jaddref;
8519 jaddref->ja_mkdir = mkdir2;
8520 } else if (inodedep == NULL ||
8521 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8522 dap->da_state &= ~MKDIR_PARENT;
8523 WORKITEM_FREE(mkdir2, D_MKDIR);
8526 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8527 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8535 * Directory entry addition dependencies.
8537 * When adding a new directory entry, the inode (with its incremented link
8538 * count) must be written to disk before the directory entry's pointer to it.
8539 * Also, if the inode is newly allocated, the corresponding freemap must be
8540 * updated (on disk) before the directory entry's pointer. These requirements
8541 * are met via undo/redo on the directory entry's pointer, which consists
8542 * simply of the inode number.
8544 * As directory entries are added and deleted, the free space within a
8545 * directory block can become fragmented. The ufs filesystem will compact
8546 * a fragmented directory block to make space for a new entry. When this
8547 * occurs, the offsets of previously added entries change. Any "diradd"
8548 * dependency structures corresponding to these entries must be updated with
8553 * This routine is called after the in-memory inode's link
8554 * count has been incremented, but before the directory entry's
8555 * pointer to the inode has been set.
8558 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8559 struct buf *bp; /* buffer containing directory block */
8560 struct inode *dp; /* inode for directory */
8561 off_t diroffset; /* offset of new entry in directory */
8562 ino_t newinum; /* inode referenced by new directory entry */
8563 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
8564 int isnewblk; /* entry is in a newly allocated block */
8566 int offset; /* offset of new entry within directory block */
8567 ufs_lbn_t lbn; /* block in directory containing new entry */
8570 struct newblk *newblk;
8571 struct pagedep *pagedep;
8572 struct inodedep *inodedep;
8573 struct newdirblk *newdirblk;
8574 struct mkdir *mkdir1, *mkdir2;
8575 struct jaddref *jaddref;
8576 struct ufsmount *ump;
8582 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8583 ("softdep_setup_directory_add called on non-softdep filesystem"));
8585 * Whiteouts have no dependencies.
8587 if (newinum == UFS_WINO) {
8588 if (newdirbp != NULL)
8593 mkdir1 = mkdir2 = NULL;
8595 lbn = lblkno(fs, diroffset);
8596 offset = blkoff(fs, diroffset);
8597 dap = malloc(sizeof(struct diradd), M_DIRADD,
8598 M_SOFTDEP_FLAGS|M_ZERO);
8599 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8600 dap->da_offset = offset;
8601 dap->da_newinum = newinum;
8602 dap->da_state = ATTACHED;
8603 LIST_INIT(&dap->da_jwork);
8604 isindir = bp->b_lblkno >= UFS_NDADDR;
8607 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8608 newdirblk = malloc(sizeof(struct newdirblk),
8609 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8610 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8611 LIST_INIT(&newdirblk->db_mkdir);
8614 * If we're creating a new directory setup the dependencies and set
8615 * the dap state to wait for them. Otherwise it's COMPLETE and
8618 if (newdirbp == NULL) {
8619 dap->da_state |= DEPCOMPLETE;
8622 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8623 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8627 * Link into parent directory pagedep to await its being written.
8629 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8631 if (diradd_lookup(pagedep, offset) != NULL)
8632 panic("softdep_setup_directory_add: %p already at off %d\n",
8633 diradd_lookup(pagedep, offset), offset);
8635 dap->da_pagedep = pagedep;
8636 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8638 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8640 * If we're journaling, link the diradd into the jaddref so it
8641 * may be completed after the journal entry is written. Otherwise,
8642 * link the diradd into its inodedep. If the inode is not yet
8643 * written place it on the bufwait list, otherwise do the post-inode
8644 * write processing to put it on the id_pendinghd list.
8646 if (MOUNTEDSUJ(mp)) {
8647 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8649 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8650 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8651 jaddref->ja_diroff = diroffset;
8652 jaddref->ja_diradd = dap;
8653 add_to_journal(&jaddref->ja_list);
8654 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8655 diradd_inode_written(dap, inodedep);
8657 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8659 * Add the journal entries for . and .. links now that the primary
8662 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8663 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8664 inoreflst, if_deps);
8665 KASSERT(jaddref != NULL &&
8666 jaddref->ja_ino == jaddref->ja_parent &&
8667 (jaddref->ja_state & MKDIR_BODY),
8668 ("softdep_setup_directory_add: bad dot jaddref %p",
8670 mkdir1->md_jaddref = jaddref;
8671 jaddref->ja_mkdir = mkdir1;
8673 * It is important that the dotdot journal entry
8674 * is added prior to the dot entry since dot writes
8675 * both the dot and dotdot links. These both must
8676 * be added after the primary link for the journal
8677 * to remain consistent.
8679 add_to_journal(&mkdir2->md_jaddref->ja_list);
8680 add_to_journal(&jaddref->ja_list);
8683 * If we are adding a new directory remember this diradd so that if
8684 * we rename it we can keep the dot and dotdot dependencies. If
8685 * we are adding a new name for an inode that has a mkdiradd we
8686 * must be in rename and we have to move the dot and dotdot
8687 * dependencies to this new name. The old name is being orphaned
8690 if (mkdir1 != NULL) {
8691 if (inodedep->id_mkdiradd != NULL)
8692 panic("softdep_setup_directory_add: Existing mkdir");
8693 inodedep->id_mkdiradd = dap;
8694 } else if (inodedep->id_mkdiradd)
8695 merge_diradd(inodedep, dap);
8696 if (newdirblk != NULL) {
8698 * There is nothing to do if we are already tracking
8701 if ((pagedep->pd_state & NEWBLOCK) != 0) {
8702 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8706 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8708 panic("softdep_setup_directory_add: lost entry");
8709 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8710 pagedep->pd_state |= NEWBLOCK;
8711 pagedep->pd_newdirblk = newdirblk;
8712 newdirblk->db_pagedep = pagedep;
8715 * If we extended into an indirect signal direnter to sync.
8726 * This procedure is called to change the offset of a directory
8727 * entry when compacting a directory block which must be owned
8728 * exclusively by the caller. Note that the actual entry movement
8729 * must be done in this procedure to ensure that no I/O completions
8730 * occur while the move is in progress.
8733 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
8734 struct buf *bp; /* Buffer holding directory block. */
8735 struct inode *dp; /* inode for directory */
8736 caddr_t base; /* address of dp->i_offset */
8737 caddr_t oldloc; /* address of old directory location */
8738 caddr_t newloc; /* address of new directory location */
8739 int entrysize; /* size of directory entry */
8741 int offset, oldoffset, newoffset;
8742 struct pagedep *pagedep;
8743 struct jmvref *jmvref;
8747 struct ufsmount *ump;
8753 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8754 ("softdep_change_directoryentry_offset called on "
8755 "non-softdep filesystem"));
8756 de = (struct direct *)oldloc;
8760 * Moves are always journaled as it would be too complex to
8761 * determine if any affected adds or removes are present in the
8764 if (MOUNTEDSUJ(mp)) {
8766 jmvref = newjmvref(dp, de->d_ino,
8767 dp->i_offset + (oldloc - base),
8768 dp->i_offset + (newloc - base));
8770 lbn = lblkno(ump->um_fs, dp->i_offset);
8771 offset = blkoff(ump->um_fs, dp->i_offset);
8772 oldoffset = offset + (oldloc - base);
8773 newoffset = offset + (newloc - base);
8775 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8777 dap = diradd_lookup(pagedep, oldoffset);
8779 dap->da_offset = newoffset;
8780 newoffset = DIRADDHASH(newoffset);
8781 oldoffset = DIRADDHASH(oldoffset);
8782 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8783 newoffset != oldoffset) {
8784 LIST_REMOVE(dap, da_pdlist);
8785 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8791 jmvref->jm_pagedep = pagedep;
8792 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8793 add_to_journal(&jmvref->jm_list);
8795 bcopy(oldloc, newloc, entrysize);
8800 * Move the mkdir dependencies and journal work from one diradd to another
8801 * when renaming a directory. The new name must depend on the mkdir deps
8802 * completing as the old name did. Directories can only have one valid link
8803 * at a time so one must be canonical.
8806 merge_diradd(inodedep, newdap)
8807 struct inodedep *inodedep;
8808 struct diradd *newdap;
8810 struct diradd *olddap;
8811 struct mkdir *mkdir, *nextmd;
8812 struct ufsmount *ump;
8815 olddap = inodedep->id_mkdiradd;
8816 inodedep->id_mkdiradd = newdap;
8817 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8818 newdap->da_state &= ~DEPCOMPLETE;
8819 ump = VFSTOUFS(inodedep->id_list.wk_mp);
8820 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8822 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8823 if (mkdir->md_diradd != olddap)
8825 mkdir->md_diradd = newdap;
8826 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8827 newdap->da_state |= state;
8828 olddap->da_state &= ~state;
8829 if ((olddap->da_state &
8830 (MKDIR_PARENT | MKDIR_BODY)) == 0)
8833 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8834 panic("merge_diradd: unfound ref");
8837 * Any mkdir related journal items are not safe to be freed until
8838 * the new name is stable.
8840 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8841 olddap->da_state |= DEPCOMPLETE;
8842 complete_diradd(olddap);
8846 * Move the diradd to the pending list when all diradd dependencies are
8850 complete_diradd(dap)
8853 struct pagedep *pagedep;
8855 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
8856 if (dap->da_state & DIRCHG)
8857 pagedep = dap->da_previous->dm_pagedep;
8859 pagedep = dap->da_pagedep;
8860 LIST_REMOVE(dap, da_pdlist);
8861 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
8866 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
8867 * add entries and conditonally journal the remove.
8870 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
8872 struct dirrem *dirrem;
8873 struct jremref *jremref;
8874 struct jremref *dotremref;
8875 struct jremref *dotdotremref;
8877 struct inodedep *inodedep;
8878 struct jaddref *jaddref;
8879 struct inoref *inoref;
8880 struct ufsmount *ump;
8881 struct mkdir *mkdir;
8884 * If no remove references were allocated we're on a non-journaled
8885 * filesystem and can skip the cancel step.
8887 if (jremref == NULL) {
8888 free_diradd(dap, NULL);
8892 * Cancel the primary name an free it if it does not require
8895 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
8896 0, &inodedep) != 0) {
8897 /* Abort the addref that reference this diradd. */
8898 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
8899 if (inoref->if_list.wk_type != D_JADDREF)
8901 jaddref = (struct jaddref *)inoref;
8902 if (jaddref->ja_diradd != dap)
8904 if (cancel_jaddref(jaddref, inodedep,
8905 &dirrem->dm_jwork) == 0) {
8906 free_jremref(jremref);
8913 * Cancel subordinate names and free them if they do not require
8916 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8917 ump = VFSTOUFS(dap->da_list.wk_mp);
8918 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
8919 if (mkdir->md_diradd != dap)
8921 if ((jaddref = mkdir->md_jaddref) == NULL)
8923 mkdir->md_jaddref = NULL;
8924 if (mkdir->md_state & MKDIR_PARENT) {
8925 if (cancel_jaddref(jaddref, NULL,
8926 &dirrem->dm_jwork) == 0) {
8927 free_jremref(dotdotremref);
8928 dotdotremref = NULL;
8931 if (cancel_jaddref(jaddref, inodedep,
8932 &dirrem->dm_jwork) == 0) {
8933 free_jremref(dotremref);
8941 journal_jremref(dirrem, jremref, inodedep);
8943 journal_jremref(dirrem, dotremref, inodedep);
8945 journal_jremref(dirrem, dotdotremref, NULL);
8946 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
8947 free_diradd(dap, &dirrem->dm_jwork);
8951 * Free a diradd dependency structure.
8954 free_diradd(dap, wkhd)
8956 struct workhead *wkhd;
8958 struct dirrem *dirrem;
8959 struct pagedep *pagedep;
8960 struct inodedep *inodedep;
8961 struct mkdir *mkdir, *nextmd;
8962 struct ufsmount *ump;
8964 ump = VFSTOUFS(dap->da_list.wk_mp);
8966 LIST_REMOVE(dap, da_pdlist);
8967 if (dap->da_state & ONWORKLIST)
8968 WORKLIST_REMOVE(&dap->da_list);
8969 if ((dap->da_state & DIRCHG) == 0) {
8970 pagedep = dap->da_pagedep;
8972 dirrem = dap->da_previous;
8973 pagedep = dirrem->dm_pagedep;
8974 dirrem->dm_dirinum = pagedep->pd_ino;
8975 dirrem->dm_state |= COMPLETE;
8976 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
8977 add_to_worklist(&dirrem->dm_list, 0);
8979 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
8981 if (inodedep->id_mkdiradd == dap)
8982 inodedep->id_mkdiradd = NULL;
8983 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8984 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8986 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8987 if (mkdir->md_diradd != dap)
8990 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
8991 LIST_REMOVE(mkdir, md_mkdirs);
8992 if (mkdir->md_state & ONWORKLIST)
8993 WORKLIST_REMOVE(&mkdir->md_list);
8994 if (mkdir->md_jaddref != NULL)
8995 panic("free_diradd: Unexpected jaddref");
8996 WORKITEM_FREE(mkdir, D_MKDIR);
8997 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
9000 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
9001 panic("free_diradd: unfound ref");
9004 free_inodedep(inodedep);
9006 * Free any journal segments waiting for the directory write.
9008 handle_jwork(&dap->da_jwork);
9009 WORKITEM_FREE(dap, D_DIRADD);
9013 * Directory entry removal dependencies.
9015 * When removing a directory entry, the entry's inode pointer must be
9016 * zero'ed on disk before the corresponding inode's link count is decremented
9017 * (possibly freeing the inode for re-use). This dependency is handled by
9018 * updating the directory entry but delaying the inode count reduction until
9019 * after the directory block has been written to disk. After this point, the
9020 * inode count can be decremented whenever it is convenient.
9024 * This routine should be called immediately after removing
9025 * a directory entry. The inode's link count should not be
9026 * decremented by the calling procedure -- the soft updates
9027 * code will do this task when it is safe.
9030 softdep_setup_remove(bp, dp, ip, isrmdir)
9031 struct buf *bp; /* buffer containing directory block */
9032 struct inode *dp; /* inode for the directory being modified */
9033 struct inode *ip; /* inode for directory entry being removed */
9034 int isrmdir; /* indicates if doing RMDIR */
9036 struct dirrem *dirrem, *prevdirrem;
9037 struct inodedep *inodedep;
9038 struct ufsmount *ump;
9042 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9043 ("softdep_setup_remove called on non-softdep filesystem"));
9045 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
9046 * newdirrem() to setup the full directory remove which requires
9049 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9051 * Add the dirrem to the inodedep's pending remove list for quick
9054 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9055 panic("softdep_setup_remove: Lost inodedep.");
9056 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9057 dirrem->dm_state |= ONDEPLIST;
9058 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9061 * If the COMPLETE flag is clear, then there were no active
9062 * entries and we want to roll back to a zeroed entry until
9063 * the new inode is committed to disk. If the COMPLETE flag is
9064 * set then we have deleted an entry that never made it to
9065 * disk. If the entry we deleted resulted from a name change,
9066 * then the old name still resides on disk. We cannot delete
9067 * its inode (returned to us in prevdirrem) until the zeroed
9068 * directory entry gets to disk. The new inode has never been
9069 * referenced on the disk, so can be deleted immediately.
9071 if ((dirrem->dm_state & COMPLETE) == 0) {
9072 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9076 if (prevdirrem != NULL)
9077 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9078 prevdirrem, dm_next);
9079 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9080 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9083 handle_workitem_remove(dirrem, 0);
9088 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9089 * pd_pendinghd list of a pagedep.
9091 static struct diradd *
9092 diradd_lookup(pagedep, offset)
9093 struct pagedep *pagedep;
9098 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9099 if (dap->da_offset == offset)
9101 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9102 if (dap->da_offset == offset)
9108 * Search for a .. diradd dependency in a directory that is being removed.
9109 * If the directory was renamed to a new parent we have a diradd rather
9110 * than a mkdir for the .. entry. We need to cancel it now before
9111 * it is found in truncate().
9113 static struct jremref *
9114 cancel_diradd_dotdot(ip, dirrem, jremref)
9116 struct dirrem *dirrem;
9117 struct jremref *jremref;
9119 struct pagedep *pagedep;
9121 struct worklist *wk;
9123 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9125 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9128 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9130 * Mark any journal work as belonging to the parent so it is freed
9131 * with the .. reference.
9133 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9134 wk->wk_state |= MKDIR_PARENT;
9139 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9140 * replace it with a dirrem/diradd pair as a result of re-parenting a
9141 * directory. This ensures that we don't simultaneously have a mkdir and
9142 * a diradd for the same .. entry.
9144 static struct jremref *
9145 cancel_mkdir_dotdot(ip, dirrem, jremref)
9147 struct dirrem *dirrem;
9148 struct jremref *jremref;
9150 struct inodedep *inodedep;
9151 struct jaddref *jaddref;
9152 struct ufsmount *ump;
9153 struct mkdir *mkdir;
9158 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9160 dap = inodedep->id_mkdiradd;
9161 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9163 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9164 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9165 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9166 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9169 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9170 if ((jaddref = mkdir->md_jaddref) != NULL) {
9171 mkdir->md_jaddref = NULL;
9172 jaddref->ja_state &= ~MKDIR_PARENT;
9173 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9174 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9175 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9176 journal_jremref(dirrem, jremref, inodedep);
9180 if (mkdir->md_state & ONWORKLIST)
9181 WORKLIST_REMOVE(&mkdir->md_list);
9182 mkdir->md_state |= ALLCOMPLETE;
9183 complete_mkdir(mkdir);
9188 journal_jremref(dirrem, jremref, inodedep)
9189 struct dirrem *dirrem;
9190 struct jremref *jremref;
9191 struct inodedep *inodedep;
9194 if (inodedep == NULL)
9195 if (inodedep_lookup(jremref->jr_list.wk_mp,
9196 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9197 panic("journal_jremref: Lost inodedep");
9198 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9199 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9200 add_to_journal(&jremref->jr_list);
9204 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9205 struct dirrem *dirrem;
9206 struct jremref *jremref;
9207 struct jremref *dotremref;
9208 struct jremref *dotdotremref;
9210 struct inodedep *inodedep;
9212 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9214 panic("dirrem_journal: Lost inodedep");
9215 journal_jremref(dirrem, jremref, inodedep);
9217 journal_jremref(dirrem, dotremref, inodedep);
9219 journal_jremref(dirrem, dotdotremref, NULL);
9223 * Allocate a new dirrem if appropriate and return it along with
9224 * its associated pagedep. Called without a lock, returns with lock.
9226 static struct dirrem *
9227 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9228 struct buf *bp; /* buffer containing directory block */
9229 struct inode *dp; /* inode for the directory being modified */
9230 struct inode *ip; /* inode for directory entry being removed */
9231 int isrmdir; /* indicates if doing RMDIR */
9232 struct dirrem **prevdirremp; /* previously referenced inode, if any */
9237 struct dirrem *dirrem;
9238 struct pagedep *pagedep;
9239 struct jremref *jremref;
9240 struct jremref *dotremref;
9241 struct jremref *dotdotremref;
9243 struct ufsmount *ump;
9246 * Whiteouts have no deletion dependencies.
9249 panic("newdirrem: whiteout");
9254 * If the system is over its limit and our filesystem is
9255 * responsible for more than our share of that usage and
9256 * we are not a snapshot, request some inodedep cleanup.
9257 * Limiting the number of dirrem structures will also limit
9258 * the number of freefile and freeblks structures.
9261 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9262 schedule_cleanup(UFSTOVFS(ump));
9265 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9267 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9268 LIST_INIT(&dirrem->dm_jremrefhd);
9269 LIST_INIT(&dirrem->dm_jwork);
9270 dirrem->dm_state = isrmdir ? RMDIR : 0;
9271 dirrem->dm_oldinum = ip->i_number;
9272 *prevdirremp = NULL;
9274 * Allocate remove reference structures to track journal write
9275 * dependencies. We will always have one for the link and
9276 * when doing directories we will always have one more for dot.
9277 * When renaming a directory we skip the dotdot link change so
9278 * this is not needed.
9280 jremref = dotremref = dotdotremref = NULL;
9281 if (DOINGSUJ(dvp)) {
9283 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9284 ip->i_effnlink + 2);
9285 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9286 ip->i_effnlink + 1);
9287 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9288 dp->i_effnlink + 1);
9289 dotdotremref->jr_state |= MKDIR_PARENT;
9291 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9292 ip->i_effnlink + 1);
9295 lbn = lblkno(ump->um_fs, dp->i_offset);
9296 offset = blkoff(ump->um_fs, dp->i_offset);
9297 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9299 dirrem->dm_pagedep = pagedep;
9300 dirrem->dm_offset = offset;
9302 * If we're renaming a .. link to a new directory, cancel any
9303 * existing MKDIR_PARENT mkdir. If it has already been canceled
9304 * the jremref is preserved for any potential diradd in this
9305 * location. This can not coincide with a rmdir.
9307 if (dp->i_offset == DOTDOT_OFFSET) {
9309 panic("newdirrem: .. directory change during remove?");
9310 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9313 * If we're removing a directory search for the .. dependency now and
9314 * cancel it. Any pending journal work will be added to the dirrem
9315 * to be completed when the workitem remove completes.
9318 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9320 * Check for a diradd dependency for the same directory entry.
9321 * If present, then both dependencies become obsolete and can
9324 dap = diradd_lookup(pagedep, offset);
9327 * Link the jremref structures into the dirrem so they are
9328 * written prior to the pagedep.
9331 dirrem_journal(dirrem, jremref, dotremref,
9336 * Must be ATTACHED at this point.
9338 if ((dap->da_state & ATTACHED) == 0)
9339 panic("newdirrem: not ATTACHED");
9340 if (dap->da_newinum != ip->i_number)
9341 panic("newdirrem: inum %ju should be %ju",
9342 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9344 * If we are deleting a changed name that never made it to disk,
9345 * then return the dirrem describing the previous inode (which
9346 * represents the inode currently referenced from this entry on disk).
9348 if ((dap->da_state & DIRCHG) != 0) {
9349 *prevdirremp = dap->da_previous;
9350 dap->da_state &= ~DIRCHG;
9351 dap->da_pagedep = pagedep;
9354 * We are deleting an entry that never made it to disk.
9355 * Mark it COMPLETE so we can delete its inode immediately.
9357 dirrem->dm_state |= COMPLETE;
9358 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9361 struct worklist *wk;
9363 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9364 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9365 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9373 * Directory entry change dependencies.
9375 * Changing an existing directory entry requires that an add operation
9376 * be completed first followed by a deletion. The semantics for the addition
9377 * are identical to the description of adding a new entry above except
9378 * that the rollback is to the old inode number rather than zero. Once
9379 * the addition dependency is completed, the removal is done as described
9380 * in the removal routine above.
9384 * This routine should be called immediately after changing
9385 * a directory entry. The inode's link count should not be
9386 * decremented by the calling procedure -- the soft updates
9387 * code will perform this task when it is safe.
9390 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9391 struct buf *bp; /* buffer containing directory block */
9392 struct inode *dp; /* inode for the directory being modified */
9393 struct inode *ip; /* inode for directory entry being removed */
9394 ino_t newinum; /* new inode number for changed entry */
9395 int isrmdir; /* indicates if doing RMDIR */
9398 struct diradd *dap = NULL;
9399 struct dirrem *dirrem, *prevdirrem;
9400 struct pagedep *pagedep;
9401 struct inodedep *inodedep;
9402 struct jaddref *jaddref;
9404 struct ufsmount *ump;
9408 offset = blkoff(ump->um_fs, dp->i_offset);
9409 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9410 ("softdep_setup_directory_change called on non-softdep filesystem"));
9413 * Whiteouts do not need diradd dependencies.
9415 if (newinum != UFS_WINO) {
9416 dap = malloc(sizeof(struct diradd),
9417 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9418 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9419 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9420 dap->da_offset = offset;
9421 dap->da_newinum = newinum;
9422 LIST_INIT(&dap->da_jwork);
9426 * Allocate a new dirrem and ACQUIRE_LOCK.
9428 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9429 pagedep = dirrem->dm_pagedep;
9431 * The possible values for isrmdir:
9432 * 0 - non-directory file rename
9433 * 1 - directory rename within same directory
9434 * inum - directory rename to new directory of given inode number
9435 * When renaming to a new directory, we are both deleting and
9436 * creating a new directory entry, so the link count on the new
9437 * directory should not change. Thus we do not need the followup
9438 * dirrem which is usually done in handle_workitem_remove. We set
9439 * the DIRCHG flag to tell handle_workitem_remove to skip the
9443 dirrem->dm_state |= DIRCHG;
9446 * Whiteouts have no additional dependencies,
9447 * so just put the dirrem on the correct list.
9449 if (newinum == UFS_WINO) {
9450 if ((dirrem->dm_state & COMPLETE) == 0) {
9451 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9454 dirrem->dm_dirinum = pagedep->pd_ino;
9455 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9456 add_to_worklist(&dirrem->dm_list, 0);
9462 * Add the dirrem to the inodedep's pending remove list for quick
9463 * discovery later. A valid nlinkdelta ensures that this lookup
9466 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9467 panic("softdep_setup_directory_change: Lost inodedep.");
9468 dirrem->dm_state |= ONDEPLIST;
9469 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9472 * If the COMPLETE flag is clear, then there were no active
9473 * entries and we want to roll back to the previous inode until
9474 * the new inode is committed to disk. If the COMPLETE flag is
9475 * set, then we have deleted an entry that never made it to disk.
9476 * If the entry we deleted resulted from a name change, then the old
9477 * inode reference still resides on disk. Any rollback that we do
9478 * needs to be to that old inode (returned to us in prevdirrem). If
9479 * the entry we deleted resulted from a create, then there is
9480 * no entry on the disk, so we want to roll back to zero rather
9481 * than the uncommitted inode. In either of the COMPLETE cases we
9482 * want to immediately free the unwritten and unreferenced inode.
9484 if ((dirrem->dm_state & COMPLETE) == 0) {
9485 dap->da_previous = dirrem;
9487 if (prevdirrem != NULL) {
9488 dap->da_previous = prevdirrem;
9490 dap->da_state &= ~DIRCHG;
9491 dap->da_pagedep = pagedep;
9493 dirrem->dm_dirinum = pagedep->pd_ino;
9494 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9495 add_to_worklist(&dirrem->dm_list, 0);
9498 * Lookup the jaddref for this journal entry. We must finish
9499 * initializing it and make the diradd write dependent on it.
9500 * If we're not journaling, put it on the id_bufwait list if the
9501 * inode is not yet written. If it is written, do the post-inode
9502 * write processing to put it on the id_pendinghd list.
9504 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9505 if (MOUNTEDSUJ(mp)) {
9506 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9508 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9509 ("softdep_setup_directory_change: bad jaddref %p",
9511 jaddref->ja_diroff = dp->i_offset;
9512 jaddref->ja_diradd = dap;
9513 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9515 add_to_journal(&jaddref->ja_list);
9516 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9517 dap->da_state |= COMPLETE;
9518 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9519 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9521 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9523 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9526 * If we're making a new name for a directory that has not been
9527 * committed when need to move the dot and dotdot references to
9530 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET)
9531 merge_diradd(inodedep, dap);
9536 * Called whenever the link count on an inode is changed.
9537 * It creates an inode dependency so that the new reference(s)
9538 * to the inode cannot be committed to disk until the updated
9539 * inode has been written.
9542 softdep_change_linkcnt(ip)
9543 struct inode *ip; /* the inode with the increased link count */
9545 struct inodedep *inodedep;
9546 struct ufsmount *ump;
9549 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9550 ("softdep_change_linkcnt called on non-softdep filesystem"));
9552 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9553 if (ip->i_nlink < ip->i_effnlink)
9554 panic("softdep_change_linkcnt: bad delta");
9555 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9560 * Attach a sbdep dependency to the superblock buf so that we can keep
9561 * track of the head of the linked list of referenced but unlinked inodes.
9564 softdep_setup_sbupdate(ump, fs, bp)
9565 struct ufsmount *ump;
9569 struct sbdep *sbdep;
9570 struct worklist *wk;
9572 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9573 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9574 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9575 if (wk->wk_type == D_SBDEP)
9579 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9580 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9582 sbdep->sb_ump = ump;
9584 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9589 * Return the first unlinked inodedep which is ready to be the head of the
9590 * list. The inodedep and all those after it must have valid next pointers.
9592 static struct inodedep *
9593 first_unlinked_inodedep(ump)
9594 struct ufsmount *ump;
9596 struct inodedep *inodedep;
9597 struct inodedep *idp;
9600 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9601 inodedep; inodedep = idp) {
9602 if ((inodedep->id_state & UNLINKNEXT) == 0)
9604 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9605 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9607 if ((inodedep->id_state & UNLINKPREV) == 0)
9614 * Set the sujfree unlinked head pointer prior to writing a superblock.
9617 initiate_write_sbdep(sbdep)
9618 struct sbdep *sbdep;
9620 struct inodedep *inodedep;
9624 bpfs = sbdep->sb_fs;
9625 fs = sbdep->sb_ump->um_fs;
9626 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9628 fs->fs_sujfree = inodedep->id_ino;
9629 inodedep->id_state |= UNLINKPREV;
9632 bpfs->fs_sujfree = fs->fs_sujfree;
9634 * Because we have made changes to the superblock, we need to
9635 * recompute its check-hash.
9637 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9641 * After a superblock is written determine whether it must be written again
9642 * due to a changing unlinked list head.
9645 handle_written_sbdep(sbdep, bp)
9646 struct sbdep *sbdep;
9649 struct inodedep *inodedep;
9652 LOCK_OWNED(sbdep->sb_ump);
9655 * If the superblock doesn't match the in-memory list start over.
9657 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9658 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9659 (inodedep == NULL && fs->fs_sujfree != 0)) {
9663 WORKITEM_FREE(sbdep, D_SBDEP);
9664 if (fs->fs_sujfree == 0)
9667 * Now that we have a record of this inode in stable store allow it
9668 * to be written to free up pending work. Inodes may see a lot of
9669 * write activity after they are unlinked which we must not hold up.
9671 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9672 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9673 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9674 inodedep, inodedep->id_state);
9675 if (inodedep->id_state & UNLINKONLIST)
9677 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9684 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9687 unlinked_inodedep(mp, inodedep)
9689 struct inodedep *inodedep;
9691 struct ufsmount *ump;
9695 if (MOUNTEDSUJ(mp) == 0)
9697 ump->um_fs->fs_fmod = 1;
9698 if (inodedep->id_state & UNLINKED)
9699 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9700 inodedep->id_state |= UNLINKED;
9701 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9705 * Remove an inodedep from the unlinked inodedep list. This may require
9706 * disk writes if the inode has made it that far.
9709 clear_unlinked_inodedep(inodedep)
9710 struct inodedep *inodedep;
9712 struct ufs2_dinode *dip;
9713 struct ufsmount *ump;
9714 struct inodedep *idp;
9715 struct inodedep *idn;
9716 struct fs *fs, *bpfs;
9724 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9726 ino = inodedep->id_ino;
9730 KASSERT((inodedep->id_state & UNLINKED) != 0,
9731 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9734 * If nothing has yet been written simply remove us from
9735 * the in memory list and return. This is the most common
9736 * case where handle_workitem_remove() loses the final
9739 if ((inodedep->id_state & UNLINKLINKS) == 0)
9742 * If we have a NEXT pointer and no PREV pointer we can simply
9743 * clear NEXT's PREV and remove ourselves from the list. Be
9744 * careful not to clear PREV if the superblock points at
9747 idn = TAILQ_NEXT(inodedep, id_unlinked);
9748 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9749 if (idn && fs->fs_sujfree != idn->id_ino)
9750 idn->id_state &= ~UNLINKPREV;
9754 * Here we have an inodedep which is actually linked into
9755 * the list. We must remove it by forcing a write to the
9756 * link before us, whether it be the superblock or an inode.
9757 * Unfortunately the list may change while we're waiting
9758 * on the buf lock for either resource so we must loop until
9759 * we lock the right one. If both the superblock and an
9760 * inode point to this inode we must clear the inode first
9761 * followed by the superblock.
9763 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9765 if (idp && (idp->id_state & UNLINKNEXT))
9769 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9770 (int)fs->fs_sbsize, 0, 0, 0);
9772 dbn = fsbtodb(fs, ino_to_fsba(fs, pino));
9773 error = ffs_breadz(ump, ump->um_devvp, dbn, dbn,
9774 (int)fs->fs_bsize, NULL, NULL, 0, NOCRED, 0, NULL,
9780 /* If the list has changed restart the loop. */
9781 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9783 if (idp && (idp->id_state & UNLINKNEXT))
9786 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9793 idn = TAILQ_NEXT(inodedep, id_unlinked);
9797 * Remove us from the in memory list. After this we cannot
9798 * access the inodedep.
9800 KASSERT((inodedep->id_state & UNLINKED) != 0,
9801 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9803 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9804 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9807 * The predecessor's next pointer is manually updated here
9808 * so that the NEXT flag is never cleared for an element
9809 * that is in the list.
9812 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9813 bpfs = (struct fs *)bp->b_data;
9814 ffs_oldfscompat_write(bpfs, ump);
9815 softdep_setup_sbupdate(ump, bpfs, bp);
9817 * Because we may have made changes to the superblock,
9818 * we need to recompute its check-hash.
9820 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9821 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
9822 ((struct ufs1_dinode *)bp->b_data +
9823 ino_to_fsbo(fs, pino))->di_freelink = nino;
9825 dip = (struct ufs2_dinode *)bp->b_data +
9826 ino_to_fsbo(fs, pino);
9827 dip->di_freelink = nino;
9828 ffs_update_dinode_ckhash(fs, dip);
9831 * If the bwrite fails we have no recourse to recover. The
9832 * filesystem is corrupted already.
9837 * If the superblock pointer still needs to be cleared force
9840 if (fs->fs_sujfree == ino) {
9842 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9843 (int)fs->fs_sbsize, 0, 0, 0);
9844 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9845 bpfs = (struct fs *)bp->b_data;
9846 ffs_oldfscompat_write(bpfs, ump);
9847 softdep_setup_sbupdate(ump, bpfs, bp);
9849 * Because we may have made changes to the superblock,
9850 * we need to recompute its check-hash.
9852 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9857 if (fs->fs_sujfree != ino)
9859 panic("clear_unlinked_inodedep: Failed to clear free head");
9861 if (inodedep->id_ino == fs->fs_sujfree)
9862 panic("clear_unlinked_inodedep: Freeing head of free list");
9863 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9864 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9869 * This workitem decrements the inode's link count.
9870 * If the link count reaches zero, the file is removed.
9873 handle_workitem_remove(dirrem, flags)
9874 struct dirrem *dirrem;
9877 struct inodedep *inodedep;
9878 struct workhead dotdotwk;
9879 struct worklist *wk;
9880 struct ufsmount *ump;
9886 if (dirrem->dm_state & ONWORKLIST)
9887 panic("handle_workitem_remove: dirrem %p still on worklist",
9889 oldinum = dirrem->dm_oldinum;
9890 mp = dirrem->dm_list.wk_mp;
9892 flags |= LK_EXCLUSIVE;
9893 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0)
9896 MPASS(ip->i_mode != 0);
9898 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
9899 panic("handle_workitem_remove: lost inodedep");
9900 if (dirrem->dm_state & ONDEPLIST)
9901 LIST_REMOVE(dirrem, dm_inonext);
9902 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
9903 ("handle_workitem_remove: Journal entries not written."));
9906 * Move all dependencies waiting on the remove to complete
9907 * from the dirrem to the inode inowait list to be completed
9908 * after the inode has been updated and written to disk.
9910 * Any marked MKDIR_PARENT are saved to be completed when the
9911 * dotdot ref is removed unless DIRCHG is specified. For
9912 * directory change operations there will be no further
9913 * directory writes and the jsegdeps need to be moved along
9914 * with the rest to be completed when the inode is free or
9915 * stable in the inode free list.
9917 LIST_INIT(&dotdotwk);
9918 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
9919 WORKLIST_REMOVE(wk);
9920 if ((dirrem->dm_state & DIRCHG) == 0 &&
9921 wk->wk_state & MKDIR_PARENT) {
9922 wk->wk_state &= ~MKDIR_PARENT;
9923 WORKLIST_INSERT(&dotdotwk, wk);
9926 WORKLIST_INSERT(&inodedep->id_inowait, wk);
9928 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
9930 * Normal file deletion.
9932 if ((dirrem->dm_state & RMDIR) == 0) {
9934 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
9935 "%ju negative i_nlink %d", (intmax_t)ip->i_number,
9937 DIP_SET(ip, i_nlink, ip->i_nlink);
9938 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
9939 if (ip->i_nlink < ip->i_effnlink)
9940 panic("handle_workitem_remove: bad file delta");
9941 if (ip->i_nlink == 0)
9942 unlinked_inodedep(mp, inodedep);
9943 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9944 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9945 ("handle_workitem_remove: worklist not empty. %s",
9946 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
9947 WORKITEM_FREE(dirrem, D_DIRREM);
9952 * Directory deletion. Decrement reference count for both the
9953 * just deleted parent directory entry and the reference for ".".
9954 * Arrange to have the reference count on the parent decremented
9955 * to account for the loss of "..".
9958 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
9959 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
9960 DIP_SET(ip, i_nlink, ip->i_nlink);
9961 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
9962 if (ip->i_nlink < ip->i_effnlink)
9963 panic("handle_workitem_remove: bad dir delta");
9964 if (ip->i_nlink == 0)
9965 unlinked_inodedep(mp, inodedep);
9966 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9968 * Rename a directory to a new parent. Since, we are both deleting
9969 * and creating a new directory entry, the link count on the new
9970 * directory should not change. Thus we skip the followup dirrem.
9972 if (dirrem->dm_state & DIRCHG) {
9973 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9974 ("handle_workitem_remove: DIRCHG and worklist not empty."));
9975 WORKITEM_FREE(dirrem, D_DIRREM);
9979 dirrem->dm_state = ONDEPLIST;
9980 dirrem->dm_oldinum = dirrem->dm_dirinum;
9982 * Place the dirrem on the parent's diremhd list.
9984 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
9985 panic("handle_workitem_remove: lost dir inodedep");
9986 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9988 * If the allocated inode has never been written to disk, then
9989 * the on-disk inode is zero'ed and we can remove the file
9990 * immediately. When journaling if the inode has been marked
9991 * unlinked and not DEPCOMPLETE we know it can never be written.
9993 inodedep_lookup(mp, oldinum, 0, &inodedep);
9994 if (inodedep == NULL ||
9995 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
9996 check_inode_unwritten(inodedep)) {
9999 return handle_workitem_remove(dirrem, flags);
10001 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
10003 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
10011 * Inode de-allocation dependencies.
10013 * When an inode's link count is reduced to zero, it can be de-allocated. We
10014 * found it convenient to postpone de-allocation until after the inode is
10015 * written to disk with its new link count (zero). At this point, all of the
10016 * on-disk inode's block pointers are nullified and, with careful dependency
10017 * list ordering, all dependencies related to the inode will be satisfied and
10018 * the corresponding dependency structures de-allocated. So, if/when the
10019 * inode is reused, there will be no mixing of old dependencies with new
10020 * ones. This artificial dependency is set up by the block de-allocation
10021 * procedure above (softdep_setup_freeblocks) and completed by the
10022 * following procedure.
10025 handle_workitem_freefile(freefile)
10026 struct freefile *freefile;
10028 struct workhead wkhd;
10030 struct ufsmount *ump;
10033 struct inodedep *idp;
10036 ump = VFSTOUFS(freefile->fx_list.wk_mp);
10040 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10043 panic("handle_workitem_freefile: inodedep %p survived", idp);
10046 fs->fs_pendinginodes -= 1;
10049 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10050 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10051 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10052 softdep_error("handle_workitem_freefile", error);
10054 WORKITEM_FREE(freefile, D_FREEFILE);
10059 * Helper function which unlinks marker element from work list and returns
10060 * the next element on the list.
10062 static __inline struct worklist *
10063 markernext(struct worklist *marker)
10065 struct worklist *next;
10067 next = LIST_NEXT(marker, wk_list);
10068 LIST_REMOVE(marker, wk_list);
10075 * The dependency structures constructed above are most actively used when file
10076 * system blocks are written to disk. No constraints are placed on when a
10077 * block can be written, but unsatisfied update dependencies are made safe by
10078 * modifying (or replacing) the source memory for the duration of the disk
10079 * write. When the disk write completes, the memory block is again brought
10082 * In-core inode structure reclamation.
10084 * Because there are a finite number of "in-core" inode structures, they are
10085 * reused regularly. By transferring all inode-related dependencies to the
10086 * in-memory inode block and indexing them separately (via "inodedep"s), we
10087 * can allow "in-core" inode structures to be reused at any time and avoid
10088 * any increase in contention.
10090 * Called just before entering the device driver to initiate a new disk I/O.
10091 * The buffer must be locked, thus, no I/O completion operations can occur
10092 * while we are manipulating its associated dependencies.
10095 softdep_disk_io_initiation(bp)
10096 struct buf *bp; /* structure describing disk write to occur */
10098 struct worklist *wk;
10099 struct worklist marker;
10100 struct inodedep *inodedep;
10101 struct freeblks *freeblks;
10102 struct jblkdep *jblkdep;
10103 struct newblk *newblk;
10104 struct ufsmount *ump;
10107 * We only care about write operations. There should never
10108 * be dependencies for reads.
10110 if (bp->b_iocmd != BIO_WRITE)
10111 panic("softdep_disk_io_initiation: not write");
10113 if (bp->b_vflags & BV_BKGRDINPROG)
10114 panic("softdep_disk_io_initiation: Writing buffer with "
10115 "background write in progress: %p", bp);
10117 ump = softdep_bp_to_mp(bp);
10121 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10122 PHOLD(curproc); /* Don't swap out kernel stack */
10125 * Do any necessary pre-I/O processing.
10127 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10128 wk = markernext(&marker)) {
10129 LIST_INSERT_AFTER(wk, &marker, wk_list);
10130 switch (wk->wk_type) {
10132 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10136 inodedep = WK_INODEDEP(wk);
10137 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10138 initiate_write_inodeblock_ufs1(inodedep, bp);
10140 initiate_write_inodeblock_ufs2(inodedep, bp);
10144 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10148 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10152 WK_JSEG(wk)->js_buf = NULL;
10156 freeblks = WK_FREEBLKS(wk);
10157 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10159 * We have to wait for the freeblks to be journaled
10160 * before we can write an inodeblock with updated
10161 * pointers. Be careful to arrange the marker so
10162 * we revisit the freeblks if it's not removed by
10163 * the first jwait().
10165 if (jblkdep != NULL) {
10166 LIST_REMOVE(&marker, wk_list);
10167 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10168 jwait(&jblkdep->jb_list, MNT_WAIT);
10171 case D_ALLOCDIRECT:
10174 * We have to wait for the jnewblk to be journaled
10175 * before we can write to a block if the contents
10176 * may be confused with an earlier file's indirect
10177 * at recovery time. Handle the marker as described
10180 newblk = WK_NEWBLK(wk);
10181 if (newblk->nb_jnewblk != NULL &&
10182 indirblk_lookup(newblk->nb_list.wk_mp,
10183 newblk->nb_newblkno)) {
10184 LIST_REMOVE(&marker, wk_list);
10185 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10186 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10191 initiate_write_sbdep(WK_SBDEP(wk));
10201 panic("handle_disk_io_initiation: Unexpected type %s",
10202 TYPENAME(wk->wk_type));
10207 PRELE(curproc); /* Allow swapout of kernel stack */
10211 * Called from within the procedure above to deal with unsatisfied
10212 * allocation dependencies in a directory. The buffer must be locked,
10213 * thus, no I/O completion operations can occur while we are
10214 * manipulating its associated dependencies.
10217 initiate_write_filepage(pagedep, bp)
10218 struct pagedep *pagedep;
10221 struct jremref *jremref;
10222 struct jmvref *jmvref;
10223 struct dirrem *dirrem;
10224 struct diradd *dap;
10228 if (pagedep->pd_state & IOSTARTED) {
10230 * This can only happen if there is a driver that does not
10231 * understand chaining. Here biodone will reissue the call
10232 * to strategy for the incomplete buffers.
10234 printf("initiate_write_filepage: already started\n");
10237 pagedep->pd_state |= IOSTARTED;
10239 * Wait for all journal remove dependencies to hit the disk.
10240 * We can not allow any potentially conflicting directory adds
10241 * to be visible before removes and rollback is too difficult.
10242 * The per-filesystem lock may be dropped and re-acquired, however
10243 * we hold the buf locked so the dependency can not go away.
10245 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10246 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10247 jwait(&jremref->jr_list, MNT_WAIT);
10248 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10249 jwait(&jmvref->jm_list, MNT_WAIT);
10250 for (i = 0; i < DAHASHSZ; i++) {
10251 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10252 ep = (struct direct *)
10253 ((char *)bp->b_data + dap->da_offset);
10254 if (ep->d_ino != dap->da_newinum)
10255 panic("%s: dir inum %ju != new %ju",
10256 "initiate_write_filepage",
10257 (uintmax_t)ep->d_ino,
10258 (uintmax_t)dap->da_newinum);
10259 if (dap->da_state & DIRCHG)
10260 ep->d_ino = dap->da_previous->dm_oldinum;
10263 dap->da_state &= ~ATTACHED;
10264 dap->da_state |= UNDONE;
10270 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10271 * Note that any bug fixes made to this routine must be done in the
10272 * version found below.
10274 * Called from within the procedure above to deal with unsatisfied
10275 * allocation dependencies in an inodeblock. The buffer must be
10276 * locked, thus, no I/O completion operations can occur while we
10277 * are manipulating its associated dependencies.
10280 initiate_write_inodeblock_ufs1(inodedep, bp)
10281 struct inodedep *inodedep;
10282 struct buf *bp; /* The inode block */
10284 struct allocdirect *adp, *lastadp;
10285 struct ufs1_dinode *dp;
10286 struct ufs1_dinode *sip;
10287 struct inoref *inoref;
10288 struct ufsmount *ump;
10292 ufs_lbn_t prevlbn = 0;
10296 if (inodedep->id_state & IOSTARTED)
10297 panic("initiate_write_inodeblock_ufs1: already started");
10298 inodedep->id_state |= IOSTARTED;
10299 fs = inodedep->id_fs;
10300 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10302 dp = (struct ufs1_dinode *)bp->b_data +
10303 ino_to_fsbo(fs, inodedep->id_ino);
10306 * If we're on the unlinked list but have not yet written our
10307 * next pointer initialize it here.
10309 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10310 struct inodedep *inon;
10312 inon = TAILQ_NEXT(inodedep, id_unlinked);
10313 dp->di_freelink = inon ? inon->id_ino : 0;
10316 * If the bitmap is not yet written, then the allocated
10317 * inode cannot be written to disk.
10319 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10320 if (inodedep->id_savedino1 != NULL)
10321 panic("initiate_write_inodeblock_ufs1: I/O underway");
10323 sip = malloc(sizeof(struct ufs1_dinode),
10324 M_SAVEDINO, M_SOFTDEP_FLAGS);
10326 inodedep->id_savedino1 = sip;
10327 *inodedep->id_savedino1 = *dp;
10328 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10329 dp->di_gen = inodedep->id_savedino1->di_gen;
10330 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10334 * If no dependencies, then there is nothing to roll back.
10336 inodedep->id_savedsize = dp->di_size;
10337 inodedep->id_savedextsize = 0;
10338 inodedep->id_savednlink = dp->di_nlink;
10339 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10340 TAILQ_EMPTY(&inodedep->id_inoreflst))
10343 * Revert the link count to that of the first unwritten journal entry.
10345 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10347 dp->di_nlink = inoref->if_nlink;
10349 * Set the dependencies to busy.
10351 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10352 adp = TAILQ_NEXT(adp, ad_next)) {
10354 if (deplist != 0 && prevlbn >= adp->ad_offset)
10355 panic("softdep_write_inodeblock: lbn order");
10356 prevlbn = adp->ad_offset;
10357 if (adp->ad_offset < UFS_NDADDR &&
10358 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10359 panic("initiate_write_inodeblock_ufs1: "
10360 "direct pointer #%jd mismatch %d != %jd",
10361 (intmax_t)adp->ad_offset,
10362 dp->di_db[adp->ad_offset],
10363 (intmax_t)adp->ad_newblkno);
10364 if (adp->ad_offset >= UFS_NDADDR &&
10365 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10366 panic("initiate_write_inodeblock_ufs1: "
10367 "indirect pointer #%jd mismatch %d != %jd",
10368 (intmax_t)adp->ad_offset - UFS_NDADDR,
10369 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10370 (intmax_t)adp->ad_newblkno);
10371 deplist |= 1 << adp->ad_offset;
10372 if ((adp->ad_state & ATTACHED) == 0)
10373 panic("initiate_write_inodeblock_ufs1: "
10374 "Unknown state 0x%x", adp->ad_state);
10375 #endif /* INVARIANTS */
10376 adp->ad_state &= ~ATTACHED;
10377 adp->ad_state |= UNDONE;
10380 * The on-disk inode cannot claim to be any larger than the last
10381 * fragment that has been written. Otherwise, the on-disk inode
10382 * might have fragments that were not the last block in the file
10383 * which would corrupt the filesystem.
10385 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10386 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10387 if (adp->ad_offset >= UFS_NDADDR)
10389 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10390 /* keep going until hitting a rollback to a frag */
10391 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10393 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10394 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10396 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10397 panic("initiate_write_inodeblock_ufs1: "
10399 #endif /* INVARIANTS */
10402 for (i = 0; i < UFS_NIADDR; i++) {
10404 if (dp->di_ib[i] != 0 &&
10405 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10406 panic("initiate_write_inodeblock_ufs1: "
10408 #endif /* INVARIANTS */
10414 * If we have zero'ed out the last allocated block of the file,
10415 * roll back the size to the last currently allocated block.
10416 * We know that this last allocated block is a full-sized as
10417 * we already checked for fragments in the loop above.
10419 if (lastadp != NULL &&
10420 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10421 for (i = lastadp->ad_offset; i >= 0; i--)
10422 if (dp->di_db[i] != 0)
10424 dp->di_size = (i + 1) * fs->fs_bsize;
10427 * The only dependencies are for indirect blocks.
10429 * The file size for indirect block additions is not guaranteed.
10430 * Such a guarantee would be non-trivial to achieve. The conventional
10431 * synchronous write implementation also does not make this guarantee.
10432 * Fsck should catch and fix discrepancies. Arguably, the file size
10433 * can be over-estimated without destroying integrity when the file
10434 * moves into the indirect blocks (i.e., is large). If we want to
10435 * postpone fsck, we are stuck with this argument.
10437 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10438 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10442 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10443 * Note that any bug fixes made to this routine must be done in the
10444 * version found above.
10446 * Called from within the procedure above to deal with unsatisfied
10447 * allocation dependencies in an inodeblock. The buffer must be
10448 * locked, thus, no I/O completion operations can occur while we
10449 * are manipulating its associated dependencies.
10452 initiate_write_inodeblock_ufs2(inodedep, bp)
10453 struct inodedep *inodedep;
10454 struct buf *bp; /* The inode block */
10456 struct allocdirect *adp, *lastadp;
10457 struct ufs2_dinode *dp;
10458 struct ufs2_dinode *sip;
10459 struct inoref *inoref;
10460 struct ufsmount *ump;
10464 ufs_lbn_t prevlbn = 0;
10468 if (inodedep->id_state & IOSTARTED)
10469 panic("initiate_write_inodeblock_ufs2: already started");
10470 inodedep->id_state |= IOSTARTED;
10471 fs = inodedep->id_fs;
10472 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10474 dp = (struct ufs2_dinode *)bp->b_data +
10475 ino_to_fsbo(fs, inodedep->id_ino);
10478 * If we're on the unlinked list but have not yet written our
10479 * next pointer initialize it here.
10481 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10482 struct inodedep *inon;
10484 inon = TAILQ_NEXT(inodedep, id_unlinked);
10485 dp->di_freelink = inon ? inon->id_ino : 0;
10486 ffs_update_dinode_ckhash(fs, dp);
10489 * If the bitmap is not yet written, then the allocated
10490 * inode cannot be written to disk.
10492 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10493 if (inodedep->id_savedino2 != NULL)
10494 panic("initiate_write_inodeblock_ufs2: I/O underway");
10496 sip = malloc(sizeof(struct ufs2_dinode),
10497 M_SAVEDINO, M_SOFTDEP_FLAGS);
10499 inodedep->id_savedino2 = sip;
10500 *inodedep->id_savedino2 = *dp;
10501 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10502 dp->di_gen = inodedep->id_savedino2->di_gen;
10503 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10507 * If no dependencies, then there is nothing to roll back.
10509 inodedep->id_savedsize = dp->di_size;
10510 inodedep->id_savedextsize = dp->di_extsize;
10511 inodedep->id_savednlink = dp->di_nlink;
10512 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10513 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10514 TAILQ_EMPTY(&inodedep->id_inoreflst))
10517 * Revert the link count to that of the first unwritten journal entry.
10519 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10521 dp->di_nlink = inoref->if_nlink;
10524 * Set the ext data dependencies to busy.
10526 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10527 adp = TAILQ_NEXT(adp, ad_next)) {
10529 if (deplist != 0 && prevlbn >= adp->ad_offset)
10530 panic("initiate_write_inodeblock_ufs2: lbn order");
10531 prevlbn = adp->ad_offset;
10532 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10533 panic("initiate_write_inodeblock_ufs2: "
10534 "ext pointer #%jd mismatch %jd != %jd",
10535 (intmax_t)adp->ad_offset,
10536 (intmax_t)dp->di_extb[adp->ad_offset],
10537 (intmax_t)adp->ad_newblkno);
10538 deplist |= 1 << adp->ad_offset;
10539 if ((adp->ad_state & ATTACHED) == 0)
10540 panic("initiate_write_inodeblock_ufs2: Unknown "
10541 "state 0x%x", adp->ad_state);
10542 #endif /* INVARIANTS */
10543 adp->ad_state &= ~ATTACHED;
10544 adp->ad_state |= UNDONE;
10547 * The on-disk inode cannot claim to be any larger than the last
10548 * fragment that has been written. Otherwise, the on-disk inode
10549 * might have fragments that were not the last block in the ext
10550 * data which would corrupt the filesystem.
10552 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10553 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10554 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10555 /* keep going until hitting a rollback to a frag */
10556 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10558 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10559 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10561 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10562 panic("initiate_write_inodeblock_ufs2: "
10564 #endif /* INVARIANTS */
10565 dp->di_extb[i] = 0;
10571 * If we have zero'ed out the last allocated block of the ext
10572 * data, roll back the size to the last currently allocated block.
10573 * We know that this last allocated block is a full-sized as
10574 * we already checked for fragments in the loop above.
10576 if (lastadp != NULL &&
10577 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10578 for (i = lastadp->ad_offset; i >= 0; i--)
10579 if (dp->di_extb[i] != 0)
10581 dp->di_extsize = (i + 1) * fs->fs_bsize;
10584 * Set the file data dependencies to busy.
10586 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10587 adp = TAILQ_NEXT(adp, ad_next)) {
10589 if (deplist != 0 && prevlbn >= adp->ad_offset)
10590 panic("softdep_write_inodeblock: lbn order");
10591 if ((adp->ad_state & ATTACHED) == 0)
10592 panic("inodedep %p and adp %p not attached", inodedep, adp);
10593 prevlbn = adp->ad_offset;
10594 if (!ffs_fsfail_cleanup(ump, 0) &&
10595 adp->ad_offset < UFS_NDADDR &&
10596 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10597 panic("initiate_write_inodeblock_ufs2: "
10598 "direct pointer #%jd mismatch %jd != %jd",
10599 (intmax_t)adp->ad_offset,
10600 (intmax_t)dp->di_db[adp->ad_offset],
10601 (intmax_t)adp->ad_newblkno);
10602 if (!ffs_fsfail_cleanup(ump, 0) &&
10603 adp->ad_offset >= UFS_NDADDR &&
10604 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10605 panic("initiate_write_inodeblock_ufs2: "
10606 "indirect pointer #%jd mismatch %jd != %jd",
10607 (intmax_t)adp->ad_offset - UFS_NDADDR,
10608 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10609 (intmax_t)adp->ad_newblkno);
10610 deplist |= 1 << adp->ad_offset;
10611 if ((adp->ad_state & ATTACHED) == 0)
10612 panic("initiate_write_inodeblock_ufs2: Unknown "
10613 "state 0x%x", adp->ad_state);
10614 #endif /* INVARIANTS */
10615 adp->ad_state &= ~ATTACHED;
10616 adp->ad_state |= UNDONE;
10619 * The on-disk inode cannot claim to be any larger than the last
10620 * fragment that has been written. Otherwise, the on-disk inode
10621 * might have fragments that were not the last block in the file
10622 * which would corrupt the filesystem.
10624 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10625 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10626 if (adp->ad_offset >= UFS_NDADDR)
10628 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10629 /* keep going until hitting a rollback to a frag */
10630 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10632 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10633 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10635 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10636 panic("initiate_write_inodeblock_ufs2: "
10638 #endif /* INVARIANTS */
10641 for (i = 0; i < UFS_NIADDR; i++) {
10643 if (dp->di_ib[i] != 0 &&
10644 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10645 panic("initiate_write_inodeblock_ufs2: "
10647 #endif /* INVARIANTS */
10650 ffs_update_dinode_ckhash(fs, dp);
10654 * If we have zero'ed out the last allocated block of the file,
10655 * roll back the size to the last currently allocated block.
10656 * We know that this last allocated block is a full-sized as
10657 * we already checked for fragments in the loop above.
10659 if (lastadp != NULL &&
10660 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10661 for (i = lastadp->ad_offset; i >= 0; i--)
10662 if (dp->di_db[i] != 0)
10664 dp->di_size = (i + 1) * fs->fs_bsize;
10667 * The only dependencies are for indirect blocks.
10669 * The file size for indirect block additions is not guaranteed.
10670 * Such a guarantee would be non-trivial to achieve. The conventional
10671 * synchronous write implementation also does not make this guarantee.
10672 * Fsck should catch and fix discrepancies. Arguably, the file size
10673 * can be over-estimated without destroying integrity when the file
10674 * moves into the indirect blocks (i.e., is large). If we want to
10675 * postpone fsck, we are stuck with this argument.
10677 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10678 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10679 ffs_update_dinode_ckhash(fs, dp);
10683 * Cancel an indirdep as a result of truncation. Release all of the
10684 * children allocindirs and place their journal work on the appropriate
10688 cancel_indirdep(indirdep, bp, freeblks)
10689 struct indirdep *indirdep;
10691 struct freeblks *freeblks;
10693 struct allocindir *aip;
10696 * None of the indirect pointers will ever be visible,
10697 * so they can simply be tossed. GOINGAWAY ensures
10698 * that allocated pointers will be saved in the buffer
10699 * cache until they are freed. Note that they will
10700 * only be able to be found by their physical address
10701 * since the inode mapping the logical address will
10702 * be gone. The save buffer used for the safe copy
10703 * was allocated in setup_allocindir_phase2 using
10704 * the physical address so it could be used for this
10705 * purpose. Hence we swap the safe copy with the real
10706 * copy, allowing the safe copy to be freed and holding
10707 * on to the real copy for later use in indir_trunc.
10709 if (indirdep->ir_state & GOINGAWAY)
10710 panic("cancel_indirdep: already gone");
10711 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10712 indirdep->ir_state |= DEPCOMPLETE;
10713 LIST_REMOVE(indirdep, ir_next);
10715 indirdep->ir_state |= GOINGAWAY;
10717 * Pass in bp for blocks still have journal writes
10718 * pending so we can cancel them on their own.
10720 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
10721 cancel_allocindir(aip, bp, freeblks, 0);
10722 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
10723 cancel_allocindir(aip, NULL, freeblks, 0);
10724 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
10725 cancel_allocindir(aip, NULL, freeblks, 0);
10726 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
10727 cancel_allocindir(aip, NULL, freeblks, 0);
10729 * If there are pending partial truncations we need to keep the
10730 * old block copy around until they complete. This is because
10731 * the current b_data is not a perfect superset of the available
10734 if (TAILQ_EMPTY(&indirdep->ir_trunc))
10735 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10737 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10738 WORKLIST_REMOVE(&indirdep->ir_list);
10739 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10740 indirdep->ir_bp = NULL;
10741 indirdep->ir_freeblks = freeblks;
10745 * Free an indirdep once it no longer has new pointers to track.
10748 free_indirdep(indirdep)
10749 struct indirdep *indirdep;
10752 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10753 ("free_indirdep: Indir trunc list not empty."));
10754 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10755 ("free_indirdep: Complete head not empty."));
10756 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10757 ("free_indirdep: write head not empty."));
10758 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10759 ("free_indirdep: done head not empty."));
10760 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10761 ("free_indirdep: deplist head not empty."));
10762 KASSERT((indirdep->ir_state & DEPCOMPLETE),
10763 ("free_indirdep: %p still on newblk list.", indirdep));
10764 KASSERT(indirdep->ir_saveddata == NULL,
10765 ("free_indirdep: %p still has saved data.", indirdep));
10766 KASSERT(indirdep->ir_savebp == NULL,
10767 ("free_indirdep: %p still has savebp buffer.", indirdep));
10768 if (indirdep->ir_state & ONWORKLIST)
10769 WORKLIST_REMOVE(&indirdep->ir_list);
10770 WORKITEM_FREE(indirdep, D_INDIRDEP);
10774 * Called before a write to an indirdep. This routine is responsible for
10775 * rolling back pointers to a safe state which includes only those
10776 * allocindirs which have been completed.
10779 initiate_write_indirdep(indirdep, bp)
10780 struct indirdep *indirdep;
10783 struct ufsmount *ump;
10785 indirdep->ir_state |= IOSTARTED;
10786 if (indirdep->ir_state & GOINGAWAY)
10787 panic("disk_io_initiation: indirdep gone");
10789 * If there are no remaining dependencies, this will be writing
10790 * the real pointers.
10792 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10793 TAILQ_EMPTY(&indirdep->ir_trunc))
10796 * Replace up-to-date version with safe version.
10798 if (indirdep->ir_saveddata == NULL) {
10799 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10802 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10806 indirdep->ir_state &= ~ATTACHED;
10807 indirdep->ir_state |= UNDONE;
10808 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10809 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10814 * Called when an inode has been cleared in a cg bitmap. This finally
10815 * eliminates any canceled jaddrefs
10818 softdep_setup_inofree(mp, bp, ino, wkhd)
10822 struct workhead *wkhd;
10824 struct worklist *wk, *wkn;
10825 struct inodedep *inodedep;
10826 struct ufsmount *ump;
10831 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10832 ("softdep_setup_inofree called on non-softdep filesystem"));
10833 ump = VFSTOUFS(mp);
10835 if (!ffs_fsfail_cleanup(ump, 0)) {
10837 cgp = (struct cg *)bp->b_data;
10838 inosused = cg_inosused(cgp);
10839 if (isset(inosused, ino % fs->fs_ipg))
10840 panic("softdep_setup_inofree: inode %ju not freed.",
10843 if (inodedep_lookup(mp, ino, 0, &inodedep))
10844 panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
10845 (uintmax_t)ino, inodedep);
10847 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10848 if (wk->wk_type != D_JADDREF)
10850 WORKLIST_REMOVE(wk);
10852 * We can free immediately even if the jaddref
10853 * isn't attached in a background write as now
10854 * the bitmaps are reconciled.
10856 wk->wk_state |= COMPLETE | ATTACHED;
10857 free_jaddref(WK_JADDREF(wk));
10859 jwork_move(&bp->b_dep, wkhd);
10865 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10866 * map. Any dependencies waiting for the write to clear are added to the
10867 * buf's list and any jnewblks that are being canceled are discarded
10871 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
10874 ufs2_daddr_t blkno;
10876 struct workhead *wkhd;
10878 struct bmsafemap *bmsafemap;
10879 struct jnewblk *jnewblk;
10880 struct ufsmount *ump;
10881 struct worklist *wk;
10886 ufs2_daddr_t jstart;
10894 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
10895 blkno, frags, wkhd);
10897 ump = VFSTOUFS(mp);
10898 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
10899 ("softdep_setup_blkfree called on non-softdep filesystem"));
10901 /* Lookup the bmsafemap so we track when it is dirty. */
10903 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10905 * Detach any jnewblks which have been canceled. They must linger
10906 * until the bitmap is cleared again by ffs_blkfree() to prevent
10907 * an unjournaled allocation from hitting the disk.
10910 while ((wk = LIST_FIRST(wkhd)) != NULL) {
10912 "softdep_setup_blkfree: blkno %jd wk type %d",
10913 blkno, wk->wk_type);
10914 WORKLIST_REMOVE(wk);
10915 if (wk->wk_type != D_JNEWBLK) {
10916 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
10919 jnewblk = WK_JNEWBLK(wk);
10920 KASSERT(jnewblk->jn_state & GOINGAWAY,
10921 ("softdep_setup_blkfree: jnewblk not canceled."));
10924 * Assert that this block is free in the bitmap
10925 * before we discard the jnewblk.
10927 cgp = (struct cg *)bp->b_data;
10928 blksfree = cg_blksfree(cgp);
10929 bno = dtogd(fs, jnewblk->jn_blkno);
10930 for (i = jnewblk->jn_oldfrags;
10931 i < jnewblk->jn_frags; i++) {
10932 if (isset(blksfree, bno + i))
10934 panic("softdep_setup_blkfree: not free");
10938 * Even if it's not attached we can free immediately
10939 * as the new bitmap is correct.
10941 wk->wk_state |= COMPLETE | ATTACHED;
10942 free_jnewblk(jnewblk);
10948 * Assert that we are not freeing a block which has an outstanding
10949 * allocation dependency.
10951 fs = VFSTOUFS(mp)->um_fs;
10952 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10953 end = blkno + frags;
10954 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
10956 * Don't match against blocks that will be freed when the
10957 * background write is done.
10959 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
10960 (COMPLETE | DEPCOMPLETE))
10962 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
10963 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
10964 if ((blkno >= jstart && blkno < jend) ||
10965 (end > jstart && end <= jend)) {
10966 printf("state 0x%X %jd - %d %d dep %p\n",
10967 jnewblk->jn_state, jnewblk->jn_blkno,
10968 jnewblk->jn_oldfrags, jnewblk->jn_frags,
10970 panic("softdep_setup_blkfree: "
10971 "%jd-%jd(%d) overlaps with %jd-%jd",
10972 blkno, end, frags, jstart, jend);
10980 * Revert a block allocation when the journal record that describes it
10981 * is not yet written.
10984 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
10985 struct jnewblk *jnewblk;
10990 ufs1_daddr_t fragno;
10996 cgbno = dtogd(fs, jnewblk->jn_blkno);
10998 * We have to test which frags need to be rolled back. We may
10999 * be operating on a stale copy when doing background writes.
11001 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
11002 if (isclr(blksfree, cgbno + i))
11007 * This is mostly ffs_blkfree() sans some validation and
11008 * superblock updates.
11010 if (frags == fs->fs_frag) {
11011 fragno = fragstoblks(fs, cgbno);
11012 ffs_setblock(fs, blksfree, fragno);
11013 ffs_clusteracct(fs, cgp, fragno, 1);
11014 cgp->cg_cs.cs_nbfree++;
11016 cgbno += jnewblk->jn_oldfrags;
11017 bbase = cgbno - fragnum(fs, cgbno);
11018 /* Decrement the old frags. */
11019 blk = blkmap(fs, blksfree, bbase);
11020 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11021 /* Deallocate the fragment */
11022 for (i = 0; i < frags; i++)
11023 setbit(blksfree, cgbno + i);
11024 cgp->cg_cs.cs_nffree += frags;
11025 /* Add back in counts associated with the new frags */
11026 blk = blkmap(fs, blksfree, bbase);
11027 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11028 /* If a complete block has been reassembled, account for it. */
11029 fragno = fragstoblks(fs, bbase);
11030 if (ffs_isblock(fs, blksfree, fragno)) {
11031 cgp->cg_cs.cs_nffree -= fs->fs_frag;
11032 ffs_clusteracct(fs, cgp, fragno, 1);
11033 cgp->cg_cs.cs_nbfree++;
11037 jnewblk->jn_state &= ~ATTACHED;
11038 jnewblk->jn_state |= UNDONE;
11044 initiate_write_bmsafemap(bmsafemap, bp)
11045 struct bmsafemap *bmsafemap;
11046 struct buf *bp; /* The cg block. */
11048 struct jaddref *jaddref;
11049 struct jnewblk *jnewblk;
11057 * If this is a background write, we did this at the time that
11058 * the copy was made, so do not need to do it again.
11060 if (bmsafemap->sm_state & IOSTARTED)
11062 bmsafemap->sm_state |= IOSTARTED;
11064 * Clear any inode allocations which are pending journal writes.
11066 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11067 cgp = (struct cg *)bp->b_data;
11068 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11069 inosused = cg_inosused(cgp);
11070 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11071 ino = jaddref->ja_ino % fs->fs_ipg;
11072 if (isset(inosused, ino)) {
11073 if ((jaddref->ja_mode & IFMT) == IFDIR)
11074 cgp->cg_cs.cs_ndir--;
11075 cgp->cg_cs.cs_nifree++;
11076 clrbit(inosused, ino);
11077 jaddref->ja_state &= ~ATTACHED;
11078 jaddref->ja_state |= UNDONE;
11081 panic("initiate_write_bmsafemap: inode %ju "
11082 "marked free", (uintmax_t)jaddref->ja_ino);
11086 * Clear any block allocations which are pending journal writes.
11088 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11089 cgp = (struct cg *)bp->b_data;
11090 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11091 blksfree = cg_blksfree(cgp);
11092 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11093 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11095 panic("initiate_write_bmsafemap: block %jd "
11096 "marked free", jnewblk->jn_blkno);
11100 * Move allocation lists to the written lists so they can be
11101 * cleared once the block write is complete.
11103 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11104 inodedep, id_deps);
11105 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11107 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11112 softdep_handle_error(struct buf *bp)
11114 struct ufsmount *ump;
11116 ump = softdep_bp_to_mp(bp);
11120 if (ffs_fsfail_cleanup(ump, bp->b_error)) {
11122 * No future writes will succeed, so the on-disk image is safe.
11123 * Pretend that this write succeeded so that the softdep state
11124 * will be cleaned up naturally.
11126 bp->b_ioflags &= ~BIO_ERROR;
11132 * This routine is called during the completion interrupt
11133 * service routine for a disk write (from the procedure called
11134 * by the device driver to inform the filesystem caches of
11135 * a request completion). It should be called early in this
11136 * procedure, before the block is made available to other
11137 * processes or other routines are called.
11141 softdep_disk_write_complete(bp)
11142 struct buf *bp; /* describes the completed disk write */
11144 struct worklist *wk;
11145 struct worklist *owk;
11146 struct ufsmount *ump;
11147 struct workhead reattach;
11148 struct freeblks *freeblks;
11151 ump = softdep_bp_to_mp(bp);
11152 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11153 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11154 "with outstanding dependencies for buffer %p", bp));
11157 if ((bp->b_ioflags & BIO_ERROR) != 0)
11158 softdep_handle_error(bp);
11160 * If an error occurred while doing the write, then the data
11161 * has not hit the disk and the dependencies cannot be processed.
11162 * But we do have to go through and roll forward any dependencies
11163 * that were rolled back before the disk write.
11167 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11168 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11169 switch (wk->wk_type) {
11171 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11175 handle_written_inodeblock(WK_INODEDEP(wk),
11180 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11185 handle_written_indirdep(WK_INDIRDEP(wk),
11189 /* nothing to roll forward */
11198 LIST_INIT(&reattach);
11201 * Ump SU lock must not be released anywhere in this code segment.
11204 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11205 WORKLIST_REMOVE(wk);
11206 atomic_add_long(&dep_write[wk->wk_type], 1);
11208 panic("duplicate worklist: %p\n", wk);
11210 switch (wk->wk_type) {
11212 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11214 WORKLIST_INSERT(&reattach, wk);
11218 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11220 WORKLIST_INSERT(&reattach, wk);
11224 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11226 WORKLIST_INSERT(&reattach, wk);
11230 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11233 case D_ALLOCDIRECT:
11234 wk->wk_state |= COMPLETE;
11235 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11239 wk->wk_state |= COMPLETE;
11240 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11244 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11246 WORKLIST_INSERT(&reattach, wk);
11250 wk->wk_state |= COMPLETE;
11251 freeblks = WK_FREEBLKS(wk);
11252 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11253 LIST_EMPTY(&freeblks->fb_jblkdephd))
11254 add_to_worklist(wk, WK_NODELAY);
11258 handle_written_freework(WK_FREEWORK(wk));
11262 free_jsegdep(WK_JSEGDEP(wk));
11266 handle_written_jseg(WK_JSEG(wk), bp);
11270 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11271 WORKLIST_INSERT(&reattach, wk);
11275 free_freedep(WK_FREEDEP(wk));
11279 panic("handle_disk_write_complete: Unknown type %s",
11280 TYPENAME(wk->wk_type));
11285 * Reattach any requests that must be redone.
11287 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11288 WORKLIST_REMOVE(wk);
11289 WORKLIST_INSERT(&bp->b_dep, wk);
11297 * Called from within softdep_disk_write_complete above.
11300 handle_allocdirect_partdone(adp, wkhd)
11301 struct allocdirect *adp; /* the completed allocdirect */
11302 struct workhead *wkhd; /* Work to do when inode is writtne. */
11304 struct allocdirectlst *listhead;
11305 struct allocdirect *listadp;
11306 struct inodedep *inodedep;
11309 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11310 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11313 * The on-disk inode cannot claim to be any larger than the last
11314 * fragment that has been written. Otherwise, the on-disk inode
11315 * might have fragments that were not the last block in the file
11316 * which would corrupt the filesystem. Thus, we cannot free any
11317 * allocdirects after one whose ad_oldblkno claims a fragment as
11318 * these blocks must be rolled back to zero before writing the inode.
11319 * We check the currently active set of allocdirects in id_inoupdt
11320 * or id_extupdt as appropriate.
11322 inodedep = adp->ad_inodedep;
11323 bsize = inodedep->id_fs->fs_bsize;
11324 if (adp->ad_state & EXTDATA)
11325 listhead = &inodedep->id_extupdt;
11327 listhead = &inodedep->id_inoupdt;
11328 TAILQ_FOREACH(listadp, listhead, ad_next) {
11329 /* found our block */
11330 if (listadp == adp)
11332 /* continue if ad_oldlbn is not a fragment */
11333 if (listadp->ad_oldsize == 0 ||
11334 listadp->ad_oldsize == bsize)
11336 /* hit a fragment */
11340 * If we have reached the end of the current list without
11341 * finding the just finished dependency, then it must be
11342 * on the future dependency list. Future dependencies cannot
11343 * be freed until they are moved to the current list.
11345 if (listadp == NULL) {
11347 if (adp->ad_state & EXTDATA)
11348 listhead = &inodedep->id_newextupdt;
11350 listhead = &inodedep->id_newinoupdt;
11351 TAILQ_FOREACH(listadp, listhead, ad_next)
11352 /* found our block */
11353 if (listadp == adp)
11355 if (listadp == NULL)
11356 panic("handle_allocdirect_partdone: lost dep");
11357 #endif /* INVARIANTS */
11361 * If we have found the just finished dependency, then queue
11362 * it along with anything that follows it that is complete.
11363 * Since the pointer has not yet been written in the inode
11364 * as the dependency prevents it, place the allocdirect on the
11365 * bufwait list where it will be freed once the pointer is
11369 wkhd = &inodedep->id_bufwait;
11370 for (; adp; adp = listadp) {
11371 listadp = TAILQ_NEXT(adp, ad_next);
11372 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11374 TAILQ_REMOVE(listhead, adp, ad_next);
11375 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11380 * Called from within softdep_disk_write_complete above. This routine
11381 * completes successfully written allocindirs.
11384 handle_allocindir_partdone(aip)
11385 struct allocindir *aip; /* the completed allocindir */
11387 struct indirdep *indirdep;
11389 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11391 indirdep = aip->ai_indirdep;
11392 LIST_REMOVE(aip, ai_next);
11394 * Don't set a pointer while the buffer is undergoing IO or while
11395 * we have active truncations.
11397 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11398 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11401 if (indirdep->ir_state & UFS1FMT)
11402 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11405 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11408 * Await the pointer write before freeing the allocindir.
11410 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11414 * Release segments held on a jwork list.
11418 struct workhead *wkhd;
11420 struct worklist *wk;
11422 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11423 WORKLIST_REMOVE(wk);
11424 switch (wk->wk_type) {
11426 free_jsegdep(WK_JSEGDEP(wk));
11429 free_freedep(WK_FREEDEP(wk));
11432 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11433 WORKITEM_FREE(wk, D_FREEFRAG);
11436 handle_written_freework(WK_FREEWORK(wk));
11439 panic("handle_jwork: Unknown type %s\n",
11440 TYPENAME(wk->wk_type));
11446 * Handle the bufwait list on an inode when it is safe to release items
11447 * held there. This normally happens after an inode block is written but
11448 * may be delayed and handled later if there are pending journal items that
11449 * are not yet safe to be released.
11451 static struct freefile *
11452 handle_bufwait(inodedep, refhd)
11453 struct inodedep *inodedep;
11454 struct workhead *refhd;
11456 struct jaddref *jaddref;
11457 struct freefile *freefile;
11458 struct worklist *wk;
11461 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11462 WORKLIST_REMOVE(wk);
11463 switch (wk->wk_type) {
11466 * We defer adding freefile to the worklist
11467 * until all other additions have been made to
11468 * ensure that it will be done after all the
11469 * old blocks have been freed.
11471 if (freefile != NULL)
11472 panic("handle_bufwait: freefile");
11473 freefile = WK_FREEFILE(wk);
11477 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11481 diradd_inode_written(WK_DIRADD(wk), inodedep);
11485 wk->wk_state |= COMPLETE;
11486 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11487 add_to_worklist(wk, 0);
11491 wk->wk_state |= COMPLETE;
11492 add_to_worklist(wk, 0);
11495 case D_ALLOCDIRECT:
11497 free_newblk(WK_NEWBLK(wk));
11501 wk->wk_state |= COMPLETE;
11502 free_jnewblk(WK_JNEWBLK(wk));
11506 * Save freed journal segments and add references on
11507 * the supplied list which will delay their release
11508 * until the cg bitmap is cleared on disk.
11512 free_jsegdep(WK_JSEGDEP(wk));
11514 WORKLIST_INSERT(refhd, wk);
11518 jaddref = WK_JADDREF(wk);
11519 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11522 * Transfer any jaddrefs to the list to be freed with
11523 * the bitmap if we're handling a removed file.
11525 if (refhd == NULL) {
11526 wk->wk_state |= COMPLETE;
11527 free_jaddref(jaddref);
11529 WORKLIST_INSERT(refhd, wk);
11533 panic("handle_bufwait: Unknown type %p(%s)",
11534 wk, TYPENAME(wk->wk_type));
11541 * Called from within softdep_disk_write_complete above to restore
11542 * in-memory inode block contents to their most up-to-date state. Note
11543 * that this routine is always called from interrupt level with further
11544 * interrupts from this device blocked.
11546 * If the write did not succeed, we will do all the roll-forward
11547 * operations, but we will not take the actions that will allow its
11548 * dependencies to be processed.
11551 handle_written_inodeblock(inodedep, bp, flags)
11552 struct inodedep *inodedep;
11553 struct buf *bp; /* buffer containing the inode block */
11556 struct freefile *freefile;
11557 struct allocdirect *adp, *nextadp;
11558 struct ufs1_dinode *dp1 = NULL;
11559 struct ufs2_dinode *dp2 = NULL;
11560 struct workhead wkhd;
11561 int hadchanges, fstype;
11567 if ((inodedep->id_state & IOSTARTED) == 0)
11568 panic("handle_written_inodeblock: not started");
11569 inodedep->id_state &= ~IOSTARTED;
11570 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11572 dp1 = (struct ufs1_dinode *)bp->b_data +
11573 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11574 freelink = dp1->di_freelink;
11577 dp2 = (struct ufs2_dinode *)bp->b_data +
11578 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11579 freelink = dp2->di_freelink;
11582 * Leave this inodeblock dirty until it's in the list.
11584 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11585 (flags & WRITESUCCEEDED)) {
11586 struct inodedep *inon;
11588 inon = TAILQ_NEXT(inodedep, id_unlinked);
11589 if ((inon == NULL && freelink == 0) ||
11590 (inon && inon->id_ino == freelink)) {
11592 inon->id_state |= UNLINKPREV;
11593 inodedep->id_state |= UNLINKNEXT;
11598 * If we had to rollback the inode allocation because of
11599 * bitmaps being incomplete, then simply restore it.
11600 * Keep the block dirty so that it will not be reclaimed until
11601 * all associated dependencies have been cleared and the
11602 * corresponding updates written to disk.
11604 if (inodedep->id_savedino1 != NULL) {
11606 if (fstype == UFS1)
11607 *dp1 = *inodedep->id_savedino1;
11609 *dp2 = *inodedep->id_savedino2;
11610 free(inodedep->id_savedino1, M_SAVEDINO);
11611 inodedep->id_savedino1 = NULL;
11612 if ((bp->b_flags & B_DELWRI) == 0)
11613 stat_inode_bitmap++;
11616 * If the inode is clear here and GOINGAWAY it will never
11617 * be written. Process the bufwait and clear any pending
11618 * work which may include the freefile.
11620 if (inodedep->id_state & GOINGAWAY)
11624 if (flags & WRITESUCCEEDED)
11625 inodedep->id_state |= COMPLETE;
11627 * Roll forward anything that had to be rolled back before
11628 * the inode could be updated.
11630 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11631 nextadp = TAILQ_NEXT(adp, ad_next);
11632 if (adp->ad_state & ATTACHED)
11633 panic("handle_written_inodeblock: new entry");
11634 if (fstype == UFS1) {
11635 if (adp->ad_offset < UFS_NDADDR) {
11636 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11637 panic("%s %s #%jd mismatch %d != %jd",
11638 "handle_written_inodeblock:",
11640 (intmax_t)adp->ad_offset,
11641 dp1->di_db[adp->ad_offset],
11642 (intmax_t)adp->ad_oldblkno);
11643 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11645 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11647 panic("%s: %s #%jd allocated as %d",
11648 "handle_written_inodeblock",
11649 "indirect pointer",
11650 (intmax_t)adp->ad_offset -
11652 dp1->di_ib[adp->ad_offset -
11654 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11658 if (adp->ad_offset < UFS_NDADDR) {
11659 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11660 panic("%s: %s #%jd %s %jd != %jd",
11661 "handle_written_inodeblock",
11663 (intmax_t)adp->ad_offset, "mismatch",
11664 (intmax_t)dp2->di_db[adp->ad_offset],
11665 (intmax_t)adp->ad_oldblkno);
11666 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11668 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11670 panic("%s: %s #%jd allocated as %jd",
11671 "handle_written_inodeblock",
11672 "indirect pointer",
11673 (intmax_t)adp->ad_offset -
11676 dp2->di_ib[adp->ad_offset -
11678 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11682 adp->ad_state &= ~UNDONE;
11683 adp->ad_state |= ATTACHED;
11686 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11687 nextadp = TAILQ_NEXT(adp, ad_next);
11688 if (adp->ad_state & ATTACHED)
11689 panic("handle_written_inodeblock: new entry");
11690 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11691 panic("%s: direct pointers #%jd %s %jd != %jd",
11692 "handle_written_inodeblock",
11693 (intmax_t)adp->ad_offset, "mismatch",
11694 (intmax_t)dp2->di_extb[adp->ad_offset],
11695 (intmax_t)adp->ad_oldblkno);
11696 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11697 adp->ad_state &= ~UNDONE;
11698 adp->ad_state |= ATTACHED;
11701 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11702 stat_direct_blk_ptrs++;
11704 * Reset the file size to its most up-to-date value.
11706 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11707 panic("handle_written_inodeblock: bad size");
11708 if (inodedep->id_savednlink > UFS_LINK_MAX)
11709 panic("handle_written_inodeblock: Invalid link count "
11710 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11712 if (fstype == UFS1) {
11713 if (dp1->di_nlink != inodedep->id_savednlink) {
11714 dp1->di_nlink = inodedep->id_savednlink;
11717 if (dp1->di_size != inodedep->id_savedsize) {
11718 dp1->di_size = inodedep->id_savedsize;
11722 if (dp2->di_nlink != inodedep->id_savednlink) {
11723 dp2->di_nlink = inodedep->id_savednlink;
11726 if (dp2->di_size != inodedep->id_savedsize) {
11727 dp2->di_size = inodedep->id_savedsize;
11730 if (dp2->di_extsize != inodedep->id_savedextsize) {
11731 dp2->di_extsize = inodedep->id_savedextsize;
11735 inodedep->id_savedsize = -1;
11736 inodedep->id_savedextsize = -1;
11737 inodedep->id_savednlink = -1;
11739 * If there were any rollbacks in the inode block, then it must be
11740 * marked dirty so that its will eventually get written back in
11741 * its correct form.
11744 if (fstype == UFS2)
11745 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
11750 * If the write did not succeed, we have done all the roll-forward
11751 * operations, but we cannot take the actions that will allow its
11752 * dependencies to be processed.
11754 if ((flags & WRITESUCCEEDED) == 0)
11755 return (hadchanges);
11757 * Process any allocdirects that completed during the update.
11759 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11760 handle_allocdirect_partdone(adp, &wkhd);
11761 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11762 handle_allocdirect_partdone(adp, &wkhd);
11764 * Process deallocations that were held pending until the
11765 * inode had been written to disk. Freeing of the inode
11766 * is delayed until after all blocks have been freed to
11767 * avoid creation of new <vfsid, inum, lbn> triples
11768 * before the old ones have been deleted. Completely
11769 * unlinked inodes are not processed until the unlinked
11770 * inode list is written or the last reference is removed.
11772 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11773 freefile = handle_bufwait(inodedep, NULL);
11774 if (freefile && !LIST_EMPTY(&wkhd)) {
11775 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11780 * Move rolled forward dependency completions to the bufwait list
11781 * now that those that were already written have been processed.
11783 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11784 panic("handle_written_inodeblock: bufwait but no changes");
11785 jwork_move(&inodedep->id_bufwait, &wkhd);
11787 if (freefile != NULL) {
11789 * If the inode is goingaway it was never written. Fake up
11790 * the state here so free_inodedep() can succeed.
11792 if (inodedep->id_state & GOINGAWAY)
11793 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11794 if (free_inodedep(inodedep) == 0)
11795 panic("handle_written_inodeblock: live inodedep %p",
11797 add_to_worklist(&freefile->fx_list, 0);
11802 * If no outstanding dependencies, free it.
11804 if (free_inodedep(inodedep) ||
11805 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11806 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11807 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11808 LIST_FIRST(&inodedep->id_bufwait) == 0))
11810 return (hadchanges);
11814 * Perform needed roll-forwards and kick off any dependencies that
11815 * can now be processed.
11817 * If the write did not succeed, we will do all the roll-forward
11818 * operations, but we will not take the actions that will allow its
11819 * dependencies to be processed.
11822 handle_written_indirdep(indirdep, bp, bpp, flags)
11823 struct indirdep *indirdep;
11828 struct allocindir *aip;
11832 if (indirdep->ir_state & GOINGAWAY)
11833 panic("handle_written_indirdep: indirdep gone");
11834 if ((indirdep->ir_state & IOSTARTED) == 0)
11835 panic("handle_written_indirdep: IO not started");
11838 * If there were rollbacks revert them here.
11840 if (indirdep->ir_saveddata) {
11841 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11842 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11843 free(indirdep->ir_saveddata, M_INDIRDEP);
11844 indirdep->ir_saveddata = NULL;
11848 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11849 indirdep->ir_state |= ATTACHED;
11851 * If the write did not succeed, we have done all the roll-forward
11852 * operations, but we cannot take the actions that will allow its
11853 * dependencies to be processed.
11855 if ((flags & WRITESUCCEEDED) == 0) {
11856 stat_indir_blk_ptrs++;
11861 * Move allocindirs with written pointers to the completehd if
11862 * the indirdep's pointer is not yet written. Otherwise
11865 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
11866 LIST_REMOVE(aip, ai_next);
11867 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11868 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11870 newblk_freefrag(&aip->ai_block);
11873 free_newblk(&aip->ai_block);
11876 * Move allocindirs that have finished dependency processing from
11877 * the done list to the write list after updating the pointers.
11879 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11880 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
11881 handle_allocindir_partdone(aip);
11882 if (aip == LIST_FIRST(&indirdep->ir_donehd))
11883 panic("disk_write_complete: not gone");
11888 * Preserve the indirdep if there were any changes or if it is not
11889 * yet valid on disk.
11892 stat_indir_blk_ptrs++;
11897 * If there were no changes we can discard the savedbp and detach
11898 * ourselves from the buf. We are only carrying completed pointers
11901 sbp = indirdep->ir_savebp;
11902 sbp->b_flags |= B_INVAL | B_NOCACHE;
11903 indirdep->ir_savebp = NULL;
11904 indirdep->ir_bp = NULL;
11906 panic("handle_written_indirdep: bp already exists.");
11909 * The indirdep may not be freed until its parent points at it.
11911 if (indirdep->ir_state & DEPCOMPLETE)
11912 free_indirdep(indirdep);
11918 * Process a diradd entry after its dependent inode has been written.
11921 diradd_inode_written(dap, inodedep)
11922 struct diradd *dap;
11923 struct inodedep *inodedep;
11926 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
11927 dap->da_state |= COMPLETE;
11928 complete_diradd(dap);
11929 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
11933 * Returns true if the bmsafemap will have rollbacks when written. Must only
11934 * be called with the per-filesystem lock and the buf lock on the cg held.
11937 bmsafemap_backgroundwrite(bmsafemap, bp)
11938 struct bmsafemap *bmsafemap;
11943 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
11944 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
11945 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
11947 * If we're initiating a background write we need to process the
11948 * rollbacks as they exist now, not as they exist when IO starts.
11949 * No other consumers will look at the contents of the shadowed
11950 * buf so this is safe to do here.
11952 if (bp->b_xflags & BX_BKGRDMARKER)
11953 initiate_write_bmsafemap(bmsafemap, bp);
11959 * Re-apply an allocation when a cg write is complete.
11962 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
11963 struct jnewblk *jnewblk;
11968 ufs1_daddr_t fragno;
11969 ufs2_daddr_t blkno;
11975 cgbno = dtogd(fs, jnewblk->jn_blkno);
11976 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
11977 if (isclr(blksfree, cgbno + i))
11978 panic("jnewblk_rollforward: re-allocated fragment");
11981 if (frags == fs->fs_frag) {
11982 blkno = fragstoblks(fs, cgbno);
11983 ffs_clrblock(fs, blksfree, (long)blkno);
11984 ffs_clusteracct(fs, cgp, blkno, -1);
11985 cgp->cg_cs.cs_nbfree--;
11987 bbase = cgbno - fragnum(fs, cgbno);
11988 cgbno += jnewblk->jn_oldfrags;
11989 /* If a complete block had been reassembled, account for it. */
11990 fragno = fragstoblks(fs, bbase);
11991 if (ffs_isblock(fs, blksfree, fragno)) {
11992 cgp->cg_cs.cs_nffree += fs->fs_frag;
11993 ffs_clusteracct(fs, cgp, fragno, -1);
11994 cgp->cg_cs.cs_nbfree--;
11996 /* Decrement the old frags. */
11997 blk = blkmap(fs, blksfree, bbase);
11998 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11999 /* Allocate the fragment */
12000 for (i = 0; i < frags; i++)
12001 clrbit(blksfree, cgbno + i);
12002 cgp->cg_cs.cs_nffree -= frags;
12003 /* Add back in counts associated with the new frags */
12004 blk = blkmap(fs, blksfree, bbase);
12005 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12011 * Complete a write to a bmsafemap structure. Roll forward any bitmap
12012 * changes if it's not a background write. Set all written dependencies
12013 * to DEPCOMPLETE and free the structure if possible.
12015 * If the write did not succeed, we will do all the roll-forward
12016 * operations, but we will not take the actions that will allow its
12017 * dependencies to be processed.
12020 handle_written_bmsafemap(bmsafemap, bp, flags)
12021 struct bmsafemap *bmsafemap;
12025 struct newblk *newblk;
12026 struct inodedep *inodedep;
12027 struct jaddref *jaddref, *jatmp;
12028 struct jnewblk *jnewblk, *jntmp;
12029 struct ufsmount *ump;
12038 if ((bmsafemap->sm_state & IOSTARTED) == 0)
12039 panic("handle_written_bmsafemap: Not started\n");
12040 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
12042 bmsafemap->sm_state &= ~IOSTARTED;
12043 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
12045 * If write was successful, release journal work that was waiting
12046 * on the write. Otherwise move the work back.
12048 if (flags & WRITESUCCEEDED)
12049 handle_jwork(&bmsafemap->sm_freewr);
12051 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12052 worklist, wk_list);
12055 * Restore unwritten inode allocation pending jaddref writes.
12057 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12058 cgp = (struct cg *)bp->b_data;
12059 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12060 inosused = cg_inosused(cgp);
12061 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12062 ja_bmdeps, jatmp) {
12063 if ((jaddref->ja_state & UNDONE) == 0)
12065 ino = jaddref->ja_ino % fs->fs_ipg;
12066 if (isset(inosused, ino))
12067 panic("handle_written_bmsafemap: "
12068 "re-allocated inode");
12069 /* Do the roll-forward only if it's a real copy. */
12071 if ((jaddref->ja_mode & IFMT) == IFDIR)
12072 cgp->cg_cs.cs_ndir++;
12073 cgp->cg_cs.cs_nifree--;
12074 setbit(inosused, ino);
12077 jaddref->ja_state &= ~UNDONE;
12078 jaddref->ja_state |= ATTACHED;
12079 free_jaddref(jaddref);
12083 * Restore any block allocations which are pending journal writes.
12085 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12086 cgp = (struct cg *)bp->b_data;
12087 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12088 blksfree = cg_blksfree(cgp);
12089 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12091 if ((jnewblk->jn_state & UNDONE) == 0)
12093 /* Do the roll-forward only if it's a real copy. */
12095 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12097 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12098 jnewblk->jn_state |= ATTACHED;
12099 free_jnewblk(jnewblk);
12103 * If the write did not succeed, we have done all the roll-forward
12104 * operations, but we cannot take the actions that will allow its
12105 * dependencies to be processed.
12107 if ((flags & WRITESUCCEEDED) == 0) {
12108 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12110 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12111 worklist, wk_list);
12116 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12117 newblk->nb_state |= DEPCOMPLETE;
12118 newblk->nb_state &= ~ONDEPLIST;
12119 newblk->nb_bmsafemap = NULL;
12120 LIST_REMOVE(newblk, nb_deps);
12121 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12122 handle_allocdirect_partdone(
12123 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12124 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12125 handle_allocindir_partdone(
12126 WK_ALLOCINDIR(&newblk->nb_list));
12127 else if (newblk->nb_list.wk_type != D_NEWBLK)
12128 panic("handle_written_bmsafemap: Unexpected type: %s",
12129 TYPENAME(newblk->nb_list.wk_type));
12131 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12132 inodedep->id_state |= DEPCOMPLETE;
12133 inodedep->id_state &= ~ONDEPLIST;
12134 LIST_REMOVE(inodedep, id_deps);
12135 inodedep->id_bmsafemap = NULL;
12137 LIST_REMOVE(bmsafemap, sm_next);
12138 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12139 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12140 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12141 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12142 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12143 LIST_REMOVE(bmsafemap, sm_hash);
12144 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12147 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12154 * Try to free a mkdir dependency.
12157 complete_mkdir(mkdir)
12158 struct mkdir *mkdir;
12160 struct diradd *dap;
12162 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12164 LIST_REMOVE(mkdir, md_mkdirs);
12165 dap = mkdir->md_diradd;
12166 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12167 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12168 dap->da_state |= DEPCOMPLETE;
12169 complete_diradd(dap);
12171 WORKITEM_FREE(mkdir, D_MKDIR);
12175 * Handle the completion of a mkdir dependency.
12178 handle_written_mkdir(mkdir, type)
12179 struct mkdir *mkdir;
12183 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12184 panic("handle_written_mkdir: bad type");
12185 mkdir->md_state |= COMPLETE;
12186 complete_mkdir(mkdir);
12190 free_pagedep(pagedep)
12191 struct pagedep *pagedep;
12195 if (pagedep->pd_state & NEWBLOCK)
12197 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12199 for (i = 0; i < DAHASHSZ; i++)
12200 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12202 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12204 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12206 if (pagedep->pd_state & ONWORKLIST)
12207 WORKLIST_REMOVE(&pagedep->pd_list);
12208 LIST_REMOVE(pagedep, pd_hash);
12209 WORKITEM_FREE(pagedep, D_PAGEDEP);
12215 * Called from within softdep_disk_write_complete above.
12216 * A write operation was just completed. Removed inodes can
12217 * now be freed and associated block pointers may be committed.
12218 * Note that this routine is always called from interrupt level
12219 * with further interrupts from this device blocked.
12221 * If the write did not succeed, we will do all the roll-forward
12222 * operations, but we will not take the actions that will allow its
12223 * dependencies to be processed.
12226 handle_written_filepage(pagedep, bp, flags)
12227 struct pagedep *pagedep;
12228 struct buf *bp; /* buffer containing the written page */
12231 struct dirrem *dirrem;
12232 struct diradd *dap, *nextdap;
12236 if ((pagedep->pd_state & IOSTARTED) == 0)
12237 panic("handle_written_filepage: not started");
12238 pagedep->pd_state &= ~IOSTARTED;
12239 if ((flags & WRITESUCCEEDED) == 0)
12242 * Process any directory removals that have been committed.
12244 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12245 LIST_REMOVE(dirrem, dm_next);
12246 dirrem->dm_state |= COMPLETE;
12247 dirrem->dm_dirinum = pagedep->pd_ino;
12248 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12249 ("handle_written_filepage: Journal entries not written."));
12250 add_to_worklist(&dirrem->dm_list, 0);
12253 * Free any directory additions that have been committed.
12254 * If it is a newly allocated block, we have to wait until
12255 * the on-disk directory inode claims the new block.
12257 if ((pagedep->pd_state & NEWBLOCK) == 0)
12258 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12259 free_diradd(dap, NULL);
12262 * Uncommitted directory entries must be restored.
12264 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12265 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12267 nextdap = LIST_NEXT(dap, da_pdlist);
12268 if (dap->da_state & ATTACHED)
12269 panic("handle_written_filepage: attached");
12270 ep = (struct direct *)
12271 ((char *)bp->b_data + dap->da_offset);
12272 ep->d_ino = dap->da_newinum;
12273 dap->da_state &= ~UNDONE;
12274 dap->da_state |= ATTACHED;
12277 * If the inode referenced by the directory has
12278 * been written out, then the dependency can be
12279 * moved to the pending list.
12281 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12282 LIST_REMOVE(dap, da_pdlist);
12283 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12289 * If there were any rollbacks in the directory, then it must be
12290 * marked dirty so that its will eventually get written back in
12291 * its correct form.
12293 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12294 if ((bp->b_flags & B_DELWRI) == 0)
12300 * If we are not waiting for a new directory block to be
12301 * claimed by its inode, then the pagedep will be freed.
12302 * Otherwise it will remain to track any new entries on
12303 * the page in case they are fsync'ed.
12305 free_pagedep(pagedep);
12310 * Writing back in-core inode structures.
12312 * The filesystem only accesses an inode's contents when it occupies an
12313 * "in-core" inode structure. These "in-core" structures are separate from
12314 * the page frames used to cache inode blocks. Only the latter are
12315 * transferred to/from the disk. So, when the updated contents of the
12316 * "in-core" inode structure are copied to the corresponding in-memory inode
12317 * block, the dependencies are also transferred. The following procedure is
12318 * called when copying a dirty "in-core" inode to a cached inode block.
12322 * Called when an inode is loaded from disk. If the effective link count
12323 * differed from the actual link count when it was last flushed, then we
12324 * need to ensure that the correct effective link count is put back.
12327 softdep_load_inodeblock(ip)
12328 struct inode *ip; /* the "in_core" copy of the inode */
12330 struct inodedep *inodedep;
12331 struct ufsmount *ump;
12334 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12335 ("softdep_load_inodeblock called on non-softdep filesystem"));
12337 * Check for alternate nlink count.
12339 ip->i_effnlink = ip->i_nlink;
12341 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12345 if (ip->i_nlink != inodedep->id_nlinkwrote &&
12346 inodedep->id_nlinkwrote != -1) {
12347 KASSERT(ip->i_nlink == 0 &&
12348 (ump->um_flags & UM_FSFAIL_CLEANUP) != 0,
12349 ("read bad i_nlink value"));
12350 ip->i_effnlink = ip->i_nlink = inodedep->id_nlinkwrote;
12352 ip->i_effnlink -= inodedep->id_nlinkdelta;
12353 KASSERT(ip->i_effnlink >= 0,
12354 ("softdep_load_inodeblock: negative i_effnlink"));
12359 * This routine is called just before the "in-core" inode
12360 * information is to be copied to the in-memory inode block.
12361 * Recall that an inode block contains several inodes. If
12362 * the force flag is set, then the dependencies will be
12363 * cleared so that the update can always be made. Note that
12364 * the buffer is locked when this routine is called, so we
12365 * will never be in the middle of writing the inode block
12369 softdep_update_inodeblock(ip, bp, waitfor)
12370 struct inode *ip; /* the "in_core" copy of the inode */
12371 struct buf *bp; /* the buffer containing the inode block */
12372 int waitfor; /* nonzero => update must be allowed */
12374 struct inodedep *inodedep;
12375 struct inoref *inoref;
12376 struct ufsmount *ump;
12377 struct worklist *wk;
12384 mp = UFSTOVFS(ump);
12385 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12386 ("softdep_update_inodeblock called on non-softdep filesystem"));
12389 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12390 * does not have access to the in-core ip so must write directly into
12391 * the inode block buffer when setting freelink.
12393 if (fs->fs_magic == FS_UFS1_MAGIC)
12394 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12395 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12397 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12398 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12400 * If the effective link count is not equal to the actual link
12401 * count, then we must track the difference in an inodedep while
12402 * the inode is (potentially) tossed out of the cache. Otherwise,
12403 * if there is no existing inodedep, then there are no dependencies
12408 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12410 if (ip->i_effnlink != ip->i_nlink)
12411 panic("softdep_update_inodeblock: bad link count");
12414 KASSERT(ip->i_nlink >= inodedep->id_nlinkdelta,
12415 ("softdep_update_inodeblock inconsistent ip %p i_nlink %d "
12416 "inodedep %p id_nlinkdelta %jd",
12417 ip, ip->i_nlink, inodedep, (intmax_t)inodedep->id_nlinkdelta));
12418 inodedep->id_nlinkwrote = ip->i_nlink;
12419 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12420 panic("softdep_update_inodeblock: bad delta");
12422 * If we're flushing all dependencies we must also move any waiting
12423 * for journal writes onto the bufwait list prior to I/O.
12426 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12427 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12429 jwait(&inoref->if_list, MNT_WAIT);
12435 * Changes have been initiated. Anything depending on these
12436 * changes cannot occur until this inode has been written.
12438 inodedep->id_state &= ~COMPLETE;
12439 if ((inodedep->id_state & ONWORKLIST) == 0)
12440 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12442 * Any new dependencies associated with the incore inode must
12443 * now be moved to the list associated with the buffer holding
12444 * the in-memory copy of the inode. Once merged process any
12445 * allocdirects that are completed by the merger.
12447 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12448 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12449 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12451 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12452 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12453 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12456 * Now that the inode has been pushed into the buffer, the
12457 * operations dependent on the inode being written to disk
12458 * can be moved to the id_bufwait so that they will be
12459 * processed when the buffer I/O completes.
12461 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12462 WORKLIST_REMOVE(wk);
12463 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12466 * Newly allocated inodes cannot be written until the bitmap
12467 * that allocates them have been written (indicated by
12468 * DEPCOMPLETE being set in id_state). If we are doing a
12469 * forced sync (e.g., an fsync on a file), we force the bitmap
12470 * to be written so that the update can be done.
12472 if (waitfor == 0) {
12477 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12481 ibp = inodedep->id_bmsafemap->sm_buf;
12482 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12485 * If ibp came back as NULL, the dependency could have been
12486 * freed while we slept. Look it up again, and check to see
12487 * that it has completed.
12489 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12495 if ((error = bwrite(ibp)) != 0)
12496 softdep_error("softdep_update_inodeblock: bwrite", error);
12500 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12501 * old inode dependency list (such as id_inoupdt).
12504 merge_inode_lists(newlisthead, oldlisthead)
12505 struct allocdirectlst *newlisthead;
12506 struct allocdirectlst *oldlisthead;
12508 struct allocdirect *listadp, *newadp;
12510 newadp = TAILQ_FIRST(newlisthead);
12511 if (newadp != NULL)
12512 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12513 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12514 if (listadp->ad_offset < newadp->ad_offset) {
12515 listadp = TAILQ_NEXT(listadp, ad_next);
12518 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12519 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12520 if (listadp->ad_offset == newadp->ad_offset) {
12521 allocdirect_merge(oldlisthead, newadp,
12525 newadp = TAILQ_FIRST(newlisthead);
12527 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12528 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12529 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12534 * If we are doing an fsync, then we must ensure that any directory
12535 * entries for the inode have been written after the inode gets to disk.
12539 struct vnode *vp; /* the "in_core" copy of the inode */
12541 struct inodedep *inodedep;
12542 struct pagedep *pagedep;
12543 struct inoref *inoref;
12544 struct ufsmount *ump;
12545 struct worklist *wk;
12546 struct diradd *dap;
12552 struct thread *td = curthread;
12553 int error, flushparent, pagedep_new_block;
12559 ump = VFSTOUFS(mp);
12561 if (MOUNTEDSOFTDEP(mp) == 0)
12565 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12569 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12570 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12572 jwait(&inoref->if_list, MNT_WAIT);
12576 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12577 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12578 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12579 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12580 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12581 panic("softdep_fsync: pending ops %p", inodedep);
12582 for (error = 0, flushparent = 0; ; ) {
12583 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12585 if (wk->wk_type != D_DIRADD)
12586 panic("softdep_fsync: Unexpected type %s",
12587 TYPENAME(wk->wk_type));
12588 dap = WK_DIRADD(wk);
12590 * Flush our parent if this directory entry has a MKDIR_PARENT
12591 * dependency or is contained in a newly allocated block.
12593 if (dap->da_state & DIRCHG)
12594 pagedep = dap->da_previous->dm_pagedep;
12596 pagedep = dap->da_pagedep;
12597 parentino = pagedep->pd_ino;
12598 lbn = pagedep->pd_lbn;
12599 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12600 panic("softdep_fsync: dirty");
12601 if ((dap->da_state & MKDIR_PARENT) ||
12602 (pagedep->pd_state & NEWBLOCK))
12607 * If we are being fsync'ed as part of vgone'ing this vnode,
12608 * then we will not be able to release and recover the
12609 * vnode below, so we just have to give up on writing its
12610 * directory entry out. It will eventually be written, just
12611 * not now, but then the user was not asking to have it
12612 * written, so we are not breaking any promises.
12614 if (VN_IS_DOOMED(vp))
12617 * We prevent deadlock by always fetching inodes from the
12618 * root, moving down the directory tree. Thus, when fetching
12619 * our parent directory, we first try to get the lock. If
12620 * that fails, we must unlock ourselves before requesting
12621 * the lock on our parent. See the comment in ufs_lookup
12622 * for details on possible races.
12625 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
12626 FFSV_FORCEINSMQ)) {
12628 * Unmount cannot proceed after unlock because
12629 * caller must have called vn_start_write().
12632 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
12633 &pvp, FFSV_FORCEINSMQ);
12634 MPASS(VTOI(pvp)->i_mode != 0);
12635 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12636 if (VN_IS_DOOMED(vp)) {
12645 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12646 * that are contained in direct blocks will be resolved by
12647 * doing a ffs_update. Pagedeps contained in indirect blocks
12648 * may require a complete sync'ing of the directory. So, we
12649 * try the cheap and fast ffs_update first, and if that fails,
12650 * then we do the slower ffs_syncvnode of the directory.
12655 if ((error = ffs_update(pvp, 1)) != 0) {
12661 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12662 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12663 if (wk->wk_type != D_DIRADD)
12664 panic("softdep_fsync: Unexpected type %s",
12665 TYPENAME(wk->wk_type));
12666 dap = WK_DIRADD(wk);
12667 if (dap->da_state & DIRCHG)
12668 pagedep = dap->da_previous->dm_pagedep;
12670 pagedep = dap->da_pagedep;
12671 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12674 if (pagedep_new_block && (error =
12675 ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12685 * Flush directory page containing the inode's name.
12687 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12690 error = bwrite(bp);
12694 if (!ffs_fsfail_cleanup(ump, error))
12697 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12705 * Flush all the dirty bitmaps associated with the block device
12706 * before flushing the rest of the dirty blocks so as to reduce
12707 * the number of dependencies that will have to be rolled back.
12712 softdep_fsync_mountdev(vp)
12715 struct buf *bp, *nbp;
12716 struct worklist *wk;
12719 if (!vn_isdisk(vp))
12720 panic("softdep_fsync_mountdev: vnode not a disk");
12721 bo = &vp->v_bufobj;
12724 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12726 * If it is already scheduled, skip to the next buffer.
12728 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12731 if ((bp->b_flags & B_DELWRI) == 0)
12732 panic("softdep_fsync_mountdev: not dirty");
12734 * We are only interested in bitmaps with outstanding
12737 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12738 wk->wk_type != D_BMSAFEMAP ||
12739 (bp->b_vflags & BV_BKGRDINPROG)) {
12745 (void) bawrite(bp);
12753 * Sync all cylinder groups that were dirty at the time this function is
12754 * called. Newly dirtied cgs will be inserted before the sentinel. This
12755 * is used to flush freedep activity that may be holding up writes to a
12759 sync_cgs(mp, waitfor)
12763 struct bmsafemap *bmsafemap;
12764 struct bmsafemap *sentinel;
12765 struct ufsmount *ump;
12769 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12770 sentinel->sm_cg = -1;
12771 ump = VFSTOUFS(mp);
12774 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12775 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12776 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12777 /* Skip sentinels and cgs with no work to release. */
12778 if (bmsafemap->sm_cg == -1 ||
12779 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12780 LIST_EMPTY(&bmsafemap->sm_freewr))) {
12781 LIST_REMOVE(sentinel, sm_next);
12782 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12786 * If we don't get the lock and we're waiting try again, if
12787 * not move on to the next buf and try to sync it.
12789 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12790 if (bp == NULL && waitfor == MNT_WAIT)
12792 LIST_REMOVE(sentinel, sm_next);
12793 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12797 if (waitfor == MNT_NOWAIT)
12800 error = bwrite(bp);
12805 LIST_REMOVE(sentinel, sm_next);
12807 free(sentinel, M_BMSAFEMAP);
12812 * This routine is called when we are trying to synchronously flush a
12813 * file. This routine must eliminate any filesystem metadata dependencies
12814 * so that the syncing routine can succeed.
12817 softdep_sync_metadata(struct vnode *vp)
12823 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12824 ("softdep_sync_metadata called on non-softdep filesystem"));
12826 * Ensure that any direct block dependencies have been cleared,
12827 * truncations are started, and inode references are journaled.
12829 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
12831 * Write all journal records to prevent rollbacks on devvp.
12833 if (vp->v_type == VCHR)
12834 softdep_flushjournal(vp->v_mount);
12835 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12837 * Ensure that all truncates are written so we won't find deps on
12840 process_truncates(vp);
12841 FREE_LOCK(VFSTOUFS(vp->v_mount));
12847 * This routine is called when we are attempting to sync a buf with
12848 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
12849 * other IO it can but returns EBUSY if the buffer is not yet able to
12850 * be written. Dependencies which will not cause rollbacks will always
12854 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
12856 struct indirdep *indirdep;
12857 struct pagedep *pagedep;
12858 struct allocindir *aip;
12859 struct newblk *newblk;
12860 struct ufsmount *ump;
12862 struct worklist *wk;
12865 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12866 ("softdep_sync_buf called on non-softdep filesystem"));
12868 * For VCHR we just don't want to force flush any dependencies that
12869 * will cause rollbacks.
12871 if (vp->v_type == VCHR) {
12872 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12876 ump = VFSTOUFS(vp->v_mount);
12879 * As we hold the buffer locked, none of its dependencies
12884 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12885 switch (wk->wk_type) {
12886 case D_ALLOCDIRECT:
12888 newblk = WK_NEWBLK(wk);
12889 if (newblk->nb_jnewblk != NULL) {
12890 if (waitfor == MNT_NOWAIT) {
12894 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
12897 if (newblk->nb_state & DEPCOMPLETE ||
12898 waitfor == MNT_NOWAIT)
12900 nbp = newblk->nb_bmsafemap->sm_buf;
12901 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12905 if ((error = bwrite(nbp)) != 0)
12911 indirdep = WK_INDIRDEP(wk);
12912 if (waitfor == MNT_NOWAIT) {
12913 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
12914 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
12919 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
12920 panic("softdep_sync_buf: truncation pending.");
12922 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
12923 newblk = (struct newblk *)aip;
12924 if (newblk->nb_jnewblk != NULL) {
12925 jwait(&newblk->nb_jnewblk->jn_list,
12929 if (newblk->nb_state & DEPCOMPLETE)
12931 nbp = newblk->nb_bmsafemap->sm_buf;
12932 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12936 if ((error = bwrite(nbp)) != 0)
12945 * Only flush directory entries in synchronous passes.
12947 if (waitfor != MNT_WAIT) {
12952 * While syncing snapshots, we must allow recursive
12957 * We are trying to sync a directory that may
12958 * have dependencies on both its own metadata
12959 * and/or dependencies on the inodes of any
12960 * recently allocated files. We walk its diradd
12961 * lists pushing out the associated inode.
12963 pagedep = WK_PAGEDEP(wk);
12964 for (i = 0; i < DAHASHSZ; i++) {
12965 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
12967 if ((error = flush_pagedep_deps(vp, wk->wk_mp,
12968 &pagedep->pd_diraddhd[i]))) {
12983 panic("softdep_sync_buf: Unknown type %s",
12984 TYPENAME(wk->wk_type));
12995 * Flush the dependencies associated with an inodedep.
12998 flush_inodedep_deps(vp, mp, ino)
13003 struct inodedep *inodedep;
13004 struct inoref *inoref;
13005 struct ufsmount *ump;
13006 int error, waitfor;
13009 * This work is done in two passes. The first pass grabs most
13010 * of the buffers and begins asynchronously writing them. The
13011 * only way to wait for these asynchronous writes is to sleep
13012 * on the filesystem vnode which may stay busy for a long time
13013 * if the filesystem is active. So, instead, we make a second
13014 * pass over the dependencies blocking on each write. In the
13015 * usual case we will be blocking against a write that we
13016 * initiated, so when it is done the dependency will have been
13017 * resolved. Thus the second pass is expected to end quickly.
13018 * We give a brief window at the top of the loop to allow
13019 * any pending I/O to complete.
13021 ump = VFSTOUFS(mp);
13023 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
13029 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13031 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13032 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13034 jwait(&inoref->if_list, MNT_WAIT);
13038 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
13039 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
13040 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
13041 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
13044 * If pass2, we are done, otherwise do pass 2.
13046 if (waitfor == MNT_WAIT)
13048 waitfor = MNT_WAIT;
13051 * Try freeing inodedep in case all dependencies have been removed.
13053 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
13054 (void) free_inodedep(inodedep);
13059 * Flush an inode dependency list.
13062 flush_deplist(listhead, waitfor, errorp)
13063 struct allocdirectlst *listhead;
13067 struct allocdirect *adp;
13068 struct newblk *newblk;
13069 struct ufsmount *ump;
13072 if ((adp = TAILQ_FIRST(listhead)) == NULL)
13074 ump = VFSTOUFS(adp->ad_list.wk_mp);
13076 TAILQ_FOREACH(adp, listhead, ad_next) {
13077 newblk = (struct newblk *)adp;
13078 if (newblk->nb_jnewblk != NULL) {
13079 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13082 if (newblk->nb_state & DEPCOMPLETE)
13084 bp = newblk->nb_bmsafemap->sm_buf;
13085 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13087 if (waitfor == MNT_NOWAIT)
13092 if (waitfor == MNT_NOWAIT)
13095 *errorp = bwrite(bp);
13103 * Flush dependencies associated with an allocdirect block.
13106 flush_newblk_dep(vp, mp, lbn)
13111 struct newblk *newblk;
13112 struct ufsmount *ump;
13116 ufs2_daddr_t blkno;
13120 bo = &vp->v_bufobj;
13122 blkno = DIP(ip, i_db[lbn]);
13124 panic("flush_newblk_dep: Missing block");
13125 ump = VFSTOUFS(mp);
13128 * Loop until all dependencies related to this block are satisfied.
13129 * We must be careful to restart after each sleep in case a write
13130 * completes some part of this process for us.
13133 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13137 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13138 panic("flush_newblk_dep: Bad newblk %p", newblk);
13140 * Flush the journal.
13142 if (newblk->nb_jnewblk != NULL) {
13143 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13147 * Write the bitmap dependency.
13149 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13150 bp = newblk->nb_bmsafemap->sm_buf;
13151 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13155 error = bwrite(bp);
13162 * Write the buffer.
13166 bp = gbincore(bo, lbn);
13168 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13169 LK_INTERLOCK, BO_LOCKPTR(bo));
13170 if (error == ENOLCK) {
13173 continue; /* Slept, retry */
13176 break; /* Failed */
13177 if (bp->b_flags & B_DELWRI) {
13179 error = bwrite(bp);
13187 * We have to wait for the direct pointers to
13188 * point at the newdirblk before the dependency
13191 error = ffs_update(vp, 1);
13200 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13203 flush_pagedep_deps(pvp, mp, diraddhdp)
13206 struct diraddhd *diraddhdp;
13208 struct inodedep *inodedep;
13209 struct inoref *inoref;
13210 struct ufsmount *ump;
13211 struct diradd *dap;
13216 struct diraddhd unfinished;
13218 LIST_INIT(&unfinished);
13219 ump = VFSTOUFS(mp);
13222 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13224 * Flush ourselves if this directory entry
13225 * has a MKDIR_PARENT dependency.
13227 if (dap->da_state & MKDIR_PARENT) {
13229 if ((error = ffs_update(pvp, 1)) != 0)
13233 * If that cleared dependencies, go on to next.
13235 if (dap != LIST_FIRST(diraddhdp))
13238 * All MKDIR_PARENT dependencies and all the
13239 * NEWBLOCK pagedeps that are contained in direct
13240 * blocks were resolved by doing above ffs_update.
13241 * Pagedeps contained in indirect blocks may
13242 * require a complete sync'ing of the directory.
13243 * We are in the midst of doing a complete sync,
13244 * so if they are not resolved in this pass we
13245 * defer them for now as they will be sync'ed by
13246 * our caller shortly.
13248 LIST_REMOVE(dap, da_pdlist);
13249 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13253 * A newly allocated directory must have its "." and
13254 * ".." entries written out before its name can be
13255 * committed in its parent.
13257 inum = dap->da_newinum;
13258 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13259 panic("flush_pagedep_deps: lost inode1");
13261 * Wait for any pending journal adds to complete so we don't
13262 * cause rollbacks while syncing.
13264 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13265 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13267 jwait(&inoref->if_list, MNT_WAIT);
13271 if (dap->da_state & MKDIR_BODY) {
13273 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13276 MPASS(VTOI(vp)->i_mode != 0);
13277 error = flush_newblk_dep(vp, mp, 0);
13279 * If we still have the dependency we might need to
13280 * update the vnode to sync the new link count to
13283 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13284 error = ffs_update(vp, 1);
13290 * If that cleared dependencies, go on to next.
13292 if (dap != LIST_FIRST(diraddhdp))
13294 if (dap->da_state & MKDIR_BODY) {
13295 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13297 panic("flush_pagedep_deps: MKDIR_BODY "
13298 "inodedep %p dap %p vp %p",
13299 inodedep, dap, vp);
13303 * Flush the inode on which the directory entry depends.
13304 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13305 * the only remaining dependency is that the updated inode
13306 * count must get pushed to disk. The inode has already
13307 * been pushed into its inode buffer (via VOP_UPDATE) at
13308 * the time of the reference count change. So we need only
13309 * locate that buffer, ensure that there will be no rollback
13310 * caused by a bitmap dependency, then write the inode buffer.
13313 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13314 panic("flush_pagedep_deps: lost inode");
13316 * If the inode still has bitmap dependencies,
13317 * push them to disk.
13319 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13320 bp = inodedep->id_bmsafemap->sm_buf;
13321 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13325 if ((error = bwrite(bp)) != 0)
13328 if (dap != LIST_FIRST(diraddhdp))
13332 * If the inode is still sitting in a buffer waiting
13333 * to be written or waiting for the link count to be
13334 * adjusted update it here to flush it to disk.
13336 if (dap == LIST_FIRST(diraddhdp)) {
13338 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13341 MPASS(VTOI(vp)->i_mode != 0);
13342 error = ffs_update(vp, 1);
13349 * If we have failed to get rid of all the dependencies
13350 * then something is seriously wrong.
13352 if (dap == LIST_FIRST(diraddhdp)) {
13353 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13354 panic("flush_pagedep_deps: failed to flush "
13355 "inodedep %p ino %ju dap %p",
13356 inodedep, (uintmax_t)inum, dap);
13361 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13362 LIST_REMOVE(dap, da_pdlist);
13363 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13369 * A large burst of file addition or deletion activity can drive the
13370 * memory load excessively high. First attempt to slow things down
13371 * using the techniques below. If that fails, this routine requests
13372 * the offending operations to fall back to running synchronously
13373 * until the memory load returns to a reasonable level.
13376 softdep_slowdown(vp)
13379 struct ufsmount *ump;
13381 int max_softdeps_hard;
13383 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13384 ("softdep_slowdown called on non-softdep filesystem"));
13385 ump = VFSTOUFS(vp->v_mount);
13389 * Check for journal space if needed.
13391 if (DOINGSUJ(vp)) {
13392 if (journal_space(ump, 0) == 0)
13396 * If the system is under its limits and our filesystem is
13397 * not responsible for more than our share of the usage and
13398 * we are not low on journal space, then no need to slow down.
13400 max_softdeps_hard = max_softdeps * 11 / 10;
13401 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13402 dep_current[D_INODEDEP] < max_softdeps_hard &&
13403 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13404 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13405 ump->softdep_curdeps[D_DIRREM] <
13406 (max_softdeps_hard / 2) / stat_flush_threads &&
13407 ump->softdep_curdeps[D_INODEDEP] <
13408 max_softdeps_hard / stat_flush_threads &&
13409 ump->softdep_curdeps[D_INDIRDEP] <
13410 (max_softdeps_hard / 1000) / stat_flush_threads &&
13411 ump->softdep_curdeps[D_FREEBLKS] <
13412 max_softdeps_hard / stat_flush_threads) {
13417 * If the journal is low or our filesystem is over its limit
13418 * then speedup the cleanup.
13420 if (ump->softdep_curdeps[D_INDIRDEP] <
13421 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13422 softdep_speedup(ump);
13423 stat_sync_limit_hit += 1;
13426 * We only slow down the rate at which new dependencies are
13427 * generated if we are not using journaling. With journaling,
13428 * the cleanup should always be sufficient to keep things
13437 * Called by the allocation routines when they are about to fail
13438 * in the hope that we can free up the requested resource (inodes
13441 * First check to see if the work list has anything on it. If it has,
13442 * clean up entries until we successfully free the requested resource.
13443 * Because this process holds inodes locked, we cannot handle any remove
13444 * requests that might block on a locked inode as that could lead to
13445 * deadlock. If the worklist yields none of the requested resource,
13446 * start syncing out vnodes to free up the needed space.
13449 softdep_request_cleanup(fs, vp, cred, resource)
13452 struct ucred *cred;
13455 struct ufsmount *ump;
13458 ufs2_daddr_t needed;
13459 int error, failed_vnode;
13462 * If we are being called because of a process doing a
13463 * copy-on-write, then it is not safe to process any
13464 * worklist items as we will recurse into the copyonwrite
13465 * routine. This will result in an incoherent snapshot.
13466 * If the vnode that we hold is a snapshot, we must avoid
13467 * handling other resources that could cause deadlock.
13469 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13472 if (resource == FLUSH_BLOCKS_WAIT)
13473 stat_cleanup_blkrequests += 1;
13475 stat_cleanup_inorequests += 1;
13478 ump = VFSTOUFS(mp);
13479 mtx_assert(UFS_MTX(ump), MA_OWNED);
13481 error = ffs_update(vp, 1);
13482 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13487 * If we are in need of resources, start by cleaning up
13488 * any block removals associated with our inode.
13491 process_removes(vp);
13492 process_truncates(vp);
13495 * Now clean up at least as many resources as we will need.
13497 * When requested to clean up inodes, the number that are needed
13498 * is set by the number of simultaneous writers (mnt_writeopcount)
13499 * plus a bit of slop (2) in case some more writers show up while
13502 * When requested to free up space, the amount of space that
13503 * we need is enough blocks to allocate a full-sized segment
13504 * (fs_contigsumsize). The number of such segments that will
13505 * be needed is set by the number of simultaneous writers
13506 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13507 * writers show up while we are cleaning.
13509 * Additionally, if we are unpriviledged and allocating space,
13510 * we need to ensure that we clean up enough blocks to get the
13511 * needed number of blocks over the threshold of the minimum
13512 * number of blocks required to be kept free by the filesystem
13515 if (resource == FLUSH_INODES_WAIT) {
13516 needed = vfs_mount_fetch_counter(vp->v_mount,
13517 MNT_COUNT_WRITEOPCOUNT) + 2;
13518 } else if (resource == FLUSH_BLOCKS_WAIT) {
13519 needed = (vfs_mount_fetch_counter(vp->v_mount,
13520 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13521 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13522 needed += fragstoblks(fs,
13523 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13524 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13526 printf("softdep_request_cleanup: Unknown resource type %d\n",
13531 starttime = time_second;
13533 if (resource == FLUSH_BLOCKS_WAIT &&
13534 fs->fs_cstotal.cs_nbfree <= needed)
13535 softdep_send_speedup(ump, needed * fs->fs_bsize,
13537 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13538 fs->fs_cstotal.cs_nbfree <= needed) ||
13539 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13540 fs->fs_cstotal.cs_nifree <= needed)) {
13542 if (ump->softdep_on_worklist > 0 &&
13543 process_worklist_item(UFSTOVFS(ump),
13544 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13545 stat_worklist_push += 1;
13549 * If we still need resources and there are no more worklist
13550 * entries to process to obtain them, we have to start flushing
13551 * the dirty vnodes to force the release of additional requests
13552 * to the worklist that we can then process to reap addition
13553 * resources. We walk the vnodes associated with the mount point
13554 * until we get the needed worklist requests that we can reap.
13556 * If there are several threads all needing to clean the same
13557 * mount point, only one is allowed to walk the mount list.
13558 * When several threads all try to walk the same mount list,
13559 * they end up competing with each other and often end up in
13560 * livelock. This approach ensures that forward progress is
13561 * made at the cost of occational ENOSPC errors being returned
13562 * that might otherwise have been avoided.
13565 if ((resource == FLUSH_BLOCKS_WAIT &&
13566 fs->fs_cstotal.cs_nbfree <= needed) ||
13567 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13568 fs->fs_cstotal.cs_nifree <= needed)) {
13570 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13571 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13573 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13575 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13577 if (ump->softdep_on_worklist > 0) {
13578 stat_cleanup_retries += 1;
13586 stat_cleanup_failures += 1;
13588 if (time_second - starttime > stat_cleanup_high_delay)
13589 stat_cleanup_high_delay = time_second - starttime;
13595 * Scan the vnodes for the specified mount point flushing out any
13596 * vnodes that can be locked without waiting. Finally, try to flush
13597 * the device associated with the mount point if it can be locked
13600 * We return 0 if we were able to lock every vnode in our scan.
13601 * If we had to skip one or more vnodes, we return 1.
13604 softdep_request_cleanup_flush(mp, ump)
13606 struct ufsmount *ump;
13609 struct vnode *lvp, *mvp;
13614 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13615 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13619 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT) != 0) {
13623 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
13627 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13630 lvp = ump->um_devvp;
13631 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13632 VOP_FSYNC(lvp, MNT_NOWAIT, td);
13635 return (failed_vnode);
13639 softdep_excess_items(struct ufsmount *ump, int item)
13642 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13643 return (dep_current[item] > max_softdeps &&
13644 ump->softdep_curdeps[item] > max_softdeps /
13645 stat_flush_threads);
13649 schedule_cleanup(struct mount *mp)
13651 struct ufsmount *ump;
13654 ump = VFSTOUFS(mp);
13658 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13659 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13661 * No ast is delivered to kernel threads, so nobody
13662 * would deref the mp. Some kernel threads
13663 * explicitely check for AST, e.g. NFS daemon does
13664 * this in the serving loop.
13668 if (td->td_su != NULL)
13669 vfs_rel(td->td_su);
13673 td->td_flags |= TDF_ASTPENDING;
13678 softdep_ast_cleanup_proc(struct thread *td)
13681 struct ufsmount *ump;
13685 while ((mp = td->td_su) != NULL) {
13687 error = vfs_busy(mp, MBF_NOWAIT);
13691 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13692 ump = VFSTOUFS(mp);
13696 if (softdep_excess_items(ump, D_INODEDEP)) {
13698 request_cleanup(mp, FLUSH_INODES);
13700 if (softdep_excess_items(ump, D_DIRREM)) {
13702 request_cleanup(mp, FLUSH_BLOCKS);
13705 if (softdep_excess_items(ump, D_NEWBLK) ||
13706 softdep_excess_items(ump, D_ALLOCDIRECT) ||
13707 softdep_excess_items(ump, D_ALLOCINDIR)) {
13708 error = vn_start_write(NULL, &mp,
13712 VFS_SYNC(mp, MNT_WAIT);
13713 vn_finished_write(mp);
13716 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13722 if ((mp = td->td_su) != NULL) {
13729 * If memory utilization has gotten too high, deliberately slow things
13730 * down and speed up the I/O processing.
13733 request_cleanup(mp, resource)
13737 struct thread *td = curthread;
13738 struct ufsmount *ump;
13740 ump = VFSTOUFS(mp);
13743 * We never hold up the filesystem syncer or buf daemon.
13745 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13748 * First check to see if the work list has gotten backlogged.
13749 * If it has, co-opt this process to help clean up two entries.
13750 * Because this process may hold inodes locked, we cannot
13751 * handle any remove requests that might block on a locked
13752 * inode as that could lead to deadlock. We set TDP_SOFTDEP
13753 * to avoid recursively processing the worklist.
13755 if (ump->softdep_on_worklist > max_softdeps / 10) {
13756 td->td_pflags |= TDP_SOFTDEP;
13757 process_worklist_item(mp, 2, LK_NOWAIT);
13758 td->td_pflags &= ~TDP_SOFTDEP;
13759 stat_worklist_push += 2;
13763 * Next, we attempt to speed up the syncer process. If that
13764 * is successful, then we allow the process to continue.
13766 if (softdep_speedup(ump) &&
13767 resource != FLUSH_BLOCKS_WAIT &&
13768 resource != FLUSH_INODES_WAIT)
13771 * If we are resource constrained on inode dependencies, try
13772 * flushing some dirty inodes. Otherwise, we are constrained
13773 * by file deletions, so try accelerating flushes of directories
13774 * with removal dependencies. We would like to do the cleanup
13775 * here, but we probably hold an inode locked at this point and
13776 * that might deadlock against one that we try to clean. So,
13777 * the best that we can do is request the syncer daemon to do
13778 * the cleanup for us.
13780 switch (resource) {
13782 case FLUSH_INODES_WAIT:
13783 ACQUIRE_GBLLOCK(&lk);
13784 stat_ino_limit_push += 1;
13785 req_clear_inodedeps += 1;
13787 stat_countp = &stat_ino_limit_hit;
13791 case FLUSH_BLOCKS_WAIT:
13792 ACQUIRE_GBLLOCK(&lk);
13793 stat_blk_limit_push += 1;
13794 req_clear_remove += 1;
13796 stat_countp = &stat_blk_limit_hit;
13800 panic("request_cleanup: unknown type");
13803 * Hopefully the syncer daemon will catch up and awaken us.
13804 * We wait at most tickdelay before proceeding in any case.
13806 ACQUIRE_GBLLOCK(&lk);
13809 if (callout_pending(&softdep_callout) == FALSE)
13810 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13813 if ((td->td_pflags & TDP_KTHREAD) == 0)
13814 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13822 * Awaken processes pausing in request_cleanup and clear proc_waiting
13823 * to indicate that there is no longer a timer running. Pause_timer
13824 * will be called with the global softdep mutex (&lk) locked.
13831 GBLLOCK_OWNED(&lk);
13833 * The callout_ API has acquired mtx and will hold it around this
13836 *stat_countp += proc_waiting;
13837 wakeup(&proc_waiting);
13841 * If requested, try removing inode or removal dependencies.
13844 check_clear_deps(mp)
13847 struct ufsmount *ump;
13851 * Tell the lower layers that any TRIM or WRITE transactions that have
13852 * been delayed for performance reasons should proceed to help alleviate
13853 * the shortage faster. The race between checking req_* and the softdep
13854 * mutex (lk) is fine since this is an advisory operation that at most
13855 * causes deferred work to be done sooner.
13857 ump = VFSTOUFS(mp);
13858 suj_susp = MOUNTEDSUJ(mp) && ump->softdep_jblocks->jb_suspended;
13859 if (req_clear_remove || req_clear_inodedeps || suj_susp) {
13861 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
13866 * If we are suspended, it may be because of our using
13867 * too many inodedeps, so help clear them out.
13870 clear_inodedeps(mp);
13873 * General requests for cleanup of backed up dependencies
13875 ACQUIRE_GBLLOCK(&lk);
13876 if (req_clear_inodedeps) {
13877 req_clear_inodedeps -= 1;
13879 clear_inodedeps(mp);
13880 ACQUIRE_GBLLOCK(&lk);
13881 wakeup(&proc_waiting);
13883 if (req_clear_remove) {
13884 req_clear_remove -= 1;
13887 ACQUIRE_GBLLOCK(&lk);
13888 wakeup(&proc_waiting);
13894 * Flush out a directory with at least one removal dependency in an effort to
13895 * reduce the number of dirrem, freefile, and freeblks dependency structures.
13901 struct pagedep_hashhead *pagedephd;
13902 struct pagedep *pagedep;
13903 struct ufsmount *ump;
13909 ump = VFSTOUFS(mp);
13912 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
13913 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
13914 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
13915 ump->pagedep_nextclean = 0;
13916 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
13917 if (LIST_EMPTY(&pagedep->pd_dirremhd))
13919 ino = pagedep->pd_ino;
13920 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13925 * Let unmount clear deps
13927 error = vfs_busy(mp, MBF_NOWAIT);
13930 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13934 softdep_error("clear_remove: vget", error);
13937 MPASS(VTOI(vp)->i_mode != 0);
13938 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13939 softdep_error("clear_remove: fsync", error);
13940 bo = &vp->v_bufobj;
13946 vn_finished_write(mp);
13954 * Clear out a block of dirty inodes in an effort to reduce
13955 * the number of inodedep dependency structures.
13958 clear_inodedeps(mp)
13961 struct inodedep_hashhead *inodedephd;
13962 struct inodedep *inodedep;
13963 struct ufsmount *ump;
13967 ino_t firstino, lastino, ino;
13969 ump = VFSTOUFS(mp);
13973 * Pick a random inode dependency to be cleared.
13974 * We will then gather up all the inodes in its block
13975 * that have dependencies and flush them out.
13977 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
13978 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
13979 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
13980 ump->inodedep_nextclean = 0;
13981 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
13984 if (inodedep == NULL)
13987 * Find the last inode in the block with dependencies.
13989 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
13990 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
13991 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
13994 * Asynchronously push all but the last inode with dependencies.
13995 * Synchronously push the last inode with dependencies to ensure
13996 * that the inode block gets written to free up the inodedeps.
13998 for (ino = firstino; ino <= lastino; ino++) {
13999 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
14001 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
14004 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
14006 vn_finished_write(mp);
14010 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
14011 FFSV_FORCEINSMQ)) != 0) {
14012 softdep_error("clear_inodedeps: vget", error);
14014 vn_finished_write(mp);
14019 if (VTOI(vp)->i_mode == 0) {
14021 } else if (ino == lastino) {
14022 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)))
14023 softdep_error("clear_inodedeps: fsync1", error);
14025 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
14026 softdep_error("clear_inodedeps: fsync2", error);
14027 BO_LOCK(&vp->v_bufobj);
14029 BO_UNLOCK(&vp->v_bufobj);
14032 vn_finished_write(mp);
14038 softdep_buf_append(bp, wkhd)
14040 struct workhead *wkhd;
14042 struct worklist *wk;
14043 struct ufsmount *ump;
14045 if ((wk = LIST_FIRST(wkhd)) == NULL)
14047 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14048 ("softdep_buf_append called on non-softdep filesystem"));
14049 ump = VFSTOUFS(wk->wk_mp);
14051 while ((wk = LIST_FIRST(wkhd)) != NULL) {
14052 WORKLIST_REMOVE(wk);
14053 WORKLIST_INSERT(&bp->b_dep, wk);
14060 softdep_inode_append(ip, cred, wkhd)
14062 struct ucred *cred;
14063 struct workhead *wkhd;
14067 struct ufsmount *ump;
14071 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14072 ("softdep_inode_append called on non-softdep filesystem"));
14074 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14075 (int)fs->fs_bsize, cred, &bp);
14078 softdep_freework(wkhd);
14081 softdep_buf_append(bp, wkhd);
14086 softdep_freework(wkhd)
14087 struct workhead *wkhd;
14089 struct worklist *wk;
14090 struct ufsmount *ump;
14092 if ((wk = LIST_FIRST(wkhd)) == NULL)
14094 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14095 ("softdep_freework called on non-softdep filesystem"));
14096 ump = VFSTOUFS(wk->wk_mp);
14098 handle_jwork(wkhd);
14102 static struct ufsmount *
14103 softdep_bp_to_mp(bp)
14109 if (LIST_EMPTY(&bp->b_dep))
14112 KASSERT(vp != NULL,
14113 ("%s, buffer with dependencies lacks vnode", __func__));
14116 * The ump mount point is stable after we get a correct
14117 * pointer, since bp is locked and this prevents unmount from
14118 * proceeding. But to get to it, we cannot dereference bp->b_dep
14119 * head wk_mp, because we do not yet own SU ump lock and
14120 * workitem might be freed while dereferenced.
14123 switch (vp->v_type) {
14126 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14139 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14147 vn_printf(vp, "unknown vnode type");
14151 return (VFSTOUFS(mp));
14155 * Function to determine if the buffer has outstanding dependencies
14156 * that will cause a roll-back if the buffer is written. If wantcount
14157 * is set, return number of dependencies, otherwise just yes or no.
14160 softdep_count_dependencies(bp, wantcount)
14164 struct worklist *wk;
14165 struct ufsmount *ump;
14166 struct bmsafemap *bmsafemap;
14167 struct freework *freework;
14168 struct inodedep *inodedep;
14169 struct indirdep *indirdep;
14170 struct freeblks *freeblks;
14171 struct allocindir *aip;
14172 struct pagedep *pagedep;
14173 struct dirrem *dirrem;
14174 struct newblk *newblk;
14175 struct mkdir *mkdir;
14176 struct diradd *dap;
14179 ump = softdep_bp_to_mp(bp);
14184 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14185 switch (wk->wk_type) {
14187 inodedep = WK_INODEDEP(wk);
14188 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14189 /* bitmap allocation dependency */
14194 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14195 /* direct block pointer dependency */
14200 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14201 /* direct block pointer dependency */
14206 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14207 /* Add reference dependency. */
14215 indirdep = WK_INDIRDEP(wk);
14217 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14218 /* indirect truncation dependency */
14224 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14225 /* indirect block pointer dependency */
14233 pagedep = WK_PAGEDEP(wk);
14234 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14235 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14236 /* Journal remove ref dependency. */
14242 for (i = 0; i < DAHASHSZ; i++) {
14243 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14244 /* directory entry dependency */
14253 bmsafemap = WK_BMSAFEMAP(wk);
14254 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14255 /* Add reference dependency. */
14260 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14261 /* Allocate block dependency. */
14269 freeblks = WK_FREEBLKS(wk);
14270 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14271 /* Freeblk journal dependency. */
14278 case D_ALLOCDIRECT:
14280 newblk = WK_NEWBLK(wk);
14281 if (newblk->nb_jnewblk) {
14282 /* Journal allocate dependency. */
14290 mkdir = WK_MKDIR(wk);
14291 if (mkdir->md_jaddref) {
14292 /* Journal reference dependency. */
14304 /* never a dependency on these blocks */
14308 panic("softdep_count_dependencies: Unexpected type %s",
14309 TYPENAME(wk->wk_type));
14319 * Acquire exclusive access to a buffer.
14320 * Must be called with a locked mtx parameter.
14321 * Return acquired buffer or NULL on failure.
14323 static struct buf *
14324 getdirtybuf(bp, lock, waitfor)
14326 struct rwlock *lock;
14331 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14332 if (waitfor != MNT_WAIT)
14334 error = BUF_LOCK(bp,
14335 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14337 * Even if we successfully acquire bp here, we have dropped
14338 * lock, which may violates our guarantee.
14342 else if (error != ENOLCK)
14343 panic("getdirtybuf: inconsistent lock: %d", error);
14347 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14348 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14350 BO_LOCK(bp->b_bufobj);
14352 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14353 bp->b_vflags |= BV_BKGRDWAIT;
14354 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14355 PRIBIO | PDROP, "getbuf", 0);
14357 BO_UNLOCK(bp->b_bufobj);
14362 if (waitfor != MNT_WAIT)
14364 #ifdef DEBUG_VFS_LOCKS
14365 if (bp->b_vp->v_type != VCHR)
14366 ASSERT_BO_WLOCKED(bp->b_bufobj);
14368 bp->b_vflags |= BV_BKGRDWAIT;
14369 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14372 if ((bp->b_flags & B_DELWRI) == 0) {
14381 * Check if it is safe to suspend the file system now. On entry,
14382 * the vnode interlock for devvp should be held. Return 0 with
14383 * the mount interlock held if the file system can be suspended now,
14384 * otherwise return EAGAIN with the mount interlock held.
14387 softdep_check_suspend(struct mount *mp,
14388 struct vnode *devvp,
14389 int softdep_depcnt,
14390 int softdep_accdepcnt,
14391 int secondary_writes,
14392 int secondary_accwrites)
14395 struct ufsmount *ump;
14396 struct inodedep *inodedep;
14397 int error, unlinked;
14399 bo = &devvp->v_bufobj;
14400 ASSERT_BO_WLOCKED(bo);
14403 * If we are not running with soft updates, then we need only
14404 * deal with secondary writes as we try to suspend.
14406 if (MOUNTEDSOFTDEP(mp) == 0) {
14408 while (mp->mnt_secondary_writes != 0) {
14410 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14411 (PUSER - 1) | PDROP, "secwr", 0);
14417 * Reasons for needing more work before suspend:
14418 * - Dirty buffers on devvp.
14419 * - Secondary writes occurred after start of vnode sync loop
14422 if (bo->bo_numoutput > 0 ||
14423 bo->bo_dirty.bv_cnt > 0 ||
14424 secondary_writes != 0 ||
14425 mp->mnt_secondary_writes != 0 ||
14426 secondary_accwrites != mp->mnt_secondary_accwrites)
14433 * If we are running with soft updates, then we need to coordinate
14434 * with them as we try to suspend.
14436 ump = VFSTOUFS(mp);
14438 if (!TRY_ACQUIRE_LOCK(ump)) {
14446 if (mp->mnt_secondary_writes != 0) {
14449 msleep(&mp->mnt_secondary_writes,
14451 (PUSER - 1) | PDROP, "secwr", 0);
14459 if (MOUNTEDSUJ(mp)) {
14460 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14462 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14463 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14464 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14466 !check_inodedep_free(inodedep))
14473 * Reasons for needing more work before suspend:
14474 * - Dirty buffers on devvp.
14475 * - Softdep activity occurred after start of vnode sync loop
14476 * - Secondary writes occurred after start of vnode sync loop
14479 if (bo->bo_numoutput > 0 ||
14480 bo->bo_dirty.bv_cnt > 0 ||
14481 softdep_depcnt != unlinked ||
14482 ump->softdep_deps != unlinked ||
14483 softdep_accdepcnt != ump->softdep_accdeps ||
14484 secondary_writes != 0 ||
14485 mp->mnt_secondary_writes != 0 ||
14486 secondary_accwrites != mp->mnt_secondary_accwrites)
14494 * Get the number of dependency structures for the file system, both
14495 * the current number and the total number allocated. These will
14496 * later be used to detect that softdep processing has occurred.
14499 softdep_get_depcounts(struct mount *mp,
14500 int *softdep_depsp,
14501 int *softdep_accdepsp)
14503 struct ufsmount *ump;
14505 if (MOUNTEDSOFTDEP(mp) == 0) {
14506 *softdep_depsp = 0;
14507 *softdep_accdepsp = 0;
14510 ump = VFSTOUFS(mp);
14512 *softdep_depsp = ump->softdep_deps;
14513 *softdep_accdepsp = ump->softdep_accdeps;
14518 * Wait for pending output on a vnode to complete.
14525 ASSERT_VOP_LOCKED(vp, "drain_output");
14526 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14530 * Called whenever a buffer that is being invalidated or reallocated
14531 * contains dependencies. This should only happen if an I/O error has
14532 * occurred. The routine is called with the buffer locked.
14535 softdep_deallocate_dependencies(bp)
14539 if ((bp->b_ioflags & BIO_ERROR) == 0)
14540 panic("softdep_deallocate_dependencies: dangling deps");
14541 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14542 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14544 printf("softdep_deallocate_dependencies: "
14545 "got error %d while accessing filesystem\n", bp->b_error);
14546 if (bp->b_error != ENXIO)
14547 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14551 * Function to handle asynchronous write errors in the filesystem.
14554 softdep_error(func, error)
14559 /* XXX should do something better! */
14560 printf("%s: got error %d while accessing filesystem\n", func, error);
14565 /* exported to ffs_vfsops.c */
14566 extern void db_print_ffs(struct ufsmount *ump);
14568 db_print_ffs(struct ufsmount *ump)
14570 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14571 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14572 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n",
14573 ump->um_fs, ump->softdep_on_worklist,
14574 ump->softdep_deps, ump->softdep_req);
14578 worklist_print(struct worklist *wk, int verbose)
14582 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14583 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
14586 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
14587 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
14588 LIST_NEXT(wk, wk_list));
14589 db_print_ffs(VFSTOUFS(wk->wk_mp));
14593 inodedep_print(struct inodedep *inodedep, int verbose)
14596 worklist_print(&inodedep->id_list, 0);
14597 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14599 (intmax_t)inodedep->id_ino,
14600 (intmax_t)fsbtodb(inodedep->id_fs,
14601 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14602 (intmax_t)inodedep->id_nlinkdelta,
14603 (intmax_t)inodedep->id_savednlink);
14608 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14609 inodedep->id_bmsafemap,
14610 inodedep->id_mkdiradd,
14611 TAILQ_FIRST(&inodedep->id_inoreflst));
14612 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
14613 LIST_FIRST(&inodedep->id_dirremhd),
14614 LIST_FIRST(&inodedep->id_pendinghd),
14615 LIST_FIRST(&inodedep->id_bufwait));
14616 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
14617 LIST_FIRST(&inodedep->id_inowait),
14618 TAILQ_FIRST(&inodedep->id_inoupdt),
14619 TAILQ_FIRST(&inodedep->id_newinoupdt));
14620 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
14621 TAILQ_FIRST(&inodedep->id_extupdt),
14622 TAILQ_FIRST(&inodedep->id_newextupdt),
14623 TAILQ_FIRST(&inodedep->id_freeblklst));
14624 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
14625 inodedep->id_savedino1,
14626 (intmax_t)inodedep->id_savedsize,
14627 (intmax_t)inodedep->id_savedextsize);
14631 newblk_print(struct newblk *nbp)
14634 worklist_print(&nbp->nb_list, 0);
14635 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14636 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
14638 &nbp->nb_bmsafemap,
14639 &nbp->nb_freefrag);
14640 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
14641 LIST_FIRST(&nbp->nb_indirdeps),
14642 LIST_FIRST(&nbp->nb_newdirblk),
14643 LIST_FIRST(&nbp->nb_jwork));
14647 allocdirect_print(struct allocdirect *adp)
14650 newblk_print(&adp->ad_block);
14651 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
14652 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14653 db_printf(" offset %d, inodedep %p\n",
14654 adp->ad_offset, adp->ad_inodedep);
14658 allocindir_print(struct allocindir *aip)
14661 newblk_print(&aip->ai_block);
14662 db_printf(" oldblkno %jd, lbn %jd\n",
14663 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
14664 db_printf(" offset %d, indirdep %p\n",
14665 aip->ai_offset, aip->ai_indirdep);
14669 mkdir_print(struct mkdir *mkdir)
14672 worklist_print(&mkdir->md_list, 0);
14673 db_printf(" diradd %p, jaddref %p, buf %p\n",
14674 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
14677 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
14680 if (have_addr == 0) {
14681 db_printf("inodedep address required\n");
14684 inodedep_print((struct inodedep*)addr, 1);
14687 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
14689 struct inodedep_hashhead *inodedephd;
14690 struct inodedep *inodedep;
14691 struct ufsmount *ump;
14694 if (have_addr == 0) {
14695 db_printf("ufsmount address required\n");
14698 ump = (struct ufsmount *)addr;
14699 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14700 inodedephd = &ump->inodedep_hashtbl[cnt];
14701 LIST_FOREACH(inodedep, inodedephd, id_hash) {
14702 inodedep_print(inodedep, 0);
14707 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
14710 if (have_addr == 0) {
14711 db_printf("worklist address required\n");
14714 worklist_print((struct worklist *)addr, 1);
14717 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
14719 struct worklist *wk;
14720 struct workhead *wkhd;
14722 if (have_addr == 0) {
14723 db_printf("worklist address required "
14724 "(for example value in bp->b_dep)\n");
14728 * We often do not have the address of the worklist head but
14729 * instead a pointer to its first entry (e.g., we have the
14730 * contents of bp->b_dep rather than &bp->b_dep). But the back
14731 * pointer of bp->b_dep will point at the head of the list, so
14732 * we cheat and use that instead. If we are in the middle of
14733 * a list we will still get the same result, so nothing
14734 * unexpected will result.
14736 wk = (struct worklist *)addr;
14739 wkhd = (struct workhead *)wk->wk_list.le_prev;
14740 LIST_FOREACH(wk, wkhd, wk_list) {
14741 switch(wk->wk_type) {
14743 inodedep_print(WK_INODEDEP(wk), 0);
14745 case D_ALLOCDIRECT:
14746 allocdirect_print(WK_ALLOCDIRECT(wk));
14749 allocindir_print(WK_ALLOCINDIR(wk));
14752 mkdir_print(WK_MKDIR(wk));
14755 worklist_print(wk, 0);
14761 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
14763 if (have_addr == 0) {
14764 db_printf("mkdir address required\n");
14767 mkdir_print((struct mkdir *)addr);
14770 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
14772 struct mkdirlist *mkdirlisthd;
14773 struct mkdir *mkdir;
14775 if (have_addr == 0) {
14776 db_printf("mkdir listhead address required\n");
14779 mkdirlisthd = (struct mkdirlist *)addr;
14780 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14781 mkdir_print(mkdir);
14782 if (mkdir->md_diradd != NULL) {
14784 worklist_print(&mkdir->md_diradd->da_list, 0);
14786 if (mkdir->md_jaddref != NULL) {
14788 worklist_print(&mkdir->md_jaddref->ja_list, 0);
14793 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
14795 if (have_addr == 0) {
14796 db_printf("allocdirect address required\n");
14799 allocdirect_print((struct allocdirect *)addr);
14802 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
14804 if (have_addr == 0) {
14805 db_printf("allocindir address required\n");
14808 allocindir_print((struct allocindir *)addr);
14813 #endif /* SOFTUPDATES */