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