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
669 static int stat_worklist_push; /* number of worklist cleanups */
670 static int stat_blk_limit_push; /* number of times block limit neared */
671 static int stat_ino_limit_push; /* number of times inode limit neared */
672 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
673 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
674 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
675 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
676 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
677 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
678 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
680 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
681 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
682 SYSCTL_INT(_debug, OID_AUTO, maxindirdeps, CTLFLAG_RW, &maxindirdeps, 0, "");
683 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
684 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
685 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
686 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
687 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
688 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
689 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
690 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
691 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
692 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
693 /* SYSCTL_INT(_debug, OID_AUTO, worklist_num, CTLFLAG_RD, &softdep_on_worklist, 0, ""); */
695 SYSCTL_DECL(_vfs_ffs);
697 static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */
698 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
699 &compute_summary_at_mount, 0, "Recompute summary at mount");
701 static struct proc *softdepproc;
702 static struct kproc_desc softdep_kp = {
707 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start,
715 struct ufsmount *ump;
721 td->td_pflags |= TDP_NORUNNINGBUF;
724 kproc_suspend_check(softdepproc);
725 vfslocked = VFS_LOCK_GIANT((struct mount *)NULL);
728 * If requested, try removing inode or removal dependencies.
730 if (req_clear_inodedeps) {
732 req_clear_inodedeps -= 1;
733 wakeup_one(&proc_waiting);
735 if (req_clear_remove) {
737 req_clear_remove -= 1;
738 wakeup_one(&proc_waiting);
741 VFS_UNLOCK_GIANT(vfslocked);
743 mtx_lock(&mountlist_mtx);
744 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
745 nmp = TAILQ_NEXT(mp, mnt_list);
746 if ((mp->mnt_flag & MNT_SOFTDEP) == 0)
748 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
750 vfslocked = VFS_LOCK_GIANT(mp);
751 softdep_process_worklist(mp, 0);
753 remaining += ump->softdep_on_worklist -
754 ump->softdep_on_worklist_inprogress;
755 VFS_UNLOCK_GIANT(vfslocked);
756 mtx_lock(&mountlist_mtx);
757 nmp = TAILQ_NEXT(mp, mnt_list);
760 mtx_unlock(&mountlist_mtx);
765 msleep(&req_pending, &lk, PVM, "sdflush", hz);
772 softdep_speedup(void)
775 mtx_assert(&lk, MA_OWNED);
776 if (req_pending == 0) {
778 wakeup(&req_pending);
781 return speedup_syncer();
785 * Add an item to the end of the work queue.
786 * This routine requires that the lock be held.
787 * This is the only routine that adds items to the list.
788 * The following routine is the only one that removes items
789 * and does so in order from first to last.
795 struct ufsmount *ump;
797 mtx_assert(&lk, MA_OWNED);
798 ump = VFSTOUFS(wk->wk_mp);
799 if (wk->wk_state & ONWORKLIST)
800 panic("add_to_worklist: already on list");
801 wk->wk_state |= ONWORKLIST;
802 if (LIST_EMPTY(&ump->softdep_workitem_pending))
803 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
805 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
806 ump->softdep_worklist_tail = wk;
807 ump->softdep_on_worklist += 1;
811 * Process that runs once per second to handle items in the background queue.
813 * Note that we ensure that everything is done in the order in which they
814 * appear in the queue. The code below depends on this property to ensure
815 * that blocks of a file are freed before the inode itself is freed. This
816 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
817 * until all the old ones have been purged from the dependency lists.
820 softdep_process_worklist(mp, full)
824 struct thread *td = curthread;
825 int cnt, matchcnt, loopcount;
826 struct ufsmount *ump;
829 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
831 * Record the process identifier of our caller so that we can give
832 * this process preferential treatment in request_cleanup below.
838 starttime = time_second;
839 while (ump->softdep_on_worklist > 0) {
840 if ((cnt = process_worklist_item(mp, 0)) == -1)
845 * If requested, try removing inode or removal dependencies.
847 if (req_clear_inodedeps) {
849 req_clear_inodedeps -= 1;
850 wakeup_one(&proc_waiting);
852 if (req_clear_remove) {
854 req_clear_remove -= 1;
855 wakeup_one(&proc_waiting);
858 * We do not generally want to stop for buffer space, but if
859 * we are really being a buffer hog, we will stop and wait.
861 if (loopcount++ % 128 == 0) {
868 * Never allow processing to run for more than one
869 * second. Otherwise the other mountpoints may get
870 * excessively backlogged.
872 if (!full && starttime != time_second) {
882 * Process one item on the worklist.
885 process_worklist_item(mp, flags)
889 struct worklist *wk, *wkend;
890 struct ufsmount *ump;
894 mtx_assert(&lk, MA_OWNED);
895 KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
897 * If we are being called because of a process doing a
898 * copy-on-write, then it is not safe to write as we may
899 * recurse into the copy-on-write routine.
901 if (curthread->td_pflags & TDP_COWINPROGRESS)
904 * Normally we just process each item on the worklist in order.
905 * However, if we are in a situation where we cannot lock any
906 * inodes, we have to skip over any dirrem requests whose
907 * vnodes are resident and locked.
911 LIST_FOREACH(wk, &ump->softdep_workitem_pending, wk_list) {
912 if (wk->wk_state & INPROGRESS)
914 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
916 wk->wk_state |= INPROGRESS;
917 ump->softdep_on_worklist_inprogress++;
919 ffs_vgetf(mp, WK_DIRREM(wk)->dm_oldinum,
920 LK_NOWAIT | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ);
922 wk->wk_state &= ~INPROGRESS;
923 ump->softdep_on_worklist_inprogress--;
930 * Remove the item to be processed. If we are removing the last
931 * item on the list, we need to recalculate the tail pointer.
932 * As this happens rarely and usually when the list is short,
933 * we just run down the list to find it rather than tracking it
937 if (wk == ump->softdep_worklist_tail) {
938 LIST_FOREACH(wkend, &ump->softdep_workitem_pending, wk_list)
939 if (LIST_NEXT(wkend, wk_list) == NULL)
941 ump->softdep_worklist_tail = wkend;
943 ump->softdep_on_worklist -= 1;
945 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
946 panic("process_worklist_item: suspended filesystem");
948 switch (wk->wk_type) {
951 /* removal of a directory entry */
952 handle_workitem_remove(WK_DIRREM(wk), vp);
956 /* releasing blocks and/or fragments from a file */
957 handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT);
961 /* releasing a fragment when replaced as a file grows */
962 handle_workitem_freefrag(WK_FREEFRAG(wk));
966 /* releasing an inode when its link count drops to 0 */
967 handle_workitem_freefile(WK_FREEFILE(wk));
971 panic("%s_process_worklist: Unknown type %s",
972 "softdep", TYPENAME(wk->wk_type));
975 vn_finished_secondary_write(mp);
981 * Move dependencies from one buffer to another.
984 softdep_move_dependencies(oldbp, newbp)
988 struct worklist *wk, *wktail;
990 if (!LIST_EMPTY(&newbp->b_dep))
991 panic("softdep_move_dependencies: need merge code");
994 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
995 LIST_REMOVE(wk, wk_list);
997 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
999 LIST_INSERT_AFTER(wktail, wk, wk_list);
1006 * Purge the work list of all items associated with a particular mount point.
1009 softdep_flushworklist(oldmnt, countp, td)
1010 struct mount *oldmnt;
1014 struct vnode *devvp;
1015 int count, error = 0;
1016 struct ufsmount *ump;
1019 * Alternately flush the block device associated with the mount
1020 * point and process any dependencies that the flushing
1021 * creates. We continue until no more worklist dependencies
1025 ump = VFSTOUFS(oldmnt);
1026 devvp = ump->um_devvp;
1027 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1029 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1030 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1031 VOP_UNLOCK(devvp, 0);
1039 softdep_waitidle(struct mount *mp)
1041 struct ufsmount *ump;
1047 for (i = 0; i < 10 && ump->softdep_deps; i++) {
1048 ump->softdep_req = 1;
1049 if (ump->softdep_on_worklist)
1050 panic("softdep_waitidle: work added after flush.");
1051 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1);
1053 ump->softdep_req = 0;
1058 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1066 * Flush all vnodes and worklist items associated with a specified mount point.
1069 softdep_flushfiles(oldmnt, flags, td)
1070 struct mount *oldmnt;
1074 int error, depcount, loopcnt, retry_flush_count, retry;
1077 retry_flush_count = 3;
1082 * Alternately flush the vnodes associated with the mount
1083 * point and process any dependencies that the flushing
1084 * creates. In theory, this loop can happen at most twice,
1085 * but we give it a few extra just to be sure.
1087 for (; loopcnt > 0; loopcnt--) {
1089 * Do another flush in case any vnodes were brought in
1090 * as part of the cleanup operations.
1092 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
1094 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 ||
1099 * If we are unmounting then it is an error to fail. If we
1100 * are simply trying to downgrade to read-only, then filesystem
1101 * activity can keep us busy forever, so we just fail with EBUSY.
1104 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
1105 panic("softdep_flushfiles: looping");
1109 error = softdep_waitidle(oldmnt);
1111 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) {
1114 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0,
1115 ("softdep_flushfiles: !MNTK_NOINSMNTQ"));
1116 if (oldmnt->mnt_nvnodelistsize > 0) {
1117 if (--retry_flush_count > 0) {
1123 MNT_IUNLOCK(oldmnt);
1132 * Structure hashing.
1134 * There are three types of structures that can be looked up:
1135 * 1) pagedep structures identified by mount point, inode number,
1136 * and logical block.
1137 * 2) inodedep structures identified by mount point and inode number.
1138 * 3) newblk structures identified by mount point and
1139 * physical block number.
1141 * The "pagedep" and "inodedep" dependency structures are hashed
1142 * separately from the file blocks and inodes to which they correspond.
1143 * This separation helps when the in-memory copy of an inode or
1144 * file block must be replaced. It also obviates the need to access
1145 * an inode or file page when simply updating (or de-allocating)
1146 * dependency structures. Lookup of newblk structures is needed to
1147 * find newly allocated blocks when trying to associate them with
1148 * their allocdirect or allocindir structure.
1150 * The lookup routines optionally create and hash a new instance when
1151 * an existing entry is not found.
1153 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
1154 #define NODELAY 0x0002 /* cannot do background work */
1157 * Structures and routines associated with pagedep caching.
1159 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
1160 u_long pagedep_hash; /* size of hash table - 1 */
1161 #define PAGEDEP_HASH(mp, inum, lbn) \
1162 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
1166 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp)
1167 struct pagedep_hashhead *pagedephd;
1172 struct pagedep **pagedeppp;
1174 struct pagedep *pagedep;
1176 LIST_FOREACH(pagedep, pagedephd, pd_hash)
1177 if (ino == pagedep->pd_ino &&
1178 lbn == pagedep->pd_lbn &&
1179 mp == pagedep->pd_list.wk_mp)
1182 *pagedeppp = pagedep;
1183 if ((flags & DEPALLOC) != 0 &&
1184 (pagedep->pd_state & ONWORKLIST) == 0)
1192 * Look up a pagedep. Return 1 if found, 0 if not found or found
1193 * when asked to allocate but not associated with any buffer.
1194 * If not found, allocate if DEPALLOC flag is passed.
1195 * Found or allocated entry is returned in pagedeppp.
1196 * This routine must be called with splbio interrupts blocked.
1199 pagedep_lookup(ip, lbn, flags, pagedeppp)
1203 struct pagedep **pagedeppp;
1205 struct pagedep *pagedep;
1206 struct pagedep_hashhead *pagedephd;
1211 mtx_assert(&lk, MA_OWNED);
1212 mp = ITOV(ip)->v_mount;
1213 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
1215 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1216 if (*pagedeppp || (flags & DEPALLOC) == 0)
1219 pagedep = malloc(sizeof(struct pagedep),
1220 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
1221 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
1223 ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1225 WORKITEM_FREE(pagedep, D_PAGEDEP);
1228 pagedep->pd_ino = ip->i_number;
1229 pagedep->pd_lbn = lbn;
1230 LIST_INIT(&pagedep->pd_dirremhd);
1231 LIST_INIT(&pagedep->pd_pendinghd);
1232 for (i = 0; i < DAHASHSZ; i++)
1233 LIST_INIT(&pagedep->pd_diraddhd[i]);
1234 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
1235 *pagedeppp = pagedep;
1240 * Structures and routines associated with inodedep caching.
1242 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
1243 static u_long inodedep_hash; /* size of hash table - 1 */
1244 static long num_inodedep; /* number of inodedep allocated */
1245 #define INODEDEP_HASH(fs, inum) \
1246 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
1249 inodedep_find(inodedephd, fs, inum, inodedeppp)
1250 struct inodedep_hashhead *inodedephd;
1253 struct inodedep **inodedeppp;
1255 struct inodedep *inodedep;
1257 LIST_FOREACH(inodedep, inodedephd, id_hash)
1258 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
1261 *inodedeppp = inodedep;
1269 * Look up an inodedep. Return 1 if found, 0 if not found.
1270 * If not found, allocate if DEPALLOC flag is passed.
1271 * Found or allocated entry is returned in inodedeppp.
1272 * This routine must be called with splbio interrupts blocked.
1275 inodedep_lookup(mp, inum, flags, inodedeppp)
1279 struct inodedep **inodedeppp;
1281 struct inodedep *inodedep;
1282 struct inodedep_hashhead *inodedephd;
1285 mtx_assert(&lk, MA_OWNED);
1286 fs = VFSTOUFS(mp)->um_fs;
1287 inodedephd = INODEDEP_HASH(fs, inum);
1289 if (inodedep_find(inodedephd, fs, inum, inodedeppp))
1291 if ((flags & DEPALLOC) == 0)
1294 * If we are over our limit, try to improve the situation.
1296 if (num_inodedep > max_softdeps && (flags & NODELAY) == 0)
1297 request_cleanup(mp, FLUSH_INODES);
1299 inodedep = malloc(sizeof(struct inodedep),
1300 M_INODEDEP, M_SOFTDEP_FLAGS);
1301 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
1303 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) {
1304 WORKITEM_FREE(inodedep, D_INODEDEP);
1308 inodedep->id_fs = fs;
1309 inodedep->id_ino = inum;
1310 inodedep->id_state = ALLCOMPLETE;
1311 inodedep->id_nlinkdelta = 0;
1312 inodedep->id_savedino1 = NULL;
1313 inodedep->id_savedsize = -1;
1314 inodedep->id_savedextsize = -1;
1315 inodedep->id_buf = NULL;
1316 LIST_INIT(&inodedep->id_pendinghd);
1317 LIST_INIT(&inodedep->id_inowait);
1318 LIST_INIT(&inodedep->id_bufwait);
1319 TAILQ_INIT(&inodedep->id_inoupdt);
1320 TAILQ_INIT(&inodedep->id_newinoupdt);
1321 TAILQ_INIT(&inodedep->id_extupdt);
1322 TAILQ_INIT(&inodedep->id_newextupdt);
1323 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1324 *inodedeppp = inodedep;
1329 * Structures and routines associated with newblk caching.
1331 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1332 u_long newblk_hash; /* size of hash table - 1 */
1333 #define NEWBLK_HASH(fs, inum) \
1334 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1337 newblk_find(newblkhd, fs, newblkno, newblkpp)
1338 struct newblk_hashhead *newblkhd;
1340 ufs2_daddr_t newblkno;
1341 struct newblk **newblkpp;
1343 struct newblk *newblk;
1345 LIST_FOREACH(newblk, newblkhd, nb_hash)
1346 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1357 * Look up a newblk. Return 1 if found, 0 if not found.
1358 * If not found, allocate if DEPALLOC flag is passed.
1359 * Found or allocated entry is returned in newblkpp.
1362 newblk_lookup(fs, newblkno, flags, newblkpp)
1364 ufs2_daddr_t newblkno;
1366 struct newblk **newblkpp;
1368 struct newblk *newblk;
1369 struct newblk_hashhead *newblkhd;
1371 newblkhd = NEWBLK_HASH(fs, newblkno);
1372 if (newblk_find(newblkhd, fs, newblkno, newblkpp))
1374 if ((flags & DEPALLOC) == 0)
1377 newblk = malloc(sizeof(struct newblk),
1378 M_NEWBLK, M_SOFTDEP_FLAGS);
1380 if (newblk_find(newblkhd, fs, newblkno, newblkpp)) {
1381 free(newblk, M_NEWBLK);
1384 newblk->nb_state = 0;
1386 newblk->nb_newblkno = newblkno;
1387 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1393 * Executed during filesystem system initialization before
1394 * mounting any filesystems.
1397 softdep_initialize()
1400 LIST_INIT(&mkdirlisthd);
1401 max_softdeps = desiredvnodes * 4;
1402 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1404 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1405 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1407 /* initialise bioops hack */
1408 bioops.io_start = softdep_disk_io_initiation;
1409 bioops.io_complete = softdep_disk_write_complete;
1410 bioops.io_deallocate = softdep_deallocate_dependencies;
1411 bioops.io_countdeps = softdep_count_dependencies;
1413 /* Initialize the callout with an mtx. */
1414 callout_init_mtx(&softdep_callout, &lk, 0);
1418 * Executed after all filesystems have been unmounted during
1419 * filesystem module unload.
1422 softdep_uninitialize()
1425 callout_drain(&softdep_callout);
1426 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash);
1427 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash);
1428 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash);
1432 * Called at mount time to notify the dependency code that a
1433 * filesystem wishes to use it.
1436 softdep_mount(devvp, mp, fs, cred)
1437 struct vnode *devvp;
1442 struct csum_total cstotal;
1443 struct ufsmount *ump;
1449 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
1450 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
1451 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) |
1457 LIST_INIT(&ump->softdep_workitem_pending);
1458 ump->softdep_worklist_tail = NULL;
1459 ump->softdep_on_worklist = 0;
1460 ump->softdep_deps = 0;
1462 * When doing soft updates, the counters in the
1463 * superblock may have gotten out of sync. Recomputation
1464 * can take a long time and can be deferred for background
1465 * fsck. However, the old behavior of scanning the cylinder
1466 * groups and recalculating them at mount time is available
1467 * by setting vfs.ffs.compute_summary_at_mount to one.
1469 if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
1471 bzero(&cstotal, sizeof cstotal);
1472 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1473 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1474 fs->fs_cgsize, cred, &bp)) != 0) {
1478 cgp = (struct cg *)bp->b_data;
1479 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1480 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1481 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1482 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1483 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1487 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1488 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
1490 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1495 * Protecting the freemaps (or bitmaps).
1497 * To eliminate the need to execute fsck before mounting a filesystem
1498 * after a power failure, one must (conservatively) guarantee that the
1499 * on-disk copy of the bitmaps never indicate that a live inode or block is
1500 * free. So, when a block or inode is allocated, the bitmap should be
1501 * updated (on disk) before any new pointers. When a block or inode is
1502 * freed, the bitmap should not be updated until all pointers have been
1503 * reset. The latter dependency is handled by the delayed de-allocation
1504 * approach described below for block and inode de-allocation. The former
1505 * dependency is handled by calling the following procedure when a block or
1506 * inode is allocated. When an inode is allocated an "inodedep" is created
1507 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1508 * Each "inodedep" is also inserted into the hash indexing structure so
1509 * that any additional link additions can be made dependent on the inode
1512 * The ufs filesystem maintains a number of free block counts (e.g., per
1513 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1514 * in addition to the bitmaps. These counts are used to improve efficiency
1515 * during allocation and therefore must be consistent with the bitmaps.
1516 * There is no convenient way to guarantee post-crash consistency of these
1517 * counts with simple update ordering, for two main reasons: (1) The counts
1518 * and bitmaps for a single cylinder group block are not in the same disk
1519 * sector. If a disk write is interrupted (e.g., by power failure), one may
1520 * be written and the other not. (2) Some of the counts are located in the
1521 * superblock rather than the cylinder group block. So, we focus our soft
1522 * updates implementation on protecting the bitmaps. When mounting a
1523 * filesystem, we recompute the auxiliary counts from the bitmaps.
1527 * Called just after updating the cylinder group block to allocate an inode.
1530 softdep_setup_inomapdep(bp, ip, newinum)
1531 struct buf *bp; /* buffer for cylgroup block with inode map */
1532 struct inode *ip; /* inode related to allocation */
1533 ino_t newinum; /* new inode number being allocated */
1535 struct inodedep *inodedep;
1536 struct bmsafemap *bmsafemap;
1539 * Create a dependency for the newly allocated inode.
1540 * Panic if it already exists as something is seriously wrong.
1541 * Otherwise add it to the dependency list for the buffer holding
1542 * the cylinder group map from which it was allocated.
1545 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), newinum, DEPALLOC|NODELAY,
1547 panic("softdep_setup_inomapdep: dependency for new inode "
1549 inodedep->id_buf = bp;
1550 inodedep->id_state &= ~DEPCOMPLETE;
1551 bmsafemap = bmsafemap_lookup(inodedep->id_list.wk_mp, bp);
1552 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1557 * Called just after updating the cylinder group block to
1558 * allocate block or fragment.
1561 softdep_setup_blkmapdep(bp, mp, newblkno)
1562 struct buf *bp; /* buffer for cylgroup block with block map */
1563 struct mount *mp; /* filesystem doing allocation */
1564 ufs2_daddr_t newblkno; /* number of newly allocated block */
1566 struct newblk *newblk;
1567 struct bmsafemap *bmsafemap;
1570 fs = VFSTOUFS(mp)->um_fs;
1572 * Create a dependency for the newly allocated block.
1573 * Add it to the dependency list for the buffer holding
1574 * the cylinder group map from which it was allocated.
1577 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1578 panic("softdep_setup_blkmapdep: found block");
1579 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp);
1580 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1585 * Find the bmsafemap associated with a cylinder group buffer.
1586 * If none exists, create one. The buffer must be locked when
1587 * this routine is called and this routine must be called with
1588 * splbio interrupts blocked.
1590 static struct bmsafemap *
1591 bmsafemap_lookup(mp, bp)
1595 struct bmsafemap *bmsafemap;
1596 struct worklist *wk;
1598 mtx_assert(&lk, MA_OWNED);
1599 LIST_FOREACH(wk, &bp->b_dep, wk_list)
1600 if (wk->wk_type == D_BMSAFEMAP)
1601 return (WK_BMSAFEMAP(wk));
1603 bmsafemap = malloc(sizeof(struct bmsafemap),
1604 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1605 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
1606 bmsafemap->sm_buf = bp;
1607 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1608 LIST_INIT(&bmsafemap->sm_allocindirhd);
1609 LIST_INIT(&bmsafemap->sm_inodedephd);
1610 LIST_INIT(&bmsafemap->sm_newblkhd);
1612 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1617 * Direct block allocation dependencies.
1619 * When a new block is allocated, the corresponding disk locations must be
1620 * initialized (with zeros or new data) before the on-disk inode points to
1621 * them. Also, the freemap from which the block was allocated must be
1622 * updated (on disk) before the inode's pointer. These two dependencies are
1623 * independent of each other and are needed for all file blocks and indirect
1624 * blocks that are pointed to directly by the inode. Just before the
1625 * "in-core" version of the inode is updated with a newly allocated block
1626 * number, a procedure (below) is called to setup allocation dependency
1627 * structures. These structures are removed when the corresponding
1628 * dependencies are satisfied or when the block allocation becomes obsolete
1629 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1630 * fragment that gets upgraded). All of these cases are handled in
1631 * procedures described later.
1633 * When a file extension causes a fragment to be upgraded, either to a larger
1634 * fragment or to a full block, the on-disk location may change (if the
1635 * previous fragment could not simply be extended). In this case, the old
1636 * fragment must be de-allocated, but not until after the inode's pointer has
1637 * been updated. In most cases, this is handled by later procedures, which
1638 * will construct a "freefrag" structure to be added to the workitem queue
1639 * when the inode update is complete (or obsolete). The main exception to
1640 * this is when an allocation occurs while a pending allocation dependency
1641 * (for the same block pointer) remains. This case is handled in the main
1642 * allocation dependency setup procedure by immediately freeing the
1643 * unreferenced fragments.
1646 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1647 struct inode *ip; /* inode to which block is being added */
1648 ufs_lbn_t lbn; /* block pointer within inode */
1649 ufs2_daddr_t newblkno; /* disk block number being added */
1650 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */
1651 long newsize; /* size of new block */
1652 long oldsize; /* size of new block */
1653 struct buf *bp; /* bp for allocated block */
1655 struct allocdirect *adp, *oldadp;
1656 struct allocdirectlst *adphead;
1657 struct bmsafemap *bmsafemap;
1658 struct inodedep *inodedep;
1659 struct pagedep *pagedep;
1660 struct newblk *newblk;
1663 mp = UFSTOVFS(ip->i_ump);
1664 adp = malloc(sizeof(struct allocdirect),
1665 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1666 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1668 adp->ad_newblkno = newblkno;
1669 adp->ad_oldblkno = oldblkno;
1670 adp->ad_newsize = newsize;
1671 adp->ad_oldsize = oldsize;
1672 adp->ad_state = ATTACHED;
1673 LIST_INIT(&adp->ad_newdirblk);
1674 if (newblkno == oldblkno)
1675 adp->ad_freefrag = NULL;
1677 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1680 if (lbn >= NDADDR) {
1681 /* allocating an indirect block */
1683 panic("softdep_setup_allocdirect: non-zero indir");
1686 * Allocating a direct block.
1688 * If we are allocating a directory block, then we must
1689 * allocate an associated pagedep to track additions and
1692 if ((ip->i_mode & IFMT) == IFDIR &&
1693 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1694 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1696 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1697 panic("softdep_setup_allocdirect: lost block");
1698 if (newblk->nb_state == DEPCOMPLETE) {
1699 adp->ad_state |= DEPCOMPLETE;
1702 bmsafemap = newblk->nb_bmsafemap;
1703 adp->ad_buf = bmsafemap->sm_buf;
1704 LIST_REMOVE(newblk, nb_deps);
1705 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1707 LIST_REMOVE(newblk, nb_hash);
1708 free(newblk, M_NEWBLK);
1710 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1711 adp->ad_inodedep = inodedep;
1712 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1714 * The list of allocdirects must be kept in sorted and ascending
1715 * order so that the rollback routines can quickly determine the
1716 * first uncommitted block (the size of the file stored on disk
1717 * ends at the end of the lowest committed fragment, or if there
1718 * are no fragments, at the end of the highest committed block).
1719 * Since files generally grow, the typical case is that the new
1720 * block is to be added at the end of the list. We speed this
1721 * special case by checking against the last allocdirect in the
1722 * list before laboriously traversing the list looking for the
1725 adphead = &inodedep->id_newinoupdt;
1726 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1727 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1728 /* insert at end of list */
1729 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1730 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1731 allocdirect_merge(adphead, adp, oldadp);
1735 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1736 if (oldadp->ad_lbn >= lbn)
1740 panic("softdep_setup_allocdirect: lost entry");
1741 /* insert in middle of list */
1742 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1743 if (oldadp->ad_lbn == lbn)
1744 allocdirect_merge(adphead, adp, oldadp);
1749 * Replace an old allocdirect dependency with a newer one.
1750 * This routine must be called with splbio interrupts blocked.
1753 allocdirect_merge(adphead, newadp, oldadp)
1754 struct allocdirectlst *adphead; /* head of list holding allocdirects */
1755 struct allocdirect *newadp; /* allocdirect being added */
1756 struct allocdirect *oldadp; /* existing allocdirect being checked */
1758 struct worklist *wk;
1759 struct freefrag *freefrag;
1760 struct newdirblk *newdirblk;
1762 mtx_assert(&lk, MA_OWNED);
1763 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1764 newadp->ad_oldsize != oldadp->ad_newsize ||
1765 newadp->ad_lbn >= NDADDR)
1766 panic("%s %jd != new %jd || old size %ld != new %ld",
1767 "allocdirect_merge: old blkno",
1768 (intmax_t)newadp->ad_oldblkno,
1769 (intmax_t)oldadp->ad_newblkno,
1770 newadp->ad_oldsize, oldadp->ad_newsize);
1771 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1772 newadp->ad_oldsize = oldadp->ad_oldsize;
1774 * If the old dependency had a fragment to free or had never
1775 * previously had a block allocated, then the new dependency
1776 * can immediately post its freefrag and adopt the old freefrag.
1777 * This action is done by swapping the freefrag dependencies.
1778 * The new dependency gains the old one's freefrag, and the
1779 * old one gets the new one and then immediately puts it on
1780 * the worklist when it is freed by free_allocdirect. It is
1781 * not possible to do this swap when the old dependency had a
1782 * non-zero size but no previous fragment to free. This condition
1783 * arises when the new block is an extension of the old block.
1784 * Here, the first part of the fragment allocated to the new
1785 * dependency is part of the block currently claimed on disk by
1786 * the old dependency, so cannot legitimately be freed until the
1787 * conditions for the new dependency are fulfilled.
1789 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1790 freefrag = newadp->ad_freefrag;
1791 newadp->ad_freefrag = oldadp->ad_freefrag;
1792 oldadp->ad_freefrag = freefrag;
1795 * If we are tracking a new directory-block allocation,
1796 * move it from the old allocdirect to the new allocdirect.
1798 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
1799 newdirblk = WK_NEWDIRBLK(wk);
1800 WORKLIST_REMOVE(&newdirblk->db_list);
1801 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
1802 panic("allocdirect_merge: extra newdirblk");
1803 WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list);
1805 free_allocdirect(adphead, oldadp, 0);
1809 * Allocate a new freefrag structure if needed.
1811 static struct freefrag *
1812 newfreefrag(ip, blkno, size)
1817 struct freefrag *freefrag;
1823 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1824 panic("newfreefrag: frag size");
1825 freefrag = malloc(sizeof(struct freefrag),
1826 M_FREEFRAG, M_SOFTDEP_FLAGS);
1827 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump));
1828 freefrag->ff_inum = ip->i_number;
1829 freefrag->ff_blkno = blkno;
1830 freefrag->ff_fragsize = size;
1835 * This workitem de-allocates fragments that were replaced during
1836 * file block allocation.
1839 handle_workitem_freefrag(freefrag)
1840 struct freefrag *freefrag;
1842 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
1844 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
1845 freefrag->ff_fragsize, freefrag->ff_inum);
1847 WORKITEM_FREE(freefrag, D_FREEFRAG);
1852 * Set up a dependency structure for an external attributes data block.
1853 * This routine follows much of the structure of softdep_setup_allocdirect.
1854 * See the description of softdep_setup_allocdirect above for details.
1857 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1860 ufs2_daddr_t newblkno;
1861 ufs2_daddr_t oldblkno;
1866 struct allocdirect *adp, *oldadp;
1867 struct allocdirectlst *adphead;
1868 struct bmsafemap *bmsafemap;
1869 struct inodedep *inodedep;
1870 struct newblk *newblk;
1873 mp = UFSTOVFS(ip->i_ump);
1874 adp = malloc(sizeof(struct allocdirect),
1875 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1876 workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1878 adp->ad_newblkno = newblkno;
1879 adp->ad_oldblkno = oldblkno;
1880 adp->ad_newsize = newsize;
1881 adp->ad_oldsize = oldsize;
1882 adp->ad_state = ATTACHED | EXTDATA;
1883 LIST_INIT(&adp->ad_newdirblk);
1884 if (newblkno == oldblkno)
1885 adp->ad_freefrag = NULL;
1887 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1890 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1891 panic("softdep_setup_allocext: lost block");
1893 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1894 adp->ad_inodedep = inodedep;
1896 if (newblk->nb_state == DEPCOMPLETE) {
1897 adp->ad_state |= DEPCOMPLETE;
1900 bmsafemap = newblk->nb_bmsafemap;
1901 adp->ad_buf = bmsafemap->sm_buf;
1902 LIST_REMOVE(newblk, nb_deps);
1903 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1905 LIST_REMOVE(newblk, nb_hash);
1906 free(newblk, M_NEWBLK);
1908 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1910 panic("softdep_setup_allocext: lbn %lld > NXADDR",
1913 * The list of allocdirects must be kept in sorted and ascending
1914 * order so that the rollback routines can quickly determine the
1915 * first uncommitted block (the size of the file stored on disk
1916 * ends at the end of the lowest committed fragment, or if there
1917 * are no fragments, at the end of the highest committed block).
1918 * Since files generally grow, the typical case is that the new
1919 * block is to be added at the end of the list. We speed this
1920 * special case by checking against the last allocdirect in the
1921 * list before laboriously traversing the list looking for the
1924 adphead = &inodedep->id_newextupdt;
1925 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1926 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1927 /* insert at end of list */
1928 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1929 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1930 allocdirect_merge(adphead, adp, oldadp);
1934 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1935 if (oldadp->ad_lbn >= lbn)
1939 panic("softdep_setup_allocext: lost entry");
1940 /* insert in middle of list */
1941 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1942 if (oldadp->ad_lbn == lbn)
1943 allocdirect_merge(adphead, adp, oldadp);
1948 * Indirect block allocation dependencies.
1950 * The same dependencies that exist for a direct block also exist when
1951 * a new block is allocated and pointed to by an entry in a block of
1952 * indirect pointers. The undo/redo states described above are also
1953 * used here. Because an indirect block contains many pointers that
1954 * may have dependencies, a second copy of the entire in-memory indirect
1955 * block is kept. The buffer cache copy is always completely up-to-date.
1956 * The second copy, which is used only as a source for disk writes,
1957 * contains only the safe pointers (i.e., those that have no remaining
1958 * update dependencies). The second copy is freed when all pointers
1959 * are safe. The cache is not allowed to replace indirect blocks with
1960 * pending update dependencies. If a buffer containing an indirect
1961 * block with dependencies is written, these routines will mark it
1962 * dirty again. It can only be successfully written once all the
1963 * dependencies are removed. The ffs_fsync routine in conjunction with
1964 * softdep_sync_metadata work together to get all the dependencies
1965 * removed so that a file can be successfully written to disk. Three
1966 * procedures are used when setting up indirect block pointer
1967 * dependencies. The division is necessary because of the organization
1968 * of the "balloc" routine and because of the distinction between file
1969 * pages and file metadata blocks.
1973 * Allocate a new allocindir structure.
1975 static struct allocindir *
1976 newallocindir(ip, ptrno, newblkno, oldblkno)
1977 struct inode *ip; /* inode for file being extended */
1978 int ptrno; /* offset of pointer in indirect block */
1979 ufs2_daddr_t newblkno; /* disk block number being added */
1980 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
1982 struct allocindir *aip;
1984 aip = malloc(sizeof(struct allocindir),
1985 M_ALLOCINDIR, M_SOFTDEP_FLAGS|M_ZERO);
1986 workitem_alloc(&aip->ai_list, D_ALLOCINDIR, UFSTOVFS(ip->i_ump));
1987 aip->ai_state = ATTACHED;
1988 aip->ai_offset = ptrno;
1989 aip->ai_newblkno = newblkno;
1990 aip->ai_oldblkno = oldblkno;
1991 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1996 * Called just before setting an indirect block pointer
1997 * to a newly allocated file page.
2000 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
2001 struct inode *ip; /* inode for file being extended */
2002 ufs_lbn_t lbn; /* allocated block number within file */
2003 struct buf *bp; /* buffer with indirect blk referencing page */
2004 int ptrno; /* offset of pointer in indirect block */
2005 ufs2_daddr_t newblkno; /* disk block number being added */
2006 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */
2007 struct buf *nbp; /* buffer holding allocated page */
2009 struct allocindir *aip;
2010 struct pagedep *pagedep;
2012 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
2013 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
2016 * If we are allocating a directory page, then we must
2017 * allocate an associated pagedep to track additions and
2020 if ((ip->i_mode & IFMT) == IFDIR &&
2021 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
2022 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
2023 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2024 setup_allocindir_phase2(bp, ip, aip);
2029 * Called just before setting an indirect block pointer to a
2030 * newly allocated indirect block.
2033 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
2034 struct buf *nbp; /* newly allocated indirect block */
2035 struct inode *ip; /* inode for file being extended */
2036 struct buf *bp; /* indirect block referencing allocated block */
2037 int ptrno; /* offset of pointer in indirect block */
2038 ufs2_daddr_t newblkno; /* disk block number being added */
2040 struct allocindir *aip;
2042 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
2043 aip = newallocindir(ip, ptrno, newblkno, 0);
2045 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2046 setup_allocindir_phase2(bp, ip, aip);
2051 * Called to finish the allocation of the "aip" allocated
2052 * by one of the two routines above.
2055 setup_allocindir_phase2(bp, ip, aip)
2056 struct buf *bp; /* in-memory copy of the indirect block */
2057 struct inode *ip; /* inode for file being extended */
2058 struct allocindir *aip; /* allocindir allocated by the above routines */
2060 struct worklist *wk;
2061 struct indirdep *indirdep, *newindirdep;
2062 struct bmsafemap *bmsafemap;
2063 struct allocindir *oldaip;
2064 struct freefrag *freefrag;
2065 struct newblk *newblk;
2068 mtx_assert(&lk, MA_OWNED);
2069 if (bp->b_lblkno >= 0)
2070 panic("setup_allocindir_phase2: not indir blk");
2071 for (indirdep = NULL, newindirdep = NULL; ; ) {
2072 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2073 if (wk->wk_type != D_INDIRDEP)
2075 indirdep = WK_INDIRDEP(wk);
2078 if (indirdep == NULL && newindirdep) {
2079 indirdep = newindirdep;
2080 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
2084 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
2086 panic("setup_allocindir: lost block");
2087 if (newblk->nb_state == DEPCOMPLETE) {
2088 aip->ai_state |= DEPCOMPLETE;
2091 bmsafemap = newblk->nb_bmsafemap;
2092 aip->ai_buf = bmsafemap->sm_buf;
2093 LIST_REMOVE(newblk, nb_deps);
2094 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
2097 LIST_REMOVE(newblk, nb_hash);
2098 free(newblk, M_NEWBLK);
2099 aip->ai_indirdep = indirdep;
2101 * Check to see if there is an existing dependency
2102 * for this block. If there is, merge the old
2103 * dependency into the new one.
2105 if (aip->ai_oldblkno == 0)
2109 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
2110 if (oldaip->ai_offset == aip->ai_offset)
2113 if (oldaip != NULL) {
2114 if (oldaip->ai_newblkno != aip->ai_oldblkno)
2115 panic("setup_allocindir_phase2: blkno");
2116 aip->ai_oldblkno = oldaip->ai_oldblkno;
2117 freefrag = aip->ai_freefrag;
2118 aip->ai_freefrag = oldaip->ai_freefrag;
2119 oldaip->ai_freefrag = NULL;
2120 free_allocindir(oldaip, NULL);
2122 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
2123 if (ip->i_ump->um_fstype == UFS1)
2124 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)
2125 [aip->ai_offset] = aip->ai_oldblkno;
2127 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)
2128 [aip->ai_offset] = aip->ai_oldblkno;
2130 if (freefrag != NULL)
2131 handle_workitem_freefrag(freefrag);
2135 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
2136 brelse(newindirdep->ir_savebp);
2138 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
2147 newindirdep = malloc(sizeof(struct indirdep),
2148 M_INDIRDEP, M_SOFTDEP_FLAGS);
2149 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP,
2150 UFSTOVFS(ip->i_ump));
2151 newindirdep->ir_state = ATTACHED;
2152 if (ip->i_ump->um_fstype == UFS1)
2153 newindirdep->ir_state |= UFS1FMT;
2154 LIST_INIT(&newindirdep->ir_deplisthd);
2155 LIST_INIT(&newindirdep->ir_donehd);
2156 if (bp->b_blkno == bp->b_lblkno) {
2157 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
2159 bp->b_blkno = blkno;
2161 newindirdep->ir_savebp =
2162 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
2163 BUF_KERNPROC(newindirdep->ir_savebp);
2164 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
2170 * Block de-allocation dependencies.
2172 * When blocks are de-allocated, the on-disk pointers must be nullified before
2173 * the blocks are made available for use by other files. (The true
2174 * requirement is that old pointers must be nullified before new on-disk
2175 * pointers are set. We chose this slightly more stringent requirement to
2176 * reduce complexity.) Our implementation handles this dependency by updating
2177 * the inode (or indirect block) appropriately but delaying the actual block
2178 * de-allocation (i.e., freemap and free space count manipulation) until
2179 * after the updated versions reach stable storage. After the disk is
2180 * updated, the blocks can be safely de-allocated whenever it is convenient.
2181 * This implementation handles only the common case of reducing a file's
2182 * length to zero. Other cases are handled by the conventional synchronous
2185 * The ffs implementation with which we worked double-checks
2186 * the state of the block pointers and file size as it reduces
2187 * a file's length. Some of this code is replicated here in our
2188 * soft updates implementation. The freeblks->fb_chkcnt field is
2189 * used to transfer a part of this information to the procedure
2190 * that eventually de-allocates the blocks.
2192 * This routine should be called from the routine that shortens
2193 * a file's length, before the inode's size or block pointers
2194 * are modified. It will save the block pointer information for
2195 * later release and zero the inode so that the calling routine
2199 softdep_setup_freeblocks(ip, length, flags)
2200 struct inode *ip; /* The inode whose length is to be reduced */
2201 off_t length; /* The new length for the file */
2202 int flags; /* IO_EXT and/or IO_NORMAL */
2204 struct freeblks *freeblks;
2205 struct inodedep *inodedep;
2206 struct allocdirect *adp;
2211 ufs2_daddr_t extblocks, datablocks;
2213 int i, delay, error;
2216 mp = UFSTOVFS(ip->i_ump);
2218 panic("softdep_setup_freeblocks: non-zero length");
2219 freeblks = malloc(sizeof(struct freeblks),
2220 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
2221 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
2222 freeblks->fb_state = ATTACHED;
2223 freeblks->fb_uid = ip->i_uid;
2224 freeblks->fb_previousinum = ip->i_number;
2225 freeblks->fb_devvp = ip->i_devvp;
2227 if (fs->fs_magic == FS_UFS2_MAGIC)
2228 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
2229 datablocks = DIP(ip, i_blocks) - extblocks;
2230 if ((flags & IO_NORMAL) == 0) {
2231 freeblks->fb_oldsize = 0;
2232 freeblks->fb_chkcnt = 0;
2234 freeblks->fb_oldsize = ip->i_size;
2236 DIP_SET(ip, i_size, 0);
2237 freeblks->fb_chkcnt = datablocks;
2238 for (i = 0; i < NDADDR; i++) {
2239 freeblks->fb_dblks[i] = DIP(ip, i_db[i]);
2240 DIP_SET(ip, i_db[i], 0);
2242 for (i = 0; i < NIADDR; i++) {
2243 freeblks->fb_iblks[i] = DIP(ip, i_ib[i]);
2244 DIP_SET(ip, i_ib[i], 0);
2247 * If the file was removed, then the space being freed was
2248 * accounted for then (see softdep_releasefile()). If the
2249 * file is merely being truncated, then we account for it now.
2251 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2252 UFS_LOCK(ip->i_ump);
2253 fs->fs_pendingblocks += datablocks;
2254 UFS_UNLOCK(ip->i_ump);
2257 if ((flags & IO_EXT) == 0) {
2258 freeblks->fb_oldextsize = 0;
2260 freeblks->fb_oldextsize = ip->i_din2->di_extsize;
2261 ip->i_din2->di_extsize = 0;
2262 freeblks->fb_chkcnt += extblocks;
2263 for (i = 0; i < NXADDR; i++) {
2264 freeblks->fb_eblks[i] = ip->i_din2->di_extb[i];
2265 ip->i_din2->di_extb[i] = 0;
2268 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt);
2270 * Push the zero'ed inode to to its disk buffer so that we are free
2271 * to delete its dependencies below. Once the dependencies are gone
2272 * the buffer can be safely released.
2274 if ((error = bread(ip->i_devvp,
2275 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
2276 (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
2278 softdep_error("softdep_setup_freeblocks", error);
2280 if (ip->i_ump->um_fstype == UFS1)
2281 *((struct ufs1_dinode *)bp->b_data +
2282 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
2284 *((struct ufs2_dinode *)bp->b_data +
2285 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
2287 * Find and eliminate any inode dependencies.
2290 (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
2291 if ((inodedep->id_state & IOSTARTED) != 0)
2292 panic("softdep_setup_freeblocks: inode busy");
2294 * Add the freeblks structure to the list of operations that
2295 * must await the zero'ed inode being written to disk. If we
2296 * still have a bitmap dependency (delay == 0), then the inode
2297 * has never been written to disk, so we can process the
2298 * freeblks below once we have deleted the dependencies.
2300 delay = (inodedep->id_state & DEPCOMPLETE);
2302 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
2304 * Because the file length has been truncated to zero, any
2305 * pending block allocation dependency structures associated
2306 * with this inode are obsolete and can simply be de-allocated.
2307 * We must first merge the two dependency lists to get rid of
2308 * any duplicate freefrag structures, then purge the merged list.
2309 * If we still have a bitmap dependency, then the inode has never
2310 * been written to disk, so we can free any fragments without delay.
2312 if (flags & IO_NORMAL) {
2313 merge_inode_lists(&inodedep->id_newinoupdt,
2314 &inodedep->id_inoupdt);
2315 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
2316 free_allocdirect(&inodedep->id_inoupdt, adp, delay);
2318 if (flags & IO_EXT) {
2319 merge_inode_lists(&inodedep->id_newextupdt,
2320 &inodedep->id_extupdt);
2321 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
2322 free_allocdirect(&inodedep->id_extupdt, adp, delay);
2327 * We must wait for any I/O in progress to finish so that
2328 * all potential buffers on the dirty list will be visible.
2329 * Once they are all there, walk the list and get rid of
2337 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
2338 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
2339 ((flags & IO_NORMAL) == 0 &&
2340 (bp->b_xflags & BX_ALTDATA) == 0))
2342 if ((bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT)) == NULL)
2346 (void) inodedep_lookup(mp, ip->i_number, 0, &inodedep);
2347 deallocate_dependencies(bp, inodedep);
2349 bp->b_flags |= B_INVAL | B_NOCACHE;
2356 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
2357 (void) free_inodedep(inodedep);
2360 freeblks->fb_state |= DEPCOMPLETE;
2362 * If the inode with zeroed block pointers is now on disk
2363 * we can start freeing blocks. Add freeblks to the worklist
2364 * instead of calling handle_workitem_freeblocks directly as
2365 * it is more likely that additional IO is needed to complete
2366 * the request here than in the !delay case.
2368 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
2369 add_to_worklist(&freeblks->fb_list);
2374 * If the inode has never been written to disk (delay == 0),
2375 * then we can process the freeblks now that we have deleted
2379 handle_workitem_freeblocks(freeblks, 0);
2383 * Reclaim any dependency structures from a buffer that is about to
2384 * be reallocated to a new vnode. The buffer must be locked, thus,
2385 * no I/O completion operations can occur while we are manipulating
2386 * its associated dependencies. The mutex is held so that other I/O's
2387 * associated with related dependencies do not occur.
2390 deallocate_dependencies(bp, inodedep)
2392 struct inodedep *inodedep;
2394 struct worklist *wk;
2395 struct indirdep *indirdep;
2396 struct allocindir *aip;
2397 struct pagedep *pagedep;
2398 struct dirrem *dirrem;
2402 mtx_assert(&lk, MA_OWNED);
2403 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2404 switch (wk->wk_type) {
2407 indirdep = WK_INDIRDEP(wk);
2409 * None of the indirect pointers will ever be visible,
2410 * so they can simply be tossed. GOINGAWAY ensures
2411 * that allocated pointers will be saved in the buffer
2412 * cache until they are freed. Note that they will
2413 * only be able to be found by their physical address
2414 * since the inode mapping the logical address will
2415 * be gone. The save buffer used for the safe copy
2416 * was allocated in setup_allocindir_phase2 using
2417 * the physical address so it could be used for this
2418 * purpose. Hence we swap the safe copy with the real
2419 * copy, allowing the safe copy to be freed and holding
2420 * on to the real copy for later use in indir_trunc.
2422 if (indirdep->ir_state & GOINGAWAY)
2423 panic("deallocate_dependencies: already gone");
2424 indirdep->ir_state |= GOINGAWAY;
2425 VFSTOUFS(bp->b_vp->v_mount)->um_numindirdeps += 1;
2426 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
2427 free_allocindir(aip, inodedep);
2428 if (bp->b_lblkno >= 0 ||
2429 bp->b_blkno != indirdep->ir_savebp->b_lblkno)
2430 panic("deallocate_dependencies: not indir");
2431 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
2433 WORKLIST_REMOVE(wk);
2434 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
2438 pagedep = WK_PAGEDEP(wk);
2440 * None of the directory additions will ever be
2441 * visible, so they can simply be tossed.
2443 for (i = 0; i < DAHASHSZ; i++)
2445 LIST_FIRST(&pagedep->pd_diraddhd[i])))
2447 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
2450 * Copy any directory remove dependencies to the list
2451 * to be processed after the zero'ed inode is written.
2452 * If the inode has already been written, then they
2453 * can be dumped directly onto the work list.
2455 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
2456 LIST_REMOVE(dirrem, dm_next);
2457 dirrem->dm_dirinum = pagedep->pd_ino;
2458 if (inodedep == NULL ||
2459 (inodedep->id_state & ALLCOMPLETE) ==
2461 add_to_worklist(&dirrem->dm_list);
2463 WORKLIST_INSERT(&inodedep->id_bufwait,
2466 if ((pagedep->pd_state & NEWBLOCK) != 0) {
2467 LIST_FOREACH(wk, &inodedep->id_bufwait, wk_list)
2468 if (wk->wk_type == D_NEWDIRBLK &&
2469 WK_NEWDIRBLK(wk)->db_pagedep ==
2473 WORKLIST_REMOVE(wk);
2474 free_newdirblk(WK_NEWDIRBLK(wk));
2476 panic("deallocate_dependencies: "
2479 WORKLIST_REMOVE(&pagedep->pd_list);
2480 LIST_REMOVE(pagedep, pd_hash);
2481 WORKITEM_FREE(pagedep, D_PAGEDEP);
2485 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
2490 panic("deallocate_dependencies: Unexpected type %s",
2491 TYPENAME(wk->wk_type));
2495 panic("deallocate_dependencies: Unknown type %s",
2496 TYPENAME(wk->wk_type));
2503 * Free an allocdirect. Generate a new freefrag work request if appropriate.
2504 * This routine must be called with splbio interrupts blocked.
2507 free_allocdirect(adphead, adp, delay)
2508 struct allocdirectlst *adphead;
2509 struct allocdirect *adp;
2512 struct newdirblk *newdirblk;
2513 struct worklist *wk;
2515 mtx_assert(&lk, MA_OWNED);
2516 if ((adp->ad_state & DEPCOMPLETE) == 0)
2517 LIST_REMOVE(adp, ad_deps);
2518 TAILQ_REMOVE(adphead, adp, ad_next);
2519 if ((adp->ad_state & COMPLETE) == 0)
2520 WORKLIST_REMOVE(&adp->ad_list);
2521 if (adp->ad_freefrag != NULL) {
2523 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2524 &adp->ad_freefrag->ff_list);
2526 add_to_worklist(&adp->ad_freefrag->ff_list);
2528 if ((wk = LIST_FIRST(&adp->ad_newdirblk)) != NULL) {
2529 newdirblk = WK_NEWDIRBLK(wk);
2530 WORKLIST_REMOVE(&newdirblk->db_list);
2531 if (!LIST_EMPTY(&adp->ad_newdirblk))
2532 panic("free_allocdirect: extra newdirblk");
2534 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2535 &newdirblk->db_list);
2537 free_newdirblk(newdirblk);
2539 WORKITEM_FREE(adp, D_ALLOCDIRECT);
2543 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
2544 * This routine must be called with splbio interrupts blocked.
2547 free_newdirblk(newdirblk)
2548 struct newdirblk *newdirblk;
2550 struct pagedep *pagedep;
2554 mtx_assert(&lk, MA_OWNED);
2556 * If the pagedep is still linked onto the directory buffer
2557 * dependency chain, then some of the entries on the
2558 * pd_pendinghd list may not be committed to disk yet. In
2559 * this case, we will simply clear the NEWBLOCK flag and
2560 * let the pd_pendinghd list be processed when the pagedep
2561 * is next written. If the pagedep is no longer on the buffer
2562 * dependency chain, then all the entries on the pd_pending
2563 * list are committed to disk and we can free them here.
2565 pagedep = newdirblk->db_pagedep;
2566 pagedep->pd_state &= ~NEWBLOCK;
2567 if ((pagedep->pd_state & ONWORKLIST) == 0)
2568 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
2571 * If no dependencies remain, the pagedep will be freed.
2573 for (i = 0; i < DAHASHSZ; i++)
2574 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
2576 if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0) {
2577 LIST_REMOVE(pagedep, pd_hash);
2578 WORKITEM_FREE(pagedep, D_PAGEDEP);
2580 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2584 * Prepare an inode to be freed. The actual free operation is not
2585 * done until the zero'ed inode has been written to disk.
2588 softdep_freefile(pvp, ino, mode)
2593 struct inode *ip = VTOI(pvp);
2594 struct inodedep *inodedep;
2595 struct freefile *freefile;
2598 * This sets up the inode de-allocation dependency.
2600 freefile = malloc(sizeof(struct freefile),
2601 M_FREEFILE, M_SOFTDEP_FLAGS);
2602 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
2603 freefile->fx_mode = mode;
2604 freefile->fx_oldinum = ino;
2605 freefile->fx_devvp = ip->i_devvp;
2606 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2607 UFS_LOCK(ip->i_ump);
2608 ip->i_fs->fs_pendinginodes += 1;
2609 UFS_UNLOCK(ip->i_ump);
2613 * If the inodedep does not exist, then the zero'ed inode has
2614 * been written to disk. If the allocated inode has never been
2615 * written to disk, then the on-disk inode is zero'ed. In either
2616 * case we can free the file immediately.
2619 if (inodedep_lookup(pvp->v_mount, ino, 0, &inodedep) == 0 ||
2620 check_inode_unwritten(inodedep)) {
2622 handle_workitem_freefile(freefile);
2625 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2627 if (ip->i_number == ino)
2628 ip->i_flag |= IN_MODIFIED;
2632 * Check to see if an inode has never been written to disk. If
2633 * so free the inodedep and return success, otherwise return failure.
2634 * This routine must be called with splbio interrupts blocked.
2636 * If we still have a bitmap dependency, then the inode has never
2637 * been written to disk. Drop the dependency as it is no longer
2638 * necessary since the inode is being deallocated. We set the
2639 * ALLCOMPLETE flags since the bitmap now properly shows that the
2640 * inode is not allocated. Even if the inode is actively being
2641 * written, it has been rolled back to its zero'ed state, so we
2642 * are ensured that a zero inode is what is on the disk. For short
2643 * lived files, this change will usually result in removing all the
2644 * dependencies from the inode so that it can be freed immediately.
2647 check_inode_unwritten(inodedep)
2648 struct inodedep *inodedep;
2651 mtx_assert(&lk, MA_OWNED);
2652 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2653 !LIST_EMPTY(&inodedep->id_pendinghd) ||
2654 !LIST_EMPTY(&inodedep->id_bufwait) ||
2655 !LIST_EMPTY(&inodedep->id_inowait) ||
2656 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2657 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2658 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2659 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2660 inodedep->id_nlinkdelta != 0)
2664 * Another process might be in initiate_write_inodeblock_ufs[12]
2665 * trying to allocate memory without holding "Softdep Lock".
2667 if ((inodedep->id_state & IOSTARTED) != 0 &&
2668 inodedep->id_savedino1 == NULL)
2671 inodedep->id_state |= ALLCOMPLETE;
2672 LIST_REMOVE(inodedep, id_deps);
2673 inodedep->id_buf = NULL;
2674 if (inodedep->id_state & ONWORKLIST)
2675 WORKLIST_REMOVE(&inodedep->id_list);
2676 if (inodedep->id_savedino1 != NULL) {
2677 free(inodedep->id_savedino1, M_SAVEDINO);
2678 inodedep->id_savedino1 = NULL;
2680 if (free_inodedep(inodedep) == 0)
2681 panic("check_inode_unwritten: busy inode");
2686 * Try to free an inodedep structure. Return 1 if it could be freed.
2689 free_inodedep(inodedep)
2690 struct inodedep *inodedep;
2693 mtx_assert(&lk, MA_OWNED);
2694 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2695 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2696 !LIST_EMPTY(&inodedep->id_pendinghd) ||
2697 !LIST_EMPTY(&inodedep->id_bufwait) ||
2698 !LIST_EMPTY(&inodedep->id_inowait) ||
2699 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2700 !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2701 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2702 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2703 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino1 != NULL)
2705 LIST_REMOVE(inodedep, id_hash);
2706 WORKITEM_FREE(inodedep, D_INODEDEP);
2712 * This workitem routine performs the block de-allocation.
2713 * The workitem is added to the pending list after the updated
2714 * inode block has been written to disk. As mentioned above,
2715 * checks regarding the number of blocks de-allocated (compared
2716 * to the number of blocks allocated for the file) are also
2717 * performed in this function.
2720 handle_workitem_freeblocks(freeblks, flags)
2721 struct freeblks *freeblks;
2727 struct ufsmount *ump;
2728 int i, nblocks, level, bsize;
2729 ufs2_daddr_t bn, blocksreleased = 0;
2730 int error, allerror = 0;
2731 ufs_lbn_t baselbns[NIADDR], tmpval;
2732 int fs_pendingblocks;
2734 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2736 fs_pendingblocks = 0;
2738 baselbns[0] = NDADDR;
2739 for (i = 1; i < NIADDR; i++) {
2740 tmpval *= NINDIR(fs);
2741 baselbns[i] = baselbns[i - 1] + tmpval;
2743 nblocks = btodb(fs->fs_bsize);
2746 * Release all extended attribute blocks or frags.
2748 if (freeblks->fb_oldextsize > 0) {
2749 for (i = (NXADDR - 1); i >= 0; i--) {
2750 if ((bn = freeblks->fb_eblks[i]) == 0)
2752 bsize = sblksize(fs, freeblks->fb_oldextsize, i);
2753 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2754 freeblks->fb_previousinum);
2755 blocksreleased += btodb(bsize);
2759 * Release all data blocks or frags.
2761 if (freeblks->fb_oldsize > 0) {
2763 * Indirect blocks first.
2765 for (level = (NIADDR - 1); level >= 0; level--) {
2766 if ((bn = freeblks->fb_iblks[level]) == 0)
2768 if ((error = indir_trunc(freeblks, fsbtodb(fs, bn),
2769 level, baselbns[level], &blocksreleased)) != 0)
2771 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn,
2772 fs->fs_bsize, freeblks->fb_previousinum);
2773 fs_pendingblocks += nblocks;
2774 blocksreleased += nblocks;
2777 * All direct blocks or frags.
2779 for (i = (NDADDR - 1); i >= 0; i--) {
2780 if ((bn = freeblks->fb_dblks[i]) == 0)
2782 bsize = sblksize(fs, freeblks->fb_oldsize, i);
2783 ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2784 freeblks->fb_previousinum);
2785 fs_pendingblocks += btodb(bsize);
2786 blocksreleased += btodb(bsize);
2790 fs->fs_pendingblocks -= fs_pendingblocks;
2793 * If we still have not finished background cleanup, then check
2794 * to see if the block count needs to be adjusted.
2796 if (freeblks->fb_chkcnt != blocksreleased &&
2797 (fs->fs_flags & FS_UNCLEAN) != 0 &&
2798 ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_previousinum,
2799 (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ)
2802 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + \
2803 freeblks->fb_chkcnt - blocksreleased);
2804 ip->i_flag |= IN_CHANGE;
2809 if (freeblks->fb_chkcnt != blocksreleased &&
2810 ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0))
2811 printf("handle_workitem_freeblocks: block count\n");
2813 softdep_error("handle_workitem_freeblks", allerror);
2814 #endif /* INVARIANTS */
2817 WORKITEM_FREE(freeblks, D_FREEBLKS);
2822 * Release blocks associated with the inode ip and stored in the indirect
2823 * block dbn. If level is greater than SINGLE, the block is an indirect block
2824 * and recursive calls to indirtrunc must be used to cleanse other indirect
2828 indir_trunc(freeblks, dbn, level, lbn, countp)
2829 struct freeblks *freeblks;
2833 ufs2_daddr_t *countp;
2837 struct worklist *wk;
2838 struct indirdep *indirdep;
2839 struct ufsmount *ump;
2840 ufs1_daddr_t *bap1 = 0;
2841 ufs2_daddr_t nb, *bap2 = 0;
2843 int i, nblocks, ufs1fmt;
2844 int error, allerror = 0;
2845 int fs_pendingblocks;
2847 ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2849 fs_pendingblocks = 0;
2851 for (i = level; i > 0; i--)
2852 lbnadd *= NINDIR(fs);
2854 * Get buffer of block pointers to be freed. This routine is not
2855 * called until the zero'ed inode has been written, so it is safe
2856 * to free blocks as they are encountered. Because the inode has
2857 * been zero'ed, calls to bmap on these blocks will fail. So, we
2858 * have to use the on-disk address and the block device for the
2859 * filesystem to look them up. If the file was deleted before its
2860 * indirect blocks were all written to disk, the routine that set
2861 * us up (deallocate_dependencies) will have arranged to leave
2862 * a complete copy of the indirect block in memory for our use.
2863 * Otherwise we have to read the blocks in from the disk.
2866 bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0,
2869 bp = incore(&freeblks->fb_devvp->v_bufobj, dbn);
2872 if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2873 if (wk->wk_type != D_INDIRDEP ||
2874 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2875 (indirdep->ir_state & GOINGAWAY) == 0)
2876 panic("indir_trunc: lost indirdep");
2877 WORKLIST_REMOVE(wk);
2878 WORKITEM_FREE(indirdep, D_INDIRDEP);
2879 if (!LIST_EMPTY(&bp->b_dep))
2880 panic("indir_trunc: dangling dep");
2881 ump->um_numindirdeps -= 1;
2889 error = bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
2897 * Recursively free indirect blocks.
2899 if (ump->um_fstype == UFS1) {
2901 bap1 = (ufs1_daddr_t *)bp->b_data;
2904 bap2 = (ufs2_daddr_t *)bp->b_data;
2906 nblocks = btodb(fs->fs_bsize);
2907 for (i = NINDIR(fs) - 1; i >= 0; i--) {
2915 if ((error = indir_trunc(freeblks, fsbtodb(fs, nb),
2916 level - 1, lbn + (i * lbnadd), countp)) != 0)
2919 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, fs->fs_bsize,
2920 freeblks->fb_previousinum);
2921 fs_pendingblocks += nblocks;
2925 fs->fs_pendingblocks -= fs_pendingblocks;
2927 bp->b_flags |= B_INVAL | B_NOCACHE;
2933 * Free an allocindir.
2934 * This routine must be called with splbio interrupts blocked.
2937 free_allocindir(aip, inodedep)
2938 struct allocindir *aip;
2939 struct inodedep *inodedep;
2941 struct freefrag *freefrag;
2943 mtx_assert(&lk, MA_OWNED);
2944 if ((aip->ai_state & DEPCOMPLETE) == 0)
2945 LIST_REMOVE(aip, ai_deps);
2946 if (aip->ai_state & ONWORKLIST)
2947 WORKLIST_REMOVE(&aip->ai_list);
2948 LIST_REMOVE(aip, ai_next);
2949 if ((freefrag = aip->ai_freefrag) != NULL) {
2950 if (inodedep == NULL)
2951 add_to_worklist(&freefrag->ff_list);
2953 WORKLIST_INSERT(&inodedep->id_bufwait,
2954 &freefrag->ff_list);
2956 WORKITEM_FREE(aip, D_ALLOCINDIR);
2960 * Directory entry addition dependencies.
2962 * When adding a new directory entry, the inode (with its incremented link
2963 * count) must be written to disk before the directory entry's pointer to it.
2964 * Also, if the inode is newly allocated, the corresponding freemap must be
2965 * updated (on disk) before the directory entry's pointer. These requirements
2966 * are met via undo/redo on the directory entry's pointer, which consists
2967 * simply of the inode number.
2969 * As directory entries are added and deleted, the free space within a
2970 * directory block can become fragmented. The ufs filesystem will compact
2971 * a fragmented directory block to make space for a new entry. When this
2972 * occurs, the offsets of previously added entries change. Any "diradd"
2973 * dependency structures corresponding to these entries must be updated with
2978 * This routine is called after the in-memory inode's link
2979 * count has been incremented, but before the directory entry's
2980 * pointer to the inode has been set.
2983 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
2984 struct buf *bp; /* buffer containing directory block */
2985 struct inode *dp; /* inode for directory */
2986 off_t diroffset; /* offset of new entry in directory */
2987 ino_t newinum; /* inode referenced by new directory entry */
2988 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
2989 int isnewblk; /* entry is in a newly allocated block */
2991 int offset; /* offset of new entry within directory block */
2992 ufs_lbn_t lbn; /* block in directory containing new entry */
2995 struct allocdirect *adp;
2996 struct pagedep *pagedep;
2997 struct inodedep *inodedep;
2998 struct newdirblk *newdirblk = 0;
2999 struct mkdir *mkdir1, *mkdir2;
3003 * Whiteouts have no dependencies.
3005 if (newinum == WINO) {
3006 if (newdirbp != NULL)
3010 mp = UFSTOVFS(dp->i_ump);
3012 lbn = lblkno(fs, diroffset);
3013 offset = blkoff(fs, diroffset);
3014 dap = malloc(sizeof(struct diradd), M_DIRADD,
3015 M_SOFTDEP_FLAGS|M_ZERO);
3016 workitem_alloc(&dap->da_list, D_DIRADD, mp);
3017 dap->da_offset = offset;
3018 dap->da_newinum = newinum;
3019 dap->da_state = ATTACHED;
3020 if (isnewblk && lbn < NDADDR && fragoff(fs, diroffset) == 0) {
3021 newdirblk = malloc(sizeof(struct newdirblk),
3022 M_NEWDIRBLK, M_SOFTDEP_FLAGS);
3023 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
3025 if (newdirbp == NULL) {
3026 dap->da_state |= DEPCOMPLETE;
3029 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
3030 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR,
3032 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
3033 mkdir1->md_state = MKDIR_BODY;
3034 mkdir1->md_diradd = dap;
3035 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR,
3037 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
3038 mkdir2->md_state = MKDIR_PARENT;
3039 mkdir2->md_diradd = dap;
3041 * Dependency on "." and ".." being written to disk.
3043 mkdir1->md_buf = newdirbp;
3045 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
3046 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
3050 * Dependency on link count increase for parent directory
3053 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0
3054 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3055 dap->da_state &= ~MKDIR_PARENT;
3056 WORKITEM_FREE(mkdir2, D_MKDIR);
3058 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
3059 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
3063 * Link into parent directory pagedep to await its being written.
3065 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3066 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3067 dap->da_pagedep = pagedep;
3068 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
3071 * Link into its inodedep. Put it on the id_bufwait list if the inode
3072 * is not yet written. If it is written, do the post-inode write
3073 * processing to put it on the id_pendinghd list.
3075 (void) inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
3076 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
3077 diradd_inode_written(dap, inodedep);
3079 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3082 * Directories growing into indirect blocks are rare
3083 * enough and the frequency of new block allocation
3084 * in those cases even more rare, that we choose not
3085 * to bother tracking them. Rather we simply force the
3086 * new directory entry to disk.
3088 if (lbn >= NDADDR) {
3091 * We only have a new allocation when at the
3092 * beginning of a new block, not when we are
3093 * expanding into an existing block.
3095 if (blkoff(fs, diroffset) == 0)
3100 * We only have a new allocation when at the beginning
3101 * of a new fragment, not when we are expanding into an
3102 * existing fragment. Also, there is nothing to do if we
3103 * are already tracking this block.
3105 if (fragoff(fs, diroffset) != 0) {
3109 if ((pagedep->pd_state & NEWBLOCK) != 0) {
3110 WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
3115 * Find our associated allocdirect and have it track us.
3117 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0)
3118 panic("softdep_setup_directory_add: lost inodedep");
3119 adp = TAILQ_LAST(&inodedep->id_newinoupdt, allocdirectlst);
3120 if (adp == NULL || adp->ad_lbn != lbn)
3121 panic("softdep_setup_directory_add: lost entry");
3122 pagedep->pd_state |= NEWBLOCK;
3123 newdirblk->db_pagedep = pagedep;
3124 WORKLIST_INSERT(&adp->ad_newdirblk, &newdirblk->db_list);
3131 * This procedure is called to change the offset of a directory
3132 * entry when compacting a directory block which must be owned
3133 * exclusively by the caller. Note that the actual entry movement
3134 * must be done in this procedure to ensure that no I/O completions
3135 * occur while the move is in progress.
3138 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
3139 struct inode *dp; /* inode for directory */
3140 caddr_t base; /* address of dp->i_offset */
3141 caddr_t oldloc; /* address of old directory location */
3142 caddr_t newloc; /* address of new directory location */
3143 int entrysize; /* size of directory entry */
3145 int offset, oldoffset, newoffset;
3146 struct pagedep *pagedep;
3151 lbn = lblkno(dp->i_fs, dp->i_offset);
3152 offset = blkoff(dp->i_fs, dp->i_offset);
3153 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
3155 oldoffset = offset + (oldloc - base);
3156 newoffset = offset + (newloc - base);
3158 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
3159 if (dap->da_offset != oldoffset)
3161 dap->da_offset = newoffset;
3162 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
3164 LIST_REMOVE(dap, da_pdlist);
3165 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
3171 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
3172 if (dap->da_offset == oldoffset) {
3173 dap->da_offset = newoffset;
3179 bcopy(oldloc, newloc, entrysize);
3184 * Free a diradd dependency structure. This routine must be called
3185 * with splbio interrupts blocked.
3191 struct dirrem *dirrem;
3192 struct pagedep *pagedep;
3193 struct inodedep *inodedep;
3194 struct mkdir *mkdir, *nextmd;
3196 mtx_assert(&lk, MA_OWNED);
3197 WORKLIST_REMOVE(&dap->da_list);
3198 LIST_REMOVE(dap, da_pdlist);
3199 if ((dap->da_state & DIRCHG) == 0) {
3200 pagedep = dap->da_pagedep;
3202 dirrem = dap->da_previous;
3203 pagedep = dirrem->dm_pagedep;
3204 dirrem->dm_dirinum = pagedep->pd_ino;
3205 add_to_worklist(&dirrem->dm_list);
3207 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
3209 (void) free_inodedep(inodedep);
3210 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
3211 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
3212 nextmd = LIST_NEXT(mkdir, md_mkdirs);
3213 if (mkdir->md_diradd != dap)
3215 dap->da_state &= ~mkdir->md_state;
3216 WORKLIST_REMOVE(&mkdir->md_list);
3217 LIST_REMOVE(mkdir, md_mkdirs);
3218 WORKITEM_FREE(mkdir, D_MKDIR);
3220 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
3221 panic("free_diradd: unfound ref");
3223 WORKITEM_FREE(dap, D_DIRADD);
3227 * Directory entry removal dependencies.
3229 * When removing a directory entry, the entry's inode pointer must be
3230 * zero'ed on disk before the corresponding inode's link count is decremented
3231 * (possibly freeing the inode for re-use). This dependency is handled by
3232 * updating the directory entry but delaying the inode count reduction until
3233 * after the directory block has been written to disk. After this point, the
3234 * inode count can be decremented whenever it is convenient.
3238 * This routine should be called immediately after removing
3239 * a directory entry. The inode's link count should not be
3240 * decremented by the calling procedure -- the soft updates
3241 * code will do this task when it is safe.
3244 softdep_setup_remove(bp, dp, ip, isrmdir)
3245 struct buf *bp; /* buffer containing directory block */
3246 struct inode *dp; /* inode for the directory being modified */
3247 struct inode *ip; /* inode for directory entry being removed */
3248 int isrmdir; /* indicates if doing RMDIR */
3250 struct dirrem *dirrem, *prevdirrem;
3253 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
3255 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3258 * If the COMPLETE flag is clear, then there were no active
3259 * entries and we want to roll back to a zeroed entry until
3260 * the new inode is committed to disk. If the COMPLETE flag is
3261 * set then we have deleted an entry that never made it to
3262 * disk. If the entry we deleted resulted from a name change,
3263 * then the old name still resides on disk. We cannot delete
3264 * its inode (returned to us in prevdirrem) until the zeroed
3265 * directory entry gets to disk. The new inode has never been
3266 * referenced on the disk, so can be deleted immediately.
3268 if ((dirrem->dm_state & COMPLETE) == 0) {
3269 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
3273 if (prevdirrem != NULL)
3274 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
3275 prevdirrem, dm_next);
3276 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
3278 handle_workitem_remove(dirrem, NULL);
3283 * Allocate a new dirrem if appropriate and return it along with
3284 * its associated pagedep. Called without a lock, returns with lock.
3286 static long num_dirrem; /* number of dirrem allocated */
3287 static struct dirrem *
3288 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
3289 struct buf *bp; /* buffer containing directory block */
3290 struct inode *dp; /* inode for the directory being modified */
3291 struct inode *ip; /* inode for directory entry being removed */
3292 int isrmdir; /* indicates if doing RMDIR */
3293 struct dirrem **prevdirremp; /* previously referenced inode, if any */
3298 struct dirrem *dirrem;
3299 struct pagedep *pagedep;
3302 * Whiteouts have no deletion dependencies.
3305 panic("newdirrem: whiteout");
3307 * If we are over our limit, try to improve the situation.
3308 * Limiting the number of dirrem structures will also limit
3309 * the number of freefile and freeblks structures.
3312 if (!(ip->i_flags & SF_SNAPSHOT) && num_dirrem > max_softdeps / 2)
3313 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_REMOVE);
3316 dirrem = malloc(sizeof(struct dirrem),
3317 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO);
3318 workitem_alloc(&dirrem->dm_list, D_DIRREM, ITOV(dp)->v_mount);
3319 dirrem->dm_state = isrmdir ? RMDIR : 0;
3320 dirrem->dm_oldinum = ip->i_number;
3321 *prevdirremp = NULL;
3324 lbn = lblkno(dp->i_fs, dp->i_offset);
3325 offset = blkoff(dp->i_fs, dp->i_offset);
3326 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3327 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3328 dirrem->dm_pagedep = pagedep;
3330 * Check for a diradd dependency for the same directory entry.
3331 * If present, then both dependencies become obsolete and can
3332 * be de-allocated. Check for an entry on both the pd_dirraddhd
3333 * list and the pd_pendinghd list.
3336 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
3337 if (dap->da_offset == offset)
3341 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
3342 if (dap->da_offset == offset)
3348 * Must be ATTACHED at this point.
3350 if ((dap->da_state & ATTACHED) == 0)
3351 panic("newdirrem: not ATTACHED");
3352 if (dap->da_newinum != ip->i_number)
3353 panic("newdirrem: inum %d should be %d",
3354 ip->i_number, dap->da_newinum);
3356 * If we are deleting a changed name that never made it to disk,
3357 * then return the dirrem describing the previous inode (which
3358 * represents the inode currently referenced from this entry on disk).
3360 if ((dap->da_state & DIRCHG) != 0) {
3361 *prevdirremp = dap->da_previous;
3362 dap->da_state &= ~DIRCHG;
3363 dap->da_pagedep = pagedep;
3366 * We are deleting an entry that never made it to disk.
3367 * Mark it COMPLETE so we can delete its inode immediately.
3369 dirrem->dm_state |= COMPLETE;
3375 * Directory entry change dependencies.
3377 * Changing an existing directory entry requires that an add operation
3378 * be completed first followed by a deletion. The semantics for the addition
3379 * are identical to the description of adding a new entry above except
3380 * that the rollback is to the old inode number rather than zero. Once
3381 * the addition dependency is completed, the removal is done as described
3382 * in the removal routine above.
3386 * This routine should be called immediately after changing
3387 * a directory entry. The inode's link count should not be
3388 * decremented by the calling procedure -- the soft updates
3389 * code will perform this task when it is safe.
3392 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
3393 struct buf *bp; /* buffer containing directory block */
3394 struct inode *dp; /* inode for the directory being modified */
3395 struct inode *ip; /* inode for directory entry being removed */
3396 ino_t newinum; /* new inode number for changed entry */
3397 int isrmdir; /* indicates if doing RMDIR */
3400 struct diradd *dap = NULL;
3401 struct dirrem *dirrem, *prevdirrem;
3402 struct pagedep *pagedep;
3403 struct inodedep *inodedep;
3406 offset = blkoff(dp->i_fs, dp->i_offset);
3407 mp = UFSTOVFS(dp->i_ump);
3410 * Whiteouts do not need diradd dependencies.
3412 if (newinum != WINO) {
3413 dap = malloc(sizeof(struct diradd),
3414 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
3415 workitem_alloc(&dap->da_list, D_DIRADD, mp);
3416 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
3417 dap->da_offset = offset;
3418 dap->da_newinum = newinum;
3422 * Allocate a new dirrem and ACQUIRE_LOCK.
3424 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3425 pagedep = dirrem->dm_pagedep;
3427 * The possible values for isrmdir:
3428 * 0 - non-directory file rename
3429 * 1 - directory rename within same directory
3430 * inum - directory rename to new directory of given inode number
3431 * When renaming to a new directory, we are both deleting and
3432 * creating a new directory entry, so the link count on the new
3433 * directory should not change. Thus we do not need the followup
3434 * dirrem which is usually done in handle_workitem_remove. We set
3435 * the DIRCHG flag to tell handle_workitem_remove to skip the
3439 dirrem->dm_state |= DIRCHG;
3442 * Whiteouts have no additional dependencies,
3443 * so just put the dirrem on the correct list.
3445 if (newinum == WINO) {
3446 if ((dirrem->dm_state & COMPLETE) == 0) {
3447 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
3450 dirrem->dm_dirinum = pagedep->pd_ino;
3451 add_to_worklist(&dirrem->dm_list);
3458 * If the COMPLETE flag is clear, then there were no active
3459 * entries and we want to roll back to the previous inode until
3460 * the new inode is committed to disk. If the COMPLETE flag is
3461 * set, then we have deleted an entry that never made it to disk.
3462 * If the entry we deleted resulted from a name change, then the old
3463 * inode reference still resides on disk. Any rollback that we do
3464 * needs to be to that old inode (returned to us in prevdirrem). If
3465 * the entry we deleted resulted from a create, then there is
3466 * no entry on the disk, so we want to roll back to zero rather
3467 * than the uncommitted inode. In either of the COMPLETE cases we
3468 * want to immediately free the unwritten and unreferenced inode.
3470 if ((dirrem->dm_state & COMPLETE) == 0) {
3471 dap->da_previous = dirrem;
3473 if (prevdirrem != NULL) {
3474 dap->da_previous = prevdirrem;
3476 dap->da_state &= ~DIRCHG;
3477 dap->da_pagedep = pagedep;
3479 dirrem->dm_dirinum = pagedep->pd_ino;
3480 add_to_worklist(&dirrem->dm_list);
3483 * Link into its inodedep. Put it on the id_bufwait list if the inode
3484 * is not yet written. If it is written, do the post-inode write
3485 * processing to put it on the id_pendinghd list.
3487 if (inodedep_lookup(mp, newinum, DEPALLOC, &inodedep) == 0 ||
3488 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3489 dap->da_state |= COMPLETE;
3490 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3491 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3493 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
3495 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3501 * Called whenever the link count on an inode is changed.
3502 * It creates an inode dependency so that the new reference(s)
3503 * to the inode cannot be committed to disk until the updated
3504 * inode has been written.
3507 softdep_change_linkcnt(ip)
3508 struct inode *ip; /* the inode with the increased link count */
3510 struct inodedep *inodedep;
3513 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number,
3514 DEPALLOC, &inodedep);
3515 if (ip->i_nlink < ip->i_effnlink)
3516 panic("softdep_change_linkcnt: bad delta");
3517 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3522 * Called when the effective link count and the reference count
3523 * on an inode drops to zero. At this point there are no names
3524 * referencing the file in the filesystem and no active file
3525 * references. The space associated with the file will be freed
3526 * as soon as the necessary soft dependencies are cleared.
3529 softdep_releasefile(ip)
3530 struct inode *ip; /* inode with the zero effective link count */
3532 struct inodedep *inodedep;
3536 if (ip->i_effnlink > 0)
3537 panic("softdep_releasefile: file still referenced");
3539 * We may be called several times as the on-disk link count
3540 * drops to zero. We only want to account for the space once.
3542 if (ip->i_flag & IN_SPACECOUNTED)
3545 * We have to deactivate a snapshot otherwise copyonwrites may
3546 * add blocks and the cleanup may remove blocks after we have
3547 * tried to account for them.
3549 if ((ip->i_flags & SF_SNAPSHOT) != 0)
3550 ffs_snapremove(ITOV(ip));
3552 * If we are tracking an nlinkdelta, we have to also remember
3553 * whether we accounted for the freed space yet.
3556 if ((inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, &inodedep)))
3557 inodedep->id_state |= SPACECOUNTED;
3561 if (fs->fs_magic == FS_UFS2_MAGIC)
3562 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
3563 UFS_LOCK(ip->i_ump);
3564 ip->i_fs->fs_pendingblocks += DIP(ip, i_blocks) - extblocks;
3565 ip->i_fs->fs_pendinginodes += 1;
3566 UFS_UNLOCK(ip->i_ump);
3567 ip->i_flag |= IN_SPACECOUNTED;
3571 * This workitem decrements the inode's link count.
3572 * If the link count reaches zero, the file is removed.
3575 handle_workitem_remove(dirrem, xp)
3576 struct dirrem *dirrem;
3579 struct thread *td = curthread;
3580 struct inodedep *inodedep;
3586 if ((vp = xp) == NULL &&
3587 (error = ffs_vgetf(dirrem->dm_list.wk_mp,
3588 dirrem->dm_oldinum, LK_EXCLUSIVE, &vp, FFSV_FORCEINSMQ)) != 0) {
3589 softdep_error("handle_workitem_remove: vget", error);
3594 if ((inodedep_lookup(dirrem->dm_list.wk_mp,
3595 dirrem->dm_oldinum, 0, &inodedep)) == 0)
3596 panic("handle_workitem_remove: lost inodedep");
3598 * Normal file deletion.
3600 if ((dirrem->dm_state & RMDIR) == 0) {
3602 DIP_SET(ip, i_nlink, ip->i_nlink);
3603 ip->i_flag |= IN_CHANGE;
3604 if (ip->i_nlink < ip->i_effnlink)
3605 panic("handle_workitem_remove: bad file delta");
3606 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3608 WORKITEM_FREE(dirrem, D_DIRREM);
3614 * Directory deletion. Decrement reference count for both the
3615 * just deleted parent directory entry and the reference for ".".
3616 * Next truncate the directory to length zero. When the
3617 * truncation completes, arrange to have the reference count on
3618 * the parent decremented to account for the loss of "..".
3621 DIP_SET(ip, i_nlink, ip->i_nlink);
3622 ip->i_flag |= IN_CHANGE;
3623 if (ip->i_nlink < ip->i_effnlink)
3624 panic("handle_workitem_remove: bad dir delta");
3625 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3627 if ((error = ffs_truncate(vp, (off_t)0, 0, td->td_ucred, td)) != 0)
3628 softdep_error("handle_workitem_remove: truncate", error);
3631 * Rename a directory to a new parent. Since, we are both deleting
3632 * and creating a new directory entry, the link count on the new
3633 * directory should not change. Thus we skip the followup dirrem.
3635 if (dirrem->dm_state & DIRCHG) {
3637 WORKITEM_FREE(dirrem, D_DIRREM);
3643 * If the inodedep does not exist, then the zero'ed inode has
3644 * been written to disk. If the allocated inode has never been
3645 * written to disk, then the on-disk inode is zero'ed. In either
3646 * case we can remove the file immediately.
3648 dirrem->dm_state = 0;
3649 oldinum = dirrem->dm_oldinum;
3650 dirrem->dm_oldinum = dirrem->dm_dirinum;
3651 if (inodedep_lookup(dirrem->dm_list.wk_mp, oldinum,
3652 0, &inodedep) == 0 || check_inode_unwritten(inodedep)) {
3654 add_to_worklist(&dirrem->dm_list);
3658 handle_workitem_remove(dirrem, NULL);
3661 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
3663 ip->i_flag |= IN_CHANGE;
3669 * Inode de-allocation dependencies.
3671 * When an inode's link count is reduced to zero, it can be de-allocated. We
3672 * found it convenient to postpone de-allocation until after the inode is
3673 * written to disk with its new link count (zero). At this point, all of the
3674 * on-disk inode's block pointers are nullified and, with careful dependency
3675 * list ordering, all dependencies related to the inode will be satisfied and
3676 * the corresponding dependency structures de-allocated. So, if/when the
3677 * inode is reused, there will be no mixing of old dependencies with new
3678 * ones. This artificial dependency is set up by the block de-allocation
3679 * procedure above (softdep_setup_freeblocks) and completed by the
3680 * following procedure.
3683 handle_workitem_freefile(freefile)
3684 struct freefile *freefile;
3687 struct inodedep *idp;
3688 struct ufsmount *ump;
3691 ump = VFSTOUFS(freefile->fx_list.wk_mp);
3695 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
3698 panic("handle_workitem_freefile: inodedep survived");
3701 fs->fs_pendinginodes -= 1;
3703 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
3704 freefile->fx_oldinum, freefile->fx_mode)) != 0)
3705 softdep_error("handle_workitem_freefile", error);
3707 WORKITEM_FREE(freefile, D_FREEFILE);
3713 * Helper function which unlinks marker element from work list and returns
3714 * the next element on the list.
3716 static __inline struct worklist *
3717 markernext(struct worklist *marker)
3719 struct worklist *next;
3721 next = LIST_NEXT(marker, wk_list);
3722 LIST_REMOVE(marker, wk_list);
3729 * The dependency structures constructed above are most actively used when file
3730 * system blocks are written to disk. No constraints are placed on when a
3731 * block can be written, but unsatisfied update dependencies are made safe by
3732 * modifying (or replacing) the source memory for the duration of the disk
3733 * write. When the disk write completes, the memory block is again brought
3736 * In-core inode structure reclamation.
3738 * Because there are a finite number of "in-core" inode structures, they are
3739 * reused regularly. By transferring all inode-related dependencies to the
3740 * in-memory inode block and indexing them separately (via "inodedep"s), we
3741 * can allow "in-core" inode structures to be reused at any time and avoid
3742 * any increase in contention.
3744 * Called just before entering the device driver to initiate a new disk I/O.
3745 * The buffer must be locked, thus, no I/O completion operations can occur
3746 * while we are manipulating its associated dependencies.
3749 softdep_disk_io_initiation(bp)
3750 struct buf *bp; /* structure describing disk write to occur */
3752 struct worklist *wk;
3753 struct worklist marker;
3754 struct indirdep *indirdep;
3755 struct inodedep *inodedep;
3758 * We only care about write operations. There should never
3759 * be dependencies for reads.
3761 if (bp->b_iocmd != BIO_WRITE)
3762 panic("softdep_disk_io_initiation: not write");
3764 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
3765 PHOLD(curproc); /* Don't swap out kernel stack */
3769 * Do any necessary pre-I/O processing.
3771 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
3772 wk = markernext(&marker)) {
3773 LIST_INSERT_AFTER(wk, &marker, wk_list);
3774 switch (wk->wk_type) {
3777 initiate_write_filepage(WK_PAGEDEP(wk), bp);
3781 inodedep = WK_INODEDEP(wk);
3782 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
3783 initiate_write_inodeblock_ufs1(inodedep, bp);
3785 initiate_write_inodeblock_ufs2(inodedep, bp);
3789 indirdep = WK_INDIRDEP(wk);
3790 if (indirdep->ir_state & GOINGAWAY)
3791 panic("disk_io_initiation: indirdep gone");
3793 * If there are no remaining dependencies, this
3794 * will be writing the real pointers, so the
3795 * dependency can be freed.
3797 if (LIST_EMPTY(&indirdep->ir_deplisthd)) {
3800 bp = indirdep->ir_savebp;
3801 bp->b_flags |= B_INVAL | B_NOCACHE;
3802 /* inline expand WORKLIST_REMOVE(wk); */
3803 wk->wk_state &= ~ONWORKLIST;
3804 LIST_REMOVE(wk, wk_list);
3805 WORKITEM_FREE(indirdep, D_INDIRDEP);
3812 * Replace up-to-date version with safe version.
3815 indirdep->ir_saveddata = malloc(bp->b_bcount,
3816 M_INDIRDEP, M_SOFTDEP_FLAGS);
3818 indirdep->ir_state &= ~ATTACHED;
3819 indirdep->ir_state |= UNDONE;
3820 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3821 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3832 panic("handle_disk_io_initiation: Unexpected type %s",
3833 TYPENAME(wk->wk_type));
3838 PRELE(curproc); /* Allow swapout of kernel stack */
3842 * Called from within the procedure above to deal with unsatisfied
3843 * allocation dependencies in a directory. The buffer must be locked,
3844 * thus, no I/O completion operations can occur while we are
3845 * manipulating its associated dependencies.
3848 initiate_write_filepage(pagedep, bp)
3849 struct pagedep *pagedep;
3856 if (pagedep->pd_state & IOSTARTED) {
3858 * This can only happen if there is a driver that does not
3859 * understand chaining. Here biodone will reissue the call
3860 * to strategy for the incomplete buffers.
3862 printf("initiate_write_filepage: already started\n");
3865 pagedep->pd_state |= IOSTARTED;
3866 for (i = 0; i < DAHASHSZ; i++) {
3867 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3868 ep = (struct direct *)
3869 ((char *)bp->b_data + dap->da_offset);
3870 if (ep->d_ino != dap->da_newinum)
3871 panic("%s: dir inum %d != new %d",
3872 "initiate_write_filepage",
3873 ep->d_ino, dap->da_newinum);
3874 if (dap->da_state & DIRCHG)
3875 ep->d_ino = dap->da_previous->dm_oldinum;
3878 dap->da_state &= ~ATTACHED;
3879 dap->da_state |= UNDONE;
3885 * Version of initiate_write_inodeblock that handles UFS1 dinodes.
3886 * Note that any bug fixes made to this routine must be done in the
3887 * version found below.
3889 * Called from within the procedure above to deal with unsatisfied
3890 * allocation dependencies in an inodeblock. The buffer must be
3891 * locked, thus, no I/O completion operations can occur while we
3892 * are manipulating its associated dependencies.
3895 initiate_write_inodeblock_ufs1(inodedep, bp)
3896 struct inodedep *inodedep;
3897 struct buf *bp; /* The inode block */
3899 struct allocdirect *adp, *lastadp;
3900 struct ufs1_dinode *dp;
3901 struct ufs1_dinode *sip;
3905 ufs_lbn_t prevlbn = 0;
3909 if (inodedep->id_state & IOSTARTED)
3910 panic("initiate_write_inodeblock_ufs1: already started");
3911 inodedep->id_state |= IOSTARTED;
3912 fs = inodedep->id_fs;
3913 dp = (struct ufs1_dinode *)bp->b_data +
3914 ino_to_fsbo(fs, inodedep->id_ino);
3916 * If the bitmap is not yet written, then the allocated
3917 * inode cannot be written to disk.
3919 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3920 if (inodedep->id_savedino1 != NULL)
3921 panic("initiate_write_inodeblock_ufs1: I/O underway");
3923 sip = malloc(sizeof(struct ufs1_dinode),
3924 M_SAVEDINO, M_SOFTDEP_FLAGS);
3926 inodedep->id_savedino1 = sip;
3927 *inodedep->id_savedino1 = *dp;
3928 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
3929 dp->di_gen = inodedep->id_savedino1->di_gen;
3933 * If no dependencies, then there is nothing to roll back.
3935 inodedep->id_savedsize = dp->di_size;
3936 inodedep->id_savedextsize = 0;
3937 if (TAILQ_EMPTY(&inodedep->id_inoupdt))
3940 * Set the dependencies to busy.
3942 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3943 adp = TAILQ_NEXT(adp, ad_next)) {
3945 if (deplist != 0 && prevlbn >= adp->ad_lbn)
3946 panic("softdep_write_inodeblock: lbn order");
3947 prevlbn = adp->ad_lbn;
3948 if (adp->ad_lbn < NDADDR &&
3949 dp->di_db[adp->ad_lbn] != adp->ad_newblkno)
3950 panic("%s: direct pointer #%jd mismatch %d != %jd",
3951 "softdep_write_inodeblock",
3952 (intmax_t)adp->ad_lbn,
3953 dp->di_db[adp->ad_lbn],
3954 (intmax_t)adp->ad_newblkno);
3955 if (adp->ad_lbn >= NDADDR &&
3956 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno)
3957 panic("%s: indirect pointer #%jd mismatch %d != %jd",
3958 "softdep_write_inodeblock",
3959 (intmax_t)adp->ad_lbn - NDADDR,
3960 dp->di_ib[adp->ad_lbn - NDADDR],
3961 (intmax_t)adp->ad_newblkno);
3962 deplist |= 1 << adp->ad_lbn;
3963 if ((adp->ad_state & ATTACHED) == 0)
3964 panic("softdep_write_inodeblock: Unknown state 0x%x",
3966 #endif /* INVARIANTS */
3967 adp->ad_state &= ~ATTACHED;
3968 adp->ad_state |= UNDONE;
3971 * The on-disk inode cannot claim to be any larger than the last
3972 * fragment that has been written. Otherwise, the on-disk inode
3973 * might have fragments that were not the last block in the file
3974 * which would corrupt the filesystem.
3976 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3977 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3978 if (adp->ad_lbn >= NDADDR)
3980 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3981 /* keep going until hitting a rollback to a frag */
3982 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3984 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3985 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3987 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
3988 panic("softdep_write_inodeblock: lost dep1");
3989 #endif /* INVARIANTS */
3992 for (i = 0; i < NIADDR; i++) {
3994 if (dp->di_ib[i] != 0 &&
3995 (deplist & ((1 << NDADDR) << i)) == 0)
3996 panic("softdep_write_inodeblock: lost dep2");
3997 #endif /* INVARIANTS */
4003 * If we have zero'ed out the last allocated block of the file,
4004 * roll back the size to the last currently allocated block.
4005 * We know that this last allocated block is a full-sized as
4006 * we already checked for fragments in the loop above.
4008 if (lastadp != NULL &&
4009 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4010 for (i = lastadp->ad_lbn; i >= 0; i--)
4011 if (dp->di_db[i] != 0)
4013 dp->di_size = (i + 1) * fs->fs_bsize;
4016 * The only dependencies are for indirect blocks.
4018 * The file size for indirect block additions is not guaranteed.
4019 * Such a guarantee would be non-trivial to achieve. The conventional
4020 * synchronous write implementation also does not make this guarantee.
4021 * Fsck should catch and fix discrepancies. Arguably, the file size
4022 * can be over-estimated without destroying integrity when the file
4023 * moves into the indirect blocks (i.e., is large). If we want to
4024 * postpone fsck, we are stuck with this argument.
4026 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
4027 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
4031 * Version of initiate_write_inodeblock that handles UFS2 dinodes.
4032 * Note that any bug fixes made to this routine must be done in the
4033 * version found above.
4035 * Called from within the procedure above to deal with unsatisfied
4036 * allocation dependencies in an inodeblock. The buffer must be
4037 * locked, thus, no I/O completion operations can occur while we
4038 * are manipulating its associated dependencies.
4041 initiate_write_inodeblock_ufs2(inodedep, bp)
4042 struct inodedep *inodedep;
4043 struct buf *bp; /* The inode block */
4045 struct allocdirect *adp, *lastadp;
4046 struct ufs2_dinode *dp;
4047 struct ufs2_dinode *sip;
4051 ufs_lbn_t prevlbn = 0;
4055 if (inodedep->id_state & IOSTARTED)
4056 panic("initiate_write_inodeblock_ufs2: already started");
4057 inodedep->id_state |= IOSTARTED;
4058 fs = inodedep->id_fs;
4059 dp = (struct ufs2_dinode *)bp->b_data +
4060 ino_to_fsbo(fs, inodedep->id_ino);
4062 * If the bitmap is not yet written, then the allocated
4063 * inode cannot be written to disk.
4065 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4066 if (inodedep->id_savedino2 != NULL)
4067 panic("initiate_write_inodeblock_ufs2: I/O underway");
4069 sip = malloc(sizeof(struct ufs2_dinode),
4070 M_SAVEDINO, M_SOFTDEP_FLAGS);
4072 inodedep->id_savedino2 = sip;
4073 *inodedep->id_savedino2 = *dp;
4074 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
4075 dp->di_gen = inodedep->id_savedino2->di_gen;
4079 * If no dependencies, then there is nothing to roll back.
4081 inodedep->id_savedsize = dp->di_size;
4082 inodedep->id_savedextsize = dp->di_extsize;
4083 if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
4084 TAILQ_EMPTY(&inodedep->id_extupdt))
4087 * Set the ext data dependencies to busy.
4089 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
4090 adp = TAILQ_NEXT(adp, ad_next)) {
4092 if (deplist != 0 && prevlbn >= adp->ad_lbn)
4093 panic("softdep_write_inodeblock: lbn order");
4094 prevlbn = adp->ad_lbn;
4095 if (dp->di_extb[adp->ad_lbn] != adp->ad_newblkno)
4096 panic("%s: direct pointer #%jd mismatch %jd != %jd",
4097 "softdep_write_inodeblock",
4098 (intmax_t)adp->ad_lbn,
4099 (intmax_t)dp->di_extb[adp->ad_lbn],
4100 (intmax_t)adp->ad_newblkno);
4101 deplist |= 1 << adp->ad_lbn;
4102 if ((adp->ad_state & ATTACHED) == 0)
4103 panic("softdep_write_inodeblock: Unknown state 0x%x",
4105 #endif /* INVARIANTS */
4106 adp->ad_state &= ~ATTACHED;
4107 adp->ad_state |= UNDONE;
4110 * The on-disk inode cannot claim to be any larger than the last
4111 * fragment that has been written. Otherwise, the on-disk inode
4112 * might have fragments that were not the last block in the ext
4113 * data which would corrupt the filesystem.
4115 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
4116 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
4117 dp->di_extb[adp->ad_lbn] = adp->ad_oldblkno;
4118 /* keep going until hitting a rollback to a frag */
4119 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
4121 dp->di_extsize = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
4122 for (i = adp->ad_lbn + 1; i < NXADDR; i++) {
4124 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
4125 panic("softdep_write_inodeblock: lost dep1");
4126 #endif /* INVARIANTS */
4133 * If we have zero'ed out the last allocated block of the ext
4134 * data, roll back the size to the last currently allocated block.
4135 * We know that this last allocated block is a full-sized as
4136 * we already checked for fragments in the loop above.
4138 if (lastadp != NULL &&
4139 dp->di_extsize <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4140 for (i = lastadp->ad_lbn; i >= 0; i--)
4141 if (dp->di_extb[i] != 0)
4143 dp->di_extsize = (i + 1) * fs->fs_bsize;
4146 * Set the file data dependencies to busy.
4148 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
4149 adp = TAILQ_NEXT(adp, ad_next)) {
4151 if (deplist != 0 && prevlbn >= adp->ad_lbn)
4152 panic("softdep_write_inodeblock: lbn order");
4153 prevlbn = adp->ad_lbn;
4154 if (adp->ad_lbn < NDADDR &&
4155 dp->di_db[adp->ad_lbn] != adp->ad_newblkno)
4156 panic("%s: direct pointer #%jd mismatch %jd != %jd",
4157 "softdep_write_inodeblock",
4158 (intmax_t)adp->ad_lbn,
4159 (intmax_t)dp->di_db[adp->ad_lbn],
4160 (intmax_t)adp->ad_newblkno);
4161 if (adp->ad_lbn >= NDADDR &&
4162 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno)
4163 panic("%s indirect pointer #%jd mismatch %jd != %jd",
4164 "softdep_write_inodeblock:",
4165 (intmax_t)adp->ad_lbn - NDADDR,
4166 (intmax_t)dp->di_ib[adp->ad_lbn - NDADDR],
4167 (intmax_t)adp->ad_newblkno);
4168 deplist |= 1 << adp->ad_lbn;
4169 if ((adp->ad_state & ATTACHED) == 0)
4170 panic("softdep_write_inodeblock: Unknown state 0x%x",
4172 #endif /* INVARIANTS */
4173 adp->ad_state &= ~ATTACHED;
4174 adp->ad_state |= UNDONE;
4177 * The on-disk inode cannot claim to be any larger than the last
4178 * fragment that has been written. Otherwise, the on-disk inode
4179 * might have fragments that were not the last block in the file
4180 * which would corrupt the filesystem.
4182 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
4183 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
4184 if (adp->ad_lbn >= NDADDR)
4186 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
4187 /* keep going until hitting a rollback to a frag */
4188 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
4190 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
4191 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
4193 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
4194 panic("softdep_write_inodeblock: lost dep2");
4195 #endif /* INVARIANTS */
4198 for (i = 0; i < NIADDR; i++) {
4200 if (dp->di_ib[i] != 0 &&
4201 (deplist & ((1 << NDADDR) << i)) == 0)
4202 panic("softdep_write_inodeblock: lost dep3");
4203 #endif /* INVARIANTS */
4209 * If we have zero'ed out the last allocated block of the file,
4210 * roll back the size to the last currently allocated block.
4211 * We know that this last allocated block is a full-sized as
4212 * we already checked for fragments in the loop above.
4214 if (lastadp != NULL &&
4215 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4216 for (i = lastadp->ad_lbn; i >= 0; i--)
4217 if (dp->di_db[i] != 0)
4219 dp->di_size = (i + 1) * fs->fs_bsize;
4222 * The only dependencies are for indirect blocks.
4224 * The file size for indirect block additions is not guaranteed.
4225 * Such a guarantee would be non-trivial to achieve. The conventional
4226 * synchronous write implementation also does not make this guarantee.
4227 * Fsck should catch and fix discrepancies. Arguably, the file size
4228 * can be over-estimated without destroying integrity when the file
4229 * moves into the indirect blocks (i.e., is large). If we want to
4230 * postpone fsck, we are stuck with this argument.
4232 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
4233 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
4237 * This routine is called during the completion interrupt
4238 * service routine for a disk write (from the procedure called
4239 * by the device driver to inform the filesystem caches of
4240 * a request completion). It should be called early in this
4241 * procedure, before the block is made available to other
4242 * processes or other routines are called.
4245 softdep_disk_write_complete(bp)
4246 struct buf *bp; /* describes the completed disk write */
4248 struct worklist *wk;
4249 struct worklist *owk;
4250 struct workhead reattach;
4251 struct newblk *newblk;
4252 struct allocindir *aip;
4253 struct allocdirect *adp;
4254 struct indirdep *indirdep;
4255 struct inodedep *inodedep;
4256 struct bmsafemap *bmsafemap;
4259 * If an error occurred while doing the write, then the data
4260 * has not hit the disk and the dependencies cannot be unrolled.
4262 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0)
4264 LIST_INIT(&reattach);
4266 * This lock must not be released anywhere in this code segment.
4270 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
4271 WORKLIST_REMOVE(wk);
4273 panic("duplicate worklist: %p\n", wk);
4275 switch (wk->wk_type) {
4278 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
4279 WORKLIST_INSERT(&reattach, wk);
4283 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
4284 WORKLIST_INSERT(&reattach, wk);
4288 bmsafemap = WK_BMSAFEMAP(wk);
4289 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
4290 newblk->nb_state |= DEPCOMPLETE;
4291 newblk->nb_bmsafemap = NULL;
4292 LIST_REMOVE(newblk, nb_deps);
4295 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
4296 adp->ad_state |= DEPCOMPLETE;
4298 LIST_REMOVE(adp, ad_deps);
4299 handle_allocdirect_partdone(adp);
4302 LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
4303 aip->ai_state |= DEPCOMPLETE;
4305 LIST_REMOVE(aip, ai_deps);
4306 handle_allocindir_partdone(aip);
4309 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
4310 inodedep->id_state |= DEPCOMPLETE;
4311 LIST_REMOVE(inodedep, id_deps);
4312 inodedep->id_buf = NULL;
4314 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
4318 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
4322 adp = WK_ALLOCDIRECT(wk);
4323 adp->ad_state |= COMPLETE;
4324 handle_allocdirect_partdone(adp);
4328 aip = WK_ALLOCINDIR(wk);
4329 aip->ai_state |= COMPLETE;
4330 handle_allocindir_partdone(aip);
4334 indirdep = WK_INDIRDEP(wk);
4335 if (indirdep->ir_state & GOINGAWAY)
4336 panic("disk_write_complete: indirdep gone");
4337 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
4338 free(indirdep->ir_saveddata, M_INDIRDEP);
4339 indirdep->ir_saveddata = 0;
4340 indirdep->ir_state &= ~UNDONE;
4341 indirdep->ir_state |= ATTACHED;
4342 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
4343 handle_allocindir_partdone(aip);
4344 if (aip == LIST_FIRST(&indirdep->ir_donehd))
4345 panic("disk_write_complete: not gone");
4347 WORKLIST_INSERT(&reattach, wk);
4348 if ((bp->b_flags & B_DELWRI) == 0)
4349 stat_indir_blk_ptrs++;
4354 panic("handle_disk_write_complete: Unknown type %s",
4355 TYPENAME(wk->wk_type));
4360 * Reattach any requests that must be redone.
4362 while ((wk = LIST_FIRST(&reattach)) != NULL) {
4363 WORKLIST_REMOVE(wk);
4364 WORKLIST_INSERT(&bp->b_dep, wk);
4370 * Called from within softdep_disk_write_complete above. Note that
4371 * this routine is always called from interrupt level with further
4372 * splbio interrupts blocked.
4375 handle_allocdirect_partdone(adp)
4376 struct allocdirect *adp; /* the completed allocdirect */
4378 struct allocdirectlst *listhead;
4379 struct allocdirect *listadp;
4380 struct inodedep *inodedep;
4383 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4385 if (adp->ad_buf != NULL)
4386 panic("handle_allocdirect_partdone: dangling dep");
4388 * The on-disk inode cannot claim to be any larger than the last
4389 * fragment that has been written. Otherwise, the on-disk inode
4390 * might have fragments that were not the last block in the file
4391 * which would corrupt the filesystem. Thus, we cannot free any
4392 * allocdirects after one whose ad_oldblkno claims a fragment as
4393 * these blocks must be rolled back to zero before writing the inode.
4394 * We check the currently active set of allocdirects in id_inoupdt
4395 * or id_extupdt as appropriate.
4397 inodedep = adp->ad_inodedep;
4398 bsize = inodedep->id_fs->fs_bsize;
4399 if (adp->ad_state & EXTDATA)
4400 listhead = &inodedep->id_extupdt;
4402 listhead = &inodedep->id_inoupdt;
4403 TAILQ_FOREACH(listadp, listhead, ad_next) {
4404 /* found our block */
4407 /* continue if ad_oldlbn is not a fragment */
4408 if (listadp->ad_oldsize == 0 ||
4409 listadp->ad_oldsize == bsize)
4411 /* hit a fragment */
4415 * If we have reached the end of the current list without
4416 * finding the just finished dependency, then it must be
4417 * on the future dependency list. Future dependencies cannot
4418 * be freed until they are moved to the current list.
4420 if (listadp == NULL) {
4422 if (adp->ad_state & EXTDATA)
4423 listhead = &inodedep->id_newextupdt;
4425 listhead = &inodedep->id_newinoupdt;
4426 TAILQ_FOREACH(listadp, listhead, ad_next)
4427 /* found our block */
4430 if (listadp == NULL)
4431 panic("handle_allocdirect_partdone: lost dep");
4436 * If we have found the just finished dependency, then free
4437 * it along with anything that follows it that is complete.
4438 * If the inode still has a bitmap dependency, then it has
4439 * never been written to disk, hence the on-disk inode cannot
4440 * reference the old fragment so we can free it without delay.
4442 delay = (inodedep->id_state & DEPCOMPLETE);
4443 for (; adp; adp = listadp) {
4444 listadp = TAILQ_NEXT(adp, ad_next);
4445 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4447 free_allocdirect(listhead, adp, delay);
4452 * Called from within softdep_disk_write_complete above. Note that
4453 * this routine is always called from interrupt level with further
4454 * splbio interrupts blocked.
4457 handle_allocindir_partdone(aip)
4458 struct allocindir *aip; /* the completed allocindir */
4460 struct indirdep *indirdep;
4462 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
4464 if (aip->ai_buf != NULL)
4465 panic("handle_allocindir_partdone: dangling dependency");
4466 indirdep = aip->ai_indirdep;
4467 if (indirdep->ir_state & UNDONE) {
4468 LIST_REMOVE(aip, ai_next);
4469 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
4472 if (indirdep->ir_state & UFS1FMT)
4473 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4476 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4478 LIST_REMOVE(aip, ai_next);
4479 if (aip->ai_freefrag != NULL)
4480 add_to_worklist(&aip->ai_freefrag->ff_list);
4481 WORKITEM_FREE(aip, D_ALLOCINDIR);
4485 * Called from within softdep_disk_write_complete above to restore
4486 * in-memory inode block contents to their most up-to-date state. Note
4487 * that this routine is always called from interrupt level with further
4488 * splbio interrupts blocked.
4491 handle_written_inodeblock(inodedep, bp)
4492 struct inodedep *inodedep;
4493 struct buf *bp; /* buffer containing the inode block */
4495 struct worklist *wk, *filefree;
4496 struct allocdirect *adp, *nextadp;
4497 struct ufs1_dinode *dp1 = NULL;
4498 struct ufs2_dinode *dp2 = NULL;
4499 int hadchanges, fstype;
4501 if ((inodedep->id_state & IOSTARTED) == 0)
4502 panic("handle_written_inodeblock: not started");
4503 inodedep->id_state &= ~IOSTARTED;
4504 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
4506 dp1 = (struct ufs1_dinode *)bp->b_data +
4507 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4510 dp2 = (struct ufs2_dinode *)bp->b_data +
4511 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4514 * If we had to rollback the inode allocation because of
4515 * bitmaps being incomplete, then simply restore it.
4516 * Keep the block dirty so that it will not be reclaimed until
4517 * all associated dependencies have been cleared and the
4518 * corresponding updates written to disk.
4520 if (inodedep->id_savedino1 != NULL) {
4522 *dp1 = *inodedep->id_savedino1;
4524 *dp2 = *inodedep->id_savedino2;
4525 free(inodedep->id_savedino1, M_SAVEDINO);
4526 inodedep->id_savedino1 = NULL;
4527 if ((bp->b_flags & B_DELWRI) == 0)
4528 stat_inode_bitmap++;
4532 inodedep->id_state |= COMPLETE;
4534 * Roll forward anything that had to be rolled back before
4535 * the inode could be updated.
4538 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
4539 nextadp = TAILQ_NEXT(adp, ad_next);
4540 if (adp->ad_state & ATTACHED)
4541 panic("handle_written_inodeblock: new entry");
4542 if (fstype == UFS1) {
4543 if (adp->ad_lbn < NDADDR) {
4544 if (dp1->di_db[adp->ad_lbn]!=adp->ad_oldblkno)
4545 panic("%s %s #%jd mismatch %d != %jd",
4546 "handle_written_inodeblock:",
4548 (intmax_t)adp->ad_lbn,
4549 dp1->di_db[adp->ad_lbn],
4550 (intmax_t)adp->ad_oldblkno);
4551 dp1->di_db[adp->ad_lbn] = adp->ad_newblkno;
4553 if (dp1->di_ib[adp->ad_lbn - NDADDR] != 0)
4554 panic("%s: %s #%jd allocated as %d",
4555 "handle_written_inodeblock",
4557 (intmax_t)adp->ad_lbn - NDADDR,
4558 dp1->di_ib[adp->ad_lbn - NDADDR]);
4559 dp1->di_ib[adp->ad_lbn - NDADDR] =
4563 if (adp->ad_lbn < NDADDR) {
4564 if (dp2->di_db[adp->ad_lbn]!=adp->ad_oldblkno)
4565 panic("%s: %s #%jd %s %jd != %jd",
4566 "handle_written_inodeblock",
4568 (intmax_t)adp->ad_lbn, "mismatch",
4569 (intmax_t)dp2->di_db[adp->ad_lbn],
4570 (intmax_t)adp->ad_oldblkno);
4571 dp2->di_db[adp->ad_lbn] = adp->ad_newblkno;
4573 if (dp2->di_ib[adp->ad_lbn - NDADDR] != 0)
4574 panic("%s: %s #%jd allocated as %jd",
4575 "handle_written_inodeblock",
4577 (intmax_t)adp->ad_lbn - NDADDR,
4579 dp2->di_ib[adp->ad_lbn - NDADDR]);
4580 dp2->di_ib[adp->ad_lbn - NDADDR] =
4584 adp->ad_state &= ~UNDONE;
4585 adp->ad_state |= ATTACHED;
4588 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
4589 nextadp = TAILQ_NEXT(adp, ad_next);
4590 if (adp->ad_state & ATTACHED)
4591 panic("handle_written_inodeblock: new entry");
4592 if (dp2->di_extb[adp->ad_lbn] != adp->ad_oldblkno)
4593 panic("%s: direct pointers #%jd %s %jd != %jd",
4594 "handle_written_inodeblock",
4595 (intmax_t)adp->ad_lbn, "mismatch",
4596 (intmax_t)dp2->di_extb[adp->ad_lbn],
4597 (intmax_t)adp->ad_oldblkno);
4598 dp2->di_extb[adp->ad_lbn] = adp->ad_newblkno;
4599 adp->ad_state &= ~UNDONE;
4600 adp->ad_state |= ATTACHED;
4603 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
4604 stat_direct_blk_ptrs++;
4606 * Reset the file size to its most up-to-date value.
4608 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
4609 panic("handle_written_inodeblock: bad size");
4610 if (fstype == UFS1) {
4611 if (dp1->di_size != inodedep->id_savedsize) {
4612 dp1->di_size = inodedep->id_savedsize;
4616 if (dp2->di_size != inodedep->id_savedsize) {
4617 dp2->di_size = inodedep->id_savedsize;
4620 if (dp2->di_extsize != inodedep->id_savedextsize) {
4621 dp2->di_extsize = inodedep->id_savedextsize;
4625 inodedep->id_savedsize = -1;
4626 inodedep->id_savedextsize = -1;
4628 * If there were any rollbacks in the inode block, then it must be
4629 * marked dirty so that its will eventually get written back in
4635 * Process any allocdirects that completed during the update.
4637 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
4638 handle_allocdirect_partdone(adp);
4639 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
4640 handle_allocdirect_partdone(adp);
4642 * Process deallocations that were held pending until the
4643 * inode had been written to disk. Freeing of the inode
4644 * is delayed until after all blocks have been freed to
4645 * avoid creation of new <vfsid, inum, lbn> triples
4646 * before the old ones have been deleted.
4649 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
4650 WORKLIST_REMOVE(wk);
4651 switch (wk->wk_type) {
4655 * We defer adding filefree to the worklist until
4656 * all other additions have been made to ensure
4657 * that it will be done after all the old blocks
4660 if (filefree != NULL)
4661 panic("handle_written_inodeblock: filefree");
4666 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
4670 diradd_inode_written(WK_DIRADD(wk), inodedep);
4674 wk->wk_state |= COMPLETE;
4675 if ((wk->wk_state & ALLCOMPLETE) != ALLCOMPLETE)
4677 /* -- fall through -- */
4680 add_to_worklist(wk);
4684 free_newdirblk(WK_NEWDIRBLK(wk));
4688 panic("handle_written_inodeblock: Unknown type %s",
4689 TYPENAME(wk->wk_type));
4693 if (filefree != NULL) {
4694 if (free_inodedep(inodedep) == 0)
4695 panic("handle_written_inodeblock: live inodedep");
4696 add_to_worklist(filefree);
4701 * If no outstanding dependencies, free it.
4703 if (free_inodedep(inodedep) ||
4704 (TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
4705 TAILQ_FIRST(&inodedep->id_extupdt) == 0))
4707 return (hadchanges);
4711 * Process a diradd entry after its dependent inode has been written.
4712 * This routine must be called with splbio interrupts blocked.
4715 diradd_inode_written(dap, inodedep)
4717 struct inodedep *inodedep;
4719 struct pagedep *pagedep;
4721 dap->da_state |= COMPLETE;
4722 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4723 if (dap->da_state & DIRCHG)
4724 pagedep = dap->da_previous->dm_pagedep;
4726 pagedep = dap->da_pagedep;
4727 LIST_REMOVE(dap, da_pdlist);
4728 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4730 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
4734 * Handle the completion of a mkdir dependency.
4737 handle_written_mkdir(mkdir, type)
4738 struct mkdir *mkdir;
4742 struct pagedep *pagedep;
4744 if (mkdir->md_state != type)
4745 panic("handle_written_mkdir: bad type");
4746 dap = mkdir->md_diradd;
4747 dap->da_state &= ~type;
4748 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
4749 dap->da_state |= DEPCOMPLETE;
4750 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4751 if (dap->da_state & DIRCHG)
4752 pagedep = dap->da_previous->dm_pagedep;
4754 pagedep = dap->da_pagedep;
4755 LIST_REMOVE(dap, da_pdlist);
4756 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4758 LIST_REMOVE(mkdir, md_mkdirs);
4759 WORKITEM_FREE(mkdir, D_MKDIR);
4763 * Called from within softdep_disk_write_complete above.
4764 * A write operation was just completed. Removed inodes can
4765 * now be freed and associated block pointers may be committed.
4766 * Note that this routine is always called from interrupt level
4767 * with further splbio interrupts blocked.
4770 handle_written_filepage(pagedep, bp)
4771 struct pagedep *pagedep;
4772 struct buf *bp; /* buffer containing the written page */
4774 struct dirrem *dirrem;
4775 struct diradd *dap, *nextdap;
4779 if ((pagedep->pd_state & IOSTARTED) == 0)
4780 panic("handle_written_filepage: not started");
4781 pagedep->pd_state &= ~IOSTARTED;
4783 * Process any directory removals that have been committed.
4785 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
4786 LIST_REMOVE(dirrem, dm_next);
4787 dirrem->dm_dirinum = pagedep->pd_ino;
4788 add_to_worklist(&dirrem->dm_list);
4791 * Free any directory additions that have been committed.
4792 * If it is a newly allocated block, we have to wait until
4793 * the on-disk directory inode claims the new block.
4795 if ((pagedep->pd_state & NEWBLOCK) == 0)
4796 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
4799 * Uncommitted directory entries must be restored.
4801 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
4802 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
4804 nextdap = LIST_NEXT(dap, da_pdlist);
4805 if (dap->da_state & ATTACHED)
4806 panic("handle_written_filepage: attached");
4807 ep = (struct direct *)
4808 ((char *)bp->b_data + dap->da_offset);
4809 ep->d_ino = dap->da_newinum;
4810 dap->da_state &= ~UNDONE;
4811 dap->da_state |= ATTACHED;
4814 * If the inode referenced by the directory has
4815 * been written out, then the dependency can be
4816 * moved to the pending list.
4818 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4819 LIST_REMOVE(dap, da_pdlist);
4820 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
4826 * If there were any rollbacks in the directory, then it must be
4827 * marked dirty so that its will eventually get written back in
4831 if ((bp->b_flags & B_DELWRI) == 0)
4837 * If we are not waiting for a new directory block to be
4838 * claimed by its inode, then the pagedep will be freed.
4839 * Otherwise it will remain to track any new entries on
4840 * the page in case they are fsync'ed.
4842 if ((pagedep->pd_state & NEWBLOCK) == 0) {
4843 LIST_REMOVE(pagedep, pd_hash);
4844 WORKITEM_FREE(pagedep, D_PAGEDEP);
4850 * Writing back in-core inode structures.
4852 * The filesystem only accesses an inode's contents when it occupies an
4853 * "in-core" inode structure. These "in-core" structures are separate from
4854 * the page frames used to cache inode blocks. Only the latter are
4855 * transferred to/from the disk. So, when the updated contents of the
4856 * "in-core" inode structure are copied to the corresponding in-memory inode
4857 * block, the dependencies are also transferred. The following procedure is
4858 * called when copying a dirty "in-core" inode to a cached inode block.
4862 * Called when an inode is loaded from disk. If the effective link count
4863 * differed from the actual link count when it was last flushed, then we
4864 * need to ensure that the correct effective link count is put back.
4867 softdep_load_inodeblock(ip)
4868 struct inode *ip; /* the "in_core" copy of the inode */
4870 struct inodedep *inodedep;
4873 * Check for alternate nlink count.
4875 ip->i_effnlink = ip->i_nlink;
4877 if (inodedep_lookup(UFSTOVFS(ip->i_ump),
4878 ip->i_number, 0, &inodedep) == 0) {
4882 ip->i_effnlink -= inodedep->id_nlinkdelta;
4883 if (inodedep->id_state & SPACECOUNTED)
4884 ip->i_flag |= IN_SPACECOUNTED;
4889 * This routine is called just before the "in-core" inode
4890 * information is to be copied to the in-memory inode block.
4891 * Recall that an inode block contains several inodes. If
4892 * the force flag is set, then the dependencies will be
4893 * cleared so that the update can always be made. Note that
4894 * the buffer is locked when this routine is called, so we
4895 * will never be in the middle of writing the inode block
4899 softdep_update_inodeblock(ip, bp, waitfor)
4900 struct inode *ip; /* the "in_core" copy of the inode */
4901 struct buf *bp; /* the buffer containing the inode block */
4902 int waitfor; /* nonzero => update must be allowed */
4904 struct inodedep *inodedep;
4905 struct worklist *wk;
4911 * If the effective link count is not equal to the actual link
4912 * count, then we must track the difference in an inodedep while
4913 * the inode is (potentially) tossed out of the cache. Otherwise,
4914 * if there is no existing inodedep, then there are no dependencies
4917 mp = UFSTOVFS(ip->i_ump);
4919 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
4921 if (ip->i_effnlink != ip->i_nlink)
4922 panic("softdep_update_inodeblock: bad link count");
4925 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
4926 panic("softdep_update_inodeblock: bad delta");
4928 * Changes have been initiated. Anything depending on these
4929 * changes cannot occur until this inode has been written.
4931 inodedep->id_state &= ~COMPLETE;
4932 if ((inodedep->id_state & ONWORKLIST) == 0)
4933 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
4935 * Any new dependencies associated with the incore inode must
4936 * now be moved to the list associated with the buffer holding
4937 * the in-memory copy of the inode. Once merged process any
4938 * allocdirects that are completed by the merger.
4940 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
4941 if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
4942 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
4943 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
4944 if (!TAILQ_EMPTY(&inodedep->id_extupdt))
4945 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt));
4947 * Now that the inode has been pushed into the buffer, the
4948 * operations dependent on the inode being written to disk
4949 * can be moved to the id_bufwait so that they will be
4950 * processed when the buffer I/O completes.
4952 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
4953 WORKLIST_REMOVE(wk);
4954 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
4957 * Newly allocated inodes cannot be written until the bitmap
4958 * that allocates them have been written (indicated by
4959 * DEPCOMPLETE being set in id_state). If we are doing a
4960 * forced sync (e.g., an fsync on a file), we force the bitmap
4961 * to be written so that the update can be done.
4968 if ((inodedep->id_state & DEPCOMPLETE) != 0) {
4972 ibp = inodedep->id_buf;
4973 ibp = getdirtybuf(ibp, &lk, MNT_WAIT);
4976 * If ibp came back as NULL, the dependency could have been
4977 * freed while we slept. Look it up again, and check to see
4978 * that it has completed.
4980 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
4986 if ((error = bwrite(ibp)) != 0)
4987 softdep_error("softdep_update_inodeblock: bwrite", error);
4991 * Merge the a new inode dependency list (such as id_newinoupdt) into an
4992 * old inode dependency list (such as id_inoupdt). This routine must be
4993 * called with splbio interrupts blocked.
4996 merge_inode_lists(newlisthead, oldlisthead)
4997 struct allocdirectlst *newlisthead;
4998 struct allocdirectlst *oldlisthead;
5000 struct allocdirect *listadp, *newadp;
5002 newadp = TAILQ_FIRST(newlisthead);
5003 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
5004 if (listadp->ad_lbn < newadp->ad_lbn) {
5005 listadp = TAILQ_NEXT(listadp, ad_next);
5008 TAILQ_REMOVE(newlisthead, newadp, ad_next);
5009 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
5010 if (listadp->ad_lbn == newadp->ad_lbn) {
5011 allocdirect_merge(oldlisthead, newadp,
5015 newadp = TAILQ_FIRST(newlisthead);
5017 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
5018 TAILQ_REMOVE(newlisthead, newadp, ad_next);
5019 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
5024 * If we are doing an fsync, then we must ensure that any directory
5025 * entries for the inode have been written after the inode gets to disk.
5029 struct vnode *vp; /* the "in_core" copy of the inode */
5031 struct inodedep *inodedep;
5032 struct pagedep *pagedep;
5033 struct worklist *wk;
5040 struct thread *td = curthread;
5041 int error, flushparent, pagedep_new_block;
5049 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
5053 if (!LIST_EMPTY(&inodedep->id_inowait) ||
5054 !LIST_EMPTY(&inodedep->id_bufwait) ||
5055 !TAILQ_EMPTY(&inodedep->id_extupdt) ||
5056 !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
5057 !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
5058 !TAILQ_EMPTY(&inodedep->id_newinoupdt))
5059 panic("softdep_fsync: pending ops");
5060 for (error = 0, flushparent = 0; ; ) {
5061 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
5063 if (wk->wk_type != D_DIRADD)
5064 panic("softdep_fsync: Unexpected type %s",
5065 TYPENAME(wk->wk_type));
5066 dap = WK_DIRADD(wk);
5068 * Flush our parent if this directory entry has a MKDIR_PARENT
5069 * dependency or is contained in a newly allocated block.
5071 if (dap->da_state & DIRCHG)
5072 pagedep = dap->da_previous->dm_pagedep;
5074 pagedep = dap->da_pagedep;
5075 parentino = pagedep->pd_ino;
5076 lbn = pagedep->pd_lbn;
5077 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
5078 panic("softdep_fsync: dirty");
5079 if ((dap->da_state & MKDIR_PARENT) ||
5080 (pagedep->pd_state & NEWBLOCK))
5085 * If we are being fsync'ed as part of vgone'ing this vnode,
5086 * then we will not be able to release and recover the
5087 * vnode below, so we just have to give up on writing its
5088 * directory entry out. It will eventually be written, just
5089 * not now, but then the user was not asking to have it
5090 * written, so we are not breaking any promises.
5092 if (vp->v_iflag & VI_DOOMED)
5095 * We prevent deadlock by always fetching inodes from the
5096 * root, moving down the directory tree. Thus, when fetching
5097 * our parent directory, we first try to get the lock. If
5098 * that fails, we must unlock ourselves before requesting
5099 * the lock on our parent. See the comment in ufs_lookup
5100 * for details on possible races.
5103 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp,
5105 error = vfs_busy(mp, MBF_NOWAIT);
5109 error = vfs_busy(mp, 0);
5110 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5114 if (vp->v_iflag & VI_DOOMED) {
5120 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE,
5121 &pvp, FFSV_FORCEINSMQ);
5123 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5124 if (vp->v_iflag & VI_DOOMED) {
5133 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
5134 * that are contained in direct blocks will be resolved by
5135 * doing a ffs_update. Pagedeps contained in indirect blocks
5136 * may require a complete sync'ing of the directory. So, we
5137 * try the cheap and fast ffs_update first, and if that fails,
5138 * then we do the slower ffs_syncvnode of the directory.
5143 if ((error = ffs_update(pvp, 1)) != 0) {
5149 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
5150 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
5151 if (wk->wk_type != D_DIRADD)
5152 panic("softdep_fsync: Unexpected type %s",
5153 TYPENAME(wk->wk_type));
5154 dap = WK_DIRADD(wk);
5155 if (dap->da_state & DIRCHG)
5156 pagedep = dap->da_previous->dm_pagedep;
5158 pagedep = dap->da_pagedep;
5159 pagedep_new_block = pagedep->pd_state & NEWBLOCK;
5162 if (pagedep_new_block &&
5163 (error = ffs_syncvnode(pvp, MNT_WAIT))) {
5173 * Flush directory page containing the inode's name.
5175 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
5185 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
5193 * Flush all the dirty bitmaps associated with the block device
5194 * before flushing the rest of the dirty blocks so as to reduce
5195 * the number of dependencies that will have to be rolled back.
5198 softdep_fsync_mountdev(vp)
5201 struct buf *bp, *nbp;
5202 struct worklist *wk;
5205 if (!vn_isdisk(vp, NULL))
5206 panic("softdep_fsync_mountdev: vnode not a disk");
5211 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
5213 * If it is already scheduled, skip to the next buffer.
5215 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
5218 if ((bp->b_flags & B_DELWRI) == 0)
5219 panic("softdep_fsync_mountdev: not dirty");
5221 * We are only interested in bitmaps with outstanding
5224 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
5225 wk->wk_type != D_BMSAFEMAP ||
5226 (bp->b_vflags & BV_BKGRDINPROG)) {
5242 * This routine is called when we are trying to synchronously flush a
5243 * file. This routine must eliminate any filesystem metadata dependencies
5244 * so that the syncing routine can succeed by pushing the dirty blocks
5245 * associated with the file. If any I/O errors occur, they are returned.
5248 softdep_sync_metadata(struct vnode *vp)
5250 struct pagedep *pagedep;
5251 struct allocdirect *adp;
5252 struct allocindir *aip;
5253 struct buf *bp, *nbp;
5254 struct worklist *wk;
5256 int i, error, waitfor;
5258 if (!DOINGSOFTDEP(vp))
5261 * Ensure that any direct block dependencies have been cleared.
5264 if ((error = flush_inodedep_deps(vp->v_mount, VTOI(vp)->i_number))) {
5270 * For most files, the only metadata dependencies are the
5271 * cylinder group maps that allocate their inode or blocks.
5272 * The block allocation dependencies can be found by traversing
5273 * the dependency lists for any buffers that remain on their
5274 * dirty buffer list. The inode allocation dependency will
5275 * be resolved when the inode is updated with MNT_WAIT.
5276 * This work is done in two passes. The first pass grabs most
5277 * of the buffers and begins asynchronously writing them. The
5278 * only way to wait for these asynchronous writes is to sleep
5279 * on the filesystem vnode which may stay busy for a long time
5280 * if the filesystem is active. So, instead, we make a second
5281 * pass over the dependencies blocking on each write. In the
5282 * usual case we will be blocking against a write that we
5283 * initiated, so when it is done the dependency will have been
5284 * resolved. Thus the second pass is expected to end quickly.
5286 waitfor = MNT_NOWAIT;
5291 * We must wait for any I/O in progress to finish so that
5292 * all potential buffers on the dirty list will be visible.
5296 while ((bp = TAILQ_FIRST(&bo->bo_dirty.bv_hd)) != NULL) {
5297 bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT);
5305 /* While syncing snapshots, we must allow recursive lookups */
5309 * As we hold the buffer locked, none of its dependencies
5312 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5313 switch (wk->wk_type) {
5316 adp = WK_ALLOCDIRECT(wk);
5317 if (adp->ad_state & DEPCOMPLETE)
5320 nbp = getdirtybuf(nbp, &lk, waitfor);
5324 if (waitfor == MNT_NOWAIT) {
5326 } else if ((error = bwrite(nbp)) != 0) {
5333 aip = WK_ALLOCINDIR(wk);
5334 if (aip->ai_state & DEPCOMPLETE)
5337 nbp = getdirtybuf(nbp, &lk, waitfor);
5341 if (waitfor == MNT_NOWAIT) {
5343 } else if ((error = bwrite(nbp)) != 0) {
5352 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
5353 if (aip->ai_state & DEPCOMPLETE)
5356 nbp = getdirtybuf(nbp, &lk, MNT_WAIT);
5360 if ((error = bwrite(nbp)) != 0) {
5369 if ((error = flush_inodedep_deps(wk->wk_mp,
5370 WK_INODEDEP(wk)->id_ino)) != 0) {
5378 * We are trying to sync a directory that may
5379 * have dependencies on both its own metadata
5380 * and/or dependencies on the inodes of any
5381 * recently allocated files. We walk its diradd
5382 * lists pushing out the associated inode.
5384 pagedep = WK_PAGEDEP(wk);
5385 for (i = 0; i < DAHASHSZ; i++) {
5386 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
5389 flush_pagedep_deps(vp, wk->wk_mp,
5390 &pagedep->pd_diraddhd[i]))) {
5399 * This case should never happen if the vnode has
5400 * been properly sync'ed. However, if this function
5401 * is used at a place where the vnode has not yet
5402 * been sync'ed, this dependency can show up. So,
5403 * rather than panic, just flush it.
5405 nbp = WK_MKDIR(wk)->md_buf;
5406 nbp = getdirtybuf(nbp, &lk, waitfor);
5410 if (waitfor == MNT_NOWAIT) {
5412 } else if ((error = bwrite(nbp)) != 0) {
5420 * This case should never happen if the vnode has
5421 * been properly sync'ed. However, if this function
5422 * is used at a place where the vnode has not yet
5423 * been sync'ed, this dependency can show up. So,
5424 * rather than panic, just flush it.
5426 nbp = WK_BMSAFEMAP(wk)->sm_buf;
5427 nbp = getdirtybuf(nbp, &lk, waitfor);
5431 if (waitfor == MNT_NOWAIT) {
5433 } else if ((error = bwrite(nbp)) != 0) {
5440 panic("softdep_sync_metadata: Unknown type %s",
5441 TYPENAME(wk->wk_type));
5445 /* We reach here only in error and unlocked */
5447 panic("softdep_sync_metadata: zero error");
5454 while ((nbp = TAILQ_NEXT(bp, b_bobufs)) != NULL) {
5455 nbp = getdirtybuf(nbp, BO_MTX(bo), MNT_WAIT);
5467 * The brief unlock is to allow any pent up dependency
5468 * processing to be done. Then proceed with the second pass.
5470 if (waitfor == MNT_NOWAIT) {
5476 * If we have managed to get rid of all the dirty buffers,
5477 * then we are done. For certain directories and block
5478 * devices, we may need to do further work.
5480 * We must wait for any I/O in progress to finish so that
5481 * all potential buffers on the dirty list will be visible.
5490 * Flush the dependencies associated with an inodedep.
5491 * Called with splbio blocked.
5494 flush_inodedep_deps(mp, ino)
5498 struct inodedep *inodedep;
5502 * This work is done in two passes. The first pass grabs most
5503 * of the buffers and begins asynchronously writing them. The
5504 * only way to wait for these asynchronous writes is to sleep
5505 * on the filesystem vnode which may stay busy for a long time
5506 * if the filesystem is active. So, instead, we make a second
5507 * pass over the dependencies blocking on each write. In the
5508 * usual case we will be blocking against a write that we
5509 * initiated, so when it is done the dependency will have been
5510 * resolved. Thus the second pass is expected to end quickly.
5511 * We give a brief window at the top of the loop to allow
5512 * any pending I/O to complete.
5514 for (error = 0, waitfor = MNT_NOWAIT; ; ) {
5519 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
5521 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
5522 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
5523 flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
5524 flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
5527 * If pass2, we are done, otherwise do pass 2.
5529 if (waitfor == MNT_WAIT)
5534 * Try freeing inodedep in case all dependencies have been removed.
5536 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
5537 (void) free_inodedep(inodedep);
5542 * Flush an inode dependency list.
5543 * Called with splbio blocked.
5546 flush_deplist(listhead, waitfor, errorp)
5547 struct allocdirectlst *listhead;
5551 struct allocdirect *adp;
5554 mtx_assert(&lk, MA_OWNED);
5555 TAILQ_FOREACH(adp, listhead, ad_next) {
5556 if (adp->ad_state & DEPCOMPLETE)
5559 bp = getdirtybuf(bp, &lk, waitfor);
5561 if (waitfor == MNT_NOWAIT)
5566 if (waitfor == MNT_NOWAIT) {
5568 } else if ((*errorp = bwrite(bp)) != 0) {
5579 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
5580 * Called with splbio blocked.
5583 flush_pagedep_deps(pvp, mp, diraddhdp)
5586 struct diraddhd *diraddhdp;
5588 struct inodedep *inodedep;
5589 struct ufsmount *ump;
5596 struct worklist *wk;
5599 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
5601 * Flush ourselves if this directory entry
5602 * has a MKDIR_PARENT dependency.
5604 if (dap->da_state & MKDIR_PARENT) {
5606 if ((error = ffs_update(pvp, 1)) != 0)
5610 * If that cleared dependencies, go on to next.
5612 if (dap != LIST_FIRST(diraddhdp))
5614 if (dap->da_state & MKDIR_PARENT)
5615 panic("flush_pagedep_deps: MKDIR_PARENT");
5618 * A newly allocated directory must have its "." and
5619 * ".." entries written out before its name can be
5620 * committed in its parent. We do not want or need
5621 * the full semantics of a synchronous ffs_syncvnode as
5622 * that may end up here again, once for each directory
5623 * level in the filesystem. Instead, we push the blocks
5624 * and wait for them to clear. We have to fsync twice
5625 * because the first call may choose to defer blocks
5626 * that still have dependencies, but deferral will
5627 * happen at most once.
5629 inum = dap->da_newinum;
5630 if (dap->da_state & MKDIR_BODY) {
5632 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp,
5635 if ((error=ffs_syncvnode(vp, MNT_NOWAIT)) ||
5636 (error=ffs_syncvnode(vp, MNT_NOWAIT))) {
5644 * If first block is still dirty with a D_MKDIR
5645 * dependency then it needs to be written now.
5649 bp = gbincore(bo, 0);
5651 break; /* First block not present */
5652 error = BUF_LOCK(bp,
5658 if (error == ENOLCK)
5659 continue; /* Slept, retry */
5662 if ((bp->b_flags & B_DELWRI) == 0) {
5664 break; /* Buffer not dirty */
5666 for (wk = LIST_FIRST(&bp->b_dep);
5668 wk = LIST_NEXT(wk, wk_list))
5669 if (wk->wk_type == D_MKDIR)
5672 BUF_UNLOCK(bp); /* Dependency gone */
5675 * D_MKDIR dependency remains,
5676 * must write buffer to stable
5689 break; /* Flushing of first block failed */
5692 * If that cleared dependencies, go on to next.
5694 if (dap != LIST_FIRST(diraddhdp))
5696 if (dap->da_state & MKDIR_BODY)
5697 panic("flush_pagedep_deps: MKDIR_BODY");
5700 * Flush the inode on which the directory entry depends.
5701 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
5702 * the only remaining dependency is that the updated inode
5703 * count must get pushed to disk. The inode has already
5704 * been pushed into its inode buffer (via VOP_UPDATE) at
5705 * the time of the reference count change. So we need only
5706 * locate that buffer, ensure that there will be no rollback
5707 * caused by a bitmap dependency, then write the inode buffer.
5710 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
5711 panic("flush_pagedep_deps: lost inode");
5713 * If the inode still has bitmap dependencies,
5714 * push them to disk.
5716 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5717 bp = inodedep->id_buf;
5718 bp = getdirtybuf(bp, &lk, MNT_WAIT);
5722 if ((error = bwrite(bp)) != 0)
5725 if (dap != LIST_FIRST(diraddhdp))
5729 * If the inode is still sitting in a buffer waiting
5730 * to be written, push it to disk.
5733 if ((error = bread(ump->um_devvp,
5734 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
5735 (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) {
5739 if ((error = bwrite(bp)) != 0)
5743 * If we have failed to get rid of all the dependencies
5744 * then something is seriously wrong.
5746 if (dap == LIST_FIRST(diraddhdp))
5747 panic("flush_pagedep_deps: flush failed");
5755 * A large burst of file addition or deletion activity can drive the
5756 * memory load excessively high. First attempt to slow things down
5757 * using the techniques below. If that fails, this routine requests
5758 * the offending operations to fall back to running synchronously
5759 * until the memory load returns to a reasonable level.
5762 softdep_slowdown(vp)
5765 int max_softdeps_hard;
5768 max_softdeps_hard = max_softdeps * 11 / 10;
5769 if (num_dirrem < max_softdeps_hard / 2 &&
5770 num_inodedep < max_softdeps_hard &&
5771 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps) {
5775 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps)
5777 stat_sync_limit_hit += 1;
5783 * Called by the allocation routines when they are about to fail
5784 * in the hope that we can free up some disk space.
5786 * First check to see if the work list has anything on it. If it has,
5787 * clean up entries until we successfully free some space. Because this
5788 * process holds inodes locked, we cannot handle any remove requests
5789 * that might block on a locked inode as that could lead to deadlock.
5790 * If the worklist yields no free space, encourage the syncer daemon
5791 * to help us. In no event will we try for longer than tickdelay seconds.
5794 softdep_request_cleanup(fs, vp)
5798 struct ufsmount *ump;
5800 ufs2_daddr_t needed;
5803 ump = VTOI(vp)->i_ump;
5804 mtx_assert(UFS_MTX(ump), MA_OWNED);
5805 needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize;
5806 starttime = time_second + tickdelay;
5808 * If we are being called because of a process doing a
5809 * copy-on-write, then it is not safe to update the vnode
5810 * as we may recurse into the copy-on-write routine.
5812 if (!(curthread->td_pflags & TDP_COWINPROGRESS)) {
5814 error = ffs_update(vp, 1);
5819 while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) {
5820 if (time_second > starttime)
5824 if (ump->softdep_on_worklist > 0 &&
5825 process_worklist_item(UFSTOVFS(ump), LK_NOWAIT) != -1) {
5826 stat_worklist_push += 1;
5831 request_cleanup(UFSTOVFS(ump), FLUSH_REMOVE_WAIT);
5839 * If memory utilization has gotten too high, deliberately slow things
5840 * down and speed up the I/O processing.
5842 extern struct thread *syncertd;
5844 request_cleanup(mp, resource)
5848 struct thread *td = curthread;
5849 struct ufsmount *ump;
5851 mtx_assert(&lk, MA_OWNED);
5853 * We never hold up the filesystem syncer or buf daemon.
5855 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
5859 * First check to see if the work list has gotten backlogged.
5860 * If it has, co-opt this process to help clean up two entries.
5861 * Because this process may hold inodes locked, we cannot
5862 * handle any remove requests that might block on a locked
5863 * inode as that could lead to deadlock. We set TDP_SOFTDEP
5864 * to avoid recursively processing the worklist.
5866 if (ump->softdep_on_worklist > max_softdeps / 10) {
5867 td->td_pflags |= TDP_SOFTDEP;
5868 process_worklist_item(mp, LK_NOWAIT);
5869 process_worklist_item(mp, LK_NOWAIT);
5870 td->td_pflags &= ~TDP_SOFTDEP;
5871 stat_worklist_push += 2;
5875 * Next, we attempt to speed up the syncer process. If that
5876 * is successful, then we allow the process to continue.
5878 if (softdep_speedup() && resource != FLUSH_REMOVE_WAIT)
5881 * If we are resource constrained on inode dependencies, try
5882 * flushing some dirty inodes. Otherwise, we are constrained
5883 * by file deletions, so try accelerating flushes of directories
5884 * with removal dependencies. We would like to do the cleanup
5885 * here, but we probably hold an inode locked at this point and
5886 * that might deadlock against one that we try to clean. So,
5887 * the best that we can do is request the syncer daemon to do
5888 * the cleanup for us.
5893 stat_ino_limit_push += 1;
5894 req_clear_inodedeps += 1;
5895 stat_countp = &stat_ino_limit_hit;
5899 case FLUSH_REMOVE_WAIT:
5900 stat_blk_limit_push += 1;
5901 req_clear_remove += 1;
5902 stat_countp = &stat_blk_limit_hit;
5906 panic("request_cleanup: unknown type");
5909 * Hopefully the syncer daemon will catch up and awaken us.
5910 * We wait at most tickdelay before proceeding in any case.
5913 if (callout_pending(&softdep_callout) == FALSE)
5914 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
5917 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
5923 * Awaken processes pausing in request_cleanup and clear proc_waiting
5924 * to indicate that there is no longer a timer running.
5932 * The callout_ API has acquired mtx and will hold it around this
5936 wakeup_one(&proc_waiting);
5937 if (proc_waiting > 0)
5938 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2,
5943 * Flush out a directory with at least one removal dependency in an effort to
5944 * reduce the number of dirrem, freefile, and freeblks dependency structures.
5950 struct pagedep_hashhead *pagedephd;
5951 struct pagedep *pagedep;
5952 static int next = 0;
5959 mtx_assert(&lk, MA_OWNED);
5961 for (cnt = 0; cnt < pagedep_hash; cnt++) {
5962 pagedephd = &pagedep_hashtbl[next++];
5963 if (next >= pagedep_hash)
5965 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
5966 if (LIST_EMPTY(&pagedep->pd_dirremhd))
5968 mp = pagedep->pd_list.wk_mp;
5969 ino = pagedep->pd_ino;
5970 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5973 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
5974 FFSV_FORCEINSMQ))) {
5975 softdep_error("clear_remove: vget", error);
5976 vn_finished_write(mp);
5980 if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
5981 softdep_error("clear_remove: fsync", error);
5987 vn_finished_write(mp);
5995 * Clear out a block of dirty inodes in an effort to reduce
5996 * the number of inodedep dependency structures.
6002 struct inodedep_hashhead *inodedephd;
6003 struct inodedep *inodedep;
6004 static int next = 0;
6009 ino_t firstino, lastino, ino;
6011 mtx_assert(&lk, MA_OWNED);
6013 * Pick a random inode dependency to be cleared.
6014 * We will then gather up all the inodes in its block
6015 * that have dependencies and flush them out.
6017 for (cnt = 0; cnt < inodedep_hash; cnt++) {
6018 inodedephd = &inodedep_hashtbl[next++];
6019 if (next >= inodedep_hash)
6021 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
6024 if (inodedep == NULL)
6026 fs = inodedep->id_fs;
6027 mp = inodedep->id_list.wk_mp;
6029 * Find the last inode in the block with dependencies.
6031 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
6032 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
6033 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
6036 * Asynchronously push all but the last inode with dependencies.
6037 * Synchronously push the last inode with dependencies to ensure
6038 * that the inode block gets written to free up the inodedeps.
6040 for (ino = firstino; ino <= lastino; ino++) {
6041 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
6043 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
6046 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp,
6047 FFSV_FORCEINSMQ)) != 0) {
6048 softdep_error("clear_inodedeps: vget", error);
6049 vn_finished_write(mp);
6053 if (ino == lastino) {
6054 if ((error = ffs_syncvnode(vp, MNT_WAIT)))
6055 softdep_error("clear_inodedeps: fsync1", error);
6057 if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
6058 softdep_error("clear_inodedeps: fsync2", error);
6059 BO_LOCK(&vp->v_bufobj);
6061 BO_UNLOCK(&vp->v_bufobj);
6064 vn_finished_write(mp);
6070 * Function to determine if the buffer has outstanding dependencies
6071 * that will cause a roll-back if the buffer is written. If wantcount
6072 * is set, return number of dependencies, otherwise just yes or no.
6075 softdep_count_dependencies(bp, wantcount)
6079 struct worklist *wk;
6080 struct inodedep *inodedep;
6081 struct indirdep *indirdep;
6082 struct allocindir *aip;
6083 struct pagedep *pagedep;
6089 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6090 switch (wk->wk_type) {
6093 inodedep = WK_INODEDEP(wk);
6094 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
6095 /* bitmap allocation dependency */
6100 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
6101 /* direct block pointer dependency */
6106 if (TAILQ_FIRST(&inodedep->id_extupdt)) {
6107 /* direct block pointer dependency */
6115 indirdep = WK_INDIRDEP(wk);
6117 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
6118 /* indirect block pointer dependency */
6126 pagedep = WK_PAGEDEP(wk);
6127 for (i = 0; i < DAHASHSZ; i++) {
6129 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
6130 /* directory entry dependency */
6142 /* never a dependency on these blocks */
6146 panic("softdep_check_for_rollback: Unexpected type %s",
6147 TYPENAME(wk->wk_type));
6157 * Acquire exclusive access to a buffer.
6158 * Must be called with a locked mtx parameter.
6159 * Return acquired buffer or NULL on failure.
6162 getdirtybuf(bp, mtx, waitfor)
6169 mtx_assert(mtx, MA_OWNED);
6170 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
6171 if (waitfor != MNT_WAIT)
6173 error = BUF_LOCK(bp,
6174 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx);
6176 * Even if we sucessfully acquire bp here, we have dropped
6177 * mtx, which may violates our guarantee.
6181 else if (error != ENOLCK)
6182 panic("getdirtybuf: inconsistent lock: %d", error);
6186 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6187 if (mtx == &lk && waitfor == MNT_WAIT) {
6189 BO_LOCK(bp->b_bufobj);
6191 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6192 bp->b_vflags |= BV_BKGRDWAIT;
6193 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj),
6194 PRIBIO | PDROP, "getbuf", 0);
6196 BO_UNLOCK(bp->b_bufobj);
6201 if (waitfor != MNT_WAIT)
6204 * The mtx argument must be bp->b_vp's mutex in
6207 #ifdef DEBUG_VFS_LOCKS
6208 if (bp->b_vp->v_type != VCHR)
6209 ASSERT_BO_LOCKED(bp->b_bufobj);
6211 bp->b_vflags |= BV_BKGRDWAIT;
6212 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0);
6215 if ((bp->b_flags & B_DELWRI) == 0) {
6225 * Check if it is safe to suspend the file system now. On entry,
6226 * the vnode interlock for devvp should be held. Return 0 with
6227 * the mount interlock held if the file system can be suspended now,
6228 * otherwise return EAGAIN with the mount interlock held.
6231 softdep_check_suspend(struct mount *mp,
6232 struct vnode *devvp,
6234 int softdep_accdeps,
6235 int secondary_writes,
6236 int secondary_accwrites)
6239 struct ufsmount *ump;
6243 bo = &devvp->v_bufobj;
6244 ASSERT_BO_LOCKED(bo);
6247 if (!TRY_ACQUIRE_LOCK(&lk)) {
6255 if (mp->mnt_secondary_writes != 0) {
6258 msleep(&mp->mnt_secondary_writes,
6260 (PUSER - 1) | PDROP, "secwr", 0);
6268 * Reasons for needing more work before suspend:
6269 * - Dirty buffers on devvp.
6270 * - Softdep activity occurred after start of vnode sync loop
6271 * - Secondary writes occurred after start of vnode sync loop
6274 if (bo->bo_numoutput > 0 ||
6275 bo->bo_dirty.bv_cnt > 0 ||
6276 softdep_deps != 0 ||
6277 ump->softdep_deps != 0 ||
6278 softdep_accdeps != ump->softdep_accdeps ||
6279 secondary_writes != 0 ||
6280 mp->mnt_secondary_writes != 0 ||
6281 secondary_accwrites != mp->mnt_secondary_accwrites)
6290 * Get the number of dependency structures for the file system, both
6291 * the current number and the total number allocated. These will
6292 * later be used to detect that softdep processing has occurred.
6295 softdep_get_depcounts(struct mount *mp,
6297 int *softdep_accdepsp)
6299 struct ufsmount *ump;
6303 *softdep_depsp = ump->softdep_deps;
6304 *softdep_accdepsp = ump->softdep_accdeps;
6309 * Wait for pending output on a vnode to complete.
6310 * Must be called with vnode lock and interlock locked.
6312 * XXX: Should just be a call to bufobj_wwait().
6321 ASSERT_VOP_LOCKED(vp, "drain_output");
6322 ASSERT_BO_LOCKED(bo);
6324 while (bo->bo_numoutput) {
6325 bo->bo_flag |= BO_WWAIT;
6326 msleep((caddr_t)&bo->bo_numoutput,
6327 BO_MTX(bo), PRIBIO + 1, "drainvp", 0);
6332 * Called whenever a buffer that is being invalidated or reallocated
6333 * contains dependencies. This should only happen if an I/O error has
6334 * occurred. The routine is called with the buffer locked.
6337 softdep_deallocate_dependencies(bp)
6341 if ((bp->b_ioflags & BIO_ERROR) == 0)
6342 panic("softdep_deallocate_dependencies: dangling deps");
6343 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
6344 panic("softdep_deallocate_dependencies: unrecovered I/O error");
6348 * Function to handle asynchronous write errors in the filesystem.
6351 softdep_error(func, error)
6356 /* XXX should do something better! */
6357 printf("%s: got error %d while accessing filesystem\n", func, error);
6362 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps)
6364 struct inodedep_hashhead *inodedephd;
6365 struct inodedep *inodedep;
6369 fs = have_addr ? (struct fs *)addr : NULL;
6370 for (cnt = 0; cnt < inodedep_hash; cnt++) {
6371 inodedephd = &inodedep_hashtbl[cnt];
6372 LIST_FOREACH(inodedep, inodedephd, id_hash) {
6373 if (fs != NULL && fs != inodedep->id_fs)
6375 db_printf("%p fs %p st %x ino %jd inoblk %jd\n",
6376 inodedep, inodedep->id_fs, inodedep->id_state,
6377 (intmax_t)inodedep->id_ino,
6378 (intmax_t)fsbtodb(inodedep->id_fs,
6379 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)));
6386 #endif /* SOFTUPDATES */