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