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 the DEBUG flag provides.
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>
88 softdep_flushfiles(oldmnt, flags, td)
94 panic("softdep_flushfiles called");
98 softdep_mount(devvp, mp, fs, cred)
116 softdep_uninitialize()
123 softdep_setup_inomapdep(bp, ip, newinum)
129 panic("softdep_setup_inomapdep called");
133 softdep_setup_blkmapdep(bp, mp, newblkno)
136 ufs2_daddr_t newblkno;
139 panic("softdep_setup_blkmapdep called");
143 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
146 ufs2_daddr_t newblkno;
147 ufs2_daddr_t oldblkno;
153 panic("softdep_setup_allocdirect called");
157 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
160 ufs2_daddr_t newblkno;
161 ufs2_daddr_t oldblkno;
167 panic("softdep_setup_allocext called");
171 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
176 ufs2_daddr_t newblkno;
177 ufs2_daddr_t oldblkno;
181 panic("softdep_setup_allocindir_page called");
185 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
190 ufs2_daddr_t newblkno;
193 panic("softdep_setup_allocindir_meta called");
197 softdep_setup_freeblocks(ip, length, flags)
203 panic("softdep_setup_freeblocks called");
207 softdep_freefile(pvp, ino, mode)
213 panic("softdep_freefile called");
217 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
222 struct buf *newdirbp;
226 panic("softdep_setup_directory_add called");
230 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
238 panic("softdep_change_directoryentry_offset called");
242 softdep_setup_remove(bp, dp, ip, isrmdir)
249 panic("softdep_setup_remove called");
253 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
261 panic("softdep_setup_directory_change called");
265 softdep_change_linkcnt(ip)
269 panic("softdep_change_linkcnt called");
273 softdep_load_inodeblock(ip)
277 panic("softdep_load_inodeblock called");
281 softdep_update_inodeblock(ip, bp, waitfor)
287 panic("softdep_update_inodeblock called");
292 struct vnode *vp; /* the "in_core" copy of the inode */
299 softdep_fsync_mountdev(vp)
307 softdep_flushworklist(oldmnt, countp, td)
308 struct mount *oldmnt;
318 softdep_sync_metadata(struct vnode *vp)
329 panic("softdep_slowdown called");
333 softdep_releasefile(ip)
334 struct inode *ip; /* inode with the zero effective link count */
337 panic("softdep_releasefile called");
341 softdep_request_cleanup(fs, vp)
350 softdep_check_suspend(struct mount *mp,
354 int secondary_writes,
355 int secondary_accwrites)
361 (void) softdep_accdeps;
363 bo = &devvp->v_bufobj;
364 ASSERT_BO_LOCKED(bo);
367 while (mp->mnt_secondary_writes != 0) {
369 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp),
370 (PUSER - 1) | PDROP, "secwr", 0);
376 * Reasons for needing more work before suspend:
377 * - Dirty buffers on devvp.
378 * - Secondary writes occurred after start of vnode sync loop
381 if (bo->bo_numoutput > 0 ||
382 bo->bo_dirty.bv_cnt > 0 ||
383 secondary_writes != 0 ||
384 mp->mnt_secondary_writes != 0 ||
385 secondary_accwrites != mp->mnt_secondary_accwrites)
392 softdep_get_depcounts(struct mount *mp,
394 int *softdepactiveaccp)
398 *softdepactiveaccp = 0;
403 * These definitions need to be adapted to the system to which
404 * this file is being ported.
407 * malloc types defined for the softdep system.
409 static MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies");
410 static MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies");
411 static MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation");
412 static MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map");
413 static MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode");
414 static MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies");
415 static MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block");
416 static MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode");
417 static MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode");
418 static MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated");
419 static MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry");
420 static MALLOC_DEFINE(M_MKDIR, "mkdir","New directory");
421 static MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted");
422 static MALLOC_DEFINE(M_NEWDIRBLK, "newdirblk","Unclaimed new directory block");
423 static MALLOC_DEFINE(M_SAVEDINO, "savedino","Saved inodes");
425 #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE)
430 #define D_BMSAFEMAP 3
431 #define D_ALLOCDIRECT 4
433 #define D_ALLOCINDIR 6
440 #define D_NEWDIRBLK 13
441 #define D_LAST D_NEWDIRBLK
444 * translate from workitem type to memory type
445 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
447 static struct malloc_type *memtype[] = {
464 #define DtoM(type) (memtype[type])
467 * Names of malloc types.
469 #define TYPENAME(type) \
470 ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???")
472 * End system adaptation definitions.
476 * Forward declarations.
478 struct inodedep_hashhead;
479 struct newblk_hashhead;
480 struct pagedep_hashhead;
483 * Internal function prototypes.
485 static void softdep_error(char *, int);
486 static void drain_output(struct vnode *);
487 static struct buf *getdirtybuf(struct buf *, struct mtx *, int);
488 static void clear_remove(struct thread *);
489 static void clear_inodedeps(struct thread *);
490 static int flush_pagedep_deps(struct vnode *, struct mount *,
492 static int flush_inodedep_deps(struct mount *, ino_t);
493 static int flush_deplist(struct allocdirectlst *, int, int *);
494 static int handle_written_filepage(struct pagedep *, struct buf *);
495 static void diradd_inode_written(struct diradd *, struct inodedep *);
496 static int handle_written_inodeblock(struct inodedep *, struct buf *);
497 static void handle_allocdirect_partdone(struct allocdirect *);
498 static void handle_allocindir_partdone(struct allocindir *);
499 static void initiate_write_filepage(struct pagedep *, struct buf *);
500 static void handle_written_mkdir(struct mkdir *, int);
501 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
502 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
503 static void handle_workitem_freefile(struct freefile *);
504 static void handle_workitem_remove(struct dirrem *, struct vnode *);
505 static struct dirrem *newdirrem(struct buf *, struct inode *,
506 struct inode *, int, struct dirrem **);
507 static void free_diradd(struct diradd *);
508 static void free_allocindir(struct allocindir *, struct inodedep *);
509 static void free_newdirblk(struct newdirblk *);
510 static int indir_trunc(struct freeblks *, ufs2_daddr_t, int, ufs_lbn_t,
512 static void deallocate_dependencies(struct buf *, struct inodedep *);
513 static void free_allocdirect(struct allocdirectlst *,
514 struct allocdirect *, int);
515 static int check_inode_unwritten(struct inodedep *);
516 static int free_inodedep(struct inodedep *);
517 static void handle_workitem_freeblocks(struct freeblks *, int);
518 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
519 static void setup_allocindir_phase2(struct buf *, struct inode *,
520 struct allocindir *);
521 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
523 static void handle_workitem_freefrag(struct freefrag *);
524 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long);
525 static void allocdirect_merge(struct allocdirectlst *,
526 struct allocdirect *, struct allocdirect *);
527 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *);
528 static int newblk_find(struct newblk_hashhead *, struct fs *, ufs2_daddr_t,
530 static int newblk_lookup(struct fs *, ufs2_daddr_t, int, struct newblk **);
531 static int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t,
533 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
534 static int pagedep_lookup(struct inode *, ufs_lbn_t, int, struct pagedep **);
535 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
536 struct mount *mp, int, struct pagedep **);
537 static void pause_timer(void *);
538 static int request_cleanup(struct mount *, int);
539 static int process_worklist_item(struct mount *, int);
540 static void add_to_worklist(struct worklist *);
541 static void softdep_flush(void);
542 static int softdep_speedup(void);
545 * Exported softdep operations.
547 static void softdep_disk_io_initiation(struct buf *);
548 static void softdep_disk_write_complete(struct buf *);
549 static void softdep_deallocate_dependencies(struct buf *);
550 static int softdep_count_dependencies(struct buf *bp, int);
552 static struct mtx lk;
553 MTX_SYSINIT(softdep_lock, &lk, "Softdep Lock", MTX_DEF);
555 #define TRY_ACQUIRE_LOCK(lk) mtx_trylock(lk)
556 #define ACQUIRE_LOCK(lk) mtx_lock(lk)
557 #define FREE_LOCK(lk) mtx_unlock(lk)
559 #define BUF_AREC(bp) ((bp)->b_lock.lock_object.lo_flags |= LO_RECURSABLE)
560 #define BUF_NOREC(bp) ((bp)->b_lock.lock_object.lo_flags &= ~LO_RECURSABLE)
563 * Worklist queue management.
564 * These routines require that the lock be held.
566 #ifndef /* NOT */ DEBUG
567 #define WORKLIST_INSERT(head, item) do { \
568 (item)->wk_state |= ONWORKLIST; \
569 LIST_INSERT_HEAD(head, item, wk_list); \
571 #define WORKLIST_REMOVE(item) do { \
572 (item)->wk_state &= ~ONWORKLIST; \
573 LIST_REMOVE(item, wk_list); \
576 static void worklist_insert(struct workhead *, struct worklist *);
577 static void worklist_remove(struct worklist *);
579 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
580 #define WORKLIST_REMOVE(item) worklist_remove(item)
583 worklist_insert(head, item)
584 struct workhead *head;
585 struct worklist *item;
588 mtx_assert(&lk, MA_OWNED);
589 if (item->wk_state & ONWORKLIST)
590 panic("worklist_insert: already on list");
591 item->wk_state |= ONWORKLIST;
592 LIST_INSERT_HEAD(head, item, wk_list);
596 worklist_remove(item)
597 struct worklist *item;
600 mtx_assert(&lk, MA_OWNED);
601 if ((item->wk_state & ONWORKLIST) == 0)
602 panic("worklist_remove: not on list");
603 item->wk_state &= ~ONWORKLIST;
604 LIST_REMOVE(item, wk_list);
609 * Routines for tracking and managing workitems.
611 static void workitem_free(struct worklist *, int);
612 static void workitem_alloc(struct worklist *, int, struct mount *);
614 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type))
617 workitem_free(item, type)
618 struct worklist *item;
621 struct ufsmount *ump;
622 mtx_assert(&lk, MA_OWNED);
625 if (item->wk_state & ONWORKLIST)
626 panic("workitem_free: still on list");
627 if (item->wk_type != type)
628 panic("workitem_free: type mismatch");
630 ump = VFSTOUFS(item->wk_mp);
631 if (--ump->softdep_deps == 0 && ump->softdep_req)
632 wakeup(&ump->softdep_deps);
633 free(item, DtoM(type));
637 workitem_alloc(item, type, mp)
638 struct worklist *item;
642 item->wk_type = type;
646 VFSTOUFS(mp)->softdep_deps++;
647 VFSTOUFS(mp)->softdep_accdeps++;
652 * Workitem queue management
654 static int max_softdeps; /* maximum number of structs before slowdown */
655 static int maxindirdeps = 50; /* max number of indirdeps before slowdown */
656 static int tickdelay = 2; /* number of ticks to pause during slowdown */
657 static int proc_waiting; /* tracks whether we have a timeout posted */
658 static int *stat_countp; /* statistic to count in proc_waiting timeout */
659 static struct callout softdep_callout;
660 static int req_pending;
661 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
662 #define FLUSH_INODES 1
663 static int req_clear_remove; /* syncer process flush some freeblks */
664 #define FLUSH_REMOVE 2
665 #define FLUSH_REMOVE_WAIT 3
666 static long num_freeblkdep; /* number of freeblks workitems allocated */
671 static int stat_worklist_push; /* number of worklist cleanups */
672 static int stat_blk_limit_push; /* number of times block limit neared */
673 static int stat_ino_limit_push; /* number of times inode limit neared */
674 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
675 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
676 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
677 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
678 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
679 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
680 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
682 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
683 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
684 SYSCTL_INT(_debug, OID_AUTO, maxindirdeps, CTLFLAG_RW, &maxindirdeps, 0, "");
685 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
686 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
687 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
688 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
689 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
690 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
691 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
692 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
693 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
694 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
695 /* SYSCTL_INT(_debug, OID_AUTO, worklist_num, CTLFLAG_RD, &softdep_on_worklist, 0, ""); */
697 SYSCTL_DECL(_vfs_ffs);
699 static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */
700 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
701 &compute_summary_at_mount, 0, "Recompute summary at mount");
703 static struct proc *softdepproc;
704 static struct kproc_desc softdep_kp = {
709 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
717 struct ufsmount *ump;
723 td->td_pflags |= TDP_NORUNNINGBUF;
726 kproc_suspend_check(softdepproc);
727 vfslocked = VFS_LOCK_GIANT((struct mount *)NULL);
730 * If requested, try removing inode or removal dependencies.
732 if (req_clear_inodedeps) {
734 req_clear_inodedeps -= 1;
735 wakeup_one(&proc_waiting);
737 if (req_clear_remove) {
739 req_clear_remove -= 1;
740 wakeup_one(&proc_waiting);
743 VFS_UNLOCK_GIANT(vfslocked);
745 mtx_lock(&mountlist_mtx);
746 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
747 nmp = TAILQ_NEXT(mp, mnt_list);
748 if ((mp->mnt_flag & MNT_SOFTDEP) == 0)
750 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
752 vfslocked = VFS_LOCK_GIANT(mp);
753 softdep_process_worklist(mp, 0);
755 remaining += ump->softdep_on_worklist -
756 ump->softdep_on_worklist_inprogress;
757 VFS_UNLOCK_GIANT(vfslocked);
758 mtx_lock(&mountlist_mtx);
759 nmp = TAILQ_NEXT(mp, mnt_list);
762 mtx_unlock(&mountlist_mtx);
767 msleep(&req_pending, &lk, PVM, "sdflush", hz);
774 softdep_speedup(void)
777 mtx_assert(&lk, MA_OWNED);
778 if (req_pending == 0) {
780 wakeup(&req_pending);
783 return speedup_syncer();
787 * Add an item to the end of the work queue.
788 * This routine requires that the lock be held.
789 * This is the only routine that adds items to the list.
790 * The following routine is the only one that removes items
791 * and does so in order from first to last.
797 struct ufsmount *ump;
799 mtx_assert(&lk, MA_OWNED);
800 ump = VFSTOUFS(wk->wk_mp);
801 if (wk->wk_state & ONWORKLIST)
802 panic("add_to_worklist: already on list");
803 wk->wk_state |= ONWORKLIST;
804 if (LIST_EMPTY(&ump->softdep_workitem_pending))
805 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
807 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
808 ump->softdep_worklist_tail = wk;
809 ump->softdep_on_worklist += 1;
813 * Process that runs once per second to handle items in the background queue.
815 * Note that we ensure that everything is done in the order in which they
816 * appear in the queue. The code below depends on this property to ensure
817 * that blocks of a file are freed before the inode itself is freed. This
818 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
819 * until all the old ones have been purged from the dependency lists.
822 softdep_process_worklist(mp, full)
826 struct thread *td = curthread;
827 int cnt, matchcnt, loopcount;
828 struct ufsmount *ump;
831 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
833 * Record the process identifier of our caller so that we can give
834 * this process preferential treatment in request_cleanup below.
840 starttime = time_second;
841 while (ump->softdep_on_worklist > 0) {
842 if ((cnt = process_worklist_item(mp, 0)) == -1)
847 * If requested, try removing inode or removal dependencies.
849 if (req_clear_inodedeps) {
851 req_clear_inodedeps -= 1;
852 wakeup_one(&proc_waiting);
854 if (req_clear_remove) {
856 req_clear_remove -= 1;
857 wakeup_one(&proc_waiting);
860 * We do not generally want to stop for buffer space, but if
861 * we are really being a buffer hog, we will stop and wait.
863 if (loopcount++ % 128 == 0) {
870 * Never allow processing to run for more than one
871 * second. Otherwise the other mountpoints may get
872 * excessively backlogged.
874 if (!full && starttime != time_second) {
884 * Process one item on the worklist.
887 process_worklist_item(mp, flags)
891 struct worklist *wk, *wkend;
892 struct ufsmount *ump;
896 mtx_assert(&lk, MA_OWNED);
897 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
899 * If we are being called because of a process doing a
900 * copy-on-write, then it is not safe to write as we may
901 * recurse into the copy-on-write routine.
903 if (curthread->td_pflags & TDP_COWINPROGRESS)
906 * Normally we just process each item on the worklist in order.
907 * However, if we are in a situation where we cannot lock any
908 * inodes, we have to skip over any dirrem requests whose
909 * vnodes are resident and locked.
913 LIST_FOREACH(wk, &ump->softdep_workitem_pending, wk_list) {
914 if (wk->wk_state & INPROGRESS)
916 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
918 wk->wk_state |= INPROGRESS;
919 ump->softdep_on_worklist_inprogress++;
921 ffs_vgetf(mp, WK_DIRREM(wk)->dm_oldinum,
922 LK_NOWAIT | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ);
924 wk->wk_state &= ~INPROGRESS;
925 ump->softdep_on_worklist_inprogress--;
932 * Remove the item to be processed. If we are removing the last
933 * item on the list, we need to recalculate the tail pointer.
934 * As this happens rarely and usually when the list is short,
935 * we just run down the list to find it rather than tracking it
939 if (wk == ump->softdep_worklist_tail) {
940 LIST_FOREACH(wkend, &ump->softdep_workitem_pending, wk_list)
941 if (LIST_NEXT(wkend, wk_list) == NULL)
943 ump->softdep_worklist_tail = wkend;
945 ump->softdep_on_worklist -= 1;
947 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
948 panic("process_worklist_item: suspended filesystem");
950 switch (wk->wk_type) {
953 /* removal of a directory entry */
954 handle_workitem_remove(WK_DIRREM(wk), vp);
958 /* releasing blocks and/or fragments from a file */
959 handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT);
963 /* releasing a fragment when replaced as a file grows */
964 handle_workitem_freefrag(WK_FREEFRAG(wk));
968 /* releasing an inode when its link count drops to 0 */
969 handle_workitem_freefile(WK_FREEFILE(wk));
973 panic("%s_process_worklist: Unknown type %s",
974 "softdep", TYPENAME(wk->wk_type));
977 vn_finished_secondary_write(mp);
983 * Move dependencies from one buffer to another.
986 softdep_move_dependencies(oldbp, newbp)
990 struct worklist *wk, *wktail;
992 if (!LIST_EMPTY(&newbp->b_dep))
993 panic("softdep_move_dependencies: need merge code");
996 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
997 LIST_REMOVE(wk, wk_list);
999 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1001 LIST_INSERT_AFTER(wktail, wk, wk_list);
1008 * Purge the work list of all items associated with a particular mount point.
1011 softdep_flushworklist(oldmnt, countp, td)
1012 struct mount *oldmnt;
1016 struct vnode *devvp;
1017 int count, error = 0;
1018 struct ufsmount *ump;
1021 * Alternately flush the block device associated with the mount
1022 * point and process any dependencies that the flushing
1023 * creates. We continue until no more worklist dependencies
1027 ump = VFSTOUFS(oldmnt);
1028 devvp = ump->um_devvp;
1029 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1031 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1032 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1033 VOP_UNLOCK(devvp, 0);
1041 softdep_waitidle(struct mount *mp)
1043 struct ufsmount *ump;
1049 for (i = 0; i < 10 && ump->softdep_deps; i++) {
1050 ump->softdep_req = 1;
1051 if (ump->softdep_on_worklist)
1052 panic("softdep_waitidle: work added after flush.");
1053 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1);
1055 ump->softdep_req = 0;
1060 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1068 * Flush all vnodes and worklist items associated with a specified mount point.
1071 softdep_flushfiles(oldmnt, flags, td)
1072 struct mount *oldmnt;
1076 int error, depcount, loopcnt, retry_flush_count, retry;
1079 retry_flush_count = 3;
1084 * Alternately flush the vnodes associated with the mount
1085 * point and process any dependencies that the flushing
1086 * creates. In theory, this loop can happen at most twice,
1087 * but we give it a few extra just to be sure.
1089 for (; loopcnt > 0; loopcnt--) {
1091 * Do another flush in case any vnodes were brought in
1092 * as part of the cleanup operations.
1094 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
1096 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
1101 * If we are unmounting then it is an error to fail. If we
1102 * are simply trying to downgrade to read-only, then filesystem
1103 * activity can keep us busy forever, so we just fail with EBUSY.
1106 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
1107 panic("softdep_flushfiles: looping");
1111 error = softdep_waitidle(oldmnt);
1113 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
1116 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0,
1117 ("softdep_flushfiles: !MNTK_NOINSMNTQ"));
1118 if (oldmnt->mnt_nvnodelistsize > 0) {
1119 if (--retry_flush_count > 0) {
1125 MNT_IUNLOCK(oldmnt);
1134 * Structure hashing.
1136 * There are three types of structures that can be looked up:
1137 * 1) pagedep structures identified by mount point, inode number,
1138 * and logical block.
1139 * 2) inodedep structures identified by mount point and inode number.
1140 * 3) newblk structures identified by mount point and
1141 * physical block number.
1143 * The "pagedep" and "inodedep" dependency structures are hashed
1144 * separately from the file blocks and inodes to which they correspond.
1145 * This separation helps when the in-memory copy of an inode or
1146 * file block must be replaced. It also obviates the need to access
1147 * an inode or file page when simply updating (or de-allocating)
1148 * dependency structures. Lookup of newblk structures is needed to
1149 * find newly allocated blocks when trying to associate them with
1150 * their allocdirect or allocindir structure.
1152 * The lookup routines optionally create and hash a new instance when
1153 * an existing entry is not found.
1155 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
1156 #define NODELAY 0x0002 /* cannot do background work */
1159 * Structures and routines associated with pagedep caching.
1161 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
1162 u_long pagedep_hash; /* size of hash table - 1 */
1163 #define PAGEDEP_HASH(mp, inum, lbn) \
1164 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
1168 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp)
1169 struct pagedep_hashhead *pagedephd;
1174 struct pagedep **pagedeppp;
1176 struct pagedep *pagedep;
1178 LIST_FOREACH(pagedep, pagedephd, pd_hash)
1179 if (ino == pagedep->pd_ino &&
1180 lbn == pagedep->pd_lbn &&
1181 mp == pagedep->pd_list.wk_mp)
1184 *pagedeppp = pagedep;
1185 if ((flags & DEPALLOC) != 0 &&
1186 (pagedep->pd_state & ONWORKLIST) == 0)
1194 * Look up a pagedep. Return 1 if found, 0 if not found or found
1195 * when asked to allocate but not associated with any buffer.
1196 * If not found, allocate if DEPALLOC flag is passed.
1197 * Found or allocated entry is returned in pagedeppp.
1198 * This routine must be called with splbio interrupts blocked.
1201 pagedep_lookup(ip, lbn, flags, pagedeppp)
1205 struct pagedep **pagedeppp;
1207 struct pagedep *pagedep;
1208 struct pagedep_hashhead *pagedephd;
1213 mtx_assert(&lk, MA_OWNED);
1214 mp = ITOV(ip)->v_mount;
1215 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
1217 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1218 if (*pagedeppp || (flags & DEPALLOC) == 0)
1221 pagedep = malloc(sizeof(struct pagedep),
1222 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
1223 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
1225 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1227 WORKITEM_FREE(pagedep, D_PAGEDEP);
1230 pagedep->pd_ino = ip->i_number;
1231 pagedep->pd_lbn = lbn;
1232 LIST_INIT(&pagedep->pd_dirremhd);
1233 LIST_INIT(&pagedep->pd_pendinghd);
1234 for (i = 0; i < DAHASHSZ; i++)
1235 LIST_INIT(&pagedep->pd_diraddhd[i]);
1236 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
1237 *pagedeppp = pagedep;
1242 * Structures and routines associated with inodedep caching.
1244 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
1245 static u_long inodedep_hash; /* size of hash table - 1 */
1246 static long num_inodedep; /* number of inodedep allocated */
1247 #define INODEDEP_HASH(fs, inum) \
1248 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
1251 inodedep_find(inodedephd, fs, inum, inodedeppp)
1252 struct inodedep_hashhead *inodedephd;
1255 struct inodedep **inodedeppp;
1257 struct inodedep *inodedep;
1259 LIST_FOREACH(inodedep, inodedephd, id_hash)
1260 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
1263 *inodedeppp = inodedep;
1271 * Look up an inodedep. Return 1 if found, 0 if not found.
1272 * If not found, allocate if DEPALLOC flag is passed.
1273 * Found or allocated entry is returned in inodedeppp.
1274 * This routine must be called with splbio interrupts blocked.
1277 inodedep_lookup(mp, inum, flags, inodedeppp)
1281 struct inodedep **inodedeppp;
1283 struct inodedep *inodedep;
1284 struct inodedep_hashhead *inodedephd;
1287 mtx_assert(&lk, MA_OWNED);
1288 fs = VFSTOUFS(mp)->um_fs;
1289 inodedephd = INODEDEP_HASH(fs, inum);
1291 if (inodedep_find(inodedephd, fs, inum, inodedeppp))
1293 if ((flags & DEPALLOC) == 0)
1296 * If we are over our limit, try to improve the situation.
1298 if (num_inodedep > max_softdeps && (flags & NODELAY) == 0)
1299 request_cleanup(mp, FLUSH_INODES);
1301 inodedep = malloc(sizeof(struct inodedep),
1302 M_INODEDEP, M_SOFTDEP_FLAGS);
1303 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
1305 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) {
1306 WORKITEM_FREE(inodedep, D_INODEDEP);
1310 inodedep->id_fs = fs;
1311 inodedep->id_ino = inum;
1312 inodedep->id_state = ALLCOMPLETE;
1313 inodedep->id_nlinkdelta = 0;
1314 inodedep->id_savedino1 = NULL;
1315 inodedep->id_savedsize = -1;
1316 inodedep->id_savedextsize = -1;
1317 inodedep->id_buf = NULL;
1318 LIST_INIT(&inodedep->id_pendinghd);
1319 LIST_INIT(&inodedep->id_inowait);
1320 LIST_INIT(&inodedep->id_bufwait);
1321 TAILQ_INIT(&inodedep->id_inoupdt);
1322 TAILQ_INIT(&inodedep->id_newinoupdt);
1323 TAILQ_INIT(&inodedep->id_extupdt);
1324 TAILQ_INIT(&inodedep->id_newextupdt);
1325 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1326 *inodedeppp = inodedep;
1331 * Structures and routines associated with newblk caching.
1333 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1334 u_long newblk_hash; /* size of hash table - 1 */
1335 #define NEWBLK_HASH(fs, inum) \
1336 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1339 newblk_find(newblkhd, fs, newblkno, newblkpp)
1340 struct newblk_hashhead *newblkhd;
1342 ufs2_daddr_t newblkno;
1343 struct newblk **newblkpp;
1345 struct newblk *newblk;
1347 LIST_FOREACH(newblk, newblkhd, nb_hash)
1348 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1359 * Look up a newblk. Return 1 if found, 0 if not found.
1360 * If not found, allocate if DEPALLOC flag is passed.
1361 * Found or allocated entry is returned in newblkpp.
1364 newblk_lookup(fs, newblkno, flags, newblkpp)
1366 ufs2_daddr_t newblkno;
1368 struct newblk **newblkpp;
1370 struct newblk *newblk;
1371 struct newblk_hashhead *newblkhd;
1373 newblkhd = NEWBLK_HASH(fs, newblkno);
1374 if (newblk_find(newblkhd, fs, newblkno, newblkpp))
1376 if ((flags & DEPALLOC) == 0)
1379 newblk = malloc(sizeof(struct newblk),
1380 M_NEWBLK, M_SOFTDEP_FLAGS);
1382 if (newblk_find(newblkhd, fs, newblkno, newblkpp)) {
1383 free(newblk, M_NEWBLK);
1386 newblk->nb_state = 0;
1388 newblk->nb_newblkno = newblkno;
1389 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1395 * Executed during filesystem system initialization before
1396 * mounting any filesystems.
1399 softdep_initialize()
1402 LIST_INIT(&mkdirlisthd);
1403 max_softdeps = desiredvnodes * 4;
1404 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1406 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1407 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1409 /* initialise bioops hack */
1410 bioops.io_start = softdep_disk_io_initiation;
1411 bioops.io_complete = softdep_disk_write_complete;
1412 bioops.io_deallocate = softdep_deallocate_dependencies;
1413 bioops.io_countdeps = softdep_count_dependencies;
1415 /* Initialize the callout with an mtx. */
1416 callout_init_mtx(&softdep_callout, &lk, 0);
1420 * Executed after all filesystems have been unmounted during
1421 * filesystem module unload.
1424 softdep_uninitialize()
1427 callout_drain(&softdep_callout);
1428 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash);
1429 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash);
1430 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash);
1434 * Called at mount time to notify the dependency code that a
1435 * filesystem wishes to use it.
1438 softdep_mount(devvp, mp, fs, cred)
1439 struct vnode *devvp;
1444 struct csum_total cstotal;
1445 struct ufsmount *ump;
1451 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
1452 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
1453 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
1459 LIST_INIT(&ump->softdep_workitem_pending);
1460 ump->softdep_worklist_tail = NULL;
1461 ump->softdep_on_worklist = 0;
1462 ump->softdep_deps = 0;
1464 * When doing soft updates, the counters in the
1465 * superblock may have gotten out of sync. Recomputation
1466 * can take a long time and can be deferred for background
1467 * fsck. However, the old behavior of scanning the cylinder
1468 * groups and recalculating them at mount time is available
1469 * by setting vfs.ffs.compute_summary_at_mount to one.
1471 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
1473 bzero(&cstotal, sizeof cstotal);
1474 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1475 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1476 fs->fs_cgsize, cred, &bp)) != 0) {
1480 cgp = (struct cg *)bp->b_data;
1481 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1482 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1483 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1484 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1485 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1489 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1490 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
1492 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1497 * Protecting the freemaps (or bitmaps).
1499 * To eliminate the need to execute fsck before mounting a filesystem
1500 * after a power failure, one must (conservatively) guarantee that the
1501 * on-disk copy of the bitmaps never indicate that a live inode or block is
1502 * free. So, when a block or inode is allocated, the bitmap should be
1503 * updated (on disk) before any new pointers. When a block or inode is
1504 * freed, the bitmap should not be updated until all pointers have been
1505 * reset. The latter dependency is handled by the delayed de-allocation
1506 * approach described below for block and inode de-allocation. The former
1507 * dependency is handled by calling the following procedure when a block or
1508 * inode is allocated. When an inode is allocated an "inodedep" is created
1509 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1510 * Each "inodedep" is also inserted into the hash indexing structure so
1511 * that any additional link additions can be made dependent on the inode
1514 * The ufs filesystem maintains a number of free block counts (e.g., per
1515 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1516 * in addition to the bitmaps. These counts are used to improve efficiency
1517 * during allocation and therefore must be consistent with the bitmaps.
1518 * There is no convenient way to guarantee post-crash consistency of these
1519 * counts with simple update ordering, for two main reasons: (1) The counts
1520 * and bitmaps for a single cylinder group block are not in the same disk
1521 * sector. If a disk write is interrupted (e.g., by power failure), one may
1522 * be written and the other not. (2) Some of the counts are located in the
1523 * superblock rather than the cylinder group block. So, we focus our soft
1524 * updates implementation on protecting the bitmaps. When mounting a
1525 * filesystem, we recompute the auxiliary counts from the bitmaps.
1529 * Called just after updating the cylinder group block to allocate an inode.
1532 softdep_setup_inomapdep(bp, ip, newinum)
1533 struct buf *bp; /* buffer for cylgroup block with inode map */
1534 struct inode *ip; /* inode related to allocation */
1535 ino_t newinum; /* new inode number being allocated */
1537 struct inodedep *inodedep;
1538 struct bmsafemap *bmsafemap;
1541 * Create a dependency for the newly allocated inode.
1542 * Panic if it already exists as something is seriously wrong.
1543 * Otherwise add it to the dependency list for the buffer holding
1544 * the cylinder group map from which it was allocated.
1547 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), newinum, DEPALLOC|NODELAY,
1549 panic("softdep_setup_inomapdep: dependency for new inode "
1551 inodedep->id_buf = bp;
1552 inodedep->id_state &= ~DEPCOMPLETE;
1553 bmsafemap = bmsafemap_lookup(inodedep->id_list.wk_mp, bp);
1554 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1559 * Called just after updating the cylinder group block to
1560 * allocate block or fragment.
1563 softdep_setup_blkmapdep(bp, mp, newblkno)
1564 struct buf *bp; /* buffer for cylgroup block with block map */
1565 struct mount *mp; /* filesystem doing allocation */
1566 ufs2_daddr_t newblkno; /* number of newly allocated block */
1568 struct newblk *newblk;
1569 struct bmsafemap *bmsafemap;
1572 fs = VFSTOUFS(mp)->um_fs;
1574 * Create a dependency for the newly allocated block.
1575 * Add it to the dependency list for the buffer holding
1576 * the cylinder group map from which it was allocated.
1579 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1580 panic("softdep_setup_blkmapdep: found block");
1581 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp);
1582 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1587 * Find the bmsafemap associated with a cylinder group buffer.
1588 * If none exists, create one. The buffer must be locked when
1589 * this routine is called and this routine must be called with
1590 * splbio interrupts blocked.
1592 static struct bmsafemap *
1593 bmsafemap_lookup(mp, bp)
1597 struct bmsafemap *bmsafemap;
1598 struct worklist *wk;
1600 mtx_assert(&lk, MA_OWNED);
1601 LIST_FOREACH(wk, &bp->b_dep, wk_list)
1602 if (wk->wk_type == D_BMSAFEMAP)
1603 return (WK_BMSAFEMAP(wk));
1605 bmsafemap = malloc(sizeof(struct bmsafemap),
1606 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1607 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
1608 bmsafemap->sm_buf = bp;
1609 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1610 LIST_INIT(&bmsafemap->sm_allocindirhd);
1611 LIST_INIT(&bmsafemap->sm_inodedephd);
1612 LIST_INIT(&bmsafemap->sm_newblkhd);
1614 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1619 * Direct block allocation dependencies.
1621 * When a new block is allocated, the corresponding disk locations must be
1622 * initialized (with zeros or new data) before the on-disk inode points to
1623 * them. Also, the freemap from which the block was allocated must be
1624 * updated (on disk) before the inode's pointer. These two dependencies are
1625 * independent of each other and are needed for all file blocks and indirect
1626 * blocks that are pointed to directly by the inode. Just before the
1627 * "in-core" version of the inode is updated with a newly allocated block
1628 * number, a procedure (below) is called to setup allocation dependency
1629 * structures. These structures are removed when the corresponding
1630 * dependencies are satisfied or when the block allocation becomes obsolete
1631 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1632 * fragment that gets upgraded). All of these cases are handled in
1633 * procedures described later.
1635 * When a file extension causes a fragment to be upgraded, either to a larger
1636 * fragment or to a full block, the on-disk location may change (if the
1637 * previous fragment could not simply be extended). In this case, the old
1638 * fragment must be de-allocated, but not until after the inode's pointer has
1639 * been updated. In most cases, this is handled by later procedures, which
1640 * will construct a "freefrag" structure to be added to the workitem queue
1641 * when the inode update is complete (or obsolete). The main exception to
1642 * this is when an allocation occurs while a pending allocation dependency
1643 * (for the same block pointer) remains. This case is handled in the main
1644 * allocation dependency setup procedure by immediately freeing the
1645 * unreferenced fragments.
1648 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1649 struct inode *ip; /* inode to which block is being added */
1650 ufs_lbn_t lbn; /* block pointer within inode */
1651 ufs2_daddr_t newblkno; /* disk block number being added */
1652 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
1653 long newsize; /* size of new block */
1654 long oldsize; /* size of new block */
1655 struct buf *bp; /* bp for allocated block */
1657 struct allocdirect *adp, *oldadp;
1658 struct allocdirectlst *adphead;
1659 struct bmsafemap *bmsafemap;
1660 struct inodedep *inodedep;
1661 struct pagedep *pagedep;
1662 struct newblk *newblk;
1665 mp = UFSTOVFS(ip->i_ump);
1666 adp = malloc(sizeof(struct allocdirect),
1667 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1668 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1670 adp->ad_newblkno = newblkno;
1671 adp->ad_oldblkno = oldblkno;
1672 adp->ad_newsize = newsize;
1673 adp->ad_oldsize = oldsize;
1674 adp->ad_state = ATTACHED;
1675 LIST_INIT(&adp->ad_newdirblk);
1676 if (newblkno == oldblkno)
1677 adp->ad_freefrag = NULL;
1679 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1682 if (lbn >= NDADDR) {
1683 /* allocating an indirect block */
1685 panic("softdep_setup_allocdirect: non-zero indir");
1688 * Allocating a direct block.
1690 * If we are allocating a directory block, then we must
1691 * allocate an associated pagedep to track additions and
1694 if ((ip->i_mode & IFMT) == IFDIR &&
1695 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1696 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1698 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1699 panic("softdep_setup_allocdirect: lost block");
1700 if (newblk->nb_state == DEPCOMPLETE) {
1701 adp->ad_state |= DEPCOMPLETE;
1704 bmsafemap = newblk->nb_bmsafemap;
1705 adp->ad_buf = bmsafemap->sm_buf;
1706 LIST_REMOVE(newblk, nb_deps);
1707 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1709 LIST_REMOVE(newblk, nb_hash);
1710 free(newblk, M_NEWBLK);
1712 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1713 adp->ad_inodedep = inodedep;
1714 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1716 * The list of allocdirects must be kept in sorted and ascending
1717 * order so that the rollback routines can quickly determine the
1718 * first uncommitted block (the size of the file stored on disk
1719 * ends at the end of the lowest committed fragment, or if there
1720 * are no fragments, at the end of the highest committed block).
1721 * Since files generally grow, the typical case is that the new
1722 * block is to be added at the end of the list. We speed this
1723 * special case by checking against the last allocdirect in the
1724 * list before laboriously traversing the list looking for the
1727 adphead = &inodedep->id_newinoupdt;
1728 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1729 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1730 /* insert at end of list */
1731 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1732 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1733 allocdirect_merge(adphead, adp, oldadp);
1737 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1738 if (oldadp->ad_lbn >= lbn)
1742 panic("softdep_setup_allocdirect: lost entry");
1743 /* insert in middle of list */
1744 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1745 if (oldadp->ad_lbn == lbn)
1746 allocdirect_merge(adphead, adp, oldadp);
1751 * Replace an old allocdirect dependency with a newer one.
1752 * This routine must be called with splbio interrupts blocked.
1755 allocdirect_merge(adphead, newadp, oldadp)
1756 struct allocdirectlst *adphead; /* head of list holding allocdirects */
1757 struct allocdirect *newadp; /* allocdirect being added */
1758 struct allocdirect *oldadp; /* existing allocdirect being checked */
1760 struct worklist *wk;
1761 struct freefrag *freefrag;
1762 struct newdirblk *newdirblk;
1764 mtx_assert(&lk, MA_OWNED);
1765 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1766 newadp->ad_oldsize != oldadp->ad_newsize ||
1767 newadp->ad_lbn >= NDADDR)
1768 panic("%s %jd != new %jd || old size %ld != new %ld",
1769 "allocdirect_merge: old blkno",
1770 (intmax_t)newadp->ad_oldblkno,
1771 (intmax_t)oldadp->ad_newblkno,
1772 newadp->ad_oldsize, oldadp->ad_newsize);
1773 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1774 newadp->ad_oldsize = oldadp->ad_oldsize;
1776 * If the old dependency had a fragment to free or had never
1777 * previously had a block allocated, then the new dependency
1778 * can immediately post its freefrag and adopt the old freefrag.
1779 * This action is done by swapping the freefrag dependencies.
1780 * The new dependency gains the old one's freefrag, and the
1781 * old one gets the new one and then immediately puts it on
1782 * the worklist when it is freed by free_allocdirect. It is
1783 * not possible to do this swap when the old dependency had a
1784 * non-zero size but no previous fragment to free. This condition
1785 * arises when the new block is an extension of the old block.
1786 * Here, the first part of the fragment allocated to the new
1787 * dependency is part of the block currently claimed on disk by
1788 * the old dependency, so cannot legitimately be freed until the
1789 * conditions for the new dependency are fulfilled.
1791 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1792 freefrag = newadp->ad_freefrag;
1793 newadp->ad_freefrag = oldadp->ad_freefrag;
1794 oldadp->ad_freefrag = freefrag;
1797 * If we are tracking a new directory-block allocation,
1798 * move it from the old allocdirect to the new allocdirect.
1800 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
1801 newdirblk = WK_NEWDIRBLK(wk);
1802 WORKLIST_REMOVE(&newdirblk->db_list);
1803 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
1804 panic("allocdirect_merge: extra newdirblk");
1805 WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list);
1807 free_allocdirect(adphead, oldadp, 0);
1811 * Allocate a new freefrag structure if needed.
1813 static struct freefrag *
1814 newfreefrag(ip, blkno, size)
1819 struct freefrag *freefrag;
1825 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1826 panic("newfreefrag: frag size");
1827 freefrag = malloc(sizeof(struct freefrag),
1828 M_FREEFRAG, M_SOFTDEP_FLAGS);
1829 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump));
1830 freefrag->ff_inum = ip->i_number;
1831 freefrag->ff_blkno = blkno;
1832 freefrag->ff_fragsize = size;
1837 * This workitem de-allocates fragments that were replaced during
1838 * file block allocation.
1841 handle_workitem_freefrag(freefrag)
1842 struct freefrag *freefrag;
1844 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
1846 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
1847 freefrag->ff_fragsize, freefrag->ff_inum);
1849 WORKITEM_FREE(freefrag, D_FREEFRAG);
1854 * Set up a dependency structure for an external attributes data block.
1855 * This routine follows much of the structure of softdep_setup_allocdirect.
1856 * See the description of softdep_setup_allocdirect above for details.
1859 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1862 ufs2_daddr_t newblkno;
1863 ufs2_daddr_t oldblkno;
1868 struct allocdirect *adp, *oldadp;
1869 struct allocdirectlst *adphead;
1870 struct bmsafemap *bmsafemap;
1871 struct inodedep *inodedep;
1872 struct newblk *newblk;
1875 mp = UFSTOVFS(ip->i_ump);
1876 adp = malloc(sizeof(struct allocdirect),
1877 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1878 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1880 adp->ad_newblkno = newblkno;
1881 adp->ad_oldblkno = oldblkno;
1882 adp->ad_newsize = newsize;
1883 adp->ad_oldsize = oldsize;
1884 adp->ad_state = ATTACHED | EXTDATA;
1885 LIST_INIT(&adp->ad_newdirblk);
1886 if (newblkno == oldblkno)
1887 adp->ad_freefrag = NULL;
1889 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1892 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1893 panic("softdep_setup_allocext: lost block");
1895 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1896 adp->ad_inodedep = inodedep;
1898 if (newblk->nb_state == DEPCOMPLETE) {
1899 adp->ad_state |= DEPCOMPLETE;
1902 bmsafemap = newblk->nb_bmsafemap;
1903 adp->ad_buf = bmsafemap->sm_buf;
1904 LIST_REMOVE(newblk, nb_deps);
1905 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1907 LIST_REMOVE(newblk, nb_hash);
1908 free(newblk, M_NEWBLK);
1910 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1912 panic("softdep_setup_allocext: lbn %lld > NXADDR",
1915 * The list of allocdirects must be kept in sorted and ascending
1916 * order so that the rollback routines can quickly determine the
1917 * first uncommitted block (the size of the file stored on disk
1918 * ends at the end of the lowest committed fragment, or if there
1919 * are no fragments, at the end of the highest committed block).
1920 * Since files generally grow, the typical case is that the new
1921 * block is to be added at the end of the list. We speed this
1922 * special case by checking against the last allocdirect in the
1923 * list before laboriously traversing the list looking for the
1926 adphead = &inodedep->id_newextupdt;
1927 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1928 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1929 /* insert at end of list */
1930 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1931 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1932 allocdirect_merge(adphead, adp, oldadp);
1936 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1937 if (oldadp->ad_lbn >= lbn)
1941 panic("softdep_setup_allocext: lost entry");
1942 /* insert in middle of list */
1943 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1944 if (oldadp->ad_lbn == lbn)
1945 allocdirect_merge(adphead, adp, oldadp);
1950 * Indirect block allocation dependencies.
1952 * The same dependencies that exist for a direct block also exist when
1953 * a new block is allocated and pointed to by an entry in a block of
1954 * indirect pointers. The undo/redo states described above are also
1955 * used here. Because an indirect block contains many pointers that
1956 * may have dependencies, a second copy of the entire in-memory indirect
1957 * block is kept. The buffer cache copy is always completely up-to-date.
1958 * The second copy, which is used only as a source for disk writes,
1959 * contains only the safe pointers (i.e., those that have no remaining
1960 * update dependencies). The second copy is freed when all pointers
1961 * are safe. The cache is not allowed to replace indirect blocks with
1962 * pending update dependencies. If a buffer containing an indirect
1963 * block with dependencies is written, these routines will mark it
1964 * dirty again. It can only be successfully written once all the
1965 * dependencies are removed. The ffs_fsync routine in conjunction with
1966 * softdep_sync_metadata work together to get all the dependencies
1967 * removed so that a file can be successfully written to disk. Three
1968 * procedures are used when setting up indirect block pointer
1969 * dependencies. The division is necessary because of the organization
1970 * of the "balloc" routine and because of the distinction between file
1971 * pages and file metadata blocks.
1975 * Allocate a new allocindir structure.
1977 static struct allocindir *
1978 newallocindir(ip, ptrno, newblkno, oldblkno)
1979 struct inode *ip; /* inode for file being extended */
1980 int ptrno; /* offset of pointer in indirect block */
1981 ufs2_daddr_t newblkno; /* disk block number being added */
1982 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
1984 struct allocindir *aip;
1986 aip = malloc(sizeof(struct allocindir),
1987 M_ALLOCINDIR, M_SOFTDEP_FLAGS|M_ZERO);
1988 workitem_alloc(&aip->ai_list, D_ALLOCINDIR, UFSTOVFS(ip->i_ump));
1989 aip->ai_state = ATTACHED;
1990 aip->ai_offset = ptrno;
1991 aip->ai_newblkno = newblkno;
1992 aip->ai_oldblkno = oldblkno;
1993 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1998 * Called just before setting an indirect block pointer
1999 * to a newly allocated file page.
2002 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
2003 struct inode *ip; /* inode for file being extended */
2004 ufs_lbn_t lbn; /* allocated block number within file */
2005 struct buf *bp; /* buffer with indirect blk referencing page */
2006 int ptrno; /* offset of pointer in indirect block */
2007 ufs2_daddr_t newblkno; /* disk block number being added */
2008 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
2009 struct buf *nbp; /* buffer holding allocated page */
2011 struct allocindir *aip;
2012 struct pagedep *pagedep;
2014 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
2015 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
2018 * If we are allocating a directory page, then we must
2019 * allocate an associated pagedep to track additions and
2022 if ((ip->i_mode & IFMT) == IFDIR &&
2023 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
2024 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
2025 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2026 setup_allocindir_phase2(bp, ip, aip);
2031 * Called just before setting an indirect block pointer to a
2032 * newly allocated indirect block.
2035 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
2036 struct buf *nbp; /* newly allocated indirect block */
2037 struct inode *ip; /* inode for file being extended */
2038 struct buf *bp; /* indirect block referencing allocated block */
2039 int ptrno; /* offset of pointer in indirect block */
2040 ufs2_daddr_t newblkno; /* disk block number being added */
2042 struct allocindir *aip;
2044 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
2045 aip = newallocindir(ip, ptrno, newblkno, 0);
2047 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2048 setup_allocindir_phase2(bp, ip, aip);
2053 * Called to finish the allocation of the "aip" allocated
2054 * by one of the two routines above.
2057 setup_allocindir_phase2(bp, ip, aip)
2058 struct buf *bp; /* in-memory copy of the indirect block */
2059 struct inode *ip; /* inode for file being extended */
2060 struct allocindir *aip; /* allocindir allocated by the above routines */
2062 struct worklist *wk;
2063 struct indirdep *indirdep, *newindirdep;
2064 struct bmsafemap *bmsafemap;
2065 struct allocindir *oldaip;
2066 struct freefrag *freefrag;
2067 struct newblk *newblk;
2070 mtx_assert(&lk, MA_OWNED);
2071 if (bp->b_lblkno >= 0)
2072 panic("setup_allocindir_phase2: not indir blk");
2073 for (indirdep = NULL, newindirdep = NULL; ; ) {
2074 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2075 if (wk->wk_type != D_INDIRDEP)
2077 indirdep = WK_INDIRDEP(wk);
2080 if (indirdep == NULL && newindirdep) {
2081 indirdep = newindirdep;
2082 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
2086 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
2088 panic("setup_allocindir: lost block");
2089 if (newblk->nb_state == DEPCOMPLETE) {
2090 aip->ai_state |= DEPCOMPLETE;
2093 bmsafemap = newblk->nb_bmsafemap;
2094 aip->ai_buf = bmsafemap->sm_buf;
2095 LIST_REMOVE(newblk, nb_deps);
2096 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
2099 LIST_REMOVE(newblk, nb_hash);
2100 free(newblk, M_NEWBLK);
2101 aip->ai_indirdep = indirdep;
2103 * Check to see if there is an existing dependency
2104 * for this block. If there is, merge the old
2105 * dependency into the new one.
2107 if (aip->ai_oldblkno == 0)
2111 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
2112 if (oldaip->ai_offset == aip->ai_offset)
2115 if (oldaip != NULL) {
2116 if (oldaip->ai_newblkno != aip->ai_oldblkno)
2117 panic("setup_allocindir_phase2: blkno");
2118 aip->ai_oldblkno = oldaip->ai_oldblkno;
2119 freefrag = aip->ai_freefrag;
2120 aip->ai_freefrag = oldaip->ai_freefrag;
2121 oldaip->ai_freefrag = NULL;
2122 free_allocindir(oldaip, NULL);
2124 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
2125 if (ip->i_ump->um_fstype == UFS1)
2126 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)
2127 [aip->ai_offset] = aip->ai_oldblkno;
2129 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)
2130 [aip->ai_offset] = aip->ai_oldblkno;
2132 if (freefrag != NULL)
2133 handle_workitem_freefrag(freefrag);
2137 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
2138 brelse(newindirdep->ir_savebp);
2140 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
2149 newindirdep = malloc(sizeof(struct indirdep),
2150 M_INDIRDEP, M_SOFTDEP_FLAGS);
2151 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP,
2152 UFSTOVFS(ip->i_ump));
2153 newindirdep->ir_state = ATTACHED;
2154 if (ip->i_ump->um_fstype == UFS1)
2155 newindirdep->ir_state |= UFS1FMT;
2156 LIST_INIT(&newindirdep->ir_deplisthd);
2157 LIST_INIT(&newindirdep->ir_donehd);
2158 if (bp->b_blkno == bp->b_lblkno) {
2159 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
2161 bp->b_blkno = blkno;
2163 newindirdep->ir_savebp =
2164 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
2165 BUF_KERNPROC(newindirdep->ir_savebp);
2166 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
2172 * Block de-allocation dependencies.
2174 * When blocks are de-allocated, the on-disk pointers must be nullified before
2175 * the blocks are made available for use by other files. (The true
2176 * requirement is that old pointers must be nullified before new on-disk
2177 * pointers are set. We chose this slightly more stringent requirement to
2178 * reduce complexity.) Our implementation handles this dependency by updating
2179 * the inode (or indirect block) appropriately but delaying the actual block
2180 * de-allocation (i.e., freemap and free space count manipulation) until
2181 * after the updated versions reach stable storage. After the disk is
2182 * updated, the blocks can be safely de-allocated whenever it is convenient.
2183 * This implementation handles only the common case of reducing a file's
2184 * length to zero. Other cases are handled by the conventional synchronous
2187 * The ffs implementation with which we worked double-checks
2188 * the state of the block pointers and file size as it reduces
2189 * a file's length. Some of this code is replicated here in our
2190 * soft updates implementation. The freeblks->fb_chkcnt field is
2191 * used to transfer a part of this information to the procedure
2192 * that eventually de-allocates the blocks.
2194 * This routine should be called from the routine that shortens
2195 * a file's length, before the inode's size or block pointers
2196 * are modified. It will save the block pointer information for
2197 * later release and zero the inode so that the calling routine
2201 softdep_setup_freeblocks(ip, length, flags)
2202 struct inode *ip; /* The inode whose length is to be reduced */
2203 off_t length; /* The new length for the file */
2204 int flags; /* IO_EXT and/or IO_NORMAL */
2206 struct freeblks *freeblks;
2207 struct inodedep *inodedep;
2208 struct allocdirect *adp;
2213 ufs2_daddr_t extblocks, datablocks;
2215 int i, delay, error;
2218 mp = UFSTOVFS(ip->i_ump);
2220 panic("softdep_setup_freeblocks: non-zero length");
2221 freeblks = malloc(sizeof(struct freeblks),
2222 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
2223 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
2224 freeblks->fb_state = ATTACHED;
2225 freeblks->fb_uid = ip->i_uid;
2226 freeblks->fb_previousinum = ip->i_number;
2227 freeblks->fb_devvp = ip->i_devvp;
2232 if (fs->fs_magic == FS_UFS2_MAGIC)
2233 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
2234 datablocks = DIP(ip, i_blocks) - extblocks;
2235 if ((flags & IO_NORMAL) == 0) {
2236 freeblks->fb_oldsize = 0;
2237 freeblks->fb_chkcnt = 0;
2239 freeblks->fb_oldsize = ip->i_size;
2241 DIP_SET(ip, i_size, 0);
2242 freeblks->fb_chkcnt = datablocks;
2243 for (i = 0; i < NDADDR; i++) {
2244 freeblks->fb_dblks[i] = DIP(ip, i_db[i]);
2245 DIP_SET(ip, i_db[i], 0);
2247 for (i = 0; i < NIADDR; i++) {
2248 freeblks->fb_iblks[i] = DIP(ip, i_ib[i]);
2249 DIP_SET(ip, i_ib[i], 0);
2252 * If the file was removed, then the space being freed was
2253 * accounted for then (see softdep_releasefile()). If the
2254 * file is merely being truncated, then we account for it now.
2256 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2257 UFS_LOCK(ip->i_ump);
2258 fs->fs_pendingblocks += datablocks;
2259 UFS_UNLOCK(ip->i_ump);
2262 if ((flags & IO_EXT) == 0) {
2263 freeblks->fb_oldextsize = 0;
2265 freeblks->fb_oldextsize = ip->i_din2->di_extsize;
2266 ip->i_din2->di_extsize = 0;
2267 freeblks->fb_chkcnt += extblocks;
2268 for (i = 0; i < NXADDR; i++) {
2269 freeblks->fb_eblks[i] = ip->i_din2->di_extb[i];
2270 ip->i_din2->di_extb[i] = 0;
2273 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt);
2275 * Push the zero'ed inode to to its disk buffer so that we are free
2276 * to delete its dependencies below. Once the dependencies are gone
2277 * the buffer can be safely released.
2279 if ((error = bread(ip->i_devvp,
2280 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
2281 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
2283 softdep_error("softdep_setup_freeblocks", error);
2285 if (ip->i_ump->um_fstype == UFS1)
2286 *((struct ufs1_dinode *)bp->b_data +
2287 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
2289 *((struct ufs2_dinode *)bp->b_data +
2290 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
2292 * Find and eliminate any inode dependencies.
2295 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
2296 if ((inodedep->id_state & IOSTARTED) != 0)
2297 panic("softdep_setup_freeblocks: inode busy");
2299 * Add the freeblks structure to the list of operations that
2300 * must await the zero'ed inode being written to disk. If we
2301 * still have a bitmap dependency (delay == 0), then the inode
2302 * has never been written to disk, so we can process the
2303 * freeblks below once we have deleted the dependencies.
2305 delay = (inodedep->id_state & DEPCOMPLETE);
2307 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
2309 * Because the file length has been truncated to zero, any
2310 * pending block allocation dependency structures associated
2311 * with this inode are obsolete and can simply be de-allocated.
2312 * We must first merge the two dependency lists to get rid of
2313 * any duplicate freefrag structures, then purge the merged list.
2314 * If we still have a bitmap dependency, then the inode has never
2315 * been written to disk, so we can free any fragments without delay.
2317 if (flags & IO_NORMAL) {
2318 merge_inode_lists(&inodedep->id_newinoupdt,
2319 &inodedep->id_inoupdt);
2320 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
2321 free_allocdirect(&inodedep->id_inoupdt, adp, delay);
2323 if (flags & IO_EXT) {
2324 merge_inode_lists(&inodedep->id_newextupdt,
2325 &inodedep->id_extupdt);
2326 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
2327 free_allocdirect(&inodedep->id_extupdt, adp, delay);
2332 * We must wait for any I/O in progress to finish so that
2333 * all potential buffers on the dirty list will be visible.
2334 * Once they are all there, walk the list and get rid of
2342 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
2343 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
2344 ((flags & IO_NORMAL) == 0 &&
2345 (bp->b_xflags & BX_ALTDATA) == 0))
2347 if ((bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT)) == NULL)
2351 (void) inodedep_lookup(mp, ip->i_number, 0, &inodedep);
2352 deallocate_dependencies(bp, inodedep);
2354 bp->b_flags |= B_INVAL | B_NOCACHE;
2361 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
2362 (void) free_inodedep(inodedep);
2365 freeblks->fb_state |= DEPCOMPLETE;
2367 * If the inode with zeroed block pointers is now on disk
2368 * we can start freeing blocks. Add freeblks to the worklist
2369 * instead of calling handle_workitem_freeblocks directly as
2370 * it is more likely that additional IO is needed to complete
2371 * the request here than in the !delay case.
2373 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
2374 add_to_worklist(&freeblks->fb_list);
2379 * If the inode has never been written to disk (delay == 0),
2380 * then we can process the freeblks now that we have deleted
2384 handle_workitem_freeblocks(freeblks, 0);
2388 * Reclaim any dependency structures from a buffer that is about to
2389 * be reallocated to a new vnode. The buffer must be locked, thus,
2390 * no I/O completion operations can occur while we are manipulating
2391 * its associated dependencies. The mutex is held so that other I/O's
2392 * associated with related dependencies do not occur.
2395 deallocate_dependencies(bp, inodedep)
2397 struct inodedep *inodedep;
2399 struct worklist *wk;
2400 struct indirdep *indirdep;
2401 struct allocindir *aip;
2402 struct pagedep *pagedep;
2403 struct dirrem *dirrem;
2407 mtx_assert(&lk, MA_OWNED);
2408 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2409 switch (wk->wk_type) {
2412 indirdep = WK_INDIRDEP(wk);
2414 * None of the indirect pointers will ever be visible,
2415 * so they can simply be tossed. GOINGAWAY ensures
2416 * that allocated pointers will be saved in the buffer
2417 * cache until they are freed. Note that they will
2418 * only be able to be found by their physical address
2419 * since the inode mapping the logical address will
2420 * be gone. The save buffer used for the safe copy
2421 * was allocated in setup_allocindir_phase2 using
2422 * the physical address so it could be used for this
2423 * purpose. Hence we swap the safe copy with the real
2424 * copy, allowing the safe copy to be freed and holding
2425 * on to the real copy for later use in indir_trunc.
2427 if (indirdep->ir_state & GOINGAWAY)
2428 panic("deallocate_dependencies: already gone");
2429 indirdep->ir_state |= GOINGAWAY;
2430 VFSTOUFS(bp->b_vp->v_mount)->um_numindirdeps += 1;
2431 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
2432 free_allocindir(aip, inodedep);
2433 if (bp->b_lblkno >= 0 ||
2434 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
2435 panic("deallocate_dependencies: not indir");
2436 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
2438 WORKLIST_REMOVE(wk);
2439 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
2443 pagedep = WK_PAGEDEP(wk);
2445 * None of the directory additions will ever be
2446 * visible, so they can simply be tossed.
2448 for (i = 0; i < DAHASHSZ; i++)
2450 LIST_FIRST(&pagedep->pd_diraddhd[i])))
2452 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
2455 * Copy any directory remove dependencies to the list
2456 * to be processed after the zero'ed inode is written.
2457 * If the inode has already been written, then they
2458 * can be dumped directly onto the work list.
2460 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
2461 LIST_REMOVE(dirrem, dm_next);
2462 dirrem->dm_dirinum = pagedep->pd_ino;
2463 if (inodedep == NULL ||
2464 (inodedep->id_state & ALLCOMPLETE) ==
2466 add_to_worklist(&dirrem->dm_list);
2468 WORKLIST_INSERT(&inodedep->id_bufwait,
2471 if ((pagedep->pd_state & NEWBLOCK) != 0) {
2472 LIST_FOREACH(wk, &inodedep->id_bufwait, wk_list)
2473 if (wk->wk_type == D_NEWDIRBLK &&
2474 WK_NEWDIRBLK(wk)->db_pagedep ==
2478 WORKLIST_REMOVE(wk);
2479 free_newdirblk(WK_NEWDIRBLK(wk));
2481 panic("deallocate_dependencies: "
2484 WORKLIST_REMOVE(&pagedep->pd_list);
2485 LIST_REMOVE(pagedep, pd_hash);
2486 WORKITEM_FREE(pagedep, D_PAGEDEP);
2490 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
2495 panic("deallocate_dependencies: Unexpected type %s",
2496 TYPENAME(wk->wk_type));
2500 panic("deallocate_dependencies: Unknown type %s",
2501 TYPENAME(wk->wk_type));
2508 * Free an allocdirect. Generate a new freefrag work request if appropriate.
2509 * This routine must be called with splbio interrupts blocked.
2512 free_allocdirect(adphead, adp, delay)
2513 struct allocdirectlst *adphead;
2514 struct allocdirect *adp;
2517 struct newdirblk *newdirblk;
2518 struct worklist *wk;
2520 mtx_assert(&lk, MA_OWNED);
2521 if ((adp->ad_state & DEPCOMPLETE) == 0)
2522 LIST_REMOVE(adp, ad_deps);
2523 TAILQ_REMOVE(adphead, adp, ad_next);
2524 if ((adp->ad_state & COMPLETE) == 0)
2525 WORKLIST_REMOVE(&adp->ad_list);
2526 if (adp->ad_freefrag != NULL) {
2528 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2529 &adp->ad_freefrag->ff_list);
2531 add_to_worklist(&adp->ad_freefrag->ff_list);
2533 if ((wk = LIST_FIRST(&adp->ad_newdirblk)) != NULL) {
2534 newdirblk = WK_NEWDIRBLK(wk);
2535 WORKLIST_REMOVE(&newdirblk->db_list);
2536 if (!LIST_EMPTY(&adp->ad_newdirblk))
2537 panic("free_allocdirect: extra newdirblk");
2539 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2540 &newdirblk->db_list);
2542 free_newdirblk(newdirblk);
2544 WORKITEM_FREE(adp, D_ALLOCDIRECT);
2548 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
2549 * This routine must be called with splbio interrupts blocked.
2552 free_newdirblk(newdirblk)
2553 struct newdirblk *newdirblk;
2555 struct pagedep *pagedep;
2559 mtx_assert(&lk, MA_OWNED);
2561 * If the pagedep is still linked onto the directory buffer
2562 * dependency chain, then some of the entries on the
2563 * pd_pendinghd list may not be committed to disk yet. In
2564 * this case, we will simply clear the NEWBLOCK flag and
2565 * let the pd_pendinghd list be processed when the pagedep
2566 * is next written. If the pagedep is no longer on the buffer
2567 * dependency chain, then all the entries on the pd_pending
2568 * list are committed to disk and we can free them here.
2570 pagedep = newdirblk->db_pagedep;
2571 pagedep->pd_state &= ~NEWBLOCK;
2572 if ((pagedep->pd_state & ONWORKLIST) == 0)
2573 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
2576 * If no dependencies remain, the pagedep will be freed.
2578 for (i = 0; i < DAHASHSZ; i++)
2579 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
2581 if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0) {
2582 LIST_REMOVE(pagedep, pd_hash);
2583 WORKITEM_FREE(pagedep, D_PAGEDEP);
2585 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2589 * Prepare an inode to be freed. The actual free operation is not
2590 * done until the zero'ed inode has been written to disk.
2593 softdep_freefile(pvp, ino, mode)
2598 struct inode *ip = VTOI(pvp);
2599 struct inodedep *inodedep;
2600 struct freefile *freefile;
2603 * This sets up the inode de-allocation dependency.
2605 freefile = malloc(sizeof(struct freefile),
2606 M_FREEFILE, M_SOFTDEP_FLAGS);
2607 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
2608 freefile->fx_mode = mode;
2609 freefile->fx_oldinum = ino;
2610 freefile->fx_devvp = ip->i_devvp;
2611 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2612 UFS_LOCK(ip->i_ump);
2613 ip->i_fs->fs_pendinginodes += 1;
2614 UFS_UNLOCK(ip->i_ump);
2618 * If the inodedep does not exist, then the zero'ed inode has
2619 * been written to disk. If the allocated inode has never been
2620 * written to disk, then the on-disk inode is zero'ed. In either
2621 * case we can free the file immediately.
2624 if (inodedep_lookup(pvp->v_mount, ino, 0, &inodedep) == 0 ||
2625 check_inode_unwritten(inodedep)) {
2627 handle_workitem_freefile(freefile);
2630 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2632 if (ip->i_number == ino)
2633 ip->i_flag |= IN_MODIFIED;
2637 * Check to see if an inode has never been written to disk. If
2638 * so free the inodedep and return success, otherwise return failure.
2639 * This routine must be called with splbio interrupts blocked.
2641 * If we still have a bitmap dependency, then the inode has never
2642 * been written to disk. Drop the dependency as it is no longer
2643 * necessary since the inode is being deallocated. We set the
2644 * ALLCOMPLETE flags since the bitmap now properly shows that the
2645 * inode is not allocated. Even if the inode is actively being
2646 * written, it has been rolled back to its zero'ed state, so we
2647 * are ensured that a zero inode is what is on the disk. For short
2648 * lived files, this change will usually result in removing all the
2649 * dependencies from the inode so that it can be freed immediately.
2652 check_inode_unwritten(inodedep)
2653 struct inodedep *inodedep;
2656 mtx_assert(&lk, MA_OWNED);
2657 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2658 !LIST_EMPTY(&inodedep->id_pendinghd) ||
2659 !LIST_EMPTY(&inodedep->id_bufwait) ||
2660 !LIST_EMPTY(&inodedep->id_inowait) ||
2661 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2662 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2663 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2664 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2665 inodedep->id_nlinkdelta != 0)
2669 * Another process might be in initiate_write_inodeblock_ufs[12]
2670 * trying to allocate memory without holding "Softdep Lock".
2672 if ((inodedep->id_state & IOSTARTED) != 0 &&
2673 inodedep->id_savedino1 == NULL)
2676 inodedep->id_state |= ALLCOMPLETE;
2677 LIST_REMOVE(inodedep, id_deps);
2678 inodedep->id_buf = NULL;
2679 if (inodedep->id_state & ONWORKLIST)
2680 WORKLIST_REMOVE(&inodedep->id_list);
2681 if (inodedep->id_savedino1 != NULL) {
2682 free(inodedep->id_savedino1, M_SAVEDINO);
2683 inodedep->id_savedino1 = NULL;
2685 if (free_inodedep(inodedep) == 0)
2686 panic("check_inode_unwritten: busy inode");
2691 * Try to free an inodedep structure. Return 1 if it could be freed.
2694 free_inodedep(inodedep)
2695 struct inodedep *inodedep;
2698 mtx_assert(&lk, MA_OWNED);
2699 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2700 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2701 !LIST_EMPTY(&inodedep->id_pendinghd) ||
2702 !LIST_EMPTY(&inodedep->id_bufwait) ||
2703 !LIST_EMPTY(&inodedep->id_inowait) ||
2704 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2705 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2706 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2707 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2708 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino1 != NULL)
2710 LIST_REMOVE(inodedep, id_hash);
2711 WORKITEM_FREE(inodedep, D_INODEDEP);
2717 * This workitem routine performs the block de-allocation.
2718 * The workitem is added to the pending list after the updated
2719 * inode block has been written to disk. As mentioned above,
2720 * checks regarding the number of blocks de-allocated (compared
2721 * to the number of blocks allocated for the file) are also
2722 * performed in this function.
2725 handle_workitem_freeblocks(freeblks, flags)
2726 struct freeblks *freeblks;
2732 struct ufsmount *ump;
2733 int i, nblocks, level, bsize;
2734 ufs2_daddr_t bn, blocksreleased = 0;
2735 int error, allerror = 0;
2736 ufs_lbn_t baselbns[NIADDR], tmpval;
2737 int fs_pendingblocks;
2739 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2741 fs_pendingblocks = 0;
2743 baselbns[0] = NDADDR;
2744 for (i = 1; i < NIADDR; i++) {
2745 tmpval *= NINDIR(fs);
2746 baselbns[i] = baselbns[i - 1] + tmpval;
2748 nblocks = btodb(fs->fs_bsize);
2751 * Release all extended attribute blocks or frags.
2753 if (freeblks->fb_oldextsize > 0) {
2754 for (i = (NXADDR - 1); i >= 0; i--) {
2755 if ((bn = freeblks->fb_eblks[i]) == 0)
2757 bsize = sblksize(fs, freeblks->fb_oldextsize, i);
2758 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2759 freeblks->fb_previousinum);
2760 blocksreleased += btodb(bsize);
2764 * Release all data blocks or frags.
2766 if (freeblks->fb_oldsize > 0) {
2768 * Indirect blocks first.
2770 for (level = (NIADDR - 1); level >= 0; level--) {
2771 if ((bn = freeblks->fb_iblks[level]) == 0)
2773 if ((error = indir_trunc(freeblks, fsbtodb(fs, bn),
2774 level, baselbns[level], &blocksreleased)) != 0)
2776 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn,
2777 fs->fs_bsize, freeblks->fb_previousinum);
2778 fs_pendingblocks += nblocks;
2779 blocksreleased += nblocks;
2782 * All direct blocks or frags.
2784 for (i = (NDADDR - 1); i >= 0; i--) {
2785 if ((bn = freeblks->fb_dblks[i]) == 0)
2787 bsize = sblksize(fs, freeblks->fb_oldsize, i);
2788 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2789 freeblks->fb_previousinum);
2790 fs_pendingblocks += btodb(bsize);
2791 blocksreleased += btodb(bsize);
2795 fs->fs_pendingblocks -= fs_pendingblocks;
2798 * If we still have not finished background cleanup, then check
2799 * to see if the block count needs to be adjusted.
2801 if (freeblks->fb_chkcnt != blocksreleased &&
2802 (fs->fs_flags & FS_UNCLEAN) != 0 &&
2803 ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_previousinum,
2804 (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ)
2807 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + \
2808 freeblks->fb_chkcnt - blocksreleased);
2809 ip->i_flag |= IN_CHANGE;
2814 if (freeblks->fb_chkcnt != blocksreleased &&
2815 ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0))
2816 printf("handle_workitem_freeblocks: block count\n");
2818 softdep_error("handle_workitem_freeblks", allerror);
2819 #endif /* INVARIANTS */
2822 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 dap = malloc(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 newdirblk = malloc(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 mkdir1 = malloc(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 mkdir2 = malloc(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 (!(ip->i_flags & SF_SNAPSHOT) && num_dirrem > max_softdeps / 2)
3319 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_REMOVE);
3322 dirrem = malloc(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 dap = malloc(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 indirdep->ir_saveddata = malloc(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 sip = malloc(sizeof(struct ufs1_dinode),
3930 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 sip = malloc(sizeof(struct ufs2_dinode),
4076 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 error = vfs_busy(mp, MBF_NOWAIT);
5115 error = vfs_busy(mp, 0);
5116 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5120 if (vp->v_iflag & VI_DOOMED) {
5126 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
5127 &pvp, FFSV_FORCEINSMQ);
5129 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5130 if (vp->v_iflag & VI_DOOMED) {
5139 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
5140 * that are contained in direct blocks will be resolved by
5141 * doing a ffs_update. Pagedeps contained in indirect blocks
5142 * may require a complete sync'ing of the directory. So, we
5143 * try the cheap and fast ffs_update first, and if that fails,
5144 * then we do the slower ffs_syncvnode of the directory.
5149 if ((error = ffs_update(pvp, 1)) != 0) {
5155 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
5156 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
5157 if (wk->wk_type != D_DIRADD)
5158 panic("softdep_fsync: Unexpected type %s",
5159 TYPENAME(wk->wk_type));
5160 dap = WK_DIRADD(wk);
5161 if (dap->da_state & DIRCHG)
5162 pagedep = dap->da_previous->dm_pagedep;
5164 pagedep = dap->da_pagedep;
5165 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
5168 if (pagedep_new_block &&
5169 (error = ffs_syncvnode(pvp, MNT_WAIT))) {
5179 * Flush directory page containing the inode's name.
5181 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
5191 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
5199 * Flush all the dirty bitmaps associated with the block device
5200 * before flushing the rest of the dirty blocks so as to reduce
5201 * the number of dependencies that will have to be rolled back.
5204 softdep_fsync_mountdev(vp)
5207 struct buf *bp, *nbp;
5208 struct worklist *wk;
5211 if (!vn_isdisk(vp, NULL))
5212 panic("softdep_fsync_mountdev: vnode not a disk");
5217 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
5219 * If it is already scheduled, skip to the next buffer.
5221 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
5224 if ((bp->b_flags & B_DELWRI) == 0)
5225 panic("softdep_fsync_mountdev: not dirty");
5227 * We are only interested in bitmaps with outstanding
5230 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
5231 wk->wk_type != D_BMSAFEMAP ||
5232 (bp->b_vflags & BV_BKGRDINPROG)) {
5248 * This routine is called when we are trying to synchronously flush a
5249 * file. This routine must eliminate any filesystem metadata dependencies
5250 * so that the syncing routine can succeed by pushing the dirty blocks
5251 * associated with the file. If any I/O errors occur, they are returned.
5254 softdep_sync_metadata(struct vnode *vp)
5256 struct pagedep *pagedep;
5257 struct allocdirect *adp;
5258 struct allocindir *aip;
5259 struct buf *bp, *nbp;
5260 struct worklist *wk;
5262 int i, error, waitfor;
5264 if (!DOINGSOFTDEP(vp))
5267 * Ensure that any direct block dependencies have been cleared.
5270 if ((error = flush_inodedep_deps(vp->v_mount, VTOI(vp)->i_number))) {
5276 * For most files, the only metadata dependencies are the
5277 * cylinder group maps that allocate their inode or blocks.
5278 * The block allocation dependencies can be found by traversing
5279 * the dependency lists for any buffers that remain on their
5280 * dirty buffer list. The inode allocation dependency will
5281 * be resolved when the inode is updated with MNT_WAIT.
5282 * This work is done in two passes. The first pass grabs most
5283 * of the buffers and begins asynchronously writing them. The
5284 * only way to wait for these asynchronous writes is to sleep
5285 * on the filesystem vnode which may stay busy for a long time
5286 * if the filesystem is active. So, instead, we make a second
5287 * pass over the dependencies blocking on each write. In the
5288 * usual case we will be blocking against a write that we
5289 * initiated, so when it is done the dependency will have been
5290 * resolved. Thus the second pass is expected to end quickly.
5292 waitfor = MNT_NOWAIT;
5297 * We must wait for any I/O in progress to finish so that
5298 * all potential buffers on the dirty list will be visible.
5302 while ((bp = TAILQ_FIRST(&bo->bo_dirty.bv_hd)) != NULL) {
5303 bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT);
5311 /* While syncing snapshots, we must allow recursive lookups */
5315 * As we hold the buffer locked, none of its dependencies
5318 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5319 switch (wk->wk_type) {
5322 adp = WK_ALLOCDIRECT(wk);
5323 if (adp->ad_state & DEPCOMPLETE)
5326 nbp = getdirtybuf(nbp, &lk, waitfor);
5330 if (waitfor == MNT_NOWAIT) {
5332 } else if ((error = bwrite(nbp)) != 0) {
5339 aip = WK_ALLOCINDIR(wk);
5340 if (aip->ai_state & DEPCOMPLETE)
5343 nbp = getdirtybuf(nbp, &lk, waitfor);
5347 if (waitfor == MNT_NOWAIT) {
5349 } else if ((error = bwrite(nbp)) != 0) {
5358 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
5359 if (aip->ai_state & DEPCOMPLETE)
5362 nbp = getdirtybuf(nbp, &lk, MNT_WAIT);
5366 if ((error = bwrite(nbp)) != 0) {
5375 if ((error = flush_inodedep_deps(wk->wk_mp,
5376 WK_INODEDEP(wk)->id_ino)) != 0) {
5384 * We are trying to sync a directory that may
5385 * have dependencies on both its own metadata
5386 * and/or dependencies on the inodes of any
5387 * recently allocated files. We walk its diradd
5388 * lists pushing out the associated inode.
5390 pagedep = WK_PAGEDEP(wk);
5391 for (i = 0; i < DAHASHSZ; i++) {
5392 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
5395 flush_pagedep_deps(vp, wk->wk_mp,
5396 &pagedep->pd_diraddhd[i]))) {
5405 * This case should never happen if the vnode has
5406 * been properly sync'ed. However, if this function
5407 * is used at a place where the vnode has not yet
5408 * been sync'ed, this dependency can show up. So,
5409 * rather than panic, just flush it.
5411 nbp = WK_MKDIR(wk)->md_buf;
5412 nbp = getdirtybuf(nbp, &lk, waitfor);
5416 if (waitfor == MNT_NOWAIT) {
5418 } else if ((error = bwrite(nbp)) != 0) {
5426 * This case should never happen if the vnode has
5427 * been properly sync'ed. However, if this function
5428 * is used at a place where the vnode has not yet
5429 * been sync'ed, this dependency can show up. So,
5430 * rather than panic, just flush it.
5432 nbp = WK_BMSAFEMAP(wk)->sm_buf;
5433 nbp = getdirtybuf(nbp, &lk, waitfor);
5437 if (waitfor == MNT_NOWAIT) {
5439 } else if ((error = bwrite(nbp)) != 0) {
5446 panic("softdep_sync_metadata: Unknown type %s",
5447 TYPENAME(wk->wk_type));
5451 /* We reach here only in error and unlocked */
5453 panic("softdep_sync_metadata: zero error");
5460 while ((nbp = TAILQ_NEXT(bp, b_bobufs)) != NULL) {
5461 nbp = getdirtybuf(nbp, BO_MTX(bo), MNT_WAIT);
5473 * The brief unlock is to allow any pent up dependency
5474 * processing to be done. Then proceed with the second pass.
5476 if (waitfor == MNT_NOWAIT) {
5482 * If we have managed to get rid of all the dirty buffers,
5483 * then we are done. For certain directories and block
5484 * devices, we may need to do further work.
5486 * We must wait for any I/O in progress to finish so that
5487 * all potential buffers on the dirty list will be visible.
5496 * Flush the dependencies associated with an inodedep.
5497 * Called with splbio blocked.
5500 flush_inodedep_deps(mp, ino)
5504 struct inodedep *inodedep;
5508 * This work is done in two passes. The first pass grabs most
5509 * of the buffers and begins asynchronously writing them. The
5510 * only way to wait for these asynchronous writes is to sleep
5511 * on the filesystem vnode which may stay busy for a long time
5512 * if the filesystem is active. So, instead, we make a second
5513 * pass over the dependencies blocking on each write. In the
5514 * usual case we will be blocking against a write that we
5515 * initiated, so when it is done the dependency will have been
5516 * resolved. Thus the second pass is expected to end quickly.
5517 * We give a brief window at the top of the loop to allow
5518 * any pending I/O to complete.
5520 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
5525 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
5527 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
5528 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
5529 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
5530 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
5533 * If pass2, we are done, otherwise do pass 2.
5535 if (waitfor == MNT_WAIT)
5540 * Try freeing inodedep in case all dependencies have been removed.
5542 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
5543 (void) free_inodedep(inodedep);
5548 * Flush an inode dependency list.
5549 * Called with splbio blocked.
5552 flush_deplist(listhead, waitfor, errorp)
5553 struct allocdirectlst *listhead;
5557 struct allocdirect *adp;
5560 mtx_assert(&lk, MA_OWNED);
5561 TAILQ_FOREACH(adp, listhead, ad_next) {
5562 if (adp->ad_state & DEPCOMPLETE)
5565 bp = getdirtybuf(bp, &lk, waitfor);
5567 if (waitfor == MNT_NOWAIT)
5572 if (waitfor == MNT_NOWAIT) {
5574 } else if ((*errorp = bwrite(bp)) != 0) {
5585 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
5586 * Called with splbio blocked.
5589 flush_pagedep_deps(pvp, mp, diraddhdp)
5592 struct diraddhd *diraddhdp;
5594 struct inodedep *inodedep;
5595 struct ufsmount *ump;
5602 struct worklist *wk;
5605 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
5607 * Flush ourselves if this directory entry
5608 * has a MKDIR_PARENT dependency.
5610 if (dap->da_state & MKDIR_PARENT) {
5612 if ((error = ffs_update(pvp, 1)) != 0)
5616 * If that cleared dependencies, go on to next.
5618 if (dap != LIST_FIRST(diraddhdp))
5620 if (dap->da_state & MKDIR_PARENT)
5621 panic("flush_pagedep_deps: MKDIR_PARENT");
5624 * A newly allocated directory must have its "." and
5625 * ".." entries written out before its name can be
5626 * committed in its parent. We do not want or need
5627 * the full semantics of a synchronous ffs_syncvnode as
5628 * that may end up here again, once for each directory
5629 * level in the filesystem. Instead, we push the blocks
5630 * and wait for them to clear. We have to fsync twice
5631 * because the first call may choose to defer blocks
5632 * that still have dependencies, but deferral will
5633 * happen at most once.
5635 inum = dap->da_newinum;
5636 if (dap->da_state & MKDIR_BODY) {
5638 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
5641 if ((error=ffs_syncvnode(vp, MNT_NOWAIT)) ||
5642 (error=ffs_syncvnode(vp, MNT_NOWAIT))) {
5650 * If first block is still dirty with a D_MKDIR
5651 * dependency then it needs to be written now.
5655 bp = gbincore(bo, 0);
5657 break; /* First block not present */
5658 error = BUF_LOCK(bp,
5664 if (error == ENOLCK)
5665 continue; /* Slept, retry */
5668 if ((bp->b_flags & B_DELWRI) == 0) {
5670 break; /* Buffer not dirty */
5672 for (wk = LIST_FIRST(&bp->b_dep);
5674 wk = LIST_NEXT(wk, wk_list))
5675 if (wk->wk_type == D_MKDIR)
5678 BUF_UNLOCK(bp); /* Dependency gone */
5681 * D_MKDIR dependency remains,
5682 * must write buffer to stable
5695 break; /* Flushing of first block failed */
5698 * If that cleared dependencies, go on to next.
5700 if (dap != LIST_FIRST(diraddhdp))
5702 if (dap->da_state & MKDIR_BODY)
5703 panic("flush_pagedep_deps: MKDIR_BODY");
5706 * Flush the inode on which the directory entry depends.
5707 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
5708 * the only remaining dependency is that the updated inode
5709 * count must get pushed to disk. The inode has already
5710 * been pushed into its inode buffer (via VOP_UPDATE) at
5711 * the time of the reference count change. So we need only
5712 * locate that buffer, ensure that there will be no rollback
5713 * caused by a bitmap dependency, then write the inode buffer.
5716 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
5717 panic("flush_pagedep_deps: lost inode");
5719 * If the inode still has bitmap dependencies,
5720 * push them to disk.
5722 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5723 bp = inodedep->id_buf;
5724 bp = getdirtybuf(bp, &lk, MNT_WAIT);
5728 if ((error = bwrite(bp)) != 0)
5731 if (dap != LIST_FIRST(diraddhdp))
5735 * If the inode is still sitting in a buffer waiting
5736 * to be written, push it to disk.
5739 if ((error = bread(ump->um_devvp,
5740 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
5741 (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) {
5745 if ((error = bwrite(bp)) != 0)
5749 * If we have failed to get rid of all the dependencies
5750 * then something is seriously wrong.
5752 if (dap == LIST_FIRST(diraddhdp))
5753 panic("flush_pagedep_deps: flush failed");
5761 * A large burst of file addition or deletion activity can drive the
5762 * memory load excessively high. First attempt to slow things down
5763 * using the techniques below. If that fails, this routine requests
5764 * the offending operations to fall back to running synchronously
5765 * until the memory load returns to a reasonable level.
5768 softdep_slowdown(vp)
5771 int max_softdeps_hard;
5774 max_softdeps_hard = max_softdeps * 11 / 10;
5775 if (num_dirrem < max_softdeps_hard / 2 &&
5776 num_inodedep < max_softdeps_hard &&
5777 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps &&
5778 num_freeblkdep < max_softdeps_hard) {
5782 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps)
5784 stat_sync_limit_hit += 1;
5790 * Called by the allocation routines when they are about to fail
5791 * in the hope that we can free up some disk space.
5793 * First check to see if the work list has anything on it. If it has,
5794 * clean up entries until we successfully free some space. Because this
5795 * process holds inodes locked, we cannot handle any remove requests
5796 * that might block on a locked inode as that could lead to deadlock.
5797 * If the worklist yields no free space, encourage the syncer daemon
5798 * to help us. In no event will we try for longer than tickdelay seconds.
5801 softdep_request_cleanup(fs, vp)
5805 struct ufsmount *ump;
5807 ufs2_daddr_t needed;
5810 ump = VTOI(vp)->i_ump;
5811 mtx_assert(UFS_MTX(ump), MA_OWNED);
5812 needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize;
5813 starttime = time_second + tickdelay;
5815 * If we are being called because of a process doing a
5816 * copy-on-write, then it is not safe to update the vnode
5817 * as we may recurse into the copy-on-write routine.
5819 if (!(curthread->td_pflags & TDP_COWINPROGRESS)) {
5821 error = ffs_update(vp, 1);
5826 while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) {
5827 if (time_second > starttime)
5831 if (ump->softdep_on_worklist > 0 &&
5832 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT) != -1) {
5833 stat_worklist_push += 1;
5838 request_cleanup(UFSTOVFS(ump), FLUSH_REMOVE_WAIT);
5846 * If memory utilization has gotten too high, deliberately slow things
5847 * down and speed up the I/O processing.
5849 extern struct thread *syncertd;
5851 request_cleanup(mp, resource)
5855 struct thread *td = curthread;
5856 struct ufsmount *ump;
5858 mtx_assert(&lk, MA_OWNED);
5860 * We never hold up the filesystem syncer or buf daemon.
5862 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
5866 * First check to see if the work list has gotten backlogged.
5867 * If it has, co-opt this process to help clean up two entries.
5868 * Because this process may hold inodes locked, we cannot
5869 * handle any remove requests that might block on a locked
5870 * inode as that could lead to deadlock. We set TDP_SOFTDEP
5871 * to avoid recursively processing the worklist.
5873 if (ump->softdep_on_worklist > max_softdeps / 10) {
5874 td->td_pflags |= TDP_SOFTDEP;
5875 process_worklist_item(mp, LK_NOWAIT);
5876 process_worklist_item(mp, LK_NOWAIT);
5877 td->td_pflags &= ~TDP_SOFTDEP;
5878 stat_worklist_push += 2;
5882 * Next, we attempt to speed up the syncer process. If that
5883 * is successful, then we allow the process to continue.
5885 if (softdep_speedup() && resource != FLUSH_REMOVE_WAIT)
5888 * If we are resource constrained on inode dependencies, try
5889 * flushing some dirty inodes. Otherwise, we are constrained
5890 * by file deletions, so try accelerating flushes of directories
5891 * with removal dependencies. We would like to do the cleanup
5892 * here, but we probably hold an inode locked at this point and
5893 * that might deadlock against one that we try to clean. So,
5894 * the best that we can do is request the syncer daemon to do
5895 * the cleanup for us.
5900 stat_ino_limit_push += 1;
5901 req_clear_inodedeps += 1;
5902 stat_countp = &stat_ino_limit_hit;
5906 case FLUSH_REMOVE_WAIT:
5907 stat_blk_limit_push += 1;
5908 req_clear_remove += 1;
5909 stat_countp = &stat_blk_limit_hit;
5913 panic("request_cleanup: unknown type");
5916 * Hopefully the syncer daemon will catch up and awaken us.
5917 * We wait at most tickdelay before proceeding in any case.
5920 if (callout_pending(&softdep_callout) == FALSE)
5921 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
5924 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
5930 * Awaken processes pausing in request_cleanup and clear proc_waiting
5931 * to indicate that there is no longer a timer running.
5939 * The callout_ API has acquired mtx and will hold it around this
5943 wakeup_one(&proc_waiting);
5944 if (proc_waiting > 0)
5945 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
5950 * Flush out a directory with at least one removal dependency in an effort to
5951 * reduce the number of dirrem, freefile, and freeblks dependency structures.
5957 struct pagedep_hashhead *pagedephd;
5958 struct pagedep *pagedep;
5959 static int next = 0;
5966 mtx_assert(&lk, MA_OWNED);
5968 for (cnt = 0; cnt < pagedep_hash; cnt++) {
5969 pagedephd = &pagedep_hashtbl[next++];
5970 if (next >= pagedep_hash)
5972 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
5973 if (LIST_EMPTY(&pagedep->pd_dirremhd))
5975 mp = pagedep->pd_list.wk_mp;
5976 ino = pagedep->pd_ino;
5977 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5982 * Let unmount clear deps
5984 error = vfs_busy(mp, MBF_NOWAIT);
5987 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
5991 softdep_error("clear_remove: vget", error);
5994 if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
5995 softdep_error("clear_remove: fsync", error);
6002 vn_finished_write(mp);
6010 * Clear out a block of dirty inodes in an effort to reduce
6011 * the number of inodedep dependency structures.
6017 struct inodedep_hashhead *inodedephd;
6018 struct inodedep *inodedep;
6019 static int next = 0;
6024 ino_t firstino, lastino, ino;
6026 mtx_assert(&lk, MA_OWNED);
6028 * Pick a random inode dependency to be cleared.
6029 * We will then gather up all the inodes in its block
6030 * that have dependencies and flush them out.
6032 for (cnt = 0; cnt < inodedep_hash; cnt++) {
6033 inodedephd = &inodedep_hashtbl[next++];
6034 if (next >= inodedep_hash)
6036 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
6039 if (inodedep == NULL)
6041 fs = inodedep->id_fs;
6042 mp = inodedep->id_list.wk_mp;
6044 * Find the last inode in the block with dependencies.
6046 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
6047 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
6048 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
6051 * Asynchronously push all but the last inode with dependencies.
6052 * Synchronously push the last inode with dependencies to ensure
6053 * that the inode block gets written to free up the inodedeps.
6055 for (ino = firstino; ino <= lastino; ino++) {
6056 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
6058 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
6061 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */
6063 vn_finished_write(mp);
6067 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
6068 FFSV_FORCEINSMQ)) != 0) {
6069 softdep_error("clear_inodedeps: vget", error);
6071 vn_finished_write(mp);
6076 if (ino == lastino) {
6077 if ((error = ffs_syncvnode(vp, MNT_WAIT)))
6078 softdep_error("clear_inodedeps: fsync1", error);
6080 if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
6081 softdep_error("clear_inodedeps: fsync2", error);
6082 BO_LOCK(&vp->v_bufobj);
6084 BO_UNLOCK(&vp->v_bufobj);
6087 vn_finished_write(mp);
6093 * Function to determine if the buffer has outstanding dependencies
6094 * that will cause a roll-back if the buffer is written. If wantcount
6095 * is set, return number of dependencies, otherwise just yes or no.
6098 softdep_count_dependencies(bp, wantcount)
6102 struct worklist *wk;
6103 struct inodedep *inodedep;
6104 struct indirdep *indirdep;
6105 struct allocindir *aip;
6106 struct pagedep *pagedep;
6112 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6113 switch (wk->wk_type) {
6116 inodedep = WK_INODEDEP(wk);
6117 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
6118 /* bitmap allocation dependency */
6123 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
6124 /* direct block pointer dependency */
6129 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
6130 /* direct block pointer dependency */
6138 indirdep = WK_INDIRDEP(wk);
6140 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
6141 /* indirect block pointer dependency */
6149 pagedep = WK_PAGEDEP(wk);
6150 for (i = 0; i < DAHASHSZ; i++) {
6152 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
6153 /* directory entry dependency */
6165 /* never a dependency on these blocks */
6169 panic("softdep_check_for_rollback: Unexpected type %s",
6170 TYPENAME(wk->wk_type));
6180 * Acquire exclusive access to a buffer.
6181 * Must be called with a locked mtx parameter.
6182 * Return acquired buffer or NULL on failure.
6185 getdirtybuf(bp, mtx, waitfor)
6192 mtx_assert(mtx, MA_OWNED);
6193 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
6194 if (waitfor != MNT_WAIT)
6196 error = BUF_LOCK(bp,
6197 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx);
6199 * Even if we sucessfully acquire bp here, we have dropped
6200 * mtx, which may violates our guarantee.
6204 else if (error != ENOLCK)
6205 panic("getdirtybuf: inconsistent lock: %d", error);
6209 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6210 if (mtx == &lk && waitfor == MNT_WAIT) {
6212 BO_LOCK(bp->b_bufobj);
6214 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6215 bp->b_vflags |= BV_BKGRDWAIT;
6216 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj),
6217 PRIBIO | PDROP, "getbuf", 0);
6219 BO_UNLOCK(bp->b_bufobj);
6224 if (waitfor != MNT_WAIT)
6227 * The mtx argument must be bp->b_vp's mutex in
6230 #ifdef DEBUG_VFS_LOCKS
6231 if (bp->b_vp->v_type != VCHR)
6232 ASSERT_BO_LOCKED(bp->b_bufobj);
6234 bp->b_vflags |= BV_BKGRDWAIT;
6235 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0);
6238 if ((bp->b_flags & B_DELWRI) == 0) {
6248 * Check if it is safe to suspend the file system now. On entry,
6249 * the vnode interlock for devvp should be held. Return 0 with
6250 * the mount interlock held if the file system can be suspended now,
6251 * otherwise return EAGAIN with the mount interlock held.
6254 softdep_check_suspend(struct mount *mp,
6255 struct vnode *devvp,
6257 int softdep_accdeps,
6258 int secondary_writes,
6259 int secondary_accwrites)
6262 struct ufsmount *ump;
6266 bo = &devvp->v_bufobj;
6267 ASSERT_BO_LOCKED(bo);
6270 if (!TRY_ACQUIRE_LOCK(&lk)) {
6278 if (mp->mnt_secondary_writes != 0) {
6281 msleep(&mp->mnt_secondary_writes,
6283 (PUSER - 1) | PDROP, "secwr", 0);
6291 * Reasons for needing more work before suspend:
6292 * - Dirty buffers on devvp.
6293 * - Softdep activity occurred after start of vnode sync loop
6294 * - Secondary writes occurred after start of vnode sync loop
6297 if (bo->bo_numoutput > 0 ||
6298 bo->bo_dirty.bv_cnt > 0 ||
6299 softdep_deps != 0 ||
6300 ump->softdep_deps != 0 ||
6301 softdep_accdeps != ump->softdep_accdeps ||
6302 secondary_writes != 0 ||
6303 mp->mnt_secondary_writes != 0 ||
6304 secondary_accwrites != mp->mnt_secondary_accwrites)
6313 * Get the number of dependency structures for the file system, both
6314 * the current number and the total number allocated. These will
6315 * later be used to detect that softdep processing has occurred.
6318 softdep_get_depcounts(struct mount *mp,
6320 int *softdep_accdepsp)
6322 struct ufsmount *ump;
6326 *softdep_depsp = ump->softdep_deps;
6327 *softdep_accdepsp = ump->softdep_accdeps;
6332 * Wait for pending output on a vnode to complete.
6333 * Must be called with vnode lock and interlock locked.
6335 * XXX: Should just be a call to bufobj_wwait().
6344 ASSERT_VOP_LOCKED(vp, "drain_output");
6345 ASSERT_BO_LOCKED(bo);
6347 while (bo->bo_numoutput) {
6348 bo->bo_flag |= BO_WWAIT;
6349 msleep((caddr_t)&bo->bo_numoutput,
6350 BO_MTX(bo), PRIBIO + 1, "drainvp", 0);
6355 * Called whenever a buffer that is being invalidated or reallocated
6356 * contains dependencies. This should only happen if an I/O error has
6357 * occurred. The routine is called with the buffer locked.
6360 softdep_deallocate_dependencies(bp)
6364 if ((bp->b_ioflags & BIO_ERROR) == 0)
6365 panic("softdep_deallocate_dependencies: dangling deps");
6366 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
6367 panic("softdep_deallocate_dependencies: unrecovered I/O error");
6371 * Function to handle asynchronous write errors in the filesystem.
6374 softdep_error(func, error)
6379 /* XXX should do something better! */
6380 printf("%s: got error %d while accessing filesystem\n", func, error);
6385 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps)
6387 struct inodedep_hashhead *inodedephd;
6388 struct inodedep *inodedep;
6392 fs = have_addr ? (struct fs *)addr : NULL;
6393 for (cnt = 0; cnt < inodedep_hash; cnt++) {
6394 inodedephd = &inodedep_hashtbl[cnt];
6395 LIST_FOREACH(inodedep, inodedephd, id_hash) {
6396 if (fs != NULL && fs != inodedep->id_fs)
6398 db_printf("%p fs %p st %x ino %jd inoblk %jd\n",
6399 inodedep, inodedep->id_fs, inodedep->id_state,
6400 (intmax_t)inodedep->id_ino,
6401 (intmax_t)fsbtodb(inodedep->id_fs,
6402 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)));
6409 #endif /* SOFTUPDATES */