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
39 * $FreeBSD: src/sys/ufs/ffs/ffs_softdep.c,v 1.57.2.11 2002/02/05 18:46:53 dillon Exp $
40 * $DragonFly: src/sys/vfs/ufs/ffs_softdep.c,v 1.32 2005/12/05 06:13:16 dillon Exp $
44 * For now we want the safety net that the DIAGNOSTIC and DEBUG flags provide.
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/systm.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
60 #include <sys/syslog.h>
61 #include <sys/vnode.h>
64 #include <machine/inttypes.h>
71 #include "ffs_extern.h"
72 #include "ufs_extern.h"
74 #include <sys/thread2.h>
77 * These definitions need to be adapted to the system to which
78 * this file is being ported.
81 * malloc types defined for the softdep system.
83 MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies");
84 MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies");
85 MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation");
86 MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map");
87 MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode");
88 MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies");
89 MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block");
90 MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode");
91 MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode");
92 MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated");
93 MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry");
94 MALLOC_DEFINE(M_MKDIR, "mkdir","New directory");
95 MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted");
97 #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE)
102 #define D_BMSAFEMAP 3
103 #define D_ALLOCDIRECT 4
105 #define D_ALLOCINDIR 6
112 #define D_LAST D_DIRREM
115 * translate from workitem type to memory type
116 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
118 static struct malloc_type *memtype[] = {
134 #define DtoM(type) (memtype[type])
137 * Names of malloc types.
139 #define TYPENAME(type) \
140 ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???")
142 * End system adaptaion definitions.
146 * Internal function prototypes.
148 static void softdep_error(char *, int);
149 static void drain_output(struct vnode *, int);
150 static int getdirtybuf(struct buf **, int);
151 static void clear_remove(struct thread *);
152 static void clear_inodedeps(struct thread *);
153 static int flush_pagedep_deps(struct vnode *, struct mount *,
155 static int flush_inodedep_deps(struct fs *, ino_t);
156 static int handle_written_filepage(struct pagedep *, struct buf *);
157 static void diradd_inode_written(struct diradd *, struct inodedep *);
158 static int handle_written_inodeblock(struct inodedep *, struct buf *);
159 static void handle_allocdirect_partdone(struct allocdirect *);
160 static void handle_allocindir_partdone(struct allocindir *);
161 static void initiate_write_filepage(struct pagedep *, struct buf *);
162 static void handle_written_mkdir(struct mkdir *, int);
163 static void initiate_write_inodeblock(struct inodedep *, struct buf *);
164 static void handle_workitem_freefile(struct freefile *);
165 static void handle_workitem_remove(struct dirrem *);
166 static struct dirrem *newdirrem(struct buf *, struct inode *,
167 struct inode *, int, struct dirrem **);
168 static void free_diradd(struct diradd *);
169 static void free_allocindir(struct allocindir *, struct inodedep *);
170 static int indir_trunc (struct inode *, ufs_daddr_t, int, ufs_lbn_t,
172 static void deallocate_dependencies(struct buf *, struct inodedep *);
173 static void free_allocdirect(struct allocdirectlst *,
174 struct allocdirect *, int);
175 static int check_inode_unwritten(struct inodedep *);
176 static int free_inodedep(struct inodedep *);
177 static void handle_workitem_freeblocks(struct freeblks *);
178 static void merge_inode_lists(struct inodedep *);
179 static void setup_allocindir_phase2(struct buf *, struct inode *,
180 struct allocindir *);
181 static struct allocindir *newallocindir(struct inode *, int, ufs_daddr_t,
183 static void handle_workitem_freefrag(struct freefrag *);
184 static struct freefrag *newfreefrag(struct inode *, ufs_daddr_t, long);
185 static void allocdirect_merge(struct allocdirectlst *,
186 struct allocdirect *, struct allocdirect *);
187 static struct bmsafemap *bmsafemap_lookup(struct buf *);
188 static int newblk_lookup(struct fs *, ufs_daddr_t, int,
190 static int inodedep_lookup(struct fs *, ino_t, int, struct inodedep **);
191 static int pagedep_lookup(struct inode *, ufs_lbn_t, int,
193 static void pause_timer(void *);
194 static int request_cleanup(int, int);
195 static int process_worklist_item(struct mount *, int);
196 static void add_to_worklist(struct worklist *);
199 * Exported softdep operations.
201 static void softdep_disk_io_initiation(struct buf *);
202 static void softdep_disk_write_complete(struct buf *);
203 static void softdep_deallocate_dependencies(struct buf *);
204 static int softdep_fsync(struct vnode *);
205 static int softdep_process_worklist(struct mount *);
206 static void softdep_move_dependencies(struct buf *, struct buf *);
207 static int softdep_count_dependencies(struct buf *bp, int);
209 static struct bio_ops softdep_bioops = {
210 softdep_disk_io_initiation, /* io_start */
211 softdep_disk_write_complete, /* io_complete */
212 softdep_deallocate_dependencies, /* io_deallocate */
213 softdep_fsync, /* io_fsync */
214 softdep_process_worklist, /* io_sync */
215 softdep_move_dependencies, /* io_movedeps */
216 softdep_count_dependencies, /* io_countdeps */
220 * Locking primitives.
222 * For a uniprocessor, all we need to do is protect against disk
223 * interrupts. For a multiprocessor, this lock would have to be
224 * a mutex. A single mutex is used throughout this file, though
225 * finer grain locking could be used if contention warranted it.
227 * For a multiprocessor, the sleep call would accept a lock and
228 * release it after the sleep processing was complete. In a uniprocessor
229 * implementation there is no such interlock, so we simple mark
230 * the places where it needs to be done with the `interlocked' form
231 * of the lock calls. Since the uniprocessor sleep already interlocks
232 * the spl, there is nothing that really needs to be done.
234 #ifndef /* NOT */ DEBUG
235 static struct lockit {
237 #define ACQUIRE_LOCK(lk) crit_enter_id("softupdates");
238 #define FREE_LOCK(lk) crit_exit_id("softupdates");
241 #define NOHOLDER ((struct thread *)-1)
242 #define SPECIAL_FLAG ((struct thread *)-2)
243 static struct lockit {
245 struct thread *lkt_held;
246 } lk = { 0, NOHOLDER };
249 static void acquire_lock(struct lockit *);
250 static void free_lock(struct lockit *);
251 void softdep_panic(char *);
253 #define ACQUIRE_LOCK(lk) acquire_lock(lk)
254 #define FREE_LOCK(lk) free_lock(lk)
262 if (lk->lkt_held != NOHOLDER) {
263 holder = lk->lkt_held;
265 if (holder == curthread)
266 panic("softdep_lock: locking against myself");
268 panic("softdep_lock: lock held by %p", holder);
270 crit_enter_id("softupdates");
271 lk->lkt_held = curthread;
280 if (lk->lkt_held == NOHOLDER)
281 panic("softdep_unlock: lock not held");
282 lk->lkt_held = NOHOLDER;
283 crit_exit_id("softupdates");
287 * Function to release soft updates lock and panic.
294 if (lk.lkt_held != NOHOLDER)
300 static int interlocked_sleep(struct lockit *, int, void *, int,
304 * When going to sleep, we must save our SPL so that it does
305 * not get lost if some other process uses the lock while we
306 * are sleeping. We restore it after we have slept. This routine
307 * wraps the interlocking with functions that sleep. The list
308 * below enumerates the available set of operations.
315 interlocked_sleep(lk, op, ident, flags, wmesg, timo)
328 if (lk->lkt_held == NOHOLDER)
329 panic("interlocked_sleep: lock not held");
330 lk->lkt_held = NOHOLDER;
334 retval = tsleep(ident, flags, wmesg, timo);
337 retval = BUF_LOCK((struct buf *)ident, flags);
340 panic("interlocked_sleep: unknown operation");
343 if (lk->lkt_held != NOHOLDER) {
344 holder = lk->lkt_held;
346 if (holder == curthread)
347 panic("interlocked_sleep: locking against self");
349 panic("interlocked_sleep: lock held by %p", holder);
351 lk->lkt_held = curthread;
359 * Place holder for real semaphores.
368 static void sema_init(struct sema *, char *, int, int);
369 static int sema_get(struct sema *, struct lockit *);
370 static void sema_release(struct sema *);
373 sema_init(semap, name, prio, timo)
379 semap->holder = NOHOLDER;
387 sema_get(semap, interlock)
389 struct lockit *interlock;
392 if (semap->value++ > 0) {
393 if (interlock != NULL) {
394 interlocked_sleep(interlock, SLEEP, (caddr_t)semap,
395 semap->prio, semap->name, semap->timo);
396 FREE_LOCK(interlock);
398 tsleep((caddr_t)semap, semap->prio, semap->name,
403 semap->holder = curthread;
404 if (interlock != NULL)
405 FREE_LOCK(interlock);
414 if (semap->value <= 0 || semap->holder != curthread) {
415 if (lk.lkt_held != NOHOLDER)
417 panic("sema_release: not held");
419 if (--semap->value > 0) {
423 semap->holder = NOHOLDER;
427 * Worklist queue management.
428 * These routines require that the lock be held.
430 #ifndef /* NOT */ DEBUG
431 #define WORKLIST_INSERT(head, item) do { \
432 (item)->wk_state |= ONWORKLIST; \
433 LIST_INSERT_HEAD(head, item, wk_list); \
435 #define WORKLIST_REMOVE(item) do { \
436 (item)->wk_state &= ~ONWORKLIST; \
437 LIST_REMOVE(item, wk_list); \
439 #define WORKITEM_FREE(item, type) FREE(item, DtoM(type))
442 static void worklist_insert(struct workhead *, struct worklist *);
443 static void worklist_remove(struct worklist *);
444 static void workitem_free(struct worklist *, int);
446 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
447 #define WORKLIST_REMOVE(item) worklist_remove(item)
448 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)item, type)
451 worklist_insert(head, item)
452 struct workhead *head;
453 struct worklist *item;
456 if (lk.lkt_held == NOHOLDER)
457 panic("worklist_insert: lock not held");
458 if (item->wk_state & ONWORKLIST) {
460 panic("worklist_insert: already on list");
462 item->wk_state |= ONWORKLIST;
463 LIST_INSERT_HEAD(head, item, wk_list);
467 worklist_remove(item)
468 struct worklist *item;
471 if (lk.lkt_held == NOHOLDER)
472 panic("worklist_remove: lock not held");
473 if ((item->wk_state & ONWORKLIST) == 0) {
475 panic("worklist_remove: not on list");
477 item->wk_state &= ~ONWORKLIST;
478 LIST_REMOVE(item, wk_list);
482 workitem_free(item, type)
483 struct worklist *item;
487 if (item->wk_state & ONWORKLIST) {
488 if (lk.lkt_held != NOHOLDER)
490 panic("workitem_free: still on list");
492 if (item->wk_type != type) {
493 if (lk.lkt_held != NOHOLDER)
495 panic("workitem_free: type mismatch");
497 FREE(item, DtoM(type));
502 * Workitem queue management
504 static struct workhead softdep_workitem_pending;
505 static int num_on_worklist; /* number of worklist items to be processed */
506 static int softdep_worklist_busy; /* 1 => trying to do unmount */
507 static int softdep_worklist_req; /* serialized waiters */
508 static int max_softdeps; /* maximum number of structs before slowdown */
509 static int tickdelay = 2; /* number of ticks to pause during slowdown */
510 static int *stat_countp; /* statistic to count in proc_waiting timeout */
511 static int proc_waiting; /* tracks whether we have a timeout posted */
512 static struct callout handle; /* handle on posted proc_waiting timeout */
513 static struct thread *filesys_syncer; /* proc of filesystem syncer process */
514 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
515 #define FLUSH_INODES 1
516 static int req_clear_remove; /* syncer process flush some freeblks */
517 #define FLUSH_REMOVE 2
521 static int stat_worklist_push; /* number of worklist cleanups */
522 static int stat_blk_limit_push; /* number of times block limit neared */
523 static int stat_ino_limit_push; /* number of times inode limit neared */
524 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
525 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
526 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
527 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
528 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
529 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
530 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
533 #include <sys/sysctl.h>
534 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
535 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
536 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
537 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
538 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
539 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
540 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
541 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
542 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
543 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
544 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
545 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
549 * Add an item to the end of the work queue.
550 * This routine requires that the lock be held.
551 * This is the only routine that adds items to the list.
552 * The following routine is the only one that removes items
553 * and does so in order from first to last.
559 static struct worklist *worklist_tail;
561 if (wk->wk_state & ONWORKLIST) {
562 if (lk.lkt_held != NOHOLDER)
564 panic("add_to_worklist: already on list");
566 wk->wk_state |= ONWORKLIST;
567 if (LIST_FIRST(&softdep_workitem_pending) == NULL)
568 LIST_INSERT_HEAD(&softdep_workitem_pending, wk, wk_list);
570 LIST_INSERT_AFTER(worklist_tail, wk, wk_list);
572 num_on_worklist += 1;
576 * Process that runs once per second to handle items in the background queue.
578 * Note that we ensure that everything is done in the order in which they
579 * appear in the queue. The code below depends on this property to ensure
580 * that blocks of a file are freed before the inode itself is freed. This
581 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
582 * until all the old ones have been purged from the dependency lists.
585 softdep_process_worklist(matchmnt)
586 struct mount *matchmnt;
588 thread_t td = curthread;
589 int matchcnt, loopcount;
593 * Record the process identifier of our caller so that we can give
594 * this process preferential treatment in request_cleanup below.
600 * There is no danger of having multiple processes run this
601 * code, but we have to single-thread it when softdep_flushfiles()
602 * is in operation to get an accurate count of the number of items
603 * related to its mount point that are in the list.
605 if (matchmnt == NULL) {
606 if (softdep_worklist_busy < 0)
608 softdep_worklist_busy += 1;
612 * If requested, try removing inode or removal dependencies.
614 if (req_clear_inodedeps) {
616 req_clear_inodedeps -= 1;
617 wakeup_one(&proc_waiting);
619 if (req_clear_remove) {
621 req_clear_remove -= 1;
622 wakeup_one(&proc_waiting);
625 starttime = time_second;
626 while (num_on_worklist > 0) {
627 matchcnt += process_worklist_item(matchmnt, 0);
630 * If a umount operation wants to run the worklist
633 if (softdep_worklist_req && matchmnt == NULL) {
639 * If requested, try removing inode or removal dependencies.
641 if (req_clear_inodedeps) {
643 req_clear_inodedeps -= 1;
644 wakeup_one(&proc_waiting);
646 if (req_clear_remove) {
648 req_clear_remove -= 1;
649 wakeup_one(&proc_waiting);
652 * We do not generally want to stop for buffer space, but if
653 * we are really being a buffer hog, we will stop and wait.
655 if (loopcount++ % 128 == 0)
658 * Never allow processing to run for more than one
659 * second. Otherwise the other syncer tasks may get
660 * excessively backlogged.
662 if (starttime != time_second && matchmnt == NULL) {
667 if (matchmnt == NULL) {
668 --softdep_worklist_busy;
669 if (softdep_worklist_req && softdep_worklist_busy == 0)
670 wakeup(&softdep_worklist_req);
676 * Process one item on the worklist.
679 process_worklist_item(matchmnt, flags)
680 struct mount *matchmnt;
684 struct dirrem *dirrem;
690 if (matchmnt != NULL)
691 matchfs = VFSTOUFS(matchmnt)->um_fs;
694 * Normally we just process each item on the worklist in order.
695 * However, if we are in a situation where we cannot lock any
696 * inodes, we have to skip over any dirrem requests whose
697 * vnodes are resident and locked.
699 LIST_FOREACH(wk, &softdep_workitem_pending, wk_list) {
700 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
702 dirrem = WK_DIRREM(wk);
703 vp = ufs_ihashlookup(VFSTOUFS(dirrem->dm_mnt)->um_dev,
705 if (vp == NULL || !VOP_ISLOCKED(vp, curthread))
713 num_on_worklist -= 1;
715 switch (wk->wk_type) {
718 /* removal of a directory entry */
719 if (WK_DIRREM(wk)->dm_mnt == matchmnt)
721 handle_workitem_remove(WK_DIRREM(wk));
725 /* releasing blocks and/or fragments from a file */
726 if (WK_FREEBLKS(wk)->fb_fs == matchfs)
728 handle_workitem_freeblocks(WK_FREEBLKS(wk));
732 /* releasing a fragment when replaced as a file grows */
733 if (WK_FREEFRAG(wk)->ff_fs == matchfs)
735 handle_workitem_freefrag(WK_FREEFRAG(wk));
739 /* releasing an inode when its link count drops to 0 */
740 if (WK_FREEFILE(wk)->fx_fs == matchfs)
742 handle_workitem_freefile(WK_FREEFILE(wk));
746 panic("%s_process_worklist: Unknown type %s",
747 "softdep", TYPENAME(wk->wk_type));
754 * Move dependencies from one buffer to another.
757 softdep_move_dependencies(oldbp, newbp)
761 struct worklist *wk, *wktail;
763 if (LIST_FIRST(&newbp->b_dep) != NULL)
764 panic("softdep_move_dependencies: need merge code");
767 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
768 LIST_REMOVE(wk, wk_list);
770 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
772 LIST_INSERT_AFTER(wktail, wk, wk_list);
779 * Purge the work list of all items associated with a particular mount point.
782 softdep_flushfiles(struct mount *oldmnt, int flags, struct thread *td)
788 * Await our turn to clear out the queue, then serialize access.
790 while (softdep_worklist_busy != 0) {
791 softdep_worklist_req += 1;
792 tsleep(&softdep_worklist_req, 0, "softflush", 0);
793 softdep_worklist_req -= 1;
795 softdep_worklist_busy = -1;
797 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0) {
798 softdep_worklist_busy = 0;
799 if (softdep_worklist_req)
800 wakeup(&softdep_worklist_req);
804 * Alternately flush the block device associated with the mount
805 * point and process any dependencies that the flushing
806 * creates. In theory, this loop can happen at most twice,
807 * but we give it a few extra just to be sure.
809 devvp = VFSTOUFS(oldmnt)->um_devvp;
810 for (loopcnt = 10; loopcnt > 0; ) {
811 if (softdep_process_worklist(oldmnt) == 0) {
814 * Do another flush in case any vnodes were brought in
815 * as part of the cleanup operations.
817 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
820 * If we still found nothing to do, we are really done.
822 if (softdep_process_worklist(oldmnt) == 0)
825 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
826 error = VOP_FSYNC(devvp, MNT_WAIT, td);
827 VOP_UNLOCK(devvp, 0, td);
831 softdep_worklist_busy = 0;
832 if (softdep_worklist_req)
833 wakeup(&softdep_worklist_req);
836 * If we are unmounting then it is an error to fail. If we
837 * are simply trying to downgrade to read-only, then filesystem
838 * activity can keep us busy forever, so we just fail with EBUSY.
841 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
842 panic("softdep_flushfiles: looping");
851 * There are three types of structures that can be looked up:
852 * 1) pagedep structures identified by mount point, inode number,
854 * 2) inodedep structures identified by mount point and inode number.
855 * 3) newblk structures identified by mount point and
856 * physical block number.
858 * The "pagedep" and "inodedep" dependency structures are hashed
859 * separately from the file blocks and inodes to which they correspond.
860 * This separation helps when the in-memory copy of an inode or
861 * file block must be replaced. It also obviates the need to access
862 * an inode or file page when simply updating (or de-allocating)
863 * dependency structures. Lookup of newblk structures is needed to
864 * find newly allocated blocks when trying to associate them with
865 * their allocdirect or allocindir structure.
867 * The lookup routines optionally create and hash a new instance when
868 * an existing entry is not found.
870 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
871 #define NODELAY 0x0002 /* cannot do background work */
874 * Structures and routines associated with pagedep caching.
876 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
877 u_long pagedep_hash; /* size of hash table - 1 */
878 #define PAGEDEP_HASH(mp, inum, lbn) \
879 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
881 static struct sema pagedep_in_progress;
884 * Look up a pagedep. Return 1 if found, 0 if not found.
885 * If not found, allocate if DEPALLOC flag is passed.
886 * Found or allocated entry is returned in pagedeppp.
887 * This routine must be called with splbio interrupts blocked.
890 pagedep_lookup(ip, lbn, flags, pagedeppp)
894 struct pagedep **pagedeppp;
896 struct pagedep *pagedep;
897 struct pagedep_hashhead *pagedephd;
902 if (lk.lkt_held == NOHOLDER)
903 panic("pagedep_lookup: lock not held");
905 mp = ITOV(ip)->v_mount;
906 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
908 LIST_FOREACH(pagedep, pagedephd, pd_hash)
909 if (ip->i_number == pagedep->pd_ino &&
910 lbn == pagedep->pd_lbn &&
911 mp == pagedep->pd_mnt)
914 *pagedeppp = pagedep;
917 if ((flags & DEPALLOC) == 0) {
921 if (sema_get(&pagedep_in_progress, &lk) == 0) {
925 MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep), M_PAGEDEP,
927 bzero(pagedep, sizeof(struct pagedep));
928 pagedep->pd_list.wk_type = D_PAGEDEP;
929 pagedep->pd_mnt = mp;
930 pagedep->pd_ino = ip->i_number;
931 pagedep->pd_lbn = lbn;
932 LIST_INIT(&pagedep->pd_dirremhd);
933 LIST_INIT(&pagedep->pd_pendinghd);
934 for (i = 0; i < DAHASHSZ; i++)
935 LIST_INIT(&pagedep->pd_diraddhd[i]);
937 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
938 sema_release(&pagedep_in_progress);
939 *pagedeppp = pagedep;
944 * Structures and routines associated with inodedep caching.
946 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
947 static u_long inodedep_hash; /* size of hash table - 1 */
948 static long num_inodedep; /* number of inodedep allocated */
949 #define INODEDEP_HASH(fs, inum) \
950 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
951 static struct sema inodedep_in_progress;
954 * Look up a inodedep. Return 1 if found, 0 if not found.
955 * If not found, allocate if DEPALLOC flag is passed.
956 * Found or allocated entry is returned in inodedeppp.
957 * This routine must be called with splbio interrupts blocked.
960 inodedep_lookup(fs, inum, flags, inodedeppp)
964 struct inodedep **inodedeppp;
966 struct inodedep *inodedep;
967 struct inodedep_hashhead *inodedephd;
971 if (lk.lkt_held == NOHOLDER)
972 panic("inodedep_lookup: lock not held");
975 inodedephd = INODEDEP_HASH(fs, inum);
977 LIST_FOREACH(inodedep, inodedephd, id_hash)
978 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
981 *inodedeppp = inodedep;
984 if ((flags & DEPALLOC) == 0) {
989 * If we are over our limit, try to improve the situation.
991 if (num_inodedep > max_softdeps && firsttry &&
992 speedup_syncer() == 0 && (flags & NODELAY) == 0 &&
993 request_cleanup(FLUSH_INODES, 1)) {
997 if (sema_get(&inodedep_in_progress, &lk) == 0) {
1002 MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep),
1003 M_INODEDEP, M_SOFTDEP_FLAGS);
1004 inodedep->id_list.wk_type = D_INODEDEP;
1005 inodedep->id_fs = fs;
1006 inodedep->id_ino = inum;
1007 inodedep->id_state = ALLCOMPLETE;
1008 inodedep->id_nlinkdelta = 0;
1009 inodedep->id_savedino = NULL;
1010 inodedep->id_savedsize = -1;
1011 inodedep->id_buf = NULL;
1012 LIST_INIT(&inodedep->id_pendinghd);
1013 LIST_INIT(&inodedep->id_inowait);
1014 LIST_INIT(&inodedep->id_bufwait);
1015 TAILQ_INIT(&inodedep->id_inoupdt);
1016 TAILQ_INIT(&inodedep->id_newinoupdt);
1018 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1019 sema_release(&inodedep_in_progress);
1020 *inodedeppp = inodedep;
1025 * Structures and routines associated with newblk caching.
1027 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1028 u_long newblk_hash; /* size of hash table - 1 */
1029 #define NEWBLK_HASH(fs, inum) \
1030 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1031 static struct sema newblk_in_progress;
1034 * Look up a newblk. Return 1 if found, 0 if not found.
1035 * If not found, allocate if DEPALLOC flag is passed.
1036 * Found or allocated entry is returned in newblkpp.
1039 newblk_lookup(fs, newblkno, flags, newblkpp)
1041 ufs_daddr_t newblkno;
1043 struct newblk **newblkpp;
1045 struct newblk *newblk;
1046 struct newblk_hashhead *newblkhd;
1048 newblkhd = NEWBLK_HASH(fs, newblkno);
1050 LIST_FOREACH(newblk, newblkhd, nb_hash)
1051 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1057 if ((flags & DEPALLOC) == 0) {
1061 if (sema_get(&newblk_in_progress, 0) == 0)
1063 MALLOC(newblk, struct newblk *, sizeof(struct newblk),
1064 M_NEWBLK, M_SOFTDEP_FLAGS);
1065 newblk->nb_state = 0;
1067 newblk->nb_newblkno = newblkno;
1068 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1069 sema_release(&newblk_in_progress);
1075 * Executed during filesystem system initialization before
1076 * mounting any filesystems.
1079 softdep_initialize()
1081 callout_init(&handle);
1082 bioops = softdep_bioops; /* XXX hack */
1084 LIST_INIT(&mkdirlisthd);
1085 LIST_INIT(&softdep_workitem_pending);
1086 max_softdeps = min(desiredvnodes * 8,
1087 M_INODEDEP->ks_limit / (2 * sizeof(struct inodedep)));
1088 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1090 sema_init(&pagedep_in_progress, "pagedep", 0, 0);
1091 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1092 sema_init(&inodedep_in_progress, "inodedep", 0, 0);
1093 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1094 sema_init(&newblk_in_progress, "newblk", 0, 0);
1098 * Called at mount time to notify the dependency code that a
1099 * filesystem wishes to use it.
1102 softdep_mount(devvp, mp, fs)
1103 struct vnode *devvp;
1107 struct csum cstotal;
1112 mp->mnt_flag &= ~MNT_ASYNC;
1113 mp->mnt_flag |= MNT_SOFTDEP;
1115 * When doing soft updates, the counters in the
1116 * superblock may have gotten out of sync, so we have
1117 * to scan the cylinder groups and recalculate them.
1119 if (fs->fs_clean != 0)
1121 bzero(&cstotal, sizeof cstotal);
1122 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1123 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1124 fs->fs_cgsize, &bp)) != 0) {
1128 cgp = (struct cg *)bp->b_data;
1129 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1130 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1131 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1132 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1133 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1137 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1138 printf("ffs_mountfs: superblock updated for soft updates\n");
1140 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1145 * Protecting the freemaps (or bitmaps).
1147 * To eliminate the need to execute fsck before mounting a filesystem
1148 * after a power failure, one must (conservatively) guarantee that the
1149 * on-disk copy of the bitmaps never indicate that a live inode or block is
1150 * free. So, when a block or inode is allocated, the bitmap should be
1151 * updated (on disk) before any new pointers. When a block or inode is
1152 * freed, the bitmap should not be updated until all pointers have been
1153 * reset. The latter dependency is handled by the delayed de-allocation
1154 * approach described below for block and inode de-allocation. The former
1155 * dependency is handled by calling the following procedure when a block or
1156 * inode is allocated. When an inode is allocated an "inodedep" is created
1157 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1158 * Each "inodedep" is also inserted into the hash indexing structure so
1159 * that any additional link additions can be made dependent on the inode
1162 * The ufs filesystem maintains a number of free block counts (e.g., per
1163 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1164 * in addition to the bitmaps. These counts are used to improve efficiency
1165 * during allocation and therefore must be consistent with the bitmaps.
1166 * There is no convenient way to guarantee post-crash consistency of these
1167 * counts with simple update ordering, for two main reasons: (1) The counts
1168 * and bitmaps for a single cylinder group block are not in the same disk
1169 * sector. If a disk write is interrupted (e.g., by power failure), one may
1170 * be written and the other not. (2) Some of the counts are located in the
1171 * superblock rather than the cylinder group block. So, we focus our soft
1172 * updates implementation on protecting the bitmaps. When mounting a
1173 * filesystem, we recompute the auxiliary counts from the bitmaps.
1177 * Called just after updating the cylinder group block to allocate an inode.
1180 softdep_setup_inomapdep(bp, ip, newinum)
1181 struct buf *bp; /* buffer for cylgroup block with inode map */
1182 struct inode *ip; /* inode related to allocation */
1183 ino_t newinum; /* new inode number being allocated */
1185 struct inodedep *inodedep;
1186 struct bmsafemap *bmsafemap;
1189 * Create a dependency for the newly allocated inode.
1190 * Panic if it already exists as something is seriously wrong.
1191 * Otherwise add it to the dependency list for the buffer holding
1192 * the cylinder group map from which it was allocated.
1195 if ((inodedep_lookup(ip->i_fs, newinum, DEPALLOC|NODELAY, &inodedep))) {
1197 panic("softdep_setup_inomapdep: found inode");
1199 inodedep->id_buf = bp;
1200 inodedep->id_state &= ~DEPCOMPLETE;
1201 bmsafemap = bmsafemap_lookup(bp);
1202 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1207 * Called just after updating the cylinder group block to
1208 * allocate block or fragment.
1211 softdep_setup_blkmapdep(bp, fs, newblkno)
1212 struct buf *bp; /* buffer for cylgroup block with block map */
1213 struct fs *fs; /* filesystem doing allocation */
1214 ufs_daddr_t newblkno; /* number of newly allocated block */
1216 struct newblk *newblk;
1217 struct bmsafemap *bmsafemap;
1220 * Create a dependency for the newly allocated block.
1221 * Add it to the dependency list for the buffer holding
1222 * the cylinder group map from which it was allocated.
1224 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1225 panic("softdep_setup_blkmapdep: found block");
1227 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp);
1228 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1233 * Find the bmsafemap associated with a cylinder group buffer.
1234 * If none exists, create one. The buffer must be locked when
1235 * this routine is called and this routine must be called with
1236 * splbio interrupts blocked.
1238 static struct bmsafemap *
1239 bmsafemap_lookup(bp)
1242 struct bmsafemap *bmsafemap;
1243 struct worklist *wk;
1246 if (lk.lkt_held == NOHOLDER)
1247 panic("bmsafemap_lookup: lock not held");
1249 LIST_FOREACH(wk, &bp->b_dep, wk_list)
1250 if (wk->wk_type == D_BMSAFEMAP)
1251 return (WK_BMSAFEMAP(wk));
1253 MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1254 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1255 bmsafemap->sm_list.wk_type = D_BMSAFEMAP;
1256 bmsafemap->sm_list.wk_state = 0;
1257 bmsafemap->sm_buf = bp;
1258 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1259 LIST_INIT(&bmsafemap->sm_allocindirhd);
1260 LIST_INIT(&bmsafemap->sm_inodedephd);
1261 LIST_INIT(&bmsafemap->sm_newblkhd);
1263 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1268 * Direct block allocation dependencies.
1270 * When a new block is allocated, the corresponding disk locations must be
1271 * initialized (with zeros or new data) before the on-disk inode points to
1272 * them. Also, the freemap from which the block was allocated must be
1273 * updated (on disk) before the inode's pointer. These two dependencies are
1274 * independent of each other and are needed for all file blocks and indirect
1275 * blocks that are pointed to directly by the inode. Just before the
1276 * "in-core" version of the inode is updated with a newly allocated block
1277 * number, a procedure (below) is called to setup allocation dependency
1278 * structures. These structures are removed when the corresponding
1279 * dependencies are satisfied or when the block allocation becomes obsolete
1280 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1281 * fragment that gets upgraded). All of these cases are handled in
1282 * procedures described later.
1284 * When a file extension causes a fragment to be upgraded, either to a larger
1285 * fragment or to a full block, the on-disk location may change (if the
1286 * previous fragment could not simply be extended). In this case, the old
1287 * fragment must be de-allocated, but not until after the inode's pointer has
1288 * been updated. In most cases, this is handled by later procedures, which
1289 * will construct a "freefrag" structure to be added to the workitem queue
1290 * when the inode update is complete (or obsolete). The main exception to
1291 * this is when an allocation occurs while a pending allocation dependency
1292 * (for the same block pointer) remains. This case is handled in the main
1293 * allocation dependency setup procedure by immediately freeing the
1294 * unreferenced fragments.
1297 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1298 struct inode *ip; /* inode to which block is being added */
1299 ufs_lbn_t lbn; /* block pointer within inode */
1300 ufs_daddr_t newblkno; /* disk block number being added */
1301 ufs_daddr_t oldblkno; /* previous block number, 0 unless frag */
1302 long newsize; /* size of new block */
1303 long oldsize; /* size of new block */
1304 struct buf *bp; /* bp for allocated block */
1306 struct allocdirect *adp, *oldadp;
1307 struct allocdirectlst *adphead;
1308 struct bmsafemap *bmsafemap;
1309 struct inodedep *inodedep;
1310 struct pagedep *pagedep;
1311 struct newblk *newblk;
1313 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1314 M_ALLOCDIRECT, M_SOFTDEP_FLAGS);
1315 bzero(adp, sizeof(struct allocdirect));
1316 adp->ad_list.wk_type = D_ALLOCDIRECT;
1318 adp->ad_newblkno = newblkno;
1319 adp->ad_oldblkno = oldblkno;
1320 adp->ad_newsize = newsize;
1321 adp->ad_oldsize = oldsize;
1322 adp->ad_state = ATTACHED;
1323 if (newblkno == oldblkno)
1324 adp->ad_freefrag = NULL;
1326 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1328 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1329 panic("softdep_setup_allocdirect: lost block");
1332 inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1333 adp->ad_inodedep = inodedep;
1335 if (newblk->nb_state == DEPCOMPLETE) {
1336 adp->ad_state |= DEPCOMPLETE;
1339 bmsafemap = newblk->nb_bmsafemap;
1340 adp->ad_buf = bmsafemap->sm_buf;
1341 LIST_REMOVE(newblk, nb_deps);
1342 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1344 LIST_REMOVE(newblk, nb_hash);
1345 FREE(newblk, M_NEWBLK);
1347 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1348 if (lbn >= NDADDR) {
1349 /* allocating an indirect block */
1350 if (oldblkno != 0) {
1352 panic("softdep_setup_allocdirect: non-zero indir");
1356 * Allocating a direct block.
1358 * If we are allocating a directory block, then we must
1359 * allocate an associated pagedep to track additions and
1362 if ((ip->i_mode & IFMT) == IFDIR &&
1363 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1364 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1367 * The list of allocdirects must be kept in sorted and ascending
1368 * order so that the rollback routines can quickly determine the
1369 * first uncommitted block (the size of the file stored on disk
1370 * ends at the end of the lowest committed fragment, or if there
1371 * are no fragments, at the end of the highest committed block).
1372 * Since files generally grow, the typical case is that the new
1373 * block is to be added at the end of the list. We speed this
1374 * special case by checking against the last allocdirect in the
1375 * list before laboriously traversing the list looking for the
1378 adphead = &inodedep->id_newinoupdt;
1379 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1380 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1381 /* insert at end of list */
1382 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1383 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1384 allocdirect_merge(adphead, adp, oldadp);
1388 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1389 if (oldadp->ad_lbn >= lbn)
1392 if (oldadp == NULL) {
1394 panic("softdep_setup_allocdirect: lost entry");
1396 /* insert in middle of list */
1397 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1398 if (oldadp->ad_lbn == lbn)
1399 allocdirect_merge(adphead, adp, oldadp);
1404 * Replace an old allocdirect dependency with a newer one.
1405 * This routine must be called with splbio interrupts blocked.
1408 allocdirect_merge(adphead, newadp, oldadp)
1409 struct allocdirectlst *adphead; /* head of list holding allocdirects */
1410 struct allocdirect *newadp; /* allocdirect being added */
1411 struct allocdirect *oldadp; /* existing allocdirect being checked */
1413 struct freefrag *freefrag;
1416 if (lk.lkt_held == NOHOLDER)
1417 panic("allocdirect_merge: lock not held");
1419 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1420 newadp->ad_oldsize != oldadp->ad_newsize ||
1421 newadp->ad_lbn >= NDADDR) {
1423 panic("allocdirect_check: old %d != new %d || lbn %ld >= %d",
1424 newadp->ad_oldblkno, oldadp->ad_newblkno, newadp->ad_lbn,
1427 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1428 newadp->ad_oldsize = oldadp->ad_oldsize;
1430 * If the old dependency had a fragment to free or had never
1431 * previously had a block allocated, then the new dependency
1432 * can immediately post its freefrag and adopt the old freefrag.
1433 * This action is done by swapping the freefrag dependencies.
1434 * The new dependency gains the old one's freefrag, and the
1435 * old one gets the new one and then immediately puts it on
1436 * the worklist when it is freed by free_allocdirect. It is
1437 * not possible to do this swap when the old dependency had a
1438 * non-zero size but no previous fragment to free. This condition
1439 * arises when the new block is an extension of the old block.
1440 * Here, the first part of the fragment allocated to the new
1441 * dependency is part of the block currently claimed on disk by
1442 * the old dependency, so cannot legitimately be freed until the
1443 * conditions for the new dependency are fulfilled.
1445 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1446 freefrag = newadp->ad_freefrag;
1447 newadp->ad_freefrag = oldadp->ad_freefrag;
1448 oldadp->ad_freefrag = freefrag;
1450 free_allocdirect(adphead, oldadp, 0);
1454 * Allocate a new freefrag structure if needed.
1456 static struct freefrag *
1457 newfreefrag(ip, blkno, size)
1462 struct freefrag *freefrag;
1468 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1469 panic("newfreefrag: frag size");
1470 MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1471 M_FREEFRAG, M_SOFTDEP_FLAGS);
1472 freefrag->ff_list.wk_type = D_FREEFRAG;
1473 freefrag->ff_state = ip->i_uid & ~ONWORKLIST; /* XXX - used below */
1474 freefrag->ff_inum = ip->i_number;
1475 freefrag->ff_fs = fs;
1476 freefrag->ff_devvp = ip->i_devvp;
1477 freefrag->ff_blkno = blkno;
1478 freefrag->ff_fragsize = size;
1483 * This workitem de-allocates fragments that were replaced during
1484 * file block allocation.
1487 handle_workitem_freefrag(freefrag)
1488 struct freefrag *freefrag;
1492 tip.i_fs = freefrag->ff_fs;
1493 tip.i_devvp = freefrag->ff_devvp;
1494 tip.i_dev = freefrag->ff_devvp->v_rdev;
1495 tip.i_number = freefrag->ff_inum;
1496 tip.i_uid = freefrag->ff_state & ~ONWORKLIST; /* XXX - set above */
1497 ffs_blkfree(&tip, freefrag->ff_blkno, freefrag->ff_fragsize);
1498 FREE(freefrag, M_FREEFRAG);
1502 * Indirect block allocation dependencies.
1504 * The same dependencies that exist for a direct block also exist when
1505 * a new block is allocated and pointed to by an entry in a block of
1506 * indirect pointers. The undo/redo states described above are also
1507 * used here. Because an indirect block contains many pointers that
1508 * may have dependencies, a second copy of the entire in-memory indirect
1509 * block is kept. The buffer cache copy is always completely up-to-date.
1510 * The second copy, which is used only as a source for disk writes,
1511 * contains only the safe pointers (i.e., those that have no remaining
1512 * update dependencies). The second copy is freed when all pointers
1513 * are safe. The cache is not allowed to replace indirect blocks with
1514 * pending update dependencies. If a buffer containing an indirect
1515 * block with dependencies is written, these routines will mark it
1516 * dirty again. It can only be successfully written once all the
1517 * dependencies are removed. The ffs_fsync routine in conjunction with
1518 * softdep_sync_metadata work together to get all the dependencies
1519 * removed so that a file can be successfully written to disk. Three
1520 * procedures are used when setting up indirect block pointer
1521 * dependencies. The division is necessary because of the organization
1522 * of the "balloc" routine and because of the distinction between file
1523 * pages and file metadata blocks.
1527 * Allocate a new allocindir structure.
1529 static struct allocindir *
1530 newallocindir(ip, ptrno, newblkno, oldblkno)
1531 struct inode *ip; /* inode for file being extended */
1532 int ptrno; /* offset of pointer in indirect block */
1533 ufs_daddr_t newblkno; /* disk block number being added */
1534 ufs_daddr_t oldblkno; /* previous block number, 0 if none */
1536 struct allocindir *aip;
1538 MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1539 M_ALLOCINDIR, M_SOFTDEP_FLAGS);
1540 bzero(aip, sizeof(struct allocindir));
1541 aip->ai_list.wk_type = D_ALLOCINDIR;
1542 aip->ai_state = ATTACHED;
1543 aip->ai_offset = ptrno;
1544 aip->ai_newblkno = newblkno;
1545 aip->ai_oldblkno = oldblkno;
1546 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1551 * Called just before setting an indirect block pointer
1552 * to a newly allocated file page.
1555 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
1556 struct inode *ip; /* inode for file being extended */
1557 ufs_lbn_t lbn; /* allocated block number within file */
1558 struct buf *bp; /* buffer with indirect blk referencing page */
1559 int ptrno; /* offset of pointer in indirect block */
1560 ufs_daddr_t newblkno; /* disk block number being added */
1561 ufs_daddr_t oldblkno; /* previous block number, 0 if none */
1562 struct buf *nbp; /* buffer holding allocated page */
1564 struct allocindir *aip;
1565 struct pagedep *pagedep;
1567 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
1570 * If we are allocating a directory page, then we must
1571 * allocate an associated pagedep to track additions and
1574 if ((ip->i_mode & IFMT) == IFDIR &&
1575 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1576 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
1577 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1579 setup_allocindir_phase2(bp, ip, aip);
1583 * Called just before setting an indirect block pointer to a
1584 * newly allocated indirect block.
1587 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
1588 struct buf *nbp; /* newly allocated indirect block */
1589 struct inode *ip; /* inode for file being extended */
1590 struct buf *bp; /* indirect block referencing allocated block */
1591 int ptrno; /* offset of pointer in indirect block */
1592 ufs_daddr_t newblkno; /* disk block number being added */
1594 struct allocindir *aip;
1596 aip = newallocindir(ip, ptrno, newblkno, 0);
1598 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1600 setup_allocindir_phase2(bp, ip, aip);
1604 * Called to finish the allocation of the "aip" allocated
1605 * by one of the two routines above.
1608 setup_allocindir_phase2(bp, ip, aip)
1609 struct buf *bp; /* in-memory copy of the indirect block */
1610 struct inode *ip; /* inode for file being extended */
1611 struct allocindir *aip; /* allocindir allocated by the above routines */
1613 struct worklist *wk;
1614 struct indirdep *indirdep, *newindirdep;
1615 struct bmsafemap *bmsafemap;
1616 struct allocindir *oldaip;
1617 struct freefrag *freefrag;
1618 struct newblk *newblk;
1620 if (bp->b_lblkno >= 0)
1621 panic("setup_allocindir_phase2: not indir blk");
1622 for (indirdep = NULL, newindirdep = NULL; ; ) {
1624 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
1625 if (wk->wk_type != D_INDIRDEP)
1627 indirdep = WK_INDIRDEP(wk);
1630 if (indirdep == NULL && newindirdep) {
1631 indirdep = newindirdep;
1632 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
1637 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
1639 panic("setup_allocindir: lost block");
1641 if (newblk->nb_state == DEPCOMPLETE) {
1642 aip->ai_state |= DEPCOMPLETE;
1645 bmsafemap = newblk->nb_bmsafemap;
1646 aip->ai_buf = bmsafemap->sm_buf;
1647 LIST_REMOVE(newblk, nb_deps);
1648 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
1651 LIST_REMOVE(newblk, nb_hash);
1652 FREE(newblk, M_NEWBLK);
1653 aip->ai_indirdep = indirdep;
1655 * Check to see if there is an existing dependency
1656 * for this block. If there is, merge the old
1657 * dependency into the new one.
1659 if (aip->ai_oldblkno == 0)
1663 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
1664 if (oldaip->ai_offset == aip->ai_offset)
1666 if (oldaip != NULL) {
1667 if (oldaip->ai_newblkno != aip->ai_oldblkno) {
1669 panic("setup_allocindir_phase2: blkno");
1671 aip->ai_oldblkno = oldaip->ai_oldblkno;
1672 freefrag = oldaip->ai_freefrag;
1673 oldaip->ai_freefrag = aip->ai_freefrag;
1674 aip->ai_freefrag = freefrag;
1675 free_allocindir(oldaip, NULL);
1677 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
1678 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)
1679 [aip->ai_offset] = aip->ai_oldblkno;
1684 * Avoid any possibility of data corruption by
1685 * ensuring that our old version is thrown away.
1687 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
1688 brelse(newindirdep->ir_savebp);
1689 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
1693 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
1694 M_INDIRDEP, M_SOFTDEP_FLAGS);
1695 newindirdep->ir_list.wk_type = D_INDIRDEP;
1696 newindirdep->ir_state = ATTACHED;
1697 LIST_INIT(&newindirdep->ir_deplisthd);
1698 LIST_INIT(&newindirdep->ir_donehd);
1699 if (bp->b_blkno == bp->b_lblkno) {
1700 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
1703 newindirdep->ir_savebp =
1704 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0);
1705 BUF_KERNPROC(newindirdep->ir_savebp);
1706 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
1711 * Block de-allocation dependencies.
1713 * When blocks are de-allocated, the on-disk pointers must be nullified before
1714 * the blocks are made available for use by other files. (The true
1715 * requirement is that old pointers must be nullified before new on-disk
1716 * pointers are set. We chose this slightly more stringent requirement to
1717 * reduce complexity.) Our implementation handles this dependency by updating
1718 * the inode (or indirect block) appropriately but delaying the actual block
1719 * de-allocation (i.e., freemap and free space count manipulation) until
1720 * after the updated versions reach stable storage. After the disk is
1721 * updated, the blocks can be safely de-allocated whenever it is convenient.
1722 * This implementation handles only the common case of reducing a file's
1723 * length to zero. Other cases are handled by the conventional synchronous
1726 * The ffs implementation with which we worked double-checks
1727 * the state of the block pointers and file size as it reduces
1728 * a file's length. Some of this code is replicated here in our
1729 * soft updates implementation. The freeblks->fb_chkcnt field is
1730 * used to transfer a part of this information to the procedure
1731 * that eventually de-allocates the blocks.
1733 * This routine should be called from the routine that shortens
1734 * a file's length, before the inode's size or block pointers
1735 * are modified. It will save the block pointer information for
1736 * later release and zero the inode so that the calling routine
1739 struct softdep_setup_freeblocks_info {
1744 static int softdep_setup_freeblocks_bp(struct buf *bp, void *data);
1747 softdep_setup_freeblocks(ip, length)
1748 struct inode *ip; /* The inode whose length is to be reduced */
1749 off_t length; /* The new length for the file */
1751 struct softdep_setup_freeblocks_info info;
1752 struct freeblks *freeblks;
1753 struct inodedep *inodedep;
1754 struct allocdirect *adp;
1758 int i, error, delay;
1763 panic("softde_setup_freeblocks: non-zero length");
1764 MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
1765 M_FREEBLKS, M_SOFTDEP_FLAGS);
1766 bzero(freeblks, sizeof(struct freeblks));
1767 freeblks->fb_list.wk_type = D_FREEBLKS;
1768 freeblks->fb_state = ATTACHED;
1769 freeblks->fb_uid = ip->i_uid;
1770 freeblks->fb_previousinum = ip->i_number;
1771 freeblks->fb_devvp = ip->i_devvp;
1772 freeblks->fb_fs = fs;
1773 freeblks->fb_oldsize = ip->i_size;
1774 freeblks->fb_newsize = length;
1775 freeblks->fb_chkcnt = ip->i_blocks;
1776 for (i = 0; i < NDADDR; i++) {
1777 freeblks->fb_dblks[i] = ip->i_db[i];
1780 for (i = 0; i < NIADDR; i++) {
1781 freeblks->fb_iblks[i] = ip->i_ib[i];
1787 * Push the zero'ed inode to to its disk buffer so that we are free
1788 * to delete its dependencies below. Once the dependencies are gone
1789 * the buffer can be safely released.
1791 if ((error = bread(ip->i_devvp,
1792 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
1793 (int)fs->fs_bsize, &bp)) != 0)
1794 softdep_error("softdep_setup_freeblocks", error);
1795 *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)) =
1798 * Find and eliminate any inode dependencies.
1801 (void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep);
1802 if ((inodedep->id_state & IOSTARTED) != 0) {
1804 panic("softdep_setup_freeblocks: inode busy");
1807 * Add the freeblks structure to the list of operations that
1808 * must await the zero'ed inode being written to disk. If we
1809 * still have a bitmap dependency (delay == 0), then the inode
1810 * has never been written to disk, so we can process the
1811 * freeblks below once we have deleted the dependencies.
1813 delay = (inodedep->id_state & DEPCOMPLETE);
1815 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
1817 * Because the file length has been truncated to zero, any
1818 * pending block allocation dependency structures associated
1819 * with this inode are obsolete and can simply be de-allocated.
1820 * We must first merge the two dependency lists to get rid of
1821 * any duplicate freefrag structures, then purge the merged list.
1823 merge_inode_lists(inodedep);
1824 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
1825 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
1829 * We must wait for any I/O in progress to finish so that
1830 * all potential buffers on the dirty list will be visible.
1831 * Once they are all there, walk the list and get rid of
1836 drain_output(vp, 1);
1842 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
1843 softdep_setup_freeblocks_bp, &info);
1844 } while (count != 0);
1846 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) != 0)
1847 (void)free_inodedep(inodedep);
1850 freeblks->fb_state |= DEPCOMPLETE;
1852 * If the inode with zeroed block pointers is now on disk
1853 * we can start freeing blocks. Add freeblks to the worklist
1854 * instead of calling handle_workitem_freeblocks directly as
1855 * it is more likely that additional IO is needed to complete
1856 * the request here than in the !delay case.
1858 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
1859 add_to_worklist(&freeblks->fb_list);
1864 * If the inode has never been written to disk (delay == 0),
1865 * then we can process the freeblks now that we have deleted
1869 handle_workitem_freeblocks(freeblks);
1873 softdep_setup_freeblocks_bp(struct buf *bp, void *data)
1875 struct softdep_setup_freeblocks_info *info = data;
1876 struct inodedep *inodedep;
1878 if (getdirtybuf(&bp, MNT_WAIT) == 0) {
1879 printf("softdep_setup_freeblocks_bp(1): caught bp %p going away\n", bp);
1882 if (bp->b_vp != ITOV(info->ip) || (bp->b_flags & B_DELWRI) == 0) {
1883 printf("softdep_setup_freeblocks_bp(2): caught bp %p going away\n", bp);
1887 (void) inodedep_lookup(info->fs, info->ip->i_number, 0, &inodedep);
1888 deallocate_dependencies(bp, inodedep);
1889 bp->b_flags |= B_INVAL | B_NOCACHE;
1897 * Reclaim any dependency structures from a buffer that is about to
1898 * be reallocated to a new vnode. The buffer must be locked, thus,
1899 * no I/O completion operations can occur while we are manipulating
1900 * its associated dependencies. The mutex is held so that other I/O's
1901 * associated with related dependencies do not occur.
1904 deallocate_dependencies(bp, inodedep)
1906 struct inodedep *inodedep;
1908 struct worklist *wk;
1909 struct indirdep *indirdep;
1910 struct allocindir *aip;
1911 struct pagedep *pagedep;
1912 struct dirrem *dirrem;
1916 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
1917 switch (wk->wk_type) {
1920 indirdep = WK_INDIRDEP(wk);
1922 * None of the indirect pointers will ever be visible,
1923 * so they can simply be tossed. GOINGAWAY ensures
1924 * that allocated pointers will be saved in the buffer
1925 * cache until they are freed. Note that they will
1926 * only be able to be found by their physical address
1927 * since the inode mapping the logical address will
1928 * be gone. The save buffer used for the safe copy
1929 * was allocated in setup_allocindir_phase2 using
1930 * the physical address so it could be used for this
1931 * purpose. Hence we swap the safe copy with the real
1932 * copy, allowing the safe copy to be freed and holding
1933 * on to the real copy for later use in indir_trunc.
1935 if (indirdep->ir_state & GOINGAWAY) {
1937 panic("deallocate_dependencies: already gone");
1939 indirdep->ir_state |= GOINGAWAY;
1940 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
1941 free_allocindir(aip, inodedep);
1942 if (bp->b_lblkno >= 0 ||
1943 bp->b_blkno != indirdep->ir_savebp->b_lblkno) {
1945 panic("deallocate_dependencies: not indir");
1947 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
1949 WORKLIST_REMOVE(wk);
1950 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
1954 pagedep = WK_PAGEDEP(wk);
1956 * None of the directory additions will ever be
1957 * visible, so they can simply be tossed.
1959 for (i = 0; i < DAHASHSZ; i++)
1961 LIST_FIRST(&pagedep->pd_diraddhd[i])))
1963 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
1966 * Copy any directory remove dependencies to the list
1967 * to be processed after the zero'ed inode is written.
1968 * If the inode has already been written, then they
1969 * can be dumped directly onto the work list.
1971 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
1972 LIST_REMOVE(dirrem, dm_next);
1973 dirrem->dm_dirinum = pagedep->pd_ino;
1974 if (inodedep == NULL ||
1975 (inodedep->id_state & ALLCOMPLETE) ==
1977 add_to_worklist(&dirrem->dm_list);
1979 WORKLIST_INSERT(&inodedep->id_bufwait,
1982 WORKLIST_REMOVE(&pagedep->pd_list);
1983 LIST_REMOVE(pagedep, pd_hash);
1984 WORKITEM_FREE(pagedep, D_PAGEDEP);
1988 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
1994 panic("deallocate_dependencies: Unexpected type %s",
1995 TYPENAME(wk->wk_type));
2000 panic("deallocate_dependencies: Unknown type %s",
2001 TYPENAME(wk->wk_type));
2008 * Free an allocdirect. Generate a new freefrag work request if appropriate.
2009 * This routine must be called with splbio interrupts blocked.
2012 free_allocdirect(adphead, adp, delay)
2013 struct allocdirectlst *adphead;
2014 struct allocdirect *adp;
2019 if (lk.lkt_held == NOHOLDER)
2020 panic("free_allocdirect: lock not held");
2022 if ((adp->ad_state & DEPCOMPLETE) == 0)
2023 LIST_REMOVE(adp, ad_deps);
2024 TAILQ_REMOVE(adphead, adp, ad_next);
2025 if ((adp->ad_state & COMPLETE) == 0)
2026 WORKLIST_REMOVE(&adp->ad_list);
2027 if (adp->ad_freefrag != NULL) {
2029 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2030 &adp->ad_freefrag->ff_list);
2032 add_to_worklist(&adp->ad_freefrag->ff_list);
2034 WORKITEM_FREE(adp, D_ALLOCDIRECT);
2038 * Prepare an inode to be freed. The actual free operation is not
2039 * done until the zero'ed inode has been written to disk.
2042 softdep_freefile(pvp, ino, mode)
2047 struct inode *ip = VTOI(pvp);
2048 struct inodedep *inodedep;
2049 struct freefile *freefile;
2052 * This sets up the inode de-allocation dependency.
2054 MALLOC(freefile, struct freefile *, sizeof(struct freefile),
2055 M_FREEFILE, M_SOFTDEP_FLAGS);
2056 freefile->fx_list.wk_type = D_FREEFILE;
2057 freefile->fx_list.wk_state = 0;
2058 freefile->fx_mode = mode;
2059 freefile->fx_oldinum = ino;
2060 freefile->fx_devvp = ip->i_devvp;
2061 freefile->fx_fs = ip->i_fs;
2064 * If the inodedep does not exist, then the zero'ed inode has
2065 * been written to disk. If the allocated inode has never been
2066 * written to disk, then the on-disk inode is zero'ed. In either
2067 * case we can free the file immediately.
2070 if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0 ||
2071 check_inode_unwritten(inodedep)) {
2073 handle_workitem_freefile(freefile);
2076 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2081 * Check to see if an inode has never been written to disk. If
2082 * so free the inodedep and return success, otherwise return failure.
2083 * This routine must be called with splbio interrupts blocked.
2085 * If we still have a bitmap dependency, then the inode has never
2086 * been written to disk. Drop the dependency as it is no longer
2087 * necessary since the inode is being deallocated. We set the
2088 * ALLCOMPLETE flags since the bitmap now properly shows that the
2089 * inode is not allocated. Even if the inode is actively being
2090 * written, it has been rolled back to its zero'ed state, so we
2091 * are ensured that a zero inode is what is on the disk. For short
2092 * lived files, this change will usually result in removing all the
2093 * dependencies from the inode so that it can be freed immediately.
2096 check_inode_unwritten(inodedep)
2097 struct inodedep *inodedep;
2100 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2101 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2102 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2103 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2104 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2105 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2106 inodedep->id_nlinkdelta != 0)
2110 * Another process might be in initiate_write_inodeblock
2111 * trying to allocate memory without holding "Softdep Lock".
2113 if ((inodedep->id_state & IOSTARTED) != 0 &&
2114 inodedep->id_savedino == NULL)
2117 inodedep->id_state |= ALLCOMPLETE;
2118 LIST_REMOVE(inodedep, id_deps);
2119 inodedep->id_buf = NULL;
2120 if (inodedep->id_state & ONWORKLIST)
2121 WORKLIST_REMOVE(&inodedep->id_list);
2122 if (inodedep->id_savedino != NULL) {
2123 FREE(inodedep->id_savedino, M_INODEDEP);
2124 inodedep->id_savedino = NULL;
2126 if (free_inodedep(inodedep) == 0) {
2128 panic("check_inode_unwritten: busy inode");
2134 * Try to free an inodedep structure. Return 1 if it could be freed.
2137 free_inodedep(inodedep)
2138 struct inodedep *inodedep;
2141 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2142 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2143 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2144 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2145 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2146 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2147 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2148 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino != NULL)
2150 LIST_REMOVE(inodedep, id_hash);
2151 WORKITEM_FREE(inodedep, D_INODEDEP);
2157 * This workitem routine performs the block de-allocation.
2158 * The workitem is added to the pending list after the updated
2159 * inode block has been written to disk. As mentioned above,
2160 * checks regarding the number of blocks de-allocated (compared
2161 * to the number of blocks allocated for the file) are also
2162 * performed in this function.
2165 handle_workitem_freeblocks(freeblks)
2166 struct freeblks *freeblks;
2171 int i, level, bsize;
2172 long nblocks, blocksreleased = 0;
2173 int error, allerror = 0;
2174 ufs_lbn_t baselbns[NIADDR], tmpval;
2176 tip.i_number = freeblks->fb_previousinum;
2177 tip.i_devvp = freeblks->fb_devvp;
2178 tip.i_dev = freeblks->fb_devvp->v_rdev;
2179 tip.i_fs = freeblks->fb_fs;
2180 tip.i_size = freeblks->fb_oldsize;
2181 tip.i_uid = freeblks->fb_uid;
2182 fs = freeblks->fb_fs;
2184 baselbns[0] = NDADDR;
2185 for (i = 1; i < NIADDR; i++) {
2186 tmpval *= NINDIR(fs);
2187 baselbns[i] = baselbns[i - 1] + tmpval;
2189 nblocks = btodb(fs->fs_bsize);
2192 * Indirect blocks first.
2194 for (level = (NIADDR - 1); level >= 0; level--) {
2195 if ((bn = freeblks->fb_iblks[level]) == 0)
2197 if ((error = indir_trunc(&tip, fsbtodb(fs, bn), level,
2198 baselbns[level], &blocksreleased)) == 0)
2200 ffs_blkfree(&tip, bn, fs->fs_bsize);
2201 blocksreleased += nblocks;
2204 * All direct blocks or frags.
2206 for (i = (NDADDR - 1); i >= 0; i--) {
2207 if ((bn = freeblks->fb_dblks[i]) == 0)
2209 bsize = blksize(fs, &tip, i);
2210 ffs_blkfree(&tip, bn, bsize);
2211 blocksreleased += btodb(bsize);
2215 if (freeblks->fb_chkcnt != blocksreleased)
2216 printf("handle_workitem_freeblocks: block count\n");
2218 softdep_error("handle_workitem_freeblks", allerror);
2219 #endif /* DIAGNOSTIC */
2220 WORKITEM_FREE(freeblks, D_FREEBLKS);
2224 * Release blocks associated with the inode ip and stored in the indirect
2225 * block dbn. If level is greater than SINGLE, the block is an indirect block
2226 * and recursive calls to indirtrunc must be used to cleanse other indirect
2230 indir_trunc(ip, dbn, level, lbn, countp)
2241 struct worklist *wk;
2242 struct indirdep *indirdep;
2243 int i, lbnadd, nblocks;
2244 int error, allerror = 0;
2248 for (i = level; i > 0; i--)
2249 lbnadd *= NINDIR(fs);
2251 * Get buffer of block pointers to be freed. This routine is not
2252 * called until the zero'ed inode has been written, so it is safe
2253 * to free blocks as they are encountered. Because the inode has
2254 * been zero'ed, calls to bmap on these blocks will fail. So, we
2255 * have to use the on-disk address and the block device for the
2256 * filesystem to look them up. If the file was deleted before its
2257 * indirect blocks were all written to disk, the routine that set
2258 * us up (deallocate_dependencies) will have arranged to leave
2259 * a complete copy of the indirect block in memory for our use.
2260 * Otherwise we have to read the blocks in from the disk.
2263 if ((bp = incore(ip->i_devvp, dbn)) != NULL &&
2264 (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2265 if (wk->wk_type != D_INDIRDEP ||
2266 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2267 (indirdep->ir_state & GOINGAWAY) == 0) {
2269 panic("indir_trunc: lost indirdep");
2271 WORKLIST_REMOVE(wk);
2272 WORKITEM_FREE(indirdep, D_INDIRDEP);
2273 if (LIST_FIRST(&bp->b_dep) != NULL) {
2275 panic("indir_trunc: dangling dep");
2280 error = bread(ip->i_devvp, dbn, (int)fs->fs_bsize, &bp);
2285 * Recursively free indirect blocks.
2287 bap = (ufs_daddr_t *)bp->b_data;
2288 nblocks = btodb(fs->fs_bsize);
2289 for (i = NINDIR(fs) - 1; i >= 0; i--) {
2290 if ((nb = bap[i]) == 0)
2293 if ((error = indir_trunc(ip, fsbtodb(fs, nb),
2294 level - 1, lbn + (i * lbnadd), countp)) != 0)
2297 ffs_blkfree(ip, nb, fs->fs_bsize);
2300 bp->b_flags |= B_INVAL | B_NOCACHE;
2306 * Free an allocindir.
2307 * This routine must be called with splbio interrupts blocked.
2310 free_allocindir(aip, inodedep)
2311 struct allocindir *aip;
2312 struct inodedep *inodedep;
2314 struct freefrag *freefrag;
2317 if (lk.lkt_held == NOHOLDER)
2318 panic("free_allocindir: lock not held");
2320 if ((aip->ai_state & DEPCOMPLETE) == 0)
2321 LIST_REMOVE(aip, ai_deps);
2322 if (aip->ai_state & ONWORKLIST)
2323 WORKLIST_REMOVE(&aip->ai_list);
2324 LIST_REMOVE(aip, ai_next);
2325 if ((freefrag = aip->ai_freefrag) != NULL) {
2326 if (inodedep == NULL)
2327 add_to_worklist(&freefrag->ff_list);
2329 WORKLIST_INSERT(&inodedep->id_bufwait,
2330 &freefrag->ff_list);
2332 WORKITEM_FREE(aip, D_ALLOCINDIR);
2336 * Directory entry addition dependencies.
2338 * When adding a new directory entry, the inode (with its incremented link
2339 * count) must be written to disk before the directory entry's pointer to it.
2340 * Also, if the inode is newly allocated, the corresponding freemap must be
2341 * updated (on disk) before the directory entry's pointer. These requirements
2342 * are met via undo/redo on the directory entry's pointer, which consists
2343 * simply of the inode number.
2345 * As directory entries are added and deleted, the free space within a
2346 * directory block can become fragmented. The ufs filesystem will compact
2347 * a fragmented directory block to make space for a new entry. When this
2348 * occurs, the offsets of previously added entries change. Any "diradd"
2349 * dependency structures corresponding to these entries must be updated with
2354 * This routine is called after the in-memory inode's link
2355 * count has been incremented, but before the directory entry's
2356 * pointer to the inode has been set.
2359 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp)
2360 struct buf *bp; /* buffer containing directory block */
2361 struct inode *dp; /* inode for directory */
2362 off_t diroffset; /* offset of new entry in directory */
2363 ino_t newinum; /* inode referenced by new directory entry */
2364 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
2366 int offset; /* offset of new entry within directory block */
2367 ufs_lbn_t lbn; /* block in directory containing new entry */
2370 struct pagedep *pagedep;
2371 struct inodedep *inodedep;
2372 struct mkdir *mkdir1, *mkdir2;
2375 * Whiteouts have no dependencies.
2377 if (newinum == WINO) {
2378 if (newdirbp != NULL)
2384 lbn = lblkno(fs, diroffset);
2385 offset = blkoff(fs, diroffset);
2386 MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2388 bzero(dap, sizeof(struct diradd));
2389 dap->da_list.wk_type = D_DIRADD;
2390 dap->da_offset = offset;
2391 dap->da_newinum = newinum;
2392 dap->da_state = ATTACHED;
2393 if (newdirbp == NULL) {
2394 dap->da_state |= DEPCOMPLETE;
2397 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
2398 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2400 mkdir1->md_list.wk_type = D_MKDIR;
2401 mkdir1->md_state = MKDIR_BODY;
2402 mkdir1->md_diradd = dap;
2403 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2405 mkdir2->md_list.wk_type = D_MKDIR;
2406 mkdir2->md_state = MKDIR_PARENT;
2407 mkdir2->md_diradd = dap;
2409 * Dependency on "." and ".." being written to disk.
2411 mkdir1->md_buf = newdirbp;
2413 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
2414 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
2418 * Dependency on link count increase for parent directory
2421 if (inodedep_lookup(dp->i_fs, dp->i_number, 0, &inodedep) == 0
2422 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2423 dap->da_state &= ~MKDIR_PARENT;
2424 WORKITEM_FREE(mkdir2, D_MKDIR);
2426 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
2427 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
2431 * Link into parent directory pagedep to await its being written.
2433 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2434 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2435 dap->da_pagedep = pagedep;
2436 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
2439 * Link into its inodedep. Put it on the id_bufwait list if the inode
2440 * is not yet written. If it is written, do the post-inode write
2441 * processing to put it on the id_pendinghd list.
2443 (void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep);
2444 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
2445 diradd_inode_written(dap, inodedep);
2447 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2452 * This procedure is called to change the offset of a directory
2453 * entry when compacting a directory block which must be owned
2454 * exclusively by the caller. Note that the actual entry movement
2455 * must be done in this procedure to ensure that no I/O completions
2456 * occur while the move is in progress.
2459 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
2460 struct inode *dp; /* inode for directory */
2461 caddr_t base; /* address of dp->i_offset */
2462 caddr_t oldloc; /* address of old directory location */
2463 caddr_t newloc; /* address of new directory location */
2464 int entrysize; /* size of directory entry */
2466 int offset, oldoffset, newoffset;
2467 struct pagedep *pagedep;
2472 lbn = lblkno(dp->i_fs, dp->i_offset);
2473 offset = blkoff(dp->i_fs, dp->i_offset);
2474 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
2476 oldoffset = offset + (oldloc - base);
2477 newoffset = offset + (newloc - base);
2479 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
2480 if (dap->da_offset != oldoffset)
2482 dap->da_offset = newoffset;
2483 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
2485 LIST_REMOVE(dap, da_pdlist);
2486 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
2492 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
2493 if (dap->da_offset == oldoffset) {
2494 dap->da_offset = newoffset;
2500 bcopy(oldloc, newloc, entrysize);
2505 * Free a diradd dependency structure. This routine must be called
2506 * with splbio interrupts blocked.
2512 struct dirrem *dirrem;
2513 struct pagedep *pagedep;
2514 struct inodedep *inodedep;
2515 struct mkdir *mkdir, *nextmd;
2518 if (lk.lkt_held == NOHOLDER)
2519 panic("free_diradd: lock not held");
2521 WORKLIST_REMOVE(&dap->da_list);
2522 LIST_REMOVE(dap, da_pdlist);
2523 if ((dap->da_state & DIRCHG) == 0) {
2524 pagedep = dap->da_pagedep;
2526 dirrem = dap->da_previous;
2527 pagedep = dirrem->dm_pagedep;
2528 dirrem->dm_dirinum = pagedep->pd_ino;
2529 add_to_worklist(&dirrem->dm_list);
2531 if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum,
2533 (void) free_inodedep(inodedep);
2534 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2535 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
2536 nextmd = LIST_NEXT(mkdir, md_mkdirs);
2537 if (mkdir->md_diradd != dap)
2539 dap->da_state &= ~mkdir->md_state;
2540 WORKLIST_REMOVE(&mkdir->md_list);
2541 LIST_REMOVE(mkdir, md_mkdirs);
2542 WORKITEM_FREE(mkdir, D_MKDIR);
2544 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2546 panic("free_diradd: unfound ref");
2549 WORKITEM_FREE(dap, D_DIRADD);
2553 * Directory entry removal dependencies.
2555 * When removing a directory entry, the entry's inode pointer must be
2556 * zero'ed on disk before the corresponding inode's link count is decremented
2557 * (possibly freeing the inode for re-use). This dependency is handled by
2558 * updating the directory entry but delaying the inode count reduction until
2559 * after the directory block has been written to disk. After this point, the
2560 * inode count can be decremented whenever it is convenient.
2564 * This routine should be called immediately after removing
2565 * a directory entry. The inode's link count should not be
2566 * decremented by the calling procedure -- the soft updates
2567 * code will do this task when it is safe.
2570 softdep_setup_remove(bp, dp, ip, isrmdir)
2571 struct buf *bp; /* buffer containing directory block */
2572 struct inode *dp; /* inode for the directory being modified */
2573 struct inode *ip; /* inode for directory entry being removed */
2574 int isrmdir; /* indicates if doing RMDIR */
2576 struct dirrem *dirrem, *prevdirrem;
2579 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
2581 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2584 * If the COMPLETE flag is clear, then there were no active
2585 * entries and we want to roll back to a zeroed entry until
2586 * the new inode is committed to disk. If the COMPLETE flag is
2587 * set then we have deleted an entry that never made it to
2588 * disk. If the entry we deleted resulted from a name change,
2589 * then the old name still resides on disk. We cannot delete
2590 * its inode (returned to us in prevdirrem) until the zeroed
2591 * directory entry gets to disk. The new inode has never been
2592 * referenced on the disk, so can be deleted immediately.
2594 if ((dirrem->dm_state & COMPLETE) == 0) {
2595 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
2599 if (prevdirrem != NULL)
2600 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
2601 prevdirrem, dm_next);
2602 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
2604 handle_workitem_remove(dirrem);
2609 * Allocate a new dirrem if appropriate and return it along with
2610 * its associated pagedep. Called without a lock, returns with lock.
2612 static long num_dirrem; /* number of dirrem allocated */
2613 static struct dirrem *
2614 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
2615 struct buf *bp; /* buffer containing directory block */
2616 struct inode *dp; /* inode for the directory being modified */
2617 struct inode *ip; /* inode for directory entry being removed */
2618 int isrmdir; /* indicates if doing RMDIR */
2619 struct dirrem **prevdirremp; /* previously referenced inode, if any */
2624 struct dirrem *dirrem;
2625 struct pagedep *pagedep;
2628 * Whiteouts have no deletion dependencies.
2631 panic("newdirrem: whiteout");
2633 * If we are over our limit, try to improve the situation.
2634 * Limiting the number of dirrem structures will also limit
2635 * the number of freefile and freeblks structures.
2637 if (num_dirrem > max_softdeps / 2 && speedup_syncer() == 0)
2638 (void) request_cleanup(FLUSH_REMOVE, 0);
2640 MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
2641 M_DIRREM, M_SOFTDEP_FLAGS);
2642 bzero(dirrem, sizeof(struct dirrem));
2643 dirrem->dm_list.wk_type = D_DIRREM;
2644 dirrem->dm_state = isrmdir ? RMDIR : 0;
2645 dirrem->dm_mnt = ITOV(ip)->v_mount;
2646 dirrem->dm_oldinum = ip->i_number;
2647 *prevdirremp = NULL;
2650 lbn = lblkno(dp->i_fs, dp->i_offset);
2651 offset = blkoff(dp->i_fs, dp->i_offset);
2652 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2653 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2654 dirrem->dm_pagedep = pagedep;
2656 * Check for a diradd dependency for the same directory entry.
2657 * If present, then both dependencies become obsolete and can
2658 * be de-allocated. Check for an entry on both the pd_dirraddhd
2659 * list and the pd_pendinghd list.
2662 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
2663 if (dap->da_offset == offset)
2667 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
2668 if (dap->da_offset == offset)
2674 * Must be ATTACHED at this point.
2676 if ((dap->da_state & ATTACHED) == 0) {
2678 panic("newdirrem: not ATTACHED");
2680 if (dap->da_newinum != ip->i_number) {
2682 panic("newdirrem: inum %"PRId64" should be %"PRId64,
2683 ip->i_number, dap->da_newinum);
2686 * If we are deleting a changed name that never made it to disk,
2687 * then return the dirrem describing the previous inode (which
2688 * represents the inode currently referenced from this entry on disk).
2690 if ((dap->da_state & DIRCHG) != 0) {
2691 *prevdirremp = dap->da_previous;
2692 dap->da_state &= ~DIRCHG;
2693 dap->da_pagedep = pagedep;
2696 * We are deleting an entry that never made it to disk.
2697 * Mark it COMPLETE so we can delete its inode immediately.
2699 dirrem->dm_state |= COMPLETE;
2705 * Directory entry change dependencies.
2707 * Changing an existing directory entry requires that an add operation
2708 * be completed first followed by a deletion. The semantics for the addition
2709 * are identical to the description of adding a new entry above except
2710 * that the rollback is to the old inode number rather than zero. Once
2711 * the addition dependency is completed, the removal is done as described
2712 * in the removal routine above.
2716 * This routine should be called immediately after changing
2717 * a directory entry. The inode's link count should not be
2718 * decremented by the calling procedure -- the soft updates
2719 * code will perform this task when it is safe.
2722 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
2723 struct buf *bp; /* buffer containing directory block */
2724 struct inode *dp; /* inode for the directory being modified */
2725 struct inode *ip; /* inode for directory entry being removed */
2726 ino_t newinum; /* new inode number for changed entry */
2727 int isrmdir; /* indicates if doing RMDIR */
2730 struct diradd *dap = NULL;
2731 struct dirrem *dirrem, *prevdirrem;
2732 struct pagedep *pagedep;
2733 struct inodedep *inodedep;
2735 offset = blkoff(dp->i_fs, dp->i_offset);
2738 * Whiteouts do not need diradd dependencies.
2740 if (newinum != WINO) {
2741 MALLOC(dap, struct diradd *, sizeof(struct diradd),
2742 M_DIRADD, M_SOFTDEP_FLAGS);
2743 bzero(dap, sizeof(struct diradd));
2744 dap->da_list.wk_type = D_DIRADD;
2745 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
2746 dap->da_offset = offset;
2747 dap->da_newinum = newinum;
2751 * Allocate a new dirrem and ACQUIRE_LOCK.
2753 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2754 pagedep = dirrem->dm_pagedep;
2756 * The possible values for isrmdir:
2757 * 0 - non-directory file rename
2758 * 1 - directory rename within same directory
2759 * inum - directory rename to new directory of given inode number
2760 * When renaming to a new directory, we are both deleting and
2761 * creating a new directory entry, so the link count on the new
2762 * directory should not change. Thus we do not need the followup
2763 * dirrem which is usually done in handle_workitem_remove. We set
2764 * the DIRCHG flag to tell handle_workitem_remove to skip the
2768 dirrem->dm_state |= DIRCHG;
2771 * Whiteouts have no additional dependencies,
2772 * so just put the dirrem on the correct list.
2774 if (newinum == WINO) {
2775 if ((dirrem->dm_state & COMPLETE) == 0) {
2776 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
2779 dirrem->dm_dirinum = pagedep->pd_ino;
2780 add_to_worklist(&dirrem->dm_list);
2787 * If the COMPLETE flag is clear, then there were no active
2788 * entries and we want to roll back to the previous inode until
2789 * the new inode is committed to disk. If the COMPLETE flag is
2790 * set, then we have deleted an entry that never made it to disk.
2791 * If the entry we deleted resulted from a name change, then the old
2792 * inode reference still resides on disk. Any rollback that we do
2793 * needs to be to that old inode (returned to us in prevdirrem). If
2794 * the entry we deleted resulted from a create, then there is
2795 * no entry on the disk, so we want to roll back to zero rather
2796 * than the uncommitted inode. In either of the COMPLETE cases we
2797 * want to immediately free the unwritten and unreferenced inode.
2799 if ((dirrem->dm_state & COMPLETE) == 0) {
2800 dap->da_previous = dirrem;
2802 if (prevdirrem != NULL) {
2803 dap->da_previous = prevdirrem;
2805 dap->da_state &= ~DIRCHG;
2806 dap->da_pagedep = pagedep;
2808 dirrem->dm_dirinum = pagedep->pd_ino;
2809 add_to_worklist(&dirrem->dm_list);
2812 * Link into its inodedep. Put it on the id_bufwait list if the inode
2813 * is not yet written. If it is written, do the post-inode write
2814 * processing to put it on the id_pendinghd list.
2816 if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 ||
2817 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2818 dap->da_state |= COMPLETE;
2819 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
2820 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
2822 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
2824 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2830 * Called whenever the link count on an inode is changed.
2831 * It creates an inode dependency so that the new reference(s)
2832 * to the inode cannot be committed to disk until the updated
2833 * inode has been written.
2836 softdep_change_linkcnt(ip)
2837 struct inode *ip; /* the inode with the increased link count */
2839 struct inodedep *inodedep;
2842 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep);
2843 if (ip->i_nlink < ip->i_effnlink) {
2845 panic("softdep_change_linkcnt: bad delta");
2847 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2852 * This workitem decrements the inode's link count.
2853 * If the link count reaches zero, the file is removed.
2856 handle_workitem_remove(dirrem)
2857 struct dirrem *dirrem;
2859 struct thread *td = curthread; /* XXX */
2860 struct inodedep *inodedep;
2866 if ((error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, &vp)) != 0) {
2867 softdep_error("handle_workitem_remove: vget", error);
2872 if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){
2874 panic("handle_workitem_remove: lost inodedep");
2877 * Normal file deletion.
2879 if ((dirrem->dm_state & RMDIR) == 0) {
2881 ip->i_flag |= IN_CHANGE;
2882 if (ip->i_nlink < ip->i_effnlink) {
2884 panic("handle_workitem_remove: bad file delta");
2886 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2890 WORKITEM_FREE(dirrem, D_DIRREM);
2894 * Directory deletion. Decrement reference count for both the
2895 * just deleted parent directory entry and the reference for ".".
2896 * Next truncate the directory to length zero. When the
2897 * truncation completes, arrange to have the reference count on
2898 * the parent decremented to account for the loss of "..".
2901 ip->i_flag |= IN_CHANGE;
2902 if (ip->i_nlink < ip->i_effnlink) {
2904 panic("handle_workitem_remove: bad dir delta");
2906 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2908 if ((error = UFS_TRUNCATE(vp, (off_t)0, 0, proc0.p_ucred, td)) != 0)
2909 softdep_error("handle_workitem_remove: truncate", error);
2911 * Rename a directory to a new parent. Since, we are both deleting
2912 * and creating a new directory entry, the link count on the new
2913 * directory should not change. Thus we skip the followup dirrem.
2915 if (dirrem->dm_state & DIRCHG) {
2918 WORKITEM_FREE(dirrem, D_DIRREM);
2922 * If the inodedep does not exist, then the zero'ed inode has
2923 * been written to disk. If the allocated inode has never been
2924 * written to disk, then the on-disk inode is zero'ed. In either
2925 * case we can remove the file immediately.
2928 dirrem->dm_state = 0;
2929 oldinum = dirrem->dm_oldinum;
2930 dirrem->dm_oldinum = dirrem->dm_dirinum;
2931 if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 ||
2932 check_inode_unwritten(inodedep)) {
2935 handle_workitem_remove(dirrem);
2938 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
2940 ip->i_flag |= IN_CHANGE;
2946 * Inode de-allocation dependencies.
2948 * When an inode's link count is reduced to zero, it can be de-allocated. We
2949 * found it convenient to postpone de-allocation until after the inode is
2950 * written to disk with its new link count (zero). At this point, all of the
2951 * on-disk inode's block pointers are nullified and, with careful dependency
2952 * list ordering, all dependencies related to the inode will be satisfied and
2953 * the corresponding dependency structures de-allocated. So, if/when the
2954 * inode is reused, there will be no mixing of old dependencies with new
2955 * ones. This artificial dependency is set up by the block de-allocation
2956 * procedure above (softdep_setup_freeblocks) and completed by the
2957 * following procedure.
2960 handle_workitem_freefile(freefile)
2961 struct freefile *freefile;
2965 struct inodedep *idp;
2970 error = inodedep_lookup(freefile->fx_fs, freefile->fx_oldinum, 0, &idp);
2973 panic("handle_workitem_freefile: inodedep survived");
2975 tip.i_devvp = freefile->fx_devvp;
2976 tip.i_dev = freefile->fx_devvp->v_rdev;
2977 tip.i_fs = freefile->fx_fs;
2979 if ((error = ffs_freefile(&vp, freefile->fx_oldinum, freefile->fx_mode)) != 0)
2980 softdep_error("handle_workitem_freefile", error);
2981 WORKITEM_FREE(freefile, D_FREEFILE);
2985 * Helper function which unlinks marker element from work list and returns
2986 * the next element on the list.
2988 static __inline struct worklist *
2989 markernext(struct worklist *marker)
2991 struct worklist *next;
2993 next = LIST_NEXT(marker, wk_list);
2994 LIST_REMOVE(marker, wk_list);
3001 * The dependency structures constructed above are most actively used when file
3002 * system blocks are written to disk. No constraints are placed on when a
3003 * block can be written, but unsatisfied update dependencies are made safe by
3004 * modifying (or replacing) the source memory for the duration of the disk
3005 * write. When the disk write completes, the memory block is again brought
3008 * In-core inode structure reclamation.
3010 * Because there are a finite number of "in-core" inode structures, they are
3011 * reused regularly. By transferring all inode-related dependencies to the
3012 * in-memory inode block and indexing them separately (via "inodedep"s), we
3013 * can allow "in-core" inode structures to be reused at any time and avoid
3014 * any increase in contention.
3016 * Called just before entering the device driver to initiate a new disk I/O.
3017 * The buffer must be locked, thus, no I/O completion operations can occur
3018 * while we are manipulating its associated dependencies.
3021 softdep_disk_io_initiation(bp)
3022 struct buf *bp; /* structure describing disk write to occur */
3024 struct worklist *wk;
3025 struct worklist marker;
3026 struct indirdep *indirdep;
3029 * We only care about write operations. There should never
3030 * be dependencies for reads.
3032 if (bp->b_flags & B_READ)
3033 panic("softdep_disk_io_initiation: read");
3035 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
3038 * Do any necessary pre-I/O processing.
3040 for (wk = LIST_FIRST(&bp->b_dep); wk; wk = markernext(&marker)) {
3041 LIST_INSERT_AFTER(wk, &marker, wk_list);
3043 switch (wk->wk_type) {
3046 initiate_write_filepage(WK_PAGEDEP(wk), bp);
3050 initiate_write_inodeblock(WK_INODEDEP(wk), bp);
3054 indirdep = WK_INDIRDEP(wk);
3055 if (indirdep->ir_state & GOINGAWAY)
3056 panic("disk_io_initiation: indirdep gone");
3058 * If there are no remaining dependencies, this
3059 * will be writing the real pointers, so the
3060 * dependency can be freed.
3062 if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) {
3063 indirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
3064 brelse(indirdep->ir_savebp);
3065 /* inline expand WORKLIST_REMOVE(wk); */
3066 wk->wk_state &= ~ONWORKLIST;
3067 LIST_REMOVE(wk, wk_list);
3068 WORKITEM_FREE(indirdep, D_INDIRDEP);
3072 * Replace up-to-date version with safe version.
3074 MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
3075 M_INDIRDEP, M_SOFTDEP_FLAGS);
3077 indirdep->ir_state &= ~ATTACHED;
3078 indirdep->ir_state |= UNDONE;
3079 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3080 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3092 panic("handle_disk_io_initiation: Unexpected type %s",
3093 TYPENAME(wk->wk_type));
3100 * Called from within the procedure above to deal with unsatisfied
3101 * allocation dependencies in a directory. The buffer must be locked,
3102 * thus, no I/O completion operations can occur while we are
3103 * manipulating its associated dependencies.
3106 initiate_write_filepage(pagedep, bp)
3107 struct pagedep *pagedep;
3114 if (pagedep->pd_state & IOSTARTED) {
3116 * This can only happen if there is a driver that does not
3117 * understand chaining. Here biodone will reissue the call
3118 * to strategy for the incomplete buffers.
3120 printf("initiate_write_filepage: already started\n");
3123 pagedep->pd_state |= IOSTARTED;
3125 for (i = 0; i < DAHASHSZ; i++) {
3126 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3127 ep = (struct direct *)
3128 ((char *)bp->b_data + dap->da_offset);
3129 if (ep->d_ino != dap->da_newinum) {
3131 panic("%s: dir inum %d != new %"PRId64,
3132 "initiate_write_filepage",
3133 ep->d_ino, dap->da_newinum);
3135 if (dap->da_state & DIRCHG)
3136 ep->d_ino = dap->da_previous->dm_oldinum;
3139 dap->da_state &= ~ATTACHED;
3140 dap->da_state |= UNDONE;
3147 * Called from within the procedure above to deal with unsatisfied
3148 * allocation dependencies in an inodeblock. The buffer must be
3149 * locked, thus, no I/O completion operations can occur while we
3150 * are manipulating its associated dependencies.
3153 initiate_write_inodeblock(inodedep, bp)
3154 struct inodedep *inodedep;
3155 struct buf *bp; /* The inode block */
3157 struct allocdirect *adp, *lastadp;
3161 ufs_lbn_t prevlbn = 0;
3164 if (inodedep->id_state & IOSTARTED)
3165 panic("initiate_write_inodeblock: already started");
3166 inodedep->id_state |= IOSTARTED;
3167 fs = inodedep->id_fs;
3168 dp = (struct dinode *)bp->b_data +
3169 ino_to_fsbo(fs, inodedep->id_ino);
3171 * If the bitmap is not yet written, then the allocated
3172 * inode cannot be written to disk.
3174 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3175 if (inodedep->id_savedino != NULL)
3176 panic("initiate_write_inodeblock: already doing I/O");
3177 MALLOC(sip, struct dinode *,
3178 sizeof(struct dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3179 inodedep->id_savedino = sip;
3180 *inodedep->id_savedino = *dp;
3181 bzero((caddr_t)dp, sizeof(struct dinode));
3182 dp->di_gen = inodedep->id_savedino->di_gen;
3186 * If no dependencies, then there is nothing to roll back.
3188 inodedep->id_savedsize = dp->di_size;
3189 if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL)
3192 * Set the dependencies to busy.
3195 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3196 adp = TAILQ_NEXT(adp, ad_next)) {
3198 if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3200 panic("softdep_write_inodeblock: lbn order");
3202 prevlbn = adp->ad_lbn;
3203 if (adp->ad_lbn < NDADDR &&
3204 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3206 panic("%s: direct pointer #%ld mismatch %d != %d",
3207 "softdep_write_inodeblock", adp->ad_lbn,
3208 dp->di_db[adp->ad_lbn], adp->ad_newblkno);
3210 if (adp->ad_lbn >= NDADDR &&
3211 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3213 panic("%s: indirect pointer #%ld mismatch %d != %d",
3214 "softdep_write_inodeblock", adp->ad_lbn - NDADDR,
3215 dp->di_ib[adp->ad_lbn - NDADDR], adp->ad_newblkno);
3217 deplist |= 1 << adp->ad_lbn;
3218 if ((adp->ad_state & ATTACHED) == 0) {
3220 panic("softdep_write_inodeblock: Unknown state 0x%x",
3223 #endif /* DIAGNOSTIC */
3224 adp->ad_state &= ~ATTACHED;
3225 adp->ad_state |= UNDONE;
3228 * The on-disk inode cannot claim to be any larger than the last
3229 * fragment that has been written. Otherwise, the on-disk inode
3230 * might have fragments that were not the last block in the file
3231 * which would corrupt the filesystem.
3233 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3234 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3235 if (adp->ad_lbn >= NDADDR)
3237 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3238 /* keep going until hitting a rollback to a frag */
3239 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3241 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3242 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3244 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3246 panic("softdep_write_inodeblock: lost dep1");
3248 #endif /* DIAGNOSTIC */
3251 for (i = 0; i < NIADDR; i++) {
3253 if (dp->di_ib[i] != 0 &&
3254 (deplist & ((1 << NDADDR) << i)) == 0) {
3256 panic("softdep_write_inodeblock: lost dep2");
3258 #endif /* DIAGNOSTIC */
3265 * If we have zero'ed out the last allocated block of the file,
3266 * roll back the size to the last currently allocated block.
3267 * We know that this last allocated block is a full-sized as
3268 * we already checked for fragments in the loop above.
3270 if (lastadp != NULL &&
3271 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3272 for (i = lastadp->ad_lbn; i >= 0; i--)
3273 if (dp->di_db[i] != 0)
3275 dp->di_size = (i + 1) * fs->fs_bsize;
3278 * The only dependencies are for indirect blocks.
3280 * The file size for indirect block additions is not guaranteed.
3281 * Such a guarantee would be non-trivial to achieve. The conventional
3282 * synchronous write implementation also does not make this guarantee.
3283 * Fsck should catch and fix discrepancies. Arguably, the file size
3284 * can be over-estimated without destroying integrity when the file
3285 * moves into the indirect blocks (i.e., is large). If we want to
3286 * postpone fsck, we are stuck with this argument.
3288 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3289 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3294 * This routine is called during the completion interrupt
3295 * service routine for a disk write (from the procedure called
3296 * by the device driver to inform the filesystem caches of
3297 * a request completion). It should be called early in this
3298 * procedure, before the block is made available to other
3299 * processes or other routines are called.
3302 softdep_disk_write_complete(bp)
3303 struct buf *bp; /* describes the completed disk write */
3305 struct worklist *wk;
3306 struct workhead reattach;
3307 struct newblk *newblk;
3308 struct allocindir *aip;
3309 struct allocdirect *adp;
3310 struct indirdep *indirdep;
3311 struct inodedep *inodedep;
3312 struct bmsafemap *bmsafemap;
3315 if (lk.lkt_held != NOHOLDER)
3316 panic("softdep_disk_write_complete: lock is held");
3317 lk.lkt_held = SPECIAL_FLAG;
3319 LIST_INIT(&reattach);
3320 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
3321 WORKLIST_REMOVE(wk);
3322 switch (wk->wk_type) {
3325 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
3326 WORKLIST_INSERT(&reattach, wk);
3330 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
3331 WORKLIST_INSERT(&reattach, wk);
3335 bmsafemap = WK_BMSAFEMAP(wk);
3336 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
3337 newblk->nb_state |= DEPCOMPLETE;
3338 newblk->nb_bmsafemap = NULL;
3339 LIST_REMOVE(newblk, nb_deps);
3342 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
3343 adp->ad_state |= DEPCOMPLETE;
3345 LIST_REMOVE(adp, ad_deps);
3346 handle_allocdirect_partdone(adp);
3349 LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
3350 aip->ai_state |= DEPCOMPLETE;
3352 LIST_REMOVE(aip, ai_deps);
3353 handle_allocindir_partdone(aip);
3356 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
3357 inodedep->id_state |= DEPCOMPLETE;
3358 LIST_REMOVE(inodedep, id_deps);
3359 inodedep->id_buf = NULL;
3361 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
3365 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
3369 adp = WK_ALLOCDIRECT(wk);
3370 adp->ad_state |= COMPLETE;
3371 handle_allocdirect_partdone(adp);
3375 aip = WK_ALLOCINDIR(wk);
3376 aip->ai_state |= COMPLETE;
3377 handle_allocindir_partdone(aip);
3381 indirdep = WK_INDIRDEP(wk);
3382 if (indirdep->ir_state & GOINGAWAY) {
3383 lk.lkt_held = NOHOLDER;
3384 panic("disk_write_complete: indirdep gone");
3386 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
3387 FREE(indirdep->ir_saveddata, M_INDIRDEP);
3388 indirdep->ir_saveddata = 0;
3389 indirdep->ir_state &= ~UNDONE;
3390 indirdep->ir_state |= ATTACHED;
3391 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
3392 handle_allocindir_partdone(aip);
3393 if (aip == LIST_FIRST(&indirdep->ir_donehd)) {
3394 lk.lkt_held = NOHOLDER;
3395 panic("disk_write_complete: not gone");
3398 WORKLIST_INSERT(&reattach, wk);
3399 if ((bp->b_flags & B_DELWRI) == 0)
3400 stat_indir_blk_ptrs++;
3405 lk.lkt_held = NOHOLDER;
3406 panic("handle_disk_write_complete: Unknown type %s",
3407 TYPENAME(wk->wk_type));
3412 * Reattach any requests that must be redone.
3414 while ((wk = LIST_FIRST(&reattach)) != NULL) {
3415 WORKLIST_REMOVE(wk);
3416 WORKLIST_INSERT(&bp->b_dep, wk);
3419 if (lk.lkt_held != SPECIAL_FLAG)
3420 panic("softdep_disk_write_complete: lock lost");
3421 lk.lkt_held = NOHOLDER;
3426 * Called from within softdep_disk_write_complete above. Note that
3427 * this routine is always called from interrupt level with further
3428 * splbio interrupts blocked.
3431 handle_allocdirect_partdone(adp)
3432 struct allocdirect *adp; /* the completed allocdirect */
3434 struct allocdirect *listadp;
3435 struct inodedep *inodedep;
3438 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3440 if (adp->ad_buf != NULL) {
3441 lk.lkt_held = NOHOLDER;
3442 panic("handle_allocdirect_partdone: dangling dep");
3445 * The on-disk inode cannot claim to be any larger than the last
3446 * fragment that has been written. Otherwise, the on-disk inode
3447 * might have fragments that were not the last block in the file
3448 * which would corrupt the filesystem. Thus, we cannot free any
3449 * allocdirects after one whose ad_oldblkno claims a fragment as
3450 * these blocks must be rolled back to zero before writing the inode.
3451 * We check the currently active set of allocdirects in id_inoupdt.
3453 inodedep = adp->ad_inodedep;
3454 bsize = inodedep->id_fs->fs_bsize;
3455 TAILQ_FOREACH(listadp, &inodedep->id_inoupdt, ad_next) {
3456 /* found our block */
3459 /* continue if ad_oldlbn is not a fragment */
3460 if (listadp->ad_oldsize == 0 ||
3461 listadp->ad_oldsize == bsize)
3463 /* hit a fragment */
3467 * If we have reached the end of the current list without
3468 * finding the just finished dependency, then it must be
3469 * on the future dependency list. Future dependencies cannot
3470 * be freed until they are moved to the current list.
3472 if (listadp == NULL) {
3474 TAILQ_FOREACH(listadp, &inodedep->id_newinoupdt, ad_next)
3475 /* found our block */
3478 if (listadp == NULL) {
3479 lk.lkt_held = NOHOLDER;
3480 panic("handle_allocdirect_partdone: lost dep");
3486 * If we have found the just finished dependency, then free
3487 * it along with anything that follows it that is complete.
3489 for (; adp; adp = listadp) {
3490 listadp = TAILQ_NEXT(adp, ad_next);
3491 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3493 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
3498 * Called from within softdep_disk_write_complete above. Note that
3499 * this routine is always called from interrupt level with further
3500 * splbio interrupts blocked.
3503 handle_allocindir_partdone(aip)
3504 struct allocindir *aip; /* the completed allocindir */
3506 struct indirdep *indirdep;
3508 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
3510 if (aip->ai_buf != NULL) {
3511 lk.lkt_held = NOHOLDER;
3512 panic("handle_allocindir_partdone: dangling dependency");
3514 indirdep = aip->ai_indirdep;
3515 if (indirdep->ir_state & UNDONE) {
3516 LIST_REMOVE(aip, ai_next);
3517 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
3520 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
3522 LIST_REMOVE(aip, ai_next);
3523 if (aip->ai_freefrag != NULL)
3524 add_to_worklist(&aip->ai_freefrag->ff_list);
3525 WORKITEM_FREE(aip, D_ALLOCINDIR);
3529 * Called from within softdep_disk_write_complete above to restore
3530 * in-memory inode block contents to their most up-to-date state. Note
3531 * that this routine is always called from interrupt level with further
3532 * splbio interrupts blocked.
3535 handle_written_inodeblock(inodedep, bp)
3536 struct inodedep *inodedep;
3537 struct buf *bp; /* buffer containing the inode block */
3539 struct worklist *wk, *filefree;
3540 struct allocdirect *adp, *nextadp;
3544 if ((inodedep->id_state & IOSTARTED) == 0) {
3545 lk.lkt_held = NOHOLDER;
3546 panic("handle_written_inodeblock: not started");
3548 inodedep->id_state &= ~IOSTARTED;
3549 inodedep->id_state |= COMPLETE;
3550 dp = (struct dinode *)bp->b_data +
3551 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
3553 * If we had to rollback the inode allocation because of
3554 * bitmaps being incomplete, then simply restore it.
3555 * Keep the block dirty so that it will not be reclaimed until
3556 * all associated dependencies have been cleared and the
3557 * corresponding updates written to disk.
3559 if (inodedep->id_savedino != NULL) {
3560 *dp = *inodedep->id_savedino;
3561 FREE(inodedep->id_savedino, M_INODEDEP);
3562 inodedep->id_savedino = NULL;
3563 if ((bp->b_flags & B_DELWRI) == 0)
3564 stat_inode_bitmap++;
3569 * Roll forward anything that had to be rolled back before
3570 * the inode could be updated.
3573 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
3574 nextadp = TAILQ_NEXT(adp, ad_next);
3575 if (adp->ad_state & ATTACHED) {
3576 lk.lkt_held = NOHOLDER;
3577 panic("handle_written_inodeblock: new entry");
3579 if (adp->ad_lbn < NDADDR) {
3580 if (dp->di_db[adp->ad_lbn] != adp->ad_oldblkno) {
3581 lk.lkt_held = NOHOLDER;
3582 panic("%s: %s #%ld mismatch %d != %d",
3583 "handle_written_inodeblock",
3584 "direct pointer", adp->ad_lbn,
3585 dp->di_db[adp->ad_lbn], adp->ad_oldblkno);
3587 dp->di_db[adp->ad_lbn] = adp->ad_newblkno;
3589 if (dp->di_ib[adp->ad_lbn - NDADDR] != 0) {
3590 lk.lkt_held = NOHOLDER;
3591 panic("%s: %s #%ld allocated as %d",
3592 "handle_written_inodeblock",
3593 "indirect pointer", adp->ad_lbn - NDADDR,
3594 dp->di_ib[adp->ad_lbn - NDADDR]);
3596 dp->di_ib[adp->ad_lbn - NDADDR] = adp->ad_newblkno;
3598 adp->ad_state &= ~UNDONE;
3599 adp->ad_state |= ATTACHED;
3602 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
3603 stat_direct_blk_ptrs++;
3605 * Reset the file size to its most up-to-date value.
3607 if (inodedep->id_savedsize == -1) {
3608 lk.lkt_held = NOHOLDER;
3609 panic("handle_written_inodeblock: bad size");
3611 if (dp->di_size != inodedep->id_savedsize) {
3612 dp->di_size = inodedep->id_savedsize;
3615 inodedep->id_savedsize = -1;
3617 * If there were any rollbacks in the inode block, then it must be
3618 * marked dirty so that its will eventually get written back in
3624 * Process any allocdirects that completed during the update.
3626 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
3627 handle_allocdirect_partdone(adp);
3629 * Process deallocations that were held pending until the
3630 * inode had been written to disk. Freeing of the inode
3631 * is delayed until after all blocks have been freed to
3632 * avoid creation of new <vfsid, inum, lbn> triples
3633 * before the old ones have been deleted.
3636 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
3637 WORKLIST_REMOVE(wk);
3638 switch (wk->wk_type) {
3642 * We defer adding filefree to the worklist until
3643 * all other additions have been made to ensure
3644 * that it will be done after all the old blocks
3647 if (filefree != NULL) {
3648 lk.lkt_held = NOHOLDER;
3649 panic("handle_written_inodeblock: filefree");
3655 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
3659 diradd_inode_written(WK_DIRADD(wk), inodedep);
3663 wk->wk_state |= COMPLETE;
3664 if ((wk->wk_state & ALLCOMPLETE) != ALLCOMPLETE)
3666 /* -- fall through -- */
3669 add_to_worklist(wk);
3673 lk.lkt_held = NOHOLDER;
3674 panic("handle_written_inodeblock: Unknown type %s",
3675 TYPENAME(wk->wk_type));
3679 if (filefree != NULL) {
3680 if (free_inodedep(inodedep) == 0) {
3681 lk.lkt_held = NOHOLDER;
3682 panic("handle_written_inodeblock: live inodedep");
3684 add_to_worklist(filefree);
3689 * If no outstanding dependencies, free it.
3691 if (free_inodedep(inodedep) || TAILQ_FIRST(&inodedep->id_inoupdt) == 0)
3693 return (hadchanges);
3697 * Process a diradd entry after its dependent inode has been written.
3698 * This routine must be called with splbio interrupts blocked.
3701 diradd_inode_written(dap, inodedep)
3703 struct inodedep *inodedep;
3705 struct pagedep *pagedep;
3707 dap->da_state |= COMPLETE;
3708 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3709 if (dap->da_state & DIRCHG)
3710 pagedep = dap->da_previous->dm_pagedep;
3712 pagedep = dap->da_pagedep;
3713 LIST_REMOVE(dap, da_pdlist);
3714 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3716 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3720 * Handle the completion of a mkdir dependency.
3723 handle_written_mkdir(mkdir, type)
3724 struct mkdir *mkdir;
3728 struct pagedep *pagedep;
3730 if (mkdir->md_state != type) {
3731 lk.lkt_held = NOHOLDER;
3732 panic("handle_written_mkdir: bad type");
3734 dap = mkdir->md_diradd;
3735 dap->da_state &= ~type;
3736 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
3737 dap->da_state |= DEPCOMPLETE;
3738 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3739 if (dap->da_state & DIRCHG)
3740 pagedep = dap->da_previous->dm_pagedep;
3742 pagedep = dap->da_pagedep;
3743 LIST_REMOVE(dap, da_pdlist);
3744 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3746 LIST_REMOVE(mkdir, md_mkdirs);
3747 WORKITEM_FREE(mkdir, D_MKDIR);
3751 * Called from within softdep_disk_write_complete above.
3752 * A write operation was just completed. Removed inodes can
3753 * now be freed and associated block pointers may be committed.
3754 * Note that this routine is always called from interrupt level
3755 * with further splbio interrupts blocked.
3758 handle_written_filepage(pagedep, bp)
3759 struct pagedep *pagedep;
3760 struct buf *bp; /* buffer containing the written page */
3762 struct dirrem *dirrem;
3763 struct diradd *dap, *nextdap;
3767 if ((pagedep->pd_state & IOSTARTED) == 0) {
3768 lk.lkt_held = NOHOLDER;
3769 panic("handle_written_filepage: not started");
3771 pagedep->pd_state &= ~IOSTARTED;
3773 * Process any directory removals that have been committed.
3775 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
3776 LIST_REMOVE(dirrem, dm_next);
3777 dirrem->dm_dirinum = pagedep->pd_ino;
3778 add_to_worklist(&dirrem->dm_list);
3781 * Free any directory additions that have been committed.
3783 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
3786 * Uncommitted directory entries must be restored.
3788 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
3789 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
3791 nextdap = LIST_NEXT(dap, da_pdlist);
3792 if (dap->da_state & ATTACHED) {
3793 lk.lkt_held = NOHOLDER;
3794 panic("handle_written_filepage: attached");
3796 ep = (struct direct *)
3797 ((char *)bp->b_data + dap->da_offset);
3798 ep->d_ino = dap->da_newinum;
3799 dap->da_state &= ~UNDONE;
3800 dap->da_state |= ATTACHED;
3803 * If the inode referenced by the directory has
3804 * been written out, then the dependency can be
3805 * moved to the pending list.
3807 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3808 LIST_REMOVE(dap, da_pdlist);
3809 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
3815 * If there were any rollbacks in the directory, then it must be
3816 * marked dirty so that its will eventually get written back in
3820 if ((bp->b_flags & B_DELWRI) == 0)
3825 * If no dependencies remain, the pagedep will be freed.
3826 * Otherwise it will remain to update the page before it
3827 * is written back to disk.
3829 if (LIST_FIRST(&pagedep->pd_pendinghd) == 0) {
3830 for (i = 0; i < DAHASHSZ; i++)
3831 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) != NULL)
3833 if (i == DAHASHSZ) {
3834 LIST_REMOVE(pagedep, pd_hash);
3835 WORKITEM_FREE(pagedep, D_PAGEDEP);
3843 * Writing back in-core inode structures.
3845 * The filesystem only accesses an inode's contents when it occupies an
3846 * "in-core" inode structure. These "in-core" structures are separate from
3847 * the page frames used to cache inode blocks. Only the latter are
3848 * transferred to/from the disk. So, when the updated contents of the
3849 * "in-core" inode structure are copied to the corresponding in-memory inode
3850 * block, the dependencies are also transferred. The following procedure is
3851 * called when copying a dirty "in-core" inode to a cached inode block.
3855 * Called when an inode is loaded from disk. If the effective link count
3856 * differed from the actual link count when it was last flushed, then we
3857 * need to ensure that the correct effective link count is put back.
3860 softdep_load_inodeblock(ip)
3861 struct inode *ip; /* the "in_core" copy of the inode */
3863 struct inodedep *inodedep;
3866 * Check for alternate nlink count.
3868 ip->i_effnlink = ip->i_nlink;
3870 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3874 ip->i_effnlink -= inodedep->id_nlinkdelta;
3879 * This routine is called just before the "in-core" inode
3880 * information is to be copied to the in-memory inode block.
3881 * Recall that an inode block contains several inodes. If
3882 * the force flag is set, then the dependencies will be
3883 * cleared so that the update can always be made. Note that
3884 * the buffer is locked when this routine is called, so we
3885 * will never be in the middle of writing the inode block
3889 softdep_update_inodeblock(ip, bp, waitfor)
3890 struct inode *ip; /* the "in_core" copy of the inode */
3891 struct buf *bp; /* the buffer containing the inode block */
3892 int waitfor; /* nonzero => update must be allowed */
3894 struct inodedep *inodedep;
3895 struct worklist *wk;
3899 * If the effective link count is not equal to the actual link
3900 * count, then we must track the difference in an inodedep while
3901 * the inode is (potentially) tossed out of the cache. Otherwise,
3902 * if there is no existing inodedep, then there are no dependencies
3906 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3908 if (ip->i_effnlink != ip->i_nlink)
3909 panic("softdep_update_inodeblock: bad link count");
3912 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) {
3914 panic("softdep_update_inodeblock: bad delta");
3917 * Changes have been initiated. Anything depending on these
3918 * changes cannot occur until this inode has been written.
3920 inodedep->id_state &= ~COMPLETE;
3921 if ((inodedep->id_state & ONWORKLIST) == 0)
3922 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
3924 * Any new dependencies associated with the incore inode must
3925 * now be moved to the list associated with the buffer holding
3926 * the in-memory copy of the inode. Once merged process any
3927 * allocdirects that are completed by the merger.
3929 merge_inode_lists(inodedep);
3930 if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL)
3931 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
3933 * Now that the inode has been pushed into the buffer, the
3934 * operations dependent on the inode being written to disk
3935 * can be moved to the id_bufwait so that they will be
3936 * processed when the buffer I/O completes.
3938 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
3939 WORKLIST_REMOVE(wk);
3940 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
3943 * Newly allocated inodes cannot be written until the bitmap
3944 * that allocates them have been written (indicated by
3945 * DEPCOMPLETE being set in id_state). If we are doing a
3946 * forced sync (e.g., an fsync on a file), we force the bitmap
3947 * to be written so that the update can be done.
3949 if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) {
3953 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
3956 (error = VOP_BWRITE(inodedep->id_buf->b_vp, inodedep->id_buf)) != 0)
3957 softdep_error("softdep_update_inodeblock: bwrite", error);
3958 if ((inodedep->id_state & DEPCOMPLETE) == 0)
3959 panic("softdep_update_inodeblock: update failed");
3963 * Merge the new inode dependency list (id_newinoupdt) into the old
3964 * inode dependency list (id_inoupdt). This routine must be called
3965 * with splbio interrupts blocked.
3968 merge_inode_lists(inodedep)
3969 struct inodedep *inodedep;
3971 struct allocdirect *listadp, *newadp;
3973 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
3974 for (listadp = TAILQ_FIRST(&inodedep->id_inoupdt); listadp && newadp;) {
3975 if (listadp->ad_lbn < newadp->ad_lbn) {
3976 listadp = TAILQ_NEXT(listadp, ad_next);
3979 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
3980 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
3981 if (listadp->ad_lbn == newadp->ad_lbn) {
3982 allocdirect_merge(&inodedep->id_inoupdt, newadp,
3986 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
3988 while ((newadp = TAILQ_FIRST(&inodedep->id_newinoupdt)) != NULL) {
3989 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
3990 TAILQ_INSERT_TAIL(&inodedep->id_inoupdt, newadp, ad_next);
3995 * If we are doing an fsync, then we must ensure that any directory
3996 * entries for the inode have been written after the inode gets to disk.
4000 struct vnode *vp; /* the "in_core" copy of the inode */
4002 struct inodedep *inodedep;
4003 struct pagedep *pagedep;
4004 struct worklist *wk;
4011 struct thread *td = curthread; /* XXX */
4012 int error, flushparent;
4019 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) {
4023 if (LIST_FIRST(&inodedep->id_inowait) != NULL ||
4024 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
4025 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
4026 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) {
4028 panic("softdep_fsync: pending ops");
4030 for (error = 0, flushparent = 0; ; ) {
4031 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
4033 if (wk->wk_type != D_DIRADD) {
4035 panic("softdep_fsync: Unexpected type %s",
4036 TYPENAME(wk->wk_type));
4038 dap = WK_DIRADD(wk);
4040 * Flush our parent if this directory entry
4041 * has a MKDIR_PARENT dependency.
4043 if (dap->da_state & DIRCHG)
4044 pagedep = dap->da_previous->dm_pagedep;
4046 pagedep = dap->da_pagedep;
4047 mnt = pagedep->pd_mnt;
4048 parentino = pagedep->pd_ino;
4049 lbn = pagedep->pd_lbn;
4050 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) {
4052 panic("softdep_fsync: dirty");
4054 flushparent = dap->da_state & MKDIR_PARENT;
4056 * If we are being fsync'ed as part of vgone'ing this vnode,
4057 * then we will not be able to release and recover the
4058 * vnode below, so we just have to give up on writing its
4059 * directory entry out. It will eventually be written, just
4060 * not now, but then the user was not asking to have it
4061 * written, so we are not breaking any promises.
4063 if (vp->v_flag & VRECLAIMED)
4066 * We prevent deadlock by always fetching inodes from the
4067 * root, moving down the directory tree. Thus, when fetching
4068 * our parent directory, we must unlock ourselves before
4069 * requesting the lock on our parent. See the comment in
4070 * ufs_lookup for details on possible races.
4073 VOP_UNLOCK(vp, 0, td);
4074 error = VFS_VGET(mnt, parentino, &pvp);
4075 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
4079 if ((error = UFS_UPDATE(pvp, 1)) != 0) {
4085 * Flush directory page containing the inode's name.
4087 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), &bp);
4089 error = VOP_BWRITE(bp->b_vp, bp);
4094 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0)
4102 * Flush all the dirty bitmaps associated with the block device
4103 * before flushing the rest of the dirty blocks so as to reduce
4104 * the number of dependencies that will have to be rolled back.
4106 static int softdep_fsync_mountdev_bp(struct buf *bp, void *data);
4109 softdep_fsync_mountdev(vp)
4112 if (!vn_isdisk(vp, NULL))
4113 panic("softdep_fsync_mountdev: vnode not a disk");
4116 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
4117 softdep_fsync_mountdev_bp, vp);
4119 drain_output(vp, 1);
4124 softdep_fsync_mountdev_bp(struct buf *bp, void *data)
4126 struct worklist *wk;
4127 struct vnode *vp = data;
4130 * If it is already scheduled, skip to the next buffer.
4132 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
4134 if (bp->b_vp != vp || (bp->b_flags & B_DELWRI) == 0) {
4136 printf("softdep_fsync_mountdev_bp: warning, buffer %p ripped out from under vnode %p\n", bp, vp);
4140 * We are only interested in bitmaps with outstanding
4143 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
4144 wk->wk_type != D_BMSAFEMAP ||
4145 (bp->b_xflags & BX_BKGRDINPROG)) {
4157 * This routine is called when we are trying to synchronously flush a
4158 * file. This routine must eliminate any filesystem metadata dependencies
4159 * so that the syncing routine can succeed by pushing the dirty blocks
4160 * associated with the file. If any I/O errors occur, they are returned.
4162 struct softdep_sync_metadata_info {
4167 static int softdep_sync_metadata_bp(struct buf *bp, void *data);
4170 softdep_sync_metadata(struct vnode *vp, struct thread *td)
4172 struct softdep_sync_metadata_info info;
4176 * Check whether this vnode is involved in a filesystem
4177 * that is doing soft dependency processing.
4179 if (!vn_isdisk(vp, NULL)) {
4180 if (!DOINGSOFTDEP(vp))
4183 if (vp->v_rdev->si_mountpoint == NULL ||
4184 (vp->v_rdev->si_mountpoint->mnt_flag & MNT_SOFTDEP) == 0)
4187 * Ensure that any direct block dependencies have been cleared.
4190 if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) {
4195 * For most files, the only metadata dependencies are the
4196 * cylinder group maps that allocate their inode or blocks.
4197 * The block allocation dependencies can be found by traversing
4198 * the dependency lists for any buffers that remain on their
4199 * dirty buffer list. The inode allocation dependency will
4200 * be resolved when the inode is updated with MNT_WAIT.
4201 * This work is done in two passes. The first pass grabs most
4202 * of the buffers and begins asynchronously writing them. The
4203 * only way to wait for these asynchronous writes is to sleep
4204 * on the filesystem vnode which may stay busy for a long time
4205 * if the filesystem is active. So, instead, we make a second
4206 * pass over the dependencies blocking on each write. In the
4207 * usual case we will be blocking against a write that we
4208 * initiated, so when it is done the dependency will have been
4209 * resolved. Thus the second pass is expected to end quickly.
4211 waitfor = MNT_NOWAIT;
4214 * We must wait for any I/O in progress to finish so that
4215 * all potential buffers on the dirty list will be visible.
4217 drain_output(vp, 1);
4219 info.waitfor = waitfor;
4221 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
4222 softdep_sync_metadata_bp, &info);
4226 return(-error); /* error code */
4230 * The brief unlock is to allow any pent up dependency
4231 * processing to be done. Then proceed with the second pass.
4233 if (waitfor == MNT_NOWAIT) {
4241 * If we have managed to get rid of all the dirty buffers,
4242 * then we are done. For certain directories and block
4243 * devices, we may need to do further work.
4245 * We must wait for any I/O in progress to finish so that
4246 * all potential buffers on the dirty list will be visible.
4248 drain_output(vp, 1);
4249 if (RB_EMPTY(&vp->v_rbdirty_tree)) {
4256 * If we are trying to sync a block device, some of its buffers may
4257 * contain metadata that cannot be written until the contents of some
4258 * partially written files have been written to disk. The only easy
4259 * way to accomplish this is to sync the entire filesystem (luckily
4260 * this happens rarely).
4262 if (vn_isdisk(vp, NULL) &&
4264 vp->v_rdev->si_mountpoint && !VOP_ISLOCKED(vp, NULL) &&
4265 (error = VFS_SYNC(vp->v_rdev->si_mountpoint, MNT_WAIT, td)) != 0)
4271 softdep_sync_metadata_bp(struct buf *bp, void *data)
4273 struct softdep_sync_metadata_info *info = data;
4274 struct pagedep *pagedep;
4275 struct allocdirect *adp;
4276 struct allocindir *aip;
4277 struct worklist *wk;
4282 if (getdirtybuf(&bp, MNT_WAIT) == 0) {
4283 printf("softdep_sync_metadata_bp(1): caught buf %p going away\n", bp);
4286 if (bp->b_vp != info->vp || (bp->b_flags & B_DELWRI) == 0) {
4287 printf("softdep_sync_metadata_bp(2): caught buf %p going away vp %p\n", bp, info->vp);
4293 * As we hold the buffer locked, none of its dependencies
4296 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4297 switch (wk->wk_type) {
4300 adp = WK_ALLOCDIRECT(wk);
4301 if (adp->ad_state & DEPCOMPLETE)
4304 if (getdirtybuf(&nbp, info->waitfor) == 0)
4307 if (info->waitfor == MNT_NOWAIT) {
4309 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4318 aip = WK_ALLOCINDIR(wk);
4319 if (aip->ai_state & DEPCOMPLETE)
4322 if (getdirtybuf(&nbp, info->waitfor) == 0)
4325 if (info->waitfor == MNT_NOWAIT) {
4327 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4338 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
4339 if (aip->ai_state & DEPCOMPLETE)
4342 if (getdirtybuf(&nbp, MNT_WAIT) == 0)
4345 if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4356 if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs,
4357 WK_INODEDEP(wk)->id_ino)) != 0) {
4367 * We are trying to sync a directory that may
4368 * have dependencies on both its own metadata
4369 * and/or dependencies on the inodes of any
4370 * recently allocated files. We walk its diradd
4371 * lists pushing out the associated inode.
4373 pagedep = WK_PAGEDEP(wk);
4374 for (i = 0; i < DAHASHSZ; i++) {
4375 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
4378 flush_pagedep_deps(info->vp,
4380 &pagedep->pd_diraddhd[i]))) {
4391 * This case should never happen if the vnode has
4392 * been properly sync'ed. However, if this function
4393 * is used at a place where the vnode has not yet
4394 * been sync'ed, this dependency can show up. So,
4395 * rather than panic, just flush it.
4397 nbp = WK_MKDIR(wk)->md_buf;
4398 if (getdirtybuf(&nbp, info->waitfor) == 0)
4401 if (info->waitfor == MNT_NOWAIT) {
4403 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4413 * This case should never happen if the vnode has
4414 * been properly sync'ed. However, if this function
4415 * is used at a place where the vnode has not yet
4416 * been sync'ed, this dependency can show up. So,
4417 * rather than panic, just flush it.
4419 * nbp can wind up == bp if a device node for the
4420 * same filesystem is being fsynced at the same time,
4421 * leading to a panic if we don't catch the case.
4423 nbp = WK_BMSAFEMAP(wk)->sm_buf;
4426 if (getdirtybuf(&nbp, info->waitfor) == 0)
4429 if (info->waitfor == MNT_NOWAIT) {
4431 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4441 panic("softdep_sync_metadata: Unknown type %s",
4442 TYPENAME(wk->wk_type));
4453 * Flush the dependencies associated with an inodedep.
4454 * Called with splbio blocked.
4457 flush_inodedep_deps(fs, ino)
4461 struct inodedep *inodedep;
4462 struct allocdirect *adp;
4467 * This work is done in two passes. The first pass grabs most
4468 * of the buffers and begins asynchronously writing them. The
4469 * only way to wait for these asynchronous writes is to sleep
4470 * on the filesystem vnode which may stay busy for a long time
4471 * if the filesystem is active. So, instead, we make a second
4472 * pass over the dependencies blocking on each write. In the
4473 * usual case we will be blocking against a write that we
4474 * initiated, so when it is done the dependency will have been
4475 * resolved. Thus the second pass is expected to end quickly.
4476 * We give a brief window at the top of the loop to allow
4477 * any pending I/O to complete.
4479 for (waitfor = MNT_NOWAIT; ; ) {
4482 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4484 TAILQ_FOREACH(adp, &inodedep->id_inoupdt, ad_next) {
4485 if (adp->ad_state & DEPCOMPLETE)
4488 if (getdirtybuf(&bp, waitfor) == 0) {
4489 if (waitfor == MNT_NOWAIT)
4494 if (waitfor == MNT_NOWAIT) {
4496 } else if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) {
4505 TAILQ_FOREACH(adp, &inodedep->id_newinoupdt, ad_next) {
4506 if (adp->ad_state & DEPCOMPLETE)
4509 if (getdirtybuf(&bp, waitfor) == 0) {
4510 if (waitfor == MNT_NOWAIT)
4515 if (waitfor == MNT_NOWAIT) {
4517 } else if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) {
4527 * If pass2, we are done, otherwise do pass 2.
4529 if (waitfor == MNT_WAIT)
4534 * Try freeing inodedep in case all dependencies have been removed.
4536 if (inodedep_lookup(fs, ino, 0, &inodedep) != 0)
4537 (void) free_inodedep(inodedep);
4542 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
4543 * Called with splbio blocked.
4546 flush_pagedep_deps(pvp, mp, diraddhdp)
4549 struct diraddhd *diraddhdp;
4551 struct thread *td = curthread; /* XXX */
4552 struct inodedep *inodedep;
4553 struct ufsmount *ump;
4556 int gotit, error = 0;
4561 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
4563 * Flush ourselves if this directory entry
4564 * has a MKDIR_PARENT dependency.
4566 if (dap->da_state & MKDIR_PARENT) {
4568 if ((error = UFS_UPDATE(pvp, 1)) != 0)
4572 * If that cleared dependencies, go on to next.
4574 if (dap != LIST_FIRST(diraddhdp))
4576 if (dap->da_state & MKDIR_PARENT) {
4578 panic("flush_pagedep_deps: MKDIR_PARENT");
4582 * A newly allocated directory must have its "." and
4583 * ".." entries written out before its name can be
4584 * committed in its parent. We do not want or need
4585 * the full semantics of a synchronous VOP_FSYNC as
4586 * that may end up here again, once for each directory
4587 * level in the filesystem. Instead, we push the blocks
4588 * and wait for them to clear. We have to fsync twice
4589 * because the first call may choose to defer blocks
4590 * that still have dependencies, but deferral will
4591 * happen at most once.
4593 inum = dap->da_newinum;
4594 if (dap->da_state & MKDIR_BODY) {
4596 if ((error = VFS_VGET(mp, inum, &vp)) != 0)
4598 if ((error=VOP_FSYNC(vp, MNT_NOWAIT, td)) ||
4599 (error=VOP_FSYNC(vp, MNT_NOWAIT, td))) {
4603 drain_output(vp, 0);
4607 * If that cleared dependencies, go on to next.
4609 if (dap != LIST_FIRST(diraddhdp))
4611 if (dap->da_state & MKDIR_BODY) {
4613 panic("flush_pagedep_deps: MKDIR_BODY");
4617 * Flush the inode on which the directory entry depends.
4618 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
4619 * the only remaining dependency is that the updated inode
4620 * count must get pushed to disk. The inode has already
4621 * been pushed into its inode buffer (via VOP_UPDATE) at
4622 * the time of the reference count change. So we need only
4623 * locate that buffer, ensure that there will be no rollback
4624 * caused by a bitmap dependency, then write the inode buffer.
4626 if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0) {
4628 panic("flush_pagedep_deps: lost inode");
4631 * If the inode still has bitmap dependencies,
4632 * push them to disk.
4634 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4635 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
4638 (error = VOP_BWRITE(inodedep->id_buf->b_vp,
4639 inodedep->id_buf)) != 0)
4642 if (dap != LIST_FIRST(diraddhdp))
4646 * If the inode is still sitting in a buffer waiting
4647 * to be written, push it to disk.
4650 if ((error = bread(ump->um_devvp,
4651 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
4652 (int)ump->um_fs->fs_bsize, &bp)) != 0)
4654 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
4658 * If we have failed to get rid of all the dependencies
4659 * then something is seriously wrong.
4661 if (dap == LIST_FIRST(diraddhdp)) {
4663 panic("flush_pagedep_deps: flush failed");
4672 * A large burst of file addition or deletion activity can drive the
4673 * memory load excessively high. First attempt to slow things down
4674 * using the techniques below. If that fails, this routine requests
4675 * the offending operations to fall back to running synchronously
4676 * until the memory load returns to a reasonable level.
4679 softdep_slowdown(vp)
4682 int max_softdeps_hard;
4684 max_softdeps_hard = max_softdeps * 11 / 10;
4685 if (num_dirrem < max_softdeps_hard / 2 &&
4686 num_inodedep < max_softdeps_hard)
4688 stat_sync_limit_hit += 1;
4693 * If memory utilization has gotten too high, deliberately slow things
4694 * down and speed up the I/O processing.
4697 request_cleanup(resource, islocked)
4701 struct thread *td = curthread; /* XXX */
4704 * We never hold up the filesystem syncer process.
4706 if (td == filesys_syncer)
4709 * First check to see if the work list has gotten backlogged.
4710 * If it has, co-opt this process to help clean up two entries.
4711 * Because this process may hold inodes locked, we cannot
4712 * handle any remove requests that might block on a locked
4713 * inode as that could lead to deadlock.
4715 if (num_on_worklist > max_softdeps / 10) {
4718 process_worklist_item(NULL, LK_NOWAIT);
4719 process_worklist_item(NULL, LK_NOWAIT);
4720 stat_worklist_push += 2;
4727 * If we are resource constrained on inode dependencies, try
4728 * flushing some dirty inodes. Otherwise, we are constrained
4729 * by file deletions, so try accelerating flushes of directories
4730 * with removal dependencies. We would like to do the cleanup
4731 * here, but we probably hold an inode locked at this point and
4732 * that might deadlock against one that we try to clean. So,
4733 * the best that we can do is request the syncer daemon to do
4734 * the cleanup for us.
4739 stat_ino_limit_push += 1;
4740 req_clear_inodedeps += 1;
4741 stat_countp = &stat_ino_limit_hit;
4745 stat_blk_limit_push += 1;
4746 req_clear_remove += 1;
4747 stat_countp = &stat_blk_limit_hit;
4753 panic("request_cleanup: unknown type");
4756 * Hopefully the syncer daemon will catch up and awaken us.
4757 * We wait at most tickdelay before proceeding in any case.
4763 if (!callout_active(&handle))
4764 callout_reset(&handle, tickdelay > 2 ? tickdelay : 2,
4766 interlocked_sleep(&lk, SLEEP, (caddr_t)&proc_waiting, 0,
4776 * Awaken processes pausing in request_cleanup and clear proc_waiting
4777 * to indicate that there is no longer a timer running.
4784 wakeup_one(&proc_waiting);
4785 if (proc_waiting > 0)
4786 callout_reset(&handle, tickdelay > 2 ? tickdelay : 2,
4789 callout_deactivate(&handle);
4793 * Flush out a directory with at least one removal dependency in an effort to
4794 * reduce the number of dirrem, freefile, and freeblks dependency structures.
4797 clear_remove(struct thread *td)
4799 struct pagedep_hashhead *pagedephd;
4800 struct pagedep *pagedep;
4801 static int next = 0;
4808 for (cnt = 0; cnt < pagedep_hash; cnt++) {
4809 pagedephd = &pagedep_hashtbl[next++];
4810 if (next >= pagedep_hash)
4812 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
4813 if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL)
4815 mp = pagedep->pd_mnt;
4816 ino = pagedep->pd_ino;
4818 if ((error = VFS_VGET(mp, ino, &vp)) != 0) {
4819 softdep_error("clear_remove: vget", error);
4822 if ((error = VOP_FSYNC(vp, MNT_NOWAIT, td)))
4823 softdep_error("clear_remove: fsync", error);
4824 drain_output(vp, 0);
4833 * Clear out a block of dirty inodes in an effort to reduce
4834 * the number of inodedep dependency structures.
4836 struct clear_inodedeps_info {
4842 clear_inodedeps_mountlist_callback(struct mount *mp, void *data)
4844 struct clear_inodedeps_info *info = data;
4846 if ((mp->mnt_flag & MNT_SOFTDEP) && info->fs == VFSTOUFS(mp)->um_fs) {
4854 clear_inodedeps(struct thread *td)
4856 struct clear_inodedeps_info info;
4857 struct inodedep_hashhead *inodedephd;
4858 struct inodedep *inodedep;
4859 static int next = 0;
4863 ino_t firstino, lastino, ino;
4867 * Pick a random inode dependency to be cleared.
4868 * We will then gather up all the inodes in its block
4869 * that have dependencies and flush them out.
4871 for (cnt = 0; cnt < inodedep_hash; cnt++) {
4872 inodedephd = &inodedep_hashtbl[next++];
4873 if (next >= inodedep_hash)
4875 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
4878 if (inodedep == NULL) {
4883 * Ugly code to find mount point given pointer to superblock.
4885 fs = inodedep->id_fs;
4888 mountlist_scan(clear_inodedeps_mountlist_callback,
4889 &info, MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
4891 * Find the last inode in the block with dependencies.
4893 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
4894 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
4895 if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
4898 * Asynchronously push all but the last inode with dependencies.
4899 * Synchronously push the last inode with dependencies to ensure
4900 * that the inode block gets written to free up the inodedeps.
4902 for (ino = firstino; ino <= lastino; ino++) {
4903 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4906 if ((error = VFS_VGET(info.mp, ino, &vp)) != 0) {
4907 softdep_error("clear_inodedeps: vget", error);
4910 if (ino == lastino) {
4911 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)))
4912 softdep_error("clear_inodedeps: fsync1", error);
4914 if ((error = VOP_FSYNC(vp, MNT_NOWAIT, td)))
4915 softdep_error("clear_inodedeps: fsync2", error);
4916 drain_output(vp, 0);
4925 * Function to determine if the buffer has outstanding dependencies
4926 * that will cause a roll-back if the buffer is written. If wantcount
4927 * is set, return number of dependencies, otherwise just yes or no.
4930 softdep_count_dependencies(bp, wantcount)
4934 struct worklist *wk;
4935 struct inodedep *inodedep;
4936 struct indirdep *indirdep;
4937 struct allocindir *aip;
4938 struct pagedep *pagedep;
4944 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4945 switch (wk->wk_type) {
4948 inodedep = WK_INODEDEP(wk);
4949 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4950 /* bitmap allocation dependency */
4955 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
4956 /* direct block pointer dependency */
4964 indirdep = WK_INDIRDEP(wk);
4966 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
4967 /* indirect block pointer dependency */
4975 pagedep = WK_PAGEDEP(wk);
4976 for (i = 0; i < DAHASHSZ; i++) {
4978 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
4979 /* directory entry dependency */
4991 /* never a dependency on these blocks */
4996 panic("softdep_check_for_rollback: Unexpected type %s",
4997 TYPENAME(wk->wk_type));
5007 * Acquire exclusive access to a buffer.
5008 * Must be called with splbio blocked.
5009 * Return 1 if buffer was acquired.
5012 getdirtybuf(bpp, waitfor)
5020 if ((bp = *bpp) == NULL)
5022 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
5023 if ((bp->b_xflags & BX_BKGRDINPROG) == 0)
5026 if (waitfor != MNT_WAIT)
5028 bp->b_xflags |= BX_BKGRDWAIT;
5029 interlocked_sleep(&lk, SLEEP, &bp->b_xflags, 0,
5033 if (waitfor != MNT_WAIT)
5035 error = interlocked_sleep(&lk, LOCKBUF, bp,
5036 LK_EXCLUSIVE | LK_SLEEPFAIL, 0, 0);
5037 if (error != ENOLCK) {
5039 panic("getdirtybuf: inconsistent lock");
5042 if ((bp->b_flags & B_DELWRI) == 0) {
5051 * Wait for pending output on a vnode to complete.
5052 * Must be called with vnode locked.
5055 drain_output(vp, islocked)
5062 while (vp->v_numoutput) {
5063 vp->v_flag |= VBWAIT;
5064 interlocked_sleep(&lk, SLEEP, (caddr_t)&vp->v_numoutput,
5072 * Called whenever a buffer that is being invalidated or reallocated
5073 * contains dependencies. This should only happen if an I/O error has
5074 * occurred. The routine is called with the buffer locked.
5077 softdep_deallocate_dependencies(bp)
5081 if ((bp->b_flags & B_ERROR) == 0)
5082 panic("softdep_deallocate_dependencies: dangling deps");
5083 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntfromname, bp->b_error);
5084 panic("softdep_deallocate_dependencies: unrecovered I/O error");
5088 * Function to handle asynchronous write errors in the filesystem.
5091 softdep_error(func, error)
5096 /* XXX should do something better! */
5097 printf("%s: got error %d while accessing filesystem\n", func, error);