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.54 2007/11/07 00:46:39 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 *, off_t, int, ufs_lbn_t, long *);
171 static void deallocate_dependencies(struct buf *, struct inodedep *);
172 static void free_allocdirect(struct allocdirectlst *,
173 struct allocdirect *, int);
174 static int check_inode_unwritten(struct inodedep *);
175 static int free_inodedep(struct inodedep *);
176 static void handle_workitem_freeblocks(struct freeblks *);
177 static void merge_inode_lists(struct inodedep *);
178 static void setup_allocindir_phase2(struct buf *, struct inode *,
179 struct allocindir *);
180 static struct allocindir *newallocindir(struct inode *, int, ufs_daddr_t,
182 static void handle_workitem_freefrag(struct freefrag *);
183 static struct freefrag *newfreefrag(struct inode *, ufs_daddr_t, long);
184 static void allocdirect_merge(struct allocdirectlst *,
185 struct allocdirect *, struct allocdirect *);
186 static struct bmsafemap *bmsafemap_lookup(struct buf *);
187 static int newblk_lookup(struct fs *, ufs_daddr_t, int,
189 static int inodedep_lookup(struct fs *, ino_t, int, struct inodedep **);
190 static int pagedep_lookup(struct inode *, ufs_lbn_t, int,
192 static void pause_timer(void *);
193 static int request_cleanup(int, int);
194 static int process_worklist_item(struct mount *, int);
195 static void add_to_worklist(struct worklist *);
198 * Exported softdep operations.
200 static void softdep_disk_io_initiation(struct buf *);
201 static void softdep_disk_write_complete(struct buf *);
202 static void softdep_deallocate_dependencies(struct buf *);
203 static int softdep_fsync(struct vnode *);
204 static int softdep_process_worklist(struct mount *);
205 static void softdep_move_dependencies(struct buf *, struct buf *);
206 static int softdep_count_dependencies(struct buf *bp, int);
207 static int softdep_checkread(struct buf *bp);
208 static int softdep_checkwrite(struct buf *bp);
210 static struct bio_ops softdep_bioops = {
211 .io_start = softdep_disk_io_initiation,
212 .io_complete = softdep_disk_write_complete,
213 .io_deallocate = softdep_deallocate_dependencies,
214 .io_fsync = softdep_fsync,
215 .io_sync = softdep_process_worklist,
216 .io_movedeps = softdep_move_dependencies,
217 .io_countdeps = softdep_count_dependencies,
218 .io_checkread = softdep_checkread,
219 .io_checkwrite = softdep_checkwrite
223 * Locking primitives.
225 * For a uniprocessor, all we need to do is protect against disk
226 * interrupts. For a multiprocessor, this lock would have to be
227 * a mutex. A single mutex is used throughout this file, though
228 * finer grain locking could be used if contention warranted it.
230 * For a multiprocessor, the sleep call would accept a lock and
231 * release it after the sleep processing was complete. In a uniprocessor
232 * implementation there is no such interlock, so we simple mark
233 * the places where it needs to be done with the `interlocked' form
234 * of the lock calls. Since the uniprocessor sleep already interlocks
235 * the spl, there is nothing that really needs to be done.
237 #ifndef /* NOT */ DEBUG
238 static struct lockit {
240 #define ACQUIRE_LOCK(lk) crit_enter_id("softupdates");
241 #define FREE_LOCK(lk) crit_exit_id("softupdates");
244 #define NOHOLDER ((struct thread *)-1)
245 #define SPECIAL_FLAG ((struct thread *)-2)
246 static struct lockit {
248 struct thread *lkt_held;
249 } lk = { 0, NOHOLDER };
252 static void acquire_lock(struct lockit *);
253 static void free_lock(struct lockit *);
254 void softdep_panic(char *);
256 #define ACQUIRE_LOCK(lk) acquire_lock(lk)
257 #define FREE_LOCK(lk) free_lock(lk)
260 acquire_lock(struct lockit *lk)
264 if (lk->lkt_held != NOHOLDER) {
265 holder = lk->lkt_held;
267 if (holder == curthread)
268 panic("softdep_lock: locking against myself");
270 panic("softdep_lock: lock held by %p", holder);
272 crit_enter_id("softupdates");
273 lk->lkt_held = curthread;
278 free_lock(struct lockit *lk)
281 if (lk->lkt_held == NOHOLDER)
282 panic("softdep_unlock: lock not held");
283 lk->lkt_held = NOHOLDER;
284 crit_exit_id("softupdates");
288 * Function to release soft updates lock and panic.
291 softdep_panic(char *msg)
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(struct lockit *lk, int op, void *ident, int flags,
316 const char *wmesg, int timo)
323 if (lk->lkt_held == NOHOLDER)
324 panic("interlocked_sleep: lock not held");
325 lk->lkt_held = NOHOLDER;
329 retval = tsleep(ident, flags, wmesg, timo);
332 retval = BUF_LOCK((struct buf *)ident, flags);
335 panic("interlocked_sleep: unknown operation");
338 if (lk->lkt_held != NOHOLDER) {
339 holder = lk->lkt_held;
341 if (holder == curthread)
342 panic("interlocked_sleep: locking against self");
344 panic("interlocked_sleep: lock held by %p", holder);
346 lk->lkt_held = curthread;
354 * Place holder for real semaphores.
363 static void sema_init(struct sema *, char *, int, int);
364 static int sema_get(struct sema *, struct lockit *);
365 static void sema_release(struct sema *);
368 sema_init(struct sema *semap, char *name, int prio, int timo)
371 semap->holder = NOHOLDER;
379 sema_get(struct sema *semap, struct lockit *interlock)
382 if (semap->value++ > 0) {
383 if (interlock != NULL) {
384 interlocked_sleep(interlock, SLEEP, (caddr_t)semap,
385 semap->prio, semap->name, semap->timo);
386 FREE_LOCK(interlock);
388 tsleep((caddr_t)semap, semap->prio, semap->name,
393 semap->holder = curthread;
394 if (interlock != NULL)
395 FREE_LOCK(interlock);
400 sema_release(struct sema *semap)
403 if (semap->value <= 0 || semap->holder != curthread) {
404 if (lk.lkt_held != NOHOLDER)
406 panic("sema_release: not held");
408 if (--semap->value > 0) {
412 semap->holder = NOHOLDER;
416 * Worklist queue management.
417 * These routines require that the lock be held.
419 #ifndef /* NOT */ DEBUG
420 #define WORKLIST_INSERT(head, item) do { \
421 (item)->wk_state |= ONWORKLIST; \
422 LIST_INSERT_HEAD(head, item, wk_list); \
425 #define WORKLIST_INSERT_BP(bp, item) do { \
426 (item)->wk_state |= ONWORKLIST; \
427 (bp)->b_ops = &softdep_bioops; \
428 LIST_INSERT_HEAD(&(bp)->b_dep, item, wk_list); \
431 #define WORKLIST_REMOVE(item) do { \
432 (item)->wk_state &= ~ONWORKLIST; \
433 LIST_REMOVE(item, wk_list); \
436 #define WORKITEM_FREE(item, type) FREE(item, DtoM(type))
439 static void worklist_insert(struct workhead *, struct worklist *);
440 static void worklist_remove(struct worklist *);
441 static void workitem_free(struct worklist *, int);
443 #define WORKLIST_INSERT_BP(bp, item) do { \
444 (bp)->b_ops = &softdep_bioops; \
445 worklist_insert(&(bp)->b_dep, item); \
448 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
449 #define WORKLIST_REMOVE(item) worklist_remove(item)
450 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)item, type)
453 worklist_insert(struct workhead *head, 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(struct worklist *item)
470 if (lk.lkt_held == NOHOLDER)
471 panic("worklist_remove: lock not held");
472 if ((item->wk_state & ONWORKLIST) == 0) {
474 panic("worklist_remove: not on list");
476 item->wk_state &= ~ONWORKLIST;
477 LIST_REMOVE(item, wk_list);
481 workitem_free(struct worklist *item, int type)
484 if (item->wk_state & ONWORKLIST) {
485 if (lk.lkt_held != NOHOLDER)
487 panic("workitem_free: still on list");
489 if (item->wk_type != type) {
490 if (lk.lkt_held != NOHOLDER)
492 panic("workitem_free: type mismatch");
494 FREE(item, DtoM(type));
499 * Workitem queue management
501 static struct workhead softdep_workitem_pending;
502 static int num_on_worklist; /* number of worklist items to be processed */
503 static int softdep_worklist_busy; /* 1 => trying to do unmount */
504 static int softdep_worklist_req; /* serialized waiters */
505 static int max_softdeps; /* maximum number of structs before slowdown */
506 static int tickdelay = 2; /* number of ticks to pause during slowdown */
507 static int *stat_countp; /* statistic to count in proc_waiting timeout */
508 static int proc_waiting; /* tracks whether we have a timeout posted */
509 static struct callout handle; /* handle on posted proc_waiting timeout */
510 static struct thread *filesys_syncer; /* proc of filesystem syncer process */
511 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
512 #define FLUSH_INODES 1
513 static int req_clear_remove; /* syncer process flush some freeblks */
514 #define FLUSH_REMOVE 2
518 static int stat_worklist_push; /* number of worklist cleanups */
519 static int stat_blk_limit_push; /* number of times block limit neared */
520 static int stat_ino_limit_push; /* number of times inode limit neared */
521 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
522 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
523 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
524 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
525 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
526 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
527 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
530 #include <sys/sysctl.h>
531 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
532 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
533 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
534 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
535 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
536 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
537 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
538 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
539 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
540 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
541 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
542 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
546 * Add an item to the end of the work queue.
547 * This routine requires that the lock be held.
548 * This is the only routine that adds items to the list.
549 * The following routine is the only one that removes items
550 * and does so in order from first to last.
553 add_to_worklist(struct worklist *wk)
555 static struct worklist *worklist_tail;
557 if (wk->wk_state & ONWORKLIST) {
558 if (lk.lkt_held != NOHOLDER)
560 panic("add_to_worklist: already on list");
562 wk->wk_state |= ONWORKLIST;
563 if (LIST_FIRST(&softdep_workitem_pending) == NULL)
564 LIST_INSERT_HEAD(&softdep_workitem_pending, wk, wk_list);
566 LIST_INSERT_AFTER(worklist_tail, wk, wk_list);
568 num_on_worklist += 1;
572 * Process that runs once per second to handle items in the background queue.
574 * Note that we ensure that everything is done in the order in which they
575 * appear in the queue. The code below depends on this property to ensure
576 * that blocks of a file are freed before the inode itself is freed. This
577 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
578 * until all the old ones have been purged from the dependency lists.
581 softdep_process_worklist(struct mount *matchmnt)
583 thread_t td = curthread;
584 int matchcnt, loopcount;
588 * Record the process identifier of our caller so that we can give
589 * this process preferential treatment in request_cleanup below.
595 * There is no danger of having multiple processes run this
596 * code, but we have to single-thread it when softdep_flushfiles()
597 * is in operation to get an accurate count of the number of items
598 * related to its mount point that are in the list.
600 if (matchmnt == NULL) {
601 if (softdep_worklist_busy < 0)
603 softdep_worklist_busy += 1;
607 * If requested, try removing inode or removal dependencies.
609 if (req_clear_inodedeps) {
611 req_clear_inodedeps -= 1;
612 wakeup_one(&proc_waiting);
614 if (req_clear_remove) {
616 req_clear_remove -= 1;
617 wakeup_one(&proc_waiting);
620 starttime = time_second;
621 while (num_on_worklist > 0) {
622 matchcnt += process_worklist_item(matchmnt, 0);
625 * If a umount operation wants to run the worklist
628 if (softdep_worklist_req && matchmnt == NULL) {
634 * If requested, try removing inode or removal dependencies.
636 if (req_clear_inodedeps) {
638 req_clear_inodedeps -= 1;
639 wakeup_one(&proc_waiting);
641 if (req_clear_remove) {
643 req_clear_remove -= 1;
644 wakeup_one(&proc_waiting);
647 * We do not generally want to stop for buffer space, but if
648 * we are really being a buffer hog, we will stop and wait.
650 if (loopcount++ % 128 == 0)
653 * Never allow processing to run for more than one
654 * second. Otherwise the other syncer tasks may get
655 * excessively backlogged.
657 if (starttime != time_second && matchmnt == NULL) {
662 if (matchmnt == NULL) {
663 --softdep_worklist_busy;
664 if (softdep_worklist_req && softdep_worklist_busy == 0)
665 wakeup(&softdep_worklist_req);
671 * Process one item on the worklist.
674 process_worklist_item(struct mount *matchmnt, int flags)
677 struct dirrem *dirrem;
683 if (matchmnt != NULL)
684 matchfs = VFSTOUFS(matchmnt)->um_fs;
687 * Normally we just process each item on the worklist in order.
688 * However, if we are in a situation where we cannot lock any
689 * inodes, we have to skip over any dirrem requests whose
690 * vnodes are resident and locked.
692 LIST_FOREACH(wk, &softdep_workitem_pending, wk_list) {
693 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
695 dirrem = WK_DIRREM(wk);
696 vp = ufs_ihashlookup(VFSTOUFS(dirrem->dm_mnt)->um_dev,
698 if (vp == NULL || !vn_islocked(vp))
706 num_on_worklist -= 1;
708 switch (wk->wk_type) {
711 /* removal of a directory entry */
712 if (WK_DIRREM(wk)->dm_mnt == matchmnt)
714 handle_workitem_remove(WK_DIRREM(wk));
718 /* releasing blocks and/or fragments from a file */
719 if (WK_FREEBLKS(wk)->fb_fs == matchfs)
721 handle_workitem_freeblocks(WK_FREEBLKS(wk));
725 /* releasing a fragment when replaced as a file grows */
726 if (WK_FREEFRAG(wk)->ff_fs == matchfs)
728 handle_workitem_freefrag(WK_FREEFRAG(wk));
732 /* releasing an inode when its link count drops to 0 */
733 if (WK_FREEFILE(wk)->fx_fs == matchfs)
735 handle_workitem_freefile(WK_FREEFILE(wk));
739 panic("%s_process_worklist: Unknown type %s",
740 "softdep", TYPENAME(wk->wk_type));
747 * Move dependencies from one buffer to another.
750 softdep_move_dependencies(struct buf *oldbp, struct buf *newbp)
752 struct worklist *wk, *wktail;
754 if (LIST_FIRST(&newbp->b_dep) != NULL)
755 panic("softdep_move_dependencies: need merge code");
758 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
759 LIST_REMOVE(wk, wk_list);
761 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
763 LIST_INSERT_AFTER(wktail, wk, wk_list);
765 newbp->b_ops = &softdep_bioops;
771 * Purge the work list of all items associated with a particular mount point.
774 softdep_flushfiles(struct mount *oldmnt, int flags)
780 * Await our turn to clear out the queue, then serialize access.
782 while (softdep_worklist_busy != 0) {
783 softdep_worklist_req += 1;
784 tsleep(&softdep_worklist_req, 0, "softflush", 0);
785 softdep_worklist_req -= 1;
787 softdep_worklist_busy = -1;
789 if ((error = ffs_flushfiles(oldmnt, flags)) != 0) {
790 softdep_worklist_busy = 0;
791 if (softdep_worklist_req)
792 wakeup(&softdep_worklist_req);
796 * Alternately flush the block device associated with the mount
797 * point and process any dependencies that the flushing
798 * creates. In theory, this loop can happen at most twice,
799 * but we give it a few extra just to be sure.
801 devvp = VFSTOUFS(oldmnt)->um_devvp;
802 for (loopcnt = 10; loopcnt > 0; ) {
803 if (softdep_process_worklist(oldmnt) == 0) {
806 * Do another flush in case any vnodes were brought in
807 * as part of the cleanup operations.
809 if ((error = ffs_flushfiles(oldmnt, flags)) != 0)
812 * If we still found nothing to do, we are really done.
814 if (softdep_process_worklist(oldmnt) == 0)
817 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
818 error = VOP_FSYNC(devvp, MNT_WAIT);
823 softdep_worklist_busy = 0;
824 if (softdep_worklist_req)
825 wakeup(&softdep_worklist_req);
828 * If we are unmounting then it is an error to fail. If we
829 * are simply trying to downgrade to read-only, then filesystem
830 * activity can keep us busy forever, so we just fail with EBUSY.
833 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
834 panic("softdep_flushfiles: looping");
843 * There are three types of structures that can be looked up:
844 * 1) pagedep structures identified by mount point, inode number,
846 * 2) inodedep structures identified by mount point and inode number.
847 * 3) newblk structures identified by mount point and
848 * physical block number.
850 * The "pagedep" and "inodedep" dependency structures are hashed
851 * separately from the file blocks and inodes to which they correspond.
852 * This separation helps when the in-memory copy of an inode or
853 * file block must be replaced. It also obviates the need to access
854 * an inode or file page when simply updating (or de-allocating)
855 * dependency structures. Lookup of newblk structures is needed to
856 * find newly allocated blocks when trying to associate them with
857 * their allocdirect or allocindir structure.
859 * The lookup routines optionally create and hash a new instance when
860 * an existing entry is not found.
862 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
863 #define NODELAY 0x0002 /* cannot do background work */
866 * Structures and routines associated with pagedep caching.
868 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
869 u_long pagedep_hash; /* size of hash table - 1 */
870 #define PAGEDEP_HASH(mp, inum, lbn) \
871 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
873 static struct sema pagedep_in_progress;
876 * Helper routine for pagedep_lookup()
880 pagedep_find(struct pagedep_hashhead *pagedephd, ino_t ino, ufs_lbn_t lbn,
883 struct pagedep *pagedep;
885 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
886 if (ino == pagedep->pd_ino &&
887 lbn == pagedep->pd_lbn &&
888 mp == pagedep->pd_mnt) {
896 * Look up a pagedep. Return 1 if found, 0 if not found.
897 * If not found, allocate if DEPALLOC flag is passed.
898 * Found or allocated entry is returned in pagedeppp.
899 * This routine must be called with splbio interrupts blocked.
902 pagedep_lookup(struct inode *ip, ufs_lbn_t lbn, int flags,
903 struct pagedep **pagedeppp)
905 struct pagedep *pagedep;
906 struct pagedep_hashhead *pagedephd;
911 if (lk.lkt_held == NOHOLDER)
912 panic("pagedep_lookup: lock not held");
914 mp = ITOV(ip)->v_mount;
915 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
917 *pagedeppp = pagedep_find(pagedephd, ip->i_number, lbn, mp);
920 if ((flags & DEPALLOC) == 0)
922 if (sema_get(&pagedep_in_progress, &lk) == 0) {
926 MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep), M_PAGEDEP,
927 M_SOFTDEP_FLAGS | M_ZERO);
929 if (pagedep_find(pagedephd, ip->i_number, lbn, mp)) {
930 kprintf("pagedep_lookup: blocking race avoided\n");
932 sema_release(&pagedep_in_progress);
933 kfree(pagedep, M_PAGEDEP);
937 pagedep->pd_list.wk_type = D_PAGEDEP;
938 pagedep->pd_mnt = mp;
939 pagedep->pd_ino = ip->i_number;
940 pagedep->pd_lbn = lbn;
941 LIST_INIT(&pagedep->pd_dirremhd);
942 LIST_INIT(&pagedep->pd_pendinghd);
943 for (i = 0; i < DAHASHSZ; i++)
944 LIST_INIT(&pagedep->pd_diraddhd[i]);
946 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
947 sema_release(&pagedep_in_progress);
948 *pagedeppp = pagedep;
953 * Structures and routines associated with inodedep caching.
955 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
956 static u_long inodedep_hash; /* size of hash table - 1 */
957 static long num_inodedep; /* number of inodedep allocated */
958 #define INODEDEP_HASH(fs, inum) \
959 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
960 static struct sema inodedep_in_progress;
963 * Helper routine for inodedep_lookup()
967 inodedep_find(struct inodedep_hashhead *inodedephd, struct fs *fs, ino_t inum)
969 struct inodedep *inodedep;
971 LIST_FOREACH(inodedep, inodedephd, id_hash) {
972 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
979 * Look up a inodedep. Return 1 if found, 0 if not found.
980 * If not found, allocate if DEPALLOC flag is passed.
981 * Found or allocated entry is returned in inodedeppp.
982 * This routine must be called with splbio interrupts blocked.
985 inodedep_lookup(struct fs *fs, ino_t inum, int flags,
986 struct inodedep **inodedeppp)
988 struct inodedep *inodedep;
989 struct inodedep_hashhead *inodedephd;
993 if (lk.lkt_held == NOHOLDER)
994 panic("inodedep_lookup: lock not held");
997 inodedephd = INODEDEP_HASH(fs, inum);
999 *inodedeppp = inodedep_find(inodedephd, fs, inum);
1002 if ((flags & DEPALLOC) == 0)
1005 * If we are over our limit, try to improve the situation.
1007 if (num_inodedep > max_softdeps && firsttry &&
1008 speedup_syncer() == 0 && (flags & NODELAY) == 0 &&
1009 request_cleanup(FLUSH_INODES, 1)) {
1013 if (sema_get(&inodedep_in_progress, &lk) == 0) {
1017 MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep),
1018 M_INODEDEP, M_SOFTDEP_FLAGS | M_ZERO);
1019 if (inodedep_find(inodedephd, fs, inum)) {
1020 kprintf("inodedep_lookup: blocking race avoided\n");
1022 sema_release(&inodedep_in_progress);
1023 kfree(inodedep, M_INODEDEP);
1026 inodedep->id_list.wk_type = D_INODEDEP;
1027 inodedep->id_fs = fs;
1028 inodedep->id_ino = inum;
1029 inodedep->id_state = ALLCOMPLETE;
1030 inodedep->id_nlinkdelta = 0;
1031 inodedep->id_savedino = NULL;
1032 inodedep->id_savedsize = -1;
1033 inodedep->id_buf = NULL;
1034 LIST_INIT(&inodedep->id_pendinghd);
1035 LIST_INIT(&inodedep->id_inowait);
1036 LIST_INIT(&inodedep->id_bufwait);
1037 TAILQ_INIT(&inodedep->id_inoupdt);
1038 TAILQ_INIT(&inodedep->id_newinoupdt);
1041 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1042 sema_release(&inodedep_in_progress);
1043 *inodedeppp = inodedep;
1048 * Structures and routines associated with newblk caching.
1050 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1051 u_long newblk_hash; /* size of hash table - 1 */
1052 #define NEWBLK_HASH(fs, inum) \
1053 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1054 static struct sema newblk_in_progress;
1057 * Helper routine for newblk_lookup()
1061 newblk_find(struct newblk_hashhead *newblkhd, struct fs *fs,
1062 ufs_daddr_t newblkno)
1064 struct newblk *newblk;
1066 LIST_FOREACH(newblk, newblkhd, nb_hash) {
1067 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1074 * Look up a newblk. Return 1 if found, 0 if not found.
1075 * If not found, allocate if DEPALLOC flag is passed.
1076 * Found or allocated entry is returned in newblkpp.
1079 newblk_lookup(struct fs *fs, ufs_daddr_t newblkno, int flags,
1080 struct newblk **newblkpp)
1082 struct newblk *newblk;
1083 struct newblk_hashhead *newblkhd;
1085 newblkhd = NEWBLK_HASH(fs, newblkno);
1087 *newblkpp = newblk_find(newblkhd, fs, newblkno);
1090 if ((flags & DEPALLOC) == 0)
1092 if (sema_get(&newblk_in_progress, 0) == 0)
1094 MALLOC(newblk, struct newblk *, sizeof(struct newblk),
1095 M_NEWBLK, M_SOFTDEP_FLAGS | M_ZERO);
1097 if (newblk_find(newblkhd, fs, newblkno)) {
1098 kprintf("newblk_lookup: blocking race avoided\n");
1099 sema_release(&pagedep_in_progress);
1100 kfree(newblk, M_NEWBLK);
1103 newblk->nb_state = 0;
1105 newblk->nb_newblkno = newblkno;
1106 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1107 sema_release(&newblk_in_progress);
1113 * Executed during filesystem system initialization before
1114 * mounting any filesystems.
1117 softdep_initialize(void)
1119 callout_init(&handle);
1121 LIST_INIT(&mkdirlisthd);
1122 LIST_INIT(&softdep_workitem_pending);
1123 max_softdeps = min(desiredvnodes * 8,
1124 M_INODEDEP->ks_limit / (2 * sizeof(struct inodedep)));
1125 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1127 sema_init(&pagedep_in_progress, "pagedep", 0, 0);
1128 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1129 sema_init(&inodedep_in_progress, "inodedep", 0, 0);
1130 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1131 sema_init(&newblk_in_progress, "newblk", 0, 0);
1132 add_bio_ops(&softdep_bioops);
1136 * Called at mount time to notify the dependency code that a
1137 * filesystem wishes to use it.
1140 softdep_mount(struct vnode *devvp, struct mount *mp, struct fs *fs)
1142 struct csum cstotal;
1147 mp->mnt_flag &= ~MNT_ASYNC;
1148 mp->mnt_flag |= MNT_SOFTDEP;
1149 mp->mnt_bioops = &softdep_bioops;
1151 * When doing soft updates, the counters in the
1152 * superblock may have gotten out of sync, so we have
1153 * to scan the cylinder groups and recalculate them.
1155 if (fs->fs_clean != 0)
1157 bzero(&cstotal, sizeof cstotal);
1158 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1159 if ((error = bread(devvp, fsbtodoff(fs, cgtod(fs, cyl)),
1160 fs->fs_cgsize, &bp)) != 0) {
1164 cgp = (struct cg *)bp->b_data;
1165 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1166 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1167 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1168 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1169 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1173 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1174 kprintf("ffs_mountfs: superblock updated for soft updates\n");
1176 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1181 * Protecting the freemaps (or bitmaps).
1183 * To eliminate the need to execute fsck before mounting a filesystem
1184 * after a power failure, one must (conservatively) guarantee that the
1185 * on-disk copy of the bitmaps never indicate that a live inode or block is
1186 * free. So, when a block or inode is allocated, the bitmap should be
1187 * updated (on disk) before any new pointers. When a block or inode is
1188 * freed, the bitmap should not be updated until all pointers have been
1189 * reset. The latter dependency is handled by the delayed de-allocation
1190 * approach described below for block and inode de-allocation. The former
1191 * dependency is handled by calling the following procedure when a block or
1192 * inode is allocated. When an inode is allocated an "inodedep" is created
1193 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1194 * Each "inodedep" is also inserted into the hash indexing structure so
1195 * that any additional link additions can be made dependent on the inode
1198 * The ufs filesystem maintains a number of free block counts (e.g., per
1199 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1200 * in addition to the bitmaps. These counts are used to improve efficiency
1201 * during allocation and therefore must be consistent with the bitmaps.
1202 * There is no convenient way to guarantee post-crash consistency of these
1203 * counts with simple update ordering, for two main reasons: (1) The counts
1204 * and bitmaps for a single cylinder group block are not in the same disk
1205 * sector. If a disk write is interrupted (e.g., by power failure), one may
1206 * be written and the other not. (2) Some of the counts are located in the
1207 * superblock rather than the cylinder group block. So, we focus our soft
1208 * updates implementation on protecting the bitmaps. When mounting a
1209 * filesystem, we recompute the auxiliary counts from the bitmaps.
1213 * Called just after updating the cylinder group block to allocate an inode.
1216 * bp: buffer for cylgroup block with inode map
1217 * ip: inode related to allocation
1218 * newinum: new inode number being allocated
1221 softdep_setup_inomapdep(struct buf *bp, struct inode *ip, ino_t newinum)
1223 struct inodedep *inodedep;
1224 struct bmsafemap *bmsafemap;
1227 * Create a dependency for the newly allocated inode.
1228 * Panic if it already exists as something is seriously wrong.
1229 * Otherwise add it to the dependency list for the buffer holding
1230 * the cylinder group map from which it was allocated.
1233 if ((inodedep_lookup(ip->i_fs, newinum, DEPALLOC|NODELAY, &inodedep))) {
1235 panic("softdep_setup_inomapdep: found inode");
1237 inodedep->id_buf = bp;
1238 inodedep->id_state &= ~DEPCOMPLETE;
1239 bmsafemap = bmsafemap_lookup(bp);
1240 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1245 * Called just after updating the cylinder group block to
1246 * allocate block or fragment.
1249 * bp: buffer for cylgroup block with block map
1250 * fs: filesystem doing allocation
1251 * newblkno: number of newly allocated block
1254 softdep_setup_blkmapdep(struct buf *bp, struct fs *fs,
1255 ufs_daddr_t newblkno)
1257 struct newblk *newblk;
1258 struct bmsafemap *bmsafemap;
1261 * Create a dependency for the newly allocated block.
1262 * Add it to the dependency list for the buffer holding
1263 * the cylinder group map from which it was allocated.
1265 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1266 panic("softdep_setup_blkmapdep: found block");
1268 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp);
1269 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1274 * Find the bmsafemap associated with a cylinder group buffer.
1275 * If none exists, create one. The buffer must be locked when
1276 * this routine is called and this routine must be called with
1277 * splbio interrupts blocked.
1279 static struct bmsafemap *
1280 bmsafemap_lookup(struct buf *bp)
1282 struct bmsafemap *bmsafemap;
1283 struct worklist *wk;
1286 if (lk.lkt_held == NOHOLDER)
1287 panic("bmsafemap_lookup: lock not held");
1289 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
1290 if (wk->wk_type == D_BMSAFEMAP)
1291 return (WK_BMSAFEMAP(wk));
1294 MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1295 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1296 bmsafemap->sm_list.wk_type = D_BMSAFEMAP;
1297 bmsafemap->sm_list.wk_state = 0;
1298 bmsafemap->sm_buf = bp;
1299 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1300 LIST_INIT(&bmsafemap->sm_allocindirhd);
1301 LIST_INIT(&bmsafemap->sm_inodedephd);
1302 LIST_INIT(&bmsafemap->sm_newblkhd);
1304 WORKLIST_INSERT_BP(bp, &bmsafemap->sm_list);
1309 * Direct block allocation dependencies.
1311 * When a new block is allocated, the corresponding disk locations must be
1312 * initialized (with zeros or new data) before the on-disk inode points to
1313 * them. Also, the freemap from which the block was allocated must be
1314 * updated (on disk) before the inode's pointer. These two dependencies are
1315 * independent of each other and are needed for all file blocks and indirect
1316 * blocks that are pointed to directly by the inode. Just before the
1317 * "in-core" version of the inode is updated with a newly allocated block
1318 * number, a procedure (below) is called to setup allocation dependency
1319 * structures. These structures are removed when the corresponding
1320 * dependencies are satisfied or when the block allocation becomes obsolete
1321 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1322 * fragment that gets upgraded). All of these cases are handled in
1323 * procedures described later.
1325 * When a file extension causes a fragment to be upgraded, either to a larger
1326 * fragment or to a full block, the on-disk location may change (if the
1327 * previous fragment could not simply be extended). In this case, the old
1328 * fragment must be de-allocated, but not until after the inode's pointer has
1329 * been updated. In most cases, this is handled by later procedures, which
1330 * will construct a "freefrag" structure to be added to the workitem queue
1331 * when the inode update is complete (or obsolete). The main exception to
1332 * this is when an allocation occurs while a pending allocation dependency
1333 * (for the same block pointer) remains. This case is handled in the main
1334 * allocation dependency setup procedure by immediately freeing the
1335 * unreferenced fragments.
1338 * ip: inode to which block is being added
1339 * lbn: block pointer within inode
1340 * newblkno: disk block number being added
1341 * oldblkno: previous block number, 0 unless frag
1342 * newsize: size of new block
1343 * oldsize: size of new block
1344 * bp: bp for allocated block
1347 softdep_setup_allocdirect(struct inode *ip, ufs_lbn_t lbn, ufs_daddr_t newblkno,
1348 ufs_daddr_t oldblkno, long newsize, long oldsize,
1351 struct allocdirect *adp, *oldadp;
1352 struct allocdirectlst *adphead;
1353 struct bmsafemap *bmsafemap;
1354 struct inodedep *inodedep;
1355 struct pagedep *pagedep;
1356 struct newblk *newblk;
1358 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1359 M_ALLOCDIRECT, M_SOFTDEP_FLAGS);
1360 bzero(adp, sizeof(struct allocdirect));
1361 adp->ad_list.wk_type = D_ALLOCDIRECT;
1363 adp->ad_newblkno = newblkno;
1364 adp->ad_oldblkno = oldblkno;
1365 adp->ad_newsize = newsize;
1366 adp->ad_oldsize = oldsize;
1367 adp->ad_state = ATTACHED;
1368 if (newblkno == oldblkno)
1369 adp->ad_freefrag = NULL;
1371 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1373 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1374 panic("softdep_setup_allocdirect: lost block");
1377 inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1378 adp->ad_inodedep = inodedep;
1380 if (newblk->nb_state == DEPCOMPLETE) {
1381 adp->ad_state |= DEPCOMPLETE;
1384 bmsafemap = newblk->nb_bmsafemap;
1385 adp->ad_buf = bmsafemap->sm_buf;
1386 LIST_REMOVE(newblk, nb_deps);
1387 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1389 LIST_REMOVE(newblk, nb_hash);
1390 FREE(newblk, M_NEWBLK);
1392 WORKLIST_INSERT_BP(bp, &adp->ad_list);
1393 if (lbn >= NDADDR) {
1394 /* allocating an indirect block */
1395 if (oldblkno != 0) {
1397 panic("softdep_setup_allocdirect: non-zero indir");
1401 * Allocating a direct block.
1403 * If we are allocating a directory block, then we must
1404 * allocate an associated pagedep to track additions and
1407 if ((ip->i_mode & IFMT) == IFDIR &&
1408 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0) {
1409 WORKLIST_INSERT_BP(bp, &pagedep->pd_list);
1413 * The list of allocdirects must be kept in sorted and ascending
1414 * order so that the rollback routines can quickly determine the
1415 * first uncommitted block (the size of the file stored on disk
1416 * ends at the end of the lowest committed fragment, or if there
1417 * are no fragments, at the end of the highest committed block).
1418 * Since files generally grow, the typical case is that the new
1419 * block is to be added at the end of the list. We speed this
1420 * special case by checking against the last allocdirect in the
1421 * list before laboriously traversing the list looking for the
1424 adphead = &inodedep->id_newinoupdt;
1425 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1426 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1427 /* insert at end of list */
1428 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1429 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1430 allocdirect_merge(adphead, adp, oldadp);
1434 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1435 if (oldadp->ad_lbn >= lbn)
1438 if (oldadp == NULL) {
1440 panic("softdep_setup_allocdirect: lost entry");
1442 /* insert in middle of list */
1443 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1444 if (oldadp->ad_lbn == lbn)
1445 allocdirect_merge(adphead, adp, oldadp);
1450 * Replace an old allocdirect dependency with a newer one.
1451 * This routine must be called with splbio interrupts blocked.
1454 * adphead: head of list holding allocdirects
1455 * newadp: allocdirect being added
1456 * oldadp: existing allocdirect being checked
1459 allocdirect_merge(struct allocdirectlst *adphead,
1460 struct allocdirect *newadp,
1461 struct allocdirect *oldadp)
1463 struct freefrag *freefrag;
1466 if (lk.lkt_held == NOHOLDER)
1467 panic("allocdirect_merge: lock not held");
1469 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1470 newadp->ad_oldsize != oldadp->ad_newsize ||
1471 newadp->ad_lbn >= NDADDR) {
1473 panic("allocdirect_check: old %d != new %d || lbn %ld >= %d",
1474 newadp->ad_oldblkno, oldadp->ad_newblkno, newadp->ad_lbn,
1477 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1478 newadp->ad_oldsize = oldadp->ad_oldsize;
1480 * If the old dependency had a fragment to free or had never
1481 * previously had a block allocated, then the new dependency
1482 * can immediately post its freefrag and adopt the old freefrag.
1483 * This action is done by swapping the freefrag dependencies.
1484 * The new dependency gains the old one's freefrag, and the
1485 * old one gets the new one and then immediately puts it on
1486 * the worklist when it is freed by free_allocdirect. It is
1487 * not possible to do this swap when the old dependency had a
1488 * non-zero size but no previous fragment to free. This condition
1489 * arises when the new block is an extension of the old block.
1490 * Here, the first part of the fragment allocated to the new
1491 * dependency is part of the block currently claimed on disk by
1492 * the old dependency, so cannot legitimately be freed until the
1493 * conditions for the new dependency are fulfilled.
1495 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1496 freefrag = newadp->ad_freefrag;
1497 newadp->ad_freefrag = oldadp->ad_freefrag;
1498 oldadp->ad_freefrag = freefrag;
1500 free_allocdirect(adphead, oldadp, 0);
1504 * Allocate a new freefrag structure if needed.
1506 static struct freefrag *
1507 newfreefrag(struct inode *ip, ufs_daddr_t blkno, long size)
1509 struct freefrag *freefrag;
1515 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1516 panic("newfreefrag: frag size");
1517 MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1518 M_FREEFRAG, M_SOFTDEP_FLAGS);
1519 freefrag->ff_list.wk_type = D_FREEFRAG;
1520 freefrag->ff_state = ip->i_uid & ~ONWORKLIST; /* XXX - used below */
1521 freefrag->ff_inum = ip->i_number;
1522 freefrag->ff_fs = fs;
1523 freefrag->ff_devvp = ip->i_devvp;
1524 freefrag->ff_blkno = blkno;
1525 freefrag->ff_fragsize = size;
1530 * This workitem de-allocates fragments that were replaced during
1531 * file block allocation.
1534 handle_workitem_freefrag(struct freefrag *freefrag)
1538 tip.i_fs = freefrag->ff_fs;
1539 tip.i_devvp = freefrag->ff_devvp;
1540 tip.i_dev = freefrag->ff_devvp->v_rdev;
1541 tip.i_number = freefrag->ff_inum;
1542 tip.i_uid = freefrag->ff_state & ~ONWORKLIST; /* XXX - set above */
1543 ffs_blkfree(&tip, freefrag->ff_blkno, freefrag->ff_fragsize);
1544 FREE(freefrag, M_FREEFRAG);
1548 * Indirect block allocation dependencies.
1550 * The same dependencies that exist for a direct block also exist when
1551 * a new block is allocated and pointed to by an entry in a block of
1552 * indirect pointers. The undo/redo states described above are also
1553 * used here. Because an indirect block contains many pointers that
1554 * may have dependencies, a second copy of the entire in-memory indirect
1555 * block is kept. The buffer cache copy is always completely up-to-date.
1556 * The second copy, which is used only as a source for disk writes,
1557 * contains only the safe pointers (i.e., those that have no remaining
1558 * update dependencies). The second copy is freed when all pointers
1559 * are safe. The cache is not allowed to replace indirect blocks with
1560 * pending update dependencies. If a buffer containing an indirect
1561 * block with dependencies is written, these routines will mark it
1562 * dirty again. It can only be successfully written once all the
1563 * dependencies are removed. The ffs_fsync routine in conjunction with
1564 * softdep_sync_metadata work together to get all the dependencies
1565 * removed so that a file can be successfully written to disk. Three
1566 * procedures are used when setting up indirect block pointer
1567 * dependencies. The division is necessary because of the organization
1568 * of the "balloc" routine and because of the distinction between file
1569 * pages and file metadata blocks.
1573 * Allocate a new allocindir structure.
1576 * ip: inode for file being extended
1577 * ptrno: offset of pointer in indirect block
1578 * newblkno: disk block number being added
1579 * oldblkno: previous block number, 0 if none
1581 static struct allocindir *
1582 newallocindir(struct inode *ip, int ptrno, ufs_daddr_t newblkno,
1583 ufs_daddr_t oldblkno)
1585 struct allocindir *aip;
1587 MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1588 M_ALLOCINDIR, M_SOFTDEP_FLAGS);
1589 bzero(aip, sizeof(struct allocindir));
1590 aip->ai_list.wk_type = D_ALLOCINDIR;
1591 aip->ai_state = ATTACHED;
1592 aip->ai_offset = ptrno;
1593 aip->ai_newblkno = newblkno;
1594 aip->ai_oldblkno = oldblkno;
1595 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1600 * Called just before setting an indirect block pointer
1601 * to a newly allocated file page.
1604 * ip: inode for file being extended
1605 * lbn: allocated block number within file
1606 * bp: buffer with indirect blk referencing page
1607 * ptrno: offset of pointer in indirect block
1608 * newblkno: disk block number being added
1609 * oldblkno: previous block number, 0 if none
1610 * nbp: buffer holding allocated page
1613 softdep_setup_allocindir_page(struct inode *ip, ufs_lbn_t lbn,
1614 struct buf *bp, int ptrno,
1615 ufs_daddr_t newblkno, ufs_daddr_t oldblkno,
1618 struct allocindir *aip;
1619 struct pagedep *pagedep;
1621 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
1624 * If we are allocating a directory page, then we must
1625 * allocate an associated pagedep to track additions and
1628 if ((ip->i_mode & IFMT) == IFDIR &&
1629 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1630 WORKLIST_INSERT_BP(nbp, &pagedep->pd_list);
1631 WORKLIST_INSERT_BP(nbp, &aip->ai_list);
1633 setup_allocindir_phase2(bp, ip, aip);
1637 * Called just before setting an indirect block pointer to a
1638 * newly allocated indirect block.
1640 * nbp: newly allocated indirect block
1641 * ip: inode for file being extended
1642 * bp: indirect block referencing allocated block
1643 * ptrno: offset of pointer in indirect block
1644 * newblkno: disk block number being added
1647 softdep_setup_allocindir_meta(struct buf *nbp, struct inode *ip,
1648 struct buf *bp, int ptrno,
1649 ufs_daddr_t newblkno)
1651 struct allocindir *aip;
1653 aip = newallocindir(ip, ptrno, newblkno, 0);
1655 WORKLIST_INSERT_BP(nbp, &aip->ai_list);
1657 setup_allocindir_phase2(bp, ip, aip);
1661 * Called to finish the allocation of the "aip" allocated
1662 * by one of the two routines above.
1665 * bp: in-memory copy of the indirect block
1666 * ip: inode for file being extended
1667 * aip: allocindir allocated by the above routines
1670 setup_allocindir_phase2(struct buf *bp, struct inode *ip,
1671 struct allocindir *aip)
1673 struct worklist *wk;
1674 struct indirdep *indirdep, *newindirdep;
1675 struct bmsafemap *bmsafemap;
1676 struct allocindir *oldaip;
1677 struct freefrag *freefrag;
1678 struct newblk *newblk;
1680 if (bp->b_loffset >= 0)
1681 panic("setup_allocindir_phase2: not indir blk");
1682 for (indirdep = NULL, newindirdep = NULL; ; ) {
1684 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
1685 if (wk->wk_type != D_INDIRDEP)
1687 indirdep = WK_INDIRDEP(wk);
1690 if (indirdep == NULL && newindirdep) {
1691 indirdep = newindirdep;
1692 WORKLIST_INSERT_BP(bp, &indirdep->ir_list);
1697 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
1699 panic("setup_allocindir: lost block");
1701 if (newblk->nb_state == DEPCOMPLETE) {
1702 aip->ai_state |= DEPCOMPLETE;
1705 bmsafemap = newblk->nb_bmsafemap;
1706 aip->ai_buf = bmsafemap->sm_buf;
1707 LIST_REMOVE(newblk, nb_deps);
1708 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
1711 LIST_REMOVE(newblk, nb_hash);
1712 FREE(newblk, M_NEWBLK);
1713 aip->ai_indirdep = indirdep;
1715 * Check to see if there is an existing dependency
1716 * for this block. If there is, merge the old
1717 * dependency into the new one.
1719 if (aip->ai_oldblkno == 0)
1723 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
1724 if (oldaip->ai_offset == aip->ai_offset)
1726 if (oldaip != NULL) {
1727 if (oldaip->ai_newblkno != aip->ai_oldblkno) {
1729 panic("setup_allocindir_phase2: blkno");
1731 aip->ai_oldblkno = oldaip->ai_oldblkno;
1732 freefrag = oldaip->ai_freefrag;
1733 oldaip->ai_freefrag = aip->ai_freefrag;
1734 aip->ai_freefrag = freefrag;
1735 free_allocindir(oldaip, NULL);
1737 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
1738 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)
1739 [aip->ai_offset] = aip->ai_oldblkno;
1744 * Avoid any possibility of data corruption by
1745 * ensuring that our old version is thrown away.
1747 newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
1748 brelse(newindirdep->ir_savebp);
1749 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
1753 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
1754 M_INDIRDEP, M_SOFTDEP_FLAGS);
1755 newindirdep->ir_list.wk_type = D_INDIRDEP;
1756 newindirdep->ir_state = ATTACHED;
1757 LIST_INIT(&newindirdep->ir_deplisthd);
1758 LIST_INIT(&newindirdep->ir_donehd);
1759 if (bp->b_bio2.bio_offset == NOOFFSET) {
1760 VOP_BMAP(bp->b_vp, bp->b_bio1.bio_offset,
1761 &bp->b_bio2.bio_offset, NULL, NULL);
1763 KKASSERT(bp->b_bio2.bio_offset != NOOFFSET);
1764 newindirdep->ir_savebp = getblk(ip->i_devvp,
1765 bp->b_bio2.bio_offset,
1766 bp->b_bcount, 0, 0);
1767 BUF_KERNPROC(newindirdep->ir_savebp);
1768 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
1773 * Block de-allocation dependencies.
1775 * When blocks are de-allocated, the on-disk pointers must be nullified before
1776 * the blocks are made available for use by other files. (The true
1777 * requirement is that old pointers must be nullified before new on-disk
1778 * pointers are set. We chose this slightly more stringent requirement to
1779 * reduce complexity.) Our implementation handles this dependency by updating
1780 * the inode (or indirect block) appropriately but delaying the actual block
1781 * de-allocation (i.e., freemap and free space count manipulation) until
1782 * after the updated versions reach stable storage. After the disk is
1783 * updated, the blocks can be safely de-allocated whenever it is convenient.
1784 * This implementation handles only the common case of reducing a file's
1785 * length to zero. Other cases are handled by the conventional synchronous
1788 * The ffs implementation with which we worked double-checks
1789 * the state of the block pointers and file size as it reduces
1790 * a file's length. Some of this code is replicated here in our
1791 * soft updates implementation. The freeblks->fb_chkcnt field is
1792 * used to transfer a part of this information to the procedure
1793 * that eventually de-allocates the blocks.
1795 * This routine should be called from the routine that shortens
1796 * a file's length, before the inode's size or block pointers
1797 * are modified. It will save the block pointer information for
1798 * later release and zero the inode so that the calling routine
1801 struct softdep_setup_freeblocks_info {
1806 static int softdep_setup_freeblocks_bp(struct buf *bp, void *data);
1810 * ip: The inode whose length is to be reduced
1811 * length: The new length for the file
1814 softdep_setup_freeblocks(struct inode *ip, off_t length)
1816 struct softdep_setup_freeblocks_info info;
1817 struct freeblks *freeblks;
1818 struct inodedep *inodedep;
1819 struct allocdirect *adp;
1823 int i, error, delay;
1828 panic("softde_setup_freeblocks: non-zero length");
1829 MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
1830 M_FREEBLKS, M_SOFTDEP_FLAGS);
1831 bzero(freeblks, sizeof(struct freeblks));
1832 freeblks->fb_list.wk_type = D_FREEBLKS;
1833 freeblks->fb_state = ATTACHED;
1834 freeblks->fb_uid = ip->i_uid;
1835 freeblks->fb_previousinum = ip->i_number;
1836 freeblks->fb_devvp = ip->i_devvp;
1837 freeblks->fb_fs = fs;
1838 freeblks->fb_oldsize = ip->i_size;
1839 freeblks->fb_newsize = length;
1840 freeblks->fb_chkcnt = ip->i_blocks;
1841 for (i = 0; i < NDADDR; i++) {
1842 freeblks->fb_dblks[i] = ip->i_db[i];
1845 for (i = 0; i < NIADDR; i++) {
1846 freeblks->fb_iblks[i] = ip->i_ib[i];
1852 * Push the zero'ed inode to to its disk buffer so that we are free
1853 * to delete its dependencies below. Once the dependencies are gone
1854 * the buffer can be safely released.
1856 if ((error = bread(ip->i_devvp,
1857 fsbtodoff(fs, ino_to_fsba(fs, ip->i_number)),
1858 (int)fs->fs_bsize, &bp)) != 0)
1859 softdep_error("softdep_setup_freeblocks", error);
1860 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)) =
1863 * Find and eliminate any inode dependencies.
1866 (void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep);
1867 if ((inodedep->id_state & IOSTARTED) != 0) {
1869 panic("softdep_setup_freeblocks: inode busy");
1872 * Add the freeblks structure to the list of operations that
1873 * must await the zero'ed inode being written to disk. If we
1874 * still have a bitmap dependency (delay == 0), then the inode
1875 * has never been written to disk, so we can process the
1876 * freeblks below once we have deleted the dependencies.
1878 delay = (inodedep->id_state & DEPCOMPLETE);
1880 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
1882 * Because the file length has been truncated to zero, any
1883 * pending block allocation dependency structures associated
1884 * with this inode are obsolete and can simply be de-allocated.
1885 * We must first merge the two dependency lists to get rid of
1886 * any duplicate freefrag structures, then purge the merged list.
1888 merge_inode_lists(inodedep);
1889 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
1890 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
1894 * We must wait for any I/O in progress to finish so that
1895 * all potential buffers on the dirty list will be visible.
1896 * Once they are all there, walk the list and get rid of
1901 drain_output(vp, 1);
1906 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
1907 softdep_setup_freeblocks_bp, &info);
1908 } while (count != 0);
1909 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) != 0)
1910 (void)free_inodedep(inodedep);
1913 freeblks->fb_state |= DEPCOMPLETE;
1915 * If the inode with zeroed block pointers is now on disk
1916 * we can start freeing blocks. Add freeblks to the worklist
1917 * instead of calling handle_workitem_freeblocks directly as
1918 * it is more likely that additional IO is needed to complete
1919 * the request here than in the !delay case.
1921 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
1922 add_to_worklist(&freeblks->fb_list);
1927 * If the inode has never been written to disk (delay == 0),
1928 * then we can process the freeblks now that we have deleted
1932 handle_workitem_freeblocks(freeblks);
1936 softdep_setup_freeblocks_bp(struct buf *bp, void *data)
1938 struct softdep_setup_freeblocks_info *info = data;
1939 struct inodedep *inodedep;
1941 if (getdirtybuf(&bp, MNT_WAIT) == 0) {
1942 kprintf("softdep_setup_freeblocks_bp(1): caught bp %p going away\n", bp);
1945 if (bp->b_vp != ITOV(info->ip) || (bp->b_flags & B_DELWRI) == 0) {
1946 kprintf("softdep_setup_freeblocks_bp(2): caught bp %p going away\n", bp);
1950 (void) inodedep_lookup(info->fs, info->ip->i_number, 0, &inodedep);
1951 deallocate_dependencies(bp, inodedep);
1952 bp->b_flags |= B_INVAL | B_NOCACHE;
1960 * Reclaim any dependency structures from a buffer that is about to
1961 * be reallocated to a new vnode. The buffer must be locked, thus,
1962 * no I/O completion operations can occur while we are manipulating
1963 * its associated dependencies. The mutex is held so that other I/O's
1964 * associated with related dependencies do not occur.
1967 deallocate_dependencies(struct buf *bp, struct inodedep *inodedep)
1969 struct worklist *wk;
1970 struct indirdep *indirdep;
1971 struct allocindir *aip;
1972 struct pagedep *pagedep;
1973 struct dirrem *dirrem;
1977 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
1978 switch (wk->wk_type) {
1981 indirdep = WK_INDIRDEP(wk);
1983 * None of the indirect pointers will ever be visible,
1984 * so they can simply be tossed. GOINGAWAY ensures
1985 * that allocated pointers will be saved in the buffer
1986 * cache until they are freed. Note that they will
1987 * only be able to be found by their physical address
1988 * since the inode mapping the logical address will
1989 * be gone. The save buffer used for the safe copy
1990 * was allocated in setup_allocindir_phase2 using
1991 * the physical address so it could be used for this
1992 * purpose. Hence we swap the safe copy with the real
1993 * copy, allowing the safe copy to be freed and holding
1994 * on to the real copy for later use in indir_trunc.
1996 * NOTE: ir_savebp is relative to the block device
1997 * so b_bio1 contains the device block number.
1999 if (indirdep->ir_state & GOINGAWAY) {
2001 panic("deallocate_dependencies: already gone");
2003 indirdep->ir_state |= GOINGAWAY;
2004 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
2005 free_allocindir(aip, inodedep);
2006 if (bp->b_bio1.bio_offset >= 0 ||
2007 bp->b_bio2.bio_offset != indirdep->ir_savebp->b_bio1.bio_offset) {
2009 panic("deallocate_dependencies: not indir");
2011 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
2013 WORKLIST_REMOVE(wk);
2014 WORKLIST_INSERT_BP(indirdep->ir_savebp, wk);
2018 pagedep = WK_PAGEDEP(wk);
2020 * None of the directory additions will ever be
2021 * visible, so they can simply be tossed.
2023 for (i = 0; i < DAHASHSZ; i++)
2025 LIST_FIRST(&pagedep->pd_diraddhd[i])))
2027 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
2030 * Copy any directory remove dependencies to the list
2031 * to be processed after the zero'ed inode is written.
2032 * If the inode has already been written, then they
2033 * can be dumped directly onto the work list.
2035 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
2036 LIST_REMOVE(dirrem, dm_next);
2037 dirrem->dm_dirinum = pagedep->pd_ino;
2038 if (inodedep == NULL ||
2039 (inodedep->id_state & ALLCOMPLETE) ==
2041 add_to_worklist(&dirrem->dm_list);
2043 WORKLIST_INSERT(&inodedep->id_bufwait,
2046 WORKLIST_REMOVE(&pagedep->pd_list);
2047 LIST_REMOVE(pagedep, pd_hash);
2048 WORKITEM_FREE(pagedep, D_PAGEDEP);
2052 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
2058 panic("deallocate_dependencies: Unexpected type %s",
2059 TYPENAME(wk->wk_type));
2064 panic("deallocate_dependencies: Unknown type %s",
2065 TYPENAME(wk->wk_type));
2072 * Free an allocdirect. Generate a new freefrag work request if appropriate.
2073 * This routine must be called with splbio interrupts blocked.
2076 free_allocdirect(struct allocdirectlst *adphead,
2077 struct allocdirect *adp, int delay)
2081 if (lk.lkt_held == NOHOLDER)
2082 panic("free_allocdirect: lock not held");
2084 if ((adp->ad_state & DEPCOMPLETE) == 0)
2085 LIST_REMOVE(adp, ad_deps);
2086 TAILQ_REMOVE(adphead, adp, ad_next);
2087 if ((adp->ad_state & COMPLETE) == 0)
2088 WORKLIST_REMOVE(&adp->ad_list);
2089 if (adp->ad_freefrag != NULL) {
2091 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2092 &adp->ad_freefrag->ff_list);
2094 add_to_worklist(&adp->ad_freefrag->ff_list);
2096 WORKITEM_FREE(adp, D_ALLOCDIRECT);
2100 * Prepare an inode to be freed. The actual free operation is not
2101 * done until the zero'ed inode has been written to disk.
2104 softdep_freefile(struct vnode *pvp, ino_t ino, int mode)
2106 struct inode *ip = VTOI(pvp);
2107 struct inodedep *inodedep;
2108 struct freefile *freefile;
2111 * This sets up the inode de-allocation dependency.
2113 MALLOC(freefile, struct freefile *, sizeof(struct freefile),
2114 M_FREEFILE, M_SOFTDEP_FLAGS);
2115 freefile->fx_list.wk_type = D_FREEFILE;
2116 freefile->fx_list.wk_state = 0;
2117 freefile->fx_mode = mode;
2118 freefile->fx_oldinum = ino;
2119 freefile->fx_devvp = ip->i_devvp;
2120 freefile->fx_fs = ip->i_fs;
2123 * If the inodedep does not exist, then the zero'ed inode has
2124 * been written to disk. If the allocated inode has never been
2125 * written to disk, then the on-disk inode is zero'ed. In either
2126 * case we can free the file immediately.
2129 if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0 ||
2130 check_inode_unwritten(inodedep)) {
2132 handle_workitem_freefile(freefile);
2135 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2140 * Check to see if an inode has never been written to disk. If
2141 * so free the inodedep and return success, otherwise return failure.
2142 * This routine must be called with splbio interrupts blocked.
2144 * If we still have a bitmap dependency, then the inode has never
2145 * been written to disk. Drop the dependency as it is no longer
2146 * necessary since the inode is being deallocated. We set the
2147 * ALLCOMPLETE flags since the bitmap now properly shows that the
2148 * inode is not allocated. Even if the inode is actively being
2149 * written, it has been rolled back to its zero'ed state, so we
2150 * are ensured that a zero inode is what is on the disk. For short
2151 * lived files, this change will usually result in removing all the
2152 * dependencies from the inode so that it can be freed immediately.
2155 check_inode_unwritten(struct inodedep *inodedep)
2158 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2159 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2160 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2161 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2162 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2163 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2164 inodedep->id_nlinkdelta != 0)
2168 * Another process might be in initiate_write_inodeblock
2169 * trying to allocate memory without holding "Softdep Lock".
2171 if ((inodedep->id_state & IOSTARTED) != 0 &&
2172 inodedep->id_savedino == NULL)
2175 inodedep->id_state |= ALLCOMPLETE;
2176 LIST_REMOVE(inodedep, id_deps);
2177 inodedep->id_buf = NULL;
2178 if (inodedep->id_state & ONWORKLIST)
2179 WORKLIST_REMOVE(&inodedep->id_list);
2180 if (inodedep->id_savedino != NULL) {
2181 FREE(inodedep->id_savedino, M_INODEDEP);
2182 inodedep->id_savedino = NULL;
2184 if (free_inodedep(inodedep) == 0) {
2186 panic("check_inode_unwritten: busy inode");
2192 * Try to free an inodedep structure. Return 1 if it could be freed.
2195 free_inodedep(struct inodedep *inodedep)
2198 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2199 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2200 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2201 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2202 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2203 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2204 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2205 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino != NULL)
2207 LIST_REMOVE(inodedep, id_hash);
2208 WORKITEM_FREE(inodedep, D_INODEDEP);
2214 * This workitem routine performs the block de-allocation.
2215 * The workitem is added to the pending list after the updated
2216 * inode block has been written to disk. As mentioned above,
2217 * checks regarding the number of blocks de-allocated (compared
2218 * to the number of blocks allocated for the file) are also
2219 * performed in this function.
2222 handle_workitem_freeblocks(struct freeblks *freeblks)
2227 int i, level, bsize;
2228 long nblocks, blocksreleased = 0;
2229 int error, allerror = 0;
2230 ufs_lbn_t baselbns[NIADDR], tmpval;
2232 tip.i_number = freeblks->fb_previousinum;
2233 tip.i_devvp = freeblks->fb_devvp;
2234 tip.i_dev = freeblks->fb_devvp->v_rdev;
2235 tip.i_fs = freeblks->fb_fs;
2236 tip.i_size = freeblks->fb_oldsize;
2237 tip.i_uid = freeblks->fb_uid;
2238 fs = freeblks->fb_fs;
2240 baselbns[0] = NDADDR;
2241 for (i = 1; i < NIADDR; i++) {
2242 tmpval *= NINDIR(fs);
2243 baselbns[i] = baselbns[i - 1] + tmpval;
2245 nblocks = btodb(fs->fs_bsize);
2248 * Indirect blocks first.
2250 for (level = (NIADDR - 1); level >= 0; level--) {
2251 if ((bn = freeblks->fb_iblks[level]) == 0)
2253 if ((error = indir_trunc(&tip, fsbtodoff(fs, bn), level,
2254 baselbns[level], &blocksreleased)) == 0)
2256 ffs_blkfree(&tip, bn, fs->fs_bsize);
2257 blocksreleased += nblocks;
2260 * All direct blocks or frags.
2262 for (i = (NDADDR - 1); i >= 0; i--) {
2263 if ((bn = freeblks->fb_dblks[i]) == 0)
2265 bsize = blksize(fs, &tip, i);
2266 ffs_blkfree(&tip, bn, bsize);
2267 blocksreleased += btodb(bsize);
2271 if (freeblks->fb_chkcnt != blocksreleased)
2272 kprintf("handle_workitem_freeblocks: block count\n");
2274 softdep_error("handle_workitem_freeblks", allerror);
2275 #endif /* DIAGNOSTIC */
2276 WORKITEM_FREE(freeblks, D_FREEBLKS);
2280 * Release blocks associated with the inode ip and stored in the indirect
2281 * block at doffset. If level is greater than SINGLE, the block is an
2282 * indirect block and recursive calls to indirtrunc must be used to
2283 * cleanse other indirect blocks.
2286 indir_trunc(struct inode *ip, off_t doffset, int level, ufs_lbn_t lbn,
2293 struct worklist *wk;
2294 struct indirdep *indirdep;
2295 int i, lbnadd, nblocks;
2296 int error, allerror = 0;
2300 for (i = level; i > 0; i--)
2301 lbnadd *= NINDIR(fs);
2303 * Get buffer of block pointers to be freed. This routine is not
2304 * called until the zero'ed inode has been written, so it is safe
2305 * to free blocks as they are encountered. Because the inode has
2306 * been zero'ed, calls to bmap on these blocks will fail. So, we
2307 * have to use the on-disk address and the block device for the
2308 * filesystem to look them up. If the file was deleted before its
2309 * indirect blocks were all written to disk, the routine that set
2310 * us up (deallocate_dependencies) will have arranged to leave
2311 * a complete copy of the indirect block in memory for our use.
2312 * Otherwise we have to read the blocks in from the disk.
2315 if ((bp = findblk(ip->i_devvp, doffset)) != NULL &&
2316 (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2318 * bp must be ir_savebp, which is held locked for our use.
2320 if (wk->wk_type != D_INDIRDEP ||
2321 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2322 (indirdep->ir_state & GOINGAWAY) == 0) {
2324 panic("indir_trunc: lost indirdep");
2326 WORKLIST_REMOVE(wk);
2327 WORKITEM_FREE(indirdep, D_INDIRDEP);
2328 if (LIST_FIRST(&bp->b_dep) != NULL) {
2330 panic("indir_trunc: dangling dep");
2335 error = bread(ip->i_devvp, doffset, (int)fs->fs_bsize, &bp);
2340 * Recursively free indirect blocks.
2342 bap = (ufs_daddr_t *)bp->b_data;
2343 nblocks = btodb(fs->fs_bsize);
2344 for (i = NINDIR(fs) - 1; i >= 0; i--) {
2345 if ((nb = bap[i]) == 0)
2348 if ((error = indir_trunc(ip, fsbtodoff(fs, nb),
2349 level - 1, lbn + (i * lbnadd), countp)) != 0)
2352 ffs_blkfree(ip, nb, fs->fs_bsize);
2355 bp->b_flags |= B_INVAL | B_NOCACHE;
2361 * Free an allocindir.
2362 * This routine must be called with splbio interrupts blocked.
2365 free_allocindir(struct allocindir *aip, struct inodedep *inodedep)
2367 struct freefrag *freefrag;
2370 if (lk.lkt_held == NOHOLDER)
2371 panic("free_allocindir: lock not held");
2373 if ((aip->ai_state & DEPCOMPLETE) == 0)
2374 LIST_REMOVE(aip, ai_deps);
2375 if (aip->ai_state & ONWORKLIST)
2376 WORKLIST_REMOVE(&aip->ai_list);
2377 LIST_REMOVE(aip, ai_next);
2378 if ((freefrag = aip->ai_freefrag) != NULL) {
2379 if (inodedep == NULL)
2380 add_to_worklist(&freefrag->ff_list);
2382 WORKLIST_INSERT(&inodedep->id_bufwait,
2383 &freefrag->ff_list);
2385 WORKITEM_FREE(aip, D_ALLOCINDIR);
2389 * Directory entry addition dependencies.
2391 * When adding a new directory entry, the inode (with its incremented link
2392 * count) must be written to disk before the directory entry's pointer to it.
2393 * Also, if the inode is newly allocated, the corresponding freemap must be
2394 * updated (on disk) before the directory entry's pointer. These requirements
2395 * are met via undo/redo on the directory entry's pointer, which consists
2396 * simply of the inode number.
2398 * As directory entries are added and deleted, the free space within a
2399 * directory block can become fragmented. The ufs filesystem will compact
2400 * a fragmented directory block to make space for a new entry. When this
2401 * occurs, the offsets of previously added entries change. Any "diradd"
2402 * dependency structures corresponding to these entries must be updated with
2407 * This routine is called after the in-memory inode's link
2408 * count has been incremented, but before the directory entry's
2409 * pointer to the inode has been set.
2412 * bp: buffer containing directory block
2413 * dp: inode for directory
2414 * diroffset: offset of new entry in directory
2415 * newinum: inode referenced by new directory entry
2416 * newdirbp: non-NULL => contents of new mkdir
2419 softdep_setup_directory_add(struct buf *bp, struct inode *dp, off_t diroffset,
2420 ino_t newinum, struct buf *newdirbp)
2422 int offset; /* offset of new entry within directory block */
2423 ufs_lbn_t lbn; /* block in directory containing new entry */
2426 struct pagedep *pagedep;
2427 struct inodedep *inodedep;
2428 struct mkdir *mkdir1, *mkdir2;
2431 * Whiteouts have no dependencies.
2433 if (newinum == WINO) {
2434 if (newdirbp != NULL)
2440 lbn = lblkno(fs, diroffset);
2441 offset = blkoff(fs, diroffset);
2442 MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2444 bzero(dap, sizeof(struct diradd));
2445 dap->da_list.wk_type = D_DIRADD;
2446 dap->da_offset = offset;
2447 dap->da_newinum = newinum;
2448 dap->da_state = ATTACHED;
2449 if (newdirbp == NULL) {
2450 dap->da_state |= DEPCOMPLETE;
2453 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
2454 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2456 mkdir1->md_list.wk_type = D_MKDIR;
2457 mkdir1->md_state = MKDIR_BODY;
2458 mkdir1->md_diradd = dap;
2459 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2461 mkdir2->md_list.wk_type = D_MKDIR;
2462 mkdir2->md_state = MKDIR_PARENT;
2463 mkdir2->md_diradd = dap;
2465 * Dependency on "." and ".." being written to disk.
2467 mkdir1->md_buf = newdirbp;
2469 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
2470 WORKLIST_INSERT_BP(newdirbp, &mkdir1->md_list);
2474 * Dependency on link count increase for parent directory
2477 if (inodedep_lookup(dp->i_fs, dp->i_number, 0, &inodedep) == 0
2478 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2479 dap->da_state &= ~MKDIR_PARENT;
2480 WORKITEM_FREE(mkdir2, D_MKDIR);
2482 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
2483 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
2487 * Link into parent directory pagedep to await its being written.
2489 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2490 WORKLIST_INSERT_BP(bp, &pagedep->pd_list);
2491 dap->da_pagedep = pagedep;
2492 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
2495 * Link into its inodedep. Put it on the id_bufwait list if the inode
2496 * is not yet written. If it is written, do the post-inode write
2497 * processing to put it on the id_pendinghd list.
2499 (void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep);
2500 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
2501 diradd_inode_written(dap, inodedep);
2503 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2508 * This procedure is called to change the offset of a directory
2509 * entry when compacting a directory block which must be owned
2510 * exclusively by the caller. Note that the actual entry movement
2511 * must be done in this procedure to ensure that no I/O completions
2512 * occur while the move is in progress.
2515 * dp: inode for directory
2516 * base: address of dp->i_offset
2517 * oldloc: address of old directory location
2518 * newloc: address of new directory location
2519 * entrysize: size of directory entry
2522 softdep_change_directoryentry_offset(struct inode *dp, caddr_t base,
2523 caddr_t oldloc, caddr_t newloc,
2526 int offset, oldoffset, newoffset;
2527 struct pagedep *pagedep;
2532 lbn = lblkno(dp->i_fs, dp->i_offset);
2533 offset = blkoff(dp->i_fs, dp->i_offset);
2534 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
2536 oldoffset = offset + (oldloc - base);
2537 newoffset = offset + (newloc - base);
2539 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
2540 if (dap->da_offset != oldoffset)
2542 dap->da_offset = newoffset;
2543 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
2545 LIST_REMOVE(dap, da_pdlist);
2546 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
2552 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
2553 if (dap->da_offset == oldoffset) {
2554 dap->da_offset = newoffset;
2560 bcopy(oldloc, newloc, entrysize);
2565 * Free a diradd dependency structure. This routine must be called
2566 * with splbio interrupts blocked.
2569 free_diradd(struct diradd *dap)
2571 struct dirrem *dirrem;
2572 struct pagedep *pagedep;
2573 struct inodedep *inodedep;
2574 struct mkdir *mkdir, *nextmd;
2577 if (lk.lkt_held == NOHOLDER)
2578 panic("free_diradd: lock not held");
2580 WORKLIST_REMOVE(&dap->da_list);
2581 LIST_REMOVE(dap, da_pdlist);
2582 if ((dap->da_state & DIRCHG) == 0) {
2583 pagedep = dap->da_pagedep;
2585 dirrem = dap->da_previous;
2586 pagedep = dirrem->dm_pagedep;
2587 dirrem->dm_dirinum = pagedep->pd_ino;
2588 add_to_worklist(&dirrem->dm_list);
2590 if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum,
2592 (void) free_inodedep(inodedep);
2593 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2594 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
2595 nextmd = LIST_NEXT(mkdir, md_mkdirs);
2596 if (mkdir->md_diradd != dap)
2598 dap->da_state &= ~mkdir->md_state;
2599 WORKLIST_REMOVE(&mkdir->md_list);
2600 LIST_REMOVE(mkdir, md_mkdirs);
2601 WORKITEM_FREE(mkdir, D_MKDIR);
2603 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2605 panic("free_diradd: unfound ref");
2608 WORKITEM_FREE(dap, D_DIRADD);
2612 * Directory entry removal dependencies.
2614 * When removing a directory entry, the entry's inode pointer must be
2615 * zero'ed on disk before the corresponding inode's link count is decremented
2616 * (possibly freeing the inode for re-use). This dependency is handled by
2617 * updating the directory entry but delaying the inode count reduction until
2618 * after the directory block has been written to disk. After this point, the
2619 * inode count can be decremented whenever it is convenient.
2623 * This routine should be called immediately after removing
2624 * a directory entry. The inode's link count should not be
2625 * decremented by the calling procedure -- the soft updates
2626 * code will do this task when it is safe.
2629 * bp: buffer containing directory block
2630 * dp: inode for the directory being modified
2631 * ip: inode for directory entry being removed
2632 * isrmdir: indicates if doing RMDIR
2635 softdep_setup_remove(struct buf *bp, struct inode *dp, struct inode *ip,
2638 struct dirrem *dirrem, *prevdirrem;
2641 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
2643 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2646 * If the COMPLETE flag is clear, then there were no active
2647 * entries and we want to roll back to a zeroed entry until
2648 * the new inode is committed to disk. If the COMPLETE flag is
2649 * set then we have deleted an entry that never made it to
2650 * disk. If the entry we deleted resulted from a name change,
2651 * then the old name still resides on disk. We cannot delete
2652 * its inode (returned to us in prevdirrem) until the zeroed
2653 * directory entry gets to disk. The new inode has never been
2654 * referenced on the disk, so can be deleted immediately.
2656 if ((dirrem->dm_state & COMPLETE) == 0) {
2657 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
2661 if (prevdirrem != NULL)
2662 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
2663 prevdirrem, dm_next);
2664 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
2666 handle_workitem_remove(dirrem);
2671 * Allocate a new dirrem if appropriate and return it along with
2672 * its associated pagedep. Called without a lock, returns with lock.
2674 static long num_dirrem; /* number of dirrem allocated */
2678 * bp: buffer containing directory block
2679 * dp: inode for the directory being modified
2680 * ip: inode for directory entry being removed
2681 * isrmdir: indicates if doing RMDIR
2682 * prevdirremp: previously referenced inode, if any
2684 static struct dirrem *
2685 newdirrem(struct buf *bp, struct inode *dp, struct inode *ip,
2686 int isrmdir, struct dirrem **prevdirremp)
2691 struct dirrem *dirrem;
2692 struct pagedep *pagedep;
2695 * Whiteouts have no deletion dependencies.
2698 panic("newdirrem: whiteout");
2700 * If we are over our limit, try to improve the situation.
2701 * Limiting the number of dirrem structures will also limit
2702 * the number of freefile and freeblks structures.
2704 if (num_dirrem > max_softdeps / 2 && speedup_syncer() == 0)
2705 (void) request_cleanup(FLUSH_REMOVE, 0);
2707 MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
2708 M_DIRREM, M_SOFTDEP_FLAGS);
2709 bzero(dirrem, sizeof(struct dirrem));
2710 dirrem->dm_list.wk_type = D_DIRREM;
2711 dirrem->dm_state = isrmdir ? RMDIR : 0;
2712 dirrem->dm_mnt = ITOV(ip)->v_mount;
2713 dirrem->dm_oldinum = ip->i_number;
2714 *prevdirremp = NULL;
2717 lbn = lblkno(dp->i_fs, dp->i_offset);
2718 offset = blkoff(dp->i_fs, dp->i_offset);
2719 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2720 WORKLIST_INSERT_BP(bp, &pagedep->pd_list);
2721 dirrem->dm_pagedep = pagedep;
2723 * Check for a diradd dependency for the same directory entry.
2724 * If present, then both dependencies become obsolete and can
2725 * be de-allocated. Check for an entry on both the pd_dirraddhd
2726 * list and the pd_pendinghd list.
2729 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
2730 if (dap->da_offset == offset)
2734 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
2735 if (dap->da_offset == offset)
2741 * Must be ATTACHED at this point.
2743 if ((dap->da_state & ATTACHED) == 0) {
2745 panic("newdirrem: not ATTACHED");
2747 if (dap->da_newinum != ip->i_number) {
2749 panic("newdirrem: inum %"PRId64" should be %"PRId64,
2750 ip->i_number, dap->da_newinum);
2753 * If we are deleting a changed name that never made it to disk,
2754 * then return the dirrem describing the previous inode (which
2755 * represents the inode currently referenced from this entry on disk).
2757 if ((dap->da_state & DIRCHG) != 0) {
2758 *prevdirremp = dap->da_previous;
2759 dap->da_state &= ~DIRCHG;
2760 dap->da_pagedep = pagedep;
2763 * We are deleting an entry that never made it to disk.
2764 * Mark it COMPLETE so we can delete its inode immediately.
2766 dirrem->dm_state |= COMPLETE;
2772 * Directory entry change dependencies.
2774 * Changing an existing directory entry requires that an add operation
2775 * be completed first followed by a deletion. The semantics for the addition
2776 * are identical to the description of adding a new entry above except
2777 * that the rollback is to the old inode number rather than zero. Once
2778 * the addition dependency is completed, the removal is done as described
2779 * in the removal routine above.
2783 * This routine should be called immediately after changing
2784 * a directory entry. The inode's link count should not be
2785 * decremented by the calling procedure -- the soft updates
2786 * code will perform this task when it is safe.
2789 * bp: buffer containing directory block
2790 * dp: inode for the directory being modified
2791 * ip: inode for directory entry being removed
2792 * newinum: new inode number for changed entry
2793 * isrmdir: indicates if doing RMDIR
2796 softdep_setup_directory_change(struct buf *bp, struct inode *dp,
2797 struct inode *ip, ino_t newinum,
2801 struct diradd *dap = NULL;
2802 struct dirrem *dirrem, *prevdirrem;
2803 struct pagedep *pagedep;
2804 struct inodedep *inodedep;
2806 offset = blkoff(dp->i_fs, dp->i_offset);
2809 * Whiteouts do not need diradd dependencies.
2811 if (newinum != WINO) {
2812 MALLOC(dap, struct diradd *, sizeof(struct diradd),
2813 M_DIRADD, M_SOFTDEP_FLAGS);
2814 bzero(dap, sizeof(struct diradd));
2815 dap->da_list.wk_type = D_DIRADD;
2816 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
2817 dap->da_offset = offset;
2818 dap->da_newinum = newinum;
2822 * Allocate a new dirrem and ACQUIRE_LOCK.
2824 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2825 pagedep = dirrem->dm_pagedep;
2827 * The possible values for isrmdir:
2828 * 0 - non-directory file rename
2829 * 1 - directory rename within same directory
2830 * inum - directory rename to new directory of given inode number
2831 * When renaming to a new directory, we are both deleting and
2832 * creating a new directory entry, so the link count on the new
2833 * directory should not change. Thus we do not need the followup
2834 * dirrem which is usually done in handle_workitem_remove. We set
2835 * the DIRCHG flag to tell handle_workitem_remove to skip the
2839 dirrem->dm_state |= DIRCHG;
2842 * Whiteouts have no additional dependencies,
2843 * so just put the dirrem on the correct list.
2845 if (newinum == WINO) {
2846 if ((dirrem->dm_state & COMPLETE) == 0) {
2847 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
2850 dirrem->dm_dirinum = pagedep->pd_ino;
2851 add_to_worklist(&dirrem->dm_list);
2858 * If the COMPLETE flag is clear, then there were no active
2859 * entries and we want to roll back to the previous inode until
2860 * the new inode is committed to disk. If the COMPLETE flag is
2861 * set, then we have deleted an entry that never made it to disk.
2862 * If the entry we deleted resulted from a name change, then the old
2863 * inode reference still resides on disk. Any rollback that we do
2864 * needs to be to that old inode (returned to us in prevdirrem). If
2865 * the entry we deleted resulted from a create, then there is
2866 * no entry on the disk, so we want to roll back to zero rather
2867 * than the uncommitted inode. In either of the COMPLETE cases we
2868 * want to immediately free the unwritten and unreferenced inode.
2870 if ((dirrem->dm_state & COMPLETE) == 0) {
2871 dap->da_previous = dirrem;
2873 if (prevdirrem != NULL) {
2874 dap->da_previous = prevdirrem;
2876 dap->da_state &= ~DIRCHG;
2877 dap->da_pagedep = pagedep;
2879 dirrem->dm_dirinum = pagedep->pd_ino;
2880 add_to_worklist(&dirrem->dm_list);
2883 * Link into its inodedep. Put it on the id_bufwait list if the inode
2884 * is not yet written. If it is written, do the post-inode write
2885 * processing to put it on the id_pendinghd list.
2887 if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 ||
2888 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2889 dap->da_state |= COMPLETE;
2890 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
2891 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
2893 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
2895 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2901 * Called whenever the link count on an inode is changed.
2902 * It creates an inode dependency so that the new reference(s)
2903 * to the inode cannot be committed to disk until the updated
2904 * inode has been written.
2907 * ip: the inode with the increased link count
2910 softdep_change_linkcnt(struct inode *ip)
2912 struct inodedep *inodedep;
2915 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep);
2916 if (ip->i_nlink < ip->i_effnlink) {
2918 panic("softdep_change_linkcnt: bad delta");
2920 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2925 * This workitem decrements the inode's link count.
2926 * If the link count reaches zero, the file is removed.
2929 handle_workitem_remove(struct dirrem *dirrem)
2931 struct inodedep *inodedep;
2937 if ((error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, &vp)) != 0) {
2938 softdep_error("handle_workitem_remove: vget", error);
2943 if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){
2945 panic("handle_workitem_remove: lost inodedep");
2948 * Normal file deletion.
2950 if ((dirrem->dm_state & RMDIR) == 0) {
2952 ip->i_flag |= IN_CHANGE;
2953 if (ip->i_nlink < ip->i_effnlink) {
2955 panic("handle_workitem_remove: bad file delta");
2957 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2961 WORKITEM_FREE(dirrem, D_DIRREM);
2965 * Directory deletion. Decrement reference count for both the
2966 * just deleted parent directory entry and the reference for ".".
2967 * Next truncate the directory to length zero. When the
2968 * truncation completes, arrange to have the reference count on
2969 * the parent decremented to account for the loss of "..".
2972 ip->i_flag |= IN_CHANGE;
2973 if (ip->i_nlink < ip->i_effnlink) {
2975 panic("handle_workitem_remove: bad dir delta");
2977 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2979 if ((error = ffs_truncate(vp, (off_t)0, 0, proc0.p_ucred)) != 0)
2980 softdep_error("handle_workitem_remove: truncate", error);
2982 * Rename a directory to a new parent. Since, we are both deleting
2983 * and creating a new directory entry, the link count on the new
2984 * directory should not change. Thus we skip the followup dirrem.
2986 if (dirrem->dm_state & DIRCHG) {
2989 WORKITEM_FREE(dirrem, D_DIRREM);
2993 * If the inodedep does not exist, then the zero'ed inode has
2994 * been written to disk. If the allocated inode has never been
2995 * written to disk, then the on-disk inode is zero'ed. In either
2996 * case we can remove the file immediately.
2999 dirrem->dm_state = 0;
3000 oldinum = dirrem->dm_oldinum;
3001 dirrem->dm_oldinum = dirrem->dm_dirinum;
3002 if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 ||
3003 check_inode_unwritten(inodedep)) {
3006 handle_workitem_remove(dirrem);
3009 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
3011 ip->i_flag |= IN_CHANGE;
3017 * Inode de-allocation dependencies.
3019 * When an inode's link count is reduced to zero, it can be de-allocated. We
3020 * found it convenient to postpone de-allocation until after the inode is
3021 * written to disk with its new link count (zero). At this point, all of the
3022 * on-disk inode's block pointers are nullified and, with careful dependency
3023 * list ordering, all dependencies related to the inode will be satisfied and
3024 * the corresponding dependency structures de-allocated. So, if/when the
3025 * inode is reused, there will be no mixing of old dependencies with new
3026 * ones. This artificial dependency is set up by the block de-allocation
3027 * procedure above (softdep_setup_freeblocks) and completed by the
3028 * following procedure.
3031 handle_workitem_freefile(struct freefile *freefile)
3035 struct inodedep *idp;
3040 error = inodedep_lookup(freefile->fx_fs, freefile->fx_oldinum, 0, &idp);
3043 panic("handle_workitem_freefile: inodedep survived");
3045 tip.i_devvp = freefile->fx_devvp;
3046 tip.i_dev = freefile->fx_devvp->v_rdev;
3047 tip.i_fs = freefile->fx_fs;
3049 if ((error = ffs_freefile(&vp, freefile->fx_oldinum, freefile->fx_mode)) != 0)
3050 softdep_error("handle_workitem_freefile", error);
3051 WORKITEM_FREE(freefile, D_FREEFILE);
3055 * Helper function which unlinks marker element from work list and returns
3056 * the next element on the list.
3058 static __inline struct worklist *
3059 markernext(struct worklist *marker)
3061 struct worklist *next;
3063 next = LIST_NEXT(marker, wk_list);
3064 LIST_REMOVE(marker, wk_list);
3069 * checkread, checkwrite
3073 softdep_checkread(struct buf *bp)
3079 softdep_checkwrite(struct buf *bp)
3087 * The dependency structures constructed above are most actively used when file
3088 * system blocks are written to disk. No constraints are placed on when a
3089 * block can be written, but unsatisfied update dependencies are made safe by
3090 * modifying (or replacing) the source memory for the duration of the disk
3091 * write. When the disk write completes, the memory block is again brought
3094 * In-core inode structure reclamation.
3096 * Because there are a finite number of "in-core" inode structures, they are
3097 * reused regularly. By transferring all inode-related dependencies to the
3098 * in-memory inode block and indexing them separately (via "inodedep"s), we
3099 * can allow "in-core" inode structures to be reused at any time and avoid
3100 * any increase in contention.
3102 * Called just before entering the device driver to initiate a new disk I/O.
3103 * The buffer must be locked, thus, no I/O completion operations can occur
3104 * while we are manipulating its associated dependencies.
3107 * bp: structure describing disk write to occur
3110 softdep_disk_io_initiation(struct buf *bp)
3112 struct worklist *wk;
3113 struct worklist marker;
3114 struct indirdep *indirdep;
3117 * We only care about write operations. There should never
3118 * be dependencies for reads.
3120 if (bp->b_cmd == BUF_CMD_READ)
3121 panic("softdep_disk_io_initiation: read");
3123 marker.wk_type = D_LAST + 1; /* Not a normal workitem */
3126 * Do any necessary pre-I/O processing.
3128 for (wk = LIST_FIRST(&bp->b_dep); wk; wk = markernext(&marker)) {
3129 LIST_INSERT_AFTER(wk, &marker, wk_list);
3131 switch (wk->wk_type) {
3134 initiate_write_filepage(WK_PAGEDEP(wk), bp);
3138 initiate_write_inodeblock(WK_INODEDEP(wk), bp);
3142 indirdep = WK_INDIRDEP(wk);
3143 if (indirdep->ir_state & GOINGAWAY)
3144 panic("disk_io_initiation: indirdep gone");
3146 * If there are no remaining dependencies, this
3147 * will be writing the real pointers, so the
3148 * dependency can be freed.
3150 if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) {
3151 indirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
3152 brelse(indirdep->ir_savebp);
3153 /* inline expand WORKLIST_REMOVE(wk); */
3154 wk->wk_state &= ~ONWORKLIST;
3155 LIST_REMOVE(wk, wk_list);
3156 WORKITEM_FREE(indirdep, D_INDIRDEP);
3160 * Replace up-to-date version with safe version.
3162 MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
3163 M_INDIRDEP, M_SOFTDEP_FLAGS);
3165 indirdep->ir_state &= ~ATTACHED;
3166 indirdep->ir_state |= UNDONE;
3167 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3168 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3180 panic("handle_disk_io_initiation: Unexpected type %s",
3181 TYPENAME(wk->wk_type));
3188 * Called from within the procedure above to deal with unsatisfied
3189 * allocation dependencies in a directory. The buffer must be locked,
3190 * thus, no I/O completion operations can occur while we are
3191 * manipulating its associated dependencies.
3194 initiate_write_filepage(struct pagedep *pagedep, struct buf *bp)
3200 if (pagedep->pd_state & IOSTARTED) {
3202 * This can only happen if there is a driver that does not
3203 * understand chaining. Here biodone will reissue the call
3204 * to strategy for the incomplete buffers.
3206 kprintf("initiate_write_filepage: already started\n");
3209 pagedep->pd_state |= IOSTARTED;
3211 for (i = 0; i < DAHASHSZ; i++) {
3212 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3213 ep = (struct direct *)
3214 ((char *)bp->b_data + dap->da_offset);
3215 if (ep->d_ino != dap->da_newinum) {
3217 panic("%s: dir inum %d != new %"PRId64,
3218 "initiate_write_filepage",
3219 ep->d_ino, dap->da_newinum);
3221 if (dap->da_state & DIRCHG)
3222 ep->d_ino = dap->da_previous->dm_oldinum;
3225 dap->da_state &= ~ATTACHED;
3226 dap->da_state |= UNDONE;
3233 * Called from within the procedure above to deal with unsatisfied
3234 * allocation dependencies in an inodeblock. The buffer must be
3235 * locked, thus, no I/O completion operations can occur while we
3236 * are manipulating its associated dependencies.
3239 * bp: The inode block
3242 initiate_write_inodeblock(struct inodedep *inodedep, struct buf *bp)
3244 struct allocdirect *adp, *lastadp;
3245 struct ufs1_dinode *dp;
3246 struct ufs1_dinode *sip;
3248 ufs_lbn_t prevlbn = 0;
3251 if (inodedep->id_state & IOSTARTED)
3252 panic("initiate_write_inodeblock: already started");
3253 inodedep->id_state |= IOSTARTED;
3254 fs = inodedep->id_fs;
3255 dp = (struct ufs1_dinode *)bp->b_data +
3256 ino_to_fsbo(fs, inodedep->id_ino);
3258 * If the bitmap is not yet written, then the allocated
3259 * inode cannot be written to disk.
3261 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3262 if (inodedep->id_savedino != NULL)
3263 panic("initiate_write_inodeblock: already doing I/O");
3264 MALLOC(sip, struct ufs1_dinode *,
3265 sizeof(struct ufs1_dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3266 inodedep->id_savedino = sip;
3267 *inodedep->id_savedino = *dp;
3268 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
3269 dp->di_gen = inodedep->id_savedino->di_gen;
3273 * If no dependencies, then there is nothing to roll back.
3275 inodedep->id_savedsize = dp->di_size;
3276 if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL)
3279 * Set the dependencies to busy.
3282 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3283 adp = TAILQ_NEXT(adp, ad_next)) {
3285 if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3287 panic("softdep_write_inodeblock: lbn order");
3289 prevlbn = adp->ad_lbn;
3290 if (adp->ad_lbn < NDADDR &&
3291 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3293 panic("%s: direct pointer #%ld mismatch %d != %d",
3294 "softdep_write_inodeblock", adp->ad_lbn,
3295 dp->di_db[adp->ad_lbn], adp->ad_newblkno);
3297 if (adp->ad_lbn >= NDADDR &&
3298 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3300 panic("%s: indirect pointer #%ld mismatch %d != %d",
3301 "softdep_write_inodeblock", adp->ad_lbn - NDADDR,
3302 dp->di_ib[adp->ad_lbn - NDADDR], adp->ad_newblkno);
3304 deplist |= 1 << adp->ad_lbn;
3305 if ((adp->ad_state & ATTACHED) == 0) {
3307 panic("softdep_write_inodeblock: Unknown state 0x%x",
3310 #endif /* DIAGNOSTIC */
3311 adp->ad_state &= ~ATTACHED;
3312 adp->ad_state |= UNDONE;
3315 * The on-disk inode cannot claim to be any larger than the last
3316 * fragment that has been written. Otherwise, the on-disk inode
3317 * might have fragments that were not the last block in the file
3318 * which would corrupt the filesystem.
3320 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3321 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3322 if (adp->ad_lbn >= NDADDR)
3324 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3325 /* keep going until hitting a rollback to a frag */
3326 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3328 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3329 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3331 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3333 panic("softdep_write_inodeblock: lost dep1");
3335 #endif /* DIAGNOSTIC */
3338 for (i = 0; i < NIADDR; i++) {
3340 if (dp->di_ib[i] != 0 &&
3341 (deplist & ((1 << NDADDR) << i)) == 0) {
3343 panic("softdep_write_inodeblock: lost dep2");
3345 #endif /* DIAGNOSTIC */
3352 * If we have zero'ed out the last allocated block of the file,
3353 * roll back the size to the last currently allocated block.
3354 * We know that this last allocated block is a full-sized as
3355 * we already checked for fragments in the loop above.
3357 if (lastadp != NULL &&
3358 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3359 for (i = lastadp->ad_lbn; i >= 0; i--)
3360 if (dp->di_db[i] != 0)
3362 dp->di_size = (i + 1) * fs->fs_bsize;
3365 * The only dependencies are for indirect blocks.
3367 * The file size for indirect block additions is not guaranteed.
3368 * Such a guarantee would be non-trivial to achieve. The conventional
3369 * synchronous write implementation also does not make this guarantee.
3370 * Fsck should catch and fix discrepancies. Arguably, the file size
3371 * can be over-estimated without destroying integrity when the file
3372 * moves into the indirect blocks (i.e., is large). If we want to
3373 * postpone fsck, we are stuck with this argument.
3375 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3376 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3381 * This routine is called during the completion interrupt
3382 * service routine for a disk write (from the procedure called
3383 * by the device driver to inform the filesystem caches of
3384 * a request completion). It should be called early in this
3385 * procedure, before the block is made available to other
3386 * processes or other routines are called.
3389 * bp: describes the completed disk write
3392 softdep_disk_write_complete(struct buf *bp)
3394 struct worklist *wk;
3395 struct workhead reattach;
3396 struct newblk *newblk;
3397 struct allocindir *aip;
3398 struct allocdirect *adp;
3399 struct indirdep *indirdep;
3400 struct inodedep *inodedep;
3401 struct bmsafemap *bmsafemap;
3404 if (lk.lkt_held != NOHOLDER)
3405 panic("softdep_disk_write_complete: lock is held");
3406 lk.lkt_held = SPECIAL_FLAG;
3408 LIST_INIT(&reattach);
3409 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
3410 WORKLIST_REMOVE(wk);
3411 switch (wk->wk_type) {
3414 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
3415 WORKLIST_INSERT(&reattach, wk);
3419 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
3420 WORKLIST_INSERT(&reattach, wk);
3424 bmsafemap = WK_BMSAFEMAP(wk);
3425 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
3426 newblk->nb_state |= DEPCOMPLETE;
3427 newblk->nb_bmsafemap = NULL;
3428 LIST_REMOVE(newblk, nb_deps);
3431 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
3432 adp->ad_state |= DEPCOMPLETE;
3434 LIST_REMOVE(adp, ad_deps);
3435 handle_allocdirect_partdone(adp);
3438 LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
3439 aip->ai_state |= DEPCOMPLETE;
3441 LIST_REMOVE(aip, ai_deps);
3442 handle_allocindir_partdone(aip);
3445 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
3446 inodedep->id_state |= DEPCOMPLETE;
3447 LIST_REMOVE(inodedep, id_deps);
3448 inodedep->id_buf = NULL;
3450 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
3454 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
3458 adp = WK_ALLOCDIRECT(wk);
3459 adp->ad_state |= COMPLETE;
3460 handle_allocdirect_partdone(adp);
3464 aip = WK_ALLOCINDIR(wk);
3465 aip->ai_state |= COMPLETE;
3466 handle_allocindir_partdone(aip);
3470 indirdep = WK_INDIRDEP(wk);
3471 if (indirdep->ir_state & GOINGAWAY) {
3472 lk.lkt_held = NOHOLDER;
3473 panic("disk_write_complete: indirdep gone");
3475 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
3476 FREE(indirdep->ir_saveddata, M_INDIRDEP);
3477 indirdep->ir_saveddata = 0;
3478 indirdep->ir_state &= ~UNDONE;
3479 indirdep->ir_state |= ATTACHED;
3480 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
3481 handle_allocindir_partdone(aip);
3482 if (aip == LIST_FIRST(&indirdep->ir_donehd)) {
3483 lk.lkt_held = NOHOLDER;
3484 panic("disk_write_complete: not gone");
3487 WORKLIST_INSERT(&reattach, wk);
3488 if ((bp->b_flags & B_DELWRI) == 0)
3489 stat_indir_blk_ptrs++;
3494 lk.lkt_held = NOHOLDER;
3495 panic("handle_disk_write_complete: Unknown type %s",
3496 TYPENAME(wk->wk_type));
3501 * Reattach any requests that must be redone.
3503 while ((wk = LIST_FIRST(&reattach)) != NULL) {
3504 WORKLIST_REMOVE(wk);
3505 WORKLIST_INSERT_BP(bp, wk);
3508 if (lk.lkt_held != SPECIAL_FLAG)
3509 panic("softdep_disk_write_complete: lock lost");
3510 lk.lkt_held = NOHOLDER;
3515 * Called from within softdep_disk_write_complete above. Note that
3516 * this routine is always called from interrupt level with further
3517 * splbio interrupts blocked.
3520 * adp: the completed allocdirect
3523 handle_allocdirect_partdone(struct allocdirect *adp)
3525 struct allocdirect *listadp;
3526 struct inodedep *inodedep;
3529 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3531 if (adp->ad_buf != NULL) {
3532 lk.lkt_held = NOHOLDER;
3533 panic("handle_allocdirect_partdone: dangling dep");
3536 * The on-disk inode cannot claim to be any larger than the last
3537 * fragment that has been written. Otherwise, the on-disk inode
3538 * might have fragments that were not the last block in the file
3539 * which would corrupt the filesystem. Thus, we cannot free any
3540 * allocdirects after one whose ad_oldblkno claims a fragment as
3541 * these blocks must be rolled back to zero before writing the inode.
3542 * We check the currently active set of allocdirects in id_inoupdt.
3544 inodedep = adp->ad_inodedep;
3545 bsize = inodedep->id_fs->fs_bsize;
3546 TAILQ_FOREACH(listadp, &inodedep->id_inoupdt, ad_next) {
3547 /* found our block */
3550 /* continue if ad_oldlbn is not a fragment */
3551 if (listadp->ad_oldsize == 0 ||
3552 listadp->ad_oldsize == bsize)
3554 /* hit a fragment */
3558 * If we have reached the end of the current list without
3559 * finding the just finished dependency, then it must be
3560 * on the future dependency list. Future dependencies cannot
3561 * be freed until they are moved to the current list.
3563 if (listadp == NULL) {
3565 TAILQ_FOREACH(listadp, &inodedep->id_newinoupdt, ad_next)
3566 /* found our block */
3569 if (listadp == NULL) {
3570 lk.lkt_held = NOHOLDER;
3571 panic("handle_allocdirect_partdone: lost dep");
3577 * If we have found the just finished dependency, then free
3578 * it along with anything that follows it that is complete.
3580 for (; adp; adp = listadp) {
3581 listadp = TAILQ_NEXT(adp, ad_next);
3582 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3584 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
3589 * Called from within softdep_disk_write_complete above. Note that
3590 * this routine is always called from interrupt level with further
3591 * splbio interrupts blocked.
3594 * aip: the completed allocindir
3597 handle_allocindir_partdone(struct allocindir *aip)
3599 struct indirdep *indirdep;
3601 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
3603 if (aip->ai_buf != NULL) {
3604 lk.lkt_held = NOHOLDER;
3605 panic("handle_allocindir_partdone: dangling dependency");
3607 indirdep = aip->ai_indirdep;
3608 if (indirdep->ir_state & UNDONE) {
3609 LIST_REMOVE(aip, ai_next);
3610 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
3613 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
3615 LIST_REMOVE(aip, ai_next);
3616 if (aip->ai_freefrag != NULL)
3617 add_to_worklist(&aip->ai_freefrag->ff_list);
3618 WORKITEM_FREE(aip, D_ALLOCINDIR);
3622 * Called from within softdep_disk_write_complete above to restore
3623 * in-memory inode block contents to their most up-to-date state. Note
3624 * that this routine is always called from interrupt level with further
3625 * splbio interrupts blocked.
3628 * bp: buffer containing the inode block
3631 handle_written_inodeblock(struct inodedep *inodedep, struct buf *bp)
3633 struct worklist *wk, *filefree;
3634 struct allocdirect *adp, *nextadp;
3635 struct ufs1_dinode *dp;
3638 if ((inodedep->id_state & IOSTARTED) == 0) {
3639 lk.lkt_held = NOHOLDER;
3640 panic("handle_written_inodeblock: not started");
3642 inodedep->id_state &= ~IOSTARTED;
3643 dp = (struct ufs1_dinode *)bp->b_data +
3644 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
3646 * If we had to rollback the inode allocation because of
3647 * bitmaps being incomplete, then simply restore it.
3648 * Keep the block dirty so that it will not be reclaimed until
3649 * all associated dependencies have been cleared and the
3650 * corresponding updates written to disk.
3652 if (inodedep->id_savedino != NULL) {
3653 *dp = *inodedep->id_savedino;
3654 FREE(inodedep->id_savedino, M_INODEDEP);
3655 inodedep->id_savedino = NULL;
3656 if ((bp->b_flags & B_DELWRI) == 0)
3657 stat_inode_bitmap++;
3661 inodedep->id_state |= COMPLETE;
3663 * Roll forward anything that had to be rolled back before
3664 * the inode could be updated.
3667 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
3668 nextadp = TAILQ_NEXT(adp, ad_next);
3669 if (adp->ad_state & ATTACHED) {
3670 lk.lkt_held = NOHOLDER;
3671 panic("handle_written_inodeblock: new entry");
3673 if (adp->ad_lbn < NDADDR) {
3674 if (dp->di_db[adp->ad_lbn] != adp->ad_oldblkno) {
3675 lk.lkt_held = NOHOLDER;
3676 panic("%s: %s #%ld mismatch %d != %d",
3677 "handle_written_inodeblock",
3678 "direct pointer", adp->ad_lbn,
3679 dp->di_db[adp->ad_lbn], adp->ad_oldblkno);
3681 dp->di_db[adp->ad_lbn] = adp->ad_newblkno;
3683 if (dp->di_ib[adp->ad_lbn - NDADDR] != 0) {
3684 lk.lkt_held = NOHOLDER;
3685 panic("%s: %s #%ld allocated as %d",
3686 "handle_written_inodeblock",
3687 "indirect pointer", adp->ad_lbn - NDADDR,
3688 dp->di_ib[adp->ad_lbn - NDADDR]);
3690 dp->di_ib[adp->ad_lbn - NDADDR] = adp->ad_newblkno;
3692 adp->ad_state &= ~UNDONE;
3693 adp->ad_state |= ATTACHED;
3696 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
3697 stat_direct_blk_ptrs++;
3699 * Reset the file size to its most up-to-date value.
3701 if (inodedep->id_savedsize == -1) {
3702 lk.lkt_held = NOHOLDER;
3703 panic("handle_written_inodeblock: bad size");
3705 if (dp->di_size != inodedep->id_savedsize) {
3706 dp->di_size = inodedep->id_savedsize;
3709 inodedep->id_savedsize = -1;
3711 * If there were any rollbacks in the inode block, then it must be
3712 * marked dirty so that its will eventually get written back in
3718 * Process any allocdirects that completed during the update.
3720 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
3721 handle_allocdirect_partdone(adp);
3723 * Process deallocations that were held pending until the
3724 * inode had been written to disk. Freeing of the inode
3725 * is delayed until after all blocks have been freed to
3726 * avoid creation of new <vfsid, inum, lbn> triples
3727 * before the old ones have been deleted.
3730 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
3731 WORKLIST_REMOVE(wk);
3732 switch (wk->wk_type) {
3736 * We defer adding filefree to the worklist until
3737 * all other additions have been made to ensure
3738 * that it will be done after all the old blocks
3741 if (filefree != NULL) {
3742 lk.lkt_held = NOHOLDER;
3743 panic("handle_written_inodeblock: filefree");
3749 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
3753 diradd_inode_written(WK_DIRADD(wk), inodedep);
3757 wk->wk_state |= COMPLETE;
3758 if ((wk->wk_state & ALLCOMPLETE) != ALLCOMPLETE)
3760 /* -- fall through -- */
3763 add_to_worklist(wk);
3767 lk.lkt_held = NOHOLDER;
3768 panic("handle_written_inodeblock: Unknown type %s",
3769 TYPENAME(wk->wk_type));
3773 if (filefree != NULL) {
3774 if (free_inodedep(inodedep) == 0) {
3775 lk.lkt_held = NOHOLDER;
3776 panic("handle_written_inodeblock: live inodedep");
3778 add_to_worklist(filefree);
3783 * If no outstanding dependencies, free it.
3785 if (free_inodedep(inodedep) || TAILQ_FIRST(&inodedep->id_inoupdt) == 0)
3787 return (hadchanges);
3791 * Process a diradd entry after its dependent inode has been written.
3792 * This routine must be called with splbio interrupts blocked.
3795 diradd_inode_written(struct diradd *dap, struct inodedep *inodedep)
3797 struct pagedep *pagedep;
3799 dap->da_state |= COMPLETE;
3800 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3801 if (dap->da_state & DIRCHG)
3802 pagedep = dap->da_previous->dm_pagedep;
3804 pagedep = dap->da_pagedep;
3805 LIST_REMOVE(dap, da_pdlist);
3806 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3808 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3812 * Handle the completion of a mkdir dependency.
3815 handle_written_mkdir(struct mkdir *mkdir, int type)
3818 struct pagedep *pagedep;
3820 if (mkdir->md_state != type) {
3821 lk.lkt_held = NOHOLDER;
3822 panic("handle_written_mkdir: bad type");
3824 dap = mkdir->md_diradd;
3825 dap->da_state &= ~type;
3826 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
3827 dap->da_state |= DEPCOMPLETE;
3828 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3829 if (dap->da_state & DIRCHG)
3830 pagedep = dap->da_previous->dm_pagedep;
3832 pagedep = dap->da_pagedep;
3833 LIST_REMOVE(dap, da_pdlist);
3834 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3836 LIST_REMOVE(mkdir, md_mkdirs);
3837 WORKITEM_FREE(mkdir, D_MKDIR);
3841 * Called from within softdep_disk_write_complete above.
3842 * A write operation was just completed. Removed inodes can
3843 * now be freed and associated block pointers may be committed.
3844 * Note that this routine is always called from interrupt level
3845 * with further splbio interrupts blocked.
3848 * bp: buffer containing the written page
3851 handle_written_filepage(struct pagedep *pagedep, struct buf *bp)
3853 struct dirrem *dirrem;
3854 struct diradd *dap, *nextdap;
3858 if ((pagedep->pd_state & IOSTARTED) == 0) {
3859 lk.lkt_held = NOHOLDER;
3860 panic("handle_written_filepage: not started");
3862 pagedep->pd_state &= ~IOSTARTED;
3864 * Process any directory removals that have been committed.
3866 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
3867 LIST_REMOVE(dirrem, dm_next);
3868 dirrem->dm_dirinum = pagedep->pd_ino;
3869 add_to_worklist(&dirrem->dm_list);
3872 * Free any directory additions that have been committed.
3874 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
3877 * Uncommitted directory entries must be restored.
3879 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
3880 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
3882 nextdap = LIST_NEXT(dap, da_pdlist);
3883 if (dap->da_state & ATTACHED) {
3884 lk.lkt_held = NOHOLDER;
3885 panic("handle_written_filepage: attached");
3887 ep = (struct direct *)
3888 ((char *)bp->b_data + dap->da_offset);
3889 ep->d_ino = dap->da_newinum;
3890 dap->da_state &= ~UNDONE;
3891 dap->da_state |= ATTACHED;
3894 * If the inode referenced by the directory has
3895 * been written out, then the dependency can be
3896 * moved to the pending list.
3898 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3899 LIST_REMOVE(dap, da_pdlist);
3900 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
3906 * If there were any rollbacks in the directory, then it must be
3907 * marked dirty so that its will eventually get written back in
3911 if ((bp->b_flags & B_DELWRI) == 0)
3916 * If no dependencies remain, the pagedep will be freed.
3917 * Otherwise it will remain to update the page before it
3918 * is written back to disk.
3920 if (LIST_FIRST(&pagedep->pd_pendinghd) == 0) {
3921 for (i = 0; i < DAHASHSZ; i++)
3922 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) != NULL)
3924 if (i == DAHASHSZ) {
3925 LIST_REMOVE(pagedep, pd_hash);
3926 WORKITEM_FREE(pagedep, D_PAGEDEP);
3934 * Writing back in-core inode structures.
3936 * The filesystem only accesses an inode's contents when it occupies an
3937 * "in-core" inode structure. These "in-core" structures are separate from
3938 * the page frames used to cache inode blocks. Only the latter are
3939 * transferred to/from the disk. So, when the updated contents of the
3940 * "in-core" inode structure are copied to the corresponding in-memory inode
3941 * block, the dependencies are also transferred. The following procedure is
3942 * called when copying a dirty "in-core" inode to a cached inode block.
3946 * Called when an inode is loaded from disk. If the effective link count
3947 * differed from the actual link count when it was last flushed, then we
3948 * need to ensure that the correct effective link count is put back.
3951 * ip: the "in_core" copy of the inode
3954 softdep_load_inodeblock(struct inode *ip)
3956 struct inodedep *inodedep;
3959 * Check for alternate nlink count.
3961 ip->i_effnlink = ip->i_nlink;
3963 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3967 ip->i_effnlink -= inodedep->id_nlinkdelta;
3972 * This routine is called just before the "in-core" inode
3973 * information is to be copied to the in-memory inode block.
3974 * Recall that an inode block contains several inodes. If
3975 * the force flag is set, then the dependencies will be
3976 * cleared so that the update can always be made. Note that
3977 * the buffer is locked when this routine is called, so we
3978 * will never be in the middle of writing the inode block
3982 * ip: the "in_core" copy of the inode
3983 * bp: the buffer containing the inode block
3984 * waitfor: nonzero => update must be allowed
3987 softdep_update_inodeblock(struct inode *ip, struct buf *bp,
3990 struct inodedep *inodedep;
3991 struct worklist *wk;
3995 * If the effective link count is not equal to the actual link
3996 * count, then we must track the difference in an inodedep while
3997 * the inode is (potentially) tossed out of the cache. Otherwise,
3998 * if there is no existing inodedep, then there are no dependencies
4002 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
4004 if (ip->i_effnlink != ip->i_nlink)
4005 panic("softdep_update_inodeblock: bad link count");
4008 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) {
4010 panic("softdep_update_inodeblock: bad delta");
4013 * Changes have been initiated. Anything depending on these
4014 * changes cannot occur until this inode has been written.
4016 inodedep->id_state &= ~COMPLETE;
4017 if ((inodedep->id_state & ONWORKLIST) == 0)
4018 WORKLIST_INSERT_BP(bp, &inodedep->id_list);
4020 * Any new dependencies associated with the incore inode must
4021 * now be moved to the list associated with the buffer holding
4022 * the in-memory copy of the inode. Once merged process any
4023 * allocdirects that are completed by the merger.
4025 merge_inode_lists(inodedep);
4026 if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL)
4027 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
4029 * Now that the inode has been pushed into the buffer, the
4030 * operations dependent on the inode being written to disk
4031 * can be moved to the id_bufwait so that they will be
4032 * processed when the buffer I/O completes.
4034 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
4035 WORKLIST_REMOVE(wk);
4036 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
4039 * Newly allocated inodes cannot be written until the bitmap
4040 * that allocates them have been written (indicated by
4041 * DEPCOMPLETE being set in id_state). If we are doing a
4042 * forced sync (e.g., an fsync on a file), we force the bitmap
4043 * to be written so that the update can be done.
4045 if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) {
4049 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
4052 (error = bwrite(inodedep->id_buf)) != 0)
4053 softdep_error("softdep_update_inodeblock: bwrite", error);
4057 * Merge the new inode dependency list (id_newinoupdt) into the old
4058 * inode dependency list (id_inoupdt). This routine must be called
4059 * with splbio interrupts blocked.
4062 merge_inode_lists(struct inodedep *inodedep)
4064 struct allocdirect *listadp, *newadp;
4066 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
4067 for (listadp = TAILQ_FIRST(&inodedep->id_inoupdt); listadp && newadp;) {
4068 if (listadp->ad_lbn < newadp->ad_lbn) {
4069 listadp = TAILQ_NEXT(listadp, ad_next);
4072 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
4073 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
4074 if (listadp->ad_lbn == newadp->ad_lbn) {
4075 allocdirect_merge(&inodedep->id_inoupdt, newadp,
4079 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
4081 while ((newadp = TAILQ_FIRST(&inodedep->id_newinoupdt)) != NULL) {
4082 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
4083 TAILQ_INSERT_TAIL(&inodedep->id_inoupdt, newadp, ad_next);
4088 * If we are doing an fsync, then we must ensure that any directory
4089 * entries for the inode have been written after the inode gets to disk.
4092 * vp: the "in_core" copy of the inode
4095 softdep_fsync(struct vnode *vp)
4097 struct inodedep *inodedep;
4098 struct pagedep *pagedep;
4099 struct worklist *wk;
4106 int error, flushparent;
4111 * Move check from original kernel code, possibly not needed any
4112 * more with the per-mount bioops.
4114 if ((vp->v_mount->mnt_flag & MNT_SOFTDEP) == 0)
4120 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) {
4124 if (LIST_FIRST(&inodedep->id_inowait) != NULL ||
4125 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
4126 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
4127 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) {
4129 panic("softdep_fsync: pending ops");
4131 for (error = 0, flushparent = 0; ; ) {
4132 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
4134 if (wk->wk_type != D_DIRADD) {
4136 panic("softdep_fsync: Unexpected type %s",
4137 TYPENAME(wk->wk_type));
4139 dap = WK_DIRADD(wk);
4141 * Flush our parent if this directory entry
4142 * has a MKDIR_PARENT dependency.
4144 if (dap->da_state & DIRCHG)
4145 pagedep = dap->da_previous->dm_pagedep;
4147 pagedep = dap->da_pagedep;
4148 mnt = pagedep->pd_mnt;
4149 parentino = pagedep->pd_ino;
4150 lbn = pagedep->pd_lbn;
4151 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) {
4153 panic("softdep_fsync: dirty");
4155 flushparent = dap->da_state & MKDIR_PARENT;
4157 * If we are being fsync'ed as part of vgone'ing this vnode,
4158 * then we will not be able to release and recover the
4159 * vnode below, so we just have to give up on writing its
4160 * directory entry out. It will eventually be written, just
4161 * not now, but then the user was not asking to have it
4162 * written, so we are not breaking any promises.
4164 if (vp->v_flag & VRECLAIMED)
4167 * We prevent deadlock by always fetching inodes from the
4168 * root, moving down the directory tree. Thus, when fetching
4169 * our parent directory, we must unlock ourselves before
4170 * requesting the lock on our parent. See the comment in
4171 * ufs_lookup for details on possible races.
4175 error = VFS_VGET(mnt, parentino, &pvp);
4176 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4180 if ((error = ffs_update(pvp, 1)) != 0) {
4186 * Flush directory page containing the inode's name.
4188 error = bread(pvp, lblktodoff(fs, lbn), blksize(fs, VTOI(pvp), lbn), &bp);
4195 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0)
4203 * Flush all the dirty bitmaps associated with the block device
4204 * before flushing the rest of the dirty blocks so as to reduce
4205 * the number of dependencies that will have to be rolled back.
4207 static int softdep_fsync_mountdev_bp(struct buf *bp, void *data);
4210 softdep_fsync_mountdev(struct vnode *vp)
4212 if (!vn_isdisk(vp, NULL))
4213 panic("softdep_fsync_mountdev: vnode not a disk");
4215 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
4216 softdep_fsync_mountdev_bp, vp);
4217 drain_output(vp, 1);
4222 softdep_fsync_mountdev_bp(struct buf *bp, void *data)
4224 struct worklist *wk;
4225 struct vnode *vp = data;
4228 * If it is already scheduled, skip to the next buffer.
4230 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
4232 if (bp->b_vp != vp || (bp->b_flags & B_DELWRI) == 0) {
4234 kprintf("softdep_fsync_mountdev_bp: warning, buffer %p ripped out from under vnode %p\n", bp, vp);
4238 * We are only interested in bitmaps with outstanding
4241 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
4242 wk->wk_type != D_BMSAFEMAP) {
4254 * This routine is called when we are trying to synchronously flush a
4255 * file. This routine must eliminate any filesystem metadata dependencies
4256 * so that the syncing routine can succeed by pushing the dirty blocks
4257 * associated with the file. If any I/O errors occur, they are returned.
4259 struct softdep_sync_metadata_info {
4264 static int softdep_sync_metadata_bp(struct buf *bp, void *data);
4267 softdep_sync_metadata(struct vnode *vp, struct thread *td)
4269 struct softdep_sync_metadata_info info;
4273 * Check whether this vnode is involved in a filesystem
4274 * that is doing soft dependency processing.
4276 if (!vn_isdisk(vp, NULL)) {
4277 if (!DOINGSOFTDEP(vp))
4280 if (vp->v_rdev->si_mountpoint == NULL ||
4281 (vp->v_rdev->si_mountpoint->mnt_flag & MNT_SOFTDEP) == 0)
4284 * Ensure that any direct block dependencies have been cleared.
4287 if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) {
4292 * For most files, the only metadata dependencies are the
4293 * cylinder group maps that allocate their inode or blocks.
4294 * The block allocation dependencies can be found by traversing
4295 * the dependency lists for any buffers that remain on their
4296 * dirty buffer list. The inode allocation dependency will
4297 * be resolved when the inode is updated with MNT_WAIT.
4298 * This work is done in two passes. The first pass grabs most
4299 * of the buffers and begins asynchronously writing them. The
4300 * only way to wait for these asynchronous writes is to sleep
4301 * on the filesystem vnode which may stay busy for a long time
4302 * if the filesystem is active. So, instead, we make a second
4303 * pass over the dependencies blocking on each write. In the
4304 * usual case we will be blocking against a write that we
4305 * initiated, so when it is done the dependency will have been
4306 * resolved. Thus the second pass is expected to end quickly.
4308 waitfor = MNT_NOWAIT;
4311 * We must wait for any I/O in progress to finish so that
4312 * all potential buffers on the dirty list will be visible.
4314 drain_output(vp, 1);
4316 info.waitfor = waitfor;
4317 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
4318 softdep_sync_metadata_bp, &info);
4321 return(-error); /* error code */
4325 * The brief unlock is to allow any pent up dependency
4326 * processing to be done. Then proceed with the second pass.
4328 if (waitfor == MNT_NOWAIT) {
4336 * If we have managed to get rid of all the dirty buffers,
4337 * then we are done. For certain directories and block
4338 * devices, we may need to do further work.
4340 * We must wait for any I/O in progress to finish so that
4341 * all potential buffers on the dirty list will be visible.
4343 drain_output(vp, 1);
4344 if (RB_EMPTY(&vp->v_rbdirty_tree)) {
4351 * If we are trying to sync a block device, some of its buffers may
4352 * contain metadata that cannot be written until the contents of some
4353 * partially written files have been written to disk. The only easy
4354 * way to accomplish this is to sync the entire filesystem (luckily
4355 * this happens rarely).
4357 if (vn_isdisk(vp, NULL) &&
4359 vp->v_rdev->si_mountpoint && !vn_islocked(vp) &&
4360 (error = VFS_SYNC(vp->v_rdev->si_mountpoint, MNT_WAIT)) != 0)
4366 softdep_sync_metadata_bp(struct buf *bp, void *data)
4368 struct softdep_sync_metadata_info *info = data;
4369 struct pagedep *pagedep;
4370 struct allocdirect *adp;
4371 struct allocindir *aip;
4372 struct worklist *wk;
4377 if (getdirtybuf(&bp, MNT_WAIT) == 0) {
4378 kprintf("softdep_sync_metadata_bp(1): caught buf %p going away\n", bp);
4381 if (bp->b_vp != info->vp || (bp->b_flags & B_DELWRI) == 0) {
4382 kprintf("softdep_sync_metadata_bp(2): caught buf %p going away vp %p\n", bp, info->vp);
4388 * As we hold the buffer locked, none of its dependencies
4391 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4392 switch (wk->wk_type) {
4395 adp = WK_ALLOCDIRECT(wk);
4396 if (adp->ad_state & DEPCOMPLETE)
4399 if (getdirtybuf(&nbp, info->waitfor) == 0)
4402 if (info->waitfor == MNT_NOWAIT) {
4404 } else if ((error = bwrite(nbp)) != 0) {
4413 aip = WK_ALLOCINDIR(wk);
4414 if (aip->ai_state & DEPCOMPLETE)
4417 if (getdirtybuf(&nbp, info->waitfor) == 0)
4420 if (info->waitfor == MNT_NOWAIT) {
4422 } else if ((error = bwrite(nbp)) != 0) {
4433 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
4434 if (aip->ai_state & DEPCOMPLETE)
4437 if (getdirtybuf(&nbp, MNT_WAIT) == 0)
4440 if ((error = bwrite(nbp)) != 0) {
4451 if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs,
4452 WK_INODEDEP(wk)->id_ino)) != 0) {
4462 * We are trying to sync a directory that may
4463 * have dependencies on both its own metadata
4464 * and/or dependencies on the inodes of any
4465 * recently allocated files. We walk its diradd
4466 * lists pushing out the associated inode.
4468 pagedep = WK_PAGEDEP(wk);
4469 for (i = 0; i < DAHASHSZ; i++) {
4470 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
4473 flush_pagedep_deps(info->vp,
4475 &pagedep->pd_diraddhd[i]))) {
4486 * This case should never happen if the vnode has
4487 * been properly sync'ed. However, if this function
4488 * is used at a place where the vnode has not yet
4489 * been sync'ed, this dependency can show up. So,
4490 * rather than panic, just flush it.
4492 nbp = WK_MKDIR(wk)->md_buf;
4493 if (getdirtybuf(&nbp, info->waitfor) == 0)
4496 if (info->waitfor == MNT_NOWAIT) {
4498 } else if ((error = bwrite(nbp)) != 0) {
4508 * This case should never happen if the vnode has
4509 * been properly sync'ed. However, if this function
4510 * is used at a place where the vnode has not yet
4511 * been sync'ed, this dependency can show up. So,
4512 * rather than panic, just flush it.
4514 * nbp can wind up == bp if a device node for the
4515 * same filesystem is being fsynced at the same time,
4516 * leading to a panic if we don't catch the case.
4518 nbp = WK_BMSAFEMAP(wk)->sm_buf;
4521 if (getdirtybuf(&nbp, info->waitfor) == 0)
4524 if (info->waitfor == MNT_NOWAIT) {
4526 } else if ((error = bwrite(nbp)) != 0) {
4536 panic("softdep_sync_metadata: Unknown type %s",
4537 TYPENAME(wk->wk_type));
4548 * Flush the dependencies associated with an inodedep.
4549 * Called with splbio blocked.
4552 flush_inodedep_deps(struct fs *fs, ino_t ino)
4554 struct inodedep *inodedep;
4555 struct allocdirect *adp;
4560 * This work is done in two passes. The first pass grabs most
4561 * of the buffers and begins asynchronously writing them. The
4562 * only way to wait for these asynchronous writes is to sleep
4563 * on the filesystem vnode which may stay busy for a long time
4564 * if the filesystem is active. So, instead, we make a second
4565 * pass over the dependencies blocking on each write. In the
4566 * usual case we will be blocking against a write that we
4567 * initiated, so when it is done the dependency will have been
4568 * resolved. Thus the second pass is expected to end quickly.
4569 * We give a brief window at the top of the loop to allow
4570 * any pending I/O to complete.
4572 for (waitfor = MNT_NOWAIT; ; ) {
4575 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4577 TAILQ_FOREACH(adp, &inodedep->id_inoupdt, ad_next) {
4578 if (adp->ad_state & DEPCOMPLETE)
4581 if (getdirtybuf(&bp, waitfor) == 0) {
4582 if (waitfor == MNT_NOWAIT)
4587 if (waitfor == MNT_NOWAIT) {
4589 } else if ((error = bwrite(bp)) != 0) {
4598 TAILQ_FOREACH(adp, &inodedep->id_newinoupdt, ad_next) {
4599 if (adp->ad_state & DEPCOMPLETE)
4602 if (getdirtybuf(&bp, waitfor) == 0) {
4603 if (waitfor == MNT_NOWAIT)
4608 if (waitfor == MNT_NOWAIT) {
4610 } else if ((error = bwrite(bp)) != 0) {
4620 * If pass2, we are done, otherwise do pass 2.
4622 if (waitfor == MNT_WAIT)
4627 * Try freeing inodedep in case all dependencies have been removed.
4629 if (inodedep_lookup(fs, ino, 0, &inodedep) != 0)
4630 (void) free_inodedep(inodedep);
4635 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
4636 * Called with splbio blocked.
4639 flush_pagedep_deps(struct vnode *pvp, struct mount *mp,
4640 struct diraddhd *diraddhdp)
4642 struct inodedep *inodedep;
4643 struct ufsmount *ump;
4646 int gotit, error = 0;
4651 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
4653 * Flush ourselves if this directory entry
4654 * has a MKDIR_PARENT dependency.
4656 if (dap->da_state & MKDIR_PARENT) {
4658 if ((error = ffs_update(pvp, 1)) != 0)
4662 * If that cleared dependencies, go on to next.
4664 if (dap != LIST_FIRST(diraddhdp))
4666 if (dap->da_state & MKDIR_PARENT) {
4668 panic("flush_pagedep_deps: MKDIR_PARENT");
4672 * A newly allocated directory must have its "." and
4673 * ".." entries written out before its name can be
4674 * committed in its parent. We do not want or need
4675 * the full semantics of a synchronous VOP_FSYNC as
4676 * that may end up here again, once for each directory
4677 * level in the filesystem. Instead, we push the blocks
4678 * and wait for them to clear. We have to fsync twice
4679 * because the first call may choose to defer blocks
4680 * that still have dependencies, but deferral will
4681 * happen at most once.
4683 inum = dap->da_newinum;
4684 if (dap->da_state & MKDIR_BODY) {
4686 if ((error = VFS_VGET(mp, inum, &vp)) != 0)
4688 if ((error=VOP_FSYNC(vp, MNT_NOWAIT)) ||
4689 (error=VOP_FSYNC(vp, MNT_NOWAIT))) {
4693 drain_output(vp, 0);
4697 * If that cleared dependencies, go on to next.
4699 if (dap != LIST_FIRST(diraddhdp))
4701 if (dap->da_state & MKDIR_BODY) {
4703 panic("flush_pagedep_deps: MKDIR_BODY");
4707 * Flush the inode on which the directory entry depends.
4708 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
4709 * the only remaining dependency is that the updated inode
4710 * count must get pushed to disk. The inode has already
4711 * been pushed into its inode buffer (via VOP_UPDATE) at
4712 * the time of the reference count change. So we need only
4713 * locate that buffer, ensure that there will be no rollback
4714 * caused by a bitmap dependency, then write the inode buffer.
4716 if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0) {
4718 panic("flush_pagedep_deps: lost inode");
4721 * If the inode still has bitmap dependencies,
4722 * push them to disk.
4724 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4725 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
4727 if (gotit && (error = bwrite(inodedep->id_buf)) != 0)
4730 if (dap != LIST_FIRST(diraddhdp))
4734 * If the inode is still sitting in a buffer waiting
4735 * to be written, push it to disk.
4738 if ((error = bread(ump->um_devvp,
4739 fsbtodoff(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
4740 (int)ump->um_fs->fs_bsize, &bp)) != 0)
4742 if ((error = bwrite(bp)) != 0)
4746 * If we have failed to get rid of all the dependencies
4747 * then something is seriously wrong.
4749 if (dap == LIST_FIRST(diraddhdp)) {
4751 panic("flush_pagedep_deps: flush failed");
4760 * A large burst of file addition or deletion activity can drive the
4761 * memory load excessively high. First attempt to slow things down
4762 * using the techniques below. If that fails, this routine requests
4763 * the offending operations to fall back to running synchronously
4764 * until the memory load returns to a reasonable level.
4767 softdep_slowdown(struct vnode *vp)
4769 int max_softdeps_hard;
4771 max_softdeps_hard = max_softdeps * 11 / 10;
4772 if (num_dirrem < max_softdeps_hard / 2 &&
4773 num_inodedep < max_softdeps_hard)
4775 stat_sync_limit_hit += 1;
4780 * If memory utilization has gotten too high, deliberately slow things
4781 * down and speed up the I/O processing.
4784 request_cleanup(int resource, int islocked)
4786 struct thread *td = curthread; /* XXX */
4789 * We never hold up the filesystem syncer process.
4791 if (td == filesys_syncer)
4794 * First check to see if the work list has gotten backlogged.
4795 * If it has, co-opt this process to help clean up two entries.
4796 * Because this process may hold inodes locked, we cannot
4797 * handle any remove requests that might block on a locked
4798 * inode as that could lead to deadlock.
4800 if (num_on_worklist > max_softdeps / 10) {
4803 process_worklist_item(NULL, LK_NOWAIT);
4804 process_worklist_item(NULL, LK_NOWAIT);
4805 stat_worklist_push += 2;
4812 * If we are resource constrained on inode dependencies, try
4813 * flushing some dirty inodes. Otherwise, we are constrained
4814 * by file deletions, so try accelerating flushes of directories
4815 * with removal dependencies. We would like to do the cleanup
4816 * here, but we probably hold an inode locked at this point and
4817 * that might deadlock against one that we try to clean. So,
4818 * the best that we can do is request the syncer daemon to do
4819 * the cleanup for us.
4824 stat_ino_limit_push += 1;
4825 req_clear_inodedeps += 1;
4826 stat_countp = &stat_ino_limit_hit;
4830 stat_blk_limit_push += 1;
4831 req_clear_remove += 1;
4832 stat_countp = &stat_blk_limit_hit;
4838 panic("request_cleanup: unknown type");
4841 * Hopefully the syncer daemon will catch up and awaken us.
4842 * We wait at most tickdelay before proceeding in any case.
4847 if (!callout_active(&handle))
4848 callout_reset(&handle, tickdelay > 2 ? tickdelay : 2,
4850 interlocked_sleep(&lk, SLEEP, (caddr_t)&proc_waiting, 0,
4859 * Awaken processes pausing in request_cleanup and clear proc_waiting
4860 * to indicate that there is no longer a timer running.
4863 pause_timer(void *arg)
4866 wakeup_one(&proc_waiting);
4867 if (proc_waiting > 0)
4868 callout_reset(&handle, tickdelay > 2 ? tickdelay : 2,
4871 callout_deactivate(&handle);
4875 * Flush out a directory with at least one removal dependency in an effort to
4876 * reduce the number of dirrem, freefile, and freeblks dependency structures.
4879 clear_remove(struct thread *td)
4881 struct pagedep_hashhead *pagedephd;
4882 struct pagedep *pagedep;
4883 static int next = 0;
4890 for (cnt = 0; cnt < pagedep_hash; cnt++) {
4891 pagedephd = &pagedep_hashtbl[next++];
4892 if (next >= pagedep_hash)
4894 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
4895 if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL)
4897 mp = pagedep->pd_mnt;
4898 ino = pagedep->pd_ino;
4900 if ((error = VFS_VGET(mp, ino, &vp)) != 0) {
4901 softdep_error("clear_remove: vget", error);
4904 if ((error = VOP_FSYNC(vp, MNT_NOWAIT)))
4905 softdep_error("clear_remove: fsync", error);
4906 drain_output(vp, 0);
4915 * Clear out a block of dirty inodes in an effort to reduce
4916 * the number of inodedep dependency structures.
4918 struct clear_inodedeps_info {
4924 clear_inodedeps_mountlist_callback(struct mount *mp, void *data)
4926 struct clear_inodedeps_info *info = data;
4928 if ((mp->mnt_flag & MNT_SOFTDEP) && info->fs == VFSTOUFS(mp)->um_fs) {
4936 clear_inodedeps(struct thread *td)
4938 struct clear_inodedeps_info info;
4939 struct inodedep_hashhead *inodedephd;
4940 struct inodedep *inodedep;
4941 static int next = 0;
4945 ino_t firstino, lastino, ino;
4949 * Pick a random inode dependency to be cleared.
4950 * We will then gather up all the inodes in its block
4951 * that have dependencies and flush them out.
4953 for (cnt = 0; cnt < inodedep_hash; cnt++) {
4954 inodedephd = &inodedep_hashtbl[next++];
4955 if (next >= inodedep_hash)
4957 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
4960 if (inodedep == NULL) {
4965 * Ugly code to find mount point given pointer to superblock.
4967 fs = inodedep->id_fs;
4970 mountlist_scan(clear_inodedeps_mountlist_callback,
4971 &info, MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
4973 * Find the last inode in the block with dependencies.
4975 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
4976 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
4977 if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
4980 * Asynchronously push all but the last inode with dependencies.
4981 * Synchronously push the last inode with dependencies to ensure
4982 * that the inode block gets written to free up the inodedeps.
4984 for (ino = firstino; ino <= lastino; ino++) {
4985 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4988 if ((error = VFS_VGET(info.mp, ino, &vp)) != 0) {
4989 softdep_error("clear_inodedeps: vget", error);
4992 if (ino == lastino) {
4993 if ((error = VOP_FSYNC(vp, MNT_WAIT)))
4994 softdep_error("clear_inodedeps: fsync1", error);
4996 if ((error = VOP_FSYNC(vp, MNT_NOWAIT)))
4997 softdep_error("clear_inodedeps: fsync2", error);
4998 drain_output(vp, 0);
5007 * Function to determine if the buffer has outstanding dependencies
5008 * that will cause a roll-back if the buffer is written. If wantcount
5009 * is set, return number of dependencies, otherwise just yes or no.
5012 softdep_count_dependencies(struct buf *bp, int wantcount)
5014 struct worklist *wk;
5015 struct inodedep *inodedep;
5016 struct indirdep *indirdep;
5017 struct allocindir *aip;
5018 struct pagedep *pagedep;
5024 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5025 switch (wk->wk_type) {
5028 inodedep = WK_INODEDEP(wk);
5029 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5030 /* bitmap allocation dependency */
5035 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
5036 /* direct block pointer dependency */
5044 indirdep = WK_INDIRDEP(wk);
5046 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
5047 /* indirect block pointer dependency */
5055 pagedep = WK_PAGEDEP(wk);
5056 for (i = 0; i < DAHASHSZ; i++) {
5058 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
5059 /* directory entry dependency */
5071 /* never a dependency on these blocks */
5076 panic("softdep_check_for_rollback: Unexpected type %s",
5077 TYPENAME(wk->wk_type));
5087 * Acquire exclusive access to a buffer.
5088 * Must be called with splbio blocked.
5089 * Return 1 if buffer was acquired.
5092 getdirtybuf(struct buf **bpp, int waitfor)
5098 if ((bp = *bpp) == NULL)
5100 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) == 0)
5102 if (waitfor != MNT_WAIT)
5104 error = interlocked_sleep(&lk, LOCKBUF, bp,
5105 LK_EXCLUSIVE | LK_SLEEPFAIL, 0, 0);
5106 if (error != ENOLCK) {
5108 panic("getdirtybuf: inconsistent lock");
5111 if ((bp->b_flags & B_DELWRI) == 0) {
5120 * Wait for pending output on a vnode to complete.
5121 * Must be called with vnode locked.
5124 drain_output(struct vnode *vp, int islocked)
5129 while (vp->v_track_write.bk_active) {
5130 vp->v_track_write.bk_waitflag = 1;
5131 interlocked_sleep(&lk, SLEEP, &vp->v_track_write,
5139 * Called whenever a buffer that is being invalidated or reallocated
5140 * contains dependencies. This should only happen if an I/O error has
5141 * occurred. The routine is called with the buffer locked.
5144 softdep_deallocate_dependencies(struct buf *bp)
5146 if ((bp->b_flags & B_ERROR) == 0)
5147 panic("softdep_deallocate_dependencies: dangling deps");
5148 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntfromname, bp->b_error);
5149 panic("softdep_deallocate_dependencies: unrecovered I/O error");
5153 * Function to handle asynchronous write errors in the filesystem.
5156 softdep_error(char *func, int error)
5159 /* XXX should do something better! */
5160 kprintf("%s: got error %d while accessing filesystem\n", func, error);