1 /*- 2 * Copyright 1998, 2000 Marshall Kirk McKusick. 3 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org> 4 * All rights reserved. 5 * 6 * The soft updates code is derived from the appendix of a University 7 * of Michigan technical report (Gregory R. Ganger and Yale N. Patt, 8 * "Soft Updates: A Solution to the Metadata Update Problem in File 9 * Systems", CSE-TR-254-95, August 1995). 10 * 11 * Further information about soft updates can be obtained from: 12 * 13 * Marshall Kirk McKusick http://www.mckusick.com/softdep/ 14 * 1614 Oxford Street mckusick@mckusick.com 15 * Berkeley, CA 94709-1608 +1-510-843-9542 16 * USA 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions 20 * are met: 21 * 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * from: @(#)ffs_softdep.c 9.59 (McKusick) 6/21/00 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include "opt_ffs.h" 46 #include "opt_quota.h" 47 #include "opt_ddb.h" 48 49 /* 50 * For now we want the safety net that the DEBUG flag provides. 51 */ 52 #ifndef DEBUG 53 #define DEBUG 54 #endif 55 56 #include <sys/param.h> 57 #include <sys/kernel.h> 58 #include <sys/systm.h> 59 #include <sys/bio.h> 60 #include <sys/buf.h> 61 #include <sys/kdb.h> 62 #include <sys/kthread.h> 63 #include <sys/ktr.h> 64 #include <sys/limits.h> 65 #include <sys/lock.h> 66 #include <sys/malloc.h> 67 #include <sys/mount.h> 68 #include <sys/mutex.h> 69 #include <sys/namei.h> 70 #include <sys/priv.h> 71 #include <sys/proc.h> 72 #include <sys/rwlock.h> 73 #include <sys/stat.h> 74 #include <sys/sysctl.h> 75 #include <sys/syslog.h> 76 #include <sys/vnode.h> 77 #include <sys/conf.h> 78 79 #include <ufs/ufs/dir.h> 80 #include <ufs/ufs/extattr.h> 81 #include <ufs/ufs/quota.h> 82 #include <ufs/ufs/inode.h> 83 #include <ufs/ufs/ufsmount.h> 84 #include <ufs/ffs/fs.h> 85 #include <ufs/ffs/softdep.h> 86 #include <ufs/ffs/ffs_extern.h> 87 #include <ufs/ufs/ufs_extern.h> 88 89 #include <vm/vm.h> 90 #include <vm/vm_extern.h> 91 #include <vm/vm_object.h> 92 93 #include <geom/geom.h> 94 95 #include <ddb/ddb.h> 96 97 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 98 99 #ifndef SOFTUPDATES 100 101 int 102 softdep_flushfiles(oldmnt, flags, td) 103 struct mount *oldmnt; 104 int flags; 105 struct thread *td; 106 { 107 108 panic("softdep_flushfiles called"); 109 } 110 111 int 112 softdep_mount(devvp, mp, fs, cred) 113 struct vnode *devvp; 114 struct mount *mp; 115 struct fs *fs; 116 struct ucred *cred; 117 { 118 119 return (0); 120 } 121 122 void 123 softdep_initialize() 124 { 125 126 return; 127 } 128 129 void 130 softdep_uninitialize() 131 { 132 133 return; 134 } 135 136 void 137 softdep_unmount(mp) 138 struct mount *mp; 139 { 140 141 panic("softdep_unmount called"); 142 } 143 144 void 145 softdep_setup_sbupdate(ump, fs, bp) 146 struct ufsmount *ump; 147 struct fs *fs; 148 struct buf *bp; 149 { 150 151 panic("softdep_setup_sbupdate called"); 152 } 153 154 void 155 softdep_setup_inomapdep(bp, ip, newinum, mode) 156 struct buf *bp; 157 struct inode *ip; 158 ino_t newinum; 159 int mode; 160 { 161 162 panic("softdep_setup_inomapdep called"); 163 } 164 165 void 166 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 167 struct buf *bp; 168 struct mount *mp; 169 ufs2_daddr_t newblkno; 170 int frags; 171 int oldfrags; 172 { 173 174 panic("softdep_setup_blkmapdep called"); 175 } 176 177 void 178 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 179 struct inode *ip; 180 ufs_lbn_t lbn; 181 ufs2_daddr_t newblkno; 182 ufs2_daddr_t oldblkno; 183 long newsize; 184 long oldsize; 185 struct buf *bp; 186 { 187 188 panic("softdep_setup_allocdirect called"); 189 } 190 191 void 192 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 193 struct inode *ip; 194 ufs_lbn_t lbn; 195 ufs2_daddr_t newblkno; 196 ufs2_daddr_t oldblkno; 197 long newsize; 198 long oldsize; 199 struct buf *bp; 200 { 201 202 panic("softdep_setup_allocext called"); 203 } 204 205 void 206 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 207 struct inode *ip; 208 ufs_lbn_t lbn; 209 struct buf *bp; 210 int ptrno; 211 ufs2_daddr_t newblkno; 212 ufs2_daddr_t oldblkno; 213 struct buf *nbp; 214 { 215 216 panic("softdep_setup_allocindir_page called"); 217 } 218 219 void 220 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 221 struct buf *nbp; 222 struct inode *ip; 223 struct buf *bp; 224 int ptrno; 225 ufs2_daddr_t newblkno; 226 { 227 228 panic("softdep_setup_allocindir_meta called"); 229 } 230 231 void 232 softdep_journal_freeblocks(ip, cred, length, flags) 233 struct inode *ip; 234 struct ucred *cred; 235 off_t length; 236 int flags; 237 { 238 239 panic("softdep_journal_freeblocks called"); 240 } 241 242 void 243 softdep_journal_fsync(ip) 244 struct inode *ip; 245 { 246 247 panic("softdep_journal_fsync called"); 248 } 249 250 void 251 softdep_setup_freeblocks(ip, length, flags) 252 struct inode *ip; 253 off_t length; 254 int flags; 255 { 256 257 panic("softdep_setup_freeblocks called"); 258 } 259 260 void 261 softdep_freefile(pvp, ino, mode) 262 struct vnode *pvp; 263 ino_t ino; 264 int mode; 265 { 266 267 panic("softdep_freefile called"); 268 } 269 270 int 271 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 272 struct buf *bp; 273 struct inode *dp; 274 off_t diroffset; 275 ino_t newinum; 276 struct buf *newdirbp; 277 int isnewblk; 278 { 279 280 panic("softdep_setup_directory_add called"); 281 } 282 283 void 284 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 285 struct buf *bp; 286 struct inode *dp; 287 caddr_t base; 288 caddr_t oldloc; 289 caddr_t newloc; 290 int entrysize; 291 { 292 293 panic("softdep_change_directoryentry_offset called"); 294 } 295 296 void 297 softdep_setup_remove(bp, dp, ip, isrmdir) 298 struct buf *bp; 299 struct inode *dp; 300 struct inode *ip; 301 int isrmdir; 302 { 303 304 panic("softdep_setup_remove called"); 305 } 306 307 void 308 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 309 struct buf *bp; 310 struct inode *dp; 311 struct inode *ip; 312 ino_t newinum; 313 int isrmdir; 314 { 315 316 panic("softdep_setup_directory_change called"); 317 } 318 319 void 320 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 321 struct mount *mp; 322 struct buf *bp; 323 ufs2_daddr_t blkno; 324 int frags; 325 struct workhead *wkhd; 326 { 327 328 panic("%s called", __FUNCTION__); 329 } 330 331 void 332 softdep_setup_inofree(mp, bp, ino, wkhd) 333 struct mount *mp; 334 struct buf *bp; 335 ino_t ino; 336 struct workhead *wkhd; 337 { 338 339 panic("%s called", __FUNCTION__); 340 } 341 342 void 343 softdep_setup_unlink(dp, ip) 344 struct inode *dp; 345 struct inode *ip; 346 { 347 348 panic("%s called", __FUNCTION__); 349 } 350 351 void 352 softdep_setup_link(dp, ip) 353 struct inode *dp; 354 struct inode *ip; 355 { 356 357 panic("%s called", __FUNCTION__); 358 } 359 360 void 361 softdep_revert_link(dp, ip) 362 struct inode *dp; 363 struct inode *ip; 364 { 365 366 panic("%s called", __FUNCTION__); 367 } 368 369 void 370 softdep_setup_rmdir(dp, ip) 371 struct inode *dp; 372 struct inode *ip; 373 { 374 375 panic("%s called", __FUNCTION__); 376 } 377 378 void 379 softdep_revert_rmdir(dp, ip) 380 struct inode *dp; 381 struct inode *ip; 382 { 383 384 panic("%s called", __FUNCTION__); 385 } 386 387 void 388 softdep_setup_create(dp, ip) 389 struct inode *dp; 390 struct inode *ip; 391 { 392 393 panic("%s called", __FUNCTION__); 394 } 395 396 void 397 softdep_revert_create(dp, ip) 398 struct inode *dp; 399 struct inode *ip; 400 { 401 402 panic("%s called", __FUNCTION__); 403 } 404 405 void 406 softdep_setup_mkdir(dp, ip) 407 struct inode *dp; 408 struct inode *ip; 409 { 410 411 panic("%s called", __FUNCTION__); 412 } 413 414 void 415 softdep_revert_mkdir(dp, ip) 416 struct inode *dp; 417 struct inode *ip; 418 { 419 420 panic("%s called", __FUNCTION__); 421 } 422 423 void 424 softdep_setup_dotdot_link(dp, ip) 425 struct inode *dp; 426 struct inode *ip; 427 { 428 429 panic("%s called", __FUNCTION__); 430 } 431 432 int 433 softdep_prealloc(vp, waitok) 434 struct vnode *vp; 435 int waitok; 436 { 437 438 panic("%s called", __FUNCTION__); 439 } 440 441 int 442 softdep_journal_lookup(mp, vpp) 443 struct mount *mp; 444 struct vnode **vpp; 445 { 446 447 return (ENOENT); 448 } 449 450 void 451 softdep_change_linkcnt(ip) 452 struct inode *ip; 453 { 454 455 panic("softdep_change_linkcnt called"); 456 } 457 458 void 459 softdep_load_inodeblock(ip) 460 struct inode *ip; 461 { 462 463 panic("softdep_load_inodeblock called"); 464 } 465 466 void 467 softdep_update_inodeblock(ip, bp, waitfor) 468 struct inode *ip; 469 struct buf *bp; 470 int waitfor; 471 { 472 473 panic("softdep_update_inodeblock called"); 474 } 475 476 int 477 softdep_fsync(vp) 478 struct vnode *vp; /* the "in_core" copy of the inode */ 479 { 480 481 return (0); 482 } 483 484 void 485 softdep_fsync_mountdev(vp) 486 struct vnode *vp; 487 { 488 489 return; 490 } 491 492 int 493 softdep_flushworklist(oldmnt, countp, td) 494 struct mount *oldmnt; 495 int *countp; 496 struct thread *td; 497 { 498 499 *countp = 0; 500 return (0); 501 } 502 503 int 504 softdep_sync_metadata(struct vnode *vp) 505 { 506 507 panic("softdep_sync_metadata called"); 508 } 509 510 int 511 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 512 { 513 514 panic("softdep_sync_buf called"); 515 } 516 517 int 518 softdep_slowdown(vp) 519 struct vnode *vp; 520 { 521 522 panic("softdep_slowdown called"); 523 } 524 525 int 526 softdep_request_cleanup(fs, vp, cred, resource) 527 struct fs *fs; 528 struct vnode *vp; 529 struct ucred *cred; 530 int resource; 531 { 532 533 return (0); 534 } 535 536 int 537 softdep_check_suspend(struct mount *mp, 538 struct vnode *devvp, 539 int softdep_depcnt, 540 int softdep_accdepcnt, 541 int secondary_writes, 542 int secondary_accwrites) 543 { 544 struct bufobj *bo; 545 int error; 546 547 (void) softdep_depcnt, 548 (void) softdep_accdepcnt; 549 550 bo = &devvp->v_bufobj; 551 ASSERT_BO_WLOCKED(bo); 552 553 MNT_ILOCK(mp); 554 while (mp->mnt_secondary_writes != 0) { 555 BO_UNLOCK(bo); 556 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 557 (PUSER - 1) | PDROP, "secwr", 0); 558 BO_LOCK(bo); 559 MNT_ILOCK(mp); 560 } 561 562 /* 563 * Reasons for needing more work before suspend: 564 * - Dirty buffers on devvp. 565 * - Secondary writes occurred after start of vnode sync loop 566 */ 567 error = 0; 568 if (bo->bo_numoutput > 0 || 569 bo->bo_dirty.bv_cnt > 0 || 570 secondary_writes != 0 || 571 mp->mnt_secondary_writes != 0 || 572 secondary_accwrites != mp->mnt_secondary_accwrites) 573 error = EAGAIN; 574 BO_UNLOCK(bo); 575 return (error); 576 } 577 578 void 579 softdep_get_depcounts(struct mount *mp, 580 int *softdepactivep, 581 int *softdepactiveaccp) 582 { 583 (void) mp; 584 *softdepactivep = 0; 585 *softdepactiveaccp = 0; 586 } 587 588 void 589 softdep_buf_append(bp, wkhd) 590 struct buf *bp; 591 struct workhead *wkhd; 592 { 593 594 panic("softdep_buf_appendwork called"); 595 } 596 597 void 598 softdep_inode_append(ip, cred, wkhd) 599 struct inode *ip; 600 struct ucred *cred; 601 struct workhead *wkhd; 602 { 603 604 panic("softdep_inode_appendwork called"); 605 } 606 607 void 608 softdep_freework(wkhd) 609 struct workhead *wkhd; 610 { 611 612 panic("softdep_freework called"); 613 } 614 615 #else 616 617 FEATURE(softupdates, "FFS soft-updates support"); 618 619 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 620 "soft updates stats"); 621 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 622 "total dependencies allocated"); 623 static SYSCTL_NODE(_debug_softdep, OID_AUTO, highuse, CTLFLAG_RW, 0, 624 "high use dependencies allocated"); 625 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 626 "current dependencies allocated"); 627 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 628 "current dependencies written"); 629 630 unsigned long dep_current[D_LAST + 1]; 631 unsigned long dep_highuse[D_LAST + 1]; 632 unsigned long dep_total[D_LAST + 1]; 633 unsigned long dep_write[D_LAST + 1]; 634 635 #define SOFTDEP_TYPE(type, str, long) \ 636 static MALLOC_DEFINE(M_ ## type, #str, long); \ 637 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 638 &dep_total[D_ ## type], 0, ""); \ 639 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 640 &dep_current[D_ ## type], 0, ""); \ 641 SYSCTL_ULONG(_debug_softdep_highuse, OID_AUTO, str, CTLFLAG_RD, \ 642 &dep_highuse[D_ ## type], 0, ""); \ 643 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 644 &dep_write[D_ ## type], 0, ""); 645 646 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 647 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 648 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 649 "Block or frag allocated from cyl group map"); 650 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 651 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 652 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 653 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 654 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 655 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 656 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 657 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 658 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 659 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 660 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 661 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 662 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 663 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 664 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 665 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 666 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 667 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 668 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 669 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 670 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 671 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 672 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 673 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 674 675 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 676 677 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 678 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 679 static MALLOC_DEFINE(M_MOUNTDATA, "softdep", "Softdep per-mount data"); 680 681 #define M_SOFTDEP_FLAGS (M_WAITOK) 682 683 /* 684 * translate from workitem type to memory type 685 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 686 */ 687 static struct malloc_type *memtype[] = { 688 M_PAGEDEP, 689 M_INODEDEP, 690 M_BMSAFEMAP, 691 M_NEWBLK, 692 M_ALLOCDIRECT, 693 M_INDIRDEP, 694 M_ALLOCINDIR, 695 M_FREEFRAG, 696 M_FREEBLKS, 697 M_FREEFILE, 698 M_DIRADD, 699 M_MKDIR, 700 M_DIRREM, 701 M_NEWDIRBLK, 702 M_FREEWORK, 703 M_FREEDEP, 704 M_JADDREF, 705 M_JREMREF, 706 M_JMVREF, 707 M_JNEWBLK, 708 M_JFREEBLK, 709 M_JFREEFRAG, 710 M_JSEG, 711 M_JSEGDEP, 712 M_SBDEP, 713 M_JTRUNC, 714 M_JFSYNC, 715 M_SENTINEL 716 }; 717 718 #define DtoM(type) (memtype[type]) 719 720 /* 721 * Names of malloc types. 722 */ 723 #define TYPENAME(type) \ 724 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 725 /* 726 * End system adaptation definitions. 727 */ 728 729 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 730 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 731 732 /* 733 * Internal function prototypes. 734 */ 735 static void check_clear_deps(struct mount *); 736 static void softdep_error(char *, int); 737 static int softdep_process_worklist(struct mount *, int); 738 static int softdep_waitidle(struct mount *, int); 739 static void drain_output(struct vnode *); 740 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 741 static void clear_remove(struct mount *); 742 static void clear_inodedeps(struct mount *); 743 static void unlinked_inodedep(struct mount *, struct inodedep *); 744 static void clear_unlinked_inodedep(struct inodedep *); 745 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 746 static int flush_pagedep_deps(struct vnode *, struct mount *, 747 struct diraddhd *); 748 static int free_pagedep(struct pagedep *); 749 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 750 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 751 static int flush_deplist(struct allocdirectlst *, int, int *); 752 static int sync_cgs(struct mount *, int); 753 static int handle_written_filepage(struct pagedep *, struct buf *); 754 static int handle_written_sbdep(struct sbdep *, struct buf *); 755 static void initiate_write_sbdep(struct sbdep *); 756 static void diradd_inode_written(struct diradd *, struct inodedep *); 757 static int handle_written_indirdep(struct indirdep *, struct buf *, 758 struct buf**); 759 static int handle_written_inodeblock(struct inodedep *, struct buf *); 760 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 761 uint8_t *); 762 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *); 763 static void handle_written_jaddref(struct jaddref *); 764 static void handle_written_jremref(struct jremref *); 765 static void handle_written_jseg(struct jseg *, struct buf *); 766 static void handle_written_jnewblk(struct jnewblk *); 767 static void handle_written_jblkdep(struct jblkdep *); 768 static void handle_written_jfreefrag(struct jfreefrag *); 769 static void complete_jseg(struct jseg *); 770 static void complete_jsegs(struct jseg *); 771 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 772 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 773 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 774 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 775 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 776 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 777 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 778 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 779 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 780 static inline void inoref_write(struct inoref *, struct jseg *, 781 struct jrefrec *); 782 static void handle_allocdirect_partdone(struct allocdirect *, 783 struct workhead *); 784 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 785 struct workhead *); 786 static void indirdep_complete(struct indirdep *); 787 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 788 static void indirblk_insert(struct freework *); 789 static void indirblk_remove(struct freework *); 790 static void handle_allocindir_partdone(struct allocindir *); 791 static void initiate_write_filepage(struct pagedep *, struct buf *); 792 static void initiate_write_indirdep(struct indirdep*, struct buf *); 793 static void handle_written_mkdir(struct mkdir *, int); 794 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 795 uint8_t *); 796 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 797 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 798 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 799 static void handle_workitem_freefile(struct freefile *); 800 static int handle_workitem_remove(struct dirrem *, int); 801 static struct dirrem *newdirrem(struct buf *, struct inode *, 802 struct inode *, int, struct dirrem **); 803 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 804 struct buf *); 805 static void cancel_indirdep(struct indirdep *, struct buf *, 806 struct freeblks *); 807 static void free_indirdep(struct indirdep *); 808 static void free_diradd(struct diradd *, struct workhead *); 809 static void merge_diradd(struct inodedep *, struct diradd *); 810 static void complete_diradd(struct diradd *); 811 static struct diradd *diradd_lookup(struct pagedep *, int); 812 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 813 struct jremref *); 814 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 815 struct jremref *); 816 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 817 struct jremref *, struct jremref *); 818 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 819 struct jremref *); 820 static void cancel_allocindir(struct allocindir *, struct buf *bp, 821 struct freeblks *, int); 822 static int setup_trunc_indir(struct freeblks *, struct inode *, 823 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 824 static void complete_trunc_indir(struct freework *); 825 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 826 int); 827 static void complete_mkdir(struct mkdir *); 828 static void free_newdirblk(struct newdirblk *); 829 static void free_jremref(struct jremref *); 830 static void free_jaddref(struct jaddref *); 831 static void free_jsegdep(struct jsegdep *); 832 static void free_jsegs(struct jblocks *); 833 static void rele_jseg(struct jseg *); 834 static void free_jseg(struct jseg *, struct jblocks *); 835 static void free_jnewblk(struct jnewblk *); 836 static void free_jblkdep(struct jblkdep *); 837 static void free_jfreefrag(struct jfreefrag *); 838 static void free_freedep(struct freedep *); 839 static void journal_jremref(struct dirrem *, struct jremref *, 840 struct inodedep *); 841 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 842 static int cancel_jaddref(struct jaddref *, struct inodedep *, 843 struct workhead *); 844 static void cancel_jfreefrag(struct jfreefrag *); 845 static inline void setup_freedirect(struct freeblks *, struct inode *, 846 int, int); 847 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 848 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 849 ufs_lbn_t, int); 850 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 851 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 852 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 853 static ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 854 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 855 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 856 int, int); 857 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 858 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 859 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 860 static void newblk_freefrag(struct newblk*); 861 static void free_newblk(struct newblk *); 862 static void cancel_allocdirect(struct allocdirectlst *, 863 struct allocdirect *, struct freeblks *); 864 static int check_inode_unwritten(struct inodedep *); 865 static int free_inodedep(struct inodedep *); 866 static void freework_freeblock(struct freework *); 867 static void freework_enqueue(struct freework *); 868 static int handle_workitem_freeblocks(struct freeblks *, int); 869 static int handle_complete_freeblocks(struct freeblks *, int); 870 static void handle_workitem_indirblk(struct freework *); 871 static void handle_written_freework(struct freework *); 872 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 873 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 874 struct workhead *); 875 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 876 struct inodedep *, struct allocindir *, ufs_lbn_t); 877 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 878 ufs2_daddr_t, ufs_lbn_t); 879 static void handle_workitem_freefrag(struct freefrag *); 880 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 881 ufs_lbn_t); 882 static void allocdirect_merge(struct allocdirectlst *, 883 struct allocdirect *, struct allocdirect *); 884 static struct freefrag *allocindir_merge(struct allocindir *, 885 struct allocindir *); 886 static int bmsafemap_find(struct bmsafemap_hashhead *, int, 887 struct bmsafemap **); 888 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 889 int cg, struct bmsafemap *); 890 static int newblk_find(struct newblk_hashhead *, ufs2_daddr_t, int, 891 struct newblk **); 892 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 893 static int inodedep_find(struct inodedep_hashhead *, ino_t, 894 struct inodedep **); 895 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 896 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 897 int, struct pagedep **); 898 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 899 struct pagedep **); 900 static void pause_timer(void *); 901 static int request_cleanup(struct mount *, int); 902 static int process_worklist_item(struct mount *, int, int); 903 static void process_removes(struct vnode *); 904 static void process_truncates(struct vnode *); 905 static void jwork_move(struct workhead *, struct workhead *); 906 static void jwork_insert(struct workhead *, struct jsegdep *); 907 static void add_to_worklist(struct worklist *, int); 908 static void wake_worklist(struct worklist *); 909 static void wait_worklist(struct worklist *, char *); 910 static void remove_from_worklist(struct worklist *); 911 static void softdep_flush(void *); 912 static void softdep_flushjournal(struct mount *); 913 static int softdep_speedup(struct ufsmount *); 914 static void worklist_speedup(struct mount *); 915 static int journal_mount(struct mount *, struct fs *, struct ucred *); 916 static void journal_unmount(struct ufsmount *); 917 static int journal_space(struct ufsmount *, int); 918 static void journal_suspend(struct ufsmount *); 919 static int journal_unsuspend(struct ufsmount *ump); 920 static void softdep_prelink(struct vnode *, struct vnode *); 921 static void add_to_journal(struct worklist *); 922 static void remove_from_journal(struct worklist *); 923 static void softdep_process_journal(struct mount *, struct worklist *, int); 924 static struct jremref *newjremref(struct dirrem *, struct inode *, 925 struct inode *ip, off_t, nlink_t); 926 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 927 uint16_t); 928 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 929 uint16_t); 930 static inline struct jsegdep *inoref_jseg(struct inoref *); 931 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 932 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 933 ufs2_daddr_t, int); 934 static void adjust_newfreework(struct freeblks *, int); 935 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 936 static void move_newblock_dep(struct jaddref *, struct inodedep *); 937 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 938 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 939 ufs2_daddr_t, long, ufs_lbn_t); 940 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 941 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 942 static int jwait(struct worklist *, int); 943 static struct inodedep *inodedep_lookup_ip(struct inode *); 944 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 945 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 946 static void handle_jwork(struct workhead *); 947 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 948 struct mkdir **); 949 static struct jblocks *jblocks_create(void); 950 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 951 static void jblocks_free(struct jblocks *, struct mount *, int); 952 static void jblocks_destroy(struct jblocks *); 953 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 954 955 /* 956 * Exported softdep operations. 957 */ 958 static void softdep_disk_io_initiation(struct buf *); 959 static void softdep_disk_write_complete(struct buf *); 960 static void softdep_deallocate_dependencies(struct buf *); 961 static int softdep_count_dependencies(struct buf *bp, int); 962 963 /* 964 * Global lock over all of soft updates. 965 */ 966 static struct mtx lk; 967 MTX_SYSINIT(softdep_lock, &lk, "Global Softdep Lock", MTX_DEF); 968 969 #define ACQUIRE_GBLLOCK(lk) mtx_lock(lk) 970 #define FREE_GBLLOCK(lk) mtx_unlock(lk) 971 #define GBLLOCK_OWNED(lk) mtx_assert((lk), MA_OWNED) 972 973 /* 974 * Per-filesystem soft-updates locking. 975 */ 976 #define LOCK_PTR(ump) (&(ump)->um_softdep->sd_fslock) 977 #define TRY_ACQUIRE_LOCK(ump) rw_try_wlock(&(ump)->um_softdep->sd_fslock) 978 #define ACQUIRE_LOCK(ump) rw_wlock(&(ump)->um_softdep->sd_fslock) 979 #define FREE_LOCK(ump) rw_wunlock(&(ump)->um_softdep->sd_fslock) 980 #define LOCK_OWNED(ump) rw_assert(&(ump)->um_softdep->sd_fslock, \ 981 RA_WLOCKED) 982 983 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 984 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 985 986 /* 987 * Worklist queue management. 988 * These routines require that the lock be held. 989 */ 990 #ifndef /* NOT */ DEBUG 991 #define WORKLIST_INSERT(head, item) do { \ 992 (item)->wk_state |= ONWORKLIST; \ 993 LIST_INSERT_HEAD(head, item, wk_list); \ 994 } while (0) 995 #define WORKLIST_REMOVE(item) do { \ 996 (item)->wk_state &= ~ONWORKLIST; \ 997 LIST_REMOVE(item, wk_list); \ 998 } while (0) 999 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1000 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1001 1002 #else /* DEBUG */ 1003 static void worklist_insert(struct workhead *, struct worklist *, int); 1004 static void worklist_remove(struct worklist *, int); 1005 1006 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1007 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1008 #define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1009 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1010 1011 static void 1012 worklist_insert(head, item, locked) 1013 struct workhead *head; 1014 struct worklist *item; 1015 int locked; 1016 { 1017 1018 if (locked) 1019 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1020 if (item->wk_state & ONWORKLIST) 1021 panic("worklist_insert: %p %s(0x%X) already on list", 1022 item, TYPENAME(item->wk_type), item->wk_state); 1023 item->wk_state |= ONWORKLIST; 1024 LIST_INSERT_HEAD(head, item, wk_list); 1025 } 1026 1027 static void 1028 worklist_remove(item, locked) 1029 struct worklist *item; 1030 int locked; 1031 { 1032 1033 if (locked) 1034 LOCK_OWNED(VFSTOUFS(item->wk_mp)); 1035 if ((item->wk_state & ONWORKLIST) == 0) 1036 panic("worklist_remove: %p %s(0x%X) not on list", 1037 item, TYPENAME(item->wk_type), item->wk_state); 1038 item->wk_state &= ~ONWORKLIST; 1039 LIST_REMOVE(item, wk_list); 1040 } 1041 #endif /* DEBUG */ 1042 1043 /* 1044 * Merge two jsegdeps keeping only the oldest one as newer references 1045 * can't be discarded until after older references. 1046 */ 1047 static inline struct jsegdep * 1048 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1049 { 1050 struct jsegdep *swp; 1051 1052 if (two == NULL) 1053 return (one); 1054 1055 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1056 swp = one; 1057 one = two; 1058 two = swp; 1059 } 1060 WORKLIST_REMOVE(&two->jd_list); 1061 free_jsegdep(two); 1062 1063 return (one); 1064 } 1065 1066 /* 1067 * If two freedeps are compatible free one to reduce list size. 1068 */ 1069 static inline struct freedep * 1070 freedep_merge(struct freedep *one, struct freedep *two) 1071 { 1072 if (two == NULL) 1073 return (one); 1074 1075 if (one->fd_freework == two->fd_freework) { 1076 WORKLIST_REMOVE(&two->fd_list); 1077 free_freedep(two); 1078 } 1079 return (one); 1080 } 1081 1082 /* 1083 * Move journal work from one list to another. Duplicate freedeps and 1084 * jsegdeps are coalesced to keep the lists as small as possible. 1085 */ 1086 static void 1087 jwork_move(dst, src) 1088 struct workhead *dst; 1089 struct workhead *src; 1090 { 1091 struct freedep *freedep; 1092 struct jsegdep *jsegdep; 1093 struct worklist *wkn; 1094 struct worklist *wk; 1095 1096 KASSERT(dst != src, 1097 ("jwork_move: dst == src")); 1098 freedep = NULL; 1099 jsegdep = NULL; 1100 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1101 if (wk->wk_type == D_JSEGDEP) 1102 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1103 else if (wk->wk_type == D_FREEDEP) 1104 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1105 } 1106 1107 while ((wk = LIST_FIRST(src)) != NULL) { 1108 WORKLIST_REMOVE(wk); 1109 WORKLIST_INSERT(dst, wk); 1110 if (wk->wk_type == D_JSEGDEP) { 1111 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1112 continue; 1113 } 1114 if (wk->wk_type == D_FREEDEP) 1115 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1116 } 1117 } 1118 1119 static void 1120 jwork_insert(dst, jsegdep) 1121 struct workhead *dst; 1122 struct jsegdep *jsegdep; 1123 { 1124 struct jsegdep *jsegdepn; 1125 struct worklist *wk; 1126 1127 LIST_FOREACH(wk, dst, wk_list) 1128 if (wk->wk_type == D_JSEGDEP) 1129 break; 1130 if (wk == NULL) { 1131 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1132 return; 1133 } 1134 jsegdepn = WK_JSEGDEP(wk); 1135 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1136 WORKLIST_REMOVE(wk); 1137 free_jsegdep(jsegdepn); 1138 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1139 } else 1140 free_jsegdep(jsegdep); 1141 } 1142 1143 /* 1144 * Routines for tracking and managing workitems. 1145 */ 1146 static void workitem_free(struct worklist *, int); 1147 static void workitem_alloc(struct worklist *, int, struct mount *); 1148 static void workitem_reassign(struct worklist *, int); 1149 1150 #define WORKITEM_FREE(item, type) \ 1151 workitem_free((struct worklist *)(item), (type)) 1152 #define WORKITEM_REASSIGN(item, type) \ 1153 workitem_reassign((struct worklist *)(item), (type)) 1154 1155 static void 1156 workitem_free(item, type) 1157 struct worklist *item; 1158 int type; 1159 { 1160 struct ufsmount *ump; 1161 1162 #ifdef DEBUG 1163 if (item->wk_state & ONWORKLIST) 1164 panic("workitem_free: %s(0x%X) still on list", 1165 TYPENAME(item->wk_type), item->wk_state); 1166 if (item->wk_type != type && type != D_NEWBLK) 1167 panic("workitem_free: type mismatch %s != %s", 1168 TYPENAME(item->wk_type), TYPENAME(type)); 1169 #endif 1170 if (item->wk_state & IOWAITING) 1171 wakeup(item); 1172 ump = VFSTOUFS(item->wk_mp); 1173 LOCK_OWNED(ump); 1174 KASSERT(ump->softdep_deps > 0, 1175 ("workitem_free: %s: softdep_deps going negative", 1176 ump->um_fs->fs_fsmnt)); 1177 if (--ump->softdep_deps == 0 && ump->softdep_req) 1178 wakeup(&ump->softdep_deps); 1179 KASSERT(dep_current[item->wk_type] > 0, 1180 ("workitem_free: %s: dep_current[%s] going negative", 1181 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1182 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1183 ("workitem_free: %s: softdep_curdeps[%s] going negative", 1184 ump->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1185 atomic_subtract_long(&dep_current[item->wk_type], 1); 1186 ump->softdep_curdeps[item->wk_type] -= 1; 1187 free(item, DtoM(type)); 1188 } 1189 1190 static void 1191 workitem_alloc(item, type, mp) 1192 struct worklist *item; 1193 int type; 1194 struct mount *mp; 1195 { 1196 struct ufsmount *ump; 1197 1198 item->wk_type = type; 1199 item->wk_mp = mp; 1200 item->wk_state = 0; 1201 1202 ump = VFSTOUFS(mp); 1203 ACQUIRE_GBLLOCK(&lk); 1204 dep_current[type]++; 1205 if (dep_current[type] > dep_highuse[type]) 1206 dep_highuse[type] = dep_current[type]; 1207 dep_total[type]++; 1208 FREE_GBLLOCK(&lk); 1209 ACQUIRE_LOCK(ump); 1210 ump->softdep_curdeps[type] += 1; 1211 ump->softdep_deps++; 1212 ump->softdep_accdeps++; 1213 FREE_LOCK(ump); 1214 } 1215 1216 static void 1217 workitem_reassign(item, newtype) 1218 struct worklist *item; 1219 int newtype; 1220 { 1221 struct ufsmount *ump; 1222 1223 ump = VFSTOUFS(item->wk_mp); 1224 LOCK_OWNED(ump); 1225 KASSERT(ump->softdep_curdeps[item->wk_type] > 0, 1226 ("workitem_reassign: %s: softdep_curdeps[%s] going negative", 1227 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1228 ump->softdep_curdeps[item->wk_type] -= 1; 1229 ump->softdep_curdeps[newtype] += 1; 1230 KASSERT(dep_current[item->wk_type] > 0, 1231 ("workitem_reassign: %s: dep_current[%s] going negative", 1232 VFSTOUFS(item->wk_mp)->um_fs->fs_fsmnt, TYPENAME(item->wk_type))); 1233 ACQUIRE_GBLLOCK(&lk); 1234 dep_current[newtype]++; 1235 dep_current[item->wk_type]--; 1236 if (dep_current[newtype] > dep_highuse[newtype]) 1237 dep_highuse[newtype] = dep_current[newtype]; 1238 dep_total[newtype]++; 1239 FREE_GBLLOCK(&lk); 1240 item->wk_type = newtype; 1241 } 1242 1243 /* 1244 * Workitem queue management 1245 */ 1246 static int max_softdeps; /* maximum number of structs before slowdown */ 1247 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1248 static int proc_waiting; /* tracks whether we have a timeout posted */ 1249 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1250 static struct callout softdep_callout; 1251 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1252 static int req_clear_remove; /* syncer process flush some freeblks */ 1253 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1254 1255 /* 1256 * runtime statistics 1257 */ 1258 static int stat_flush_threads; /* number of softdep flushing threads */ 1259 static int stat_worklist_push; /* number of worklist cleanups */ 1260 static int stat_blk_limit_push; /* number of times block limit neared */ 1261 static int stat_ino_limit_push; /* number of times inode limit neared */ 1262 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1263 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1264 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1265 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1266 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1267 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1268 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1269 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1270 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1271 static int stat_journal_min; /* Times hit journal min threshold */ 1272 static int stat_journal_low; /* Times hit journal low threshold */ 1273 static int stat_journal_wait; /* Times blocked in jwait(). */ 1274 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1275 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1276 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1277 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1278 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1279 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1280 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1281 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1282 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1283 static int stat_emptyjblocks; /* Number of potentially empty journal blocks */ 1284 1285 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1286 &max_softdeps, 0, ""); 1287 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1288 &tickdelay, 0, ""); 1289 SYSCTL_INT(_debug_softdep, OID_AUTO, flush_threads, CTLFLAG_RD, 1290 &stat_flush_threads, 0, ""); 1291 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1292 &stat_worklist_push, 0,""); 1293 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1294 &stat_blk_limit_push, 0,""); 1295 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1296 &stat_ino_limit_push, 0,""); 1297 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1298 &stat_blk_limit_hit, 0, ""); 1299 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1300 &stat_ino_limit_hit, 0, ""); 1301 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1302 &stat_sync_limit_hit, 0, ""); 1303 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1304 &stat_indir_blk_ptrs, 0, ""); 1305 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1306 &stat_inode_bitmap, 0, ""); 1307 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1308 &stat_direct_blk_ptrs, 0, ""); 1309 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1310 &stat_dir_entry, 0, ""); 1311 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1312 &stat_jaddref, 0, ""); 1313 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1314 &stat_jnewblk, 0, ""); 1315 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1316 &stat_journal_low, 0, ""); 1317 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1318 &stat_journal_min, 0, ""); 1319 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1320 &stat_journal_wait, 0, ""); 1321 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1322 &stat_jwait_filepage, 0, ""); 1323 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1324 &stat_jwait_freeblks, 0, ""); 1325 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1326 &stat_jwait_inode, 0, ""); 1327 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1328 &stat_jwait_newblk, 0, ""); 1329 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1330 &stat_cleanup_blkrequests, 0, ""); 1331 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1332 &stat_cleanup_inorequests, 0, ""); 1333 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1334 &stat_cleanup_high_delay, 0, ""); 1335 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1336 &stat_cleanup_retries, 0, ""); 1337 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1338 &stat_cleanup_failures, 0, ""); 1339 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1340 &softdep_flushcache, 0, ""); 1341 SYSCTL_INT(_debug_softdep, OID_AUTO, emptyjblocks, CTLFLAG_RD, 1342 &stat_emptyjblocks, 0, ""); 1343 1344 SYSCTL_DECL(_vfs_ffs); 1345 1346 /* Whether to recompute the summary at mount time */ 1347 static int compute_summary_at_mount = 0; 1348 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1349 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1350 static int print_threads = 0; 1351 SYSCTL_INT(_debug_softdep, OID_AUTO, print_threads, CTLFLAG_RW, 1352 &print_threads, 0, "Notify flusher thread start/stop"); 1353 1354 /* List of all filesystems mounted with soft updates */ 1355 static TAILQ_HEAD(, mount_softdeps) softdepmounts; 1356 1357 /* 1358 * This function cleans the worklist for a filesystem. 1359 * Each filesystem running with soft dependencies gets its own 1360 * thread to run in this function. The thread is started up in 1361 * softdep_mount and shutdown in softdep_unmount. They show up 1362 * as part of the kernel "bufdaemon" process whose process 1363 * entry is available in bufdaemonproc. 1364 */ 1365 static int searchfailed; 1366 extern struct proc *bufdaemonproc; 1367 static void 1368 softdep_flush(addr) 1369 void *addr; 1370 { 1371 struct mount *mp; 1372 struct thread *td; 1373 struct ufsmount *ump; 1374 1375 td = curthread; 1376 td->td_pflags |= TDP_NORUNNINGBUF; 1377 mp = (struct mount *)addr; 1378 ump = VFSTOUFS(mp); 1379 atomic_add_int(&stat_flush_threads, 1); 1380 if (print_threads) { 1381 if (stat_flush_threads == 1) 1382 printf("Running %s at pid %d\n", bufdaemonproc->p_comm, 1383 bufdaemonproc->p_pid); 1384 printf("Start thread %s\n", td->td_name); 1385 } 1386 for (;;) { 1387 while (softdep_process_worklist(mp, 0) > 0 || 1388 (MOUNTEDSUJ(mp) && 1389 VFSTOUFS(mp)->softdep_jblocks->jb_suspended)) 1390 kthread_suspend_check(); 1391 ACQUIRE_LOCK(ump); 1392 if ((ump->softdep_flags & FLUSH_CLEANUP) == 0) 1393 msleep(&ump->softdep_flushtd, LOCK_PTR(ump), PVM, 1394 "sdflush", hz / 2); 1395 ump->softdep_flags &= ~FLUSH_CLEANUP; 1396 /* 1397 * Check to see if we are done and need to exit. 1398 */ 1399 if ((ump->softdep_flags & FLUSH_EXIT) == 0) { 1400 FREE_LOCK(ump); 1401 continue; 1402 } 1403 ump->softdep_flags &= ~FLUSH_EXIT; 1404 FREE_LOCK(ump); 1405 wakeup(&ump->softdep_flags); 1406 if (print_threads) 1407 printf("Stop thread %s: searchfailed %d, did cleanups %d\n", td->td_name, searchfailed, ump->um_softdep->sd_cleanups); 1408 atomic_subtract_int(&stat_flush_threads, 1); 1409 kthread_exit(); 1410 panic("kthread_exit failed\n"); 1411 } 1412 } 1413 1414 static void 1415 worklist_speedup(mp) 1416 struct mount *mp; 1417 { 1418 struct ufsmount *ump; 1419 1420 ump = VFSTOUFS(mp); 1421 LOCK_OWNED(ump); 1422 if ((ump->softdep_flags & (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) { 1423 ump->softdep_flags |= FLUSH_CLEANUP; 1424 if (ump->softdep_flushtd->td_wchan == &ump->softdep_flushtd) 1425 wakeup(&ump->softdep_flushtd); 1426 } 1427 } 1428 1429 static int 1430 softdep_speedup(ump) 1431 struct ufsmount *ump; 1432 { 1433 struct ufsmount *altump; 1434 struct mount_softdeps *sdp; 1435 1436 LOCK_OWNED(ump); 1437 worklist_speedup(ump->um_mountp); 1438 bd_speedup(); 1439 /* 1440 * If we have global shortages, then we need other 1441 * filesystems to help with the cleanup. Here we wakeup a 1442 * flusher thread for a filesystem that is over its fair 1443 * share of resources. 1444 */ 1445 if (req_clear_inodedeps || req_clear_remove) { 1446 ACQUIRE_GBLLOCK(&lk); 1447 TAILQ_FOREACH(sdp, &softdepmounts, sd_next) { 1448 if ((altump = sdp->sd_ump) == ump) 1449 continue; 1450 if (((req_clear_inodedeps && 1451 altump->softdep_curdeps[D_INODEDEP] > 1452 max_softdeps / stat_flush_threads) || 1453 (req_clear_remove && 1454 altump->softdep_curdeps[D_DIRREM] > 1455 (max_softdeps / 2) / stat_flush_threads)) && 1456 TRY_ACQUIRE_LOCK(altump)) 1457 break; 1458 } 1459 if (sdp == NULL) { 1460 searchfailed++; 1461 FREE_GBLLOCK(&lk); 1462 } else { 1463 /* 1464 * Move to the end of the list so we pick a 1465 * different one on out next try. 1466 */ 1467 TAILQ_REMOVE(&softdepmounts, sdp, sd_next); 1468 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 1469 FREE_GBLLOCK(&lk); 1470 if ((altump->softdep_flags & 1471 (FLUSH_CLEANUP | FLUSH_EXIT)) == 0) { 1472 altump->softdep_flags |= FLUSH_CLEANUP; 1473 altump->um_softdep->sd_cleanups++; 1474 if (altump->softdep_flushtd->td_wchan == 1475 &altump->softdep_flushtd) { 1476 wakeup(&altump->softdep_flushtd); 1477 } 1478 } 1479 FREE_LOCK(altump); 1480 } 1481 } 1482 return (speedup_syncer()); 1483 } 1484 1485 /* 1486 * Add an item to the end of the work queue. 1487 * This routine requires that the lock be held. 1488 * This is the only routine that adds items to the list. 1489 * The following routine is the only one that removes items 1490 * and does so in order from first to last. 1491 */ 1492 1493 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1494 #define WK_NODELAY 0x0002 /* Process immediately. */ 1495 1496 static void 1497 add_to_worklist(wk, flags) 1498 struct worklist *wk; 1499 int flags; 1500 { 1501 struct ufsmount *ump; 1502 1503 ump = VFSTOUFS(wk->wk_mp); 1504 LOCK_OWNED(ump); 1505 if (wk->wk_state & ONWORKLIST) 1506 panic("add_to_worklist: %s(0x%X) already on list", 1507 TYPENAME(wk->wk_type), wk->wk_state); 1508 wk->wk_state |= ONWORKLIST; 1509 if (ump->softdep_on_worklist == 0) { 1510 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1511 ump->softdep_worklist_tail = wk; 1512 } else if (flags & WK_HEAD) { 1513 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1514 } else { 1515 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1516 ump->softdep_worklist_tail = wk; 1517 } 1518 ump->softdep_on_worklist += 1; 1519 if (flags & WK_NODELAY) 1520 worklist_speedup(wk->wk_mp); 1521 } 1522 1523 /* 1524 * Remove the item to be processed. If we are removing the last 1525 * item on the list, we need to recalculate the tail pointer. 1526 */ 1527 static void 1528 remove_from_worklist(wk) 1529 struct worklist *wk; 1530 { 1531 struct ufsmount *ump; 1532 1533 ump = VFSTOUFS(wk->wk_mp); 1534 WORKLIST_REMOVE(wk); 1535 if (ump->softdep_worklist_tail == wk) 1536 ump->softdep_worklist_tail = 1537 (struct worklist *)wk->wk_list.le_prev; 1538 ump->softdep_on_worklist -= 1; 1539 } 1540 1541 static void 1542 wake_worklist(wk) 1543 struct worklist *wk; 1544 { 1545 if (wk->wk_state & IOWAITING) { 1546 wk->wk_state &= ~IOWAITING; 1547 wakeup(wk); 1548 } 1549 } 1550 1551 static void 1552 wait_worklist(wk, wmesg) 1553 struct worklist *wk; 1554 char *wmesg; 1555 { 1556 struct ufsmount *ump; 1557 1558 ump = VFSTOUFS(wk->wk_mp); 1559 wk->wk_state |= IOWAITING; 1560 msleep(wk, LOCK_PTR(ump), PVM, wmesg, 0); 1561 } 1562 1563 /* 1564 * Process that runs once per second to handle items in the background queue. 1565 * 1566 * Note that we ensure that everything is done in the order in which they 1567 * appear in the queue. The code below depends on this property to ensure 1568 * that blocks of a file are freed before the inode itself is freed. This 1569 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1570 * until all the old ones have been purged from the dependency lists. 1571 */ 1572 static int 1573 softdep_process_worklist(mp, full) 1574 struct mount *mp; 1575 int full; 1576 { 1577 int cnt, matchcnt; 1578 struct ufsmount *ump; 1579 long starttime; 1580 1581 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1582 if (MOUNTEDSOFTDEP(mp) == 0) 1583 return (0); 1584 matchcnt = 0; 1585 ump = VFSTOUFS(mp); 1586 ACQUIRE_LOCK(ump); 1587 starttime = time_second; 1588 softdep_process_journal(mp, NULL, full ? MNT_WAIT : 0); 1589 check_clear_deps(mp); 1590 while (ump->softdep_on_worklist > 0) { 1591 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1592 break; 1593 else 1594 matchcnt += cnt; 1595 check_clear_deps(mp); 1596 /* 1597 * We do not generally want to stop for buffer space, but if 1598 * we are really being a buffer hog, we will stop and wait. 1599 */ 1600 if (should_yield()) { 1601 FREE_LOCK(ump); 1602 kern_yield(PRI_USER); 1603 bwillwrite(); 1604 ACQUIRE_LOCK(ump); 1605 } 1606 /* 1607 * Never allow processing to run for more than one 1608 * second. This gives the syncer thread the opportunity 1609 * to pause if appropriate. 1610 */ 1611 if (!full && starttime != time_second) 1612 break; 1613 } 1614 if (full == 0) 1615 journal_unsuspend(ump); 1616 FREE_LOCK(ump); 1617 return (matchcnt); 1618 } 1619 1620 /* 1621 * Process all removes associated with a vnode if we are running out of 1622 * journal space. Any other process which attempts to flush these will 1623 * be unable as we have the vnodes locked. 1624 */ 1625 static void 1626 process_removes(vp) 1627 struct vnode *vp; 1628 { 1629 struct inodedep *inodedep; 1630 struct dirrem *dirrem; 1631 struct ufsmount *ump; 1632 struct mount *mp; 1633 ino_t inum; 1634 1635 mp = vp->v_mount; 1636 ump = VFSTOUFS(mp); 1637 LOCK_OWNED(ump); 1638 inum = VTOI(vp)->i_number; 1639 for (;;) { 1640 top: 1641 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1642 return; 1643 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1644 /* 1645 * If another thread is trying to lock this vnode 1646 * it will fail but we must wait for it to do so 1647 * before we can proceed. 1648 */ 1649 if (dirrem->dm_state & INPROGRESS) { 1650 wait_worklist(&dirrem->dm_list, "pwrwait"); 1651 goto top; 1652 } 1653 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1654 (COMPLETE | ONWORKLIST)) 1655 break; 1656 } 1657 if (dirrem == NULL) 1658 return; 1659 remove_from_worklist(&dirrem->dm_list); 1660 FREE_LOCK(ump); 1661 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1662 panic("process_removes: suspended filesystem"); 1663 handle_workitem_remove(dirrem, 0); 1664 vn_finished_secondary_write(mp); 1665 ACQUIRE_LOCK(ump); 1666 } 1667 } 1668 1669 /* 1670 * Process all truncations associated with a vnode if we are running out 1671 * of journal space. This is called when the vnode lock is already held 1672 * and no other process can clear the truncation. This function returns 1673 * a value greater than zero if it did any work. 1674 */ 1675 static void 1676 process_truncates(vp) 1677 struct vnode *vp; 1678 { 1679 struct inodedep *inodedep; 1680 struct freeblks *freeblks; 1681 struct ufsmount *ump; 1682 struct mount *mp; 1683 ino_t inum; 1684 int cgwait; 1685 1686 mp = vp->v_mount; 1687 ump = VFSTOUFS(mp); 1688 LOCK_OWNED(ump); 1689 inum = VTOI(vp)->i_number; 1690 for (;;) { 1691 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1692 return; 1693 cgwait = 0; 1694 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1695 /* Journal entries not yet written. */ 1696 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1697 jwait(&LIST_FIRST( 1698 &freeblks->fb_jblkdephd)->jb_list, 1699 MNT_WAIT); 1700 break; 1701 } 1702 /* Another thread is executing this item. */ 1703 if (freeblks->fb_state & INPROGRESS) { 1704 wait_worklist(&freeblks->fb_list, "ptrwait"); 1705 break; 1706 } 1707 /* Freeblks is waiting on a inode write. */ 1708 if ((freeblks->fb_state & COMPLETE) == 0) { 1709 FREE_LOCK(ump); 1710 ffs_update(vp, 1); 1711 ACQUIRE_LOCK(ump); 1712 break; 1713 } 1714 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1715 (ALLCOMPLETE | ONWORKLIST)) { 1716 remove_from_worklist(&freeblks->fb_list); 1717 freeblks->fb_state |= INPROGRESS; 1718 FREE_LOCK(ump); 1719 if (vn_start_secondary_write(NULL, &mp, 1720 V_NOWAIT)) 1721 panic("process_truncates: " 1722 "suspended filesystem"); 1723 handle_workitem_freeblocks(freeblks, 0); 1724 vn_finished_secondary_write(mp); 1725 ACQUIRE_LOCK(ump); 1726 break; 1727 } 1728 if (freeblks->fb_cgwait) 1729 cgwait++; 1730 } 1731 if (cgwait) { 1732 FREE_LOCK(ump); 1733 sync_cgs(mp, MNT_WAIT); 1734 ffs_sync_snap(mp, MNT_WAIT); 1735 ACQUIRE_LOCK(ump); 1736 continue; 1737 } 1738 if (freeblks == NULL) 1739 break; 1740 } 1741 return; 1742 } 1743 1744 /* 1745 * Process one item on the worklist. 1746 */ 1747 static int 1748 process_worklist_item(mp, target, flags) 1749 struct mount *mp; 1750 int target; 1751 int flags; 1752 { 1753 struct worklist sentinel; 1754 struct worklist *wk; 1755 struct ufsmount *ump; 1756 int matchcnt; 1757 int error; 1758 1759 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1760 /* 1761 * If we are being called because of a process doing a 1762 * copy-on-write, then it is not safe to write as we may 1763 * recurse into the copy-on-write routine. 1764 */ 1765 if (curthread->td_pflags & TDP_COWINPROGRESS) 1766 return (-1); 1767 PHOLD(curproc); /* Don't let the stack go away. */ 1768 ump = VFSTOUFS(mp); 1769 LOCK_OWNED(ump); 1770 matchcnt = 0; 1771 sentinel.wk_mp = NULL; 1772 sentinel.wk_type = D_SENTINEL; 1773 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1774 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1775 wk = LIST_NEXT(&sentinel, wk_list)) { 1776 if (wk->wk_type == D_SENTINEL) { 1777 LIST_REMOVE(&sentinel, wk_list); 1778 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1779 continue; 1780 } 1781 if (wk->wk_state & INPROGRESS) 1782 panic("process_worklist_item: %p already in progress.", 1783 wk); 1784 wk->wk_state |= INPROGRESS; 1785 remove_from_worklist(wk); 1786 FREE_LOCK(ump); 1787 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1788 panic("process_worklist_item: suspended filesystem"); 1789 switch (wk->wk_type) { 1790 case D_DIRREM: 1791 /* removal of a directory entry */ 1792 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1793 break; 1794 1795 case D_FREEBLKS: 1796 /* releasing blocks and/or fragments from a file */ 1797 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1798 flags); 1799 break; 1800 1801 case D_FREEFRAG: 1802 /* releasing a fragment when replaced as a file grows */ 1803 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1804 error = 0; 1805 break; 1806 1807 case D_FREEFILE: 1808 /* releasing an inode when its link count drops to 0 */ 1809 handle_workitem_freefile(WK_FREEFILE(wk)); 1810 error = 0; 1811 break; 1812 1813 default: 1814 panic("%s_process_worklist: Unknown type %s", 1815 "softdep", TYPENAME(wk->wk_type)); 1816 /* NOTREACHED */ 1817 } 1818 vn_finished_secondary_write(mp); 1819 ACQUIRE_LOCK(ump); 1820 if (error == 0) { 1821 if (++matchcnt == target) 1822 break; 1823 continue; 1824 } 1825 /* 1826 * We have to retry the worklist item later. Wake up any 1827 * waiters who may be able to complete it immediately and 1828 * add the item back to the head so we don't try to execute 1829 * it again. 1830 */ 1831 wk->wk_state &= ~INPROGRESS; 1832 wake_worklist(wk); 1833 add_to_worklist(wk, WK_HEAD); 1834 } 1835 LIST_REMOVE(&sentinel, wk_list); 1836 /* Sentinal could've become the tail from remove_from_worklist. */ 1837 if (ump->softdep_worklist_tail == &sentinel) 1838 ump->softdep_worklist_tail = 1839 (struct worklist *)sentinel.wk_list.le_prev; 1840 PRELE(curproc); 1841 return (matchcnt); 1842 } 1843 1844 /* 1845 * Move dependencies from one buffer to another. 1846 */ 1847 int 1848 softdep_move_dependencies(oldbp, newbp) 1849 struct buf *oldbp; 1850 struct buf *newbp; 1851 { 1852 struct worklist *wk, *wktail; 1853 struct ufsmount *ump; 1854 int dirty; 1855 1856 if ((wk = LIST_FIRST(&oldbp->b_dep)) == NULL) 1857 return (0); 1858 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 1859 ("softdep_move_dependencies called on non-softdep filesystem")); 1860 dirty = 0; 1861 wktail = NULL; 1862 ump = VFSTOUFS(wk->wk_mp); 1863 ACQUIRE_LOCK(ump); 1864 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1865 LIST_REMOVE(wk, wk_list); 1866 if (wk->wk_type == D_BMSAFEMAP && 1867 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1868 dirty = 1; 1869 if (wktail == 0) 1870 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1871 else 1872 LIST_INSERT_AFTER(wktail, wk, wk_list); 1873 wktail = wk; 1874 } 1875 FREE_LOCK(ump); 1876 1877 return (dirty); 1878 } 1879 1880 /* 1881 * Purge the work list of all items associated with a particular mount point. 1882 */ 1883 int 1884 softdep_flushworklist(oldmnt, countp, td) 1885 struct mount *oldmnt; 1886 int *countp; 1887 struct thread *td; 1888 { 1889 struct vnode *devvp; 1890 int count, error = 0; 1891 struct ufsmount *ump; 1892 1893 /* 1894 * Alternately flush the block device associated with the mount 1895 * point and process any dependencies that the flushing 1896 * creates. We continue until no more worklist dependencies 1897 * are found. 1898 */ 1899 *countp = 0; 1900 ump = VFSTOUFS(oldmnt); 1901 devvp = ump->um_devvp; 1902 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1903 *countp += count; 1904 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1905 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1906 VOP_UNLOCK(devvp, 0); 1907 if (error) 1908 break; 1909 } 1910 return (error); 1911 } 1912 1913 static int 1914 softdep_waitidle(struct mount *mp, int flags __unused) 1915 { 1916 struct ufsmount *ump; 1917 int error; 1918 int i; 1919 1920 ump = VFSTOUFS(mp); 1921 ACQUIRE_LOCK(ump); 1922 for (i = 0; i < 10 && ump->softdep_deps; i++) { 1923 ump->softdep_req = 1; 1924 KASSERT((flags & FORCECLOSE) == 0 || 1925 ump->softdep_on_worklist == 0, 1926 ("softdep_waitidle: work added after flush")); 1927 msleep(&ump->softdep_deps, LOCK_PTR(ump), PVM, "softdeps", 1); 1928 } 1929 ump->softdep_req = 0; 1930 FREE_LOCK(ump); 1931 error = 0; 1932 if (i == 10) { 1933 error = EBUSY; 1934 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1935 mp); 1936 } 1937 1938 return (error); 1939 } 1940 1941 /* 1942 * Flush all vnodes and worklist items associated with a specified mount point. 1943 */ 1944 int 1945 softdep_flushfiles(oldmnt, flags, td) 1946 struct mount *oldmnt; 1947 int flags; 1948 struct thread *td; 1949 { 1950 #ifdef QUOTA 1951 struct ufsmount *ump; 1952 int i; 1953 #endif 1954 int error, early, depcount, loopcnt, retry_flush_count, retry; 1955 int morework; 1956 1957 KASSERT(MOUNTEDSOFTDEP(oldmnt) != 0, 1958 ("softdep_flushfiles called on non-softdep filesystem")); 1959 loopcnt = 10; 1960 retry_flush_count = 3; 1961 retry_flush: 1962 error = 0; 1963 1964 /* 1965 * Alternately flush the vnodes associated with the mount 1966 * point and process any dependencies that the flushing 1967 * creates. In theory, this loop can happen at most twice, 1968 * but we give it a few extra just to be sure. 1969 */ 1970 for (; loopcnt > 0; loopcnt--) { 1971 /* 1972 * Do another flush in case any vnodes were brought in 1973 * as part of the cleanup operations. 1974 */ 1975 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1976 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1977 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1978 break; 1979 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1980 depcount == 0) 1981 break; 1982 } 1983 /* 1984 * If we are unmounting then it is an error to fail. If we 1985 * are simply trying to downgrade to read-only, then filesystem 1986 * activity can keep us busy forever, so we just fail with EBUSY. 1987 */ 1988 if (loopcnt == 0) { 1989 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 1990 panic("softdep_flushfiles: looping"); 1991 error = EBUSY; 1992 } 1993 if (!error) 1994 error = softdep_waitidle(oldmnt, flags); 1995 if (!error) { 1996 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 1997 retry = 0; 1998 MNT_ILOCK(oldmnt); 1999 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 2000 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 2001 morework = oldmnt->mnt_nvnodelistsize > 0; 2002 #ifdef QUOTA 2003 ump = VFSTOUFS(oldmnt); 2004 UFS_LOCK(ump); 2005 for (i = 0; i < MAXQUOTAS; i++) { 2006 if (ump->um_quotas[i] != NULLVP) 2007 morework = 1; 2008 } 2009 UFS_UNLOCK(ump); 2010 #endif 2011 if (morework) { 2012 if (--retry_flush_count > 0) { 2013 retry = 1; 2014 loopcnt = 3; 2015 } else 2016 error = EBUSY; 2017 } 2018 MNT_IUNLOCK(oldmnt); 2019 if (retry) 2020 goto retry_flush; 2021 } 2022 } 2023 return (error); 2024 } 2025 2026 /* 2027 * Structure hashing. 2028 * 2029 * There are four types of structures that can be looked up: 2030 * 1) pagedep structures identified by mount point, inode number, 2031 * and logical block. 2032 * 2) inodedep structures identified by mount point and inode number. 2033 * 3) newblk structures identified by mount point and 2034 * physical block number. 2035 * 4) bmsafemap structures identified by mount point and 2036 * cylinder group number. 2037 * 2038 * The "pagedep" and "inodedep" dependency structures are hashed 2039 * separately from the file blocks and inodes to which they correspond. 2040 * This separation helps when the in-memory copy of an inode or 2041 * file block must be replaced. It also obviates the need to access 2042 * an inode or file page when simply updating (or de-allocating) 2043 * dependency structures. Lookup of newblk structures is needed to 2044 * find newly allocated blocks when trying to associate them with 2045 * their allocdirect or allocindir structure. 2046 * 2047 * The lookup routines optionally create and hash a new instance when 2048 * an existing entry is not found. The bmsafemap lookup routine always 2049 * allocates a new structure if an existing one is not found. 2050 */ 2051 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2052 #define NODELAY 0x0002 /* cannot do background work */ 2053 2054 /* 2055 * Structures and routines associated with pagedep caching. 2056 */ 2057 #define PAGEDEP_HASH(ump, inum, lbn) \ 2058 (&(ump)->pagedep_hashtbl[((inum) + (lbn)) & (ump)->pagedep_hash_size]) 2059 2060 static int 2061 pagedep_find(pagedephd, ino, lbn, pagedeppp) 2062 struct pagedep_hashhead *pagedephd; 2063 ino_t ino; 2064 ufs_lbn_t lbn; 2065 struct pagedep **pagedeppp; 2066 { 2067 struct pagedep *pagedep; 2068 2069 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2070 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn) { 2071 *pagedeppp = pagedep; 2072 return (1); 2073 } 2074 } 2075 *pagedeppp = NULL; 2076 return (0); 2077 } 2078 /* 2079 * Look up a pagedep. Return 1 if found, 0 otherwise. 2080 * If not found, allocate if DEPALLOC flag is passed. 2081 * Found or allocated entry is returned in pagedeppp. 2082 * This routine must be called with splbio interrupts blocked. 2083 */ 2084 static int 2085 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2086 struct mount *mp; 2087 struct buf *bp; 2088 ino_t ino; 2089 ufs_lbn_t lbn; 2090 int flags; 2091 struct pagedep **pagedeppp; 2092 { 2093 struct pagedep *pagedep; 2094 struct pagedep_hashhead *pagedephd; 2095 struct worklist *wk; 2096 struct ufsmount *ump; 2097 int ret; 2098 int i; 2099 2100 ump = VFSTOUFS(mp); 2101 LOCK_OWNED(ump); 2102 if (bp) { 2103 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2104 if (wk->wk_type == D_PAGEDEP) { 2105 *pagedeppp = WK_PAGEDEP(wk); 2106 return (1); 2107 } 2108 } 2109 } 2110 pagedephd = PAGEDEP_HASH(ump, ino, lbn); 2111 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2112 if (ret) { 2113 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2114 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2115 return (1); 2116 } 2117 if ((flags & DEPALLOC) == 0) 2118 return (0); 2119 FREE_LOCK(ump); 2120 pagedep = malloc(sizeof(struct pagedep), 2121 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2122 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2123 ACQUIRE_LOCK(ump); 2124 ret = pagedep_find(pagedephd, ino, lbn, pagedeppp); 2125 if (*pagedeppp) { 2126 /* 2127 * This should never happen since we only create pagedeps 2128 * with the vnode lock held. Could be an assert. 2129 */ 2130 WORKITEM_FREE(pagedep, D_PAGEDEP); 2131 return (ret); 2132 } 2133 pagedep->pd_ino = ino; 2134 pagedep->pd_lbn = lbn; 2135 LIST_INIT(&pagedep->pd_dirremhd); 2136 LIST_INIT(&pagedep->pd_pendinghd); 2137 for (i = 0; i < DAHASHSZ; i++) 2138 LIST_INIT(&pagedep->pd_diraddhd[i]); 2139 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2140 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2141 *pagedeppp = pagedep; 2142 return (0); 2143 } 2144 2145 /* 2146 * Structures and routines associated with inodedep caching. 2147 */ 2148 #define INODEDEP_HASH(ump, inum) \ 2149 (&(ump)->inodedep_hashtbl[(inum) & (ump)->inodedep_hash_size]) 2150 2151 static int 2152 inodedep_find(inodedephd, inum, inodedeppp) 2153 struct inodedep_hashhead *inodedephd; 2154 ino_t inum; 2155 struct inodedep **inodedeppp; 2156 { 2157 struct inodedep *inodedep; 2158 2159 LIST_FOREACH(inodedep, inodedephd, id_hash) 2160 if (inum == inodedep->id_ino) 2161 break; 2162 if (inodedep) { 2163 *inodedeppp = inodedep; 2164 return (1); 2165 } 2166 *inodedeppp = NULL; 2167 2168 return (0); 2169 } 2170 /* 2171 * Look up an inodedep. Return 1 if found, 0 if not found. 2172 * If not found, allocate if DEPALLOC flag is passed. 2173 * Found or allocated entry is returned in inodedeppp. 2174 * This routine must be called with splbio interrupts blocked. 2175 */ 2176 static int 2177 inodedep_lookup(mp, inum, flags, inodedeppp) 2178 struct mount *mp; 2179 ino_t inum; 2180 int flags; 2181 struct inodedep **inodedeppp; 2182 { 2183 struct inodedep *inodedep; 2184 struct inodedep_hashhead *inodedephd; 2185 struct ufsmount *ump; 2186 struct fs *fs; 2187 2188 ump = VFSTOUFS(mp); 2189 LOCK_OWNED(ump); 2190 fs = ump->um_fs; 2191 inodedephd = INODEDEP_HASH(ump, inum); 2192 2193 if (inodedep_find(inodedephd, inum, inodedeppp)) 2194 return (1); 2195 if ((flags & DEPALLOC) == 0) 2196 return (0); 2197 /* 2198 * If the system is over its limit and our filesystem is 2199 * responsible for more than our share of that usage and 2200 * we are not in a rush, request some inodedep cleanup. 2201 */ 2202 while (dep_current[D_INODEDEP] > max_softdeps && 2203 (flags & NODELAY) == 0 && 2204 ump->softdep_curdeps[D_INODEDEP] > 2205 max_softdeps / stat_flush_threads) 2206 request_cleanup(mp, FLUSH_INODES); 2207 FREE_LOCK(ump); 2208 inodedep = malloc(sizeof(struct inodedep), 2209 M_INODEDEP, M_SOFTDEP_FLAGS); 2210 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2211 ACQUIRE_LOCK(ump); 2212 if (inodedep_find(inodedephd, inum, inodedeppp)) { 2213 WORKITEM_FREE(inodedep, D_INODEDEP); 2214 return (1); 2215 } 2216 inodedep->id_fs = fs; 2217 inodedep->id_ino = inum; 2218 inodedep->id_state = ALLCOMPLETE; 2219 inodedep->id_nlinkdelta = 0; 2220 inodedep->id_savedino1 = NULL; 2221 inodedep->id_savedsize = -1; 2222 inodedep->id_savedextsize = -1; 2223 inodedep->id_savednlink = -1; 2224 inodedep->id_bmsafemap = NULL; 2225 inodedep->id_mkdiradd = NULL; 2226 LIST_INIT(&inodedep->id_dirremhd); 2227 LIST_INIT(&inodedep->id_pendinghd); 2228 LIST_INIT(&inodedep->id_inowait); 2229 LIST_INIT(&inodedep->id_bufwait); 2230 TAILQ_INIT(&inodedep->id_inoreflst); 2231 TAILQ_INIT(&inodedep->id_inoupdt); 2232 TAILQ_INIT(&inodedep->id_newinoupdt); 2233 TAILQ_INIT(&inodedep->id_extupdt); 2234 TAILQ_INIT(&inodedep->id_newextupdt); 2235 TAILQ_INIT(&inodedep->id_freeblklst); 2236 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2237 *inodedeppp = inodedep; 2238 return (0); 2239 } 2240 2241 /* 2242 * Structures and routines associated with newblk caching. 2243 */ 2244 #define NEWBLK_HASH(ump, inum) \ 2245 (&(ump)->newblk_hashtbl[(inum) & (ump)->newblk_hash_size]) 2246 2247 static int 2248 newblk_find(newblkhd, newblkno, flags, newblkpp) 2249 struct newblk_hashhead *newblkhd; 2250 ufs2_daddr_t newblkno; 2251 int flags; 2252 struct newblk **newblkpp; 2253 { 2254 struct newblk *newblk; 2255 2256 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2257 if (newblkno != newblk->nb_newblkno) 2258 continue; 2259 /* 2260 * If we're creating a new dependency don't match those that 2261 * have already been converted to allocdirects. This is for 2262 * a frag extend. 2263 */ 2264 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2265 continue; 2266 break; 2267 } 2268 if (newblk) { 2269 *newblkpp = newblk; 2270 return (1); 2271 } 2272 *newblkpp = NULL; 2273 return (0); 2274 } 2275 2276 /* 2277 * Look up a newblk. Return 1 if found, 0 if not found. 2278 * If not found, allocate if DEPALLOC flag is passed. 2279 * Found or allocated entry is returned in newblkpp. 2280 */ 2281 static int 2282 newblk_lookup(mp, newblkno, flags, newblkpp) 2283 struct mount *mp; 2284 ufs2_daddr_t newblkno; 2285 int flags; 2286 struct newblk **newblkpp; 2287 { 2288 struct newblk *newblk; 2289 struct newblk_hashhead *newblkhd; 2290 struct ufsmount *ump; 2291 2292 ump = VFSTOUFS(mp); 2293 LOCK_OWNED(ump); 2294 newblkhd = NEWBLK_HASH(ump, newblkno); 2295 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) 2296 return (1); 2297 if ((flags & DEPALLOC) == 0) 2298 return (0); 2299 FREE_LOCK(ump); 2300 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2301 M_SOFTDEP_FLAGS | M_ZERO); 2302 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2303 ACQUIRE_LOCK(ump); 2304 if (newblk_find(newblkhd, newblkno, flags, newblkpp)) { 2305 WORKITEM_FREE(newblk, D_NEWBLK); 2306 return (1); 2307 } 2308 newblk->nb_freefrag = NULL; 2309 LIST_INIT(&newblk->nb_indirdeps); 2310 LIST_INIT(&newblk->nb_newdirblk); 2311 LIST_INIT(&newblk->nb_jwork); 2312 newblk->nb_state = ATTACHED; 2313 newblk->nb_newblkno = newblkno; 2314 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2315 *newblkpp = newblk; 2316 return (0); 2317 } 2318 2319 /* 2320 * Structures and routines associated with freed indirect block caching. 2321 */ 2322 #define INDIR_HASH(ump, blkno) \ 2323 (&(ump)->indir_hashtbl[(blkno) & (ump)->indir_hash_size]) 2324 2325 /* 2326 * Lookup an indirect block in the indir hash table. The freework is 2327 * removed and potentially freed. The caller must do a blocking journal 2328 * write before writing to the blkno. 2329 */ 2330 static int 2331 indirblk_lookup(mp, blkno) 2332 struct mount *mp; 2333 ufs2_daddr_t blkno; 2334 { 2335 struct freework *freework; 2336 struct indir_hashhead *wkhd; 2337 struct ufsmount *ump; 2338 2339 ump = VFSTOUFS(mp); 2340 wkhd = INDIR_HASH(ump, blkno); 2341 TAILQ_FOREACH(freework, wkhd, fw_next) { 2342 if (freework->fw_blkno != blkno) 2343 continue; 2344 indirblk_remove(freework); 2345 return (1); 2346 } 2347 return (0); 2348 } 2349 2350 /* 2351 * Insert an indirect block represented by freework into the indirblk 2352 * hash table so that it may prevent the block from being re-used prior 2353 * to the journal being written. 2354 */ 2355 static void 2356 indirblk_insert(freework) 2357 struct freework *freework; 2358 { 2359 struct jblocks *jblocks; 2360 struct jseg *jseg; 2361 struct ufsmount *ump; 2362 2363 ump = VFSTOUFS(freework->fw_list.wk_mp); 2364 jblocks = ump->softdep_jblocks; 2365 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2366 if (jseg == NULL) 2367 return; 2368 2369 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2370 TAILQ_INSERT_HEAD(INDIR_HASH(ump, freework->fw_blkno), freework, 2371 fw_next); 2372 freework->fw_state &= ~DEPCOMPLETE; 2373 } 2374 2375 static void 2376 indirblk_remove(freework) 2377 struct freework *freework; 2378 { 2379 struct ufsmount *ump; 2380 2381 ump = VFSTOUFS(freework->fw_list.wk_mp); 2382 LIST_REMOVE(freework, fw_segs); 2383 TAILQ_REMOVE(INDIR_HASH(ump, freework->fw_blkno), freework, fw_next); 2384 freework->fw_state |= DEPCOMPLETE; 2385 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2386 WORKITEM_FREE(freework, D_FREEWORK); 2387 } 2388 2389 /* 2390 * Executed during filesystem system initialization before 2391 * mounting any filesystems. 2392 */ 2393 void 2394 softdep_initialize() 2395 { 2396 2397 TAILQ_INIT(&softdepmounts); 2398 max_softdeps = desiredvnodes * 4; 2399 2400 /* initialise bioops hack */ 2401 bioops.io_start = softdep_disk_io_initiation; 2402 bioops.io_complete = softdep_disk_write_complete; 2403 bioops.io_deallocate = softdep_deallocate_dependencies; 2404 bioops.io_countdeps = softdep_count_dependencies; 2405 2406 /* Initialize the callout with an mtx. */ 2407 callout_init_mtx(&softdep_callout, &lk, 0); 2408 } 2409 2410 /* 2411 * Executed after all filesystems have been unmounted during 2412 * filesystem module unload. 2413 */ 2414 void 2415 softdep_uninitialize() 2416 { 2417 2418 /* clear bioops hack */ 2419 bioops.io_start = NULL; 2420 bioops.io_complete = NULL; 2421 bioops.io_deallocate = NULL; 2422 bioops.io_countdeps = NULL; 2423 2424 callout_drain(&softdep_callout); 2425 } 2426 2427 /* 2428 * Called at mount time to notify the dependency code that a 2429 * filesystem wishes to use it. 2430 */ 2431 int 2432 softdep_mount(devvp, mp, fs, cred) 2433 struct vnode *devvp; 2434 struct mount *mp; 2435 struct fs *fs; 2436 struct ucred *cred; 2437 { 2438 struct csum_total cstotal; 2439 struct mount_softdeps *sdp; 2440 struct ufsmount *ump; 2441 struct cg *cgp; 2442 struct buf *bp; 2443 int i, error, cyl; 2444 2445 sdp = malloc(sizeof(struct mount_softdeps), M_MOUNTDATA, 2446 M_WAITOK | M_ZERO); 2447 MNT_ILOCK(mp); 2448 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2449 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2450 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2451 MNTK_SOFTDEP | MNTK_NOASYNC; 2452 } 2453 ump = VFSTOUFS(mp); 2454 ump->um_softdep = sdp; 2455 MNT_IUNLOCK(mp); 2456 rw_init(LOCK_PTR(ump), "Per-Filesystem Softdep Lock"); 2457 sdp->sd_ump = ump; 2458 LIST_INIT(&ump->softdep_workitem_pending); 2459 LIST_INIT(&ump->softdep_journal_pending); 2460 TAILQ_INIT(&ump->softdep_unlinked); 2461 LIST_INIT(&ump->softdep_dirtycg); 2462 ump->softdep_worklist_tail = NULL; 2463 ump->softdep_on_worklist = 0; 2464 ump->softdep_deps = 0; 2465 LIST_INIT(&ump->softdep_mkdirlisthd); 2466 ump->pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, 2467 &ump->pagedep_hash_size); 2468 ump->pagedep_nextclean = 0; 2469 ump->inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, 2470 &ump->inodedep_hash_size); 2471 ump->inodedep_nextclean = 0; 2472 ump->newblk_hashtbl = hashinit(max_softdeps / 2, M_NEWBLK, 2473 &ump->newblk_hash_size); 2474 ump->bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, 2475 &ump->bmsafemap_hash_size); 2476 i = 1 << (ffs(desiredvnodes / 10) - 1); 2477 ump->indir_hashtbl = malloc(i * sizeof(struct indir_hashhead), 2478 M_FREEWORK, M_WAITOK); 2479 ump->indir_hash_size = i - 1; 2480 for (i = 0; i <= ump->indir_hash_size; i++) 2481 TAILQ_INIT(&ump->indir_hashtbl[i]); 2482 ACQUIRE_GBLLOCK(&lk); 2483 TAILQ_INSERT_TAIL(&softdepmounts, sdp, sd_next); 2484 FREE_GBLLOCK(&lk); 2485 if ((fs->fs_flags & FS_SUJ) && 2486 (error = journal_mount(mp, fs, cred)) != 0) { 2487 printf("Failed to start journal: %d\n", error); 2488 softdep_unmount(mp); 2489 return (error); 2490 } 2491 /* 2492 * Start our flushing thread in the bufdaemon process. 2493 */ 2494 kproc_kthread_add(&softdep_flush, mp, &bufdaemonproc, 2495 &ump->softdep_flushtd, 0, 0, "softdepflush", "%s worker", 2496 mp->mnt_stat.f_mntonname); 2497 /* 2498 * When doing soft updates, the counters in the 2499 * superblock may have gotten out of sync. Recomputation 2500 * can take a long time and can be deferred for background 2501 * fsck. However, the old behavior of scanning the cylinder 2502 * groups and recalculating them at mount time is available 2503 * by setting vfs.ffs.compute_summary_at_mount to one. 2504 */ 2505 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2506 return (0); 2507 bzero(&cstotal, sizeof cstotal); 2508 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2509 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2510 fs->fs_cgsize, cred, &bp)) != 0) { 2511 brelse(bp); 2512 softdep_unmount(mp); 2513 return (error); 2514 } 2515 cgp = (struct cg *)bp->b_data; 2516 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2517 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2518 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2519 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2520 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2521 brelse(bp); 2522 } 2523 #ifdef DEBUG 2524 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2525 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2526 #endif 2527 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2528 return (0); 2529 } 2530 2531 void 2532 softdep_unmount(mp) 2533 struct mount *mp; 2534 { 2535 struct ufsmount *ump; 2536 #ifdef INVARIANTS 2537 int i; 2538 #endif 2539 2540 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 2541 ("softdep_unmount called on non-softdep filesystem")); 2542 ump = VFSTOUFS(mp); 2543 MNT_ILOCK(mp); 2544 mp->mnt_flag &= ~MNT_SOFTDEP; 2545 if (MOUNTEDSUJ(mp) == 0) { 2546 MNT_IUNLOCK(mp); 2547 } else { 2548 mp->mnt_flag &= ~MNT_SUJ; 2549 MNT_IUNLOCK(mp); 2550 journal_unmount(ump); 2551 } 2552 /* 2553 * Shut down our flushing thread. Check for NULL is if 2554 * softdep_mount errors out before the thread has been created. 2555 */ 2556 if (ump->softdep_flushtd != NULL) { 2557 ACQUIRE_LOCK(ump); 2558 ump->softdep_flags |= FLUSH_EXIT; 2559 wakeup(&ump->softdep_flushtd); 2560 msleep(&ump->softdep_flags, LOCK_PTR(ump), PVM | PDROP, 2561 "sdwait", 0); 2562 KASSERT((ump->softdep_flags & FLUSH_EXIT) == 0, 2563 ("Thread shutdown failed")); 2564 } 2565 /* 2566 * Free up our resources. 2567 */ 2568 ACQUIRE_GBLLOCK(&lk); 2569 TAILQ_REMOVE(&softdepmounts, ump->um_softdep, sd_next); 2570 FREE_GBLLOCK(&lk); 2571 rw_destroy(LOCK_PTR(ump)); 2572 hashdestroy(ump->pagedep_hashtbl, M_PAGEDEP, ump->pagedep_hash_size); 2573 hashdestroy(ump->inodedep_hashtbl, M_INODEDEP, ump->inodedep_hash_size); 2574 hashdestroy(ump->newblk_hashtbl, M_NEWBLK, ump->newblk_hash_size); 2575 hashdestroy(ump->bmsafemap_hashtbl, M_BMSAFEMAP, 2576 ump->bmsafemap_hash_size); 2577 free(ump->indir_hashtbl, M_FREEWORK); 2578 #ifdef INVARIANTS 2579 for (i = 0; i <= D_LAST; i++) 2580 KASSERT(ump->softdep_curdeps[i] == 0, 2581 ("Unmount %s: Dep type %s != 0 (%ld)", ump->um_fs->fs_fsmnt, 2582 TYPENAME(i), ump->softdep_curdeps[i])); 2583 #endif 2584 free(ump->um_softdep, M_MOUNTDATA); 2585 } 2586 2587 static struct jblocks * 2588 jblocks_create(void) 2589 { 2590 struct jblocks *jblocks; 2591 2592 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2593 TAILQ_INIT(&jblocks->jb_segs); 2594 jblocks->jb_avail = 10; 2595 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2596 M_JBLOCKS, M_WAITOK | M_ZERO); 2597 2598 return (jblocks); 2599 } 2600 2601 static ufs2_daddr_t 2602 jblocks_alloc(jblocks, bytes, actual) 2603 struct jblocks *jblocks; 2604 int bytes; 2605 int *actual; 2606 { 2607 ufs2_daddr_t daddr; 2608 struct jextent *jext; 2609 int freecnt; 2610 int blocks; 2611 2612 blocks = bytes / DEV_BSIZE; 2613 jext = &jblocks->jb_extent[jblocks->jb_head]; 2614 freecnt = jext->je_blocks - jblocks->jb_off; 2615 if (freecnt == 0) { 2616 jblocks->jb_off = 0; 2617 if (++jblocks->jb_head > jblocks->jb_used) 2618 jblocks->jb_head = 0; 2619 jext = &jblocks->jb_extent[jblocks->jb_head]; 2620 freecnt = jext->je_blocks; 2621 } 2622 if (freecnt > blocks) 2623 freecnt = blocks; 2624 *actual = freecnt * DEV_BSIZE; 2625 daddr = jext->je_daddr + jblocks->jb_off; 2626 jblocks->jb_off += freecnt; 2627 jblocks->jb_free -= freecnt; 2628 2629 return (daddr); 2630 } 2631 2632 static void 2633 jblocks_free(jblocks, mp, bytes) 2634 struct jblocks *jblocks; 2635 struct mount *mp; 2636 int bytes; 2637 { 2638 2639 LOCK_OWNED(VFSTOUFS(mp)); 2640 jblocks->jb_free += bytes / DEV_BSIZE; 2641 if (jblocks->jb_suspended) 2642 worklist_speedup(mp); 2643 wakeup(jblocks); 2644 } 2645 2646 static void 2647 jblocks_destroy(jblocks) 2648 struct jblocks *jblocks; 2649 { 2650 2651 if (jblocks->jb_extent) 2652 free(jblocks->jb_extent, M_JBLOCKS); 2653 free(jblocks, M_JBLOCKS); 2654 } 2655 2656 static void 2657 jblocks_add(jblocks, daddr, blocks) 2658 struct jblocks *jblocks; 2659 ufs2_daddr_t daddr; 2660 int blocks; 2661 { 2662 struct jextent *jext; 2663 2664 jblocks->jb_blocks += blocks; 2665 jblocks->jb_free += blocks; 2666 jext = &jblocks->jb_extent[jblocks->jb_used]; 2667 /* Adding the first block. */ 2668 if (jext->je_daddr == 0) { 2669 jext->je_daddr = daddr; 2670 jext->je_blocks = blocks; 2671 return; 2672 } 2673 /* Extending the last extent. */ 2674 if (jext->je_daddr + jext->je_blocks == daddr) { 2675 jext->je_blocks += blocks; 2676 return; 2677 } 2678 /* Adding a new extent. */ 2679 if (++jblocks->jb_used == jblocks->jb_avail) { 2680 jblocks->jb_avail *= 2; 2681 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2682 M_JBLOCKS, M_WAITOK | M_ZERO); 2683 memcpy(jext, jblocks->jb_extent, 2684 sizeof(struct jextent) * jblocks->jb_used); 2685 free(jblocks->jb_extent, M_JBLOCKS); 2686 jblocks->jb_extent = jext; 2687 } 2688 jext = &jblocks->jb_extent[jblocks->jb_used]; 2689 jext->je_daddr = daddr; 2690 jext->je_blocks = blocks; 2691 return; 2692 } 2693 2694 int 2695 softdep_journal_lookup(mp, vpp) 2696 struct mount *mp; 2697 struct vnode **vpp; 2698 { 2699 struct componentname cnp; 2700 struct vnode *dvp; 2701 ino_t sujournal; 2702 int error; 2703 2704 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp); 2705 if (error) 2706 return (error); 2707 bzero(&cnp, sizeof(cnp)); 2708 cnp.cn_nameiop = LOOKUP; 2709 cnp.cn_flags = ISLASTCN; 2710 cnp.cn_thread = curthread; 2711 cnp.cn_cred = curthread->td_ucred; 2712 cnp.cn_pnbuf = SUJ_FILE; 2713 cnp.cn_nameptr = SUJ_FILE; 2714 cnp.cn_namelen = strlen(SUJ_FILE); 2715 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2716 vput(dvp); 2717 if (error != 0) 2718 return (error); 2719 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2720 return (error); 2721 } 2722 2723 /* 2724 * Open and verify the journal file. 2725 */ 2726 static int 2727 journal_mount(mp, fs, cred) 2728 struct mount *mp; 2729 struct fs *fs; 2730 struct ucred *cred; 2731 { 2732 struct jblocks *jblocks; 2733 struct ufsmount *ump; 2734 struct vnode *vp; 2735 struct inode *ip; 2736 ufs2_daddr_t blkno; 2737 int bcount; 2738 int error; 2739 int i; 2740 2741 ump = VFSTOUFS(mp); 2742 ump->softdep_journal_tail = NULL; 2743 ump->softdep_on_journal = 0; 2744 ump->softdep_accdeps = 0; 2745 ump->softdep_req = 0; 2746 ump->softdep_jblocks = NULL; 2747 error = softdep_journal_lookup(mp, &vp); 2748 if (error != 0) { 2749 printf("Failed to find journal. Use tunefs to create one\n"); 2750 return (error); 2751 } 2752 ip = VTOI(vp); 2753 if (ip->i_size < SUJ_MIN) { 2754 error = ENOSPC; 2755 goto out; 2756 } 2757 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2758 jblocks = jblocks_create(); 2759 for (i = 0; i < bcount; i++) { 2760 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2761 if (error) 2762 break; 2763 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2764 } 2765 if (error) { 2766 jblocks_destroy(jblocks); 2767 goto out; 2768 } 2769 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2770 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2771 ump->softdep_jblocks = jblocks; 2772 out: 2773 if (error == 0) { 2774 MNT_ILOCK(mp); 2775 mp->mnt_flag |= MNT_SUJ; 2776 mp->mnt_flag &= ~MNT_SOFTDEP; 2777 MNT_IUNLOCK(mp); 2778 /* 2779 * Only validate the journal contents if the 2780 * filesystem is clean, otherwise we write the logs 2781 * but they'll never be used. If the filesystem was 2782 * still dirty when we mounted it the journal is 2783 * invalid and a new journal can only be valid if it 2784 * starts from a clean mount. 2785 */ 2786 if (fs->fs_clean) { 2787 DIP_SET(ip, i_modrev, fs->fs_mtime); 2788 ip->i_flags |= IN_MODIFIED; 2789 ffs_update(vp, 1); 2790 } 2791 } 2792 vput(vp); 2793 return (error); 2794 } 2795 2796 static void 2797 journal_unmount(ump) 2798 struct ufsmount *ump; 2799 { 2800 2801 if (ump->softdep_jblocks) 2802 jblocks_destroy(ump->softdep_jblocks); 2803 ump->softdep_jblocks = NULL; 2804 } 2805 2806 /* 2807 * Called when a journal record is ready to be written. Space is allocated 2808 * and the journal entry is created when the journal is flushed to stable 2809 * store. 2810 */ 2811 static void 2812 add_to_journal(wk) 2813 struct worklist *wk; 2814 { 2815 struct ufsmount *ump; 2816 2817 ump = VFSTOUFS(wk->wk_mp); 2818 LOCK_OWNED(ump); 2819 if (wk->wk_state & ONWORKLIST) 2820 panic("add_to_journal: %s(0x%X) already on list", 2821 TYPENAME(wk->wk_type), wk->wk_state); 2822 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2823 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2824 ump->softdep_jblocks->jb_age = ticks; 2825 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2826 } else 2827 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2828 ump->softdep_journal_tail = wk; 2829 ump->softdep_on_journal += 1; 2830 } 2831 2832 /* 2833 * Remove an arbitrary item for the journal worklist maintain the tail 2834 * pointer. This happens when a new operation obviates the need to 2835 * journal an old operation. 2836 */ 2837 static void 2838 remove_from_journal(wk) 2839 struct worklist *wk; 2840 { 2841 struct ufsmount *ump; 2842 2843 ump = VFSTOUFS(wk->wk_mp); 2844 LOCK_OWNED(ump); 2845 #ifdef SUJ_DEBUG 2846 { 2847 struct worklist *wkn; 2848 2849 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2850 if (wkn == wk) 2851 break; 2852 if (wkn == NULL) 2853 panic("remove_from_journal: %p is not in journal", wk); 2854 } 2855 #endif 2856 /* 2857 * We emulate a TAILQ to save space in most structures which do not 2858 * require TAILQ semantics. Here we must update the tail position 2859 * when removing the tail which is not the final entry. This works 2860 * only if the worklist linkage are at the beginning of the structure. 2861 */ 2862 if (ump->softdep_journal_tail == wk) 2863 ump->softdep_journal_tail = 2864 (struct worklist *)wk->wk_list.le_prev; 2865 2866 WORKLIST_REMOVE(wk); 2867 ump->softdep_on_journal -= 1; 2868 } 2869 2870 /* 2871 * Check for journal space as well as dependency limits so the prelink 2872 * code can throttle both journaled and non-journaled filesystems. 2873 * Threshold is 0 for low and 1 for min. 2874 */ 2875 static int 2876 journal_space(ump, thresh) 2877 struct ufsmount *ump; 2878 int thresh; 2879 { 2880 struct jblocks *jblocks; 2881 int limit, avail; 2882 2883 jblocks = ump->softdep_jblocks; 2884 if (jblocks == NULL) 2885 return (1); 2886 /* 2887 * We use a tighter restriction here to prevent request_cleanup() 2888 * running in threads from running into locks we currently hold. 2889 * We have to be over the limit and our filesystem has to be 2890 * responsible for more than our share of that usage. 2891 */ 2892 limit = (max_softdeps / 10) * 9; 2893 if (dep_current[D_INODEDEP] > limit && 2894 ump->softdep_curdeps[D_INODEDEP] > limit / stat_flush_threads) 2895 return (0); 2896 if (thresh) 2897 thresh = jblocks->jb_min; 2898 else 2899 thresh = jblocks->jb_low; 2900 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2901 avail = jblocks->jb_free - avail; 2902 2903 return (avail > thresh); 2904 } 2905 2906 static void 2907 journal_suspend(ump) 2908 struct ufsmount *ump; 2909 { 2910 struct jblocks *jblocks; 2911 struct mount *mp; 2912 2913 mp = UFSTOVFS(ump); 2914 jblocks = ump->softdep_jblocks; 2915 MNT_ILOCK(mp); 2916 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2917 stat_journal_min++; 2918 mp->mnt_kern_flag |= MNTK_SUSPEND; 2919 mp->mnt_susp_owner = ump->softdep_flushtd; 2920 } 2921 jblocks->jb_suspended = 1; 2922 MNT_IUNLOCK(mp); 2923 } 2924 2925 static int 2926 journal_unsuspend(struct ufsmount *ump) 2927 { 2928 struct jblocks *jblocks; 2929 struct mount *mp; 2930 2931 mp = UFSTOVFS(ump); 2932 jblocks = ump->softdep_jblocks; 2933 2934 if (jblocks != NULL && jblocks->jb_suspended && 2935 journal_space(ump, jblocks->jb_min)) { 2936 jblocks->jb_suspended = 0; 2937 FREE_LOCK(ump); 2938 mp->mnt_susp_owner = curthread; 2939 vfs_write_resume(mp, 0); 2940 ACQUIRE_LOCK(ump); 2941 return (1); 2942 } 2943 return (0); 2944 } 2945 2946 /* 2947 * Called before any allocation function to be certain that there is 2948 * sufficient space in the journal prior to creating any new records. 2949 * Since in the case of block allocation we may have multiple locked 2950 * buffers at the time of the actual allocation we can not block 2951 * when the journal records are created. Doing so would create a deadlock 2952 * if any of these buffers needed to be flushed to reclaim space. Instead 2953 * we require a sufficiently large amount of available space such that 2954 * each thread in the system could have passed this allocation check and 2955 * still have sufficient free space. With 20% of a minimum journal size 2956 * of 1MB we have 6553 records available. 2957 */ 2958 int 2959 softdep_prealloc(vp, waitok) 2960 struct vnode *vp; 2961 int waitok; 2962 { 2963 struct ufsmount *ump; 2964 2965 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 2966 ("softdep_prealloc called on non-softdep filesystem")); 2967 /* 2968 * Nothing to do if we are not running journaled soft updates. 2969 * If we currently hold the snapshot lock, we must avoid handling 2970 * other resources that could cause deadlock. 2971 */ 2972 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp))) 2973 return (0); 2974 ump = VFSTOUFS(vp->v_mount); 2975 ACQUIRE_LOCK(ump); 2976 if (journal_space(ump, 0)) { 2977 FREE_LOCK(ump); 2978 return (0); 2979 } 2980 stat_journal_low++; 2981 FREE_LOCK(ump); 2982 if (waitok == MNT_NOWAIT) 2983 return (ENOSPC); 2984 /* 2985 * Attempt to sync this vnode once to flush any journal 2986 * work attached to it. 2987 */ 2988 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 2989 ffs_syncvnode(vp, waitok, 0); 2990 ACQUIRE_LOCK(ump); 2991 process_removes(vp); 2992 process_truncates(vp); 2993 if (journal_space(ump, 0) == 0) { 2994 softdep_speedup(ump); 2995 if (journal_space(ump, 1) == 0) 2996 journal_suspend(ump); 2997 } 2998 FREE_LOCK(ump); 2999 3000 return (0); 3001 } 3002 3003 /* 3004 * Before adjusting a link count on a vnode verify that we have sufficient 3005 * journal space. If not, process operations that depend on the currently 3006 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 3007 * and softdep flush threads can not acquire these locks to reclaim space. 3008 */ 3009 static void 3010 softdep_prelink(dvp, vp) 3011 struct vnode *dvp; 3012 struct vnode *vp; 3013 { 3014 struct ufsmount *ump; 3015 3016 ump = VFSTOUFS(dvp->v_mount); 3017 LOCK_OWNED(ump); 3018 /* 3019 * Nothing to do if we have sufficient journal space. 3020 * If we currently hold the snapshot lock, we must avoid 3021 * handling other resources that could cause deadlock. 3022 */ 3023 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 3024 return; 3025 stat_journal_low++; 3026 FREE_LOCK(ump); 3027 if (vp) 3028 ffs_syncvnode(vp, MNT_NOWAIT, 0); 3029 ffs_syncvnode(dvp, MNT_WAIT, 0); 3030 ACQUIRE_LOCK(ump); 3031 /* Process vp before dvp as it may create .. removes. */ 3032 if (vp) { 3033 process_removes(vp); 3034 process_truncates(vp); 3035 } 3036 process_removes(dvp); 3037 process_truncates(dvp); 3038 softdep_speedup(ump); 3039 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 3040 if (journal_space(ump, 0) == 0) { 3041 softdep_speedup(ump); 3042 if (journal_space(ump, 1) == 0) 3043 journal_suspend(ump); 3044 } 3045 } 3046 3047 static void 3048 jseg_write(ump, jseg, data) 3049 struct ufsmount *ump; 3050 struct jseg *jseg; 3051 uint8_t *data; 3052 { 3053 struct jsegrec *rec; 3054 3055 rec = (struct jsegrec *)data; 3056 rec->jsr_seq = jseg->js_seq; 3057 rec->jsr_oldest = jseg->js_oldseq; 3058 rec->jsr_cnt = jseg->js_cnt; 3059 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 3060 rec->jsr_crc = 0; 3061 rec->jsr_time = ump->um_fs->fs_mtime; 3062 } 3063 3064 static inline void 3065 inoref_write(inoref, jseg, rec) 3066 struct inoref *inoref; 3067 struct jseg *jseg; 3068 struct jrefrec *rec; 3069 { 3070 3071 inoref->if_jsegdep->jd_seg = jseg; 3072 rec->jr_ino = inoref->if_ino; 3073 rec->jr_parent = inoref->if_parent; 3074 rec->jr_nlink = inoref->if_nlink; 3075 rec->jr_mode = inoref->if_mode; 3076 rec->jr_diroff = inoref->if_diroff; 3077 } 3078 3079 static void 3080 jaddref_write(jaddref, jseg, data) 3081 struct jaddref *jaddref; 3082 struct jseg *jseg; 3083 uint8_t *data; 3084 { 3085 struct jrefrec *rec; 3086 3087 rec = (struct jrefrec *)data; 3088 rec->jr_op = JOP_ADDREF; 3089 inoref_write(&jaddref->ja_ref, jseg, rec); 3090 } 3091 3092 static void 3093 jremref_write(jremref, jseg, data) 3094 struct jremref *jremref; 3095 struct jseg *jseg; 3096 uint8_t *data; 3097 { 3098 struct jrefrec *rec; 3099 3100 rec = (struct jrefrec *)data; 3101 rec->jr_op = JOP_REMREF; 3102 inoref_write(&jremref->jr_ref, jseg, rec); 3103 } 3104 3105 static void 3106 jmvref_write(jmvref, jseg, data) 3107 struct jmvref *jmvref; 3108 struct jseg *jseg; 3109 uint8_t *data; 3110 { 3111 struct jmvrec *rec; 3112 3113 rec = (struct jmvrec *)data; 3114 rec->jm_op = JOP_MVREF; 3115 rec->jm_ino = jmvref->jm_ino; 3116 rec->jm_parent = jmvref->jm_parent; 3117 rec->jm_oldoff = jmvref->jm_oldoff; 3118 rec->jm_newoff = jmvref->jm_newoff; 3119 } 3120 3121 static void 3122 jnewblk_write(jnewblk, jseg, data) 3123 struct jnewblk *jnewblk; 3124 struct jseg *jseg; 3125 uint8_t *data; 3126 { 3127 struct jblkrec *rec; 3128 3129 jnewblk->jn_jsegdep->jd_seg = jseg; 3130 rec = (struct jblkrec *)data; 3131 rec->jb_op = JOP_NEWBLK; 3132 rec->jb_ino = jnewblk->jn_ino; 3133 rec->jb_blkno = jnewblk->jn_blkno; 3134 rec->jb_lbn = jnewblk->jn_lbn; 3135 rec->jb_frags = jnewblk->jn_frags; 3136 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3137 } 3138 3139 static void 3140 jfreeblk_write(jfreeblk, jseg, data) 3141 struct jfreeblk *jfreeblk; 3142 struct jseg *jseg; 3143 uint8_t *data; 3144 { 3145 struct jblkrec *rec; 3146 3147 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3148 rec = (struct jblkrec *)data; 3149 rec->jb_op = JOP_FREEBLK; 3150 rec->jb_ino = jfreeblk->jf_ino; 3151 rec->jb_blkno = jfreeblk->jf_blkno; 3152 rec->jb_lbn = jfreeblk->jf_lbn; 3153 rec->jb_frags = jfreeblk->jf_frags; 3154 rec->jb_oldfrags = 0; 3155 } 3156 3157 static void 3158 jfreefrag_write(jfreefrag, jseg, data) 3159 struct jfreefrag *jfreefrag; 3160 struct jseg *jseg; 3161 uint8_t *data; 3162 { 3163 struct jblkrec *rec; 3164 3165 jfreefrag->fr_jsegdep->jd_seg = jseg; 3166 rec = (struct jblkrec *)data; 3167 rec->jb_op = JOP_FREEBLK; 3168 rec->jb_ino = jfreefrag->fr_ino; 3169 rec->jb_blkno = jfreefrag->fr_blkno; 3170 rec->jb_lbn = jfreefrag->fr_lbn; 3171 rec->jb_frags = jfreefrag->fr_frags; 3172 rec->jb_oldfrags = 0; 3173 } 3174 3175 static void 3176 jtrunc_write(jtrunc, jseg, data) 3177 struct jtrunc *jtrunc; 3178 struct jseg *jseg; 3179 uint8_t *data; 3180 { 3181 struct jtrncrec *rec; 3182 3183 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3184 rec = (struct jtrncrec *)data; 3185 rec->jt_op = JOP_TRUNC; 3186 rec->jt_ino = jtrunc->jt_ino; 3187 rec->jt_size = jtrunc->jt_size; 3188 rec->jt_extsize = jtrunc->jt_extsize; 3189 } 3190 3191 static void 3192 jfsync_write(jfsync, jseg, data) 3193 struct jfsync *jfsync; 3194 struct jseg *jseg; 3195 uint8_t *data; 3196 { 3197 struct jtrncrec *rec; 3198 3199 rec = (struct jtrncrec *)data; 3200 rec->jt_op = JOP_SYNC; 3201 rec->jt_ino = jfsync->jfs_ino; 3202 rec->jt_size = jfsync->jfs_size; 3203 rec->jt_extsize = jfsync->jfs_extsize; 3204 } 3205 3206 static void 3207 softdep_flushjournal(mp) 3208 struct mount *mp; 3209 { 3210 struct jblocks *jblocks; 3211 struct ufsmount *ump; 3212 3213 if (MOUNTEDSUJ(mp) == 0) 3214 return; 3215 ump = VFSTOUFS(mp); 3216 jblocks = ump->softdep_jblocks; 3217 ACQUIRE_LOCK(ump); 3218 while (ump->softdep_on_journal) { 3219 jblocks->jb_needseg = 1; 3220 softdep_process_journal(mp, NULL, MNT_WAIT); 3221 } 3222 FREE_LOCK(ump); 3223 } 3224 3225 static void softdep_synchronize_completed(struct bio *); 3226 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3227 3228 static void 3229 softdep_synchronize_completed(bp) 3230 struct bio *bp; 3231 { 3232 struct jseg *oldest; 3233 struct jseg *jseg; 3234 struct ufsmount *ump; 3235 3236 /* 3237 * caller1 marks the last segment written before we issued the 3238 * synchronize cache. 3239 */ 3240 jseg = bp->bio_caller1; 3241 if (jseg == NULL) { 3242 g_destroy_bio(bp); 3243 return; 3244 } 3245 ump = VFSTOUFS(jseg->js_list.wk_mp); 3246 ACQUIRE_LOCK(ump); 3247 oldest = NULL; 3248 /* 3249 * Mark all the journal entries waiting on the synchronize cache 3250 * as completed so they may continue on. 3251 */ 3252 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3253 jseg->js_state |= COMPLETE; 3254 oldest = jseg; 3255 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3256 } 3257 /* 3258 * Restart deferred journal entry processing from the oldest 3259 * completed jseg. 3260 */ 3261 if (oldest) 3262 complete_jsegs(oldest); 3263 3264 FREE_LOCK(ump); 3265 g_destroy_bio(bp); 3266 } 3267 3268 /* 3269 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3270 * barriers. The journal must be written prior to any blocks that depend 3271 * on it and the journal can not be released until the blocks have be 3272 * written. This code handles both barriers simultaneously. 3273 */ 3274 static void 3275 softdep_synchronize(bp, ump, caller1) 3276 struct bio *bp; 3277 struct ufsmount *ump; 3278 void *caller1; 3279 { 3280 3281 bp->bio_cmd = BIO_FLUSH; 3282 bp->bio_flags |= BIO_ORDERED; 3283 bp->bio_data = NULL; 3284 bp->bio_offset = ump->um_cp->provider->mediasize; 3285 bp->bio_length = 0; 3286 bp->bio_done = softdep_synchronize_completed; 3287 bp->bio_caller1 = caller1; 3288 g_io_request(bp, 3289 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3290 } 3291 3292 /* 3293 * Flush some journal records to disk. 3294 */ 3295 static void 3296 softdep_process_journal(mp, needwk, flags) 3297 struct mount *mp; 3298 struct worklist *needwk; 3299 int flags; 3300 { 3301 struct jblocks *jblocks; 3302 struct ufsmount *ump; 3303 struct worklist *wk; 3304 struct jseg *jseg; 3305 struct buf *bp; 3306 struct bio *bio; 3307 uint8_t *data; 3308 struct fs *fs; 3309 int shouldflush; 3310 int segwritten; 3311 int jrecmin; /* Minimum records per block. */ 3312 int jrecmax; /* Maximum records per block. */ 3313 int size; 3314 int cnt; 3315 int off; 3316 int devbsize; 3317 3318 if (MOUNTEDSUJ(mp) == 0) 3319 return; 3320 shouldflush = softdep_flushcache; 3321 bio = NULL; 3322 jseg = NULL; 3323 ump = VFSTOUFS(mp); 3324 LOCK_OWNED(ump); 3325 fs = ump->um_fs; 3326 jblocks = ump->softdep_jblocks; 3327 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3328 /* 3329 * We write anywhere between a disk block and fs block. The upper 3330 * bound is picked to prevent buffer cache fragmentation and limit 3331 * processing time per I/O. 3332 */ 3333 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3334 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3335 segwritten = 0; 3336 for (;;) { 3337 cnt = ump->softdep_on_journal; 3338 /* 3339 * Criteria for writing a segment: 3340 * 1) We have a full block. 3341 * 2) We're called from jwait() and haven't found the 3342 * journal item yet. 3343 * 3) Always write if needseg is set. 3344 * 4) If we are called from process_worklist and have 3345 * not yet written anything we write a partial block 3346 * to enforce a 1 second maximum latency on journal 3347 * entries. 3348 */ 3349 if (cnt < (jrecmax - 1) && needwk == NULL && 3350 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3351 break; 3352 cnt++; 3353 /* 3354 * Verify some free journal space. softdep_prealloc() should 3355 * guarantee that we don't run out so this is indicative of 3356 * a problem with the flow control. Try to recover 3357 * gracefully in any event. 3358 */ 3359 while (jblocks->jb_free == 0) { 3360 if (flags != MNT_WAIT) 3361 break; 3362 printf("softdep: Out of journal space!\n"); 3363 softdep_speedup(ump); 3364 msleep(jblocks, LOCK_PTR(ump), PRIBIO, "jblocks", hz); 3365 } 3366 FREE_LOCK(ump); 3367 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3368 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3369 LIST_INIT(&jseg->js_entries); 3370 LIST_INIT(&jseg->js_indirs); 3371 jseg->js_state = ATTACHED; 3372 if (shouldflush == 0) 3373 jseg->js_state |= COMPLETE; 3374 else if (bio == NULL) 3375 bio = g_alloc_bio(); 3376 jseg->js_jblocks = jblocks; 3377 bp = geteblk(fs->fs_bsize, 0); 3378 ACQUIRE_LOCK(ump); 3379 /* 3380 * If there was a race while we were allocating the block 3381 * and jseg the entry we care about was likely written. 3382 * We bail out in both the WAIT and NOWAIT case and assume 3383 * the caller will loop if the entry it cares about is 3384 * not written. 3385 */ 3386 cnt = ump->softdep_on_journal; 3387 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3388 bp->b_flags |= B_INVAL | B_NOCACHE; 3389 WORKITEM_FREE(jseg, D_JSEG); 3390 FREE_LOCK(ump); 3391 brelse(bp); 3392 ACQUIRE_LOCK(ump); 3393 break; 3394 } 3395 /* 3396 * Calculate the disk block size required for the available 3397 * records rounded to the min size. 3398 */ 3399 if (cnt == 0) 3400 size = devbsize; 3401 else if (cnt < jrecmax) 3402 size = howmany(cnt, jrecmin) * devbsize; 3403 else 3404 size = fs->fs_bsize; 3405 /* 3406 * Allocate a disk block for this journal data and account 3407 * for truncation of the requested size if enough contiguous 3408 * space was not available. 3409 */ 3410 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3411 bp->b_lblkno = bp->b_blkno; 3412 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3413 bp->b_bcount = size; 3414 bp->b_flags &= ~B_INVAL; 3415 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3416 /* 3417 * Initialize our jseg with cnt records. Assign the next 3418 * sequence number to it and link it in-order. 3419 */ 3420 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3421 jseg->js_buf = bp; 3422 jseg->js_cnt = cnt; 3423 jseg->js_refs = cnt + 1; /* Self ref. */ 3424 jseg->js_size = size; 3425 jseg->js_seq = jblocks->jb_nextseq++; 3426 if (jblocks->jb_oldestseg == NULL) 3427 jblocks->jb_oldestseg = jseg; 3428 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3429 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3430 if (jblocks->jb_writeseg == NULL) 3431 jblocks->jb_writeseg = jseg; 3432 /* 3433 * Start filling in records from the pending list. 3434 */ 3435 data = bp->b_data; 3436 off = 0; 3437 3438 /* 3439 * Always put a header on the first block. 3440 * XXX As with below, there might not be a chance to get 3441 * into the loop. Ensure that something valid is written. 3442 */ 3443 jseg_write(ump, jseg, data); 3444 off += JREC_SIZE; 3445 data = bp->b_data + off; 3446 3447 /* 3448 * XXX Something is wrong here. There's no work to do, 3449 * but we need to perform and I/O and allow it to complete 3450 * anyways. 3451 */ 3452 if (LIST_EMPTY(&ump->softdep_journal_pending)) 3453 stat_emptyjblocks++; 3454 3455 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3456 != NULL) { 3457 if (cnt == 0) 3458 break; 3459 /* Place a segment header on every device block. */ 3460 if ((off % devbsize) == 0) { 3461 jseg_write(ump, jseg, data); 3462 off += JREC_SIZE; 3463 data = bp->b_data + off; 3464 } 3465 if (wk == needwk) 3466 needwk = NULL; 3467 remove_from_journal(wk); 3468 wk->wk_state |= INPROGRESS; 3469 WORKLIST_INSERT(&jseg->js_entries, wk); 3470 switch (wk->wk_type) { 3471 case D_JADDREF: 3472 jaddref_write(WK_JADDREF(wk), jseg, data); 3473 break; 3474 case D_JREMREF: 3475 jremref_write(WK_JREMREF(wk), jseg, data); 3476 break; 3477 case D_JMVREF: 3478 jmvref_write(WK_JMVREF(wk), jseg, data); 3479 break; 3480 case D_JNEWBLK: 3481 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3482 break; 3483 case D_JFREEBLK: 3484 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3485 break; 3486 case D_JFREEFRAG: 3487 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3488 break; 3489 case D_JTRUNC: 3490 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3491 break; 3492 case D_JFSYNC: 3493 jfsync_write(WK_JFSYNC(wk), jseg, data); 3494 break; 3495 default: 3496 panic("process_journal: Unknown type %s", 3497 TYPENAME(wk->wk_type)); 3498 /* NOTREACHED */ 3499 } 3500 off += JREC_SIZE; 3501 data = bp->b_data + off; 3502 cnt--; 3503 } 3504 3505 /* Clear any remaining space so we don't leak kernel data */ 3506 if (size > off) 3507 bzero(data, size - off); 3508 3509 /* 3510 * Write this one buffer and continue. 3511 */ 3512 segwritten = 1; 3513 jblocks->jb_needseg = 0; 3514 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3515 FREE_LOCK(ump); 3516 pbgetvp(ump->um_devvp, bp); 3517 /* 3518 * We only do the blocking wait once we find the journal 3519 * entry we're looking for. 3520 */ 3521 if (needwk == NULL && flags == MNT_WAIT) 3522 bwrite(bp); 3523 else 3524 bawrite(bp); 3525 ACQUIRE_LOCK(ump); 3526 } 3527 /* 3528 * If we wrote a segment issue a synchronize cache so the journal 3529 * is reflected on disk before the data is written. Since reclaiming 3530 * journal space also requires writing a journal record this 3531 * process also enforces a barrier before reclamation. 3532 */ 3533 if (segwritten && shouldflush) { 3534 softdep_synchronize(bio, ump, 3535 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3536 } else if (bio) 3537 g_destroy_bio(bio); 3538 /* 3539 * If we've suspended the filesystem because we ran out of journal 3540 * space either try to sync it here to make some progress or 3541 * unsuspend it if we already have. 3542 */ 3543 if (flags == 0 && jblocks->jb_suspended) { 3544 if (journal_unsuspend(ump)) 3545 return; 3546 FREE_LOCK(ump); 3547 VFS_SYNC(mp, MNT_NOWAIT); 3548 ffs_sbupdate(ump, MNT_WAIT, 0); 3549 ACQUIRE_LOCK(ump); 3550 } 3551 } 3552 3553 /* 3554 * Complete a jseg, allowing all dependencies awaiting journal writes 3555 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3556 * structures so that the journal segment can be freed to reclaim space. 3557 */ 3558 static void 3559 complete_jseg(jseg) 3560 struct jseg *jseg; 3561 { 3562 struct worklist *wk; 3563 struct jmvref *jmvref; 3564 int waiting; 3565 #ifdef INVARIANTS 3566 int i = 0; 3567 #endif 3568 3569 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3570 WORKLIST_REMOVE(wk); 3571 waiting = wk->wk_state & IOWAITING; 3572 wk->wk_state &= ~(INPROGRESS | IOWAITING); 3573 wk->wk_state |= COMPLETE; 3574 KASSERT(i++ < jseg->js_cnt, 3575 ("handle_written_jseg: overflow %d >= %d", 3576 i - 1, jseg->js_cnt)); 3577 switch (wk->wk_type) { 3578 case D_JADDREF: 3579 handle_written_jaddref(WK_JADDREF(wk)); 3580 break; 3581 case D_JREMREF: 3582 handle_written_jremref(WK_JREMREF(wk)); 3583 break; 3584 case D_JMVREF: 3585 rele_jseg(jseg); /* No jsegdep. */ 3586 jmvref = WK_JMVREF(wk); 3587 LIST_REMOVE(jmvref, jm_deps); 3588 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3589 free_pagedep(jmvref->jm_pagedep); 3590 WORKITEM_FREE(jmvref, D_JMVREF); 3591 break; 3592 case D_JNEWBLK: 3593 handle_written_jnewblk(WK_JNEWBLK(wk)); 3594 break; 3595 case D_JFREEBLK: 3596 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3597 break; 3598 case D_JTRUNC: 3599 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3600 break; 3601 case D_JFSYNC: 3602 rele_jseg(jseg); /* No jsegdep. */ 3603 WORKITEM_FREE(wk, D_JFSYNC); 3604 break; 3605 case D_JFREEFRAG: 3606 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3607 break; 3608 default: 3609 panic("handle_written_jseg: Unknown type %s", 3610 TYPENAME(wk->wk_type)); 3611 /* NOTREACHED */ 3612 } 3613 if (waiting) 3614 wakeup(wk); 3615 } 3616 /* Release the self reference so the structure may be freed. */ 3617 rele_jseg(jseg); 3618 } 3619 3620 /* 3621 * Determine which jsegs are ready for completion processing. Waits for 3622 * synchronize cache to complete as well as forcing in-order completion 3623 * of journal entries. 3624 */ 3625 static void 3626 complete_jsegs(jseg) 3627 struct jseg *jseg; 3628 { 3629 struct jblocks *jblocks; 3630 struct jseg *jsegn; 3631 3632 jblocks = jseg->js_jblocks; 3633 /* 3634 * Don't allow out of order completions. If this isn't the first 3635 * block wait for it to write before we're done. 3636 */ 3637 if (jseg != jblocks->jb_writeseg) 3638 return; 3639 /* Iterate through available jsegs processing their entries. */ 3640 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3641 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3642 jsegn = TAILQ_NEXT(jseg, js_next); 3643 complete_jseg(jseg); 3644 jseg = jsegn; 3645 } 3646 jblocks->jb_writeseg = jseg; 3647 /* 3648 * Attempt to free jsegs now that oldestwrseq may have advanced. 3649 */ 3650 free_jsegs(jblocks); 3651 } 3652 3653 /* 3654 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3655 * the final completions. 3656 */ 3657 static void 3658 handle_written_jseg(jseg, bp) 3659 struct jseg *jseg; 3660 struct buf *bp; 3661 { 3662 3663 if (jseg->js_refs == 0) 3664 panic("handle_written_jseg: No self-reference on %p", jseg); 3665 jseg->js_state |= DEPCOMPLETE; 3666 /* 3667 * We'll never need this buffer again, set flags so it will be 3668 * discarded. 3669 */ 3670 bp->b_flags |= B_INVAL | B_NOCACHE; 3671 pbrelvp(bp); 3672 complete_jsegs(jseg); 3673 } 3674 3675 static inline struct jsegdep * 3676 inoref_jseg(inoref) 3677 struct inoref *inoref; 3678 { 3679 struct jsegdep *jsegdep; 3680 3681 jsegdep = inoref->if_jsegdep; 3682 inoref->if_jsegdep = NULL; 3683 3684 return (jsegdep); 3685 } 3686 3687 /* 3688 * Called once a jremref has made it to stable store. The jremref is marked 3689 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3690 * for the jremref to complete will be awoken by free_jremref. 3691 */ 3692 static void 3693 handle_written_jremref(jremref) 3694 struct jremref *jremref; 3695 { 3696 struct inodedep *inodedep; 3697 struct jsegdep *jsegdep; 3698 struct dirrem *dirrem; 3699 3700 /* Grab the jsegdep. */ 3701 jsegdep = inoref_jseg(&jremref->jr_ref); 3702 /* 3703 * Remove us from the inoref list. 3704 */ 3705 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3706 0, &inodedep) == 0) 3707 panic("handle_written_jremref: Lost inodedep"); 3708 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3709 /* 3710 * Complete the dirrem. 3711 */ 3712 dirrem = jremref->jr_dirrem; 3713 jremref->jr_dirrem = NULL; 3714 LIST_REMOVE(jremref, jr_deps); 3715 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3716 jwork_insert(&dirrem->dm_jwork, jsegdep); 3717 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3718 (dirrem->dm_state & COMPLETE) != 0) 3719 add_to_worklist(&dirrem->dm_list, 0); 3720 free_jremref(jremref); 3721 } 3722 3723 /* 3724 * Called once a jaddref has made it to stable store. The dependency is 3725 * marked complete and any dependent structures are added to the inode 3726 * bufwait list to be completed as soon as it is written. If a bitmap write 3727 * depends on this entry we move the inode into the inodedephd of the 3728 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3729 */ 3730 static void 3731 handle_written_jaddref(jaddref) 3732 struct jaddref *jaddref; 3733 { 3734 struct jsegdep *jsegdep; 3735 struct inodedep *inodedep; 3736 struct diradd *diradd; 3737 struct mkdir *mkdir; 3738 3739 /* Grab the jsegdep. */ 3740 jsegdep = inoref_jseg(&jaddref->ja_ref); 3741 mkdir = NULL; 3742 diradd = NULL; 3743 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3744 0, &inodedep) == 0) 3745 panic("handle_written_jaddref: Lost inodedep."); 3746 if (jaddref->ja_diradd == NULL) 3747 panic("handle_written_jaddref: No dependency"); 3748 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3749 diradd = jaddref->ja_diradd; 3750 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3751 } else if (jaddref->ja_state & MKDIR_PARENT) { 3752 mkdir = jaddref->ja_mkdir; 3753 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3754 } else if (jaddref->ja_state & MKDIR_BODY) 3755 mkdir = jaddref->ja_mkdir; 3756 else 3757 panic("handle_written_jaddref: Unknown dependency %p", 3758 jaddref->ja_diradd); 3759 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3760 /* 3761 * Remove us from the inode list. 3762 */ 3763 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3764 /* 3765 * The mkdir may be waiting on the jaddref to clear before freeing. 3766 */ 3767 if (mkdir) { 3768 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3769 ("handle_written_jaddref: Incorrect type for mkdir %s", 3770 TYPENAME(mkdir->md_list.wk_type))); 3771 mkdir->md_jaddref = NULL; 3772 diradd = mkdir->md_diradd; 3773 mkdir->md_state |= DEPCOMPLETE; 3774 complete_mkdir(mkdir); 3775 } 3776 jwork_insert(&diradd->da_jwork, jsegdep); 3777 if (jaddref->ja_state & NEWBLOCK) { 3778 inodedep->id_state |= ONDEPLIST; 3779 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3780 inodedep, id_deps); 3781 } 3782 free_jaddref(jaddref); 3783 } 3784 3785 /* 3786 * Called once a jnewblk journal is written. The allocdirect or allocindir 3787 * is placed in the bmsafemap to await notification of a written bitmap. If 3788 * the operation was canceled we add the segdep to the appropriate 3789 * dependency to free the journal space once the canceling operation 3790 * completes. 3791 */ 3792 static void 3793 handle_written_jnewblk(jnewblk) 3794 struct jnewblk *jnewblk; 3795 { 3796 struct bmsafemap *bmsafemap; 3797 struct freefrag *freefrag; 3798 struct freework *freework; 3799 struct jsegdep *jsegdep; 3800 struct newblk *newblk; 3801 3802 /* Grab the jsegdep. */ 3803 jsegdep = jnewblk->jn_jsegdep; 3804 jnewblk->jn_jsegdep = NULL; 3805 if (jnewblk->jn_dep == NULL) 3806 panic("handle_written_jnewblk: No dependency for the segdep."); 3807 switch (jnewblk->jn_dep->wk_type) { 3808 case D_NEWBLK: 3809 case D_ALLOCDIRECT: 3810 case D_ALLOCINDIR: 3811 /* 3812 * Add the written block to the bmsafemap so it can 3813 * be notified when the bitmap is on disk. 3814 */ 3815 newblk = WK_NEWBLK(jnewblk->jn_dep); 3816 newblk->nb_jnewblk = NULL; 3817 if ((newblk->nb_state & GOINGAWAY) == 0) { 3818 bmsafemap = newblk->nb_bmsafemap; 3819 newblk->nb_state |= ONDEPLIST; 3820 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3821 nb_deps); 3822 } 3823 jwork_insert(&newblk->nb_jwork, jsegdep); 3824 break; 3825 case D_FREEFRAG: 3826 /* 3827 * A newblock being removed by a freefrag when replaced by 3828 * frag extension. 3829 */ 3830 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3831 freefrag->ff_jdep = NULL; 3832 jwork_insert(&freefrag->ff_jwork, jsegdep); 3833 break; 3834 case D_FREEWORK: 3835 /* 3836 * A direct block was removed by truncate. 3837 */ 3838 freework = WK_FREEWORK(jnewblk->jn_dep); 3839 freework->fw_jnewblk = NULL; 3840 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3841 break; 3842 default: 3843 panic("handle_written_jnewblk: Unknown type %d.", 3844 jnewblk->jn_dep->wk_type); 3845 } 3846 jnewblk->jn_dep = NULL; 3847 free_jnewblk(jnewblk); 3848 } 3849 3850 /* 3851 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3852 * an in-flight allocation that has not yet been committed. Divorce us 3853 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3854 * to the worklist. 3855 */ 3856 static void 3857 cancel_jfreefrag(jfreefrag) 3858 struct jfreefrag *jfreefrag; 3859 { 3860 struct freefrag *freefrag; 3861 3862 if (jfreefrag->fr_jsegdep) { 3863 free_jsegdep(jfreefrag->fr_jsegdep); 3864 jfreefrag->fr_jsegdep = NULL; 3865 } 3866 freefrag = jfreefrag->fr_freefrag; 3867 jfreefrag->fr_freefrag = NULL; 3868 free_jfreefrag(jfreefrag); 3869 freefrag->ff_state |= DEPCOMPLETE; 3870 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3871 } 3872 3873 /* 3874 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3875 */ 3876 static void 3877 free_jfreefrag(jfreefrag) 3878 struct jfreefrag *jfreefrag; 3879 { 3880 3881 if (jfreefrag->fr_state & INPROGRESS) 3882 WORKLIST_REMOVE(&jfreefrag->fr_list); 3883 else if (jfreefrag->fr_state & ONWORKLIST) 3884 remove_from_journal(&jfreefrag->fr_list); 3885 if (jfreefrag->fr_freefrag != NULL) 3886 panic("free_jfreefrag: Still attached to a freefrag."); 3887 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3888 } 3889 3890 /* 3891 * Called when the journal write for a jfreefrag completes. The parent 3892 * freefrag is added to the worklist if this completes its dependencies. 3893 */ 3894 static void 3895 handle_written_jfreefrag(jfreefrag) 3896 struct jfreefrag *jfreefrag; 3897 { 3898 struct jsegdep *jsegdep; 3899 struct freefrag *freefrag; 3900 3901 /* Grab the jsegdep. */ 3902 jsegdep = jfreefrag->fr_jsegdep; 3903 jfreefrag->fr_jsegdep = NULL; 3904 freefrag = jfreefrag->fr_freefrag; 3905 if (freefrag == NULL) 3906 panic("handle_written_jfreefrag: No freefrag."); 3907 freefrag->ff_state |= DEPCOMPLETE; 3908 freefrag->ff_jdep = NULL; 3909 jwork_insert(&freefrag->ff_jwork, jsegdep); 3910 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3911 add_to_worklist(&freefrag->ff_list, 0); 3912 jfreefrag->fr_freefrag = NULL; 3913 free_jfreefrag(jfreefrag); 3914 } 3915 3916 /* 3917 * Called when the journal write for a jfreeblk completes. The jfreeblk 3918 * is removed from the freeblks list of pending journal writes and the 3919 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3920 * have been reclaimed. 3921 */ 3922 static void 3923 handle_written_jblkdep(jblkdep) 3924 struct jblkdep *jblkdep; 3925 { 3926 struct freeblks *freeblks; 3927 struct jsegdep *jsegdep; 3928 3929 /* Grab the jsegdep. */ 3930 jsegdep = jblkdep->jb_jsegdep; 3931 jblkdep->jb_jsegdep = NULL; 3932 freeblks = jblkdep->jb_freeblks; 3933 LIST_REMOVE(jblkdep, jb_deps); 3934 jwork_insert(&freeblks->fb_jwork, jsegdep); 3935 /* 3936 * If the freeblks is all journaled, we can add it to the worklist. 3937 */ 3938 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3939 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3940 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3941 3942 free_jblkdep(jblkdep); 3943 } 3944 3945 static struct jsegdep * 3946 newjsegdep(struct worklist *wk) 3947 { 3948 struct jsegdep *jsegdep; 3949 3950 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3951 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3952 jsegdep->jd_seg = NULL; 3953 3954 return (jsegdep); 3955 } 3956 3957 static struct jmvref * 3958 newjmvref(dp, ino, oldoff, newoff) 3959 struct inode *dp; 3960 ino_t ino; 3961 off_t oldoff; 3962 off_t newoff; 3963 { 3964 struct jmvref *jmvref; 3965 3966 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3967 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump)); 3968 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 3969 jmvref->jm_parent = dp->i_number; 3970 jmvref->jm_ino = ino; 3971 jmvref->jm_oldoff = oldoff; 3972 jmvref->jm_newoff = newoff; 3973 3974 return (jmvref); 3975 } 3976 3977 /* 3978 * Allocate a new jremref that tracks the removal of ip from dp with the 3979 * directory entry offset of diroff. Mark the entry as ATTACHED and 3980 * DEPCOMPLETE as we have all the information required for the journal write 3981 * and the directory has already been removed from the buffer. The caller 3982 * is responsible for linking the jremref into the pagedep and adding it 3983 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 3984 * a DOTDOT addition so handle_workitem_remove() can properly assign 3985 * the jsegdep when we're done. 3986 */ 3987 static struct jremref * 3988 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 3989 off_t diroff, nlink_t nlink) 3990 { 3991 struct jremref *jremref; 3992 3993 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 3994 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump)); 3995 jremref->jr_state = ATTACHED; 3996 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 3997 nlink, ip->i_mode); 3998 jremref->jr_dirrem = dirrem; 3999 4000 return (jremref); 4001 } 4002 4003 static inline void 4004 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 4005 nlink_t nlink, uint16_t mode) 4006 { 4007 4008 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 4009 inoref->if_diroff = diroff; 4010 inoref->if_ino = ino; 4011 inoref->if_parent = parent; 4012 inoref->if_nlink = nlink; 4013 inoref->if_mode = mode; 4014 } 4015 4016 /* 4017 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 4018 * directory offset may not be known until later. The caller is responsible 4019 * adding the entry to the journal when this information is available. nlink 4020 * should be the link count prior to the addition and mode is only required 4021 * to have the correct FMT. 4022 */ 4023 static struct jaddref * 4024 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 4025 uint16_t mode) 4026 { 4027 struct jaddref *jaddref; 4028 4029 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 4030 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump)); 4031 jaddref->ja_state = ATTACHED; 4032 jaddref->ja_mkdir = NULL; 4033 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 4034 4035 return (jaddref); 4036 } 4037 4038 /* 4039 * Create a new free dependency for a freework. The caller is responsible 4040 * for adjusting the reference count when it has the lock held. The freedep 4041 * will track an outstanding bitmap write that will ultimately clear the 4042 * freework to continue. 4043 */ 4044 static struct freedep * 4045 newfreedep(struct freework *freework) 4046 { 4047 struct freedep *freedep; 4048 4049 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 4050 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 4051 freedep->fd_freework = freework; 4052 4053 return (freedep); 4054 } 4055 4056 /* 4057 * Free a freedep structure once the buffer it is linked to is written. If 4058 * this is the last reference to the freework schedule it for completion. 4059 */ 4060 static void 4061 free_freedep(freedep) 4062 struct freedep *freedep; 4063 { 4064 struct freework *freework; 4065 4066 freework = freedep->fd_freework; 4067 freework->fw_freeblks->fb_cgwait--; 4068 if (--freework->fw_ref == 0) 4069 freework_enqueue(freework); 4070 WORKITEM_FREE(freedep, D_FREEDEP); 4071 } 4072 4073 /* 4074 * Allocate a new freework structure that may be a level in an indirect 4075 * when parent is not NULL or a top level block when it is. The top level 4076 * freework structures are allocated without the per-filesystem lock held 4077 * and before the freeblks is visible outside of softdep_setup_freeblocks(). 4078 */ 4079 static struct freework * 4080 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 4081 struct ufsmount *ump; 4082 struct freeblks *freeblks; 4083 struct freework *parent; 4084 ufs_lbn_t lbn; 4085 ufs2_daddr_t nb; 4086 int frags; 4087 int off; 4088 int journal; 4089 { 4090 struct freework *freework; 4091 4092 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 4093 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 4094 freework->fw_state = ATTACHED; 4095 freework->fw_jnewblk = NULL; 4096 freework->fw_freeblks = freeblks; 4097 freework->fw_parent = parent; 4098 freework->fw_lbn = lbn; 4099 freework->fw_blkno = nb; 4100 freework->fw_frags = frags; 4101 freework->fw_indir = NULL; 4102 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || lbn >= -NXADDR) 4103 ? 0 : NINDIR(ump->um_fs) + 1; 4104 freework->fw_start = freework->fw_off = off; 4105 if (journal) 4106 newjfreeblk(freeblks, lbn, nb, frags); 4107 if (parent == NULL) { 4108 ACQUIRE_LOCK(ump); 4109 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 4110 freeblks->fb_ref++; 4111 FREE_LOCK(ump); 4112 } 4113 4114 return (freework); 4115 } 4116 4117 /* 4118 * Eliminate a jfreeblk for a block that does not need journaling. 4119 */ 4120 static void 4121 cancel_jfreeblk(freeblks, blkno) 4122 struct freeblks *freeblks; 4123 ufs2_daddr_t blkno; 4124 { 4125 struct jfreeblk *jfreeblk; 4126 struct jblkdep *jblkdep; 4127 4128 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 4129 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 4130 continue; 4131 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 4132 if (jfreeblk->jf_blkno == blkno) 4133 break; 4134 } 4135 if (jblkdep == NULL) 4136 return; 4137 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 4138 free_jsegdep(jblkdep->jb_jsegdep); 4139 LIST_REMOVE(jblkdep, jb_deps); 4140 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 4141 } 4142 4143 /* 4144 * Allocate a new jfreeblk to journal top level block pointer when truncating 4145 * a file. The caller must add this to the worklist when the per-filesystem 4146 * lock is held. 4147 */ 4148 static struct jfreeblk * 4149 newjfreeblk(freeblks, lbn, blkno, frags) 4150 struct freeblks *freeblks; 4151 ufs_lbn_t lbn; 4152 ufs2_daddr_t blkno; 4153 int frags; 4154 { 4155 struct jfreeblk *jfreeblk; 4156 4157 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4158 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4159 freeblks->fb_list.wk_mp); 4160 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4161 jfreeblk->jf_dep.jb_freeblks = freeblks; 4162 jfreeblk->jf_ino = freeblks->fb_inum; 4163 jfreeblk->jf_lbn = lbn; 4164 jfreeblk->jf_blkno = blkno; 4165 jfreeblk->jf_frags = frags; 4166 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4167 4168 return (jfreeblk); 4169 } 4170 4171 /* 4172 * The journal is only prepared to handle full-size block numbers, so we 4173 * have to adjust the record to reflect the change to a full-size block. 4174 * For example, suppose we have a block made up of fragments 8-15 and 4175 * want to free its last two fragments. We are given a request that says: 4176 * FREEBLK ino=5, blkno=14, lbn=0, frags=2, oldfrags=0 4177 * where frags are the number of fragments to free and oldfrags are the 4178 * number of fragments to keep. To block align it, we have to change it to 4179 * have a valid full-size blkno, so it becomes: 4180 * FREEBLK ino=5, blkno=8, lbn=0, frags=2, oldfrags=6 4181 */ 4182 static void 4183 adjust_newfreework(freeblks, frag_offset) 4184 struct freeblks *freeblks; 4185 int frag_offset; 4186 { 4187 struct jfreeblk *jfreeblk; 4188 4189 KASSERT((LIST_FIRST(&freeblks->fb_jblkdephd) != NULL && 4190 LIST_FIRST(&freeblks->fb_jblkdephd)->jb_list.wk_type == D_JFREEBLK), 4191 ("adjust_newfreework: Missing freeblks dependency")); 4192 4193 jfreeblk = WK_JFREEBLK(LIST_FIRST(&freeblks->fb_jblkdephd)); 4194 jfreeblk->jf_blkno -= frag_offset; 4195 jfreeblk->jf_frags += frag_offset; 4196 } 4197 4198 /* 4199 * Allocate a new jtrunc to track a partial truncation. 4200 */ 4201 static struct jtrunc * 4202 newjtrunc(freeblks, size, extsize) 4203 struct freeblks *freeblks; 4204 off_t size; 4205 int extsize; 4206 { 4207 struct jtrunc *jtrunc; 4208 4209 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4210 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4211 freeblks->fb_list.wk_mp); 4212 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4213 jtrunc->jt_dep.jb_freeblks = freeblks; 4214 jtrunc->jt_ino = freeblks->fb_inum; 4215 jtrunc->jt_size = size; 4216 jtrunc->jt_extsize = extsize; 4217 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4218 4219 return (jtrunc); 4220 } 4221 4222 /* 4223 * If we're canceling a new bitmap we have to search for another ref 4224 * to move into the bmsafemap dep. This might be better expressed 4225 * with another structure. 4226 */ 4227 static void 4228 move_newblock_dep(jaddref, inodedep) 4229 struct jaddref *jaddref; 4230 struct inodedep *inodedep; 4231 { 4232 struct inoref *inoref; 4233 struct jaddref *jaddrefn; 4234 4235 jaddrefn = NULL; 4236 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4237 inoref = TAILQ_NEXT(inoref, if_deps)) { 4238 if ((jaddref->ja_state & NEWBLOCK) && 4239 inoref->if_list.wk_type == D_JADDREF) { 4240 jaddrefn = (struct jaddref *)inoref; 4241 break; 4242 } 4243 } 4244 if (jaddrefn == NULL) 4245 return; 4246 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4247 jaddrefn->ja_state |= jaddref->ja_state & 4248 (ATTACHED | UNDONE | NEWBLOCK); 4249 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4250 jaddref->ja_state |= ATTACHED; 4251 LIST_REMOVE(jaddref, ja_bmdeps); 4252 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4253 ja_bmdeps); 4254 } 4255 4256 /* 4257 * Cancel a jaddref either before it has been written or while it is being 4258 * written. This happens when a link is removed before the add reaches 4259 * the disk. The jaddref dependency is kept linked into the bmsafemap 4260 * and inode to prevent the link count or bitmap from reaching the disk 4261 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4262 * required. 4263 * 4264 * Returns 1 if the canceled addref requires journaling of the remove and 4265 * 0 otherwise. 4266 */ 4267 static int 4268 cancel_jaddref(jaddref, inodedep, wkhd) 4269 struct jaddref *jaddref; 4270 struct inodedep *inodedep; 4271 struct workhead *wkhd; 4272 { 4273 struct inoref *inoref; 4274 struct jsegdep *jsegdep; 4275 int needsj; 4276 4277 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4278 ("cancel_jaddref: Canceling complete jaddref")); 4279 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4280 needsj = 1; 4281 else 4282 needsj = 0; 4283 if (inodedep == NULL) 4284 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4285 0, &inodedep) == 0) 4286 panic("cancel_jaddref: Lost inodedep"); 4287 /* 4288 * We must adjust the nlink of any reference operation that follows 4289 * us so that it is consistent with the in-memory reference. This 4290 * ensures that inode nlink rollbacks always have the correct link. 4291 */ 4292 if (needsj == 0) { 4293 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4294 inoref = TAILQ_NEXT(inoref, if_deps)) { 4295 if (inoref->if_state & GOINGAWAY) 4296 break; 4297 inoref->if_nlink--; 4298 } 4299 } 4300 jsegdep = inoref_jseg(&jaddref->ja_ref); 4301 if (jaddref->ja_state & NEWBLOCK) 4302 move_newblock_dep(jaddref, inodedep); 4303 wake_worklist(&jaddref->ja_list); 4304 jaddref->ja_mkdir = NULL; 4305 if (jaddref->ja_state & INPROGRESS) { 4306 jaddref->ja_state &= ~INPROGRESS; 4307 WORKLIST_REMOVE(&jaddref->ja_list); 4308 jwork_insert(wkhd, jsegdep); 4309 } else { 4310 free_jsegdep(jsegdep); 4311 if (jaddref->ja_state & DEPCOMPLETE) 4312 remove_from_journal(&jaddref->ja_list); 4313 } 4314 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4315 /* 4316 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4317 * can arrange for them to be freed with the bitmap. Otherwise we 4318 * no longer need this addref attached to the inoreflst and it 4319 * will incorrectly adjust nlink if we leave it. 4320 */ 4321 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4322 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4323 if_deps); 4324 jaddref->ja_state |= COMPLETE; 4325 free_jaddref(jaddref); 4326 return (needsj); 4327 } 4328 /* 4329 * Leave the head of the list for jsegdeps for fast merging. 4330 */ 4331 if (LIST_FIRST(wkhd) != NULL) { 4332 jaddref->ja_state |= ONWORKLIST; 4333 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4334 } else 4335 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4336 4337 return (needsj); 4338 } 4339 4340 /* 4341 * Attempt to free a jaddref structure when some work completes. This 4342 * should only succeed once the entry is written and all dependencies have 4343 * been notified. 4344 */ 4345 static void 4346 free_jaddref(jaddref) 4347 struct jaddref *jaddref; 4348 { 4349 4350 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4351 return; 4352 if (jaddref->ja_ref.if_jsegdep) 4353 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4354 jaddref, jaddref->ja_state); 4355 if (jaddref->ja_state & NEWBLOCK) 4356 LIST_REMOVE(jaddref, ja_bmdeps); 4357 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4358 panic("free_jaddref: Bad state %p(0x%X)", 4359 jaddref, jaddref->ja_state); 4360 if (jaddref->ja_mkdir != NULL) 4361 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4362 WORKITEM_FREE(jaddref, D_JADDREF); 4363 } 4364 4365 /* 4366 * Free a jremref structure once it has been written or discarded. 4367 */ 4368 static void 4369 free_jremref(jremref) 4370 struct jremref *jremref; 4371 { 4372 4373 if (jremref->jr_ref.if_jsegdep) 4374 free_jsegdep(jremref->jr_ref.if_jsegdep); 4375 if (jremref->jr_state & INPROGRESS) 4376 panic("free_jremref: IO still pending"); 4377 WORKITEM_FREE(jremref, D_JREMREF); 4378 } 4379 4380 /* 4381 * Free a jnewblk structure. 4382 */ 4383 static void 4384 free_jnewblk(jnewblk) 4385 struct jnewblk *jnewblk; 4386 { 4387 4388 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4389 return; 4390 LIST_REMOVE(jnewblk, jn_deps); 4391 if (jnewblk->jn_dep != NULL) 4392 panic("free_jnewblk: Dependency still attached."); 4393 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4394 } 4395 4396 /* 4397 * Cancel a jnewblk which has been been made redundant by frag extension. 4398 */ 4399 static void 4400 cancel_jnewblk(jnewblk, wkhd) 4401 struct jnewblk *jnewblk; 4402 struct workhead *wkhd; 4403 { 4404 struct jsegdep *jsegdep; 4405 4406 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4407 jsegdep = jnewblk->jn_jsegdep; 4408 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4409 panic("cancel_jnewblk: Invalid state"); 4410 jnewblk->jn_jsegdep = NULL; 4411 jnewblk->jn_dep = NULL; 4412 jnewblk->jn_state |= GOINGAWAY; 4413 if (jnewblk->jn_state & INPROGRESS) { 4414 jnewblk->jn_state &= ~INPROGRESS; 4415 WORKLIST_REMOVE(&jnewblk->jn_list); 4416 jwork_insert(wkhd, jsegdep); 4417 } else { 4418 free_jsegdep(jsegdep); 4419 remove_from_journal(&jnewblk->jn_list); 4420 } 4421 wake_worklist(&jnewblk->jn_list); 4422 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4423 } 4424 4425 static void 4426 free_jblkdep(jblkdep) 4427 struct jblkdep *jblkdep; 4428 { 4429 4430 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4431 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4432 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4433 WORKITEM_FREE(jblkdep, D_JTRUNC); 4434 else 4435 panic("free_jblkdep: Unexpected type %s", 4436 TYPENAME(jblkdep->jb_list.wk_type)); 4437 } 4438 4439 /* 4440 * Free a single jseg once it is no longer referenced in memory or on 4441 * disk. Reclaim journal blocks and dependencies waiting for the segment 4442 * to disappear. 4443 */ 4444 static void 4445 free_jseg(jseg, jblocks) 4446 struct jseg *jseg; 4447 struct jblocks *jblocks; 4448 { 4449 struct freework *freework; 4450 4451 /* 4452 * Free freework structures that were lingering to indicate freed 4453 * indirect blocks that forced journal write ordering on reallocate. 4454 */ 4455 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4456 indirblk_remove(freework); 4457 if (jblocks->jb_oldestseg == jseg) 4458 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4459 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4460 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4461 KASSERT(LIST_EMPTY(&jseg->js_entries), 4462 ("free_jseg: Freed jseg has valid entries.")); 4463 WORKITEM_FREE(jseg, D_JSEG); 4464 } 4465 4466 /* 4467 * Free all jsegs that meet the criteria for being reclaimed and update 4468 * oldestseg. 4469 */ 4470 static void 4471 free_jsegs(jblocks) 4472 struct jblocks *jblocks; 4473 { 4474 struct jseg *jseg; 4475 4476 /* 4477 * Free only those jsegs which have none allocated before them to 4478 * preserve the journal space ordering. 4479 */ 4480 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4481 /* 4482 * Only reclaim space when nothing depends on this journal 4483 * set and another set has written that it is no longer 4484 * valid. 4485 */ 4486 if (jseg->js_refs != 0) { 4487 jblocks->jb_oldestseg = jseg; 4488 return; 4489 } 4490 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4491 break; 4492 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4493 break; 4494 /* 4495 * We can free jsegs that didn't write entries when 4496 * oldestwrseq == js_seq. 4497 */ 4498 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4499 jseg->js_cnt != 0) 4500 break; 4501 free_jseg(jseg, jblocks); 4502 } 4503 /* 4504 * If we exited the loop above we still must discover the 4505 * oldest valid segment. 4506 */ 4507 if (jseg) 4508 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4509 jseg = TAILQ_NEXT(jseg, js_next)) 4510 if (jseg->js_refs != 0) 4511 break; 4512 jblocks->jb_oldestseg = jseg; 4513 /* 4514 * The journal has no valid records but some jsegs may still be 4515 * waiting on oldestwrseq to advance. We force a small record 4516 * out to permit these lingering records to be reclaimed. 4517 */ 4518 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4519 jblocks->jb_needseg = 1; 4520 } 4521 4522 /* 4523 * Release one reference to a jseg and free it if the count reaches 0. This 4524 * should eventually reclaim journal space as well. 4525 */ 4526 static void 4527 rele_jseg(jseg) 4528 struct jseg *jseg; 4529 { 4530 4531 KASSERT(jseg->js_refs > 0, 4532 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4533 if (--jseg->js_refs != 0) 4534 return; 4535 free_jsegs(jseg->js_jblocks); 4536 } 4537 4538 /* 4539 * Release a jsegdep and decrement the jseg count. 4540 */ 4541 static void 4542 free_jsegdep(jsegdep) 4543 struct jsegdep *jsegdep; 4544 { 4545 4546 if (jsegdep->jd_seg) 4547 rele_jseg(jsegdep->jd_seg); 4548 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4549 } 4550 4551 /* 4552 * Wait for a journal item to make it to disk. Initiate journal processing 4553 * if required. 4554 */ 4555 static int 4556 jwait(wk, waitfor) 4557 struct worklist *wk; 4558 int waitfor; 4559 { 4560 4561 LOCK_OWNED(VFSTOUFS(wk->wk_mp)); 4562 /* 4563 * Blocking journal waits cause slow synchronous behavior. Record 4564 * stats on the frequency of these blocking operations. 4565 */ 4566 if (waitfor == MNT_WAIT) { 4567 stat_journal_wait++; 4568 switch (wk->wk_type) { 4569 case D_JREMREF: 4570 case D_JMVREF: 4571 stat_jwait_filepage++; 4572 break; 4573 case D_JTRUNC: 4574 case D_JFREEBLK: 4575 stat_jwait_freeblks++; 4576 break; 4577 case D_JNEWBLK: 4578 stat_jwait_newblk++; 4579 break; 4580 case D_JADDREF: 4581 stat_jwait_inode++; 4582 break; 4583 default: 4584 break; 4585 } 4586 } 4587 /* 4588 * If IO has not started we process the journal. We can't mark the 4589 * worklist item as IOWAITING because we drop the lock while 4590 * processing the journal and the worklist entry may be freed after 4591 * this point. The caller may call back in and re-issue the request. 4592 */ 4593 if ((wk->wk_state & INPROGRESS) == 0) { 4594 softdep_process_journal(wk->wk_mp, wk, waitfor); 4595 if (waitfor != MNT_WAIT) 4596 return (EBUSY); 4597 return (0); 4598 } 4599 if (waitfor != MNT_WAIT) 4600 return (EBUSY); 4601 wait_worklist(wk, "jwait"); 4602 return (0); 4603 } 4604 4605 /* 4606 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4607 * appropriate. This is a convenience function to reduce duplicate code 4608 * for the setup and revert functions below. 4609 */ 4610 static struct inodedep * 4611 inodedep_lookup_ip(ip) 4612 struct inode *ip; 4613 { 4614 struct inodedep *inodedep; 4615 int dflags; 4616 4617 KASSERT(ip->i_nlink >= ip->i_effnlink, 4618 ("inodedep_lookup_ip: bad delta")); 4619 dflags = DEPALLOC; 4620 if (IS_SNAPSHOT(ip)) 4621 dflags |= NODELAY; 4622 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, 4623 &inodedep); 4624 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4625 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4626 4627 return (inodedep); 4628 } 4629 4630 /* 4631 * Called prior to creating a new inode and linking it to a directory. The 4632 * jaddref structure must already be allocated by softdep_setup_inomapdep 4633 * and it is discovered here so we can initialize the mode and update 4634 * nlinkdelta. 4635 */ 4636 void 4637 softdep_setup_create(dp, ip) 4638 struct inode *dp; 4639 struct inode *ip; 4640 { 4641 struct inodedep *inodedep; 4642 struct jaddref *jaddref; 4643 struct vnode *dvp; 4644 4645 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4646 ("softdep_setup_create called on non-softdep filesystem")); 4647 KASSERT(ip->i_nlink == 1, 4648 ("softdep_setup_create: Invalid link count.")); 4649 dvp = ITOV(dp); 4650 ACQUIRE_LOCK(dp->i_ump); 4651 inodedep = inodedep_lookup_ip(ip); 4652 if (DOINGSUJ(dvp)) { 4653 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4654 inoreflst); 4655 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4656 ("softdep_setup_create: No addref structure present.")); 4657 } 4658 softdep_prelink(dvp, NULL); 4659 FREE_LOCK(dp->i_ump); 4660 } 4661 4662 /* 4663 * Create a jaddref structure to track the addition of a DOTDOT link when 4664 * we are reparenting an inode as part of a rename. This jaddref will be 4665 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4666 * non-journaling softdep. 4667 */ 4668 void 4669 softdep_setup_dotdot_link(dp, ip) 4670 struct inode *dp; 4671 struct inode *ip; 4672 { 4673 struct inodedep *inodedep; 4674 struct jaddref *jaddref; 4675 struct vnode *dvp; 4676 struct vnode *vp; 4677 4678 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4679 ("softdep_setup_dotdot_link called on non-softdep filesystem")); 4680 dvp = ITOV(dp); 4681 vp = ITOV(ip); 4682 jaddref = NULL; 4683 /* 4684 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4685 * is used as a normal link would be. 4686 */ 4687 if (DOINGSUJ(dvp)) 4688 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4689 dp->i_effnlink - 1, dp->i_mode); 4690 ACQUIRE_LOCK(dp->i_ump); 4691 inodedep = inodedep_lookup_ip(dp); 4692 if (jaddref) 4693 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4694 if_deps); 4695 softdep_prelink(dvp, ITOV(ip)); 4696 FREE_LOCK(dp->i_ump); 4697 } 4698 4699 /* 4700 * Create a jaddref structure to track a new link to an inode. The directory 4701 * offset is not known until softdep_setup_directory_add or 4702 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4703 * softdep. 4704 */ 4705 void 4706 softdep_setup_link(dp, ip) 4707 struct inode *dp; 4708 struct inode *ip; 4709 { 4710 struct inodedep *inodedep; 4711 struct jaddref *jaddref; 4712 struct vnode *dvp; 4713 4714 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4715 ("softdep_setup_link called on non-softdep filesystem")); 4716 dvp = ITOV(dp); 4717 jaddref = NULL; 4718 if (DOINGSUJ(dvp)) 4719 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4720 ip->i_mode); 4721 ACQUIRE_LOCK(dp->i_ump); 4722 inodedep = inodedep_lookup_ip(ip); 4723 if (jaddref) 4724 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4725 if_deps); 4726 softdep_prelink(dvp, ITOV(ip)); 4727 FREE_LOCK(dp->i_ump); 4728 } 4729 4730 /* 4731 * Called to create the jaddref structures to track . and .. references as 4732 * well as lookup and further initialize the incomplete jaddref created 4733 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4734 * nlinkdelta for non-journaling softdep. 4735 */ 4736 void 4737 softdep_setup_mkdir(dp, ip) 4738 struct inode *dp; 4739 struct inode *ip; 4740 { 4741 struct inodedep *inodedep; 4742 struct jaddref *dotdotaddref; 4743 struct jaddref *dotaddref; 4744 struct jaddref *jaddref; 4745 struct vnode *dvp; 4746 4747 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4748 ("softdep_setup_mkdir called on non-softdep filesystem")); 4749 dvp = ITOV(dp); 4750 dotaddref = dotdotaddref = NULL; 4751 if (DOINGSUJ(dvp)) { 4752 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4753 ip->i_mode); 4754 dotaddref->ja_state |= MKDIR_BODY; 4755 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4756 dp->i_effnlink - 1, dp->i_mode); 4757 dotdotaddref->ja_state |= MKDIR_PARENT; 4758 } 4759 ACQUIRE_LOCK(dp->i_ump); 4760 inodedep = inodedep_lookup_ip(ip); 4761 if (DOINGSUJ(dvp)) { 4762 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4763 inoreflst); 4764 KASSERT(jaddref != NULL, 4765 ("softdep_setup_mkdir: No addref structure present.")); 4766 KASSERT(jaddref->ja_parent == dp->i_number, 4767 ("softdep_setup_mkdir: bad parent %ju", 4768 (uintmax_t)jaddref->ja_parent)); 4769 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4770 if_deps); 4771 } 4772 inodedep = inodedep_lookup_ip(dp); 4773 if (DOINGSUJ(dvp)) 4774 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4775 &dotdotaddref->ja_ref, if_deps); 4776 softdep_prelink(ITOV(dp), NULL); 4777 FREE_LOCK(dp->i_ump); 4778 } 4779 4780 /* 4781 * Called to track nlinkdelta of the inode and parent directories prior to 4782 * unlinking a directory. 4783 */ 4784 void 4785 softdep_setup_rmdir(dp, ip) 4786 struct inode *dp; 4787 struct inode *ip; 4788 { 4789 struct vnode *dvp; 4790 4791 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4792 ("softdep_setup_rmdir called on non-softdep filesystem")); 4793 dvp = ITOV(dp); 4794 ACQUIRE_LOCK(dp->i_ump); 4795 (void) inodedep_lookup_ip(ip); 4796 (void) inodedep_lookup_ip(dp); 4797 softdep_prelink(dvp, ITOV(ip)); 4798 FREE_LOCK(dp->i_ump); 4799 } 4800 4801 /* 4802 * Called to track nlinkdelta of the inode and parent directories prior to 4803 * unlink. 4804 */ 4805 void 4806 softdep_setup_unlink(dp, ip) 4807 struct inode *dp; 4808 struct inode *ip; 4809 { 4810 struct vnode *dvp; 4811 4812 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4813 ("softdep_setup_unlink called on non-softdep filesystem")); 4814 dvp = ITOV(dp); 4815 ACQUIRE_LOCK(dp->i_ump); 4816 (void) inodedep_lookup_ip(ip); 4817 (void) inodedep_lookup_ip(dp); 4818 softdep_prelink(dvp, ITOV(ip)); 4819 FREE_LOCK(dp->i_ump); 4820 } 4821 4822 /* 4823 * Called to release the journal structures created by a failed non-directory 4824 * creation. Adjusts nlinkdelta for non-journaling softdep. 4825 */ 4826 void 4827 softdep_revert_create(dp, ip) 4828 struct inode *dp; 4829 struct inode *ip; 4830 { 4831 struct inodedep *inodedep; 4832 struct jaddref *jaddref; 4833 struct vnode *dvp; 4834 4835 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4836 ("softdep_revert_create called on non-softdep filesystem")); 4837 dvp = ITOV(dp); 4838 ACQUIRE_LOCK(dp->i_ump); 4839 inodedep = inodedep_lookup_ip(ip); 4840 if (DOINGSUJ(dvp)) { 4841 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4842 inoreflst); 4843 KASSERT(jaddref->ja_parent == dp->i_number, 4844 ("softdep_revert_create: addref parent mismatch")); 4845 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4846 } 4847 FREE_LOCK(dp->i_ump); 4848 } 4849 4850 /* 4851 * Called to release the journal structures created by a failed link 4852 * addition. Adjusts nlinkdelta for non-journaling softdep. 4853 */ 4854 void 4855 softdep_revert_link(dp, ip) 4856 struct inode *dp; 4857 struct inode *ip; 4858 { 4859 struct inodedep *inodedep; 4860 struct jaddref *jaddref; 4861 struct vnode *dvp; 4862 4863 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4864 ("softdep_revert_link called on non-softdep filesystem")); 4865 dvp = ITOV(dp); 4866 ACQUIRE_LOCK(dp->i_ump); 4867 inodedep = inodedep_lookup_ip(ip); 4868 if (DOINGSUJ(dvp)) { 4869 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4870 inoreflst); 4871 KASSERT(jaddref->ja_parent == dp->i_number, 4872 ("softdep_revert_link: addref parent mismatch")); 4873 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4874 } 4875 FREE_LOCK(dp->i_ump); 4876 } 4877 4878 /* 4879 * Called to release the journal structures created by a failed mkdir 4880 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4881 */ 4882 void 4883 softdep_revert_mkdir(dp, ip) 4884 struct inode *dp; 4885 struct inode *ip; 4886 { 4887 struct inodedep *inodedep; 4888 struct jaddref *jaddref; 4889 struct jaddref *dotaddref; 4890 struct vnode *dvp; 4891 4892 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4893 ("softdep_revert_mkdir called on non-softdep filesystem")); 4894 dvp = ITOV(dp); 4895 4896 ACQUIRE_LOCK(dp->i_ump); 4897 inodedep = inodedep_lookup_ip(dp); 4898 if (DOINGSUJ(dvp)) { 4899 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4900 inoreflst); 4901 KASSERT(jaddref->ja_parent == ip->i_number, 4902 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4903 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4904 } 4905 inodedep = inodedep_lookup_ip(ip); 4906 if (DOINGSUJ(dvp)) { 4907 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4908 inoreflst); 4909 KASSERT(jaddref->ja_parent == dp->i_number, 4910 ("softdep_revert_mkdir: addref parent mismatch")); 4911 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4912 inoreflst, if_deps); 4913 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4914 KASSERT(dotaddref->ja_parent == ip->i_number, 4915 ("softdep_revert_mkdir: dot addref parent mismatch")); 4916 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4917 } 4918 FREE_LOCK(dp->i_ump); 4919 } 4920 4921 /* 4922 * Called to correct nlinkdelta after a failed rmdir. 4923 */ 4924 void 4925 softdep_revert_rmdir(dp, ip) 4926 struct inode *dp; 4927 struct inode *ip; 4928 { 4929 4930 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(dp->i_ump)) != 0, 4931 ("softdep_revert_rmdir called on non-softdep filesystem")); 4932 ACQUIRE_LOCK(dp->i_ump); 4933 (void) inodedep_lookup_ip(ip); 4934 (void) inodedep_lookup_ip(dp); 4935 FREE_LOCK(dp->i_ump); 4936 } 4937 4938 /* 4939 * Protecting the freemaps (or bitmaps). 4940 * 4941 * To eliminate the need to execute fsck before mounting a filesystem 4942 * after a power failure, one must (conservatively) guarantee that the 4943 * on-disk copy of the bitmaps never indicate that a live inode or block is 4944 * free. So, when a block or inode is allocated, the bitmap should be 4945 * updated (on disk) before any new pointers. When a block or inode is 4946 * freed, the bitmap should not be updated until all pointers have been 4947 * reset. The latter dependency is handled by the delayed de-allocation 4948 * approach described below for block and inode de-allocation. The former 4949 * dependency is handled by calling the following procedure when a block or 4950 * inode is allocated. When an inode is allocated an "inodedep" is created 4951 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4952 * Each "inodedep" is also inserted into the hash indexing structure so 4953 * that any additional link additions can be made dependent on the inode 4954 * allocation. 4955 * 4956 * The ufs filesystem maintains a number of free block counts (e.g., per 4957 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4958 * in addition to the bitmaps. These counts are used to improve efficiency 4959 * during allocation and therefore must be consistent with the bitmaps. 4960 * There is no convenient way to guarantee post-crash consistency of these 4961 * counts with simple update ordering, for two main reasons: (1) The counts 4962 * and bitmaps for a single cylinder group block are not in the same disk 4963 * sector. If a disk write is interrupted (e.g., by power failure), one may 4964 * be written and the other not. (2) Some of the counts are located in the 4965 * superblock rather than the cylinder group block. So, we focus our soft 4966 * updates implementation on protecting the bitmaps. When mounting a 4967 * filesystem, we recompute the auxiliary counts from the bitmaps. 4968 */ 4969 4970 /* 4971 * Called just after updating the cylinder group block to allocate an inode. 4972 */ 4973 void 4974 softdep_setup_inomapdep(bp, ip, newinum, mode) 4975 struct buf *bp; /* buffer for cylgroup block with inode map */ 4976 struct inode *ip; /* inode related to allocation */ 4977 ino_t newinum; /* new inode number being allocated */ 4978 int mode; 4979 { 4980 struct inodedep *inodedep; 4981 struct bmsafemap *bmsafemap; 4982 struct jaddref *jaddref; 4983 struct mount *mp; 4984 struct fs *fs; 4985 4986 mp = UFSTOVFS(ip->i_ump); 4987 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 4988 ("softdep_setup_inomapdep called on non-softdep filesystem")); 4989 fs = ip->i_ump->um_fs; 4990 jaddref = NULL; 4991 4992 /* 4993 * Allocate the journal reference add structure so that the bitmap 4994 * can be dependent on it. 4995 */ 4996 if (MOUNTEDSUJ(mp)) { 4997 jaddref = newjaddref(ip, newinum, 0, 0, mode); 4998 jaddref->ja_state |= NEWBLOCK; 4999 } 5000 5001 /* 5002 * Create a dependency for the newly allocated inode. 5003 * Panic if it already exists as something is seriously wrong. 5004 * Otherwise add it to the dependency list for the buffer holding 5005 * the cylinder group map from which it was allocated. 5006 * 5007 * We have to preallocate a bmsafemap entry in case it is needed 5008 * in bmsafemap_lookup since once we allocate the inodedep, we 5009 * have to finish initializing it before we can FREE_LOCK(). 5010 * By preallocating, we avoid FREE_LOCK() while doing a malloc 5011 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 5012 * creating the inodedep as it can be freed during the time 5013 * that we FREE_LOCK() while allocating the inodedep. We must 5014 * call workitem_alloc() before entering the locked section as 5015 * it also acquires the lock and we must avoid trying doing so 5016 * recursively. 5017 */ 5018 bmsafemap = malloc(sizeof(struct bmsafemap), 5019 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5020 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5021 ACQUIRE_LOCK(ip->i_ump); 5022 if ((inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep))) 5023 panic("softdep_setup_inomapdep: dependency %p for new" 5024 "inode already exists", inodedep); 5025 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 5026 if (jaddref) { 5027 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 5028 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 5029 if_deps); 5030 } else { 5031 inodedep->id_state |= ONDEPLIST; 5032 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 5033 } 5034 inodedep->id_bmsafemap = bmsafemap; 5035 inodedep->id_state &= ~DEPCOMPLETE; 5036 FREE_LOCK(ip->i_ump); 5037 } 5038 5039 /* 5040 * Called just after updating the cylinder group block to 5041 * allocate block or fragment. 5042 */ 5043 void 5044 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 5045 struct buf *bp; /* buffer for cylgroup block with block map */ 5046 struct mount *mp; /* filesystem doing allocation */ 5047 ufs2_daddr_t newblkno; /* number of newly allocated block */ 5048 int frags; /* Number of fragments. */ 5049 int oldfrags; /* Previous number of fragments for extend. */ 5050 { 5051 struct newblk *newblk; 5052 struct bmsafemap *bmsafemap; 5053 struct jnewblk *jnewblk; 5054 struct ufsmount *ump; 5055 struct fs *fs; 5056 5057 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5058 ("softdep_setup_blkmapdep called on non-softdep filesystem")); 5059 ump = VFSTOUFS(mp); 5060 fs = ump->um_fs; 5061 jnewblk = NULL; 5062 /* 5063 * Create a dependency for the newly allocated block. 5064 * Add it to the dependency list for the buffer holding 5065 * the cylinder group map from which it was allocated. 5066 */ 5067 if (MOUNTEDSUJ(mp)) { 5068 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 5069 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 5070 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 5071 jnewblk->jn_state = ATTACHED; 5072 jnewblk->jn_blkno = newblkno; 5073 jnewblk->jn_frags = frags; 5074 jnewblk->jn_oldfrags = oldfrags; 5075 #ifdef SUJ_DEBUG 5076 { 5077 struct cg *cgp; 5078 uint8_t *blksfree; 5079 long bno; 5080 int i; 5081 5082 cgp = (struct cg *)bp->b_data; 5083 blksfree = cg_blksfree(cgp); 5084 bno = dtogd(fs, jnewblk->jn_blkno); 5085 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 5086 i++) { 5087 if (isset(blksfree, bno + i)) 5088 panic("softdep_setup_blkmapdep: " 5089 "free fragment %d from %d-%d " 5090 "state 0x%X dep %p", i, 5091 jnewblk->jn_oldfrags, 5092 jnewblk->jn_frags, 5093 jnewblk->jn_state, 5094 jnewblk->jn_dep); 5095 } 5096 } 5097 #endif 5098 } 5099 5100 CTR3(KTR_SUJ, 5101 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 5102 newblkno, frags, oldfrags); 5103 ACQUIRE_LOCK(ump); 5104 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 5105 panic("softdep_setup_blkmapdep: found block"); 5106 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 5107 dtog(fs, newblkno), NULL); 5108 if (jnewblk) { 5109 jnewblk->jn_dep = (struct worklist *)newblk; 5110 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 5111 } else { 5112 newblk->nb_state |= ONDEPLIST; 5113 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 5114 } 5115 newblk->nb_bmsafemap = bmsafemap; 5116 newblk->nb_jnewblk = jnewblk; 5117 FREE_LOCK(ump); 5118 } 5119 5120 #define BMSAFEMAP_HASH(ump, cg) \ 5121 (&(ump)->bmsafemap_hashtbl[(cg) & (ump)->bmsafemap_hash_size]) 5122 5123 static int 5124 bmsafemap_find(bmsafemaphd, cg, bmsafemapp) 5125 struct bmsafemap_hashhead *bmsafemaphd; 5126 int cg; 5127 struct bmsafemap **bmsafemapp; 5128 { 5129 struct bmsafemap *bmsafemap; 5130 5131 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 5132 if (bmsafemap->sm_cg == cg) 5133 break; 5134 if (bmsafemap) { 5135 *bmsafemapp = bmsafemap; 5136 return (1); 5137 } 5138 *bmsafemapp = NULL; 5139 5140 return (0); 5141 } 5142 5143 /* 5144 * Find the bmsafemap associated with a cylinder group buffer. 5145 * If none exists, create one. The buffer must be locked when 5146 * this routine is called and this routine must be called with 5147 * the softdep lock held. To avoid giving up the lock while 5148 * allocating a new bmsafemap, a preallocated bmsafemap may be 5149 * provided. If it is provided but not needed, it is freed. 5150 */ 5151 static struct bmsafemap * 5152 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 5153 struct mount *mp; 5154 struct buf *bp; 5155 int cg; 5156 struct bmsafemap *newbmsafemap; 5157 { 5158 struct bmsafemap_hashhead *bmsafemaphd; 5159 struct bmsafemap *bmsafemap, *collision; 5160 struct worklist *wk; 5161 struct ufsmount *ump; 5162 5163 ump = VFSTOUFS(mp); 5164 LOCK_OWNED(ump); 5165 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 5166 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5167 if (wk->wk_type == D_BMSAFEMAP) { 5168 if (newbmsafemap) 5169 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5170 return (WK_BMSAFEMAP(wk)); 5171 } 5172 } 5173 bmsafemaphd = BMSAFEMAP_HASH(ump, cg); 5174 if (bmsafemap_find(bmsafemaphd, cg, &bmsafemap) == 1) { 5175 if (newbmsafemap) 5176 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5177 return (bmsafemap); 5178 } 5179 if (newbmsafemap) { 5180 bmsafemap = newbmsafemap; 5181 } else { 5182 FREE_LOCK(ump); 5183 bmsafemap = malloc(sizeof(struct bmsafemap), 5184 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5185 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5186 ACQUIRE_LOCK(ump); 5187 } 5188 bmsafemap->sm_buf = bp; 5189 LIST_INIT(&bmsafemap->sm_inodedephd); 5190 LIST_INIT(&bmsafemap->sm_inodedepwr); 5191 LIST_INIT(&bmsafemap->sm_newblkhd); 5192 LIST_INIT(&bmsafemap->sm_newblkwr); 5193 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5194 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5195 LIST_INIT(&bmsafemap->sm_freehd); 5196 LIST_INIT(&bmsafemap->sm_freewr); 5197 if (bmsafemap_find(bmsafemaphd, cg, &collision) == 1) { 5198 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5199 return (collision); 5200 } 5201 bmsafemap->sm_cg = cg; 5202 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5203 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 5204 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5205 return (bmsafemap); 5206 } 5207 5208 /* 5209 * Direct block allocation dependencies. 5210 * 5211 * When a new block is allocated, the corresponding disk locations must be 5212 * initialized (with zeros or new data) before the on-disk inode points to 5213 * them. Also, the freemap from which the block was allocated must be 5214 * updated (on disk) before the inode's pointer. These two dependencies are 5215 * independent of each other and are needed for all file blocks and indirect 5216 * blocks that are pointed to directly by the inode. Just before the 5217 * "in-core" version of the inode is updated with a newly allocated block 5218 * number, a procedure (below) is called to setup allocation dependency 5219 * structures. These structures are removed when the corresponding 5220 * dependencies are satisfied or when the block allocation becomes obsolete 5221 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5222 * fragment that gets upgraded). All of these cases are handled in 5223 * procedures described later. 5224 * 5225 * When a file extension causes a fragment to be upgraded, either to a larger 5226 * fragment or to a full block, the on-disk location may change (if the 5227 * previous fragment could not simply be extended). In this case, the old 5228 * fragment must be de-allocated, but not until after the inode's pointer has 5229 * been updated. In most cases, this is handled by later procedures, which 5230 * will construct a "freefrag" structure to be added to the workitem queue 5231 * when the inode update is complete (or obsolete). The main exception to 5232 * this is when an allocation occurs while a pending allocation dependency 5233 * (for the same block pointer) remains. This case is handled in the main 5234 * allocation dependency setup procedure by immediately freeing the 5235 * unreferenced fragments. 5236 */ 5237 void 5238 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5239 struct inode *ip; /* inode to which block is being added */ 5240 ufs_lbn_t off; /* block pointer within inode */ 5241 ufs2_daddr_t newblkno; /* disk block number being added */ 5242 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5243 long newsize; /* size of new block */ 5244 long oldsize; /* size of new block */ 5245 struct buf *bp; /* bp for allocated block */ 5246 { 5247 struct allocdirect *adp, *oldadp; 5248 struct allocdirectlst *adphead; 5249 struct freefrag *freefrag; 5250 struct inodedep *inodedep; 5251 struct pagedep *pagedep; 5252 struct jnewblk *jnewblk; 5253 struct newblk *newblk; 5254 struct mount *mp; 5255 ufs_lbn_t lbn; 5256 5257 lbn = bp->b_lblkno; 5258 mp = UFSTOVFS(ip->i_ump); 5259 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5260 ("softdep_setup_allocdirect called on non-softdep filesystem")); 5261 if (oldblkno && oldblkno != newblkno) 5262 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5263 else 5264 freefrag = NULL; 5265 5266 CTR6(KTR_SUJ, 5267 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5268 "off %jd newsize %ld oldsize %d", 5269 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5270 ACQUIRE_LOCK(ip->i_ump); 5271 if (off >= NDADDR) { 5272 if (lbn > 0) 5273 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5274 lbn, off); 5275 /* allocating an indirect block */ 5276 if (oldblkno != 0) 5277 panic("softdep_setup_allocdirect: non-zero indir"); 5278 } else { 5279 if (off != lbn) 5280 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5281 lbn, off); 5282 /* 5283 * Allocating a direct block. 5284 * 5285 * If we are allocating a directory block, then we must 5286 * allocate an associated pagedep to track additions and 5287 * deletions. 5288 */ 5289 if ((ip->i_mode & IFMT) == IFDIR) 5290 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5291 &pagedep); 5292 } 5293 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5294 panic("softdep_setup_allocdirect: lost block"); 5295 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5296 ("softdep_setup_allocdirect: newblk already initialized")); 5297 /* 5298 * Convert the newblk to an allocdirect. 5299 */ 5300 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5301 adp = (struct allocdirect *)newblk; 5302 newblk->nb_freefrag = freefrag; 5303 adp->ad_offset = off; 5304 adp->ad_oldblkno = oldblkno; 5305 adp->ad_newsize = newsize; 5306 adp->ad_oldsize = oldsize; 5307 5308 /* 5309 * Finish initializing the journal. 5310 */ 5311 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5312 jnewblk->jn_ino = ip->i_number; 5313 jnewblk->jn_lbn = lbn; 5314 add_to_journal(&jnewblk->jn_list); 5315 } 5316 if (freefrag && freefrag->ff_jdep != NULL && 5317 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5318 add_to_journal(freefrag->ff_jdep); 5319 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5320 adp->ad_inodedep = inodedep; 5321 5322 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5323 /* 5324 * The list of allocdirects must be kept in sorted and ascending 5325 * order so that the rollback routines can quickly determine the 5326 * first uncommitted block (the size of the file stored on disk 5327 * ends at the end of the lowest committed fragment, or if there 5328 * are no fragments, at the end of the highest committed block). 5329 * Since files generally grow, the typical case is that the new 5330 * block is to be added at the end of the list. We speed this 5331 * special case by checking against the last allocdirect in the 5332 * list before laboriously traversing the list looking for the 5333 * insertion point. 5334 */ 5335 adphead = &inodedep->id_newinoupdt; 5336 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5337 if (oldadp == NULL || oldadp->ad_offset <= off) { 5338 /* insert at end of list */ 5339 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5340 if (oldadp != NULL && oldadp->ad_offset == off) 5341 allocdirect_merge(adphead, adp, oldadp); 5342 FREE_LOCK(ip->i_ump); 5343 return; 5344 } 5345 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5346 if (oldadp->ad_offset >= off) 5347 break; 5348 } 5349 if (oldadp == NULL) 5350 panic("softdep_setup_allocdirect: lost entry"); 5351 /* insert in middle of list */ 5352 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5353 if (oldadp->ad_offset == off) 5354 allocdirect_merge(adphead, adp, oldadp); 5355 5356 FREE_LOCK(ip->i_ump); 5357 } 5358 5359 /* 5360 * Merge a newer and older journal record to be stored either in a 5361 * newblock or freefrag. This handles aggregating journal records for 5362 * fragment allocation into a second record as well as replacing a 5363 * journal free with an aborted journal allocation. A segment for the 5364 * oldest record will be placed on wkhd if it has been written. If not 5365 * the segment for the newer record will suffice. 5366 */ 5367 static struct worklist * 5368 jnewblk_merge(new, old, wkhd) 5369 struct worklist *new; 5370 struct worklist *old; 5371 struct workhead *wkhd; 5372 { 5373 struct jnewblk *njnewblk; 5374 struct jnewblk *jnewblk; 5375 5376 /* Handle NULLs to simplify callers. */ 5377 if (new == NULL) 5378 return (old); 5379 if (old == NULL) 5380 return (new); 5381 /* Replace a jfreefrag with a jnewblk. */ 5382 if (new->wk_type == D_JFREEFRAG) { 5383 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5384 panic("jnewblk_merge: blkno mismatch: %p, %p", 5385 old, new); 5386 cancel_jfreefrag(WK_JFREEFRAG(new)); 5387 return (old); 5388 } 5389 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5390 panic("jnewblk_merge: Bad type: old %d new %d\n", 5391 old->wk_type, new->wk_type); 5392 /* 5393 * Handle merging of two jnewblk records that describe 5394 * different sets of fragments in the same block. 5395 */ 5396 jnewblk = WK_JNEWBLK(old); 5397 njnewblk = WK_JNEWBLK(new); 5398 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5399 panic("jnewblk_merge: Merging disparate blocks."); 5400 /* 5401 * The record may be rolled back in the cg. 5402 */ 5403 if (jnewblk->jn_state & UNDONE) { 5404 jnewblk->jn_state &= ~UNDONE; 5405 njnewblk->jn_state |= UNDONE; 5406 njnewblk->jn_state &= ~ATTACHED; 5407 } 5408 /* 5409 * We modify the newer addref and free the older so that if neither 5410 * has been written the most up-to-date copy will be on disk. If 5411 * both have been written but rolled back we only temporarily need 5412 * one of them to fix the bits when the cg write completes. 5413 */ 5414 jnewblk->jn_state |= ATTACHED | COMPLETE; 5415 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5416 cancel_jnewblk(jnewblk, wkhd); 5417 WORKLIST_REMOVE(&jnewblk->jn_list); 5418 free_jnewblk(jnewblk); 5419 return (new); 5420 } 5421 5422 /* 5423 * Replace an old allocdirect dependency with a newer one. 5424 * This routine must be called with splbio interrupts blocked. 5425 */ 5426 static void 5427 allocdirect_merge(adphead, newadp, oldadp) 5428 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5429 struct allocdirect *newadp; /* allocdirect being added */ 5430 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5431 { 5432 struct worklist *wk; 5433 struct freefrag *freefrag; 5434 5435 freefrag = NULL; 5436 LOCK_OWNED(VFSTOUFS(newadp->ad_list.wk_mp)); 5437 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5438 newadp->ad_oldsize != oldadp->ad_newsize || 5439 newadp->ad_offset >= NDADDR) 5440 panic("%s %jd != new %jd || old size %ld != new %ld", 5441 "allocdirect_merge: old blkno", 5442 (intmax_t)newadp->ad_oldblkno, 5443 (intmax_t)oldadp->ad_newblkno, 5444 newadp->ad_oldsize, oldadp->ad_newsize); 5445 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5446 newadp->ad_oldsize = oldadp->ad_oldsize; 5447 /* 5448 * If the old dependency had a fragment to free or had never 5449 * previously had a block allocated, then the new dependency 5450 * can immediately post its freefrag and adopt the old freefrag. 5451 * This action is done by swapping the freefrag dependencies. 5452 * The new dependency gains the old one's freefrag, and the 5453 * old one gets the new one and then immediately puts it on 5454 * the worklist when it is freed by free_newblk. It is 5455 * not possible to do this swap when the old dependency had a 5456 * non-zero size but no previous fragment to free. This condition 5457 * arises when the new block is an extension of the old block. 5458 * Here, the first part of the fragment allocated to the new 5459 * dependency is part of the block currently claimed on disk by 5460 * the old dependency, so cannot legitimately be freed until the 5461 * conditions for the new dependency are fulfilled. 5462 */ 5463 freefrag = newadp->ad_freefrag; 5464 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5465 newadp->ad_freefrag = oldadp->ad_freefrag; 5466 oldadp->ad_freefrag = freefrag; 5467 } 5468 /* 5469 * If we are tracking a new directory-block allocation, 5470 * move it from the old allocdirect to the new allocdirect. 5471 */ 5472 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5473 WORKLIST_REMOVE(wk); 5474 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5475 panic("allocdirect_merge: extra newdirblk"); 5476 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5477 } 5478 TAILQ_REMOVE(adphead, oldadp, ad_next); 5479 /* 5480 * We need to move any journal dependencies over to the freefrag 5481 * that releases this block if it exists. Otherwise we are 5482 * extending an existing block and we'll wait until that is 5483 * complete to release the journal space and extend the 5484 * new journal to cover this old space as well. 5485 */ 5486 if (freefrag == NULL) { 5487 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5488 panic("allocdirect_merge: %jd != %jd", 5489 oldadp->ad_newblkno, newadp->ad_newblkno); 5490 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5491 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5492 &oldadp->ad_block.nb_jnewblk->jn_list, 5493 &newadp->ad_block.nb_jwork); 5494 oldadp->ad_block.nb_jnewblk = NULL; 5495 cancel_newblk(&oldadp->ad_block, NULL, 5496 &newadp->ad_block.nb_jwork); 5497 } else { 5498 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5499 &freefrag->ff_list, &freefrag->ff_jwork); 5500 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5501 &freefrag->ff_jwork); 5502 } 5503 free_newblk(&oldadp->ad_block); 5504 } 5505 5506 /* 5507 * Allocate a jfreefrag structure to journal a single block free. 5508 */ 5509 static struct jfreefrag * 5510 newjfreefrag(freefrag, ip, blkno, size, lbn) 5511 struct freefrag *freefrag; 5512 struct inode *ip; 5513 ufs2_daddr_t blkno; 5514 long size; 5515 ufs_lbn_t lbn; 5516 { 5517 struct jfreefrag *jfreefrag; 5518 struct fs *fs; 5519 5520 fs = ip->i_fs; 5521 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5522 M_SOFTDEP_FLAGS); 5523 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump)); 5524 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5525 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5526 jfreefrag->fr_ino = ip->i_number; 5527 jfreefrag->fr_lbn = lbn; 5528 jfreefrag->fr_blkno = blkno; 5529 jfreefrag->fr_frags = numfrags(fs, size); 5530 jfreefrag->fr_freefrag = freefrag; 5531 5532 return (jfreefrag); 5533 } 5534 5535 /* 5536 * Allocate a new freefrag structure. 5537 */ 5538 static struct freefrag * 5539 newfreefrag(ip, blkno, size, lbn) 5540 struct inode *ip; 5541 ufs2_daddr_t blkno; 5542 long size; 5543 ufs_lbn_t lbn; 5544 { 5545 struct freefrag *freefrag; 5546 struct fs *fs; 5547 5548 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5549 ip->i_number, blkno, size, lbn); 5550 fs = ip->i_fs; 5551 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5552 panic("newfreefrag: frag size"); 5553 freefrag = malloc(sizeof(struct freefrag), 5554 M_FREEFRAG, M_SOFTDEP_FLAGS); 5555 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 5556 freefrag->ff_state = ATTACHED; 5557 LIST_INIT(&freefrag->ff_jwork); 5558 freefrag->ff_inum = ip->i_number; 5559 freefrag->ff_vtype = ITOV(ip)->v_type; 5560 freefrag->ff_blkno = blkno; 5561 freefrag->ff_fragsize = size; 5562 5563 if (MOUNTEDSUJ(UFSTOVFS(ip->i_ump))) { 5564 freefrag->ff_jdep = (struct worklist *) 5565 newjfreefrag(freefrag, ip, blkno, size, lbn); 5566 } else { 5567 freefrag->ff_state |= DEPCOMPLETE; 5568 freefrag->ff_jdep = NULL; 5569 } 5570 5571 return (freefrag); 5572 } 5573 5574 /* 5575 * This workitem de-allocates fragments that were replaced during 5576 * file block allocation. 5577 */ 5578 static void 5579 handle_workitem_freefrag(freefrag) 5580 struct freefrag *freefrag; 5581 { 5582 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5583 struct workhead wkhd; 5584 5585 CTR3(KTR_SUJ, 5586 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5587 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5588 /* 5589 * It would be illegal to add new completion items to the 5590 * freefrag after it was schedule to be done so it must be 5591 * safe to modify the list head here. 5592 */ 5593 LIST_INIT(&wkhd); 5594 ACQUIRE_LOCK(ump); 5595 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5596 /* 5597 * If the journal has not been written we must cancel it here. 5598 */ 5599 if (freefrag->ff_jdep) { 5600 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5601 panic("handle_workitem_freefrag: Unexpected type %d\n", 5602 freefrag->ff_jdep->wk_type); 5603 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5604 } 5605 FREE_LOCK(ump); 5606 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5607 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd); 5608 ACQUIRE_LOCK(ump); 5609 WORKITEM_FREE(freefrag, D_FREEFRAG); 5610 FREE_LOCK(ump); 5611 } 5612 5613 /* 5614 * Set up a dependency structure for an external attributes data block. 5615 * This routine follows much of the structure of softdep_setup_allocdirect. 5616 * See the description of softdep_setup_allocdirect above for details. 5617 */ 5618 void 5619 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5620 struct inode *ip; 5621 ufs_lbn_t off; 5622 ufs2_daddr_t newblkno; 5623 ufs2_daddr_t oldblkno; 5624 long newsize; 5625 long oldsize; 5626 struct buf *bp; 5627 { 5628 struct allocdirect *adp, *oldadp; 5629 struct allocdirectlst *adphead; 5630 struct freefrag *freefrag; 5631 struct inodedep *inodedep; 5632 struct jnewblk *jnewblk; 5633 struct newblk *newblk; 5634 struct mount *mp; 5635 ufs_lbn_t lbn; 5636 5637 mp = UFSTOVFS(ip->i_ump); 5638 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5639 ("softdep_setup_allocext called on non-softdep filesystem")); 5640 KASSERT(off < NXADDR, ("softdep_setup_allocext: lbn %lld > NXADDR", 5641 (long long)off)); 5642 5643 lbn = bp->b_lblkno; 5644 if (oldblkno && oldblkno != newblkno) 5645 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5646 else 5647 freefrag = NULL; 5648 5649 ACQUIRE_LOCK(ip->i_ump); 5650 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5651 panic("softdep_setup_allocext: lost block"); 5652 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5653 ("softdep_setup_allocext: newblk already initialized")); 5654 /* 5655 * Convert the newblk to an allocdirect. 5656 */ 5657 WORKITEM_REASSIGN(newblk, D_ALLOCDIRECT); 5658 adp = (struct allocdirect *)newblk; 5659 newblk->nb_freefrag = freefrag; 5660 adp->ad_offset = off; 5661 adp->ad_oldblkno = oldblkno; 5662 adp->ad_newsize = newsize; 5663 adp->ad_oldsize = oldsize; 5664 adp->ad_state |= EXTDATA; 5665 5666 /* 5667 * Finish initializing the journal. 5668 */ 5669 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5670 jnewblk->jn_ino = ip->i_number; 5671 jnewblk->jn_lbn = lbn; 5672 add_to_journal(&jnewblk->jn_list); 5673 } 5674 if (freefrag && freefrag->ff_jdep != NULL && 5675 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5676 add_to_journal(freefrag->ff_jdep); 5677 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5678 adp->ad_inodedep = inodedep; 5679 5680 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5681 /* 5682 * The list of allocdirects must be kept in sorted and ascending 5683 * order so that the rollback routines can quickly determine the 5684 * first uncommitted block (the size of the file stored on disk 5685 * ends at the end of the lowest committed fragment, or if there 5686 * are no fragments, at the end of the highest committed block). 5687 * Since files generally grow, the typical case is that the new 5688 * block is to be added at the end of the list. We speed this 5689 * special case by checking against the last allocdirect in the 5690 * list before laboriously traversing the list looking for the 5691 * insertion point. 5692 */ 5693 adphead = &inodedep->id_newextupdt; 5694 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5695 if (oldadp == NULL || oldadp->ad_offset <= off) { 5696 /* insert at end of list */ 5697 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5698 if (oldadp != NULL && oldadp->ad_offset == off) 5699 allocdirect_merge(adphead, adp, oldadp); 5700 FREE_LOCK(ip->i_ump); 5701 return; 5702 } 5703 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5704 if (oldadp->ad_offset >= off) 5705 break; 5706 } 5707 if (oldadp == NULL) 5708 panic("softdep_setup_allocext: lost entry"); 5709 /* insert in middle of list */ 5710 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5711 if (oldadp->ad_offset == off) 5712 allocdirect_merge(adphead, adp, oldadp); 5713 FREE_LOCK(ip->i_ump); 5714 } 5715 5716 /* 5717 * Indirect block allocation dependencies. 5718 * 5719 * The same dependencies that exist for a direct block also exist when 5720 * a new block is allocated and pointed to by an entry in a block of 5721 * indirect pointers. The undo/redo states described above are also 5722 * used here. Because an indirect block contains many pointers that 5723 * may have dependencies, a second copy of the entire in-memory indirect 5724 * block is kept. The buffer cache copy is always completely up-to-date. 5725 * The second copy, which is used only as a source for disk writes, 5726 * contains only the safe pointers (i.e., those that have no remaining 5727 * update dependencies). The second copy is freed when all pointers 5728 * are safe. The cache is not allowed to replace indirect blocks with 5729 * pending update dependencies. If a buffer containing an indirect 5730 * block with dependencies is written, these routines will mark it 5731 * dirty again. It can only be successfully written once all the 5732 * dependencies are removed. The ffs_fsync routine in conjunction with 5733 * softdep_sync_metadata work together to get all the dependencies 5734 * removed so that a file can be successfully written to disk. Three 5735 * procedures are used when setting up indirect block pointer 5736 * dependencies. The division is necessary because of the organization 5737 * of the "balloc" routine and because of the distinction between file 5738 * pages and file metadata blocks. 5739 */ 5740 5741 /* 5742 * Allocate a new allocindir structure. 5743 */ 5744 static struct allocindir * 5745 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5746 struct inode *ip; /* inode for file being extended */ 5747 int ptrno; /* offset of pointer in indirect block */ 5748 ufs2_daddr_t newblkno; /* disk block number being added */ 5749 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5750 ufs_lbn_t lbn; 5751 { 5752 struct newblk *newblk; 5753 struct allocindir *aip; 5754 struct freefrag *freefrag; 5755 struct jnewblk *jnewblk; 5756 5757 if (oldblkno) 5758 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn); 5759 else 5760 freefrag = NULL; 5761 ACQUIRE_LOCK(ip->i_ump); 5762 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0) 5763 panic("new_allocindir: lost block"); 5764 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5765 ("newallocindir: newblk already initialized")); 5766 WORKITEM_REASSIGN(newblk, D_ALLOCINDIR); 5767 newblk->nb_freefrag = freefrag; 5768 aip = (struct allocindir *)newblk; 5769 aip->ai_offset = ptrno; 5770 aip->ai_oldblkno = oldblkno; 5771 aip->ai_lbn = lbn; 5772 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5773 jnewblk->jn_ino = ip->i_number; 5774 jnewblk->jn_lbn = lbn; 5775 add_to_journal(&jnewblk->jn_list); 5776 } 5777 if (freefrag && freefrag->ff_jdep != NULL && 5778 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5779 add_to_journal(freefrag->ff_jdep); 5780 return (aip); 5781 } 5782 5783 /* 5784 * Called just before setting an indirect block pointer 5785 * to a newly allocated file page. 5786 */ 5787 void 5788 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5789 struct inode *ip; /* inode for file being extended */ 5790 ufs_lbn_t lbn; /* allocated block number within file */ 5791 struct buf *bp; /* buffer with indirect blk referencing page */ 5792 int ptrno; /* offset of pointer in indirect block */ 5793 ufs2_daddr_t newblkno; /* disk block number being added */ 5794 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5795 struct buf *nbp; /* buffer holding allocated page */ 5796 { 5797 struct inodedep *inodedep; 5798 struct freefrag *freefrag; 5799 struct allocindir *aip; 5800 struct pagedep *pagedep; 5801 struct mount *mp; 5802 int dflags; 5803 5804 mp = UFSTOVFS(ip->i_ump); 5805 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 5806 ("softdep_setup_allocindir_page called on non-softdep filesystem")); 5807 KASSERT(lbn == nbp->b_lblkno, 5808 ("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5809 lbn, bp->b_lblkno)); 5810 CTR4(KTR_SUJ, 5811 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5812 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5813 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5814 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5815 dflags = DEPALLOC; 5816 if (IS_SNAPSHOT(ip)) 5817 dflags |= NODELAY; 5818 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 5819 /* 5820 * If we are allocating a directory page, then we must 5821 * allocate an associated pagedep to track additions and 5822 * deletions. 5823 */ 5824 if ((ip->i_mode & IFMT) == IFDIR) 5825 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5826 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5827 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5828 FREE_LOCK(ip->i_ump); 5829 if (freefrag) 5830 handle_workitem_freefrag(freefrag); 5831 } 5832 5833 /* 5834 * Called just before setting an indirect block pointer to a 5835 * newly allocated indirect block. 5836 */ 5837 void 5838 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5839 struct buf *nbp; /* newly allocated indirect block */ 5840 struct inode *ip; /* inode for file being extended */ 5841 struct buf *bp; /* indirect block referencing allocated block */ 5842 int ptrno; /* offset of pointer in indirect block */ 5843 ufs2_daddr_t newblkno; /* disk block number being added */ 5844 { 5845 struct inodedep *inodedep; 5846 struct allocindir *aip; 5847 ufs_lbn_t lbn; 5848 int dflags; 5849 5850 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 5851 ("softdep_setup_allocindir_meta called on non-softdep filesystem")); 5852 CTR3(KTR_SUJ, 5853 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5854 ip->i_number, newblkno, ptrno); 5855 lbn = nbp->b_lblkno; 5856 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5857 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5858 dflags = DEPALLOC; 5859 if (IS_SNAPSHOT(ip)) 5860 dflags |= NODELAY; 5861 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 5862 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5863 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5864 panic("softdep_setup_allocindir_meta: Block already existed"); 5865 FREE_LOCK(ip->i_ump); 5866 } 5867 5868 static void 5869 indirdep_complete(indirdep) 5870 struct indirdep *indirdep; 5871 { 5872 struct allocindir *aip; 5873 5874 LIST_REMOVE(indirdep, ir_next); 5875 indirdep->ir_state |= DEPCOMPLETE; 5876 5877 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5878 LIST_REMOVE(aip, ai_next); 5879 free_newblk(&aip->ai_block); 5880 } 5881 /* 5882 * If this indirdep is not attached to a buf it was simply waiting 5883 * on completion to clear completehd. free_indirdep() asserts 5884 * that nothing is dangling. 5885 */ 5886 if ((indirdep->ir_state & ONWORKLIST) == 0) 5887 free_indirdep(indirdep); 5888 } 5889 5890 static struct indirdep * 5891 indirdep_lookup(mp, ip, bp) 5892 struct mount *mp; 5893 struct inode *ip; 5894 struct buf *bp; 5895 { 5896 struct indirdep *indirdep, *newindirdep; 5897 struct newblk *newblk; 5898 struct ufsmount *ump; 5899 struct worklist *wk; 5900 struct fs *fs; 5901 ufs2_daddr_t blkno; 5902 5903 ump = VFSTOUFS(mp); 5904 LOCK_OWNED(ump); 5905 indirdep = NULL; 5906 newindirdep = NULL; 5907 fs = ip->i_fs; 5908 for (;;) { 5909 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5910 if (wk->wk_type != D_INDIRDEP) 5911 continue; 5912 indirdep = WK_INDIRDEP(wk); 5913 break; 5914 } 5915 /* Found on the buffer worklist, no new structure to free. */ 5916 if (indirdep != NULL && newindirdep == NULL) 5917 return (indirdep); 5918 if (indirdep != NULL && newindirdep != NULL) 5919 panic("indirdep_lookup: simultaneous create"); 5920 /* None found on the buffer and a new structure is ready. */ 5921 if (indirdep == NULL && newindirdep != NULL) 5922 break; 5923 /* None found and no new structure available. */ 5924 FREE_LOCK(ump); 5925 newindirdep = malloc(sizeof(struct indirdep), 5926 M_INDIRDEP, M_SOFTDEP_FLAGS); 5927 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5928 newindirdep->ir_state = ATTACHED; 5929 if (ip->i_ump->um_fstype == UFS1) 5930 newindirdep->ir_state |= UFS1FMT; 5931 TAILQ_INIT(&newindirdep->ir_trunc); 5932 newindirdep->ir_saveddata = NULL; 5933 LIST_INIT(&newindirdep->ir_deplisthd); 5934 LIST_INIT(&newindirdep->ir_donehd); 5935 LIST_INIT(&newindirdep->ir_writehd); 5936 LIST_INIT(&newindirdep->ir_completehd); 5937 if (bp->b_blkno == bp->b_lblkno) { 5938 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5939 NULL, NULL); 5940 bp->b_blkno = blkno; 5941 } 5942 newindirdep->ir_freeblks = NULL; 5943 newindirdep->ir_savebp = 5944 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5945 newindirdep->ir_bp = bp; 5946 BUF_KERNPROC(newindirdep->ir_savebp); 5947 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5948 ACQUIRE_LOCK(ump); 5949 } 5950 indirdep = newindirdep; 5951 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 5952 /* 5953 * If the block is not yet allocated we don't set DEPCOMPLETE so 5954 * that we don't free dependencies until the pointers are valid. 5955 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 5956 * than using the hash. 5957 */ 5958 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 5959 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 5960 else 5961 indirdep->ir_state |= DEPCOMPLETE; 5962 return (indirdep); 5963 } 5964 5965 /* 5966 * Called to finish the allocation of the "aip" allocated 5967 * by one of the two routines above. 5968 */ 5969 static struct freefrag * 5970 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 5971 struct buf *bp; /* in-memory copy of the indirect block */ 5972 struct inode *ip; /* inode for file being extended */ 5973 struct inodedep *inodedep; /* Inodedep for ip */ 5974 struct allocindir *aip; /* allocindir allocated by the above routines */ 5975 ufs_lbn_t lbn; /* Logical block number for this block. */ 5976 { 5977 struct fs *fs; 5978 struct indirdep *indirdep; 5979 struct allocindir *oldaip; 5980 struct freefrag *freefrag; 5981 struct mount *mp; 5982 5983 LOCK_OWNED(ip->i_ump); 5984 mp = UFSTOVFS(ip->i_ump); 5985 fs = ip->i_fs; 5986 if (bp->b_lblkno >= 0) 5987 panic("setup_allocindir_phase2: not indir blk"); 5988 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 5989 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 5990 indirdep = indirdep_lookup(mp, ip, bp); 5991 KASSERT(indirdep->ir_savebp != NULL, 5992 ("setup_allocindir_phase2 NULL ir_savebp")); 5993 aip->ai_indirdep = indirdep; 5994 /* 5995 * Check for an unwritten dependency for this indirect offset. If 5996 * there is, merge the old dependency into the new one. This happens 5997 * as a result of reallocblk only. 5998 */ 5999 freefrag = NULL; 6000 if (aip->ai_oldblkno != 0) { 6001 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 6002 if (oldaip->ai_offset == aip->ai_offset) { 6003 freefrag = allocindir_merge(aip, oldaip); 6004 goto done; 6005 } 6006 } 6007 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 6008 if (oldaip->ai_offset == aip->ai_offset) { 6009 freefrag = allocindir_merge(aip, oldaip); 6010 goto done; 6011 } 6012 } 6013 } 6014 done: 6015 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 6016 return (freefrag); 6017 } 6018 6019 /* 6020 * Merge two allocindirs which refer to the same block. Move newblock 6021 * dependencies and setup the freefrags appropriately. 6022 */ 6023 static struct freefrag * 6024 allocindir_merge(aip, oldaip) 6025 struct allocindir *aip; 6026 struct allocindir *oldaip; 6027 { 6028 struct freefrag *freefrag; 6029 struct worklist *wk; 6030 6031 if (oldaip->ai_newblkno != aip->ai_oldblkno) 6032 panic("allocindir_merge: blkno"); 6033 aip->ai_oldblkno = oldaip->ai_oldblkno; 6034 freefrag = aip->ai_freefrag; 6035 aip->ai_freefrag = oldaip->ai_freefrag; 6036 oldaip->ai_freefrag = NULL; 6037 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 6038 /* 6039 * If we are tracking a new directory-block allocation, 6040 * move it from the old allocindir to the new allocindir. 6041 */ 6042 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 6043 WORKLIST_REMOVE(wk); 6044 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 6045 panic("allocindir_merge: extra newdirblk"); 6046 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 6047 } 6048 /* 6049 * We can skip journaling for this freefrag and just complete 6050 * any pending journal work for the allocindir that is being 6051 * removed after the freefrag completes. 6052 */ 6053 if (freefrag->ff_jdep) 6054 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 6055 LIST_REMOVE(oldaip, ai_next); 6056 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 6057 &freefrag->ff_list, &freefrag->ff_jwork); 6058 free_newblk(&oldaip->ai_block); 6059 6060 return (freefrag); 6061 } 6062 6063 static inline void 6064 setup_freedirect(freeblks, ip, i, needj) 6065 struct freeblks *freeblks; 6066 struct inode *ip; 6067 int i; 6068 int needj; 6069 { 6070 ufs2_daddr_t blkno; 6071 int frags; 6072 6073 blkno = DIP(ip, i_db[i]); 6074 if (blkno == 0) 6075 return; 6076 DIP_SET(ip, i_db[i], 0); 6077 frags = sblksize(ip->i_fs, ip->i_size, i); 6078 frags = numfrags(ip->i_fs, frags); 6079 newfreework(ip->i_ump, freeblks, NULL, i, blkno, frags, 0, needj); 6080 } 6081 6082 static inline void 6083 setup_freeext(freeblks, ip, i, needj) 6084 struct freeblks *freeblks; 6085 struct inode *ip; 6086 int i; 6087 int needj; 6088 { 6089 ufs2_daddr_t blkno; 6090 int frags; 6091 6092 blkno = ip->i_din2->di_extb[i]; 6093 if (blkno == 0) 6094 return; 6095 ip->i_din2->di_extb[i] = 0; 6096 frags = sblksize(ip->i_fs, ip->i_din2->di_extsize, i); 6097 frags = numfrags(ip->i_fs, frags); 6098 newfreework(ip->i_ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 6099 } 6100 6101 static inline void 6102 setup_freeindir(freeblks, ip, i, lbn, needj) 6103 struct freeblks *freeblks; 6104 struct inode *ip; 6105 int i; 6106 ufs_lbn_t lbn; 6107 int needj; 6108 { 6109 ufs2_daddr_t blkno; 6110 6111 blkno = DIP(ip, i_ib[i]); 6112 if (blkno == 0) 6113 return; 6114 DIP_SET(ip, i_ib[i], 0); 6115 newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, ip->i_fs->fs_frag, 6116 0, needj); 6117 } 6118 6119 static inline struct freeblks * 6120 newfreeblks(mp, ip) 6121 struct mount *mp; 6122 struct inode *ip; 6123 { 6124 struct freeblks *freeblks; 6125 6126 freeblks = malloc(sizeof(struct freeblks), 6127 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 6128 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 6129 LIST_INIT(&freeblks->fb_jblkdephd); 6130 LIST_INIT(&freeblks->fb_jwork); 6131 freeblks->fb_ref = 0; 6132 freeblks->fb_cgwait = 0; 6133 freeblks->fb_state = ATTACHED; 6134 freeblks->fb_uid = ip->i_uid; 6135 freeblks->fb_inum = ip->i_number; 6136 freeblks->fb_vtype = ITOV(ip)->v_type; 6137 freeblks->fb_modrev = DIP(ip, i_modrev); 6138 freeblks->fb_devvp = ip->i_devvp; 6139 freeblks->fb_chkcnt = 0; 6140 freeblks->fb_len = 0; 6141 6142 return (freeblks); 6143 } 6144 6145 static void 6146 trunc_indirdep(indirdep, freeblks, bp, off) 6147 struct indirdep *indirdep; 6148 struct freeblks *freeblks; 6149 struct buf *bp; 6150 int off; 6151 { 6152 struct allocindir *aip, *aipn; 6153 6154 /* 6155 * The first set of allocindirs won't be in savedbp. 6156 */ 6157 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 6158 if (aip->ai_offset > off) 6159 cancel_allocindir(aip, bp, freeblks, 1); 6160 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 6161 if (aip->ai_offset > off) 6162 cancel_allocindir(aip, bp, freeblks, 1); 6163 /* 6164 * These will exist in savedbp. 6165 */ 6166 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 6167 if (aip->ai_offset > off) 6168 cancel_allocindir(aip, NULL, freeblks, 0); 6169 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 6170 if (aip->ai_offset > off) 6171 cancel_allocindir(aip, NULL, freeblks, 0); 6172 } 6173 6174 /* 6175 * Follow the chain of indirects down to lastlbn creating a freework 6176 * structure for each. This will be used to start indir_trunc() at 6177 * the right offset and create the journal records for the parrtial 6178 * truncation. A second step will handle the truncated dependencies. 6179 */ 6180 static int 6181 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6182 struct freeblks *freeblks; 6183 struct inode *ip; 6184 ufs_lbn_t lbn; 6185 ufs_lbn_t lastlbn; 6186 ufs2_daddr_t blkno; 6187 { 6188 struct indirdep *indirdep; 6189 struct indirdep *indirn; 6190 struct freework *freework; 6191 struct newblk *newblk; 6192 struct mount *mp; 6193 struct buf *bp; 6194 uint8_t *start; 6195 uint8_t *end; 6196 ufs_lbn_t lbnadd; 6197 int level; 6198 int error; 6199 int off; 6200 6201 6202 freework = NULL; 6203 if (blkno == 0) 6204 return (0); 6205 mp = freeblks->fb_list.wk_mp; 6206 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6207 if ((bp->b_flags & B_CACHE) == 0) { 6208 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6209 bp->b_iocmd = BIO_READ; 6210 bp->b_flags &= ~B_INVAL; 6211 bp->b_ioflags &= ~BIO_ERROR; 6212 vfs_busy_pages(bp, 0); 6213 bp->b_iooffset = dbtob(bp->b_blkno); 6214 bstrategy(bp); 6215 curthread->td_ru.ru_inblock++; 6216 error = bufwait(bp); 6217 if (error) { 6218 brelse(bp); 6219 return (error); 6220 } 6221 } 6222 level = lbn_level(lbn); 6223 lbnadd = lbn_offset(ip->i_fs, level); 6224 /* 6225 * Compute the offset of the last block we want to keep. Store 6226 * in the freework the first block we want to completely free. 6227 */ 6228 off = (lastlbn - -(lbn + level)) / lbnadd; 6229 if (off + 1 == NINDIR(ip->i_fs)) 6230 goto nowork; 6231 freework = newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, 0, off+1, 6232 0); 6233 /* 6234 * Link the freework into the indirdep. This will prevent any new 6235 * allocations from proceeding until we are finished with the 6236 * truncate and the block is written. 6237 */ 6238 ACQUIRE_LOCK(ip->i_ump); 6239 indirdep = indirdep_lookup(mp, ip, bp); 6240 if (indirdep->ir_freeblks) 6241 panic("setup_trunc_indir: indirdep already truncated."); 6242 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6243 freework->fw_indir = indirdep; 6244 /* 6245 * Cancel any allocindirs that will not make it to disk. 6246 * We have to do this for all copies of the indirdep that 6247 * live on this newblk. 6248 */ 6249 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6250 newblk_lookup(mp, dbtofsb(ip->i_fs, bp->b_blkno), 0, &newblk); 6251 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6252 trunc_indirdep(indirn, freeblks, bp, off); 6253 } else 6254 trunc_indirdep(indirdep, freeblks, bp, off); 6255 FREE_LOCK(ip->i_ump); 6256 /* 6257 * Creation is protected by the buf lock. The saveddata is only 6258 * needed if a full truncation follows a partial truncation but it 6259 * is difficult to allocate in that case so we fetch it anyway. 6260 */ 6261 if (indirdep->ir_saveddata == NULL) 6262 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6263 M_SOFTDEP_FLAGS); 6264 nowork: 6265 /* Fetch the blkno of the child and the zero start offset. */ 6266 if (ip->i_ump->um_fstype == UFS1) { 6267 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6268 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6269 } else { 6270 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6271 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6272 } 6273 if (freework) { 6274 /* Zero the truncated pointers. */ 6275 end = bp->b_data + bp->b_bcount; 6276 bzero(start, end - start); 6277 bdwrite(bp); 6278 } else 6279 bqrelse(bp); 6280 if (level == 0) 6281 return (0); 6282 lbn++; /* adjust level */ 6283 lbn -= (off * lbnadd); 6284 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6285 } 6286 6287 /* 6288 * Complete the partial truncation of an indirect block setup by 6289 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6290 * copy and writes them to disk before the freeblks is allowed to complete. 6291 */ 6292 static void 6293 complete_trunc_indir(freework) 6294 struct freework *freework; 6295 { 6296 struct freework *fwn; 6297 struct indirdep *indirdep; 6298 struct ufsmount *ump; 6299 struct buf *bp; 6300 uintptr_t start; 6301 int count; 6302 6303 ump = VFSTOUFS(freework->fw_list.wk_mp); 6304 LOCK_OWNED(ump); 6305 indirdep = freework->fw_indir; 6306 for (;;) { 6307 bp = indirdep->ir_bp; 6308 /* See if the block was discarded. */ 6309 if (bp == NULL) 6310 break; 6311 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6312 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6313 break; 6314 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6315 LOCK_PTR(ump)) == 0) 6316 BUF_UNLOCK(bp); 6317 ACQUIRE_LOCK(ump); 6318 } 6319 freework->fw_state |= DEPCOMPLETE; 6320 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6321 /* 6322 * Zero the pointers in the saved copy. 6323 */ 6324 if (indirdep->ir_state & UFS1FMT) 6325 start = sizeof(ufs1_daddr_t); 6326 else 6327 start = sizeof(ufs2_daddr_t); 6328 start *= freework->fw_start; 6329 count = indirdep->ir_savebp->b_bcount - start; 6330 start += (uintptr_t)indirdep->ir_savebp->b_data; 6331 bzero((char *)start, count); 6332 /* 6333 * We need to start the next truncation in the list if it has not 6334 * been started yet. 6335 */ 6336 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6337 if (fwn != NULL) { 6338 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6339 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6340 if ((fwn->fw_state & ONWORKLIST) == 0) 6341 freework_enqueue(fwn); 6342 } 6343 /* 6344 * If bp is NULL the block was fully truncated, restore 6345 * the saved block list otherwise free it if it is no 6346 * longer needed. 6347 */ 6348 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6349 if (bp == NULL) 6350 bcopy(indirdep->ir_saveddata, 6351 indirdep->ir_savebp->b_data, 6352 indirdep->ir_savebp->b_bcount); 6353 free(indirdep->ir_saveddata, M_INDIRDEP); 6354 indirdep->ir_saveddata = NULL; 6355 } 6356 /* 6357 * When bp is NULL there is a full truncation pending. We 6358 * must wait for this full truncation to be journaled before 6359 * we can release this freework because the disk pointers will 6360 * never be written as zero. 6361 */ 6362 if (bp == NULL) { 6363 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6364 handle_written_freework(freework); 6365 else 6366 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6367 &freework->fw_list); 6368 } else { 6369 /* Complete when the real copy is written. */ 6370 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6371 BUF_UNLOCK(bp); 6372 } 6373 } 6374 6375 /* 6376 * Calculate the number of blocks we are going to release where datablocks 6377 * is the current total and length is the new file size. 6378 */ 6379 static ufs2_daddr_t 6380 blkcount(fs, datablocks, length) 6381 struct fs *fs; 6382 ufs2_daddr_t datablocks; 6383 off_t length; 6384 { 6385 off_t totblks, numblks; 6386 6387 totblks = 0; 6388 numblks = howmany(length, fs->fs_bsize); 6389 if (numblks <= NDADDR) { 6390 totblks = howmany(length, fs->fs_fsize); 6391 goto out; 6392 } 6393 totblks = blkstofrags(fs, numblks); 6394 numblks -= NDADDR; 6395 /* 6396 * Count all single, then double, then triple indirects required. 6397 * Subtracting one indirects worth of blocks for each pass 6398 * acknowledges one of each pointed to by the inode. 6399 */ 6400 for (;;) { 6401 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6402 numblks -= NINDIR(fs); 6403 if (numblks <= 0) 6404 break; 6405 numblks = howmany(numblks, NINDIR(fs)); 6406 } 6407 out: 6408 totblks = fsbtodb(fs, totblks); 6409 /* 6410 * Handle sparse files. We can't reclaim more blocks than the inode 6411 * references. We will correct it later in handle_complete_freeblks() 6412 * when we know the real count. 6413 */ 6414 if (totblks > datablocks) 6415 return (0); 6416 return (datablocks - totblks); 6417 } 6418 6419 /* 6420 * Handle freeblocks for journaled softupdate filesystems. 6421 * 6422 * Contrary to normal softupdates, we must preserve the block pointers in 6423 * indirects until their subordinates are free. This is to avoid journaling 6424 * every block that is freed which may consume more space than the journal 6425 * itself. The recovery program will see the free block journals at the 6426 * base of the truncated area and traverse them to reclaim space. The 6427 * pointers in the inode may be cleared immediately after the journal 6428 * records are written because each direct and indirect pointer in the 6429 * inode is recorded in a journal. This permits full truncation to proceed 6430 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6431 * 6432 * The algorithm is as follows: 6433 * 1) Traverse the in-memory state and create journal entries to release 6434 * the relevant blocks and full indirect trees. 6435 * 2) Traverse the indirect block chain adding partial truncation freework 6436 * records to indirects in the path to lastlbn. The freework will 6437 * prevent new allocation dependencies from being satisfied in this 6438 * indirect until the truncation completes. 6439 * 3) Read and lock the inode block, performing an update with the new size 6440 * and pointers. This prevents truncated data from becoming valid on 6441 * disk through step 4. 6442 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6443 * eliminate journal work for those records that do not require it. 6444 * 5) Schedule the journal records to be written followed by the inode block. 6445 * 6) Allocate any necessary frags for the end of file. 6446 * 7) Zero any partially truncated blocks. 6447 * 6448 * From this truncation proceeds asynchronously using the freework and 6449 * indir_trunc machinery. The file will not be extended again into a 6450 * partially truncated indirect block until all work is completed but 6451 * the normal dependency mechanism ensures that it is rolled back/forward 6452 * as appropriate. Further truncation may occur without delay and is 6453 * serialized in indir_trunc(). 6454 */ 6455 void 6456 softdep_journal_freeblocks(ip, cred, length, flags) 6457 struct inode *ip; /* The inode whose length is to be reduced */ 6458 struct ucred *cred; 6459 off_t length; /* The new length for the file */ 6460 int flags; /* IO_EXT and/or IO_NORMAL */ 6461 { 6462 struct freeblks *freeblks, *fbn; 6463 struct worklist *wk, *wkn; 6464 struct inodedep *inodedep; 6465 struct jblkdep *jblkdep; 6466 struct allocdirect *adp, *adpn; 6467 struct ufsmount *ump; 6468 struct fs *fs; 6469 struct buf *bp; 6470 struct vnode *vp; 6471 struct mount *mp; 6472 ufs2_daddr_t extblocks, datablocks; 6473 ufs_lbn_t tmpval, lbn, lastlbn; 6474 int frags, lastoff, iboff, allocblock, needj, dflags, error, i; 6475 6476 fs = ip->i_fs; 6477 ump = ip->i_ump; 6478 mp = UFSTOVFS(ump); 6479 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6480 ("softdep_journal_freeblocks called on non-softdep filesystem")); 6481 vp = ITOV(ip); 6482 needj = 1; 6483 iboff = -1; 6484 allocblock = 0; 6485 extblocks = 0; 6486 datablocks = 0; 6487 frags = 0; 6488 freeblks = newfreeblks(mp, ip); 6489 ACQUIRE_LOCK(ump); 6490 /* 6491 * If we're truncating a removed file that will never be written 6492 * we don't need to journal the block frees. The canceled journals 6493 * for the allocations will suffice. 6494 */ 6495 dflags = DEPALLOC; 6496 if (IS_SNAPSHOT(ip)) 6497 dflags |= NODELAY; 6498 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6499 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6500 length == 0) 6501 needj = 0; 6502 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6503 ip->i_number, length, needj); 6504 FREE_LOCK(ump); 6505 /* 6506 * Calculate the lbn that we are truncating to. This results in -1 6507 * if we're truncating the 0 bytes. So it is the last lbn we want 6508 * to keep, not the first lbn we want to truncate. 6509 */ 6510 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6511 lastoff = blkoff(fs, length); 6512 /* 6513 * Compute frags we are keeping in lastlbn. 0 means all. 6514 */ 6515 if (lastlbn >= 0 && lastlbn < NDADDR) { 6516 frags = fragroundup(fs, lastoff); 6517 /* adp offset of last valid allocdirect. */ 6518 iboff = lastlbn; 6519 } else if (lastlbn > 0) 6520 iboff = NDADDR; 6521 if (fs->fs_magic == FS_UFS2_MAGIC) 6522 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6523 /* 6524 * Handle normal data blocks and indirects. This section saves 6525 * values used after the inode update to complete frag and indirect 6526 * truncation. 6527 */ 6528 if ((flags & IO_NORMAL) != 0) { 6529 /* 6530 * Handle truncation of whole direct and indirect blocks. 6531 */ 6532 for (i = iboff + 1; i < NDADDR; i++) 6533 setup_freedirect(freeblks, ip, i, needj); 6534 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6535 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6536 /* Release a whole indirect tree. */ 6537 if (lbn > lastlbn) { 6538 setup_freeindir(freeblks, ip, i, -lbn -i, 6539 needj); 6540 continue; 6541 } 6542 iboff = i + NDADDR; 6543 /* 6544 * Traverse partially truncated indirect tree. 6545 */ 6546 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6547 setup_trunc_indir(freeblks, ip, -lbn - i, 6548 lastlbn, DIP(ip, i_ib[i])); 6549 } 6550 /* 6551 * Handle partial truncation to a frag boundary. 6552 */ 6553 if (frags) { 6554 ufs2_daddr_t blkno; 6555 long oldfrags; 6556 6557 oldfrags = blksize(fs, ip, lastlbn); 6558 blkno = DIP(ip, i_db[lastlbn]); 6559 if (blkno && oldfrags != frags) { 6560 oldfrags -= frags; 6561 oldfrags = numfrags(ip->i_fs, oldfrags); 6562 blkno += numfrags(ip->i_fs, frags); 6563 newfreework(ump, freeblks, NULL, lastlbn, 6564 blkno, oldfrags, 0, needj); 6565 if (needj) 6566 adjust_newfreework(freeblks, 6567 numfrags(ip->i_fs, frags)); 6568 } else if (blkno == 0) 6569 allocblock = 1; 6570 } 6571 /* 6572 * Add a journal record for partial truncate if we are 6573 * handling indirect blocks. Non-indirects need no extra 6574 * journaling. 6575 */ 6576 if (length != 0 && lastlbn >= NDADDR) { 6577 ip->i_flag |= IN_TRUNCATED; 6578 newjtrunc(freeblks, length, 0); 6579 } 6580 ip->i_size = length; 6581 DIP_SET(ip, i_size, ip->i_size); 6582 datablocks = DIP(ip, i_blocks) - extblocks; 6583 if (length != 0) 6584 datablocks = blkcount(ip->i_fs, datablocks, length); 6585 freeblks->fb_len = length; 6586 } 6587 if ((flags & IO_EXT) != 0) { 6588 for (i = 0; i < NXADDR; i++) 6589 setup_freeext(freeblks, ip, i, needj); 6590 ip->i_din2->di_extsize = 0; 6591 datablocks += extblocks; 6592 } 6593 #ifdef QUOTA 6594 /* Reference the quotas in case the block count is wrong in the end. */ 6595 quotaref(vp, freeblks->fb_quota); 6596 (void) chkdq(ip, -datablocks, NOCRED, 0); 6597 #endif 6598 freeblks->fb_chkcnt = -datablocks; 6599 UFS_LOCK(ump); 6600 fs->fs_pendingblocks += datablocks; 6601 UFS_UNLOCK(ump); 6602 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6603 /* 6604 * Handle truncation of incomplete alloc direct dependencies. We 6605 * hold the inode block locked to prevent incomplete dependencies 6606 * from reaching the disk while we are eliminating those that 6607 * have been truncated. This is a partially inlined ffs_update(). 6608 */ 6609 ufs_itimes(vp); 6610 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6611 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6612 (int)fs->fs_bsize, cred, &bp); 6613 if (error) { 6614 brelse(bp); 6615 softdep_error("softdep_journal_freeblocks", error); 6616 return; 6617 } 6618 if (bp->b_bufsize == fs->fs_bsize) 6619 bp->b_flags |= B_CLUSTEROK; 6620 softdep_update_inodeblock(ip, bp, 0); 6621 if (ump->um_fstype == UFS1) 6622 *((struct ufs1_dinode *)bp->b_data + 6623 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6624 else 6625 *((struct ufs2_dinode *)bp->b_data + 6626 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6627 ACQUIRE_LOCK(ump); 6628 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6629 if ((inodedep->id_state & IOSTARTED) != 0) 6630 panic("softdep_setup_freeblocks: inode busy"); 6631 /* 6632 * Add the freeblks structure to the list of operations that 6633 * must await the zero'ed inode being written to disk. If we 6634 * still have a bitmap dependency (needj), then the inode 6635 * has never been written to disk, so we can process the 6636 * freeblks below once we have deleted the dependencies. 6637 */ 6638 if (needj) 6639 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6640 else 6641 freeblks->fb_state |= COMPLETE; 6642 if ((flags & IO_NORMAL) != 0) { 6643 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6644 if (adp->ad_offset > iboff) 6645 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6646 freeblks); 6647 /* 6648 * Truncate the allocdirect. We could eliminate 6649 * or modify journal records as well. 6650 */ 6651 else if (adp->ad_offset == iboff && frags) 6652 adp->ad_newsize = frags; 6653 } 6654 } 6655 if ((flags & IO_EXT) != 0) 6656 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6657 cancel_allocdirect(&inodedep->id_extupdt, adp, 6658 freeblks); 6659 /* 6660 * Scan the bufwait list for newblock dependencies that will never 6661 * make it to disk. 6662 */ 6663 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6664 if (wk->wk_type != D_ALLOCDIRECT) 6665 continue; 6666 adp = WK_ALLOCDIRECT(wk); 6667 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6668 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6669 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6670 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6671 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6672 } 6673 } 6674 /* 6675 * Add journal work. 6676 */ 6677 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6678 add_to_journal(&jblkdep->jb_list); 6679 FREE_LOCK(ump); 6680 bdwrite(bp); 6681 /* 6682 * Truncate dependency structures beyond length. 6683 */ 6684 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6685 /* 6686 * This is only set when we need to allocate a fragment because 6687 * none existed at the end of a frag-sized file. It handles only 6688 * allocating a new, zero filled block. 6689 */ 6690 if (allocblock) { 6691 ip->i_size = length - lastoff; 6692 DIP_SET(ip, i_size, ip->i_size); 6693 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6694 if (error != 0) { 6695 softdep_error("softdep_journal_freeblks", error); 6696 return; 6697 } 6698 ip->i_size = length; 6699 DIP_SET(ip, i_size, length); 6700 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6701 allocbuf(bp, frags); 6702 ffs_update(vp, 0); 6703 bawrite(bp); 6704 } else if (lastoff != 0 && vp->v_type != VDIR) { 6705 int size; 6706 6707 /* 6708 * Zero the end of a truncated frag or block. 6709 */ 6710 size = sblksize(fs, length, lastlbn); 6711 error = bread(vp, lastlbn, size, cred, &bp); 6712 if (error) { 6713 softdep_error("softdep_journal_freeblks", error); 6714 return; 6715 } 6716 bzero((char *)bp->b_data + lastoff, size - lastoff); 6717 bawrite(bp); 6718 6719 } 6720 ACQUIRE_LOCK(ump); 6721 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6722 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6723 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6724 /* 6725 * We zero earlier truncations so they don't erroneously 6726 * update i_blocks. 6727 */ 6728 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6729 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6730 fbn->fb_len = 0; 6731 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6732 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6733 freeblks->fb_state |= INPROGRESS; 6734 else 6735 freeblks = NULL; 6736 FREE_LOCK(ump); 6737 if (freeblks) 6738 handle_workitem_freeblocks(freeblks, 0); 6739 trunc_pages(ip, length, extblocks, flags); 6740 6741 } 6742 6743 /* 6744 * Flush a JOP_SYNC to the journal. 6745 */ 6746 void 6747 softdep_journal_fsync(ip) 6748 struct inode *ip; 6749 { 6750 struct jfsync *jfsync; 6751 6752 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 6753 ("softdep_journal_fsync called on non-softdep filesystem")); 6754 if ((ip->i_flag & IN_TRUNCATED) == 0) 6755 return; 6756 ip->i_flag &= ~IN_TRUNCATED; 6757 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6758 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ip->i_ump)); 6759 jfsync->jfs_size = ip->i_size; 6760 jfsync->jfs_ino = ip->i_number; 6761 ACQUIRE_LOCK(ip->i_ump); 6762 add_to_journal(&jfsync->jfs_list); 6763 jwait(&jfsync->jfs_list, MNT_WAIT); 6764 FREE_LOCK(ip->i_ump); 6765 } 6766 6767 /* 6768 * Block de-allocation dependencies. 6769 * 6770 * When blocks are de-allocated, the on-disk pointers must be nullified before 6771 * the blocks are made available for use by other files. (The true 6772 * requirement is that old pointers must be nullified before new on-disk 6773 * pointers are set. We chose this slightly more stringent requirement to 6774 * reduce complexity.) Our implementation handles this dependency by updating 6775 * the inode (or indirect block) appropriately but delaying the actual block 6776 * de-allocation (i.e., freemap and free space count manipulation) until 6777 * after the updated versions reach stable storage. After the disk is 6778 * updated, the blocks can be safely de-allocated whenever it is convenient. 6779 * This implementation handles only the common case of reducing a file's 6780 * length to zero. Other cases are handled by the conventional synchronous 6781 * write approach. 6782 * 6783 * The ffs implementation with which we worked double-checks 6784 * the state of the block pointers and file size as it reduces 6785 * a file's length. Some of this code is replicated here in our 6786 * soft updates implementation. The freeblks->fb_chkcnt field is 6787 * used to transfer a part of this information to the procedure 6788 * that eventually de-allocates the blocks. 6789 * 6790 * This routine should be called from the routine that shortens 6791 * a file's length, before the inode's size or block pointers 6792 * are modified. It will save the block pointer information for 6793 * later release and zero the inode so that the calling routine 6794 * can release it. 6795 */ 6796 void 6797 softdep_setup_freeblocks(ip, length, flags) 6798 struct inode *ip; /* The inode whose length is to be reduced */ 6799 off_t length; /* The new length for the file */ 6800 int flags; /* IO_EXT and/or IO_NORMAL */ 6801 { 6802 struct ufs1_dinode *dp1; 6803 struct ufs2_dinode *dp2; 6804 struct freeblks *freeblks; 6805 struct inodedep *inodedep; 6806 struct allocdirect *adp; 6807 struct ufsmount *ump; 6808 struct buf *bp; 6809 struct fs *fs; 6810 ufs2_daddr_t extblocks, datablocks; 6811 struct mount *mp; 6812 int i, delay, error, dflags; 6813 ufs_lbn_t tmpval; 6814 ufs_lbn_t lbn; 6815 6816 ump = ip->i_ump; 6817 mp = UFSTOVFS(ump); 6818 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 6819 ("softdep_setup_freeblocks called on non-softdep filesystem")); 6820 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6821 ip->i_number, length); 6822 KASSERT(length == 0, ("softdep_setup_freeblocks: non-zero length")); 6823 fs = ip->i_fs; 6824 freeblks = newfreeblks(mp, ip); 6825 extblocks = 0; 6826 datablocks = 0; 6827 if (fs->fs_magic == FS_UFS2_MAGIC) 6828 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6829 if ((flags & IO_NORMAL) != 0) { 6830 for (i = 0; i < NDADDR; i++) 6831 setup_freedirect(freeblks, ip, i, 0); 6832 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6833 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6834 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6835 ip->i_size = 0; 6836 DIP_SET(ip, i_size, 0); 6837 datablocks = DIP(ip, i_blocks) - extblocks; 6838 } 6839 if ((flags & IO_EXT) != 0) { 6840 for (i = 0; i < NXADDR; i++) 6841 setup_freeext(freeblks, ip, i, 0); 6842 ip->i_din2->di_extsize = 0; 6843 datablocks += extblocks; 6844 } 6845 #ifdef QUOTA 6846 /* Reference the quotas in case the block count is wrong in the end. */ 6847 quotaref(ITOV(ip), freeblks->fb_quota); 6848 (void) chkdq(ip, -datablocks, NOCRED, 0); 6849 #endif 6850 freeblks->fb_chkcnt = -datablocks; 6851 UFS_LOCK(ump); 6852 fs->fs_pendingblocks += datablocks; 6853 UFS_UNLOCK(ump); 6854 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6855 /* 6856 * Push the zero'ed inode to to its disk buffer so that we are free 6857 * to delete its dependencies below. Once the dependencies are gone 6858 * the buffer can be safely released. 6859 */ 6860 if ((error = bread(ip->i_devvp, 6861 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6862 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6863 brelse(bp); 6864 softdep_error("softdep_setup_freeblocks", error); 6865 } 6866 if (ump->um_fstype == UFS1) { 6867 dp1 = ((struct ufs1_dinode *)bp->b_data + 6868 ino_to_fsbo(fs, ip->i_number)); 6869 ip->i_din1->di_freelink = dp1->di_freelink; 6870 *dp1 = *ip->i_din1; 6871 } else { 6872 dp2 = ((struct ufs2_dinode *)bp->b_data + 6873 ino_to_fsbo(fs, ip->i_number)); 6874 ip->i_din2->di_freelink = dp2->di_freelink; 6875 *dp2 = *ip->i_din2; 6876 } 6877 /* 6878 * Find and eliminate any inode dependencies. 6879 */ 6880 ACQUIRE_LOCK(ump); 6881 dflags = DEPALLOC; 6882 if (IS_SNAPSHOT(ip)) 6883 dflags |= NODELAY; 6884 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6885 if ((inodedep->id_state & IOSTARTED) != 0) 6886 panic("softdep_setup_freeblocks: inode busy"); 6887 /* 6888 * Add the freeblks structure to the list of operations that 6889 * must await the zero'ed inode being written to disk. If we 6890 * still have a bitmap dependency (delay == 0), then the inode 6891 * has never been written to disk, so we can process the 6892 * freeblks below once we have deleted the dependencies. 6893 */ 6894 delay = (inodedep->id_state & DEPCOMPLETE); 6895 if (delay) 6896 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6897 else 6898 freeblks->fb_state |= COMPLETE; 6899 /* 6900 * Because the file length has been truncated to zero, any 6901 * pending block allocation dependency structures associated 6902 * with this inode are obsolete and can simply be de-allocated. 6903 * We must first merge the two dependency lists to get rid of 6904 * any duplicate freefrag structures, then purge the merged list. 6905 * If we still have a bitmap dependency, then the inode has never 6906 * been written to disk, so we can free any fragments without delay. 6907 */ 6908 if (flags & IO_NORMAL) { 6909 merge_inode_lists(&inodedep->id_newinoupdt, 6910 &inodedep->id_inoupdt); 6911 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 6912 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6913 freeblks); 6914 } 6915 if (flags & IO_EXT) { 6916 merge_inode_lists(&inodedep->id_newextupdt, 6917 &inodedep->id_extupdt); 6918 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6919 cancel_allocdirect(&inodedep->id_extupdt, adp, 6920 freeblks); 6921 } 6922 FREE_LOCK(ump); 6923 bdwrite(bp); 6924 trunc_dependencies(ip, freeblks, -1, 0, flags); 6925 ACQUIRE_LOCK(ump); 6926 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 6927 (void) free_inodedep(inodedep); 6928 freeblks->fb_state |= DEPCOMPLETE; 6929 /* 6930 * If the inode with zeroed block pointers is now on disk 6931 * we can start freeing blocks. 6932 */ 6933 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 6934 freeblks->fb_state |= INPROGRESS; 6935 else 6936 freeblks = NULL; 6937 FREE_LOCK(ump); 6938 if (freeblks) 6939 handle_workitem_freeblocks(freeblks, 0); 6940 trunc_pages(ip, length, extblocks, flags); 6941 } 6942 6943 /* 6944 * Eliminate pages from the page cache that back parts of this inode and 6945 * adjust the vnode pager's idea of our size. This prevents stale data 6946 * from hanging around in the page cache. 6947 */ 6948 static void 6949 trunc_pages(ip, length, extblocks, flags) 6950 struct inode *ip; 6951 off_t length; 6952 ufs2_daddr_t extblocks; 6953 int flags; 6954 { 6955 struct vnode *vp; 6956 struct fs *fs; 6957 ufs_lbn_t lbn; 6958 off_t end, extend; 6959 6960 vp = ITOV(ip); 6961 fs = ip->i_fs; 6962 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 6963 if ((flags & IO_EXT) != 0) 6964 vn_pages_remove(vp, extend, 0); 6965 if ((flags & IO_NORMAL) == 0) 6966 return; 6967 BO_LOCK(&vp->v_bufobj); 6968 drain_output(vp); 6969 BO_UNLOCK(&vp->v_bufobj); 6970 /* 6971 * The vnode pager eliminates file pages we eliminate indirects 6972 * below. 6973 */ 6974 vnode_pager_setsize(vp, length); 6975 /* 6976 * Calculate the end based on the last indirect we want to keep. If 6977 * the block extends into indirects we can just use the negative of 6978 * its lbn. Doubles and triples exist at lower numbers so we must 6979 * be careful not to remove those, if they exist. double and triple 6980 * indirect lbns do not overlap with others so it is not important 6981 * to verify how many levels are required. 6982 */ 6983 lbn = lblkno(fs, length); 6984 if (lbn >= NDADDR) { 6985 /* Calculate the virtual lbn of the triple indirect. */ 6986 lbn = -lbn - (NIADDR - 1); 6987 end = OFF_TO_IDX(lblktosize(fs, lbn)); 6988 } else 6989 end = extend; 6990 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 6991 } 6992 6993 /* 6994 * See if the buf bp is in the range eliminated by truncation. 6995 */ 6996 static int 6997 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 6998 struct buf *bp; 6999 int *blkoffp; 7000 ufs_lbn_t lastlbn; 7001 int lastoff; 7002 int flags; 7003 { 7004 ufs_lbn_t lbn; 7005 7006 *blkoffp = 0; 7007 /* Only match ext/normal blocks as appropriate. */ 7008 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 7009 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 7010 return (0); 7011 /* ALTDATA is always a full truncation. */ 7012 if ((bp->b_xflags & BX_ALTDATA) != 0) 7013 return (1); 7014 /* -1 is full truncation. */ 7015 if (lastlbn == -1) 7016 return (1); 7017 /* 7018 * If this is a partial truncate we only want those 7019 * blocks and indirect blocks that cover the range 7020 * we're after. 7021 */ 7022 lbn = bp->b_lblkno; 7023 if (lbn < 0) 7024 lbn = -(lbn + lbn_level(lbn)); 7025 if (lbn < lastlbn) 7026 return (0); 7027 /* Here we only truncate lblkno if it's partial. */ 7028 if (lbn == lastlbn) { 7029 if (lastoff == 0) 7030 return (0); 7031 *blkoffp = lastoff; 7032 } 7033 return (1); 7034 } 7035 7036 /* 7037 * Eliminate any dependencies that exist in memory beyond lblkno:off 7038 */ 7039 static void 7040 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 7041 struct inode *ip; 7042 struct freeblks *freeblks; 7043 ufs_lbn_t lastlbn; 7044 int lastoff; 7045 int flags; 7046 { 7047 struct bufobj *bo; 7048 struct vnode *vp; 7049 struct buf *bp; 7050 struct fs *fs; 7051 int blkoff; 7052 7053 /* 7054 * We must wait for any I/O in progress to finish so that 7055 * all potential buffers on the dirty list will be visible. 7056 * Once they are all there, walk the list and get rid of 7057 * any dependencies. 7058 */ 7059 fs = ip->i_fs; 7060 vp = ITOV(ip); 7061 bo = &vp->v_bufobj; 7062 BO_LOCK(bo); 7063 drain_output(vp); 7064 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 7065 bp->b_vflags &= ~BV_SCANNED; 7066 restart: 7067 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 7068 if (bp->b_vflags & BV_SCANNED) 7069 continue; 7070 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7071 bp->b_vflags |= BV_SCANNED; 7072 continue; 7073 } 7074 KASSERT(bp->b_bufobj == bo, ("Wrong object in buffer")); 7075 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 7076 goto restart; 7077 BO_UNLOCK(bo); 7078 if (deallocate_dependencies(bp, freeblks, blkoff)) 7079 bqrelse(bp); 7080 else 7081 brelse(bp); 7082 BO_LOCK(bo); 7083 goto restart; 7084 } 7085 /* 7086 * Now do the work of vtruncbuf while also matching indirect blocks. 7087 */ 7088 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 7089 bp->b_vflags &= ~BV_SCANNED; 7090 cleanrestart: 7091 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 7092 if (bp->b_vflags & BV_SCANNED) 7093 continue; 7094 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 7095 bp->b_vflags |= BV_SCANNED; 7096 continue; 7097 } 7098 if (BUF_LOCK(bp, 7099 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 7100 BO_LOCKPTR(bo)) == ENOLCK) { 7101 BO_LOCK(bo); 7102 goto cleanrestart; 7103 } 7104 bp->b_vflags |= BV_SCANNED; 7105 bremfree(bp); 7106 if (blkoff != 0) { 7107 allocbuf(bp, blkoff); 7108 bqrelse(bp); 7109 } else { 7110 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 7111 brelse(bp); 7112 } 7113 BO_LOCK(bo); 7114 goto cleanrestart; 7115 } 7116 drain_output(vp); 7117 BO_UNLOCK(bo); 7118 } 7119 7120 static int 7121 cancel_pagedep(pagedep, freeblks, blkoff) 7122 struct pagedep *pagedep; 7123 struct freeblks *freeblks; 7124 int blkoff; 7125 { 7126 struct jremref *jremref; 7127 struct jmvref *jmvref; 7128 struct dirrem *dirrem, *tmp; 7129 int i; 7130 7131 /* 7132 * Copy any directory remove dependencies to the list 7133 * to be processed after the freeblks proceeds. If 7134 * directory entry never made it to disk they 7135 * can be dumped directly onto the work list. 7136 */ 7137 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 7138 /* Skip this directory removal if it is intended to remain. */ 7139 if (dirrem->dm_offset < blkoff) 7140 continue; 7141 /* 7142 * If there are any dirrems we wait for the journal write 7143 * to complete and then restart the buf scan as the lock 7144 * has been dropped. 7145 */ 7146 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 7147 jwait(&jremref->jr_list, MNT_WAIT); 7148 return (ERESTART); 7149 } 7150 LIST_REMOVE(dirrem, dm_next); 7151 dirrem->dm_dirinum = pagedep->pd_ino; 7152 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 7153 } 7154 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 7155 jwait(&jmvref->jm_list, MNT_WAIT); 7156 return (ERESTART); 7157 } 7158 /* 7159 * When we're partially truncating a pagedep we just want to flush 7160 * journal entries and return. There can not be any adds in the 7161 * truncated portion of the directory and newblk must remain if 7162 * part of the block remains. 7163 */ 7164 if (blkoff != 0) { 7165 struct diradd *dap; 7166 7167 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 7168 if (dap->da_offset > blkoff) 7169 panic("cancel_pagedep: diradd %p off %d > %d", 7170 dap, dap->da_offset, blkoff); 7171 for (i = 0; i < DAHASHSZ; i++) 7172 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 7173 if (dap->da_offset > blkoff) 7174 panic("cancel_pagedep: diradd %p off %d > %d", 7175 dap, dap->da_offset, blkoff); 7176 return (0); 7177 } 7178 /* 7179 * There should be no directory add dependencies present 7180 * as the directory could not be truncated until all 7181 * children were removed. 7182 */ 7183 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 7184 ("deallocate_dependencies: pendinghd != NULL")); 7185 for (i = 0; i < DAHASHSZ; i++) 7186 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 7187 ("deallocate_dependencies: diraddhd != NULL")); 7188 if ((pagedep->pd_state & NEWBLOCK) != 0) 7189 free_newdirblk(pagedep->pd_newdirblk); 7190 if (free_pagedep(pagedep) == 0) 7191 panic("Failed to free pagedep %p", pagedep); 7192 return (0); 7193 } 7194 7195 /* 7196 * Reclaim any dependency structures from a buffer that is about to 7197 * be reallocated to a new vnode. The buffer must be locked, thus, 7198 * no I/O completion operations can occur while we are manipulating 7199 * its associated dependencies. The mutex is held so that other I/O's 7200 * associated with related dependencies do not occur. 7201 */ 7202 static int 7203 deallocate_dependencies(bp, freeblks, off) 7204 struct buf *bp; 7205 struct freeblks *freeblks; 7206 int off; 7207 { 7208 struct indirdep *indirdep; 7209 struct pagedep *pagedep; 7210 struct allocdirect *adp; 7211 struct worklist *wk, *wkn; 7212 struct ufsmount *ump; 7213 7214 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 7215 goto done; 7216 ump = VFSTOUFS(wk->wk_mp); 7217 ACQUIRE_LOCK(ump); 7218 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7219 switch (wk->wk_type) { 7220 case D_INDIRDEP: 7221 indirdep = WK_INDIRDEP(wk); 7222 if (bp->b_lblkno >= 0 || 7223 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7224 panic("deallocate_dependencies: not indir"); 7225 cancel_indirdep(indirdep, bp, freeblks); 7226 continue; 7227 7228 case D_PAGEDEP: 7229 pagedep = WK_PAGEDEP(wk); 7230 if (cancel_pagedep(pagedep, freeblks, off)) { 7231 FREE_LOCK(ump); 7232 return (ERESTART); 7233 } 7234 continue; 7235 7236 case D_ALLOCINDIR: 7237 /* 7238 * Simply remove the allocindir, we'll find it via 7239 * the indirdep where we can clear pointers if 7240 * needed. 7241 */ 7242 WORKLIST_REMOVE(wk); 7243 continue; 7244 7245 case D_FREEWORK: 7246 /* 7247 * A truncation is waiting for the zero'd pointers 7248 * to be written. It can be freed when the freeblks 7249 * is journaled. 7250 */ 7251 WORKLIST_REMOVE(wk); 7252 wk->wk_state |= ONDEPLIST; 7253 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7254 break; 7255 7256 case D_ALLOCDIRECT: 7257 adp = WK_ALLOCDIRECT(wk); 7258 if (off != 0) 7259 continue; 7260 /* FALLTHROUGH */ 7261 default: 7262 panic("deallocate_dependencies: Unexpected type %s", 7263 TYPENAME(wk->wk_type)); 7264 /* NOTREACHED */ 7265 } 7266 } 7267 FREE_LOCK(ump); 7268 done: 7269 /* 7270 * Don't throw away this buf, we were partially truncating and 7271 * some deps may always remain. 7272 */ 7273 if (off) { 7274 allocbuf(bp, off); 7275 bp->b_vflags |= BV_SCANNED; 7276 return (EBUSY); 7277 } 7278 bp->b_flags |= B_INVAL | B_NOCACHE; 7279 7280 return (0); 7281 } 7282 7283 /* 7284 * An allocdirect is being canceled due to a truncate. We must make sure 7285 * the journal entry is released in concert with the blkfree that releases 7286 * the storage. Completed journal entries must not be released until the 7287 * space is no longer pointed to by the inode or in the bitmap. 7288 */ 7289 static void 7290 cancel_allocdirect(adphead, adp, freeblks) 7291 struct allocdirectlst *adphead; 7292 struct allocdirect *adp; 7293 struct freeblks *freeblks; 7294 { 7295 struct freework *freework; 7296 struct newblk *newblk; 7297 struct worklist *wk; 7298 7299 TAILQ_REMOVE(adphead, adp, ad_next); 7300 newblk = (struct newblk *)adp; 7301 freework = NULL; 7302 /* 7303 * Find the correct freework structure. 7304 */ 7305 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7306 if (wk->wk_type != D_FREEWORK) 7307 continue; 7308 freework = WK_FREEWORK(wk); 7309 if (freework->fw_blkno == newblk->nb_newblkno) 7310 break; 7311 } 7312 if (freework == NULL) 7313 panic("cancel_allocdirect: Freework not found"); 7314 /* 7315 * If a newblk exists at all we still have the journal entry that 7316 * initiated the allocation so we do not need to journal the free. 7317 */ 7318 cancel_jfreeblk(freeblks, freework->fw_blkno); 7319 /* 7320 * If the journal hasn't been written the jnewblk must be passed 7321 * to the call to ffs_blkfree that reclaims the space. We accomplish 7322 * this by linking the journal dependency into the freework to be 7323 * freed when freework_freeblock() is called. If the journal has 7324 * been written we can simply reclaim the journal space when the 7325 * freeblks work is complete. 7326 */ 7327 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7328 &freeblks->fb_jwork); 7329 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7330 } 7331 7332 7333 /* 7334 * Cancel a new block allocation. May be an indirect or direct block. We 7335 * remove it from various lists and return any journal record that needs to 7336 * be resolved by the caller. 7337 * 7338 * A special consideration is made for indirects which were never pointed 7339 * at on disk and will never be found once this block is released. 7340 */ 7341 static struct jnewblk * 7342 cancel_newblk(newblk, wk, wkhd) 7343 struct newblk *newblk; 7344 struct worklist *wk; 7345 struct workhead *wkhd; 7346 { 7347 struct jnewblk *jnewblk; 7348 7349 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7350 7351 newblk->nb_state |= GOINGAWAY; 7352 /* 7353 * Previously we traversed the completedhd on each indirdep 7354 * attached to this newblk to cancel them and gather journal 7355 * work. Since we need only the oldest journal segment and 7356 * the lowest point on the tree will always have the oldest 7357 * journal segment we are free to release the segments 7358 * of any subordinates and may leave the indirdep list to 7359 * indirdep_complete() when this newblk is freed. 7360 */ 7361 if (newblk->nb_state & ONDEPLIST) { 7362 newblk->nb_state &= ~ONDEPLIST; 7363 LIST_REMOVE(newblk, nb_deps); 7364 } 7365 if (newblk->nb_state & ONWORKLIST) 7366 WORKLIST_REMOVE(&newblk->nb_list); 7367 /* 7368 * If the journal entry hasn't been written we save a pointer to 7369 * the dependency that frees it until it is written or the 7370 * superseding operation completes. 7371 */ 7372 jnewblk = newblk->nb_jnewblk; 7373 if (jnewblk != NULL && wk != NULL) { 7374 newblk->nb_jnewblk = NULL; 7375 jnewblk->jn_dep = wk; 7376 } 7377 if (!LIST_EMPTY(&newblk->nb_jwork)) 7378 jwork_move(wkhd, &newblk->nb_jwork); 7379 /* 7380 * When truncating we must free the newdirblk early to remove 7381 * the pagedep from the hash before returning. 7382 */ 7383 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7384 free_newdirblk(WK_NEWDIRBLK(wk)); 7385 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7386 panic("cancel_newblk: extra newdirblk"); 7387 7388 return (jnewblk); 7389 } 7390 7391 /* 7392 * Schedule the freefrag associated with a newblk to be released once 7393 * the pointers are written and the previous block is no longer needed. 7394 */ 7395 static void 7396 newblk_freefrag(newblk) 7397 struct newblk *newblk; 7398 { 7399 struct freefrag *freefrag; 7400 7401 if (newblk->nb_freefrag == NULL) 7402 return; 7403 freefrag = newblk->nb_freefrag; 7404 newblk->nb_freefrag = NULL; 7405 freefrag->ff_state |= COMPLETE; 7406 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7407 add_to_worklist(&freefrag->ff_list, 0); 7408 } 7409 7410 /* 7411 * Free a newblk. Generate a new freefrag work request if appropriate. 7412 * This must be called after the inode pointer and any direct block pointers 7413 * are valid or fully removed via truncate or frag extension. 7414 */ 7415 static void 7416 free_newblk(newblk) 7417 struct newblk *newblk; 7418 { 7419 struct indirdep *indirdep; 7420 struct worklist *wk; 7421 7422 KASSERT(newblk->nb_jnewblk == NULL, 7423 ("free_newblk: jnewblk %p still attached", newblk->nb_jnewblk)); 7424 KASSERT(newblk->nb_list.wk_type != D_NEWBLK, 7425 ("free_newblk: unclaimed newblk")); 7426 LOCK_OWNED(VFSTOUFS(newblk->nb_list.wk_mp)); 7427 newblk_freefrag(newblk); 7428 if (newblk->nb_state & ONDEPLIST) 7429 LIST_REMOVE(newblk, nb_deps); 7430 if (newblk->nb_state & ONWORKLIST) 7431 WORKLIST_REMOVE(&newblk->nb_list); 7432 LIST_REMOVE(newblk, nb_hash); 7433 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7434 free_newdirblk(WK_NEWDIRBLK(wk)); 7435 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7436 panic("free_newblk: extra newdirblk"); 7437 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7438 indirdep_complete(indirdep); 7439 handle_jwork(&newblk->nb_jwork); 7440 WORKITEM_FREE(newblk, D_NEWBLK); 7441 } 7442 7443 /* 7444 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7445 * This routine must be called with splbio interrupts blocked. 7446 */ 7447 static void 7448 free_newdirblk(newdirblk) 7449 struct newdirblk *newdirblk; 7450 { 7451 struct pagedep *pagedep; 7452 struct diradd *dap; 7453 struct worklist *wk; 7454 7455 LOCK_OWNED(VFSTOUFS(newdirblk->db_list.wk_mp)); 7456 WORKLIST_REMOVE(&newdirblk->db_list); 7457 /* 7458 * If the pagedep is still linked onto the directory buffer 7459 * dependency chain, then some of the entries on the 7460 * pd_pendinghd list may not be committed to disk yet. In 7461 * this case, we will simply clear the NEWBLOCK flag and 7462 * let the pd_pendinghd list be processed when the pagedep 7463 * is next written. If the pagedep is no longer on the buffer 7464 * dependency chain, then all the entries on the pd_pending 7465 * list are committed to disk and we can free them here. 7466 */ 7467 pagedep = newdirblk->db_pagedep; 7468 pagedep->pd_state &= ~NEWBLOCK; 7469 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7470 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7471 free_diradd(dap, NULL); 7472 /* 7473 * If no dependencies remain, the pagedep will be freed. 7474 */ 7475 free_pagedep(pagedep); 7476 } 7477 /* Should only ever be one item in the list. */ 7478 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7479 WORKLIST_REMOVE(wk); 7480 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7481 } 7482 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7483 } 7484 7485 /* 7486 * Prepare an inode to be freed. The actual free operation is not 7487 * done until the zero'ed inode has been written to disk. 7488 */ 7489 void 7490 softdep_freefile(pvp, ino, mode) 7491 struct vnode *pvp; 7492 ino_t ino; 7493 int mode; 7494 { 7495 struct inode *ip = VTOI(pvp); 7496 struct inodedep *inodedep; 7497 struct freefile *freefile; 7498 struct freeblks *freeblks; 7499 struct ufsmount *ump; 7500 7501 ump = ip->i_ump; 7502 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 7503 ("softdep_freefile called on non-softdep filesystem")); 7504 /* 7505 * This sets up the inode de-allocation dependency. 7506 */ 7507 freefile = malloc(sizeof(struct freefile), 7508 M_FREEFILE, M_SOFTDEP_FLAGS); 7509 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7510 freefile->fx_mode = mode; 7511 freefile->fx_oldinum = ino; 7512 freefile->fx_devvp = ip->i_devvp; 7513 LIST_INIT(&freefile->fx_jwork); 7514 UFS_LOCK(ump); 7515 ip->i_fs->fs_pendinginodes += 1; 7516 UFS_UNLOCK(ump); 7517 7518 /* 7519 * If the inodedep does not exist, then the zero'ed inode has 7520 * been written to disk. If the allocated inode has never been 7521 * written to disk, then the on-disk inode is zero'ed. In either 7522 * case we can free the file immediately. If the journal was 7523 * canceled before being written the inode will never make it to 7524 * disk and we must send the canceled journal entrys to 7525 * ffs_freefile() to be cleared in conjunction with the bitmap. 7526 * Any blocks waiting on the inode to write can be safely freed 7527 * here as it will never been written. 7528 */ 7529 ACQUIRE_LOCK(ump); 7530 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7531 if (inodedep) { 7532 /* 7533 * Clear out freeblks that no longer need to reference 7534 * this inode. 7535 */ 7536 while ((freeblks = 7537 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7538 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7539 fb_next); 7540 freeblks->fb_state &= ~ONDEPLIST; 7541 } 7542 /* 7543 * Remove this inode from the unlinked list. 7544 */ 7545 if (inodedep->id_state & UNLINKED) { 7546 /* 7547 * Save the journal work to be freed with the bitmap 7548 * before we clear UNLINKED. Otherwise it can be lost 7549 * if the inode block is written. 7550 */ 7551 handle_bufwait(inodedep, &freefile->fx_jwork); 7552 clear_unlinked_inodedep(inodedep); 7553 /* 7554 * Re-acquire inodedep as we've dropped the 7555 * per-filesystem lock in clear_unlinked_inodedep(). 7556 */ 7557 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7558 } 7559 } 7560 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7561 FREE_LOCK(ump); 7562 handle_workitem_freefile(freefile); 7563 return; 7564 } 7565 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7566 inodedep->id_state |= GOINGAWAY; 7567 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7568 FREE_LOCK(ump); 7569 if (ip->i_number == ino) 7570 ip->i_flag |= IN_MODIFIED; 7571 } 7572 7573 /* 7574 * Check to see if an inode has never been written to disk. If 7575 * so free the inodedep and return success, otherwise return failure. 7576 * This routine must be called with splbio interrupts blocked. 7577 * 7578 * If we still have a bitmap dependency, then the inode has never 7579 * been written to disk. Drop the dependency as it is no longer 7580 * necessary since the inode is being deallocated. We set the 7581 * ALLCOMPLETE flags since the bitmap now properly shows that the 7582 * inode is not allocated. Even if the inode is actively being 7583 * written, it has been rolled back to its zero'ed state, so we 7584 * are ensured that a zero inode is what is on the disk. For short 7585 * lived files, this change will usually result in removing all the 7586 * dependencies from the inode so that it can be freed immediately. 7587 */ 7588 static int 7589 check_inode_unwritten(inodedep) 7590 struct inodedep *inodedep; 7591 { 7592 7593 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7594 7595 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7596 !LIST_EMPTY(&inodedep->id_dirremhd) || 7597 !LIST_EMPTY(&inodedep->id_pendinghd) || 7598 !LIST_EMPTY(&inodedep->id_bufwait) || 7599 !LIST_EMPTY(&inodedep->id_inowait) || 7600 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7601 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7602 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7603 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7604 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7605 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7606 inodedep->id_mkdiradd != NULL || 7607 inodedep->id_nlinkdelta != 0) 7608 return (0); 7609 /* 7610 * Another process might be in initiate_write_inodeblock_ufs[12] 7611 * trying to allocate memory without holding "Softdep Lock". 7612 */ 7613 if ((inodedep->id_state & IOSTARTED) != 0 && 7614 inodedep->id_savedino1 == NULL) 7615 return (0); 7616 7617 if (inodedep->id_state & ONDEPLIST) 7618 LIST_REMOVE(inodedep, id_deps); 7619 inodedep->id_state &= ~ONDEPLIST; 7620 inodedep->id_state |= ALLCOMPLETE; 7621 inodedep->id_bmsafemap = NULL; 7622 if (inodedep->id_state & ONWORKLIST) 7623 WORKLIST_REMOVE(&inodedep->id_list); 7624 if (inodedep->id_savedino1 != NULL) { 7625 free(inodedep->id_savedino1, M_SAVEDINO); 7626 inodedep->id_savedino1 = NULL; 7627 } 7628 if (free_inodedep(inodedep) == 0) 7629 panic("check_inode_unwritten: busy inode"); 7630 return (1); 7631 } 7632 7633 /* 7634 * Try to free an inodedep structure. Return 1 if it could be freed. 7635 */ 7636 static int 7637 free_inodedep(inodedep) 7638 struct inodedep *inodedep; 7639 { 7640 7641 LOCK_OWNED(VFSTOUFS(inodedep->id_list.wk_mp)); 7642 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7643 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7644 !LIST_EMPTY(&inodedep->id_dirremhd) || 7645 !LIST_EMPTY(&inodedep->id_pendinghd) || 7646 !LIST_EMPTY(&inodedep->id_bufwait) || 7647 !LIST_EMPTY(&inodedep->id_inowait) || 7648 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7649 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7650 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7651 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7652 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7653 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7654 inodedep->id_mkdiradd != NULL || 7655 inodedep->id_nlinkdelta != 0 || 7656 inodedep->id_savedino1 != NULL) 7657 return (0); 7658 if (inodedep->id_state & ONDEPLIST) 7659 LIST_REMOVE(inodedep, id_deps); 7660 LIST_REMOVE(inodedep, id_hash); 7661 WORKITEM_FREE(inodedep, D_INODEDEP); 7662 return (1); 7663 } 7664 7665 /* 7666 * Free the block referenced by a freework structure. The parent freeblks 7667 * structure is released and completed when the final cg bitmap reaches 7668 * the disk. This routine may be freeing a jnewblk which never made it to 7669 * disk in which case we do not have to wait as the operation is undone 7670 * in memory immediately. 7671 */ 7672 static void 7673 freework_freeblock(freework) 7674 struct freework *freework; 7675 { 7676 struct freeblks *freeblks; 7677 struct jnewblk *jnewblk; 7678 struct ufsmount *ump; 7679 struct workhead wkhd; 7680 struct fs *fs; 7681 int bsize; 7682 int needj; 7683 7684 ump = VFSTOUFS(freework->fw_list.wk_mp); 7685 LOCK_OWNED(ump); 7686 /* 7687 * Handle partial truncate separately. 7688 */ 7689 if (freework->fw_indir) { 7690 complete_trunc_indir(freework); 7691 return; 7692 } 7693 freeblks = freework->fw_freeblks; 7694 fs = ump->um_fs; 7695 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7696 bsize = lfragtosize(fs, freework->fw_frags); 7697 LIST_INIT(&wkhd); 7698 /* 7699 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7700 * on the indirblk hashtable and prevents premature freeing. 7701 */ 7702 freework->fw_state |= DEPCOMPLETE; 7703 /* 7704 * SUJ needs to wait for the segment referencing freed indirect 7705 * blocks to expire so that we know the checker will not confuse 7706 * a re-allocated indirect block with its old contents. 7707 */ 7708 if (needj && freework->fw_lbn <= -NDADDR) 7709 indirblk_insert(freework); 7710 /* 7711 * If we are canceling an existing jnewblk pass it to the free 7712 * routine, otherwise pass the freeblk which will ultimately 7713 * release the freeblks. If we're not journaling, we can just 7714 * free the freeblks immediately. 7715 */ 7716 jnewblk = freework->fw_jnewblk; 7717 if (jnewblk != NULL) { 7718 cancel_jnewblk(jnewblk, &wkhd); 7719 needj = 0; 7720 } else if (needj) { 7721 freework->fw_state |= DELAYEDFREE; 7722 freeblks->fb_cgwait++; 7723 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7724 } 7725 FREE_LOCK(ump); 7726 freeblks_free(ump, freeblks, btodb(bsize)); 7727 CTR4(KTR_SUJ, 7728 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld", 7729 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7730 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7731 freeblks->fb_inum, freeblks->fb_vtype, &wkhd); 7732 ACQUIRE_LOCK(ump); 7733 /* 7734 * The jnewblk will be discarded and the bits in the map never 7735 * made it to disk. We can immediately free the freeblk. 7736 */ 7737 if (needj == 0) 7738 handle_written_freework(freework); 7739 } 7740 7741 /* 7742 * We enqueue freework items that need processing back on the freeblks and 7743 * add the freeblks to the worklist. This makes it easier to find all work 7744 * required to flush a truncation in process_truncates(). 7745 */ 7746 static void 7747 freework_enqueue(freework) 7748 struct freework *freework; 7749 { 7750 struct freeblks *freeblks; 7751 7752 freeblks = freework->fw_freeblks; 7753 if ((freework->fw_state & INPROGRESS) == 0) 7754 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7755 if ((freeblks->fb_state & 7756 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7757 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7758 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7759 } 7760 7761 /* 7762 * Start, continue, or finish the process of freeing an indirect block tree. 7763 * The free operation may be paused at any point with fw_off containing the 7764 * offset to restart from. This enables us to implement some flow control 7765 * for large truncates which may fan out and generate a huge number of 7766 * dependencies. 7767 */ 7768 static void 7769 handle_workitem_indirblk(freework) 7770 struct freework *freework; 7771 { 7772 struct freeblks *freeblks; 7773 struct ufsmount *ump; 7774 struct fs *fs; 7775 7776 freeblks = freework->fw_freeblks; 7777 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7778 fs = ump->um_fs; 7779 if (freework->fw_state & DEPCOMPLETE) { 7780 handle_written_freework(freework); 7781 return; 7782 } 7783 if (freework->fw_off == NINDIR(fs)) { 7784 freework_freeblock(freework); 7785 return; 7786 } 7787 freework->fw_state |= INPROGRESS; 7788 FREE_LOCK(ump); 7789 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7790 freework->fw_lbn); 7791 ACQUIRE_LOCK(ump); 7792 } 7793 7794 /* 7795 * Called when a freework structure attached to a cg buf is written. The 7796 * ref on either the parent or the freeblks structure is released and 7797 * the freeblks is added back to the worklist if there is more work to do. 7798 */ 7799 static void 7800 handle_written_freework(freework) 7801 struct freework *freework; 7802 { 7803 struct freeblks *freeblks; 7804 struct freework *parent; 7805 7806 freeblks = freework->fw_freeblks; 7807 parent = freework->fw_parent; 7808 if (freework->fw_state & DELAYEDFREE) 7809 freeblks->fb_cgwait--; 7810 freework->fw_state |= COMPLETE; 7811 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7812 WORKITEM_FREE(freework, D_FREEWORK); 7813 if (parent) { 7814 if (--parent->fw_ref == 0) 7815 freework_enqueue(parent); 7816 return; 7817 } 7818 if (--freeblks->fb_ref != 0) 7819 return; 7820 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7821 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7822 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7823 } 7824 7825 /* 7826 * This workitem routine performs the block de-allocation. 7827 * The workitem is added to the pending list after the updated 7828 * inode block has been written to disk. As mentioned above, 7829 * checks regarding the number of blocks de-allocated (compared 7830 * to the number of blocks allocated for the file) are also 7831 * performed in this function. 7832 */ 7833 static int 7834 handle_workitem_freeblocks(freeblks, flags) 7835 struct freeblks *freeblks; 7836 int flags; 7837 { 7838 struct freework *freework; 7839 struct newblk *newblk; 7840 struct allocindir *aip; 7841 struct ufsmount *ump; 7842 struct worklist *wk; 7843 7844 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7845 ("handle_workitem_freeblocks: Journal entries not written.")); 7846 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7847 ACQUIRE_LOCK(ump); 7848 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7849 WORKLIST_REMOVE(wk); 7850 switch (wk->wk_type) { 7851 case D_DIRREM: 7852 wk->wk_state |= COMPLETE; 7853 add_to_worklist(wk, 0); 7854 continue; 7855 7856 case D_ALLOCDIRECT: 7857 free_newblk(WK_NEWBLK(wk)); 7858 continue; 7859 7860 case D_ALLOCINDIR: 7861 aip = WK_ALLOCINDIR(wk); 7862 freework = NULL; 7863 if (aip->ai_state & DELAYEDFREE) { 7864 FREE_LOCK(ump); 7865 freework = newfreework(ump, freeblks, NULL, 7866 aip->ai_lbn, aip->ai_newblkno, 7867 ump->um_fs->fs_frag, 0, 0); 7868 ACQUIRE_LOCK(ump); 7869 } 7870 newblk = WK_NEWBLK(wk); 7871 if (newblk->nb_jnewblk) { 7872 freework->fw_jnewblk = newblk->nb_jnewblk; 7873 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7874 newblk->nb_jnewblk = NULL; 7875 } 7876 free_newblk(newblk); 7877 continue; 7878 7879 case D_FREEWORK: 7880 freework = WK_FREEWORK(wk); 7881 if (freework->fw_lbn <= -NDADDR) 7882 handle_workitem_indirblk(freework); 7883 else 7884 freework_freeblock(freework); 7885 continue; 7886 default: 7887 panic("handle_workitem_freeblocks: Unknown type %s", 7888 TYPENAME(wk->wk_type)); 7889 } 7890 } 7891 if (freeblks->fb_ref != 0) { 7892 freeblks->fb_state &= ~INPROGRESS; 7893 wake_worklist(&freeblks->fb_list); 7894 freeblks = NULL; 7895 } 7896 FREE_LOCK(ump); 7897 if (freeblks) 7898 return handle_complete_freeblocks(freeblks, flags); 7899 return (0); 7900 } 7901 7902 /* 7903 * Handle completion of block free via truncate. This allows fs_pending 7904 * to track the actual free block count more closely than if we only updated 7905 * it at the end. We must be careful to handle cases where the block count 7906 * on free was incorrect. 7907 */ 7908 static void 7909 freeblks_free(ump, freeblks, blocks) 7910 struct ufsmount *ump; 7911 struct freeblks *freeblks; 7912 int blocks; 7913 { 7914 struct fs *fs; 7915 ufs2_daddr_t remain; 7916 7917 UFS_LOCK(ump); 7918 remain = -freeblks->fb_chkcnt; 7919 freeblks->fb_chkcnt += blocks; 7920 if (remain > 0) { 7921 if (remain < blocks) 7922 blocks = remain; 7923 fs = ump->um_fs; 7924 fs->fs_pendingblocks -= blocks; 7925 } 7926 UFS_UNLOCK(ump); 7927 } 7928 7929 /* 7930 * Once all of the freework workitems are complete we can retire the 7931 * freeblocks dependency and any journal work awaiting completion. This 7932 * can not be called until all other dependencies are stable on disk. 7933 */ 7934 static int 7935 handle_complete_freeblocks(freeblks, flags) 7936 struct freeblks *freeblks; 7937 int flags; 7938 { 7939 struct inodedep *inodedep; 7940 struct inode *ip; 7941 struct vnode *vp; 7942 struct fs *fs; 7943 struct ufsmount *ump; 7944 ufs2_daddr_t spare; 7945 7946 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7947 fs = ump->um_fs; 7948 flags = LK_EXCLUSIVE | flags; 7949 spare = freeblks->fb_chkcnt; 7950 7951 /* 7952 * If we did not release the expected number of blocks we may have 7953 * to adjust the inode block count here. Only do so if it wasn't 7954 * a truncation to zero and the modrev still matches. 7955 */ 7956 if (spare && freeblks->fb_len != 0) { 7957 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7958 flags, &vp, FFSV_FORCEINSMQ) != 0) 7959 return (EBUSY); 7960 ip = VTOI(vp); 7961 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 7962 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 7963 ip->i_flag |= IN_CHANGE; 7964 /* 7965 * We must wait so this happens before the 7966 * journal is reclaimed. 7967 */ 7968 ffs_update(vp, 1); 7969 } 7970 vput(vp); 7971 } 7972 if (spare < 0) { 7973 UFS_LOCK(ump); 7974 fs->fs_pendingblocks += spare; 7975 UFS_UNLOCK(ump); 7976 } 7977 #ifdef QUOTA 7978 /* Handle spare. */ 7979 if (spare) 7980 quotaadj(freeblks->fb_quota, ump, -spare); 7981 quotarele(freeblks->fb_quota); 7982 #endif 7983 ACQUIRE_LOCK(ump); 7984 if (freeblks->fb_state & ONDEPLIST) { 7985 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7986 0, &inodedep); 7987 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 7988 freeblks->fb_state &= ~ONDEPLIST; 7989 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 7990 free_inodedep(inodedep); 7991 } 7992 /* 7993 * All of the freeblock deps must be complete prior to this call 7994 * so it's now safe to complete earlier outstanding journal entries. 7995 */ 7996 handle_jwork(&freeblks->fb_jwork); 7997 WORKITEM_FREE(freeblks, D_FREEBLKS); 7998 FREE_LOCK(ump); 7999 return (0); 8000 } 8001 8002 /* 8003 * Release blocks associated with the freeblks and stored in the indirect 8004 * block dbn. If level is greater than SINGLE, the block is an indirect block 8005 * and recursive calls to indirtrunc must be used to cleanse other indirect 8006 * blocks. 8007 * 8008 * This handles partial and complete truncation of blocks. Partial is noted 8009 * with goingaway == 0. In this case the freework is completed after the 8010 * zero'd indirects are written to disk. For full truncation the freework 8011 * is completed after the block is freed. 8012 */ 8013 static void 8014 indir_trunc(freework, dbn, lbn) 8015 struct freework *freework; 8016 ufs2_daddr_t dbn; 8017 ufs_lbn_t lbn; 8018 { 8019 struct freework *nfreework; 8020 struct workhead wkhd; 8021 struct freeblks *freeblks; 8022 struct buf *bp; 8023 struct fs *fs; 8024 struct indirdep *indirdep; 8025 struct ufsmount *ump; 8026 ufs1_daddr_t *bap1 = 0; 8027 ufs2_daddr_t nb, nnb, *bap2 = 0; 8028 ufs_lbn_t lbnadd, nlbn; 8029 int i, nblocks, ufs1fmt; 8030 int freedblocks; 8031 int goingaway; 8032 int freedeps; 8033 int needj; 8034 int level; 8035 int cnt; 8036 8037 freeblks = freework->fw_freeblks; 8038 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 8039 fs = ump->um_fs; 8040 /* 8041 * Get buffer of block pointers to be freed. There are three cases: 8042 * 8043 * 1) Partial truncate caches the indirdep pointer in the freework 8044 * which provides us a back copy to the save bp which holds the 8045 * pointers we want to clear. When this completes the zero 8046 * pointers are written to the real copy. 8047 * 2) The indirect is being completely truncated, cancel_indirdep() 8048 * eliminated the real copy and placed the indirdep on the saved 8049 * copy. The indirdep and buf are discarded when this completes. 8050 * 3) The indirect was not in memory, we read a copy off of the disk 8051 * using the devvp and drop and invalidate the buffer when we're 8052 * done. 8053 */ 8054 goingaway = 1; 8055 indirdep = NULL; 8056 if (freework->fw_indir != NULL) { 8057 goingaway = 0; 8058 indirdep = freework->fw_indir; 8059 bp = indirdep->ir_savebp; 8060 if (bp == NULL || bp->b_blkno != dbn) 8061 panic("indir_trunc: Bad saved buf %p blkno %jd", 8062 bp, (intmax_t)dbn); 8063 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 8064 /* 8065 * The lock prevents the buf dep list from changing and 8066 * indirects on devvp should only ever have one dependency. 8067 */ 8068 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 8069 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 8070 panic("indir_trunc: Bad indirdep %p from buf %p", 8071 indirdep, bp); 8072 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 8073 NOCRED, &bp) != 0) { 8074 brelse(bp); 8075 return; 8076 } 8077 ACQUIRE_LOCK(ump); 8078 /* Protects against a race with complete_trunc_indir(). */ 8079 freework->fw_state &= ~INPROGRESS; 8080 /* 8081 * If we have an indirdep we need to enforce the truncation order 8082 * and discard it when it is complete. 8083 */ 8084 if (indirdep) { 8085 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 8086 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 8087 /* 8088 * Add the complete truncate to the list on the 8089 * indirdep to enforce in-order processing. 8090 */ 8091 if (freework->fw_indir == NULL) 8092 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 8093 freework, fw_next); 8094 FREE_LOCK(ump); 8095 return; 8096 } 8097 /* 8098 * If we're goingaway, free the indirdep. Otherwise it will 8099 * linger until the write completes. 8100 */ 8101 if (goingaway) 8102 free_indirdep(indirdep); 8103 } 8104 FREE_LOCK(ump); 8105 /* Initialize pointers depending on block size. */ 8106 if (ump->um_fstype == UFS1) { 8107 bap1 = (ufs1_daddr_t *)bp->b_data; 8108 nb = bap1[freework->fw_off]; 8109 ufs1fmt = 1; 8110 } else { 8111 bap2 = (ufs2_daddr_t *)bp->b_data; 8112 nb = bap2[freework->fw_off]; 8113 ufs1fmt = 0; 8114 } 8115 level = lbn_level(lbn); 8116 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 8117 lbnadd = lbn_offset(fs, level); 8118 nblocks = btodb(fs->fs_bsize); 8119 nfreework = freework; 8120 freedeps = 0; 8121 cnt = 0; 8122 /* 8123 * Reclaim blocks. Traverses into nested indirect levels and 8124 * arranges for the current level to be freed when subordinates 8125 * are free when journaling. 8126 */ 8127 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 8128 if (i != NINDIR(fs) - 1) { 8129 if (ufs1fmt) 8130 nnb = bap1[i+1]; 8131 else 8132 nnb = bap2[i+1]; 8133 } else 8134 nnb = 0; 8135 if (nb == 0) 8136 continue; 8137 cnt++; 8138 if (level != 0) { 8139 nlbn = (lbn + 1) - (i * lbnadd); 8140 if (needj != 0) { 8141 nfreework = newfreework(ump, freeblks, freework, 8142 nlbn, nb, fs->fs_frag, 0, 0); 8143 freedeps++; 8144 } 8145 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 8146 } else { 8147 struct freedep *freedep; 8148 8149 /* 8150 * Attempt to aggregate freedep dependencies for 8151 * all blocks being released to the same CG. 8152 */ 8153 LIST_INIT(&wkhd); 8154 if (needj != 0 && 8155 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 8156 freedep = newfreedep(freework); 8157 WORKLIST_INSERT_UNLOCKED(&wkhd, 8158 &freedep->fd_list); 8159 freedeps++; 8160 } 8161 CTR3(KTR_SUJ, 8162 "indir_trunc: ino %d blkno %jd size %ld", 8163 freeblks->fb_inum, nb, fs->fs_bsize); 8164 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 8165 fs->fs_bsize, freeblks->fb_inum, 8166 freeblks->fb_vtype, &wkhd); 8167 } 8168 } 8169 if (goingaway) { 8170 bp->b_flags |= B_INVAL | B_NOCACHE; 8171 brelse(bp); 8172 } 8173 freedblocks = 0; 8174 if (level == 0) 8175 freedblocks = (nblocks * cnt); 8176 if (needj == 0) 8177 freedblocks += nblocks; 8178 freeblks_free(ump, freeblks, freedblocks); 8179 /* 8180 * If we are journaling set up the ref counts and offset so this 8181 * indirect can be completed when its children are free. 8182 */ 8183 if (needj) { 8184 ACQUIRE_LOCK(ump); 8185 freework->fw_off = i; 8186 freework->fw_ref += freedeps; 8187 freework->fw_ref -= NINDIR(fs) + 1; 8188 if (level == 0) 8189 freeblks->fb_cgwait += freedeps; 8190 if (freework->fw_ref == 0) 8191 freework_freeblock(freework); 8192 FREE_LOCK(ump); 8193 return; 8194 } 8195 /* 8196 * If we're not journaling we can free the indirect now. 8197 */ 8198 dbn = dbtofsb(fs, dbn); 8199 CTR3(KTR_SUJ, 8200 "indir_trunc 2: ino %d blkno %jd size %ld", 8201 freeblks->fb_inum, dbn, fs->fs_bsize); 8202 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 8203 freeblks->fb_inum, freeblks->fb_vtype, NULL); 8204 /* Non SUJ softdep does single-threaded truncations. */ 8205 if (freework->fw_blkno == dbn) { 8206 freework->fw_state |= ALLCOMPLETE; 8207 ACQUIRE_LOCK(ump); 8208 handle_written_freework(freework); 8209 FREE_LOCK(ump); 8210 } 8211 return; 8212 } 8213 8214 /* 8215 * Cancel an allocindir when it is removed via truncation. When bp is not 8216 * NULL the indirect never appeared on disk and is scheduled to be freed 8217 * independently of the indir so we can more easily track journal work. 8218 */ 8219 static void 8220 cancel_allocindir(aip, bp, freeblks, trunc) 8221 struct allocindir *aip; 8222 struct buf *bp; 8223 struct freeblks *freeblks; 8224 int trunc; 8225 { 8226 struct indirdep *indirdep; 8227 struct freefrag *freefrag; 8228 struct newblk *newblk; 8229 8230 newblk = (struct newblk *)aip; 8231 LIST_REMOVE(aip, ai_next); 8232 /* 8233 * We must eliminate the pointer in bp if it must be freed on its 8234 * own due to partial truncate or pending journal work. 8235 */ 8236 if (bp && (trunc || newblk->nb_jnewblk)) { 8237 /* 8238 * Clear the pointer and mark the aip to be freed 8239 * directly if it never existed on disk. 8240 */ 8241 aip->ai_state |= DELAYEDFREE; 8242 indirdep = aip->ai_indirdep; 8243 if (indirdep->ir_state & UFS1FMT) 8244 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8245 else 8246 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8247 } 8248 /* 8249 * When truncating the previous pointer will be freed via 8250 * savedbp. Eliminate the freefrag which would dup free. 8251 */ 8252 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8253 newblk->nb_freefrag = NULL; 8254 if (freefrag->ff_jdep) 8255 cancel_jfreefrag( 8256 WK_JFREEFRAG(freefrag->ff_jdep)); 8257 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8258 WORKITEM_FREE(freefrag, D_FREEFRAG); 8259 } 8260 /* 8261 * If the journal hasn't been written the jnewblk must be passed 8262 * to the call to ffs_blkfree that reclaims the space. We accomplish 8263 * this by leaving the journal dependency on the newblk to be freed 8264 * when a freework is created in handle_workitem_freeblocks(). 8265 */ 8266 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8267 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8268 } 8269 8270 /* 8271 * Create the mkdir dependencies for . and .. in a new directory. Link them 8272 * in to a newdirblk so any subsequent additions are tracked properly. The 8273 * caller is responsible for adding the mkdir1 dependency to the journal 8274 * and updating id_mkdiradd. This function returns with the per-filesystem 8275 * lock held. 8276 */ 8277 static struct mkdir * 8278 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8279 struct diradd *dap; 8280 ino_t newinum; 8281 ino_t dinum; 8282 struct buf *newdirbp; 8283 struct mkdir **mkdirp; 8284 { 8285 struct newblk *newblk; 8286 struct pagedep *pagedep; 8287 struct inodedep *inodedep; 8288 struct newdirblk *newdirblk = 0; 8289 struct mkdir *mkdir1, *mkdir2; 8290 struct worklist *wk; 8291 struct jaddref *jaddref; 8292 struct ufsmount *ump; 8293 struct mount *mp; 8294 8295 mp = dap->da_list.wk_mp; 8296 ump = VFSTOUFS(mp); 8297 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8298 M_SOFTDEP_FLAGS); 8299 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8300 LIST_INIT(&newdirblk->db_mkdir); 8301 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8302 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8303 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8304 mkdir1->md_diradd = dap; 8305 mkdir1->md_jaddref = NULL; 8306 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8307 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8308 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8309 mkdir2->md_diradd = dap; 8310 mkdir2->md_jaddref = NULL; 8311 if (MOUNTEDSUJ(mp) == 0) { 8312 mkdir1->md_state |= DEPCOMPLETE; 8313 mkdir2->md_state |= DEPCOMPLETE; 8314 } 8315 /* 8316 * Dependency on "." and ".." being written to disk. 8317 */ 8318 mkdir1->md_buf = newdirbp; 8319 ACQUIRE_LOCK(VFSTOUFS(mp)); 8320 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir1, md_mkdirs); 8321 /* 8322 * We must link the pagedep, allocdirect, and newdirblk for 8323 * the initial file page so the pointer to the new directory 8324 * is not written until the directory contents are live and 8325 * any subsequent additions are not marked live until the 8326 * block is reachable via the inode. 8327 */ 8328 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8329 panic("setup_newdir: lost pagedep"); 8330 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8331 if (wk->wk_type == D_ALLOCDIRECT) 8332 break; 8333 if (wk == NULL) 8334 panic("setup_newdir: lost allocdirect"); 8335 if (pagedep->pd_state & NEWBLOCK) 8336 panic("setup_newdir: NEWBLOCK already set"); 8337 newblk = WK_NEWBLK(wk); 8338 pagedep->pd_state |= NEWBLOCK; 8339 pagedep->pd_newdirblk = newdirblk; 8340 newdirblk->db_pagedep = pagedep; 8341 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8342 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8343 /* 8344 * Look up the inodedep for the parent directory so that we 8345 * can link mkdir2 into the pending dotdot jaddref or 8346 * the inode write if there is none. If the inode is 8347 * ALLCOMPLETE and no jaddref is present all dependencies have 8348 * been satisfied and mkdir2 can be freed. 8349 */ 8350 inodedep_lookup(mp, dinum, 0, &inodedep); 8351 if (MOUNTEDSUJ(mp)) { 8352 if (inodedep == NULL) 8353 panic("setup_newdir: Lost parent."); 8354 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8355 inoreflst); 8356 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8357 (jaddref->ja_state & MKDIR_PARENT), 8358 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8359 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8360 mkdir2->md_jaddref = jaddref; 8361 jaddref->ja_mkdir = mkdir2; 8362 } else if (inodedep == NULL || 8363 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8364 dap->da_state &= ~MKDIR_PARENT; 8365 WORKITEM_FREE(mkdir2, D_MKDIR); 8366 mkdir2 = NULL; 8367 } else { 8368 LIST_INSERT_HEAD(&ump->softdep_mkdirlisthd, mkdir2, md_mkdirs); 8369 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8370 } 8371 *mkdirp = mkdir2; 8372 8373 return (mkdir1); 8374 } 8375 8376 /* 8377 * Directory entry addition dependencies. 8378 * 8379 * When adding a new directory entry, the inode (with its incremented link 8380 * count) must be written to disk before the directory entry's pointer to it. 8381 * Also, if the inode is newly allocated, the corresponding freemap must be 8382 * updated (on disk) before the directory entry's pointer. These requirements 8383 * are met via undo/redo on the directory entry's pointer, which consists 8384 * simply of the inode number. 8385 * 8386 * As directory entries are added and deleted, the free space within a 8387 * directory block can become fragmented. The ufs filesystem will compact 8388 * a fragmented directory block to make space for a new entry. When this 8389 * occurs, the offsets of previously added entries change. Any "diradd" 8390 * dependency structures corresponding to these entries must be updated with 8391 * the new offsets. 8392 */ 8393 8394 /* 8395 * This routine is called after the in-memory inode's link 8396 * count has been incremented, but before the directory entry's 8397 * pointer to the inode has been set. 8398 */ 8399 int 8400 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8401 struct buf *bp; /* buffer containing directory block */ 8402 struct inode *dp; /* inode for directory */ 8403 off_t diroffset; /* offset of new entry in directory */ 8404 ino_t newinum; /* inode referenced by new directory entry */ 8405 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8406 int isnewblk; /* entry is in a newly allocated block */ 8407 { 8408 int offset; /* offset of new entry within directory block */ 8409 ufs_lbn_t lbn; /* block in directory containing new entry */ 8410 struct fs *fs; 8411 struct diradd *dap; 8412 struct newblk *newblk; 8413 struct pagedep *pagedep; 8414 struct inodedep *inodedep; 8415 struct newdirblk *newdirblk = 0; 8416 struct mkdir *mkdir1, *mkdir2; 8417 struct jaddref *jaddref; 8418 struct ufsmount *ump; 8419 struct mount *mp; 8420 int isindir; 8421 8422 ump = dp->i_ump; 8423 mp = UFSTOVFS(ump); 8424 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8425 ("softdep_setup_directory_add called on non-softdep filesystem")); 8426 /* 8427 * Whiteouts have no dependencies. 8428 */ 8429 if (newinum == WINO) { 8430 if (newdirbp != NULL) 8431 bdwrite(newdirbp); 8432 return (0); 8433 } 8434 jaddref = NULL; 8435 mkdir1 = mkdir2 = NULL; 8436 fs = dp->i_fs; 8437 lbn = lblkno(fs, diroffset); 8438 offset = blkoff(fs, diroffset); 8439 dap = malloc(sizeof(struct diradd), M_DIRADD, 8440 M_SOFTDEP_FLAGS|M_ZERO); 8441 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8442 dap->da_offset = offset; 8443 dap->da_newinum = newinum; 8444 dap->da_state = ATTACHED; 8445 LIST_INIT(&dap->da_jwork); 8446 isindir = bp->b_lblkno >= NDADDR; 8447 if (isnewblk && 8448 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8449 newdirblk = malloc(sizeof(struct newdirblk), 8450 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8451 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8452 LIST_INIT(&newdirblk->db_mkdir); 8453 } 8454 /* 8455 * If we're creating a new directory setup the dependencies and set 8456 * the dap state to wait for them. Otherwise it's COMPLETE and 8457 * we can move on. 8458 */ 8459 if (newdirbp == NULL) { 8460 dap->da_state |= DEPCOMPLETE; 8461 ACQUIRE_LOCK(ump); 8462 } else { 8463 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8464 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8465 &mkdir2); 8466 } 8467 /* 8468 * Link into parent directory pagedep to await its being written. 8469 */ 8470 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8471 #ifdef DEBUG 8472 if (diradd_lookup(pagedep, offset) != NULL) 8473 panic("softdep_setup_directory_add: %p already at off %d\n", 8474 diradd_lookup(pagedep, offset), offset); 8475 #endif 8476 dap->da_pagedep = pagedep; 8477 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8478 da_pdlist); 8479 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 8480 /* 8481 * If we're journaling, link the diradd into the jaddref so it 8482 * may be completed after the journal entry is written. Otherwise, 8483 * link the diradd into its inodedep. If the inode is not yet 8484 * written place it on the bufwait list, otherwise do the post-inode 8485 * write processing to put it on the id_pendinghd list. 8486 */ 8487 if (MOUNTEDSUJ(mp)) { 8488 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8489 inoreflst); 8490 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8491 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8492 jaddref->ja_diroff = diroffset; 8493 jaddref->ja_diradd = dap; 8494 add_to_journal(&jaddref->ja_list); 8495 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8496 diradd_inode_written(dap, inodedep); 8497 else 8498 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8499 /* 8500 * Add the journal entries for . and .. links now that the primary 8501 * link is written. 8502 */ 8503 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8504 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8505 inoreflst, if_deps); 8506 KASSERT(jaddref != NULL && 8507 jaddref->ja_ino == jaddref->ja_parent && 8508 (jaddref->ja_state & MKDIR_BODY), 8509 ("softdep_setup_directory_add: bad dot jaddref %p", 8510 jaddref)); 8511 mkdir1->md_jaddref = jaddref; 8512 jaddref->ja_mkdir = mkdir1; 8513 /* 8514 * It is important that the dotdot journal entry 8515 * is added prior to the dot entry since dot writes 8516 * both the dot and dotdot links. These both must 8517 * be added after the primary link for the journal 8518 * to remain consistent. 8519 */ 8520 add_to_journal(&mkdir2->md_jaddref->ja_list); 8521 add_to_journal(&jaddref->ja_list); 8522 } 8523 /* 8524 * If we are adding a new directory remember this diradd so that if 8525 * we rename it we can keep the dot and dotdot dependencies. If 8526 * we are adding a new name for an inode that has a mkdiradd we 8527 * must be in rename and we have to move the dot and dotdot 8528 * dependencies to this new name. The old name is being orphaned 8529 * soon. 8530 */ 8531 if (mkdir1 != NULL) { 8532 if (inodedep->id_mkdiradd != NULL) 8533 panic("softdep_setup_directory_add: Existing mkdir"); 8534 inodedep->id_mkdiradd = dap; 8535 } else if (inodedep->id_mkdiradd) 8536 merge_diradd(inodedep, dap); 8537 if (newdirblk) { 8538 /* 8539 * There is nothing to do if we are already tracking 8540 * this block. 8541 */ 8542 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8543 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8544 FREE_LOCK(ump); 8545 return (0); 8546 } 8547 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8548 == 0) 8549 panic("softdep_setup_directory_add: lost entry"); 8550 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8551 pagedep->pd_state |= NEWBLOCK; 8552 pagedep->pd_newdirblk = newdirblk; 8553 newdirblk->db_pagedep = pagedep; 8554 FREE_LOCK(ump); 8555 /* 8556 * If we extended into an indirect signal direnter to sync. 8557 */ 8558 if (isindir) 8559 return (1); 8560 return (0); 8561 } 8562 FREE_LOCK(ump); 8563 return (0); 8564 } 8565 8566 /* 8567 * This procedure is called to change the offset of a directory 8568 * entry when compacting a directory block which must be owned 8569 * exclusively by the caller. Note that the actual entry movement 8570 * must be done in this procedure to ensure that no I/O completions 8571 * occur while the move is in progress. 8572 */ 8573 void 8574 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8575 struct buf *bp; /* Buffer holding directory block. */ 8576 struct inode *dp; /* inode for directory */ 8577 caddr_t base; /* address of dp->i_offset */ 8578 caddr_t oldloc; /* address of old directory location */ 8579 caddr_t newloc; /* address of new directory location */ 8580 int entrysize; /* size of directory entry */ 8581 { 8582 int offset, oldoffset, newoffset; 8583 struct pagedep *pagedep; 8584 struct jmvref *jmvref; 8585 struct diradd *dap; 8586 struct direct *de; 8587 struct mount *mp; 8588 ufs_lbn_t lbn; 8589 int flags; 8590 8591 mp = UFSTOVFS(dp->i_ump); 8592 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 8593 ("softdep_change_directoryentry_offset called on " 8594 "non-softdep filesystem")); 8595 de = (struct direct *)oldloc; 8596 jmvref = NULL; 8597 flags = 0; 8598 /* 8599 * Moves are always journaled as it would be too complex to 8600 * determine if any affected adds or removes are present in the 8601 * journal. 8602 */ 8603 if (MOUNTEDSUJ(mp)) { 8604 flags = DEPALLOC; 8605 jmvref = newjmvref(dp, de->d_ino, 8606 dp->i_offset + (oldloc - base), 8607 dp->i_offset + (newloc - base)); 8608 } 8609 lbn = lblkno(dp->i_fs, dp->i_offset); 8610 offset = blkoff(dp->i_fs, dp->i_offset); 8611 oldoffset = offset + (oldloc - base); 8612 newoffset = offset + (newloc - base); 8613 ACQUIRE_LOCK(dp->i_ump); 8614 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8615 goto done; 8616 dap = diradd_lookup(pagedep, oldoffset); 8617 if (dap) { 8618 dap->da_offset = newoffset; 8619 newoffset = DIRADDHASH(newoffset); 8620 oldoffset = DIRADDHASH(oldoffset); 8621 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8622 newoffset != oldoffset) { 8623 LIST_REMOVE(dap, da_pdlist); 8624 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8625 dap, da_pdlist); 8626 } 8627 } 8628 done: 8629 if (jmvref) { 8630 jmvref->jm_pagedep = pagedep; 8631 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8632 add_to_journal(&jmvref->jm_list); 8633 } 8634 bcopy(oldloc, newloc, entrysize); 8635 FREE_LOCK(dp->i_ump); 8636 } 8637 8638 /* 8639 * Move the mkdir dependencies and journal work from one diradd to another 8640 * when renaming a directory. The new name must depend on the mkdir deps 8641 * completing as the old name did. Directories can only have one valid link 8642 * at a time so one must be canonical. 8643 */ 8644 static void 8645 merge_diradd(inodedep, newdap) 8646 struct inodedep *inodedep; 8647 struct diradd *newdap; 8648 { 8649 struct diradd *olddap; 8650 struct mkdir *mkdir, *nextmd; 8651 struct ufsmount *ump; 8652 short state; 8653 8654 olddap = inodedep->id_mkdiradd; 8655 inodedep->id_mkdiradd = newdap; 8656 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8657 newdap->da_state &= ~DEPCOMPLETE; 8658 ump = VFSTOUFS(inodedep->id_list.wk_mp); 8659 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8660 mkdir = nextmd) { 8661 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8662 if (mkdir->md_diradd != olddap) 8663 continue; 8664 mkdir->md_diradd = newdap; 8665 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8666 newdap->da_state |= state; 8667 olddap->da_state &= ~state; 8668 if ((olddap->da_state & 8669 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8670 break; 8671 } 8672 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8673 panic("merge_diradd: unfound ref"); 8674 } 8675 /* 8676 * Any mkdir related journal items are not safe to be freed until 8677 * the new name is stable. 8678 */ 8679 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8680 olddap->da_state |= DEPCOMPLETE; 8681 complete_diradd(olddap); 8682 } 8683 8684 /* 8685 * Move the diradd to the pending list when all diradd dependencies are 8686 * complete. 8687 */ 8688 static void 8689 complete_diradd(dap) 8690 struct diradd *dap; 8691 { 8692 struct pagedep *pagedep; 8693 8694 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8695 if (dap->da_state & DIRCHG) 8696 pagedep = dap->da_previous->dm_pagedep; 8697 else 8698 pagedep = dap->da_pagedep; 8699 LIST_REMOVE(dap, da_pdlist); 8700 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8701 } 8702 } 8703 8704 /* 8705 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8706 * add entries and conditonally journal the remove. 8707 */ 8708 static void 8709 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8710 struct diradd *dap; 8711 struct dirrem *dirrem; 8712 struct jremref *jremref; 8713 struct jremref *dotremref; 8714 struct jremref *dotdotremref; 8715 { 8716 struct inodedep *inodedep; 8717 struct jaddref *jaddref; 8718 struct inoref *inoref; 8719 struct ufsmount *ump; 8720 struct mkdir *mkdir; 8721 8722 /* 8723 * If no remove references were allocated we're on a non-journaled 8724 * filesystem and can skip the cancel step. 8725 */ 8726 if (jremref == NULL) { 8727 free_diradd(dap, NULL); 8728 return; 8729 } 8730 /* 8731 * Cancel the primary name an free it if it does not require 8732 * journaling. 8733 */ 8734 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8735 0, &inodedep) != 0) { 8736 /* Abort the addref that reference this diradd. */ 8737 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8738 if (inoref->if_list.wk_type != D_JADDREF) 8739 continue; 8740 jaddref = (struct jaddref *)inoref; 8741 if (jaddref->ja_diradd != dap) 8742 continue; 8743 if (cancel_jaddref(jaddref, inodedep, 8744 &dirrem->dm_jwork) == 0) { 8745 free_jremref(jremref); 8746 jremref = NULL; 8747 } 8748 break; 8749 } 8750 } 8751 /* 8752 * Cancel subordinate names and free them if they do not require 8753 * journaling. 8754 */ 8755 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8756 ump = VFSTOUFS(dap->da_list.wk_mp); 8757 LIST_FOREACH(mkdir, &ump->softdep_mkdirlisthd, md_mkdirs) { 8758 if (mkdir->md_diradd != dap) 8759 continue; 8760 if ((jaddref = mkdir->md_jaddref) == NULL) 8761 continue; 8762 mkdir->md_jaddref = NULL; 8763 if (mkdir->md_state & MKDIR_PARENT) { 8764 if (cancel_jaddref(jaddref, NULL, 8765 &dirrem->dm_jwork) == 0) { 8766 free_jremref(dotdotremref); 8767 dotdotremref = NULL; 8768 } 8769 } else { 8770 if (cancel_jaddref(jaddref, inodedep, 8771 &dirrem->dm_jwork) == 0) { 8772 free_jremref(dotremref); 8773 dotremref = NULL; 8774 } 8775 } 8776 } 8777 } 8778 8779 if (jremref) 8780 journal_jremref(dirrem, jremref, inodedep); 8781 if (dotremref) 8782 journal_jremref(dirrem, dotremref, inodedep); 8783 if (dotdotremref) 8784 journal_jremref(dirrem, dotdotremref, NULL); 8785 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8786 free_diradd(dap, &dirrem->dm_jwork); 8787 } 8788 8789 /* 8790 * Free a diradd dependency structure. This routine must be called 8791 * with splbio interrupts blocked. 8792 */ 8793 static void 8794 free_diradd(dap, wkhd) 8795 struct diradd *dap; 8796 struct workhead *wkhd; 8797 { 8798 struct dirrem *dirrem; 8799 struct pagedep *pagedep; 8800 struct inodedep *inodedep; 8801 struct mkdir *mkdir, *nextmd; 8802 struct ufsmount *ump; 8803 8804 ump = VFSTOUFS(dap->da_list.wk_mp); 8805 LOCK_OWNED(ump); 8806 LIST_REMOVE(dap, da_pdlist); 8807 if (dap->da_state & ONWORKLIST) 8808 WORKLIST_REMOVE(&dap->da_list); 8809 if ((dap->da_state & DIRCHG) == 0) { 8810 pagedep = dap->da_pagedep; 8811 } else { 8812 dirrem = dap->da_previous; 8813 pagedep = dirrem->dm_pagedep; 8814 dirrem->dm_dirinum = pagedep->pd_ino; 8815 dirrem->dm_state |= COMPLETE; 8816 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8817 add_to_worklist(&dirrem->dm_list, 0); 8818 } 8819 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8820 0, &inodedep) != 0) 8821 if (inodedep->id_mkdiradd == dap) 8822 inodedep->id_mkdiradd = NULL; 8823 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8824 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 8825 mkdir = nextmd) { 8826 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8827 if (mkdir->md_diradd != dap) 8828 continue; 8829 dap->da_state &= 8830 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8831 LIST_REMOVE(mkdir, md_mkdirs); 8832 if (mkdir->md_state & ONWORKLIST) 8833 WORKLIST_REMOVE(&mkdir->md_list); 8834 if (mkdir->md_jaddref != NULL) 8835 panic("free_diradd: Unexpected jaddref"); 8836 WORKITEM_FREE(mkdir, D_MKDIR); 8837 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8838 break; 8839 } 8840 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8841 panic("free_diradd: unfound ref"); 8842 } 8843 if (inodedep) 8844 free_inodedep(inodedep); 8845 /* 8846 * Free any journal segments waiting for the directory write. 8847 */ 8848 handle_jwork(&dap->da_jwork); 8849 WORKITEM_FREE(dap, D_DIRADD); 8850 } 8851 8852 /* 8853 * Directory entry removal dependencies. 8854 * 8855 * When removing a directory entry, the entry's inode pointer must be 8856 * zero'ed on disk before the corresponding inode's link count is decremented 8857 * (possibly freeing the inode for re-use). This dependency is handled by 8858 * updating the directory entry but delaying the inode count reduction until 8859 * after the directory block has been written to disk. After this point, the 8860 * inode count can be decremented whenever it is convenient. 8861 */ 8862 8863 /* 8864 * This routine should be called immediately after removing 8865 * a directory entry. The inode's link count should not be 8866 * decremented by the calling procedure -- the soft updates 8867 * code will do this task when it is safe. 8868 */ 8869 void 8870 softdep_setup_remove(bp, dp, ip, isrmdir) 8871 struct buf *bp; /* buffer containing directory block */ 8872 struct inode *dp; /* inode for the directory being modified */ 8873 struct inode *ip; /* inode for directory entry being removed */ 8874 int isrmdir; /* indicates if doing RMDIR */ 8875 { 8876 struct dirrem *dirrem, *prevdirrem; 8877 struct inodedep *inodedep; 8878 int direct; 8879 8880 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 8881 ("softdep_setup_remove called on non-softdep filesystem")); 8882 /* 8883 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8884 * newdirrem() to setup the full directory remove which requires 8885 * isrmdir > 1. 8886 */ 8887 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8888 /* 8889 * Add the dirrem to the inodedep's pending remove list for quick 8890 * discovery later. 8891 */ 8892 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8893 &inodedep) == 0) 8894 panic("softdep_setup_remove: Lost inodedep."); 8895 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8896 dirrem->dm_state |= ONDEPLIST; 8897 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8898 8899 /* 8900 * If the COMPLETE flag is clear, then there were no active 8901 * entries and we want to roll back to a zeroed entry until 8902 * the new inode is committed to disk. If the COMPLETE flag is 8903 * set then we have deleted an entry that never made it to 8904 * disk. If the entry we deleted resulted from a name change, 8905 * then the old name still resides on disk. We cannot delete 8906 * its inode (returned to us in prevdirrem) until the zeroed 8907 * directory entry gets to disk. The new inode has never been 8908 * referenced on the disk, so can be deleted immediately. 8909 */ 8910 if ((dirrem->dm_state & COMPLETE) == 0) { 8911 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 8912 dm_next); 8913 FREE_LOCK(ip->i_ump); 8914 } else { 8915 if (prevdirrem != NULL) 8916 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 8917 prevdirrem, dm_next); 8918 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 8919 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 8920 FREE_LOCK(ip->i_ump); 8921 if (direct) 8922 handle_workitem_remove(dirrem, 0); 8923 } 8924 } 8925 8926 /* 8927 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 8928 * pd_pendinghd list of a pagedep. 8929 */ 8930 static struct diradd * 8931 diradd_lookup(pagedep, offset) 8932 struct pagedep *pagedep; 8933 int offset; 8934 { 8935 struct diradd *dap; 8936 8937 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 8938 if (dap->da_offset == offset) 8939 return (dap); 8940 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 8941 if (dap->da_offset == offset) 8942 return (dap); 8943 return (NULL); 8944 } 8945 8946 /* 8947 * Search for a .. diradd dependency in a directory that is being removed. 8948 * If the directory was renamed to a new parent we have a diradd rather 8949 * than a mkdir for the .. entry. We need to cancel it now before 8950 * it is found in truncate(). 8951 */ 8952 static struct jremref * 8953 cancel_diradd_dotdot(ip, dirrem, jremref) 8954 struct inode *ip; 8955 struct dirrem *dirrem; 8956 struct jremref *jremref; 8957 { 8958 struct pagedep *pagedep; 8959 struct diradd *dap; 8960 struct worklist *wk; 8961 8962 if (pagedep_lookup(UFSTOVFS(ip->i_ump), NULL, ip->i_number, 0, 0, 8963 &pagedep) == 0) 8964 return (jremref); 8965 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 8966 if (dap == NULL) 8967 return (jremref); 8968 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 8969 /* 8970 * Mark any journal work as belonging to the parent so it is freed 8971 * with the .. reference. 8972 */ 8973 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 8974 wk->wk_state |= MKDIR_PARENT; 8975 return (NULL); 8976 } 8977 8978 /* 8979 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 8980 * replace it with a dirrem/diradd pair as a result of re-parenting a 8981 * directory. This ensures that we don't simultaneously have a mkdir and 8982 * a diradd for the same .. entry. 8983 */ 8984 static struct jremref * 8985 cancel_mkdir_dotdot(ip, dirrem, jremref) 8986 struct inode *ip; 8987 struct dirrem *dirrem; 8988 struct jremref *jremref; 8989 { 8990 struct inodedep *inodedep; 8991 struct jaddref *jaddref; 8992 struct ufsmount *ump; 8993 struct mkdir *mkdir; 8994 struct diradd *dap; 8995 8996 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8997 &inodedep) == 0) 8998 return (jremref); 8999 dap = inodedep->id_mkdiradd; 9000 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 9001 return (jremref); 9002 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9003 for (mkdir = LIST_FIRST(&ump->softdep_mkdirlisthd); mkdir; 9004 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 9005 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 9006 break; 9007 if (mkdir == NULL) 9008 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 9009 if ((jaddref = mkdir->md_jaddref) != NULL) { 9010 mkdir->md_jaddref = NULL; 9011 jaddref->ja_state &= ~MKDIR_PARENT; 9012 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0, 9013 &inodedep) == 0) 9014 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 9015 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 9016 journal_jremref(dirrem, jremref, inodedep); 9017 jremref = NULL; 9018 } 9019 } 9020 if (mkdir->md_state & ONWORKLIST) 9021 WORKLIST_REMOVE(&mkdir->md_list); 9022 mkdir->md_state |= ALLCOMPLETE; 9023 complete_mkdir(mkdir); 9024 return (jremref); 9025 } 9026 9027 static void 9028 journal_jremref(dirrem, jremref, inodedep) 9029 struct dirrem *dirrem; 9030 struct jremref *jremref; 9031 struct inodedep *inodedep; 9032 { 9033 9034 if (inodedep == NULL) 9035 if (inodedep_lookup(jremref->jr_list.wk_mp, 9036 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 9037 panic("journal_jremref: Lost inodedep"); 9038 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 9039 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 9040 add_to_journal(&jremref->jr_list); 9041 } 9042 9043 static void 9044 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 9045 struct dirrem *dirrem; 9046 struct jremref *jremref; 9047 struct jremref *dotremref; 9048 struct jremref *dotdotremref; 9049 { 9050 struct inodedep *inodedep; 9051 9052 9053 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 9054 &inodedep) == 0) 9055 panic("dirrem_journal: Lost inodedep"); 9056 journal_jremref(dirrem, jremref, inodedep); 9057 if (dotremref) 9058 journal_jremref(dirrem, dotremref, inodedep); 9059 if (dotdotremref) 9060 journal_jremref(dirrem, dotdotremref, NULL); 9061 } 9062 9063 /* 9064 * Allocate a new dirrem if appropriate and return it along with 9065 * its associated pagedep. Called without a lock, returns with lock. 9066 */ 9067 static struct dirrem * 9068 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 9069 struct buf *bp; /* buffer containing directory block */ 9070 struct inode *dp; /* inode for the directory being modified */ 9071 struct inode *ip; /* inode for directory entry being removed */ 9072 int isrmdir; /* indicates if doing RMDIR */ 9073 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 9074 { 9075 int offset; 9076 ufs_lbn_t lbn; 9077 struct diradd *dap; 9078 struct dirrem *dirrem; 9079 struct pagedep *pagedep; 9080 struct jremref *jremref; 9081 struct jremref *dotremref; 9082 struct jremref *dotdotremref; 9083 struct vnode *dvp; 9084 9085 /* 9086 * Whiteouts have no deletion dependencies. 9087 */ 9088 if (ip == NULL) 9089 panic("newdirrem: whiteout"); 9090 dvp = ITOV(dp); 9091 /* 9092 * If the system is over its limit and our filesystem is 9093 * responsible for more than our share of that usage and 9094 * we are not a snapshot, request some inodedep cleanup. 9095 * Limiting the number of dirrem structures will also limit 9096 * the number of freefile and freeblks structures. 9097 */ 9098 ACQUIRE_LOCK(ip->i_ump); 9099 while (!IS_SNAPSHOT(ip) && dep_current[D_DIRREM] > max_softdeps / 2 && 9100 ip->i_ump->softdep_curdeps[D_DIRREM] > 9101 (max_softdeps / 2) / stat_flush_threads) 9102 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_BLOCKS); 9103 FREE_LOCK(ip->i_ump); 9104 dirrem = malloc(sizeof(struct dirrem), 9105 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO); 9106 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 9107 LIST_INIT(&dirrem->dm_jremrefhd); 9108 LIST_INIT(&dirrem->dm_jwork); 9109 dirrem->dm_state = isrmdir ? RMDIR : 0; 9110 dirrem->dm_oldinum = ip->i_number; 9111 *prevdirremp = NULL; 9112 /* 9113 * Allocate remove reference structures to track journal write 9114 * dependencies. We will always have one for the link and 9115 * when doing directories we will always have one more for dot. 9116 * When renaming a directory we skip the dotdot link change so 9117 * this is not needed. 9118 */ 9119 jremref = dotremref = dotdotremref = NULL; 9120 if (DOINGSUJ(dvp)) { 9121 if (isrmdir) { 9122 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9123 ip->i_effnlink + 2); 9124 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 9125 ip->i_effnlink + 1); 9126 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 9127 dp->i_effnlink + 1); 9128 dotdotremref->jr_state |= MKDIR_PARENT; 9129 } else 9130 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 9131 ip->i_effnlink + 1); 9132 } 9133 ACQUIRE_LOCK(ip->i_ump); 9134 lbn = lblkno(dp->i_fs, dp->i_offset); 9135 offset = blkoff(dp->i_fs, dp->i_offset); 9136 pagedep_lookup(UFSTOVFS(dp->i_ump), bp, dp->i_number, lbn, DEPALLOC, 9137 &pagedep); 9138 dirrem->dm_pagedep = pagedep; 9139 dirrem->dm_offset = offset; 9140 /* 9141 * If we're renaming a .. link to a new directory, cancel any 9142 * existing MKDIR_PARENT mkdir. If it has already been canceled 9143 * the jremref is preserved for any potential diradd in this 9144 * location. This can not coincide with a rmdir. 9145 */ 9146 if (dp->i_offset == DOTDOT_OFFSET) { 9147 if (isrmdir) 9148 panic("newdirrem: .. directory change during remove?"); 9149 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 9150 } 9151 /* 9152 * If we're removing a directory search for the .. dependency now and 9153 * cancel it. Any pending journal work will be added to the dirrem 9154 * to be completed when the workitem remove completes. 9155 */ 9156 if (isrmdir) 9157 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 9158 /* 9159 * Check for a diradd dependency for the same directory entry. 9160 * If present, then both dependencies become obsolete and can 9161 * be de-allocated. 9162 */ 9163 dap = diradd_lookup(pagedep, offset); 9164 if (dap == NULL) { 9165 /* 9166 * Link the jremref structures into the dirrem so they are 9167 * written prior to the pagedep. 9168 */ 9169 if (jremref) 9170 dirrem_journal(dirrem, jremref, dotremref, 9171 dotdotremref); 9172 return (dirrem); 9173 } 9174 /* 9175 * Must be ATTACHED at this point. 9176 */ 9177 if ((dap->da_state & ATTACHED) == 0) 9178 panic("newdirrem: not ATTACHED"); 9179 if (dap->da_newinum != ip->i_number) 9180 panic("newdirrem: inum %ju should be %ju", 9181 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 9182 /* 9183 * If we are deleting a changed name that never made it to disk, 9184 * then return the dirrem describing the previous inode (which 9185 * represents the inode currently referenced from this entry on disk). 9186 */ 9187 if ((dap->da_state & DIRCHG) != 0) { 9188 *prevdirremp = dap->da_previous; 9189 dap->da_state &= ~DIRCHG; 9190 dap->da_pagedep = pagedep; 9191 } 9192 /* 9193 * We are deleting an entry that never made it to disk. 9194 * Mark it COMPLETE so we can delete its inode immediately. 9195 */ 9196 dirrem->dm_state |= COMPLETE; 9197 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 9198 #ifdef SUJ_DEBUG 9199 if (isrmdir == 0) { 9200 struct worklist *wk; 9201 9202 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 9203 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 9204 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 9205 } 9206 #endif 9207 9208 return (dirrem); 9209 } 9210 9211 /* 9212 * Directory entry change dependencies. 9213 * 9214 * Changing an existing directory entry requires that an add operation 9215 * be completed first followed by a deletion. The semantics for the addition 9216 * are identical to the description of adding a new entry above except 9217 * that the rollback is to the old inode number rather than zero. Once 9218 * the addition dependency is completed, the removal is done as described 9219 * in the removal routine above. 9220 */ 9221 9222 /* 9223 * This routine should be called immediately after changing 9224 * a directory entry. The inode's link count should not be 9225 * decremented by the calling procedure -- the soft updates 9226 * code will perform this task when it is safe. 9227 */ 9228 void 9229 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 9230 struct buf *bp; /* buffer containing directory block */ 9231 struct inode *dp; /* inode for the directory being modified */ 9232 struct inode *ip; /* inode for directory entry being removed */ 9233 ino_t newinum; /* new inode number for changed entry */ 9234 int isrmdir; /* indicates if doing RMDIR */ 9235 { 9236 int offset; 9237 struct diradd *dap = NULL; 9238 struct dirrem *dirrem, *prevdirrem; 9239 struct pagedep *pagedep; 9240 struct inodedep *inodedep; 9241 struct jaddref *jaddref; 9242 struct mount *mp; 9243 9244 offset = blkoff(dp->i_fs, dp->i_offset); 9245 mp = UFSTOVFS(dp->i_ump); 9246 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 9247 ("softdep_setup_directory_change called on non-softdep filesystem")); 9248 9249 /* 9250 * Whiteouts do not need diradd dependencies. 9251 */ 9252 if (newinum != WINO) { 9253 dap = malloc(sizeof(struct diradd), 9254 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9255 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9256 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9257 dap->da_offset = offset; 9258 dap->da_newinum = newinum; 9259 LIST_INIT(&dap->da_jwork); 9260 } 9261 9262 /* 9263 * Allocate a new dirrem and ACQUIRE_LOCK. 9264 */ 9265 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9266 pagedep = dirrem->dm_pagedep; 9267 /* 9268 * The possible values for isrmdir: 9269 * 0 - non-directory file rename 9270 * 1 - directory rename within same directory 9271 * inum - directory rename to new directory of given inode number 9272 * When renaming to a new directory, we are both deleting and 9273 * creating a new directory entry, so the link count on the new 9274 * directory should not change. Thus we do not need the followup 9275 * dirrem which is usually done in handle_workitem_remove. We set 9276 * the DIRCHG flag to tell handle_workitem_remove to skip the 9277 * followup dirrem. 9278 */ 9279 if (isrmdir > 1) 9280 dirrem->dm_state |= DIRCHG; 9281 9282 /* 9283 * Whiteouts have no additional dependencies, 9284 * so just put the dirrem on the correct list. 9285 */ 9286 if (newinum == WINO) { 9287 if ((dirrem->dm_state & COMPLETE) == 0) { 9288 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9289 dm_next); 9290 } else { 9291 dirrem->dm_dirinum = pagedep->pd_ino; 9292 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9293 add_to_worklist(&dirrem->dm_list, 0); 9294 } 9295 FREE_LOCK(dp->i_ump); 9296 return; 9297 } 9298 /* 9299 * Add the dirrem to the inodedep's pending remove list for quick 9300 * discovery later. A valid nlinkdelta ensures that this lookup 9301 * will not fail. 9302 */ 9303 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9304 panic("softdep_setup_directory_change: Lost inodedep."); 9305 dirrem->dm_state |= ONDEPLIST; 9306 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9307 9308 /* 9309 * If the COMPLETE flag is clear, then there were no active 9310 * entries and we want to roll back to the previous inode until 9311 * the new inode is committed to disk. If the COMPLETE flag is 9312 * set, then we have deleted an entry that never made it to disk. 9313 * If the entry we deleted resulted from a name change, then the old 9314 * inode reference still resides on disk. Any rollback that we do 9315 * needs to be to that old inode (returned to us in prevdirrem). If 9316 * the entry we deleted resulted from a create, then there is 9317 * no entry on the disk, so we want to roll back to zero rather 9318 * than the uncommitted inode. In either of the COMPLETE cases we 9319 * want to immediately free the unwritten and unreferenced inode. 9320 */ 9321 if ((dirrem->dm_state & COMPLETE) == 0) { 9322 dap->da_previous = dirrem; 9323 } else { 9324 if (prevdirrem != NULL) { 9325 dap->da_previous = prevdirrem; 9326 } else { 9327 dap->da_state &= ~DIRCHG; 9328 dap->da_pagedep = pagedep; 9329 } 9330 dirrem->dm_dirinum = pagedep->pd_ino; 9331 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9332 add_to_worklist(&dirrem->dm_list, 0); 9333 } 9334 /* 9335 * Lookup the jaddref for this journal entry. We must finish 9336 * initializing it and make the diradd write dependent on it. 9337 * If we're not journaling, put it on the id_bufwait list if the 9338 * inode is not yet written. If it is written, do the post-inode 9339 * write processing to put it on the id_pendinghd list. 9340 */ 9341 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 9342 if (MOUNTEDSUJ(mp)) { 9343 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9344 inoreflst); 9345 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9346 ("softdep_setup_directory_change: bad jaddref %p", 9347 jaddref)); 9348 jaddref->ja_diroff = dp->i_offset; 9349 jaddref->ja_diradd = dap; 9350 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9351 dap, da_pdlist); 9352 add_to_journal(&jaddref->ja_list); 9353 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9354 dap->da_state |= COMPLETE; 9355 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9356 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9357 } else { 9358 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9359 dap, da_pdlist); 9360 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9361 } 9362 /* 9363 * If we're making a new name for a directory that has not been 9364 * committed when need to move the dot and dotdot references to 9365 * this new name. 9366 */ 9367 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9368 merge_diradd(inodedep, dap); 9369 FREE_LOCK(dp->i_ump); 9370 } 9371 9372 /* 9373 * Called whenever the link count on an inode is changed. 9374 * It creates an inode dependency so that the new reference(s) 9375 * to the inode cannot be committed to disk until the updated 9376 * inode has been written. 9377 */ 9378 void 9379 softdep_change_linkcnt(ip) 9380 struct inode *ip; /* the inode with the increased link count */ 9381 { 9382 struct inodedep *inodedep; 9383 int dflags; 9384 9385 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 9386 ("softdep_change_linkcnt called on non-softdep filesystem")); 9387 ACQUIRE_LOCK(ip->i_ump); 9388 dflags = DEPALLOC; 9389 if (IS_SNAPSHOT(ip)) 9390 dflags |= NODELAY; 9391 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 9392 if (ip->i_nlink < ip->i_effnlink) 9393 panic("softdep_change_linkcnt: bad delta"); 9394 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9395 FREE_LOCK(ip->i_ump); 9396 } 9397 9398 /* 9399 * Attach a sbdep dependency to the superblock buf so that we can keep 9400 * track of the head of the linked list of referenced but unlinked inodes. 9401 */ 9402 void 9403 softdep_setup_sbupdate(ump, fs, bp) 9404 struct ufsmount *ump; 9405 struct fs *fs; 9406 struct buf *bp; 9407 { 9408 struct sbdep *sbdep; 9409 struct worklist *wk; 9410 9411 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 9412 ("softdep_setup_sbupdate called on non-softdep filesystem")); 9413 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9414 if (wk->wk_type == D_SBDEP) 9415 break; 9416 if (wk != NULL) 9417 return; 9418 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9419 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9420 sbdep->sb_fs = fs; 9421 sbdep->sb_ump = ump; 9422 ACQUIRE_LOCK(ump); 9423 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9424 FREE_LOCK(ump); 9425 } 9426 9427 /* 9428 * Return the first unlinked inodedep which is ready to be the head of the 9429 * list. The inodedep and all those after it must have valid next pointers. 9430 */ 9431 static struct inodedep * 9432 first_unlinked_inodedep(ump) 9433 struct ufsmount *ump; 9434 { 9435 struct inodedep *inodedep; 9436 struct inodedep *idp; 9437 9438 LOCK_OWNED(ump); 9439 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9440 inodedep; inodedep = idp) { 9441 if ((inodedep->id_state & UNLINKNEXT) == 0) 9442 return (NULL); 9443 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9444 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9445 break; 9446 if ((inodedep->id_state & UNLINKPREV) == 0) 9447 break; 9448 } 9449 return (inodedep); 9450 } 9451 9452 /* 9453 * Set the sujfree unlinked head pointer prior to writing a superblock. 9454 */ 9455 static void 9456 initiate_write_sbdep(sbdep) 9457 struct sbdep *sbdep; 9458 { 9459 struct inodedep *inodedep; 9460 struct fs *bpfs; 9461 struct fs *fs; 9462 9463 bpfs = sbdep->sb_fs; 9464 fs = sbdep->sb_ump->um_fs; 9465 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9466 if (inodedep) { 9467 fs->fs_sujfree = inodedep->id_ino; 9468 inodedep->id_state |= UNLINKPREV; 9469 } else 9470 fs->fs_sujfree = 0; 9471 bpfs->fs_sujfree = fs->fs_sujfree; 9472 } 9473 9474 /* 9475 * After a superblock is written determine whether it must be written again 9476 * due to a changing unlinked list head. 9477 */ 9478 static int 9479 handle_written_sbdep(sbdep, bp) 9480 struct sbdep *sbdep; 9481 struct buf *bp; 9482 { 9483 struct inodedep *inodedep; 9484 struct mount *mp; 9485 struct fs *fs; 9486 9487 LOCK_OWNED(sbdep->sb_ump); 9488 fs = sbdep->sb_fs; 9489 mp = UFSTOVFS(sbdep->sb_ump); 9490 /* 9491 * If the superblock doesn't match the in-memory list start over. 9492 */ 9493 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9494 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9495 (inodedep == NULL && fs->fs_sujfree != 0)) { 9496 bdirty(bp); 9497 return (1); 9498 } 9499 WORKITEM_FREE(sbdep, D_SBDEP); 9500 if (fs->fs_sujfree == 0) 9501 return (0); 9502 /* 9503 * Now that we have a record of this inode in stable store allow it 9504 * to be written to free up pending work. Inodes may see a lot of 9505 * write activity after they are unlinked which we must not hold up. 9506 */ 9507 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9508 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9509 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9510 inodedep, inodedep->id_state); 9511 if (inodedep->id_state & UNLINKONLIST) 9512 break; 9513 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9514 } 9515 9516 return (0); 9517 } 9518 9519 /* 9520 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9521 */ 9522 static void 9523 unlinked_inodedep(mp, inodedep) 9524 struct mount *mp; 9525 struct inodedep *inodedep; 9526 { 9527 struct ufsmount *ump; 9528 9529 ump = VFSTOUFS(mp); 9530 LOCK_OWNED(ump); 9531 if (MOUNTEDSUJ(mp) == 0) 9532 return; 9533 ump->um_fs->fs_fmod = 1; 9534 if (inodedep->id_state & UNLINKED) 9535 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9536 inodedep->id_state |= UNLINKED; 9537 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9538 } 9539 9540 /* 9541 * Remove an inodedep from the unlinked inodedep list. This may require 9542 * disk writes if the inode has made it that far. 9543 */ 9544 static void 9545 clear_unlinked_inodedep(inodedep) 9546 struct inodedep *inodedep; 9547 { 9548 struct ufsmount *ump; 9549 struct inodedep *idp; 9550 struct inodedep *idn; 9551 struct fs *fs; 9552 struct buf *bp; 9553 ino_t ino; 9554 ino_t nino; 9555 ino_t pino; 9556 int error; 9557 9558 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9559 fs = ump->um_fs; 9560 ino = inodedep->id_ino; 9561 error = 0; 9562 for (;;) { 9563 LOCK_OWNED(ump); 9564 KASSERT((inodedep->id_state & UNLINKED) != 0, 9565 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9566 inodedep)); 9567 /* 9568 * If nothing has yet been written simply remove us from 9569 * the in memory list and return. This is the most common 9570 * case where handle_workitem_remove() loses the final 9571 * reference. 9572 */ 9573 if ((inodedep->id_state & UNLINKLINKS) == 0) 9574 break; 9575 /* 9576 * If we have a NEXT pointer and no PREV pointer we can simply 9577 * clear NEXT's PREV and remove ourselves from the list. Be 9578 * careful not to clear PREV if the superblock points at 9579 * next as well. 9580 */ 9581 idn = TAILQ_NEXT(inodedep, id_unlinked); 9582 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9583 if (idn && fs->fs_sujfree != idn->id_ino) 9584 idn->id_state &= ~UNLINKPREV; 9585 break; 9586 } 9587 /* 9588 * Here we have an inodedep which is actually linked into 9589 * the list. We must remove it by forcing a write to the 9590 * link before us, whether it be the superblock or an inode. 9591 * Unfortunately the list may change while we're waiting 9592 * on the buf lock for either resource so we must loop until 9593 * we lock the right one. If both the superblock and an 9594 * inode point to this inode we must clear the inode first 9595 * followed by the superblock. 9596 */ 9597 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9598 pino = 0; 9599 if (idp && (idp->id_state & UNLINKNEXT)) 9600 pino = idp->id_ino; 9601 FREE_LOCK(ump); 9602 if (pino == 0) { 9603 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9604 (int)fs->fs_sbsize, 0, 0, 0); 9605 } else { 9606 error = bread(ump->um_devvp, 9607 fsbtodb(fs, ino_to_fsba(fs, pino)), 9608 (int)fs->fs_bsize, NOCRED, &bp); 9609 if (error) 9610 brelse(bp); 9611 } 9612 ACQUIRE_LOCK(ump); 9613 if (error) 9614 break; 9615 /* If the list has changed restart the loop. */ 9616 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9617 nino = 0; 9618 if (idp && (idp->id_state & UNLINKNEXT)) 9619 nino = idp->id_ino; 9620 if (nino != pino || 9621 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9622 FREE_LOCK(ump); 9623 brelse(bp); 9624 ACQUIRE_LOCK(ump); 9625 continue; 9626 } 9627 nino = 0; 9628 idn = TAILQ_NEXT(inodedep, id_unlinked); 9629 if (idn) 9630 nino = idn->id_ino; 9631 /* 9632 * Remove us from the in memory list. After this we cannot 9633 * access the inodedep. 9634 */ 9635 KASSERT((inodedep->id_state & UNLINKED) != 0, 9636 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9637 inodedep)); 9638 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9639 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9640 FREE_LOCK(ump); 9641 /* 9642 * The predecessor's next pointer is manually updated here 9643 * so that the NEXT flag is never cleared for an element 9644 * that is in the list. 9645 */ 9646 if (pino == 0) { 9647 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9648 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9649 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9650 bp); 9651 } else if (fs->fs_magic == FS_UFS1_MAGIC) 9652 ((struct ufs1_dinode *)bp->b_data + 9653 ino_to_fsbo(fs, pino))->di_freelink = nino; 9654 else 9655 ((struct ufs2_dinode *)bp->b_data + 9656 ino_to_fsbo(fs, pino))->di_freelink = nino; 9657 /* 9658 * If the bwrite fails we have no recourse to recover. The 9659 * filesystem is corrupted already. 9660 */ 9661 bwrite(bp); 9662 ACQUIRE_LOCK(ump); 9663 /* 9664 * If the superblock pointer still needs to be cleared force 9665 * a write here. 9666 */ 9667 if (fs->fs_sujfree == ino) { 9668 FREE_LOCK(ump); 9669 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9670 (int)fs->fs_sbsize, 0, 0, 0); 9671 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9672 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9673 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9674 bp); 9675 bwrite(bp); 9676 ACQUIRE_LOCK(ump); 9677 } 9678 9679 if (fs->fs_sujfree != ino) 9680 return; 9681 panic("clear_unlinked_inodedep: Failed to clear free head"); 9682 } 9683 if (inodedep->id_ino == fs->fs_sujfree) 9684 panic("clear_unlinked_inodedep: Freeing head of free list"); 9685 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9686 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9687 return; 9688 } 9689 9690 /* 9691 * This workitem decrements the inode's link count. 9692 * If the link count reaches zero, the file is removed. 9693 */ 9694 static int 9695 handle_workitem_remove(dirrem, flags) 9696 struct dirrem *dirrem; 9697 int flags; 9698 { 9699 struct inodedep *inodedep; 9700 struct workhead dotdotwk; 9701 struct worklist *wk; 9702 struct ufsmount *ump; 9703 struct mount *mp; 9704 struct vnode *vp; 9705 struct inode *ip; 9706 ino_t oldinum; 9707 9708 if (dirrem->dm_state & ONWORKLIST) 9709 panic("handle_workitem_remove: dirrem %p still on worklist", 9710 dirrem); 9711 oldinum = dirrem->dm_oldinum; 9712 mp = dirrem->dm_list.wk_mp; 9713 ump = VFSTOUFS(mp); 9714 flags |= LK_EXCLUSIVE; 9715 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9716 return (EBUSY); 9717 ip = VTOI(vp); 9718 ACQUIRE_LOCK(ump); 9719 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9720 panic("handle_workitem_remove: lost inodedep"); 9721 if (dirrem->dm_state & ONDEPLIST) 9722 LIST_REMOVE(dirrem, dm_inonext); 9723 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9724 ("handle_workitem_remove: Journal entries not written.")); 9725 9726 /* 9727 * Move all dependencies waiting on the remove to complete 9728 * from the dirrem to the inode inowait list to be completed 9729 * after the inode has been updated and written to disk. Any 9730 * marked MKDIR_PARENT are saved to be completed when the .. ref 9731 * is removed. 9732 */ 9733 LIST_INIT(&dotdotwk); 9734 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9735 WORKLIST_REMOVE(wk); 9736 if (wk->wk_state & MKDIR_PARENT) { 9737 wk->wk_state &= ~MKDIR_PARENT; 9738 WORKLIST_INSERT(&dotdotwk, wk); 9739 continue; 9740 } 9741 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9742 } 9743 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9744 /* 9745 * Normal file deletion. 9746 */ 9747 if ((dirrem->dm_state & RMDIR) == 0) { 9748 ip->i_nlink--; 9749 DIP_SET(ip, i_nlink, ip->i_nlink); 9750 ip->i_flag |= IN_CHANGE; 9751 if (ip->i_nlink < ip->i_effnlink) 9752 panic("handle_workitem_remove: bad file delta"); 9753 if (ip->i_nlink == 0) 9754 unlinked_inodedep(mp, inodedep); 9755 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9756 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9757 ("handle_workitem_remove: worklist not empty. %s", 9758 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9759 WORKITEM_FREE(dirrem, D_DIRREM); 9760 FREE_LOCK(ump); 9761 goto out; 9762 } 9763 /* 9764 * Directory deletion. Decrement reference count for both the 9765 * just deleted parent directory entry and the reference for ".". 9766 * Arrange to have the reference count on the parent decremented 9767 * to account for the loss of "..". 9768 */ 9769 ip->i_nlink -= 2; 9770 DIP_SET(ip, i_nlink, ip->i_nlink); 9771 ip->i_flag |= IN_CHANGE; 9772 if (ip->i_nlink < ip->i_effnlink) 9773 panic("handle_workitem_remove: bad dir delta"); 9774 if (ip->i_nlink == 0) 9775 unlinked_inodedep(mp, inodedep); 9776 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9777 /* 9778 * Rename a directory to a new parent. Since, we are both deleting 9779 * and creating a new directory entry, the link count on the new 9780 * directory should not change. Thus we skip the followup dirrem. 9781 */ 9782 if (dirrem->dm_state & DIRCHG) { 9783 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9784 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9785 WORKITEM_FREE(dirrem, D_DIRREM); 9786 FREE_LOCK(ump); 9787 goto out; 9788 } 9789 dirrem->dm_state = ONDEPLIST; 9790 dirrem->dm_oldinum = dirrem->dm_dirinum; 9791 /* 9792 * Place the dirrem on the parent's diremhd list. 9793 */ 9794 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9795 panic("handle_workitem_remove: lost dir inodedep"); 9796 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9797 /* 9798 * If the allocated inode has never been written to disk, then 9799 * the on-disk inode is zero'ed and we can remove the file 9800 * immediately. When journaling if the inode has been marked 9801 * unlinked and not DEPCOMPLETE we know it can never be written. 9802 */ 9803 inodedep_lookup(mp, oldinum, 0, &inodedep); 9804 if (inodedep == NULL || 9805 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9806 check_inode_unwritten(inodedep)) { 9807 FREE_LOCK(ump); 9808 vput(vp); 9809 return handle_workitem_remove(dirrem, flags); 9810 } 9811 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9812 FREE_LOCK(ump); 9813 ip->i_flag |= IN_CHANGE; 9814 out: 9815 ffs_update(vp, 0); 9816 vput(vp); 9817 return (0); 9818 } 9819 9820 /* 9821 * Inode de-allocation dependencies. 9822 * 9823 * When an inode's link count is reduced to zero, it can be de-allocated. We 9824 * found it convenient to postpone de-allocation until after the inode is 9825 * written to disk with its new link count (zero). At this point, all of the 9826 * on-disk inode's block pointers are nullified and, with careful dependency 9827 * list ordering, all dependencies related to the inode will be satisfied and 9828 * the corresponding dependency structures de-allocated. So, if/when the 9829 * inode is reused, there will be no mixing of old dependencies with new 9830 * ones. This artificial dependency is set up by the block de-allocation 9831 * procedure above (softdep_setup_freeblocks) and completed by the 9832 * following procedure. 9833 */ 9834 static void 9835 handle_workitem_freefile(freefile) 9836 struct freefile *freefile; 9837 { 9838 struct workhead wkhd; 9839 struct fs *fs; 9840 struct inodedep *idp; 9841 struct ufsmount *ump; 9842 int error; 9843 9844 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9845 fs = ump->um_fs; 9846 #ifdef DEBUG 9847 ACQUIRE_LOCK(ump); 9848 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9849 FREE_LOCK(ump); 9850 if (error) 9851 panic("handle_workitem_freefile: inodedep %p survived", idp); 9852 #endif 9853 UFS_LOCK(ump); 9854 fs->fs_pendinginodes -= 1; 9855 UFS_UNLOCK(ump); 9856 LIST_INIT(&wkhd); 9857 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9858 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9859 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9860 softdep_error("handle_workitem_freefile", error); 9861 ACQUIRE_LOCK(ump); 9862 WORKITEM_FREE(freefile, D_FREEFILE); 9863 FREE_LOCK(ump); 9864 } 9865 9866 9867 /* 9868 * Helper function which unlinks marker element from work list and returns 9869 * the next element on the list. 9870 */ 9871 static __inline struct worklist * 9872 markernext(struct worklist *marker) 9873 { 9874 struct worklist *next; 9875 9876 next = LIST_NEXT(marker, wk_list); 9877 LIST_REMOVE(marker, wk_list); 9878 return next; 9879 } 9880 9881 /* 9882 * Disk writes. 9883 * 9884 * The dependency structures constructed above are most actively used when file 9885 * system blocks are written to disk. No constraints are placed on when a 9886 * block can be written, but unsatisfied update dependencies are made safe by 9887 * modifying (or replacing) the source memory for the duration of the disk 9888 * write. When the disk write completes, the memory block is again brought 9889 * up-to-date. 9890 * 9891 * In-core inode structure reclamation. 9892 * 9893 * Because there are a finite number of "in-core" inode structures, they are 9894 * reused regularly. By transferring all inode-related dependencies to the 9895 * in-memory inode block and indexing them separately (via "inodedep"s), we 9896 * can allow "in-core" inode structures to be reused at any time and avoid 9897 * any increase in contention. 9898 * 9899 * Called just before entering the device driver to initiate a new disk I/O. 9900 * The buffer must be locked, thus, no I/O completion operations can occur 9901 * while we are manipulating its associated dependencies. 9902 */ 9903 static void 9904 softdep_disk_io_initiation(bp) 9905 struct buf *bp; /* structure describing disk write to occur */ 9906 { 9907 struct worklist *wk; 9908 struct worklist marker; 9909 struct inodedep *inodedep; 9910 struct freeblks *freeblks; 9911 struct jblkdep *jblkdep; 9912 struct newblk *newblk; 9913 struct ufsmount *ump; 9914 9915 /* 9916 * We only care about write operations. There should never 9917 * be dependencies for reads. 9918 */ 9919 if (bp->b_iocmd != BIO_WRITE) 9920 panic("softdep_disk_io_initiation: not write"); 9921 9922 if (bp->b_vflags & BV_BKGRDINPROG) 9923 panic("softdep_disk_io_initiation: Writing buffer with " 9924 "background write in progress: %p", bp); 9925 9926 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 9927 return; 9928 ump = VFSTOUFS(wk->wk_mp); 9929 9930 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 9931 PHOLD(curproc); /* Don't swap out kernel stack */ 9932 ACQUIRE_LOCK(ump); 9933 /* 9934 * Do any necessary pre-I/O processing. 9935 */ 9936 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 9937 wk = markernext(&marker)) { 9938 LIST_INSERT_AFTER(wk, &marker, wk_list); 9939 switch (wk->wk_type) { 9940 9941 case D_PAGEDEP: 9942 initiate_write_filepage(WK_PAGEDEP(wk), bp); 9943 continue; 9944 9945 case D_INODEDEP: 9946 inodedep = WK_INODEDEP(wk); 9947 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 9948 initiate_write_inodeblock_ufs1(inodedep, bp); 9949 else 9950 initiate_write_inodeblock_ufs2(inodedep, bp); 9951 continue; 9952 9953 case D_INDIRDEP: 9954 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 9955 continue; 9956 9957 case D_BMSAFEMAP: 9958 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 9959 continue; 9960 9961 case D_JSEG: 9962 WK_JSEG(wk)->js_buf = NULL; 9963 continue; 9964 9965 case D_FREEBLKS: 9966 freeblks = WK_FREEBLKS(wk); 9967 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 9968 /* 9969 * We have to wait for the freeblks to be journaled 9970 * before we can write an inodeblock with updated 9971 * pointers. Be careful to arrange the marker so 9972 * we revisit the freeblks if it's not removed by 9973 * the first jwait(). 9974 */ 9975 if (jblkdep != NULL) { 9976 LIST_REMOVE(&marker, wk_list); 9977 LIST_INSERT_BEFORE(wk, &marker, wk_list); 9978 jwait(&jblkdep->jb_list, MNT_WAIT); 9979 } 9980 continue; 9981 case D_ALLOCDIRECT: 9982 case D_ALLOCINDIR: 9983 /* 9984 * We have to wait for the jnewblk to be journaled 9985 * before we can write to a block if the contents 9986 * may be confused with an earlier file's indirect 9987 * at recovery time. Handle the marker as described 9988 * above. 9989 */ 9990 newblk = WK_NEWBLK(wk); 9991 if (newblk->nb_jnewblk != NULL && 9992 indirblk_lookup(newblk->nb_list.wk_mp, 9993 newblk->nb_newblkno)) { 9994 LIST_REMOVE(&marker, wk_list); 9995 LIST_INSERT_BEFORE(wk, &marker, wk_list); 9996 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 9997 } 9998 continue; 9999 10000 case D_SBDEP: 10001 initiate_write_sbdep(WK_SBDEP(wk)); 10002 continue; 10003 10004 case D_MKDIR: 10005 case D_FREEWORK: 10006 case D_FREEDEP: 10007 case D_JSEGDEP: 10008 continue; 10009 10010 default: 10011 panic("handle_disk_io_initiation: Unexpected type %s", 10012 TYPENAME(wk->wk_type)); 10013 /* NOTREACHED */ 10014 } 10015 } 10016 FREE_LOCK(ump); 10017 PRELE(curproc); /* Allow swapout of kernel stack */ 10018 } 10019 10020 /* 10021 * Called from within the procedure above to deal with unsatisfied 10022 * allocation dependencies in a directory. The buffer must be locked, 10023 * thus, no I/O completion operations can occur while we are 10024 * manipulating its associated dependencies. 10025 */ 10026 static void 10027 initiate_write_filepage(pagedep, bp) 10028 struct pagedep *pagedep; 10029 struct buf *bp; 10030 { 10031 struct jremref *jremref; 10032 struct jmvref *jmvref; 10033 struct dirrem *dirrem; 10034 struct diradd *dap; 10035 struct direct *ep; 10036 int i; 10037 10038 if (pagedep->pd_state & IOSTARTED) { 10039 /* 10040 * This can only happen if there is a driver that does not 10041 * understand chaining. Here biodone will reissue the call 10042 * to strategy for the incomplete buffers. 10043 */ 10044 printf("initiate_write_filepage: already started\n"); 10045 return; 10046 } 10047 pagedep->pd_state |= IOSTARTED; 10048 /* 10049 * Wait for all journal remove dependencies to hit the disk. 10050 * We can not allow any potentially conflicting directory adds 10051 * to be visible before removes and rollback is too difficult. 10052 * The per-filesystem lock may be dropped and re-acquired, however 10053 * we hold the buf locked so the dependency can not go away. 10054 */ 10055 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 10056 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 10057 jwait(&jremref->jr_list, MNT_WAIT); 10058 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 10059 jwait(&jmvref->jm_list, MNT_WAIT); 10060 for (i = 0; i < DAHASHSZ; i++) { 10061 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 10062 ep = (struct direct *) 10063 ((char *)bp->b_data + dap->da_offset); 10064 if (ep->d_ino != dap->da_newinum) 10065 panic("%s: dir inum %ju != new %ju", 10066 "initiate_write_filepage", 10067 (uintmax_t)ep->d_ino, 10068 (uintmax_t)dap->da_newinum); 10069 if (dap->da_state & DIRCHG) 10070 ep->d_ino = dap->da_previous->dm_oldinum; 10071 else 10072 ep->d_ino = 0; 10073 dap->da_state &= ~ATTACHED; 10074 dap->da_state |= UNDONE; 10075 } 10076 } 10077 } 10078 10079 /* 10080 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 10081 * Note that any bug fixes made to this routine must be done in the 10082 * version found below. 10083 * 10084 * Called from within the procedure above to deal with unsatisfied 10085 * allocation dependencies in an inodeblock. The buffer must be 10086 * locked, thus, no I/O completion operations can occur while we 10087 * are manipulating its associated dependencies. 10088 */ 10089 static void 10090 initiate_write_inodeblock_ufs1(inodedep, bp) 10091 struct inodedep *inodedep; 10092 struct buf *bp; /* The inode block */ 10093 { 10094 struct allocdirect *adp, *lastadp; 10095 struct ufs1_dinode *dp; 10096 struct ufs1_dinode *sip; 10097 struct inoref *inoref; 10098 struct ufsmount *ump; 10099 struct fs *fs; 10100 ufs_lbn_t i; 10101 #ifdef INVARIANTS 10102 ufs_lbn_t prevlbn = 0; 10103 #endif 10104 int deplist; 10105 10106 if (inodedep->id_state & IOSTARTED) 10107 panic("initiate_write_inodeblock_ufs1: already started"); 10108 inodedep->id_state |= IOSTARTED; 10109 fs = inodedep->id_fs; 10110 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10111 LOCK_OWNED(ump); 10112 dp = (struct ufs1_dinode *)bp->b_data + 10113 ino_to_fsbo(fs, inodedep->id_ino); 10114 10115 /* 10116 * If we're on the unlinked list but have not yet written our 10117 * next pointer initialize it here. 10118 */ 10119 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10120 struct inodedep *inon; 10121 10122 inon = TAILQ_NEXT(inodedep, id_unlinked); 10123 dp->di_freelink = inon ? inon->id_ino : 0; 10124 } 10125 /* 10126 * If the bitmap is not yet written, then the allocated 10127 * inode cannot be written to disk. 10128 */ 10129 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10130 if (inodedep->id_savedino1 != NULL) 10131 panic("initiate_write_inodeblock_ufs1: I/O underway"); 10132 FREE_LOCK(ump); 10133 sip = malloc(sizeof(struct ufs1_dinode), 10134 M_SAVEDINO, M_SOFTDEP_FLAGS); 10135 ACQUIRE_LOCK(ump); 10136 inodedep->id_savedino1 = sip; 10137 *inodedep->id_savedino1 = *dp; 10138 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 10139 dp->di_gen = inodedep->id_savedino1->di_gen; 10140 dp->di_freelink = inodedep->id_savedino1->di_freelink; 10141 return; 10142 } 10143 /* 10144 * If no dependencies, then there is nothing to roll back. 10145 */ 10146 inodedep->id_savedsize = dp->di_size; 10147 inodedep->id_savedextsize = 0; 10148 inodedep->id_savednlink = dp->di_nlink; 10149 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10150 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10151 return; 10152 /* 10153 * Revert the link count to that of the first unwritten journal entry. 10154 */ 10155 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10156 if (inoref) 10157 dp->di_nlink = inoref->if_nlink; 10158 /* 10159 * Set the dependencies to busy. 10160 */ 10161 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10162 adp = TAILQ_NEXT(adp, ad_next)) { 10163 #ifdef INVARIANTS 10164 if (deplist != 0 && prevlbn >= adp->ad_offset) 10165 panic("softdep_write_inodeblock: lbn order"); 10166 prevlbn = adp->ad_offset; 10167 if (adp->ad_offset < NDADDR && 10168 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10169 panic("%s: direct pointer #%jd mismatch %d != %jd", 10170 "softdep_write_inodeblock", 10171 (intmax_t)adp->ad_offset, 10172 dp->di_db[adp->ad_offset], 10173 (intmax_t)adp->ad_newblkno); 10174 if (adp->ad_offset >= NDADDR && 10175 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10176 panic("%s: indirect pointer #%jd mismatch %d != %jd", 10177 "softdep_write_inodeblock", 10178 (intmax_t)adp->ad_offset - NDADDR, 10179 dp->di_ib[adp->ad_offset - NDADDR], 10180 (intmax_t)adp->ad_newblkno); 10181 deplist |= 1 << adp->ad_offset; 10182 if ((adp->ad_state & ATTACHED) == 0) 10183 panic("softdep_write_inodeblock: Unknown state 0x%x", 10184 adp->ad_state); 10185 #endif /* INVARIANTS */ 10186 adp->ad_state &= ~ATTACHED; 10187 adp->ad_state |= UNDONE; 10188 } 10189 /* 10190 * The on-disk inode cannot claim to be any larger than the last 10191 * fragment that has been written. Otherwise, the on-disk inode 10192 * might have fragments that were not the last block in the file 10193 * which would corrupt the filesystem. 10194 */ 10195 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10196 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10197 if (adp->ad_offset >= NDADDR) 10198 break; 10199 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10200 /* keep going until hitting a rollback to a frag */ 10201 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10202 continue; 10203 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10204 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10205 #ifdef INVARIANTS 10206 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10207 panic("softdep_write_inodeblock: lost dep1"); 10208 #endif /* INVARIANTS */ 10209 dp->di_db[i] = 0; 10210 } 10211 for (i = 0; i < NIADDR; i++) { 10212 #ifdef INVARIANTS 10213 if (dp->di_ib[i] != 0 && 10214 (deplist & ((1 << NDADDR) << i)) == 0) 10215 panic("softdep_write_inodeblock: lost dep2"); 10216 #endif /* INVARIANTS */ 10217 dp->di_ib[i] = 0; 10218 } 10219 return; 10220 } 10221 /* 10222 * If we have zero'ed out the last allocated block of the file, 10223 * roll back the size to the last currently allocated block. 10224 * We know that this last allocated block is a full-sized as 10225 * we already checked for fragments in the loop above. 10226 */ 10227 if (lastadp != NULL && 10228 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10229 for (i = lastadp->ad_offset; i >= 0; i--) 10230 if (dp->di_db[i] != 0) 10231 break; 10232 dp->di_size = (i + 1) * fs->fs_bsize; 10233 } 10234 /* 10235 * The only dependencies are for indirect blocks. 10236 * 10237 * The file size for indirect block additions is not guaranteed. 10238 * Such a guarantee would be non-trivial to achieve. The conventional 10239 * synchronous write implementation also does not make this guarantee. 10240 * Fsck should catch and fix discrepancies. Arguably, the file size 10241 * can be over-estimated without destroying integrity when the file 10242 * moves into the indirect blocks (i.e., is large). If we want to 10243 * postpone fsck, we are stuck with this argument. 10244 */ 10245 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10246 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10247 } 10248 10249 /* 10250 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10251 * Note that any bug fixes made to this routine must be done in the 10252 * version found above. 10253 * 10254 * Called from within the procedure above to deal with unsatisfied 10255 * allocation dependencies in an inodeblock. The buffer must be 10256 * locked, thus, no I/O completion operations can occur while we 10257 * are manipulating its associated dependencies. 10258 */ 10259 static void 10260 initiate_write_inodeblock_ufs2(inodedep, bp) 10261 struct inodedep *inodedep; 10262 struct buf *bp; /* The inode block */ 10263 { 10264 struct allocdirect *adp, *lastadp; 10265 struct ufs2_dinode *dp; 10266 struct ufs2_dinode *sip; 10267 struct inoref *inoref; 10268 struct ufsmount *ump; 10269 struct fs *fs; 10270 ufs_lbn_t i; 10271 #ifdef INVARIANTS 10272 ufs_lbn_t prevlbn = 0; 10273 #endif 10274 int deplist; 10275 10276 if (inodedep->id_state & IOSTARTED) 10277 panic("initiate_write_inodeblock_ufs2: already started"); 10278 inodedep->id_state |= IOSTARTED; 10279 fs = inodedep->id_fs; 10280 ump = VFSTOUFS(inodedep->id_list.wk_mp); 10281 LOCK_OWNED(ump); 10282 dp = (struct ufs2_dinode *)bp->b_data + 10283 ino_to_fsbo(fs, inodedep->id_ino); 10284 10285 /* 10286 * If we're on the unlinked list but have not yet written our 10287 * next pointer initialize it here. 10288 */ 10289 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10290 struct inodedep *inon; 10291 10292 inon = TAILQ_NEXT(inodedep, id_unlinked); 10293 dp->di_freelink = inon ? inon->id_ino : 0; 10294 } 10295 /* 10296 * If the bitmap is not yet written, then the allocated 10297 * inode cannot be written to disk. 10298 */ 10299 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10300 if (inodedep->id_savedino2 != NULL) 10301 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10302 FREE_LOCK(ump); 10303 sip = malloc(sizeof(struct ufs2_dinode), 10304 M_SAVEDINO, M_SOFTDEP_FLAGS); 10305 ACQUIRE_LOCK(ump); 10306 inodedep->id_savedino2 = sip; 10307 *inodedep->id_savedino2 = *dp; 10308 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10309 dp->di_gen = inodedep->id_savedino2->di_gen; 10310 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10311 return; 10312 } 10313 /* 10314 * If no dependencies, then there is nothing to roll back. 10315 */ 10316 inodedep->id_savedsize = dp->di_size; 10317 inodedep->id_savedextsize = dp->di_extsize; 10318 inodedep->id_savednlink = dp->di_nlink; 10319 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10320 TAILQ_EMPTY(&inodedep->id_extupdt) && 10321 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10322 return; 10323 /* 10324 * Revert the link count to that of the first unwritten journal entry. 10325 */ 10326 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10327 if (inoref) 10328 dp->di_nlink = inoref->if_nlink; 10329 10330 /* 10331 * Set the ext data dependencies to busy. 10332 */ 10333 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10334 adp = TAILQ_NEXT(adp, ad_next)) { 10335 #ifdef INVARIANTS 10336 if (deplist != 0 && prevlbn >= adp->ad_offset) 10337 panic("softdep_write_inodeblock: lbn order"); 10338 prevlbn = adp->ad_offset; 10339 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10340 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10341 "softdep_write_inodeblock", 10342 (intmax_t)adp->ad_offset, 10343 (intmax_t)dp->di_extb[adp->ad_offset], 10344 (intmax_t)adp->ad_newblkno); 10345 deplist |= 1 << adp->ad_offset; 10346 if ((adp->ad_state & ATTACHED) == 0) 10347 panic("softdep_write_inodeblock: Unknown state 0x%x", 10348 adp->ad_state); 10349 #endif /* INVARIANTS */ 10350 adp->ad_state &= ~ATTACHED; 10351 adp->ad_state |= UNDONE; 10352 } 10353 /* 10354 * The on-disk inode cannot claim to be any larger than the last 10355 * fragment that has been written. Otherwise, the on-disk inode 10356 * might have fragments that were not the last block in the ext 10357 * data which would corrupt the filesystem. 10358 */ 10359 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10360 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10361 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10362 /* keep going until hitting a rollback to a frag */ 10363 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10364 continue; 10365 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10366 for (i = adp->ad_offset + 1; i < NXADDR; i++) { 10367 #ifdef INVARIANTS 10368 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10369 panic("softdep_write_inodeblock: lost dep1"); 10370 #endif /* INVARIANTS */ 10371 dp->di_extb[i] = 0; 10372 } 10373 lastadp = NULL; 10374 break; 10375 } 10376 /* 10377 * If we have zero'ed out the last allocated block of the ext 10378 * data, roll back the size to the last currently allocated block. 10379 * We know that this last allocated block is a full-sized as 10380 * we already checked for fragments in the loop above. 10381 */ 10382 if (lastadp != NULL && 10383 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10384 for (i = lastadp->ad_offset; i >= 0; i--) 10385 if (dp->di_extb[i] != 0) 10386 break; 10387 dp->di_extsize = (i + 1) * fs->fs_bsize; 10388 } 10389 /* 10390 * Set the file data dependencies to busy. 10391 */ 10392 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10393 adp = TAILQ_NEXT(adp, ad_next)) { 10394 #ifdef INVARIANTS 10395 if (deplist != 0 && prevlbn >= adp->ad_offset) 10396 panic("softdep_write_inodeblock: lbn order"); 10397 if ((adp->ad_state & ATTACHED) == 0) 10398 panic("inodedep %p and adp %p not attached", inodedep, adp); 10399 prevlbn = adp->ad_offset; 10400 if (adp->ad_offset < NDADDR && 10401 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10402 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10403 "softdep_write_inodeblock", 10404 (intmax_t)adp->ad_offset, 10405 (intmax_t)dp->di_db[adp->ad_offset], 10406 (intmax_t)adp->ad_newblkno); 10407 if (adp->ad_offset >= NDADDR && 10408 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10409 panic("%s indirect pointer #%jd mismatch %jd != %jd", 10410 "softdep_write_inodeblock:", 10411 (intmax_t)adp->ad_offset - NDADDR, 10412 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR], 10413 (intmax_t)adp->ad_newblkno); 10414 deplist |= 1 << adp->ad_offset; 10415 if ((adp->ad_state & ATTACHED) == 0) 10416 panic("softdep_write_inodeblock: Unknown state 0x%x", 10417 adp->ad_state); 10418 #endif /* INVARIANTS */ 10419 adp->ad_state &= ~ATTACHED; 10420 adp->ad_state |= UNDONE; 10421 } 10422 /* 10423 * The on-disk inode cannot claim to be any larger than the last 10424 * fragment that has been written. Otherwise, the on-disk inode 10425 * might have fragments that were not the last block in the file 10426 * which would corrupt the filesystem. 10427 */ 10428 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10429 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10430 if (adp->ad_offset >= NDADDR) 10431 break; 10432 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10433 /* keep going until hitting a rollback to a frag */ 10434 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10435 continue; 10436 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10437 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10438 #ifdef INVARIANTS 10439 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10440 panic("softdep_write_inodeblock: lost dep2"); 10441 #endif /* INVARIANTS */ 10442 dp->di_db[i] = 0; 10443 } 10444 for (i = 0; i < NIADDR; i++) { 10445 #ifdef INVARIANTS 10446 if (dp->di_ib[i] != 0 && 10447 (deplist & ((1 << NDADDR) << i)) == 0) 10448 panic("softdep_write_inodeblock: lost dep3"); 10449 #endif /* INVARIANTS */ 10450 dp->di_ib[i] = 0; 10451 } 10452 return; 10453 } 10454 /* 10455 * If we have zero'ed out the last allocated block of the file, 10456 * roll back the size to the last currently allocated block. 10457 * We know that this last allocated block is a full-sized as 10458 * we already checked for fragments in the loop above. 10459 */ 10460 if (lastadp != NULL && 10461 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10462 for (i = lastadp->ad_offset; i >= 0; i--) 10463 if (dp->di_db[i] != 0) 10464 break; 10465 dp->di_size = (i + 1) * fs->fs_bsize; 10466 } 10467 /* 10468 * The only dependencies are for indirect blocks. 10469 * 10470 * The file size for indirect block additions is not guaranteed. 10471 * Such a guarantee would be non-trivial to achieve. The conventional 10472 * synchronous write implementation also does not make this guarantee. 10473 * Fsck should catch and fix discrepancies. Arguably, the file size 10474 * can be over-estimated without destroying integrity when the file 10475 * moves into the indirect blocks (i.e., is large). If we want to 10476 * postpone fsck, we are stuck with this argument. 10477 */ 10478 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10479 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10480 } 10481 10482 /* 10483 * Cancel an indirdep as a result of truncation. Release all of the 10484 * children allocindirs and place their journal work on the appropriate 10485 * list. 10486 */ 10487 static void 10488 cancel_indirdep(indirdep, bp, freeblks) 10489 struct indirdep *indirdep; 10490 struct buf *bp; 10491 struct freeblks *freeblks; 10492 { 10493 struct allocindir *aip; 10494 10495 /* 10496 * None of the indirect pointers will ever be visible, 10497 * so they can simply be tossed. GOINGAWAY ensures 10498 * that allocated pointers will be saved in the buffer 10499 * cache until they are freed. Note that they will 10500 * only be able to be found by their physical address 10501 * since the inode mapping the logical address will 10502 * be gone. The save buffer used for the safe copy 10503 * was allocated in setup_allocindir_phase2 using 10504 * the physical address so it could be used for this 10505 * purpose. Hence we swap the safe copy with the real 10506 * copy, allowing the safe copy to be freed and holding 10507 * on to the real copy for later use in indir_trunc. 10508 */ 10509 if (indirdep->ir_state & GOINGAWAY) 10510 panic("cancel_indirdep: already gone"); 10511 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10512 indirdep->ir_state |= DEPCOMPLETE; 10513 LIST_REMOVE(indirdep, ir_next); 10514 } 10515 indirdep->ir_state |= GOINGAWAY; 10516 /* 10517 * Pass in bp for blocks still have journal writes 10518 * pending so we can cancel them on their own. 10519 */ 10520 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 10521 cancel_allocindir(aip, bp, freeblks, 0); 10522 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) 10523 cancel_allocindir(aip, NULL, freeblks, 0); 10524 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) 10525 cancel_allocindir(aip, NULL, freeblks, 0); 10526 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != 0) 10527 cancel_allocindir(aip, NULL, freeblks, 0); 10528 /* 10529 * If there are pending partial truncations we need to keep the 10530 * old block copy around until they complete. This is because 10531 * the current b_data is not a perfect superset of the available 10532 * blocks. 10533 */ 10534 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10535 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10536 else 10537 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10538 WORKLIST_REMOVE(&indirdep->ir_list); 10539 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10540 indirdep->ir_bp = NULL; 10541 indirdep->ir_freeblks = freeblks; 10542 } 10543 10544 /* 10545 * Free an indirdep once it no longer has new pointers to track. 10546 */ 10547 static void 10548 free_indirdep(indirdep) 10549 struct indirdep *indirdep; 10550 { 10551 10552 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10553 ("free_indirdep: Indir trunc list not empty.")); 10554 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10555 ("free_indirdep: Complete head not empty.")); 10556 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10557 ("free_indirdep: write head not empty.")); 10558 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10559 ("free_indirdep: done head not empty.")); 10560 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10561 ("free_indirdep: deplist head not empty.")); 10562 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10563 ("free_indirdep: %p still on newblk list.", indirdep)); 10564 KASSERT(indirdep->ir_saveddata == NULL, 10565 ("free_indirdep: %p still has saved data.", indirdep)); 10566 if (indirdep->ir_state & ONWORKLIST) 10567 WORKLIST_REMOVE(&indirdep->ir_list); 10568 WORKITEM_FREE(indirdep, D_INDIRDEP); 10569 } 10570 10571 /* 10572 * Called before a write to an indirdep. This routine is responsible for 10573 * rolling back pointers to a safe state which includes only those 10574 * allocindirs which have been completed. 10575 */ 10576 static void 10577 initiate_write_indirdep(indirdep, bp) 10578 struct indirdep *indirdep; 10579 struct buf *bp; 10580 { 10581 struct ufsmount *ump; 10582 10583 indirdep->ir_state |= IOSTARTED; 10584 if (indirdep->ir_state & GOINGAWAY) 10585 panic("disk_io_initiation: indirdep gone"); 10586 /* 10587 * If there are no remaining dependencies, this will be writing 10588 * the real pointers. 10589 */ 10590 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10591 TAILQ_EMPTY(&indirdep->ir_trunc)) 10592 return; 10593 /* 10594 * Replace up-to-date version with safe version. 10595 */ 10596 if (indirdep->ir_saveddata == NULL) { 10597 ump = VFSTOUFS(indirdep->ir_list.wk_mp); 10598 LOCK_OWNED(ump); 10599 FREE_LOCK(ump); 10600 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10601 M_SOFTDEP_FLAGS); 10602 ACQUIRE_LOCK(ump); 10603 } 10604 indirdep->ir_state &= ~ATTACHED; 10605 indirdep->ir_state |= UNDONE; 10606 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10607 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10608 bp->b_bcount); 10609 } 10610 10611 /* 10612 * Called when an inode has been cleared in a cg bitmap. This finally 10613 * eliminates any canceled jaddrefs 10614 */ 10615 void 10616 softdep_setup_inofree(mp, bp, ino, wkhd) 10617 struct mount *mp; 10618 struct buf *bp; 10619 ino_t ino; 10620 struct workhead *wkhd; 10621 { 10622 struct worklist *wk, *wkn; 10623 struct inodedep *inodedep; 10624 struct ufsmount *ump; 10625 uint8_t *inosused; 10626 struct cg *cgp; 10627 struct fs *fs; 10628 10629 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 10630 ("softdep_setup_inofree called on non-softdep filesystem")); 10631 ump = VFSTOUFS(mp); 10632 ACQUIRE_LOCK(ump); 10633 fs = ump->um_fs; 10634 cgp = (struct cg *)bp->b_data; 10635 inosused = cg_inosused(cgp); 10636 if (isset(inosused, ino % fs->fs_ipg)) 10637 panic("softdep_setup_inofree: inode %ju not freed.", 10638 (uintmax_t)ino); 10639 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10640 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10641 (uintmax_t)ino, inodedep); 10642 if (wkhd) { 10643 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10644 if (wk->wk_type != D_JADDREF) 10645 continue; 10646 WORKLIST_REMOVE(wk); 10647 /* 10648 * We can free immediately even if the jaddref 10649 * isn't attached in a background write as now 10650 * the bitmaps are reconciled. 10651 */ 10652 wk->wk_state |= COMPLETE | ATTACHED; 10653 free_jaddref(WK_JADDREF(wk)); 10654 } 10655 jwork_move(&bp->b_dep, wkhd); 10656 } 10657 FREE_LOCK(ump); 10658 } 10659 10660 10661 /* 10662 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10663 * map. Any dependencies waiting for the write to clear are added to the 10664 * buf's list and any jnewblks that are being canceled are discarded 10665 * immediately. 10666 */ 10667 void 10668 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10669 struct mount *mp; 10670 struct buf *bp; 10671 ufs2_daddr_t blkno; 10672 int frags; 10673 struct workhead *wkhd; 10674 { 10675 struct bmsafemap *bmsafemap; 10676 struct jnewblk *jnewblk; 10677 struct ufsmount *ump; 10678 struct worklist *wk; 10679 struct fs *fs; 10680 #ifdef SUJ_DEBUG 10681 uint8_t *blksfree; 10682 struct cg *cgp; 10683 ufs2_daddr_t jstart; 10684 ufs2_daddr_t jend; 10685 ufs2_daddr_t end; 10686 long bno; 10687 int i; 10688 #endif 10689 10690 CTR3(KTR_SUJ, 10691 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10692 blkno, frags, wkhd); 10693 10694 ump = VFSTOUFS(mp); 10695 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ump)) != 0, 10696 ("softdep_setup_blkfree called on non-softdep filesystem")); 10697 ACQUIRE_LOCK(ump); 10698 /* Lookup the bmsafemap so we track when it is dirty. */ 10699 fs = ump->um_fs; 10700 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10701 /* 10702 * Detach any jnewblks which have been canceled. They must linger 10703 * until the bitmap is cleared again by ffs_blkfree() to prevent 10704 * an unjournaled allocation from hitting the disk. 10705 */ 10706 if (wkhd) { 10707 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10708 CTR2(KTR_SUJ, 10709 "softdep_setup_blkfree: blkno %jd wk type %d", 10710 blkno, wk->wk_type); 10711 WORKLIST_REMOVE(wk); 10712 if (wk->wk_type != D_JNEWBLK) { 10713 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10714 continue; 10715 } 10716 jnewblk = WK_JNEWBLK(wk); 10717 KASSERT(jnewblk->jn_state & GOINGAWAY, 10718 ("softdep_setup_blkfree: jnewblk not canceled.")); 10719 #ifdef SUJ_DEBUG 10720 /* 10721 * Assert that this block is free in the bitmap 10722 * before we discard the jnewblk. 10723 */ 10724 cgp = (struct cg *)bp->b_data; 10725 blksfree = cg_blksfree(cgp); 10726 bno = dtogd(fs, jnewblk->jn_blkno); 10727 for (i = jnewblk->jn_oldfrags; 10728 i < jnewblk->jn_frags; i++) { 10729 if (isset(blksfree, bno + i)) 10730 continue; 10731 panic("softdep_setup_blkfree: not free"); 10732 } 10733 #endif 10734 /* 10735 * Even if it's not attached we can free immediately 10736 * as the new bitmap is correct. 10737 */ 10738 wk->wk_state |= COMPLETE | ATTACHED; 10739 free_jnewblk(jnewblk); 10740 } 10741 } 10742 10743 #ifdef SUJ_DEBUG 10744 /* 10745 * Assert that we are not freeing a block which has an outstanding 10746 * allocation dependency. 10747 */ 10748 fs = VFSTOUFS(mp)->um_fs; 10749 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10750 end = blkno + frags; 10751 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10752 /* 10753 * Don't match against blocks that will be freed when the 10754 * background write is done. 10755 */ 10756 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10757 (COMPLETE | DEPCOMPLETE)) 10758 continue; 10759 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10760 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10761 if ((blkno >= jstart && blkno < jend) || 10762 (end > jstart && end <= jend)) { 10763 printf("state 0x%X %jd - %d %d dep %p\n", 10764 jnewblk->jn_state, jnewblk->jn_blkno, 10765 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10766 jnewblk->jn_dep); 10767 panic("softdep_setup_blkfree: " 10768 "%jd-%jd(%d) overlaps with %jd-%jd", 10769 blkno, end, frags, jstart, jend); 10770 } 10771 } 10772 #endif 10773 FREE_LOCK(ump); 10774 } 10775 10776 /* 10777 * Revert a block allocation when the journal record that describes it 10778 * is not yet written. 10779 */ 10780 static int 10781 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10782 struct jnewblk *jnewblk; 10783 struct fs *fs; 10784 struct cg *cgp; 10785 uint8_t *blksfree; 10786 { 10787 ufs1_daddr_t fragno; 10788 long cgbno, bbase; 10789 int frags, blk; 10790 int i; 10791 10792 frags = 0; 10793 cgbno = dtogd(fs, jnewblk->jn_blkno); 10794 /* 10795 * We have to test which frags need to be rolled back. We may 10796 * be operating on a stale copy when doing background writes. 10797 */ 10798 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10799 if (isclr(blksfree, cgbno + i)) 10800 frags++; 10801 if (frags == 0) 10802 return (0); 10803 /* 10804 * This is mostly ffs_blkfree() sans some validation and 10805 * superblock updates. 10806 */ 10807 if (frags == fs->fs_frag) { 10808 fragno = fragstoblks(fs, cgbno); 10809 ffs_setblock(fs, blksfree, fragno); 10810 ffs_clusteracct(fs, cgp, fragno, 1); 10811 cgp->cg_cs.cs_nbfree++; 10812 } else { 10813 cgbno += jnewblk->jn_oldfrags; 10814 bbase = cgbno - fragnum(fs, cgbno); 10815 /* Decrement the old frags. */ 10816 blk = blkmap(fs, blksfree, bbase); 10817 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10818 /* Deallocate the fragment */ 10819 for (i = 0; i < frags; i++) 10820 setbit(blksfree, cgbno + i); 10821 cgp->cg_cs.cs_nffree += frags; 10822 /* Add back in counts associated with the new frags */ 10823 blk = blkmap(fs, blksfree, bbase); 10824 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10825 /* If a complete block has been reassembled, account for it. */ 10826 fragno = fragstoblks(fs, bbase); 10827 if (ffs_isblock(fs, blksfree, fragno)) { 10828 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10829 ffs_clusteracct(fs, cgp, fragno, 1); 10830 cgp->cg_cs.cs_nbfree++; 10831 } 10832 } 10833 stat_jnewblk++; 10834 jnewblk->jn_state &= ~ATTACHED; 10835 jnewblk->jn_state |= UNDONE; 10836 10837 return (frags); 10838 } 10839 10840 static void 10841 initiate_write_bmsafemap(bmsafemap, bp) 10842 struct bmsafemap *bmsafemap; 10843 struct buf *bp; /* The cg block. */ 10844 { 10845 struct jaddref *jaddref; 10846 struct jnewblk *jnewblk; 10847 uint8_t *inosused; 10848 uint8_t *blksfree; 10849 struct cg *cgp; 10850 struct fs *fs; 10851 ino_t ino; 10852 10853 if (bmsafemap->sm_state & IOSTARTED) 10854 return; 10855 bmsafemap->sm_state |= IOSTARTED; 10856 /* 10857 * Clear any inode allocations which are pending journal writes. 10858 */ 10859 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10860 cgp = (struct cg *)bp->b_data; 10861 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10862 inosused = cg_inosused(cgp); 10863 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10864 ino = jaddref->ja_ino % fs->fs_ipg; 10865 if (isset(inosused, ino)) { 10866 if ((jaddref->ja_mode & IFMT) == IFDIR) 10867 cgp->cg_cs.cs_ndir--; 10868 cgp->cg_cs.cs_nifree++; 10869 clrbit(inosused, ino); 10870 jaddref->ja_state &= ~ATTACHED; 10871 jaddref->ja_state |= UNDONE; 10872 stat_jaddref++; 10873 } else 10874 panic("initiate_write_bmsafemap: inode %ju " 10875 "marked free", (uintmax_t)jaddref->ja_ino); 10876 } 10877 } 10878 /* 10879 * Clear any block allocations which are pending journal writes. 10880 */ 10881 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10882 cgp = (struct cg *)bp->b_data; 10883 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10884 blksfree = cg_blksfree(cgp); 10885 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10886 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10887 continue; 10888 panic("initiate_write_bmsafemap: block %jd " 10889 "marked free", jnewblk->jn_blkno); 10890 } 10891 } 10892 /* 10893 * Move allocation lists to the written lists so they can be 10894 * cleared once the block write is complete. 10895 */ 10896 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 10897 inodedep, id_deps); 10898 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 10899 newblk, nb_deps); 10900 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 10901 wk_list); 10902 } 10903 10904 /* 10905 * This routine is called during the completion interrupt 10906 * service routine for a disk write (from the procedure called 10907 * by the device driver to inform the filesystem caches of 10908 * a request completion). It should be called early in this 10909 * procedure, before the block is made available to other 10910 * processes or other routines are called. 10911 * 10912 */ 10913 static void 10914 softdep_disk_write_complete(bp) 10915 struct buf *bp; /* describes the completed disk write */ 10916 { 10917 struct worklist *wk; 10918 struct worklist *owk; 10919 struct ufsmount *ump; 10920 struct workhead reattach; 10921 struct freeblks *freeblks; 10922 struct buf *sbp; 10923 10924 /* 10925 * If an error occurred while doing the write, then the data 10926 * has not hit the disk and the dependencies cannot be unrolled. 10927 */ 10928 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 10929 return; 10930 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 10931 return; 10932 ump = VFSTOUFS(wk->wk_mp); 10933 LIST_INIT(&reattach); 10934 /* 10935 * This lock must not be released anywhere in this code segment. 10936 */ 10937 sbp = NULL; 10938 owk = NULL; 10939 ACQUIRE_LOCK(ump); 10940 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 10941 WORKLIST_REMOVE(wk); 10942 atomic_add_long(&dep_write[wk->wk_type], 1); 10943 if (wk == owk) 10944 panic("duplicate worklist: %p\n", wk); 10945 owk = wk; 10946 switch (wk->wk_type) { 10947 10948 case D_PAGEDEP: 10949 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 10950 WORKLIST_INSERT(&reattach, wk); 10951 continue; 10952 10953 case D_INODEDEP: 10954 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 10955 WORKLIST_INSERT(&reattach, wk); 10956 continue; 10957 10958 case D_BMSAFEMAP: 10959 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp)) 10960 WORKLIST_INSERT(&reattach, wk); 10961 continue; 10962 10963 case D_MKDIR: 10964 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 10965 continue; 10966 10967 case D_ALLOCDIRECT: 10968 wk->wk_state |= COMPLETE; 10969 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 10970 continue; 10971 10972 case D_ALLOCINDIR: 10973 wk->wk_state |= COMPLETE; 10974 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 10975 continue; 10976 10977 case D_INDIRDEP: 10978 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp)) 10979 WORKLIST_INSERT(&reattach, wk); 10980 continue; 10981 10982 case D_FREEBLKS: 10983 wk->wk_state |= COMPLETE; 10984 freeblks = WK_FREEBLKS(wk); 10985 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 10986 LIST_EMPTY(&freeblks->fb_jblkdephd)) 10987 add_to_worklist(wk, WK_NODELAY); 10988 continue; 10989 10990 case D_FREEWORK: 10991 handle_written_freework(WK_FREEWORK(wk)); 10992 break; 10993 10994 case D_JSEGDEP: 10995 free_jsegdep(WK_JSEGDEP(wk)); 10996 continue; 10997 10998 case D_JSEG: 10999 handle_written_jseg(WK_JSEG(wk), bp); 11000 continue; 11001 11002 case D_SBDEP: 11003 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 11004 WORKLIST_INSERT(&reattach, wk); 11005 continue; 11006 11007 case D_FREEDEP: 11008 free_freedep(WK_FREEDEP(wk)); 11009 continue; 11010 11011 default: 11012 panic("handle_disk_write_complete: Unknown type %s", 11013 TYPENAME(wk->wk_type)); 11014 /* NOTREACHED */ 11015 } 11016 } 11017 /* 11018 * Reattach any requests that must be redone. 11019 */ 11020 while ((wk = LIST_FIRST(&reattach)) != NULL) { 11021 WORKLIST_REMOVE(wk); 11022 WORKLIST_INSERT(&bp->b_dep, wk); 11023 } 11024 FREE_LOCK(ump); 11025 if (sbp) 11026 brelse(sbp); 11027 } 11028 11029 /* 11030 * Called from within softdep_disk_write_complete above. Note that 11031 * this routine is always called from interrupt level with further 11032 * splbio interrupts blocked. 11033 */ 11034 static void 11035 handle_allocdirect_partdone(adp, wkhd) 11036 struct allocdirect *adp; /* the completed allocdirect */ 11037 struct workhead *wkhd; /* Work to do when inode is writtne. */ 11038 { 11039 struct allocdirectlst *listhead; 11040 struct allocdirect *listadp; 11041 struct inodedep *inodedep; 11042 long bsize; 11043 11044 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11045 return; 11046 /* 11047 * The on-disk inode cannot claim to be any larger than the last 11048 * fragment that has been written. Otherwise, the on-disk inode 11049 * might have fragments that were not the last block in the file 11050 * which would corrupt the filesystem. Thus, we cannot free any 11051 * allocdirects after one whose ad_oldblkno claims a fragment as 11052 * these blocks must be rolled back to zero before writing the inode. 11053 * We check the currently active set of allocdirects in id_inoupdt 11054 * or id_extupdt as appropriate. 11055 */ 11056 inodedep = adp->ad_inodedep; 11057 bsize = inodedep->id_fs->fs_bsize; 11058 if (adp->ad_state & EXTDATA) 11059 listhead = &inodedep->id_extupdt; 11060 else 11061 listhead = &inodedep->id_inoupdt; 11062 TAILQ_FOREACH(listadp, listhead, ad_next) { 11063 /* found our block */ 11064 if (listadp == adp) 11065 break; 11066 /* continue if ad_oldlbn is not a fragment */ 11067 if (listadp->ad_oldsize == 0 || 11068 listadp->ad_oldsize == bsize) 11069 continue; 11070 /* hit a fragment */ 11071 return; 11072 } 11073 /* 11074 * If we have reached the end of the current list without 11075 * finding the just finished dependency, then it must be 11076 * on the future dependency list. Future dependencies cannot 11077 * be freed until they are moved to the current list. 11078 */ 11079 if (listadp == NULL) { 11080 #ifdef DEBUG 11081 if (adp->ad_state & EXTDATA) 11082 listhead = &inodedep->id_newextupdt; 11083 else 11084 listhead = &inodedep->id_newinoupdt; 11085 TAILQ_FOREACH(listadp, listhead, ad_next) 11086 /* found our block */ 11087 if (listadp == adp) 11088 break; 11089 if (listadp == NULL) 11090 panic("handle_allocdirect_partdone: lost dep"); 11091 #endif /* DEBUG */ 11092 return; 11093 } 11094 /* 11095 * If we have found the just finished dependency, then queue 11096 * it along with anything that follows it that is complete. 11097 * Since the pointer has not yet been written in the inode 11098 * as the dependency prevents it, place the allocdirect on the 11099 * bufwait list where it will be freed once the pointer is 11100 * valid. 11101 */ 11102 if (wkhd == NULL) 11103 wkhd = &inodedep->id_bufwait; 11104 for (; adp; adp = listadp) { 11105 listadp = TAILQ_NEXT(adp, ad_next); 11106 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 11107 return; 11108 TAILQ_REMOVE(listhead, adp, ad_next); 11109 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 11110 } 11111 } 11112 11113 /* 11114 * Called from within softdep_disk_write_complete above. This routine 11115 * completes successfully written allocindirs. 11116 */ 11117 static void 11118 handle_allocindir_partdone(aip) 11119 struct allocindir *aip; /* the completed allocindir */ 11120 { 11121 struct indirdep *indirdep; 11122 11123 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 11124 return; 11125 indirdep = aip->ai_indirdep; 11126 LIST_REMOVE(aip, ai_next); 11127 /* 11128 * Don't set a pointer while the buffer is undergoing IO or while 11129 * we have active truncations. 11130 */ 11131 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 11132 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 11133 return; 11134 } 11135 if (indirdep->ir_state & UFS1FMT) 11136 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11137 aip->ai_newblkno; 11138 else 11139 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 11140 aip->ai_newblkno; 11141 /* 11142 * Await the pointer write before freeing the allocindir. 11143 */ 11144 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 11145 } 11146 11147 /* 11148 * Release segments held on a jwork list. 11149 */ 11150 static void 11151 handle_jwork(wkhd) 11152 struct workhead *wkhd; 11153 { 11154 struct worklist *wk; 11155 11156 while ((wk = LIST_FIRST(wkhd)) != NULL) { 11157 WORKLIST_REMOVE(wk); 11158 switch (wk->wk_type) { 11159 case D_JSEGDEP: 11160 free_jsegdep(WK_JSEGDEP(wk)); 11161 continue; 11162 case D_FREEDEP: 11163 free_freedep(WK_FREEDEP(wk)); 11164 continue; 11165 case D_FREEFRAG: 11166 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 11167 WORKITEM_FREE(wk, D_FREEFRAG); 11168 continue; 11169 case D_FREEWORK: 11170 handle_written_freework(WK_FREEWORK(wk)); 11171 continue; 11172 default: 11173 panic("handle_jwork: Unknown type %s\n", 11174 TYPENAME(wk->wk_type)); 11175 } 11176 } 11177 } 11178 11179 /* 11180 * Handle the bufwait list on an inode when it is safe to release items 11181 * held there. This normally happens after an inode block is written but 11182 * may be delayed and handled later if there are pending journal items that 11183 * are not yet safe to be released. 11184 */ 11185 static struct freefile * 11186 handle_bufwait(inodedep, refhd) 11187 struct inodedep *inodedep; 11188 struct workhead *refhd; 11189 { 11190 struct jaddref *jaddref; 11191 struct freefile *freefile; 11192 struct worklist *wk; 11193 11194 freefile = NULL; 11195 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 11196 WORKLIST_REMOVE(wk); 11197 switch (wk->wk_type) { 11198 case D_FREEFILE: 11199 /* 11200 * We defer adding freefile to the worklist 11201 * until all other additions have been made to 11202 * ensure that it will be done after all the 11203 * old blocks have been freed. 11204 */ 11205 if (freefile != NULL) 11206 panic("handle_bufwait: freefile"); 11207 freefile = WK_FREEFILE(wk); 11208 continue; 11209 11210 case D_MKDIR: 11211 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 11212 continue; 11213 11214 case D_DIRADD: 11215 diradd_inode_written(WK_DIRADD(wk), inodedep); 11216 continue; 11217 11218 case D_FREEFRAG: 11219 wk->wk_state |= COMPLETE; 11220 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 11221 add_to_worklist(wk, 0); 11222 continue; 11223 11224 case D_DIRREM: 11225 wk->wk_state |= COMPLETE; 11226 add_to_worklist(wk, 0); 11227 continue; 11228 11229 case D_ALLOCDIRECT: 11230 case D_ALLOCINDIR: 11231 free_newblk(WK_NEWBLK(wk)); 11232 continue; 11233 11234 case D_JNEWBLK: 11235 wk->wk_state |= COMPLETE; 11236 free_jnewblk(WK_JNEWBLK(wk)); 11237 continue; 11238 11239 /* 11240 * Save freed journal segments and add references on 11241 * the supplied list which will delay their release 11242 * until the cg bitmap is cleared on disk. 11243 */ 11244 case D_JSEGDEP: 11245 if (refhd == NULL) 11246 free_jsegdep(WK_JSEGDEP(wk)); 11247 else 11248 WORKLIST_INSERT(refhd, wk); 11249 continue; 11250 11251 case D_JADDREF: 11252 jaddref = WK_JADDREF(wk); 11253 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 11254 if_deps); 11255 /* 11256 * Transfer any jaddrefs to the list to be freed with 11257 * the bitmap if we're handling a removed file. 11258 */ 11259 if (refhd == NULL) { 11260 wk->wk_state |= COMPLETE; 11261 free_jaddref(jaddref); 11262 } else 11263 WORKLIST_INSERT(refhd, wk); 11264 continue; 11265 11266 default: 11267 panic("handle_bufwait: Unknown type %p(%s)", 11268 wk, TYPENAME(wk->wk_type)); 11269 /* NOTREACHED */ 11270 } 11271 } 11272 return (freefile); 11273 } 11274 /* 11275 * Called from within softdep_disk_write_complete above to restore 11276 * in-memory inode block contents to their most up-to-date state. Note 11277 * that this routine is always called from interrupt level with further 11278 * splbio interrupts blocked. 11279 */ 11280 static int 11281 handle_written_inodeblock(inodedep, bp) 11282 struct inodedep *inodedep; 11283 struct buf *bp; /* buffer containing the inode block */ 11284 { 11285 struct freefile *freefile; 11286 struct allocdirect *adp, *nextadp; 11287 struct ufs1_dinode *dp1 = NULL; 11288 struct ufs2_dinode *dp2 = NULL; 11289 struct workhead wkhd; 11290 int hadchanges, fstype; 11291 ino_t freelink; 11292 11293 LIST_INIT(&wkhd); 11294 hadchanges = 0; 11295 freefile = NULL; 11296 if ((inodedep->id_state & IOSTARTED) == 0) 11297 panic("handle_written_inodeblock: not started"); 11298 inodedep->id_state &= ~IOSTARTED; 11299 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11300 fstype = UFS1; 11301 dp1 = (struct ufs1_dinode *)bp->b_data + 11302 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11303 freelink = dp1->di_freelink; 11304 } else { 11305 fstype = UFS2; 11306 dp2 = (struct ufs2_dinode *)bp->b_data + 11307 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11308 freelink = dp2->di_freelink; 11309 } 11310 /* 11311 * Leave this inodeblock dirty until it's in the list. 11312 */ 11313 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) { 11314 struct inodedep *inon; 11315 11316 inon = TAILQ_NEXT(inodedep, id_unlinked); 11317 if ((inon == NULL && freelink == 0) || 11318 (inon && inon->id_ino == freelink)) { 11319 if (inon) 11320 inon->id_state |= UNLINKPREV; 11321 inodedep->id_state |= UNLINKNEXT; 11322 } 11323 hadchanges = 1; 11324 } 11325 /* 11326 * If we had to rollback the inode allocation because of 11327 * bitmaps being incomplete, then simply restore it. 11328 * Keep the block dirty so that it will not be reclaimed until 11329 * all associated dependencies have been cleared and the 11330 * corresponding updates written to disk. 11331 */ 11332 if (inodedep->id_savedino1 != NULL) { 11333 hadchanges = 1; 11334 if (fstype == UFS1) 11335 *dp1 = *inodedep->id_savedino1; 11336 else 11337 *dp2 = *inodedep->id_savedino2; 11338 free(inodedep->id_savedino1, M_SAVEDINO); 11339 inodedep->id_savedino1 = NULL; 11340 if ((bp->b_flags & B_DELWRI) == 0) 11341 stat_inode_bitmap++; 11342 bdirty(bp); 11343 /* 11344 * If the inode is clear here and GOINGAWAY it will never 11345 * be written. Process the bufwait and clear any pending 11346 * work which may include the freefile. 11347 */ 11348 if (inodedep->id_state & GOINGAWAY) 11349 goto bufwait; 11350 return (1); 11351 } 11352 inodedep->id_state |= COMPLETE; 11353 /* 11354 * Roll forward anything that had to be rolled back before 11355 * the inode could be updated. 11356 */ 11357 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11358 nextadp = TAILQ_NEXT(adp, ad_next); 11359 if (adp->ad_state & ATTACHED) 11360 panic("handle_written_inodeblock: new entry"); 11361 if (fstype == UFS1) { 11362 if (adp->ad_offset < NDADDR) { 11363 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11364 panic("%s %s #%jd mismatch %d != %jd", 11365 "handle_written_inodeblock:", 11366 "direct pointer", 11367 (intmax_t)adp->ad_offset, 11368 dp1->di_db[adp->ad_offset], 11369 (intmax_t)adp->ad_oldblkno); 11370 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11371 } else { 11372 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0) 11373 panic("%s: %s #%jd allocated as %d", 11374 "handle_written_inodeblock", 11375 "indirect pointer", 11376 (intmax_t)adp->ad_offset - NDADDR, 11377 dp1->di_ib[adp->ad_offset - NDADDR]); 11378 dp1->di_ib[adp->ad_offset - NDADDR] = 11379 adp->ad_newblkno; 11380 } 11381 } else { 11382 if (adp->ad_offset < NDADDR) { 11383 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11384 panic("%s: %s #%jd %s %jd != %jd", 11385 "handle_written_inodeblock", 11386 "direct pointer", 11387 (intmax_t)adp->ad_offset, "mismatch", 11388 (intmax_t)dp2->di_db[adp->ad_offset], 11389 (intmax_t)adp->ad_oldblkno); 11390 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11391 } else { 11392 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0) 11393 panic("%s: %s #%jd allocated as %jd", 11394 "handle_written_inodeblock", 11395 "indirect pointer", 11396 (intmax_t)adp->ad_offset - NDADDR, 11397 (intmax_t) 11398 dp2->di_ib[adp->ad_offset - NDADDR]); 11399 dp2->di_ib[adp->ad_offset - NDADDR] = 11400 adp->ad_newblkno; 11401 } 11402 } 11403 adp->ad_state &= ~UNDONE; 11404 adp->ad_state |= ATTACHED; 11405 hadchanges = 1; 11406 } 11407 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11408 nextadp = TAILQ_NEXT(adp, ad_next); 11409 if (adp->ad_state & ATTACHED) 11410 panic("handle_written_inodeblock: new entry"); 11411 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11412 panic("%s: direct pointers #%jd %s %jd != %jd", 11413 "handle_written_inodeblock", 11414 (intmax_t)adp->ad_offset, "mismatch", 11415 (intmax_t)dp2->di_extb[adp->ad_offset], 11416 (intmax_t)adp->ad_oldblkno); 11417 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11418 adp->ad_state &= ~UNDONE; 11419 adp->ad_state |= ATTACHED; 11420 hadchanges = 1; 11421 } 11422 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11423 stat_direct_blk_ptrs++; 11424 /* 11425 * Reset the file size to its most up-to-date value. 11426 */ 11427 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11428 panic("handle_written_inodeblock: bad size"); 11429 if (inodedep->id_savednlink > LINK_MAX) 11430 panic("handle_written_inodeblock: Invalid link count " 11431 "%d for inodedep %p", inodedep->id_savednlink, inodedep); 11432 if (fstype == UFS1) { 11433 if (dp1->di_nlink != inodedep->id_savednlink) { 11434 dp1->di_nlink = inodedep->id_savednlink; 11435 hadchanges = 1; 11436 } 11437 if (dp1->di_size != inodedep->id_savedsize) { 11438 dp1->di_size = inodedep->id_savedsize; 11439 hadchanges = 1; 11440 } 11441 } else { 11442 if (dp2->di_nlink != inodedep->id_savednlink) { 11443 dp2->di_nlink = inodedep->id_savednlink; 11444 hadchanges = 1; 11445 } 11446 if (dp2->di_size != inodedep->id_savedsize) { 11447 dp2->di_size = inodedep->id_savedsize; 11448 hadchanges = 1; 11449 } 11450 if (dp2->di_extsize != inodedep->id_savedextsize) { 11451 dp2->di_extsize = inodedep->id_savedextsize; 11452 hadchanges = 1; 11453 } 11454 } 11455 inodedep->id_savedsize = -1; 11456 inodedep->id_savedextsize = -1; 11457 inodedep->id_savednlink = -1; 11458 /* 11459 * If there were any rollbacks in the inode block, then it must be 11460 * marked dirty so that its will eventually get written back in 11461 * its correct form. 11462 */ 11463 if (hadchanges) 11464 bdirty(bp); 11465 bufwait: 11466 /* 11467 * Process any allocdirects that completed during the update. 11468 */ 11469 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11470 handle_allocdirect_partdone(adp, &wkhd); 11471 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11472 handle_allocdirect_partdone(adp, &wkhd); 11473 /* 11474 * Process deallocations that were held pending until the 11475 * inode had been written to disk. Freeing of the inode 11476 * is delayed until after all blocks have been freed to 11477 * avoid creation of new <vfsid, inum, lbn> triples 11478 * before the old ones have been deleted. Completely 11479 * unlinked inodes are not processed until the unlinked 11480 * inode list is written or the last reference is removed. 11481 */ 11482 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11483 freefile = handle_bufwait(inodedep, NULL); 11484 if (freefile && !LIST_EMPTY(&wkhd)) { 11485 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11486 freefile = NULL; 11487 } 11488 } 11489 /* 11490 * Move rolled forward dependency completions to the bufwait list 11491 * now that those that were already written have been processed. 11492 */ 11493 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11494 panic("handle_written_inodeblock: bufwait but no changes"); 11495 jwork_move(&inodedep->id_bufwait, &wkhd); 11496 11497 if (freefile != NULL) { 11498 /* 11499 * If the inode is goingaway it was never written. Fake up 11500 * the state here so free_inodedep() can succeed. 11501 */ 11502 if (inodedep->id_state & GOINGAWAY) 11503 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11504 if (free_inodedep(inodedep) == 0) 11505 panic("handle_written_inodeblock: live inodedep %p", 11506 inodedep); 11507 add_to_worklist(&freefile->fx_list, 0); 11508 return (0); 11509 } 11510 11511 /* 11512 * If no outstanding dependencies, free it. 11513 */ 11514 if (free_inodedep(inodedep) || 11515 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11516 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11517 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11518 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11519 return (0); 11520 return (hadchanges); 11521 } 11522 11523 static int 11524 handle_written_indirdep(indirdep, bp, bpp) 11525 struct indirdep *indirdep; 11526 struct buf *bp; 11527 struct buf **bpp; 11528 { 11529 struct allocindir *aip; 11530 struct buf *sbp; 11531 int chgs; 11532 11533 if (indirdep->ir_state & GOINGAWAY) 11534 panic("handle_written_indirdep: indirdep gone"); 11535 if ((indirdep->ir_state & IOSTARTED) == 0) 11536 panic("handle_written_indirdep: IO not started"); 11537 chgs = 0; 11538 /* 11539 * If there were rollbacks revert them here. 11540 */ 11541 if (indirdep->ir_saveddata) { 11542 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11543 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11544 free(indirdep->ir_saveddata, M_INDIRDEP); 11545 indirdep->ir_saveddata = NULL; 11546 } 11547 chgs = 1; 11548 } 11549 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11550 indirdep->ir_state |= ATTACHED; 11551 /* 11552 * Move allocindirs with written pointers to the completehd if 11553 * the indirdep's pointer is not yet written. Otherwise 11554 * free them here. 11555 */ 11556 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) { 11557 LIST_REMOVE(aip, ai_next); 11558 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11559 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11560 ai_next); 11561 newblk_freefrag(&aip->ai_block); 11562 continue; 11563 } 11564 free_newblk(&aip->ai_block); 11565 } 11566 /* 11567 * Move allocindirs that have finished dependency processing from 11568 * the done list to the write list after updating the pointers. 11569 */ 11570 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11571 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 11572 handle_allocindir_partdone(aip); 11573 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11574 panic("disk_write_complete: not gone"); 11575 chgs = 1; 11576 } 11577 } 11578 /* 11579 * Preserve the indirdep if there were any changes or if it is not 11580 * yet valid on disk. 11581 */ 11582 if (chgs) { 11583 stat_indir_blk_ptrs++; 11584 bdirty(bp); 11585 return (1); 11586 } 11587 /* 11588 * If there were no changes we can discard the savedbp and detach 11589 * ourselves from the buf. We are only carrying completed pointers 11590 * in this case. 11591 */ 11592 sbp = indirdep->ir_savebp; 11593 sbp->b_flags |= B_INVAL | B_NOCACHE; 11594 indirdep->ir_savebp = NULL; 11595 indirdep->ir_bp = NULL; 11596 if (*bpp != NULL) 11597 panic("handle_written_indirdep: bp already exists."); 11598 *bpp = sbp; 11599 /* 11600 * The indirdep may not be freed until its parent points at it. 11601 */ 11602 if (indirdep->ir_state & DEPCOMPLETE) 11603 free_indirdep(indirdep); 11604 11605 return (0); 11606 } 11607 11608 /* 11609 * Process a diradd entry after its dependent inode has been written. 11610 * This routine must be called with splbio interrupts blocked. 11611 */ 11612 static void 11613 diradd_inode_written(dap, inodedep) 11614 struct diradd *dap; 11615 struct inodedep *inodedep; 11616 { 11617 11618 dap->da_state |= COMPLETE; 11619 complete_diradd(dap); 11620 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11621 } 11622 11623 /* 11624 * Returns true if the bmsafemap will have rollbacks when written. Must only 11625 * be called with the per-filesystem lock and the buf lock on the cg held. 11626 */ 11627 static int 11628 bmsafemap_backgroundwrite(bmsafemap, bp) 11629 struct bmsafemap *bmsafemap; 11630 struct buf *bp; 11631 { 11632 int dirty; 11633 11634 LOCK_OWNED(VFSTOUFS(bmsafemap->sm_list.wk_mp)); 11635 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11636 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11637 /* 11638 * If we're initiating a background write we need to process the 11639 * rollbacks as they exist now, not as they exist when IO starts. 11640 * No other consumers will look at the contents of the shadowed 11641 * buf so this is safe to do here. 11642 */ 11643 if (bp->b_xflags & BX_BKGRDMARKER) 11644 initiate_write_bmsafemap(bmsafemap, bp); 11645 11646 return (dirty); 11647 } 11648 11649 /* 11650 * Re-apply an allocation when a cg write is complete. 11651 */ 11652 static int 11653 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11654 struct jnewblk *jnewblk; 11655 struct fs *fs; 11656 struct cg *cgp; 11657 uint8_t *blksfree; 11658 { 11659 ufs1_daddr_t fragno; 11660 ufs2_daddr_t blkno; 11661 long cgbno, bbase; 11662 int frags, blk; 11663 int i; 11664 11665 frags = 0; 11666 cgbno = dtogd(fs, jnewblk->jn_blkno); 11667 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11668 if (isclr(blksfree, cgbno + i)) 11669 panic("jnewblk_rollforward: re-allocated fragment"); 11670 frags++; 11671 } 11672 if (frags == fs->fs_frag) { 11673 blkno = fragstoblks(fs, cgbno); 11674 ffs_clrblock(fs, blksfree, (long)blkno); 11675 ffs_clusteracct(fs, cgp, blkno, -1); 11676 cgp->cg_cs.cs_nbfree--; 11677 } else { 11678 bbase = cgbno - fragnum(fs, cgbno); 11679 cgbno += jnewblk->jn_oldfrags; 11680 /* If a complete block had been reassembled, account for it. */ 11681 fragno = fragstoblks(fs, bbase); 11682 if (ffs_isblock(fs, blksfree, fragno)) { 11683 cgp->cg_cs.cs_nffree += fs->fs_frag; 11684 ffs_clusteracct(fs, cgp, fragno, -1); 11685 cgp->cg_cs.cs_nbfree--; 11686 } 11687 /* Decrement the old frags. */ 11688 blk = blkmap(fs, blksfree, bbase); 11689 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11690 /* Allocate the fragment */ 11691 for (i = 0; i < frags; i++) 11692 clrbit(blksfree, cgbno + i); 11693 cgp->cg_cs.cs_nffree -= frags; 11694 /* Add back in counts associated with the new frags */ 11695 blk = blkmap(fs, blksfree, bbase); 11696 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11697 } 11698 return (frags); 11699 } 11700 11701 /* 11702 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11703 * changes if it's not a background write. Set all written dependencies 11704 * to DEPCOMPLETE and free the structure if possible. 11705 */ 11706 static int 11707 handle_written_bmsafemap(bmsafemap, bp) 11708 struct bmsafemap *bmsafemap; 11709 struct buf *bp; 11710 { 11711 struct newblk *newblk; 11712 struct inodedep *inodedep; 11713 struct jaddref *jaddref, *jatmp; 11714 struct jnewblk *jnewblk, *jntmp; 11715 struct ufsmount *ump; 11716 uint8_t *inosused; 11717 uint8_t *blksfree; 11718 struct cg *cgp; 11719 struct fs *fs; 11720 ino_t ino; 11721 int foreground; 11722 int chgs; 11723 11724 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11725 panic("initiate_write_bmsafemap: Not started\n"); 11726 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11727 chgs = 0; 11728 bmsafemap->sm_state &= ~IOSTARTED; 11729 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11730 /* 11731 * Release journal work that was waiting on the write. 11732 */ 11733 handle_jwork(&bmsafemap->sm_freewr); 11734 11735 /* 11736 * Restore unwritten inode allocation pending jaddref writes. 11737 */ 11738 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11739 cgp = (struct cg *)bp->b_data; 11740 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11741 inosused = cg_inosused(cgp); 11742 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11743 ja_bmdeps, jatmp) { 11744 if ((jaddref->ja_state & UNDONE) == 0) 11745 continue; 11746 ino = jaddref->ja_ino % fs->fs_ipg; 11747 if (isset(inosused, ino)) 11748 panic("handle_written_bmsafemap: " 11749 "re-allocated inode"); 11750 /* Do the roll-forward only if it's a real copy. */ 11751 if (foreground) { 11752 if ((jaddref->ja_mode & IFMT) == IFDIR) 11753 cgp->cg_cs.cs_ndir++; 11754 cgp->cg_cs.cs_nifree--; 11755 setbit(inosused, ino); 11756 chgs = 1; 11757 } 11758 jaddref->ja_state &= ~UNDONE; 11759 jaddref->ja_state |= ATTACHED; 11760 free_jaddref(jaddref); 11761 } 11762 } 11763 /* 11764 * Restore any block allocations which are pending journal writes. 11765 */ 11766 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11767 cgp = (struct cg *)bp->b_data; 11768 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11769 blksfree = cg_blksfree(cgp); 11770 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11771 jntmp) { 11772 if ((jnewblk->jn_state & UNDONE) == 0) 11773 continue; 11774 /* Do the roll-forward only if it's a real copy. */ 11775 if (foreground && 11776 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11777 chgs = 1; 11778 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11779 jnewblk->jn_state |= ATTACHED; 11780 free_jnewblk(jnewblk); 11781 } 11782 } 11783 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11784 newblk->nb_state |= DEPCOMPLETE; 11785 newblk->nb_state &= ~ONDEPLIST; 11786 newblk->nb_bmsafemap = NULL; 11787 LIST_REMOVE(newblk, nb_deps); 11788 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11789 handle_allocdirect_partdone( 11790 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 11791 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 11792 handle_allocindir_partdone( 11793 WK_ALLOCINDIR(&newblk->nb_list)); 11794 else if (newblk->nb_list.wk_type != D_NEWBLK) 11795 panic("handle_written_bmsafemap: Unexpected type: %s", 11796 TYPENAME(newblk->nb_list.wk_type)); 11797 } 11798 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 11799 inodedep->id_state |= DEPCOMPLETE; 11800 inodedep->id_state &= ~ONDEPLIST; 11801 LIST_REMOVE(inodedep, id_deps); 11802 inodedep->id_bmsafemap = NULL; 11803 } 11804 LIST_REMOVE(bmsafemap, sm_next); 11805 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 11806 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 11807 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 11808 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 11809 LIST_EMPTY(&bmsafemap->sm_freehd)) { 11810 LIST_REMOVE(bmsafemap, sm_hash); 11811 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 11812 return (0); 11813 } 11814 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 11815 if (foreground) 11816 bdirty(bp); 11817 return (1); 11818 } 11819 11820 /* 11821 * Try to free a mkdir dependency. 11822 */ 11823 static void 11824 complete_mkdir(mkdir) 11825 struct mkdir *mkdir; 11826 { 11827 struct diradd *dap; 11828 11829 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 11830 return; 11831 LIST_REMOVE(mkdir, md_mkdirs); 11832 dap = mkdir->md_diradd; 11833 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 11834 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 11835 dap->da_state |= DEPCOMPLETE; 11836 complete_diradd(dap); 11837 } 11838 WORKITEM_FREE(mkdir, D_MKDIR); 11839 } 11840 11841 /* 11842 * Handle the completion of a mkdir dependency. 11843 */ 11844 static void 11845 handle_written_mkdir(mkdir, type) 11846 struct mkdir *mkdir; 11847 int type; 11848 { 11849 11850 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 11851 panic("handle_written_mkdir: bad type"); 11852 mkdir->md_state |= COMPLETE; 11853 complete_mkdir(mkdir); 11854 } 11855 11856 static int 11857 free_pagedep(pagedep) 11858 struct pagedep *pagedep; 11859 { 11860 int i; 11861 11862 if (pagedep->pd_state & NEWBLOCK) 11863 return (0); 11864 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 11865 return (0); 11866 for (i = 0; i < DAHASHSZ; i++) 11867 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 11868 return (0); 11869 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 11870 return (0); 11871 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 11872 return (0); 11873 if (pagedep->pd_state & ONWORKLIST) 11874 WORKLIST_REMOVE(&pagedep->pd_list); 11875 LIST_REMOVE(pagedep, pd_hash); 11876 WORKITEM_FREE(pagedep, D_PAGEDEP); 11877 11878 return (1); 11879 } 11880 11881 /* 11882 * Called from within softdep_disk_write_complete above. 11883 * A write operation was just completed. Removed inodes can 11884 * now be freed and associated block pointers may be committed. 11885 * Note that this routine is always called from interrupt level 11886 * with further splbio interrupts blocked. 11887 */ 11888 static int 11889 handle_written_filepage(pagedep, bp) 11890 struct pagedep *pagedep; 11891 struct buf *bp; /* buffer containing the written page */ 11892 { 11893 struct dirrem *dirrem; 11894 struct diradd *dap, *nextdap; 11895 struct direct *ep; 11896 int i, chgs; 11897 11898 if ((pagedep->pd_state & IOSTARTED) == 0) 11899 panic("handle_written_filepage: not started"); 11900 pagedep->pd_state &= ~IOSTARTED; 11901 /* 11902 * Process any directory removals that have been committed. 11903 */ 11904 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 11905 LIST_REMOVE(dirrem, dm_next); 11906 dirrem->dm_state |= COMPLETE; 11907 dirrem->dm_dirinum = pagedep->pd_ino; 11908 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 11909 ("handle_written_filepage: Journal entries not written.")); 11910 add_to_worklist(&dirrem->dm_list, 0); 11911 } 11912 /* 11913 * Free any directory additions that have been committed. 11914 * If it is a newly allocated block, we have to wait until 11915 * the on-disk directory inode claims the new block. 11916 */ 11917 if ((pagedep->pd_state & NEWBLOCK) == 0) 11918 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 11919 free_diradd(dap, NULL); 11920 /* 11921 * Uncommitted directory entries must be restored. 11922 */ 11923 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 11924 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 11925 dap = nextdap) { 11926 nextdap = LIST_NEXT(dap, da_pdlist); 11927 if (dap->da_state & ATTACHED) 11928 panic("handle_written_filepage: attached"); 11929 ep = (struct direct *) 11930 ((char *)bp->b_data + dap->da_offset); 11931 ep->d_ino = dap->da_newinum; 11932 dap->da_state &= ~UNDONE; 11933 dap->da_state |= ATTACHED; 11934 chgs = 1; 11935 /* 11936 * If the inode referenced by the directory has 11937 * been written out, then the dependency can be 11938 * moved to the pending list. 11939 */ 11940 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 11941 LIST_REMOVE(dap, da_pdlist); 11942 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 11943 da_pdlist); 11944 } 11945 } 11946 } 11947 /* 11948 * If there were any rollbacks in the directory, then it must be 11949 * marked dirty so that its will eventually get written back in 11950 * its correct form. 11951 */ 11952 if (chgs) { 11953 if ((bp->b_flags & B_DELWRI) == 0) 11954 stat_dir_entry++; 11955 bdirty(bp); 11956 return (1); 11957 } 11958 /* 11959 * If we are not waiting for a new directory block to be 11960 * claimed by its inode, then the pagedep will be freed. 11961 * Otherwise it will remain to track any new entries on 11962 * the page in case they are fsync'ed. 11963 */ 11964 free_pagedep(pagedep); 11965 return (0); 11966 } 11967 11968 /* 11969 * Writing back in-core inode structures. 11970 * 11971 * The filesystem only accesses an inode's contents when it occupies an 11972 * "in-core" inode structure. These "in-core" structures are separate from 11973 * the page frames used to cache inode blocks. Only the latter are 11974 * transferred to/from the disk. So, when the updated contents of the 11975 * "in-core" inode structure are copied to the corresponding in-memory inode 11976 * block, the dependencies are also transferred. The following procedure is 11977 * called when copying a dirty "in-core" inode to a cached inode block. 11978 */ 11979 11980 /* 11981 * Called when an inode is loaded from disk. If the effective link count 11982 * differed from the actual link count when it was last flushed, then we 11983 * need to ensure that the correct effective link count is put back. 11984 */ 11985 void 11986 softdep_load_inodeblock(ip) 11987 struct inode *ip; /* the "in_core" copy of the inode */ 11988 { 11989 struct inodedep *inodedep; 11990 11991 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 11992 ("softdep_load_inodeblock called on non-softdep filesystem")); 11993 /* 11994 * Check for alternate nlink count. 11995 */ 11996 ip->i_effnlink = ip->i_nlink; 11997 ACQUIRE_LOCK(ip->i_ump); 11998 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 11999 &inodedep) == 0) { 12000 FREE_LOCK(ip->i_ump); 12001 return; 12002 } 12003 ip->i_effnlink -= inodedep->id_nlinkdelta; 12004 FREE_LOCK(ip->i_ump); 12005 } 12006 12007 /* 12008 * This routine is called just before the "in-core" inode 12009 * information is to be copied to the in-memory inode block. 12010 * Recall that an inode block contains several inodes. If 12011 * the force flag is set, then the dependencies will be 12012 * cleared so that the update can always be made. Note that 12013 * the buffer is locked when this routine is called, so we 12014 * will never be in the middle of writing the inode block 12015 * to disk. 12016 */ 12017 void 12018 softdep_update_inodeblock(ip, bp, waitfor) 12019 struct inode *ip; /* the "in_core" copy of the inode */ 12020 struct buf *bp; /* the buffer containing the inode block */ 12021 int waitfor; /* nonzero => update must be allowed */ 12022 { 12023 struct inodedep *inodedep; 12024 struct inoref *inoref; 12025 struct ufsmount *ump; 12026 struct worklist *wk; 12027 struct mount *mp; 12028 struct buf *ibp; 12029 struct fs *fs; 12030 int error; 12031 12032 ump = ip->i_ump; 12033 mp = UFSTOVFS(ump); 12034 KASSERT(MOUNTEDSOFTDEP(mp) != 0, 12035 ("softdep_update_inodeblock called on non-softdep filesystem")); 12036 fs = ip->i_fs; 12037 /* 12038 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 12039 * does not have access to the in-core ip so must write directly into 12040 * the inode block buffer when setting freelink. 12041 */ 12042 if (fs->fs_magic == FS_UFS1_MAGIC) 12043 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 12044 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12045 else 12046 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 12047 ino_to_fsbo(fs, ip->i_number))->di_freelink); 12048 /* 12049 * If the effective link count is not equal to the actual link 12050 * count, then we must track the difference in an inodedep while 12051 * the inode is (potentially) tossed out of the cache. Otherwise, 12052 * if there is no existing inodedep, then there are no dependencies 12053 * to track. 12054 */ 12055 ACQUIRE_LOCK(ump); 12056 again: 12057 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12058 FREE_LOCK(ump); 12059 if (ip->i_effnlink != ip->i_nlink) 12060 panic("softdep_update_inodeblock: bad link count"); 12061 return; 12062 } 12063 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 12064 panic("softdep_update_inodeblock: bad delta"); 12065 /* 12066 * If we're flushing all dependencies we must also move any waiting 12067 * for journal writes onto the bufwait list prior to I/O. 12068 */ 12069 if (waitfor) { 12070 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12071 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12072 == DEPCOMPLETE) { 12073 jwait(&inoref->if_list, MNT_WAIT); 12074 goto again; 12075 } 12076 } 12077 } 12078 /* 12079 * Changes have been initiated. Anything depending on these 12080 * changes cannot occur until this inode has been written. 12081 */ 12082 inodedep->id_state &= ~COMPLETE; 12083 if ((inodedep->id_state & ONWORKLIST) == 0) 12084 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 12085 /* 12086 * Any new dependencies associated with the incore inode must 12087 * now be moved to the list associated with the buffer holding 12088 * the in-memory copy of the inode. Once merged process any 12089 * allocdirects that are completed by the merger. 12090 */ 12091 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 12092 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 12093 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 12094 NULL); 12095 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 12096 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 12097 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 12098 NULL); 12099 /* 12100 * Now that the inode has been pushed into the buffer, the 12101 * operations dependent on the inode being written to disk 12102 * can be moved to the id_bufwait so that they will be 12103 * processed when the buffer I/O completes. 12104 */ 12105 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 12106 WORKLIST_REMOVE(wk); 12107 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 12108 } 12109 /* 12110 * Newly allocated inodes cannot be written until the bitmap 12111 * that allocates them have been written (indicated by 12112 * DEPCOMPLETE being set in id_state). If we are doing a 12113 * forced sync (e.g., an fsync on a file), we force the bitmap 12114 * to be written so that the update can be done. 12115 */ 12116 if (waitfor == 0) { 12117 FREE_LOCK(ump); 12118 return; 12119 } 12120 retry: 12121 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 12122 FREE_LOCK(ump); 12123 return; 12124 } 12125 ibp = inodedep->id_bmsafemap->sm_buf; 12126 ibp = getdirtybuf(ibp, LOCK_PTR(ump), MNT_WAIT); 12127 if (ibp == NULL) { 12128 /* 12129 * If ibp came back as NULL, the dependency could have been 12130 * freed while we slept. Look it up again, and check to see 12131 * that it has completed. 12132 */ 12133 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 12134 goto retry; 12135 FREE_LOCK(ump); 12136 return; 12137 } 12138 FREE_LOCK(ump); 12139 if ((error = bwrite(ibp)) != 0) 12140 softdep_error("softdep_update_inodeblock: bwrite", error); 12141 } 12142 12143 /* 12144 * Merge the a new inode dependency list (such as id_newinoupdt) into an 12145 * old inode dependency list (such as id_inoupdt). This routine must be 12146 * called with splbio interrupts blocked. 12147 */ 12148 static void 12149 merge_inode_lists(newlisthead, oldlisthead) 12150 struct allocdirectlst *newlisthead; 12151 struct allocdirectlst *oldlisthead; 12152 { 12153 struct allocdirect *listadp, *newadp; 12154 12155 newadp = TAILQ_FIRST(newlisthead); 12156 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 12157 if (listadp->ad_offset < newadp->ad_offset) { 12158 listadp = TAILQ_NEXT(listadp, ad_next); 12159 continue; 12160 } 12161 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12162 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 12163 if (listadp->ad_offset == newadp->ad_offset) { 12164 allocdirect_merge(oldlisthead, newadp, 12165 listadp); 12166 listadp = newadp; 12167 } 12168 newadp = TAILQ_FIRST(newlisthead); 12169 } 12170 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 12171 TAILQ_REMOVE(newlisthead, newadp, ad_next); 12172 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 12173 } 12174 } 12175 12176 /* 12177 * If we are doing an fsync, then we must ensure that any directory 12178 * entries for the inode have been written after the inode gets to disk. 12179 */ 12180 int 12181 softdep_fsync(vp) 12182 struct vnode *vp; /* the "in_core" copy of the inode */ 12183 { 12184 struct inodedep *inodedep; 12185 struct pagedep *pagedep; 12186 struct inoref *inoref; 12187 struct ufsmount *ump; 12188 struct worklist *wk; 12189 struct diradd *dap; 12190 struct mount *mp; 12191 struct vnode *pvp; 12192 struct inode *ip; 12193 struct buf *bp; 12194 struct fs *fs; 12195 struct thread *td = curthread; 12196 int error, flushparent, pagedep_new_block; 12197 ino_t parentino; 12198 ufs_lbn_t lbn; 12199 12200 ip = VTOI(vp); 12201 fs = ip->i_fs; 12202 ump = ip->i_ump; 12203 mp = vp->v_mount; 12204 if (MOUNTEDSOFTDEP(mp) == 0) 12205 return (0); 12206 ACQUIRE_LOCK(ump); 12207 restart: 12208 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 12209 FREE_LOCK(ump); 12210 return (0); 12211 } 12212 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12213 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12214 == DEPCOMPLETE) { 12215 jwait(&inoref->if_list, MNT_WAIT); 12216 goto restart; 12217 } 12218 } 12219 if (!LIST_EMPTY(&inodedep->id_inowait) || 12220 !TAILQ_EMPTY(&inodedep->id_extupdt) || 12221 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 12222 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 12223 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 12224 panic("softdep_fsync: pending ops %p", inodedep); 12225 for (error = 0, flushparent = 0; ; ) { 12226 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 12227 break; 12228 if (wk->wk_type != D_DIRADD) 12229 panic("softdep_fsync: Unexpected type %s", 12230 TYPENAME(wk->wk_type)); 12231 dap = WK_DIRADD(wk); 12232 /* 12233 * Flush our parent if this directory entry has a MKDIR_PARENT 12234 * dependency or is contained in a newly allocated block. 12235 */ 12236 if (dap->da_state & DIRCHG) 12237 pagedep = dap->da_previous->dm_pagedep; 12238 else 12239 pagedep = dap->da_pagedep; 12240 parentino = pagedep->pd_ino; 12241 lbn = pagedep->pd_lbn; 12242 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 12243 panic("softdep_fsync: dirty"); 12244 if ((dap->da_state & MKDIR_PARENT) || 12245 (pagedep->pd_state & NEWBLOCK)) 12246 flushparent = 1; 12247 else 12248 flushparent = 0; 12249 /* 12250 * If we are being fsync'ed as part of vgone'ing this vnode, 12251 * then we will not be able to release and recover the 12252 * vnode below, so we just have to give up on writing its 12253 * directory entry out. It will eventually be written, just 12254 * not now, but then the user was not asking to have it 12255 * written, so we are not breaking any promises. 12256 */ 12257 if (vp->v_iflag & VI_DOOMED) 12258 break; 12259 /* 12260 * We prevent deadlock by always fetching inodes from the 12261 * root, moving down the directory tree. Thus, when fetching 12262 * our parent directory, we first try to get the lock. If 12263 * that fails, we must unlock ourselves before requesting 12264 * the lock on our parent. See the comment in ufs_lookup 12265 * for details on possible races. 12266 */ 12267 FREE_LOCK(ump); 12268 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 12269 FFSV_FORCEINSMQ)) { 12270 error = vfs_busy(mp, MBF_NOWAIT); 12271 if (error != 0) { 12272 vfs_ref(mp); 12273 VOP_UNLOCK(vp, 0); 12274 error = vfs_busy(mp, 0); 12275 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12276 vfs_rel(mp); 12277 if (error != 0) 12278 return (ENOENT); 12279 if (vp->v_iflag & VI_DOOMED) { 12280 vfs_unbusy(mp); 12281 return (ENOENT); 12282 } 12283 } 12284 VOP_UNLOCK(vp, 0); 12285 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12286 &pvp, FFSV_FORCEINSMQ); 12287 vfs_unbusy(mp); 12288 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12289 if (vp->v_iflag & VI_DOOMED) { 12290 if (error == 0) 12291 vput(pvp); 12292 error = ENOENT; 12293 } 12294 if (error != 0) 12295 return (error); 12296 } 12297 /* 12298 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12299 * that are contained in direct blocks will be resolved by 12300 * doing a ffs_update. Pagedeps contained in indirect blocks 12301 * may require a complete sync'ing of the directory. So, we 12302 * try the cheap and fast ffs_update first, and if that fails, 12303 * then we do the slower ffs_syncvnode of the directory. 12304 */ 12305 if (flushparent) { 12306 int locked; 12307 12308 if ((error = ffs_update(pvp, 1)) != 0) { 12309 vput(pvp); 12310 return (error); 12311 } 12312 ACQUIRE_LOCK(ump); 12313 locked = 1; 12314 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12315 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12316 if (wk->wk_type != D_DIRADD) 12317 panic("softdep_fsync: Unexpected type %s", 12318 TYPENAME(wk->wk_type)); 12319 dap = WK_DIRADD(wk); 12320 if (dap->da_state & DIRCHG) 12321 pagedep = dap->da_previous->dm_pagedep; 12322 else 12323 pagedep = dap->da_pagedep; 12324 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12325 FREE_LOCK(ump); 12326 locked = 0; 12327 if (pagedep_new_block && (error = 12328 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12329 vput(pvp); 12330 return (error); 12331 } 12332 } 12333 } 12334 if (locked) 12335 FREE_LOCK(ump); 12336 } 12337 /* 12338 * Flush directory page containing the inode's name. 12339 */ 12340 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12341 &bp); 12342 if (error == 0) 12343 error = bwrite(bp); 12344 else 12345 brelse(bp); 12346 vput(pvp); 12347 if (error != 0) 12348 return (error); 12349 ACQUIRE_LOCK(ump); 12350 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12351 break; 12352 } 12353 FREE_LOCK(ump); 12354 return (0); 12355 } 12356 12357 /* 12358 * Flush all the dirty bitmaps associated with the block device 12359 * before flushing the rest of the dirty blocks so as to reduce 12360 * the number of dependencies that will have to be rolled back. 12361 * 12362 * XXX Unused? 12363 */ 12364 void 12365 softdep_fsync_mountdev(vp) 12366 struct vnode *vp; 12367 { 12368 struct buf *bp, *nbp; 12369 struct worklist *wk; 12370 struct bufobj *bo; 12371 12372 if (!vn_isdisk(vp, NULL)) 12373 panic("softdep_fsync_mountdev: vnode not a disk"); 12374 bo = &vp->v_bufobj; 12375 restart: 12376 BO_LOCK(bo); 12377 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12378 /* 12379 * If it is already scheduled, skip to the next buffer. 12380 */ 12381 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12382 continue; 12383 12384 if ((bp->b_flags & B_DELWRI) == 0) 12385 panic("softdep_fsync_mountdev: not dirty"); 12386 /* 12387 * We are only interested in bitmaps with outstanding 12388 * dependencies. 12389 */ 12390 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12391 wk->wk_type != D_BMSAFEMAP || 12392 (bp->b_vflags & BV_BKGRDINPROG)) { 12393 BUF_UNLOCK(bp); 12394 continue; 12395 } 12396 BO_UNLOCK(bo); 12397 bremfree(bp); 12398 (void) bawrite(bp); 12399 goto restart; 12400 } 12401 drain_output(vp); 12402 BO_UNLOCK(bo); 12403 } 12404 12405 /* 12406 * Sync all cylinder groups that were dirty at the time this function is 12407 * called. Newly dirtied cgs will be inserted before the sentinel. This 12408 * is used to flush freedep activity that may be holding up writes to a 12409 * indirect block. 12410 */ 12411 static int 12412 sync_cgs(mp, waitfor) 12413 struct mount *mp; 12414 int waitfor; 12415 { 12416 struct bmsafemap *bmsafemap; 12417 struct bmsafemap *sentinel; 12418 struct ufsmount *ump; 12419 struct buf *bp; 12420 int error; 12421 12422 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12423 sentinel->sm_cg = -1; 12424 ump = VFSTOUFS(mp); 12425 error = 0; 12426 ACQUIRE_LOCK(ump); 12427 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12428 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12429 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12430 /* Skip sentinels and cgs with no work to release. */ 12431 if (bmsafemap->sm_cg == -1 || 12432 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12433 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12434 LIST_REMOVE(sentinel, sm_next); 12435 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12436 continue; 12437 } 12438 /* 12439 * If we don't get the lock and we're waiting try again, if 12440 * not move on to the next buf and try to sync it. 12441 */ 12442 bp = getdirtybuf(bmsafemap->sm_buf, LOCK_PTR(ump), waitfor); 12443 if (bp == NULL && waitfor == MNT_WAIT) 12444 continue; 12445 LIST_REMOVE(sentinel, sm_next); 12446 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12447 if (bp == NULL) 12448 continue; 12449 FREE_LOCK(ump); 12450 if (waitfor == MNT_NOWAIT) 12451 bawrite(bp); 12452 else 12453 error = bwrite(bp); 12454 ACQUIRE_LOCK(ump); 12455 if (error) 12456 break; 12457 } 12458 LIST_REMOVE(sentinel, sm_next); 12459 FREE_LOCK(ump); 12460 free(sentinel, M_BMSAFEMAP); 12461 return (error); 12462 } 12463 12464 /* 12465 * This routine is called when we are trying to synchronously flush a 12466 * file. This routine must eliminate any filesystem metadata dependencies 12467 * so that the syncing routine can succeed. 12468 */ 12469 int 12470 softdep_sync_metadata(struct vnode *vp) 12471 { 12472 struct inode *ip; 12473 int error; 12474 12475 ip = VTOI(vp); 12476 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 12477 ("softdep_sync_metadata called on non-softdep filesystem")); 12478 /* 12479 * Ensure that any direct block dependencies have been cleared, 12480 * truncations are started, and inode references are journaled. 12481 */ 12482 ACQUIRE_LOCK(ip->i_ump); 12483 /* 12484 * Write all journal records to prevent rollbacks on devvp. 12485 */ 12486 if (vp->v_type == VCHR) 12487 softdep_flushjournal(vp->v_mount); 12488 error = flush_inodedep_deps(vp, vp->v_mount, ip->i_number); 12489 /* 12490 * Ensure that all truncates are written so we won't find deps on 12491 * indirect blocks. 12492 */ 12493 process_truncates(vp); 12494 FREE_LOCK(ip->i_ump); 12495 12496 return (error); 12497 } 12498 12499 /* 12500 * This routine is called when we are attempting to sync a buf with 12501 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12502 * other IO it can but returns EBUSY if the buffer is not yet able to 12503 * be written. Dependencies which will not cause rollbacks will always 12504 * return 0. 12505 */ 12506 int 12507 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12508 { 12509 struct indirdep *indirdep; 12510 struct pagedep *pagedep; 12511 struct allocindir *aip; 12512 struct newblk *newblk; 12513 struct ufsmount *ump; 12514 struct buf *nbp; 12515 struct worklist *wk; 12516 int i, error; 12517 12518 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 12519 ("softdep_sync_buf called on non-softdep filesystem")); 12520 /* 12521 * For VCHR we just don't want to force flush any dependencies that 12522 * will cause rollbacks. 12523 */ 12524 if (vp->v_type == VCHR) { 12525 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12526 return (EBUSY); 12527 return (0); 12528 } 12529 ump = VTOI(vp)->i_ump; 12530 ACQUIRE_LOCK(ump); 12531 /* 12532 * As we hold the buffer locked, none of its dependencies 12533 * will disappear. 12534 */ 12535 error = 0; 12536 top: 12537 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12538 switch (wk->wk_type) { 12539 12540 case D_ALLOCDIRECT: 12541 case D_ALLOCINDIR: 12542 newblk = WK_NEWBLK(wk); 12543 if (newblk->nb_jnewblk != NULL) { 12544 if (waitfor == MNT_NOWAIT) { 12545 error = EBUSY; 12546 goto out_unlock; 12547 } 12548 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12549 goto top; 12550 } 12551 if (newblk->nb_state & DEPCOMPLETE || 12552 waitfor == MNT_NOWAIT) 12553 continue; 12554 nbp = newblk->nb_bmsafemap->sm_buf; 12555 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12556 if (nbp == NULL) 12557 goto top; 12558 FREE_LOCK(ump); 12559 if ((error = bwrite(nbp)) != 0) 12560 goto out; 12561 ACQUIRE_LOCK(ump); 12562 continue; 12563 12564 case D_INDIRDEP: 12565 indirdep = WK_INDIRDEP(wk); 12566 if (waitfor == MNT_NOWAIT) { 12567 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12568 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12569 error = EBUSY; 12570 goto out_unlock; 12571 } 12572 } 12573 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12574 panic("softdep_sync_buf: truncation pending."); 12575 restart: 12576 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12577 newblk = (struct newblk *)aip; 12578 if (newblk->nb_jnewblk != NULL) { 12579 jwait(&newblk->nb_jnewblk->jn_list, 12580 waitfor); 12581 goto restart; 12582 } 12583 if (newblk->nb_state & DEPCOMPLETE) 12584 continue; 12585 nbp = newblk->nb_bmsafemap->sm_buf; 12586 nbp = getdirtybuf(nbp, LOCK_PTR(ump), waitfor); 12587 if (nbp == NULL) 12588 goto restart; 12589 FREE_LOCK(ump); 12590 if ((error = bwrite(nbp)) != 0) 12591 goto out; 12592 ACQUIRE_LOCK(ump); 12593 goto restart; 12594 } 12595 continue; 12596 12597 case D_PAGEDEP: 12598 /* 12599 * Only flush directory entries in synchronous passes. 12600 */ 12601 if (waitfor != MNT_WAIT) { 12602 error = EBUSY; 12603 goto out_unlock; 12604 } 12605 /* 12606 * While syncing snapshots, we must allow recursive 12607 * lookups. 12608 */ 12609 BUF_AREC(bp); 12610 /* 12611 * We are trying to sync a directory that may 12612 * have dependencies on both its own metadata 12613 * and/or dependencies on the inodes of any 12614 * recently allocated files. We walk its diradd 12615 * lists pushing out the associated inode. 12616 */ 12617 pagedep = WK_PAGEDEP(wk); 12618 for (i = 0; i < DAHASHSZ; i++) { 12619 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12620 continue; 12621 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12622 &pagedep->pd_diraddhd[i]))) { 12623 BUF_NOREC(bp); 12624 goto out_unlock; 12625 } 12626 } 12627 BUF_NOREC(bp); 12628 continue; 12629 12630 case D_FREEWORK: 12631 case D_FREEDEP: 12632 case D_JSEGDEP: 12633 case D_JNEWBLK: 12634 continue; 12635 12636 default: 12637 panic("softdep_sync_buf: Unknown type %s", 12638 TYPENAME(wk->wk_type)); 12639 /* NOTREACHED */ 12640 } 12641 } 12642 out_unlock: 12643 FREE_LOCK(ump); 12644 out: 12645 return (error); 12646 } 12647 12648 /* 12649 * Flush the dependencies associated with an inodedep. 12650 * Called with splbio blocked. 12651 */ 12652 static int 12653 flush_inodedep_deps(vp, mp, ino) 12654 struct vnode *vp; 12655 struct mount *mp; 12656 ino_t ino; 12657 { 12658 struct inodedep *inodedep; 12659 struct inoref *inoref; 12660 struct ufsmount *ump; 12661 int error, waitfor; 12662 12663 /* 12664 * This work is done in two passes. The first pass grabs most 12665 * of the buffers and begins asynchronously writing them. The 12666 * only way to wait for these asynchronous writes is to sleep 12667 * on the filesystem vnode which may stay busy for a long time 12668 * if the filesystem is active. So, instead, we make a second 12669 * pass over the dependencies blocking on each write. In the 12670 * usual case we will be blocking against a write that we 12671 * initiated, so when it is done the dependency will have been 12672 * resolved. Thus the second pass is expected to end quickly. 12673 * We give a brief window at the top of the loop to allow 12674 * any pending I/O to complete. 12675 */ 12676 ump = VFSTOUFS(mp); 12677 LOCK_OWNED(ump); 12678 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12679 if (error) 12680 return (error); 12681 FREE_LOCK(ump); 12682 ACQUIRE_LOCK(ump); 12683 restart: 12684 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12685 return (0); 12686 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12687 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12688 == DEPCOMPLETE) { 12689 jwait(&inoref->if_list, MNT_WAIT); 12690 goto restart; 12691 } 12692 } 12693 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12694 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12695 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12696 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12697 continue; 12698 /* 12699 * If pass2, we are done, otherwise do pass 2. 12700 */ 12701 if (waitfor == MNT_WAIT) 12702 break; 12703 waitfor = MNT_WAIT; 12704 } 12705 /* 12706 * Try freeing inodedep in case all dependencies have been removed. 12707 */ 12708 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12709 (void) free_inodedep(inodedep); 12710 return (0); 12711 } 12712 12713 /* 12714 * Flush an inode dependency list. 12715 * Called with splbio blocked. 12716 */ 12717 static int 12718 flush_deplist(listhead, waitfor, errorp) 12719 struct allocdirectlst *listhead; 12720 int waitfor; 12721 int *errorp; 12722 { 12723 struct allocdirect *adp; 12724 struct newblk *newblk; 12725 struct ufsmount *ump; 12726 struct buf *bp; 12727 12728 if ((adp = TAILQ_FIRST(listhead)) == NULL) 12729 return (0); 12730 ump = VFSTOUFS(adp->ad_list.wk_mp); 12731 LOCK_OWNED(ump); 12732 TAILQ_FOREACH(adp, listhead, ad_next) { 12733 newblk = (struct newblk *)adp; 12734 if (newblk->nb_jnewblk != NULL) { 12735 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12736 return (1); 12737 } 12738 if (newblk->nb_state & DEPCOMPLETE) 12739 continue; 12740 bp = newblk->nb_bmsafemap->sm_buf; 12741 bp = getdirtybuf(bp, LOCK_PTR(ump), waitfor); 12742 if (bp == NULL) { 12743 if (waitfor == MNT_NOWAIT) 12744 continue; 12745 return (1); 12746 } 12747 FREE_LOCK(ump); 12748 if (waitfor == MNT_NOWAIT) 12749 bawrite(bp); 12750 else 12751 *errorp = bwrite(bp); 12752 ACQUIRE_LOCK(ump); 12753 return (1); 12754 } 12755 return (0); 12756 } 12757 12758 /* 12759 * Flush dependencies associated with an allocdirect block. 12760 */ 12761 static int 12762 flush_newblk_dep(vp, mp, lbn) 12763 struct vnode *vp; 12764 struct mount *mp; 12765 ufs_lbn_t lbn; 12766 { 12767 struct newblk *newblk; 12768 struct ufsmount *ump; 12769 struct bufobj *bo; 12770 struct inode *ip; 12771 struct buf *bp; 12772 ufs2_daddr_t blkno; 12773 int error; 12774 12775 error = 0; 12776 bo = &vp->v_bufobj; 12777 ip = VTOI(vp); 12778 blkno = DIP(ip, i_db[lbn]); 12779 if (blkno == 0) 12780 panic("flush_newblk_dep: Missing block"); 12781 ump = VFSTOUFS(mp); 12782 ACQUIRE_LOCK(ump); 12783 /* 12784 * Loop until all dependencies related to this block are satisfied. 12785 * We must be careful to restart after each sleep in case a write 12786 * completes some part of this process for us. 12787 */ 12788 for (;;) { 12789 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 12790 FREE_LOCK(ump); 12791 break; 12792 } 12793 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 12794 panic("flush_newblk_deps: Bad newblk %p", newblk); 12795 /* 12796 * Flush the journal. 12797 */ 12798 if (newblk->nb_jnewblk != NULL) { 12799 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12800 continue; 12801 } 12802 /* 12803 * Write the bitmap dependency. 12804 */ 12805 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 12806 bp = newblk->nb_bmsafemap->sm_buf; 12807 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 12808 if (bp == NULL) 12809 continue; 12810 FREE_LOCK(ump); 12811 error = bwrite(bp); 12812 if (error) 12813 break; 12814 ACQUIRE_LOCK(ump); 12815 continue; 12816 } 12817 /* 12818 * Write the buffer. 12819 */ 12820 FREE_LOCK(ump); 12821 BO_LOCK(bo); 12822 bp = gbincore(bo, lbn); 12823 if (bp != NULL) { 12824 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 12825 LK_INTERLOCK, BO_LOCKPTR(bo)); 12826 if (error == ENOLCK) { 12827 ACQUIRE_LOCK(ump); 12828 continue; /* Slept, retry */ 12829 } 12830 if (error != 0) 12831 break; /* Failed */ 12832 if (bp->b_flags & B_DELWRI) { 12833 bremfree(bp); 12834 error = bwrite(bp); 12835 if (error) 12836 break; 12837 } else 12838 BUF_UNLOCK(bp); 12839 } else 12840 BO_UNLOCK(bo); 12841 /* 12842 * We have to wait for the direct pointers to 12843 * point at the newdirblk before the dependency 12844 * will go away. 12845 */ 12846 error = ffs_update(vp, 1); 12847 if (error) 12848 break; 12849 ACQUIRE_LOCK(ump); 12850 } 12851 return (error); 12852 } 12853 12854 /* 12855 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 12856 * Called with splbio blocked. 12857 */ 12858 static int 12859 flush_pagedep_deps(pvp, mp, diraddhdp) 12860 struct vnode *pvp; 12861 struct mount *mp; 12862 struct diraddhd *diraddhdp; 12863 { 12864 struct inodedep *inodedep; 12865 struct inoref *inoref; 12866 struct ufsmount *ump; 12867 struct diradd *dap; 12868 struct vnode *vp; 12869 int error = 0; 12870 struct buf *bp; 12871 ino_t inum; 12872 struct diraddhd unfinished; 12873 12874 LIST_INIT(&unfinished); 12875 ump = VFSTOUFS(mp); 12876 LOCK_OWNED(ump); 12877 restart: 12878 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 12879 /* 12880 * Flush ourselves if this directory entry 12881 * has a MKDIR_PARENT dependency. 12882 */ 12883 if (dap->da_state & MKDIR_PARENT) { 12884 FREE_LOCK(ump); 12885 if ((error = ffs_update(pvp, 1)) != 0) 12886 break; 12887 ACQUIRE_LOCK(ump); 12888 /* 12889 * If that cleared dependencies, go on to next. 12890 */ 12891 if (dap != LIST_FIRST(diraddhdp)) 12892 continue; 12893 /* 12894 * All MKDIR_PARENT dependencies and all the 12895 * NEWBLOCK pagedeps that are contained in direct 12896 * blocks were resolved by doing above ffs_update. 12897 * Pagedeps contained in indirect blocks may 12898 * require a complete sync'ing of the directory. 12899 * We are in the midst of doing a complete sync, 12900 * so if they are not resolved in this pass we 12901 * defer them for now as they will be sync'ed by 12902 * our caller shortly. 12903 */ 12904 LIST_REMOVE(dap, da_pdlist); 12905 LIST_INSERT_HEAD(&unfinished, dap, da_pdlist); 12906 continue; 12907 } 12908 /* 12909 * A newly allocated directory must have its "." and 12910 * ".." entries written out before its name can be 12911 * committed in its parent. 12912 */ 12913 inum = dap->da_newinum; 12914 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12915 panic("flush_pagedep_deps: lost inode1"); 12916 /* 12917 * Wait for any pending journal adds to complete so we don't 12918 * cause rollbacks while syncing. 12919 */ 12920 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12921 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12922 == DEPCOMPLETE) { 12923 jwait(&inoref->if_list, MNT_WAIT); 12924 goto restart; 12925 } 12926 } 12927 if (dap->da_state & MKDIR_BODY) { 12928 FREE_LOCK(ump); 12929 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12930 FFSV_FORCEINSMQ))) 12931 break; 12932 error = flush_newblk_dep(vp, mp, 0); 12933 /* 12934 * If we still have the dependency we might need to 12935 * update the vnode to sync the new link count to 12936 * disk. 12937 */ 12938 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 12939 error = ffs_update(vp, 1); 12940 vput(vp); 12941 if (error != 0) 12942 break; 12943 ACQUIRE_LOCK(ump); 12944 /* 12945 * If that cleared dependencies, go on to next. 12946 */ 12947 if (dap != LIST_FIRST(diraddhdp)) 12948 continue; 12949 if (dap->da_state & MKDIR_BODY) { 12950 inodedep_lookup(UFSTOVFS(ump), inum, 0, 12951 &inodedep); 12952 panic("flush_pagedep_deps: MKDIR_BODY " 12953 "inodedep %p dap %p vp %p", 12954 inodedep, dap, vp); 12955 } 12956 } 12957 /* 12958 * Flush the inode on which the directory entry depends. 12959 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 12960 * the only remaining dependency is that the updated inode 12961 * count must get pushed to disk. The inode has already 12962 * been pushed into its inode buffer (via VOP_UPDATE) at 12963 * the time of the reference count change. So we need only 12964 * locate that buffer, ensure that there will be no rollback 12965 * caused by a bitmap dependency, then write the inode buffer. 12966 */ 12967 retry: 12968 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12969 panic("flush_pagedep_deps: lost inode"); 12970 /* 12971 * If the inode still has bitmap dependencies, 12972 * push them to disk. 12973 */ 12974 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 12975 bp = inodedep->id_bmsafemap->sm_buf; 12976 bp = getdirtybuf(bp, LOCK_PTR(ump), MNT_WAIT); 12977 if (bp == NULL) 12978 goto retry; 12979 FREE_LOCK(ump); 12980 if ((error = bwrite(bp)) != 0) 12981 break; 12982 ACQUIRE_LOCK(ump); 12983 if (dap != LIST_FIRST(diraddhdp)) 12984 continue; 12985 } 12986 /* 12987 * If the inode is still sitting in a buffer waiting 12988 * to be written or waiting for the link count to be 12989 * adjusted update it here to flush it to disk. 12990 */ 12991 if (dap == LIST_FIRST(diraddhdp)) { 12992 FREE_LOCK(ump); 12993 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12994 FFSV_FORCEINSMQ))) 12995 break; 12996 error = ffs_update(vp, 1); 12997 vput(vp); 12998 if (error) 12999 break; 13000 ACQUIRE_LOCK(ump); 13001 } 13002 /* 13003 * If we have failed to get rid of all the dependencies 13004 * then something is seriously wrong. 13005 */ 13006 if (dap == LIST_FIRST(diraddhdp)) { 13007 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 13008 panic("flush_pagedep_deps: failed to flush " 13009 "inodedep %p ino %ju dap %p", 13010 inodedep, (uintmax_t)inum, dap); 13011 } 13012 } 13013 if (error) 13014 ACQUIRE_LOCK(ump); 13015 while ((dap = LIST_FIRST(&unfinished)) != NULL) { 13016 LIST_REMOVE(dap, da_pdlist); 13017 LIST_INSERT_HEAD(diraddhdp, dap, da_pdlist); 13018 } 13019 return (error); 13020 } 13021 13022 /* 13023 * A large burst of file addition or deletion activity can drive the 13024 * memory load excessively high. First attempt to slow things down 13025 * using the techniques below. If that fails, this routine requests 13026 * the offending operations to fall back to running synchronously 13027 * until the memory load returns to a reasonable level. 13028 */ 13029 int 13030 softdep_slowdown(vp) 13031 struct vnode *vp; 13032 { 13033 struct ufsmount *ump; 13034 int jlow; 13035 int max_softdeps_hard; 13036 13037 KASSERT(MOUNTEDSOFTDEP(vp->v_mount) != 0, 13038 ("softdep_slowdown called on non-softdep filesystem")); 13039 ump = VFSTOUFS(vp->v_mount); 13040 ACQUIRE_LOCK(ump); 13041 jlow = 0; 13042 /* 13043 * Check for journal space if needed. 13044 */ 13045 if (DOINGSUJ(vp)) { 13046 if (journal_space(ump, 0) == 0) 13047 jlow = 1; 13048 } 13049 /* 13050 * If the system is under its limits and our filesystem is 13051 * not responsible for more than our share of the usage and 13052 * we are not low on journal space, then no need to slow down. 13053 */ 13054 max_softdeps_hard = max_softdeps * 11 / 10; 13055 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 13056 dep_current[D_INODEDEP] < max_softdeps_hard && 13057 dep_current[D_INDIRDEP] < max_softdeps_hard / 1000 && 13058 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0 && 13059 ump->softdep_curdeps[D_DIRREM] < 13060 (max_softdeps_hard / 2) / stat_flush_threads && 13061 ump->softdep_curdeps[D_INODEDEP] < 13062 max_softdeps_hard / stat_flush_threads && 13063 ump->softdep_curdeps[D_INDIRDEP] < 13064 (max_softdeps_hard / 1000) / stat_flush_threads && 13065 ump->softdep_curdeps[D_FREEBLKS] < 13066 max_softdeps_hard / stat_flush_threads) { 13067 FREE_LOCK(ump); 13068 return (0); 13069 } 13070 /* 13071 * If the journal is low or our filesystem is over its limit 13072 * then speedup the cleanup. 13073 */ 13074 if (ump->softdep_curdeps[D_INDIRDEP] < 13075 (max_softdeps_hard / 1000) / stat_flush_threads || jlow) 13076 softdep_speedup(ump); 13077 stat_sync_limit_hit += 1; 13078 FREE_LOCK(ump); 13079 /* 13080 * We only slow down the rate at which new dependencies are 13081 * generated if we are not using journaling. With journaling, 13082 * the cleanup should always be sufficient to keep things 13083 * under control. 13084 */ 13085 if (DOINGSUJ(vp)) 13086 return (0); 13087 return (1); 13088 } 13089 13090 /* 13091 * Called by the allocation routines when they are about to fail 13092 * in the hope that we can free up the requested resource (inodes 13093 * or disk space). 13094 * 13095 * First check to see if the work list has anything on it. If it has, 13096 * clean up entries until we successfully free the requested resource. 13097 * Because this process holds inodes locked, we cannot handle any remove 13098 * requests that might block on a locked inode as that could lead to 13099 * deadlock. If the worklist yields none of the requested resource, 13100 * start syncing out vnodes to free up the needed space. 13101 */ 13102 int 13103 softdep_request_cleanup(fs, vp, cred, resource) 13104 struct fs *fs; 13105 struct vnode *vp; 13106 struct ucred *cred; 13107 int resource; 13108 { 13109 struct ufsmount *ump; 13110 struct mount *mp; 13111 struct vnode *lvp, *mvp; 13112 long starttime; 13113 ufs2_daddr_t needed; 13114 int error; 13115 13116 /* 13117 * If we are being called because of a process doing a 13118 * copy-on-write, then it is not safe to process any 13119 * worklist items as we will recurse into the copyonwrite 13120 * routine. This will result in an incoherent snapshot. 13121 * If the vnode that we hold is a snapshot, we must avoid 13122 * handling other resources that could cause deadlock. 13123 */ 13124 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 13125 return (0); 13126 13127 if (resource == FLUSH_BLOCKS_WAIT) 13128 stat_cleanup_blkrequests += 1; 13129 else 13130 stat_cleanup_inorequests += 1; 13131 13132 mp = vp->v_mount; 13133 ump = VFSTOUFS(mp); 13134 mtx_assert(UFS_MTX(ump), MA_OWNED); 13135 UFS_UNLOCK(ump); 13136 error = ffs_update(vp, 1); 13137 if (error != 0 || MOUNTEDSOFTDEP(mp) == 0) { 13138 UFS_LOCK(ump); 13139 return (0); 13140 } 13141 /* 13142 * If we are in need of resources, start by cleaning up 13143 * any block removals associated with our inode. 13144 */ 13145 ACQUIRE_LOCK(ump); 13146 process_removes(vp); 13147 process_truncates(vp); 13148 FREE_LOCK(ump); 13149 /* 13150 * Now clean up at least as many resources as we will need. 13151 * 13152 * When requested to clean up inodes, the number that are needed 13153 * is set by the number of simultaneous writers (mnt_writeopcount) 13154 * plus a bit of slop (2) in case some more writers show up while 13155 * we are cleaning. 13156 * 13157 * When requested to free up space, the amount of space that 13158 * we need is enough blocks to allocate a full-sized segment 13159 * (fs_contigsumsize). The number of such segments that will 13160 * be needed is set by the number of simultaneous writers 13161 * (mnt_writeopcount) plus a bit of slop (2) in case some more 13162 * writers show up while we are cleaning. 13163 * 13164 * Additionally, if we are unpriviledged and allocating space, 13165 * we need to ensure that we clean up enough blocks to get the 13166 * needed number of blocks over the threshhold of the minimum 13167 * number of blocks required to be kept free by the filesystem 13168 * (fs_minfree). 13169 */ 13170 if (resource == FLUSH_INODES_WAIT) { 13171 needed = vp->v_mount->mnt_writeopcount + 2; 13172 } else if (resource == FLUSH_BLOCKS_WAIT) { 13173 needed = (vp->v_mount->mnt_writeopcount + 2) * 13174 fs->fs_contigsumsize; 13175 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 13176 needed += fragstoblks(fs, 13177 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 13178 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 13179 } else { 13180 UFS_LOCK(ump); 13181 printf("softdep_request_cleanup: Unknown resource type %d\n", 13182 resource); 13183 return (0); 13184 } 13185 starttime = time_second; 13186 retry: 13187 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 13188 fs->fs_cstotal.cs_nbfree <= needed) || 13189 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13190 fs->fs_cstotal.cs_nifree <= needed)) { 13191 ACQUIRE_LOCK(ump); 13192 if (ump->softdep_on_worklist > 0 && 13193 process_worklist_item(UFSTOVFS(ump), 13194 ump->softdep_on_worklist, LK_NOWAIT) != 0) 13195 stat_worklist_push += 1; 13196 FREE_LOCK(ump); 13197 } 13198 /* 13199 * If we still need resources and there are no more worklist 13200 * entries to process to obtain them, we have to start flushing 13201 * the dirty vnodes to force the release of additional requests 13202 * to the worklist that we can then process to reap addition 13203 * resources. We walk the vnodes associated with the mount point 13204 * until we get the needed worklist requests that we can reap. 13205 */ 13206 if ((resource == FLUSH_BLOCKS_WAIT && 13207 fs->fs_cstotal.cs_nbfree <= needed) || 13208 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 13209 fs->fs_cstotal.cs_nifree <= needed)) { 13210 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 13211 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 13212 VI_UNLOCK(lvp); 13213 continue; 13214 } 13215 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 13216 curthread)) 13217 continue; 13218 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 13219 vput(lvp); 13220 continue; 13221 } 13222 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 13223 vput(lvp); 13224 } 13225 lvp = ump->um_devvp; 13226 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 13227 VOP_FSYNC(lvp, MNT_NOWAIT, curthread); 13228 VOP_UNLOCK(lvp, 0); 13229 } 13230 if (ump->softdep_on_worklist > 0) { 13231 stat_cleanup_retries += 1; 13232 goto retry; 13233 } 13234 stat_cleanup_failures += 1; 13235 } 13236 if (time_second - starttime > stat_cleanup_high_delay) 13237 stat_cleanup_high_delay = time_second - starttime; 13238 UFS_LOCK(ump); 13239 return (1); 13240 } 13241 13242 /* 13243 * If memory utilization has gotten too high, deliberately slow things 13244 * down and speed up the I/O processing. 13245 */ 13246 static int 13247 request_cleanup(mp, resource) 13248 struct mount *mp; 13249 int resource; 13250 { 13251 struct thread *td = curthread; 13252 struct ufsmount *ump; 13253 13254 ump = VFSTOUFS(mp); 13255 LOCK_OWNED(ump); 13256 /* 13257 * We never hold up the filesystem syncer or buf daemon. 13258 */ 13259 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 13260 return (0); 13261 /* 13262 * First check to see if the work list has gotten backlogged. 13263 * If it has, co-opt this process to help clean up two entries. 13264 * Because this process may hold inodes locked, we cannot 13265 * handle any remove requests that might block on a locked 13266 * inode as that could lead to deadlock. We set TDP_SOFTDEP 13267 * to avoid recursively processing the worklist. 13268 */ 13269 if (ump->softdep_on_worklist > max_softdeps / 10) { 13270 td->td_pflags |= TDP_SOFTDEP; 13271 process_worklist_item(mp, 2, LK_NOWAIT); 13272 td->td_pflags &= ~TDP_SOFTDEP; 13273 stat_worklist_push += 2; 13274 return(1); 13275 } 13276 /* 13277 * Next, we attempt to speed up the syncer process. If that 13278 * is successful, then we allow the process to continue. 13279 */ 13280 if (softdep_speedup(ump) && 13281 resource != FLUSH_BLOCKS_WAIT && 13282 resource != FLUSH_INODES_WAIT) 13283 return(0); 13284 /* 13285 * If we are resource constrained on inode dependencies, try 13286 * flushing some dirty inodes. Otherwise, we are constrained 13287 * by file deletions, so try accelerating flushes of directories 13288 * with removal dependencies. We would like to do the cleanup 13289 * here, but we probably hold an inode locked at this point and 13290 * that might deadlock against one that we try to clean. So, 13291 * the best that we can do is request the syncer daemon to do 13292 * the cleanup for us. 13293 */ 13294 switch (resource) { 13295 13296 case FLUSH_INODES: 13297 case FLUSH_INODES_WAIT: 13298 ACQUIRE_GBLLOCK(&lk); 13299 stat_ino_limit_push += 1; 13300 req_clear_inodedeps += 1; 13301 FREE_GBLLOCK(&lk); 13302 stat_countp = &stat_ino_limit_hit; 13303 break; 13304 13305 case FLUSH_BLOCKS: 13306 case FLUSH_BLOCKS_WAIT: 13307 ACQUIRE_GBLLOCK(&lk); 13308 stat_blk_limit_push += 1; 13309 req_clear_remove += 1; 13310 FREE_GBLLOCK(&lk); 13311 stat_countp = &stat_blk_limit_hit; 13312 break; 13313 13314 default: 13315 panic("request_cleanup: unknown type"); 13316 } 13317 /* 13318 * Hopefully the syncer daemon will catch up and awaken us. 13319 * We wait at most tickdelay before proceeding in any case. 13320 */ 13321 ACQUIRE_GBLLOCK(&lk); 13322 FREE_LOCK(ump); 13323 proc_waiting += 1; 13324 if (callout_pending(&softdep_callout) == FALSE) 13325 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13326 pause_timer, 0); 13327 13328 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 13329 proc_waiting -= 1; 13330 FREE_GBLLOCK(&lk); 13331 ACQUIRE_LOCK(ump); 13332 return (1); 13333 } 13334 13335 /* 13336 * Awaken processes pausing in request_cleanup and clear proc_waiting 13337 * to indicate that there is no longer a timer running. Pause_timer 13338 * will be called with the global softdep mutex (&lk) locked. 13339 */ 13340 static void 13341 pause_timer(arg) 13342 void *arg; 13343 { 13344 13345 GBLLOCK_OWNED(&lk); 13346 /* 13347 * The callout_ API has acquired mtx and will hold it around this 13348 * function call. 13349 */ 13350 *stat_countp += proc_waiting; 13351 wakeup(&proc_waiting); 13352 } 13353 13354 /* 13355 * If requested, try removing inode or removal dependencies. 13356 */ 13357 static void 13358 check_clear_deps(mp) 13359 struct mount *mp; 13360 { 13361 13362 /* 13363 * If we are suspended, it may be because of our using 13364 * too many inodedeps, so help clear them out. 13365 */ 13366 if (MOUNTEDSUJ(mp) && VFSTOUFS(mp)->softdep_jblocks->jb_suspended) 13367 clear_inodedeps(mp); 13368 /* 13369 * General requests for cleanup of backed up dependencies 13370 */ 13371 ACQUIRE_GBLLOCK(&lk); 13372 if (req_clear_inodedeps) { 13373 req_clear_inodedeps -= 1; 13374 FREE_GBLLOCK(&lk); 13375 clear_inodedeps(mp); 13376 ACQUIRE_GBLLOCK(&lk); 13377 wakeup(&proc_waiting); 13378 } 13379 if (req_clear_remove) { 13380 req_clear_remove -= 1; 13381 FREE_GBLLOCK(&lk); 13382 clear_remove(mp); 13383 ACQUIRE_GBLLOCK(&lk); 13384 wakeup(&proc_waiting); 13385 } 13386 FREE_GBLLOCK(&lk); 13387 } 13388 13389 /* 13390 * Flush out a directory with at least one removal dependency in an effort to 13391 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13392 */ 13393 static void 13394 clear_remove(mp) 13395 struct mount *mp; 13396 { 13397 struct pagedep_hashhead *pagedephd; 13398 struct pagedep *pagedep; 13399 struct ufsmount *ump; 13400 struct vnode *vp; 13401 struct bufobj *bo; 13402 int error, cnt; 13403 ino_t ino; 13404 13405 ump = VFSTOUFS(mp); 13406 LOCK_OWNED(ump); 13407 13408 for (cnt = 0; cnt <= ump->pagedep_hash_size; cnt++) { 13409 pagedephd = &ump->pagedep_hashtbl[ump->pagedep_nextclean++]; 13410 if (ump->pagedep_nextclean > ump->pagedep_hash_size) 13411 ump->pagedep_nextclean = 0; 13412 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13413 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13414 continue; 13415 ino = pagedep->pd_ino; 13416 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13417 continue; 13418 FREE_LOCK(ump); 13419 13420 /* 13421 * Let unmount clear deps 13422 */ 13423 error = vfs_busy(mp, MBF_NOWAIT); 13424 if (error != 0) 13425 goto finish_write; 13426 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13427 FFSV_FORCEINSMQ); 13428 vfs_unbusy(mp); 13429 if (error != 0) { 13430 softdep_error("clear_remove: vget", error); 13431 goto finish_write; 13432 } 13433 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13434 softdep_error("clear_remove: fsync", error); 13435 bo = &vp->v_bufobj; 13436 BO_LOCK(bo); 13437 drain_output(vp); 13438 BO_UNLOCK(bo); 13439 vput(vp); 13440 finish_write: 13441 vn_finished_write(mp); 13442 ACQUIRE_LOCK(ump); 13443 return; 13444 } 13445 } 13446 } 13447 13448 /* 13449 * Clear out a block of dirty inodes in an effort to reduce 13450 * the number of inodedep dependency structures. 13451 */ 13452 static void 13453 clear_inodedeps(mp) 13454 struct mount *mp; 13455 { 13456 struct inodedep_hashhead *inodedephd; 13457 struct inodedep *inodedep; 13458 struct ufsmount *ump; 13459 struct vnode *vp; 13460 struct fs *fs; 13461 int error, cnt; 13462 ino_t firstino, lastino, ino; 13463 13464 ump = VFSTOUFS(mp); 13465 fs = ump->um_fs; 13466 LOCK_OWNED(ump); 13467 /* 13468 * Pick a random inode dependency to be cleared. 13469 * We will then gather up all the inodes in its block 13470 * that have dependencies and flush them out. 13471 */ 13472 for (cnt = 0; cnt <= ump->inodedep_hash_size; cnt++) { 13473 inodedephd = &ump->inodedep_hashtbl[ump->inodedep_nextclean++]; 13474 if (ump->inodedep_nextclean > ump->inodedep_hash_size) 13475 ump->inodedep_nextclean = 0; 13476 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13477 break; 13478 } 13479 if (inodedep == NULL) 13480 return; 13481 /* 13482 * Find the last inode in the block with dependencies. 13483 */ 13484 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 13485 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13486 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13487 break; 13488 /* 13489 * Asynchronously push all but the last inode with dependencies. 13490 * Synchronously push the last inode with dependencies to ensure 13491 * that the inode block gets written to free up the inodedeps. 13492 */ 13493 for (ino = firstino; ino <= lastino; ino++) { 13494 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13495 continue; 13496 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13497 continue; 13498 FREE_LOCK(ump); 13499 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13500 if (error != 0) { 13501 vn_finished_write(mp); 13502 ACQUIRE_LOCK(ump); 13503 return; 13504 } 13505 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13506 FFSV_FORCEINSMQ)) != 0) { 13507 softdep_error("clear_inodedeps: vget", error); 13508 vfs_unbusy(mp); 13509 vn_finished_write(mp); 13510 ACQUIRE_LOCK(ump); 13511 return; 13512 } 13513 vfs_unbusy(mp); 13514 if (ino == lastino) { 13515 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13516 softdep_error("clear_inodedeps: fsync1", error); 13517 } else { 13518 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13519 softdep_error("clear_inodedeps: fsync2", error); 13520 BO_LOCK(&vp->v_bufobj); 13521 drain_output(vp); 13522 BO_UNLOCK(&vp->v_bufobj); 13523 } 13524 vput(vp); 13525 vn_finished_write(mp); 13526 ACQUIRE_LOCK(ump); 13527 } 13528 } 13529 13530 void 13531 softdep_buf_append(bp, wkhd) 13532 struct buf *bp; 13533 struct workhead *wkhd; 13534 { 13535 struct worklist *wk; 13536 struct ufsmount *ump; 13537 13538 if ((wk = LIST_FIRST(wkhd)) == NULL) 13539 return; 13540 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13541 ("softdep_buf_append called on non-softdep filesystem")); 13542 ump = VFSTOUFS(wk->wk_mp); 13543 ACQUIRE_LOCK(ump); 13544 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13545 WORKLIST_REMOVE(wk); 13546 WORKLIST_INSERT(&bp->b_dep, wk); 13547 } 13548 FREE_LOCK(ump); 13549 13550 } 13551 13552 void 13553 softdep_inode_append(ip, cred, wkhd) 13554 struct inode *ip; 13555 struct ucred *cred; 13556 struct workhead *wkhd; 13557 { 13558 struct buf *bp; 13559 struct fs *fs; 13560 int error; 13561 13562 KASSERT(MOUNTEDSOFTDEP(UFSTOVFS(ip->i_ump)) != 0, 13563 ("softdep_inode_append called on non-softdep filesystem")); 13564 fs = ip->i_fs; 13565 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13566 (int)fs->fs_bsize, cred, &bp); 13567 if (error) { 13568 bqrelse(bp); 13569 softdep_freework(wkhd); 13570 return; 13571 } 13572 softdep_buf_append(bp, wkhd); 13573 bqrelse(bp); 13574 } 13575 13576 void 13577 softdep_freework(wkhd) 13578 struct workhead *wkhd; 13579 { 13580 struct worklist *wk; 13581 struct ufsmount *ump; 13582 13583 if ((wk = LIST_FIRST(wkhd)) == NULL) 13584 return; 13585 KASSERT(MOUNTEDSOFTDEP(wk->wk_mp) != 0, 13586 ("softdep_freework called on non-softdep filesystem")); 13587 ump = VFSTOUFS(wk->wk_mp); 13588 ACQUIRE_LOCK(ump); 13589 handle_jwork(wkhd); 13590 FREE_LOCK(ump); 13591 } 13592 13593 /* 13594 * Function to determine if the buffer has outstanding dependencies 13595 * that will cause a roll-back if the buffer is written. If wantcount 13596 * is set, return number of dependencies, otherwise just yes or no. 13597 */ 13598 static int 13599 softdep_count_dependencies(bp, wantcount) 13600 struct buf *bp; 13601 int wantcount; 13602 { 13603 struct worklist *wk; 13604 struct ufsmount *ump; 13605 struct bmsafemap *bmsafemap; 13606 struct freework *freework; 13607 struct inodedep *inodedep; 13608 struct indirdep *indirdep; 13609 struct freeblks *freeblks; 13610 struct allocindir *aip; 13611 struct pagedep *pagedep; 13612 struct dirrem *dirrem; 13613 struct newblk *newblk; 13614 struct mkdir *mkdir; 13615 struct diradd *dap; 13616 int i, retval; 13617 13618 retval = 0; 13619 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL) 13620 return (0); 13621 ump = VFSTOUFS(wk->wk_mp); 13622 ACQUIRE_LOCK(ump); 13623 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13624 switch (wk->wk_type) { 13625 13626 case D_INODEDEP: 13627 inodedep = WK_INODEDEP(wk); 13628 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 13629 /* bitmap allocation dependency */ 13630 retval += 1; 13631 if (!wantcount) 13632 goto out; 13633 } 13634 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 13635 /* direct block pointer dependency */ 13636 retval += 1; 13637 if (!wantcount) 13638 goto out; 13639 } 13640 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 13641 /* direct block pointer dependency */ 13642 retval += 1; 13643 if (!wantcount) 13644 goto out; 13645 } 13646 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 13647 /* Add reference dependency. */ 13648 retval += 1; 13649 if (!wantcount) 13650 goto out; 13651 } 13652 continue; 13653 13654 case D_INDIRDEP: 13655 indirdep = WK_INDIRDEP(wk); 13656 13657 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 13658 /* indirect truncation dependency */ 13659 retval += 1; 13660 if (!wantcount) 13661 goto out; 13662 } 13663 13664 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13665 /* indirect block pointer dependency */ 13666 retval += 1; 13667 if (!wantcount) 13668 goto out; 13669 } 13670 continue; 13671 13672 case D_PAGEDEP: 13673 pagedep = WK_PAGEDEP(wk); 13674 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 13675 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 13676 /* Journal remove ref dependency. */ 13677 retval += 1; 13678 if (!wantcount) 13679 goto out; 13680 } 13681 } 13682 for (i = 0; i < DAHASHSZ; i++) { 13683 13684 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 13685 /* directory entry dependency */ 13686 retval += 1; 13687 if (!wantcount) 13688 goto out; 13689 } 13690 } 13691 continue; 13692 13693 case D_BMSAFEMAP: 13694 bmsafemap = WK_BMSAFEMAP(wk); 13695 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 13696 /* Add reference dependency. */ 13697 retval += 1; 13698 if (!wantcount) 13699 goto out; 13700 } 13701 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 13702 /* Allocate block dependency. */ 13703 retval += 1; 13704 if (!wantcount) 13705 goto out; 13706 } 13707 continue; 13708 13709 case D_FREEBLKS: 13710 freeblks = WK_FREEBLKS(wk); 13711 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 13712 /* Freeblk journal dependency. */ 13713 retval += 1; 13714 if (!wantcount) 13715 goto out; 13716 } 13717 continue; 13718 13719 case D_ALLOCDIRECT: 13720 case D_ALLOCINDIR: 13721 newblk = WK_NEWBLK(wk); 13722 if (newblk->nb_jnewblk) { 13723 /* Journal allocate dependency. */ 13724 retval += 1; 13725 if (!wantcount) 13726 goto out; 13727 } 13728 continue; 13729 13730 case D_MKDIR: 13731 mkdir = WK_MKDIR(wk); 13732 if (mkdir->md_jaddref) { 13733 /* Journal reference dependency. */ 13734 retval += 1; 13735 if (!wantcount) 13736 goto out; 13737 } 13738 continue; 13739 13740 case D_FREEWORK: 13741 case D_FREEDEP: 13742 case D_JSEGDEP: 13743 case D_JSEG: 13744 case D_SBDEP: 13745 /* never a dependency on these blocks */ 13746 continue; 13747 13748 default: 13749 panic("softdep_count_dependencies: Unexpected type %s", 13750 TYPENAME(wk->wk_type)); 13751 /* NOTREACHED */ 13752 } 13753 } 13754 out: 13755 FREE_LOCK(ump); 13756 return retval; 13757 } 13758 13759 /* 13760 * Acquire exclusive access to a buffer. 13761 * Must be called with a locked mtx parameter. 13762 * Return acquired buffer or NULL on failure. 13763 */ 13764 static struct buf * 13765 getdirtybuf(bp, lock, waitfor) 13766 struct buf *bp; 13767 struct rwlock *lock; 13768 int waitfor; 13769 { 13770 int error; 13771 13772 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 13773 if (waitfor != MNT_WAIT) 13774 return (NULL); 13775 error = BUF_LOCK(bp, 13776 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 13777 /* 13778 * Even if we sucessfully acquire bp here, we have dropped 13779 * lock, which may violates our guarantee. 13780 */ 13781 if (error == 0) 13782 BUF_UNLOCK(bp); 13783 else if (error != ENOLCK) 13784 panic("getdirtybuf: inconsistent lock: %d", error); 13785 rw_wlock(lock); 13786 return (NULL); 13787 } 13788 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13789 if (lock != BO_LOCKPTR(bp->b_bufobj) && waitfor == MNT_WAIT) { 13790 rw_wunlock(lock); 13791 BO_LOCK(bp->b_bufobj); 13792 BUF_UNLOCK(bp); 13793 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13794 bp->b_vflags |= BV_BKGRDWAIT; 13795 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 13796 PRIBIO | PDROP, "getbuf", 0); 13797 } else 13798 BO_UNLOCK(bp->b_bufobj); 13799 rw_wlock(lock); 13800 return (NULL); 13801 } 13802 BUF_UNLOCK(bp); 13803 if (waitfor != MNT_WAIT) 13804 return (NULL); 13805 /* 13806 * The lock argument must be bp->b_vp's mutex in 13807 * this case. 13808 */ 13809 #ifdef DEBUG_VFS_LOCKS 13810 if (bp->b_vp->v_type != VCHR) 13811 ASSERT_BO_WLOCKED(bp->b_bufobj); 13812 #endif 13813 bp->b_vflags |= BV_BKGRDWAIT; 13814 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 13815 return (NULL); 13816 } 13817 if ((bp->b_flags & B_DELWRI) == 0) { 13818 BUF_UNLOCK(bp); 13819 return (NULL); 13820 } 13821 bremfree(bp); 13822 return (bp); 13823 } 13824 13825 13826 /* 13827 * Check if it is safe to suspend the file system now. On entry, 13828 * the vnode interlock for devvp should be held. Return 0 with 13829 * the mount interlock held if the file system can be suspended now, 13830 * otherwise return EAGAIN with the mount interlock held. 13831 */ 13832 int 13833 softdep_check_suspend(struct mount *mp, 13834 struct vnode *devvp, 13835 int softdep_depcnt, 13836 int softdep_accdepcnt, 13837 int secondary_writes, 13838 int secondary_accwrites) 13839 { 13840 struct bufobj *bo; 13841 struct ufsmount *ump; 13842 int error; 13843 13844 bo = &devvp->v_bufobj; 13845 ASSERT_BO_WLOCKED(bo); 13846 13847 /* 13848 * If we are not running with soft updates, then we need only 13849 * deal with secondary writes as we try to suspend. 13850 */ 13851 if (MOUNTEDSOFTDEP(mp) == 0) { 13852 MNT_ILOCK(mp); 13853 while (mp->mnt_secondary_writes != 0) { 13854 BO_UNLOCK(bo); 13855 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 13856 (PUSER - 1) | PDROP, "secwr", 0); 13857 BO_LOCK(bo); 13858 MNT_ILOCK(mp); 13859 } 13860 13861 /* 13862 * Reasons for needing more work before suspend: 13863 * - Dirty buffers on devvp. 13864 * - Secondary writes occurred after start of vnode sync loop 13865 */ 13866 error = 0; 13867 if (bo->bo_numoutput > 0 || 13868 bo->bo_dirty.bv_cnt > 0 || 13869 secondary_writes != 0 || 13870 mp->mnt_secondary_writes != 0 || 13871 secondary_accwrites != mp->mnt_secondary_accwrites) 13872 error = EAGAIN; 13873 BO_UNLOCK(bo); 13874 return (error); 13875 } 13876 13877 /* 13878 * If we are running with soft updates, then we need to coordinate 13879 * with them as we try to suspend. 13880 */ 13881 ump = VFSTOUFS(mp); 13882 for (;;) { 13883 if (!TRY_ACQUIRE_LOCK(ump)) { 13884 BO_UNLOCK(bo); 13885 ACQUIRE_LOCK(ump); 13886 FREE_LOCK(ump); 13887 BO_LOCK(bo); 13888 continue; 13889 } 13890 MNT_ILOCK(mp); 13891 if (mp->mnt_secondary_writes != 0) { 13892 FREE_LOCK(ump); 13893 BO_UNLOCK(bo); 13894 msleep(&mp->mnt_secondary_writes, 13895 MNT_MTX(mp), 13896 (PUSER - 1) | PDROP, "secwr", 0); 13897 BO_LOCK(bo); 13898 continue; 13899 } 13900 break; 13901 } 13902 13903 /* 13904 * Reasons for needing more work before suspend: 13905 * - Dirty buffers on devvp. 13906 * - Softdep activity occurred after start of vnode sync loop 13907 * - Secondary writes occurred after start of vnode sync loop 13908 */ 13909 error = 0; 13910 if (bo->bo_numoutput > 0 || 13911 bo->bo_dirty.bv_cnt > 0 || 13912 softdep_depcnt != 0 || 13913 ump->softdep_deps != 0 || 13914 softdep_accdepcnt != ump->softdep_accdeps || 13915 secondary_writes != 0 || 13916 mp->mnt_secondary_writes != 0 || 13917 secondary_accwrites != mp->mnt_secondary_accwrites) 13918 error = EAGAIN; 13919 FREE_LOCK(ump); 13920 BO_UNLOCK(bo); 13921 return (error); 13922 } 13923 13924 13925 /* 13926 * Get the number of dependency structures for the file system, both 13927 * the current number and the total number allocated. These will 13928 * later be used to detect that softdep processing has occurred. 13929 */ 13930 void 13931 softdep_get_depcounts(struct mount *mp, 13932 int *softdep_depsp, 13933 int *softdep_accdepsp) 13934 { 13935 struct ufsmount *ump; 13936 13937 if (MOUNTEDSOFTDEP(mp) == 0) { 13938 *softdep_depsp = 0; 13939 *softdep_accdepsp = 0; 13940 return; 13941 } 13942 ump = VFSTOUFS(mp); 13943 ACQUIRE_LOCK(ump); 13944 *softdep_depsp = ump->softdep_deps; 13945 *softdep_accdepsp = ump->softdep_accdeps; 13946 FREE_LOCK(ump); 13947 } 13948 13949 /* 13950 * Wait for pending output on a vnode to complete. 13951 * Must be called with vnode lock and interlock locked. 13952 * 13953 * XXX: Should just be a call to bufobj_wwait(). 13954 */ 13955 static void 13956 drain_output(vp) 13957 struct vnode *vp; 13958 { 13959 struct bufobj *bo; 13960 13961 bo = &vp->v_bufobj; 13962 ASSERT_VOP_LOCKED(vp, "drain_output"); 13963 ASSERT_BO_WLOCKED(bo); 13964 13965 while (bo->bo_numoutput) { 13966 bo->bo_flag |= BO_WWAIT; 13967 msleep((caddr_t)&bo->bo_numoutput, 13968 BO_LOCKPTR(bo), PRIBIO + 1, "drainvp", 0); 13969 } 13970 } 13971 13972 /* 13973 * Called whenever a buffer that is being invalidated or reallocated 13974 * contains dependencies. This should only happen if an I/O error has 13975 * occurred. The routine is called with the buffer locked. 13976 */ 13977 static void 13978 softdep_deallocate_dependencies(bp) 13979 struct buf *bp; 13980 { 13981 13982 if ((bp->b_ioflags & BIO_ERROR) == 0) 13983 panic("softdep_deallocate_dependencies: dangling deps"); 13984 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 13985 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 13986 else 13987 printf("softdep_deallocate_dependencies: " 13988 "got error %d while accessing filesystem\n", bp->b_error); 13989 if (bp->b_error != ENXIO) 13990 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 13991 } 13992 13993 /* 13994 * Function to handle asynchronous write errors in the filesystem. 13995 */ 13996 static void 13997 softdep_error(func, error) 13998 char *func; 13999 int error; 14000 { 14001 14002 /* XXX should do something better! */ 14003 printf("%s: got error %d while accessing filesystem\n", func, error); 14004 } 14005 14006 #ifdef DDB 14007 14008 static void 14009 inodedep_print(struct inodedep *inodedep, int verbose) 14010 { 14011 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d" 14012 " saveino %p\n", 14013 inodedep, inodedep->id_fs, inodedep->id_state, 14014 (intmax_t)inodedep->id_ino, 14015 (intmax_t)fsbtodb(inodedep->id_fs, 14016 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 14017 inodedep->id_nlinkdelta, inodedep->id_savednlink, 14018 inodedep->id_savedino1); 14019 14020 if (verbose == 0) 14021 return; 14022 14023 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 14024 "mkdiradd %p\n", 14025 LIST_FIRST(&inodedep->id_pendinghd), 14026 LIST_FIRST(&inodedep->id_bufwait), 14027 LIST_FIRST(&inodedep->id_inowait), 14028 TAILQ_FIRST(&inodedep->id_inoreflst), 14029 inodedep->id_mkdiradd); 14030 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 14031 TAILQ_FIRST(&inodedep->id_inoupdt), 14032 TAILQ_FIRST(&inodedep->id_newinoupdt), 14033 TAILQ_FIRST(&inodedep->id_extupdt), 14034 TAILQ_FIRST(&inodedep->id_newextupdt)); 14035 } 14036 14037 DB_SHOW_COMMAND(inodedep, db_show_inodedep) 14038 { 14039 14040 if (have_addr == 0) { 14041 db_printf("Address required\n"); 14042 return; 14043 } 14044 inodedep_print((struct inodedep*)addr, 1); 14045 } 14046 14047 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 14048 { 14049 struct inodedep_hashhead *inodedephd; 14050 struct inodedep *inodedep; 14051 struct ufsmount *ump; 14052 int cnt; 14053 14054 if (have_addr == 0) { 14055 db_printf("Address required\n"); 14056 return; 14057 } 14058 ump = (struct ufsmount *)addr; 14059 for (cnt = 0; cnt < ump->inodedep_hash_size; cnt++) { 14060 inodedephd = &ump->inodedep_hashtbl[cnt]; 14061 LIST_FOREACH(inodedep, inodedephd, id_hash) { 14062 inodedep_print(inodedep, 0); 14063 } 14064 } 14065 } 14066 14067 DB_SHOW_COMMAND(worklist, db_show_worklist) 14068 { 14069 struct worklist *wk; 14070 14071 if (have_addr == 0) { 14072 db_printf("Address required\n"); 14073 return; 14074 } 14075 wk = (struct worklist *)addr; 14076 printf("worklist: %p type %s state 0x%X\n", 14077 wk, TYPENAME(wk->wk_type), wk->wk_state); 14078 } 14079 14080 DB_SHOW_COMMAND(workhead, db_show_workhead) 14081 { 14082 struct workhead *wkhd; 14083 struct worklist *wk; 14084 int i; 14085 14086 if (have_addr == 0) { 14087 db_printf("Address required\n"); 14088 return; 14089 } 14090 wkhd = (struct workhead *)addr; 14091 wk = LIST_FIRST(wkhd); 14092 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 14093 db_printf("worklist: %p type %s state 0x%X", 14094 wk, TYPENAME(wk->wk_type), wk->wk_state); 14095 if (i == 100) 14096 db_printf("workhead overflow"); 14097 printf("\n"); 14098 } 14099 14100 14101 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 14102 { 14103 struct mkdirlist *mkdirlisthd; 14104 struct jaddref *jaddref; 14105 struct diradd *diradd; 14106 struct mkdir *mkdir; 14107 14108 if (have_addr == 0) { 14109 db_printf("Address required\n"); 14110 return; 14111 } 14112 mkdirlisthd = (struct mkdirlist *)addr; 14113 LIST_FOREACH(mkdir, mkdirlisthd, md_mkdirs) { 14114 diradd = mkdir->md_diradd; 14115 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 14116 mkdir, mkdir->md_state, diradd, diradd->da_state); 14117 if ((jaddref = mkdir->md_jaddref) != NULL) 14118 db_printf(" jaddref %p jaddref state 0x%X", 14119 jaddref, jaddref->ja_state); 14120 db_printf("\n"); 14121 } 14122 } 14123 14124 /* exported to ffs_vfsops.c */ 14125 extern void db_print_ffs(struct ufsmount *ump); 14126 void 14127 db_print_ffs(struct ufsmount *ump) 14128 { 14129 db_printf("mp %p %s devvp %p fs %p su_wl %d su_deps %d su_req %d\n", 14130 ump->um_mountp, ump->um_mountp->mnt_stat.f_mntonname, 14131 ump->um_devvp, ump->um_fs, ump->softdep_on_worklist, 14132 ump->softdep_deps, ump->softdep_req); 14133 } 14134 14135 #endif /* DDB */ 14136 14137 #endif /* SOFTUPDATES */ 14138