]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ffs/ffs_softdep.c
Introduce some functions in the vnode locks namespace and in the ffs
[FreeBSD/FreeBSD.git] / sys / ufs / ffs / ffs_softdep.c
1 /*-
2  * Copyright 1998, 2000 Marshall Kirk McKusick. All Rights Reserved.
3  *
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).
8  *
9  * Further information about soft updates can be obtained from:
10  *
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
14  *      USA
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
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.
25  *
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
36  * SUCH DAMAGE.
37  *
38  *      from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00
39  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 /*
45  * For now we want the safety net that the DEBUG flag provides.
46  */
47 #ifndef DEBUG
48 #define DEBUG
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/kdb.h>
57 #include <sys/kthread.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
61 #include <sys/mutex.h>
62 #include <sys/proc.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/syslog.h>
66 #include <sys/vnode.h>
67 #include <sys/conf.h>
68 #include <ufs/ufs/dir.h>
69 #include <ufs/ufs/extattr.h>
70 #include <ufs/ufs/quota.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/ufsmount.h>
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/softdep.h>
75 #include <ufs/ffs/ffs_extern.h>
76 #include <ufs/ufs/ufs_extern.h>
77
78 #include <vm/vm.h>
79
80 #include "opt_ffs.h"
81 #include "opt_quota.h"
82
83 #ifndef SOFTUPDATES
84
85 int
86 softdep_flushfiles(oldmnt, flags, td)
87         struct mount *oldmnt;
88         int flags;
89         struct thread *td;
90 {
91
92         panic("softdep_flushfiles called");
93 }
94
95 int
96 softdep_mount(devvp, mp, fs, cred)
97         struct vnode *devvp;
98         struct mount *mp;
99         struct fs *fs;
100         struct ucred *cred;
101 {
102
103         return (0);
104 }
105
106 void 
107 softdep_initialize()
108 {
109
110         return;
111 }
112
113 void
114 softdep_uninitialize()
115 {
116
117         return;
118 }
119
120 void
121 softdep_setup_inomapdep(bp, ip, newinum)
122         struct buf *bp;
123         struct inode *ip;
124         ino_t newinum;
125 {
126
127         panic("softdep_setup_inomapdep called");
128 }
129
130 void
131 softdep_setup_blkmapdep(bp, mp, newblkno)
132         struct buf *bp;
133         struct mount *mp;
134         ufs2_daddr_t newblkno;
135 {
136
137         panic("softdep_setup_blkmapdep called");
138 }
139
140 void 
141 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
142         struct inode *ip;
143         ufs_lbn_t lbn;
144         ufs2_daddr_t newblkno;
145         ufs2_daddr_t oldblkno;
146         long newsize;
147         long oldsize;
148         struct buf *bp;
149 {
150         
151         panic("softdep_setup_allocdirect called");
152 }
153
154 void 
155 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
156         struct inode *ip;
157         ufs_lbn_t lbn;
158         ufs2_daddr_t newblkno;
159         ufs2_daddr_t oldblkno;
160         long newsize;
161         long oldsize;
162         struct buf *bp;
163 {
164         
165         panic("softdep_setup_allocext called");
166 }
167
168 void
169 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
170         struct inode *ip;
171         ufs_lbn_t lbn;
172         struct buf *bp;
173         int ptrno;
174         ufs2_daddr_t newblkno;
175         ufs2_daddr_t oldblkno;
176         struct buf *nbp;
177 {
178
179         panic("softdep_setup_allocindir_page called");
180 }
181
182 void
183 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
184         struct buf *nbp;
185         struct inode *ip;
186         struct buf *bp;
187         int ptrno;
188         ufs2_daddr_t newblkno;
189 {
190
191         panic("softdep_setup_allocindir_meta called");
192 }
193
194 void
195 softdep_setup_freeblocks(ip, length, flags)
196         struct inode *ip;
197         off_t length;
198         int flags;
199 {
200         
201         panic("softdep_setup_freeblocks called");
202 }
203
204 void
205 softdep_freefile(pvp, ino, mode)
206                 struct vnode *pvp;
207                 ino_t ino;
208                 int mode;
209 {
210
211         panic("softdep_freefile called");
212 }
213
214 int 
215 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
216         struct buf *bp;
217         struct inode *dp;
218         off_t diroffset;
219         ino_t newinum;
220         struct buf *newdirbp;
221         int isnewblk;
222 {
223
224         panic("softdep_setup_directory_add called");
225 }
226
227 void 
228 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
229         struct inode *dp;
230         caddr_t base;
231         caddr_t oldloc;
232         caddr_t newloc;
233         int entrysize;
234 {
235
236         panic("softdep_change_directoryentry_offset called");
237 }
238
239 void 
240 softdep_setup_remove(bp, dp, ip, isrmdir)
241         struct buf *bp;
242         struct inode *dp;
243         struct inode *ip;
244         int isrmdir;
245 {
246         
247         panic("softdep_setup_remove called");
248 }
249
250 void 
251 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
252         struct buf *bp;
253         struct inode *dp;
254         struct inode *ip;
255         ino_t newinum;
256         int isrmdir;
257 {
258
259         panic("softdep_setup_directory_change called");
260 }
261
262 void
263 softdep_change_linkcnt(ip)
264         struct inode *ip;
265 {
266
267         panic("softdep_change_linkcnt called");
268 }
269
270 void 
271 softdep_load_inodeblock(ip)
272         struct inode *ip;
273 {
274
275         panic("softdep_load_inodeblock called");
276 }
277
278 void 
279 softdep_update_inodeblock(ip, bp, waitfor)
280         struct inode *ip;
281         struct buf *bp;
282         int waitfor;
283 {
284
285         panic("softdep_update_inodeblock called");
286 }
287
288 int
289 softdep_fsync(vp)
290         struct vnode *vp;       /* the "in_core" copy of the inode */
291 {
292
293         return (0);
294 }
295
296 void
297 softdep_fsync_mountdev(vp)
298         struct vnode *vp;
299 {
300
301         return;
302 }
303
304 int
305 softdep_flushworklist(oldmnt, countp, td)
306         struct mount *oldmnt;
307         int *countp;
308         struct thread *td;
309 {
310
311         *countp = 0;
312         return (0);
313 }
314
315 int
316 softdep_sync_metadata(struct vnode *vp)
317 {
318
319         return (0);
320 }
321
322 int
323 softdep_slowdown(vp)
324         struct vnode *vp;
325 {
326
327         panic("softdep_slowdown called");
328 }
329
330 void
331 softdep_releasefile(ip)
332         struct inode *ip;       /* inode with the zero effective link count */
333 {
334
335         panic("softdep_releasefile called");
336 }
337
338 int
339 softdep_request_cleanup(fs, vp)
340         struct fs *fs;
341         struct vnode *vp;
342 {
343
344         return (0);
345 }
346
347 int
348 softdep_check_suspend(struct mount *mp,
349                       struct vnode *devvp,
350                       int softdep_deps,
351                       int softdep_accdeps,
352                       int secondary_writes,
353                       int secondary_accwrites)
354 {
355         struct bufobj *bo;
356         int error;
357         
358         (void) softdep_deps,
359         (void) softdep_accdeps;
360
361         ASSERT_VI_LOCKED(devvp, "softdep_check_suspend");
362         bo = &devvp->v_bufobj;
363
364         for (;;) {
365                 if (!MNT_ITRYLOCK(mp)) {
366                         VI_UNLOCK(devvp);
367                         MNT_ILOCK(mp);
368                         MNT_IUNLOCK(mp);
369                         VI_LOCK(devvp);
370                         continue;
371                 }
372                 if (mp->mnt_secondary_writes != 0) {
373                         VI_UNLOCK(devvp);
374                         msleep(&mp->mnt_secondary_writes,
375                                MNT_MTX(mp),
376                                (PUSER - 1) | PDROP, "secwr", 0);
377                         VI_LOCK(devvp);
378                         continue;
379                 }
380                 break;
381         }
382
383         /*
384          * Reasons for needing more work before suspend:
385          * - Dirty buffers on devvp.
386          * - Secondary writes occurred after start of vnode sync loop
387          */
388         error = 0;
389         if (bo->bo_numoutput > 0 ||
390             bo->bo_dirty.bv_cnt > 0 ||
391             secondary_writes != 0 ||
392             mp->mnt_secondary_writes != 0 ||
393             secondary_accwrites != mp->mnt_secondary_accwrites)
394                 error = EAGAIN;
395         VI_UNLOCK(devvp);
396         return (error);
397 }
398
399 void
400 softdep_get_depcounts(struct mount *mp,
401                       int *softdepactivep,
402                       int *softdepactiveaccp)
403 {
404         (void) mp;
405         *softdepactivep = 0;
406         *softdepactiveaccp = 0;
407 }
408
409 #else
410 /*
411  * These definitions need to be adapted to the system to which
412  * this file is being ported.
413  */
414 /*
415  * malloc types defined for the softdep system.
416  */
417 static MALLOC_DEFINE(M_PAGEDEP, "pagedep","File page dependencies");
418 static MALLOC_DEFINE(M_INODEDEP, "inodedep","Inode dependencies");
419 static MALLOC_DEFINE(M_NEWBLK, "newblk","New block allocation");
420 static MALLOC_DEFINE(M_BMSAFEMAP, "bmsafemap","Block or frag allocated from cyl group map");
421 static MALLOC_DEFINE(M_ALLOCDIRECT, "allocdirect","Block or frag dependency for an inode");
422 static MALLOC_DEFINE(M_INDIRDEP, "indirdep","Indirect block dependencies");
423 static MALLOC_DEFINE(M_ALLOCINDIR, "allocindir","Block dependency for an indirect block");
424 static MALLOC_DEFINE(M_FREEFRAG, "freefrag","Previously used frag for an inode");
425 static MALLOC_DEFINE(M_FREEBLKS, "freeblks","Blocks freed from an inode");
426 static MALLOC_DEFINE(M_FREEFILE, "freefile","Inode deallocated");
427 static MALLOC_DEFINE(M_DIRADD, "diradd","New directory entry");
428 static MALLOC_DEFINE(M_MKDIR, "mkdir","New directory");
429 static MALLOC_DEFINE(M_DIRREM, "dirrem","Directory entry deleted");
430 static MALLOC_DEFINE(M_NEWDIRBLK, "newdirblk","Unclaimed new directory block");
431 static MALLOC_DEFINE(M_SAVEDINO, "savedino","Saved inodes");
432
433 #define M_SOFTDEP_FLAGS (M_WAITOK | M_USE_RESERVE)
434
435 #define D_PAGEDEP       0
436 #define D_INODEDEP      1
437 #define D_NEWBLK        2
438 #define D_BMSAFEMAP     3
439 #define D_ALLOCDIRECT   4
440 #define D_INDIRDEP      5
441 #define D_ALLOCINDIR    6
442 #define D_FREEFRAG      7
443 #define D_FREEBLKS      8
444 #define D_FREEFILE      9
445 #define D_DIRADD        10
446 #define D_MKDIR         11
447 #define D_DIRREM        12
448 #define D_NEWDIRBLK     13
449 #define D_LAST          D_NEWDIRBLK
450
451 /* 
452  * translate from workitem type to memory type
453  * MUST match the defines above, such that memtype[D_XXX] == M_XXX
454  */
455 static struct malloc_type *memtype[] = {
456         M_PAGEDEP,
457         M_INODEDEP,
458         M_NEWBLK,
459         M_BMSAFEMAP,
460         M_ALLOCDIRECT,
461         M_INDIRDEP,
462         M_ALLOCINDIR,
463         M_FREEFRAG,
464         M_FREEBLKS,
465         M_FREEFILE,
466         M_DIRADD,
467         M_MKDIR,
468         M_DIRREM,
469         M_NEWDIRBLK
470 };
471
472 #define DtoM(type) (memtype[type])
473
474 /*
475  * Names of malloc types.
476  */
477 #define TYPENAME(type)  \
478         ((unsigned)(type) < D_LAST ? memtype[type]->ks_shortdesc : "???")
479 /*
480  * End system adaptation definitions.
481  */
482
483 /*
484  * Forward declarations.
485  */
486 struct inodedep_hashhead;
487 struct newblk_hashhead;
488 struct pagedep_hashhead;
489
490 /*
491  * Internal function prototypes.
492  */
493 static  void softdep_error(char *, int);
494 static  void drain_output(struct vnode *);
495 static  struct buf *getdirtybuf(struct buf *, struct mtx *, int);
496 static  void clear_remove(struct thread *);
497 static  void clear_inodedeps(struct thread *);
498 static  int flush_pagedep_deps(struct vnode *, struct mount *,
499             struct diraddhd *);
500 static  int flush_inodedep_deps(struct mount *, ino_t);
501 static  int flush_deplist(struct allocdirectlst *, int, int *);
502 static  int handle_written_filepage(struct pagedep *, struct buf *);
503 static  void diradd_inode_written(struct diradd *, struct inodedep *);
504 static  int handle_written_inodeblock(struct inodedep *, struct buf *);
505 static  void handle_allocdirect_partdone(struct allocdirect *);
506 static  void handle_allocindir_partdone(struct allocindir *);
507 static  void initiate_write_filepage(struct pagedep *, struct buf *);
508 static  void handle_written_mkdir(struct mkdir *, int);
509 static  void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *);
510 static  void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *);
511 static  void handle_workitem_freefile(struct freefile *);
512 static  void handle_workitem_remove(struct dirrem *, struct vnode *);
513 static  struct dirrem *newdirrem(struct buf *, struct inode *,
514             struct inode *, int, struct dirrem **);
515 static  void free_diradd(struct diradd *);
516 static  void free_allocindir(struct allocindir *, struct inodedep *);
517 static  void free_newdirblk(struct newdirblk *);
518 static  int indir_trunc(struct freeblks *, ufs2_daddr_t, int, ufs_lbn_t,
519             ufs2_daddr_t *);
520 static  void deallocate_dependencies(struct buf *, struct inodedep *);
521 static  void free_allocdirect(struct allocdirectlst *,
522             struct allocdirect *, int);
523 static  int check_inode_unwritten(struct inodedep *);
524 static  int free_inodedep(struct inodedep *);
525 static  void handle_workitem_freeblocks(struct freeblks *, int);
526 static  void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *);
527 static  void setup_allocindir_phase2(struct buf *, struct inode *,
528             struct allocindir *);
529 static  struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t,
530             ufs2_daddr_t);
531 static  void handle_workitem_freefrag(struct freefrag *);
532 static  struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long);
533 static  void allocdirect_merge(struct allocdirectlst *,
534             struct allocdirect *, struct allocdirect *);
535 static  struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *);
536 static  int newblk_find(struct newblk_hashhead *, struct fs *, ufs2_daddr_t,
537             struct newblk **);
538 static  int newblk_lookup(struct fs *, ufs2_daddr_t, int, struct newblk **);
539 static  int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t,
540             struct inodedep **);
541 static  int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **);
542 static  int pagedep_lookup(struct inode *, ufs_lbn_t, int, struct pagedep **);
543 static  int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t,
544             struct mount *mp, int, struct pagedep **);
545 static  void pause_timer(void *);
546 static  int request_cleanup(struct mount *, int);
547 static  int process_worklist_item(struct mount *, int);
548 static  void add_to_worklist(struct worklist *);
549 static  void softdep_flush(void);
550 static  int softdep_speedup(void);
551
552 /*
553  * Exported softdep operations.
554  */
555 static  void softdep_disk_io_initiation(struct buf *);
556 static  void softdep_disk_write_complete(struct buf *);
557 static  void softdep_deallocate_dependencies(struct buf *);
558 static  int softdep_count_dependencies(struct buf *bp, int);
559
560 static struct mtx lk;
561 MTX_SYSINIT(softdep_lock, &lk, "Softdep Lock", MTX_DEF);
562
563 #define TRY_ACQUIRE_LOCK(lk)            mtx_trylock(lk)
564 #define ACQUIRE_LOCK(lk)                mtx_lock(lk)
565 #define FREE_LOCK(lk)                   mtx_unlock(lk)
566
567 #define BUF_AREC(bp)    ((bp)->b_lock.lk_flags |= LK_CANRECURSE)
568 #define BUF_NOREC(bp)   ((bp)->b_lock.lk_flags &= ~LK_CANRECURSE)
569
570 /*
571  * Worklist queue management.
572  * These routines require that the lock be held.
573  */
574 #ifndef /* NOT */ DEBUG
575 #define WORKLIST_INSERT(head, item) do {        \
576         (item)->wk_state |= ONWORKLIST;         \
577         LIST_INSERT_HEAD(head, item, wk_list);  \
578 } while (0)
579 #define WORKLIST_REMOVE(item) do {              \
580         (item)->wk_state &= ~ONWORKLIST;        \
581         LIST_REMOVE(item, wk_list);             \
582 } while (0)
583 #else /* DEBUG */
584 static  void worklist_insert(struct workhead *, struct worklist *);
585 static  void worklist_remove(struct worklist *);
586
587 #define WORKLIST_INSERT(head, item) worklist_insert(head, item)
588 #define WORKLIST_REMOVE(item) worklist_remove(item)
589
590 static void
591 worklist_insert(head, item)
592         struct workhead *head;
593         struct worklist *item;
594 {
595
596         mtx_assert(&lk, MA_OWNED);
597         if (item->wk_state & ONWORKLIST)
598                 panic("worklist_insert: already on list");
599         item->wk_state |= ONWORKLIST;
600         LIST_INSERT_HEAD(head, item, wk_list);
601 }
602
603 static void
604 worklist_remove(item)
605         struct worklist *item;
606 {
607
608         mtx_assert(&lk, MA_OWNED);
609         if ((item->wk_state & ONWORKLIST) == 0)
610                 panic("worklist_remove: not on list");
611         item->wk_state &= ~ONWORKLIST;
612         LIST_REMOVE(item, wk_list);
613 }
614 #endif /* DEBUG */
615
616 /*
617  * Routines for tracking and managing workitems.
618  */
619 static  void workitem_free(struct worklist *, int);
620 static  void workitem_alloc(struct worklist *, int, struct mount *);
621
622 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type))
623
624 static void
625 workitem_free(item, type)
626         struct worklist *item;
627         int type;
628 {
629         struct ufsmount *ump;
630         mtx_assert(&lk, MA_OWNED);
631
632 #ifdef DEBUG
633         if (item->wk_state & ONWORKLIST)
634                 panic("workitem_free: still on list");
635         if (item->wk_type != type)
636                 panic("workitem_free: type mismatch");
637 #endif
638         ump = VFSTOUFS(item->wk_mp);
639         if (--ump->softdep_deps == 0 && ump->softdep_req)
640                 wakeup(&ump->softdep_deps);
641         FREE(item, DtoM(type));
642 }
643
644 static void
645 workitem_alloc(item, type, mp)
646         struct worklist *item;
647         int type;
648         struct mount *mp;
649 {
650         item->wk_type = type;
651         item->wk_mp = mp;
652         item->wk_state = 0;
653         ACQUIRE_LOCK(&lk);
654         VFSTOUFS(mp)->softdep_deps++;
655         VFSTOUFS(mp)->softdep_accdeps++;
656         FREE_LOCK(&lk);
657 }
658
659 /*
660  * Workitem queue management
661  */
662 static int max_softdeps;        /* maximum number of structs before slowdown */
663 static int maxindirdeps = 50;   /* max number of indirdeps before slowdown */
664 static int tickdelay = 2;       /* number of ticks to pause during slowdown */
665 static int proc_waiting;        /* tracks whether we have a timeout posted */
666 static int *stat_countp;        /* statistic to count in proc_waiting timeout */
667 static struct callout_handle handle; /* handle on posted proc_waiting timeout */
668 static int req_pending;
669 static int req_clear_inodedeps; /* syncer process flush some inodedeps */
670 #define FLUSH_INODES            1
671 static int req_clear_remove;    /* syncer process flush some freeblks */
672 #define FLUSH_REMOVE            2
673 #define FLUSH_REMOVE_WAIT       3
674 /*
675  * runtime statistics
676  */
677 static int stat_worklist_push;  /* number of worklist cleanups */
678 static int stat_blk_limit_push; /* number of times block limit neared */
679 static int stat_ino_limit_push; /* number of times inode limit neared */
680 static int stat_blk_limit_hit;  /* number of times block slowdown imposed */
681 static int stat_ino_limit_hit;  /* number of times inode slowdown imposed */
682 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */
683 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */
684 static int stat_inode_bitmap;   /* bufs redirtied as inode bitmap not written */
685 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */
686 static int stat_dir_entry;      /* bufs redirtied as dir entry cannot write */
687
688 SYSCTL_INT(_debug, OID_AUTO, max_softdeps, CTLFLAG_RW, &max_softdeps, 0, "");
689 SYSCTL_INT(_debug, OID_AUTO, tickdelay, CTLFLAG_RW, &tickdelay, 0, "");
690 SYSCTL_INT(_debug, OID_AUTO, maxindirdeps, CTLFLAG_RW, &maxindirdeps, 0, "");
691 SYSCTL_INT(_debug, OID_AUTO, worklist_push, CTLFLAG_RW, &stat_worklist_push, 0,"");
692 SYSCTL_INT(_debug, OID_AUTO, blk_limit_push, CTLFLAG_RW, &stat_blk_limit_push, 0,"");
693 SYSCTL_INT(_debug, OID_AUTO, ino_limit_push, CTLFLAG_RW, &stat_ino_limit_push, 0,"");
694 SYSCTL_INT(_debug, OID_AUTO, blk_limit_hit, CTLFLAG_RW, &stat_blk_limit_hit, 0, "");
695 SYSCTL_INT(_debug, OID_AUTO, ino_limit_hit, CTLFLAG_RW, &stat_ino_limit_hit, 0, "");
696 SYSCTL_INT(_debug, OID_AUTO, sync_limit_hit, CTLFLAG_RW, &stat_sync_limit_hit, 0, "");
697 SYSCTL_INT(_debug, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, &stat_indir_blk_ptrs, 0, "");
698 SYSCTL_INT(_debug, OID_AUTO, inode_bitmap, CTLFLAG_RW, &stat_inode_bitmap, 0, "");
699 SYSCTL_INT(_debug, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, &stat_direct_blk_ptrs, 0, "");
700 SYSCTL_INT(_debug, OID_AUTO, dir_entry, CTLFLAG_RW, &stat_dir_entry, 0, "");
701 /* SYSCTL_INT(_debug, OID_AUTO, worklist_num, CTLFLAG_RD, &softdep_on_worklist, 0, ""); */
702
703 SYSCTL_DECL(_vfs_ffs);
704
705 static int compute_summary_at_mount = 0;        /* Whether to recompute the summary at mount time */
706 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW,
707            &compute_summary_at_mount, 0, "Recompute summary at mount");
708
709 static struct proc *softdepproc;
710 static struct kproc_desc softdep_kp = {
711         "softdepflush",
712         softdep_flush,
713         &softdepproc
714 };
715 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, &softdep_kp)
716
717 static void
718 softdep_flush(void)
719 {
720         struct mount *nmp;
721         struct mount *mp;
722         struct ufsmount *ump;
723         struct thread *td;
724         int remaining;
725         int vfslocked;
726
727         td = curthread;
728         td->td_pflags |= TDP_NORUNNINGBUF;
729
730         for (;;) {      
731                 kproc_suspend_check(softdepproc);
732                 vfslocked = VFS_LOCK_GIANT((struct mount *)NULL);
733                 ACQUIRE_LOCK(&lk);
734                 /*
735                  * If requested, try removing inode or removal dependencies.
736                  */
737                 if (req_clear_inodedeps) {
738                         clear_inodedeps(td);
739                         req_clear_inodedeps -= 1;
740                         wakeup_one(&proc_waiting);
741                 }
742                 if (req_clear_remove) {
743                         clear_remove(td);
744                         req_clear_remove -= 1;
745                         wakeup_one(&proc_waiting);
746                 }
747                 FREE_LOCK(&lk);
748                 VFS_UNLOCK_GIANT(vfslocked);
749                 remaining = 0;
750                 mtx_lock(&mountlist_mtx);
751                 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp)  {
752                         nmp = TAILQ_NEXT(mp, mnt_list);
753                         if ((mp->mnt_flag & MNT_SOFTDEP) == 0)
754                                 continue;
755                         if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
756                                 continue;
757                         vfslocked = VFS_LOCK_GIANT(mp);
758                         softdep_process_worklist(mp, 0);
759                         ump = VFSTOUFS(mp);
760                         remaining += ump->softdep_on_worklist -
761                                 ump->softdep_on_worklist_inprogress;
762                         VFS_UNLOCK_GIANT(vfslocked);
763                         mtx_lock(&mountlist_mtx);
764                         nmp = TAILQ_NEXT(mp, mnt_list);
765                         vfs_unbusy(mp, td);
766                 }
767                 mtx_unlock(&mountlist_mtx);
768                 if (remaining)
769                         continue;
770                 ACQUIRE_LOCK(&lk);
771                 if (!req_pending)
772                         msleep(&req_pending, &lk, PVM, "sdflush", hz);
773                 req_pending = 0;
774                 FREE_LOCK(&lk);
775         }
776 }
777
778 static int
779 softdep_speedup(void)
780 {
781
782         mtx_assert(&lk, MA_OWNED);
783         if (req_pending == 0) {
784                 req_pending = 1;
785                 wakeup(&req_pending);
786         }
787
788         return speedup_syncer();
789 }
790
791 /*
792  * Add an item to the end of the work queue.
793  * This routine requires that the lock be held.
794  * This is the only routine that adds items to the list.
795  * The following routine is the only one that removes items
796  * and does so in order from first to last.
797  */
798 static void
799 add_to_worklist(wk)
800         struct worklist *wk;
801 {
802         struct ufsmount *ump;
803
804         mtx_assert(&lk, MA_OWNED);
805         ump = VFSTOUFS(wk->wk_mp);
806         if (wk->wk_state & ONWORKLIST)
807                 panic("add_to_worklist: already on list");
808         wk->wk_state |= ONWORKLIST;
809         if (LIST_EMPTY(&ump->softdep_workitem_pending))
810                 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list);
811         else
812                 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list);
813         ump->softdep_worklist_tail = wk;
814         ump->softdep_on_worklist += 1;
815 }
816
817 /*
818  * Process that runs once per second to handle items in the background queue.
819  *
820  * Note that we ensure that everything is done in the order in which they
821  * appear in the queue. The code below depends on this property to ensure
822  * that blocks of a file are freed before the inode itself is freed. This
823  * ordering ensures that no new <vfsid, inum, lbn> triples will be generated
824  * until all the old ones have been purged from the dependency lists.
825  */
826 int 
827 softdep_process_worklist(mp, full)
828         struct mount *mp;
829         int full;
830 {
831         struct thread *td = curthread;
832         int cnt, matchcnt, loopcount;
833         struct ufsmount *ump;
834         long starttime;
835
836         KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp"));
837         /*
838          * Record the process identifier of our caller so that we can give
839          * this process preferential treatment in request_cleanup below.
840          */
841         matchcnt = 0;
842         ump = VFSTOUFS(mp);
843         ACQUIRE_LOCK(&lk);
844         loopcount = 1;
845         starttime = time_second;
846         while (ump->softdep_on_worklist > 0) {
847                 if ((cnt = process_worklist_item(mp, 0)) == -1)
848                         break;
849                 else
850                         matchcnt += cnt;
851                 /*
852                  * If requested, try removing inode or removal dependencies.
853                  */
854                 if (req_clear_inodedeps) {
855                         clear_inodedeps(td);
856                         req_clear_inodedeps -= 1;
857                         wakeup_one(&proc_waiting);
858                 }
859                 if (req_clear_remove) {
860                         clear_remove(td);
861                         req_clear_remove -= 1;
862                         wakeup_one(&proc_waiting);
863                 }
864                 /*
865                  * We do not generally want to stop for buffer space, but if
866                  * we are really being a buffer hog, we will stop and wait.
867                  */
868                 if (loopcount++ % 128 == 0) {
869                         FREE_LOCK(&lk);
870                         bwillwrite();
871                         ACQUIRE_LOCK(&lk);
872                 }
873                 /*
874                  * Never allow processing to run for more than one
875                  * second. Otherwise the other mountpoints may get
876                  * excessively backlogged.
877                  */
878                 if (!full && starttime != time_second) {
879                         matchcnt = -1;
880                         break;
881                 }
882         }
883         FREE_LOCK(&lk);
884         return (matchcnt);
885 }
886
887 /*
888  * Process one item on the worklist.
889  */
890 static int
891 process_worklist_item(mp, flags)
892         struct mount *mp;
893         int flags;
894 {
895         struct worklist *wk, *wkend;
896         struct ufsmount *ump;
897         struct vnode *vp;
898         int matchcnt = 0;
899
900         mtx_assert(&lk, MA_OWNED);
901         KASSERT(mp != NULL, ("process_worklist_item: NULL mp"));
902         /*
903          * If we are being called because of a process doing a
904          * copy-on-write, then it is not safe to write as we may
905          * recurse into the copy-on-write routine.
906          */
907         if (curthread->td_pflags & TDP_COWINPROGRESS)
908                 return (-1);
909         /*
910          * Normally we just process each item on the worklist in order.
911          * However, if we are in a situation where we cannot lock any
912          * inodes, we have to skip over any dirrem requests whose
913          * vnodes are resident and locked.
914          */
915         ump = VFSTOUFS(mp);
916         vp = NULL;
917         LIST_FOREACH(wk, &ump->softdep_workitem_pending, wk_list) {
918                 if (wk->wk_state & INPROGRESS)
919                         continue;
920                 if ((flags & LK_NOWAIT) == 0 || wk->wk_type != D_DIRREM)
921                         break;
922                 wk->wk_state |= INPROGRESS;
923                 ump->softdep_on_worklist_inprogress++;
924                 FREE_LOCK(&lk);
925                 ffs_vget(mp, WK_DIRREM(wk)->dm_oldinum,
926                     LK_NOWAIT | LK_EXCLUSIVE, &vp);
927                 ACQUIRE_LOCK(&lk);
928                 wk->wk_state &= ~INPROGRESS;
929                 ump->softdep_on_worklist_inprogress--;
930                 if (vp != NULL)
931                         break;
932         }
933         if (wk == 0)
934                 return (-1);
935         /*
936          * Remove the item to be processed. If we are removing the last
937          * item on the list, we need to recalculate the tail pointer.
938          * As this happens rarely and usually when the list is short,
939          * we just run down the list to find it rather than tracking it
940          * in the above loop.
941          */
942         WORKLIST_REMOVE(wk);
943         if (wk == ump->softdep_worklist_tail) {
944                 LIST_FOREACH(wkend, &ump->softdep_workitem_pending, wk_list)
945                         if (LIST_NEXT(wkend, wk_list) == NULL)
946                                 break;
947                 ump->softdep_worklist_tail = wkend;
948         }
949         ump->softdep_on_worklist -= 1;
950         FREE_LOCK(&lk);
951         if (vn_start_secondary_write(NULL, &mp, V_NOWAIT))
952                 panic("process_worklist_item: suspended filesystem");
953         matchcnt++;
954         switch (wk->wk_type) {
955
956         case D_DIRREM:
957                 /* removal of a directory entry */
958                 handle_workitem_remove(WK_DIRREM(wk), vp);
959                 break;
960
961         case D_FREEBLKS:
962                 /* releasing blocks and/or fragments from a file */
963                 handle_workitem_freeblocks(WK_FREEBLKS(wk), flags & LK_NOWAIT);
964                 break;
965
966         case D_FREEFRAG:
967                 /* releasing a fragment when replaced as a file grows */
968                 handle_workitem_freefrag(WK_FREEFRAG(wk));
969                 break;
970
971         case D_FREEFILE:
972                 /* releasing an inode when its link count drops to 0 */
973                 handle_workitem_freefile(WK_FREEFILE(wk));
974                 break;
975
976         default:
977                 panic("%s_process_worklist: Unknown type %s",
978                     "softdep", TYPENAME(wk->wk_type));
979                 /* NOTREACHED */
980         }
981         vn_finished_secondary_write(mp);
982         ACQUIRE_LOCK(&lk);
983         return (matchcnt);
984 }
985
986 /*
987  * Move dependencies from one buffer to another.
988  */
989 void
990 softdep_move_dependencies(oldbp, newbp)
991         struct buf *oldbp;
992         struct buf *newbp;
993 {
994         struct worklist *wk, *wktail;
995
996         if (!LIST_EMPTY(&newbp->b_dep))
997                 panic("softdep_move_dependencies: need merge code");
998         wktail = 0;
999         ACQUIRE_LOCK(&lk);
1000         while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) {
1001                 LIST_REMOVE(wk, wk_list);
1002                 if (wktail == 0)
1003                         LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list);
1004                 else
1005                         LIST_INSERT_AFTER(wktail, wk, wk_list);
1006                 wktail = wk;
1007         }
1008         FREE_LOCK(&lk);
1009 }
1010
1011 /*
1012  * Purge the work list of all items associated with a particular mount point.
1013  */
1014 int
1015 softdep_flushworklist(oldmnt, countp, td)
1016         struct mount *oldmnt;
1017         int *countp;
1018         struct thread *td;
1019 {
1020         struct vnode *devvp;
1021         int count, error = 0;
1022         struct ufsmount *ump;
1023
1024         /*
1025          * Alternately flush the block device associated with the mount
1026          * point and process any dependencies that the flushing
1027          * creates. We continue until no more worklist dependencies
1028          * are found.
1029          */
1030         *countp = 0;
1031         ump = VFSTOUFS(oldmnt);
1032         devvp = ump->um_devvp;
1033         while ((count = softdep_process_worklist(oldmnt, 1)) > 0) {
1034                 *countp += count;
1035                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1036                 error = VOP_FSYNC(devvp, MNT_WAIT, td);
1037                 VOP_UNLOCK(devvp, 0);
1038                 if (error)
1039                         break;
1040         }
1041         return (error);
1042 }
1043
1044 int
1045 softdep_waitidle(struct mount *mp)
1046 {
1047         struct ufsmount *ump;
1048         int error;
1049         int i;
1050
1051         ump = VFSTOUFS(mp);
1052         ACQUIRE_LOCK(&lk);
1053         for (i = 0; i < 10 && ump->softdep_deps; i++) {
1054                 ump->softdep_req = 1;
1055                 if (ump->softdep_on_worklist)
1056                         panic("softdep_waitidle: work added after flush.");
1057                 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1);
1058         }
1059         ump->softdep_req = 0;
1060         FREE_LOCK(&lk);
1061         error = 0;
1062         if (i == 10) {
1063                 error = EBUSY;
1064                 printf("softdep_waitidle: Failed to flush worklist for %p\n",
1065                     mp);
1066         }
1067
1068         return (error);
1069 }
1070
1071 /*
1072  * Flush all vnodes and worklist items associated with a specified mount point.
1073  */
1074 int
1075 softdep_flushfiles(oldmnt, flags, td)
1076         struct mount *oldmnt;
1077         int flags;
1078         struct thread *td;
1079 {
1080         int error, count, loopcnt;
1081
1082         error = 0;
1083
1084         /*
1085          * Alternately flush the vnodes associated with the mount
1086          * point and process any dependencies that the flushing
1087          * creates. In theory, this loop can happen at most twice,
1088          * but we give it a few extra just to be sure.
1089          */
1090         for (loopcnt = 10; loopcnt > 0; loopcnt--) {
1091                 /*
1092                  * Do another flush in case any vnodes were brought in
1093                  * as part of the cleanup operations.
1094                  */
1095                 if ((error = ffs_flushfiles(oldmnt, flags, td)) != 0)
1096                         break;
1097                 if ((error = softdep_flushworklist(oldmnt, &count, td)) != 0 ||
1098                     count == 0)
1099                         break;
1100         }
1101         /*
1102          * If we are unmounting then it is an error to fail. If we
1103          * are simply trying to downgrade to read-only, then filesystem
1104          * activity can keep us busy forever, so we just fail with EBUSY.
1105          */
1106         if (loopcnt == 0) {
1107                 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT)
1108                         panic("softdep_flushfiles: looping");
1109                 error = EBUSY;
1110         }
1111         if (!error)
1112                 error = softdep_waitidle(oldmnt);
1113         return (error);
1114 }
1115
1116 /*
1117  * Structure hashing.
1118  * 
1119  * There are three types of structures that can be looked up:
1120  *      1) pagedep structures identified by mount point, inode number,
1121  *         and logical block.
1122  *      2) inodedep structures identified by mount point and inode number.
1123  *      3) newblk structures identified by mount point and
1124  *         physical block number.
1125  *
1126  * The "pagedep" and "inodedep" dependency structures are hashed
1127  * separately from the file blocks and inodes to which they correspond.
1128  * This separation helps when the in-memory copy of an inode or
1129  * file block must be replaced. It also obviates the need to access
1130  * an inode or file page when simply updating (or de-allocating)
1131  * dependency structures. Lookup of newblk structures is needed to
1132  * find newly allocated blocks when trying to associate them with
1133  * their allocdirect or allocindir structure.
1134  *
1135  * The lookup routines optionally create and hash a new instance when
1136  * an existing entry is not found.
1137  */
1138 #define DEPALLOC        0x0001  /* allocate structure if lookup fails */
1139 #define NODELAY         0x0002  /* cannot do background work */
1140
1141 /*
1142  * Structures and routines associated with pagedep caching.
1143  */
1144 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
1145 u_long  pagedep_hash;           /* size of hash table - 1 */
1146 #define PAGEDEP_HASH(mp, inum, lbn) \
1147         (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
1148             pagedep_hash])
1149
1150 static int
1151 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp)
1152         struct pagedep_hashhead *pagedephd;
1153         ino_t ino;
1154         ufs_lbn_t lbn;
1155         struct mount *mp;
1156         int flags;
1157         struct pagedep **pagedeppp;
1158 {
1159         struct pagedep *pagedep;
1160
1161         LIST_FOREACH(pagedep, pagedephd, pd_hash)
1162                 if (ino == pagedep->pd_ino &&
1163                     lbn == pagedep->pd_lbn &&
1164                     mp == pagedep->pd_list.wk_mp)
1165                         break;
1166         if (pagedep) {
1167                 *pagedeppp = pagedep;
1168                 if ((flags & DEPALLOC) != 0 &&
1169                     (pagedep->pd_state & ONWORKLIST) == 0)
1170                         return (0);
1171                 return (1);
1172         }
1173         *pagedeppp = NULL;
1174         return (0);
1175 }
1176 /*
1177  * Look up a pagedep. Return 1 if found, 0 if not found or found
1178  * when asked to allocate but not associated with any buffer.
1179  * If not found, allocate if DEPALLOC flag is passed.
1180  * Found or allocated entry is returned in pagedeppp.
1181  * This routine must be called with splbio interrupts blocked.
1182  */
1183 static int
1184 pagedep_lookup(ip, lbn, flags, pagedeppp)
1185         struct inode *ip;
1186         ufs_lbn_t lbn;
1187         int flags;
1188         struct pagedep **pagedeppp;
1189 {
1190         struct pagedep *pagedep;
1191         struct pagedep_hashhead *pagedephd;
1192         struct mount *mp;
1193         int ret;
1194         int i;
1195
1196         mtx_assert(&lk, MA_OWNED);
1197         mp = ITOV(ip)->v_mount;
1198         pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
1199
1200         ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1201         if (*pagedeppp || (flags & DEPALLOC) == 0)
1202                 return (ret);
1203         FREE_LOCK(&lk);
1204         MALLOC(pagedep, struct pagedep *, sizeof(struct pagedep),
1205             M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO);
1206         workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp);
1207         ACQUIRE_LOCK(&lk);
1208         ret = pagedep_find(pagedephd, ip->i_number, lbn, mp, flags, pagedeppp);
1209         if (*pagedeppp) {
1210                 WORKITEM_FREE(pagedep, D_PAGEDEP);
1211                 return (ret);
1212         }
1213         pagedep->pd_ino = ip->i_number;
1214         pagedep->pd_lbn = lbn;
1215         LIST_INIT(&pagedep->pd_dirremhd);
1216         LIST_INIT(&pagedep->pd_pendinghd);
1217         for (i = 0; i < DAHASHSZ; i++)
1218                 LIST_INIT(&pagedep->pd_diraddhd[i]);
1219         LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash);
1220         *pagedeppp = pagedep;
1221         return (0);
1222 }
1223
1224 /*
1225  * Structures and routines associated with inodedep caching.
1226  */
1227 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
1228 static u_long   inodedep_hash;  /* size of hash table - 1 */
1229 static long     num_inodedep;   /* number of inodedep allocated */
1230 #define INODEDEP_HASH(fs, inum) \
1231       (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash])
1232
1233 static int
1234 inodedep_find(inodedephd, fs, inum, inodedeppp)
1235         struct inodedep_hashhead *inodedephd;
1236         struct fs *fs;
1237         ino_t inum;
1238         struct inodedep **inodedeppp;
1239 {
1240         struct inodedep *inodedep;
1241
1242         LIST_FOREACH(inodedep, inodedephd, id_hash)
1243                 if (inum == inodedep->id_ino && fs == inodedep->id_fs)
1244                         break;
1245         if (inodedep) {
1246                 *inodedeppp = inodedep;
1247                 return (1);
1248         }
1249         *inodedeppp = NULL;
1250
1251         return (0);
1252 }
1253 /*
1254  * Look up an inodedep. Return 1 if found, 0 if not found.
1255  * If not found, allocate if DEPALLOC flag is passed.
1256  * Found or allocated entry is returned in inodedeppp.
1257  * This routine must be called with splbio interrupts blocked.
1258  */
1259 static int
1260 inodedep_lookup(mp, inum, flags, inodedeppp)
1261         struct mount *mp;
1262         ino_t inum;
1263         int flags;
1264         struct inodedep **inodedeppp;
1265 {
1266         struct inodedep *inodedep;
1267         struct inodedep_hashhead *inodedephd;
1268         struct fs *fs;
1269
1270         mtx_assert(&lk, MA_OWNED);
1271         fs = VFSTOUFS(mp)->um_fs;
1272         inodedephd = INODEDEP_HASH(fs, inum);
1273
1274         if (inodedep_find(inodedephd, fs, inum, inodedeppp))
1275                 return (1);
1276         if ((flags & DEPALLOC) == 0)
1277                 return (0);
1278         /*
1279          * If we are over our limit, try to improve the situation.
1280          */
1281         if (num_inodedep > max_softdeps && (flags & NODELAY) == 0)
1282                 request_cleanup(mp, FLUSH_INODES);
1283         FREE_LOCK(&lk);
1284         MALLOC(inodedep, struct inodedep *, sizeof(struct inodedep),
1285                 M_INODEDEP, M_SOFTDEP_FLAGS);
1286         workitem_alloc(&inodedep->id_list, D_INODEDEP, mp);
1287         ACQUIRE_LOCK(&lk);
1288         if (inodedep_find(inodedephd, fs, inum, inodedeppp)) {
1289                 WORKITEM_FREE(inodedep, D_INODEDEP);
1290                 return (1);
1291         }
1292         num_inodedep += 1;
1293         inodedep->id_fs = fs;
1294         inodedep->id_ino = inum;
1295         inodedep->id_state = ALLCOMPLETE;
1296         inodedep->id_nlinkdelta = 0;
1297         inodedep->id_savedino1 = NULL;
1298         inodedep->id_savedsize = -1;
1299         inodedep->id_savedextsize = -1;
1300         inodedep->id_buf = NULL;
1301         LIST_INIT(&inodedep->id_pendinghd);
1302         LIST_INIT(&inodedep->id_inowait);
1303         LIST_INIT(&inodedep->id_bufwait);
1304         TAILQ_INIT(&inodedep->id_inoupdt);
1305         TAILQ_INIT(&inodedep->id_newinoupdt);
1306         TAILQ_INIT(&inodedep->id_extupdt);
1307         TAILQ_INIT(&inodedep->id_newextupdt);
1308         LIST_INSERT_HEAD(inodedephd, inodedep, id_hash);
1309         *inodedeppp = inodedep;
1310         return (0);
1311 }
1312
1313 /*
1314  * Structures and routines associated with newblk caching.
1315  */
1316 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
1317 u_long  newblk_hash;            /* size of hash table - 1 */
1318 #define NEWBLK_HASH(fs, inum) \
1319         (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
1320
1321 static int
1322 newblk_find(newblkhd, fs, newblkno, newblkpp)
1323         struct newblk_hashhead *newblkhd;
1324         struct fs *fs;
1325         ufs2_daddr_t newblkno;
1326         struct newblk **newblkpp;
1327 {
1328         struct newblk *newblk;
1329
1330         LIST_FOREACH(newblk, newblkhd, nb_hash)
1331                 if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
1332                         break;
1333         if (newblk) {
1334                 *newblkpp = newblk;
1335                 return (1);
1336         }
1337         *newblkpp = NULL;
1338         return (0);
1339 }
1340
1341 /*
1342  * Look up a newblk. Return 1 if found, 0 if not found.
1343  * If not found, allocate if DEPALLOC flag is passed.
1344  * Found or allocated entry is returned in newblkpp.
1345  */
1346 static int
1347 newblk_lookup(fs, newblkno, flags, newblkpp)
1348         struct fs *fs;
1349         ufs2_daddr_t newblkno;
1350         int flags;
1351         struct newblk **newblkpp;
1352 {
1353         struct newblk *newblk;
1354         struct newblk_hashhead *newblkhd;
1355
1356         newblkhd = NEWBLK_HASH(fs, newblkno);
1357         if (newblk_find(newblkhd, fs, newblkno, newblkpp))
1358                 return (1);
1359         if ((flags & DEPALLOC) == 0)
1360                 return (0);
1361         FREE_LOCK(&lk);
1362         MALLOC(newblk, struct newblk *, sizeof(struct newblk),
1363                 M_NEWBLK, M_SOFTDEP_FLAGS);
1364         ACQUIRE_LOCK(&lk);
1365         if (newblk_find(newblkhd, fs, newblkno, newblkpp)) {
1366                 FREE(newblk, M_NEWBLK);
1367                 return (1);
1368         }
1369         newblk->nb_state = 0;
1370         newblk->nb_fs = fs;
1371         newblk->nb_newblkno = newblkno;
1372         LIST_INSERT_HEAD(newblkhd, newblk, nb_hash);
1373         *newblkpp = newblk;
1374         return (0);
1375 }
1376
1377 /*
1378  * Executed during filesystem system initialization before
1379  * mounting any filesystems.
1380  */
1381 void 
1382 softdep_initialize()
1383 {
1384
1385         LIST_INIT(&mkdirlisthd);
1386         max_softdeps = desiredvnodes * 4;
1387         pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP,
1388             &pagedep_hash);
1389         inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash);
1390         newblk_hashtbl = hashinit(64, M_NEWBLK, &newblk_hash);
1391
1392         /* initialise bioops hack */
1393         bioops.io_start = softdep_disk_io_initiation;
1394         bioops.io_complete = softdep_disk_write_complete;
1395         bioops.io_deallocate = softdep_deallocate_dependencies;
1396         bioops.io_countdeps = softdep_count_dependencies;
1397 }
1398
1399 /*
1400  * Executed after all filesystems have been unmounted during
1401  * filesystem module unload.
1402  */
1403 void
1404 softdep_uninitialize()
1405 {
1406
1407         hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash);
1408         hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash);
1409         hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash);
1410 }
1411
1412 /*
1413  * Called at mount time to notify the dependency code that a
1414  * filesystem wishes to use it.
1415  */
1416 int
1417 softdep_mount(devvp, mp, fs, cred)
1418         struct vnode *devvp;
1419         struct mount *mp;
1420         struct fs *fs;
1421         struct ucred *cred;
1422 {
1423         struct csum_total cstotal;
1424         struct ufsmount *ump;
1425         struct cg *cgp;
1426         struct buf *bp;
1427         int error, cyl;
1428
1429         MNT_ILOCK(mp);
1430         mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP;
1431         if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) {
1432                 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 
1433                         MNTK_SOFTDEP;
1434                 mp->mnt_noasync++;
1435         }
1436         MNT_IUNLOCK(mp);
1437         ump = VFSTOUFS(mp);
1438         LIST_INIT(&ump->softdep_workitem_pending);
1439         ump->softdep_worklist_tail = NULL;
1440         ump->softdep_on_worklist = 0;
1441         ump->softdep_deps = 0;
1442         /*
1443          * When doing soft updates, the counters in the
1444          * superblock may have gotten out of sync. Recomputation
1445          * can take a long time and can be deferred for background
1446          * fsck.  However, the old behavior of scanning the cylinder
1447          * groups and recalculating them at mount time is available
1448          * by setting vfs.ffs.compute_summary_at_mount to one.
1449          */
1450         if (compute_summary_at_mount == 0 || fs->fs_clean != 0)
1451                 return (0);
1452         bzero(&cstotal, sizeof cstotal);
1453         for (cyl = 0; cyl < fs->fs_ncg; cyl++) {
1454                 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)),
1455                     fs->fs_cgsize, cred, &bp)) != 0) {
1456                         brelse(bp);
1457                         return (error);
1458                 }
1459                 cgp = (struct cg *)bp->b_data;
1460                 cstotal.cs_nffree += cgp->cg_cs.cs_nffree;
1461                 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree;
1462                 cstotal.cs_nifree += cgp->cg_cs.cs_nifree;
1463                 cstotal.cs_ndir += cgp->cg_cs.cs_ndir;
1464                 fs->fs_cs(fs, cyl) = cgp->cg_cs;
1465                 brelse(bp);
1466         }
1467 #ifdef DEBUG
1468         if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal))
1469                 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt);
1470 #endif
1471         bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal);
1472         return (0);
1473 }
1474
1475 /*
1476  * Protecting the freemaps (or bitmaps).
1477  * 
1478  * To eliminate the need to execute fsck before mounting a filesystem
1479  * after a power failure, one must (conservatively) guarantee that the
1480  * on-disk copy of the bitmaps never indicate that a live inode or block is
1481  * free.  So, when a block or inode is allocated, the bitmap should be
1482  * updated (on disk) before any new pointers.  When a block or inode is
1483  * freed, the bitmap should not be updated until all pointers have been
1484  * reset.  The latter dependency is handled by the delayed de-allocation
1485  * approach described below for block and inode de-allocation.  The former
1486  * dependency is handled by calling the following procedure when a block or
1487  * inode is allocated. When an inode is allocated an "inodedep" is created
1488  * with its DEPCOMPLETE flag cleared until its bitmap is written to disk.
1489  * Each "inodedep" is also inserted into the hash indexing structure so
1490  * that any additional link additions can be made dependent on the inode
1491  * allocation.
1492  * 
1493  * The ufs filesystem maintains a number of free block counts (e.g., per
1494  * cylinder group, per cylinder and per <cylinder, rotational position> pair)
1495  * in addition to the bitmaps.  These counts are used to improve efficiency
1496  * during allocation and therefore must be consistent with the bitmaps.
1497  * There is no convenient way to guarantee post-crash consistency of these
1498  * counts with simple update ordering, for two main reasons: (1) The counts
1499  * and bitmaps for a single cylinder group block are not in the same disk
1500  * sector.  If a disk write is interrupted (e.g., by power failure), one may
1501  * be written and the other not.  (2) Some of the counts are located in the
1502  * superblock rather than the cylinder group block. So, we focus our soft
1503  * updates implementation on protecting the bitmaps. When mounting a
1504  * filesystem, we recompute the auxiliary counts from the bitmaps.
1505  */
1506
1507 /*
1508  * Called just after updating the cylinder group block to allocate an inode.
1509  */
1510 void
1511 softdep_setup_inomapdep(bp, ip, newinum)
1512         struct buf *bp;         /* buffer for cylgroup block with inode map */
1513         struct inode *ip;       /* inode related to allocation */
1514         ino_t newinum;          /* new inode number being allocated */
1515 {
1516         struct inodedep *inodedep;
1517         struct bmsafemap *bmsafemap;
1518
1519         /*
1520          * Create a dependency for the newly allocated inode.
1521          * Panic if it already exists as something is seriously wrong.
1522          * Otherwise add it to the dependency list for the buffer holding
1523          * the cylinder group map from which it was allocated.
1524          */
1525         ACQUIRE_LOCK(&lk);
1526         if ((inodedep_lookup(UFSTOVFS(ip->i_ump), newinum, DEPALLOC|NODELAY,
1527             &inodedep)))
1528                 panic("softdep_setup_inomapdep: dependency for new inode "
1529                     "already exists");
1530         inodedep->id_buf = bp;
1531         inodedep->id_state &= ~DEPCOMPLETE;
1532         bmsafemap = bmsafemap_lookup(inodedep->id_list.wk_mp, bp);
1533         LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps);
1534         FREE_LOCK(&lk);
1535 }
1536
1537 /*
1538  * Called just after updating the cylinder group block to
1539  * allocate block or fragment.
1540  */
1541 void
1542 softdep_setup_blkmapdep(bp, mp, newblkno)
1543         struct buf *bp;         /* buffer for cylgroup block with block map */
1544         struct mount *mp;       /* filesystem doing allocation */
1545         ufs2_daddr_t newblkno;  /* number of newly allocated block */
1546 {
1547         struct newblk *newblk;
1548         struct bmsafemap *bmsafemap;
1549         struct fs *fs;
1550
1551         fs = VFSTOUFS(mp)->um_fs;
1552         /*
1553          * Create a dependency for the newly allocated block.
1554          * Add it to the dependency list for the buffer holding
1555          * the cylinder group map from which it was allocated.
1556          */
1557         ACQUIRE_LOCK(&lk);
1558         if (newblk_lookup(fs, newblkno, DEPALLOC, &newblk) != 0)
1559                 panic("softdep_setup_blkmapdep: found block");
1560         newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp);
1561         LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps);
1562         FREE_LOCK(&lk);
1563 }
1564
1565 /*
1566  * Find the bmsafemap associated with a cylinder group buffer.
1567  * If none exists, create one. The buffer must be locked when
1568  * this routine is called and this routine must be called with
1569  * splbio interrupts blocked.
1570  */
1571 static struct bmsafemap *
1572 bmsafemap_lookup(mp, bp)
1573         struct mount *mp;
1574         struct buf *bp;
1575 {
1576         struct bmsafemap *bmsafemap;
1577         struct worklist *wk;
1578
1579         mtx_assert(&lk, MA_OWNED);
1580         LIST_FOREACH(wk, &bp->b_dep, wk_list)
1581                 if (wk->wk_type == D_BMSAFEMAP)
1582                         return (WK_BMSAFEMAP(wk));
1583         FREE_LOCK(&lk);
1584         MALLOC(bmsafemap, struct bmsafemap *, sizeof(struct bmsafemap),
1585                 M_BMSAFEMAP, M_SOFTDEP_FLAGS);
1586         workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp);
1587         bmsafemap->sm_buf = bp;
1588         LIST_INIT(&bmsafemap->sm_allocdirecthd);
1589         LIST_INIT(&bmsafemap->sm_allocindirhd);
1590         LIST_INIT(&bmsafemap->sm_inodedephd);
1591         LIST_INIT(&bmsafemap->sm_newblkhd);
1592         ACQUIRE_LOCK(&lk);
1593         WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list);
1594         return (bmsafemap);
1595 }
1596
1597 /*
1598  * Direct block allocation dependencies.
1599  * 
1600  * When a new block is allocated, the corresponding disk locations must be
1601  * initialized (with zeros or new data) before the on-disk inode points to
1602  * them.  Also, the freemap from which the block was allocated must be
1603  * updated (on disk) before the inode's pointer. These two dependencies are
1604  * independent of each other and are needed for all file blocks and indirect
1605  * blocks that are pointed to directly by the inode.  Just before the
1606  * "in-core" version of the inode is updated with a newly allocated block
1607  * number, a procedure (below) is called to setup allocation dependency
1608  * structures.  These structures are removed when the corresponding
1609  * dependencies are satisfied or when the block allocation becomes obsolete
1610  * (i.e., the file is deleted, the block is de-allocated, or the block is a
1611  * fragment that gets upgraded).  All of these cases are handled in
1612  * procedures described later.
1613  * 
1614  * When a file extension causes a fragment to be upgraded, either to a larger
1615  * fragment or to a full block, the on-disk location may change (if the
1616  * previous fragment could not simply be extended). In this case, the old
1617  * fragment must be de-allocated, but not until after the inode's pointer has
1618  * been updated. In most cases, this is handled by later procedures, which
1619  * will construct a "freefrag" structure to be added to the workitem queue
1620  * when the inode update is complete (or obsolete).  The main exception to
1621  * this is when an allocation occurs while a pending allocation dependency
1622  * (for the same block pointer) remains.  This case is handled in the main
1623  * allocation dependency setup procedure by immediately freeing the
1624  * unreferenced fragments.
1625  */ 
1626 void 
1627 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1628         struct inode *ip;       /* inode to which block is being added */
1629         ufs_lbn_t lbn;          /* block pointer within inode */
1630         ufs2_daddr_t newblkno;  /* disk block number being added */
1631         ufs2_daddr_t oldblkno;  /* previous block number, 0 unless frag */
1632         long newsize;           /* size of new block */
1633         long oldsize;           /* size of new block */
1634         struct buf *bp;         /* bp for allocated block */
1635 {
1636         struct allocdirect *adp, *oldadp;
1637         struct allocdirectlst *adphead;
1638         struct bmsafemap *bmsafemap;
1639         struct inodedep *inodedep;
1640         struct pagedep *pagedep;
1641         struct newblk *newblk;
1642         struct mount *mp;
1643
1644         mp = UFSTOVFS(ip->i_ump);
1645         MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1646                 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1647         workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1648         adp->ad_lbn = lbn;
1649         adp->ad_newblkno = newblkno;
1650         adp->ad_oldblkno = oldblkno;
1651         adp->ad_newsize = newsize;
1652         adp->ad_oldsize = oldsize;
1653         adp->ad_state = ATTACHED;
1654         LIST_INIT(&adp->ad_newdirblk);
1655         if (newblkno == oldblkno)
1656                 adp->ad_freefrag = NULL;
1657         else
1658                 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1659
1660         ACQUIRE_LOCK(&lk);
1661         if (lbn >= NDADDR) {
1662                 /* allocating an indirect block */
1663                 if (oldblkno != 0)
1664                         panic("softdep_setup_allocdirect: non-zero indir");
1665         } else {
1666                 /*
1667                  * Allocating a direct block.
1668                  *
1669                  * If we are allocating a directory block, then we must
1670                  * allocate an associated pagedep to track additions and
1671                  * deletions.
1672                  */
1673                 if ((ip->i_mode & IFMT) == IFDIR &&
1674                     pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
1675                         WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
1676         }
1677         if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1678                 panic("softdep_setup_allocdirect: lost block");
1679         if (newblk->nb_state == DEPCOMPLETE) {
1680                 adp->ad_state |= DEPCOMPLETE;
1681                 adp->ad_buf = NULL;
1682         } else {
1683                 bmsafemap = newblk->nb_bmsafemap;
1684                 adp->ad_buf = bmsafemap->sm_buf;
1685                 LIST_REMOVE(newblk, nb_deps);
1686                 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1687         }
1688         LIST_REMOVE(newblk, nb_hash);
1689         FREE(newblk, M_NEWBLK);
1690
1691         inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1692         adp->ad_inodedep = inodedep;
1693         WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1694         /*
1695          * The list of allocdirects must be kept in sorted and ascending
1696          * order so that the rollback routines can quickly determine the
1697          * first uncommitted block (the size of the file stored on disk
1698          * ends at the end of the lowest committed fragment, or if there
1699          * are no fragments, at the end of the highest committed block).
1700          * Since files generally grow, the typical case is that the new
1701          * block is to be added at the end of the list. We speed this
1702          * special case by checking against the last allocdirect in the
1703          * list before laboriously traversing the list looking for the
1704          * insertion point.
1705          */
1706         adphead = &inodedep->id_newinoupdt;
1707         oldadp = TAILQ_LAST(adphead, allocdirectlst);
1708         if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1709                 /* insert at end of list */
1710                 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1711                 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1712                         allocdirect_merge(adphead, adp, oldadp);
1713                 FREE_LOCK(&lk);
1714                 return;
1715         }
1716         TAILQ_FOREACH(oldadp, adphead, ad_next) {
1717                 if (oldadp->ad_lbn >= lbn)
1718                         break;
1719         }
1720         if (oldadp == NULL)
1721                 panic("softdep_setup_allocdirect: lost entry");
1722         /* insert in middle of list */
1723         TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1724         if (oldadp->ad_lbn == lbn)
1725                 allocdirect_merge(adphead, adp, oldadp);
1726         FREE_LOCK(&lk);
1727 }
1728
1729 /*
1730  * Replace an old allocdirect dependency with a newer one.
1731  * This routine must be called with splbio interrupts blocked.
1732  */
1733 static void
1734 allocdirect_merge(adphead, newadp, oldadp)
1735         struct allocdirectlst *adphead; /* head of list holding allocdirects */
1736         struct allocdirect *newadp;     /* allocdirect being added */
1737         struct allocdirect *oldadp;     /* existing allocdirect being checked */
1738 {
1739         struct worklist *wk;
1740         struct freefrag *freefrag;
1741         struct newdirblk *newdirblk;
1742
1743         mtx_assert(&lk, MA_OWNED);
1744         if (newadp->ad_oldblkno != oldadp->ad_newblkno ||
1745             newadp->ad_oldsize != oldadp->ad_newsize ||
1746             newadp->ad_lbn >= NDADDR)
1747                 panic("%s %jd != new %jd || old size %ld != new %ld",
1748                     "allocdirect_merge: old blkno",
1749                     (intmax_t)newadp->ad_oldblkno,
1750                     (intmax_t)oldadp->ad_newblkno,
1751                     newadp->ad_oldsize, oldadp->ad_newsize);
1752         newadp->ad_oldblkno = oldadp->ad_oldblkno;
1753         newadp->ad_oldsize = oldadp->ad_oldsize;
1754         /*
1755          * If the old dependency had a fragment to free or had never
1756          * previously had a block allocated, then the new dependency
1757          * can immediately post its freefrag and adopt the old freefrag.
1758          * This action is done by swapping the freefrag dependencies.
1759          * The new dependency gains the old one's freefrag, and the
1760          * old one gets the new one and then immediately puts it on
1761          * the worklist when it is freed by free_allocdirect. It is
1762          * not possible to do this swap when the old dependency had a
1763          * non-zero size but no previous fragment to free. This condition
1764          * arises when the new block is an extension of the old block.
1765          * Here, the first part of the fragment allocated to the new
1766          * dependency is part of the block currently claimed on disk by
1767          * the old dependency, so cannot legitimately be freed until the
1768          * conditions for the new dependency are fulfilled.
1769          */
1770         if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) {
1771                 freefrag = newadp->ad_freefrag;
1772                 newadp->ad_freefrag = oldadp->ad_freefrag;
1773                 oldadp->ad_freefrag = freefrag;
1774         }
1775         /*
1776          * If we are tracking a new directory-block allocation,
1777          * move it from the old allocdirect to the new allocdirect.
1778          */
1779         if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) {
1780                 newdirblk = WK_NEWDIRBLK(wk);
1781                 WORKLIST_REMOVE(&newdirblk->db_list);
1782                 if (!LIST_EMPTY(&oldadp->ad_newdirblk))
1783                         panic("allocdirect_merge: extra newdirblk");
1784                 WORKLIST_INSERT(&newadp->ad_newdirblk, &newdirblk->db_list);
1785         }
1786         free_allocdirect(adphead, oldadp, 0);
1787 }
1788                 
1789 /*
1790  * Allocate a new freefrag structure if needed.
1791  */
1792 static struct freefrag *
1793 newfreefrag(ip, blkno, size)
1794         struct inode *ip;
1795         ufs2_daddr_t blkno;
1796         long size;
1797 {
1798         struct freefrag *freefrag;
1799         struct fs *fs;
1800
1801         if (blkno == 0)
1802                 return (NULL);
1803         fs = ip->i_fs;
1804         if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag)
1805                 panic("newfreefrag: frag size");
1806         MALLOC(freefrag, struct freefrag *, sizeof(struct freefrag),
1807                 M_FREEFRAG, M_SOFTDEP_FLAGS);
1808         workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump));
1809         freefrag->ff_inum = ip->i_number;
1810         freefrag->ff_blkno = blkno;
1811         freefrag->ff_fragsize = size;
1812         return (freefrag);
1813 }
1814
1815 /*
1816  * This workitem de-allocates fragments that were replaced during
1817  * file block allocation.
1818  */
1819 static void 
1820 handle_workitem_freefrag(freefrag)
1821         struct freefrag *freefrag;
1822 {
1823         struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp);
1824
1825         ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno,
1826             freefrag->ff_fragsize, freefrag->ff_inum);
1827         ACQUIRE_LOCK(&lk);
1828         WORKITEM_FREE(freefrag, D_FREEFRAG);
1829         FREE_LOCK(&lk);
1830 }
1831
1832 /*
1833  * Set up a dependency structure for an external attributes data block.
1834  * This routine follows much of the structure of softdep_setup_allocdirect.
1835  * See the description of softdep_setup_allocdirect above for details.
1836  */
1837 void 
1838 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp)
1839         struct inode *ip;
1840         ufs_lbn_t lbn;
1841         ufs2_daddr_t newblkno;
1842         ufs2_daddr_t oldblkno;
1843         long newsize;
1844         long oldsize;
1845         struct buf *bp;
1846 {
1847         struct allocdirect *adp, *oldadp;
1848         struct allocdirectlst *adphead;
1849         struct bmsafemap *bmsafemap;
1850         struct inodedep *inodedep;
1851         struct newblk *newblk;
1852         struct mount *mp;
1853
1854         mp = UFSTOVFS(ip->i_ump);
1855         MALLOC(adp, struct allocdirect *, sizeof(struct allocdirect),
1856                 M_ALLOCDIRECT, M_SOFTDEP_FLAGS|M_ZERO);
1857         workitem_alloc(&adp->ad_list, D_ALLOCDIRECT, mp);
1858         adp->ad_lbn = lbn;
1859         adp->ad_newblkno = newblkno;
1860         adp->ad_oldblkno = oldblkno;
1861         adp->ad_newsize = newsize;
1862         adp->ad_oldsize = oldsize;
1863         adp->ad_state = ATTACHED | EXTDATA;
1864         LIST_INIT(&adp->ad_newdirblk);
1865         if (newblkno == oldblkno)
1866                 adp->ad_freefrag = NULL;
1867         else
1868                 adp->ad_freefrag = newfreefrag(ip, oldblkno, oldsize);
1869
1870         ACQUIRE_LOCK(&lk);
1871         if (newblk_lookup(ip->i_fs, newblkno, 0, &newblk) == 0)
1872                 panic("softdep_setup_allocext: lost block");
1873
1874         inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep);
1875         adp->ad_inodedep = inodedep;
1876
1877         if (newblk->nb_state == DEPCOMPLETE) {
1878                 adp->ad_state |= DEPCOMPLETE;
1879                 adp->ad_buf = NULL;
1880         } else {
1881                 bmsafemap = newblk->nb_bmsafemap;
1882                 adp->ad_buf = bmsafemap->sm_buf;
1883                 LIST_REMOVE(newblk, nb_deps);
1884                 LIST_INSERT_HEAD(&bmsafemap->sm_allocdirecthd, adp, ad_deps);
1885         }
1886         LIST_REMOVE(newblk, nb_hash);
1887         FREE(newblk, M_NEWBLK);
1888
1889         WORKLIST_INSERT(&bp->b_dep, &adp->ad_list);
1890         if (lbn >= NXADDR)
1891                 panic("softdep_setup_allocext: lbn %lld > NXADDR",
1892                     (long long)lbn);
1893         /*
1894          * The list of allocdirects must be kept in sorted and ascending
1895          * order so that the rollback routines can quickly determine the
1896          * first uncommitted block (the size of the file stored on disk
1897          * ends at the end of the lowest committed fragment, or if there
1898          * are no fragments, at the end of the highest committed block).
1899          * Since files generally grow, the typical case is that the new
1900          * block is to be added at the end of the list. We speed this
1901          * special case by checking against the last allocdirect in the
1902          * list before laboriously traversing the list looking for the
1903          * insertion point.
1904          */
1905         adphead = &inodedep->id_newextupdt;
1906         oldadp = TAILQ_LAST(adphead, allocdirectlst);
1907         if (oldadp == NULL || oldadp->ad_lbn <= lbn) {
1908                 /* insert at end of list */
1909                 TAILQ_INSERT_TAIL(adphead, adp, ad_next);
1910                 if (oldadp != NULL && oldadp->ad_lbn == lbn)
1911                         allocdirect_merge(adphead, adp, oldadp);
1912                 FREE_LOCK(&lk);
1913                 return;
1914         }
1915         TAILQ_FOREACH(oldadp, adphead, ad_next) {
1916                 if (oldadp->ad_lbn >= lbn)
1917                         break;
1918         }
1919         if (oldadp == NULL)
1920                 panic("softdep_setup_allocext: lost entry");
1921         /* insert in middle of list */
1922         TAILQ_INSERT_BEFORE(oldadp, adp, ad_next);
1923         if (oldadp->ad_lbn == lbn)
1924                 allocdirect_merge(adphead, adp, oldadp);
1925         FREE_LOCK(&lk);
1926 }
1927
1928 /*
1929  * Indirect block allocation dependencies.
1930  * 
1931  * The same dependencies that exist for a direct block also exist when
1932  * a new block is allocated and pointed to by an entry in a block of
1933  * indirect pointers. The undo/redo states described above are also
1934  * used here. Because an indirect block contains many pointers that
1935  * may have dependencies, a second copy of the entire in-memory indirect
1936  * block is kept. The buffer cache copy is always completely up-to-date.
1937  * The second copy, which is used only as a source for disk writes,
1938  * contains only the safe pointers (i.e., those that have no remaining
1939  * update dependencies). The second copy is freed when all pointers
1940  * are safe. The cache is not allowed to replace indirect blocks with
1941  * pending update dependencies. If a buffer containing an indirect
1942  * block with dependencies is written, these routines will mark it
1943  * dirty again. It can only be successfully written once all the
1944  * dependencies are removed. The ffs_fsync routine in conjunction with
1945  * softdep_sync_metadata work together to get all the dependencies
1946  * removed so that a file can be successfully written to disk. Three
1947  * procedures are used when setting up indirect block pointer
1948  * dependencies. The division is necessary because of the organization
1949  * of the "balloc" routine and because of the distinction between file
1950  * pages and file metadata blocks.
1951  */
1952
1953 /*
1954  * Allocate a new allocindir structure.
1955  */
1956 static struct allocindir *
1957 newallocindir(ip, ptrno, newblkno, oldblkno)
1958         struct inode *ip;       /* inode for file being extended */
1959         int ptrno;              /* offset of pointer in indirect block */
1960         ufs2_daddr_t newblkno;  /* disk block number being added */
1961         ufs2_daddr_t oldblkno;  /* previous block number, 0 if none */
1962 {
1963         struct allocindir *aip;
1964
1965         MALLOC(aip, struct allocindir *, sizeof(struct allocindir),
1966                 M_ALLOCINDIR, M_SOFTDEP_FLAGS|M_ZERO);
1967         workitem_alloc(&aip->ai_list, D_ALLOCINDIR, UFSTOVFS(ip->i_ump));
1968         aip->ai_state = ATTACHED;
1969         aip->ai_offset = ptrno;
1970         aip->ai_newblkno = newblkno;
1971         aip->ai_oldblkno = oldblkno;
1972         aip->ai_freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize);
1973         return (aip);
1974 }
1975
1976 /*
1977  * Called just before setting an indirect block pointer
1978  * to a newly allocated file page.
1979  */
1980 void
1981 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp)
1982         struct inode *ip;       /* inode for file being extended */
1983         ufs_lbn_t lbn;          /* allocated block number within file */
1984         struct buf *bp;         /* buffer with indirect blk referencing page */
1985         int ptrno;              /* offset of pointer in indirect block */
1986         ufs2_daddr_t newblkno;  /* disk block number being added */
1987         ufs2_daddr_t oldblkno;  /* previous block number, 0 if none */
1988         struct buf *nbp;        /* buffer holding allocated page */
1989 {
1990         struct allocindir *aip;
1991         struct pagedep *pagedep;
1992
1993         ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page");
1994         aip = newallocindir(ip, ptrno, newblkno, oldblkno);
1995         ACQUIRE_LOCK(&lk);
1996         /*
1997          * If we are allocating a directory page, then we must
1998          * allocate an associated pagedep to track additions and
1999          * deletions.
2000          */
2001         if ((ip->i_mode & IFMT) == IFDIR &&
2002             pagedep_lookup(ip, lbn, DEPALLOC, &pagedep) == 0)
2003                 WORKLIST_INSERT(&nbp->b_dep, &pagedep->pd_list);
2004         WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2005         setup_allocindir_phase2(bp, ip, aip);
2006         FREE_LOCK(&lk);
2007 }
2008
2009 /*
2010  * Called just before setting an indirect block pointer to a
2011  * newly allocated indirect block.
2012  */
2013 void
2014 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno)
2015         struct buf *nbp;        /* newly allocated indirect block */
2016         struct inode *ip;       /* inode for file being extended */
2017         struct buf *bp;         /* indirect block referencing allocated block */
2018         int ptrno;              /* offset of pointer in indirect block */
2019         ufs2_daddr_t newblkno;  /* disk block number being added */
2020 {
2021         struct allocindir *aip;
2022
2023         ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta");
2024         aip = newallocindir(ip, ptrno, newblkno, 0);
2025         ACQUIRE_LOCK(&lk);
2026         WORKLIST_INSERT(&nbp->b_dep, &aip->ai_list);
2027         setup_allocindir_phase2(bp, ip, aip);
2028         FREE_LOCK(&lk);
2029 }
2030
2031 /*
2032  * Called to finish the allocation of the "aip" allocated
2033  * by one of the two routines above.
2034  */
2035 static void 
2036 setup_allocindir_phase2(bp, ip, aip)
2037         struct buf *bp;         /* in-memory copy of the indirect block */
2038         struct inode *ip;       /* inode for file being extended */
2039         struct allocindir *aip; /* allocindir allocated by the above routines */
2040 {
2041         struct worklist *wk;
2042         struct indirdep *indirdep, *newindirdep;
2043         struct bmsafemap *bmsafemap;
2044         struct allocindir *oldaip;
2045         struct freefrag *freefrag;
2046         struct newblk *newblk;
2047         ufs2_daddr_t blkno;
2048
2049         mtx_assert(&lk, MA_OWNED);
2050         if (bp->b_lblkno >= 0)
2051                 panic("setup_allocindir_phase2: not indir blk");
2052         for (indirdep = NULL, newindirdep = NULL; ; ) {
2053                 LIST_FOREACH(wk, &bp->b_dep, wk_list) {
2054                         if (wk->wk_type != D_INDIRDEP)
2055                                 continue;
2056                         indirdep = WK_INDIRDEP(wk);
2057                         break;
2058                 }
2059                 if (indirdep == NULL && newindirdep) {
2060                         indirdep = newindirdep;
2061                         WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list);
2062                         newindirdep = NULL;
2063                 }
2064                 if (indirdep) {
2065                         if (newblk_lookup(ip->i_fs, aip->ai_newblkno, 0,
2066                             &newblk) == 0)
2067                                 panic("setup_allocindir: lost block");
2068                         if (newblk->nb_state == DEPCOMPLETE) {
2069                                 aip->ai_state |= DEPCOMPLETE;
2070                                 aip->ai_buf = NULL;
2071                         } else {
2072                                 bmsafemap = newblk->nb_bmsafemap;
2073                                 aip->ai_buf = bmsafemap->sm_buf;
2074                                 LIST_REMOVE(newblk, nb_deps);
2075                                 LIST_INSERT_HEAD(&bmsafemap->sm_allocindirhd,
2076                                     aip, ai_deps);
2077                         }
2078                         LIST_REMOVE(newblk, nb_hash);
2079                         FREE(newblk, M_NEWBLK);
2080                         aip->ai_indirdep = indirdep;
2081                         /*
2082                          * Check to see if there is an existing dependency
2083                          * for this block. If there is, merge the old
2084                          * dependency into the new one.
2085                          */
2086                         if (aip->ai_oldblkno == 0)
2087                                 oldaip = NULL;
2088                         else
2089
2090                                 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next)
2091                                         if (oldaip->ai_offset == aip->ai_offset)
2092                                                 break;
2093                         freefrag = NULL;
2094                         if (oldaip != NULL) {
2095                                 if (oldaip->ai_newblkno != aip->ai_oldblkno)
2096                                         panic("setup_allocindir_phase2: blkno");
2097                                 aip->ai_oldblkno = oldaip->ai_oldblkno;
2098                                 freefrag = aip->ai_freefrag;
2099                                 aip->ai_freefrag = oldaip->ai_freefrag;
2100                                 oldaip->ai_freefrag = NULL;
2101                                 free_allocindir(oldaip, NULL);
2102                         }
2103                         LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next);
2104                         if (ip->i_ump->um_fstype == UFS1)
2105                                 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)
2106                                     [aip->ai_offset] = aip->ai_oldblkno;
2107                         else
2108                                 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)
2109                                     [aip->ai_offset] = aip->ai_oldblkno;
2110                         FREE_LOCK(&lk);
2111                         if (freefrag != NULL)
2112                                 handle_workitem_freefrag(freefrag);
2113                 } else
2114                         FREE_LOCK(&lk);
2115                 if (newindirdep) {
2116                         newindirdep->ir_savebp->b_flags |= B_INVAL | B_NOCACHE;
2117                         brelse(newindirdep->ir_savebp);
2118                         ACQUIRE_LOCK(&lk);
2119                         WORKITEM_FREE((caddr_t)newindirdep, D_INDIRDEP);
2120                         if (indirdep)
2121                                 break;
2122                         FREE_LOCK(&lk);
2123                 }
2124                 if (indirdep) {
2125                         ACQUIRE_LOCK(&lk);
2126                         break;
2127                 }
2128                 MALLOC(newindirdep, struct indirdep *, sizeof(struct indirdep),
2129                         M_INDIRDEP, M_SOFTDEP_FLAGS);
2130                 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP,
2131                     UFSTOVFS(ip->i_ump));
2132                 newindirdep->ir_state = ATTACHED;
2133                 if (ip->i_ump->um_fstype == UFS1)
2134                         newindirdep->ir_state |= UFS1FMT;
2135                 LIST_INIT(&newindirdep->ir_deplisthd);
2136                 LIST_INIT(&newindirdep->ir_donehd);
2137                 if (bp->b_blkno == bp->b_lblkno) {
2138                         ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp,
2139                             NULL, NULL);
2140                         bp->b_blkno = blkno;
2141                 }
2142                 newindirdep->ir_savebp =
2143                     getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0);
2144                 BUF_KERNPROC(newindirdep->ir_savebp);
2145                 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount);
2146                 ACQUIRE_LOCK(&lk);
2147         }
2148 }
2149
2150 /*
2151  * Block de-allocation dependencies.
2152  * 
2153  * When blocks are de-allocated, the on-disk pointers must be nullified before
2154  * the blocks are made available for use by other files.  (The true
2155  * requirement is that old pointers must be nullified before new on-disk
2156  * pointers are set.  We chose this slightly more stringent requirement to
2157  * reduce complexity.) Our implementation handles this dependency by updating
2158  * the inode (or indirect block) appropriately but delaying the actual block
2159  * de-allocation (i.e., freemap and free space count manipulation) until
2160  * after the updated versions reach stable storage.  After the disk is
2161  * updated, the blocks can be safely de-allocated whenever it is convenient.
2162  * This implementation handles only the common case of reducing a file's
2163  * length to zero. Other cases are handled by the conventional synchronous
2164  * write approach.
2165  *
2166  * The ffs implementation with which we worked double-checks
2167  * the state of the block pointers and file size as it reduces
2168  * a file's length.  Some of this code is replicated here in our
2169  * soft updates implementation.  The freeblks->fb_chkcnt field is
2170  * used to transfer a part of this information to the procedure
2171  * that eventually de-allocates the blocks.
2172  *
2173  * This routine should be called from the routine that shortens
2174  * a file's length, before the inode's size or block pointers
2175  * are modified. It will save the block pointer information for
2176  * later release and zero the inode so that the calling routine
2177  * can release it.
2178  */
2179 void
2180 softdep_setup_freeblocks(ip, length, flags)
2181         struct inode *ip;       /* The inode whose length is to be reduced */
2182         off_t length;           /* The new length for the file */
2183         int flags;              /* IO_EXT and/or IO_NORMAL */
2184 {
2185         struct freeblks *freeblks;
2186         struct inodedep *inodedep;
2187         struct allocdirect *adp;
2188         struct vnode *vp;
2189         struct buf *bp;
2190         struct fs *fs;
2191         ufs2_daddr_t extblocks, datablocks;
2192         struct mount *mp;
2193         int i, delay, error;
2194
2195         fs = ip->i_fs;
2196         mp = UFSTOVFS(ip->i_ump);
2197         if (length != 0)
2198                 panic("softdep_setup_freeblocks: non-zero length");
2199         MALLOC(freeblks, struct freeblks *, sizeof(struct freeblks),
2200                 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO);
2201         workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp);
2202         freeblks->fb_state = ATTACHED;
2203         freeblks->fb_uid = ip->i_uid;
2204         freeblks->fb_previousinum = ip->i_number;
2205         freeblks->fb_devvp = ip->i_devvp;
2206         extblocks = 0;
2207         if (fs->fs_magic == FS_UFS2_MAGIC)
2208                 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
2209         datablocks = DIP(ip, i_blocks) - extblocks;
2210         if ((flags & IO_NORMAL) == 0) {
2211                 freeblks->fb_oldsize = 0;
2212                 freeblks->fb_chkcnt = 0;
2213         } else {
2214                 freeblks->fb_oldsize = ip->i_size;
2215                 ip->i_size = 0;
2216                 DIP_SET(ip, i_size, 0);
2217                 freeblks->fb_chkcnt = datablocks;
2218                 for (i = 0; i < NDADDR; i++) {
2219                         freeblks->fb_dblks[i] = DIP(ip, i_db[i]);
2220                         DIP_SET(ip, i_db[i], 0);
2221                 }
2222                 for (i = 0; i < NIADDR; i++) {
2223                         freeblks->fb_iblks[i] = DIP(ip, i_ib[i]);
2224                         DIP_SET(ip, i_ib[i], 0);
2225                 }
2226                 /*
2227                  * If the file was removed, then the space being freed was
2228                  * accounted for then (see softdep_releasefile()). If the
2229                  * file is merely being truncated, then we account for it now.
2230                  */
2231                 if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2232                         UFS_LOCK(ip->i_ump);
2233                         fs->fs_pendingblocks += datablocks;
2234                         UFS_UNLOCK(ip->i_ump);
2235                 }
2236         }
2237         if ((flags & IO_EXT) == 0) {
2238                 freeblks->fb_oldextsize = 0;
2239         } else {
2240                 freeblks->fb_oldextsize = ip->i_din2->di_extsize;
2241                 ip->i_din2->di_extsize = 0;
2242                 freeblks->fb_chkcnt += extblocks;
2243                 for (i = 0; i < NXADDR; i++) {
2244                         freeblks->fb_eblks[i] = ip->i_din2->di_extb[i];
2245                         ip->i_din2->di_extb[i] = 0;
2246                 }
2247         }
2248         DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - freeblks->fb_chkcnt);
2249         /*
2250          * Push the zero'ed inode to to its disk buffer so that we are free
2251          * to delete its dependencies below. Once the dependencies are gone
2252          * the buffer can be safely released.
2253          */
2254         if ((error = bread(ip->i_devvp,
2255             fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
2256             (int)fs->fs_bsize, NOCRED, &bp)) != 0) {
2257                 brelse(bp);
2258                 softdep_error("softdep_setup_freeblocks", error);
2259         }
2260         if (ip->i_ump->um_fstype == UFS1)
2261                 *((struct ufs1_dinode *)bp->b_data +
2262                     ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
2263         else
2264                 *((struct ufs2_dinode *)bp->b_data +
2265                     ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
2266         /*
2267          * Find and eliminate any inode dependencies.
2268          */
2269         ACQUIRE_LOCK(&lk);
2270         (void) inodedep_lookup(mp, ip->i_number, DEPALLOC, &inodedep);
2271         if ((inodedep->id_state & IOSTARTED) != 0)
2272                 panic("softdep_setup_freeblocks: inode busy");
2273         /*
2274          * Add the freeblks structure to the list of operations that
2275          * must await the zero'ed inode being written to disk. If we
2276          * still have a bitmap dependency (delay == 0), then the inode
2277          * has never been written to disk, so we can process the
2278          * freeblks below once we have deleted the dependencies.
2279          */
2280         delay = (inodedep->id_state & DEPCOMPLETE);
2281         if (delay)
2282                 WORKLIST_INSERT(&inodedep->id_bufwait, &freeblks->fb_list);
2283         /*
2284          * Because the file length has been truncated to zero, any
2285          * pending block allocation dependency structures associated
2286          * with this inode are obsolete and can simply be de-allocated.
2287          * We must first merge the two dependency lists to get rid of
2288          * any duplicate freefrag structures, then purge the merged list.
2289          * If we still have a bitmap dependency, then the inode has never
2290          * been written to disk, so we can free any fragments without delay.
2291          */
2292         if (flags & IO_NORMAL) {
2293                 merge_inode_lists(&inodedep->id_newinoupdt,
2294                     &inodedep->id_inoupdt);
2295                 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0)
2296                         free_allocdirect(&inodedep->id_inoupdt, adp, delay);
2297         }
2298         if (flags & IO_EXT) {
2299                 merge_inode_lists(&inodedep->id_newextupdt,
2300                     &inodedep->id_extupdt);
2301                 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0)
2302                         free_allocdirect(&inodedep->id_extupdt, adp, delay);
2303         }
2304         FREE_LOCK(&lk);
2305         bdwrite(bp);
2306         /*
2307          * We must wait for any I/O in progress to finish so that
2308          * all potential buffers on the dirty list will be visible.
2309          * Once they are all there, walk the list and get rid of
2310          * any dependencies.
2311          */
2312         vp = ITOV(ip);
2313         VI_LOCK(vp);
2314         drain_output(vp);
2315 restart:
2316         TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
2317                 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) ||
2318                     ((flags & IO_NORMAL) == 0 &&
2319                       (bp->b_xflags & BX_ALTDATA) == 0))
2320                         continue;
2321                 if ((bp = getdirtybuf(bp, VI_MTX(vp), MNT_WAIT)) == NULL)
2322                         goto restart;
2323                 VI_UNLOCK(vp);
2324                 ACQUIRE_LOCK(&lk);
2325                 (void) inodedep_lookup(mp, ip->i_number, 0, &inodedep);
2326                 deallocate_dependencies(bp, inodedep);
2327                 FREE_LOCK(&lk);
2328                 bp->b_flags |= B_INVAL | B_NOCACHE;
2329                 brelse(bp);
2330                 VI_LOCK(vp);
2331                 goto restart;
2332         }
2333         VI_UNLOCK(vp);
2334         ACQUIRE_LOCK(&lk);
2335         if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
2336                 (void) free_inodedep(inodedep);
2337
2338         if(delay) {
2339                 freeblks->fb_state |= DEPCOMPLETE;
2340                 /*
2341                  * If the inode with zeroed block pointers is now on disk
2342                  * we can start freeing blocks. Add freeblks to the worklist
2343                  * instead of calling  handle_workitem_freeblocks directly as
2344                  * it is more likely that additional IO is needed to complete
2345                  * the request here than in the !delay case.
2346                  */  
2347                 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE)
2348                         add_to_worklist(&freeblks->fb_list);
2349         }
2350
2351         FREE_LOCK(&lk);
2352         /*
2353          * If the inode has never been written to disk (delay == 0),
2354          * then we can process the freeblks now that we have deleted
2355          * the dependencies.
2356          */
2357         if (!delay)
2358                 handle_workitem_freeblocks(freeblks, 0);
2359 }
2360
2361 /*
2362  * Reclaim any dependency structures from a buffer that is about to
2363  * be reallocated to a new vnode. The buffer must be locked, thus,
2364  * no I/O completion operations can occur while we are manipulating
2365  * its associated dependencies. The mutex is held so that other I/O's
2366  * associated with related dependencies do not occur.
2367  */
2368 static void
2369 deallocate_dependencies(bp, inodedep)
2370         struct buf *bp;
2371         struct inodedep *inodedep;
2372 {
2373         struct worklist *wk;
2374         struct indirdep *indirdep;
2375         struct allocindir *aip;
2376         struct pagedep *pagedep;
2377         struct dirrem *dirrem;
2378         struct diradd *dap;
2379         int i;
2380
2381         mtx_assert(&lk, MA_OWNED);
2382         while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2383                 switch (wk->wk_type) {
2384
2385                 case D_INDIRDEP:
2386                         indirdep = WK_INDIRDEP(wk);
2387                         /*
2388                          * None of the indirect pointers will ever be visible,
2389                          * so they can simply be tossed. GOINGAWAY ensures
2390                          * that allocated pointers will be saved in the buffer
2391                          * cache until they are freed. Note that they will
2392                          * only be able to be found by their physical address
2393                          * since the inode mapping the logical address will
2394                          * be gone. The save buffer used for the safe copy
2395                          * was allocated in setup_allocindir_phase2 using
2396                          * the physical address so it could be used for this
2397                          * purpose. Hence we swap the safe copy with the real
2398                          * copy, allowing the safe copy to be freed and holding
2399                          * on to the real copy for later use in indir_trunc.
2400                          */
2401                         if (indirdep->ir_state & GOINGAWAY)
2402                                 panic("deallocate_dependencies: already gone");
2403                         indirdep->ir_state |= GOINGAWAY;
2404                         VFSTOUFS(bp->b_vp->v_mount)->um_numindirdeps += 1;
2405                         while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0)
2406                                 free_allocindir(aip, inodedep);
2407                         if (bp->b_lblkno >= 0 ||
2408                             bp->b_blkno != indirdep->ir_savebp->b_lblkno)
2409                                 panic("deallocate_dependencies: not indir");
2410                         bcopy(bp->b_data, indirdep->ir_savebp->b_data,
2411                             bp->b_bcount);
2412                         WORKLIST_REMOVE(wk);
2413                         WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, wk);
2414                         continue;
2415
2416                 case D_PAGEDEP:
2417                         pagedep = WK_PAGEDEP(wk);
2418                         /*
2419                          * None of the directory additions will ever be
2420                          * visible, so they can simply be tossed.
2421                          */
2422                         for (i = 0; i < DAHASHSZ; i++)
2423                                 while ((dap =
2424                                     LIST_FIRST(&pagedep->pd_diraddhd[i])))
2425                                         free_diradd(dap);
2426                         while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != 0)
2427                                 free_diradd(dap);
2428                         /*
2429                          * Copy any directory remove dependencies to the list
2430                          * to be processed after the zero'ed inode is written.
2431                          * If the inode has already been written, then they 
2432                          * can be dumped directly onto the work list.
2433                          */
2434                         LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) {
2435                                 LIST_REMOVE(dirrem, dm_next);
2436                                 dirrem->dm_dirinum = pagedep->pd_ino;
2437                                 if (inodedep == NULL ||
2438                                     (inodedep->id_state & ALLCOMPLETE) ==
2439                                      ALLCOMPLETE)
2440                                         add_to_worklist(&dirrem->dm_list);
2441                                 else
2442                                         WORKLIST_INSERT(&inodedep->id_bufwait,
2443                                             &dirrem->dm_list);
2444                         }
2445                         if ((pagedep->pd_state & NEWBLOCK) != 0) {
2446                                 LIST_FOREACH(wk, &inodedep->id_bufwait, wk_list)
2447                                         if (wk->wk_type == D_NEWDIRBLK &&
2448                                             WK_NEWDIRBLK(wk)->db_pagedep ==
2449                                               pagedep)
2450                                                 break;
2451                                 if (wk != NULL) {
2452                                         WORKLIST_REMOVE(wk);
2453                                         free_newdirblk(WK_NEWDIRBLK(wk));
2454                                 } else
2455                                         panic("deallocate_dependencies: "
2456                                               "lost pagedep");
2457                         }
2458                         WORKLIST_REMOVE(&pagedep->pd_list);
2459                         LIST_REMOVE(pagedep, pd_hash);
2460                         WORKITEM_FREE(pagedep, D_PAGEDEP);
2461                         continue;
2462
2463                 case D_ALLOCINDIR:
2464                         free_allocindir(WK_ALLOCINDIR(wk), inodedep);
2465                         continue;
2466
2467                 case D_ALLOCDIRECT:
2468                 case D_INODEDEP:
2469                         panic("deallocate_dependencies: Unexpected type %s",
2470                             TYPENAME(wk->wk_type));
2471                         /* NOTREACHED */
2472
2473                 default:
2474                         panic("deallocate_dependencies: Unknown type %s",
2475                             TYPENAME(wk->wk_type));
2476                         /* NOTREACHED */
2477                 }
2478         }
2479 }
2480
2481 /*
2482  * Free an allocdirect. Generate a new freefrag work request if appropriate.
2483  * This routine must be called with splbio interrupts blocked.
2484  */
2485 static void
2486 free_allocdirect(adphead, adp, delay)
2487         struct allocdirectlst *adphead;
2488         struct allocdirect *adp;
2489         int delay;
2490 {
2491         struct newdirblk *newdirblk;
2492         struct worklist *wk;
2493
2494         mtx_assert(&lk, MA_OWNED);
2495         if ((adp->ad_state & DEPCOMPLETE) == 0)
2496                 LIST_REMOVE(adp, ad_deps);
2497         TAILQ_REMOVE(adphead, adp, ad_next);
2498         if ((adp->ad_state & COMPLETE) == 0)
2499                 WORKLIST_REMOVE(&adp->ad_list);
2500         if (adp->ad_freefrag != NULL) {
2501                 if (delay)
2502                         WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2503                             &adp->ad_freefrag->ff_list);
2504                 else
2505                         add_to_worklist(&adp->ad_freefrag->ff_list);
2506         }
2507         if ((wk = LIST_FIRST(&adp->ad_newdirblk)) != NULL) {
2508                 newdirblk = WK_NEWDIRBLK(wk);
2509                 WORKLIST_REMOVE(&newdirblk->db_list);
2510                 if (!LIST_EMPTY(&adp->ad_newdirblk))
2511                         panic("free_allocdirect: extra newdirblk");
2512                 if (delay)
2513                         WORKLIST_INSERT(&adp->ad_inodedep->id_bufwait,
2514                             &newdirblk->db_list);
2515                 else
2516                         free_newdirblk(newdirblk);
2517         }
2518         WORKITEM_FREE(adp, D_ALLOCDIRECT);
2519 }
2520
2521 /*
2522  * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep.
2523  * This routine must be called with splbio interrupts blocked.
2524  */
2525 static void
2526 free_newdirblk(newdirblk)
2527         struct newdirblk *newdirblk;
2528 {
2529         struct pagedep *pagedep;
2530         struct diradd *dap;
2531         int i;
2532
2533         mtx_assert(&lk, MA_OWNED);
2534         /*
2535          * If the pagedep is still linked onto the directory buffer
2536          * dependency chain, then some of the entries on the
2537          * pd_pendinghd list may not be committed to disk yet. In
2538          * this case, we will simply clear the NEWBLOCK flag and
2539          * let the pd_pendinghd list be processed when the pagedep
2540          * is next written. If the pagedep is no longer on the buffer
2541          * dependency chain, then all the entries on the pd_pending
2542          * list are committed to disk and we can free them here.
2543          */
2544         pagedep = newdirblk->db_pagedep;
2545         pagedep->pd_state &= ~NEWBLOCK;
2546         if ((pagedep->pd_state & ONWORKLIST) == 0)
2547                 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
2548                         free_diradd(dap);
2549         /*
2550          * If no dependencies remain, the pagedep will be freed.
2551          */
2552         for (i = 0; i < DAHASHSZ; i++)
2553                 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i]))
2554                         break;
2555         if (i == DAHASHSZ && (pagedep->pd_state & ONWORKLIST) == 0) {
2556                 LIST_REMOVE(pagedep, pd_hash);
2557                 WORKITEM_FREE(pagedep, D_PAGEDEP);
2558         }
2559         WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
2560 }
2561
2562 /*
2563  * Prepare an inode to be freed. The actual free operation is not
2564  * done until the zero'ed inode has been written to disk.
2565  */
2566 void
2567 softdep_freefile(pvp, ino, mode)
2568         struct vnode *pvp;
2569         ino_t ino;
2570         int mode;
2571 {
2572         struct inode *ip = VTOI(pvp);
2573         struct inodedep *inodedep;
2574         struct freefile *freefile;
2575
2576         /*
2577          * This sets up the inode de-allocation dependency.
2578          */
2579         MALLOC(freefile, struct freefile *, sizeof(struct freefile),
2580                 M_FREEFILE, M_SOFTDEP_FLAGS);
2581         workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount);
2582         freefile->fx_mode = mode;
2583         freefile->fx_oldinum = ino;
2584         freefile->fx_devvp = ip->i_devvp;
2585         if ((ip->i_flag & IN_SPACECOUNTED) == 0) {
2586                 UFS_LOCK(ip->i_ump);
2587                 ip->i_fs->fs_pendinginodes += 1;
2588                 UFS_UNLOCK(ip->i_ump);
2589         }
2590
2591         /*
2592          * If the inodedep does not exist, then the zero'ed inode has
2593          * been written to disk. If the allocated inode has never been
2594          * written to disk, then the on-disk inode is zero'ed. In either
2595          * case we can free the file immediately.
2596          */
2597         ACQUIRE_LOCK(&lk);
2598         if (inodedep_lookup(pvp->v_mount, ino, 0, &inodedep) == 0 ||
2599             check_inode_unwritten(inodedep)) {
2600                 FREE_LOCK(&lk);
2601                 handle_workitem_freefile(freefile);
2602                 return;
2603         }
2604         WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list);
2605         FREE_LOCK(&lk);
2606         ip->i_flag |= IN_MODIFIED;
2607 }
2608
2609 /*
2610  * Check to see if an inode has never been written to disk. If
2611  * so free the inodedep and return success, otherwise return failure.
2612  * This routine must be called with splbio interrupts blocked.
2613  *
2614  * If we still have a bitmap dependency, then the inode has never
2615  * been written to disk. Drop the dependency as it is no longer
2616  * necessary since the inode is being deallocated. We set the
2617  * ALLCOMPLETE flags since the bitmap now properly shows that the
2618  * inode is not allocated. Even if the inode is actively being
2619  * written, it has been rolled back to its zero'ed state, so we
2620  * are ensured that a zero inode is what is on the disk. For short
2621  * lived files, this change will usually result in removing all the
2622  * dependencies from the inode so that it can be freed immediately.
2623  */
2624 static int
2625 check_inode_unwritten(inodedep)
2626         struct inodedep *inodedep;
2627 {
2628
2629         mtx_assert(&lk, MA_OWNED);
2630         if ((inodedep->id_state & DEPCOMPLETE) != 0 ||
2631             !LIST_EMPTY(&inodedep->id_pendinghd) ||
2632             !LIST_EMPTY(&inodedep->id_bufwait) ||
2633             !LIST_EMPTY(&inodedep->id_inowait) ||
2634             !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2635             !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2636             !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2637             !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2638             inodedep->id_nlinkdelta != 0)
2639                 return (0);
2640
2641         /*
2642          * Another process might be in initiate_write_inodeblock_ufs[12]
2643          * trying to allocate memory without holding "Softdep Lock".
2644          */
2645         if ((inodedep->id_state & IOSTARTED) != 0 &&
2646             inodedep->id_savedino1 == NULL)
2647                 return (0);
2648
2649         inodedep->id_state |= ALLCOMPLETE;
2650         LIST_REMOVE(inodedep, id_deps);
2651         inodedep->id_buf = NULL;
2652         if (inodedep->id_state & ONWORKLIST)
2653                 WORKLIST_REMOVE(&inodedep->id_list);
2654         if (inodedep->id_savedino1 != NULL) {
2655                 FREE(inodedep->id_savedino1, M_SAVEDINO);
2656                 inodedep->id_savedino1 = NULL;
2657         }
2658         if (free_inodedep(inodedep) == 0)
2659                 panic("check_inode_unwritten: busy inode");
2660         return (1);
2661 }
2662
2663 /*
2664  * Try to free an inodedep structure. Return 1 if it could be freed.
2665  */
2666 static int
2667 free_inodedep(inodedep)
2668         struct inodedep *inodedep;
2669 {
2670
2671         mtx_assert(&lk, MA_OWNED);
2672         if ((inodedep->id_state & ONWORKLIST) != 0 ||
2673             (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE ||
2674             !LIST_EMPTY(&inodedep->id_pendinghd) ||
2675             !LIST_EMPTY(&inodedep->id_bufwait) ||
2676             !LIST_EMPTY(&inodedep->id_inowait) ||
2677             !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
2678             !TAILQ_EMPTY(&inodedep->id_newinoupdt) ||
2679             !TAILQ_EMPTY(&inodedep->id_extupdt) ||
2680             !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
2681             inodedep->id_nlinkdelta != 0 || inodedep->id_savedino1 != NULL)
2682                 return (0);
2683         LIST_REMOVE(inodedep, id_hash);
2684         WORKITEM_FREE(inodedep, D_INODEDEP);
2685         num_inodedep -= 1;
2686         return (1);
2687 }
2688
2689 /*
2690  * This workitem routine performs the block de-allocation.
2691  * The workitem is added to the pending list after the updated
2692  * inode block has been written to disk.  As mentioned above,
2693  * checks regarding the number of blocks de-allocated (compared
2694  * to the number of blocks allocated for the file) are also
2695  * performed in this function.
2696  */
2697 static void
2698 handle_workitem_freeblocks(freeblks, flags)
2699         struct freeblks *freeblks;
2700         int flags;
2701 {
2702         struct inode *ip;
2703         struct vnode *vp;
2704         struct fs *fs;
2705         struct ufsmount *ump;
2706         int i, nblocks, level, bsize;
2707         ufs2_daddr_t bn, blocksreleased = 0;
2708         int error, allerror = 0;
2709         ufs_lbn_t baselbns[NIADDR], tmpval;
2710         int fs_pendingblocks;
2711
2712         ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2713         fs = ump->um_fs;
2714         fs_pendingblocks = 0;
2715         tmpval = 1;
2716         baselbns[0] = NDADDR;
2717         for (i = 1; i < NIADDR; i++) {
2718                 tmpval *= NINDIR(fs);
2719                 baselbns[i] = baselbns[i - 1] + tmpval;
2720         }
2721         nblocks = btodb(fs->fs_bsize);
2722         blocksreleased = 0;
2723         /*
2724          * Release all extended attribute blocks or frags.
2725          */
2726         if (freeblks->fb_oldextsize > 0) {
2727                 for (i = (NXADDR - 1); i >= 0; i--) {
2728                         if ((bn = freeblks->fb_eblks[i]) == 0)
2729                                 continue;
2730                         bsize = sblksize(fs, freeblks->fb_oldextsize, i);
2731                         ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2732                             freeblks->fb_previousinum);
2733                         blocksreleased += btodb(bsize);
2734                 }
2735         }
2736         /*
2737          * Release all data blocks or frags.
2738          */
2739         if (freeblks->fb_oldsize > 0) {
2740                 /*
2741                  * Indirect blocks first.
2742                  */
2743                 for (level = (NIADDR - 1); level >= 0; level--) {
2744                         if ((bn = freeblks->fb_iblks[level]) == 0)
2745                                 continue;
2746                         if ((error = indir_trunc(freeblks, fsbtodb(fs, bn),
2747                             level, baselbns[level], &blocksreleased)) != 0)
2748                                 allerror = error;
2749                         ffs_blkfree(ump, fs, freeblks->fb_devvp, bn,
2750                             fs->fs_bsize, freeblks->fb_previousinum);
2751                         fs_pendingblocks += nblocks;
2752                         blocksreleased += nblocks;
2753                 }
2754                 /*
2755                  * All direct blocks or frags.
2756                  */
2757                 for (i = (NDADDR - 1); i >= 0; i--) {
2758                         if ((bn = freeblks->fb_dblks[i]) == 0)
2759                                 continue;
2760                         bsize = sblksize(fs, freeblks->fb_oldsize, i);
2761                         ffs_blkfree(ump, fs, freeblks->fb_devvp, bn, bsize,
2762                             freeblks->fb_previousinum);
2763                         fs_pendingblocks += btodb(bsize);
2764                         blocksreleased += btodb(bsize);
2765                 }
2766         }
2767         UFS_LOCK(ump);
2768         fs->fs_pendingblocks -= fs_pendingblocks;
2769         UFS_UNLOCK(ump);
2770         /*
2771          * If we still have not finished background cleanup, then check
2772          * to see if the block count needs to be adjusted.
2773          */
2774         if (freeblks->fb_chkcnt != blocksreleased &&
2775             (fs->fs_flags & FS_UNCLEAN) != 0 &&
2776             ffs_vget(freeblks->fb_list.wk_mp, freeblks->fb_previousinum,
2777             (flags & LK_NOWAIT) | LK_EXCLUSIVE, &vp) == 0) {
2778                 ip = VTOI(vp);
2779                 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + \
2780                     freeblks->fb_chkcnt - blocksreleased);
2781                 ip->i_flag |= IN_CHANGE;
2782                 vput(vp);
2783         }
2784
2785 #ifdef INVARIANTS
2786         if (freeblks->fb_chkcnt != blocksreleased &&
2787             ((fs->fs_flags & FS_UNCLEAN) == 0 || (flags & LK_NOWAIT) != 0))
2788                 printf("handle_workitem_freeblocks: block count\n");
2789         if (allerror)
2790                 softdep_error("handle_workitem_freeblks", allerror);
2791 #endif /* INVARIANTS */
2792
2793         ACQUIRE_LOCK(&lk);
2794         WORKITEM_FREE(freeblks, D_FREEBLKS);
2795         FREE_LOCK(&lk);
2796 }
2797
2798 /*
2799  * Release blocks associated with the inode ip and stored in the indirect
2800  * block dbn. If level is greater than SINGLE, the block is an indirect block
2801  * and recursive calls to indirtrunc must be used to cleanse other indirect
2802  * blocks.
2803  */
2804 static int
2805 indir_trunc(freeblks, dbn, level, lbn, countp)
2806         struct freeblks *freeblks;
2807         ufs2_daddr_t dbn;
2808         int level;
2809         ufs_lbn_t lbn;
2810         ufs2_daddr_t *countp;
2811 {
2812         struct buf *bp;
2813         struct fs *fs;
2814         struct worklist *wk;
2815         struct indirdep *indirdep;
2816         struct ufsmount *ump;
2817         ufs1_daddr_t *bap1 = 0;
2818         ufs2_daddr_t nb, *bap2 = 0;
2819         ufs_lbn_t lbnadd;
2820         int i, nblocks, ufs1fmt;
2821         int error, allerror = 0;
2822         int fs_pendingblocks;
2823
2824         ump = VFSTOUFS(freeblks->fb_list.wk_mp);
2825         fs = ump->um_fs;
2826         fs_pendingblocks = 0;
2827         lbnadd = 1;
2828         for (i = level; i > 0; i--)
2829                 lbnadd *= NINDIR(fs);
2830         /*
2831          * Get buffer of block pointers to be freed. This routine is not
2832          * called until the zero'ed inode has been written, so it is safe
2833          * to free blocks as they are encountered. Because the inode has
2834          * been zero'ed, calls to bmap on these blocks will fail. So, we
2835          * have to use the on-disk address and the block device for the
2836          * filesystem to look them up. If the file was deleted before its
2837          * indirect blocks were all written to disk, the routine that set
2838          * us up (deallocate_dependencies) will have arranged to leave
2839          * a complete copy of the indirect block in memory for our use.
2840          * Otherwise we have to read the blocks in from the disk.
2841          */
2842 #ifdef notyet
2843         bp = getblk(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 0, 0,
2844             GB_NOCREAT);
2845 #else
2846         bp = incore(&freeblks->fb_devvp->v_bufobj, dbn);
2847 #endif
2848         ACQUIRE_LOCK(&lk);
2849         if (bp != NULL && (wk = LIST_FIRST(&bp->b_dep)) != NULL) {
2850                 if (wk->wk_type != D_INDIRDEP ||
2851                     (indirdep = WK_INDIRDEP(wk))->ir_savebp != bp ||
2852                     (indirdep->ir_state & GOINGAWAY) == 0)
2853                         panic("indir_trunc: lost indirdep");
2854                 WORKLIST_REMOVE(wk);
2855                 WORKITEM_FREE(indirdep, D_INDIRDEP);
2856                 if (!LIST_EMPTY(&bp->b_dep))
2857                         panic("indir_trunc: dangling dep");
2858                 ump->um_numindirdeps -= 1;
2859                 FREE_LOCK(&lk);
2860         } else {
2861 #ifdef notyet
2862                 if (bp)
2863                         brelse(bp);
2864 #endif
2865                 FREE_LOCK(&lk);
2866                 error = bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize,
2867                     NOCRED, &bp);
2868                 if (error) {
2869                         brelse(bp);
2870                         return (error);
2871                 }
2872         }
2873         /*
2874          * Recursively free indirect blocks.
2875          */
2876         if (ump->um_fstype == UFS1) {
2877                 ufs1fmt = 1;
2878                 bap1 = (ufs1_daddr_t *)bp->b_data;
2879         } else {
2880                 ufs1fmt = 0;
2881                 bap2 = (ufs2_daddr_t *)bp->b_data;
2882         }
2883         nblocks = btodb(fs->fs_bsize);
2884         for (i = NINDIR(fs) - 1; i >= 0; i--) {
2885                 if (ufs1fmt)
2886                         nb = bap1[i];
2887                 else
2888                         nb = bap2[i];
2889                 if (nb == 0)
2890                         continue;
2891                 if (level != 0) {
2892                         if ((error = indir_trunc(freeblks, fsbtodb(fs, nb),
2893                              level - 1, lbn + (i * lbnadd), countp)) != 0)
2894                                 allerror = error;
2895                 }
2896                 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, fs->fs_bsize,
2897                     freeblks->fb_previousinum);
2898                 fs_pendingblocks += nblocks;
2899                 *countp += nblocks;
2900         }
2901         UFS_LOCK(ump);
2902         fs->fs_pendingblocks -= fs_pendingblocks;
2903         UFS_UNLOCK(ump);
2904         bp->b_flags |= B_INVAL | B_NOCACHE;
2905         brelse(bp);
2906         return (allerror);
2907 }
2908
2909 /*
2910  * Free an allocindir.
2911  * This routine must be called with splbio interrupts blocked.
2912  */
2913 static void
2914 free_allocindir(aip, inodedep)
2915         struct allocindir *aip;
2916         struct inodedep *inodedep;
2917 {
2918         struct freefrag *freefrag;
2919
2920         mtx_assert(&lk, MA_OWNED);
2921         if ((aip->ai_state & DEPCOMPLETE) == 0)
2922                 LIST_REMOVE(aip, ai_deps);
2923         if (aip->ai_state & ONWORKLIST)
2924                 WORKLIST_REMOVE(&aip->ai_list);
2925         LIST_REMOVE(aip, ai_next);
2926         if ((freefrag = aip->ai_freefrag) != NULL) {
2927                 if (inodedep == NULL)
2928                         add_to_worklist(&freefrag->ff_list);
2929                 else
2930                         WORKLIST_INSERT(&inodedep->id_bufwait,
2931                             &freefrag->ff_list);
2932         }
2933         WORKITEM_FREE(aip, D_ALLOCINDIR);
2934 }
2935
2936 /*
2937  * Directory entry addition dependencies.
2938  * 
2939  * When adding a new directory entry, the inode (with its incremented link
2940  * count) must be written to disk before the directory entry's pointer to it.
2941  * Also, if the inode is newly allocated, the corresponding freemap must be
2942  * updated (on disk) before the directory entry's pointer. These requirements
2943  * are met via undo/redo on the directory entry's pointer, which consists
2944  * simply of the inode number.
2945  * 
2946  * As directory entries are added and deleted, the free space within a
2947  * directory block can become fragmented.  The ufs filesystem will compact
2948  * a fragmented directory block to make space for a new entry. When this
2949  * occurs, the offsets of previously added entries change. Any "diradd"
2950  * dependency structures corresponding to these entries must be updated with
2951  * the new offsets.
2952  */
2953
2954 /*
2955  * This routine is called after the in-memory inode's link
2956  * count has been incremented, but before the directory entry's
2957  * pointer to the inode has been set.
2958  */
2959 int
2960 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk)
2961         struct buf *bp;         /* buffer containing directory block */
2962         struct inode *dp;       /* inode for directory */
2963         off_t diroffset;        /* offset of new entry in directory */
2964         ino_t newinum;          /* inode referenced by new directory entry */
2965         struct buf *newdirbp;   /* non-NULL => contents of new mkdir */
2966         int isnewblk;           /* entry is in a newly allocated block */
2967 {
2968         int offset;             /* offset of new entry within directory block */
2969         ufs_lbn_t lbn;          /* block in directory containing new entry */
2970         struct fs *fs;
2971         struct diradd *dap;
2972         struct allocdirect *adp;
2973         struct pagedep *pagedep;
2974         struct inodedep *inodedep;
2975         struct newdirblk *newdirblk = 0;
2976         struct mkdir *mkdir1, *mkdir2;
2977         struct mount *mp;
2978
2979         /*
2980          * Whiteouts have no dependencies.
2981          */
2982         if (newinum == WINO) {
2983                 if (newdirbp != NULL)
2984                         bdwrite(newdirbp);
2985                 return (0);
2986         }
2987         mp = UFSTOVFS(dp->i_ump);
2988         fs = dp->i_fs;
2989         lbn = lblkno(fs, diroffset);
2990         offset = blkoff(fs, diroffset);
2991         MALLOC(dap, struct diradd *, sizeof(struct diradd), M_DIRADD,
2992                 M_SOFTDEP_FLAGS|M_ZERO);
2993         workitem_alloc(&dap->da_list, D_DIRADD, mp);
2994         dap->da_offset = offset;
2995         dap->da_newinum = newinum;
2996         dap->da_state = ATTACHED;
2997         if (isnewblk && lbn < NDADDR && fragoff(fs, diroffset) == 0) {
2998                 MALLOC(newdirblk, struct newdirblk *, sizeof(struct newdirblk),
2999                     M_NEWDIRBLK, M_SOFTDEP_FLAGS);
3000                 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp);
3001         }
3002         if (newdirbp == NULL) {
3003                 dap->da_state |= DEPCOMPLETE;
3004                 ACQUIRE_LOCK(&lk);
3005         } else {
3006                 dap->da_state |= MKDIR_BODY | MKDIR_PARENT;
3007                 MALLOC(mkdir1, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
3008                     M_SOFTDEP_FLAGS);
3009                 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp);
3010                 mkdir1->md_state = MKDIR_BODY;
3011                 mkdir1->md_diradd = dap;
3012                 MALLOC(mkdir2, struct mkdir *, sizeof(struct mkdir), M_MKDIR,
3013                     M_SOFTDEP_FLAGS);
3014                 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp);
3015                 mkdir2->md_state = MKDIR_PARENT;
3016                 mkdir2->md_diradd = dap;
3017                 /*
3018                  * Dependency on "." and ".." being written to disk.
3019                  */
3020                 mkdir1->md_buf = newdirbp;
3021                 ACQUIRE_LOCK(&lk);
3022                 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs);
3023                 WORKLIST_INSERT(&newdirbp->b_dep, &mkdir1->md_list);
3024                 FREE_LOCK(&lk);
3025                 bdwrite(newdirbp);
3026                 /*
3027                  * Dependency on link count increase for parent directory
3028                  */
3029                 ACQUIRE_LOCK(&lk);
3030                 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0
3031                     || (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3032                         dap->da_state &= ~MKDIR_PARENT;
3033                         WORKITEM_FREE(mkdir2, D_MKDIR);
3034                 } else {
3035                         LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs);
3036                         WORKLIST_INSERT(&inodedep->id_bufwait,&mkdir2->md_list);
3037                 }
3038         }
3039         /*
3040          * Link into parent directory pagedep to await its being written.
3041          */
3042         if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3043                 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3044         dap->da_pagedep = pagedep;
3045         LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap,
3046             da_pdlist);
3047         /*
3048          * Link into its inodedep. Put it on the id_bufwait list if the inode
3049          * is not yet written. If it is written, do the post-inode write
3050          * processing to put it on the id_pendinghd list.
3051          */
3052         (void) inodedep_lookup(mp, newinum, DEPALLOC, &inodedep);
3053         if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE)
3054                 diradd_inode_written(dap, inodedep);
3055         else
3056                 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3057         if (isnewblk) {
3058                 /*
3059                  * Directories growing into indirect blocks are rare
3060                  * enough and the frequency of new block allocation
3061                  * in those cases even more rare, that we choose not
3062                  * to bother tracking them. Rather we simply force the
3063                  * new directory entry to disk.
3064                  */
3065                 if (lbn >= NDADDR) {
3066                         FREE_LOCK(&lk);
3067                         /*
3068                          * We only have a new allocation when at the
3069                          * beginning of a new block, not when we are
3070                          * expanding into an existing block.
3071                          */
3072                         if (blkoff(fs, diroffset) == 0)
3073                                 return (1);
3074                         return (0);
3075                 }
3076                 /*
3077                  * We only have a new allocation when at the beginning
3078                  * of a new fragment, not when we are expanding into an
3079                  * existing fragment. Also, there is nothing to do if we
3080                  * are already tracking this block.
3081                  */
3082                 if (fragoff(fs, diroffset) != 0) {
3083                         FREE_LOCK(&lk);
3084                         return (0);
3085                 }
3086                 if ((pagedep->pd_state & NEWBLOCK) != 0) {
3087                         WORKITEM_FREE(newdirblk, D_NEWDIRBLK);
3088                         FREE_LOCK(&lk);
3089                         return (0);
3090                 }
3091                 /*
3092                  * Find our associated allocdirect and have it track us.
3093                  */
3094                 if (inodedep_lookup(mp, dp->i_number, 0, &inodedep) == 0)
3095                         panic("softdep_setup_directory_add: lost inodedep");
3096                 adp = TAILQ_LAST(&inodedep->id_newinoupdt, allocdirectlst);
3097                 if (adp == NULL || adp->ad_lbn != lbn)
3098                         panic("softdep_setup_directory_add: lost entry");
3099                 pagedep->pd_state |= NEWBLOCK;
3100                 newdirblk->db_pagedep = pagedep;
3101                 WORKLIST_INSERT(&adp->ad_newdirblk, &newdirblk->db_list);
3102         }
3103         FREE_LOCK(&lk);
3104         return (0);
3105 }
3106
3107 /*
3108  * This procedure is called to change the offset of a directory
3109  * entry when compacting a directory block which must be owned
3110  * exclusively by the caller. Note that the actual entry movement
3111  * must be done in this procedure to ensure that no I/O completions
3112  * occur while the move is in progress.
3113  */
3114 void 
3115 softdep_change_directoryentry_offset(dp, base, oldloc, newloc, entrysize)
3116         struct inode *dp;       /* inode for directory */
3117         caddr_t base;           /* address of dp->i_offset */
3118         caddr_t oldloc;         /* address of old directory location */
3119         caddr_t newloc;         /* address of new directory location */
3120         int entrysize;          /* size of directory entry */
3121 {
3122         int offset, oldoffset, newoffset;
3123         struct pagedep *pagedep;
3124         struct diradd *dap;
3125         ufs_lbn_t lbn;
3126
3127         ACQUIRE_LOCK(&lk);
3128         lbn = lblkno(dp->i_fs, dp->i_offset);
3129         offset = blkoff(dp->i_fs, dp->i_offset);
3130         if (pagedep_lookup(dp, lbn, 0, &pagedep) == 0)
3131                 goto done;
3132         oldoffset = offset + (oldloc - base);
3133         newoffset = offset + (newloc - base);
3134
3135         LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(oldoffset)], da_pdlist) {
3136                 if (dap->da_offset != oldoffset)
3137                         continue;
3138                 dap->da_offset = newoffset;
3139                 if (DIRADDHASH(newoffset) == DIRADDHASH(oldoffset))
3140                         break;
3141                 LIST_REMOVE(dap, da_pdlist);
3142                 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(newoffset)],
3143                     dap, da_pdlist);
3144                 break;
3145         }
3146         if (dap == NULL) {
3147
3148                 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) {
3149                         if (dap->da_offset == oldoffset) {
3150                                 dap->da_offset = newoffset;
3151                                 break;
3152                         }
3153                 }
3154         }
3155 done:
3156         bcopy(oldloc, newloc, entrysize);
3157         FREE_LOCK(&lk);
3158 }
3159
3160 /*
3161  * Free a diradd dependency structure. This routine must be called
3162  * with splbio interrupts blocked.
3163  */
3164 static void
3165 free_diradd(dap)
3166         struct diradd *dap;
3167 {
3168         struct dirrem *dirrem;
3169         struct pagedep *pagedep;
3170         struct inodedep *inodedep;
3171         struct mkdir *mkdir, *nextmd;
3172
3173         mtx_assert(&lk, MA_OWNED);
3174         WORKLIST_REMOVE(&dap->da_list);
3175         LIST_REMOVE(dap, da_pdlist);
3176         if ((dap->da_state & DIRCHG) == 0) {
3177                 pagedep = dap->da_pagedep;
3178         } else {
3179                 dirrem = dap->da_previous;
3180                 pagedep = dirrem->dm_pagedep;
3181                 dirrem->dm_dirinum = pagedep->pd_ino;
3182                 add_to_worklist(&dirrem->dm_list);
3183         }
3184         if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum,
3185             0, &inodedep) != 0)
3186                 (void) free_inodedep(inodedep);
3187         if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) {
3188                 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) {
3189                         nextmd = LIST_NEXT(mkdir, md_mkdirs);
3190                         if (mkdir->md_diradd != dap)
3191                                 continue;
3192                         dap->da_state &= ~mkdir->md_state;
3193                         WORKLIST_REMOVE(&mkdir->md_list);
3194                         LIST_REMOVE(mkdir, md_mkdirs);
3195                         WORKITEM_FREE(mkdir, D_MKDIR);
3196                 }
3197                 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0)
3198                         panic("free_diradd: unfound ref");
3199         }
3200         WORKITEM_FREE(dap, D_DIRADD);
3201 }
3202
3203 /*
3204  * Directory entry removal dependencies.
3205  * 
3206  * When removing a directory entry, the entry's inode pointer must be
3207  * zero'ed on disk before the corresponding inode's link count is decremented
3208  * (possibly freeing the inode for re-use). This dependency is handled by
3209  * updating the directory entry but delaying the inode count reduction until
3210  * after the directory block has been written to disk. After this point, the
3211  * inode count can be decremented whenever it is convenient.
3212  */
3213
3214 /*
3215  * This routine should be called immediately after removing
3216  * a directory entry.  The inode's link count should not be
3217  * decremented by the calling procedure -- the soft updates
3218  * code will do this task when it is safe.
3219  */
3220 void 
3221 softdep_setup_remove(bp, dp, ip, isrmdir)
3222         struct buf *bp;         /* buffer containing directory block */
3223         struct inode *dp;       /* inode for the directory being modified */
3224         struct inode *ip;       /* inode for directory entry being removed */
3225         int isrmdir;            /* indicates if doing RMDIR */
3226 {
3227         struct dirrem *dirrem, *prevdirrem;
3228
3229         /*
3230          * Allocate a new dirrem if appropriate and ACQUIRE_LOCK.
3231          */
3232         dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3233
3234         /*
3235          * If the COMPLETE flag is clear, then there were no active
3236          * entries and we want to roll back to a zeroed entry until
3237          * the new inode is committed to disk. If the COMPLETE flag is
3238          * set then we have deleted an entry that never made it to
3239          * disk. If the entry we deleted resulted from a name change,
3240          * then the old name still resides on disk. We cannot delete
3241          * its inode (returned to us in prevdirrem) until the zeroed
3242          * directory entry gets to disk. The new inode has never been
3243          * referenced on the disk, so can be deleted immediately.
3244          */
3245         if ((dirrem->dm_state & COMPLETE) == 0) {
3246                 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem,
3247                     dm_next);
3248                 FREE_LOCK(&lk);
3249         } else {
3250                 if (prevdirrem != NULL)
3251                         LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd,
3252                             prevdirrem, dm_next);
3253                 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino;
3254                 FREE_LOCK(&lk);
3255                 handle_workitem_remove(dirrem, NULL);
3256         }
3257 }
3258
3259 /*
3260  * Allocate a new dirrem if appropriate and return it along with
3261  * its associated pagedep. Called without a lock, returns with lock.
3262  */
3263 static long num_dirrem;         /* number of dirrem allocated */
3264 static struct dirrem *
3265 newdirrem(bp, dp, ip, isrmdir, prevdirremp)
3266         struct buf *bp;         /* buffer containing directory block */
3267         struct inode *dp;       /* inode for the directory being modified */
3268         struct inode *ip;       /* inode for directory entry being removed */
3269         int isrmdir;            /* indicates if doing RMDIR */
3270         struct dirrem **prevdirremp; /* previously referenced inode, if any */
3271 {
3272         int offset;
3273         ufs_lbn_t lbn;
3274         struct diradd *dap;
3275         struct dirrem *dirrem;
3276         struct pagedep *pagedep;
3277
3278         /*
3279          * Whiteouts have no deletion dependencies.
3280          */
3281         if (ip == NULL)
3282                 panic("newdirrem: whiteout");
3283         /*
3284          * If we are over our limit, try to improve the situation.
3285          * Limiting the number of dirrem structures will also limit
3286          * the number of freefile and freeblks structures.
3287          */
3288         ACQUIRE_LOCK(&lk);
3289         if (num_dirrem > max_softdeps / 2)
3290                 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_REMOVE);
3291         num_dirrem += 1;
3292         FREE_LOCK(&lk);
3293         MALLOC(dirrem, struct dirrem *, sizeof(struct dirrem),
3294                 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO);
3295         workitem_alloc(&dirrem->dm_list, D_DIRREM, ITOV(dp)->v_mount);
3296         dirrem->dm_state = isrmdir ? RMDIR : 0;
3297         dirrem->dm_oldinum = ip->i_number;
3298         *prevdirremp = NULL;
3299
3300         ACQUIRE_LOCK(&lk);
3301         lbn = lblkno(dp->i_fs, dp->i_offset);
3302         offset = blkoff(dp->i_fs, dp->i_offset);
3303         if (pagedep_lookup(dp, lbn, DEPALLOC, &pagedep) == 0)
3304                 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list);
3305         dirrem->dm_pagedep = pagedep;
3306         /*
3307          * Check for a diradd dependency for the same directory entry.
3308          * If present, then both dependencies become obsolete and can
3309          * be de-allocated. Check for an entry on both the pd_dirraddhd
3310          * list and the pd_pendinghd list.
3311          */
3312
3313         LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist)
3314                 if (dap->da_offset == offset)
3315                         break;
3316         if (dap == NULL) {
3317
3318                 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist)
3319                         if (dap->da_offset == offset)
3320                                 break;
3321                 if (dap == NULL)
3322                         return (dirrem);
3323         }
3324         /*
3325          * Must be ATTACHED at this point.
3326          */
3327         if ((dap->da_state & ATTACHED) == 0)
3328                 panic("newdirrem: not ATTACHED");
3329         if (dap->da_newinum != ip->i_number)
3330                 panic("newdirrem: inum %d should be %d",
3331                     ip->i_number, dap->da_newinum);
3332         /*
3333          * If we are deleting a changed name that never made it to disk,
3334          * then return the dirrem describing the previous inode (which
3335          * represents the inode currently referenced from this entry on disk).
3336          */
3337         if ((dap->da_state & DIRCHG) != 0) {
3338                 *prevdirremp = dap->da_previous;
3339                 dap->da_state &= ~DIRCHG;
3340                 dap->da_pagedep = pagedep;
3341         }
3342         /*
3343          * We are deleting an entry that never made it to disk.
3344          * Mark it COMPLETE so we can delete its inode immediately.
3345          */
3346         dirrem->dm_state |= COMPLETE;
3347         free_diradd(dap);
3348         return (dirrem);
3349 }
3350
3351 /*
3352  * Directory entry change dependencies.
3353  * 
3354  * Changing an existing directory entry requires that an add operation
3355  * be completed first followed by a deletion. The semantics for the addition
3356  * are identical to the description of adding a new entry above except
3357  * that the rollback is to the old inode number rather than zero. Once
3358  * the addition dependency is completed, the removal is done as described
3359  * in the removal routine above.
3360  */
3361
3362 /*
3363  * This routine should be called immediately after changing
3364  * a directory entry.  The inode's link count should not be
3365  * decremented by the calling procedure -- the soft updates
3366  * code will perform this task when it is safe.
3367  */
3368 void 
3369 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir)
3370         struct buf *bp;         /* buffer containing directory block */
3371         struct inode *dp;       /* inode for the directory being modified */
3372         struct inode *ip;       /* inode for directory entry being removed */
3373         ino_t newinum;          /* new inode number for changed entry */
3374         int isrmdir;            /* indicates if doing RMDIR */
3375 {
3376         int offset;
3377         struct diradd *dap = NULL;
3378         struct dirrem *dirrem, *prevdirrem;
3379         struct pagedep *pagedep;
3380         struct inodedep *inodedep;
3381         struct mount *mp;
3382
3383         offset = blkoff(dp->i_fs, dp->i_offset);
3384         mp = UFSTOVFS(dp->i_ump);
3385
3386         /*
3387          * Whiteouts do not need diradd dependencies.
3388          */
3389         if (newinum != WINO) {
3390                 MALLOC(dap, struct diradd *, sizeof(struct diradd),
3391                     M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO);
3392                 workitem_alloc(&dap->da_list, D_DIRADD, mp);
3393                 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE;
3394                 dap->da_offset = offset;
3395                 dap->da_newinum = newinum;
3396         }
3397
3398         /*
3399          * Allocate a new dirrem and ACQUIRE_LOCK.
3400          */
3401         dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem);
3402         pagedep = dirrem->dm_pagedep;
3403         /*
3404          * The possible values for isrmdir:
3405          *      0 - non-directory file rename
3406          *      1 - directory rename within same directory
3407          *   inum - directory rename to new directory of given inode number
3408          * When renaming to a new directory, we are both deleting and
3409          * creating a new directory entry, so the link count on the new
3410          * directory should not change. Thus we do not need the followup
3411          * dirrem which is usually done in handle_workitem_remove. We set
3412          * the DIRCHG flag to tell handle_workitem_remove to skip the 
3413          * followup dirrem.
3414          */
3415         if (isrmdir > 1)
3416                 dirrem->dm_state |= DIRCHG;
3417
3418         /*
3419          * Whiteouts have no additional dependencies,
3420          * so just put the dirrem on the correct list.
3421          */
3422         if (newinum == WINO) {
3423                 if ((dirrem->dm_state & COMPLETE) == 0) {
3424                         LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem,
3425                             dm_next);
3426                 } else {
3427                         dirrem->dm_dirinum = pagedep->pd_ino;
3428                         add_to_worklist(&dirrem->dm_list);
3429                 }
3430                 FREE_LOCK(&lk);
3431                 return;
3432         }
3433
3434         /*
3435          * If the COMPLETE flag is clear, then there were no active
3436          * entries and we want to roll back to the previous inode until
3437          * the new inode is committed to disk. If the COMPLETE flag is
3438          * set, then we have deleted an entry that never made it to disk.
3439          * If the entry we deleted resulted from a name change, then the old
3440          * inode reference still resides on disk. Any rollback that we do
3441          * needs to be to that old inode (returned to us in prevdirrem). If
3442          * the entry we deleted resulted from a create, then there is
3443          * no entry on the disk, so we want to roll back to zero rather
3444          * than the uncommitted inode. In either of the COMPLETE cases we
3445          * want to immediately free the unwritten and unreferenced inode.
3446          */
3447         if ((dirrem->dm_state & COMPLETE) == 0) {
3448                 dap->da_previous = dirrem;
3449         } else {
3450                 if (prevdirrem != NULL) {
3451                         dap->da_previous = prevdirrem;
3452                 } else {
3453                         dap->da_state &= ~DIRCHG;
3454                         dap->da_pagedep = pagedep;
3455                 }
3456                 dirrem->dm_dirinum = pagedep->pd_ino;
3457                 add_to_worklist(&dirrem->dm_list);
3458         }
3459         /*
3460          * Link into its inodedep. Put it on the id_bufwait list if the inode
3461          * is not yet written. If it is written, do the post-inode write
3462          * processing to put it on the id_pendinghd list.
3463          */
3464         if (inodedep_lookup(mp, newinum, DEPALLOC, &inodedep) == 0 ||
3465             (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) {
3466                 dap->da_state |= COMPLETE;
3467                 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
3468                 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
3469         } else {
3470                 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)],
3471                     dap, da_pdlist);
3472                 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list);
3473         }
3474         FREE_LOCK(&lk);
3475 }
3476
3477 /*
3478  * Called whenever the link count on an inode is changed.
3479  * It creates an inode dependency so that the new reference(s)
3480  * to the inode cannot be committed to disk until the updated
3481  * inode has been written.
3482  */
3483 void
3484 softdep_change_linkcnt(ip)
3485         struct inode *ip;       /* the inode with the increased link count */
3486 {
3487         struct inodedep *inodedep;
3488
3489         ACQUIRE_LOCK(&lk);
3490         (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number,
3491             DEPALLOC, &inodedep);
3492         if (ip->i_nlink < ip->i_effnlink)
3493                 panic("softdep_change_linkcnt: bad delta");
3494         inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3495         FREE_LOCK(&lk);
3496 }
3497
3498 /*
3499  * Called when the effective link count and the reference count
3500  * on an inode drops to zero. At this point there are no names
3501  * referencing the file in the filesystem and no active file
3502  * references. The space associated with the file will be freed
3503  * as soon as the necessary soft dependencies are cleared.
3504  */
3505 void
3506 softdep_releasefile(ip)
3507         struct inode *ip;       /* inode with the zero effective link count */
3508 {
3509         struct inodedep *inodedep;
3510         struct fs *fs;
3511         int extblocks;
3512
3513         if (ip->i_effnlink > 0)
3514                 panic("softdep_releasefile: file still referenced");
3515         /*
3516          * We may be called several times as the on-disk link count
3517          * drops to zero. We only want to account for the space once.
3518          */
3519         if (ip->i_flag & IN_SPACECOUNTED)
3520                 return;
3521         /*
3522          * We have to deactivate a snapshot otherwise copyonwrites may
3523          * add blocks and the cleanup may remove blocks after we have
3524          * tried to account for them.
3525          */
3526         if ((ip->i_flags & SF_SNAPSHOT) != 0)
3527                 ffs_snapremove(ITOV(ip));
3528         /*
3529          * If we are tracking an nlinkdelta, we have to also remember
3530          * whether we accounted for the freed space yet.
3531          */
3532         ACQUIRE_LOCK(&lk);
3533         if ((inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, &inodedep)))
3534                 inodedep->id_state |= SPACECOUNTED;
3535         FREE_LOCK(&lk);
3536         fs = ip->i_fs;
3537         extblocks = 0;
3538         if (fs->fs_magic == FS_UFS2_MAGIC)
3539                 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize));
3540         UFS_LOCK(ip->i_ump);
3541         ip->i_fs->fs_pendingblocks += DIP(ip, i_blocks) - extblocks;
3542         ip->i_fs->fs_pendinginodes += 1;
3543         UFS_UNLOCK(ip->i_ump);
3544         ip->i_flag |= IN_SPACECOUNTED;
3545 }
3546
3547 /*
3548  * This workitem decrements the inode's link count.
3549  * If the link count reaches zero, the file is removed.
3550  */
3551 static void 
3552 handle_workitem_remove(dirrem, xp)
3553         struct dirrem *dirrem;
3554         struct vnode *xp;
3555 {
3556         struct thread *td = curthread;
3557         struct inodedep *inodedep;
3558         struct vnode *vp;
3559         struct inode *ip;
3560         ino_t oldinum;
3561         int error;
3562
3563         if ((vp = xp) == NULL &&
3564             (error = ffs_vget(dirrem->dm_list.wk_mp,
3565             dirrem->dm_oldinum, LK_EXCLUSIVE, &vp)) != 0) {
3566                 softdep_error("handle_workitem_remove: vget", error);
3567                 return;
3568         }
3569         ip = VTOI(vp);
3570         ACQUIRE_LOCK(&lk);
3571         if ((inodedep_lookup(dirrem->dm_list.wk_mp,
3572             dirrem->dm_oldinum, 0, &inodedep)) == 0)
3573                 panic("handle_workitem_remove: lost inodedep");
3574         /*
3575          * Normal file deletion.
3576          */
3577         if ((dirrem->dm_state & RMDIR) == 0) {
3578                 ip->i_nlink--;
3579                 DIP_SET(ip, i_nlink, ip->i_nlink);
3580                 ip->i_flag |= IN_CHANGE;
3581                 if (ip->i_nlink < ip->i_effnlink)
3582                         panic("handle_workitem_remove: bad file delta");
3583                 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3584                 num_dirrem -= 1;
3585                 WORKITEM_FREE(dirrem, D_DIRREM);
3586                 FREE_LOCK(&lk);
3587                 vput(vp);
3588                 return;
3589         }
3590         /*
3591          * Directory deletion. Decrement reference count for both the
3592          * just deleted parent directory entry and the reference for ".".
3593          * Next truncate the directory to length zero. When the
3594          * truncation completes, arrange to have the reference count on
3595          * the parent decremented to account for the loss of "..".
3596          */
3597         ip->i_nlink -= 2;
3598         DIP_SET(ip, i_nlink, ip->i_nlink);
3599         ip->i_flag |= IN_CHANGE;
3600         if (ip->i_nlink < ip->i_effnlink)
3601                 panic("handle_workitem_remove: bad dir delta");
3602         inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink;
3603         FREE_LOCK(&lk);
3604         if ((error = ffs_truncate(vp, (off_t)0, 0, td->td_ucred, td)) != 0)
3605                 softdep_error("handle_workitem_remove: truncate", error);
3606         ACQUIRE_LOCK(&lk);
3607         /*
3608          * Rename a directory to a new parent. Since, we are both deleting
3609          * and creating a new directory entry, the link count on the new
3610          * directory should not change. Thus we skip the followup dirrem.
3611          */
3612         if (dirrem->dm_state & DIRCHG) {
3613                 num_dirrem -= 1;
3614                 WORKITEM_FREE(dirrem, D_DIRREM);
3615                 FREE_LOCK(&lk);
3616                 vput(vp);
3617                 return;
3618         }
3619         /*
3620          * If the inodedep does not exist, then the zero'ed inode has
3621          * been written to disk. If the allocated inode has never been
3622          * written to disk, then the on-disk inode is zero'ed. In either
3623          * case we can remove the file immediately.
3624          */
3625         dirrem->dm_state = 0;
3626         oldinum = dirrem->dm_oldinum;
3627         dirrem->dm_oldinum = dirrem->dm_dirinum;
3628         if (inodedep_lookup(dirrem->dm_list.wk_mp, oldinum,
3629             0, &inodedep) == 0 || check_inode_unwritten(inodedep)) {
3630                 if (xp != NULL)
3631                         add_to_worklist(&dirrem->dm_list);
3632                 FREE_LOCK(&lk);
3633                 vput(vp);
3634                 if (xp == NULL)
3635                         handle_workitem_remove(dirrem, NULL);
3636                 return;
3637         }
3638         WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list);
3639         FREE_LOCK(&lk);
3640         ip->i_flag |= IN_CHANGE;
3641         ffs_update(vp, 0);
3642         vput(vp);
3643 }
3644
3645 /*
3646  * Inode de-allocation dependencies.
3647  * 
3648  * When an inode's link count is reduced to zero, it can be de-allocated. We
3649  * found it convenient to postpone de-allocation until after the inode is
3650  * written to disk with its new link count (zero).  At this point, all of the
3651  * on-disk inode's block pointers are nullified and, with careful dependency
3652  * list ordering, all dependencies related to the inode will be satisfied and
3653  * the corresponding dependency structures de-allocated.  So, if/when the
3654  * inode is reused, there will be no mixing of old dependencies with new
3655  * ones.  This artificial dependency is set up by the block de-allocation
3656  * procedure above (softdep_setup_freeblocks) and completed by the
3657  * following procedure.
3658  */
3659 static void 
3660 handle_workitem_freefile(freefile)
3661         struct freefile *freefile;
3662 {
3663         struct fs *fs;
3664         struct inodedep *idp;
3665         struct ufsmount *ump;
3666         int error;
3667
3668         ump = VFSTOUFS(freefile->fx_list.wk_mp);
3669         fs = ump->um_fs;
3670 #ifdef DEBUG
3671         ACQUIRE_LOCK(&lk);
3672         error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp);
3673         FREE_LOCK(&lk);
3674         if (error)
3675                 panic("handle_workitem_freefile: inodedep survived");
3676 #endif
3677         UFS_LOCK(ump);
3678         fs->fs_pendinginodes -= 1;
3679         UFS_UNLOCK(ump);
3680         if ((error = ffs_freefile(ump, fs, freefile->fx_devvp,
3681             freefile->fx_oldinum, freefile->fx_mode)) != 0)
3682                 softdep_error("handle_workitem_freefile", error);
3683         ACQUIRE_LOCK(&lk);
3684         WORKITEM_FREE(freefile, D_FREEFILE);
3685         FREE_LOCK(&lk);
3686 }
3687
3688
3689 /*
3690  * Helper function which unlinks marker element from work list and returns
3691  * the next element on the list.
3692  */
3693 static __inline struct worklist *
3694 markernext(struct worklist *marker)
3695 {
3696         struct worklist *next;
3697         
3698         next = LIST_NEXT(marker, wk_list);
3699         LIST_REMOVE(marker, wk_list);
3700         return next;
3701 }
3702
3703 /*
3704  * Disk writes.
3705  * 
3706  * The dependency structures constructed above are most actively used when file
3707  * system blocks are written to disk.  No constraints are placed on when a
3708  * block can be written, but unsatisfied update dependencies are made safe by
3709  * modifying (or replacing) the source memory for the duration of the disk
3710  * write.  When the disk write completes, the memory block is again brought
3711  * up-to-date.
3712  *
3713  * In-core inode structure reclamation.
3714  * 
3715  * Because there are a finite number of "in-core" inode structures, they are
3716  * reused regularly.  By transferring all inode-related dependencies to the
3717  * in-memory inode block and indexing them separately (via "inodedep"s), we
3718  * can allow "in-core" inode structures to be reused at any time and avoid
3719  * any increase in contention.
3720  *
3721  * Called just before entering the device driver to initiate a new disk I/O.
3722  * The buffer must be locked, thus, no I/O completion operations can occur
3723  * while we are manipulating its associated dependencies.
3724  */
3725 static void 
3726 softdep_disk_io_initiation(bp)
3727         struct buf *bp;         /* structure describing disk write to occur */
3728 {
3729         struct worklist *wk;
3730         struct worklist marker;
3731         struct indirdep *indirdep;
3732         struct inodedep *inodedep;
3733
3734         /*
3735          * We only care about write operations. There should never
3736          * be dependencies for reads.
3737          */
3738         if (bp->b_iocmd != BIO_WRITE)
3739                 panic("softdep_disk_io_initiation: not write");
3740
3741         marker.wk_type = D_LAST + 1;    /* Not a normal workitem */
3742         PHOLD(curproc);                 /* Don't swap out kernel stack */
3743
3744         ACQUIRE_LOCK(&lk);
3745         /*
3746          * Do any necessary pre-I/O processing.
3747          */
3748         for (wk = LIST_FIRST(&bp->b_dep); wk != NULL;
3749              wk = markernext(&marker)) {
3750                 LIST_INSERT_AFTER(wk, &marker, wk_list);
3751                 switch (wk->wk_type) {
3752
3753                 case D_PAGEDEP:
3754                         initiate_write_filepage(WK_PAGEDEP(wk), bp);
3755                         continue;
3756
3757                 case D_INODEDEP:
3758                         inodedep = WK_INODEDEP(wk);
3759                         if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC)
3760                                 initiate_write_inodeblock_ufs1(inodedep, bp);
3761                         else
3762                                 initiate_write_inodeblock_ufs2(inodedep, bp);
3763                         continue;
3764
3765                 case D_INDIRDEP:
3766                         indirdep = WK_INDIRDEP(wk);
3767                         if (indirdep->ir_state & GOINGAWAY)
3768                                 panic("disk_io_initiation: indirdep gone");
3769                         /*
3770                          * If there are no remaining dependencies, this
3771                          * will be writing the real pointers, so the
3772                          * dependency can be freed.
3773                          */
3774                         if (LIST_EMPTY(&indirdep->ir_deplisthd)) {
3775                                 struct buf *bp;
3776
3777                                 bp = indirdep->ir_savebp;
3778                                 bp->b_flags |= B_INVAL | B_NOCACHE;
3779                                 /* inline expand WORKLIST_REMOVE(wk); */
3780                                 wk->wk_state &= ~ONWORKLIST;
3781                                 LIST_REMOVE(wk, wk_list);
3782                                 WORKITEM_FREE(indirdep, D_INDIRDEP);
3783                                 FREE_LOCK(&lk);
3784                                 brelse(bp);
3785                                 ACQUIRE_LOCK(&lk);
3786                                 continue;
3787                         }
3788                         /*
3789                          * Replace up-to-date version with safe version.
3790                          */
3791                         FREE_LOCK(&lk);
3792                         MALLOC(indirdep->ir_saveddata, caddr_t, bp->b_bcount,
3793                             M_INDIRDEP, M_SOFTDEP_FLAGS);
3794                         ACQUIRE_LOCK(&lk);
3795                         indirdep->ir_state &= ~ATTACHED;
3796                         indirdep->ir_state |= UNDONE;
3797                         bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount);
3798                         bcopy(indirdep->ir_savebp->b_data, bp->b_data,
3799                             bp->b_bcount);
3800                         continue;
3801
3802                 case D_MKDIR:
3803                 case D_BMSAFEMAP:
3804                 case D_ALLOCDIRECT:
3805                 case D_ALLOCINDIR:
3806                         continue;
3807
3808                 default:
3809                         panic("handle_disk_io_initiation: Unexpected type %s",
3810                             TYPENAME(wk->wk_type));
3811                         /* NOTREACHED */
3812                 }
3813         }
3814         FREE_LOCK(&lk);
3815         PRELE(curproc);                 /* Allow swapout of kernel stack */
3816 }
3817
3818 /*
3819  * Called from within the procedure above to deal with unsatisfied
3820  * allocation dependencies in a directory. The buffer must be locked,
3821  * thus, no I/O completion operations can occur while we are
3822  * manipulating its associated dependencies.
3823  */
3824 static void
3825 initiate_write_filepage(pagedep, bp)
3826         struct pagedep *pagedep;
3827         struct buf *bp;
3828 {
3829         struct diradd *dap;
3830         struct direct *ep;
3831         int i;
3832
3833         if (pagedep->pd_state & IOSTARTED) {
3834                 /*
3835                  * This can only happen if there is a driver that does not
3836                  * understand chaining. Here biodone will reissue the call
3837                  * to strategy for the incomplete buffers.
3838                  */
3839                 printf("initiate_write_filepage: already started\n");
3840                 return;
3841         }
3842         pagedep->pd_state |= IOSTARTED;
3843         for (i = 0; i < DAHASHSZ; i++) {
3844                 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
3845                         ep = (struct direct *)
3846                             ((char *)bp->b_data + dap->da_offset);
3847                         if (ep->d_ino != dap->da_newinum)
3848                                 panic("%s: dir inum %d != new %d",
3849                                     "initiate_write_filepage",
3850                                     ep->d_ino, dap->da_newinum);
3851                         if (dap->da_state & DIRCHG)
3852                                 ep->d_ino = dap->da_previous->dm_oldinum;
3853                         else
3854                                 ep->d_ino = 0;
3855                         dap->da_state &= ~ATTACHED;
3856                         dap->da_state |= UNDONE;
3857                 }
3858         }
3859 }
3860
3861 /*
3862  * Version of initiate_write_inodeblock that handles UFS1 dinodes.
3863  * Note that any bug fixes made to this routine must be done in the
3864  * version found below.
3865  *
3866  * Called from within the procedure above to deal with unsatisfied
3867  * allocation dependencies in an inodeblock. The buffer must be
3868  * locked, thus, no I/O completion operations can occur while we
3869  * are manipulating its associated dependencies.
3870  */
3871 static void 
3872 initiate_write_inodeblock_ufs1(inodedep, bp)
3873         struct inodedep *inodedep;
3874         struct buf *bp;                 /* The inode block */
3875 {
3876         struct allocdirect *adp, *lastadp;
3877         struct ufs1_dinode *dp;
3878         struct ufs1_dinode *sip;
3879         struct fs *fs;
3880         ufs_lbn_t i;
3881 #ifdef INVARIANTS
3882         ufs_lbn_t prevlbn = 0;
3883 #endif
3884         int deplist;
3885
3886         if (inodedep->id_state & IOSTARTED)
3887                 panic("initiate_write_inodeblock_ufs1: already started");
3888         inodedep->id_state |= IOSTARTED;
3889         fs = inodedep->id_fs;
3890         dp = (struct ufs1_dinode *)bp->b_data +
3891             ino_to_fsbo(fs, inodedep->id_ino);
3892         /*
3893          * If the bitmap is not yet written, then the allocated
3894          * inode cannot be written to disk.
3895          */
3896         if ((inodedep->id_state & DEPCOMPLETE) == 0) {
3897                 if (inodedep->id_savedino1 != NULL)
3898                         panic("initiate_write_inodeblock_ufs1: I/O underway");
3899                 FREE_LOCK(&lk);
3900                 MALLOC(sip, struct ufs1_dinode *,
3901                     sizeof(struct ufs1_dinode), M_SAVEDINO, M_SOFTDEP_FLAGS);
3902                 ACQUIRE_LOCK(&lk);
3903                 inodedep->id_savedino1 = sip;
3904                 *inodedep->id_savedino1 = *dp;
3905                 bzero((caddr_t)dp, sizeof(struct ufs1_dinode));
3906                 dp->di_gen = inodedep->id_savedino1->di_gen;
3907                 return;
3908         }
3909         /*
3910          * If no dependencies, then there is nothing to roll back.
3911          */
3912         inodedep->id_savedsize = dp->di_size;
3913         inodedep->id_savedextsize = 0;
3914         if (TAILQ_EMPTY(&inodedep->id_inoupdt))
3915                 return;
3916         /*
3917          * Set the dependencies to busy.
3918          */
3919         for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3920              adp = TAILQ_NEXT(adp, ad_next)) {
3921 #ifdef INVARIANTS
3922                 if (deplist != 0 && prevlbn >= adp->ad_lbn)
3923                         panic("softdep_write_inodeblock: lbn order");
3924                 prevlbn = adp->ad_lbn;
3925                 if (adp->ad_lbn < NDADDR &&
3926                     dp->di_db[adp->ad_lbn] != adp->ad_newblkno)
3927                         panic("%s: direct pointer #%jd mismatch %d != %jd",
3928                             "softdep_write_inodeblock",
3929                             (intmax_t)adp->ad_lbn,
3930                             dp->di_db[adp->ad_lbn],
3931                             (intmax_t)adp->ad_newblkno);
3932                 if (adp->ad_lbn >= NDADDR &&
3933                     dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno)
3934                         panic("%s: indirect pointer #%jd mismatch %d != %jd",
3935                             "softdep_write_inodeblock",
3936                             (intmax_t)adp->ad_lbn - NDADDR,
3937                             dp->di_ib[adp->ad_lbn - NDADDR],
3938                             (intmax_t)adp->ad_newblkno);
3939                 deplist |= 1 << adp->ad_lbn;
3940                 if ((adp->ad_state & ATTACHED) == 0)
3941                         panic("softdep_write_inodeblock: Unknown state 0x%x",
3942                             adp->ad_state);
3943 #endif /* INVARIANTS */
3944                 adp->ad_state &= ~ATTACHED;
3945                 adp->ad_state |= UNDONE;
3946         }
3947         /*
3948          * The on-disk inode cannot claim to be any larger than the last
3949          * fragment that has been written. Otherwise, the on-disk inode
3950          * might have fragments that were not the last block in the file
3951          * which would corrupt the filesystem.
3952          */
3953         for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
3954              lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
3955                 if (adp->ad_lbn >= NDADDR)
3956                         break;
3957                 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
3958                 /* keep going until hitting a rollback to a frag */
3959                 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
3960                         continue;
3961                 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
3962                 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
3963 #ifdef INVARIANTS
3964                         if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
3965                                 panic("softdep_write_inodeblock: lost dep1");
3966 #endif /* INVARIANTS */
3967                         dp->di_db[i] = 0;
3968                 }
3969                 for (i = 0; i < NIADDR; i++) {
3970 #ifdef INVARIANTS
3971                         if (dp->di_ib[i] != 0 &&
3972                             (deplist & ((1 << NDADDR) << i)) == 0)
3973                                 panic("softdep_write_inodeblock: lost dep2");
3974 #endif /* INVARIANTS */
3975                         dp->di_ib[i] = 0;
3976                 }
3977                 return;
3978         }
3979         /*
3980          * If we have zero'ed out the last allocated block of the file,
3981          * roll back the size to the last currently allocated block.
3982          * We know that this last allocated block is a full-sized as
3983          * we already checked for fragments in the loop above.
3984          */
3985         if (lastadp != NULL &&
3986             dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
3987                 for (i = lastadp->ad_lbn; i >= 0; i--)
3988                         if (dp->di_db[i] != 0)
3989                                 break;
3990                 dp->di_size = (i + 1) * fs->fs_bsize;
3991         }
3992         /*
3993          * The only dependencies are for indirect blocks.
3994          *
3995          * The file size for indirect block additions is not guaranteed.
3996          * Such a guarantee would be non-trivial to achieve. The conventional
3997          * synchronous write implementation also does not make this guarantee.
3998          * Fsck should catch and fix discrepancies. Arguably, the file size
3999          * can be over-estimated without destroying integrity when the file
4000          * moves into the indirect blocks (i.e., is large). If we want to
4001          * postpone fsck, we are stuck with this argument.
4002          */
4003         for (; adp; adp = TAILQ_NEXT(adp, ad_next))
4004                 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
4005 }
4006                 
4007 /*
4008  * Version of initiate_write_inodeblock that handles UFS2 dinodes.
4009  * Note that any bug fixes made to this routine must be done in the
4010  * version found above.
4011  *
4012  * Called from within the procedure above to deal with unsatisfied
4013  * allocation dependencies in an inodeblock. The buffer must be
4014  * locked, thus, no I/O completion operations can occur while we
4015  * are manipulating its associated dependencies.
4016  */
4017 static void 
4018 initiate_write_inodeblock_ufs2(inodedep, bp)
4019         struct inodedep *inodedep;
4020         struct buf *bp;                 /* The inode block */
4021 {
4022         struct allocdirect *adp, *lastadp;
4023         struct ufs2_dinode *dp;
4024         struct ufs2_dinode *sip;
4025         struct fs *fs;
4026         ufs_lbn_t i;
4027 #ifdef INVARIANTS
4028         ufs_lbn_t prevlbn = 0;
4029 #endif
4030         int deplist;
4031
4032         if (inodedep->id_state & IOSTARTED)
4033                 panic("initiate_write_inodeblock_ufs2: already started");
4034         inodedep->id_state |= IOSTARTED;
4035         fs = inodedep->id_fs;
4036         dp = (struct ufs2_dinode *)bp->b_data +
4037             ino_to_fsbo(fs, inodedep->id_ino);
4038         /*
4039          * If the bitmap is not yet written, then the allocated
4040          * inode cannot be written to disk.
4041          */
4042         if ((inodedep->id_state & DEPCOMPLETE) == 0) {
4043                 if (inodedep->id_savedino2 != NULL)
4044                         panic("initiate_write_inodeblock_ufs2: I/O underway");
4045                 FREE_LOCK(&lk);
4046                 MALLOC(sip, struct ufs2_dinode *,
4047                     sizeof(struct ufs2_dinode), M_SAVEDINO, M_SOFTDEP_FLAGS);
4048                 ACQUIRE_LOCK(&lk);
4049                 inodedep->id_savedino2 = sip;
4050                 *inodedep->id_savedino2 = *dp;
4051                 bzero((caddr_t)dp, sizeof(struct ufs2_dinode));
4052                 dp->di_gen = inodedep->id_savedino2->di_gen;
4053                 return;
4054         }
4055         /*
4056          * If no dependencies, then there is nothing to roll back.
4057          */
4058         inodedep->id_savedsize = dp->di_size;
4059         inodedep->id_savedextsize = dp->di_extsize;
4060         if (TAILQ_EMPTY(&inodedep->id_inoupdt) &&
4061             TAILQ_EMPTY(&inodedep->id_extupdt))
4062                 return;
4063         /*
4064          * Set the ext data dependencies to busy.
4065          */
4066         for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
4067              adp = TAILQ_NEXT(adp, ad_next)) {
4068 #ifdef INVARIANTS
4069                 if (deplist != 0 && prevlbn >= adp->ad_lbn)
4070                         panic("softdep_write_inodeblock: lbn order");
4071                 prevlbn = adp->ad_lbn;
4072                 if (dp->di_extb[adp->ad_lbn] != adp->ad_newblkno)
4073                         panic("%s: direct pointer #%jd mismatch %jd != %jd",
4074                             "softdep_write_inodeblock",
4075                             (intmax_t)adp->ad_lbn,
4076                             (intmax_t)dp->di_extb[adp->ad_lbn],
4077                             (intmax_t)adp->ad_newblkno);
4078                 deplist |= 1 << adp->ad_lbn;
4079                 if ((adp->ad_state & ATTACHED) == 0)
4080                         panic("softdep_write_inodeblock: Unknown state 0x%x",
4081                             adp->ad_state);
4082 #endif /* INVARIANTS */
4083                 adp->ad_state &= ~ATTACHED;
4084                 adp->ad_state |= UNDONE;
4085         }
4086         /*
4087          * The on-disk inode cannot claim to be any larger than the last
4088          * fragment that has been written. Otherwise, the on-disk inode
4089          * might have fragments that were not the last block in the ext
4090          * data which would corrupt the filesystem.
4091          */
4092         for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp;
4093              lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
4094                 dp->di_extb[adp->ad_lbn] = adp->ad_oldblkno;
4095                 /* keep going until hitting a rollback to a frag */
4096                 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
4097                         continue;
4098                 dp->di_extsize = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
4099                 for (i = adp->ad_lbn + 1; i < NXADDR; i++) {
4100 #ifdef INVARIANTS
4101                         if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0)
4102                                 panic("softdep_write_inodeblock: lost dep1");
4103 #endif /* INVARIANTS */
4104                         dp->di_extb[i] = 0;
4105                 }
4106                 lastadp = NULL;
4107                 break;
4108         }
4109         /*
4110          * If we have zero'ed out the last allocated block of the ext
4111          * data, roll back the size to the last currently allocated block.
4112          * We know that this last allocated block is a full-sized as
4113          * we already checked for fragments in the loop above.
4114          */
4115         if (lastadp != NULL &&
4116             dp->di_extsize <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4117                 for (i = lastadp->ad_lbn; i >= 0; i--)
4118                         if (dp->di_extb[i] != 0)
4119                                 break;
4120                 dp->di_extsize = (i + 1) * fs->fs_bsize;
4121         }
4122         /*
4123          * Set the file data dependencies to busy.
4124          */
4125         for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
4126              adp = TAILQ_NEXT(adp, ad_next)) {
4127 #ifdef INVARIANTS
4128                 if (deplist != 0 && prevlbn >= adp->ad_lbn)
4129                         panic("softdep_write_inodeblock: lbn order");
4130                 prevlbn = adp->ad_lbn;
4131                 if (adp->ad_lbn < NDADDR &&
4132                     dp->di_db[adp->ad_lbn] != adp->ad_newblkno)
4133                         panic("%s: direct pointer #%jd mismatch %jd != %jd",
4134                             "softdep_write_inodeblock",
4135                             (intmax_t)adp->ad_lbn,
4136                             (intmax_t)dp->di_db[adp->ad_lbn],
4137                             (intmax_t)adp->ad_newblkno);
4138                 if (adp->ad_lbn >= NDADDR &&
4139                     dp->di_ib[adp->ad_lbn - NDADDR] != adp->ad_newblkno)
4140                         panic("%s indirect pointer #%jd mismatch %jd != %jd",
4141                             "softdep_write_inodeblock:",
4142                             (intmax_t)adp->ad_lbn - NDADDR,
4143                             (intmax_t)dp->di_ib[adp->ad_lbn - NDADDR],
4144                             (intmax_t)adp->ad_newblkno);
4145                 deplist |= 1 << adp->ad_lbn;
4146                 if ((adp->ad_state & ATTACHED) == 0)
4147                         panic("softdep_write_inodeblock: Unknown state 0x%x",
4148                             adp->ad_state);
4149 #endif /* INVARIANTS */
4150                 adp->ad_state &= ~ATTACHED;
4151                 adp->ad_state |= UNDONE;
4152         }
4153         /*
4154          * The on-disk inode cannot claim to be any larger than the last
4155          * fragment that has been written. Otherwise, the on-disk inode
4156          * might have fragments that were not the last block in the file
4157          * which would corrupt the filesystem.
4158          */
4159         for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp;
4160              lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) {
4161                 if (adp->ad_lbn >= NDADDR)
4162                         break;
4163                 dp->di_db[adp->ad_lbn] = adp->ad_oldblkno;
4164                 /* keep going until hitting a rollback to a frag */
4165                 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize)
4166                         continue;
4167                 dp->di_size = fs->fs_bsize * adp->ad_lbn + adp->ad_oldsize;
4168                 for (i = adp->ad_lbn + 1; i < NDADDR; i++) {
4169 #ifdef INVARIANTS
4170                         if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0)
4171                                 panic("softdep_write_inodeblock: lost dep2");
4172 #endif /* INVARIANTS */
4173                         dp->di_db[i] = 0;
4174                 }
4175                 for (i = 0; i < NIADDR; i++) {
4176 #ifdef INVARIANTS
4177                         if (dp->di_ib[i] != 0 &&
4178                             (deplist & ((1 << NDADDR) << i)) == 0)
4179                                 panic("softdep_write_inodeblock: lost dep3");
4180 #endif /* INVARIANTS */
4181                         dp->di_ib[i] = 0;
4182                 }
4183                 return;
4184         }
4185         /*
4186          * If we have zero'ed out the last allocated block of the file,
4187          * roll back the size to the last currently allocated block.
4188          * We know that this last allocated block is a full-sized as
4189          * we already checked for fragments in the loop above.
4190          */
4191         if (lastadp != NULL &&
4192             dp->di_size <= (lastadp->ad_lbn + 1) * fs->fs_bsize) {
4193                 for (i = lastadp->ad_lbn; i >= 0; i--)
4194                         if (dp->di_db[i] != 0)
4195                                 break;
4196                 dp->di_size = (i + 1) * fs->fs_bsize;
4197         }
4198         /*
4199          * The only dependencies are for indirect blocks.
4200          *
4201          * The file size for indirect block additions is not guaranteed.
4202          * Such a guarantee would be non-trivial to achieve. The conventional
4203          * synchronous write implementation also does not make this guarantee.
4204          * Fsck should catch and fix discrepancies. Arguably, the file size
4205          * can be over-estimated without destroying integrity when the file
4206          * moves into the indirect blocks (i.e., is large). If we want to
4207          * postpone fsck, we are stuck with this argument.
4208          */
4209         for (; adp; adp = TAILQ_NEXT(adp, ad_next))
4210                 dp->di_ib[adp->ad_lbn - NDADDR] = 0;
4211 }
4212
4213 /*
4214  * This routine is called during the completion interrupt
4215  * service routine for a disk write (from the procedure called
4216  * by the device driver to inform the filesystem caches of
4217  * a request completion).  It should be called early in this
4218  * procedure, before the block is made available to other
4219  * processes or other routines are called.
4220  */
4221 static void 
4222 softdep_disk_write_complete(bp)
4223         struct buf *bp;         /* describes the completed disk write */
4224 {
4225         struct worklist *wk;
4226         struct worklist *owk;
4227         struct workhead reattach;
4228         struct newblk *newblk;
4229         struct allocindir *aip;
4230         struct allocdirect *adp;
4231         struct indirdep *indirdep;
4232         struct inodedep *inodedep;
4233         struct bmsafemap *bmsafemap;
4234
4235         /*
4236          * If an error occurred while doing the write, then the data
4237          * has not hit the disk and the dependencies cannot be unrolled.
4238          */
4239         if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0)
4240                 return;
4241         LIST_INIT(&reattach);
4242         /*
4243          * This lock must not be released anywhere in this code segment.
4244          */
4245         ACQUIRE_LOCK(&lk);
4246         owk = NULL;
4247         while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) {
4248                 WORKLIST_REMOVE(wk);
4249                 if (wk == owk)
4250                         panic("duplicate worklist: %p\n", wk);
4251                 owk = wk;
4252                 switch (wk->wk_type) {
4253
4254                 case D_PAGEDEP:
4255                         if (handle_written_filepage(WK_PAGEDEP(wk), bp))
4256                                 WORKLIST_INSERT(&reattach, wk);
4257                         continue;
4258
4259                 case D_INODEDEP:
4260                         if (handle_written_inodeblock(WK_INODEDEP(wk), bp))
4261                                 WORKLIST_INSERT(&reattach, wk);
4262                         continue;
4263
4264                 case D_BMSAFEMAP:
4265                         bmsafemap = WK_BMSAFEMAP(wk);
4266                         while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkhd))) {
4267                                 newblk->nb_state |= DEPCOMPLETE;
4268                                 newblk->nb_bmsafemap = NULL;
4269                                 LIST_REMOVE(newblk, nb_deps);
4270                         }
4271                         while ((adp =
4272                            LIST_FIRST(&bmsafemap->sm_allocdirecthd))) {
4273                                 adp->ad_state |= DEPCOMPLETE;
4274                                 adp->ad_buf = NULL;
4275                                 LIST_REMOVE(adp, ad_deps);
4276                                 handle_allocdirect_partdone(adp);
4277                         }
4278                         while ((aip =
4279                             LIST_FIRST(&bmsafemap->sm_allocindirhd))) {
4280                                 aip->ai_state |= DEPCOMPLETE;
4281                                 aip->ai_buf = NULL;
4282                                 LIST_REMOVE(aip, ai_deps);
4283                                 handle_allocindir_partdone(aip);
4284                         }
4285                         while ((inodedep =
4286                              LIST_FIRST(&bmsafemap->sm_inodedephd)) != NULL) {
4287                                 inodedep->id_state |= DEPCOMPLETE;
4288                                 LIST_REMOVE(inodedep, id_deps);
4289                                 inodedep->id_buf = NULL;
4290                         }
4291                         WORKITEM_FREE(bmsafemap, D_BMSAFEMAP);
4292                         continue;
4293
4294                 case D_MKDIR:
4295                         handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY);
4296                         continue;
4297
4298                 case D_ALLOCDIRECT:
4299                         adp = WK_ALLOCDIRECT(wk);
4300                         adp->ad_state |= COMPLETE;
4301                         handle_allocdirect_partdone(adp);
4302                         continue;
4303
4304                 case D_ALLOCINDIR:
4305                         aip = WK_ALLOCINDIR(wk);
4306                         aip->ai_state |= COMPLETE;
4307                         handle_allocindir_partdone(aip);
4308                         continue;
4309
4310                 case D_INDIRDEP:
4311                         indirdep = WK_INDIRDEP(wk);
4312                         if (indirdep->ir_state & GOINGAWAY)
4313                                 panic("disk_write_complete: indirdep gone");
4314                         bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount);
4315                         FREE(indirdep->ir_saveddata, M_INDIRDEP);
4316                         indirdep->ir_saveddata = 0;
4317                         indirdep->ir_state &= ~UNDONE;
4318                         indirdep->ir_state |= ATTACHED;
4319                         while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) {
4320                                 handle_allocindir_partdone(aip);
4321                                 if (aip == LIST_FIRST(&indirdep->ir_donehd))
4322                                         panic("disk_write_complete: not gone");
4323                         }
4324                         WORKLIST_INSERT(&reattach, wk);
4325                         if ((bp->b_flags & B_DELWRI) == 0)
4326                                 stat_indir_blk_ptrs++;
4327                         bdirty(bp);
4328                         continue;
4329
4330                 default:
4331                         panic("handle_disk_write_complete: Unknown type %s",
4332                             TYPENAME(wk->wk_type));
4333                         /* NOTREACHED */
4334                 }
4335         }
4336         /*
4337          * Reattach any requests that must be redone.
4338          */
4339         while ((wk = LIST_FIRST(&reattach)) != NULL) {
4340                 WORKLIST_REMOVE(wk);
4341                 WORKLIST_INSERT(&bp->b_dep, wk);
4342         }
4343         FREE_LOCK(&lk);
4344 }
4345
4346 /*
4347  * Called from within softdep_disk_write_complete above. Note that
4348  * this routine is always called from interrupt level with further
4349  * splbio interrupts blocked.
4350  */
4351 static void 
4352 handle_allocdirect_partdone(adp)
4353         struct allocdirect *adp;        /* the completed allocdirect */
4354 {
4355         struct allocdirectlst *listhead;
4356         struct allocdirect *listadp;
4357         struct inodedep *inodedep;
4358         long bsize, delay;
4359
4360         if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4361                 return;
4362         if (adp->ad_buf != NULL)
4363                 panic("handle_allocdirect_partdone: dangling dep");
4364         /*
4365          * The on-disk inode cannot claim to be any larger than the last
4366          * fragment that has been written. Otherwise, the on-disk inode
4367          * might have fragments that were not the last block in the file
4368          * which would corrupt the filesystem. Thus, we cannot free any
4369          * allocdirects after one whose ad_oldblkno claims a fragment as
4370          * these blocks must be rolled back to zero before writing the inode.
4371          * We check the currently active set of allocdirects in id_inoupdt
4372          * or id_extupdt as appropriate.
4373          */
4374         inodedep = adp->ad_inodedep;
4375         bsize = inodedep->id_fs->fs_bsize;
4376         if (adp->ad_state & EXTDATA)
4377                 listhead = &inodedep->id_extupdt;
4378         else
4379                 listhead = &inodedep->id_inoupdt;
4380         TAILQ_FOREACH(listadp, listhead, ad_next) {
4381                 /* found our block */
4382                 if (listadp == adp)
4383                         break;
4384                 /* continue if ad_oldlbn is not a fragment */
4385                 if (listadp->ad_oldsize == 0 ||
4386                     listadp->ad_oldsize == bsize)
4387                         continue;
4388                 /* hit a fragment */
4389                 return;
4390         }
4391         /*
4392          * If we have reached the end of the current list without
4393          * finding the just finished dependency, then it must be
4394          * on the future dependency list. Future dependencies cannot
4395          * be freed until they are moved to the current list.
4396          */
4397         if (listadp == NULL) {
4398 #ifdef DEBUG
4399                 if (adp->ad_state & EXTDATA)
4400                         listhead = &inodedep->id_newextupdt;
4401                 else
4402                         listhead = &inodedep->id_newinoupdt;
4403                 TAILQ_FOREACH(listadp, listhead, ad_next)
4404                         /* found our block */
4405                         if (listadp == adp)
4406                                 break;
4407                 if (listadp == NULL)
4408                         panic("handle_allocdirect_partdone: lost dep");
4409 #endif /* DEBUG */
4410                 return;
4411         }
4412         /*
4413          * If we have found the just finished dependency, then free
4414          * it along with anything that follows it that is complete.
4415          * If the inode still has a bitmap dependency, then it has
4416          * never been written to disk, hence the on-disk inode cannot
4417          * reference the old fragment so we can free it without delay.
4418          */
4419         delay = (inodedep->id_state & DEPCOMPLETE);
4420         for (; adp; adp = listadp) {
4421                 listadp = TAILQ_NEXT(adp, ad_next);
4422                 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE)
4423                         return;
4424                 free_allocdirect(listhead, adp, delay);
4425         }
4426 }
4427
4428 /*
4429  * Called from within softdep_disk_write_complete above. Note that
4430  * this routine is always called from interrupt level with further
4431  * splbio interrupts blocked.
4432  */
4433 static void
4434 handle_allocindir_partdone(aip)
4435         struct allocindir *aip;         /* the completed allocindir */
4436 {
4437         struct indirdep *indirdep;
4438
4439         if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE)
4440                 return;
4441         if (aip->ai_buf != NULL)
4442                 panic("handle_allocindir_partdone: dangling dependency");
4443         indirdep = aip->ai_indirdep;
4444         if (indirdep->ir_state & UNDONE) {
4445                 LIST_REMOVE(aip, ai_next);
4446                 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next);
4447                 return;
4448         }
4449         if (indirdep->ir_state & UFS1FMT)
4450                 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4451                     aip->ai_newblkno;
4452         else
4453                 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] =
4454                     aip->ai_newblkno;
4455         LIST_REMOVE(aip, ai_next);
4456         if (aip->ai_freefrag != NULL)
4457                 add_to_worklist(&aip->ai_freefrag->ff_list);
4458         WORKITEM_FREE(aip, D_ALLOCINDIR);
4459 }
4460
4461 /*
4462  * Called from within softdep_disk_write_complete above to restore
4463  * in-memory inode block contents to their most up-to-date state. Note
4464  * that this routine is always called from interrupt level with further
4465  * splbio interrupts blocked.
4466  */
4467 static int 
4468 handle_written_inodeblock(inodedep, bp)
4469         struct inodedep *inodedep;
4470         struct buf *bp;         /* buffer containing the inode block */
4471 {
4472         struct worklist *wk, *filefree;
4473         struct allocdirect *adp, *nextadp;
4474         struct ufs1_dinode *dp1 = NULL;
4475         struct ufs2_dinode *dp2 = NULL;
4476         int hadchanges, fstype;
4477
4478         if ((inodedep->id_state & IOSTARTED) == 0)
4479                 panic("handle_written_inodeblock: not started");
4480         inodedep->id_state &= ~IOSTARTED;
4481         if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) {
4482                 fstype = UFS1;
4483                 dp1 = (struct ufs1_dinode *)bp->b_data +
4484                     ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4485         } else {
4486                 fstype = UFS2;
4487                 dp2 = (struct ufs2_dinode *)bp->b_data +
4488                     ino_to_fsbo(inodedep->id_fs, inodedep->id_ino);
4489         }
4490         /*
4491          * If we had to rollback the inode allocation because of
4492          * bitmaps being incomplete, then simply restore it.
4493          * Keep the block dirty so that it will not be reclaimed until
4494          * all associated dependencies have been cleared and the
4495          * corresponding updates written to disk.
4496          */
4497         if (inodedep->id_savedino1 != NULL) {
4498                 if (fstype == UFS1)
4499                         *dp1 = *inodedep->id_savedino1;
4500                 else
4501                         *dp2 = *inodedep->id_savedino2;
4502                 FREE(inodedep->id_savedino1, M_SAVEDINO);
4503                 inodedep->id_savedino1 = NULL;
4504                 if ((bp->b_flags & B_DELWRI) == 0)
4505                         stat_inode_bitmap++;
4506                 bdirty(bp);
4507                 return (1);
4508         }
4509         inodedep->id_state |= COMPLETE;
4510         /*
4511          * Roll forward anything that had to be rolled back before 
4512          * the inode could be updated.
4513          */
4514         hadchanges = 0;
4515         for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) {
4516                 nextadp = TAILQ_NEXT(adp, ad_next);
4517                 if (adp->ad_state & ATTACHED)
4518                         panic("handle_written_inodeblock: new entry");
4519                 if (fstype == UFS1) {
4520                         if (adp->ad_lbn < NDADDR) {
4521                                 if (dp1->di_db[adp->ad_lbn]!=adp->ad_oldblkno)
4522                                         panic("%s %s #%jd mismatch %d != %jd",
4523                                             "handle_written_inodeblock:",
4524                                             "direct pointer",
4525                                             (intmax_t)adp->ad_lbn,
4526                                             dp1->di_db[adp->ad_lbn],
4527                                             (intmax_t)adp->ad_oldblkno);
4528                                 dp1->di_db[adp->ad_lbn] = adp->ad_newblkno;
4529                         } else {
4530                                 if (dp1->di_ib[adp->ad_lbn - NDADDR] != 0)
4531                                         panic("%s: %s #%jd allocated as %d",
4532                                             "handle_written_inodeblock",
4533                                             "indirect pointer",
4534                                             (intmax_t)adp->ad_lbn - NDADDR,
4535                                             dp1->di_ib[adp->ad_lbn - NDADDR]);
4536                                 dp1->di_ib[adp->ad_lbn - NDADDR] =
4537                                     adp->ad_newblkno;
4538                         }
4539                 } else {
4540                         if (adp->ad_lbn < NDADDR) {
4541                                 if (dp2->di_db[adp->ad_lbn]!=adp->ad_oldblkno)
4542                                         panic("%s: %s #%jd %s %jd != %jd",
4543                                             "handle_written_inodeblock",
4544                                             "direct pointer",
4545                                             (intmax_t)adp->ad_lbn, "mismatch",
4546                                             (intmax_t)dp2->di_db[adp->ad_lbn],
4547                                             (intmax_t)adp->ad_oldblkno);
4548                                 dp2->di_db[adp->ad_lbn] = adp->ad_newblkno;
4549                         } else {
4550                                 if (dp2->di_ib[adp->ad_lbn - NDADDR] != 0)
4551                                         panic("%s: %s #%jd allocated as %jd",
4552                                             "handle_written_inodeblock",
4553                                             "indirect pointer",
4554                                             (intmax_t)adp->ad_lbn - NDADDR,
4555                                             (intmax_t)
4556                                             dp2->di_ib[adp->ad_lbn - NDADDR]);
4557                                 dp2->di_ib[adp->ad_lbn - NDADDR] =
4558                                     adp->ad_newblkno;
4559                         }
4560                 }
4561                 adp->ad_state &= ~UNDONE;
4562                 adp->ad_state |= ATTACHED;
4563                 hadchanges = 1;
4564         }
4565         for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) {
4566                 nextadp = TAILQ_NEXT(adp, ad_next);
4567                 if (adp->ad_state & ATTACHED)
4568                         panic("handle_written_inodeblock: new entry");
4569                 if (dp2->di_extb[adp->ad_lbn] != adp->ad_oldblkno)
4570                         panic("%s: direct pointers #%jd %s %jd != %jd",
4571                             "handle_written_inodeblock",
4572                             (intmax_t)adp->ad_lbn, "mismatch",
4573                             (intmax_t)dp2->di_extb[adp->ad_lbn],
4574                             (intmax_t)adp->ad_oldblkno);
4575                 dp2->di_extb[adp->ad_lbn] = adp->ad_newblkno;
4576                 adp->ad_state &= ~UNDONE;
4577                 adp->ad_state |= ATTACHED;
4578                 hadchanges = 1;
4579         }
4580         if (hadchanges && (bp->b_flags & B_DELWRI) == 0)
4581                 stat_direct_blk_ptrs++;
4582         /*
4583          * Reset the file size to its most up-to-date value.
4584          */
4585         if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1)
4586                 panic("handle_written_inodeblock: bad size");
4587         if (fstype == UFS1) {
4588                 if (dp1->di_size != inodedep->id_savedsize) {
4589                         dp1->di_size = inodedep->id_savedsize;
4590                         hadchanges = 1;
4591                 }
4592         } else {
4593                 if (dp2->di_size != inodedep->id_savedsize) {
4594                         dp2->di_size = inodedep->id_savedsize;
4595                         hadchanges = 1;
4596                 }
4597                 if (dp2->di_extsize != inodedep->id_savedextsize) {
4598                         dp2->di_extsize = inodedep->id_savedextsize;
4599                         hadchanges = 1;
4600                 }
4601         }
4602         inodedep->id_savedsize = -1;
4603         inodedep->id_savedextsize = -1;
4604         /*
4605          * If there were any rollbacks in the inode block, then it must be
4606          * marked dirty so that its will eventually get written back in
4607          * its correct form.
4608          */
4609         if (hadchanges)
4610                 bdirty(bp);
4611         /*
4612          * Process any allocdirects that completed during the update.
4613          */
4614         if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL)
4615                 handle_allocdirect_partdone(adp);
4616         if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL)
4617                 handle_allocdirect_partdone(adp);
4618         /*
4619          * Process deallocations that were held pending until the
4620          * inode had been written to disk. Freeing of the inode
4621          * is delayed until after all blocks have been freed to
4622          * avoid creation of new <vfsid, inum, lbn> triples
4623          * before the old ones have been deleted.
4624          */
4625         filefree = NULL;
4626         while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) {
4627                 WORKLIST_REMOVE(wk);
4628                 switch (wk->wk_type) {
4629
4630                 case D_FREEFILE:
4631                         /*
4632                          * We defer adding filefree to the worklist until
4633                          * all other additions have been made to ensure
4634                          * that it will be done after all the old blocks
4635                          * have been freed.
4636                          */
4637                         if (filefree != NULL)
4638                                 panic("handle_written_inodeblock: filefree");
4639                         filefree = wk;
4640                         continue;
4641
4642                 case D_MKDIR:
4643                         handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT);
4644                         continue;
4645
4646                 case D_DIRADD:
4647                         diradd_inode_written(WK_DIRADD(wk), inodedep);
4648                         continue;
4649
4650                 case D_FREEBLKS:
4651                         wk->wk_state |= COMPLETE;
4652                         if ((wk->wk_state  & ALLCOMPLETE) != ALLCOMPLETE)
4653                                 continue;
4654                          /* -- fall through -- */
4655                 case D_FREEFRAG:
4656                 case D_DIRREM:
4657                         add_to_worklist(wk);
4658                         continue;
4659
4660                 case D_NEWDIRBLK:
4661                         free_newdirblk(WK_NEWDIRBLK(wk));
4662                         continue;
4663
4664                 default:
4665                         panic("handle_written_inodeblock: Unknown type %s",
4666                             TYPENAME(wk->wk_type));
4667                         /* NOTREACHED */
4668                 }
4669         }
4670         if (filefree != NULL) {
4671                 if (free_inodedep(inodedep) == 0)
4672                         panic("handle_written_inodeblock: live inodedep");
4673                 add_to_worklist(filefree);
4674                 return (0);
4675         }
4676
4677         /*
4678          * If no outstanding dependencies, free it.
4679          */
4680         if (free_inodedep(inodedep) ||
4681             (TAILQ_FIRST(&inodedep->id_inoupdt) == 0 &&
4682              TAILQ_FIRST(&inodedep->id_extupdt) == 0))
4683                 return (0);
4684         return (hadchanges);
4685 }
4686
4687 /*
4688  * Process a diradd entry after its dependent inode has been written.
4689  * This routine must be called with splbio interrupts blocked.
4690  */
4691 static void
4692 diradd_inode_written(dap, inodedep)
4693         struct diradd *dap;
4694         struct inodedep *inodedep;
4695 {
4696         struct pagedep *pagedep;
4697
4698         dap->da_state |= COMPLETE;
4699         if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4700                 if (dap->da_state & DIRCHG)
4701                         pagedep = dap->da_previous->dm_pagedep;
4702                 else
4703                         pagedep = dap->da_pagedep;
4704                 LIST_REMOVE(dap, da_pdlist);
4705                 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4706         }
4707         WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list);
4708 }
4709
4710 /*
4711  * Handle the completion of a mkdir dependency.
4712  */
4713 static void
4714 handle_written_mkdir(mkdir, type)
4715         struct mkdir *mkdir;
4716         int type;
4717 {
4718         struct diradd *dap;
4719         struct pagedep *pagedep;
4720
4721         if (mkdir->md_state != type)
4722                 panic("handle_written_mkdir: bad type");
4723         dap = mkdir->md_diradd;
4724         dap->da_state &= ~type;
4725         if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0)
4726                 dap->da_state |= DEPCOMPLETE;
4727         if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4728                 if (dap->da_state & DIRCHG)
4729                         pagedep = dap->da_previous->dm_pagedep;
4730                 else
4731                         pagedep = dap->da_pagedep;
4732                 LIST_REMOVE(dap, da_pdlist);
4733                 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist);
4734         }
4735         LIST_REMOVE(mkdir, md_mkdirs);
4736         WORKITEM_FREE(mkdir, D_MKDIR);
4737 }
4738
4739 /*
4740  * Called from within softdep_disk_write_complete above.
4741  * A write operation was just completed. Removed inodes can
4742  * now be freed and associated block pointers may be committed.
4743  * Note that this routine is always called from interrupt level
4744  * with further splbio interrupts blocked.
4745  */
4746 static int 
4747 handle_written_filepage(pagedep, bp)
4748         struct pagedep *pagedep;
4749         struct buf *bp;         /* buffer containing the written page */
4750 {
4751         struct dirrem *dirrem;
4752         struct diradd *dap, *nextdap;
4753         struct direct *ep;
4754         int i, chgs;
4755
4756         if ((pagedep->pd_state & IOSTARTED) == 0)
4757                 panic("handle_written_filepage: not started");
4758         pagedep->pd_state &= ~IOSTARTED;
4759         /*
4760          * Process any directory removals that have been committed.
4761          */
4762         while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) {
4763                 LIST_REMOVE(dirrem, dm_next);
4764                 dirrem->dm_dirinum = pagedep->pd_ino;
4765                 add_to_worklist(&dirrem->dm_list);
4766         }
4767         /*
4768          * Free any directory additions that have been committed.
4769          * If it is a newly allocated block, we have to wait until
4770          * the on-disk directory inode claims the new block.
4771          */
4772         if ((pagedep->pd_state & NEWBLOCK) == 0)
4773                 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL)
4774                         free_diradd(dap);
4775         /*
4776          * Uncommitted directory entries must be restored.
4777          */
4778         for (chgs = 0, i = 0; i < DAHASHSZ; i++) {
4779                 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap;
4780                      dap = nextdap) {
4781                         nextdap = LIST_NEXT(dap, da_pdlist);
4782                         if (dap->da_state & ATTACHED)
4783                                 panic("handle_written_filepage: attached");
4784                         ep = (struct direct *)
4785                             ((char *)bp->b_data + dap->da_offset);
4786                         ep->d_ino = dap->da_newinum;
4787                         dap->da_state &= ~UNDONE;
4788                         dap->da_state |= ATTACHED;
4789                         chgs = 1;
4790                         /*
4791                          * If the inode referenced by the directory has
4792                          * been written out, then the dependency can be
4793                          * moved to the pending list.
4794                          */
4795                         if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) {
4796                                 LIST_REMOVE(dap, da_pdlist);
4797                                 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap,
4798                                     da_pdlist);
4799                         }
4800                 }
4801         }
4802         /*
4803          * If there were any rollbacks in the directory, then it must be
4804          * marked dirty so that its will eventually get written back in
4805          * its correct form.
4806          */
4807         if (chgs) {
4808                 if ((bp->b_flags & B_DELWRI) == 0)
4809                         stat_dir_entry++;
4810                 bdirty(bp);
4811                 return (1);
4812         }
4813         /*
4814          * If we are not waiting for a new directory block to be
4815          * claimed by its inode, then the pagedep will be freed.
4816          * Otherwise it will remain to track any new entries on
4817          * the page in case they are fsync'ed.
4818          */
4819         if ((pagedep->pd_state & NEWBLOCK) == 0) {
4820                 LIST_REMOVE(pagedep, pd_hash);
4821                 WORKITEM_FREE(pagedep, D_PAGEDEP);
4822         }
4823         return (0);
4824 }
4825
4826 /*
4827  * Writing back in-core inode structures.
4828  * 
4829  * The filesystem only accesses an inode's contents when it occupies an
4830  * "in-core" inode structure.  These "in-core" structures are separate from
4831  * the page frames used to cache inode blocks.  Only the latter are
4832  * transferred to/from the disk.  So, when the updated contents of the
4833  * "in-core" inode structure are copied to the corresponding in-memory inode
4834  * block, the dependencies are also transferred.  The following procedure is
4835  * called when copying a dirty "in-core" inode to a cached inode block.
4836  */
4837
4838 /*
4839  * Called when an inode is loaded from disk. If the effective link count
4840  * differed from the actual link count when it was last flushed, then we
4841  * need to ensure that the correct effective link count is put back.
4842  */
4843 void 
4844 softdep_load_inodeblock(ip)
4845         struct inode *ip;       /* the "in_core" copy of the inode */
4846 {
4847         struct inodedep *inodedep;
4848
4849         /*
4850          * Check for alternate nlink count.
4851          */
4852         ip->i_effnlink = ip->i_nlink;
4853         ACQUIRE_LOCK(&lk);
4854         if (inodedep_lookup(UFSTOVFS(ip->i_ump),
4855             ip->i_number, 0, &inodedep) == 0) {
4856                 FREE_LOCK(&lk);
4857                 return;
4858         }
4859         ip->i_effnlink -= inodedep->id_nlinkdelta;
4860         if (inodedep->id_state & SPACECOUNTED)
4861                 ip->i_flag |= IN_SPACECOUNTED;
4862         FREE_LOCK(&lk);
4863 }
4864
4865 /*
4866  * This routine is called just before the "in-core" inode
4867  * information is to be copied to the in-memory inode block.
4868  * Recall that an inode block contains several inodes. If
4869  * the force flag is set, then the dependencies will be
4870  * cleared so that the update can always be made. Note that
4871  * the buffer is locked when this routine is called, so we
4872  * will never be in the middle of writing the inode block 
4873  * to disk.
4874  */
4875 void 
4876 softdep_update_inodeblock(ip, bp, waitfor)
4877         struct inode *ip;       /* the "in_core" copy of the inode */
4878         struct buf *bp;         /* the buffer containing the inode block */
4879         int waitfor;            /* nonzero => update must be allowed */
4880 {
4881         struct inodedep *inodedep;
4882         struct worklist *wk;
4883         struct mount *mp;
4884         struct buf *ibp;
4885         int error;
4886
4887         /*
4888          * If the effective link count is not equal to the actual link
4889          * count, then we must track the difference in an inodedep while
4890          * the inode is (potentially) tossed out of the cache. Otherwise,
4891          * if there is no existing inodedep, then there are no dependencies
4892          * to track.
4893          */
4894         mp = UFSTOVFS(ip->i_ump);
4895         ACQUIRE_LOCK(&lk);
4896         if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
4897                 FREE_LOCK(&lk);
4898                 if (ip->i_effnlink != ip->i_nlink)
4899                         panic("softdep_update_inodeblock: bad link count");
4900                 return;
4901         }
4902         if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink)
4903                 panic("softdep_update_inodeblock: bad delta");
4904         /*
4905          * Changes have been initiated. Anything depending on these
4906          * changes cannot occur until this inode has been written.
4907          */
4908         inodedep->id_state &= ~COMPLETE;
4909         if ((inodedep->id_state & ONWORKLIST) == 0)
4910                 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list);
4911         /*
4912          * Any new dependencies associated with the incore inode must 
4913          * now be moved to the list associated with the buffer holding
4914          * the in-memory copy of the inode. Once merged process any
4915          * allocdirects that are completed by the merger.
4916          */
4917         merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt);
4918         if (!TAILQ_EMPTY(&inodedep->id_inoupdt))
4919                 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt));
4920         merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt);
4921         if (!TAILQ_EMPTY(&inodedep->id_extupdt))
4922                 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt));
4923         /*
4924          * Now that the inode has been pushed into the buffer, the
4925          * operations dependent on the inode being written to disk
4926          * can be moved to the id_bufwait so that they will be
4927          * processed when the buffer I/O completes.
4928          */
4929         while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) {
4930                 WORKLIST_REMOVE(wk);
4931                 WORKLIST_INSERT(&inodedep->id_bufwait, wk);
4932         }
4933         /*
4934          * Newly allocated inodes cannot be written until the bitmap
4935          * that allocates them have been written (indicated by
4936          * DEPCOMPLETE being set in id_state). If we are doing a
4937          * forced sync (e.g., an fsync on a file), we force the bitmap
4938          * to be written so that the update can be done.
4939          */
4940         if (waitfor == 0) {
4941                 FREE_LOCK(&lk);
4942                 return;
4943         }
4944 retry:
4945         if ((inodedep->id_state & DEPCOMPLETE) != 0) {
4946                 FREE_LOCK(&lk);
4947                 return;
4948         }
4949         ibp = inodedep->id_buf;
4950         ibp = getdirtybuf(ibp, &lk, MNT_WAIT);
4951         if (ibp == NULL) {
4952                 /*
4953                  * If ibp came back as NULL, the dependency could have been
4954                  * freed while we slept.  Look it up again, and check to see
4955                  * that it has completed.
4956                  */
4957                 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0)
4958                         goto retry;
4959                 FREE_LOCK(&lk);
4960                 return;
4961         }
4962         FREE_LOCK(&lk);
4963         if ((error = bwrite(ibp)) != 0)
4964                 softdep_error("softdep_update_inodeblock: bwrite", error);
4965 }
4966
4967 /*
4968  * Merge the a new inode dependency list (such as id_newinoupdt) into an
4969  * old inode dependency list (such as id_inoupdt). This routine must be
4970  * called with splbio interrupts blocked.
4971  */
4972 static void
4973 merge_inode_lists(newlisthead, oldlisthead)
4974         struct allocdirectlst *newlisthead;
4975         struct allocdirectlst *oldlisthead;
4976 {
4977         struct allocdirect *listadp, *newadp;
4978
4979         newadp = TAILQ_FIRST(newlisthead);
4980         for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) {
4981                 if (listadp->ad_lbn < newadp->ad_lbn) {
4982                         listadp = TAILQ_NEXT(listadp, ad_next);
4983                         continue;
4984                 }
4985                 TAILQ_REMOVE(newlisthead, newadp, ad_next);
4986                 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next);
4987                 if (listadp->ad_lbn == newadp->ad_lbn) {
4988                         allocdirect_merge(oldlisthead, newadp,
4989                             listadp);
4990                         listadp = newadp;
4991                 }
4992                 newadp = TAILQ_FIRST(newlisthead);
4993         }
4994         while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) {
4995                 TAILQ_REMOVE(newlisthead, newadp, ad_next);
4996                 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next);
4997         }
4998 }
4999
5000 /*
5001  * If we are doing an fsync, then we must ensure that any directory
5002  * entries for the inode have been written after the inode gets to disk.
5003  */
5004 int
5005 softdep_fsync(vp)
5006         struct vnode *vp;       /* the "in_core" copy of the inode */
5007 {
5008         struct inodedep *inodedep;
5009         struct pagedep *pagedep;
5010         struct worklist *wk;
5011         struct diradd *dap;
5012         struct mount *mp;
5013         struct vnode *pvp;
5014         struct inode *ip;
5015         struct buf *bp;
5016         struct fs *fs;
5017         struct thread *td = curthread;
5018         int error, flushparent, pagedep_new_block;
5019         ino_t parentino;
5020         ufs_lbn_t lbn;
5021
5022         ip = VTOI(vp);
5023         fs = ip->i_fs;
5024         mp = vp->v_mount;
5025         ACQUIRE_LOCK(&lk);
5026         if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) {
5027                 FREE_LOCK(&lk);
5028                 return (0);
5029         }
5030         if (!LIST_EMPTY(&inodedep->id_inowait) ||
5031             !LIST_EMPTY(&inodedep->id_bufwait) ||
5032             !TAILQ_EMPTY(&inodedep->id_extupdt) ||
5033             !TAILQ_EMPTY(&inodedep->id_newextupdt) ||
5034             !TAILQ_EMPTY(&inodedep->id_inoupdt) ||
5035             !TAILQ_EMPTY(&inodedep->id_newinoupdt))
5036                 panic("softdep_fsync: pending ops");
5037         for (error = 0, flushparent = 0; ; ) {
5038                 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL)
5039                         break;
5040                 if (wk->wk_type != D_DIRADD)
5041                         panic("softdep_fsync: Unexpected type %s",
5042                             TYPENAME(wk->wk_type));
5043                 dap = WK_DIRADD(wk);
5044                 /*
5045                  * Flush our parent if this directory entry has a MKDIR_PARENT
5046                  * dependency or is contained in a newly allocated block.
5047                  */
5048                 if (dap->da_state & DIRCHG)
5049                         pagedep = dap->da_previous->dm_pagedep;
5050                 else
5051                         pagedep = dap->da_pagedep;
5052                 parentino = pagedep->pd_ino;
5053                 lbn = pagedep->pd_lbn;
5054                 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE)
5055                         panic("softdep_fsync: dirty");
5056                 if ((dap->da_state & MKDIR_PARENT) ||
5057                     (pagedep->pd_state & NEWBLOCK))
5058                         flushparent = 1;
5059                 else
5060                         flushparent = 0;
5061                 /*
5062                  * If we are being fsync'ed as part of vgone'ing this vnode,
5063                  * then we will not be able to release and recover the
5064                  * vnode below, so we just have to give up on writing its
5065                  * directory entry out. It will eventually be written, just
5066                  * not now, but then the user was not asking to have it
5067                  * written, so we are not breaking any promises.
5068                  */
5069                 if (vp->v_iflag & VI_DOOMED)
5070                         break;
5071                 /*
5072                  * We prevent deadlock by always fetching inodes from the
5073                  * root, moving down the directory tree. Thus, when fetching
5074                  * our parent directory, we first try to get the lock. If
5075                  * that fails, we must unlock ourselves before requesting
5076                  * the lock on our parent. See the comment in ufs_lookup
5077                  * for details on possible races.
5078                  */
5079                 FREE_LOCK(&lk);
5080                 if (ffs_vget(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp)) {
5081                         VOP_UNLOCK(vp, 0);
5082                         error = ffs_vget(mp, parentino, LK_EXCLUSIVE, &pvp);
5083                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5084                         if (error != 0)
5085                                 return (error);
5086                 }
5087                 /*
5088                  * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps
5089                  * that are contained in direct blocks will be resolved by 
5090                  * doing a ffs_update. Pagedeps contained in indirect blocks
5091                  * may require a complete sync'ing of the directory. So, we
5092                  * try the cheap and fast ffs_update first, and if that fails,
5093                  * then we do the slower ffs_syncvnode of the directory.
5094                  */
5095                 if (flushparent) {
5096                         int locked;
5097
5098                         if ((error = ffs_update(pvp, 1)) != 0) {
5099                                 vput(pvp);
5100                                 return (error);
5101                         }
5102                         ACQUIRE_LOCK(&lk);
5103                         locked = 1;
5104                         if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) {
5105                                 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) {
5106                                         if (wk->wk_type != D_DIRADD)
5107                                                 panic("softdep_fsync: Unexpected type %s",
5108                                                       TYPENAME(wk->wk_type));
5109                                         dap = WK_DIRADD(wk);
5110                                         if (dap->da_state & DIRCHG)
5111                                                 pagedep = dap->da_previous->dm_pagedep;
5112                                         else
5113                                                 pagedep = dap->da_pagedep;
5114                                         pagedep_new_block = pagedep->pd_state & NEWBLOCK;
5115                                         FREE_LOCK(&lk);
5116                                         locked = 0;
5117                                         if (pagedep_new_block &&
5118                                             (error = ffs_syncvnode(pvp, MNT_WAIT))) {
5119                                                 vput(pvp);
5120                                                 return (error);
5121                                         }
5122                                 }
5123                         }
5124                         if (locked)
5125                                 FREE_LOCK(&lk);
5126                 }
5127                 /*
5128                  * Flush directory page containing the inode's name.
5129                  */
5130                 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred,
5131                     &bp);
5132                 if (error == 0)
5133                         error = bwrite(bp);
5134                 else
5135                         brelse(bp);
5136                 vput(pvp);
5137                 if (error != 0)
5138                         return (error);
5139                 ACQUIRE_LOCK(&lk);
5140                 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0)
5141                         break;
5142         }
5143         FREE_LOCK(&lk);
5144         return (0);
5145 }
5146
5147 /*
5148  * Flush all the dirty bitmaps associated with the block device
5149  * before flushing the rest of the dirty blocks so as to reduce
5150  * the number of dependencies that will have to be rolled back.
5151  */
5152 void
5153 softdep_fsync_mountdev(vp)
5154         struct vnode *vp;
5155 {
5156         struct buf *bp, *nbp;
5157         struct worklist *wk;
5158
5159         if (!vn_isdisk(vp, NULL))
5160                 panic("softdep_fsync_mountdev: vnode not a disk");
5161 restart:
5162         ACQUIRE_LOCK(&lk);
5163         VI_LOCK(vp);
5164         TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
5165                 /* 
5166                  * If it is already scheduled, skip to the next buffer.
5167                  */
5168                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
5169                         continue;
5170
5171                 if ((bp->b_flags & B_DELWRI) == 0)
5172                         panic("softdep_fsync_mountdev: not dirty");
5173                 /*
5174                  * We are only interested in bitmaps with outstanding
5175                  * dependencies.
5176                  */
5177                 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL ||
5178                     wk->wk_type != D_BMSAFEMAP ||
5179                     (bp->b_vflags & BV_BKGRDINPROG)) {
5180                         BUF_UNLOCK(bp);
5181                         continue;
5182                 }
5183                 VI_UNLOCK(vp);
5184                 FREE_LOCK(&lk);
5185                 bremfree(bp);
5186                 (void) bawrite(bp);
5187                 goto restart;
5188         }
5189         FREE_LOCK(&lk);
5190         drain_output(vp);
5191         VI_UNLOCK(vp);
5192 }
5193
5194 /*
5195  * This routine is called when we are trying to synchronously flush a
5196  * file. This routine must eliminate any filesystem metadata dependencies
5197  * so that the syncing routine can succeed by pushing the dirty blocks
5198  * associated with the file. If any I/O errors occur, they are returned.
5199  */
5200 int
5201 softdep_sync_metadata(struct vnode *vp)
5202 {
5203         struct pagedep *pagedep;
5204         struct allocdirect *adp;
5205         struct allocindir *aip;
5206         struct buf *bp, *nbp;
5207         struct worklist *wk;
5208         int i, error, waitfor;
5209
5210         if (!DOINGSOFTDEP(vp))
5211                 return (0);
5212         /*
5213          * Ensure that any direct block dependencies have been cleared.
5214          */
5215         ACQUIRE_LOCK(&lk);
5216         if ((error = flush_inodedep_deps(vp->v_mount, VTOI(vp)->i_number))) {
5217                 FREE_LOCK(&lk);
5218                 return (error);
5219         }
5220         FREE_LOCK(&lk);
5221         /*
5222          * For most files, the only metadata dependencies are the
5223          * cylinder group maps that allocate their inode or blocks.
5224          * The block allocation dependencies can be found by traversing
5225          * the dependency lists for any buffers that remain on their
5226          * dirty buffer list. The inode allocation dependency will
5227          * be resolved when the inode is updated with MNT_WAIT.
5228          * This work is done in two passes. The first pass grabs most
5229          * of the buffers and begins asynchronously writing them. The
5230          * only way to wait for these asynchronous writes is to sleep
5231          * on the filesystem vnode which may stay busy for a long time
5232          * if the filesystem is active. So, instead, we make a second
5233          * pass over the dependencies blocking on each write. In the
5234          * usual case we will be blocking against a write that we
5235          * initiated, so when it is done the dependency will have been
5236          * resolved. Thus the second pass is expected to end quickly.
5237          */
5238         waitfor = MNT_NOWAIT;
5239
5240 top:
5241         /*
5242          * We must wait for any I/O in progress to finish so that
5243          * all potential buffers on the dirty list will be visible.
5244          */
5245         VI_LOCK(vp);
5246         drain_output(vp);
5247         while ((bp = TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd)) != NULL) {
5248                 bp = getdirtybuf(bp, VI_MTX(vp), MNT_WAIT);
5249                 if (bp)
5250                         break;
5251         }
5252         VI_UNLOCK(vp);
5253         if (bp == NULL)
5254                 return (0);
5255 loop:
5256         /* While syncing snapshots, we must allow recursive lookups */
5257         BUF_AREC(bp);
5258         ACQUIRE_LOCK(&lk);
5259         /*
5260          * As we hold the buffer locked, none of its dependencies
5261          * will disappear.
5262          */
5263         LIST_FOREACH(wk, &bp->b_dep, wk_list) {
5264                 switch (wk->wk_type) {
5265
5266                 case D_ALLOCDIRECT:
5267                         adp = WK_ALLOCDIRECT(wk);
5268                         if (adp->ad_state & DEPCOMPLETE)
5269                                 continue;
5270                         nbp = adp->ad_buf;
5271                         nbp = getdirtybuf(nbp, &lk, waitfor);
5272                         if (nbp == NULL)
5273                                 continue;
5274                         FREE_LOCK(&lk);
5275                         if (waitfor == MNT_NOWAIT) {
5276                                 bawrite(nbp);
5277                         } else if ((error = bwrite(nbp)) != 0) {
5278                                 break;
5279                         }
5280                         ACQUIRE_LOCK(&lk);
5281                         continue;
5282
5283                 case D_ALLOCINDIR:
5284                         aip = WK_ALLOCINDIR(wk);
5285                         if (aip->ai_state & DEPCOMPLETE)
5286                                 continue;
5287                         nbp = aip->ai_buf;
5288                         nbp = getdirtybuf(nbp, &lk, waitfor);
5289                         if (nbp == NULL)
5290                                 continue;
5291                         FREE_LOCK(&lk);
5292                         if (waitfor == MNT_NOWAIT) {
5293                                 bawrite(nbp);
5294                         } else if ((error = bwrite(nbp)) != 0) {
5295                                 break;
5296                         }
5297                         ACQUIRE_LOCK(&lk);
5298                         continue;
5299
5300                 case D_INDIRDEP:
5301                 restart:
5302
5303                         LIST_FOREACH(aip, &WK_INDIRDEP(wk)->ir_deplisthd, ai_next) {
5304                                 if (aip->ai_state & DEPCOMPLETE)
5305                                         continue;
5306                                 nbp = aip->ai_buf;
5307                                 nbp = getdirtybuf(nbp, &lk, MNT_WAIT);
5308                                 if (nbp == NULL)
5309                                         goto restart;
5310                                 FREE_LOCK(&lk);
5311                                 if ((error = bwrite(nbp)) != 0) {
5312                                         goto loop_end;
5313                                 }
5314                                 ACQUIRE_LOCK(&lk);
5315                                 goto restart;
5316                         }
5317                         continue;
5318
5319                 case D_INODEDEP:
5320                         if ((error = flush_inodedep_deps(wk->wk_mp,
5321                             WK_INODEDEP(wk)->id_ino)) != 0) {
5322                                 FREE_LOCK(&lk);
5323                                 break;
5324                         }
5325                         continue;
5326
5327                 case D_PAGEDEP:
5328                         /*
5329                          * We are trying to sync a directory that may
5330                          * have dependencies on both its own metadata
5331                          * and/or dependencies on the inodes of any
5332                          * recently allocated files. We walk its diradd
5333                          * lists pushing out the associated inode.
5334                          */
5335                         pagedep = WK_PAGEDEP(wk);
5336                         for (i = 0; i < DAHASHSZ; i++) {
5337                                 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0)
5338                                         continue;
5339                                 if ((error =
5340                                     flush_pagedep_deps(vp, wk->wk_mp,
5341                                                 &pagedep->pd_diraddhd[i]))) {
5342                                         FREE_LOCK(&lk);
5343                                         goto loop_end;
5344                                 }
5345                         }
5346                         continue;
5347
5348                 case D_MKDIR:
5349                         /*
5350                          * This case should never happen if the vnode has
5351                          * been properly sync'ed. However, if this function
5352                          * is used at a place where the vnode has not yet
5353                          * been sync'ed, this dependency can show up. So,
5354                          * rather than panic, just flush it.
5355                          */
5356                         nbp = WK_MKDIR(wk)->md_buf;
5357                         nbp = getdirtybuf(nbp, &lk, waitfor);
5358                         if (nbp == NULL)
5359                                 continue;
5360                         FREE_LOCK(&lk);
5361                         if (waitfor == MNT_NOWAIT) {
5362                                 bawrite(nbp);
5363                         } else if ((error = bwrite(nbp)) != 0) {
5364                                 break;
5365                         }
5366                         ACQUIRE_LOCK(&lk);
5367                         continue;
5368
5369                 case D_BMSAFEMAP:
5370                         /*
5371                          * This case should never happen if the vnode has
5372                          * been properly sync'ed. However, if this function
5373                          * is used at a place where the vnode has not yet
5374                          * been sync'ed, this dependency can show up. So,
5375                          * rather than panic, just flush it.
5376                          */
5377                         nbp = WK_BMSAFEMAP(wk)->sm_buf;
5378                         nbp = getdirtybuf(nbp, &lk, waitfor);
5379                         if (nbp == NULL)
5380                                 continue;
5381                         FREE_LOCK(&lk);
5382                         if (waitfor == MNT_NOWAIT) {
5383                                 bawrite(nbp);
5384                         } else if ((error = bwrite(nbp)) != 0) {
5385                                 break;
5386                         }
5387                         ACQUIRE_LOCK(&lk);
5388                         continue;
5389
5390                 default:
5391                         panic("softdep_sync_metadata: Unknown type %s",
5392                             TYPENAME(wk->wk_type));
5393                         /* NOTREACHED */
5394                 }
5395         loop_end:
5396                 /* We reach here only in error and unlocked */
5397                 if (error == 0)
5398                         panic("softdep_sync_metadata: zero error");
5399                 BUF_NOREC(bp);
5400                 bawrite(bp);
5401                 return (error);
5402         }
5403         FREE_LOCK(&lk);
5404         VI_LOCK(vp);
5405         while ((nbp = TAILQ_NEXT(bp, b_bobufs)) != NULL) {
5406                 nbp = getdirtybuf(nbp, VI_MTX(vp), MNT_WAIT);
5407                 if (nbp)
5408                         break;
5409         }
5410         VI_UNLOCK(vp);
5411         BUF_NOREC(bp);
5412         bawrite(bp);
5413         if (nbp != NULL) {
5414                 bp = nbp;
5415                 goto loop;
5416         }
5417         /*
5418          * The brief unlock is to allow any pent up dependency
5419          * processing to be done. Then proceed with the second pass.
5420          */
5421         if (waitfor == MNT_NOWAIT) {
5422                 waitfor = MNT_WAIT;
5423                 goto top;
5424         }
5425
5426         /*
5427          * If we have managed to get rid of all the dirty buffers,
5428          * then we are done. For certain directories and block
5429          * devices, we may need to do further work.
5430          *
5431          * We must wait for any I/O in progress to finish so that
5432          * all potential buffers on the dirty list will be visible.
5433          */
5434         VI_LOCK(vp);
5435         drain_output(vp);
5436         VI_UNLOCK(vp);
5437         return (0);
5438 }
5439
5440 /*
5441  * Flush the dependencies associated with an inodedep.
5442  * Called with splbio blocked.
5443  */
5444 static int
5445 flush_inodedep_deps(mp, ino)
5446         struct mount *mp;
5447         ino_t ino;
5448 {
5449         struct inodedep *inodedep;
5450         int error, waitfor;
5451
5452         /*
5453          * This work is done in two passes. The first pass grabs most
5454          * of the buffers and begins asynchronously writing them. The
5455          * only way to wait for these asynchronous writes is to sleep
5456          * on the filesystem vnode which may stay busy for a long time
5457          * if the filesystem is active. So, instead, we make a second
5458          * pass over the dependencies blocking on each write. In the
5459          * usual case we will be blocking against a write that we
5460          * initiated, so when it is done the dependency will have been
5461          * resolved. Thus the second pass is expected to end quickly.
5462          * We give a brief window at the top of the loop to allow
5463          * any pending I/O to complete.
5464          */
5465         for (error = 0, waitfor = MNT_NOWAIT; ; ) {
5466                 if (error)
5467                         return (error);
5468                 FREE_LOCK(&lk);
5469                 ACQUIRE_LOCK(&lk);
5470                 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
5471                         return (0);
5472                 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) ||
5473                     flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) ||
5474                     flush_deplist(&inodedep->id_extupdt, waitfor, &error) ||
5475                     flush_deplist(&inodedep->id_newextupdt, waitfor, &error))
5476                         continue;
5477                 /*
5478                  * If pass2, we are done, otherwise do pass 2.
5479                  */
5480                 if (waitfor == MNT_WAIT)
5481                         break;
5482                 waitfor = MNT_WAIT;
5483         }
5484         /*
5485          * Try freeing inodedep in case all dependencies have been removed.
5486          */
5487         if (inodedep_lookup(mp, ino, 0, &inodedep) != 0)
5488                 (void) free_inodedep(inodedep);
5489         return (0);
5490 }
5491
5492 /*
5493  * Flush an inode dependency list.
5494  * Called with splbio blocked.
5495  */
5496 static int
5497 flush_deplist(listhead, waitfor, errorp)
5498         struct allocdirectlst *listhead;
5499         int waitfor;
5500         int *errorp;
5501 {
5502         struct allocdirect *adp;
5503         struct buf *bp;
5504
5505         mtx_assert(&lk, MA_OWNED);
5506         TAILQ_FOREACH(adp, listhead, ad_next) {
5507                 if (adp->ad_state & DEPCOMPLETE)
5508                         continue;
5509                 bp = adp->ad_buf;
5510                 bp = getdirtybuf(bp, &lk, waitfor);
5511                 if (bp == NULL) {
5512                         if (waitfor == MNT_NOWAIT)
5513                                 continue;
5514                         return (1);
5515                 }
5516                 FREE_LOCK(&lk);
5517                 if (waitfor == MNT_NOWAIT) {
5518                         bawrite(bp);
5519                 } else if ((*errorp = bwrite(bp)) != 0) {
5520                         ACQUIRE_LOCK(&lk);
5521                         return (1);
5522                 }
5523                 ACQUIRE_LOCK(&lk);
5524                 return (1);
5525         }
5526         return (0);
5527 }
5528
5529 /*
5530  * Eliminate a pagedep dependency by flushing out all its diradd dependencies.
5531  * Called with splbio blocked.
5532  */
5533 static int
5534 flush_pagedep_deps(pvp, mp, diraddhdp)
5535         struct vnode *pvp;
5536         struct mount *mp;
5537         struct diraddhd *diraddhdp;
5538 {
5539         struct inodedep *inodedep;
5540         struct ufsmount *ump;
5541         struct diradd *dap;
5542         struct vnode *vp;
5543         int error = 0;
5544         struct buf *bp;
5545         ino_t inum;
5546         struct worklist *wk;
5547
5548         ump = VFSTOUFS(mp);
5549         while ((dap = LIST_FIRST(diraddhdp)) != NULL) {
5550                 /*
5551                  * Flush ourselves if this directory entry
5552                  * has a MKDIR_PARENT dependency.
5553                  */
5554                 if (dap->da_state & MKDIR_PARENT) {
5555                         FREE_LOCK(&lk);
5556                         if ((error = ffs_update(pvp, 1)) != 0)
5557                                 break;
5558                         ACQUIRE_LOCK(&lk);
5559                         /*
5560                          * If that cleared dependencies, go on to next.
5561                          */
5562                         if (dap != LIST_FIRST(diraddhdp))
5563                                 continue;
5564                         if (dap->da_state & MKDIR_PARENT)
5565                                 panic("flush_pagedep_deps: MKDIR_PARENT");
5566                 }
5567                 /*
5568                  * A newly allocated directory must have its "." and
5569                  * ".." entries written out before its name can be
5570                  * committed in its parent. We do not want or need
5571                  * the full semantics of a synchronous ffs_syncvnode as
5572                  * that may end up here again, once for each directory
5573                  * level in the filesystem. Instead, we push the blocks
5574                  * and wait for them to clear. We have to fsync twice
5575                  * because the first call may choose to defer blocks
5576                  * that still have dependencies, but deferral will
5577                  * happen at most once.
5578                  */
5579                 inum = dap->da_newinum;
5580                 if (dap->da_state & MKDIR_BODY) {
5581                         FREE_LOCK(&lk);
5582                         if ((error = ffs_vget(mp, inum, LK_EXCLUSIVE, &vp)))
5583                                 break;
5584                         if ((error=ffs_syncvnode(vp, MNT_NOWAIT)) ||
5585                             (error=ffs_syncvnode(vp, MNT_NOWAIT))) {
5586                                 vput(vp);
5587                                 break;
5588                         }
5589                         VI_LOCK(vp);
5590                         drain_output(vp);
5591                         /*
5592                          * If first block is still dirty with a D_MKDIR
5593                          * dependency then it needs to be written now.
5594                          */
5595                         for (;;) {
5596                                 error = 0;
5597                                 bp = gbincore(&vp->v_bufobj, 0);
5598                                 if (bp == NULL)
5599                                         break;  /* First block not present */
5600                                 error = BUF_LOCK(bp,
5601                                                  LK_EXCLUSIVE |
5602                                                  LK_SLEEPFAIL |
5603                                                  LK_INTERLOCK,
5604                                                  VI_MTX(vp));
5605                                 VI_LOCK(vp);
5606                                 if (error == ENOLCK)
5607                                         continue;       /* Slept, retry */
5608                                 if (error != 0)
5609                                         break;          /* Failed */
5610                                 if ((bp->b_flags & B_DELWRI) == 0) {
5611                                         BUF_UNLOCK(bp);
5612                                         break;  /* Buffer not dirty */
5613                                 }
5614                                 for (wk = LIST_FIRST(&bp->b_dep);
5615                                      wk != NULL;
5616                                      wk = LIST_NEXT(wk, wk_list))
5617                                         if (wk->wk_type == D_MKDIR)
5618                                                 break;
5619                                 if (wk == NULL)
5620                                         BUF_UNLOCK(bp); /* Dependency gone */
5621                                 else {
5622                                         /*
5623                                          * D_MKDIR dependency remains,
5624                                          * must write buffer to stable
5625                                          * storage.
5626                                          */
5627                                         VI_UNLOCK(vp);
5628                                         bremfree(bp);
5629                                         error = bwrite(bp);
5630                                         VI_LOCK(vp);
5631                                 }
5632                                 break;
5633                         }
5634                         VI_UNLOCK(vp);
5635                         vput(vp);
5636                         if (error != 0)
5637                                 break;  /* Flushing of first block failed */
5638                         ACQUIRE_LOCK(&lk);
5639                         /*
5640                          * If that cleared dependencies, go on to next.
5641                          */
5642                         if (dap != LIST_FIRST(diraddhdp))
5643                                 continue;
5644                         if (dap->da_state & MKDIR_BODY)
5645                                 panic("flush_pagedep_deps: MKDIR_BODY");
5646                 }
5647                 /*
5648                  * Flush the inode on which the directory entry depends.
5649                  * Having accounted for MKDIR_PARENT and MKDIR_BODY above,
5650                  * the only remaining dependency is that the updated inode
5651                  * count must get pushed to disk. The inode has already
5652                  * been pushed into its inode buffer (via VOP_UPDATE) at
5653                  * the time of the reference count change. So we need only
5654                  * locate that buffer, ensure that there will be no rollback
5655                  * caused by a bitmap dependency, then write the inode buffer.
5656                  */
5657 retry:
5658                 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0)
5659                         panic("flush_pagedep_deps: lost inode");
5660                 /*
5661                  * If the inode still has bitmap dependencies,
5662                  * push them to disk.
5663                  */
5664                 if ((inodedep->id_state & DEPCOMPLETE) == 0) {
5665                         bp = inodedep->id_buf;
5666                         bp = getdirtybuf(bp, &lk, MNT_WAIT);
5667                         if (bp == NULL)
5668                                 goto retry;
5669                         FREE_LOCK(&lk);
5670                         if ((error = bwrite(bp)) != 0)
5671                                 break;
5672                         ACQUIRE_LOCK(&lk);
5673                         if (dap != LIST_FIRST(diraddhdp))
5674                                 continue;
5675                 }
5676                 /*
5677                  * If the inode is still sitting in a buffer waiting
5678                  * to be written, push it to disk.
5679                  */
5680                 FREE_LOCK(&lk);
5681                 if ((error = bread(ump->um_devvp,
5682                     fsbtodb(ump->um_fs, ino_to_fsba(ump->um_fs, inum)),
5683                     (int)ump->um_fs->fs_bsize, NOCRED, &bp)) != 0) {
5684                         brelse(bp);
5685                         break;
5686                 }
5687                 if ((error = bwrite(bp)) != 0)
5688                         break;
5689                 ACQUIRE_LOCK(&lk);
5690                 /*
5691                  * If we have failed to get rid of all the dependencies
5692                  * then something is seriously wrong.
5693                  */
5694                 if (dap == LIST_FIRST(diraddhdp))
5695                         panic("flush_pagedep_deps: flush failed");
5696         }
5697         if (error)
5698                 ACQUIRE_LOCK(&lk);
5699         return (error);
5700 }
5701
5702 /*
5703  * A large burst of file addition or deletion activity can drive the
5704  * memory load excessively high. First attempt to slow things down
5705  * using the techniques below. If that fails, this routine requests
5706  * the offending operations to fall back to running synchronously
5707  * until the memory load returns to a reasonable level.
5708  */
5709 int
5710 softdep_slowdown(vp)
5711         struct vnode *vp;
5712 {
5713         int max_softdeps_hard;
5714
5715         ACQUIRE_LOCK(&lk);
5716         max_softdeps_hard = max_softdeps * 11 / 10;
5717         if (num_dirrem < max_softdeps_hard / 2 &&
5718             num_inodedep < max_softdeps_hard &&
5719             VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps) {
5720                 FREE_LOCK(&lk);
5721                 return (0);
5722         }
5723         if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps)
5724                 softdep_speedup();
5725         stat_sync_limit_hit += 1;
5726         FREE_LOCK(&lk);
5727         return (1);
5728 }
5729
5730 /*
5731  * Called by the allocation routines when they are about to fail
5732  * in the hope that we can free up some disk space.
5733  * 
5734  * First check to see if the work list has anything on it. If it has,
5735  * clean up entries until we successfully free some space. Because this
5736  * process holds inodes locked, we cannot handle any remove requests
5737  * that might block on a locked inode as that could lead to deadlock.
5738  * If the worklist yields no free space, encourage the syncer daemon
5739  * to help us. In no event will we try for longer than tickdelay seconds.
5740  */
5741 int
5742 softdep_request_cleanup(fs, vp)
5743         struct fs *fs;
5744         struct vnode *vp;
5745 {
5746         struct ufsmount *ump;
5747         long starttime;
5748         ufs2_daddr_t needed;
5749         int error;
5750
5751         ump = VTOI(vp)->i_ump;
5752         mtx_assert(UFS_MTX(ump), MA_OWNED);
5753         needed = fs->fs_cstotal.cs_nbfree + fs->fs_contigsumsize;
5754         starttime = time_second + tickdelay;
5755         /*
5756          * If we are being called because of a process doing a
5757          * copy-on-write, then it is not safe to update the vnode
5758          * as we may recurse into the copy-on-write routine.
5759          */
5760         if (!(curthread->td_pflags & TDP_COWINPROGRESS)) {
5761                 UFS_UNLOCK(ump);
5762                 error = ffs_update(vp, 1);
5763                 UFS_LOCK(ump);
5764                 if (error != 0)
5765                         return (0);
5766         }
5767         while (fs->fs_pendingblocks > 0 && fs->fs_cstotal.cs_nbfree <= needed) {
5768                 if (time_second > starttime)
5769                         return (0);
5770                 UFS_UNLOCK(ump);
5771                 ACQUIRE_LOCK(&lk);
5772                 if (ump->softdep_on_worklist > 0 &&
5773                     process_worklist_item(UFSTOVFS(ump), LK_NOWAIT) != -1) {
5774                         stat_worklist_push += 1;
5775                         FREE_LOCK(&lk);
5776                         UFS_LOCK(ump);
5777                         continue;
5778                 }
5779                 request_cleanup(UFSTOVFS(ump), FLUSH_REMOVE_WAIT);
5780                 FREE_LOCK(&lk);
5781                 UFS_LOCK(ump);
5782         }
5783         return (1);
5784 }
5785
5786 /*
5787  * If memory utilization has gotten too high, deliberately slow things
5788  * down and speed up the I/O processing.
5789  */
5790 extern struct thread *syncertd;
5791 static int
5792 request_cleanup(mp, resource)
5793         struct mount *mp;
5794         int resource;
5795 {
5796         struct thread *td = curthread;
5797         struct ufsmount *ump;
5798
5799         mtx_assert(&lk, MA_OWNED);
5800         /*
5801          * We never hold up the filesystem syncer or buf daemon.
5802          */
5803         if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF))
5804                 return (0);
5805         ump = VFSTOUFS(mp);
5806         /*
5807          * First check to see if the work list has gotten backlogged.
5808          * If it has, co-opt this process to help clean up two entries.
5809          * Because this process may hold inodes locked, we cannot
5810          * handle any remove requests that might block on a locked
5811          * inode as that could lead to deadlock.  We set TDP_SOFTDEP
5812          * to avoid recursively processing the worklist.
5813          */
5814         if (ump->softdep_on_worklist > max_softdeps / 10) {
5815                 td->td_pflags |= TDP_SOFTDEP;
5816                 process_worklist_item(mp, LK_NOWAIT);
5817                 process_worklist_item(mp, LK_NOWAIT);
5818                 td->td_pflags &= ~TDP_SOFTDEP;
5819                 stat_worklist_push += 2;
5820                 return(1);
5821         }
5822         /*
5823          * Next, we attempt to speed up the syncer process. If that
5824          * is successful, then we allow the process to continue.
5825          */
5826         if (softdep_speedup() && resource != FLUSH_REMOVE_WAIT)
5827                 return(0);
5828         /*
5829          * If we are resource constrained on inode dependencies, try
5830          * flushing some dirty inodes. Otherwise, we are constrained
5831          * by file deletions, so try accelerating flushes of directories
5832          * with removal dependencies. We would like to do the cleanup
5833          * here, but we probably hold an inode locked at this point and 
5834          * that might deadlock against one that we try to clean. So,
5835          * the best that we can do is request the syncer daemon to do
5836          * the cleanup for us.
5837          */
5838         switch (resource) {
5839
5840         case FLUSH_INODES:
5841                 stat_ino_limit_push += 1;
5842                 req_clear_inodedeps += 1;
5843                 stat_countp = &stat_ino_limit_hit;
5844                 break;
5845
5846         case FLUSH_REMOVE:
5847         case FLUSH_REMOVE_WAIT:
5848                 stat_blk_limit_push += 1;
5849                 req_clear_remove += 1;
5850                 stat_countp = &stat_blk_limit_hit;
5851                 break;
5852
5853         default:
5854                 panic("request_cleanup: unknown type");
5855         }
5856         /*
5857          * Hopefully the syncer daemon will catch up and awaken us.
5858          * We wait at most tickdelay before proceeding in any case.
5859          */
5860         proc_waiting += 1;
5861         if (handle.callout == NULL)
5862                 handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
5863         msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0);
5864         proc_waiting -= 1;
5865         return (1);
5866 }
5867
5868 /*
5869  * Awaken processes pausing in request_cleanup and clear proc_waiting
5870  * to indicate that there is no longer a timer running.
5871  */
5872 static void
5873 pause_timer(arg)
5874         void *arg;
5875 {
5876
5877         ACQUIRE_LOCK(&lk);
5878         *stat_countp += 1;
5879         wakeup_one(&proc_waiting);
5880         if (proc_waiting > 0)
5881                 handle = timeout(pause_timer, 0, tickdelay > 2 ? tickdelay : 2);
5882         else
5883                 handle.callout = NULL;
5884         FREE_LOCK(&lk);
5885 }
5886
5887 /*
5888  * Flush out a directory with at least one removal dependency in an effort to
5889  * reduce the number of dirrem, freefile, and freeblks dependency structures.
5890  */
5891 static void
5892 clear_remove(td)
5893         struct thread *td;
5894 {
5895         struct pagedep_hashhead *pagedephd;
5896         struct pagedep *pagedep;
5897         static int next = 0;
5898         struct mount *mp;
5899         struct vnode *vp;
5900         int error, cnt;
5901         ino_t ino;
5902
5903         mtx_assert(&lk, MA_OWNED);
5904
5905         for (cnt = 0; cnt < pagedep_hash; cnt++) {
5906                 pagedephd = &pagedep_hashtbl[next++];
5907                 if (next >= pagedep_hash)
5908                         next = 0;
5909                 LIST_FOREACH(pagedep, pagedephd, pd_hash) {
5910                         if (LIST_EMPTY(&pagedep->pd_dirremhd))
5911                                 continue;
5912                         mp = pagedep->pd_list.wk_mp;
5913                         ino = pagedep->pd_ino;
5914                         if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5915                                 continue;
5916                         FREE_LOCK(&lk);
5917                         if ((error = ffs_vget(mp, ino, LK_EXCLUSIVE, &vp))) {
5918                                 softdep_error("clear_remove: vget", error);
5919                                 vn_finished_write(mp);
5920                                 ACQUIRE_LOCK(&lk);
5921                                 return;
5922                         }
5923                         if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
5924                                 softdep_error("clear_remove: fsync", error);
5925                         VI_LOCK(vp);
5926                         drain_output(vp);
5927                         VI_UNLOCK(vp);
5928                         vput(vp);
5929                         vn_finished_write(mp);
5930                         ACQUIRE_LOCK(&lk);
5931                         return;
5932                 }
5933         }
5934 }
5935
5936 /*
5937  * Clear out a block of dirty inodes in an effort to reduce
5938  * the number of inodedep dependency structures.
5939  */
5940 static void
5941 clear_inodedeps(td)
5942         struct thread *td;
5943 {
5944         struct inodedep_hashhead *inodedephd;
5945         struct inodedep *inodedep;
5946         static int next = 0;
5947         struct mount *mp;
5948         struct vnode *vp;
5949         struct fs *fs;
5950         int error, cnt;
5951         ino_t firstino, lastino, ino;
5952
5953         mtx_assert(&lk, MA_OWNED);
5954         /*
5955          * Pick a random inode dependency to be cleared.
5956          * We will then gather up all the inodes in its block 
5957          * that have dependencies and flush them out.
5958          */
5959         for (cnt = 0; cnt < inodedep_hash; cnt++) {
5960                 inodedephd = &inodedep_hashtbl[next++];
5961                 if (next >= inodedep_hash)
5962                         next = 0;
5963                 if ((inodedep = LIST_FIRST(inodedephd)) != NULL)
5964                         break;
5965         }
5966         if (inodedep == NULL)
5967                 return;
5968         fs = inodedep->id_fs;
5969         mp = inodedep->id_list.wk_mp;
5970         /*
5971          * Find the last inode in the block with dependencies.
5972          */
5973         firstino = inodedep->id_ino & ~(INOPB(fs) - 1);
5974         for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--)
5975                 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0)
5976                         break;
5977         /*
5978          * Asynchronously push all but the last inode with dependencies.
5979          * Synchronously push the last inode with dependencies to ensure
5980          * that the inode block gets written to free up the inodedeps.
5981          */
5982         for (ino = firstino; ino <= lastino; ino++) {
5983                 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0)
5984                         continue;
5985                 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0)
5986                         continue;
5987                 FREE_LOCK(&lk);
5988                 if ((error = ffs_vget(mp, ino, LK_EXCLUSIVE, &vp)) != 0) {
5989                         softdep_error("clear_inodedeps: vget", error);
5990                         vn_finished_write(mp);
5991                         ACQUIRE_LOCK(&lk);
5992                         return;
5993                 }
5994                 if (ino == lastino) {
5995                         if ((error = ffs_syncvnode(vp, MNT_WAIT)))
5996                                 softdep_error("clear_inodedeps: fsync1", error);
5997                 } else {
5998                         if ((error = ffs_syncvnode(vp, MNT_NOWAIT)))
5999                                 softdep_error("clear_inodedeps: fsync2", error);
6000                         VI_LOCK(vp);
6001                         drain_output(vp);
6002                         VI_UNLOCK(vp);
6003                 }
6004                 vput(vp);
6005                 vn_finished_write(mp);
6006                 ACQUIRE_LOCK(&lk);
6007         }
6008 }
6009
6010 /*
6011  * Function to determine if the buffer has outstanding dependencies
6012  * that will cause a roll-back if the buffer is written. If wantcount
6013  * is set, return number of dependencies, otherwise just yes or no.
6014  */
6015 static int
6016 softdep_count_dependencies(bp, wantcount)
6017         struct buf *bp;
6018         int wantcount;
6019 {
6020         struct worklist *wk;
6021         struct inodedep *inodedep;
6022         struct indirdep *indirdep;
6023         struct allocindir *aip;
6024         struct pagedep *pagedep;
6025         struct diradd *dap;
6026         int i, retval;
6027
6028         retval = 0;
6029         ACQUIRE_LOCK(&lk);
6030         LIST_FOREACH(wk, &bp->b_dep, wk_list) {
6031                 switch (wk->wk_type) {
6032
6033                 case D_INODEDEP:
6034                         inodedep = WK_INODEDEP(wk);
6035                         if ((inodedep->id_state & DEPCOMPLETE) == 0) {
6036                                 /* bitmap allocation dependency */
6037                                 retval += 1;
6038                                 if (!wantcount)
6039                                         goto out;
6040                         }
6041                         if (TAILQ_FIRST(&inodedep->id_inoupdt)) {
6042                                 /* direct block pointer dependency */
6043                                 retval += 1;
6044                                 if (!wantcount)
6045                                         goto out;
6046                         }
6047                         if (TAILQ_FIRST(&inodedep->id_extupdt)) {
6048                                 /* direct block pointer dependency */
6049                                 retval += 1;
6050                                 if (!wantcount)
6051                                         goto out;
6052                         }
6053                         continue;
6054
6055                 case D_INDIRDEP:
6056                         indirdep = WK_INDIRDEP(wk);
6057
6058                         LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) {
6059                                 /* indirect block pointer dependency */
6060                                 retval += 1;
6061                                 if (!wantcount)
6062                                         goto out;
6063                         }
6064                         continue;
6065
6066                 case D_PAGEDEP:
6067                         pagedep = WK_PAGEDEP(wk);
6068                         for (i = 0; i < DAHASHSZ; i++) {
6069
6070                                 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) {
6071                                         /* directory entry dependency */
6072                                         retval += 1;
6073                                         if (!wantcount)
6074                                                 goto out;
6075                                 }
6076                         }
6077                         continue;
6078
6079                 case D_BMSAFEMAP:
6080                 case D_ALLOCDIRECT:
6081                 case D_ALLOCINDIR:
6082                 case D_MKDIR:
6083                         /* never a dependency on these blocks */
6084                         continue;
6085
6086                 default:
6087                         panic("softdep_check_for_rollback: Unexpected type %s",
6088                             TYPENAME(wk->wk_type));
6089                         /* NOTREACHED */
6090                 }
6091         }
6092 out:
6093         FREE_LOCK(&lk);
6094         return retval;
6095 }
6096
6097 /*
6098  * Acquire exclusive access to a buffer.
6099  * Must be called with a locked mtx parameter.
6100  * Return acquired buffer or NULL on failure.
6101  */
6102 static struct buf *
6103 getdirtybuf(bp, mtx, waitfor)
6104         struct buf *bp;
6105         struct mtx *mtx;
6106         int waitfor;
6107 {
6108         int error;
6109
6110         mtx_assert(mtx, MA_OWNED);
6111         if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
6112                 if (waitfor != MNT_WAIT)
6113                         return (NULL);
6114                 error = BUF_LOCK(bp,
6115                     LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx);
6116                 /*
6117                  * Even if we sucessfully acquire bp here, we have dropped
6118                  * mtx, which may violates our guarantee.
6119                  */
6120                 if (error == 0)
6121                         BUF_UNLOCK(bp);
6122                 else if (error != ENOLCK)
6123                         panic("getdirtybuf: inconsistent lock: %d", error);
6124                 mtx_lock(mtx);
6125                 return (NULL);
6126         }
6127         if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6128                 if (mtx == &lk && waitfor == MNT_WAIT) {
6129                         mtx_unlock(mtx);
6130                         BO_LOCK(bp->b_bufobj);
6131                         BUF_UNLOCK(bp);
6132                         if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
6133                                 bp->b_vflags |= BV_BKGRDWAIT;
6134                                 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj),
6135                                        PRIBIO | PDROP, "getbuf", 0);
6136                         } else
6137                                 BO_UNLOCK(bp->b_bufobj);
6138                         mtx_lock(mtx);
6139                         return (NULL);
6140                 }
6141                 BUF_UNLOCK(bp);
6142                 if (waitfor != MNT_WAIT)
6143                         return (NULL);
6144                 /*
6145                  * The mtx argument must be bp->b_vp's mutex in
6146                  * this case.
6147                  */
6148 #ifdef  DEBUG_VFS_LOCKS
6149                 if (bp->b_vp->v_type != VCHR)
6150                         ASSERT_VI_LOCKED(bp->b_vp, "getdirtybuf");
6151 #endif
6152                 bp->b_vflags |= BV_BKGRDWAIT;
6153                 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0);
6154                 return (NULL);
6155         }
6156         if ((bp->b_flags & B_DELWRI) == 0) {
6157                 BUF_UNLOCK(bp);
6158                 return (NULL);
6159         }
6160         bremfree(bp);
6161         return (bp);
6162 }
6163
6164
6165 /*
6166  * Check if it is safe to suspend the file system now.  On entry,
6167  * the vnode interlock for devvp should be held.  Return 0 with
6168  * the mount interlock held if the file system can be suspended now,
6169  * otherwise return EAGAIN with the mount interlock held.
6170  */
6171 int
6172 softdep_check_suspend(struct mount *mp,
6173                       struct vnode *devvp,
6174                       int softdep_deps,
6175                       int softdep_accdeps,
6176                       int secondary_writes,
6177                       int secondary_accwrites)
6178 {
6179         struct bufobj *bo;
6180         struct ufsmount *ump;
6181         int error;
6182
6183         ASSERT_VI_LOCKED(devvp, "softdep_check_suspend");
6184         ump = VFSTOUFS(mp);
6185         bo = &devvp->v_bufobj;
6186
6187         for (;;) {
6188                 if (!TRY_ACQUIRE_LOCK(&lk)) {
6189                         VI_UNLOCK(devvp);
6190                         ACQUIRE_LOCK(&lk);
6191                         FREE_LOCK(&lk);
6192                         VI_LOCK(devvp);
6193                         continue;
6194                 }
6195                 if (!MNT_ITRYLOCK(mp)) {
6196                         FREE_LOCK(&lk);
6197                         VI_UNLOCK(devvp);
6198                         MNT_ILOCK(mp);
6199                         MNT_IUNLOCK(mp);
6200                         VI_LOCK(devvp);
6201                         continue;
6202                 }
6203                 if (mp->mnt_secondary_writes != 0) {
6204                         FREE_LOCK(&lk);
6205                         VI_UNLOCK(devvp);
6206                         msleep(&mp->mnt_secondary_writes,
6207                                MNT_MTX(mp),
6208                                (PUSER - 1) | PDROP, "secwr", 0);
6209                         VI_LOCK(devvp);
6210                         continue;
6211                 }
6212                 break;
6213         }
6214
6215         /*
6216          * Reasons for needing more work before suspend:
6217          * - Dirty buffers on devvp.
6218          * - Softdep activity occurred after start of vnode sync loop
6219          * - Secondary writes occurred after start of vnode sync loop
6220          */
6221         error = 0;
6222         if (bo->bo_numoutput > 0 ||
6223             bo->bo_dirty.bv_cnt > 0 ||
6224             softdep_deps != 0 ||
6225             ump->softdep_deps != 0 ||
6226             softdep_accdeps != ump->softdep_accdeps ||
6227             secondary_writes != 0 ||
6228             mp->mnt_secondary_writes != 0 ||
6229             secondary_accwrites != mp->mnt_secondary_accwrites)
6230                 error = EAGAIN;
6231         FREE_LOCK(&lk);
6232         VI_UNLOCK(devvp);
6233         return (error);
6234 }
6235
6236
6237 /*
6238  * Get the number of dependency structures for the file system, both
6239  * the current number and the total number allocated.  These will
6240  * later be used to detect that softdep processing has occurred.
6241  */
6242 void
6243 softdep_get_depcounts(struct mount *mp,
6244                       int *softdep_depsp,
6245                       int *softdep_accdepsp)
6246 {
6247         struct ufsmount *ump;
6248
6249         ump = VFSTOUFS(mp);
6250         ACQUIRE_LOCK(&lk);
6251         *softdep_depsp = ump->softdep_deps;
6252         *softdep_accdepsp = ump->softdep_accdeps;
6253         FREE_LOCK(&lk);
6254 }
6255
6256 /*
6257  * Wait for pending output on a vnode to complete.
6258  * Must be called with vnode lock and interlock locked.
6259  *
6260  * XXX: Should just be a call to bufobj_wwait().
6261  */
6262 static void
6263 drain_output(vp)
6264         struct vnode *vp;
6265 {
6266         ASSERT_VOP_LOCKED(vp, "drain_output");
6267         ASSERT_VI_LOCKED(vp, "drain_output");
6268
6269         while (vp->v_bufobj.bo_numoutput) {
6270                 vp->v_bufobj.bo_flag |= BO_WWAIT;
6271                 msleep((caddr_t)&vp->v_bufobj.bo_numoutput,
6272                     VI_MTX(vp), PRIBIO + 1, "drainvp", 0);
6273         }
6274 }
6275
6276 /*
6277  * Called whenever a buffer that is being invalidated or reallocated
6278  * contains dependencies. This should only happen if an I/O error has
6279  * occurred. The routine is called with the buffer locked.
6280  */ 
6281 static void
6282 softdep_deallocate_dependencies(bp)
6283         struct buf *bp;
6284 {
6285
6286         if ((bp->b_ioflags & BIO_ERROR) == 0)
6287                 panic("softdep_deallocate_dependencies: dangling deps");
6288         softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error);
6289         panic("softdep_deallocate_dependencies: unrecovered I/O error");
6290 }
6291
6292 /*
6293  * Function to handle asynchronous write errors in the filesystem.
6294  */
6295 static void
6296 softdep_error(func, error)
6297         char *func;
6298         int error;
6299 {
6300
6301         /* XXX should do something better! */
6302         printf("%s: got error %d while accessing filesystem\n", func, error);
6303 }
6304
6305 #endif /* SOFTUPDATES */