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, 0,
617 "soft updates stats");
618 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0,
619 "total dependencies allocated");
620 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0,
621 "high use dependencies allocated");
622 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0,
623 "current dependencies allocated");
624 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0,
625 "current dependencies written");
627 unsigned long dep_current[D_LAST + 1];
628 unsigned long dep_highuse[D_LAST + 1];
629 unsigned long dep_total[D_LAST + 1];
630 unsigned long dep_write[D_LAST + 1];
632 #define SOFTDEP_TYPE(type, str, long) \
633 static MALLOC_DEFINE(M_ ## type, #str, long); \
634 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \
635 &dep_total[D_ ## type], 0, ""); \
636 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \
637 &dep_current[D_ ## type], 0, ""); \
638 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \
639 &dep_highuse[D_ ## type], 0, ""); \
640 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \
641 &dep_write[D_ ## type], 0, "");
643 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
644 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
645 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
646 "Block or frag allocated from cyl group map");
647 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
648 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
649 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
650 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
651 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
652 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
653 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
654 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
655 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
656 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
657 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
658 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
659 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
660 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
661 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
662 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
663 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
664 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
665 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
666 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
667 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
668 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
669 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
670 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
672 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
674 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
675 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
676 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
678 #define M_SOFTDEP_FLAGS (M_WAITOK)
681 * translate from workitem type to memory type
682 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
684 static struct malloc_type *memtype[] = {
716 #define DtoM(type) (memtype[type])
719 * Names of malloc types.
721 #define TYPENAME(type) \
722 ((unsigned)(type) <= D_LAST && (unsigned)(type) >= D_FIRST ? \
723 memtype[type]->ks_shortdesc : "???")
725 * End system adaptation definitions.
728 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
729 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
732 * Internal function prototypes.
734 static void check_clear_deps(struct mount *);
735 static void softdep_error(char *, int);
736 static int softdep_process_worklist(struct mount *, int);
737 static int softdep_waitidle(struct mount *, int);
738 static void drain_output(struct vnode *);
739 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
740 static int check_inodedep_free(struct inodedep *);
741 static void clear_remove(struct mount *);
742 static void clear_inodedeps(struct mount *);
743 static void unlinked_inodedep(struct mount *, struct inodedep *);
744 static void clear_unlinked_inodedep(struct inodedep *);
745 static struct inodedep *first_unlinked_inodedep(struct ufsmount *);
746 static int flush_pagedep_deps(struct vnode *, struct mount *,
748 static int free_pagedep(struct pagedep *);
749 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
750 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
751 static int flush_deplist(struct allocdirectlst *, int, int *);
752 static int sync_cgs(struct mount *, int);
753 static int handle_written_filepage(struct pagedep *, struct buf *, int);
754 static int handle_written_sbdep(struct sbdep *, struct buf *);
755 static void initiate_write_sbdep(struct sbdep *);
756 static void diradd_inode_written(struct diradd *, struct inodedep *);
757 static int handle_written_indirdep(struct indirdep *, struct buf *,
759 static int handle_written_inodeblock(struct inodedep *, struct buf *, int);
760 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
762 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *, int);
763 static void handle_written_jaddref(struct jaddref *);
764 static void handle_written_jremref(struct jremref *);
765 static void handle_written_jseg(struct jseg *, struct buf *);
766 static void handle_written_jnewblk(struct jnewblk *);
767 static void handle_written_jblkdep(struct jblkdep *);
768 static void handle_written_jfreefrag(struct jfreefrag *);
769 static void complete_jseg(struct jseg *);
770 static void complete_jsegs(struct jseg *);
771 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
772 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
773 static void jremref_write(struct jremref *, struct jseg *, uint8_t *);
774 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
775 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
776 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
777 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
778 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
779 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
780 static inline void inoref_write(struct inoref *, struct jseg *,
782 static void handle_allocdirect_partdone(struct allocdirect *,
784 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
786 static void indirdep_complete(struct indirdep *);
787 static int indirblk_lookup(struct mount *, ufs2_daddr_t);
788 static void indirblk_insert(struct freework *);
789 static void indirblk_remove(struct freework *);
790 static void handle_allocindir_partdone(struct allocindir *);
791 static void initiate_write_filepage(struct pagedep *, struct buf *);
792 static void initiate_write_indirdep(struct indirdep*, struct buf *);
793 static void handle_written_mkdir(struct mkdir *, int);
794 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
796 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
797 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
798 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
799 static void handle_workitem_freefile(struct freefile *);
800 static int handle_workitem_remove(struct dirrem *, int);
801 static struct dirrem *newdirrem(struct buf *, struct inode *,
802 struct inode *, int, struct dirrem **);
803 static struct indirdep *indirdep_lookup(struct mount *, struct inode *,
805 static void cancel_indirdep(struct indirdep *, struct buf *,
807 static void free_indirdep(struct indirdep *);
808 static void free_diradd(struct diradd *, struct workhead *);
809 static void merge_diradd(struct inodedep *, struct diradd *);
810 static void complete_diradd(struct diradd *);
811 static struct diradd *diradd_lookup(struct pagedep *, int);
812 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
814 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
816 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
817 struct jremref *, struct jremref *);
818 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
820 static void cancel_allocindir(struct allocindir *, struct buf *bp,
821 struct freeblks *, int);
822 static int setup_trunc_indir(struct freeblks *, struct inode *,
823 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
824 static void complete_trunc_indir(struct freework *);
825 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
827 static void complete_mkdir(struct mkdir *);
828 static void free_newdirblk(struct newdirblk *);
829 static void free_jremref(struct jremref *);
830 static void free_jaddref(struct jaddref *);
831 static void free_jsegdep(struct jsegdep *);
832 static void free_jsegs(struct jblocks *);
833 static void rele_jseg(struct jseg *);
834 static void free_jseg(struct jseg *, struct jblocks *);
835 static void free_jnewblk(struct jnewblk *);
836 static void free_jblkdep(struct jblkdep *);
837 static void free_jfreefrag(struct jfreefrag *);
838 static void free_freedep(struct freedep *);
839 static void journal_jremref(struct dirrem *, struct jremref *,
841 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
842 static int cancel_jaddref(struct jaddref *, struct inodedep *,
844 static void cancel_jfreefrag(struct jfreefrag *);
845 static inline void setup_freedirect(struct freeblks *, struct inode *,
847 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
848 static inline void setup_freeindir(struct freeblks *, struct inode *, int,
850 static inline struct freeblks *newfreeblks(struct mount *, struct inode *);
851 static void freeblks_free(struct ufsmount *, struct freeblks *, int);
852 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
853 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
854 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
855 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
857 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
858 static int cancel_pagedep(struct pagedep *, struct freeblks *, int);
859 static int deallocate_dependencies(struct buf *, struct freeblks *, int);
860 static void newblk_freefrag(struct newblk*);
861 static void free_newblk(struct newblk *);
862 static void cancel_allocdirect(struct allocdirectlst *,
863 struct allocdirect *, struct freeblks *);
864 static int check_inode_unwritten(struct inodedep *);
865 static int free_inodedep(struct inodedep *);
866 static void freework_freeblock(struct freework *, u_long);
867 static void freework_enqueue(struct freework *);
868 static int handle_workitem_freeblocks(struct freeblks *, int);
869 static int handle_complete_freeblocks(struct freeblks *, int);
870 static void handle_workitem_indirblk(struct freework *);
871 static void handle_written_freework(struct freework *);
872 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
873 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
875 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
876 struct inodedep *, struct allocindir *, ufs_lbn_t);
877 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
878 ufs2_daddr_t, ufs_lbn_t);
879 static void handle_workitem_freefrag(struct freefrag *);
880 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
882 static void allocdirect_merge(struct allocdirectlst *,
883 struct allocdirect *, struct allocdirect *);
884 static struct freefrag *allocindir_merge(struct allocindir *,
885 struct allocindir *);
886 static int bmsafemap_find(struct bmsafemap_hashhead *, int,
887 struct bmsafemap **);
888 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
889 int cg, struct bmsafemap *);
890 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
892 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
893 static int inodedep_find(struct inodedep_hashhead *, ino_t,
895 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
896 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
897 int, struct pagedep **);
898 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
900 static void pause_timer(void *);
901 static int request_cleanup(struct mount *, int);
902 static int softdep_request_cleanup_flush(struct mount *, struct ufsmount *);
903 static void schedule_cleanup(struct mount *);
904 static void softdep_ast_cleanup_proc(struct thread *);
905 static struct ufsmount *softdep_bp_to_mp(struct buf *bp);
906 static int process_worklist_item(struct mount *, int, int);
907 static void process_removes(struct vnode *);
908 static void process_truncates(struct vnode *);
909 static void jwork_move(struct workhead *, struct workhead *);
910 static void jwork_insert(struct workhead *, struct jsegdep *);
911 static void add_to_worklist(struct worklist *, int);
912 static void wake_worklist(struct worklist *);
913 static void wait_worklist(struct worklist *, char *);
914 static void remove_from_worklist(struct worklist *);
915 static void softdep_flush(void *);
916 static void softdep_flushjournal(struct mount *);
917 static int softdep_speedup(struct ufsmount *);
918 static void worklist_speedup(struct mount *);
919 static int journal_mount(struct mount *, struct fs *, struct ucred *);
920 static void journal_unmount(struct ufsmount *);
921 static int journal_space(struct ufsmount *, int);
922 static void journal_suspend(struct ufsmount *);
923 static int journal_unsuspend(struct ufsmount *ump);
924 static void softdep_prelink(struct vnode *, struct vnode *);
925 static void add_to_journal(struct worklist *);
926 static void remove_from_journal(struct worklist *);
927 static bool softdep_excess_items(struct ufsmount *, int);
928 static void softdep_process_journal(struct mount *, struct worklist *, int);
929 static struct jremref *newjremref(struct dirrem *, struct inode *,
930 struct inode *ip, off_t, nlink_t);
931 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t,
933 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t,
935 static inline struct jsegdep *inoref_jseg(struct inoref *);
936 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t);
937 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t,
939 static void adjust_newfreework(struct freeblks *, int);
940 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int);
941 static void move_newblock_dep(struct jaddref *, struct inodedep *);
942 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t);
943 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *,
944 ufs2_daddr_t, long, ufs_lbn_t);
945 static struct freework *newfreework(struct ufsmount *, struct freeblks *,
946 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int);
947 static int jwait(struct worklist *, int);
948 static struct inodedep *inodedep_lookup_ip(struct inode *);
949 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *);
950 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *);
951 static void handle_jwork(struct workhead *);
952 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *,
954 static struct jblocks *jblocks_create(void);
955 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *);
956 static void jblocks_free(struct jblocks *, struct mount *, int);
957 static void jblocks_destroy(struct jblocks *);
958 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int);
961 * Exported softdep operations.
963 static void softdep_disk_io_initiation(struct buf *);
964 static void softdep_disk_write_complete(struct buf *);
965 static void softdep_deallocate_dependencies(struct buf *);
966 static int softdep_count_dependencies(struct buf *bp, int);
969 * Global lock over all of soft updates.
971 static struct mtx lk;
972 MTX_SYSINIT(softdep_lock, &lk, "global softdep", MTX_DEF);
974 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk)
975 #define FREE_GBLLOCK(lk) mtx_unlock(lk)
976 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED)
979 * Per-filesystem soft-updates locking.
981 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock)
982 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock)
983 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock)
984 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock)
985 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \
988 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock)
989 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock)
992 * Worklist queue management.
993 * These routines require that the lock be held.
995 #ifndef /* NOT */ INVARIANTS
996 #define WORKLIST_INSERT(head, item) do { \
997 (item)->wk_state |= ONWORKLIST; \
998 LIST_INSERT_HEAD(head, item, wk_list); \
1000 #define WORKLIST_REMOVE(item) do { \
1001 (item)->wk_state &= ~ONWORKLIST; \
1002 LIST_REMOVE(item, wk_list); \
1004 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT
1005 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE
1007 #else /* INVARIANTS */
1008 static void worklist_insert(struct workhead *, struct worklist *, int,
1010 static void worklist_remove(struct worklist *, int, const char *, int);
1012 #define WORKLIST_INSERT(head, item) \
1013 worklist_insert(head, item, 1, __func__, __LINE__)
1014 #define WORKLIST_INSERT_UNLOCKED(head, item)\
1015 worklist_insert(head, item, 0, __func__, __LINE__)
1016 #define WORKLIST_REMOVE(item)\
1017 worklist_remove(item, 1, __func__, __LINE__)
1018 #define WORKLIST_REMOVE_UNLOCKED(item)\
1019 worklist_remove(item, 0, __func__, __LINE__)
1022 worklist_insert(head, item, locked, func, line)
1023 struct workhead *head;
1024 struct worklist *item;
1031 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1032 if (item->wk_state & ONWORKLIST)
1033 panic("worklist_insert: %p %s(0x%X) already on list, "
1034 "added in function %s at line %d",
1035 item, TYPENAME(item->wk_type), item->wk_state,
1036 item->wk_func, item->wk_line);
1037 item->wk_state |= ONWORKLIST;
1038 item->wk_func = func;
1039 item->wk_line = line;
1040 LIST_INSERT_HEAD(head, item, wk_list);
1044 worklist_remove(item, locked, func, line)
1045 struct worklist *item;
1052 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1053 if ((item->wk_state & ONWORKLIST) == 0)
1054 panic("worklist_remove: %p %s(0x%X) not on list, "
1055 "removed in function %s at line %d",
1056 item, TYPENAME(item->wk_type), item->wk_state,
1057 item->wk_func, item->wk_line);
1058 item->wk_state &= ~ONWORKLIST;
1059 item->wk_func = func;
1060 item->wk_line = line;
1061 LIST_REMOVE(item, wk_list);
1063 #endif /* INVARIANTS */
1066 * Merge two jsegdeps keeping only the oldest one as newer references
1067 * can't be discarded until after older references.
1069 static inline struct jsegdep *
1070 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1072 struct jsegdep *swp;
1077 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1082 WORKLIST_REMOVE(&two->jd_list);
1089 * If two freedeps are compatible free one to reduce list size.
1091 static inline struct freedep *
1092 freedep_merge(struct freedep *one, struct freedep *two)
1097 if (one->fd_freework == two->fd_freework) {
1098 WORKLIST_REMOVE(&two->fd_list);
1105 * Move journal work from one list to another. Duplicate freedeps and
1106 * jsegdeps are coalesced to keep the lists as small as possible.
1109 jwork_move(dst, src)
1110 struct workhead *dst;
1111 struct workhead *src;
1113 struct freedep *freedep;
1114 struct jsegdep *jsegdep;
1115 struct worklist *wkn;
1116 struct worklist *wk;
1119 ("jwork_move: dst == src"));
1122 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1123 if (wk->wk_type == D_JSEGDEP)
1124 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1125 else if (wk->wk_type == D_FREEDEP)
1126 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1129 while ((wk = LIST_FIRST(src)) != NULL) {
1130 WORKLIST_REMOVE(wk);
1131 WORKLIST_INSERT(dst, wk);
1132 if (wk->wk_type == D_JSEGDEP) {
1133 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1136 if (wk->wk_type == D_FREEDEP)
1137 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1142 jwork_insert(dst, jsegdep)
1143 struct workhead *dst;
1144 struct jsegdep *jsegdep;
1146 struct jsegdep *jsegdepn;
1147 struct worklist *wk;
1149 LIST_FOREACH(wk, dst, wk_list)
1150 if (wk->wk_type == D_JSEGDEP)
1153 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1156 jsegdepn = WK_JSEGDEP(wk);
1157 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1158 WORKLIST_REMOVE(wk);
1159 free_jsegdep(jsegdepn);
1160 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1162 free_jsegdep(jsegdep);
1166 * Routines for tracking and managing workitems.
1168 static void workitem_free(struct worklist *, int);
1169 static void workitem_alloc(struct worklist *, int, struct mount *);
1170 static void workitem_reassign(struct worklist *, int);
1172 #define WORKITEM_FREE(item, type) \
1173 workitem_free((struct worklist *)(item), (type))
1174 #define WORKITEM_REASSIGN(item, type) \
1175 workitem_reassign((struct worklist *)(item), (type))
1178 workitem_free(item, type)
1179 struct worklist *item;
1182 struct ufsmount *ump;
1185 if (item->wk_state & ONWORKLIST)
1186 panic("workitem_free: %s(0x%X) still on list, "
1187 "added in function %s at line %d",
1188 TYPENAME(item->wk_type), item->wk_state,
1189 item->wk_func, item->wk_line);
1190 if (item->wk_type != type && type != D_NEWBLK)
1191 panic("workitem_free: type mismatch %s != %s",
1192 TYPENAME(item->wk_type), TYPENAME(type));
1194 if (item->wk_state & IOWAITING)
1196 ump = VFSTOUFS(item->wk_mp);
1198 KASSERT(ump->softdep_deps > 0,
1199 ("workitem_free: %s: softdep_deps going negative",
1200 ump->um_fs->fs_fsmnt));
1201 if (--ump->softdep_deps == 0 && ump->softdep_req)
1202 wakeup(&ump->softdep_deps);
1203 KASSERT(dep_current[item->wk_type] > 0,
1204 ("workitem_free: %s: dep_current[%s] going negative",
1205 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1206 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1207 ("workitem_free: %s: softdep_curdeps[%s] going negative",
1208 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1209 atomic_subtract_long(&dep_current[item->wk_type], 1);
1210 ump->softdep_curdeps[item->wk_type] -= 1;
1212 LIST_REMOVE(item, wk_all);
1214 free(item, DtoM(type));
1218 workitem_alloc(item, type, mp)
1219 struct worklist *item;
1223 struct ufsmount *ump;
1225 item->wk_type = type;
1230 ACQUIRE_GBLLOCK(&lk);
1231 dep_current[type]++;
1232 if (dep_current[type] > dep_highuse[type])
1233 dep_highuse[type] = dep_current[type];
1237 ump->softdep_curdeps[type] += 1;
1238 ump->softdep_deps++;
1239 ump->softdep_accdeps++;
1241 LIST_INSERT_HEAD(&ump->softdep_alldeps[type], item, wk_all);
1247 workitem_reassign(item, newtype)
1248 struct worklist *item;
1251 struct ufsmount *ump;
1253 ump = VFSTOUFS(item->wk_mp);
1255 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1256 ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1257 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1258 ump->softdep_curdeps[item->wk_type] -= 1;
1259 ump->softdep_curdeps[newtype] += 1;
1260 KASSERT(dep_current[item->wk_type] > 0,
1261 ("workitem_reassign: %s: dep_current[%s] going negative",
1262 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1263 ACQUIRE_GBLLOCK(&lk);
1264 dep_current[newtype]++;
1265 dep_current[item->wk_type]--;
1266 if (dep_current[newtype] > dep_highuse[newtype])
1267 dep_highuse[newtype] = dep_current[newtype];
1268 dep_total[newtype]++;
1270 item->wk_type = newtype;
1274 * Workitem queue management
1276 static int max_softdeps; /* maximum number of structs before slowdown */
1277 static int tickdelay = 2; /* number of ticks to pause during slowdown */
1278 static int proc_waiting; /* tracks whether we have a timeout posted */
1279 static int *stat_countp; /* statistic to count in proc_waiting timeout */
1280 static struct callout softdep_callout;
1281 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
1282 static int req_clear_remove; /* syncer process flush some freeblks */
1283 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1286 * runtime statistics
1288 static int stat_flush_threads; /* number of softdep flushing threads */
1289 static int stat_worklist_push; /* number of worklist cleanups */
1290 static int stat_blk_limit_push; /* number of times block limit neared */
1291 static int stat_ino_limit_push; /* number of times inode limit neared */
1292 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
1293 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
1294 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
1295 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
1296 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
1297 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1298 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
1299 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */
1300 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */
1301 static int stat_journal_min; /* Times hit journal min threshold */
1302 static int stat_journal_low; /* Times hit journal low threshold */
1303 static int stat_journal_wait; /* Times blocked in jwait(). */
1304 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */
1305 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */
1306 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */
1307 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */
1308 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1309 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1310 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1311 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1312 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1313 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1315 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1316 &max_softdeps, 0, "");
1317 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1319 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1320 &stat_flush_threads, 0, "");
1321 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push,
1322 CTLFLAG_RW | CTLFLAG_STATS, &stat_worklist_push, 0,"");
1323 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push,
1324 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_push, 0,"");
1325 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push,
1326 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_push, 0,"");
1327 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit,
1328 CTLFLAG_RW | CTLFLAG_STATS, &stat_blk_limit_hit, 0, "");
1329 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit,
1330 CTLFLAG_RW | CTLFLAG_STATS, &stat_ino_limit_hit, 0, "");
1331 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit,
1332 CTLFLAG_RW | CTLFLAG_STATS, &stat_sync_limit_hit, 0, "");
1333 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs,
1334 CTLFLAG_RW | CTLFLAG_STATS, &stat_indir_blk_ptrs, 0, "");
1335 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap,
1336 CTLFLAG_RW | CTLFLAG_STATS, &stat_inode_bitmap, 0, "");
1337 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs,
1338 CTLFLAG_RW | CTLFLAG_STATS, &stat_direct_blk_ptrs, 0, "");
1339 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry,
1340 CTLFLAG_RW | CTLFLAG_STATS, &stat_dir_entry, 0, "");
1341 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback,
1342 CTLFLAG_RW | CTLFLAG_STATS, &stat_jaddref, 0, "");
1343 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback,
1344 CTLFLAG_RW | CTLFLAG_STATS, &stat_jnewblk, 0, "");
1345 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low,
1346 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_low, 0, "");
1347 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min,
1348 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_min, 0, "");
1349 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait,
1350 CTLFLAG_RW | CTLFLAG_STATS, &stat_journal_wait, 0, "");
1351 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage,
1352 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_filepage, 0, "");
1353 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks,
1354 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_freeblks, 0, "");
1355 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode,
1356 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_inode, 0, "");
1357 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk,
1358 CTLFLAG_RW | CTLFLAG_STATS, &stat_jwait_newblk, 0, "");
1359 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests,
1360 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_blkrequests, 0, "");
1361 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests,
1362 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_inorequests, 0, "");
1363 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay,
1364 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_high_delay, 0, "");
1365 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries,
1366 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_retries, 0, "");
1367 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures,
1368 CTLFLAG_RW | CTLFLAG_STATS, &stat_cleanup_failures, 0, "");
1370 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1371 &softdep_flushcache, 0, "");
1372 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1373 &stat_emptyjblocks, 0, "");
1375 SYSCTL_DECL(_vfs_ffs);
1377 /* Whether to recompute the summary at mount time */
1378 static int compute_summary_at_mount = 0;
1379 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1380 &compute_summary_at_mount, 0, "Recompute summary at mount");
1381 static int print_threads = 0;
1382 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1383 &print_threads, 0, "Notify flusher thread start/stop");
1385 /* List of all filesystems mounted with soft updates */
1386 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1389 * This function cleans the worklist for a filesystem.
1390 * Each filesystem running with soft dependencies gets its own
1391 * thread to run in this function. The thread is started up in
1392 * softdep_mount and shutdown in softdep_unmount. They show up
1393 * as part of the kernel "bufdaemon" process whose process
1394 * entry is available in bufdaemonproc.
1396 static int searchfailed;
1397 extern struct proc *bufdaemonproc;
1404 struct ufsmount *ump;
1407 td->td_pflags |= TDP_NORUNNINGBUF;
1408 mp = (struct mount *)addr;
1410 atomic_add_int(&stat_flush_threads, 1);
1412 ump->softdep_flags &= ~FLUSH_STARTING;
1413 wakeup(&ump->softdep_flushtd);
1415 if (print_threads) {
1416 if (stat_flush_threads == 1)
1417 printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1418 bufdaemonproc->p_pid);
1419 printf("Start thread %s\n", td->td_name);
1422 while (softdep_process_worklist(mp, 0) > 0 ||
1424 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1425 kthread_suspend_check();
1427 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1428 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1430 ump->softdep_flags &= ~FLUSH_CLEANUP;
1432 * Check to see if we are done and need to exit.
1434 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1438 ump->softdep_flags &= ~FLUSH_EXIT;
1440 wakeup(&ump->softdep_flags);
1442 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups);
1443 atomic_subtract_int(&stat_flush_threads, 1);
1445 panic("kthread_exit failed\n");
1450 worklist_speedup(mp)
1453 struct ufsmount *ump;
1457 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1458 ump->softdep_flags |= FLUSH_CLEANUP;
1459 wakeup(&ump->softdep_flushtd);
1463 softdep_send_speedup(struct ufsmount *ump, size_t shortage, u_int flags)
1467 if ((ump->um_flags & UM_CANSPEEDUP) == 0)
1470 bp = malloc(sizeof(*bp), M_TRIM, M_WAITOK | M_ZERO);
1471 bp->b_iocmd = BIO_SPEEDUP;
1472 bp->b_ioflags = flags;
1473 bp->b_bcount = shortage;
1474 g_vfs_strategy(ump->um_bo, bp);
1480 softdep_speedup(ump)
1481 struct ufsmount *ump;
1483 struct ufsmount *altump;
1484 struct mount_softdeps *sdp;
1487 worklist_speedup(ump->um_mountp);
1490 * If we have global shortages, then we need other
1491 * filesystems to help with the cleanup. Here we wakeup a
1492 * flusher thread for a filesystem that is over its fair
1493 * share of resources.
1495 if (req_clear_inodedeps || req_clear_remove) {
1496 ACQUIRE_GBLLOCK(&lk);
1497 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1498 if ((altump = sdp->sd_ump) == ump)
1500 if (((req_clear_inodedeps &&
1501 altump->softdep_curdeps[D_INODEDEP] >
1502 max_softdeps / stat_flush_threads) ||
1503 (req_clear_remove &&
1504 altump->softdep_curdeps[D_DIRREM] >
1505 (max_softdeps / 2) / stat_flush_threads)) &&
1506 TRY_ACQUIRE_LOCK(altump))
1514 * Move to the end of the list so we pick a
1515 * different one on out next try.
1517 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1518 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1520 if ((altump->softdep_flags &
1521 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1522 altump->softdep_flags |= FLUSH_CLEANUP;
1523 altump->um_softdep->sd_cleanups++;
1524 wakeup(&altump->softdep_flushtd);
1528 return (speedup_syncer());
1532 * Add an item to the end of the work queue.
1533 * This routine requires that the lock be held.
1534 * This is the only routine that adds items to the list.
1535 * The following routine is the only one that removes items
1536 * and does so in order from first to last.
1539 #define WK_HEAD 0x0001 /* Add to HEAD. */
1540 #define WK_NODELAY 0x0002 /* Process immediately. */
1543 add_to_worklist(wk, flags)
1544 struct worklist *wk;
1547 struct ufsmount *ump;
1549 ump = VFSTOUFS(wk->wk_mp);
1551 if (wk->wk_state & ONWORKLIST)
1552 panic("add_to_worklist: %s(0x%X) already on list",
1553 TYPENAME(wk->wk_type), wk->wk_state);
1554 wk->wk_state |= ONWORKLIST;
1555 if (ump->softdep_on_worklist == 0) {
1556 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1557 ump->softdep_worklist_tail = wk;
1558 } else if (flags & WK_HEAD) {
1559 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1561 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1562 ump->softdep_worklist_tail = wk;
1564 ump->softdep_on_worklist += 1;
1565 if (flags & WK_NODELAY)
1566 worklist_speedup(wk->wk_mp);
1570 * Remove the item to be processed. If we are removing the last
1571 * item on the list, we need to recalculate the tail pointer.
1574 remove_from_worklist(wk)
1575 struct worklist *wk;
1577 struct ufsmount *ump;
1579 ump = VFSTOUFS(wk->wk_mp);
1580 if (ump->softdep_worklist_tail == wk)
1581 ump->softdep_worklist_tail =
1582 (struct worklist *)wk->wk_list.le_prev;
1583 WORKLIST_REMOVE(wk);
1584 ump->softdep_on_worklist -= 1;
1589 struct worklist *wk;
1591 if (wk->wk_state & IOWAITING) {
1592 wk->wk_state &= ~IOWAITING;
1598 wait_worklist(wk, wmesg)
1599 struct worklist *wk;
1602 struct ufsmount *ump;
1604 ump = VFSTOUFS(wk->wk_mp);
1605 wk->wk_state |= IOWAITING;
1606 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1610 * Process that runs once per second to handle items in the background queue.
1612 * Note that we ensure that everything is done in the order in which they
1613 * appear in the queue. The code below depends on this property to ensure
1614 * that blocks of a file are freed before the inode itself is freed. This
1615 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1616 * until all the old ones have been purged from the dependency lists.
1619 softdep_process_worklist(mp, full)
1624 struct ufsmount *ump;
1627 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1628 if (MOUNTEDSOFTDEP(mp) == 0)
1633 starttime = time_second;
1634 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1635 check_clear_deps(mp);
1636 while (ump->softdep_on_worklist > 0) {
1637 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1641 check_clear_deps(mp);
1643 * We do not generally want to stop for buffer space, but if
1644 * we are really being a buffer hog, we will stop and wait.
1646 if (should_yield()) {
1648 kern_yield(PRI_USER);
1653 * Never allow processing to run for more than one
1654 * second. This gives the syncer thread the opportunity
1655 * to pause if appropriate.
1657 if (!full && starttime != time_second)
1661 journal_unsuspend(ump);
1667 * Process all removes associated with a vnode if we are running out of
1668 * journal space. Any other process which attempts to flush these will
1669 * be unable as we have the vnodes locked.
1675 struct inodedep *inodedep;
1676 struct dirrem *dirrem;
1677 struct ufsmount *ump;
1684 inum = VTOI(vp)->i_number;
1687 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1689 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1691 * If another thread is trying to lock this vnode
1692 * it will fail but we must wait for it to do so
1693 * before we can proceed.
1695 if (dirrem->dm_state & INPROGRESS) {
1696 wait_worklist(&dirrem->dm_list, "pwrwait");
1699 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1700 (COMPLETE | ONWORKLIST))
1705 remove_from_worklist(&dirrem->dm_list);
1707 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1708 panic("process_removes: suspended filesystem");
1709 handle_workitem_remove(dirrem, 0);
1710 vn_finished_secondary_write(mp);
1716 * Process all truncations associated with a vnode if we are running out
1717 * of journal space. This is called when the vnode lock is already held
1718 * and no other process can clear the truncation. This function returns
1719 * a value greater than zero if it did any work.
1722 process_truncates(vp)
1725 struct inodedep *inodedep;
1726 struct freeblks *freeblks;
1727 struct ufsmount *ump;
1735 inum = VTOI(vp)->i_number;
1737 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1740 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1741 /* Journal entries not yet written. */
1742 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1744 &freeblks->fb_jblkdephd)->jb_list,
1748 /* Another thread is executing this item. */
1749 if (freeblks->fb_state & INPROGRESS) {
1750 wait_worklist(&freeblks->fb_list, "ptrwait");
1753 /* Freeblks is waiting on a inode write. */
1754 if ((freeblks->fb_state & COMPLETE) == 0) {
1760 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1761 (ALLCOMPLETE | ONWORKLIST)) {
1762 remove_from_worklist(&freeblks->fb_list);
1763 freeblks->fb_state |= INPROGRESS;
1765 if (vn_start_secondary_write(NULL, &mp,
1767 panic("process_truncates: "
1768 "suspended filesystem");
1769 handle_workitem_freeblocks(freeblks, 0);
1770 vn_finished_secondary_write(mp);
1774 if (freeblks->fb_cgwait)
1779 sync_cgs(mp, MNT_WAIT);
1780 ffs_sync_snap(mp, MNT_WAIT);
1784 if (freeblks == NULL)
1791 * Process one item on the worklist.
1794 process_worklist_item(mp, target, flags)
1799 struct worklist sentinel;
1800 struct worklist *wk;
1801 struct ufsmount *ump;
1805 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1807 * If we are being called because of a process doing a
1808 * copy-on-write, then it is not safe to write as we may
1809 * recurse into the copy-on-write routine.
1811 if (curthread->td_pflags & TDP_COWINPROGRESS)
1813 PHOLD(curproc); /* Don't let the stack go away. */
1817 sentinel.wk_mp = NULL;
1818 sentinel.wk_type = D_SENTINEL;
1819 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1820 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1821 wk = LIST_NEXT(&sentinel, wk_list)) {
1822 if (wk->wk_type == D_SENTINEL) {
1823 LIST_REMOVE(&sentinel, wk_list);
1824 LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1827 if (wk->wk_state & INPROGRESS)
1828 panic("process_worklist_item: %p already in progress.",
1830 wk->wk_state |= INPROGRESS;
1831 remove_from_worklist(wk);
1833 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1834 panic("process_worklist_item: suspended filesystem");
1835 switch (wk->wk_type) {
1837 /* removal of a directory entry */
1838 error = handle_workitem_remove(WK_DIRREM(wk), flags);
1842 /* releasing blocks and/or fragments from a file */
1843 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
1848 /* releasing a fragment when replaced as a file grows */
1849 handle_workitem_freefrag(WK_FREEFRAG(wk));
1854 /* releasing an inode when its link count drops to 0 */
1855 handle_workitem_freefile(WK_FREEFILE(wk));
1860 panic("%s_process_worklist: Unknown type %s",
1861 "softdep", TYPENAME(wk->wk_type));
1864 vn_finished_secondary_write(mp);
1867 if (++matchcnt == target)
1872 * We have to retry the worklist item later. Wake up any
1873 * waiters who may be able to complete it immediately and
1874 * add the item back to the head so we don't try to execute
1877 wk->wk_state &= ~INPROGRESS;
1879 add_to_worklist(wk, WK_HEAD);
1881 /* Sentinal could've become the tail from remove_from_worklist. */
1882 if (ump->softdep_worklist_tail == &sentinel)
1883 ump->softdep_worklist_tail =
1884 (struct worklist *)sentinel.wk_list.le_prev;
1885 LIST_REMOVE(&sentinel, wk_list);
1891 * Move dependencies from one buffer to another.
1894 softdep_move_dependencies(oldbp, newbp)
1898 struct worklist *wk, *wktail;
1899 struct ufsmount *ump;
1902 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
1904 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
1905 ("softdep_move_dependencies called on non-softdep filesystem"));
1908 ump = VFSTOUFS(wk->wk_mp);
1910 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
1911 LIST_REMOVE(wk, wk_list);
1912 if (wk->wk_type == D_BMSAFEMAP &&
1913 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
1916 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1918 LIST_INSERT_AFTER(wktail, wk, wk_list);
1927 * Purge the work list of all items associated with a particular mount point.
1930 softdep_flushworklist(oldmnt, countp, td)
1931 struct mount *oldmnt;
1935 struct vnode *devvp;
1936 struct ufsmount *ump;
1940 * Alternately flush the block device associated with the mount
1941 * point and process any dependencies that the flushing
1942 * creates. We continue until no more worklist dependencies
1947 ump = VFSTOUFS(oldmnt);
1948 devvp = ump->um_devvp;
1949 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1951 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1952 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1960 #define SU_WAITIDLE_RETRIES 20
1962 softdep_waitidle(struct mount *mp, int flags __unused)
1964 struct ufsmount *ump;
1965 struct vnode *devvp;
1970 devvp = ump->um_devvp;
1974 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
1975 ump->softdep_req = 1;
1976 KASSERT((flags & FORCECLOSE) == 0 ||
1977 ump->softdep_on_worklist == 0,
1978 ("softdep_waitidle: work added after flush"));
1979 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
1980 "softdeps", 10 * hz);
1981 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1982 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1988 ump->softdep_req = 0;
1989 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
1991 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1999 * Flush all vnodes and worklist items associated with a specified mount point.
2002 softdep_flushfiles(oldmnt, flags, td)
2003 struct mount *oldmnt;
2008 struct ufsmount *ump;
2011 int error, early, depcount, loopcnt, retry_flush_count, retry;
2014 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0,
2015 ("softdep_flushfiles called on non-softdep filesystem"));
2017 retry_flush_count = 3;
2022 * Alternately flush the vnodes associated with the mount
2023 * point and process any dependencies that the flushing
2024 * creates. In theory, this loop can happen at most twice,
2025 * but we give it a few extra just to be sure.
2027 for (; loopcnt > 0; loopcnt--) {
2029 * Do another flush in case any vnodes were brought in
2030 * as part of the cleanup operations.
2032 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
2033 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
2034 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
2036 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
2041 * If we are unmounting then it is an error to fail. If we
2042 * are simply trying to downgrade to read-only, then filesystem
2043 * activity can keep us busy forever, so we just fail with EBUSY.
2046 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2047 panic("softdep_flushfiles: looping");
2051 error = softdep_waitidle(oldmnt, flags);
2053 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2056 morework = oldmnt->mnt_nvnodelistsize > 0;
2058 ump = VFSTOUFS(oldmnt);
2060 for (i = 0; i < MAXQUOTAS; i++) {
2061 if (ump->um_quotas[i] != NULLVP)
2067 if (--retry_flush_count > 0) {
2073 MNT_IUNLOCK(oldmnt);
2082 * Structure hashing.
2084 * There are four types of structures that can be looked up:
2085 * 1) pagedep structures identified by mount point, inode number,
2086 * and logical block.
2087 * 2) inodedep structures identified by mount point and inode number.
2088 * 3) newblk structures identified by mount point and
2089 * physical block number.
2090 * 4) bmsafemap structures identified by mount point and
2091 * cylinder group number.
2093 * The "pagedep" and "inodedep" dependency structures are hashed
2094 * separately from the file blocks and inodes to which they correspond.
2095 * This separation helps when the in-memory copy of an inode or
2096 * file block must be replaced. It also obviates the need to access
2097 * an inode or file page when simply updating (or de-allocating)
2098 * dependency structures. Lookup of newblk structures is needed to
2099 * find newly allocated blocks when trying to associate them with
2100 * their allocdirect or allocindir structure.
2102 * The lookup routines optionally create and hash a new instance when
2103 * an existing entry is not found. The bmsafemap lookup routine always
2104 * allocates a new structure if an existing one is not found.
2106 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2109 * Structures and routines associated with pagedep caching.
2111 #define PAGEDEP_HASH(ump, inum, lbn) \
2112 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2115 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2116 struct pagedep_hashhead *pagedephd;
2119 struct pagedep **pagedeppp;
2121 struct pagedep *pagedep;
2123 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2124 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2125 *pagedeppp = pagedep;
2133 * Look up a pagedep. Return 1 if found, 0 otherwise.
2134 * If not found, allocate if DEPALLOC flag is passed.
2135 * Found or allocated entry is returned in pagedeppp.
2138 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2144 struct pagedep **pagedeppp;
2146 struct pagedep *pagedep;
2147 struct pagedep_hashhead *pagedephd;
2148 struct worklist *wk;
2149 struct ufsmount *ump;
2156 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2157 if (wk->wk_type == D_PAGEDEP) {
2158 *pagedeppp = WK_PAGEDEP(wk);
2163 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2164 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2166 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2167 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2170 if ((flags & DEPALLOC) == 0)
2173 pagedep = malloc(sizeof(struct pagedep),
2174 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2175 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2177 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2180 * This should never happen since we only create pagedeps
2181 * with the vnode lock held. Could be an assert.
2183 WORKITEM_FREE(pagedep, D_PAGEDEP);
2186 pagedep->pd_ino = ino;
2187 pagedep->pd_lbn = lbn;
2188 LIST_INIT(&pagedep->pd_dirremhd);
2189 LIST_INIT(&pagedep->pd_pendinghd);
2190 for (i = 0; i < DAHASHSZ; i++)
2191 LIST_INIT(&pagedep->pd_diraddhd[i]);
2192 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2193 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2194 *pagedeppp = pagedep;
2199 * Structures and routines associated with inodedep caching.
2201 #define INODEDEP_HASH(ump, inum) \
2202 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2205 inodedep_find(inodedephd, inum, inodedeppp)
2206 struct inodedep_hashhead *inodedephd;
2208 struct inodedep **inodedeppp;
2210 struct inodedep *inodedep;
2212 LIST_FOREACH(inodedep, inodedephd, id_hash)
2213 if (inum == inodedep->id_ino)
2216 *inodedeppp = inodedep;
2224 * Look up an inodedep. Return 1 if found, 0 if not found.
2225 * If not found, allocate if DEPALLOC flag is passed.
2226 * Found or allocated entry is returned in inodedeppp.
2229 inodedep_lookup(mp, inum, flags, inodedeppp)
2233 struct inodedep **inodedeppp;
2235 struct inodedep *inodedep;
2236 struct inodedep_hashhead *inodedephd;
2237 struct ufsmount *ump;
2243 inodedephd = INODEDEP_HASH(ump, inum);
2245 if (inodedep_find(inodedephd, inum, inodedeppp))
2247 if ((flags & DEPALLOC) == 0)
2250 * If the system is over its limit and our filesystem is
2251 * responsible for more than our share of that usage and
2252 * we are not in a rush, request some inodedep cleanup.
2254 if (softdep_excess_items(ump, D_INODEDEP))
2255 schedule_cleanup(mp);
2258 inodedep = malloc(sizeof(struct inodedep),
2259 M_INODEDEP, M_SOFTDEP_FLAGS);
2260 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2262 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2263 WORKITEM_FREE(inodedep, D_INODEDEP);
2266 inodedep->id_fs = fs;
2267 inodedep->id_ino = inum;
2268 inodedep->id_state = ALLCOMPLETE;
2269 inodedep->id_nlinkdelta = 0;
2270 inodedep->id_savedino1 = NULL;
2271 inodedep->id_savedsize = -1;
2272 inodedep->id_savedextsize = -1;
2273 inodedep->id_savednlink = -1;
2274 inodedep->id_bmsafemap = NULL;
2275 inodedep->id_mkdiradd = NULL;
2276 LIST_INIT(&inodedep->id_dirremhd);
2277 LIST_INIT(&inodedep->id_pendinghd);
2278 LIST_INIT(&inodedep->id_inowait);
2279 LIST_INIT(&inodedep->id_bufwait);
2280 TAILQ_INIT(&inodedep->id_inoreflst);
2281 TAILQ_INIT(&inodedep->id_inoupdt);
2282 TAILQ_INIT(&inodedep->id_newinoupdt);
2283 TAILQ_INIT(&inodedep->id_extupdt);
2284 TAILQ_INIT(&inodedep->id_newextupdt);
2285 TAILQ_INIT(&inodedep->id_freeblklst);
2286 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2287 *inodedeppp = inodedep;
2292 * Structures and routines associated with newblk caching.
2294 #define NEWBLK_HASH(ump, inum) \
2295 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2298 newblk_find(newblkhd, newblkno, flags, newblkpp)
2299 struct newblk_hashhead *newblkhd;
2300 ufs2_daddr_t newblkno;
2302 struct newblk **newblkpp;
2304 struct newblk *newblk;
2306 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2307 if (newblkno != newblk->nb_newblkno)
2310 * If we're creating a new dependency don't match those that
2311 * have already been converted to allocdirects. This is for
2314 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2327 * Look up a newblk. Return 1 if found, 0 if not found.
2328 * If not found, allocate if DEPALLOC flag is passed.
2329 * Found or allocated entry is returned in newblkpp.
2332 newblk_lookup(mp, newblkno, flags, newblkpp)
2334 ufs2_daddr_t newblkno;
2336 struct newblk **newblkpp;
2338 struct newblk *newblk;
2339 struct newblk_hashhead *newblkhd;
2340 struct ufsmount *ump;
2344 newblkhd = NEWBLK_HASH(ump, newblkno);
2345 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2347 if ((flags & DEPALLOC) == 0)
2349 if (softdep_excess_items(ump, D_NEWBLK) ||
2350 softdep_excess_items(ump, D_ALLOCDIRECT) ||
2351 softdep_excess_items(ump, D_ALLOCINDIR))
2352 schedule_cleanup(mp);
2355 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2356 M_SOFTDEP_FLAGS | M_ZERO);
2357 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2359 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2360 WORKITEM_FREE(newblk, D_NEWBLK);
2363 newblk->nb_freefrag = NULL;
2364 LIST_INIT(&newblk->nb_indirdeps);
2365 LIST_INIT(&newblk->nb_newdirblk);
2366 LIST_INIT(&newblk->nb_jwork);
2367 newblk->nb_state = ATTACHED;
2368 newblk->nb_newblkno = newblkno;
2369 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2375 * Structures and routines associated with freed indirect block caching.
2377 #define INDIR_HASH(ump, blkno) \
2378 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2381 * Lookup an indirect block in the indir hash table. The freework is
2382 * removed and potentially freed. The caller must do a blocking journal
2383 * write before writing to the blkno.
2386 indirblk_lookup(mp, blkno)
2390 struct freework *freework;
2391 struct indir_hashhead *wkhd;
2392 struct ufsmount *ump;
2395 wkhd = INDIR_HASH(ump, blkno);
2396 TAILQ_FOREACH(freework, wkhd, fw_next) {
2397 if (freework->fw_blkno != blkno)
2399 indirblk_remove(freework);
2406 * Insert an indirect block represented by freework into the indirblk
2407 * hash table so that it may prevent the block from being re-used prior
2408 * to the journal being written.
2411 indirblk_insert(freework)
2412 struct freework *freework;
2414 struct jblocks *jblocks;
2416 struct ufsmount *ump;
2418 ump = VFSTOUFS(freework->fw_list.wk_mp);
2419 jblocks = ump->softdep_jblocks;
2420 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2424 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2425 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2427 freework->fw_state &= ~DEPCOMPLETE;
2431 indirblk_remove(freework)
2432 struct freework *freework;
2434 struct ufsmount *ump;
2436 ump = VFSTOUFS(freework->fw_list.wk_mp);
2437 LIST_REMOVE(freework, fw_segs);
2438 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2439 freework->fw_state |= DEPCOMPLETE;
2440 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2441 WORKITEM_FREE(freework, D_FREEWORK);
2445 * Executed during filesystem system initialization before
2446 * mounting any filesystems.
2449 softdep_initialize()
2452 TAILQ_INIT(&softdepmounts);
2454 max_softdeps = desiredvnodes * 4;
2456 max_softdeps = desiredvnodes * 2;
2459 /* initialise bioops hack */
2460 bioops.io_start = softdep_disk_io_initiation;
2461 bioops.io_complete = softdep_disk_write_complete;
2462 bioops.io_deallocate = softdep_deallocate_dependencies;
2463 bioops.io_countdeps = softdep_count_dependencies;
2464 softdep_ast_cleanup = softdep_ast_cleanup_proc;
2466 /* Initialize the callout with an mtx. */
2467 callout_init_mtx(&softdep_callout, &lk, 0);
2471 * Executed after all filesystems have been unmounted during
2472 * filesystem module unload.
2475 softdep_uninitialize()
2478 /* clear bioops hack */
2479 bioops.io_start = NULL;
2480 bioops.io_complete = NULL;
2481 bioops.io_deallocate = NULL;
2482 bioops.io_countdeps = NULL;
2483 softdep_ast_cleanup = NULL;
2485 callout_drain(&softdep_callout);
2489 * Called at mount time to notify the dependency code that a
2490 * filesystem wishes to use it.
2493 softdep_mount(devvp, mp, fs, cred)
2494 struct vnode *devvp;
2499 struct csum_total cstotal;
2500 struct mount_softdeps *sdp;
2501 struct ufsmount *ump;
2507 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2510 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2511 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2512 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2513 MNTK_SOFTDEP | MNTK_NOASYNC;
2516 ump->um_softdep = sdp;
2518 rw_init(LOCK_PTR(ump), "per-fs softdep");
2520 LIST_INIT(&ump->softdep_workitem_pending);
2521 LIST_INIT(&ump->softdep_journal_pending);
2522 TAILQ_INIT(&ump->softdep_unlinked);
2523 LIST_INIT(&ump->softdep_dirtycg);
2524 ump->softdep_worklist_tail = NULL;
2525 ump->softdep_on_worklist = 0;
2526 ump->softdep_deps = 0;
2527 LIST_INIT(&ump->softdep_mkdirlisthd);
2528 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
2529 &ump->pagedep_hash_size);
2530 ump->pagedep_nextclean = 0;
2531 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP,
2532 &ump->inodedep_hash_size);
2533 ump->inodedep_nextclean = 0;
2534 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK,
2535 &ump->newblk_hash_size);
2536 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP,
2537 &ump->bmsafemap_hash_size);
2538 i = 1 << (ffs(desiredvnodes / 10) - 1);
2539 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead),
2540 M_FREEWORK, M_WAITOK);
2541 ump->indir_hash_size = i - 1;
2542 for (i = 0; i <= ump->indir_hash_size; i++)
2543 TAILQ_INIT(&ump->indir_hashtbl[i]);
2545 for (i = 0; i <= D_LAST; i++)
2546 LIST_INIT(&ump->softdep_alldeps[i]);
2548 ACQUIRE_GBLLOCK(&lk);
2549 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2551 if ((fs->fs_flags & FS_SUJ) &&
2552 (error = journal_mount(mp, fs, cred)) != 0) {
2553 printf("Failed to start journal: %d\n", error);
2554 softdep_unmount(mp);
2558 * Start our flushing thread in the bufdaemon process.
2561 ump->softdep_flags |= FLUSH_STARTING;
2563 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2564 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2565 mp->mnt_stat.f_mntonname);
2567 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2568 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2573 * When doing soft updates, the counters in the
2574 * superblock may have gotten out of sync. Recomputation
2575 * can take a long time and can be deferred for background
2576 * fsck. However, the old behavior of scanning the cylinder
2577 * groups and recalculating them at mount time is available
2578 * by setting vfs.ffs.compute_summary_at_mount to one.
2580 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2582 bzero(&cstotal, sizeof cstotal);
2583 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2584 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2585 fs->fs_cgsize, cred, &bp)) != 0) {
2587 softdep_unmount(mp);
2590 cgp = (struct cg *)bp->b_data;
2591 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2592 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2593 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2594 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2595 fs->fs_cs(fs, cyl) = cgp->cg_cs;
2599 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2600 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2602 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2610 struct ufsmount *ump;
2615 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
2616 ("softdep_unmount called on non-softdep filesystem"));
2619 mp->mnt_flag &= ~MNT_SOFTDEP;
2620 if (MOUNTEDSUJ(mp) == 0) {
2623 mp->mnt_flag &= ~MNT_SUJ;
2625 journal_unmount(ump);
2628 * Shut down our flushing thread. Check for NULL is if
2629 * softdep_mount errors out before the thread has been created.
2631 if (ump->softdep_flushtd != NULL) {
2633 ump->softdep_flags |= FLUSH_EXIT;
2634 wakeup(&ump->softdep_flushtd);
2635 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP,
2637 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2638 ("Thread shutdown failed"));
2641 * Free up our resources.
2643 ACQUIRE_GBLLOCK(&lk);
2644 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next);
2646 rw_destroy(LOCK_PTR(ump));
2647 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size);
2648 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size);
2649 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size);
2650 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP,
2651 ump->bmsafemap_hash_size);
2652 free(ump->indir_hashtbl, M_FREEWORK);
2654 for (i = 0; i <= D_LAST; i++) {
2655 KASSERT(ump->softdep_curdeps[i] == 0,
2656 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2657 TYPENAME(i), ump->softdep_curdeps[i]));
2658 KASSERT(LIST_EMPTY(&ump->softdep_alldeps[i]),
2659 ("Unmount %s: Dep type %s not empty (%p)", ump->um_fs->fs_fsmnt,
2660 TYPENAME(i), LIST_FIRST(&ump->softdep_alldeps[i])));
2663 free(ump->um_softdep, M_MOUNTDATA);
2666 static struct jblocks *
2667 jblocks_create(void)
2669 struct jblocks *jblocks;
2671 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2672 TAILQ_INIT(&jblocks->jb_segs);
2673 jblocks->jb_avail = 10;
2674 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2675 M_JBLOCKS, M_WAITOK | M_ZERO);
2681 jblocks_alloc(jblocks, bytes, actual)
2682 struct jblocks *jblocks;
2687 struct jextent *jext;
2691 blocks = bytes / DEV_BSIZE;
2692 jext = &jblocks->jb_extent[jblocks->jb_head];
2693 freecnt = jext->je_blocks - jblocks->jb_off;
2695 jblocks->jb_off = 0;
2696 if (++jblocks->jb_head > jblocks->jb_used)
2697 jblocks->jb_head = 0;
2698 jext = &jblocks->jb_extent[jblocks->jb_head];
2699 freecnt = jext->je_blocks;
2701 if (freecnt > blocks)
2703 *actual = freecnt * DEV_BSIZE;
2704 daddr = jext->je_daddr + jblocks->jb_off;
2705 jblocks->jb_off += freecnt;
2706 jblocks->jb_free -= freecnt;
2712 jblocks_free(jblocks, mp, bytes)
2713 struct jblocks *jblocks;
2718 LOCK_OWNED(VFSTOUFS(mp));
2719 jblocks->jb_free += bytes / DEV_BSIZE;
2720 if (jblocks->jb_suspended)
2721 worklist_speedup(mp);
2726 jblocks_destroy(jblocks)
2727 struct jblocks *jblocks;
2730 if (jblocks->jb_extent)
2731 free(jblocks->jb_extent, M_JBLOCKS);
2732 free(jblocks, M_JBLOCKS);
2736 jblocks_add(jblocks, daddr, blocks)
2737 struct jblocks *jblocks;
2741 struct jextent *jext;
2743 jblocks->jb_blocks += blocks;
2744 jblocks->jb_free += blocks;
2745 jext = &jblocks->jb_extent[jblocks->jb_used];
2746 /* Adding the first block. */
2747 if (jext->je_daddr == 0) {
2748 jext->je_daddr = daddr;
2749 jext->je_blocks = blocks;
2752 /* Extending the last extent. */
2753 if (jext->je_daddr + jext->je_blocks == daddr) {
2754 jext->je_blocks += blocks;
2757 /* Adding a new extent. */
2758 if (++jblocks->jb_used == jblocks->jb_avail) {
2759 jblocks->jb_avail *= 2;
2760 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2761 M_JBLOCKS, M_WAITOK | M_ZERO);
2762 memcpy(jext, jblocks->jb_extent,
2763 sizeof(struct jextent) * jblocks->jb_used);
2764 free(jblocks->jb_extent, M_JBLOCKS);
2765 jblocks->jb_extent = jext;
2767 jext = &jblocks->jb_extent[jblocks->jb_used];
2768 jext->je_daddr = daddr;
2769 jext->je_blocks = blocks;
2774 softdep_journal_lookup(mp, vpp)
2778 struct componentname cnp;
2783 error = VFS_VGET(mp, UFS_ROOTINO, LK_EXCLUSIVE, &dvp);
2786 bzero(&cnp, sizeof(cnp));
2787 cnp.cn_nameiop = LOOKUP;
2788 cnp.cn_flags = ISLASTCN;
2789 cnp.cn_thread = curthread;
2790 cnp.cn_cred = curthread->td_ucred;
2791 cnp.cn_pnbuf = SUJ_FILE;
2792 cnp.cn_nameptr = SUJ_FILE;
2793 cnp.cn_namelen = strlen(SUJ_FILE);
2794 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2798 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2803 * Open and verify the journal file.
2806 journal_mount(mp, fs, cred)
2811 struct jblocks *jblocks;
2812 struct ufsmount *ump;
2821 ump->softdep_journal_tail = NULL;
2822 ump->softdep_on_journal = 0;
2823 ump->softdep_accdeps = 0;
2824 ump->softdep_req = 0;
2825 ump->softdep_jblocks = NULL;
2826 error = softdep_journal_lookup(mp, &vp);
2828 printf("Failed to find journal. Use tunefs to create one\n");
2832 if (ip->i_size < SUJ_MIN) {
2836 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */
2837 jblocks = jblocks_create();
2838 for (i = 0; i < bcount; i++) {
2839 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
2842 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
2845 jblocks_destroy(jblocks);
2848 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */
2849 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
2850 ump->softdep_jblocks = jblocks;
2854 mp->mnt_flag |= MNT_SUJ;
2855 mp->mnt_flag &= ~MNT_SOFTDEP;
2858 * Only validate the journal contents if the
2859 * filesystem is clean, otherwise we write the logs
2860 * but they'll never be used. If the filesystem was
2861 * still dirty when we mounted it the journal is
2862 * invalid and a new journal can only be valid if it
2863 * starts from a clean mount.
2866 DIP_SET(ip, i_modrev, fs->fs_mtime);
2867 ip->i_flags |= IN_MODIFIED;
2876 journal_unmount(ump)
2877 struct ufsmount *ump;
2880 if (ump->softdep_jblocks)
2881 jblocks_destroy(ump->softdep_jblocks);
2882 ump->softdep_jblocks = NULL;
2886 * Called when a journal record is ready to be written. Space is allocated
2887 * and the journal entry is created when the journal is flushed to stable
2892 struct worklist *wk;
2894 struct ufsmount *ump;
2896 ump = VFSTOUFS(wk->wk_mp);
2898 if (wk->wk_state & ONWORKLIST)
2899 panic("add_to_journal: %s(0x%X) already on list",
2900 TYPENAME(wk->wk_type), wk->wk_state);
2901 wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
2902 if (LIST_EMPTY(&ump->softdep_journal_pending)) {
2903 ump->softdep_jblocks->jb_age = ticks;
2904 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
2906 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
2907 ump->softdep_journal_tail = wk;
2908 ump->softdep_on_journal += 1;
2912 * Remove an arbitrary item for the journal worklist maintain the tail
2913 * pointer. This happens when a new operation obviates the need to
2914 * journal an old operation.
2917 remove_from_journal(wk)
2918 struct worklist *wk;
2920 struct ufsmount *ump;
2922 ump = VFSTOUFS(wk->wk_mp);
2926 struct worklist *wkn;
2928 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
2932 panic("remove_from_journal: %p is not in journal", wk);
2936 * We emulate a TAILQ to save space in most structures which do not
2937 * require TAILQ semantics. Here we must update the tail position
2938 * when removing the tail which is not the final entry. This works
2939 * only if the worklist linkage are at the beginning of the structure.
2941 if (ump->softdep_journal_tail == wk)
2942 ump->softdep_journal_tail =
2943 (struct worklist *)wk->wk_list.le_prev;
2944 WORKLIST_REMOVE(wk);
2945 ump->softdep_on_journal -= 1;
2949 * Check for journal space as well as dependency limits so the prelink
2950 * code can throttle both journaled and non-journaled filesystems.
2951 * Threshold is 0 for low and 1 for min.
2954 journal_space(ump, thresh)
2955 struct ufsmount *ump;
2958 struct jblocks *jblocks;
2961 jblocks = ump->softdep_jblocks;
2962 if (jblocks == NULL)
2965 * We use a tighter restriction here to prevent request_cleanup()
2966 * running in threads from running into locks we currently hold.
2967 * We have to be over the limit and our filesystem has to be
2968 * responsible for more than our share of that usage.
2970 limit = (max_softdeps / 10) * 9;
2971 if (dep_current[D_INODEDEP] > limit &&
2972 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
2975 thresh = jblocks->jb_min;
2977 thresh = jblocks->jb_low;
2978 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
2979 avail = jblocks->jb_free - avail;
2981 return (avail > thresh);
2985 journal_suspend(ump)
2986 struct ufsmount *ump;
2988 struct jblocks *jblocks;
2993 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0)
2996 jblocks = ump->softdep_jblocks;
3000 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
3002 mp->mnt_kern_flag |= MNTK_SUSPEND;
3003 mp->mnt_susp_owner = ump->softdep_flushtd;
3006 jblocks->jb_suspended = 1;
3013 journal_unsuspend(struct ufsmount *ump)
3015 struct jblocks *jblocks;
3019 jblocks = ump->softdep_jblocks;
3021 if (jblocks != NULL && jblocks->jb_suspended &&
3022 journal_space(ump, jblocks->jb_min)) {
3023 jblocks->jb_suspended = 0;
3025 mp->mnt_susp_owner = curthread;
3026 vfs_write_resume(mp, 0);
3034 * Called before any allocation function to be certain that there is
3035 * sufficient space in the journal prior to creating any new records.
3036 * Since in the case of block allocation we may have multiple locked
3037 * buffers at the time of the actual allocation we can not block
3038 * when the journal records are created. Doing so would create a deadlock
3039 * if any of these buffers needed to be flushed to reclaim space. Instead
3040 * we require a sufficiently large amount of available space such that
3041 * each thread in the system could have passed this allocation check and
3042 * still have sufficient free space. With 20% of a minimum journal size
3043 * of 1MB we have 6553 records available.
3046 softdep_prealloc(vp, waitok)
3050 struct ufsmount *ump;
3052 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
3053 ("softdep_prealloc called on non-softdep filesystem"));
3055 * Nothing to do if we are not running journaled soft updates.
3056 * If we currently hold the snapshot lock, we must avoid
3057 * handling other resources that could cause deadlock. Do not
3058 * touch quotas vnode since it is typically recursed with
3059 * other vnode locks held.
3061 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)) ||
3062 (vp->v_vflag & VV_SYSTEM) != 0)
3064 ump = VFSTOUFS(vp->v_mount);
3066 if (journal_space(ump, 0)) {
3072 if (waitok == MNT_NOWAIT)
3075 * Attempt to sync this vnode once to flush any journal
3076 * work attached to it.
3078 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3079 ffs_syncvnode(vp, waitok, 0);
3081 process_removes(vp);
3082 process_truncates(vp);
3083 if (journal_space(ump, 0) == 0) {
3084 softdep_speedup(ump);
3085 if (journal_space(ump, 1) == 0)
3086 journal_suspend(ump);
3094 * Before adjusting a link count on a vnode verify that we have sufficient
3095 * journal space. If not, process operations that depend on the currently
3096 * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3097 * and softdep flush threads can not acquire these locks to reclaim space.
3100 softdep_prelink(dvp, vp)
3104 struct ufsmount *ump;
3106 ump = VFSTOUFS(dvp->v_mount);
3109 * Nothing to do if we have sufficient journal space.
3110 * If we currently hold the snapshot lock, we must avoid
3111 * handling other resources that could cause deadlock.
3113 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp))))
3118 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3119 ffs_syncvnode(dvp, MNT_WAIT, 0);
3121 /* Process vp before dvp as it may create .. removes. */
3123 process_removes(vp);
3124 process_truncates(vp);
3126 process_removes(dvp);
3127 process_truncates(dvp);
3128 softdep_speedup(ump);
3129 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3130 if (journal_space(ump, 0) == 0) {
3131 softdep_speedup(ump);
3132 if (journal_space(ump, 1) == 0)
3133 journal_suspend(ump);
3138 jseg_write(ump, jseg, data)
3139 struct ufsmount *ump;
3143 struct jsegrec *rec;
3145 rec = (struct jsegrec *)data;
3146 rec->jsr_seq = jseg->js_seq;
3147 rec->jsr_oldest = jseg->js_oldseq;
3148 rec->jsr_cnt = jseg->js_cnt;
3149 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3151 rec->jsr_time = ump->um_fs->fs_mtime;
3155 inoref_write(inoref, jseg, rec)
3156 struct inoref *inoref;
3158 struct jrefrec *rec;
3161 inoref->if_jsegdep->jd_seg = jseg;
3162 rec->jr_ino = inoref->if_ino;
3163 rec->jr_parent = inoref->if_parent;
3164 rec->jr_nlink = inoref->if_nlink;
3165 rec->jr_mode = inoref->if_mode;
3166 rec->jr_diroff = inoref->if_diroff;
3170 jaddref_write(jaddref, jseg, data)
3171 struct jaddref *jaddref;
3175 struct jrefrec *rec;
3177 rec = (struct jrefrec *)data;
3178 rec->jr_op = JOP_ADDREF;
3179 inoref_write(&jaddref->ja_ref, jseg, rec);
3183 jremref_write(jremref, jseg, data)
3184 struct jremref *jremref;
3188 struct jrefrec *rec;
3190 rec = (struct jrefrec *)data;
3191 rec->jr_op = JOP_REMREF;
3192 inoref_write(&jremref->jr_ref, jseg, rec);
3196 jmvref_write(jmvref, jseg, data)
3197 struct jmvref *jmvref;
3203 rec = (struct jmvrec *)data;
3204 rec->jm_op = JOP_MVREF;
3205 rec->jm_ino = jmvref->jm_ino;
3206 rec->jm_parent = jmvref->jm_parent;
3207 rec->jm_oldoff = jmvref->jm_oldoff;
3208 rec->jm_newoff = jmvref->jm_newoff;
3212 jnewblk_write(jnewblk, jseg, data)
3213 struct jnewblk *jnewblk;
3217 struct jblkrec *rec;
3219 jnewblk->jn_jsegdep->jd_seg = jseg;
3220 rec = (struct jblkrec *)data;
3221 rec->jb_op = JOP_NEWBLK;
3222 rec->jb_ino = jnewblk->jn_ino;
3223 rec->jb_blkno = jnewblk->jn_blkno;
3224 rec->jb_lbn = jnewblk->jn_lbn;
3225 rec->jb_frags = jnewblk->jn_frags;
3226 rec->jb_oldfrags = jnewblk->jn_oldfrags;
3230 jfreeblk_write(jfreeblk, jseg, data)
3231 struct jfreeblk *jfreeblk;
3235 struct jblkrec *rec;
3237 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3238 rec = (struct jblkrec *)data;
3239 rec->jb_op = JOP_FREEBLK;
3240 rec->jb_ino = jfreeblk->jf_ino;
3241 rec->jb_blkno = jfreeblk->jf_blkno;
3242 rec->jb_lbn = jfreeblk->jf_lbn;
3243 rec->jb_frags = jfreeblk->jf_frags;
3244 rec->jb_oldfrags = 0;
3248 jfreefrag_write(jfreefrag, jseg, data)
3249 struct jfreefrag *jfreefrag;
3253 struct jblkrec *rec;
3255 jfreefrag->fr_jsegdep->jd_seg = jseg;
3256 rec = (struct jblkrec *)data;
3257 rec->jb_op = JOP_FREEBLK;
3258 rec->jb_ino = jfreefrag->fr_ino;
3259 rec->jb_blkno = jfreefrag->fr_blkno;
3260 rec->jb_lbn = jfreefrag->fr_lbn;
3261 rec->jb_frags = jfreefrag->fr_frags;
3262 rec->jb_oldfrags = 0;
3266 jtrunc_write(jtrunc, jseg, data)
3267 struct jtrunc *jtrunc;
3271 struct jtrncrec *rec;
3273 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3274 rec = (struct jtrncrec *)data;
3275 rec->jt_op = JOP_TRUNC;
3276 rec->jt_ino = jtrunc->jt_ino;
3277 rec->jt_size = jtrunc->jt_size;
3278 rec->jt_extsize = jtrunc->jt_extsize;
3282 jfsync_write(jfsync, jseg, data)
3283 struct jfsync *jfsync;
3287 struct jtrncrec *rec;
3289 rec = (struct jtrncrec *)data;
3290 rec->jt_op = JOP_SYNC;
3291 rec->jt_ino = jfsync->jfs_ino;
3292 rec->jt_size = jfsync->jfs_size;
3293 rec->jt_extsize = jfsync->jfs_extsize;
3297 softdep_flushjournal(mp)
3300 struct jblocks *jblocks;
3301 struct ufsmount *ump;
3303 if (MOUNTEDSUJ(mp) == 0)
3306 jblocks = ump->softdep_jblocks;
3308 while (ump->softdep_on_journal) {
3309 jblocks->jb_needseg = 1;
3310 softdep_process_journal(mp, NULL, MNT_WAIT);
3315 static void softdep_synchronize_completed(struct bio *);
3316 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3319 softdep_synchronize_completed(bp)
3322 struct jseg *oldest;
3324 struct ufsmount *ump;
3327 * caller1 marks the last segment written before we issued the
3328 * synchronize cache.
3330 jseg = bp->bio_caller1;
3335 ump = VFSTOUFS(jseg->js_list.wk_mp);
3339 * Mark all the journal entries waiting on the synchronize cache
3340 * as completed so they may continue on.
3342 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3343 jseg->js_state |= COMPLETE;
3345 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3348 * Restart deferred journal entry processing from the oldest
3352 complete_jsegs(oldest);
3359 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3360 * barriers. The journal must be written prior to any blocks that depend
3361 * on it and the journal can not be released until the blocks have be
3362 * written. This code handles both barriers simultaneously.
3365 softdep_synchronize(bp, ump, caller1)
3367 struct ufsmount *ump;
3371 bp->bio_cmd = BIO_FLUSH;
3372 bp->bio_flags |= BIO_ORDERED;
3373 bp->bio_data = NULL;
3374 bp->bio_offset = ump->um_cp->provider->mediasize;
3376 bp->bio_done = softdep_synchronize_completed;
3377 bp->bio_caller1 = caller1;
3378 g_io_request(bp, ump->um_cp);
3382 * Flush some journal records to disk.
3385 softdep_process_journal(mp, needwk, flags)
3387 struct worklist *needwk;
3390 struct jblocks *jblocks;
3391 struct ufsmount *ump;
3392 struct worklist *wk;
3400 int jrecmin; /* Minimum records per block. */
3401 int jrecmax; /* Maximum records per block. */
3407 if (MOUNTEDSUJ(mp) == 0)
3409 shouldflush = softdep_flushcache;
3415 jblocks = ump->softdep_jblocks;
3416 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3418 * We write anywhere between a disk block and fs block. The upper
3419 * bound is picked to prevent buffer cache fragmentation and limit
3420 * processing time per I/O.
3422 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3423 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3426 cnt = ump->softdep_on_journal;
3428 * Criteria for writing a segment:
3429 * 1) We have a full block.
3430 * 2) We're called from jwait() and haven't found the
3432 * 3) Always write if needseg is set.
3433 * 4) If we are called from process_worklist and have
3434 * not yet written anything we write a partial block
3435 * to enforce a 1 second maximum latency on journal
3438 if (cnt < (jrecmax - 1) && needwk == NULL &&
3439 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3443 * Verify some free journal space. softdep_prealloc() should
3444 * guarantee that we don't run out so this is indicative of
3445 * a problem with the flow control. Try to recover
3446 * gracefully in any event.
3448 while (jblocks->jb_free == 0) {
3449 if (flags != MNT_WAIT)
3451 printf("softdep: Out of journal space!\n");
3452 softdep_speedup(ump);
3453 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3456 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3457 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3458 LIST_INIT(&jseg->js_entries);
3459 LIST_INIT(&jseg->js_indirs);
3460 jseg->js_state = ATTACHED;
3461 if (shouldflush == 0)
3462 jseg->js_state |= COMPLETE;
3463 else if (bio == NULL)
3464 bio = g_alloc_bio();
3465 jseg->js_jblocks = jblocks;
3466 bp = geteblk(fs->fs_bsize, 0);
3469 * If there was a race while we were allocating the block
3470 * and jseg the entry we care about was likely written.
3471 * We bail out in both the WAIT and NOWAIT case and assume
3472 * the caller will loop if the entry it cares about is
3475 cnt = ump->softdep_on_journal;
3476 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3477 bp->b_flags |= B_INVAL | B_NOCACHE;
3478 WORKITEM_FREE(jseg, D_JSEG);
3485 * Calculate the disk block size required for the available
3486 * records rounded to the min size.
3490 else if (cnt < jrecmax)
3491 size = howmany(cnt, jrecmin) * devbsize;
3493 size = fs->fs_bsize;
3495 * Allocate a disk block for this journal data and account
3496 * for truncation of the requested size if enough contiguous
3497 * space was not available.
3499 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3500 bp->b_lblkno = bp->b_blkno;
3501 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3502 bp->b_bcount = size;
3503 bp->b_flags &= ~B_INVAL;
3504 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3506 * Initialize our jseg with cnt records. Assign the next
3507 * sequence number to it and link it in-order.
3509 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3512 jseg->js_refs = cnt + 1; /* Self ref. */
3513 jseg->js_size = size;
3514 jseg->js_seq = jblocks->jb_nextseq++;
3515 if (jblocks->jb_oldestseg == NULL)
3516 jblocks->jb_oldestseg = jseg;
3517 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3518 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3519 if (jblocks->jb_writeseg == NULL)
3520 jblocks->jb_writeseg = jseg;
3522 * Start filling in records from the pending list.
3528 * Always put a header on the first block.
3529 * XXX As with below, there might not be a chance to get
3530 * into the loop. Ensure that something valid is written.
3532 jseg_write(ump, jseg, data);
3534 data = bp->b_data + off;
3537 * XXX Something is wrong here. There's no work to do,
3538 * but we need to perform and I/O and allow it to complete
3541 if (LIST_EMPTY(&ump->softdep_journal_pending))
3542 stat_emptyjblocks++;
3544 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3548 /* Place a segment header on every device block. */
3549 if ((off % devbsize) == 0) {
3550 jseg_write(ump, jseg, data);
3552 data = bp->b_data + off;
3556 remove_from_journal(wk);
3557 wk->wk_state |= INPROGRESS;
3558 WORKLIST_INSERT(&jseg->js_entries, wk);
3559 switch (wk->wk_type) {
3561 jaddref_write(WK_JADDREF(wk), jseg, data);
3564 jremref_write(WK_JREMREF(wk), jseg, data);
3567 jmvref_write(WK_JMVREF(wk), jseg, data);
3570 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3573 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3576 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3579 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3582 jfsync_write(WK_JFSYNC(wk), jseg, data);
3585 panic("process_journal: Unknown type %s",
3586 TYPENAME(wk->wk_type));
3590 data = bp->b_data + off;
3594 /* Clear any remaining space so we don't leak kernel data */
3596 bzero(data, size - off);
3599 * Write this one buffer and continue.
3602 jblocks->jb_needseg = 0;
3603 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3605 pbgetvp(ump->um_devvp, bp);
3607 * We only do the blocking wait once we find the journal
3608 * entry we're looking for.
3610 if (needwk == NULL && flags == MNT_WAIT)
3617 * If we wrote a segment issue a synchronize cache so the journal
3618 * is reflected on disk before the data is written. Since reclaiming
3619 * journal space also requires writing a journal record this
3620 * process also enforces a barrier before reclamation.
3622 if (segwritten && shouldflush) {
3623 softdep_synchronize(bio, ump,
3624 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3628 * If we've suspended the filesystem because we ran out of journal
3629 * space either try to sync it here to make some progress or
3630 * unsuspend it if we already have.
3632 if (flags == 0 && jblocks->jb_suspended) {
3633 if (journal_unsuspend(ump))
3636 VFS_SYNC(mp, MNT_NOWAIT);
3637 ffs_sbupdate(ump, MNT_WAIT, 0);
3643 * Complete a jseg, allowing all dependencies awaiting journal writes
3644 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3645 * structures so that the journal segment can be freed to reclaim space.
3651 struct worklist *wk;
3652 struct jmvref *jmvref;
3657 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3658 WORKLIST_REMOVE(wk);
3659 wk->wk_state &= ~INPROGRESS;
3660 wk->wk_state |= COMPLETE;
3661 KASSERT(i++ < jseg->js_cnt,
3662 ("handle_written_jseg: overflow %d >= %d",
3663 i - 1, jseg->js_cnt));
3664 switch (wk->wk_type) {
3666 handle_written_jaddref(WK_JADDREF(wk));
3669 handle_written_jremref(WK_JREMREF(wk));
3672 rele_jseg(jseg); /* No jsegdep. */
3673 jmvref = WK_JMVREF(wk);
3674 LIST_REMOVE(jmvref, jm_deps);
3675 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3676 free_pagedep(jmvref->jm_pagedep);
3677 WORKITEM_FREE(jmvref, D_JMVREF);
3680 handle_written_jnewblk(WK_JNEWBLK(wk));
3683 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3686 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3689 rele_jseg(jseg); /* No jsegdep. */
3690 WORKITEM_FREE(wk, D_JFSYNC);
3693 handle_written_jfreefrag(WK_JFREEFRAG(wk));
3696 panic("handle_written_jseg: Unknown type %s",
3697 TYPENAME(wk->wk_type));
3701 /* Release the self reference so the structure may be freed. */
3706 * Determine which jsegs are ready for completion processing. Waits for
3707 * synchronize cache to complete as well as forcing in-order completion
3708 * of journal entries.
3711 complete_jsegs(jseg)
3714 struct jblocks *jblocks;
3717 jblocks = jseg->js_jblocks;
3719 * Don't allow out of order completions. If this isn't the first
3720 * block wait for it to write before we're done.
3722 if (jseg != jblocks->jb_writeseg)
3724 /* Iterate through available jsegs processing their entries. */
3725 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
3726 jblocks->jb_oldestwrseq = jseg->js_oldseq;
3727 jsegn = TAILQ_NEXT(jseg, js_next);
3728 complete_jseg(jseg);
3731 jblocks->jb_writeseg = jseg;
3733 * Attempt to free jsegs now that oldestwrseq may have advanced.
3735 free_jsegs(jblocks);
3739 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
3740 * the final completions.
3743 handle_written_jseg(jseg, bp)
3748 if (jseg->js_refs == 0)
3749 panic("handle_written_jseg: No self-reference on %p", jseg);
3750 jseg->js_state |= DEPCOMPLETE;
3752 * We'll never need this buffer again, set flags so it will be
3755 bp->b_flags |= B_INVAL | B_NOCACHE;
3757 complete_jsegs(jseg);
3760 static inline struct jsegdep *
3762 struct inoref *inoref;
3764 struct jsegdep *jsegdep;
3766 jsegdep = inoref->if_jsegdep;
3767 inoref->if_jsegdep = NULL;
3773 * Called once a jremref has made it to stable store. The jremref is marked
3774 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
3775 * for the jremref to complete will be awoken by free_jremref.
3778 handle_written_jremref(jremref)
3779 struct jremref *jremref;
3781 struct inodedep *inodedep;
3782 struct jsegdep *jsegdep;
3783 struct dirrem *dirrem;
3785 /* Grab the jsegdep. */
3786 jsegdep = inoref_jseg(&jremref->jr_ref);
3788 * Remove us from the inoref list.
3790 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
3792 panic("handle_written_jremref: Lost inodedep");
3793 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
3795 * Complete the dirrem.
3797 dirrem = jremref->jr_dirrem;
3798 jremref->jr_dirrem = NULL;
3799 LIST_REMOVE(jremref, jr_deps);
3800 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
3801 jwork_insert(&dirrem->dm_jwork, jsegdep);
3802 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
3803 (dirrem->dm_state & COMPLETE) != 0)
3804 add_to_worklist(&dirrem->dm_list, 0);
3805 free_jremref(jremref);
3809 * Called once a jaddref has made it to stable store. The dependency is
3810 * marked complete and any dependent structures are added to the inode
3811 * bufwait list to be completed as soon as it is written. If a bitmap write
3812 * depends on this entry we move the inode into the inodedephd of the
3813 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
3816 handle_written_jaddref(jaddref)
3817 struct jaddref *jaddref;
3819 struct jsegdep *jsegdep;
3820 struct inodedep *inodedep;
3821 struct diradd *diradd;
3822 struct mkdir *mkdir;
3824 /* Grab the jsegdep. */
3825 jsegdep = inoref_jseg(&jaddref->ja_ref);
3828 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
3830 panic("handle_written_jaddref: Lost inodedep.");
3831 if (jaddref->ja_diradd == NULL)
3832 panic("handle_written_jaddref: No dependency");
3833 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
3834 diradd = jaddref->ja_diradd;
3835 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
3836 } else if (jaddref->ja_state & MKDIR_PARENT) {
3837 mkdir = jaddref->ja_mkdir;
3838 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
3839 } else if (jaddref->ja_state & MKDIR_BODY)
3840 mkdir = jaddref->ja_mkdir;
3842 panic("handle_written_jaddref: Unknown dependency %p",
3843 jaddref->ja_diradd);
3844 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
3846 * Remove us from the inode list.
3848 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
3850 * The mkdir may be waiting on the jaddref to clear before freeing.
3853 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
3854 ("handle_written_jaddref: Incorrect type for mkdir %s",
3855 TYPENAME(mkdir->md_list.wk_type)));
3856 mkdir->md_jaddref = NULL;
3857 diradd = mkdir->md_diradd;
3858 mkdir->md_state |= DEPCOMPLETE;
3859 complete_mkdir(mkdir);
3861 jwork_insert(&diradd->da_jwork, jsegdep);
3862 if (jaddref->ja_state & NEWBLOCK) {
3863 inodedep->id_state |= ONDEPLIST;
3864 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
3867 free_jaddref(jaddref);
3871 * Called once a jnewblk journal is written. The allocdirect or allocindir
3872 * is placed in the bmsafemap to await notification of a written bitmap. If
3873 * the operation was canceled we add the segdep to the appropriate
3874 * dependency to free the journal space once the canceling operation
3878 handle_written_jnewblk(jnewblk)
3879 struct jnewblk *jnewblk;
3881 struct bmsafemap *bmsafemap;
3882 struct freefrag *freefrag;
3883 struct freework *freework;
3884 struct jsegdep *jsegdep;
3885 struct newblk *newblk;
3887 /* Grab the jsegdep. */
3888 jsegdep = jnewblk->jn_jsegdep;
3889 jnewblk->jn_jsegdep = NULL;
3890 if (jnewblk->jn_dep == NULL)
3891 panic("handle_written_jnewblk: No dependency for the segdep.");
3892 switch (jnewblk->jn_dep->wk_type) {
3897 * Add the written block to the bmsafemap so it can
3898 * be notified when the bitmap is on disk.
3900 newblk = WK_NEWBLK(jnewblk->jn_dep);
3901 newblk->nb_jnewblk = NULL;
3902 if ((newblk->nb_state & GOINGAWAY) == 0) {
3903 bmsafemap = newblk->nb_bmsafemap;
3904 newblk->nb_state |= ONDEPLIST;
3905 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
3908 jwork_insert(&newblk->nb_jwork, jsegdep);
3912 * A newblock being removed by a freefrag when replaced by
3915 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
3916 freefrag->ff_jdep = NULL;
3917 jwork_insert(&freefrag->ff_jwork, jsegdep);
3921 * A direct block was removed by truncate.
3923 freework = WK_FREEWORK(jnewblk->jn_dep);
3924 freework->fw_jnewblk = NULL;
3925 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
3928 panic("handle_written_jnewblk: Unknown type %d.",
3929 jnewblk->jn_dep->wk_type);
3931 jnewblk->jn_dep = NULL;
3932 free_jnewblk(jnewblk);
3936 * Cancel a jfreefrag that won't be needed, probably due to colliding with
3937 * an in-flight allocation that has not yet been committed. Divorce us
3938 * from the freefrag and mark it DEPCOMPLETE so that it may be added
3942 cancel_jfreefrag(jfreefrag)
3943 struct jfreefrag *jfreefrag;
3945 struct freefrag *freefrag;
3947 if (jfreefrag->fr_jsegdep) {
3948 free_jsegdep(jfreefrag->fr_jsegdep);
3949 jfreefrag->fr_jsegdep = NULL;
3951 freefrag = jfreefrag->fr_freefrag;
3952 jfreefrag->fr_freefrag = NULL;
3953 free_jfreefrag(jfreefrag);
3954 freefrag->ff_state |= DEPCOMPLETE;
3955 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
3959 * Free a jfreefrag when the parent freefrag is rendered obsolete.
3962 free_jfreefrag(jfreefrag)
3963 struct jfreefrag *jfreefrag;
3966 if (jfreefrag->fr_state & INPROGRESS)
3967 WORKLIST_REMOVE(&jfreefrag->fr_list);
3968 else if (jfreefrag->fr_state & ONWORKLIST)
3969 remove_from_journal(&jfreefrag->fr_list);
3970 if (jfreefrag->fr_freefrag != NULL)
3971 panic("free_jfreefrag: Still attached to a freefrag.");
3972 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
3976 * Called when the journal write for a jfreefrag completes. The parent
3977 * freefrag is added to the worklist if this completes its dependencies.
3980 handle_written_jfreefrag(jfreefrag)
3981 struct jfreefrag *jfreefrag;
3983 struct jsegdep *jsegdep;
3984 struct freefrag *freefrag;
3986 /* Grab the jsegdep. */
3987 jsegdep = jfreefrag->fr_jsegdep;
3988 jfreefrag->fr_jsegdep = NULL;
3989 freefrag = jfreefrag->fr_freefrag;
3990 if (freefrag == NULL)
3991 panic("handle_written_jfreefrag: No freefrag.");
3992 freefrag->ff_state |= DEPCOMPLETE;
3993 freefrag->ff_jdep = NULL;
3994 jwork_insert(&freefrag->ff_jwork, jsegdep);
3995 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
3996 add_to_worklist(&freefrag->ff_list, 0);
3997 jfreefrag->fr_freefrag = NULL;
3998 free_jfreefrag(jfreefrag);
4002 * Called when the journal write for a jfreeblk completes. The jfreeblk
4003 * is removed from the freeblks list of pending journal writes and the
4004 * jsegdep is moved to the freeblks jwork to be completed when all blocks
4005 * have been reclaimed.
4008 handle_written_jblkdep(jblkdep)
4009 struct jblkdep *jblkdep;
4011 struct freeblks *freeblks;
4012 struct jsegdep *jsegdep;
4014 /* Grab the jsegdep. */
4015 jsegdep = jblkdep->jb_jsegdep;
4016 jblkdep->jb_jsegdep = NULL;
4017 freeblks = jblkdep->jb_freeblks;
4018 LIST_REMOVE(jblkdep, jb_deps);
4019 jwork_insert(&freeblks->fb_jwork, jsegdep);
4021 * If the freeblks is all journaled, we can add it to the worklist.
4023 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
4024 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
4025 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
4027 free_jblkdep(jblkdep);
4030 static struct jsegdep *
4031 newjsegdep(struct worklist *wk)
4033 struct jsegdep *jsegdep;
4035 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
4036 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
4037 jsegdep->jd_seg = NULL;
4042 static struct jmvref *
4043 newjmvref(dp, ino, oldoff, newoff)
4049 struct jmvref *jmvref;
4051 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
4052 workitem_alloc(&jmvref->jm_list, D_JMVREF, ITOVFS(dp));
4053 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
4054 jmvref->jm_parent = dp->i_number;
4055 jmvref->jm_ino = ino;
4056 jmvref->jm_oldoff = oldoff;
4057 jmvref->jm_newoff = newoff;
4063 * Allocate a new jremref that tracks the removal of ip from dp with the
4064 * directory entry offset of diroff. Mark the entry as ATTACHED and
4065 * DEPCOMPLETE as we have all the information required for the journal write
4066 * and the directory has already been removed from the buffer. The caller
4067 * is responsible for linking the jremref into the pagedep and adding it
4068 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4069 * a DOTDOT addition so handle_workitem_remove() can properly assign
4070 * the jsegdep when we're done.
4072 static struct jremref *
4073 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4074 off_t diroff, nlink_t nlink)
4076 struct jremref *jremref;
4078 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4079 workitem_alloc(&jremref->jr_list, D_JREMREF, ITOVFS(dp));
4080 jremref->jr_state = ATTACHED;
4081 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4083 jremref->jr_dirrem = dirrem;
4089 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4090 nlink_t nlink, uint16_t mode)
4093 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4094 inoref->if_diroff = diroff;
4095 inoref->if_ino = ino;
4096 inoref->if_parent = parent;
4097 inoref->if_nlink = nlink;
4098 inoref->if_mode = mode;
4102 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4103 * directory offset may not be known until later. The caller is responsible
4104 * adding the entry to the journal when this information is available. nlink
4105 * should be the link count prior to the addition and mode is only required
4106 * to have the correct FMT.
4108 static struct jaddref *
4109 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4112 struct jaddref *jaddref;
4114 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4115 workitem_alloc(&jaddref->ja_list, D_JADDREF, ITOVFS(dp));
4116 jaddref->ja_state = ATTACHED;
4117 jaddref->ja_mkdir = NULL;
4118 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4124 * Create a new free dependency for a freework. The caller is responsible
4125 * for adjusting the reference count when it has the lock held. The freedep
4126 * will track an outstanding bitmap write that will ultimately clear the
4127 * freework to continue.
4129 static struct freedep *
4130 newfreedep(struct freework *freework)
4132 struct freedep *freedep;
4134 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4135 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4136 freedep->fd_freework = freework;
4142 * Free a freedep structure once the buffer it is linked to is written. If
4143 * this is the last reference to the freework schedule it for completion.
4146 free_freedep(freedep)
4147 struct freedep *freedep;
4149 struct freework *freework;
4151 freework = freedep->fd_freework;
4152 freework->fw_freeblks->fb_cgwait--;
4153 if (--freework->fw_ref == 0)
4154 freework_enqueue(freework);
4155 WORKITEM_FREE(freedep, D_FREEDEP);
4159 * Allocate a new freework structure that may be a level in an indirect
4160 * when parent is not NULL or a top level block when it is. The top level
4161 * freework structures are allocated without the per-filesystem lock held
4162 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4164 static struct freework *
4165 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4166 struct ufsmount *ump;
4167 struct freeblks *freeblks;
4168 struct freework *parent;
4175 struct freework *freework;
4177 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4178 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4179 freework->fw_state = ATTACHED;
4180 freework->fw_jnewblk = NULL;
4181 freework->fw_freeblks = freeblks;
4182 freework->fw_parent = parent;
4183 freework->fw_lbn = lbn;
4184 freework->fw_blkno = nb;
4185 freework->fw_frags = frags;
4186 freework->fw_indir = NULL;
4187 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 ||
4188 lbn >= -UFS_NXADDR) ? 0 : NINDIR(ump->um_fs) + 1;
4189 freework->fw_start = freework->fw_off = off;
4191 newjfreeblk(freeblks, lbn, nb, frags);
4192 if (parent == NULL) {
4194 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4203 * Eliminate a jfreeblk for a block that does not need journaling.
4206 cancel_jfreeblk(freeblks, blkno)
4207 struct freeblks *freeblks;
4210 struct jfreeblk *jfreeblk;
4211 struct jblkdep *jblkdep;
4213 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4214 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4216 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4217 if (jfreeblk->jf_blkno == blkno)
4220 if (jblkdep == NULL)
4222 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4223 free_jsegdep(jblkdep->jb_jsegdep);
4224 LIST_REMOVE(jblkdep, jb_deps);
4225 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4229 * Allocate a new jfreeblk to journal top level block pointer when truncating
4230 * a file. The caller must add this to the worklist when the per-filesystem
4233 static struct jfreeblk *
4234 newjfreeblk(freeblks, lbn, blkno, frags)
4235 struct freeblks *freeblks;
4240 struct jfreeblk *jfreeblk;
4242 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4243 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4244 freeblks->fb_list.wk_mp);
4245 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4246 jfreeblk->jf_dep.jb_freeblks = freeblks;
4247 jfreeblk->jf_ino = freeblks->fb_inum;
4248 jfreeblk->jf_lbn = lbn;
4249 jfreeblk->jf_blkno = blkno;
4250 jfreeblk->jf_frags = frags;
4251 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4257 * The journal is only prepared to handle full-size block numbers, so we
4258 * have to adjust the record to reflect the change to a full-size block.
4259 * For example, suppose we have a block made up of fragments 8-15 and
4260 * want to free its last two fragments. We are given a request that says:
4261 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4262 * where frags are the number of fragments to free and oldfrags are the
4263 * number of fragments to keep. To block align it, we have to change it to
4264 * have a valid full-size blkno, so it becomes:
4265 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4268 adjust_newfreework(freeblks, frag_offset)
4269 struct freeblks *freeblks;
4272 struct jfreeblk *jfreeblk;
4274 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4275 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4276 ("adjust_newfreework: Missing freeblks dependency"));
4278 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4279 jfreeblk->jf_blkno -= frag_offset;
4280 jfreeblk->jf_frags += frag_offset;
4284 * Allocate a new jtrunc to track a partial truncation.
4286 static struct jtrunc *
4287 newjtrunc(freeblks, size, extsize)
4288 struct freeblks *freeblks;
4292 struct jtrunc *jtrunc;
4294 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4295 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4296 freeblks->fb_list.wk_mp);
4297 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4298 jtrunc->jt_dep.jb_freeblks = freeblks;
4299 jtrunc->jt_ino = freeblks->fb_inum;
4300 jtrunc->jt_size = size;
4301 jtrunc->jt_extsize = extsize;
4302 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4308 * If we're canceling a new bitmap we have to search for another ref
4309 * to move into the bmsafemap dep. This might be better expressed
4310 * with another structure.
4313 move_newblock_dep(jaddref, inodedep)
4314 struct jaddref *jaddref;
4315 struct inodedep *inodedep;
4317 struct inoref *inoref;
4318 struct jaddref *jaddrefn;
4321 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4322 inoref = TAILQ_NEXT(inoref, if_deps)) {
4323 if ((jaddref->ja_state & NEWBLOCK) &&
4324 inoref->if_list.wk_type == D_JADDREF) {
4325 jaddrefn = (struct jaddref *)inoref;
4329 if (jaddrefn == NULL)
4331 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4332 jaddrefn->ja_state |= jaddref->ja_state &
4333 (ATTACHED | UNDONE | NEWBLOCK);
4334 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4335 jaddref->ja_state |= ATTACHED;
4336 LIST_REMOVE(jaddref, ja_bmdeps);
4337 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4342 * Cancel a jaddref either before it has been written or while it is being
4343 * written. This happens when a link is removed before the add reaches
4344 * the disk. The jaddref dependency is kept linked into the bmsafemap
4345 * and inode to prevent the link count or bitmap from reaching the disk
4346 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4349 * Returns 1 if the canceled addref requires journaling of the remove and
4353 cancel_jaddref(jaddref, inodedep, wkhd)
4354 struct jaddref *jaddref;
4355 struct inodedep *inodedep;
4356 struct workhead *wkhd;
4358 struct inoref *inoref;
4359 struct jsegdep *jsegdep;
4362 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4363 ("cancel_jaddref: Canceling complete jaddref"));
4364 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4368 if (inodedep == NULL)
4369 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4371 panic("cancel_jaddref: Lost inodedep");
4373 * We must adjust the nlink of any reference operation that follows
4374 * us so that it is consistent with the in-memory reference. This
4375 * ensures that inode nlink rollbacks always have the correct link.
4378 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4379 inoref = TAILQ_NEXT(inoref, if_deps)) {
4380 if (inoref->if_state & GOINGAWAY)
4385 jsegdep = inoref_jseg(&jaddref->ja_ref);
4386 if (jaddref->ja_state & NEWBLOCK)
4387 move_newblock_dep(jaddref, inodedep);
4388 wake_worklist(&jaddref->ja_list);
4389 jaddref->ja_mkdir = NULL;
4390 if (jaddref->ja_state & INPROGRESS) {
4391 jaddref->ja_state &= ~INPROGRESS;
4392 WORKLIST_REMOVE(&jaddref->ja_list);
4393 jwork_insert(wkhd, jsegdep);
4395 free_jsegdep(jsegdep);
4396 if (jaddref->ja_state & DEPCOMPLETE)
4397 remove_from_journal(&jaddref->ja_list);
4399 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4401 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4402 * can arrange for them to be freed with the bitmap. Otherwise we
4403 * no longer need this addref attached to the inoreflst and it
4404 * will incorrectly adjust nlink if we leave it.
4406 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4407 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4409 jaddref->ja_state |= COMPLETE;
4410 free_jaddref(jaddref);
4414 * Leave the head of the list for jsegdeps for fast merging.
4416 if (LIST_FIRST(wkhd) != NULL) {
4417 jaddref->ja_state |= ONWORKLIST;
4418 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4420 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4426 * Attempt to free a jaddref structure when some work completes. This
4427 * should only succeed once the entry is written and all dependencies have
4431 free_jaddref(jaddref)
4432 struct jaddref *jaddref;
4435 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4437 if (jaddref->ja_ref.if_jsegdep)
4438 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4439 jaddref, jaddref->ja_state);
4440 if (jaddref->ja_state & NEWBLOCK)
4441 LIST_REMOVE(jaddref, ja_bmdeps);
4442 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4443 panic("free_jaddref: Bad state %p(0x%X)",
4444 jaddref, jaddref->ja_state);
4445 if (jaddref->ja_mkdir != NULL)
4446 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4447 WORKITEM_FREE(jaddref, D_JADDREF);
4451 * Free a jremref structure once it has been written or discarded.
4454 free_jremref(jremref)
4455 struct jremref *jremref;
4458 if (jremref->jr_ref.if_jsegdep)
4459 free_jsegdep(jremref->jr_ref.if_jsegdep);
4460 if (jremref->jr_state & INPROGRESS)
4461 panic("free_jremref: IO still pending");
4462 WORKITEM_FREE(jremref, D_JREMREF);
4466 * Free a jnewblk structure.
4469 free_jnewblk(jnewblk)
4470 struct jnewblk *jnewblk;
4473 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4475 LIST_REMOVE(jnewblk, jn_deps);
4476 if (jnewblk->jn_dep != NULL)
4477 panic("free_jnewblk: Dependency still attached.");
4478 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4482 * Cancel a jnewblk which has been been made redundant by frag extension.
4485 cancel_jnewblk(jnewblk, wkhd)
4486 struct jnewblk *jnewblk;
4487 struct workhead *wkhd;
4489 struct jsegdep *jsegdep;
4491 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4492 jsegdep = jnewblk->jn_jsegdep;
4493 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4494 panic("cancel_jnewblk: Invalid state");
4495 jnewblk->jn_jsegdep = NULL;
4496 jnewblk->jn_dep = NULL;
4497 jnewblk->jn_state |= GOINGAWAY;
4498 if (jnewblk->jn_state & INPROGRESS) {
4499 jnewblk->jn_state &= ~INPROGRESS;
4500 WORKLIST_REMOVE(&jnewblk->jn_list);
4501 jwork_insert(wkhd, jsegdep);
4503 free_jsegdep(jsegdep);
4504 remove_from_journal(&jnewblk->jn_list);
4506 wake_worklist(&jnewblk->jn_list);
4507 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4511 free_jblkdep(jblkdep)
4512 struct jblkdep *jblkdep;
4515 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4516 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4517 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4518 WORKITEM_FREE(jblkdep, D_JTRUNC);
4520 panic("free_jblkdep: Unexpected type %s",
4521 TYPENAME(jblkdep->jb_list.wk_type));
4525 * Free a single jseg once it is no longer referenced in memory or on
4526 * disk. Reclaim journal blocks and dependencies waiting for the segment
4530 free_jseg(jseg, jblocks)
4532 struct jblocks *jblocks;
4534 struct freework *freework;
4537 * Free freework structures that were lingering to indicate freed
4538 * indirect blocks that forced journal write ordering on reallocate.
4540 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4541 indirblk_remove(freework);
4542 if (jblocks->jb_oldestseg == jseg)
4543 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4544 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4545 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4546 KASSERT(LIST_EMPTY(&jseg->js_entries),
4547 ("free_jseg: Freed jseg has valid entries."));
4548 WORKITEM_FREE(jseg, D_JSEG);
4552 * Free all jsegs that meet the criteria for being reclaimed and update
4557 struct jblocks *jblocks;
4562 * Free only those jsegs which have none allocated before them to
4563 * preserve the journal space ordering.
4565 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4567 * Only reclaim space when nothing depends on this journal
4568 * set and another set has written that it is no longer
4571 if (jseg->js_refs != 0) {
4572 jblocks->jb_oldestseg = jseg;
4575 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4577 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4580 * We can free jsegs that didn't write entries when
4581 * oldestwrseq == js_seq.
4583 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4586 free_jseg(jseg, jblocks);
4589 * If we exited the loop above we still must discover the
4590 * oldest valid segment.
4593 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4594 jseg = TAILQ_NEXT(jseg, js_next))
4595 if (jseg->js_refs != 0)
4597 jblocks->jb_oldestseg = jseg;
4599 * The journal has no valid records but some jsegs may still be
4600 * waiting on oldestwrseq to advance. We force a small record
4601 * out to permit these lingering records to be reclaimed.
4603 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4604 jblocks->jb_needseg = 1;
4608 * Release one reference to a jseg and free it if the count reaches 0. This
4609 * should eventually reclaim journal space as well.
4616 KASSERT(jseg->js_refs > 0,
4617 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4618 if (--jseg->js_refs != 0)
4620 free_jsegs(jseg->js_jblocks);
4624 * Release a jsegdep and decrement the jseg count.
4627 free_jsegdep(jsegdep)
4628 struct jsegdep *jsegdep;
4631 if (jsegdep->jd_seg)
4632 rele_jseg(jsegdep->jd_seg);
4633 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4637 * Wait for a journal item to make it to disk. Initiate journal processing
4642 struct worklist *wk;
4646 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4648 * Blocking journal waits cause slow synchronous behavior. Record
4649 * stats on the frequency of these blocking operations.
4651 if (waitfor == MNT_WAIT) {
4652 stat_journal_wait++;
4653 switch (wk->wk_type) {
4656 stat_jwait_filepage++;
4660 stat_jwait_freeblks++;
4663 stat_jwait_newblk++;
4673 * If IO has not started we process the journal. We can't mark the
4674 * worklist item as IOWAITING because we drop the lock while
4675 * processing the journal and the worklist entry may be freed after
4676 * this point. The caller may call back in and re-issue the request.
4678 if ((wk->wk_state & INPROGRESS) == 0) {
4679 softdep_process_journal(wk->wk_mp, wk, waitfor);
4680 if (waitfor != MNT_WAIT)
4684 if (waitfor != MNT_WAIT)
4686 wait_worklist(wk, "jwait");
4691 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4692 * appropriate. This is a convenience function to reduce duplicate code
4693 * for the setup and revert functions below.
4695 static struct inodedep *
4696 inodedep_lookup_ip(ip)
4699 struct inodedep *inodedep;
4701 KASSERT(ip->i_nlink >= ip->i_effnlink,
4702 ("inodedep_lookup_ip: bad delta"));
4703 (void) inodedep_lookup(ITOVFS(ip), ip->i_number, DEPALLOC,
4705 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4706 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
4712 * Called prior to creating a new inode and linking it to a directory. The
4713 * jaddref structure must already be allocated by softdep_setup_inomapdep
4714 * and it is discovered here so we can initialize the mode and update
4718 softdep_setup_create(dp, ip)
4722 struct inodedep *inodedep;
4723 struct jaddref *jaddref;
4726 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4727 ("softdep_setup_create called on non-softdep filesystem"));
4728 KASSERT(ip->i_nlink == 1,
4729 ("softdep_setup_create: Invalid link count."));
4731 ACQUIRE_LOCK(ITOUMP(dp));
4732 inodedep = inodedep_lookup_ip(ip);
4733 if (DOINGSUJ(dvp)) {
4734 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4736 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4737 ("softdep_setup_create: No addref structure present."));
4739 softdep_prelink(dvp, NULL);
4740 FREE_LOCK(ITOUMP(dp));
4744 * Create a jaddref structure to track the addition of a DOTDOT link when
4745 * we are reparenting an inode as part of a rename. This jaddref will be
4746 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
4747 * non-journaling softdep.
4750 softdep_setup_dotdot_link(dp, ip)
4754 struct inodedep *inodedep;
4755 struct jaddref *jaddref;
4758 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4759 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
4763 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
4764 * is used as a normal link would be.
4767 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4768 dp->i_effnlink - 1, dp->i_mode);
4769 ACQUIRE_LOCK(ITOUMP(dp));
4770 inodedep = inodedep_lookup_ip(dp);
4772 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4774 softdep_prelink(dvp, ITOV(ip));
4775 FREE_LOCK(ITOUMP(dp));
4779 * Create a jaddref structure to track a new link to an inode. The directory
4780 * offset is not known until softdep_setup_directory_add or
4781 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
4785 softdep_setup_link(dp, ip)
4789 struct inodedep *inodedep;
4790 struct jaddref *jaddref;
4793 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4794 ("softdep_setup_link called on non-softdep filesystem"));
4798 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
4800 ACQUIRE_LOCK(ITOUMP(dp));
4801 inodedep = inodedep_lookup_ip(ip);
4803 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4805 softdep_prelink(dvp, ITOV(ip));
4806 FREE_LOCK(ITOUMP(dp));
4810 * Called to create the jaddref structures to track . and .. references as
4811 * well as lookup and further initialize the incomplete jaddref created
4812 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
4813 * nlinkdelta for non-journaling softdep.
4816 softdep_setup_mkdir(dp, ip)
4820 struct inodedep *inodedep;
4821 struct jaddref *dotdotaddref;
4822 struct jaddref *dotaddref;
4823 struct jaddref *jaddref;
4826 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4827 ("softdep_setup_mkdir called on non-softdep filesystem"));
4829 dotaddref = dotdotaddref = NULL;
4830 if (DOINGSUJ(dvp)) {
4831 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
4833 dotaddref->ja_state |= MKDIR_BODY;
4834 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4835 dp->i_effnlink - 1, dp->i_mode);
4836 dotdotaddref->ja_state |= MKDIR_PARENT;
4838 ACQUIRE_LOCK(ITOUMP(dp));
4839 inodedep = inodedep_lookup_ip(ip);
4840 if (DOINGSUJ(dvp)) {
4841 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4843 KASSERT(jaddref != NULL,
4844 ("softdep_setup_mkdir: No addref structure present."));
4845 KASSERT(jaddref->ja_parent == dp->i_number,
4846 ("softdep_setup_mkdir: bad parent %ju",
4847 (uintmax_t)jaddref->ja_parent));
4848 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
4851 inodedep = inodedep_lookup_ip(dp);
4853 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
4854 &dotdotaddref->ja_ref, if_deps);
4855 softdep_prelink(ITOV(dp), NULL);
4856 FREE_LOCK(ITOUMP(dp));
4860 * Called to track nlinkdelta of the inode and parent directories prior to
4861 * unlinking a directory.
4864 softdep_setup_rmdir(dp, ip)
4870 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4871 ("softdep_setup_rmdir called on non-softdep filesystem"));
4873 ACQUIRE_LOCK(ITOUMP(dp));
4874 (void) inodedep_lookup_ip(ip);
4875 (void) inodedep_lookup_ip(dp);
4876 softdep_prelink(dvp, ITOV(ip));
4877 FREE_LOCK(ITOUMP(dp));
4881 * Called to track nlinkdelta of the inode and parent directories prior to
4885 softdep_setup_unlink(dp, ip)
4891 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4892 ("softdep_setup_unlink called on non-softdep filesystem"));
4894 ACQUIRE_LOCK(ITOUMP(dp));
4895 (void) inodedep_lookup_ip(ip);
4896 (void) inodedep_lookup_ip(dp);
4897 softdep_prelink(dvp, ITOV(ip));
4898 FREE_LOCK(ITOUMP(dp));
4902 * Called to release the journal structures created by a failed non-directory
4903 * creation. Adjusts nlinkdelta for non-journaling softdep.
4906 softdep_revert_create(dp, ip)
4910 struct inodedep *inodedep;
4911 struct jaddref *jaddref;
4914 KASSERT(MOUNTEDSOFTDEP(ITOVFS((dp))) != 0,
4915 ("softdep_revert_create called on non-softdep filesystem"));
4917 ACQUIRE_LOCK(ITOUMP(dp));
4918 inodedep = inodedep_lookup_ip(ip);
4919 if (DOINGSUJ(dvp)) {
4920 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4922 KASSERT(jaddref->ja_parent == dp->i_number,
4923 ("softdep_revert_create: addref parent mismatch"));
4924 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4926 FREE_LOCK(ITOUMP(dp));
4930 * Called to release the journal structures created by a failed link
4931 * addition. Adjusts nlinkdelta for non-journaling softdep.
4934 softdep_revert_link(dp, ip)
4938 struct inodedep *inodedep;
4939 struct jaddref *jaddref;
4942 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4943 ("softdep_revert_link called on non-softdep filesystem"));
4945 ACQUIRE_LOCK(ITOUMP(dp));
4946 inodedep = inodedep_lookup_ip(ip);
4947 if (DOINGSUJ(dvp)) {
4948 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4950 KASSERT(jaddref->ja_parent == dp->i_number,
4951 ("softdep_revert_link: addref parent mismatch"));
4952 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4954 FREE_LOCK(ITOUMP(dp));
4958 * Called to release the journal structures created by a failed mkdir
4959 * attempt. Adjusts nlinkdelta for non-journaling softdep.
4962 softdep_revert_mkdir(dp, ip)
4966 struct inodedep *inodedep;
4967 struct jaddref *jaddref;
4968 struct jaddref *dotaddref;
4971 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
4972 ("softdep_revert_mkdir called on non-softdep filesystem"));
4975 ACQUIRE_LOCK(ITOUMP(dp));
4976 inodedep = inodedep_lookup_ip(dp);
4977 if (DOINGSUJ(dvp)) {
4978 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4980 KASSERT(jaddref->ja_parent == ip->i_number,
4981 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
4982 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4984 inodedep = inodedep_lookup_ip(ip);
4985 if (DOINGSUJ(dvp)) {
4986 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4988 KASSERT(jaddref->ja_parent == dp->i_number,
4989 ("softdep_revert_mkdir: addref parent mismatch"));
4990 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
4991 inoreflst, if_deps);
4992 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4993 KASSERT(dotaddref->ja_parent == ip->i_number,
4994 ("softdep_revert_mkdir: dot addref parent mismatch"));
4995 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
4997 FREE_LOCK(ITOUMP(dp));
5001 * Called to correct nlinkdelta after a failed rmdir.
5004 softdep_revert_rmdir(dp, ip)
5009 KASSERT(MOUNTEDSOFTDEP(ITOVFS(dp)) != 0,
5010 ("softdep_revert_rmdir called on non-softdep filesystem"));
5011 ACQUIRE_LOCK(ITOUMP(dp));
5012 (void) inodedep_lookup_ip(ip);
5013 (void) inodedep_lookup_ip(dp);
5014 FREE_LOCK(ITOUMP(dp));
5018 * Protecting the freemaps (or bitmaps).
5020 * To eliminate the need to execute fsck before mounting a filesystem
5021 * after a power failure, one must (conservatively) guarantee that the
5022 * on-disk copy of the bitmaps never indicate that a live inode or block is
5023 * free. So, when a block or inode is allocated, the bitmap should be
5024 * updated (on disk) before any new pointers. When a block or inode is
5025 * freed, the bitmap should not be updated until all pointers have been
5026 * reset. The latter dependency is handled by the delayed de-allocation
5027 * approach described below for block and inode de-allocation. The former
5028 * dependency is handled by calling the following procedure when a block or
5029 * inode is allocated. When an inode is allocated an "inodedep" is created
5030 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
5031 * Each "inodedep" is also inserted into the hash indexing structure so
5032 * that any additional link additions can be made dependent on the inode
5035 * The ufs filesystem maintains a number of free block counts (e.g., per
5036 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
5037 * in addition to the bitmaps. These counts are used to improve efficiency
5038 * during allocation and therefore must be consistent with the bitmaps.
5039 * There is no convenient way to guarantee post-crash consistency of these
5040 * counts with simple update ordering, for two main reasons: (1) The counts
5041 * and bitmaps for a single cylinder group block are not in the same disk
5042 * sector. If a disk write is interrupted (e.g., by power failure), one may
5043 * be written and the other not. (2) Some of the counts are located in the
5044 * superblock rather than the cylinder group block. So, we focus our soft
5045 * updates implementation on protecting the bitmaps. When mounting a
5046 * filesystem, we recompute the auxiliary counts from the bitmaps.
5050 * Called just after updating the cylinder group block to allocate an inode.
5053 softdep_setup_inomapdep(bp, ip, newinum, mode)
5054 struct buf *bp; /* buffer for cylgroup block with inode map */
5055 struct inode *ip; /* inode related to allocation */
5056 ino_t newinum; /* new inode number being allocated */
5059 struct inodedep *inodedep;
5060 struct bmsafemap *bmsafemap;
5061 struct jaddref *jaddref;
5066 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5067 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5068 fs = VFSTOUFS(mp)->um_fs;
5072 * Allocate the journal reference add structure so that the bitmap
5073 * can be dependent on it.
5075 if (MOUNTEDSUJ(mp)) {
5076 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5077 jaddref->ja_state |= NEWBLOCK;
5081 * Create a dependency for the newly allocated inode.
5082 * Panic if it already exists as something is seriously wrong.
5083 * Otherwise add it to the dependency list for the buffer holding
5084 * the cylinder group map from which it was allocated.
5086 * We have to preallocate a bmsafemap entry in case it is needed
5087 * in bmsafemap_lookup since once we allocate the inodedep, we
5088 * have to finish initializing it before we can FREE_LOCK().
5089 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5090 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5091 * creating the inodedep as it can be freed during the time
5092 * that we FREE_LOCK() while allocating the inodedep. We must
5093 * call workitem_alloc() before entering the locked section as
5094 * it also acquires the lock and we must avoid trying doing so
5097 bmsafemap = malloc(sizeof(struct bmsafemap),
5098 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5099 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5100 ACQUIRE_LOCK(ITOUMP(ip));
5101 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5102 panic("softdep_setup_inomapdep: dependency %p for new"
5103 "inode already exists", inodedep);
5104 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5106 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5107 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5110 inodedep->id_state |= ONDEPLIST;
5111 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5113 inodedep->id_bmsafemap = bmsafemap;
5114 inodedep->id_state &= ~DEPCOMPLETE;
5115 FREE_LOCK(ITOUMP(ip));
5119 * Called just after updating the cylinder group block to
5120 * allocate block or fragment.
5123 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5124 struct buf *bp; /* buffer for cylgroup block with block map */
5125 struct mount *mp; /* filesystem doing allocation */
5126 ufs2_daddr_t newblkno; /* number of newly allocated block */
5127 int frags; /* Number of fragments. */
5128 int oldfrags; /* Previous number of fragments for extend. */
5130 struct newblk *newblk;
5131 struct bmsafemap *bmsafemap;
5132 struct jnewblk *jnewblk;
5133 struct ufsmount *ump;
5136 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5137 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5142 * Create a dependency for the newly allocated block.
5143 * Add it to the dependency list for the buffer holding
5144 * the cylinder group map from which it was allocated.
5146 if (MOUNTEDSUJ(mp)) {
5147 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5148 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5149 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5150 jnewblk->jn_state = ATTACHED;
5151 jnewblk->jn_blkno = newblkno;
5152 jnewblk->jn_frags = frags;
5153 jnewblk->jn_oldfrags = oldfrags;
5161 cgp = (struct cg *)bp->b_data;
5162 blksfree = cg_blksfree(cgp);
5163 bno = dtogd(fs, jnewblk->jn_blkno);
5164 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5166 if (isset(blksfree, bno + i))
5167 panic("softdep_setup_blkmapdep: "
5168 "free fragment %d from %d-%d "
5169 "state 0x%X dep %p", i,
5170 jnewblk->jn_oldfrags,
5180 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5181 newblkno, frags, oldfrags);
5183 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5184 panic("softdep_setup_blkmapdep: found block");
5185 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5186 dtog(fs, newblkno), NULL);
5188 jnewblk->jn_dep = (struct worklist *)newblk;
5189 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5191 newblk->nb_state |= ONDEPLIST;
5192 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5194 newblk->nb_bmsafemap = bmsafemap;
5195 newblk->nb_jnewblk = jnewblk;
5199 #define BMSAFEMAP_HASH(ump, cg) \
5200 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5203 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5204 struct bmsafemap_hashhead *bmsafemaphd;
5206 struct bmsafemap **bmsafemapp;
5208 struct bmsafemap *bmsafemap;
5210 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5211 if (bmsafemap->sm_cg == cg)
5214 *bmsafemapp = bmsafemap;
5223 * Find the bmsafemap associated with a cylinder group buffer.
5224 * If none exists, create one. The buffer must be locked when
5225 * this routine is called and this routine must be called with
5226 * the softdep lock held. To avoid giving up the lock while
5227 * allocating a new bmsafemap, a preallocated bmsafemap may be
5228 * provided. If it is provided but not needed, it is freed.
5230 static struct bmsafemap *
5231 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5235 struct bmsafemap *newbmsafemap;
5237 struct bmsafemap_hashhead *bmsafemaphd;
5238 struct bmsafemap *bmsafemap, *collision;
5239 struct worklist *wk;
5240 struct ufsmount *ump;
5244 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5245 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5246 if (wk->wk_type == D_BMSAFEMAP) {
5248 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5249 return (WK_BMSAFEMAP(wk));
5252 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5253 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5255 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5259 bmsafemap = newbmsafemap;
5262 bmsafemap = malloc(sizeof(struct bmsafemap),
5263 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5264 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5267 bmsafemap->sm_buf = bp;
5268 LIST_INIT(&bmsafemap->sm_inodedephd);
5269 LIST_INIT(&bmsafemap->sm_inodedepwr);
5270 LIST_INIT(&bmsafemap->sm_newblkhd);
5271 LIST_INIT(&bmsafemap->sm_newblkwr);
5272 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5273 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5274 LIST_INIT(&bmsafemap->sm_freehd);
5275 LIST_INIT(&bmsafemap->sm_freewr);
5276 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5277 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5280 bmsafemap->sm_cg = cg;
5281 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5282 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5283 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5288 * Direct block allocation dependencies.
5290 * When a new block is allocated, the corresponding disk locations must be
5291 * initialized (with zeros or new data) before the on-disk inode points to
5292 * them. Also, the freemap from which the block was allocated must be
5293 * updated (on disk) before the inode's pointer. These two dependencies are
5294 * independent of each other and are needed for all file blocks and indirect
5295 * blocks that are pointed to directly by the inode. Just before the
5296 * "in-core" version of the inode is updated with a newly allocated block
5297 * number, a procedure (below) is called to setup allocation dependency
5298 * structures. These structures are removed when the corresponding
5299 * dependencies are satisfied or when the block allocation becomes obsolete
5300 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5301 * fragment that gets upgraded). All of these cases are handled in
5302 * procedures described later.
5304 * When a file extension causes a fragment to be upgraded, either to a larger
5305 * fragment or to a full block, the on-disk location may change (if the
5306 * previous fragment could not simply be extended). In this case, the old
5307 * fragment must be de-allocated, but not until after the inode's pointer has
5308 * been updated. In most cases, this is handled by later procedures, which
5309 * will construct a "freefrag" structure to be added to the workitem queue
5310 * when the inode update is complete (or obsolete). The main exception to
5311 * this is when an allocation occurs while a pending allocation dependency
5312 * (for the same block pointer) remains. This case is handled in the main
5313 * allocation dependency setup procedure by immediately freeing the
5314 * unreferenced fragments.
5317 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5318 struct inode *ip; /* inode to which block is being added */
5319 ufs_lbn_t off; /* block pointer within inode */
5320 ufs2_daddr_t newblkno; /* disk block number being added */
5321 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
5322 long newsize; /* size of new block */
5323 long oldsize; /* size of new block */
5324 struct buf *bp; /* bp for allocated block */
5326 struct allocdirect *adp, *oldadp;
5327 struct allocdirectlst *adphead;
5328 struct freefrag *freefrag;
5329 struct inodedep *inodedep;
5330 struct pagedep *pagedep;
5331 struct jnewblk *jnewblk;
5332 struct newblk *newblk;
5338 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5339 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5340 if (oldblkno && oldblkno != newblkno)
5342 * The usual case is that a smaller fragment that
5343 * was just allocated has been replaced with a bigger
5344 * fragment or a full-size block. If it is marked as
5345 * B_DELWRI, the current contents have not been written
5346 * to disk. It is possible that the block was written
5347 * earlier, but very uncommon. If the block has never
5348 * been written, there is no need to send a BIO_DELETE
5349 * for it when it is freed. The gain from avoiding the
5350 * TRIMs for the common case of unwritten blocks far
5351 * exceeds the cost of the write amplification for the
5352 * uncommon case of failing to send a TRIM for a block
5353 * that had been written.
5355 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5356 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5361 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5362 "off %jd newsize %ld oldsize %d",
5363 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5364 ACQUIRE_LOCK(ITOUMP(ip));
5365 if (off >= UFS_NDADDR) {
5367 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5369 /* allocating an indirect block */
5371 panic("softdep_setup_allocdirect: non-zero indir");
5374 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5377 * Allocating a direct block.
5379 * If we are allocating a directory block, then we must
5380 * allocate an associated pagedep to track additions and
5383 if ((ip->i_mode & IFMT) == IFDIR)
5384 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5387 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5388 panic("softdep_setup_allocdirect: lost block");
5389 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5390 ("softdep_setup_allocdirect: newblk already initialized"));
5392 * Convert the newblk to an allocdirect.
5394 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5395 adp = (struct allocdirect *)newblk;
5396 newblk->nb_freefrag = freefrag;
5397 adp->ad_offset = off;
5398 adp->ad_oldblkno = oldblkno;
5399 adp->ad_newsize = newsize;
5400 adp->ad_oldsize = oldsize;
5403 * Finish initializing the journal.
5405 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5406 jnewblk->jn_ino = ip->i_number;
5407 jnewblk->jn_lbn = lbn;
5408 add_to_journal(&jnewblk->jn_list);
5410 if (freefrag && freefrag->ff_jdep != NULL &&
5411 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5412 add_to_journal(freefrag->ff_jdep);
5413 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5414 adp->ad_inodedep = inodedep;
5416 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5418 * The list of allocdirects must be kept in sorted and ascending
5419 * order so that the rollback routines can quickly determine the
5420 * first uncommitted block (the size of the file stored on disk
5421 * ends at the end of the lowest committed fragment, or if there
5422 * are no fragments, at the end of the highest committed block).
5423 * Since files generally grow, the typical case is that the new
5424 * block is to be added at the end of the list. We speed this
5425 * special case by checking against the last allocdirect in the
5426 * list before laboriously traversing the list looking for the
5429 adphead = &inodedep->id_newinoupdt;
5430 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5431 if (oldadp == NULL || oldadp->ad_offset <= off) {
5432 /* insert at end of list */
5433 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5434 if (oldadp != NULL && oldadp->ad_offset == off)
5435 allocdirect_merge(adphead, adp, oldadp);
5436 FREE_LOCK(ITOUMP(ip));
5439 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5440 if (oldadp->ad_offset >= off)
5444 panic("softdep_setup_allocdirect: lost entry");
5445 /* insert in middle of list */
5446 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5447 if (oldadp->ad_offset == off)
5448 allocdirect_merge(adphead, adp, oldadp);
5450 FREE_LOCK(ITOUMP(ip));
5454 * Merge a newer and older journal record to be stored either in a
5455 * newblock or freefrag. This handles aggregating journal records for
5456 * fragment allocation into a second record as well as replacing a
5457 * journal free with an aborted journal allocation. A segment for the
5458 * oldest record will be placed on wkhd if it has been written. If not
5459 * the segment for the newer record will suffice.
5461 static struct worklist *
5462 jnewblk_merge(new, old, wkhd)
5463 struct worklist *new;
5464 struct worklist *old;
5465 struct workhead *wkhd;
5467 struct jnewblk *njnewblk;
5468 struct jnewblk *jnewblk;
5470 /* Handle NULLs to simplify callers. */
5475 /* Replace a jfreefrag with a jnewblk. */
5476 if (new->wk_type == D_JFREEFRAG) {
5477 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5478 panic("jnewblk_merge: blkno mismatch: %p, %p",
5480 cancel_jfreefrag(WK_JFREEFRAG(new));
5483 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5484 panic("jnewblk_merge: Bad type: old %d new %d\n",
5485 old->wk_type, new->wk_type);
5487 * Handle merging of two jnewblk records that describe
5488 * different sets of fragments in the same block.
5490 jnewblk = WK_JNEWBLK(old);
5491 njnewblk = WK_JNEWBLK(new);
5492 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5493 panic("jnewblk_merge: Merging disparate blocks.");
5495 * The record may be rolled back in the cg.
5497 if (jnewblk->jn_state & UNDONE) {
5498 jnewblk->jn_state &= ~UNDONE;
5499 njnewblk->jn_state |= UNDONE;
5500 njnewblk->jn_state &= ~ATTACHED;
5503 * We modify the newer addref and free the older so that if neither
5504 * has been written the most up-to-date copy will be on disk. If
5505 * both have been written but rolled back we only temporarily need
5506 * one of them to fix the bits when the cg write completes.
5508 jnewblk->jn_state |= ATTACHED | COMPLETE;
5509 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5510 cancel_jnewblk(jnewblk, wkhd);
5511 WORKLIST_REMOVE(&jnewblk->jn_list);
5512 free_jnewblk(jnewblk);
5517 * Replace an old allocdirect dependency with a newer one.
5520 allocdirect_merge(adphead, newadp, oldadp)
5521 struct allocdirectlst *adphead; /* head of list holding allocdirects */
5522 struct allocdirect *newadp; /* allocdirect being added */
5523 struct allocdirect *oldadp; /* existing allocdirect being checked */
5525 struct worklist *wk;
5526 struct freefrag *freefrag;
5529 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5530 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5531 newadp->ad_oldsize != oldadp->ad_newsize ||
5532 newadp->ad_offset >= UFS_NDADDR)
5533 panic("%s %jd != new %jd || old size %ld != new %ld",
5534 "allocdirect_merge: old blkno",
5535 (intmax_t)newadp->ad_oldblkno,
5536 (intmax_t)oldadp->ad_newblkno,
5537 newadp->ad_oldsize, oldadp->ad_newsize);
5538 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5539 newadp->ad_oldsize = oldadp->ad_oldsize;
5541 * If the old dependency had a fragment to free or had never
5542 * previously had a block allocated, then the new dependency
5543 * can immediately post its freefrag and adopt the old freefrag.
5544 * This action is done by swapping the freefrag dependencies.
5545 * The new dependency gains the old one's freefrag, and the
5546 * old one gets the new one and then immediately puts it on
5547 * the worklist when it is freed by free_newblk. It is
5548 * not possible to do this swap when the old dependency had a
5549 * non-zero size but no previous fragment to free. This condition
5550 * arises when the new block is an extension of the old block.
5551 * Here, the first part of the fragment allocated to the new
5552 * dependency is part of the block currently claimed on disk by
5553 * the old dependency, so cannot legitimately be freed until the
5554 * conditions for the new dependency are fulfilled.
5556 freefrag = newadp->ad_freefrag;
5557 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5558 newadp->ad_freefrag = oldadp->ad_freefrag;
5559 oldadp->ad_freefrag = freefrag;
5562 * If we are tracking a new directory-block allocation,
5563 * move it from the old allocdirect to the new allocdirect.
5565 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5566 WORKLIST_REMOVE(wk);
5567 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5568 panic("allocdirect_merge: extra newdirblk");
5569 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5571 TAILQ_REMOVE(adphead, oldadp, ad_next);
5573 * We need to move any journal dependencies over to the freefrag
5574 * that releases this block if it exists. Otherwise we are
5575 * extending an existing block and we'll wait until that is
5576 * complete to release the journal space and extend the
5577 * new journal to cover this old space as well.
5579 if (freefrag == NULL) {
5580 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5581 panic("allocdirect_merge: %jd != %jd",
5582 oldadp->ad_newblkno, newadp->ad_newblkno);
5583 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5584 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5585 &oldadp->ad_block.nb_jnewblk->jn_list,
5586 &newadp->ad_block.nb_jwork);
5587 oldadp->ad_block.nb_jnewblk = NULL;
5588 cancel_newblk(&oldadp->ad_block, NULL,
5589 &newadp->ad_block.nb_jwork);
5591 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5592 &freefrag->ff_list, &freefrag->ff_jwork);
5593 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5594 &freefrag->ff_jwork);
5596 free_newblk(&oldadp->ad_block);
5600 * Allocate a jfreefrag structure to journal a single block free.
5602 static struct jfreefrag *
5603 newjfreefrag(freefrag, ip, blkno, size, lbn)
5604 struct freefrag *freefrag;
5610 struct jfreefrag *jfreefrag;
5614 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5616 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, ITOVFS(ip));
5617 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5618 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5619 jfreefrag->fr_ino = ip->i_number;
5620 jfreefrag->fr_lbn = lbn;
5621 jfreefrag->fr_blkno = blkno;
5622 jfreefrag->fr_frags = numfrags(fs, size);
5623 jfreefrag->fr_freefrag = freefrag;
5629 * Allocate a new freefrag structure.
5631 static struct freefrag *
5632 newfreefrag(ip, blkno, size, lbn, key)
5639 struct freefrag *freefrag;
5640 struct ufsmount *ump;
5643 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5644 ip->i_number, blkno, size, lbn);
5647 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5648 panic("newfreefrag: frag size");
5649 freefrag = malloc(sizeof(struct freefrag),
5650 M_FREEFRAG, M_SOFTDEP_FLAGS);
5651 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ump));
5652 freefrag->ff_state = ATTACHED;
5653 LIST_INIT(&freefrag->ff_jwork);
5654 freefrag->ff_inum = ip->i_number;
5655 freefrag->ff_vtype = ITOV(ip)->v_type;
5656 freefrag->ff_blkno = blkno;
5657 freefrag->ff_fragsize = size;
5658 freefrag->ff_key = key;
5660 if (MOUNTEDSUJ(UFSTOVFS(ump))) {
5661 freefrag->ff_jdep = (struct worklist *)
5662 newjfreefrag(freefrag, ip, blkno, size, lbn);
5664 freefrag->ff_state |= DEPCOMPLETE;
5665 freefrag->ff_jdep = NULL;
5672 * This workitem de-allocates fragments that were replaced during
5673 * file block allocation.
5676 handle_workitem_freefrag(freefrag)
5677 struct freefrag *freefrag;
5679 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5680 struct workhead wkhd;
5683 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5684 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5686 * It would be illegal to add new completion items to the
5687 * freefrag after it was schedule to be done so it must be
5688 * safe to modify the list head here.
5692 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5694 * If the journal has not been written we must cancel it here.
5696 if (freefrag->ff_jdep) {
5697 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5698 panic("handle_workitem_freefrag: Unexpected type %d\n",
5699 freefrag->ff_jdep->wk_type);
5700 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5703 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5704 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype,
5705 &wkhd, freefrag->ff_key);
5707 WORKITEM_FREE(freefrag, D_FREEFRAG);
5712 * Set up a dependency structure for an external attributes data block.
5713 * This routine follows much of the structure of softdep_setup_allocdirect.
5714 * See the description of softdep_setup_allocdirect above for details.
5717 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5720 ufs2_daddr_t newblkno;
5721 ufs2_daddr_t oldblkno;
5726 struct allocdirect *adp, *oldadp;
5727 struct allocdirectlst *adphead;
5728 struct freefrag *freefrag;
5729 struct inodedep *inodedep;
5730 struct jnewblk *jnewblk;
5731 struct newblk *newblk;
5733 struct ufsmount *ump;
5738 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5739 ("softdep_setup_allocext called on non-softdep filesystem"));
5740 KASSERT(off < UFS_NXADDR,
5741 ("softdep_setup_allocext: lbn %lld > UFS_NXADDR", (long long)off));
5744 if (oldblkno && oldblkno != newblkno)
5746 * The usual case is that a smaller fragment that
5747 * was just allocated has been replaced with a bigger
5748 * fragment or a full-size block. If it is marked as
5749 * B_DELWRI, the current contents have not been written
5750 * to disk. It is possible that the block was written
5751 * earlier, but very uncommon. If the block has never
5752 * been written, there is no need to send a BIO_DELETE
5753 * for it when it is freed. The gain from avoiding the
5754 * TRIMs for the common case of unwritten blocks far
5755 * exceeds the cost of the write amplification for the
5756 * uncommon case of failing to send a TRIM for a block
5757 * that had been written.
5759 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn,
5760 (bp->b_flags & B_DELWRI) != 0 ? NOTRIM_KEY : SINGLETON_KEY);
5765 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5766 panic("softdep_setup_allocext: lost block");
5767 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5768 ("softdep_setup_allocext: newblk already initialized"));
5770 * Convert the newblk to an allocdirect.
5772 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5773 adp = (struct allocdirect *)newblk;
5774 newblk->nb_freefrag = freefrag;
5775 adp->ad_offset = off;
5776 adp->ad_oldblkno = oldblkno;
5777 adp->ad_newsize = newsize;
5778 adp->ad_oldsize = oldsize;
5779 adp->ad_state |= EXTDATA;
5782 * Finish initializing the journal.
5784 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5785 jnewblk->jn_ino = ip->i_number;
5786 jnewblk->jn_lbn = lbn;
5787 add_to_journal(&jnewblk->jn_list);
5789 if (freefrag && freefrag->ff_jdep != NULL &&
5790 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5791 add_to_journal(freefrag->ff_jdep);
5792 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5793 adp->ad_inodedep = inodedep;
5795 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5797 * The list of allocdirects must be kept in sorted and ascending
5798 * order so that the rollback routines can quickly determine the
5799 * first uncommitted block (the size of the file stored on disk
5800 * ends at the end of the lowest committed fragment, or if there
5801 * are no fragments, at the end of the highest committed block).
5802 * Since files generally grow, the typical case is that the new
5803 * block is to be added at the end of the list. We speed this
5804 * special case by checking against the last allocdirect in the
5805 * list before laboriously traversing the list looking for the
5808 adphead = &inodedep->id_newextupdt;
5809 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5810 if (oldadp == NULL || oldadp->ad_offset <= off) {
5811 /* insert at end of list */
5812 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5813 if (oldadp != NULL && oldadp->ad_offset == off)
5814 allocdirect_merge(adphead, adp, oldadp);
5818 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5819 if (oldadp->ad_offset >= off)
5823 panic("softdep_setup_allocext: lost entry");
5824 /* insert in middle of list */
5825 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5826 if (oldadp->ad_offset == off)
5827 allocdirect_merge(adphead, adp, oldadp);
5832 * Indirect block allocation dependencies.
5834 * The same dependencies that exist for a direct block also exist when
5835 * a new block is allocated and pointed to by an entry in a block of
5836 * indirect pointers. The undo/redo states described above are also
5837 * used here. Because an indirect block contains many pointers that
5838 * may have dependencies, a second copy of the entire in-memory indirect
5839 * block is kept. The buffer cache copy is always completely up-to-date.
5840 * The second copy, which is used only as a source for disk writes,
5841 * contains only the safe pointers (i.e., those that have no remaining
5842 * update dependencies). The second copy is freed when all pointers
5843 * are safe. The cache is not allowed to replace indirect blocks with
5844 * pending update dependencies. If a buffer containing an indirect
5845 * block with dependencies is written, these routines will mark it
5846 * dirty again. It can only be successfully written once all the
5847 * dependencies are removed. The ffs_fsync routine in conjunction with
5848 * softdep_sync_metadata work together to get all the dependencies
5849 * removed so that a file can be successfully written to disk. Three
5850 * procedures are used when setting up indirect block pointer
5851 * dependencies. The division is necessary because of the organization
5852 * of the "balloc" routine and because of the distinction between file
5853 * pages and file metadata blocks.
5857 * Allocate a new allocindir structure.
5859 static struct allocindir *
5860 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
5861 struct inode *ip; /* inode for file being extended */
5862 int ptrno; /* offset of pointer in indirect block */
5863 ufs2_daddr_t newblkno; /* disk block number being added */
5864 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
5867 struct newblk *newblk;
5868 struct allocindir *aip;
5869 struct freefrag *freefrag;
5870 struct jnewblk *jnewblk;
5873 freefrag = newfreefrag(ip, oldblkno, ITOFS(ip)->fs_bsize, lbn,
5877 ACQUIRE_LOCK(ITOUMP(ip));
5878 if (newblk_lookup(ITOVFS(ip), newblkno, 0, &newblk) == 0)
5879 panic("new_allocindir: lost block");
5880 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5881 ("newallocindir: newblk already initialized"));
5882 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
5883 newblk->nb_freefrag = freefrag;
5884 aip = (struct allocindir *)newblk;
5885 aip->ai_offset = ptrno;
5886 aip->ai_oldblkno = oldblkno;
5888 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5889 jnewblk->jn_ino = ip->i_number;
5890 jnewblk->jn_lbn = lbn;
5891 add_to_journal(&jnewblk->jn_list);
5893 if (freefrag && freefrag->ff_jdep != NULL &&
5894 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5895 add_to_journal(freefrag->ff_jdep);
5900 * Called just before setting an indirect block pointer
5901 * to a newly allocated file page.
5904 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
5905 struct inode *ip; /* inode for file being extended */
5906 ufs_lbn_t lbn; /* allocated block number within file */
5907 struct buf *bp; /* buffer with indirect blk referencing page */
5908 int ptrno; /* offset of pointer in indirect block */
5909 ufs2_daddr_t newblkno; /* disk block number being added */
5910 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
5911 struct buf *nbp; /* buffer holding allocated page */
5913 struct inodedep *inodedep;
5914 struct freefrag *freefrag;
5915 struct allocindir *aip;
5916 struct pagedep *pagedep;
5918 struct ufsmount *ump;
5922 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5923 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
5924 KASSERT(lbn == nbp->b_lblkno,
5925 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
5926 lbn, bp->b_lblkno));
5928 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
5929 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
5930 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
5931 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
5932 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5934 * If we are allocating a directory page, then we must
5935 * allocate an associated pagedep to track additions and
5938 if ((ip->i_mode & IFMT) == IFDIR)
5939 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
5940 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5941 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
5944 handle_workitem_freefrag(freefrag);
5948 * Called just before setting an indirect block pointer to a
5949 * newly allocated indirect block.
5952 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
5953 struct buf *nbp; /* newly allocated indirect block */
5954 struct inode *ip; /* inode for file being extended */
5955 struct buf *bp; /* indirect block referencing allocated block */
5956 int ptrno; /* offset of pointer in indirect block */
5957 ufs2_daddr_t newblkno; /* disk block number being added */
5959 struct inodedep *inodedep;
5960 struct allocindir *aip;
5961 struct ufsmount *ump;
5965 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
5966 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
5968 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
5969 ip->i_number, newblkno, ptrno);
5970 lbn = nbp->b_lblkno;
5971 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
5972 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
5973 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
5974 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5975 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
5976 panic("softdep_setup_allocindir_meta: Block already existed");
5981 indirdep_complete(indirdep)
5982 struct indirdep *indirdep;
5984 struct allocindir *aip;
5986 LIST_REMOVE(indirdep, ir_next);
5987 indirdep->ir_state |= DEPCOMPLETE;
5989 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
5990 LIST_REMOVE(aip, ai_next);
5991 free_newblk(&aip->ai_block);
5994 * If this indirdep is not attached to a buf it was simply waiting
5995 * on completion to clear completehd. free_indirdep() asserts
5996 * that nothing is dangling.
5998 if ((indirdep->ir_state & ONWORKLIST) == 0)
5999 free_indirdep(indirdep);
6002 static struct indirdep *
6003 indirdep_lookup(mp, ip, bp)
6008 struct indirdep *indirdep, *newindirdep;
6009 struct newblk *newblk;
6010 struct ufsmount *ump;
6011 struct worklist *wk;
6021 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6022 if (wk->wk_type != D_INDIRDEP)
6024 indirdep = WK_INDIRDEP(wk);
6027 /* Found on the buffer worklist, no new structure to free. */
6028 if (indirdep != NULL && newindirdep == NULL)
6030 if (indirdep != NULL && newindirdep != NULL)
6031 panic("indirdep_lookup: simultaneous create");
6032 /* None found on the buffer and a new structure is ready. */
6033 if (indirdep == NULL && newindirdep != NULL)
6035 /* None found and no new structure available. */
6037 newindirdep = malloc(sizeof(struct indirdep),
6038 M_INDIRDEP, M_SOFTDEP_FLAGS);
6039 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
6040 newindirdep->ir_state = ATTACHED;
6042 newindirdep->ir_state |= UFS1FMT;
6043 TAILQ_INIT(&newindirdep->ir_trunc);
6044 newindirdep->ir_saveddata = NULL;
6045 LIST_INIT(&newindirdep->ir_deplisthd);
6046 LIST_INIT(&newindirdep->ir_donehd);
6047 LIST_INIT(&newindirdep->ir_writehd);
6048 LIST_INIT(&newindirdep->ir_completehd);
6049 if (bp->b_blkno == bp->b_lblkno) {
6050 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
6052 bp->b_blkno = blkno;
6054 newindirdep->ir_freeblks = NULL;
6055 newindirdep->ir_savebp =
6056 getblk(ump->um_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
6057 newindirdep->ir_bp = bp;
6058 BUF_KERNPROC(newindirdep->ir_savebp);
6059 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
6062 indirdep = newindirdep;
6063 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
6065 * If the block is not yet allocated we don't set DEPCOMPLETE so
6066 * that we don't free dependencies until the pointers are valid.
6067 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
6068 * than using the hash.
6070 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
6071 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
6073 indirdep->ir_state |= DEPCOMPLETE;
6078 * Called to finish the allocation of the "aip" allocated
6079 * by one of the two routines above.
6081 static struct freefrag *
6082 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
6083 struct buf *bp; /* in-memory copy of the indirect block */
6084 struct inode *ip; /* inode for file being extended */
6085 struct inodedep *inodedep; /* Inodedep for ip */
6086 struct allocindir *aip; /* allocindir allocated by the above routines */
6087 ufs_lbn_t lbn; /* Logical block number for this block. */
6090 struct indirdep *indirdep;
6091 struct allocindir *oldaip;
6092 struct freefrag *freefrag;
6094 struct ufsmount *ump;
6100 if (bp->b_lblkno >= 0)
6101 panic("setup_allocindir_phase2: not indir blk");
6102 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
6103 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
6104 indirdep = indirdep_lookup(mp, ip, bp);
6105 KASSERT(indirdep->ir_savebp != NULL,
6106 ("setup_allocindir_phase2 NULL ir_savebp"));
6107 aip->ai_indirdep = indirdep;
6109 * Check for an unwritten dependency for this indirect offset. If
6110 * there is, merge the old dependency into the new one. This happens
6111 * as a result of reallocblk only.
6114 if (aip->ai_oldblkno != 0) {
6115 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6116 if (oldaip->ai_offset == aip->ai_offset) {
6117 freefrag = allocindir_merge(aip, oldaip);
6121 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6122 if (oldaip->ai_offset == aip->ai_offset) {
6123 freefrag = allocindir_merge(aip, oldaip);
6129 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6134 * Merge two allocindirs which refer to the same block. Move newblock
6135 * dependencies and setup the freefrags appropriately.
6137 static struct freefrag *
6138 allocindir_merge(aip, oldaip)
6139 struct allocindir *aip;
6140 struct allocindir *oldaip;
6142 struct freefrag *freefrag;
6143 struct worklist *wk;
6145 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6146 panic("allocindir_merge: blkno");
6147 aip->ai_oldblkno = oldaip->ai_oldblkno;
6148 freefrag = aip->ai_freefrag;
6149 aip->ai_freefrag = oldaip->ai_freefrag;
6150 oldaip->ai_freefrag = NULL;
6151 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6153 * If we are tracking a new directory-block allocation,
6154 * move it from the old allocindir to the new allocindir.
6156 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6157 WORKLIST_REMOVE(wk);
6158 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6159 panic("allocindir_merge: extra newdirblk");
6160 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6163 * We can skip journaling for this freefrag and just complete
6164 * any pending journal work for the allocindir that is being
6165 * removed after the freefrag completes.
6167 if (freefrag->ff_jdep)
6168 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6169 LIST_REMOVE(oldaip, ai_next);
6170 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6171 &freefrag->ff_list, &freefrag->ff_jwork);
6172 free_newblk(&oldaip->ai_block);
6178 setup_freedirect(freeblks, ip, i, needj)
6179 struct freeblks *freeblks;
6184 struct ufsmount *ump;
6188 blkno = DIP(ip, i_db[i]);
6191 DIP_SET(ip, i_db[i], 0);
6193 frags = sblksize(ump->um_fs, ip->i_size, i);
6194 frags = numfrags(ump->um_fs, frags);
6195 newfreework(ump, freeblks, NULL, i, blkno, frags, 0, needj);
6199 setup_freeext(freeblks, ip, i, needj)
6200 struct freeblks *freeblks;
6205 struct ufsmount *ump;
6209 blkno = ip->i_din2->di_extb[i];
6212 ip->i_din2->di_extb[i] = 0;
6214 frags = sblksize(ump->um_fs, ip->i_din2->di_extsize, i);
6215 frags = numfrags(ump->um_fs, frags);
6216 newfreework(ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6220 setup_freeindir(freeblks, ip, i, lbn, needj)
6221 struct freeblks *freeblks;
6227 struct ufsmount *ump;
6230 blkno = DIP(ip, i_ib[i]);
6233 DIP_SET(ip, i_ib[i], 0);
6235 newfreework(ump, freeblks, NULL, lbn, blkno, ump->um_fs->fs_frag,
6239 static inline struct freeblks *
6244 struct freeblks *freeblks;
6246 freeblks = malloc(sizeof(struct freeblks),
6247 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6248 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6249 LIST_INIT(&freeblks->fb_jblkdephd);
6250 LIST_INIT(&freeblks->fb_jwork);
6251 freeblks->fb_ref = 0;
6252 freeblks->fb_cgwait = 0;
6253 freeblks->fb_state = ATTACHED;
6254 freeblks->fb_uid = ip->i_uid;
6255 freeblks->fb_inum = ip->i_number;
6256 freeblks->fb_vtype = ITOV(ip)->v_type;
6257 freeblks->fb_modrev = DIP(ip, i_modrev);
6258 freeblks->fb_devvp = ITODEVVP(ip);
6259 freeblks->fb_chkcnt = 0;
6260 freeblks->fb_len = 0;
6266 trunc_indirdep(indirdep, freeblks, bp, off)
6267 struct indirdep *indirdep;
6268 struct freeblks *freeblks;
6272 struct allocindir *aip, *aipn;
6275 * The first set of allocindirs won't be in savedbp.
6277 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6278 if (aip->ai_offset > off)
6279 cancel_allocindir(aip, bp, freeblks, 1);
6280 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6281 if (aip->ai_offset > off)
6282 cancel_allocindir(aip, bp, freeblks, 1);
6284 * These will exist in savedbp.
6286 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6287 if (aip->ai_offset > off)
6288 cancel_allocindir(aip, NULL, freeblks, 0);
6289 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6290 if (aip->ai_offset > off)
6291 cancel_allocindir(aip, NULL, freeblks, 0);
6295 * Follow the chain of indirects down to lastlbn creating a freework
6296 * structure for each. This will be used to start indir_trunc() at
6297 * the right offset and create the journal records for the parrtial
6298 * truncation. A second step will handle the truncated dependencies.
6301 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6302 struct freeblks *freeblks;
6308 struct indirdep *indirdep;
6309 struct indirdep *indirn;
6310 struct freework *freework;
6311 struct newblk *newblk;
6313 struct ufsmount *ump;
6326 mp = freeblks->fb_list.wk_mp;
6329 * Here, calls to VOP_BMAP() will fail. However, we already have
6330 * the on-disk address, so we just pass it to bread() instead of
6331 * having bread() attempt to calculate it using VOP_BMAP().
6333 error = breadn_flags(ITOV(ip), lbn, blkptrtodb(ump, blkno),
6334 (int)mp->mnt_stat.f_iosize, NULL, NULL, 0, NOCRED, 0, NULL, &bp);
6337 level = lbn_level(lbn);
6338 lbnadd = lbn_offset(ump->um_fs, level);
6340 * Compute the offset of the last block we want to keep. Store
6341 * in the freework the first block we want to completely free.
6343 off = (lastlbn - -(lbn + level)) / lbnadd;
6344 if (off + 1 == NINDIR(ump->um_fs))
6346 freework = newfreework(ump, freeblks, NULL, lbn, blkno, 0, off + 1, 0);
6348 * Link the freework into the indirdep. This will prevent any new
6349 * allocations from proceeding until we are finished with the
6350 * truncate and the block is written.
6353 indirdep = indirdep_lookup(mp, ip, bp);
6354 if (indirdep->ir_freeblks)
6355 panic("setup_trunc_indir: indirdep already truncated.");
6356 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6357 freework->fw_indir = indirdep;
6359 * Cancel any allocindirs that will not make it to disk.
6360 * We have to do this for all copies of the indirdep that
6361 * live on this newblk.
6363 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6364 if (newblk_lookup(mp, dbtofsb(ump->um_fs, bp->b_blkno), 0,
6366 panic("setup_trunc_indir: lost block");
6367 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6368 trunc_indirdep(indirn, freeblks, bp, off);
6370 trunc_indirdep(indirdep, freeblks, bp, off);
6373 * Creation is protected by the buf lock. The saveddata is only
6374 * needed if a full truncation follows a partial truncation but it
6375 * is difficult to allocate in that case so we fetch it anyway.
6377 if (indirdep->ir_saveddata == NULL)
6378 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6381 /* Fetch the blkno of the child and the zero start offset. */
6382 if (I_IS_UFS1(ip)) {
6383 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6384 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6386 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6387 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6390 /* Zero the truncated pointers. */
6391 end = bp->b_data + bp->b_bcount;
6392 bzero(start, end - start);
6398 lbn++; /* adjust level */
6399 lbn -= (off * lbnadd);
6400 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6404 * Complete the partial truncation of an indirect block setup by
6405 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6406 * copy and writes them to disk before the freeblks is allowed to complete.
6409 complete_trunc_indir(freework)
6410 struct freework *freework;
6412 struct freework *fwn;
6413 struct indirdep *indirdep;
6414 struct ufsmount *ump;
6419 ump = VFSTOUFS(freework->fw_list.wk_mp);
6421 indirdep = freework->fw_indir;
6423 bp = indirdep->ir_bp;
6424 /* See if the block was discarded. */
6427 /* Inline part of getdirtybuf(). We dont want bremfree. */
6428 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6430 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6431 LOCK_PTR(ump)) == 0)
6435 freework->fw_state |= DEPCOMPLETE;
6436 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6438 * Zero the pointers in the saved copy.
6440 if (indirdep->ir_state & UFS1FMT)
6441 start = sizeof(ufs1_daddr_t);
6443 start = sizeof(ufs2_daddr_t);
6444 start *= freework->fw_start;
6445 count = indirdep->ir_savebp->b_bcount - start;
6446 start += (uintptr_t)indirdep->ir_savebp->b_data;
6447 bzero((char *)start, count);
6449 * We need to start the next truncation in the list if it has not
6452 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6454 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6455 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6456 if ((fwn->fw_state & ONWORKLIST) == 0)
6457 freework_enqueue(fwn);
6460 * If bp is NULL the block was fully truncated, restore
6461 * the saved block list otherwise free it if it is no
6464 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6466 bcopy(indirdep->ir_saveddata,
6467 indirdep->ir_savebp->b_data,
6468 indirdep->ir_savebp->b_bcount);
6469 free(indirdep->ir_saveddata, M_INDIRDEP);
6470 indirdep->ir_saveddata = NULL;
6473 * When bp is NULL there is a full truncation pending. We
6474 * must wait for this full truncation to be journaled before
6475 * we can release this freework because the disk pointers will
6476 * never be written as zero.
6479 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6480 handle_written_freework(freework);
6482 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6483 &freework->fw_list);
6485 /* Complete when the real copy is written. */
6486 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6492 * Calculate the number of blocks we are going to release where datablocks
6493 * is the current total and length is the new file size.
6496 blkcount(fs, datablocks, length)
6498 ufs2_daddr_t datablocks;
6501 off_t totblks, numblks;
6504 numblks = howmany(length, fs->fs_bsize);
6505 if (numblks <= UFS_NDADDR) {
6506 totblks = howmany(length, fs->fs_fsize);
6509 totblks = blkstofrags(fs, numblks);
6510 numblks -= UFS_NDADDR;
6512 * Count all single, then double, then triple indirects required.
6513 * Subtracting one indirects worth of blocks for each pass
6514 * acknowledges one of each pointed to by the inode.
6517 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6518 numblks -= NINDIR(fs);
6521 numblks = howmany(numblks, NINDIR(fs));
6524 totblks = fsbtodb(fs, totblks);
6526 * Handle sparse files. We can't reclaim more blocks than the inode
6527 * references. We will correct it later in handle_complete_freeblks()
6528 * when we know the real count.
6530 if (totblks > datablocks)
6532 return (datablocks - totblks);
6536 * Handle freeblocks for journaled softupdate filesystems.
6538 * Contrary to normal softupdates, we must preserve the block pointers in
6539 * indirects until their subordinates are free. This is to avoid journaling
6540 * every block that is freed which may consume more space than the journal
6541 * itself. The recovery program will see the free block journals at the
6542 * base of the truncated area and traverse them to reclaim space. The
6543 * pointers in the inode may be cleared immediately after the journal
6544 * records are written because each direct and indirect pointer in the
6545 * inode is recorded in a journal. This permits full truncation to proceed
6546 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6548 * The algorithm is as follows:
6549 * 1) Traverse the in-memory state and create journal entries to release
6550 * the relevant blocks and full indirect trees.
6551 * 2) Traverse the indirect block chain adding partial truncation freework
6552 * records to indirects in the path to lastlbn. The freework will
6553 * prevent new allocation dependencies from being satisfied in this
6554 * indirect until the truncation completes.
6555 * 3) Read and lock the inode block, performing an update with the new size
6556 * and pointers. This prevents truncated data from becoming valid on
6557 * disk through step 4.
6558 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6559 * eliminate journal work for those records that do not require it.
6560 * 5) Schedule the journal records to be written followed by the inode block.
6561 * 6) Allocate any necessary frags for the end of file.
6562 * 7) Zero any partially truncated blocks.
6564 * From this truncation proceeds asynchronously using the freework and
6565 * indir_trunc machinery. The file will not be extended again into a
6566 * partially truncated indirect block until all work is completed but
6567 * the normal dependency mechanism ensures that it is rolled back/forward
6568 * as appropriate. Further truncation may occur without delay and is
6569 * serialized in indir_trunc().
6572 softdep_journal_freeblocks(ip, cred, length, flags)
6573 struct inode *ip; /* The inode whose length is to be reduced */
6575 off_t length; /* The new length for the file */
6576 int flags; /* IO_EXT and/or IO_NORMAL */
6578 struct freeblks *freeblks, *fbn;
6579 struct worklist *wk, *wkn;
6580 struct inodedep *inodedep;
6581 struct jblkdep *jblkdep;
6582 struct allocdirect *adp, *adpn;
6583 struct ufsmount *ump;
6588 ufs2_daddr_t extblocks, datablocks;
6589 ufs_lbn_t tmpval, lbn, lastlbn;
6590 int frags, lastoff, iboff, allocblock, needj, error, i;
6595 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6596 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6604 freeblks = newfreeblks(mp, ip);
6607 * If we're truncating a removed file that will never be written
6608 * we don't need to journal the block frees. The canceled journals
6609 * for the allocations will suffice.
6611 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6612 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6615 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6616 ip->i_number, length, needj);
6619 * Calculate the lbn that we are truncating to. This results in -1
6620 * if we're truncating the 0 bytes. So it is the last lbn we want
6621 * to keep, not the first lbn we want to truncate.
6623 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6624 lastoff = blkoff(fs, length);
6626 * Compute frags we are keeping in lastlbn. 0 means all.
6628 if (lastlbn >= 0 && lastlbn < UFS_NDADDR) {
6629 frags = fragroundup(fs, lastoff);
6630 /* adp offset of last valid allocdirect. */
6632 } else if (lastlbn > 0)
6634 if (fs->fs_magic == FS_UFS2_MAGIC)
6635 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6637 * Handle normal data blocks and indirects. This section saves
6638 * values used after the inode update to complete frag and indirect
6641 if ((flags & IO_NORMAL) != 0) {
6643 * Handle truncation of whole direct and indirect blocks.
6645 for (i = iboff + 1; i < UFS_NDADDR; i++)
6646 setup_freedirect(freeblks, ip, i, needj);
6647 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6649 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6650 /* Release a whole indirect tree. */
6651 if (lbn > lastlbn) {
6652 setup_freeindir(freeblks, ip, i, -lbn -i,
6656 iboff = i + UFS_NDADDR;
6658 * Traverse partially truncated indirect tree.
6660 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6661 setup_trunc_indir(freeblks, ip, -lbn - i,
6662 lastlbn, DIP(ip, i_ib[i]));
6665 * Handle partial truncation to a frag boundary.
6671 oldfrags = blksize(fs, ip, lastlbn);
6672 blkno = DIP(ip, i_db[lastlbn]);
6673 if (blkno && oldfrags != frags) {
6675 oldfrags = numfrags(fs, oldfrags);
6676 blkno += numfrags(fs, frags);
6677 newfreework(ump, freeblks, NULL, lastlbn,
6678 blkno, oldfrags, 0, needj);
6680 adjust_newfreework(freeblks,
6681 numfrags(fs, frags));
6682 } else if (blkno == 0)
6686 * Add a journal record for partial truncate if we are
6687 * handling indirect blocks. Non-indirects need no extra
6690 if (length != 0 && lastlbn >= UFS_NDADDR) {
6691 UFS_INODE_SET_FLAG(ip, IN_TRUNCATED);
6692 newjtrunc(freeblks, length, 0);
6694 ip->i_size = length;
6695 DIP_SET(ip, i_size, ip->i_size);
6696 datablocks = DIP(ip, i_blocks) - extblocks;
6698 datablocks = blkcount(fs, datablocks, length);
6699 freeblks->fb_len = length;
6701 if ((flags & IO_EXT) != 0) {
6702 for (i = 0; i < UFS_NXADDR; i++)
6703 setup_freeext(freeblks, ip, i, needj);
6704 ip->i_din2->di_extsize = 0;
6705 datablocks += extblocks;
6708 /* Reference the quotas in case the block count is wrong in the end. */
6709 quotaref(vp, freeblks->fb_quota);
6710 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
6712 freeblks->fb_chkcnt = -datablocks;
6714 fs->fs_pendingblocks += datablocks;
6716 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6718 * Handle truncation of incomplete alloc direct dependencies. We
6719 * hold the inode block locked to prevent incomplete dependencies
6720 * from reaching the disk while we are eliminating those that
6721 * have been truncated. This is a partially inlined ffs_update().
6724 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
6725 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6726 (int)fs->fs_bsize, cred, &bp);
6728 softdep_error("softdep_journal_freeblocks", error);
6731 if (bp->b_bufsize == fs->fs_bsize)
6732 bp->b_flags |= B_CLUSTEROK;
6733 softdep_update_inodeblock(ip, bp, 0);
6734 if (ump->um_fstype == UFS1) {
6735 *((struct ufs1_dinode *)bp->b_data +
6736 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
6738 ffs_update_dinode_ckhash(fs, ip->i_din2);
6739 *((struct ufs2_dinode *)bp->b_data +
6740 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
6743 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6744 if ((inodedep->id_state & IOSTARTED) != 0)
6745 panic("softdep_setup_freeblocks: inode busy");
6747 * Add the freeblks structure to the list of operations that
6748 * must await the zero'ed inode being written to disk. If we
6749 * still have a bitmap dependency (needj), then the inode
6750 * has never been written to disk, so we can process the
6751 * freeblks below once we have deleted the dependencies.
6754 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6756 freeblks->fb_state |= COMPLETE;
6757 if ((flags & IO_NORMAL) != 0) {
6758 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
6759 if (adp->ad_offset > iboff)
6760 cancel_allocdirect(&inodedep->id_inoupdt, adp,
6763 * Truncate the allocdirect. We could eliminate
6764 * or modify journal records as well.
6766 else if (adp->ad_offset == iboff && frags)
6767 adp->ad_newsize = frags;
6770 if ((flags & IO_EXT) != 0)
6771 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
6772 cancel_allocdirect(&inodedep->id_extupdt, adp,
6775 * Scan the bufwait list for newblock dependencies that will never
6778 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6779 if (wk->wk_type != D_ALLOCDIRECT)
6781 adp = WK_ALLOCDIRECT(wk);
6782 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
6783 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
6784 cancel_jfreeblk(freeblks, adp->ad_newblkno);
6785 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
6786 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
6792 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6793 add_to_journal(&jblkdep->jb_list);
6797 * Truncate dependency structures beyond length.
6799 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
6801 * This is only set when we need to allocate a fragment because
6802 * none existed at the end of a frag-sized file. It handles only
6803 * allocating a new, zero filled block.
6806 ip->i_size = length - lastoff;
6807 DIP_SET(ip, i_size, ip->i_size);
6808 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
6810 softdep_error("softdep_journal_freeblks", error);
6813 ip->i_size = length;
6814 DIP_SET(ip, i_size, length);
6815 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
6816 allocbuf(bp, frags);
6819 } else if (lastoff != 0 && vp->v_type != VDIR) {
6823 * Zero the end of a truncated frag or block.
6825 size = sblksize(fs, length, lastlbn);
6826 error = bread(vp, lastlbn, size, cred, &bp);
6828 softdep_error("softdep_journal_freeblks", error);
6831 bzero((char *)bp->b_data + lastoff, size - lastoff);
6836 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6837 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
6838 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
6840 * We zero earlier truncations so they don't erroneously
6843 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
6844 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
6846 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
6847 LIST_EMPTY(&freeblks->fb_jblkdephd))
6848 freeblks->fb_state |= INPROGRESS;
6853 handle_workitem_freeblocks(freeblks, 0);
6854 trunc_pages(ip, length, extblocks, flags);
6859 * Flush a JOP_SYNC to the journal.
6862 softdep_journal_fsync(ip)
6865 struct jfsync *jfsync;
6866 struct ufsmount *ump;
6869 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
6870 ("softdep_journal_fsync called on non-softdep filesystem"));
6871 if ((ip->i_flag & IN_TRUNCATED) == 0)
6873 ip->i_flag &= ~IN_TRUNCATED;
6874 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
6875 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ump));
6876 jfsync->jfs_size = ip->i_size;
6877 jfsync->jfs_ino = ip->i_number;
6879 add_to_journal(&jfsync->jfs_list);
6880 jwait(&jfsync->jfs_list, MNT_WAIT);
6885 * Block de-allocation dependencies.
6887 * When blocks are de-allocated, the on-disk pointers must be nullified before
6888 * the blocks are made available for use by other files. (The true
6889 * requirement is that old pointers must be nullified before new on-disk
6890 * pointers are set. We chose this slightly more stringent requirement to
6891 * reduce complexity.) Our implementation handles this dependency by updating
6892 * the inode (or indirect block) appropriately but delaying the actual block
6893 * de-allocation (i.e., freemap and free space count manipulation) until
6894 * after the updated versions reach stable storage. After the disk is
6895 * updated, the blocks can be safely de-allocated whenever it is convenient.
6896 * This implementation handles only the common case of reducing a file's
6897 * length to zero. Other cases are handled by the conventional synchronous
6900 * The ffs implementation with which we worked double-checks
6901 * the state of the block pointers and file size as it reduces
6902 * a file's length. Some of this code is replicated here in our
6903 * soft updates implementation. The freeblks->fb_chkcnt field is
6904 * used to transfer a part of this information to the procedure
6905 * that eventually de-allocates the blocks.
6907 * This routine should be called from the routine that shortens
6908 * a file's length, before the inode's size or block pointers
6909 * are modified. It will save the block pointer information for
6910 * later release and zero the inode so that the calling routine
6914 softdep_setup_freeblocks(ip, length, flags)
6915 struct inode *ip; /* The inode whose length is to be reduced */
6916 off_t length; /* The new length for the file */
6917 int flags; /* IO_EXT and/or IO_NORMAL */
6919 struct ufs1_dinode *dp1;
6920 struct ufs2_dinode *dp2;
6921 struct freeblks *freeblks;
6922 struct inodedep *inodedep;
6923 struct allocdirect *adp;
6924 struct ufsmount *ump;
6927 ufs2_daddr_t extblocks, datablocks;
6929 int i, delay, error;
6935 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6936 ("softdep_setup_freeblocks called on non-softdep filesystem"));
6937 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
6938 ip->i_number, length);
6939 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
6941 if ((error = bread(ump->um_devvp,
6942 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6943 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
6945 softdep_error("softdep_setup_freeblocks", error);
6948 freeblks = newfreeblks(mp, ip);
6951 if (fs->fs_magic == FS_UFS2_MAGIC)
6952 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6953 if ((flags & IO_NORMAL) != 0) {
6954 for (i = 0; i < UFS_NDADDR; i++)
6955 setup_freedirect(freeblks, ip, i, 0);
6956 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR;
6958 i++, lbn += tmpval, tmpval *= NINDIR(fs))
6959 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
6961 DIP_SET(ip, i_size, 0);
6962 datablocks = DIP(ip, i_blocks) - extblocks;
6964 if ((flags & IO_EXT) != 0) {
6965 for (i = 0; i < UFS_NXADDR; i++)
6966 setup_freeext(freeblks, ip, i, 0);
6967 ip->i_din2->di_extsize = 0;
6968 datablocks += extblocks;
6971 /* Reference the quotas in case the block count is wrong in the end. */
6972 quotaref(ITOV(ip), freeblks->fb_quota);
6973 (void) chkdq(ip, -datablocks, NOCRED, FORCE);
6975 freeblks->fb_chkcnt = -datablocks;
6977 fs->fs_pendingblocks += datablocks;
6979 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6981 * Push the zero'ed inode to its disk buffer so that we are free
6982 * to delete its dependencies below. Once the dependencies are gone
6983 * the buffer can be safely released.
6985 if (ump->um_fstype == UFS1) {
6986 dp1 = ((struct ufs1_dinode *)bp->b_data +
6987 ino_to_fsbo(fs, ip->i_number));
6988 ip->i_din1->di_freelink = dp1->di_freelink;
6991 dp2 = ((struct ufs2_dinode *)bp->b_data +
6992 ino_to_fsbo(fs, ip->i_number));
6993 ip->i_din2->di_freelink = dp2->di_freelink;
6994 ffs_update_dinode_ckhash(fs, ip->i_din2);
6998 * Find and eliminate any inode dependencies.
7001 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
7002 if ((inodedep->id_state & IOSTARTED) != 0)
7003 panic("softdep_setup_freeblocks: inode busy");
7005 * Add the freeblks structure to the list of operations that
7006 * must await the zero'ed inode being written to disk. If we
7007 * still have a bitmap dependency (delay == 0), then the inode
7008 * has never been written to disk, so we can process the
7009 * freeblks below once we have deleted the dependencies.
7011 delay = (inodedep->id_state & DEPCOMPLETE);
7013 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
7015 freeblks->fb_state |= COMPLETE;
7017 * Because the file length has been truncated to zero, any
7018 * pending block allocation dependency structures associated
7019 * with this inode are obsolete and can simply be de-allocated.
7020 * We must first merge the two dependency lists to get rid of
7021 * any duplicate freefrag structures, then purge the merged list.
7022 * If we still have a bitmap dependency, then the inode has never
7023 * been written to disk, so we can free any fragments without delay.
7025 if (flags & IO_NORMAL) {
7026 merge_inode_lists(&inodedep->id_newinoupdt,
7027 &inodedep->id_inoupdt);
7028 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
7029 cancel_allocdirect(&inodedep->id_inoupdt, adp,
7032 if (flags & IO_EXT) {
7033 merge_inode_lists(&inodedep->id_newextupdt,
7034 &inodedep->id_extupdt);
7035 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
7036 cancel_allocdirect(&inodedep->id_extupdt, adp,
7041 trunc_dependencies(ip, freeblks, -1, 0, flags);
7043 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
7044 (void) free_inodedep(inodedep);
7045 freeblks->fb_state |= DEPCOMPLETE;
7047 * If the inode with zeroed block pointers is now on disk
7048 * we can start freeing blocks.
7050 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
7051 freeblks->fb_state |= INPROGRESS;
7056 handle_workitem_freeblocks(freeblks, 0);
7057 trunc_pages(ip, length, extblocks, flags);
7061 * Eliminate pages from the page cache that back parts of this inode and
7062 * adjust the vnode pager's idea of our size. This prevents stale data
7063 * from hanging around in the page cache.
7066 trunc_pages(ip, length, extblocks, flags)
7069 ufs2_daddr_t extblocks;
7079 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
7080 if ((flags & IO_EXT) != 0)
7081 vn_pages_remove(vp, extend, 0);
7082 if ((flags & IO_NORMAL) == 0)
7084 BO_LOCK(&vp->v_bufobj);
7086 BO_UNLOCK(&vp->v_bufobj);
7088 * The vnode pager eliminates file pages we eliminate indirects
7091 vnode_pager_setsize(vp, length);
7093 * Calculate the end based on the last indirect we want to keep. If
7094 * the block extends into indirects we can just use the negative of
7095 * its lbn. Doubles and triples exist at lower numbers so we must
7096 * be careful not to remove those, if they exist. double and triple
7097 * indirect lbns do not overlap with others so it is not important
7098 * to verify how many levels are required.
7100 lbn = lblkno(fs, length);
7101 if (lbn >= UFS_NDADDR) {
7102 /* Calculate the virtual lbn of the triple indirect. */
7103 lbn = -lbn - (UFS_NIADDR - 1);
7104 end = OFF_TO_IDX(lblktosize(fs, lbn));
7107 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
7111 * See if the buf bp is in the range eliminated by truncation.
7114 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7124 /* Only match ext/normal blocks as appropriate. */
7125 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7126 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7128 /* ALTDATA is always a full truncation. */
7129 if ((bp->b_xflags & BX_ALTDATA) != 0)
7131 /* -1 is full truncation. */
7135 * If this is a partial truncate we only want those
7136 * blocks and indirect blocks that cover the range
7141 lbn = -(lbn + lbn_level(lbn));
7144 /* Here we only truncate lblkno if it's partial. */
7145 if (lbn == lastlbn) {
7154 * Eliminate any dependencies that exist in memory beyond lblkno:off
7157 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7159 struct freeblks *freeblks;
7170 * We must wait for any I/O in progress to finish so that
7171 * all potential buffers on the dirty list will be visible.
7172 * Once they are all there, walk the list and get rid of
7179 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7180 bp->b_vflags &= ~BV_SCANNED;
7182 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7183 if (bp->b_vflags & BV_SCANNED)
7185 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7186 bp->b_vflags |= BV_SCANNED;
7189 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7190 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7193 if (deallocate_dependencies(bp, freeblks, blkoff))
7201 * Now do the work of vtruncbuf while also matching indirect blocks.
7203 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7204 bp->b_vflags &= ~BV_SCANNED;
7206 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7207 if (bp->b_vflags & BV_SCANNED)
7209 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7210 bp->b_vflags |= BV_SCANNED;
7214 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7215 BO_LOCKPTR(bo)) == ENOLCK) {
7219 bp->b_vflags |= BV_SCANNED;
7222 allocbuf(bp, blkoff);
7225 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7236 cancel_pagedep(pagedep, freeblks, blkoff)
7237 struct pagedep *pagedep;
7238 struct freeblks *freeblks;
7241 struct jremref *jremref;
7242 struct jmvref *jmvref;
7243 struct dirrem *dirrem, *tmp;
7247 * Copy any directory remove dependencies to the list
7248 * to be processed after the freeblks proceeds. If
7249 * directory entry never made it to disk they
7250 * can be dumped directly onto the work list.
7252 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7253 /* Skip this directory removal if it is intended to remain. */
7254 if (dirrem->dm_offset < blkoff)
7257 * If there are any dirrems we wait for the journal write
7258 * to complete and then restart the buf scan as the lock
7261 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7262 jwait(&jremref->jr_list, MNT_WAIT);
7265 LIST_REMOVE(dirrem, dm_next);
7266 dirrem->dm_dirinum = pagedep->pd_ino;
7267 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7269 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7270 jwait(&jmvref->jm_list, MNT_WAIT);
7274 * When we're partially truncating a pagedep we just want to flush
7275 * journal entries and return. There can not be any adds in the
7276 * truncated portion of the directory and newblk must remain if
7277 * part of the block remains.
7282 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7283 if (dap->da_offset > blkoff)
7284 panic("cancel_pagedep: diradd %p off %d > %d",
7285 dap, dap->da_offset, blkoff);
7286 for (i = 0; i < DAHASHSZ; i++)
7287 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7288 if (dap->da_offset > blkoff)
7289 panic("cancel_pagedep: diradd %p off %d > %d",
7290 dap, dap->da_offset, blkoff);
7294 * There should be no directory add dependencies present
7295 * as the directory could not be truncated until all
7296 * children were removed.
7298 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7299 ("deallocate_dependencies: pendinghd != NULL"));
7300 for (i = 0; i < DAHASHSZ; i++)
7301 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7302 ("deallocate_dependencies: diraddhd != NULL"));
7303 if ((pagedep->pd_state & NEWBLOCK) != 0)
7304 free_newdirblk(pagedep->pd_newdirblk);
7305 if (free_pagedep(pagedep) == 0)
7306 panic("Failed to free pagedep %p", pagedep);
7311 * Reclaim any dependency structures from a buffer that is about to
7312 * be reallocated to a new vnode. The buffer must be locked, thus,
7313 * no I/O completion operations can occur while we are manipulating
7314 * its associated dependencies. The mutex is held so that other I/O's
7315 * associated with related dependencies do not occur.
7318 deallocate_dependencies(bp, freeblks, off)
7320 struct freeblks *freeblks;
7323 struct indirdep *indirdep;
7324 struct pagedep *pagedep;
7325 struct worklist *wk, *wkn;
7326 struct ufsmount *ump;
7328 ump = softdep_bp_to_mp(bp);
7332 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7333 switch (wk->wk_type) {
7335 indirdep = WK_INDIRDEP(wk);
7336 if (bp->b_lblkno >= 0 ||
7337 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7338 panic("deallocate_dependencies: not indir");
7339 cancel_indirdep(indirdep, bp, freeblks);
7343 pagedep = WK_PAGEDEP(wk);
7344 if (cancel_pagedep(pagedep, freeblks, off)) {
7352 * Simply remove the allocindir, we'll find it via
7353 * the indirdep where we can clear pointers if
7356 WORKLIST_REMOVE(wk);
7361 * A truncation is waiting for the zero'd pointers
7362 * to be written. It can be freed when the freeblks
7365 WORKLIST_REMOVE(wk);
7366 wk->wk_state |= ONDEPLIST;
7367 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7375 panic("deallocate_dependencies: Unexpected type %s",
7376 TYPENAME(wk->wk_type));
7383 * Don't throw away this buf, we were partially truncating and
7384 * some deps may always remain.
7388 bp->b_vflags |= BV_SCANNED;
7391 bp->b_flags |= B_INVAL | B_NOCACHE;
7397 * An allocdirect is being canceled due to a truncate. We must make sure
7398 * the journal entry is released in concert with the blkfree that releases
7399 * the storage. Completed journal entries must not be released until the
7400 * space is no longer pointed to by the inode or in the bitmap.
7403 cancel_allocdirect(adphead, adp, freeblks)
7404 struct allocdirectlst *adphead;
7405 struct allocdirect *adp;
7406 struct freeblks *freeblks;
7408 struct freework *freework;
7409 struct newblk *newblk;
7410 struct worklist *wk;
7412 TAILQ_REMOVE(adphead, adp, ad_next);
7413 newblk = (struct newblk *)adp;
7416 * Find the correct freework structure.
7418 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7419 if (wk->wk_type != D_FREEWORK)
7421 freework = WK_FREEWORK(wk);
7422 if (freework->fw_blkno == newblk->nb_newblkno)
7425 if (freework == NULL)
7426 panic("cancel_allocdirect: Freework not found");
7428 * If a newblk exists at all we still have the journal entry that
7429 * initiated the allocation so we do not need to journal the free.
7431 cancel_jfreeblk(freeblks, freework->fw_blkno);
7433 * If the journal hasn't been written the jnewblk must be passed
7434 * to the call to ffs_blkfree that reclaims the space. We accomplish
7435 * this by linking the journal dependency into the freework to be
7436 * freed when freework_freeblock() is called. If the journal has
7437 * been written we can simply reclaim the journal space when the
7438 * freeblks work is complete.
7440 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7441 &freeblks->fb_jwork);
7442 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7447 * Cancel a new block allocation. May be an indirect or direct block. We
7448 * remove it from various lists and return any journal record that needs to
7449 * be resolved by the caller.
7451 * A special consideration is made for indirects which were never pointed
7452 * at on disk and will never be found once this block is released.
7454 static struct jnewblk *
7455 cancel_newblk(newblk, wk, wkhd)
7456 struct newblk *newblk;
7457 struct worklist *wk;
7458 struct workhead *wkhd;
7460 struct jnewblk *jnewblk;
7462 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7464 newblk->nb_state |= GOINGAWAY;
7466 * Previously we traversed the completedhd on each indirdep
7467 * attached to this newblk to cancel them and gather journal
7468 * work. Since we need only the oldest journal segment and
7469 * the lowest point on the tree will always have the oldest
7470 * journal segment we are free to release the segments
7471 * of any subordinates and may leave the indirdep list to
7472 * indirdep_complete() when this newblk is freed.
7474 if (newblk->nb_state & ONDEPLIST) {
7475 newblk->nb_state &= ~ONDEPLIST;
7476 LIST_REMOVE(newblk, nb_deps);
7478 if (newblk->nb_state & ONWORKLIST)
7479 WORKLIST_REMOVE(&newblk->nb_list);
7481 * If the journal entry hasn't been written we save a pointer to
7482 * the dependency that frees it until it is written or the
7483 * superseding operation completes.
7485 jnewblk = newblk->nb_jnewblk;
7486 if (jnewblk != NULL && wk != NULL) {
7487 newblk->nb_jnewblk = NULL;
7488 jnewblk->jn_dep = wk;
7490 if (!LIST_EMPTY(&newblk->nb_jwork))
7491 jwork_move(wkhd, &newblk->nb_jwork);
7493 * When truncating we must free the newdirblk early to remove
7494 * the pagedep from the hash before returning.
7496 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7497 free_newdirblk(WK_NEWDIRBLK(wk));
7498 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7499 panic("cancel_newblk: extra newdirblk");
7505 * Schedule the freefrag associated with a newblk to be released once
7506 * the pointers are written and the previous block is no longer needed.
7509 newblk_freefrag(newblk)
7510 struct newblk *newblk;
7512 struct freefrag *freefrag;
7514 if (newblk->nb_freefrag == NULL)
7516 freefrag = newblk->nb_freefrag;
7517 newblk->nb_freefrag = NULL;
7518 freefrag->ff_state |= COMPLETE;
7519 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7520 add_to_worklist(&freefrag->ff_list, 0);
7524 * Free a newblk. Generate a new freefrag work request if appropriate.
7525 * This must be called after the inode pointer and any direct block pointers
7526 * are valid or fully removed via truncate or frag extension.
7530 struct newblk *newblk;
7532 struct indirdep *indirdep;
7533 struct worklist *wk;
7535 KASSERT(newblk->nb_jnewblk == NULL,
7536 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7537 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7538 ("free_newblk: unclaimed newblk"));
7539 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7540 newblk_freefrag(newblk);
7541 if (newblk->nb_state & ONDEPLIST)
7542 LIST_REMOVE(newblk, nb_deps);
7543 if (newblk->nb_state & ONWORKLIST)
7544 WORKLIST_REMOVE(&newblk->nb_list);
7545 LIST_REMOVE(newblk, nb_hash);
7546 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7547 free_newdirblk(WK_NEWDIRBLK(wk));
7548 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7549 panic("free_newblk: extra newdirblk");
7550 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7551 indirdep_complete(indirdep);
7552 handle_jwork(&newblk->nb_jwork);
7553 WORKITEM_FREE(newblk, D_NEWBLK);
7557 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7560 free_newdirblk(newdirblk)
7561 struct newdirblk *newdirblk;
7563 struct pagedep *pagedep;
7565 struct worklist *wk;
7567 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7568 WORKLIST_REMOVE(&newdirblk->db_list);
7570 * If the pagedep is still linked onto the directory buffer
7571 * dependency chain, then some of the entries on the
7572 * pd_pendinghd list may not be committed to disk yet. In
7573 * this case, we will simply clear the NEWBLOCK flag and
7574 * let the pd_pendinghd list be processed when the pagedep
7575 * is next written. If the pagedep is no longer on the buffer
7576 * dependency chain, then all the entries on the pd_pending
7577 * list are committed to disk and we can free them here.
7579 pagedep = newdirblk->db_pagedep;
7580 pagedep->pd_state &= ~NEWBLOCK;
7581 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7582 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7583 free_diradd(dap, NULL);
7585 * If no dependencies remain, the pagedep will be freed.
7587 free_pagedep(pagedep);
7589 /* Should only ever be one item in the list. */
7590 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7591 WORKLIST_REMOVE(wk);
7592 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7594 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7598 * Prepare an inode to be freed. The actual free operation is not
7599 * done until the zero'ed inode has been written to disk.
7602 softdep_freefile(pvp, ino, mode)
7607 struct inode *ip = VTOI(pvp);
7608 struct inodedep *inodedep;
7609 struct freefile *freefile;
7610 struct freeblks *freeblks;
7611 struct ufsmount *ump;
7614 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7615 ("softdep_freefile called on non-softdep filesystem"));
7617 * This sets up the inode de-allocation dependency.
7619 freefile = malloc(sizeof(struct freefile),
7620 M_FREEFILE, M_SOFTDEP_FLAGS);
7621 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7622 freefile->fx_mode = mode;
7623 freefile->fx_oldinum = ino;
7624 freefile->fx_devvp = ump->um_devvp;
7625 LIST_INIT(&freefile->fx_jwork);
7627 ump->um_fs->fs_pendinginodes += 1;
7631 * If the inodedep does not exist, then the zero'ed inode has
7632 * been written to disk. If the allocated inode has never been
7633 * written to disk, then the on-disk inode is zero'ed. In either
7634 * case we can free the file immediately. If the journal was
7635 * canceled before being written the inode will never make it to
7636 * disk and we must send the canceled journal entrys to
7637 * ffs_freefile() to be cleared in conjunction with the bitmap.
7638 * Any blocks waiting on the inode to write can be safely freed
7639 * here as it will never been written.
7642 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7645 * Clear out freeblks that no longer need to reference
7649 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7650 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7652 freeblks->fb_state &= ~ONDEPLIST;
7655 * Remove this inode from the unlinked list.
7657 if (inodedep->id_state & UNLINKED) {
7659 * Save the journal work to be freed with the bitmap
7660 * before we clear UNLINKED. Otherwise it can be lost
7661 * if the inode block is written.
7663 handle_bufwait(inodedep, &freefile->fx_jwork);
7664 clear_unlinked_inodedep(inodedep);
7666 * Re-acquire inodedep as we've dropped the
7667 * per-filesystem lock in clear_unlinked_inodedep().
7669 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7672 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7674 handle_workitem_freefile(freefile);
7677 if ((inodedep->id_state & DEPCOMPLETE) == 0)
7678 inodedep->id_state |= GOINGAWAY;
7679 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7681 if (ip->i_number == ino)
7682 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
7686 * Check to see if an inode has never been written to disk. If
7687 * so free the inodedep and return success, otherwise return failure.
7689 * If we still have a bitmap dependency, then the inode has never
7690 * been written to disk. Drop the dependency as it is no longer
7691 * necessary since the inode is being deallocated. We set the
7692 * ALLCOMPLETE flags since the bitmap now properly shows that the
7693 * inode is not allocated. Even if the inode is actively being
7694 * written, it has been rolled back to its zero'ed state, so we
7695 * are ensured that a zero inode is what is on the disk. For short
7696 * lived files, this change will usually result in removing all the
7697 * dependencies from the inode so that it can be freed immediately.
7700 check_inode_unwritten(inodedep)
7701 struct inodedep *inodedep;
7704 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7706 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
7707 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7708 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7709 !LIST_EMPTY(&inodedep->id_bufwait) ||
7710 !LIST_EMPTY(&inodedep->id_inowait) ||
7711 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7712 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7713 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7714 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7715 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7716 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7717 inodedep->id_mkdiradd != NULL ||
7718 inodedep->id_nlinkdelta != 0)
7721 * Another process might be in initiate_write_inodeblock_ufs[12]
7722 * trying to allocate memory without holding "Softdep Lock".
7724 if ((inodedep->id_state & IOSTARTED) != 0 &&
7725 inodedep->id_savedino1 == NULL)
7728 if (inodedep->id_state & ONDEPLIST)
7729 LIST_REMOVE(inodedep, id_deps);
7730 inodedep->id_state &= ~ONDEPLIST;
7731 inodedep->id_state |= ALLCOMPLETE;
7732 inodedep->id_bmsafemap = NULL;
7733 if (inodedep->id_state & ONWORKLIST)
7734 WORKLIST_REMOVE(&inodedep->id_list);
7735 if (inodedep->id_savedino1 != NULL) {
7736 free(inodedep->id_savedino1, M_SAVEDINO);
7737 inodedep->id_savedino1 = NULL;
7739 if (free_inodedep(inodedep) == 0)
7740 panic("check_inode_unwritten: busy inode");
7745 check_inodedep_free(inodedep)
7746 struct inodedep *inodedep;
7749 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7750 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
7751 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7752 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7753 !LIST_EMPTY(&inodedep->id_bufwait) ||
7754 !LIST_EMPTY(&inodedep->id_inowait) ||
7755 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7756 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7757 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7758 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7759 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7760 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7761 inodedep->id_mkdiradd != NULL ||
7762 inodedep->id_nlinkdelta != 0 ||
7763 inodedep->id_savedino1 != NULL)
7769 * Try to free an inodedep structure. Return 1 if it could be freed.
7772 free_inodedep(inodedep)
7773 struct inodedep *inodedep;
7776 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7777 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7778 !check_inodedep_free(inodedep))
7780 if (inodedep->id_state & ONDEPLIST)
7781 LIST_REMOVE(inodedep, id_deps);
7782 LIST_REMOVE(inodedep, id_hash);
7783 WORKITEM_FREE(inodedep, D_INODEDEP);
7788 * Free the block referenced by a freework structure. The parent freeblks
7789 * structure is released and completed when the final cg bitmap reaches
7790 * the disk. This routine may be freeing a jnewblk which never made it to
7791 * disk in which case we do not have to wait as the operation is undone
7792 * in memory immediately.
7795 freework_freeblock(freework, key)
7796 struct freework *freework;
7799 struct freeblks *freeblks;
7800 struct jnewblk *jnewblk;
7801 struct ufsmount *ump;
7802 struct workhead wkhd;
7807 ump = VFSTOUFS(freework->fw_list.wk_mp);
7810 * Handle partial truncate separately.
7812 if (freework->fw_indir) {
7813 complete_trunc_indir(freework);
7816 freeblks = freework->fw_freeblks;
7818 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7819 bsize = lfragtosize(fs, freework->fw_frags);
7822 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7823 * on the indirblk hashtable and prevents premature freeing.
7825 freework->fw_state |= DEPCOMPLETE;
7827 * SUJ needs to wait for the segment referencing freed indirect
7828 * blocks to expire so that we know the checker will not confuse
7829 * a re-allocated indirect block with its old contents.
7831 if (needj && freework->fw_lbn <= -UFS_NDADDR)
7832 indirblk_insert(freework);
7834 * If we are canceling an existing jnewblk pass it to the free
7835 * routine, otherwise pass the freeblk which will ultimately
7836 * release the freeblks. If we're not journaling, we can just
7837 * free the freeblks immediately.
7839 jnewblk = freework->fw_jnewblk;
7840 if (jnewblk != NULL) {
7841 cancel_jnewblk(jnewblk, &wkhd);
7844 freework->fw_state |= DELAYEDFREE;
7845 freeblks->fb_cgwait++;
7846 WORKLIST_INSERT(&wkhd, &freework->fw_list);
7849 freeblks_free(ump, freeblks, btodb(bsize));
7851 "freework_freeblock: ino %jd blkno %jd lbn %jd size %d",
7852 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
7853 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
7854 freeblks->fb_inum, freeblks->fb_vtype, &wkhd, key);
7857 * The jnewblk will be discarded and the bits in the map never
7858 * made it to disk. We can immediately free the freeblk.
7861 handle_written_freework(freework);
7865 * We enqueue freework items that need processing back on the freeblks and
7866 * add the freeblks to the worklist. This makes it easier to find all work
7867 * required to flush a truncation in process_truncates().
7870 freework_enqueue(freework)
7871 struct freework *freework;
7873 struct freeblks *freeblks;
7875 freeblks = freework->fw_freeblks;
7876 if ((freework->fw_state & INPROGRESS) == 0)
7877 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
7878 if ((freeblks->fb_state &
7879 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
7880 LIST_EMPTY(&freeblks->fb_jblkdephd))
7881 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7885 * Start, continue, or finish the process of freeing an indirect block tree.
7886 * The free operation may be paused at any point with fw_off containing the
7887 * offset to restart from. This enables us to implement some flow control
7888 * for large truncates which may fan out and generate a huge number of
7892 handle_workitem_indirblk(freework)
7893 struct freework *freework;
7895 struct freeblks *freeblks;
7896 struct ufsmount *ump;
7899 freeblks = freework->fw_freeblks;
7900 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7902 if (freework->fw_state & DEPCOMPLETE) {
7903 handle_written_freework(freework);
7906 if (freework->fw_off == NINDIR(fs)) {
7907 freework_freeblock(freework, SINGLETON_KEY);
7910 freework->fw_state |= INPROGRESS;
7912 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
7918 * Called when a freework structure attached to a cg buf is written. The
7919 * ref on either the parent or the freeblks structure is released and
7920 * the freeblks is added back to the worklist if there is more work to do.
7923 handle_written_freework(freework)
7924 struct freework *freework;
7926 struct freeblks *freeblks;
7927 struct freework *parent;
7929 freeblks = freework->fw_freeblks;
7930 parent = freework->fw_parent;
7931 if (freework->fw_state & DELAYEDFREE)
7932 freeblks->fb_cgwait--;
7933 freework->fw_state |= COMPLETE;
7934 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
7935 WORKITEM_FREE(freework, D_FREEWORK);
7937 if (--parent->fw_ref == 0)
7938 freework_enqueue(parent);
7941 if (--freeblks->fb_ref != 0)
7943 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
7944 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
7945 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7949 * This workitem routine performs the block de-allocation.
7950 * The workitem is added to the pending list after the updated
7951 * inode block has been written to disk. As mentioned above,
7952 * checks regarding the number of blocks de-allocated (compared
7953 * to the number of blocks allocated for the file) are also
7954 * performed in this function.
7957 handle_workitem_freeblocks(freeblks, flags)
7958 struct freeblks *freeblks;
7961 struct freework *freework;
7962 struct newblk *newblk;
7963 struct allocindir *aip;
7964 struct ufsmount *ump;
7965 struct worklist *wk;
7968 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
7969 ("handle_workitem_freeblocks: Journal entries not written."));
7970 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7971 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
7973 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
7974 WORKLIST_REMOVE(wk);
7975 switch (wk->wk_type) {
7977 wk->wk_state |= COMPLETE;
7978 add_to_worklist(wk, 0);
7982 free_newblk(WK_NEWBLK(wk));
7986 aip = WK_ALLOCINDIR(wk);
7988 if (aip->ai_state & DELAYEDFREE) {
7990 freework = newfreework(ump, freeblks, NULL,
7991 aip->ai_lbn, aip->ai_newblkno,
7992 ump->um_fs->fs_frag, 0, 0);
7995 newblk = WK_NEWBLK(wk);
7996 if (newblk->nb_jnewblk) {
7997 freework->fw_jnewblk = newblk->nb_jnewblk;
7998 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
7999 newblk->nb_jnewblk = NULL;
8001 free_newblk(newblk);
8005 freework = WK_FREEWORK(wk);
8006 if (freework->fw_lbn <= -UFS_NDADDR)
8007 handle_workitem_indirblk(freework);
8009 freework_freeblock(freework, key);
8012 panic("handle_workitem_freeblocks: Unknown type %s",
8013 TYPENAME(wk->wk_type));
8016 if (freeblks->fb_ref != 0) {
8017 freeblks->fb_state &= ~INPROGRESS;
8018 wake_worklist(&freeblks->fb_list);
8022 ffs_blkrelease_finish(ump, key);
8024 return handle_complete_freeblocks(freeblks, flags);
8029 * Handle completion of block free via truncate. This allows fs_pending
8030 * to track the actual free block count more closely than if we only updated
8031 * it at the end. We must be careful to handle cases where the block count
8032 * on free was incorrect.
8035 freeblks_free(ump, freeblks, blocks)
8036 struct ufsmount *ump;
8037 struct freeblks *freeblks;
8041 ufs2_daddr_t remain;
8044 remain = -freeblks->fb_chkcnt;
8045 freeblks->fb_chkcnt += blocks;
8047 if (remain < blocks)
8050 fs->fs_pendingblocks -= blocks;
8056 * Once all of the freework workitems are complete we can retire the
8057 * freeblocks dependency and any journal work awaiting completion. This
8058 * can not be called until all other dependencies are stable on disk.
8061 handle_complete_freeblocks(freeblks, flags)
8062 struct freeblks *freeblks;
8065 struct inodedep *inodedep;
8069 struct ufsmount *ump;
8072 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8074 flags = LK_EXCLUSIVE | flags;
8075 spare = freeblks->fb_chkcnt;
8078 * If we did not release the expected number of blocks we may have
8079 * to adjust the inode block count here. Only do so if it wasn't
8080 * a truncation to zero and the modrev still matches.
8082 if (spare && freeblks->fb_len != 0) {
8083 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8084 flags, &vp, FFSV_FORCEINSMQ) != 0)
8087 if (ip->i_mode == 0) {
8089 } else if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
8090 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
8091 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
8093 * We must wait so this happens before the
8094 * journal is reclaimed.
8102 fs->fs_pendingblocks += spare;
8108 quotaadj(freeblks->fb_quota, ump, -spare);
8109 quotarele(freeblks->fb_quota);
8112 if (freeblks->fb_state & ONDEPLIST) {
8113 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
8115 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
8116 freeblks->fb_state &= ~ONDEPLIST;
8117 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8118 free_inodedep(inodedep);
8121 * All of the freeblock deps must be complete prior to this call
8122 * so it's now safe to complete earlier outstanding journal entries.
8124 handle_jwork(&freeblks->fb_jwork);
8125 WORKITEM_FREE(freeblks, D_FREEBLKS);
8131 * Release blocks associated with the freeblks and stored in the indirect
8132 * block dbn. If level is greater than SINGLE, the block is an indirect block
8133 * and recursive calls to indirtrunc must be used to cleanse other indirect
8136 * This handles partial and complete truncation of blocks. Partial is noted
8137 * with goingaway == 0. In this case the freework is completed after the
8138 * zero'd indirects are written to disk. For full truncation the freework
8139 * is completed after the block is freed.
8142 indir_trunc(freework, dbn, lbn)
8143 struct freework *freework;
8147 struct freework *nfreework;
8148 struct workhead wkhd;
8149 struct freeblks *freeblks;
8152 struct indirdep *indirdep;
8154 struct ufsmount *ump;
8156 ufs2_daddr_t nb, nnb, *bap2;
8157 ufs_lbn_t lbnadd, nlbn;
8159 int nblocks, ufs1fmt, freedblocks;
8160 int goingaway, freedeps, needj, level, cnt, i;
8162 freeblks = freework->fw_freeblks;
8163 mp = freeblks->fb_list.wk_mp;
8167 * Get buffer of block pointers to be freed. There are three cases:
8169 * 1) Partial truncate caches the indirdep pointer in the freework
8170 * which provides us a back copy to the save bp which holds the
8171 * pointers we want to clear. When this completes the zero
8172 * pointers are written to the real copy.
8173 * 2) The indirect is being completely truncated, cancel_indirdep()
8174 * eliminated the real copy and placed the indirdep on the saved
8175 * copy. The indirdep and buf are discarded when this completes.
8176 * 3) The indirect was not in memory, we read a copy off of the disk
8177 * using the devvp and drop and invalidate the buffer when we're
8182 if (freework->fw_indir != NULL) {
8184 indirdep = freework->fw_indir;
8185 bp = indirdep->ir_savebp;
8186 if (bp == NULL || bp->b_blkno != dbn)
8187 panic("indir_trunc: Bad saved buf %p blkno %jd",
8189 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8191 * The lock prevents the buf dep list from changing and
8192 * indirects on devvp should only ever have one dependency.
8194 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8195 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8196 panic("indir_trunc: Bad indirdep %p from buf %p",
8198 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
8199 NOCRED, &bp) != 0) {
8204 /* Protects against a race with complete_trunc_indir(). */
8205 freework->fw_state &= ~INPROGRESS;
8207 * If we have an indirdep we need to enforce the truncation order
8208 * and discard it when it is complete.
8211 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8212 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8214 * Add the complete truncate to the list on the
8215 * indirdep to enforce in-order processing.
8217 if (freework->fw_indir == NULL)
8218 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8224 * If we're goingaway, free the indirdep. Otherwise it will
8225 * linger until the write completes.
8228 free_indirdep(indirdep);
8231 /* Initialize pointers depending on block size. */
8232 if (ump->um_fstype == UFS1) {
8233 bap1 = (ufs1_daddr_t *)bp->b_data;
8234 nb = bap1[freework->fw_off];
8238 bap2 = (ufs2_daddr_t *)bp->b_data;
8239 nb = bap2[freework->fw_off];
8243 level = lbn_level(lbn);
8244 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8245 lbnadd = lbn_offset(fs, level);
8246 nblocks = btodb(fs->fs_bsize);
8247 nfreework = freework;
8251 * Reclaim blocks. Traverses into nested indirect levels and
8252 * arranges for the current level to be freed when subordinates
8253 * are free when journaling.
8255 key = ffs_blkrelease_start(ump, freeblks->fb_devvp, freeblks->fb_inum);
8256 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8257 if (UFS_CHECK_BLKNO(mp, freeblks->fb_inum, nb,
8260 if (i != NINDIR(fs) - 1) {
8271 nlbn = (lbn + 1) - (i * lbnadd);
8273 nfreework = newfreework(ump, freeblks, freework,
8274 nlbn, nb, fs->fs_frag, 0, 0);
8277 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8279 struct freedep *freedep;
8282 * Attempt to aggregate freedep dependencies for
8283 * all blocks being released to the same CG.
8287 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8288 freedep = newfreedep(freework);
8289 WORKLIST_INSERT_UNLOCKED(&wkhd,
8294 "indir_trunc: ino %jd blkno %jd size %d",
8295 freeblks->fb_inum, nb, fs->fs_bsize);
8296 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8297 fs->fs_bsize, freeblks->fb_inum,
8298 freeblks->fb_vtype, &wkhd, key);
8301 ffs_blkrelease_finish(ump, key);
8303 bp->b_flags |= B_INVAL | B_NOCACHE;
8308 freedblocks = (nblocks * cnt);
8310 freedblocks += nblocks;
8311 freeblks_free(ump, freeblks, freedblocks);
8313 * If we are journaling set up the ref counts and offset so this
8314 * indirect can be completed when its children are free.
8318 freework->fw_off = i;
8319 freework->fw_ref += freedeps;
8320 freework->fw_ref -= NINDIR(fs) + 1;
8322 freeblks->fb_cgwait += freedeps;
8323 if (freework->fw_ref == 0)
8324 freework_freeblock(freework, SINGLETON_KEY);
8329 * If we're not journaling we can free the indirect now.
8331 dbn = dbtofsb(fs, dbn);
8333 "indir_trunc 2: ino %jd blkno %jd size %d",
8334 freeblks->fb_inum, dbn, fs->fs_bsize);
8335 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8336 freeblks->fb_inum, freeblks->fb_vtype, NULL, SINGLETON_KEY);
8337 /* Non SUJ softdep does single-threaded truncations. */
8338 if (freework->fw_blkno == dbn) {
8339 freework->fw_state |= ALLCOMPLETE;
8341 handle_written_freework(freework);
8348 * Cancel an allocindir when it is removed via truncation. When bp is not
8349 * NULL the indirect never appeared on disk and is scheduled to be freed
8350 * independently of the indir so we can more easily track journal work.
8353 cancel_allocindir(aip, bp, freeblks, trunc)
8354 struct allocindir *aip;
8356 struct freeblks *freeblks;
8359 struct indirdep *indirdep;
8360 struct freefrag *freefrag;
8361 struct newblk *newblk;
8363 newblk = (struct newblk *)aip;
8364 LIST_REMOVE(aip, ai_next);
8366 * We must eliminate the pointer in bp if it must be freed on its
8367 * own due to partial truncate or pending journal work.
8369 if (bp && (trunc || newblk->nb_jnewblk)) {
8371 * Clear the pointer and mark the aip to be freed
8372 * directly if it never existed on disk.
8374 aip->ai_state |= DELAYEDFREE;
8375 indirdep = aip->ai_indirdep;
8376 if (indirdep->ir_state & UFS1FMT)
8377 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8379 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8382 * When truncating the previous pointer will be freed via
8383 * savedbp. Eliminate the freefrag which would dup free.
8385 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8386 newblk->nb_freefrag = NULL;
8387 if (freefrag->ff_jdep)
8389 WK_JFREEFRAG(freefrag->ff_jdep));
8390 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8391 WORKITEM_FREE(freefrag, D_FREEFRAG);
8394 * If the journal hasn't been written the jnewblk must be passed
8395 * to the call to ffs_blkfree that reclaims the space. We accomplish
8396 * this by leaving the journal dependency on the newblk to be freed
8397 * when a freework is created in handle_workitem_freeblocks().
8399 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8400 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8404 * Create the mkdir dependencies for . and .. in a new directory. Link them
8405 * in to a newdirblk so any subsequent additions are tracked properly. The
8406 * caller is responsible for adding the mkdir1 dependency to the journal
8407 * and updating id_mkdiradd. This function returns with the per-filesystem
8410 static struct mkdir *
8411 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8415 struct buf *newdirbp;
8416 struct mkdir **mkdirp;
8418 struct newblk *newblk;
8419 struct pagedep *pagedep;
8420 struct inodedep *inodedep;
8421 struct newdirblk *newdirblk;
8422 struct mkdir *mkdir1, *mkdir2;
8423 struct worklist *wk;
8424 struct jaddref *jaddref;
8425 struct ufsmount *ump;
8428 mp = dap->da_list.wk_mp;
8430 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8432 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8433 LIST_INIT(&newdirblk->db_mkdir);
8434 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8435 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8436 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8437 mkdir1->md_diradd = dap;
8438 mkdir1->md_jaddref = NULL;
8439 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8440 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8441 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8442 mkdir2->md_diradd = dap;
8443 mkdir2->md_jaddref = NULL;
8444 if (MOUNTEDSUJ(mp) == 0) {
8445 mkdir1->md_state |= DEPCOMPLETE;
8446 mkdir2->md_state |= DEPCOMPLETE;
8449 * Dependency on "." and ".." being written to disk.
8451 mkdir1->md_buf = newdirbp;
8452 ACQUIRE_LOCK(VFSTOUFS(mp));
8453 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8455 * We must link the pagedep, allocdirect, and newdirblk for
8456 * the initial file page so the pointer to the new directory
8457 * is not written until the directory contents are live and
8458 * any subsequent additions are not marked live until the
8459 * block is reachable via the inode.
8461 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8462 panic("setup_newdir: lost pagedep");
8463 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8464 if (wk->wk_type == D_ALLOCDIRECT)
8467 panic("setup_newdir: lost allocdirect");
8468 if (pagedep->pd_state & NEWBLOCK)
8469 panic("setup_newdir: NEWBLOCK already set");
8470 newblk = WK_NEWBLK(wk);
8471 pagedep->pd_state |= NEWBLOCK;
8472 pagedep->pd_newdirblk = newdirblk;
8473 newdirblk->db_pagedep = pagedep;
8474 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8475 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8477 * Look up the inodedep for the parent directory so that we
8478 * can link mkdir2 into the pending dotdot jaddref or
8479 * the inode write if there is none. If the inode is
8480 * ALLCOMPLETE and no jaddref is present all dependencies have
8481 * been satisfied and mkdir2 can be freed.
8483 inodedep_lookup(mp, dinum, 0, &inodedep);
8484 if (MOUNTEDSUJ(mp)) {
8485 if (inodedep == NULL)
8486 panic("setup_newdir: Lost parent.");
8487 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8489 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8490 (jaddref->ja_state & MKDIR_PARENT),
8491 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8492 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8493 mkdir2->md_jaddref = jaddref;
8494 jaddref->ja_mkdir = mkdir2;
8495 } else if (inodedep == NULL ||
8496 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8497 dap->da_state &= ~MKDIR_PARENT;
8498 WORKITEM_FREE(mkdir2, D_MKDIR);
8501 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8502 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8510 * Directory entry addition dependencies.
8512 * When adding a new directory entry, the inode (with its incremented link
8513 * count) must be written to disk before the directory entry's pointer to it.
8514 * Also, if the inode is newly allocated, the corresponding freemap must be
8515 * updated (on disk) before the directory entry's pointer. These requirements
8516 * are met via undo/redo on the directory entry's pointer, which consists
8517 * simply of the inode number.
8519 * As directory entries are added and deleted, the free space within a
8520 * directory block can become fragmented. The ufs filesystem will compact
8521 * a fragmented directory block to make space for a new entry. When this
8522 * occurs, the offsets of previously added entries change. Any "diradd"
8523 * dependency structures corresponding to these entries must be updated with
8528 * This routine is called after the in-memory inode's link
8529 * count has been incremented, but before the directory entry's
8530 * pointer to the inode has been set.
8533 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8534 struct buf *bp; /* buffer containing directory block */
8535 struct inode *dp; /* inode for directory */
8536 off_t diroffset; /* offset of new entry in directory */
8537 ino_t newinum; /* inode referenced by new directory entry */
8538 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
8539 int isnewblk; /* entry is in a newly allocated block */
8541 int offset; /* offset of new entry within directory block */
8542 ufs_lbn_t lbn; /* block in directory containing new entry */
8545 struct newblk *newblk;
8546 struct pagedep *pagedep;
8547 struct inodedep *inodedep;
8548 struct newdirblk *newdirblk;
8549 struct mkdir *mkdir1, *mkdir2;
8550 struct jaddref *jaddref;
8551 struct ufsmount *ump;
8557 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8558 ("softdep_setup_directory_add called on non-softdep filesystem"));
8560 * Whiteouts have no dependencies.
8562 if (newinum == UFS_WINO) {
8563 if (newdirbp != NULL)
8568 mkdir1 = mkdir2 = NULL;
8570 lbn = lblkno(fs, diroffset);
8571 offset = blkoff(fs, diroffset);
8572 dap = malloc(sizeof(struct diradd), M_DIRADD,
8573 M_SOFTDEP_FLAGS|M_ZERO);
8574 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8575 dap->da_offset = offset;
8576 dap->da_newinum = newinum;
8577 dap->da_state = ATTACHED;
8578 LIST_INIT(&dap->da_jwork);
8579 isindir = bp->b_lblkno >= UFS_NDADDR;
8582 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8583 newdirblk = malloc(sizeof(struct newdirblk),
8584 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8585 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8586 LIST_INIT(&newdirblk->db_mkdir);
8589 * If we're creating a new directory setup the dependencies and set
8590 * the dap state to wait for them. Otherwise it's COMPLETE and
8593 if (newdirbp == NULL) {
8594 dap->da_state |= DEPCOMPLETE;
8597 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8598 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8602 * Link into parent directory pagedep to await its being written.
8604 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8606 if (diradd_lookup(pagedep, offset) != NULL)
8607 panic("softdep_setup_directory_add: %p already at off %d\n",
8608 diradd_lookup(pagedep, offset), offset);
8610 dap->da_pagedep = pagedep;
8611 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8613 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8615 * If we're journaling, link the diradd into the jaddref so it
8616 * may be completed after the journal entry is written. Otherwise,
8617 * link the diradd into its inodedep. If the inode is not yet
8618 * written place it on the bufwait list, otherwise do the post-inode
8619 * write processing to put it on the id_pendinghd list.
8621 if (MOUNTEDSUJ(mp)) {
8622 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8624 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8625 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8626 jaddref->ja_diroff = diroffset;
8627 jaddref->ja_diradd = dap;
8628 add_to_journal(&jaddref->ja_list);
8629 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8630 diradd_inode_written(dap, inodedep);
8632 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8634 * Add the journal entries for . and .. links now that the primary
8637 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8638 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8639 inoreflst, if_deps);
8640 KASSERT(jaddref != NULL &&
8641 jaddref->ja_ino == jaddref->ja_parent &&
8642 (jaddref->ja_state & MKDIR_BODY),
8643 ("softdep_setup_directory_add: bad dot jaddref %p",
8645 mkdir1->md_jaddref = jaddref;
8646 jaddref->ja_mkdir = mkdir1;
8648 * It is important that the dotdot journal entry
8649 * is added prior to the dot entry since dot writes
8650 * both the dot and dotdot links. These both must
8651 * be added after the primary link for the journal
8652 * to remain consistent.
8654 add_to_journal(&mkdir2->md_jaddref->ja_list);
8655 add_to_journal(&jaddref->ja_list);
8658 * If we are adding a new directory remember this diradd so that if
8659 * we rename it we can keep the dot and dotdot dependencies. If
8660 * we are adding a new name for an inode that has a mkdiradd we
8661 * must be in rename and we have to move the dot and dotdot
8662 * dependencies to this new name. The old name is being orphaned
8665 if (mkdir1 != NULL) {
8666 if (inodedep->id_mkdiradd != NULL)
8667 panic("softdep_setup_directory_add: Existing mkdir");
8668 inodedep->id_mkdiradd = dap;
8669 } else if (inodedep->id_mkdiradd)
8670 merge_diradd(inodedep, dap);
8671 if (newdirblk != NULL) {
8673 * There is nothing to do if we are already tracking
8676 if ((pagedep->pd_state & NEWBLOCK) != 0) {
8677 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8681 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8683 panic("softdep_setup_directory_add: lost entry");
8684 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8685 pagedep->pd_state |= NEWBLOCK;
8686 pagedep->pd_newdirblk = newdirblk;
8687 newdirblk->db_pagedep = pagedep;
8690 * If we extended into an indirect signal direnter to sync.
8701 * This procedure is called to change the offset of a directory
8702 * entry when compacting a directory block which must be owned
8703 * exclusively by the caller. Note that the actual entry movement
8704 * must be done in this procedure to ensure that no I/O completions
8705 * occur while the move is in progress.
8708 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
8709 struct buf *bp; /* Buffer holding directory block. */
8710 struct inode *dp; /* inode for directory */
8711 caddr_t base; /* address of dp->i_offset */
8712 caddr_t oldloc; /* address of old directory location */
8713 caddr_t newloc; /* address of new directory location */
8714 int entrysize; /* size of directory entry */
8716 int offset, oldoffset, newoffset;
8717 struct pagedep *pagedep;
8718 struct jmvref *jmvref;
8722 struct ufsmount *ump;
8728 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8729 ("softdep_change_directoryentry_offset called on "
8730 "non-softdep filesystem"));
8731 de = (struct direct *)oldloc;
8735 * Moves are always journaled as it would be too complex to
8736 * determine if any affected adds or removes are present in the
8739 if (MOUNTEDSUJ(mp)) {
8741 jmvref = newjmvref(dp, de->d_ino,
8742 dp->i_offset + (oldloc - base),
8743 dp->i_offset + (newloc - base));
8745 lbn = lblkno(ump->um_fs, dp->i_offset);
8746 offset = blkoff(ump->um_fs, dp->i_offset);
8747 oldoffset = offset + (oldloc - base);
8748 newoffset = offset + (newloc - base);
8750 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8752 dap = diradd_lookup(pagedep, oldoffset);
8754 dap->da_offset = newoffset;
8755 newoffset = DIRADDHASH(newoffset);
8756 oldoffset = DIRADDHASH(oldoffset);
8757 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8758 newoffset != oldoffset) {
8759 LIST_REMOVE(dap, da_pdlist);
8760 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8766 jmvref->jm_pagedep = pagedep;
8767 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8768 add_to_journal(&jmvref->jm_list);
8770 bcopy(oldloc, newloc, entrysize);
8775 * Move the mkdir dependencies and journal work from one diradd to another
8776 * when renaming a directory. The new name must depend on the mkdir deps
8777 * completing as the old name did. Directories can only have one valid link
8778 * at a time so one must be canonical.
8781 merge_diradd(inodedep, newdap)
8782 struct inodedep *inodedep;
8783 struct diradd *newdap;
8785 struct diradd *olddap;
8786 struct mkdir *mkdir, *nextmd;
8787 struct ufsmount *ump;
8790 olddap = inodedep->id_mkdiradd;
8791 inodedep->id_mkdiradd = newdap;
8792 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8793 newdap->da_state &= ~DEPCOMPLETE;
8794 ump = VFSTOUFS(inodedep->id_list.wk_mp);
8795 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8797 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8798 if (mkdir->md_diradd != olddap)
8800 mkdir->md_diradd = newdap;
8801 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8802 newdap->da_state |= state;
8803 olddap->da_state &= ~state;
8804 if ((olddap->da_state &
8805 (MKDIR_PARENT | MKDIR_BODY)) == 0)
8808 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8809 panic("merge_diradd: unfound ref");
8812 * Any mkdir related journal items are not safe to be freed until
8813 * the new name is stable.
8815 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8816 olddap->da_state |= DEPCOMPLETE;
8817 complete_diradd(olddap);
8821 * Move the diradd to the pending list when all diradd dependencies are
8825 complete_diradd(dap)
8828 struct pagedep *pagedep;
8830 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
8831 if (dap->da_state & DIRCHG)
8832 pagedep = dap->da_previous->dm_pagedep;
8834 pagedep = dap->da_pagedep;
8835 LIST_REMOVE(dap, da_pdlist);
8836 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
8841 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
8842 * add entries and conditonally journal the remove.
8845 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
8847 struct dirrem *dirrem;
8848 struct jremref *jremref;
8849 struct jremref *dotremref;
8850 struct jremref *dotdotremref;
8852 struct inodedep *inodedep;
8853 struct jaddref *jaddref;
8854 struct inoref *inoref;
8855 struct ufsmount *ump;
8856 struct mkdir *mkdir;
8859 * If no remove references were allocated we're on a non-journaled
8860 * filesystem and can skip the cancel step.
8862 if (jremref == NULL) {
8863 free_diradd(dap, NULL);
8867 * Cancel the primary name an free it if it does not require
8870 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
8871 0, &inodedep) != 0) {
8872 /* Abort the addref that reference this diradd. */
8873 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
8874 if (inoref->if_list.wk_type != D_JADDREF)
8876 jaddref = (struct jaddref *)inoref;
8877 if (jaddref->ja_diradd != dap)
8879 if (cancel_jaddref(jaddref, inodedep,
8880 &dirrem->dm_jwork) == 0) {
8881 free_jremref(jremref);
8888 * Cancel subordinate names and free them if they do not require
8891 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8892 ump = VFSTOUFS(dap->da_list.wk_mp);
8893 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
8894 if (mkdir->md_diradd != dap)
8896 if ((jaddref = mkdir->md_jaddref) == NULL)
8898 mkdir->md_jaddref = NULL;
8899 if (mkdir->md_state & MKDIR_PARENT) {
8900 if (cancel_jaddref(jaddref, NULL,
8901 &dirrem->dm_jwork) == 0) {
8902 free_jremref(dotdotremref);
8903 dotdotremref = NULL;
8906 if (cancel_jaddref(jaddref, inodedep,
8907 &dirrem->dm_jwork) == 0) {
8908 free_jremref(dotremref);
8916 journal_jremref(dirrem, jremref, inodedep);
8918 journal_jremref(dirrem, dotremref, inodedep);
8920 journal_jremref(dirrem, dotdotremref, NULL);
8921 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
8922 free_diradd(dap, &dirrem->dm_jwork);
8926 * Free a diradd dependency structure.
8929 free_diradd(dap, wkhd)
8931 struct workhead *wkhd;
8933 struct dirrem *dirrem;
8934 struct pagedep *pagedep;
8935 struct inodedep *inodedep;
8936 struct mkdir *mkdir, *nextmd;
8937 struct ufsmount *ump;
8939 ump = VFSTOUFS(dap->da_list.wk_mp);
8941 LIST_REMOVE(dap, da_pdlist);
8942 if (dap->da_state & ONWORKLIST)
8943 WORKLIST_REMOVE(&dap->da_list);
8944 if ((dap->da_state & DIRCHG) == 0) {
8945 pagedep = dap->da_pagedep;
8947 dirrem = dap->da_previous;
8948 pagedep = dirrem->dm_pagedep;
8949 dirrem->dm_dirinum = pagedep->pd_ino;
8950 dirrem->dm_state |= COMPLETE;
8951 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
8952 add_to_worklist(&dirrem->dm_list, 0);
8954 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
8956 if (inodedep->id_mkdiradd == dap)
8957 inodedep->id_mkdiradd = NULL;
8958 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8959 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8961 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8962 if (mkdir->md_diradd != dap)
8965 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
8966 LIST_REMOVE(mkdir, md_mkdirs);
8967 if (mkdir->md_state & ONWORKLIST)
8968 WORKLIST_REMOVE(&mkdir->md_list);
8969 if (mkdir->md_jaddref != NULL)
8970 panic("free_diradd: Unexpected jaddref");
8971 WORKITEM_FREE(mkdir, D_MKDIR);
8972 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
8975 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8976 panic("free_diradd: unfound ref");
8979 free_inodedep(inodedep);
8981 * Free any journal segments waiting for the directory write.
8983 handle_jwork(&dap->da_jwork);
8984 WORKITEM_FREE(dap, D_DIRADD);
8988 * Directory entry removal dependencies.
8990 * When removing a directory entry, the entry's inode pointer must be
8991 * zero'ed on disk before the corresponding inode's link count is decremented
8992 * (possibly freeing the inode for re-use). This dependency is handled by
8993 * updating the directory entry but delaying the inode count reduction until
8994 * after the directory block has been written to disk. After this point, the
8995 * inode count can be decremented whenever it is convenient.
8999 * This routine should be called immediately after removing
9000 * a directory entry. The inode's link count should not be
9001 * decremented by the calling procedure -- the soft updates
9002 * code will do this task when it is safe.
9005 softdep_setup_remove(bp, dp, ip, isrmdir)
9006 struct buf *bp; /* buffer containing directory block */
9007 struct inode *dp; /* inode for the directory being modified */
9008 struct inode *ip; /* inode for directory entry being removed */
9009 int isrmdir; /* indicates if doing RMDIR */
9011 struct dirrem *dirrem, *prevdirrem;
9012 struct inodedep *inodedep;
9013 struct ufsmount *ump;
9017 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9018 ("softdep_setup_remove called on non-softdep filesystem"));
9020 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
9021 * newdirrem() to setup the full directory remove which requires
9024 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9026 * Add the dirrem to the inodedep's pending remove list for quick
9029 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0)
9030 panic("softdep_setup_remove: Lost inodedep.");
9031 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
9032 dirrem->dm_state |= ONDEPLIST;
9033 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9036 * If the COMPLETE flag is clear, then there were no active
9037 * entries and we want to roll back to a zeroed entry until
9038 * the new inode is committed to disk. If the COMPLETE flag is
9039 * set then we have deleted an entry that never made it to
9040 * disk. If the entry we deleted resulted from a name change,
9041 * then the old name still resides on disk. We cannot delete
9042 * its inode (returned to us in prevdirrem) until the zeroed
9043 * directory entry gets to disk. The new inode has never been
9044 * referenced on the disk, so can be deleted immediately.
9046 if ((dirrem->dm_state & COMPLETE) == 0) {
9047 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
9051 if (prevdirrem != NULL)
9052 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
9053 prevdirrem, dm_next);
9054 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
9055 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
9058 handle_workitem_remove(dirrem, 0);
9063 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
9064 * pd_pendinghd list of a pagedep.
9066 static struct diradd *
9067 diradd_lookup(pagedep, offset)
9068 struct pagedep *pagedep;
9073 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
9074 if (dap->da_offset == offset)
9076 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
9077 if (dap->da_offset == offset)
9083 * Search for a .. diradd dependency in a directory that is being removed.
9084 * If the directory was renamed to a new parent we have a diradd rather
9085 * than a mkdir for the .. entry. We need to cancel it now before
9086 * it is found in truncate().
9088 static struct jremref *
9089 cancel_diradd_dotdot(ip, dirrem, jremref)
9091 struct dirrem *dirrem;
9092 struct jremref *jremref;
9094 struct pagedep *pagedep;
9096 struct worklist *wk;
9098 if (pagedep_lookup(ITOVFS(ip), NULL, ip->i_number, 0, 0, &pagedep) == 0)
9100 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
9103 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
9105 * Mark any journal work as belonging to the parent so it is freed
9106 * with the .. reference.
9108 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9109 wk->wk_state |= MKDIR_PARENT;
9114 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
9115 * replace it with a dirrem/diradd pair as a result of re-parenting a
9116 * directory. This ensures that we don't simultaneously have a mkdir and
9117 * a diradd for the same .. entry.
9119 static struct jremref *
9120 cancel_mkdir_dotdot(ip, dirrem, jremref)
9122 struct dirrem *dirrem;
9123 struct jremref *jremref;
9125 struct inodedep *inodedep;
9126 struct jaddref *jaddref;
9127 struct ufsmount *ump;
9128 struct mkdir *mkdir;
9133 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9135 dap = inodedep->id_mkdiradd;
9136 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9138 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9139 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9140 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9141 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9144 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9145 if ((jaddref = mkdir->md_jaddref) != NULL) {
9146 mkdir->md_jaddref = NULL;
9147 jaddref->ja_state &= ~MKDIR_PARENT;
9148 if (inodedep_lookup(mp, jaddref->ja_ino, 0, &inodedep) == 0)
9149 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9150 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9151 journal_jremref(dirrem, jremref, inodedep);
9155 if (mkdir->md_state & ONWORKLIST)
9156 WORKLIST_REMOVE(&mkdir->md_list);
9157 mkdir->md_state |= ALLCOMPLETE;
9158 complete_mkdir(mkdir);
9163 journal_jremref(dirrem, jremref, inodedep)
9164 struct dirrem *dirrem;
9165 struct jremref *jremref;
9166 struct inodedep *inodedep;
9169 if (inodedep == NULL)
9170 if (inodedep_lookup(jremref->jr_list.wk_mp,
9171 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9172 panic("journal_jremref: Lost inodedep");
9173 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9174 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9175 add_to_journal(&jremref->jr_list);
9179 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9180 struct dirrem *dirrem;
9181 struct jremref *jremref;
9182 struct jremref *dotremref;
9183 struct jremref *dotdotremref;
9185 struct inodedep *inodedep;
9188 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9190 panic("dirrem_journal: Lost inodedep");
9191 journal_jremref(dirrem, jremref, inodedep);
9193 journal_jremref(dirrem, dotremref, inodedep);
9195 journal_jremref(dirrem, dotdotremref, NULL);
9199 * Allocate a new dirrem if appropriate and return it along with
9200 * its associated pagedep. Called without a lock, returns with lock.
9202 static struct dirrem *
9203 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9204 struct buf *bp; /* buffer containing directory block */
9205 struct inode *dp; /* inode for the directory being modified */
9206 struct inode *ip; /* inode for directory entry being removed */
9207 int isrmdir; /* indicates if doing RMDIR */
9208 struct dirrem **prevdirremp; /* previously referenced inode, if any */
9213 struct dirrem *dirrem;
9214 struct pagedep *pagedep;
9215 struct jremref *jremref;
9216 struct jremref *dotremref;
9217 struct jremref *dotdotremref;
9219 struct ufsmount *ump;
9222 * Whiteouts have no deletion dependencies.
9225 panic("newdirrem: whiteout");
9230 * If the system is over its limit and our filesystem is
9231 * responsible for more than our share of that usage and
9232 * we are not a snapshot, request some inodedep cleanup.
9233 * Limiting the number of dirrem structures will also limit
9234 * the number of freefile and freeblks structures.
9237 if (!IS_SNAPSHOT(ip) && softdep_excess_items(ump, D_DIRREM))
9238 schedule_cleanup(UFSTOVFS(ump));
9241 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9243 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9244 LIST_INIT(&dirrem->dm_jremrefhd);
9245 LIST_INIT(&dirrem->dm_jwork);
9246 dirrem->dm_state = isrmdir ? RMDIR : 0;
9247 dirrem->dm_oldinum = ip->i_number;
9248 *prevdirremp = NULL;
9250 * Allocate remove reference structures to track journal write
9251 * dependencies. We will always have one for the link and
9252 * when doing directories we will always have one more for dot.
9253 * When renaming a directory we skip the dotdot link change so
9254 * this is not needed.
9256 jremref = dotremref = dotdotremref = NULL;
9257 if (DOINGSUJ(dvp)) {
9259 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9260 ip->i_effnlink + 2);
9261 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9262 ip->i_effnlink + 1);
9263 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9264 dp->i_effnlink + 1);
9265 dotdotremref->jr_state |= MKDIR_PARENT;
9267 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9268 ip->i_effnlink + 1);
9271 lbn = lblkno(ump->um_fs, dp->i_offset);
9272 offset = blkoff(ump->um_fs, dp->i_offset);
9273 pagedep_lookup(UFSTOVFS(ump), bp, dp->i_number, lbn, DEPALLOC,
9275 dirrem->dm_pagedep = pagedep;
9276 dirrem->dm_offset = offset;
9278 * If we're renaming a .. link to a new directory, cancel any
9279 * existing MKDIR_PARENT mkdir. If it has already been canceled
9280 * the jremref is preserved for any potential diradd in this
9281 * location. This can not coincide with a rmdir.
9283 if (dp->i_offset == DOTDOT_OFFSET) {
9285 panic("newdirrem: .. directory change during remove?");
9286 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9289 * If we're removing a directory search for the .. dependency now and
9290 * cancel it. Any pending journal work will be added to the dirrem
9291 * to be completed when the workitem remove completes.
9294 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9296 * Check for a diradd dependency for the same directory entry.
9297 * If present, then both dependencies become obsolete and can
9300 dap = diradd_lookup(pagedep, offset);
9303 * Link the jremref structures into the dirrem so they are
9304 * written prior to the pagedep.
9307 dirrem_journal(dirrem, jremref, dotremref,
9312 * Must be ATTACHED at this point.
9314 if ((dap->da_state & ATTACHED) == 0)
9315 panic("newdirrem: not ATTACHED");
9316 if (dap->da_newinum != ip->i_number)
9317 panic("newdirrem: inum %ju should be %ju",
9318 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9320 * If we are deleting a changed name that never made it to disk,
9321 * then return the dirrem describing the previous inode (which
9322 * represents the inode currently referenced from this entry on disk).
9324 if ((dap->da_state & DIRCHG) != 0) {
9325 *prevdirremp = dap->da_previous;
9326 dap->da_state &= ~DIRCHG;
9327 dap->da_pagedep = pagedep;
9330 * We are deleting an entry that never made it to disk.
9331 * Mark it COMPLETE so we can delete its inode immediately.
9333 dirrem->dm_state |= COMPLETE;
9334 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9337 struct worklist *wk;
9339 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9340 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9341 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9349 * Directory entry change dependencies.
9351 * Changing an existing directory entry requires that an add operation
9352 * be completed first followed by a deletion. The semantics for the addition
9353 * are identical to the description of adding a new entry above except
9354 * that the rollback is to the old inode number rather than zero. Once
9355 * the addition dependency is completed, the removal is done as described
9356 * in the removal routine above.
9360 * This routine should be called immediately after changing
9361 * a directory entry. The inode's link count should not be
9362 * decremented by the calling procedure -- the soft updates
9363 * code will perform this task when it is safe.
9366 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9367 struct buf *bp; /* buffer containing directory block */
9368 struct inode *dp; /* inode for the directory being modified */
9369 struct inode *ip; /* inode for directory entry being removed */
9370 ino_t newinum; /* new inode number for changed entry */
9371 int isrmdir; /* indicates if doing RMDIR */
9374 struct diradd *dap = NULL;
9375 struct dirrem *dirrem, *prevdirrem;
9376 struct pagedep *pagedep;
9377 struct inodedep *inodedep;
9378 struct jaddref *jaddref;
9380 struct ufsmount *ump;
9384 offset = blkoff(ump->um_fs, dp->i_offset);
9385 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9386 ("softdep_setup_directory_change called on non-softdep filesystem"));
9389 * Whiteouts do not need diradd dependencies.
9391 if (newinum != UFS_WINO) {
9392 dap = malloc(sizeof(struct diradd),
9393 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9394 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9395 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9396 dap->da_offset = offset;
9397 dap->da_newinum = newinum;
9398 LIST_INIT(&dap->da_jwork);
9402 * Allocate a new dirrem and ACQUIRE_LOCK.
9404 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9405 pagedep = dirrem->dm_pagedep;
9407 * The possible values for isrmdir:
9408 * 0 - non-directory file rename
9409 * 1 - directory rename within same directory
9410 * inum - directory rename to new directory of given inode number
9411 * When renaming to a new directory, we are both deleting and
9412 * creating a new directory entry, so the link count on the new
9413 * directory should not change. Thus we do not need the followup
9414 * dirrem which is usually done in handle_workitem_remove. We set
9415 * the DIRCHG flag to tell handle_workitem_remove to skip the
9419 dirrem->dm_state |= DIRCHG;
9422 * Whiteouts have no additional dependencies,
9423 * so just put the dirrem on the correct list.
9425 if (newinum == UFS_WINO) {
9426 if ((dirrem->dm_state & COMPLETE) == 0) {
9427 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9430 dirrem->dm_dirinum = pagedep->pd_ino;
9431 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9432 add_to_worklist(&dirrem->dm_list, 0);
9438 * Add the dirrem to the inodedep's pending remove list for quick
9439 * discovery later. A valid nlinkdelta ensures that this lookup
9442 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9443 panic("softdep_setup_directory_change: Lost inodedep.");
9444 dirrem->dm_state |= ONDEPLIST;
9445 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9448 * If the COMPLETE flag is clear, then there were no active
9449 * entries and we want to roll back to the previous inode until
9450 * the new inode is committed to disk. If the COMPLETE flag is
9451 * set, then we have deleted an entry that never made it to disk.
9452 * If the entry we deleted resulted from a name change, then the old
9453 * inode reference still resides on disk. Any rollback that we do
9454 * needs to be to that old inode (returned to us in prevdirrem). If
9455 * the entry we deleted resulted from a create, then there is
9456 * no entry on the disk, so we want to roll back to zero rather
9457 * than the uncommitted inode. In either of the COMPLETE cases we
9458 * want to immediately free the unwritten and unreferenced inode.
9460 if ((dirrem->dm_state & COMPLETE) == 0) {
9461 dap->da_previous = dirrem;
9463 if (prevdirrem != NULL) {
9464 dap->da_previous = prevdirrem;
9466 dap->da_state &= ~DIRCHG;
9467 dap->da_pagedep = pagedep;
9469 dirrem->dm_dirinum = pagedep->pd_ino;
9470 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9471 add_to_worklist(&dirrem->dm_list, 0);
9474 * Lookup the jaddref for this journal entry. We must finish
9475 * initializing it and make the diradd write dependent on it.
9476 * If we're not journaling, put it on the id_bufwait list if the
9477 * inode is not yet written. If it is written, do the post-inode
9478 * write processing to put it on the id_pendinghd list.
9480 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9481 if (MOUNTEDSUJ(mp)) {
9482 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9484 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9485 ("softdep_setup_directory_change: bad jaddref %p",
9487 jaddref->ja_diroff = dp->i_offset;
9488 jaddref->ja_diradd = dap;
9489 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9491 add_to_journal(&jaddref->ja_list);
9492 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9493 dap->da_state |= COMPLETE;
9494 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9495 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9497 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9499 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9502 * If we're making a new name for a directory that has not been
9503 * committed when need to move the dot and dotdot references to
9506 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET)
9507 merge_diradd(inodedep, dap);
9512 * Called whenever the link count on an inode is changed.
9513 * It creates an inode dependency so that the new reference(s)
9514 * to the inode cannot be committed to disk until the updated
9515 * inode has been written.
9518 softdep_change_linkcnt(ip)
9519 struct inode *ip; /* the inode with the increased link count */
9521 struct inodedep *inodedep;
9522 struct ufsmount *ump;
9525 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9526 ("softdep_change_linkcnt called on non-softdep filesystem"));
9528 inodedep_lookup(UFSTOVFS(ump), ip->i_number, DEPALLOC, &inodedep);
9529 if (ip->i_nlink < ip->i_effnlink)
9530 panic("softdep_change_linkcnt: bad delta");
9531 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9536 * Attach a sbdep dependency to the superblock buf so that we can keep
9537 * track of the head of the linked list of referenced but unlinked inodes.
9540 softdep_setup_sbupdate(ump, fs, bp)
9541 struct ufsmount *ump;
9545 struct sbdep *sbdep;
9546 struct worklist *wk;
9548 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9549 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9550 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9551 if (wk->wk_type == D_SBDEP)
9555 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9556 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9558 sbdep->sb_ump = ump;
9560 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9565 * Return the first unlinked inodedep which is ready to be the head of the
9566 * list. The inodedep and all those after it must have valid next pointers.
9568 static struct inodedep *
9569 first_unlinked_inodedep(ump)
9570 struct ufsmount *ump;
9572 struct inodedep *inodedep;
9573 struct inodedep *idp;
9576 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9577 inodedep; inodedep = idp) {
9578 if ((inodedep->id_state & UNLINKNEXT) == 0)
9580 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9581 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9583 if ((inodedep->id_state & UNLINKPREV) == 0)
9590 * Set the sujfree unlinked head pointer prior to writing a superblock.
9593 initiate_write_sbdep(sbdep)
9594 struct sbdep *sbdep;
9596 struct inodedep *inodedep;
9600 bpfs = sbdep->sb_fs;
9601 fs = sbdep->sb_ump->um_fs;
9602 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9604 fs->fs_sujfree = inodedep->id_ino;
9605 inodedep->id_state |= UNLINKPREV;
9608 bpfs->fs_sujfree = fs->fs_sujfree;
9610 * Because we have made changes to the superblock, we need to
9611 * recompute its check-hash.
9613 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9617 * After a superblock is written determine whether it must be written again
9618 * due to a changing unlinked list head.
9621 handle_written_sbdep(sbdep, bp)
9622 struct sbdep *sbdep;
9625 struct inodedep *inodedep;
9628 LOCK_OWNED(sbdep->sb_ump);
9631 * If the superblock doesn't match the in-memory list start over.
9633 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9634 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9635 (inodedep == NULL && fs->fs_sujfree != 0)) {
9639 WORKITEM_FREE(sbdep, D_SBDEP);
9640 if (fs->fs_sujfree == 0)
9643 * Now that we have a record of this inode in stable store allow it
9644 * to be written to free up pending work. Inodes may see a lot of
9645 * write activity after they are unlinked which we must not hold up.
9647 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9648 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9649 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9650 inodedep, inodedep->id_state);
9651 if (inodedep->id_state & UNLINKONLIST)
9653 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9660 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9663 unlinked_inodedep(mp, inodedep)
9665 struct inodedep *inodedep;
9667 struct ufsmount *ump;
9671 if (MOUNTEDSUJ(mp) == 0)
9673 ump->um_fs->fs_fmod = 1;
9674 if (inodedep->id_state & UNLINKED)
9675 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9676 inodedep->id_state |= UNLINKED;
9677 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9681 * Remove an inodedep from the unlinked inodedep list. This may require
9682 * disk writes if the inode has made it that far.
9685 clear_unlinked_inodedep(inodedep)
9686 struct inodedep *inodedep;
9688 struct ufs2_dinode *dip;
9689 struct ufsmount *ump;
9690 struct inodedep *idp;
9691 struct inodedep *idn;
9692 struct fs *fs, *bpfs;
9699 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9701 ino = inodedep->id_ino;
9705 KASSERT((inodedep->id_state & UNLINKED) != 0,
9706 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9709 * If nothing has yet been written simply remove us from
9710 * the in memory list and return. This is the most common
9711 * case where handle_workitem_remove() loses the final
9714 if ((inodedep->id_state & UNLINKLINKS) == 0)
9717 * If we have a NEXT pointer and no PREV pointer we can simply
9718 * clear NEXT's PREV and remove ourselves from the list. Be
9719 * careful not to clear PREV if the superblock points at
9722 idn = TAILQ_NEXT(inodedep, id_unlinked);
9723 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9724 if (idn && fs->fs_sujfree != idn->id_ino)
9725 idn->id_state &= ~UNLINKPREV;
9729 * Here we have an inodedep which is actually linked into
9730 * the list. We must remove it by forcing a write to the
9731 * link before us, whether it be the superblock or an inode.
9732 * Unfortunately the list may change while we're waiting
9733 * on the buf lock for either resource so we must loop until
9734 * we lock the right one. If both the superblock and an
9735 * inode point to this inode we must clear the inode first
9736 * followed by the superblock.
9738 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9740 if (idp && (idp->id_state & UNLINKNEXT))
9744 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9745 (int)fs->fs_sbsize, 0, 0, 0);
9747 error = bread(ump->um_devvp,
9748 fsbtodb(fs, ino_to_fsba(fs, pino)),
9749 (int)fs->fs_bsize, NOCRED, &bp);
9756 /* If the list has changed restart the loop. */
9757 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9759 if (idp && (idp->id_state & UNLINKNEXT))
9762 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9769 idn = TAILQ_NEXT(inodedep, id_unlinked);
9773 * Remove us from the in memory list. After this we cannot
9774 * access the inodedep.
9776 KASSERT((inodedep->id_state & UNLINKED) != 0,
9777 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9779 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9780 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9783 * The predecessor's next pointer is manually updated here
9784 * so that the NEXT flag is never cleared for an element
9785 * that is in the list.
9788 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9789 bpfs = (struct fs *)bp->b_data;
9790 ffs_oldfscompat_write(bpfs, ump);
9791 softdep_setup_sbupdate(ump, bpfs, bp);
9793 * Because we may have made changes to the superblock,
9794 * we need to recompute its check-hash.
9796 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9797 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
9798 ((struct ufs1_dinode *)bp->b_data +
9799 ino_to_fsbo(fs, pino))->di_freelink = nino;
9801 dip = (struct ufs2_dinode *)bp->b_data +
9802 ino_to_fsbo(fs, pino);
9803 dip->di_freelink = nino;
9804 ffs_update_dinode_ckhash(fs, dip);
9807 * If the bwrite fails we have no recourse to recover. The
9808 * filesystem is corrupted already.
9813 * If the superblock pointer still needs to be cleared force
9816 if (fs->fs_sujfree == ino) {
9818 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9819 (int)fs->fs_sbsize, 0, 0, 0);
9820 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9821 bpfs = (struct fs *)bp->b_data;
9822 ffs_oldfscompat_write(bpfs, ump);
9823 softdep_setup_sbupdate(ump, bpfs, bp);
9825 * Because we may have made changes to the superblock,
9826 * we need to recompute its check-hash.
9828 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
9833 if (fs->fs_sujfree != ino)
9835 panic("clear_unlinked_inodedep: Failed to clear free head");
9837 if (inodedep->id_ino == fs->fs_sujfree)
9838 panic("clear_unlinked_inodedep: Freeing head of free list");
9839 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9840 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9845 * This workitem decrements the inode's link count.
9846 * If the link count reaches zero, the file is removed.
9849 handle_workitem_remove(dirrem, flags)
9850 struct dirrem *dirrem;
9853 struct inodedep *inodedep;
9854 struct workhead dotdotwk;
9855 struct worklist *wk;
9856 struct ufsmount *ump;
9862 if (dirrem->dm_state & ONWORKLIST)
9863 panic("handle_workitem_remove: dirrem %p still on worklist",
9865 oldinum = dirrem->dm_oldinum;
9866 mp = dirrem->dm_list.wk_mp;
9868 flags |= LK_EXCLUSIVE;
9869 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0)
9872 MPASS(ip->i_mode != 0);
9874 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
9875 panic("handle_workitem_remove: lost inodedep");
9876 if (dirrem->dm_state & ONDEPLIST)
9877 LIST_REMOVE(dirrem, dm_inonext);
9878 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
9879 ("handle_workitem_remove: Journal entries not written."));
9882 * Move all dependencies waiting on the remove to complete
9883 * from the dirrem to the inode inowait list to be completed
9884 * after the inode has been updated and written to disk.
9886 * Any marked MKDIR_PARENT are saved to be completed when the
9887 * dotdot ref is removed unless DIRCHG is specified. For
9888 * directory change operations there will be no further
9889 * directory writes and the jsegdeps need to be moved along
9890 * with the rest to be completed when the inode is free or
9891 * stable in the inode free list.
9893 LIST_INIT(&dotdotwk);
9894 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
9895 WORKLIST_REMOVE(wk);
9896 if ((dirrem->dm_state & DIRCHG) == 0 &&
9897 wk->wk_state & MKDIR_PARENT) {
9898 wk->wk_state &= ~MKDIR_PARENT;
9899 WORKLIST_INSERT(&dotdotwk, wk);
9902 WORKLIST_INSERT(&inodedep->id_inowait, wk);
9904 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
9906 * Normal file deletion.
9908 if ((dirrem->dm_state & RMDIR) == 0) {
9910 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: file ino "
9911 "%ju negative i_nlink %d", (intmax_t)ip->i_number,
9913 DIP_SET(ip, i_nlink, ip->i_nlink);
9914 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
9915 if (ip->i_nlink < ip->i_effnlink)
9916 panic("handle_workitem_remove: bad file delta");
9917 if (ip->i_nlink == 0)
9918 unlinked_inodedep(mp, inodedep);
9919 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9920 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9921 ("handle_workitem_remove: worklist not empty. %s",
9922 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
9923 WORKITEM_FREE(dirrem, D_DIRREM);
9928 * Directory deletion. Decrement reference count for both the
9929 * just deleted parent directory entry and the reference for ".".
9930 * Arrange to have the reference count on the parent decremented
9931 * to account for the loss of "..".
9934 KASSERT(ip->i_nlink >= 0, ("handle_workitem_remove: directory ino "
9935 "%ju negative i_nlink %d", (intmax_t)ip->i_number, ip->i_nlink));
9936 DIP_SET(ip, i_nlink, ip->i_nlink);
9937 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
9938 if (ip->i_nlink < ip->i_effnlink)
9939 panic("handle_workitem_remove: bad dir delta");
9940 if (ip->i_nlink == 0)
9941 unlinked_inodedep(mp, inodedep);
9942 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9944 * Rename a directory to a new parent. Since, we are both deleting
9945 * and creating a new directory entry, the link count on the new
9946 * directory should not change. Thus we skip the followup dirrem.
9948 if (dirrem->dm_state & DIRCHG) {
9949 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9950 ("handle_workitem_remove: DIRCHG and worklist not empty."));
9951 WORKITEM_FREE(dirrem, D_DIRREM);
9955 dirrem->dm_state = ONDEPLIST;
9956 dirrem->dm_oldinum = dirrem->dm_dirinum;
9958 * Place the dirrem on the parent's diremhd list.
9960 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
9961 panic("handle_workitem_remove: lost dir inodedep");
9962 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9964 * If the allocated inode has never been written to disk, then
9965 * the on-disk inode is zero'ed and we can remove the file
9966 * immediately. When journaling if the inode has been marked
9967 * unlinked and not DEPCOMPLETE we know it can never be written.
9969 inodedep_lookup(mp, oldinum, 0, &inodedep);
9970 if (inodedep == NULL ||
9971 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
9972 check_inode_unwritten(inodedep)) {
9975 return handle_workitem_remove(dirrem, flags);
9977 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
9979 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
9987 * Inode de-allocation dependencies.
9989 * When an inode's link count is reduced to zero, it can be de-allocated. We
9990 * found it convenient to postpone de-allocation until after the inode is
9991 * written to disk with its new link count (zero). At this point, all of the
9992 * on-disk inode's block pointers are nullified and, with careful dependency
9993 * list ordering, all dependencies related to the inode will be satisfied and
9994 * the corresponding dependency structures de-allocated. So, if/when the
9995 * inode is reused, there will be no mixing of old dependencies with new
9996 * ones. This artificial dependency is set up by the block de-allocation
9997 * procedure above (softdep_setup_freeblocks) and completed by the
9998 * following procedure.
10001 handle_workitem_freefile(freefile)
10002 struct freefile *freefile;
10004 struct workhead wkhd;
10006 struct ufsmount *ump;
10009 struct inodedep *idp;
10012 ump = VFSTOUFS(freefile->fx_list.wk_mp);
10016 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
10019 panic("handle_workitem_freefile: inodedep %p survived", idp);
10022 fs->fs_pendinginodes -= 1;
10025 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
10026 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
10027 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
10028 softdep_error("handle_workitem_freefile", error);
10030 WORKITEM_FREE(freefile, D_FREEFILE);
10036 * Helper function which unlinks marker element from work list and returns
10037 * the next element on the list.
10039 static __inline struct worklist *
10040 markernext(struct worklist *marker)
10042 struct worklist *next;
10044 next = LIST_NEXT(marker, wk_list);
10045 LIST_REMOVE(marker, wk_list);
10052 * The dependency structures constructed above are most actively used when file
10053 * system blocks are written to disk. No constraints are placed on when a
10054 * block can be written, but unsatisfied update dependencies are made safe by
10055 * modifying (or replacing) the source memory for the duration of the disk
10056 * write. When the disk write completes, the memory block is again brought
10059 * In-core inode structure reclamation.
10061 * Because there are a finite number of "in-core" inode structures, they are
10062 * reused regularly. By transferring all inode-related dependencies to the
10063 * in-memory inode block and indexing them separately (via "inodedep"s), we
10064 * can allow "in-core" inode structures to be reused at any time and avoid
10065 * any increase in contention.
10067 * Called just before entering the device driver to initiate a new disk I/O.
10068 * The buffer must be locked, thus, no I/O completion operations can occur
10069 * while we are manipulating its associated dependencies.
10072 softdep_disk_io_initiation(bp)
10073 struct buf *bp; /* structure describing disk write to occur */
10075 struct worklist *wk;
10076 struct worklist marker;
10077 struct inodedep *inodedep;
10078 struct freeblks *freeblks;
10079 struct jblkdep *jblkdep;
10080 struct newblk *newblk;
10081 struct ufsmount *ump;
10084 * We only care about write operations. There should never
10085 * be dependencies for reads.
10087 if (bp->b_iocmd != BIO_WRITE)
10088 panic("softdep_disk_io_initiation: not write");
10090 if (bp->b_vflags & BV_BKGRDINPROG)
10091 panic("softdep_disk_io_initiation: Writing buffer with "
10092 "background write in progress: %p", bp);
10094 ump = softdep_bp_to_mp(bp);
10098 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
10099 PHOLD(curproc); /* Don't swap out kernel stack */
10102 * Do any necessary pre-I/O processing.
10104 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
10105 wk = markernext(&marker)) {
10106 LIST_INSERT_AFTER(wk, &marker, wk_list);
10107 switch (wk->wk_type) {
10110 initiate_write_filepage(WK_PAGEDEP(wk), bp);
10114 inodedep = WK_INODEDEP(wk);
10115 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
10116 initiate_write_inodeblock_ufs1(inodedep, bp);
10118 initiate_write_inodeblock_ufs2(inodedep, bp);
10122 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
10126 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
10130 WK_JSEG(wk)->js_buf = NULL;
10134 freeblks = WK_FREEBLKS(wk);
10135 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
10137 * We have to wait for the freeblks to be journaled
10138 * before we can write an inodeblock with updated
10139 * pointers. Be careful to arrange the marker so
10140 * we revisit the freeblks if it's not removed by
10141 * the first jwait().
10143 if (jblkdep != NULL) {
10144 LIST_REMOVE(&marker, wk_list);
10145 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10146 jwait(&jblkdep->jb_list, MNT_WAIT);
10149 case D_ALLOCDIRECT:
10152 * We have to wait for the jnewblk to be journaled
10153 * before we can write to a block if the contents
10154 * may be confused with an earlier file's indirect
10155 * at recovery time. Handle the marker as described
10158 newblk = WK_NEWBLK(wk);
10159 if (newblk->nb_jnewblk != NULL &&
10160 indirblk_lookup(newblk->nb_list.wk_mp,
10161 newblk->nb_newblkno)) {
10162 LIST_REMOVE(&marker, wk_list);
10163 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10164 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10169 initiate_write_sbdep(WK_SBDEP(wk));
10179 panic("handle_disk_io_initiation: Unexpected type %s",
10180 TYPENAME(wk->wk_type));
10185 PRELE(curproc); /* Allow swapout of kernel stack */
10189 * Called from within the procedure above to deal with unsatisfied
10190 * allocation dependencies in a directory. The buffer must be locked,
10191 * thus, no I/O completion operations can occur while we are
10192 * manipulating its associated dependencies.
10195 initiate_write_filepage(pagedep, bp)
10196 struct pagedep *pagedep;
10199 struct jremref *jremref;
10200 struct jmvref *jmvref;
10201 struct dirrem *dirrem;
10202 struct diradd *dap;
10206 if (pagedep->pd_state & IOSTARTED) {
10208 * This can only happen if there is a driver that does not
10209 * understand chaining. Here biodone will reissue the call
10210 * to strategy for the incomplete buffers.
10212 printf("initiate_write_filepage: already started\n");
10215 pagedep->pd_state |= IOSTARTED;
10217 * Wait for all journal remove dependencies to hit the disk.
10218 * We can not allow any potentially conflicting directory adds
10219 * to be visible before removes and rollback is too difficult.
10220 * The per-filesystem lock may be dropped and re-acquired, however
10221 * we hold the buf locked so the dependency can not go away.
10223 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10224 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10225 jwait(&jremref->jr_list, MNT_WAIT);
10226 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10227 jwait(&jmvref->jm_list, MNT_WAIT);
10228 for (i = 0; i < DAHASHSZ; i++) {
10229 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10230 ep = (struct direct *)
10231 ((char *)bp->b_data + dap->da_offset);
10232 if (ep->d_ino != dap->da_newinum)
10233 panic("%s: dir inum %ju != new %ju",
10234 "initiate_write_filepage",
10235 (uintmax_t)ep->d_ino,
10236 (uintmax_t)dap->da_newinum);
10237 if (dap->da_state & DIRCHG)
10238 ep->d_ino = dap->da_previous->dm_oldinum;
10241 dap->da_state &= ~ATTACHED;
10242 dap->da_state |= UNDONE;
10248 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10249 * Note that any bug fixes made to this routine must be done in the
10250 * version found below.
10252 * Called from within the procedure above to deal with unsatisfied
10253 * allocation dependencies in an inodeblock. The buffer must be
10254 * locked, thus, no I/O completion operations can occur while we
10255 * are manipulating its associated dependencies.
10258 initiate_write_inodeblock_ufs1(inodedep, bp)
10259 struct inodedep *inodedep;
10260 struct buf *bp; /* The inode block */
10262 struct allocdirect *adp, *lastadp;
10263 struct ufs1_dinode *dp;
10264 struct ufs1_dinode *sip;
10265 struct inoref *inoref;
10266 struct ufsmount *ump;
10270 ufs_lbn_t prevlbn = 0;
10274 if (inodedep->id_state & IOSTARTED)
10275 panic("initiate_write_inodeblock_ufs1: already started");
10276 inodedep->id_state |= IOSTARTED;
10277 fs = inodedep->id_fs;
10278 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10280 dp = (struct ufs1_dinode *)bp->b_data +
10281 ino_to_fsbo(fs, inodedep->id_ino);
10284 * If we're on the unlinked list but have not yet written our
10285 * next pointer initialize it here.
10287 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10288 struct inodedep *inon;
10290 inon = TAILQ_NEXT(inodedep, id_unlinked);
10291 dp->di_freelink = inon ? inon->id_ino : 0;
10294 * If the bitmap is not yet written, then the allocated
10295 * inode cannot be written to disk.
10297 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10298 if (inodedep->id_savedino1 != NULL)
10299 panic("initiate_write_inodeblock_ufs1: I/O underway");
10301 sip = malloc(sizeof(struct ufs1_dinode),
10302 M_SAVEDINO, M_SOFTDEP_FLAGS);
10304 inodedep->id_savedino1 = sip;
10305 *inodedep->id_savedino1 = *dp;
10306 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10307 dp->di_gen = inodedep->id_savedino1->di_gen;
10308 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10312 * If no dependencies, then there is nothing to roll back.
10314 inodedep->id_savedsize = dp->di_size;
10315 inodedep->id_savedextsize = 0;
10316 inodedep->id_savednlink = dp->di_nlink;
10317 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10318 TAILQ_EMPTY(&inodedep->id_inoreflst))
10321 * Revert the link count to that of the first unwritten journal entry.
10323 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10325 dp->di_nlink = inoref->if_nlink;
10327 * Set the dependencies to busy.
10329 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10330 adp = TAILQ_NEXT(adp, ad_next)) {
10332 if (deplist != 0 && prevlbn >= adp->ad_offset)
10333 panic("softdep_write_inodeblock: lbn order");
10334 prevlbn = adp->ad_offset;
10335 if (adp->ad_offset < UFS_NDADDR &&
10336 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10337 panic("initiate_write_inodeblock_ufs1: "
10338 "direct pointer #%jd mismatch %d != %jd",
10339 (intmax_t)adp->ad_offset,
10340 dp->di_db[adp->ad_offset],
10341 (intmax_t)adp->ad_newblkno);
10342 if (adp->ad_offset >= UFS_NDADDR &&
10343 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10344 panic("initiate_write_inodeblock_ufs1: "
10345 "indirect pointer #%jd mismatch %d != %jd",
10346 (intmax_t)adp->ad_offset - UFS_NDADDR,
10347 dp->di_ib[adp->ad_offset - UFS_NDADDR],
10348 (intmax_t)adp->ad_newblkno);
10349 deplist |= 1 << adp->ad_offset;
10350 if ((adp->ad_state & ATTACHED) == 0)
10351 panic("initiate_write_inodeblock_ufs1: "
10352 "Unknown state 0x%x", adp->ad_state);
10353 #endif /* INVARIANTS */
10354 adp->ad_state &= ~ATTACHED;
10355 adp->ad_state |= UNDONE;
10358 * The on-disk inode cannot claim to be any larger than the last
10359 * fragment that has been written. Otherwise, the on-disk inode
10360 * might have fragments that were not the last block in the file
10361 * which would corrupt the filesystem.
10363 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10364 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10365 if (adp->ad_offset >= UFS_NDADDR)
10367 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10368 /* keep going until hitting a rollback to a frag */
10369 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10371 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10372 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10374 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10375 panic("initiate_write_inodeblock_ufs1: "
10377 #endif /* INVARIANTS */
10380 for (i = 0; i < UFS_NIADDR; i++) {
10382 if (dp->di_ib[i] != 0 &&
10383 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10384 panic("initiate_write_inodeblock_ufs1: "
10386 #endif /* INVARIANTS */
10392 * If we have zero'ed out the last allocated block of the file,
10393 * roll back the size to the last currently allocated block.
10394 * We know that this last allocated block is a full-sized as
10395 * we already checked for fragments in the loop above.
10397 if (lastadp != NULL &&
10398 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10399 for (i = lastadp->ad_offset; i >= 0; i--)
10400 if (dp->di_db[i] != 0)
10402 dp->di_size = (i + 1) * fs->fs_bsize;
10405 * The only dependencies are for indirect blocks.
10407 * The file size for indirect block additions is not guaranteed.
10408 * Such a guarantee would be non-trivial to achieve. The conventional
10409 * synchronous write implementation also does not make this guarantee.
10410 * Fsck should catch and fix discrepancies. Arguably, the file size
10411 * can be over-estimated without destroying integrity when the file
10412 * moves into the indirect blocks (i.e., is large). If we want to
10413 * postpone fsck, we are stuck with this argument.
10415 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10416 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10420 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10421 * Note that any bug fixes made to this routine must be done in the
10422 * version found above.
10424 * Called from within the procedure above to deal with unsatisfied
10425 * allocation dependencies in an inodeblock. The buffer must be
10426 * locked, thus, no I/O completion operations can occur while we
10427 * are manipulating its associated dependencies.
10430 initiate_write_inodeblock_ufs2(inodedep, bp)
10431 struct inodedep *inodedep;
10432 struct buf *bp; /* The inode block */
10434 struct allocdirect *adp, *lastadp;
10435 struct ufs2_dinode *dp;
10436 struct ufs2_dinode *sip;
10437 struct inoref *inoref;
10438 struct ufsmount *ump;
10442 ufs_lbn_t prevlbn = 0;
10446 if (inodedep->id_state & IOSTARTED)
10447 panic("initiate_write_inodeblock_ufs2: already started");
10448 inodedep->id_state |= IOSTARTED;
10449 fs = inodedep->id_fs;
10450 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10452 dp = (struct ufs2_dinode *)bp->b_data +
10453 ino_to_fsbo(fs, inodedep->id_ino);
10456 * If we're on the unlinked list but have not yet written our
10457 * next pointer initialize it here.
10459 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10460 struct inodedep *inon;
10462 inon = TAILQ_NEXT(inodedep, id_unlinked);
10463 dp->di_freelink = inon ? inon->id_ino : 0;
10464 ffs_update_dinode_ckhash(fs, dp);
10467 * If the bitmap is not yet written, then the allocated
10468 * inode cannot be written to disk.
10470 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10471 if (inodedep->id_savedino2 != NULL)
10472 panic("initiate_write_inodeblock_ufs2: I/O underway");
10474 sip = malloc(sizeof(struct ufs2_dinode),
10475 M_SAVEDINO, M_SOFTDEP_FLAGS);
10477 inodedep->id_savedino2 = sip;
10478 *inodedep->id_savedino2 = *dp;
10479 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10480 dp->di_gen = inodedep->id_savedino2->di_gen;
10481 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10485 * If no dependencies, then there is nothing to roll back.
10487 inodedep->id_savedsize = dp->di_size;
10488 inodedep->id_savedextsize = dp->di_extsize;
10489 inodedep->id_savednlink = dp->di_nlink;
10490 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10491 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10492 TAILQ_EMPTY(&inodedep->id_inoreflst))
10495 * Revert the link count to that of the first unwritten journal entry.
10497 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10499 dp->di_nlink = inoref->if_nlink;
10502 * Set the ext data dependencies to busy.
10504 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10505 adp = TAILQ_NEXT(adp, ad_next)) {
10507 if (deplist != 0 && prevlbn >= adp->ad_offset)
10508 panic("initiate_write_inodeblock_ufs2: lbn order");
10509 prevlbn = adp->ad_offset;
10510 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10511 panic("initiate_write_inodeblock_ufs2: "
10512 "ext pointer #%jd mismatch %jd != %jd",
10513 (intmax_t)adp->ad_offset,
10514 (intmax_t)dp->di_extb[adp->ad_offset],
10515 (intmax_t)adp->ad_newblkno);
10516 deplist |= 1 << adp->ad_offset;
10517 if ((adp->ad_state & ATTACHED) == 0)
10518 panic("initiate_write_inodeblock_ufs2: Unknown "
10519 "state 0x%x", adp->ad_state);
10520 #endif /* INVARIANTS */
10521 adp->ad_state &= ~ATTACHED;
10522 adp->ad_state |= UNDONE;
10525 * The on-disk inode cannot claim to be any larger than the last
10526 * fragment that has been written. Otherwise, the on-disk inode
10527 * might have fragments that were not the last block in the ext
10528 * data which would corrupt the filesystem.
10530 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10531 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10532 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10533 /* keep going until hitting a rollback to a frag */
10534 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10536 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10537 for (i = adp->ad_offset + 1; i < UFS_NXADDR; i++) {
10539 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10540 panic("initiate_write_inodeblock_ufs2: "
10542 #endif /* INVARIANTS */
10543 dp->di_extb[i] = 0;
10549 * If we have zero'ed out the last allocated block of the ext
10550 * data, roll back the size to the last currently allocated block.
10551 * We know that this last allocated block is a full-sized as
10552 * we already checked for fragments in the loop above.
10554 if (lastadp != NULL &&
10555 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10556 for (i = lastadp->ad_offset; i >= 0; i--)
10557 if (dp->di_extb[i] != 0)
10559 dp->di_extsize = (i + 1) * fs->fs_bsize;
10562 * Set the file data dependencies to busy.
10564 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10565 adp = TAILQ_NEXT(adp, ad_next)) {
10567 if (deplist != 0 && prevlbn >= adp->ad_offset)
10568 panic("softdep_write_inodeblock: lbn order");
10569 if ((adp->ad_state & ATTACHED) == 0)
10570 panic("inodedep %p and adp %p not attached", inodedep, adp);
10571 prevlbn = adp->ad_offset;
10572 if (adp->ad_offset < UFS_NDADDR &&
10573 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10574 panic("initiate_write_inodeblock_ufs2: "
10575 "direct pointer #%jd mismatch %jd != %jd",
10576 (intmax_t)adp->ad_offset,
10577 (intmax_t)dp->di_db[adp->ad_offset],
10578 (intmax_t)adp->ad_newblkno);
10579 if (adp->ad_offset >= UFS_NDADDR &&
10580 dp->di_ib[adp->ad_offset - UFS_NDADDR] != adp->ad_newblkno)
10581 panic("initiate_write_inodeblock_ufs2: "
10582 "indirect pointer #%jd mismatch %jd != %jd",
10583 (intmax_t)adp->ad_offset - UFS_NDADDR,
10584 (intmax_t)dp->di_ib[adp->ad_offset - UFS_NDADDR],
10585 (intmax_t)adp->ad_newblkno);
10586 deplist |= 1 << adp->ad_offset;
10587 if ((adp->ad_state & ATTACHED) == 0)
10588 panic("initiate_write_inodeblock_ufs2: Unknown "
10589 "state 0x%x", adp->ad_state);
10590 #endif /* INVARIANTS */
10591 adp->ad_state &= ~ATTACHED;
10592 adp->ad_state |= UNDONE;
10595 * The on-disk inode cannot claim to be any larger than the last
10596 * fragment that has been written. Otherwise, the on-disk inode
10597 * might have fragments that were not the last block in the file
10598 * which would corrupt the filesystem.
10600 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10601 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10602 if (adp->ad_offset >= UFS_NDADDR)
10604 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10605 /* keep going until hitting a rollback to a frag */
10606 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10608 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10609 for (i = adp->ad_offset + 1; i < UFS_NDADDR; i++) {
10611 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10612 panic("initiate_write_inodeblock_ufs2: "
10614 #endif /* INVARIANTS */
10617 for (i = 0; i < UFS_NIADDR; i++) {
10619 if (dp->di_ib[i] != 0 &&
10620 (deplist & ((1 << UFS_NDADDR) << i)) == 0)
10621 panic("initiate_write_inodeblock_ufs2: "
10623 #endif /* INVARIANTS */
10626 ffs_update_dinode_ckhash(fs, dp);
10630 * If we have zero'ed out the last allocated block of the file,
10631 * roll back the size to the last currently allocated block.
10632 * We know that this last allocated block is a full-sized as
10633 * we already checked for fragments in the loop above.
10635 if (lastadp != NULL &&
10636 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10637 for (i = lastadp->ad_offset; i >= 0; i--)
10638 if (dp->di_db[i] != 0)
10640 dp->di_size = (i + 1) * fs->fs_bsize;
10643 * The only dependencies are for indirect blocks.
10645 * The file size for indirect block additions is not guaranteed.
10646 * Such a guarantee would be non-trivial to achieve. The conventional
10647 * synchronous write implementation also does not make this guarantee.
10648 * Fsck should catch and fix discrepancies. Arguably, the file size
10649 * can be over-estimated without destroying integrity when the file
10650 * moves into the indirect blocks (i.e., is large). If we want to
10651 * postpone fsck, we are stuck with this argument.
10653 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10654 dp->di_ib[adp->ad_offset - UFS_NDADDR] = 0;
10655 ffs_update_dinode_ckhash(fs, dp);
10659 * Cancel an indirdep as a result of truncation. Release all of the
10660 * children allocindirs and place their journal work on the appropriate
10664 cancel_indirdep(indirdep, bp, freeblks)
10665 struct indirdep *indirdep;
10667 struct freeblks *freeblks;
10669 struct allocindir *aip;
10672 * None of the indirect pointers will ever be visible,
10673 * so they can simply be tossed. GOINGAWAY ensures
10674 * that allocated pointers will be saved in the buffer
10675 * cache until they are freed. Note that they will
10676 * only be able to be found by their physical address
10677 * since the inode mapping the logical address will
10678 * be gone. The save buffer used for the safe copy
10679 * was allocated in setup_allocindir_phase2 using
10680 * the physical address so it could be used for this
10681 * purpose. Hence we swap the safe copy with the real
10682 * copy, allowing the safe copy to be freed and holding
10683 * on to the real copy for later use in indir_trunc.
10685 if (indirdep->ir_state & GOINGAWAY)
10686 panic("cancel_indirdep: already gone");
10687 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10688 indirdep->ir_state |= DEPCOMPLETE;
10689 LIST_REMOVE(indirdep, ir_next);
10691 indirdep->ir_state |= GOINGAWAY;
10693 * Pass in bp for blocks still have journal writes
10694 * pending so we can cancel them on their own.
10696 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != NULL)
10697 cancel_allocindir(aip, bp, freeblks, 0);
10698 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL)
10699 cancel_allocindir(aip, NULL, freeblks, 0);
10700 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL)
10701 cancel_allocindir(aip, NULL, freeblks, 0);
10702 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL)
10703 cancel_allocindir(aip, NULL, freeblks, 0);
10705 * If there are pending partial truncations we need to keep the
10706 * old block copy around until they complete. This is because
10707 * the current b_data is not a perfect superset of the available
10710 if (TAILQ_EMPTY(&indirdep->ir_trunc))
10711 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10713 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10714 WORKLIST_REMOVE(&indirdep->ir_list);
10715 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10716 indirdep->ir_bp = NULL;
10717 indirdep->ir_freeblks = freeblks;
10721 * Free an indirdep once it no longer has new pointers to track.
10724 free_indirdep(indirdep)
10725 struct indirdep *indirdep;
10728 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10729 ("free_indirdep: Indir trunc list not empty."));
10730 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10731 ("free_indirdep: Complete head not empty."));
10732 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10733 ("free_indirdep: write head not empty."));
10734 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10735 ("free_indirdep: done head not empty."));
10736 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10737 ("free_indirdep: deplist head not empty."));
10738 KASSERT((indirdep->ir_state & DEPCOMPLETE),
10739 ("free_indirdep: %p still on newblk list.", indirdep));
10740 KASSERT(indirdep->ir_saveddata == NULL,
10741 ("free_indirdep: %p still has saved data.", indirdep));
10742 if (indirdep->ir_state & ONWORKLIST)
10743 WORKLIST_REMOVE(&indirdep->ir_list);
10744 WORKITEM_FREE(indirdep, D_INDIRDEP);
10748 * Called before a write to an indirdep. This routine is responsible for
10749 * rolling back pointers to a safe state which includes only those
10750 * allocindirs which have been completed.
10753 initiate_write_indirdep(indirdep, bp)
10754 struct indirdep *indirdep;
10757 struct ufsmount *ump;
10759 indirdep->ir_state |= IOSTARTED;
10760 if (indirdep->ir_state & GOINGAWAY)
10761 panic("disk_io_initiation: indirdep gone");
10763 * If there are no remaining dependencies, this will be writing
10764 * the real pointers.
10766 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10767 TAILQ_EMPTY(&indirdep->ir_trunc))
10770 * Replace up-to-date version with safe version.
10772 if (indirdep->ir_saveddata == NULL) {
10773 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10776 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10780 indirdep->ir_state &= ~ATTACHED;
10781 indirdep->ir_state |= UNDONE;
10782 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10783 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10788 * Called when an inode has been cleared in a cg bitmap. This finally
10789 * eliminates any canceled jaddrefs
10792 softdep_setup_inofree(mp, bp, ino, wkhd)
10796 struct workhead *wkhd;
10798 struct worklist *wk, *wkn;
10799 struct inodedep *inodedep;
10800 struct ufsmount *ump;
10805 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10806 ("softdep_setup_inofree called on non-softdep filesystem"));
10807 ump = VFSTOUFS(mp);
10810 cgp = (struct cg *)bp->b_data;
10811 inosused = cg_inosused(cgp);
10812 if (isset(inosused, ino % fs->fs_ipg))
10813 panic("softdep_setup_inofree: inode %ju not freed.",
10815 if (inodedep_lookup(mp, ino, 0, &inodedep))
10816 panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
10817 (uintmax_t)ino, inodedep);
10819 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10820 if (wk->wk_type != D_JADDREF)
10822 WORKLIST_REMOVE(wk);
10824 * We can free immediately even if the jaddref
10825 * isn't attached in a background write as now
10826 * the bitmaps are reconciled.
10828 wk->wk_state |= COMPLETE | ATTACHED;
10829 free_jaddref(WK_JADDREF(wk));
10831 jwork_move(&bp->b_dep, wkhd);
10837 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10838 * map. Any dependencies waiting for the write to clear are added to the
10839 * buf's list and any jnewblks that are being canceled are discarded
10843 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
10846 ufs2_daddr_t blkno;
10848 struct workhead *wkhd;
10850 struct bmsafemap *bmsafemap;
10851 struct jnewblk *jnewblk;
10852 struct ufsmount *ump;
10853 struct worklist *wk;
10858 ufs2_daddr_t jstart;
10866 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
10867 blkno, frags, wkhd);
10869 ump = VFSTOUFS(mp);
10870 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
10871 ("softdep_setup_blkfree called on non-softdep filesystem"));
10873 /* Lookup the bmsafemap so we track when it is dirty. */
10875 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10877 * Detach any jnewblks which have been canceled. They must linger
10878 * until the bitmap is cleared again by ffs_blkfree() to prevent
10879 * an unjournaled allocation from hitting the disk.
10882 while ((wk = LIST_FIRST(wkhd)) != NULL) {
10884 "softdep_setup_blkfree: blkno %jd wk type %d",
10885 blkno, wk->wk_type);
10886 WORKLIST_REMOVE(wk);
10887 if (wk->wk_type != D_JNEWBLK) {
10888 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
10891 jnewblk = WK_JNEWBLK(wk);
10892 KASSERT(jnewblk->jn_state & GOINGAWAY,
10893 ("softdep_setup_blkfree: jnewblk not canceled."));
10896 * Assert that this block is free in the bitmap
10897 * before we discard the jnewblk.
10899 cgp = (struct cg *)bp->b_data;
10900 blksfree = cg_blksfree(cgp);
10901 bno = dtogd(fs, jnewblk->jn_blkno);
10902 for (i = jnewblk->jn_oldfrags;
10903 i < jnewblk->jn_frags; i++) {
10904 if (isset(blksfree, bno + i))
10906 panic("softdep_setup_blkfree: not free");
10910 * Even if it's not attached we can free immediately
10911 * as the new bitmap is correct.
10913 wk->wk_state |= COMPLETE | ATTACHED;
10914 free_jnewblk(jnewblk);
10920 * Assert that we are not freeing a block which has an outstanding
10921 * allocation dependency.
10923 fs = VFSTOUFS(mp)->um_fs;
10924 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10925 end = blkno + frags;
10926 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
10928 * Don't match against blocks that will be freed when the
10929 * background write is done.
10931 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
10932 (COMPLETE | DEPCOMPLETE))
10934 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
10935 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
10936 if ((blkno >= jstart && blkno < jend) ||
10937 (end > jstart && end <= jend)) {
10938 printf("state 0x%X %jd - %d %d dep %p\n",
10939 jnewblk->jn_state, jnewblk->jn_blkno,
10940 jnewblk->jn_oldfrags, jnewblk->jn_frags,
10942 panic("softdep_setup_blkfree: "
10943 "%jd-%jd(%d) overlaps with %jd-%jd",
10944 blkno, end, frags, jstart, jend);
10952 * Revert a block allocation when the journal record that describes it
10953 * is not yet written.
10956 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
10957 struct jnewblk *jnewblk;
10962 ufs1_daddr_t fragno;
10968 cgbno = dtogd(fs, jnewblk->jn_blkno);
10970 * We have to test which frags need to be rolled back. We may
10971 * be operating on a stale copy when doing background writes.
10973 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
10974 if (isclr(blksfree, cgbno + i))
10979 * This is mostly ffs_blkfree() sans some validation and
10980 * superblock updates.
10982 if (frags == fs->fs_frag) {
10983 fragno = fragstoblks(fs, cgbno);
10984 ffs_setblock(fs, blksfree, fragno);
10985 ffs_clusteracct(fs, cgp, fragno, 1);
10986 cgp->cg_cs.cs_nbfree++;
10988 cgbno += jnewblk->jn_oldfrags;
10989 bbase = cgbno - fragnum(fs, cgbno);
10990 /* Decrement the old frags. */
10991 blk = blkmap(fs, blksfree, bbase);
10992 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
10993 /* Deallocate the fragment */
10994 for (i = 0; i < frags; i++)
10995 setbit(blksfree, cgbno + i);
10996 cgp->cg_cs.cs_nffree += frags;
10997 /* Add back in counts associated with the new frags */
10998 blk = blkmap(fs, blksfree, bbase);
10999 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11000 /* If a complete block has been reassembled, account for it. */
11001 fragno = fragstoblks(fs, bbase);
11002 if (ffs_isblock(fs, blksfree, fragno)) {
11003 cgp->cg_cs.cs_nffree -= fs->fs_frag;
11004 ffs_clusteracct(fs, cgp, fragno, 1);
11005 cgp->cg_cs.cs_nbfree++;
11009 jnewblk->jn_state &= ~ATTACHED;
11010 jnewblk->jn_state |= UNDONE;
11016 initiate_write_bmsafemap(bmsafemap, bp)
11017 struct bmsafemap *bmsafemap;
11018 struct buf *bp; /* The cg block. */
11020 struct jaddref *jaddref;
11021 struct jnewblk *jnewblk;
11029 * If this is a background write, we did this at the time that
11030 * the copy was made, so do not need to do it again.
11032 if (bmsafemap->sm_state & IOSTARTED)
11034 bmsafemap->sm_state |= IOSTARTED;
11036 * Clear any inode allocations which are pending journal writes.
11038 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
11039 cgp = (struct cg *)bp->b_data;
11040 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11041 inosused = cg_inosused(cgp);
11042 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
11043 ino = jaddref->ja_ino % fs->fs_ipg;
11044 if (isset(inosused, ino)) {
11045 if ((jaddref->ja_mode & IFMT) == IFDIR)
11046 cgp->cg_cs.cs_ndir--;
11047 cgp->cg_cs.cs_nifree++;
11048 clrbit(inosused, ino);
11049 jaddref->ja_state &= ~ATTACHED;
11050 jaddref->ja_state |= UNDONE;
11053 panic("initiate_write_bmsafemap: inode %ju "
11054 "marked free", (uintmax_t)jaddref->ja_ino);
11058 * Clear any block allocations which are pending journal writes.
11060 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11061 cgp = (struct cg *)bp->b_data;
11062 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11063 blksfree = cg_blksfree(cgp);
11064 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
11065 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
11067 panic("initiate_write_bmsafemap: block %jd "
11068 "marked free", jnewblk->jn_blkno);
11072 * Move allocation lists to the written lists so they can be
11073 * cleared once the block write is complete.
11075 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
11076 inodedep, id_deps);
11077 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
11079 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
11084 * This routine is called during the completion interrupt
11085 * service routine for a disk write (from the procedure called
11086 * by the device driver to inform the filesystem caches of
11087 * a request completion). It should be called early in this
11088 * procedure, before the block is made available to other
11089 * processes or other routines are called.
11093 softdep_disk_write_complete(bp)
11094 struct buf *bp; /* describes the completed disk write */
11096 struct worklist *wk;
11097 struct worklist *owk;
11098 struct ufsmount *ump;
11099 struct workhead reattach;
11100 struct freeblks *freeblks;
11103 ump = softdep_bp_to_mp(bp);
11104 KASSERT(LIST_EMPTY(&bp->b_dep) || ump != NULL,
11105 ("softdep_disk_write_complete: softdep_bp_to_mp returned NULL "
11106 "with outstanding dependencies for buffer %p", bp));
11110 * If an error occurred while doing the write, then the data
11111 * has not hit the disk and the dependencies cannot be processed.
11112 * But we do have to go through and roll forward any dependencies
11113 * that were rolled back before the disk write.
11117 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) {
11118 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
11119 switch (wk->wk_type) {
11122 handle_written_filepage(WK_PAGEDEP(wk), bp, 0);
11126 handle_written_inodeblock(WK_INODEDEP(wk),
11131 handle_written_bmsafemap(WK_BMSAFEMAP(wk),
11136 handle_written_indirdep(WK_INDIRDEP(wk),
11140 /* nothing to roll forward */
11149 LIST_INIT(&reattach);
11152 * Ump SU lock must not be released anywhere in this code segment.
11155 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
11156 WORKLIST_REMOVE(wk);
11157 atomic_add_long(&dep_write[wk->wk_type], 1);
11159 panic("duplicate worklist: %p\n", wk);
11161 switch (wk->wk_type) {
11164 if (handle_written_filepage(WK_PAGEDEP(wk), bp,
11166 WORKLIST_INSERT(&reattach, wk);
11170 if (handle_written_inodeblock(WK_INODEDEP(wk), bp,
11172 WORKLIST_INSERT(&reattach, wk);
11176 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp,
11178 WORKLIST_INSERT(&reattach, wk);
11182 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
11185 case D_ALLOCDIRECT:
11186 wk->wk_state |= COMPLETE;
11187 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
11191 wk->wk_state |= COMPLETE;
11192 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
11196 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp,
11198 WORKLIST_INSERT(&reattach, wk);
11202 wk->wk_state |= COMPLETE;
11203 freeblks = WK_FREEBLKS(wk);
11204 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
11205 LIST_EMPTY(&freeblks->fb_jblkdephd))
11206 add_to_worklist(wk, WK_NODELAY);
11210 handle_written_freework(WK_FREEWORK(wk));
11214 free_jsegdep(WK_JSEGDEP(wk));
11218 handle_written_jseg(WK_JSEG(wk), bp);
11222 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11223 WORKLIST_INSERT(&reattach, wk);
11227 free_freedep(WK_FREEDEP(wk));
11231 panic("handle_disk_write_complete: Unknown type %s",
11232 TYPENAME(wk->wk_type));
11237 * Reattach any requests that must be redone.
11239 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11240 WORKLIST_REMOVE(wk);
11241 WORKLIST_INSERT(&bp->b_dep, wk);
11249 * Called from within softdep_disk_write_complete above.
11252 handle_allocdirect_partdone(adp, wkhd)
11253 struct allocdirect *adp; /* the completed allocdirect */
11254 struct workhead *wkhd; /* Work to do when inode is writtne. */
11256 struct allocdirectlst *listhead;
11257 struct allocdirect *listadp;
11258 struct inodedep *inodedep;
11261 LOCK_OWNED(VFSTOUFS(adp->ad_block.nb_list.wk_mp));
11262 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11265 * The on-disk inode cannot claim to be any larger than the last
11266 * fragment that has been written. Otherwise, the on-disk inode
11267 * might have fragments that were not the last block in the file
11268 * which would corrupt the filesystem. Thus, we cannot free any
11269 * allocdirects after one whose ad_oldblkno claims a fragment as
11270 * these blocks must be rolled back to zero before writing the inode.
11271 * We check the currently active set of allocdirects in id_inoupdt
11272 * or id_extupdt as appropriate.
11274 inodedep = adp->ad_inodedep;
11275 bsize = inodedep->id_fs->fs_bsize;
11276 if (adp->ad_state & EXTDATA)
11277 listhead = &inodedep->id_extupdt;
11279 listhead = &inodedep->id_inoupdt;
11280 TAILQ_FOREACH(listadp, listhead, ad_next) {
11281 /* found our block */
11282 if (listadp == adp)
11284 /* continue if ad_oldlbn is not a fragment */
11285 if (listadp->ad_oldsize == 0 ||
11286 listadp->ad_oldsize == bsize)
11288 /* hit a fragment */
11292 * If we have reached the end of the current list without
11293 * finding the just finished dependency, then it must be
11294 * on the future dependency list. Future dependencies cannot
11295 * be freed until they are moved to the current list.
11297 if (listadp == NULL) {
11299 if (adp->ad_state & EXTDATA)
11300 listhead = &inodedep->id_newextupdt;
11302 listhead = &inodedep->id_newinoupdt;
11303 TAILQ_FOREACH(listadp, listhead, ad_next)
11304 /* found our block */
11305 if (listadp == adp)
11307 if (listadp == NULL)
11308 panic("handle_allocdirect_partdone: lost dep");
11309 #endif /* INVARIANTS */
11313 * If we have found the just finished dependency, then queue
11314 * it along with anything that follows it that is complete.
11315 * Since the pointer has not yet been written in the inode
11316 * as the dependency prevents it, place the allocdirect on the
11317 * bufwait list where it will be freed once the pointer is
11321 wkhd = &inodedep->id_bufwait;
11322 for (; adp; adp = listadp) {
11323 listadp = TAILQ_NEXT(adp, ad_next);
11324 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11326 TAILQ_REMOVE(listhead, adp, ad_next);
11327 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11332 * Called from within softdep_disk_write_complete above. This routine
11333 * completes successfully written allocindirs.
11336 handle_allocindir_partdone(aip)
11337 struct allocindir *aip; /* the completed allocindir */
11339 struct indirdep *indirdep;
11341 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11343 indirdep = aip->ai_indirdep;
11344 LIST_REMOVE(aip, ai_next);
11346 * Don't set a pointer while the buffer is undergoing IO or while
11347 * we have active truncations.
11349 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11350 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11353 if (indirdep->ir_state & UFS1FMT)
11354 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11357 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11360 * Await the pointer write before freeing the allocindir.
11362 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11366 * Release segments held on a jwork list.
11370 struct workhead *wkhd;
11372 struct worklist *wk;
11374 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11375 WORKLIST_REMOVE(wk);
11376 switch (wk->wk_type) {
11378 free_jsegdep(WK_JSEGDEP(wk));
11381 free_freedep(WK_FREEDEP(wk));
11384 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11385 WORKITEM_FREE(wk, D_FREEFRAG);
11388 handle_written_freework(WK_FREEWORK(wk));
11391 panic("handle_jwork: Unknown type %s\n",
11392 TYPENAME(wk->wk_type));
11398 * Handle the bufwait list on an inode when it is safe to release items
11399 * held there. This normally happens after an inode block is written but
11400 * may be delayed and handled later if there are pending journal items that
11401 * are not yet safe to be released.
11403 static struct freefile *
11404 handle_bufwait(inodedep, refhd)
11405 struct inodedep *inodedep;
11406 struct workhead *refhd;
11408 struct jaddref *jaddref;
11409 struct freefile *freefile;
11410 struct worklist *wk;
11413 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11414 WORKLIST_REMOVE(wk);
11415 switch (wk->wk_type) {
11418 * We defer adding freefile to the worklist
11419 * until all other additions have been made to
11420 * ensure that it will be done after all the
11421 * old blocks have been freed.
11423 if (freefile != NULL)
11424 panic("handle_bufwait: freefile");
11425 freefile = WK_FREEFILE(wk);
11429 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11433 diradd_inode_written(WK_DIRADD(wk), inodedep);
11437 wk->wk_state |= COMPLETE;
11438 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11439 add_to_worklist(wk, 0);
11443 wk->wk_state |= COMPLETE;
11444 add_to_worklist(wk, 0);
11447 case D_ALLOCDIRECT:
11449 free_newblk(WK_NEWBLK(wk));
11453 wk->wk_state |= COMPLETE;
11454 free_jnewblk(WK_JNEWBLK(wk));
11458 * Save freed journal segments and add references on
11459 * the supplied list which will delay their release
11460 * until the cg bitmap is cleared on disk.
11464 free_jsegdep(WK_JSEGDEP(wk));
11466 WORKLIST_INSERT(refhd, wk);
11470 jaddref = WK_JADDREF(wk);
11471 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11474 * Transfer any jaddrefs to the list to be freed with
11475 * the bitmap if we're handling a removed file.
11477 if (refhd == NULL) {
11478 wk->wk_state |= COMPLETE;
11479 free_jaddref(jaddref);
11481 WORKLIST_INSERT(refhd, wk);
11485 panic("handle_bufwait: Unknown type %p(%s)",
11486 wk, TYPENAME(wk->wk_type));
11493 * Called from within softdep_disk_write_complete above to restore
11494 * in-memory inode block contents to their most up-to-date state. Note
11495 * that this routine is always called from interrupt level with further
11496 * interrupts from this device blocked.
11498 * If the write did not succeed, we will do all the roll-forward
11499 * operations, but we will not take the actions that will allow its
11500 * dependencies to be processed.
11503 handle_written_inodeblock(inodedep, bp, flags)
11504 struct inodedep *inodedep;
11505 struct buf *bp; /* buffer containing the inode block */
11508 struct freefile *freefile;
11509 struct allocdirect *adp, *nextadp;
11510 struct ufs1_dinode *dp1 = NULL;
11511 struct ufs2_dinode *dp2 = NULL;
11512 struct workhead wkhd;
11513 int hadchanges, fstype;
11519 if ((inodedep->id_state & IOSTARTED) == 0)
11520 panic("handle_written_inodeblock: not started");
11521 inodedep->id_state &= ~IOSTARTED;
11522 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11524 dp1 = (struct ufs1_dinode *)bp->b_data +
11525 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11526 freelink = dp1->di_freelink;
11529 dp2 = (struct ufs2_dinode *)bp->b_data +
11530 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11531 freelink = dp2->di_freelink;
11534 * Leave this inodeblock dirty until it's in the list.
11536 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED &&
11537 (flags & WRITESUCCEEDED)) {
11538 struct inodedep *inon;
11540 inon = TAILQ_NEXT(inodedep, id_unlinked);
11541 if ((inon == NULL && freelink == 0) ||
11542 (inon && inon->id_ino == freelink)) {
11544 inon->id_state |= UNLINKPREV;
11545 inodedep->id_state |= UNLINKNEXT;
11550 * If we had to rollback the inode allocation because of
11551 * bitmaps being incomplete, then simply restore it.
11552 * Keep the block dirty so that it will not be reclaimed until
11553 * all associated dependencies have been cleared and the
11554 * corresponding updates written to disk.
11556 if (inodedep->id_savedino1 != NULL) {
11558 if (fstype == UFS1)
11559 *dp1 = *inodedep->id_savedino1;
11561 *dp2 = *inodedep->id_savedino2;
11562 free(inodedep->id_savedino1, M_SAVEDINO);
11563 inodedep->id_savedino1 = NULL;
11564 if ((bp->b_flags & B_DELWRI) == 0)
11565 stat_inode_bitmap++;
11568 * If the inode is clear here and GOINGAWAY it will never
11569 * be written. Process the bufwait and clear any pending
11570 * work which may include the freefile.
11572 if (inodedep->id_state & GOINGAWAY)
11576 if (flags & WRITESUCCEEDED)
11577 inodedep->id_state |= COMPLETE;
11579 * Roll forward anything that had to be rolled back before
11580 * the inode could be updated.
11582 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11583 nextadp = TAILQ_NEXT(adp, ad_next);
11584 if (adp->ad_state & ATTACHED)
11585 panic("handle_written_inodeblock: new entry");
11586 if (fstype == UFS1) {
11587 if (adp->ad_offset < UFS_NDADDR) {
11588 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11589 panic("%s %s #%jd mismatch %d != %jd",
11590 "handle_written_inodeblock:",
11592 (intmax_t)adp->ad_offset,
11593 dp1->di_db[adp->ad_offset],
11594 (intmax_t)adp->ad_oldblkno);
11595 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11597 if (dp1->di_ib[adp->ad_offset - UFS_NDADDR] !=
11599 panic("%s: %s #%jd allocated as %d",
11600 "handle_written_inodeblock",
11601 "indirect pointer",
11602 (intmax_t)adp->ad_offset -
11604 dp1->di_ib[adp->ad_offset -
11606 dp1->di_ib[adp->ad_offset - UFS_NDADDR] =
11610 if (adp->ad_offset < UFS_NDADDR) {
11611 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11612 panic("%s: %s #%jd %s %jd != %jd",
11613 "handle_written_inodeblock",
11615 (intmax_t)adp->ad_offset, "mismatch",
11616 (intmax_t)dp2->di_db[adp->ad_offset],
11617 (intmax_t)adp->ad_oldblkno);
11618 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11620 if (dp2->di_ib[adp->ad_offset - UFS_NDADDR] !=
11622 panic("%s: %s #%jd allocated as %jd",
11623 "handle_written_inodeblock",
11624 "indirect pointer",
11625 (intmax_t)adp->ad_offset -
11628 dp2->di_ib[adp->ad_offset -
11630 dp2->di_ib[adp->ad_offset - UFS_NDADDR] =
11634 adp->ad_state &= ~UNDONE;
11635 adp->ad_state |= ATTACHED;
11638 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11639 nextadp = TAILQ_NEXT(adp, ad_next);
11640 if (adp->ad_state & ATTACHED)
11641 panic("handle_written_inodeblock: new entry");
11642 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11643 panic("%s: direct pointers #%jd %s %jd != %jd",
11644 "handle_written_inodeblock",
11645 (intmax_t)adp->ad_offset, "mismatch",
11646 (intmax_t)dp2->di_extb[adp->ad_offset],
11647 (intmax_t)adp->ad_oldblkno);
11648 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11649 adp->ad_state &= ~UNDONE;
11650 adp->ad_state |= ATTACHED;
11653 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11654 stat_direct_blk_ptrs++;
11656 * Reset the file size to its most up-to-date value.
11658 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11659 panic("handle_written_inodeblock: bad size");
11660 if (inodedep->id_savednlink > UFS_LINK_MAX)
11661 panic("handle_written_inodeblock: Invalid link count "
11662 "%jd for inodedep %p", (uintmax_t)inodedep->id_savednlink,
11664 if (fstype == UFS1) {
11665 if (dp1->di_nlink != inodedep->id_savednlink) {
11666 dp1->di_nlink = inodedep->id_savednlink;
11669 if (dp1->di_size != inodedep->id_savedsize) {
11670 dp1->di_size = inodedep->id_savedsize;
11674 if (dp2->di_nlink != inodedep->id_savednlink) {
11675 dp2->di_nlink = inodedep->id_savednlink;
11678 if (dp2->di_size != inodedep->id_savedsize) {
11679 dp2->di_size = inodedep->id_savedsize;
11682 if (dp2->di_extsize != inodedep->id_savedextsize) {
11683 dp2->di_extsize = inodedep->id_savedextsize;
11687 inodedep->id_savedsize = -1;
11688 inodedep->id_savedextsize = -1;
11689 inodedep->id_savednlink = -1;
11691 * If there were any rollbacks in the inode block, then it must be
11692 * marked dirty so that its will eventually get written back in
11693 * its correct form.
11696 if (fstype == UFS2)
11697 ffs_update_dinode_ckhash(inodedep->id_fs, dp2);
11702 * If the write did not succeed, we have done all the roll-forward
11703 * operations, but we cannot take the actions that will allow its
11704 * dependencies to be processed.
11706 if ((flags & WRITESUCCEEDED) == 0)
11707 return (hadchanges);
11709 * Process any allocdirects that completed during the update.
11711 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11712 handle_allocdirect_partdone(adp, &wkhd);
11713 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11714 handle_allocdirect_partdone(adp, &wkhd);
11716 * Process deallocations that were held pending until the
11717 * inode had been written to disk. Freeing of the inode
11718 * is delayed until after all blocks have been freed to
11719 * avoid creation of new <vfsid, inum, lbn> triples
11720 * before the old ones have been deleted. Completely
11721 * unlinked inodes are not processed until the unlinked
11722 * inode list is written or the last reference is removed.
11724 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11725 freefile = handle_bufwait(inodedep, NULL);
11726 if (freefile && !LIST_EMPTY(&wkhd)) {
11727 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11732 * Move rolled forward dependency completions to the bufwait list
11733 * now that those that were already written have been processed.
11735 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11736 panic("handle_written_inodeblock: bufwait but no changes");
11737 jwork_move(&inodedep->id_bufwait, &wkhd);
11739 if (freefile != NULL) {
11741 * If the inode is goingaway it was never written. Fake up
11742 * the state here so free_inodedep() can succeed.
11744 if (inodedep->id_state & GOINGAWAY)
11745 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11746 if (free_inodedep(inodedep) == 0)
11747 panic("handle_written_inodeblock: live inodedep %p",
11749 add_to_worklist(&freefile->fx_list, 0);
11754 * If no outstanding dependencies, free it.
11756 if (free_inodedep(inodedep) ||
11757 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11758 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11759 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11760 LIST_FIRST(&inodedep->id_bufwait) == 0))
11762 return (hadchanges);
11766 * Perform needed roll-forwards and kick off any dependencies that
11767 * can now be processed.
11769 * If the write did not succeed, we will do all the roll-forward
11770 * operations, but we will not take the actions that will allow its
11771 * dependencies to be processed.
11774 handle_written_indirdep(indirdep, bp, bpp, flags)
11775 struct indirdep *indirdep;
11780 struct allocindir *aip;
11784 if (indirdep->ir_state & GOINGAWAY)
11785 panic("handle_written_indirdep: indirdep gone");
11786 if ((indirdep->ir_state & IOSTARTED) == 0)
11787 panic("handle_written_indirdep: IO not started");
11790 * If there were rollbacks revert them here.
11792 if (indirdep->ir_saveddata) {
11793 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11794 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11795 free(indirdep->ir_saveddata, M_INDIRDEP);
11796 indirdep->ir_saveddata = NULL;
11800 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11801 indirdep->ir_state |= ATTACHED;
11803 * If the write did not succeed, we have done all the roll-forward
11804 * operations, but we cannot take the actions that will allow its
11805 * dependencies to be processed.
11807 if ((flags & WRITESUCCEEDED) == 0) {
11808 stat_indir_blk_ptrs++;
11813 * Move allocindirs with written pointers to the completehd if
11814 * the indirdep's pointer is not yet written. Otherwise
11817 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != NULL) {
11818 LIST_REMOVE(aip, ai_next);
11819 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11820 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11822 newblk_freefrag(&aip->ai_block);
11825 free_newblk(&aip->ai_block);
11828 * Move allocindirs that have finished dependency processing from
11829 * the done list to the write list after updating the pointers.
11831 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11832 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != NULL) {
11833 handle_allocindir_partdone(aip);
11834 if (aip == LIST_FIRST(&indirdep->ir_donehd))
11835 panic("disk_write_complete: not gone");
11840 * Preserve the indirdep if there were any changes or if it is not
11841 * yet valid on disk.
11844 stat_indir_blk_ptrs++;
11849 * If there were no changes we can discard the savedbp and detach
11850 * ourselves from the buf. We are only carrying completed pointers
11853 sbp = indirdep->ir_savebp;
11854 sbp->b_flags |= B_INVAL | B_NOCACHE;
11855 indirdep->ir_savebp = NULL;
11856 indirdep->ir_bp = NULL;
11858 panic("handle_written_indirdep: bp already exists.");
11861 * The indirdep may not be freed until its parent points at it.
11863 if (indirdep->ir_state & DEPCOMPLETE)
11864 free_indirdep(indirdep);
11870 * Process a diradd entry after its dependent inode has been written.
11873 diradd_inode_written(dap, inodedep)
11874 struct diradd *dap;
11875 struct inodedep *inodedep;
11878 LOCK_OWNED(VFSTOUFS(dap->da_list.wk_mp));
11879 dap->da_state |= COMPLETE;
11880 complete_diradd(dap);
11881 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
11885 * Returns true if the bmsafemap will have rollbacks when written. Must only
11886 * be called with the per-filesystem lock and the buf lock on the cg held.
11889 bmsafemap_backgroundwrite(bmsafemap, bp)
11890 struct bmsafemap *bmsafemap;
11895 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
11896 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
11897 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
11899 * If we're initiating a background write we need to process the
11900 * rollbacks as they exist now, not as they exist when IO starts.
11901 * No other consumers will look at the contents of the shadowed
11902 * buf so this is safe to do here.
11904 if (bp->b_xflags & BX_BKGRDMARKER)
11905 initiate_write_bmsafemap(bmsafemap, bp);
11911 * Re-apply an allocation when a cg write is complete.
11914 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
11915 struct jnewblk *jnewblk;
11920 ufs1_daddr_t fragno;
11921 ufs2_daddr_t blkno;
11927 cgbno = dtogd(fs, jnewblk->jn_blkno);
11928 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
11929 if (isclr(blksfree, cgbno + i))
11930 panic("jnewblk_rollforward: re-allocated fragment");
11933 if (frags == fs->fs_frag) {
11934 blkno = fragstoblks(fs, cgbno);
11935 ffs_clrblock(fs, blksfree, (long)blkno);
11936 ffs_clusteracct(fs, cgp, blkno, -1);
11937 cgp->cg_cs.cs_nbfree--;
11939 bbase = cgbno - fragnum(fs, cgbno);
11940 cgbno += jnewblk->jn_oldfrags;
11941 /* If a complete block had been reassembled, account for it. */
11942 fragno = fragstoblks(fs, bbase);
11943 if (ffs_isblock(fs, blksfree, fragno)) {
11944 cgp->cg_cs.cs_nffree += fs->fs_frag;
11945 ffs_clusteracct(fs, cgp, fragno, -1);
11946 cgp->cg_cs.cs_nbfree--;
11948 /* Decrement the old frags. */
11949 blk = blkmap(fs, blksfree, bbase);
11950 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11951 /* Allocate the fragment */
11952 for (i = 0; i < frags; i++)
11953 clrbit(blksfree, cgbno + i);
11954 cgp->cg_cs.cs_nffree -= frags;
11955 /* Add back in counts associated with the new frags */
11956 blk = blkmap(fs, blksfree, bbase);
11957 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11963 * Complete a write to a bmsafemap structure. Roll forward any bitmap
11964 * changes if it's not a background write. Set all written dependencies
11965 * to DEPCOMPLETE and free the structure if possible.
11967 * If the write did not succeed, we will do all the roll-forward
11968 * operations, but we will not take the actions that will allow its
11969 * dependencies to be processed.
11972 handle_written_bmsafemap(bmsafemap, bp, flags)
11973 struct bmsafemap *bmsafemap;
11977 struct newblk *newblk;
11978 struct inodedep *inodedep;
11979 struct jaddref *jaddref, *jatmp;
11980 struct jnewblk *jnewblk, *jntmp;
11981 struct ufsmount *ump;
11990 if ((bmsafemap->sm_state & IOSTARTED) == 0)
11991 panic("handle_written_bmsafemap: Not started\n");
11992 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
11994 bmsafemap->sm_state &= ~IOSTARTED;
11995 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
11997 * If write was successful, release journal work that was waiting
11998 * on the write. Otherwise move the work back.
12000 if (flags & WRITESUCCEEDED)
12001 handle_jwork(&bmsafemap->sm_freewr);
12003 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12004 worklist, wk_list);
12007 * Restore unwritten inode allocation pending jaddref writes.
12009 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
12010 cgp = (struct cg *)bp->b_data;
12011 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12012 inosused = cg_inosused(cgp);
12013 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
12014 ja_bmdeps, jatmp) {
12015 if ((jaddref->ja_state & UNDONE) == 0)
12017 ino = jaddref->ja_ino % fs->fs_ipg;
12018 if (isset(inosused, ino))
12019 panic("handle_written_bmsafemap: "
12020 "re-allocated inode");
12021 /* Do the roll-forward only if it's a real copy. */
12023 if ((jaddref->ja_mode & IFMT) == IFDIR)
12024 cgp->cg_cs.cs_ndir++;
12025 cgp->cg_cs.cs_nifree--;
12026 setbit(inosused, ino);
12029 jaddref->ja_state &= ~UNDONE;
12030 jaddref->ja_state |= ATTACHED;
12031 free_jaddref(jaddref);
12035 * Restore any block allocations which are pending journal writes.
12037 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
12038 cgp = (struct cg *)bp->b_data;
12039 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
12040 blksfree = cg_blksfree(cgp);
12041 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
12043 if ((jnewblk->jn_state & UNDONE) == 0)
12045 /* Do the roll-forward only if it's a real copy. */
12047 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
12049 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
12050 jnewblk->jn_state |= ATTACHED;
12051 free_jnewblk(jnewblk);
12055 * If the write did not succeed, we have done all the roll-forward
12056 * operations, but we cannot take the actions that will allow its
12057 * dependencies to be processed.
12059 if ((flags & WRITESUCCEEDED) == 0) {
12060 LIST_CONCAT(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
12062 LIST_CONCAT(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr,
12063 worklist, wk_list);
12068 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
12069 newblk->nb_state |= DEPCOMPLETE;
12070 newblk->nb_state &= ~ONDEPLIST;
12071 newblk->nb_bmsafemap = NULL;
12072 LIST_REMOVE(newblk, nb_deps);
12073 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
12074 handle_allocdirect_partdone(
12075 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
12076 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
12077 handle_allocindir_partdone(
12078 WK_ALLOCINDIR(&newblk->nb_list));
12079 else if (newblk->nb_list.wk_type != D_NEWBLK)
12080 panic("handle_written_bmsafemap: Unexpected type: %s",
12081 TYPENAME(newblk->nb_list.wk_type));
12083 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
12084 inodedep->id_state |= DEPCOMPLETE;
12085 inodedep->id_state &= ~ONDEPLIST;
12086 LIST_REMOVE(inodedep, id_deps);
12087 inodedep->id_bmsafemap = NULL;
12089 LIST_REMOVE(bmsafemap, sm_next);
12090 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
12091 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
12092 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
12093 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
12094 LIST_EMPTY(&bmsafemap->sm_freehd)) {
12095 LIST_REMOVE(bmsafemap, sm_hash);
12096 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
12099 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
12106 * Try to free a mkdir dependency.
12109 complete_mkdir(mkdir)
12110 struct mkdir *mkdir;
12112 struct diradd *dap;
12114 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
12116 LIST_REMOVE(mkdir, md_mkdirs);
12117 dap = mkdir->md_diradd;
12118 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
12119 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
12120 dap->da_state |= DEPCOMPLETE;
12121 complete_diradd(dap);
12123 WORKITEM_FREE(mkdir, D_MKDIR);
12127 * Handle the completion of a mkdir dependency.
12130 handle_written_mkdir(mkdir, type)
12131 struct mkdir *mkdir;
12135 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
12136 panic("handle_written_mkdir: bad type");
12137 mkdir->md_state |= COMPLETE;
12138 complete_mkdir(mkdir);
12142 free_pagedep(pagedep)
12143 struct pagedep *pagedep;
12147 if (pagedep->pd_state & NEWBLOCK)
12149 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
12151 for (i = 0; i < DAHASHSZ; i++)
12152 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
12154 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
12156 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
12158 if (pagedep->pd_state & ONWORKLIST)
12159 WORKLIST_REMOVE(&pagedep->pd_list);
12160 LIST_REMOVE(pagedep, pd_hash);
12161 WORKITEM_FREE(pagedep, D_PAGEDEP);
12167 * Called from within softdep_disk_write_complete above.
12168 * A write operation was just completed. Removed inodes can
12169 * now be freed and associated block pointers may be committed.
12170 * Note that this routine is always called from interrupt level
12171 * with further interrupts from this device blocked.
12173 * If the write did not succeed, we will do all the roll-forward
12174 * operations, but we will not take the actions that will allow its
12175 * dependencies to be processed.
12178 handle_written_filepage(pagedep, bp, flags)
12179 struct pagedep *pagedep;
12180 struct buf *bp; /* buffer containing the written page */
12183 struct dirrem *dirrem;
12184 struct diradd *dap, *nextdap;
12188 if ((pagedep->pd_state & IOSTARTED) == 0)
12189 panic("handle_written_filepage: not started");
12190 pagedep->pd_state &= ~IOSTARTED;
12191 if ((flags & WRITESUCCEEDED) == 0)
12194 * Process any directory removals that have been committed.
12196 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
12197 LIST_REMOVE(dirrem, dm_next);
12198 dirrem->dm_state |= COMPLETE;
12199 dirrem->dm_dirinum = pagedep->pd_ino;
12200 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
12201 ("handle_written_filepage: Journal entries not written."));
12202 add_to_worklist(&dirrem->dm_list, 0);
12205 * Free any directory additions that have been committed.
12206 * If it is a newly allocated block, we have to wait until
12207 * the on-disk directory inode claims the new block.
12209 if ((pagedep->pd_state & NEWBLOCK) == 0)
12210 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
12211 free_diradd(dap, NULL);
12214 * Uncommitted directory entries must be restored.
12216 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
12217 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
12219 nextdap = LIST_NEXT(dap, da_pdlist);
12220 if (dap->da_state & ATTACHED)
12221 panic("handle_written_filepage: attached");
12222 ep = (struct direct *)
12223 ((char *)bp->b_data + dap->da_offset);
12224 ep->d_ino = dap->da_newinum;
12225 dap->da_state &= ~UNDONE;
12226 dap->da_state |= ATTACHED;
12229 * If the inode referenced by the directory has
12230 * been written out, then the dependency can be
12231 * moved to the pending list.
12233 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
12234 LIST_REMOVE(dap, da_pdlist);
12235 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
12241 * If there were any rollbacks in the directory, then it must be
12242 * marked dirty so that its will eventually get written back in
12243 * its correct form.
12245 if (chgs || (flags & WRITESUCCEEDED) == 0) {
12246 if ((bp->b_flags & B_DELWRI) == 0)
12252 * If we are not waiting for a new directory block to be
12253 * claimed by its inode, then the pagedep will be freed.
12254 * Otherwise it will remain to track any new entries on
12255 * the page in case they are fsync'ed.
12257 free_pagedep(pagedep);
12262 * Writing back in-core inode structures.
12264 * The filesystem only accesses an inode's contents when it occupies an
12265 * "in-core" inode structure. These "in-core" structures are separate from
12266 * the page frames used to cache inode blocks. Only the latter are
12267 * transferred to/from the disk. So, when the updated contents of the
12268 * "in-core" inode structure are copied to the corresponding in-memory inode
12269 * block, the dependencies are also transferred. The following procedure is
12270 * called when copying a dirty "in-core" inode to a cached inode block.
12274 * Called when an inode is loaded from disk. If the effective link count
12275 * differed from the actual link count when it was last flushed, then we
12276 * need to ensure that the correct effective link count is put back.
12279 softdep_load_inodeblock(ip)
12280 struct inode *ip; /* the "in_core" copy of the inode */
12282 struct inodedep *inodedep;
12283 struct ufsmount *ump;
12286 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
12287 ("softdep_load_inodeblock called on non-softdep filesystem"));
12289 * Check for alternate nlink count.
12291 ip->i_effnlink = ip->i_nlink;
12293 if (inodedep_lookup(UFSTOVFS(ump), ip->i_number, 0, &inodedep) == 0) {
12297 ip->i_effnlink -= inodedep->id_nlinkdelta;
12298 KASSERT(ip->i_effnlink >= 0,
12299 ("softdep_load_inodeblock: negative i_effnlink"));
12304 * This routine is called just before the "in-core" inode
12305 * information is to be copied to the in-memory inode block.
12306 * Recall that an inode block contains several inodes. If
12307 * the force flag is set, then the dependencies will be
12308 * cleared so that the update can always be made. Note that
12309 * the buffer is locked when this routine is called, so we
12310 * will never be in the middle of writing the inode block
12314 softdep_update_inodeblock(ip, bp, waitfor)
12315 struct inode *ip; /* the "in_core" copy of the inode */
12316 struct buf *bp; /* the buffer containing the inode block */
12317 int waitfor; /* nonzero => update must be allowed */
12319 struct inodedep *inodedep;
12320 struct inoref *inoref;
12321 struct ufsmount *ump;
12322 struct worklist *wk;
12329 mp = UFSTOVFS(ump);
12330 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12331 ("softdep_update_inodeblock called on non-softdep filesystem"));
12334 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12335 * does not have access to the in-core ip so must write directly into
12336 * the inode block buffer when setting freelink.
12338 if (fs->fs_magic == FS_UFS1_MAGIC)
12339 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12340 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12342 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12343 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12345 * If the effective link count is not equal to the actual link
12346 * count, then we must track the difference in an inodedep while
12347 * the inode is (potentially) tossed out of the cache. Otherwise,
12348 * if there is no existing inodedep, then there are no dependencies
12353 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12355 if (ip->i_effnlink != ip->i_nlink)
12356 panic("softdep_update_inodeblock: bad link count");
12359 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12360 panic("softdep_update_inodeblock: bad delta");
12362 * If we're flushing all dependencies we must also move any waiting
12363 * for journal writes onto the bufwait list prior to I/O.
12366 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12367 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12369 jwait(&inoref->if_list, MNT_WAIT);
12375 * Changes have been initiated. Anything depending on these
12376 * changes cannot occur until this inode has been written.
12378 inodedep->id_state &= ~COMPLETE;
12379 if ((inodedep->id_state & ONWORKLIST) == 0)
12380 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12382 * Any new dependencies associated with the incore inode must
12383 * now be moved to the list associated with the buffer holding
12384 * the in-memory copy of the inode. Once merged process any
12385 * allocdirects that are completed by the merger.
12387 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12388 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12389 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12391 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12392 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12393 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12396 * Now that the inode has been pushed into the buffer, the
12397 * operations dependent on the inode being written to disk
12398 * can be moved to the id_bufwait so that they will be
12399 * processed when the buffer I/O completes.
12401 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12402 WORKLIST_REMOVE(wk);
12403 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12406 * Newly allocated inodes cannot be written until the bitmap
12407 * that allocates them have been written (indicated by
12408 * DEPCOMPLETE being set in id_state). If we are doing a
12409 * forced sync (e.g., an fsync on a file), we force the bitmap
12410 * to be written so that the update can be done.
12412 if (waitfor == 0) {
12417 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12421 ibp = inodedep->id_bmsafemap->sm_buf;
12422 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12425 * If ibp came back as NULL, the dependency could have been
12426 * freed while we slept. Look it up again, and check to see
12427 * that it has completed.
12429 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12435 if ((error = bwrite(ibp)) != 0)
12436 softdep_error("softdep_update_inodeblock: bwrite", error);
12440 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12441 * old inode dependency list (such as id_inoupdt).
12444 merge_inode_lists(newlisthead, oldlisthead)
12445 struct allocdirectlst *newlisthead;
12446 struct allocdirectlst *oldlisthead;
12448 struct allocdirect *listadp, *newadp;
12450 newadp = TAILQ_FIRST(newlisthead);
12451 if (newadp != NULL)
12452 LOCK_OWNED(VFSTOUFS(newadp->ad_block.nb_list.wk_mp));
12453 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12454 if (listadp->ad_offset < newadp->ad_offset) {
12455 listadp = TAILQ_NEXT(listadp, ad_next);
12458 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12459 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12460 if (listadp->ad_offset == newadp->ad_offset) {
12461 allocdirect_merge(oldlisthead, newadp,
12465 newadp = TAILQ_FIRST(newlisthead);
12467 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12468 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12469 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12474 * If we are doing an fsync, then we must ensure that any directory
12475 * entries for the inode have been written after the inode gets to disk.
12479 struct vnode *vp; /* the "in_core" copy of the inode */
12481 struct inodedep *inodedep;
12482 struct pagedep *pagedep;
12483 struct inoref *inoref;
12484 struct ufsmount *ump;
12485 struct worklist *wk;
12486 struct diradd *dap;
12492 struct thread *td = curthread;
12493 int error, flushparent, pagedep_new_block;
12499 ump = VFSTOUFS(mp);
12501 if (MOUNTEDSOFTDEP(mp) == 0)
12505 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12509 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12510 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12512 jwait(&inoref->if_list, MNT_WAIT);
12516 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12517 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12518 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12519 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12520 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12521 panic("softdep_fsync: pending ops %p", inodedep);
12522 for (error = 0, flushparent = 0; ; ) {
12523 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12525 if (wk->wk_type != D_DIRADD)
12526 panic("softdep_fsync: Unexpected type %s",
12527 TYPENAME(wk->wk_type));
12528 dap = WK_DIRADD(wk);
12530 * Flush our parent if this directory entry has a MKDIR_PARENT
12531 * dependency or is contained in a newly allocated block.
12533 if (dap->da_state & DIRCHG)
12534 pagedep = dap->da_previous->dm_pagedep;
12536 pagedep = dap->da_pagedep;
12537 parentino = pagedep->pd_ino;
12538 lbn = pagedep->pd_lbn;
12539 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12540 panic("softdep_fsync: dirty");
12541 if ((dap->da_state & MKDIR_PARENT) ||
12542 (pagedep->pd_state & NEWBLOCK))
12547 * If we are being fsync'ed as part of vgone'ing this vnode,
12548 * then we will not be able to release and recover the
12549 * vnode below, so we just have to give up on writing its
12550 * directory entry out. It will eventually be written, just
12551 * not now, but then the user was not asking to have it
12552 * written, so we are not breaking any promises.
12554 if (VN_IS_DOOMED(vp))
12557 * We prevent deadlock by always fetching inodes from the
12558 * root, moving down the directory tree. Thus, when fetching
12559 * our parent directory, we first try to get the lock. If
12560 * that fails, we must unlock ourselves before requesting
12561 * the lock on our parent. See the comment in ufs_lookup
12562 * for details on possible races.
12565 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
12566 FFSV_FORCEINSMQ)) {
12568 * Unmount cannot proceed after unlock because
12569 * caller must have called vn_start_write().
12572 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
12573 &pvp, FFSV_FORCEINSMQ);
12574 MPASS(VTOI(pvp)->i_mode != 0);
12575 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12576 if (VN_IS_DOOMED(vp)) {
12585 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12586 * that are contained in direct blocks will be resolved by
12587 * doing a ffs_update. Pagedeps contained in indirect blocks
12588 * may require a complete sync'ing of the directory. So, we
12589 * try the cheap and fast ffs_update first, and if that fails,
12590 * then we do the slower ffs_syncvnode of the directory.
12595 if ((error = ffs_update(pvp, 1)) != 0) {
12601 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12602 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12603 if (wk->wk_type != D_DIRADD)
12604 panic("softdep_fsync: Unexpected type %s",
12605 TYPENAME(wk->wk_type));
12606 dap = WK_DIRADD(wk);
12607 if (dap->da_state & DIRCHG)
12608 pagedep = dap->da_previous->dm_pagedep;
12610 pagedep = dap->da_pagedep;
12611 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12614 if (pagedep_new_block && (error =
12615 ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12625 * Flush directory page containing the inode's name.
12627 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12630 error = bwrite(bp);
12637 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12645 * Flush all the dirty bitmaps associated with the block device
12646 * before flushing the rest of the dirty blocks so as to reduce
12647 * the number of dependencies that will have to be rolled back.
12652 softdep_fsync_mountdev(vp)
12655 struct buf *bp, *nbp;
12656 struct worklist *wk;
12659 if (!vn_isdisk(vp, NULL))
12660 panic("softdep_fsync_mountdev: vnode not a disk");
12661 bo = &vp->v_bufobj;
12664 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12666 * If it is already scheduled, skip to the next buffer.
12668 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12671 if ((bp->b_flags & B_DELWRI) == 0)
12672 panic("softdep_fsync_mountdev: not dirty");
12674 * We are only interested in bitmaps with outstanding
12677 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12678 wk->wk_type != D_BMSAFEMAP ||
12679 (bp->b_vflags & BV_BKGRDINPROG)) {
12685 (void) bawrite(bp);
12693 * Sync all cylinder groups that were dirty at the time this function is
12694 * called. Newly dirtied cgs will be inserted before the sentinel. This
12695 * is used to flush freedep activity that may be holding up writes to a
12699 sync_cgs(mp, waitfor)
12703 struct bmsafemap *bmsafemap;
12704 struct bmsafemap *sentinel;
12705 struct ufsmount *ump;
12709 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12710 sentinel->sm_cg = -1;
12711 ump = VFSTOUFS(mp);
12714 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12715 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12716 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12717 /* Skip sentinels and cgs with no work to release. */
12718 if (bmsafemap->sm_cg == -1 ||
12719 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12720 LIST_EMPTY(&bmsafemap->sm_freewr))) {
12721 LIST_REMOVE(sentinel, sm_next);
12722 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12726 * If we don't get the lock and we're waiting try again, if
12727 * not move on to the next buf and try to sync it.
12729 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12730 if (bp == NULL && waitfor == MNT_WAIT)
12732 LIST_REMOVE(sentinel, sm_next);
12733 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12737 if (waitfor == MNT_NOWAIT)
12740 error = bwrite(bp);
12745 LIST_REMOVE(sentinel, sm_next);
12747 free(sentinel, M_BMSAFEMAP);
12752 * This routine is called when we are trying to synchronously flush a
12753 * file. This routine must eliminate any filesystem metadata dependencies
12754 * so that the syncing routine can succeed.
12757 softdep_sync_metadata(struct vnode *vp)
12763 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12764 ("softdep_sync_metadata called on non-softdep filesystem"));
12766 * Ensure that any direct block dependencies have been cleared,
12767 * truncations are started, and inode references are journaled.
12769 ACQUIRE_LOCK(VFSTOUFS(vp->v_mount));
12771 * Write all journal records to prevent rollbacks on devvp.
12773 if (vp->v_type == VCHR)
12774 softdep_flushjournal(vp->v_mount);
12775 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12777 * Ensure that all truncates are written so we won't find deps on
12780 process_truncates(vp);
12781 FREE_LOCK(VFSTOUFS(vp->v_mount));
12787 * This routine is called when we are attempting to sync a buf with
12788 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
12789 * other IO it can but returns EBUSY if the buffer is not yet able to
12790 * be written. Dependencies which will not cause rollbacks will always
12794 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
12796 struct indirdep *indirdep;
12797 struct pagedep *pagedep;
12798 struct allocindir *aip;
12799 struct newblk *newblk;
12800 struct ufsmount *ump;
12802 struct worklist *wk;
12805 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12806 ("softdep_sync_buf called on non-softdep filesystem"));
12808 * For VCHR we just don't want to force flush any dependencies that
12809 * will cause rollbacks.
12811 if (vp->v_type == VCHR) {
12812 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12816 ump = VFSTOUFS(vp->v_mount);
12819 * As we hold the buffer locked, none of its dependencies
12824 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12825 switch (wk->wk_type) {
12827 case D_ALLOCDIRECT:
12829 newblk = WK_NEWBLK(wk);
12830 if (newblk->nb_jnewblk != NULL) {
12831 if (waitfor == MNT_NOWAIT) {
12835 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
12838 if (newblk->nb_state & DEPCOMPLETE ||
12839 waitfor == MNT_NOWAIT)
12841 nbp = newblk->nb_bmsafemap->sm_buf;
12842 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12846 if ((error = bwrite(nbp)) != 0)
12852 indirdep = WK_INDIRDEP(wk);
12853 if (waitfor == MNT_NOWAIT) {
12854 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
12855 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
12860 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
12861 panic("softdep_sync_buf: truncation pending.");
12863 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
12864 newblk = (struct newblk *)aip;
12865 if (newblk->nb_jnewblk != NULL) {
12866 jwait(&newblk->nb_jnewblk->jn_list,
12870 if (newblk->nb_state & DEPCOMPLETE)
12872 nbp = newblk->nb_bmsafemap->sm_buf;
12873 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12877 if ((error = bwrite(nbp)) != 0)
12886 * Only flush directory entries in synchronous passes.
12888 if (waitfor != MNT_WAIT) {
12893 * While syncing snapshots, we must allow recursive
12898 * We are trying to sync a directory that may
12899 * have dependencies on both its own metadata
12900 * and/or dependencies on the inodes of any
12901 * recently allocated files. We walk its diradd
12902 * lists pushing out the associated inode.
12904 pagedep = WK_PAGEDEP(wk);
12905 for (i = 0; i < DAHASHSZ; i++) {
12906 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
12908 if ((error = flush_pagedep_deps(vp, wk->wk_mp,
12909 &pagedep->pd_diraddhd[i]))) {
12924 panic("softdep_sync_buf: Unknown type %s",
12925 TYPENAME(wk->wk_type));
12936 * Flush the dependencies associated with an inodedep.
12939 flush_inodedep_deps(vp, mp, ino)
12944 struct inodedep *inodedep;
12945 struct inoref *inoref;
12946 struct ufsmount *ump;
12947 int error, waitfor;
12950 * This work is done in two passes. The first pass grabs most
12951 * of the buffers and begins asynchronously writing them. The
12952 * only way to wait for these asynchronous writes is to sleep
12953 * on the filesystem vnode which may stay busy for a long time
12954 * if the filesystem is active. So, instead, we make a second
12955 * pass over the dependencies blocking on each write. In the
12956 * usual case we will be blocking against a write that we
12957 * initiated, so when it is done the dependency will have been
12958 * resolved. Thus the second pass is expected to end quickly.
12959 * We give a brief window at the top of the loop to allow
12960 * any pending I/O to complete.
12962 ump = VFSTOUFS(mp);
12964 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
12970 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
12972 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12973 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12975 jwait(&inoref->if_list, MNT_WAIT);
12979 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
12980 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
12981 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
12982 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
12985 * If pass2, we are done, otherwise do pass 2.
12987 if (waitfor == MNT_WAIT)
12989 waitfor = MNT_WAIT;
12992 * Try freeing inodedep in case all dependencies have been removed.
12994 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
12995 (void) free_inodedep(inodedep);
13000 * Flush an inode dependency list.
13003 flush_deplist(listhead, waitfor, errorp)
13004 struct allocdirectlst *listhead;
13008 struct allocdirect *adp;
13009 struct newblk *newblk;
13010 struct ufsmount *ump;
13013 if ((adp = TAILQ_FIRST(listhead)) == NULL)
13015 ump = VFSTOUFS(adp->ad_list.wk_mp);
13017 TAILQ_FOREACH(adp, listhead, ad_next) {
13018 newblk = (struct newblk *)adp;
13019 if (newblk->nb_jnewblk != NULL) {
13020 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13023 if (newblk->nb_state & DEPCOMPLETE)
13025 bp = newblk->nb_bmsafemap->sm_buf;
13026 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
13028 if (waitfor == MNT_NOWAIT)
13033 if (waitfor == MNT_NOWAIT)
13036 *errorp = bwrite(bp);
13044 * Flush dependencies associated with an allocdirect block.
13047 flush_newblk_dep(vp, mp, lbn)
13052 struct newblk *newblk;
13053 struct ufsmount *ump;
13057 ufs2_daddr_t blkno;
13061 bo = &vp->v_bufobj;
13063 blkno = DIP(ip, i_db[lbn]);
13065 panic("flush_newblk_dep: Missing block");
13066 ump = VFSTOUFS(mp);
13069 * Loop until all dependencies related to this block are satisfied.
13070 * We must be careful to restart after each sleep in case a write
13071 * completes some part of this process for us.
13074 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
13078 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
13079 panic("flush_newblk_dep: Bad newblk %p", newblk);
13081 * Flush the journal.
13083 if (newblk->nb_jnewblk != NULL) {
13084 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
13088 * Write the bitmap dependency.
13090 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
13091 bp = newblk->nb_bmsafemap->sm_buf;
13092 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13096 error = bwrite(bp);
13103 * Write the buffer.
13107 bp = gbincore(bo, lbn);
13109 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
13110 LK_INTERLOCK, BO_LOCKPTR(bo));
13111 if (error == ENOLCK) {
13114 continue; /* Slept, retry */
13117 break; /* Failed */
13118 if (bp->b_flags & B_DELWRI) {
13120 error = bwrite(bp);
13128 * We have to wait for the direct pointers to
13129 * point at the newdirblk before the dependency
13132 error = ffs_update(vp, 1);
13141 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
13144 flush_pagedep_deps(pvp, mp, diraddhdp)
13147 struct diraddhd *diraddhdp;
13149 struct inodedep *inodedep;
13150 struct inoref *inoref;
13151 struct ufsmount *ump;
13152 struct diradd *dap;
13157 struct diraddhd unfinished;
13159 LIST_INIT(&unfinished);
13160 ump = VFSTOUFS(mp);
13163 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
13165 * Flush ourselves if this directory entry
13166 * has a MKDIR_PARENT dependency.
13168 if (dap->da_state & MKDIR_PARENT) {
13170 if ((error = ffs_update(pvp, 1)) != 0)
13174 * If that cleared dependencies, go on to next.
13176 if (dap != LIST_FIRST(diraddhdp))
13179 * All MKDIR_PARENT dependencies and all the
13180 * NEWBLOCK pagedeps that are contained in direct
13181 * blocks were resolved by doing above ffs_update.
13182 * Pagedeps contained in indirect blocks may
13183 * require a complete sync'ing of the directory.
13184 * We are in the midst of doing a complete sync,
13185 * so if they are not resolved in this pass we
13186 * defer them for now as they will be sync'ed by
13187 * our caller shortly.
13189 LIST_REMOVE(dap, da_pdlist);
13190 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
13194 * A newly allocated directory must have its "." and
13195 * ".." entries written out before its name can be
13196 * committed in its parent.
13198 inum = dap->da_newinum;
13199 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13200 panic("flush_pagedep_deps: lost inode1");
13202 * Wait for any pending journal adds to complete so we don't
13203 * cause rollbacks while syncing.
13205 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
13206 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
13208 jwait(&inoref->if_list, MNT_WAIT);
13212 if (dap->da_state & MKDIR_BODY) {
13214 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13217 MPASS(VTOI(vp)->i_mode != 0);
13218 error = flush_newblk_dep(vp, mp, 0);
13220 * If we still have the dependency we might need to
13221 * update the vnode to sync the new link count to
13224 if (error == 0 && dap == LIST_FIRST(diraddhdp))
13225 error = ffs_update(vp, 1);
13231 * If that cleared dependencies, go on to next.
13233 if (dap != LIST_FIRST(diraddhdp))
13235 if (dap->da_state & MKDIR_BODY) {
13236 inodedep_lookup(UFSTOVFS(ump), inum, 0,
13238 panic("flush_pagedep_deps: MKDIR_BODY "
13239 "inodedep %p dap %p vp %p",
13240 inodedep, dap, vp);
13244 * Flush the inode on which the directory entry depends.
13245 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
13246 * the only remaining dependency is that the updated inode
13247 * count must get pushed to disk. The inode has already
13248 * been pushed into its inode buffer (via VOP_UPDATE) at
13249 * the time of the reference count change. So we need only
13250 * locate that buffer, ensure that there will be no rollback
13251 * caused by a bitmap dependency, then write the inode buffer.
13254 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
13255 panic("flush_pagedep_deps: lost inode");
13257 * If the inode still has bitmap dependencies,
13258 * push them to disk.
13260 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
13261 bp = inodedep->id_bmsafemap->sm_buf;
13262 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
13266 if ((error = bwrite(bp)) != 0)
13269 if (dap != LIST_FIRST(diraddhdp))
13273 * If the inode is still sitting in a buffer waiting
13274 * to be written or waiting for the link count to be
13275 * adjusted update it here to flush it to disk.
13277 if (dap == LIST_FIRST(diraddhdp)) {
13279 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13282 MPASS(VTOI(vp)->i_mode != 0);
13283 error = ffs_update(vp, 1);
13290 * If we have failed to get rid of all the dependencies
13291 * then something is seriously wrong.
13293 if (dap == LIST_FIRST(diraddhdp)) {
13294 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13295 panic("flush_pagedep_deps: failed to flush "
13296 "inodedep %p ino %ju dap %p",
13297 inodedep, (uintmax_t)inum, dap);
13302 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13303 LIST_REMOVE(dap, da_pdlist);
13304 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13310 * A large burst of file addition or deletion activity can drive the
13311 * memory load excessively high. First attempt to slow things down
13312 * using the techniques below. If that fails, this routine requests
13313 * the offending operations to fall back to running synchronously
13314 * until the memory load returns to a reasonable level.
13317 softdep_slowdown(vp)
13320 struct ufsmount *ump;
13322 int max_softdeps_hard;
13324 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13325 ("softdep_slowdown called on non-softdep filesystem"));
13326 ump = VFSTOUFS(vp->v_mount);
13330 * Check for journal space if needed.
13332 if (DOINGSUJ(vp)) {
13333 if (journal_space(ump, 0) == 0)
13337 * If the system is under its limits and our filesystem is
13338 * not responsible for more than our share of the usage and
13339 * we are not low on journal space, then no need to slow down.
13341 max_softdeps_hard = max_softdeps * 11 / 10;
13342 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13343 dep_current[D_INODEDEP] < max_softdeps_hard &&
13344 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13345 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13346 ump->softdep_curdeps[D_DIRREM] <
13347 (max_softdeps_hard / 2) / stat_flush_threads &&
13348 ump->softdep_curdeps[D_INODEDEP] <
13349 max_softdeps_hard / stat_flush_threads &&
13350 ump->softdep_curdeps[D_INDIRDEP] <
13351 (max_softdeps_hard / 1000) / stat_flush_threads &&
13352 ump->softdep_curdeps[D_FREEBLKS] <
13353 max_softdeps_hard / stat_flush_threads) {
13358 * If the journal is low or our filesystem is over its limit
13359 * then speedup the cleanup.
13361 if (ump->softdep_curdeps[D_INDIRDEP] <
13362 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13363 softdep_speedup(ump);
13364 stat_sync_limit_hit += 1;
13367 * We only slow down the rate at which new dependencies are
13368 * generated if we are not using journaling. With journaling,
13369 * the cleanup should always be sufficient to keep things
13378 * Called by the allocation routines when they are about to fail
13379 * in the hope that we can free up the requested resource (inodes
13382 * First check to see if the work list has anything on it. If it has,
13383 * clean up entries until we successfully free the requested resource.
13384 * Because this process holds inodes locked, we cannot handle any remove
13385 * requests that might block on a locked inode as that could lead to
13386 * deadlock. If the worklist yields none of the requested resource,
13387 * start syncing out vnodes to free up the needed space.
13390 softdep_request_cleanup(fs, vp, cred, resource)
13393 struct ucred *cred;
13396 struct ufsmount *ump;
13399 ufs2_daddr_t needed;
13400 int error, failed_vnode;
13403 * If we are being called because of a process doing a
13404 * copy-on-write, then it is not safe to process any
13405 * worklist items as we will recurse into the copyonwrite
13406 * routine. This will result in an incoherent snapshot.
13407 * If the vnode that we hold is a snapshot, we must avoid
13408 * handling other resources that could cause deadlock.
13410 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13413 if (resource == FLUSH_BLOCKS_WAIT)
13414 stat_cleanup_blkrequests += 1;
13416 stat_cleanup_inorequests += 1;
13419 ump = VFSTOUFS(mp);
13420 mtx_assert(UFS_MTX(ump), MA_OWNED);
13422 error = ffs_update(vp, 1);
13423 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13428 * If we are in need of resources, start by cleaning up
13429 * any block removals associated with our inode.
13432 process_removes(vp);
13433 process_truncates(vp);
13436 * Now clean up at least as many resources as we will need.
13438 * When requested to clean up inodes, the number that are needed
13439 * is set by the number of simultaneous writers (mnt_writeopcount)
13440 * plus a bit of slop (2) in case some more writers show up while
13443 * When requested to free up space, the amount of space that
13444 * we need is enough blocks to allocate a full-sized segment
13445 * (fs_contigsumsize). The number of such segments that will
13446 * be needed is set by the number of simultaneous writers
13447 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13448 * writers show up while we are cleaning.
13450 * Additionally, if we are unpriviledged and allocating space,
13451 * we need to ensure that we clean up enough blocks to get the
13452 * needed number of blocks over the threshold of the minimum
13453 * number of blocks required to be kept free by the filesystem
13456 if (resource == FLUSH_INODES_WAIT) {
13457 needed = vfs_mount_fetch_counter(vp->v_mount,
13458 MNT_COUNT_WRITEOPCOUNT) + 2;
13459 } else if (resource == FLUSH_BLOCKS_WAIT) {
13460 needed = (vfs_mount_fetch_counter(vp->v_mount,
13461 MNT_COUNT_WRITEOPCOUNT) + 2) * fs->fs_contigsumsize;
13462 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE))
13463 needed += fragstoblks(fs,
13464 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13465 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13467 printf("softdep_request_cleanup: Unknown resource type %d\n",
13472 starttime = time_second;
13474 if (resource == FLUSH_BLOCKS_WAIT &&
13475 fs->fs_cstotal.cs_nbfree <= needed)
13476 softdep_send_speedup(ump, needed * fs->fs_bsize,
13478 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13479 fs->fs_cstotal.cs_nbfree <= needed) ||
13480 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13481 fs->fs_cstotal.cs_nifree <= needed)) {
13483 if (ump->softdep_on_worklist > 0 &&
13484 process_worklist_item(UFSTOVFS(ump),
13485 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13486 stat_worklist_push += 1;
13490 * If we still need resources and there are no more worklist
13491 * entries to process to obtain them, we have to start flushing
13492 * the dirty vnodes to force the release of additional requests
13493 * to the worklist that we can then process to reap addition
13494 * resources. We walk the vnodes associated with the mount point
13495 * until we get the needed worklist requests that we can reap.
13497 * If there are several threads all needing to clean the same
13498 * mount point, only one is allowed to walk the mount list.
13499 * When several threads all try to walk the same mount list,
13500 * they end up competing with each other and often end up in
13501 * livelock. This approach ensures that forward progress is
13502 * made at the cost of occational ENOSPC errors being returned
13503 * that might otherwise have been avoided.
13506 if ((resource == FLUSH_BLOCKS_WAIT &&
13507 fs->fs_cstotal.cs_nbfree <= needed) ||
13508 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13509 fs->fs_cstotal.cs_nifree <= needed)) {
13511 if ((ump->um_softdep->sd_flags & FLUSH_RC_ACTIVE) == 0) {
13512 ump->um_softdep->sd_flags |= FLUSH_RC_ACTIVE;
13514 failed_vnode = softdep_request_cleanup_flush(mp, ump);
13516 ump->um_softdep->sd_flags &= ~FLUSH_RC_ACTIVE;
13518 if (ump->softdep_on_worklist > 0) {
13519 stat_cleanup_retries += 1;
13527 stat_cleanup_failures += 1;
13529 if (time_second - starttime > stat_cleanup_high_delay)
13530 stat_cleanup_high_delay = time_second - starttime;
13536 * Scan the vnodes for the specified mount point flushing out any
13537 * vnodes that can be locked without waiting. Finally, try to flush
13538 * the device associated with the mount point if it can be locked
13541 * We return 0 if we were able to lock every vnode in our scan.
13542 * If we had to skip one or more vnodes, we return 1.
13545 softdep_request_cleanup_flush(mp, ump)
13547 struct ufsmount *ump;
13550 struct vnode *lvp, *mvp;
13555 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13556 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13560 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT,
13565 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
13569 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13572 lvp = ump->um_devvp;
13573 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13574 VOP_FSYNC(lvp, MNT_NOWAIT, td);
13577 return (failed_vnode);
13581 softdep_excess_items(struct ufsmount *ump, int item)
13584 KASSERT(item >= 0 && item < D_LAST, ("item %d", item));
13585 return (dep_current[item] > max_softdeps &&
13586 ump->softdep_curdeps[item] > max_softdeps /
13587 stat_flush_threads);
13591 schedule_cleanup(struct mount *mp)
13593 struct ufsmount *ump;
13596 ump = VFSTOUFS(mp);
13600 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13601 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13603 * No ast is delivered to kernel threads, so nobody
13604 * would deref the mp. Some kernel threads
13605 * explicitely check for AST, e.g. NFS daemon does
13606 * this in the serving loop.
13610 if (td->td_su != NULL)
13611 vfs_rel(td->td_su);
13615 td->td_flags |= TDF_ASTPENDING;
13620 softdep_ast_cleanup_proc(struct thread *td)
13623 struct ufsmount *ump;
13627 while ((mp = td->td_su) != NULL) {
13629 error = vfs_busy(mp, MBF_NOWAIT);
13633 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13634 ump = VFSTOUFS(mp);
13638 if (softdep_excess_items(ump, D_INODEDEP)) {
13640 request_cleanup(mp, FLUSH_INODES);
13642 if (softdep_excess_items(ump, D_DIRREM)) {
13644 request_cleanup(mp, FLUSH_BLOCKS);
13647 if (softdep_excess_items(ump, D_NEWBLK) ||
13648 softdep_excess_items(ump, D_ALLOCDIRECT) ||
13649 softdep_excess_items(ump, D_ALLOCINDIR)) {
13650 error = vn_start_write(NULL, &mp,
13654 VFS_SYNC(mp, MNT_WAIT);
13655 vn_finished_write(mp);
13658 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13664 if ((mp = td->td_su) != NULL) {
13671 * If memory utilization has gotten too high, deliberately slow things
13672 * down and speed up the I/O processing.
13675 request_cleanup(mp, resource)
13679 struct thread *td = curthread;
13680 struct ufsmount *ump;
13682 ump = VFSTOUFS(mp);
13685 * We never hold up the filesystem syncer or buf daemon.
13687 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13690 * First check to see if the work list has gotten backlogged.
13691 * If it has, co-opt this process to help clean up two entries.
13692 * Because this process may hold inodes locked, we cannot
13693 * handle any remove requests that might block on a locked
13694 * inode as that could lead to deadlock. We set TDP_SOFTDEP
13695 * to avoid recursively processing the worklist.
13697 if (ump->softdep_on_worklist > max_softdeps / 10) {
13698 td->td_pflags |= TDP_SOFTDEP;
13699 process_worklist_item(mp, 2, LK_NOWAIT);
13700 td->td_pflags &= ~TDP_SOFTDEP;
13701 stat_worklist_push += 2;
13705 * Next, we attempt to speed up the syncer process. If that
13706 * is successful, then we allow the process to continue.
13708 if (softdep_speedup(ump) &&
13709 resource != FLUSH_BLOCKS_WAIT &&
13710 resource != FLUSH_INODES_WAIT)
13713 * If we are resource constrained on inode dependencies, try
13714 * flushing some dirty inodes. Otherwise, we are constrained
13715 * by file deletions, so try accelerating flushes of directories
13716 * with removal dependencies. We would like to do the cleanup
13717 * here, but we probably hold an inode locked at this point and
13718 * that might deadlock against one that we try to clean. So,
13719 * the best that we can do is request the syncer daemon to do
13720 * the cleanup for us.
13722 switch (resource) {
13725 case FLUSH_INODES_WAIT:
13726 ACQUIRE_GBLLOCK(&lk);
13727 stat_ino_limit_push += 1;
13728 req_clear_inodedeps += 1;
13730 stat_countp = &stat_ino_limit_hit;
13734 case FLUSH_BLOCKS_WAIT:
13735 ACQUIRE_GBLLOCK(&lk);
13736 stat_blk_limit_push += 1;
13737 req_clear_remove += 1;
13739 stat_countp = &stat_blk_limit_hit;
13743 panic("request_cleanup: unknown type");
13746 * Hopefully the syncer daemon will catch up and awaken us.
13747 * We wait at most tickdelay before proceeding in any case.
13749 ACQUIRE_GBLLOCK(&lk);
13752 if (callout_pending(&softdep_callout) == FALSE)
13753 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13756 if ((td->td_pflags & TDP_KTHREAD) == 0)
13757 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13765 * Awaken processes pausing in request_cleanup and clear proc_waiting
13766 * to indicate that there is no longer a timer running. Pause_timer
13767 * will be called with the global softdep mutex (&lk) locked.
13774 GBLLOCK_OWNED(&lk);
13776 * The callout_ API has acquired mtx and will hold it around this
13779 *stat_countp += proc_waiting;
13780 wakeup(&proc_waiting);
13784 * If requested, try removing inode or removal dependencies.
13787 check_clear_deps(mp)
13790 struct ufsmount *ump;
13794 * Tell the lower layers that any TRIM or WRITE transactions that have
13795 * been delayed for performance reasons should proceed to help alleviate
13796 * the shortage faster. The race between checking req_* and the softdep
13797 * mutex (lk) is fine since this is an advisory operation that at most
13798 * causes deferred work to be done sooner.
13800 ump = VFSTOUFS(mp);
13801 suj_susp = MOUNTEDSUJ(mp) && ump->softdep_jblocks->jb_suspended;
13802 if (req_clear_remove || req_clear_inodedeps || suj_susp) {
13804 softdep_send_speedup(ump, 0, BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE);
13809 * If we are suspended, it may be because of our using
13810 * too many inodedeps, so help clear them out.
13813 clear_inodedeps(mp);
13816 * General requests for cleanup of backed up dependencies
13818 ACQUIRE_GBLLOCK(&lk);
13819 if (req_clear_inodedeps) {
13820 req_clear_inodedeps -= 1;
13822 clear_inodedeps(mp);
13823 ACQUIRE_GBLLOCK(&lk);
13824 wakeup(&proc_waiting);
13826 if (req_clear_remove) {
13827 req_clear_remove -= 1;
13830 ACQUIRE_GBLLOCK(&lk);
13831 wakeup(&proc_waiting);
13837 * Flush out a directory with at least one removal dependency in an effort to
13838 * reduce the number of dirrem, freefile, and freeblks dependency structures.
13844 struct pagedep_hashhead *pagedephd;
13845 struct pagedep *pagedep;
13846 struct ufsmount *ump;
13852 ump = VFSTOUFS(mp);
13855 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
13856 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
13857 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
13858 ump->pagedep_nextclean = 0;
13859 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
13860 if (LIST_EMPTY(&pagedep->pd_dirremhd))
13862 ino = pagedep->pd_ino;
13863 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13868 * Let unmount clear deps
13870 error = vfs_busy(mp, MBF_NOWAIT);
13873 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13877 softdep_error("clear_remove: vget", error);
13880 MPASS(VTOI(vp)->i_mode != 0);
13881 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13882 softdep_error("clear_remove: fsync", error);
13883 bo = &vp->v_bufobj;
13889 vn_finished_write(mp);
13897 * Clear out a block of dirty inodes in an effort to reduce
13898 * the number of inodedep dependency structures.
13901 clear_inodedeps(mp)
13904 struct inodedep_hashhead *inodedephd;
13905 struct inodedep *inodedep;
13906 struct ufsmount *ump;
13910 ino_t firstino, lastino, ino;
13912 ump = VFSTOUFS(mp);
13916 * Pick a random inode dependency to be cleared.
13917 * We will then gather up all the inodes in its block
13918 * that have dependencies and flush them out.
13920 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
13921 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
13922 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
13923 ump->inodedep_nextclean = 0;
13924 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
13927 if (inodedep == NULL)
13930 * Find the last inode in the block with dependencies.
13932 firstino = rounddown2(inodedep->id_ino, INOPB(fs));
13933 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
13934 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
13937 * Asynchronously push all but the last inode with dependencies.
13938 * Synchronously push the last inode with dependencies to ensure
13939 * that the inode block gets written to free up the inodedeps.
13941 for (ino = firstino; ino <= lastino; ino++) {
13942 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13944 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13947 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
13949 vn_finished_write(mp);
13953 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13954 FFSV_FORCEINSMQ)) != 0) {
13955 softdep_error("clear_inodedeps: vget", error);
13957 vn_finished_write(mp);
13962 if (VTOI(vp)->i_mode == 0) {
13964 } else if (ino == lastino) {
13965 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)))
13966 softdep_error("clear_inodedeps: fsync1", error);
13968 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13969 softdep_error("clear_inodedeps: fsync2", error);
13970 BO_LOCK(&vp->v_bufobj);
13972 BO_UNLOCK(&vp->v_bufobj);
13975 vn_finished_write(mp);
13981 softdep_buf_append(bp, wkhd)
13983 struct workhead *wkhd;
13985 struct worklist *wk;
13986 struct ufsmount *ump;
13988 if ((wk = LIST_FIRST(wkhd)) == NULL)
13990 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13991 ("softdep_buf_append called on non-softdep filesystem"));
13992 ump = VFSTOUFS(wk->wk_mp);
13994 while ((wk = LIST_FIRST(wkhd)) != NULL) {
13995 WORKLIST_REMOVE(wk);
13996 WORKLIST_INSERT(&bp->b_dep, wk);
14003 softdep_inode_append(ip, cred, wkhd)
14005 struct ucred *cred;
14006 struct workhead *wkhd;
14010 struct ufsmount *ump;
14014 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
14015 ("softdep_inode_append called on non-softdep filesystem"));
14017 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
14018 (int)fs->fs_bsize, cred, &bp);
14021 softdep_freework(wkhd);
14024 softdep_buf_append(bp, wkhd);
14029 softdep_freework(wkhd)
14030 struct workhead *wkhd;
14032 struct worklist *wk;
14033 struct ufsmount *ump;
14035 if ((wk = LIST_FIRST(wkhd)) == NULL)
14037 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
14038 ("softdep_freework called on non-softdep filesystem"));
14039 ump = VFSTOUFS(wk->wk_mp);
14041 handle_jwork(wkhd);
14045 static struct ufsmount *
14046 softdep_bp_to_mp(bp)
14052 if (LIST_EMPTY(&bp->b_dep))
14055 KASSERT(vp != NULL,
14056 ("%s, buffer with dependencies lacks vnode", __func__));
14059 * The ump mount point is stable after we get a correct
14060 * pointer, since bp is locked and this prevents unmount from
14061 * proceeding. But to get to it, we cannot dereference bp->b_dep
14062 * head wk_mp, because we do not yet own SU ump lock and
14063 * workitem might be freed while dereferenced.
14066 switch (vp->v_type) {
14069 mp = vp->v_type == VCHR ? vp->v_rdev->si_mountpt : NULL;
14082 vn_printf(vp, "softdep_bp_to_mp: unexpected block device\n");
14090 vn_printf(vp, "unknown vnode type");
14094 return (VFSTOUFS(mp));
14098 * Function to determine if the buffer has outstanding dependencies
14099 * that will cause a roll-back if the buffer is written. If wantcount
14100 * is set, return number of dependencies, otherwise just yes or no.
14103 softdep_count_dependencies(bp, wantcount)
14107 struct worklist *wk;
14108 struct ufsmount *ump;
14109 struct bmsafemap *bmsafemap;
14110 struct freework *freework;
14111 struct inodedep *inodedep;
14112 struct indirdep *indirdep;
14113 struct freeblks *freeblks;
14114 struct allocindir *aip;
14115 struct pagedep *pagedep;
14116 struct dirrem *dirrem;
14117 struct newblk *newblk;
14118 struct mkdir *mkdir;
14119 struct diradd *dap;
14122 ump = softdep_bp_to_mp(bp);
14127 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
14128 switch (wk->wk_type) {
14131 inodedep = WK_INODEDEP(wk);
14132 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
14133 /* bitmap allocation dependency */
14138 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
14139 /* direct block pointer dependency */
14144 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
14145 /* direct block pointer dependency */
14150 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
14151 /* Add reference dependency. */
14159 indirdep = WK_INDIRDEP(wk);
14161 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
14162 /* indirect truncation dependency */
14168 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
14169 /* indirect block pointer dependency */
14177 pagedep = WK_PAGEDEP(wk);
14178 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
14179 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
14180 /* Journal remove ref dependency. */
14186 for (i = 0; i < DAHASHSZ; i++) {
14188 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
14189 /* directory entry dependency */
14198 bmsafemap = WK_BMSAFEMAP(wk);
14199 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
14200 /* Add reference dependency. */
14205 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
14206 /* Allocate block dependency. */
14214 freeblks = WK_FREEBLKS(wk);
14215 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
14216 /* Freeblk journal dependency. */
14223 case D_ALLOCDIRECT:
14225 newblk = WK_NEWBLK(wk);
14226 if (newblk->nb_jnewblk) {
14227 /* Journal allocate dependency. */
14235 mkdir = WK_MKDIR(wk);
14236 if (mkdir->md_jaddref) {
14237 /* Journal reference dependency. */
14249 /* never a dependency on these blocks */
14253 panic("softdep_count_dependencies: Unexpected type %s",
14254 TYPENAME(wk->wk_type));
14264 * Acquire exclusive access to a buffer.
14265 * Must be called with a locked mtx parameter.
14266 * Return acquired buffer or NULL on failure.
14268 static struct buf *
14269 getdirtybuf(bp, lock, waitfor)
14271 struct rwlock *lock;
14276 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
14277 if (waitfor != MNT_WAIT)
14279 error = BUF_LOCK(bp,
14280 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
14282 * Even if we successfully acquire bp here, we have dropped
14283 * lock, which may violates our guarantee.
14287 else if (error != ENOLCK)
14288 panic("getdirtybuf: inconsistent lock: %d", error);
14292 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14293 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
14295 BO_LOCK(bp->b_bufobj);
14297 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
14298 bp->b_vflags |= BV_BKGRDWAIT;
14299 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
14300 PRIBIO | PDROP, "getbuf", 0);
14302 BO_UNLOCK(bp->b_bufobj);
14307 if (waitfor != MNT_WAIT)
14309 #ifdef DEBUG_VFS_LOCKS
14310 if (bp->b_vp->v_type != VCHR)
14311 ASSERT_BO_WLOCKED(bp->b_bufobj);
14313 bp->b_vflags |= BV_BKGRDWAIT;
14314 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
14317 if ((bp->b_flags & B_DELWRI) == 0) {
14327 * Check if it is safe to suspend the file system now. On entry,
14328 * the vnode interlock for devvp should be held. Return 0 with
14329 * the mount interlock held if the file system can be suspended now,
14330 * otherwise return EAGAIN with the mount interlock held.
14333 softdep_check_suspend(struct mount *mp,
14334 struct vnode *devvp,
14335 int softdep_depcnt,
14336 int softdep_accdepcnt,
14337 int secondary_writes,
14338 int secondary_accwrites)
14341 struct ufsmount *ump;
14342 struct inodedep *inodedep;
14343 int error, unlinked;
14345 bo = &devvp->v_bufobj;
14346 ASSERT_BO_WLOCKED(bo);
14349 * If we are not running with soft updates, then we need only
14350 * deal with secondary writes as we try to suspend.
14352 if (MOUNTEDSOFTDEP(mp) == 0) {
14354 while (mp->mnt_secondary_writes != 0) {
14356 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
14357 (PUSER - 1) | PDROP, "secwr", 0);
14363 * Reasons for needing more work before suspend:
14364 * - Dirty buffers on devvp.
14365 * - Secondary writes occurred after start of vnode sync loop
14368 if (bo->bo_numoutput > 0 ||
14369 bo->bo_dirty.bv_cnt > 0 ||
14370 secondary_writes != 0 ||
14371 mp->mnt_secondary_writes != 0 ||
14372 secondary_accwrites != mp->mnt_secondary_accwrites)
14379 * If we are running with soft updates, then we need to coordinate
14380 * with them as we try to suspend.
14382 ump = VFSTOUFS(mp);
14384 if (!TRY_ACQUIRE_LOCK(ump)) {
14392 if (mp->mnt_secondary_writes != 0) {
14395 msleep(&mp->mnt_secondary_writes,
14397 (PUSER - 1) | PDROP, "secwr", 0);
14405 if (MOUNTEDSUJ(mp)) {
14406 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
14408 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14409 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14410 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14412 !check_inodedep_free(inodedep))
14419 * Reasons for needing more work before suspend:
14420 * - Dirty buffers on devvp.
14421 * - Softdep activity occurred after start of vnode sync loop
14422 * - Secondary writes occurred after start of vnode sync loop
14425 if (bo->bo_numoutput > 0 ||
14426 bo->bo_dirty.bv_cnt > 0 ||
14427 softdep_depcnt != unlinked ||
14428 ump->softdep_deps != unlinked ||
14429 softdep_accdepcnt != ump->softdep_accdeps ||
14430 secondary_writes != 0 ||
14431 mp->mnt_secondary_writes != 0 ||
14432 secondary_accwrites != mp->mnt_secondary_accwrites)
14441 * Get the number of dependency structures for the file system, both
14442 * the current number and the total number allocated. These will
14443 * later be used to detect that softdep processing has occurred.
14446 softdep_get_depcounts(struct mount *mp,
14447 int *softdep_depsp,
14448 int *softdep_accdepsp)
14450 struct ufsmount *ump;
14452 if (MOUNTEDSOFTDEP(mp) == 0) {
14453 *softdep_depsp = 0;
14454 *softdep_accdepsp = 0;
14457 ump = VFSTOUFS(mp);
14459 *softdep_depsp = ump->softdep_deps;
14460 *softdep_accdepsp = ump->softdep_accdeps;
14465 * Wait for pending output on a vnode to complete.
14472 ASSERT_VOP_LOCKED(vp, "drain_output");
14473 (void)bufobj_wwait(&vp->v_bufobj, 0, 0);
14477 * Called whenever a buffer that is being invalidated or reallocated
14478 * contains dependencies. This should only happen if an I/O error has
14479 * occurred. The routine is called with the buffer locked.
14482 softdep_deallocate_dependencies(bp)
14486 if ((bp->b_ioflags & BIO_ERROR) == 0)
14487 panic("softdep_deallocate_dependencies: dangling deps");
14488 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14489 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14491 printf("softdep_deallocate_dependencies: "
14492 "got error %d while accessing filesystem\n", bp->b_error);
14493 if (bp->b_error != ENXIO)
14494 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14498 * Function to handle asynchronous write errors in the filesystem.
14501 softdep_error(func, error)
14506 /* XXX should do something better! */
14507 printf("%s: got error %d while accessing filesystem\n", func, error);
14512 /* exported to ffs_vfsops.c */
14513 extern void db_print_ffs(struct ufsmount *ump);
14515 db_print_ffs(struct ufsmount *ump)
14517 db_printf("mp %p (%s) devvp %p\n", ump->um_mountp,
14518 ump->um_mountp->mnt_stat.f_mntonname, ump->um_devvp);
14519 db_printf(" fs %p su_wl %d su_deps %d su_req %d\n",
14520 ump->um_fs, ump->softdep_on_worklist,
14521 ump->softdep_deps, ump->softdep_req);
14525 worklist_print(struct worklist *wk, int verbose)
14529 db_printf("%s: %p state 0x%b\n", TYPENAME(wk->wk_type), wk,
14530 (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS);
14533 db_printf("worklist: %p type %s state 0x%b next %p\n ", wk,
14534 TYPENAME(wk->wk_type), (u_int)wk->wk_state, PRINT_SOFTDEP_FLAGS,
14535 LIST_NEXT(wk, wk_list));
14536 db_print_ffs(VFSTOUFS(wk->wk_mp));
14540 inodedep_print(struct inodedep *inodedep, int verbose)
14543 worklist_print(&inodedep->id_list, 0);
14544 db_printf(" fs %p ino %jd inoblk %jd delta %jd nlink %jd\n",
14546 (intmax_t)inodedep->id_ino,
14547 (intmax_t)fsbtodb(inodedep->id_fs,
14548 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14549 (intmax_t)inodedep->id_nlinkdelta,
14550 (intmax_t)inodedep->id_savednlink);
14555 db_printf(" bmsafemap %p, mkdiradd %p, inoreflst %p\n",
14556 inodedep->id_bmsafemap,
14557 inodedep->id_mkdiradd,
14558 TAILQ_FIRST(&inodedep->id_inoreflst));
14559 db_printf(" dirremhd %p, pendinghd %p, bufwait %p\n",
14560 LIST_FIRST(&inodedep->id_dirremhd),
14561 LIST_FIRST(&inodedep->id_pendinghd),
14562 LIST_FIRST(&inodedep->id_bufwait));
14563 db_printf(" inowait %p, inoupdt %p, newinoupdt %p\n",
14564 LIST_FIRST(&inodedep->id_inowait),
14565 TAILQ_FIRST(&inodedep->id_inoupdt),
14566 TAILQ_FIRST(&inodedep->id_newinoupdt));
14567 db_printf(" extupdt %p, newextupdt %p, freeblklst %p\n",
14568 TAILQ_FIRST(&inodedep->id_extupdt),
14569 TAILQ_FIRST(&inodedep->id_newextupdt),
14570 TAILQ_FIRST(&inodedep->id_freeblklst));
14571 db_printf(" saveino %p, savedsize %jd, savedextsize %jd\n",
14572 inodedep->id_savedino1,
14573 (intmax_t)inodedep->id_savedsize,
14574 (intmax_t)inodedep->id_savedextsize);
14578 newblk_print(struct newblk *nbp)
14581 worklist_print(&nbp->nb_list, 0);
14582 db_printf(" newblkno %jd\n", (intmax_t)nbp->nb_newblkno);
14583 db_printf(" jnewblk %p, bmsafemap %p, freefrag %p\n",
14585 &nbp->nb_bmsafemap,
14586 &nbp->nb_freefrag);
14587 db_printf(" indirdeps %p, newdirblk %p, jwork %p\n",
14588 LIST_FIRST(&nbp->nb_indirdeps),
14589 LIST_FIRST(&nbp->nb_newdirblk),
14590 LIST_FIRST(&nbp->nb_jwork));
14594 allocdirect_print(struct allocdirect *adp)
14597 newblk_print(&adp->ad_block);
14598 db_printf(" oldblkno %jd, oldsize %ld, newsize %ld\n",
14599 adp->ad_oldblkno, adp->ad_oldsize, adp->ad_newsize);
14600 db_printf(" offset %d, inodedep %p\n",
14601 adp->ad_offset, adp->ad_inodedep);
14605 allocindir_print(struct allocindir *aip)
14608 newblk_print(&aip->ai_block);
14609 db_printf(" oldblkno %jd, lbn %jd\n",
14610 (intmax_t)aip->ai_oldblkno, (intmax_t)aip->ai_lbn);
14611 db_printf(" offset %d, indirdep %p\n",
14612 aip->ai_offset, aip->ai_indirdep);
14616 mkdir_print(struct mkdir *mkdir)
14619 worklist_print(&mkdir->md_list, 0);
14620 db_printf(" diradd %p, jaddref %p, buf %p\n",
14621 mkdir->md_diradd, mkdir->md_jaddref, mkdir->md_buf);
14624 DB_SHOW_COMMAND(sd_inodedep, db_show_sd_inodedep)
14627 if (have_addr == 0) {
14628 db_printf("inodedep address required\n");
14631 inodedep_print((struct inodedep*)addr, 1);
14634 DB_SHOW_COMMAND(sd_allinodedeps, db_show_sd_allinodedeps)
14636 struct inodedep_hashhead *inodedephd;
14637 struct inodedep *inodedep;
14638 struct ufsmount *ump;
14641 if (have_addr == 0) {
14642 db_printf("ufsmount address required\n");
14645 ump = (struct ufsmount *)addr;
14646 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14647 inodedephd = &ump->inodedep_hashtbl[cnt];
14648 LIST_FOREACH(inodedep, inodedephd, id_hash) {
14649 inodedep_print(inodedep, 0);
14654 DB_SHOW_COMMAND(sd_worklist, db_show_sd_worklist)
14657 if (have_addr == 0) {
14658 db_printf("worklist address required\n");
14661 worklist_print((struct worklist *)addr, 1);
14664 DB_SHOW_COMMAND(sd_workhead, db_show_sd_workhead)
14666 struct worklist *wk;
14667 struct workhead *wkhd;
14669 if (have_addr == 0) {
14670 db_printf("worklist address required "
14671 "(for example value in bp->b_dep)\n");
14675 * We often do not have the address of the worklist head but
14676 * instead a pointer to its first entry (e.g., we have the
14677 * contents of bp->b_dep rather than &bp->b_dep). But the back
14678 * pointer of bp->b_dep will point at the head of the list, so
14679 * we cheat and use that instead. If we are in the middle of
14680 * a list we will still get the same result, so nothing
14681 * unexpected will result.
14683 wk = (struct worklist *)addr;
14686 wkhd = (struct workhead *)wk->wk_list.le_prev;
14687 LIST_FOREACH(wk, wkhd, wk_list) {
14688 switch(wk->wk_type) {
14690 inodedep_print(WK_INODEDEP(wk), 0);
14692 case D_ALLOCDIRECT:
14693 allocdirect_print(WK_ALLOCDIRECT(wk));
14696 allocindir_print(WK_ALLOCINDIR(wk));
14699 mkdir_print(WK_MKDIR(wk));
14702 worklist_print(wk, 0);
14708 DB_SHOW_COMMAND(sd_mkdir, db_show_sd_mkdir)
14710 if (have_addr == 0) {
14711 db_printf("mkdir address required\n");
14714 mkdir_print((struct mkdir *)addr);
14717 DB_SHOW_COMMAND(sd_mkdir_list, db_show_sd_mkdir_list)
14719 struct mkdirlist *mkdirlisthd;
14720 struct mkdir *mkdir;
14722 if (have_addr == 0) {
14723 db_printf("mkdir listhead address required\n");
14726 mkdirlisthd = (struct mkdirlist *)addr;
14727 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14728 mkdir_print(mkdir);
14729 if (mkdir->md_diradd != NULL) {
14731 worklist_print(&mkdir->md_diradd->da_list, 0);
14733 if (mkdir->md_jaddref != NULL) {
14735 worklist_print(&mkdir->md_jaddref->ja_list, 0);
14740 DB_SHOW_COMMAND(sd_allocdirect, db_show_sd_allocdirect)
14742 if (have_addr == 0) {
14743 db_printf("allocdirect address required\n");
14746 allocdirect_print((struct allocdirect *)addr);
14749 DB_SHOW_COMMAND(sd_allocindir, db_show_sd_allocindir)
14751 if (have_addr == 0) {
14752 db_printf("allocindir address required\n");
14755 allocindir_print((struct allocindir *)addr);
14760 #endif /* SOFTUPDATES */