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.6 2003/06/27 01:53:26 dillon Exp $
44 * For now we want the safety net that the DIAGNOSTIC and DEBUG flags provide.
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/systm.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
60 #include <sys/syslog.h>
61 #include <sys/vnode.h>
64 #include <ufs/ufs/dir.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ffs/fs.h>
69 #include <ufs/ffs/softdep.h>
70 #include <ufs/ffs/ffs_extern.h>
71 #include <ufs/ufs/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 __P((char *, int));
146 static void drain_output __P((struct vnode *, int));
147 static int getdirtybuf __P((struct buf **, int));
148 static void clear_remove __P((struct thread *));
149 static void clear_inodedeps __P((struct thread *));
150 static int flush_pagedep_deps __P((struct vnode *, struct mount *,
152 static int flush_inodedep_deps __P((struct fs *, ino_t));
153 static int handle_written_filepage __P((struct pagedep *, struct buf *));
154 static void diradd_inode_written __P((struct diradd *, struct inodedep *));
155 static int handle_written_inodeblock __P((struct inodedep *, struct buf *));
156 static void handle_allocdirect_partdone __P((struct allocdirect *));
157 static void handle_allocindir_partdone __P((struct allocindir *));
158 static void initiate_write_filepage __P((struct pagedep *, struct buf *));
159 static void handle_written_mkdir __P((struct mkdir *, int));
160 static void initiate_write_inodeblock __P((struct inodedep *, struct buf *));
161 static void handle_workitem_freefile __P((struct freefile *));
162 static void handle_workitem_remove __P((struct dirrem *));
163 static struct dirrem *newdirrem __P((struct buf *, struct inode *,
164 struct inode *, int, struct dirrem **));
165 static void free_diradd __P((struct diradd *));
166 static void free_allocindir __P((struct allocindir *, struct inodedep *));
167 static int indir_trunc __P((struct inode *, ufs_daddr_t, int, ufs_lbn_t,
169 static void deallocate_dependencies __P((struct buf *, struct inodedep *));
170 static void free_allocdirect __P((struct allocdirectlst *,
171 struct allocdirect *, int));
172 static int check_inode_unwritten __P((struct inodedep *));
173 static int free_inodedep __P((struct inodedep *));
174 static void handle_workitem_freeblocks __P((struct freeblks *));
175 static void merge_inode_lists __P((struct inodedep *));
176 static void setup_allocindir_phase2 __P((struct buf *, struct inode *,
177 struct allocindir *));
178 static struct allocindir *newallocindir __P((struct inode *, int, ufs_daddr_t,
180 static void handle_workitem_freefrag __P((struct freefrag *));
181 static struct freefrag *newfreefrag __P((struct inode *, ufs_daddr_t, long));
182 static void allocdirect_merge __P((struct allocdirectlst *,
183 struct allocdirect *, struct allocdirect *));
184 static struct bmsafemap *bmsafemap_lookup __P((struct buf *));
185 static int newblk_lookup __P((struct fs *, ufs_daddr_t, int,
187 static int inodedep_lookup __P((struct fs *, ino_t, int, struct inodedep **));
188 static int pagedep_lookup __P((struct inode *, ufs_lbn_t, int,
190 static void pause_timer __P((void *));
191 static int request_cleanup __P((int, int));
192 static int process_worklist_item __P((struct mount *, int));
193 static void add_to_worklist __P((struct worklist *));
196 * Exported softdep operations.
198 static void softdep_disk_io_initiation __P((struct buf *));
199 static void softdep_disk_write_complete __P((struct buf *));
200 static void softdep_deallocate_dependencies __P((struct buf *));
201 static int softdep_fsync __P((struct vnode *));
202 static int softdep_process_worklist __P((struct mount *));
203 static void softdep_move_dependencies __P((struct buf *, struct buf *));
204 static int softdep_count_dependencies __P((struct buf *bp, int));
206 struct bio_ops 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 __P((struct lockit *));
248 static void free_lock __P((struct lockit *));
249 void softdep_panic __P((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 __P((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 __P((struct sema *, char *, int, int));
367 static int sema_get __P((struct sema *, struct lockit *));
368 static void sema_release __P((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 __P((struct workhead *, struct worklist *));
441 static void worklist_remove __P((struct worklist *));
442 static void workitem_free __P((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, PRIBIO, "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, LK_EXCLUSIVE | LK_RETRY, td);
824 error = VOP_FSYNC(devvp, MNT_WAIT, td);
825 VOP_UNLOCK(devvp, 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 file systems.
1077 softdep_initialize()
1080 LIST_INIT(&mkdirlisthd);
1081 LIST_INIT(&softdep_workitem_pending);
1082 max_softdeps = min(desiredvnodes * 8,
1083 M_INODEDEP->ks_limit / (2 * sizeof(struct inodedep)));
1084 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1086 sema_init(&pagedep_in_progress, "pagedep", PRIBIO, 0);
1087 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1088 sema_init(&inodedep_in_progress, "inodedep", PRIBIO, 0);
1089 newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1090 sema_init(&newblk_in_progress, "newblk", PRIBIO, 0);
1094 * Called at mount time to notify the dependency code that a
1095 * filesystem wishes to use it.
1098 softdep_mount(devvp, mp, fs)
1099 struct vnode *devvp;
1103 struct csum cstotal;
1108 mp->mnt_flag &= ~MNT_ASYNC;
1109 mp->mnt_flag |= MNT_SOFTDEP;
1111 * When doing soft updates, the counters in the
1112 * superblock may have gotten out of sync, so we have
1113 * to scan the cylinder groups and recalculate them.
1115 if (fs->fs_clean != 0)
1117 bzero(&cstotal, sizeof cstotal);
1118 for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1119 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1120 fs->fs_cgsize, &bp)) != 0) {
1124 cgp = (struct cg *)bp->b_data;
1125 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1126 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1127 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1128 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1129 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1133 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1134 printf("ffs_mountfs: superblock updated for soft updates\n");
1136 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1141 * Protecting the freemaps (or bitmaps).
1143 * To eliminate the need to execute fsck before mounting a file system
1144 * after a power failure, one must (conservatively) guarantee that the
1145 * on-disk copy of the bitmaps never indicate that a live inode or block is
1146 * free. So, when a block or inode is allocated, the bitmap should be
1147 * updated (on disk) before any new pointers. When a block or inode is
1148 * freed, the bitmap should not be updated until all pointers have been
1149 * reset. The latter dependency is handled by the delayed de-allocation
1150 * approach described below for block and inode de-allocation. The former
1151 * dependency is handled by calling the following procedure when a block or
1152 * inode is allocated. When an inode is allocated an "inodedep" is created
1153 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1154 * Each "inodedep" is also inserted into the hash indexing structure so
1155 * that any additional link additions can be made dependent on the inode
1158 * The ufs file system maintains a number of free block counts (e.g., per
1159 * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1160 * in addition to the bitmaps. These counts are used to improve efficiency
1161 * during allocation and therefore must be consistent with the bitmaps.
1162 * There is no convenient way to guarantee post-crash consistency of these
1163 * counts with simple update ordering, for two main reasons: (1) The counts
1164 * and bitmaps for a single cylinder group block are not in the same disk
1165 * sector. If a disk write is interrupted (e.g., by power failure), one may
1166 * be written and the other not. (2) Some of the counts are located in the
1167 * superblock rather than the cylinder group block. So, we focus our soft
1168 * updates implementation on protecting the bitmaps. When mounting a
1169 * filesystem, we recompute the auxiliary counts from the bitmaps.
1173 * Called just after updating the cylinder group block to allocate an inode.
1176 softdep_setup_inomapdep(bp, ip, newinum)
1177 struct buf *bp; /* buffer for cylgroup block with inode map */
1178 struct inode *ip; /* inode related to allocation */
1179 ino_t newinum; /* new inode number being allocated */
1181 struct inodedep *inodedep;
1182 struct bmsafemap *bmsafemap;
1185 * Create a dependency for the newly allocated inode.
1186 * Panic if it already exists as something is seriously wrong.
1187 * Otherwise add it to the dependency list for the buffer holding
1188 * the cylinder group map from which it was allocated.
1191 if ((inodedep_lookup(ip->i_fs, newinum, DEPALLOC|NODELAY, &inodedep))) {
1193 panic("softdep_setup_inomapdep: found inode");
1195 inodedep->id_buf = bp;
1196 inodedep->id_state &= ~DEPCOMPLETE;
1197 bmsafemap = bmsafemap_lookup(bp);
1198 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1203 * Called just after updating the cylinder group block to
1204 * allocate block or fragment.
1207 softdep_setup_blkmapdep(bp, fs, newblkno)
1208 struct buf *bp; /* buffer for cylgroup block with block map */
1209 struct fs *fs; /* filesystem doing allocation */
1210 ufs_daddr_t newblkno; /* number of newly allocated block */
1212 struct newblk *newblk;
1213 struct bmsafemap *bmsafemap;
1216 * Create a dependency for the newly allocated block.
1217 * Add it to the dependency list for the buffer holding
1218 * the cylinder group map from which it was allocated.
1220 if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1221 panic("softdep_setup_blkmapdep: found block");
1223 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(bp);
1224 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1229 * Find the bmsafemap associated with a cylinder group buffer.
1230 * If none exists, create one. The buffer must be locked when
1231 * this routine is called and this routine must be called with
1232 * splbio interrupts blocked.
1234 static struct bmsafemap *
1235 bmsafemap_lookup(bp)
1238 struct bmsafemap *bmsafemap;
1239 struct worklist *wk;
1242 if (lk.lkt_held == NOHOLDER)
1243 panic("bmsafemap_lookup: lock not held");
1245 LIST_FOREACH(wk, &bp->b_dep, wk_list)
1246 if (wk->wk_type == D_BMSAFEMAP)
1247 return (WK_BMSAFEMAP(wk));
1249 MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1250 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1251 bmsafemap->sm_list.wk_type = D_BMSAFEMAP;
1252 bmsafemap->sm_list.wk_state = 0;
1253 bmsafemap->sm_buf = bp;
1254 LIST_INIT(&bmsafemap->sm_allocdirecthd);
1255 LIST_INIT(&bmsafemap->sm_allocindirhd);
1256 LIST_INIT(&bmsafemap->sm_inodedephd);
1257 LIST_INIT(&bmsafemap->sm_newblkhd);
1259 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1264 * Direct block allocation dependencies.
1266 * When a new block is allocated, the corresponding disk locations must be
1267 * initialized (with zeros or new data) before the on-disk inode points to
1268 * them. Also, the freemap from which the block was allocated must be
1269 * updated (on disk) before the inode's pointer. These two dependencies are
1270 * independent of each other and are needed for all file blocks and indirect
1271 * blocks that are pointed to directly by the inode. Just before the
1272 * "in-core" version of the inode is updated with a newly allocated block
1273 * number, a procedure (below) is called to setup allocation dependency
1274 * structures. These structures are removed when the corresponding
1275 * dependencies are satisfied or when the block allocation becomes obsolete
1276 * (i.e., the file is deleted, the block is de-allocated, or the block is a
1277 * fragment that gets upgraded). All of these cases are handled in
1278 * procedures described later.
1280 * When a file extension causes a fragment to be upgraded, either to a larger
1281 * fragment or to a full block, the on-disk location may change (if the
1282 * previous fragment could not simply be extended). In this case, the old
1283 * fragment must be de-allocated, but not until after the inode's pointer has
1284 * been updated. In most cases, this is handled by later procedures, which
1285 * will construct a "freefrag" structure to be added to the workitem queue
1286 * when the inode update is complete (or obsolete). The main exception to
1287 * this is when an allocation occurs while a pending allocation dependency
1288 * (for the same block pointer) remains. This case is handled in the main
1289 * allocation dependency setup procedure by immediately freeing the
1290 * unreferenced fragments.
1293 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1294 struct inode *ip; /* inode to which block is being added */
1295 ufs_lbn_t lbn; /* block pointer within inode */
1296 ufs_daddr_t newblkno; /* disk block number being added */
1297 ufs_daddr_t oldblkno; /* previous block number, 0 unless frag */
1298 long newsize; /* size of new block */
1299 long oldsize; /* size of new block */
1300 struct buf *bp; /* bp for allocated block */
1302 struct allocdirect *adp, *oldadp;
1303 struct allocdirectlst *adphead;
1304 struct bmsafemap *bmsafemap;
1305 struct inodedep *inodedep;
1306 struct pagedep *pagedep;
1307 struct newblk *newblk;
1309 MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1310 M_ALLOCDIRECT, M_SOFTDEP_FLAGS);
1311 bzero(adp, sizeof(struct allocdirect));
1312 adp->ad_list.wk_type = D_ALLOCDIRECT;
1314 adp->ad_newblkno = newblkno;
1315 adp->ad_oldblkno = oldblkno;
1316 adp->ad_newsize = newsize;
1317 adp->ad_oldsize = oldsize;
1318 adp->ad_state = ATTACHED;
1319 if (newblkno == oldblkno)
1320 adp->ad_freefrag = NULL;
1322 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1324 if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1325 panic("softdep_setup_allocdirect: lost block");
1328 inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1329 adp->ad_inodedep = inodedep;
1331 if (newblk->nb_state == DEPCOMPLETE) {
1332 adp->ad_state |= DEPCOMPLETE;
1335 bmsafemap = newblk->nb_bmsafemap;
1336 adp->ad_buf = bmsafemap->sm_buf;
1337 LIST_REMOVE(newblk, nb_deps);
1338 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1340 LIST_REMOVE(newblk, nb_hash);
1341 FREE(newblk, M_NEWBLK);
1343 WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1344 if (lbn >= NDADDR) {
1345 /* allocating an indirect block */
1346 if (oldblkno != 0) {
1348 panic("softdep_setup_allocdirect: non-zero indir");
1352 * Allocating a direct block.
1354 * If we are allocating a directory block, then we must
1355 * allocate an associated pagedep to track additions and
1358 if ((ip->i_mode & IFMT) == IFDIR &&
1359 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1360 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1363 * The list of allocdirects must be kept in sorted and ascending
1364 * order so that the rollback routines can quickly determine the
1365 * first uncommitted block (the size of the file stored on disk
1366 * ends at the end of the lowest committed fragment, or if there
1367 * are no fragments, at the end of the highest committed block).
1368 * Since files generally grow, the typical case is that the new
1369 * block is to be added at the end of the list. We speed this
1370 * special case by checking against the last allocdirect in the
1371 * list before laboriously traversing the list looking for the
1374 adphead = &inodedep->id_newinoupdt;
1375 oldadp = TAILQ_LAST(adphead, allocdirectlst);
1376 if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1377 /* insert at end of list */
1378 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1379 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1380 allocdirect_merge(adphead, adp, oldadp);
1384 TAILQ_FOREACH(oldadp, adphead, ad_next) {
1385 if (oldadp->ad_lbn >= lbn)
1388 if (oldadp == NULL) {
1390 panic("softdep_setup_allocdirect: lost entry");
1392 /* insert in middle of list */
1393 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1394 if (oldadp->ad_lbn == lbn)
1395 allocdirect_merge(adphead, adp, oldadp);
1400 * Replace an old allocdirect dependency with a newer one.
1401 * This routine must be called with splbio interrupts blocked.
1404 allocdirect_merge(adphead, newadp, oldadp)
1405 struct allocdirectlst *adphead; /* head of list holding allocdirects */
1406 struct allocdirect *newadp; /* allocdirect being added */
1407 struct allocdirect *oldadp; /* existing allocdirect being checked */
1409 struct freefrag *freefrag;
1412 if (lk.lkt_held == NOHOLDER)
1413 panic("allocdirect_merge: lock not held");
1415 if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1416 newadp->ad_oldsize != oldadp->ad_newsize ||
1417 newadp->ad_lbn >= NDADDR) {
1419 panic("allocdirect_check: old %d != new %d || lbn %ld >= %d",
1420 newadp->ad_oldblkno, oldadp->ad_newblkno, newadp->ad_lbn,
1423 newadp->ad_oldblkno = oldadp->ad_oldblkno;
1424 newadp->ad_oldsize = oldadp->ad_oldsize;
1426 * If the old dependency had a fragment to free or had never
1427 * previously had a block allocated, then the new dependency
1428 * can immediately post its freefrag and adopt the old freefrag.
1429 * This action is done by swapping the freefrag dependencies.
1430 * The new dependency gains the old one's freefrag, and the
1431 * old one gets the new one and then immediately puts it on
1432 * the worklist when it is freed by free_allocdirect. It is
1433 * not possible to do this swap when the old dependency had a
1434 * non-zero size but no previous fragment to free. This condition
1435 * arises when the new block is an extension of the old block.
1436 * Here, the first part of the fragment allocated to the new
1437 * dependency is part of the block currently claimed on disk by
1438 * the old dependency, so cannot legitimately be freed until the
1439 * conditions for the new dependency are fulfilled.
1441 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1442 freefrag = newadp->ad_freefrag;
1443 newadp->ad_freefrag = oldadp->ad_freefrag;
1444 oldadp->ad_freefrag = freefrag;
1446 free_allocdirect(adphead, oldadp, 0);
1450 * Allocate a new freefrag structure if needed.
1452 static struct freefrag *
1453 newfreefrag(ip, blkno, size)
1458 struct freefrag *freefrag;
1464 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1465 panic("newfreefrag: frag size");
1466 MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1467 M_FREEFRAG, M_SOFTDEP_FLAGS);
1468 freefrag->ff_list.wk_type = D_FREEFRAG;
1469 freefrag->ff_state = ip->i_uid & ~ONWORKLIST; /* XXX - used below */
1470 freefrag->ff_inum = ip->i_number;
1471 freefrag->ff_fs = fs;
1472 freefrag->ff_devvp = ip->i_devvp;
1473 freefrag->ff_blkno = blkno;
1474 freefrag->ff_fragsize = size;
1479 * This workitem de-allocates fragments that were replaced during
1480 * file block allocation.
1483 handle_workitem_freefrag(freefrag)
1484 struct freefrag *freefrag;
1488 tip.i_fs = freefrag->ff_fs;
1489 tip.i_devvp = freefrag->ff_devvp;
1490 tip.i_dev = freefrag->ff_devvp->v_rdev;
1491 tip.i_number = freefrag->ff_inum;
1492 tip.i_uid = freefrag->ff_state & ~ONWORKLIST; /* XXX - set above */
1493 ffs_blkfree(&tip, freefrag->ff_blkno, freefrag->ff_fragsize);
1494 FREE(freefrag, M_FREEFRAG);
1498 * Indirect block allocation dependencies.
1500 * The same dependencies that exist for a direct block also exist when
1501 * a new block is allocated and pointed to by an entry in a block of
1502 * indirect pointers. The undo/redo states described above are also
1503 * used here. Because an indirect block contains many pointers that
1504 * may have dependencies, a second copy of the entire in-memory indirect
1505 * block is kept. The buffer cache copy is always completely up-to-date.
1506 * The second copy, which is used only as a source for disk writes,
1507 * contains only the safe pointers (i.e., those that have no remaining
1508 * update dependencies). The second copy is freed when all pointers
1509 * are safe. The cache is not allowed to replace indirect blocks with
1510 * pending update dependencies. If a buffer containing an indirect
1511 * block with dependencies is written, these routines will mark it
1512 * dirty again. It can only be successfully written once all the
1513 * dependencies are removed. The ffs_fsync routine in conjunction with
1514 * softdep_sync_metadata work together to get all the dependencies
1515 * removed so that a file can be successfully written to disk. Three
1516 * procedures are used when setting up indirect block pointer
1517 * dependencies. The division is necessary because of the organization
1518 * of the "balloc" routine and because of the distinction between file
1519 * pages and file metadata blocks.
1523 * Allocate a new allocindir structure.
1525 static struct allocindir *
1526 newallocindir(ip, ptrno, newblkno, oldblkno)
1527 struct inode *ip; /* inode for file being extended */
1528 int ptrno; /* offset of pointer in indirect block */
1529 ufs_daddr_t newblkno; /* disk block number being added */
1530 ufs_daddr_t oldblkno; /* previous block number, 0 if none */
1532 struct allocindir *aip;
1534 MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1535 M_ALLOCINDIR, M_SOFTDEP_FLAGS);
1536 bzero(aip, sizeof(struct allocindir));
1537 aip->ai_list.wk_type = D_ALLOCINDIR;
1538 aip->ai_state = ATTACHED;
1539 aip->ai_offset = ptrno;
1540 aip->ai_newblkno = newblkno;
1541 aip->ai_oldblkno = oldblkno;
1542 aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1547 * Called just before setting an indirect block pointer
1548 * to a newly allocated file page.
1551 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
1552 struct inode *ip; /* inode for file being extended */
1553 ufs_lbn_t lbn; /* allocated block number within file */
1554 struct buf *bp; /* buffer with indirect blk referencing page */
1555 int ptrno; /* offset of pointer in indirect block */
1556 ufs_daddr_t newblkno; /* disk block number being added */
1557 ufs_daddr_t oldblkno; /* previous block number, 0 if none */
1558 struct buf *nbp; /* buffer holding allocated page */
1560 struct allocindir *aip;
1561 struct pagedep *pagedep;
1563 aip = newallocindir(ip, ptrno, newblkno, oldblkno);
1566 * If we are allocating a directory page, then we must
1567 * allocate an associated pagedep to track additions and
1570 if ((ip->i_mode & IFMT) == IFDIR &&
1571 pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1572 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
1573 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1575 setup_allocindir_phase2(bp, ip, aip);
1579 * Called just before setting an indirect block pointer to a
1580 * newly allocated indirect block.
1583 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
1584 struct buf *nbp; /* newly allocated indirect block */
1585 struct inode *ip; /* inode for file being extended */
1586 struct buf *bp; /* indirect block referencing allocated block */
1587 int ptrno; /* offset of pointer in indirect block */
1588 ufs_daddr_t newblkno; /* disk block number being added */
1590 struct allocindir *aip;
1592 aip = newallocindir(ip, ptrno, newblkno, 0);
1594 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
1596 setup_allocindir_phase2(bp, ip, aip);
1600 * Called to finish the allocation of the "aip" allocated
1601 * by one of the two routines above.
1604 setup_allocindir_phase2(bp, ip, aip)
1605 struct buf *bp; /* in-memory copy of the indirect block */
1606 struct inode *ip; /* inode for file being extended */
1607 struct allocindir *aip; /* allocindir allocated by the above routines */
1609 struct worklist *wk;
1610 struct indirdep *indirdep, *newindirdep;
1611 struct bmsafemap *bmsafemap;
1612 struct allocindir *oldaip;
1613 struct freefrag *freefrag;
1614 struct newblk *newblk;
1616 if (bp->b_lblkno >= 0)
1617 panic("setup_allocindir_phase2: not indir blk");
1618 for (indirdep = NULL, newindirdep = NULL; ; ) {
1620 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
1621 if (wk->wk_type != D_INDIRDEP)
1623 indirdep = WK_INDIRDEP(wk);
1626 if (indirdep == NULL && newindirdep) {
1627 indirdep = newindirdep;
1628 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
1633 if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
1635 panic("setup_allocindir: lost block");
1637 if (newblk->nb_state == DEPCOMPLETE) {
1638 aip->ai_state |= DEPCOMPLETE;
1641 bmsafemap = newblk->nb_bmsafemap;
1642 aip->ai_buf = bmsafemap->sm_buf;
1643 LIST_REMOVE(newblk, nb_deps);
1644 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
1647 LIST_REMOVE(newblk, nb_hash);
1648 FREE(newblk, M_NEWBLK);
1649 aip->ai_indirdep = indirdep;
1651 * Check to see if there is an existing dependency
1652 * for this block. If there is, merge the old
1653 * dependency into the new one.
1655 if (aip->ai_oldblkno == 0)
1659 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
1660 if (oldaip->ai_offset == aip->ai_offset)
1662 if (oldaip != NULL) {
1663 if (oldaip->ai_newblkno != aip->ai_oldblkno) {
1665 panic("setup_allocindir_phase2: blkno");
1667 aip->ai_oldblkno = oldaip->ai_oldblkno;
1668 freefrag = oldaip->ai_freefrag;
1669 oldaip->ai_freefrag = aip->ai_freefrag;
1670 aip->ai_freefrag = freefrag;
1671 free_allocindir(oldaip, NULL);
1673 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
1674 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)
1675 [aip->ai_offset] = aip->ai_oldblkno;
1679 if (indirdep->ir_savebp != NULL)
1680 brelse(newindirdep->ir_savebp);
1681 WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
1685 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
1686 M_INDIRDEP, M_SOFTDEP_FLAGS);
1687 newindirdep->ir_list.wk_type = D_INDIRDEP;
1688 newindirdep->ir_state = ATTACHED;
1689 LIST_INIT(&newindirdep->ir_deplisthd);
1690 LIST_INIT(&newindirdep->ir_donehd);
1691 if (bp->b_blkno == bp->b_lblkno) {
1692 VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno,
1695 newindirdep->ir_savebp =
1696 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0);
1697 BUF_KERNPROC(newindirdep->ir_savebp);
1698 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
1703 * Block de-allocation dependencies.
1705 * When blocks are de-allocated, the on-disk pointers must be nullified before
1706 * the blocks are made available for use by other files. (The true
1707 * requirement is that old pointers must be nullified before new on-disk
1708 * pointers are set. We chose this slightly more stringent requirement to
1709 * reduce complexity.) Our implementation handles this dependency by updating
1710 * the inode (or indirect block) appropriately but delaying the actual block
1711 * de-allocation (i.e., freemap and free space count manipulation) until
1712 * after the updated versions reach stable storage. After the disk is
1713 * updated, the blocks can be safely de-allocated whenever it is convenient.
1714 * This implementation handles only the common case of reducing a file's
1715 * length to zero. Other cases are handled by the conventional synchronous
1718 * The ffs implementation with which we worked double-checks
1719 * the state of the block pointers and file size as it reduces
1720 * a file's length. Some of this code is replicated here in our
1721 * soft updates implementation. The freeblks->fb_chkcnt field is
1722 * used to transfer a part of this information to the procedure
1723 * that eventually de-allocates the blocks.
1725 * This routine should be called from the routine that shortens
1726 * a file's length, before the inode's size or block pointers
1727 * are modified. It will save the block pointer information for
1728 * later release and zero the inode so that the calling routine
1732 softdep_setup_freeblocks(ip, length)
1733 struct inode *ip; /* The inode whose length is to be reduced */
1734 off_t length; /* The new length for the file */
1736 struct freeblks *freeblks;
1737 struct inodedep *inodedep;
1738 struct allocdirect *adp;
1742 int i, error, delay;
1746 panic("softde_setup_freeblocks: non-zero length");
1747 MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
1748 M_FREEBLKS, M_SOFTDEP_FLAGS);
1749 bzero(freeblks, sizeof(struct freeblks));
1750 freeblks->fb_list.wk_type = D_FREEBLKS;
1751 freeblks->fb_uid = ip->i_uid;
1752 freeblks->fb_previousinum = ip->i_number;
1753 freeblks->fb_devvp = ip->i_devvp;
1754 freeblks->fb_fs = fs;
1755 freeblks->fb_oldsize = ip->i_size;
1756 freeblks->fb_newsize = length;
1757 freeblks->fb_chkcnt = ip->i_blocks;
1758 for (i = 0; i < NDADDR; i++) {
1759 freeblks->fb_dblks[i] = ip->i_db[i];
1762 for (i = 0; i < NIADDR; i++) {
1763 freeblks->fb_iblks[i] = ip->i_ib[i];
1769 * Push the zero'ed inode to to its disk buffer so that we are free
1770 * to delete its dependencies below. Once the dependencies are gone
1771 * the buffer can be safely released.
1773 if ((error = bread(ip->i_devvp,
1774 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
1775 (int)fs->fs_bsize, &bp)) != 0)
1776 softdep_error("softdep_setup_freeblocks", error);
1777 *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number)) =
1780 * Find and eliminate any inode dependencies.
1783 (void) inodedep_lookup(fs, ip->i_number, DEPALLOC, &inodedep);
1784 if ((inodedep->id_state & IOSTARTED) != 0) {
1786 panic("softdep_setup_freeblocks: inode busy");
1789 * Add the freeblks structure to the list of operations that
1790 * must await the zero'ed inode being written to disk. If we
1791 * still have a bitmap dependency (delay == 0), then the inode
1792 * has never been written to disk, so we can process the
1793 * freeblks below once we have deleted the dependencies.
1795 delay = (inodedep->id_state & DEPCOMPLETE);
1797 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
1799 * Because the file length has been truncated to zero, any
1800 * pending block allocation dependency structures associated
1801 * with this inode are obsolete and can simply be de-allocated.
1802 * We must first merge the two dependency lists to get rid of
1803 * any duplicate freefrag structures, then purge the merged list.
1805 merge_inode_lists(inodedep);
1806 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
1807 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
1811 * We must wait for any I/O in progress to finish so that
1812 * all potential buffers on the dirty list will be visible.
1813 * Once they are all there, walk the list and get rid of
1818 drain_output(vp, 1);
1819 while (getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd), MNT_WAIT)) {
1820 bp = TAILQ_FIRST(&vp->v_dirtyblkhd);
1821 (void) inodedep_lookup(fs, ip->i_number, 0, &inodedep);
1822 deallocate_dependencies(bp, inodedep);
1823 bp->b_flags |= B_INVAL | B_NOCACHE;
1828 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) != 0)
1829 (void)free_inodedep(inodedep);
1832 * If the inode has never been written to disk (delay == 0),
1833 * then we can process the freeblks now that we have deleted
1837 handle_workitem_freeblocks(freeblks);
1841 * Reclaim any dependency structures from a buffer that is about to
1842 * be reallocated to a new vnode. The buffer must be locked, thus,
1843 * no I/O completion operations can occur while we are manipulating
1844 * its associated dependencies. The mutex is held so that other I/O's
1845 * associated with related dependencies do not occur.
1848 deallocate_dependencies(bp, inodedep)
1850 struct inodedep *inodedep;
1852 struct worklist *wk;
1853 struct indirdep *indirdep;
1854 struct allocindir *aip;
1855 struct pagedep *pagedep;
1856 struct dirrem *dirrem;
1860 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
1861 switch (wk->wk_type) {
1864 indirdep = WK_INDIRDEP(wk);
1866 * None of the indirect pointers will ever be visible,
1867 * so they can simply be tossed. GOINGAWAY ensures
1868 * that allocated pointers will be saved in the buffer
1869 * cache until they are freed. Note that they will
1870 * only be able to be found by their physical address
1871 * since the inode mapping the logical address will
1872 * be gone. The save buffer used for the safe copy
1873 * was allocated in setup_allocindir_phase2 using
1874 * the physical address so it could be used for this
1875 * purpose. Hence we swap the safe copy with the real
1876 * copy, allowing the safe copy to be freed and holding
1877 * on to the real copy for later use in indir_trunc.
1879 if (indirdep->ir_state & GOINGAWAY) {
1881 panic("deallocate_dependencies: already gone");
1883 indirdep->ir_state |= GOINGAWAY;
1884 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
1885 free_allocindir(aip, inodedep);
1886 if (bp->b_lblkno >= 0 ||
1887 bp->b_blkno != indirdep->ir_savebp->b_lblkno) {
1889 panic("deallocate_dependencies: not indir");
1891 bcopy(bp->b_data, indirdep->ir_savebp->b_data,
1893 WORKLIST_REMOVE(wk);
1894 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
1898 pagedep = WK_PAGEDEP(wk);
1900 * None of the directory additions will ever be
1901 * visible, so they can simply be tossed.
1903 for (i = 0; i < DAHASHSZ; i++)
1905 LIST_FIRST(&pagedep->pd_diraddhd[i])))
1907 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
1910 * Copy any directory remove dependencies to the list
1911 * to be processed after the zero'ed inode is written.
1912 * If the inode has already been written, then they
1913 * can be dumped directly onto the work list.
1915 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
1916 LIST_REMOVE(dirrem, dm_next);
1917 dirrem->dm_dirinum = pagedep->pd_ino;
1918 if (inodedep == NULL ||
1919 (inodedep->id_state & ALLCOMPLETE) ==
1921 add_to_worklist(&dirrem->dm_list);
1923 WORKLIST_INSERT(&inodedep->id_bufwait,
1926 WORKLIST_REMOVE(&pagedep->pd_list);
1927 LIST_REMOVE(pagedep, pd_hash);
1928 WORKITEM_FREE(pagedep, D_PAGEDEP);
1932 free_allocindir(WK_ALLOCINDIR(wk), inodedep);
1938 panic("deallocate_dependencies: Unexpected type %s",
1939 TYPENAME(wk->wk_type));
1944 panic("deallocate_dependencies: Unknown type %s",
1945 TYPENAME(wk->wk_type));
1952 * Free an allocdirect. Generate a new freefrag work request if appropriate.
1953 * This routine must be called with splbio interrupts blocked.
1956 free_allocdirect(adphead, adp, delay)
1957 struct allocdirectlst *adphead;
1958 struct allocdirect *adp;
1963 if (lk.lkt_held == NOHOLDER)
1964 panic("free_allocdirect: lock not held");
1966 if ((adp->ad_state & DEPCOMPLETE) == 0)
1967 LIST_REMOVE(adp, ad_deps);
1968 TAILQ_REMOVE(adphead, adp, ad_next);
1969 if ((adp->ad_state & COMPLETE) == 0)
1970 WORKLIST_REMOVE(&adp->ad_list);
1971 if (adp->ad_freefrag != NULL) {
1973 WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
1974 &adp->ad_freefrag->ff_list);
1976 add_to_worklist(&adp->ad_freefrag->ff_list);
1978 WORKITEM_FREE(adp, D_ALLOCDIRECT);
1982 * Prepare an inode to be freed. The actual free operation is not
1983 * done until the zero'ed inode has been written to disk.
1986 softdep_freefile(pvp, ino, mode)
1991 struct inode *ip = VTOI(pvp);
1992 struct inodedep *inodedep;
1993 struct freefile *freefile;
1996 * This sets up the inode de-allocation dependency.
1998 MALLOC(freefile, struct freefile *, sizeof(struct freefile),
1999 M_FREEFILE, M_SOFTDEP_FLAGS);
2000 freefile->fx_list.wk_type = D_FREEFILE;
2001 freefile->fx_list.wk_state = 0;
2002 freefile->fx_mode = mode;
2003 freefile->fx_oldinum = ino;
2004 freefile->fx_devvp = ip->i_devvp;
2005 freefile->fx_fs = ip->i_fs;
2008 * If the inodedep does not exist, then the zero'ed inode has
2009 * been written to disk. If the allocated inode has never been
2010 * written to disk, then the on-disk inode is zero'ed. In either
2011 * case we can free the file immediately.
2014 if (inodedep_lookup(ip->i_fs, ino, 0, &inodedep) == 0 ||
2015 check_inode_unwritten(inodedep)) {
2017 handle_workitem_freefile(freefile);
2020 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2025 * Check to see if an inode has never been written to disk. If
2026 * so free the inodedep and return success, otherwise return failure.
2027 * This routine must be called with splbio interrupts blocked.
2029 * If we still have a bitmap dependency, then the inode has never
2030 * been written to disk. Drop the dependency as it is no longer
2031 * necessary since the inode is being deallocated. We set the
2032 * ALLCOMPLETE flags since the bitmap now properly shows that the
2033 * inode is not allocated. Even if the inode is actively being
2034 * written, it has been rolled back to its zero'ed state, so we
2035 * are ensured that a zero inode is what is on the disk. For short
2036 * lived files, this change will usually result in removing all the
2037 * dependencies from the inode so that it can be freed immediately.
2040 check_inode_unwritten(inodedep)
2041 struct inodedep *inodedep;
2044 if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2045 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2046 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2047 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2048 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2049 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2050 inodedep->id_nlinkdelta != 0)
2052 inodedep->id_state |= ALLCOMPLETE;
2053 LIST_REMOVE(inodedep, id_deps);
2054 inodedep->id_buf = NULL;
2055 if (inodedep->id_state & ONWORKLIST)
2056 WORKLIST_REMOVE(&inodedep->id_list);
2057 if (inodedep->id_savedino != NULL) {
2058 FREE(inodedep->id_savedino, M_INODEDEP);
2059 inodedep->id_savedino = NULL;
2061 if (free_inodedep(inodedep) == 0) {
2063 panic("check_inode_unwritten: busy inode");
2069 * Try to free an inodedep structure. Return 1 if it could be freed.
2072 free_inodedep(inodedep)
2073 struct inodedep *inodedep;
2076 if ((inodedep->id_state & ONWORKLIST) != 0 ||
2077 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2078 LIST_FIRST(&inodedep->id_pendinghd) != NULL ||
2079 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
2080 LIST_FIRST(&inodedep->id_inowait) != NULL ||
2081 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
2082 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL ||
2083 inodedep->id_nlinkdelta != 0 || inodedep->id_savedino != NULL)
2085 LIST_REMOVE(inodedep, id_hash);
2086 WORKITEM_FREE(inodedep, D_INODEDEP);
2092 * This workitem routine performs the block de-allocation.
2093 * The workitem is added to the pending list after the updated
2094 * inode block has been written to disk. As mentioned above,
2095 * checks regarding the number of blocks de-allocated (compared
2096 * to the number of blocks allocated for the file) are also
2097 * performed in this function.
2100 handle_workitem_freeblocks(freeblks)
2101 struct freeblks *freeblks;
2106 int i, level, bsize;
2107 long nblocks, blocksreleased = 0;
2108 int error, allerror = 0;
2109 ufs_lbn_t baselbns[NIADDR], tmpval;
2111 tip.i_number = freeblks->fb_previousinum;
2112 tip.i_devvp = freeblks->fb_devvp;
2113 tip.i_dev = freeblks->fb_devvp->v_rdev;
2114 tip.i_fs = freeblks->fb_fs;
2115 tip.i_size = freeblks->fb_oldsize;
2116 tip.i_uid = freeblks->fb_uid;
2117 fs = freeblks->fb_fs;
2119 baselbns[0] = NDADDR;
2120 for (i = 1; i < NIADDR; i++) {
2121 tmpval *= NINDIR(fs);
2122 baselbns[i] = baselbns[i - 1] + tmpval;
2124 nblocks = btodb(fs->fs_bsize);
2127 * Indirect blocks first.
2129 for (level = (NIADDR - 1); level >= 0; level--) {
2130 if ((bn = freeblks->fb_iblks[level]) == 0)
2132 if ((error = indir_trunc(&tip, fsbtodb(fs, bn), level,
2133 baselbns[level], &blocksreleased)) == 0)
2135 ffs_blkfree(&tip, bn, fs->fs_bsize);
2136 blocksreleased += nblocks;
2139 * All direct blocks or frags.
2141 for (i = (NDADDR - 1); i >= 0; i--) {
2142 if ((bn = freeblks->fb_dblks[i]) == 0)
2144 bsize = blksize(fs, &tip, i);
2145 ffs_blkfree(&tip, bn, bsize);
2146 blocksreleased += btodb(bsize);
2150 if (freeblks->fb_chkcnt != blocksreleased)
2151 printf("handle_workitem_freeblocks: block count\n");
2153 softdep_error("handle_workitem_freeblks", allerror);
2154 #endif /* DIAGNOSTIC */
2155 WORKITEM_FREE(freeblks, D_FREEBLKS);
2159 * Release blocks associated with the inode ip and stored in the indirect
2160 * block dbn. If level is greater than SINGLE, the block is an indirect block
2161 * and recursive calls to indirtrunc must be used to cleanse other indirect
2165 indir_trunc(ip, dbn, level, lbn, countp)
2176 struct worklist *wk;
2177 struct indirdep *indirdep;
2178 int i, lbnadd, nblocks;
2179 int error, allerror = 0;
2183 for (i = level; i > 0; i--)
2184 lbnadd *= NINDIR(fs);
2186 * Get buffer of block pointers to be freed. This routine is not
2187 * called until the zero'ed inode has been written, so it is safe
2188 * to free blocks as they are encountered. Because the inode has
2189 * been zero'ed, calls to bmap on these blocks will fail. So, we
2190 * have to use the on-disk address and the block device for the
2191 * filesystem to look them up. If the file was deleted before its
2192 * indirect blocks were all written to disk, the routine that set
2193 * us up (deallocate_dependencies) will have arranged to leave
2194 * a complete copy of the indirect block in memory for our use.
2195 * Otherwise we have to read the blocks in from the disk.
2198 if ((bp = incore(ip->i_devvp, dbn)) != NULL &&
2199 (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2200 if (wk->wk_type != D_INDIRDEP ||
2201 (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2202 (indirdep->ir_state & GOINGAWAY) == 0) {
2204 panic("indir_trunc: lost indirdep");
2206 WORKLIST_REMOVE(wk);
2207 WORKITEM_FREE(indirdep, D_INDIRDEP);
2208 if (LIST_FIRST(&bp->b_dep) != NULL) {
2210 panic("indir_trunc: dangling dep");
2215 error = bread(ip->i_devvp, dbn, (int)fs->fs_bsize, &bp);
2220 * Recursively free indirect blocks.
2222 bap = (ufs_daddr_t *)bp->b_data;
2223 nblocks = btodb(fs->fs_bsize);
2224 for (i = NINDIR(fs) - 1; i >= 0; i--) {
2225 if ((nb = bap[i]) == 0)
2228 if ((error = indir_trunc(ip, fsbtodb(fs, nb),
2229 level - 1, lbn + (i * lbnadd), countp)) != 0)
2232 ffs_blkfree(ip, nb, fs->fs_bsize);
2235 bp->b_flags |= B_INVAL | B_NOCACHE;
2241 * Free an allocindir.
2242 * This routine must be called with splbio interrupts blocked.
2245 free_allocindir(aip, inodedep)
2246 struct allocindir *aip;
2247 struct inodedep *inodedep;
2249 struct freefrag *freefrag;
2252 if (lk.lkt_held == NOHOLDER)
2253 panic("free_allocindir: lock not held");
2255 if ((aip->ai_state & DEPCOMPLETE) == 0)
2256 LIST_REMOVE(aip, ai_deps);
2257 if (aip->ai_state & ONWORKLIST)
2258 WORKLIST_REMOVE(&aip->ai_list);
2259 LIST_REMOVE(aip, ai_next);
2260 if ((freefrag = aip->ai_freefrag) != NULL) {
2261 if (inodedep == NULL)
2262 add_to_worklist(&freefrag->ff_list);
2264 WORKLIST_INSERT(&inodedep->id_bufwait,
2265 &freefrag->ff_list);
2267 WORKITEM_FREE(aip, D_ALLOCINDIR);
2271 * Directory entry addition dependencies.
2273 * When adding a new directory entry, the inode (with its incremented link
2274 * count) must be written to disk before the directory entry's pointer to it.
2275 * Also, if the inode is newly allocated, the corresponding freemap must be
2276 * updated (on disk) before the directory entry's pointer. These requirements
2277 * are met via undo/redo on the directory entry's pointer, which consists
2278 * simply of the inode number.
2280 * As directory entries are added and deleted, the free space within a
2281 * directory block can become fragmented. The ufs file system will compact
2282 * a fragmented directory block to make space for a new entry. When this
2283 * occurs, the offsets of previously added entries change. Any "diradd"
2284 * dependency structures corresponding to these entries must be updated with
2289 * This routine is called after the in-memory inode's link
2290 * count has been incremented, but before the directory entry's
2291 * pointer to the inode has been set.
2294 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp)
2295 struct buf *bp; /* buffer containing directory block */
2296 struct inode *dp; /* inode for directory */
2297 off_t diroffset; /* offset of new entry in directory */
2298 long newinum; /* inode referenced by new directory entry */
2299 struct buf *newdirbp; /* non-NULL => contents of new mkdir */
2301 int offset; /* offset of new entry within directory block */
2302 ufs_lbn_t lbn; /* block in directory containing new entry */
2305 struct pagedep *pagedep;
2306 struct inodedep *inodedep;
2307 struct mkdir *mkdir1, *mkdir2;
2310 * Whiteouts have no dependencies.
2312 if (newinum == WINO) {
2313 if (newdirbp != NULL)
2319 lbn = lblkno(fs, diroffset);
2320 offset = blkoff(fs, diroffset);
2321 MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2323 bzero(dap, sizeof(struct diradd));
2324 dap->da_list.wk_type = D_DIRADD;
2325 dap->da_offset = offset;
2326 dap->da_newinum = newinum;
2327 dap->da_state = ATTACHED;
2328 if (newdirbp == NULL) {
2329 dap->da_state |= DEPCOMPLETE;
2332 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
2333 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2335 mkdir1->md_list.wk_type = D_MKDIR;
2336 mkdir1->md_state = MKDIR_BODY;
2337 mkdir1->md_diradd = dap;
2338 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
2340 mkdir2->md_list.wk_type = D_MKDIR;
2341 mkdir2->md_state = MKDIR_PARENT;
2342 mkdir2->md_diradd = dap;
2344 * Dependency on "." and ".." being written to disk.
2346 mkdir1->md_buf = newdirbp;
2348 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
2349 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
2353 * Dependency on link count increase for parent directory
2356 if (inodedep_lookup(dp->i_fs, dp->i_number, 0, &inodedep) == 0
2357 || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2358 dap->da_state &= ~MKDIR_PARENT;
2359 WORKITEM_FREE(mkdir2, D_MKDIR);
2361 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
2362 WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
2366 * Link into parent directory pagedep to await its being written.
2368 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2369 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2370 dap->da_pagedep = pagedep;
2371 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
2374 * Link into its inodedep. Put it on the id_bufwait list if the inode
2375 * is not yet written. If it is written, do the post-inode write
2376 * processing to put it on the id_pendinghd list.
2378 (void) inodedep_lookup(fs, newinum, DEPALLOC, &inodedep);
2379 if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
2380 diradd_inode_written(dap, inodedep);
2382 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2387 * This procedure is called to change the offset of a directory
2388 * entry when compacting a directory block which must be owned
2389 * exclusively by the caller. Note that the actual entry movement
2390 * must be done in this procedure to ensure that no I/O completions
2391 * occur while the move is in progress.
2394 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
2395 struct inode *dp; /* inode for directory */
2396 caddr_t base; /* address of dp->i_offset */
2397 caddr_t oldloc; /* address of old directory location */
2398 caddr_t newloc; /* address of new directory location */
2399 int entrysize; /* size of directory entry */
2401 int offset, oldoffset, newoffset;
2402 struct pagedep *pagedep;
2407 lbn = lblkno(dp->i_fs, dp->i_offset);
2408 offset = blkoff(dp->i_fs, dp->i_offset);
2409 if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
2411 oldoffset = offset + (oldloc - base);
2412 newoffset = offset + (newloc - base);
2414 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
2415 if (dap->da_offset != oldoffset)
2417 dap->da_offset = newoffset;
2418 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
2420 LIST_REMOVE(dap, da_pdlist);
2421 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
2427 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
2428 if (dap->da_offset == oldoffset) {
2429 dap->da_offset = newoffset;
2435 bcopy(oldloc, newloc, entrysize);
2440 * Free a diradd dependency structure. This routine must be called
2441 * with splbio interrupts blocked.
2447 struct dirrem *dirrem;
2448 struct pagedep *pagedep;
2449 struct inodedep *inodedep;
2450 struct mkdir *mkdir, *nextmd;
2453 if (lk.lkt_held == NOHOLDER)
2454 panic("free_diradd: lock not held");
2456 WORKLIST_REMOVE(&dap->da_list);
2457 LIST_REMOVE(dap, da_pdlist);
2458 if ((dap->da_state & DIRCHG) == 0) {
2459 pagedep = dap->da_pagedep;
2461 dirrem = dap->da_previous;
2462 pagedep = dirrem->dm_pagedep;
2463 dirrem->dm_dirinum = pagedep->pd_ino;
2464 add_to_worklist(&dirrem->dm_list);
2466 if (inodedep_lookup(VFSTOUFS(pagedep->pd_mnt)->um_fs, dap->da_newinum,
2468 (void) free_inodedep(inodedep);
2469 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2470 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
2471 nextmd = LIST_NEXT(mkdir, md_mkdirs);
2472 if (mkdir->md_diradd != dap)
2474 dap->da_state &= ~mkdir->md_state;
2475 WORKLIST_REMOVE(&mkdir->md_list);
2476 LIST_REMOVE(mkdir, md_mkdirs);
2477 WORKITEM_FREE(mkdir, D_MKDIR);
2479 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
2481 panic("free_diradd: unfound ref");
2484 WORKITEM_FREE(dap, D_DIRADD);
2488 * Directory entry removal dependencies.
2490 * When removing a directory entry, the entry's inode pointer must be
2491 * zero'ed on disk before the corresponding inode's link count is decremented
2492 * (possibly freeing the inode for re-use). This dependency is handled by
2493 * updating the directory entry but delaying the inode count reduction until
2494 * after the directory block has been written to disk. After this point, the
2495 * inode count can be decremented whenever it is convenient.
2499 * This routine should be called immediately after removing
2500 * a directory entry. The inode's link count should not be
2501 * decremented by the calling procedure -- the soft updates
2502 * code will do this task when it is safe.
2505 softdep_setup_remove(bp, dp, ip, isrmdir)
2506 struct buf *bp; /* buffer containing directory block */
2507 struct inode *dp; /* inode for the directory being modified */
2508 struct inode *ip; /* inode for directory entry being removed */
2509 int isrmdir; /* indicates if doing RMDIR */
2511 struct dirrem *dirrem, *prevdirrem;
2514 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
2516 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2519 * If the COMPLETE flag is clear, then there were no active
2520 * entries and we want to roll back to a zeroed entry until
2521 * the new inode is committed to disk. If the COMPLETE flag is
2522 * set then we have deleted an entry that never made it to
2523 * disk. If the entry we deleted resulted from a name change,
2524 * then the old name still resides on disk. We cannot delete
2525 * its inode (returned to us in prevdirrem) until the zeroed
2526 * directory entry gets to disk. The new inode has never been
2527 * referenced on the disk, so can be deleted immediately.
2529 if ((dirrem->dm_state & COMPLETE) == 0) {
2530 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
2534 if (prevdirrem != NULL)
2535 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
2536 prevdirrem, dm_next);
2537 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
2539 handle_workitem_remove(dirrem);
2544 * Allocate a new dirrem if appropriate and return it along with
2545 * its associated pagedep. Called without a lock, returns with lock.
2547 static long num_dirrem; /* number of dirrem allocated */
2548 static struct dirrem *
2549 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
2550 struct buf *bp; /* buffer containing directory block */
2551 struct inode *dp; /* inode for the directory being modified */
2552 struct inode *ip; /* inode for directory entry being removed */
2553 int isrmdir; /* indicates if doing RMDIR */
2554 struct dirrem **prevdirremp; /* previously referenced inode, if any */
2559 struct dirrem *dirrem;
2560 struct pagedep *pagedep;
2563 * Whiteouts have no deletion dependencies.
2566 panic("newdirrem: whiteout");
2568 * If we are over our limit, try to improve the situation.
2569 * Limiting the number of dirrem structures will also limit
2570 * the number of freefile and freeblks structures.
2572 if (num_dirrem > max_softdeps / 2 && speedup_syncer() == 0)
2573 (void) request_cleanup(FLUSH_REMOVE, 0);
2575 MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
2576 M_DIRREM, M_SOFTDEP_FLAGS);
2577 bzero(dirrem, sizeof(struct dirrem));
2578 dirrem->dm_list.wk_type = D_DIRREM;
2579 dirrem->dm_state = isrmdir ? RMDIR : 0;
2580 dirrem->dm_mnt = ITOV(ip)->v_mount;
2581 dirrem->dm_oldinum = ip->i_number;
2582 *prevdirremp = NULL;
2585 lbn = lblkno(dp->i_fs, dp->i_offset);
2586 offset = blkoff(dp->i_fs, dp->i_offset);
2587 if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
2588 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
2589 dirrem->dm_pagedep = pagedep;
2591 * Check for a diradd dependency for the same directory entry.
2592 * If present, then both dependencies become obsolete and can
2593 * be de-allocated. Check for an entry on both the pd_dirraddhd
2594 * list and the pd_pendinghd list.
2597 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
2598 if (dap->da_offset == offset)
2602 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
2603 if (dap->da_offset == offset)
2609 * Must be ATTACHED at this point.
2611 if ((dap->da_state & ATTACHED) == 0) {
2613 panic("newdirrem: not ATTACHED");
2615 if (dap->da_newinum != ip->i_number) {
2617 panic("newdirrem: inum %d should be %d",
2618 ip->i_number, dap->da_newinum);
2621 * If we are deleting a changed name that never made it to disk,
2622 * then return the dirrem describing the previous inode (which
2623 * represents the inode currently referenced from this entry on disk).
2625 if ((dap->da_state & DIRCHG) != 0) {
2626 *prevdirremp = dap->da_previous;
2627 dap->da_state &= ~DIRCHG;
2628 dap->da_pagedep = pagedep;
2631 * We are deleting an entry that never made it to disk.
2632 * Mark it COMPLETE so we can delete its inode immediately.
2634 dirrem->dm_state |= COMPLETE;
2640 * Directory entry change dependencies.
2642 * Changing an existing directory entry requires that an add operation
2643 * be completed first followed by a deletion. The semantics for the addition
2644 * are identical to the description of adding a new entry above except
2645 * that the rollback is to the old inode number rather than zero. Once
2646 * the addition dependency is completed, the removal is done as described
2647 * in the removal routine above.
2651 * This routine should be called immediately after changing
2652 * a directory entry. The inode's link count should not be
2653 * decremented by the calling procedure -- the soft updates
2654 * code will perform this task when it is safe.
2657 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
2658 struct buf *bp; /* buffer containing directory block */
2659 struct inode *dp; /* inode for the directory being modified */
2660 struct inode *ip; /* inode for directory entry being removed */
2661 long newinum; /* new inode number for changed entry */
2662 int isrmdir; /* indicates if doing RMDIR */
2665 struct diradd *dap = NULL;
2666 struct dirrem *dirrem, *prevdirrem;
2667 struct pagedep *pagedep;
2668 struct inodedep *inodedep;
2670 offset = blkoff(dp->i_fs, dp->i_offset);
2673 * Whiteouts do not need diradd dependencies.
2675 if (newinum != WINO) {
2676 MALLOC(dap, struct diradd *, sizeof(struct diradd),
2677 M_DIRADD, M_SOFTDEP_FLAGS);
2678 bzero(dap, sizeof(struct diradd));
2679 dap->da_list.wk_type = D_DIRADD;
2680 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
2681 dap->da_offset = offset;
2682 dap->da_newinum = newinum;
2686 * Allocate a new dirrem and ACQUIRE_LOCK.
2688 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
2689 pagedep = dirrem->dm_pagedep;
2691 * The possible values for isrmdir:
2692 * 0 - non-directory file rename
2693 * 1 - directory rename within same directory
2694 * inum - directory rename to new directory of given inode number
2695 * When renaming to a new directory, we are both deleting and
2696 * creating a new directory entry, so the link count on the new
2697 * directory should not change. Thus we do not need the followup
2698 * dirrem which is usually done in handle_workitem_remove. We set
2699 * the DIRCHG flag to tell handle_workitem_remove to skip the
2703 dirrem->dm_state |= DIRCHG;
2706 * Whiteouts have no additional dependencies,
2707 * so just put the dirrem on the correct list.
2709 if (newinum == WINO) {
2710 if ((dirrem->dm_state & COMPLETE) == 0) {
2711 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
2714 dirrem->dm_dirinum = pagedep->pd_ino;
2715 add_to_worklist(&dirrem->dm_list);
2722 * If the COMPLETE flag is clear, then there were no active
2723 * entries and we want to roll back to the previous inode until
2724 * the new inode is committed to disk. If the COMPLETE flag is
2725 * set, then we have deleted an entry that never made it to disk.
2726 * If the entry we deleted resulted from a name change, then the old
2727 * inode reference still resides on disk. Any rollback that we do
2728 * needs to be to that old inode (returned to us in prevdirrem). If
2729 * the entry we deleted resulted from a create, then there is
2730 * no entry on the disk, so we want to roll back to zero rather
2731 * than the uncommitted inode. In either of the COMPLETE cases we
2732 * want to immediately free the unwritten and unreferenced inode.
2734 if ((dirrem->dm_state & COMPLETE) == 0) {
2735 dap->da_previous = dirrem;
2737 if (prevdirrem != NULL) {
2738 dap->da_previous = prevdirrem;
2740 dap->da_state &= ~DIRCHG;
2741 dap->da_pagedep = pagedep;
2743 dirrem->dm_dirinum = pagedep->pd_ino;
2744 add_to_worklist(&dirrem->dm_list);
2747 * Link into its inodedep. Put it on the id_bufwait list if the inode
2748 * is not yet written. If it is written, do the post-inode write
2749 * processing to put it on the id_pendinghd list.
2751 if (inodedep_lookup(dp->i_fs, newinum, DEPALLOC, &inodedep) == 0 ||
2752 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
2753 dap->da_state |= COMPLETE;
2754 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
2755 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
2757 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
2759 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
2765 * Called whenever the link count on an inode is changed.
2766 * It creates an inode dependency so that the new reference(s)
2767 * to the inode cannot be committed to disk until the updated
2768 * inode has been written.
2771 softdep_change_linkcnt(ip)
2772 struct inode *ip; /* the inode with the increased link count */
2774 struct inodedep *inodedep;
2777 (void) inodedep_lookup(ip->i_fs, ip->i_number, DEPALLOC, &inodedep);
2778 if (ip->i_nlink < ip->i_effnlink) {
2780 panic("softdep_change_linkcnt: bad delta");
2782 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2787 * This workitem decrements the inode's link count.
2788 * If the link count reaches zero, the file is removed.
2791 handle_workitem_remove(dirrem)
2792 struct dirrem *dirrem;
2794 struct thread *td = curthread; /* XXX */
2795 struct inodedep *inodedep;
2801 if ((error = VFS_VGET(dirrem->dm_mnt, dirrem->dm_oldinum, &vp)) != 0) {
2802 softdep_error("handle_workitem_remove: vget", error);
2807 if ((inodedep_lookup(ip->i_fs, dirrem->dm_oldinum, 0, &inodedep)) == 0){
2809 panic("handle_workitem_remove: lost inodedep");
2812 * Normal file deletion.
2814 if ((dirrem->dm_state & RMDIR) == 0) {
2816 ip->i_flag |= IN_CHANGE;
2817 if (ip->i_nlink < ip->i_effnlink) {
2819 panic("handle_workitem_remove: bad file delta");
2821 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2825 WORKITEM_FREE(dirrem, D_DIRREM);
2829 * Directory deletion. Decrement reference count for both the
2830 * just deleted parent directory entry and the reference for ".".
2831 * Next truncate the directory to length zero. When the
2832 * truncation completes, arrange to have the reference count on
2833 * the parent decremented to account for the loss of "..".
2836 ip->i_flag |= IN_CHANGE;
2837 if (ip->i_nlink < ip->i_effnlink) {
2839 panic("handle_workitem_remove: bad dir delta");
2841 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
2843 if ((error = UFS_TRUNCATE(vp, (off_t)0, 0, proc0.p_ucred, td)) != 0)
2844 softdep_error("handle_workitem_remove: truncate", error);
2846 * Rename a directory to a new parent. Since, we are both deleting
2847 * and creating a new directory entry, the link count on the new
2848 * directory should not change. Thus we skip the followup dirrem.
2850 if (dirrem->dm_state & DIRCHG) {
2853 WORKITEM_FREE(dirrem, D_DIRREM);
2857 * If the inodedep does not exist, then the zero'ed inode has
2858 * been written to disk. If the allocated inode has never been
2859 * written to disk, then the on-disk inode is zero'ed. In either
2860 * case we can remove the file immediately.
2863 dirrem->dm_state = 0;
2864 oldinum = dirrem->dm_oldinum;
2865 dirrem->dm_oldinum = dirrem->dm_dirinum;
2866 if (inodedep_lookup(ip->i_fs, oldinum, 0, &inodedep) == 0 ||
2867 check_inode_unwritten(inodedep)) {
2870 handle_workitem_remove(dirrem);
2873 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
2879 * Inode de-allocation dependencies.
2881 * When an inode's link count is reduced to zero, it can be de-allocated. We
2882 * found it convenient to postpone de-allocation until after the inode is
2883 * written to disk with its new link count (zero). At this point, all of the
2884 * on-disk inode's block pointers are nullified and, with careful dependency
2885 * list ordering, all dependencies related to the inode will be satisfied and
2886 * the corresponding dependency structures de-allocated. So, if/when the
2887 * inode is reused, there will be no mixing of old dependencies with new
2888 * ones. This artificial dependency is set up by the block de-allocation
2889 * procedure above (softdep_setup_freeblocks) and completed by the
2890 * following procedure.
2893 handle_workitem_freefile(freefile)
2894 struct freefile *freefile;
2898 struct inodedep *idp;
2903 error = inodedep_lookup(freefile->fx_fs, freefile->fx_oldinum, 0, &idp);
2906 panic("handle_workitem_freefile: inodedep survived");
2908 tip.i_devvp = freefile->fx_devvp;
2909 tip.i_dev = freefile->fx_devvp->v_rdev;
2910 tip.i_fs = freefile->fx_fs;
2912 if ((error = ffs_freefile(&vp, freefile->fx_oldinum, freefile->fx_mode)) != 0)
2913 softdep_error("handle_workitem_freefile", error);
2914 WORKITEM_FREE(freefile, D_FREEFILE);
2920 * The dependency structures constructed above are most actively used when file
2921 * system blocks are written to disk. No constraints are placed on when a
2922 * block can be written, but unsatisfied update dependencies are made safe by
2923 * modifying (or replacing) the source memory for the duration of the disk
2924 * write. When the disk write completes, the memory block is again brought
2927 * In-core inode structure reclamation.
2929 * Because there are a finite number of "in-core" inode structures, they are
2930 * reused regularly. By transferring all inode-related dependencies to the
2931 * in-memory inode block and indexing them separately (via "inodedep"s), we
2932 * can allow "in-core" inode structures to be reused at any time and avoid
2933 * any increase in contention.
2935 * Called just before entering the device driver to initiate a new disk I/O.
2936 * The buffer must be locked, thus, no I/O completion operations can occur
2937 * while we are manipulating its associated dependencies.
2940 softdep_disk_io_initiation(bp)
2941 struct buf *bp; /* structure describing disk write to occur */
2943 struct worklist *wk, *nextwk;
2944 struct indirdep *indirdep;
2947 * We only care about write operations. There should never
2948 * be dependencies for reads.
2950 if (bp->b_flags & B_READ)
2951 panic("softdep_disk_io_initiation: read");
2953 * Do any necessary pre-I/O processing.
2955 for (wk = LIST_FIRST(&bp->b_dep); wk; wk = nextwk) {
2956 nextwk = LIST_NEXT(wk, wk_list);
2957 switch (wk->wk_type) {
2960 initiate_write_filepage(WK_PAGEDEP(wk), bp);
2964 initiate_write_inodeblock(WK_INODEDEP(wk), bp);
2968 indirdep = WK_INDIRDEP(wk);
2969 if (indirdep->ir_state & GOINGAWAY)
2970 panic("disk_io_initiation: indirdep gone");
2972 * If there are no remaining dependencies, this
2973 * will be writing the real pointers, so the
2974 * dependency can be freed.
2976 if (LIST_FIRST(&indirdep->ir_deplisthd) == NULL) {
2977 indirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
2978 brelse(indirdep->ir_savebp);
2979 /* inline expand WORKLIST_REMOVE(wk); */
2980 wk->wk_state &= ~ONWORKLIST;
2981 LIST_REMOVE(wk, wk_list);
2982 WORKITEM_FREE(indirdep, D_INDIRDEP);
2986 * Replace up-to-date version with safe version.
2988 MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
2989 M_INDIRDEP, M_SOFTDEP_FLAGS);
2991 indirdep->ir_state &= ~ATTACHED;
2992 indirdep->ir_state |= UNDONE;
2993 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
2994 bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3006 panic("handle_disk_io_initiation: Unexpected type %s",
3007 TYPENAME(wk->wk_type));
3014 * Called from within the procedure above to deal with unsatisfied
3015 * allocation dependencies in a directory. The buffer must be locked,
3016 * thus, no I/O completion operations can occur while we are
3017 * manipulating its associated dependencies.
3020 initiate_write_filepage(pagedep, bp)
3021 struct pagedep *pagedep;
3028 if (pagedep->pd_state & IOSTARTED) {
3030 * This can only happen if there is a driver that does not
3031 * understand chaining. Here biodone will reissue the call
3032 * to strategy for the incomplete buffers.
3034 printf("initiate_write_filepage: already started\n");
3037 pagedep->pd_state |= IOSTARTED;
3039 for (i = 0; i < DAHASHSZ; i++) {
3040 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3041 ep = (struct direct *)
3042 ((char *)bp->b_data + dap->da_offset);
3043 if (ep->d_ino != dap->da_newinum) {
3045 panic("%s: dir inum %d != new %d",
3046 "initiate_write_filepage",
3047 ep->d_ino, dap->da_newinum);
3049 if (dap->da_state & DIRCHG)
3050 ep->d_ino = dap->da_previous->dm_oldinum;
3053 dap->da_state &= ~ATTACHED;
3054 dap->da_state |= UNDONE;
3061 * Called from within the procedure above to deal with unsatisfied
3062 * allocation dependencies in an inodeblock. The buffer must be
3063 * locked, thus, no I/O completion operations can occur while we
3064 * are manipulating its associated dependencies.
3067 initiate_write_inodeblock(inodedep, bp)
3068 struct inodedep *inodedep;
3069 struct buf *bp; /* The inode block */
3071 struct allocdirect *adp, *lastadp;
3074 ufs_lbn_t prevlbn = 0;
3077 if (inodedep->id_state & IOSTARTED)
3078 panic("initiate_write_inodeblock: already started");
3079 inodedep->id_state |= IOSTARTED;
3080 fs = inodedep->id_fs;
3081 dp = (struct dinode *)bp->b_data +
3082 ino_to_fsbo(fs, inodedep->id_ino);
3084 * If the bitmap is not yet written, then the allocated
3085 * inode cannot be written to disk.
3087 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3088 if (inodedep->id_savedino != NULL)
3089 panic("initiate_write_inodeblock: already doing I/O");
3090 MALLOC(inodedep->id_savedino, struct dinode *,
3091 sizeof(struct dinode), M_INODEDEP, M_SOFTDEP_FLAGS);
3092 *inodedep->id_savedino = *dp;
3093 bzero((caddr_t)dp, sizeof(struct dinode));
3097 * If no dependencies, then there is nothing to roll back.
3099 inodedep->id_savedsize = dp->di_size;
3100 if (TAILQ_FIRST(&inodedep->id_inoupdt) == NULL)
3103 * Set the dependencies to busy.
3106 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3107 adp = TAILQ_NEXT(adp, ad_next)) {
3109 if (deplist != 0 && prevlbn >= adp->ad_lbn) {
3111 panic("softdep_write_inodeblock: lbn order");
3113 prevlbn = adp->ad_lbn;
3114 if (adp->ad_lbn < NDADDR &&
3115 dp->di_db[adp->ad_lbn] != adp->ad_newblkno) {
3117 panic("%s: direct pointer #%ld mismatch %d != %d",
3118 "softdep_write_inodeblock", adp->ad_lbn,
3119 dp->di_db[adp->ad_lbn], adp->ad_newblkno);
3121 if (adp->ad_lbn >= NDADDR &&
3122 dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno) {
3124 panic("%s: indirect pointer #%ld mismatch %d != %d",
3125 "softdep_write_inodeblock", adp->ad_lbn - NDADDR,
3126 dp->di_ib[adp->ad_lbn - NDADDR], adp->ad_newblkno);
3128 deplist |= 1 << adp->ad_lbn;
3129 if ((adp->ad_state & ATTACHED) == 0) {
3131 panic("softdep_write_inodeblock: Unknown state 0x%x",
3134 #endif /* DIAGNOSTIC */
3135 adp->ad_state &= ~ATTACHED;
3136 adp->ad_state |= UNDONE;
3139 * The on-disk inode cannot claim to be any larger than the last
3140 * fragment that has been written. Otherwise, the on-disk inode
3141 * might have fragments that were not the last block in the file
3142 * which would corrupt the filesystem.
3144 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3145 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3146 if (adp->ad_lbn >= NDADDR)
3148 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3149 /* keep going until hitting a rollback to a frag */
3150 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3152 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3153 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3155 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) {
3157 panic("softdep_write_inodeblock: lost dep1");
3159 #endif /* DIAGNOSTIC */
3162 for (i = 0; i < NIADDR; i++) {
3164 if (dp->di_ib[i] != 0 &&
3165 (deplist & ((1 << NDADDR) << i)) == 0) {
3167 panic("softdep_write_inodeblock: lost dep2");
3169 #endif /* DIAGNOSTIC */
3176 * If we have zero'ed out the last allocated block of the file,
3177 * roll back the size to the last currently allocated block.
3178 * We know that this last allocated block is a full-sized as
3179 * we already checked for fragments in the loop above.
3181 if (lastadp != NULL &&
3182 dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3183 for (i = lastadp->ad_lbn; i >= 0; i--)
3184 if (dp->di_db[i] != 0)
3186 dp->di_size = (i + 1) * fs->fs_bsize;
3189 * The only dependencies are for indirect blocks.
3191 * The file size for indirect block additions is not guaranteed.
3192 * Such a guarantee would be non-trivial to achieve. The conventional
3193 * synchronous write implementation also does not make this guarantee.
3194 * Fsck should catch and fix discrepancies. Arguably, the file size
3195 * can be over-estimated without destroying integrity when the file
3196 * moves into the indirect blocks (i.e., is large). If we want to
3197 * postpone fsck, we are stuck with this argument.
3199 for (; adp; adp = TAILQ_NEXT(adp, ad_next))
3200 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
3205 * This routine is called during the completion interrupt
3206 * service routine for a disk write (from the procedure called
3207 * by the device driver to inform the file system caches of
3208 * a request completion). It should be called early in this
3209 * procedure, before the block is made available to other
3210 * processes or other routines are called.
3213 softdep_disk_write_complete(bp)
3214 struct buf *bp; /* describes the completed disk write */
3216 struct worklist *wk;
3217 struct workhead reattach;
3218 struct newblk *newblk;
3219 struct allocindir *aip;
3220 struct allocdirect *adp;
3221 struct indirdep *indirdep;
3222 struct inodedep *inodedep;
3223 struct bmsafemap *bmsafemap;
3226 if (lk.lkt_held != NOHOLDER)
3227 panic("softdep_disk_write_complete: lock is held");
3228 lk.lkt_held = SPECIAL_FLAG;
3230 LIST_INIT(&reattach);
3231 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
3232 WORKLIST_REMOVE(wk);
3233 switch (wk->wk_type) {
3236 if (handle_written_filepage(WK_PAGEDEP(wk), bp))
3237 WORKLIST_INSERT(&reattach, wk);
3241 if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
3242 WORKLIST_INSERT(&reattach, wk);
3246 bmsafemap = WK_BMSAFEMAP(wk);
3247 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
3248 newblk->nb_state |= DEPCOMPLETE;
3249 newblk->nb_bmsafemap = NULL;
3250 LIST_REMOVE(newblk, nb_deps);
3253 LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
3254 adp->ad_state |= DEPCOMPLETE;
3256 LIST_REMOVE(adp, ad_deps);
3257 handle_allocdirect_partdone(adp);
3260 LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
3261 aip->ai_state |= DEPCOMPLETE;
3263 LIST_REMOVE(aip, ai_deps);
3264 handle_allocindir_partdone(aip);
3267 LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
3268 inodedep->id_state |= DEPCOMPLETE;
3269 LIST_REMOVE(inodedep, id_deps);
3270 inodedep->id_buf = NULL;
3272 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
3276 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
3280 adp = WK_ALLOCDIRECT(wk);
3281 adp->ad_state |= COMPLETE;
3282 handle_allocdirect_partdone(adp);
3286 aip = WK_ALLOCINDIR(wk);
3287 aip->ai_state |= COMPLETE;
3288 handle_allocindir_partdone(aip);
3292 indirdep = WK_INDIRDEP(wk);
3293 if (indirdep->ir_state & GOINGAWAY) {
3294 lk.lkt_held = NOHOLDER;
3295 panic("disk_write_complete: indirdep gone");
3297 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
3298 FREE(indirdep->ir_saveddata, M_INDIRDEP);
3299 indirdep->ir_saveddata = 0;
3300 indirdep->ir_state &= ~UNDONE;
3301 indirdep->ir_state |= ATTACHED;
3302 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
3303 handle_allocindir_partdone(aip);
3304 if (aip == LIST_FIRST(&indirdep->ir_donehd)) {
3305 lk.lkt_held = NOHOLDER;
3306 panic("disk_write_complete: not gone");
3309 WORKLIST_INSERT(&reattach, wk);
3310 if ((bp->b_flags & B_DELWRI) == 0)
3311 stat_indir_blk_ptrs++;
3316 lk.lkt_held = NOHOLDER;
3317 panic("handle_disk_write_complete: Unknown type %s",
3318 TYPENAME(wk->wk_type));
3323 * Reattach any requests that must be redone.
3325 while ((wk = LIST_FIRST(&reattach)) != NULL) {
3326 WORKLIST_REMOVE(wk);
3327 WORKLIST_INSERT(&bp->b_dep, wk);
3330 if (lk.lkt_held != SPECIAL_FLAG)
3331 panic("softdep_disk_write_complete: lock lost");
3332 lk.lkt_held = NOHOLDER;
3337 * Called from within softdep_disk_write_complete above. Note that
3338 * this routine is always called from interrupt level with further
3339 * splbio interrupts blocked.
3342 handle_allocdirect_partdone(adp)
3343 struct allocdirect *adp; /* the completed allocdirect */
3345 struct allocdirect *listadp;
3346 struct inodedep *inodedep;
3349 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3351 if (adp->ad_buf != NULL) {
3352 lk.lkt_held = NOHOLDER;
3353 panic("handle_allocdirect_partdone: dangling dep");
3356 * The on-disk inode cannot claim to be any larger than the last
3357 * fragment that has been written. Otherwise, the on-disk inode
3358 * might have fragments that were not the last block in the file
3359 * which would corrupt the filesystem. Thus, we cannot free any
3360 * allocdirects after one whose ad_oldblkno claims a fragment as
3361 * these blocks must be rolled back to zero before writing the inode.
3362 * We check the currently active set of allocdirects in id_inoupdt.
3364 inodedep = adp->ad_inodedep;
3365 bsize = inodedep->id_fs->fs_bsize;
3366 TAILQ_FOREACH(listadp, &inodedep->id_inoupdt, ad_next) {
3367 /* found our block */
3370 /* continue if ad_oldlbn is not a fragment */
3371 if (listadp->ad_oldsize == 0 ||
3372 listadp->ad_oldsize == bsize)
3374 /* hit a fragment */
3378 * If we have reached the end of the current list without
3379 * finding the just finished dependency, then it must be
3380 * on the future dependency list. Future dependencies cannot
3381 * be freed until they are moved to the current list.
3383 if (listadp == NULL) {
3385 TAILQ_FOREACH(listadp, &inodedep->id_newinoupdt, ad_next)
3386 /* found our block */
3389 if (listadp == NULL) {
3390 lk.lkt_held = NOHOLDER;
3391 panic("handle_allocdirect_partdone: lost dep");
3397 * If we have found the just finished dependency, then free
3398 * it along with anything that follows it that is complete.
3400 for (; adp; adp = listadp) {
3401 listadp = TAILQ_NEXT(adp, ad_next);
3402 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
3404 free_allocdirect(&inodedep->id_inoupdt, adp, 1);
3409 * Called from within softdep_disk_write_complete above. Note that
3410 * this routine is always called from interrupt level with further
3411 * splbio interrupts blocked.
3414 handle_allocindir_partdone(aip)
3415 struct allocindir *aip; /* the completed allocindir */
3417 struct indirdep *indirdep;
3419 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
3421 if (aip->ai_buf != NULL) {
3422 lk.lkt_held = NOHOLDER;
3423 panic("handle_allocindir_partdone: dangling dependency");
3425 indirdep = aip->ai_indirdep;
3426 if (indirdep->ir_state & UNDONE) {
3427 LIST_REMOVE(aip, ai_next);
3428 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
3431 ((ufs_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
3433 LIST_REMOVE(aip, ai_next);
3434 if (aip->ai_freefrag != NULL)
3435 add_to_worklist(&aip->ai_freefrag->ff_list);
3436 WORKITEM_FREE(aip, D_ALLOCINDIR);
3440 * Called from within softdep_disk_write_complete above to restore
3441 * in-memory inode block contents to their most up-to-date state. Note
3442 * that this routine is always called from interrupt level with further
3443 * splbio interrupts blocked.
3446 handle_written_inodeblock(inodedep, bp)
3447 struct inodedep *inodedep;
3448 struct buf *bp; /* buffer containing the inode block */
3450 struct worklist *wk, *filefree;
3451 struct allocdirect *adp, *nextadp;
3455 if ((inodedep->id_state & IOSTARTED) == 0) {
3456 lk.lkt_held = NOHOLDER;
3457 panic("handle_written_inodeblock: not started");
3459 inodedep->id_state &= ~IOSTARTED;
3460 inodedep->id_state |= COMPLETE;
3461 dp = (struct dinode *)bp->b_data +
3462 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
3464 * If we had to rollback the inode allocation because of
3465 * bitmaps being incomplete, then simply restore it.
3466 * Keep the block dirty so that it will not be reclaimed until
3467 * all associated dependencies have been cleared and the
3468 * corresponding updates written to disk.
3470 if (inodedep->id_savedino != NULL) {
3471 *dp = *inodedep->id_savedino;
3472 FREE(inodedep->id_savedino, M_INODEDEP);
3473 inodedep->id_savedino = NULL;
3474 if ((bp->b_flags & B_DELWRI) == 0)
3475 stat_inode_bitmap++;
3480 * Roll forward anything that had to be rolled back before
3481 * the inode could be updated.
3484 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
3485 nextadp = TAILQ_NEXT(adp, ad_next);
3486 if (adp->ad_state & ATTACHED) {
3487 lk.lkt_held = NOHOLDER;
3488 panic("handle_written_inodeblock: new entry");
3490 if (adp->ad_lbn < NDADDR) {
3491 if (dp->di_db[adp->ad_lbn] != adp->ad_oldblkno) {
3492 lk.lkt_held = NOHOLDER;
3493 panic("%s: %s #%ld mismatch %d != %d",
3494 "handle_written_inodeblock",
3495 "direct pointer", adp->ad_lbn,
3496 dp->di_db[adp->ad_lbn], adp->ad_oldblkno);
3498 dp->di_db[adp->ad_lbn] = adp->ad_newblkno;
3500 if (dp->di_ib[adp->ad_lbn - NDADDR] != 0) {
3501 lk.lkt_held = NOHOLDER;
3502 panic("%s: %s #%ld allocated as %d",
3503 "handle_written_inodeblock",
3504 "indirect pointer", adp->ad_lbn - NDADDR,
3505 dp->di_ib[adp->ad_lbn - NDADDR]);
3507 dp->di_ib[adp->ad_lbn - NDADDR] = adp->ad_newblkno;
3509 adp->ad_state &= ~UNDONE;
3510 adp->ad_state |= ATTACHED;
3513 if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
3514 stat_direct_blk_ptrs++;
3516 * Reset the file size to its most up-to-date value.
3518 if (inodedep->id_savedsize == -1) {
3519 lk.lkt_held = NOHOLDER;
3520 panic("handle_written_inodeblock: bad size");
3522 if (dp->di_size != inodedep->id_savedsize) {
3523 dp->di_size = inodedep->id_savedsize;
3526 inodedep->id_savedsize = -1;
3528 * If there were any rollbacks in the inode block, then it must be
3529 * marked dirty so that its will eventually get written back in
3535 * Process any allocdirects that completed during the update.
3537 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
3538 handle_allocdirect_partdone(adp);
3540 * Process deallocations that were held pending until the
3541 * inode had been written to disk. Freeing of the inode
3542 * is delayed until after all blocks have been freed to
3543 * avoid creation of new <vfsid, inum, lbn> triples
3544 * before the old ones have been deleted.
3547 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
3548 WORKLIST_REMOVE(wk);
3549 switch (wk->wk_type) {
3553 * We defer adding filefree to the worklist until
3554 * all other additions have been made to ensure
3555 * that it will be done after all the old blocks
3558 if (filefree != NULL) {
3559 lk.lkt_held = NOHOLDER;
3560 panic("handle_written_inodeblock: filefree");
3566 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
3570 diradd_inode_written(WK_DIRADD(wk), inodedep);
3576 add_to_worklist(wk);
3580 lk.lkt_held = NOHOLDER;
3581 panic("handle_written_inodeblock: Unknown type %s",
3582 TYPENAME(wk->wk_type));
3586 if (filefree != NULL) {
3587 if (free_inodedep(inodedep) == 0) {
3588 lk.lkt_held = NOHOLDER;
3589 panic("handle_written_inodeblock: live inodedep");
3591 add_to_worklist(filefree);
3596 * If no outstanding dependencies, free it.
3598 if (free_inodedep(inodedep) || TAILQ_FIRST(&inodedep->id_inoupdt) == 0)
3600 return (hadchanges);
3604 * Process a diradd entry after its dependent inode has been written.
3605 * This routine must be called with splbio interrupts blocked.
3608 diradd_inode_written(dap, inodedep)
3610 struct inodedep *inodedep;
3612 struct pagedep *pagedep;
3614 dap->da_state |= COMPLETE;
3615 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3616 if (dap->da_state & DIRCHG)
3617 pagedep = dap->da_previous->dm_pagedep;
3619 pagedep = dap->da_pagedep;
3620 LIST_REMOVE(dap, da_pdlist);
3621 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3623 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3627 * Handle the completion of a mkdir dependency.
3630 handle_written_mkdir(mkdir, type)
3631 struct mkdir *mkdir;
3635 struct pagedep *pagedep;
3637 if (mkdir->md_state != type) {
3638 lk.lkt_held = NOHOLDER;
3639 panic("handle_written_mkdir: bad type");
3641 dap = mkdir->md_diradd;
3642 dap->da_state &= ~type;
3643 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
3644 dap->da_state |= DEPCOMPLETE;
3645 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3646 if (dap->da_state & DIRCHG)
3647 pagedep = dap->da_previous->dm_pagedep;
3649 pagedep = dap->da_pagedep;
3650 LIST_REMOVE(dap, da_pdlist);
3651 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3653 LIST_REMOVE(mkdir, md_mkdirs);
3654 WORKITEM_FREE(mkdir, D_MKDIR);
3658 * Called from within softdep_disk_write_complete above.
3659 * A write operation was just completed. Removed inodes can
3660 * now be freed and associated block pointers may be committed.
3661 * Note that this routine is always called from interrupt level
3662 * with further splbio interrupts blocked.
3665 handle_written_filepage(pagedep, bp)
3666 struct pagedep *pagedep;
3667 struct buf *bp; /* buffer containing the written page */
3669 struct dirrem *dirrem;
3670 struct diradd *dap, *nextdap;
3674 if ((pagedep->pd_state & IOSTARTED) == 0) {
3675 lk.lkt_held = NOHOLDER;
3676 panic("handle_written_filepage: not started");
3678 pagedep->pd_state &= ~IOSTARTED;
3680 * Process any directory removals that have been committed.
3682 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
3683 LIST_REMOVE(dirrem, dm_next);
3684 dirrem->dm_dirinum = pagedep->pd_ino;
3685 add_to_worklist(&dirrem->dm_list);
3688 * Free any directory additions that have been committed.
3690 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
3693 * Uncommitted directory entries must be restored.
3695 for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
3696 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
3698 nextdap = LIST_NEXT(dap, da_pdlist);
3699 if (dap->da_state & ATTACHED) {
3700 lk.lkt_held = NOHOLDER;
3701 panic("handle_written_filepage: attached");
3703 ep = (struct direct *)
3704 ((char *)bp->b_data + dap->da_offset);
3705 ep->d_ino = dap->da_newinum;
3706 dap->da_state &= ~UNDONE;
3707 dap->da_state |= ATTACHED;
3710 * If the inode referenced by the directory has
3711 * been written out, then the dependency can be
3712 * moved to the pending list.
3714 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
3715 LIST_REMOVE(dap, da_pdlist);
3716 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
3722 * If there were any rollbacks in the directory, then it must be
3723 * marked dirty so that its will eventually get written back in
3727 if ((bp->b_flags & B_DELWRI) == 0)
3732 * If no dependencies remain, the pagedep will be freed.
3733 * Otherwise it will remain to update the page before it
3734 * is written back to disk.
3736 if (LIST_FIRST(&pagedep->pd_pendinghd) == 0) {
3737 for (i = 0; i < DAHASHSZ; i++)
3738 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) != NULL)
3740 if (i == DAHASHSZ) {
3741 LIST_REMOVE(pagedep, pd_hash);
3742 WORKITEM_FREE(pagedep, D_PAGEDEP);
3750 * Writing back in-core inode structures.
3752 * The file system only accesses an inode's contents when it occupies an
3753 * "in-core" inode structure. These "in-core" structures are separate from
3754 * the page frames used to cache inode blocks. Only the latter are
3755 * transferred to/from the disk. So, when the updated contents of the
3756 * "in-core" inode structure are copied to the corresponding in-memory inode
3757 * block, the dependencies are also transferred. The following procedure is
3758 * called when copying a dirty "in-core" inode to a cached inode block.
3762 * Called when an inode is loaded from disk. If the effective link count
3763 * differed from the actual link count when it was last flushed, then we
3764 * need to ensure that the correct effective link count is put back.
3767 softdep_load_inodeblock(ip)
3768 struct inode *ip; /* the "in_core" copy of the inode */
3770 struct inodedep *inodedep;
3773 * Check for alternate nlink count.
3775 ip->i_effnlink = ip->i_nlink;
3777 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3781 ip->i_effnlink -= inodedep->id_nlinkdelta;
3786 * This routine is called just before the "in-core" inode
3787 * information is to be copied to the in-memory inode block.
3788 * Recall that an inode block contains several inodes. If
3789 * the force flag is set, then the dependencies will be
3790 * cleared so that the update can always be made. Note that
3791 * the buffer is locked when this routine is called, so we
3792 * will never be in the middle of writing the inode block
3796 softdep_update_inodeblock(ip, bp, waitfor)
3797 struct inode *ip; /* the "in_core" copy of the inode */
3798 struct buf *bp; /* the buffer containing the inode block */
3799 int waitfor; /* nonzero => update must be allowed */
3801 struct inodedep *inodedep;
3802 struct worklist *wk;
3806 * If the effective link count is not equal to the actual link
3807 * count, then we must track the difference in an inodedep while
3808 * the inode is (potentially) tossed out of the cache. Otherwise,
3809 * if there is no existing inodedep, then there are no dependencies
3813 if (inodedep_lookup(ip->i_fs, ip->i_number, 0, &inodedep) == 0) {
3815 if (ip->i_effnlink != ip->i_nlink)
3816 panic("softdep_update_inodeblock: bad link count");
3819 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) {
3821 panic("softdep_update_inodeblock: bad delta");
3824 * Changes have been initiated. Anything depending on these
3825 * changes cannot occur until this inode has been written.
3827 inodedep->id_state &= ~COMPLETE;
3828 if ((inodedep->id_state & ONWORKLIST) == 0)
3829 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
3831 * Any new dependencies associated with the incore inode must
3832 * now be moved to the list associated with the buffer holding
3833 * the in-memory copy of the inode. Once merged process any
3834 * allocdirects that are completed by the merger.
3836 merge_inode_lists(inodedep);
3837 if (TAILQ_FIRST(&inodedep->id_inoupdt) != NULL)
3838 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
3840 * Now that the inode has been pushed into the buffer, the
3841 * operations dependent on the inode being written to disk
3842 * can be moved to the id_bufwait so that they will be
3843 * processed when the buffer I/O completes.
3845 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
3846 WORKLIST_REMOVE(wk);
3847 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
3850 * Newly allocated inodes cannot be written until the bitmap
3851 * that allocates them have been written (indicated by
3852 * DEPCOMPLETE being set in id_state). If we are doing a
3853 * forced sync (e.g., an fsync on a file), we force the bitmap
3854 * to be written so that the update can be done.
3856 if ((inodedep->id_state & DEPCOMPLETE) != 0 || waitfor == 0) {
3860 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
3863 (error = VOP_BWRITE(inodedep->id_buf->b_vp, inodedep->id_buf)) != 0)
3864 softdep_error("softdep_update_inodeblock: bwrite", error);
3865 if ((inodedep->id_state & DEPCOMPLETE) == 0)
3866 panic("softdep_update_inodeblock: update failed");
3870 * Merge the new inode dependency list (id_newinoupdt) into the old
3871 * inode dependency list (id_inoupdt). This routine must be called
3872 * with splbio interrupts blocked.
3875 merge_inode_lists(inodedep)
3876 struct inodedep *inodedep;
3878 struct allocdirect *listadp, *newadp;
3880 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
3881 for (listadp = TAILQ_FIRST(&inodedep->id_inoupdt); listadp && newadp;) {
3882 if (listadp->ad_lbn < newadp->ad_lbn) {
3883 listadp = TAILQ_NEXT(listadp, ad_next);
3886 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
3887 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
3888 if (listadp->ad_lbn == newadp->ad_lbn) {
3889 allocdirect_merge(&inodedep->id_inoupdt, newadp,
3893 newadp = TAILQ_FIRST(&inodedep->id_newinoupdt);
3895 while ((newadp = TAILQ_FIRST(&inodedep->id_newinoupdt)) != NULL) {
3896 TAILQ_REMOVE(&inodedep->id_newinoupdt, newadp, ad_next);
3897 TAILQ_INSERT_TAIL(&inodedep->id_inoupdt, newadp, ad_next);
3902 * If we are doing an fsync, then we must ensure that any directory
3903 * entries for the inode have been written after the inode gets to disk.
3907 struct vnode *vp; /* the "in_core" copy of the inode */
3909 struct inodedep *inodedep;
3910 struct pagedep *pagedep;
3911 struct worklist *wk;
3918 struct thread *td = curthread; /* XXX */
3919 int error, flushparent;
3926 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0) {
3930 if (LIST_FIRST(&inodedep->id_inowait) != NULL ||
3931 LIST_FIRST(&inodedep->id_bufwait) != NULL ||
3932 TAILQ_FIRST(&inodedep->id_inoupdt) != NULL ||
3933 TAILQ_FIRST(&inodedep->id_newinoupdt) != NULL) {
3935 panic("softdep_fsync: pending ops");
3937 for (error = 0, flushparent = 0; ; ) {
3938 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
3940 if (wk->wk_type != D_DIRADD) {
3942 panic("softdep_fsync: Unexpected type %s",
3943 TYPENAME(wk->wk_type));
3945 dap = WK_DIRADD(wk);
3947 * Flush our parent if this directory entry
3948 * has a MKDIR_PARENT dependency.
3950 if (dap->da_state & DIRCHG)
3951 pagedep = dap->da_previous->dm_pagedep;
3953 pagedep = dap->da_pagedep;
3954 mnt = pagedep->pd_mnt;
3955 parentino = pagedep->pd_ino;
3956 lbn = pagedep->pd_lbn;
3957 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) {
3959 panic("softdep_fsync: dirty");
3961 flushparent = dap->da_state & MKDIR_PARENT;
3963 * If we are being fsync'ed as part of vgone'ing this vnode,
3964 * then we will not be able to release and recover the
3965 * vnode below, so we just have to give up on writing its
3966 * directory entry out. It will eventually be written, just
3967 * not now, but then the user was not asking to have it
3968 * written, so we are not breaking any promises.
3970 if (vp->v_flag & VXLOCK)
3973 * We prevent deadlock by always fetching inodes from the
3974 * root, moving down the directory tree. Thus, when fetching
3975 * our parent directory, we must unlock ourselves before
3976 * requesting the lock on our parent. See the comment in
3977 * ufs_lookup for details on possible races.
3980 VOP_UNLOCK(vp, 0, td);
3981 error = VFS_VGET(mnt, parentino, &pvp);
3982 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3986 if ((error = UFS_UPDATE(pvp, 1)) != 0) {
3992 * Flush directory page containing the inode's name.
3994 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), &bp);
3996 error = VOP_BWRITE(bp->b_vp, bp);
4001 if (inodedep_lookup(fs, ip->i_number, 0, &inodedep) == 0)
4009 * Flush all the dirty bitmaps associated with the block device
4010 * before flushing the rest of the dirty blocks so as to reduce
4011 * the number of dependencies that will have to be rolled back.
4014 softdep_fsync_mountdev(vp)
4017 struct buf *bp, *nbp;
4018 struct worklist *wk;
4020 if (!vn_isdisk(vp, NULL))
4021 panic("softdep_fsync_mountdev: vnode not a disk");
4023 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
4024 nbp = TAILQ_NEXT(bp, b_vnbufs);
4026 * If it is already scheduled, skip to the next buffer.
4028 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
4030 if ((bp->b_flags & B_DELWRI) == 0) {
4032 panic("softdep_fsync_mountdev: not dirty");
4035 * We are only interested in bitmaps with outstanding
4038 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
4039 wk->wk_type != D_BMSAFEMAP ||
4040 (bp->b_xflags & BX_BKGRDINPROG)) {
4049 * Since we may have slept during the I/O, we need
4050 * to start from a known point.
4052 nbp = TAILQ_FIRST(&vp->v_dirtyblkhd);
4054 drain_output(vp, 1);
4059 * This routine is called when we are trying to synchronously flush a
4060 * file. This routine must eliminate any filesystem metadata dependencies
4061 * so that the syncing routine can succeed by pushing the dirty blocks
4062 * associated with the file. If any I/O errors occur, they are returned.
4065 softdep_sync_metadata(ap)
4066 struct vop_fsync_args /* {
4068 struct ucred *a_cred;
4073 struct vnode *vp = ap->a_vp;
4074 struct pagedep *pagedep;
4075 struct allocdirect *adp;
4076 struct allocindir *aip;
4077 struct buf *bp, *nbp;
4078 struct worklist *wk;
4079 int i, error, waitfor;
4082 * Check whether this vnode is involved in a filesystem
4083 * that is doing soft dependency processing.
4085 if (!vn_isdisk(vp, NULL)) {
4086 if (!DOINGSOFTDEP(vp))
4089 if (vp->v_specmountpoint == NULL ||
4090 (vp->v_specmountpoint->mnt_flag & MNT_SOFTDEP) == 0)
4093 * Ensure that any direct block dependencies have been cleared.
4096 if ((error = flush_inodedep_deps(VTOI(vp)->i_fs, VTOI(vp)->i_number))) {
4101 * For most files, the only metadata dependencies are the
4102 * cylinder group maps that allocate their inode or blocks.
4103 * The block allocation dependencies can be found by traversing
4104 * the dependency lists for any buffers that remain on their
4105 * dirty buffer list. The inode allocation dependency will
4106 * be resolved when the inode is updated with MNT_WAIT.
4107 * This work is done in two passes. The first pass grabs most
4108 * of the buffers and begins asynchronously writing them. The
4109 * only way to wait for these asynchronous writes is to sleep
4110 * on the filesystem vnode which may stay busy for a long time
4111 * if the filesystem is active. So, instead, we make a second
4112 * pass over the dependencies blocking on each write. In the
4113 * usual case we will be blocking against a write that we
4114 * initiated, so when it is done the dependency will have been
4115 * resolved. Thus the second pass is expected to end quickly.
4117 waitfor = MNT_NOWAIT;
4120 * We must wait for any I/O in progress to finish so that
4121 * all potential buffers on the dirty list will be visible.
4123 drain_output(vp, 1);
4124 if (getdirtybuf(&TAILQ_FIRST(&vp->v_dirtyblkhd), MNT_WAIT) == 0) {
4128 bp = TAILQ_FIRST(&vp->v_dirtyblkhd);
4131 * As we hold the buffer locked, none of its dependencies
4134 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4135 switch (wk->wk_type) {
4138 adp = WK_ALLOCDIRECT(wk);
4139 if (adp->ad_state & DEPCOMPLETE)
4142 if (getdirtybuf(&nbp, waitfor) == 0)
4145 if (waitfor == MNT_NOWAIT) {
4147 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4155 aip = WK_ALLOCINDIR(wk);
4156 if (aip->ai_state & DEPCOMPLETE)
4159 if (getdirtybuf(&nbp, waitfor) == 0)
4162 if (waitfor == MNT_NOWAIT) {
4164 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4174 LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
4175 if (aip->ai_state & DEPCOMPLETE)
4178 if (getdirtybuf(&nbp, MNT_WAIT) == 0)
4181 if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4191 if ((error = flush_inodedep_deps(WK_INODEDEP(wk)->id_fs,
4192 WK_INODEDEP(wk)->id_ino)) != 0) {
4201 * We are trying to sync a directory that may
4202 * have dependencies on both its own metadata
4203 * and/or dependencies on the inodes of any
4204 * recently allocated files. We walk its diradd
4205 * lists pushing out the associated inode.
4207 pagedep = WK_PAGEDEP(wk);
4208 for (i = 0; i < DAHASHSZ; i++) {
4209 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
4212 flush_pagedep_deps(vp, pagedep->pd_mnt,
4213 &pagedep->pd_diraddhd[i]))) {
4223 * This case should never happen if the vnode has
4224 * been properly sync'ed. However, if this function
4225 * is used at a place where the vnode has not yet
4226 * been sync'ed, this dependency can show up. So,
4227 * rather than panic, just flush it.
4229 nbp = WK_MKDIR(wk)->md_buf;
4230 if (getdirtybuf(&nbp, waitfor) == 0)
4233 if (waitfor == MNT_NOWAIT) {
4235 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4244 * This case should never happen if the vnode has
4245 * been properly sync'ed. However, if this function
4246 * is used at a place where the vnode has not yet
4247 * been sync'ed, this dependency can show up. So,
4248 * rather than panic, just flush it.
4250 nbp = WK_BMSAFEMAP(wk)->sm_buf;
4251 if (getdirtybuf(&nbp, waitfor) == 0)
4254 if (waitfor == MNT_NOWAIT) {
4256 } else if ((error = VOP_BWRITE(nbp->b_vp, nbp)) != 0) {
4265 panic("softdep_sync_metadata: Unknown type %s",
4266 TYPENAME(wk->wk_type));
4270 (void) getdirtybuf(&TAILQ_NEXT(bp, b_vnbufs), MNT_WAIT);
4271 nbp = TAILQ_NEXT(bp, b_vnbufs);
4280 * The brief unlock is to allow any pent up dependency
4281 * processing to be done. Then proceed with the second pass.
4283 if (waitfor == MNT_NOWAIT) {
4291 * If we have managed to get rid of all the dirty buffers,
4292 * then we are done. For certain directories and block
4293 * devices, we may need to do further work.
4295 if (TAILQ_FIRST(&vp->v_dirtyblkhd) == NULL) {
4302 * If we are trying to sync a block device, some of its buffers may
4303 * contain metadata that cannot be written until the contents of some
4304 * partially written files have been written to disk. The only easy
4305 * way to accomplish this is to sync the entire filesystem (luckily
4306 * this happens rarely).
4308 * We must wait for any I/O in progress to finish so that
4309 * all potential buffers on the dirty list will be visible.
4311 drain_output(vp, 1);
4312 if (vn_isdisk(vp, NULL) &&
4313 vp->v_specmountpoint && !VOP_ISLOCKED(vp, NULL) &&
4314 (error = VFS_SYNC(vp->v_specmountpoint, MNT_WAIT, ap->a_td)) != 0)
4320 * Flush the dependencies associated with an inodedep.
4321 * Called with splbio blocked.
4324 flush_inodedep_deps(fs, ino)
4328 struct inodedep *inodedep;
4329 struct allocdirect *adp;
4334 * This work is done in two passes. The first pass grabs most
4335 * of the buffers and begins asynchronously writing them. The
4336 * only way to wait for these asynchronous writes is to sleep
4337 * on the filesystem vnode which may stay busy for a long time
4338 * if the filesystem is active. So, instead, we make a second
4339 * pass over the dependencies blocking on each write. In the
4340 * usual case we will be blocking against a write that we
4341 * initiated, so when it is done the dependency will have been
4342 * resolved. Thus the second pass is expected to end quickly.
4343 * We give a brief window at the top of the loop to allow
4344 * any pending I/O to complete.
4346 for (waitfor = MNT_NOWAIT; ; ) {
4349 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4351 TAILQ_FOREACH(adp, &inodedep->id_inoupdt, ad_next) {
4352 if (adp->ad_state & DEPCOMPLETE)
4355 if (getdirtybuf(&bp, waitfor) == 0) {
4356 if (waitfor == MNT_NOWAIT)
4361 if (waitfor == MNT_NOWAIT) {
4363 } else if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) {
4372 TAILQ_FOREACH(adp, &inodedep->id_newinoupdt, ad_next) {
4373 if (adp->ad_state & DEPCOMPLETE)
4376 if (getdirtybuf(&bp, waitfor) == 0) {
4377 if (waitfor == MNT_NOWAIT)
4382 if (waitfor == MNT_NOWAIT) {
4384 } else if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0) {
4394 * If pass2, we are done, otherwise do pass 2.
4396 if (waitfor == MNT_WAIT)
4401 * Try freeing inodedep in case all dependencies have been removed.
4403 if (inodedep_lookup(fs, ino, 0, &inodedep) != 0)
4404 (void) free_inodedep(inodedep);
4409 * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
4410 * Called with splbio blocked.
4413 flush_pagedep_deps(pvp, mp, diraddhdp)
4416 struct diraddhd *diraddhdp;
4418 struct thread *td = curthread; /* XXX */
4420 struct inodedep *inodedep;
4421 struct ufsmount *ump;
4424 int gotit, error = 0;
4428 KKASSERT(td->td_proc);
4429 cr = td->td_proc->p_ucred;
4432 while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
4434 * Flush ourselves if this directory entry
4435 * has a MKDIR_PARENT dependency.
4437 if (dap->da_state & MKDIR_PARENT) {
4439 if ((error = UFS_UPDATE(pvp, 1)) != 0)
4443 * If that cleared dependencies, go on to next.
4445 if (dap != LIST_FIRST(diraddhdp))
4447 if (dap->da_state & MKDIR_PARENT) {
4449 panic("flush_pagedep_deps: MKDIR_PARENT");
4453 * A newly allocated directory must have its "." and
4454 * ".." entries written out before its name can be
4455 * committed in its parent. We do not want or need
4456 * the full semantics of a synchronous VOP_FSYNC as
4457 * that may end up here again, once for each directory
4458 * level in the filesystem. Instead, we push the blocks
4459 * and wait for them to clear. We have to fsync twice
4460 * because the first call may choose to defer blocks
4461 * that still have dependencies, but deferral will
4462 * happen at most once.
4464 inum = dap->da_newinum;
4465 if (dap->da_state & MKDIR_BODY) {
4467 if ((error = VFS_VGET(mp, inum, &vp)) != 0)
4469 if ((error=VOP_FSYNC(vp, MNT_NOWAIT, td)) ||
4470 (error=VOP_FSYNC(vp, MNT_NOWAIT, td))) {
4474 drain_output(vp, 0);
4478 * If that cleared dependencies, go on to next.
4480 if (dap != LIST_FIRST(diraddhdp))
4482 if (dap->da_state & MKDIR_BODY) {
4484 panic("flush_pagedep_deps: MKDIR_BODY");
4488 * Flush the inode on which the directory entry depends.
4489 * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
4490 * the only remaining dependency is that the updated inode
4491 * count must get pushed to disk. The inode has already
4492 * been pushed into its inode buffer (via VOP_UPDATE) at
4493 * the time of the reference count change. So we need only
4494 * locate that buffer, ensure that there will be no rollback
4495 * caused by a bitmap dependency, then write the inode buffer.
4497 if (inodedep_lookup(ump->um_fs, inum, 0, &inodedep) == 0) {
4499 panic("flush_pagedep_deps: lost inode");
4502 * If the inode still has bitmap dependencies,
4503 * push them to disk.
4505 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4506 gotit = getdirtybuf(&inodedep->id_buf, MNT_WAIT);
4509 (error = VOP_BWRITE(inodedep->id_buf->b_vp,
4510 inodedep->id_buf)) != 0)
4513 if (dap != LIST_FIRST(diraddhdp))
4517 * If the inode is still sitting in a buffer waiting
4518 * to be written, push it to disk.
4521 if ((error = bread(ump->um_devvp,
4522 fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
4523 (int)ump->um_fs->fs_bsize, &bp)) != 0)
4525 if ((error = VOP_BWRITE(bp->b_vp, bp)) != 0)
4529 * If we have failed to get rid of all the dependencies
4530 * then something is seriously wrong.
4532 if (dap == LIST_FIRST(diraddhdp)) {
4534 panic("flush_pagedep_deps: flush failed");
4543 * A large burst of file addition or deletion activity can drive the
4544 * memory load excessively high. First attempt to slow things down
4545 * using the techniques below. If that fails, this routine requests
4546 * the offending operations to fall back to running synchronously
4547 * until the memory load returns to a reasonable level.
4550 softdep_slowdown(vp)
4553 int max_softdeps_hard;
4555 max_softdeps_hard = max_softdeps * 11 / 10;
4556 if (num_dirrem < max_softdeps_hard / 2 &&
4557 num_inodedep < max_softdeps_hard)
4559 stat_sync_limit_hit += 1;
4564 * If memory utilization has gotten too high, deliberately slow things
4565 * down and speed up the I/O processing.
4568 request_cleanup(resource, islocked)
4572 struct thread *td = curthread; /* XXX */
4575 * We never hold up the filesystem syncer process.
4577 if (td == filesys_syncer)
4580 * First check to see if the work list has gotten backlogged.
4581 * If it has, co-opt this process to help clean up two entries.
4582 * Because this process may hold inodes locked, we cannot
4583 * handle any remove requests that might block on a locked
4584 * inode as that could lead to deadlock.
4586 if (num_on_worklist > max_softdeps / 10) {
4589 process_worklist_item(NULL, LK_NOWAIT);
4590 process_worklist_item(NULL, LK_NOWAIT);
4591 stat_worklist_push += 2;
4598 * If we are resource constrained on inode dependencies, try
4599 * flushing some dirty inodes. Otherwise, we are constrained
4600 * by file deletions, so try accelerating flushes of directories
4601 * with removal dependencies. We would like to do the cleanup
4602 * here, but we probably hold an inode locked at this point and
4603 * that might deadlock against one that we try to clean. So,
4604 * the best that we can do is request the syncer daemon to do
4605 * the cleanup for us.
4610 stat_ino_limit_push += 1;
4611 req_clear_inodedeps += 1;
4612 stat_countp = &stat_ino_limit_hit;
4616 stat_blk_limit_push += 1;
4617 req_clear_remove += 1;
4618 stat_countp = &stat_blk_limit_hit;
4624 panic("request_cleanup: unknown type");
4627 * Hopefully the syncer daemon will catch up and awaken us.
4628 * We wait at most tickdelay before proceeding in any case.
4633 if (handle.callout == NULL)
4634 handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
4635 interlocked_sleep(&lk, SLEEP, (caddr_t)&proc_waiting, PPAUSE,
4644 * Awaken processes pausing in request_cleanup and clear proc_waiting
4645 * to indicate that there is no longer a timer running.
4653 wakeup_one(&proc_waiting);
4654 if (proc_waiting > 0)
4655 handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
4657 handle.callout = NULL;
4661 * Flush out a directory with at least one removal dependency in an effort to
4662 * reduce the number of dirrem, freefile, and freeblks dependency structures.
4665 clear_remove(struct thread *td)
4667 struct pagedep_hashhead *pagedephd;
4668 struct pagedep *pagedep;
4669 static int next = 0;
4676 for (cnt = 0; cnt < pagedep_hash; cnt++) {
4677 pagedephd = &pagedep_hashtbl[next++];
4678 if (next >= pagedep_hash)
4680 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
4681 if (LIST_FIRST(&pagedep->pd_dirremhd) == NULL)
4683 mp = pagedep->pd_mnt;
4684 ino = pagedep->pd_ino;
4686 if ((error = VFS_VGET(mp, ino, &vp)) != 0) {
4687 softdep_error("clear_remove: vget", error);
4690 if ((error = VOP_FSYNC(vp, MNT_NOWAIT, td)))
4691 softdep_error("clear_remove: fsync", error);
4692 drain_output(vp, 0);
4701 * Clear out a block of dirty inodes in an effort to reduce
4702 * the number of inodedep dependency structures.
4705 clear_inodedeps(struct thread *td)
4708 struct inodedep_hashhead *inodedephd;
4709 struct inodedep *inodedep;
4710 static int next = 0;
4715 ino_t firstino, lastino, ino;
4717 KKASSERT(td->td_proc);
4718 cred = td->td_proc->p_ucred;
4722 * Pick a random inode dependency to be cleared.
4723 * We will then gather up all the inodes in its block
4724 * that have dependencies and flush them out.
4726 for (cnt = 0; cnt < inodedep_hash; cnt++) {
4727 inodedephd = &inodedep_hashtbl[next++];
4728 if (next >= inodedep_hash)
4730 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
4733 if (inodedep == NULL)
4736 * Ugly code to find mount point given pointer to superblock.
4738 fs = inodedep->id_fs;
4739 TAILQ_FOREACH(mp, &mountlist, mnt_list)
4740 if ((mp->mnt_flag & MNT_SOFTDEP) && fs == VFSTOUFS(mp)->um_fs)
4743 * Find the last inode in the block with dependencies.
4745 firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
4746 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
4747 if (inodedep_lookup(fs, lastino, 0, &inodedep) != 0)
4750 * Asynchronously push all but the last inode with dependencies.
4751 * Synchronously push the last inode with dependencies to ensure
4752 * that the inode block gets written to free up the inodedeps.
4754 for (ino = firstino; ino <= lastino; ino++) {
4755 if (inodedep_lookup(fs, ino, 0, &inodedep) == 0)
4758 if ((error = VFS_VGET(mp, ino, &vp)) != 0) {
4759 softdep_error("clear_inodedeps: vget", error);
4762 if (ino == lastino) {
4763 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)))
4764 softdep_error("clear_inodedeps: fsync1", error);
4766 if ((error = VOP_FSYNC(vp, MNT_NOWAIT, td)))
4767 softdep_error("clear_inodedeps: fsync2", error);
4768 drain_output(vp, 0);
4777 * Function to determine if the buffer has outstanding dependencies
4778 * that will cause a roll-back if the buffer is written. If wantcount
4779 * is set, return number of dependencies, otherwise just yes or no.
4782 softdep_count_dependencies(bp, wantcount)
4786 struct worklist *wk;
4787 struct inodedep *inodedep;
4788 struct indirdep *indirdep;
4789 struct allocindir *aip;
4790 struct pagedep *pagedep;
4796 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
4797 switch (wk->wk_type) {
4800 inodedep = WK_INODEDEP(wk);
4801 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4802 /* bitmap allocation dependency */
4807 if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
4808 /* direct block pointer dependency */
4816 indirdep = WK_INDIRDEP(wk);
4818 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
4819 /* indirect block pointer dependency */
4827 pagedep = WK_PAGEDEP(wk);
4828 for (i = 0; i < DAHASHSZ; i++) {
4830 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
4831 /* directory entry dependency */
4843 /* never a dependency on these blocks */
4848 panic("softdep_check_for_rollback: Unexpected type %s",
4849 TYPENAME(wk->wk_type));
4859 * Acquire exclusive access to a buffer.
4860 * Must be called with splbio blocked.
4861 * Return 1 if buffer was acquired.
4864 getdirtybuf(bpp, waitfor)
4872 if ((bp = *bpp) == NULL)
4874 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
4875 if ((bp->b_xflags & BX_BKGRDINPROG) == 0)
4878 if (waitfor != MNT_WAIT)
4880 bp->b_xflags |= BX_BKGRDWAIT;
4881 interlocked_sleep(&lk, SLEEP, &bp->b_xflags, PRIBIO,
4885 if (waitfor != MNT_WAIT)
4887 error = interlocked_sleep(&lk, LOCKBUF, bp,
4888 LK_EXCLUSIVE | LK_SLEEPFAIL, 0, 0);
4889 if (error != ENOLCK) {
4891 panic("getdirtybuf: inconsistent lock");
4894 if ((bp->b_flags & B_DELWRI) == 0) {
4903 * Wait for pending output on a vnode to complete.
4904 * Must be called with vnode locked.
4907 drain_output(vp, islocked)
4914 while (vp->v_numoutput) {
4915 vp->v_flag |= VBWAIT;
4916 interlocked_sleep(&lk, SLEEP, (caddr_t)&vp->v_numoutput,
4917 PRIBIO + 1, "drainvp", 0);
4924 * Called whenever a buffer that is being invalidated or reallocated
4925 * contains dependencies. This should only happen if an I/O error has
4926 * occurred. The routine is called with the buffer locked.
4929 softdep_deallocate_dependencies(bp)
4933 if ((bp->b_flags & B_ERROR) == 0)
4934 panic("softdep_deallocate_dependencies: dangling deps");
4935 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
4936 panic("softdep_deallocate_dependencies: unrecovered I/O error");
4940 * Function to handle asynchronous write errors in the filesystem.
4943 softdep_error(func, error)
4948 /* XXX should do something better! */
4949 printf("%s: got error %d while accessing filesystem\n", func, error);