2 * Copyright 1998, 2000 Marshall Kirk McKusick. All Rights Reserved.
4 * The soft updates code is derived from the appendix of a University
5 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt,
6 * "Soft Updates: A Solution to the Metadata Update Problem in File
7 * Systems", CSE-TR-254-95, August 1995).
9 * Further information about soft updates can be obtained from:
11 * Marshall Kirk McKusick http://www.mckusick.com/softdep/
12 * 1614 Oxford Street mckusick@mckusick.com
13 * Berkeley, CA 94709-1608 +1-510-843-9542
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
48 * For now we want the safety net that DEBUG flags provide.
54 #include <sys/param.h>
55 #include <sys/kernel.h>
56 #include <sys/systm.h>
60 #include <sys/kthread.h>
62 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/mutex.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
69 #include <sys/vnode.h>
71 #include <ufs/ufs/dir.h>
72 #include <ufs/ufs/extattr.h>
73 #include <ufs/ufs/quota.h>
74 #include <ufs/ufs/inode.h>
75 #include <ufs/ufs/ufsmount.h>
76 #include <ufs/ffs/fs.h>
77 #include <ufs/ffs/softdep.h>
78 #include <ufs/ffs/ffs_extern.h>
79 #include <ufs/ufs/ufs_extern.h>
84 #include "opt_quota.h"
89 softdep_flushfiles(oldmnt, flags, td)
95 panic("softdep_flushfiles called");
99 softdep_mount(devvp, mp, fs, cred)
117 softdep_uninitialize()
124 softdep_setup_inomapdep(bp, ip, newinum)
130 panic("softdep_setup_inomapdep called");
134 softdep_setup_blkmapdep(bp, mp, newblkno)
137 ufs2_daddr_t newblkno;
140 panic("softdep_setup_blkmapdep called");
144 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
147 ufs2_daddr_t newblkno;
148 ufs2_daddr_t oldblkno;
154 panic("softdep_setup_allocdirect called");
158 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
161 ufs2_daddr_t newblkno;
162 ufs2_daddr_t oldblkno;
168 panic("softdep_setup_allocext called");
172 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
177 ufs2_daddr_t newblkno;
178 ufs2_daddr_t oldblkno;
182 panic("softdep_setup_allocindir_page called");
186 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
191 ufs2_daddr_t newblkno;
194 panic("softdep_setup_allocindir_meta called");
198 softdep_setup_freeblocks(ip, length, flags)
204 panic("softdep_setup_freeblocks called");
208 softdep_freefile(pvp, ino, mode)
214 panic("softdep_freefile called");
218 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
223 struct buf *newdirbp;
227 panic("softdep_setup_directory_add called");
231 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
239 panic("softdep_change_directoryentry_offset called");
243 softdep_setup_remove(bp, dp, ip, isrmdir)
250 panic("softdep_setup_remove called");
254 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
262 panic("softdep_setup_directory_change called");
266 softdep_change_linkcnt(ip)
270 panic("softdep_change_linkcnt called");
274 softdep_load_inodeblock(ip)
278 panic("softdep_load_inodeblock called");
282 softdep_update_inodeblock(ip, bp, waitfor)
288 panic("softdep_update_inodeblock called");
293 struct vnode *vp; /* the "in_core" copy of the inode */
300 softdep_fsync_mountdev(vp)
308 softdep_flushworklist(oldmnt, countp, td)
309 struct mount *oldmnt;
319 softdep_sync_metadata(struct vnode *vp)
330 panic("softdep_slowdown called");
334 softdep_releasefile(ip)
335 struct inode *ip; /* inode with the zero effective link count */
338 panic("softdep_releasefile called");
342 softdep_request_cleanup(fs, vp)
351 softdep_check_suspend(struct mount *mp,
355 int secondary_writes,
356 int secondary_accwrites)
362 (void) softdep_accdeps;
364 ASSERT_VI_LOCKED(devvp, "softdep_check_suspend");
365 bo = &devvp->v_bufobj;
368 if (!MNT_ITRYLOCK(mp)) {
375 if (mp->mnt_secondary_writes != 0) {
377 msleep(&mp->mnt_secondary_writes,
379 (PUSER - 1) | PDROP, "secwr", 0);
387 * Reasons for needing more work before suspend:
388 * - Dirty buffers on devvp.
389 * - Secondary writes occurred after start of vnode sync loop
392 if (bo->bo_numoutput > 0 ||
393 bo->bo_dirty.bv_cnt > 0 ||
394 secondary_writes != 0 ||
395 mp->mnt_secondary_writes != 0 ||
396 secondary_accwrites != mp->mnt_secondary_accwrites)
403 softdep_get_depcounts(struct mount *mp,
405 int *softdepactiveaccp)
409 *softdepactiveaccp = 0;
414 * These definitions need to be adapted to the system to which
415 * this file is being ported.
418 * malloc types defined for the softdep system.
420 static MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies");
421 static MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies");
422 static MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation");
423 static MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map");
424 static MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode");
425 static MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies");
426 static MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block");
427 static MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode");
428 static MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode");
429 static MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated");
430 static MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry");
431 static MALLOC_DEFINE(M_MKDIR, "mkdir","New directory");
432 static MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted");
433 static MALLOC_DEFINE(M_NEWDIRBLK, "newdirblk","Unclaimed new directory block");
434 static MALLOC_DEFINE(M_SAVEDINO, "savedino","Saved inodes");
436 #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE)
441 #define D_BMSAFEMAP 3
442 #define D_ALLOCDIRECT 4
444 #define D_ALLOCINDIR 6
451 #define D_NEWDIRBLK 13
452 #define D_LAST D_NEWDIRBLK
455 * translate from workitem type to memory type
456 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
458 static struct malloc_type *memtype[] = {
475 #define DtoM(type) (memtype[type])
478 * Names of malloc types.
480 #define TYPENAME(type) \
481 ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???")
483 * End system adaptation definitions.
487 * Forward declarations.
489 struct inodedep_hashhead;
490 struct newblk_hashhead;
491 struct pagedep_hashhead;
494 * Internal function prototypes.
496 static void softdep_error(char *, int);
497 static void drain_output(struct vnode *);
498 static struct buf *getdirtybuf(struct buf *, struct mtx *, int);
499 static void clear_remove(struct thread *);
500 static void clear_inodedeps(struct thread *);
501 static int flush_pagedep_deps(struct vnode *, struct mount *,
503 static int flush_inodedep_deps(struct mount *, ino_t);
504 static int flush_deplist(struct allocdirectlst *, int, int *);
505 static int handle_written_filepage(struct pagedep *, struct buf *);
506 static void diradd_inode_written(struct diradd *, struct inodedep *);
507 static int handle_written_inodeblock(struct inodedep *, struct buf *);
508 static void handle_allocdirect_partdone(struct allocdirect *);
509 static void handle_allocindir_partdone(struct allocindir *);
510 static void initiate_write_filepage(struct pagedep *, struct buf *);
511 static void handle_written_mkdir(struct mkdir *, int);
512 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
513 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
514 static void handle_workitem_freefile(struct freefile *);
515 static void handle_workitem_remove(struct dirrem *, struct vnode *);
516 static struct dirrem *newdirrem(struct buf *, struct inode *,
517 struct inode *, int, struct dirrem **);
518 static void free_diradd(struct diradd *);
519 static void free_allocindir(struct allocindir *, struct inodedep *);
520 static void free_newdirblk(struct newdirblk *);
521 static int indir_trunc(struct freeblks *, ufs2_daddr_t, int, ufs_lbn_t,
523 static void deallocate_dependencies(struct buf *, struct inodedep *);
524 static void free_allocdirect(struct allocdirectlst *,
525 struct allocdirect *, int);
526 static int check_inode_unwritten(struct inodedep *);
527 static int free_inodedep(struct inodedep *);
528 static void handle_workitem_freeblocks(struct freeblks *, int);
529 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
530 static void setup_allocindir_phase2(struct buf *, struct inode *,
531 struct allocindir *);
532 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
534 static void handle_workitem_freefrag(struct freefrag *);
535 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long);
536 static void allocdirect_merge(struct allocdirectlst *,
537 struct allocdirect *, struct allocdirect *);
538 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *);
539 static int newblk_find(struct newblk_hashhead *, struct fs *, ufs2_daddr_t,
541 static int newblk_lookup(struct fs *, ufs2_daddr_t, int, struct newblk **);
542 static int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t,
544 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
545 static int pagedep_lookup(struct inode *, ufs_lbn_t, int, struct pagedep **);
546 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
547 struct mount *mp, int, struct pagedep **);
548 static void pause_timer(void *);
549 static int request_cleanup(struct mount *, int);
550 static int process_worklist_item(struct mount *, int);
551 static void add_to_worklist(struct worklist *);
552 static void softdep_flush(void);
553 static int softdep_speedup(void);
556 * Exported softdep operations.
558 static void softdep_disk_io_initiation(struct buf *);
559 static void softdep_disk_write_complete(struct buf *);
560 static void softdep_deallocate_dependencies(struct buf *);
561 static int softdep_count_dependencies(struct buf *bp, int);
563 static struct mtx lk;
564 MTX_SYSINIT(softdep_lock, &lk, "Softdep Lock", MTX_DEF);
566 #define TRY_ACQUIRE_LOCK(lk) mtx_trylock(lk)
567 #define ACQUIRE_LOCK(lk) mtx_lock(lk)
568 #define FREE_LOCK(lk) mtx_unlock(lk)
571 * Worklist queue management.
572 * These routines require that the lock be held.
574 #ifndef /* NOT */ DEBUG
575 #define WORKLIST_INSERT(head, item) do { \
576 (item)->wk_state |= ONWORKLIST; \
577 LIST_INSERT_HEAD(head, item, wk_list); \
579 #define WORKLIST_REMOVE(item) do { \
580 (item)->wk_state &= ~ONWORKLIST; \
581 LIST_REMOVE(item, wk_list); \
584 static void worklist_insert(struct workhead *, struct worklist *);
585 static void worklist_remove(struct worklist *);
587 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
588 #define WORKLIST_REMOVE(item) worklist_remove(item)
591 worklist_insert(head, item)
592 struct workhead *head;
593 struct worklist *item;
596 mtx_assert(&lk, MA_OWNED);
597 if (item->wk_state & ONWORKLIST)
598 panic("worklist_insert: already on list");
599 item->wk_state |= ONWORKLIST;
600 LIST_INSERT_HEAD(head, item, wk_list);
604 worklist_remove(item)
605 struct worklist *item;
608 mtx_assert(&lk, MA_OWNED);
609 if ((item->wk_state & ONWORKLIST) == 0)
610 panic("worklist_remove: not on list");
611 item->wk_state &= ~ONWORKLIST;
612 LIST_REMOVE(item, wk_list);
617 * Routines for tracking and managing workitems.
619 static void workitem_free(struct worklist *, int);
620 static void workitem_alloc(struct worklist *, int, struct mount *);
622 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type))
625 workitem_free(item, type)
626 struct worklist *item;
629 struct ufsmount *ump;
630 mtx_assert(&lk, MA_OWNED);
633 if (item->wk_state & ONWORKLIST)
634 panic("workitem_free: still on list");
635 if (item->wk_type != type)
636 panic("workitem_free: type mismatch");
638 ump = VFSTOUFS(item->wk_mp);
639 if (--ump->softdep_deps == 0 && ump->softdep_req)
640 wakeup(&ump->softdep_deps);
641 FREE(item, DtoM(type));
645 workitem_alloc(item, type, mp)
646 struct worklist *item;
650 item->wk_type = type;
654 VFSTOUFS(mp)->softdep_deps++;
655 VFSTOUFS(mp)->softdep_accdeps++;
660 * Workitem queue management
662 static int max_softdeps; /* maximum number of structs before slowdown */
663 static int maxindirdeps = 50; /* max number of indirdeps before slowdown */
664 static int tickdelay = 2; /* number of ticks to pause during slowdown */
665 static int proc_waiting; /* tracks whether we have a timeout posted */
666 static int *stat_countp; /* statistic to count in proc_waiting timeout */
667 static struct callout softdep_callout;
668 static int req_pending;
669 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
670 #define FLUSH_INODES 1
671 static int req_clear_remove; /* syncer process flush some freeblks */
672 #define FLUSH_REMOVE 2
673 #define FLUSH_REMOVE_WAIT 3
677 static int stat_worklist_push; /* number of worklist cleanups */
678 static int stat_blk_limit_push; /* number of times block limit neared */
679 static int stat_ino_limit_push; /* number of times inode limit neared */
680 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
681 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
682 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
683 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
684 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
685 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
686 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
688 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
689 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
690 SYSCTL_INT(_debug, OID_AUTO, maxindirdeps, CTLFLAG_RW, &maxindirdeps, 0, "");
691 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
692 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
693 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
694 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
695 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
696 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
697 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
698 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
699 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
700 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
701 /* SYSCTL_INT(_debug, OID_AUTO, worklist_num, CTLFLAG_RD, &softdep_on_worklist, 0, ""); */
703 SYSCTL_DECL(_vfs_ffs);
705 static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */
706 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
707 &compute_summary_at_mount, 0, "Recompute summary at mount");
709 static struct proc *softdepproc;
710 static struct kproc_desc softdep_kp = {
715 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
723 struct ufsmount *ump;
729 td->td_pflags |= TDP_NORUNNINGBUF;
732 kthread_suspend_check(softdepproc);
733 vfslocked = VFS_LOCK_GIANT((struct mount *)NULL);
736 * If requested, try removing inode or removal dependencies.
738 if (req_clear_inodedeps) {
740 req_clear_inodedeps -= 1;
741 wakeup_one(&proc_waiting);
743 if (req_clear_remove) {
745 req_clear_remove -= 1;
746 wakeup_one(&proc_waiting);
749 VFS_UNLOCK_GIANT(vfslocked);
751 mtx_lock(&mountlist_mtx);
752 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
753 nmp = TAILQ_NEXT(mp, mnt_list);
754 if ((mp->mnt_flag & MNT_SOFTDEP) == 0)
756 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
758 vfslocked = VFS_LOCK_GIANT(mp);
759 softdep_process_worklist(mp, 0);
761 remaining += ump->softdep_on_worklist -
762 ump->softdep_on_worklist_inprogress;
763 VFS_UNLOCK_GIANT(vfslocked);
764 mtx_lock(&mountlist_mtx);
765 nmp = TAILQ_NEXT(mp, mnt_list);
768 mtx_unlock(&mountlist_mtx);
773 msleep(&req_pending, &lk, PVM, "sdflush", hz);
780 softdep_speedup(void)
783 mtx_assert(&lk, MA_OWNED);
784 if (req_pending == 0) {
786 wakeup(&req_pending);
789 return speedup_syncer();
793 * Add an item to the end of the work queue.
794 * This routine requires that the lock be held.
795 * This is the only routine that adds items to the list.
796 * The following routine is the only one that removes items
797 * and does so in order from first to last.
803 struct ufsmount *ump;
805 mtx_assert(&lk, MA_OWNED);
806 ump = VFSTOUFS(wk->wk_mp);
807 if (wk->wk_state & ONWORKLIST)
808 panic("add_to_worklist: already on list");
809 wk->wk_state |= ONWORKLIST;
810 if (LIST_EMPTY(&ump->softdep_workitem_pending))
811 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
813 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
814 ump->softdep_worklist_tail = wk;
815 ump->softdep_on_worklist += 1;
819 * Process that runs once per second to handle items in the background queue.
821 * Note that we ensure that everything is done in the order in which they
822 * appear in the queue. The code below depends on this property to ensure
823 * that blocks of a file are freed before the inode itself is freed. This
824 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
825 * until all the old ones have been purged from the dependency lists.
828 softdep_process_worklist(mp, full)
832 struct thread *td = curthread;
833 int cnt, matchcnt, loopcount;
834 struct ufsmount *ump;
837 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
839 * Record the process identifier of our caller so that we can give
840 * this process preferential treatment in request_cleanup below.
846 starttime = time_second;
847 while (ump->softdep_on_worklist > 0) {
848 if ((cnt = process_worklist_item(mp, 0)) == -1)
853 * If requested, try removing inode or removal dependencies.
855 if (req_clear_inodedeps) {
857 req_clear_inodedeps -= 1;
858 wakeup_one(&proc_waiting);
860 if (req_clear_remove) {
862 req_clear_remove -= 1;
863 wakeup_one(&proc_waiting);
866 * We do not generally want to stop for buffer space, but if
867 * we are really being a buffer hog, we will stop and wait.
869 if (loopcount++ % 128 == 0) {
876 * Never allow processing to run for more than one
877 * second. Otherwise the other mountpoints may get
878 * excessively backlogged.
880 if (!full && starttime != time_second) {
890 * Process one item on the worklist.
893 process_worklist_item(mp, flags)
897 struct worklist *wk, *wkend;
898 struct ufsmount *ump;
902 mtx_assert(&lk, MA_OWNED);
903 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
905 * If we are being called because of a process doing a
906 * copy-on-write, then it is not safe to write as we may
907 * recurse into the copy-on-write routine.
909 if (curthread->td_pflags & TDP_COWINPROGRESS)
912 * Normally we just process each item on the worklist in order.
913 * However, if we are in a situation where we cannot lock any
914 * inodes, we have to skip over any dirrem requests whose
915 * vnodes are resident and locked.
919 LIST_FOREACH(wk, &ump->softdep_workitem_pending, wk_list) {
920 if (wk->wk_state & INPROGRESS)
922 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
924 wk->wk_state |= INPROGRESS;
925 ump->softdep_on_worklist_inprogress++;
927 ffs_vgetf(mp, WK_DIRREM(wk)->dm_oldinum,
928 LK_NOWAIT | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ);
930 wk->wk_state &= ~INPROGRESS;
931 ump->softdep_on_worklist_inprogress--;
938 * Remove the item to be processed. If we are removing the last
939 * item on the list, we need to recalculate the tail pointer.
940 * As this happens rarely and usually when the list is short,
941 * we just run down the list to find it rather than tracking it
945 if (wk == ump->softdep_worklist_tail) {
946 LIST_FOREACH(wkend, &ump->softdep_workitem_pending, wk_list)
947 if (LIST_NEXT(wkend, wk_list) == NULL)
949 ump->softdep_worklist_tail = wkend;
951 ump->softdep_on_worklist -= 1;
953 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
954 panic("process_worklist_item: suspended filesystem");
956 switch (wk->wk_type) {
959 /* removal of a directory entry */
960 handle_workitem_remove(WK_DIRREM(wk), vp);
964 /* releasing blocks and/or fragments from a file */
965 handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT);
969 /* releasing a fragment when replaced as a file grows */
970 handle_workitem_freefrag(WK_FREEFRAG(wk));
974 /* releasing an inode when its link count drops to 0 */
975 handle_workitem_freefile(WK_FREEFILE(wk));
979 panic("%s_process_worklist: Unknown type %s",
980 "softdep", TYPENAME(wk->wk_type));
983 vn_finished_secondary_write(mp);
989 * Move dependencies from one buffer to another.
992 softdep_move_dependencies(oldbp, newbp)
996 struct worklist *wk, *wktail;
998 if (!LIST_EMPTY(&newbp->b_dep))
999 panic("softdep_move_dependencies: need merge code");
1002 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
1003 LIST_REMOVE(wk, wk_list);
1005 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1007 LIST_INSERT_AFTER(wktail, wk, wk_list);
1014 * Purge the work list of all items associated with a particular mount point.
1017 softdep_flushworklist(oldmnt, countp, td)
1018 struct mount *oldmnt;
1022 struct vnode *devvp;
1023 int count, error = 0;
1024 struct ufsmount *ump;
1027 * Alternately flush the block device associated with the mount
1028 * point and process any dependencies that the flushing
1029 * creates. We continue until no more worklist dependencies
1033 ump = VFSTOUFS(oldmnt);
1034 devvp = ump->um_devvp;
1035 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1037 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
1038 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1039 VOP_UNLOCK(devvp, 0, td);
1047 softdep_waitidle(struct mount *mp)
1049 struct ufsmount *ump;
1055 for (i = 0; i < 10 && ump->softdep_deps; i++) {
1056 ump->softdep_req = 1;
1057 if (ump->softdep_on_worklist)
1058 panic("softdep_waitidle: work added after flush.");
1059 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1);
1061 ump->softdep_req = 0;
1066 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1074 * Flush all vnodes and worklist items associated with a specified mount point.
1077 softdep_flushfiles(oldmnt, flags, td)
1078 struct mount *oldmnt;
1082 int error, depcount, loopcnt, retry_flush_count, retry;
1085 retry_flush_count = 3;
1090 * Alternately flush the vnodes associated with the mount
1091 * point and process any dependencies that the flushing
1092 * creates. In theory, this loop can happen at most twice,
1093 * but we give it a few extra just to be sure.
1095 for (; loopcnt > 0; loopcnt--) {
1097 * Do another flush in case any vnodes were brought in
1098 * as part of the cleanup operations.
1100 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
1102 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
1107 * If we are unmounting then it is an error to fail. If we
1108 * are simply trying to downgrade to read-only, then filesystem
1109 * activity can keep us busy forever, so we just fail with EBUSY.
1112 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
1113 panic("softdep_flushfiles: looping");
1117 error = softdep_waitidle(oldmnt);
1119 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
1122 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0,
1123 ("softdep_flushfiles: !MNTK_NOINSMNTQ"));
1124 if (oldmnt->mnt_nvnodelistsize > 0) {
1125 if (--retry_flush_count > 0) {
1131 MNT_IUNLOCK(oldmnt);
1140 * Structure hashing.
1142 * There are three types of structures that can be looked up:
1143 * 1) pagedep structures identified by mount point, inode number,
1144 * and logical block.
1145 * 2) inodedep structures identified by mount point and inode number.
1146 * 3) newblk structures identified by mount point and
1147 * physical block number.
1149 * The "pagedep" and "inodedep" dependency structures are hashed
1150 * separately from the file blocks and inodes to which they correspond.
1151 * This separation helps when the in-memory copy of an inode or
1152 * file block must be replaced. It also obviates the need to access
1153 * an inode or file page when simply updating (or de-allocating)
1154 * dependency structures. Lookup of newblk structures is needed to
1155 * find newly allocated blocks when trying to associate them with
1156 * their allocdirect or allocindir structure.
1158 * The lookup routines optionally create and hash a new instance when
1159 * an existing entry is not found.
1161 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
1162 #define NODELAY 0x0002 /* cannot do background work */
1165 * Structures and routines associated with pagedep caching.
1167 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
1168 u_long pagedep_hash; /* size of hash table - 1 */
1169 #define PAGEDEP_HASH(mp, inum, lbn) \
1170 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
1174 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp)
1175 struct pagedep_hashhead *pagedephd;
1180 struct pagedep **pagedeppp;
1182 struct pagedep *pagedep;
1184 LIST_FOREACH(pagedep, pagedephd, pd_hash)
1185 if (ino == pagedep->pd_ino &&
1186 lbn == pagedep->pd_lbn &&
1187 mp == pagedep->pd_list.wk_mp)
1190 *pagedeppp = pagedep;
1191 if ((flags & DEPALLOC) != 0 &&
1192 (pagedep->pd_state & ONWORKLIST) == 0)
1200 * Look up a pagedep. Return 1 if found, 0 if not found or found
1201 * when asked to allocate but not associated with any buffer.
1202 * If not found, allocate if DEPALLOC flag is passed.
1203 * Found or allocated entry is returned in pagedeppp.
1204 * This routine must be called with splbio interrupts blocked.
1207 pagedep_lookup(ip, lbn, flags, pagedeppp)
1211 struct pagedep **pagedeppp;
1213 struct pagedep *pagedep;
1214 struct pagedep_hashhead *pagedephd;
1219 mtx_assert(&lk, MA_OWNED);
1220 mp = ITOV(ip)->v_mount;
1221 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
1223 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1224 if (*pagedeppp || (flags & DEPALLOC) == 0)
1227 MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep),
1228 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
1229 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
1231 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1233 WORKITEM_FREE(pagedep, D_PAGEDEP);
1236 pagedep->pd_ino = ip->i_number;
1237 pagedep->pd_lbn = lbn;
1238 LIST_INIT(&pagedep->pd_dirremhd);
1239 LIST_INIT(&pagedep->pd_pendinghd);
1240 for (i = 0; i < DAHASHSZ; i++)
1241 LIST_INIT(&pagedep->pd_diraddhd[i]);
1242 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
1243 *pagedeppp = pagedep;
1248 * Structures and routines associated with inodedep caching.
1250 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
1251 static u_long inodedep_hash; /* size of hash table - 1 */
1252 static long num_inodedep; /* number of inodedep allocated */
1253 #define INODEDEP_HASH(fs, inum) \
1254 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
1257 inodedep_find(inodedephd, fs, inum, inodedeppp)
1258 struct inodedep_hashhead *inodedephd;
1261 struct inodedep **inodedeppp;
1263 struct inodedep *inodedep;
1265 LIST_FOREACH(inodedep, inodedephd, id_hash)
1266 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
1269 *inodedeppp = inodedep;
1277 * Look up an inodedep. Return 1 if found, 0 if not found.
1278 * If not found, allocate if DEPALLOC flag is passed.
1279 * Found or allocated entry is returned in inodedeppp.
1280 * This routine must be called with splbio interrupts blocked.
1283 inodedep_lookup(mp, inum, flags, inodedeppp)
1287 struct inodedep **inodedeppp;
1289 struct inodedep *inodedep;
1290 struct inodedep_hashhead *inodedephd;
1293 mtx_assert(&lk, MA_OWNED);
1294 fs = VFSTOUFS(mp)->um_fs;
1295 inodedephd = INODEDEP_HASH(fs, inum);
1297 if (inodedep_find(inodedephd, fs, inum, inodedeppp))
1299 if ((flags & DEPALLOC) == 0)
1302 * If we are over our limit, try to improve the situation.
1304 if (num_inodedep > max_softdeps && (flags & NODELAY) == 0)
1305 request_cleanup(mp, FLUSH_INODES);
1307 MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep),
1308 M_INODEDEP, M_SOFTDEP_FLAGS);
1309 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
1311 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) {
1312 WORKITEM_FREE(inodedep, D_INODEDEP);
1316 inodedep->id_fs = fs;
1317 inodedep->id_ino = inum;
1318 inodedep->id_state = ALLCOMPLETE;
1319 inodedep->id_nlinkdelta = 0;
1320 inodedep->id_savedino1 = NULL;
1321 inodedep->id_savedsize = -1;
1322 inodedep->id_savedextsize = -1;
1323 inodedep->id_buf = NULL;
1324 LIST_INIT(&inodedep->id_pendinghd);
1325 LIST_INIT(&inodedep->id_inowait);
1326 LIST_INIT(&inodedep->id_bufwait);
1327 TAILQ_INIT(&inodedep->id_inoupdt);
1328 TAILQ_INIT(&inodedep->id_newinoupdt);
1329 TAILQ_INIT(&inodedep->id_extupdt);
1330 TAILQ_INIT(&inodedep->id_newextupdt);
1331 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1332 *inodedeppp = inodedep;
1337 * Structures and routines associated with newblk caching.
1339 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1340 u_long newblk_hash; /* size of hash table - 1 */
1341 #define NEWBLK_HASH(fs, inum) \
1342 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1345 newblk_find(newblkhd, fs, newblkno, newblkpp)
1346 struct newblk_hashhead *newblkhd;
1348 ufs2_daddr_t newblkno;
1349 struct newblk **newblkpp;
1351 struct newblk *newblk;
1353 LIST_FOREACH(newblk, newblkhd, nb_hash)
1354 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1365 * Look up a newblk. Return 1 if found, 0 if not found.
1366 * If not found, allocate if DEPALLOC flag is passed.
1367 * Found or allocated entry is returned in newblkpp.
1370 newblk_lookup(fs, newblkno, flags, newblkpp)
1372 ufs2_daddr_t newblkno;
1374 struct newblk **newblkpp;
1376 struct newblk *newblk;
1377 struct newblk_hashhead *newblkhd;
1379 newblkhd = NEWBLK_HASH(fs, newblkno);
1380 if (newblk_find(newblkhd, fs, newblkno, newblkpp))
1382 if ((flags & DEPALLOC) == 0)
1385 MALLOC(newblk, struct newblk *, sizeof(struct newblk),
1386 M_NEWBLK, M_SOFTDEP_FLAGS);
1388 if (newblk_find(newblkhd, fs, newblkno, newblkpp)) {
1389 FREE(newblk, M_NEWBLK);
1392 newblk->nb_state = 0;
1394 newblk->nb_newblkno = newblkno;
1395 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1401 * Executed during filesystem system initialization before
1402 * mounting any filesystems.
1405 softdep_initialize()
1408 LIST_INIT(&mkdirlisthd);
1409 max_softdeps = desiredvnodes * 4;
1410 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1412 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1413 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1415 /* initialise bioops hack */
1416 bioops.io_start = softdep_disk_io_initiation;
1417 bioops.io_complete = softdep_disk_write_complete;
1418 bioops.io_deallocate = softdep_deallocate_dependencies;
1419 bioops.io_countdeps = softdep_count_dependencies;
1421 /* Initialize the callout with an mtx. */
1422 callout_init_mtx(&softdep_callout, &lk, 0);
1426 * Executed after all filesystems have been unmounted during
1427 * filesystem module unload.
1430 softdep_uninitialize()
1433 callout_drain(&softdep_callout);
1434 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash);
1435 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash);
1436 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash);
1440 * Called at mount time to notify the dependency code that a
1441 * filesystem wishes to use it.
1444 softdep_mount(devvp, mp, fs, cred)
1445 struct vnode *devvp;
1450 struct csum_total cstotal;
1451 struct ufsmount *ump;
1457 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
1458 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
1459 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
1465 LIST_INIT(&ump->softdep_workitem_pending);
1466 ump->softdep_worklist_tail = NULL;
1467 ump->softdep_on_worklist = 0;
1468 ump->softdep_deps = 0;
1470 * When doing soft updates, the counters in the
1471 * superblock may have gotten out of sync. Recomputation
1472 * can take a long time and can be deferred for background
1473 * fsck. However, the old behavior of scanning the cylinder
1474 * groups and recalculating them at mount time is available
1475 * by setting vfs.ffs.compute_summary_at_mount to one.
1477 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
1479 bzero(&cstotal, sizeof cstotal);
1480 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1481 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1482 fs->fs_cgsize, cred, &bp)) != 0) {
1486 cgp = (struct cg *)bp->b_data;
1487 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1488 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1489 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1490 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1491 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1495 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1496 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
1498 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1503 * Protecting the freemaps (or bitmaps).
1505 * To eliminate the need to execute fsck before mounting a filesystem
1506 * after a power failure, one must (conservatively) guarantee that the
1507 * on-disk copy of the bitmaps never indicate that a live inode or block is
1508 * free. So, when a block or inode is allocated, the bitmap should be
1509 * updated (on disk) before any new pointers. When a block or inode is
1510 * freed, the bitmap should not be updated until all pointers have been
1511 * reset. The latter dependency is handled by the delayed de-allocation
1512 * approach described below for block and inode de-allocation. The former
1513 * dependency is handled by calling the following procedure when a block or
1514 * inode is allocated. When an inode is allocated an "inodedep" is created
1515 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1516 * Each "inodedep" is also inserted into the hash indexing structure so
1517 * that any additional link additions can be made dependent on the inode
1520 * The ufs filesystem maintains a number of free block counts (e.g., per
1521 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1522 * in addition to the bitmaps. These counts are used to improve efficiency
1523 * during allocation and therefore must be consistent with the bitmaps.
1524 * There is no convenient way to guarantee post-crash consistency of these
1525 * counts with simple update ordering, for two main reasons: (1) The counts
1526 * and bitmaps for a single cylinder group block are not in the same disk
1527 * sector. If a disk write is interrupted (e.g., by power failure), one may
1528 * be written and the other not. (2) Some of the counts are located in the
1529 * superblock rather than the cylinder group block. So, we focus our soft
1530 * updates implementation on protecting the bitmaps. When mounting a
1531 * filesystem, we recompute the auxiliary counts from the bitmaps.
1535 * Called just after updating the cylinder group block to allocate an inode.
1538 softdep_setup_inomapdep(bp, ip, newinum)
1539 struct buf *bp; /* buffer for cylgroup block with inode map */
1540 struct inode *ip; /* inode related to allocation */
1541 ino_t newinum; /* new inode number being allocated */
1543 struct inodedep *inodedep;
1544 struct bmsafemap *bmsafemap;
1547 * Create a dependency for the newly allocated inode.
1548 * Panic if it already exists as something is seriously wrong.
1549 * Otherwise add it to the dependency list for the buffer holding
1550 * the cylinder group map from which it was allocated.
1553 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), newinum, DEPALLOC|NODELAY,
1555 panic("softdep_setup_inomapdep: dependency for new inode "
1557 inodedep->id_buf = bp;
1558 inodedep->id_state &= ~DEPCOMPLETE;
1559 bmsafemap = bmsafemap_lookup(inodedep->id_list.wk_mp, bp);
1560 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1565 * Called just after updating the cylinder group block to
1566 * allocate block or fragment.
1569 softdep_setup_blkmapdep(bp, mp, newblkno)
1570 struct buf *bp; /* buffer for cylgroup block with block map */
1571 struct mount *mp; /* filesystem doing allocation */
1572 ufs2_daddr_t newblkno; /* number of newly allocated block */
1574 struct newblk *newblk;
1575 struct bmsafemap *bmsafemap;
1578 fs = VFSTOUFS(mp)->um_fs;
1580 * Create a dependency for the newly allocated block.
1581 * Add it to the dependency list for the buffer holding
1582 * the cylinder group map from which it was allocated.
1585 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1586 panic("softdep_setup_blkmapdep: found block");
1587 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp);
1588 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1593 * Find the bmsafemap associated with a cylinder group buffer.
1594 * If none exists, create one. The buffer must be locked when
1595 * this routine is called and this routine must be called with
1596 * splbio interrupts blocked.
1598 static struct bmsafemap *
1599 bmsafemap_lookup(mp, bp)
1603 struct bmsafemap *bmsafemap;
1604 struct worklist *wk;
1606 mtx_assert(&lk, MA_OWNED);
1607 LIST_FOREACH(wk, &bp->b_dep, wk_list)
1608 if (wk->wk_type == D_BMSAFEMAP)
1609 return (WK_BMSAFEMAP(wk));
1611 MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1612 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1613 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
1614 bmsafemap->sm_buf = bp;
1615 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1616 LIST_INIT(&bmsafemap->sm_allocindirhd);
1617 LIST_INIT(&bmsafemap->sm_inodedephd);
1618 LIST_INIT(&bmsafemap->sm_newblkhd);
1620 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1625 * Direct block allocation dependencies.
1627 * When a new block is allocated, the corresponding disk locations must be
1628 * initialized (with zeros or new data) before the on-disk inode points to
1629 * them. Also, the freemap from which the block was allocated must be
1630 * updated (on disk) before the inode's pointer. These two dependencies are
1631 * independent of each other and are needed for all file blocks and indirect
1632 * blocks that are pointed to directly by the inode. Just before the
1633 * "in-core" version of the inode is updated with a newly allocated block
1634 * number, a procedure (below) is called to setup allocation dependency
1635 * structures. These structures are removed when the corresponding
1636 * dependencies are satisfied or when the block allocation becomes obsolete
1637 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1638 * fragment that gets upgraded). All of these cases are handled in
1639 * procedures described later.
1641 * When a file extension causes a fragment to be upgraded, either to a larger
1642 * fragment or to a full block, the on-disk location may change (if the
1643 * previous fragment could not simply be extended). In this case, the old
1644 * fragment must be de-allocated, but not until after the inode's pointer has
1645 * been updated. In most cases, this is handled by later procedures, which
1646 * will construct a "freefrag" structure to be added to the workitem queue
1647 * when the inode update is complete (or obsolete). The main exception to
1648 * this is when an allocation occurs while a pending allocation dependency
1649 * (for the same block pointer) remains. This case is handled in the main
1650 * allocation dependency setup procedure by immediately freeing the
1651 * unreferenced fragments.
1654 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1655 struct inode *ip; /* inode to which block is being added */
1656 ufs_lbn_t lbn; /* block pointer within inode */
1657 ufs2_daddr_t newblkno; /* disk block number being added */
1658 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
1659 long newsize; /* size of new block */
1660 long oldsize; /* size of new block */
1661 struct buf *bp; /* bp for allocated block */
1663 struct allocdirect *adp, *oldadp;
1664 struct allocdirectlst *adphead;
1665 struct bmsafemap *bmsafemap;
1666 struct inodedep *inodedep;
1667 struct pagedep *pagedep;
1668 struct newblk *newblk;
1671 mp = UFSTOVFS(ip->i_ump);
1672 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1673 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1674 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1676 adp->ad_newblkno = newblkno;
1677 adp->ad_oldblkno = oldblkno;
1678 adp->ad_newsize = newsize;
1679 adp->ad_oldsize = oldsize;
1680 adp->ad_state = ATTACHED;
1681 LIST_INIT(&adp->ad_newdirblk);
1682 if (newblkno == oldblkno)
1683 adp->ad_freefrag = NULL;
1685 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1688 if (lbn >= NDADDR) {
1689 /* allocating an indirect block */
1691 panic("softdep_setup_allocdirect: non-zero indir");
1694 * Allocating a direct block.
1696 * If we are allocating a directory block, then we must
1697 * allocate an associated pagedep to track additions and
1700 if ((ip->i_mode & IFMT) == IFDIR &&
1701 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1702 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1704 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1705 panic("softdep_setup_allocdirect: lost block");
1706 if (newblk->nb_state == DEPCOMPLETE) {
1707 adp->ad_state |= DEPCOMPLETE;
1710 bmsafemap = newblk->nb_bmsafemap;
1711 adp->ad_buf = bmsafemap->sm_buf;
1712 LIST_REMOVE(newblk, nb_deps);
1713 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1715 LIST_REMOVE(newblk, nb_hash);
1716 FREE(newblk, M_NEWBLK);
1718 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1719 adp->ad_inodedep = inodedep;
1720 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1722 * The list of allocdirects must be kept in sorted and ascending
1723 * order so that the rollback routines can quickly determine the
1724 * first uncommitted block (the size of the file stored on disk
1725 * ends at the end of the lowest committed fragment, or if there
1726 * are no fragments, at the end of the highest committed block).
1727 * Since files generally grow, the typical case is that the new
1728 * block is to be added at the end of the list. We speed this
1729 * special case by checking against the last allocdirect in the
1730 * list before laboriously traversing the list looking for the
1733 adphead = &inodedep->id_newinoupdt;
1734 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1735 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1736 /* insert at end of list */
1737 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1738 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1739 allocdirect_merge(adphead, adp, oldadp);
1743 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1744 if (oldadp->ad_lbn >= lbn)
1748 panic("softdep_setup_allocdirect: lost entry");
1749 /* insert in middle of list */
1750 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1751 if (oldadp->ad_lbn == lbn)
1752 allocdirect_merge(adphead, adp, oldadp);
1757 * Replace an old allocdirect dependency with a newer one.
1758 * This routine must be called with splbio interrupts blocked.
1761 allocdirect_merge(adphead, newadp, oldadp)
1762 struct allocdirectlst *adphead; /* head of list holding allocdirects */
1763 struct allocdirect *newadp; /* allocdirect being added */
1764 struct allocdirect *oldadp; /* existing allocdirect being checked */
1766 struct worklist *wk;
1767 struct freefrag *freefrag;
1768 struct newdirblk *newdirblk;
1770 mtx_assert(&lk, MA_OWNED);
1771 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1772 newadp->ad_oldsize != oldadp->ad_newsize ||
1773 newadp->ad_lbn >= NDADDR)
1774 panic("%s %jd != new %jd || old size %ld != new %ld",
1775 "allocdirect_merge: old blkno",
1776 (intmax_t)newadp->ad_oldblkno,
1777 (intmax_t)oldadp->ad_newblkno,
1778 newadp->ad_oldsize, oldadp->ad_newsize);
1779 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1780 newadp->ad_oldsize = oldadp->ad_oldsize;
1782 * If the old dependency had a fragment to free or had never
1783 * previously had a block allocated, then the new dependency
1784 * can immediately post its freefrag and adopt the old freefrag.
1785 * This action is done by swapping the freefrag dependencies.
1786 * The new dependency gains the old one's freefrag, and the
1787 * old one gets the new one and then immediately puts it on
1788 * the worklist when it is freed by free_allocdirect. It is
1789 * not possible to do this swap when the old dependency had a
1790 * non-zero size but no previous fragment to free. This condition
1791 * arises when the new block is an extension of the old block.
1792 * Here, the first part of the fragment allocated to the new
1793 * dependency is part of the block currently claimed on disk by
1794 * the old dependency, so cannot legitimately be freed until the
1795 * conditions for the new dependency are fulfilled.
1797 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1798 freefrag = newadp->ad_freefrag;
1799 newadp->ad_freefrag = oldadp->ad_freefrag;
1800 oldadp->ad_freefrag = freefrag;
1803 * If we are tracking a new directory-block allocation,
1804 * move it from the old allocdirect to the new allocdirect.
1806 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
1807 newdirblk = WK_NEWDIRBLK(wk);
1808 WORKLIST_REMOVE(&newdirblk->db_list);
1809 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
1810 panic("allocdirect_merge: extra newdirblk");
1811 WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list);
1813 free_allocdirect(adphead, oldadp, 0);
1817 * Allocate a new freefrag structure if needed.
1819 static struct freefrag *
1820 newfreefrag(ip, blkno, size)
1825 struct freefrag *freefrag;
1831 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1832 panic("newfreefrag: frag size");
1833 MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1834 M_FREEFRAG, M_SOFTDEP_FLAGS);
1835 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump));
1836 freefrag->ff_inum = ip->i_number;
1837 freefrag->ff_blkno = blkno;
1838 freefrag->ff_fragsize = size;
1843 * This workitem de-allocates fragments that were replaced during
1844 * file block allocation.
1847 handle_workitem_freefrag(freefrag)
1848 struct freefrag *freefrag;
1850 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
1852 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
1853 freefrag->ff_fragsize, freefrag->ff_inum);
1855 WORKITEM_FREE(freefrag, D_FREEFRAG);
1860 * Set up a dependency structure for an external attributes data block.
1861 * This routine follows much of the structure of softdep_setup_allocdirect.
1862 * See the description of softdep_setup_allocdirect above for details.
1865 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1868 ufs2_daddr_t newblkno;
1869 ufs2_daddr_t oldblkno;
1874 struct allocdirect *adp, *oldadp;
1875 struct allocdirectlst *adphead;
1876 struct bmsafemap *bmsafemap;
1877 struct inodedep *inodedep;
1878 struct newblk *newblk;
1881 mp = UFSTOVFS(ip->i_ump);
1882 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1883 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1884 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1886 adp->ad_newblkno = newblkno;
1887 adp->ad_oldblkno = oldblkno;
1888 adp->ad_newsize = newsize;
1889 adp->ad_oldsize = oldsize;
1890 adp->ad_state = ATTACHED | EXTDATA;
1891 LIST_INIT(&adp->ad_newdirblk);
1892 if (newblkno == oldblkno)
1893 adp->ad_freefrag = NULL;
1895 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1898 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1899 panic("softdep_setup_allocext: lost block");
1901 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1902 adp->ad_inodedep = inodedep;
1904 if (newblk->nb_state == DEPCOMPLETE) {
1905 adp->ad_state |= DEPCOMPLETE;
1908 bmsafemap = newblk->nb_bmsafemap;
1909 adp->ad_buf = bmsafemap->sm_buf;
1910 LIST_REMOVE(newblk, nb_deps);
1911 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1913 LIST_REMOVE(newblk, nb_hash);
1914 FREE(newblk, M_NEWBLK);
1916 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1918 panic("softdep_setup_allocext: lbn %lld > NXADDR",
1921 * The list of allocdirects must be kept in sorted and ascending
1922 * order so that the rollback routines can quickly determine the
1923 * first uncommitted block (the size of the file stored on disk
1924 * ends at the end of the lowest committed fragment, or if there
1925 * are no fragments, at the end of the highest committed block).
1926 * Since files generally grow, the typical case is that the new
1927 * block is to be added at the end of the list. We speed this
1928 * special case by checking against the last allocdirect in the
1929 * list before laboriously traversing the list looking for the
1932 adphead = &inodedep->id_newextupdt;
1933 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1934 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1935 /* insert at end of list */
1936 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1937 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1938 allocdirect_merge(adphead, adp, oldadp);
1942 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1943 if (oldadp->ad_lbn >= lbn)
1947 panic("softdep_setup_allocext: lost entry");
1948 /* insert in middle of list */
1949 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1950 if (oldadp->ad_lbn == lbn)
1951 allocdirect_merge(adphead, adp, oldadp);
1956 * Indirect block allocation dependencies.
1958 * The same dependencies that exist for a direct block also exist when
1959 * a new block is allocated and pointed to by an entry in a block of
1960 * indirect pointers. The undo/redo states described above are also
1961 * used here. Because an indirect block contains many pointers that
1962 * may have dependencies, a second copy of the entire in-memory indirect
1963 * block is kept. The buffer cache copy is always completely up-to-date.
1964 * The second copy, which is used only as a source for disk writes,
1965 * contains only the safe pointers (i.e., those that have no remaining
1966 * update dependencies). The second copy is freed when all pointers
1967 * are safe. The cache is not allowed to replace indirect blocks with
1968 * pending update dependencies. If a buffer containing an indirect
1969 * block with dependencies is written, these routines will mark it
1970 * dirty again. It can only be successfully written once all the
1971 * dependencies are removed. The ffs_fsync routine in conjunction with
1972 * softdep_sync_metadata work together to get all the dependencies
1973 * removed so that a file can be successfully written to disk. Three
1974 * procedures are used when setting up indirect block pointer
1975 * dependencies. The division is necessary because of the organization
1976 * of the "balloc" routine and because of the distinction between file
1977 * pages and file metadata blocks.
1981 * Allocate a new allocindir structure.
1983 static struct allocindir *
1984 newallocindir(ip, ptrno, newblkno, oldblkno)
1985 struct inode *ip; /* inode for file being extended */
1986 int ptrno; /* offset of pointer in indirect block */
1987 ufs2_daddr_t newblkno; /* disk block number being added */
1988 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
1990 struct allocindir *aip;
1992 MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1993 M_ALLOCINDIR, M_SOFTDEP_FLAGS|M_ZERO);
1994 workitem_alloc(&aip->ai_list, D_ALLOCINDIR, UFSTOVFS(ip->i_ump));
1995 aip->ai_state = ATTACHED;
1996 aip->ai_offset = ptrno;
1997 aip->ai_newblkno = newblkno;
1998 aip->ai_oldblkno = oldblkno;
1999 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
2004 * Called just before setting an indirect block pointer
2005 * to a newly allocated file page.
2008 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
2009 struct inode *ip; /* inode for file being extended */
2010 ufs_lbn_t lbn; /* allocated block number within file */
2011 struct buf *bp; /* buffer with indirect blk referencing page */
2012 int ptrno; /* offset of pointer in indirect block */
2013 ufs2_daddr_t newblkno; /* disk block number being added */
2014 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
2015 struct buf *nbp; /* buffer holding allocated page */
2017 struct allocindir *aip;
2018 struct pagedep *pagedep;
2020 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
2021 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
2024 * If we are allocating a directory page, then we must
2025 * allocate an associated pagedep to track additions and
2028 if ((ip->i_mode & IFMT) == IFDIR &&
2029 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
2030 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
2031 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2032 setup_allocindir_phase2(bp, ip, aip);
2037 * Called just before setting an indirect block pointer to a
2038 * newly allocated indirect block.
2041 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
2042 struct buf *nbp; /* newly allocated indirect block */
2043 struct inode *ip; /* inode for file being extended */
2044 struct buf *bp; /* indirect block referencing allocated block */
2045 int ptrno; /* offset of pointer in indirect block */
2046 ufs2_daddr_t newblkno; /* disk block number being added */
2048 struct allocindir *aip;
2050 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
2051 aip = newallocindir(ip, ptrno, newblkno, 0);
2053 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2054 setup_allocindir_phase2(bp, ip, aip);
2059 * Called to finish the allocation of the "aip" allocated
2060 * by one of the two routines above.
2063 setup_allocindir_phase2(bp, ip, aip)
2064 struct buf *bp; /* in-memory copy of the indirect block */
2065 struct inode *ip; /* inode for file being extended */
2066 struct allocindir *aip; /* allocindir allocated by the above routines */
2068 struct worklist *wk;
2069 struct indirdep *indirdep, *newindirdep;
2070 struct bmsafemap *bmsafemap;
2071 struct allocindir *oldaip;
2072 struct freefrag *freefrag;
2073 struct newblk *newblk;
2076 mtx_assert(&lk, MA_OWNED);
2077 if (bp->b_lblkno >= 0)
2078 panic("setup_allocindir_phase2: not indir blk");
2079 for (indirdep = NULL, newindirdep = NULL; ; ) {
2080 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2081 if (wk->wk_type != D_INDIRDEP)
2083 indirdep = WK_INDIRDEP(wk);
2086 if (indirdep == NULL && newindirdep) {
2087 indirdep = newindirdep;
2088 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
2092 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
2094 panic("setup_allocindir: lost block");
2095 if (newblk->nb_state == DEPCOMPLETE) {
2096 aip->ai_state |= DEPCOMPLETE;
2099 bmsafemap = newblk->nb_bmsafemap;
2100 aip->ai_buf = bmsafemap->sm_buf;
2101 LIST_REMOVE(newblk, nb_deps);
2102 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
2105 LIST_REMOVE(newblk, nb_hash);
2106 FREE(newblk, M_NEWBLK);
2107 aip->ai_indirdep = indirdep;
2109 * Check to see if there is an existing dependency
2110 * for this block. If there is, merge the old
2111 * dependency into the new one.
2113 if (aip->ai_oldblkno == 0)
2117 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
2118 if (oldaip->ai_offset == aip->ai_offset)
2121 if (oldaip != NULL) {
2122 if (oldaip->ai_newblkno != aip->ai_oldblkno)
2123 panic("setup_allocindir_phase2: blkno");
2124 aip->ai_oldblkno = oldaip->ai_oldblkno;
2125 freefrag = aip->ai_freefrag;
2126 aip->ai_freefrag = oldaip->ai_freefrag;
2127 oldaip->ai_freefrag = NULL;
2128 free_allocindir(oldaip, NULL);
2130 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
2131 if (ip->i_ump->um_fstype == UFS1)
2132 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)
2133 [aip->ai_offset] = aip->ai_oldblkno;
2135 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)
2136 [aip->ai_offset] = aip->ai_oldblkno;
2138 if (freefrag != NULL)
2139 handle_workitem_freefrag(freefrag);
2143 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
2144 brelse(newindirdep->ir_savebp);
2146 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
2155 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
2156 M_INDIRDEP, M_SOFTDEP_FLAGS);
2157 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP,
2158 UFSTOVFS(ip->i_ump));
2159 newindirdep->ir_state = ATTACHED;
2160 if (ip->i_ump->um_fstype == UFS1)
2161 newindirdep->ir_state |= UFS1FMT;
2162 LIST_INIT(&newindirdep->ir_deplisthd);
2163 LIST_INIT(&newindirdep->ir_donehd);
2164 if (bp->b_blkno == bp->b_lblkno) {
2165 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
2167 bp->b_blkno = blkno;
2169 newindirdep->ir_savebp =
2170 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
2171 BUF_KERNPROC(newindirdep->ir_savebp);
2172 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
2178 * Block de-allocation dependencies.
2180 * When blocks are de-allocated, the on-disk pointers must be nullified before
2181 * the blocks are made available for use by other files. (The true
2182 * requirement is that old pointers must be nullified before new on-disk
2183 * pointers are set. We chose this slightly more stringent requirement to
2184 * reduce complexity.) Our implementation handles this dependency by updating
2185 * the inode (or indirect block) appropriately but delaying the actual block
2186 * de-allocation (i.e., freemap and free space count manipulation) until
2187 * after the updated versions reach stable storage. After the disk is
2188 * updated, the blocks can be safely de-allocated whenever it is convenient.
2189 * This implementation handles only the common case of reducing a file's
2190 * length to zero. Other cases are handled by the conventional synchronous
2193 * The ffs implementation with which we worked double-checks
2194 * the state of the block pointers and file size as it reduces
2195 * a file's length. Some of this code is replicated here in our
2196 * soft updates implementation. The freeblks->fb_chkcnt field is
2197 * used to transfer a part of this information to the procedure
2198 * that eventually de-allocates the blocks.
2200 * This routine should be called from the routine that shortens
2201 * a file's length, before the inode's size or block pointers
2202 * are modified. It will save the block pointer information for
2203 * later release and zero the inode so that the calling routine
2207 softdep_setup_freeblocks(ip, length, flags)
2208 struct inode *ip; /* The inode whose length is to be reduced */
2209 off_t length; /* The new length for the file */
2210 int flags; /* IO_EXT and/or IO_NORMAL */
2212 struct freeblks *freeblks;
2213 struct inodedep *inodedep;
2214 struct allocdirect *adp;
2218 ufs2_daddr_t extblocks, datablocks;
2220 int i, delay, error;
2223 mp = UFSTOVFS(ip->i_ump);
2225 panic("softdep_setup_freeblocks: non-zero length");
2226 MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
2227 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
2228 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
2229 freeblks->fb_state = ATTACHED;
2230 freeblks->fb_uid = ip->i_uid;
2231 freeblks->fb_previousinum = ip->i_number;
2232 freeblks->fb_devvp = ip->i_devvp;
2234 if (fs->fs_magic == FS_UFS2_MAGIC)
2235 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
2236 datablocks = DIP(ip, i_blocks) - extblocks;
2237 if ((flags & IO_NORMAL) == 0) {
2238 freeblks->fb_oldsize = 0;
2239 freeblks->fb_chkcnt = 0;
2241 freeblks->fb_oldsize = ip->i_size;
2243 DIP_SET(ip, i_size, 0);
2244 freeblks->fb_chkcnt = datablocks;
2245 for (i = 0; i < NDADDR; i++) {
2246 freeblks->fb_dblks[i] = DIP(ip, i_db[i]);
2247 DIP_SET(ip, i_db[i], 0);
2249 for (i = 0; i < NIADDR; i++) {
2250 freeblks->fb_iblks[i] = DIP(ip, i_ib[i]);
2251 DIP_SET(ip, i_ib[i], 0);
2254 * If the file was removed, then the space being freed was
2255 * accounted for then (see softdep_releasefile()). If the
2256 * file is merely being truncated, then we account for it now.
2258 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2259 UFS_LOCK(ip->i_ump);
2260 fs->fs_pendingblocks += datablocks;
2261 UFS_UNLOCK(ip->i_ump);
2264 if ((flags & IO_EXT) == 0) {
2265 freeblks->fb_oldextsize = 0;
2267 freeblks->fb_oldextsize = ip->i_din2->di_extsize;
2268 ip->i_din2->di_extsize = 0;
2269 freeblks->fb_chkcnt += extblocks;
2270 for (i = 0; i < NXADDR; i++) {
2271 freeblks->fb_eblks[i] = ip->i_din2->di_extb[i];
2272 ip->i_din2->di_extb[i] = 0;
2275 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt);
2277 * Push the zero'ed inode to to its disk buffer so that we are free
2278 * to delete its dependencies below. Once the dependencies are gone
2279 * the buffer can be safely released.
2281 if ((error = bread(ip->i_devvp,
2282 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
2283 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
2285 softdep_error("softdep_setup_freeblocks", error);
2287 if (ip->i_ump->um_fstype == UFS1)
2288 *((struct ufs1_dinode *)bp->b_data +
2289 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
2291 *((struct ufs2_dinode *)bp->b_data +
2292 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
2294 * Find and eliminate any inode dependencies.
2297 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
2298 if ((inodedep->id_state & IOSTARTED) != 0)
2299 panic("softdep_setup_freeblocks: inode busy");
2301 * Add the freeblks structure to the list of operations that
2302 * must await the zero'ed inode being written to disk. If we
2303 * still have a bitmap dependency (delay == 0), then the inode
2304 * has never been written to disk, so we can process the
2305 * freeblks below once we have deleted the dependencies.
2307 delay = (inodedep->id_state & DEPCOMPLETE);
2309 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
2311 * Because the file length has been truncated to zero, any
2312 * pending block allocation dependency structures associated
2313 * with this inode are obsolete and can simply be de-allocated.
2314 * We must first merge the two dependency lists to get rid of
2315 * any duplicate freefrag structures, then purge the merged list.
2316 * If we still have a bitmap dependency, then the inode has never
2317 * been written to disk, so we can free any fragments without delay.
2319 if (flags & IO_NORMAL) {
2320 merge_inode_lists(&inodedep->id_newinoupdt,
2321 &inodedep->id_inoupdt);
2322 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
2323 free_allocdirect(&inodedep->id_inoupdt, adp, delay);
2325 if (flags & IO_EXT) {
2326 merge_inode_lists(&inodedep->id_newextupdt,
2327 &inodedep->id_extupdt);
2328 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
2329 free_allocdirect(&inodedep->id_extupdt, adp, delay);
2334 * We must wait for any I/O in progress to finish so that
2335 * all potential buffers on the dirty list will be visible.
2336 * Once they are all there, walk the list and get rid of
2343 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
2344 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
2345 ((flags & IO_NORMAL) == 0 &&
2346 (bp->b_xflags & BX_ALTDATA) == 0))
2348 if ((bp = getdirtybuf(bp, VI_MTX(vp), MNT_WAIT)) == NULL)
2352 (void) inodedep_lookup(mp, ip->i_number, 0, &inodedep);
2353 deallocate_dependencies(bp, inodedep);
2355 bp->b_flags |= B_INVAL | B_NOCACHE;
2362 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
2363 (void) free_inodedep(inodedep);
2366 freeblks->fb_state |= DEPCOMPLETE;
2368 * If the inode with zeroed block pointers is now on disk
2369 * we can start freeing blocks. Add freeblks to the worklist
2370 * instead of calling handle_workitem_freeblocks directly as
2371 * it is more likely that additional IO is needed to complete
2372 * the request here than in the !delay case.
2374 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
2375 add_to_worklist(&freeblks->fb_list);
2380 * If the inode has never been written to disk (delay == 0),
2381 * then we can process the freeblks now that we have deleted
2385 handle_workitem_freeblocks(freeblks, 0);
2389 * Reclaim any dependency structures from a buffer that is about to
2390 * be reallocated to a new vnode. The buffer must be locked, thus,
2391 * no I/O completion operations can occur while we are manipulating
2392 * its associated dependencies. The mutex is held so that other I/O's
2393 * associated with related dependencies do not occur.
2396 deallocate_dependencies(bp, inodedep)
2398 struct inodedep *inodedep;
2400 struct worklist *wk;
2401 struct indirdep *indirdep;
2402 struct allocindir *aip;
2403 struct pagedep *pagedep;
2404 struct dirrem *dirrem;
2408 mtx_assert(&lk, MA_OWNED);
2409 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2410 switch (wk->wk_type) {
2413 indirdep = WK_INDIRDEP(wk);
2415 * None of the indirect pointers will ever be visible,
2416 * so they can simply be tossed. GOINGAWAY ensures
2417 * that allocated pointers will be saved in the buffer
2418 * cache until they are freed. Note that they will
2419 * only be able to be found by their physical address
2420 * since the inode mapping the logical address will
2421 * be gone. The save buffer used for the safe copy
2422 * was allocated in setup_allocindir_phase2 using
2423 * the physical address so it could be used for this
2424 * purpose. Hence we swap the safe copy with the real
2425 * copy, allowing the safe copy to be freed and holding
2426 * on to the real copy for later use in indir_trunc.
2428 if (indirdep->ir_state & GOINGAWAY)
2429 panic("deallocate_dependencies: already gone");
2430 indirdep->ir_state |= GOINGAWAY;
2431 VFSTOUFS(bp->b_vp->v_mount)->um_numindirdeps += 1;
2432 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
2433 free_allocindir(aip, inodedep);
2434 if (bp->b_lblkno >= 0 ||
2435 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
2436 panic("deallocate_dependencies: not indir");
2437 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
2439 WORKLIST_REMOVE(wk);
2440 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
2444 pagedep = WK_PAGEDEP(wk);
2446 * None of the directory additions will ever be
2447 * visible, so they can simply be tossed.
2449 for (i = 0; i < DAHASHSZ; i++)
2451 LIST_FIRST(&pagedep->pd_diraddhd[i])))
2453 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
2456 * Copy any directory remove dependencies to the list
2457 * to be processed after the zero'ed inode is written.
2458 * If the inode has already been written, then they
2459 * can be dumped directly onto the work list.
2461 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
2462 LIST_REMOVE(dirrem, dm_next);
2463 dirrem->dm_dirinum = pagedep->pd_ino;
2464 if (inodedep == NULL ||
2465 (inodedep->id_state & ALLCOMPLETE) ==
2467 add_to_worklist(&dirrem->dm_list);
2469 WORKLIST_INSERT(&inodedep->id_bufwait,
2472 if ((pagedep->pd_state & NEWBLOCK) != 0) {
2473 LIST_FOREACH(wk, &inodedep->id_bufwait, wk_list)
2474 if (wk->wk_type == D_NEWDIRBLK &&
2475 WK_NEWDIRBLK(wk)->db_pagedep ==
2479 WORKLIST_REMOVE(wk);
2480 free_newdirblk(WK_NEWDIRBLK(wk));
2482 panic("deallocate_dependencies: "
2485 WORKLIST_REMOVE(&pagedep->pd_list);
2486 LIST_REMOVE(pagedep, pd_hash);
2487 WORKITEM_FREE(pagedep, D_PAGEDEP);
2491 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
2496 panic("deallocate_dependencies: Unexpected type %s",
2497 TYPENAME(wk->wk_type));
2501 panic("deallocate_dependencies: Unknown type %s",
2502 TYPENAME(wk->wk_type));
2509 * Free an allocdirect. Generate a new freefrag work request if appropriate.
2510 * This routine must be called with splbio interrupts blocked.
2513 free_allocdirect(adphead, adp, delay)
2514 struct allocdirectlst *adphead;
2515 struct allocdirect *adp;
2518 struct newdirblk *newdirblk;
2519 struct worklist *wk;
2521 mtx_assert(&lk, MA_OWNED);
2522 if ((adp->ad_state & DEPCOMPLETE) == 0)
2523 LIST_REMOVE(adp, ad_deps);
2524 TAILQ_REMOVE(adphead, adp, ad_next);
2525 if ((adp->ad_state & COMPLETE) == 0)
2526 WORKLIST_REMOVE(&adp->ad_list);
2527 if (adp->ad_freefrag != NULL) {
2529 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2530 &adp->ad_freefrag->ff_list);
2532 add_to_worklist(&adp->ad_freefrag->ff_list);
2534 if ((wk = LIST_FIRST(&adp->ad_newdirblk)) != NULL) {
2535 newdirblk = WK_NEWDIRBLK(wk);
2536 WORKLIST_REMOVE(&newdirblk->db_list);
2537 if (!LIST_EMPTY(&adp->ad_newdirblk))
2538 panic("free_allocdirect: extra newdirblk");
2540 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2541 &newdirblk->db_list);
2543 free_newdirblk(newdirblk);
2545 WORKITEM_FREE(adp, D_ALLOCDIRECT);
2549 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
2550 * This routine must be called with splbio interrupts blocked.
2553 free_newdirblk(newdirblk)
2554 struct newdirblk *newdirblk;
2556 struct pagedep *pagedep;
2560 mtx_assert(&lk, MA_OWNED);
2562 * If the pagedep is still linked onto the directory buffer
2563 * dependency chain, then some of the entries on the
2564 * pd_pendinghd list may not be committed to disk yet. In
2565 * this case, we will simply clear the NEWBLOCK flag and
2566 * let the pd_pendinghd list be processed when the pagedep
2567 * is next written. If the pagedep is no longer on the buffer
2568 * dependency chain, then all the entries on the pd_pending
2569 * list are committed to disk and we can free them here.
2571 pagedep = newdirblk->db_pagedep;
2572 pagedep->pd_state &= ~NEWBLOCK;
2573 if ((pagedep->pd_state & ONWORKLIST) == 0)
2574 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
2577 * If no dependencies remain, the pagedep will be freed.
2579 for (i = 0; i < DAHASHSZ; i++)
2580 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
2582 if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0) {
2583 LIST_REMOVE(pagedep, pd_hash);
2584 WORKITEM_FREE(pagedep, D_PAGEDEP);
2586 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2590 * Prepare an inode to be freed. The actual free operation is not
2591 * done until the zero'ed inode has been written to disk.
2594 softdep_freefile(pvp, ino, mode)
2599 struct inode *ip = VTOI(pvp);
2600 struct inodedep *inodedep;
2601 struct freefile *freefile;
2604 * This sets up the inode de-allocation dependency.
2606 MALLOC(freefile, struct freefile *, sizeof(struct freefile),
2607 M_FREEFILE, M_SOFTDEP_FLAGS);
2608 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
2609 freefile->fx_mode = mode;
2610 freefile->fx_oldinum = ino;
2611 freefile->fx_devvp = ip->i_devvp;
2612 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2613 UFS_LOCK(ip->i_ump);
2614 ip->i_fs->fs_pendinginodes += 1;
2615 UFS_UNLOCK(ip->i_ump);
2619 * If the inodedep does not exist, then the zero'ed inode has
2620 * been written to disk. If the allocated inode has never been
2621 * written to disk, then the on-disk inode is zero'ed. In either
2622 * case we can free the file immediately.
2625 if (inodedep_lookup(pvp->v_mount, ino, 0, &inodedep) == 0 ||
2626 check_inode_unwritten(inodedep)) {
2628 handle_workitem_freefile(freefile);
2631 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2633 if (ip->i_number == ino)
2634 ip->i_flag |= IN_MODIFIED;
2638 * Check to see if an inode has never been written to disk. If
2639 * so free the inodedep and return success, otherwise return failure.
2640 * This routine must be called with splbio interrupts blocked.
2642 * If we still have a bitmap dependency, then the inode has never
2643 * been written to disk. Drop the dependency as it is no longer
2644 * necessary since the inode is being deallocated. We set the
2645 * ALLCOMPLETE flags since the bitmap now properly shows that the
2646 * inode is not allocated. Even if the inode is actively being
2647 * written, it has been rolled back to its zero'ed state, so we
2648 * are ensured that a zero inode is what is on the disk. For short
2649 * lived files, this change will usually result in removing all the
2650 * dependencies from the inode so that it can be freed immediately.
2653 check_inode_unwritten(inodedep)
2654 struct inodedep *inodedep;
2657 mtx_assert(&lk, MA_OWNED);
2658 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2659 !LIST_EMPTY(&inodedep->id_pendinghd) ||
2660 !LIST_EMPTY(&inodedep->id_bufwait) ||
2661 !LIST_EMPTY(&inodedep->id_inowait) ||
2662 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2663 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2664 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2665 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2666 inodedep->id_nlinkdelta != 0)
2670 * Another process might be in initiate_write_inodeblock_ufs[12]
2671 * trying to allocate memory without holding "Softdep Lock".
2673 if ((inodedep->id_state & IOSTARTED) != 0 &&
2674 inodedep->id_savedino1 == NULL)
2677 inodedep->id_state |= ALLCOMPLETE;
2678 LIST_REMOVE(inodedep, id_deps);
2679 inodedep->id_buf = NULL;
2680 if (inodedep->id_state & ONWORKLIST)
2681 WORKLIST_REMOVE(&inodedep->id_list);
2682 if (inodedep->id_savedino1 != NULL) {
2683 FREE(inodedep->id_savedino1, M_SAVEDINO);
2684 inodedep->id_savedino1 = NULL;
2686 if (free_inodedep(inodedep) == 0)
2687 panic("check_inode_unwritten: busy inode");
2692 * Try to free an inodedep structure. Return 1 if it could be freed.
2695 free_inodedep(inodedep)
2696 struct inodedep *inodedep;
2699 mtx_assert(&lk, MA_OWNED);
2700 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2701 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2702 !LIST_EMPTY(&inodedep->id_pendinghd) ||
2703 !LIST_EMPTY(&inodedep->id_bufwait) ||
2704 !LIST_EMPTY(&inodedep->id_inowait) ||
2705 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2706 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2707 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2708 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2709 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino1 != NULL)
2711 LIST_REMOVE(inodedep, id_hash);
2712 WORKITEM_FREE(inodedep, D_INODEDEP);
2718 * This workitem routine performs the block de-allocation.
2719 * The workitem is added to the pending list after the updated
2720 * inode block has been written to disk. As mentioned above,
2721 * checks regarding the number of blocks de-allocated (compared
2722 * to the number of blocks allocated for the file) are also
2723 * performed in this function.
2726 handle_workitem_freeblocks(freeblks, flags)
2727 struct freeblks *freeblks;
2733 struct ufsmount *ump;
2734 int i, nblocks, level, bsize;
2735 ufs2_daddr_t bn, blocksreleased = 0;
2736 int error, allerror = 0;
2737 ufs_lbn_t baselbns[NIADDR], tmpval;
2738 int fs_pendingblocks;
2740 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2742 fs_pendingblocks = 0;
2744 baselbns[0] = NDADDR;
2745 for (i = 1; i < NIADDR; i++) {
2746 tmpval *= NINDIR(fs);
2747 baselbns[i] = baselbns[i - 1] + tmpval;
2749 nblocks = btodb(fs->fs_bsize);
2752 * Release all extended attribute blocks or frags.
2754 if (freeblks->fb_oldextsize > 0) {
2755 for (i = (NXADDR - 1); i >= 0; i--) {
2756 if ((bn = freeblks->fb_eblks[i]) == 0)
2758 bsize = sblksize(fs, freeblks->fb_oldextsize, i);
2759 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2760 freeblks->fb_previousinum);
2761 blocksreleased += btodb(bsize);
2765 * Release all data blocks or frags.
2767 if (freeblks->fb_oldsize > 0) {
2769 * Indirect blocks first.
2771 for (level = (NIADDR - 1); level >= 0; level--) {
2772 if ((bn = freeblks->fb_iblks[level]) == 0)
2774 if ((error = indir_trunc(freeblks, fsbtodb(fs, bn),
2775 level, baselbns[level], &blocksreleased)) != 0)
2777 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn,
2778 fs->fs_bsize, freeblks->fb_previousinum);
2779 fs_pendingblocks += nblocks;
2780 blocksreleased += nblocks;
2783 * All direct blocks or frags.
2785 for (i = (NDADDR - 1); i >= 0; i--) {
2786 if ((bn = freeblks->fb_dblks[i]) == 0)
2788 bsize = sblksize(fs, freeblks->fb_oldsize, i);
2789 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2790 freeblks->fb_previousinum);
2791 fs_pendingblocks += btodb(bsize);
2792 blocksreleased += btodb(bsize);
2796 fs->fs_pendingblocks -= fs_pendingblocks;
2799 * If we still have not finished background cleanup, then check
2800 * to see if the block count needs to be adjusted.
2802 if (freeblks->fb_chkcnt != blocksreleased &&
2803 (fs->fs_flags & FS_UNCLEAN) != 0 &&
2804 ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_previousinum,
2805 (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ)
2808 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + \
2809 freeblks->fb_chkcnt - blocksreleased);
2810 ip->i_flag |= IN_CHANGE;
2815 if (freeblks->fb_chkcnt != blocksreleased &&
2816 ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0))
2817 printf("handle_workitem_freeblocks: block count\n");
2819 softdep_error("handle_workitem_freeblks", allerror);
2820 #endif /* INVARIANTS */
2823 WORKITEM_FREE(freeblks, D_FREEBLKS);
2828 * Release blocks associated with the inode ip and stored in the indirect
2829 * block dbn. If level is greater than SINGLE, the block is an indirect block
2830 * and recursive calls to indirtrunc must be used to cleanse other indirect
2834 indir_trunc(freeblks, dbn, level, lbn, countp)
2835 struct freeblks *freeblks;
2839 ufs2_daddr_t *countp;
2843 struct worklist *wk;
2844 struct indirdep *indirdep;
2845 struct ufsmount *ump;
2846 ufs1_daddr_t *bap1 = 0;
2847 ufs2_daddr_t nb, *bap2 = 0;
2849 int i, nblocks, ufs1fmt;
2850 int error, allerror = 0;
2851 int fs_pendingblocks;
2853 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2855 fs_pendingblocks = 0;
2857 for (i = level; i > 0; i--)
2858 lbnadd *= NINDIR(fs);
2860 * Get buffer of block pointers to be freed. This routine is not
2861 * called until the zero'ed inode has been written, so it is safe
2862 * to free blocks as they are encountered. Because the inode has
2863 * been zero'ed, calls to bmap on these blocks will fail. So, we
2864 * have to use the on-disk address and the block device for the
2865 * filesystem to look them up. If the file was deleted before its
2866 * indirect blocks were all written to disk, the routine that set
2867 * us up (deallocate_dependencies) will have arranged to leave
2868 * a complete copy of the indirect block in memory for our use.
2869 * Otherwise we have to read the blocks in from the disk.
2872 bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0,
2875 bp = incore(&freeblks->fb_devvp->v_bufobj, dbn);
2878 if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2879 if (wk->wk_type != D_INDIRDEP ||
2880 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2881 (indirdep->ir_state & GOINGAWAY) == 0)
2882 panic("indir_trunc: lost indirdep");
2883 WORKLIST_REMOVE(wk);
2884 WORKITEM_FREE(indirdep, D_INDIRDEP);
2885 if (!LIST_EMPTY(&bp->b_dep))
2886 panic("indir_trunc: dangling dep");
2887 ump->um_numindirdeps -= 1;
2895 error = bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
2903 * Recursively free indirect blocks.
2905 if (ump->um_fstype == UFS1) {
2907 bap1 = (ufs1_daddr_t *)bp->b_data;
2910 bap2 = (ufs2_daddr_t *)bp->b_data;
2912 nblocks = btodb(fs->fs_bsize);
2913 for (i = NINDIR(fs) - 1; i >= 0; i--) {
2921 if ((error = indir_trunc(freeblks, fsbtodb(fs, nb),
2922 level - 1, lbn + (i * lbnadd), countp)) != 0)
2925 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, fs->fs_bsize,
2926 freeblks->fb_previousinum);
2927 fs_pendingblocks += nblocks;
2931 fs->fs_pendingblocks -= fs_pendingblocks;
2933 bp->b_flags |= B_INVAL | B_NOCACHE;
2939 * Free an allocindir.
2940 * This routine must be called with splbio interrupts blocked.
2943 free_allocindir(aip, inodedep)
2944 struct allocindir *aip;
2945 struct inodedep *inodedep;
2947 struct freefrag *freefrag;
2949 mtx_assert(&lk, MA_OWNED);
2950 if ((aip->ai_state & DEPCOMPLETE) == 0)
2951 LIST_REMOVE(aip, ai_deps);
2952 if (aip->ai_state & ONWORKLIST)
2953 WORKLIST_REMOVE(&aip->ai_list);
2954 LIST_REMOVE(aip, ai_next);
2955 if ((freefrag = aip->ai_freefrag) != NULL) {
2956 if (inodedep == NULL)
2957 add_to_worklist(&freefrag->ff_list);
2959 WORKLIST_INSERT(&inodedep->id_bufwait,
2960 &freefrag->ff_list);
2962 WORKITEM_FREE(aip, D_ALLOCINDIR);
2966 * Directory entry addition dependencies.
2968 * When adding a new directory entry, the inode (with its incremented link
2969 * count) must be written to disk before the directory entry's pointer to it.
2970 * Also, if the inode is newly allocated, the corresponding freemap must be
2971 * updated (on disk) before the directory entry's pointer. These requirements
2972 * are met via undo/redo on the directory entry's pointer, which consists
2973 * simply of the inode number.
2975 * As directory entries are added and deleted, the free space within a
2976 * directory block can become fragmented. The ufs filesystem will compact
2977 * a fragmented directory block to make space for a new entry. When this
2978 * occurs, the offsets of previously added entries change. Any "diradd"
2979 * dependency structures corresponding to these entries must be updated with
2984 * This routine is called after the in-memory inode's link
2985 * count has been incremented, but before the directory entry's
2986 * pointer to the inode has been set.
2989 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
2990 struct buf *bp; /* buffer containing directory block */
2991 struct inode *dp; /* inode for directory */
2992 off_t diroffset; /* offset of new entry in directory */
2993 ino_t newinum; /* inode referenced by new directory entry */
2994 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
2995 int isnewblk; /* entry is in a newly allocated block */
2997 int offset; /* offset of new entry within directory block */
2998 ufs_lbn_t lbn; /* block in directory containing new entry */
3001 struct allocdirect *adp;
3002 struct pagedep *pagedep;
3003 struct inodedep *inodedep;
3004 struct newdirblk *newdirblk = 0;
3005 struct mkdir *mkdir1, *mkdir2;
3009 * Whiteouts have no dependencies.
3011 if (newinum == WINO) {
3012 if (newdirbp != NULL)
3016 mp = UFSTOVFS(dp->i_ump);
3018 lbn = lblkno(fs, diroffset);
3019 offset = blkoff(fs, diroffset);
3020 MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
3021 M_SOFTDEP_FLAGS|M_ZERO);
3022 workitem_alloc(&dap->da_list, D_DIRADD, mp);
3023 dap->da_offset = offset;
3024 dap->da_newinum = newinum;
3025 dap->da_state = ATTACHED;
3026 if (isnewblk && lbn < NDADDR && fragoff(fs, diroffset) == 0) {
3027 MALLOC(newdirblk, struct newdirblk *, sizeof(struct newdirblk),
3028 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
3029 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
3031 if (newdirbp == NULL) {
3032 dap->da_state |= DEPCOMPLETE;
3035 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
3036 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
3038 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
3039 mkdir1->md_state = MKDIR_BODY;
3040 mkdir1->md_diradd = dap;
3041 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
3043 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
3044 mkdir2->md_state = MKDIR_PARENT;
3045 mkdir2->md_diradd = dap;
3047 * Dependency on "." and ".." being written to disk.
3049 mkdir1->md_buf = newdirbp;
3051 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
3052 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
3056 * Dependency on link count increase for parent directory
3059 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0
3060 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3061 dap->da_state &= ~MKDIR_PARENT;
3062 WORKITEM_FREE(mkdir2, D_MKDIR);
3064 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
3065 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
3069 * Link into parent directory pagedep to await its being written.
3071 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3072 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3073 dap->da_pagedep = pagedep;
3074 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
3077 * Link into its inodedep. Put it on the id_bufwait list if the inode
3078 * is not yet written. If it is written, do the post-inode write
3079 * processing to put it on the id_pendinghd list.
3081 (void) inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
3082 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
3083 diradd_inode_written(dap, inodedep);
3085 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3088 * Directories growing into indirect blocks are rare
3089 * enough and the frequency of new block allocation
3090 * in those cases even more rare, that we choose not
3091 * to bother tracking them. Rather we simply force the
3092 * new directory entry to disk.
3094 if (lbn >= NDADDR) {
3097 * We only have a new allocation when at the
3098 * beginning of a new block, not when we are
3099 * expanding into an existing block.
3101 if (blkoff(fs, diroffset) == 0)
3106 * We only have a new allocation when at the beginning
3107 * of a new fragment, not when we are expanding into an
3108 * existing fragment. Also, there is nothing to do if we
3109 * are already tracking this block.
3111 if (fragoff(fs, diroffset) != 0) {
3115 if ((pagedep->pd_state & NEWBLOCK) != 0) {
3116 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
3121 * Find our associated allocdirect and have it track us.
3123 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0)
3124 panic("softdep_setup_directory_add: lost inodedep");
3125 adp = TAILQ_LAST(&inodedep->id_newinoupdt, allocdirectlst);
3126 if (adp == NULL || adp->ad_lbn != lbn)
3127 panic("softdep_setup_directory_add: lost entry");
3128 pagedep->pd_state |= NEWBLOCK;
3129 newdirblk->db_pagedep = pagedep;
3130 WORKLIST_INSERT(&adp->ad_newdirblk, &newdirblk->db_list);
3137 * This procedure is called to change the offset of a directory
3138 * entry when compacting a directory block which must be owned
3139 * exclusively by the caller. Note that the actual entry movement
3140 * must be done in this procedure to ensure that no I/O completions
3141 * occur while the move is in progress.
3144 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
3145 struct inode *dp; /* inode for directory */
3146 caddr_t base; /* address of dp->i_offset */
3147 caddr_t oldloc; /* address of old directory location */
3148 caddr_t newloc; /* address of new directory location */
3149 int entrysize; /* size of directory entry */
3151 int offset, oldoffset, newoffset;
3152 struct pagedep *pagedep;
3157 lbn = lblkno(dp->i_fs, dp->i_offset);
3158 offset = blkoff(dp->i_fs, dp->i_offset);
3159 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
3161 oldoffset = offset + (oldloc - base);
3162 newoffset = offset + (newloc - base);
3164 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
3165 if (dap->da_offset != oldoffset)
3167 dap->da_offset = newoffset;
3168 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
3170 LIST_REMOVE(dap, da_pdlist);
3171 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
3177 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
3178 if (dap->da_offset == oldoffset) {
3179 dap->da_offset = newoffset;
3185 bcopy(oldloc, newloc, entrysize);
3190 * Free a diradd dependency structure. This routine must be called
3191 * with splbio interrupts blocked.
3197 struct dirrem *dirrem;
3198 struct pagedep *pagedep;
3199 struct inodedep *inodedep;
3200 struct mkdir *mkdir, *nextmd;
3202 mtx_assert(&lk, MA_OWNED);
3203 WORKLIST_REMOVE(&dap->da_list);
3204 LIST_REMOVE(dap, da_pdlist);
3205 if ((dap->da_state & DIRCHG) == 0) {
3206 pagedep = dap->da_pagedep;
3208 dirrem = dap->da_previous;
3209 pagedep = dirrem->dm_pagedep;
3210 dirrem->dm_dirinum = pagedep->pd_ino;
3211 add_to_worklist(&dirrem->dm_list);
3213 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
3215 (void) free_inodedep(inodedep);
3216 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
3217 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
3218 nextmd = LIST_NEXT(mkdir, md_mkdirs);
3219 if (mkdir->md_diradd != dap)
3221 dap->da_state &= ~mkdir->md_state;
3222 WORKLIST_REMOVE(&mkdir->md_list);
3223 LIST_REMOVE(mkdir, md_mkdirs);
3224 WORKITEM_FREE(mkdir, D_MKDIR);
3226 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
3227 panic("free_diradd: unfound ref");
3229 WORKITEM_FREE(dap, D_DIRADD);
3233 * Directory entry removal dependencies.
3235 * When removing a directory entry, the entry's inode pointer must be
3236 * zero'ed on disk before the corresponding inode's link count is decremented
3237 * (possibly freeing the inode for re-use). This dependency is handled by
3238 * updating the directory entry but delaying the inode count reduction until
3239 * after the directory block has been written to disk. After this point, the
3240 * inode count can be decremented whenever it is convenient.
3244 * This routine should be called immediately after removing
3245 * a directory entry. The inode's link count should not be
3246 * decremented by the calling procedure -- the soft updates
3247 * code will do this task when it is safe.
3250 softdep_setup_remove(bp, dp, ip, isrmdir)
3251 struct buf *bp; /* buffer containing directory block */
3252 struct inode *dp; /* inode for the directory being modified */
3253 struct inode *ip; /* inode for directory entry being removed */
3254 int isrmdir; /* indicates if doing RMDIR */
3256 struct dirrem *dirrem, *prevdirrem;
3259 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
3261 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3264 * If the COMPLETE flag is clear, then there were no active
3265 * entries and we want to roll back to a zeroed entry until
3266 * the new inode is committed to disk. If the COMPLETE flag is
3267 * set then we have deleted an entry that never made it to
3268 * disk. If the entry we deleted resulted from a name change,
3269 * then the old name still resides on disk. We cannot delete
3270 * its inode (returned to us in prevdirrem) until the zeroed
3271 * directory entry gets to disk. The new inode has never been
3272 * referenced on the disk, so can be deleted immediately.
3274 if ((dirrem->dm_state & COMPLETE) == 0) {
3275 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
3279 if (prevdirrem != NULL)
3280 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
3281 prevdirrem, dm_next);
3282 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
3284 handle_workitem_remove(dirrem, NULL);
3289 * Allocate a new dirrem if appropriate and return it along with
3290 * its associated pagedep. Called without a lock, returns with lock.
3292 static long num_dirrem; /* number of dirrem allocated */
3293 static struct dirrem *
3294 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
3295 struct buf *bp; /* buffer containing directory block */
3296 struct inode *dp; /* inode for the directory being modified */
3297 struct inode *ip; /* inode for directory entry being removed */
3298 int isrmdir; /* indicates if doing RMDIR */
3299 struct dirrem **prevdirremp; /* previously referenced inode, if any */
3304 struct dirrem *dirrem;
3305 struct pagedep *pagedep;
3308 * Whiteouts have no deletion dependencies.
3311 panic("newdirrem: whiteout");
3313 * If we are over our limit, try to improve the situation.
3314 * Limiting the number of dirrem structures will also limit
3315 * the number of freefile and freeblks structures.
3318 if (num_dirrem > max_softdeps / 2)
3319 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_REMOVE);
3322 MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
3323 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO);
3324 workitem_alloc(&dirrem->dm_list, D_DIRREM, ITOV(dp)->v_mount);
3325 dirrem->dm_state = isrmdir ? RMDIR : 0;
3326 dirrem->dm_oldinum = ip->i_number;
3327 *prevdirremp = NULL;
3330 lbn = lblkno(dp->i_fs, dp->i_offset);
3331 offset = blkoff(dp->i_fs, dp->i_offset);
3332 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3333 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3334 dirrem->dm_pagedep = pagedep;
3336 * Check for a diradd dependency for the same directory entry.
3337 * If present, then both dependencies become obsolete and can
3338 * be de-allocated. Check for an entry on both the pd_dirraddhd
3339 * list and the pd_pendinghd list.
3342 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
3343 if (dap->da_offset == offset)
3347 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
3348 if (dap->da_offset == offset)
3354 * Must be ATTACHED at this point.
3356 if ((dap->da_state & ATTACHED) == 0)
3357 panic("newdirrem: not ATTACHED");
3358 if (dap->da_newinum != ip->i_number)
3359 panic("newdirrem: inum %d should be %d",
3360 ip->i_number, dap->da_newinum);
3362 * If we are deleting a changed name that never made it to disk,
3363 * then return the dirrem describing the previous inode (which
3364 * represents the inode currently referenced from this entry on disk).
3366 if ((dap->da_state & DIRCHG) != 0) {
3367 *prevdirremp = dap->da_previous;
3368 dap->da_state &= ~DIRCHG;
3369 dap->da_pagedep = pagedep;
3372 * We are deleting an entry that never made it to disk.
3373 * Mark it COMPLETE so we can delete its inode immediately.
3375 dirrem->dm_state |= COMPLETE;
3381 * Directory entry change dependencies.
3383 * Changing an existing directory entry requires that an add operation
3384 * be completed first followed by a deletion. The semantics for the addition
3385 * are identical to the description of adding a new entry above except
3386 * that the rollback is to the old inode number rather than zero. Once
3387 * the addition dependency is completed, the removal is done as described
3388 * in the removal routine above.
3392 * This routine should be called immediately after changing
3393 * a directory entry. The inode's link count should not be
3394 * decremented by the calling procedure -- the soft updates
3395 * code will perform this task when it is safe.
3398 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
3399 struct buf *bp; /* buffer containing directory block */
3400 struct inode *dp; /* inode for the directory being modified */
3401 struct inode *ip; /* inode for directory entry being removed */
3402 ino_t newinum; /* new inode number for changed entry */
3403 int isrmdir; /* indicates if doing RMDIR */
3406 struct diradd *dap = NULL;
3407 struct dirrem *dirrem, *prevdirrem;
3408 struct pagedep *pagedep;
3409 struct inodedep *inodedep;
3412 offset = blkoff(dp->i_fs, dp->i_offset);
3413 mp = UFSTOVFS(dp->i_ump);
3416 * Whiteouts do not need diradd dependencies.
3418 if (newinum != WINO) {
3419 MALLOC(dap, struct diradd *, sizeof(struct diradd),
3420 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
3421 workitem_alloc(&dap->da_list, D_DIRADD, mp);
3422 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
3423 dap->da_offset = offset;
3424 dap->da_newinum = newinum;
3428 * Allocate a new dirrem and ACQUIRE_LOCK.
3430 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3431 pagedep = dirrem->dm_pagedep;
3433 * The possible values for isrmdir:
3434 * 0 - non-directory file rename
3435 * 1 - directory rename within same directory
3436 * inum - directory rename to new directory of given inode number
3437 * When renaming to a new directory, we are both deleting and
3438 * creating a new directory entry, so the link count on the new
3439 * directory should not change. Thus we do not need the followup
3440 * dirrem which is usually done in handle_workitem_remove. We set
3441 * the DIRCHG flag to tell handle_workitem_remove to skip the
3445 dirrem->dm_state |= DIRCHG;
3448 * Whiteouts have no additional dependencies,
3449 * so just put the dirrem on the correct list.
3451 if (newinum == WINO) {
3452 if ((dirrem->dm_state & COMPLETE) == 0) {
3453 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
3456 dirrem->dm_dirinum = pagedep->pd_ino;
3457 add_to_worklist(&dirrem->dm_list);
3464 * If the COMPLETE flag is clear, then there were no active
3465 * entries and we want to roll back to the previous inode until
3466 * the new inode is committed to disk. If the COMPLETE flag is
3467 * set, then we have deleted an entry that never made it to disk.
3468 * If the entry we deleted resulted from a name change, then the old
3469 * inode reference still resides on disk. Any rollback that we do
3470 * needs to be to that old inode (returned to us in prevdirrem). If
3471 * the entry we deleted resulted from a create, then there is
3472 * no entry on the disk, so we want to roll back to zero rather
3473 * than the uncommitted inode. In either of the COMPLETE cases we
3474 * want to immediately free the unwritten and unreferenced inode.
3476 if ((dirrem->dm_state & COMPLETE) == 0) {
3477 dap->da_previous = dirrem;
3479 if (prevdirrem != NULL) {
3480 dap->da_previous = prevdirrem;
3482 dap->da_state &= ~DIRCHG;
3483 dap->da_pagedep = pagedep;
3485 dirrem->dm_dirinum = pagedep->pd_ino;
3486 add_to_worklist(&dirrem->dm_list);
3489 * Link into its inodedep. Put it on the id_bufwait list if the inode
3490 * is not yet written. If it is written, do the post-inode write
3491 * processing to put it on the id_pendinghd list.
3493 if (inodedep_lookup(mp, newinum, DEPALLOC, &inodedep) == 0 ||
3494 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3495 dap->da_state |= COMPLETE;
3496 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3497 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3499 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
3501 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3507 * Called whenever the link count on an inode is changed.
3508 * It creates an inode dependency so that the new reference(s)
3509 * to the inode cannot be committed to disk until the updated
3510 * inode has been written.
3513 softdep_change_linkcnt(ip)
3514 struct inode *ip; /* the inode with the increased link count */
3516 struct inodedep *inodedep;
3519 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number,
3520 DEPALLOC, &inodedep);
3521 if (ip->i_nlink < ip->i_effnlink)
3522 panic("softdep_change_linkcnt: bad delta");
3523 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3528 * Called when the effective link count and the reference count
3529 * on an inode drops to zero. At this point there are no names
3530 * referencing the file in the filesystem and no active file
3531 * references. The space associated with the file will be freed
3532 * as soon as the necessary soft dependencies are cleared.
3535 softdep_releasefile(ip)
3536 struct inode *ip; /* inode with the zero effective link count */
3538 struct inodedep *inodedep;
3542 if (ip->i_effnlink > 0)
3543 panic("softdep_releasefile: file still referenced");
3545 * We may be called several times as the on-disk link count
3546 * drops to zero. We only want to account for the space once.
3548 if (ip->i_flag & IN_SPACECOUNTED)
3551 * We have to deactivate a snapshot otherwise copyonwrites may
3552 * add blocks and the cleanup may remove blocks after we have
3553 * tried to account for them.
3555 if ((ip->i_flags & SF_SNAPSHOT) != 0)
3556 ffs_snapremove(ITOV(ip));
3558 * If we are tracking an nlinkdelta, we have to also remember
3559 * whether we accounted for the freed space yet.
3562 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, &inodedep)))
3563 inodedep->id_state |= SPACECOUNTED;
3567 if (fs->fs_magic == FS_UFS2_MAGIC)
3568 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
3569 UFS_LOCK(ip->i_ump);
3570 ip->i_fs->fs_pendingblocks += DIP(ip, i_blocks) - extblocks;
3571 ip->i_fs->fs_pendinginodes += 1;
3572 UFS_UNLOCK(ip->i_ump);
3573 ip->i_flag |= IN_SPACECOUNTED;
3577 * This workitem decrements the inode's link count.
3578 * If the link count reaches zero, the file is removed.
3581 handle_workitem_remove(dirrem, xp)
3582 struct dirrem *dirrem;
3585 struct thread *td = curthread;
3586 struct inodedep *inodedep;
3592 if ((vp = xp) == NULL &&
3593 (error = ffs_vgetf(dirrem->dm_list.wk_mp,
3594 dirrem->dm_oldinum, LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ)) != 0) {
3595 softdep_error("handle_workitem_remove: vget", error);
3600 if ((inodedep_lookup(dirrem->dm_list.wk_mp,
3601 dirrem->dm_oldinum, 0, &inodedep)) == 0)
3602 panic("handle_workitem_remove: lost inodedep");
3604 * Normal file deletion.
3606 if ((dirrem->dm_state & RMDIR) == 0) {
3608 DIP_SET(ip, i_nlink, ip->i_nlink);
3609 ip->i_flag |= IN_CHANGE;
3610 if (ip->i_nlink < ip->i_effnlink)
3611 panic("handle_workitem_remove: bad file delta");
3612 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3614 WORKITEM_FREE(dirrem, D_DIRREM);
3620 * Directory deletion. Decrement reference count for both the
3621 * just deleted parent directory entry and the reference for ".".
3622 * Next truncate the directory to length zero. When the
3623 * truncation completes, arrange to have the reference count on
3624 * the parent decremented to account for the loss of "..".
3627 DIP_SET(ip, i_nlink, ip->i_nlink);
3628 ip->i_flag |= IN_CHANGE;
3629 if (ip->i_nlink < ip->i_effnlink)
3630 panic("handle_workitem_remove: bad dir delta");
3631 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3633 if ((error = ffs_truncate(vp, (off_t)0, 0, td->td_ucred, td)) != 0)
3634 softdep_error("handle_workitem_remove: truncate", error);
3637 * Rename a directory to a new parent. Since, we are both deleting
3638 * and creating a new directory entry, the link count on the new
3639 * directory should not change. Thus we skip the followup dirrem.
3641 if (dirrem->dm_state & DIRCHG) {
3643 WORKITEM_FREE(dirrem, D_DIRREM);
3649 * If the inodedep does not exist, then the zero'ed inode has
3650 * been written to disk. If the allocated inode has never been
3651 * written to disk, then the on-disk inode is zero'ed. In either
3652 * case we can remove the file immediately.
3654 dirrem->dm_state = 0;
3655 oldinum = dirrem->dm_oldinum;
3656 dirrem->dm_oldinum = dirrem->dm_dirinum;
3657 if (inodedep_lookup(dirrem->dm_list.wk_mp, oldinum,
3658 0, &inodedep) == 0 || check_inode_unwritten(inodedep)) {
3660 add_to_worklist(&dirrem->dm_list);
3664 handle_workitem_remove(dirrem, NULL);
3667 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
3669 ip->i_flag |= IN_CHANGE;
3675 * Inode de-allocation dependencies.
3677 * When an inode's link count is reduced to zero, it can be de-allocated. We
3678 * found it convenient to postpone de-allocation until after the inode is
3679 * written to disk with its new link count (zero). At this point, all of the
3680 * on-disk inode's block pointers are nullified and, with careful dependency
3681 * list ordering, all dependencies related to the inode will be satisfied and
3682 * the corresponding dependency structures de-allocated. So, if/when the
3683 * inode is reused, there will be no mixing of old dependencies with new
3684 * ones. This artificial dependency is set up by the block de-allocation
3685 * procedure above (softdep_setup_freeblocks) and completed by the
3686 * following procedure.
3689 handle_workitem_freefile(freefile)
3690 struct freefile *freefile;
3693 struct inodedep *idp;
3694 struct ufsmount *ump;
3697 ump = VFSTOUFS(freefile->fx_list.wk_mp);
3701 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
3704 panic("handle_workitem_freefile: inodedep survived");
3707 fs->fs_pendinginodes -= 1;
3709 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
3710 freefile->fx_oldinum, freefile->fx_mode)) != 0)
3711 softdep_error("handle_workitem_freefile", error);
3713 WORKITEM_FREE(freefile, D_FREEFILE);
3719 * Helper function which unlinks marker element from work list and returns
3720 * the next element on the list.
3722 static __inline struct worklist *
3723 markernext(struct worklist *marker)
3725 struct worklist *next;
3727 next = LIST_NEXT(marker, wk_list);
3728 LIST_REMOVE(marker, wk_list);
3735 * The dependency structures constructed above are most actively used when file
3736 * system blocks are written to disk. No constraints are placed on when a
3737 * block can be written, but unsatisfied update dependencies are made safe by
3738 * modifying (or replacing) the source memory for the duration of the disk
3739 * write. When the disk write completes, the memory block is again brought
3742 * In-core inode structure reclamation.
3744 * Because there are a finite number of "in-core" inode structures, they are
3745 * reused regularly. By transferring all inode-related dependencies to the
3746 * in-memory inode block and indexing them separately (via "inodedep"s), we
3747 * can allow "in-core" inode structures to be reused at any time and avoid
3748 * any increase in contention.
3750 * Called just before entering the device driver to initiate a new disk I/O.
3751 * The buffer must be locked, thus, no I/O completion operations can occur
3752 * while we are manipulating its associated dependencies.
3755 softdep_disk_io_initiation(bp)
3756 struct buf *bp; /* structure describing disk write to occur */
3758 struct worklist *wk;
3759 struct worklist marker;
3760 struct indirdep *indirdep;
3761 struct inodedep *inodedep;
3764 * We only care about write operations. There should never
3765 * be dependencies for reads.
3767 if (bp->b_iocmd != BIO_WRITE)
3768 panic("softdep_disk_io_initiation: not write");
3770 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
3771 PHOLD(curproc); /* Don't swap out kernel stack */
3775 * Do any necessary pre-I/O processing.
3777 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
3778 wk = markernext(&marker)) {
3779 LIST_INSERT_AFTER(wk, &marker, wk_list);
3780 switch (wk->wk_type) {
3783 initiate_write_filepage(WK_PAGEDEP(wk), bp);
3787 inodedep = WK_INODEDEP(wk);
3788 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
3789 initiate_write_inodeblock_ufs1(inodedep, bp);
3791 initiate_write_inodeblock_ufs2(inodedep, bp);
3795 indirdep = WK_INDIRDEP(wk);
3796 if (indirdep->ir_state & GOINGAWAY)
3797 panic("disk_io_initiation: indirdep gone");
3799 * If there are no remaining dependencies, this
3800 * will be writing the real pointers, so the
3801 * dependency can be freed.
3803 if (LIST_EMPTY(&indirdep->ir_deplisthd)) {
3806 bp = indirdep->ir_savebp;
3807 bp->b_flags |= B_INVAL | B_NOCACHE;
3808 /* inline expand WORKLIST_REMOVE(wk); */
3809 wk->wk_state &= ~ONWORKLIST;
3810 LIST_REMOVE(wk, wk_list);
3811 WORKITEM_FREE(indirdep, D_INDIRDEP);
3818 * Replace up-to-date version with safe version.
3821 MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
3822 M_INDIRDEP, M_SOFTDEP_FLAGS);
3824 indirdep->ir_state &= ~ATTACHED;
3825 indirdep->ir_state |= UNDONE;
3826 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3827 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3838 panic("handle_disk_io_initiation: Unexpected type %s",
3839 TYPENAME(wk->wk_type));
3844 PRELE(curproc); /* Allow swapout of kernel stack */
3848 * Called from within the procedure above to deal with unsatisfied
3849 * allocation dependencies in a directory. The buffer must be locked,
3850 * thus, no I/O completion operations can occur while we are
3851 * manipulating its associated dependencies.
3854 initiate_write_filepage(pagedep, bp)
3855 struct pagedep *pagedep;
3862 if (pagedep->pd_state & IOSTARTED) {
3864 * This can only happen if there is a driver that does not
3865 * understand chaining. Here biodone will reissue the call
3866 * to strategy for the incomplete buffers.
3868 printf("initiate_write_filepage: already started\n");
3871 pagedep->pd_state |= IOSTARTED;
3872 for (i = 0; i < DAHASHSZ; i++) {
3873 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3874 ep = (struct direct *)
3875 ((char *)bp->b_data + dap->da_offset);
3876 if (ep->d_ino != dap->da_newinum)
3877 panic("%s: dir inum %d != new %d",
3878 "initiate_write_filepage",
3879 ep->d_ino, dap->da_newinum);
3880 if (dap->da_state & DIRCHG)
3881 ep->d_ino = dap->da_previous->dm_oldinum;
3884 dap->da_state &= ~ATTACHED;
3885 dap->da_state |= UNDONE;
3891 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
3892 * Note that any bug fixes made to this routine must be done in the
3893 * version found below.
3895 * Called from within the procedure above to deal with unsatisfied
3896 * allocation dependencies in an inodeblock. The buffer must be
3897 * locked, thus, no I/O completion operations can occur while we
3898 * are manipulating its associated dependencies.
3901 initiate_write_inodeblock_ufs1(inodedep, bp)
3902 struct inodedep *inodedep;
3903 struct buf *bp; /* The inode block */
3905 struct allocdirect *adp, *lastadp;
3906 struct ufs1_dinode *dp;
3907 struct ufs1_dinode *sip;
3911 ufs_lbn_t prevlbn = 0;
3915 if (inodedep->id_state & IOSTARTED)
3916 panic("initiate_write_inodeblock_ufs1: already started");
3917 inodedep->id_state |= IOSTARTED;
3918 fs = inodedep->id_fs;
3919 dp = (struct ufs1_dinode *)bp->b_data +
3920 ino_to_fsbo(fs, inodedep->id_ino);
3922 * If the bitmap is not yet written, then the allocated
3923 * inode cannot be written to disk.
3925 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3926 if (inodedep->id_savedino1 != NULL)
3927 panic("initiate_write_inodeblock_ufs1: I/O underway");
3929 MALLOC(sip, struct ufs1_dinode *,
3930 sizeof(struct ufs1_dinode), M_SAVEDINO, M_SOFTDEP_FLAGS);
3932 inodedep->id_savedino1 = sip;
3933 *inodedep->id_savedino1 = *dp;
3934 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
3935 dp->di_gen = inodedep->id_savedino1->di_gen;
3939 * If no dependencies, then there is nothing to roll back.
3941 inodedep->id_savedsize = dp->di_size;
3942 inodedep->id_savedextsize = 0;
3943 if (TAILQ_EMPTY(&inodedep->id_inoupdt))
3946 * Set the dependencies to busy.
3948 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3949 adp = TAILQ_NEXT(adp, ad_next)) {
3951 if (deplist != 0 && prevlbn >= adp->ad_lbn)
3952 panic("softdep_write_inodeblock: lbn order");
3953 prevlbn = adp->ad_lbn;
3954 if (adp->ad_lbn < NDADDR &&
3955 dp->di_db[adp->ad_lbn] != adp->ad_newblkno)
3956 panic("%s: direct pointer #%jd mismatch %d != %jd",
3957 "softdep_write_inodeblock",
3958 (intmax_t)adp->ad_lbn,
3959 dp->di_db[adp->ad_lbn],
3960 (intmax_t)adp->ad_newblkno);
3961 if (adp->ad_lbn >= NDADDR &&
3962 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno)
3963 panic("%s: indirect pointer #%jd mismatch %d != %jd",
3964 "softdep_write_inodeblock",
3965 (intmax_t)adp->ad_lbn - NDADDR,
3966 dp->di_ib[adp->ad_lbn - NDADDR],
3967 (intmax_t)adp->ad_newblkno);
3968 deplist |= 1 << adp->ad_lbn;
3969 if ((adp->ad_state & ATTACHED) == 0)
3970 panic("softdep_write_inodeblock: Unknown state 0x%x",
3972 #endif /* INVARIANTS */
3973 adp->ad_state &= ~ATTACHED;
3974 adp->ad_state |= UNDONE;
3977 * The on-disk inode cannot claim to be any larger than the last
3978 * fragment that has been written. Otherwise, the on-disk inode
3979 * might have fragments that were not the last block in the file
3980 * which would corrupt the filesystem.
3982 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3983 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3984 if (adp->ad_lbn >= NDADDR)
3986 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3987 /* keep going until hitting a rollback to a frag */
3988 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3990 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3991 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3993 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
3994 panic("softdep_write_inodeblock: lost dep1");
3995 #endif /* INVARIANTS */
3998 for (i = 0; i < NIADDR; i++) {
4000 if (dp->di_ib[i] != 0 &&
4001 (deplist & ((1 << NDADDR) << i)) == 0)
4002 panic("softdep_write_inodeblock: lost dep2");
4003 #endif /* INVARIANTS */
4009 * If we have zero'ed out the last allocated block of the file,
4010 * roll back the size to the last currently allocated block.
4011 * We know that this last allocated block is a full-sized as
4012 * we already checked for fragments in the loop above.
4014 if (lastadp != NULL &&
4015 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4016 for (i = lastadp->ad_lbn; i >= 0; i--)
4017 if (dp->di_db[i] != 0)
4019 dp->di_size = (i + 1) * fs->fs_bsize;
4022 * The only dependencies are for indirect blocks.
4024 * The file size for indirect block additions is not guaranteed.
4025 * Such a guarantee would be non-trivial to achieve. The conventional
4026 * synchronous write implementation also does not make this guarantee.
4027 * Fsck should catch and fix discrepancies. Arguably, the file size
4028 * can be over-estimated without destroying integrity when the file
4029 * moves into the indirect blocks (i.e., is large). If we want to
4030 * postpone fsck, we are stuck with this argument.
4032 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
4033 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
4037 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
4038 * Note that any bug fixes made to this routine must be done in the
4039 * version found above.
4041 * Called from within the procedure above to deal with unsatisfied
4042 * allocation dependencies in an inodeblock. The buffer must be
4043 * locked, thus, no I/O completion operations can occur while we
4044 * are manipulating its associated dependencies.
4047 initiate_write_inodeblock_ufs2(inodedep, bp)
4048 struct inodedep *inodedep;
4049 struct buf *bp; /* The inode block */
4051 struct allocdirect *adp, *lastadp;
4052 struct ufs2_dinode *dp;
4053 struct ufs2_dinode *sip;
4057 ufs_lbn_t prevlbn = 0;
4061 if (inodedep->id_state & IOSTARTED)
4062 panic("initiate_write_inodeblock_ufs2: already started");
4063 inodedep->id_state |= IOSTARTED;
4064 fs = inodedep->id_fs;
4065 dp = (struct ufs2_dinode *)bp->b_data +
4066 ino_to_fsbo(fs, inodedep->id_ino);
4068 * If the bitmap is not yet written, then the allocated
4069 * inode cannot be written to disk.
4071 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4072 if (inodedep->id_savedino2 != NULL)
4073 panic("initiate_write_inodeblock_ufs2: I/O underway");
4075 MALLOC(sip, struct ufs2_dinode *,
4076 sizeof(struct ufs2_dinode), M_SAVEDINO, M_SOFTDEP_FLAGS);
4078 inodedep->id_savedino2 = sip;
4079 *inodedep->id_savedino2 = *dp;
4080 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
4081 dp->di_gen = inodedep->id_savedino2->di_gen;
4085 * If no dependencies, then there is nothing to roll back.
4087 inodedep->id_savedsize = dp->di_size;
4088 inodedep->id_savedextsize = dp->di_extsize;
4089 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
4090 TAILQ_EMPTY(&inodedep->id_extupdt))
4093 * Set the ext data dependencies to busy.
4095 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
4096 adp = TAILQ_NEXT(adp, ad_next)) {
4098 if (deplist != 0 && prevlbn >= adp->ad_lbn)
4099 panic("softdep_write_inodeblock: lbn order");
4100 prevlbn = adp->ad_lbn;
4101 if (dp->di_extb[adp->ad_lbn] != adp->ad_newblkno)
4102 panic("%s: direct pointer #%jd mismatch %jd != %jd",
4103 "softdep_write_inodeblock",
4104 (intmax_t)adp->ad_lbn,
4105 (intmax_t)dp->di_extb[adp->ad_lbn],
4106 (intmax_t)adp->ad_newblkno);
4107 deplist |= 1 << adp->ad_lbn;
4108 if ((adp->ad_state & ATTACHED) == 0)
4109 panic("softdep_write_inodeblock: Unknown state 0x%x",
4111 #endif /* INVARIANTS */
4112 adp->ad_state &= ~ATTACHED;
4113 adp->ad_state |= UNDONE;
4116 * The on-disk inode cannot claim to be any larger than the last
4117 * fragment that has been written. Otherwise, the on-disk inode
4118 * might have fragments that were not the last block in the ext
4119 * data which would corrupt the filesystem.
4121 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
4122 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
4123 dp->di_extb[adp->ad_lbn] = adp->ad_oldblkno;
4124 /* keep going until hitting a rollback to a frag */
4125 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
4127 dp->di_extsize = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
4128 for (i = adp->ad_lbn + 1; i < NXADDR; i++) {
4130 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
4131 panic("softdep_write_inodeblock: lost dep1");
4132 #endif /* INVARIANTS */
4139 * If we have zero'ed out the last allocated block of the ext
4140 * data, roll back the size to the last currently allocated block.
4141 * We know that this last allocated block is a full-sized as
4142 * we already checked for fragments in the loop above.
4144 if (lastadp != NULL &&
4145 dp->di_extsize <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4146 for (i = lastadp->ad_lbn; i >= 0; i--)
4147 if (dp->di_extb[i] != 0)
4149 dp->di_extsize = (i + 1) * fs->fs_bsize;
4152 * Set the file data dependencies to busy.
4154 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
4155 adp = TAILQ_NEXT(adp, ad_next)) {
4157 if (deplist != 0 && prevlbn >= adp->ad_lbn)
4158 panic("softdep_write_inodeblock: lbn order");
4159 prevlbn = adp->ad_lbn;
4160 if (adp->ad_lbn < NDADDR &&
4161 dp->di_db[adp->ad_lbn] != adp->ad_newblkno)
4162 panic("%s: direct pointer #%jd mismatch %jd != %jd",
4163 "softdep_write_inodeblock",
4164 (intmax_t)adp->ad_lbn,
4165 (intmax_t)dp->di_db[adp->ad_lbn],
4166 (intmax_t)adp->ad_newblkno);
4167 if (adp->ad_lbn >= NDADDR &&
4168 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno)
4169 panic("%s indirect pointer #%jd mismatch %jd != %jd",
4170 "softdep_write_inodeblock:",
4171 (intmax_t)adp->ad_lbn - NDADDR,
4172 (intmax_t)dp->di_ib[adp->ad_lbn - NDADDR],
4173 (intmax_t)adp->ad_newblkno);
4174 deplist |= 1 << adp->ad_lbn;
4175 if ((adp->ad_state & ATTACHED) == 0)
4176 panic("softdep_write_inodeblock: Unknown state 0x%x",
4178 #endif /* INVARIANTS */
4179 adp->ad_state &= ~ATTACHED;
4180 adp->ad_state |= UNDONE;
4183 * The on-disk inode cannot claim to be any larger than the last
4184 * fragment that has been written. Otherwise, the on-disk inode
4185 * might have fragments that were not the last block in the file
4186 * which would corrupt the filesystem.
4188 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
4189 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
4190 if (adp->ad_lbn >= NDADDR)
4192 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
4193 /* keep going until hitting a rollback to a frag */
4194 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
4196 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
4197 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
4199 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
4200 panic("softdep_write_inodeblock: lost dep2");
4201 #endif /* INVARIANTS */
4204 for (i = 0; i < NIADDR; i++) {
4206 if (dp->di_ib[i] != 0 &&
4207 (deplist & ((1 << NDADDR) << i)) == 0)
4208 panic("softdep_write_inodeblock: lost dep3");
4209 #endif /* INVARIANTS */
4215 * If we have zero'ed out the last allocated block of the file,
4216 * roll back the size to the last currently allocated block.
4217 * We know that this last allocated block is a full-sized as
4218 * we already checked for fragments in the loop above.
4220 if (lastadp != NULL &&
4221 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4222 for (i = lastadp->ad_lbn; i >= 0; i--)
4223 if (dp->di_db[i] != 0)
4225 dp->di_size = (i + 1) * fs->fs_bsize;
4228 * The only dependencies are for indirect blocks.
4230 * The file size for indirect block additions is not guaranteed.
4231 * Such a guarantee would be non-trivial to achieve. The conventional
4232 * synchronous write implementation also does not make this guarantee.
4233 * Fsck should catch and fix discrepancies. Arguably, the file size
4234 * can be over-estimated without destroying integrity when the file
4235 * moves into the indirect blocks (i.e., is large). If we want to
4236 * postpone fsck, we are stuck with this argument.
4238 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
4239 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
4243 * This routine is called during the completion interrupt
4244 * service routine for a disk write (from the procedure called
4245 * by the device driver to inform the filesystem caches of
4246 * a request completion). It should be called early in this
4247 * procedure, before the block is made available to other
4248 * processes or other routines are called.
4251 softdep_disk_write_complete(bp)
4252 struct buf *bp; /* describes the completed disk write */
4254 struct worklist *wk;
4255 struct worklist *owk;
4256 struct workhead reattach;
4257 struct newblk *newblk;
4258 struct allocindir *aip;
4259 struct allocdirect *adp;
4260 struct indirdep *indirdep;
4261 struct inodedep *inodedep;
4262 struct bmsafemap *bmsafemap;
4265 * If an error occurred while doing the write, then the data
4266 * has not hit the disk and the dependencies cannot be unrolled.
4268 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0)
4270 LIST_INIT(&reattach);
4272 * This lock must not be released anywhere in this code segment.
4276 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
4277 WORKLIST_REMOVE(wk);
4279 panic("duplicate worklist: %p\n", wk);
4281 switch (wk->wk_type) {
4284 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
4285 WORKLIST_INSERT(&reattach, wk);
4289 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
4290 WORKLIST_INSERT(&reattach, wk);
4294 bmsafemap = WK_BMSAFEMAP(wk);
4295 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
4296 newblk->nb_state |= DEPCOMPLETE;
4297 newblk->nb_bmsafemap = NULL;
4298 LIST_REMOVE(newblk, nb_deps);
4301 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
4302 adp->ad_state |= DEPCOMPLETE;
4304 LIST_REMOVE(adp, ad_deps);
4305 handle_allocdirect_partdone(adp);
4308 LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
4309 aip->ai_state |= DEPCOMPLETE;
4311 LIST_REMOVE(aip, ai_deps);
4312 handle_allocindir_partdone(aip);
4315 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
4316 inodedep->id_state |= DEPCOMPLETE;
4317 LIST_REMOVE(inodedep, id_deps);
4318 inodedep->id_buf = NULL;
4320 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
4324 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
4328 adp = WK_ALLOCDIRECT(wk);
4329 adp->ad_state |= COMPLETE;
4330 handle_allocdirect_partdone(adp);
4334 aip = WK_ALLOCINDIR(wk);
4335 aip->ai_state |= COMPLETE;
4336 handle_allocindir_partdone(aip);
4340 indirdep = WK_INDIRDEP(wk);
4341 if (indirdep->ir_state & GOINGAWAY)
4342 panic("disk_write_complete: indirdep gone");
4343 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
4344 FREE(indirdep->ir_saveddata, M_INDIRDEP);
4345 indirdep->ir_saveddata = 0;
4346 indirdep->ir_state &= ~UNDONE;
4347 indirdep->ir_state |= ATTACHED;
4348 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
4349 handle_allocindir_partdone(aip);
4350 if (aip == LIST_FIRST(&indirdep->ir_donehd))
4351 panic("disk_write_complete: not gone");
4353 WORKLIST_INSERT(&reattach, wk);
4354 if ((bp->b_flags & B_DELWRI) == 0)
4355 stat_indir_blk_ptrs++;
4360 panic("handle_disk_write_complete: Unknown type %s",
4361 TYPENAME(wk->wk_type));
4366 * Reattach any requests that must be redone.
4368 while ((wk = LIST_FIRST(&reattach)) != NULL) {
4369 WORKLIST_REMOVE(wk);
4370 WORKLIST_INSERT(&bp->b_dep, wk);
4376 * Called from within softdep_disk_write_complete above. Note that
4377 * this routine is always called from interrupt level with further
4378 * splbio interrupts blocked.
4381 handle_allocdirect_partdone(adp)
4382 struct allocdirect *adp; /* the completed allocdirect */
4384 struct allocdirectlst *listhead;
4385 struct allocdirect *listadp;
4386 struct inodedep *inodedep;
4389 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4391 if (adp->ad_buf != NULL)
4392 panic("handle_allocdirect_partdone: dangling dep");
4394 * The on-disk inode cannot claim to be any larger than the last
4395 * fragment that has been written. Otherwise, the on-disk inode
4396 * might have fragments that were not the last block in the file
4397 * which would corrupt the filesystem. Thus, we cannot free any
4398 * allocdirects after one whose ad_oldblkno claims a fragment as
4399 * these blocks must be rolled back to zero before writing the inode.
4400 * We check the currently active set of allocdirects in id_inoupdt
4401 * or id_extupdt as appropriate.
4403 inodedep = adp->ad_inodedep;
4404 bsize = inodedep->id_fs->fs_bsize;
4405 if (adp->ad_state & EXTDATA)
4406 listhead = &inodedep->id_extupdt;
4408 listhead = &inodedep->id_inoupdt;
4409 TAILQ_FOREACH(listadp, listhead, ad_next) {
4410 /* found our block */
4413 /* continue if ad_oldlbn is not a fragment */
4414 if (listadp->ad_oldsize == 0 ||
4415 listadp->ad_oldsize == bsize)
4417 /* hit a fragment */
4421 * If we have reached the end of the current list without
4422 * finding the just finished dependency, then it must be
4423 * on the future dependency list. Future dependencies cannot
4424 * be freed until they are moved to the current list.
4426 if (listadp == NULL) {
4428 if (adp->ad_state & EXTDATA)
4429 listhead = &inodedep->id_newextupdt;
4431 listhead = &inodedep->id_newinoupdt;
4432 TAILQ_FOREACH(listadp, listhead, ad_next)
4433 /* found our block */
4436 if (listadp == NULL)
4437 panic("handle_allocdirect_partdone: lost dep");
4442 * If we have found the just finished dependency, then free
4443 * it along with anything that follows it that is complete.
4444 * If the inode still has a bitmap dependency, then it has
4445 * never been written to disk, hence the on-disk inode cannot
4446 * reference the old fragment so we can free it without delay.
4448 delay = (inodedep->id_state & DEPCOMPLETE);
4449 for (; adp; adp = listadp) {
4450 listadp = TAILQ_NEXT(adp, ad_next);
4451 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4453 free_allocdirect(listhead, adp, delay);
4458 * Called from within softdep_disk_write_complete above. Note that
4459 * this routine is always called from interrupt level with further
4460 * splbio interrupts blocked.
4463 handle_allocindir_partdone(aip)
4464 struct allocindir *aip; /* the completed allocindir */
4466 struct indirdep *indirdep;
4468 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
4470 if (aip->ai_buf != NULL)
4471 panic("handle_allocindir_partdone: dangling dependency");
4472 indirdep = aip->ai_indirdep;
4473 if (indirdep->ir_state & UNDONE) {
4474 LIST_REMOVE(aip, ai_next);
4475 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
4478 if (indirdep->ir_state & UFS1FMT)
4479 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4482 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4484 LIST_REMOVE(aip, ai_next);
4485 if (aip->ai_freefrag != NULL)
4486 add_to_worklist(&aip->ai_freefrag->ff_list);
4487 WORKITEM_FREE(aip, D_ALLOCINDIR);
4491 * Called from within softdep_disk_write_complete above to restore
4492 * in-memory inode block contents to their most up-to-date state. Note
4493 * that this routine is always called from interrupt level with further
4494 * splbio interrupts blocked.
4497 handle_written_inodeblock(inodedep, bp)
4498 struct inodedep *inodedep;
4499 struct buf *bp; /* buffer containing the inode block */
4501 struct worklist *wk, *filefree;
4502 struct allocdirect *adp, *nextadp;
4503 struct ufs1_dinode *dp1 = NULL;
4504 struct ufs2_dinode *dp2 = NULL;
4505 int hadchanges, fstype;
4507 if ((inodedep->id_state & IOSTARTED) == 0)
4508 panic("handle_written_inodeblock: not started");
4509 inodedep->id_state &= ~IOSTARTED;
4510 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
4512 dp1 = (struct ufs1_dinode *)bp->b_data +
4513 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4516 dp2 = (struct ufs2_dinode *)bp->b_data +
4517 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4520 * If we had to rollback the inode allocation because of
4521 * bitmaps being incomplete, then simply restore it.
4522 * Keep the block dirty so that it will not be reclaimed until
4523 * all associated dependencies have been cleared and the
4524 * corresponding updates written to disk.
4526 if (inodedep->id_savedino1 != NULL) {
4528 *dp1 = *inodedep->id_savedino1;
4530 *dp2 = *inodedep->id_savedino2;
4531 FREE(inodedep->id_savedino1, M_SAVEDINO);
4532 inodedep->id_savedino1 = NULL;
4533 if ((bp->b_flags & B_DELWRI) == 0)
4534 stat_inode_bitmap++;
4538 inodedep->id_state |= COMPLETE;
4540 * Roll forward anything that had to be rolled back before
4541 * the inode could be updated.
4544 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
4545 nextadp = TAILQ_NEXT(adp, ad_next);
4546 if (adp->ad_state & ATTACHED)
4547 panic("handle_written_inodeblock: new entry");
4548 if (fstype == UFS1) {
4549 if (adp->ad_lbn < NDADDR) {
4550 if (dp1->di_db[adp->ad_lbn]!=adp->ad_oldblkno)
4551 panic("%s %s #%jd mismatch %d != %jd",
4552 "handle_written_inodeblock:",
4554 (intmax_t)adp->ad_lbn,
4555 dp1->di_db[adp->ad_lbn],
4556 (intmax_t)adp->ad_oldblkno);
4557 dp1->di_db[adp->ad_lbn] = adp->ad_newblkno;
4559 if (dp1->di_ib[adp->ad_lbn - NDADDR] != 0)
4560 panic("%s: %s #%jd allocated as %d",
4561 "handle_written_inodeblock",
4563 (intmax_t)adp->ad_lbn - NDADDR,
4564 dp1->di_ib[adp->ad_lbn - NDADDR]);
4565 dp1->di_ib[adp->ad_lbn - NDADDR] =
4569 if (adp->ad_lbn < NDADDR) {
4570 if (dp2->di_db[adp->ad_lbn]!=adp->ad_oldblkno)
4571 panic("%s: %s #%jd %s %jd != %jd",
4572 "handle_written_inodeblock",
4574 (intmax_t)adp->ad_lbn, "mismatch",
4575 (intmax_t)dp2->di_db[adp->ad_lbn],
4576 (intmax_t)adp->ad_oldblkno);
4577 dp2->di_db[adp->ad_lbn] = adp->ad_newblkno;
4579 if (dp2->di_ib[adp->ad_lbn - NDADDR] != 0)
4580 panic("%s: %s #%jd allocated as %jd",
4581 "handle_written_inodeblock",
4583 (intmax_t)adp->ad_lbn - NDADDR,
4585 dp2->di_ib[adp->ad_lbn - NDADDR]);
4586 dp2->di_ib[adp->ad_lbn - NDADDR] =
4590 adp->ad_state &= ~UNDONE;
4591 adp->ad_state |= ATTACHED;
4594 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
4595 nextadp = TAILQ_NEXT(adp, ad_next);
4596 if (adp->ad_state & ATTACHED)
4597 panic("handle_written_inodeblock: new entry");
4598 if (dp2->di_extb[adp->ad_lbn] != adp->ad_oldblkno)
4599 panic("%s: direct pointers #%jd %s %jd != %jd",
4600 "handle_written_inodeblock",
4601 (intmax_t)adp->ad_lbn, "mismatch",
4602 (intmax_t)dp2->di_extb[adp->ad_lbn],
4603 (intmax_t)adp->ad_oldblkno);
4604 dp2->di_extb[adp->ad_lbn] = adp->ad_newblkno;
4605 adp->ad_state &= ~UNDONE;
4606 adp->ad_state |= ATTACHED;
4609 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
4610 stat_direct_blk_ptrs++;
4612 * Reset the file size to its most up-to-date value.
4614 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
4615 panic("handle_written_inodeblock: bad size");
4616 if (fstype == UFS1) {
4617 if (dp1->di_size != inodedep->id_savedsize) {
4618 dp1->di_size = inodedep->id_savedsize;
4622 if (dp2->di_size != inodedep->id_savedsize) {
4623 dp2->di_size = inodedep->id_savedsize;
4626 if (dp2->di_extsize != inodedep->id_savedextsize) {
4627 dp2->di_extsize = inodedep->id_savedextsize;
4631 inodedep->id_savedsize = -1;
4632 inodedep->id_savedextsize = -1;
4634 * If there were any rollbacks in the inode block, then it must be
4635 * marked dirty so that its will eventually get written back in
4641 * Process any allocdirects that completed during the update.
4643 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
4644 handle_allocdirect_partdone(adp);
4645 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
4646 handle_allocdirect_partdone(adp);
4648 * Process deallocations that were held pending until the
4649 * inode had been written to disk. Freeing of the inode
4650 * is delayed until after all blocks have been freed to
4651 * avoid creation of new <vfsid, inum, lbn> triples
4652 * before the old ones have been deleted.
4655 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
4656 WORKLIST_REMOVE(wk);
4657 switch (wk->wk_type) {
4661 * We defer adding filefree to the worklist until
4662 * all other additions have been made to ensure
4663 * that it will be done after all the old blocks
4666 if (filefree != NULL)
4667 panic("handle_written_inodeblock: filefree");
4672 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
4676 diradd_inode_written(WK_DIRADD(wk), inodedep);
4680 wk->wk_state |= COMPLETE;
4681 if ((wk->wk_state & ALLCOMPLETE) != ALLCOMPLETE)
4683 /* -- fall through -- */
4686 add_to_worklist(wk);
4690 free_newdirblk(WK_NEWDIRBLK(wk));
4694 panic("handle_written_inodeblock: Unknown type %s",
4695 TYPENAME(wk->wk_type));
4699 if (filefree != NULL) {
4700 if (free_inodedep(inodedep) == 0)
4701 panic("handle_written_inodeblock: live inodedep");
4702 add_to_worklist(filefree);
4707 * If no outstanding dependencies, free it.
4709 if (free_inodedep(inodedep) ||
4710 (TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
4711 TAILQ_FIRST(&inodedep->id_extupdt) == 0))
4713 return (hadchanges);
4717 * Process a diradd entry after its dependent inode has been written.
4718 * This routine must be called with splbio interrupts blocked.
4721 diradd_inode_written(dap, inodedep)
4723 struct inodedep *inodedep;
4725 struct pagedep *pagedep;
4727 dap->da_state |= COMPLETE;
4728 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4729 if (dap->da_state & DIRCHG)
4730 pagedep = dap->da_previous->dm_pagedep;
4732 pagedep = dap->da_pagedep;
4733 LIST_REMOVE(dap, da_pdlist);
4734 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4736 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
4740 * Handle the completion of a mkdir dependency.
4743 handle_written_mkdir(mkdir, type)
4744 struct mkdir *mkdir;
4748 struct pagedep *pagedep;
4750 if (mkdir->md_state != type)
4751 panic("handle_written_mkdir: bad type");
4752 dap = mkdir->md_diradd;
4753 dap->da_state &= ~type;
4754 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
4755 dap->da_state |= DEPCOMPLETE;
4756 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4757 if (dap->da_state & DIRCHG)
4758 pagedep = dap->da_previous->dm_pagedep;
4760 pagedep = dap->da_pagedep;
4761 LIST_REMOVE(dap, da_pdlist);
4762 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4764 LIST_REMOVE(mkdir, md_mkdirs);
4765 WORKITEM_FREE(mkdir, D_MKDIR);
4769 * Called from within softdep_disk_write_complete above.
4770 * A write operation was just completed. Removed inodes can
4771 * now be freed and associated block pointers may be committed.
4772 * Note that this routine is always called from interrupt level
4773 * with further splbio interrupts blocked.
4776 handle_written_filepage(pagedep, bp)
4777 struct pagedep *pagedep;
4778 struct buf *bp; /* buffer containing the written page */
4780 struct dirrem *dirrem;
4781 struct diradd *dap, *nextdap;
4785 if ((pagedep->pd_state & IOSTARTED) == 0)
4786 panic("handle_written_filepage: not started");
4787 pagedep->pd_state &= ~IOSTARTED;
4789 * Process any directory removals that have been committed.
4791 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
4792 LIST_REMOVE(dirrem, dm_next);
4793 dirrem->dm_dirinum = pagedep->pd_ino;
4794 add_to_worklist(&dirrem->dm_list);
4797 * Free any directory additions that have been committed.
4798 * If it is a newly allocated block, we have to wait until
4799 * the on-disk directory inode claims the new block.
4801 if ((pagedep->pd_state & NEWBLOCK) == 0)
4802 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
4805 * Uncommitted directory entries must be restored.
4807 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
4808 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
4810 nextdap = LIST_NEXT(dap, da_pdlist);
4811 if (dap->da_state & ATTACHED)
4812 panic("handle_written_filepage: attached");
4813 ep = (struct direct *)
4814 ((char *)bp->b_data + dap->da_offset);
4815 ep->d_ino = dap->da_newinum;
4816 dap->da_state &= ~UNDONE;
4817 dap->da_state |= ATTACHED;
4820 * If the inode referenced by the directory has
4821 * been written out, then the dependency can be
4822 * moved to the pending list.
4824 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4825 LIST_REMOVE(dap, da_pdlist);
4826 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
4832 * If there were any rollbacks in the directory, then it must be
4833 * marked dirty so that its will eventually get written back in
4837 if ((bp->b_flags & B_DELWRI) == 0)
4843 * If we are not waiting for a new directory block to be
4844 * claimed by its inode, then the pagedep will be freed.
4845 * Otherwise it will remain to track any new entries on
4846 * the page in case they are fsync'ed.
4848 if ((pagedep->pd_state & NEWBLOCK) == 0) {
4849 LIST_REMOVE(pagedep, pd_hash);
4850 WORKITEM_FREE(pagedep, D_PAGEDEP);
4856 * Writing back in-core inode structures.
4858 * The filesystem only accesses an inode's contents when it occupies an
4859 * "in-core" inode structure. These "in-core" structures are separate from
4860 * the page frames used to cache inode blocks. Only the latter are
4861 * transferred to/from the disk. So, when the updated contents of the
4862 * "in-core" inode structure are copied to the corresponding in-memory inode
4863 * block, the dependencies are also transferred. The following procedure is
4864 * called when copying a dirty "in-core" inode to a cached inode block.
4868 * Called when an inode is loaded from disk. If the effective link count
4869 * differed from the actual link count when it was last flushed, then we
4870 * need to ensure that the correct effective link count is put back.
4873 softdep_load_inodeblock(ip)
4874 struct inode *ip; /* the "in_core" copy of the inode */
4876 struct inodedep *inodedep;
4879 * Check for alternate nlink count.
4881 ip->i_effnlink = ip->i_nlink;
4883 if (inodedep_lookup(UFSTOVFS(ip->i_ump),
4884 ip->i_number, 0, &inodedep) == 0) {
4888 ip->i_effnlink -= inodedep->id_nlinkdelta;
4889 if (inodedep->id_state & SPACECOUNTED)
4890 ip->i_flag |= IN_SPACECOUNTED;
4895 * This routine is called just before the "in-core" inode
4896 * information is to be copied to the in-memory inode block.
4897 * Recall that an inode block contains several inodes. If
4898 * the force flag is set, then the dependencies will be
4899 * cleared so that the update can always be made. Note that
4900 * the buffer is locked when this routine is called, so we
4901 * will never be in the middle of writing the inode block
4905 softdep_update_inodeblock(ip, bp, waitfor)
4906 struct inode *ip; /* the "in_core" copy of the inode */
4907 struct buf *bp; /* the buffer containing the inode block */
4908 int waitfor; /* nonzero => update must be allowed */
4910 struct inodedep *inodedep;
4911 struct worklist *wk;
4917 * If the effective link count is not equal to the actual link
4918 * count, then we must track the difference in an inodedep while
4919 * the inode is (potentially) tossed out of the cache. Otherwise,
4920 * if there is no existing inodedep, then there are no dependencies
4923 mp = UFSTOVFS(ip->i_ump);
4925 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
4927 if (ip->i_effnlink != ip->i_nlink)
4928 panic("softdep_update_inodeblock: bad link count");
4931 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
4932 panic("softdep_update_inodeblock: bad delta");
4934 * Changes have been initiated. Anything depending on these
4935 * changes cannot occur until this inode has been written.
4937 inodedep->id_state &= ~COMPLETE;
4938 if ((inodedep->id_state & ONWORKLIST) == 0)
4939 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
4941 * Any new dependencies associated with the incore inode must
4942 * now be moved to the list associated with the buffer holding
4943 * the in-memory copy of the inode. Once merged process any
4944 * allocdirects that are completed by the merger.
4946 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
4947 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
4948 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
4949 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
4950 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
4951 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt));
4953 * Now that the inode has been pushed into the buffer, the
4954 * operations dependent on the inode being written to disk
4955 * can be moved to the id_bufwait so that they will be
4956 * processed when the buffer I/O completes.
4958 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
4959 WORKLIST_REMOVE(wk);
4960 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
4963 * Newly allocated inodes cannot be written until the bitmap
4964 * that allocates them have been written (indicated by
4965 * DEPCOMPLETE being set in id_state). If we are doing a
4966 * forced sync (e.g., an fsync on a file), we force the bitmap
4967 * to be written so that the update can be done.
4974 if ((inodedep->id_state & DEPCOMPLETE) != 0) {
4978 ibp = inodedep->id_buf;
4979 ibp = getdirtybuf(ibp, &lk, MNT_WAIT);
4982 * If ibp came back as NULL, the dependency could have been
4983 * freed while we slept. Look it up again, and check to see
4984 * that it has completed.
4986 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
4992 if ((error = bwrite(ibp)) != 0)
4993 softdep_error("softdep_update_inodeblock: bwrite", error);
4997 * Merge the a new inode dependency list (such as id_newinoupdt) into an
4998 * old inode dependency list (such as id_inoupdt). This routine must be
4999 * called with splbio interrupts blocked.
5002 merge_inode_lists(newlisthead, oldlisthead)
5003 struct allocdirectlst *newlisthead;
5004 struct allocdirectlst *oldlisthead;
5006 struct allocdirect *listadp, *newadp;
5008 newadp = TAILQ_FIRST(newlisthead);
5009 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
5010 if (listadp->ad_lbn < newadp->ad_lbn) {
5011 listadp = TAILQ_NEXT(listadp, ad_next);
5014 TAILQ_REMOVE(newlisthead, newadp, ad_next);
5015 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
5016 if (listadp->ad_lbn == newadp->ad_lbn) {
5017 allocdirect_merge(oldlisthead, newadp,
5021 newadp = TAILQ_FIRST(newlisthead);
5023 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
5024 TAILQ_REMOVE(newlisthead, newadp, ad_next);
5025 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
5030 * If we are doing an fsync, then we must ensure that any directory
5031 * entries for the inode have been written after the inode gets to disk.
5035 struct vnode *vp; /* the "in_core" copy of the inode */
5037 struct inodedep *inodedep;
5038 struct pagedep *pagedep;
5039 struct worklist *wk;
5046 struct thread *td = curthread;
5047 int error, flushparent, pagedep_new_block;
5055 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
5059 if (!LIST_EMPTY(&inodedep->id_inowait) ||
5060 !LIST_EMPTY(&inodedep->id_bufwait) ||
5061 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
5062 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
5063 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
5064 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
5065 panic("softdep_fsync: pending ops");
5066 for (error = 0, flushparent = 0; ; ) {
5067 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
5069 if (wk->wk_type != D_DIRADD)
5070 panic("softdep_fsync: Unexpected type %s",
5071 TYPENAME(wk->wk_type));
5072 dap = WK_DIRADD(wk);
5074 * Flush our parent if this directory entry has a MKDIR_PARENT
5075 * dependency or is contained in a newly allocated block.
5077 if (dap->da_state & DIRCHG)
5078 pagedep = dap->da_previous->dm_pagedep;
5080 pagedep = dap->da_pagedep;
5081 parentino = pagedep->pd_ino;
5082 lbn = pagedep->pd_lbn;
5083 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
5084 panic("softdep_fsync: dirty");
5085 if ((dap->da_state & MKDIR_PARENT) ||
5086 (pagedep->pd_state & NEWBLOCK))
5091 * If we are being fsync'ed as part of vgone'ing this vnode,
5092 * then we will not be able to release and recover the
5093 * vnode below, so we just have to give up on writing its
5094 * directory entry out. It will eventually be written, just
5095 * not now, but then the user was not asking to have it
5096 * written, so we are not breaking any promises.
5098 if (vp->v_iflag & VI_DOOMED)
5101 * We prevent deadlock by always fetching inodes from the
5102 * root, moving down the directory tree. Thus, when fetching
5103 * our parent directory, we first try to get the lock. If
5104 * that fails, we must unlock ourselves before requesting
5105 * the lock on our parent. See the comment in ufs_lookup
5106 * for details on possible races.
5109 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
5111 VOP_UNLOCK(vp, 0, td);
5112 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
5113 &pvp, FFSV_FORCEINSMQ);
5114 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
5119 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
5120 * that are contained in direct blocks will be resolved by
5121 * doing a ffs_update. Pagedeps contained in indirect blocks
5122 * may require a complete sync'ing of the directory. So, we
5123 * try the cheap and fast ffs_update first, and if that fails,
5124 * then we do the slower ffs_syncvnode of the directory.
5129 if ((error = ffs_update(pvp, 1)) != 0) {
5135 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
5136 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
5137 if (wk->wk_type != D_DIRADD)
5138 panic("softdep_fsync: Unexpected type %s",
5139 TYPENAME(wk->wk_type));
5140 dap = WK_DIRADD(wk);
5141 if (dap->da_state & DIRCHG)
5142 pagedep = dap->da_previous->dm_pagedep;
5144 pagedep = dap->da_pagedep;
5145 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
5148 if (pagedep_new_block &&
5149 (error = ffs_syncvnode(pvp, MNT_WAIT))) {
5159 * Flush directory page containing the inode's name.
5161 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
5171 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
5179 * Flush all the dirty bitmaps associated with the block device
5180 * before flushing the rest of the dirty blocks so as to reduce
5181 * the number of dependencies that will have to be rolled back.
5184 softdep_fsync_mountdev(vp)
5187 struct buf *bp, *nbp;
5188 struct worklist *wk;
5190 if (!vn_isdisk(vp, NULL))
5191 panic("softdep_fsync_mountdev: vnode not a disk");
5195 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
5197 * If it is already scheduled, skip to the next buffer.
5199 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
5202 if ((bp->b_flags & B_DELWRI) == 0)
5203 panic("softdep_fsync_mountdev: not dirty");
5205 * We are only interested in bitmaps with outstanding
5208 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
5209 wk->wk_type != D_BMSAFEMAP ||
5210 (bp->b_vflags & BV_BKGRDINPROG)) {
5226 * This routine is called when we are trying to synchronously flush a
5227 * file. This routine must eliminate any filesystem metadata dependencies
5228 * so that the syncing routine can succeed by pushing the dirty blocks
5229 * associated with the file. If any I/O errors occur, they are returned.
5232 softdep_sync_metadata(struct vnode *vp)
5234 struct pagedep *pagedep;
5235 struct allocdirect *adp;
5236 struct allocindir *aip;
5237 struct buf *bp, *nbp;
5238 struct worklist *wk;
5239 int i, error, waitfor;
5241 if (!DOINGSOFTDEP(vp))
5244 * Ensure that any direct block dependencies have been cleared.
5247 if ((error = flush_inodedep_deps(vp->v_mount, VTOI(vp)->i_number))) {
5253 * For most files, the only metadata dependencies are the
5254 * cylinder group maps that allocate their inode or blocks.
5255 * The block allocation dependencies can be found by traversing
5256 * the dependency lists for any buffers that remain on their
5257 * dirty buffer list. The inode allocation dependency will
5258 * be resolved when the inode is updated with MNT_WAIT.
5259 * This work is done in two passes. The first pass grabs most
5260 * of the buffers and begins asynchronously writing them. The
5261 * only way to wait for these asynchronous writes is to sleep
5262 * on the filesystem vnode which may stay busy for a long time
5263 * if the filesystem is active. So, instead, we make a second
5264 * pass over the dependencies blocking on each write. In the
5265 * usual case we will be blocking against a write that we
5266 * initiated, so when it is done the dependency will have been
5267 * resolved. Thus the second pass is expected to end quickly.
5269 waitfor = MNT_NOWAIT;
5273 * We must wait for any I/O in progress to finish so that
5274 * all potential buffers on the dirty list will be visible.
5278 while ((bp = TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd)) != NULL) {
5279 bp = getdirtybuf(bp, VI_MTX(vp), MNT_WAIT);
5287 /* While syncing snapshots, we must allow recursive lookups */
5288 bp->b_lock.lk_flags |= LK_CANRECURSE;
5291 * As we hold the buffer locked, none of its dependencies
5294 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5295 switch (wk->wk_type) {
5298 adp = WK_ALLOCDIRECT(wk);
5299 if (adp->ad_state & DEPCOMPLETE)
5302 nbp = getdirtybuf(nbp, &lk, waitfor);
5306 if (waitfor == MNT_NOWAIT) {
5308 } else if ((error = bwrite(nbp)) != 0) {
5315 aip = WK_ALLOCINDIR(wk);
5316 if (aip->ai_state & DEPCOMPLETE)
5319 nbp = getdirtybuf(nbp, &lk, waitfor);
5323 if (waitfor == MNT_NOWAIT) {
5325 } else if ((error = bwrite(nbp)) != 0) {
5334 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
5335 if (aip->ai_state & DEPCOMPLETE)
5338 nbp = getdirtybuf(nbp, &lk, MNT_WAIT);
5342 if ((error = bwrite(nbp)) != 0) {
5351 if ((error = flush_inodedep_deps(wk->wk_mp,
5352 WK_INODEDEP(wk)->id_ino)) != 0) {
5360 * We are trying to sync a directory that may
5361 * have dependencies on both its own metadata
5362 * and/or dependencies on the inodes of any
5363 * recently allocated files. We walk its diradd
5364 * lists pushing out the associated inode.
5366 pagedep = WK_PAGEDEP(wk);
5367 for (i = 0; i < DAHASHSZ; i++) {
5368 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
5371 flush_pagedep_deps(vp, wk->wk_mp,
5372 &pagedep->pd_diraddhd[i]))) {
5381 * This case should never happen if the vnode has
5382 * been properly sync'ed. However, if this function
5383 * is used at a place where the vnode has not yet
5384 * been sync'ed, this dependency can show up. So,
5385 * rather than panic, just flush it.
5387 nbp = WK_MKDIR(wk)->md_buf;
5388 nbp = getdirtybuf(nbp, &lk, waitfor);
5392 if (waitfor == MNT_NOWAIT) {
5394 } else if ((error = bwrite(nbp)) != 0) {
5402 * This case should never happen if the vnode has
5403 * been properly sync'ed. However, if this function
5404 * is used at a place where the vnode has not yet
5405 * been sync'ed, this dependency can show up. So,
5406 * rather than panic, just flush it.
5408 nbp = WK_BMSAFEMAP(wk)->sm_buf;
5409 nbp = getdirtybuf(nbp, &lk, waitfor);
5413 if (waitfor == MNT_NOWAIT) {
5415 } else if ((error = bwrite(nbp)) != 0) {
5422 panic("softdep_sync_metadata: Unknown type %s",
5423 TYPENAME(wk->wk_type));
5427 /* We reach here only in error and unlocked */
5429 panic("softdep_sync_metadata: zero error");
5430 bp->b_lock.lk_flags &= ~LK_CANRECURSE;
5436 while ((nbp = TAILQ_NEXT(bp, b_bobufs)) != NULL) {
5437 nbp = getdirtybuf(nbp, VI_MTX(vp), MNT_WAIT);
5442 bp->b_lock.lk_flags &= ~LK_CANRECURSE;
5449 * The brief unlock is to allow any pent up dependency
5450 * processing to be done. Then proceed with the second pass.
5452 if (waitfor == MNT_NOWAIT) {
5458 * If we have managed to get rid of all the dirty buffers,
5459 * then we are done. For certain directories and block
5460 * devices, we may need to do further work.
5462 * We must wait for any I/O in progress to finish so that
5463 * all potential buffers on the dirty list will be visible.
5472 * Flush the dependencies associated with an inodedep.
5473 * Called with splbio blocked.
5476 flush_inodedep_deps(mp, ino)
5480 struct inodedep *inodedep;
5484 * This work is done in two passes. The first pass grabs most
5485 * of the buffers and begins asynchronously writing them. The
5486 * only way to wait for these asynchronous writes is to sleep
5487 * on the filesystem vnode which may stay busy for a long time
5488 * if the filesystem is active. So, instead, we make a second
5489 * pass over the dependencies blocking on each write. In the
5490 * usual case we will be blocking against a write that we
5491 * initiated, so when it is done the dependency will have been
5492 * resolved. Thus the second pass is expected to end quickly.
5493 * We give a brief window at the top of the loop to allow
5494 * any pending I/O to complete.
5496 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
5501 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
5503 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
5504 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
5505 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
5506 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
5509 * If pass2, we are done, otherwise do pass 2.
5511 if (waitfor == MNT_WAIT)
5516 * Try freeing inodedep in case all dependencies have been removed.
5518 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
5519 (void) free_inodedep(inodedep);
5524 * Flush an inode dependency list.
5525 * Called with splbio blocked.
5528 flush_deplist(listhead, waitfor, errorp)
5529 struct allocdirectlst *listhead;
5533 struct allocdirect *adp;
5536 mtx_assert(&lk, MA_OWNED);
5537 TAILQ_FOREACH(adp, listhead, ad_next) {
5538 if (adp->ad_state & DEPCOMPLETE)
5541 bp = getdirtybuf(bp, &lk, waitfor);
5543 if (waitfor == MNT_NOWAIT)
5548 if (waitfor == MNT_NOWAIT) {
5550 } else if ((*errorp = bwrite(bp)) != 0) {
5561 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
5562 * Called with splbio blocked.
5565 flush_pagedep_deps(pvp, mp, diraddhdp)
5568 struct diraddhd *diraddhdp;
5570 struct inodedep *inodedep;
5571 struct ufsmount *ump;
5577 struct worklist *wk;
5580 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
5582 * Flush ourselves if this directory entry
5583 * has a MKDIR_PARENT dependency.
5585 if (dap->da_state & MKDIR_PARENT) {
5587 if ((error = ffs_update(pvp, 1)) != 0)
5591 * If that cleared dependencies, go on to next.
5593 if (dap != LIST_FIRST(diraddhdp))
5595 if (dap->da_state & MKDIR_PARENT)
5596 panic("flush_pagedep_deps: MKDIR_PARENT");
5599 * A newly allocated directory must have its "." and
5600 * ".." entries written out before its name can be
5601 * committed in its parent. We do not want or need
5602 * the full semantics of a synchronous ffs_syncvnode as
5603 * that may end up here again, once for each directory
5604 * level in the filesystem. Instead, we push the blocks
5605 * and wait for them to clear. We have to fsync twice
5606 * because the first call may choose to defer blocks
5607 * that still have dependencies, but deferral will
5608 * happen at most once.
5610 inum = dap->da_newinum;
5611 if (dap->da_state & MKDIR_BODY) {
5613 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
5616 if ((error=ffs_syncvnode(vp, MNT_NOWAIT)) ||
5617 (error=ffs_syncvnode(vp, MNT_NOWAIT))) {
5624 * If first block is still dirty with a D_MKDIR
5625 * dependency then it needs to be written now.
5629 bp = gbincore(&vp->v_bufobj, 0);
5631 break; /* First block not present */
5632 error = BUF_LOCK(bp,
5638 if (error == ENOLCK)
5639 continue; /* Slept, retry */
5642 if ((bp->b_flags & B_DELWRI) == 0) {
5644 break; /* Buffer not dirty */
5646 for (wk = LIST_FIRST(&bp->b_dep);
5648 wk = LIST_NEXT(wk, wk_list))
5649 if (wk->wk_type == D_MKDIR)
5652 BUF_UNLOCK(bp); /* Dependency gone */
5655 * D_MKDIR dependency remains,
5656 * must write buffer to stable
5669 break; /* Flushing of first block failed */
5672 * If that cleared dependencies, go on to next.
5674 if (dap != LIST_FIRST(diraddhdp))
5676 if (dap->da_state & MKDIR_BODY)
5677 panic("flush_pagedep_deps: MKDIR_BODY");
5680 * Flush the inode on which the directory entry depends.
5681 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
5682 * the only remaining dependency is that the updated inode
5683 * count must get pushed to disk. The inode has already
5684 * been pushed into its inode buffer (via VOP_UPDATE) at
5685 * the time of the reference count change. So we need only
5686 * locate that buffer, ensure that there will be no rollback
5687 * caused by a bitmap dependency, then write the inode buffer.
5690 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
5691 panic("flush_pagedep_deps: lost inode");
5693 * If the inode still has bitmap dependencies,
5694 * push them to disk.
5696 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5697 bp = inodedep->id_buf;
5698 bp = getdirtybuf(bp, &lk, MNT_WAIT);
5702 if ((error = bwrite(bp)) != 0)
5705 if (dap != LIST_FIRST(diraddhdp))
5709 * If the inode is still sitting in a buffer waiting
5710 * to be written, push it to disk.
5713 if ((error = bread(ump->um_devvp,
5714 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
5715 (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) {
5719 if ((error = bwrite(bp)) != 0)
5723 * If we have failed to get rid of all the dependencies
5724 * then something is seriously wrong.
5726 if (dap == LIST_FIRST(diraddhdp))
5727 panic("flush_pagedep_deps: flush failed");
5735 * A large burst of file addition or deletion activity can drive the
5736 * memory load excessively high. First attempt to slow things down
5737 * using the techniques below. If that fails, this routine requests
5738 * the offending operations to fall back to running synchronously
5739 * until the memory load returns to a reasonable level.
5742 softdep_slowdown(vp)
5745 int max_softdeps_hard;
5748 max_softdeps_hard = max_softdeps * 11 / 10;
5749 if (num_dirrem < max_softdeps_hard / 2 &&
5750 num_inodedep < max_softdeps_hard &&
5751 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps) {
5755 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps)
5757 stat_sync_limit_hit += 1;
5763 * Called by the allocation routines when they are about to fail
5764 * in the hope that we can free up some disk space.
5766 * First check to see if the work list has anything on it. If it has,
5767 * clean up entries until we successfully free some space. Because this
5768 * process holds inodes locked, we cannot handle any remove requests
5769 * that might block on a locked inode as that could lead to deadlock.
5770 * If the worklist yields no free space, encourage the syncer daemon
5771 * to help us. In no event will we try for longer than tickdelay seconds.
5774 softdep_request_cleanup(fs, vp)
5778 struct ufsmount *ump;
5780 ufs2_daddr_t needed;
5783 ump = VTOI(vp)->i_ump;
5784 mtx_assert(UFS_MTX(ump), MA_OWNED);
5785 needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize;
5786 starttime = time_second + tickdelay;
5788 * If we are being called because of a process doing a
5789 * copy-on-write, then it is not safe to update the vnode
5790 * as we may recurse into the copy-on-write routine.
5792 if (!(curthread->td_pflags & TDP_COWINPROGRESS)) {
5794 error = ffs_update(vp, 1);
5799 while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) {
5800 if (time_second > starttime)
5804 if (ump->softdep_on_worklist > 0 &&
5805 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT) != -1) {
5806 stat_worklist_push += 1;
5811 request_cleanup(UFSTOVFS(ump), FLUSH_REMOVE_WAIT);
5819 * If memory utilization has gotten too high, deliberately slow things
5820 * down and speed up the I/O processing.
5822 extern struct thread *syncertd;
5824 request_cleanup(mp, resource)
5828 struct thread *td = curthread;
5829 struct ufsmount *ump;
5831 mtx_assert(&lk, MA_OWNED);
5833 * We never hold up the filesystem syncer or buf daemon.
5835 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
5839 * First check to see if the work list has gotten backlogged.
5840 * If it has, co-opt this process to help clean up two entries.
5841 * Because this process may hold inodes locked, we cannot
5842 * handle any remove requests that might block on a locked
5843 * inode as that could lead to deadlock. We set TDP_SOFTDEP
5844 * to avoid recursively processing the worklist.
5846 if (ump->softdep_on_worklist > max_softdeps / 10) {
5847 td->td_pflags |= TDP_SOFTDEP;
5848 process_worklist_item(mp, LK_NOWAIT);
5849 process_worklist_item(mp, LK_NOWAIT);
5850 td->td_pflags &= ~TDP_SOFTDEP;
5851 stat_worklist_push += 2;
5855 * Next, we attempt to speed up the syncer process. If that
5856 * is successful, then we allow the process to continue.
5858 if (softdep_speedup() && resource != FLUSH_REMOVE_WAIT)
5861 * If we are resource constrained on inode dependencies, try
5862 * flushing some dirty inodes. Otherwise, we are constrained
5863 * by file deletions, so try accelerating flushes of directories
5864 * with removal dependencies. We would like to do the cleanup
5865 * here, but we probably hold an inode locked at this point and
5866 * that might deadlock against one that we try to clean. So,
5867 * the best that we can do is request the syncer daemon to do
5868 * the cleanup for us.
5873 stat_ino_limit_push += 1;
5874 req_clear_inodedeps += 1;
5875 stat_countp = &stat_ino_limit_hit;
5879 case FLUSH_REMOVE_WAIT:
5880 stat_blk_limit_push += 1;
5881 req_clear_remove += 1;
5882 stat_countp = &stat_blk_limit_hit;
5886 panic("request_cleanup: unknown type");
5889 * Hopefully the syncer daemon will catch up and awaken us.
5890 * We wait at most tickdelay before proceeding in any case.
5893 if (callout_pending(&softdep_callout) == FALSE)
5894 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
5897 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
5903 * Awaken processes pausing in request_cleanup and clear proc_waiting
5904 * to indicate that there is no longer a timer running.
5912 * The callout_ API has acquired mtx and will hold it around this
5916 wakeup_one(&proc_waiting);
5917 if (proc_waiting > 0)
5918 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
5923 * Flush out a directory with at least one removal dependency in an effort to
5924 * reduce the number of dirrem, freefile, and freeblks dependency structures.
5930 struct pagedep_hashhead *pagedephd;
5931 struct pagedep *pagedep;
5932 static int next = 0;
5938 mtx_assert(&lk, MA_OWNED);
5940 for (cnt = 0; cnt < pagedep_hash; cnt++) {
5941 pagedephd = &pagedep_hashtbl[next++];
5942 if (next >= pagedep_hash)
5944 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
5945 if (LIST_EMPTY(&pagedep->pd_dirremhd))
5947 mp = pagedep->pd_list.wk_mp;
5948 ino = pagedep->pd_ino;
5949 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5952 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
5953 FFSV_FORCEINSMQ))) {
5954 softdep_error("clear_remove: vget", error);
5955 vn_finished_write(mp);
5959 if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
5960 softdep_error("clear_remove: fsync", error);
5965 vn_finished_write(mp);
5973 * Clear out a block of dirty inodes in an effort to reduce
5974 * the number of inodedep dependency structures.
5980 struct inodedep_hashhead *inodedephd;
5981 struct inodedep *inodedep;
5982 static int next = 0;
5987 ino_t firstino, lastino, ino;
5989 mtx_assert(&lk, MA_OWNED);
5991 * Pick a random inode dependency to be cleared.
5992 * We will then gather up all the inodes in its block
5993 * that have dependencies and flush them out.
5995 for (cnt = 0; cnt < inodedep_hash; cnt++) {
5996 inodedephd = &inodedep_hashtbl[next++];
5997 if (next >= inodedep_hash)
5999 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
6002 if (inodedep == NULL)
6004 fs = inodedep->id_fs;
6005 mp = inodedep->id_list.wk_mp;
6007 * Find the last inode in the block with dependencies.
6009 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
6010 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
6011 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
6014 * Asynchronously push all but the last inode with dependencies.
6015 * Synchronously push the last inode with dependencies to ensure
6016 * that the inode block gets written to free up the inodedeps.
6018 for (ino = firstino; ino <= lastino; ino++) {
6019 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
6021 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
6024 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
6025 FFSV_FORCEINSMQ)) != 0) {
6026 softdep_error("clear_inodedeps: vget", error);
6027 vn_finished_write(mp);
6031 if (ino == lastino) {
6032 if ((error = ffs_syncvnode(vp, MNT_WAIT)))
6033 softdep_error("clear_inodedeps: fsync1", error);
6035 if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
6036 softdep_error("clear_inodedeps: fsync2", error);
6042 vn_finished_write(mp);
6048 * Function to determine if the buffer has outstanding dependencies
6049 * that will cause a roll-back if the buffer is written. If wantcount
6050 * is set, return number of dependencies, otherwise just yes or no.
6053 softdep_count_dependencies(bp, wantcount)
6057 struct worklist *wk;
6058 struct inodedep *inodedep;
6059 struct indirdep *indirdep;
6060 struct allocindir *aip;
6061 struct pagedep *pagedep;
6067 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6068 switch (wk->wk_type) {
6071 inodedep = WK_INODEDEP(wk);
6072 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
6073 /* bitmap allocation dependency */
6078 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
6079 /* direct block pointer dependency */
6084 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
6085 /* direct block pointer dependency */
6093 indirdep = WK_INDIRDEP(wk);
6095 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
6096 /* indirect block pointer dependency */
6104 pagedep = WK_PAGEDEP(wk);
6105 for (i = 0; i < DAHASHSZ; i++) {
6107 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
6108 /* directory entry dependency */
6120 /* never a dependency on these blocks */
6124 panic("softdep_check_for_rollback: Unexpected type %s",
6125 TYPENAME(wk->wk_type));
6135 * Acquire exclusive access to a buffer.
6136 * Must be called with a locked mtx parameter.
6137 * Return acquired buffer or NULL on failure.
6140 getdirtybuf(bp, mtx, waitfor)
6147 mtx_assert(mtx, MA_OWNED);
6148 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
6149 if (waitfor != MNT_WAIT)
6151 error = BUF_LOCK(bp,
6152 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx);
6154 * Even if we sucessfully acquire bp here, we have dropped
6155 * mtx, which may violates our guarantee.
6159 else if (error != ENOLCK)
6160 panic("getdirtybuf: inconsistent lock: %d", error);
6164 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6165 if (mtx == &lk && waitfor == MNT_WAIT) {
6167 BO_LOCK(bp->b_bufobj);
6169 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6170 bp->b_vflags |= BV_BKGRDWAIT;
6171 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj),
6172 PRIBIO | PDROP, "getbuf", 0);
6174 BO_UNLOCK(bp->b_bufobj);
6179 if (waitfor != MNT_WAIT)
6182 * The mtx argument must be bp->b_vp's mutex in
6185 #ifdef DEBUG_VFS_LOCKS
6186 if (bp->b_vp->v_type != VCHR)
6187 ASSERT_VI_LOCKED(bp->b_vp, "getdirtybuf");
6189 bp->b_vflags |= BV_BKGRDWAIT;
6190 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0);
6193 if ((bp->b_flags & B_DELWRI) == 0) {
6203 * Check if it is safe to suspend the file system now. On entry,
6204 * the vnode interlock for devvp should be held. Return 0 with
6205 * the mount interlock held if the file system can be suspended now,
6206 * otherwise return EAGAIN with the mount interlock held.
6209 softdep_check_suspend(struct mount *mp,
6210 struct vnode *devvp,
6212 int softdep_accdeps,
6213 int secondary_writes,
6214 int secondary_accwrites)
6217 struct ufsmount *ump;
6220 ASSERT_VI_LOCKED(devvp, "softdep_check_suspend");
6222 bo = &devvp->v_bufobj;
6225 if (!TRY_ACQUIRE_LOCK(&lk)) {
6232 if (!MNT_ITRYLOCK(mp)) {
6240 if (mp->mnt_secondary_writes != 0) {
6243 msleep(&mp->mnt_secondary_writes,
6245 (PUSER - 1) | PDROP, "secwr", 0);
6253 * Reasons for needing more work before suspend:
6254 * - Dirty buffers on devvp.
6255 * - Softdep activity occurred after start of vnode sync loop
6256 * - Secondary writes occurred after start of vnode sync loop
6259 if (bo->bo_numoutput > 0 ||
6260 bo->bo_dirty.bv_cnt > 0 ||
6261 softdep_deps != 0 ||
6262 ump->softdep_deps != 0 ||
6263 softdep_accdeps != ump->softdep_accdeps ||
6264 secondary_writes != 0 ||
6265 mp->mnt_secondary_writes != 0 ||
6266 secondary_accwrites != mp->mnt_secondary_accwrites)
6275 * Get the number of dependency structures for the file system, both
6276 * the current number and the total number allocated. These will
6277 * later be used to detect that softdep processing has occurred.
6280 softdep_get_depcounts(struct mount *mp,
6282 int *softdep_accdepsp)
6284 struct ufsmount *ump;
6288 *softdep_depsp = ump->softdep_deps;
6289 *softdep_accdepsp = ump->softdep_accdeps;
6294 * Wait for pending output on a vnode to complete.
6295 * Must be called with vnode lock and interlock locked.
6297 * XXX: Should just be a call to bufobj_wwait().
6303 ASSERT_VOP_LOCKED(vp, "drain_output");
6304 ASSERT_VI_LOCKED(vp, "drain_output");
6306 while (vp->v_bufobj.bo_numoutput) {
6307 vp->v_bufobj.bo_flag |= BO_WWAIT;
6308 msleep((caddr_t)&vp->v_bufobj.bo_numoutput,
6309 VI_MTX(vp), PRIBIO + 1, "drainvp", 0);
6314 * Called whenever a buffer that is being invalidated or reallocated
6315 * contains dependencies. This should only happen if an I/O error has
6316 * occurred. The routine is called with the buffer locked.
6319 softdep_deallocate_dependencies(bp)
6323 if ((bp->b_ioflags & BIO_ERROR) == 0)
6324 panic("softdep_deallocate_dependencies: dangling deps");
6325 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
6326 panic("softdep_deallocate_dependencies: unrecovered I/O error");
6330 * Function to handle asynchronous write errors in the filesystem.
6333 softdep_error(func, error)
6338 /* XXX should do something better! */
6339 printf("%s: got error %d while accessing filesystem\n", func, error);
6344 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps)
6346 struct inodedep_hashhead *inodedephd;
6347 struct inodedep *inodedep;
6351 fs = have_addr ? (struct fs *)addr : NULL;
6352 for (cnt = 0; cnt < inodedep_hash; cnt++) {
6353 inodedephd = &inodedep_hashtbl[cnt];
6354 LIST_FOREACH(inodedep, inodedephd, id_hash) {
6355 if (fs != NULL && fs != inodedep->id_fs)
6357 db_printf("%p fs %p st %x ino %jd inoblk %jd\n",
6358 inodedep, inodedep->id_fs, inodedep->id_state,
6359 (intmax_t)inodedep->id_ino,
6360 (intmax_t)fsbtodb(inodedep->id_fs,
6361 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)));
6368 #endif /* SOFTUPDATES */