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.16 2004/07/18 19:43:48 drhodus 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>
70 #include "ffs_extern.h"
71 #include "ufs_extern.h"
74 * These definitions need to be adapted to the system to which
75 * this file is being ported.
78 * malloc types defined for the softdep system.
80 MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies");
81 MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies");
82 MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation");
83 MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map");
84 MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode");
85 MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies");
86 MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block");
87 MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode");
88 MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode");
89 MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated");
90 MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry");
91 MALLOC_DEFINE(M_MKDIR, "mkdir","New directory");
92 MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted");
94 #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE)
100 #define D_ALLOCDIRECT 4
102 #define D_ALLOCINDIR 6
109 #define D_LAST D_DIRREM
112 * translate from workitem type to memory type
113 * MUST match the defines above, such that memtype[D_XXX] == M_XXX
115 static struct malloc_type *memtype[] = {
131 #define DtoM(type) (memtype[type])
134 * Names of malloc types.
136 #define TYPENAME(type) \
137 ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???")
139 * End system adaptaion definitions.
143 * Internal function prototypes.
145 static void softdep_error(char *, int);
146 static void drain_output(struct vnode *, int);
147 static int getdirtybuf(struct buf **, int);
148 static void clear_remove(struct thread *);
149 static void clear_inodedeps(struct thread *);
150 static int flush_pagedep_deps(struct vnode *, struct mount *,
152 static int flush_inodedep_deps(struct fs *, ino_t);
153 static int handle_written_filepage(struct pagedep *, struct buf *);
154 static void diradd_inode_written(struct diradd *, struct inodedep *);
155 static int handle_written_inodeblock(struct inodedep *, struct buf *);
156 static void handle_allocdirect_partdone(struct allocdirect *);
157 static void handle_allocindir_partdone(struct allocindir *);
158 static void initiate_write_filepage(struct pagedep *, struct buf *);
159 static void handle_written_mkdir(struct mkdir *, int);
160 static void initiate_write_inodeblock(struct inodedep *, struct buf *);
161 static void handle_workitem_freefile(struct freefile *);
162 static void handle_workitem_remove(struct dirrem *);
163 static struct dirrem *newdirrem(struct buf *, struct inode *,
164 struct inode *, int, struct dirrem **);
165 static void free_diradd(struct diradd *);
166 static void free_allocindir(struct allocindir *, struct inodedep *);
167 static int indir_trunc (struct inode *, ufs_daddr_t, int, ufs_lbn_t,
169 static void deallocate_dependencies(struct buf *, struct inodedep *);
170 static void free_allocdirect(struct allocdirectlst *,
171 struct allocdirect *, int);
172 static int check_inode_unwritten(struct inodedep *);
173 static int free_inodedep(struct inodedep *);
174 static void handle_workitem_freeblocks(struct freeblks *);
175 static void merge_inode_lists(struct inodedep *);
176 static void setup_allocindir_phase2(struct buf *, struct inode *,
177 struct allocindir *);
178 static struct allocindir *newallocindir(struct inode *, int, ufs_daddr_t,
180 static void handle_workitem_freefrag(struct freefrag *);
181 static struct freefrag *newfreefrag(struct inode *, ufs_daddr_t, long);
182 static void allocdirect_merge(struct allocdirectlst *,
183 struct allocdirect *, struct allocdirect *);
184 static struct bmsafemap *bmsafemap_lookup(struct buf *);
185 static int newblk_lookup(struct fs *, ufs_daddr_t, int,
187 static int inodedep_lookup(struct fs *, ino_t, int, struct inodedep **);
188 static int pagedep_lookup(struct inode *, ufs_lbn_t, int,
190 static void pause_timer(void *);
191 static int request_cleanup(int, int);
192 static int process_worklist_item(struct mount *, int);
193 static void add_to_worklist(struct worklist *);
196 * Exported softdep operations.
198 static void softdep_disk_io_initiation(struct buf *);
199 static void softdep_disk_write_complete(struct buf *);
200 static void softdep_deallocate_dependencies(struct buf *);
201 static int softdep_fsync(struct vnode *);
202 static int softdep_process_worklist(struct mount *);
203 static void softdep_move_dependencies(struct buf *, struct buf *);
204 static int softdep_count_dependencies(struct buf *bp, int);
206 static struct bio_ops softdep_bioops = {
207 softdep_disk_io_initiation, /* io_start */
208 softdep_disk_write_complete, /* io_complete */
209 softdep_deallocate_dependencies, /* io_deallocate */
210 softdep_fsync, /* io_fsync */
211 softdep_process_worklist, /* io_sync */
212 softdep_move_dependencies, /* io_movedeps */
213 softdep_count_dependencies, /* io_countdeps */
217 * Locking primitives.
219 * For a uniprocessor, all we need to do is protect against disk
220 * interrupts. For a multiprocessor, this lock would have to be
221 * a mutex. A single mutex is used throughout this file, though
222 * finer grain locking could be used if contention warranted it.
224 * For a multiprocessor, the sleep call would accept a lock and
225 * release it after the sleep processing was complete. In a uniprocessor
226 * implementation there is no such interlock, so we simple mark
227 * the places where it needs to be done with the `interlocked' form
228 * of the lock calls. Since the uniprocessor sleep already interlocks
229 * the spl, there is nothing that really needs to be done.
231 #ifndef /* NOT */ DEBUG
232 static struct lockit {
235 #define ACQUIRE_LOCK(lk) (lk)->lkt_spl = splbio()
236 #define FREE_LOCK(lk) splx((lk)->lkt_spl)
239 #define NOHOLDER ((struct thread *)-1)
240 #define SPECIAL_FLAG ((struct thread *)-2)
241 static struct lockit {
243 struct thread *lkt_held;
244 } lk = { 0, NOHOLDER };
247 static void acquire_lock(struct lockit *);
248 static void free_lock(struct lockit *);
249 void softdep_panic(char *);
251 #define ACQUIRE_LOCK(lk) acquire_lock(lk)
252 #define FREE_LOCK(lk) free_lock(lk)
260 if (lk->lkt_held != NOHOLDER) {
261 holder = lk->lkt_held;
263 if (holder == curthread)
264 panic("softdep_lock: locking against myself");
266 panic("softdep_lock: lock held by %p", holder);
268 lk->lkt_spl = splbio();
269 lk->lkt_held = curthread;
278 if (lk->lkt_held == NOHOLDER)
279 panic("softdep_unlock: lock not held");
280 lk->lkt_held = NOHOLDER;
285 * Function to release soft updates lock and panic.
292 if (lk.lkt_held != NOHOLDER)
298 static int interlocked_sleep(struct lockit *, int, void *, int,
302 * When going to sleep, we must save our SPL so that it does
303 * not get lost if some other process uses the lock while we
304 * are sleeping. We restore it after we have slept. This routine
305 * wraps the interlocking with functions that sleep. The list
306 * below enumerates the available set of operations.
313 interlocked_sleep(lk, op, ident, flags, wmesg, timo)
326 if (lk->lkt_held == NOHOLDER)
327 panic("interlocked_sleep: lock not held");
328 lk->lkt_held = NOHOLDER;
332 retval = tsleep(ident, flags, wmesg, timo);
335 retval = BUF_LOCK((struct buf *)ident, flags);
338 panic("interlocked_sleep: unknown operation");
341 if (lk->lkt_held != NOHOLDER) {
342 holder = lk->lkt_held;
344 if (holder == curthread)
345 panic("interlocked_sleep: locking against self");
347 panic("interlocked_sleep: lock held by %p", holder);
349 lk->lkt_held = curthread;
357 * Place holder for real semaphores.
366 static void sema_init(struct sema *, char *, int, int);
367 static int sema_get(struct sema *, struct lockit *);
368 static void sema_release(struct sema *);
371 sema_init(semap, name, prio, timo)
377 semap->holder = NOHOLDER;
385 sema_get(semap, interlock)
387 struct lockit *interlock;
390 if (semap->value++ > 0) {
391 if (interlock != NULL) {
392 interlocked_sleep(interlock, SLEEP, (caddr_t)semap,
393 semap->prio, semap->name, semap->timo);
394 FREE_LOCK(interlock);
396 tsleep((caddr_t)semap, semap->prio, semap->name,
401 semap->holder = curthread;
402 if (interlock != NULL)
403 FREE_LOCK(interlock);
412 if (semap->value <= 0 || semap->holder != curthread) {
413 if (lk.lkt_held != NOHOLDER)
415 panic("sema_release: not held");
417 if (--semap->value > 0) {
421 semap->holder = NOHOLDER;
425 * Worklist queue management.
426 * These routines require that the lock be held.
428 #ifndef /* NOT */ DEBUG
429 #define WORKLIST_INSERT(head, item) do { \
430 (item)->wk_state |= ONWORKLIST; \
431 LIST_INSERT_HEAD(head, item, wk_list); \
433 #define WORKLIST_REMOVE(item) do { \
434 (item)->wk_state &= ~ONWORKLIST; \
435 LIST_REMOVE(item, wk_list); \
437 #define WORKITEM_FREE(item, type) FREE(item, DtoM(type))
440 static void worklist_insert(struct workhead *, struct worklist *);
441 static void worklist_remove(struct worklist *);
442 static void workitem_free(struct worklist *, int);
444 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
445 #define WORKLIST_REMOVE(item) worklist_remove(item)
446 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)item, type)
449 worklist_insert(head, item)
450 struct workhead *head;
451 struct worklist *item;
454 if (lk.lkt_held == NOHOLDER)
455 panic("worklist_insert: lock not held");
456 if (item->wk_state & ONWORKLIST) {
458 panic("worklist_insert: already on list");
460 item->wk_state |= ONWORKLIST;
461 LIST_INSERT_HEAD(head, item, wk_list);
465 worklist_remove(item)
466 struct worklist *item;
469 if (lk.lkt_held == NOHOLDER)
470 panic("worklist_remove: lock not held");
471 if ((item->wk_state & ONWORKLIST) == 0) {
473 panic("worklist_remove: not on list");
475 item->wk_state &= ~ONWORKLIST;
476 LIST_REMOVE(item, wk_list);
480 workitem_free(item, type)
481 struct worklist *item;
485 if (item->wk_state & ONWORKLIST) {
486 if (lk.lkt_held != NOHOLDER)
488 panic("workitem_free: still on list");
490 if (item->wk_type != type) {
491 if (lk.lkt_held != NOHOLDER)
493 panic("workitem_free: type mismatch");
495 FREE(item, DtoM(type));
500 * Workitem queue management
502 static struct workhead softdep_workitem_pending;
503 static int num_on_worklist; /* number of worklist items to be processed */
504 static int softdep_worklist_busy; /* 1 => trying to do unmount */
505 static int softdep_worklist_req; /* serialized waiters */
506 static int max_softdeps; /* maximum number of structs before slowdown */
507 static int tickdelay = 2; /* number of ticks to pause during slowdown */
508 static int *stat_countp; /* statistic to count in proc_waiting timeout */
509 static int proc_waiting; /* tracks whether we have a timeout posted */
510 static struct callout_handle handle; /* handle on posted proc_waiting timeout */
511 static struct thread *filesys_syncer; /* proc of filesystem syncer process */
512 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
513 #define FLUSH_INODES 1
514 static int req_clear_remove; /* syncer process flush some freeblks */
515 #define FLUSH_REMOVE 2
519 static int stat_worklist_push; /* number of worklist cleanups */
520 static int stat_blk_limit_push; /* number of times block limit neared */
521 static int stat_ino_limit_push; /* number of times inode limit neared */
522 static int stat_blk_limit_hit; /* number of times block slowdown imposed */
523 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */
524 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
525 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
526 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */
527 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
528 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */
531 #include <sys/sysctl.h>
532 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
533 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
534 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
535 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
536 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
537 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
538 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
539 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
540 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
541 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
542 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
543 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
547 * Add an item to the end of the work queue.
548 * This routine requires that the lock be held.
549 * This is the only routine that adds items to the list.
550 * The following routine is the only one that removes items
551 * and does so in order from first to last.
557 static struct worklist *worklist_tail;
559 if (wk->wk_state & ONWORKLIST) {
560 if (lk.lkt_held != NOHOLDER)
562 panic("add_to_worklist: already on list");
564 wk->wk_state |= ONWORKLIST;
565 if (LIST_FIRST(&softdep_workitem_pending) == NULL)
566 LIST_INSERT_HEAD(&softdep_workitem_pending, wk, wk_list);
568 LIST_INSERT_AFTER(worklist_tail, wk, wk_list);
570 num_on_worklist += 1;
574 * Process that runs once per second to handle items in the background queue.
576 * Note that we ensure that everything is done in the order in which they
577 * appear in the queue. The code below depends on this property to ensure
578 * that blocks of a file are freed before the inode itself is freed. This
579 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
580 * until all the old ones have been purged from the dependency lists.
583 softdep_process_worklist(matchmnt)
584 struct mount *matchmnt;
586 thread_t td = curthread;
587 int matchcnt, loopcount;
591 * Record the process identifier of our caller so that we can give
592 * this process preferential treatment in request_cleanup below.
598 * There is no danger of having multiple processes run this
599 * code, but we have to single-thread it when softdep_flushfiles()
600 * is in operation to get an accurate count of the number of items
601 * related to its mount point that are in the list.
603 if (matchmnt == NULL) {
604 if (softdep_worklist_busy < 0)
606 softdep_worklist_busy += 1;
610 * If requested, try removing inode or removal dependencies.
612 if (req_clear_inodedeps) {
614 req_clear_inodedeps -= 1;
615 wakeup_one(&proc_waiting);
617 if (req_clear_remove) {
619 req_clear_remove -= 1;
620 wakeup_one(&proc_waiting);
623 starttime = time_second;
624 while (num_on_worklist > 0) {
625 matchcnt += process_worklist_item(matchmnt, 0);
628 * If a umount operation wants to run the worklist
631 if (softdep_worklist_req && matchmnt == NULL) {
637 * If requested, try removing inode or removal dependencies.
639 if (req_clear_inodedeps) {
641 req_clear_inodedeps -= 1;
642 wakeup_one(&proc_waiting);
644 if (req_clear_remove) {
646 req_clear_remove -= 1;
647 wakeup_one(&proc_waiting);
650 * We do not generally want to stop for buffer space, but if
651 * we are really being a buffer hog, we will stop and wait.
653 if (loopcount++ % 128 == 0)
656 * Never allow processing to run for more than one
657 * second. Otherwise the other syncer tasks may get
658 * excessively backlogged.
660 if (starttime != time_second && matchmnt == NULL) {
665 if (matchmnt == NULL) {
666 --softdep_worklist_busy;
667 if (softdep_worklist_req && softdep_worklist_busy == 0)
668 wakeup(&softdep_worklist_req);
674 * Process one item on the worklist.
677 process_worklist_item(matchmnt, flags)
678 struct mount *matchmnt;
682 struct dirrem *dirrem;
688 if (matchmnt != NULL)
689 matchfs = VFSTOUFS(matchmnt)->um_fs;
692 * Normally we just process each item on the worklist in order.
693 * However, if we are in a situation where we cannot lock any
694 * inodes, we have to skip over any dirrem requests whose
695 * vnodes are resident and locked.
697 LIST_FOREACH(wk, &softdep_workitem_pending, wk_list) {
698 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
700 dirrem = WK_DIRREM(wk);
701 vp = ufs_ihashlookup(VFSTOUFS(dirrem->dm_mnt)->um_dev,
703 if (vp == NULL || !VOP_ISLOCKED(vp, curthread))
711 num_on_worklist -= 1;
713 switch (wk->wk_type) {
716 /* removal of a directory entry */
717 if (WK_DIRREM(wk)->dm_mnt == matchmnt)
719 handle_workitem_remove(WK_DIRREM(wk));
723 /* releasing blocks and/or fragments from a file */
724 if (WK_FREEBLKS(wk)->fb_fs == matchfs)
726 handle_workitem_freeblocks(WK_FREEBLKS(wk));
730 /* releasing a fragment when replaced as a file grows */
731 if (WK_FREEFRAG(wk)->ff_fs == matchfs)
733 handle_workitem_freefrag(WK_FREEFRAG(wk));
737 /* releasing an inode when its link count drops to 0 */
738 if (WK_FREEFILE(wk)->fx_fs == matchfs)
740 handle_workitem_freefile(WK_FREEFILE(wk));
744 panic("%s_process_worklist: Unknown type %s",
745 "softdep", TYPENAME(wk->wk_type));
752 * Move dependencies from one buffer to another.
755 softdep_move_dependencies(oldbp, newbp)
759 struct worklist *wk, *wktail;
761 if (LIST_FIRST(&newbp->b_dep) != NULL)
762 panic("softdep_move_dependencies: need merge code");
765 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
766 LIST_REMOVE(wk, wk_list);
768 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
770 LIST_INSERT_AFTER(wktail, wk, wk_list);
777 * Purge the work list of all items associated with a particular mount point.
780 softdep_flushfiles(struct mount *oldmnt, int flags, struct thread *td)
786 * Await our turn to clear out the queue, then serialize access.
788 while (softdep_worklist_busy != 0) {
789 softdep_worklist_req += 1;
790 tsleep(&softdep_worklist_req, 0, "softflush", 0);
791 softdep_worklist_req -= 1;
793 softdep_worklist_busy = -1;
795 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0) {
796 softdep_worklist_busy = 0;
797 if (softdep_worklist_req)
798 wakeup(&softdep_worklist_req);
802 * Alternately flush the block device associated with the mount
803 * point and process any dependencies that the flushing
804 * creates. In theory, this loop can happen at most twice,
805 * but we give it a few extra just to be sure.
807 devvp = VFSTOUFS(oldmnt)->um_devvp;
808 for (loopcnt = 10; loopcnt > 0; ) {
809 if (softdep_process_worklist(oldmnt) == 0) {
812 * Do another flush in case any vnodes were brought in
813 * as part of the cleanup operations.
815 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
818 * If we still found nothing to do, we are really done.
820 if (softdep_process_worklist(oldmnt) == 0)
823 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
824 error = VOP_FSYNC(devvp, MNT_WAIT, td);
825 VOP_UNLOCK(devvp, NULL, 0, td);
829 softdep_worklist_busy = 0;
830 if (softdep_worklist_req)
831 wakeup(&softdep_worklist_req);
834 * If we are unmounting then it is an error to fail. If we
835 * are simply trying to downgrade to read-only, then filesystem
836 * activity can keep us busy forever, so we just fail with EBUSY.
839 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
840 panic("softdep_flushfiles: looping");
849 * There are three types of structures that can be looked up:
850 * 1) pagedep structures identified by mount point, inode number,
852 * 2) inodedep structures identified by mount point and inode number.
853 * 3) newblk structures identified by mount point and
854 * physical block number.
856 * The "pagedep" and "inodedep" dependency structures are hashed
857 * separately from the file blocks and inodes to which they correspond.
858 * This separation helps when the in-memory copy of an inode or
859 * file block must be replaced. It also obviates the need to access
860 * an inode or file page when simply updating (or de-allocating)
861 * dependency structures. Lookup of newblk structures is needed to
862 * find newly allocated blocks when trying to associate them with
863 * their allocdirect or allocindir structure.
865 * The lookup routines optionally create and hash a new instance when
866 * an existing entry is not found.
868 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */
869 #define NODELAY 0x0002 /* cannot do background work */
872 * Structures and routines associated with pagedep caching.
874 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
875 u_long pagedep_hash; /* size of hash table - 1 */
876 #define PAGEDEP_HASH(mp, inum, lbn) \
877 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
879 static struct sema pagedep_in_progress;
882 * Look up a pagedep. Return 1 if found, 0 if not found.
883 * If not found, allocate if DEPALLOC flag is passed.
884 * Found or allocated entry is returned in pagedeppp.
885 * This routine must be called with splbio interrupts blocked.
888 pagedep_lookup(ip, lbn, flags, pagedeppp)
892 struct pagedep **pagedeppp;
894 struct pagedep *pagedep;
895 struct pagedep_hashhead *pagedephd;
900 if (lk.lkt_held == NOHOLDER)
901 panic("pagedep_lookup: lock not held");
903 mp = ITOV(ip)->v_mount;
904 pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
906 LIST_FOREACH(pagedep, pagedephd, pd_hash)
907 if (ip->i_number == pagedep->pd_ino &&
908 lbn == pagedep->pd_lbn &&
909 mp == pagedep->pd_mnt)
912 *pagedeppp = pagedep;
915 if ((flags & DEPALLOC) == 0) {
919 if (sema_get(&pagedep_in_progress, &lk) == 0) {
923 MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep), M_PAGEDEP,
925 bzero(pagedep, sizeof(struct pagedep));
926 pagedep->pd_list.wk_type = D_PAGEDEP;
927 pagedep->pd_mnt = mp;
928 pagedep->pd_ino = ip->i_number;
929 pagedep->pd_lbn = lbn;
930 LIST_INIT(&pagedep->pd_dirremhd);
931 LIST_INIT(&pagedep->pd_pendinghd);
932 for (i = 0; i < DAHASHSZ; i++)
933 LIST_INIT(&pagedep->pd_diraddhd[i]);
935 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
936 sema_release(&pagedep_in_progress);
937 *pagedeppp = pagedep;
942 * Structures and routines associated with inodedep caching.
944 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
945 static u_long inodedep_hash; /* size of hash table - 1 */
946 static long num_inodedep; /* number of inodedep allocated */
947 #define INODEDEP_HASH(fs, inum) \
948 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
949 static struct sema inodedep_in_progress;
952 * Look up a inodedep. Return 1 if found, 0 if not found.
953 * If not found, allocate if DEPALLOC flag is passed.
954 * Found or allocated entry is returned in inodedeppp.
955 * This routine must be called with splbio interrupts blocked.
958 inodedep_lookup(fs, inum, flags, inodedeppp)
962 struct inodedep **inodedeppp;
964 struct inodedep *inodedep;
965 struct inodedep_hashhead *inodedephd;
969 if (lk.lkt_held == NOHOLDER)
970 panic("inodedep_lookup: lock not held");
973 inodedephd = INODEDEP_HASH(fs, inum);
975 LIST_FOREACH(inodedep, inodedephd, id_hash)
976 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
979 *inodedeppp = inodedep;
982 if ((flags & DEPALLOC) == 0) {
987 * If we are over our limit, try to improve the situation.
989 if (num_inodedep > max_softdeps && firsttry &&
990 speedup_syncer() == 0 && (flags & NODELAY) == 0 &&
991 request_cleanup(FLUSH_INODES, 1)) {
995 if (sema_get(&inodedep_in_progress, &lk) == 0) {
1000 MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep),
1001 M_INODEDEP, M_SOFTDEP_FLAGS);
1002 inodedep->id_list.wk_type = D_INODEDEP;
1003 inodedep->id_fs = fs;
1004 inodedep->id_ino = inum;
1005 inodedep->id_state = ALLCOMPLETE;
1006 inodedep->id_nlinkdelta = 0;
1007 inodedep->id_savedino = NULL;
1008 inodedep->id_savedsize = -1;
1009 inodedep->id_buf = NULL;
1010 LIST_INIT(&inodedep->id_pendinghd);
1011 LIST_INIT(&inodedep->id_inowait);
1012 LIST_INIT(&inodedep->id_bufwait);
1013 TAILQ_INIT(&inodedep->id_inoupdt);
1014 TAILQ_INIT(&inodedep->id_newinoupdt);
1016 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1017 sema_release(&inodedep_in_progress);
1018 *inodedeppp = inodedep;
1023 * Structures and routines associated with newblk caching.
1025 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1026 u_long newblk_hash; /* size of hash table - 1 */
1027 #define NEWBLK_HASH(fs, inum) \
1028 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1029 static struct sema newblk_in_progress;
1032 * Look up a newblk. Return 1 if found, 0 if not found.
1033 * If not found, allocate if DEPALLOC flag is passed.
1034 * Found or allocated entry is returned in newblkpp.
1037 newblk_lookup(fs, newblkno, flags, newblkpp)
1039 ufs_daddr_t newblkno;
1041 struct newblk **newblkpp;
1043 struct newblk *newblk;
1044 struct newblk_hashhead *newblkhd;
1046 newblkhd = NEWBLK_HASH(fs, newblkno);
1048 LIST_FOREACH(newblk, newblkhd, nb_hash)
1049 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1055 if ((flags & DEPALLOC) == 0) {
1059 if (sema_get(&newblk_in_progress, 0) == 0)
1061 MALLOC(newblk, struct newblk *, sizeof(struct newblk),
1062 M_NEWBLK, M_SOFTDEP_FLAGS);
1063 newblk->nb_state = 0;
1065 newblk->nb_newblkno = newblkno;
1066 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1067 sema_release(&newblk_in_progress);
1073 * Executed during filesystem system initialization before
1074 * mounting any filesystems.
1077 softdep_initialize()
1079 bioops = softdep_bioops; /* XXX hack */
1081 LIST_INIT(&mkdirlisthd);
1082 LIST_INIT(&softdep_workitem_pending);
1083 max_softdeps = min(desiredvnodes * 8,
1084 M_INODEDEP->ks_limit / (2 * sizeof(struct inodedep)));
1085 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1087 sema_init(&pagedep_in_progress, "pagedep", 0, 0);
1088 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1089 sema_init(&inodedep_in_progress, "inodedep", 0, 0);
1090 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1091 sema_init(&newblk_in_progress, "newblk", 0, 0);
1095 * Called at mount time to notify the dependency code that a
1096 * filesystem wishes to use it.
1099 softdep_mount(devvp, mp, fs)
1100 struct vnode *devvp;
1104 struct csum cstotal;
1109 mp->mnt_flag &= ~MNT_ASYNC;
1110 mp->mnt_flag |= MNT_SOFTDEP;
1112 * When doing soft updates, the counters in the
1113 * superblock may have gotten out of sync, so we have
1114 * to scan the cylinder groups and recalculate them.
1116 if (fs->fs_clean != 0)
1118 bzero(&cstotal, sizeof cstotal);
1119 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1120 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1121 fs->fs_cgsize, &bp)) != 0) {
1125 cgp = (struct cg *)bp->b_data;
1126 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1127 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1128 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1129 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1130 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1134 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1135 printf("ffs_mountfs: superblock updated for soft updates\n");
1137 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1142 * Protecting the freemaps (or bitmaps).
1144 * To eliminate the need to execute fsck before mounting a filesystem
1145 * after a power failure, one must (conservatively) guarantee that the
1146 * on-disk copy of the bitmaps never indicate that a live inode or block is
1147 * free. So, when a block or inode is allocated, the bitmap should be
1148 * updated (on disk) before any new pointers. When a block or inode is
1149 * freed, the bitmap should not be updated until all pointers have been
1150 * reset. The latter dependency is handled by the delayed de-allocation
1151 * approach described below for block and inode de-allocation. The former
1152 * dependency is handled by calling the following procedure when a block or
1153 * inode is allocated. When an inode is allocated an "inodedep" is created
1154 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1155 * Each "inodedep" is also inserted into the hash indexing structure so
1156 * that any additional link additions can be made dependent on the inode
1159 * The ufs filesystem maintains a number of free block counts (e.g., per
1160 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1161 * in addition to the bitmaps. These counts are used to improve efficiency
1162 * during allocation and therefore must be consistent with the bitmaps.
1163 * There is no convenient way to guarantee post-crash consistency of these
1164 * counts with simple update ordering, for two main reasons: (1) The counts
1165 * and bitmaps for a single cylinder group block are not in the same disk
1166 * sector. If a disk write is interrupted (e.g., by power failure), one may
1167 * be written and the other not. (2) Some of the counts are located in the
1168 * superblock rather than the cylinder group block. So, we focus our soft
1169 * updates implementation on protecting the bitmaps. When mounting a
1170 * filesystem, we recompute the auxiliary counts from the bitmaps.
1174 * Called just after updating the cylinder group block to allocate an inode.
1177 softdep_setup_inomapdep(bp, ip, newinum)
1178 struct buf *bp; /* buffer for cylgroup block with inode map */
1179 struct inode *ip; /* inode related to allocation */
1180 ino_t newinum; /* new inode number being allocated */
1182 struct inodedep *inodedep;
1183 struct bmsafemap *bmsafemap;
1186 * Create a dependency for the newly allocated inode.
1187 * Panic if it already exists as something is seriously wrong.
1188 * Otherwise add it to the dependency list for the buffer holding
1189 * the cylinder group map from which it was allocated.
1192 if ((inodedep_lookup(ip->i_fs, newinum, DEPALLOC|NODELAY, &inodedep))) {
1194 panic("softdep_setup_inomapdep: found inode");
1196 inodedep->id_buf = bp;
1197 inodedep->id_state &= ~DEPCOMPLETE;
1198 bmsafemap = bmsafemap_lookup(bp);
1199 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1204 * Called just after updating the cylinder group block to
1205 * allocate block or fragment.
1208 softdep_setup_blkmapdep(bp, fs, newblkno)
1209 struct buf *bp; /* buffer for cylgroup block with block map */
1210 struct fs *fs; /* filesystem doing allocation */
1211 ufs_daddr_t newblkno; /* number of newly allocated block */
1213 struct newblk *newblk;
1214 struct bmsafemap *bmsafemap;
1217 * Create a dependency for the newly allocated block.
1218 * Add it to the dependency list for the buffer holding
1219 * the cylinder group map from which it was allocated.
1221 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1222 panic("softdep_setup_blkmapdep: found block");
1224 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp);
1225 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1230 * Find the bmsafemap associated with a cylinder group buffer.
1231 * If none exists, create one. The buffer must be locked when
1232 * this routine is called and this routine must be called with
1233 * splbio interrupts blocked.
1235 static struct bmsafemap *
1236 bmsafemap_lookup(bp)
1239 struct bmsafemap *bmsafemap;
1240 struct worklist *wk;
1243 if (lk.lkt_held == NOHOLDER)
1244 panic("bmsafemap_lookup: lock not held");
1246 LIST_FOREACH(wk, &bp->b_dep, wk_list)
1247 if (wk->wk_type == D_BMSAFEMAP)
1248 return (WK_BMSAFEMAP(wk));
1250 MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1251 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1252 bmsafemap->sm_list.wk_type = D_BMSAFEMAP;
1253 bmsafemap->sm_list.wk_state = 0;
1254 bmsafemap->sm_buf = bp;
1255 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1256 LIST_INIT(&bmsafemap->sm_allocindirhd);
1257 LIST_INIT(&bmsafemap->sm_inodedephd);
1258 LIST_INIT(&bmsafemap->sm_newblkhd);
1260 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1265 * Direct block allocation dependencies.
1267 * When a new block is allocated, the corresponding disk locations must be
1268 * initialized (with zeros or new data) before the on-disk inode points to
1269 * them. Also, the freemap from which the block was allocated must be
1270 * updated (on disk) before the inode's pointer. These two dependencies are
1271 * independent of each other and are needed for all file blocks and indirect
1272 * blocks that are pointed to directly by the inode. Just before the
1273 * "in-core" version of the inode is updated with a newly allocated block
1274 * number, a procedure (below) is called to setup allocation dependency
1275 * structures. These structures are removed when the corresponding
1276 * dependencies are satisfied or when the block allocation becomes obsolete
1277 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1278 * fragment that gets upgraded). All of these cases are handled in
1279 * procedures described later.
1281 * When a file extension causes a fragment to be upgraded, either to a larger
1282 * fragment or to a full block, the on-disk location may change (if the
1283 * previous fragment could not simply be extended). In this case, the old
1284 * fragment must be de-allocated, but not until after the inode's pointer has
1285 * been updated. In most cases, this is handled by later procedures, which
1286 * will construct a "freefrag" structure to be added to the workitem queue
1287 * when the inode update is complete (or obsolete). The main exception to
1288 * this is when an allocation occurs while a pending allocation dependency
1289 * (for the same block pointer) remains. This case is handled in the main
1290 * allocation dependency setup procedure by immediately freeing the
1291 * unreferenced fragments.
1294 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1295 struct inode *ip; /* inode to which block is being added */
1296 ufs_lbn_t lbn; /* block pointer within inode */
1297 ufs_daddr_t newblkno; /* disk block number being added */
1298 ufs_daddr_t oldblkno; /* previous block number, 0 unless frag */
1299 long newsize; /* size of new block */
1300 long oldsize; /* size of new block */
1301 struct buf *bp; /* bp for allocated block */
1303 struct allocdirect *adp, *oldadp;
1304 struct allocdirectlst *adphead;
1305 struct bmsafemap *bmsafemap;
1306 struct inodedep *inodedep;
1307 struct pagedep *pagedep;
1308 struct newblk *newblk;
1310 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1311 M_ALLOCDIRECT, M_SOFTDEP_FLAGS);
1312 bzero(adp, sizeof(struct allocdirect));
1313 adp->ad_list.wk_type = D_ALLOCDIRECT;
1315 adp->ad_newblkno = newblkno;
1316 adp->ad_oldblkno = oldblkno;
1317 adp->ad_newsize = newsize;
1318 adp->ad_oldsize = oldsize;
1319 adp->ad_state = ATTACHED;
1320 if (newblkno == oldblkno)
1321 adp->ad_freefrag = NULL;
1323 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1325 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1326 panic("softdep_setup_allocdirect: lost block");
1329 inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1330 adp->ad_inodedep = inodedep;
1332 if (newblk->nb_state == DEPCOMPLETE) {
1333 adp->ad_state |= DEPCOMPLETE;
1336 bmsafemap = newblk->nb_bmsafemap;
1337 adp->ad_buf = bmsafemap->sm_buf;
1338 LIST_REMOVE(newblk, nb_deps);
1339 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1341 LIST_REMOVE(newblk, nb_hash);
1342 FREE(newblk, M_NEWBLK);
1344 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1345 if (lbn >= NDADDR) {
1346 /* allocating an indirect block */
1347 if (oldblkno != 0) {
1349 panic("softdep_setup_allocdirect: non-zero indir");
1353 * Allocating a direct block.
1355 * If we are allocating a directory block, then we must
1356 * allocate an associated pagedep to track additions and
1359 if ((ip->i_mode & IFMT) == IFDIR &&
1360 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1361 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1364 * The list of allocdirects must be kept in sorted and ascending
1365 * order so that the rollback routines can quickly determine the
1366 * first uncommitted block (the size of the file stored on disk
1367 * ends at the end of the lowest committed fragment, or if there
1368 * are no fragments, at the end of the highest committed block).
1369 * Since files generally grow, the typical case is that the new
1370 * block is to be added at the end of the list. We speed this
1371 * special case by checking against the last allocdirect in the
1372 * list before laboriously traversing the list looking for the
1375 adphead = &inodedep->id_newinoupdt;
1376 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1377 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1378 /* insert at end of list */
1379 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1380 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1381 allocdirect_merge(adphead, adp, oldadp);
1385 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1386 if (oldadp->ad_lbn >= lbn)
1389 if (oldadp == NULL) {
1391 panic("softdep_setup_allocdirect: lost entry");
1393 /* insert in middle of list */
1394 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1395 if (oldadp->ad_lbn == lbn)
1396 allocdirect_merge(adphead, adp, oldadp);
1401 * Replace an old allocdirect dependency with a newer one.
1402 * This routine must be called with splbio interrupts blocked.
1405 allocdirect_merge(adphead, newadp, oldadp)
1406 struct allocdirectlst *adphead; /* head of list holding allocdirects */
1407 struct allocdirect *newadp; /* allocdirect being added */
1408 struct allocdirect *oldadp; /* existing allocdirect being checked */
1410 struct freefrag *freefrag;
1413 if (lk.lkt_held == NOHOLDER)
1414 panic("allocdirect_merge: lock not held");
1416 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1417 newadp->ad_oldsize != oldadp->ad_newsize ||
1418 newadp->ad_lbn >= NDADDR) {
1420 panic("allocdirect_check: old %d != new %d || lbn %ld >= %d",
1421 newadp->ad_oldblkno, oldadp->ad_newblkno, newadp->ad_lbn,
1424 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1425 newadp->ad_oldsize = oldadp->ad_oldsize;
1427 * If the old dependency had a fragment to free or had never
1428 * previously had a block allocated, then the new dependency
1429 * can immediately post its freefrag and adopt the old freefrag.
1430 * This action is done by swapping the freefrag dependencies.
1431 * The new dependency gains the old one's freefrag, and the
1432 * old one gets the new one and then immediately puts it on
1433 * the worklist when it is freed by free_allocdirect. It is
1434 * not possible to do this swap when the old dependency had a
1435 * non-zero size but no previous fragment to free. This condition
1436 * arises when the new block is an extension of the old block.
1437 * Here, the first part of the fragment allocated to the new
1438 * dependency is part of the block currently claimed on disk by
1439 * the old dependency, so cannot legitimately be freed until the
1440 * conditions for the new dependency are fulfilled.
1442 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1443 freefrag = newadp->ad_freefrag;
1444 newadp->ad_freefrag = oldadp->ad_freefrag;
1445 oldadp->ad_freefrag = freefrag;
1447 free_allocdirect(adphead, oldadp, 0);
1451 * Allocate a new freefrag structure if needed.
1453 static struct freefrag *
1454 newfreefrag(ip, blkno, size)
1459 struct freefrag *freefrag;
1465 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1466 panic("newfreefrag: frag size");
1467 MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1468 M_FREEFRAG, M_SOFTDEP_FLAGS);
1469 freefrag->ff_list.wk_type = D_FREEFRAG;
1470 freefrag->ff_state = ip->i_uid & ~ONWORKLIST; /* XXX - used below */
1471 freefrag->ff_inum = ip->i_number;
1472 freefrag->ff_fs = fs;
1473 freefrag->ff_devvp = ip->i_devvp;
1474 freefrag->ff_blkno = blkno;
1475 freefrag->ff_fragsize = size;
1480 * This workitem de-allocates fragments that were replaced during
1481 * file block allocation.
1484 handle_workitem_freefrag(freefrag)
1485 struct freefrag *freefrag;
1489 tip.i_fs = freefrag->ff_fs;
1490 tip.i_devvp = freefrag->ff_devvp;
1491 tip.i_dev = freefrag->ff_devvp->v_rdev;
1492 tip.i_number = freefrag->ff_inum;
1493 tip.i_uid = freefrag->ff_state & ~ONWORKLIST; /* XXX - set above */
1494 ffs_blkfree(&tip, freefrag->ff_blkno, freefrag->ff_fragsize);
1495 FREE(freefrag, M_FREEFRAG);
1499 * Indirect block allocation dependencies.
1501 * The same dependencies that exist for a direct block also exist when
1502 * a new block is allocated and pointed to by an entry in a block of
1503 * indirect pointers. The undo/redo states described above are also
1504 * used here. Because an indirect block contains many pointers that
1505 * may have dependencies, a second copy of the entire in-memory indirect
1506 * block is kept. The buffer cache copy is always completely up-to-date.
1507 * The second copy, which is used only as a source for disk writes,
1508 * contains only the safe pointers (i.e., those that have no remaining
1509 * update dependencies). The second copy is freed when all pointers
1510 * are safe. The cache is not allowed to replace indirect blocks with
1511 * pending update dependencies. If a buffer containing an indirect
1512 * block with dependencies is written, these routines will mark it
1513 * dirty again. It can only be successfully written once all the
1514 * dependencies are removed. The ffs_fsync routine in conjunction with
1515 * softdep_sync_metadata work together to get all the dependencies
1516 * removed so that a file can be successfully written to disk. Three
1517 * procedures are used when setting up indirect block pointer
1518 * dependencies. The division is necessary because of the organization
1519 * of the "balloc" routine and because of the distinction between file
1520 * pages and file metadata blocks.
1524 * Allocate a new allocindir structure.
1526 static struct allocindir *
1527 newallocindir(ip, ptrno, newblkno, oldblkno)
1528 struct inode *ip; /* inode for file being extended */
1529 int ptrno; /* offset of pointer in indirect block */
1530 ufs_daddr_t newblkno; /* disk block number being added */
1531 ufs_daddr_t oldblkno; /* previous block number, 0 if none */
1533 struct allocindir *aip;
1535 MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1536 M_ALLOCINDIR, M_SOFTDEP_FLAGS);
1537 bzero(aip, sizeof(struct allocindir));
1538 aip->ai_list.wk_type = D_ALLOCINDIR;
1539 aip->ai_state = ATTACHED;
1540 aip->ai_offset = ptrno;
1541 aip->ai_newblkno = newblkno;
1542 aip->ai_oldblkno = oldblkno;
1543 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1548 * Called just before setting an indirect block pointer
1549 * to a newly allocated file page.
1552 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
1553 struct inode *ip; /* inode for file being extended */
1554 ufs_lbn_t lbn; /* allocated block number within file */
1555 struct buf *bp; /* buffer with indirect blk referencing page */
1556 int ptrno; /* offset of pointer in indirect block */
1557 ufs_daddr_t newblkno; /* disk block number being added */
1558 ufs_daddr_t oldblkno; /* previous block number, 0 if none */
1559 struct buf *nbp; /* buffer holding allocated page */
1561 struct allocindir *aip;
1562 struct pagedep *pagedep;
1564 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
1567 * If we are allocating a directory page, then we must
1568 * allocate an associated pagedep to track additions and
1571 if ((ip->i_mode & IFMT) == IFDIR &&
1572 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1573 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
1574 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1576 setup_allocindir_phase2(bp, ip, aip);
1580 * Called just before setting an indirect block pointer to a
1581 * newly allocated indirect block.
1584 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
1585 struct buf *nbp; /* newly allocated indirect block */
1586 struct inode *ip; /* inode for file being extended */
1587 struct buf *bp; /* indirect block referencing allocated block */
1588 int ptrno; /* offset of pointer in indirect block */
1589 ufs_daddr_t newblkno; /* disk block number being added */
1591 struct allocindir *aip;
1593 aip = newallocindir(ip, ptrno, newblkno, 0);
1595 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1597 setup_allocindir_phase2(bp, ip, aip);
1601 * Called to finish the allocation of the "aip" allocated
1602 * by one of the two routines above.
1605 setup_allocindir_phase2(bp, ip, aip)
1606 struct buf *bp; /* in-memory copy of the indirect block */
1607 struct inode *ip; /* inode for file being extended */
1608 struct allocindir *aip; /* allocindir allocated by the above routines */
1610 struct worklist *wk;
1611 struct indirdep *indirdep, *newindirdep;
1612 struct bmsafemap *bmsafemap;
1613 struct allocindir *oldaip;
1614 struct freefrag *freefrag;
1615 struct newblk *newblk;
1617 if (bp->b_lblkno >= 0)
1618 panic("setup_allocindir_phase2: not indir blk");
1619 for (indirdep = NULL, newindirdep = NULL; ; ) {
1621 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
1622 if (wk->wk_type != D_INDIRDEP)
1624 indirdep = WK_INDIRDEP(wk);
1627 if (indirdep == NULL && newindirdep) {
1628 indirdep = newindirdep;
1629 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
1634 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
1636 panic("setup_allocindir: lost block");
1638 if (newblk->nb_state == DEPCOMPLETE) {
1639 aip->ai_state |= DEPCOMPLETE;
1642 bmsafemap = newblk->nb_bmsafemap;
1643 aip->ai_buf = bmsafemap->sm_buf;
1644 LIST_REMOVE(newblk, nb_deps);
1645 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
1648 LIST_REMOVE(newblk, nb_hash);
1649 FREE(newblk, M_NEWBLK);
1650 aip->ai_indirdep = indirdep;
1652 * Check to see if there is an existing dependency
1653 * for this block. If there is, merge the old
1654 * dependency into the new one.
1656 if (aip->ai_oldblkno == 0)
1660 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
1661 if (oldaip->ai_offset == aip->ai_offset)
1663 if (oldaip != NULL) {
1664 if (oldaip->ai_newblkno != aip->ai_oldblkno) {
1666 panic("setup_allocindir_phase2: blkno");
1668 aip->ai_oldblkno = oldaip->ai_oldblkno;
1669 freefrag = oldaip->ai_freefrag;
1670 oldaip->ai_freefrag = aip->ai_freefrag;
1671 aip->ai_freefrag = freefrag;
1672 free_allocindir(oldaip, NULL);
1674 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
1675 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)
1676 [aip->ai_offset] = aip->ai_oldblkno;
1680 if (indirdep->ir_savebp != NULL)
1681 brelse(newindirdep->ir_savebp);
1682 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
1686 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
1687 M_INDIRDEP, M_SOFTDEP_FLAGS);
1688 newindirdep->ir_list.wk_type = D_INDIRDEP;
1689 newindirdep->ir_state = ATTACHED;
1690 LIST_INIT(&newindirdep->ir_deplisthd);
1691 LIST_INIT(&newindirdep->ir_donehd);
1692 if (bp->b_blkno == bp->b_lblkno) {
1693 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
1696 newindirdep->ir_savebp =
1697 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0);
1698 BUF_KERNPROC(newindirdep->ir_savebp);
1699 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
1704 * Block de-allocation dependencies.
1706 * When blocks are de-allocated, the on-disk pointers must be nullified before
1707 * the blocks are made available for use by other files. (The true
1708 * requirement is that old pointers must be nullified before new on-disk
1709 * pointers are set. We chose this slightly more stringent requirement to
1710 * reduce complexity.) Our implementation handles this dependency by updating
1711 * the inode (or indirect block) appropriately but delaying the actual block
1712 * de-allocation (i.e., freemap and free space count manipulation) until
1713 * after the updated versions reach stable storage. After the disk is
1714 * updated, the blocks can be safely de-allocated whenever it is convenient.
1715 * This implementation handles only the common case of reducing a file's
1716 * length to zero. Other cases are handled by the conventional synchronous
1719 * The ffs implementation with which we worked double-checks
1720 * the state of the block pointers and file size as it reduces
1721 * a file's length. Some of this code is replicated here in our
1722 * soft updates implementation. The freeblks->fb_chkcnt field is
1723 * used to transfer a part of this information to the procedure
1724 * that eventually de-allocates the blocks.
1726 * This routine should be called from the routine that shortens
1727 * a file's length, before the inode's size or block pointers
1728 * are modified. It will save the block pointer information for
1729 * later release and zero the inode so that the calling routine
1733 softdep_setup_freeblocks(ip, length)
1734 struct inode *ip; /* The inode whose length is to be reduced */
1735 off_t length; /* The new length for the file */
1737 struct freeblks *freeblks;
1738 struct inodedep *inodedep;
1739 struct allocdirect *adp;
1743 int i, error, delay;
1747 panic("softde_setup_freeblocks: non-zero length");
1748 MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
1749 M_FREEBLKS, M_SOFTDEP_FLAGS);
1750 bzero(freeblks, sizeof(struct freeblks));
1751 freeblks->fb_list.wk_type = D_FREEBLKS;
1752 freeblks->fb_uid = ip->i_uid;
1753 freeblks->fb_previousinum = ip->i_number;
1754 freeblks->fb_devvp = ip->i_devvp;
1755 freeblks->fb_fs = fs;
1756 freeblks->fb_oldsize = ip->i_size;
1757 freeblks->fb_newsize = length;
1758 freeblks->fb_chkcnt = ip->i_blocks;
1759 for (i = 0; i < NDADDR; i++) {
1760 freeblks->fb_dblks[i] = ip->i_db[i];
1763 for (i = 0; i < NIADDR; i++) {
1764 freeblks->fb_iblks[i] = ip->i_ib[i];
1770 * Push the zero'ed inode to to its disk buffer so that we are free
1771 * to delete its dependencies below. Once the dependencies are gone
1772 * the buffer can be safely released.
1774 if ((error = bread(ip->i_devvp,
1775 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
1776 (int)fs->fs_bsize, &bp)) != 0)
1777 softdep_error("softdep_setup_freeblocks", error);
1778 *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)) =
1781 * Find and eliminate any inode dependencies.
1784 (void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep);
1785 if ((inodedep->id_state & IOSTARTED) != 0) {
1787 panic("softdep_setup_freeblocks: inode busy");
1790 * Add the freeblks structure to the list of operations that
1791 * must await the zero'ed inode being written to disk. If we
1792 * still have a bitmap dependency (delay == 0), then the inode
1793 * has never been written to disk, so we can process the
1794 * freeblks below once we have deleted the dependencies.
1796 delay = (inodedep->id_state & DEPCOMPLETE);
1798 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
1800 * Because the file length has been truncated to zero, any
1801 * pending block allocation dependency structures associated
1802 * with this inode are obsolete and can simply be de-allocated.
1803 * We must first merge the two dependency lists to get rid of
1804 * any duplicate freefrag structures, then purge the merged list.
1806 merge_inode_lists(inodedep);
1807 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
1808 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
1812 * We must wait for any I/O in progress to finish so that
1813 * all potential buffers on the dirty list will be visible.
1814 * Once they are all there, walk the list and get rid of
1819 drain_output(vp, 1);
1820 while (getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd), MNT_WAIT)) {
1821 bp = TAILQ_FIRST(&vp->v_dirtyblkhd);
1822 (void) inodedep_lookup(fs, ip->i_number, 0, &inodedep);
1823 deallocate_dependencies(bp, inodedep);
1824 bp->b_flags |= B_INVAL | B_NOCACHE;
1829 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) != 0)
1830 (void)free_inodedep(inodedep);
1833 * If the inode has never been written to disk (delay == 0),
1834 * then we can process the freeblks now that we have deleted
1838 handle_workitem_freeblocks(freeblks);
1842 * Reclaim any dependency structures from a buffer that is about to
1843 * be reallocated to a new vnode. The buffer must be locked, thus,
1844 * no I/O completion operations can occur while we are manipulating
1845 * its associated dependencies. The mutex is held so that other I/O's
1846 * associated with related dependencies do not occur.
1849 deallocate_dependencies(bp, inodedep)
1851 struct inodedep *inodedep;
1853 struct worklist *wk;
1854 struct indirdep *indirdep;
1855 struct allocindir *aip;
1856 struct pagedep *pagedep;
1857 struct dirrem *dirrem;
1861 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
1862 switch (wk->wk_type) {
1865 indirdep = WK_INDIRDEP(wk);
1867 * None of the indirect pointers will ever be visible,
1868 * so they can simply be tossed. GOINGAWAY ensures
1869 * that allocated pointers will be saved in the buffer
1870 * cache until they are freed. Note that they will
1871 * only be able to be found by their physical address
1872 * since the inode mapping the logical address will
1873 * be gone. The save buffer used for the safe copy
1874 * was allocated in setup_allocindir_phase2 using
1875 * the physical address so it could be used for this
1876 * purpose. Hence we swap the safe copy with the real
1877 * copy, allowing the safe copy to be freed and holding
1878 * on to the real copy for later use in indir_trunc.
1880 if (indirdep->ir_state & GOINGAWAY) {
1882 panic("deallocate_dependencies: already gone");
1884 indirdep->ir_state |= GOINGAWAY;
1885 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
1886 free_allocindir(aip, inodedep);
1887 if (bp->b_lblkno >= 0 ||
1888 bp->b_blkno != indirdep->ir_savebp->b_lblkno) {
1890 panic("deallocate_dependencies: not indir");
1892 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
1894 WORKLIST_REMOVE(wk);
1895 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
1899 pagedep = WK_PAGEDEP(wk);
1901 * None of the directory additions will ever be
1902 * visible, so they can simply be tossed.
1904 for (i = 0; i < DAHASHSZ; i++)
1906 LIST_FIRST(&pagedep->pd_diraddhd[i])))
1908 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
1911 * Copy any directory remove dependencies to the list
1912 * to be processed after the zero'ed inode is written.
1913 * If the inode has already been written, then they
1914 * can be dumped directly onto the work list.
1916 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
1917 LIST_REMOVE(dirrem, dm_next);
1918 dirrem->dm_dirinum = pagedep->pd_ino;
1919 if (inodedep == NULL ||
1920 (inodedep->id_state & ALLCOMPLETE) ==
1922 add_to_worklist(&dirrem->dm_list);
1924 WORKLIST_INSERT(&inodedep->id_bufwait,
1927 WORKLIST_REMOVE(&pagedep->pd_list);
1928 LIST_REMOVE(pagedep, pd_hash);
1929 WORKITEM_FREE(pagedep, D_PAGEDEP);
1933 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
1939 panic("deallocate_dependencies: Unexpected type %s",
1940 TYPENAME(wk->wk_type));
1945 panic("deallocate_dependencies: Unknown type %s",
1946 TYPENAME(wk->wk_type));
1953 * Free an allocdirect. Generate a new freefrag work request if appropriate.
1954 * This routine must be called with splbio interrupts blocked.
1957 free_allocdirect(adphead, adp, delay)
1958 struct allocdirectlst *adphead;
1959 struct allocdirect *adp;
1964 if (lk.lkt_held == NOHOLDER)
1965 panic("free_allocdirect: lock not held");
1967 if ((adp->ad_state & DEPCOMPLETE) == 0)
1968 LIST_REMOVE(adp, ad_deps);
1969 TAILQ_REMOVE(adphead, adp, ad_next);
1970 if ((adp->ad_state & COMPLETE) == 0)
1971 WORKLIST_REMOVE(&adp->ad_list);
1972 if (adp->ad_freefrag != NULL) {
1974 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
1975 &adp->ad_freefrag->ff_list);
1977 add_to_worklist(&adp->ad_freefrag->ff_list);
1979 WORKITEM_FREE(adp, D_ALLOCDIRECT);
1983 * Prepare an inode to be freed. The actual free operation is not
1984 * done until the zero'ed inode has been written to disk.
1987 softdep_freefile(pvp, ino, mode)
1992 struct inode *ip = VTOI(pvp);
1993 struct inodedep *inodedep;
1994 struct freefile *freefile;
1997 * This sets up the inode de-allocation dependency.
1999 MALLOC(freefile, struct freefile *, sizeof(struct freefile),
2000 M_FREEFILE, M_SOFTDEP_FLAGS);
2001 freefile->fx_list.wk_type = D_FREEFILE;
2002 freefile->fx_list.wk_state = 0;
2003 freefile->fx_mode = mode;
2004 freefile->fx_oldinum = ino;
2005 freefile->fx_devvp = ip->i_devvp;
2006 freefile->fx_fs = ip->i_fs;
2009 * If the inodedep does not exist, then the zero'ed inode has
2010 * been written to disk. If the allocated inode has never been
2011 * written to disk, then the on-disk inode is zero'ed. In either
2012 * case we can free the file immediately.
2015 if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0 ||
2016 check_inode_unwritten(inodedep)) {
2018 handle_workitem_freefile(freefile);
2021 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2026 * Check to see if an inode has never been written to disk. If
2027 * so free the inodedep and return success, otherwise return failure.
2028 * This routine must be called with splbio interrupts blocked.
2030 * If we still have a bitmap dependency, then the inode has never
2031 * been written to disk. Drop the dependency as it is no longer
2032 * necessary since the inode is being deallocated. We set the
2033 * ALLCOMPLETE flags since the bitmap now properly shows that the
2034 * inode is not allocated. Even if the inode is actively being
2035 * written, it has been rolled back to its zero'ed state, so we
2036 * are ensured that a zero inode is what is on the disk. For short
2037 * lived files, this change will usually result in removing all the
2038 * dependencies from the inode so that it can be freed immediately.
2041 check_inode_unwritten(inodedep)
2042 struct inodedep *inodedep;
2045 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2046 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2047 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2048 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2049 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2050 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2051 inodedep->id_nlinkdelta != 0)
2053 inodedep->id_state |= ALLCOMPLETE;
2054 LIST_REMOVE(inodedep, id_deps);
2055 inodedep->id_buf = NULL;
2056 if (inodedep->id_state & ONWORKLIST)
2057 WORKLIST_REMOVE(&inodedep->id_list);
2058 if (inodedep->id_savedino != NULL) {
2059 FREE(inodedep->id_savedino, M_INODEDEP);
2060 inodedep->id_savedino = NULL;
2062 if (free_inodedep(inodedep) == 0) {
2064 panic("check_inode_unwritten: busy inode");
2070 * Try to free an inodedep structure. Return 1 if it could be freed.
2073 free_inodedep(inodedep)
2074 struct inodedep *inodedep;
2077 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2078 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2079 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2080 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2081 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2082 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2083 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2084 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino != NULL)
2086 LIST_REMOVE(inodedep, id_hash);
2087 WORKITEM_FREE(inodedep, D_INODEDEP);
2093 * This workitem routine performs the block de-allocation.
2094 * The workitem is added to the pending list after the updated
2095 * inode block has been written to disk. As mentioned above,
2096 * checks regarding the number of blocks de-allocated (compared
2097 * to the number of blocks allocated for the file) are also
2098 * performed in this function.
2101 handle_workitem_freeblocks(freeblks)
2102 struct freeblks *freeblks;
2107 int i, level, bsize;
2108 long nblocks, blocksreleased = 0;
2109 int error, allerror = 0;
2110 ufs_lbn_t baselbns[NIADDR], tmpval;
2112 tip.i_number = freeblks->fb_previousinum;
2113 tip.i_devvp = freeblks->fb_devvp;
2114 tip.i_dev = freeblks->fb_devvp->v_rdev;
2115 tip.i_fs = freeblks->fb_fs;
2116 tip.i_size = freeblks->fb_oldsize;
2117 tip.i_uid = freeblks->fb_uid;
2118 fs = freeblks->fb_fs;
2120 baselbns[0] = NDADDR;
2121 for (i = 1; i < NIADDR; i++) {
2122 tmpval *= NINDIR(fs);
2123 baselbns[i] = baselbns[i - 1] + tmpval;
2125 nblocks = btodb(fs->fs_bsize);
2128 * Indirect blocks first.
2130 for (level = (NIADDR - 1); level >= 0; level--) {
2131 if ((bn = freeblks->fb_iblks[level]) == 0)
2133 if ((error = indir_trunc(&tip, fsbtodb(fs, bn), level,
2134 baselbns[level], &blocksreleased)) == 0)
2136 ffs_blkfree(&tip, bn, fs->fs_bsize);
2137 blocksreleased += nblocks;
2140 * All direct blocks or frags.
2142 for (i = (NDADDR - 1); i >= 0; i--) {
2143 if ((bn = freeblks->fb_dblks[i]) == 0)
2145 bsize = blksize(fs, &tip, i);
2146 ffs_blkfree(&tip, bn, bsize);
2147 blocksreleased += btodb(bsize);
2151 if (freeblks->fb_chkcnt != blocksreleased)
2152 printf("handle_workitem_freeblocks: block count\n");
2154 softdep_error("handle_workitem_freeblks", allerror);
2155 #endif /* DIAGNOSTIC */
2156 WORKITEM_FREE(freeblks, D_FREEBLKS);
2160 * Release blocks associated with the inode ip and stored in the indirect
2161 * block dbn. If level is greater than SINGLE, the block is an indirect block
2162 * and recursive calls to indirtrunc must be used to cleanse other indirect
2166 indir_trunc(ip, dbn, level, lbn, countp)
2177 struct worklist *wk;
2178 struct indirdep *indirdep;
2179 int i, lbnadd, nblocks;
2180 int error, allerror = 0;
2184 for (i = level; i > 0; i--)
2185 lbnadd *= NINDIR(fs);
2187 * Get buffer of block pointers to be freed. This routine is not
2188 * called until the zero'ed inode has been written, so it is safe
2189 * to free blocks as they are encountered. Because the inode has
2190 * been zero'ed, calls to bmap on these blocks will fail. So, we
2191 * have to use the on-disk address and the block device for the
2192 * filesystem to look them up. If the file was deleted before its
2193 * indirect blocks were all written to disk, the routine that set
2194 * us up (deallocate_dependencies) will have arranged to leave
2195 * a complete copy of the indirect block in memory for our use.
2196 * Otherwise we have to read the blocks in from the disk.
2199 if ((bp = incore(ip->i_devvp, dbn)) != NULL &&
2200 (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2201 if (wk->wk_type != D_INDIRDEP ||
2202 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2203 (indirdep->ir_state & GOINGAWAY) == 0) {
2205 panic("indir_trunc: lost indirdep");
2207 WORKLIST_REMOVE(wk);
2208 WORKITEM_FREE(indirdep, D_INDIRDEP);
2209 if (LIST_FIRST(&bp->b_dep) != NULL) {
2211 panic("indir_trunc: dangling dep");
2216 error = bread(ip->i_devvp, dbn, (int)fs->fs_bsize, &bp);
2221 * Recursively free indirect blocks.
2223 bap = (ufs_daddr_t *)bp->b_data;
2224 nblocks = btodb(fs->fs_bsize);
2225 for (i = NINDIR(fs) - 1; i >= 0; i--) {
2226 if ((nb = bap[i]) == 0)
2229 if ((error = indir_trunc(ip, fsbtodb(fs, nb),
2230 level - 1, lbn + (i * lbnadd), countp)) != 0)
2233 ffs_blkfree(ip, nb, fs->fs_bsize);
2236 bp->b_flags |= B_INVAL | B_NOCACHE;
2242 * Free an allocindir.
2243 * This routine must be called with splbio interrupts blocked.
2246 free_allocindir(aip, inodedep)
2247 struct allocindir *aip;
2248 struct inodedep *inodedep;
2250 struct freefrag *freefrag;
2253 if (lk.lkt_held == NOHOLDER)
2254 panic("free_allocindir: lock not held");
2256 if ((aip->ai_state & DEPCOMPLETE) == 0)
2257 LIST_REMOVE(aip, ai_deps);
2258 if (aip->ai_state & ONWORKLIST)
2259 WORKLIST_REMOVE(&aip->ai_list);
2260 LIST_REMOVE(aip, ai_next);
2261 if ((freefrag = aip->ai_freefrag) != NULL) {
2262 if (inodedep == NULL)
2263 add_to_worklist(&freefrag->ff_list);
2265 WORKLIST_INSERT(&inodedep->id_bufwait,
2266 &freefrag->ff_list);
2268 WORKITEM_FREE(aip, D_ALLOCINDIR);
2272 * Directory entry addition dependencies.
2274 * When adding a new directory entry, the inode (with its incremented link
2275 * count) must be written to disk before the directory entry's pointer to it.
2276 * Also, if the inode is newly allocated, the corresponding freemap must be
2277 * updated (on disk) before the directory entry's pointer. These requirements
2278 * are met via undo/redo on the directory entry's pointer, which consists
2279 * simply of the inode number.
2281 * As directory entries are added and deleted, the free space within a
2282 * directory block can become fragmented. The ufs filesystem will compact
2283 * a fragmented directory block to make space for a new entry. When this
2284 * occurs, the offsets of previously added entries change. Any "diradd"
2285 * dependency structures corresponding to these entries must be updated with
2290 * This routine is called after the in-memory inode's link
2291 * count has been incremented, but before the directory entry's
2292 * pointer to the inode has been set.
2295 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp)
2296 struct buf *bp; /* buffer containing directory block */
2297 struct inode *dp; /* inode for directory */
2298 off_t diroffset; /* offset of new entry in directory */
2299 ino_t newinum; /* inode referenced by new directory entry */
2300 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
2302 int offset; /* offset of new entry within directory block */
2303 ufs_lbn_t lbn; /* block in directory containing new entry */
2306 struct pagedep *pagedep;
2307 struct inodedep *inodedep;
2308 struct mkdir *mkdir1, *mkdir2;
2311 * Whiteouts have no dependencies.
2313 if (newinum == WINO) {
2314 if (newdirbp != NULL)
2320 lbn = lblkno(fs, diroffset);
2321 offset = blkoff(fs, diroffset);
2322 MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2324 bzero(dap, sizeof(struct diradd));
2325 dap->da_list.wk_type = D_DIRADD;
2326 dap->da_offset = offset;
2327 dap->da_newinum = newinum;
2328 dap->da_state = ATTACHED;
2329 if (newdirbp == NULL) {
2330 dap->da_state |= DEPCOMPLETE;
2333 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
2334 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2336 mkdir1->md_list.wk_type = D_MKDIR;
2337 mkdir1->md_state = MKDIR_BODY;
2338 mkdir1->md_diradd = dap;
2339 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2341 mkdir2->md_list.wk_type = D_MKDIR;
2342 mkdir2->md_state = MKDIR_PARENT;
2343 mkdir2->md_diradd = dap;
2345 * Dependency on "." and ".." being written to disk.
2347 mkdir1->md_buf = newdirbp;
2349 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
2350 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
2354 * Dependency on link count increase for parent directory
2357 if (inodedep_lookup(dp->i_fs, dp->i_number, 0, &inodedep) == 0
2358 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2359 dap->da_state &= ~MKDIR_PARENT;
2360 WORKITEM_FREE(mkdir2, D_MKDIR);
2362 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
2363 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
2367 * Link into parent directory pagedep to await its being written.
2369 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2370 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2371 dap->da_pagedep = pagedep;
2372 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
2375 * Link into its inodedep. Put it on the id_bufwait list if the inode
2376 * is not yet written. If it is written, do the post-inode write
2377 * processing to put it on the id_pendinghd list.
2379 (void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep);
2380 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
2381 diradd_inode_written(dap, inodedep);
2383 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2388 * This procedure is called to change the offset of a directory
2389 * entry when compacting a directory block which must be owned
2390 * exclusively by the caller. Note that the actual entry movement
2391 * must be done in this procedure to ensure that no I/O completions
2392 * occur while the move is in progress.
2395 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
2396 struct inode *dp; /* inode for directory */
2397 caddr_t base; /* address of dp->i_offset */
2398 caddr_t oldloc; /* address of old directory location */
2399 caddr_t newloc; /* address of new directory location */
2400 int entrysize; /* size of directory entry */
2402 int offset, oldoffset, newoffset;
2403 struct pagedep *pagedep;
2408 lbn = lblkno(dp->i_fs, dp->i_offset);
2409 offset = blkoff(dp->i_fs, dp->i_offset);
2410 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
2412 oldoffset = offset + (oldloc - base);
2413 newoffset = offset + (newloc - base);
2415 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
2416 if (dap->da_offset != oldoffset)
2418 dap->da_offset = newoffset;
2419 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
2421 LIST_REMOVE(dap, da_pdlist);
2422 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
2428 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
2429 if (dap->da_offset == oldoffset) {
2430 dap->da_offset = newoffset;
2436 bcopy(oldloc, newloc, entrysize);
2441 * Free a diradd dependency structure. This routine must be called
2442 * with splbio interrupts blocked.
2448 struct dirrem *dirrem;
2449 struct pagedep *pagedep;
2450 struct inodedep *inodedep;
2451 struct mkdir *mkdir, *nextmd;
2454 if (lk.lkt_held == NOHOLDER)
2455 panic("free_diradd: lock not held");
2457 WORKLIST_REMOVE(&dap->da_list);
2458 LIST_REMOVE(dap, da_pdlist);
2459 if ((dap->da_state & DIRCHG) == 0) {
2460 pagedep = dap->da_pagedep;
2462 dirrem = dap->da_previous;
2463 pagedep = dirrem->dm_pagedep;
2464 dirrem->dm_dirinum = pagedep->pd_ino;
2465 add_to_worklist(&dirrem->dm_list);
2467 if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum,
2469 (void) free_inodedep(inodedep);
2470 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2471 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
2472 nextmd = LIST_NEXT(mkdir, md_mkdirs);
2473 if (mkdir->md_diradd != dap)
2475 dap->da_state &= ~mkdir->md_state;
2476 WORKLIST_REMOVE(&mkdir->md_list);
2477 LIST_REMOVE(mkdir, md_mkdirs);
2478 WORKITEM_FREE(mkdir, D_MKDIR);
2480 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2482 panic("free_diradd: unfound ref");
2485 WORKITEM_FREE(dap, D_DIRADD);
2489 * Directory entry removal dependencies.
2491 * When removing a directory entry, the entry's inode pointer must be
2492 * zero'ed on disk before the corresponding inode's link count is decremented
2493 * (possibly freeing the inode for re-use). This dependency is handled by
2494 * updating the directory entry but delaying the inode count reduction until
2495 * after the directory block has been written to disk. After this point, the
2496 * inode count can be decremented whenever it is convenient.
2500 * This routine should be called immediately after removing
2501 * a directory entry. The inode's link count should not be
2502 * decremented by the calling procedure -- the soft updates
2503 * code will do this task when it is safe.
2506 softdep_setup_remove(bp, dp, ip, isrmdir)
2507 struct buf *bp; /* buffer containing directory block */
2508 struct inode *dp; /* inode for the directory being modified */
2509 struct inode *ip; /* inode for directory entry being removed */
2510 int isrmdir; /* indicates if doing RMDIR */
2512 struct dirrem *dirrem, *prevdirrem;
2515 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
2517 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2520 * If the COMPLETE flag is clear, then there were no active
2521 * entries and we want to roll back to a zeroed entry until
2522 * the new inode is committed to disk. If the COMPLETE flag is
2523 * set then we have deleted an entry that never made it to
2524 * disk. If the entry we deleted resulted from a name change,
2525 * then the old name still resides on disk. We cannot delete
2526 * its inode (returned to us in prevdirrem) until the zeroed
2527 * directory entry gets to disk. The new inode has never been
2528 * referenced on the disk, so can be deleted immediately.
2530 if ((dirrem->dm_state & COMPLETE) == 0) {
2531 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
2535 if (prevdirrem != NULL)
2536 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
2537 prevdirrem, dm_next);
2538 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
2540 handle_workitem_remove(dirrem);
2545 * Allocate a new dirrem if appropriate and return it along with
2546 * its associated pagedep. Called without a lock, returns with lock.
2548 static long num_dirrem; /* number of dirrem allocated */
2549 static struct dirrem *
2550 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
2551 struct buf *bp; /* buffer containing directory block */
2552 struct inode *dp; /* inode for the directory being modified */
2553 struct inode *ip; /* inode for directory entry being removed */
2554 int isrmdir; /* indicates if doing RMDIR */
2555 struct dirrem **prevdirremp; /* previously referenced inode, if any */
2560 struct dirrem *dirrem;
2561 struct pagedep *pagedep;
2564 * Whiteouts have no deletion dependencies.
2567 panic("newdirrem: whiteout");
2569 * If we are over our limit, try to improve the situation.
2570 * Limiting the number of dirrem structures will also limit
2571 * the number of freefile and freeblks structures.
2573 if (num_dirrem > max_softdeps / 2 && speedup_syncer() == 0)
2574 (void) request_cleanup(FLUSH_REMOVE, 0);
2576 MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
2577 M_DIRREM, M_SOFTDEP_FLAGS);
2578 bzero(dirrem, sizeof(struct dirrem));
2579 dirrem->dm_list.wk_type = D_DIRREM;
2580 dirrem->dm_state = isrmdir ? RMDIR : 0;
2581 dirrem->dm_mnt = ITOV(ip)->v_mount;
2582 dirrem->dm_oldinum = ip->i_number;
2583 *prevdirremp = NULL;
2586 lbn = lblkno(dp->i_fs, dp->i_offset);
2587 offset = blkoff(dp->i_fs, dp->i_offset);
2588 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2589 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2590 dirrem->dm_pagedep = pagedep;
2592 * Check for a diradd dependency for the same directory entry.
2593 * If present, then both dependencies become obsolete and can
2594 * be de-allocated. Check for an entry on both the pd_dirraddhd
2595 * list and the pd_pendinghd list.
2598 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
2599 if (dap->da_offset == offset)
2603 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
2604 if (dap->da_offset == offset)
2610 * Must be ATTACHED at this point.
2612 if ((dap->da_state & ATTACHED) == 0) {
2614 panic("newdirrem: not ATTACHED");
2616 if (dap->da_newinum != ip->i_number) {
2618 panic("newdirrem: inum %d should be %d",
2619 ip->i_number, dap->da_newinum);
2622 * If we are deleting a changed name that never made it to disk,
2623 * then return the dirrem describing the previous inode (which
2624 * represents the inode currently referenced from this entry on disk).
2626 if ((dap->da_state & DIRCHG) != 0) {
2627 *prevdirremp = dap->da_previous;
2628 dap->da_state &= ~DIRCHG;
2629 dap->da_pagedep = pagedep;
2632 * We are deleting an entry that never made it to disk.
2633 * Mark it COMPLETE so we can delete its inode immediately.
2635 dirrem->dm_state |= COMPLETE;
2641 * Directory entry change dependencies.
2643 * Changing an existing directory entry requires that an add operation
2644 * be completed first followed by a deletion. The semantics for the addition
2645 * are identical to the description of adding a new entry above except
2646 * that the rollback is to the old inode number rather than zero. Once
2647 * the addition dependency is completed, the removal is done as described
2648 * in the removal routine above.
2652 * This routine should be called immediately after changing
2653 * a directory entry. The inode's link count should not be
2654 * decremented by the calling procedure -- the soft updates
2655 * code will perform this task when it is safe.
2658 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
2659 struct buf *bp; /* buffer containing directory block */
2660 struct inode *dp; /* inode for the directory being modified */
2661 struct inode *ip; /* inode for directory entry being removed */
2662 ino_t newinum; /* new inode number for changed entry */
2663 int isrmdir; /* indicates if doing RMDIR */
2666 struct diradd *dap = NULL;
2667 struct dirrem *dirrem, *prevdirrem;
2668 struct pagedep *pagedep;
2669 struct inodedep *inodedep;
2671 offset = blkoff(dp->i_fs, dp->i_offset);
2674 * Whiteouts do not need diradd dependencies.
2676 if (newinum != WINO) {
2677 MALLOC(dap, struct diradd *, sizeof(struct diradd),
2678 M_DIRADD, M_SOFTDEP_FLAGS);
2679 bzero(dap, sizeof(struct diradd));
2680 dap->da_list.wk_type = D_DIRADD;
2681 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
2682 dap->da_offset = offset;
2683 dap->da_newinum = newinum;
2687 * Allocate a new dirrem and ACQUIRE_LOCK.
2689 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2690 pagedep = dirrem->dm_pagedep;
2692 * The possible values for isrmdir:
2693 * 0 - non-directory file rename
2694 * 1 - directory rename within same directory
2695 * inum - directory rename to new directory of given inode number
2696 * When renaming to a new directory, we are both deleting and
2697 * creating a new directory entry, so the link count on the new
2698 * directory should not change. Thus we do not need the followup
2699 * dirrem which is usually done in handle_workitem_remove. We set
2700 * the DIRCHG flag to tell handle_workitem_remove to skip the
2704 dirrem->dm_state |= DIRCHG;
2707 * Whiteouts have no additional dependencies,
2708 * so just put the dirrem on the correct list.
2710 if (newinum == WINO) {
2711 if ((dirrem->dm_state & COMPLETE) == 0) {
2712 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
2715 dirrem->dm_dirinum = pagedep->pd_ino;
2716 add_to_worklist(&dirrem->dm_list);
2723 * If the COMPLETE flag is clear, then there were no active
2724 * entries and we want to roll back to the previous inode until
2725 * the new inode is committed to disk. If the COMPLETE flag is
2726 * set, then we have deleted an entry that never made it to disk.
2727 * If the entry we deleted resulted from a name change, then the old
2728 * inode reference still resides on disk. Any rollback that we do
2729 * needs to be to that old inode (returned to us in prevdirrem). If
2730 * the entry we deleted resulted from a create, then there is
2731 * no entry on the disk, so we want to roll back to zero rather
2732 * than the uncommitted inode. In either of the COMPLETE cases we
2733 * want to immediately free the unwritten and unreferenced inode.
2735 if ((dirrem->dm_state & COMPLETE) == 0) {
2736 dap->da_previous = dirrem;
2738 if (prevdirrem != NULL) {
2739 dap->da_previous = prevdirrem;
2741 dap->da_state &= ~DIRCHG;
2742 dap->da_pagedep = pagedep;
2744 dirrem->dm_dirinum = pagedep->pd_ino;
2745 add_to_worklist(&dirrem->dm_list);
2748 * Link into its inodedep. Put it on the id_bufwait list if the inode
2749 * is not yet written. If it is written, do the post-inode write
2750 * processing to put it on the id_pendinghd list.
2752 if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 ||
2753 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2754 dap->da_state |= COMPLETE;
2755 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
2756 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
2758 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
2760 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2766 * Called whenever the link count on an inode is changed.
2767 * It creates an inode dependency so that the new reference(s)
2768 * to the inode cannot be committed to disk until the updated
2769 * inode has been written.
2772 softdep_change_linkcnt(ip)
2773 struct inode *ip; /* the inode with the increased link count */
2775 struct inodedep *inodedep;
2778 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep);
2779 if (ip->i_nlink < ip->i_effnlink) {
2781 panic("softdep_change_linkcnt: bad delta");
2783 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2788 * This workitem decrements the inode's link count.
2789 * If the link count reaches zero, the file is removed.
2792 handle_workitem_remove(dirrem)
2793 struct dirrem *dirrem;
2795 struct thread *td = curthread; /* XXX */
2796 struct inodedep *inodedep;
2802 if ((error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, &vp)) != 0) {
2803 softdep_error("handle_workitem_remove: vget", error);
2808 if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){
2810 panic("handle_workitem_remove: lost inodedep");
2813 * Normal file deletion.
2815 if ((dirrem->dm_state & RMDIR) == 0) {
2817 ip->i_flag |= IN_CHANGE;
2818 if (ip->i_nlink < ip->i_effnlink) {
2820 panic("handle_workitem_remove: bad file delta");
2822 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2826 WORKITEM_FREE(dirrem, D_DIRREM);
2830 * Directory deletion. Decrement reference count for both the
2831 * just deleted parent directory entry and the reference for ".".
2832 * Next truncate the directory to length zero. When the
2833 * truncation completes, arrange to have the reference count on
2834 * the parent decremented to account for the loss of "..".
2837 ip->i_flag |= IN_CHANGE;
2838 if (ip->i_nlink < ip->i_effnlink) {
2840 panic("handle_workitem_remove: bad dir delta");
2842 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2844 if ((error = UFS_TRUNCATE(vp, (off_t)0, 0, proc0.p_ucred, td)) != 0)
2845 softdep_error("handle_workitem_remove: truncate", error);
2847 * Rename a directory to a new parent. Since, we are both deleting
2848 * and creating a new directory entry, the link count on the new
2849 * directory should not change. Thus we skip the followup dirrem.
2851 if (dirrem->dm_state & DIRCHG) {
2854 WORKITEM_FREE(dirrem, D_DIRREM);
2858 * If the inodedep does not exist, then the zero'ed inode has
2859 * been written to disk. If the allocated inode has never been
2860 * written to disk, then the on-disk inode is zero'ed. In either
2861 * case we can remove the file immediately.
2864 dirrem->dm_state = 0;
2865 oldinum = dirrem->dm_oldinum;
2866 dirrem->dm_oldinum = dirrem->dm_dirinum;
2867 if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 ||
2868 check_inode_unwritten(inodedep)) {
2871 handle_workitem_remove(dirrem);
2874 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
2880 * Inode de-allocation dependencies.
2882 * When an inode's link count is reduced to zero, it can be de-allocated. We
2883 * found it convenient to postpone de-allocation until after the inode is
2884 * written to disk with its new link count (zero). At this point, all of the
2885 * on-disk inode's block pointers are nullified and, with careful dependency
2886 * list ordering, all dependencies related to the inode will be satisfied and
2887 * the corresponding dependency structures de-allocated. So, if/when the
2888 * inode is reused, there will be no mixing of old dependencies with new
2889 * ones. This artificial dependency is set up by the block de-allocation
2890 * procedure above (softdep_setup_freeblocks) and completed by the
2891 * following procedure.
2894 handle_workitem_freefile(freefile)
2895 struct freefile *freefile;
2899 struct inodedep *idp;
2904 error = inodedep_lookup(freefile->fx_fs, freefile->fx_oldinum, 0, &idp);
2907 panic("handle_workitem_freefile: inodedep survived");
2909 tip.i_devvp = freefile->fx_devvp;
2910 tip.i_dev = freefile->fx_devvp->v_rdev;
2911 tip.i_fs = freefile->fx_fs;
2913 if ((error = ffs_freefile(&vp, freefile->fx_oldinum, freefile->fx_mode)) != 0)
2914 softdep_error("handle_workitem_freefile", error);
2915 WORKITEM_FREE(freefile, D_FREEFILE);
2921 * The dependency structures constructed above are most actively used when file
2922 * system blocks are written to disk. No constraints are placed on when a
2923 * block can be written, but unsatisfied update dependencies are made safe by
2924 * modifying (or replacing) the source memory for the duration of the disk
2925 * write. When the disk write completes, the memory block is again brought
2928 * In-core inode structure reclamation.
2930 * Because there are a finite number of "in-core" inode structures, they are
2931 * reused regularly. By transferring all inode-related dependencies to the
2932 * in-memory inode block and indexing them separately (via "inodedep"s), we
2933 * can allow "in-core" inode structures to be reused at any time and avoid
2934 * any increase in contention.
2936 * Called just before entering the device driver to initiate a new disk I/O.
2937 * The buffer must be locked, thus, no I/O completion operations can occur
2938 * while we are manipulating its associated dependencies.
2941 softdep_disk_io_initiation(bp)
2942 struct buf *bp; /* structure describing disk write to occur */
2944 struct worklist *wk, *nextwk;
2945 struct indirdep *indirdep;
2948 * We only care about write operations. There should never
2949 * be dependencies for reads.
2951 if (bp->b_flags & B_READ)
2952 panic("softdep_disk_io_initiation: read");
2954 * Do any necessary pre-I/O processing.
2956 for (wk = LIST_FIRST(&bp->b_dep); wk; wk = nextwk) {
2957 nextwk = LIST_NEXT(wk, wk_list);
2958 switch (wk->wk_type) {
2961 initiate_write_filepage(WK_PAGEDEP(wk), bp);
2965 initiate_write_inodeblock(WK_INODEDEP(wk), bp);
2969 indirdep = WK_INDIRDEP(wk);
2970 if (indirdep->ir_state & GOINGAWAY)
2971 panic("disk_io_initiation: indirdep gone");
2973 * If there are no remaining dependencies, this
2974 * will be writing the real pointers, so the
2975 * dependency can be freed.
2977 if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) {
2978 indirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
2979 brelse(indirdep->ir_savebp);
2980 /* inline expand WORKLIST_REMOVE(wk); */
2981 wk->wk_state &= ~ONWORKLIST;
2982 LIST_REMOVE(wk, wk_list);
2983 WORKITEM_FREE(indirdep, D_INDIRDEP);
2987 * Replace up-to-date version with safe version.
2989 MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
2990 M_INDIRDEP, M_SOFTDEP_FLAGS);
2992 indirdep->ir_state &= ~ATTACHED;
2993 indirdep->ir_state |= UNDONE;
2994 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
2995 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3007 panic("handle_disk_io_initiation: Unexpected type %s",
3008 TYPENAME(wk->wk_type));
3015 * Called from within the procedure above to deal with unsatisfied
3016 * allocation dependencies in a directory. The buffer must be locked,
3017 * thus, no I/O completion operations can occur while we are
3018 * manipulating its associated dependencies.
3021 initiate_write_filepage(pagedep, bp)
3022 struct pagedep *pagedep;
3029 if (pagedep->pd_state & IOSTARTED) {
3031 * This can only happen if there is a driver that does not
3032 * understand chaining. Here biodone will reissue the call
3033 * to strategy for the incomplete buffers.
3035 printf("initiate_write_filepage: already started\n");
3038 pagedep->pd_state |= IOSTARTED;
3040 for (i = 0; i < DAHASHSZ; i++) {
3041 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3042 ep = (struct direct *)
3043 ((char *)bp->b_data + dap->da_offset);
3044 if (ep->d_ino != dap->da_newinum) {
3046 panic("%s: dir inum %d != new %d",
3047 "initiate_write_filepage",
3048 ep->d_ino, dap->da_newinum);
3050 if (dap->da_state & DIRCHG)
3051 ep->d_ino = dap->da_previous->dm_oldinum;
3054 dap->da_state &= ~ATTACHED;
3055 dap->da_state |= UNDONE;
3062 * Called from within the procedure above to deal with unsatisfied
3063 * allocation dependencies in an inodeblock. The buffer must be
3064 * locked, thus, no I/O completion operations can occur while we
3065 * are manipulating its associated dependencies.
3068 initiate_write_inodeblock(inodedep, bp)
3069 struct inodedep *inodedep;
3070 struct buf *bp; /* The inode block */
3072 struct allocdirect *adp, *lastadp;
3075 ufs_lbn_t prevlbn = 0;
3078 if (inodedep->id_state & IOSTARTED)
3079 panic("initiate_write_inodeblock: already started");
3080 inodedep->id_state |= IOSTARTED;
3081 fs = inodedep->id_fs;
3082 dp = (struct dinode *)bp->b_data +
3083 ino_to_fsbo(fs, inodedep->id_ino);
3085 * If the bitmap is not yet written, then the allocated
3086 * inode cannot be written to disk.
3088 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3089 if (inodedep->id_savedino != NULL)
3090 panic("initiate_write_inodeblock: already doing I/O");
3091 MALLOC(inodedep->id_savedino, struct dinode *,
3092 sizeof(struct dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3093 *inodedep->id_savedino = *dp;
3094 bzero((caddr_t)dp, sizeof(struct dinode));
3098 * If no dependencies, then there is nothing to roll back.
3100 inodedep->id_savedsize = dp->di_size;
3101 if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL)
3104 * Set the dependencies to busy.
3107 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3108 adp = TAILQ_NEXT(adp, ad_next)) {
3110 if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3112 panic("softdep_write_inodeblock: lbn order");
3114 prevlbn = adp->ad_lbn;
3115 if (adp->ad_lbn < NDADDR &&
3116 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3118 panic("%s: direct pointer #%ld mismatch %d != %d",
3119 "softdep_write_inodeblock", adp->ad_lbn,
3120 dp->di_db[adp->ad_lbn], adp->ad_newblkno);
3122 if (adp->ad_lbn >= NDADDR &&
3123 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3125 panic("%s: indirect pointer #%ld mismatch %d != %d",
3126 "softdep_write_inodeblock", adp->ad_lbn - NDADDR,
3127 dp->di_ib[adp->ad_lbn - NDADDR], adp->ad_newblkno);
3129 deplist |= 1 << adp->ad_lbn;
3130 if ((adp->ad_state & ATTACHED) == 0) {
3132 panic("softdep_write_inodeblock: Unknown state 0x%x",
3135 #endif /* DIAGNOSTIC */
3136 adp->ad_state &= ~ATTACHED;
3137 adp->ad_state |= UNDONE;
3140 * The on-disk inode cannot claim to be any larger than the last
3141 * fragment that has been written. Otherwise, the on-disk inode
3142 * might have fragments that were not the last block in the file
3143 * which would corrupt the filesystem.
3145 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3146 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3147 if (adp->ad_lbn >= NDADDR)
3149 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3150 /* keep going until hitting a rollback to a frag */
3151 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3153 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3154 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3156 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3158 panic("softdep_write_inodeblock: lost dep1");
3160 #endif /* DIAGNOSTIC */
3163 for (i = 0; i < NIADDR; i++) {
3165 if (dp->di_ib[i] != 0 &&
3166 (deplist & ((1 << NDADDR) << i)) == 0) {
3168 panic("softdep_write_inodeblock: lost dep2");
3170 #endif /* DIAGNOSTIC */
3177 * If we have zero'ed out the last allocated block of the file,
3178 * roll back the size to the last currently allocated block.
3179 * We know that this last allocated block is a full-sized as
3180 * we already checked for fragments in the loop above.
3182 if (lastadp != NULL &&
3183 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3184 for (i = lastadp->ad_lbn; i >= 0; i--)
3185 if (dp->di_db[i] != 0)
3187 dp->di_size = (i + 1) * fs->fs_bsize;
3190 * The only dependencies are for indirect blocks.
3192 * The file size for indirect block additions is not guaranteed.
3193 * Such a guarantee would be non-trivial to achieve. The conventional
3194 * synchronous write implementation also does not make this guarantee.
3195 * Fsck should catch and fix discrepancies. Arguably, the file size
3196 * can be over-estimated without destroying integrity when the file
3197 * moves into the indirect blocks (i.e., is large). If we want to
3198 * postpone fsck, we are stuck with this argument.
3200 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3201 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3206 * This routine is called during the completion interrupt
3207 * service routine for a disk write (from the procedure called
3208 * by the device driver to inform the filesystem caches of
3209 * a request completion). It should be called early in this
3210 * procedure, before the block is made available to other
3211 * processes or other routines are called.
3214 softdep_disk_write_complete(bp)
3215 struct buf *bp; /* describes the completed disk write */
3217 struct worklist *wk;
3218 struct workhead reattach;
3219 struct newblk *newblk;
3220 struct allocindir *aip;
3221 struct allocdirect *adp;
3222 struct indirdep *indirdep;
3223 struct inodedep *inodedep;
3224 struct bmsafemap *bmsafemap;
3227 if (lk.lkt_held != NOHOLDER)
3228 panic("softdep_disk_write_complete: lock is held");
3229 lk.lkt_held = SPECIAL_FLAG;
3231 LIST_INIT(&reattach);
3232 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
3233 WORKLIST_REMOVE(wk);
3234 switch (wk->wk_type) {
3237 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
3238 WORKLIST_INSERT(&reattach, wk);
3242 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
3243 WORKLIST_INSERT(&reattach, wk);
3247 bmsafemap = WK_BMSAFEMAP(wk);
3248 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
3249 newblk->nb_state |= DEPCOMPLETE;
3250 newblk->nb_bmsafemap = NULL;
3251 LIST_REMOVE(newblk, nb_deps);
3254 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
3255 adp->ad_state |= DEPCOMPLETE;
3257 LIST_REMOVE(adp, ad_deps);
3258 handle_allocdirect_partdone(adp);
3261 LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
3262 aip->ai_state |= DEPCOMPLETE;
3264 LIST_REMOVE(aip, ai_deps);
3265 handle_allocindir_partdone(aip);
3268 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
3269 inodedep->id_state |= DEPCOMPLETE;
3270 LIST_REMOVE(inodedep, id_deps);
3271 inodedep->id_buf = NULL;
3273 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
3277 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
3281 adp = WK_ALLOCDIRECT(wk);
3282 adp->ad_state |= COMPLETE;
3283 handle_allocdirect_partdone(adp);
3287 aip = WK_ALLOCINDIR(wk);
3288 aip->ai_state |= COMPLETE;
3289 handle_allocindir_partdone(aip);
3293 indirdep = WK_INDIRDEP(wk);
3294 if (indirdep->ir_state & GOINGAWAY) {
3295 lk.lkt_held = NOHOLDER;
3296 panic("disk_write_complete: indirdep gone");
3298 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
3299 FREE(indirdep->ir_saveddata, M_INDIRDEP);
3300 indirdep->ir_saveddata = 0;
3301 indirdep->ir_state &= ~UNDONE;
3302 indirdep->ir_state |= ATTACHED;
3303 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
3304 handle_allocindir_partdone(aip);
3305 if (aip == LIST_FIRST(&indirdep->ir_donehd)) {
3306 lk.lkt_held = NOHOLDER;
3307 panic("disk_write_complete: not gone");
3310 WORKLIST_INSERT(&reattach, wk);
3311 if ((bp->b_flags & B_DELWRI) == 0)
3312 stat_indir_blk_ptrs++;
3317 lk.lkt_held = NOHOLDER;
3318 panic("handle_disk_write_complete: Unknown type %s",
3319 TYPENAME(wk->wk_type));
3324 * Reattach any requests that must be redone.
3326 while ((wk = LIST_FIRST(&reattach)) != NULL) {
3327 WORKLIST_REMOVE(wk);
3328 WORKLIST_INSERT(&bp->b_dep, wk);
3331 if (lk.lkt_held != SPECIAL_FLAG)
3332 panic("softdep_disk_write_complete: lock lost");
3333 lk.lkt_held = NOHOLDER;
3338 * Called from within softdep_disk_write_complete above. Note that
3339 * this routine is always called from interrupt level with further
3340 * splbio interrupts blocked.
3343 handle_allocdirect_partdone(adp)
3344 struct allocdirect *adp; /* the completed allocdirect */
3346 struct allocdirect *listadp;
3347 struct inodedep *inodedep;
3350 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3352 if (adp->ad_buf != NULL) {
3353 lk.lkt_held = NOHOLDER;
3354 panic("handle_allocdirect_partdone: dangling dep");
3357 * The on-disk inode cannot claim to be any larger than the last
3358 * fragment that has been written. Otherwise, the on-disk inode
3359 * might have fragments that were not the last block in the file
3360 * which would corrupt the filesystem. Thus, we cannot free any
3361 * allocdirects after one whose ad_oldblkno claims a fragment as
3362 * these blocks must be rolled back to zero before writing the inode.
3363 * We check the currently active set of allocdirects in id_inoupdt.
3365 inodedep = adp->ad_inodedep;
3366 bsize = inodedep->id_fs->fs_bsize;
3367 TAILQ_FOREACH(listadp, &inodedep->id_inoupdt, ad_next) {
3368 /* found our block */
3371 /* continue if ad_oldlbn is not a fragment */
3372 if (listadp->ad_oldsize == 0 ||
3373 listadp->ad_oldsize == bsize)
3375 /* hit a fragment */
3379 * If we have reached the end of the current list without
3380 * finding the just finished dependency, then it must be
3381 * on the future dependency list. Future dependencies cannot
3382 * be freed until they are moved to the current list.
3384 if (listadp == NULL) {
3386 TAILQ_FOREACH(listadp, &inodedep->id_newinoupdt, ad_next)
3387 /* found our block */
3390 if (listadp == NULL) {
3391 lk.lkt_held = NOHOLDER;
3392 panic("handle_allocdirect_partdone: lost dep");
3398 * If we have found the just finished dependency, then free
3399 * it along with anything that follows it that is complete.
3401 for (; adp; adp = listadp) {
3402 listadp = TAILQ_NEXT(adp, ad_next);
3403 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3405 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
3410 * Called from within softdep_disk_write_complete above. Note that
3411 * this routine is always called from interrupt level with further
3412 * splbio interrupts blocked.
3415 handle_allocindir_partdone(aip)
3416 struct allocindir *aip; /* the completed allocindir */
3418 struct indirdep *indirdep;
3420 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
3422 if (aip->ai_buf != NULL) {
3423 lk.lkt_held = NOHOLDER;
3424 panic("handle_allocindir_partdone: dangling dependency");
3426 indirdep = aip->ai_indirdep;
3427 if (indirdep->ir_state & UNDONE) {
3428 LIST_REMOVE(aip, ai_next);
3429 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
3432 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
3434 LIST_REMOVE(aip, ai_next);
3435 if (aip->ai_freefrag != NULL)
3436 add_to_worklist(&aip->ai_freefrag->ff_list);
3437 WORKITEM_FREE(aip, D_ALLOCINDIR);
3441 * Called from within softdep_disk_write_complete above to restore
3442 * in-memory inode block contents to their most up-to-date state. Note
3443 * that this routine is always called from interrupt level with further
3444 * splbio interrupts blocked.
3447 handle_written_inodeblock(inodedep, bp)
3448 struct inodedep *inodedep;
3449 struct buf *bp; /* buffer containing the inode block */
3451 struct worklist *wk, *filefree;
3452 struct allocdirect *adp, *nextadp;
3456 if ((inodedep->id_state & IOSTARTED) == 0) {
3457 lk.lkt_held = NOHOLDER;
3458 panic("handle_written_inodeblock: not started");
3460 inodedep->id_state &= ~IOSTARTED;
3461 inodedep->id_state |= COMPLETE;
3462 dp = (struct dinode *)bp->b_data +
3463 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
3465 * If we had to rollback the inode allocation because of
3466 * bitmaps being incomplete, then simply restore it.
3467 * Keep the block dirty so that it will not be reclaimed until
3468 * all associated dependencies have been cleared and the
3469 * corresponding updates written to disk.
3471 if (inodedep->id_savedino != NULL) {
3472 *dp = *inodedep->id_savedino;
3473 FREE(inodedep->id_savedino, M_INODEDEP);
3474 inodedep->id_savedino = NULL;
3475 if ((bp->b_flags & B_DELWRI) == 0)
3476 stat_inode_bitmap++;
3481 * Roll forward anything that had to be rolled back before
3482 * the inode could be updated.
3485 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
3486 nextadp = TAILQ_NEXT(adp, ad_next);
3487 if (adp->ad_state & ATTACHED) {
3488 lk.lkt_held = NOHOLDER;
3489 panic("handle_written_inodeblock: new entry");
3491 if (adp->ad_lbn < NDADDR) {
3492 if (dp->di_db[adp->ad_lbn] != adp->ad_oldblkno) {
3493 lk.lkt_held = NOHOLDER;
3494 panic("%s: %s #%ld mismatch %d != %d",
3495 "handle_written_inodeblock",
3496 "direct pointer", adp->ad_lbn,
3497 dp->di_db[adp->ad_lbn], adp->ad_oldblkno);
3499 dp->di_db[adp->ad_lbn] = adp->ad_newblkno;
3501 if (dp->di_ib[adp->ad_lbn - NDADDR] != 0) {
3502 lk.lkt_held = NOHOLDER;
3503 panic("%s: %s #%ld allocated as %d",
3504 "handle_written_inodeblock",
3505 "indirect pointer", adp->ad_lbn - NDADDR,
3506 dp->di_ib[adp->ad_lbn - NDADDR]);
3508 dp->di_ib[adp->ad_lbn - NDADDR] = adp->ad_newblkno;
3510 adp->ad_state &= ~UNDONE;
3511 adp->ad_state |= ATTACHED;
3514 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
3515 stat_direct_blk_ptrs++;
3517 * Reset the file size to its most up-to-date value.
3519 if (inodedep->id_savedsize == -1) {
3520 lk.lkt_held = NOHOLDER;
3521 panic("handle_written_inodeblock: bad size");
3523 if (dp->di_size != inodedep->id_savedsize) {
3524 dp->di_size = inodedep->id_savedsize;
3527 inodedep->id_savedsize = -1;
3529 * If there were any rollbacks in the inode block, then it must be
3530 * marked dirty so that its will eventually get written back in
3536 * Process any allocdirects that completed during the update.
3538 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
3539 handle_allocdirect_partdone(adp);
3541 * Process deallocations that were held pending until the
3542 * inode had been written to disk. Freeing of the inode
3543 * is delayed until after all blocks have been freed to
3544 * avoid creation of new <vfsid, inum, lbn> triples
3545 * before the old ones have been deleted.
3548 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
3549 WORKLIST_REMOVE(wk);
3550 switch (wk->wk_type) {
3554 * We defer adding filefree to the worklist until
3555 * all other additions have been made to ensure
3556 * that it will be done after all the old blocks
3559 if (filefree != NULL) {
3560 lk.lkt_held = NOHOLDER;
3561 panic("handle_written_inodeblock: filefree");
3567 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
3571 diradd_inode_written(WK_DIRADD(wk), inodedep);
3577 add_to_worklist(wk);
3581 lk.lkt_held = NOHOLDER;
3582 panic("handle_written_inodeblock: Unknown type %s",
3583 TYPENAME(wk->wk_type));
3587 if (filefree != NULL) {
3588 if (free_inodedep(inodedep) == 0) {
3589 lk.lkt_held = NOHOLDER;
3590 panic("handle_written_inodeblock: live inodedep");
3592 add_to_worklist(filefree);
3597 * If no outstanding dependencies, free it.
3599 if (free_inodedep(inodedep) || TAILQ_FIRST(&inodedep->id_inoupdt) == 0)
3601 return (hadchanges);
3605 * Process a diradd entry after its dependent inode has been written.
3606 * This routine must be called with splbio interrupts blocked.
3609 diradd_inode_written(dap, inodedep)
3611 struct inodedep *inodedep;
3613 struct pagedep *pagedep;
3615 dap->da_state |= COMPLETE;
3616 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3617 if (dap->da_state & DIRCHG)
3618 pagedep = dap->da_previous->dm_pagedep;
3620 pagedep = dap->da_pagedep;
3621 LIST_REMOVE(dap, da_pdlist);
3622 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3624 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3628 * Handle the completion of a mkdir dependency.
3631 handle_written_mkdir(mkdir, type)
3632 struct mkdir *mkdir;
3636 struct pagedep *pagedep;
3638 if (mkdir->md_state != type) {
3639 lk.lkt_held = NOHOLDER;
3640 panic("handle_written_mkdir: bad type");
3642 dap = mkdir->md_diradd;
3643 dap->da_state &= ~type;
3644 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
3645 dap->da_state |= DEPCOMPLETE;
3646 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3647 if (dap->da_state & DIRCHG)
3648 pagedep = dap->da_previous->dm_pagedep;
3650 pagedep = dap->da_pagedep;
3651 LIST_REMOVE(dap, da_pdlist);
3652 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3654 LIST_REMOVE(mkdir, md_mkdirs);
3655 WORKITEM_FREE(mkdir, D_MKDIR);
3659 * Called from within softdep_disk_write_complete above.
3660 * A write operation was just completed. Removed inodes can
3661 * now be freed and associated block pointers may be committed.
3662 * Note that this routine is always called from interrupt level
3663 * with further splbio interrupts blocked.
3666 handle_written_filepage(pagedep, bp)
3667 struct pagedep *pagedep;
3668 struct buf *bp; /* buffer containing the written page */
3670 struct dirrem *dirrem;
3671 struct diradd *dap, *nextdap;
3675 if ((pagedep->pd_state & IOSTARTED) == 0) {
3676 lk.lkt_held = NOHOLDER;
3677 panic("handle_written_filepage: not started");
3679 pagedep->pd_state &= ~IOSTARTED;
3681 * Process any directory removals that have been committed.
3683 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
3684 LIST_REMOVE(dirrem, dm_next);
3685 dirrem->dm_dirinum = pagedep->pd_ino;
3686 add_to_worklist(&dirrem->dm_list);
3689 * Free any directory additions that have been committed.
3691 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
3694 * Uncommitted directory entries must be restored.
3696 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
3697 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
3699 nextdap = LIST_NEXT(dap, da_pdlist);
3700 if (dap->da_state & ATTACHED) {
3701 lk.lkt_held = NOHOLDER;
3702 panic("handle_written_filepage: attached");
3704 ep = (struct direct *)
3705 ((char *)bp->b_data + dap->da_offset);
3706 ep->d_ino = dap->da_newinum;
3707 dap->da_state &= ~UNDONE;
3708 dap->da_state |= ATTACHED;
3711 * If the inode referenced by the directory has
3712 * been written out, then the dependency can be
3713 * moved to the pending list.
3715 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3716 LIST_REMOVE(dap, da_pdlist);
3717 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
3723 * If there were any rollbacks in the directory, then it must be
3724 * marked dirty so that its will eventually get written back in
3728 if ((bp->b_flags & B_DELWRI) == 0)
3733 * If no dependencies remain, the pagedep will be freed.
3734 * Otherwise it will remain to update the page before it
3735 * is written back to disk.
3737 if (LIST_FIRST(&pagedep->pd_pendinghd) == 0) {
3738 for (i = 0; i < DAHASHSZ; i++)
3739 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) != NULL)
3741 if (i == DAHASHSZ) {
3742 LIST_REMOVE(pagedep, pd_hash);
3743 WORKITEM_FREE(pagedep, D_PAGEDEP);
3751 * Writing back in-core inode structures.
3753 * The filesystem only accesses an inode's contents when it occupies an
3754 * "in-core" inode structure. These "in-core" structures are separate from
3755 * the page frames used to cache inode blocks. Only the latter are
3756 * transferred to/from the disk. So, when the updated contents of the
3757 * "in-core" inode structure are copied to the corresponding in-memory inode
3758 * block, the dependencies are also transferred. The following procedure is
3759 * called when copying a dirty "in-core" inode to a cached inode block.
3763 * Called when an inode is loaded from disk. If the effective link count
3764 * differed from the actual link count when it was last flushed, then we
3765 * need to ensure that the correct effective link count is put back.
3768 softdep_load_inodeblock(ip)
3769 struct inode *ip; /* the "in_core" copy of the inode */
3771 struct inodedep *inodedep;
3774 * Check for alternate nlink count.
3776 ip->i_effnlink = ip->i_nlink;
3778 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3782 ip->i_effnlink -= inodedep->id_nlinkdelta;
3787 * This routine is called just before the "in-core" inode
3788 * information is to be copied to the in-memory inode block.
3789 * Recall that an inode block contains several inodes. If
3790 * the force flag is set, then the dependencies will be
3791 * cleared so that the update can always be made. Note that
3792 * the buffer is locked when this routine is called, so we
3793 * will never be in the middle of writing the inode block
3797 softdep_update_inodeblock(ip, bp, waitfor)
3798 struct inode *ip; /* the "in_core" copy of the inode */
3799 struct buf *bp; /* the buffer containing the inode block */
3800 int waitfor; /* nonzero => update must be allowed */
3802 struct inodedep *inodedep;
3803 struct worklist *wk;
3807 * If the effective link count is not equal to the actual link
3808 * count, then we must track the difference in an inodedep while
3809 * the inode is (potentially) tossed out of the cache. Otherwise,
3810 * if there is no existing inodedep, then there are no dependencies
3814 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3816 if (ip->i_effnlink != ip->i_nlink)
3817 panic("softdep_update_inodeblock: bad link count");
3820 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) {
3822 panic("softdep_update_inodeblock: bad delta");
3825 * Changes have been initiated. Anything depending on these
3826 * changes cannot occur until this inode has been written.
3828 inodedep->id_state &= ~COMPLETE;
3829 if ((inodedep->id_state & ONWORKLIST) == 0)
3830 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
3832 * Any new dependencies associated with the incore inode must
3833 * now be moved to the list associated with the buffer holding
3834 * the in-memory copy of the inode. Once merged process any
3835 * allocdirects that are completed by the merger.
3837 merge_inode_lists(inodedep);
3838 if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL)
3839 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
3841 * Now that the inode has been pushed into the buffer, the
3842 * operations dependent on the inode being written to disk
3843 * can be moved to the id_bufwait so that they will be
3844 * processed when the buffer I/O completes.
3846 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
3847 WORKLIST_REMOVE(wk);
3848 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
3851 * Newly allocated inodes cannot be written until the bitmap
3852 * that allocates them have been written (indicated by
3853 * DEPCOMPLETE being set in id_state). If we are doing a
3854 * forced sync (e.g., an fsync on a file), we force the bitmap
3855 * to be written so that the update can be done.
3857 if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) {
3861 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
3864 (error = VOP_BWRITE(inodedep->id_buf->b_vp, inodedep->id_buf)) != 0)
3865 softdep_error("softdep_update_inodeblock: bwrite", error);
3866 if ((inodedep->id_state & DEPCOMPLETE) == 0)
3867 panic("softdep_update_inodeblock: update failed");
3871 * Merge the new inode dependency list (id_newinoupdt) into the old
3872 * inode dependency list (id_inoupdt). This routine must be called
3873 * with splbio interrupts blocked.
3876 merge_inode_lists(inodedep)
3877 struct inodedep *inodedep;
3879 struct allocdirect *listadp, *newadp;
3881 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
3882 for (listadp = TAILQ_FIRST(&inodedep->id_inoupdt); listadp && newadp;) {
3883 if (listadp->ad_lbn < newadp->ad_lbn) {
3884 listadp = TAILQ_NEXT(listadp, ad_next);
3887 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
3888 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
3889 if (listadp->ad_lbn == newadp->ad_lbn) {
3890 allocdirect_merge(&inodedep->id_inoupdt, newadp,
3894 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
3896 while ((newadp = TAILQ_FIRST(&inodedep->id_newinoupdt)) != NULL) {
3897 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
3898 TAILQ_INSERT_TAIL(&inodedep->id_inoupdt, newadp, ad_next);
3903 * If we are doing an fsync, then we must ensure that any directory
3904 * entries for the inode have been written after the inode gets to disk.
3908 struct vnode *vp; /* the "in_core" copy of the inode */
3910 struct inodedep *inodedep;
3911 struct pagedep *pagedep;
3912 struct worklist *wk;
3919 struct thread *td = curthread; /* XXX */
3920 int error, flushparent;
3927 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) {
3931 if (LIST_FIRST(&inodedep->id_inowait) != NULL ||
3932 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
3933 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
3934 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) {
3936 panic("softdep_fsync: pending ops");
3938 for (error = 0, flushparent = 0; ; ) {
3939 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
3941 if (wk->wk_type != D_DIRADD) {
3943 panic("softdep_fsync: Unexpected type %s",
3944 TYPENAME(wk->wk_type));
3946 dap = WK_DIRADD(wk);
3948 * Flush our parent if this directory entry
3949 * has a MKDIR_PARENT dependency.
3951 if (dap->da_state & DIRCHG)
3952 pagedep = dap->da_previous->dm_pagedep;
3954 pagedep = dap->da_pagedep;
3955 mnt = pagedep->pd_mnt;
3956 parentino = pagedep->pd_ino;
3957 lbn = pagedep->pd_lbn;
3958 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) {
3960 panic("softdep_fsync: dirty");
3962 flushparent = dap->da_state & MKDIR_PARENT;
3964 * If we are being fsync'ed as part of vgone'ing this vnode,
3965 * then we will not be able to release and recover the
3966 * vnode below, so we just have to give up on writing its
3967 * directory entry out. It will eventually be written, just
3968 * not now, but then the user was not asking to have it
3969 * written, so we are not breaking any promises.
3971 if (vp->v_flag & VXLOCK)
3974 * We prevent deadlock by always fetching inodes from the
3975 * root, moving down the directory tree. Thus, when fetching
3976 * our parent directory, we must unlock ourselves before
3977 * requesting the lock on our parent. See the comment in
3978 * ufs_lookup for details on possible races.
3981 VOP_UNLOCK(vp, NULL, 0, td);
3982 error = VFS_VGET(mnt, parentino, &pvp);
3983 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
3987 if ((error = UFS_UPDATE(pvp, 1)) != 0) {
3993 * Flush directory page containing the inode's name.
3995 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), &bp);
3997 error = VOP_BWRITE(bp->b_vp, bp);
4002 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0)
4010 * Flush all the dirty bitmaps associated with the block device
4011 * before flushing the rest of the dirty blocks so as to reduce
4012 * the number of dependencies that will have to be rolled back.
4015 softdep_fsync_mountdev(vp)
4018 struct buf *bp, *nbp;
4019 struct worklist *wk;
4021 if (!vn_isdisk(vp, NULL))
4022 panic("softdep_fsync_mountdev: vnode not a disk");
4024 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
4025 nbp = TAILQ_NEXT(bp, b_vnbufs);
4027 * If it is already scheduled, skip to the next buffer.
4029 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
4031 if ((bp->b_flags & B_DELWRI) == 0) {
4033 panic("softdep_fsync_mountdev: not dirty");
4036 * We are only interested in bitmaps with outstanding
4039 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
4040 wk->wk_type != D_BMSAFEMAP ||
4041 (bp->b_xflags & BX_BKGRDINPROG)) {
4050 * Since we may have slept during the I/O, we need
4051 * to start from a known point.
4053 nbp = TAILQ_FIRST(&vp->v_dirtyblkhd);
4055 drain_output(vp, 1);
4060 * This routine is called when we are trying to synchronously flush a
4061 * file. This routine must eliminate any filesystem metadata dependencies
4062 * so that the syncing routine can succeed by pushing the dirty blocks
4063 * associated with the file. If any I/O errors occur, they are returned.
4066 softdep_sync_metadata(ap)
4067 struct vop_fsync_args /* {
4069 struct ucred *a_cred;
4074 struct vnode *vp = ap->a_vp;
4075 struct pagedep *pagedep;
4076 struct allocdirect *adp;
4077 struct allocindir *aip;
4078 struct buf *bp, *nbp;
4079 struct worklist *wk;
4080 int i, error, waitfor;
4083 * Check whether this vnode is involved in a filesystem
4084 * that is doing soft dependency processing.
4086 if (!vn_isdisk(vp, NULL)) {
4087 if (!DOINGSOFTDEP(vp))
4090 if (vp->v_rdev->si_mountpoint == NULL ||
4091 (vp->v_rdev->si_mountpoint->mnt_flag & MNT_SOFTDEP) == 0)
4094 * Ensure that any direct block dependencies have been cleared.
4097 if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) {
4102 * For most files, the only metadata dependencies are the
4103 * cylinder group maps that allocate their inode or blocks.
4104 * The block allocation dependencies can be found by traversing
4105 * the dependency lists for any buffers that remain on their
4106 * dirty buffer list. The inode allocation dependency will
4107 * be resolved when the inode is updated with MNT_WAIT.
4108 * This work is done in two passes. The first pass grabs most
4109 * of the buffers and begins asynchronously writing them. The
4110 * only way to wait for these asynchronous writes is to sleep
4111 * on the filesystem vnode which may stay busy for a long time
4112 * if the filesystem is active. So, instead, we make a second
4113 * pass over the dependencies blocking on each write. In the
4114 * usual case we will be blocking against a write that we
4115 * initiated, so when it is done the dependency will have been
4116 * resolved. Thus the second pass is expected to end quickly.
4118 waitfor = MNT_NOWAIT;
4121 * We must wait for any I/O in progress to finish so that
4122 * all potential buffers on the dirty list will be visible.
4124 drain_output(vp, 1);
4125 if (getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd), MNT_WAIT) == 0) {
4129 bp = TAILQ_FIRST(&vp->v_dirtyblkhd);
4132 * As we hold the buffer locked, none of its dependencies
4135 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4136 switch (wk->wk_type) {
4139 adp = WK_ALLOCDIRECT(wk);
4140 if (adp->ad_state & DEPCOMPLETE)
4143 if (getdirtybuf(&nbp, waitfor) == 0)
4146 if (waitfor == MNT_NOWAIT) {
4148 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4156 aip = WK_ALLOCINDIR(wk);
4157 if (aip->ai_state & DEPCOMPLETE)
4160 if (getdirtybuf(&nbp, waitfor) == 0)
4163 if (waitfor == MNT_NOWAIT) {
4165 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4175 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
4176 if (aip->ai_state & DEPCOMPLETE)
4179 if (getdirtybuf(&nbp, MNT_WAIT) == 0)
4182 if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4192 if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs,
4193 WK_INODEDEP(wk)->id_ino)) != 0) {
4202 * We are trying to sync a directory that may
4203 * have dependencies on both its own metadata
4204 * and/or dependencies on the inodes of any
4205 * recently allocated files. We walk its diradd
4206 * lists pushing out the associated inode.
4208 pagedep = WK_PAGEDEP(wk);
4209 for (i = 0; i < DAHASHSZ; i++) {
4210 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
4213 flush_pagedep_deps(vp, pagedep->pd_mnt,
4214 &pagedep->pd_diraddhd[i]))) {
4224 * This case should never happen if the vnode has
4225 * been properly sync'ed. However, if this function
4226 * is used at a place where the vnode has not yet
4227 * been sync'ed, this dependency can show up. So,
4228 * rather than panic, just flush it.
4230 nbp = WK_MKDIR(wk)->md_buf;
4231 if (getdirtybuf(&nbp, waitfor) == 0)
4234 if (waitfor == MNT_NOWAIT) {
4236 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4245 * This case should never happen if the vnode has
4246 * been properly sync'ed. However, if this function
4247 * is used at a place where the vnode has not yet
4248 * been sync'ed, this dependency can show up. So,
4249 * rather than panic, just flush it.
4251 nbp = WK_BMSAFEMAP(wk)->sm_buf;
4252 if (getdirtybuf(&nbp, waitfor) == 0)
4255 if (waitfor == MNT_NOWAIT) {
4257 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4266 panic("softdep_sync_metadata: Unknown type %s",
4267 TYPENAME(wk->wk_type));
4271 (void) getdirtybuf(&TAILQ_NEXT(bp, b_vnbufs), MNT_WAIT);
4272 nbp = TAILQ_NEXT(bp, b_vnbufs);
4281 * The brief unlock is to allow any pent up dependency
4282 * processing to be done. Then proceed with the second pass.
4284 if (waitfor == MNT_NOWAIT) {
4292 * If we have managed to get rid of all the dirty buffers,
4293 * then we are done. For certain directories and block
4294 * devices, we may need to do further work.
4296 * We must wait for any I/O in progress to finish so that
4297 * all potential buffers on the dirty list will be visible.
4299 drain_output(vp, 1);
4300 if (TAILQ_FIRST(&vp->v_dirtyblkhd) == NULL) {
4307 * If we are trying to sync a block device, some of its buffers may
4308 * contain metadata that cannot be written until the contents of some
4309 * partially written files have been written to disk. The only easy
4310 * way to accomplish this is to sync the entire filesystem (luckily
4311 * this happens rarely).
4313 if (vn_isdisk(vp, NULL) &&
4315 vp->v_rdev->si_mountpoint && !VOP_ISLOCKED(vp, NULL) &&
4316 (error = VFS_SYNC(vp->v_rdev->si_mountpoint, MNT_WAIT, ap->a_td)) != 0)
4322 * Flush the dependencies associated with an inodedep.
4323 * Called with splbio blocked.
4326 flush_inodedep_deps(fs, ino)
4330 struct inodedep *inodedep;
4331 struct allocdirect *adp;
4336 * This work is done in two passes. The first pass grabs most
4337 * of the buffers and begins asynchronously writing them. The
4338 * only way to wait for these asynchronous writes is to sleep
4339 * on the filesystem vnode which may stay busy for a long time
4340 * if the filesystem is active. So, instead, we make a second
4341 * pass over the dependencies blocking on each write. In the
4342 * usual case we will be blocking against a write that we
4343 * initiated, so when it is done the dependency will have been
4344 * resolved. Thus the second pass is expected to end quickly.
4345 * We give a brief window at the top of the loop to allow
4346 * any pending I/O to complete.
4348 for (waitfor = MNT_NOWAIT; ; ) {
4351 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4353 TAILQ_FOREACH(adp, &inodedep->id_inoupdt, ad_next) {
4354 if (adp->ad_state & DEPCOMPLETE)
4357 if (getdirtybuf(&bp, waitfor) == 0) {
4358 if (waitfor == MNT_NOWAIT)
4363 if (waitfor == MNT_NOWAIT) {
4365 } else if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) {
4374 TAILQ_FOREACH(adp, &inodedep->id_newinoupdt, ad_next) {
4375 if (adp->ad_state & DEPCOMPLETE)
4378 if (getdirtybuf(&bp, waitfor) == 0) {
4379 if (waitfor == MNT_NOWAIT)
4384 if (waitfor == MNT_NOWAIT) {
4386 } else if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) {
4396 * If pass2, we are done, otherwise do pass 2.
4398 if (waitfor == MNT_WAIT)
4403 * Try freeing inodedep in case all dependencies have been removed.
4405 if (inodedep_lookup(fs, ino, 0, &inodedep) != 0)
4406 (void) free_inodedep(inodedep);
4411 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
4412 * Called with splbio blocked.
4415 flush_pagedep_deps(pvp, mp, diraddhdp)
4418 struct diraddhd *diraddhdp;
4420 struct thread *td = curthread; /* XXX */
4421 struct inodedep *inodedep;
4422 struct ufsmount *ump;
4425 int gotit, error = 0;
4430 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
4432 * Flush ourselves if this directory entry
4433 * has a MKDIR_PARENT dependency.
4435 if (dap->da_state & MKDIR_PARENT) {
4437 if ((error = UFS_UPDATE(pvp, 1)) != 0)
4441 * If that cleared dependencies, go on to next.
4443 if (dap != LIST_FIRST(diraddhdp))
4445 if (dap->da_state & MKDIR_PARENT) {
4447 panic("flush_pagedep_deps: MKDIR_PARENT");
4451 * A newly allocated directory must have its "." and
4452 * ".." entries written out before its name can be
4453 * committed in its parent. We do not want or need
4454 * the full semantics of a synchronous VOP_FSYNC as
4455 * that may end up here again, once for each directory
4456 * level in the filesystem. Instead, we push the blocks
4457 * and wait for them to clear. We have to fsync twice
4458 * because the first call may choose to defer blocks
4459 * that still have dependencies, but deferral will
4460 * happen at most once.
4462 inum = dap->da_newinum;
4463 if (dap->da_state & MKDIR_BODY) {
4465 if ((error = VFS_VGET(mp, inum, &vp)) != 0)
4467 if ((error=VOP_FSYNC(vp, MNT_NOWAIT, td)) ||
4468 (error=VOP_FSYNC(vp, MNT_NOWAIT, td))) {
4472 drain_output(vp, 0);
4476 * If that cleared dependencies, go on to next.
4478 if (dap != LIST_FIRST(diraddhdp))
4480 if (dap->da_state & MKDIR_BODY) {
4482 panic("flush_pagedep_deps: MKDIR_BODY");
4486 * Flush the inode on which the directory entry depends.
4487 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
4488 * the only remaining dependency is that the updated inode
4489 * count must get pushed to disk. The inode has already
4490 * been pushed into its inode buffer (via VOP_UPDATE) at
4491 * the time of the reference count change. So we need only
4492 * locate that buffer, ensure that there will be no rollback
4493 * caused by a bitmap dependency, then write the inode buffer.
4495 if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0) {
4497 panic("flush_pagedep_deps: lost inode");
4500 * If the inode still has bitmap dependencies,
4501 * push them to disk.
4503 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4504 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
4507 (error = VOP_BWRITE(inodedep->id_buf->b_vp,
4508 inodedep->id_buf)) != 0)
4511 if (dap != LIST_FIRST(diraddhdp))
4515 * If the inode is still sitting in a buffer waiting
4516 * to be written, push it to disk.
4519 if ((error = bread(ump->um_devvp,
4520 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
4521 (int)ump->um_fs->fs_bsize, &bp)) != 0)
4523 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
4527 * If we have failed to get rid of all the dependencies
4528 * then something is seriously wrong.
4530 if (dap == LIST_FIRST(diraddhdp)) {
4532 panic("flush_pagedep_deps: flush failed");
4541 * A large burst of file addition or deletion activity can drive the
4542 * memory load excessively high. First attempt to slow things down
4543 * using the techniques below. If that fails, this routine requests
4544 * the offending operations to fall back to running synchronously
4545 * until the memory load returns to a reasonable level.
4548 softdep_slowdown(vp)
4551 int max_softdeps_hard;
4553 max_softdeps_hard = max_softdeps * 11 / 10;
4554 if (num_dirrem < max_softdeps_hard / 2 &&
4555 num_inodedep < max_softdeps_hard)
4557 stat_sync_limit_hit += 1;
4562 * If memory utilization has gotten too high, deliberately slow things
4563 * down and speed up the I/O processing.
4566 request_cleanup(resource, islocked)
4570 struct thread *td = curthread; /* XXX */
4573 * We never hold up the filesystem syncer process.
4575 if (td == filesys_syncer)
4578 * First check to see if the work list has gotten backlogged.
4579 * If it has, co-opt this process to help clean up two entries.
4580 * Because this process may hold inodes locked, we cannot
4581 * handle any remove requests that might block on a locked
4582 * inode as that could lead to deadlock.
4584 if (num_on_worklist > max_softdeps / 10) {
4587 process_worklist_item(NULL, LK_NOWAIT);
4588 process_worklist_item(NULL, LK_NOWAIT);
4589 stat_worklist_push += 2;
4596 * If we are resource constrained on inode dependencies, try
4597 * flushing some dirty inodes. Otherwise, we are constrained
4598 * by file deletions, so try accelerating flushes of directories
4599 * with removal dependencies. We would like to do the cleanup
4600 * here, but we probably hold an inode locked at this point and
4601 * that might deadlock against one that we try to clean. So,
4602 * the best that we can do is request the syncer daemon to do
4603 * the cleanup for us.
4608 stat_ino_limit_push += 1;
4609 req_clear_inodedeps += 1;
4610 stat_countp = &stat_ino_limit_hit;
4614 stat_blk_limit_push += 1;
4615 req_clear_remove += 1;
4616 stat_countp = &stat_blk_limit_hit;
4622 panic("request_cleanup: unknown type");
4625 * Hopefully the syncer daemon will catch up and awaken us.
4626 * We wait at most tickdelay before proceeding in any case.
4631 if (handle.callout == NULL)
4632 handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
4633 interlocked_sleep(&lk, SLEEP, (caddr_t)&proc_waiting, 0,
4642 * Awaken processes pausing in request_cleanup and clear proc_waiting
4643 * to indicate that there is no longer a timer running.
4651 wakeup_one(&proc_waiting);
4652 if (proc_waiting > 0)
4653 handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
4655 handle.callout = NULL;
4659 * Flush out a directory with at least one removal dependency in an effort to
4660 * reduce the number of dirrem, freefile, and freeblks dependency structures.
4663 clear_remove(struct thread *td)
4665 struct pagedep_hashhead *pagedephd;
4666 struct pagedep *pagedep;
4667 static int next = 0;
4674 for (cnt = 0; cnt < pagedep_hash; cnt++) {
4675 pagedephd = &pagedep_hashtbl[next++];
4676 if (next >= pagedep_hash)
4678 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
4679 if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL)
4681 mp = pagedep->pd_mnt;
4682 ino = pagedep->pd_ino;
4684 if ((error = VFS_VGET(mp, ino, &vp)) != 0) {
4685 softdep_error("clear_remove: vget", error);
4688 if ((error = VOP_FSYNC(vp, MNT_NOWAIT, td)))
4689 softdep_error("clear_remove: fsync", error);
4690 drain_output(vp, 0);
4699 * Clear out a block of dirty inodes in an effort to reduce
4700 * the number of inodedep dependency structures.
4703 clear_inodedeps(struct thread *td)
4705 struct inodedep_hashhead *inodedephd;
4706 struct inodedep *inodedep;
4707 static int next = 0;
4712 ino_t firstino, lastino, ino;
4716 * Pick a random inode dependency to be cleared.
4717 * We will then gather up all the inodes in its block
4718 * that have dependencies and flush them out.
4720 for (cnt = 0; cnt < inodedep_hash; cnt++) {
4721 inodedephd = &inodedep_hashtbl[next++];
4722 if (next >= inodedep_hash)
4724 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
4727 if (inodedep == NULL)
4730 * Ugly code to find mount point given pointer to superblock.
4732 fs = inodedep->id_fs;
4733 TAILQ_FOREACH(mp, &mountlist, mnt_list)
4734 if ((mp->mnt_flag & MNT_SOFTDEP) && fs == VFSTOUFS(mp)->um_fs)
4737 * Find the last inode in the block with dependencies.
4739 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
4740 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
4741 if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
4744 * Asynchronously push all but the last inode with dependencies.
4745 * Synchronously push the last inode with dependencies to ensure
4746 * that the inode block gets written to free up the inodedeps.
4748 for (ino = firstino; ino <= lastino; ino++) {
4749 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4752 if ((error = VFS_VGET(mp, ino, &vp)) != 0) {
4753 softdep_error("clear_inodedeps: vget", error);
4756 if (ino == lastino) {
4757 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)))
4758 softdep_error("clear_inodedeps: fsync1", error);
4760 if ((error = VOP_FSYNC(vp, MNT_NOWAIT, td)))
4761 softdep_error("clear_inodedeps: fsync2", error);
4762 drain_output(vp, 0);
4771 * Function to determine if the buffer has outstanding dependencies
4772 * that will cause a roll-back if the buffer is written. If wantcount
4773 * is set, return number of dependencies, otherwise just yes or no.
4776 softdep_count_dependencies(bp, wantcount)
4780 struct worklist *wk;
4781 struct inodedep *inodedep;
4782 struct indirdep *indirdep;
4783 struct allocindir *aip;
4784 struct pagedep *pagedep;
4790 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4791 switch (wk->wk_type) {
4794 inodedep = WK_INODEDEP(wk);
4795 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4796 /* bitmap allocation dependency */
4801 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
4802 /* direct block pointer dependency */
4810 indirdep = WK_INDIRDEP(wk);
4812 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
4813 /* indirect block pointer dependency */
4821 pagedep = WK_PAGEDEP(wk);
4822 for (i = 0; i < DAHASHSZ; i++) {
4824 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
4825 /* directory entry dependency */
4837 /* never a dependency on these blocks */
4842 panic("softdep_check_for_rollback: Unexpected type %s",
4843 TYPENAME(wk->wk_type));
4853 * Acquire exclusive access to a buffer.
4854 * Must be called with splbio blocked.
4855 * Return 1 if buffer was acquired.
4858 getdirtybuf(bpp, waitfor)
4866 if ((bp = *bpp) == NULL)
4868 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
4869 if ((bp->b_xflags & BX_BKGRDINPROG) == 0)
4872 if (waitfor != MNT_WAIT)
4874 bp->b_xflags |= BX_BKGRDWAIT;
4875 interlocked_sleep(&lk, SLEEP, &bp->b_xflags, 0,
4879 if (waitfor != MNT_WAIT)
4881 error = interlocked_sleep(&lk, LOCKBUF, bp,
4882 LK_EXCLUSIVE | LK_SLEEPFAIL, 0, 0);
4883 if (error != ENOLCK) {
4885 panic("getdirtybuf: inconsistent lock");
4888 if ((bp->b_flags & B_DELWRI) == 0) {
4897 * Wait for pending output on a vnode to complete.
4898 * Must be called with vnode locked.
4901 drain_output(vp, islocked)
4908 while (vp->v_numoutput) {
4909 vp->v_flag |= VBWAIT;
4910 interlocked_sleep(&lk, SLEEP, (caddr_t)&vp->v_numoutput,
4918 * Called whenever a buffer that is being invalidated or reallocated
4919 * contains dependencies. This should only happen if an I/O error has
4920 * occurred. The routine is called with the buffer locked.
4923 softdep_deallocate_dependencies(bp)
4927 if ((bp->b_flags & B_ERROR) == 0)
4928 panic("softdep_deallocate_dependencies: dangling deps");
4929 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
4930 panic("softdep_deallocate_dependencies: unrecovered I/O error");
4934 * Function to handle asynchronous write errors in the filesystem.
4937 softdep_error(func, error)
4942 /* XXX should do something better! */
4943 printf("%s: got error %d while accessing filesystem\n", func, error);