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 } 142 143 void 144 softdep_setup_sbupdate(ump, fs, bp) 145 struct ufsmount *ump; 146 struct fs *fs; 147 struct buf *bp; 148 { 149 } 150 151 void 152 softdep_setup_inomapdep(bp, ip, newinum, mode) 153 struct buf *bp; 154 struct inode *ip; 155 ino_t newinum; 156 int mode; 157 { 158 159 panic("softdep_setup_inomapdep called"); 160 } 161 162 void 163 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 164 struct buf *bp; 165 struct mount *mp; 166 ufs2_daddr_t newblkno; 167 int frags; 168 int oldfrags; 169 { 170 171 panic("softdep_setup_blkmapdep called"); 172 } 173 174 void 175 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 176 struct inode *ip; 177 ufs_lbn_t lbn; 178 ufs2_daddr_t newblkno; 179 ufs2_daddr_t oldblkno; 180 long newsize; 181 long oldsize; 182 struct buf *bp; 183 { 184 185 panic("softdep_setup_allocdirect called"); 186 } 187 188 void 189 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 190 struct inode *ip; 191 ufs_lbn_t lbn; 192 ufs2_daddr_t newblkno; 193 ufs2_daddr_t oldblkno; 194 long newsize; 195 long oldsize; 196 struct buf *bp; 197 { 198 199 panic("softdep_setup_allocext called"); 200 } 201 202 void 203 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 204 struct inode *ip; 205 ufs_lbn_t lbn; 206 struct buf *bp; 207 int ptrno; 208 ufs2_daddr_t newblkno; 209 ufs2_daddr_t oldblkno; 210 struct buf *nbp; 211 { 212 213 panic("softdep_setup_allocindir_page called"); 214 } 215 216 void 217 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 218 struct buf *nbp; 219 struct inode *ip; 220 struct buf *bp; 221 int ptrno; 222 ufs2_daddr_t newblkno; 223 { 224 225 panic("softdep_setup_allocindir_meta called"); 226 } 227 228 void 229 softdep_journal_freeblocks(ip, cred, length, flags) 230 struct inode *ip; 231 struct ucred *cred; 232 off_t length; 233 int flags; 234 { 235 236 panic("softdep_journal_freeblocks called"); 237 } 238 239 void 240 softdep_journal_fsync(ip) 241 struct inode *ip; 242 { 243 244 panic("softdep_journal_fsync called"); 245 } 246 247 void 248 softdep_setup_freeblocks(ip, length, flags) 249 struct inode *ip; 250 off_t length; 251 int flags; 252 { 253 254 panic("softdep_setup_freeblocks called"); 255 } 256 257 void 258 softdep_freefile(pvp, ino, mode) 259 struct vnode *pvp; 260 ino_t ino; 261 int mode; 262 { 263 264 panic("softdep_freefile called"); 265 } 266 267 int 268 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 269 struct buf *bp; 270 struct inode *dp; 271 off_t diroffset; 272 ino_t newinum; 273 struct buf *newdirbp; 274 int isnewblk; 275 { 276 277 panic("softdep_setup_directory_add called"); 278 } 279 280 void 281 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 282 struct buf *bp; 283 struct inode *dp; 284 caddr_t base; 285 caddr_t oldloc; 286 caddr_t newloc; 287 int entrysize; 288 { 289 290 panic("softdep_change_directoryentry_offset called"); 291 } 292 293 void 294 softdep_setup_remove(bp, dp, ip, isrmdir) 295 struct buf *bp; 296 struct inode *dp; 297 struct inode *ip; 298 int isrmdir; 299 { 300 301 panic("softdep_setup_remove called"); 302 } 303 304 void 305 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 306 struct buf *bp; 307 struct inode *dp; 308 struct inode *ip; 309 ino_t newinum; 310 int isrmdir; 311 { 312 313 panic("softdep_setup_directory_change called"); 314 } 315 316 void 317 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 318 struct mount *mp; 319 struct buf *bp; 320 ufs2_daddr_t blkno; 321 int frags; 322 struct workhead *wkhd; 323 { 324 325 panic("%s called", __FUNCTION__); 326 } 327 328 void 329 softdep_setup_inofree(mp, bp, ino, wkhd) 330 struct mount *mp; 331 struct buf *bp; 332 ino_t ino; 333 struct workhead *wkhd; 334 { 335 336 panic("%s called", __FUNCTION__); 337 } 338 339 void 340 softdep_setup_unlink(dp, ip) 341 struct inode *dp; 342 struct inode *ip; 343 { 344 345 panic("%s called", __FUNCTION__); 346 } 347 348 void 349 softdep_setup_link(dp, ip) 350 struct inode *dp; 351 struct inode *ip; 352 { 353 354 panic("%s called", __FUNCTION__); 355 } 356 357 void 358 softdep_revert_link(dp, ip) 359 struct inode *dp; 360 struct inode *ip; 361 { 362 363 panic("%s called", __FUNCTION__); 364 } 365 366 void 367 softdep_setup_rmdir(dp, ip) 368 struct inode *dp; 369 struct inode *ip; 370 { 371 372 panic("%s called", __FUNCTION__); 373 } 374 375 void 376 softdep_revert_rmdir(dp, ip) 377 struct inode *dp; 378 struct inode *ip; 379 { 380 381 panic("%s called", __FUNCTION__); 382 } 383 384 void 385 softdep_setup_create(dp, ip) 386 struct inode *dp; 387 struct inode *ip; 388 { 389 390 panic("%s called", __FUNCTION__); 391 } 392 393 void 394 softdep_revert_create(dp, ip) 395 struct inode *dp; 396 struct inode *ip; 397 { 398 399 panic("%s called", __FUNCTION__); 400 } 401 402 void 403 softdep_setup_mkdir(dp, ip) 404 struct inode *dp; 405 struct inode *ip; 406 { 407 408 panic("%s called", __FUNCTION__); 409 } 410 411 void 412 softdep_revert_mkdir(dp, ip) 413 struct inode *dp; 414 struct inode *ip; 415 { 416 417 panic("%s called", __FUNCTION__); 418 } 419 420 void 421 softdep_setup_dotdot_link(dp, ip) 422 struct inode *dp; 423 struct inode *ip; 424 { 425 426 panic("%s called", __FUNCTION__); 427 } 428 429 int 430 softdep_prealloc(vp, waitok) 431 struct vnode *vp; 432 int waitok; 433 { 434 435 panic("%s called", __FUNCTION__); 436 437 return (0); 438 } 439 440 int 441 softdep_journal_lookup(mp, vpp) 442 struct mount *mp; 443 struct vnode **vpp; 444 { 445 446 return (ENOENT); 447 } 448 449 void 450 softdep_change_linkcnt(ip) 451 struct inode *ip; 452 { 453 454 panic("softdep_change_linkcnt called"); 455 } 456 457 void 458 softdep_load_inodeblock(ip) 459 struct inode *ip; 460 { 461 462 panic("softdep_load_inodeblock called"); 463 } 464 465 void 466 softdep_update_inodeblock(ip, bp, waitfor) 467 struct inode *ip; 468 struct buf *bp; 469 int waitfor; 470 { 471 472 panic("softdep_update_inodeblock called"); 473 } 474 475 int 476 softdep_fsync(vp) 477 struct vnode *vp; /* the "in_core" copy of the inode */ 478 { 479 480 return (0); 481 } 482 483 void 484 softdep_fsync_mountdev(vp) 485 struct vnode *vp; 486 { 487 488 return; 489 } 490 491 int 492 softdep_flushworklist(oldmnt, countp, td) 493 struct mount *oldmnt; 494 int *countp; 495 struct thread *td; 496 { 497 498 *countp = 0; 499 return (0); 500 } 501 502 int 503 softdep_sync_metadata(struct vnode *vp) 504 { 505 506 return (0); 507 } 508 509 int 510 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 511 { 512 513 return (0); 514 } 515 516 int 517 softdep_slowdown(vp) 518 struct vnode *vp; 519 { 520 521 panic("softdep_slowdown called"); 522 } 523 524 void 525 softdep_releasefile(ip) 526 struct inode *ip; /* inode with the zero effective link count */ 527 { 528 529 panic("softdep_releasefile called"); 530 } 531 532 int 533 softdep_request_cleanup(fs, vp, cred, resource) 534 struct fs *fs; 535 struct vnode *vp; 536 struct ucred *cred; 537 int resource; 538 { 539 540 return (0); 541 } 542 543 int 544 softdep_check_suspend(struct mount *mp, 545 struct vnode *devvp, 546 int softdep_deps, 547 int softdep_accdeps, 548 int secondary_writes, 549 int secondary_accwrites) 550 { 551 struct bufobj *bo; 552 int error; 553 554 (void) softdep_deps, 555 (void) softdep_accdeps; 556 557 bo = &devvp->v_bufobj; 558 ASSERT_BO_WLOCKED(bo); 559 560 MNT_ILOCK(mp); 561 while (mp->mnt_secondary_writes != 0) { 562 BO_UNLOCK(bo); 563 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 564 (PUSER - 1) | PDROP, "secwr", 0); 565 BO_LOCK(bo); 566 MNT_ILOCK(mp); 567 } 568 569 /* 570 * Reasons for needing more work before suspend: 571 * - Dirty buffers on devvp. 572 * - Secondary writes occurred after start of vnode sync loop 573 */ 574 error = 0; 575 if (bo->bo_numoutput > 0 || 576 bo->bo_dirty.bv_cnt > 0 || 577 secondary_writes != 0 || 578 mp->mnt_secondary_writes != 0 || 579 secondary_accwrites != mp->mnt_secondary_accwrites) 580 error = EAGAIN; 581 BO_UNLOCK(bo); 582 return (error); 583 } 584 585 void 586 softdep_get_depcounts(struct mount *mp, 587 int *softdepactivep, 588 int *softdepactiveaccp) 589 { 590 (void) mp; 591 *softdepactivep = 0; 592 *softdepactiveaccp = 0; 593 } 594 595 void 596 softdep_buf_append(bp, wkhd) 597 struct buf *bp; 598 struct workhead *wkhd; 599 { 600 601 panic("softdep_buf_appendwork called"); 602 } 603 604 void 605 softdep_inode_append(ip, cred, wkhd) 606 struct inode *ip; 607 struct ucred *cred; 608 struct workhead *wkhd; 609 { 610 611 panic("softdep_inode_appendwork called"); 612 } 613 614 void 615 softdep_freework(wkhd) 616 struct workhead *wkhd; 617 { 618 619 panic("softdep_freework called"); 620 } 621 622 #else 623 624 FEATURE(softupdates, "FFS soft-updates support"); 625 626 /* 627 * These definitions need to be adapted to the system to which 628 * this file is being ported. 629 */ 630 631 #define M_SOFTDEP_FLAGS (M_WAITOK) 632 633 #define D_PAGEDEP 0 634 #define D_INODEDEP 1 635 #define D_BMSAFEMAP 2 636 #define D_NEWBLK 3 637 #define D_ALLOCDIRECT 4 638 #define D_INDIRDEP 5 639 #define D_ALLOCINDIR 6 640 #define D_FREEFRAG 7 641 #define D_FREEBLKS 8 642 #define D_FREEFILE 9 643 #define D_DIRADD 10 644 #define D_MKDIR 11 645 #define D_DIRREM 12 646 #define D_NEWDIRBLK 13 647 #define D_FREEWORK 14 648 #define D_FREEDEP 15 649 #define D_JADDREF 16 650 #define D_JREMREF 17 651 #define D_JMVREF 18 652 #define D_JNEWBLK 19 653 #define D_JFREEBLK 20 654 #define D_JFREEFRAG 21 655 #define D_JSEG 22 656 #define D_JSEGDEP 23 657 #define D_SBDEP 24 658 #define D_JTRUNC 25 659 #define D_JFSYNC 26 660 #define D_SENTINEL 27 661 #define D_LAST D_SENTINEL 662 663 unsigned long dep_current[D_LAST + 1]; 664 unsigned long dep_total[D_LAST + 1]; 665 unsigned long dep_write[D_LAST + 1]; 666 667 668 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 669 "soft updates stats"); 670 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 671 "total dependencies allocated"); 672 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 673 "current dependencies allocated"); 674 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 675 "current dependencies written"); 676 677 #define SOFTDEP_TYPE(type, str, long) \ 678 static MALLOC_DEFINE(M_ ## type, #str, long); \ 679 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 680 &dep_total[D_ ## type], 0, ""); \ 681 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 682 &dep_current[D_ ## type], 0, ""); \ 683 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 684 &dep_write[D_ ## type], 0, ""); 685 686 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 687 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 688 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 689 "Block or frag allocated from cyl group map"); 690 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 691 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 692 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 693 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 694 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 695 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 696 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 697 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 698 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 699 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 700 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 701 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 702 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 703 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 704 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 705 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 706 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 707 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 708 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 709 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 710 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 711 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 712 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 713 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 714 715 static MALLOC_DEFINE(M_SENTINEL, "sentinel", "Worklist sentinel"); 716 717 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 718 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 719 720 /* 721 * translate from workitem type to memory type 722 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 723 */ 724 static struct malloc_type *memtype[] = { 725 M_PAGEDEP, 726 M_INODEDEP, 727 M_BMSAFEMAP, 728 M_NEWBLK, 729 M_ALLOCDIRECT, 730 M_INDIRDEP, 731 M_ALLOCINDIR, 732 M_FREEFRAG, 733 M_FREEBLKS, 734 M_FREEFILE, 735 M_DIRADD, 736 M_MKDIR, 737 M_DIRREM, 738 M_NEWDIRBLK, 739 M_FREEWORK, 740 M_FREEDEP, 741 M_JADDREF, 742 M_JREMREF, 743 M_JMVREF, 744 M_JNEWBLK, 745 M_JFREEBLK, 746 M_JFREEFRAG, 747 M_JSEG, 748 M_JSEGDEP, 749 M_SBDEP, 750 M_JTRUNC, 751 M_JFSYNC, 752 M_SENTINEL 753 }; 754 755 static LIST_HEAD(mkdirlist, mkdir) mkdirlisthd; 756 757 #define DtoM(type) (memtype[type]) 758 759 /* 760 * Names of malloc types. 761 */ 762 #define TYPENAME(type) \ 763 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 764 /* 765 * End system adaptation definitions. 766 */ 767 768 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 769 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 770 771 /* 772 * Forward declarations. 773 */ 774 struct inodedep_hashhead; 775 struct newblk_hashhead; 776 struct pagedep_hashhead; 777 struct bmsafemap_hashhead; 778 779 /* 780 * Private journaling structures. 781 */ 782 struct jblocks { 783 struct jseglst jb_segs; /* TAILQ of current segments. */ 784 struct jseg *jb_writeseg; /* Next write to complete. */ 785 struct jseg *jb_oldestseg; /* Oldest segment with valid entries. */ 786 struct jextent *jb_extent; /* Extent array. */ 787 uint64_t jb_nextseq; /* Next sequence number. */ 788 uint64_t jb_oldestwrseq; /* Oldest written sequence number. */ 789 uint8_t jb_needseg; /* Need a forced segment. */ 790 uint8_t jb_suspended; /* Did journal suspend writes? */ 791 int jb_avail; /* Available extents. */ 792 int jb_used; /* Last used extent. */ 793 int jb_head; /* Allocator head. */ 794 int jb_off; /* Allocator extent offset. */ 795 int jb_blocks; /* Total disk blocks covered. */ 796 int jb_free; /* Total disk blocks free. */ 797 int jb_min; /* Minimum free space. */ 798 int jb_low; /* Low on space. */ 799 int jb_age; /* Insertion time of oldest rec. */ 800 }; 801 802 struct jextent { 803 ufs2_daddr_t je_daddr; /* Disk block address. */ 804 int je_blocks; /* Disk block count. */ 805 }; 806 807 /* 808 * Internal function prototypes. 809 */ 810 static void softdep_error(char *, int); 811 static void drain_output(struct vnode *); 812 static struct buf *getdirtybuf(struct buf *, struct rwlock *, int); 813 static void clear_remove(void); 814 static void clear_inodedeps(void); 815 static void unlinked_inodedep(struct mount *, struct inodedep *); 816 static void clear_unlinked_inodedep(struct inodedep *); 817 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 818 static int flush_pagedep_deps(struct vnode *, struct mount *, 819 struct diraddhd *); 820 static int free_pagedep(struct pagedep *); 821 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 822 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 823 static int flush_deplist(struct allocdirectlst *, int, int *); 824 static int sync_cgs(struct mount *, int); 825 static int handle_written_filepage(struct pagedep *, struct buf *); 826 static int handle_written_sbdep(struct sbdep *, struct buf *); 827 static void initiate_write_sbdep(struct sbdep *); 828 static void diradd_inode_written(struct diradd *, struct inodedep *); 829 static int handle_written_indirdep(struct indirdep *, struct buf *, 830 struct buf**); 831 static int handle_written_inodeblock(struct inodedep *, struct buf *); 832 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 833 uint8_t *); 834 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *); 835 static void handle_written_jaddref(struct jaddref *); 836 static void handle_written_jremref(struct jremref *); 837 static void handle_written_jseg(struct jseg *, struct buf *); 838 static void handle_written_jnewblk(struct jnewblk *); 839 static void handle_written_jblkdep(struct jblkdep *); 840 static void handle_written_jfreefrag(struct jfreefrag *); 841 static void complete_jseg(struct jseg *); 842 static void complete_jsegs(struct jseg *); 843 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 844 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 845 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 846 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 847 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 848 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 849 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 850 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 851 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 852 static inline void inoref_write(struct inoref *, struct jseg *, 853 struct jrefrec *); 854 static void handle_allocdirect_partdone(struct allocdirect *, 855 struct workhead *); 856 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 857 struct workhead *); 858 static void indirdep_complete(struct indirdep *); 859 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 860 static void indirblk_insert(struct freework *); 861 static void indirblk_remove(struct freework *); 862 static void handle_allocindir_partdone(struct allocindir *); 863 static void initiate_write_filepage(struct pagedep *, struct buf *); 864 static void initiate_write_indirdep(struct indirdep*, struct buf *); 865 static void handle_written_mkdir(struct mkdir *, int); 866 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 867 uint8_t *); 868 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 869 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 870 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 871 static void handle_workitem_freefile(struct freefile *); 872 static int handle_workitem_remove(struct dirrem *, int); 873 static struct dirrem *newdirrem(struct buf *, struct inode *, 874 struct inode *, int, struct dirrem **); 875 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 876 struct buf *); 877 static void cancel_indirdep(struct indirdep *, struct buf *, 878 struct freeblks *); 879 static void free_indirdep(struct indirdep *); 880 static void free_diradd(struct diradd *, struct workhead *); 881 static void merge_diradd(struct inodedep *, struct diradd *); 882 static void complete_diradd(struct diradd *); 883 static struct diradd *diradd_lookup(struct pagedep *, int); 884 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 885 struct jremref *); 886 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 887 struct jremref *); 888 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 889 struct jremref *, struct jremref *); 890 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 891 struct jremref *); 892 static void cancel_allocindir(struct allocindir *, struct buf *bp, 893 struct freeblks *, int); 894 static int setup_trunc_indir(struct freeblks *, struct inode *, 895 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 896 static void complete_trunc_indir(struct freework *); 897 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 898 int); 899 static void complete_mkdir(struct mkdir *); 900 static void free_newdirblk(struct newdirblk *); 901 static void free_jremref(struct jremref *); 902 static void free_jaddref(struct jaddref *); 903 static void free_jsegdep(struct jsegdep *); 904 static void free_jsegs(struct jblocks *); 905 static void rele_jseg(struct jseg *); 906 static void free_jseg(struct jseg *, struct jblocks *); 907 static void free_jnewblk(struct jnewblk *); 908 static void free_jblkdep(struct jblkdep *); 909 static void free_jfreefrag(struct jfreefrag *); 910 static void free_freedep(struct freedep *); 911 static void journal_jremref(struct dirrem *, struct jremref *, 912 struct inodedep *); 913 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 914 static int cancel_jaddref(struct jaddref *, struct inodedep *, 915 struct workhead *); 916 static void cancel_jfreefrag(struct jfreefrag *); 917 static inline void setup_freedirect(struct freeblks *, struct inode *, 918 int, int); 919 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 920 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 921 ufs_lbn_t, int); 922 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 923 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 924 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 925 ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 926 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 927 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 928 int, int); 929 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 930 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 931 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 932 static void newblk_freefrag(struct newblk*); 933 static void free_newblk(struct newblk *); 934 static void cancel_allocdirect(struct allocdirectlst *, 935 struct allocdirect *, struct freeblks *); 936 static int check_inode_unwritten(struct inodedep *); 937 static int free_inodedep(struct inodedep *); 938 static void freework_freeblock(struct freework *); 939 static void freework_enqueue(struct freework *); 940 static int handle_workitem_freeblocks(struct freeblks *, int); 941 static int handle_complete_freeblocks(struct freeblks *, int); 942 static void handle_workitem_indirblk(struct freework *); 943 static void handle_written_freework(struct freework *); 944 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 945 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 946 struct workhead *); 947 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 948 struct inodedep *, struct allocindir *, ufs_lbn_t); 949 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 950 ufs2_daddr_t, ufs_lbn_t); 951 static void handle_workitem_freefrag(struct freefrag *); 952 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 953 ufs_lbn_t); 954 static void allocdirect_merge(struct allocdirectlst *, 955 struct allocdirect *, struct allocdirect *); 956 static struct freefrag *allocindir_merge(struct allocindir *, 957 struct allocindir *); 958 static int bmsafemap_find(struct bmsafemap_hashhead *, struct mount *, int, 959 struct bmsafemap **); 960 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 961 int cg, struct bmsafemap *); 962 static int newblk_find(struct newblk_hashhead *, struct mount *, ufs2_daddr_t, 963 int, struct newblk **); 964 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 965 static int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t, 966 struct inodedep **); 967 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 968 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 969 int, struct pagedep **); 970 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 971 struct mount *mp, int, struct pagedep **); 972 static void pause_timer(void *); 973 static int request_cleanup(struct mount *, int); 974 static int process_worklist_item(struct mount *, int, int); 975 static void process_removes(struct vnode *); 976 static void process_truncates(struct vnode *); 977 static void jwork_move(struct workhead *, struct workhead *); 978 static void jwork_insert(struct workhead *, struct jsegdep *); 979 static void add_to_worklist(struct worklist *, int); 980 static void wake_worklist(struct worklist *); 981 static void wait_worklist(struct worklist *, char *); 982 static void remove_from_worklist(struct worklist *); 983 static void softdep_flush(void); 984 static void softdep_flushjournal(struct mount *); 985 static int softdep_speedup(void); 986 static void worklist_speedup(void); 987 static int journal_mount(struct mount *, struct fs *, struct ucred *); 988 static void journal_unmount(struct mount *); 989 static int journal_space(struct ufsmount *, int); 990 static void journal_suspend(struct ufsmount *); 991 static int journal_unsuspend(struct ufsmount *ump); 992 static void softdep_prelink(struct vnode *, struct vnode *); 993 static void add_to_journal(struct worklist *); 994 static void remove_from_journal(struct worklist *); 995 static void softdep_process_journal(struct mount *, struct worklist *, int); 996 static struct jremref *newjremref(struct dirrem *, struct inode *, 997 struct inode *ip, off_t, nlink_t); 998 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 999 uint16_t); 1000 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 1001 uint16_t); 1002 static inline struct jsegdep *inoref_jseg(struct inoref *); 1003 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 1004 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 1005 ufs2_daddr_t, int); 1006 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 1007 static void move_newblock_dep(struct jaddref *, struct inodedep *); 1008 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 1009 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 1010 ufs2_daddr_t, long, ufs_lbn_t); 1011 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 1012 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 1013 static int jwait(struct worklist *, int); 1014 static struct inodedep *inodedep_lookup_ip(struct inode *); 1015 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 1016 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 1017 static void handle_jwork(struct workhead *); 1018 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 1019 struct mkdir **); 1020 static struct jblocks *jblocks_create(void); 1021 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 1022 static void jblocks_free(struct jblocks *, struct mount *, int); 1023 static void jblocks_destroy(struct jblocks *); 1024 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 1025 1026 /* 1027 * Exported softdep operations. 1028 */ 1029 static void softdep_disk_io_initiation(struct buf *); 1030 static void softdep_disk_write_complete(struct buf *); 1031 static void softdep_deallocate_dependencies(struct buf *); 1032 static int softdep_count_dependencies(struct buf *bp, int); 1033 1034 static struct rwlock lk; 1035 RW_SYSINIT(softdep_lock, &lk, "Softdep Lock"); 1036 1037 #define TRY_ACQUIRE_LOCK(lk) rw_try_wlock(lk) 1038 #define ACQUIRE_LOCK(lk) rw_wlock(lk) 1039 #define FREE_LOCK(lk) rw_wunlock(lk) 1040 1041 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 1042 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 1043 1044 /* 1045 * Worklist queue management. 1046 * These routines require that the lock be held. 1047 */ 1048 #ifndef /* NOT */ DEBUG 1049 #define WORKLIST_INSERT(head, item) do { \ 1050 (item)->wk_state |= ONWORKLIST; \ 1051 LIST_INSERT_HEAD(head, item, wk_list); \ 1052 } while (0) 1053 #define WORKLIST_REMOVE(item) do { \ 1054 (item)->wk_state &= ~ONWORKLIST; \ 1055 LIST_REMOVE(item, wk_list); \ 1056 } while (0) 1057 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1058 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1059 1060 #else /* DEBUG */ 1061 static void worklist_insert(struct workhead *, struct worklist *, int); 1062 static void worklist_remove(struct worklist *, int); 1063 1064 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1065 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1066 #define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1067 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1068 1069 static void 1070 worklist_insert(head, item, locked) 1071 struct workhead *head; 1072 struct worklist *item; 1073 int locked; 1074 { 1075 1076 if (locked) 1077 rw_assert(&lk, RA_WLOCKED); 1078 if (item->wk_state & ONWORKLIST) 1079 panic("worklist_insert: %p %s(0x%X) already on list", 1080 item, TYPENAME(item->wk_type), item->wk_state); 1081 item->wk_state |= ONWORKLIST; 1082 LIST_INSERT_HEAD(head, item, wk_list); 1083 } 1084 1085 static void 1086 worklist_remove(item, locked) 1087 struct worklist *item; 1088 int locked; 1089 { 1090 1091 if (locked) 1092 rw_assert(&lk, RA_WLOCKED); 1093 if ((item->wk_state & ONWORKLIST) == 0) 1094 panic("worklist_remove: %p %s(0x%X) not on list", 1095 item, TYPENAME(item->wk_type), item->wk_state); 1096 item->wk_state &= ~ONWORKLIST; 1097 LIST_REMOVE(item, wk_list); 1098 } 1099 #endif /* DEBUG */ 1100 1101 /* 1102 * Merge two jsegdeps keeping only the oldest one as newer references 1103 * can't be discarded until after older references. 1104 */ 1105 static inline struct jsegdep * 1106 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1107 { 1108 struct jsegdep *swp; 1109 1110 if (two == NULL) 1111 return (one); 1112 1113 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1114 swp = one; 1115 one = two; 1116 two = swp; 1117 } 1118 WORKLIST_REMOVE(&two->jd_list); 1119 free_jsegdep(two); 1120 1121 return (one); 1122 } 1123 1124 /* 1125 * If two freedeps are compatible free one to reduce list size. 1126 */ 1127 static inline struct freedep * 1128 freedep_merge(struct freedep *one, struct freedep *two) 1129 { 1130 if (two == NULL) 1131 return (one); 1132 1133 if (one->fd_freework == two->fd_freework) { 1134 WORKLIST_REMOVE(&two->fd_list); 1135 free_freedep(two); 1136 } 1137 return (one); 1138 } 1139 1140 /* 1141 * Move journal work from one list to another. Duplicate freedeps and 1142 * jsegdeps are coalesced to keep the lists as small as possible. 1143 */ 1144 static void 1145 jwork_move(dst, src) 1146 struct workhead *dst; 1147 struct workhead *src; 1148 { 1149 struct freedep *freedep; 1150 struct jsegdep *jsegdep; 1151 struct worklist *wkn; 1152 struct worklist *wk; 1153 1154 KASSERT(dst != src, 1155 ("jwork_move: dst == src")); 1156 freedep = NULL; 1157 jsegdep = NULL; 1158 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1159 if (wk->wk_type == D_JSEGDEP) 1160 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1161 if (wk->wk_type == D_FREEDEP) 1162 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1163 } 1164 1165 rw_assert(&lk, RA_WLOCKED); 1166 while ((wk = LIST_FIRST(src)) != NULL) { 1167 WORKLIST_REMOVE(wk); 1168 WORKLIST_INSERT(dst, wk); 1169 if (wk->wk_type == D_JSEGDEP) { 1170 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1171 continue; 1172 } 1173 if (wk->wk_type == D_FREEDEP) 1174 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1175 } 1176 } 1177 1178 static void 1179 jwork_insert(dst, jsegdep) 1180 struct workhead *dst; 1181 struct jsegdep *jsegdep; 1182 { 1183 struct jsegdep *jsegdepn; 1184 struct worklist *wk; 1185 1186 LIST_FOREACH(wk, dst, wk_list) 1187 if (wk->wk_type == D_JSEGDEP) 1188 break; 1189 if (wk == NULL) { 1190 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1191 return; 1192 } 1193 jsegdepn = WK_JSEGDEP(wk); 1194 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1195 WORKLIST_REMOVE(wk); 1196 free_jsegdep(jsegdepn); 1197 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1198 } else 1199 free_jsegdep(jsegdep); 1200 } 1201 1202 /* 1203 * Routines for tracking and managing workitems. 1204 */ 1205 static void workitem_free(struct worklist *, int); 1206 static void workitem_alloc(struct worklist *, int, struct mount *); 1207 1208 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type)) 1209 1210 static void 1211 workitem_free(item, type) 1212 struct worklist *item; 1213 int type; 1214 { 1215 struct ufsmount *ump; 1216 rw_assert(&lk, RA_WLOCKED); 1217 1218 #ifdef DEBUG 1219 if (item->wk_state & ONWORKLIST) 1220 panic("workitem_free: %s(0x%X) still on list", 1221 TYPENAME(item->wk_type), item->wk_state); 1222 if (item->wk_type != type) 1223 panic("workitem_free: type mismatch %s != %s", 1224 TYPENAME(item->wk_type), TYPENAME(type)); 1225 #endif 1226 if (item->wk_state & IOWAITING) 1227 wakeup(item); 1228 ump = VFSTOUFS(item->wk_mp); 1229 if (--ump->softdep_deps == 0 && ump->softdep_req) 1230 wakeup(&ump->softdep_deps); 1231 dep_current[type]--; 1232 free(item, DtoM(type)); 1233 } 1234 1235 static void 1236 workitem_alloc(item, type, mp) 1237 struct worklist *item; 1238 int type; 1239 struct mount *mp; 1240 { 1241 struct ufsmount *ump; 1242 1243 item->wk_type = type; 1244 item->wk_mp = mp; 1245 item->wk_state = 0; 1246 1247 ump = VFSTOUFS(mp); 1248 ACQUIRE_LOCK(&lk); 1249 dep_current[type]++; 1250 dep_total[type]++; 1251 ump->softdep_deps++; 1252 ump->softdep_accdeps++; 1253 FREE_LOCK(&lk); 1254 } 1255 1256 /* 1257 * Workitem queue management 1258 */ 1259 static int max_softdeps; /* maximum number of structs before slowdown */ 1260 static int maxindirdeps = 50; /* max number of indirdeps before slowdown */ 1261 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1262 static int proc_waiting; /* tracks whether we have a timeout posted */ 1263 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1264 static struct callout softdep_callout; 1265 static int req_pending; 1266 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1267 static int req_clear_remove; /* syncer process flush some freeblks */ 1268 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1269 1270 /* 1271 * runtime statistics 1272 */ 1273 static int stat_worklist_push; /* number of worklist cleanups */ 1274 static int stat_blk_limit_push; /* number of times block limit neared */ 1275 static int stat_ino_limit_push; /* number of times inode limit neared */ 1276 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1277 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1278 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1279 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1280 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1281 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1282 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1283 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1284 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1285 static int stat_journal_min; /* Times hit journal min threshold */ 1286 static int stat_journal_low; /* Times hit journal low threshold */ 1287 static int stat_journal_wait; /* Times blocked in jwait(). */ 1288 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1289 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1290 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1291 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1292 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1293 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1294 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1295 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1296 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1297 1298 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1299 &max_softdeps, 0, ""); 1300 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1301 &tickdelay, 0, ""); 1302 SYSCTL_INT(_debug_softdep, OID_AUTO, maxindirdeps, CTLFLAG_RW, 1303 &maxindirdeps, 0, ""); 1304 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1305 &stat_worklist_push, 0,""); 1306 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1307 &stat_blk_limit_push, 0,""); 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1309 &stat_ino_limit_push, 0,""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1311 &stat_blk_limit_hit, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1313 &stat_ino_limit_hit, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1315 &stat_sync_limit_hit, 0, ""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1317 &stat_indir_blk_ptrs, 0, ""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1319 &stat_inode_bitmap, 0, ""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1321 &stat_direct_blk_ptrs, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1323 &stat_dir_entry, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1325 &stat_jaddref, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1327 &stat_jnewblk, 0, ""); 1328 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1329 &stat_journal_low, 0, ""); 1330 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1331 &stat_journal_min, 0, ""); 1332 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1333 &stat_journal_wait, 0, ""); 1334 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1335 &stat_jwait_filepage, 0, ""); 1336 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1337 &stat_jwait_freeblks, 0, ""); 1338 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1339 &stat_jwait_inode, 0, ""); 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1341 &stat_jwait_newblk, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1343 &stat_cleanup_blkrequests, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1345 &stat_cleanup_inorequests, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1347 &stat_cleanup_high_delay, 0, ""); 1348 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1349 &stat_cleanup_retries, 0, ""); 1350 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1351 &stat_cleanup_failures, 0, ""); 1352 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1353 &softdep_flushcache, 0, ""); 1354 1355 SYSCTL_DECL(_vfs_ffs); 1356 1357 LIST_HEAD(bmsafemap_hashhead, bmsafemap) *bmsafemap_hashtbl; 1358 static u_long bmsafemap_hash; /* size of hash table - 1 */ 1359 1360 static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */ 1361 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1362 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1363 1364 static struct proc *softdepproc; 1365 static struct kproc_desc softdep_kp = { 1366 "softdepflush", 1367 softdep_flush, 1368 &softdepproc 1369 }; 1370 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 1371 &softdep_kp); 1372 1373 static void 1374 softdep_flush(void) 1375 { 1376 struct mount *nmp; 1377 struct mount *mp; 1378 struct ufsmount *ump; 1379 struct thread *td; 1380 int remaining; 1381 int progress; 1382 1383 td = curthread; 1384 td->td_pflags |= TDP_NORUNNINGBUF; 1385 1386 for (;;) { 1387 kproc_suspend_check(softdepproc); 1388 ACQUIRE_LOCK(&lk); 1389 /* 1390 * If requested, try removing inode or removal dependencies. 1391 */ 1392 if (req_clear_inodedeps) { 1393 clear_inodedeps(); 1394 req_clear_inodedeps -= 1; 1395 wakeup_one(&proc_waiting); 1396 } 1397 if (req_clear_remove) { 1398 clear_remove(); 1399 req_clear_remove -= 1; 1400 wakeup_one(&proc_waiting); 1401 } 1402 FREE_LOCK(&lk); 1403 remaining = progress = 0; 1404 mtx_lock(&mountlist_mtx); 1405 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1406 nmp = TAILQ_NEXT(mp, mnt_list); 1407 if (MOUNTEDSOFTDEP(mp) == 0) 1408 continue; 1409 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 1410 continue; 1411 progress += softdep_process_worklist(mp, 0); 1412 ump = VFSTOUFS(mp); 1413 remaining += ump->softdep_on_worklist; 1414 mtx_lock(&mountlist_mtx); 1415 nmp = TAILQ_NEXT(mp, mnt_list); 1416 vfs_unbusy(mp); 1417 } 1418 mtx_unlock(&mountlist_mtx); 1419 if (remaining && progress) 1420 continue; 1421 ACQUIRE_LOCK(&lk); 1422 if (!req_pending) 1423 msleep(&req_pending, &lk, PVM, "sdflush", hz); 1424 req_pending = 0; 1425 FREE_LOCK(&lk); 1426 } 1427 } 1428 1429 static void 1430 worklist_speedup(void) 1431 { 1432 rw_assert(&lk, RA_WLOCKED); 1433 if (req_pending == 0) { 1434 req_pending = 1; 1435 wakeup(&req_pending); 1436 } 1437 } 1438 1439 static int 1440 softdep_speedup(void) 1441 { 1442 1443 worklist_speedup(); 1444 bd_speedup(); 1445 return speedup_syncer(); 1446 } 1447 1448 /* 1449 * Add an item to the end of the work queue. 1450 * This routine requires that the lock be held. 1451 * This is the only routine that adds items to the list. 1452 * The following routine is the only one that removes items 1453 * and does so in order from first to last. 1454 */ 1455 1456 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1457 #define WK_NODELAY 0x0002 /* Process immediately. */ 1458 1459 static void 1460 add_to_worklist(wk, flags) 1461 struct worklist *wk; 1462 int flags; 1463 { 1464 struct ufsmount *ump; 1465 1466 rw_assert(&lk, RA_WLOCKED); 1467 ump = VFSTOUFS(wk->wk_mp); 1468 if (wk->wk_state & ONWORKLIST) 1469 panic("add_to_worklist: %s(0x%X) already on list", 1470 TYPENAME(wk->wk_type), wk->wk_state); 1471 wk->wk_state |= ONWORKLIST; 1472 if (ump->softdep_on_worklist == 0) { 1473 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1474 ump->softdep_worklist_tail = wk; 1475 } else if (flags & WK_HEAD) { 1476 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1477 } else { 1478 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1479 ump->softdep_worklist_tail = wk; 1480 } 1481 ump->softdep_on_worklist += 1; 1482 if (flags & WK_NODELAY) 1483 worklist_speedup(); 1484 } 1485 1486 /* 1487 * Remove the item to be processed. If we are removing the last 1488 * item on the list, we need to recalculate the tail pointer. 1489 */ 1490 static void 1491 remove_from_worklist(wk) 1492 struct worklist *wk; 1493 { 1494 struct ufsmount *ump; 1495 1496 ump = VFSTOUFS(wk->wk_mp); 1497 WORKLIST_REMOVE(wk); 1498 if (ump->softdep_worklist_tail == wk) 1499 ump->softdep_worklist_tail = 1500 (struct worklist *)wk->wk_list.le_prev; 1501 ump->softdep_on_worklist -= 1; 1502 } 1503 1504 static void 1505 wake_worklist(wk) 1506 struct worklist *wk; 1507 { 1508 if (wk->wk_state & IOWAITING) { 1509 wk->wk_state &= ~IOWAITING; 1510 wakeup(wk); 1511 } 1512 } 1513 1514 static void 1515 wait_worklist(wk, wmesg) 1516 struct worklist *wk; 1517 char *wmesg; 1518 { 1519 1520 wk->wk_state |= IOWAITING; 1521 msleep(wk, &lk, PVM, wmesg, 0); 1522 } 1523 1524 /* 1525 * Process that runs once per second to handle items in the background queue. 1526 * 1527 * Note that we ensure that everything is done in the order in which they 1528 * appear in the queue. The code below depends on this property to ensure 1529 * that blocks of a file are freed before the inode itself is freed. This 1530 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1531 * until all the old ones have been purged from the dependency lists. 1532 */ 1533 int 1534 softdep_process_worklist(mp, full) 1535 struct mount *mp; 1536 int full; 1537 { 1538 int cnt, matchcnt; 1539 struct ufsmount *ump; 1540 long starttime; 1541 1542 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1543 /* 1544 * Record the process identifier of our caller so that we can give 1545 * this process preferential treatment in request_cleanup below. 1546 */ 1547 matchcnt = 0; 1548 ump = VFSTOUFS(mp); 1549 ACQUIRE_LOCK(&lk); 1550 starttime = time_second; 1551 softdep_process_journal(mp, NULL, full?MNT_WAIT:0); 1552 while (ump->softdep_on_worklist > 0) { 1553 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1554 break; 1555 else 1556 matchcnt += cnt; 1557 /* 1558 * If requested, try removing inode or removal dependencies. 1559 */ 1560 if (req_clear_inodedeps) { 1561 clear_inodedeps(); 1562 req_clear_inodedeps -= 1; 1563 wakeup_one(&proc_waiting); 1564 } 1565 if (req_clear_remove) { 1566 clear_remove(); 1567 req_clear_remove -= 1; 1568 wakeup_one(&proc_waiting); 1569 } 1570 /* 1571 * We do not generally want to stop for buffer space, but if 1572 * we are really being a buffer hog, we will stop and wait. 1573 */ 1574 if (should_yield()) { 1575 FREE_LOCK(&lk); 1576 kern_yield(PRI_USER); 1577 bwillwrite(); 1578 ACQUIRE_LOCK(&lk); 1579 } 1580 /* 1581 * Never allow processing to run for more than one 1582 * second. Otherwise the other mountpoints may get 1583 * excessively backlogged. 1584 */ 1585 if (!full && starttime != time_second) 1586 break; 1587 } 1588 if (full == 0) 1589 journal_unsuspend(ump); 1590 FREE_LOCK(&lk); 1591 return (matchcnt); 1592 } 1593 1594 /* 1595 * Process all removes associated with a vnode if we are running out of 1596 * journal space. Any other process which attempts to flush these will 1597 * be unable as we have the vnodes locked. 1598 */ 1599 static void 1600 process_removes(vp) 1601 struct vnode *vp; 1602 { 1603 struct inodedep *inodedep; 1604 struct dirrem *dirrem; 1605 struct mount *mp; 1606 ino_t inum; 1607 1608 rw_assert(&lk, RA_WLOCKED); 1609 1610 mp = vp->v_mount; 1611 inum = VTOI(vp)->i_number; 1612 for (;;) { 1613 top: 1614 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1615 return; 1616 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1617 /* 1618 * If another thread is trying to lock this vnode 1619 * it will fail but we must wait for it to do so 1620 * before we can proceed. 1621 */ 1622 if (dirrem->dm_state & INPROGRESS) { 1623 wait_worklist(&dirrem->dm_list, "pwrwait"); 1624 goto top; 1625 } 1626 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1627 (COMPLETE | ONWORKLIST)) 1628 break; 1629 } 1630 if (dirrem == NULL) 1631 return; 1632 remove_from_worklist(&dirrem->dm_list); 1633 FREE_LOCK(&lk); 1634 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1635 panic("process_removes: suspended filesystem"); 1636 handle_workitem_remove(dirrem, 0); 1637 vn_finished_secondary_write(mp); 1638 ACQUIRE_LOCK(&lk); 1639 } 1640 } 1641 1642 /* 1643 * Process all truncations associated with a vnode if we are running out 1644 * of journal space. This is called when the vnode lock is already held 1645 * and no other process can clear the truncation. This function returns 1646 * a value greater than zero if it did any work. 1647 */ 1648 static void 1649 process_truncates(vp) 1650 struct vnode *vp; 1651 { 1652 struct inodedep *inodedep; 1653 struct freeblks *freeblks; 1654 struct mount *mp; 1655 ino_t inum; 1656 int cgwait; 1657 1658 rw_assert(&lk, RA_WLOCKED); 1659 1660 mp = vp->v_mount; 1661 inum = VTOI(vp)->i_number; 1662 for (;;) { 1663 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1664 return; 1665 cgwait = 0; 1666 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1667 /* Journal entries not yet written. */ 1668 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1669 jwait(&LIST_FIRST( 1670 &freeblks->fb_jblkdephd)->jb_list, 1671 MNT_WAIT); 1672 break; 1673 } 1674 /* Another thread is executing this item. */ 1675 if (freeblks->fb_state & INPROGRESS) { 1676 wait_worklist(&freeblks->fb_list, "ptrwait"); 1677 break; 1678 } 1679 /* Freeblks is waiting on a inode write. */ 1680 if ((freeblks->fb_state & COMPLETE) == 0) { 1681 FREE_LOCK(&lk); 1682 ffs_update(vp, 1); 1683 ACQUIRE_LOCK(&lk); 1684 break; 1685 } 1686 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1687 (ALLCOMPLETE | ONWORKLIST)) { 1688 remove_from_worklist(&freeblks->fb_list); 1689 freeblks->fb_state |= INPROGRESS; 1690 FREE_LOCK(&lk); 1691 if (vn_start_secondary_write(NULL, &mp, 1692 V_NOWAIT)) 1693 panic("process_truncates: " 1694 "suspended filesystem"); 1695 handle_workitem_freeblocks(freeblks, 0); 1696 vn_finished_secondary_write(mp); 1697 ACQUIRE_LOCK(&lk); 1698 break; 1699 } 1700 if (freeblks->fb_cgwait) 1701 cgwait++; 1702 } 1703 if (cgwait) { 1704 FREE_LOCK(&lk); 1705 sync_cgs(mp, MNT_WAIT); 1706 ffs_sync_snap(mp, MNT_WAIT); 1707 ACQUIRE_LOCK(&lk); 1708 continue; 1709 } 1710 if (freeblks == NULL) 1711 break; 1712 } 1713 return; 1714 } 1715 1716 /* 1717 * Process one item on the worklist. 1718 */ 1719 static int 1720 process_worklist_item(mp, target, flags) 1721 struct mount *mp; 1722 int target; 1723 int flags; 1724 { 1725 struct worklist sentinel; 1726 struct worklist *wk; 1727 struct ufsmount *ump; 1728 int matchcnt; 1729 int error; 1730 1731 rw_assert(&lk, RA_WLOCKED); 1732 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1733 /* 1734 * If we are being called because of a process doing a 1735 * copy-on-write, then it is not safe to write as we may 1736 * recurse into the copy-on-write routine. 1737 */ 1738 if (curthread->td_pflags & TDP_COWINPROGRESS) 1739 return (-1); 1740 PHOLD(curproc); /* Don't let the stack go away. */ 1741 ump = VFSTOUFS(mp); 1742 matchcnt = 0; 1743 sentinel.wk_mp = NULL; 1744 sentinel.wk_type = D_SENTINEL; 1745 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sentinel, wk_list); 1746 for (wk = LIST_NEXT(&sentinel, wk_list); wk != NULL; 1747 wk = LIST_NEXT(&sentinel, wk_list)) { 1748 if (wk->wk_type == D_SENTINEL) { 1749 LIST_REMOVE(&sentinel, wk_list); 1750 LIST_INSERT_AFTER(wk, &sentinel, wk_list); 1751 continue; 1752 } 1753 if (wk->wk_state & INPROGRESS) 1754 panic("process_worklist_item: %p already in progress.", 1755 wk); 1756 wk->wk_state |= INPROGRESS; 1757 remove_from_worklist(wk); 1758 FREE_LOCK(&lk); 1759 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1760 panic("process_worklist_item: suspended filesystem"); 1761 switch (wk->wk_type) { 1762 case D_DIRREM: 1763 /* removal of a directory entry */ 1764 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1765 break; 1766 1767 case D_FREEBLKS: 1768 /* releasing blocks and/or fragments from a file */ 1769 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1770 flags); 1771 break; 1772 1773 case D_FREEFRAG: 1774 /* releasing a fragment when replaced as a file grows */ 1775 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1776 error = 0; 1777 break; 1778 1779 case D_FREEFILE: 1780 /* releasing an inode when its link count drops to 0 */ 1781 handle_workitem_freefile(WK_FREEFILE(wk)); 1782 error = 0; 1783 break; 1784 1785 default: 1786 panic("%s_process_worklist: Unknown type %s", 1787 "softdep", TYPENAME(wk->wk_type)); 1788 /* NOTREACHED */ 1789 } 1790 vn_finished_secondary_write(mp); 1791 ACQUIRE_LOCK(&lk); 1792 if (error == 0) { 1793 if (++matchcnt == target) 1794 break; 1795 continue; 1796 } 1797 /* 1798 * We have to retry the worklist item later. Wake up any 1799 * waiters who may be able to complete it immediately and 1800 * add the item back to the head so we don't try to execute 1801 * it again. 1802 */ 1803 wk->wk_state &= ~INPROGRESS; 1804 wake_worklist(wk); 1805 add_to_worklist(wk, WK_HEAD); 1806 } 1807 LIST_REMOVE(&sentinel, wk_list); 1808 /* Sentinal could've become the tail from remove_from_worklist. */ 1809 if (ump->softdep_worklist_tail == &sentinel) 1810 ump->softdep_worklist_tail = 1811 (struct worklist *)sentinel.wk_list.le_prev; 1812 PRELE(curproc); 1813 return (matchcnt); 1814 } 1815 1816 /* 1817 * Move dependencies from one buffer to another. 1818 */ 1819 int 1820 softdep_move_dependencies(oldbp, newbp) 1821 struct buf *oldbp; 1822 struct buf *newbp; 1823 { 1824 struct worklist *wk, *wktail; 1825 int dirty; 1826 1827 dirty = 0; 1828 wktail = NULL; 1829 ACQUIRE_LOCK(&lk); 1830 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1831 LIST_REMOVE(wk, wk_list); 1832 if (wk->wk_type == D_BMSAFEMAP && 1833 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1834 dirty = 1; 1835 if (wktail == 0) 1836 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1837 else 1838 LIST_INSERT_AFTER(wktail, wk, wk_list); 1839 wktail = wk; 1840 } 1841 FREE_LOCK(&lk); 1842 1843 return (dirty); 1844 } 1845 1846 /* 1847 * Purge the work list of all items associated with a particular mount point. 1848 */ 1849 int 1850 softdep_flushworklist(oldmnt, countp, td) 1851 struct mount *oldmnt; 1852 int *countp; 1853 struct thread *td; 1854 { 1855 struct vnode *devvp; 1856 int count, error = 0; 1857 struct ufsmount *ump; 1858 1859 /* 1860 * Alternately flush the block device associated with the mount 1861 * point and process any dependencies that the flushing 1862 * creates. We continue until no more worklist dependencies 1863 * are found. 1864 */ 1865 *countp = 0; 1866 ump = VFSTOUFS(oldmnt); 1867 devvp = ump->um_devvp; 1868 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1869 *countp += count; 1870 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1871 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1872 VOP_UNLOCK(devvp, 0); 1873 if (error) 1874 break; 1875 } 1876 return (error); 1877 } 1878 1879 int 1880 softdep_waitidle(struct mount *mp) 1881 { 1882 struct ufsmount *ump; 1883 int error; 1884 int i; 1885 1886 ump = VFSTOUFS(mp); 1887 ACQUIRE_LOCK(&lk); 1888 for (i = 0; i < 10 && ump->softdep_deps; i++) { 1889 ump->softdep_req = 1; 1890 if (ump->softdep_on_worklist) 1891 panic("softdep_waitidle: work added after flush."); 1892 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1); 1893 } 1894 ump->softdep_req = 0; 1895 FREE_LOCK(&lk); 1896 error = 0; 1897 if (i == 10) { 1898 error = EBUSY; 1899 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1900 mp); 1901 } 1902 1903 return (error); 1904 } 1905 1906 /* 1907 * Flush all vnodes and worklist items associated with a specified mount point. 1908 */ 1909 int 1910 softdep_flushfiles(oldmnt, flags, td) 1911 struct mount *oldmnt; 1912 int flags; 1913 struct thread *td; 1914 { 1915 #ifdef QUOTA 1916 struct ufsmount *ump; 1917 int i; 1918 #endif 1919 int error, early, depcount, loopcnt, retry_flush_count, retry; 1920 int morework; 1921 1922 loopcnt = 10; 1923 retry_flush_count = 3; 1924 retry_flush: 1925 error = 0; 1926 1927 /* 1928 * Alternately flush the vnodes associated with the mount 1929 * point and process any dependencies that the flushing 1930 * creates. In theory, this loop can happen at most twice, 1931 * but we give it a few extra just to be sure. 1932 */ 1933 for (; loopcnt > 0; loopcnt--) { 1934 /* 1935 * Do another flush in case any vnodes were brought in 1936 * as part of the cleanup operations. 1937 */ 1938 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1939 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1940 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1941 break; 1942 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1943 depcount == 0) 1944 break; 1945 } 1946 /* 1947 * If we are unmounting then it is an error to fail. If we 1948 * are simply trying to downgrade to read-only, then filesystem 1949 * activity can keep us busy forever, so we just fail with EBUSY. 1950 */ 1951 if (loopcnt == 0) { 1952 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 1953 panic("softdep_flushfiles: looping"); 1954 error = EBUSY; 1955 } 1956 if (!error) 1957 error = softdep_waitidle(oldmnt); 1958 if (!error) { 1959 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 1960 retry = 0; 1961 MNT_ILOCK(oldmnt); 1962 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 1963 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 1964 morework = oldmnt->mnt_nvnodelistsize > 0; 1965 #ifdef QUOTA 1966 ump = VFSTOUFS(oldmnt); 1967 UFS_LOCK(ump); 1968 for (i = 0; i < MAXQUOTAS; i++) { 1969 if (ump->um_quotas[i] != NULLVP) 1970 morework = 1; 1971 } 1972 UFS_UNLOCK(ump); 1973 #endif 1974 if (morework) { 1975 if (--retry_flush_count > 0) { 1976 retry = 1; 1977 loopcnt = 3; 1978 } else 1979 error = EBUSY; 1980 } 1981 MNT_IUNLOCK(oldmnt); 1982 if (retry) 1983 goto retry_flush; 1984 } 1985 } 1986 return (error); 1987 } 1988 1989 /* 1990 * Structure hashing. 1991 * 1992 * There are three types of structures that can be looked up: 1993 * 1) pagedep structures identified by mount point, inode number, 1994 * and logical block. 1995 * 2) inodedep structures identified by mount point and inode number. 1996 * 3) newblk structures identified by mount point and 1997 * physical block number. 1998 * 1999 * The "pagedep" and "inodedep" dependency structures are hashed 2000 * separately from the file blocks and inodes to which they correspond. 2001 * This separation helps when the in-memory copy of an inode or 2002 * file block must be replaced. It also obviates the need to access 2003 * an inode or file page when simply updating (or de-allocating) 2004 * dependency structures. Lookup of newblk structures is needed to 2005 * find newly allocated blocks when trying to associate them with 2006 * their allocdirect or allocindir structure. 2007 * 2008 * The lookup routines optionally create and hash a new instance when 2009 * an existing entry is not found. 2010 */ 2011 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2012 #define NODELAY 0x0002 /* cannot do background work */ 2013 2014 /* 2015 * Structures and routines associated with pagedep caching. 2016 */ 2017 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl; 2018 u_long pagedep_hash; /* size of hash table - 1 */ 2019 #define PAGEDEP_HASH(mp, inum, lbn) \ 2020 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \ 2021 pagedep_hash]) 2022 2023 static int 2024 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp) 2025 struct pagedep_hashhead *pagedephd; 2026 ino_t ino; 2027 ufs_lbn_t lbn; 2028 struct mount *mp; 2029 int flags; 2030 struct pagedep **pagedeppp; 2031 { 2032 struct pagedep *pagedep; 2033 2034 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2035 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn && 2036 mp == pagedep->pd_list.wk_mp) { 2037 *pagedeppp = pagedep; 2038 return (1); 2039 } 2040 } 2041 *pagedeppp = NULL; 2042 return (0); 2043 } 2044 /* 2045 * Look up a pagedep. Return 1 if found, 0 otherwise. 2046 * If not found, allocate if DEPALLOC flag is passed. 2047 * Found or allocated entry is returned in pagedeppp. 2048 * This routine must be called with splbio interrupts blocked. 2049 */ 2050 static int 2051 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2052 struct mount *mp; 2053 struct buf *bp; 2054 ino_t ino; 2055 ufs_lbn_t lbn; 2056 int flags; 2057 struct pagedep **pagedeppp; 2058 { 2059 struct pagedep *pagedep; 2060 struct pagedep_hashhead *pagedephd; 2061 struct worklist *wk; 2062 int ret; 2063 int i; 2064 2065 rw_assert(&lk, RA_WLOCKED); 2066 if (bp) { 2067 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2068 if (wk->wk_type == D_PAGEDEP) { 2069 *pagedeppp = WK_PAGEDEP(wk); 2070 return (1); 2071 } 2072 } 2073 } 2074 pagedephd = PAGEDEP_HASH(mp, ino, lbn); 2075 ret = pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp); 2076 if (ret) { 2077 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2078 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2079 return (1); 2080 } 2081 if ((flags & DEPALLOC) == 0) 2082 return (0); 2083 FREE_LOCK(&lk); 2084 pagedep = malloc(sizeof(struct pagedep), 2085 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2086 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2087 ACQUIRE_LOCK(&lk); 2088 ret = pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp); 2089 if (*pagedeppp) { 2090 /* 2091 * This should never happen since we only create pagedeps 2092 * with the vnode lock held. Could be an assert. 2093 */ 2094 WORKITEM_FREE(pagedep, D_PAGEDEP); 2095 return (ret); 2096 } 2097 pagedep->pd_ino = ino; 2098 pagedep->pd_lbn = lbn; 2099 LIST_INIT(&pagedep->pd_dirremhd); 2100 LIST_INIT(&pagedep->pd_pendinghd); 2101 for (i = 0; i < DAHASHSZ; i++) 2102 LIST_INIT(&pagedep->pd_diraddhd[i]); 2103 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2104 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2105 *pagedeppp = pagedep; 2106 return (0); 2107 } 2108 2109 /* 2110 * Structures and routines associated with inodedep caching. 2111 */ 2112 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl; 2113 static u_long inodedep_hash; /* size of hash table - 1 */ 2114 #define INODEDEP_HASH(fs, inum) \ 2115 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash]) 2116 2117 static int 2118 inodedep_find(inodedephd, fs, inum, inodedeppp) 2119 struct inodedep_hashhead *inodedephd; 2120 struct fs *fs; 2121 ino_t inum; 2122 struct inodedep **inodedeppp; 2123 { 2124 struct inodedep *inodedep; 2125 2126 LIST_FOREACH(inodedep, inodedephd, id_hash) 2127 if (inum == inodedep->id_ino && fs == inodedep->id_fs) 2128 break; 2129 if (inodedep) { 2130 *inodedeppp = inodedep; 2131 return (1); 2132 } 2133 *inodedeppp = NULL; 2134 2135 return (0); 2136 } 2137 /* 2138 * Look up an inodedep. Return 1 if found, 0 if not found. 2139 * If not found, allocate if DEPALLOC flag is passed. 2140 * Found or allocated entry is returned in inodedeppp. 2141 * This routine must be called with splbio interrupts blocked. 2142 */ 2143 static int 2144 inodedep_lookup(mp, inum, flags, inodedeppp) 2145 struct mount *mp; 2146 ino_t inum; 2147 int flags; 2148 struct inodedep **inodedeppp; 2149 { 2150 struct inodedep *inodedep; 2151 struct inodedep_hashhead *inodedephd; 2152 struct fs *fs; 2153 2154 rw_assert(&lk, RA_WLOCKED); 2155 fs = VFSTOUFS(mp)->um_fs; 2156 inodedephd = INODEDEP_HASH(fs, inum); 2157 2158 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) 2159 return (1); 2160 if ((flags & DEPALLOC) == 0) 2161 return (0); 2162 /* 2163 * If we are over our limit, try to improve the situation. 2164 */ 2165 if (dep_current[D_INODEDEP] > max_softdeps && (flags & NODELAY) == 0) 2166 request_cleanup(mp, FLUSH_INODES); 2167 FREE_LOCK(&lk); 2168 inodedep = malloc(sizeof(struct inodedep), 2169 M_INODEDEP, M_SOFTDEP_FLAGS); 2170 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2171 ACQUIRE_LOCK(&lk); 2172 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) { 2173 WORKITEM_FREE(inodedep, D_INODEDEP); 2174 return (1); 2175 } 2176 inodedep->id_fs = fs; 2177 inodedep->id_ino = inum; 2178 inodedep->id_state = ALLCOMPLETE; 2179 inodedep->id_nlinkdelta = 0; 2180 inodedep->id_savedino1 = NULL; 2181 inodedep->id_savedsize = -1; 2182 inodedep->id_savedextsize = -1; 2183 inodedep->id_savednlink = -1; 2184 inodedep->id_bmsafemap = NULL; 2185 inodedep->id_mkdiradd = NULL; 2186 LIST_INIT(&inodedep->id_dirremhd); 2187 LIST_INIT(&inodedep->id_pendinghd); 2188 LIST_INIT(&inodedep->id_inowait); 2189 LIST_INIT(&inodedep->id_bufwait); 2190 TAILQ_INIT(&inodedep->id_inoreflst); 2191 TAILQ_INIT(&inodedep->id_inoupdt); 2192 TAILQ_INIT(&inodedep->id_newinoupdt); 2193 TAILQ_INIT(&inodedep->id_extupdt); 2194 TAILQ_INIT(&inodedep->id_newextupdt); 2195 TAILQ_INIT(&inodedep->id_freeblklst); 2196 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2197 *inodedeppp = inodedep; 2198 return (0); 2199 } 2200 2201 /* 2202 * Structures and routines associated with newblk caching. 2203 */ 2204 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl; 2205 u_long newblk_hash; /* size of hash table - 1 */ 2206 #define NEWBLK_HASH(fs, inum) \ 2207 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash]) 2208 2209 static int 2210 newblk_find(newblkhd, mp, newblkno, flags, newblkpp) 2211 struct newblk_hashhead *newblkhd; 2212 struct mount *mp; 2213 ufs2_daddr_t newblkno; 2214 int flags; 2215 struct newblk **newblkpp; 2216 { 2217 struct newblk *newblk; 2218 2219 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2220 if (newblkno != newblk->nb_newblkno) 2221 continue; 2222 if (mp != newblk->nb_list.wk_mp) 2223 continue; 2224 /* 2225 * If we're creating a new dependency don't match those that 2226 * have already been converted to allocdirects. This is for 2227 * a frag extend. 2228 */ 2229 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2230 continue; 2231 break; 2232 } 2233 if (newblk) { 2234 *newblkpp = newblk; 2235 return (1); 2236 } 2237 *newblkpp = NULL; 2238 return (0); 2239 } 2240 2241 /* 2242 * Look up a newblk. Return 1 if found, 0 if not found. 2243 * If not found, allocate if DEPALLOC flag is passed. 2244 * Found or allocated entry is returned in newblkpp. 2245 */ 2246 static int 2247 newblk_lookup(mp, newblkno, flags, newblkpp) 2248 struct mount *mp; 2249 ufs2_daddr_t newblkno; 2250 int flags; 2251 struct newblk **newblkpp; 2252 { 2253 struct newblk *newblk; 2254 struct newblk_hashhead *newblkhd; 2255 2256 newblkhd = NEWBLK_HASH(VFSTOUFS(mp)->um_fs, newblkno); 2257 if (newblk_find(newblkhd, mp, newblkno, flags, newblkpp)) 2258 return (1); 2259 if ((flags & DEPALLOC) == 0) 2260 return (0); 2261 FREE_LOCK(&lk); 2262 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2263 M_SOFTDEP_FLAGS | M_ZERO); 2264 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2265 ACQUIRE_LOCK(&lk); 2266 if (newblk_find(newblkhd, mp, newblkno, flags, newblkpp)) { 2267 WORKITEM_FREE(newblk, D_NEWBLK); 2268 return (1); 2269 } 2270 newblk->nb_freefrag = NULL; 2271 LIST_INIT(&newblk->nb_indirdeps); 2272 LIST_INIT(&newblk->nb_newdirblk); 2273 LIST_INIT(&newblk->nb_jwork); 2274 newblk->nb_state = ATTACHED; 2275 newblk->nb_newblkno = newblkno; 2276 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2277 *newblkpp = newblk; 2278 return (0); 2279 } 2280 2281 /* 2282 * Structures and routines associated with freed indirect block caching. 2283 */ 2284 struct freeworklst *indir_hashtbl; 2285 u_long indir_hash; /* size of hash table - 1 */ 2286 #define INDIR_HASH(mp, blkno) \ 2287 (&indir_hashtbl[((((register_t)(mp)) >> 13) + (blkno)) & indir_hash]) 2288 2289 /* 2290 * Lookup an indirect block in the indir hash table. The freework is 2291 * removed and potentially freed. The caller must do a blocking journal 2292 * write before writing to the blkno. 2293 */ 2294 static int 2295 indirblk_lookup(mp, blkno) 2296 struct mount *mp; 2297 ufs2_daddr_t blkno; 2298 { 2299 struct freework *freework; 2300 struct freeworklst *wkhd; 2301 2302 wkhd = INDIR_HASH(mp, blkno); 2303 TAILQ_FOREACH(freework, wkhd, fw_next) { 2304 if (freework->fw_blkno != blkno) 2305 continue; 2306 if (freework->fw_list.wk_mp != mp) 2307 continue; 2308 indirblk_remove(freework); 2309 return (1); 2310 } 2311 return (0); 2312 } 2313 2314 /* 2315 * Insert an indirect block represented by freework into the indirblk 2316 * hash table so that it may prevent the block from being re-used prior 2317 * to the journal being written. 2318 */ 2319 static void 2320 indirblk_insert(freework) 2321 struct freework *freework; 2322 { 2323 struct jblocks *jblocks; 2324 struct jseg *jseg; 2325 2326 jblocks = VFSTOUFS(freework->fw_list.wk_mp)->softdep_jblocks; 2327 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2328 if (jseg == NULL) 2329 return; 2330 2331 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2332 TAILQ_INSERT_HEAD(INDIR_HASH(freework->fw_list.wk_mp, 2333 freework->fw_blkno), freework, fw_next); 2334 freework->fw_state &= ~DEPCOMPLETE; 2335 } 2336 2337 static void 2338 indirblk_remove(freework) 2339 struct freework *freework; 2340 { 2341 2342 LIST_REMOVE(freework, fw_segs); 2343 TAILQ_REMOVE(INDIR_HASH(freework->fw_list.wk_mp, 2344 freework->fw_blkno), freework, fw_next); 2345 freework->fw_state |= DEPCOMPLETE; 2346 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2347 WORKITEM_FREE(freework, D_FREEWORK); 2348 } 2349 2350 /* 2351 * Executed during filesystem system initialization before 2352 * mounting any filesystems. 2353 */ 2354 void 2355 softdep_initialize() 2356 { 2357 int i; 2358 2359 LIST_INIT(&mkdirlisthd); 2360 max_softdeps = desiredvnodes * 4; 2361 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, &pagedep_hash); 2362 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash); 2363 newblk_hashtbl = hashinit(desiredvnodes / 5, M_NEWBLK, &newblk_hash); 2364 bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, &bmsafemap_hash); 2365 i = 1 << (ffs(desiredvnodes / 10) - 1); 2366 indir_hashtbl = malloc(i * sizeof(indir_hashtbl[0]), M_FREEWORK, 2367 M_WAITOK); 2368 indir_hash = i - 1; 2369 for (i = 0; i <= indir_hash; i++) 2370 TAILQ_INIT(&indir_hashtbl[i]); 2371 2372 /* initialise bioops hack */ 2373 bioops.io_start = softdep_disk_io_initiation; 2374 bioops.io_complete = softdep_disk_write_complete; 2375 bioops.io_deallocate = softdep_deallocate_dependencies; 2376 bioops.io_countdeps = softdep_count_dependencies; 2377 2378 /* Initialize the callout with an mtx. */ 2379 callout_init_mtx(&softdep_callout, &lk, 0); 2380 } 2381 2382 /* 2383 * Executed after all filesystems have been unmounted during 2384 * filesystem module unload. 2385 */ 2386 void 2387 softdep_uninitialize() 2388 { 2389 2390 callout_drain(&softdep_callout); 2391 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash); 2392 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash); 2393 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash); 2394 hashdestroy(bmsafemap_hashtbl, M_BMSAFEMAP, bmsafemap_hash); 2395 free(indir_hashtbl, M_FREEWORK); 2396 } 2397 2398 /* 2399 * Called at mount time to notify the dependency code that a 2400 * filesystem wishes to use it. 2401 */ 2402 int 2403 softdep_mount(devvp, mp, fs, cred) 2404 struct vnode *devvp; 2405 struct mount *mp; 2406 struct fs *fs; 2407 struct ucred *cred; 2408 { 2409 struct csum_total cstotal; 2410 struct ufsmount *ump; 2411 struct cg *cgp; 2412 struct buf *bp; 2413 int error, cyl; 2414 2415 MNT_ILOCK(mp); 2416 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2417 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2418 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2419 MNTK_SOFTDEP | MNTK_NOASYNC; 2420 } 2421 MNT_IUNLOCK(mp); 2422 ump = VFSTOUFS(mp); 2423 LIST_INIT(&ump->softdep_workitem_pending); 2424 LIST_INIT(&ump->softdep_journal_pending); 2425 TAILQ_INIT(&ump->softdep_unlinked); 2426 LIST_INIT(&ump->softdep_dirtycg); 2427 ump->softdep_worklist_tail = NULL; 2428 ump->softdep_on_worklist = 0; 2429 ump->softdep_deps = 0; 2430 if ((fs->fs_flags & FS_SUJ) && 2431 (error = journal_mount(mp, fs, cred)) != 0) { 2432 printf("Failed to start journal: %d\n", error); 2433 return (error); 2434 } 2435 /* 2436 * When doing soft updates, the counters in the 2437 * superblock may have gotten out of sync. Recomputation 2438 * can take a long time and can be deferred for background 2439 * fsck. However, the old behavior of scanning the cylinder 2440 * groups and recalculating them at mount time is available 2441 * by setting vfs.ffs.compute_summary_at_mount to one. 2442 */ 2443 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2444 return (0); 2445 bzero(&cstotal, sizeof cstotal); 2446 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2447 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2448 fs->fs_cgsize, cred, &bp)) != 0) { 2449 brelse(bp); 2450 return (error); 2451 } 2452 cgp = (struct cg *)bp->b_data; 2453 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2454 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2455 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2456 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2457 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2458 brelse(bp); 2459 } 2460 #ifdef DEBUG 2461 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2462 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2463 #endif 2464 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2465 return (0); 2466 } 2467 2468 void 2469 softdep_unmount(mp) 2470 struct mount *mp; 2471 { 2472 2473 MNT_ILOCK(mp); 2474 mp->mnt_flag &= ~MNT_SOFTDEP; 2475 if (MOUNTEDSUJ(mp) == 0) { 2476 MNT_IUNLOCK(mp); 2477 return; 2478 } 2479 mp->mnt_flag &= ~MNT_SUJ; 2480 MNT_IUNLOCK(mp); 2481 journal_unmount(mp); 2482 } 2483 2484 static struct jblocks * 2485 jblocks_create(void) 2486 { 2487 struct jblocks *jblocks; 2488 2489 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2490 TAILQ_INIT(&jblocks->jb_segs); 2491 jblocks->jb_avail = 10; 2492 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2493 M_JBLOCKS, M_WAITOK | M_ZERO); 2494 2495 return (jblocks); 2496 } 2497 2498 static ufs2_daddr_t 2499 jblocks_alloc(jblocks, bytes, actual) 2500 struct jblocks *jblocks; 2501 int bytes; 2502 int *actual; 2503 { 2504 ufs2_daddr_t daddr; 2505 struct jextent *jext; 2506 int freecnt; 2507 int blocks; 2508 2509 blocks = bytes / DEV_BSIZE; 2510 jext = &jblocks->jb_extent[jblocks->jb_head]; 2511 freecnt = jext->je_blocks - jblocks->jb_off; 2512 if (freecnt == 0) { 2513 jblocks->jb_off = 0; 2514 if (++jblocks->jb_head > jblocks->jb_used) 2515 jblocks->jb_head = 0; 2516 jext = &jblocks->jb_extent[jblocks->jb_head]; 2517 freecnt = jext->je_blocks; 2518 } 2519 if (freecnt > blocks) 2520 freecnt = blocks; 2521 *actual = freecnt * DEV_BSIZE; 2522 daddr = jext->je_daddr + jblocks->jb_off; 2523 jblocks->jb_off += freecnt; 2524 jblocks->jb_free -= freecnt; 2525 2526 return (daddr); 2527 } 2528 2529 static void 2530 jblocks_free(jblocks, mp, bytes) 2531 struct jblocks *jblocks; 2532 struct mount *mp; 2533 int bytes; 2534 { 2535 2536 jblocks->jb_free += bytes / DEV_BSIZE; 2537 if (jblocks->jb_suspended) 2538 worklist_speedup(); 2539 wakeup(jblocks); 2540 } 2541 2542 static void 2543 jblocks_destroy(jblocks) 2544 struct jblocks *jblocks; 2545 { 2546 2547 if (jblocks->jb_extent) 2548 free(jblocks->jb_extent, M_JBLOCKS); 2549 free(jblocks, M_JBLOCKS); 2550 } 2551 2552 static void 2553 jblocks_add(jblocks, daddr, blocks) 2554 struct jblocks *jblocks; 2555 ufs2_daddr_t daddr; 2556 int blocks; 2557 { 2558 struct jextent *jext; 2559 2560 jblocks->jb_blocks += blocks; 2561 jblocks->jb_free += blocks; 2562 jext = &jblocks->jb_extent[jblocks->jb_used]; 2563 /* Adding the first block. */ 2564 if (jext->je_daddr == 0) { 2565 jext->je_daddr = daddr; 2566 jext->je_blocks = blocks; 2567 return; 2568 } 2569 /* Extending the last extent. */ 2570 if (jext->je_daddr + jext->je_blocks == daddr) { 2571 jext->je_blocks += blocks; 2572 return; 2573 } 2574 /* Adding a new extent. */ 2575 if (++jblocks->jb_used == jblocks->jb_avail) { 2576 jblocks->jb_avail *= 2; 2577 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2578 M_JBLOCKS, M_WAITOK | M_ZERO); 2579 memcpy(jext, jblocks->jb_extent, 2580 sizeof(struct jextent) * jblocks->jb_used); 2581 free(jblocks->jb_extent, M_JBLOCKS); 2582 jblocks->jb_extent = jext; 2583 } 2584 jext = &jblocks->jb_extent[jblocks->jb_used]; 2585 jext->je_daddr = daddr; 2586 jext->je_blocks = blocks; 2587 return; 2588 } 2589 2590 int 2591 softdep_journal_lookup(mp, vpp) 2592 struct mount *mp; 2593 struct vnode **vpp; 2594 { 2595 struct componentname cnp; 2596 struct vnode *dvp; 2597 ino_t sujournal; 2598 int error; 2599 2600 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp); 2601 if (error) 2602 return (error); 2603 bzero(&cnp, sizeof(cnp)); 2604 cnp.cn_nameiop = LOOKUP; 2605 cnp.cn_flags = ISLASTCN; 2606 cnp.cn_thread = curthread; 2607 cnp.cn_cred = curthread->td_ucred; 2608 cnp.cn_pnbuf = SUJ_FILE; 2609 cnp.cn_nameptr = SUJ_FILE; 2610 cnp.cn_namelen = strlen(SUJ_FILE); 2611 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2612 vput(dvp); 2613 if (error != 0) 2614 return (error); 2615 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2616 return (error); 2617 } 2618 2619 /* 2620 * Open and verify the journal file. 2621 */ 2622 static int 2623 journal_mount(mp, fs, cred) 2624 struct mount *mp; 2625 struct fs *fs; 2626 struct ucred *cred; 2627 { 2628 struct jblocks *jblocks; 2629 struct vnode *vp; 2630 struct inode *ip; 2631 ufs2_daddr_t blkno; 2632 int bcount; 2633 int error; 2634 int i; 2635 2636 error = softdep_journal_lookup(mp, &vp); 2637 if (error != 0) { 2638 printf("Failed to find journal. Use tunefs to create one\n"); 2639 return (error); 2640 } 2641 ip = VTOI(vp); 2642 if (ip->i_size < SUJ_MIN) { 2643 error = ENOSPC; 2644 goto out; 2645 } 2646 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2647 jblocks = jblocks_create(); 2648 for (i = 0; i < bcount; i++) { 2649 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2650 if (error) 2651 break; 2652 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2653 } 2654 if (error) { 2655 jblocks_destroy(jblocks); 2656 goto out; 2657 } 2658 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2659 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2660 VFSTOUFS(mp)->softdep_jblocks = jblocks; 2661 out: 2662 if (error == 0) { 2663 MNT_ILOCK(mp); 2664 mp->mnt_flag |= MNT_SUJ; 2665 mp->mnt_flag &= ~MNT_SOFTDEP; 2666 MNT_IUNLOCK(mp); 2667 /* 2668 * Only validate the journal contents if the 2669 * filesystem is clean, otherwise we write the logs 2670 * but they'll never be used. If the filesystem was 2671 * still dirty when we mounted it the journal is 2672 * invalid and a new journal can only be valid if it 2673 * starts from a clean mount. 2674 */ 2675 if (fs->fs_clean) { 2676 DIP_SET(ip, i_modrev, fs->fs_mtime); 2677 ip->i_flags |= IN_MODIFIED; 2678 ffs_update(vp, 1); 2679 } 2680 } 2681 vput(vp); 2682 return (error); 2683 } 2684 2685 static void 2686 journal_unmount(mp) 2687 struct mount *mp; 2688 { 2689 struct ufsmount *ump; 2690 2691 ump = VFSTOUFS(mp); 2692 if (ump->softdep_jblocks) 2693 jblocks_destroy(ump->softdep_jblocks); 2694 ump->softdep_jblocks = NULL; 2695 } 2696 2697 /* 2698 * Called when a journal record is ready to be written. Space is allocated 2699 * and the journal entry is created when the journal is flushed to stable 2700 * store. 2701 */ 2702 static void 2703 add_to_journal(wk) 2704 struct worklist *wk; 2705 { 2706 struct ufsmount *ump; 2707 2708 rw_assert(&lk, RA_WLOCKED); 2709 ump = VFSTOUFS(wk->wk_mp); 2710 if (wk->wk_state & ONWORKLIST) 2711 panic("add_to_journal: %s(0x%X) already on list", 2712 TYPENAME(wk->wk_type), wk->wk_state); 2713 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2714 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2715 ump->softdep_jblocks->jb_age = ticks; 2716 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2717 } else 2718 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2719 ump->softdep_journal_tail = wk; 2720 ump->softdep_on_journal += 1; 2721 } 2722 2723 /* 2724 * Remove an arbitrary item for the journal worklist maintain the tail 2725 * pointer. This happens when a new operation obviates the need to 2726 * journal an old operation. 2727 */ 2728 static void 2729 remove_from_journal(wk) 2730 struct worklist *wk; 2731 { 2732 struct ufsmount *ump; 2733 2734 rw_assert(&lk, RA_WLOCKED); 2735 ump = VFSTOUFS(wk->wk_mp); 2736 #ifdef SUJ_DEBUG 2737 { 2738 struct worklist *wkn; 2739 2740 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2741 if (wkn == wk) 2742 break; 2743 if (wkn == NULL) 2744 panic("remove_from_journal: %p is not in journal", wk); 2745 } 2746 #endif 2747 /* 2748 * We emulate a TAILQ to save space in most structures which do not 2749 * require TAILQ semantics. Here we must update the tail position 2750 * when removing the tail which is not the final entry. This works 2751 * only if the worklist linkage are at the beginning of the structure. 2752 */ 2753 if (ump->softdep_journal_tail == wk) 2754 ump->softdep_journal_tail = 2755 (struct worklist *)wk->wk_list.le_prev; 2756 2757 WORKLIST_REMOVE(wk); 2758 ump->softdep_on_journal -= 1; 2759 } 2760 2761 /* 2762 * Check for journal space as well as dependency limits so the prelink 2763 * code can throttle both journaled and non-journaled filesystems. 2764 * Threshold is 0 for low and 1 for min. 2765 */ 2766 static int 2767 journal_space(ump, thresh) 2768 struct ufsmount *ump; 2769 int thresh; 2770 { 2771 struct jblocks *jblocks; 2772 int avail; 2773 2774 jblocks = ump->softdep_jblocks; 2775 if (jblocks == NULL) 2776 return (1); 2777 /* 2778 * We use a tighter restriction here to prevent request_cleanup() 2779 * running in threads from running into locks we currently hold. 2780 */ 2781 if (dep_current[D_INODEDEP] > (max_softdeps / 10) * 9) 2782 return (0); 2783 if (thresh) 2784 thresh = jblocks->jb_min; 2785 else 2786 thresh = jblocks->jb_low; 2787 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2788 avail = jblocks->jb_free - avail; 2789 2790 return (avail > thresh); 2791 } 2792 2793 static void 2794 journal_suspend(ump) 2795 struct ufsmount *ump; 2796 { 2797 struct jblocks *jblocks; 2798 struct mount *mp; 2799 2800 mp = UFSTOVFS(ump); 2801 jblocks = ump->softdep_jblocks; 2802 MNT_ILOCK(mp); 2803 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2804 stat_journal_min++; 2805 mp->mnt_kern_flag |= MNTK_SUSPEND; 2806 mp->mnt_susp_owner = FIRST_THREAD_IN_PROC(softdepproc); 2807 } 2808 jblocks->jb_suspended = 1; 2809 MNT_IUNLOCK(mp); 2810 } 2811 2812 static int 2813 journal_unsuspend(struct ufsmount *ump) 2814 { 2815 struct jblocks *jblocks; 2816 struct mount *mp; 2817 2818 mp = UFSTOVFS(ump); 2819 jblocks = ump->softdep_jblocks; 2820 2821 if (jblocks != NULL && jblocks->jb_suspended && 2822 journal_space(ump, jblocks->jb_min)) { 2823 jblocks->jb_suspended = 0; 2824 FREE_LOCK(&lk); 2825 mp->mnt_susp_owner = curthread; 2826 vfs_write_resume(mp, 0); 2827 ACQUIRE_LOCK(&lk); 2828 return (1); 2829 } 2830 return (0); 2831 } 2832 2833 /* 2834 * Called before any allocation function to be certain that there is 2835 * sufficient space in the journal prior to creating any new records. 2836 * Since in the case of block allocation we may have multiple locked 2837 * buffers at the time of the actual allocation we can not block 2838 * when the journal records are created. Doing so would create a deadlock 2839 * if any of these buffers needed to be flushed to reclaim space. Instead 2840 * we require a sufficiently large amount of available space such that 2841 * each thread in the system could have passed this allocation check and 2842 * still have sufficient free space. With 20% of a minimum journal size 2843 * of 1MB we have 6553 records available. 2844 */ 2845 int 2846 softdep_prealloc(vp, waitok) 2847 struct vnode *vp; 2848 int waitok; 2849 { 2850 struct ufsmount *ump; 2851 2852 /* 2853 * Nothing to do if we are not running journaled soft updates. 2854 * If we currently hold the snapshot lock, we must avoid handling 2855 * other resources that could cause deadlock. 2856 */ 2857 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp))) 2858 return (0); 2859 ump = VFSTOUFS(vp->v_mount); 2860 ACQUIRE_LOCK(&lk); 2861 if (journal_space(ump, 0)) { 2862 FREE_LOCK(&lk); 2863 return (0); 2864 } 2865 stat_journal_low++; 2866 FREE_LOCK(&lk); 2867 if (waitok == MNT_NOWAIT) 2868 return (ENOSPC); 2869 /* 2870 * Attempt to sync this vnode once to flush any journal 2871 * work attached to it. 2872 */ 2873 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 2874 ffs_syncvnode(vp, waitok, 0); 2875 ACQUIRE_LOCK(&lk); 2876 process_removes(vp); 2877 process_truncates(vp); 2878 if (journal_space(ump, 0) == 0) { 2879 softdep_speedup(); 2880 if (journal_space(ump, 1) == 0) 2881 journal_suspend(ump); 2882 } 2883 FREE_LOCK(&lk); 2884 2885 return (0); 2886 } 2887 2888 /* 2889 * Before adjusting a link count on a vnode verify that we have sufficient 2890 * journal space. If not, process operations that depend on the currently 2891 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 2892 * and softdep flush threads can not acquire these locks to reclaim space. 2893 */ 2894 static void 2895 softdep_prelink(dvp, vp) 2896 struct vnode *dvp; 2897 struct vnode *vp; 2898 { 2899 struct ufsmount *ump; 2900 2901 ump = VFSTOUFS(dvp->v_mount); 2902 rw_assert(&lk, RA_WLOCKED); 2903 /* 2904 * Nothing to do if we have sufficient journal space. 2905 * If we currently hold the snapshot lock, we must avoid 2906 * handling other resources that could cause deadlock. 2907 */ 2908 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 2909 return; 2910 stat_journal_low++; 2911 FREE_LOCK(&lk); 2912 if (vp) 2913 ffs_syncvnode(vp, MNT_NOWAIT, 0); 2914 ffs_syncvnode(dvp, MNT_WAIT, 0); 2915 ACQUIRE_LOCK(&lk); 2916 /* Process vp before dvp as it may create .. removes. */ 2917 if (vp) { 2918 process_removes(vp); 2919 process_truncates(vp); 2920 } 2921 process_removes(dvp); 2922 process_truncates(dvp); 2923 softdep_speedup(); 2924 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 2925 if (journal_space(ump, 0) == 0) { 2926 softdep_speedup(); 2927 if (journal_space(ump, 1) == 0) 2928 journal_suspend(ump); 2929 } 2930 } 2931 2932 static void 2933 jseg_write(ump, jseg, data) 2934 struct ufsmount *ump; 2935 struct jseg *jseg; 2936 uint8_t *data; 2937 { 2938 struct jsegrec *rec; 2939 2940 rec = (struct jsegrec *)data; 2941 rec->jsr_seq = jseg->js_seq; 2942 rec->jsr_oldest = jseg->js_oldseq; 2943 rec->jsr_cnt = jseg->js_cnt; 2944 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 2945 rec->jsr_crc = 0; 2946 rec->jsr_time = ump->um_fs->fs_mtime; 2947 } 2948 2949 static inline void 2950 inoref_write(inoref, jseg, rec) 2951 struct inoref *inoref; 2952 struct jseg *jseg; 2953 struct jrefrec *rec; 2954 { 2955 2956 inoref->if_jsegdep->jd_seg = jseg; 2957 rec->jr_ino = inoref->if_ino; 2958 rec->jr_parent = inoref->if_parent; 2959 rec->jr_nlink = inoref->if_nlink; 2960 rec->jr_mode = inoref->if_mode; 2961 rec->jr_diroff = inoref->if_diroff; 2962 } 2963 2964 static void 2965 jaddref_write(jaddref, jseg, data) 2966 struct jaddref *jaddref; 2967 struct jseg *jseg; 2968 uint8_t *data; 2969 { 2970 struct jrefrec *rec; 2971 2972 rec = (struct jrefrec *)data; 2973 rec->jr_op = JOP_ADDREF; 2974 inoref_write(&jaddref->ja_ref, jseg, rec); 2975 } 2976 2977 static void 2978 jremref_write(jremref, jseg, data) 2979 struct jremref *jremref; 2980 struct jseg *jseg; 2981 uint8_t *data; 2982 { 2983 struct jrefrec *rec; 2984 2985 rec = (struct jrefrec *)data; 2986 rec->jr_op = JOP_REMREF; 2987 inoref_write(&jremref->jr_ref, jseg, rec); 2988 } 2989 2990 static void 2991 jmvref_write(jmvref, jseg, data) 2992 struct jmvref *jmvref; 2993 struct jseg *jseg; 2994 uint8_t *data; 2995 { 2996 struct jmvrec *rec; 2997 2998 rec = (struct jmvrec *)data; 2999 rec->jm_op = JOP_MVREF; 3000 rec->jm_ino = jmvref->jm_ino; 3001 rec->jm_parent = jmvref->jm_parent; 3002 rec->jm_oldoff = jmvref->jm_oldoff; 3003 rec->jm_newoff = jmvref->jm_newoff; 3004 } 3005 3006 static void 3007 jnewblk_write(jnewblk, jseg, data) 3008 struct jnewblk *jnewblk; 3009 struct jseg *jseg; 3010 uint8_t *data; 3011 { 3012 struct jblkrec *rec; 3013 3014 jnewblk->jn_jsegdep->jd_seg = jseg; 3015 rec = (struct jblkrec *)data; 3016 rec->jb_op = JOP_NEWBLK; 3017 rec->jb_ino = jnewblk->jn_ino; 3018 rec->jb_blkno = jnewblk->jn_blkno; 3019 rec->jb_lbn = jnewblk->jn_lbn; 3020 rec->jb_frags = jnewblk->jn_frags; 3021 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3022 } 3023 3024 static void 3025 jfreeblk_write(jfreeblk, jseg, data) 3026 struct jfreeblk *jfreeblk; 3027 struct jseg *jseg; 3028 uint8_t *data; 3029 { 3030 struct jblkrec *rec; 3031 3032 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3033 rec = (struct jblkrec *)data; 3034 rec->jb_op = JOP_FREEBLK; 3035 rec->jb_ino = jfreeblk->jf_ino; 3036 rec->jb_blkno = jfreeblk->jf_blkno; 3037 rec->jb_lbn = jfreeblk->jf_lbn; 3038 rec->jb_frags = jfreeblk->jf_frags; 3039 rec->jb_oldfrags = 0; 3040 } 3041 3042 static void 3043 jfreefrag_write(jfreefrag, jseg, data) 3044 struct jfreefrag *jfreefrag; 3045 struct jseg *jseg; 3046 uint8_t *data; 3047 { 3048 struct jblkrec *rec; 3049 3050 jfreefrag->fr_jsegdep->jd_seg = jseg; 3051 rec = (struct jblkrec *)data; 3052 rec->jb_op = JOP_FREEBLK; 3053 rec->jb_ino = jfreefrag->fr_ino; 3054 rec->jb_blkno = jfreefrag->fr_blkno; 3055 rec->jb_lbn = jfreefrag->fr_lbn; 3056 rec->jb_frags = jfreefrag->fr_frags; 3057 rec->jb_oldfrags = 0; 3058 } 3059 3060 static void 3061 jtrunc_write(jtrunc, jseg, data) 3062 struct jtrunc *jtrunc; 3063 struct jseg *jseg; 3064 uint8_t *data; 3065 { 3066 struct jtrncrec *rec; 3067 3068 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3069 rec = (struct jtrncrec *)data; 3070 rec->jt_op = JOP_TRUNC; 3071 rec->jt_ino = jtrunc->jt_ino; 3072 rec->jt_size = jtrunc->jt_size; 3073 rec->jt_extsize = jtrunc->jt_extsize; 3074 } 3075 3076 static void 3077 jfsync_write(jfsync, jseg, data) 3078 struct jfsync *jfsync; 3079 struct jseg *jseg; 3080 uint8_t *data; 3081 { 3082 struct jtrncrec *rec; 3083 3084 rec = (struct jtrncrec *)data; 3085 rec->jt_op = JOP_SYNC; 3086 rec->jt_ino = jfsync->jfs_ino; 3087 rec->jt_size = jfsync->jfs_size; 3088 rec->jt_extsize = jfsync->jfs_extsize; 3089 } 3090 3091 static void 3092 softdep_flushjournal(mp) 3093 struct mount *mp; 3094 { 3095 struct jblocks *jblocks; 3096 struct ufsmount *ump; 3097 3098 if (MOUNTEDSUJ(mp) == 0) 3099 return; 3100 ump = VFSTOUFS(mp); 3101 jblocks = ump->softdep_jblocks; 3102 ACQUIRE_LOCK(&lk); 3103 while (ump->softdep_on_journal) { 3104 jblocks->jb_needseg = 1; 3105 softdep_process_journal(mp, NULL, MNT_WAIT); 3106 } 3107 FREE_LOCK(&lk); 3108 } 3109 3110 static void softdep_synchronize_completed(struct bio *); 3111 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3112 3113 static void 3114 softdep_synchronize_completed(bp) 3115 struct bio *bp; 3116 { 3117 struct jseg *oldest; 3118 struct jseg *jseg; 3119 3120 /* 3121 * caller1 marks the last segment written before we issued the 3122 * synchronize cache. 3123 */ 3124 jseg = bp->bio_caller1; 3125 oldest = NULL; 3126 ACQUIRE_LOCK(&lk); 3127 /* 3128 * Mark all the journal entries waiting on the synchronize cache 3129 * as completed so they may continue on. 3130 */ 3131 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3132 jseg->js_state |= COMPLETE; 3133 oldest = jseg; 3134 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3135 } 3136 /* 3137 * Restart deferred journal entry processing from the oldest 3138 * completed jseg. 3139 */ 3140 if (oldest) 3141 complete_jsegs(oldest); 3142 3143 FREE_LOCK(&lk); 3144 g_destroy_bio(bp); 3145 } 3146 3147 /* 3148 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3149 * barriers. The journal must be written prior to any blocks that depend 3150 * on it and the journal can not be released until the blocks have be 3151 * written. This code handles both barriers simultaneously. 3152 */ 3153 static void 3154 softdep_synchronize(bp, ump, caller1) 3155 struct bio *bp; 3156 struct ufsmount *ump; 3157 void *caller1; 3158 { 3159 3160 bp->bio_cmd = BIO_FLUSH; 3161 bp->bio_flags |= BIO_ORDERED; 3162 bp->bio_data = NULL; 3163 bp->bio_offset = ump->um_cp->provider->mediasize; 3164 bp->bio_length = 0; 3165 bp->bio_done = softdep_synchronize_completed; 3166 bp->bio_caller1 = caller1; 3167 g_io_request(bp, 3168 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3169 } 3170 3171 /* 3172 * Flush some journal records to disk. 3173 */ 3174 static void 3175 softdep_process_journal(mp, needwk, flags) 3176 struct mount *mp; 3177 struct worklist *needwk; 3178 int flags; 3179 { 3180 struct jblocks *jblocks; 3181 struct ufsmount *ump; 3182 struct worklist *wk; 3183 struct jseg *jseg; 3184 struct buf *bp; 3185 struct bio *bio; 3186 uint8_t *data; 3187 struct fs *fs; 3188 int shouldflush; 3189 int segwritten; 3190 int jrecmin; /* Minimum records per block. */ 3191 int jrecmax; /* Maximum records per block. */ 3192 int size; 3193 int cnt; 3194 int off; 3195 int devbsize; 3196 3197 if (MOUNTEDSUJ(mp) == 0) 3198 return; 3199 shouldflush = softdep_flushcache; 3200 bio = NULL; 3201 jseg = NULL; 3202 ump = VFSTOUFS(mp); 3203 fs = ump->um_fs; 3204 jblocks = ump->softdep_jblocks; 3205 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3206 /* 3207 * We write anywhere between a disk block and fs block. The upper 3208 * bound is picked to prevent buffer cache fragmentation and limit 3209 * processing time per I/O. 3210 */ 3211 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3212 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3213 segwritten = 0; 3214 for (;;) { 3215 cnt = ump->softdep_on_journal; 3216 /* 3217 * Criteria for writing a segment: 3218 * 1) We have a full block. 3219 * 2) We're called from jwait() and haven't found the 3220 * journal item yet. 3221 * 3) Always write if needseg is set. 3222 * 4) If we are called from process_worklist and have 3223 * not yet written anything we write a partial block 3224 * to enforce a 1 second maximum latency on journal 3225 * entries. 3226 */ 3227 if (cnt < (jrecmax - 1) && needwk == NULL && 3228 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3229 break; 3230 cnt++; 3231 /* 3232 * Verify some free journal space. softdep_prealloc() should 3233 * guarantee that we don't run out so this is indicative of 3234 * a problem with the flow control. Try to recover 3235 * gracefully in any event. 3236 */ 3237 while (jblocks->jb_free == 0) { 3238 if (flags != MNT_WAIT) 3239 break; 3240 printf("softdep: Out of journal space!\n"); 3241 softdep_speedup(); 3242 msleep(jblocks, &lk, PRIBIO, "jblocks", hz); 3243 } 3244 FREE_LOCK(&lk); 3245 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3246 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3247 LIST_INIT(&jseg->js_entries); 3248 LIST_INIT(&jseg->js_indirs); 3249 jseg->js_state = ATTACHED; 3250 if (shouldflush == 0) 3251 jseg->js_state |= COMPLETE; 3252 else if (bio == NULL) 3253 bio = g_alloc_bio(); 3254 jseg->js_jblocks = jblocks; 3255 bp = geteblk(fs->fs_bsize, 0); 3256 ACQUIRE_LOCK(&lk); 3257 /* 3258 * If there was a race while we were allocating the block 3259 * and jseg the entry we care about was likely written. 3260 * We bail out in both the WAIT and NOWAIT case and assume 3261 * the caller will loop if the entry it cares about is 3262 * not written. 3263 */ 3264 cnt = ump->softdep_on_journal; 3265 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3266 bp->b_flags |= B_INVAL | B_NOCACHE; 3267 WORKITEM_FREE(jseg, D_JSEG); 3268 FREE_LOCK(&lk); 3269 brelse(bp); 3270 ACQUIRE_LOCK(&lk); 3271 break; 3272 } 3273 /* 3274 * Calculate the disk block size required for the available 3275 * records rounded to the min size. 3276 */ 3277 if (cnt == 0) 3278 size = devbsize; 3279 else if (cnt < jrecmax) 3280 size = howmany(cnt, jrecmin) * devbsize; 3281 else 3282 size = fs->fs_bsize; 3283 /* 3284 * Allocate a disk block for this journal data and account 3285 * for truncation of the requested size if enough contiguous 3286 * space was not available. 3287 */ 3288 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3289 bp->b_lblkno = bp->b_blkno; 3290 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3291 bp->b_bcount = size; 3292 bp->b_flags &= ~B_INVAL; 3293 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3294 /* 3295 * Initialize our jseg with cnt records. Assign the next 3296 * sequence number to it and link it in-order. 3297 */ 3298 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3299 jseg->js_buf = bp; 3300 jseg->js_cnt = cnt; 3301 jseg->js_refs = cnt + 1; /* Self ref. */ 3302 jseg->js_size = size; 3303 jseg->js_seq = jblocks->jb_nextseq++; 3304 if (jblocks->jb_oldestseg == NULL) 3305 jblocks->jb_oldestseg = jseg; 3306 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3307 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3308 if (jblocks->jb_writeseg == NULL) 3309 jblocks->jb_writeseg = jseg; 3310 /* 3311 * Start filling in records from the pending list. 3312 */ 3313 data = bp->b_data; 3314 off = 0; 3315 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3316 != NULL) { 3317 if (cnt == 0) 3318 break; 3319 /* Place a segment header on every device block. */ 3320 if ((off % devbsize) == 0) { 3321 jseg_write(ump, jseg, data); 3322 off += JREC_SIZE; 3323 data = bp->b_data + off; 3324 } 3325 if (wk == needwk) 3326 needwk = NULL; 3327 remove_from_journal(wk); 3328 wk->wk_state |= INPROGRESS; 3329 WORKLIST_INSERT(&jseg->js_entries, wk); 3330 switch (wk->wk_type) { 3331 case D_JADDREF: 3332 jaddref_write(WK_JADDREF(wk), jseg, data); 3333 break; 3334 case D_JREMREF: 3335 jremref_write(WK_JREMREF(wk), jseg, data); 3336 break; 3337 case D_JMVREF: 3338 jmvref_write(WK_JMVREF(wk), jseg, data); 3339 break; 3340 case D_JNEWBLK: 3341 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3342 break; 3343 case D_JFREEBLK: 3344 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3345 break; 3346 case D_JFREEFRAG: 3347 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3348 break; 3349 case D_JTRUNC: 3350 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3351 break; 3352 case D_JFSYNC: 3353 jfsync_write(WK_JFSYNC(wk), jseg, data); 3354 break; 3355 default: 3356 panic("process_journal: Unknown type %s", 3357 TYPENAME(wk->wk_type)); 3358 /* NOTREACHED */ 3359 } 3360 off += JREC_SIZE; 3361 data = bp->b_data + off; 3362 cnt--; 3363 } 3364 /* 3365 * Write this one buffer and continue. 3366 */ 3367 segwritten = 1; 3368 jblocks->jb_needseg = 0; 3369 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3370 FREE_LOCK(&lk); 3371 pbgetvp(ump->um_devvp, bp); 3372 /* 3373 * We only do the blocking wait once we find the journal 3374 * entry we're looking for. 3375 */ 3376 if (needwk == NULL && flags == MNT_WAIT) 3377 bwrite(bp); 3378 else 3379 bawrite(bp); 3380 ACQUIRE_LOCK(&lk); 3381 } 3382 /* 3383 * If we wrote a segment issue a synchronize cache so the journal 3384 * is reflected on disk before the data is written. Since reclaiming 3385 * journal space also requires writing a journal record this 3386 * process also enforces a barrier before reclamation. 3387 */ 3388 if (segwritten && shouldflush) { 3389 softdep_synchronize(bio, ump, 3390 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3391 } else if (bio) 3392 g_destroy_bio(bio); 3393 /* 3394 * If we've suspended the filesystem because we ran out of journal 3395 * space either try to sync it here to make some progress or 3396 * unsuspend it if we already have. 3397 */ 3398 if (flags == 0 && jblocks->jb_suspended) { 3399 if (journal_unsuspend(ump)) 3400 return; 3401 FREE_LOCK(&lk); 3402 VFS_SYNC(mp, MNT_NOWAIT); 3403 ffs_sbupdate(ump, MNT_WAIT, 0); 3404 ACQUIRE_LOCK(&lk); 3405 } 3406 } 3407 3408 /* 3409 * Complete a jseg, allowing all dependencies awaiting journal writes 3410 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3411 * structures so that the journal segment can be freed to reclaim space. 3412 */ 3413 static void 3414 complete_jseg(jseg) 3415 struct jseg *jseg; 3416 { 3417 struct worklist *wk; 3418 struct jmvref *jmvref; 3419 int waiting; 3420 #ifdef INVARIANTS 3421 int i = 0; 3422 #endif 3423 3424 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3425 WORKLIST_REMOVE(wk); 3426 waiting = wk->wk_state & IOWAITING; 3427 wk->wk_state &= ~(INPROGRESS | IOWAITING); 3428 wk->wk_state |= COMPLETE; 3429 KASSERT(i++ < jseg->js_cnt, 3430 ("handle_written_jseg: overflow %d >= %d", 3431 i - 1, jseg->js_cnt)); 3432 switch (wk->wk_type) { 3433 case D_JADDREF: 3434 handle_written_jaddref(WK_JADDREF(wk)); 3435 break; 3436 case D_JREMREF: 3437 handle_written_jremref(WK_JREMREF(wk)); 3438 break; 3439 case D_JMVREF: 3440 rele_jseg(jseg); /* No jsegdep. */ 3441 jmvref = WK_JMVREF(wk); 3442 LIST_REMOVE(jmvref, jm_deps); 3443 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3444 free_pagedep(jmvref->jm_pagedep); 3445 WORKITEM_FREE(jmvref, D_JMVREF); 3446 break; 3447 case D_JNEWBLK: 3448 handle_written_jnewblk(WK_JNEWBLK(wk)); 3449 break; 3450 case D_JFREEBLK: 3451 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3452 break; 3453 case D_JTRUNC: 3454 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3455 break; 3456 case D_JFSYNC: 3457 rele_jseg(jseg); /* No jsegdep. */ 3458 WORKITEM_FREE(wk, D_JFSYNC); 3459 break; 3460 case D_JFREEFRAG: 3461 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3462 break; 3463 default: 3464 panic("handle_written_jseg: Unknown type %s", 3465 TYPENAME(wk->wk_type)); 3466 /* NOTREACHED */ 3467 } 3468 if (waiting) 3469 wakeup(wk); 3470 } 3471 /* Release the self reference so the structure may be freed. */ 3472 rele_jseg(jseg); 3473 } 3474 3475 /* 3476 * Determine which jsegs are ready for completion processing. Waits for 3477 * synchronize cache to complete as well as forcing in-order completion 3478 * of journal entries. 3479 */ 3480 static void 3481 complete_jsegs(jseg) 3482 struct jseg *jseg; 3483 { 3484 struct jblocks *jblocks; 3485 struct jseg *jsegn; 3486 3487 jblocks = jseg->js_jblocks; 3488 /* 3489 * Don't allow out of order completions. If this isn't the first 3490 * block wait for it to write before we're done. 3491 */ 3492 if (jseg != jblocks->jb_writeseg) 3493 return; 3494 /* Iterate through available jsegs processing their entries. */ 3495 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3496 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3497 jsegn = TAILQ_NEXT(jseg, js_next); 3498 complete_jseg(jseg); 3499 jseg = jsegn; 3500 } 3501 jblocks->jb_writeseg = jseg; 3502 /* 3503 * Attempt to free jsegs now that oldestwrseq may have advanced. 3504 */ 3505 free_jsegs(jblocks); 3506 } 3507 3508 /* 3509 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3510 * the final completions. 3511 */ 3512 static void 3513 handle_written_jseg(jseg, bp) 3514 struct jseg *jseg; 3515 struct buf *bp; 3516 { 3517 3518 if (jseg->js_refs == 0) 3519 panic("handle_written_jseg: No self-reference on %p", jseg); 3520 jseg->js_state |= DEPCOMPLETE; 3521 /* 3522 * We'll never need this buffer again, set flags so it will be 3523 * discarded. 3524 */ 3525 bp->b_flags |= B_INVAL | B_NOCACHE; 3526 pbrelvp(bp); 3527 complete_jsegs(jseg); 3528 } 3529 3530 static inline struct jsegdep * 3531 inoref_jseg(inoref) 3532 struct inoref *inoref; 3533 { 3534 struct jsegdep *jsegdep; 3535 3536 jsegdep = inoref->if_jsegdep; 3537 inoref->if_jsegdep = NULL; 3538 3539 return (jsegdep); 3540 } 3541 3542 /* 3543 * Called once a jremref has made it to stable store. The jremref is marked 3544 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3545 * for the jremref to complete will be awoken by free_jremref. 3546 */ 3547 static void 3548 handle_written_jremref(jremref) 3549 struct jremref *jremref; 3550 { 3551 struct inodedep *inodedep; 3552 struct jsegdep *jsegdep; 3553 struct dirrem *dirrem; 3554 3555 /* Grab the jsegdep. */ 3556 jsegdep = inoref_jseg(&jremref->jr_ref); 3557 /* 3558 * Remove us from the inoref list. 3559 */ 3560 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3561 0, &inodedep) == 0) 3562 panic("handle_written_jremref: Lost inodedep"); 3563 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3564 /* 3565 * Complete the dirrem. 3566 */ 3567 dirrem = jremref->jr_dirrem; 3568 jremref->jr_dirrem = NULL; 3569 LIST_REMOVE(jremref, jr_deps); 3570 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3571 jwork_insert(&dirrem->dm_jwork, jsegdep); 3572 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3573 (dirrem->dm_state & COMPLETE) != 0) 3574 add_to_worklist(&dirrem->dm_list, 0); 3575 free_jremref(jremref); 3576 } 3577 3578 /* 3579 * Called once a jaddref has made it to stable store. The dependency is 3580 * marked complete and any dependent structures are added to the inode 3581 * bufwait list to be completed as soon as it is written. If a bitmap write 3582 * depends on this entry we move the inode into the inodedephd of the 3583 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3584 */ 3585 static void 3586 handle_written_jaddref(jaddref) 3587 struct jaddref *jaddref; 3588 { 3589 struct jsegdep *jsegdep; 3590 struct inodedep *inodedep; 3591 struct diradd *diradd; 3592 struct mkdir *mkdir; 3593 3594 /* Grab the jsegdep. */ 3595 jsegdep = inoref_jseg(&jaddref->ja_ref); 3596 mkdir = NULL; 3597 diradd = NULL; 3598 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3599 0, &inodedep) == 0) 3600 panic("handle_written_jaddref: Lost inodedep."); 3601 if (jaddref->ja_diradd == NULL) 3602 panic("handle_written_jaddref: No dependency"); 3603 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3604 diradd = jaddref->ja_diradd; 3605 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3606 } else if (jaddref->ja_state & MKDIR_PARENT) { 3607 mkdir = jaddref->ja_mkdir; 3608 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3609 } else if (jaddref->ja_state & MKDIR_BODY) 3610 mkdir = jaddref->ja_mkdir; 3611 else 3612 panic("handle_written_jaddref: Unknown dependency %p", 3613 jaddref->ja_diradd); 3614 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3615 /* 3616 * Remove us from the inode list. 3617 */ 3618 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3619 /* 3620 * The mkdir may be waiting on the jaddref to clear before freeing. 3621 */ 3622 if (mkdir) { 3623 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3624 ("handle_written_jaddref: Incorrect type for mkdir %s", 3625 TYPENAME(mkdir->md_list.wk_type))); 3626 mkdir->md_jaddref = NULL; 3627 diradd = mkdir->md_diradd; 3628 mkdir->md_state |= DEPCOMPLETE; 3629 complete_mkdir(mkdir); 3630 } 3631 jwork_insert(&diradd->da_jwork, jsegdep); 3632 if (jaddref->ja_state & NEWBLOCK) { 3633 inodedep->id_state |= ONDEPLIST; 3634 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3635 inodedep, id_deps); 3636 } 3637 free_jaddref(jaddref); 3638 } 3639 3640 /* 3641 * Called once a jnewblk journal is written. The allocdirect or allocindir 3642 * is placed in the bmsafemap to await notification of a written bitmap. If 3643 * the operation was canceled we add the segdep to the appropriate 3644 * dependency to free the journal space once the canceling operation 3645 * completes. 3646 */ 3647 static void 3648 handle_written_jnewblk(jnewblk) 3649 struct jnewblk *jnewblk; 3650 { 3651 struct bmsafemap *bmsafemap; 3652 struct freefrag *freefrag; 3653 struct freework *freework; 3654 struct jsegdep *jsegdep; 3655 struct newblk *newblk; 3656 3657 /* Grab the jsegdep. */ 3658 jsegdep = jnewblk->jn_jsegdep; 3659 jnewblk->jn_jsegdep = NULL; 3660 if (jnewblk->jn_dep == NULL) 3661 panic("handle_written_jnewblk: No dependency for the segdep."); 3662 switch (jnewblk->jn_dep->wk_type) { 3663 case D_NEWBLK: 3664 case D_ALLOCDIRECT: 3665 case D_ALLOCINDIR: 3666 /* 3667 * Add the written block to the bmsafemap so it can 3668 * be notified when the bitmap is on disk. 3669 */ 3670 newblk = WK_NEWBLK(jnewblk->jn_dep); 3671 newblk->nb_jnewblk = NULL; 3672 if ((newblk->nb_state & GOINGAWAY) == 0) { 3673 bmsafemap = newblk->nb_bmsafemap; 3674 newblk->nb_state |= ONDEPLIST; 3675 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3676 nb_deps); 3677 } 3678 jwork_insert(&newblk->nb_jwork, jsegdep); 3679 break; 3680 case D_FREEFRAG: 3681 /* 3682 * A newblock being removed by a freefrag when replaced by 3683 * frag extension. 3684 */ 3685 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3686 freefrag->ff_jdep = NULL; 3687 jwork_insert(&freefrag->ff_jwork, jsegdep); 3688 break; 3689 case D_FREEWORK: 3690 /* 3691 * A direct block was removed by truncate. 3692 */ 3693 freework = WK_FREEWORK(jnewblk->jn_dep); 3694 freework->fw_jnewblk = NULL; 3695 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3696 break; 3697 default: 3698 panic("handle_written_jnewblk: Unknown type %d.", 3699 jnewblk->jn_dep->wk_type); 3700 } 3701 jnewblk->jn_dep = NULL; 3702 free_jnewblk(jnewblk); 3703 } 3704 3705 /* 3706 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3707 * an in-flight allocation that has not yet been committed. Divorce us 3708 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3709 * to the worklist. 3710 */ 3711 static void 3712 cancel_jfreefrag(jfreefrag) 3713 struct jfreefrag *jfreefrag; 3714 { 3715 struct freefrag *freefrag; 3716 3717 if (jfreefrag->fr_jsegdep) { 3718 free_jsegdep(jfreefrag->fr_jsegdep); 3719 jfreefrag->fr_jsegdep = NULL; 3720 } 3721 freefrag = jfreefrag->fr_freefrag; 3722 jfreefrag->fr_freefrag = NULL; 3723 free_jfreefrag(jfreefrag); 3724 freefrag->ff_state |= DEPCOMPLETE; 3725 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3726 } 3727 3728 /* 3729 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3730 */ 3731 static void 3732 free_jfreefrag(jfreefrag) 3733 struct jfreefrag *jfreefrag; 3734 { 3735 3736 if (jfreefrag->fr_state & INPROGRESS) 3737 WORKLIST_REMOVE(&jfreefrag->fr_list); 3738 else if (jfreefrag->fr_state & ONWORKLIST) 3739 remove_from_journal(&jfreefrag->fr_list); 3740 if (jfreefrag->fr_freefrag != NULL) 3741 panic("free_jfreefrag: Still attached to a freefrag."); 3742 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3743 } 3744 3745 /* 3746 * Called when the journal write for a jfreefrag completes. The parent 3747 * freefrag is added to the worklist if this completes its dependencies. 3748 */ 3749 static void 3750 handle_written_jfreefrag(jfreefrag) 3751 struct jfreefrag *jfreefrag; 3752 { 3753 struct jsegdep *jsegdep; 3754 struct freefrag *freefrag; 3755 3756 /* Grab the jsegdep. */ 3757 jsegdep = jfreefrag->fr_jsegdep; 3758 jfreefrag->fr_jsegdep = NULL; 3759 freefrag = jfreefrag->fr_freefrag; 3760 if (freefrag == NULL) 3761 panic("handle_written_jfreefrag: No freefrag."); 3762 freefrag->ff_state |= DEPCOMPLETE; 3763 freefrag->ff_jdep = NULL; 3764 jwork_insert(&freefrag->ff_jwork, jsegdep); 3765 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3766 add_to_worklist(&freefrag->ff_list, 0); 3767 jfreefrag->fr_freefrag = NULL; 3768 free_jfreefrag(jfreefrag); 3769 } 3770 3771 /* 3772 * Called when the journal write for a jfreeblk completes. The jfreeblk 3773 * is removed from the freeblks list of pending journal writes and the 3774 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3775 * have been reclaimed. 3776 */ 3777 static void 3778 handle_written_jblkdep(jblkdep) 3779 struct jblkdep *jblkdep; 3780 { 3781 struct freeblks *freeblks; 3782 struct jsegdep *jsegdep; 3783 3784 /* Grab the jsegdep. */ 3785 jsegdep = jblkdep->jb_jsegdep; 3786 jblkdep->jb_jsegdep = NULL; 3787 freeblks = jblkdep->jb_freeblks; 3788 LIST_REMOVE(jblkdep, jb_deps); 3789 jwork_insert(&freeblks->fb_jwork, jsegdep); 3790 /* 3791 * If the freeblks is all journaled, we can add it to the worklist. 3792 */ 3793 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3794 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3795 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3796 3797 free_jblkdep(jblkdep); 3798 } 3799 3800 static struct jsegdep * 3801 newjsegdep(struct worklist *wk) 3802 { 3803 struct jsegdep *jsegdep; 3804 3805 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3806 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3807 jsegdep->jd_seg = NULL; 3808 3809 return (jsegdep); 3810 } 3811 3812 static struct jmvref * 3813 newjmvref(dp, ino, oldoff, newoff) 3814 struct inode *dp; 3815 ino_t ino; 3816 off_t oldoff; 3817 off_t newoff; 3818 { 3819 struct jmvref *jmvref; 3820 3821 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3822 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump)); 3823 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 3824 jmvref->jm_parent = dp->i_number; 3825 jmvref->jm_ino = ino; 3826 jmvref->jm_oldoff = oldoff; 3827 jmvref->jm_newoff = newoff; 3828 3829 return (jmvref); 3830 } 3831 3832 /* 3833 * Allocate a new jremref that tracks the removal of ip from dp with the 3834 * directory entry offset of diroff. Mark the entry as ATTACHED and 3835 * DEPCOMPLETE as we have all the information required for the journal write 3836 * and the directory has already been removed from the buffer. The caller 3837 * is responsible for linking the jremref into the pagedep and adding it 3838 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 3839 * a DOTDOT addition so handle_workitem_remove() can properly assign 3840 * the jsegdep when we're done. 3841 */ 3842 static struct jremref * 3843 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 3844 off_t diroff, nlink_t nlink) 3845 { 3846 struct jremref *jremref; 3847 3848 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 3849 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump)); 3850 jremref->jr_state = ATTACHED; 3851 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 3852 nlink, ip->i_mode); 3853 jremref->jr_dirrem = dirrem; 3854 3855 return (jremref); 3856 } 3857 3858 static inline void 3859 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 3860 nlink_t nlink, uint16_t mode) 3861 { 3862 3863 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 3864 inoref->if_diroff = diroff; 3865 inoref->if_ino = ino; 3866 inoref->if_parent = parent; 3867 inoref->if_nlink = nlink; 3868 inoref->if_mode = mode; 3869 } 3870 3871 /* 3872 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 3873 * directory offset may not be known until later. The caller is responsible 3874 * adding the entry to the journal when this information is available. nlink 3875 * should be the link count prior to the addition and mode is only required 3876 * to have the correct FMT. 3877 */ 3878 static struct jaddref * 3879 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 3880 uint16_t mode) 3881 { 3882 struct jaddref *jaddref; 3883 3884 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 3885 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump)); 3886 jaddref->ja_state = ATTACHED; 3887 jaddref->ja_mkdir = NULL; 3888 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 3889 3890 return (jaddref); 3891 } 3892 3893 /* 3894 * Create a new free dependency for a freework. The caller is responsible 3895 * for adjusting the reference count when it has the lock held. The freedep 3896 * will track an outstanding bitmap write that will ultimately clear the 3897 * freework to continue. 3898 */ 3899 static struct freedep * 3900 newfreedep(struct freework *freework) 3901 { 3902 struct freedep *freedep; 3903 3904 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 3905 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 3906 freedep->fd_freework = freework; 3907 3908 return (freedep); 3909 } 3910 3911 /* 3912 * Free a freedep structure once the buffer it is linked to is written. If 3913 * this is the last reference to the freework schedule it for completion. 3914 */ 3915 static void 3916 free_freedep(freedep) 3917 struct freedep *freedep; 3918 { 3919 struct freework *freework; 3920 3921 freework = freedep->fd_freework; 3922 freework->fw_freeblks->fb_cgwait--; 3923 if (--freework->fw_ref == 0) 3924 freework_enqueue(freework); 3925 WORKITEM_FREE(freedep, D_FREEDEP); 3926 } 3927 3928 /* 3929 * Allocate a new freework structure that may be a level in an indirect 3930 * when parent is not NULL or a top level block when it is. The top level 3931 * freework structures are allocated without lk held and before the freeblks 3932 * is visible outside of softdep_setup_freeblocks(). 3933 */ 3934 static struct freework * 3935 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 3936 struct ufsmount *ump; 3937 struct freeblks *freeblks; 3938 struct freework *parent; 3939 ufs_lbn_t lbn; 3940 ufs2_daddr_t nb; 3941 int frags; 3942 int off; 3943 int journal; 3944 { 3945 struct freework *freework; 3946 3947 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 3948 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 3949 freework->fw_state = ATTACHED; 3950 freework->fw_jnewblk = NULL; 3951 freework->fw_freeblks = freeblks; 3952 freework->fw_parent = parent; 3953 freework->fw_lbn = lbn; 3954 freework->fw_blkno = nb; 3955 freework->fw_frags = frags; 3956 freework->fw_indir = NULL; 3957 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || lbn >= -NXADDR) 3958 ? 0 : NINDIR(ump->um_fs) + 1; 3959 freework->fw_start = freework->fw_off = off; 3960 if (journal) 3961 newjfreeblk(freeblks, lbn, nb, frags); 3962 if (parent == NULL) { 3963 ACQUIRE_LOCK(&lk); 3964 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 3965 freeblks->fb_ref++; 3966 FREE_LOCK(&lk); 3967 } 3968 3969 return (freework); 3970 } 3971 3972 /* 3973 * Eliminate a jfreeblk for a block that does not need journaling. 3974 */ 3975 static void 3976 cancel_jfreeblk(freeblks, blkno) 3977 struct freeblks *freeblks; 3978 ufs2_daddr_t blkno; 3979 { 3980 struct jfreeblk *jfreeblk; 3981 struct jblkdep *jblkdep; 3982 3983 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 3984 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 3985 continue; 3986 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 3987 if (jfreeblk->jf_blkno == blkno) 3988 break; 3989 } 3990 if (jblkdep == NULL) 3991 return; 3992 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 3993 free_jsegdep(jblkdep->jb_jsegdep); 3994 LIST_REMOVE(jblkdep, jb_deps); 3995 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 3996 } 3997 3998 /* 3999 * Allocate a new jfreeblk to journal top level block pointer when truncating 4000 * a file. The caller must add this to the worklist when lk is held. 4001 */ 4002 static struct jfreeblk * 4003 newjfreeblk(freeblks, lbn, blkno, frags) 4004 struct freeblks *freeblks; 4005 ufs_lbn_t lbn; 4006 ufs2_daddr_t blkno; 4007 int frags; 4008 { 4009 struct jfreeblk *jfreeblk; 4010 4011 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4012 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4013 freeblks->fb_list.wk_mp); 4014 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4015 jfreeblk->jf_dep.jb_freeblks = freeblks; 4016 jfreeblk->jf_ino = freeblks->fb_inum; 4017 jfreeblk->jf_lbn = lbn; 4018 jfreeblk->jf_blkno = blkno; 4019 jfreeblk->jf_frags = frags; 4020 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4021 4022 return (jfreeblk); 4023 } 4024 4025 /* 4026 * Allocate a new jtrunc to track a partial truncation. 4027 */ 4028 static struct jtrunc * 4029 newjtrunc(freeblks, size, extsize) 4030 struct freeblks *freeblks; 4031 off_t size; 4032 int extsize; 4033 { 4034 struct jtrunc *jtrunc; 4035 4036 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4037 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4038 freeblks->fb_list.wk_mp); 4039 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4040 jtrunc->jt_dep.jb_freeblks = freeblks; 4041 jtrunc->jt_ino = freeblks->fb_inum; 4042 jtrunc->jt_size = size; 4043 jtrunc->jt_extsize = extsize; 4044 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4045 4046 return (jtrunc); 4047 } 4048 4049 /* 4050 * If we're canceling a new bitmap we have to search for another ref 4051 * to move into the bmsafemap dep. This might be better expressed 4052 * with another structure. 4053 */ 4054 static void 4055 move_newblock_dep(jaddref, inodedep) 4056 struct jaddref *jaddref; 4057 struct inodedep *inodedep; 4058 { 4059 struct inoref *inoref; 4060 struct jaddref *jaddrefn; 4061 4062 jaddrefn = NULL; 4063 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4064 inoref = TAILQ_NEXT(inoref, if_deps)) { 4065 if ((jaddref->ja_state & NEWBLOCK) && 4066 inoref->if_list.wk_type == D_JADDREF) { 4067 jaddrefn = (struct jaddref *)inoref; 4068 break; 4069 } 4070 } 4071 if (jaddrefn == NULL) 4072 return; 4073 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4074 jaddrefn->ja_state |= jaddref->ja_state & 4075 (ATTACHED | UNDONE | NEWBLOCK); 4076 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4077 jaddref->ja_state |= ATTACHED; 4078 LIST_REMOVE(jaddref, ja_bmdeps); 4079 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4080 ja_bmdeps); 4081 } 4082 4083 /* 4084 * Cancel a jaddref either before it has been written or while it is being 4085 * written. This happens when a link is removed before the add reaches 4086 * the disk. The jaddref dependency is kept linked into the bmsafemap 4087 * and inode to prevent the link count or bitmap from reaching the disk 4088 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4089 * required. 4090 * 4091 * Returns 1 if the canceled addref requires journaling of the remove and 4092 * 0 otherwise. 4093 */ 4094 static int 4095 cancel_jaddref(jaddref, inodedep, wkhd) 4096 struct jaddref *jaddref; 4097 struct inodedep *inodedep; 4098 struct workhead *wkhd; 4099 { 4100 struct inoref *inoref; 4101 struct jsegdep *jsegdep; 4102 int needsj; 4103 4104 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4105 ("cancel_jaddref: Canceling complete jaddref")); 4106 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4107 needsj = 1; 4108 else 4109 needsj = 0; 4110 if (inodedep == NULL) 4111 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4112 0, &inodedep) == 0) 4113 panic("cancel_jaddref: Lost inodedep"); 4114 /* 4115 * We must adjust the nlink of any reference operation that follows 4116 * us so that it is consistent with the in-memory reference. This 4117 * ensures that inode nlink rollbacks always have the correct link. 4118 */ 4119 if (needsj == 0) { 4120 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4121 inoref = TAILQ_NEXT(inoref, if_deps)) { 4122 if (inoref->if_state & GOINGAWAY) 4123 break; 4124 inoref->if_nlink--; 4125 } 4126 } 4127 jsegdep = inoref_jseg(&jaddref->ja_ref); 4128 if (jaddref->ja_state & NEWBLOCK) 4129 move_newblock_dep(jaddref, inodedep); 4130 wake_worklist(&jaddref->ja_list); 4131 jaddref->ja_mkdir = NULL; 4132 if (jaddref->ja_state & INPROGRESS) { 4133 jaddref->ja_state &= ~INPROGRESS; 4134 WORKLIST_REMOVE(&jaddref->ja_list); 4135 jwork_insert(wkhd, jsegdep); 4136 } else { 4137 free_jsegdep(jsegdep); 4138 if (jaddref->ja_state & DEPCOMPLETE) 4139 remove_from_journal(&jaddref->ja_list); 4140 } 4141 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4142 /* 4143 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4144 * can arrange for them to be freed with the bitmap. Otherwise we 4145 * no longer need this addref attached to the inoreflst and it 4146 * will incorrectly adjust nlink if we leave it. 4147 */ 4148 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4149 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4150 if_deps); 4151 jaddref->ja_state |= COMPLETE; 4152 free_jaddref(jaddref); 4153 return (needsj); 4154 } 4155 /* 4156 * Leave the head of the list for jsegdeps for fast merging. 4157 */ 4158 if (LIST_FIRST(wkhd) != NULL) { 4159 jaddref->ja_state |= ONWORKLIST; 4160 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4161 } else 4162 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4163 4164 return (needsj); 4165 } 4166 4167 /* 4168 * Attempt to free a jaddref structure when some work completes. This 4169 * should only succeed once the entry is written and all dependencies have 4170 * been notified. 4171 */ 4172 static void 4173 free_jaddref(jaddref) 4174 struct jaddref *jaddref; 4175 { 4176 4177 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4178 return; 4179 if (jaddref->ja_ref.if_jsegdep) 4180 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4181 jaddref, jaddref->ja_state); 4182 if (jaddref->ja_state & NEWBLOCK) 4183 LIST_REMOVE(jaddref, ja_bmdeps); 4184 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4185 panic("free_jaddref: Bad state %p(0x%X)", 4186 jaddref, jaddref->ja_state); 4187 if (jaddref->ja_mkdir != NULL) 4188 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4189 WORKITEM_FREE(jaddref, D_JADDREF); 4190 } 4191 4192 /* 4193 * Free a jremref structure once it has been written or discarded. 4194 */ 4195 static void 4196 free_jremref(jremref) 4197 struct jremref *jremref; 4198 { 4199 4200 if (jremref->jr_ref.if_jsegdep) 4201 free_jsegdep(jremref->jr_ref.if_jsegdep); 4202 if (jremref->jr_state & INPROGRESS) 4203 panic("free_jremref: IO still pending"); 4204 WORKITEM_FREE(jremref, D_JREMREF); 4205 } 4206 4207 /* 4208 * Free a jnewblk structure. 4209 */ 4210 static void 4211 free_jnewblk(jnewblk) 4212 struct jnewblk *jnewblk; 4213 { 4214 4215 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4216 return; 4217 LIST_REMOVE(jnewblk, jn_deps); 4218 if (jnewblk->jn_dep != NULL) 4219 panic("free_jnewblk: Dependency still attached."); 4220 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4221 } 4222 4223 /* 4224 * Cancel a jnewblk which has been been made redundant by frag extension. 4225 */ 4226 static void 4227 cancel_jnewblk(jnewblk, wkhd) 4228 struct jnewblk *jnewblk; 4229 struct workhead *wkhd; 4230 { 4231 struct jsegdep *jsegdep; 4232 4233 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4234 jsegdep = jnewblk->jn_jsegdep; 4235 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4236 panic("cancel_jnewblk: Invalid state"); 4237 jnewblk->jn_jsegdep = NULL; 4238 jnewblk->jn_dep = NULL; 4239 jnewblk->jn_state |= GOINGAWAY; 4240 if (jnewblk->jn_state & INPROGRESS) { 4241 jnewblk->jn_state &= ~INPROGRESS; 4242 WORKLIST_REMOVE(&jnewblk->jn_list); 4243 jwork_insert(wkhd, jsegdep); 4244 } else { 4245 free_jsegdep(jsegdep); 4246 remove_from_journal(&jnewblk->jn_list); 4247 } 4248 wake_worklist(&jnewblk->jn_list); 4249 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4250 } 4251 4252 static void 4253 free_jblkdep(jblkdep) 4254 struct jblkdep *jblkdep; 4255 { 4256 4257 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4258 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4259 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4260 WORKITEM_FREE(jblkdep, D_JTRUNC); 4261 else 4262 panic("free_jblkdep: Unexpected type %s", 4263 TYPENAME(jblkdep->jb_list.wk_type)); 4264 } 4265 4266 /* 4267 * Free a single jseg once it is no longer referenced in memory or on 4268 * disk. Reclaim journal blocks and dependencies waiting for the segment 4269 * to disappear. 4270 */ 4271 static void 4272 free_jseg(jseg, jblocks) 4273 struct jseg *jseg; 4274 struct jblocks *jblocks; 4275 { 4276 struct freework *freework; 4277 4278 /* 4279 * Free freework structures that were lingering to indicate freed 4280 * indirect blocks that forced journal write ordering on reallocate. 4281 */ 4282 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4283 indirblk_remove(freework); 4284 if (jblocks->jb_oldestseg == jseg) 4285 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4286 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4287 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4288 KASSERT(LIST_EMPTY(&jseg->js_entries), 4289 ("free_jseg: Freed jseg has valid entries.")); 4290 WORKITEM_FREE(jseg, D_JSEG); 4291 } 4292 4293 /* 4294 * Free all jsegs that meet the criteria for being reclaimed and update 4295 * oldestseg. 4296 */ 4297 static void 4298 free_jsegs(jblocks) 4299 struct jblocks *jblocks; 4300 { 4301 struct jseg *jseg; 4302 4303 /* 4304 * Free only those jsegs which have none allocated before them to 4305 * preserve the journal space ordering. 4306 */ 4307 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4308 /* 4309 * Only reclaim space when nothing depends on this journal 4310 * set and another set has written that it is no longer 4311 * valid. 4312 */ 4313 if (jseg->js_refs != 0) { 4314 jblocks->jb_oldestseg = jseg; 4315 return; 4316 } 4317 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4318 break; 4319 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4320 break; 4321 /* 4322 * We can free jsegs that didn't write entries when 4323 * oldestwrseq == js_seq. 4324 */ 4325 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4326 jseg->js_cnt != 0) 4327 break; 4328 free_jseg(jseg, jblocks); 4329 } 4330 /* 4331 * If we exited the loop above we still must discover the 4332 * oldest valid segment. 4333 */ 4334 if (jseg) 4335 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4336 jseg = TAILQ_NEXT(jseg, js_next)) 4337 if (jseg->js_refs != 0) 4338 break; 4339 jblocks->jb_oldestseg = jseg; 4340 /* 4341 * The journal has no valid records but some jsegs may still be 4342 * waiting on oldestwrseq to advance. We force a small record 4343 * out to permit these lingering records to be reclaimed. 4344 */ 4345 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4346 jblocks->jb_needseg = 1; 4347 } 4348 4349 /* 4350 * Release one reference to a jseg and free it if the count reaches 0. This 4351 * should eventually reclaim journal space as well. 4352 */ 4353 static void 4354 rele_jseg(jseg) 4355 struct jseg *jseg; 4356 { 4357 4358 KASSERT(jseg->js_refs > 0, 4359 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4360 if (--jseg->js_refs != 0) 4361 return; 4362 free_jsegs(jseg->js_jblocks); 4363 } 4364 4365 /* 4366 * Release a jsegdep and decrement the jseg count. 4367 */ 4368 static void 4369 free_jsegdep(jsegdep) 4370 struct jsegdep *jsegdep; 4371 { 4372 4373 if (jsegdep->jd_seg) 4374 rele_jseg(jsegdep->jd_seg); 4375 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4376 } 4377 4378 /* 4379 * Wait for a journal item to make it to disk. Initiate journal processing 4380 * if required. 4381 */ 4382 static int 4383 jwait(wk, waitfor) 4384 struct worklist *wk; 4385 int waitfor; 4386 { 4387 4388 /* 4389 * Blocking journal waits cause slow synchronous behavior. Record 4390 * stats on the frequency of these blocking operations. 4391 */ 4392 if (waitfor == MNT_WAIT) { 4393 stat_journal_wait++; 4394 switch (wk->wk_type) { 4395 case D_JREMREF: 4396 case D_JMVREF: 4397 stat_jwait_filepage++; 4398 break; 4399 case D_JTRUNC: 4400 case D_JFREEBLK: 4401 stat_jwait_freeblks++; 4402 break; 4403 case D_JNEWBLK: 4404 stat_jwait_newblk++; 4405 break; 4406 case D_JADDREF: 4407 stat_jwait_inode++; 4408 break; 4409 default: 4410 break; 4411 } 4412 } 4413 /* 4414 * If IO has not started we process the journal. We can't mark the 4415 * worklist item as IOWAITING because we drop the lock while 4416 * processing the journal and the worklist entry may be freed after 4417 * this point. The caller may call back in and re-issue the request. 4418 */ 4419 if ((wk->wk_state & INPROGRESS) == 0) { 4420 softdep_process_journal(wk->wk_mp, wk, waitfor); 4421 if (waitfor != MNT_WAIT) 4422 return (EBUSY); 4423 return (0); 4424 } 4425 if (waitfor != MNT_WAIT) 4426 return (EBUSY); 4427 wait_worklist(wk, "jwait"); 4428 return (0); 4429 } 4430 4431 /* 4432 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4433 * appropriate. This is a convenience function to reduce duplicate code 4434 * for the setup and revert functions below. 4435 */ 4436 static struct inodedep * 4437 inodedep_lookup_ip(ip) 4438 struct inode *ip; 4439 { 4440 struct inodedep *inodedep; 4441 int dflags; 4442 4443 KASSERT(ip->i_nlink >= ip->i_effnlink, 4444 ("inodedep_lookup_ip: bad delta")); 4445 dflags = DEPALLOC; 4446 if (IS_SNAPSHOT(ip)) 4447 dflags |= NODELAY; 4448 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, 4449 &inodedep); 4450 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4451 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4452 4453 return (inodedep); 4454 } 4455 4456 /* 4457 * Called prior to creating a new inode and linking it to a directory. The 4458 * jaddref structure must already be allocated by softdep_setup_inomapdep 4459 * and it is discovered here so we can initialize the mode and update 4460 * nlinkdelta. 4461 */ 4462 void 4463 softdep_setup_create(dp, ip) 4464 struct inode *dp; 4465 struct inode *ip; 4466 { 4467 struct inodedep *inodedep; 4468 struct jaddref *jaddref; 4469 struct vnode *dvp; 4470 4471 KASSERT(ip->i_nlink == 1, 4472 ("softdep_setup_create: Invalid link count.")); 4473 dvp = ITOV(dp); 4474 ACQUIRE_LOCK(&lk); 4475 inodedep = inodedep_lookup_ip(ip); 4476 if (DOINGSUJ(dvp)) { 4477 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4478 inoreflst); 4479 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4480 ("softdep_setup_create: No addref structure present.")); 4481 } 4482 softdep_prelink(dvp, NULL); 4483 FREE_LOCK(&lk); 4484 } 4485 4486 /* 4487 * Create a jaddref structure to track the addition of a DOTDOT link when 4488 * we are reparenting an inode as part of a rename. This jaddref will be 4489 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4490 * non-journaling softdep. 4491 */ 4492 void 4493 softdep_setup_dotdot_link(dp, ip) 4494 struct inode *dp; 4495 struct inode *ip; 4496 { 4497 struct inodedep *inodedep; 4498 struct jaddref *jaddref; 4499 struct vnode *dvp; 4500 struct vnode *vp; 4501 4502 dvp = ITOV(dp); 4503 vp = ITOV(ip); 4504 jaddref = NULL; 4505 /* 4506 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4507 * is used as a normal link would be. 4508 */ 4509 if (DOINGSUJ(dvp)) 4510 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4511 dp->i_effnlink - 1, dp->i_mode); 4512 ACQUIRE_LOCK(&lk); 4513 inodedep = inodedep_lookup_ip(dp); 4514 if (jaddref) 4515 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4516 if_deps); 4517 softdep_prelink(dvp, ITOV(ip)); 4518 FREE_LOCK(&lk); 4519 } 4520 4521 /* 4522 * Create a jaddref structure to track a new link to an inode. The directory 4523 * offset is not known until softdep_setup_directory_add or 4524 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4525 * softdep. 4526 */ 4527 void 4528 softdep_setup_link(dp, ip) 4529 struct inode *dp; 4530 struct inode *ip; 4531 { 4532 struct inodedep *inodedep; 4533 struct jaddref *jaddref; 4534 struct vnode *dvp; 4535 4536 dvp = ITOV(dp); 4537 jaddref = NULL; 4538 if (DOINGSUJ(dvp)) 4539 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4540 ip->i_mode); 4541 ACQUIRE_LOCK(&lk); 4542 inodedep = inodedep_lookup_ip(ip); 4543 if (jaddref) 4544 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4545 if_deps); 4546 softdep_prelink(dvp, ITOV(ip)); 4547 FREE_LOCK(&lk); 4548 } 4549 4550 /* 4551 * Called to create the jaddref structures to track . and .. references as 4552 * well as lookup and further initialize the incomplete jaddref created 4553 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4554 * nlinkdelta for non-journaling softdep. 4555 */ 4556 void 4557 softdep_setup_mkdir(dp, ip) 4558 struct inode *dp; 4559 struct inode *ip; 4560 { 4561 struct inodedep *inodedep; 4562 struct jaddref *dotdotaddref; 4563 struct jaddref *dotaddref; 4564 struct jaddref *jaddref; 4565 struct vnode *dvp; 4566 4567 dvp = ITOV(dp); 4568 dotaddref = dotdotaddref = NULL; 4569 if (DOINGSUJ(dvp)) { 4570 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4571 ip->i_mode); 4572 dotaddref->ja_state |= MKDIR_BODY; 4573 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4574 dp->i_effnlink - 1, dp->i_mode); 4575 dotdotaddref->ja_state |= MKDIR_PARENT; 4576 } 4577 ACQUIRE_LOCK(&lk); 4578 inodedep = inodedep_lookup_ip(ip); 4579 if (DOINGSUJ(dvp)) { 4580 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4581 inoreflst); 4582 KASSERT(jaddref != NULL, 4583 ("softdep_setup_mkdir: No addref structure present.")); 4584 KASSERT(jaddref->ja_parent == dp->i_number, 4585 ("softdep_setup_mkdir: bad parent %ju", 4586 (uintmax_t)jaddref->ja_parent)); 4587 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4588 if_deps); 4589 } 4590 inodedep = inodedep_lookup_ip(dp); 4591 if (DOINGSUJ(dvp)) 4592 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4593 &dotdotaddref->ja_ref, if_deps); 4594 softdep_prelink(ITOV(dp), NULL); 4595 FREE_LOCK(&lk); 4596 } 4597 4598 /* 4599 * Called to track nlinkdelta of the inode and parent directories prior to 4600 * unlinking a directory. 4601 */ 4602 void 4603 softdep_setup_rmdir(dp, ip) 4604 struct inode *dp; 4605 struct inode *ip; 4606 { 4607 struct vnode *dvp; 4608 4609 dvp = ITOV(dp); 4610 ACQUIRE_LOCK(&lk); 4611 (void) inodedep_lookup_ip(ip); 4612 (void) inodedep_lookup_ip(dp); 4613 softdep_prelink(dvp, ITOV(ip)); 4614 FREE_LOCK(&lk); 4615 } 4616 4617 /* 4618 * Called to track nlinkdelta of the inode and parent directories prior to 4619 * unlink. 4620 */ 4621 void 4622 softdep_setup_unlink(dp, ip) 4623 struct inode *dp; 4624 struct inode *ip; 4625 { 4626 struct vnode *dvp; 4627 4628 dvp = ITOV(dp); 4629 ACQUIRE_LOCK(&lk); 4630 (void) inodedep_lookup_ip(ip); 4631 (void) inodedep_lookup_ip(dp); 4632 softdep_prelink(dvp, ITOV(ip)); 4633 FREE_LOCK(&lk); 4634 } 4635 4636 /* 4637 * Called to release the journal structures created by a failed non-directory 4638 * creation. Adjusts nlinkdelta for non-journaling softdep. 4639 */ 4640 void 4641 softdep_revert_create(dp, ip) 4642 struct inode *dp; 4643 struct inode *ip; 4644 { 4645 struct inodedep *inodedep; 4646 struct jaddref *jaddref; 4647 struct vnode *dvp; 4648 4649 dvp = ITOV(dp); 4650 ACQUIRE_LOCK(&lk); 4651 inodedep = inodedep_lookup_ip(ip); 4652 if (DOINGSUJ(dvp)) { 4653 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4654 inoreflst); 4655 KASSERT(jaddref->ja_parent == dp->i_number, 4656 ("softdep_revert_create: addref parent mismatch")); 4657 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4658 } 4659 FREE_LOCK(&lk); 4660 } 4661 4662 /* 4663 * Called to release the journal structures created by a failed dotdot link 4664 * creation. Adjusts nlinkdelta for non-journaling softdep. 4665 */ 4666 void 4667 softdep_revert_dotdot_link(dp, ip) 4668 struct inode *dp; 4669 struct inode *ip; 4670 { 4671 struct inodedep *inodedep; 4672 struct jaddref *jaddref; 4673 struct vnode *dvp; 4674 4675 dvp = ITOV(dp); 4676 ACQUIRE_LOCK(&lk); 4677 inodedep = inodedep_lookup_ip(dp); 4678 if (DOINGSUJ(dvp)) { 4679 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4680 inoreflst); 4681 KASSERT(jaddref->ja_parent == ip->i_number, 4682 ("softdep_revert_dotdot_link: addref parent mismatch")); 4683 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4684 } 4685 FREE_LOCK(&lk); 4686 } 4687 4688 /* 4689 * Called to release the journal structures created by a failed link 4690 * addition. Adjusts nlinkdelta for non-journaling softdep. 4691 */ 4692 void 4693 softdep_revert_link(dp, ip) 4694 struct inode *dp; 4695 struct inode *ip; 4696 { 4697 struct inodedep *inodedep; 4698 struct jaddref *jaddref; 4699 struct vnode *dvp; 4700 4701 dvp = ITOV(dp); 4702 ACQUIRE_LOCK(&lk); 4703 inodedep = inodedep_lookup_ip(ip); 4704 if (DOINGSUJ(dvp)) { 4705 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4706 inoreflst); 4707 KASSERT(jaddref->ja_parent == dp->i_number, 4708 ("softdep_revert_link: addref parent mismatch")); 4709 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4710 } 4711 FREE_LOCK(&lk); 4712 } 4713 4714 /* 4715 * Called to release the journal structures created by a failed mkdir 4716 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4717 */ 4718 void 4719 softdep_revert_mkdir(dp, ip) 4720 struct inode *dp; 4721 struct inode *ip; 4722 { 4723 struct inodedep *inodedep; 4724 struct jaddref *jaddref; 4725 struct jaddref *dotaddref; 4726 struct vnode *dvp; 4727 4728 dvp = ITOV(dp); 4729 4730 ACQUIRE_LOCK(&lk); 4731 inodedep = inodedep_lookup_ip(dp); 4732 if (DOINGSUJ(dvp)) { 4733 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4734 inoreflst); 4735 KASSERT(jaddref->ja_parent == ip->i_number, 4736 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4737 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4738 } 4739 inodedep = inodedep_lookup_ip(ip); 4740 if (DOINGSUJ(dvp)) { 4741 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4742 inoreflst); 4743 KASSERT(jaddref->ja_parent == dp->i_number, 4744 ("softdep_revert_mkdir: addref parent mismatch")); 4745 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4746 inoreflst, if_deps); 4747 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4748 KASSERT(dotaddref->ja_parent == ip->i_number, 4749 ("softdep_revert_mkdir: dot addref parent mismatch")); 4750 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4751 } 4752 FREE_LOCK(&lk); 4753 } 4754 4755 /* 4756 * Called to correct nlinkdelta after a failed rmdir. 4757 */ 4758 void 4759 softdep_revert_rmdir(dp, ip) 4760 struct inode *dp; 4761 struct inode *ip; 4762 { 4763 4764 ACQUIRE_LOCK(&lk); 4765 (void) inodedep_lookup_ip(ip); 4766 (void) inodedep_lookup_ip(dp); 4767 FREE_LOCK(&lk); 4768 } 4769 4770 /* 4771 * Protecting the freemaps (or bitmaps). 4772 * 4773 * To eliminate the need to execute fsck before mounting a filesystem 4774 * after a power failure, one must (conservatively) guarantee that the 4775 * on-disk copy of the bitmaps never indicate that a live inode or block is 4776 * free. So, when a block or inode is allocated, the bitmap should be 4777 * updated (on disk) before any new pointers. When a block or inode is 4778 * freed, the bitmap should not be updated until all pointers have been 4779 * reset. The latter dependency is handled by the delayed de-allocation 4780 * approach described below for block and inode de-allocation. The former 4781 * dependency is handled by calling the following procedure when a block or 4782 * inode is allocated. When an inode is allocated an "inodedep" is created 4783 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4784 * Each "inodedep" is also inserted into the hash indexing structure so 4785 * that any additional link additions can be made dependent on the inode 4786 * allocation. 4787 * 4788 * The ufs filesystem maintains a number of free block counts (e.g., per 4789 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4790 * in addition to the bitmaps. These counts are used to improve efficiency 4791 * during allocation and therefore must be consistent with the bitmaps. 4792 * There is no convenient way to guarantee post-crash consistency of these 4793 * counts with simple update ordering, for two main reasons: (1) The counts 4794 * and bitmaps for a single cylinder group block are not in the same disk 4795 * sector. If a disk write is interrupted (e.g., by power failure), one may 4796 * be written and the other not. (2) Some of the counts are located in the 4797 * superblock rather than the cylinder group block. So, we focus our soft 4798 * updates implementation on protecting the bitmaps. When mounting a 4799 * filesystem, we recompute the auxiliary counts from the bitmaps. 4800 */ 4801 4802 /* 4803 * Called just after updating the cylinder group block to allocate an inode. 4804 */ 4805 void 4806 softdep_setup_inomapdep(bp, ip, newinum, mode) 4807 struct buf *bp; /* buffer for cylgroup block with inode map */ 4808 struct inode *ip; /* inode related to allocation */ 4809 ino_t newinum; /* new inode number being allocated */ 4810 int mode; 4811 { 4812 struct inodedep *inodedep; 4813 struct bmsafemap *bmsafemap; 4814 struct jaddref *jaddref; 4815 struct mount *mp; 4816 struct fs *fs; 4817 4818 mp = UFSTOVFS(ip->i_ump); 4819 fs = ip->i_ump->um_fs; 4820 jaddref = NULL; 4821 4822 /* 4823 * Allocate the journal reference add structure so that the bitmap 4824 * can be dependent on it. 4825 */ 4826 if (MOUNTEDSUJ(mp)) { 4827 jaddref = newjaddref(ip, newinum, 0, 0, mode); 4828 jaddref->ja_state |= NEWBLOCK; 4829 } 4830 4831 /* 4832 * Create a dependency for the newly allocated inode. 4833 * Panic if it already exists as something is seriously wrong. 4834 * Otherwise add it to the dependency list for the buffer holding 4835 * the cylinder group map from which it was allocated. 4836 * 4837 * We have to preallocate a bmsafemap entry in case it is needed 4838 * in bmsafemap_lookup since once we allocate the inodedep, we 4839 * have to finish initializing it before we can FREE_LOCK(). 4840 * By preallocating, we avoid FREE_LOCK() while doing a malloc 4841 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 4842 * creating the inodedep as it can be freed during the time 4843 * that we FREE_LOCK() while allocating the inodedep. We must 4844 * call workitem_alloc() before entering the locked section as 4845 * it also acquires the lock and we must avoid trying doing so 4846 * recursively. 4847 */ 4848 bmsafemap = malloc(sizeof(struct bmsafemap), 4849 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 4850 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 4851 ACQUIRE_LOCK(&lk); 4852 if ((inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep))) 4853 panic("softdep_setup_inomapdep: dependency %p for new" 4854 "inode already exists", inodedep); 4855 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 4856 if (jaddref) { 4857 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 4858 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4859 if_deps); 4860 } else { 4861 inodedep->id_state |= ONDEPLIST; 4862 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 4863 } 4864 inodedep->id_bmsafemap = bmsafemap; 4865 inodedep->id_state &= ~DEPCOMPLETE; 4866 FREE_LOCK(&lk); 4867 } 4868 4869 /* 4870 * Called just after updating the cylinder group block to 4871 * allocate block or fragment. 4872 */ 4873 void 4874 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 4875 struct buf *bp; /* buffer for cylgroup block with block map */ 4876 struct mount *mp; /* filesystem doing allocation */ 4877 ufs2_daddr_t newblkno; /* number of newly allocated block */ 4878 int frags; /* Number of fragments. */ 4879 int oldfrags; /* Previous number of fragments for extend. */ 4880 { 4881 struct newblk *newblk; 4882 struct bmsafemap *bmsafemap; 4883 struct jnewblk *jnewblk; 4884 struct fs *fs; 4885 4886 fs = VFSTOUFS(mp)->um_fs; 4887 jnewblk = NULL; 4888 /* 4889 * Create a dependency for the newly allocated block. 4890 * Add it to the dependency list for the buffer holding 4891 * the cylinder group map from which it was allocated. 4892 */ 4893 if (MOUNTEDSUJ(mp)) { 4894 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 4895 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 4896 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 4897 jnewblk->jn_state = ATTACHED; 4898 jnewblk->jn_blkno = newblkno; 4899 jnewblk->jn_frags = frags; 4900 jnewblk->jn_oldfrags = oldfrags; 4901 #ifdef SUJ_DEBUG 4902 { 4903 struct cg *cgp; 4904 uint8_t *blksfree; 4905 long bno; 4906 int i; 4907 4908 cgp = (struct cg *)bp->b_data; 4909 blksfree = cg_blksfree(cgp); 4910 bno = dtogd(fs, jnewblk->jn_blkno); 4911 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 4912 i++) { 4913 if (isset(blksfree, bno + i)) 4914 panic("softdep_setup_blkmapdep: " 4915 "free fragment %d from %d-%d " 4916 "state 0x%X dep %p", i, 4917 jnewblk->jn_oldfrags, 4918 jnewblk->jn_frags, 4919 jnewblk->jn_state, 4920 jnewblk->jn_dep); 4921 } 4922 } 4923 #endif 4924 } 4925 4926 CTR3(KTR_SUJ, 4927 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 4928 newblkno, frags, oldfrags); 4929 ACQUIRE_LOCK(&lk); 4930 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 4931 panic("softdep_setup_blkmapdep: found block"); 4932 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 4933 dtog(fs, newblkno), NULL); 4934 if (jnewblk) { 4935 jnewblk->jn_dep = (struct worklist *)newblk; 4936 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 4937 } else { 4938 newblk->nb_state |= ONDEPLIST; 4939 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 4940 } 4941 newblk->nb_bmsafemap = bmsafemap; 4942 newblk->nb_jnewblk = jnewblk; 4943 FREE_LOCK(&lk); 4944 } 4945 4946 #define BMSAFEMAP_HASH(fs, cg) \ 4947 (&bmsafemap_hashtbl[((((register_t)(fs)) >> 13) + (cg)) & bmsafemap_hash]) 4948 4949 static int 4950 bmsafemap_find(bmsafemaphd, mp, cg, bmsafemapp) 4951 struct bmsafemap_hashhead *bmsafemaphd; 4952 struct mount *mp; 4953 int cg; 4954 struct bmsafemap **bmsafemapp; 4955 { 4956 struct bmsafemap *bmsafemap; 4957 4958 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 4959 if (bmsafemap->sm_list.wk_mp == mp && bmsafemap->sm_cg == cg) 4960 break; 4961 if (bmsafemap) { 4962 *bmsafemapp = bmsafemap; 4963 return (1); 4964 } 4965 *bmsafemapp = NULL; 4966 4967 return (0); 4968 } 4969 4970 /* 4971 * Find the bmsafemap associated with a cylinder group buffer. 4972 * If none exists, create one. The buffer must be locked when 4973 * this routine is called and this routine must be called with 4974 * the softdep lock held. To avoid giving up the lock while 4975 * allocating a new bmsafemap, a preallocated bmsafemap may be 4976 * provided. If it is provided but not needed, it is freed. 4977 */ 4978 static struct bmsafemap * 4979 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 4980 struct mount *mp; 4981 struct buf *bp; 4982 int cg; 4983 struct bmsafemap *newbmsafemap; 4984 { 4985 struct bmsafemap_hashhead *bmsafemaphd; 4986 struct bmsafemap *bmsafemap, *collision; 4987 struct worklist *wk; 4988 struct fs *fs; 4989 4990 rw_assert(&lk, RA_WLOCKED); 4991 KASSERT(bp != NULL, ("bmsafemap_lookup: missing buffer")); 4992 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 4993 if (wk->wk_type == D_BMSAFEMAP) { 4994 if (newbmsafemap) 4995 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 4996 return (WK_BMSAFEMAP(wk)); 4997 } 4998 } 4999 fs = VFSTOUFS(mp)->um_fs; 5000 bmsafemaphd = BMSAFEMAP_HASH(fs, cg); 5001 if (bmsafemap_find(bmsafemaphd, mp, cg, &bmsafemap) == 1) { 5002 if (newbmsafemap) 5003 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5004 return (bmsafemap); 5005 } 5006 if (newbmsafemap) { 5007 bmsafemap = newbmsafemap; 5008 } else { 5009 FREE_LOCK(&lk); 5010 bmsafemap = malloc(sizeof(struct bmsafemap), 5011 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5012 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5013 ACQUIRE_LOCK(&lk); 5014 } 5015 bmsafemap->sm_buf = bp; 5016 LIST_INIT(&bmsafemap->sm_inodedephd); 5017 LIST_INIT(&bmsafemap->sm_inodedepwr); 5018 LIST_INIT(&bmsafemap->sm_newblkhd); 5019 LIST_INIT(&bmsafemap->sm_newblkwr); 5020 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5021 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5022 LIST_INIT(&bmsafemap->sm_freehd); 5023 LIST_INIT(&bmsafemap->sm_freewr); 5024 if (bmsafemap_find(bmsafemaphd, mp, cg, &collision) == 1) { 5025 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5026 return (collision); 5027 } 5028 bmsafemap->sm_cg = cg; 5029 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5030 LIST_INSERT_HEAD(&VFSTOUFS(mp)->softdep_dirtycg, bmsafemap, sm_next); 5031 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5032 return (bmsafemap); 5033 } 5034 5035 /* 5036 * Direct block allocation dependencies. 5037 * 5038 * When a new block is allocated, the corresponding disk locations must be 5039 * initialized (with zeros or new data) before the on-disk inode points to 5040 * them. Also, the freemap from which the block was allocated must be 5041 * updated (on disk) before the inode's pointer. These two dependencies are 5042 * independent of each other and are needed for all file blocks and indirect 5043 * blocks that are pointed to directly by the inode. Just before the 5044 * "in-core" version of the inode is updated with a newly allocated block 5045 * number, a procedure (below) is called to setup allocation dependency 5046 * structures. These structures are removed when the corresponding 5047 * dependencies are satisfied or when the block allocation becomes obsolete 5048 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5049 * fragment that gets upgraded). All of these cases are handled in 5050 * procedures described later. 5051 * 5052 * When a file extension causes a fragment to be upgraded, either to a larger 5053 * fragment or to a full block, the on-disk location may change (if the 5054 * previous fragment could not simply be extended). In this case, the old 5055 * fragment must be de-allocated, but not until after the inode's pointer has 5056 * been updated. In most cases, this is handled by later procedures, which 5057 * will construct a "freefrag" structure to be added to the workitem queue 5058 * when the inode update is complete (or obsolete). The main exception to 5059 * this is when an allocation occurs while a pending allocation dependency 5060 * (for the same block pointer) remains. This case is handled in the main 5061 * allocation dependency setup procedure by immediately freeing the 5062 * unreferenced fragments. 5063 */ 5064 void 5065 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5066 struct inode *ip; /* inode to which block is being added */ 5067 ufs_lbn_t off; /* block pointer within inode */ 5068 ufs2_daddr_t newblkno; /* disk block number being added */ 5069 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5070 long newsize; /* size of new block */ 5071 long oldsize; /* size of new block */ 5072 struct buf *bp; /* bp for allocated block */ 5073 { 5074 struct allocdirect *adp, *oldadp; 5075 struct allocdirectlst *adphead; 5076 struct freefrag *freefrag; 5077 struct inodedep *inodedep; 5078 struct pagedep *pagedep; 5079 struct jnewblk *jnewblk; 5080 struct newblk *newblk; 5081 struct mount *mp; 5082 ufs_lbn_t lbn; 5083 5084 lbn = bp->b_lblkno; 5085 mp = UFSTOVFS(ip->i_ump); 5086 if (oldblkno && oldblkno != newblkno) 5087 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5088 else 5089 freefrag = NULL; 5090 5091 CTR6(KTR_SUJ, 5092 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5093 "off %jd newsize %ld oldsize %d", 5094 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5095 ACQUIRE_LOCK(&lk); 5096 if (off >= NDADDR) { 5097 if (lbn > 0) 5098 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5099 lbn, off); 5100 /* allocating an indirect block */ 5101 if (oldblkno != 0) 5102 panic("softdep_setup_allocdirect: non-zero indir"); 5103 } else { 5104 if (off != lbn) 5105 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5106 lbn, off); 5107 /* 5108 * Allocating a direct block. 5109 * 5110 * If we are allocating a directory block, then we must 5111 * allocate an associated pagedep to track additions and 5112 * deletions. 5113 */ 5114 if ((ip->i_mode & IFMT) == IFDIR) 5115 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5116 &pagedep); 5117 } 5118 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5119 panic("softdep_setup_allocdirect: lost block"); 5120 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5121 ("softdep_setup_allocdirect: newblk already initialized")); 5122 /* 5123 * Convert the newblk to an allocdirect. 5124 */ 5125 newblk->nb_list.wk_type = D_ALLOCDIRECT; 5126 adp = (struct allocdirect *)newblk; 5127 newblk->nb_freefrag = freefrag; 5128 adp->ad_offset = off; 5129 adp->ad_oldblkno = oldblkno; 5130 adp->ad_newsize = newsize; 5131 adp->ad_oldsize = oldsize; 5132 5133 /* 5134 * Finish initializing the journal. 5135 */ 5136 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5137 jnewblk->jn_ino = ip->i_number; 5138 jnewblk->jn_lbn = lbn; 5139 add_to_journal(&jnewblk->jn_list); 5140 } 5141 if (freefrag && freefrag->ff_jdep != NULL && 5142 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5143 add_to_journal(freefrag->ff_jdep); 5144 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5145 adp->ad_inodedep = inodedep; 5146 5147 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5148 /* 5149 * The list of allocdirects must be kept in sorted and ascending 5150 * order so that the rollback routines can quickly determine the 5151 * first uncommitted block (the size of the file stored on disk 5152 * ends at the end of the lowest committed fragment, or if there 5153 * are no fragments, at the end of the highest committed block). 5154 * Since files generally grow, the typical case is that the new 5155 * block is to be added at the end of the list. We speed this 5156 * special case by checking against the last allocdirect in the 5157 * list before laboriously traversing the list looking for the 5158 * insertion point. 5159 */ 5160 adphead = &inodedep->id_newinoupdt; 5161 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5162 if (oldadp == NULL || oldadp->ad_offset <= off) { 5163 /* insert at end of list */ 5164 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5165 if (oldadp != NULL && oldadp->ad_offset == off) 5166 allocdirect_merge(adphead, adp, oldadp); 5167 FREE_LOCK(&lk); 5168 return; 5169 } 5170 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5171 if (oldadp->ad_offset >= off) 5172 break; 5173 } 5174 if (oldadp == NULL) 5175 panic("softdep_setup_allocdirect: lost entry"); 5176 /* insert in middle of list */ 5177 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5178 if (oldadp->ad_offset == off) 5179 allocdirect_merge(adphead, adp, oldadp); 5180 5181 FREE_LOCK(&lk); 5182 } 5183 5184 /* 5185 * Merge a newer and older journal record to be stored either in a 5186 * newblock or freefrag. This handles aggregating journal records for 5187 * fragment allocation into a second record as well as replacing a 5188 * journal free with an aborted journal allocation. A segment for the 5189 * oldest record will be placed on wkhd if it has been written. If not 5190 * the segment for the newer record will suffice. 5191 */ 5192 static struct worklist * 5193 jnewblk_merge(new, old, wkhd) 5194 struct worklist *new; 5195 struct worklist *old; 5196 struct workhead *wkhd; 5197 { 5198 struct jnewblk *njnewblk; 5199 struct jnewblk *jnewblk; 5200 5201 /* Handle NULLs to simplify callers. */ 5202 if (new == NULL) 5203 return (old); 5204 if (old == NULL) 5205 return (new); 5206 /* Replace a jfreefrag with a jnewblk. */ 5207 if (new->wk_type == D_JFREEFRAG) { 5208 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5209 panic("jnewblk_merge: blkno mismatch: %p, %p", 5210 old, new); 5211 cancel_jfreefrag(WK_JFREEFRAG(new)); 5212 return (old); 5213 } 5214 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5215 panic("jnewblk_merge: Bad type: old %d new %d\n", 5216 old->wk_type, new->wk_type); 5217 /* 5218 * Handle merging of two jnewblk records that describe 5219 * different sets of fragments in the same block. 5220 */ 5221 jnewblk = WK_JNEWBLK(old); 5222 njnewblk = WK_JNEWBLK(new); 5223 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5224 panic("jnewblk_merge: Merging disparate blocks."); 5225 /* 5226 * The record may be rolled back in the cg. 5227 */ 5228 if (jnewblk->jn_state & UNDONE) { 5229 jnewblk->jn_state &= ~UNDONE; 5230 njnewblk->jn_state |= UNDONE; 5231 njnewblk->jn_state &= ~ATTACHED; 5232 } 5233 /* 5234 * We modify the newer addref and free the older so that if neither 5235 * has been written the most up-to-date copy will be on disk. If 5236 * both have been written but rolled back we only temporarily need 5237 * one of them to fix the bits when the cg write completes. 5238 */ 5239 jnewblk->jn_state |= ATTACHED | COMPLETE; 5240 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5241 cancel_jnewblk(jnewblk, wkhd); 5242 WORKLIST_REMOVE(&jnewblk->jn_list); 5243 free_jnewblk(jnewblk); 5244 return (new); 5245 } 5246 5247 /* 5248 * Replace an old allocdirect dependency with a newer one. 5249 * This routine must be called with splbio interrupts blocked. 5250 */ 5251 static void 5252 allocdirect_merge(adphead, newadp, oldadp) 5253 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5254 struct allocdirect *newadp; /* allocdirect being added */ 5255 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5256 { 5257 struct worklist *wk; 5258 struct freefrag *freefrag; 5259 5260 freefrag = NULL; 5261 rw_assert(&lk, RA_WLOCKED); 5262 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5263 newadp->ad_oldsize != oldadp->ad_newsize || 5264 newadp->ad_offset >= NDADDR) 5265 panic("%s %jd != new %jd || old size %ld != new %ld", 5266 "allocdirect_merge: old blkno", 5267 (intmax_t)newadp->ad_oldblkno, 5268 (intmax_t)oldadp->ad_newblkno, 5269 newadp->ad_oldsize, oldadp->ad_newsize); 5270 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5271 newadp->ad_oldsize = oldadp->ad_oldsize; 5272 /* 5273 * If the old dependency had a fragment to free or had never 5274 * previously had a block allocated, then the new dependency 5275 * can immediately post its freefrag and adopt the old freefrag. 5276 * This action is done by swapping the freefrag dependencies. 5277 * The new dependency gains the old one's freefrag, and the 5278 * old one gets the new one and then immediately puts it on 5279 * the worklist when it is freed by free_newblk. It is 5280 * not possible to do this swap when the old dependency had a 5281 * non-zero size but no previous fragment to free. This condition 5282 * arises when the new block is an extension of the old block. 5283 * Here, the first part of the fragment allocated to the new 5284 * dependency is part of the block currently claimed on disk by 5285 * the old dependency, so cannot legitimately be freed until the 5286 * conditions for the new dependency are fulfilled. 5287 */ 5288 freefrag = newadp->ad_freefrag; 5289 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5290 newadp->ad_freefrag = oldadp->ad_freefrag; 5291 oldadp->ad_freefrag = freefrag; 5292 } 5293 /* 5294 * If we are tracking a new directory-block allocation, 5295 * move it from the old allocdirect to the new allocdirect. 5296 */ 5297 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5298 WORKLIST_REMOVE(wk); 5299 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5300 panic("allocdirect_merge: extra newdirblk"); 5301 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5302 } 5303 TAILQ_REMOVE(adphead, oldadp, ad_next); 5304 /* 5305 * We need to move any journal dependencies over to the freefrag 5306 * that releases this block if it exists. Otherwise we are 5307 * extending an existing block and we'll wait until that is 5308 * complete to release the journal space and extend the 5309 * new journal to cover this old space as well. 5310 */ 5311 if (freefrag == NULL) { 5312 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5313 panic("allocdirect_merge: %jd != %jd", 5314 oldadp->ad_newblkno, newadp->ad_newblkno); 5315 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5316 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5317 &oldadp->ad_block.nb_jnewblk->jn_list, 5318 &newadp->ad_block.nb_jwork); 5319 oldadp->ad_block.nb_jnewblk = NULL; 5320 cancel_newblk(&oldadp->ad_block, NULL, 5321 &newadp->ad_block.nb_jwork); 5322 } else { 5323 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5324 &freefrag->ff_list, &freefrag->ff_jwork); 5325 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5326 &freefrag->ff_jwork); 5327 } 5328 free_newblk(&oldadp->ad_block); 5329 } 5330 5331 /* 5332 * Allocate a jfreefrag structure to journal a single block free. 5333 */ 5334 static struct jfreefrag * 5335 newjfreefrag(freefrag, ip, blkno, size, lbn) 5336 struct freefrag *freefrag; 5337 struct inode *ip; 5338 ufs2_daddr_t blkno; 5339 long size; 5340 ufs_lbn_t lbn; 5341 { 5342 struct jfreefrag *jfreefrag; 5343 struct fs *fs; 5344 5345 fs = ip->i_fs; 5346 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5347 M_SOFTDEP_FLAGS); 5348 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump)); 5349 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5350 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5351 jfreefrag->fr_ino = ip->i_number; 5352 jfreefrag->fr_lbn = lbn; 5353 jfreefrag->fr_blkno = blkno; 5354 jfreefrag->fr_frags = numfrags(fs, size); 5355 jfreefrag->fr_freefrag = freefrag; 5356 5357 return (jfreefrag); 5358 } 5359 5360 /* 5361 * Allocate a new freefrag structure. 5362 */ 5363 static struct freefrag * 5364 newfreefrag(ip, blkno, size, lbn) 5365 struct inode *ip; 5366 ufs2_daddr_t blkno; 5367 long size; 5368 ufs_lbn_t lbn; 5369 { 5370 struct freefrag *freefrag; 5371 struct fs *fs; 5372 5373 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5374 ip->i_number, blkno, size, lbn); 5375 fs = ip->i_fs; 5376 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5377 panic("newfreefrag: frag size"); 5378 freefrag = malloc(sizeof(struct freefrag), 5379 M_FREEFRAG, M_SOFTDEP_FLAGS); 5380 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 5381 freefrag->ff_state = ATTACHED; 5382 LIST_INIT(&freefrag->ff_jwork); 5383 freefrag->ff_inum = ip->i_number; 5384 freefrag->ff_vtype = ITOV(ip)->v_type; 5385 freefrag->ff_blkno = blkno; 5386 freefrag->ff_fragsize = size; 5387 5388 if (MOUNTEDSUJ(UFSTOVFS(ip->i_ump))) { 5389 freefrag->ff_jdep = (struct worklist *) 5390 newjfreefrag(freefrag, ip, blkno, size, lbn); 5391 } else { 5392 freefrag->ff_state |= DEPCOMPLETE; 5393 freefrag->ff_jdep = NULL; 5394 } 5395 5396 return (freefrag); 5397 } 5398 5399 /* 5400 * This workitem de-allocates fragments that were replaced during 5401 * file block allocation. 5402 */ 5403 static void 5404 handle_workitem_freefrag(freefrag) 5405 struct freefrag *freefrag; 5406 { 5407 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5408 struct workhead wkhd; 5409 5410 CTR3(KTR_SUJ, 5411 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5412 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5413 /* 5414 * It would be illegal to add new completion items to the 5415 * freefrag after it was schedule to be done so it must be 5416 * safe to modify the list head here. 5417 */ 5418 LIST_INIT(&wkhd); 5419 ACQUIRE_LOCK(&lk); 5420 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5421 /* 5422 * If the journal has not been written we must cancel it here. 5423 */ 5424 if (freefrag->ff_jdep) { 5425 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5426 panic("handle_workitem_freefrag: Unexpected type %d\n", 5427 freefrag->ff_jdep->wk_type); 5428 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5429 } 5430 FREE_LOCK(&lk); 5431 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5432 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd); 5433 ACQUIRE_LOCK(&lk); 5434 WORKITEM_FREE(freefrag, D_FREEFRAG); 5435 FREE_LOCK(&lk); 5436 } 5437 5438 /* 5439 * Set up a dependency structure for an external attributes data block. 5440 * This routine follows much of the structure of softdep_setup_allocdirect. 5441 * See the description of softdep_setup_allocdirect above for details. 5442 */ 5443 void 5444 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5445 struct inode *ip; 5446 ufs_lbn_t off; 5447 ufs2_daddr_t newblkno; 5448 ufs2_daddr_t oldblkno; 5449 long newsize; 5450 long oldsize; 5451 struct buf *bp; 5452 { 5453 struct allocdirect *adp, *oldadp; 5454 struct allocdirectlst *adphead; 5455 struct freefrag *freefrag; 5456 struct inodedep *inodedep; 5457 struct jnewblk *jnewblk; 5458 struct newblk *newblk; 5459 struct mount *mp; 5460 ufs_lbn_t lbn; 5461 5462 if (off >= NXADDR) 5463 panic("softdep_setup_allocext: lbn %lld > NXADDR", 5464 (long long)off); 5465 5466 lbn = bp->b_lblkno; 5467 mp = UFSTOVFS(ip->i_ump); 5468 if (oldblkno && oldblkno != newblkno) 5469 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5470 else 5471 freefrag = NULL; 5472 5473 ACQUIRE_LOCK(&lk); 5474 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5475 panic("softdep_setup_allocext: lost block"); 5476 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5477 ("softdep_setup_allocext: newblk already initialized")); 5478 /* 5479 * Convert the newblk to an allocdirect. 5480 */ 5481 newblk->nb_list.wk_type = D_ALLOCDIRECT; 5482 adp = (struct allocdirect *)newblk; 5483 newblk->nb_freefrag = freefrag; 5484 adp->ad_offset = off; 5485 adp->ad_oldblkno = oldblkno; 5486 adp->ad_newsize = newsize; 5487 adp->ad_oldsize = oldsize; 5488 adp->ad_state |= EXTDATA; 5489 5490 /* 5491 * Finish initializing the journal. 5492 */ 5493 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5494 jnewblk->jn_ino = ip->i_number; 5495 jnewblk->jn_lbn = lbn; 5496 add_to_journal(&jnewblk->jn_list); 5497 } 5498 if (freefrag && freefrag->ff_jdep != NULL && 5499 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5500 add_to_journal(freefrag->ff_jdep); 5501 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5502 adp->ad_inodedep = inodedep; 5503 5504 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5505 /* 5506 * The list of allocdirects must be kept in sorted and ascending 5507 * order so that the rollback routines can quickly determine the 5508 * first uncommitted block (the size of the file stored on disk 5509 * ends at the end of the lowest committed fragment, or if there 5510 * are no fragments, at the end of the highest committed block). 5511 * Since files generally grow, the typical case is that the new 5512 * block is to be added at the end of the list. We speed this 5513 * special case by checking against the last allocdirect in the 5514 * list before laboriously traversing the list looking for the 5515 * insertion point. 5516 */ 5517 adphead = &inodedep->id_newextupdt; 5518 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5519 if (oldadp == NULL || oldadp->ad_offset <= off) { 5520 /* insert at end of list */ 5521 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5522 if (oldadp != NULL && oldadp->ad_offset == off) 5523 allocdirect_merge(adphead, adp, oldadp); 5524 FREE_LOCK(&lk); 5525 return; 5526 } 5527 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5528 if (oldadp->ad_offset >= off) 5529 break; 5530 } 5531 if (oldadp == NULL) 5532 panic("softdep_setup_allocext: lost entry"); 5533 /* insert in middle of list */ 5534 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5535 if (oldadp->ad_offset == off) 5536 allocdirect_merge(adphead, adp, oldadp); 5537 FREE_LOCK(&lk); 5538 } 5539 5540 /* 5541 * Indirect block allocation dependencies. 5542 * 5543 * The same dependencies that exist for a direct block also exist when 5544 * a new block is allocated and pointed to by an entry in a block of 5545 * indirect pointers. The undo/redo states described above are also 5546 * used here. Because an indirect block contains many pointers that 5547 * may have dependencies, a second copy of the entire in-memory indirect 5548 * block is kept. The buffer cache copy is always completely up-to-date. 5549 * The second copy, which is used only as a source for disk writes, 5550 * contains only the safe pointers (i.e., those that have no remaining 5551 * update dependencies). The second copy is freed when all pointers 5552 * are safe. The cache is not allowed to replace indirect blocks with 5553 * pending update dependencies. If a buffer containing an indirect 5554 * block with dependencies is written, these routines will mark it 5555 * dirty again. It can only be successfully written once all the 5556 * dependencies are removed. The ffs_fsync routine in conjunction with 5557 * softdep_sync_metadata work together to get all the dependencies 5558 * removed so that a file can be successfully written to disk. Three 5559 * procedures are used when setting up indirect block pointer 5560 * dependencies. The division is necessary because of the organization 5561 * of the "balloc" routine and because of the distinction between file 5562 * pages and file metadata blocks. 5563 */ 5564 5565 /* 5566 * Allocate a new allocindir structure. 5567 */ 5568 static struct allocindir * 5569 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5570 struct inode *ip; /* inode for file being extended */ 5571 int ptrno; /* offset of pointer in indirect block */ 5572 ufs2_daddr_t newblkno; /* disk block number being added */ 5573 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5574 ufs_lbn_t lbn; 5575 { 5576 struct newblk *newblk; 5577 struct allocindir *aip; 5578 struct freefrag *freefrag; 5579 struct jnewblk *jnewblk; 5580 5581 if (oldblkno) 5582 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn); 5583 else 5584 freefrag = NULL; 5585 ACQUIRE_LOCK(&lk); 5586 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0) 5587 panic("new_allocindir: lost block"); 5588 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5589 ("newallocindir: newblk already initialized")); 5590 newblk->nb_list.wk_type = D_ALLOCINDIR; 5591 newblk->nb_freefrag = freefrag; 5592 aip = (struct allocindir *)newblk; 5593 aip->ai_offset = ptrno; 5594 aip->ai_oldblkno = oldblkno; 5595 aip->ai_lbn = lbn; 5596 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5597 jnewblk->jn_ino = ip->i_number; 5598 jnewblk->jn_lbn = lbn; 5599 add_to_journal(&jnewblk->jn_list); 5600 } 5601 if (freefrag && freefrag->ff_jdep != NULL && 5602 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5603 add_to_journal(freefrag->ff_jdep); 5604 return (aip); 5605 } 5606 5607 /* 5608 * Called just before setting an indirect block pointer 5609 * to a newly allocated file page. 5610 */ 5611 void 5612 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5613 struct inode *ip; /* inode for file being extended */ 5614 ufs_lbn_t lbn; /* allocated block number within file */ 5615 struct buf *bp; /* buffer with indirect blk referencing page */ 5616 int ptrno; /* offset of pointer in indirect block */ 5617 ufs2_daddr_t newblkno; /* disk block number being added */ 5618 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5619 struct buf *nbp; /* buffer holding allocated page */ 5620 { 5621 struct inodedep *inodedep; 5622 struct freefrag *freefrag; 5623 struct allocindir *aip; 5624 struct pagedep *pagedep; 5625 struct mount *mp; 5626 int dflags; 5627 5628 if (lbn != nbp->b_lblkno) 5629 panic("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5630 lbn, bp->b_lblkno); 5631 CTR4(KTR_SUJ, 5632 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5633 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5634 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5635 mp = UFSTOVFS(ip->i_ump); 5636 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5637 dflags = DEPALLOC; 5638 if (IS_SNAPSHOT(ip)) 5639 dflags |= NODELAY; 5640 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 5641 /* 5642 * If we are allocating a directory page, then we must 5643 * allocate an associated pagedep to track additions and 5644 * deletions. 5645 */ 5646 if ((ip->i_mode & IFMT) == IFDIR) 5647 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5648 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5649 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5650 FREE_LOCK(&lk); 5651 if (freefrag) 5652 handle_workitem_freefrag(freefrag); 5653 } 5654 5655 /* 5656 * Called just before setting an indirect block pointer to a 5657 * newly allocated indirect block. 5658 */ 5659 void 5660 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5661 struct buf *nbp; /* newly allocated indirect block */ 5662 struct inode *ip; /* inode for file being extended */ 5663 struct buf *bp; /* indirect block referencing allocated block */ 5664 int ptrno; /* offset of pointer in indirect block */ 5665 ufs2_daddr_t newblkno; /* disk block number being added */ 5666 { 5667 struct inodedep *inodedep; 5668 struct allocindir *aip; 5669 ufs_lbn_t lbn; 5670 int dflags; 5671 5672 CTR3(KTR_SUJ, 5673 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5674 ip->i_number, newblkno, ptrno); 5675 lbn = nbp->b_lblkno; 5676 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5677 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5678 dflags = DEPALLOC; 5679 if (IS_SNAPSHOT(ip)) 5680 dflags |= NODELAY; 5681 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 5682 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5683 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5684 panic("softdep_setup_allocindir_meta: Block already existed"); 5685 FREE_LOCK(&lk); 5686 } 5687 5688 static void 5689 indirdep_complete(indirdep) 5690 struct indirdep *indirdep; 5691 { 5692 struct allocindir *aip; 5693 5694 LIST_REMOVE(indirdep, ir_next); 5695 indirdep->ir_state |= DEPCOMPLETE; 5696 5697 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5698 LIST_REMOVE(aip, ai_next); 5699 free_newblk(&aip->ai_block); 5700 } 5701 /* 5702 * If this indirdep is not attached to a buf it was simply waiting 5703 * on completion to clear completehd. free_indirdep() asserts 5704 * that nothing is dangling. 5705 */ 5706 if ((indirdep->ir_state & ONWORKLIST) == 0) 5707 free_indirdep(indirdep); 5708 } 5709 5710 static struct indirdep * 5711 indirdep_lookup(mp, ip, bp) 5712 struct mount *mp; 5713 struct inode *ip; 5714 struct buf *bp; 5715 { 5716 struct indirdep *indirdep, *newindirdep; 5717 struct newblk *newblk; 5718 struct worklist *wk; 5719 struct fs *fs; 5720 ufs2_daddr_t blkno; 5721 5722 rw_assert(&lk, RA_WLOCKED); 5723 indirdep = NULL; 5724 newindirdep = NULL; 5725 fs = ip->i_fs; 5726 for (;;) { 5727 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5728 if (wk->wk_type != D_INDIRDEP) 5729 continue; 5730 indirdep = WK_INDIRDEP(wk); 5731 break; 5732 } 5733 /* Found on the buffer worklist, no new structure to free. */ 5734 if (indirdep != NULL && newindirdep == NULL) 5735 return (indirdep); 5736 if (indirdep != NULL && newindirdep != NULL) 5737 panic("indirdep_lookup: simultaneous create"); 5738 /* None found on the buffer and a new structure is ready. */ 5739 if (indirdep == NULL && newindirdep != NULL) 5740 break; 5741 /* None found and no new structure available. */ 5742 FREE_LOCK(&lk); 5743 newindirdep = malloc(sizeof(struct indirdep), 5744 M_INDIRDEP, M_SOFTDEP_FLAGS); 5745 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5746 newindirdep->ir_state = ATTACHED; 5747 if (ip->i_ump->um_fstype == UFS1) 5748 newindirdep->ir_state |= UFS1FMT; 5749 TAILQ_INIT(&newindirdep->ir_trunc); 5750 newindirdep->ir_saveddata = NULL; 5751 LIST_INIT(&newindirdep->ir_deplisthd); 5752 LIST_INIT(&newindirdep->ir_donehd); 5753 LIST_INIT(&newindirdep->ir_writehd); 5754 LIST_INIT(&newindirdep->ir_completehd); 5755 if (bp->b_blkno == bp->b_lblkno) { 5756 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5757 NULL, NULL); 5758 bp->b_blkno = blkno; 5759 } 5760 newindirdep->ir_freeblks = NULL; 5761 newindirdep->ir_savebp = 5762 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5763 newindirdep->ir_bp = bp; 5764 BUF_KERNPROC(newindirdep->ir_savebp); 5765 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5766 ACQUIRE_LOCK(&lk); 5767 } 5768 indirdep = newindirdep; 5769 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 5770 /* 5771 * If the block is not yet allocated we don't set DEPCOMPLETE so 5772 * that we don't free dependencies until the pointers are valid. 5773 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 5774 * than using the hash. 5775 */ 5776 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 5777 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 5778 else 5779 indirdep->ir_state |= DEPCOMPLETE; 5780 return (indirdep); 5781 } 5782 5783 /* 5784 * Called to finish the allocation of the "aip" allocated 5785 * by one of the two routines above. 5786 */ 5787 static struct freefrag * 5788 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 5789 struct buf *bp; /* in-memory copy of the indirect block */ 5790 struct inode *ip; /* inode for file being extended */ 5791 struct inodedep *inodedep; /* Inodedep for ip */ 5792 struct allocindir *aip; /* allocindir allocated by the above routines */ 5793 ufs_lbn_t lbn; /* Logical block number for this block. */ 5794 { 5795 struct fs *fs; 5796 struct indirdep *indirdep; 5797 struct allocindir *oldaip; 5798 struct freefrag *freefrag; 5799 struct mount *mp; 5800 5801 rw_assert(&lk, RA_WLOCKED); 5802 mp = UFSTOVFS(ip->i_ump); 5803 fs = ip->i_fs; 5804 if (bp->b_lblkno >= 0) 5805 panic("setup_allocindir_phase2: not indir blk"); 5806 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 5807 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 5808 indirdep = indirdep_lookup(mp, ip, bp); 5809 KASSERT(indirdep->ir_savebp != NULL, 5810 ("setup_allocindir_phase2 NULL ir_savebp")); 5811 aip->ai_indirdep = indirdep; 5812 /* 5813 * Check for an unwritten dependency for this indirect offset. If 5814 * there is, merge the old dependency into the new one. This happens 5815 * as a result of reallocblk only. 5816 */ 5817 freefrag = NULL; 5818 if (aip->ai_oldblkno != 0) { 5819 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 5820 if (oldaip->ai_offset == aip->ai_offset) { 5821 freefrag = allocindir_merge(aip, oldaip); 5822 goto done; 5823 } 5824 } 5825 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 5826 if (oldaip->ai_offset == aip->ai_offset) { 5827 freefrag = allocindir_merge(aip, oldaip); 5828 goto done; 5829 } 5830 } 5831 } 5832 done: 5833 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 5834 return (freefrag); 5835 } 5836 5837 /* 5838 * Merge two allocindirs which refer to the same block. Move newblock 5839 * dependencies and setup the freefrags appropriately. 5840 */ 5841 static struct freefrag * 5842 allocindir_merge(aip, oldaip) 5843 struct allocindir *aip; 5844 struct allocindir *oldaip; 5845 { 5846 struct freefrag *freefrag; 5847 struct worklist *wk; 5848 5849 if (oldaip->ai_newblkno != aip->ai_oldblkno) 5850 panic("allocindir_merge: blkno"); 5851 aip->ai_oldblkno = oldaip->ai_oldblkno; 5852 freefrag = aip->ai_freefrag; 5853 aip->ai_freefrag = oldaip->ai_freefrag; 5854 oldaip->ai_freefrag = NULL; 5855 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 5856 /* 5857 * If we are tracking a new directory-block allocation, 5858 * move it from the old allocindir to the new allocindir. 5859 */ 5860 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 5861 WORKLIST_REMOVE(wk); 5862 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 5863 panic("allocindir_merge: extra newdirblk"); 5864 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 5865 } 5866 /* 5867 * We can skip journaling for this freefrag and just complete 5868 * any pending journal work for the allocindir that is being 5869 * removed after the freefrag completes. 5870 */ 5871 if (freefrag->ff_jdep) 5872 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 5873 LIST_REMOVE(oldaip, ai_next); 5874 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 5875 &freefrag->ff_list, &freefrag->ff_jwork); 5876 free_newblk(&oldaip->ai_block); 5877 5878 return (freefrag); 5879 } 5880 5881 static inline void 5882 setup_freedirect(freeblks, ip, i, needj) 5883 struct freeblks *freeblks; 5884 struct inode *ip; 5885 int i; 5886 int needj; 5887 { 5888 ufs2_daddr_t blkno; 5889 int frags; 5890 5891 blkno = DIP(ip, i_db[i]); 5892 if (blkno == 0) 5893 return; 5894 DIP_SET(ip, i_db[i], 0); 5895 frags = sblksize(ip->i_fs, ip->i_size, i); 5896 frags = numfrags(ip->i_fs, frags); 5897 newfreework(ip->i_ump, freeblks, NULL, i, blkno, frags, 0, needj); 5898 } 5899 5900 static inline void 5901 setup_freeext(freeblks, ip, i, needj) 5902 struct freeblks *freeblks; 5903 struct inode *ip; 5904 int i; 5905 int needj; 5906 { 5907 ufs2_daddr_t blkno; 5908 int frags; 5909 5910 blkno = ip->i_din2->di_extb[i]; 5911 if (blkno == 0) 5912 return; 5913 ip->i_din2->di_extb[i] = 0; 5914 frags = sblksize(ip->i_fs, ip->i_din2->di_extsize, i); 5915 frags = numfrags(ip->i_fs, frags); 5916 newfreework(ip->i_ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 5917 } 5918 5919 static inline void 5920 setup_freeindir(freeblks, ip, i, lbn, needj) 5921 struct freeblks *freeblks; 5922 struct inode *ip; 5923 int i; 5924 ufs_lbn_t lbn; 5925 int needj; 5926 { 5927 ufs2_daddr_t blkno; 5928 5929 blkno = DIP(ip, i_ib[i]); 5930 if (blkno == 0) 5931 return; 5932 DIP_SET(ip, i_ib[i], 0); 5933 newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, ip->i_fs->fs_frag, 5934 0, needj); 5935 } 5936 5937 static inline struct freeblks * 5938 newfreeblks(mp, ip) 5939 struct mount *mp; 5940 struct inode *ip; 5941 { 5942 struct freeblks *freeblks; 5943 5944 freeblks = malloc(sizeof(struct freeblks), 5945 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 5946 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 5947 LIST_INIT(&freeblks->fb_jblkdephd); 5948 LIST_INIT(&freeblks->fb_jwork); 5949 freeblks->fb_ref = 0; 5950 freeblks->fb_cgwait = 0; 5951 freeblks->fb_state = ATTACHED; 5952 freeblks->fb_uid = ip->i_uid; 5953 freeblks->fb_inum = ip->i_number; 5954 freeblks->fb_vtype = ITOV(ip)->v_type; 5955 freeblks->fb_modrev = DIP(ip, i_modrev); 5956 freeblks->fb_devvp = ip->i_devvp; 5957 freeblks->fb_chkcnt = 0; 5958 freeblks->fb_len = 0; 5959 5960 return (freeblks); 5961 } 5962 5963 static void 5964 trunc_indirdep(indirdep, freeblks, bp, off) 5965 struct indirdep *indirdep; 5966 struct freeblks *freeblks; 5967 struct buf *bp; 5968 int off; 5969 { 5970 struct allocindir *aip, *aipn; 5971 5972 /* 5973 * The first set of allocindirs won't be in savedbp. 5974 */ 5975 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 5976 if (aip->ai_offset > off) 5977 cancel_allocindir(aip, bp, freeblks, 1); 5978 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 5979 if (aip->ai_offset > off) 5980 cancel_allocindir(aip, bp, freeblks, 1); 5981 /* 5982 * These will exist in savedbp. 5983 */ 5984 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 5985 if (aip->ai_offset > off) 5986 cancel_allocindir(aip, NULL, freeblks, 0); 5987 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 5988 if (aip->ai_offset > off) 5989 cancel_allocindir(aip, NULL, freeblks, 0); 5990 } 5991 5992 /* 5993 * Follow the chain of indirects down to lastlbn creating a freework 5994 * structure for each. This will be used to start indir_trunc() at 5995 * the right offset and create the journal records for the parrtial 5996 * truncation. A second step will handle the truncated dependencies. 5997 */ 5998 static int 5999 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 6000 struct freeblks *freeblks; 6001 struct inode *ip; 6002 ufs_lbn_t lbn; 6003 ufs_lbn_t lastlbn; 6004 ufs2_daddr_t blkno; 6005 { 6006 struct indirdep *indirdep; 6007 struct indirdep *indirn; 6008 struct freework *freework; 6009 struct newblk *newblk; 6010 struct mount *mp; 6011 struct buf *bp; 6012 uint8_t *start; 6013 uint8_t *end; 6014 ufs_lbn_t lbnadd; 6015 int level; 6016 int error; 6017 int off; 6018 6019 6020 freework = NULL; 6021 if (blkno == 0) 6022 return (0); 6023 mp = freeblks->fb_list.wk_mp; 6024 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6025 if ((bp->b_flags & B_CACHE) == 0) { 6026 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6027 bp->b_iocmd = BIO_READ; 6028 bp->b_flags &= ~B_INVAL; 6029 bp->b_ioflags &= ~BIO_ERROR; 6030 vfs_busy_pages(bp, 0); 6031 bp->b_iooffset = dbtob(bp->b_blkno); 6032 bstrategy(bp); 6033 curthread->td_ru.ru_inblock++; 6034 error = bufwait(bp); 6035 if (error) { 6036 brelse(bp); 6037 return (error); 6038 } 6039 } 6040 level = lbn_level(lbn); 6041 lbnadd = lbn_offset(ip->i_fs, level); 6042 /* 6043 * Compute the offset of the last block we want to keep. Store 6044 * in the freework the first block we want to completely free. 6045 */ 6046 off = (lastlbn - -(lbn + level)) / lbnadd; 6047 if (off + 1 == NINDIR(ip->i_fs)) 6048 goto nowork; 6049 freework = newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, 0, off+1, 6050 0); 6051 /* 6052 * Link the freework into the indirdep. This will prevent any new 6053 * allocations from proceeding until we are finished with the 6054 * truncate and the block is written. 6055 */ 6056 ACQUIRE_LOCK(&lk); 6057 indirdep = indirdep_lookup(mp, ip, bp); 6058 if (indirdep->ir_freeblks) 6059 panic("setup_trunc_indir: indirdep already truncated."); 6060 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6061 freework->fw_indir = indirdep; 6062 /* 6063 * Cancel any allocindirs that will not make it to disk. 6064 * We have to do this for all copies of the indirdep that 6065 * live on this newblk. 6066 */ 6067 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6068 newblk_lookup(mp, dbtofsb(ip->i_fs, bp->b_blkno), 0, &newblk); 6069 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6070 trunc_indirdep(indirn, freeblks, bp, off); 6071 } else 6072 trunc_indirdep(indirdep, freeblks, bp, off); 6073 FREE_LOCK(&lk); 6074 /* 6075 * Creation is protected by the buf lock. The saveddata is only 6076 * needed if a full truncation follows a partial truncation but it 6077 * is difficult to allocate in that case so we fetch it anyway. 6078 */ 6079 if (indirdep->ir_saveddata == NULL) 6080 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6081 M_SOFTDEP_FLAGS); 6082 nowork: 6083 /* Fetch the blkno of the child and the zero start offset. */ 6084 if (ip->i_ump->um_fstype == UFS1) { 6085 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6086 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6087 } else { 6088 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6089 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6090 } 6091 if (freework) { 6092 /* Zero the truncated pointers. */ 6093 end = bp->b_data + bp->b_bcount; 6094 bzero(start, end - start); 6095 bdwrite(bp); 6096 } else 6097 bqrelse(bp); 6098 if (level == 0) 6099 return (0); 6100 lbn++; /* adjust level */ 6101 lbn -= (off * lbnadd); 6102 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6103 } 6104 6105 /* 6106 * Complete the partial truncation of an indirect block setup by 6107 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6108 * copy and writes them to disk before the freeblks is allowed to complete. 6109 */ 6110 static void 6111 complete_trunc_indir(freework) 6112 struct freework *freework; 6113 { 6114 struct freework *fwn; 6115 struct indirdep *indirdep; 6116 struct buf *bp; 6117 uintptr_t start; 6118 int count; 6119 6120 indirdep = freework->fw_indir; 6121 for (;;) { 6122 bp = indirdep->ir_bp; 6123 /* See if the block was discarded. */ 6124 if (bp == NULL) 6125 break; 6126 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6127 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6128 break; 6129 if (BUF_LOCK(bp, 6130 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, &lk) == 0) 6131 BUF_UNLOCK(bp); 6132 ACQUIRE_LOCK(&lk); 6133 } 6134 rw_assert(&lk, RA_WLOCKED); 6135 freework->fw_state |= DEPCOMPLETE; 6136 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6137 /* 6138 * Zero the pointers in the saved copy. 6139 */ 6140 if (indirdep->ir_state & UFS1FMT) 6141 start = sizeof(ufs1_daddr_t); 6142 else 6143 start = sizeof(ufs2_daddr_t); 6144 start *= freework->fw_start; 6145 count = indirdep->ir_savebp->b_bcount - start; 6146 start += (uintptr_t)indirdep->ir_savebp->b_data; 6147 bzero((char *)start, count); 6148 /* 6149 * We need to start the next truncation in the list if it has not 6150 * been started yet. 6151 */ 6152 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6153 if (fwn != NULL) { 6154 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6155 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6156 if ((fwn->fw_state & ONWORKLIST) == 0) 6157 freework_enqueue(fwn); 6158 } 6159 /* 6160 * If bp is NULL the block was fully truncated, restore 6161 * the saved block list otherwise free it if it is no 6162 * longer needed. 6163 */ 6164 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6165 if (bp == NULL) 6166 bcopy(indirdep->ir_saveddata, 6167 indirdep->ir_savebp->b_data, 6168 indirdep->ir_savebp->b_bcount); 6169 free(indirdep->ir_saveddata, M_INDIRDEP); 6170 indirdep->ir_saveddata = NULL; 6171 } 6172 /* 6173 * When bp is NULL there is a full truncation pending. We 6174 * must wait for this full truncation to be journaled before 6175 * we can release this freework because the disk pointers will 6176 * never be written as zero. 6177 */ 6178 if (bp == NULL) { 6179 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6180 handle_written_freework(freework); 6181 else 6182 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6183 &freework->fw_list); 6184 } else { 6185 /* Complete when the real copy is written. */ 6186 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6187 BUF_UNLOCK(bp); 6188 } 6189 } 6190 6191 /* 6192 * Calculate the number of blocks we are going to release where datablocks 6193 * is the current total and length is the new file size. 6194 */ 6195 ufs2_daddr_t 6196 blkcount(fs, datablocks, length) 6197 struct fs *fs; 6198 ufs2_daddr_t datablocks; 6199 off_t length; 6200 { 6201 off_t totblks, numblks; 6202 6203 totblks = 0; 6204 numblks = howmany(length, fs->fs_bsize); 6205 if (numblks <= NDADDR) { 6206 totblks = howmany(length, fs->fs_fsize); 6207 goto out; 6208 } 6209 totblks = blkstofrags(fs, numblks); 6210 numblks -= NDADDR; 6211 /* 6212 * Count all single, then double, then triple indirects required. 6213 * Subtracting one indirects worth of blocks for each pass 6214 * acknowledges one of each pointed to by the inode. 6215 */ 6216 for (;;) { 6217 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6218 numblks -= NINDIR(fs); 6219 if (numblks <= 0) 6220 break; 6221 numblks = howmany(numblks, NINDIR(fs)); 6222 } 6223 out: 6224 totblks = fsbtodb(fs, totblks); 6225 /* 6226 * Handle sparse files. We can't reclaim more blocks than the inode 6227 * references. We will correct it later in handle_complete_freeblks() 6228 * when we know the real count. 6229 */ 6230 if (totblks > datablocks) 6231 return (0); 6232 return (datablocks - totblks); 6233 } 6234 6235 /* 6236 * Handle freeblocks for journaled softupdate filesystems. 6237 * 6238 * Contrary to normal softupdates, we must preserve the block pointers in 6239 * indirects until their subordinates are free. This is to avoid journaling 6240 * every block that is freed which may consume more space than the journal 6241 * itself. The recovery program will see the free block journals at the 6242 * base of the truncated area and traverse them to reclaim space. The 6243 * pointers in the inode may be cleared immediately after the journal 6244 * records are written because each direct and indirect pointer in the 6245 * inode is recorded in a journal. This permits full truncation to proceed 6246 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6247 * 6248 * The algorithm is as follows: 6249 * 1) Traverse the in-memory state and create journal entries to release 6250 * the relevant blocks and full indirect trees. 6251 * 2) Traverse the indirect block chain adding partial truncation freework 6252 * records to indirects in the path to lastlbn. The freework will 6253 * prevent new allocation dependencies from being satisfied in this 6254 * indirect until the truncation completes. 6255 * 3) Read and lock the inode block, performing an update with the new size 6256 * and pointers. This prevents truncated data from becoming valid on 6257 * disk through step 4. 6258 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6259 * eliminate journal work for those records that do not require it. 6260 * 5) Schedule the journal records to be written followed by the inode block. 6261 * 6) Allocate any necessary frags for the end of file. 6262 * 7) Zero any partially truncated blocks. 6263 * 6264 * From this truncation proceeds asynchronously using the freework and 6265 * indir_trunc machinery. The file will not be extended again into a 6266 * partially truncated indirect block until all work is completed but 6267 * the normal dependency mechanism ensures that it is rolled back/forward 6268 * as appropriate. Further truncation may occur without delay and is 6269 * serialized in indir_trunc(). 6270 */ 6271 void 6272 softdep_journal_freeblocks(ip, cred, length, flags) 6273 struct inode *ip; /* The inode whose length is to be reduced */ 6274 struct ucred *cred; 6275 off_t length; /* The new length for the file */ 6276 int flags; /* IO_EXT and/or IO_NORMAL */ 6277 { 6278 struct freeblks *freeblks, *fbn; 6279 struct worklist *wk, *wkn; 6280 struct inodedep *inodedep; 6281 struct jblkdep *jblkdep; 6282 struct allocdirect *adp, *adpn; 6283 struct fs *fs; 6284 struct buf *bp; 6285 struct vnode *vp; 6286 struct mount *mp; 6287 ufs2_daddr_t extblocks, datablocks; 6288 ufs_lbn_t tmpval, lbn, lastlbn; 6289 int frags, lastoff, iboff, allocblock, needj, dflags, error, i; 6290 6291 fs = ip->i_fs; 6292 mp = UFSTOVFS(ip->i_ump); 6293 vp = ITOV(ip); 6294 needj = 1; 6295 iboff = -1; 6296 allocblock = 0; 6297 extblocks = 0; 6298 datablocks = 0; 6299 frags = 0; 6300 freeblks = newfreeblks(mp, ip); 6301 ACQUIRE_LOCK(&lk); 6302 /* 6303 * If we're truncating a removed file that will never be written 6304 * we don't need to journal the block frees. The canceled journals 6305 * for the allocations will suffice. 6306 */ 6307 dflags = DEPALLOC; 6308 if (IS_SNAPSHOT(ip)) 6309 dflags |= NODELAY; 6310 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6311 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6312 length == 0) 6313 needj = 0; 6314 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6315 ip->i_number, length, needj); 6316 FREE_LOCK(&lk); 6317 /* 6318 * Calculate the lbn that we are truncating to. This results in -1 6319 * if we're truncating the 0 bytes. So it is the last lbn we want 6320 * to keep, not the first lbn we want to truncate. 6321 */ 6322 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6323 lastoff = blkoff(fs, length); 6324 /* 6325 * Compute frags we are keeping in lastlbn. 0 means all. 6326 */ 6327 if (lastlbn >= 0 && lastlbn < NDADDR) { 6328 frags = fragroundup(fs, lastoff); 6329 /* adp offset of last valid allocdirect. */ 6330 iboff = lastlbn; 6331 } else if (lastlbn > 0) 6332 iboff = NDADDR; 6333 if (fs->fs_magic == FS_UFS2_MAGIC) 6334 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6335 /* 6336 * Handle normal data blocks and indirects. This section saves 6337 * values used after the inode update to complete frag and indirect 6338 * truncation. 6339 */ 6340 if ((flags & IO_NORMAL) != 0) { 6341 /* 6342 * Handle truncation of whole direct and indirect blocks. 6343 */ 6344 for (i = iboff + 1; i < NDADDR; i++) 6345 setup_freedirect(freeblks, ip, i, needj); 6346 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6347 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6348 /* Release a whole indirect tree. */ 6349 if (lbn > lastlbn) { 6350 setup_freeindir(freeblks, ip, i, -lbn -i, 6351 needj); 6352 continue; 6353 } 6354 iboff = i + NDADDR; 6355 /* 6356 * Traverse partially truncated indirect tree. 6357 */ 6358 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6359 setup_trunc_indir(freeblks, ip, -lbn - i, 6360 lastlbn, DIP(ip, i_ib[i])); 6361 } 6362 /* 6363 * Handle partial truncation to a frag boundary. 6364 */ 6365 if (frags) { 6366 ufs2_daddr_t blkno; 6367 long oldfrags; 6368 6369 oldfrags = blksize(fs, ip, lastlbn); 6370 blkno = DIP(ip, i_db[lastlbn]); 6371 if (blkno && oldfrags != frags) { 6372 oldfrags -= frags; 6373 oldfrags = numfrags(ip->i_fs, oldfrags); 6374 blkno += numfrags(ip->i_fs, frags); 6375 newfreework(ip->i_ump, freeblks, NULL, lastlbn, 6376 blkno, oldfrags, 0, needj); 6377 } else if (blkno == 0) 6378 allocblock = 1; 6379 } 6380 /* 6381 * Add a journal record for partial truncate if we are 6382 * handling indirect blocks. Non-indirects need no extra 6383 * journaling. 6384 */ 6385 if (length != 0 && lastlbn >= NDADDR) { 6386 ip->i_flag |= IN_TRUNCATED; 6387 newjtrunc(freeblks, length, 0); 6388 } 6389 ip->i_size = length; 6390 DIP_SET(ip, i_size, ip->i_size); 6391 datablocks = DIP(ip, i_blocks) - extblocks; 6392 if (length != 0) 6393 datablocks = blkcount(ip->i_fs, datablocks, length); 6394 freeblks->fb_len = length; 6395 } 6396 if ((flags & IO_EXT) != 0) { 6397 for (i = 0; i < NXADDR; i++) 6398 setup_freeext(freeblks, ip, i, needj); 6399 ip->i_din2->di_extsize = 0; 6400 datablocks += extblocks; 6401 } 6402 #ifdef QUOTA 6403 /* Reference the quotas in case the block count is wrong in the end. */ 6404 quotaref(vp, freeblks->fb_quota); 6405 (void) chkdq(ip, -datablocks, NOCRED, 0); 6406 #endif 6407 freeblks->fb_chkcnt = -datablocks; 6408 UFS_LOCK(ip->i_ump); 6409 fs->fs_pendingblocks += datablocks; 6410 UFS_UNLOCK(ip->i_ump); 6411 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6412 /* 6413 * Handle truncation of incomplete alloc direct dependencies. We 6414 * hold the inode block locked to prevent incomplete dependencies 6415 * from reaching the disk while we are eliminating those that 6416 * have been truncated. This is a partially inlined ffs_update(). 6417 */ 6418 ufs_itimes(vp); 6419 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6420 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6421 (int)fs->fs_bsize, cred, &bp); 6422 if (error) { 6423 brelse(bp); 6424 softdep_error("softdep_journal_freeblocks", error); 6425 return; 6426 } 6427 if (bp->b_bufsize == fs->fs_bsize) 6428 bp->b_flags |= B_CLUSTEROK; 6429 softdep_update_inodeblock(ip, bp, 0); 6430 if (ip->i_ump->um_fstype == UFS1) 6431 *((struct ufs1_dinode *)bp->b_data + 6432 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6433 else 6434 *((struct ufs2_dinode *)bp->b_data + 6435 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6436 ACQUIRE_LOCK(&lk); 6437 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6438 if ((inodedep->id_state & IOSTARTED) != 0) 6439 panic("softdep_setup_freeblocks: inode busy"); 6440 /* 6441 * Add the freeblks structure to the list of operations that 6442 * must await the zero'ed inode being written to disk. If we 6443 * still have a bitmap dependency (needj), then the inode 6444 * has never been written to disk, so we can process the 6445 * freeblks below once we have deleted the dependencies. 6446 */ 6447 if (needj) 6448 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6449 else 6450 freeblks->fb_state |= COMPLETE; 6451 if ((flags & IO_NORMAL) != 0) { 6452 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6453 if (adp->ad_offset > iboff) 6454 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6455 freeblks); 6456 /* 6457 * Truncate the allocdirect. We could eliminate 6458 * or modify journal records as well. 6459 */ 6460 else if (adp->ad_offset == iboff && frags) 6461 adp->ad_newsize = frags; 6462 } 6463 } 6464 if ((flags & IO_EXT) != 0) 6465 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6466 cancel_allocdirect(&inodedep->id_extupdt, adp, 6467 freeblks); 6468 /* 6469 * Scan the bufwait list for newblock dependencies that will never 6470 * make it to disk. 6471 */ 6472 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6473 if (wk->wk_type != D_ALLOCDIRECT) 6474 continue; 6475 adp = WK_ALLOCDIRECT(wk); 6476 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6477 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6478 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6479 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6480 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6481 } 6482 } 6483 /* 6484 * Add journal work. 6485 */ 6486 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6487 add_to_journal(&jblkdep->jb_list); 6488 FREE_LOCK(&lk); 6489 bdwrite(bp); 6490 /* 6491 * Truncate dependency structures beyond length. 6492 */ 6493 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6494 /* 6495 * This is only set when we need to allocate a fragment because 6496 * none existed at the end of a frag-sized file. It handles only 6497 * allocating a new, zero filled block. 6498 */ 6499 if (allocblock) { 6500 ip->i_size = length - lastoff; 6501 DIP_SET(ip, i_size, ip->i_size); 6502 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6503 if (error != 0) { 6504 softdep_error("softdep_journal_freeblks", error); 6505 return; 6506 } 6507 ip->i_size = length; 6508 DIP_SET(ip, i_size, length); 6509 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6510 allocbuf(bp, frags); 6511 ffs_update(vp, 0); 6512 bawrite(bp); 6513 } else if (lastoff != 0 && vp->v_type != VDIR) { 6514 int size; 6515 6516 /* 6517 * Zero the end of a truncated frag or block. 6518 */ 6519 size = sblksize(fs, length, lastlbn); 6520 error = bread(vp, lastlbn, size, cred, &bp); 6521 if (error) { 6522 softdep_error("softdep_journal_freeblks", error); 6523 return; 6524 } 6525 bzero((char *)bp->b_data + lastoff, size - lastoff); 6526 bawrite(bp); 6527 6528 } 6529 ACQUIRE_LOCK(&lk); 6530 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6531 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6532 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6533 /* 6534 * We zero earlier truncations so they don't erroneously 6535 * update i_blocks. 6536 */ 6537 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6538 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6539 fbn->fb_len = 0; 6540 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6541 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6542 freeblks->fb_state |= INPROGRESS; 6543 else 6544 freeblks = NULL; 6545 FREE_LOCK(&lk); 6546 if (freeblks) 6547 handle_workitem_freeblocks(freeblks, 0); 6548 trunc_pages(ip, length, extblocks, flags); 6549 6550 } 6551 6552 /* 6553 * Flush a JOP_SYNC to the journal. 6554 */ 6555 void 6556 softdep_journal_fsync(ip) 6557 struct inode *ip; 6558 { 6559 struct jfsync *jfsync; 6560 6561 if ((ip->i_flag & IN_TRUNCATED) == 0) 6562 return; 6563 ip->i_flag &= ~IN_TRUNCATED; 6564 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6565 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ip->i_ump)); 6566 jfsync->jfs_size = ip->i_size; 6567 jfsync->jfs_ino = ip->i_number; 6568 ACQUIRE_LOCK(&lk); 6569 add_to_journal(&jfsync->jfs_list); 6570 jwait(&jfsync->jfs_list, MNT_WAIT); 6571 FREE_LOCK(&lk); 6572 } 6573 6574 /* 6575 * Block de-allocation dependencies. 6576 * 6577 * When blocks are de-allocated, the on-disk pointers must be nullified before 6578 * the blocks are made available for use by other files. (The true 6579 * requirement is that old pointers must be nullified before new on-disk 6580 * pointers are set. We chose this slightly more stringent requirement to 6581 * reduce complexity.) Our implementation handles this dependency by updating 6582 * the inode (or indirect block) appropriately but delaying the actual block 6583 * de-allocation (i.e., freemap and free space count manipulation) until 6584 * after the updated versions reach stable storage. After the disk is 6585 * updated, the blocks can be safely de-allocated whenever it is convenient. 6586 * This implementation handles only the common case of reducing a file's 6587 * length to zero. Other cases are handled by the conventional synchronous 6588 * write approach. 6589 * 6590 * The ffs implementation with which we worked double-checks 6591 * the state of the block pointers and file size as it reduces 6592 * a file's length. Some of this code is replicated here in our 6593 * soft updates implementation. The freeblks->fb_chkcnt field is 6594 * used to transfer a part of this information to the procedure 6595 * that eventually de-allocates the blocks. 6596 * 6597 * This routine should be called from the routine that shortens 6598 * a file's length, before the inode's size or block pointers 6599 * are modified. It will save the block pointer information for 6600 * later release and zero the inode so that the calling routine 6601 * can release it. 6602 */ 6603 void 6604 softdep_setup_freeblocks(ip, length, flags) 6605 struct inode *ip; /* The inode whose length is to be reduced */ 6606 off_t length; /* The new length for the file */ 6607 int flags; /* IO_EXT and/or IO_NORMAL */ 6608 { 6609 struct ufs1_dinode *dp1; 6610 struct ufs2_dinode *dp2; 6611 struct freeblks *freeblks; 6612 struct inodedep *inodedep; 6613 struct allocdirect *adp; 6614 struct buf *bp; 6615 struct fs *fs; 6616 ufs2_daddr_t extblocks, datablocks; 6617 struct mount *mp; 6618 int i, delay, error, dflags; 6619 ufs_lbn_t tmpval; 6620 ufs_lbn_t lbn; 6621 6622 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6623 ip->i_number, length); 6624 fs = ip->i_fs; 6625 mp = UFSTOVFS(ip->i_ump); 6626 if (length != 0) 6627 panic("softdep_setup_freeblocks: non-zero length"); 6628 freeblks = newfreeblks(mp, ip); 6629 extblocks = 0; 6630 datablocks = 0; 6631 if (fs->fs_magic == FS_UFS2_MAGIC) 6632 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6633 if ((flags & IO_NORMAL) != 0) { 6634 for (i = 0; i < NDADDR; i++) 6635 setup_freedirect(freeblks, ip, i, 0); 6636 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6637 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6638 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6639 ip->i_size = 0; 6640 DIP_SET(ip, i_size, 0); 6641 datablocks = DIP(ip, i_blocks) - extblocks; 6642 } 6643 if ((flags & IO_EXT) != 0) { 6644 for (i = 0; i < NXADDR; i++) 6645 setup_freeext(freeblks, ip, i, 0); 6646 ip->i_din2->di_extsize = 0; 6647 datablocks += extblocks; 6648 } 6649 #ifdef QUOTA 6650 /* Reference the quotas in case the block count is wrong in the end. */ 6651 quotaref(ITOV(ip), freeblks->fb_quota); 6652 (void) chkdq(ip, -datablocks, NOCRED, 0); 6653 #endif 6654 freeblks->fb_chkcnt = -datablocks; 6655 UFS_LOCK(ip->i_ump); 6656 fs->fs_pendingblocks += datablocks; 6657 UFS_UNLOCK(ip->i_ump); 6658 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6659 /* 6660 * Push the zero'ed inode to to its disk buffer so that we are free 6661 * to delete its dependencies below. Once the dependencies are gone 6662 * the buffer can be safely released. 6663 */ 6664 if ((error = bread(ip->i_devvp, 6665 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6666 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6667 brelse(bp); 6668 softdep_error("softdep_setup_freeblocks", error); 6669 } 6670 if (ip->i_ump->um_fstype == UFS1) { 6671 dp1 = ((struct ufs1_dinode *)bp->b_data + 6672 ino_to_fsbo(fs, ip->i_number)); 6673 ip->i_din1->di_freelink = dp1->di_freelink; 6674 *dp1 = *ip->i_din1; 6675 } else { 6676 dp2 = ((struct ufs2_dinode *)bp->b_data + 6677 ino_to_fsbo(fs, ip->i_number)); 6678 ip->i_din2->di_freelink = dp2->di_freelink; 6679 *dp2 = *ip->i_din2; 6680 } 6681 /* 6682 * Find and eliminate any inode dependencies. 6683 */ 6684 ACQUIRE_LOCK(&lk); 6685 dflags = DEPALLOC; 6686 if (IS_SNAPSHOT(ip)) 6687 dflags |= NODELAY; 6688 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6689 if ((inodedep->id_state & IOSTARTED) != 0) 6690 panic("softdep_setup_freeblocks: inode busy"); 6691 /* 6692 * Add the freeblks structure to the list of operations that 6693 * must await the zero'ed inode being written to disk. If we 6694 * still have a bitmap dependency (delay == 0), then the inode 6695 * has never been written to disk, so we can process the 6696 * freeblks below once we have deleted the dependencies. 6697 */ 6698 delay = (inodedep->id_state & DEPCOMPLETE); 6699 if (delay) 6700 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6701 else 6702 freeblks->fb_state |= COMPLETE; 6703 /* 6704 * Because the file length has been truncated to zero, any 6705 * pending block allocation dependency structures associated 6706 * with this inode are obsolete and can simply be de-allocated. 6707 * We must first merge the two dependency lists to get rid of 6708 * any duplicate freefrag structures, then purge the merged list. 6709 * If we still have a bitmap dependency, then the inode has never 6710 * been written to disk, so we can free any fragments without delay. 6711 */ 6712 if (flags & IO_NORMAL) { 6713 merge_inode_lists(&inodedep->id_newinoupdt, 6714 &inodedep->id_inoupdt); 6715 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 6716 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6717 freeblks); 6718 } 6719 if (flags & IO_EXT) { 6720 merge_inode_lists(&inodedep->id_newextupdt, 6721 &inodedep->id_extupdt); 6722 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6723 cancel_allocdirect(&inodedep->id_extupdt, adp, 6724 freeblks); 6725 } 6726 FREE_LOCK(&lk); 6727 bdwrite(bp); 6728 trunc_dependencies(ip, freeblks, -1, 0, flags); 6729 ACQUIRE_LOCK(&lk); 6730 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 6731 (void) free_inodedep(inodedep); 6732 freeblks->fb_state |= DEPCOMPLETE; 6733 /* 6734 * If the inode with zeroed block pointers is now on disk 6735 * we can start freeing blocks. 6736 */ 6737 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 6738 freeblks->fb_state |= INPROGRESS; 6739 else 6740 freeblks = NULL; 6741 FREE_LOCK(&lk); 6742 if (freeblks) 6743 handle_workitem_freeblocks(freeblks, 0); 6744 trunc_pages(ip, length, extblocks, flags); 6745 } 6746 6747 /* 6748 * Eliminate pages from the page cache that back parts of this inode and 6749 * adjust the vnode pager's idea of our size. This prevents stale data 6750 * from hanging around in the page cache. 6751 */ 6752 static void 6753 trunc_pages(ip, length, extblocks, flags) 6754 struct inode *ip; 6755 off_t length; 6756 ufs2_daddr_t extblocks; 6757 int flags; 6758 { 6759 struct vnode *vp; 6760 struct fs *fs; 6761 ufs_lbn_t lbn; 6762 off_t end, extend; 6763 6764 vp = ITOV(ip); 6765 fs = ip->i_fs; 6766 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 6767 if ((flags & IO_EXT) != 0) 6768 vn_pages_remove(vp, extend, 0); 6769 if ((flags & IO_NORMAL) == 0) 6770 return; 6771 BO_LOCK(&vp->v_bufobj); 6772 drain_output(vp); 6773 BO_UNLOCK(&vp->v_bufobj); 6774 /* 6775 * The vnode pager eliminates file pages we eliminate indirects 6776 * below. 6777 */ 6778 vnode_pager_setsize(vp, length); 6779 /* 6780 * Calculate the end based on the last indirect we want to keep. If 6781 * the block extends into indirects we can just use the negative of 6782 * its lbn. Doubles and triples exist at lower numbers so we must 6783 * be careful not to remove those, if they exist. double and triple 6784 * indirect lbns do not overlap with others so it is not important 6785 * to verify how many levels are required. 6786 */ 6787 lbn = lblkno(fs, length); 6788 if (lbn >= NDADDR) { 6789 /* Calculate the virtual lbn of the triple indirect. */ 6790 lbn = -lbn - (NIADDR - 1); 6791 end = OFF_TO_IDX(lblktosize(fs, lbn)); 6792 } else 6793 end = extend; 6794 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 6795 } 6796 6797 /* 6798 * See if the buf bp is in the range eliminated by truncation. 6799 */ 6800 static int 6801 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 6802 struct buf *bp; 6803 int *blkoffp; 6804 ufs_lbn_t lastlbn; 6805 int lastoff; 6806 int flags; 6807 { 6808 ufs_lbn_t lbn; 6809 6810 *blkoffp = 0; 6811 /* Only match ext/normal blocks as appropriate. */ 6812 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 6813 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 6814 return (0); 6815 /* ALTDATA is always a full truncation. */ 6816 if ((bp->b_xflags & BX_ALTDATA) != 0) 6817 return (1); 6818 /* -1 is full truncation. */ 6819 if (lastlbn == -1) 6820 return (1); 6821 /* 6822 * If this is a partial truncate we only want those 6823 * blocks and indirect blocks that cover the range 6824 * we're after. 6825 */ 6826 lbn = bp->b_lblkno; 6827 if (lbn < 0) 6828 lbn = -(lbn + lbn_level(lbn)); 6829 if (lbn < lastlbn) 6830 return (0); 6831 /* Here we only truncate lblkno if it's partial. */ 6832 if (lbn == lastlbn) { 6833 if (lastoff == 0) 6834 return (0); 6835 *blkoffp = lastoff; 6836 } 6837 return (1); 6838 } 6839 6840 /* 6841 * Eliminate any dependencies that exist in memory beyond lblkno:off 6842 */ 6843 static void 6844 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 6845 struct inode *ip; 6846 struct freeblks *freeblks; 6847 ufs_lbn_t lastlbn; 6848 int lastoff; 6849 int flags; 6850 { 6851 struct bufobj *bo; 6852 struct vnode *vp; 6853 struct buf *bp; 6854 struct fs *fs; 6855 int blkoff; 6856 6857 /* 6858 * We must wait for any I/O in progress to finish so that 6859 * all potential buffers on the dirty list will be visible. 6860 * Once they are all there, walk the list and get rid of 6861 * any dependencies. 6862 */ 6863 fs = ip->i_fs; 6864 vp = ITOV(ip); 6865 bo = &vp->v_bufobj; 6866 BO_LOCK(bo); 6867 drain_output(vp); 6868 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 6869 bp->b_vflags &= ~BV_SCANNED; 6870 restart: 6871 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 6872 if (bp->b_vflags & BV_SCANNED) 6873 continue; 6874 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 6875 bp->b_vflags |= BV_SCANNED; 6876 continue; 6877 } 6878 if ((bp = getdirtybuf(bp, BO_LOCKPTR(bo), MNT_WAIT)) == NULL) 6879 goto restart; 6880 BO_UNLOCK(bo); 6881 if (deallocate_dependencies(bp, freeblks, blkoff)) 6882 bqrelse(bp); 6883 else 6884 brelse(bp); 6885 BO_LOCK(bo); 6886 goto restart; 6887 } 6888 /* 6889 * Now do the work of vtruncbuf while also matching indirect blocks. 6890 */ 6891 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 6892 bp->b_vflags &= ~BV_SCANNED; 6893 cleanrestart: 6894 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 6895 if (bp->b_vflags & BV_SCANNED) 6896 continue; 6897 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 6898 bp->b_vflags |= BV_SCANNED; 6899 continue; 6900 } 6901 if (BUF_LOCK(bp, 6902 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6903 BO_LOCKPTR(bo)) == ENOLCK) { 6904 BO_LOCK(bo); 6905 goto cleanrestart; 6906 } 6907 bp->b_vflags |= BV_SCANNED; 6908 bremfree(bp); 6909 if (blkoff != 0) { 6910 allocbuf(bp, blkoff); 6911 bqrelse(bp); 6912 } else { 6913 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 6914 brelse(bp); 6915 } 6916 BO_LOCK(bo); 6917 goto cleanrestart; 6918 } 6919 drain_output(vp); 6920 BO_UNLOCK(bo); 6921 } 6922 6923 static int 6924 cancel_pagedep(pagedep, freeblks, blkoff) 6925 struct pagedep *pagedep; 6926 struct freeblks *freeblks; 6927 int blkoff; 6928 { 6929 struct jremref *jremref; 6930 struct jmvref *jmvref; 6931 struct dirrem *dirrem, *tmp; 6932 int i; 6933 6934 /* 6935 * Copy any directory remove dependencies to the list 6936 * to be processed after the freeblks proceeds. If 6937 * directory entry never made it to disk they 6938 * can be dumped directly onto the work list. 6939 */ 6940 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 6941 /* Skip this directory removal if it is intended to remain. */ 6942 if (dirrem->dm_offset < blkoff) 6943 continue; 6944 /* 6945 * If there are any dirrems we wait for the journal write 6946 * to complete and then restart the buf scan as the lock 6947 * has been dropped. 6948 */ 6949 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 6950 jwait(&jremref->jr_list, MNT_WAIT); 6951 return (ERESTART); 6952 } 6953 LIST_REMOVE(dirrem, dm_next); 6954 dirrem->dm_dirinum = pagedep->pd_ino; 6955 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 6956 } 6957 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 6958 jwait(&jmvref->jm_list, MNT_WAIT); 6959 return (ERESTART); 6960 } 6961 /* 6962 * When we're partially truncating a pagedep we just want to flush 6963 * journal entries and return. There can not be any adds in the 6964 * truncated portion of the directory and newblk must remain if 6965 * part of the block remains. 6966 */ 6967 if (blkoff != 0) { 6968 struct diradd *dap; 6969 6970 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 6971 if (dap->da_offset > blkoff) 6972 panic("cancel_pagedep: diradd %p off %d > %d", 6973 dap, dap->da_offset, blkoff); 6974 for (i = 0; i < DAHASHSZ; i++) 6975 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 6976 if (dap->da_offset > blkoff) 6977 panic("cancel_pagedep: diradd %p off %d > %d", 6978 dap, dap->da_offset, blkoff); 6979 return (0); 6980 } 6981 /* 6982 * There should be no directory add dependencies present 6983 * as the directory could not be truncated until all 6984 * children were removed. 6985 */ 6986 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 6987 ("deallocate_dependencies: pendinghd != NULL")); 6988 for (i = 0; i < DAHASHSZ; i++) 6989 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 6990 ("deallocate_dependencies: diraddhd != NULL")); 6991 if ((pagedep->pd_state & NEWBLOCK) != 0) 6992 free_newdirblk(pagedep->pd_newdirblk); 6993 if (free_pagedep(pagedep) == 0) 6994 panic("Failed to free pagedep %p", pagedep); 6995 return (0); 6996 } 6997 6998 /* 6999 * Reclaim any dependency structures from a buffer that is about to 7000 * be reallocated to a new vnode. The buffer must be locked, thus, 7001 * no I/O completion operations can occur while we are manipulating 7002 * its associated dependencies. The mutex is held so that other I/O's 7003 * associated with related dependencies do not occur. 7004 */ 7005 static int 7006 deallocate_dependencies(bp, freeblks, off) 7007 struct buf *bp; 7008 struct freeblks *freeblks; 7009 int off; 7010 { 7011 struct indirdep *indirdep; 7012 struct pagedep *pagedep; 7013 struct allocdirect *adp; 7014 struct worklist *wk, *wkn; 7015 7016 ACQUIRE_LOCK(&lk); 7017 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7018 switch (wk->wk_type) { 7019 case D_INDIRDEP: 7020 indirdep = WK_INDIRDEP(wk); 7021 if (bp->b_lblkno >= 0 || 7022 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7023 panic("deallocate_dependencies: not indir"); 7024 cancel_indirdep(indirdep, bp, freeblks); 7025 continue; 7026 7027 case D_PAGEDEP: 7028 pagedep = WK_PAGEDEP(wk); 7029 if (cancel_pagedep(pagedep, freeblks, off)) { 7030 FREE_LOCK(&lk); 7031 return (ERESTART); 7032 } 7033 continue; 7034 7035 case D_ALLOCINDIR: 7036 /* 7037 * Simply remove the allocindir, we'll find it via 7038 * the indirdep where we can clear pointers if 7039 * needed. 7040 */ 7041 WORKLIST_REMOVE(wk); 7042 continue; 7043 7044 case D_FREEWORK: 7045 /* 7046 * A truncation is waiting for the zero'd pointers 7047 * to be written. It can be freed when the freeblks 7048 * is journaled. 7049 */ 7050 WORKLIST_REMOVE(wk); 7051 wk->wk_state |= ONDEPLIST; 7052 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7053 break; 7054 7055 case D_ALLOCDIRECT: 7056 adp = WK_ALLOCDIRECT(wk); 7057 if (off != 0) 7058 continue; 7059 /* FALLTHROUGH */ 7060 default: 7061 panic("deallocate_dependencies: Unexpected type %s", 7062 TYPENAME(wk->wk_type)); 7063 /* NOTREACHED */ 7064 } 7065 } 7066 FREE_LOCK(&lk); 7067 /* 7068 * Don't throw away this buf, we were partially truncating and 7069 * some deps may always remain. 7070 */ 7071 if (off) { 7072 allocbuf(bp, off); 7073 bp->b_vflags |= BV_SCANNED; 7074 return (EBUSY); 7075 } 7076 bp->b_flags |= B_INVAL | B_NOCACHE; 7077 7078 return (0); 7079 } 7080 7081 /* 7082 * An allocdirect is being canceled due to a truncate. We must make sure 7083 * the journal entry is released in concert with the blkfree that releases 7084 * the storage. Completed journal entries must not be released until the 7085 * space is no longer pointed to by the inode or in the bitmap. 7086 */ 7087 static void 7088 cancel_allocdirect(adphead, adp, freeblks) 7089 struct allocdirectlst *adphead; 7090 struct allocdirect *adp; 7091 struct freeblks *freeblks; 7092 { 7093 struct freework *freework; 7094 struct newblk *newblk; 7095 struct worklist *wk; 7096 7097 TAILQ_REMOVE(adphead, adp, ad_next); 7098 newblk = (struct newblk *)adp; 7099 freework = NULL; 7100 /* 7101 * Find the correct freework structure. 7102 */ 7103 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7104 if (wk->wk_type != D_FREEWORK) 7105 continue; 7106 freework = WK_FREEWORK(wk); 7107 if (freework->fw_blkno == newblk->nb_newblkno) 7108 break; 7109 } 7110 if (freework == NULL) 7111 panic("cancel_allocdirect: Freework not found"); 7112 /* 7113 * If a newblk exists at all we still have the journal entry that 7114 * initiated the allocation so we do not need to journal the free. 7115 */ 7116 cancel_jfreeblk(freeblks, freework->fw_blkno); 7117 /* 7118 * If the journal hasn't been written the jnewblk must be passed 7119 * to the call to ffs_blkfree that reclaims the space. We accomplish 7120 * this by linking the journal dependency into the freework to be 7121 * freed when freework_freeblock() is called. If the journal has 7122 * been written we can simply reclaim the journal space when the 7123 * freeblks work is complete. 7124 */ 7125 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7126 &freeblks->fb_jwork); 7127 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7128 } 7129 7130 7131 /* 7132 * Cancel a new block allocation. May be an indirect or direct block. We 7133 * remove it from various lists and return any journal record that needs to 7134 * be resolved by the caller. 7135 * 7136 * A special consideration is made for indirects which were never pointed 7137 * at on disk and will never be found once this block is released. 7138 */ 7139 static struct jnewblk * 7140 cancel_newblk(newblk, wk, wkhd) 7141 struct newblk *newblk; 7142 struct worklist *wk; 7143 struct workhead *wkhd; 7144 { 7145 struct jnewblk *jnewblk; 7146 7147 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7148 7149 newblk->nb_state |= GOINGAWAY; 7150 /* 7151 * Previously we traversed the completedhd on each indirdep 7152 * attached to this newblk to cancel them and gather journal 7153 * work. Since we need only the oldest journal segment and 7154 * the lowest point on the tree will always have the oldest 7155 * journal segment we are free to release the segments 7156 * of any subordinates and may leave the indirdep list to 7157 * indirdep_complete() when this newblk is freed. 7158 */ 7159 if (newblk->nb_state & ONDEPLIST) { 7160 newblk->nb_state &= ~ONDEPLIST; 7161 LIST_REMOVE(newblk, nb_deps); 7162 } 7163 if (newblk->nb_state & ONWORKLIST) 7164 WORKLIST_REMOVE(&newblk->nb_list); 7165 /* 7166 * If the journal entry hasn't been written we save a pointer to 7167 * the dependency that frees it until it is written or the 7168 * superseding operation completes. 7169 */ 7170 jnewblk = newblk->nb_jnewblk; 7171 if (jnewblk != NULL && wk != NULL) { 7172 newblk->nb_jnewblk = NULL; 7173 jnewblk->jn_dep = wk; 7174 } 7175 if (!LIST_EMPTY(&newblk->nb_jwork)) 7176 jwork_move(wkhd, &newblk->nb_jwork); 7177 /* 7178 * When truncating we must free the newdirblk early to remove 7179 * the pagedep from the hash before returning. 7180 */ 7181 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7182 free_newdirblk(WK_NEWDIRBLK(wk)); 7183 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7184 panic("cancel_newblk: extra newdirblk"); 7185 7186 return (jnewblk); 7187 } 7188 7189 /* 7190 * Schedule the freefrag associated with a newblk to be released once 7191 * the pointers are written and the previous block is no longer needed. 7192 */ 7193 static void 7194 newblk_freefrag(newblk) 7195 struct newblk *newblk; 7196 { 7197 struct freefrag *freefrag; 7198 7199 if (newblk->nb_freefrag == NULL) 7200 return; 7201 freefrag = newblk->nb_freefrag; 7202 newblk->nb_freefrag = NULL; 7203 freefrag->ff_state |= COMPLETE; 7204 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7205 add_to_worklist(&freefrag->ff_list, 0); 7206 } 7207 7208 /* 7209 * Free a newblk. Generate a new freefrag work request if appropriate. 7210 * This must be called after the inode pointer and any direct block pointers 7211 * are valid or fully removed via truncate or frag extension. 7212 */ 7213 static void 7214 free_newblk(newblk) 7215 struct newblk *newblk; 7216 { 7217 struct indirdep *indirdep; 7218 struct worklist *wk; 7219 7220 KASSERT(newblk->nb_jnewblk == NULL, 7221 ("free_newblk; jnewblk %p still attached", newblk->nb_jnewblk)); 7222 rw_assert(&lk, RA_WLOCKED); 7223 newblk_freefrag(newblk); 7224 if (newblk->nb_state & ONDEPLIST) 7225 LIST_REMOVE(newblk, nb_deps); 7226 if (newblk->nb_state & ONWORKLIST) 7227 WORKLIST_REMOVE(&newblk->nb_list); 7228 LIST_REMOVE(newblk, nb_hash); 7229 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7230 free_newdirblk(WK_NEWDIRBLK(wk)); 7231 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7232 panic("free_newblk: extra newdirblk"); 7233 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7234 indirdep_complete(indirdep); 7235 handle_jwork(&newblk->nb_jwork); 7236 newblk->nb_list.wk_type = D_NEWBLK; 7237 WORKITEM_FREE(newblk, D_NEWBLK); 7238 } 7239 7240 /* 7241 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7242 * This routine must be called with splbio interrupts blocked. 7243 */ 7244 static void 7245 free_newdirblk(newdirblk) 7246 struct newdirblk *newdirblk; 7247 { 7248 struct pagedep *pagedep; 7249 struct diradd *dap; 7250 struct worklist *wk; 7251 7252 rw_assert(&lk, RA_WLOCKED); 7253 WORKLIST_REMOVE(&newdirblk->db_list); 7254 /* 7255 * If the pagedep is still linked onto the directory buffer 7256 * dependency chain, then some of the entries on the 7257 * pd_pendinghd list may not be committed to disk yet. In 7258 * this case, we will simply clear the NEWBLOCK flag and 7259 * let the pd_pendinghd list be processed when the pagedep 7260 * is next written. If the pagedep is no longer on the buffer 7261 * dependency chain, then all the entries on the pd_pending 7262 * list are committed to disk and we can free them here. 7263 */ 7264 pagedep = newdirblk->db_pagedep; 7265 pagedep->pd_state &= ~NEWBLOCK; 7266 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7267 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7268 free_diradd(dap, NULL); 7269 /* 7270 * If no dependencies remain, the pagedep will be freed. 7271 */ 7272 free_pagedep(pagedep); 7273 } 7274 /* Should only ever be one item in the list. */ 7275 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7276 WORKLIST_REMOVE(wk); 7277 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7278 } 7279 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7280 } 7281 7282 /* 7283 * Prepare an inode to be freed. The actual free operation is not 7284 * done until the zero'ed inode has been written to disk. 7285 */ 7286 void 7287 softdep_freefile(pvp, ino, mode) 7288 struct vnode *pvp; 7289 ino_t ino; 7290 int mode; 7291 { 7292 struct inode *ip = VTOI(pvp); 7293 struct inodedep *inodedep; 7294 struct freefile *freefile; 7295 struct freeblks *freeblks; 7296 7297 /* 7298 * This sets up the inode de-allocation dependency. 7299 */ 7300 freefile = malloc(sizeof(struct freefile), 7301 M_FREEFILE, M_SOFTDEP_FLAGS); 7302 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7303 freefile->fx_mode = mode; 7304 freefile->fx_oldinum = ino; 7305 freefile->fx_devvp = ip->i_devvp; 7306 LIST_INIT(&freefile->fx_jwork); 7307 UFS_LOCK(ip->i_ump); 7308 ip->i_fs->fs_pendinginodes += 1; 7309 UFS_UNLOCK(ip->i_ump); 7310 7311 /* 7312 * If the inodedep does not exist, then the zero'ed inode has 7313 * been written to disk. If the allocated inode has never been 7314 * written to disk, then the on-disk inode is zero'ed. In either 7315 * case we can free the file immediately. If the journal was 7316 * canceled before being written the inode will never make it to 7317 * disk and we must send the canceled journal entrys to 7318 * ffs_freefile() to be cleared in conjunction with the bitmap. 7319 * Any blocks waiting on the inode to write can be safely freed 7320 * here as it will never been written. 7321 */ 7322 ACQUIRE_LOCK(&lk); 7323 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7324 if (inodedep) { 7325 /* 7326 * Clear out freeblks that no longer need to reference 7327 * this inode. 7328 */ 7329 while ((freeblks = 7330 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7331 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7332 fb_next); 7333 freeblks->fb_state &= ~ONDEPLIST; 7334 } 7335 /* 7336 * Remove this inode from the unlinked list. 7337 */ 7338 if (inodedep->id_state & UNLINKED) { 7339 /* 7340 * Save the journal work to be freed with the bitmap 7341 * before we clear UNLINKED. Otherwise it can be lost 7342 * if the inode block is written. 7343 */ 7344 handle_bufwait(inodedep, &freefile->fx_jwork); 7345 clear_unlinked_inodedep(inodedep); 7346 /* Re-acquire inodedep as we've dropped lk. */ 7347 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7348 } 7349 } 7350 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7351 FREE_LOCK(&lk); 7352 handle_workitem_freefile(freefile); 7353 return; 7354 } 7355 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7356 inodedep->id_state |= GOINGAWAY; 7357 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7358 FREE_LOCK(&lk); 7359 if (ip->i_number == ino) 7360 ip->i_flag |= IN_MODIFIED; 7361 } 7362 7363 /* 7364 * Check to see if an inode has never been written to disk. If 7365 * so free the inodedep and return success, otherwise return failure. 7366 * This routine must be called with splbio interrupts blocked. 7367 * 7368 * If we still have a bitmap dependency, then the inode has never 7369 * been written to disk. Drop the dependency as it is no longer 7370 * necessary since the inode is being deallocated. We set the 7371 * ALLCOMPLETE flags since the bitmap now properly shows that the 7372 * inode is not allocated. Even if the inode is actively being 7373 * written, it has been rolled back to its zero'ed state, so we 7374 * are ensured that a zero inode is what is on the disk. For short 7375 * lived files, this change will usually result in removing all the 7376 * dependencies from the inode so that it can be freed immediately. 7377 */ 7378 static int 7379 check_inode_unwritten(inodedep) 7380 struct inodedep *inodedep; 7381 { 7382 7383 rw_assert(&lk, RA_WLOCKED); 7384 7385 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7386 !LIST_EMPTY(&inodedep->id_dirremhd) || 7387 !LIST_EMPTY(&inodedep->id_pendinghd) || 7388 !LIST_EMPTY(&inodedep->id_bufwait) || 7389 !LIST_EMPTY(&inodedep->id_inowait) || 7390 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7391 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7392 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7393 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7394 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7395 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7396 inodedep->id_mkdiradd != NULL || 7397 inodedep->id_nlinkdelta != 0) 7398 return (0); 7399 /* 7400 * Another process might be in initiate_write_inodeblock_ufs[12] 7401 * trying to allocate memory without holding "Softdep Lock". 7402 */ 7403 if ((inodedep->id_state & IOSTARTED) != 0 && 7404 inodedep->id_savedino1 == NULL) 7405 return (0); 7406 7407 if (inodedep->id_state & ONDEPLIST) 7408 LIST_REMOVE(inodedep, id_deps); 7409 inodedep->id_state &= ~ONDEPLIST; 7410 inodedep->id_state |= ALLCOMPLETE; 7411 inodedep->id_bmsafemap = NULL; 7412 if (inodedep->id_state & ONWORKLIST) 7413 WORKLIST_REMOVE(&inodedep->id_list); 7414 if (inodedep->id_savedino1 != NULL) { 7415 free(inodedep->id_savedino1, M_SAVEDINO); 7416 inodedep->id_savedino1 = NULL; 7417 } 7418 if (free_inodedep(inodedep) == 0) 7419 panic("check_inode_unwritten: busy inode"); 7420 return (1); 7421 } 7422 7423 /* 7424 * Try to free an inodedep structure. Return 1 if it could be freed. 7425 */ 7426 static int 7427 free_inodedep(inodedep) 7428 struct inodedep *inodedep; 7429 { 7430 7431 rw_assert(&lk, RA_WLOCKED); 7432 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7433 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7434 !LIST_EMPTY(&inodedep->id_dirremhd) || 7435 !LIST_EMPTY(&inodedep->id_pendinghd) || 7436 !LIST_EMPTY(&inodedep->id_bufwait) || 7437 !LIST_EMPTY(&inodedep->id_inowait) || 7438 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7439 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7440 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7441 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7442 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7443 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7444 inodedep->id_mkdiradd != NULL || 7445 inodedep->id_nlinkdelta != 0 || 7446 inodedep->id_savedino1 != NULL) 7447 return (0); 7448 if (inodedep->id_state & ONDEPLIST) 7449 LIST_REMOVE(inodedep, id_deps); 7450 LIST_REMOVE(inodedep, id_hash); 7451 WORKITEM_FREE(inodedep, D_INODEDEP); 7452 return (1); 7453 } 7454 7455 /* 7456 * Free the block referenced by a freework structure. The parent freeblks 7457 * structure is released and completed when the final cg bitmap reaches 7458 * the disk. This routine may be freeing a jnewblk which never made it to 7459 * disk in which case we do not have to wait as the operation is undone 7460 * in memory immediately. 7461 */ 7462 static void 7463 freework_freeblock(freework) 7464 struct freework *freework; 7465 { 7466 struct freeblks *freeblks; 7467 struct jnewblk *jnewblk; 7468 struct ufsmount *ump; 7469 struct workhead wkhd; 7470 struct fs *fs; 7471 int bsize; 7472 int needj; 7473 7474 rw_assert(&lk, RA_WLOCKED); 7475 /* 7476 * Handle partial truncate separately. 7477 */ 7478 if (freework->fw_indir) { 7479 complete_trunc_indir(freework); 7480 return; 7481 } 7482 freeblks = freework->fw_freeblks; 7483 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7484 fs = ump->um_fs; 7485 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7486 bsize = lfragtosize(fs, freework->fw_frags); 7487 LIST_INIT(&wkhd); 7488 /* 7489 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7490 * on the indirblk hashtable and prevents premature freeing. 7491 */ 7492 freework->fw_state |= DEPCOMPLETE; 7493 /* 7494 * SUJ needs to wait for the segment referencing freed indirect 7495 * blocks to expire so that we know the checker will not confuse 7496 * a re-allocated indirect block with its old contents. 7497 */ 7498 if (needj && freework->fw_lbn <= -NDADDR) 7499 indirblk_insert(freework); 7500 /* 7501 * If we are canceling an existing jnewblk pass it to the free 7502 * routine, otherwise pass the freeblk which will ultimately 7503 * release the freeblks. If we're not journaling, we can just 7504 * free the freeblks immediately. 7505 */ 7506 jnewblk = freework->fw_jnewblk; 7507 if (jnewblk != NULL) { 7508 cancel_jnewblk(jnewblk, &wkhd); 7509 needj = 0; 7510 } else if (needj) { 7511 freework->fw_state |= DELAYEDFREE; 7512 freeblks->fb_cgwait++; 7513 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7514 } 7515 FREE_LOCK(&lk); 7516 freeblks_free(ump, freeblks, btodb(bsize)); 7517 CTR4(KTR_SUJ, 7518 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld", 7519 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7520 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7521 freeblks->fb_inum, freeblks->fb_vtype, &wkhd); 7522 ACQUIRE_LOCK(&lk); 7523 /* 7524 * The jnewblk will be discarded and the bits in the map never 7525 * made it to disk. We can immediately free the freeblk. 7526 */ 7527 if (needj == 0) 7528 handle_written_freework(freework); 7529 } 7530 7531 /* 7532 * We enqueue freework items that need processing back on the freeblks and 7533 * add the freeblks to the worklist. This makes it easier to find all work 7534 * required to flush a truncation in process_truncates(). 7535 */ 7536 static void 7537 freework_enqueue(freework) 7538 struct freework *freework; 7539 { 7540 struct freeblks *freeblks; 7541 7542 freeblks = freework->fw_freeblks; 7543 if ((freework->fw_state & INPROGRESS) == 0) 7544 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7545 if ((freeblks->fb_state & 7546 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7547 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7548 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7549 } 7550 7551 /* 7552 * Start, continue, or finish the process of freeing an indirect block tree. 7553 * The free operation may be paused at any point with fw_off containing the 7554 * offset to restart from. This enables us to implement some flow control 7555 * for large truncates which may fan out and generate a huge number of 7556 * dependencies. 7557 */ 7558 static void 7559 handle_workitem_indirblk(freework) 7560 struct freework *freework; 7561 { 7562 struct freeblks *freeblks; 7563 struct ufsmount *ump; 7564 struct fs *fs; 7565 7566 freeblks = freework->fw_freeblks; 7567 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7568 fs = ump->um_fs; 7569 if (freework->fw_state & DEPCOMPLETE) { 7570 handle_written_freework(freework); 7571 return; 7572 } 7573 if (freework->fw_off == NINDIR(fs)) { 7574 freework_freeblock(freework); 7575 return; 7576 } 7577 freework->fw_state |= INPROGRESS; 7578 FREE_LOCK(&lk); 7579 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7580 freework->fw_lbn); 7581 ACQUIRE_LOCK(&lk); 7582 } 7583 7584 /* 7585 * Called when a freework structure attached to a cg buf is written. The 7586 * ref on either the parent or the freeblks structure is released and 7587 * the freeblks is added back to the worklist if there is more work to do. 7588 */ 7589 static void 7590 handle_written_freework(freework) 7591 struct freework *freework; 7592 { 7593 struct freeblks *freeblks; 7594 struct freework *parent; 7595 7596 freeblks = freework->fw_freeblks; 7597 parent = freework->fw_parent; 7598 if (freework->fw_state & DELAYEDFREE) 7599 freeblks->fb_cgwait--; 7600 freework->fw_state |= COMPLETE; 7601 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7602 WORKITEM_FREE(freework, D_FREEWORK); 7603 if (parent) { 7604 if (--parent->fw_ref == 0) 7605 freework_enqueue(parent); 7606 return; 7607 } 7608 if (--freeblks->fb_ref != 0) 7609 return; 7610 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7611 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7612 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7613 } 7614 7615 /* 7616 * This workitem routine performs the block de-allocation. 7617 * The workitem is added to the pending list after the updated 7618 * inode block has been written to disk. As mentioned above, 7619 * checks regarding the number of blocks de-allocated (compared 7620 * to the number of blocks allocated for the file) are also 7621 * performed in this function. 7622 */ 7623 static int 7624 handle_workitem_freeblocks(freeblks, flags) 7625 struct freeblks *freeblks; 7626 int flags; 7627 { 7628 struct freework *freework; 7629 struct newblk *newblk; 7630 struct allocindir *aip; 7631 struct ufsmount *ump; 7632 struct worklist *wk; 7633 7634 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7635 ("handle_workitem_freeblocks: Journal entries not written.")); 7636 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7637 ACQUIRE_LOCK(&lk); 7638 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7639 WORKLIST_REMOVE(wk); 7640 switch (wk->wk_type) { 7641 case D_DIRREM: 7642 wk->wk_state |= COMPLETE; 7643 add_to_worklist(wk, 0); 7644 continue; 7645 7646 case D_ALLOCDIRECT: 7647 free_newblk(WK_NEWBLK(wk)); 7648 continue; 7649 7650 case D_ALLOCINDIR: 7651 aip = WK_ALLOCINDIR(wk); 7652 freework = NULL; 7653 if (aip->ai_state & DELAYEDFREE) { 7654 FREE_LOCK(&lk); 7655 freework = newfreework(ump, freeblks, NULL, 7656 aip->ai_lbn, aip->ai_newblkno, 7657 ump->um_fs->fs_frag, 0, 0); 7658 ACQUIRE_LOCK(&lk); 7659 } 7660 newblk = WK_NEWBLK(wk); 7661 if (newblk->nb_jnewblk) { 7662 freework->fw_jnewblk = newblk->nb_jnewblk; 7663 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7664 newblk->nb_jnewblk = NULL; 7665 } 7666 free_newblk(newblk); 7667 continue; 7668 7669 case D_FREEWORK: 7670 freework = WK_FREEWORK(wk); 7671 if (freework->fw_lbn <= -NDADDR) 7672 handle_workitem_indirblk(freework); 7673 else 7674 freework_freeblock(freework); 7675 continue; 7676 default: 7677 panic("handle_workitem_freeblocks: Unknown type %s", 7678 TYPENAME(wk->wk_type)); 7679 } 7680 } 7681 if (freeblks->fb_ref != 0) { 7682 freeblks->fb_state &= ~INPROGRESS; 7683 wake_worklist(&freeblks->fb_list); 7684 freeblks = NULL; 7685 } 7686 FREE_LOCK(&lk); 7687 if (freeblks) 7688 return handle_complete_freeblocks(freeblks, flags); 7689 return (0); 7690 } 7691 7692 /* 7693 * Handle completion of block free via truncate. This allows fs_pending 7694 * to track the actual free block count more closely than if we only updated 7695 * it at the end. We must be careful to handle cases where the block count 7696 * on free was incorrect. 7697 */ 7698 static void 7699 freeblks_free(ump, freeblks, blocks) 7700 struct ufsmount *ump; 7701 struct freeblks *freeblks; 7702 int blocks; 7703 { 7704 struct fs *fs; 7705 ufs2_daddr_t remain; 7706 7707 UFS_LOCK(ump); 7708 remain = -freeblks->fb_chkcnt; 7709 freeblks->fb_chkcnt += blocks; 7710 if (remain > 0) { 7711 if (remain < blocks) 7712 blocks = remain; 7713 fs = ump->um_fs; 7714 fs->fs_pendingblocks -= blocks; 7715 } 7716 UFS_UNLOCK(ump); 7717 } 7718 7719 /* 7720 * Once all of the freework workitems are complete we can retire the 7721 * freeblocks dependency and any journal work awaiting completion. This 7722 * can not be called until all other dependencies are stable on disk. 7723 */ 7724 static int 7725 handle_complete_freeblocks(freeblks, flags) 7726 struct freeblks *freeblks; 7727 int flags; 7728 { 7729 struct inodedep *inodedep; 7730 struct inode *ip; 7731 struct vnode *vp; 7732 struct fs *fs; 7733 struct ufsmount *ump; 7734 ufs2_daddr_t spare; 7735 7736 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7737 fs = ump->um_fs; 7738 flags = LK_EXCLUSIVE | flags; 7739 spare = freeblks->fb_chkcnt; 7740 7741 /* 7742 * If we did not release the expected number of blocks we may have 7743 * to adjust the inode block count here. Only do so if it wasn't 7744 * a truncation to zero and the modrev still matches. 7745 */ 7746 if (spare && freeblks->fb_len != 0) { 7747 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7748 flags, &vp, FFSV_FORCEINSMQ) != 0) 7749 return (EBUSY); 7750 ip = VTOI(vp); 7751 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 7752 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 7753 ip->i_flag |= IN_CHANGE; 7754 /* 7755 * We must wait so this happens before the 7756 * journal is reclaimed. 7757 */ 7758 ffs_update(vp, 1); 7759 } 7760 vput(vp); 7761 } 7762 if (spare < 0) { 7763 UFS_LOCK(ump); 7764 fs->fs_pendingblocks += spare; 7765 UFS_UNLOCK(ump); 7766 } 7767 #ifdef QUOTA 7768 /* Handle spare. */ 7769 if (spare) 7770 quotaadj(freeblks->fb_quota, ump, -spare); 7771 quotarele(freeblks->fb_quota); 7772 #endif 7773 ACQUIRE_LOCK(&lk); 7774 if (freeblks->fb_state & ONDEPLIST) { 7775 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7776 0, &inodedep); 7777 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 7778 freeblks->fb_state &= ~ONDEPLIST; 7779 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 7780 free_inodedep(inodedep); 7781 } 7782 /* 7783 * All of the freeblock deps must be complete prior to this call 7784 * so it's now safe to complete earlier outstanding journal entries. 7785 */ 7786 handle_jwork(&freeblks->fb_jwork); 7787 WORKITEM_FREE(freeblks, D_FREEBLKS); 7788 FREE_LOCK(&lk); 7789 return (0); 7790 } 7791 7792 /* 7793 * Release blocks associated with the freeblks and stored in the indirect 7794 * block dbn. If level is greater than SINGLE, the block is an indirect block 7795 * and recursive calls to indirtrunc must be used to cleanse other indirect 7796 * blocks. 7797 * 7798 * This handles partial and complete truncation of blocks. Partial is noted 7799 * with goingaway == 0. In this case the freework is completed after the 7800 * zero'd indirects are written to disk. For full truncation the freework 7801 * is completed after the block is freed. 7802 */ 7803 static void 7804 indir_trunc(freework, dbn, lbn) 7805 struct freework *freework; 7806 ufs2_daddr_t dbn; 7807 ufs_lbn_t lbn; 7808 { 7809 struct freework *nfreework; 7810 struct workhead wkhd; 7811 struct freeblks *freeblks; 7812 struct buf *bp; 7813 struct fs *fs; 7814 struct indirdep *indirdep; 7815 struct ufsmount *ump; 7816 ufs1_daddr_t *bap1 = 0; 7817 ufs2_daddr_t nb, nnb, *bap2 = 0; 7818 ufs_lbn_t lbnadd, nlbn; 7819 int i, nblocks, ufs1fmt; 7820 int freedblocks; 7821 int goingaway; 7822 int freedeps; 7823 int needj; 7824 int level; 7825 int cnt; 7826 7827 freeblks = freework->fw_freeblks; 7828 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7829 fs = ump->um_fs; 7830 /* 7831 * Get buffer of block pointers to be freed. There are three cases: 7832 * 7833 * 1) Partial truncate caches the indirdep pointer in the freework 7834 * which provides us a back copy to the save bp which holds the 7835 * pointers we want to clear. When this completes the zero 7836 * pointers are written to the real copy. 7837 * 2) The indirect is being completely truncated, cancel_indirdep() 7838 * eliminated the real copy and placed the indirdep on the saved 7839 * copy. The indirdep and buf are discarded when this completes. 7840 * 3) The indirect was not in memory, we read a copy off of the disk 7841 * using the devvp and drop and invalidate the buffer when we're 7842 * done. 7843 */ 7844 goingaway = 1; 7845 indirdep = NULL; 7846 if (freework->fw_indir != NULL) { 7847 goingaway = 0; 7848 indirdep = freework->fw_indir; 7849 bp = indirdep->ir_savebp; 7850 if (bp == NULL || bp->b_blkno != dbn) 7851 panic("indir_trunc: Bad saved buf %p blkno %jd", 7852 bp, (intmax_t)dbn); 7853 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 7854 /* 7855 * The lock prevents the buf dep list from changing and 7856 * indirects on devvp should only ever have one dependency. 7857 */ 7858 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 7859 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 7860 panic("indir_trunc: Bad indirdep %p from buf %p", 7861 indirdep, bp); 7862 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 7863 NOCRED, &bp) != 0) { 7864 brelse(bp); 7865 return; 7866 } 7867 ACQUIRE_LOCK(&lk); 7868 /* Protects against a race with complete_trunc_indir(). */ 7869 freework->fw_state &= ~INPROGRESS; 7870 /* 7871 * If we have an indirdep we need to enforce the truncation order 7872 * and discard it when it is complete. 7873 */ 7874 if (indirdep) { 7875 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 7876 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 7877 /* 7878 * Add the complete truncate to the list on the 7879 * indirdep to enforce in-order processing. 7880 */ 7881 if (freework->fw_indir == NULL) 7882 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 7883 freework, fw_next); 7884 FREE_LOCK(&lk); 7885 return; 7886 } 7887 /* 7888 * If we're goingaway, free the indirdep. Otherwise it will 7889 * linger until the write completes. 7890 */ 7891 if (goingaway) { 7892 free_indirdep(indirdep); 7893 ump->um_numindirdeps -= 1; 7894 } 7895 } 7896 FREE_LOCK(&lk); 7897 /* Initialize pointers depending on block size. */ 7898 if (ump->um_fstype == UFS1) { 7899 bap1 = (ufs1_daddr_t *)bp->b_data; 7900 nb = bap1[freework->fw_off]; 7901 ufs1fmt = 1; 7902 } else { 7903 bap2 = (ufs2_daddr_t *)bp->b_data; 7904 nb = bap2[freework->fw_off]; 7905 ufs1fmt = 0; 7906 } 7907 level = lbn_level(lbn); 7908 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 7909 lbnadd = lbn_offset(fs, level); 7910 nblocks = btodb(fs->fs_bsize); 7911 nfreework = freework; 7912 freedeps = 0; 7913 cnt = 0; 7914 /* 7915 * Reclaim blocks. Traverses into nested indirect levels and 7916 * arranges for the current level to be freed when subordinates 7917 * are free when journaling. 7918 */ 7919 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 7920 if (i != NINDIR(fs) - 1) { 7921 if (ufs1fmt) 7922 nnb = bap1[i+1]; 7923 else 7924 nnb = bap2[i+1]; 7925 } else 7926 nnb = 0; 7927 if (nb == 0) 7928 continue; 7929 cnt++; 7930 if (level != 0) { 7931 nlbn = (lbn + 1) - (i * lbnadd); 7932 if (needj != 0) { 7933 nfreework = newfreework(ump, freeblks, freework, 7934 nlbn, nb, fs->fs_frag, 0, 0); 7935 freedeps++; 7936 } 7937 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 7938 } else { 7939 struct freedep *freedep; 7940 7941 /* 7942 * Attempt to aggregate freedep dependencies for 7943 * all blocks being released to the same CG. 7944 */ 7945 LIST_INIT(&wkhd); 7946 if (needj != 0 && 7947 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 7948 freedep = newfreedep(freework); 7949 WORKLIST_INSERT_UNLOCKED(&wkhd, 7950 &freedep->fd_list); 7951 freedeps++; 7952 } 7953 CTR3(KTR_SUJ, 7954 "indir_trunc: ino %d blkno %jd size %ld", 7955 freeblks->fb_inum, nb, fs->fs_bsize); 7956 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 7957 fs->fs_bsize, freeblks->fb_inum, 7958 freeblks->fb_vtype, &wkhd); 7959 } 7960 } 7961 if (goingaway) { 7962 bp->b_flags |= B_INVAL | B_NOCACHE; 7963 brelse(bp); 7964 } 7965 freedblocks = 0; 7966 if (level == 0) 7967 freedblocks = (nblocks * cnt); 7968 if (needj == 0) 7969 freedblocks += nblocks; 7970 freeblks_free(ump, freeblks, freedblocks); 7971 /* 7972 * If we are journaling set up the ref counts and offset so this 7973 * indirect can be completed when its children are free. 7974 */ 7975 if (needj) { 7976 ACQUIRE_LOCK(&lk); 7977 freework->fw_off = i; 7978 freework->fw_ref += freedeps; 7979 freework->fw_ref -= NINDIR(fs) + 1; 7980 if (level == 0) 7981 freeblks->fb_cgwait += freedeps; 7982 if (freework->fw_ref == 0) 7983 freework_freeblock(freework); 7984 FREE_LOCK(&lk); 7985 return; 7986 } 7987 /* 7988 * If we're not journaling we can free the indirect now. 7989 */ 7990 dbn = dbtofsb(fs, dbn); 7991 CTR3(KTR_SUJ, 7992 "indir_trunc 2: ino %d blkno %jd size %ld", 7993 freeblks->fb_inum, dbn, fs->fs_bsize); 7994 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 7995 freeblks->fb_inum, freeblks->fb_vtype, NULL); 7996 /* Non SUJ softdep does single-threaded truncations. */ 7997 if (freework->fw_blkno == dbn) { 7998 freework->fw_state |= ALLCOMPLETE; 7999 ACQUIRE_LOCK(&lk); 8000 handle_written_freework(freework); 8001 FREE_LOCK(&lk); 8002 } 8003 return; 8004 } 8005 8006 /* 8007 * Cancel an allocindir when it is removed via truncation. When bp is not 8008 * NULL the indirect never appeared on disk and is scheduled to be freed 8009 * independently of the indir so we can more easily track journal work. 8010 */ 8011 static void 8012 cancel_allocindir(aip, bp, freeblks, trunc) 8013 struct allocindir *aip; 8014 struct buf *bp; 8015 struct freeblks *freeblks; 8016 int trunc; 8017 { 8018 struct indirdep *indirdep; 8019 struct freefrag *freefrag; 8020 struct newblk *newblk; 8021 8022 newblk = (struct newblk *)aip; 8023 LIST_REMOVE(aip, ai_next); 8024 /* 8025 * We must eliminate the pointer in bp if it must be freed on its 8026 * own due to partial truncate or pending journal work. 8027 */ 8028 if (bp && (trunc || newblk->nb_jnewblk)) { 8029 /* 8030 * Clear the pointer and mark the aip to be freed 8031 * directly if it never existed on disk. 8032 */ 8033 aip->ai_state |= DELAYEDFREE; 8034 indirdep = aip->ai_indirdep; 8035 if (indirdep->ir_state & UFS1FMT) 8036 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8037 else 8038 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8039 } 8040 /* 8041 * When truncating the previous pointer will be freed via 8042 * savedbp. Eliminate the freefrag which would dup free. 8043 */ 8044 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8045 newblk->nb_freefrag = NULL; 8046 if (freefrag->ff_jdep) 8047 cancel_jfreefrag( 8048 WK_JFREEFRAG(freefrag->ff_jdep)); 8049 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8050 WORKITEM_FREE(freefrag, D_FREEFRAG); 8051 } 8052 /* 8053 * If the journal hasn't been written the jnewblk must be passed 8054 * to the call to ffs_blkfree that reclaims the space. We accomplish 8055 * this by leaving the journal dependency on the newblk to be freed 8056 * when a freework is created in handle_workitem_freeblocks(). 8057 */ 8058 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8059 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8060 } 8061 8062 /* 8063 * Create the mkdir dependencies for . and .. in a new directory. Link them 8064 * in to a newdirblk so any subsequent additions are tracked properly. The 8065 * caller is responsible for adding the mkdir1 dependency to the journal 8066 * and updating id_mkdiradd. This function returns with lk held. 8067 */ 8068 static struct mkdir * 8069 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8070 struct diradd *dap; 8071 ino_t newinum; 8072 ino_t dinum; 8073 struct buf *newdirbp; 8074 struct mkdir **mkdirp; 8075 { 8076 struct newblk *newblk; 8077 struct pagedep *pagedep; 8078 struct inodedep *inodedep; 8079 struct newdirblk *newdirblk = 0; 8080 struct mkdir *mkdir1, *mkdir2; 8081 struct worklist *wk; 8082 struct jaddref *jaddref; 8083 struct mount *mp; 8084 8085 mp = dap->da_list.wk_mp; 8086 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8087 M_SOFTDEP_FLAGS); 8088 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8089 LIST_INIT(&newdirblk->db_mkdir); 8090 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8091 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8092 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8093 mkdir1->md_diradd = dap; 8094 mkdir1->md_jaddref = NULL; 8095 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8096 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8097 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8098 mkdir2->md_diradd = dap; 8099 mkdir2->md_jaddref = NULL; 8100 if (MOUNTEDSUJ(mp) == 0) { 8101 mkdir1->md_state |= DEPCOMPLETE; 8102 mkdir2->md_state |= DEPCOMPLETE; 8103 } 8104 /* 8105 * Dependency on "." and ".." being written to disk. 8106 */ 8107 mkdir1->md_buf = newdirbp; 8108 ACQUIRE_LOCK(&lk); 8109 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs); 8110 /* 8111 * We must link the pagedep, allocdirect, and newdirblk for 8112 * the initial file page so the pointer to the new directory 8113 * is not written until the directory contents are live and 8114 * any subsequent additions are not marked live until the 8115 * block is reachable via the inode. 8116 */ 8117 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8118 panic("setup_newdir: lost pagedep"); 8119 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8120 if (wk->wk_type == D_ALLOCDIRECT) 8121 break; 8122 if (wk == NULL) 8123 panic("setup_newdir: lost allocdirect"); 8124 if (pagedep->pd_state & NEWBLOCK) 8125 panic("setup_newdir: NEWBLOCK already set"); 8126 newblk = WK_NEWBLK(wk); 8127 pagedep->pd_state |= NEWBLOCK; 8128 pagedep->pd_newdirblk = newdirblk; 8129 newdirblk->db_pagedep = pagedep; 8130 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8131 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8132 /* 8133 * Look up the inodedep for the parent directory so that we 8134 * can link mkdir2 into the pending dotdot jaddref or 8135 * the inode write if there is none. If the inode is 8136 * ALLCOMPLETE and no jaddref is present all dependencies have 8137 * been satisfied and mkdir2 can be freed. 8138 */ 8139 inodedep_lookup(mp, dinum, 0, &inodedep); 8140 if (MOUNTEDSUJ(mp)) { 8141 if (inodedep == NULL) 8142 panic("setup_newdir: Lost parent."); 8143 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8144 inoreflst); 8145 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8146 (jaddref->ja_state & MKDIR_PARENT), 8147 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8148 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 8149 mkdir2->md_jaddref = jaddref; 8150 jaddref->ja_mkdir = mkdir2; 8151 } else if (inodedep == NULL || 8152 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8153 dap->da_state &= ~MKDIR_PARENT; 8154 WORKITEM_FREE(mkdir2, D_MKDIR); 8155 mkdir2 = NULL; 8156 } else { 8157 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 8158 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8159 } 8160 *mkdirp = mkdir2; 8161 8162 return (mkdir1); 8163 } 8164 8165 /* 8166 * Directory entry addition dependencies. 8167 * 8168 * When adding a new directory entry, the inode (with its incremented link 8169 * count) must be written to disk before the directory entry's pointer to it. 8170 * Also, if the inode is newly allocated, the corresponding freemap must be 8171 * updated (on disk) before the directory entry's pointer. These requirements 8172 * are met via undo/redo on the directory entry's pointer, which consists 8173 * simply of the inode number. 8174 * 8175 * As directory entries are added and deleted, the free space within a 8176 * directory block can become fragmented. The ufs filesystem will compact 8177 * a fragmented directory block to make space for a new entry. When this 8178 * occurs, the offsets of previously added entries change. Any "diradd" 8179 * dependency structures corresponding to these entries must be updated with 8180 * the new offsets. 8181 */ 8182 8183 /* 8184 * This routine is called after the in-memory inode's link 8185 * count has been incremented, but before the directory entry's 8186 * pointer to the inode has been set. 8187 */ 8188 int 8189 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8190 struct buf *bp; /* buffer containing directory block */ 8191 struct inode *dp; /* inode for directory */ 8192 off_t diroffset; /* offset of new entry in directory */ 8193 ino_t newinum; /* inode referenced by new directory entry */ 8194 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8195 int isnewblk; /* entry is in a newly allocated block */ 8196 { 8197 int offset; /* offset of new entry within directory block */ 8198 ufs_lbn_t lbn; /* block in directory containing new entry */ 8199 struct fs *fs; 8200 struct diradd *dap; 8201 struct newblk *newblk; 8202 struct pagedep *pagedep; 8203 struct inodedep *inodedep; 8204 struct newdirblk *newdirblk = 0; 8205 struct mkdir *mkdir1, *mkdir2; 8206 struct jaddref *jaddref; 8207 struct mount *mp; 8208 int isindir; 8209 8210 /* 8211 * Whiteouts have no dependencies. 8212 */ 8213 if (newinum == WINO) { 8214 if (newdirbp != NULL) 8215 bdwrite(newdirbp); 8216 return (0); 8217 } 8218 jaddref = NULL; 8219 mkdir1 = mkdir2 = NULL; 8220 mp = UFSTOVFS(dp->i_ump); 8221 fs = dp->i_fs; 8222 lbn = lblkno(fs, diroffset); 8223 offset = blkoff(fs, diroffset); 8224 dap = malloc(sizeof(struct diradd), M_DIRADD, 8225 M_SOFTDEP_FLAGS|M_ZERO); 8226 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8227 dap->da_offset = offset; 8228 dap->da_newinum = newinum; 8229 dap->da_state = ATTACHED; 8230 LIST_INIT(&dap->da_jwork); 8231 isindir = bp->b_lblkno >= NDADDR; 8232 if (isnewblk && 8233 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8234 newdirblk = malloc(sizeof(struct newdirblk), 8235 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8236 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8237 LIST_INIT(&newdirblk->db_mkdir); 8238 } 8239 /* 8240 * If we're creating a new directory setup the dependencies and set 8241 * the dap state to wait for them. Otherwise it's COMPLETE and 8242 * we can move on. 8243 */ 8244 if (newdirbp == NULL) { 8245 dap->da_state |= DEPCOMPLETE; 8246 ACQUIRE_LOCK(&lk); 8247 } else { 8248 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8249 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8250 &mkdir2); 8251 } 8252 /* 8253 * Link into parent directory pagedep to await its being written. 8254 */ 8255 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8256 #ifdef DEBUG 8257 if (diradd_lookup(pagedep, offset) != NULL) 8258 panic("softdep_setup_directory_add: %p already at off %d\n", 8259 diradd_lookup(pagedep, offset), offset); 8260 #endif 8261 dap->da_pagedep = pagedep; 8262 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8263 da_pdlist); 8264 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 8265 /* 8266 * If we're journaling, link the diradd into the jaddref so it 8267 * may be completed after the journal entry is written. Otherwise, 8268 * link the diradd into its inodedep. If the inode is not yet 8269 * written place it on the bufwait list, otherwise do the post-inode 8270 * write processing to put it on the id_pendinghd list. 8271 */ 8272 if (MOUNTEDSUJ(mp)) { 8273 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8274 inoreflst); 8275 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8276 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8277 jaddref->ja_diroff = diroffset; 8278 jaddref->ja_diradd = dap; 8279 add_to_journal(&jaddref->ja_list); 8280 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8281 diradd_inode_written(dap, inodedep); 8282 else 8283 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8284 /* 8285 * Add the journal entries for . and .. links now that the primary 8286 * link is written. 8287 */ 8288 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8289 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8290 inoreflst, if_deps); 8291 KASSERT(jaddref != NULL && 8292 jaddref->ja_ino == jaddref->ja_parent && 8293 (jaddref->ja_state & MKDIR_BODY), 8294 ("softdep_setup_directory_add: bad dot jaddref %p", 8295 jaddref)); 8296 mkdir1->md_jaddref = jaddref; 8297 jaddref->ja_mkdir = mkdir1; 8298 /* 8299 * It is important that the dotdot journal entry 8300 * is added prior to the dot entry since dot writes 8301 * both the dot and dotdot links. These both must 8302 * be added after the primary link for the journal 8303 * to remain consistent. 8304 */ 8305 add_to_journal(&mkdir2->md_jaddref->ja_list); 8306 add_to_journal(&jaddref->ja_list); 8307 } 8308 /* 8309 * If we are adding a new directory remember this diradd so that if 8310 * we rename it we can keep the dot and dotdot dependencies. If 8311 * we are adding a new name for an inode that has a mkdiradd we 8312 * must be in rename and we have to move the dot and dotdot 8313 * dependencies to this new name. The old name is being orphaned 8314 * soon. 8315 */ 8316 if (mkdir1 != NULL) { 8317 if (inodedep->id_mkdiradd != NULL) 8318 panic("softdep_setup_directory_add: Existing mkdir"); 8319 inodedep->id_mkdiradd = dap; 8320 } else if (inodedep->id_mkdiradd) 8321 merge_diradd(inodedep, dap); 8322 if (newdirblk) { 8323 /* 8324 * There is nothing to do if we are already tracking 8325 * this block. 8326 */ 8327 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8328 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8329 FREE_LOCK(&lk); 8330 return (0); 8331 } 8332 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8333 == 0) 8334 panic("softdep_setup_directory_add: lost entry"); 8335 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8336 pagedep->pd_state |= NEWBLOCK; 8337 pagedep->pd_newdirblk = newdirblk; 8338 newdirblk->db_pagedep = pagedep; 8339 FREE_LOCK(&lk); 8340 /* 8341 * If we extended into an indirect signal direnter to sync. 8342 */ 8343 if (isindir) 8344 return (1); 8345 return (0); 8346 } 8347 FREE_LOCK(&lk); 8348 return (0); 8349 } 8350 8351 /* 8352 * This procedure is called to change the offset of a directory 8353 * entry when compacting a directory block which must be owned 8354 * exclusively by the caller. Note that the actual entry movement 8355 * must be done in this procedure to ensure that no I/O completions 8356 * occur while the move is in progress. 8357 */ 8358 void 8359 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8360 struct buf *bp; /* Buffer holding directory block. */ 8361 struct inode *dp; /* inode for directory */ 8362 caddr_t base; /* address of dp->i_offset */ 8363 caddr_t oldloc; /* address of old directory location */ 8364 caddr_t newloc; /* address of new directory location */ 8365 int entrysize; /* size of directory entry */ 8366 { 8367 int offset, oldoffset, newoffset; 8368 struct pagedep *pagedep; 8369 struct jmvref *jmvref; 8370 struct diradd *dap; 8371 struct direct *de; 8372 struct mount *mp; 8373 ufs_lbn_t lbn; 8374 int flags; 8375 8376 mp = UFSTOVFS(dp->i_ump); 8377 de = (struct direct *)oldloc; 8378 jmvref = NULL; 8379 flags = 0; 8380 /* 8381 * Moves are always journaled as it would be too complex to 8382 * determine if any affected adds or removes are present in the 8383 * journal. 8384 */ 8385 if (MOUNTEDSUJ(mp)) { 8386 flags = DEPALLOC; 8387 jmvref = newjmvref(dp, de->d_ino, 8388 dp->i_offset + (oldloc - base), 8389 dp->i_offset + (newloc - base)); 8390 } 8391 lbn = lblkno(dp->i_fs, dp->i_offset); 8392 offset = blkoff(dp->i_fs, dp->i_offset); 8393 oldoffset = offset + (oldloc - base); 8394 newoffset = offset + (newloc - base); 8395 ACQUIRE_LOCK(&lk); 8396 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8397 goto done; 8398 dap = diradd_lookup(pagedep, oldoffset); 8399 if (dap) { 8400 dap->da_offset = newoffset; 8401 newoffset = DIRADDHASH(newoffset); 8402 oldoffset = DIRADDHASH(oldoffset); 8403 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8404 newoffset != oldoffset) { 8405 LIST_REMOVE(dap, da_pdlist); 8406 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8407 dap, da_pdlist); 8408 } 8409 } 8410 done: 8411 if (jmvref) { 8412 jmvref->jm_pagedep = pagedep; 8413 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8414 add_to_journal(&jmvref->jm_list); 8415 } 8416 bcopy(oldloc, newloc, entrysize); 8417 FREE_LOCK(&lk); 8418 } 8419 8420 /* 8421 * Move the mkdir dependencies and journal work from one diradd to another 8422 * when renaming a directory. The new name must depend on the mkdir deps 8423 * completing as the old name did. Directories can only have one valid link 8424 * at a time so one must be canonical. 8425 */ 8426 static void 8427 merge_diradd(inodedep, newdap) 8428 struct inodedep *inodedep; 8429 struct diradd *newdap; 8430 { 8431 struct diradd *olddap; 8432 struct mkdir *mkdir, *nextmd; 8433 short state; 8434 8435 olddap = inodedep->id_mkdiradd; 8436 inodedep->id_mkdiradd = newdap; 8437 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8438 newdap->da_state &= ~DEPCOMPLETE; 8439 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 8440 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8441 if (mkdir->md_diradd != olddap) 8442 continue; 8443 mkdir->md_diradd = newdap; 8444 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8445 newdap->da_state |= state; 8446 olddap->da_state &= ~state; 8447 if ((olddap->da_state & 8448 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8449 break; 8450 } 8451 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8452 panic("merge_diradd: unfound ref"); 8453 } 8454 /* 8455 * Any mkdir related journal items are not safe to be freed until 8456 * the new name is stable. 8457 */ 8458 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8459 olddap->da_state |= DEPCOMPLETE; 8460 complete_diradd(olddap); 8461 } 8462 8463 /* 8464 * Move the diradd to the pending list when all diradd dependencies are 8465 * complete. 8466 */ 8467 static void 8468 complete_diradd(dap) 8469 struct diradd *dap; 8470 { 8471 struct pagedep *pagedep; 8472 8473 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8474 if (dap->da_state & DIRCHG) 8475 pagedep = dap->da_previous->dm_pagedep; 8476 else 8477 pagedep = dap->da_pagedep; 8478 LIST_REMOVE(dap, da_pdlist); 8479 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8480 } 8481 } 8482 8483 /* 8484 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8485 * add entries and conditonally journal the remove. 8486 */ 8487 static void 8488 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8489 struct diradd *dap; 8490 struct dirrem *dirrem; 8491 struct jremref *jremref; 8492 struct jremref *dotremref; 8493 struct jremref *dotdotremref; 8494 { 8495 struct inodedep *inodedep; 8496 struct jaddref *jaddref; 8497 struct inoref *inoref; 8498 struct mkdir *mkdir; 8499 8500 /* 8501 * If no remove references were allocated we're on a non-journaled 8502 * filesystem and can skip the cancel step. 8503 */ 8504 if (jremref == NULL) { 8505 free_diradd(dap, NULL); 8506 return; 8507 } 8508 /* 8509 * Cancel the primary name an free it if it does not require 8510 * journaling. 8511 */ 8512 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8513 0, &inodedep) != 0) { 8514 /* Abort the addref that reference this diradd. */ 8515 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8516 if (inoref->if_list.wk_type != D_JADDREF) 8517 continue; 8518 jaddref = (struct jaddref *)inoref; 8519 if (jaddref->ja_diradd != dap) 8520 continue; 8521 if (cancel_jaddref(jaddref, inodedep, 8522 &dirrem->dm_jwork) == 0) { 8523 free_jremref(jremref); 8524 jremref = NULL; 8525 } 8526 break; 8527 } 8528 } 8529 /* 8530 * Cancel subordinate names and free them if they do not require 8531 * journaling. 8532 */ 8533 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8534 LIST_FOREACH(mkdir, &mkdirlisthd, md_mkdirs) { 8535 if (mkdir->md_diradd != dap) 8536 continue; 8537 if ((jaddref = mkdir->md_jaddref) == NULL) 8538 continue; 8539 mkdir->md_jaddref = NULL; 8540 if (mkdir->md_state & MKDIR_PARENT) { 8541 if (cancel_jaddref(jaddref, NULL, 8542 &dirrem->dm_jwork) == 0) { 8543 free_jremref(dotdotremref); 8544 dotdotremref = NULL; 8545 } 8546 } else { 8547 if (cancel_jaddref(jaddref, inodedep, 8548 &dirrem->dm_jwork) == 0) { 8549 free_jremref(dotremref); 8550 dotremref = NULL; 8551 } 8552 } 8553 } 8554 } 8555 8556 if (jremref) 8557 journal_jremref(dirrem, jremref, inodedep); 8558 if (dotremref) 8559 journal_jremref(dirrem, dotremref, inodedep); 8560 if (dotdotremref) 8561 journal_jremref(dirrem, dotdotremref, NULL); 8562 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8563 free_diradd(dap, &dirrem->dm_jwork); 8564 } 8565 8566 /* 8567 * Free a diradd dependency structure. This routine must be called 8568 * with splbio interrupts blocked. 8569 */ 8570 static void 8571 free_diradd(dap, wkhd) 8572 struct diradd *dap; 8573 struct workhead *wkhd; 8574 { 8575 struct dirrem *dirrem; 8576 struct pagedep *pagedep; 8577 struct inodedep *inodedep; 8578 struct mkdir *mkdir, *nextmd; 8579 8580 rw_assert(&lk, RA_WLOCKED); 8581 LIST_REMOVE(dap, da_pdlist); 8582 if (dap->da_state & ONWORKLIST) 8583 WORKLIST_REMOVE(&dap->da_list); 8584 if ((dap->da_state & DIRCHG) == 0) { 8585 pagedep = dap->da_pagedep; 8586 } else { 8587 dirrem = dap->da_previous; 8588 pagedep = dirrem->dm_pagedep; 8589 dirrem->dm_dirinum = pagedep->pd_ino; 8590 dirrem->dm_state |= COMPLETE; 8591 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8592 add_to_worklist(&dirrem->dm_list, 0); 8593 } 8594 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8595 0, &inodedep) != 0) 8596 if (inodedep->id_mkdiradd == dap) 8597 inodedep->id_mkdiradd = NULL; 8598 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8599 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 8600 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8601 if (mkdir->md_diradd != dap) 8602 continue; 8603 dap->da_state &= 8604 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8605 LIST_REMOVE(mkdir, md_mkdirs); 8606 if (mkdir->md_state & ONWORKLIST) 8607 WORKLIST_REMOVE(&mkdir->md_list); 8608 if (mkdir->md_jaddref != NULL) 8609 panic("free_diradd: Unexpected jaddref"); 8610 WORKITEM_FREE(mkdir, D_MKDIR); 8611 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8612 break; 8613 } 8614 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8615 panic("free_diradd: unfound ref"); 8616 } 8617 if (inodedep) 8618 free_inodedep(inodedep); 8619 /* 8620 * Free any journal segments waiting for the directory write. 8621 */ 8622 handle_jwork(&dap->da_jwork); 8623 WORKITEM_FREE(dap, D_DIRADD); 8624 } 8625 8626 /* 8627 * Directory entry removal dependencies. 8628 * 8629 * When removing a directory entry, the entry's inode pointer must be 8630 * zero'ed on disk before the corresponding inode's link count is decremented 8631 * (possibly freeing the inode for re-use). This dependency is handled by 8632 * updating the directory entry but delaying the inode count reduction until 8633 * after the directory block has been written to disk. After this point, the 8634 * inode count can be decremented whenever it is convenient. 8635 */ 8636 8637 /* 8638 * This routine should be called immediately after removing 8639 * a directory entry. The inode's link count should not be 8640 * decremented by the calling procedure -- the soft updates 8641 * code will do this task when it is safe. 8642 */ 8643 void 8644 softdep_setup_remove(bp, dp, ip, isrmdir) 8645 struct buf *bp; /* buffer containing directory block */ 8646 struct inode *dp; /* inode for the directory being modified */ 8647 struct inode *ip; /* inode for directory entry being removed */ 8648 int isrmdir; /* indicates if doing RMDIR */ 8649 { 8650 struct dirrem *dirrem, *prevdirrem; 8651 struct inodedep *inodedep; 8652 int direct; 8653 8654 /* 8655 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8656 * newdirrem() to setup the full directory remove which requires 8657 * isrmdir > 1. 8658 */ 8659 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8660 /* 8661 * Add the dirrem to the inodedep's pending remove list for quick 8662 * discovery later. 8663 */ 8664 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8665 &inodedep) == 0) 8666 panic("softdep_setup_remove: Lost inodedep."); 8667 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8668 dirrem->dm_state |= ONDEPLIST; 8669 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8670 8671 /* 8672 * If the COMPLETE flag is clear, then there were no active 8673 * entries and we want to roll back to a zeroed entry until 8674 * the new inode is committed to disk. If the COMPLETE flag is 8675 * set then we have deleted an entry that never made it to 8676 * disk. If the entry we deleted resulted from a name change, 8677 * then the old name still resides on disk. We cannot delete 8678 * its inode (returned to us in prevdirrem) until the zeroed 8679 * directory entry gets to disk. The new inode has never been 8680 * referenced on the disk, so can be deleted immediately. 8681 */ 8682 if ((dirrem->dm_state & COMPLETE) == 0) { 8683 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 8684 dm_next); 8685 FREE_LOCK(&lk); 8686 } else { 8687 if (prevdirrem != NULL) 8688 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 8689 prevdirrem, dm_next); 8690 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 8691 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 8692 FREE_LOCK(&lk); 8693 if (direct) 8694 handle_workitem_remove(dirrem, 0); 8695 } 8696 } 8697 8698 /* 8699 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 8700 * pd_pendinghd list of a pagedep. 8701 */ 8702 static struct diradd * 8703 diradd_lookup(pagedep, offset) 8704 struct pagedep *pagedep; 8705 int offset; 8706 { 8707 struct diradd *dap; 8708 8709 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 8710 if (dap->da_offset == offset) 8711 return (dap); 8712 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 8713 if (dap->da_offset == offset) 8714 return (dap); 8715 return (NULL); 8716 } 8717 8718 /* 8719 * Search for a .. diradd dependency in a directory that is being removed. 8720 * If the directory was renamed to a new parent we have a diradd rather 8721 * than a mkdir for the .. entry. We need to cancel it now before 8722 * it is found in truncate(). 8723 */ 8724 static struct jremref * 8725 cancel_diradd_dotdot(ip, dirrem, jremref) 8726 struct inode *ip; 8727 struct dirrem *dirrem; 8728 struct jremref *jremref; 8729 { 8730 struct pagedep *pagedep; 8731 struct diradd *dap; 8732 struct worklist *wk; 8733 8734 if (pagedep_lookup(UFSTOVFS(ip->i_ump), NULL, ip->i_number, 0, 0, 8735 &pagedep) == 0) 8736 return (jremref); 8737 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 8738 if (dap == NULL) 8739 return (jremref); 8740 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 8741 /* 8742 * Mark any journal work as belonging to the parent so it is freed 8743 * with the .. reference. 8744 */ 8745 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 8746 wk->wk_state |= MKDIR_PARENT; 8747 return (NULL); 8748 } 8749 8750 /* 8751 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 8752 * replace it with a dirrem/diradd pair as a result of re-parenting a 8753 * directory. This ensures that we don't simultaneously have a mkdir and 8754 * a diradd for the same .. entry. 8755 */ 8756 static struct jremref * 8757 cancel_mkdir_dotdot(ip, dirrem, jremref) 8758 struct inode *ip; 8759 struct dirrem *dirrem; 8760 struct jremref *jremref; 8761 { 8762 struct inodedep *inodedep; 8763 struct jaddref *jaddref; 8764 struct mkdir *mkdir; 8765 struct diradd *dap; 8766 8767 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8768 &inodedep) == 0) 8769 return (jremref); 8770 dap = inodedep->id_mkdiradd; 8771 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 8772 return (jremref); 8773 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; 8774 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 8775 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 8776 break; 8777 if (mkdir == NULL) 8778 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 8779 if ((jaddref = mkdir->md_jaddref) != NULL) { 8780 mkdir->md_jaddref = NULL; 8781 jaddref->ja_state &= ~MKDIR_PARENT; 8782 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0, 8783 &inodedep) == 0) 8784 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 8785 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 8786 journal_jremref(dirrem, jremref, inodedep); 8787 jremref = NULL; 8788 } 8789 } 8790 if (mkdir->md_state & ONWORKLIST) 8791 WORKLIST_REMOVE(&mkdir->md_list); 8792 mkdir->md_state |= ALLCOMPLETE; 8793 complete_mkdir(mkdir); 8794 return (jremref); 8795 } 8796 8797 static void 8798 journal_jremref(dirrem, jremref, inodedep) 8799 struct dirrem *dirrem; 8800 struct jremref *jremref; 8801 struct inodedep *inodedep; 8802 { 8803 8804 if (inodedep == NULL) 8805 if (inodedep_lookup(jremref->jr_list.wk_mp, 8806 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 8807 panic("journal_jremref: Lost inodedep"); 8808 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 8809 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 8810 add_to_journal(&jremref->jr_list); 8811 } 8812 8813 static void 8814 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 8815 struct dirrem *dirrem; 8816 struct jremref *jremref; 8817 struct jremref *dotremref; 8818 struct jremref *dotdotremref; 8819 { 8820 struct inodedep *inodedep; 8821 8822 8823 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 8824 &inodedep) == 0) 8825 panic("dirrem_journal: Lost inodedep"); 8826 journal_jremref(dirrem, jremref, inodedep); 8827 if (dotremref) 8828 journal_jremref(dirrem, dotremref, inodedep); 8829 if (dotdotremref) 8830 journal_jremref(dirrem, dotdotremref, NULL); 8831 } 8832 8833 /* 8834 * Allocate a new dirrem if appropriate and return it along with 8835 * its associated pagedep. Called without a lock, returns with lock. 8836 */ 8837 static struct dirrem * 8838 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 8839 struct buf *bp; /* buffer containing directory block */ 8840 struct inode *dp; /* inode for the directory being modified */ 8841 struct inode *ip; /* inode for directory entry being removed */ 8842 int isrmdir; /* indicates if doing RMDIR */ 8843 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 8844 { 8845 int offset; 8846 ufs_lbn_t lbn; 8847 struct diradd *dap; 8848 struct dirrem *dirrem; 8849 struct pagedep *pagedep; 8850 struct jremref *jremref; 8851 struct jremref *dotremref; 8852 struct jremref *dotdotremref; 8853 struct vnode *dvp; 8854 8855 /* 8856 * Whiteouts have no deletion dependencies. 8857 */ 8858 if (ip == NULL) 8859 panic("newdirrem: whiteout"); 8860 dvp = ITOV(dp); 8861 /* 8862 * If we are over our limit, try to improve the situation. 8863 * Limiting the number of dirrem structures will also limit 8864 * the number of freefile and freeblks structures. 8865 */ 8866 ACQUIRE_LOCK(&lk); 8867 if (!IS_SNAPSHOT(ip) && dep_current[D_DIRREM] > max_softdeps / 2) 8868 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_BLOCKS); 8869 FREE_LOCK(&lk); 8870 dirrem = malloc(sizeof(struct dirrem), 8871 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO); 8872 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 8873 LIST_INIT(&dirrem->dm_jremrefhd); 8874 LIST_INIT(&dirrem->dm_jwork); 8875 dirrem->dm_state = isrmdir ? RMDIR : 0; 8876 dirrem->dm_oldinum = ip->i_number; 8877 *prevdirremp = NULL; 8878 /* 8879 * Allocate remove reference structures to track journal write 8880 * dependencies. We will always have one for the link and 8881 * when doing directories we will always have one more for dot. 8882 * When renaming a directory we skip the dotdot link change so 8883 * this is not needed. 8884 */ 8885 jremref = dotremref = dotdotremref = NULL; 8886 if (DOINGSUJ(dvp)) { 8887 if (isrmdir) { 8888 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 8889 ip->i_effnlink + 2); 8890 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 8891 ip->i_effnlink + 1); 8892 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 8893 dp->i_effnlink + 1); 8894 dotdotremref->jr_state |= MKDIR_PARENT; 8895 } else 8896 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 8897 ip->i_effnlink + 1); 8898 } 8899 ACQUIRE_LOCK(&lk); 8900 lbn = lblkno(dp->i_fs, dp->i_offset); 8901 offset = blkoff(dp->i_fs, dp->i_offset); 8902 pagedep_lookup(UFSTOVFS(dp->i_ump), bp, dp->i_number, lbn, DEPALLOC, 8903 &pagedep); 8904 dirrem->dm_pagedep = pagedep; 8905 dirrem->dm_offset = offset; 8906 /* 8907 * If we're renaming a .. link to a new directory, cancel any 8908 * existing MKDIR_PARENT mkdir. If it has already been canceled 8909 * the jremref is preserved for any potential diradd in this 8910 * location. This can not coincide with a rmdir. 8911 */ 8912 if (dp->i_offset == DOTDOT_OFFSET) { 8913 if (isrmdir) 8914 panic("newdirrem: .. directory change during remove?"); 8915 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 8916 } 8917 /* 8918 * If we're removing a directory search for the .. dependency now and 8919 * cancel it. Any pending journal work will be added to the dirrem 8920 * to be completed when the workitem remove completes. 8921 */ 8922 if (isrmdir) 8923 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 8924 /* 8925 * Check for a diradd dependency for the same directory entry. 8926 * If present, then both dependencies become obsolete and can 8927 * be de-allocated. 8928 */ 8929 dap = diradd_lookup(pagedep, offset); 8930 if (dap == NULL) { 8931 /* 8932 * Link the jremref structures into the dirrem so they are 8933 * written prior to the pagedep. 8934 */ 8935 if (jremref) 8936 dirrem_journal(dirrem, jremref, dotremref, 8937 dotdotremref); 8938 return (dirrem); 8939 } 8940 /* 8941 * Must be ATTACHED at this point. 8942 */ 8943 if ((dap->da_state & ATTACHED) == 0) 8944 panic("newdirrem: not ATTACHED"); 8945 if (dap->da_newinum != ip->i_number) 8946 panic("newdirrem: inum %ju should be %ju", 8947 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 8948 /* 8949 * If we are deleting a changed name that never made it to disk, 8950 * then return the dirrem describing the previous inode (which 8951 * represents the inode currently referenced from this entry on disk). 8952 */ 8953 if ((dap->da_state & DIRCHG) != 0) { 8954 *prevdirremp = dap->da_previous; 8955 dap->da_state &= ~DIRCHG; 8956 dap->da_pagedep = pagedep; 8957 } 8958 /* 8959 * We are deleting an entry that never made it to disk. 8960 * Mark it COMPLETE so we can delete its inode immediately. 8961 */ 8962 dirrem->dm_state |= COMPLETE; 8963 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 8964 #ifdef SUJ_DEBUG 8965 if (isrmdir == 0) { 8966 struct worklist *wk; 8967 8968 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 8969 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 8970 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 8971 } 8972 #endif 8973 8974 return (dirrem); 8975 } 8976 8977 /* 8978 * Directory entry change dependencies. 8979 * 8980 * Changing an existing directory entry requires that an add operation 8981 * be completed first followed by a deletion. The semantics for the addition 8982 * are identical to the description of adding a new entry above except 8983 * that the rollback is to the old inode number rather than zero. Once 8984 * the addition dependency is completed, the removal is done as described 8985 * in the removal routine above. 8986 */ 8987 8988 /* 8989 * This routine should be called immediately after changing 8990 * a directory entry. The inode's link count should not be 8991 * decremented by the calling procedure -- the soft updates 8992 * code will perform this task when it is safe. 8993 */ 8994 void 8995 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 8996 struct buf *bp; /* buffer containing directory block */ 8997 struct inode *dp; /* inode for the directory being modified */ 8998 struct inode *ip; /* inode for directory entry being removed */ 8999 ino_t newinum; /* new inode number for changed entry */ 9000 int isrmdir; /* indicates if doing RMDIR */ 9001 { 9002 int offset; 9003 struct diradd *dap = NULL; 9004 struct dirrem *dirrem, *prevdirrem; 9005 struct pagedep *pagedep; 9006 struct inodedep *inodedep; 9007 struct jaddref *jaddref; 9008 struct mount *mp; 9009 9010 offset = blkoff(dp->i_fs, dp->i_offset); 9011 mp = UFSTOVFS(dp->i_ump); 9012 9013 /* 9014 * Whiteouts do not need diradd dependencies. 9015 */ 9016 if (newinum != WINO) { 9017 dap = malloc(sizeof(struct diradd), 9018 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9019 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9020 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9021 dap->da_offset = offset; 9022 dap->da_newinum = newinum; 9023 LIST_INIT(&dap->da_jwork); 9024 } 9025 9026 /* 9027 * Allocate a new dirrem and ACQUIRE_LOCK. 9028 */ 9029 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9030 pagedep = dirrem->dm_pagedep; 9031 /* 9032 * The possible values for isrmdir: 9033 * 0 - non-directory file rename 9034 * 1 - directory rename within same directory 9035 * inum - directory rename to new directory of given inode number 9036 * When renaming to a new directory, we are both deleting and 9037 * creating a new directory entry, so the link count on the new 9038 * directory should not change. Thus we do not need the followup 9039 * dirrem which is usually done in handle_workitem_remove. We set 9040 * the DIRCHG flag to tell handle_workitem_remove to skip the 9041 * followup dirrem. 9042 */ 9043 if (isrmdir > 1) 9044 dirrem->dm_state |= DIRCHG; 9045 9046 /* 9047 * Whiteouts have no additional dependencies, 9048 * so just put the dirrem on the correct list. 9049 */ 9050 if (newinum == WINO) { 9051 if ((dirrem->dm_state & COMPLETE) == 0) { 9052 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9053 dm_next); 9054 } else { 9055 dirrem->dm_dirinum = pagedep->pd_ino; 9056 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9057 add_to_worklist(&dirrem->dm_list, 0); 9058 } 9059 FREE_LOCK(&lk); 9060 return; 9061 } 9062 /* 9063 * Add the dirrem to the inodedep's pending remove list for quick 9064 * discovery later. A valid nlinkdelta ensures that this lookup 9065 * will not fail. 9066 */ 9067 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9068 panic("softdep_setup_directory_change: Lost inodedep."); 9069 dirrem->dm_state |= ONDEPLIST; 9070 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9071 9072 /* 9073 * If the COMPLETE flag is clear, then there were no active 9074 * entries and we want to roll back to the previous inode until 9075 * the new inode is committed to disk. If the COMPLETE flag is 9076 * set, then we have deleted an entry that never made it to disk. 9077 * If the entry we deleted resulted from a name change, then the old 9078 * inode reference still resides on disk. Any rollback that we do 9079 * needs to be to that old inode (returned to us in prevdirrem). If 9080 * the entry we deleted resulted from a create, then there is 9081 * no entry on the disk, so we want to roll back to zero rather 9082 * than the uncommitted inode. In either of the COMPLETE cases we 9083 * want to immediately free the unwritten and unreferenced inode. 9084 */ 9085 if ((dirrem->dm_state & COMPLETE) == 0) { 9086 dap->da_previous = dirrem; 9087 } else { 9088 if (prevdirrem != NULL) { 9089 dap->da_previous = prevdirrem; 9090 } else { 9091 dap->da_state &= ~DIRCHG; 9092 dap->da_pagedep = pagedep; 9093 } 9094 dirrem->dm_dirinum = pagedep->pd_ino; 9095 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9096 add_to_worklist(&dirrem->dm_list, 0); 9097 } 9098 /* 9099 * Lookup the jaddref for this journal entry. We must finish 9100 * initializing it and make the diradd write dependent on it. 9101 * If we're not journaling, put it on the id_bufwait list if the 9102 * inode is not yet written. If it is written, do the post-inode 9103 * write processing to put it on the id_pendinghd list. 9104 */ 9105 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 9106 if (MOUNTEDSUJ(mp)) { 9107 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9108 inoreflst); 9109 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9110 ("softdep_setup_directory_change: bad jaddref %p", 9111 jaddref)); 9112 jaddref->ja_diroff = dp->i_offset; 9113 jaddref->ja_diradd = dap; 9114 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9115 dap, da_pdlist); 9116 add_to_journal(&jaddref->ja_list); 9117 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9118 dap->da_state |= COMPLETE; 9119 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9120 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9121 } else { 9122 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9123 dap, da_pdlist); 9124 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9125 } 9126 /* 9127 * If we're making a new name for a directory that has not been 9128 * committed when need to move the dot and dotdot references to 9129 * this new name. 9130 */ 9131 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9132 merge_diradd(inodedep, dap); 9133 FREE_LOCK(&lk); 9134 } 9135 9136 /* 9137 * Called whenever the link count on an inode is changed. 9138 * It creates an inode dependency so that the new reference(s) 9139 * to the inode cannot be committed to disk until the updated 9140 * inode has been written. 9141 */ 9142 void 9143 softdep_change_linkcnt(ip) 9144 struct inode *ip; /* the inode with the increased link count */ 9145 { 9146 struct inodedep *inodedep; 9147 int dflags; 9148 9149 ACQUIRE_LOCK(&lk); 9150 dflags = DEPALLOC; 9151 if (IS_SNAPSHOT(ip)) 9152 dflags |= NODELAY; 9153 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 9154 if (ip->i_nlink < ip->i_effnlink) 9155 panic("softdep_change_linkcnt: bad delta"); 9156 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9157 FREE_LOCK(&lk); 9158 } 9159 9160 /* 9161 * Attach a sbdep dependency to the superblock buf so that we can keep 9162 * track of the head of the linked list of referenced but unlinked inodes. 9163 */ 9164 void 9165 softdep_setup_sbupdate(ump, fs, bp) 9166 struct ufsmount *ump; 9167 struct fs *fs; 9168 struct buf *bp; 9169 { 9170 struct sbdep *sbdep; 9171 struct worklist *wk; 9172 9173 if (MOUNTEDSUJ(UFSTOVFS(ump)) == 0) 9174 return; 9175 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9176 if (wk->wk_type == D_SBDEP) 9177 break; 9178 if (wk != NULL) 9179 return; 9180 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9181 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9182 sbdep->sb_fs = fs; 9183 sbdep->sb_ump = ump; 9184 ACQUIRE_LOCK(&lk); 9185 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9186 FREE_LOCK(&lk); 9187 } 9188 9189 /* 9190 * Return the first unlinked inodedep which is ready to be the head of the 9191 * list. The inodedep and all those after it must have valid next pointers. 9192 */ 9193 static struct inodedep * 9194 first_unlinked_inodedep(ump) 9195 struct ufsmount *ump; 9196 { 9197 struct inodedep *inodedep; 9198 struct inodedep *idp; 9199 9200 rw_assert(&lk, RA_WLOCKED); 9201 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9202 inodedep; inodedep = idp) { 9203 if ((inodedep->id_state & UNLINKNEXT) == 0) 9204 return (NULL); 9205 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9206 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9207 break; 9208 if ((inodedep->id_state & UNLINKPREV) == 0) 9209 break; 9210 } 9211 return (inodedep); 9212 } 9213 9214 /* 9215 * Set the sujfree unlinked head pointer prior to writing a superblock. 9216 */ 9217 static void 9218 initiate_write_sbdep(sbdep) 9219 struct sbdep *sbdep; 9220 { 9221 struct inodedep *inodedep; 9222 struct fs *bpfs; 9223 struct fs *fs; 9224 9225 bpfs = sbdep->sb_fs; 9226 fs = sbdep->sb_ump->um_fs; 9227 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9228 if (inodedep) { 9229 fs->fs_sujfree = inodedep->id_ino; 9230 inodedep->id_state |= UNLINKPREV; 9231 } else 9232 fs->fs_sujfree = 0; 9233 bpfs->fs_sujfree = fs->fs_sujfree; 9234 } 9235 9236 /* 9237 * After a superblock is written determine whether it must be written again 9238 * due to a changing unlinked list head. 9239 */ 9240 static int 9241 handle_written_sbdep(sbdep, bp) 9242 struct sbdep *sbdep; 9243 struct buf *bp; 9244 { 9245 struct inodedep *inodedep; 9246 struct mount *mp; 9247 struct fs *fs; 9248 9249 rw_assert(&lk, RA_WLOCKED); 9250 fs = sbdep->sb_fs; 9251 mp = UFSTOVFS(sbdep->sb_ump); 9252 /* 9253 * If the superblock doesn't match the in-memory list start over. 9254 */ 9255 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9256 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9257 (inodedep == NULL && fs->fs_sujfree != 0)) { 9258 bdirty(bp); 9259 return (1); 9260 } 9261 WORKITEM_FREE(sbdep, D_SBDEP); 9262 if (fs->fs_sujfree == 0) 9263 return (0); 9264 /* 9265 * Now that we have a record of this inode in stable store allow it 9266 * to be written to free up pending work. Inodes may see a lot of 9267 * write activity after they are unlinked which we must not hold up. 9268 */ 9269 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9270 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9271 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9272 inodedep, inodedep->id_state); 9273 if (inodedep->id_state & UNLINKONLIST) 9274 break; 9275 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9276 } 9277 9278 return (0); 9279 } 9280 9281 /* 9282 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9283 */ 9284 static void 9285 unlinked_inodedep(mp, inodedep) 9286 struct mount *mp; 9287 struct inodedep *inodedep; 9288 { 9289 struct ufsmount *ump; 9290 9291 rw_assert(&lk, RA_WLOCKED); 9292 if (MOUNTEDSUJ(mp) == 0) 9293 return; 9294 ump = VFSTOUFS(mp); 9295 ump->um_fs->fs_fmod = 1; 9296 if (inodedep->id_state & UNLINKED) 9297 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9298 inodedep->id_state |= UNLINKED; 9299 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9300 } 9301 9302 /* 9303 * Remove an inodedep from the unlinked inodedep list. This may require 9304 * disk writes if the inode has made it that far. 9305 */ 9306 static void 9307 clear_unlinked_inodedep(inodedep) 9308 struct inodedep *inodedep; 9309 { 9310 struct ufsmount *ump; 9311 struct inodedep *idp; 9312 struct inodedep *idn; 9313 struct fs *fs; 9314 struct buf *bp; 9315 ino_t ino; 9316 ino_t nino; 9317 ino_t pino; 9318 int error; 9319 9320 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9321 fs = ump->um_fs; 9322 ino = inodedep->id_ino; 9323 error = 0; 9324 for (;;) { 9325 rw_assert(&lk, RA_WLOCKED); 9326 KASSERT((inodedep->id_state & UNLINKED) != 0, 9327 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9328 inodedep)); 9329 /* 9330 * If nothing has yet been written simply remove us from 9331 * the in memory list and return. This is the most common 9332 * case where handle_workitem_remove() loses the final 9333 * reference. 9334 */ 9335 if ((inodedep->id_state & UNLINKLINKS) == 0) 9336 break; 9337 /* 9338 * If we have a NEXT pointer and no PREV pointer we can simply 9339 * clear NEXT's PREV and remove ourselves from the list. Be 9340 * careful not to clear PREV if the superblock points at 9341 * next as well. 9342 */ 9343 idn = TAILQ_NEXT(inodedep, id_unlinked); 9344 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9345 if (idn && fs->fs_sujfree != idn->id_ino) 9346 idn->id_state &= ~UNLINKPREV; 9347 break; 9348 } 9349 /* 9350 * Here we have an inodedep which is actually linked into 9351 * the list. We must remove it by forcing a write to the 9352 * link before us, whether it be the superblock or an inode. 9353 * Unfortunately the list may change while we're waiting 9354 * on the buf lock for either resource so we must loop until 9355 * we lock the right one. If both the superblock and an 9356 * inode point to this inode we must clear the inode first 9357 * followed by the superblock. 9358 */ 9359 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9360 pino = 0; 9361 if (idp && (idp->id_state & UNLINKNEXT)) 9362 pino = idp->id_ino; 9363 FREE_LOCK(&lk); 9364 if (pino == 0) { 9365 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9366 (int)fs->fs_sbsize, 0, 0, 0); 9367 } else { 9368 error = bread(ump->um_devvp, 9369 fsbtodb(fs, ino_to_fsba(fs, pino)), 9370 (int)fs->fs_bsize, NOCRED, &bp); 9371 if (error) 9372 brelse(bp); 9373 } 9374 ACQUIRE_LOCK(&lk); 9375 if (error) 9376 break; 9377 /* If the list has changed restart the loop. */ 9378 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9379 nino = 0; 9380 if (idp && (idp->id_state & UNLINKNEXT)) 9381 nino = idp->id_ino; 9382 if (nino != pino || 9383 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9384 FREE_LOCK(&lk); 9385 brelse(bp); 9386 ACQUIRE_LOCK(&lk); 9387 continue; 9388 } 9389 nino = 0; 9390 idn = TAILQ_NEXT(inodedep, id_unlinked); 9391 if (idn) 9392 nino = idn->id_ino; 9393 /* 9394 * Remove us from the in memory list. After this we cannot 9395 * access the inodedep. 9396 */ 9397 KASSERT((inodedep->id_state & UNLINKED) != 0, 9398 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9399 inodedep)); 9400 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9401 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9402 FREE_LOCK(&lk); 9403 /* 9404 * The predecessor's next pointer is manually updated here 9405 * so that the NEXT flag is never cleared for an element 9406 * that is in the list. 9407 */ 9408 if (pino == 0) { 9409 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9410 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9411 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9412 bp); 9413 } else if (fs->fs_magic == FS_UFS1_MAGIC) 9414 ((struct ufs1_dinode *)bp->b_data + 9415 ino_to_fsbo(fs, pino))->di_freelink = nino; 9416 else 9417 ((struct ufs2_dinode *)bp->b_data + 9418 ino_to_fsbo(fs, pino))->di_freelink = nino; 9419 /* 9420 * If the bwrite fails we have no recourse to recover. The 9421 * filesystem is corrupted already. 9422 */ 9423 bwrite(bp); 9424 ACQUIRE_LOCK(&lk); 9425 /* 9426 * If the superblock pointer still needs to be cleared force 9427 * a write here. 9428 */ 9429 if (fs->fs_sujfree == ino) { 9430 FREE_LOCK(&lk); 9431 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9432 (int)fs->fs_sbsize, 0, 0, 0); 9433 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9434 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9435 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9436 bp); 9437 bwrite(bp); 9438 ACQUIRE_LOCK(&lk); 9439 } 9440 9441 if (fs->fs_sujfree != ino) 9442 return; 9443 panic("clear_unlinked_inodedep: Failed to clear free head"); 9444 } 9445 if (inodedep->id_ino == fs->fs_sujfree) 9446 panic("clear_unlinked_inodedep: Freeing head of free list"); 9447 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9448 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9449 return; 9450 } 9451 9452 /* 9453 * This workitem decrements the inode's link count. 9454 * If the link count reaches zero, the file is removed. 9455 */ 9456 static int 9457 handle_workitem_remove(dirrem, flags) 9458 struct dirrem *dirrem; 9459 int flags; 9460 { 9461 struct inodedep *inodedep; 9462 struct workhead dotdotwk; 9463 struct worklist *wk; 9464 struct ufsmount *ump; 9465 struct mount *mp; 9466 struct vnode *vp; 9467 struct inode *ip; 9468 ino_t oldinum; 9469 9470 if (dirrem->dm_state & ONWORKLIST) 9471 panic("handle_workitem_remove: dirrem %p still on worklist", 9472 dirrem); 9473 oldinum = dirrem->dm_oldinum; 9474 mp = dirrem->dm_list.wk_mp; 9475 ump = VFSTOUFS(mp); 9476 flags |= LK_EXCLUSIVE; 9477 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9478 return (EBUSY); 9479 ip = VTOI(vp); 9480 ACQUIRE_LOCK(&lk); 9481 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9482 panic("handle_workitem_remove: lost inodedep"); 9483 if (dirrem->dm_state & ONDEPLIST) 9484 LIST_REMOVE(dirrem, dm_inonext); 9485 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9486 ("handle_workitem_remove: Journal entries not written.")); 9487 9488 /* 9489 * Move all dependencies waiting on the remove to complete 9490 * from the dirrem to the inode inowait list to be completed 9491 * after the inode has been updated and written to disk. Any 9492 * marked MKDIR_PARENT are saved to be completed when the .. ref 9493 * is removed. 9494 */ 9495 LIST_INIT(&dotdotwk); 9496 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9497 WORKLIST_REMOVE(wk); 9498 if (wk->wk_state & MKDIR_PARENT) { 9499 wk->wk_state &= ~MKDIR_PARENT; 9500 WORKLIST_INSERT(&dotdotwk, wk); 9501 continue; 9502 } 9503 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9504 } 9505 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9506 /* 9507 * Normal file deletion. 9508 */ 9509 if ((dirrem->dm_state & RMDIR) == 0) { 9510 ip->i_nlink--; 9511 DIP_SET(ip, i_nlink, ip->i_nlink); 9512 ip->i_flag |= IN_CHANGE; 9513 if (ip->i_nlink < ip->i_effnlink) 9514 panic("handle_workitem_remove: bad file delta"); 9515 if (ip->i_nlink == 0) 9516 unlinked_inodedep(mp, inodedep); 9517 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9518 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9519 ("handle_workitem_remove: worklist not empty. %s", 9520 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9521 WORKITEM_FREE(dirrem, D_DIRREM); 9522 FREE_LOCK(&lk); 9523 goto out; 9524 } 9525 /* 9526 * Directory deletion. Decrement reference count for both the 9527 * just deleted parent directory entry and the reference for ".". 9528 * Arrange to have the reference count on the parent decremented 9529 * to account for the loss of "..". 9530 */ 9531 ip->i_nlink -= 2; 9532 DIP_SET(ip, i_nlink, ip->i_nlink); 9533 ip->i_flag |= IN_CHANGE; 9534 if (ip->i_nlink < ip->i_effnlink) 9535 panic("handle_workitem_remove: bad dir delta"); 9536 if (ip->i_nlink == 0) 9537 unlinked_inodedep(mp, inodedep); 9538 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9539 /* 9540 * Rename a directory to a new parent. Since, we are both deleting 9541 * and creating a new directory entry, the link count on the new 9542 * directory should not change. Thus we skip the followup dirrem. 9543 */ 9544 if (dirrem->dm_state & DIRCHG) { 9545 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9546 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9547 WORKITEM_FREE(dirrem, D_DIRREM); 9548 FREE_LOCK(&lk); 9549 goto out; 9550 } 9551 dirrem->dm_state = ONDEPLIST; 9552 dirrem->dm_oldinum = dirrem->dm_dirinum; 9553 /* 9554 * Place the dirrem on the parent's diremhd list. 9555 */ 9556 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9557 panic("handle_workitem_remove: lost dir inodedep"); 9558 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9559 /* 9560 * If the allocated inode has never been written to disk, then 9561 * the on-disk inode is zero'ed and we can remove the file 9562 * immediately. When journaling if the inode has been marked 9563 * unlinked and not DEPCOMPLETE we know it can never be written. 9564 */ 9565 inodedep_lookup(mp, oldinum, 0, &inodedep); 9566 if (inodedep == NULL || 9567 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9568 check_inode_unwritten(inodedep)) { 9569 FREE_LOCK(&lk); 9570 vput(vp); 9571 return handle_workitem_remove(dirrem, flags); 9572 } 9573 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9574 FREE_LOCK(&lk); 9575 ip->i_flag |= IN_CHANGE; 9576 out: 9577 ffs_update(vp, 0); 9578 vput(vp); 9579 return (0); 9580 } 9581 9582 /* 9583 * Inode de-allocation dependencies. 9584 * 9585 * When an inode's link count is reduced to zero, it can be de-allocated. We 9586 * found it convenient to postpone de-allocation until after the inode is 9587 * written to disk with its new link count (zero). At this point, all of the 9588 * on-disk inode's block pointers are nullified and, with careful dependency 9589 * list ordering, all dependencies related to the inode will be satisfied and 9590 * the corresponding dependency structures de-allocated. So, if/when the 9591 * inode is reused, there will be no mixing of old dependencies with new 9592 * ones. This artificial dependency is set up by the block de-allocation 9593 * procedure above (softdep_setup_freeblocks) and completed by the 9594 * following procedure. 9595 */ 9596 static void 9597 handle_workitem_freefile(freefile) 9598 struct freefile *freefile; 9599 { 9600 struct workhead wkhd; 9601 struct fs *fs; 9602 struct inodedep *idp; 9603 struct ufsmount *ump; 9604 int error; 9605 9606 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9607 fs = ump->um_fs; 9608 #ifdef DEBUG 9609 ACQUIRE_LOCK(&lk); 9610 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9611 FREE_LOCK(&lk); 9612 if (error) 9613 panic("handle_workitem_freefile: inodedep %p survived", idp); 9614 #endif 9615 UFS_LOCK(ump); 9616 fs->fs_pendinginodes -= 1; 9617 UFS_UNLOCK(ump); 9618 LIST_INIT(&wkhd); 9619 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9620 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9621 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9622 softdep_error("handle_workitem_freefile", error); 9623 ACQUIRE_LOCK(&lk); 9624 WORKITEM_FREE(freefile, D_FREEFILE); 9625 FREE_LOCK(&lk); 9626 } 9627 9628 9629 /* 9630 * Helper function which unlinks marker element from work list and returns 9631 * the next element on the list. 9632 */ 9633 static __inline struct worklist * 9634 markernext(struct worklist *marker) 9635 { 9636 struct worklist *next; 9637 9638 next = LIST_NEXT(marker, wk_list); 9639 LIST_REMOVE(marker, wk_list); 9640 return next; 9641 } 9642 9643 /* 9644 * Disk writes. 9645 * 9646 * The dependency structures constructed above are most actively used when file 9647 * system blocks are written to disk. No constraints are placed on when a 9648 * block can be written, but unsatisfied update dependencies are made safe by 9649 * modifying (or replacing) the source memory for the duration of the disk 9650 * write. When the disk write completes, the memory block is again brought 9651 * up-to-date. 9652 * 9653 * In-core inode structure reclamation. 9654 * 9655 * Because there are a finite number of "in-core" inode structures, they are 9656 * reused regularly. By transferring all inode-related dependencies to the 9657 * in-memory inode block and indexing them separately (via "inodedep"s), we 9658 * can allow "in-core" inode structures to be reused at any time and avoid 9659 * any increase in contention. 9660 * 9661 * Called just before entering the device driver to initiate a new disk I/O. 9662 * The buffer must be locked, thus, no I/O completion operations can occur 9663 * while we are manipulating its associated dependencies. 9664 */ 9665 static void 9666 softdep_disk_io_initiation(bp) 9667 struct buf *bp; /* structure describing disk write to occur */ 9668 { 9669 struct worklist *wk; 9670 struct worklist marker; 9671 struct inodedep *inodedep; 9672 struct freeblks *freeblks; 9673 struct jblkdep *jblkdep; 9674 struct newblk *newblk; 9675 9676 /* 9677 * We only care about write operations. There should never 9678 * be dependencies for reads. 9679 */ 9680 if (bp->b_iocmd != BIO_WRITE) 9681 panic("softdep_disk_io_initiation: not write"); 9682 9683 if (bp->b_vflags & BV_BKGRDINPROG) 9684 panic("softdep_disk_io_initiation: Writing buffer with " 9685 "background write in progress: %p", bp); 9686 9687 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 9688 PHOLD(curproc); /* Don't swap out kernel stack */ 9689 9690 ACQUIRE_LOCK(&lk); 9691 /* 9692 * Do any necessary pre-I/O processing. 9693 */ 9694 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 9695 wk = markernext(&marker)) { 9696 LIST_INSERT_AFTER(wk, &marker, wk_list); 9697 switch (wk->wk_type) { 9698 9699 case D_PAGEDEP: 9700 initiate_write_filepage(WK_PAGEDEP(wk), bp); 9701 continue; 9702 9703 case D_INODEDEP: 9704 inodedep = WK_INODEDEP(wk); 9705 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 9706 initiate_write_inodeblock_ufs1(inodedep, bp); 9707 else 9708 initiate_write_inodeblock_ufs2(inodedep, bp); 9709 continue; 9710 9711 case D_INDIRDEP: 9712 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 9713 continue; 9714 9715 case D_BMSAFEMAP: 9716 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 9717 continue; 9718 9719 case D_JSEG: 9720 WK_JSEG(wk)->js_buf = NULL; 9721 continue; 9722 9723 case D_FREEBLKS: 9724 freeblks = WK_FREEBLKS(wk); 9725 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 9726 /* 9727 * We have to wait for the freeblks to be journaled 9728 * before we can write an inodeblock with updated 9729 * pointers. Be careful to arrange the marker so 9730 * we revisit the freeblks if it's not removed by 9731 * the first jwait(). 9732 */ 9733 if (jblkdep != NULL) { 9734 LIST_REMOVE(&marker, wk_list); 9735 LIST_INSERT_BEFORE(wk, &marker, wk_list); 9736 jwait(&jblkdep->jb_list, MNT_WAIT); 9737 } 9738 continue; 9739 case D_ALLOCDIRECT: 9740 case D_ALLOCINDIR: 9741 /* 9742 * We have to wait for the jnewblk to be journaled 9743 * before we can write to a block if the contents 9744 * may be confused with an earlier file's indirect 9745 * at recovery time. Handle the marker as described 9746 * above. 9747 */ 9748 newblk = WK_NEWBLK(wk); 9749 if (newblk->nb_jnewblk != NULL && 9750 indirblk_lookup(newblk->nb_list.wk_mp, 9751 newblk->nb_newblkno)) { 9752 LIST_REMOVE(&marker, wk_list); 9753 LIST_INSERT_BEFORE(wk, &marker, wk_list); 9754 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 9755 } 9756 continue; 9757 9758 case D_SBDEP: 9759 initiate_write_sbdep(WK_SBDEP(wk)); 9760 continue; 9761 9762 case D_MKDIR: 9763 case D_FREEWORK: 9764 case D_FREEDEP: 9765 case D_JSEGDEP: 9766 continue; 9767 9768 default: 9769 panic("handle_disk_io_initiation: Unexpected type %s", 9770 TYPENAME(wk->wk_type)); 9771 /* NOTREACHED */ 9772 } 9773 } 9774 FREE_LOCK(&lk); 9775 PRELE(curproc); /* Allow swapout of kernel stack */ 9776 } 9777 9778 /* 9779 * Called from within the procedure above to deal with unsatisfied 9780 * allocation dependencies in a directory. The buffer must be locked, 9781 * thus, no I/O completion operations can occur while we are 9782 * manipulating its associated dependencies. 9783 */ 9784 static void 9785 initiate_write_filepage(pagedep, bp) 9786 struct pagedep *pagedep; 9787 struct buf *bp; 9788 { 9789 struct jremref *jremref; 9790 struct jmvref *jmvref; 9791 struct dirrem *dirrem; 9792 struct diradd *dap; 9793 struct direct *ep; 9794 int i; 9795 9796 if (pagedep->pd_state & IOSTARTED) { 9797 /* 9798 * This can only happen if there is a driver that does not 9799 * understand chaining. Here biodone will reissue the call 9800 * to strategy for the incomplete buffers. 9801 */ 9802 printf("initiate_write_filepage: already started\n"); 9803 return; 9804 } 9805 pagedep->pd_state |= IOSTARTED; 9806 /* 9807 * Wait for all journal remove dependencies to hit the disk. 9808 * We can not allow any potentially conflicting directory adds 9809 * to be visible before removes and rollback is too difficult. 9810 * lk may be dropped and re-acquired, however we hold the buf 9811 * locked so the dependency can not go away. 9812 */ 9813 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 9814 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 9815 jwait(&jremref->jr_list, MNT_WAIT); 9816 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 9817 jwait(&jmvref->jm_list, MNT_WAIT); 9818 for (i = 0; i < DAHASHSZ; i++) { 9819 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 9820 ep = (struct direct *) 9821 ((char *)bp->b_data + dap->da_offset); 9822 if (ep->d_ino != dap->da_newinum) 9823 panic("%s: dir inum %ju != new %ju", 9824 "initiate_write_filepage", 9825 (uintmax_t)ep->d_ino, 9826 (uintmax_t)dap->da_newinum); 9827 if (dap->da_state & DIRCHG) 9828 ep->d_ino = dap->da_previous->dm_oldinum; 9829 else 9830 ep->d_ino = 0; 9831 dap->da_state &= ~ATTACHED; 9832 dap->da_state |= UNDONE; 9833 } 9834 } 9835 } 9836 9837 /* 9838 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 9839 * Note that any bug fixes made to this routine must be done in the 9840 * version found below. 9841 * 9842 * Called from within the procedure above to deal with unsatisfied 9843 * allocation dependencies in an inodeblock. The buffer must be 9844 * locked, thus, no I/O completion operations can occur while we 9845 * are manipulating its associated dependencies. 9846 */ 9847 static void 9848 initiate_write_inodeblock_ufs1(inodedep, bp) 9849 struct inodedep *inodedep; 9850 struct buf *bp; /* The inode block */ 9851 { 9852 struct allocdirect *adp, *lastadp; 9853 struct ufs1_dinode *dp; 9854 struct ufs1_dinode *sip; 9855 struct inoref *inoref; 9856 struct fs *fs; 9857 ufs_lbn_t i; 9858 #ifdef INVARIANTS 9859 ufs_lbn_t prevlbn = 0; 9860 #endif 9861 int deplist; 9862 9863 if (inodedep->id_state & IOSTARTED) 9864 panic("initiate_write_inodeblock_ufs1: already started"); 9865 inodedep->id_state |= IOSTARTED; 9866 fs = inodedep->id_fs; 9867 dp = (struct ufs1_dinode *)bp->b_data + 9868 ino_to_fsbo(fs, inodedep->id_ino); 9869 9870 /* 9871 * If we're on the unlinked list but have not yet written our 9872 * next pointer initialize it here. 9873 */ 9874 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 9875 struct inodedep *inon; 9876 9877 inon = TAILQ_NEXT(inodedep, id_unlinked); 9878 dp->di_freelink = inon ? inon->id_ino : 0; 9879 } 9880 /* 9881 * If the bitmap is not yet written, then the allocated 9882 * inode cannot be written to disk. 9883 */ 9884 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 9885 if (inodedep->id_savedino1 != NULL) 9886 panic("initiate_write_inodeblock_ufs1: I/O underway"); 9887 FREE_LOCK(&lk); 9888 sip = malloc(sizeof(struct ufs1_dinode), 9889 M_SAVEDINO, M_SOFTDEP_FLAGS); 9890 ACQUIRE_LOCK(&lk); 9891 inodedep->id_savedino1 = sip; 9892 *inodedep->id_savedino1 = *dp; 9893 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 9894 dp->di_gen = inodedep->id_savedino1->di_gen; 9895 dp->di_freelink = inodedep->id_savedino1->di_freelink; 9896 return; 9897 } 9898 /* 9899 * If no dependencies, then there is nothing to roll back. 9900 */ 9901 inodedep->id_savedsize = dp->di_size; 9902 inodedep->id_savedextsize = 0; 9903 inodedep->id_savednlink = dp->di_nlink; 9904 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 9905 TAILQ_EMPTY(&inodedep->id_inoreflst)) 9906 return; 9907 /* 9908 * Revert the link count to that of the first unwritten journal entry. 9909 */ 9910 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 9911 if (inoref) 9912 dp->di_nlink = inoref->if_nlink; 9913 /* 9914 * Set the dependencies to busy. 9915 */ 9916 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 9917 adp = TAILQ_NEXT(adp, ad_next)) { 9918 #ifdef INVARIANTS 9919 if (deplist != 0 && prevlbn >= adp->ad_offset) 9920 panic("softdep_write_inodeblock: lbn order"); 9921 prevlbn = adp->ad_offset; 9922 if (adp->ad_offset < NDADDR && 9923 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 9924 panic("%s: direct pointer #%jd mismatch %d != %jd", 9925 "softdep_write_inodeblock", 9926 (intmax_t)adp->ad_offset, 9927 dp->di_db[adp->ad_offset], 9928 (intmax_t)adp->ad_newblkno); 9929 if (adp->ad_offset >= NDADDR && 9930 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 9931 panic("%s: indirect pointer #%jd mismatch %d != %jd", 9932 "softdep_write_inodeblock", 9933 (intmax_t)adp->ad_offset - NDADDR, 9934 dp->di_ib[adp->ad_offset - NDADDR], 9935 (intmax_t)adp->ad_newblkno); 9936 deplist |= 1 << adp->ad_offset; 9937 if ((adp->ad_state & ATTACHED) == 0) 9938 panic("softdep_write_inodeblock: Unknown state 0x%x", 9939 adp->ad_state); 9940 #endif /* INVARIANTS */ 9941 adp->ad_state &= ~ATTACHED; 9942 adp->ad_state |= UNDONE; 9943 } 9944 /* 9945 * The on-disk inode cannot claim to be any larger than the last 9946 * fragment that has been written. Otherwise, the on-disk inode 9947 * might have fragments that were not the last block in the file 9948 * which would corrupt the filesystem. 9949 */ 9950 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 9951 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 9952 if (adp->ad_offset >= NDADDR) 9953 break; 9954 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 9955 /* keep going until hitting a rollback to a frag */ 9956 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 9957 continue; 9958 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 9959 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 9960 #ifdef INVARIANTS 9961 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 9962 panic("softdep_write_inodeblock: lost dep1"); 9963 #endif /* INVARIANTS */ 9964 dp->di_db[i] = 0; 9965 } 9966 for (i = 0; i < NIADDR; i++) { 9967 #ifdef INVARIANTS 9968 if (dp->di_ib[i] != 0 && 9969 (deplist & ((1 << NDADDR) << i)) == 0) 9970 panic("softdep_write_inodeblock: lost dep2"); 9971 #endif /* INVARIANTS */ 9972 dp->di_ib[i] = 0; 9973 } 9974 return; 9975 } 9976 /* 9977 * If we have zero'ed out the last allocated block of the file, 9978 * roll back the size to the last currently allocated block. 9979 * We know that this last allocated block is a full-sized as 9980 * we already checked for fragments in the loop above. 9981 */ 9982 if (lastadp != NULL && 9983 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 9984 for (i = lastadp->ad_offset; i >= 0; i--) 9985 if (dp->di_db[i] != 0) 9986 break; 9987 dp->di_size = (i + 1) * fs->fs_bsize; 9988 } 9989 /* 9990 * The only dependencies are for indirect blocks. 9991 * 9992 * The file size for indirect block additions is not guaranteed. 9993 * Such a guarantee would be non-trivial to achieve. The conventional 9994 * synchronous write implementation also does not make this guarantee. 9995 * Fsck should catch and fix discrepancies. Arguably, the file size 9996 * can be over-estimated without destroying integrity when the file 9997 * moves into the indirect blocks (i.e., is large). If we want to 9998 * postpone fsck, we are stuck with this argument. 9999 */ 10000 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10001 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10002 } 10003 10004 /* 10005 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10006 * Note that any bug fixes made to this routine must be done in the 10007 * version found above. 10008 * 10009 * Called from within the procedure above to deal with unsatisfied 10010 * allocation dependencies in an inodeblock. The buffer must be 10011 * locked, thus, no I/O completion operations can occur while we 10012 * are manipulating its associated dependencies. 10013 */ 10014 static void 10015 initiate_write_inodeblock_ufs2(inodedep, bp) 10016 struct inodedep *inodedep; 10017 struct buf *bp; /* The inode block */ 10018 { 10019 struct allocdirect *adp, *lastadp; 10020 struct ufs2_dinode *dp; 10021 struct ufs2_dinode *sip; 10022 struct inoref *inoref; 10023 struct fs *fs; 10024 ufs_lbn_t i; 10025 #ifdef INVARIANTS 10026 ufs_lbn_t prevlbn = 0; 10027 #endif 10028 int deplist; 10029 10030 if (inodedep->id_state & IOSTARTED) 10031 panic("initiate_write_inodeblock_ufs2: already started"); 10032 inodedep->id_state |= IOSTARTED; 10033 fs = inodedep->id_fs; 10034 dp = (struct ufs2_dinode *)bp->b_data + 10035 ino_to_fsbo(fs, inodedep->id_ino); 10036 10037 /* 10038 * If we're on the unlinked list but have not yet written our 10039 * next pointer initialize it here. 10040 */ 10041 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10042 struct inodedep *inon; 10043 10044 inon = TAILQ_NEXT(inodedep, id_unlinked); 10045 dp->di_freelink = inon ? inon->id_ino : 0; 10046 } 10047 /* 10048 * If the bitmap is not yet written, then the allocated 10049 * inode cannot be written to disk. 10050 */ 10051 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10052 if (inodedep->id_savedino2 != NULL) 10053 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10054 FREE_LOCK(&lk); 10055 sip = malloc(sizeof(struct ufs2_dinode), 10056 M_SAVEDINO, M_SOFTDEP_FLAGS); 10057 ACQUIRE_LOCK(&lk); 10058 inodedep->id_savedino2 = sip; 10059 *inodedep->id_savedino2 = *dp; 10060 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10061 dp->di_gen = inodedep->id_savedino2->di_gen; 10062 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10063 return; 10064 } 10065 /* 10066 * If no dependencies, then there is nothing to roll back. 10067 */ 10068 inodedep->id_savedsize = dp->di_size; 10069 inodedep->id_savedextsize = dp->di_extsize; 10070 inodedep->id_savednlink = dp->di_nlink; 10071 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10072 TAILQ_EMPTY(&inodedep->id_extupdt) && 10073 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10074 return; 10075 /* 10076 * Revert the link count to that of the first unwritten journal entry. 10077 */ 10078 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10079 if (inoref) 10080 dp->di_nlink = inoref->if_nlink; 10081 10082 /* 10083 * Set the ext data dependencies to busy. 10084 */ 10085 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10086 adp = TAILQ_NEXT(adp, ad_next)) { 10087 #ifdef INVARIANTS 10088 if (deplist != 0 && prevlbn >= adp->ad_offset) 10089 panic("softdep_write_inodeblock: lbn order"); 10090 prevlbn = adp->ad_offset; 10091 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10092 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10093 "softdep_write_inodeblock", 10094 (intmax_t)adp->ad_offset, 10095 (intmax_t)dp->di_extb[adp->ad_offset], 10096 (intmax_t)adp->ad_newblkno); 10097 deplist |= 1 << adp->ad_offset; 10098 if ((adp->ad_state & ATTACHED) == 0) 10099 panic("softdep_write_inodeblock: Unknown state 0x%x", 10100 adp->ad_state); 10101 #endif /* INVARIANTS */ 10102 adp->ad_state &= ~ATTACHED; 10103 adp->ad_state |= UNDONE; 10104 } 10105 /* 10106 * The on-disk inode cannot claim to be any larger than the last 10107 * fragment that has been written. Otherwise, the on-disk inode 10108 * might have fragments that were not the last block in the ext 10109 * data which would corrupt the filesystem. 10110 */ 10111 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10112 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10113 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10114 /* keep going until hitting a rollback to a frag */ 10115 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10116 continue; 10117 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10118 for (i = adp->ad_offset + 1; i < NXADDR; i++) { 10119 #ifdef INVARIANTS 10120 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10121 panic("softdep_write_inodeblock: lost dep1"); 10122 #endif /* INVARIANTS */ 10123 dp->di_extb[i] = 0; 10124 } 10125 lastadp = NULL; 10126 break; 10127 } 10128 /* 10129 * If we have zero'ed out the last allocated block of the ext 10130 * data, roll back the size to the last currently allocated block. 10131 * We know that this last allocated block is a full-sized as 10132 * we already checked for fragments in the loop above. 10133 */ 10134 if (lastadp != NULL && 10135 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10136 for (i = lastadp->ad_offset; i >= 0; i--) 10137 if (dp->di_extb[i] != 0) 10138 break; 10139 dp->di_extsize = (i + 1) * fs->fs_bsize; 10140 } 10141 /* 10142 * Set the file data dependencies to busy. 10143 */ 10144 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10145 adp = TAILQ_NEXT(adp, ad_next)) { 10146 #ifdef INVARIANTS 10147 if (deplist != 0 && prevlbn >= adp->ad_offset) 10148 panic("softdep_write_inodeblock: lbn order"); 10149 if ((adp->ad_state & ATTACHED) == 0) 10150 panic("inodedep %p and adp %p not attached", inodedep, adp); 10151 prevlbn = adp->ad_offset; 10152 if (adp->ad_offset < NDADDR && 10153 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10154 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10155 "softdep_write_inodeblock", 10156 (intmax_t)adp->ad_offset, 10157 (intmax_t)dp->di_db[adp->ad_offset], 10158 (intmax_t)adp->ad_newblkno); 10159 if (adp->ad_offset >= NDADDR && 10160 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10161 panic("%s indirect pointer #%jd mismatch %jd != %jd", 10162 "softdep_write_inodeblock:", 10163 (intmax_t)adp->ad_offset - NDADDR, 10164 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR], 10165 (intmax_t)adp->ad_newblkno); 10166 deplist |= 1 << adp->ad_offset; 10167 if ((adp->ad_state & ATTACHED) == 0) 10168 panic("softdep_write_inodeblock: Unknown state 0x%x", 10169 adp->ad_state); 10170 #endif /* INVARIANTS */ 10171 adp->ad_state &= ~ATTACHED; 10172 adp->ad_state |= UNDONE; 10173 } 10174 /* 10175 * The on-disk inode cannot claim to be any larger than the last 10176 * fragment that has been written. Otherwise, the on-disk inode 10177 * might have fragments that were not the last block in the file 10178 * which would corrupt the filesystem. 10179 */ 10180 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10181 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10182 if (adp->ad_offset >= NDADDR) 10183 break; 10184 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10185 /* keep going until hitting a rollback to a frag */ 10186 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10187 continue; 10188 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10189 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10190 #ifdef INVARIANTS 10191 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10192 panic("softdep_write_inodeblock: lost dep2"); 10193 #endif /* INVARIANTS */ 10194 dp->di_db[i] = 0; 10195 } 10196 for (i = 0; i < NIADDR; i++) { 10197 #ifdef INVARIANTS 10198 if (dp->di_ib[i] != 0 && 10199 (deplist & ((1 << NDADDR) << i)) == 0) 10200 panic("softdep_write_inodeblock: lost dep3"); 10201 #endif /* INVARIANTS */ 10202 dp->di_ib[i] = 0; 10203 } 10204 return; 10205 } 10206 /* 10207 * If we have zero'ed out the last allocated block of the file, 10208 * roll back the size to the last currently allocated block. 10209 * We know that this last allocated block is a full-sized as 10210 * we already checked for fragments in the loop above. 10211 */ 10212 if (lastadp != NULL && 10213 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10214 for (i = lastadp->ad_offset; i >= 0; i--) 10215 if (dp->di_db[i] != 0) 10216 break; 10217 dp->di_size = (i + 1) * fs->fs_bsize; 10218 } 10219 /* 10220 * The only dependencies are for indirect blocks. 10221 * 10222 * The file size for indirect block additions is not guaranteed. 10223 * Such a guarantee would be non-trivial to achieve. The conventional 10224 * synchronous write implementation also does not make this guarantee. 10225 * Fsck should catch and fix discrepancies. Arguably, the file size 10226 * can be over-estimated without destroying integrity when the file 10227 * moves into the indirect blocks (i.e., is large). If we want to 10228 * postpone fsck, we are stuck with this argument. 10229 */ 10230 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10231 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10232 } 10233 10234 /* 10235 * Cancel an indirdep as a result of truncation. Release all of the 10236 * children allocindirs and place their journal work on the appropriate 10237 * list. 10238 */ 10239 static void 10240 cancel_indirdep(indirdep, bp, freeblks) 10241 struct indirdep *indirdep; 10242 struct buf *bp; 10243 struct freeblks *freeblks; 10244 { 10245 struct allocindir *aip; 10246 10247 /* 10248 * None of the indirect pointers will ever be visible, 10249 * so they can simply be tossed. GOINGAWAY ensures 10250 * that allocated pointers will be saved in the buffer 10251 * cache until they are freed. Note that they will 10252 * only be able to be found by their physical address 10253 * since the inode mapping the logical address will 10254 * be gone. The save buffer used for the safe copy 10255 * was allocated in setup_allocindir_phase2 using 10256 * the physical address so it could be used for this 10257 * purpose. Hence we swap the safe copy with the real 10258 * copy, allowing the safe copy to be freed and holding 10259 * on to the real copy for later use in indir_trunc. 10260 */ 10261 if (indirdep->ir_state & GOINGAWAY) 10262 panic("cancel_indirdep: already gone"); 10263 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10264 indirdep->ir_state |= DEPCOMPLETE; 10265 LIST_REMOVE(indirdep, ir_next); 10266 } 10267 indirdep->ir_state |= GOINGAWAY; 10268 VFSTOUFS(indirdep->ir_list.wk_mp)->um_numindirdeps += 1; 10269 /* 10270 * Pass in bp for blocks still have journal writes 10271 * pending so we can cancel them on their own. 10272 */ 10273 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 10274 cancel_allocindir(aip, bp, freeblks, 0); 10275 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) 10276 cancel_allocindir(aip, NULL, freeblks, 0); 10277 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) 10278 cancel_allocindir(aip, NULL, freeblks, 0); 10279 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != 0) 10280 cancel_allocindir(aip, NULL, freeblks, 0); 10281 /* 10282 * If there are pending partial truncations we need to keep the 10283 * old block copy around until they complete. This is because 10284 * the current b_data is not a perfect superset of the available 10285 * blocks. 10286 */ 10287 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10288 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10289 else 10290 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10291 WORKLIST_REMOVE(&indirdep->ir_list); 10292 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10293 indirdep->ir_bp = NULL; 10294 indirdep->ir_freeblks = freeblks; 10295 } 10296 10297 /* 10298 * Free an indirdep once it no longer has new pointers to track. 10299 */ 10300 static void 10301 free_indirdep(indirdep) 10302 struct indirdep *indirdep; 10303 { 10304 10305 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10306 ("free_indirdep: Indir trunc list not empty.")); 10307 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10308 ("free_indirdep: Complete head not empty.")); 10309 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10310 ("free_indirdep: write head not empty.")); 10311 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10312 ("free_indirdep: done head not empty.")); 10313 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10314 ("free_indirdep: deplist head not empty.")); 10315 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10316 ("free_indirdep: %p still on newblk list.", indirdep)); 10317 KASSERT(indirdep->ir_saveddata == NULL, 10318 ("free_indirdep: %p still has saved data.", indirdep)); 10319 if (indirdep->ir_state & ONWORKLIST) 10320 WORKLIST_REMOVE(&indirdep->ir_list); 10321 WORKITEM_FREE(indirdep, D_INDIRDEP); 10322 } 10323 10324 /* 10325 * Called before a write to an indirdep. This routine is responsible for 10326 * rolling back pointers to a safe state which includes only those 10327 * allocindirs which have been completed. 10328 */ 10329 static void 10330 initiate_write_indirdep(indirdep, bp) 10331 struct indirdep *indirdep; 10332 struct buf *bp; 10333 { 10334 10335 indirdep->ir_state |= IOSTARTED; 10336 if (indirdep->ir_state & GOINGAWAY) 10337 panic("disk_io_initiation: indirdep gone"); 10338 /* 10339 * If there are no remaining dependencies, this will be writing 10340 * the real pointers. 10341 */ 10342 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10343 TAILQ_EMPTY(&indirdep->ir_trunc)) 10344 return; 10345 /* 10346 * Replace up-to-date version with safe version. 10347 */ 10348 if (indirdep->ir_saveddata == NULL) { 10349 FREE_LOCK(&lk); 10350 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10351 M_SOFTDEP_FLAGS); 10352 ACQUIRE_LOCK(&lk); 10353 } 10354 indirdep->ir_state &= ~ATTACHED; 10355 indirdep->ir_state |= UNDONE; 10356 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10357 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10358 bp->b_bcount); 10359 } 10360 10361 /* 10362 * Called when an inode has been cleared in a cg bitmap. This finally 10363 * eliminates any canceled jaddrefs 10364 */ 10365 void 10366 softdep_setup_inofree(mp, bp, ino, wkhd) 10367 struct mount *mp; 10368 struct buf *bp; 10369 ino_t ino; 10370 struct workhead *wkhd; 10371 { 10372 struct worklist *wk, *wkn; 10373 struct inodedep *inodedep; 10374 uint8_t *inosused; 10375 struct cg *cgp; 10376 struct fs *fs; 10377 10378 ACQUIRE_LOCK(&lk); 10379 fs = VFSTOUFS(mp)->um_fs; 10380 cgp = (struct cg *)bp->b_data; 10381 inosused = cg_inosused(cgp); 10382 if (isset(inosused, ino % fs->fs_ipg)) 10383 panic("softdep_setup_inofree: inode %ju not freed.", 10384 (uintmax_t)ino); 10385 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10386 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10387 (uintmax_t)ino, inodedep); 10388 if (wkhd) { 10389 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10390 if (wk->wk_type != D_JADDREF) 10391 continue; 10392 WORKLIST_REMOVE(wk); 10393 /* 10394 * We can free immediately even if the jaddref 10395 * isn't attached in a background write as now 10396 * the bitmaps are reconciled. 10397 */ 10398 wk->wk_state |= COMPLETE | ATTACHED; 10399 free_jaddref(WK_JADDREF(wk)); 10400 } 10401 jwork_move(&bp->b_dep, wkhd); 10402 } 10403 FREE_LOCK(&lk); 10404 } 10405 10406 10407 /* 10408 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10409 * map. Any dependencies waiting for the write to clear are added to the 10410 * buf's list and any jnewblks that are being canceled are discarded 10411 * immediately. 10412 */ 10413 void 10414 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10415 struct mount *mp; 10416 struct buf *bp; 10417 ufs2_daddr_t blkno; 10418 int frags; 10419 struct workhead *wkhd; 10420 { 10421 struct bmsafemap *bmsafemap; 10422 struct jnewblk *jnewblk; 10423 struct worklist *wk; 10424 struct fs *fs; 10425 #ifdef SUJ_DEBUG 10426 uint8_t *blksfree; 10427 struct cg *cgp; 10428 ufs2_daddr_t jstart; 10429 ufs2_daddr_t jend; 10430 ufs2_daddr_t end; 10431 long bno; 10432 int i; 10433 #endif 10434 10435 CTR3(KTR_SUJ, 10436 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10437 blkno, frags, wkhd); 10438 10439 ACQUIRE_LOCK(&lk); 10440 /* Lookup the bmsafemap so we track when it is dirty. */ 10441 fs = VFSTOUFS(mp)->um_fs; 10442 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10443 /* 10444 * Detach any jnewblks which have been canceled. They must linger 10445 * until the bitmap is cleared again by ffs_blkfree() to prevent 10446 * an unjournaled allocation from hitting the disk. 10447 */ 10448 if (wkhd) { 10449 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10450 CTR2(KTR_SUJ, 10451 "softdep_setup_blkfree: blkno %jd wk type %d", 10452 blkno, wk->wk_type); 10453 WORKLIST_REMOVE(wk); 10454 if (wk->wk_type != D_JNEWBLK) { 10455 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10456 continue; 10457 } 10458 jnewblk = WK_JNEWBLK(wk); 10459 KASSERT(jnewblk->jn_state & GOINGAWAY, 10460 ("softdep_setup_blkfree: jnewblk not canceled.")); 10461 #ifdef SUJ_DEBUG 10462 /* 10463 * Assert that this block is free in the bitmap 10464 * before we discard the jnewblk. 10465 */ 10466 cgp = (struct cg *)bp->b_data; 10467 blksfree = cg_blksfree(cgp); 10468 bno = dtogd(fs, jnewblk->jn_blkno); 10469 for (i = jnewblk->jn_oldfrags; 10470 i < jnewblk->jn_frags; i++) { 10471 if (isset(blksfree, bno + i)) 10472 continue; 10473 panic("softdep_setup_blkfree: not free"); 10474 } 10475 #endif 10476 /* 10477 * Even if it's not attached we can free immediately 10478 * as the new bitmap is correct. 10479 */ 10480 wk->wk_state |= COMPLETE | ATTACHED; 10481 free_jnewblk(jnewblk); 10482 } 10483 } 10484 10485 #ifdef SUJ_DEBUG 10486 /* 10487 * Assert that we are not freeing a block which has an outstanding 10488 * allocation dependency. 10489 */ 10490 fs = VFSTOUFS(mp)->um_fs; 10491 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10492 end = blkno + frags; 10493 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10494 /* 10495 * Don't match against blocks that will be freed when the 10496 * background write is done. 10497 */ 10498 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10499 (COMPLETE | DEPCOMPLETE)) 10500 continue; 10501 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10502 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10503 if ((blkno >= jstart && blkno < jend) || 10504 (end > jstart && end <= jend)) { 10505 printf("state 0x%X %jd - %d %d dep %p\n", 10506 jnewblk->jn_state, jnewblk->jn_blkno, 10507 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10508 jnewblk->jn_dep); 10509 panic("softdep_setup_blkfree: " 10510 "%jd-%jd(%d) overlaps with %jd-%jd", 10511 blkno, end, frags, jstart, jend); 10512 } 10513 } 10514 #endif 10515 FREE_LOCK(&lk); 10516 } 10517 10518 /* 10519 * Revert a block allocation when the journal record that describes it 10520 * is not yet written. 10521 */ 10522 int 10523 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10524 struct jnewblk *jnewblk; 10525 struct fs *fs; 10526 struct cg *cgp; 10527 uint8_t *blksfree; 10528 { 10529 ufs1_daddr_t fragno; 10530 long cgbno, bbase; 10531 int frags, blk; 10532 int i; 10533 10534 frags = 0; 10535 cgbno = dtogd(fs, jnewblk->jn_blkno); 10536 /* 10537 * We have to test which frags need to be rolled back. We may 10538 * be operating on a stale copy when doing background writes. 10539 */ 10540 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10541 if (isclr(blksfree, cgbno + i)) 10542 frags++; 10543 if (frags == 0) 10544 return (0); 10545 /* 10546 * This is mostly ffs_blkfree() sans some validation and 10547 * superblock updates. 10548 */ 10549 if (frags == fs->fs_frag) { 10550 fragno = fragstoblks(fs, cgbno); 10551 ffs_setblock(fs, blksfree, fragno); 10552 ffs_clusteracct(fs, cgp, fragno, 1); 10553 cgp->cg_cs.cs_nbfree++; 10554 } else { 10555 cgbno += jnewblk->jn_oldfrags; 10556 bbase = cgbno - fragnum(fs, cgbno); 10557 /* Decrement the old frags. */ 10558 blk = blkmap(fs, blksfree, bbase); 10559 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10560 /* Deallocate the fragment */ 10561 for (i = 0; i < frags; i++) 10562 setbit(blksfree, cgbno + i); 10563 cgp->cg_cs.cs_nffree += frags; 10564 /* Add back in counts associated with the new frags */ 10565 blk = blkmap(fs, blksfree, bbase); 10566 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10567 /* If a complete block has been reassembled, account for it. */ 10568 fragno = fragstoblks(fs, bbase); 10569 if (ffs_isblock(fs, blksfree, fragno)) { 10570 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10571 ffs_clusteracct(fs, cgp, fragno, 1); 10572 cgp->cg_cs.cs_nbfree++; 10573 } 10574 } 10575 stat_jnewblk++; 10576 jnewblk->jn_state &= ~ATTACHED; 10577 jnewblk->jn_state |= UNDONE; 10578 10579 return (frags); 10580 } 10581 10582 static void 10583 initiate_write_bmsafemap(bmsafemap, bp) 10584 struct bmsafemap *bmsafemap; 10585 struct buf *bp; /* The cg block. */ 10586 { 10587 struct jaddref *jaddref; 10588 struct jnewblk *jnewblk; 10589 uint8_t *inosused; 10590 uint8_t *blksfree; 10591 struct cg *cgp; 10592 struct fs *fs; 10593 ino_t ino; 10594 10595 if (bmsafemap->sm_state & IOSTARTED) 10596 return; 10597 bmsafemap->sm_state |= IOSTARTED; 10598 /* 10599 * Clear any inode allocations which are pending journal writes. 10600 */ 10601 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10602 cgp = (struct cg *)bp->b_data; 10603 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10604 inosused = cg_inosused(cgp); 10605 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10606 ino = jaddref->ja_ino % fs->fs_ipg; 10607 if (isset(inosused, ino)) { 10608 if ((jaddref->ja_mode & IFMT) == IFDIR) 10609 cgp->cg_cs.cs_ndir--; 10610 cgp->cg_cs.cs_nifree++; 10611 clrbit(inosused, ino); 10612 jaddref->ja_state &= ~ATTACHED; 10613 jaddref->ja_state |= UNDONE; 10614 stat_jaddref++; 10615 } else 10616 panic("initiate_write_bmsafemap: inode %ju " 10617 "marked free", (uintmax_t)jaddref->ja_ino); 10618 } 10619 } 10620 /* 10621 * Clear any block allocations which are pending journal writes. 10622 */ 10623 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10624 cgp = (struct cg *)bp->b_data; 10625 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10626 blksfree = cg_blksfree(cgp); 10627 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10628 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10629 continue; 10630 panic("initiate_write_bmsafemap: block %jd " 10631 "marked free", jnewblk->jn_blkno); 10632 } 10633 } 10634 /* 10635 * Move allocation lists to the written lists so they can be 10636 * cleared once the block write is complete. 10637 */ 10638 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 10639 inodedep, id_deps); 10640 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 10641 newblk, nb_deps); 10642 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 10643 wk_list); 10644 } 10645 10646 /* 10647 * This routine is called during the completion interrupt 10648 * service routine for a disk write (from the procedure called 10649 * by the device driver to inform the filesystem caches of 10650 * a request completion). It should be called early in this 10651 * procedure, before the block is made available to other 10652 * processes or other routines are called. 10653 * 10654 */ 10655 static void 10656 softdep_disk_write_complete(bp) 10657 struct buf *bp; /* describes the completed disk write */ 10658 { 10659 struct worklist *wk; 10660 struct worklist *owk; 10661 struct workhead reattach; 10662 struct freeblks *freeblks; 10663 struct buf *sbp; 10664 10665 /* 10666 * If an error occurred while doing the write, then the data 10667 * has not hit the disk and the dependencies cannot be unrolled. 10668 */ 10669 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 10670 return; 10671 LIST_INIT(&reattach); 10672 /* 10673 * This lock must not be released anywhere in this code segment. 10674 */ 10675 sbp = NULL; 10676 owk = NULL; 10677 ACQUIRE_LOCK(&lk); 10678 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 10679 WORKLIST_REMOVE(wk); 10680 dep_write[wk->wk_type]++; 10681 if (wk == owk) 10682 panic("duplicate worklist: %p\n", wk); 10683 owk = wk; 10684 switch (wk->wk_type) { 10685 10686 case D_PAGEDEP: 10687 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 10688 WORKLIST_INSERT(&reattach, wk); 10689 continue; 10690 10691 case D_INODEDEP: 10692 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 10693 WORKLIST_INSERT(&reattach, wk); 10694 continue; 10695 10696 case D_BMSAFEMAP: 10697 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp)) 10698 WORKLIST_INSERT(&reattach, wk); 10699 continue; 10700 10701 case D_MKDIR: 10702 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 10703 continue; 10704 10705 case D_ALLOCDIRECT: 10706 wk->wk_state |= COMPLETE; 10707 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 10708 continue; 10709 10710 case D_ALLOCINDIR: 10711 wk->wk_state |= COMPLETE; 10712 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 10713 continue; 10714 10715 case D_INDIRDEP: 10716 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp)) 10717 WORKLIST_INSERT(&reattach, wk); 10718 continue; 10719 10720 case D_FREEBLKS: 10721 wk->wk_state |= COMPLETE; 10722 freeblks = WK_FREEBLKS(wk); 10723 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 10724 LIST_EMPTY(&freeblks->fb_jblkdephd)) 10725 add_to_worklist(wk, WK_NODELAY); 10726 continue; 10727 10728 case D_FREEWORK: 10729 handle_written_freework(WK_FREEWORK(wk)); 10730 break; 10731 10732 case D_JSEGDEP: 10733 free_jsegdep(WK_JSEGDEP(wk)); 10734 continue; 10735 10736 case D_JSEG: 10737 handle_written_jseg(WK_JSEG(wk), bp); 10738 continue; 10739 10740 case D_SBDEP: 10741 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 10742 WORKLIST_INSERT(&reattach, wk); 10743 continue; 10744 10745 case D_FREEDEP: 10746 free_freedep(WK_FREEDEP(wk)); 10747 continue; 10748 10749 default: 10750 panic("handle_disk_write_complete: Unknown type %s", 10751 TYPENAME(wk->wk_type)); 10752 /* NOTREACHED */ 10753 } 10754 } 10755 /* 10756 * Reattach any requests that must be redone. 10757 */ 10758 while ((wk = LIST_FIRST(&reattach)) != NULL) { 10759 WORKLIST_REMOVE(wk); 10760 WORKLIST_INSERT(&bp->b_dep, wk); 10761 } 10762 FREE_LOCK(&lk); 10763 if (sbp) 10764 brelse(sbp); 10765 } 10766 10767 /* 10768 * Called from within softdep_disk_write_complete above. Note that 10769 * this routine is always called from interrupt level with further 10770 * splbio interrupts blocked. 10771 */ 10772 static void 10773 handle_allocdirect_partdone(adp, wkhd) 10774 struct allocdirect *adp; /* the completed allocdirect */ 10775 struct workhead *wkhd; /* Work to do when inode is writtne. */ 10776 { 10777 struct allocdirectlst *listhead; 10778 struct allocdirect *listadp; 10779 struct inodedep *inodedep; 10780 long bsize; 10781 10782 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 10783 return; 10784 /* 10785 * The on-disk inode cannot claim to be any larger than the last 10786 * fragment that has been written. Otherwise, the on-disk inode 10787 * might have fragments that were not the last block in the file 10788 * which would corrupt the filesystem. Thus, we cannot free any 10789 * allocdirects after one whose ad_oldblkno claims a fragment as 10790 * these blocks must be rolled back to zero before writing the inode. 10791 * We check the currently active set of allocdirects in id_inoupdt 10792 * or id_extupdt as appropriate. 10793 */ 10794 inodedep = adp->ad_inodedep; 10795 bsize = inodedep->id_fs->fs_bsize; 10796 if (adp->ad_state & EXTDATA) 10797 listhead = &inodedep->id_extupdt; 10798 else 10799 listhead = &inodedep->id_inoupdt; 10800 TAILQ_FOREACH(listadp, listhead, ad_next) { 10801 /* found our block */ 10802 if (listadp == adp) 10803 break; 10804 /* continue if ad_oldlbn is not a fragment */ 10805 if (listadp->ad_oldsize == 0 || 10806 listadp->ad_oldsize == bsize) 10807 continue; 10808 /* hit a fragment */ 10809 return; 10810 } 10811 /* 10812 * If we have reached the end of the current list without 10813 * finding the just finished dependency, then it must be 10814 * on the future dependency list. Future dependencies cannot 10815 * be freed until they are moved to the current list. 10816 */ 10817 if (listadp == NULL) { 10818 #ifdef DEBUG 10819 if (adp->ad_state & EXTDATA) 10820 listhead = &inodedep->id_newextupdt; 10821 else 10822 listhead = &inodedep->id_newinoupdt; 10823 TAILQ_FOREACH(listadp, listhead, ad_next) 10824 /* found our block */ 10825 if (listadp == adp) 10826 break; 10827 if (listadp == NULL) 10828 panic("handle_allocdirect_partdone: lost dep"); 10829 #endif /* DEBUG */ 10830 return; 10831 } 10832 /* 10833 * If we have found the just finished dependency, then queue 10834 * it along with anything that follows it that is complete. 10835 * Since the pointer has not yet been written in the inode 10836 * as the dependency prevents it, place the allocdirect on the 10837 * bufwait list where it will be freed once the pointer is 10838 * valid. 10839 */ 10840 if (wkhd == NULL) 10841 wkhd = &inodedep->id_bufwait; 10842 for (; adp; adp = listadp) { 10843 listadp = TAILQ_NEXT(adp, ad_next); 10844 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 10845 return; 10846 TAILQ_REMOVE(listhead, adp, ad_next); 10847 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 10848 } 10849 } 10850 10851 /* 10852 * Called from within softdep_disk_write_complete above. This routine 10853 * completes successfully written allocindirs. 10854 */ 10855 static void 10856 handle_allocindir_partdone(aip) 10857 struct allocindir *aip; /* the completed allocindir */ 10858 { 10859 struct indirdep *indirdep; 10860 10861 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 10862 return; 10863 indirdep = aip->ai_indirdep; 10864 LIST_REMOVE(aip, ai_next); 10865 /* 10866 * Don't set a pointer while the buffer is undergoing IO or while 10867 * we have active truncations. 10868 */ 10869 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 10870 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 10871 return; 10872 } 10873 if (indirdep->ir_state & UFS1FMT) 10874 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 10875 aip->ai_newblkno; 10876 else 10877 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 10878 aip->ai_newblkno; 10879 /* 10880 * Await the pointer write before freeing the allocindir. 10881 */ 10882 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 10883 } 10884 10885 /* 10886 * Release segments held on a jwork list. 10887 */ 10888 static void 10889 handle_jwork(wkhd) 10890 struct workhead *wkhd; 10891 { 10892 struct worklist *wk; 10893 10894 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10895 WORKLIST_REMOVE(wk); 10896 switch (wk->wk_type) { 10897 case D_JSEGDEP: 10898 free_jsegdep(WK_JSEGDEP(wk)); 10899 continue; 10900 case D_FREEDEP: 10901 free_freedep(WK_FREEDEP(wk)); 10902 continue; 10903 case D_FREEFRAG: 10904 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 10905 WORKITEM_FREE(wk, D_FREEFRAG); 10906 continue; 10907 case D_FREEWORK: 10908 handle_written_freework(WK_FREEWORK(wk)); 10909 continue; 10910 default: 10911 panic("handle_jwork: Unknown type %s\n", 10912 TYPENAME(wk->wk_type)); 10913 } 10914 } 10915 } 10916 10917 /* 10918 * Handle the bufwait list on an inode when it is safe to release items 10919 * held there. This normally happens after an inode block is written but 10920 * may be delayed and handled later if there are pending journal items that 10921 * are not yet safe to be released. 10922 */ 10923 static struct freefile * 10924 handle_bufwait(inodedep, refhd) 10925 struct inodedep *inodedep; 10926 struct workhead *refhd; 10927 { 10928 struct jaddref *jaddref; 10929 struct freefile *freefile; 10930 struct worklist *wk; 10931 10932 freefile = NULL; 10933 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 10934 WORKLIST_REMOVE(wk); 10935 switch (wk->wk_type) { 10936 case D_FREEFILE: 10937 /* 10938 * We defer adding freefile to the worklist 10939 * until all other additions have been made to 10940 * ensure that it will be done after all the 10941 * old blocks have been freed. 10942 */ 10943 if (freefile != NULL) 10944 panic("handle_bufwait: freefile"); 10945 freefile = WK_FREEFILE(wk); 10946 continue; 10947 10948 case D_MKDIR: 10949 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 10950 continue; 10951 10952 case D_DIRADD: 10953 diradd_inode_written(WK_DIRADD(wk), inodedep); 10954 continue; 10955 10956 case D_FREEFRAG: 10957 wk->wk_state |= COMPLETE; 10958 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 10959 add_to_worklist(wk, 0); 10960 continue; 10961 10962 case D_DIRREM: 10963 wk->wk_state |= COMPLETE; 10964 add_to_worklist(wk, 0); 10965 continue; 10966 10967 case D_ALLOCDIRECT: 10968 case D_ALLOCINDIR: 10969 free_newblk(WK_NEWBLK(wk)); 10970 continue; 10971 10972 case D_JNEWBLK: 10973 wk->wk_state |= COMPLETE; 10974 free_jnewblk(WK_JNEWBLK(wk)); 10975 continue; 10976 10977 /* 10978 * Save freed journal segments and add references on 10979 * the supplied list which will delay their release 10980 * until the cg bitmap is cleared on disk. 10981 */ 10982 case D_JSEGDEP: 10983 if (refhd == NULL) 10984 free_jsegdep(WK_JSEGDEP(wk)); 10985 else 10986 WORKLIST_INSERT(refhd, wk); 10987 continue; 10988 10989 case D_JADDREF: 10990 jaddref = WK_JADDREF(wk); 10991 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 10992 if_deps); 10993 /* 10994 * Transfer any jaddrefs to the list to be freed with 10995 * the bitmap if we're handling a removed file. 10996 */ 10997 if (refhd == NULL) { 10998 wk->wk_state |= COMPLETE; 10999 free_jaddref(jaddref); 11000 } else 11001 WORKLIST_INSERT(refhd, wk); 11002 continue; 11003 11004 default: 11005 panic("handle_bufwait: Unknown type %p(%s)", 11006 wk, TYPENAME(wk->wk_type)); 11007 /* NOTREACHED */ 11008 } 11009 } 11010 return (freefile); 11011 } 11012 /* 11013 * Called from within softdep_disk_write_complete above to restore 11014 * in-memory inode block contents to their most up-to-date state. Note 11015 * that this routine is always called from interrupt level with further 11016 * splbio interrupts blocked. 11017 */ 11018 static int 11019 handle_written_inodeblock(inodedep, bp) 11020 struct inodedep *inodedep; 11021 struct buf *bp; /* buffer containing the inode block */ 11022 { 11023 struct freefile *freefile; 11024 struct allocdirect *adp, *nextadp; 11025 struct ufs1_dinode *dp1 = NULL; 11026 struct ufs2_dinode *dp2 = NULL; 11027 struct workhead wkhd; 11028 int hadchanges, fstype; 11029 ino_t freelink; 11030 11031 LIST_INIT(&wkhd); 11032 hadchanges = 0; 11033 freefile = NULL; 11034 if ((inodedep->id_state & IOSTARTED) == 0) 11035 panic("handle_written_inodeblock: not started"); 11036 inodedep->id_state &= ~IOSTARTED; 11037 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11038 fstype = UFS1; 11039 dp1 = (struct ufs1_dinode *)bp->b_data + 11040 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11041 freelink = dp1->di_freelink; 11042 } else { 11043 fstype = UFS2; 11044 dp2 = (struct ufs2_dinode *)bp->b_data + 11045 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11046 freelink = dp2->di_freelink; 11047 } 11048 /* 11049 * Leave this inodeblock dirty until it's in the list. 11050 */ 11051 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) { 11052 struct inodedep *inon; 11053 11054 inon = TAILQ_NEXT(inodedep, id_unlinked); 11055 if ((inon == NULL && freelink == 0) || 11056 (inon && inon->id_ino == freelink)) { 11057 if (inon) 11058 inon->id_state |= UNLINKPREV; 11059 inodedep->id_state |= UNLINKNEXT; 11060 } 11061 hadchanges = 1; 11062 } 11063 /* 11064 * If we had to rollback the inode allocation because of 11065 * bitmaps being incomplete, then simply restore it. 11066 * Keep the block dirty so that it will not be reclaimed until 11067 * all associated dependencies have been cleared and the 11068 * corresponding updates written to disk. 11069 */ 11070 if (inodedep->id_savedino1 != NULL) { 11071 hadchanges = 1; 11072 if (fstype == UFS1) 11073 *dp1 = *inodedep->id_savedino1; 11074 else 11075 *dp2 = *inodedep->id_savedino2; 11076 free(inodedep->id_savedino1, M_SAVEDINO); 11077 inodedep->id_savedino1 = NULL; 11078 if ((bp->b_flags & B_DELWRI) == 0) 11079 stat_inode_bitmap++; 11080 bdirty(bp); 11081 /* 11082 * If the inode is clear here and GOINGAWAY it will never 11083 * be written. Process the bufwait and clear any pending 11084 * work which may include the freefile. 11085 */ 11086 if (inodedep->id_state & GOINGAWAY) 11087 goto bufwait; 11088 return (1); 11089 } 11090 inodedep->id_state |= COMPLETE; 11091 /* 11092 * Roll forward anything that had to be rolled back before 11093 * the inode could be updated. 11094 */ 11095 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11096 nextadp = TAILQ_NEXT(adp, ad_next); 11097 if (adp->ad_state & ATTACHED) 11098 panic("handle_written_inodeblock: new entry"); 11099 if (fstype == UFS1) { 11100 if (adp->ad_offset < NDADDR) { 11101 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11102 panic("%s %s #%jd mismatch %d != %jd", 11103 "handle_written_inodeblock:", 11104 "direct pointer", 11105 (intmax_t)adp->ad_offset, 11106 dp1->di_db[adp->ad_offset], 11107 (intmax_t)adp->ad_oldblkno); 11108 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11109 } else { 11110 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0) 11111 panic("%s: %s #%jd allocated as %d", 11112 "handle_written_inodeblock", 11113 "indirect pointer", 11114 (intmax_t)adp->ad_offset - NDADDR, 11115 dp1->di_ib[adp->ad_offset - NDADDR]); 11116 dp1->di_ib[adp->ad_offset - NDADDR] = 11117 adp->ad_newblkno; 11118 } 11119 } else { 11120 if (adp->ad_offset < NDADDR) { 11121 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11122 panic("%s: %s #%jd %s %jd != %jd", 11123 "handle_written_inodeblock", 11124 "direct pointer", 11125 (intmax_t)adp->ad_offset, "mismatch", 11126 (intmax_t)dp2->di_db[adp->ad_offset], 11127 (intmax_t)adp->ad_oldblkno); 11128 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11129 } else { 11130 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0) 11131 panic("%s: %s #%jd allocated as %jd", 11132 "handle_written_inodeblock", 11133 "indirect pointer", 11134 (intmax_t)adp->ad_offset - NDADDR, 11135 (intmax_t) 11136 dp2->di_ib[adp->ad_offset - NDADDR]); 11137 dp2->di_ib[adp->ad_offset - NDADDR] = 11138 adp->ad_newblkno; 11139 } 11140 } 11141 adp->ad_state &= ~UNDONE; 11142 adp->ad_state |= ATTACHED; 11143 hadchanges = 1; 11144 } 11145 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11146 nextadp = TAILQ_NEXT(adp, ad_next); 11147 if (adp->ad_state & ATTACHED) 11148 panic("handle_written_inodeblock: new entry"); 11149 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11150 panic("%s: direct pointers #%jd %s %jd != %jd", 11151 "handle_written_inodeblock", 11152 (intmax_t)adp->ad_offset, "mismatch", 11153 (intmax_t)dp2->di_extb[adp->ad_offset], 11154 (intmax_t)adp->ad_oldblkno); 11155 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11156 adp->ad_state &= ~UNDONE; 11157 adp->ad_state |= ATTACHED; 11158 hadchanges = 1; 11159 } 11160 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11161 stat_direct_blk_ptrs++; 11162 /* 11163 * Reset the file size to its most up-to-date value. 11164 */ 11165 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11166 panic("handle_written_inodeblock: bad size"); 11167 if (inodedep->id_savednlink > LINK_MAX) 11168 panic("handle_written_inodeblock: Invalid link count " 11169 "%d for inodedep %p", inodedep->id_savednlink, inodedep); 11170 if (fstype == UFS1) { 11171 if (dp1->di_nlink != inodedep->id_savednlink) { 11172 dp1->di_nlink = inodedep->id_savednlink; 11173 hadchanges = 1; 11174 } 11175 if (dp1->di_size != inodedep->id_savedsize) { 11176 dp1->di_size = inodedep->id_savedsize; 11177 hadchanges = 1; 11178 } 11179 } else { 11180 if (dp2->di_nlink != inodedep->id_savednlink) { 11181 dp2->di_nlink = inodedep->id_savednlink; 11182 hadchanges = 1; 11183 } 11184 if (dp2->di_size != inodedep->id_savedsize) { 11185 dp2->di_size = inodedep->id_savedsize; 11186 hadchanges = 1; 11187 } 11188 if (dp2->di_extsize != inodedep->id_savedextsize) { 11189 dp2->di_extsize = inodedep->id_savedextsize; 11190 hadchanges = 1; 11191 } 11192 } 11193 inodedep->id_savedsize = -1; 11194 inodedep->id_savedextsize = -1; 11195 inodedep->id_savednlink = -1; 11196 /* 11197 * If there were any rollbacks in the inode block, then it must be 11198 * marked dirty so that its will eventually get written back in 11199 * its correct form. 11200 */ 11201 if (hadchanges) 11202 bdirty(bp); 11203 bufwait: 11204 /* 11205 * Process any allocdirects that completed during the update. 11206 */ 11207 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11208 handle_allocdirect_partdone(adp, &wkhd); 11209 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11210 handle_allocdirect_partdone(adp, &wkhd); 11211 /* 11212 * Process deallocations that were held pending until the 11213 * inode had been written to disk. Freeing of the inode 11214 * is delayed until after all blocks have been freed to 11215 * avoid creation of new <vfsid, inum, lbn> triples 11216 * before the old ones have been deleted. Completely 11217 * unlinked inodes are not processed until the unlinked 11218 * inode list is written or the last reference is removed. 11219 */ 11220 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11221 freefile = handle_bufwait(inodedep, NULL); 11222 if (freefile && !LIST_EMPTY(&wkhd)) { 11223 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11224 freefile = NULL; 11225 } 11226 } 11227 /* 11228 * Move rolled forward dependency completions to the bufwait list 11229 * now that those that were already written have been processed. 11230 */ 11231 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11232 panic("handle_written_inodeblock: bufwait but no changes"); 11233 jwork_move(&inodedep->id_bufwait, &wkhd); 11234 11235 if (freefile != NULL) { 11236 /* 11237 * If the inode is goingaway it was never written. Fake up 11238 * the state here so free_inodedep() can succeed. 11239 */ 11240 if (inodedep->id_state & GOINGAWAY) 11241 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11242 if (free_inodedep(inodedep) == 0) 11243 panic("handle_written_inodeblock: live inodedep %p", 11244 inodedep); 11245 add_to_worklist(&freefile->fx_list, 0); 11246 return (0); 11247 } 11248 11249 /* 11250 * If no outstanding dependencies, free it. 11251 */ 11252 if (free_inodedep(inodedep) || 11253 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11254 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11255 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11256 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11257 return (0); 11258 return (hadchanges); 11259 } 11260 11261 static int 11262 handle_written_indirdep(indirdep, bp, bpp) 11263 struct indirdep *indirdep; 11264 struct buf *bp; 11265 struct buf **bpp; 11266 { 11267 struct allocindir *aip; 11268 struct buf *sbp; 11269 int chgs; 11270 11271 if (indirdep->ir_state & GOINGAWAY) 11272 panic("handle_written_indirdep: indirdep gone"); 11273 if ((indirdep->ir_state & IOSTARTED) == 0) 11274 panic("handle_written_indirdep: IO not started"); 11275 chgs = 0; 11276 /* 11277 * If there were rollbacks revert them here. 11278 */ 11279 if (indirdep->ir_saveddata) { 11280 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11281 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11282 free(indirdep->ir_saveddata, M_INDIRDEP); 11283 indirdep->ir_saveddata = NULL; 11284 } 11285 chgs = 1; 11286 } 11287 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11288 indirdep->ir_state |= ATTACHED; 11289 /* 11290 * Move allocindirs with written pointers to the completehd if 11291 * the indirdep's pointer is not yet written. Otherwise 11292 * free them here. 11293 */ 11294 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) { 11295 LIST_REMOVE(aip, ai_next); 11296 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11297 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11298 ai_next); 11299 newblk_freefrag(&aip->ai_block); 11300 continue; 11301 } 11302 free_newblk(&aip->ai_block); 11303 } 11304 /* 11305 * Move allocindirs that have finished dependency processing from 11306 * the done list to the write list after updating the pointers. 11307 */ 11308 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11309 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 11310 handle_allocindir_partdone(aip); 11311 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11312 panic("disk_write_complete: not gone"); 11313 chgs = 1; 11314 } 11315 } 11316 /* 11317 * Preserve the indirdep if there were any changes or if it is not 11318 * yet valid on disk. 11319 */ 11320 if (chgs) { 11321 stat_indir_blk_ptrs++; 11322 bdirty(bp); 11323 return (1); 11324 } 11325 /* 11326 * If there were no changes we can discard the savedbp and detach 11327 * ourselves from the buf. We are only carrying completed pointers 11328 * in this case. 11329 */ 11330 sbp = indirdep->ir_savebp; 11331 sbp->b_flags |= B_INVAL | B_NOCACHE; 11332 indirdep->ir_savebp = NULL; 11333 indirdep->ir_bp = NULL; 11334 if (*bpp != NULL) 11335 panic("handle_written_indirdep: bp already exists."); 11336 *bpp = sbp; 11337 /* 11338 * The indirdep may not be freed until its parent points at it. 11339 */ 11340 if (indirdep->ir_state & DEPCOMPLETE) 11341 free_indirdep(indirdep); 11342 11343 return (0); 11344 } 11345 11346 /* 11347 * Process a diradd entry after its dependent inode has been written. 11348 * This routine must be called with splbio interrupts blocked. 11349 */ 11350 static void 11351 diradd_inode_written(dap, inodedep) 11352 struct diradd *dap; 11353 struct inodedep *inodedep; 11354 { 11355 11356 dap->da_state |= COMPLETE; 11357 complete_diradd(dap); 11358 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11359 } 11360 11361 /* 11362 * Returns true if the bmsafemap will have rollbacks when written. Must 11363 * only be called with lk and the buf lock on the cg held. 11364 */ 11365 static int 11366 bmsafemap_backgroundwrite(bmsafemap, bp) 11367 struct bmsafemap *bmsafemap; 11368 struct buf *bp; 11369 { 11370 int dirty; 11371 11372 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11373 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11374 /* 11375 * If we're initiating a background write we need to process the 11376 * rollbacks as they exist now, not as they exist when IO starts. 11377 * No other consumers will look at the contents of the shadowed 11378 * buf so this is safe to do here. 11379 */ 11380 if (bp->b_xflags & BX_BKGRDMARKER) 11381 initiate_write_bmsafemap(bmsafemap, bp); 11382 11383 return (dirty); 11384 } 11385 11386 /* 11387 * Re-apply an allocation when a cg write is complete. 11388 */ 11389 static int 11390 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11391 struct jnewblk *jnewblk; 11392 struct fs *fs; 11393 struct cg *cgp; 11394 uint8_t *blksfree; 11395 { 11396 ufs1_daddr_t fragno; 11397 ufs2_daddr_t blkno; 11398 long cgbno, bbase; 11399 int frags, blk; 11400 int i; 11401 11402 frags = 0; 11403 cgbno = dtogd(fs, jnewblk->jn_blkno); 11404 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11405 if (isclr(blksfree, cgbno + i)) 11406 panic("jnewblk_rollforward: re-allocated fragment"); 11407 frags++; 11408 } 11409 if (frags == fs->fs_frag) { 11410 blkno = fragstoblks(fs, cgbno); 11411 ffs_clrblock(fs, blksfree, (long)blkno); 11412 ffs_clusteracct(fs, cgp, blkno, -1); 11413 cgp->cg_cs.cs_nbfree--; 11414 } else { 11415 bbase = cgbno - fragnum(fs, cgbno); 11416 cgbno += jnewblk->jn_oldfrags; 11417 /* If a complete block had been reassembled, account for it. */ 11418 fragno = fragstoblks(fs, bbase); 11419 if (ffs_isblock(fs, blksfree, fragno)) { 11420 cgp->cg_cs.cs_nffree += fs->fs_frag; 11421 ffs_clusteracct(fs, cgp, fragno, -1); 11422 cgp->cg_cs.cs_nbfree--; 11423 } 11424 /* Decrement the old frags. */ 11425 blk = blkmap(fs, blksfree, bbase); 11426 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11427 /* Allocate the fragment */ 11428 for (i = 0; i < frags; i++) 11429 clrbit(blksfree, cgbno + i); 11430 cgp->cg_cs.cs_nffree -= frags; 11431 /* Add back in counts associated with the new frags */ 11432 blk = blkmap(fs, blksfree, bbase); 11433 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11434 } 11435 return (frags); 11436 } 11437 11438 /* 11439 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11440 * changes if it's not a background write. Set all written dependencies 11441 * to DEPCOMPLETE and free the structure if possible. 11442 */ 11443 static int 11444 handle_written_bmsafemap(bmsafemap, bp) 11445 struct bmsafemap *bmsafemap; 11446 struct buf *bp; 11447 { 11448 struct newblk *newblk; 11449 struct inodedep *inodedep; 11450 struct jaddref *jaddref, *jatmp; 11451 struct jnewblk *jnewblk, *jntmp; 11452 struct ufsmount *ump; 11453 uint8_t *inosused; 11454 uint8_t *blksfree; 11455 struct cg *cgp; 11456 struct fs *fs; 11457 ino_t ino; 11458 int foreground; 11459 int chgs; 11460 11461 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11462 panic("initiate_write_bmsafemap: Not started\n"); 11463 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11464 chgs = 0; 11465 bmsafemap->sm_state &= ~IOSTARTED; 11466 foreground = (bp->b_xflags & BX_BKGRDMARKER) == 0; 11467 /* 11468 * Release journal work that was waiting on the write. 11469 */ 11470 handle_jwork(&bmsafemap->sm_freewr); 11471 11472 /* 11473 * Restore unwritten inode allocation pending jaddref writes. 11474 */ 11475 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11476 cgp = (struct cg *)bp->b_data; 11477 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11478 inosused = cg_inosused(cgp); 11479 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11480 ja_bmdeps, jatmp) { 11481 if ((jaddref->ja_state & UNDONE) == 0) 11482 continue; 11483 ino = jaddref->ja_ino % fs->fs_ipg; 11484 if (isset(inosused, ino)) 11485 panic("handle_written_bmsafemap: " 11486 "re-allocated inode"); 11487 /* Do the roll-forward only if it's a real copy. */ 11488 if (foreground) { 11489 if ((jaddref->ja_mode & IFMT) == IFDIR) 11490 cgp->cg_cs.cs_ndir++; 11491 cgp->cg_cs.cs_nifree--; 11492 setbit(inosused, ino); 11493 chgs = 1; 11494 } 11495 jaddref->ja_state &= ~UNDONE; 11496 jaddref->ja_state |= ATTACHED; 11497 free_jaddref(jaddref); 11498 } 11499 } 11500 /* 11501 * Restore any block allocations which are pending journal writes. 11502 */ 11503 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11504 cgp = (struct cg *)bp->b_data; 11505 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11506 blksfree = cg_blksfree(cgp); 11507 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11508 jntmp) { 11509 if ((jnewblk->jn_state & UNDONE) == 0) 11510 continue; 11511 /* Do the roll-forward only if it's a real copy. */ 11512 if (foreground && 11513 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11514 chgs = 1; 11515 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11516 jnewblk->jn_state |= ATTACHED; 11517 free_jnewblk(jnewblk); 11518 } 11519 } 11520 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11521 newblk->nb_state |= DEPCOMPLETE; 11522 newblk->nb_state &= ~ONDEPLIST; 11523 newblk->nb_bmsafemap = NULL; 11524 LIST_REMOVE(newblk, nb_deps); 11525 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11526 handle_allocdirect_partdone( 11527 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 11528 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 11529 handle_allocindir_partdone( 11530 WK_ALLOCINDIR(&newblk->nb_list)); 11531 else if (newblk->nb_list.wk_type != D_NEWBLK) 11532 panic("handle_written_bmsafemap: Unexpected type: %s", 11533 TYPENAME(newblk->nb_list.wk_type)); 11534 } 11535 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 11536 inodedep->id_state |= DEPCOMPLETE; 11537 inodedep->id_state &= ~ONDEPLIST; 11538 LIST_REMOVE(inodedep, id_deps); 11539 inodedep->id_bmsafemap = NULL; 11540 } 11541 LIST_REMOVE(bmsafemap, sm_next); 11542 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 11543 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 11544 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 11545 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 11546 LIST_EMPTY(&bmsafemap->sm_freehd)) { 11547 LIST_REMOVE(bmsafemap, sm_hash); 11548 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 11549 return (0); 11550 } 11551 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 11552 if (foreground) 11553 bdirty(bp); 11554 return (1); 11555 } 11556 11557 /* 11558 * Try to free a mkdir dependency. 11559 */ 11560 static void 11561 complete_mkdir(mkdir) 11562 struct mkdir *mkdir; 11563 { 11564 struct diradd *dap; 11565 11566 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 11567 return; 11568 LIST_REMOVE(mkdir, md_mkdirs); 11569 dap = mkdir->md_diradd; 11570 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 11571 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 11572 dap->da_state |= DEPCOMPLETE; 11573 complete_diradd(dap); 11574 } 11575 WORKITEM_FREE(mkdir, D_MKDIR); 11576 } 11577 11578 /* 11579 * Handle the completion of a mkdir dependency. 11580 */ 11581 static void 11582 handle_written_mkdir(mkdir, type) 11583 struct mkdir *mkdir; 11584 int type; 11585 { 11586 11587 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 11588 panic("handle_written_mkdir: bad type"); 11589 mkdir->md_state |= COMPLETE; 11590 complete_mkdir(mkdir); 11591 } 11592 11593 static int 11594 free_pagedep(pagedep) 11595 struct pagedep *pagedep; 11596 { 11597 int i; 11598 11599 if (pagedep->pd_state & NEWBLOCK) 11600 return (0); 11601 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 11602 return (0); 11603 for (i = 0; i < DAHASHSZ; i++) 11604 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 11605 return (0); 11606 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 11607 return (0); 11608 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 11609 return (0); 11610 if (pagedep->pd_state & ONWORKLIST) 11611 WORKLIST_REMOVE(&pagedep->pd_list); 11612 LIST_REMOVE(pagedep, pd_hash); 11613 WORKITEM_FREE(pagedep, D_PAGEDEP); 11614 11615 return (1); 11616 } 11617 11618 /* 11619 * Called from within softdep_disk_write_complete above. 11620 * A write operation was just completed. Removed inodes can 11621 * now be freed and associated block pointers may be committed. 11622 * Note that this routine is always called from interrupt level 11623 * with further splbio interrupts blocked. 11624 */ 11625 static int 11626 handle_written_filepage(pagedep, bp) 11627 struct pagedep *pagedep; 11628 struct buf *bp; /* buffer containing the written page */ 11629 { 11630 struct dirrem *dirrem; 11631 struct diradd *dap, *nextdap; 11632 struct direct *ep; 11633 int i, chgs; 11634 11635 if ((pagedep->pd_state & IOSTARTED) == 0) 11636 panic("handle_written_filepage: not started"); 11637 pagedep->pd_state &= ~IOSTARTED; 11638 /* 11639 * Process any directory removals that have been committed. 11640 */ 11641 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 11642 LIST_REMOVE(dirrem, dm_next); 11643 dirrem->dm_state |= COMPLETE; 11644 dirrem->dm_dirinum = pagedep->pd_ino; 11645 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 11646 ("handle_written_filepage: Journal entries not written.")); 11647 add_to_worklist(&dirrem->dm_list, 0); 11648 } 11649 /* 11650 * Free any directory additions that have been committed. 11651 * If it is a newly allocated block, we have to wait until 11652 * the on-disk directory inode claims the new block. 11653 */ 11654 if ((pagedep->pd_state & NEWBLOCK) == 0) 11655 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 11656 free_diradd(dap, NULL); 11657 /* 11658 * Uncommitted directory entries must be restored. 11659 */ 11660 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 11661 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 11662 dap = nextdap) { 11663 nextdap = LIST_NEXT(dap, da_pdlist); 11664 if (dap->da_state & ATTACHED) 11665 panic("handle_written_filepage: attached"); 11666 ep = (struct direct *) 11667 ((char *)bp->b_data + dap->da_offset); 11668 ep->d_ino = dap->da_newinum; 11669 dap->da_state &= ~UNDONE; 11670 dap->da_state |= ATTACHED; 11671 chgs = 1; 11672 /* 11673 * If the inode referenced by the directory has 11674 * been written out, then the dependency can be 11675 * moved to the pending list. 11676 */ 11677 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 11678 LIST_REMOVE(dap, da_pdlist); 11679 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 11680 da_pdlist); 11681 } 11682 } 11683 } 11684 /* 11685 * If there were any rollbacks in the directory, then it must be 11686 * marked dirty so that its will eventually get written back in 11687 * its correct form. 11688 */ 11689 if (chgs) { 11690 if ((bp->b_flags & B_DELWRI) == 0) 11691 stat_dir_entry++; 11692 bdirty(bp); 11693 return (1); 11694 } 11695 /* 11696 * If we are not waiting for a new directory block to be 11697 * claimed by its inode, then the pagedep will be freed. 11698 * Otherwise it will remain to track any new entries on 11699 * the page in case they are fsync'ed. 11700 */ 11701 free_pagedep(pagedep); 11702 return (0); 11703 } 11704 11705 /* 11706 * Writing back in-core inode structures. 11707 * 11708 * The filesystem only accesses an inode's contents when it occupies an 11709 * "in-core" inode structure. These "in-core" structures are separate from 11710 * the page frames used to cache inode blocks. Only the latter are 11711 * transferred to/from the disk. So, when the updated contents of the 11712 * "in-core" inode structure are copied to the corresponding in-memory inode 11713 * block, the dependencies are also transferred. The following procedure is 11714 * called when copying a dirty "in-core" inode to a cached inode block. 11715 */ 11716 11717 /* 11718 * Called when an inode is loaded from disk. If the effective link count 11719 * differed from the actual link count when it was last flushed, then we 11720 * need to ensure that the correct effective link count is put back. 11721 */ 11722 void 11723 softdep_load_inodeblock(ip) 11724 struct inode *ip; /* the "in_core" copy of the inode */ 11725 { 11726 struct inodedep *inodedep; 11727 11728 /* 11729 * Check for alternate nlink count. 11730 */ 11731 ip->i_effnlink = ip->i_nlink; 11732 ACQUIRE_LOCK(&lk); 11733 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 11734 &inodedep) == 0) { 11735 FREE_LOCK(&lk); 11736 return; 11737 } 11738 ip->i_effnlink -= inodedep->id_nlinkdelta; 11739 FREE_LOCK(&lk); 11740 } 11741 11742 /* 11743 * This routine is called just before the "in-core" inode 11744 * information is to be copied to the in-memory inode block. 11745 * Recall that an inode block contains several inodes. If 11746 * the force flag is set, then the dependencies will be 11747 * cleared so that the update can always be made. Note that 11748 * the buffer is locked when this routine is called, so we 11749 * will never be in the middle of writing the inode block 11750 * to disk. 11751 */ 11752 void 11753 softdep_update_inodeblock(ip, bp, waitfor) 11754 struct inode *ip; /* the "in_core" copy of the inode */ 11755 struct buf *bp; /* the buffer containing the inode block */ 11756 int waitfor; /* nonzero => update must be allowed */ 11757 { 11758 struct inodedep *inodedep; 11759 struct inoref *inoref; 11760 struct worklist *wk; 11761 struct mount *mp; 11762 struct buf *ibp; 11763 struct fs *fs; 11764 int error; 11765 11766 mp = UFSTOVFS(ip->i_ump); 11767 fs = ip->i_fs; 11768 /* 11769 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 11770 * does not have access to the in-core ip so must write directly into 11771 * the inode block buffer when setting freelink. 11772 */ 11773 if (fs->fs_magic == FS_UFS1_MAGIC) 11774 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 11775 ino_to_fsbo(fs, ip->i_number))->di_freelink); 11776 else 11777 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 11778 ino_to_fsbo(fs, ip->i_number))->di_freelink); 11779 /* 11780 * If the effective link count is not equal to the actual link 11781 * count, then we must track the difference in an inodedep while 11782 * the inode is (potentially) tossed out of the cache. Otherwise, 11783 * if there is no existing inodedep, then there are no dependencies 11784 * to track. 11785 */ 11786 ACQUIRE_LOCK(&lk); 11787 again: 11788 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 11789 FREE_LOCK(&lk); 11790 if (ip->i_effnlink != ip->i_nlink) 11791 panic("softdep_update_inodeblock: bad link count"); 11792 return; 11793 } 11794 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 11795 panic("softdep_update_inodeblock: bad delta"); 11796 /* 11797 * If we're flushing all dependencies we must also move any waiting 11798 * for journal writes onto the bufwait list prior to I/O. 11799 */ 11800 if (waitfor) { 11801 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 11802 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 11803 == DEPCOMPLETE) { 11804 jwait(&inoref->if_list, MNT_WAIT); 11805 goto again; 11806 } 11807 } 11808 } 11809 /* 11810 * Changes have been initiated. Anything depending on these 11811 * changes cannot occur until this inode has been written. 11812 */ 11813 inodedep->id_state &= ~COMPLETE; 11814 if ((inodedep->id_state & ONWORKLIST) == 0) 11815 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 11816 /* 11817 * Any new dependencies associated with the incore inode must 11818 * now be moved to the list associated with the buffer holding 11819 * the in-memory copy of the inode. Once merged process any 11820 * allocdirects that are completed by the merger. 11821 */ 11822 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 11823 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 11824 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 11825 NULL); 11826 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 11827 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 11828 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 11829 NULL); 11830 /* 11831 * Now that the inode has been pushed into the buffer, the 11832 * operations dependent on the inode being written to disk 11833 * can be moved to the id_bufwait so that they will be 11834 * processed when the buffer I/O completes. 11835 */ 11836 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 11837 WORKLIST_REMOVE(wk); 11838 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 11839 } 11840 /* 11841 * Newly allocated inodes cannot be written until the bitmap 11842 * that allocates them have been written (indicated by 11843 * DEPCOMPLETE being set in id_state). If we are doing a 11844 * forced sync (e.g., an fsync on a file), we force the bitmap 11845 * to be written so that the update can be done. 11846 */ 11847 if (waitfor == 0) { 11848 FREE_LOCK(&lk); 11849 return; 11850 } 11851 retry: 11852 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 11853 FREE_LOCK(&lk); 11854 return; 11855 } 11856 ibp = inodedep->id_bmsafemap->sm_buf; 11857 ibp = getdirtybuf(ibp, &lk, MNT_WAIT); 11858 if (ibp == NULL) { 11859 /* 11860 * If ibp came back as NULL, the dependency could have been 11861 * freed while we slept. Look it up again, and check to see 11862 * that it has completed. 11863 */ 11864 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 11865 goto retry; 11866 FREE_LOCK(&lk); 11867 return; 11868 } 11869 FREE_LOCK(&lk); 11870 if ((error = bwrite(ibp)) != 0) 11871 softdep_error("softdep_update_inodeblock: bwrite", error); 11872 } 11873 11874 /* 11875 * Merge the a new inode dependency list (such as id_newinoupdt) into an 11876 * old inode dependency list (such as id_inoupdt). This routine must be 11877 * called with splbio interrupts blocked. 11878 */ 11879 static void 11880 merge_inode_lists(newlisthead, oldlisthead) 11881 struct allocdirectlst *newlisthead; 11882 struct allocdirectlst *oldlisthead; 11883 { 11884 struct allocdirect *listadp, *newadp; 11885 11886 newadp = TAILQ_FIRST(newlisthead); 11887 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 11888 if (listadp->ad_offset < newadp->ad_offset) { 11889 listadp = TAILQ_NEXT(listadp, ad_next); 11890 continue; 11891 } 11892 TAILQ_REMOVE(newlisthead, newadp, ad_next); 11893 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 11894 if (listadp->ad_offset == newadp->ad_offset) { 11895 allocdirect_merge(oldlisthead, newadp, 11896 listadp); 11897 listadp = newadp; 11898 } 11899 newadp = TAILQ_FIRST(newlisthead); 11900 } 11901 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 11902 TAILQ_REMOVE(newlisthead, newadp, ad_next); 11903 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 11904 } 11905 } 11906 11907 /* 11908 * If we are doing an fsync, then we must ensure that any directory 11909 * entries for the inode have been written after the inode gets to disk. 11910 */ 11911 int 11912 softdep_fsync(vp) 11913 struct vnode *vp; /* the "in_core" copy of the inode */ 11914 { 11915 struct inodedep *inodedep; 11916 struct pagedep *pagedep; 11917 struct inoref *inoref; 11918 struct worklist *wk; 11919 struct diradd *dap; 11920 struct mount *mp; 11921 struct vnode *pvp; 11922 struct inode *ip; 11923 struct buf *bp; 11924 struct fs *fs; 11925 struct thread *td = curthread; 11926 int error, flushparent, pagedep_new_block; 11927 ino_t parentino; 11928 ufs_lbn_t lbn; 11929 11930 ip = VTOI(vp); 11931 fs = ip->i_fs; 11932 mp = vp->v_mount; 11933 ACQUIRE_LOCK(&lk); 11934 restart: 11935 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 11936 FREE_LOCK(&lk); 11937 return (0); 11938 } 11939 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 11940 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 11941 == DEPCOMPLETE) { 11942 jwait(&inoref->if_list, MNT_WAIT); 11943 goto restart; 11944 } 11945 } 11946 if (!LIST_EMPTY(&inodedep->id_inowait) || 11947 !TAILQ_EMPTY(&inodedep->id_extupdt) || 11948 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 11949 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 11950 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 11951 panic("softdep_fsync: pending ops %p", inodedep); 11952 for (error = 0, flushparent = 0; ; ) { 11953 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 11954 break; 11955 if (wk->wk_type != D_DIRADD) 11956 panic("softdep_fsync: Unexpected type %s", 11957 TYPENAME(wk->wk_type)); 11958 dap = WK_DIRADD(wk); 11959 /* 11960 * Flush our parent if this directory entry has a MKDIR_PARENT 11961 * dependency or is contained in a newly allocated block. 11962 */ 11963 if (dap->da_state & DIRCHG) 11964 pagedep = dap->da_previous->dm_pagedep; 11965 else 11966 pagedep = dap->da_pagedep; 11967 parentino = pagedep->pd_ino; 11968 lbn = pagedep->pd_lbn; 11969 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 11970 panic("softdep_fsync: dirty"); 11971 if ((dap->da_state & MKDIR_PARENT) || 11972 (pagedep->pd_state & NEWBLOCK)) 11973 flushparent = 1; 11974 else 11975 flushparent = 0; 11976 /* 11977 * If we are being fsync'ed as part of vgone'ing this vnode, 11978 * then we will not be able to release and recover the 11979 * vnode below, so we just have to give up on writing its 11980 * directory entry out. It will eventually be written, just 11981 * not now, but then the user was not asking to have it 11982 * written, so we are not breaking any promises. 11983 */ 11984 if (vp->v_iflag & VI_DOOMED) 11985 break; 11986 /* 11987 * We prevent deadlock by always fetching inodes from the 11988 * root, moving down the directory tree. Thus, when fetching 11989 * our parent directory, we first try to get the lock. If 11990 * that fails, we must unlock ourselves before requesting 11991 * the lock on our parent. See the comment in ufs_lookup 11992 * for details on possible races. 11993 */ 11994 FREE_LOCK(&lk); 11995 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 11996 FFSV_FORCEINSMQ)) { 11997 error = vfs_busy(mp, MBF_NOWAIT); 11998 if (error != 0) { 11999 vfs_ref(mp); 12000 VOP_UNLOCK(vp, 0); 12001 error = vfs_busy(mp, 0); 12002 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12003 vfs_rel(mp); 12004 if (error != 0) 12005 return (ENOENT); 12006 if (vp->v_iflag & VI_DOOMED) { 12007 vfs_unbusy(mp); 12008 return (ENOENT); 12009 } 12010 } 12011 VOP_UNLOCK(vp, 0); 12012 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12013 &pvp, FFSV_FORCEINSMQ); 12014 vfs_unbusy(mp); 12015 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12016 if (vp->v_iflag & VI_DOOMED) { 12017 if (error == 0) 12018 vput(pvp); 12019 error = ENOENT; 12020 } 12021 if (error != 0) 12022 return (error); 12023 } 12024 /* 12025 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12026 * that are contained in direct blocks will be resolved by 12027 * doing a ffs_update. Pagedeps contained in indirect blocks 12028 * may require a complete sync'ing of the directory. So, we 12029 * try the cheap and fast ffs_update first, and if that fails, 12030 * then we do the slower ffs_syncvnode of the directory. 12031 */ 12032 if (flushparent) { 12033 int locked; 12034 12035 if ((error = ffs_update(pvp, 1)) != 0) { 12036 vput(pvp); 12037 return (error); 12038 } 12039 ACQUIRE_LOCK(&lk); 12040 locked = 1; 12041 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12042 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12043 if (wk->wk_type != D_DIRADD) 12044 panic("softdep_fsync: Unexpected type %s", 12045 TYPENAME(wk->wk_type)); 12046 dap = WK_DIRADD(wk); 12047 if (dap->da_state & DIRCHG) 12048 pagedep = dap->da_previous->dm_pagedep; 12049 else 12050 pagedep = dap->da_pagedep; 12051 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12052 FREE_LOCK(&lk); 12053 locked = 0; 12054 if (pagedep_new_block && (error = 12055 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12056 vput(pvp); 12057 return (error); 12058 } 12059 } 12060 } 12061 if (locked) 12062 FREE_LOCK(&lk); 12063 } 12064 /* 12065 * Flush directory page containing the inode's name. 12066 */ 12067 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12068 &bp); 12069 if (error == 0) 12070 error = bwrite(bp); 12071 else 12072 brelse(bp); 12073 vput(pvp); 12074 if (error != 0) 12075 return (error); 12076 ACQUIRE_LOCK(&lk); 12077 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12078 break; 12079 } 12080 FREE_LOCK(&lk); 12081 return (0); 12082 } 12083 12084 /* 12085 * Flush all the dirty bitmaps associated with the block device 12086 * before flushing the rest of the dirty blocks so as to reduce 12087 * the number of dependencies that will have to be rolled back. 12088 * 12089 * XXX Unused? 12090 */ 12091 void 12092 softdep_fsync_mountdev(vp) 12093 struct vnode *vp; 12094 { 12095 struct buf *bp, *nbp; 12096 struct worklist *wk; 12097 struct bufobj *bo; 12098 12099 if (!vn_isdisk(vp, NULL)) 12100 panic("softdep_fsync_mountdev: vnode not a disk"); 12101 bo = &vp->v_bufobj; 12102 restart: 12103 BO_LOCK(bo); 12104 ACQUIRE_LOCK(&lk); 12105 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12106 /* 12107 * If it is already scheduled, skip to the next buffer. 12108 */ 12109 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12110 continue; 12111 12112 if ((bp->b_flags & B_DELWRI) == 0) 12113 panic("softdep_fsync_mountdev: not dirty"); 12114 /* 12115 * We are only interested in bitmaps with outstanding 12116 * dependencies. 12117 */ 12118 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12119 wk->wk_type != D_BMSAFEMAP || 12120 (bp->b_vflags & BV_BKGRDINPROG)) { 12121 BUF_UNLOCK(bp); 12122 continue; 12123 } 12124 FREE_LOCK(&lk); 12125 BO_UNLOCK(bo); 12126 bremfree(bp); 12127 (void) bawrite(bp); 12128 goto restart; 12129 } 12130 FREE_LOCK(&lk); 12131 drain_output(vp); 12132 BO_UNLOCK(bo); 12133 } 12134 12135 /* 12136 * Sync all cylinder groups that were dirty at the time this function is 12137 * called. Newly dirtied cgs will be inserted before the sentinel. This 12138 * is used to flush freedep activity that may be holding up writes to a 12139 * indirect block. 12140 */ 12141 static int 12142 sync_cgs(mp, waitfor) 12143 struct mount *mp; 12144 int waitfor; 12145 { 12146 struct bmsafemap *bmsafemap; 12147 struct bmsafemap *sentinel; 12148 struct ufsmount *ump; 12149 struct buf *bp; 12150 int error; 12151 12152 sentinel = malloc(sizeof(*sentinel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12153 sentinel->sm_cg = -1; 12154 ump = VFSTOUFS(mp); 12155 error = 0; 12156 ACQUIRE_LOCK(&lk); 12157 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sentinel, sm_next); 12158 for (bmsafemap = LIST_NEXT(sentinel, sm_next); bmsafemap != NULL; 12159 bmsafemap = LIST_NEXT(sentinel, sm_next)) { 12160 /* Skip sentinels and cgs with no work to release. */ 12161 if (bmsafemap->sm_cg == -1 || 12162 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12163 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12164 LIST_REMOVE(sentinel, sm_next); 12165 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12166 continue; 12167 } 12168 /* 12169 * If we don't get the lock and we're waiting try again, if 12170 * not move on to the next buf and try to sync it. 12171 */ 12172 bp = getdirtybuf(bmsafemap->sm_buf, &lk, waitfor); 12173 if (bp == NULL && waitfor == MNT_WAIT) 12174 continue; 12175 LIST_REMOVE(sentinel, sm_next); 12176 LIST_INSERT_AFTER(bmsafemap, sentinel, sm_next); 12177 if (bp == NULL) 12178 continue; 12179 FREE_LOCK(&lk); 12180 if (waitfor == MNT_NOWAIT) 12181 bawrite(bp); 12182 else 12183 error = bwrite(bp); 12184 ACQUIRE_LOCK(&lk); 12185 if (error) 12186 break; 12187 } 12188 LIST_REMOVE(sentinel, sm_next); 12189 FREE_LOCK(&lk); 12190 free(sentinel, M_BMSAFEMAP); 12191 return (error); 12192 } 12193 12194 /* 12195 * This routine is called when we are trying to synchronously flush a 12196 * file. This routine must eliminate any filesystem metadata dependencies 12197 * so that the syncing routine can succeed. 12198 */ 12199 int 12200 softdep_sync_metadata(struct vnode *vp) 12201 { 12202 int error; 12203 12204 /* 12205 * Ensure that any direct block dependencies have been cleared, 12206 * truncations are started, and inode references are journaled. 12207 */ 12208 ACQUIRE_LOCK(&lk); 12209 /* 12210 * Write all journal records to prevent rollbacks on devvp. 12211 */ 12212 if (vp->v_type == VCHR) 12213 softdep_flushjournal(vp->v_mount); 12214 error = flush_inodedep_deps(vp, vp->v_mount, VTOI(vp)->i_number); 12215 /* 12216 * Ensure that all truncates are written so we won't find deps on 12217 * indirect blocks. 12218 */ 12219 process_truncates(vp); 12220 FREE_LOCK(&lk); 12221 12222 return (error); 12223 } 12224 12225 /* 12226 * This routine is called when we are attempting to sync a buf with 12227 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12228 * other IO it can but returns EBUSY if the buffer is not yet able to 12229 * be written. Dependencies which will not cause rollbacks will always 12230 * return 0. 12231 */ 12232 int 12233 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12234 { 12235 struct indirdep *indirdep; 12236 struct pagedep *pagedep; 12237 struct allocindir *aip; 12238 struct newblk *newblk; 12239 struct buf *nbp; 12240 struct worklist *wk; 12241 int i, error; 12242 12243 /* 12244 * For VCHR we just don't want to force flush any dependencies that 12245 * will cause rollbacks. 12246 */ 12247 if (vp->v_type == VCHR) { 12248 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12249 return (EBUSY); 12250 return (0); 12251 } 12252 ACQUIRE_LOCK(&lk); 12253 /* 12254 * As we hold the buffer locked, none of its dependencies 12255 * will disappear. 12256 */ 12257 error = 0; 12258 top: 12259 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12260 switch (wk->wk_type) { 12261 12262 case D_ALLOCDIRECT: 12263 case D_ALLOCINDIR: 12264 newblk = WK_NEWBLK(wk); 12265 if (newblk->nb_jnewblk != NULL) { 12266 if (waitfor == MNT_NOWAIT) { 12267 error = EBUSY; 12268 goto out_unlock; 12269 } 12270 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12271 goto top; 12272 } 12273 if (newblk->nb_state & DEPCOMPLETE || 12274 waitfor == MNT_NOWAIT) 12275 continue; 12276 nbp = newblk->nb_bmsafemap->sm_buf; 12277 nbp = getdirtybuf(nbp, &lk, waitfor); 12278 if (nbp == NULL) 12279 goto top; 12280 FREE_LOCK(&lk); 12281 if ((error = bwrite(nbp)) != 0) 12282 goto out; 12283 ACQUIRE_LOCK(&lk); 12284 continue; 12285 12286 case D_INDIRDEP: 12287 indirdep = WK_INDIRDEP(wk); 12288 if (waitfor == MNT_NOWAIT) { 12289 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12290 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12291 error = EBUSY; 12292 goto out_unlock; 12293 } 12294 } 12295 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12296 panic("softdep_sync_buf: truncation pending."); 12297 restart: 12298 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12299 newblk = (struct newblk *)aip; 12300 if (newblk->nb_jnewblk != NULL) { 12301 jwait(&newblk->nb_jnewblk->jn_list, 12302 waitfor); 12303 goto restart; 12304 } 12305 if (newblk->nb_state & DEPCOMPLETE) 12306 continue; 12307 nbp = newblk->nb_bmsafemap->sm_buf; 12308 nbp = getdirtybuf(nbp, &lk, waitfor); 12309 if (nbp == NULL) 12310 goto restart; 12311 FREE_LOCK(&lk); 12312 if ((error = bwrite(nbp)) != 0) 12313 goto out; 12314 ACQUIRE_LOCK(&lk); 12315 goto restart; 12316 } 12317 continue; 12318 12319 case D_PAGEDEP: 12320 /* 12321 * Only flush directory entries in synchronous passes. 12322 */ 12323 if (waitfor != MNT_WAIT) { 12324 error = EBUSY; 12325 goto out_unlock; 12326 } 12327 /* 12328 * While syncing snapshots, we must allow recursive 12329 * lookups. 12330 */ 12331 BUF_AREC(bp); 12332 /* 12333 * We are trying to sync a directory that may 12334 * have dependencies on both its own metadata 12335 * and/or dependencies on the inodes of any 12336 * recently allocated files. We walk its diradd 12337 * lists pushing out the associated inode. 12338 */ 12339 pagedep = WK_PAGEDEP(wk); 12340 for (i = 0; i < DAHASHSZ; i++) { 12341 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12342 continue; 12343 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12344 &pagedep->pd_diraddhd[i]))) { 12345 BUF_NOREC(bp); 12346 goto out_unlock; 12347 } 12348 } 12349 BUF_NOREC(bp); 12350 continue; 12351 12352 case D_FREEWORK: 12353 case D_FREEDEP: 12354 case D_JSEGDEP: 12355 case D_JNEWBLK: 12356 continue; 12357 12358 default: 12359 panic("softdep_sync_buf: Unknown type %s", 12360 TYPENAME(wk->wk_type)); 12361 /* NOTREACHED */ 12362 } 12363 } 12364 out_unlock: 12365 FREE_LOCK(&lk); 12366 out: 12367 return (error); 12368 } 12369 12370 /* 12371 * Flush the dependencies associated with an inodedep. 12372 * Called with splbio blocked. 12373 */ 12374 static int 12375 flush_inodedep_deps(vp, mp, ino) 12376 struct vnode *vp; 12377 struct mount *mp; 12378 ino_t ino; 12379 { 12380 struct inodedep *inodedep; 12381 struct inoref *inoref; 12382 int error, waitfor; 12383 12384 /* 12385 * This work is done in two passes. The first pass grabs most 12386 * of the buffers and begins asynchronously writing them. The 12387 * only way to wait for these asynchronous writes is to sleep 12388 * on the filesystem vnode which may stay busy for a long time 12389 * if the filesystem is active. So, instead, we make a second 12390 * pass over the dependencies blocking on each write. In the 12391 * usual case we will be blocking against a write that we 12392 * initiated, so when it is done the dependency will have been 12393 * resolved. Thus the second pass is expected to end quickly. 12394 * We give a brief window at the top of the loop to allow 12395 * any pending I/O to complete. 12396 */ 12397 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12398 if (error) 12399 return (error); 12400 FREE_LOCK(&lk); 12401 ACQUIRE_LOCK(&lk); 12402 restart: 12403 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12404 return (0); 12405 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12406 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12407 == DEPCOMPLETE) { 12408 jwait(&inoref->if_list, MNT_WAIT); 12409 goto restart; 12410 } 12411 } 12412 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12413 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12414 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12415 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12416 continue; 12417 /* 12418 * If pass2, we are done, otherwise do pass 2. 12419 */ 12420 if (waitfor == MNT_WAIT) 12421 break; 12422 waitfor = MNT_WAIT; 12423 } 12424 /* 12425 * Try freeing inodedep in case all dependencies have been removed. 12426 */ 12427 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12428 (void) free_inodedep(inodedep); 12429 return (0); 12430 } 12431 12432 /* 12433 * Flush an inode dependency list. 12434 * Called with splbio blocked. 12435 */ 12436 static int 12437 flush_deplist(listhead, waitfor, errorp) 12438 struct allocdirectlst *listhead; 12439 int waitfor; 12440 int *errorp; 12441 { 12442 struct allocdirect *adp; 12443 struct newblk *newblk; 12444 struct buf *bp; 12445 12446 rw_assert(&lk, RA_WLOCKED); 12447 TAILQ_FOREACH(adp, listhead, ad_next) { 12448 newblk = (struct newblk *)adp; 12449 if (newblk->nb_jnewblk != NULL) { 12450 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12451 return (1); 12452 } 12453 if (newblk->nb_state & DEPCOMPLETE) 12454 continue; 12455 bp = newblk->nb_bmsafemap->sm_buf; 12456 bp = getdirtybuf(bp, &lk, waitfor); 12457 if (bp == NULL) { 12458 if (waitfor == MNT_NOWAIT) 12459 continue; 12460 return (1); 12461 } 12462 FREE_LOCK(&lk); 12463 if (waitfor == MNT_NOWAIT) 12464 bawrite(bp); 12465 else 12466 *errorp = bwrite(bp); 12467 ACQUIRE_LOCK(&lk); 12468 return (1); 12469 } 12470 return (0); 12471 } 12472 12473 /* 12474 * Flush dependencies associated with an allocdirect block. 12475 */ 12476 static int 12477 flush_newblk_dep(vp, mp, lbn) 12478 struct vnode *vp; 12479 struct mount *mp; 12480 ufs_lbn_t lbn; 12481 { 12482 struct newblk *newblk; 12483 struct bufobj *bo; 12484 struct inode *ip; 12485 struct buf *bp; 12486 ufs2_daddr_t blkno; 12487 int error; 12488 12489 error = 0; 12490 bo = &vp->v_bufobj; 12491 ip = VTOI(vp); 12492 blkno = DIP(ip, i_db[lbn]); 12493 if (blkno == 0) 12494 panic("flush_newblk_dep: Missing block"); 12495 ACQUIRE_LOCK(&lk); 12496 /* 12497 * Loop until all dependencies related to this block are satisfied. 12498 * We must be careful to restart after each sleep in case a write 12499 * completes some part of this process for us. 12500 */ 12501 for (;;) { 12502 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 12503 FREE_LOCK(&lk); 12504 break; 12505 } 12506 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 12507 panic("flush_newblk_deps: Bad newblk %p", newblk); 12508 /* 12509 * Flush the journal. 12510 */ 12511 if (newblk->nb_jnewblk != NULL) { 12512 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12513 continue; 12514 } 12515 /* 12516 * Write the bitmap dependency. 12517 */ 12518 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 12519 bp = newblk->nb_bmsafemap->sm_buf; 12520 bp = getdirtybuf(bp, &lk, MNT_WAIT); 12521 if (bp == NULL) 12522 continue; 12523 FREE_LOCK(&lk); 12524 error = bwrite(bp); 12525 if (error) 12526 break; 12527 ACQUIRE_LOCK(&lk); 12528 continue; 12529 } 12530 /* 12531 * Write the buffer. 12532 */ 12533 FREE_LOCK(&lk); 12534 BO_LOCK(bo); 12535 bp = gbincore(bo, lbn); 12536 if (bp != NULL) { 12537 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 12538 LK_INTERLOCK, BO_LOCKPTR(bo)); 12539 if (error == ENOLCK) { 12540 ACQUIRE_LOCK(&lk); 12541 continue; /* Slept, retry */ 12542 } 12543 if (error != 0) 12544 break; /* Failed */ 12545 if (bp->b_flags & B_DELWRI) { 12546 bremfree(bp); 12547 error = bwrite(bp); 12548 if (error) 12549 break; 12550 } else 12551 BUF_UNLOCK(bp); 12552 } else 12553 BO_UNLOCK(bo); 12554 /* 12555 * We have to wait for the direct pointers to 12556 * point at the newdirblk before the dependency 12557 * will go away. 12558 */ 12559 error = ffs_update(vp, 1); 12560 if (error) 12561 break; 12562 ACQUIRE_LOCK(&lk); 12563 } 12564 return (error); 12565 } 12566 12567 /* 12568 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 12569 * Called with splbio blocked. 12570 */ 12571 static int 12572 flush_pagedep_deps(pvp, mp, diraddhdp) 12573 struct vnode *pvp; 12574 struct mount *mp; 12575 struct diraddhd *diraddhdp; 12576 { 12577 struct inodedep *inodedep; 12578 struct inoref *inoref; 12579 struct ufsmount *ump; 12580 struct diradd *dap; 12581 struct vnode *vp; 12582 int error = 0; 12583 struct buf *bp; 12584 ino_t inum; 12585 12586 ump = VFSTOUFS(mp); 12587 restart: 12588 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 12589 /* 12590 * Flush ourselves if this directory entry 12591 * has a MKDIR_PARENT dependency. 12592 */ 12593 if (dap->da_state & MKDIR_PARENT) { 12594 FREE_LOCK(&lk); 12595 if ((error = ffs_update(pvp, 1)) != 0) 12596 break; 12597 ACQUIRE_LOCK(&lk); 12598 /* 12599 * If that cleared dependencies, go on to next. 12600 */ 12601 if (dap != LIST_FIRST(diraddhdp)) 12602 continue; 12603 if (dap->da_state & MKDIR_PARENT) 12604 panic("flush_pagedep_deps: MKDIR_PARENT"); 12605 } 12606 /* 12607 * A newly allocated directory must have its "." and 12608 * ".." entries written out before its name can be 12609 * committed in its parent. 12610 */ 12611 inum = dap->da_newinum; 12612 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12613 panic("flush_pagedep_deps: lost inode1"); 12614 /* 12615 * Wait for any pending journal adds to complete so we don't 12616 * cause rollbacks while syncing. 12617 */ 12618 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12619 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12620 == DEPCOMPLETE) { 12621 jwait(&inoref->if_list, MNT_WAIT); 12622 goto restart; 12623 } 12624 } 12625 if (dap->da_state & MKDIR_BODY) { 12626 FREE_LOCK(&lk); 12627 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12628 FFSV_FORCEINSMQ))) 12629 break; 12630 error = flush_newblk_dep(vp, mp, 0); 12631 /* 12632 * If we still have the dependency we might need to 12633 * update the vnode to sync the new link count to 12634 * disk. 12635 */ 12636 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 12637 error = ffs_update(vp, 1); 12638 vput(vp); 12639 if (error != 0) 12640 break; 12641 ACQUIRE_LOCK(&lk); 12642 /* 12643 * If that cleared dependencies, go on to next. 12644 */ 12645 if (dap != LIST_FIRST(diraddhdp)) 12646 continue; 12647 if (dap->da_state & MKDIR_BODY) { 12648 inodedep_lookup(UFSTOVFS(ump), inum, 0, 12649 &inodedep); 12650 panic("flush_pagedep_deps: MKDIR_BODY " 12651 "inodedep %p dap %p vp %p", 12652 inodedep, dap, vp); 12653 } 12654 } 12655 /* 12656 * Flush the inode on which the directory entry depends. 12657 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 12658 * the only remaining dependency is that the updated inode 12659 * count must get pushed to disk. The inode has already 12660 * been pushed into its inode buffer (via VOP_UPDATE) at 12661 * the time of the reference count change. So we need only 12662 * locate that buffer, ensure that there will be no rollback 12663 * caused by a bitmap dependency, then write the inode buffer. 12664 */ 12665 retry: 12666 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12667 panic("flush_pagedep_deps: lost inode"); 12668 /* 12669 * If the inode still has bitmap dependencies, 12670 * push them to disk. 12671 */ 12672 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 12673 bp = inodedep->id_bmsafemap->sm_buf; 12674 bp = getdirtybuf(bp, &lk, MNT_WAIT); 12675 if (bp == NULL) 12676 goto retry; 12677 FREE_LOCK(&lk); 12678 if ((error = bwrite(bp)) != 0) 12679 break; 12680 ACQUIRE_LOCK(&lk); 12681 if (dap != LIST_FIRST(diraddhdp)) 12682 continue; 12683 } 12684 /* 12685 * If the inode is still sitting in a buffer waiting 12686 * to be written or waiting for the link count to be 12687 * adjusted update it here to flush it to disk. 12688 */ 12689 if (dap == LIST_FIRST(diraddhdp)) { 12690 FREE_LOCK(&lk); 12691 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12692 FFSV_FORCEINSMQ))) 12693 break; 12694 error = ffs_update(vp, 1); 12695 vput(vp); 12696 if (error) 12697 break; 12698 ACQUIRE_LOCK(&lk); 12699 } 12700 /* 12701 * If we have failed to get rid of all the dependencies 12702 * then something is seriously wrong. 12703 */ 12704 if (dap == LIST_FIRST(diraddhdp)) { 12705 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 12706 panic("flush_pagedep_deps: failed to flush " 12707 "inodedep %p ino %ju dap %p", 12708 inodedep, (uintmax_t)inum, dap); 12709 } 12710 } 12711 if (error) 12712 ACQUIRE_LOCK(&lk); 12713 return (error); 12714 } 12715 12716 /* 12717 * A large burst of file addition or deletion activity can drive the 12718 * memory load excessively high. First attempt to slow things down 12719 * using the techniques below. If that fails, this routine requests 12720 * the offending operations to fall back to running synchronously 12721 * until the memory load returns to a reasonable level. 12722 */ 12723 int 12724 softdep_slowdown(vp) 12725 struct vnode *vp; 12726 { 12727 struct ufsmount *ump; 12728 int jlow; 12729 int max_softdeps_hard; 12730 12731 ACQUIRE_LOCK(&lk); 12732 jlow = 0; 12733 /* 12734 * Check for journal space if needed. 12735 */ 12736 if (DOINGSUJ(vp)) { 12737 ump = VFSTOUFS(vp->v_mount); 12738 if (journal_space(ump, 0) == 0) 12739 jlow = 1; 12740 } 12741 max_softdeps_hard = max_softdeps * 11 / 10; 12742 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 12743 dep_current[D_INODEDEP] < max_softdeps_hard && 12744 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps && 12745 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0) { 12746 FREE_LOCK(&lk); 12747 return (0); 12748 } 12749 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps || jlow) 12750 softdep_speedup(); 12751 stat_sync_limit_hit += 1; 12752 FREE_LOCK(&lk); 12753 if (DOINGSUJ(vp)) 12754 return (0); 12755 return (1); 12756 } 12757 12758 /* 12759 * Called by the allocation routines when they are about to fail 12760 * in the hope that we can free up the requested resource (inodes 12761 * or disk space). 12762 * 12763 * First check to see if the work list has anything on it. If it has, 12764 * clean up entries until we successfully free the requested resource. 12765 * Because this process holds inodes locked, we cannot handle any remove 12766 * requests that might block on a locked inode as that could lead to 12767 * deadlock. If the worklist yields none of the requested resource, 12768 * start syncing out vnodes to free up the needed space. 12769 */ 12770 int 12771 softdep_request_cleanup(fs, vp, cred, resource) 12772 struct fs *fs; 12773 struct vnode *vp; 12774 struct ucred *cred; 12775 int resource; 12776 { 12777 struct ufsmount *ump; 12778 struct mount *mp; 12779 struct vnode *lvp, *mvp; 12780 long starttime; 12781 ufs2_daddr_t needed; 12782 int error; 12783 12784 /* 12785 * If we are being called because of a process doing a 12786 * copy-on-write, then it is not safe to process any 12787 * worklist items as we will recurse into the copyonwrite 12788 * routine. This will result in an incoherent snapshot. 12789 * If the vnode that we hold is a snapshot, we must avoid 12790 * handling other resources that could cause deadlock. 12791 */ 12792 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 12793 return (0); 12794 12795 if (resource == FLUSH_BLOCKS_WAIT) 12796 stat_cleanup_blkrequests += 1; 12797 else 12798 stat_cleanup_inorequests += 1; 12799 12800 mp = vp->v_mount; 12801 ump = VFSTOUFS(mp); 12802 mtx_assert(UFS_MTX(ump), MA_OWNED); 12803 UFS_UNLOCK(ump); 12804 error = ffs_update(vp, 1); 12805 if (error != 0) { 12806 UFS_LOCK(ump); 12807 return (0); 12808 } 12809 /* 12810 * If we are in need of resources, consider pausing for 12811 * tickdelay to give ourselves some breathing room. 12812 */ 12813 ACQUIRE_LOCK(&lk); 12814 process_removes(vp); 12815 process_truncates(vp); 12816 request_cleanup(UFSTOVFS(ump), resource); 12817 FREE_LOCK(&lk); 12818 /* 12819 * Now clean up at least as many resources as we will need. 12820 * 12821 * When requested to clean up inodes, the number that are needed 12822 * is set by the number of simultaneous writers (mnt_writeopcount) 12823 * plus a bit of slop (2) in case some more writers show up while 12824 * we are cleaning. 12825 * 12826 * When requested to free up space, the amount of space that 12827 * we need is enough blocks to allocate a full-sized segment 12828 * (fs_contigsumsize). The number of such segments that will 12829 * be needed is set by the number of simultaneous writers 12830 * (mnt_writeopcount) plus a bit of slop (2) in case some more 12831 * writers show up while we are cleaning. 12832 * 12833 * Additionally, if we are unpriviledged and allocating space, 12834 * we need to ensure that we clean up enough blocks to get the 12835 * needed number of blocks over the threshhold of the minimum 12836 * number of blocks required to be kept free by the filesystem 12837 * (fs_minfree). 12838 */ 12839 if (resource == FLUSH_INODES_WAIT) { 12840 needed = vp->v_mount->mnt_writeopcount + 2; 12841 } else if (resource == FLUSH_BLOCKS_WAIT) { 12842 needed = (vp->v_mount->mnt_writeopcount + 2) * 12843 fs->fs_contigsumsize; 12844 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 12845 needed += fragstoblks(fs, 12846 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 12847 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 12848 } else { 12849 UFS_LOCK(ump); 12850 printf("softdep_request_cleanup: Unknown resource type %d\n", 12851 resource); 12852 return (0); 12853 } 12854 starttime = time_second; 12855 retry: 12856 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 12857 fs->fs_cstotal.cs_nbfree <= needed) || 12858 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 12859 fs->fs_cstotal.cs_nifree <= needed)) { 12860 ACQUIRE_LOCK(&lk); 12861 if (ump->softdep_on_worklist > 0 && 12862 process_worklist_item(UFSTOVFS(ump), 12863 ump->softdep_on_worklist, LK_NOWAIT) != 0) 12864 stat_worklist_push += 1; 12865 FREE_LOCK(&lk); 12866 } 12867 /* 12868 * If we still need resources and there are no more worklist 12869 * entries to process to obtain them, we have to start flushing 12870 * the dirty vnodes to force the release of additional requests 12871 * to the worklist that we can then process to reap addition 12872 * resources. We walk the vnodes associated with the mount point 12873 * until we get the needed worklist requests that we can reap. 12874 */ 12875 if ((resource == FLUSH_BLOCKS_WAIT && 12876 fs->fs_cstotal.cs_nbfree <= needed) || 12877 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 12878 fs->fs_cstotal.cs_nifree <= needed)) { 12879 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 12880 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 12881 VI_UNLOCK(lvp); 12882 continue; 12883 } 12884 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 12885 curthread)) 12886 continue; 12887 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 12888 vput(lvp); 12889 continue; 12890 } 12891 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 12892 vput(lvp); 12893 } 12894 lvp = ump->um_devvp; 12895 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 12896 VOP_FSYNC(lvp, MNT_NOWAIT, curthread); 12897 VOP_UNLOCK(lvp, 0); 12898 } 12899 if (ump->softdep_on_worklist > 0) { 12900 stat_cleanup_retries += 1; 12901 goto retry; 12902 } 12903 stat_cleanup_failures += 1; 12904 } 12905 if (time_second - starttime > stat_cleanup_high_delay) 12906 stat_cleanup_high_delay = time_second - starttime; 12907 UFS_LOCK(ump); 12908 return (1); 12909 } 12910 12911 /* 12912 * If memory utilization has gotten too high, deliberately slow things 12913 * down and speed up the I/O processing. 12914 */ 12915 extern struct thread *syncertd; 12916 static int 12917 request_cleanup(mp, resource) 12918 struct mount *mp; 12919 int resource; 12920 { 12921 struct thread *td = curthread; 12922 struct ufsmount *ump; 12923 12924 rw_assert(&lk, RA_WLOCKED); 12925 /* 12926 * We never hold up the filesystem syncer or buf daemon. 12927 */ 12928 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 12929 return (0); 12930 ump = VFSTOUFS(mp); 12931 /* 12932 * First check to see if the work list has gotten backlogged. 12933 * If it has, co-opt this process to help clean up two entries. 12934 * Because this process may hold inodes locked, we cannot 12935 * handle any remove requests that might block on a locked 12936 * inode as that could lead to deadlock. We set TDP_SOFTDEP 12937 * to avoid recursively processing the worklist. 12938 */ 12939 if (ump->softdep_on_worklist > max_softdeps / 10) { 12940 td->td_pflags |= TDP_SOFTDEP; 12941 process_worklist_item(mp, 2, LK_NOWAIT); 12942 td->td_pflags &= ~TDP_SOFTDEP; 12943 stat_worklist_push += 2; 12944 return(1); 12945 } 12946 /* 12947 * Next, we attempt to speed up the syncer process. If that 12948 * is successful, then we allow the process to continue. 12949 */ 12950 if (softdep_speedup() && 12951 resource != FLUSH_BLOCKS_WAIT && 12952 resource != FLUSH_INODES_WAIT) 12953 return(0); 12954 /* 12955 * If we are resource constrained on inode dependencies, try 12956 * flushing some dirty inodes. Otherwise, we are constrained 12957 * by file deletions, so try accelerating flushes of directories 12958 * with removal dependencies. We would like to do the cleanup 12959 * here, but we probably hold an inode locked at this point and 12960 * that might deadlock against one that we try to clean. So, 12961 * the best that we can do is request the syncer daemon to do 12962 * the cleanup for us. 12963 */ 12964 switch (resource) { 12965 12966 case FLUSH_INODES: 12967 case FLUSH_INODES_WAIT: 12968 stat_ino_limit_push += 1; 12969 req_clear_inodedeps += 1; 12970 stat_countp = &stat_ino_limit_hit; 12971 break; 12972 12973 case FLUSH_BLOCKS: 12974 case FLUSH_BLOCKS_WAIT: 12975 stat_blk_limit_push += 1; 12976 req_clear_remove += 1; 12977 stat_countp = &stat_blk_limit_hit; 12978 break; 12979 12980 default: 12981 panic("request_cleanup: unknown type"); 12982 } 12983 /* 12984 * Hopefully the syncer daemon will catch up and awaken us. 12985 * We wait at most tickdelay before proceeding in any case. 12986 */ 12987 proc_waiting += 1; 12988 if (callout_pending(&softdep_callout) == FALSE) 12989 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 12990 pause_timer, 0); 12991 12992 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 12993 proc_waiting -= 1; 12994 return (1); 12995 } 12996 12997 /* 12998 * Awaken processes pausing in request_cleanup and clear proc_waiting 12999 * to indicate that there is no longer a timer running. 13000 */ 13001 static void 13002 pause_timer(arg) 13003 void *arg; 13004 { 13005 13006 /* 13007 * The callout_ API has acquired mtx and will hold it around this 13008 * function call. 13009 */ 13010 *stat_countp += 1; 13011 wakeup_one(&proc_waiting); 13012 if (proc_waiting > 0) 13013 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13014 pause_timer, 0); 13015 } 13016 13017 /* 13018 * Flush out a directory with at least one removal dependency in an effort to 13019 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13020 */ 13021 static void 13022 clear_remove(void) 13023 { 13024 struct pagedep_hashhead *pagedephd; 13025 struct pagedep *pagedep; 13026 static int next = 0; 13027 struct mount *mp; 13028 struct vnode *vp; 13029 struct bufobj *bo; 13030 int error, cnt; 13031 ino_t ino; 13032 13033 rw_assert(&lk, RA_WLOCKED); 13034 13035 for (cnt = 0; cnt <= pagedep_hash; cnt++) { 13036 pagedephd = &pagedep_hashtbl[next++]; 13037 if (next > pagedep_hash) 13038 next = 0; 13039 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13040 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13041 continue; 13042 mp = pagedep->pd_list.wk_mp; 13043 ino = pagedep->pd_ino; 13044 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13045 continue; 13046 FREE_LOCK(&lk); 13047 13048 /* 13049 * Let unmount clear deps 13050 */ 13051 error = vfs_busy(mp, MBF_NOWAIT); 13052 if (error != 0) 13053 goto finish_write; 13054 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13055 FFSV_FORCEINSMQ); 13056 vfs_unbusy(mp); 13057 if (error != 0) { 13058 softdep_error("clear_remove: vget", error); 13059 goto finish_write; 13060 } 13061 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13062 softdep_error("clear_remove: fsync", error); 13063 bo = &vp->v_bufobj; 13064 BO_LOCK(bo); 13065 drain_output(vp); 13066 BO_UNLOCK(bo); 13067 vput(vp); 13068 finish_write: 13069 vn_finished_write(mp); 13070 ACQUIRE_LOCK(&lk); 13071 return; 13072 } 13073 } 13074 } 13075 13076 /* 13077 * Clear out a block of dirty inodes in an effort to reduce 13078 * the number of inodedep dependency structures. 13079 */ 13080 static void 13081 clear_inodedeps(void) 13082 { 13083 struct inodedep_hashhead *inodedephd; 13084 struct inodedep *inodedep; 13085 static int next = 0; 13086 struct mount *mp; 13087 struct vnode *vp; 13088 struct fs *fs; 13089 int error, cnt; 13090 ino_t firstino, lastino, ino; 13091 13092 rw_assert(&lk, RA_WLOCKED); 13093 /* 13094 * Pick a random inode dependency to be cleared. 13095 * We will then gather up all the inodes in its block 13096 * that have dependencies and flush them out. 13097 */ 13098 for (cnt = 0; cnt <= inodedep_hash; cnt++) { 13099 inodedephd = &inodedep_hashtbl[next++]; 13100 if (next > inodedep_hash) 13101 next = 0; 13102 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13103 break; 13104 } 13105 if (inodedep == NULL) 13106 return; 13107 fs = inodedep->id_fs; 13108 mp = inodedep->id_list.wk_mp; 13109 /* 13110 * Find the last inode in the block with dependencies. 13111 */ 13112 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 13113 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13114 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13115 break; 13116 /* 13117 * Asynchronously push all but the last inode with dependencies. 13118 * Synchronously push the last inode with dependencies to ensure 13119 * that the inode block gets written to free up the inodedeps. 13120 */ 13121 for (ino = firstino; ino <= lastino; ino++) { 13122 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13123 continue; 13124 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13125 continue; 13126 FREE_LOCK(&lk); 13127 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13128 if (error != 0) { 13129 vn_finished_write(mp); 13130 ACQUIRE_LOCK(&lk); 13131 return; 13132 } 13133 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13134 FFSV_FORCEINSMQ)) != 0) { 13135 softdep_error("clear_inodedeps: vget", error); 13136 vfs_unbusy(mp); 13137 vn_finished_write(mp); 13138 ACQUIRE_LOCK(&lk); 13139 return; 13140 } 13141 vfs_unbusy(mp); 13142 if (ino == lastino) { 13143 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13144 softdep_error("clear_inodedeps: fsync1", error); 13145 } else { 13146 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13147 softdep_error("clear_inodedeps: fsync2", error); 13148 BO_LOCK(&vp->v_bufobj); 13149 drain_output(vp); 13150 BO_UNLOCK(&vp->v_bufobj); 13151 } 13152 vput(vp); 13153 vn_finished_write(mp); 13154 ACQUIRE_LOCK(&lk); 13155 } 13156 } 13157 13158 void 13159 softdep_buf_append(bp, wkhd) 13160 struct buf *bp; 13161 struct workhead *wkhd; 13162 { 13163 struct worklist *wk; 13164 13165 ACQUIRE_LOCK(&lk); 13166 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13167 WORKLIST_REMOVE(wk); 13168 WORKLIST_INSERT(&bp->b_dep, wk); 13169 } 13170 FREE_LOCK(&lk); 13171 13172 } 13173 13174 void 13175 softdep_inode_append(ip, cred, wkhd) 13176 struct inode *ip; 13177 struct ucred *cred; 13178 struct workhead *wkhd; 13179 { 13180 struct buf *bp; 13181 struct fs *fs; 13182 int error; 13183 13184 fs = ip->i_fs; 13185 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13186 (int)fs->fs_bsize, cred, &bp); 13187 if (error) { 13188 bqrelse(bp); 13189 softdep_freework(wkhd); 13190 return; 13191 } 13192 softdep_buf_append(bp, wkhd); 13193 bqrelse(bp); 13194 } 13195 13196 void 13197 softdep_freework(wkhd) 13198 struct workhead *wkhd; 13199 { 13200 13201 ACQUIRE_LOCK(&lk); 13202 handle_jwork(wkhd); 13203 FREE_LOCK(&lk); 13204 } 13205 13206 /* 13207 * Function to determine if the buffer has outstanding dependencies 13208 * that will cause a roll-back if the buffer is written. If wantcount 13209 * is set, return number of dependencies, otherwise just yes or no. 13210 */ 13211 static int 13212 softdep_count_dependencies(bp, wantcount) 13213 struct buf *bp; 13214 int wantcount; 13215 { 13216 struct worklist *wk; 13217 struct bmsafemap *bmsafemap; 13218 struct freework *freework; 13219 struct inodedep *inodedep; 13220 struct indirdep *indirdep; 13221 struct freeblks *freeblks; 13222 struct allocindir *aip; 13223 struct pagedep *pagedep; 13224 struct dirrem *dirrem; 13225 struct newblk *newblk; 13226 struct mkdir *mkdir; 13227 struct diradd *dap; 13228 int i, retval; 13229 13230 retval = 0; 13231 ACQUIRE_LOCK(&lk); 13232 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13233 switch (wk->wk_type) { 13234 13235 case D_INODEDEP: 13236 inodedep = WK_INODEDEP(wk); 13237 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 13238 /* bitmap allocation dependency */ 13239 retval += 1; 13240 if (!wantcount) 13241 goto out; 13242 } 13243 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 13244 /* direct block pointer dependency */ 13245 retval += 1; 13246 if (!wantcount) 13247 goto out; 13248 } 13249 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 13250 /* direct block pointer dependency */ 13251 retval += 1; 13252 if (!wantcount) 13253 goto out; 13254 } 13255 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 13256 /* Add reference dependency. */ 13257 retval += 1; 13258 if (!wantcount) 13259 goto out; 13260 } 13261 continue; 13262 13263 case D_INDIRDEP: 13264 indirdep = WK_INDIRDEP(wk); 13265 13266 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 13267 /* indirect truncation dependency */ 13268 retval += 1; 13269 if (!wantcount) 13270 goto out; 13271 } 13272 13273 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13274 /* indirect block pointer dependency */ 13275 retval += 1; 13276 if (!wantcount) 13277 goto out; 13278 } 13279 continue; 13280 13281 case D_PAGEDEP: 13282 pagedep = WK_PAGEDEP(wk); 13283 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 13284 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 13285 /* Journal remove ref dependency. */ 13286 retval += 1; 13287 if (!wantcount) 13288 goto out; 13289 } 13290 } 13291 for (i = 0; i < DAHASHSZ; i++) { 13292 13293 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 13294 /* directory entry dependency */ 13295 retval += 1; 13296 if (!wantcount) 13297 goto out; 13298 } 13299 } 13300 continue; 13301 13302 case D_BMSAFEMAP: 13303 bmsafemap = WK_BMSAFEMAP(wk); 13304 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 13305 /* Add reference dependency. */ 13306 retval += 1; 13307 if (!wantcount) 13308 goto out; 13309 } 13310 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 13311 /* Allocate block dependency. */ 13312 retval += 1; 13313 if (!wantcount) 13314 goto out; 13315 } 13316 continue; 13317 13318 case D_FREEBLKS: 13319 freeblks = WK_FREEBLKS(wk); 13320 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 13321 /* Freeblk journal dependency. */ 13322 retval += 1; 13323 if (!wantcount) 13324 goto out; 13325 } 13326 continue; 13327 13328 case D_ALLOCDIRECT: 13329 case D_ALLOCINDIR: 13330 newblk = WK_NEWBLK(wk); 13331 if (newblk->nb_jnewblk) { 13332 /* Journal allocate dependency. */ 13333 retval += 1; 13334 if (!wantcount) 13335 goto out; 13336 } 13337 continue; 13338 13339 case D_MKDIR: 13340 mkdir = WK_MKDIR(wk); 13341 if (mkdir->md_jaddref) { 13342 /* Journal reference dependency. */ 13343 retval += 1; 13344 if (!wantcount) 13345 goto out; 13346 } 13347 continue; 13348 13349 case D_FREEWORK: 13350 case D_FREEDEP: 13351 case D_JSEGDEP: 13352 case D_JSEG: 13353 case D_SBDEP: 13354 /* never a dependency on these blocks */ 13355 continue; 13356 13357 default: 13358 panic("softdep_count_dependencies: Unexpected type %s", 13359 TYPENAME(wk->wk_type)); 13360 /* NOTREACHED */ 13361 } 13362 } 13363 out: 13364 FREE_LOCK(&lk); 13365 return retval; 13366 } 13367 13368 /* 13369 * Acquire exclusive access to a buffer. 13370 * Must be called with a locked mtx parameter. 13371 * Return acquired buffer or NULL on failure. 13372 */ 13373 static struct buf * 13374 getdirtybuf(bp, lock, waitfor) 13375 struct buf *bp; 13376 struct rwlock *lock; 13377 int waitfor; 13378 { 13379 int error; 13380 13381 rw_assert(lock, RA_WLOCKED); 13382 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 13383 if (waitfor != MNT_WAIT) 13384 return (NULL); 13385 error = BUF_LOCK(bp, 13386 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, lock); 13387 /* 13388 * Even if we sucessfully acquire bp here, we have dropped 13389 * lock, which may violates our guarantee. 13390 */ 13391 if (error == 0) 13392 BUF_UNLOCK(bp); 13393 else if (error != ENOLCK) 13394 panic("getdirtybuf: inconsistent lock: %d", error); 13395 rw_wlock(lock); 13396 return (NULL); 13397 } 13398 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13399 if (lock == &lk && waitfor == MNT_WAIT) { 13400 rw_wunlock(lock); 13401 BO_LOCK(bp->b_bufobj); 13402 BUF_UNLOCK(bp); 13403 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13404 bp->b_vflags |= BV_BKGRDWAIT; 13405 msleep(&bp->b_xflags, BO_LOCKPTR(bp->b_bufobj), 13406 PRIBIO | PDROP, "getbuf", 0); 13407 } else 13408 BO_UNLOCK(bp->b_bufobj); 13409 rw_wlock(lock); 13410 return (NULL); 13411 } 13412 BUF_UNLOCK(bp); 13413 if (waitfor != MNT_WAIT) 13414 return (NULL); 13415 /* 13416 * The lock argument must be bp->b_vp's mutex in 13417 * this case. 13418 */ 13419 #ifdef DEBUG_VFS_LOCKS 13420 if (bp->b_vp->v_type != VCHR) 13421 ASSERT_BO_WLOCKED(bp->b_bufobj); 13422 #endif 13423 bp->b_vflags |= BV_BKGRDWAIT; 13424 rw_sleep(&bp->b_xflags, lock, PRIBIO, "getbuf", 0); 13425 return (NULL); 13426 } 13427 if ((bp->b_flags & B_DELWRI) == 0) { 13428 BUF_UNLOCK(bp); 13429 return (NULL); 13430 } 13431 bremfree(bp); 13432 return (bp); 13433 } 13434 13435 13436 /* 13437 * Check if it is safe to suspend the file system now. On entry, 13438 * the vnode interlock for devvp should be held. Return 0 with 13439 * the mount interlock held if the file system can be suspended now, 13440 * otherwise return EAGAIN with the mount interlock held. 13441 */ 13442 int 13443 softdep_check_suspend(struct mount *mp, 13444 struct vnode *devvp, 13445 int softdep_deps, 13446 int softdep_accdeps, 13447 int secondary_writes, 13448 int secondary_accwrites) 13449 { 13450 struct bufobj *bo; 13451 struct ufsmount *ump; 13452 int error; 13453 13454 ump = VFSTOUFS(mp); 13455 bo = &devvp->v_bufobj; 13456 ASSERT_BO_WLOCKED(bo); 13457 13458 for (;;) { 13459 if (!TRY_ACQUIRE_LOCK(&lk)) { 13460 BO_UNLOCK(bo); 13461 ACQUIRE_LOCK(&lk); 13462 FREE_LOCK(&lk); 13463 BO_LOCK(bo); 13464 continue; 13465 } 13466 MNT_ILOCK(mp); 13467 if (mp->mnt_secondary_writes != 0) { 13468 FREE_LOCK(&lk); 13469 BO_UNLOCK(bo); 13470 msleep(&mp->mnt_secondary_writes, 13471 MNT_MTX(mp), 13472 (PUSER - 1) | PDROP, "secwr", 0); 13473 BO_LOCK(bo); 13474 continue; 13475 } 13476 break; 13477 } 13478 13479 /* 13480 * Reasons for needing more work before suspend: 13481 * - Dirty buffers on devvp. 13482 * - Softdep activity occurred after start of vnode sync loop 13483 * - Secondary writes occurred after start of vnode sync loop 13484 */ 13485 error = 0; 13486 if (bo->bo_numoutput > 0 || 13487 bo->bo_dirty.bv_cnt > 0 || 13488 softdep_deps != 0 || 13489 ump->softdep_deps != 0 || 13490 softdep_accdeps != ump->softdep_accdeps || 13491 secondary_writes != 0 || 13492 mp->mnt_secondary_writes != 0 || 13493 secondary_accwrites != mp->mnt_secondary_accwrites) 13494 error = EAGAIN; 13495 FREE_LOCK(&lk); 13496 BO_UNLOCK(bo); 13497 return (error); 13498 } 13499 13500 13501 /* 13502 * Get the number of dependency structures for the file system, both 13503 * the current number and the total number allocated. These will 13504 * later be used to detect that softdep processing has occurred. 13505 */ 13506 void 13507 softdep_get_depcounts(struct mount *mp, 13508 int *softdep_depsp, 13509 int *softdep_accdepsp) 13510 { 13511 struct ufsmount *ump; 13512 13513 ump = VFSTOUFS(mp); 13514 ACQUIRE_LOCK(&lk); 13515 *softdep_depsp = ump->softdep_deps; 13516 *softdep_accdepsp = ump->softdep_accdeps; 13517 FREE_LOCK(&lk); 13518 } 13519 13520 /* 13521 * Wait for pending output on a vnode to complete. 13522 * Must be called with vnode lock and interlock locked. 13523 * 13524 * XXX: Should just be a call to bufobj_wwait(). 13525 */ 13526 static void 13527 drain_output(vp) 13528 struct vnode *vp; 13529 { 13530 struct bufobj *bo; 13531 13532 bo = &vp->v_bufobj; 13533 ASSERT_VOP_LOCKED(vp, "drain_output"); 13534 ASSERT_BO_WLOCKED(bo); 13535 13536 while (bo->bo_numoutput) { 13537 bo->bo_flag |= BO_WWAIT; 13538 msleep((caddr_t)&bo->bo_numoutput, 13539 BO_LOCKPTR(bo), PRIBIO + 1, "drainvp", 0); 13540 } 13541 } 13542 13543 /* 13544 * Called whenever a buffer that is being invalidated or reallocated 13545 * contains dependencies. This should only happen if an I/O error has 13546 * occurred. The routine is called with the buffer locked. 13547 */ 13548 static void 13549 softdep_deallocate_dependencies(bp) 13550 struct buf *bp; 13551 { 13552 13553 if ((bp->b_ioflags & BIO_ERROR) == 0) 13554 panic("softdep_deallocate_dependencies: dangling deps"); 13555 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 13556 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 13557 else 13558 printf("softdep_deallocate_dependencies: " 13559 "got error %d while accessing filesystem\n", bp->b_error); 13560 if (bp->b_error != ENXIO) 13561 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 13562 } 13563 13564 /* 13565 * Function to handle asynchronous write errors in the filesystem. 13566 */ 13567 static void 13568 softdep_error(func, error) 13569 char *func; 13570 int error; 13571 { 13572 13573 /* XXX should do something better! */ 13574 printf("%s: got error %d while accessing filesystem\n", func, error); 13575 } 13576 13577 #ifdef DDB 13578 13579 static void 13580 inodedep_print(struct inodedep *inodedep, int verbose) 13581 { 13582 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d" 13583 " saveino %p\n", 13584 inodedep, inodedep->id_fs, inodedep->id_state, 13585 (intmax_t)inodedep->id_ino, 13586 (intmax_t)fsbtodb(inodedep->id_fs, 13587 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 13588 inodedep->id_nlinkdelta, inodedep->id_savednlink, 13589 inodedep->id_savedino1); 13590 13591 if (verbose == 0) 13592 return; 13593 13594 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 13595 "mkdiradd %p\n", 13596 LIST_FIRST(&inodedep->id_pendinghd), 13597 LIST_FIRST(&inodedep->id_bufwait), 13598 LIST_FIRST(&inodedep->id_inowait), 13599 TAILQ_FIRST(&inodedep->id_inoreflst), 13600 inodedep->id_mkdiradd); 13601 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 13602 TAILQ_FIRST(&inodedep->id_inoupdt), 13603 TAILQ_FIRST(&inodedep->id_newinoupdt), 13604 TAILQ_FIRST(&inodedep->id_extupdt), 13605 TAILQ_FIRST(&inodedep->id_newextupdt)); 13606 } 13607 13608 DB_SHOW_COMMAND(inodedep, db_show_inodedep) 13609 { 13610 13611 if (have_addr == 0) { 13612 db_printf("Address required\n"); 13613 return; 13614 } 13615 inodedep_print((struct inodedep*)addr, 1); 13616 } 13617 13618 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 13619 { 13620 struct inodedep_hashhead *inodedephd; 13621 struct inodedep *inodedep; 13622 struct fs *fs; 13623 int cnt; 13624 13625 fs = have_addr ? (struct fs *)addr : NULL; 13626 for (cnt = 0; cnt < inodedep_hash; cnt++) { 13627 inodedephd = &inodedep_hashtbl[cnt]; 13628 LIST_FOREACH(inodedep, inodedephd, id_hash) { 13629 if (fs != NULL && fs != inodedep->id_fs) 13630 continue; 13631 inodedep_print(inodedep, 0); 13632 } 13633 } 13634 } 13635 13636 DB_SHOW_COMMAND(worklist, db_show_worklist) 13637 { 13638 struct worklist *wk; 13639 13640 if (have_addr == 0) { 13641 db_printf("Address required\n"); 13642 return; 13643 } 13644 wk = (struct worklist *)addr; 13645 printf("worklist: %p type %s state 0x%X\n", 13646 wk, TYPENAME(wk->wk_type), wk->wk_state); 13647 } 13648 13649 DB_SHOW_COMMAND(workhead, db_show_workhead) 13650 { 13651 struct workhead *wkhd; 13652 struct worklist *wk; 13653 int i; 13654 13655 if (have_addr == 0) { 13656 db_printf("Address required\n"); 13657 return; 13658 } 13659 wkhd = (struct workhead *)addr; 13660 wk = LIST_FIRST(wkhd); 13661 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 13662 db_printf("worklist: %p type %s state 0x%X", 13663 wk, TYPENAME(wk->wk_type), wk->wk_state); 13664 if (i == 100) 13665 db_printf("workhead overflow"); 13666 printf("\n"); 13667 } 13668 13669 13670 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 13671 { 13672 struct jaddref *jaddref; 13673 struct diradd *diradd; 13674 struct mkdir *mkdir; 13675 13676 LIST_FOREACH(mkdir, &mkdirlisthd, md_mkdirs) { 13677 diradd = mkdir->md_diradd; 13678 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 13679 mkdir, mkdir->md_state, diradd, diradd->da_state); 13680 if ((jaddref = mkdir->md_jaddref) != NULL) 13681 db_printf(" jaddref %p jaddref state 0x%X", 13682 jaddref, jaddref->ja_state); 13683 db_printf("\n"); 13684 } 13685 } 13686 13687 #endif /* DDB */ 13688 13689 #endif /* SOFTUPDATES */ 13690