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