2 * Copyright 1998, 2000 Marshall Kirk McKusick.
3 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
6 * The soft updates code is derived from the appendix of a University
7 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
8 * "Soft Updates: A Solution to the Metadata Update Problem in File
9 * Systems", CSE-TR-254-95, August 1995).
11 * Further information about soft updates can be obtained from:
13 * Marshall Kirk McKusick http://www.mckusick.com/softdep/
14 * 1614 Oxford Street mckusick@mckusick.com
15 * Berkeley, CA 94709-1608 +1-510-843-9542
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 #include "opt_quota.h"
50 * For now we want the safety net that the DEBUG flag provides.
56 #include <sys/param.h>
57 #include <sys/kernel.h>
58 #include <sys/systm.h>
62 #include <sys/kthread.h>
64 #include <sys/limits.h>
66 #include <sys/malloc.h>
67 #include <sys/mount.h>
68 #include <sys/mutex.h>
69 #include <sys/namei.h>
72 #include <sys/rwlock.h>
74 #include <sys/sysctl.h>
75 #include <sys/syslog.h>
76 #include <sys/vnode.h>
79 #include <ufs/ufs/dir.h>
80 #include <ufs/ufs/extattr.h>
81 #include <ufs/ufs/quota.h>
82 #include <ufs/ufs/inode.h>
83 #include <ufs/ufs/ufsmount.h>
84 #include <ufs/ffs/fs.h>
85 #include <ufs/ffs/softdep.h>
86 #include <ufs/ffs/ffs_extern.h>
87 #include <ufs/ufs/ufs_extern.h>
90 #include <vm/vm_extern.h>
91 #include <vm/vm_object.h>
93 #include <geom/geom.h>
97 #define KTR_SUJ 0 /* Define to KTR_SPARE. */
102 softdep_flushfiles(oldmnt, flags, td)
103 struct mount *oldmnt;
108 panic("softdep_flushfiles called");
112 softdep_mount(devvp, mp, fs, cred)
130 softdep_uninitialize()
141 panic("softdep_unmount called");
145 softdep_setup_sbupdate(ump, fs, bp)
146 struct ufsmount *ump;
151 panic("softdep_setup_sbupdate called");
155 softdep_setup_inomapdep(bp, ip, newinum, mode)
162 panic("softdep_setup_inomapdep called");
166 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
169 ufs2_daddr_t newblkno;
174 panic("softdep_setup_blkmapdep called");
178 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
181 ufs2_daddr_t newblkno;
182 ufs2_daddr_t oldblkno;
188 panic("softdep_setup_allocdirect called");
192 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
195 ufs2_daddr_t newblkno;
196 ufs2_daddr_t oldblkno;
202 panic("softdep_setup_allocext called");
206 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
211 ufs2_daddr_t newblkno;
212 ufs2_daddr_t oldblkno;
216 panic("softdep_setup_allocindir_page called");
220 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
225 ufs2_daddr_t newblkno;
228 panic("softdep_setup_allocindir_meta called");
232 softdep_journal_freeblocks(ip, cred, length, flags)
239 panic("softdep_journal_freeblocks called");
243 softdep_journal_fsync(ip)
247 panic("softdep_journal_fsync called");
251 softdep_setup_freeblocks(ip, length, flags)
257 panic("softdep_setup_freeblocks called");
261 softdep_freefile(pvp, ino, mode)
267 panic("softdep_freefile called");
271 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
276 struct buf *newdirbp;
280 panic("softdep_setup_directory_add called");
284 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
293 panic("softdep_change_directoryentry_offset called");
297 softdep_setup_remove(bp, dp, ip, isrmdir)
304 panic("softdep_setup_remove called");
308 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
316 panic("softdep_setup_directory_change called");
320 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
325 struct workhead *wkhd;
328 panic("%s called", __FUNCTION__);
332 softdep_setup_inofree(mp, bp, ino, wkhd)
336 struct workhead *wkhd;
339 panic("%s called", __FUNCTION__);
343 softdep_setup_unlink(dp, ip)
348 panic("%s called", __FUNCTION__);
352 softdep_setup_link(dp, ip)
357 panic("%s called", __FUNCTION__);
361 softdep_revert_link(dp, ip)
366 panic("%s called", __FUNCTION__);
370 softdep_setup_rmdir(dp, ip)
375 panic("%s called", __FUNCTION__);
379 softdep_revert_rmdir(dp, ip)
384 panic("%s called", __FUNCTION__);
388 softdep_setup_create(dp, ip)
393 panic("%s called", __FUNCTION__);
397 softdep_revert_create(dp, ip)
402 panic("%s called", __FUNCTION__);
406 softdep_setup_mkdir(dp, ip)
411 panic("%s called", __FUNCTION__);
415 softdep_revert_mkdir(dp, ip)
420 panic("%s called", __FUNCTION__);
424 softdep_setup_dotdot_link(dp, ip)
429 panic("%s called", __FUNCTION__);
433 softdep_prealloc(vp, waitok)
438 panic("%s called", __FUNCTION__);
442 softdep_journal_lookup(mp, vpp)
451 softdep_change_linkcnt(ip)
455 panic("softdep_change_linkcnt called");
459 softdep_load_inodeblock(ip)
463 panic("softdep_load_inodeblock called");
467 softdep_update_inodeblock(ip, bp, waitfor)
473 panic("softdep_update_inodeblock called");
478 struct vnode *vp; /* the "in_core" copy of the inode */
485 softdep_fsync_mountdev(vp)
493 softdep_flushworklist(oldmnt, countp, td)
494 struct mount *oldmnt;
504 softdep_sync_metadata(struct vnode *vp)
507 panic("softdep_sync_metadata called");
511 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
514 panic("softdep_sync_buf called");
522 panic("softdep_slowdown called");
526 softdep_request_cleanup(fs, vp, cred, resource)
537 softdep_check_suspend(struct mount *mp,
540 int softdep_accdepcnt,
541 int secondary_writes,
542 int secondary_accwrites)
547 (void) softdep_depcnt,
548 (void) softdep_accdepcnt;
550 bo = &devvp->v_bufobj;
551 ASSERT_BO_WLOCKED(bo);
554 while (mp->mnt_secondary_writes != 0) {
556 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
557 (PUSER - 1) | PDROP, "secwr", 0);
563 * Reasons for needing more work before suspend:
564 * - Dirty buffers on devvp.
565 * - Secondary writes occurred after start of vnode sync loop
568 if (bo->bo_numoutput > 0 ||
569 bo->bo_dirty.bv_cnt > 0 ||
570 secondary_writes != 0 ||
571 mp->mnt_secondary_writes != 0 ||
572 secondary_accwrites != mp->mnt_secondary_accwrites)
579 softdep_get_depcounts(struct mount *mp,
581 int *softdepactiveaccp)
585 *softdepactiveaccp = 0;
589 softdep_buf_append(bp, wkhd)
591 struct workhead *wkhd;
594 panic("softdep_buf_appendwork called");
598 softdep_inode_append(ip, cred, wkhd)
601 struct workhead *wkhd;
604 panic("softdep_inode_appendwork called");
608 softdep_freework(wkhd)
609 struct workhead *wkhd;
612 panic("softdep_freework called");
617 FEATURE(softupdates, "FFS soft-updates support");
619 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0,
620 "soft updates stats");
621 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0,
622 "total dependencies allocated");
623 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0,
624 "high use dependencies allocated");
625 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0,
626 "current dependencies allocated");
627 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0,
628 "current dependencies written");
630 unsigned long dep_current[D_LAST + 1];
631 unsigned long dep_highuse[D_LAST + 1];
632 unsigned long dep_total[D_LAST + 1];
633 unsigned long dep_write[D_LAST + 1];
635 #define SOFTDEP_TYPE(type, str, long) \
636 static MALLOC_DEFINE(M_ ## type, #str, long); \
637 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \
638 &dep_total[D_ ## type], 0, ""); \
639 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \
640 &dep_current[D_ ## type], 0, ""); \
641 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \
642 &dep_highuse[D_ ## type], 0, ""); \
643 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \
644 &dep_write[D_ ## type], 0, "");
646 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies");
647 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies");
648 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap,
649 "Block or frag allocated from cyl group map");
650 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency");
651 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode");
652 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies");
653 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block");
654 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode");
655 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode");
656 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated");
657 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry");
658 SOFTDEP_TYPE(MKDIR, mkdir, "New directory");
659 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted");
660 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block");
661 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block");
662 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free");
663 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add");
664 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove");
665 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move");
666 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block");
667 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block");
668 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag");
669 SOFTDEP_TYPE(JSEG, jseg, "Journal segment");
670 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete");
671 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency");
672 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation");
673 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete");
675 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel");
677 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes");
678 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations");
679 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data");
681 #define M_SOFTDEP_FLAGS (M_WAITOK)
684 * translate from workitem type to memory type
685 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
687 static struct malloc_type *memtype[] = {
718 #define DtoM(type) (memtype[type])
721 * Names of malloc types.
723 #define TYPENAME(type) \
724 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???")
726 * End system adaptation definitions.
729 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino)
730 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino)
733 * Internal function prototypes.
735 static void check_clear_deps(struct mount *);
736 static void softdep_error(char *, int);
737 static int softdep_process_worklist(struct mount *, int);
738 static int softdep_waitidle(struct mount *, int);
739 static void drain_output(struct vnode *);
740 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int);
741 static int check_inodedep_free(struct inodedep *);
742 static void clear_remove(struct mount *);
743 static void clear_inodedeps(struct mount *);
744 static void unlinked_inodedep(struct mount *, struct inodedep *);
745 static void clear_unlinked_inodedep(struct inodedep *);
746 static struct inodedep *first_unlinked_inodedep(struct ufsmount *);
747 static int flush_pagedep_deps(struct vnode *, struct mount *,
749 static int free_pagedep(struct pagedep *);
750 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t);
751 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t);
752 static int flush_deplist(struct allocdirectlst *, int, int *);
753 static int sync_cgs(struct mount *, int);
754 static int handle_written_filepage(struct pagedep *, struct buf *);
755 static int handle_written_sbdep(struct sbdep *, struct buf *);
756 static void initiate_write_sbdep(struct sbdep *);
757 static void diradd_inode_written(struct diradd *, struct inodedep *);
758 static int handle_written_indirdep(struct indirdep *, struct buf *,
760 static int handle_written_inodeblock(struct inodedep *, struct buf *);
761 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *,
763 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *);
764 static void handle_written_jaddref(struct jaddref *);
765 static void handle_written_jremref(struct jremref *);
766 static void handle_written_jseg(struct jseg *, struct buf *);
767 static void handle_written_jnewblk(struct jnewblk *);
768 static void handle_written_jblkdep(struct jblkdep *);
769 static void handle_written_jfreefrag(struct jfreefrag *);
770 static void complete_jseg(struct jseg *);
771 static void complete_jsegs(struct jseg *);
772 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *);
773 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *);
774 static void jremref_write(struct jremref *, struct jseg *, uint8_t *);
775 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *);
776 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *);
777 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data);
778 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *);
779 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *);
780 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *);
781 static inline void inoref_write(struct inoref *, struct jseg *,
783 static void handle_allocdirect_partdone(struct allocdirect *,
785 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *,
787 static void indirdep_complete(struct indirdep *);
788 static int indirblk_lookup(struct mount *, ufs2_daddr_t);
789 static void indirblk_insert(struct freework *);
790 static void indirblk_remove(struct freework *);
791 static void handle_allocindir_partdone(struct allocindir *);
792 static void initiate_write_filepage(struct pagedep *, struct buf *);
793 static void initiate_write_indirdep(struct indirdep*, struct buf *);
794 static void handle_written_mkdir(struct mkdir *, int);
795 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *,
797 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *);
798 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
799 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
800 static void handle_workitem_freefile(struct freefile *);
801 static int handle_workitem_remove(struct dirrem *, int);
802 static struct dirrem *newdirrem(struct buf *, struct inode *,
803 struct inode *, int, struct dirrem **);
804 static struct indirdep *indirdep_lookup(struct mount *, struct inode *,
806 static void cancel_indirdep(struct indirdep *, struct buf *,
808 static void free_indirdep(struct indirdep *);
809 static void free_diradd(struct diradd *, struct workhead *);
810 static void merge_diradd(struct inodedep *, struct diradd *);
811 static void complete_diradd(struct diradd *);
812 static struct diradd *diradd_lookup(struct pagedep *, int);
813 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *,
815 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *,
817 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *,
818 struct jremref *, struct jremref *);
819 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *,
821 static void cancel_allocindir(struct allocindir *, struct buf *bp,
822 struct freeblks *, int);
823 static int setup_trunc_indir(struct freeblks *, struct inode *,
824 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t);
825 static void complete_trunc_indir(struct freework *);
826 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *,
828 static void complete_mkdir(struct mkdir *);
829 static void free_newdirblk(struct newdirblk *);
830 static void free_jremref(struct jremref *);
831 static void free_jaddref(struct jaddref *);
832 static void free_jsegdep(struct jsegdep *);
833 static void free_jsegs(struct jblocks *);
834 static void rele_jseg(struct jseg *);
835 static void free_jseg(struct jseg *, struct jblocks *);
836 static void free_jnewblk(struct jnewblk *);
837 static void free_jblkdep(struct jblkdep *);
838 static void free_jfreefrag(struct jfreefrag *);
839 static void free_freedep(struct freedep *);
840 static void journal_jremref(struct dirrem *, struct jremref *,
842 static void cancel_jnewblk(struct jnewblk *, struct workhead *);
843 static int cancel_jaddref(struct jaddref *, struct inodedep *,
845 static void cancel_jfreefrag(struct jfreefrag *);
846 static inline void setup_freedirect(struct freeblks *, struct inode *,
848 static inline void setup_freeext(struct freeblks *, struct inode *, int, int);
849 static inline void setup_freeindir(struct freeblks *, struct inode *, int,
851 static inline struct freeblks *newfreeblks(struct mount *, struct inode *);
852 static void freeblks_free(struct ufsmount *, struct freeblks *, int);
853 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t);
854 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t);
855 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int);
856 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t,
858 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int);
859 static int cancel_pagedep(struct pagedep *, struct freeblks *, int);
860 static int deallocate_dependencies(struct buf *, struct freeblks *, int);
861 static void newblk_freefrag(struct newblk*);
862 static void free_newblk(struct newblk *);
863 static void cancel_allocdirect(struct allocdirectlst *,
864 struct allocdirect *, struct freeblks *);
865 static int check_inode_unwritten(struct inodedep *);
866 static int free_inodedep(struct inodedep *);
867 static void freework_freeblock(struct freework *);
868 static void freework_enqueue(struct freework *);
869 static int handle_workitem_freeblocks(struct freeblks *, int);
870 static int handle_complete_freeblocks(struct freeblks *, int);
871 static void handle_workitem_indirblk(struct freework *);
872 static void handle_written_freework(struct freework *);
873 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
874 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *,
876 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *,
877 struct inodedep *, struct allocindir *, ufs_lbn_t);
878 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
879 ufs2_daddr_t, ufs_lbn_t);
880 static void handle_workitem_freefrag(struct freefrag *);
881 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long,
883 static void allocdirect_merge(struct allocdirectlst *,
884 struct allocdirect *, struct allocdirect *);
885 static struct freefrag *allocindir_merge(struct allocindir *,
886 struct allocindir *);
887 static int bmsafemap_find(struct bmsafemap_hashhead *, int,
888 struct bmsafemap **);
889 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *,
890 int cg, struct bmsafemap *);
891 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int,
893 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **);
894 static int inodedep_find(struct inodedep_hashhead *, ino_t,
896 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
897 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t,
898 int, struct pagedep **);
899 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
901 static void pause_timer(void *);
902 static int request_cleanup(struct mount *, int);
903 static void schedule_cleanup(struct mount *);
904 static void softdep_ast_cleanup_proc(void);
905 static int process_worklist_item(struct mount *, int, int);
906 static void process_removes(struct vnode *);
907 static void process_truncates(struct vnode *);
908 static void jwork_move(struct workhead *, struct workhead *);
909 static void jwork_insert(struct workhead *, struct jsegdep *);
910 static void add_to_worklist(struct worklist *, int);
911 static void wake_worklist(struct worklist *);
912 static void wait_worklist(struct worklist *, char *);
913 static void remove_from_worklist(struct worklist *);
914 static void softdep_flush(void *);
915 static void softdep_flushjournal(struct mount *);
916 static int softdep_speedup(struct ufsmount *);
917 static void worklist_speedup(struct mount *);
918 static int journal_mount(struct mount *, struct fs *, struct ucred *);
919 static void journal_unmount(struct ufsmount *);
920 static int journal_space(struct ufsmount *, int);
921 static void journal_suspend(struct ufsmount *);
922 static int journal_unsuspend(struct ufsmount *ump);
923 static void softdep_prelink(struct vnode *, struct vnode *);
924 static void add_to_journal(struct worklist *);
925 static void remove_from_journal(struct worklist *);
926 static bool softdep_excess_inodes(struct ufsmount *);
927 static bool softdep_excess_dirrem(struct ufsmount *);
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 Lock", 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 */ DEBUG
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
1008 static void worklist_insert(struct workhead *, struct worklist *, int);
1009 static void worklist_remove(struct worklist *, int);
1011 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1)
1012 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0)
1013 #define WORKLIST_REMOVE(item) worklist_remove(item, 1)
1014 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0)
1017 worklist_insert(head, item, locked)
1018 struct workhead *head;
1019 struct worklist *item;
1024 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1025 if (item->wk_state & ONWORKLIST)
1026 panic("worklist_insert: %p %s(0x%X) already on list",
1027 item, TYPENAME(item->wk_type), item->wk_state);
1028 item->wk_state |= ONWORKLIST;
1029 LIST_INSERT_HEAD(head, item, wk_list);
1033 worklist_remove(item, locked)
1034 struct worklist *item;
1039 LOCK_OWNED(VFSTOUFS(item->wk_mp));
1040 if ((item->wk_state & ONWORKLIST) == 0)
1041 panic("worklist_remove: %p %s(0x%X) not on list",
1042 item, TYPENAME(item->wk_type), item->wk_state);
1043 item->wk_state &= ~ONWORKLIST;
1044 LIST_REMOVE(item, wk_list);
1049 * Merge two jsegdeps keeping only the oldest one as newer references
1050 * can't be discarded until after older references.
1052 static inline struct jsegdep *
1053 jsegdep_merge(struct jsegdep *one, struct jsegdep *two)
1055 struct jsegdep *swp;
1060 if (one->jd_seg->js_seq > two->jd_seg->js_seq) {
1065 WORKLIST_REMOVE(&two->jd_list);
1072 * If two freedeps are compatible free one to reduce list size.
1074 static inline struct freedep *
1075 freedep_merge(struct freedep *one, struct freedep *two)
1080 if (one->fd_freework == two->fd_freework) {
1081 WORKLIST_REMOVE(&two->fd_list);
1088 * Move journal work from one list to another. Duplicate freedeps and
1089 * jsegdeps are coalesced to keep the lists as small as possible.
1092 jwork_move(dst, src)
1093 struct workhead *dst;
1094 struct workhead *src;
1096 struct freedep *freedep;
1097 struct jsegdep *jsegdep;
1098 struct worklist *wkn;
1099 struct worklist *wk;
1102 ("jwork_move: dst == src"));
1105 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) {
1106 if (wk->wk_type == D_JSEGDEP)
1107 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1108 if (wk->wk_type == D_FREEDEP)
1109 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1112 while ((wk = LIST_FIRST(src)) != NULL) {
1113 WORKLIST_REMOVE(wk);
1114 WORKLIST_INSERT(dst, wk);
1115 if (wk->wk_type == D_JSEGDEP) {
1116 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep);
1119 if (wk->wk_type == D_FREEDEP)
1120 freedep = freedep_merge(WK_FREEDEP(wk), freedep);
1125 jwork_insert(dst, jsegdep)
1126 struct workhead *dst;
1127 struct jsegdep *jsegdep;
1129 struct jsegdep *jsegdepn;
1130 struct worklist *wk;
1132 LIST_FOREACH(wk, dst, wk_list)
1133 if (wk->wk_type == D_JSEGDEP)
1136 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1139 jsegdepn = WK_JSEGDEP(wk);
1140 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) {
1141 WORKLIST_REMOVE(wk);
1142 free_jsegdep(jsegdepn);
1143 WORKLIST_INSERT(dst, &jsegdep->jd_list);
1145 free_jsegdep(jsegdep);
1149 * Routines for tracking and managing workitems.
1151 static void workitem_free(struct worklist *, int);
1152 static void workitem_alloc(struct worklist *, int, struct mount *);
1153 static void workitem_reassign(struct worklist *, int);
1155 #define WORKITEM_FREE(item, type) \
1156 workitem_free((struct worklist *)(item), (type))
1157 #define WORKITEM_REASSIGN(item, type) \
1158 workitem_reassign((struct worklist *)(item), (type))
1161 workitem_free(item, type)
1162 struct worklist *item;
1165 struct ufsmount *ump;
1168 if (item->wk_state & ONWORKLIST)
1169 panic("workitem_free: %s(0x%X) still on list",
1170 TYPENAME(item->wk_type), item->wk_state);
1171 if (item->wk_type != type && type != D_NEWBLK)
1172 panic("workitem_free: type mismatch %s != %s",
1173 TYPENAME(item->wk_type), TYPENAME(type));
1175 if (item->wk_state & IOWAITING)
1177 ump = VFSTOUFS(item->wk_mp);
1179 KASSERT(ump->softdep_deps > 0,
1180 ("workitem_free: %s: softdep_deps going negative",
1181 ump->um_fs->fs_fsmnt));
1182 if (--ump->softdep_deps == 0 && ump->softdep_req)
1183 wakeup(&ump->softdep_deps);
1184 KASSERT(dep_current[item->wk_type] > 0,
1185 ("workitem_free: %s: dep_current[%s] going negative",
1186 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1187 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1188 ("workitem_free: %s: softdep_curdeps[%s] going negative",
1189 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1190 atomic_subtract_long(&dep_current[item->wk_type], 1);
1191 ump->softdep_curdeps[item->wk_type] -= 1;
1192 free(item, DtoM(type));
1196 workitem_alloc(item, type, mp)
1197 struct worklist *item;
1201 struct ufsmount *ump;
1203 item->wk_type = type;
1208 ACQUIRE_GBLLOCK(&lk);
1209 dep_current[type]++;
1210 if (dep_current[type] > dep_highuse[type])
1211 dep_highuse[type] = dep_current[type];
1215 ump->softdep_curdeps[type] += 1;
1216 ump->softdep_deps++;
1217 ump->softdep_accdeps++;
1222 workitem_reassign(item, newtype)
1223 struct worklist *item;
1226 struct ufsmount *ump;
1228 ump = VFSTOUFS(item->wk_mp);
1230 KASSERT(ump->softdep_curdeps[item->wk_type] > 0,
1231 ("workitem_reassign: %s: softdep_curdeps[%s] going negative",
1232 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1233 ump->softdep_curdeps[item->wk_type] -= 1;
1234 ump->softdep_curdeps[newtype] += 1;
1235 KASSERT(dep_current[item->wk_type] > 0,
1236 ("workitem_reassign: %s: dep_current[%s] going negative",
1237 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type)));
1238 ACQUIRE_GBLLOCK(&lk);
1239 dep_current[newtype]++;
1240 dep_current[item->wk_type]--;
1241 if (dep_current[newtype] > dep_highuse[newtype])
1242 dep_highuse[newtype] = dep_current[newtype];
1243 dep_total[newtype]++;
1245 item->wk_type = newtype;
1249 * Workitem queue management
1251 static int max_softdeps; /* maximum number of structs before slowdown */
1252 static int tickdelay = 2; /* number of ticks to pause during slowdown */
1253 static int proc_waiting; /* tracks whether we have a timeout posted */
1254 static int *stat_countp; /* statistic to count in proc_waiting timeout */
1255 static struct callout softdep_callout;
1256 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
1257 static int req_clear_remove; /* syncer process flush some freeblks */
1258 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */
1261 * runtime statistics
1263 static int stat_flush_threads; /* number of softdep flushing threads */
1264 static int stat_worklist_push; /* number of worklist cleanups */
1265 static int stat_blk_limit_push; /* number of times block limit neared */
1266 static int stat_ino_limit_push; /* number of times inode limit neared */
1267 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
1268 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
1269 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
1270 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
1271 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
1272 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
1273 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
1274 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */
1275 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */
1276 static int stat_journal_min; /* Times hit journal min threshold */
1277 static int stat_journal_low; /* Times hit journal low threshold */
1278 static int stat_journal_wait; /* Times blocked in jwait(). */
1279 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */
1280 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */
1281 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */
1282 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */
1283 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */
1284 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */
1285 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */
1286 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */
1287 static int stat_cleanup_failures; /* Number of cleanup requests that failed */
1288 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */
1290 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW,
1291 &max_softdeps, 0, "");
1292 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW,
1294 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD,
1295 &stat_flush_threads, 0, "");
1296 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW,
1297 &stat_worklist_push, 0,"");
1298 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW,
1299 &stat_blk_limit_push, 0,"");
1300 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW,
1301 &stat_ino_limit_push, 0,"");
1302 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW,
1303 &stat_blk_limit_hit, 0, "");
1304 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW,
1305 &stat_ino_limit_hit, 0, "");
1306 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW,
1307 &stat_sync_limit_hit, 0, "");
1308 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW,
1309 &stat_indir_blk_ptrs, 0, "");
1310 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW,
1311 &stat_inode_bitmap, 0, "");
1312 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW,
1313 &stat_direct_blk_ptrs, 0, "");
1314 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW,
1315 &stat_dir_entry, 0, "");
1316 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW,
1317 &stat_jaddref, 0, "");
1318 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW,
1319 &stat_jnewblk, 0, "");
1320 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW,
1321 &stat_journal_low, 0, "");
1322 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW,
1323 &stat_journal_min, 0, "");
1324 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW,
1325 &stat_journal_wait, 0, "");
1326 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW,
1327 &stat_jwait_filepage, 0, "");
1328 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW,
1329 &stat_jwait_freeblks, 0, "");
1330 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW,
1331 &stat_jwait_inode, 0, "");
1332 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW,
1333 &stat_jwait_newblk, 0, "");
1334 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW,
1335 &stat_cleanup_blkrequests, 0, "");
1336 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW,
1337 &stat_cleanup_inorequests, 0, "");
1338 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW,
1339 &stat_cleanup_high_delay, 0, "");
1340 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW,
1341 &stat_cleanup_retries, 0, "");
1342 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW,
1343 &stat_cleanup_failures, 0, "");
1344 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW,
1345 &softdep_flushcache, 0, "");
1346 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD,
1347 &stat_emptyjblocks, 0, "");
1349 SYSCTL_DECL(_vfs_ffs);
1351 /* Whether to recompute the summary at mount time */
1352 static int compute_summary_at_mount = 0;
1353 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
1354 &compute_summary_at_mount, 0, "Recompute summary at mount");
1355 static int print_threads = 0;
1356 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW,
1357 &print_threads, 0, "Notify flusher thread start/stop");
1359 /* List of all filesystems mounted with soft updates */
1360 static TAILQ_HEAD(, mount_softdeps) softdepmounts;
1363 * This function cleans the worklist for a filesystem.
1364 * Each filesystem running with soft dependencies gets its own
1365 * thread to run in this function. The thread is started up in
1366 * softdep_mount and shutdown in softdep_unmount. They show up
1367 * as part of the kernel "bufdaemon" process whose process
1368 * entry is available in bufdaemonproc.
1370 static int searchfailed;
1371 extern struct proc *bufdaemonproc;
1378 struct ufsmount *ump;
1381 td->td_pflags |= TDP_NORUNNINGBUF;
1382 mp = (struct mount *)addr;
1384 atomic_add_int(&stat_flush_threads, 1);
1386 ump->softdep_flags &= ~FLUSH_STARTING;
1387 wakeup(&ump->softdep_flushtd);
1389 if (print_threads) {
1390 if (stat_flush_threads == 1)
1391 printf("Running %s at pid %d\n", bufdaemonproc->p_comm,
1392 bufdaemonproc->p_pid);
1393 printf("Start thread %s\n", td->td_name);
1396 while (softdep_process_worklist(mp, 0) > 0 ||
1398 VFSTOUFS(mp)->softdep_jblocks->jb_suspended))
1399 kthread_suspend_check();
1401 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1402 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM,
1404 ump->softdep_flags &= ~FLUSH_CLEANUP;
1406 * Check to see if we are done and need to exit.
1408 if ((ump->softdep_flags & FLUSH_EXIT) == 0) {
1412 ump->softdep_flags &= ~FLUSH_EXIT;
1414 wakeup(&ump->softdep_flags);
1416 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups);
1417 atomic_subtract_int(&stat_flush_threads, 1);
1419 panic("kthread_exit failed\n");
1424 worklist_speedup(mp)
1427 struct ufsmount *ump;
1431 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1432 ump->softdep_flags |= FLUSH_CLEANUP;
1433 wakeup(&ump->softdep_flushtd);
1437 softdep_speedup(ump)
1438 struct ufsmount *ump;
1440 struct ufsmount *altump;
1441 struct mount_softdeps *sdp;
1444 worklist_speedup(ump->um_mountp);
1447 * If we have global shortages, then we need other
1448 * filesystems to help with the cleanup. Here we wakeup a
1449 * flusher thread for a filesystem that is over its fair
1450 * share of resources.
1452 if (req_clear_inodedeps || req_clear_remove) {
1453 ACQUIRE_GBLLOCK(&lk);
1454 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) {
1455 if ((altump = sdp->sd_ump) == ump)
1457 if (((req_clear_inodedeps &&
1458 altump->softdep_curdeps[D_INODEDEP] >
1459 max_softdeps / stat_flush_threads) ||
1460 (req_clear_remove &&
1461 altump->softdep_curdeps[D_DIRREM] >
1462 (max_softdeps / 2) / stat_flush_threads)) &&
1463 TRY_ACQUIRE_LOCK(altump))
1471 * Move to the end of the list so we pick a
1472 * different one on out next try.
1474 TAILQ_REMOVE(&softdepmounts, sdp, sd_next);
1475 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
1477 if ((altump->softdep_flags &
1478 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0)
1479 altump->softdep_flags |= FLUSH_CLEANUP;
1480 altump->um_softdep->sd_cleanups++;
1481 wakeup(&altump->softdep_flushtd);
1485 return (speedup_syncer());
1489 * Add an item to the end of the work queue.
1490 * This routine requires that the lock be held.
1491 * This is the only routine that adds items to the list.
1492 * The following routine is the only one that removes items
1493 * and does so in order from first to last.
1496 #define WK_HEAD 0x0001 /* Add to HEAD. */
1497 #define WK_NODELAY 0x0002 /* Process immediately. */
1500 add_to_worklist(wk, flags)
1501 struct worklist *wk;
1504 struct ufsmount *ump;
1506 ump = VFSTOUFS(wk->wk_mp);
1508 if (wk->wk_state & ONWORKLIST)
1509 panic("add_to_worklist: %s(0x%X) already on list",
1510 TYPENAME(wk->wk_type), wk->wk_state);
1511 wk->wk_state |= ONWORKLIST;
1512 if (ump->softdep_on_worklist == 0) {
1513 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1514 ump->softdep_worklist_tail = wk;
1515 } else if (flags & WK_HEAD) {
1516 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
1518 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
1519 ump->softdep_worklist_tail = wk;
1521 ump->softdep_on_worklist += 1;
1522 if (flags & WK_NODELAY)
1523 worklist_speedup(wk->wk_mp);
1527 * Remove the item to be processed. If we are removing the last
1528 * item on the list, we need to recalculate the tail pointer.
1531 remove_from_worklist(wk)
1532 struct worklist *wk;
1534 struct ufsmount *ump;
1536 ump = VFSTOUFS(wk->wk_mp);
1537 WORKLIST_REMOVE(wk);
1538 if (ump->softdep_worklist_tail == wk)
1539 ump->softdep_worklist_tail =
1540 (struct worklist *)wk->wk_list.le_prev;
1541 ump->softdep_on_worklist -= 1;
1546 struct worklist *wk;
1548 if (wk->wk_state & IOWAITING) {
1549 wk->wk_state &= ~IOWAITING;
1555 wait_worklist(wk, wmesg)
1556 struct worklist *wk;
1559 struct ufsmount *ump;
1561 ump = VFSTOUFS(wk->wk_mp);
1562 wk->wk_state |= IOWAITING;
1563 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0);
1567 * Process that runs once per second to handle items in the background queue.
1569 * Note that we ensure that everything is done in the order in which they
1570 * appear in the queue. The code below depends on this property to ensure
1571 * that blocks of a file are freed before the inode itself is freed. This
1572 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
1573 * until all the old ones have been purged from the dependency lists.
1576 softdep_process_worklist(mp, full)
1581 struct ufsmount *ump;
1584 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
1585 if (MOUNTEDSOFTDEP(mp) == 0)
1590 starttime = time_second;
1591 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0);
1592 check_clear_deps(mp);
1593 while (ump->softdep_on_worklist > 0) {
1594 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0)
1598 check_clear_deps(mp);
1600 * We do not generally want to stop for buffer space, but if
1601 * we are really being a buffer hog, we will stop and wait.
1603 if (should_yield()) {
1605 kern_yield(PRI_USER);
1610 * Never allow processing to run for more than one
1611 * second. This gives the syncer thread the opportunity
1612 * to pause if appropriate.
1614 if (!full && starttime != time_second)
1618 journal_unsuspend(ump);
1624 * Process all removes associated with a vnode if we are running out of
1625 * journal space. Any other process which attempts to flush these will
1626 * be unable as we have the vnodes locked.
1632 struct inodedep *inodedep;
1633 struct dirrem *dirrem;
1634 struct ufsmount *ump;
1641 inum = VTOI(vp)->i_number;
1644 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1646 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) {
1648 * If another thread is trying to lock this vnode
1649 * it will fail but we must wait for it to do so
1650 * before we can proceed.
1652 if (dirrem->dm_state & INPROGRESS) {
1653 wait_worklist(&dirrem->dm_list, "pwrwait");
1656 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) ==
1657 (COMPLETE | ONWORKLIST))
1662 remove_from_worklist(&dirrem->dm_list);
1664 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1665 panic("process_removes: suspended filesystem");
1666 handle_workitem_remove(dirrem, 0);
1667 vn_finished_secondary_write(mp);
1673 * Process all truncations associated with a vnode if we are running out
1674 * of journal space. This is called when the vnode lock is already held
1675 * and no other process can clear the truncation. This function returns
1676 * a value greater than zero if it did any work.
1679 process_truncates(vp)
1682 struct inodedep *inodedep;
1683 struct freeblks *freeblks;
1684 struct ufsmount *ump;
1692 inum = VTOI(vp)->i_number;
1694 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0)
1697 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) {
1698 /* Journal entries not yet written. */
1699 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) {
1701 &freeblks->fb_jblkdephd)->jb_list,
1705 /* Another thread is executing this item. */
1706 if (freeblks->fb_state & INPROGRESS) {
1707 wait_worklist(&freeblks->fb_list, "ptrwait");
1710 /* Freeblks is waiting on a inode write. */
1711 if ((freeblks->fb_state & COMPLETE) == 0) {
1717 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) ==
1718 (ALLCOMPLETE | ONWORKLIST)) {
1719 remove_from_worklist(&freeblks->fb_list);
1720 freeblks->fb_state |= INPROGRESS;
1722 if (vn_start_secondary_write(NULL, &mp,
1724 panic("process_truncates: "
1725 "suspended filesystem");
1726 handle_workitem_freeblocks(freeblks, 0);
1727 vn_finished_secondary_write(mp);
1731 if (freeblks->fb_cgwait)
1736 sync_cgs(mp, MNT_WAIT);
1737 ffs_sync_snap(mp, MNT_WAIT);
1741 if (freeblks == NULL)
1748 * Process one item on the worklist.
1751 process_worklist_item(mp, target, flags)
1756 struct worklist sentinel;
1757 struct worklist *wk;
1758 struct ufsmount *ump;
1762 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
1764 * If we are being called because of a process doing a
1765 * copy-on-write, then it is not safe to write as we may
1766 * recurse into the copy-on-write routine.
1768 if (curthread->td_pflags & TDP_COWINPROGRESS)
1770 PHOLD(curproc); /* Don't let the stack go away. */
1774 sentinel.wk_mp = NULL;
1775 sentinel.wk_type = D_SENTINEL;
1776 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list);
1777 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL;
1778 wk = LIST_NEXT(&sentinel, wk_list)) {
1779 if (wk->wk_type == D_SENTINEL) {
1780 LIST_REMOVE(&sentinel, wk_list);
1781 LIST_INSERT_AFTER(wk, &sentinel, wk_list);
1784 if (wk->wk_state & INPROGRESS)
1785 panic("process_worklist_item: %p already in progress.",
1787 wk->wk_state |= INPROGRESS;
1788 remove_from_worklist(wk);
1790 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
1791 panic("process_worklist_item: suspended filesystem");
1792 switch (wk->wk_type) {
1794 /* removal of a directory entry */
1795 error = handle_workitem_remove(WK_DIRREM(wk), flags);
1799 /* releasing blocks and/or fragments from a file */
1800 error = handle_workitem_freeblocks(WK_FREEBLKS(wk),
1805 /* releasing a fragment when replaced as a file grows */
1806 handle_workitem_freefrag(WK_FREEFRAG(wk));
1811 /* releasing an inode when its link count drops to 0 */
1812 handle_workitem_freefile(WK_FREEFILE(wk));
1817 panic("%s_process_worklist: Unknown type %s",
1818 "softdep", TYPENAME(wk->wk_type));
1821 vn_finished_secondary_write(mp);
1824 if (++matchcnt == target)
1829 * We have to retry the worklist item later. Wake up any
1830 * waiters who may be able to complete it immediately and
1831 * add the item back to the head so we don't try to execute
1834 wk->wk_state &= ~INPROGRESS;
1836 add_to_worklist(wk, WK_HEAD);
1838 LIST_REMOVE(&sentinel, wk_list);
1839 /* Sentinal could've become the tail from remove_from_worklist. */
1840 if (ump->softdep_worklist_tail == &sentinel)
1841 ump->softdep_worklist_tail =
1842 (struct worklist *)sentinel.wk_list.le_prev;
1848 * Move dependencies from one buffer to another.
1851 softdep_move_dependencies(oldbp, newbp)
1855 struct worklist *wk, *wktail;
1856 struct ufsmount *ump;
1859 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL)
1861 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
1862 ("softdep_move_dependencies called on non-softdep filesystem"));
1865 ump = VFSTOUFS(wk->wk_mp);
1867 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
1868 LIST_REMOVE(wk, wk_list);
1869 if (wk->wk_type == D_BMSAFEMAP &&
1870 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp))
1873 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1875 LIST_INSERT_AFTER(wktail, wk, wk_list);
1884 * Purge the work list of all items associated with a particular mount point.
1887 softdep_flushworklist(oldmnt, countp, td)
1888 struct mount *oldmnt;
1892 struct vnode *devvp;
1893 struct ufsmount *ump;
1897 * Alternately flush the block device associated with the mount
1898 * point and process any dependencies that the flushing
1899 * creates. We continue until no more worklist dependencies
1904 ump = VFSTOUFS(oldmnt);
1905 devvp = ump->um_devvp;
1906 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1908 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1909 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1910 VOP_UNLOCK(devvp, 0);
1917 #define SU_WAITIDLE_RETRIES 20
1919 softdep_waitidle(struct mount *mp, int flags __unused)
1921 struct ufsmount *ump;
1922 struct vnode *devvp;
1927 devvp = ump->um_devvp;
1931 for (i = 0; i < SU_WAITIDLE_RETRIES && ump->softdep_deps != 0; i++) {
1932 ump->softdep_req = 1;
1933 KASSERT((flags & FORCECLOSE) == 0 ||
1934 ump->softdep_on_worklist == 0,
1935 ("softdep_waitidle: work added after flush"));
1936 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM | PDROP,
1937 "softdeps", 10 * hz);
1938 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1939 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1940 VOP_UNLOCK(devvp, 0);
1945 ump->softdep_req = 0;
1946 if (i == SU_WAITIDLE_RETRIES && error == 0 && ump->softdep_deps != 0) {
1948 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1956 * Flush all vnodes and worklist items associated with a specified mount point.
1959 softdep_flushfiles(oldmnt, flags, td)
1960 struct mount *oldmnt;
1965 struct ufsmount *ump;
1968 int error, early, depcount, loopcnt, retry_flush_count, retry;
1971 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0,
1972 ("softdep_flushfiles called on non-softdep filesystem"));
1974 retry_flush_count = 3;
1979 * Alternately flush the vnodes associated with the mount
1980 * point and process any dependencies that the flushing
1981 * creates. In theory, this loop can happen at most twice,
1982 * but we give it a few extra just to be sure.
1984 for (; loopcnt > 0; loopcnt--) {
1986 * Do another flush in case any vnodes were brought in
1987 * as part of the cleanup operations.
1989 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag &
1990 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH;
1991 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0)
1993 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
1998 * If we are unmounting then it is an error to fail. If we
1999 * are simply trying to downgrade to read-only, then filesystem
2000 * activity can keep us busy forever, so we just fail with EBUSY.
2003 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
2004 panic("softdep_flushfiles: looping");
2008 error = softdep_waitidle(oldmnt, flags);
2010 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
2013 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0,
2014 ("softdep_flushfiles: !MNTK_NOINSMNTQ"));
2015 morework = oldmnt->mnt_nvnodelistsize > 0;
2017 ump = VFSTOUFS(oldmnt);
2019 for (i = 0; i < MAXQUOTAS; i++) {
2020 if (ump->um_quotas[i] != NULLVP)
2026 if (--retry_flush_count > 0) {
2032 MNT_IUNLOCK(oldmnt);
2041 * Structure hashing.
2043 * There are four types of structures that can be looked up:
2044 * 1) pagedep structures identified by mount point, inode number,
2045 * and logical block.
2046 * 2) inodedep structures identified by mount point and inode number.
2047 * 3) newblk structures identified by mount point and
2048 * physical block number.
2049 * 4) bmsafemap structures identified by mount point and
2050 * cylinder group number.
2052 * The "pagedep" and "inodedep" dependency structures are hashed
2053 * separately from the file blocks and inodes to which they correspond.
2054 * This separation helps when the in-memory copy of an inode or
2055 * file block must be replaced. It also obviates the need to access
2056 * an inode or file page when simply updating (or de-allocating)
2057 * dependency structures. Lookup of newblk structures is needed to
2058 * find newly allocated blocks when trying to associate them with
2059 * their allocdirect or allocindir structure.
2061 * The lookup routines optionally create and hash a new instance when
2062 * an existing entry is not found. The bmsafemap lookup routine always
2063 * allocates a new structure if an existing one is not found.
2065 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
2068 * Structures and routines associated with pagedep caching.
2070 #define PAGEDEP_HASH(ump, inum, lbn) \
2071 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size])
2074 pagedep_find(pagedephd, ino, lbn, pagedeppp)
2075 struct pagedep_hashhead *pagedephd;
2078 struct pagedep **pagedeppp;
2080 struct pagedep *pagedep;
2082 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
2083 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) {
2084 *pagedeppp = pagedep;
2092 * Look up a pagedep. Return 1 if found, 0 otherwise.
2093 * If not found, allocate if DEPALLOC flag is passed.
2094 * Found or allocated entry is returned in pagedeppp.
2095 * This routine must be called with splbio interrupts blocked.
2098 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp)
2104 struct pagedep **pagedeppp;
2106 struct pagedep *pagedep;
2107 struct pagedep_hashhead *pagedephd;
2108 struct worklist *wk;
2109 struct ufsmount *ump;
2116 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2117 if (wk->wk_type == D_PAGEDEP) {
2118 *pagedeppp = WK_PAGEDEP(wk);
2123 pagedephd = PAGEDEP_HASH(ump, ino, lbn);
2124 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2126 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp)
2127 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list);
2130 if ((flags & DEPALLOC) == 0)
2133 pagedep = malloc(sizeof(struct pagedep),
2134 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
2135 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
2137 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp);
2140 * This should never happen since we only create pagedeps
2141 * with the vnode lock held. Could be an assert.
2143 WORKITEM_FREE(pagedep, D_PAGEDEP);
2146 pagedep->pd_ino = ino;
2147 pagedep->pd_lbn = lbn;
2148 LIST_INIT(&pagedep->pd_dirremhd);
2149 LIST_INIT(&pagedep->pd_pendinghd);
2150 for (i = 0; i < DAHASHSZ; i++)
2151 LIST_INIT(&pagedep->pd_diraddhd[i]);
2152 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
2153 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2154 *pagedeppp = pagedep;
2159 * Structures and routines associated with inodedep caching.
2161 #define INODEDEP_HASH(ump, inum) \
2162 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size])
2165 inodedep_find(inodedephd, inum, inodedeppp)
2166 struct inodedep_hashhead *inodedephd;
2168 struct inodedep **inodedeppp;
2170 struct inodedep *inodedep;
2172 LIST_FOREACH(inodedep, inodedephd, id_hash)
2173 if (inum == inodedep->id_ino)
2176 *inodedeppp = inodedep;
2184 * Look up an inodedep. Return 1 if found, 0 if not found.
2185 * If not found, allocate if DEPALLOC flag is passed.
2186 * Found or allocated entry is returned in inodedeppp.
2187 * This routine must be called with splbio interrupts blocked.
2190 inodedep_lookup(mp, inum, flags, inodedeppp)
2194 struct inodedep **inodedeppp;
2196 struct inodedep *inodedep;
2197 struct inodedep_hashhead *inodedephd;
2198 struct ufsmount *ump;
2204 inodedephd = INODEDEP_HASH(ump, inum);
2206 if (inodedep_find(inodedephd, inum, inodedeppp))
2208 if ((flags & DEPALLOC) == 0)
2211 * If the system is over its limit and our filesystem is
2212 * responsible for more than our share of that usage and
2213 * we are not in a rush, request some inodedep cleanup.
2215 if (softdep_excess_inodes(ump))
2216 schedule_cleanup(mp);
2219 inodedep = malloc(sizeof(struct inodedep),
2220 M_INODEDEP, M_SOFTDEP_FLAGS);
2221 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
2223 if (inodedep_find(inodedephd, inum, inodedeppp)) {
2224 WORKITEM_FREE(inodedep, D_INODEDEP);
2227 inodedep->id_fs = fs;
2228 inodedep->id_ino = inum;
2229 inodedep->id_state = ALLCOMPLETE;
2230 inodedep->id_nlinkdelta = 0;
2231 inodedep->id_savedino1 = NULL;
2232 inodedep->id_savedsize = -1;
2233 inodedep->id_savedextsize = -1;
2234 inodedep->id_savednlink = -1;
2235 inodedep->id_bmsafemap = NULL;
2236 inodedep->id_mkdiradd = NULL;
2237 LIST_INIT(&inodedep->id_dirremhd);
2238 LIST_INIT(&inodedep->id_pendinghd);
2239 LIST_INIT(&inodedep->id_inowait);
2240 LIST_INIT(&inodedep->id_bufwait);
2241 TAILQ_INIT(&inodedep->id_inoreflst);
2242 TAILQ_INIT(&inodedep->id_inoupdt);
2243 TAILQ_INIT(&inodedep->id_newinoupdt);
2244 TAILQ_INIT(&inodedep->id_extupdt);
2245 TAILQ_INIT(&inodedep->id_newextupdt);
2246 TAILQ_INIT(&inodedep->id_freeblklst);
2247 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
2248 *inodedeppp = inodedep;
2253 * Structures and routines associated with newblk caching.
2255 #define NEWBLK_HASH(ump, inum) \
2256 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size])
2259 newblk_find(newblkhd, newblkno, flags, newblkpp)
2260 struct newblk_hashhead *newblkhd;
2261 ufs2_daddr_t newblkno;
2263 struct newblk **newblkpp;
2265 struct newblk *newblk;
2267 LIST_FOREACH(newblk, newblkhd, nb_hash) {
2268 if (newblkno != newblk->nb_newblkno)
2271 * If we're creating a new dependency don't match those that
2272 * have already been converted to allocdirects. This is for
2275 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK)
2288 * Look up a newblk. Return 1 if found, 0 if not found.
2289 * If not found, allocate if DEPALLOC flag is passed.
2290 * Found or allocated entry is returned in newblkpp.
2293 newblk_lookup(mp, newblkno, flags, newblkpp)
2295 ufs2_daddr_t newblkno;
2297 struct newblk **newblkpp;
2299 struct newblk *newblk;
2300 struct newblk_hashhead *newblkhd;
2301 struct ufsmount *ump;
2305 newblkhd = NEWBLK_HASH(ump, newblkno);
2306 if (newblk_find(newblkhd, newblkno, flags, newblkpp))
2308 if ((flags & DEPALLOC) == 0)
2311 newblk = malloc(sizeof(union allblk), M_NEWBLK,
2312 M_SOFTDEP_FLAGS | M_ZERO);
2313 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp);
2315 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) {
2316 WORKITEM_FREE(newblk, D_NEWBLK);
2319 newblk->nb_freefrag = NULL;
2320 LIST_INIT(&newblk->nb_indirdeps);
2321 LIST_INIT(&newblk->nb_newdirblk);
2322 LIST_INIT(&newblk->nb_jwork);
2323 newblk->nb_state = ATTACHED;
2324 newblk->nb_newblkno = newblkno;
2325 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
2331 * Structures and routines associated with freed indirect block caching.
2333 #define INDIR_HASH(ump, blkno) \
2334 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size])
2337 * Lookup an indirect block in the indir hash table. The freework is
2338 * removed and potentially freed. The caller must do a blocking journal
2339 * write before writing to the blkno.
2342 indirblk_lookup(mp, blkno)
2346 struct freework *freework;
2347 struct indir_hashhead *wkhd;
2348 struct ufsmount *ump;
2351 wkhd = INDIR_HASH(ump, blkno);
2352 TAILQ_FOREACH(freework, wkhd, fw_next) {
2353 if (freework->fw_blkno != blkno)
2355 indirblk_remove(freework);
2362 * Insert an indirect block represented by freework into the indirblk
2363 * hash table so that it may prevent the block from being re-used prior
2364 * to the journal being written.
2367 indirblk_insert(freework)
2368 struct freework *freework;
2370 struct jblocks *jblocks;
2372 struct ufsmount *ump;
2374 ump = VFSTOUFS(freework->fw_list.wk_mp);
2375 jblocks = ump->softdep_jblocks;
2376 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst);
2380 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs);
2381 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework,
2383 freework->fw_state &= ~DEPCOMPLETE;
2387 indirblk_remove(freework)
2388 struct freework *freework;
2390 struct ufsmount *ump;
2392 ump = VFSTOUFS(freework->fw_list.wk_mp);
2393 LIST_REMOVE(freework, fw_segs);
2394 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next);
2395 freework->fw_state |= DEPCOMPLETE;
2396 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
2397 WORKITEM_FREE(freework, D_FREEWORK);
2401 * Executed during filesystem system initialization before
2402 * mounting any filesystems.
2405 softdep_initialize()
2408 TAILQ_INIT(&softdepmounts);
2409 max_softdeps = desiredvnodes * 4;
2411 /* initialise bioops hack */
2412 bioops.io_start = softdep_disk_io_initiation;
2413 bioops.io_complete = softdep_disk_write_complete;
2414 bioops.io_deallocate = softdep_deallocate_dependencies;
2415 bioops.io_countdeps = softdep_count_dependencies;
2416 softdep_ast_cleanup = softdep_ast_cleanup_proc;
2418 /* Initialize the callout with an mtx. */
2419 callout_init_mtx(&softdep_callout, &lk, 0);
2423 * Executed after all filesystems have been unmounted during
2424 * filesystem module unload.
2427 softdep_uninitialize()
2430 /* clear bioops hack */
2431 bioops.io_start = NULL;
2432 bioops.io_complete = NULL;
2433 bioops.io_deallocate = NULL;
2434 bioops.io_countdeps = NULL;
2435 softdep_ast_cleanup = NULL;
2437 callout_drain(&softdep_callout);
2441 * Called at mount time to notify the dependency code that a
2442 * filesystem wishes to use it.
2445 softdep_mount(devvp, mp, fs, cred)
2446 struct vnode *devvp;
2451 struct csum_total cstotal;
2452 struct mount_softdeps *sdp;
2453 struct ufsmount *ump;
2458 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA,
2461 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
2462 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
2463 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
2464 MNTK_SOFTDEP | MNTK_NOASYNC;
2467 ump->um_softdep = sdp;
2469 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock");
2471 LIST_INIT(&ump->softdep_workitem_pending);
2472 LIST_INIT(&ump->softdep_journal_pending);
2473 TAILQ_INIT(&ump->softdep_unlinked);
2474 LIST_INIT(&ump->softdep_dirtycg);
2475 ump->softdep_worklist_tail = NULL;
2476 ump->softdep_on_worklist = 0;
2477 ump->softdep_deps = 0;
2478 LIST_INIT(&ump->softdep_mkdirlisthd);
2479 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
2480 &ump->pagedep_hash_size);
2481 ump->pagedep_nextclean = 0;
2482 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP,
2483 &ump->inodedep_hash_size);
2484 ump->inodedep_nextclean = 0;
2485 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK,
2486 &ump->newblk_hash_size);
2487 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP,
2488 &ump->bmsafemap_hash_size);
2489 i = 1 << (ffs(desiredvnodes / 10) - 1);
2490 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead),
2491 M_FREEWORK, M_WAITOK);
2492 ump->indir_hash_size = i - 1;
2493 for (i = 0; i <= ump->indir_hash_size; i++)
2494 TAILQ_INIT(&ump->indir_hashtbl[i]);
2495 ACQUIRE_GBLLOCK(&lk);
2496 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next);
2498 if ((fs->fs_flags & FS_SUJ) &&
2499 (error = journal_mount(mp, fs, cred)) != 0) {
2500 printf("Failed to start journal: %d\n", error);
2501 softdep_unmount(mp);
2505 * Start our flushing thread in the bufdaemon process.
2508 ump->softdep_flags |= FLUSH_STARTING;
2510 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc,
2511 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker",
2512 mp->mnt_stat.f_mntonname);
2514 while ((ump->softdep_flags & FLUSH_STARTING) != 0) {
2515 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, "sdstart",
2520 * When doing soft updates, the counters in the
2521 * superblock may have gotten out of sync. Recomputation
2522 * can take a long time and can be deferred for background
2523 * fsck. However, the old behavior of scanning the cylinder
2524 * groups and recalculating them at mount time is available
2525 * by setting vfs.ffs.compute_summary_at_mount to one.
2527 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
2529 bzero(&cstotal, sizeof cstotal);
2530 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
2531 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
2532 fs->fs_cgsize, cred, &bp)) != 0) {
2534 softdep_unmount(mp);
2537 cgp = (struct cg *)bp->b_data;
2538 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
2539 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
2540 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
2541 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
2542 fs->fs_cs(fs, cyl) = cgp->cg_cs;
2546 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
2547 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
2549 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
2557 struct ufsmount *ump;
2562 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
2563 ("softdep_unmount called on non-softdep filesystem"));
2566 mp->mnt_flag &= ~MNT_SOFTDEP;
2567 if (MOUNTEDSUJ(mp) == 0) {
2570 mp->mnt_flag &= ~MNT_SUJ;
2572 journal_unmount(ump);
2575 * Shut down our flushing thread. Check for NULL is if
2576 * softdep_mount errors out before the thread has been created.
2578 if (ump->softdep_flushtd != NULL) {
2580 ump->softdep_flags |= FLUSH_EXIT;
2581 wakeup(&ump->softdep_flushtd);
2582 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP,
2584 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0,
2585 ("Thread shutdown failed"));
2588 * Free up our resources.
2590 ACQUIRE_GBLLOCK(&lk);
2591 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next);
2593 rw_destroy(LOCK_PTR(ump));
2594 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size);
2595 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size);
2596 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size);
2597 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP,
2598 ump->bmsafemap_hash_size);
2599 free(ump->indir_hashtbl, M_FREEWORK);
2601 for (i = 0; i <= D_LAST; i++)
2602 KASSERT(ump->softdep_curdeps[i] == 0,
2603 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt,
2604 TYPENAME(i), ump->softdep_curdeps[i]));
2606 free(ump->um_softdep, M_MOUNTDATA);
2609 static struct jblocks *
2610 jblocks_create(void)
2612 struct jblocks *jblocks;
2614 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO);
2615 TAILQ_INIT(&jblocks->jb_segs);
2616 jblocks->jb_avail = 10;
2617 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2618 M_JBLOCKS, M_WAITOK | M_ZERO);
2624 jblocks_alloc(jblocks, bytes, actual)
2625 struct jblocks *jblocks;
2630 struct jextent *jext;
2634 blocks = bytes / DEV_BSIZE;
2635 jext = &jblocks->jb_extent[jblocks->jb_head];
2636 freecnt = jext->je_blocks - jblocks->jb_off;
2638 jblocks->jb_off = 0;
2639 if (++jblocks->jb_head > jblocks->jb_used)
2640 jblocks->jb_head = 0;
2641 jext = &jblocks->jb_extent[jblocks->jb_head];
2642 freecnt = jext->je_blocks;
2644 if (freecnt > blocks)
2646 *actual = freecnt * DEV_BSIZE;
2647 daddr = jext->je_daddr + jblocks->jb_off;
2648 jblocks->jb_off += freecnt;
2649 jblocks->jb_free -= freecnt;
2655 jblocks_free(jblocks, mp, bytes)
2656 struct jblocks *jblocks;
2661 LOCK_OWNED(VFSTOUFS(mp));
2662 jblocks->jb_free += bytes / DEV_BSIZE;
2663 if (jblocks->jb_suspended)
2664 worklist_speedup(mp);
2669 jblocks_destroy(jblocks)
2670 struct jblocks *jblocks;
2673 if (jblocks->jb_extent)
2674 free(jblocks->jb_extent, M_JBLOCKS);
2675 free(jblocks, M_JBLOCKS);
2679 jblocks_add(jblocks, daddr, blocks)
2680 struct jblocks *jblocks;
2684 struct jextent *jext;
2686 jblocks->jb_blocks += blocks;
2687 jblocks->jb_free += blocks;
2688 jext = &jblocks->jb_extent[jblocks->jb_used];
2689 /* Adding the first block. */
2690 if (jext->je_daddr == 0) {
2691 jext->je_daddr = daddr;
2692 jext->je_blocks = blocks;
2695 /* Extending the last extent. */
2696 if (jext->je_daddr + jext->je_blocks == daddr) {
2697 jext->je_blocks += blocks;
2700 /* Adding a new extent. */
2701 if (++jblocks->jb_used == jblocks->jb_avail) {
2702 jblocks->jb_avail *= 2;
2703 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail,
2704 M_JBLOCKS, M_WAITOK | M_ZERO);
2705 memcpy(jext, jblocks->jb_extent,
2706 sizeof(struct jextent) * jblocks->jb_used);
2707 free(jblocks->jb_extent, M_JBLOCKS);
2708 jblocks->jb_extent = jext;
2710 jext = &jblocks->jb_extent[jblocks->jb_used];
2711 jext->je_daddr = daddr;
2712 jext->je_blocks = blocks;
2717 softdep_journal_lookup(mp, vpp)
2721 struct componentname cnp;
2726 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp);
2729 bzero(&cnp, sizeof(cnp));
2730 cnp.cn_nameiop = LOOKUP;
2731 cnp.cn_flags = ISLASTCN;
2732 cnp.cn_thread = curthread;
2733 cnp.cn_cred = curthread->td_ucred;
2734 cnp.cn_pnbuf = SUJ_FILE;
2735 cnp.cn_nameptr = SUJ_FILE;
2736 cnp.cn_namelen = strlen(SUJ_FILE);
2737 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal);
2741 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp);
2746 * Open and verify the journal file.
2749 journal_mount(mp, fs, cred)
2754 struct jblocks *jblocks;
2755 struct ufsmount *ump;
2764 ump->softdep_journal_tail = NULL;
2765 ump->softdep_on_journal = 0;
2766 ump->softdep_accdeps = 0;
2767 ump->softdep_req = 0;
2768 ump->softdep_jblocks = NULL;
2769 error = softdep_journal_lookup(mp, &vp);
2771 printf("Failed to find journal. Use tunefs to create one\n");
2775 if (ip->i_size < SUJ_MIN) {
2779 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */
2780 jblocks = jblocks_create();
2781 for (i = 0; i < bcount; i++) {
2782 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL);
2785 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag));
2788 jblocks_destroy(jblocks);
2791 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */
2792 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */
2793 ump->softdep_jblocks = jblocks;
2797 mp->mnt_flag |= MNT_SUJ;
2798 mp->mnt_flag &= ~MNT_SOFTDEP;
2801 * Only validate the journal contents if the
2802 * filesystem is clean, otherwise we write the logs
2803 * but they'll never be used. If the filesystem was
2804 * still dirty when we mounted it the journal is
2805 * invalid and a new journal can only be valid if it
2806 * starts from a clean mount.
2809 DIP_SET(ip, i_modrev, fs->fs_mtime);
2810 ip->i_flags |= IN_MODIFIED;
2819 journal_unmount(ump)
2820 struct ufsmount *ump;
2823 if (ump->softdep_jblocks)
2824 jblocks_destroy(ump->softdep_jblocks);
2825 ump->softdep_jblocks = NULL;
2829 * Called when a journal record is ready to be written. Space is allocated
2830 * and the journal entry is created when the journal is flushed to stable
2835 struct worklist *wk;
2837 struct ufsmount *ump;
2839 ump = VFSTOUFS(wk->wk_mp);
2841 if (wk->wk_state & ONWORKLIST)
2842 panic("add_to_journal: %s(0x%X) already on list",
2843 TYPENAME(wk->wk_type), wk->wk_state);
2844 wk->wk_state |= ONWORKLIST | DEPCOMPLETE;
2845 if (LIST_EMPTY(&ump->softdep_journal_pending)) {
2846 ump->softdep_jblocks->jb_age = ticks;
2847 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list);
2849 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list);
2850 ump->softdep_journal_tail = wk;
2851 ump->softdep_on_journal += 1;
2855 * Remove an arbitrary item for the journal worklist maintain the tail
2856 * pointer. This happens when a new operation obviates the need to
2857 * journal an old operation.
2860 remove_from_journal(wk)
2861 struct worklist *wk;
2863 struct ufsmount *ump;
2865 ump = VFSTOUFS(wk->wk_mp);
2869 struct worklist *wkn;
2871 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list)
2875 panic("remove_from_journal: %p is not in journal", wk);
2879 * We emulate a TAILQ to save space in most structures which do not
2880 * require TAILQ semantics. Here we must update the tail position
2881 * when removing the tail which is not the final entry. This works
2882 * only if the worklist linkage are at the beginning of the structure.
2884 if (ump->softdep_journal_tail == wk)
2885 ump->softdep_journal_tail =
2886 (struct worklist *)wk->wk_list.le_prev;
2888 WORKLIST_REMOVE(wk);
2889 ump->softdep_on_journal -= 1;
2893 * Check for journal space as well as dependency limits so the prelink
2894 * code can throttle both journaled and non-journaled filesystems.
2895 * Threshold is 0 for low and 1 for min.
2898 journal_space(ump, thresh)
2899 struct ufsmount *ump;
2902 struct jblocks *jblocks;
2905 jblocks = ump->softdep_jblocks;
2906 if (jblocks == NULL)
2909 * We use a tighter restriction here to prevent request_cleanup()
2910 * running in threads from running into locks we currently hold.
2911 * We have to be over the limit and our filesystem has to be
2912 * responsible for more than our share of that usage.
2914 limit = (max_softdeps / 10) * 9;
2915 if (dep_current[D_INODEDEP] > limit &&
2916 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads)
2919 thresh = jblocks->jb_min;
2921 thresh = jblocks->jb_low;
2922 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE;
2923 avail = jblocks->jb_free - avail;
2925 return (avail > thresh);
2929 journal_suspend(ump)
2930 struct ufsmount *ump;
2932 struct jblocks *jblocks;
2936 jblocks = ump->softdep_jblocks;
2938 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
2940 mp->mnt_kern_flag |= MNTK_SUSPEND;
2941 mp->mnt_susp_owner = ump->softdep_flushtd;
2943 jblocks->jb_suspended = 1;
2948 journal_unsuspend(struct ufsmount *ump)
2950 struct jblocks *jblocks;
2954 jblocks = ump->softdep_jblocks;
2956 if (jblocks != NULL && jblocks->jb_suspended &&
2957 journal_space(ump, jblocks->jb_min)) {
2958 jblocks->jb_suspended = 0;
2960 mp->mnt_susp_owner = curthread;
2961 vfs_write_resume(mp, 0);
2969 * Called before any allocation function to be certain that there is
2970 * sufficient space in the journal prior to creating any new records.
2971 * Since in the case of block allocation we may have multiple locked
2972 * buffers at the time of the actual allocation we can not block
2973 * when the journal records are created. Doing so would create a deadlock
2974 * if any of these buffers needed to be flushed to reclaim space. Instead
2975 * we require a sufficiently large amount of available space such that
2976 * each thread in the system could have passed this allocation check and
2977 * still have sufficient free space. With 20% of a minimum journal size
2978 * of 1MB we have 6553 records available.
2981 softdep_prealloc(vp, waitok)
2985 struct ufsmount *ump;
2987 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
2988 ("softdep_prealloc called on non-softdep filesystem"));
2990 * Nothing to do if we are not running journaled soft updates.
2991 * If we currently hold the snapshot lock, we must avoid handling
2992 * other resources that could cause deadlock.
2994 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp)))
2996 ump = VFSTOUFS(vp->v_mount);
2998 if (journal_space(ump, 0)) {
3004 if (waitok == MNT_NOWAIT)
3007 * Attempt to sync this vnode once to flush any journal
3008 * work attached to it.
3010 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0)
3011 ffs_syncvnode(vp, waitok, 0);
3013 process_removes(vp);
3014 process_truncates(vp);
3015 if (journal_space(ump, 0) == 0) {
3016 softdep_speedup(ump);
3017 if (journal_space(ump, 1) == 0)
3018 journal_suspend(ump);
3026 * Before adjusting a link count on a vnode verify that we have sufficient
3027 * journal space. If not, process operations that depend on the currently
3028 * locked pair of vnodes to try to flush space as the syncer, buf daemon,
3029 * and softdep flush threads can not acquire these locks to reclaim space.
3032 softdep_prelink(dvp, vp)
3036 struct ufsmount *ump;
3038 ump = VFSTOUFS(dvp->v_mount);
3041 * Nothing to do if we have sufficient journal space.
3042 * If we currently hold the snapshot lock, we must avoid
3043 * handling other resources that could cause deadlock.
3045 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp))))
3050 ffs_syncvnode(vp, MNT_NOWAIT, 0);
3051 ffs_syncvnode(dvp, MNT_WAIT, 0);
3053 /* Process vp before dvp as it may create .. removes. */
3055 process_removes(vp);
3056 process_truncates(vp);
3058 process_removes(dvp);
3059 process_truncates(dvp);
3060 softdep_speedup(ump);
3061 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT);
3062 if (journal_space(ump, 0) == 0) {
3063 softdep_speedup(ump);
3064 if (journal_space(ump, 1) == 0)
3065 journal_suspend(ump);
3070 jseg_write(ump, jseg, data)
3071 struct ufsmount *ump;
3075 struct jsegrec *rec;
3077 rec = (struct jsegrec *)data;
3078 rec->jsr_seq = jseg->js_seq;
3079 rec->jsr_oldest = jseg->js_oldseq;
3080 rec->jsr_cnt = jseg->js_cnt;
3081 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize;
3083 rec->jsr_time = ump->um_fs->fs_mtime;
3087 inoref_write(inoref, jseg, rec)
3088 struct inoref *inoref;
3090 struct jrefrec *rec;
3093 inoref->if_jsegdep->jd_seg = jseg;
3094 rec->jr_ino = inoref->if_ino;
3095 rec->jr_parent = inoref->if_parent;
3096 rec->jr_nlink = inoref->if_nlink;
3097 rec->jr_mode = inoref->if_mode;
3098 rec->jr_diroff = inoref->if_diroff;
3102 jaddref_write(jaddref, jseg, data)
3103 struct jaddref *jaddref;
3107 struct jrefrec *rec;
3109 rec = (struct jrefrec *)data;
3110 rec->jr_op = JOP_ADDREF;
3111 inoref_write(&jaddref->ja_ref, jseg, rec);
3115 jremref_write(jremref, jseg, data)
3116 struct jremref *jremref;
3120 struct jrefrec *rec;
3122 rec = (struct jrefrec *)data;
3123 rec->jr_op = JOP_REMREF;
3124 inoref_write(&jremref->jr_ref, jseg, rec);
3128 jmvref_write(jmvref, jseg, data)
3129 struct jmvref *jmvref;
3135 rec = (struct jmvrec *)data;
3136 rec->jm_op = JOP_MVREF;
3137 rec->jm_ino = jmvref->jm_ino;
3138 rec->jm_parent = jmvref->jm_parent;
3139 rec->jm_oldoff = jmvref->jm_oldoff;
3140 rec->jm_newoff = jmvref->jm_newoff;
3144 jnewblk_write(jnewblk, jseg, data)
3145 struct jnewblk *jnewblk;
3149 struct jblkrec *rec;
3151 jnewblk->jn_jsegdep->jd_seg = jseg;
3152 rec = (struct jblkrec *)data;
3153 rec->jb_op = JOP_NEWBLK;
3154 rec->jb_ino = jnewblk->jn_ino;
3155 rec->jb_blkno = jnewblk->jn_blkno;
3156 rec->jb_lbn = jnewblk->jn_lbn;
3157 rec->jb_frags = jnewblk->jn_frags;
3158 rec->jb_oldfrags = jnewblk->jn_oldfrags;
3162 jfreeblk_write(jfreeblk, jseg, data)
3163 struct jfreeblk *jfreeblk;
3167 struct jblkrec *rec;
3169 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg;
3170 rec = (struct jblkrec *)data;
3171 rec->jb_op = JOP_FREEBLK;
3172 rec->jb_ino = jfreeblk->jf_ino;
3173 rec->jb_blkno = jfreeblk->jf_blkno;
3174 rec->jb_lbn = jfreeblk->jf_lbn;
3175 rec->jb_frags = jfreeblk->jf_frags;
3176 rec->jb_oldfrags = 0;
3180 jfreefrag_write(jfreefrag, jseg, data)
3181 struct jfreefrag *jfreefrag;
3185 struct jblkrec *rec;
3187 jfreefrag->fr_jsegdep->jd_seg = jseg;
3188 rec = (struct jblkrec *)data;
3189 rec->jb_op = JOP_FREEBLK;
3190 rec->jb_ino = jfreefrag->fr_ino;
3191 rec->jb_blkno = jfreefrag->fr_blkno;
3192 rec->jb_lbn = jfreefrag->fr_lbn;
3193 rec->jb_frags = jfreefrag->fr_frags;
3194 rec->jb_oldfrags = 0;
3198 jtrunc_write(jtrunc, jseg, data)
3199 struct jtrunc *jtrunc;
3203 struct jtrncrec *rec;
3205 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg;
3206 rec = (struct jtrncrec *)data;
3207 rec->jt_op = JOP_TRUNC;
3208 rec->jt_ino = jtrunc->jt_ino;
3209 rec->jt_size = jtrunc->jt_size;
3210 rec->jt_extsize = jtrunc->jt_extsize;
3214 jfsync_write(jfsync, jseg, data)
3215 struct jfsync *jfsync;
3219 struct jtrncrec *rec;
3221 rec = (struct jtrncrec *)data;
3222 rec->jt_op = JOP_SYNC;
3223 rec->jt_ino = jfsync->jfs_ino;
3224 rec->jt_size = jfsync->jfs_size;
3225 rec->jt_extsize = jfsync->jfs_extsize;
3229 softdep_flushjournal(mp)
3232 struct jblocks *jblocks;
3233 struct ufsmount *ump;
3235 if (MOUNTEDSUJ(mp) == 0)
3238 jblocks = ump->softdep_jblocks;
3240 while (ump->softdep_on_journal) {
3241 jblocks->jb_needseg = 1;
3242 softdep_process_journal(mp, NULL, MNT_WAIT);
3247 static void softdep_synchronize_completed(struct bio *);
3248 static void softdep_synchronize(struct bio *, struct ufsmount *, void *);
3251 softdep_synchronize_completed(bp)
3254 struct jseg *oldest;
3256 struct ufsmount *ump;
3259 * caller1 marks the last segment written before we issued the
3260 * synchronize cache.
3262 jseg = bp->bio_caller1;
3267 ump = VFSTOUFS(jseg->js_list.wk_mp);
3271 * Mark all the journal entries waiting on the synchronize cache
3272 * as completed so they may continue on.
3274 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) {
3275 jseg->js_state |= COMPLETE;
3277 jseg = TAILQ_PREV(jseg, jseglst, js_next);
3280 * Restart deferred journal entry processing from the oldest
3284 complete_jsegs(oldest);
3291 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering
3292 * barriers. The journal must be written prior to any blocks that depend
3293 * on it and the journal can not be released until the blocks have be
3294 * written. This code handles both barriers simultaneously.
3297 softdep_synchronize(bp, ump, caller1)
3299 struct ufsmount *ump;
3303 bp->bio_cmd = BIO_FLUSH;
3304 bp->bio_flags |= BIO_ORDERED;
3305 bp->bio_data = NULL;
3306 bp->bio_offset = ump->um_cp->provider->mediasize;
3308 bp->bio_done = softdep_synchronize_completed;
3309 bp->bio_caller1 = caller1;
3311 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private);
3315 * Flush some journal records to disk.
3318 softdep_process_journal(mp, needwk, flags)
3320 struct worklist *needwk;
3323 struct jblocks *jblocks;
3324 struct ufsmount *ump;
3325 struct worklist *wk;
3333 int jrecmin; /* Minimum records per block. */
3334 int jrecmax; /* Maximum records per block. */
3340 if (MOUNTEDSUJ(mp) == 0)
3342 shouldflush = softdep_flushcache;
3348 jblocks = ump->softdep_jblocks;
3349 devbsize = ump->um_devvp->v_bufobj.bo_bsize;
3351 * We write anywhere between a disk block and fs block. The upper
3352 * bound is picked to prevent buffer cache fragmentation and limit
3353 * processing time per I/O.
3355 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */
3356 jrecmax = (fs->fs_bsize / devbsize) * jrecmin;
3359 cnt = ump->softdep_on_journal;
3361 * Criteria for writing a segment:
3362 * 1) We have a full block.
3363 * 2) We're called from jwait() and haven't found the
3365 * 3) Always write if needseg is set.
3366 * 4) If we are called from process_worklist and have
3367 * not yet written anything we write a partial block
3368 * to enforce a 1 second maximum latency on journal
3371 if (cnt < (jrecmax - 1) && needwk == NULL &&
3372 jblocks->jb_needseg == 0 && (segwritten || cnt == 0))
3376 * Verify some free journal space. softdep_prealloc() should
3377 * guarantee that we don't run out so this is indicative of
3378 * a problem with the flow control. Try to recover
3379 * gracefully in any event.
3381 while (jblocks->jb_free == 0) {
3382 if (flags != MNT_WAIT)
3384 printf("softdep: Out of journal space!\n");
3385 softdep_speedup(ump);
3386 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz);
3389 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS);
3390 workitem_alloc(&jseg->js_list, D_JSEG, mp);
3391 LIST_INIT(&jseg->js_entries);
3392 LIST_INIT(&jseg->js_indirs);
3393 jseg->js_state = ATTACHED;
3394 if (shouldflush == 0)
3395 jseg->js_state |= COMPLETE;
3396 else if (bio == NULL)
3397 bio = g_alloc_bio();
3398 jseg->js_jblocks = jblocks;
3399 bp = geteblk(fs->fs_bsize, 0);
3402 * If there was a race while we were allocating the block
3403 * and jseg the entry we care about was likely written.
3404 * We bail out in both the WAIT and NOWAIT case and assume
3405 * the caller will loop if the entry it cares about is
3408 cnt = ump->softdep_on_journal;
3409 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) {
3410 bp->b_flags |= B_INVAL | B_NOCACHE;
3411 WORKITEM_FREE(jseg, D_JSEG);
3418 * Calculate the disk block size required for the available
3419 * records rounded to the min size.
3423 else if (cnt < jrecmax)
3424 size = howmany(cnt, jrecmin) * devbsize;
3426 size = fs->fs_bsize;
3428 * Allocate a disk block for this journal data and account
3429 * for truncation of the requested size if enough contiguous
3430 * space was not available.
3432 bp->b_blkno = jblocks_alloc(jblocks, size, &size);
3433 bp->b_lblkno = bp->b_blkno;
3434 bp->b_offset = bp->b_blkno * DEV_BSIZE;
3435 bp->b_bcount = size;
3436 bp->b_flags &= ~B_INVAL;
3437 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY;
3439 * Initialize our jseg with cnt records. Assign the next
3440 * sequence number to it and link it in-order.
3442 cnt = MIN(cnt, (size / devbsize) * jrecmin);
3445 jseg->js_refs = cnt + 1; /* Self ref. */
3446 jseg->js_size = size;
3447 jseg->js_seq = jblocks->jb_nextseq++;
3448 if (jblocks->jb_oldestseg == NULL)
3449 jblocks->jb_oldestseg = jseg;
3450 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq;
3451 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next);
3452 if (jblocks->jb_writeseg == NULL)
3453 jblocks->jb_writeseg = jseg;
3455 * Start filling in records from the pending list.
3461 * Always put a header on the first block.
3462 * XXX As with below, there might not be a chance to get
3463 * into the loop. Ensure that something valid is written.
3465 jseg_write(ump, jseg, data);
3467 data = bp->b_data + off;
3470 * XXX Something is wrong here. There's no work to do,
3471 * but we need to perform and I/O and allow it to complete
3474 if (LIST_EMPTY(&ump->softdep_journal_pending))
3475 stat_emptyjblocks++;
3477 while ((wk = LIST_FIRST(&ump->softdep_journal_pending))
3481 /* Place a segment header on every device block. */
3482 if ((off % devbsize) == 0) {
3483 jseg_write(ump, jseg, data);
3485 data = bp->b_data + off;
3489 remove_from_journal(wk);
3490 wk->wk_state |= INPROGRESS;
3491 WORKLIST_INSERT(&jseg->js_entries, wk);
3492 switch (wk->wk_type) {
3494 jaddref_write(WK_JADDREF(wk), jseg, data);
3497 jremref_write(WK_JREMREF(wk), jseg, data);
3500 jmvref_write(WK_JMVREF(wk), jseg, data);
3503 jnewblk_write(WK_JNEWBLK(wk), jseg, data);
3506 jfreeblk_write(WK_JFREEBLK(wk), jseg, data);
3509 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data);
3512 jtrunc_write(WK_JTRUNC(wk), jseg, data);
3515 jfsync_write(WK_JFSYNC(wk), jseg, data);
3518 panic("process_journal: Unknown type %s",
3519 TYPENAME(wk->wk_type));
3523 data = bp->b_data + off;
3527 /* Clear any remaining space so we don't leak kernel data */
3529 bzero(data, size - off);
3532 * Write this one buffer and continue.
3535 jblocks->jb_needseg = 0;
3536 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list);
3538 pbgetvp(ump->um_devvp, bp);
3540 * We only do the blocking wait once we find the journal
3541 * entry we're looking for.
3543 if (needwk == NULL && flags == MNT_WAIT)
3550 * If we wrote a segment issue a synchronize cache so the journal
3551 * is reflected on disk before the data is written. Since reclaiming
3552 * journal space also requires writing a journal record this
3553 * process also enforces a barrier before reclamation.
3555 if (segwritten && shouldflush) {
3556 softdep_synchronize(bio, ump,
3557 TAILQ_LAST(&jblocks->jb_segs, jseglst));
3561 * If we've suspended the filesystem because we ran out of journal
3562 * space either try to sync it here to make some progress or
3563 * unsuspend it if we already have.
3565 if (flags == 0 && jblocks->jb_suspended) {
3566 if (journal_unsuspend(ump))
3569 VFS_SYNC(mp, MNT_NOWAIT);
3570 ffs_sbupdate(ump, MNT_WAIT, 0);
3576 * Complete a jseg, allowing all dependencies awaiting journal writes
3577 * to proceed. Each journal dependency also attaches a jsegdep to dependent
3578 * structures so that the journal segment can be freed to reclaim space.
3584 struct worklist *wk;
3585 struct jmvref *jmvref;
3591 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) {
3592 WORKLIST_REMOVE(wk);
3593 waiting = wk->wk_state & IOWAITING;
3594 wk->wk_state &= ~(INPROGRESS | IOWAITING);
3595 wk->wk_state |= COMPLETE;
3596 KASSERT(i++ < jseg->js_cnt,
3597 ("handle_written_jseg: overflow %d >= %d",
3598 i - 1, jseg->js_cnt));
3599 switch (wk->wk_type) {
3601 handle_written_jaddref(WK_JADDREF(wk));
3604 handle_written_jremref(WK_JREMREF(wk));
3607 rele_jseg(jseg); /* No jsegdep. */
3608 jmvref = WK_JMVREF(wk);
3609 LIST_REMOVE(jmvref, jm_deps);
3610 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0)
3611 free_pagedep(jmvref->jm_pagedep);
3612 WORKITEM_FREE(jmvref, D_JMVREF);
3615 handle_written_jnewblk(WK_JNEWBLK(wk));
3618 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep);
3621 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep);
3624 rele_jseg(jseg); /* No jsegdep. */
3625 WORKITEM_FREE(wk, D_JFSYNC);
3628 handle_written_jfreefrag(WK_JFREEFRAG(wk));
3631 panic("handle_written_jseg: Unknown type %s",
3632 TYPENAME(wk->wk_type));
3638 /* Release the self reference so the structure may be freed. */
3643 * Determine which jsegs are ready for completion processing. Waits for
3644 * synchronize cache to complete as well as forcing in-order completion
3645 * of journal entries.
3648 complete_jsegs(jseg)
3651 struct jblocks *jblocks;
3654 jblocks = jseg->js_jblocks;
3656 * Don't allow out of order completions. If this isn't the first
3657 * block wait for it to write before we're done.
3659 if (jseg != jblocks->jb_writeseg)
3661 /* Iterate through available jsegs processing their entries. */
3662 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) {
3663 jblocks->jb_oldestwrseq = jseg->js_oldseq;
3664 jsegn = TAILQ_NEXT(jseg, js_next);
3665 complete_jseg(jseg);
3668 jblocks->jb_writeseg = jseg;
3670 * Attempt to free jsegs now that oldestwrseq may have advanced.
3672 free_jsegs(jblocks);
3676 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle
3677 * the final completions.
3680 handle_written_jseg(jseg, bp)
3685 if (jseg->js_refs == 0)
3686 panic("handle_written_jseg: No self-reference on %p", jseg);
3687 jseg->js_state |= DEPCOMPLETE;
3689 * We'll never need this buffer again, set flags so it will be
3692 bp->b_flags |= B_INVAL | B_NOCACHE;
3694 complete_jsegs(jseg);
3697 static inline struct jsegdep *
3699 struct inoref *inoref;
3701 struct jsegdep *jsegdep;
3703 jsegdep = inoref->if_jsegdep;
3704 inoref->if_jsegdep = NULL;
3710 * Called once a jremref has made it to stable store. The jremref is marked
3711 * complete and we attempt to free it. Any pagedeps writes sleeping waiting
3712 * for the jremref to complete will be awoken by free_jremref.
3715 handle_written_jremref(jremref)
3716 struct jremref *jremref;
3718 struct inodedep *inodedep;
3719 struct jsegdep *jsegdep;
3720 struct dirrem *dirrem;
3722 /* Grab the jsegdep. */
3723 jsegdep = inoref_jseg(&jremref->jr_ref);
3725 * Remove us from the inoref list.
3727 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino,
3729 panic("handle_written_jremref: Lost inodedep");
3730 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
3732 * Complete the dirrem.
3734 dirrem = jremref->jr_dirrem;
3735 jremref->jr_dirrem = NULL;
3736 LIST_REMOVE(jremref, jr_deps);
3737 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT;
3738 jwork_insert(&dirrem->dm_jwork, jsegdep);
3739 if (LIST_EMPTY(&dirrem->dm_jremrefhd) &&
3740 (dirrem->dm_state & COMPLETE) != 0)
3741 add_to_worklist(&dirrem->dm_list, 0);
3742 free_jremref(jremref);
3746 * Called once a jaddref has made it to stable store. The dependency is
3747 * marked complete and any dependent structures are added to the inode
3748 * bufwait list to be completed as soon as it is written. If a bitmap write
3749 * depends on this entry we move the inode into the inodedephd of the
3750 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap.
3753 handle_written_jaddref(jaddref)
3754 struct jaddref *jaddref;
3756 struct jsegdep *jsegdep;
3757 struct inodedep *inodedep;
3758 struct diradd *diradd;
3759 struct mkdir *mkdir;
3761 /* Grab the jsegdep. */
3762 jsegdep = inoref_jseg(&jaddref->ja_ref);
3765 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
3767 panic("handle_written_jaddref: Lost inodedep.");
3768 if (jaddref->ja_diradd == NULL)
3769 panic("handle_written_jaddref: No dependency");
3770 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) {
3771 diradd = jaddref->ja_diradd;
3772 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list);
3773 } else if (jaddref->ja_state & MKDIR_PARENT) {
3774 mkdir = jaddref->ja_mkdir;
3775 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list);
3776 } else if (jaddref->ja_state & MKDIR_BODY)
3777 mkdir = jaddref->ja_mkdir;
3779 panic("handle_written_jaddref: Unknown dependency %p",
3780 jaddref->ja_diradd);
3781 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */
3783 * Remove us from the inode list.
3785 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps);
3787 * The mkdir may be waiting on the jaddref to clear before freeing.
3790 KASSERT(mkdir->md_list.wk_type == D_MKDIR,
3791 ("handle_written_jaddref: Incorrect type for mkdir %s",
3792 TYPENAME(mkdir->md_list.wk_type)));
3793 mkdir->md_jaddref = NULL;
3794 diradd = mkdir->md_diradd;
3795 mkdir->md_state |= DEPCOMPLETE;
3796 complete_mkdir(mkdir);
3798 jwork_insert(&diradd->da_jwork, jsegdep);
3799 if (jaddref->ja_state & NEWBLOCK) {
3800 inodedep->id_state |= ONDEPLIST;
3801 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd,
3804 free_jaddref(jaddref);
3808 * Called once a jnewblk journal is written. The allocdirect or allocindir
3809 * is placed in the bmsafemap to await notification of a written bitmap. If
3810 * the operation was canceled we add the segdep to the appropriate
3811 * dependency to free the journal space once the canceling operation
3815 handle_written_jnewblk(jnewblk)
3816 struct jnewblk *jnewblk;
3818 struct bmsafemap *bmsafemap;
3819 struct freefrag *freefrag;
3820 struct freework *freework;
3821 struct jsegdep *jsegdep;
3822 struct newblk *newblk;
3824 /* Grab the jsegdep. */
3825 jsegdep = jnewblk->jn_jsegdep;
3826 jnewblk->jn_jsegdep = NULL;
3827 if (jnewblk->jn_dep == NULL)
3828 panic("handle_written_jnewblk: No dependency for the segdep.");
3829 switch (jnewblk->jn_dep->wk_type) {
3834 * Add the written block to the bmsafemap so it can
3835 * be notified when the bitmap is on disk.
3837 newblk = WK_NEWBLK(jnewblk->jn_dep);
3838 newblk->nb_jnewblk = NULL;
3839 if ((newblk->nb_state & GOINGAWAY) == 0) {
3840 bmsafemap = newblk->nb_bmsafemap;
3841 newblk->nb_state |= ONDEPLIST;
3842 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk,
3845 jwork_insert(&newblk->nb_jwork, jsegdep);
3849 * A newblock being removed by a freefrag when replaced by
3852 freefrag = WK_FREEFRAG(jnewblk->jn_dep);
3853 freefrag->ff_jdep = NULL;
3854 jwork_insert(&freefrag->ff_jwork, jsegdep);
3858 * A direct block was removed by truncate.
3860 freework = WK_FREEWORK(jnewblk->jn_dep);
3861 freework->fw_jnewblk = NULL;
3862 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep);
3865 panic("handle_written_jnewblk: Unknown type %d.",
3866 jnewblk->jn_dep->wk_type);
3868 jnewblk->jn_dep = NULL;
3869 free_jnewblk(jnewblk);
3873 * Cancel a jfreefrag that won't be needed, probably due to colliding with
3874 * an in-flight allocation that has not yet been committed. Divorce us
3875 * from the freefrag and mark it DEPCOMPLETE so that it may be added
3879 cancel_jfreefrag(jfreefrag)
3880 struct jfreefrag *jfreefrag;
3882 struct freefrag *freefrag;
3884 if (jfreefrag->fr_jsegdep) {
3885 free_jsegdep(jfreefrag->fr_jsegdep);
3886 jfreefrag->fr_jsegdep = NULL;
3888 freefrag = jfreefrag->fr_freefrag;
3889 jfreefrag->fr_freefrag = NULL;
3890 free_jfreefrag(jfreefrag);
3891 freefrag->ff_state |= DEPCOMPLETE;
3892 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno);
3896 * Free a jfreefrag when the parent freefrag is rendered obsolete.
3899 free_jfreefrag(jfreefrag)
3900 struct jfreefrag *jfreefrag;
3903 if (jfreefrag->fr_state & INPROGRESS)
3904 WORKLIST_REMOVE(&jfreefrag->fr_list);
3905 else if (jfreefrag->fr_state & ONWORKLIST)
3906 remove_from_journal(&jfreefrag->fr_list);
3907 if (jfreefrag->fr_freefrag != NULL)
3908 panic("free_jfreefrag: Still attached to a freefrag.");
3909 WORKITEM_FREE(jfreefrag, D_JFREEFRAG);
3913 * Called when the journal write for a jfreefrag completes. The parent
3914 * freefrag is added to the worklist if this completes its dependencies.
3917 handle_written_jfreefrag(jfreefrag)
3918 struct jfreefrag *jfreefrag;
3920 struct jsegdep *jsegdep;
3921 struct freefrag *freefrag;
3923 /* Grab the jsegdep. */
3924 jsegdep = jfreefrag->fr_jsegdep;
3925 jfreefrag->fr_jsegdep = NULL;
3926 freefrag = jfreefrag->fr_freefrag;
3927 if (freefrag == NULL)
3928 panic("handle_written_jfreefrag: No freefrag.");
3929 freefrag->ff_state |= DEPCOMPLETE;
3930 freefrag->ff_jdep = NULL;
3931 jwork_insert(&freefrag->ff_jwork, jsegdep);
3932 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
3933 add_to_worklist(&freefrag->ff_list, 0);
3934 jfreefrag->fr_freefrag = NULL;
3935 free_jfreefrag(jfreefrag);
3939 * Called when the journal write for a jfreeblk completes. The jfreeblk
3940 * is removed from the freeblks list of pending journal writes and the
3941 * jsegdep is moved to the freeblks jwork to be completed when all blocks
3942 * have been reclaimed.
3945 handle_written_jblkdep(jblkdep)
3946 struct jblkdep *jblkdep;
3948 struct freeblks *freeblks;
3949 struct jsegdep *jsegdep;
3951 /* Grab the jsegdep. */
3952 jsegdep = jblkdep->jb_jsegdep;
3953 jblkdep->jb_jsegdep = NULL;
3954 freeblks = jblkdep->jb_freeblks;
3955 LIST_REMOVE(jblkdep, jb_deps);
3956 jwork_insert(&freeblks->fb_jwork, jsegdep);
3958 * If the freeblks is all journaled, we can add it to the worklist.
3960 if (LIST_EMPTY(&freeblks->fb_jblkdephd) &&
3961 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
3962 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
3964 free_jblkdep(jblkdep);
3967 static struct jsegdep *
3968 newjsegdep(struct worklist *wk)
3970 struct jsegdep *jsegdep;
3972 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS);
3973 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp);
3974 jsegdep->jd_seg = NULL;
3979 static struct jmvref *
3980 newjmvref(dp, ino, oldoff, newoff)
3986 struct jmvref *jmvref;
3988 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS);
3989 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump));
3990 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE;
3991 jmvref->jm_parent = dp->i_number;
3992 jmvref->jm_ino = ino;
3993 jmvref->jm_oldoff = oldoff;
3994 jmvref->jm_newoff = newoff;
4000 * Allocate a new jremref that tracks the removal of ip from dp with the
4001 * directory entry offset of diroff. Mark the entry as ATTACHED and
4002 * DEPCOMPLETE as we have all the information required for the journal write
4003 * and the directory has already been removed from the buffer. The caller
4004 * is responsible for linking the jremref into the pagedep and adding it
4005 * to the journal to write. The MKDIR_PARENT flag is set if we're doing
4006 * a DOTDOT addition so handle_workitem_remove() can properly assign
4007 * the jsegdep when we're done.
4009 static struct jremref *
4010 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip,
4011 off_t diroff, nlink_t nlink)
4013 struct jremref *jremref;
4015 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS);
4016 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump));
4017 jremref->jr_state = ATTACHED;
4018 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff,
4020 jremref->jr_dirrem = dirrem;
4026 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff,
4027 nlink_t nlink, uint16_t mode)
4030 inoref->if_jsegdep = newjsegdep(&inoref->if_list);
4031 inoref->if_diroff = diroff;
4032 inoref->if_ino = ino;
4033 inoref->if_parent = parent;
4034 inoref->if_nlink = nlink;
4035 inoref->if_mode = mode;
4039 * Allocate a new jaddref to track the addition of ino to dp at diroff. The
4040 * directory offset may not be known until later. The caller is responsible
4041 * adding the entry to the journal when this information is available. nlink
4042 * should be the link count prior to the addition and mode is only required
4043 * to have the correct FMT.
4045 static struct jaddref *
4046 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink,
4049 struct jaddref *jaddref;
4051 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS);
4052 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump));
4053 jaddref->ja_state = ATTACHED;
4054 jaddref->ja_mkdir = NULL;
4055 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode);
4061 * Create a new free dependency for a freework. The caller is responsible
4062 * for adjusting the reference count when it has the lock held. The freedep
4063 * will track an outstanding bitmap write that will ultimately clear the
4064 * freework to continue.
4066 static struct freedep *
4067 newfreedep(struct freework *freework)
4069 struct freedep *freedep;
4071 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS);
4072 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp);
4073 freedep->fd_freework = freework;
4079 * Free a freedep structure once the buffer it is linked to is written. If
4080 * this is the last reference to the freework schedule it for completion.
4083 free_freedep(freedep)
4084 struct freedep *freedep;
4086 struct freework *freework;
4088 freework = freedep->fd_freework;
4089 freework->fw_freeblks->fb_cgwait--;
4090 if (--freework->fw_ref == 0)
4091 freework_enqueue(freework);
4092 WORKITEM_FREE(freedep, D_FREEDEP);
4096 * Allocate a new freework structure that may be a level in an indirect
4097 * when parent is not NULL or a top level block when it is. The top level
4098 * freework structures are allocated without the per-filesystem lock held
4099 * and before the freeblks is visible outside of softdep_setup_freeblocks().
4101 static struct freework *
4102 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal)
4103 struct ufsmount *ump;
4104 struct freeblks *freeblks;
4105 struct freework *parent;
4112 struct freework *freework;
4114 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS);
4115 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp);
4116 freework->fw_state = ATTACHED;
4117 freework->fw_jnewblk = NULL;
4118 freework->fw_freeblks = freeblks;
4119 freework->fw_parent = parent;
4120 freework->fw_lbn = lbn;
4121 freework->fw_blkno = nb;
4122 freework->fw_frags = frags;
4123 freework->fw_indir = NULL;
4124 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || lbn >= -NXADDR)
4125 ? 0 : NINDIR(ump->um_fs) + 1;
4126 freework->fw_start = freework->fw_off = off;
4128 newjfreeblk(freeblks, lbn, nb, frags);
4129 if (parent == NULL) {
4131 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
4140 * Eliminate a jfreeblk for a block that does not need journaling.
4143 cancel_jfreeblk(freeblks, blkno)
4144 struct freeblks *freeblks;
4147 struct jfreeblk *jfreeblk;
4148 struct jblkdep *jblkdep;
4150 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) {
4151 if (jblkdep->jb_list.wk_type != D_JFREEBLK)
4153 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list);
4154 if (jfreeblk->jf_blkno == blkno)
4157 if (jblkdep == NULL)
4159 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno);
4160 free_jsegdep(jblkdep->jb_jsegdep);
4161 LIST_REMOVE(jblkdep, jb_deps);
4162 WORKITEM_FREE(jfreeblk, D_JFREEBLK);
4166 * Allocate a new jfreeblk to journal top level block pointer when truncating
4167 * a file. The caller must add this to the worklist when the per-filesystem
4170 static struct jfreeblk *
4171 newjfreeblk(freeblks, lbn, blkno, frags)
4172 struct freeblks *freeblks;
4177 struct jfreeblk *jfreeblk;
4179 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS);
4180 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK,
4181 freeblks->fb_list.wk_mp);
4182 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list);
4183 jfreeblk->jf_dep.jb_freeblks = freeblks;
4184 jfreeblk->jf_ino = freeblks->fb_inum;
4185 jfreeblk->jf_lbn = lbn;
4186 jfreeblk->jf_blkno = blkno;
4187 jfreeblk->jf_frags = frags;
4188 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps);
4194 * The journal is only prepared to handle full-size block numbers, so we
4195 * have to adjust the record to reflect the change to a full-size block.
4196 * For example, suppose we have a block made up of fragments 8-15 and
4197 * want to free its last two fragments. We are given a request that says:
4198 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0
4199 * where frags are the number of fragments to free and oldfrags are the
4200 * number of fragments to keep. To block align it, we have to change it to
4201 * have a valid full-size blkno, so it becomes:
4202 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6
4205 adjust_newfreework(freeblks, frag_offset)
4206 struct freeblks *freeblks;
4209 struct jfreeblk *jfreeblk;
4211 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL &&
4212 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK),
4213 ("adjust_newfreework: Missing freeblks dependency"));
4215 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd));
4216 jfreeblk->jf_blkno -= frag_offset;
4217 jfreeblk->jf_frags += frag_offset;
4221 * Allocate a new jtrunc to track a partial truncation.
4223 static struct jtrunc *
4224 newjtrunc(freeblks, size, extsize)
4225 struct freeblks *freeblks;
4229 struct jtrunc *jtrunc;
4231 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS);
4232 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC,
4233 freeblks->fb_list.wk_mp);
4234 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list);
4235 jtrunc->jt_dep.jb_freeblks = freeblks;
4236 jtrunc->jt_ino = freeblks->fb_inum;
4237 jtrunc->jt_size = size;
4238 jtrunc->jt_extsize = extsize;
4239 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps);
4245 * If we're canceling a new bitmap we have to search for another ref
4246 * to move into the bmsafemap dep. This might be better expressed
4247 * with another structure.
4250 move_newblock_dep(jaddref, inodedep)
4251 struct jaddref *jaddref;
4252 struct inodedep *inodedep;
4254 struct inoref *inoref;
4255 struct jaddref *jaddrefn;
4258 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4259 inoref = TAILQ_NEXT(inoref, if_deps)) {
4260 if ((jaddref->ja_state & NEWBLOCK) &&
4261 inoref->if_list.wk_type == D_JADDREF) {
4262 jaddrefn = (struct jaddref *)inoref;
4266 if (jaddrefn == NULL)
4268 jaddrefn->ja_state &= ~(ATTACHED | UNDONE);
4269 jaddrefn->ja_state |= jaddref->ja_state &
4270 (ATTACHED | UNDONE | NEWBLOCK);
4271 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK);
4272 jaddref->ja_state |= ATTACHED;
4273 LIST_REMOVE(jaddref, ja_bmdeps);
4274 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn,
4279 * Cancel a jaddref either before it has been written or while it is being
4280 * written. This happens when a link is removed before the add reaches
4281 * the disk. The jaddref dependency is kept linked into the bmsafemap
4282 * and inode to prevent the link count or bitmap from reaching the disk
4283 * until handle_workitem_remove() re-adjusts the counts and bitmaps as
4286 * Returns 1 if the canceled addref requires journaling of the remove and
4290 cancel_jaddref(jaddref, inodedep, wkhd)
4291 struct jaddref *jaddref;
4292 struct inodedep *inodedep;
4293 struct workhead *wkhd;
4295 struct inoref *inoref;
4296 struct jsegdep *jsegdep;
4299 KASSERT((jaddref->ja_state & COMPLETE) == 0,
4300 ("cancel_jaddref: Canceling complete jaddref"));
4301 if (jaddref->ja_state & (INPROGRESS | COMPLETE))
4305 if (inodedep == NULL)
4306 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino,
4308 panic("cancel_jaddref: Lost inodedep");
4310 * We must adjust the nlink of any reference operation that follows
4311 * us so that it is consistent with the in-memory reference. This
4312 * ensures that inode nlink rollbacks always have the correct link.
4315 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref;
4316 inoref = TAILQ_NEXT(inoref, if_deps)) {
4317 if (inoref->if_state & GOINGAWAY)
4322 jsegdep = inoref_jseg(&jaddref->ja_ref);
4323 if (jaddref->ja_state & NEWBLOCK)
4324 move_newblock_dep(jaddref, inodedep);
4325 wake_worklist(&jaddref->ja_list);
4326 jaddref->ja_mkdir = NULL;
4327 if (jaddref->ja_state & INPROGRESS) {
4328 jaddref->ja_state &= ~INPROGRESS;
4329 WORKLIST_REMOVE(&jaddref->ja_list);
4330 jwork_insert(wkhd, jsegdep);
4332 free_jsegdep(jsegdep);
4333 if (jaddref->ja_state & DEPCOMPLETE)
4334 remove_from_journal(&jaddref->ja_list);
4336 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE);
4338 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove
4339 * can arrange for them to be freed with the bitmap. Otherwise we
4340 * no longer need this addref attached to the inoreflst and it
4341 * will incorrectly adjust nlink if we leave it.
4343 if ((jaddref->ja_state & NEWBLOCK) == 0) {
4344 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
4346 jaddref->ja_state |= COMPLETE;
4347 free_jaddref(jaddref);
4351 * Leave the head of the list for jsegdeps for fast merging.
4353 if (LIST_FIRST(wkhd) != NULL) {
4354 jaddref->ja_state |= ONWORKLIST;
4355 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list);
4357 WORKLIST_INSERT(wkhd, &jaddref->ja_list);
4363 * Attempt to free a jaddref structure when some work completes. This
4364 * should only succeed once the entry is written and all dependencies have
4368 free_jaddref(jaddref)
4369 struct jaddref *jaddref;
4372 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE)
4374 if (jaddref->ja_ref.if_jsegdep)
4375 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n",
4376 jaddref, jaddref->ja_state);
4377 if (jaddref->ja_state & NEWBLOCK)
4378 LIST_REMOVE(jaddref, ja_bmdeps);
4379 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST))
4380 panic("free_jaddref: Bad state %p(0x%X)",
4381 jaddref, jaddref->ja_state);
4382 if (jaddref->ja_mkdir != NULL)
4383 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state);
4384 WORKITEM_FREE(jaddref, D_JADDREF);
4388 * Free a jremref structure once it has been written or discarded.
4391 free_jremref(jremref)
4392 struct jremref *jremref;
4395 if (jremref->jr_ref.if_jsegdep)
4396 free_jsegdep(jremref->jr_ref.if_jsegdep);
4397 if (jremref->jr_state & INPROGRESS)
4398 panic("free_jremref: IO still pending");
4399 WORKITEM_FREE(jremref, D_JREMREF);
4403 * Free a jnewblk structure.
4406 free_jnewblk(jnewblk)
4407 struct jnewblk *jnewblk;
4410 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE)
4412 LIST_REMOVE(jnewblk, jn_deps);
4413 if (jnewblk->jn_dep != NULL)
4414 panic("free_jnewblk: Dependency still attached.");
4415 WORKITEM_FREE(jnewblk, D_JNEWBLK);
4419 * Cancel a jnewblk which has been been made redundant by frag extension.
4422 cancel_jnewblk(jnewblk, wkhd)
4423 struct jnewblk *jnewblk;
4424 struct workhead *wkhd;
4426 struct jsegdep *jsegdep;
4428 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno);
4429 jsegdep = jnewblk->jn_jsegdep;
4430 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL)
4431 panic("cancel_jnewblk: Invalid state");
4432 jnewblk->jn_jsegdep = NULL;
4433 jnewblk->jn_dep = NULL;
4434 jnewblk->jn_state |= GOINGAWAY;
4435 if (jnewblk->jn_state & INPROGRESS) {
4436 jnewblk->jn_state &= ~INPROGRESS;
4437 WORKLIST_REMOVE(&jnewblk->jn_list);
4438 jwork_insert(wkhd, jsegdep);
4440 free_jsegdep(jsegdep);
4441 remove_from_journal(&jnewblk->jn_list);
4443 wake_worklist(&jnewblk->jn_list);
4444 WORKLIST_INSERT(wkhd, &jnewblk->jn_list);
4448 free_jblkdep(jblkdep)
4449 struct jblkdep *jblkdep;
4452 if (jblkdep->jb_list.wk_type == D_JFREEBLK)
4453 WORKITEM_FREE(jblkdep, D_JFREEBLK);
4454 else if (jblkdep->jb_list.wk_type == D_JTRUNC)
4455 WORKITEM_FREE(jblkdep, D_JTRUNC);
4457 panic("free_jblkdep: Unexpected type %s",
4458 TYPENAME(jblkdep->jb_list.wk_type));
4462 * Free a single jseg once it is no longer referenced in memory or on
4463 * disk. Reclaim journal blocks and dependencies waiting for the segment
4467 free_jseg(jseg, jblocks)
4469 struct jblocks *jblocks;
4471 struct freework *freework;
4474 * Free freework structures that were lingering to indicate freed
4475 * indirect blocks that forced journal write ordering on reallocate.
4477 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL)
4478 indirblk_remove(freework);
4479 if (jblocks->jb_oldestseg == jseg)
4480 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next);
4481 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next);
4482 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size);
4483 KASSERT(LIST_EMPTY(&jseg->js_entries),
4484 ("free_jseg: Freed jseg has valid entries."));
4485 WORKITEM_FREE(jseg, D_JSEG);
4489 * Free all jsegs that meet the criteria for being reclaimed and update
4494 struct jblocks *jblocks;
4499 * Free only those jsegs which have none allocated before them to
4500 * preserve the journal space ordering.
4502 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) {
4504 * Only reclaim space when nothing depends on this journal
4505 * set and another set has written that it is no longer
4508 if (jseg->js_refs != 0) {
4509 jblocks->jb_oldestseg = jseg;
4512 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE)
4514 if (jseg->js_seq > jblocks->jb_oldestwrseq)
4517 * We can free jsegs that didn't write entries when
4518 * oldestwrseq == js_seq.
4520 if (jseg->js_seq == jblocks->jb_oldestwrseq &&
4523 free_jseg(jseg, jblocks);
4526 * If we exited the loop above we still must discover the
4527 * oldest valid segment.
4530 for (jseg = jblocks->jb_oldestseg; jseg != NULL;
4531 jseg = TAILQ_NEXT(jseg, js_next))
4532 if (jseg->js_refs != 0)
4534 jblocks->jb_oldestseg = jseg;
4536 * The journal has no valid records but some jsegs may still be
4537 * waiting on oldestwrseq to advance. We force a small record
4538 * out to permit these lingering records to be reclaimed.
4540 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs))
4541 jblocks->jb_needseg = 1;
4545 * Release one reference to a jseg and free it if the count reaches 0. This
4546 * should eventually reclaim journal space as well.
4553 KASSERT(jseg->js_refs > 0,
4554 ("free_jseg: Invalid refcnt %d", jseg->js_refs));
4555 if (--jseg->js_refs != 0)
4557 free_jsegs(jseg->js_jblocks);
4561 * Release a jsegdep and decrement the jseg count.
4564 free_jsegdep(jsegdep)
4565 struct jsegdep *jsegdep;
4568 if (jsegdep->jd_seg)
4569 rele_jseg(jsegdep->jd_seg);
4570 WORKITEM_FREE(jsegdep, D_JSEGDEP);
4574 * Wait for a journal item to make it to disk. Initiate journal processing
4579 struct worklist *wk;
4583 LOCK_OWNED(VFSTOUFS(wk->wk_mp));
4585 * Blocking journal waits cause slow synchronous behavior. Record
4586 * stats on the frequency of these blocking operations.
4588 if (waitfor == MNT_WAIT) {
4589 stat_journal_wait++;
4590 switch (wk->wk_type) {
4593 stat_jwait_filepage++;
4597 stat_jwait_freeblks++;
4600 stat_jwait_newblk++;
4610 * If IO has not started we process the journal. We can't mark the
4611 * worklist item as IOWAITING because we drop the lock while
4612 * processing the journal and the worklist entry may be freed after
4613 * this point. The caller may call back in and re-issue the request.
4615 if ((wk->wk_state & INPROGRESS) == 0) {
4616 softdep_process_journal(wk->wk_mp, wk, waitfor);
4617 if (waitfor != MNT_WAIT)
4621 if (waitfor != MNT_WAIT)
4623 wait_worklist(wk, "jwait");
4628 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as
4629 * appropriate. This is a convenience function to reduce duplicate code
4630 * for the setup and revert functions below.
4632 static struct inodedep *
4633 inodedep_lookup_ip(ip)
4636 struct inodedep *inodedep;
4638 KASSERT(ip->i_nlink >= ip->i_effnlink,
4639 ("inodedep_lookup_ip: bad delta"));
4640 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC,
4642 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
4643 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
4649 * Called prior to creating a new inode and linking it to a directory. The
4650 * jaddref structure must already be allocated by softdep_setup_inomapdep
4651 * and it is discovered here so we can initialize the mode and update
4655 softdep_setup_create(dp, ip)
4659 struct inodedep *inodedep;
4660 struct jaddref *jaddref;
4663 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4664 ("softdep_setup_create called on non-softdep filesystem"));
4665 KASSERT(ip->i_nlink == 1,
4666 ("softdep_setup_create: Invalid link count."));
4668 ACQUIRE_LOCK(dp->i_ump);
4669 inodedep = inodedep_lookup_ip(ip);
4670 if (DOINGSUJ(dvp)) {
4671 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4673 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
4674 ("softdep_setup_create: No addref structure present."));
4676 softdep_prelink(dvp, NULL);
4677 FREE_LOCK(dp->i_ump);
4681 * Create a jaddref structure to track the addition of a DOTDOT link when
4682 * we are reparenting an inode as part of a rename. This jaddref will be
4683 * found by softdep_setup_directory_change. Adjusts nlinkdelta for
4684 * non-journaling softdep.
4687 softdep_setup_dotdot_link(dp, ip)
4691 struct inodedep *inodedep;
4692 struct jaddref *jaddref;
4695 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4696 ("softdep_setup_dotdot_link called on non-softdep filesystem"));
4700 * We don't set MKDIR_PARENT as this is not tied to a mkdir and
4701 * is used as a normal link would be.
4704 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4705 dp->i_effnlink - 1, dp->i_mode);
4706 ACQUIRE_LOCK(dp->i_ump);
4707 inodedep = inodedep_lookup_ip(dp);
4709 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4711 softdep_prelink(dvp, ITOV(ip));
4712 FREE_LOCK(dp->i_ump);
4716 * Create a jaddref structure to track a new link to an inode. The directory
4717 * offset is not known until softdep_setup_directory_add or
4718 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling
4722 softdep_setup_link(dp, ip)
4726 struct inodedep *inodedep;
4727 struct jaddref *jaddref;
4730 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4731 ("softdep_setup_link called on non-softdep filesystem"));
4735 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1,
4737 ACQUIRE_LOCK(dp->i_ump);
4738 inodedep = inodedep_lookup_ip(ip);
4740 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
4742 softdep_prelink(dvp, ITOV(ip));
4743 FREE_LOCK(dp->i_ump);
4747 * Called to create the jaddref structures to track . and .. references as
4748 * well as lookup and further initialize the incomplete jaddref created
4749 * by softdep_setup_inomapdep when the inode was allocated. Adjusts
4750 * nlinkdelta for non-journaling softdep.
4753 softdep_setup_mkdir(dp, ip)
4757 struct inodedep *inodedep;
4758 struct jaddref *dotdotaddref;
4759 struct jaddref *dotaddref;
4760 struct jaddref *jaddref;
4763 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4764 ("softdep_setup_mkdir called on non-softdep filesystem"));
4766 dotaddref = dotdotaddref = NULL;
4767 if (DOINGSUJ(dvp)) {
4768 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1,
4770 dotaddref->ja_state |= MKDIR_BODY;
4771 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET,
4772 dp->i_effnlink - 1, dp->i_mode);
4773 dotdotaddref->ja_state |= MKDIR_PARENT;
4775 ACQUIRE_LOCK(dp->i_ump);
4776 inodedep = inodedep_lookup_ip(ip);
4777 if (DOINGSUJ(dvp)) {
4778 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4780 KASSERT(jaddref != NULL,
4781 ("softdep_setup_mkdir: No addref structure present."));
4782 KASSERT(jaddref->ja_parent == dp->i_number,
4783 ("softdep_setup_mkdir: bad parent %ju",
4784 (uintmax_t)jaddref->ja_parent));
4785 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref,
4788 inodedep = inodedep_lookup_ip(dp);
4790 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst,
4791 &dotdotaddref->ja_ref, if_deps);
4792 softdep_prelink(ITOV(dp), NULL);
4793 FREE_LOCK(dp->i_ump);
4797 * Called to track nlinkdelta of the inode and parent directories prior to
4798 * unlinking a directory.
4801 softdep_setup_rmdir(dp, ip)
4807 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4808 ("softdep_setup_rmdir called on non-softdep filesystem"));
4810 ACQUIRE_LOCK(dp->i_ump);
4811 (void) inodedep_lookup_ip(ip);
4812 (void) inodedep_lookup_ip(dp);
4813 softdep_prelink(dvp, ITOV(ip));
4814 FREE_LOCK(dp->i_ump);
4818 * Called to track nlinkdelta of the inode and parent directories prior to
4822 softdep_setup_unlink(dp, ip)
4828 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4829 ("softdep_setup_unlink called on non-softdep filesystem"));
4831 ACQUIRE_LOCK(dp->i_ump);
4832 (void) inodedep_lookup_ip(ip);
4833 (void) inodedep_lookup_ip(dp);
4834 softdep_prelink(dvp, ITOV(ip));
4835 FREE_LOCK(dp->i_ump);
4839 * Called to release the journal structures created by a failed non-directory
4840 * creation. Adjusts nlinkdelta for non-journaling softdep.
4843 softdep_revert_create(dp, ip)
4847 struct inodedep *inodedep;
4848 struct jaddref *jaddref;
4851 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4852 ("softdep_revert_create called on non-softdep filesystem"));
4854 ACQUIRE_LOCK(dp->i_ump);
4855 inodedep = inodedep_lookup_ip(ip);
4856 if (DOINGSUJ(dvp)) {
4857 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4859 KASSERT(jaddref->ja_parent == dp->i_number,
4860 ("softdep_revert_create: addref parent mismatch"));
4861 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4863 FREE_LOCK(dp->i_ump);
4867 * Called to release the journal structures created by a failed link
4868 * addition. Adjusts nlinkdelta for non-journaling softdep.
4871 softdep_revert_link(dp, ip)
4875 struct inodedep *inodedep;
4876 struct jaddref *jaddref;
4879 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4880 ("softdep_revert_link called on non-softdep filesystem"));
4882 ACQUIRE_LOCK(dp->i_ump);
4883 inodedep = inodedep_lookup_ip(ip);
4884 if (DOINGSUJ(dvp)) {
4885 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4887 KASSERT(jaddref->ja_parent == dp->i_number,
4888 ("softdep_revert_link: addref parent mismatch"));
4889 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4891 FREE_LOCK(dp->i_ump);
4895 * Called to release the journal structures created by a failed mkdir
4896 * attempt. Adjusts nlinkdelta for non-journaling softdep.
4899 softdep_revert_mkdir(dp, ip)
4903 struct inodedep *inodedep;
4904 struct jaddref *jaddref;
4905 struct jaddref *dotaddref;
4908 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4909 ("softdep_revert_mkdir called on non-softdep filesystem"));
4912 ACQUIRE_LOCK(dp->i_ump);
4913 inodedep = inodedep_lookup_ip(dp);
4914 if (DOINGSUJ(dvp)) {
4915 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4917 KASSERT(jaddref->ja_parent == ip->i_number,
4918 ("softdep_revert_mkdir: dotdot addref parent mismatch"));
4919 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4921 inodedep = inodedep_lookup_ip(ip);
4922 if (DOINGSUJ(dvp)) {
4923 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
4925 KASSERT(jaddref->ja_parent == dp->i_number,
4926 ("softdep_revert_mkdir: addref parent mismatch"));
4927 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
4928 inoreflst, if_deps);
4929 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait);
4930 KASSERT(dotaddref->ja_parent == ip->i_number,
4931 ("softdep_revert_mkdir: dot addref parent mismatch"));
4932 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait);
4934 FREE_LOCK(dp->i_ump);
4938 * Called to correct nlinkdelta after a failed rmdir.
4941 softdep_revert_rmdir(dp, ip)
4946 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0,
4947 ("softdep_revert_rmdir called on non-softdep filesystem"));
4948 ACQUIRE_LOCK(dp->i_ump);
4949 (void) inodedep_lookup_ip(ip);
4950 (void) inodedep_lookup_ip(dp);
4951 FREE_LOCK(dp->i_ump);
4955 * Protecting the freemaps (or bitmaps).
4957 * To eliminate the need to execute fsck before mounting a filesystem
4958 * after a power failure, one must (conservatively) guarantee that the
4959 * on-disk copy of the bitmaps never indicate that a live inode or block is
4960 * free. So, when a block or inode is allocated, the bitmap should be
4961 * updated (on disk) before any new pointers. When a block or inode is
4962 * freed, the bitmap should not be updated until all pointers have been
4963 * reset. The latter dependency is handled by the delayed de-allocation
4964 * approach described below for block and inode de-allocation. The former
4965 * dependency is handled by calling the following procedure when a block or
4966 * inode is allocated. When an inode is allocated an "inodedep" is created
4967 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
4968 * Each "inodedep" is also inserted into the hash indexing structure so
4969 * that any additional link additions can be made dependent on the inode
4972 * The ufs filesystem maintains a number of free block counts (e.g., per
4973 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
4974 * in addition to the bitmaps. These counts are used to improve efficiency
4975 * during allocation and therefore must be consistent with the bitmaps.
4976 * There is no convenient way to guarantee post-crash consistency of these
4977 * counts with simple update ordering, for two main reasons: (1) The counts
4978 * and bitmaps for a single cylinder group block are not in the same disk
4979 * sector. If a disk write is interrupted (e.g., by power failure), one may
4980 * be written and the other not. (2) Some of the counts are located in the
4981 * superblock rather than the cylinder group block. So, we focus our soft
4982 * updates implementation on protecting the bitmaps. When mounting a
4983 * filesystem, we recompute the auxiliary counts from the bitmaps.
4987 * Called just after updating the cylinder group block to allocate an inode.
4990 softdep_setup_inomapdep(bp, ip, newinum, mode)
4991 struct buf *bp; /* buffer for cylgroup block with inode map */
4992 struct inode *ip; /* inode related to allocation */
4993 ino_t newinum; /* new inode number being allocated */
4996 struct inodedep *inodedep;
4997 struct bmsafemap *bmsafemap;
4998 struct jaddref *jaddref;
5002 mp = UFSTOVFS(ip->i_ump);
5003 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5004 ("softdep_setup_inomapdep called on non-softdep filesystem"));
5005 fs = ip->i_ump->um_fs;
5009 * Allocate the journal reference add structure so that the bitmap
5010 * can be dependent on it.
5012 if (MOUNTEDSUJ(mp)) {
5013 jaddref = newjaddref(ip, newinum, 0, 0, mode);
5014 jaddref->ja_state |= NEWBLOCK;
5018 * Create a dependency for the newly allocated inode.
5019 * Panic if it already exists as something is seriously wrong.
5020 * Otherwise add it to the dependency list for the buffer holding
5021 * the cylinder group map from which it was allocated.
5023 * We have to preallocate a bmsafemap entry in case it is needed
5024 * in bmsafemap_lookup since once we allocate the inodedep, we
5025 * have to finish initializing it before we can FREE_LOCK().
5026 * By preallocating, we avoid FREE_LOCK() while doing a malloc
5027 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before
5028 * creating the inodedep as it can be freed during the time
5029 * that we FREE_LOCK() while allocating the inodedep. We must
5030 * call workitem_alloc() before entering the locked section as
5031 * it also acquires the lock and we must avoid trying doing so
5034 bmsafemap = malloc(sizeof(struct bmsafemap),
5035 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5036 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5037 ACQUIRE_LOCK(ip->i_ump);
5038 if ((inodedep_lookup(mp, newinum, DEPALLOC, &inodedep)))
5039 panic("softdep_setup_inomapdep: dependency %p for new"
5040 "inode already exists", inodedep);
5041 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap);
5043 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps);
5044 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref,
5047 inodedep->id_state |= ONDEPLIST;
5048 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
5050 inodedep->id_bmsafemap = bmsafemap;
5051 inodedep->id_state &= ~DEPCOMPLETE;
5052 FREE_LOCK(ip->i_ump);
5056 * Called just after updating the cylinder group block to
5057 * allocate block or fragment.
5060 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags)
5061 struct buf *bp; /* buffer for cylgroup block with block map */
5062 struct mount *mp; /* filesystem doing allocation */
5063 ufs2_daddr_t newblkno; /* number of newly allocated block */
5064 int frags; /* Number of fragments. */
5065 int oldfrags; /* Previous number of fragments for extend. */
5067 struct newblk *newblk;
5068 struct bmsafemap *bmsafemap;
5069 struct jnewblk *jnewblk;
5070 struct ufsmount *ump;
5073 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5074 ("softdep_setup_blkmapdep called on non-softdep filesystem"));
5079 * Create a dependency for the newly allocated block.
5080 * Add it to the dependency list for the buffer holding
5081 * the cylinder group map from which it was allocated.
5083 if (MOUNTEDSUJ(mp)) {
5084 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS);
5085 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp);
5086 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list);
5087 jnewblk->jn_state = ATTACHED;
5088 jnewblk->jn_blkno = newblkno;
5089 jnewblk->jn_frags = frags;
5090 jnewblk->jn_oldfrags = oldfrags;
5098 cgp = (struct cg *)bp->b_data;
5099 blksfree = cg_blksfree(cgp);
5100 bno = dtogd(fs, jnewblk->jn_blkno);
5101 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags;
5103 if (isset(blksfree, bno + i))
5104 panic("softdep_setup_blkmapdep: "
5105 "free fragment %d from %d-%d "
5106 "state 0x%X dep %p", i,
5107 jnewblk->jn_oldfrags,
5117 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d",
5118 newblkno, frags, oldfrags);
5120 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0)
5121 panic("softdep_setup_blkmapdep: found block");
5122 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp,
5123 dtog(fs, newblkno), NULL);
5125 jnewblk->jn_dep = (struct worklist *)newblk;
5126 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps);
5128 newblk->nb_state |= ONDEPLIST;
5129 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
5131 newblk->nb_bmsafemap = bmsafemap;
5132 newblk->nb_jnewblk = jnewblk;
5136 #define BMSAFEMAP_HASH(ump, cg) \
5137 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size])
5140 bmsafemap_find(bmsafemaphd, cg, bmsafemapp)
5141 struct bmsafemap_hashhead *bmsafemaphd;
5143 struct bmsafemap **bmsafemapp;
5145 struct bmsafemap *bmsafemap;
5147 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash)
5148 if (bmsafemap->sm_cg == cg)
5151 *bmsafemapp = bmsafemap;
5160 * Find the bmsafemap associated with a cylinder group buffer.
5161 * If none exists, create one. The buffer must be locked when
5162 * this routine is called and this routine must be called with
5163 * the softdep lock held. To avoid giving up the lock while
5164 * allocating a new bmsafemap, a preallocated bmsafemap may be
5165 * provided. If it is provided but not needed, it is freed.
5167 static struct bmsafemap *
5168 bmsafemap_lookup(mp, bp, cg, newbmsafemap)
5172 struct bmsafemap *newbmsafemap;
5174 struct bmsafemap_hashhead *bmsafemaphd;
5175 struct bmsafemap *bmsafemap, *collision;
5176 struct worklist *wk;
5177 struct ufsmount *ump;
5181 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer"));
5182 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5183 if (wk->wk_type == D_BMSAFEMAP) {
5185 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5186 return (WK_BMSAFEMAP(wk));
5189 bmsafemaphd = BMSAFEMAP_HASH(ump, cg);
5190 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) {
5192 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP);
5196 bmsafemap = newbmsafemap;
5199 bmsafemap = malloc(sizeof(struct bmsafemap),
5200 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
5201 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
5204 bmsafemap->sm_buf = bp;
5205 LIST_INIT(&bmsafemap->sm_inodedephd);
5206 LIST_INIT(&bmsafemap->sm_inodedepwr);
5207 LIST_INIT(&bmsafemap->sm_newblkhd);
5208 LIST_INIT(&bmsafemap->sm_newblkwr);
5209 LIST_INIT(&bmsafemap->sm_jaddrefhd);
5210 LIST_INIT(&bmsafemap->sm_jnewblkhd);
5211 LIST_INIT(&bmsafemap->sm_freehd);
5212 LIST_INIT(&bmsafemap->sm_freewr);
5213 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) {
5214 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
5217 bmsafemap->sm_cg = cg;
5218 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash);
5219 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
5220 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
5225 * Direct block allocation dependencies.
5227 * When a new block is allocated, the corresponding disk locations must be
5228 * initialized (with zeros or new data) before the on-disk inode points to
5229 * them. Also, the freemap from which the block was allocated must be
5230 * updated (on disk) before the inode's pointer. These two dependencies are
5231 * independent of each other and are needed for all file blocks and indirect
5232 * blocks that are pointed to directly by the inode. Just before the
5233 * "in-core" version of the inode is updated with a newly allocated block
5234 * number, a procedure (below) is called to setup allocation dependency
5235 * structures. These structures are removed when the corresponding
5236 * dependencies are satisfied or when the block allocation becomes obsolete
5237 * (i.e., the file is deleted, the block is de-allocated, or the block is a
5238 * fragment that gets upgraded). All of these cases are handled in
5239 * procedures described later.
5241 * When a file extension causes a fragment to be upgraded, either to a larger
5242 * fragment or to a full block, the on-disk location may change (if the
5243 * previous fragment could not simply be extended). In this case, the old
5244 * fragment must be de-allocated, but not until after the inode's pointer has
5245 * been updated. In most cases, this is handled by later procedures, which
5246 * will construct a "freefrag" structure to be added to the workitem queue
5247 * when the inode update is complete (or obsolete). The main exception to
5248 * this is when an allocation occurs while a pending allocation dependency
5249 * (for the same block pointer) remains. This case is handled in the main
5250 * allocation dependency setup procedure by immediately freeing the
5251 * unreferenced fragments.
5254 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5255 struct inode *ip; /* inode to which block is being added */
5256 ufs_lbn_t off; /* block pointer within inode */
5257 ufs2_daddr_t newblkno; /* disk block number being added */
5258 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
5259 long newsize; /* size of new block */
5260 long oldsize; /* size of new block */
5261 struct buf *bp; /* bp for allocated block */
5263 struct allocdirect *adp, *oldadp;
5264 struct allocdirectlst *adphead;
5265 struct freefrag *freefrag;
5266 struct inodedep *inodedep;
5267 struct pagedep *pagedep;
5268 struct jnewblk *jnewblk;
5269 struct newblk *newblk;
5274 mp = UFSTOVFS(ip->i_ump);
5275 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5276 ("softdep_setup_allocdirect called on non-softdep filesystem"));
5277 if (oldblkno && oldblkno != newblkno)
5278 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn);
5283 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd "
5284 "off %jd newsize %ld oldsize %d",
5285 ip->i_number, newblkno, oldblkno, off, newsize, oldsize);
5286 ACQUIRE_LOCK(ip->i_ump);
5287 if (off >= NDADDR) {
5289 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd",
5291 /* allocating an indirect block */
5293 panic("softdep_setup_allocdirect: non-zero indir");
5296 panic("softdep_setup_allocdirect: lbn %jd != off %jd",
5299 * Allocating a direct block.
5301 * If we are allocating a directory block, then we must
5302 * allocate an associated pagedep to track additions and
5305 if ((ip->i_mode & IFMT) == IFDIR)
5306 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC,
5309 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5310 panic("softdep_setup_allocdirect: lost block");
5311 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5312 ("softdep_setup_allocdirect: newblk already initialized"));
5314 * Convert the newblk to an allocdirect.
5316 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5317 adp = (struct allocdirect *)newblk;
5318 newblk->nb_freefrag = freefrag;
5319 adp->ad_offset = off;
5320 adp->ad_oldblkno = oldblkno;
5321 adp->ad_newsize = newsize;
5322 adp->ad_oldsize = oldsize;
5325 * Finish initializing the journal.
5327 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5328 jnewblk->jn_ino = ip->i_number;
5329 jnewblk->jn_lbn = lbn;
5330 add_to_journal(&jnewblk->jn_list);
5332 if (freefrag && freefrag->ff_jdep != NULL &&
5333 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5334 add_to_journal(freefrag->ff_jdep);
5335 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5336 adp->ad_inodedep = inodedep;
5338 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5340 * The list of allocdirects must be kept in sorted and ascending
5341 * order so that the rollback routines can quickly determine the
5342 * first uncommitted block (the size of the file stored on disk
5343 * ends at the end of the lowest committed fragment, or if there
5344 * are no fragments, at the end of the highest committed block).
5345 * Since files generally grow, the typical case is that the new
5346 * block is to be added at the end of the list. We speed this
5347 * special case by checking against the last allocdirect in the
5348 * list before laboriously traversing the list looking for the
5351 adphead = &inodedep->id_newinoupdt;
5352 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5353 if (oldadp == NULL || oldadp->ad_offset <= off) {
5354 /* insert at end of list */
5355 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5356 if (oldadp != NULL && oldadp->ad_offset == off)
5357 allocdirect_merge(adphead, adp, oldadp);
5358 FREE_LOCK(ip->i_ump);
5361 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5362 if (oldadp->ad_offset >= off)
5366 panic("softdep_setup_allocdirect: lost entry");
5367 /* insert in middle of list */
5368 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5369 if (oldadp->ad_offset == off)
5370 allocdirect_merge(adphead, adp, oldadp);
5372 FREE_LOCK(ip->i_ump);
5376 * Merge a newer and older journal record to be stored either in a
5377 * newblock or freefrag. This handles aggregating journal records for
5378 * fragment allocation into a second record as well as replacing a
5379 * journal free with an aborted journal allocation. A segment for the
5380 * oldest record will be placed on wkhd if it has been written. If not
5381 * the segment for the newer record will suffice.
5383 static struct worklist *
5384 jnewblk_merge(new, old, wkhd)
5385 struct worklist *new;
5386 struct worklist *old;
5387 struct workhead *wkhd;
5389 struct jnewblk *njnewblk;
5390 struct jnewblk *jnewblk;
5392 /* Handle NULLs to simplify callers. */
5397 /* Replace a jfreefrag with a jnewblk. */
5398 if (new->wk_type == D_JFREEFRAG) {
5399 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno)
5400 panic("jnewblk_merge: blkno mismatch: %p, %p",
5402 cancel_jfreefrag(WK_JFREEFRAG(new));
5405 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK)
5406 panic("jnewblk_merge: Bad type: old %d new %d\n",
5407 old->wk_type, new->wk_type);
5409 * Handle merging of two jnewblk records that describe
5410 * different sets of fragments in the same block.
5412 jnewblk = WK_JNEWBLK(old);
5413 njnewblk = WK_JNEWBLK(new);
5414 if (jnewblk->jn_blkno != njnewblk->jn_blkno)
5415 panic("jnewblk_merge: Merging disparate blocks.");
5417 * The record may be rolled back in the cg.
5419 if (jnewblk->jn_state & UNDONE) {
5420 jnewblk->jn_state &= ~UNDONE;
5421 njnewblk->jn_state |= UNDONE;
5422 njnewblk->jn_state &= ~ATTACHED;
5425 * We modify the newer addref and free the older so that if neither
5426 * has been written the most up-to-date copy will be on disk. If
5427 * both have been written but rolled back we only temporarily need
5428 * one of them to fix the bits when the cg write completes.
5430 jnewblk->jn_state |= ATTACHED | COMPLETE;
5431 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags;
5432 cancel_jnewblk(jnewblk, wkhd);
5433 WORKLIST_REMOVE(&jnewblk->jn_list);
5434 free_jnewblk(jnewblk);
5439 * Replace an old allocdirect dependency with a newer one.
5440 * This routine must be called with splbio interrupts blocked.
5443 allocdirect_merge(adphead, newadp, oldadp)
5444 struct allocdirectlst *adphead; /* head of list holding allocdirects */
5445 struct allocdirect *newadp; /* allocdirect being added */
5446 struct allocdirect *oldadp; /* existing allocdirect being checked */
5448 struct worklist *wk;
5449 struct freefrag *freefrag;
5452 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp));
5453 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
5454 newadp->ad_oldsize != oldadp->ad_newsize ||
5455 newadp->ad_offset >= NDADDR)
5456 panic("%s %jd != new %jd || old size %ld != new %ld",
5457 "allocdirect_merge: old blkno",
5458 (intmax_t)newadp->ad_oldblkno,
5459 (intmax_t)oldadp->ad_newblkno,
5460 newadp->ad_oldsize, oldadp->ad_newsize);
5461 newadp->ad_oldblkno = oldadp->ad_oldblkno;
5462 newadp->ad_oldsize = oldadp->ad_oldsize;
5464 * If the old dependency had a fragment to free or had never
5465 * previously had a block allocated, then the new dependency
5466 * can immediately post its freefrag and adopt the old freefrag.
5467 * This action is done by swapping the freefrag dependencies.
5468 * The new dependency gains the old one's freefrag, and the
5469 * old one gets the new one and then immediately puts it on
5470 * the worklist when it is freed by free_newblk. It is
5471 * not possible to do this swap when the old dependency had a
5472 * non-zero size but no previous fragment to free. This condition
5473 * arises when the new block is an extension of the old block.
5474 * Here, the first part of the fragment allocated to the new
5475 * dependency is part of the block currently claimed on disk by
5476 * the old dependency, so cannot legitimately be freed until the
5477 * conditions for the new dependency are fulfilled.
5479 freefrag = newadp->ad_freefrag;
5480 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
5481 newadp->ad_freefrag = oldadp->ad_freefrag;
5482 oldadp->ad_freefrag = freefrag;
5485 * If we are tracking a new directory-block allocation,
5486 * move it from the old allocdirect to the new allocdirect.
5488 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
5489 WORKLIST_REMOVE(wk);
5490 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
5491 panic("allocdirect_merge: extra newdirblk");
5492 WORKLIST_INSERT(&newadp->ad_newdirblk, wk);
5494 TAILQ_REMOVE(adphead, oldadp, ad_next);
5496 * We need to move any journal dependencies over to the freefrag
5497 * that releases this block if it exists. Otherwise we are
5498 * extending an existing block and we'll wait until that is
5499 * complete to release the journal space and extend the
5500 * new journal to cover this old space as well.
5502 if (freefrag == NULL) {
5503 if (oldadp->ad_newblkno != newadp->ad_newblkno)
5504 panic("allocdirect_merge: %jd != %jd",
5505 oldadp->ad_newblkno, newadp->ad_newblkno);
5506 newadp->ad_block.nb_jnewblk = (struct jnewblk *)
5507 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list,
5508 &oldadp->ad_block.nb_jnewblk->jn_list,
5509 &newadp->ad_block.nb_jwork);
5510 oldadp->ad_block.nb_jnewblk = NULL;
5511 cancel_newblk(&oldadp->ad_block, NULL,
5512 &newadp->ad_block.nb_jwork);
5514 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block,
5515 &freefrag->ff_list, &freefrag->ff_jwork);
5516 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk,
5517 &freefrag->ff_jwork);
5519 free_newblk(&oldadp->ad_block);
5523 * Allocate a jfreefrag structure to journal a single block free.
5525 static struct jfreefrag *
5526 newjfreefrag(freefrag, ip, blkno, size, lbn)
5527 struct freefrag *freefrag;
5533 struct jfreefrag *jfreefrag;
5537 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG,
5539 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump));
5540 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list);
5541 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE;
5542 jfreefrag->fr_ino = ip->i_number;
5543 jfreefrag->fr_lbn = lbn;
5544 jfreefrag->fr_blkno = blkno;
5545 jfreefrag->fr_frags = numfrags(fs, size);
5546 jfreefrag->fr_freefrag = freefrag;
5552 * Allocate a new freefrag structure.
5554 static struct freefrag *
5555 newfreefrag(ip, blkno, size, lbn)
5561 struct freefrag *freefrag;
5564 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd",
5565 ip->i_number, blkno, size, lbn);
5567 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
5568 panic("newfreefrag: frag size");
5569 freefrag = malloc(sizeof(struct freefrag),
5570 M_FREEFRAG, M_SOFTDEP_FLAGS);
5571 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump));
5572 freefrag->ff_state = ATTACHED;
5573 LIST_INIT(&freefrag->ff_jwork);
5574 freefrag->ff_inum = ip->i_number;
5575 freefrag->ff_vtype = ITOV(ip)->v_type;
5576 freefrag->ff_blkno = blkno;
5577 freefrag->ff_fragsize = size;
5579 if (MOUNTEDSUJ(UFSTOVFS(ip->i_ump))) {
5580 freefrag->ff_jdep = (struct worklist *)
5581 newjfreefrag(freefrag, ip, blkno, size, lbn);
5583 freefrag->ff_state |= DEPCOMPLETE;
5584 freefrag->ff_jdep = NULL;
5591 * This workitem de-allocates fragments that were replaced during
5592 * file block allocation.
5595 handle_workitem_freefrag(freefrag)
5596 struct freefrag *freefrag;
5598 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
5599 struct workhead wkhd;
5602 "handle_workitem_freefrag: ino %d blkno %jd size %ld",
5603 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize);
5605 * It would be illegal to add new completion items to the
5606 * freefrag after it was schedule to be done so it must be
5607 * safe to modify the list head here.
5611 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list);
5613 * If the journal has not been written we must cancel it here.
5615 if (freefrag->ff_jdep) {
5616 if (freefrag->ff_jdep->wk_type != D_JNEWBLK)
5617 panic("handle_workitem_freefrag: Unexpected type %d\n",
5618 freefrag->ff_jdep->wk_type);
5619 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd);
5622 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
5623 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd);
5625 WORKITEM_FREE(freefrag, D_FREEFRAG);
5630 * Set up a dependency structure for an external attributes data block.
5631 * This routine follows much of the structure of softdep_setup_allocdirect.
5632 * See the description of softdep_setup_allocdirect above for details.
5635 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp)
5638 ufs2_daddr_t newblkno;
5639 ufs2_daddr_t oldblkno;
5644 struct allocdirect *adp, *oldadp;
5645 struct allocdirectlst *adphead;
5646 struct freefrag *freefrag;
5647 struct inodedep *inodedep;
5648 struct jnewblk *jnewblk;
5649 struct newblk *newblk;
5653 mp = UFSTOVFS(ip->i_ump);
5654 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5655 ("softdep_setup_allocext called on non-softdep filesystem"));
5656 KASSERT(off < NXADDR, ("softdep_setup_allocext: lbn %lld > NXADDR",
5660 if (oldblkno && oldblkno != newblkno)
5661 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn);
5665 ACQUIRE_LOCK(ip->i_ump);
5666 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0)
5667 panic("softdep_setup_allocext: lost block");
5668 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5669 ("softdep_setup_allocext: newblk already initialized"));
5671 * Convert the newblk to an allocdirect.
5673 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT);
5674 adp = (struct allocdirect *)newblk;
5675 newblk->nb_freefrag = freefrag;
5676 adp->ad_offset = off;
5677 adp->ad_oldblkno = oldblkno;
5678 adp->ad_newsize = newsize;
5679 adp->ad_oldsize = oldsize;
5680 adp->ad_state |= EXTDATA;
5683 * Finish initializing the journal.
5685 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5686 jnewblk->jn_ino = ip->i_number;
5687 jnewblk->jn_lbn = lbn;
5688 add_to_journal(&jnewblk->jn_list);
5690 if (freefrag && freefrag->ff_jdep != NULL &&
5691 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5692 add_to_journal(freefrag->ff_jdep);
5693 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5694 adp->ad_inodedep = inodedep;
5696 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list);
5698 * The list of allocdirects must be kept in sorted and ascending
5699 * order so that the rollback routines can quickly determine the
5700 * first uncommitted block (the size of the file stored on disk
5701 * ends at the end of the lowest committed fragment, or if there
5702 * are no fragments, at the end of the highest committed block).
5703 * Since files generally grow, the typical case is that the new
5704 * block is to be added at the end of the list. We speed this
5705 * special case by checking against the last allocdirect in the
5706 * list before laboriously traversing the list looking for the
5709 adphead = &inodedep->id_newextupdt;
5710 oldadp = TAILQ_LAST(adphead, allocdirectlst);
5711 if (oldadp == NULL || oldadp->ad_offset <= off) {
5712 /* insert at end of list */
5713 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
5714 if (oldadp != NULL && oldadp->ad_offset == off)
5715 allocdirect_merge(adphead, adp, oldadp);
5716 FREE_LOCK(ip->i_ump);
5719 TAILQ_FOREACH(oldadp, adphead, ad_next) {
5720 if (oldadp->ad_offset >= off)
5724 panic("softdep_setup_allocext: lost entry");
5725 /* insert in middle of list */
5726 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
5727 if (oldadp->ad_offset == off)
5728 allocdirect_merge(adphead, adp, oldadp);
5729 FREE_LOCK(ip->i_ump);
5733 * Indirect block allocation dependencies.
5735 * The same dependencies that exist for a direct block also exist when
5736 * a new block is allocated and pointed to by an entry in a block of
5737 * indirect pointers. The undo/redo states described above are also
5738 * used here. Because an indirect block contains many pointers that
5739 * may have dependencies, a second copy of the entire in-memory indirect
5740 * block is kept. The buffer cache copy is always completely up-to-date.
5741 * The second copy, which is used only as a source for disk writes,
5742 * contains only the safe pointers (i.e., those that have no remaining
5743 * update dependencies). The second copy is freed when all pointers
5744 * are safe. The cache is not allowed to replace indirect blocks with
5745 * pending update dependencies. If a buffer containing an indirect
5746 * block with dependencies is written, these routines will mark it
5747 * dirty again. It can only be successfully written once all the
5748 * dependencies are removed. The ffs_fsync routine in conjunction with
5749 * softdep_sync_metadata work together to get all the dependencies
5750 * removed so that a file can be successfully written to disk. Three
5751 * procedures are used when setting up indirect block pointer
5752 * dependencies. The division is necessary because of the organization
5753 * of the "balloc" routine and because of the distinction between file
5754 * pages and file metadata blocks.
5758 * Allocate a new allocindir structure.
5760 static struct allocindir *
5761 newallocindir(ip, ptrno, newblkno, oldblkno, lbn)
5762 struct inode *ip; /* inode for file being extended */
5763 int ptrno; /* offset of pointer in indirect block */
5764 ufs2_daddr_t newblkno; /* disk block number being added */
5765 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
5768 struct newblk *newblk;
5769 struct allocindir *aip;
5770 struct freefrag *freefrag;
5771 struct jnewblk *jnewblk;
5774 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn);
5777 ACQUIRE_LOCK(ip->i_ump);
5778 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0)
5779 panic("new_allocindir: lost block");
5780 KASSERT(newblk->nb_list.wk_type == D_NEWBLK,
5781 ("newallocindir: newblk already initialized"));
5782 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR);
5783 newblk->nb_freefrag = freefrag;
5784 aip = (struct allocindir *)newblk;
5785 aip->ai_offset = ptrno;
5786 aip->ai_oldblkno = oldblkno;
5788 if ((jnewblk = newblk->nb_jnewblk) != NULL) {
5789 jnewblk->jn_ino = ip->i_number;
5790 jnewblk->jn_lbn = lbn;
5791 add_to_journal(&jnewblk->jn_list);
5793 if (freefrag && freefrag->ff_jdep != NULL &&
5794 freefrag->ff_jdep->wk_type == D_JFREEFRAG)
5795 add_to_journal(freefrag->ff_jdep);
5800 * Called just before setting an indirect block pointer
5801 * to a newly allocated file page.
5804 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
5805 struct inode *ip; /* inode for file being extended */
5806 ufs_lbn_t lbn; /* allocated block number within file */
5807 struct buf *bp; /* buffer with indirect blk referencing page */
5808 int ptrno; /* offset of pointer in indirect block */
5809 ufs2_daddr_t newblkno; /* disk block number being added */
5810 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
5811 struct buf *nbp; /* buffer holding allocated page */
5813 struct inodedep *inodedep;
5814 struct freefrag *freefrag;
5815 struct allocindir *aip;
5816 struct pagedep *pagedep;
5819 mp = UFSTOVFS(ip->i_ump);
5820 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
5821 ("softdep_setup_allocindir_page called on non-softdep filesystem"));
5822 KASSERT(lbn == nbp->b_lblkno,
5823 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd",
5824 lbn, bp->b_lblkno));
5826 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd "
5827 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn);
5828 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
5829 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn);
5830 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
5832 * If we are allocating a directory page, then we must
5833 * allocate an associated pagedep to track additions and
5836 if ((ip->i_mode & IFMT) == IFDIR)
5837 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep);
5838 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5839 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn);
5840 FREE_LOCK(ip->i_ump);
5842 handle_workitem_freefrag(freefrag);
5846 * Called just before setting an indirect block pointer to a
5847 * newly allocated indirect block.
5850 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
5851 struct buf *nbp; /* newly allocated indirect block */
5852 struct inode *ip; /* inode for file being extended */
5853 struct buf *bp; /* indirect block referencing allocated block */
5854 int ptrno; /* offset of pointer in indirect block */
5855 ufs2_daddr_t newblkno; /* disk block number being added */
5857 struct inodedep *inodedep;
5858 struct allocindir *aip;
5861 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
5862 ("softdep_setup_allocindir_meta called on non-softdep filesystem"));
5864 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d",
5865 ip->i_number, newblkno, ptrno);
5866 lbn = nbp->b_lblkno;
5867 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
5868 aip = newallocindir(ip, ptrno, newblkno, 0, lbn);
5869 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC,
5871 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list);
5872 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn))
5873 panic("softdep_setup_allocindir_meta: Block already existed");
5874 FREE_LOCK(ip->i_ump);
5878 indirdep_complete(indirdep)
5879 struct indirdep *indirdep;
5881 struct allocindir *aip;
5883 LIST_REMOVE(indirdep, ir_next);
5884 indirdep->ir_state |= DEPCOMPLETE;
5886 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) {
5887 LIST_REMOVE(aip, ai_next);
5888 free_newblk(&aip->ai_block);
5891 * If this indirdep is not attached to a buf it was simply waiting
5892 * on completion to clear completehd. free_indirdep() asserts
5893 * that nothing is dangling.
5895 if ((indirdep->ir_state & ONWORKLIST) == 0)
5896 free_indirdep(indirdep);
5899 static struct indirdep *
5900 indirdep_lookup(mp, ip, bp)
5905 struct indirdep *indirdep, *newindirdep;
5906 struct newblk *newblk;
5907 struct ufsmount *ump;
5908 struct worklist *wk;
5918 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5919 if (wk->wk_type != D_INDIRDEP)
5921 indirdep = WK_INDIRDEP(wk);
5924 /* Found on the buffer worklist, no new structure to free. */
5925 if (indirdep != NULL && newindirdep == NULL)
5927 if (indirdep != NULL && newindirdep != NULL)
5928 panic("indirdep_lookup: simultaneous create");
5929 /* None found on the buffer and a new structure is ready. */
5930 if (indirdep == NULL && newindirdep != NULL)
5932 /* None found and no new structure available. */
5934 newindirdep = malloc(sizeof(struct indirdep),
5935 M_INDIRDEP, M_SOFTDEP_FLAGS);
5936 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp);
5937 newindirdep->ir_state = ATTACHED;
5938 if (ip->i_ump->um_fstype == UFS1)
5939 newindirdep->ir_state |= UFS1FMT;
5940 TAILQ_INIT(&newindirdep->ir_trunc);
5941 newindirdep->ir_saveddata = NULL;
5942 LIST_INIT(&newindirdep->ir_deplisthd);
5943 LIST_INIT(&newindirdep->ir_donehd);
5944 LIST_INIT(&newindirdep->ir_writehd);
5945 LIST_INIT(&newindirdep->ir_completehd);
5946 if (bp->b_blkno == bp->b_lblkno) {
5947 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
5949 bp->b_blkno = blkno;
5951 newindirdep->ir_freeblks = NULL;
5952 newindirdep->ir_savebp =
5953 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
5954 newindirdep->ir_bp = bp;
5955 BUF_KERNPROC(newindirdep->ir_savebp);
5956 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
5959 indirdep = newindirdep;
5960 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
5962 * If the block is not yet allocated we don't set DEPCOMPLETE so
5963 * that we don't free dependencies until the pointers are valid.
5964 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather
5965 * than using the hash.
5967 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk))
5968 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next);
5970 indirdep->ir_state |= DEPCOMPLETE;
5975 * Called to finish the allocation of the "aip" allocated
5976 * by one of the two routines above.
5978 static struct freefrag *
5979 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)
5980 struct buf *bp; /* in-memory copy of the indirect block */
5981 struct inode *ip; /* inode for file being extended */
5982 struct inodedep *inodedep; /* Inodedep for ip */
5983 struct allocindir *aip; /* allocindir allocated by the above routines */
5984 ufs_lbn_t lbn; /* Logical block number for this block. */
5987 struct indirdep *indirdep;
5988 struct allocindir *oldaip;
5989 struct freefrag *freefrag;
5992 LOCK_OWNED(ip->i_ump);
5993 mp = UFSTOVFS(ip->i_ump);
5995 if (bp->b_lblkno >= 0)
5996 panic("setup_allocindir_phase2: not indir blk");
5997 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs),
5998 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset));
5999 indirdep = indirdep_lookup(mp, ip, bp);
6000 KASSERT(indirdep->ir_savebp != NULL,
6001 ("setup_allocindir_phase2 NULL ir_savebp"));
6002 aip->ai_indirdep = indirdep;
6004 * Check for an unwritten dependency for this indirect offset. If
6005 * there is, merge the old dependency into the new one. This happens
6006 * as a result of reallocblk only.
6009 if (aip->ai_oldblkno != 0) {
6010 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) {
6011 if (oldaip->ai_offset == aip->ai_offset) {
6012 freefrag = allocindir_merge(aip, oldaip);
6016 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) {
6017 if (oldaip->ai_offset == aip->ai_offset) {
6018 freefrag = allocindir_merge(aip, oldaip);
6024 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
6029 * Merge two allocindirs which refer to the same block. Move newblock
6030 * dependencies and setup the freefrags appropriately.
6032 static struct freefrag *
6033 allocindir_merge(aip, oldaip)
6034 struct allocindir *aip;
6035 struct allocindir *oldaip;
6037 struct freefrag *freefrag;
6038 struct worklist *wk;
6040 if (oldaip->ai_newblkno != aip->ai_oldblkno)
6041 panic("allocindir_merge: blkno");
6042 aip->ai_oldblkno = oldaip->ai_oldblkno;
6043 freefrag = aip->ai_freefrag;
6044 aip->ai_freefrag = oldaip->ai_freefrag;
6045 oldaip->ai_freefrag = NULL;
6046 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag"));
6048 * If we are tracking a new directory-block allocation,
6049 * move it from the old allocindir to the new allocindir.
6051 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) {
6052 WORKLIST_REMOVE(wk);
6053 if (!LIST_EMPTY(&oldaip->ai_newdirblk))
6054 panic("allocindir_merge: extra newdirblk");
6055 WORKLIST_INSERT(&aip->ai_newdirblk, wk);
6058 * We can skip journaling for this freefrag and just complete
6059 * any pending journal work for the allocindir that is being
6060 * removed after the freefrag completes.
6062 if (freefrag->ff_jdep)
6063 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep));
6064 LIST_REMOVE(oldaip, ai_next);
6065 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block,
6066 &freefrag->ff_list, &freefrag->ff_jwork);
6067 free_newblk(&oldaip->ai_block);
6073 setup_freedirect(freeblks, ip, i, needj)
6074 struct freeblks *freeblks;
6082 blkno = DIP(ip, i_db[i]);
6085 DIP_SET(ip, i_db[i], 0);
6086 frags = sblksize(ip->i_fs, ip->i_size, i);
6087 frags = numfrags(ip->i_fs, frags);
6088 newfreework(ip->i_ump, freeblks, NULL, i, blkno, frags, 0, needj);
6092 setup_freeext(freeblks, ip, i, needj)
6093 struct freeblks *freeblks;
6101 blkno = ip->i_din2->di_extb[i];
6104 ip->i_din2->di_extb[i] = 0;
6105 frags = sblksize(ip->i_fs, ip->i_din2->di_extsize, i);
6106 frags = numfrags(ip->i_fs, frags);
6107 newfreework(ip->i_ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj);
6111 setup_freeindir(freeblks, ip, i, lbn, needj)
6112 struct freeblks *freeblks;
6120 blkno = DIP(ip, i_ib[i]);
6123 DIP_SET(ip, i_ib[i], 0);
6124 newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, ip->i_fs->fs_frag,
6128 static inline struct freeblks *
6133 struct freeblks *freeblks;
6135 freeblks = malloc(sizeof(struct freeblks),
6136 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
6137 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
6138 LIST_INIT(&freeblks->fb_jblkdephd);
6139 LIST_INIT(&freeblks->fb_jwork);
6140 freeblks->fb_ref = 0;
6141 freeblks->fb_cgwait = 0;
6142 freeblks->fb_state = ATTACHED;
6143 freeblks->fb_uid = ip->i_uid;
6144 freeblks->fb_inum = ip->i_number;
6145 freeblks->fb_vtype = ITOV(ip)->v_type;
6146 freeblks->fb_modrev = DIP(ip, i_modrev);
6147 freeblks->fb_devvp = ip->i_devvp;
6148 freeblks->fb_chkcnt = 0;
6149 freeblks->fb_len = 0;
6155 trunc_indirdep(indirdep, freeblks, bp, off)
6156 struct indirdep *indirdep;
6157 struct freeblks *freeblks;
6161 struct allocindir *aip, *aipn;
6164 * The first set of allocindirs won't be in savedbp.
6166 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn)
6167 if (aip->ai_offset > off)
6168 cancel_allocindir(aip, bp, freeblks, 1);
6169 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn)
6170 if (aip->ai_offset > off)
6171 cancel_allocindir(aip, bp, freeblks, 1);
6173 * These will exist in savedbp.
6175 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn)
6176 if (aip->ai_offset > off)
6177 cancel_allocindir(aip, NULL, freeblks, 0);
6178 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn)
6179 if (aip->ai_offset > off)
6180 cancel_allocindir(aip, NULL, freeblks, 0);
6184 * Follow the chain of indirects down to lastlbn creating a freework
6185 * structure for each. This will be used to start indir_trunc() at
6186 * the right offset and create the journal records for the parrtial
6187 * truncation. A second step will handle the truncated dependencies.
6190 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno)
6191 struct freeblks *freeblks;
6197 struct indirdep *indirdep;
6198 struct indirdep *indirn;
6199 struct freework *freework;
6200 struct newblk *newblk;
6214 mp = freeblks->fb_list.wk_mp;
6215 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0);
6216 if ((bp->b_flags & B_CACHE) == 0) {
6217 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno);
6218 bp->b_iocmd = BIO_READ;
6219 bp->b_flags &= ~B_INVAL;
6220 bp->b_ioflags &= ~BIO_ERROR;
6221 vfs_busy_pages(bp, 0);
6222 bp->b_iooffset = dbtob(bp->b_blkno);
6224 curthread->td_ru.ru_inblock++;
6225 error = bufwait(bp);
6231 level = lbn_level(lbn);
6232 lbnadd = lbn_offset(ip->i_fs, level);
6234 * Compute the offset of the last block we want to keep. Store
6235 * in the freework the first block we want to completely free.
6237 off = (lastlbn - -(lbn + level)) / lbnadd;
6238 if (off + 1 == NINDIR(ip->i_fs))
6240 freework = newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, 0, off+1,
6243 * Link the freework into the indirdep. This will prevent any new
6244 * allocations from proceeding until we are finished with the
6245 * truncate and the block is written.
6247 ACQUIRE_LOCK(ip->i_ump);
6248 indirdep = indirdep_lookup(mp, ip, bp);
6249 if (indirdep->ir_freeblks)
6250 panic("setup_trunc_indir: indirdep already truncated.");
6251 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next);
6252 freework->fw_indir = indirdep;
6254 * Cancel any allocindirs that will not make it to disk.
6255 * We have to do this for all copies of the indirdep that
6256 * live on this newblk.
6258 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
6259 newblk_lookup(mp, dbtofsb(ip->i_fs, bp->b_blkno), 0, &newblk);
6260 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next)
6261 trunc_indirdep(indirn, freeblks, bp, off);
6263 trunc_indirdep(indirdep, freeblks, bp, off);
6264 FREE_LOCK(ip->i_ump);
6266 * Creation is protected by the buf lock. The saveddata is only
6267 * needed if a full truncation follows a partial truncation but it
6268 * is difficult to allocate in that case so we fetch it anyway.
6270 if (indirdep->ir_saveddata == NULL)
6271 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
6274 /* Fetch the blkno of the child and the zero start offset. */
6275 if (ip->i_ump->um_fstype == UFS1) {
6276 blkno = ((ufs1_daddr_t *)bp->b_data)[off];
6277 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1];
6279 blkno = ((ufs2_daddr_t *)bp->b_data)[off];
6280 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1];
6283 /* Zero the truncated pointers. */
6284 end = bp->b_data + bp->b_bcount;
6285 bzero(start, end - start);
6291 lbn++; /* adjust level */
6292 lbn -= (off * lbnadd);
6293 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno);
6297 * Complete the partial truncation of an indirect block setup by
6298 * setup_trunc_indir(). This zeros the truncated pointers in the saved
6299 * copy and writes them to disk before the freeblks is allowed to complete.
6302 complete_trunc_indir(freework)
6303 struct freework *freework;
6305 struct freework *fwn;
6306 struct indirdep *indirdep;
6307 struct ufsmount *ump;
6312 ump = VFSTOUFS(freework->fw_list.wk_mp);
6314 indirdep = freework->fw_indir;
6316 bp = indirdep->ir_bp;
6317 /* See if the block was discarded. */
6320 /* Inline part of getdirtybuf(). We dont want bremfree. */
6321 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0)
6323 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
6324 LOCK_PTR(ump)) == 0)
6328 freework->fw_state |= DEPCOMPLETE;
6329 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next);
6331 * Zero the pointers in the saved copy.
6333 if (indirdep->ir_state & UFS1FMT)
6334 start = sizeof(ufs1_daddr_t);
6336 start = sizeof(ufs2_daddr_t);
6337 start *= freework->fw_start;
6338 count = indirdep->ir_savebp->b_bcount - start;
6339 start += (uintptr_t)indirdep->ir_savebp->b_data;
6340 bzero((char *)start, count);
6342 * We need to start the next truncation in the list if it has not
6345 fwn = TAILQ_FIRST(&indirdep->ir_trunc);
6347 if (fwn->fw_freeblks == indirdep->ir_freeblks)
6348 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next);
6349 if ((fwn->fw_state & ONWORKLIST) == 0)
6350 freework_enqueue(fwn);
6353 * If bp is NULL the block was fully truncated, restore
6354 * the saved block list otherwise free it if it is no
6357 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
6359 bcopy(indirdep->ir_saveddata,
6360 indirdep->ir_savebp->b_data,
6361 indirdep->ir_savebp->b_bcount);
6362 free(indirdep->ir_saveddata, M_INDIRDEP);
6363 indirdep->ir_saveddata = NULL;
6366 * When bp is NULL there is a full truncation pending. We
6367 * must wait for this full truncation to be journaled before
6368 * we can release this freework because the disk pointers will
6369 * never be written as zero.
6372 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd))
6373 handle_written_freework(freework);
6375 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd,
6376 &freework->fw_list);
6378 /* Complete when the real copy is written. */
6379 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list);
6385 * Calculate the number of blocks we are going to release where datablocks
6386 * is the current total and length is the new file size.
6389 blkcount(fs, datablocks, length)
6391 ufs2_daddr_t datablocks;
6394 off_t totblks, numblks;
6397 numblks = howmany(length, fs->fs_bsize);
6398 if (numblks <= NDADDR) {
6399 totblks = howmany(length, fs->fs_fsize);
6402 totblks = blkstofrags(fs, numblks);
6405 * Count all single, then double, then triple indirects required.
6406 * Subtracting one indirects worth of blocks for each pass
6407 * acknowledges one of each pointed to by the inode.
6410 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs)));
6411 numblks -= NINDIR(fs);
6414 numblks = howmany(numblks, NINDIR(fs));
6417 totblks = fsbtodb(fs, totblks);
6419 * Handle sparse files. We can't reclaim more blocks than the inode
6420 * references. We will correct it later in handle_complete_freeblks()
6421 * when we know the real count.
6423 if (totblks > datablocks)
6425 return (datablocks - totblks);
6429 * Handle freeblocks for journaled softupdate filesystems.
6431 * Contrary to normal softupdates, we must preserve the block pointers in
6432 * indirects until their subordinates are free. This is to avoid journaling
6433 * every block that is freed which may consume more space than the journal
6434 * itself. The recovery program will see the free block journals at the
6435 * base of the truncated area and traverse them to reclaim space. The
6436 * pointers in the inode may be cleared immediately after the journal
6437 * records are written because each direct and indirect pointer in the
6438 * inode is recorded in a journal. This permits full truncation to proceed
6439 * asynchronously. The write order is journal -> inode -> cgs -> indirects.
6441 * The algorithm is as follows:
6442 * 1) Traverse the in-memory state and create journal entries to release
6443 * the relevant blocks and full indirect trees.
6444 * 2) Traverse the indirect block chain adding partial truncation freework
6445 * records to indirects in the path to lastlbn. The freework will
6446 * prevent new allocation dependencies from being satisfied in this
6447 * indirect until the truncation completes.
6448 * 3) Read and lock the inode block, performing an update with the new size
6449 * and pointers. This prevents truncated data from becoming valid on
6450 * disk through step 4.
6451 * 4) Reap unsatisfied dependencies that are beyond the truncated area,
6452 * eliminate journal work for those records that do not require it.
6453 * 5) Schedule the journal records to be written followed by the inode block.
6454 * 6) Allocate any necessary frags for the end of file.
6455 * 7) Zero any partially truncated blocks.
6457 * From this truncation proceeds asynchronously using the freework and
6458 * indir_trunc machinery. The file will not be extended again into a
6459 * partially truncated indirect block until all work is completed but
6460 * the normal dependency mechanism ensures that it is rolled back/forward
6461 * as appropriate. Further truncation may occur without delay and is
6462 * serialized in indir_trunc().
6465 softdep_journal_freeblocks(ip, cred, length, flags)
6466 struct inode *ip; /* The inode whose length is to be reduced */
6468 off_t length; /* The new length for the file */
6469 int flags; /* IO_EXT and/or IO_NORMAL */
6471 struct freeblks *freeblks, *fbn;
6472 struct worklist *wk, *wkn;
6473 struct inodedep *inodedep;
6474 struct jblkdep *jblkdep;
6475 struct allocdirect *adp, *adpn;
6476 struct ufsmount *ump;
6481 ufs2_daddr_t extblocks, datablocks;
6482 ufs_lbn_t tmpval, lbn, lastlbn;
6483 int frags, lastoff, iboff, allocblock, needj, error, i;
6488 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6489 ("softdep_journal_freeblocks called on non-softdep filesystem"));
6497 freeblks = newfreeblks(mp, ip);
6500 * If we're truncating a removed file that will never be written
6501 * we don't need to journal the block frees. The canceled journals
6502 * for the allocations will suffice.
6504 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6505 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED &&
6508 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d",
6509 ip->i_number, length, needj);
6512 * Calculate the lbn that we are truncating to. This results in -1
6513 * if we're truncating the 0 bytes. So it is the last lbn we want
6514 * to keep, not the first lbn we want to truncate.
6516 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1;
6517 lastoff = blkoff(fs, length);
6519 * Compute frags we are keeping in lastlbn. 0 means all.
6521 if (lastlbn >= 0 && lastlbn < NDADDR) {
6522 frags = fragroundup(fs, lastoff);
6523 /* adp offset of last valid allocdirect. */
6525 } else if (lastlbn > 0)
6527 if (fs->fs_magic == FS_UFS2_MAGIC)
6528 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6530 * Handle normal data blocks and indirects. This section saves
6531 * values used after the inode update to complete frag and indirect
6534 if ((flags & IO_NORMAL) != 0) {
6536 * Handle truncation of whole direct and indirect blocks.
6538 for (i = iboff + 1; i < NDADDR; i++)
6539 setup_freedirect(freeblks, ip, i, needj);
6540 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR;
6541 i++, lbn += tmpval, tmpval *= NINDIR(fs)) {
6542 /* Release a whole indirect tree. */
6543 if (lbn > lastlbn) {
6544 setup_freeindir(freeblks, ip, i, -lbn -i,
6550 * Traverse partially truncated indirect tree.
6552 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn)
6553 setup_trunc_indir(freeblks, ip, -lbn - i,
6554 lastlbn, DIP(ip, i_ib[i]));
6557 * Handle partial truncation to a frag boundary.
6563 oldfrags = blksize(fs, ip, lastlbn);
6564 blkno = DIP(ip, i_db[lastlbn]);
6565 if (blkno && oldfrags != frags) {
6567 oldfrags = numfrags(ip->i_fs, oldfrags);
6568 blkno += numfrags(ip->i_fs, frags);
6569 newfreework(ump, freeblks, NULL, lastlbn,
6570 blkno, oldfrags, 0, needj);
6572 adjust_newfreework(freeblks,
6573 numfrags(ip->i_fs, frags));
6574 } else if (blkno == 0)
6578 * Add a journal record for partial truncate if we are
6579 * handling indirect blocks. Non-indirects need no extra
6582 if (length != 0 && lastlbn >= NDADDR) {
6583 ip->i_flag |= IN_TRUNCATED;
6584 newjtrunc(freeblks, length, 0);
6586 ip->i_size = length;
6587 DIP_SET(ip, i_size, ip->i_size);
6588 datablocks = DIP(ip, i_blocks) - extblocks;
6590 datablocks = blkcount(ip->i_fs, datablocks, length);
6591 freeblks->fb_len = length;
6593 if ((flags & IO_EXT) != 0) {
6594 for (i = 0; i < NXADDR; i++)
6595 setup_freeext(freeblks, ip, i, needj);
6596 ip->i_din2->di_extsize = 0;
6597 datablocks += extblocks;
6600 /* Reference the quotas in case the block count is wrong in the end. */
6601 quotaref(vp, freeblks->fb_quota);
6602 (void) chkdq(ip, -datablocks, NOCRED, 0);
6604 freeblks->fb_chkcnt = -datablocks;
6606 fs->fs_pendingblocks += datablocks;
6608 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6610 * Handle truncation of incomplete alloc direct dependencies. We
6611 * hold the inode block locked to prevent incomplete dependencies
6612 * from reaching the disk while we are eliminating those that
6613 * have been truncated. This is a partially inlined ffs_update().
6616 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
6617 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6618 (int)fs->fs_bsize, cred, &bp);
6621 softdep_error("softdep_journal_freeblocks", error);
6624 if (bp->b_bufsize == fs->fs_bsize)
6625 bp->b_flags |= B_CLUSTEROK;
6626 softdep_update_inodeblock(ip, bp, 0);
6627 if (ump->um_fstype == UFS1)
6628 *((struct ufs1_dinode *)bp->b_data +
6629 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
6631 *((struct ufs2_dinode *)bp->b_data +
6632 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
6634 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6635 if ((inodedep->id_state & IOSTARTED) != 0)
6636 panic("softdep_setup_freeblocks: inode busy");
6638 * Add the freeblks structure to the list of operations that
6639 * must await the zero'ed inode being written to disk. If we
6640 * still have a bitmap dependency (needj), then the inode
6641 * has never been written to disk, so we can process the
6642 * freeblks below once we have deleted the dependencies.
6645 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6647 freeblks->fb_state |= COMPLETE;
6648 if ((flags & IO_NORMAL) != 0) {
6649 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) {
6650 if (adp->ad_offset > iboff)
6651 cancel_allocdirect(&inodedep->id_inoupdt, adp,
6654 * Truncate the allocdirect. We could eliminate
6655 * or modify journal records as well.
6657 else if (adp->ad_offset == iboff && frags)
6658 adp->ad_newsize = frags;
6661 if ((flags & IO_EXT) != 0)
6662 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
6663 cancel_allocdirect(&inodedep->id_extupdt, adp,
6666 * Scan the bufwait list for newblock dependencies that will never
6669 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) {
6670 if (wk->wk_type != D_ALLOCDIRECT)
6672 adp = WK_ALLOCDIRECT(wk);
6673 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) ||
6674 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) {
6675 cancel_jfreeblk(freeblks, adp->ad_newblkno);
6676 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork);
6677 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
6683 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps)
6684 add_to_journal(&jblkdep->jb_list);
6688 * Truncate dependency structures beyond length.
6690 trunc_dependencies(ip, freeblks, lastlbn, frags, flags);
6692 * This is only set when we need to allocate a fragment because
6693 * none existed at the end of a frag-sized file. It handles only
6694 * allocating a new, zero filled block.
6697 ip->i_size = length - lastoff;
6698 DIP_SET(ip, i_size, ip->i_size);
6699 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp);
6701 softdep_error("softdep_journal_freeblks", error);
6704 ip->i_size = length;
6705 DIP_SET(ip, i_size, length);
6706 ip->i_flag |= IN_CHANGE | IN_UPDATE;
6707 allocbuf(bp, frags);
6710 } else if (lastoff != 0 && vp->v_type != VDIR) {
6714 * Zero the end of a truncated frag or block.
6716 size = sblksize(fs, length, lastlbn);
6717 error = bread(vp, lastlbn, size, cred, &bp);
6719 softdep_error("softdep_journal_freeblks", error);
6722 bzero((char *)bp->b_data + lastoff, size - lastoff);
6727 inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6728 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next);
6729 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST;
6731 * We zero earlier truncations so they don't erroneously
6734 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0)
6735 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next)
6737 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE &&
6738 LIST_EMPTY(&freeblks->fb_jblkdephd))
6739 freeblks->fb_state |= INPROGRESS;
6744 handle_workitem_freeblocks(freeblks, 0);
6745 trunc_pages(ip, length, extblocks, flags);
6750 * Flush a JOP_SYNC to the journal.
6753 softdep_journal_fsync(ip)
6756 struct jfsync *jfsync;
6758 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
6759 ("softdep_journal_fsync called on non-softdep filesystem"));
6760 if ((ip->i_flag & IN_TRUNCATED) == 0)
6762 ip->i_flag &= ~IN_TRUNCATED;
6763 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO);
6764 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ip->i_ump));
6765 jfsync->jfs_size = ip->i_size;
6766 jfsync->jfs_ino = ip->i_number;
6767 ACQUIRE_LOCK(ip->i_ump);
6768 add_to_journal(&jfsync->jfs_list);
6769 jwait(&jfsync->jfs_list, MNT_WAIT);
6770 FREE_LOCK(ip->i_ump);
6774 * Block de-allocation dependencies.
6776 * When blocks are de-allocated, the on-disk pointers must be nullified before
6777 * the blocks are made available for use by other files. (The true
6778 * requirement is that old pointers must be nullified before new on-disk
6779 * pointers are set. We chose this slightly more stringent requirement to
6780 * reduce complexity.) Our implementation handles this dependency by updating
6781 * the inode (or indirect block) appropriately but delaying the actual block
6782 * de-allocation (i.e., freemap and free space count manipulation) until
6783 * after the updated versions reach stable storage. After the disk is
6784 * updated, the blocks can be safely de-allocated whenever it is convenient.
6785 * This implementation handles only the common case of reducing a file's
6786 * length to zero. Other cases are handled by the conventional synchronous
6789 * The ffs implementation with which we worked double-checks
6790 * the state of the block pointers and file size as it reduces
6791 * a file's length. Some of this code is replicated here in our
6792 * soft updates implementation. The freeblks->fb_chkcnt field is
6793 * used to transfer a part of this information to the procedure
6794 * that eventually de-allocates the blocks.
6796 * This routine should be called from the routine that shortens
6797 * a file's length, before the inode's size or block pointers
6798 * are modified. It will save the block pointer information for
6799 * later release and zero the inode so that the calling routine
6803 softdep_setup_freeblocks(ip, length, flags)
6804 struct inode *ip; /* The inode whose length is to be reduced */
6805 off_t length; /* The new length for the file */
6806 int flags; /* IO_EXT and/or IO_NORMAL */
6808 struct ufs1_dinode *dp1;
6809 struct ufs2_dinode *dp2;
6810 struct freeblks *freeblks;
6811 struct inodedep *inodedep;
6812 struct allocdirect *adp;
6813 struct ufsmount *ump;
6816 ufs2_daddr_t extblocks, datablocks;
6818 int i, delay, error;
6824 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
6825 ("softdep_setup_freeblocks called on non-softdep filesystem"));
6826 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld",
6827 ip->i_number, length);
6828 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length"));
6830 freeblks = newfreeblks(mp, ip);
6833 if (fs->fs_magic == FS_UFS2_MAGIC)
6834 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
6835 if ((flags & IO_NORMAL) != 0) {
6836 for (i = 0; i < NDADDR; i++)
6837 setup_freedirect(freeblks, ip, i, 0);
6838 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR;
6839 i++, lbn += tmpval, tmpval *= NINDIR(fs))
6840 setup_freeindir(freeblks, ip, i, -lbn -i, 0);
6842 DIP_SET(ip, i_size, 0);
6843 datablocks = DIP(ip, i_blocks) - extblocks;
6845 if ((flags & IO_EXT) != 0) {
6846 for (i = 0; i < NXADDR; i++)
6847 setup_freeext(freeblks, ip, i, 0);
6848 ip->i_din2->di_extsize = 0;
6849 datablocks += extblocks;
6852 /* Reference the quotas in case the block count is wrong in the end. */
6853 quotaref(ITOV(ip), freeblks->fb_quota);
6854 (void) chkdq(ip, -datablocks, NOCRED, 0);
6856 freeblks->fb_chkcnt = -datablocks;
6858 fs->fs_pendingblocks += datablocks;
6860 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks);
6862 * Push the zero'ed inode to to its disk buffer so that we are free
6863 * to delete its dependencies below. Once the dependencies are gone
6864 * the buffer can be safely released.
6866 if ((error = bread(ip->i_devvp,
6867 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
6868 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
6870 softdep_error("softdep_setup_freeblocks", error);
6872 if (ump->um_fstype == UFS1) {
6873 dp1 = ((struct ufs1_dinode *)bp->b_data +
6874 ino_to_fsbo(fs, ip->i_number));
6875 ip->i_din1->di_freelink = dp1->di_freelink;
6878 dp2 = ((struct ufs2_dinode *)bp->b_data +
6879 ino_to_fsbo(fs, ip->i_number));
6880 ip->i_din2->di_freelink = dp2->di_freelink;
6884 * Find and eliminate any inode dependencies.
6887 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
6888 if ((inodedep->id_state & IOSTARTED) != 0)
6889 panic("softdep_setup_freeblocks: inode busy");
6891 * Add the freeblks structure to the list of operations that
6892 * must await the zero'ed inode being written to disk. If we
6893 * still have a bitmap dependency (delay == 0), then the inode
6894 * has never been written to disk, so we can process the
6895 * freeblks below once we have deleted the dependencies.
6897 delay = (inodedep->id_state & DEPCOMPLETE);
6899 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list);
6901 freeblks->fb_state |= COMPLETE;
6903 * Because the file length has been truncated to zero, any
6904 * pending block allocation dependency structures associated
6905 * with this inode are obsolete and can simply be de-allocated.
6906 * We must first merge the two dependency lists to get rid of
6907 * any duplicate freefrag structures, then purge the merged list.
6908 * If we still have a bitmap dependency, then the inode has never
6909 * been written to disk, so we can free any fragments without delay.
6911 if (flags & IO_NORMAL) {
6912 merge_inode_lists(&inodedep->id_newinoupdt,
6913 &inodedep->id_inoupdt);
6914 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
6915 cancel_allocdirect(&inodedep->id_inoupdt, adp,
6918 if (flags & IO_EXT) {
6919 merge_inode_lists(&inodedep->id_newextupdt,
6920 &inodedep->id_extupdt);
6921 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
6922 cancel_allocdirect(&inodedep->id_extupdt, adp,
6927 trunc_dependencies(ip, freeblks, -1, 0, flags);
6929 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
6930 (void) free_inodedep(inodedep);
6931 freeblks->fb_state |= DEPCOMPLETE;
6933 * If the inode with zeroed block pointers is now on disk
6934 * we can start freeing blocks.
6936 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
6937 freeblks->fb_state |= INPROGRESS;
6942 handle_workitem_freeblocks(freeblks, 0);
6943 trunc_pages(ip, length, extblocks, flags);
6947 * Eliminate pages from the page cache that back parts of this inode and
6948 * adjust the vnode pager's idea of our size. This prevents stale data
6949 * from hanging around in the page cache.
6952 trunc_pages(ip, length, extblocks, flags)
6955 ufs2_daddr_t extblocks;
6965 extend = OFF_TO_IDX(lblktosize(fs, -extblocks));
6966 if ((flags & IO_EXT) != 0)
6967 vn_pages_remove(vp, extend, 0);
6968 if ((flags & IO_NORMAL) == 0)
6970 BO_LOCK(&vp->v_bufobj);
6972 BO_UNLOCK(&vp->v_bufobj);
6974 * The vnode pager eliminates file pages we eliminate indirects
6977 vnode_pager_setsize(vp, length);
6979 * Calculate the end based on the last indirect we want to keep. If
6980 * the block extends into indirects we can just use the negative of
6981 * its lbn. Doubles and triples exist at lower numbers so we must
6982 * be careful not to remove those, if they exist. double and triple
6983 * indirect lbns do not overlap with others so it is not important
6984 * to verify how many levels are required.
6986 lbn = lblkno(fs, length);
6987 if (lbn >= NDADDR) {
6988 /* Calculate the virtual lbn of the triple indirect. */
6989 lbn = -lbn - (NIADDR - 1);
6990 end = OFF_TO_IDX(lblktosize(fs, lbn));
6993 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end);
6997 * See if the buf bp is in the range eliminated by truncation.
7000 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags)
7010 /* Only match ext/normal blocks as appropriate. */
7011 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
7012 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0))
7014 /* ALTDATA is always a full truncation. */
7015 if ((bp->b_xflags & BX_ALTDATA) != 0)
7017 /* -1 is full truncation. */
7021 * If this is a partial truncate we only want those
7022 * blocks and indirect blocks that cover the range
7027 lbn = -(lbn + lbn_level(lbn));
7030 /* Here we only truncate lblkno if it's partial. */
7031 if (lbn == lastlbn) {
7040 * Eliminate any dependencies that exist in memory beyond lblkno:off
7043 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags)
7045 struct freeblks *freeblks;
7056 * We must wait for any I/O in progress to finish so that
7057 * all potential buffers on the dirty list will be visible.
7058 * Once they are all there, walk the list and get rid of
7065 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
7066 bp->b_vflags &= ~BV_SCANNED;
7068 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
7069 if (bp->b_vflags & BV_SCANNED)
7071 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7072 bp->b_vflags |= BV_SCANNED;
7075 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer"));
7076 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL)
7079 if (deallocate_dependencies(bp, freeblks, blkoff))
7087 * Now do the work of vtruncbuf while also matching indirect blocks.
7089 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs)
7090 bp->b_vflags &= ~BV_SCANNED;
7092 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) {
7093 if (bp->b_vflags & BV_SCANNED)
7095 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) {
7096 bp->b_vflags |= BV_SCANNED;
7100 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
7101 BO_LOCKPTR(bo)) == ENOLCK) {
7105 bp->b_vflags |= BV_SCANNED;
7108 allocbuf(bp, blkoff);
7111 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF;
7122 cancel_pagedep(pagedep, freeblks, blkoff)
7123 struct pagedep *pagedep;
7124 struct freeblks *freeblks;
7127 struct jremref *jremref;
7128 struct jmvref *jmvref;
7129 struct dirrem *dirrem, *tmp;
7133 * Copy any directory remove dependencies to the list
7134 * to be processed after the freeblks proceeds. If
7135 * directory entry never made it to disk they
7136 * can be dumped directly onto the work list.
7138 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) {
7139 /* Skip this directory removal if it is intended to remain. */
7140 if (dirrem->dm_offset < blkoff)
7143 * If there are any dirrems we wait for the journal write
7144 * to complete and then restart the buf scan as the lock
7147 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) {
7148 jwait(&jremref->jr_list, MNT_WAIT);
7151 LIST_REMOVE(dirrem, dm_next);
7152 dirrem->dm_dirinum = pagedep->pd_ino;
7153 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list);
7155 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) {
7156 jwait(&jmvref->jm_list, MNT_WAIT);
7160 * When we're partially truncating a pagedep we just want to flush
7161 * journal entries and return. There can not be any adds in the
7162 * truncated portion of the directory and newblk must remain if
7163 * part of the block remains.
7168 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
7169 if (dap->da_offset > blkoff)
7170 panic("cancel_pagedep: diradd %p off %d > %d",
7171 dap, dap->da_offset, blkoff);
7172 for (i = 0; i < DAHASHSZ; i++)
7173 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist)
7174 if (dap->da_offset > blkoff)
7175 panic("cancel_pagedep: diradd %p off %d > %d",
7176 dap, dap->da_offset, blkoff);
7180 * There should be no directory add dependencies present
7181 * as the directory could not be truncated until all
7182 * children were removed.
7184 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL,
7185 ("deallocate_dependencies: pendinghd != NULL"));
7186 for (i = 0; i < DAHASHSZ; i++)
7187 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL,
7188 ("deallocate_dependencies: diraddhd != NULL"));
7189 if ((pagedep->pd_state & NEWBLOCK) != 0)
7190 free_newdirblk(pagedep->pd_newdirblk);
7191 if (free_pagedep(pagedep) == 0)
7192 panic("Failed to free pagedep %p", pagedep);
7197 * Reclaim any dependency structures from a buffer that is about to
7198 * be reallocated to a new vnode. The buffer must be locked, thus,
7199 * no I/O completion operations can occur while we are manipulating
7200 * its associated dependencies. The mutex is held so that other I/O's
7201 * associated with related dependencies do not occur.
7204 deallocate_dependencies(bp, freeblks, off)
7206 struct freeblks *freeblks;
7209 struct indirdep *indirdep;
7210 struct pagedep *pagedep;
7211 struct worklist *wk, *wkn;
7212 struct ufsmount *ump;
7214 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL)
7216 ump = VFSTOUFS(wk->wk_mp);
7218 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) {
7219 switch (wk->wk_type) {
7221 indirdep = WK_INDIRDEP(wk);
7222 if (bp->b_lblkno >= 0 ||
7223 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
7224 panic("deallocate_dependencies: not indir");
7225 cancel_indirdep(indirdep, bp, freeblks);
7229 pagedep = WK_PAGEDEP(wk);
7230 if (cancel_pagedep(pagedep, freeblks, off)) {
7238 * Simply remove the allocindir, we'll find it via
7239 * the indirdep where we can clear pointers if
7242 WORKLIST_REMOVE(wk);
7247 * A truncation is waiting for the zero'd pointers
7248 * to be written. It can be freed when the freeblks
7251 WORKLIST_REMOVE(wk);
7252 wk->wk_state |= ONDEPLIST;
7253 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk);
7261 panic("deallocate_dependencies: Unexpected type %s",
7262 TYPENAME(wk->wk_type));
7269 * Don't throw away this buf, we were partially truncating and
7270 * some deps may always remain.
7274 bp->b_vflags |= BV_SCANNED;
7277 bp->b_flags |= B_INVAL | B_NOCACHE;
7283 * An allocdirect is being canceled due to a truncate. We must make sure
7284 * the journal entry is released in concert with the blkfree that releases
7285 * the storage. Completed journal entries must not be released until the
7286 * space is no longer pointed to by the inode or in the bitmap.
7289 cancel_allocdirect(adphead, adp, freeblks)
7290 struct allocdirectlst *adphead;
7291 struct allocdirect *adp;
7292 struct freeblks *freeblks;
7294 struct freework *freework;
7295 struct newblk *newblk;
7296 struct worklist *wk;
7298 TAILQ_REMOVE(adphead, adp, ad_next);
7299 newblk = (struct newblk *)adp;
7302 * Find the correct freework structure.
7304 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) {
7305 if (wk->wk_type != D_FREEWORK)
7307 freework = WK_FREEWORK(wk);
7308 if (freework->fw_blkno == newblk->nb_newblkno)
7311 if (freework == NULL)
7312 panic("cancel_allocdirect: Freework not found");
7314 * If a newblk exists at all we still have the journal entry that
7315 * initiated the allocation so we do not need to journal the free.
7317 cancel_jfreeblk(freeblks, freework->fw_blkno);
7319 * If the journal hasn't been written the jnewblk must be passed
7320 * to the call to ffs_blkfree that reclaims the space. We accomplish
7321 * this by linking the journal dependency into the freework to be
7322 * freed when freework_freeblock() is called. If the journal has
7323 * been written we can simply reclaim the journal space when the
7324 * freeblks work is complete.
7326 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list,
7327 &freeblks->fb_jwork);
7328 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
7333 * Cancel a new block allocation. May be an indirect or direct block. We
7334 * remove it from various lists and return any journal record that needs to
7335 * be resolved by the caller.
7337 * A special consideration is made for indirects which were never pointed
7338 * at on disk and will never be found once this block is released.
7340 static struct jnewblk *
7341 cancel_newblk(newblk, wk, wkhd)
7342 struct newblk *newblk;
7343 struct worklist *wk;
7344 struct workhead *wkhd;
7346 struct jnewblk *jnewblk;
7348 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno);
7350 newblk->nb_state |= GOINGAWAY;
7352 * Previously we traversed the completedhd on each indirdep
7353 * attached to this newblk to cancel them and gather journal
7354 * work. Since we need only the oldest journal segment and
7355 * the lowest point on the tree will always have the oldest
7356 * journal segment we are free to release the segments
7357 * of any subordinates and may leave the indirdep list to
7358 * indirdep_complete() when this newblk is freed.
7360 if (newblk->nb_state & ONDEPLIST) {
7361 newblk->nb_state &= ~ONDEPLIST;
7362 LIST_REMOVE(newblk, nb_deps);
7364 if (newblk->nb_state & ONWORKLIST)
7365 WORKLIST_REMOVE(&newblk->nb_list);
7367 * If the journal entry hasn't been written we save a pointer to
7368 * the dependency that frees it until it is written or the
7369 * superseding operation completes.
7371 jnewblk = newblk->nb_jnewblk;
7372 if (jnewblk != NULL && wk != NULL) {
7373 newblk->nb_jnewblk = NULL;
7374 jnewblk->jn_dep = wk;
7376 if (!LIST_EMPTY(&newblk->nb_jwork))
7377 jwork_move(wkhd, &newblk->nb_jwork);
7379 * When truncating we must free the newdirblk early to remove
7380 * the pagedep from the hash before returning.
7382 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7383 free_newdirblk(WK_NEWDIRBLK(wk));
7384 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7385 panic("cancel_newblk: extra newdirblk");
7391 * Schedule the freefrag associated with a newblk to be released once
7392 * the pointers are written and the previous block is no longer needed.
7395 newblk_freefrag(newblk)
7396 struct newblk *newblk;
7398 struct freefrag *freefrag;
7400 if (newblk->nb_freefrag == NULL)
7402 freefrag = newblk->nb_freefrag;
7403 newblk->nb_freefrag = NULL;
7404 freefrag->ff_state |= COMPLETE;
7405 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE)
7406 add_to_worklist(&freefrag->ff_list, 0);
7410 * Free a newblk. Generate a new freefrag work request if appropriate.
7411 * This must be called after the inode pointer and any direct block pointers
7412 * are valid or fully removed via truncate or frag extension.
7416 struct newblk *newblk;
7418 struct indirdep *indirdep;
7419 struct worklist *wk;
7421 KASSERT(newblk->nb_jnewblk == NULL,
7422 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk));
7423 KASSERT(newblk->nb_list.wk_type != D_NEWBLK,
7424 ("free_newblk: unclaimed newblk"));
7425 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp));
7426 newblk_freefrag(newblk);
7427 if (newblk->nb_state & ONDEPLIST)
7428 LIST_REMOVE(newblk, nb_deps);
7429 if (newblk->nb_state & ONWORKLIST)
7430 WORKLIST_REMOVE(&newblk->nb_list);
7431 LIST_REMOVE(newblk, nb_hash);
7432 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL)
7433 free_newdirblk(WK_NEWDIRBLK(wk));
7434 if (!LIST_EMPTY(&newblk->nb_newdirblk))
7435 panic("free_newblk: extra newdirblk");
7436 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL)
7437 indirdep_complete(indirdep);
7438 handle_jwork(&newblk->nb_jwork);
7439 WORKITEM_FREE(newblk, D_NEWBLK);
7443 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
7444 * This routine must be called with splbio interrupts blocked.
7447 free_newdirblk(newdirblk)
7448 struct newdirblk *newdirblk;
7450 struct pagedep *pagedep;
7452 struct worklist *wk;
7454 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp));
7455 WORKLIST_REMOVE(&newdirblk->db_list);
7457 * If the pagedep is still linked onto the directory buffer
7458 * dependency chain, then some of the entries on the
7459 * pd_pendinghd list may not be committed to disk yet. In
7460 * this case, we will simply clear the NEWBLOCK flag and
7461 * let the pd_pendinghd list be processed when the pagedep
7462 * is next written. If the pagedep is no longer on the buffer
7463 * dependency chain, then all the entries on the pd_pending
7464 * list are committed to disk and we can free them here.
7466 pagedep = newdirblk->db_pagedep;
7467 pagedep->pd_state &= ~NEWBLOCK;
7468 if ((pagedep->pd_state & ONWORKLIST) == 0) {
7469 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
7470 free_diradd(dap, NULL);
7472 * If no dependencies remain, the pagedep will be freed.
7474 free_pagedep(pagedep);
7476 /* Should only ever be one item in the list. */
7477 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) {
7478 WORKLIST_REMOVE(wk);
7479 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
7481 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
7485 * Prepare an inode to be freed. The actual free operation is not
7486 * done until the zero'ed inode has been written to disk.
7489 softdep_freefile(pvp, ino, mode)
7494 struct inode *ip = VTOI(pvp);
7495 struct inodedep *inodedep;
7496 struct freefile *freefile;
7497 struct freeblks *freeblks;
7498 struct ufsmount *ump;
7501 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
7502 ("softdep_freefile called on non-softdep filesystem"));
7504 * This sets up the inode de-allocation dependency.
7506 freefile = malloc(sizeof(struct freefile),
7507 M_FREEFILE, M_SOFTDEP_FLAGS);
7508 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
7509 freefile->fx_mode = mode;
7510 freefile->fx_oldinum = ino;
7511 freefile->fx_devvp = ip->i_devvp;
7512 LIST_INIT(&freefile->fx_jwork);
7514 ip->i_fs->fs_pendinginodes += 1;
7518 * If the inodedep does not exist, then the zero'ed inode has
7519 * been written to disk. If the allocated inode has never been
7520 * written to disk, then the on-disk inode is zero'ed. In either
7521 * case we can free the file immediately. If the journal was
7522 * canceled before being written the inode will never make it to
7523 * disk and we must send the canceled journal entrys to
7524 * ffs_freefile() to be cleared in conjunction with the bitmap.
7525 * Any blocks waiting on the inode to write can be safely freed
7526 * here as it will never been written.
7529 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7532 * Clear out freeblks that no longer need to reference
7536 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) {
7537 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks,
7539 freeblks->fb_state &= ~ONDEPLIST;
7542 * Remove this inode from the unlinked list.
7544 if (inodedep->id_state & UNLINKED) {
7546 * Save the journal work to be freed with the bitmap
7547 * before we clear UNLINKED. Otherwise it can be lost
7548 * if the inode block is written.
7550 handle_bufwait(inodedep, &freefile->fx_jwork);
7551 clear_unlinked_inodedep(inodedep);
7553 * Re-acquire inodedep as we've dropped the
7554 * per-filesystem lock in clear_unlinked_inodedep().
7556 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep);
7559 if (inodedep == NULL || check_inode_unwritten(inodedep)) {
7561 handle_workitem_freefile(freefile);
7564 if ((inodedep->id_state & DEPCOMPLETE) == 0)
7565 inodedep->id_state |= GOINGAWAY;
7566 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
7568 if (ip->i_number == ino)
7569 ip->i_flag |= IN_MODIFIED;
7573 * Check to see if an inode has never been written to disk. If
7574 * so free the inodedep and return success, otherwise return failure.
7575 * This routine must be called with splbio interrupts blocked.
7577 * If we still have a bitmap dependency, then the inode has never
7578 * been written to disk. Drop the dependency as it is no longer
7579 * necessary since the inode is being deallocated. We set the
7580 * ALLCOMPLETE flags since the bitmap now properly shows that the
7581 * inode is not allocated. Even if the inode is actively being
7582 * written, it has been rolled back to its zero'ed state, so we
7583 * are ensured that a zero inode is what is on the disk. For short
7584 * lived files, this change will usually result in removing all the
7585 * dependencies from the inode so that it can be freed immediately.
7588 check_inode_unwritten(inodedep)
7589 struct inodedep *inodedep;
7592 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7594 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 ||
7595 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7596 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7597 !LIST_EMPTY(&inodedep->id_bufwait) ||
7598 !LIST_EMPTY(&inodedep->id_inowait) ||
7599 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7600 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7601 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7602 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7603 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7604 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7605 inodedep->id_mkdiradd != NULL ||
7606 inodedep->id_nlinkdelta != 0)
7609 * Another process might be in initiate_write_inodeblock_ufs[12]
7610 * trying to allocate memory without holding "Softdep Lock".
7612 if ((inodedep->id_state & IOSTARTED) != 0 &&
7613 inodedep->id_savedino1 == NULL)
7616 if (inodedep->id_state & ONDEPLIST)
7617 LIST_REMOVE(inodedep, id_deps);
7618 inodedep->id_state &= ~ONDEPLIST;
7619 inodedep->id_state |= ALLCOMPLETE;
7620 inodedep->id_bmsafemap = NULL;
7621 if (inodedep->id_state & ONWORKLIST)
7622 WORKLIST_REMOVE(&inodedep->id_list);
7623 if (inodedep->id_savedino1 != NULL) {
7624 free(inodedep->id_savedino1, M_SAVEDINO);
7625 inodedep->id_savedino1 = NULL;
7627 if (free_inodedep(inodedep) == 0)
7628 panic("check_inode_unwritten: busy inode");
7633 check_inodedep_free(inodedep)
7634 struct inodedep *inodedep;
7637 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7638 if ((inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
7639 !LIST_EMPTY(&inodedep->id_dirremhd) ||
7640 !LIST_EMPTY(&inodedep->id_pendinghd) ||
7641 !LIST_EMPTY(&inodedep->id_bufwait) ||
7642 !LIST_EMPTY(&inodedep->id_inowait) ||
7643 !TAILQ_EMPTY(&inodedep->id_inoreflst) ||
7644 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
7645 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
7646 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
7647 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
7648 !TAILQ_EMPTY(&inodedep->id_freeblklst) ||
7649 inodedep->id_mkdiradd != NULL ||
7650 inodedep->id_nlinkdelta != 0 ||
7651 inodedep->id_savedino1 != NULL)
7657 * Try to free an inodedep structure. Return 1 if it could be freed.
7660 free_inodedep(inodedep)
7661 struct inodedep *inodedep;
7664 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp));
7665 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 ||
7666 !check_inodedep_free(inodedep))
7668 if (inodedep->id_state & ONDEPLIST)
7669 LIST_REMOVE(inodedep, id_deps);
7670 LIST_REMOVE(inodedep, id_hash);
7671 WORKITEM_FREE(inodedep, D_INODEDEP);
7676 * Free the block referenced by a freework structure. The parent freeblks
7677 * structure is released and completed when the final cg bitmap reaches
7678 * the disk. This routine may be freeing a jnewblk which never made it to
7679 * disk in which case we do not have to wait as the operation is undone
7680 * in memory immediately.
7683 freework_freeblock(freework)
7684 struct freework *freework;
7686 struct freeblks *freeblks;
7687 struct jnewblk *jnewblk;
7688 struct ufsmount *ump;
7689 struct workhead wkhd;
7694 ump = VFSTOUFS(freework->fw_list.wk_mp);
7697 * Handle partial truncate separately.
7699 if (freework->fw_indir) {
7700 complete_trunc_indir(freework);
7703 freeblks = freework->fw_freeblks;
7705 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0;
7706 bsize = lfragtosize(fs, freework->fw_frags);
7709 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives
7710 * on the indirblk hashtable and prevents premature freeing.
7712 freework->fw_state |= DEPCOMPLETE;
7714 * SUJ needs to wait for the segment referencing freed indirect
7715 * blocks to expire so that we know the checker will not confuse
7716 * a re-allocated indirect block with its old contents.
7718 if (needj && freework->fw_lbn <= -NDADDR)
7719 indirblk_insert(freework);
7721 * If we are canceling an existing jnewblk pass it to the free
7722 * routine, otherwise pass the freeblk which will ultimately
7723 * release the freeblks. If we're not journaling, we can just
7724 * free the freeblks immediately.
7726 jnewblk = freework->fw_jnewblk;
7727 if (jnewblk != NULL) {
7728 cancel_jnewblk(jnewblk, &wkhd);
7731 freework->fw_state |= DELAYEDFREE;
7732 freeblks->fb_cgwait++;
7733 WORKLIST_INSERT(&wkhd, &freework->fw_list);
7736 freeblks_free(ump, freeblks, btodb(bsize));
7738 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld",
7739 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize);
7740 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize,
7741 freeblks->fb_inum, freeblks->fb_vtype, &wkhd);
7744 * The jnewblk will be discarded and the bits in the map never
7745 * made it to disk. We can immediately free the freeblk.
7748 handle_written_freework(freework);
7752 * We enqueue freework items that need processing back on the freeblks and
7753 * add the freeblks to the worklist. This makes it easier to find all work
7754 * required to flush a truncation in process_truncates().
7757 freework_enqueue(freework)
7758 struct freework *freework;
7760 struct freeblks *freeblks;
7762 freeblks = freework->fw_freeblks;
7763 if ((freework->fw_state & INPROGRESS) == 0)
7764 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list);
7765 if ((freeblks->fb_state &
7766 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE &&
7767 LIST_EMPTY(&freeblks->fb_jblkdephd))
7768 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7772 * Start, continue, or finish the process of freeing an indirect block tree.
7773 * The free operation may be paused at any point with fw_off containing the
7774 * offset to restart from. This enables us to implement some flow control
7775 * for large truncates which may fan out and generate a huge number of
7779 handle_workitem_indirblk(freework)
7780 struct freework *freework;
7782 struct freeblks *freeblks;
7783 struct ufsmount *ump;
7786 freeblks = freework->fw_freeblks;
7787 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7789 if (freework->fw_state & DEPCOMPLETE) {
7790 handle_written_freework(freework);
7793 if (freework->fw_off == NINDIR(fs)) {
7794 freework_freeblock(freework);
7797 freework->fw_state |= INPROGRESS;
7799 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno),
7805 * Called when a freework structure attached to a cg buf is written. The
7806 * ref on either the parent or the freeblks structure is released and
7807 * the freeblks is added back to the worklist if there is more work to do.
7810 handle_written_freework(freework)
7811 struct freework *freework;
7813 struct freeblks *freeblks;
7814 struct freework *parent;
7816 freeblks = freework->fw_freeblks;
7817 parent = freework->fw_parent;
7818 if (freework->fw_state & DELAYEDFREE)
7819 freeblks->fb_cgwait--;
7820 freework->fw_state |= COMPLETE;
7821 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE)
7822 WORKITEM_FREE(freework, D_FREEWORK);
7824 if (--parent->fw_ref == 0)
7825 freework_enqueue(parent);
7828 if (--freeblks->fb_ref != 0)
7830 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) ==
7831 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd))
7832 add_to_worklist(&freeblks->fb_list, WK_NODELAY);
7836 * This workitem routine performs the block de-allocation.
7837 * The workitem is added to the pending list after the updated
7838 * inode block has been written to disk. As mentioned above,
7839 * checks regarding the number of blocks de-allocated (compared
7840 * to the number of blocks allocated for the file) are also
7841 * performed in this function.
7844 handle_workitem_freeblocks(freeblks, flags)
7845 struct freeblks *freeblks;
7848 struct freework *freework;
7849 struct newblk *newblk;
7850 struct allocindir *aip;
7851 struct ufsmount *ump;
7852 struct worklist *wk;
7854 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd),
7855 ("handle_workitem_freeblocks: Journal entries not written."));
7856 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7858 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) {
7859 WORKLIST_REMOVE(wk);
7860 switch (wk->wk_type) {
7862 wk->wk_state |= COMPLETE;
7863 add_to_worklist(wk, 0);
7867 free_newblk(WK_NEWBLK(wk));
7871 aip = WK_ALLOCINDIR(wk);
7873 if (aip->ai_state & DELAYEDFREE) {
7875 freework = newfreework(ump, freeblks, NULL,
7876 aip->ai_lbn, aip->ai_newblkno,
7877 ump->um_fs->fs_frag, 0, 0);
7880 newblk = WK_NEWBLK(wk);
7881 if (newblk->nb_jnewblk) {
7882 freework->fw_jnewblk = newblk->nb_jnewblk;
7883 newblk->nb_jnewblk->jn_dep = &freework->fw_list;
7884 newblk->nb_jnewblk = NULL;
7886 free_newblk(newblk);
7890 freework = WK_FREEWORK(wk);
7891 if (freework->fw_lbn <= -NDADDR)
7892 handle_workitem_indirblk(freework);
7894 freework_freeblock(freework);
7897 panic("handle_workitem_freeblocks: Unknown type %s",
7898 TYPENAME(wk->wk_type));
7901 if (freeblks->fb_ref != 0) {
7902 freeblks->fb_state &= ~INPROGRESS;
7903 wake_worklist(&freeblks->fb_list);
7908 return handle_complete_freeblocks(freeblks, flags);
7913 * Handle completion of block free via truncate. This allows fs_pending
7914 * to track the actual free block count more closely than if we only updated
7915 * it at the end. We must be careful to handle cases where the block count
7916 * on free was incorrect.
7919 freeblks_free(ump, freeblks, blocks)
7920 struct ufsmount *ump;
7921 struct freeblks *freeblks;
7925 ufs2_daddr_t remain;
7928 remain = -freeblks->fb_chkcnt;
7929 freeblks->fb_chkcnt += blocks;
7931 if (remain < blocks)
7934 fs->fs_pendingblocks -= blocks;
7940 * Once all of the freework workitems are complete we can retire the
7941 * freeblocks dependency and any journal work awaiting completion. This
7942 * can not be called until all other dependencies are stable on disk.
7945 handle_complete_freeblocks(freeblks, flags)
7946 struct freeblks *freeblks;
7949 struct inodedep *inodedep;
7953 struct ufsmount *ump;
7956 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
7958 flags = LK_EXCLUSIVE | flags;
7959 spare = freeblks->fb_chkcnt;
7962 * If we did not release the expected number of blocks we may have
7963 * to adjust the inode block count here. Only do so if it wasn't
7964 * a truncation to zero and the modrev still matches.
7966 if (spare && freeblks->fb_len != 0) {
7967 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum,
7968 flags, &vp, FFSV_FORCEINSMQ) != 0)
7971 if (DIP(ip, i_modrev) == freeblks->fb_modrev) {
7972 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare);
7973 ip->i_flag |= IN_CHANGE;
7975 * We must wait so this happens before the
7976 * journal is reclaimed.
7984 fs->fs_pendingblocks += spare;
7990 quotaadj(freeblks->fb_quota, ump, -spare);
7991 quotarele(freeblks->fb_quota);
7994 if (freeblks->fb_state & ONDEPLIST) {
7995 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum,
7997 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next);
7998 freeblks->fb_state &= ~ONDEPLIST;
7999 if (TAILQ_EMPTY(&inodedep->id_freeblklst))
8000 free_inodedep(inodedep);
8003 * All of the freeblock deps must be complete prior to this call
8004 * so it's now safe to complete earlier outstanding journal entries.
8006 handle_jwork(&freeblks->fb_jwork);
8007 WORKITEM_FREE(freeblks, D_FREEBLKS);
8013 * Release blocks associated with the freeblks and stored in the indirect
8014 * block dbn. If level is greater than SINGLE, the block is an indirect block
8015 * and recursive calls to indirtrunc must be used to cleanse other indirect
8018 * This handles partial and complete truncation of blocks. Partial is noted
8019 * with goingaway == 0. In this case the freework is completed after the
8020 * zero'd indirects are written to disk. For full truncation the freework
8021 * is completed after the block is freed.
8024 indir_trunc(freework, dbn, lbn)
8025 struct freework *freework;
8029 struct freework *nfreework;
8030 struct workhead wkhd;
8031 struct freeblks *freeblks;
8034 struct indirdep *indirdep;
8035 struct ufsmount *ump;
8036 ufs1_daddr_t *bap1 = 0;
8037 ufs2_daddr_t nb, nnb, *bap2 = 0;
8038 ufs_lbn_t lbnadd, nlbn;
8039 int i, nblocks, ufs1fmt;
8047 freeblks = freework->fw_freeblks;
8048 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
8051 * Get buffer of block pointers to be freed. There are three cases:
8053 * 1) Partial truncate caches the indirdep pointer in the freework
8054 * which provides us a back copy to the save bp which holds the
8055 * pointers we want to clear. When this completes the zero
8056 * pointers are written to the real copy.
8057 * 2) The indirect is being completely truncated, cancel_indirdep()
8058 * eliminated the real copy and placed the indirdep on the saved
8059 * copy. The indirdep and buf are discarded when this completes.
8060 * 3) The indirect was not in memory, we read a copy off of the disk
8061 * using the devvp and drop and invalidate the buffer when we're
8066 if (freework->fw_indir != NULL) {
8068 indirdep = freework->fw_indir;
8069 bp = indirdep->ir_savebp;
8070 if (bp == NULL || bp->b_blkno != dbn)
8071 panic("indir_trunc: Bad saved buf %p blkno %jd",
8073 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) {
8075 * The lock prevents the buf dep list from changing and
8076 * indirects on devvp should only ever have one dependency.
8078 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep));
8079 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0)
8080 panic("indir_trunc: Bad indirdep %p from buf %p",
8082 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
8083 NOCRED, &bp) != 0) {
8088 /* Protects against a race with complete_trunc_indir(). */
8089 freework->fw_state &= ~INPROGRESS;
8091 * If we have an indirdep we need to enforce the truncation order
8092 * and discard it when it is complete.
8095 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) &&
8096 !TAILQ_EMPTY(&indirdep->ir_trunc)) {
8098 * Add the complete truncate to the list on the
8099 * indirdep to enforce in-order processing.
8101 if (freework->fw_indir == NULL)
8102 TAILQ_INSERT_TAIL(&indirdep->ir_trunc,
8108 * If we're goingaway, free the indirdep. Otherwise it will
8109 * linger until the write completes.
8112 free_indirdep(indirdep);
8115 /* Initialize pointers depending on block size. */
8116 if (ump->um_fstype == UFS1) {
8117 bap1 = (ufs1_daddr_t *)bp->b_data;
8118 nb = bap1[freework->fw_off];
8121 bap2 = (ufs2_daddr_t *)bp->b_data;
8122 nb = bap2[freework->fw_off];
8125 level = lbn_level(lbn);
8126 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0;
8127 lbnadd = lbn_offset(fs, level);
8128 nblocks = btodb(fs->fs_bsize);
8129 nfreework = freework;
8133 * Reclaim blocks. Traverses into nested indirect levels and
8134 * arranges for the current level to be freed when subordinates
8135 * are free when journaling.
8137 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) {
8138 if (i != NINDIR(fs) - 1) {
8149 nlbn = (lbn + 1) - (i * lbnadd);
8151 nfreework = newfreework(ump, freeblks, freework,
8152 nlbn, nb, fs->fs_frag, 0, 0);
8155 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn);
8157 struct freedep *freedep;
8160 * Attempt to aggregate freedep dependencies for
8161 * all blocks being released to the same CG.
8165 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) {
8166 freedep = newfreedep(freework);
8167 WORKLIST_INSERT_UNLOCKED(&wkhd,
8172 "indir_trunc: ino %d blkno %jd size %ld",
8173 freeblks->fb_inum, nb, fs->fs_bsize);
8174 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb,
8175 fs->fs_bsize, freeblks->fb_inum,
8176 freeblks->fb_vtype, &wkhd);
8180 bp->b_flags |= B_INVAL | B_NOCACHE;
8185 freedblocks = (nblocks * cnt);
8187 freedblocks += nblocks;
8188 freeblks_free(ump, freeblks, freedblocks);
8190 * If we are journaling set up the ref counts and offset so this
8191 * indirect can be completed when its children are free.
8195 freework->fw_off = i;
8196 freework->fw_ref += freedeps;
8197 freework->fw_ref -= NINDIR(fs) + 1;
8199 freeblks->fb_cgwait += freedeps;
8200 if (freework->fw_ref == 0)
8201 freework_freeblock(freework);
8206 * If we're not journaling we can free the indirect now.
8208 dbn = dbtofsb(fs, dbn);
8210 "indir_trunc 2: ino %d blkno %jd size %ld",
8211 freeblks->fb_inum, dbn, fs->fs_bsize);
8212 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize,
8213 freeblks->fb_inum, freeblks->fb_vtype, NULL);
8214 /* Non SUJ softdep does single-threaded truncations. */
8215 if (freework->fw_blkno == dbn) {
8216 freework->fw_state |= ALLCOMPLETE;
8218 handle_written_freework(freework);
8225 * Cancel an allocindir when it is removed via truncation. When bp is not
8226 * NULL the indirect never appeared on disk and is scheduled to be freed
8227 * independently of the indir so we can more easily track journal work.
8230 cancel_allocindir(aip, bp, freeblks, trunc)
8231 struct allocindir *aip;
8233 struct freeblks *freeblks;
8236 struct indirdep *indirdep;
8237 struct freefrag *freefrag;
8238 struct newblk *newblk;
8240 newblk = (struct newblk *)aip;
8241 LIST_REMOVE(aip, ai_next);
8243 * We must eliminate the pointer in bp if it must be freed on its
8244 * own due to partial truncate or pending journal work.
8246 if (bp && (trunc || newblk->nb_jnewblk)) {
8248 * Clear the pointer and mark the aip to be freed
8249 * directly if it never existed on disk.
8251 aip->ai_state |= DELAYEDFREE;
8252 indirdep = aip->ai_indirdep;
8253 if (indirdep->ir_state & UFS1FMT)
8254 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8256 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0;
8259 * When truncating the previous pointer will be freed via
8260 * savedbp. Eliminate the freefrag which would dup free.
8262 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) {
8263 newblk->nb_freefrag = NULL;
8264 if (freefrag->ff_jdep)
8266 WK_JFREEFRAG(freefrag->ff_jdep));
8267 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork);
8268 WORKITEM_FREE(freefrag, D_FREEFRAG);
8271 * If the journal hasn't been written the jnewblk must be passed
8272 * to the call to ffs_blkfree that reclaims the space. We accomplish
8273 * this by leaving the journal dependency on the newblk to be freed
8274 * when a freework is created in handle_workitem_freeblocks().
8276 cancel_newblk(newblk, NULL, &freeblks->fb_jwork);
8277 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list);
8281 * Create the mkdir dependencies for . and .. in a new directory. Link them
8282 * in to a newdirblk so any subsequent additions are tracked properly. The
8283 * caller is responsible for adding the mkdir1 dependency to the journal
8284 * and updating id_mkdiradd. This function returns with the per-filesystem
8287 static struct mkdir *
8288 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp)
8292 struct buf *newdirbp;
8293 struct mkdir **mkdirp;
8295 struct newblk *newblk;
8296 struct pagedep *pagedep;
8297 struct inodedep *inodedep;
8298 struct newdirblk *newdirblk = 0;
8299 struct mkdir *mkdir1, *mkdir2;
8300 struct worklist *wk;
8301 struct jaddref *jaddref;
8302 struct ufsmount *ump;
8305 mp = dap->da_list.wk_mp;
8307 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK,
8309 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8310 LIST_INIT(&newdirblk->db_mkdir);
8311 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8312 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
8313 mkdir1->md_state = ATTACHED | MKDIR_BODY;
8314 mkdir1->md_diradd = dap;
8315 mkdir1->md_jaddref = NULL;
8316 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS);
8317 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
8318 mkdir2->md_state = ATTACHED | MKDIR_PARENT;
8319 mkdir2->md_diradd = dap;
8320 mkdir2->md_jaddref = NULL;
8321 if (MOUNTEDSUJ(mp) == 0) {
8322 mkdir1->md_state |= DEPCOMPLETE;
8323 mkdir2->md_state |= DEPCOMPLETE;
8326 * Dependency on "." and ".." being written to disk.
8328 mkdir1->md_buf = newdirbp;
8329 ACQUIRE_LOCK(VFSTOUFS(mp));
8330 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs);
8332 * We must link the pagedep, allocdirect, and newdirblk for
8333 * the initial file page so the pointer to the new directory
8334 * is not written until the directory contents are live and
8335 * any subsequent additions are not marked live until the
8336 * block is reachable via the inode.
8338 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0)
8339 panic("setup_newdir: lost pagedep");
8340 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list)
8341 if (wk->wk_type == D_ALLOCDIRECT)
8344 panic("setup_newdir: lost allocdirect");
8345 if (pagedep->pd_state & NEWBLOCK)
8346 panic("setup_newdir: NEWBLOCK already set");
8347 newblk = WK_NEWBLK(wk);
8348 pagedep->pd_state |= NEWBLOCK;
8349 pagedep->pd_newdirblk = newdirblk;
8350 newdirblk->db_pagedep = pagedep;
8351 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8352 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list);
8354 * Look up the inodedep for the parent directory so that we
8355 * can link mkdir2 into the pending dotdot jaddref or
8356 * the inode write if there is none. If the inode is
8357 * ALLCOMPLETE and no jaddref is present all dependencies have
8358 * been satisfied and mkdir2 can be freed.
8360 inodedep_lookup(mp, dinum, 0, &inodedep);
8361 if (MOUNTEDSUJ(mp)) {
8362 if (inodedep == NULL)
8363 panic("setup_newdir: Lost parent.");
8364 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8366 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum &&
8367 (jaddref->ja_state & MKDIR_PARENT),
8368 ("setup_newdir: bad dotdot jaddref %p", jaddref));
8369 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8370 mkdir2->md_jaddref = jaddref;
8371 jaddref->ja_mkdir = mkdir2;
8372 } else if (inodedep == NULL ||
8373 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
8374 dap->da_state &= ~MKDIR_PARENT;
8375 WORKITEM_FREE(mkdir2, D_MKDIR);
8378 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs);
8379 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list);
8387 * Directory entry addition dependencies.
8389 * When adding a new directory entry, the inode (with its incremented link
8390 * count) must be written to disk before the directory entry's pointer to it.
8391 * Also, if the inode is newly allocated, the corresponding freemap must be
8392 * updated (on disk) before the directory entry's pointer. These requirements
8393 * are met via undo/redo on the directory entry's pointer, which consists
8394 * simply of the inode number.
8396 * As directory entries are added and deleted, the free space within a
8397 * directory block can become fragmented. The ufs filesystem will compact
8398 * a fragmented directory block to make space for a new entry. When this
8399 * occurs, the offsets of previously added entries change. Any "diradd"
8400 * dependency structures corresponding to these entries must be updated with
8405 * This routine is called after the in-memory inode's link
8406 * count has been incremented, but before the directory entry's
8407 * pointer to the inode has been set.
8410 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
8411 struct buf *bp; /* buffer containing directory block */
8412 struct inode *dp; /* inode for directory */
8413 off_t diroffset; /* offset of new entry in directory */
8414 ino_t newinum; /* inode referenced by new directory entry */
8415 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
8416 int isnewblk; /* entry is in a newly allocated block */
8418 int offset; /* offset of new entry within directory block */
8419 ufs_lbn_t lbn; /* block in directory containing new entry */
8422 struct newblk *newblk;
8423 struct pagedep *pagedep;
8424 struct inodedep *inodedep;
8425 struct newdirblk *newdirblk = 0;
8426 struct mkdir *mkdir1, *mkdir2;
8427 struct jaddref *jaddref;
8428 struct ufsmount *ump;
8434 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8435 ("softdep_setup_directory_add called on non-softdep filesystem"));
8437 * Whiteouts have no dependencies.
8439 if (newinum == WINO) {
8440 if (newdirbp != NULL)
8445 mkdir1 = mkdir2 = NULL;
8447 lbn = lblkno(fs, diroffset);
8448 offset = blkoff(fs, diroffset);
8449 dap = malloc(sizeof(struct diradd), M_DIRADD,
8450 M_SOFTDEP_FLAGS|M_ZERO);
8451 workitem_alloc(&dap->da_list, D_DIRADD, mp);
8452 dap->da_offset = offset;
8453 dap->da_newinum = newinum;
8454 dap->da_state = ATTACHED;
8455 LIST_INIT(&dap->da_jwork);
8456 isindir = bp->b_lblkno >= NDADDR;
8458 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) {
8459 newdirblk = malloc(sizeof(struct newdirblk),
8460 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
8461 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
8462 LIST_INIT(&newdirblk->db_mkdir);
8465 * If we're creating a new directory setup the dependencies and set
8466 * the dap state to wait for them. Otherwise it's COMPLETE and
8469 if (newdirbp == NULL) {
8470 dap->da_state |= DEPCOMPLETE;
8473 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
8474 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp,
8478 * Link into parent directory pagedep to await its being written.
8480 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep);
8482 if (diradd_lookup(pagedep, offset) != NULL)
8483 panic("softdep_setup_directory_add: %p already at off %d\n",
8484 diradd_lookup(pagedep, offset), offset);
8486 dap->da_pagedep = pagedep;
8487 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
8489 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
8491 * If we're journaling, link the diradd into the jaddref so it
8492 * may be completed after the journal entry is written. Otherwise,
8493 * link the diradd into its inodedep. If the inode is not yet
8494 * written place it on the bufwait list, otherwise do the post-inode
8495 * write processing to put it on the id_pendinghd list.
8497 if (MOUNTEDSUJ(mp)) {
8498 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
8500 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
8501 ("softdep_setup_directory_add: bad jaddref %p", jaddref));
8502 jaddref->ja_diroff = diroffset;
8503 jaddref->ja_diradd = dap;
8504 add_to_journal(&jaddref->ja_list);
8505 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
8506 diradd_inode_written(dap, inodedep);
8508 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
8510 * Add the journal entries for . and .. links now that the primary
8513 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) {
8514 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref,
8515 inoreflst, if_deps);
8516 KASSERT(jaddref != NULL &&
8517 jaddref->ja_ino == jaddref->ja_parent &&
8518 (jaddref->ja_state & MKDIR_BODY),
8519 ("softdep_setup_directory_add: bad dot jaddref %p",
8521 mkdir1->md_jaddref = jaddref;
8522 jaddref->ja_mkdir = mkdir1;
8524 * It is important that the dotdot journal entry
8525 * is added prior to the dot entry since dot writes
8526 * both the dot and dotdot links. These both must
8527 * be added after the primary link for the journal
8528 * to remain consistent.
8530 add_to_journal(&mkdir2->md_jaddref->ja_list);
8531 add_to_journal(&jaddref->ja_list);
8534 * If we are adding a new directory remember this diradd so that if
8535 * we rename it we can keep the dot and dotdot dependencies. If
8536 * we are adding a new name for an inode that has a mkdiradd we
8537 * must be in rename and we have to move the dot and dotdot
8538 * dependencies to this new name. The old name is being orphaned
8541 if (mkdir1 != NULL) {
8542 if (inodedep->id_mkdiradd != NULL)
8543 panic("softdep_setup_directory_add: Existing mkdir");
8544 inodedep->id_mkdiradd = dap;
8545 } else if (inodedep->id_mkdiradd)
8546 merge_diradd(inodedep, dap);
8549 * There is nothing to do if we are already tracking
8552 if ((pagedep->pd_state & NEWBLOCK) != 0) {
8553 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
8557 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)
8559 panic("softdep_setup_directory_add: lost entry");
8560 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list);
8561 pagedep->pd_state |= NEWBLOCK;
8562 pagedep->pd_newdirblk = newdirblk;
8563 newdirblk->db_pagedep = pagedep;
8566 * If we extended into an indirect signal direnter to sync.
8577 * This procedure is called to change the offset of a directory
8578 * entry when compacting a directory block which must be owned
8579 * exclusively by the caller. Note that the actual entry movement
8580 * must be done in this procedure to ensure that no I/O completions
8581 * occur while the move is in progress.
8584 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize)
8585 struct buf *bp; /* Buffer holding directory block. */
8586 struct inode *dp; /* inode for directory */
8587 caddr_t base; /* address of dp->i_offset */
8588 caddr_t oldloc; /* address of old directory location */
8589 caddr_t newloc; /* address of new directory location */
8590 int entrysize; /* size of directory entry */
8592 int offset, oldoffset, newoffset;
8593 struct pagedep *pagedep;
8594 struct jmvref *jmvref;
8601 mp = UFSTOVFS(dp->i_ump);
8602 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
8603 ("softdep_change_directoryentry_offset called on "
8604 "non-softdep filesystem"));
8605 de = (struct direct *)oldloc;
8609 * Moves are always journaled as it would be too complex to
8610 * determine if any affected adds or removes are present in the
8613 if (MOUNTEDSUJ(mp)) {
8615 jmvref = newjmvref(dp, de->d_ino,
8616 dp->i_offset + (oldloc - base),
8617 dp->i_offset + (newloc - base));
8619 lbn = lblkno(dp->i_fs, dp->i_offset);
8620 offset = blkoff(dp->i_fs, dp->i_offset);
8621 oldoffset = offset + (oldloc - base);
8622 newoffset = offset + (newloc - base);
8623 ACQUIRE_LOCK(dp->i_ump);
8624 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0)
8626 dap = diradd_lookup(pagedep, oldoffset);
8628 dap->da_offset = newoffset;
8629 newoffset = DIRADDHASH(newoffset);
8630 oldoffset = DIRADDHASH(oldoffset);
8631 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE &&
8632 newoffset != oldoffset) {
8633 LIST_REMOVE(dap, da_pdlist);
8634 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset],
8640 jmvref->jm_pagedep = pagedep;
8641 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps);
8642 add_to_journal(&jmvref->jm_list);
8644 bcopy(oldloc, newloc, entrysize);
8645 FREE_LOCK(dp->i_ump);
8649 * Move the mkdir dependencies and journal work from one diradd to another
8650 * when renaming a directory. The new name must depend on the mkdir deps
8651 * completing as the old name did. Directories can only have one valid link
8652 * at a time so one must be canonical.
8655 merge_diradd(inodedep, newdap)
8656 struct inodedep *inodedep;
8657 struct diradd *newdap;
8659 struct diradd *olddap;
8660 struct mkdir *mkdir, *nextmd;
8661 struct ufsmount *ump;
8664 olddap = inodedep->id_mkdiradd;
8665 inodedep->id_mkdiradd = newdap;
8666 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8667 newdap->da_state &= ~DEPCOMPLETE;
8668 ump = VFSTOUFS(inodedep->id_list.wk_mp);
8669 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8671 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8672 if (mkdir->md_diradd != olddap)
8674 mkdir->md_diradd = newdap;
8675 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY);
8676 newdap->da_state |= state;
8677 olddap->da_state &= ~state;
8678 if ((olddap->da_state &
8679 (MKDIR_PARENT | MKDIR_BODY)) == 0)
8682 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8683 panic("merge_diradd: unfound ref");
8686 * Any mkdir related journal items are not safe to be freed until
8687 * the new name is stable.
8689 jwork_move(&newdap->da_jwork, &olddap->da_jwork);
8690 olddap->da_state |= DEPCOMPLETE;
8691 complete_diradd(olddap);
8695 * Move the diradd to the pending list when all diradd dependencies are
8699 complete_diradd(dap)
8702 struct pagedep *pagedep;
8704 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
8705 if (dap->da_state & DIRCHG)
8706 pagedep = dap->da_previous->dm_pagedep;
8708 pagedep = dap->da_pagedep;
8709 LIST_REMOVE(dap, da_pdlist);
8710 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
8715 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal
8716 * add entries and conditonally journal the remove.
8719 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref)
8721 struct dirrem *dirrem;
8722 struct jremref *jremref;
8723 struct jremref *dotremref;
8724 struct jremref *dotdotremref;
8726 struct inodedep *inodedep;
8727 struct jaddref *jaddref;
8728 struct inoref *inoref;
8729 struct ufsmount *ump;
8730 struct mkdir *mkdir;
8733 * If no remove references were allocated we're on a non-journaled
8734 * filesystem and can skip the cancel step.
8736 if (jremref == NULL) {
8737 free_diradd(dap, NULL);
8741 * Cancel the primary name an free it if it does not require
8744 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum,
8745 0, &inodedep) != 0) {
8746 /* Abort the addref that reference this diradd. */
8747 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
8748 if (inoref->if_list.wk_type != D_JADDREF)
8750 jaddref = (struct jaddref *)inoref;
8751 if (jaddref->ja_diradd != dap)
8753 if (cancel_jaddref(jaddref, inodedep,
8754 &dirrem->dm_jwork) == 0) {
8755 free_jremref(jremref);
8762 * Cancel subordinate names and free them if they do not require
8765 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8766 ump = VFSTOUFS(dap->da_list.wk_mp);
8767 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) {
8768 if (mkdir->md_diradd != dap)
8770 if ((jaddref = mkdir->md_jaddref) == NULL)
8772 mkdir->md_jaddref = NULL;
8773 if (mkdir->md_state & MKDIR_PARENT) {
8774 if (cancel_jaddref(jaddref, NULL,
8775 &dirrem->dm_jwork) == 0) {
8776 free_jremref(dotdotremref);
8777 dotdotremref = NULL;
8780 if (cancel_jaddref(jaddref, inodedep,
8781 &dirrem->dm_jwork) == 0) {
8782 free_jremref(dotremref);
8790 journal_jremref(dirrem, jremref, inodedep);
8792 journal_jremref(dirrem, dotremref, inodedep);
8794 journal_jremref(dirrem, dotdotremref, NULL);
8795 jwork_move(&dirrem->dm_jwork, &dap->da_jwork);
8796 free_diradd(dap, &dirrem->dm_jwork);
8800 * Free a diradd dependency structure. This routine must be called
8801 * with splbio interrupts blocked.
8804 free_diradd(dap, wkhd)
8806 struct workhead *wkhd;
8808 struct dirrem *dirrem;
8809 struct pagedep *pagedep;
8810 struct inodedep *inodedep;
8811 struct mkdir *mkdir, *nextmd;
8812 struct ufsmount *ump;
8814 ump = VFSTOUFS(dap->da_list.wk_mp);
8816 LIST_REMOVE(dap, da_pdlist);
8817 if (dap->da_state & ONWORKLIST)
8818 WORKLIST_REMOVE(&dap->da_list);
8819 if ((dap->da_state & DIRCHG) == 0) {
8820 pagedep = dap->da_pagedep;
8822 dirrem = dap->da_previous;
8823 pagedep = dirrem->dm_pagedep;
8824 dirrem->dm_dirinum = pagedep->pd_ino;
8825 dirrem->dm_state |= COMPLETE;
8826 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
8827 add_to_worklist(&dirrem->dm_list, 0);
8829 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
8831 if (inodedep->id_mkdiradd == dap)
8832 inodedep->id_mkdiradd = NULL;
8833 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
8834 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
8836 nextmd = LIST_NEXT(mkdir, md_mkdirs);
8837 if (mkdir->md_diradd != dap)
8840 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
8841 LIST_REMOVE(mkdir, md_mkdirs);
8842 if (mkdir->md_state & ONWORKLIST)
8843 WORKLIST_REMOVE(&mkdir->md_list);
8844 if (mkdir->md_jaddref != NULL)
8845 panic("free_diradd: Unexpected jaddref");
8846 WORKITEM_FREE(mkdir, D_MKDIR);
8847 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
8850 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
8851 panic("free_diradd: unfound ref");
8854 free_inodedep(inodedep);
8856 * Free any journal segments waiting for the directory write.
8858 handle_jwork(&dap->da_jwork);
8859 WORKITEM_FREE(dap, D_DIRADD);
8863 * Directory entry removal dependencies.
8865 * When removing a directory entry, the entry's inode pointer must be
8866 * zero'ed on disk before the corresponding inode's link count is decremented
8867 * (possibly freeing the inode for re-use). This dependency is handled by
8868 * updating the directory entry but delaying the inode count reduction until
8869 * after the directory block has been written to disk. After this point, the
8870 * inode count can be decremented whenever it is convenient.
8874 * This routine should be called immediately after removing
8875 * a directory entry. The inode's link count should not be
8876 * decremented by the calling procedure -- the soft updates
8877 * code will do this task when it is safe.
8880 softdep_setup_remove(bp, dp, ip, isrmdir)
8881 struct buf *bp; /* buffer containing directory block */
8882 struct inode *dp; /* inode for the directory being modified */
8883 struct inode *ip; /* inode for directory entry being removed */
8884 int isrmdir; /* indicates if doing RMDIR */
8886 struct dirrem *dirrem, *prevdirrem;
8887 struct inodedep *inodedep;
8890 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
8891 ("softdep_setup_remove called on non-softdep filesystem"));
8893 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want
8894 * newdirrem() to setup the full directory remove which requires
8897 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
8899 * Add the dirrem to the inodedep's pending remove list for quick
8902 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0,
8904 panic("softdep_setup_remove: Lost inodedep.");
8905 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked"));
8906 dirrem->dm_state |= ONDEPLIST;
8907 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
8910 * If the COMPLETE flag is clear, then there were no active
8911 * entries and we want to roll back to a zeroed entry until
8912 * the new inode is committed to disk. If the COMPLETE flag is
8913 * set then we have deleted an entry that never made it to
8914 * disk. If the entry we deleted resulted from a name change,
8915 * then the old name still resides on disk. We cannot delete
8916 * its inode (returned to us in prevdirrem) until the zeroed
8917 * directory entry gets to disk. The new inode has never been
8918 * referenced on the disk, so can be deleted immediately.
8920 if ((dirrem->dm_state & COMPLETE) == 0) {
8921 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
8923 FREE_LOCK(ip->i_ump);
8925 if (prevdirrem != NULL)
8926 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
8927 prevdirrem, dm_next);
8928 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
8929 direct = LIST_EMPTY(&dirrem->dm_jremrefhd);
8930 FREE_LOCK(ip->i_ump);
8932 handle_workitem_remove(dirrem, 0);
8937 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the
8938 * pd_pendinghd list of a pagedep.
8940 static struct diradd *
8941 diradd_lookup(pagedep, offset)
8942 struct pagedep *pagedep;
8947 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
8948 if (dap->da_offset == offset)
8950 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
8951 if (dap->da_offset == offset)
8957 * Search for a .. diradd dependency in a directory that is being removed.
8958 * If the directory was renamed to a new parent we have a diradd rather
8959 * than a mkdir for the .. entry. We need to cancel it now before
8960 * it is found in truncate().
8962 static struct jremref *
8963 cancel_diradd_dotdot(ip, dirrem, jremref)
8965 struct dirrem *dirrem;
8966 struct jremref *jremref;
8968 struct pagedep *pagedep;
8970 struct worklist *wk;
8972 if (pagedep_lookup(UFSTOVFS(ip->i_ump), NULL, ip->i_number, 0, 0,
8975 dap = diradd_lookup(pagedep, DOTDOT_OFFSET);
8978 cancel_diradd(dap, dirrem, jremref, NULL, NULL);
8980 * Mark any journal work as belonging to the parent so it is freed
8981 * with the .. reference.
8983 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
8984 wk->wk_state |= MKDIR_PARENT;
8989 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to
8990 * replace it with a dirrem/diradd pair as a result of re-parenting a
8991 * directory. This ensures that we don't simultaneously have a mkdir and
8992 * a diradd for the same .. entry.
8994 static struct jremref *
8995 cancel_mkdir_dotdot(ip, dirrem, jremref)
8997 struct dirrem *dirrem;
8998 struct jremref *jremref;
9000 struct inodedep *inodedep;
9001 struct jaddref *jaddref;
9002 struct ufsmount *ump;
9003 struct mkdir *mkdir;
9006 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0,
9009 dap = inodedep->id_mkdiradd;
9010 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0)
9012 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9013 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir;
9014 mkdir = LIST_NEXT(mkdir, md_mkdirs))
9015 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT)
9018 panic("cancel_mkdir_dotdot: Unable to find mkdir\n");
9019 if ((jaddref = mkdir->md_jaddref) != NULL) {
9020 mkdir->md_jaddref = NULL;
9021 jaddref->ja_state &= ~MKDIR_PARENT;
9022 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0,
9024 panic("cancel_mkdir_dotdot: Lost parent inodedep");
9025 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) {
9026 journal_jremref(dirrem, jremref, inodedep);
9030 if (mkdir->md_state & ONWORKLIST)
9031 WORKLIST_REMOVE(&mkdir->md_list);
9032 mkdir->md_state |= ALLCOMPLETE;
9033 complete_mkdir(mkdir);
9038 journal_jremref(dirrem, jremref, inodedep)
9039 struct dirrem *dirrem;
9040 struct jremref *jremref;
9041 struct inodedep *inodedep;
9044 if (inodedep == NULL)
9045 if (inodedep_lookup(jremref->jr_list.wk_mp,
9046 jremref->jr_ref.if_ino, 0, &inodedep) == 0)
9047 panic("journal_jremref: Lost inodedep");
9048 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps);
9049 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps);
9050 add_to_journal(&jremref->jr_list);
9054 dirrem_journal(dirrem, jremref, dotremref, dotdotremref)
9055 struct dirrem *dirrem;
9056 struct jremref *jremref;
9057 struct jremref *dotremref;
9058 struct jremref *dotdotremref;
9060 struct inodedep *inodedep;
9063 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0,
9065 panic("dirrem_journal: Lost inodedep");
9066 journal_jremref(dirrem, jremref, inodedep);
9068 journal_jremref(dirrem, dotremref, inodedep);
9070 journal_jremref(dirrem, dotdotremref, NULL);
9074 * Allocate a new dirrem if appropriate and return it along with
9075 * its associated pagedep. Called without a lock, returns with lock.
9077 static struct dirrem *
9078 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
9079 struct buf *bp; /* buffer containing directory block */
9080 struct inode *dp; /* inode for the directory being modified */
9081 struct inode *ip; /* inode for directory entry being removed */
9082 int isrmdir; /* indicates if doing RMDIR */
9083 struct dirrem **prevdirremp; /* previously referenced inode, if any */
9088 struct dirrem *dirrem;
9089 struct pagedep *pagedep;
9090 struct jremref *jremref;
9091 struct jremref *dotremref;
9092 struct jremref *dotdotremref;
9096 * Whiteouts have no deletion dependencies.
9099 panic("newdirrem: whiteout");
9102 * If the system is over its limit and our filesystem is
9103 * responsible for more than our share of that usage and
9104 * we are not a snapshot, request some inodedep cleanup.
9105 * Limiting the number of dirrem structures will also limit
9106 * the number of freefile and freeblks structures.
9108 ACQUIRE_LOCK(ip->i_ump);
9109 if (!IS_SNAPSHOT(ip) && softdep_excess_dirrem(ip->i_ump))
9110 schedule_cleanup(ITOV(dp)->v_mount);
9112 FREE_LOCK(ip->i_ump);
9113 dirrem = malloc(sizeof(struct dirrem), M_DIRREM, M_SOFTDEP_FLAGS |
9115 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount);
9116 LIST_INIT(&dirrem->dm_jremrefhd);
9117 LIST_INIT(&dirrem->dm_jwork);
9118 dirrem->dm_state = isrmdir ? RMDIR : 0;
9119 dirrem->dm_oldinum = ip->i_number;
9120 *prevdirremp = NULL;
9122 * Allocate remove reference structures to track journal write
9123 * dependencies. We will always have one for the link and
9124 * when doing directories we will always have one more for dot.
9125 * When renaming a directory we skip the dotdot link change so
9126 * this is not needed.
9128 jremref = dotremref = dotdotremref = NULL;
9129 if (DOINGSUJ(dvp)) {
9131 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9132 ip->i_effnlink + 2);
9133 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET,
9134 ip->i_effnlink + 1);
9135 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET,
9136 dp->i_effnlink + 1);
9137 dotdotremref->jr_state |= MKDIR_PARENT;
9139 jremref = newjremref(dirrem, dp, ip, dp->i_offset,
9140 ip->i_effnlink + 1);
9142 ACQUIRE_LOCK(ip->i_ump);
9143 lbn = lblkno(dp->i_fs, dp->i_offset);
9144 offset = blkoff(dp->i_fs, dp->i_offset);
9145 pagedep_lookup(UFSTOVFS(dp->i_ump), bp, dp->i_number, lbn, DEPALLOC,
9147 dirrem->dm_pagedep = pagedep;
9148 dirrem->dm_offset = offset;
9150 * If we're renaming a .. link to a new directory, cancel any
9151 * existing MKDIR_PARENT mkdir. If it has already been canceled
9152 * the jremref is preserved for any potential diradd in this
9153 * location. This can not coincide with a rmdir.
9155 if (dp->i_offset == DOTDOT_OFFSET) {
9157 panic("newdirrem: .. directory change during remove?");
9158 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref);
9161 * If we're removing a directory search for the .. dependency now and
9162 * cancel it. Any pending journal work will be added to the dirrem
9163 * to be completed when the workitem remove completes.
9166 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref);
9168 * Check for a diradd dependency for the same directory entry.
9169 * If present, then both dependencies become obsolete and can
9172 dap = diradd_lookup(pagedep, offset);
9175 * Link the jremref structures into the dirrem so they are
9176 * written prior to the pagedep.
9179 dirrem_journal(dirrem, jremref, dotremref,
9184 * Must be ATTACHED at this point.
9186 if ((dap->da_state & ATTACHED) == 0)
9187 panic("newdirrem: not ATTACHED");
9188 if (dap->da_newinum != ip->i_number)
9189 panic("newdirrem: inum %ju should be %ju",
9190 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum);
9192 * If we are deleting a changed name that never made it to disk,
9193 * then return the dirrem describing the previous inode (which
9194 * represents the inode currently referenced from this entry on disk).
9196 if ((dap->da_state & DIRCHG) != 0) {
9197 *prevdirremp = dap->da_previous;
9198 dap->da_state &= ~DIRCHG;
9199 dap->da_pagedep = pagedep;
9202 * We are deleting an entry that never made it to disk.
9203 * Mark it COMPLETE so we can delete its inode immediately.
9205 dirrem->dm_state |= COMPLETE;
9206 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref);
9209 struct worklist *wk;
9211 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list)
9212 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT))
9213 panic("bad wk %p (0x%X)\n", wk, wk->wk_state);
9221 * Directory entry change dependencies.
9223 * Changing an existing directory entry requires that an add operation
9224 * be completed first followed by a deletion. The semantics for the addition
9225 * are identical to the description of adding a new entry above except
9226 * that the rollback is to the old inode number rather than zero. Once
9227 * the addition dependency is completed, the removal is done as described
9228 * in the removal routine above.
9232 * This routine should be called immediately after changing
9233 * a directory entry. The inode's link count should not be
9234 * decremented by the calling procedure -- the soft updates
9235 * code will perform this task when it is safe.
9238 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
9239 struct buf *bp; /* buffer containing directory block */
9240 struct inode *dp; /* inode for the directory being modified */
9241 struct inode *ip; /* inode for directory entry being removed */
9242 ino_t newinum; /* new inode number for changed entry */
9243 int isrmdir; /* indicates if doing RMDIR */
9246 struct diradd *dap = NULL;
9247 struct dirrem *dirrem, *prevdirrem;
9248 struct pagedep *pagedep;
9249 struct inodedep *inodedep;
9250 struct jaddref *jaddref;
9253 offset = blkoff(dp->i_fs, dp->i_offset);
9254 mp = UFSTOVFS(dp->i_ump);
9255 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
9256 ("softdep_setup_directory_change called on non-softdep filesystem"));
9259 * Whiteouts do not need diradd dependencies.
9261 if (newinum != WINO) {
9262 dap = malloc(sizeof(struct diradd),
9263 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
9264 workitem_alloc(&dap->da_list, D_DIRADD, mp);
9265 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
9266 dap->da_offset = offset;
9267 dap->da_newinum = newinum;
9268 LIST_INIT(&dap->da_jwork);
9272 * Allocate a new dirrem and ACQUIRE_LOCK.
9274 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
9275 pagedep = dirrem->dm_pagedep;
9277 * The possible values for isrmdir:
9278 * 0 - non-directory file rename
9279 * 1 - directory rename within same directory
9280 * inum - directory rename to new directory of given inode number
9281 * When renaming to a new directory, we are both deleting and
9282 * creating a new directory entry, so the link count on the new
9283 * directory should not change. Thus we do not need the followup
9284 * dirrem which is usually done in handle_workitem_remove. We set
9285 * the DIRCHG flag to tell handle_workitem_remove to skip the
9289 dirrem->dm_state |= DIRCHG;
9292 * Whiteouts have no additional dependencies,
9293 * so just put the dirrem on the correct list.
9295 if (newinum == WINO) {
9296 if ((dirrem->dm_state & COMPLETE) == 0) {
9297 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
9300 dirrem->dm_dirinum = pagedep->pd_ino;
9301 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9302 add_to_worklist(&dirrem->dm_list, 0);
9304 FREE_LOCK(dp->i_ump);
9308 * Add the dirrem to the inodedep's pending remove list for quick
9309 * discovery later. A valid nlinkdelta ensures that this lookup
9312 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
9313 panic("softdep_setup_directory_change: Lost inodedep.");
9314 dirrem->dm_state |= ONDEPLIST;
9315 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9318 * If the COMPLETE flag is clear, then there were no active
9319 * entries and we want to roll back to the previous inode until
9320 * the new inode is committed to disk. If the COMPLETE flag is
9321 * set, then we have deleted an entry that never made it to disk.
9322 * If the entry we deleted resulted from a name change, then the old
9323 * inode reference still resides on disk. Any rollback that we do
9324 * needs to be to that old inode (returned to us in prevdirrem). If
9325 * the entry we deleted resulted from a create, then there is
9326 * no entry on the disk, so we want to roll back to zero rather
9327 * than the uncommitted inode. In either of the COMPLETE cases we
9328 * want to immediately free the unwritten and unreferenced inode.
9330 if ((dirrem->dm_state & COMPLETE) == 0) {
9331 dap->da_previous = dirrem;
9333 if (prevdirrem != NULL) {
9334 dap->da_previous = prevdirrem;
9336 dap->da_state &= ~DIRCHG;
9337 dap->da_pagedep = pagedep;
9339 dirrem->dm_dirinum = pagedep->pd_ino;
9340 if (LIST_EMPTY(&dirrem->dm_jremrefhd))
9341 add_to_worklist(&dirrem->dm_list, 0);
9344 * Lookup the jaddref for this journal entry. We must finish
9345 * initializing it and make the diradd write dependent on it.
9346 * If we're not journaling, put it on the id_bufwait list if the
9347 * inode is not yet written. If it is written, do the post-inode
9348 * write processing to put it on the id_pendinghd list.
9350 inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
9351 if (MOUNTEDSUJ(mp)) {
9352 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst,
9354 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number,
9355 ("softdep_setup_directory_change: bad jaddref %p",
9357 jaddref->ja_diroff = dp->i_offset;
9358 jaddref->ja_diradd = dap;
9359 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9361 add_to_journal(&jaddref->ja_list);
9362 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
9363 dap->da_state |= COMPLETE;
9364 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
9365 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
9367 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
9369 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
9372 * If we're making a new name for a directory that has not been
9373 * committed when need to move the dot and dotdot references to
9376 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET)
9377 merge_diradd(inodedep, dap);
9378 FREE_LOCK(dp->i_ump);
9382 * Called whenever the link count on an inode is changed.
9383 * It creates an inode dependency so that the new reference(s)
9384 * to the inode cannot be committed to disk until the updated
9385 * inode has been written.
9388 softdep_change_linkcnt(ip)
9389 struct inode *ip; /* the inode with the increased link count */
9391 struct inodedep *inodedep;
9393 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
9394 ("softdep_change_linkcnt called on non-softdep filesystem"));
9395 ACQUIRE_LOCK(ip->i_ump);
9396 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, DEPALLOC,
9398 if (ip->i_nlink < ip->i_effnlink)
9399 panic("softdep_change_linkcnt: bad delta");
9400 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9401 FREE_LOCK(ip->i_ump);
9405 * Attach a sbdep dependency to the superblock buf so that we can keep
9406 * track of the head of the linked list of referenced but unlinked inodes.
9409 softdep_setup_sbupdate(ump, fs, bp)
9410 struct ufsmount *ump;
9414 struct sbdep *sbdep;
9415 struct worklist *wk;
9417 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
9418 ("softdep_setup_sbupdate called on non-softdep filesystem"));
9419 LIST_FOREACH(wk, &bp->b_dep, wk_list)
9420 if (wk->wk_type == D_SBDEP)
9424 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS);
9425 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump));
9427 sbdep->sb_ump = ump;
9429 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list);
9434 * Return the first unlinked inodedep which is ready to be the head of the
9435 * list. The inodedep and all those after it must have valid next pointers.
9437 static struct inodedep *
9438 first_unlinked_inodedep(ump)
9439 struct ufsmount *ump;
9441 struct inodedep *inodedep;
9442 struct inodedep *idp;
9445 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst);
9446 inodedep; inodedep = idp) {
9447 if ((inodedep->id_state & UNLINKNEXT) == 0)
9449 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9450 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0)
9452 if ((inodedep->id_state & UNLINKPREV) == 0)
9459 * Set the sujfree unlinked head pointer prior to writing a superblock.
9462 initiate_write_sbdep(sbdep)
9463 struct sbdep *sbdep;
9465 struct inodedep *inodedep;
9469 bpfs = sbdep->sb_fs;
9470 fs = sbdep->sb_ump->um_fs;
9471 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9473 fs->fs_sujfree = inodedep->id_ino;
9474 inodedep->id_state |= UNLINKPREV;
9477 bpfs->fs_sujfree = fs->fs_sujfree;
9481 * After a superblock is written determine whether it must be written again
9482 * due to a changing unlinked list head.
9485 handle_written_sbdep(sbdep, bp)
9486 struct sbdep *sbdep;
9489 struct inodedep *inodedep;
9492 LOCK_OWNED(sbdep->sb_ump);
9495 * If the superblock doesn't match the in-memory list start over.
9497 inodedep = first_unlinked_inodedep(sbdep->sb_ump);
9498 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) ||
9499 (inodedep == NULL && fs->fs_sujfree != 0)) {
9503 WORKITEM_FREE(sbdep, D_SBDEP);
9504 if (fs->fs_sujfree == 0)
9507 * Now that we have a record of this inode in stable store allow it
9508 * to be written to free up pending work. Inodes may see a lot of
9509 * write activity after they are unlinked which we must not hold up.
9511 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
9512 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS)
9513 panic("handle_written_sbdep: Bad inodedep %p (0x%X)",
9514 inodedep, inodedep->id_state);
9515 if (inodedep->id_state & UNLINKONLIST)
9517 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST;
9524 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list.
9527 unlinked_inodedep(mp, inodedep)
9529 struct inodedep *inodedep;
9531 struct ufsmount *ump;
9535 if (MOUNTEDSUJ(mp) == 0)
9537 ump->um_fs->fs_fmod = 1;
9538 if (inodedep->id_state & UNLINKED)
9539 panic("unlinked_inodedep: %p already unlinked\n", inodedep);
9540 inodedep->id_state |= UNLINKED;
9541 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked);
9545 * Remove an inodedep from the unlinked inodedep list. This may require
9546 * disk writes if the inode has made it that far.
9549 clear_unlinked_inodedep(inodedep)
9550 struct inodedep *inodedep;
9552 struct ufsmount *ump;
9553 struct inodedep *idp;
9554 struct inodedep *idn;
9562 ump = VFSTOUFS(inodedep->id_list.wk_mp);
9564 ino = inodedep->id_ino;
9568 KASSERT((inodedep->id_state & UNLINKED) != 0,
9569 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9572 * If nothing has yet been written simply remove us from
9573 * the in memory list and return. This is the most common
9574 * case where handle_workitem_remove() loses the final
9577 if ((inodedep->id_state & UNLINKLINKS) == 0)
9580 * If we have a NEXT pointer and no PREV pointer we can simply
9581 * clear NEXT's PREV and remove ourselves from the list. Be
9582 * careful not to clear PREV if the superblock points at
9585 idn = TAILQ_NEXT(inodedep, id_unlinked);
9586 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) {
9587 if (idn && fs->fs_sujfree != idn->id_ino)
9588 idn->id_state &= ~UNLINKPREV;
9592 * Here we have an inodedep which is actually linked into
9593 * the list. We must remove it by forcing a write to the
9594 * link before us, whether it be the superblock or an inode.
9595 * Unfortunately the list may change while we're waiting
9596 * on the buf lock for either resource so we must loop until
9597 * we lock the right one. If both the superblock and an
9598 * inode point to this inode we must clear the inode first
9599 * followed by the superblock.
9601 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9603 if (idp && (idp->id_state & UNLINKNEXT))
9607 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9608 (int)fs->fs_sbsize, 0, 0, 0);
9610 error = bread(ump->um_devvp,
9611 fsbtodb(fs, ino_to_fsba(fs, pino)),
9612 (int)fs->fs_bsize, NOCRED, &bp);
9619 /* If the list has changed restart the loop. */
9620 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked);
9622 if (idp && (idp->id_state & UNLINKNEXT))
9625 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) {
9632 idn = TAILQ_NEXT(inodedep, id_unlinked);
9636 * Remove us from the in memory list. After this we cannot
9637 * access the inodedep.
9639 KASSERT((inodedep->id_state & UNLINKED) != 0,
9640 ("clear_unlinked_inodedep: inodedep %p not unlinked",
9642 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9643 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9646 * The predecessor's next pointer is manually updated here
9647 * so that the NEXT flag is never cleared for an element
9648 * that is in the list.
9651 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9652 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
9653 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data,
9655 } else if (fs->fs_magic == FS_UFS1_MAGIC)
9656 ((struct ufs1_dinode *)bp->b_data +
9657 ino_to_fsbo(fs, pino))->di_freelink = nino;
9659 ((struct ufs2_dinode *)bp->b_data +
9660 ino_to_fsbo(fs, pino))->di_freelink = nino;
9662 * If the bwrite fails we have no recourse to recover. The
9663 * filesystem is corrupted already.
9668 * If the superblock pointer still needs to be cleared force
9671 if (fs->fs_sujfree == ino) {
9673 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc),
9674 (int)fs->fs_sbsize, 0, 0, 0);
9675 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
9676 ffs_oldfscompat_write((struct fs *)bp->b_data, ump);
9677 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data,
9683 if (fs->fs_sujfree != ino)
9685 panic("clear_unlinked_inodedep: Failed to clear free head");
9687 if (inodedep->id_ino == fs->fs_sujfree)
9688 panic("clear_unlinked_inodedep: Freeing head of free list");
9689 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST);
9690 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked);
9695 * This workitem decrements the inode's link count.
9696 * If the link count reaches zero, the file is removed.
9699 handle_workitem_remove(dirrem, flags)
9700 struct dirrem *dirrem;
9703 struct inodedep *inodedep;
9704 struct workhead dotdotwk;
9705 struct worklist *wk;
9706 struct ufsmount *ump;
9712 if (dirrem->dm_state & ONWORKLIST)
9713 panic("handle_workitem_remove: dirrem %p still on worklist",
9715 oldinum = dirrem->dm_oldinum;
9716 mp = dirrem->dm_list.wk_mp;
9718 flags |= LK_EXCLUSIVE;
9719 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0)
9723 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0)
9724 panic("handle_workitem_remove: lost inodedep");
9725 if (dirrem->dm_state & ONDEPLIST)
9726 LIST_REMOVE(dirrem, dm_inonext);
9727 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
9728 ("handle_workitem_remove: Journal entries not written."));
9731 * Move all dependencies waiting on the remove to complete
9732 * from the dirrem to the inode inowait list to be completed
9733 * after the inode has been updated and written to disk. Any
9734 * marked MKDIR_PARENT are saved to be completed when the .. ref
9737 LIST_INIT(&dotdotwk);
9738 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) {
9739 WORKLIST_REMOVE(wk);
9740 if (wk->wk_state & MKDIR_PARENT) {
9741 wk->wk_state &= ~MKDIR_PARENT;
9742 WORKLIST_INSERT(&dotdotwk, wk);
9745 WORKLIST_INSERT(&inodedep->id_inowait, wk);
9747 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list);
9749 * Normal file deletion.
9751 if ((dirrem->dm_state & RMDIR) == 0) {
9753 DIP_SET(ip, i_nlink, ip->i_nlink);
9754 ip->i_flag |= IN_CHANGE;
9755 if (ip->i_nlink < ip->i_effnlink)
9756 panic("handle_workitem_remove: bad file delta");
9757 if (ip->i_nlink == 0)
9758 unlinked_inodedep(mp, inodedep);
9759 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9760 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9761 ("handle_workitem_remove: worklist not empty. %s",
9762 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type)));
9763 WORKITEM_FREE(dirrem, D_DIRREM);
9768 * Directory deletion. Decrement reference count for both the
9769 * just deleted parent directory entry and the reference for ".".
9770 * Arrange to have the reference count on the parent decremented
9771 * to account for the loss of "..".
9774 DIP_SET(ip, i_nlink, ip->i_nlink);
9775 ip->i_flag |= IN_CHANGE;
9776 if (ip->i_nlink < ip->i_effnlink)
9777 panic("handle_workitem_remove: bad dir delta");
9778 if (ip->i_nlink == 0)
9779 unlinked_inodedep(mp, inodedep);
9780 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
9782 * Rename a directory to a new parent. Since, we are both deleting
9783 * and creating a new directory entry, the link count on the new
9784 * directory should not change. Thus we skip the followup dirrem.
9786 if (dirrem->dm_state & DIRCHG) {
9787 KASSERT(LIST_EMPTY(&dirrem->dm_jwork),
9788 ("handle_workitem_remove: DIRCHG and worklist not empty."));
9789 WORKITEM_FREE(dirrem, D_DIRREM);
9793 dirrem->dm_state = ONDEPLIST;
9794 dirrem->dm_oldinum = dirrem->dm_dirinum;
9796 * Place the dirrem on the parent's diremhd list.
9798 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0)
9799 panic("handle_workitem_remove: lost dir inodedep");
9800 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext);
9802 * If the allocated inode has never been written to disk, then
9803 * the on-disk inode is zero'ed and we can remove the file
9804 * immediately. When journaling if the inode has been marked
9805 * unlinked and not DEPCOMPLETE we know it can never be written.
9807 inodedep_lookup(mp, oldinum, 0, &inodedep);
9808 if (inodedep == NULL ||
9809 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED ||
9810 check_inode_unwritten(inodedep)) {
9813 return handle_workitem_remove(dirrem, flags);
9815 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
9817 ip->i_flag |= IN_CHANGE;
9825 * Inode de-allocation dependencies.
9827 * When an inode's link count is reduced to zero, it can be de-allocated. We
9828 * found it convenient to postpone de-allocation until after the inode is
9829 * written to disk with its new link count (zero). At this point, all of the
9830 * on-disk inode's block pointers are nullified and, with careful dependency
9831 * list ordering, all dependencies related to the inode will be satisfied and
9832 * the corresponding dependency structures de-allocated. So, if/when the
9833 * inode is reused, there will be no mixing of old dependencies with new
9834 * ones. This artificial dependency is set up by the block de-allocation
9835 * procedure above (softdep_setup_freeblocks) and completed by the
9836 * following procedure.
9839 handle_workitem_freefile(freefile)
9840 struct freefile *freefile;
9842 struct workhead wkhd;
9844 struct inodedep *idp;
9845 struct ufsmount *ump;
9848 ump = VFSTOUFS(freefile->fx_list.wk_mp);
9852 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
9855 panic("handle_workitem_freefile: inodedep %p survived", idp);
9858 fs->fs_pendinginodes -= 1;
9861 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list);
9862 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
9863 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0)
9864 softdep_error("handle_workitem_freefile", error);
9866 WORKITEM_FREE(freefile, D_FREEFILE);
9872 * Helper function which unlinks marker element from work list and returns
9873 * the next element on the list.
9875 static __inline struct worklist *
9876 markernext(struct worklist *marker)
9878 struct worklist *next;
9880 next = LIST_NEXT(marker, wk_list);
9881 LIST_REMOVE(marker, wk_list);
9888 * The dependency structures constructed above are most actively used when file
9889 * system blocks are written to disk. No constraints are placed on when a
9890 * block can be written, but unsatisfied update dependencies are made safe by
9891 * modifying (or replacing) the source memory for the duration of the disk
9892 * write. When the disk write completes, the memory block is again brought
9895 * In-core inode structure reclamation.
9897 * Because there are a finite number of "in-core" inode structures, they are
9898 * reused regularly. By transferring all inode-related dependencies to the
9899 * in-memory inode block and indexing them separately (via "inodedep"s), we
9900 * can allow "in-core" inode structures to be reused at any time and avoid
9901 * any increase in contention.
9903 * Called just before entering the device driver to initiate a new disk I/O.
9904 * The buffer must be locked, thus, no I/O completion operations can occur
9905 * while we are manipulating its associated dependencies.
9908 softdep_disk_io_initiation(bp)
9909 struct buf *bp; /* structure describing disk write to occur */
9911 struct worklist *wk;
9912 struct worklist marker;
9913 struct inodedep *inodedep;
9914 struct freeblks *freeblks;
9915 struct jblkdep *jblkdep;
9916 struct newblk *newblk;
9917 struct ufsmount *ump;
9920 * We only care about write operations. There should never
9921 * be dependencies for reads.
9923 if (bp->b_iocmd != BIO_WRITE)
9924 panic("softdep_disk_io_initiation: not write");
9926 if (bp->b_vflags & BV_BKGRDINPROG)
9927 panic("softdep_disk_io_initiation: Writing buffer with "
9928 "background write in progress: %p", bp);
9930 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL)
9932 ump = VFSTOUFS(wk->wk_mp);
9934 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
9935 PHOLD(curproc); /* Don't swap out kernel stack */
9938 * Do any necessary pre-I/O processing.
9940 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
9941 wk = markernext(&marker)) {
9942 LIST_INSERT_AFTER(wk, &marker, wk_list);
9943 switch (wk->wk_type) {
9946 initiate_write_filepage(WK_PAGEDEP(wk), bp);
9950 inodedep = WK_INODEDEP(wk);
9951 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
9952 initiate_write_inodeblock_ufs1(inodedep, bp);
9954 initiate_write_inodeblock_ufs2(inodedep, bp);
9958 initiate_write_indirdep(WK_INDIRDEP(wk), bp);
9962 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp);
9966 WK_JSEG(wk)->js_buf = NULL;
9970 freeblks = WK_FREEBLKS(wk);
9971 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd);
9973 * We have to wait for the freeblks to be journaled
9974 * before we can write an inodeblock with updated
9975 * pointers. Be careful to arrange the marker so
9976 * we revisit the freeblks if it's not removed by
9977 * the first jwait().
9979 if (jblkdep != NULL) {
9980 LIST_REMOVE(&marker, wk_list);
9981 LIST_INSERT_BEFORE(wk, &marker, wk_list);
9982 jwait(&jblkdep->jb_list, MNT_WAIT);
9988 * We have to wait for the jnewblk to be journaled
9989 * before we can write to a block if the contents
9990 * may be confused with an earlier file's indirect
9991 * at recovery time. Handle the marker as described
9994 newblk = WK_NEWBLK(wk);
9995 if (newblk->nb_jnewblk != NULL &&
9996 indirblk_lookup(newblk->nb_list.wk_mp,
9997 newblk->nb_newblkno)) {
9998 LIST_REMOVE(&marker, wk_list);
9999 LIST_INSERT_BEFORE(wk, &marker, wk_list);
10000 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
10005 initiate_write_sbdep(WK_SBDEP(wk));
10015 panic("handle_disk_io_initiation: Unexpected type %s",
10016 TYPENAME(wk->wk_type));
10021 PRELE(curproc); /* Allow swapout of kernel stack */
10025 * Called from within the procedure above to deal with unsatisfied
10026 * allocation dependencies in a directory. The buffer must be locked,
10027 * thus, no I/O completion operations can occur while we are
10028 * manipulating its associated dependencies.
10031 initiate_write_filepage(pagedep, bp)
10032 struct pagedep *pagedep;
10035 struct jremref *jremref;
10036 struct jmvref *jmvref;
10037 struct dirrem *dirrem;
10038 struct diradd *dap;
10042 if (pagedep->pd_state & IOSTARTED) {
10044 * This can only happen if there is a driver that does not
10045 * understand chaining. Here biodone will reissue the call
10046 * to strategy for the incomplete buffers.
10048 printf("initiate_write_filepage: already started\n");
10051 pagedep->pd_state |= IOSTARTED;
10053 * Wait for all journal remove dependencies to hit the disk.
10054 * We can not allow any potentially conflicting directory adds
10055 * to be visible before removes and rollback is too difficult.
10056 * The per-filesystem lock may be dropped and re-acquired, however
10057 * we hold the buf locked so the dependency can not go away.
10059 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next)
10060 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL)
10061 jwait(&jremref->jr_list, MNT_WAIT);
10062 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL)
10063 jwait(&jmvref->jm_list, MNT_WAIT);
10064 for (i = 0; i < DAHASHSZ; i++) {
10065 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
10066 ep = (struct direct *)
10067 ((char *)bp->b_data + dap->da_offset);
10068 if (ep->d_ino != dap->da_newinum)
10069 panic("%s: dir inum %ju != new %ju",
10070 "initiate_write_filepage",
10071 (uintmax_t)ep->d_ino,
10072 (uintmax_t)dap->da_newinum);
10073 if (dap->da_state & DIRCHG)
10074 ep->d_ino = dap->da_previous->dm_oldinum;
10077 dap->da_state &= ~ATTACHED;
10078 dap->da_state |= UNDONE;
10084 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
10085 * Note that any bug fixes made to this routine must be done in the
10086 * version found below.
10088 * Called from within the procedure above to deal with unsatisfied
10089 * allocation dependencies in an inodeblock. The buffer must be
10090 * locked, thus, no I/O completion operations can occur while we
10091 * are manipulating its associated dependencies.
10094 initiate_write_inodeblock_ufs1(inodedep, bp)
10095 struct inodedep *inodedep;
10096 struct buf *bp; /* The inode block */
10098 struct allocdirect *adp, *lastadp;
10099 struct ufs1_dinode *dp;
10100 struct ufs1_dinode *sip;
10101 struct inoref *inoref;
10102 struct ufsmount *ump;
10106 ufs_lbn_t prevlbn = 0;
10110 if (inodedep->id_state & IOSTARTED)
10111 panic("initiate_write_inodeblock_ufs1: already started");
10112 inodedep->id_state |= IOSTARTED;
10113 fs = inodedep->id_fs;
10114 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10116 dp = (struct ufs1_dinode *)bp->b_data +
10117 ino_to_fsbo(fs, inodedep->id_ino);
10120 * If we're on the unlinked list but have not yet written our
10121 * next pointer initialize it here.
10123 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10124 struct inodedep *inon;
10126 inon = TAILQ_NEXT(inodedep, id_unlinked);
10127 dp->di_freelink = inon ? inon->id_ino : 0;
10130 * If the bitmap is not yet written, then the allocated
10131 * inode cannot be written to disk.
10133 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10134 if (inodedep->id_savedino1 != NULL)
10135 panic("initiate_write_inodeblock_ufs1: I/O underway");
10137 sip = malloc(sizeof(struct ufs1_dinode),
10138 M_SAVEDINO, M_SOFTDEP_FLAGS);
10140 inodedep->id_savedino1 = sip;
10141 *inodedep->id_savedino1 = *dp;
10142 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
10143 dp->di_gen = inodedep->id_savedino1->di_gen;
10144 dp->di_freelink = inodedep->id_savedino1->di_freelink;
10148 * If no dependencies, then there is nothing to roll back.
10150 inodedep->id_savedsize = dp->di_size;
10151 inodedep->id_savedextsize = 0;
10152 inodedep->id_savednlink = dp->di_nlink;
10153 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10154 TAILQ_EMPTY(&inodedep->id_inoreflst))
10157 * Revert the link count to that of the first unwritten journal entry.
10159 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10161 dp->di_nlink = inoref->if_nlink;
10163 * Set the dependencies to busy.
10165 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10166 adp = TAILQ_NEXT(adp, ad_next)) {
10168 if (deplist != 0 && prevlbn >= adp->ad_offset)
10169 panic("softdep_write_inodeblock: lbn order");
10170 prevlbn = adp->ad_offset;
10171 if (adp->ad_offset < NDADDR &&
10172 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10173 panic("%s: direct pointer #%jd mismatch %d != %jd",
10174 "softdep_write_inodeblock",
10175 (intmax_t)adp->ad_offset,
10176 dp->di_db[adp->ad_offset],
10177 (intmax_t)adp->ad_newblkno);
10178 if (adp->ad_offset >= NDADDR &&
10179 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno)
10180 panic("%s: indirect pointer #%jd mismatch %d != %jd",
10181 "softdep_write_inodeblock",
10182 (intmax_t)adp->ad_offset - NDADDR,
10183 dp->di_ib[adp->ad_offset - NDADDR],
10184 (intmax_t)adp->ad_newblkno);
10185 deplist |= 1 << adp->ad_offset;
10186 if ((adp->ad_state & ATTACHED) == 0)
10187 panic("softdep_write_inodeblock: Unknown state 0x%x",
10189 #endif /* INVARIANTS */
10190 adp->ad_state &= ~ATTACHED;
10191 adp->ad_state |= UNDONE;
10194 * The on-disk inode cannot claim to be any larger than the last
10195 * fragment that has been written. Otherwise, the on-disk inode
10196 * might have fragments that were not the last block in the file
10197 * which would corrupt the filesystem.
10199 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10200 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10201 if (adp->ad_offset >= NDADDR)
10203 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10204 /* keep going until hitting a rollback to a frag */
10205 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10207 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10208 for (i = adp->ad_offset + 1; i < NDADDR; i++) {
10210 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10211 panic("softdep_write_inodeblock: lost dep1");
10212 #endif /* INVARIANTS */
10215 for (i = 0; i < NIADDR; i++) {
10217 if (dp->di_ib[i] != 0 &&
10218 (deplist & ((1 << NDADDR) << i)) == 0)
10219 panic("softdep_write_inodeblock: lost dep2");
10220 #endif /* INVARIANTS */
10226 * If we have zero'ed out the last allocated block of the file,
10227 * roll back the size to the last currently allocated block.
10228 * We know that this last allocated block is a full-sized as
10229 * we already checked for fragments in the loop above.
10231 if (lastadp != NULL &&
10232 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10233 for (i = lastadp->ad_offset; i >= 0; i--)
10234 if (dp->di_db[i] != 0)
10236 dp->di_size = (i + 1) * fs->fs_bsize;
10239 * The only dependencies are for indirect blocks.
10241 * The file size for indirect block additions is not guaranteed.
10242 * Such a guarantee would be non-trivial to achieve. The conventional
10243 * synchronous write implementation also does not make this guarantee.
10244 * Fsck should catch and fix discrepancies. Arguably, the file size
10245 * can be over-estimated without destroying integrity when the file
10246 * moves into the indirect blocks (i.e., is large). If we want to
10247 * postpone fsck, we are stuck with this argument.
10249 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10250 dp->di_ib[adp->ad_offset - NDADDR] = 0;
10254 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
10255 * Note that any bug fixes made to this routine must be done in the
10256 * version found above.
10258 * Called from within the procedure above to deal with unsatisfied
10259 * allocation dependencies in an inodeblock. The buffer must be
10260 * locked, thus, no I/O completion operations can occur while we
10261 * are manipulating its associated dependencies.
10264 initiate_write_inodeblock_ufs2(inodedep, bp)
10265 struct inodedep *inodedep;
10266 struct buf *bp; /* The inode block */
10268 struct allocdirect *adp, *lastadp;
10269 struct ufs2_dinode *dp;
10270 struct ufs2_dinode *sip;
10271 struct inoref *inoref;
10272 struct ufsmount *ump;
10276 ufs_lbn_t prevlbn = 0;
10280 if (inodedep->id_state & IOSTARTED)
10281 panic("initiate_write_inodeblock_ufs2: already started");
10282 inodedep->id_state |= IOSTARTED;
10283 fs = inodedep->id_fs;
10284 ump = VFSTOUFS(inodedep->id_list.wk_mp);
10286 dp = (struct ufs2_dinode *)bp->b_data +
10287 ino_to_fsbo(fs, inodedep->id_ino);
10290 * If we're on the unlinked list but have not yet written our
10291 * next pointer initialize it here.
10293 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) {
10294 struct inodedep *inon;
10296 inon = TAILQ_NEXT(inodedep, id_unlinked);
10297 dp->di_freelink = inon ? inon->id_ino : 0;
10300 * If the bitmap is not yet written, then the allocated
10301 * inode cannot be written to disk.
10303 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
10304 if (inodedep->id_savedino2 != NULL)
10305 panic("initiate_write_inodeblock_ufs2: I/O underway");
10307 sip = malloc(sizeof(struct ufs2_dinode),
10308 M_SAVEDINO, M_SOFTDEP_FLAGS);
10310 inodedep->id_savedino2 = sip;
10311 *inodedep->id_savedino2 = *dp;
10312 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
10313 dp->di_gen = inodedep->id_savedino2->di_gen;
10314 dp->di_freelink = inodedep->id_savedino2->di_freelink;
10318 * If no dependencies, then there is nothing to roll back.
10320 inodedep->id_savedsize = dp->di_size;
10321 inodedep->id_savedextsize = dp->di_extsize;
10322 inodedep->id_savednlink = dp->di_nlink;
10323 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
10324 TAILQ_EMPTY(&inodedep->id_extupdt) &&
10325 TAILQ_EMPTY(&inodedep->id_inoreflst))
10328 * Revert the link count to that of the first unwritten journal entry.
10330 inoref = TAILQ_FIRST(&inodedep->id_inoreflst);
10332 dp->di_nlink = inoref->if_nlink;
10335 * Set the ext data dependencies to busy.
10337 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10338 adp = TAILQ_NEXT(adp, ad_next)) {
10340 if (deplist != 0 && prevlbn >= adp->ad_offset)
10341 panic("softdep_write_inodeblock: lbn order");
10342 prevlbn = adp->ad_offset;
10343 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno)
10344 panic("%s: direct pointer #%jd mismatch %jd != %jd",
10345 "softdep_write_inodeblock",
10346 (intmax_t)adp->ad_offset,
10347 (intmax_t)dp->di_extb[adp->ad_offset],
10348 (intmax_t)adp->ad_newblkno);
10349 deplist |= 1 << adp->ad_offset;
10350 if ((adp->ad_state & ATTACHED) == 0)
10351 panic("softdep_write_inodeblock: Unknown state 0x%x",
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 ext
10361 * data which would corrupt the filesystem.
10363 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
10364 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10365 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno;
10366 /* keep going until hitting a rollback to a frag */
10367 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10369 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10370 for (i = adp->ad_offset + 1; i < NXADDR; i++) {
10372 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
10373 panic("softdep_write_inodeblock: lost dep1");
10374 #endif /* INVARIANTS */
10375 dp->di_extb[i] = 0;
10381 * If we have zero'ed out the last allocated block of the ext
10382 * data, roll back the size to the last currently allocated block.
10383 * We know that this last allocated block is a full-sized as
10384 * we already checked for fragments in the loop above.
10386 if (lastadp != NULL &&
10387 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10388 for (i = lastadp->ad_offset; i >= 0; i--)
10389 if (dp->di_extb[i] != 0)
10391 dp->di_extsize = (i + 1) * fs->fs_bsize;
10394 * Set the file data dependencies to busy.
10396 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10397 adp = TAILQ_NEXT(adp, ad_next)) {
10399 if (deplist != 0 && prevlbn >= adp->ad_offset)
10400 panic("softdep_write_inodeblock: lbn order");
10401 if ((adp->ad_state & ATTACHED) == 0)
10402 panic("inodedep %p and adp %p not attached", inodedep, adp);
10403 prevlbn = adp->ad_offset;
10404 if (adp->ad_offset < NDADDR &&
10405 dp->di_db[adp->ad_offset] != adp->ad_newblkno)
10406 panic("%s: direct pointer #%jd mismatch %jd != %jd",
10407 "softdep_write_inodeblock",
10408 (intmax_t)adp->ad_offset,
10409 (intmax_t)dp->di_db[adp->ad_offset],
10410 (intmax_t)adp->ad_newblkno);
10411 if (adp->ad_offset >= NDADDR &&
10412 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno)
10413 panic("%s indirect pointer #%jd mismatch %jd != %jd",
10414 "softdep_write_inodeblock:",
10415 (intmax_t)adp->ad_offset - NDADDR,
10416 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR],
10417 (intmax_t)adp->ad_newblkno);
10418 deplist |= 1 << adp->ad_offset;
10419 if ((adp->ad_state & ATTACHED) == 0)
10420 panic("softdep_write_inodeblock: Unknown state 0x%x",
10422 #endif /* INVARIANTS */
10423 adp->ad_state &= ~ATTACHED;
10424 adp->ad_state |= UNDONE;
10427 * The on-disk inode cannot claim to be any larger than the last
10428 * fragment that has been written. Otherwise, the on-disk inode
10429 * might have fragments that were not the last block in the file
10430 * which would corrupt the filesystem.
10432 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
10433 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
10434 if (adp->ad_offset >= NDADDR)
10436 dp->di_db[adp->ad_offset] = adp->ad_oldblkno;
10437 /* keep going until hitting a rollback to a frag */
10438 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
10440 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize;
10441 for (i = adp->ad_offset + 1; i < NDADDR; i++) {
10443 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
10444 panic("softdep_write_inodeblock: lost dep2");
10445 #endif /* INVARIANTS */
10448 for (i = 0; i < NIADDR; i++) {
10450 if (dp->di_ib[i] != 0 &&
10451 (deplist & ((1 << NDADDR) << i)) == 0)
10452 panic("softdep_write_inodeblock: lost dep3");
10453 #endif /* INVARIANTS */
10459 * If we have zero'ed out the last allocated block of the file,
10460 * roll back the size to the last currently allocated block.
10461 * We know that this last allocated block is a full-sized as
10462 * we already checked for fragments in the loop above.
10464 if (lastadp != NULL &&
10465 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) {
10466 for (i = lastadp->ad_offset; i >= 0; i--)
10467 if (dp->di_db[i] != 0)
10469 dp->di_size = (i + 1) * fs->fs_bsize;
10472 * The only dependencies are for indirect blocks.
10474 * The file size for indirect block additions is not guaranteed.
10475 * Such a guarantee would be non-trivial to achieve. The conventional
10476 * synchronous write implementation also does not make this guarantee.
10477 * Fsck should catch and fix discrepancies. Arguably, the file size
10478 * can be over-estimated without destroying integrity when the file
10479 * moves into the indirect blocks (i.e., is large). If we want to
10480 * postpone fsck, we are stuck with this argument.
10482 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
10483 dp->di_ib[adp->ad_offset - NDADDR] = 0;
10487 * Cancel an indirdep as a result of truncation. Release all of the
10488 * children allocindirs and place their journal work on the appropriate
10492 cancel_indirdep(indirdep, bp, freeblks)
10493 struct indirdep *indirdep;
10495 struct freeblks *freeblks;
10497 struct allocindir *aip;
10500 * None of the indirect pointers will ever be visible,
10501 * so they can simply be tossed. GOINGAWAY ensures
10502 * that allocated pointers will be saved in the buffer
10503 * cache until they are freed. Note that they will
10504 * only be able to be found by their physical address
10505 * since the inode mapping the logical address will
10506 * be gone. The save buffer used for the safe copy
10507 * was allocated in setup_allocindir_phase2 using
10508 * the physical address so it could be used for this
10509 * purpose. Hence we swap the safe copy with the real
10510 * copy, allowing the safe copy to be freed and holding
10511 * on to the real copy for later use in indir_trunc.
10513 if (indirdep->ir_state & GOINGAWAY)
10514 panic("cancel_indirdep: already gone");
10515 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
10516 indirdep->ir_state |= DEPCOMPLETE;
10517 LIST_REMOVE(indirdep, ir_next);
10519 indirdep->ir_state |= GOINGAWAY;
10521 * Pass in bp for blocks still have journal writes
10522 * pending so we can cancel them on their own.
10524 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
10525 cancel_allocindir(aip, bp, freeblks, 0);
10526 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0)
10527 cancel_allocindir(aip, NULL, freeblks, 0);
10528 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0)
10529 cancel_allocindir(aip, NULL, freeblks, 0);
10530 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != 0)
10531 cancel_allocindir(aip, NULL, freeblks, 0);
10533 * If there are pending partial truncations we need to keep the
10534 * old block copy around until they complete. This is because
10535 * the current b_data is not a perfect superset of the available
10538 if (TAILQ_EMPTY(&indirdep->ir_trunc))
10539 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount);
10541 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10542 WORKLIST_REMOVE(&indirdep->ir_list);
10543 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list);
10544 indirdep->ir_bp = NULL;
10545 indirdep->ir_freeblks = freeblks;
10549 * Free an indirdep once it no longer has new pointers to track.
10552 free_indirdep(indirdep)
10553 struct indirdep *indirdep;
10556 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc),
10557 ("free_indirdep: Indir trunc list not empty."));
10558 KASSERT(LIST_EMPTY(&indirdep->ir_completehd),
10559 ("free_indirdep: Complete head not empty."));
10560 KASSERT(LIST_EMPTY(&indirdep->ir_writehd),
10561 ("free_indirdep: write head not empty."));
10562 KASSERT(LIST_EMPTY(&indirdep->ir_donehd),
10563 ("free_indirdep: done head not empty."));
10564 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd),
10565 ("free_indirdep: deplist head not empty."));
10566 KASSERT((indirdep->ir_state & DEPCOMPLETE),
10567 ("free_indirdep: %p still on newblk list.", indirdep));
10568 KASSERT(indirdep->ir_saveddata == NULL,
10569 ("free_indirdep: %p still has saved data.", indirdep));
10570 if (indirdep->ir_state & ONWORKLIST)
10571 WORKLIST_REMOVE(&indirdep->ir_list);
10572 WORKITEM_FREE(indirdep, D_INDIRDEP);
10576 * Called before a write to an indirdep. This routine is responsible for
10577 * rolling back pointers to a safe state which includes only those
10578 * allocindirs which have been completed.
10581 initiate_write_indirdep(indirdep, bp)
10582 struct indirdep *indirdep;
10585 struct ufsmount *ump;
10587 indirdep->ir_state |= IOSTARTED;
10588 if (indirdep->ir_state & GOINGAWAY)
10589 panic("disk_io_initiation: indirdep gone");
10591 * If there are no remaining dependencies, this will be writing
10592 * the real pointers.
10594 if (LIST_EMPTY(&indirdep->ir_deplisthd) &&
10595 TAILQ_EMPTY(&indirdep->ir_trunc))
10598 * Replace up-to-date version with safe version.
10600 if (indirdep->ir_saveddata == NULL) {
10601 ump = VFSTOUFS(indirdep->ir_list.wk_mp);
10604 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP,
10608 indirdep->ir_state &= ~ATTACHED;
10609 indirdep->ir_state |= UNDONE;
10610 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
10611 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
10616 * Called when an inode has been cleared in a cg bitmap. This finally
10617 * eliminates any canceled jaddrefs
10620 softdep_setup_inofree(mp, bp, ino, wkhd)
10624 struct workhead *wkhd;
10626 struct worklist *wk, *wkn;
10627 struct inodedep *inodedep;
10628 struct ufsmount *ump;
10633 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
10634 ("softdep_setup_inofree called on non-softdep filesystem"));
10635 ump = VFSTOUFS(mp);
10638 cgp = (struct cg *)bp->b_data;
10639 inosused = cg_inosused(cgp);
10640 if (isset(inosused, ino % fs->fs_ipg))
10641 panic("softdep_setup_inofree: inode %ju not freed.",
10643 if (inodedep_lookup(mp, ino, 0, &inodedep))
10644 panic("softdep_setup_inofree: ino %ju has existing inodedep %p",
10645 (uintmax_t)ino, inodedep);
10647 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) {
10648 if (wk->wk_type != D_JADDREF)
10650 WORKLIST_REMOVE(wk);
10652 * We can free immediately even if the jaddref
10653 * isn't attached in a background write as now
10654 * the bitmaps are reconciled.
10656 wk->wk_state |= COMPLETE | ATTACHED;
10657 free_jaddref(WK_JADDREF(wk));
10659 jwork_move(&bp->b_dep, wkhd);
10666 * Called via ffs_blkfree() after a set of frags has been cleared from a cg
10667 * map. Any dependencies waiting for the write to clear are added to the
10668 * buf's list and any jnewblks that are being canceled are discarded
10672 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd)
10675 ufs2_daddr_t blkno;
10677 struct workhead *wkhd;
10679 struct bmsafemap *bmsafemap;
10680 struct jnewblk *jnewblk;
10681 struct ufsmount *ump;
10682 struct worklist *wk;
10687 ufs2_daddr_t jstart;
10695 "softdep_setup_blkfree: blkno %jd frags %d wk head %p",
10696 blkno, frags, wkhd);
10698 ump = VFSTOUFS(mp);
10699 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0,
10700 ("softdep_setup_blkfree called on non-softdep filesystem"));
10702 /* Lookup the bmsafemap so we track when it is dirty. */
10704 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10706 * Detach any jnewblks which have been canceled. They must linger
10707 * until the bitmap is cleared again by ffs_blkfree() to prevent
10708 * an unjournaled allocation from hitting the disk.
10711 while ((wk = LIST_FIRST(wkhd)) != NULL) {
10713 "softdep_setup_blkfree: blkno %jd wk type %d",
10714 blkno, wk->wk_type);
10715 WORKLIST_REMOVE(wk);
10716 if (wk->wk_type != D_JNEWBLK) {
10717 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk);
10720 jnewblk = WK_JNEWBLK(wk);
10721 KASSERT(jnewblk->jn_state & GOINGAWAY,
10722 ("softdep_setup_blkfree: jnewblk not canceled."));
10725 * Assert that this block is free in the bitmap
10726 * before we discard the jnewblk.
10728 cgp = (struct cg *)bp->b_data;
10729 blksfree = cg_blksfree(cgp);
10730 bno = dtogd(fs, jnewblk->jn_blkno);
10731 for (i = jnewblk->jn_oldfrags;
10732 i < jnewblk->jn_frags; i++) {
10733 if (isset(blksfree, bno + i))
10735 panic("softdep_setup_blkfree: not free");
10739 * Even if it's not attached we can free immediately
10740 * as the new bitmap is correct.
10742 wk->wk_state |= COMPLETE | ATTACHED;
10743 free_jnewblk(jnewblk);
10749 * Assert that we are not freeing a block which has an outstanding
10750 * allocation dependency.
10752 fs = VFSTOUFS(mp)->um_fs;
10753 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL);
10754 end = blkno + frags;
10755 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
10757 * Don't match against blocks that will be freed when the
10758 * background write is done.
10760 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) ==
10761 (COMPLETE | DEPCOMPLETE))
10763 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags;
10764 jend = jnewblk->jn_blkno + jnewblk->jn_frags;
10765 if ((blkno >= jstart && blkno < jend) ||
10766 (end > jstart && end <= jend)) {
10767 printf("state 0x%X %jd - %d %d dep %p\n",
10768 jnewblk->jn_state, jnewblk->jn_blkno,
10769 jnewblk->jn_oldfrags, jnewblk->jn_frags,
10771 panic("softdep_setup_blkfree: "
10772 "%jd-%jd(%d) overlaps with %jd-%jd",
10773 blkno, end, frags, jstart, jend);
10781 * Revert a block allocation when the journal record that describes it
10782 * is not yet written.
10785 jnewblk_rollback(jnewblk, fs, cgp, blksfree)
10786 struct jnewblk *jnewblk;
10791 ufs1_daddr_t fragno;
10797 cgbno = dtogd(fs, jnewblk->jn_blkno);
10799 * We have to test which frags need to be rolled back. We may
10800 * be operating on a stale copy when doing background writes.
10802 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++)
10803 if (isclr(blksfree, cgbno + i))
10808 * This is mostly ffs_blkfree() sans some validation and
10809 * superblock updates.
10811 if (frags == fs->fs_frag) {
10812 fragno = fragstoblks(fs, cgbno);
10813 ffs_setblock(fs, blksfree, fragno);
10814 ffs_clusteracct(fs, cgp, fragno, 1);
10815 cgp->cg_cs.cs_nbfree++;
10817 cgbno += jnewblk->jn_oldfrags;
10818 bbase = cgbno - fragnum(fs, cgbno);
10819 /* Decrement the old frags. */
10820 blk = blkmap(fs, blksfree, bbase);
10821 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
10822 /* Deallocate the fragment */
10823 for (i = 0; i < frags; i++)
10824 setbit(blksfree, cgbno + i);
10825 cgp->cg_cs.cs_nffree += frags;
10826 /* Add back in counts associated with the new frags */
10827 blk = blkmap(fs, blksfree, bbase);
10828 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
10829 /* If a complete block has been reassembled, account for it. */
10830 fragno = fragstoblks(fs, bbase);
10831 if (ffs_isblock(fs, blksfree, fragno)) {
10832 cgp->cg_cs.cs_nffree -= fs->fs_frag;
10833 ffs_clusteracct(fs, cgp, fragno, 1);
10834 cgp->cg_cs.cs_nbfree++;
10838 jnewblk->jn_state &= ~ATTACHED;
10839 jnewblk->jn_state |= UNDONE;
10845 initiate_write_bmsafemap(bmsafemap, bp)
10846 struct bmsafemap *bmsafemap;
10847 struct buf *bp; /* The cg block. */
10849 struct jaddref *jaddref;
10850 struct jnewblk *jnewblk;
10857 if (bmsafemap->sm_state & IOSTARTED)
10859 bmsafemap->sm_state |= IOSTARTED;
10861 * Clear any inode allocations which are pending journal writes.
10863 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) {
10864 cgp = (struct cg *)bp->b_data;
10865 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
10866 inosused = cg_inosused(cgp);
10867 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) {
10868 ino = jaddref->ja_ino % fs->fs_ipg;
10869 if (isset(inosused, ino)) {
10870 if ((jaddref->ja_mode & IFMT) == IFDIR)
10871 cgp->cg_cs.cs_ndir--;
10872 cgp->cg_cs.cs_nifree++;
10873 clrbit(inosused, ino);
10874 jaddref->ja_state &= ~ATTACHED;
10875 jaddref->ja_state |= UNDONE;
10878 panic("initiate_write_bmsafemap: inode %ju "
10879 "marked free", (uintmax_t)jaddref->ja_ino);
10883 * Clear any block allocations which are pending journal writes.
10885 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
10886 cgp = (struct cg *)bp->b_data;
10887 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
10888 blksfree = cg_blksfree(cgp);
10889 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) {
10890 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree))
10892 panic("initiate_write_bmsafemap: block %jd "
10893 "marked free", jnewblk->jn_blkno);
10897 * Move allocation lists to the written lists so they can be
10898 * cleared once the block write is complete.
10900 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr,
10901 inodedep, id_deps);
10902 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr,
10904 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist,
10909 * This routine is called during the completion interrupt
10910 * service routine for a disk write (from the procedure called
10911 * by the device driver to inform the filesystem caches of
10912 * a request completion). It should be called early in this
10913 * procedure, before the block is made available to other
10914 * processes or other routines are called.
10918 softdep_disk_write_complete(bp)
10919 struct buf *bp; /* describes the completed disk write */
10921 struct worklist *wk;
10922 struct worklist *owk;
10923 struct ufsmount *ump;
10924 struct workhead reattach;
10925 struct freeblks *freeblks;
10929 * If an error occurred while doing the write, then the data
10930 * has not hit the disk and the dependencies cannot be unrolled.
10932 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0)
10934 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL)
10936 ump = VFSTOUFS(wk->wk_mp);
10937 LIST_INIT(&reattach);
10939 * This lock must not be released anywhere in this code segment.
10944 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
10945 WORKLIST_REMOVE(wk);
10946 atomic_add_long(&dep_write[wk->wk_type], 1);
10948 panic("duplicate worklist: %p\n", wk);
10950 switch (wk->wk_type) {
10953 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
10954 WORKLIST_INSERT(&reattach, wk);
10958 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
10959 WORKLIST_INSERT(&reattach, wk);
10963 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp))
10964 WORKLIST_INSERT(&reattach, wk);
10968 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
10971 case D_ALLOCDIRECT:
10972 wk->wk_state |= COMPLETE;
10973 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL);
10977 wk->wk_state |= COMPLETE;
10978 handle_allocindir_partdone(WK_ALLOCINDIR(wk));
10982 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp))
10983 WORKLIST_INSERT(&reattach, wk);
10987 wk->wk_state |= COMPLETE;
10988 freeblks = WK_FREEBLKS(wk);
10989 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE &&
10990 LIST_EMPTY(&freeblks->fb_jblkdephd))
10991 add_to_worklist(wk, WK_NODELAY);
10995 handle_written_freework(WK_FREEWORK(wk));
10999 free_jsegdep(WK_JSEGDEP(wk));
11003 handle_written_jseg(WK_JSEG(wk), bp);
11007 if (handle_written_sbdep(WK_SBDEP(wk), bp))
11008 WORKLIST_INSERT(&reattach, wk);
11012 free_freedep(WK_FREEDEP(wk));
11016 panic("handle_disk_write_complete: Unknown type %s",
11017 TYPENAME(wk->wk_type));
11022 * Reattach any requests that must be redone.
11024 while ((wk = LIST_FIRST(&reattach)) != NULL) {
11025 WORKLIST_REMOVE(wk);
11026 WORKLIST_INSERT(&bp->b_dep, wk);
11034 * Called from within softdep_disk_write_complete above. Note that
11035 * this routine is always called from interrupt level with further
11036 * splbio interrupts blocked.
11039 handle_allocdirect_partdone(adp, wkhd)
11040 struct allocdirect *adp; /* the completed allocdirect */
11041 struct workhead *wkhd; /* Work to do when inode is writtne. */
11043 struct allocdirectlst *listhead;
11044 struct allocdirect *listadp;
11045 struct inodedep *inodedep;
11048 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11051 * The on-disk inode cannot claim to be any larger than the last
11052 * fragment that has been written. Otherwise, the on-disk inode
11053 * might have fragments that were not the last block in the file
11054 * which would corrupt the filesystem. Thus, we cannot free any
11055 * allocdirects after one whose ad_oldblkno claims a fragment as
11056 * these blocks must be rolled back to zero before writing the inode.
11057 * We check the currently active set of allocdirects in id_inoupdt
11058 * or id_extupdt as appropriate.
11060 inodedep = adp->ad_inodedep;
11061 bsize = inodedep->id_fs->fs_bsize;
11062 if (adp->ad_state & EXTDATA)
11063 listhead = &inodedep->id_extupdt;
11065 listhead = &inodedep->id_inoupdt;
11066 TAILQ_FOREACH(listadp, listhead, ad_next) {
11067 /* found our block */
11068 if (listadp == adp)
11070 /* continue if ad_oldlbn is not a fragment */
11071 if (listadp->ad_oldsize == 0 ||
11072 listadp->ad_oldsize == bsize)
11074 /* hit a fragment */
11078 * If we have reached the end of the current list without
11079 * finding the just finished dependency, then it must be
11080 * on the future dependency list. Future dependencies cannot
11081 * be freed until they are moved to the current list.
11083 if (listadp == NULL) {
11085 if (adp->ad_state & EXTDATA)
11086 listhead = &inodedep->id_newextupdt;
11088 listhead = &inodedep->id_newinoupdt;
11089 TAILQ_FOREACH(listadp, listhead, ad_next)
11090 /* found our block */
11091 if (listadp == adp)
11093 if (listadp == NULL)
11094 panic("handle_allocdirect_partdone: lost dep");
11099 * If we have found the just finished dependency, then queue
11100 * it along with anything that follows it that is complete.
11101 * Since the pointer has not yet been written in the inode
11102 * as the dependency prevents it, place the allocdirect on the
11103 * bufwait list where it will be freed once the pointer is
11107 wkhd = &inodedep->id_bufwait;
11108 for (; adp; adp = listadp) {
11109 listadp = TAILQ_NEXT(adp, ad_next);
11110 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
11112 TAILQ_REMOVE(listhead, adp, ad_next);
11113 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list);
11118 * Called from within softdep_disk_write_complete above. This routine
11119 * completes successfully written allocindirs.
11122 handle_allocindir_partdone(aip)
11123 struct allocindir *aip; /* the completed allocindir */
11125 struct indirdep *indirdep;
11127 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
11129 indirdep = aip->ai_indirdep;
11130 LIST_REMOVE(aip, ai_next);
11132 * Don't set a pointer while the buffer is undergoing IO or while
11133 * we have active truncations.
11135 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) {
11136 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
11139 if (indirdep->ir_state & UFS1FMT)
11140 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11143 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
11146 * Await the pointer write before freeing the allocindir.
11148 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next);
11152 * Release segments held on a jwork list.
11156 struct workhead *wkhd;
11158 struct worklist *wk;
11160 while ((wk = LIST_FIRST(wkhd)) != NULL) {
11161 WORKLIST_REMOVE(wk);
11162 switch (wk->wk_type) {
11164 free_jsegdep(WK_JSEGDEP(wk));
11167 free_freedep(WK_FREEDEP(wk));
11170 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep));
11171 WORKITEM_FREE(wk, D_FREEFRAG);
11174 handle_written_freework(WK_FREEWORK(wk));
11177 panic("handle_jwork: Unknown type %s\n",
11178 TYPENAME(wk->wk_type));
11184 * Handle the bufwait list on an inode when it is safe to release items
11185 * held there. This normally happens after an inode block is written but
11186 * may be delayed and handled later if there are pending journal items that
11187 * are not yet safe to be released.
11189 static struct freefile *
11190 handle_bufwait(inodedep, refhd)
11191 struct inodedep *inodedep;
11192 struct workhead *refhd;
11194 struct jaddref *jaddref;
11195 struct freefile *freefile;
11196 struct worklist *wk;
11199 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
11200 WORKLIST_REMOVE(wk);
11201 switch (wk->wk_type) {
11204 * We defer adding freefile to the worklist
11205 * until all other additions have been made to
11206 * ensure that it will be done after all the
11207 * old blocks have been freed.
11209 if (freefile != NULL)
11210 panic("handle_bufwait: freefile");
11211 freefile = WK_FREEFILE(wk);
11215 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
11219 diradd_inode_written(WK_DIRADD(wk), inodedep);
11223 wk->wk_state |= COMPLETE;
11224 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE)
11225 add_to_worklist(wk, 0);
11229 wk->wk_state |= COMPLETE;
11230 add_to_worklist(wk, 0);
11233 case D_ALLOCDIRECT:
11235 free_newblk(WK_NEWBLK(wk));
11239 wk->wk_state |= COMPLETE;
11240 free_jnewblk(WK_JNEWBLK(wk));
11244 * Save freed journal segments and add references on
11245 * the supplied list which will delay their release
11246 * until the cg bitmap is cleared on disk.
11250 free_jsegdep(WK_JSEGDEP(wk));
11252 WORKLIST_INSERT(refhd, wk);
11256 jaddref = WK_JADDREF(wk);
11257 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref,
11260 * Transfer any jaddrefs to the list to be freed with
11261 * the bitmap if we're handling a removed file.
11263 if (refhd == NULL) {
11264 wk->wk_state |= COMPLETE;
11265 free_jaddref(jaddref);
11267 WORKLIST_INSERT(refhd, wk);
11271 panic("handle_bufwait: Unknown type %p(%s)",
11272 wk, TYPENAME(wk->wk_type));
11279 * Called from within softdep_disk_write_complete above to restore
11280 * in-memory inode block contents to their most up-to-date state. Note
11281 * that this routine is always called from interrupt level with further
11282 * splbio interrupts blocked.
11285 handle_written_inodeblock(inodedep, bp)
11286 struct inodedep *inodedep;
11287 struct buf *bp; /* buffer containing the inode block */
11289 struct freefile *freefile;
11290 struct allocdirect *adp, *nextadp;
11291 struct ufs1_dinode *dp1 = NULL;
11292 struct ufs2_dinode *dp2 = NULL;
11293 struct workhead wkhd;
11294 int hadchanges, fstype;
11300 if ((inodedep->id_state & IOSTARTED) == 0)
11301 panic("handle_written_inodeblock: not started");
11302 inodedep->id_state &= ~IOSTARTED;
11303 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
11305 dp1 = (struct ufs1_dinode *)bp->b_data +
11306 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11307 freelink = dp1->di_freelink;
11310 dp2 = (struct ufs2_dinode *)bp->b_data +
11311 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
11312 freelink = dp2->di_freelink;
11315 * Leave this inodeblock dirty until it's in the list.
11317 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) {
11318 struct inodedep *inon;
11320 inon = TAILQ_NEXT(inodedep, id_unlinked);
11321 if ((inon == NULL && freelink == 0) ||
11322 (inon && inon->id_ino == freelink)) {
11324 inon->id_state |= UNLINKPREV;
11325 inodedep->id_state |= UNLINKNEXT;
11330 * If we had to rollback the inode allocation because of
11331 * bitmaps being incomplete, then simply restore it.
11332 * Keep the block dirty so that it will not be reclaimed until
11333 * all associated dependencies have been cleared and the
11334 * corresponding updates written to disk.
11336 if (inodedep->id_savedino1 != NULL) {
11338 if (fstype == UFS1)
11339 *dp1 = *inodedep->id_savedino1;
11341 *dp2 = *inodedep->id_savedino2;
11342 free(inodedep->id_savedino1, M_SAVEDINO);
11343 inodedep->id_savedino1 = NULL;
11344 if ((bp->b_flags & B_DELWRI) == 0)
11345 stat_inode_bitmap++;
11348 * If the inode is clear here and GOINGAWAY it will never
11349 * be written. Process the bufwait and clear any pending
11350 * work which may include the freefile.
11352 if (inodedep->id_state & GOINGAWAY)
11356 inodedep->id_state |= COMPLETE;
11358 * Roll forward anything that had to be rolled back before
11359 * the inode could be updated.
11361 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
11362 nextadp = TAILQ_NEXT(adp, ad_next);
11363 if (adp->ad_state & ATTACHED)
11364 panic("handle_written_inodeblock: new entry");
11365 if (fstype == UFS1) {
11366 if (adp->ad_offset < NDADDR) {
11367 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11368 panic("%s %s #%jd mismatch %d != %jd",
11369 "handle_written_inodeblock:",
11371 (intmax_t)adp->ad_offset,
11372 dp1->di_db[adp->ad_offset],
11373 (intmax_t)adp->ad_oldblkno);
11374 dp1->di_db[adp->ad_offset] = adp->ad_newblkno;
11376 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0)
11377 panic("%s: %s #%jd allocated as %d",
11378 "handle_written_inodeblock",
11379 "indirect pointer",
11380 (intmax_t)adp->ad_offset - NDADDR,
11381 dp1->di_ib[adp->ad_offset - NDADDR]);
11382 dp1->di_ib[adp->ad_offset - NDADDR] =
11386 if (adp->ad_offset < NDADDR) {
11387 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno)
11388 panic("%s: %s #%jd %s %jd != %jd",
11389 "handle_written_inodeblock",
11391 (intmax_t)adp->ad_offset, "mismatch",
11392 (intmax_t)dp2->di_db[adp->ad_offset],
11393 (intmax_t)adp->ad_oldblkno);
11394 dp2->di_db[adp->ad_offset] = adp->ad_newblkno;
11396 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0)
11397 panic("%s: %s #%jd allocated as %jd",
11398 "handle_written_inodeblock",
11399 "indirect pointer",
11400 (intmax_t)adp->ad_offset - NDADDR,
11402 dp2->di_ib[adp->ad_offset - NDADDR]);
11403 dp2->di_ib[adp->ad_offset - NDADDR] =
11407 adp->ad_state &= ~UNDONE;
11408 adp->ad_state |= ATTACHED;
11411 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
11412 nextadp = TAILQ_NEXT(adp, ad_next);
11413 if (adp->ad_state & ATTACHED)
11414 panic("handle_written_inodeblock: new entry");
11415 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno)
11416 panic("%s: direct pointers #%jd %s %jd != %jd",
11417 "handle_written_inodeblock",
11418 (intmax_t)adp->ad_offset, "mismatch",
11419 (intmax_t)dp2->di_extb[adp->ad_offset],
11420 (intmax_t)adp->ad_oldblkno);
11421 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno;
11422 adp->ad_state &= ~UNDONE;
11423 adp->ad_state |= ATTACHED;
11426 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
11427 stat_direct_blk_ptrs++;
11429 * Reset the file size to its most up-to-date value.
11431 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
11432 panic("handle_written_inodeblock: bad size");
11433 if (inodedep->id_savednlink > LINK_MAX)
11434 panic("handle_written_inodeblock: Invalid link count "
11435 "%d for inodedep %p", inodedep->id_savednlink, inodedep);
11436 if (fstype == UFS1) {
11437 if (dp1->di_nlink != inodedep->id_savednlink) {
11438 dp1->di_nlink = inodedep->id_savednlink;
11441 if (dp1->di_size != inodedep->id_savedsize) {
11442 dp1->di_size = inodedep->id_savedsize;
11446 if (dp2->di_nlink != inodedep->id_savednlink) {
11447 dp2->di_nlink = inodedep->id_savednlink;
11450 if (dp2->di_size != inodedep->id_savedsize) {
11451 dp2->di_size = inodedep->id_savedsize;
11454 if (dp2->di_extsize != inodedep->id_savedextsize) {
11455 dp2->di_extsize = inodedep->id_savedextsize;
11459 inodedep->id_savedsize = -1;
11460 inodedep->id_savedextsize = -1;
11461 inodedep->id_savednlink = -1;
11463 * If there were any rollbacks in the inode block, then it must be
11464 * marked dirty so that its will eventually get written back in
11465 * its correct form.
11471 * Process any allocdirects that completed during the update.
11473 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
11474 handle_allocdirect_partdone(adp, &wkhd);
11475 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
11476 handle_allocdirect_partdone(adp, &wkhd);
11478 * Process deallocations that were held pending until the
11479 * inode had been written to disk. Freeing of the inode
11480 * is delayed until after all blocks have been freed to
11481 * avoid creation of new <vfsid, inum, lbn> triples
11482 * before the old ones have been deleted. Completely
11483 * unlinked inodes are not processed until the unlinked
11484 * inode list is written or the last reference is removed.
11486 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) {
11487 freefile = handle_bufwait(inodedep, NULL);
11488 if (freefile && !LIST_EMPTY(&wkhd)) {
11489 WORKLIST_INSERT(&wkhd, &freefile->fx_list);
11494 * Move rolled forward dependency completions to the bufwait list
11495 * now that those that were already written have been processed.
11497 if (!LIST_EMPTY(&wkhd) && hadchanges == 0)
11498 panic("handle_written_inodeblock: bufwait but no changes");
11499 jwork_move(&inodedep->id_bufwait, &wkhd);
11501 if (freefile != NULL) {
11503 * If the inode is goingaway it was never written. Fake up
11504 * the state here so free_inodedep() can succeed.
11506 if (inodedep->id_state & GOINGAWAY)
11507 inodedep->id_state |= COMPLETE | DEPCOMPLETE;
11508 if (free_inodedep(inodedep) == 0)
11509 panic("handle_written_inodeblock: live inodedep %p",
11511 add_to_worklist(&freefile->fx_list, 0);
11516 * If no outstanding dependencies, free it.
11518 if (free_inodedep(inodedep) ||
11519 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 &&
11520 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
11521 TAILQ_FIRST(&inodedep->id_extupdt) == 0 &&
11522 LIST_FIRST(&inodedep->id_bufwait) == 0))
11524 return (hadchanges);
11528 handle_written_indirdep(indirdep, bp, bpp)
11529 struct indirdep *indirdep;
11533 struct allocindir *aip;
11537 if (indirdep->ir_state & GOINGAWAY)
11538 panic("handle_written_indirdep: indirdep gone");
11539 if ((indirdep->ir_state & IOSTARTED) == 0)
11540 panic("handle_written_indirdep: IO not started");
11543 * If there were rollbacks revert them here.
11545 if (indirdep->ir_saveddata) {
11546 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
11547 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11548 free(indirdep->ir_saveddata, M_INDIRDEP);
11549 indirdep->ir_saveddata = NULL;
11553 indirdep->ir_state &= ~(UNDONE | IOSTARTED);
11554 indirdep->ir_state |= ATTACHED;
11556 * Move allocindirs with written pointers to the completehd if
11557 * the indirdep's pointer is not yet written. Otherwise
11560 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) {
11561 LIST_REMOVE(aip, ai_next);
11562 if ((indirdep->ir_state & DEPCOMPLETE) == 0) {
11563 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip,
11565 newblk_freefrag(&aip->ai_block);
11568 free_newblk(&aip->ai_block);
11571 * Move allocindirs that have finished dependency processing from
11572 * the done list to the write list after updating the pointers.
11574 if (TAILQ_EMPTY(&indirdep->ir_trunc)) {
11575 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
11576 handle_allocindir_partdone(aip);
11577 if (aip == LIST_FIRST(&indirdep->ir_donehd))
11578 panic("disk_write_complete: not gone");
11583 * Preserve the indirdep if there were any changes or if it is not
11584 * yet valid on disk.
11587 stat_indir_blk_ptrs++;
11592 * If there were no changes we can discard the savedbp and detach
11593 * ourselves from the buf. We are only carrying completed pointers
11596 sbp = indirdep->ir_savebp;
11597 sbp->b_flags |= B_INVAL | B_NOCACHE;
11598 indirdep->ir_savebp = NULL;
11599 indirdep->ir_bp = NULL;
11601 panic("handle_written_indirdep: bp already exists.");
11604 * The indirdep may not be freed until its parent points at it.
11606 if (indirdep->ir_state & DEPCOMPLETE)
11607 free_indirdep(indirdep);
11613 * Process a diradd entry after its dependent inode has been written.
11614 * This routine must be called with splbio interrupts blocked.
11617 diradd_inode_written(dap, inodedep)
11618 struct diradd *dap;
11619 struct inodedep *inodedep;
11622 dap->da_state |= COMPLETE;
11623 complete_diradd(dap);
11624 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
11628 * Returns true if the bmsafemap will have rollbacks when written. Must only
11629 * be called with the per-filesystem lock and the buf lock on the cg held.
11632 bmsafemap_backgroundwrite(bmsafemap, bp)
11633 struct bmsafemap *bmsafemap;
11638 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp));
11639 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) |
11640 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd);
11642 * If we're initiating a background write we need to process the
11643 * rollbacks as they exist now, not as they exist when IO starts.
11644 * No other consumers will look at the contents of the shadowed
11645 * buf so this is safe to do here.
11647 if (bp->b_xflags & BX_BKGRDMARKER)
11648 initiate_write_bmsafemap(bmsafemap, bp);
11654 * Re-apply an allocation when a cg write is complete.
11657 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)
11658 struct jnewblk *jnewblk;
11663 ufs1_daddr_t fragno;
11664 ufs2_daddr_t blkno;
11670 cgbno = dtogd(fs, jnewblk->jn_blkno);
11671 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) {
11672 if (isclr(blksfree, cgbno + i))
11673 panic("jnewblk_rollforward: re-allocated fragment");
11676 if (frags == fs->fs_frag) {
11677 blkno = fragstoblks(fs, cgbno);
11678 ffs_clrblock(fs, blksfree, (long)blkno);
11679 ffs_clusteracct(fs, cgp, blkno, -1);
11680 cgp->cg_cs.cs_nbfree--;
11682 bbase = cgbno - fragnum(fs, cgbno);
11683 cgbno += jnewblk->jn_oldfrags;
11684 /* If a complete block had been reassembled, account for it. */
11685 fragno = fragstoblks(fs, bbase);
11686 if (ffs_isblock(fs, blksfree, fragno)) {
11687 cgp->cg_cs.cs_nffree += fs->fs_frag;
11688 ffs_clusteracct(fs, cgp, fragno, -1);
11689 cgp->cg_cs.cs_nbfree--;
11691 /* Decrement the old frags. */
11692 blk = blkmap(fs, blksfree, bbase);
11693 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11694 /* Allocate the fragment */
11695 for (i = 0; i < frags; i++)
11696 clrbit(blksfree, cgbno + i);
11697 cgp->cg_cs.cs_nffree -= frags;
11698 /* Add back in counts associated with the new frags */
11699 blk = blkmap(fs, blksfree, bbase);
11700 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11706 * Complete a write to a bmsafemap structure. Roll forward any bitmap
11707 * changes if it's not a background write. Set all written dependencies
11708 * to DEPCOMPLETE and free the structure if possible.
11711 handle_written_bmsafemap(bmsafemap, bp)
11712 struct bmsafemap *bmsafemap;
11715 struct newblk *newblk;
11716 struct inodedep *inodedep;
11717 struct jaddref *jaddref, *jatmp;
11718 struct jnewblk *jnewblk, *jntmp;
11719 struct ufsmount *ump;
11728 if ((bmsafemap->sm_state & IOSTARTED) == 0)
11729 panic("initiate_write_bmsafemap: Not started\n");
11730 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp);
11732 bmsafemap->sm_state &= ~IOSTARTED;
11733 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0;
11735 * Release journal work that was waiting on the write.
11737 handle_jwork(&bmsafemap->sm_freewr);
11740 * Restore unwritten inode allocation pending jaddref writes.
11742 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) {
11743 cgp = (struct cg *)bp->b_data;
11744 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11745 inosused = cg_inosused(cgp);
11746 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd,
11747 ja_bmdeps, jatmp) {
11748 if ((jaddref->ja_state & UNDONE) == 0)
11750 ino = jaddref->ja_ino % fs->fs_ipg;
11751 if (isset(inosused, ino))
11752 panic("handle_written_bmsafemap: "
11753 "re-allocated inode");
11754 /* Do the roll-forward only if it's a real copy. */
11756 if ((jaddref->ja_mode & IFMT) == IFDIR)
11757 cgp->cg_cs.cs_ndir++;
11758 cgp->cg_cs.cs_nifree--;
11759 setbit(inosused, ino);
11762 jaddref->ja_state &= ~UNDONE;
11763 jaddref->ja_state |= ATTACHED;
11764 free_jaddref(jaddref);
11768 * Restore any block allocations which are pending journal writes.
11770 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) {
11771 cgp = (struct cg *)bp->b_data;
11772 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs;
11773 blksfree = cg_blksfree(cgp);
11774 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps,
11776 if ((jnewblk->jn_state & UNDONE) == 0)
11778 /* Do the roll-forward only if it's a real copy. */
11780 jnewblk_rollforward(jnewblk, fs, cgp, blksfree))
11782 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK);
11783 jnewblk->jn_state |= ATTACHED;
11784 free_jnewblk(jnewblk);
11787 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) {
11788 newblk->nb_state |= DEPCOMPLETE;
11789 newblk->nb_state &= ~ONDEPLIST;
11790 newblk->nb_bmsafemap = NULL;
11791 LIST_REMOVE(newblk, nb_deps);
11792 if (newblk->nb_list.wk_type == D_ALLOCDIRECT)
11793 handle_allocdirect_partdone(
11794 WK_ALLOCDIRECT(&newblk->nb_list), NULL);
11795 else if (newblk->nb_list.wk_type == D_ALLOCINDIR)
11796 handle_allocindir_partdone(
11797 WK_ALLOCINDIR(&newblk->nb_list));
11798 else if (newblk->nb_list.wk_type != D_NEWBLK)
11799 panic("handle_written_bmsafemap: Unexpected type: %s",
11800 TYPENAME(newblk->nb_list.wk_type));
11802 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) {
11803 inodedep->id_state |= DEPCOMPLETE;
11804 inodedep->id_state &= ~ONDEPLIST;
11805 LIST_REMOVE(inodedep, id_deps);
11806 inodedep->id_bmsafemap = NULL;
11808 LIST_REMOVE(bmsafemap, sm_next);
11809 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) &&
11810 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) &&
11811 LIST_EMPTY(&bmsafemap->sm_newblkhd) &&
11812 LIST_EMPTY(&bmsafemap->sm_inodedephd) &&
11813 LIST_EMPTY(&bmsafemap->sm_freehd)) {
11814 LIST_REMOVE(bmsafemap, sm_hash);
11815 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
11818 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next);
11825 * Try to free a mkdir dependency.
11828 complete_mkdir(mkdir)
11829 struct mkdir *mkdir;
11831 struct diradd *dap;
11833 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE)
11835 LIST_REMOVE(mkdir, md_mkdirs);
11836 dap = mkdir->md_diradd;
11837 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY));
11838 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) {
11839 dap->da_state |= DEPCOMPLETE;
11840 complete_diradd(dap);
11842 WORKITEM_FREE(mkdir, D_MKDIR);
11846 * Handle the completion of a mkdir dependency.
11849 handle_written_mkdir(mkdir, type)
11850 struct mkdir *mkdir;
11854 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type)
11855 panic("handle_written_mkdir: bad type");
11856 mkdir->md_state |= COMPLETE;
11857 complete_mkdir(mkdir);
11861 free_pagedep(pagedep)
11862 struct pagedep *pagedep;
11866 if (pagedep->pd_state & NEWBLOCK)
11868 if (!LIST_EMPTY(&pagedep->pd_dirremhd))
11870 for (i = 0; i < DAHASHSZ; i++)
11871 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
11873 if (!LIST_EMPTY(&pagedep->pd_pendinghd))
11875 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd))
11877 if (pagedep->pd_state & ONWORKLIST)
11878 WORKLIST_REMOVE(&pagedep->pd_list);
11879 LIST_REMOVE(pagedep, pd_hash);
11880 WORKITEM_FREE(pagedep, D_PAGEDEP);
11886 * Called from within softdep_disk_write_complete above.
11887 * A write operation was just completed. Removed inodes can
11888 * now be freed and associated block pointers may be committed.
11889 * Note that this routine is always called from interrupt level
11890 * with further splbio interrupts blocked.
11893 handle_written_filepage(pagedep, bp)
11894 struct pagedep *pagedep;
11895 struct buf *bp; /* buffer containing the written page */
11897 struct dirrem *dirrem;
11898 struct diradd *dap, *nextdap;
11902 if ((pagedep->pd_state & IOSTARTED) == 0)
11903 panic("handle_written_filepage: not started");
11904 pagedep->pd_state &= ~IOSTARTED;
11906 * Process any directory removals that have been committed.
11908 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
11909 LIST_REMOVE(dirrem, dm_next);
11910 dirrem->dm_state |= COMPLETE;
11911 dirrem->dm_dirinum = pagedep->pd_ino;
11912 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd),
11913 ("handle_written_filepage: Journal entries not written."));
11914 add_to_worklist(&dirrem->dm_list, 0);
11917 * Free any directory additions that have been committed.
11918 * If it is a newly allocated block, we have to wait until
11919 * the on-disk directory inode claims the new block.
11921 if ((pagedep->pd_state & NEWBLOCK) == 0)
11922 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
11923 free_diradd(dap, NULL);
11925 * Uncommitted directory entries must be restored.
11927 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
11928 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
11930 nextdap = LIST_NEXT(dap, da_pdlist);
11931 if (dap->da_state & ATTACHED)
11932 panic("handle_written_filepage: attached");
11933 ep = (struct direct *)
11934 ((char *)bp->b_data + dap->da_offset);
11935 ep->d_ino = dap->da_newinum;
11936 dap->da_state &= ~UNDONE;
11937 dap->da_state |= ATTACHED;
11940 * If the inode referenced by the directory has
11941 * been written out, then the dependency can be
11942 * moved to the pending list.
11944 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
11945 LIST_REMOVE(dap, da_pdlist);
11946 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
11952 * If there were any rollbacks in the directory, then it must be
11953 * marked dirty so that its will eventually get written back in
11954 * its correct form.
11957 if ((bp->b_flags & B_DELWRI) == 0)
11963 * If we are not waiting for a new directory block to be
11964 * claimed by its inode, then the pagedep will be freed.
11965 * Otherwise it will remain to track any new entries on
11966 * the page in case they are fsync'ed.
11968 free_pagedep(pagedep);
11973 * Writing back in-core inode structures.
11975 * The filesystem only accesses an inode's contents when it occupies an
11976 * "in-core" inode structure. These "in-core" structures are separate from
11977 * the page frames used to cache inode blocks. Only the latter are
11978 * transferred to/from the disk. So, when the updated contents of the
11979 * "in-core" inode structure are copied to the corresponding in-memory inode
11980 * block, the dependencies are also transferred. The following procedure is
11981 * called when copying a dirty "in-core" inode to a cached inode block.
11985 * Called when an inode is loaded from disk. If the effective link count
11986 * differed from the actual link count when it was last flushed, then we
11987 * need to ensure that the correct effective link count is put back.
11990 softdep_load_inodeblock(ip)
11991 struct inode *ip; /* the "in_core" copy of the inode */
11993 struct inodedep *inodedep;
11995 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
11996 ("softdep_load_inodeblock called on non-softdep filesystem"));
11998 * Check for alternate nlink count.
12000 ip->i_effnlink = ip->i_nlink;
12001 ACQUIRE_LOCK(ip->i_ump);
12002 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0,
12004 FREE_LOCK(ip->i_ump);
12007 ip->i_effnlink -= inodedep->id_nlinkdelta;
12008 FREE_LOCK(ip->i_ump);
12012 * This routine is called just before the "in-core" inode
12013 * information is to be copied to the in-memory inode block.
12014 * Recall that an inode block contains several inodes. If
12015 * the force flag is set, then the dependencies will be
12016 * cleared so that the update can always be made. Note that
12017 * the buffer is locked when this routine is called, so we
12018 * will never be in the middle of writing the inode block
12022 softdep_update_inodeblock(ip, bp, waitfor)
12023 struct inode *ip; /* the "in_core" copy of the inode */
12024 struct buf *bp; /* the buffer containing the inode block */
12025 int waitfor; /* nonzero => update must be allowed */
12027 struct inodedep *inodedep;
12028 struct inoref *inoref;
12029 struct ufsmount *ump;
12030 struct worklist *wk;
12037 mp = UFSTOVFS(ump);
12038 KASSERT(MOUNTEDSOFTDEP(mp) != 0,
12039 ("softdep_update_inodeblock called on non-softdep filesystem"));
12042 * Preserve the freelink that is on disk. clear_unlinked_inodedep()
12043 * does not have access to the in-core ip so must write directly into
12044 * the inode block buffer when setting freelink.
12046 if (fs->fs_magic == FS_UFS1_MAGIC)
12047 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data +
12048 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12050 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data +
12051 ino_to_fsbo(fs, ip->i_number))->di_freelink);
12053 * If the effective link count is not equal to the actual link
12054 * count, then we must track the difference in an inodedep while
12055 * the inode is (potentially) tossed out of the cache. Otherwise,
12056 * if there is no existing inodedep, then there are no dependencies
12061 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12063 if (ip->i_effnlink != ip->i_nlink)
12064 panic("softdep_update_inodeblock: bad link count");
12067 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
12068 panic("softdep_update_inodeblock: bad delta");
12070 * If we're flushing all dependencies we must also move any waiting
12071 * for journal writes onto the bufwait list prior to I/O.
12074 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12075 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12077 jwait(&inoref->if_list, MNT_WAIT);
12083 * Changes have been initiated. Anything depending on these
12084 * changes cannot occur until this inode has been written.
12086 inodedep->id_state &= ~COMPLETE;
12087 if ((inodedep->id_state & ONWORKLIST) == 0)
12088 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
12090 * Any new dependencies associated with the incore inode must
12091 * now be moved to the list associated with the buffer holding
12092 * the in-memory copy of the inode. Once merged process any
12093 * allocdirects that are completed by the merger.
12095 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
12096 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
12097 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt),
12099 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
12100 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
12101 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt),
12104 * Now that the inode has been pushed into the buffer, the
12105 * operations dependent on the inode being written to disk
12106 * can be moved to the id_bufwait so that they will be
12107 * processed when the buffer I/O completes.
12109 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
12110 WORKLIST_REMOVE(wk);
12111 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
12114 * Newly allocated inodes cannot be written until the bitmap
12115 * that allocates them have been written (indicated by
12116 * DEPCOMPLETE being set in id_state). If we are doing a
12117 * forced sync (e.g., an fsync on a file), we force the bitmap
12118 * to be written so that the update can be done.
12120 if (waitfor == 0) {
12125 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) {
12129 ibp = inodedep->id_bmsafemap->sm_buf;
12130 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT);
12133 * If ibp came back as NULL, the dependency could have been
12134 * freed while we slept. Look it up again, and check to see
12135 * that it has completed.
12137 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
12143 if ((error = bwrite(ibp)) != 0)
12144 softdep_error("softdep_update_inodeblock: bwrite", error);
12148 * Merge the a new inode dependency list (such as id_newinoupdt) into an
12149 * old inode dependency list (such as id_inoupdt). This routine must be
12150 * called with splbio interrupts blocked.
12153 merge_inode_lists(newlisthead, oldlisthead)
12154 struct allocdirectlst *newlisthead;
12155 struct allocdirectlst *oldlisthead;
12157 struct allocdirect *listadp, *newadp;
12159 newadp = TAILQ_FIRST(newlisthead);
12160 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
12161 if (listadp->ad_offset < newadp->ad_offset) {
12162 listadp = TAILQ_NEXT(listadp, ad_next);
12165 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12166 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
12167 if (listadp->ad_offset == newadp->ad_offset) {
12168 allocdirect_merge(oldlisthead, newadp,
12172 newadp = TAILQ_FIRST(newlisthead);
12174 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
12175 TAILQ_REMOVE(newlisthead, newadp, ad_next);
12176 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
12181 * If we are doing an fsync, then we must ensure that any directory
12182 * entries for the inode have been written after the inode gets to disk.
12186 struct vnode *vp; /* the "in_core" copy of the inode */
12188 struct inodedep *inodedep;
12189 struct pagedep *pagedep;
12190 struct inoref *inoref;
12191 struct ufsmount *ump;
12192 struct worklist *wk;
12193 struct diradd *dap;
12199 struct thread *td = curthread;
12200 int error, flushparent, pagedep_new_block;
12208 if (MOUNTEDSOFTDEP(mp) == 0)
12212 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
12216 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12217 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12219 jwait(&inoref->if_list, MNT_WAIT);
12223 if (!LIST_EMPTY(&inodedep->id_inowait) ||
12224 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
12225 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
12226 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
12227 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
12228 panic("softdep_fsync: pending ops %p", inodedep);
12229 for (error = 0, flushparent = 0; ; ) {
12230 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
12232 if (wk->wk_type != D_DIRADD)
12233 panic("softdep_fsync: Unexpected type %s",
12234 TYPENAME(wk->wk_type));
12235 dap = WK_DIRADD(wk);
12237 * Flush our parent if this directory entry has a MKDIR_PARENT
12238 * dependency or is contained in a newly allocated block.
12240 if (dap->da_state & DIRCHG)
12241 pagedep = dap->da_previous->dm_pagedep;
12243 pagedep = dap->da_pagedep;
12244 parentino = pagedep->pd_ino;
12245 lbn = pagedep->pd_lbn;
12246 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
12247 panic("softdep_fsync: dirty");
12248 if ((dap->da_state & MKDIR_PARENT) ||
12249 (pagedep->pd_state & NEWBLOCK))
12254 * If we are being fsync'ed as part of vgone'ing this vnode,
12255 * then we will not be able to release and recover the
12256 * vnode below, so we just have to give up on writing its
12257 * directory entry out. It will eventually be written, just
12258 * not now, but then the user was not asking to have it
12259 * written, so we are not breaking any promises.
12261 if (vp->v_iflag & VI_DOOMED)
12264 * We prevent deadlock by always fetching inodes from the
12265 * root, moving down the directory tree. Thus, when fetching
12266 * our parent directory, we first try to get the lock. If
12267 * that fails, we must unlock ourselves before requesting
12268 * the lock on our parent. See the comment in ufs_lookup
12269 * for details on possible races.
12272 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
12273 FFSV_FORCEINSMQ)) {
12274 error = vfs_busy(mp, MBF_NOWAIT);
12278 error = vfs_busy(mp, 0);
12279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12283 if (vp->v_iflag & VI_DOOMED) {
12289 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
12290 &pvp, FFSV_FORCEINSMQ);
12292 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
12293 if (vp->v_iflag & VI_DOOMED) {
12302 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
12303 * that are contained in direct blocks will be resolved by
12304 * doing a ffs_update. Pagedeps contained in indirect blocks
12305 * may require a complete sync'ing of the directory. So, we
12306 * try the cheap and fast ffs_update first, and if that fails,
12307 * then we do the slower ffs_syncvnode of the directory.
12312 if ((error = ffs_update(pvp, 1)) != 0) {
12318 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
12319 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
12320 if (wk->wk_type != D_DIRADD)
12321 panic("softdep_fsync: Unexpected type %s",
12322 TYPENAME(wk->wk_type));
12323 dap = WK_DIRADD(wk);
12324 if (dap->da_state & DIRCHG)
12325 pagedep = dap->da_previous->dm_pagedep;
12327 pagedep = dap->da_pagedep;
12328 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
12331 if (pagedep_new_block && (error =
12332 ffs_syncvnode(pvp, MNT_WAIT, 0))) {
12342 * Flush directory page containing the inode's name.
12344 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
12347 error = bwrite(bp);
12354 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
12362 * Flush all the dirty bitmaps associated with the block device
12363 * before flushing the rest of the dirty blocks so as to reduce
12364 * the number of dependencies that will have to be rolled back.
12369 softdep_fsync_mountdev(vp)
12372 struct buf *bp, *nbp;
12373 struct worklist *wk;
12376 if (!vn_isdisk(vp, NULL))
12377 panic("softdep_fsync_mountdev: vnode not a disk");
12378 bo = &vp->v_bufobj;
12381 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
12383 * If it is already scheduled, skip to the next buffer.
12385 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
12388 if ((bp->b_flags & B_DELWRI) == 0)
12389 panic("softdep_fsync_mountdev: not dirty");
12391 * We are only interested in bitmaps with outstanding
12394 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
12395 wk->wk_type != D_BMSAFEMAP ||
12396 (bp->b_vflags & BV_BKGRDINPROG)) {
12402 (void) bawrite(bp);
12410 * Sync all cylinder groups that were dirty at the time this function is
12411 * called. Newly dirtied cgs will be inserted before the sentinel. This
12412 * is used to flush freedep activity that may be holding up writes to a
12416 sync_cgs(mp, waitfor)
12420 struct bmsafemap *bmsafemap;
12421 struct bmsafemap *sentinel;
12422 struct ufsmount *ump;
12426 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK);
12427 sentinel->sm_cg = -1;
12428 ump = VFSTOUFS(mp);
12431 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next);
12432 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL;
12433 bmsafemap = LIST_NEXT(sentinel, sm_next)) {
12434 /* Skip sentinels and cgs with no work to release. */
12435 if (bmsafemap->sm_cg == -1 ||
12436 (LIST_EMPTY(&bmsafemap->sm_freehd) &&
12437 LIST_EMPTY(&bmsafemap->sm_freewr))) {
12438 LIST_REMOVE(sentinel, sm_next);
12439 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12443 * If we don't get the lock and we're waiting try again, if
12444 * not move on to the next buf and try to sync it.
12446 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor);
12447 if (bp == NULL && waitfor == MNT_WAIT)
12449 LIST_REMOVE(sentinel, sm_next);
12450 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next);
12454 if (waitfor == MNT_NOWAIT)
12457 error = bwrite(bp);
12462 LIST_REMOVE(sentinel, sm_next);
12464 free(sentinel, M_BMSAFEMAP);
12469 * This routine is called when we are trying to synchronously flush a
12470 * file. This routine must eliminate any filesystem metadata dependencies
12471 * so that the syncing routine can succeed.
12474 softdep_sync_metadata(struct vnode *vp)
12480 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
12481 ("softdep_sync_metadata called on non-softdep filesystem"));
12483 * Ensure that any direct block dependencies have been cleared,
12484 * truncations are started, and inode references are journaled.
12486 ACQUIRE_LOCK(ip->i_ump);
12488 * Write all journal records to prevent rollbacks on devvp.
12490 if (vp->v_type == VCHR)
12491 softdep_flushjournal(vp->v_mount);
12492 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number);
12494 * Ensure that all truncates are written so we won't find deps on
12497 process_truncates(vp);
12498 FREE_LOCK(ip->i_ump);
12504 * This routine is called when we are attempting to sync a buf with
12505 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any
12506 * other IO it can but returns EBUSY if the buffer is not yet able to
12507 * be written. Dependencies which will not cause rollbacks will always
12511 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor)
12513 struct indirdep *indirdep;
12514 struct pagedep *pagedep;
12515 struct allocindir *aip;
12516 struct newblk *newblk;
12517 struct ufsmount *ump;
12519 struct worklist *wk;
12522 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
12523 ("softdep_sync_buf called on non-softdep filesystem"));
12525 * For VCHR we just don't want to force flush any dependencies that
12526 * will cause rollbacks.
12528 if (vp->v_type == VCHR) {
12529 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0))
12533 ump = VTOI(vp)->i_ump;
12536 * As we hold the buffer locked, none of its dependencies
12541 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
12542 switch (wk->wk_type) {
12544 case D_ALLOCDIRECT:
12546 newblk = WK_NEWBLK(wk);
12547 if (newblk->nb_jnewblk != NULL) {
12548 if (waitfor == MNT_NOWAIT) {
12552 jwait(&newblk->nb_jnewblk->jn_list, waitfor);
12555 if (newblk->nb_state & DEPCOMPLETE ||
12556 waitfor == MNT_NOWAIT)
12558 nbp = newblk->nb_bmsafemap->sm_buf;
12559 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12563 if ((error = bwrite(nbp)) != 0)
12569 indirdep = WK_INDIRDEP(wk);
12570 if (waitfor == MNT_NOWAIT) {
12571 if (!TAILQ_EMPTY(&indirdep->ir_trunc) ||
12572 !LIST_EMPTY(&indirdep->ir_deplisthd)) {
12577 if (!TAILQ_EMPTY(&indirdep->ir_trunc))
12578 panic("softdep_sync_buf: truncation pending.");
12580 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
12581 newblk = (struct newblk *)aip;
12582 if (newblk->nb_jnewblk != NULL) {
12583 jwait(&newblk->nb_jnewblk->jn_list,
12587 if (newblk->nb_state & DEPCOMPLETE)
12589 nbp = newblk->nb_bmsafemap->sm_buf;
12590 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor);
12594 if ((error = bwrite(nbp)) != 0)
12603 * Only flush directory entries in synchronous passes.
12605 if (waitfor != MNT_WAIT) {
12610 * While syncing snapshots, we must allow recursive
12615 * We are trying to sync a directory that may
12616 * have dependencies on both its own metadata
12617 * and/or dependencies on the inodes of any
12618 * recently allocated files. We walk its diradd
12619 * lists pushing out the associated inode.
12621 pagedep = WK_PAGEDEP(wk);
12622 for (i = 0; i < DAHASHSZ; i++) {
12623 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
12625 if ((error = flush_pagedep_deps(vp, wk->wk_mp,
12626 &pagedep->pd_diraddhd[i]))) {
12641 panic("softdep_sync_buf: Unknown type %s",
12642 TYPENAME(wk->wk_type));
12653 * Flush the dependencies associated with an inodedep.
12654 * Called with splbio blocked.
12657 flush_inodedep_deps(vp, mp, ino)
12662 struct inodedep *inodedep;
12663 struct inoref *inoref;
12664 struct ufsmount *ump;
12665 int error, waitfor;
12668 * This work is done in two passes. The first pass grabs most
12669 * of the buffers and begins asynchronously writing them. The
12670 * only way to wait for these asynchronous writes is to sleep
12671 * on the filesystem vnode which may stay busy for a long time
12672 * if the filesystem is active. So, instead, we make a second
12673 * pass over the dependencies blocking on each write. In the
12674 * usual case we will be blocking against a write that we
12675 * initiated, so when it is done the dependency will have been
12676 * resolved. Thus the second pass is expected to end quickly.
12677 * We give a brief window at the top of the loop to allow
12678 * any pending I/O to complete.
12680 ump = VFSTOUFS(mp);
12682 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
12688 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
12690 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12691 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12693 jwait(&inoref->if_list, MNT_WAIT);
12697 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
12698 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
12699 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
12700 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
12703 * If pass2, we are done, otherwise do pass 2.
12705 if (waitfor == MNT_WAIT)
12707 waitfor = MNT_WAIT;
12710 * Try freeing inodedep in case all dependencies have been removed.
12712 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
12713 (void) free_inodedep(inodedep);
12718 * Flush an inode dependency list.
12719 * Called with splbio blocked.
12722 flush_deplist(listhead, waitfor, errorp)
12723 struct allocdirectlst *listhead;
12727 struct allocdirect *adp;
12728 struct newblk *newblk;
12729 struct ufsmount *ump;
12732 if ((adp = TAILQ_FIRST(listhead)) == NULL)
12734 ump = VFSTOUFS(adp->ad_list.wk_mp);
12736 TAILQ_FOREACH(adp, listhead, ad_next) {
12737 newblk = (struct newblk *)adp;
12738 if (newblk->nb_jnewblk != NULL) {
12739 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
12742 if (newblk->nb_state & DEPCOMPLETE)
12744 bp = newblk->nb_bmsafemap->sm_buf;
12745 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor);
12747 if (waitfor == MNT_NOWAIT)
12752 if (waitfor == MNT_NOWAIT)
12755 *errorp = bwrite(bp);
12763 * Flush dependencies associated with an allocdirect block.
12766 flush_newblk_dep(vp, mp, lbn)
12771 struct newblk *newblk;
12772 struct ufsmount *ump;
12776 ufs2_daddr_t blkno;
12780 bo = &vp->v_bufobj;
12782 blkno = DIP(ip, i_db[lbn]);
12784 panic("flush_newblk_dep: Missing block");
12785 ump = VFSTOUFS(mp);
12788 * Loop until all dependencies related to this block are satisfied.
12789 * We must be careful to restart after each sleep in case a write
12790 * completes some part of this process for us.
12793 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) {
12797 if (newblk->nb_list.wk_type != D_ALLOCDIRECT)
12798 panic("flush_newblk_deps: Bad newblk %p", newblk);
12800 * Flush the journal.
12802 if (newblk->nb_jnewblk != NULL) {
12803 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT);
12807 * Write the bitmap dependency.
12809 if ((newblk->nb_state & DEPCOMPLETE) == 0) {
12810 bp = newblk->nb_bmsafemap->sm_buf;
12811 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
12815 error = bwrite(bp);
12822 * Write the buffer.
12826 bp = gbincore(bo, lbn);
12828 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
12829 LK_INTERLOCK, BO_LOCKPTR(bo));
12830 if (error == ENOLCK) {
12832 continue; /* Slept, retry */
12835 break; /* Failed */
12836 if (bp->b_flags & B_DELWRI) {
12838 error = bwrite(bp);
12846 * We have to wait for the direct pointers to
12847 * point at the newdirblk before the dependency
12850 error = ffs_update(vp, 1);
12859 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
12860 * Called with splbio blocked.
12863 flush_pagedep_deps(pvp, mp, diraddhdp)
12866 struct diraddhd *diraddhdp;
12868 struct inodedep *inodedep;
12869 struct inoref *inoref;
12870 struct ufsmount *ump;
12871 struct diradd *dap;
12876 struct diraddhd unfinished;
12878 LIST_INIT(&unfinished);
12879 ump = VFSTOUFS(mp);
12882 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
12884 * Flush ourselves if this directory entry
12885 * has a MKDIR_PARENT dependency.
12887 if (dap->da_state & MKDIR_PARENT) {
12889 if ((error = ffs_update(pvp, 1)) != 0)
12893 * If that cleared dependencies, go on to next.
12895 if (dap != LIST_FIRST(diraddhdp))
12898 * All MKDIR_PARENT dependencies and all the
12899 * NEWBLOCK pagedeps that are contained in direct
12900 * blocks were resolved by doing above ffs_update.
12901 * Pagedeps contained in indirect blocks may
12902 * require a complete sync'ing of the directory.
12903 * We are in the midst of doing a complete sync,
12904 * so if they are not resolved in this pass we
12905 * defer them for now as they will be sync'ed by
12906 * our caller shortly.
12908 LIST_REMOVE(dap, da_pdlist);
12909 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist);
12913 * A newly allocated directory must have its "." and
12914 * ".." entries written out before its name can be
12915 * committed in its parent.
12917 inum = dap->da_newinum;
12918 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
12919 panic("flush_pagedep_deps: lost inode1");
12921 * Wait for any pending journal adds to complete so we don't
12922 * cause rollbacks while syncing.
12924 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) {
12925 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY))
12927 jwait(&inoref->if_list, MNT_WAIT);
12931 if (dap->da_state & MKDIR_BODY) {
12933 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
12936 error = flush_newblk_dep(vp, mp, 0);
12938 * If we still have the dependency we might need to
12939 * update the vnode to sync the new link count to
12942 if (error == 0 && dap == LIST_FIRST(diraddhdp))
12943 error = ffs_update(vp, 1);
12949 * If that cleared dependencies, go on to next.
12951 if (dap != LIST_FIRST(diraddhdp))
12953 if (dap->da_state & MKDIR_BODY) {
12954 inodedep_lookup(UFSTOVFS(ump), inum, 0,
12956 panic("flush_pagedep_deps: MKDIR_BODY "
12957 "inodedep %p dap %p vp %p",
12958 inodedep, dap, vp);
12962 * Flush the inode on which the directory entry depends.
12963 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
12964 * the only remaining dependency is that the updated inode
12965 * count must get pushed to disk. The inode has already
12966 * been pushed into its inode buffer (via VOP_UPDATE) at
12967 * the time of the reference count change. So we need only
12968 * locate that buffer, ensure that there will be no rollback
12969 * caused by a bitmap dependency, then write the inode buffer.
12972 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
12973 panic("flush_pagedep_deps: lost inode");
12975 * If the inode still has bitmap dependencies,
12976 * push them to disk.
12978 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) {
12979 bp = inodedep->id_bmsafemap->sm_buf;
12980 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT);
12984 if ((error = bwrite(bp)) != 0)
12987 if (dap != LIST_FIRST(diraddhdp))
12991 * If the inode is still sitting in a buffer waiting
12992 * to be written or waiting for the link count to be
12993 * adjusted update it here to flush it to disk.
12995 if (dap == LIST_FIRST(diraddhdp)) {
12997 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
13000 error = ffs_update(vp, 1);
13007 * If we have failed to get rid of all the dependencies
13008 * then something is seriously wrong.
13010 if (dap == LIST_FIRST(diraddhdp)) {
13011 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep);
13012 panic("flush_pagedep_deps: failed to flush "
13013 "inodedep %p ino %ju dap %p",
13014 inodedep, (uintmax_t)inum, dap);
13019 while ((dap = LIST_FIRST(&unfinished)) != NULL) {
13020 LIST_REMOVE(dap, da_pdlist);
13021 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist);
13027 * A large burst of file addition or deletion activity can drive the
13028 * memory load excessively high. First attempt to slow things down
13029 * using the techniques below. If that fails, this routine requests
13030 * the offending operations to fall back to running synchronously
13031 * until the memory load returns to a reasonable level.
13034 softdep_slowdown(vp)
13037 struct ufsmount *ump;
13039 int max_softdeps_hard;
13041 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0,
13042 ("softdep_slowdown called on non-softdep filesystem"));
13043 ump = VFSTOUFS(vp->v_mount);
13047 * Check for journal space if needed.
13049 if (DOINGSUJ(vp)) {
13050 if (journal_space(ump, 0) == 0)
13054 * If the system is under its limits and our filesystem is
13055 * not responsible for more than our share of the usage and
13056 * we are not low on journal space, then no need to slow down.
13058 max_softdeps_hard = max_softdeps * 11 / 10;
13059 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 &&
13060 dep_current[D_INODEDEP] < max_softdeps_hard &&
13061 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 &&
13062 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 &&
13063 ump->softdep_curdeps[D_DIRREM] <
13064 (max_softdeps_hard / 2) / stat_flush_threads &&
13065 ump->softdep_curdeps[D_INODEDEP] <
13066 max_softdeps_hard / stat_flush_threads &&
13067 ump->softdep_curdeps[D_INDIRDEP] <
13068 (max_softdeps_hard / 1000) / stat_flush_threads &&
13069 ump->softdep_curdeps[D_FREEBLKS] <
13070 max_softdeps_hard / stat_flush_threads) {
13075 * If the journal is low or our filesystem is over its limit
13076 * then speedup the cleanup.
13078 if (ump->softdep_curdeps[D_INDIRDEP] <
13079 (max_softdeps_hard / 1000) / stat_flush_threads || jlow)
13080 softdep_speedup(ump);
13081 stat_sync_limit_hit += 1;
13084 * We only slow down the rate at which new dependencies are
13085 * generated if we are not using journaling. With journaling,
13086 * the cleanup should always be sufficient to keep things
13095 * Called by the allocation routines when they are about to fail
13096 * in the hope that we can free up the requested resource (inodes
13099 * First check to see if the work list has anything on it. If it has,
13100 * clean up entries until we successfully free the requested resource.
13101 * Because this process holds inodes locked, we cannot handle any remove
13102 * requests that might block on a locked inode as that could lead to
13103 * deadlock. If the worklist yields none of the requested resource,
13104 * start syncing out vnodes to free up the needed space.
13107 softdep_request_cleanup(fs, vp, cred, resource)
13110 struct ucred *cred;
13113 struct ufsmount *ump;
13115 struct vnode *lvp, *mvp;
13117 ufs2_daddr_t needed;
13121 * If we are being called because of a process doing a
13122 * copy-on-write, then it is not safe to process any
13123 * worklist items as we will recurse into the copyonwrite
13124 * routine. This will result in an incoherent snapshot.
13125 * If the vnode that we hold is a snapshot, we must avoid
13126 * handling other resources that could cause deadlock.
13128 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp)))
13131 if (resource == FLUSH_BLOCKS_WAIT)
13132 stat_cleanup_blkrequests += 1;
13134 stat_cleanup_inorequests += 1;
13137 ump = VFSTOUFS(mp);
13138 mtx_assert(UFS_MTX(ump), MA_OWNED);
13140 error = ffs_update(vp, 1);
13141 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) {
13146 * If we are in need of resources, start by cleaning up
13147 * any block removals associated with our inode.
13150 process_removes(vp);
13151 process_truncates(vp);
13154 * Now clean up at least as many resources as we will need.
13156 * When requested to clean up inodes, the number that are needed
13157 * is set by the number of simultaneous writers (mnt_writeopcount)
13158 * plus a bit of slop (2) in case some more writers show up while
13161 * When requested to free up space, the amount of space that
13162 * we need is enough blocks to allocate a full-sized segment
13163 * (fs_contigsumsize). The number of such segments that will
13164 * be needed is set by the number of simultaneous writers
13165 * (mnt_writeopcount) plus a bit of slop (2) in case some more
13166 * writers show up while we are cleaning.
13168 * Additionally, if we are unpriviledged and allocating space,
13169 * we need to ensure that we clean up enough blocks to get the
13170 * needed number of blocks over the threshhold of the minimum
13171 * number of blocks required to be kept free by the filesystem
13174 if (resource == FLUSH_INODES_WAIT) {
13175 needed = vp->v_mount->mnt_writeopcount + 2;
13176 } else if (resource == FLUSH_BLOCKS_WAIT) {
13177 needed = (vp->v_mount->mnt_writeopcount + 2) *
13178 fs->fs_contigsumsize;
13179 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0))
13180 needed += fragstoblks(fs,
13181 roundup((fs->fs_dsize * fs->fs_minfree / 100) -
13182 fs->fs_cstotal.cs_nffree, fs->fs_frag));
13185 printf("softdep_request_cleanup: Unknown resource type %d\n",
13189 starttime = time_second;
13191 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 &&
13192 fs->fs_cstotal.cs_nbfree <= needed) ||
13193 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13194 fs->fs_cstotal.cs_nifree <= needed)) {
13196 if (ump->softdep_on_worklist > 0 &&
13197 process_worklist_item(UFSTOVFS(ump),
13198 ump->softdep_on_worklist, LK_NOWAIT) != 0)
13199 stat_worklist_push += 1;
13203 * If we still need resources and there are no more worklist
13204 * entries to process to obtain them, we have to start flushing
13205 * the dirty vnodes to force the release of additional requests
13206 * to the worklist that we can then process to reap addition
13207 * resources. We walk the vnodes associated with the mount point
13208 * until we get the needed worklist requests that we can reap.
13210 if ((resource == FLUSH_BLOCKS_WAIT &&
13211 fs->fs_cstotal.cs_nbfree <= needed) ||
13212 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 &&
13213 fs->fs_cstotal.cs_nifree <= needed)) {
13214 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) {
13215 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) {
13219 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT,
13222 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */
13226 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0);
13229 lvp = ump->um_devvp;
13230 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
13231 VOP_FSYNC(lvp, MNT_NOWAIT, curthread);
13232 VOP_UNLOCK(lvp, 0);
13234 if (ump->softdep_on_worklist > 0) {
13235 stat_cleanup_retries += 1;
13238 stat_cleanup_failures += 1;
13240 if (time_second - starttime > stat_cleanup_high_delay)
13241 stat_cleanup_high_delay = time_second - starttime;
13247 softdep_excess_inodes(struct ufsmount *ump)
13250 return (dep_current[D_INODEDEP] > max_softdeps &&
13251 ump->softdep_curdeps[D_INODEDEP] > max_softdeps /
13252 stat_flush_threads);
13256 softdep_excess_dirrem(struct ufsmount *ump)
13259 return (dep_current[D_DIRREM] > max_softdeps / 2 &&
13260 ump->softdep_curdeps[D_DIRREM] > (max_softdeps / 2) /
13261 stat_flush_threads);
13265 schedule_cleanup(struct mount *mp)
13267 struct ufsmount *ump;
13270 ump = VFSTOUFS(mp);
13274 if ((td->td_pflags & TDP_KTHREAD) != 0 &&
13275 (td->td_proc->p_flag2 & P2_AST_SU) == 0) {
13277 * No ast is delivered to kernel threads, so nobody
13278 * would deref the mp. Some kernel threads
13279 * explicitely check for AST, e.g. NFS daemon does
13280 * this in the serving loop.
13284 if (td->td_su != NULL)
13285 vfs_rel(td->td_su);
13289 td->td_flags |= TDF_ASTPENDING;
13294 softdep_ast_cleanup_proc(void)
13298 struct ufsmount *ump;
13307 error = vfs_busy(mp, MBF_NOWAIT);
13311 if (ffs_own_mount(mp) && MOUNTEDSOFTDEP(mp)) {
13312 ump = VFSTOUFS(mp);
13316 if (softdep_excess_inodes(ump)) {
13318 request_cleanup(mp, FLUSH_INODES);
13320 if (softdep_excess_dirrem(ump)) {
13322 request_cleanup(mp, FLUSH_BLOCKS);
13325 if ((td->td_pflags & TDP_KTHREAD) != 0 || !req)
13333 * If memory utilization has gotten too high, deliberately slow things
13334 * down and speed up the I/O processing.
13337 request_cleanup(mp, resource)
13341 struct thread *td = curthread;
13342 struct ufsmount *ump;
13344 ump = VFSTOUFS(mp);
13347 * We never hold up the filesystem syncer or buf daemon.
13349 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
13352 * First check to see if the work list has gotten backlogged.
13353 * If it has, co-opt this process to help clean up two entries.
13354 * Because this process may hold inodes locked, we cannot
13355 * handle any remove requests that might block on a locked
13356 * inode as that could lead to deadlock. We set TDP_SOFTDEP
13357 * to avoid recursively processing the worklist.
13359 if (ump->softdep_on_worklist > max_softdeps / 10) {
13360 td->td_pflags |= TDP_SOFTDEP;
13361 process_worklist_item(mp, 2, LK_NOWAIT);
13362 td->td_pflags &= ~TDP_SOFTDEP;
13363 stat_worklist_push += 2;
13367 * Next, we attempt to speed up the syncer process. If that
13368 * is successful, then we allow the process to continue.
13370 if (softdep_speedup(ump) &&
13371 resource != FLUSH_BLOCKS_WAIT &&
13372 resource != FLUSH_INODES_WAIT)
13375 * If we are resource constrained on inode dependencies, try
13376 * flushing some dirty inodes. Otherwise, we are constrained
13377 * by file deletions, so try accelerating flushes of directories
13378 * with removal dependencies. We would like to do the cleanup
13379 * here, but we probably hold an inode locked at this point and
13380 * that might deadlock against one that we try to clean. So,
13381 * the best that we can do is request the syncer daemon to do
13382 * the cleanup for us.
13384 switch (resource) {
13387 case FLUSH_INODES_WAIT:
13388 ACQUIRE_GBLLOCK(&lk);
13389 stat_ino_limit_push += 1;
13390 req_clear_inodedeps += 1;
13392 stat_countp = &stat_ino_limit_hit;
13396 case FLUSH_BLOCKS_WAIT:
13397 ACQUIRE_GBLLOCK(&lk);
13398 stat_blk_limit_push += 1;
13399 req_clear_remove += 1;
13401 stat_countp = &stat_blk_limit_hit;
13405 panic("request_cleanup: unknown type");
13408 * Hopefully the syncer daemon will catch up and awaken us.
13409 * We wait at most tickdelay before proceeding in any case.
13411 ACQUIRE_GBLLOCK(&lk);
13414 if (callout_pending(&softdep_callout) == FALSE)
13415 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
13418 if ((td->td_pflags & TDP_KTHREAD) == 0)
13419 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
13427 * Awaken processes pausing in request_cleanup and clear proc_waiting
13428 * to indicate that there is no longer a timer running. Pause_timer
13429 * will be called with the global softdep mutex (&lk) locked.
13436 GBLLOCK_OWNED(&lk);
13438 * The callout_ API has acquired mtx and will hold it around this
13441 *stat_countp += proc_waiting;
13442 wakeup(&proc_waiting);
13446 * If requested, try removing inode or removal dependencies.
13449 check_clear_deps(mp)
13454 * If we are suspended, it may be because of our using
13455 * too many inodedeps, so help clear them out.
13457 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended)
13458 clear_inodedeps(mp);
13460 * General requests for cleanup of backed up dependencies
13462 ACQUIRE_GBLLOCK(&lk);
13463 if (req_clear_inodedeps) {
13464 req_clear_inodedeps -= 1;
13466 clear_inodedeps(mp);
13467 ACQUIRE_GBLLOCK(&lk);
13468 wakeup(&proc_waiting);
13470 if (req_clear_remove) {
13471 req_clear_remove -= 1;
13474 ACQUIRE_GBLLOCK(&lk);
13475 wakeup(&proc_waiting);
13481 * Flush out a directory with at least one removal dependency in an effort to
13482 * reduce the number of dirrem, freefile, and freeblks dependency structures.
13488 struct pagedep_hashhead *pagedephd;
13489 struct pagedep *pagedep;
13490 struct ufsmount *ump;
13496 ump = VFSTOUFS(mp);
13499 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) {
13500 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++];
13501 if (ump->pagedep_nextclean > ump->pagedep_hash_size)
13502 ump->pagedep_nextclean = 0;
13503 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
13504 if (LIST_EMPTY(&pagedep->pd_dirremhd))
13506 ino = pagedep->pd_ino;
13507 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13512 * Let unmount clear deps
13514 error = vfs_busy(mp, MBF_NOWAIT);
13517 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13521 softdep_error("clear_remove: vget", error);
13524 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13525 softdep_error("clear_remove: fsync", error);
13526 bo = &vp->v_bufobj;
13532 vn_finished_write(mp);
13540 * Clear out a block of dirty inodes in an effort to reduce
13541 * the number of inodedep dependency structures.
13544 clear_inodedeps(mp)
13547 struct inodedep_hashhead *inodedephd;
13548 struct inodedep *inodedep;
13549 struct ufsmount *ump;
13553 ino_t firstino, lastino, ino;
13555 ump = VFSTOUFS(mp);
13559 * Pick a random inode dependency to be cleared.
13560 * We will then gather up all the inodes in its block
13561 * that have dependencies and flush them out.
13563 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) {
13564 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++];
13565 if (ump->inodedep_nextclean > ump->inodedep_hash_size)
13566 ump->inodedep_nextclean = 0;
13567 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
13570 if (inodedep == NULL)
13573 * Find the last inode in the block with dependencies.
13575 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
13576 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
13577 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
13580 * Asynchronously push all but the last inode with dependencies.
13581 * Synchronously push the last inode with dependencies to ensure
13582 * that the inode block gets written to free up the inodedeps.
13584 for (ino = firstino; ino <= lastino; ino++) {
13585 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
13587 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
13590 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
13592 vn_finished_write(mp);
13596 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
13597 FFSV_FORCEINSMQ)) != 0) {
13598 softdep_error("clear_inodedeps: vget", error);
13600 vn_finished_write(mp);
13605 if (ino == lastino) {
13606 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)))
13607 softdep_error("clear_inodedeps: fsync1", error);
13609 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0)))
13610 softdep_error("clear_inodedeps: fsync2", error);
13611 BO_LOCK(&vp->v_bufobj);
13613 BO_UNLOCK(&vp->v_bufobj);
13616 vn_finished_write(mp);
13622 softdep_buf_append(bp, wkhd)
13624 struct workhead *wkhd;
13626 struct worklist *wk;
13627 struct ufsmount *ump;
13629 if ((wk = LIST_FIRST(wkhd)) == NULL)
13631 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13632 ("softdep_buf_append called on non-softdep filesystem"));
13633 ump = VFSTOUFS(wk->wk_mp);
13635 while ((wk = LIST_FIRST(wkhd)) != NULL) {
13636 WORKLIST_REMOVE(wk);
13637 WORKLIST_INSERT(&bp->b_dep, wk);
13644 softdep_inode_append(ip, cred, wkhd)
13646 struct ucred *cred;
13647 struct workhead *wkhd;
13653 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0,
13654 ("softdep_inode_append called on non-softdep filesystem"));
13656 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
13657 (int)fs->fs_bsize, cred, &bp);
13660 softdep_freework(wkhd);
13663 softdep_buf_append(bp, wkhd);
13668 softdep_freework(wkhd)
13669 struct workhead *wkhd;
13671 struct worklist *wk;
13672 struct ufsmount *ump;
13674 if ((wk = LIST_FIRST(wkhd)) == NULL)
13676 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0,
13677 ("softdep_freework called on non-softdep filesystem"));
13678 ump = VFSTOUFS(wk->wk_mp);
13680 handle_jwork(wkhd);
13685 * Function to determine if the buffer has outstanding dependencies
13686 * that will cause a roll-back if the buffer is written. If wantcount
13687 * is set, return number of dependencies, otherwise just yes or no.
13690 softdep_count_dependencies(bp, wantcount)
13694 struct worklist *wk;
13695 struct ufsmount *ump;
13696 struct bmsafemap *bmsafemap;
13697 struct freework *freework;
13698 struct inodedep *inodedep;
13699 struct indirdep *indirdep;
13700 struct freeblks *freeblks;
13701 struct allocindir *aip;
13702 struct pagedep *pagedep;
13703 struct dirrem *dirrem;
13704 struct newblk *newblk;
13705 struct mkdir *mkdir;
13706 struct diradd *dap;
13710 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL)
13712 ump = VFSTOUFS(wk->wk_mp);
13714 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
13715 switch (wk->wk_type) {
13718 inodedep = WK_INODEDEP(wk);
13719 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
13720 /* bitmap allocation dependency */
13725 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
13726 /* direct block pointer dependency */
13731 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
13732 /* direct block pointer dependency */
13737 if (TAILQ_FIRST(&inodedep->id_inoreflst)) {
13738 /* Add reference dependency. */
13746 indirdep = WK_INDIRDEP(wk);
13748 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) {
13749 /* indirect truncation dependency */
13755 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
13756 /* indirect block pointer dependency */
13764 pagedep = WK_PAGEDEP(wk);
13765 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
13766 if (LIST_FIRST(&dirrem->dm_jremrefhd)) {
13767 /* Journal remove ref dependency. */
13773 for (i = 0; i < DAHASHSZ; i++) {
13775 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
13776 /* directory entry dependency */
13785 bmsafemap = WK_BMSAFEMAP(wk);
13786 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) {
13787 /* Add reference dependency. */
13792 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) {
13793 /* Allocate block dependency. */
13801 freeblks = WK_FREEBLKS(wk);
13802 if (LIST_FIRST(&freeblks->fb_jblkdephd)) {
13803 /* Freeblk journal dependency. */
13810 case D_ALLOCDIRECT:
13812 newblk = WK_NEWBLK(wk);
13813 if (newblk->nb_jnewblk) {
13814 /* Journal allocate dependency. */
13822 mkdir = WK_MKDIR(wk);
13823 if (mkdir->md_jaddref) {
13824 /* Journal reference dependency. */
13836 /* never a dependency on these blocks */
13840 panic("softdep_count_dependencies: Unexpected type %s",
13841 TYPENAME(wk->wk_type));
13851 * Acquire exclusive access to a buffer.
13852 * Must be called with a locked mtx parameter.
13853 * Return acquired buffer or NULL on failure.
13855 static struct buf *
13856 getdirtybuf(bp, lock, waitfor)
13858 struct rwlock *lock;
13863 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
13864 if (waitfor != MNT_WAIT)
13866 error = BUF_LOCK(bp,
13867 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock);
13869 * Even if we sucessfully acquire bp here, we have dropped
13870 * lock, which may violates our guarantee.
13874 else if (error != ENOLCK)
13875 panic("getdirtybuf: inconsistent lock: %d", error);
13879 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
13880 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) {
13882 BO_LOCK(bp->b_bufobj);
13884 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
13885 bp->b_vflags |= BV_BKGRDWAIT;
13886 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj),
13887 PRIBIO | PDROP, "getbuf", 0);
13889 BO_UNLOCK(bp->b_bufobj);
13894 if (waitfor != MNT_WAIT)
13897 * The lock argument must be bp->b_vp's mutex in
13900 #ifdef DEBUG_VFS_LOCKS
13901 if (bp->b_vp->v_type != VCHR)
13902 ASSERT_BO_WLOCKED(bp->b_bufobj);
13904 bp->b_vflags |= BV_BKGRDWAIT;
13905 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0);
13908 if ((bp->b_flags & B_DELWRI) == 0) {
13918 * Check if it is safe to suspend the file system now. On entry,
13919 * the vnode interlock for devvp should be held. Return 0 with
13920 * the mount interlock held if the file system can be suspended now,
13921 * otherwise return EAGAIN with the mount interlock held.
13924 softdep_check_suspend(struct mount *mp,
13925 struct vnode *devvp,
13926 int softdep_depcnt,
13927 int softdep_accdepcnt,
13928 int secondary_writes,
13929 int secondary_accwrites)
13932 struct ufsmount *ump;
13933 struct inodedep *inodedep;
13934 int error, unlinked;
13936 bo = &devvp->v_bufobj;
13937 ASSERT_BO_WLOCKED(bo);
13940 * If we are not running with soft updates, then we need only
13941 * deal with secondary writes as we try to suspend.
13943 if (MOUNTEDSOFTDEP(mp) == 0) {
13945 while (mp->mnt_secondary_writes != 0) {
13947 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
13948 (PUSER - 1) | PDROP, "secwr", 0);
13954 * Reasons for needing more work before suspend:
13955 * - Dirty buffers on devvp.
13956 * - Secondary writes occurred after start of vnode sync loop
13959 if (bo->bo_numoutput > 0 ||
13960 bo->bo_dirty.bv_cnt > 0 ||
13961 secondary_writes != 0 ||
13962 mp->mnt_secondary_writes != 0 ||
13963 secondary_accwrites != mp->mnt_secondary_accwrites)
13970 * If we are running with soft updates, then we need to coordinate
13971 * with them as we try to suspend.
13973 ump = VFSTOUFS(mp);
13975 if (!TRY_ACQUIRE_LOCK(ump)) {
13983 if (mp->mnt_secondary_writes != 0) {
13986 msleep(&mp->mnt_secondary_writes,
13988 (PUSER - 1) | PDROP, "secwr", 0);
13996 if (MOUNTEDSUJ(mp)) {
13997 for (inodedep = TAILQ_FIRST(&ump->softdep_unlinked);
13999 inodedep = TAILQ_NEXT(inodedep, id_unlinked)) {
14000 if ((inodedep->id_state & (UNLINKED | UNLINKLINKS |
14001 UNLINKONLIST)) != (UNLINKED | UNLINKLINKS |
14003 !check_inodedep_free(inodedep))
14010 * Reasons for needing more work before suspend:
14011 * - Dirty buffers on devvp.
14012 * - Softdep activity occurred after start of vnode sync loop
14013 * - Secondary writes occurred after start of vnode sync loop
14016 if (bo->bo_numoutput > 0 ||
14017 bo->bo_dirty.bv_cnt > 0 ||
14018 softdep_depcnt != unlinked ||
14019 ump->softdep_deps != unlinked ||
14020 softdep_accdepcnt != ump->softdep_accdeps ||
14021 secondary_writes != 0 ||
14022 mp->mnt_secondary_writes != 0 ||
14023 secondary_accwrites != mp->mnt_secondary_accwrites)
14032 * Get the number of dependency structures for the file system, both
14033 * the current number and the total number allocated. These will
14034 * later be used to detect that softdep processing has occurred.
14037 softdep_get_depcounts(struct mount *mp,
14038 int *softdep_depsp,
14039 int *softdep_accdepsp)
14041 struct ufsmount *ump;
14043 if (MOUNTEDSOFTDEP(mp) == 0) {
14044 *softdep_depsp = 0;
14045 *softdep_accdepsp = 0;
14048 ump = VFSTOUFS(mp);
14050 *softdep_depsp = ump->softdep_deps;
14051 *softdep_accdepsp = ump->softdep_accdeps;
14056 * Wait for pending output on a vnode to complete.
14057 * Must be called with vnode lock and interlock locked.
14059 * XXX: Should just be a call to bufobj_wwait().
14067 bo = &vp->v_bufobj;
14068 ASSERT_VOP_LOCKED(vp, "drain_output");
14069 ASSERT_BO_WLOCKED(bo);
14071 while (bo->bo_numoutput) {
14072 bo->bo_flag |= BO_WWAIT;
14073 msleep((caddr_t)&bo->bo_numoutput,
14074 BO_LOCKPTR(bo), PRIBIO + 1, "drainvp", 0);
14079 * Called whenever a buffer that is being invalidated or reallocated
14080 * contains dependencies. This should only happen if an I/O error has
14081 * occurred. The routine is called with the buffer locked.
14084 softdep_deallocate_dependencies(bp)
14088 if ((bp->b_ioflags & BIO_ERROR) == 0)
14089 panic("softdep_deallocate_dependencies: dangling deps");
14090 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL)
14091 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
14093 printf("softdep_deallocate_dependencies: "
14094 "got error %d while accessing filesystem\n", bp->b_error);
14095 if (bp->b_error != ENXIO)
14096 panic("softdep_deallocate_dependencies: unrecovered I/O error");
14100 * Function to handle asynchronous write errors in the filesystem.
14103 softdep_error(func, error)
14108 /* XXX should do something better! */
14109 printf("%s: got error %d while accessing filesystem\n", func, error);
14115 inodedep_print(struct inodedep *inodedep, int verbose)
14117 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d"
14119 inodedep, inodedep->id_fs, inodedep->id_state,
14120 (intmax_t)inodedep->id_ino,
14121 (intmax_t)fsbtodb(inodedep->id_fs,
14122 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)),
14123 inodedep->id_nlinkdelta, inodedep->id_savednlink,
14124 inodedep->id_savedino1);
14129 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, "
14131 LIST_FIRST(&inodedep->id_pendinghd),
14132 LIST_FIRST(&inodedep->id_bufwait),
14133 LIST_FIRST(&inodedep->id_inowait),
14134 TAILQ_FIRST(&inodedep->id_inoreflst),
14135 inodedep->id_mkdiradd);
14136 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n",
14137 TAILQ_FIRST(&inodedep->id_inoupdt),
14138 TAILQ_FIRST(&inodedep->id_newinoupdt),
14139 TAILQ_FIRST(&inodedep->id_extupdt),
14140 TAILQ_FIRST(&inodedep->id_newextupdt));
14143 DB_SHOW_COMMAND(inodedep, db_show_inodedep)
14146 if (have_addr == 0) {
14147 db_printf("Address required\n");
14150 inodedep_print((struct inodedep*)addr, 1);
14153 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps)
14155 struct inodedep_hashhead *inodedephd;
14156 struct inodedep *inodedep;
14157 struct ufsmount *ump;
14160 if (have_addr == 0) {
14161 db_printf("Address required\n");
14164 ump = (struct ufsmount *)addr;
14165 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) {
14166 inodedephd = &ump->inodedep_hashtbl[cnt];
14167 LIST_FOREACH(inodedep, inodedephd, id_hash) {
14168 inodedep_print(inodedep, 0);
14173 DB_SHOW_COMMAND(worklist, db_show_worklist)
14175 struct worklist *wk;
14177 if (have_addr == 0) {
14178 db_printf("Address required\n");
14181 wk = (struct worklist *)addr;
14182 printf("worklist: %p type %s state 0x%X\n",
14183 wk, TYPENAME(wk->wk_type), wk->wk_state);
14186 DB_SHOW_COMMAND(workhead, db_show_workhead)
14188 struct workhead *wkhd;
14189 struct worklist *wk;
14192 if (have_addr == 0) {
14193 db_printf("Address required\n");
14196 wkhd = (struct workhead *)addr;
14197 wk = LIST_FIRST(wkhd);
14198 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list))
14199 db_printf("worklist: %p type %s state 0x%X",
14200 wk, TYPENAME(wk->wk_type), wk->wk_state);
14202 db_printf("workhead overflow");
14207 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs)
14209 struct mkdirlist *mkdirlisthd;
14210 struct jaddref *jaddref;
14211 struct diradd *diradd;
14212 struct mkdir *mkdir;
14214 if (have_addr == 0) {
14215 db_printf("Address required\n");
14218 mkdirlisthd = (struct mkdirlist *)addr;
14219 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) {
14220 diradd = mkdir->md_diradd;
14221 db_printf("mkdir: %p state 0x%X dap %p state 0x%X",
14222 mkdir, mkdir->md_state, diradd, diradd->da_state);
14223 if ((jaddref = mkdir->md_jaddref) != NULL)
14224 db_printf(" jaddref %p jaddref state 0x%X",
14225 jaddref, jaddref->ja_state);
14230 /* exported to ffs_vfsops.c */
14231 extern void db_print_ffs(struct ufsmount *ump);
14233 db_print_ffs(struct ufsmount *ump)
14235 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n",
14236 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname,
14237 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist,
14238 ump->softdep_deps, ump->softdep_req);
14243 #endif /* SOFTUPDATES */