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/stat.h> 73 #include <sys/sysctl.h> 74 #include <sys/syslog.h> 75 #include <sys/vnode.h> 76 #include <sys/conf.h> 77 78 #include <ufs/ufs/dir.h> 79 #include <ufs/ufs/extattr.h> 80 #include <ufs/ufs/quota.h> 81 #include <ufs/ufs/inode.h> 82 #include <ufs/ufs/ufsmount.h> 83 #include <ufs/ffs/fs.h> 84 #include <ufs/ffs/softdep.h> 85 #include <ufs/ffs/ffs_extern.h> 86 #include <ufs/ufs/ufs_extern.h> 87 88 #include <vm/vm.h> 89 #include <vm/vm_extern.h> 90 #include <vm/vm_object.h> 91 92 #include <geom/geom.h> 93 94 #include <ddb/ddb.h> 95 96 #define KTR_SUJ 0 /* Define to KTR_SPARE. */ 97 98 #ifndef SOFTUPDATES 99 100 int 101 softdep_flushfiles(oldmnt, flags, td) 102 struct mount *oldmnt; 103 int flags; 104 struct thread *td; 105 { 106 107 panic("softdep_flushfiles called"); 108 } 109 110 int 111 softdep_mount(devvp, mp, fs, cred) 112 struct vnode *devvp; 113 struct mount *mp; 114 struct fs *fs; 115 struct ucred *cred; 116 { 117 118 return (0); 119 } 120 121 void 122 softdep_initialize() 123 { 124 125 return; 126 } 127 128 void 129 softdep_uninitialize() 130 { 131 132 return; 133 } 134 135 void 136 softdep_unmount(mp) 137 struct mount *mp; 138 { 139 140 } 141 142 void 143 softdep_setup_sbupdate(ump, fs, bp) 144 struct ufsmount *ump; 145 struct fs *fs; 146 struct buf *bp; 147 { 148 } 149 150 void 151 softdep_setup_inomapdep(bp, ip, newinum, mode) 152 struct buf *bp; 153 struct inode *ip; 154 ino_t newinum; 155 int mode; 156 { 157 158 panic("softdep_setup_inomapdep called"); 159 } 160 161 void 162 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 163 struct buf *bp; 164 struct mount *mp; 165 ufs2_daddr_t newblkno; 166 int frags; 167 int oldfrags; 168 { 169 170 panic("softdep_setup_blkmapdep called"); 171 } 172 173 void 174 softdep_setup_allocdirect(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 175 struct inode *ip; 176 ufs_lbn_t lbn; 177 ufs2_daddr_t newblkno; 178 ufs2_daddr_t oldblkno; 179 long newsize; 180 long oldsize; 181 struct buf *bp; 182 { 183 184 panic("softdep_setup_allocdirect called"); 185 } 186 187 void 188 softdep_setup_allocext(ip, lbn, newblkno, oldblkno, newsize, oldsize, bp) 189 struct inode *ip; 190 ufs_lbn_t lbn; 191 ufs2_daddr_t newblkno; 192 ufs2_daddr_t oldblkno; 193 long newsize; 194 long oldsize; 195 struct buf *bp; 196 { 197 198 panic("softdep_setup_allocext called"); 199 } 200 201 void 202 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 203 struct inode *ip; 204 ufs_lbn_t lbn; 205 struct buf *bp; 206 int ptrno; 207 ufs2_daddr_t newblkno; 208 ufs2_daddr_t oldblkno; 209 struct buf *nbp; 210 { 211 212 panic("softdep_setup_allocindir_page called"); 213 } 214 215 void 216 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 217 struct buf *nbp; 218 struct inode *ip; 219 struct buf *bp; 220 int ptrno; 221 ufs2_daddr_t newblkno; 222 { 223 224 panic("softdep_setup_allocindir_meta called"); 225 } 226 227 void 228 softdep_journal_freeblocks(ip, cred, length, flags) 229 struct inode *ip; 230 struct ucred *cred; 231 off_t length; 232 int flags; 233 { 234 235 panic("softdep_journal_freeblocks called"); 236 } 237 238 void 239 softdep_journal_fsync(ip) 240 struct inode *ip; 241 { 242 243 panic("softdep_journal_fsync called"); 244 } 245 246 void 247 softdep_setup_freeblocks(ip, length, flags) 248 struct inode *ip; 249 off_t length; 250 int flags; 251 { 252 253 panic("softdep_setup_freeblocks called"); 254 } 255 256 void 257 softdep_freefile(pvp, ino, mode) 258 struct vnode *pvp; 259 ino_t ino; 260 int mode; 261 { 262 263 panic("softdep_freefile called"); 264 } 265 266 int 267 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 268 struct buf *bp; 269 struct inode *dp; 270 off_t diroffset; 271 ino_t newinum; 272 struct buf *newdirbp; 273 int isnewblk; 274 { 275 276 panic("softdep_setup_directory_add called"); 277 } 278 279 void 280 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 281 struct buf *bp; 282 struct inode *dp; 283 caddr_t base; 284 caddr_t oldloc; 285 caddr_t newloc; 286 int entrysize; 287 { 288 289 panic("softdep_change_directoryentry_offset called"); 290 } 291 292 void 293 softdep_setup_remove(bp, dp, ip, isrmdir) 294 struct buf *bp; 295 struct inode *dp; 296 struct inode *ip; 297 int isrmdir; 298 { 299 300 panic("softdep_setup_remove called"); 301 } 302 303 void 304 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 305 struct buf *bp; 306 struct inode *dp; 307 struct inode *ip; 308 ino_t newinum; 309 int isrmdir; 310 { 311 312 panic("softdep_setup_directory_change called"); 313 } 314 315 void 316 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 317 struct mount *mp; 318 struct buf *bp; 319 ufs2_daddr_t blkno; 320 int frags; 321 struct workhead *wkhd; 322 { 323 324 panic("%s called", __FUNCTION__); 325 } 326 327 void 328 softdep_setup_inofree(mp, bp, ino, wkhd) 329 struct mount *mp; 330 struct buf *bp; 331 ino_t ino; 332 struct workhead *wkhd; 333 { 334 335 panic("%s called", __FUNCTION__); 336 } 337 338 void 339 softdep_setup_unlink(dp, ip) 340 struct inode *dp; 341 struct inode *ip; 342 { 343 344 panic("%s called", __FUNCTION__); 345 } 346 347 void 348 softdep_setup_link(dp, ip) 349 struct inode *dp; 350 struct inode *ip; 351 { 352 353 panic("%s called", __FUNCTION__); 354 } 355 356 void 357 softdep_revert_link(dp, ip) 358 struct inode *dp; 359 struct inode *ip; 360 { 361 362 panic("%s called", __FUNCTION__); 363 } 364 365 void 366 softdep_setup_rmdir(dp, ip) 367 struct inode *dp; 368 struct inode *ip; 369 { 370 371 panic("%s called", __FUNCTION__); 372 } 373 374 void 375 softdep_revert_rmdir(dp, ip) 376 struct inode *dp; 377 struct inode *ip; 378 { 379 380 panic("%s called", __FUNCTION__); 381 } 382 383 void 384 softdep_setup_create(dp, ip) 385 struct inode *dp; 386 struct inode *ip; 387 { 388 389 panic("%s called", __FUNCTION__); 390 } 391 392 void 393 softdep_revert_create(dp, ip) 394 struct inode *dp; 395 struct inode *ip; 396 { 397 398 panic("%s called", __FUNCTION__); 399 } 400 401 void 402 softdep_setup_mkdir(dp, ip) 403 struct inode *dp; 404 struct inode *ip; 405 { 406 407 panic("%s called", __FUNCTION__); 408 } 409 410 void 411 softdep_revert_mkdir(dp, ip) 412 struct inode *dp; 413 struct inode *ip; 414 { 415 416 panic("%s called", __FUNCTION__); 417 } 418 419 void 420 softdep_setup_dotdot_link(dp, ip) 421 struct inode *dp; 422 struct inode *ip; 423 { 424 425 panic("%s called", __FUNCTION__); 426 } 427 428 int 429 softdep_prealloc(vp, waitok) 430 struct vnode *vp; 431 int waitok; 432 { 433 434 panic("%s called", __FUNCTION__); 435 436 return (0); 437 } 438 439 int 440 softdep_journal_lookup(mp, vpp) 441 struct mount *mp; 442 struct vnode **vpp; 443 { 444 445 return (ENOENT); 446 } 447 448 void 449 softdep_change_linkcnt(ip) 450 struct inode *ip; 451 { 452 453 panic("softdep_change_linkcnt called"); 454 } 455 456 void 457 softdep_load_inodeblock(ip) 458 struct inode *ip; 459 { 460 461 panic("softdep_load_inodeblock called"); 462 } 463 464 void 465 softdep_update_inodeblock(ip, bp, waitfor) 466 struct inode *ip; 467 struct buf *bp; 468 int waitfor; 469 { 470 471 panic("softdep_update_inodeblock called"); 472 } 473 474 int 475 softdep_fsync(vp) 476 struct vnode *vp; /* the "in_core" copy of the inode */ 477 { 478 479 return (0); 480 } 481 482 void 483 softdep_fsync_mountdev(vp) 484 struct vnode *vp; 485 { 486 487 return; 488 } 489 490 int 491 softdep_flushworklist(oldmnt, countp, td) 492 struct mount *oldmnt; 493 int *countp; 494 struct thread *td; 495 { 496 497 *countp = 0; 498 return (0); 499 } 500 501 int 502 softdep_sync_metadata(struct vnode *vp) 503 { 504 505 return (0); 506 } 507 508 int 509 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 510 { 511 512 return (0); 513 } 514 515 int 516 softdep_slowdown(vp) 517 struct vnode *vp; 518 { 519 520 panic("softdep_slowdown called"); 521 } 522 523 void 524 softdep_releasefile(ip) 525 struct inode *ip; /* inode with the zero effective link count */ 526 { 527 528 panic("softdep_releasefile called"); 529 } 530 531 int 532 softdep_request_cleanup(fs, vp, cred, resource) 533 struct fs *fs; 534 struct vnode *vp; 535 struct ucred *cred; 536 int resource; 537 { 538 539 return (0); 540 } 541 542 int 543 softdep_check_suspend(struct mount *mp, 544 struct vnode *devvp, 545 int softdep_deps, 546 int softdep_accdeps, 547 int secondary_writes, 548 int secondary_accwrites) 549 { 550 struct bufobj *bo; 551 int error; 552 553 (void) softdep_deps, 554 (void) softdep_accdeps; 555 556 bo = &devvp->v_bufobj; 557 ASSERT_BO_LOCKED(bo); 558 559 MNT_ILOCK(mp); 560 while (mp->mnt_secondary_writes != 0) { 561 BO_UNLOCK(bo); 562 msleep(&mp->mnt_secondary_writes, MNT_MTX(mp), 563 (PUSER - 1) | PDROP, "secwr", 0); 564 BO_LOCK(bo); 565 MNT_ILOCK(mp); 566 } 567 568 /* 569 * Reasons for needing more work before suspend: 570 * - Dirty buffers on devvp. 571 * - Secondary writes occurred after start of vnode sync loop 572 */ 573 error = 0; 574 if (bo->bo_numoutput > 0 || 575 bo->bo_dirty.bv_cnt > 0 || 576 secondary_writes != 0 || 577 mp->mnt_secondary_writes != 0 || 578 secondary_accwrites != mp->mnt_secondary_accwrites) 579 error = EAGAIN; 580 BO_UNLOCK(bo); 581 return (error); 582 } 583 584 void 585 softdep_get_depcounts(struct mount *mp, 586 int *softdepactivep, 587 int *softdepactiveaccp) 588 { 589 (void) mp; 590 *softdepactivep = 0; 591 *softdepactiveaccp = 0; 592 } 593 594 void 595 softdep_buf_append(bp, wkhd) 596 struct buf *bp; 597 struct workhead *wkhd; 598 { 599 600 panic("softdep_buf_appendwork called"); 601 } 602 603 void 604 softdep_inode_append(ip, cred, wkhd) 605 struct inode *ip; 606 struct ucred *cred; 607 struct workhead *wkhd; 608 { 609 610 panic("softdep_inode_appendwork called"); 611 } 612 613 void 614 softdep_freework(wkhd) 615 struct workhead *wkhd; 616 { 617 618 panic("softdep_freework called"); 619 } 620 621 #else 622 623 FEATURE(softupdates, "FFS soft-updates support"); 624 625 /* 626 * These definitions need to be adapted to the system to which 627 * this file is being ported. 628 */ 629 630 #define M_SOFTDEP_FLAGS (M_WAITOK) 631 632 #define D_PAGEDEP 0 633 #define D_INODEDEP 1 634 #define D_BMSAFEMAP 2 635 #define D_NEWBLK 3 636 #define D_ALLOCDIRECT 4 637 #define D_INDIRDEP 5 638 #define D_ALLOCINDIR 6 639 #define D_FREEFRAG 7 640 #define D_FREEBLKS 8 641 #define D_FREEFILE 9 642 #define D_DIRADD 10 643 #define D_MKDIR 11 644 #define D_DIRREM 12 645 #define D_NEWDIRBLK 13 646 #define D_FREEWORK 14 647 #define D_FREEDEP 15 648 #define D_JADDREF 16 649 #define D_JREMREF 17 650 #define D_JMVREF 18 651 #define D_JNEWBLK 19 652 #define D_JFREEBLK 20 653 #define D_JFREEFRAG 21 654 #define D_JSEG 22 655 #define D_JSEGDEP 23 656 #define D_SBDEP 24 657 #define D_JTRUNC 25 658 #define D_JFSYNC 26 659 #define D_SENTINAL 27 660 #define D_LAST D_SENTINAL 661 662 unsigned long dep_current[D_LAST + 1]; 663 unsigned long dep_total[D_LAST + 1]; 664 unsigned long dep_write[D_LAST + 1]; 665 666 667 static SYSCTL_NODE(_debug, OID_AUTO, softdep, CTLFLAG_RW, 0, 668 "soft updates stats"); 669 static SYSCTL_NODE(_debug_softdep, OID_AUTO, total, CTLFLAG_RW, 0, 670 "total dependencies allocated"); 671 static SYSCTL_NODE(_debug_softdep, OID_AUTO, current, CTLFLAG_RW, 0, 672 "current dependencies allocated"); 673 static SYSCTL_NODE(_debug_softdep, OID_AUTO, write, CTLFLAG_RW, 0, 674 "current dependencies written"); 675 676 #define SOFTDEP_TYPE(type, str, long) \ 677 static MALLOC_DEFINE(M_ ## type, #str, long); \ 678 SYSCTL_ULONG(_debug_softdep_total, OID_AUTO, str, CTLFLAG_RD, \ 679 &dep_total[D_ ## type], 0, ""); \ 680 SYSCTL_ULONG(_debug_softdep_current, OID_AUTO, str, CTLFLAG_RD, \ 681 &dep_current[D_ ## type], 0, ""); \ 682 SYSCTL_ULONG(_debug_softdep_write, OID_AUTO, str, CTLFLAG_RD, \ 683 &dep_write[D_ ## type], 0, ""); 684 685 SOFTDEP_TYPE(PAGEDEP, pagedep, "File page dependencies"); 686 SOFTDEP_TYPE(INODEDEP, inodedep, "Inode dependencies"); 687 SOFTDEP_TYPE(BMSAFEMAP, bmsafemap, 688 "Block or frag allocated from cyl group map"); 689 SOFTDEP_TYPE(NEWBLK, newblk, "New block or frag allocation dependency"); 690 SOFTDEP_TYPE(ALLOCDIRECT, allocdirect, "Block or frag dependency for an inode"); 691 SOFTDEP_TYPE(INDIRDEP, indirdep, "Indirect block dependencies"); 692 SOFTDEP_TYPE(ALLOCINDIR, allocindir, "Block dependency for an indirect block"); 693 SOFTDEP_TYPE(FREEFRAG, freefrag, "Previously used frag for an inode"); 694 SOFTDEP_TYPE(FREEBLKS, freeblks, "Blocks freed from an inode"); 695 SOFTDEP_TYPE(FREEFILE, freefile, "Inode deallocated"); 696 SOFTDEP_TYPE(DIRADD, diradd, "New directory entry"); 697 SOFTDEP_TYPE(MKDIR, mkdir, "New directory"); 698 SOFTDEP_TYPE(DIRREM, dirrem, "Directory entry deleted"); 699 SOFTDEP_TYPE(NEWDIRBLK, newdirblk, "Unclaimed new directory block"); 700 SOFTDEP_TYPE(FREEWORK, freework, "free an inode block"); 701 SOFTDEP_TYPE(FREEDEP, freedep, "track a block free"); 702 SOFTDEP_TYPE(JADDREF, jaddref, "Journal inode ref add"); 703 SOFTDEP_TYPE(JREMREF, jremref, "Journal inode ref remove"); 704 SOFTDEP_TYPE(JMVREF, jmvref, "Journal inode ref move"); 705 SOFTDEP_TYPE(JNEWBLK, jnewblk, "Journal new block"); 706 SOFTDEP_TYPE(JFREEBLK, jfreeblk, "Journal free block"); 707 SOFTDEP_TYPE(JFREEFRAG, jfreefrag, "Journal free frag"); 708 SOFTDEP_TYPE(JSEG, jseg, "Journal segment"); 709 SOFTDEP_TYPE(JSEGDEP, jsegdep, "Journal segment complete"); 710 SOFTDEP_TYPE(SBDEP, sbdep, "Superblock write dependency"); 711 SOFTDEP_TYPE(JTRUNC, jtrunc, "Journal inode truncation"); 712 SOFTDEP_TYPE(JFSYNC, jfsync, "Journal fsync complete"); 713 714 static MALLOC_DEFINE(M_SAVEDINO, "savedino", "Saved inodes"); 715 static MALLOC_DEFINE(M_JBLOCKS, "jblocks", "Journal block locations"); 716 717 /* 718 * translate from workitem type to memory type 719 * MUST match the defines above, such that memtype[D_XXX] == M_XXX 720 */ 721 static struct malloc_type *memtype[] = { 722 M_PAGEDEP, 723 M_INODEDEP, 724 M_BMSAFEMAP, 725 M_NEWBLK, 726 M_ALLOCDIRECT, 727 M_INDIRDEP, 728 M_ALLOCINDIR, 729 M_FREEFRAG, 730 M_FREEBLKS, 731 M_FREEFILE, 732 M_DIRADD, 733 M_MKDIR, 734 M_DIRREM, 735 M_NEWDIRBLK, 736 M_FREEWORK, 737 M_FREEDEP, 738 M_JADDREF, 739 M_JREMREF, 740 M_JMVREF, 741 M_JNEWBLK, 742 M_JFREEBLK, 743 M_JFREEFRAG, 744 M_JSEG, 745 M_JSEGDEP, 746 M_SBDEP, 747 M_JTRUNC, 748 M_JFSYNC 749 }; 750 751 static LIST_HEAD(mkdirlist, mkdir) mkdirlisthd; 752 753 #define DtoM(type) (memtype[type]) 754 755 /* 756 * Names of malloc types. 757 */ 758 #define TYPENAME(type) \ 759 ((unsigned)(type) <= D_LAST ? memtype[type]->ks_shortdesc : "???") 760 /* 761 * End system adaptation definitions. 762 */ 763 764 #define DOTDOT_OFFSET offsetof(struct dirtemplate, dotdot_ino) 765 #define DOT_OFFSET offsetof(struct dirtemplate, dot_ino) 766 767 /* 768 * Forward declarations. 769 */ 770 struct inodedep_hashhead; 771 struct newblk_hashhead; 772 struct pagedep_hashhead; 773 struct bmsafemap_hashhead; 774 775 /* 776 * Private journaling structures. 777 */ 778 struct jblocks { 779 struct jseglst jb_segs; /* TAILQ of current segments. */ 780 struct jseg *jb_writeseg; /* Next write to complete. */ 781 struct jseg *jb_oldestseg; /* Oldest segment with valid entries. */ 782 struct jextent *jb_extent; /* Extent array. */ 783 uint64_t jb_nextseq; /* Next sequence number. */ 784 uint64_t jb_oldestwrseq; /* Oldest written sequence number. */ 785 uint8_t jb_needseg; /* Need a forced segment. */ 786 uint8_t jb_suspended; /* Did journal suspend writes? */ 787 int jb_avail; /* Available extents. */ 788 int jb_used; /* Last used extent. */ 789 int jb_head; /* Allocator head. */ 790 int jb_off; /* Allocator extent offset. */ 791 int jb_blocks; /* Total disk blocks covered. */ 792 int jb_free; /* Total disk blocks free. */ 793 int jb_min; /* Minimum free space. */ 794 int jb_low; /* Low on space. */ 795 int jb_age; /* Insertion time of oldest rec. */ 796 }; 797 798 struct jextent { 799 ufs2_daddr_t je_daddr; /* Disk block address. */ 800 int je_blocks; /* Disk block count. */ 801 }; 802 803 /* 804 * Internal function prototypes. 805 */ 806 static void softdep_error(char *, int); 807 static void drain_output(struct vnode *); 808 static struct buf *getdirtybuf(struct buf *, struct mtx *, int); 809 static void clear_remove(void); 810 static void clear_inodedeps(void); 811 static void unlinked_inodedep(struct mount *, struct inodedep *); 812 static void clear_unlinked_inodedep(struct inodedep *); 813 static struct inodedep *first_unlinked_inodedep(struct ufsmount *); 814 static int flush_pagedep_deps(struct vnode *, struct mount *, 815 struct diraddhd *); 816 static int free_pagedep(struct pagedep *); 817 static int flush_newblk_dep(struct vnode *, struct mount *, ufs_lbn_t); 818 static int flush_inodedep_deps(struct vnode *, struct mount *, ino_t); 819 static int flush_deplist(struct allocdirectlst *, int, int *); 820 static int sync_cgs(struct mount *, int); 821 static int handle_written_filepage(struct pagedep *, struct buf *); 822 static int handle_written_sbdep(struct sbdep *, struct buf *); 823 static void initiate_write_sbdep(struct sbdep *); 824 static void diradd_inode_written(struct diradd *, struct inodedep *); 825 static int handle_written_indirdep(struct indirdep *, struct buf *, 826 struct buf**); 827 static int handle_written_inodeblock(struct inodedep *, struct buf *); 828 static int jnewblk_rollforward(struct jnewblk *, struct fs *, struct cg *, 829 uint8_t *); 830 static int handle_written_bmsafemap(struct bmsafemap *, struct buf *); 831 static void handle_written_jaddref(struct jaddref *); 832 static void handle_written_jremref(struct jremref *); 833 static void handle_written_jseg(struct jseg *, struct buf *); 834 static void handle_written_jnewblk(struct jnewblk *); 835 static void handle_written_jblkdep(struct jblkdep *); 836 static void handle_written_jfreefrag(struct jfreefrag *); 837 static void complete_jseg(struct jseg *); 838 static void complete_jsegs(struct jseg *); 839 static void jseg_write(struct ufsmount *ump, struct jseg *, uint8_t *); 840 static void jaddref_write(struct jaddref *, struct jseg *, uint8_t *); 841 static void jremref_write(struct jremref *, struct jseg *, uint8_t *); 842 static void jmvref_write(struct jmvref *, struct jseg *, uint8_t *); 843 static void jtrunc_write(struct jtrunc *, struct jseg *, uint8_t *); 844 static void jfsync_write(struct jfsync *, struct jseg *, uint8_t *data); 845 static void jnewblk_write(struct jnewblk *, struct jseg *, uint8_t *); 846 static void jfreeblk_write(struct jfreeblk *, struct jseg *, uint8_t *); 847 static void jfreefrag_write(struct jfreefrag *, struct jseg *, uint8_t *); 848 static inline void inoref_write(struct inoref *, struct jseg *, 849 struct jrefrec *); 850 static void handle_allocdirect_partdone(struct allocdirect *, 851 struct workhead *); 852 static struct jnewblk *cancel_newblk(struct newblk *, struct worklist *, 853 struct workhead *); 854 static void indirdep_complete(struct indirdep *); 855 static int indirblk_lookup(struct mount *, ufs2_daddr_t); 856 static void indirblk_insert(struct freework *); 857 static void indirblk_remove(struct freework *); 858 static void handle_allocindir_partdone(struct allocindir *); 859 static void initiate_write_filepage(struct pagedep *, struct buf *); 860 static void initiate_write_indirdep(struct indirdep*, struct buf *); 861 static void handle_written_mkdir(struct mkdir *, int); 862 static int jnewblk_rollback(struct jnewblk *, struct fs *, struct cg *, 863 uint8_t *); 864 static void initiate_write_bmsafemap(struct bmsafemap *, struct buf *); 865 static void initiate_write_inodeblock_ufs1(struct inodedep *, struct buf *); 866 static void initiate_write_inodeblock_ufs2(struct inodedep *, struct buf *); 867 static void handle_workitem_freefile(struct freefile *); 868 static int handle_workitem_remove(struct dirrem *, int); 869 static struct dirrem *newdirrem(struct buf *, struct inode *, 870 struct inode *, int, struct dirrem **); 871 static struct indirdep *indirdep_lookup(struct mount *, struct inode *, 872 struct buf *); 873 static void cancel_indirdep(struct indirdep *, struct buf *, 874 struct freeblks *); 875 static void free_indirdep(struct indirdep *); 876 static void free_diradd(struct diradd *, struct workhead *); 877 static void merge_diradd(struct inodedep *, struct diradd *); 878 static void complete_diradd(struct diradd *); 879 static struct diradd *diradd_lookup(struct pagedep *, int); 880 static struct jremref *cancel_diradd_dotdot(struct inode *, struct dirrem *, 881 struct jremref *); 882 static struct jremref *cancel_mkdir_dotdot(struct inode *, struct dirrem *, 883 struct jremref *); 884 static void cancel_diradd(struct diradd *, struct dirrem *, struct jremref *, 885 struct jremref *, struct jremref *); 886 static void dirrem_journal(struct dirrem *, struct jremref *, struct jremref *, 887 struct jremref *); 888 static void cancel_allocindir(struct allocindir *, struct buf *bp, 889 struct freeblks *, int); 890 static int setup_trunc_indir(struct freeblks *, struct inode *, 891 ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t); 892 static void complete_trunc_indir(struct freework *); 893 static void trunc_indirdep(struct indirdep *, struct freeblks *, struct buf *, 894 int); 895 static void complete_mkdir(struct mkdir *); 896 static void free_newdirblk(struct newdirblk *); 897 static void free_jremref(struct jremref *); 898 static void free_jaddref(struct jaddref *); 899 static void free_jsegdep(struct jsegdep *); 900 static void free_jsegs(struct jblocks *); 901 static void rele_jseg(struct jseg *); 902 static void free_jseg(struct jseg *, struct jblocks *); 903 static void free_jnewblk(struct jnewblk *); 904 static void free_jblkdep(struct jblkdep *); 905 static void free_jfreefrag(struct jfreefrag *); 906 static void free_freedep(struct freedep *); 907 static void journal_jremref(struct dirrem *, struct jremref *, 908 struct inodedep *); 909 static void cancel_jnewblk(struct jnewblk *, struct workhead *); 910 static int cancel_jaddref(struct jaddref *, struct inodedep *, 911 struct workhead *); 912 static void cancel_jfreefrag(struct jfreefrag *); 913 static inline void setup_freedirect(struct freeblks *, struct inode *, 914 int, int); 915 static inline void setup_freeext(struct freeblks *, struct inode *, int, int); 916 static inline void setup_freeindir(struct freeblks *, struct inode *, int, 917 ufs_lbn_t, int); 918 static inline struct freeblks *newfreeblks(struct mount *, struct inode *); 919 static void freeblks_free(struct ufsmount *, struct freeblks *, int); 920 static void indir_trunc(struct freework *, ufs2_daddr_t, ufs_lbn_t); 921 ufs2_daddr_t blkcount(struct fs *, ufs2_daddr_t, off_t); 922 static int trunc_check_buf(struct buf *, int *, ufs_lbn_t, int, int); 923 static void trunc_dependencies(struct inode *, struct freeblks *, ufs_lbn_t, 924 int, int); 925 static void trunc_pages(struct inode *, off_t, ufs2_daddr_t, int); 926 static int cancel_pagedep(struct pagedep *, struct freeblks *, int); 927 static int deallocate_dependencies(struct buf *, struct freeblks *, int); 928 static void newblk_freefrag(struct newblk*); 929 static void free_newblk(struct newblk *); 930 static void cancel_allocdirect(struct allocdirectlst *, 931 struct allocdirect *, struct freeblks *); 932 static int check_inode_unwritten(struct inodedep *); 933 static int free_inodedep(struct inodedep *); 934 static void freework_freeblock(struct freework *); 935 static void freework_enqueue(struct freework *); 936 static int handle_workitem_freeblocks(struct freeblks *, int); 937 static int handle_complete_freeblocks(struct freeblks *, int); 938 static void handle_workitem_indirblk(struct freework *); 939 static void handle_written_freework(struct freework *); 940 static void merge_inode_lists(struct allocdirectlst *,struct allocdirectlst *); 941 static struct worklist *jnewblk_merge(struct worklist *, struct worklist *, 942 struct workhead *); 943 static struct freefrag *setup_allocindir_phase2(struct buf *, struct inode *, 944 struct inodedep *, struct allocindir *, ufs_lbn_t); 945 static struct allocindir *newallocindir(struct inode *, int, ufs2_daddr_t, 946 ufs2_daddr_t, ufs_lbn_t); 947 static void handle_workitem_freefrag(struct freefrag *); 948 static struct freefrag *newfreefrag(struct inode *, ufs2_daddr_t, long, 949 ufs_lbn_t); 950 static void allocdirect_merge(struct allocdirectlst *, 951 struct allocdirect *, struct allocdirect *); 952 static struct freefrag *allocindir_merge(struct allocindir *, 953 struct allocindir *); 954 static int bmsafemap_find(struct bmsafemap_hashhead *, struct mount *, int, 955 struct bmsafemap **); 956 static struct bmsafemap *bmsafemap_lookup(struct mount *, struct buf *, 957 int cg, struct bmsafemap *); 958 static int newblk_find(struct newblk_hashhead *, struct mount *, ufs2_daddr_t, 959 int, struct newblk **); 960 static int newblk_lookup(struct mount *, ufs2_daddr_t, int, struct newblk **); 961 static int inodedep_find(struct inodedep_hashhead *, struct fs *, ino_t, 962 struct inodedep **); 963 static int inodedep_lookup(struct mount *, ino_t, int, struct inodedep **); 964 static int pagedep_lookup(struct mount *, struct buf *bp, ino_t, ufs_lbn_t, 965 int, struct pagedep **); 966 static int pagedep_find(struct pagedep_hashhead *, ino_t, ufs_lbn_t, 967 struct mount *mp, int, struct pagedep **); 968 static void pause_timer(void *); 969 static int request_cleanup(struct mount *, int); 970 static int process_worklist_item(struct mount *, int, int); 971 static void process_removes(struct vnode *); 972 static void process_truncates(struct vnode *); 973 static void jwork_move(struct workhead *, struct workhead *); 974 static void jwork_insert(struct workhead *, struct jsegdep *); 975 static void add_to_worklist(struct worklist *, int); 976 static void wake_worklist(struct worklist *); 977 static void wait_worklist(struct worklist *, char *); 978 static void remove_from_worklist(struct worklist *); 979 static void softdep_flush(void); 980 static void softdep_flushjournal(struct mount *); 981 static int softdep_speedup(void); 982 static void worklist_speedup(void); 983 static int journal_mount(struct mount *, struct fs *, struct ucred *); 984 static void journal_unmount(struct mount *); 985 static int journal_space(struct ufsmount *, int); 986 static void journal_suspend(struct ufsmount *); 987 static int journal_unsuspend(struct ufsmount *ump); 988 static void softdep_prelink(struct vnode *, struct vnode *); 989 static void add_to_journal(struct worklist *); 990 static void remove_from_journal(struct worklist *); 991 static void softdep_process_journal(struct mount *, struct worklist *, int); 992 static struct jremref *newjremref(struct dirrem *, struct inode *, 993 struct inode *ip, off_t, nlink_t); 994 static struct jaddref *newjaddref(struct inode *, ino_t, off_t, int16_t, 995 uint16_t); 996 static inline void newinoref(struct inoref *, ino_t, ino_t, off_t, nlink_t, 997 uint16_t); 998 static inline struct jsegdep *inoref_jseg(struct inoref *); 999 static struct jmvref *newjmvref(struct inode *, ino_t, off_t, off_t); 1000 static struct jfreeblk *newjfreeblk(struct freeblks *, ufs_lbn_t, 1001 ufs2_daddr_t, int); 1002 static struct jtrunc *newjtrunc(struct freeblks *, off_t, int); 1003 static void move_newblock_dep(struct jaddref *, struct inodedep *); 1004 static void cancel_jfreeblk(struct freeblks *, ufs2_daddr_t); 1005 static struct jfreefrag *newjfreefrag(struct freefrag *, struct inode *, 1006 ufs2_daddr_t, long, ufs_lbn_t); 1007 static struct freework *newfreework(struct ufsmount *, struct freeblks *, 1008 struct freework *, ufs_lbn_t, ufs2_daddr_t, int, int, int); 1009 static int jwait(struct worklist *, int); 1010 static struct inodedep *inodedep_lookup_ip(struct inode *); 1011 static int bmsafemap_backgroundwrite(struct bmsafemap *, struct buf *); 1012 static struct freefile *handle_bufwait(struct inodedep *, struct workhead *); 1013 static void handle_jwork(struct workhead *); 1014 static struct mkdir *setup_newdir(struct diradd *, ino_t, ino_t, struct buf *, 1015 struct mkdir **); 1016 static struct jblocks *jblocks_create(void); 1017 static ufs2_daddr_t jblocks_alloc(struct jblocks *, int, int *); 1018 static void jblocks_free(struct jblocks *, struct mount *, int); 1019 static void jblocks_destroy(struct jblocks *); 1020 static void jblocks_add(struct jblocks *, ufs2_daddr_t, int); 1021 1022 /* 1023 * Exported softdep operations. 1024 */ 1025 static void softdep_disk_io_initiation(struct buf *); 1026 static void softdep_disk_write_complete(struct buf *); 1027 static void softdep_deallocate_dependencies(struct buf *); 1028 static int softdep_count_dependencies(struct buf *bp, int); 1029 1030 static struct mtx lk; 1031 MTX_SYSINIT(softdep_lock, &lk, "Softdep Lock", MTX_DEF); 1032 1033 #define TRY_ACQUIRE_LOCK(lk) mtx_trylock(lk) 1034 #define ACQUIRE_LOCK(lk) mtx_lock(lk) 1035 #define FREE_LOCK(lk) mtx_unlock(lk) 1036 1037 #define BUF_AREC(bp) lockallowrecurse(&(bp)->b_lock) 1038 #define BUF_NOREC(bp) lockdisablerecurse(&(bp)->b_lock) 1039 1040 /* 1041 * Worklist queue management. 1042 * These routines require that the lock be held. 1043 */ 1044 #ifndef /* NOT */ DEBUG 1045 #define WORKLIST_INSERT(head, item) do { \ 1046 (item)->wk_state |= ONWORKLIST; \ 1047 LIST_INSERT_HEAD(head, item, wk_list); \ 1048 } while (0) 1049 #define WORKLIST_REMOVE(item) do { \ 1050 (item)->wk_state &= ~ONWORKLIST; \ 1051 LIST_REMOVE(item, wk_list); \ 1052 } while (0) 1053 #define WORKLIST_INSERT_UNLOCKED WORKLIST_INSERT 1054 #define WORKLIST_REMOVE_UNLOCKED WORKLIST_REMOVE 1055 1056 #else /* DEBUG */ 1057 static void worklist_insert(struct workhead *, struct worklist *, int); 1058 static void worklist_remove(struct worklist *, int); 1059 1060 #define WORKLIST_INSERT(head, item) worklist_insert(head, item, 1) 1061 #define WORKLIST_INSERT_UNLOCKED(head, item) worklist_insert(head, item, 0) 1062 #define WORKLIST_REMOVE(item) worklist_remove(item, 1) 1063 #define WORKLIST_REMOVE_UNLOCKED(item) worklist_remove(item, 0) 1064 1065 static void 1066 worklist_insert(head, item, locked) 1067 struct workhead *head; 1068 struct worklist *item; 1069 int locked; 1070 { 1071 1072 if (locked) 1073 mtx_assert(&lk, MA_OWNED); 1074 if (item->wk_state & ONWORKLIST) 1075 panic("worklist_insert: %p %s(0x%X) already on list", 1076 item, TYPENAME(item->wk_type), item->wk_state); 1077 item->wk_state |= ONWORKLIST; 1078 LIST_INSERT_HEAD(head, item, wk_list); 1079 } 1080 1081 static void 1082 worklist_remove(item, locked) 1083 struct worklist *item; 1084 int locked; 1085 { 1086 1087 if (locked) 1088 mtx_assert(&lk, MA_OWNED); 1089 if ((item->wk_state & ONWORKLIST) == 0) 1090 panic("worklist_remove: %p %s(0x%X) not on list", 1091 item, TYPENAME(item->wk_type), item->wk_state); 1092 item->wk_state &= ~ONWORKLIST; 1093 LIST_REMOVE(item, wk_list); 1094 } 1095 #endif /* DEBUG */ 1096 1097 /* 1098 * Merge two jsegdeps keeping only the oldest one as newer references 1099 * can't be discarded until after older references. 1100 */ 1101 static inline struct jsegdep * 1102 jsegdep_merge(struct jsegdep *one, struct jsegdep *two) 1103 { 1104 struct jsegdep *swp; 1105 1106 if (two == NULL) 1107 return (one); 1108 1109 if (one->jd_seg->js_seq > two->jd_seg->js_seq) { 1110 swp = one; 1111 one = two; 1112 two = swp; 1113 } 1114 WORKLIST_REMOVE(&two->jd_list); 1115 free_jsegdep(two); 1116 1117 return (one); 1118 } 1119 1120 /* 1121 * If two freedeps are compatible free one to reduce list size. 1122 */ 1123 static inline struct freedep * 1124 freedep_merge(struct freedep *one, struct freedep *two) 1125 { 1126 if (two == NULL) 1127 return (one); 1128 1129 if (one->fd_freework == two->fd_freework) { 1130 WORKLIST_REMOVE(&two->fd_list); 1131 free_freedep(two); 1132 } 1133 return (one); 1134 } 1135 1136 /* 1137 * Move journal work from one list to another. Duplicate freedeps and 1138 * jsegdeps are coalesced to keep the lists as small as possible. 1139 */ 1140 static void 1141 jwork_move(dst, src) 1142 struct workhead *dst; 1143 struct workhead *src; 1144 { 1145 struct freedep *freedep; 1146 struct jsegdep *jsegdep; 1147 struct worklist *wkn; 1148 struct worklist *wk; 1149 1150 KASSERT(dst != src, 1151 ("jwork_move: dst == src")); 1152 freedep = NULL; 1153 jsegdep = NULL; 1154 LIST_FOREACH_SAFE(wk, dst, wk_list, wkn) { 1155 if (wk->wk_type == D_JSEGDEP) 1156 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1157 if (wk->wk_type == D_FREEDEP) 1158 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1159 } 1160 1161 mtx_assert(&lk, MA_OWNED); 1162 while ((wk = LIST_FIRST(src)) != NULL) { 1163 WORKLIST_REMOVE(wk); 1164 WORKLIST_INSERT(dst, wk); 1165 if (wk->wk_type == D_JSEGDEP) { 1166 jsegdep = jsegdep_merge(WK_JSEGDEP(wk), jsegdep); 1167 continue; 1168 } 1169 if (wk->wk_type == D_FREEDEP) 1170 freedep = freedep_merge(WK_FREEDEP(wk), freedep); 1171 } 1172 } 1173 1174 static void 1175 jwork_insert(dst, jsegdep) 1176 struct workhead *dst; 1177 struct jsegdep *jsegdep; 1178 { 1179 struct jsegdep *jsegdepn; 1180 struct worklist *wk; 1181 1182 LIST_FOREACH(wk, dst, wk_list) 1183 if (wk->wk_type == D_JSEGDEP) 1184 break; 1185 if (wk == NULL) { 1186 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1187 return; 1188 } 1189 jsegdepn = WK_JSEGDEP(wk); 1190 if (jsegdep->jd_seg->js_seq < jsegdepn->jd_seg->js_seq) { 1191 WORKLIST_REMOVE(wk); 1192 free_jsegdep(jsegdepn); 1193 WORKLIST_INSERT(dst, &jsegdep->jd_list); 1194 } else 1195 free_jsegdep(jsegdep); 1196 } 1197 1198 /* 1199 * Routines for tracking and managing workitems. 1200 */ 1201 static void workitem_free(struct worklist *, int); 1202 static void workitem_alloc(struct worklist *, int, struct mount *); 1203 1204 #define WORKITEM_FREE(item, type) workitem_free((struct worklist *)(item), (type)) 1205 1206 static void 1207 workitem_free(item, type) 1208 struct worklist *item; 1209 int type; 1210 { 1211 struct ufsmount *ump; 1212 mtx_assert(&lk, MA_OWNED); 1213 1214 #ifdef DEBUG 1215 if (item->wk_state & ONWORKLIST) 1216 panic("workitem_free: %s(0x%X) still on list", 1217 TYPENAME(item->wk_type), item->wk_state); 1218 if (item->wk_type != type) 1219 panic("workitem_free: type mismatch %s != %s", 1220 TYPENAME(item->wk_type), TYPENAME(type)); 1221 #endif 1222 if (item->wk_state & IOWAITING) 1223 wakeup(item); 1224 ump = VFSTOUFS(item->wk_mp); 1225 if (--ump->softdep_deps == 0 && ump->softdep_req) 1226 wakeup(&ump->softdep_deps); 1227 dep_current[type]--; 1228 free(item, DtoM(type)); 1229 } 1230 1231 static void 1232 workitem_alloc(item, type, mp) 1233 struct worklist *item; 1234 int type; 1235 struct mount *mp; 1236 { 1237 struct ufsmount *ump; 1238 1239 item->wk_type = type; 1240 item->wk_mp = mp; 1241 item->wk_state = 0; 1242 1243 ump = VFSTOUFS(mp); 1244 ACQUIRE_LOCK(&lk); 1245 dep_current[type]++; 1246 dep_total[type]++; 1247 ump->softdep_deps++; 1248 ump->softdep_accdeps++; 1249 FREE_LOCK(&lk); 1250 } 1251 1252 /* 1253 * Workitem queue management 1254 */ 1255 static int max_softdeps; /* maximum number of structs before slowdown */ 1256 static int maxindirdeps = 50; /* max number of indirdeps before slowdown */ 1257 static int tickdelay = 2; /* number of ticks to pause during slowdown */ 1258 static int proc_waiting; /* tracks whether we have a timeout posted */ 1259 static int *stat_countp; /* statistic to count in proc_waiting timeout */ 1260 static struct callout softdep_callout; 1261 static int req_pending; 1262 static int req_clear_inodedeps; /* syncer process flush some inodedeps */ 1263 static int req_clear_remove; /* syncer process flush some freeblks */ 1264 static int softdep_flushcache = 0; /* Should we do BIO_FLUSH? */ 1265 1266 /* 1267 * runtime statistics 1268 */ 1269 static int stat_worklist_push; /* number of worklist cleanups */ 1270 static int stat_blk_limit_push; /* number of times block limit neared */ 1271 static int stat_ino_limit_push; /* number of times inode limit neared */ 1272 static int stat_blk_limit_hit; /* number of times block slowdown imposed */ 1273 static int stat_ino_limit_hit; /* number of times inode slowdown imposed */ 1274 static int stat_sync_limit_hit; /* number of synchronous slowdowns imposed */ 1275 static int stat_indir_blk_ptrs; /* bufs redirtied as indir ptrs not written */ 1276 static int stat_inode_bitmap; /* bufs redirtied as inode bitmap not written */ 1277 static int stat_direct_blk_ptrs;/* bufs redirtied as direct ptrs not written */ 1278 static int stat_dir_entry; /* bufs redirtied as dir entry cannot write */ 1279 static int stat_jaddref; /* bufs redirtied as ino bitmap can not write */ 1280 static int stat_jnewblk; /* bufs redirtied as blk bitmap can not write */ 1281 static int stat_journal_min; /* Times hit journal min threshold */ 1282 static int stat_journal_low; /* Times hit journal low threshold */ 1283 static int stat_journal_wait; /* Times blocked in jwait(). */ 1284 static int stat_jwait_filepage; /* Times blocked in jwait() for filepage. */ 1285 static int stat_jwait_freeblks; /* Times blocked in jwait() for freeblks. */ 1286 static int stat_jwait_inode; /* Times blocked in jwait() for inodes. */ 1287 static int stat_jwait_newblk; /* Times blocked in jwait() for newblks. */ 1288 static int stat_cleanup_high_delay; /* Maximum cleanup delay (in ticks) */ 1289 static int stat_cleanup_blkrequests; /* Number of block cleanup requests */ 1290 static int stat_cleanup_inorequests; /* Number of inode cleanup requests */ 1291 static int stat_cleanup_retries; /* Number of cleanups that needed to flush */ 1292 static int stat_cleanup_failures; /* Number of cleanup requests that failed */ 1293 1294 SYSCTL_INT(_debug_softdep, OID_AUTO, max_softdeps, CTLFLAG_RW, 1295 &max_softdeps, 0, ""); 1296 SYSCTL_INT(_debug_softdep, OID_AUTO, tickdelay, CTLFLAG_RW, 1297 &tickdelay, 0, ""); 1298 SYSCTL_INT(_debug_softdep, OID_AUTO, maxindirdeps, CTLFLAG_RW, 1299 &maxindirdeps, 0, ""); 1300 SYSCTL_INT(_debug_softdep, OID_AUTO, worklist_push, CTLFLAG_RW, 1301 &stat_worklist_push, 0,""); 1302 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_push, CTLFLAG_RW, 1303 &stat_blk_limit_push, 0,""); 1304 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_push, CTLFLAG_RW, 1305 &stat_ino_limit_push, 0,""); 1306 SYSCTL_INT(_debug_softdep, OID_AUTO, blk_limit_hit, CTLFLAG_RW, 1307 &stat_blk_limit_hit, 0, ""); 1308 SYSCTL_INT(_debug_softdep, OID_AUTO, ino_limit_hit, CTLFLAG_RW, 1309 &stat_ino_limit_hit, 0, ""); 1310 SYSCTL_INT(_debug_softdep, OID_AUTO, sync_limit_hit, CTLFLAG_RW, 1311 &stat_sync_limit_hit, 0, ""); 1312 SYSCTL_INT(_debug_softdep, OID_AUTO, indir_blk_ptrs, CTLFLAG_RW, 1313 &stat_indir_blk_ptrs, 0, ""); 1314 SYSCTL_INT(_debug_softdep, OID_AUTO, inode_bitmap, CTLFLAG_RW, 1315 &stat_inode_bitmap, 0, ""); 1316 SYSCTL_INT(_debug_softdep, OID_AUTO, direct_blk_ptrs, CTLFLAG_RW, 1317 &stat_direct_blk_ptrs, 0, ""); 1318 SYSCTL_INT(_debug_softdep, OID_AUTO, dir_entry, CTLFLAG_RW, 1319 &stat_dir_entry, 0, ""); 1320 SYSCTL_INT(_debug_softdep, OID_AUTO, jaddref_rollback, CTLFLAG_RW, 1321 &stat_jaddref, 0, ""); 1322 SYSCTL_INT(_debug_softdep, OID_AUTO, jnewblk_rollback, CTLFLAG_RW, 1323 &stat_jnewblk, 0, ""); 1324 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_low, CTLFLAG_RW, 1325 &stat_journal_low, 0, ""); 1326 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_min, CTLFLAG_RW, 1327 &stat_journal_min, 0, ""); 1328 SYSCTL_INT(_debug_softdep, OID_AUTO, journal_wait, CTLFLAG_RW, 1329 &stat_journal_wait, 0, ""); 1330 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_filepage, CTLFLAG_RW, 1331 &stat_jwait_filepage, 0, ""); 1332 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_freeblks, CTLFLAG_RW, 1333 &stat_jwait_freeblks, 0, ""); 1334 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_inode, CTLFLAG_RW, 1335 &stat_jwait_inode, 0, ""); 1336 SYSCTL_INT(_debug_softdep, OID_AUTO, jwait_newblk, CTLFLAG_RW, 1337 &stat_jwait_newblk, 0, ""); 1338 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_blkrequests, CTLFLAG_RW, 1339 &stat_cleanup_blkrequests, 0, ""); 1340 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_inorequests, CTLFLAG_RW, 1341 &stat_cleanup_inorequests, 0, ""); 1342 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_high_delay, CTLFLAG_RW, 1343 &stat_cleanup_high_delay, 0, ""); 1344 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_retries, CTLFLAG_RW, 1345 &stat_cleanup_retries, 0, ""); 1346 SYSCTL_INT(_debug_softdep, OID_AUTO, cleanup_failures, CTLFLAG_RW, 1347 &stat_cleanup_failures, 0, ""); 1348 SYSCTL_INT(_debug_softdep, OID_AUTO, flushcache, CTLFLAG_RW, 1349 &softdep_flushcache, 0, ""); 1350 1351 SYSCTL_DECL(_vfs_ffs); 1352 1353 LIST_HEAD(bmsafemap_hashhead, bmsafemap) *bmsafemap_hashtbl; 1354 static u_long bmsafemap_hash; /* size of hash table - 1 */ 1355 1356 static int compute_summary_at_mount = 0; /* Whether to recompute the summary at mount time */ 1357 SYSCTL_INT(_vfs_ffs, OID_AUTO, compute_summary_at_mount, CTLFLAG_RW, 1358 &compute_summary_at_mount, 0, "Recompute summary at mount"); 1359 1360 static struct proc *softdepproc; 1361 static struct kproc_desc softdep_kp = { 1362 "softdepflush", 1363 softdep_flush, 1364 &softdepproc 1365 }; 1366 SYSINIT(sdproc, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY, kproc_start, 1367 &softdep_kp); 1368 1369 static void 1370 softdep_flush(void) 1371 { 1372 struct mount *nmp; 1373 struct mount *mp; 1374 struct ufsmount *ump; 1375 struct thread *td; 1376 int remaining; 1377 int progress; 1378 1379 td = curthread; 1380 td->td_pflags |= TDP_NORUNNINGBUF; 1381 1382 for (;;) { 1383 kproc_suspend_check(softdepproc); 1384 ACQUIRE_LOCK(&lk); 1385 /* 1386 * If requested, try removing inode or removal dependencies. 1387 */ 1388 if (req_clear_inodedeps) { 1389 clear_inodedeps(); 1390 req_clear_inodedeps -= 1; 1391 wakeup_one(&proc_waiting); 1392 } 1393 if (req_clear_remove) { 1394 clear_remove(); 1395 req_clear_remove -= 1; 1396 wakeup_one(&proc_waiting); 1397 } 1398 FREE_LOCK(&lk); 1399 remaining = progress = 0; 1400 mtx_lock(&mountlist_mtx); 1401 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 1402 nmp = TAILQ_NEXT(mp, mnt_list); 1403 if (MOUNTEDSOFTDEP(mp) == 0) 1404 continue; 1405 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) 1406 continue; 1407 progress += softdep_process_worklist(mp, 0); 1408 ump = VFSTOUFS(mp); 1409 remaining += ump->softdep_on_worklist; 1410 mtx_lock(&mountlist_mtx); 1411 nmp = TAILQ_NEXT(mp, mnt_list); 1412 vfs_unbusy(mp); 1413 } 1414 mtx_unlock(&mountlist_mtx); 1415 if (remaining && progress) 1416 continue; 1417 ACQUIRE_LOCK(&lk); 1418 if (!req_pending) 1419 msleep(&req_pending, &lk, PVM, "sdflush", hz); 1420 req_pending = 0; 1421 FREE_LOCK(&lk); 1422 } 1423 } 1424 1425 static void 1426 worklist_speedup(void) 1427 { 1428 mtx_assert(&lk, MA_OWNED); 1429 if (req_pending == 0) { 1430 req_pending = 1; 1431 wakeup(&req_pending); 1432 } 1433 } 1434 1435 static int 1436 softdep_speedup(void) 1437 { 1438 1439 worklist_speedup(); 1440 bd_speedup(); 1441 return speedup_syncer(); 1442 } 1443 1444 /* 1445 * Add an item to the end of the work queue. 1446 * This routine requires that the lock be held. 1447 * This is the only routine that adds items to the list. 1448 * The following routine is the only one that removes items 1449 * and does so in order from first to last. 1450 */ 1451 1452 #define WK_HEAD 0x0001 /* Add to HEAD. */ 1453 #define WK_NODELAY 0x0002 /* Process immediately. */ 1454 1455 static void 1456 add_to_worklist(wk, flags) 1457 struct worklist *wk; 1458 int flags; 1459 { 1460 struct ufsmount *ump; 1461 1462 mtx_assert(&lk, MA_OWNED); 1463 ump = VFSTOUFS(wk->wk_mp); 1464 if (wk->wk_state & ONWORKLIST) 1465 panic("add_to_worklist: %s(0x%X) already on list", 1466 TYPENAME(wk->wk_type), wk->wk_state); 1467 wk->wk_state |= ONWORKLIST; 1468 if (ump->softdep_on_worklist == 0) { 1469 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1470 ump->softdep_worklist_tail = wk; 1471 } else if (flags & WK_HEAD) { 1472 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, wk, wk_list); 1473 } else { 1474 LIST_INSERT_AFTER(ump->softdep_worklist_tail, wk, wk_list); 1475 ump->softdep_worklist_tail = wk; 1476 } 1477 ump->softdep_on_worklist += 1; 1478 if (flags & WK_NODELAY) 1479 worklist_speedup(); 1480 } 1481 1482 /* 1483 * Remove the item to be processed. If we are removing the last 1484 * item on the list, we need to recalculate the tail pointer. 1485 */ 1486 static void 1487 remove_from_worklist(wk) 1488 struct worklist *wk; 1489 { 1490 struct ufsmount *ump; 1491 1492 ump = VFSTOUFS(wk->wk_mp); 1493 WORKLIST_REMOVE(wk); 1494 if (ump->softdep_worklist_tail == wk) 1495 ump->softdep_worklist_tail = 1496 (struct worklist *)wk->wk_list.le_prev; 1497 ump->softdep_on_worklist -= 1; 1498 } 1499 1500 static void 1501 wake_worklist(wk) 1502 struct worklist *wk; 1503 { 1504 if (wk->wk_state & IOWAITING) { 1505 wk->wk_state &= ~IOWAITING; 1506 wakeup(wk); 1507 } 1508 } 1509 1510 static void 1511 wait_worklist(wk, wmesg) 1512 struct worklist *wk; 1513 char *wmesg; 1514 { 1515 1516 wk->wk_state |= IOWAITING; 1517 msleep(wk, &lk, PVM, wmesg, 0); 1518 } 1519 1520 /* 1521 * Process that runs once per second to handle items in the background queue. 1522 * 1523 * Note that we ensure that everything is done in the order in which they 1524 * appear in the queue. The code below depends on this property to ensure 1525 * that blocks of a file are freed before the inode itself is freed. This 1526 * ordering ensures that no new <vfsid, inum, lbn> triples will be generated 1527 * until all the old ones have been purged from the dependency lists. 1528 */ 1529 int 1530 softdep_process_worklist(mp, full) 1531 struct mount *mp; 1532 int full; 1533 { 1534 int cnt, matchcnt; 1535 struct ufsmount *ump; 1536 long starttime; 1537 1538 KASSERT(mp != NULL, ("softdep_process_worklist: NULL mp")); 1539 /* 1540 * Record the process identifier of our caller so that we can give 1541 * this process preferential treatment in request_cleanup below. 1542 */ 1543 matchcnt = 0; 1544 ump = VFSTOUFS(mp); 1545 ACQUIRE_LOCK(&lk); 1546 starttime = time_second; 1547 softdep_process_journal(mp, NULL, full?MNT_WAIT:0); 1548 while (ump->softdep_on_worklist > 0) { 1549 if ((cnt = process_worklist_item(mp, 10, LK_NOWAIT)) == 0) 1550 break; 1551 else 1552 matchcnt += cnt; 1553 /* 1554 * If requested, try removing inode or removal dependencies. 1555 */ 1556 if (req_clear_inodedeps) { 1557 clear_inodedeps(); 1558 req_clear_inodedeps -= 1; 1559 wakeup_one(&proc_waiting); 1560 } 1561 if (req_clear_remove) { 1562 clear_remove(); 1563 req_clear_remove -= 1; 1564 wakeup_one(&proc_waiting); 1565 } 1566 /* 1567 * We do not generally want to stop for buffer space, but if 1568 * we are really being a buffer hog, we will stop and wait. 1569 */ 1570 if (should_yield()) { 1571 FREE_LOCK(&lk); 1572 kern_yield(PRI_USER); 1573 bwillwrite(); 1574 ACQUIRE_LOCK(&lk); 1575 } 1576 /* 1577 * Never allow processing to run for more than one 1578 * second. Otherwise the other mountpoints may get 1579 * excessively backlogged. 1580 */ 1581 if (!full && starttime != time_second) 1582 break; 1583 } 1584 if (full == 0) 1585 journal_unsuspend(ump); 1586 FREE_LOCK(&lk); 1587 return (matchcnt); 1588 } 1589 1590 /* 1591 * Process all removes associated with a vnode if we are running out of 1592 * journal space. Any other process which attempts to flush these will 1593 * be unable as we have the vnodes locked. 1594 */ 1595 static void 1596 process_removes(vp) 1597 struct vnode *vp; 1598 { 1599 struct inodedep *inodedep; 1600 struct dirrem *dirrem; 1601 struct mount *mp; 1602 ino_t inum; 1603 1604 mtx_assert(&lk, MA_OWNED); 1605 1606 mp = vp->v_mount; 1607 inum = VTOI(vp)->i_number; 1608 for (;;) { 1609 top: 1610 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1611 return; 1612 LIST_FOREACH(dirrem, &inodedep->id_dirremhd, dm_inonext) { 1613 /* 1614 * If another thread is trying to lock this vnode 1615 * it will fail but we must wait for it to do so 1616 * before we can proceed. 1617 */ 1618 if (dirrem->dm_state & INPROGRESS) { 1619 wait_worklist(&dirrem->dm_list, "pwrwait"); 1620 goto top; 1621 } 1622 if ((dirrem->dm_state & (COMPLETE | ONWORKLIST)) == 1623 (COMPLETE | ONWORKLIST)) 1624 break; 1625 } 1626 if (dirrem == NULL) 1627 return; 1628 remove_from_worklist(&dirrem->dm_list); 1629 FREE_LOCK(&lk); 1630 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1631 panic("process_removes: suspended filesystem"); 1632 handle_workitem_remove(dirrem, 0); 1633 vn_finished_secondary_write(mp); 1634 ACQUIRE_LOCK(&lk); 1635 } 1636 } 1637 1638 /* 1639 * Process all truncations associated with a vnode if we are running out 1640 * of journal space. This is called when the vnode lock is already held 1641 * and no other process can clear the truncation. This function returns 1642 * a value greater than zero if it did any work. 1643 */ 1644 static void 1645 process_truncates(vp) 1646 struct vnode *vp; 1647 { 1648 struct inodedep *inodedep; 1649 struct freeblks *freeblks; 1650 struct mount *mp; 1651 ino_t inum; 1652 int cgwait; 1653 1654 mtx_assert(&lk, MA_OWNED); 1655 1656 mp = vp->v_mount; 1657 inum = VTOI(vp)->i_number; 1658 for (;;) { 1659 if (inodedep_lookup(mp, inum, 0, &inodedep) == 0) 1660 return; 1661 cgwait = 0; 1662 TAILQ_FOREACH(freeblks, &inodedep->id_freeblklst, fb_next) { 1663 /* Journal entries not yet written. */ 1664 if (!LIST_EMPTY(&freeblks->fb_jblkdephd)) { 1665 jwait(&LIST_FIRST( 1666 &freeblks->fb_jblkdephd)->jb_list, 1667 MNT_WAIT); 1668 break; 1669 } 1670 /* Another thread is executing this item. */ 1671 if (freeblks->fb_state & INPROGRESS) { 1672 wait_worklist(&freeblks->fb_list, "ptrwait"); 1673 break; 1674 } 1675 /* Freeblks is waiting on a inode write. */ 1676 if ((freeblks->fb_state & COMPLETE) == 0) { 1677 FREE_LOCK(&lk); 1678 ffs_update(vp, 1); 1679 ACQUIRE_LOCK(&lk); 1680 break; 1681 } 1682 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST)) == 1683 (ALLCOMPLETE | ONWORKLIST)) { 1684 remove_from_worklist(&freeblks->fb_list); 1685 freeblks->fb_state |= INPROGRESS; 1686 FREE_LOCK(&lk); 1687 if (vn_start_secondary_write(NULL, &mp, 1688 V_NOWAIT)) 1689 panic("process_truncates: " 1690 "suspended filesystem"); 1691 handle_workitem_freeblocks(freeblks, 0); 1692 vn_finished_secondary_write(mp); 1693 ACQUIRE_LOCK(&lk); 1694 break; 1695 } 1696 if (freeblks->fb_cgwait) 1697 cgwait++; 1698 } 1699 if (cgwait) { 1700 FREE_LOCK(&lk); 1701 sync_cgs(mp, MNT_WAIT); 1702 ffs_sync_snap(mp, MNT_WAIT); 1703 ACQUIRE_LOCK(&lk); 1704 continue; 1705 } 1706 if (freeblks == NULL) 1707 break; 1708 } 1709 return; 1710 } 1711 1712 /* 1713 * Process one item on the worklist. 1714 */ 1715 static int 1716 process_worklist_item(mp, target, flags) 1717 struct mount *mp; 1718 int target; 1719 int flags; 1720 { 1721 struct worklist sintenel; 1722 struct worklist *wk; 1723 struct ufsmount *ump; 1724 int matchcnt; 1725 int error; 1726 1727 mtx_assert(&lk, MA_OWNED); 1728 KASSERT(mp != NULL, ("process_worklist_item: NULL mp")); 1729 /* 1730 * If we are being called because of a process doing a 1731 * copy-on-write, then it is not safe to write as we may 1732 * recurse into the copy-on-write routine. 1733 */ 1734 if (curthread->td_pflags & TDP_COWINPROGRESS) 1735 return (-1); 1736 PHOLD(curproc); /* Don't let the stack go away. */ 1737 ump = VFSTOUFS(mp); 1738 matchcnt = 0; 1739 sintenel.wk_mp = NULL; 1740 sintenel.wk_type = D_SENTINAL; 1741 LIST_INSERT_HEAD(&ump->softdep_workitem_pending, &sintenel, wk_list); 1742 for (wk = LIST_NEXT(&sintenel, wk_list); wk != NULL; 1743 wk = LIST_NEXT(&sintenel, wk_list)) { 1744 if (wk->wk_type == D_SENTINAL) { 1745 LIST_REMOVE(&sintenel, wk_list); 1746 LIST_INSERT_AFTER(wk, &sintenel, wk_list); 1747 continue; 1748 } 1749 if (wk->wk_state & INPROGRESS) 1750 panic("process_worklist_item: %p already in progress.", 1751 wk); 1752 wk->wk_state |= INPROGRESS; 1753 remove_from_worklist(wk); 1754 FREE_LOCK(&lk); 1755 if (vn_start_secondary_write(NULL, &mp, V_NOWAIT)) 1756 panic("process_worklist_item: suspended filesystem"); 1757 switch (wk->wk_type) { 1758 case D_DIRREM: 1759 /* removal of a directory entry */ 1760 error = handle_workitem_remove(WK_DIRREM(wk), flags); 1761 break; 1762 1763 case D_FREEBLKS: 1764 /* releasing blocks and/or fragments from a file */ 1765 error = handle_workitem_freeblocks(WK_FREEBLKS(wk), 1766 flags); 1767 break; 1768 1769 case D_FREEFRAG: 1770 /* releasing a fragment when replaced as a file grows */ 1771 handle_workitem_freefrag(WK_FREEFRAG(wk)); 1772 error = 0; 1773 break; 1774 1775 case D_FREEFILE: 1776 /* releasing an inode when its link count drops to 0 */ 1777 handle_workitem_freefile(WK_FREEFILE(wk)); 1778 error = 0; 1779 break; 1780 1781 default: 1782 panic("%s_process_worklist: Unknown type %s", 1783 "softdep", TYPENAME(wk->wk_type)); 1784 /* NOTREACHED */ 1785 } 1786 vn_finished_secondary_write(mp); 1787 ACQUIRE_LOCK(&lk); 1788 if (error == 0) { 1789 if (++matchcnt == target) 1790 break; 1791 continue; 1792 } 1793 /* 1794 * We have to retry the worklist item later. Wake up any 1795 * waiters who may be able to complete it immediately and 1796 * add the item back to the head so we don't try to execute 1797 * it again. 1798 */ 1799 wk->wk_state &= ~INPROGRESS; 1800 wake_worklist(wk); 1801 add_to_worklist(wk, WK_HEAD); 1802 } 1803 LIST_REMOVE(&sintenel, wk_list); 1804 /* Sentinal could've become the tail from remove_from_worklist. */ 1805 if (ump->softdep_worklist_tail == &sintenel) 1806 ump->softdep_worklist_tail = 1807 (struct worklist *)sintenel.wk_list.le_prev; 1808 PRELE(curproc); 1809 return (matchcnt); 1810 } 1811 1812 /* 1813 * Move dependencies from one buffer to another. 1814 */ 1815 int 1816 softdep_move_dependencies(oldbp, newbp) 1817 struct buf *oldbp; 1818 struct buf *newbp; 1819 { 1820 struct worklist *wk, *wktail; 1821 int dirty; 1822 1823 dirty = 0; 1824 wktail = NULL; 1825 ACQUIRE_LOCK(&lk); 1826 while ((wk = LIST_FIRST(&oldbp->b_dep)) != NULL) { 1827 LIST_REMOVE(wk, wk_list); 1828 if (wk->wk_type == D_BMSAFEMAP && 1829 bmsafemap_backgroundwrite(WK_BMSAFEMAP(wk), newbp)) 1830 dirty = 1; 1831 if (wktail == 0) 1832 LIST_INSERT_HEAD(&newbp->b_dep, wk, wk_list); 1833 else 1834 LIST_INSERT_AFTER(wktail, wk, wk_list); 1835 wktail = wk; 1836 } 1837 FREE_LOCK(&lk); 1838 1839 return (dirty); 1840 } 1841 1842 /* 1843 * Purge the work list of all items associated with a particular mount point. 1844 */ 1845 int 1846 softdep_flushworklist(oldmnt, countp, td) 1847 struct mount *oldmnt; 1848 int *countp; 1849 struct thread *td; 1850 { 1851 struct vnode *devvp; 1852 int count, error = 0; 1853 struct ufsmount *ump; 1854 1855 /* 1856 * Alternately flush the block device associated with the mount 1857 * point and process any dependencies that the flushing 1858 * creates. We continue until no more worklist dependencies 1859 * are found. 1860 */ 1861 *countp = 0; 1862 ump = VFSTOUFS(oldmnt); 1863 devvp = ump->um_devvp; 1864 while ((count = softdep_process_worklist(oldmnt, 1)) > 0) { 1865 *countp += count; 1866 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1867 error = VOP_FSYNC(devvp, MNT_WAIT, td); 1868 VOP_UNLOCK(devvp, 0); 1869 if (error) 1870 break; 1871 } 1872 return (error); 1873 } 1874 1875 int 1876 softdep_waitidle(struct mount *mp) 1877 { 1878 struct ufsmount *ump; 1879 int error; 1880 int i; 1881 1882 ump = VFSTOUFS(mp); 1883 ACQUIRE_LOCK(&lk); 1884 for (i = 0; i < 10 && ump->softdep_deps; i++) { 1885 ump->softdep_req = 1; 1886 if (ump->softdep_on_worklist) 1887 panic("softdep_waitidle: work added after flush."); 1888 msleep(&ump->softdep_deps, &lk, PVM, "softdeps", 1); 1889 } 1890 ump->softdep_req = 0; 1891 FREE_LOCK(&lk); 1892 error = 0; 1893 if (i == 10) { 1894 error = EBUSY; 1895 printf("softdep_waitidle: Failed to flush worklist for %p\n", 1896 mp); 1897 } 1898 1899 return (error); 1900 } 1901 1902 /* 1903 * Flush all vnodes and worklist items associated with a specified mount point. 1904 */ 1905 int 1906 softdep_flushfiles(oldmnt, flags, td) 1907 struct mount *oldmnt; 1908 int flags; 1909 struct thread *td; 1910 { 1911 #ifdef QUOTA 1912 struct ufsmount *ump; 1913 int i; 1914 #endif 1915 int error, early, depcount, loopcnt, retry_flush_count, retry; 1916 int morework; 1917 1918 loopcnt = 10; 1919 retry_flush_count = 3; 1920 retry_flush: 1921 error = 0; 1922 1923 /* 1924 * Alternately flush the vnodes associated with the mount 1925 * point and process any dependencies that the flushing 1926 * creates. In theory, this loop can happen at most twice, 1927 * but we give it a few extra just to be sure. 1928 */ 1929 for (; loopcnt > 0; loopcnt--) { 1930 /* 1931 * Do another flush in case any vnodes were brought in 1932 * as part of the cleanup operations. 1933 */ 1934 early = retry_flush_count == 1 || (oldmnt->mnt_kern_flag & 1935 MNTK_UNMOUNT) == 0 ? 0 : EARLYFLUSH; 1936 if ((error = ffs_flushfiles(oldmnt, flags | early, td)) != 0) 1937 break; 1938 if ((error = softdep_flushworklist(oldmnt, &depcount, td)) != 0 || 1939 depcount == 0) 1940 break; 1941 } 1942 /* 1943 * If we are unmounting then it is an error to fail. If we 1944 * are simply trying to downgrade to read-only, then filesystem 1945 * activity can keep us busy forever, so we just fail with EBUSY. 1946 */ 1947 if (loopcnt == 0) { 1948 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) 1949 panic("softdep_flushfiles: looping"); 1950 error = EBUSY; 1951 } 1952 if (!error) 1953 error = softdep_waitidle(oldmnt); 1954 if (!error) { 1955 if (oldmnt->mnt_kern_flag & MNTK_UNMOUNT) { 1956 retry = 0; 1957 MNT_ILOCK(oldmnt); 1958 KASSERT((oldmnt->mnt_kern_flag & MNTK_NOINSMNTQ) != 0, 1959 ("softdep_flushfiles: !MNTK_NOINSMNTQ")); 1960 morework = oldmnt->mnt_nvnodelistsize > 0; 1961 #ifdef QUOTA 1962 ump = VFSTOUFS(oldmnt); 1963 UFS_LOCK(ump); 1964 for (i = 0; i < MAXQUOTAS; i++) { 1965 if (ump->um_quotas[i] != NULLVP) 1966 morework = 1; 1967 } 1968 UFS_UNLOCK(ump); 1969 #endif 1970 if (morework) { 1971 if (--retry_flush_count > 0) { 1972 retry = 1; 1973 loopcnt = 3; 1974 } else 1975 error = EBUSY; 1976 } 1977 MNT_IUNLOCK(oldmnt); 1978 if (retry) 1979 goto retry_flush; 1980 } 1981 } 1982 return (error); 1983 } 1984 1985 /* 1986 * Structure hashing. 1987 * 1988 * There are three types of structures that can be looked up: 1989 * 1) pagedep structures identified by mount point, inode number, 1990 * and logical block. 1991 * 2) inodedep structures identified by mount point and inode number. 1992 * 3) newblk structures identified by mount point and 1993 * physical block number. 1994 * 1995 * The "pagedep" and "inodedep" dependency structures are hashed 1996 * separately from the file blocks and inodes to which they correspond. 1997 * This separation helps when the in-memory copy of an inode or 1998 * file block must be replaced. It also obviates the need to access 1999 * an inode or file page when simply updating (or de-allocating) 2000 * dependency structures. Lookup of newblk structures is needed to 2001 * find newly allocated blocks when trying to associate them with 2002 * their allocdirect or allocindir structure. 2003 * 2004 * The lookup routines optionally create and hash a new instance when 2005 * an existing entry is not found. 2006 */ 2007 #define DEPALLOC 0x0001 /* allocate structure if lookup fails */ 2008 #define NODELAY 0x0002 /* cannot do background work */ 2009 2010 /* 2011 * Structures and routines associated with pagedep caching. 2012 */ 2013 LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl; 2014 u_long pagedep_hash; /* size of hash table - 1 */ 2015 #define PAGEDEP_HASH(mp, inum, lbn) \ 2016 (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \ 2017 pagedep_hash]) 2018 2019 static int 2020 pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp) 2021 struct pagedep_hashhead *pagedephd; 2022 ino_t ino; 2023 ufs_lbn_t lbn; 2024 struct mount *mp; 2025 int flags; 2026 struct pagedep **pagedeppp; 2027 { 2028 struct pagedep *pagedep; 2029 2030 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 2031 if (ino == pagedep->pd_ino && lbn == pagedep->pd_lbn && 2032 mp == pagedep->pd_list.wk_mp) { 2033 *pagedeppp = pagedep; 2034 return (1); 2035 } 2036 } 2037 *pagedeppp = NULL; 2038 return (0); 2039 } 2040 /* 2041 * Look up a pagedep. Return 1 if found, 0 otherwise. 2042 * If not found, allocate if DEPALLOC flag is passed. 2043 * Found or allocated entry is returned in pagedeppp. 2044 * This routine must be called with splbio interrupts blocked. 2045 */ 2046 static int 2047 pagedep_lookup(mp, bp, ino, lbn, flags, pagedeppp) 2048 struct mount *mp; 2049 struct buf *bp; 2050 ino_t ino; 2051 ufs_lbn_t lbn; 2052 int flags; 2053 struct pagedep **pagedeppp; 2054 { 2055 struct pagedep *pagedep; 2056 struct pagedep_hashhead *pagedephd; 2057 struct worklist *wk; 2058 int ret; 2059 int i; 2060 2061 mtx_assert(&lk, MA_OWNED); 2062 if (bp) { 2063 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 2064 if (wk->wk_type == D_PAGEDEP) { 2065 *pagedeppp = WK_PAGEDEP(wk); 2066 return (1); 2067 } 2068 } 2069 } 2070 pagedephd = PAGEDEP_HASH(mp, ino, lbn); 2071 ret = pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp); 2072 if (ret) { 2073 if (((*pagedeppp)->pd_state & ONWORKLIST) == 0 && bp) 2074 WORKLIST_INSERT(&bp->b_dep, &(*pagedeppp)->pd_list); 2075 return (1); 2076 } 2077 if ((flags & DEPALLOC) == 0) 2078 return (0); 2079 FREE_LOCK(&lk); 2080 pagedep = malloc(sizeof(struct pagedep), 2081 M_PAGEDEP, M_SOFTDEP_FLAGS|M_ZERO); 2082 workitem_alloc(&pagedep->pd_list, D_PAGEDEP, mp); 2083 ACQUIRE_LOCK(&lk); 2084 ret = pagedep_find(pagedephd, ino, lbn, mp, flags, pagedeppp); 2085 if (*pagedeppp) { 2086 /* 2087 * This should never happen since we only create pagedeps 2088 * with the vnode lock held. Could be an assert. 2089 */ 2090 WORKITEM_FREE(pagedep, D_PAGEDEP); 2091 return (ret); 2092 } 2093 pagedep->pd_ino = ino; 2094 pagedep->pd_lbn = lbn; 2095 LIST_INIT(&pagedep->pd_dirremhd); 2096 LIST_INIT(&pagedep->pd_pendinghd); 2097 for (i = 0; i < DAHASHSZ; i++) 2098 LIST_INIT(&pagedep->pd_diraddhd[i]); 2099 LIST_INSERT_HEAD(pagedephd, pagedep, pd_hash); 2100 WORKLIST_INSERT(&bp->b_dep, &pagedep->pd_list); 2101 *pagedeppp = pagedep; 2102 return (0); 2103 } 2104 2105 /* 2106 * Structures and routines associated with inodedep caching. 2107 */ 2108 LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl; 2109 static u_long inodedep_hash; /* size of hash table - 1 */ 2110 #define INODEDEP_HASH(fs, inum) \ 2111 (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & inodedep_hash]) 2112 2113 static int 2114 inodedep_find(inodedephd, fs, inum, inodedeppp) 2115 struct inodedep_hashhead *inodedephd; 2116 struct fs *fs; 2117 ino_t inum; 2118 struct inodedep **inodedeppp; 2119 { 2120 struct inodedep *inodedep; 2121 2122 LIST_FOREACH(inodedep, inodedephd, id_hash) 2123 if (inum == inodedep->id_ino && fs == inodedep->id_fs) 2124 break; 2125 if (inodedep) { 2126 *inodedeppp = inodedep; 2127 return (1); 2128 } 2129 *inodedeppp = NULL; 2130 2131 return (0); 2132 } 2133 /* 2134 * Look up an inodedep. Return 1 if found, 0 if not found. 2135 * If not found, allocate if DEPALLOC flag is passed. 2136 * Found or allocated entry is returned in inodedeppp. 2137 * This routine must be called with splbio interrupts blocked. 2138 */ 2139 static int 2140 inodedep_lookup(mp, inum, flags, inodedeppp) 2141 struct mount *mp; 2142 ino_t inum; 2143 int flags; 2144 struct inodedep **inodedeppp; 2145 { 2146 struct inodedep *inodedep; 2147 struct inodedep_hashhead *inodedephd; 2148 struct fs *fs; 2149 2150 mtx_assert(&lk, MA_OWNED); 2151 fs = VFSTOUFS(mp)->um_fs; 2152 inodedephd = INODEDEP_HASH(fs, inum); 2153 2154 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) 2155 return (1); 2156 if ((flags & DEPALLOC) == 0) 2157 return (0); 2158 /* 2159 * If we are over our limit, try to improve the situation. 2160 */ 2161 if (dep_current[D_INODEDEP] > max_softdeps && (flags & NODELAY) == 0) 2162 request_cleanup(mp, FLUSH_INODES); 2163 FREE_LOCK(&lk); 2164 inodedep = malloc(sizeof(struct inodedep), 2165 M_INODEDEP, M_SOFTDEP_FLAGS); 2166 workitem_alloc(&inodedep->id_list, D_INODEDEP, mp); 2167 ACQUIRE_LOCK(&lk); 2168 if (inodedep_find(inodedephd, fs, inum, inodedeppp)) { 2169 WORKITEM_FREE(inodedep, D_INODEDEP); 2170 return (1); 2171 } 2172 inodedep->id_fs = fs; 2173 inodedep->id_ino = inum; 2174 inodedep->id_state = ALLCOMPLETE; 2175 inodedep->id_nlinkdelta = 0; 2176 inodedep->id_savedino1 = NULL; 2177 inodedep->id_savedsize = -1; 2178 inodedep->id_savedextsize = -1; 2179 inodedep->id_savednlink = -1; 2180 inodedep->id_bmsafemap = NULL; 2181 inodedep->id_mkdiradd = NULL; 2182 LIST_INIT(&inodedep->id_dirremhd); 2183 LIST_INIT(&inodedep->id_pendinghd); 2184 LIST_INIT(&inodedep->id_inowait); 2185 LIST_INIT(&inodedep->id_bufwait); 2186 TAILQ_INIT(&inodedep->id_inoreflst); 2187 TAILQ_INIT(&inodedep->id_inoupdt); 2188 TAILQ_INIT(&inodedep->id_newinoupdt); 2189 TAILQ_INIT(&inodedep->id_extupdt); 2190 TAILQ_INIT(&inodedep->id_newextupdt); 2191 TAILQ_INIT(&inodedep->id_freeblklst); 2192 LIST_INSERT_HEAD(inodedephd, inodedep, id_hash); 2193 *inodedeppp = inodedep; 2194 return (0); 2195 } 2196 2197 /* 2198 * Structures and routines associated with newblk caching. 2199 */ 2200 LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl; 2201 u_long newblk_hash; /* size of hash table - 1 */ 2202 #define NEWBLK_HASH(fs, inum) \ 2203 (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash]) 2204 2205 static int 2206 newblk_find(newblkhd, mp, newblkno, flags, newblkpp) 2207 struct newblk_hashhead *newblkhd; 2208 struct mount *mp; 2209 ufs2_daddr_t newblkno; 2210 int flags; 2211 struct newblk **newblkpp; 2212 { 2213 struct newblk *newblk; 2214 2215 LIST_FOREACH(newblk, newblkhd, nb_hash) { 2216 if (newblkno != newblk->nb_newblkno) 2217 continue; 2218 if (mp != newblk->nb_list.wk_mp) 2219 continue; 2220 /* 2221 * If we're creating a new dependency don't match those that 2222 * have already been converted to allocdirects. This is for 2223 * a frag extend. 2224 */ 2225 if ((flags & DEPALLOC) && newblk->nb_list.wk_type != D_NEWBLK) 2226 continue; 2227 break; 2228 } 2229 if (newblk) { 2230 *newblkpp = newblk; 2231 return (1); 2232 } 2233 *newblkpp = NULL; 2234 return (0); 2235 } 2236 2237 /* 2238 * Look up a newblk. Return 1 if found, 0 if not found. 2239 * If not found, allocate if DEPALLOC flag is passed. 2240 * Found or allocated entry is returned in newblkpp. 2241 */ 2242 static int 2243 newblk_lookup(mp, newblkno, flags, newblkpp) 2244 struct mount *mp; 2245 ufs2_daddr_t newblkno; 2246 int flags; 2247 struct newblk **newblkpp; 2248 { 2249 struct newblk *newblk; 2250 struct newblk_hashhead *newblkhd; 2251 2252 newblkhd = NEWBLK_HASH(VFSTOUFS(mp)->um_fs, newblkno); 2253 if (newblk_find(newblkhd, mp, newblkno, flags, newblkpp)) 2254 return (1); 2255 if ((flags & DEPALLOC) == 0) 2256 return (0); 2257 FREE_LOCK(&lk); 2258 newblk = malloc(sizeof(union allblk), M_NEWBLK, 2259 M_SOFTDEP_FLAGS | M_ZERO); 2260 workitem_alloc(&newblk->nb_list, D_NEWBLK, mp); 2261 ACQUIRE_LOCK(&lk); 2262 if (newblk_find(newblkhd, mp, newblkno, flags, newblkpp)) { 2263 WORKITEM_FREE(newblk, D_NEWBLK); 2264 return (1); 2265 } 2266 newblk->nb_freefrag = NULL; 2267 LIST_INIT(&newblk->nb_indirdeps); 2268 LIST_INIT(&newblk->nb_newdirblk); 2269 LIST_INIT(&newblk->nb_jwork); 2270 newblk->nb_state = ATTACHED; 2271 newblk->nb_newblkno = newblkno; 2272 LIST_INSERT_HEAD(newblkhd, newblk, nb_hash); 2273 *newblkpp = newblk; 2274 return (0); 2275 } 2276 2277 /* 2278 * Structures and routines associated with freed indirect block caching. 2279 */ 2280 struct freeworklst *indir_hashtbl; 2281 u_long indir_hash; /* size of hash table - 1 */ 2282 #define INDIR_HASH(mp, blkno) \ 2283 (&indir_hashtbl[((((register_t)(mp)) >> 13) + (blkno)) & indir_hash]) 2284 2285 /* 2286 * Lookup an indirect block in the indir hash table. The freework is 2287 * removed and potentially freed. The caller must do a blocking journal 2288 * write before writing to the blkno. 2289 */ 2290 static int 2291 indirblk_lookup(mp, blkno) 2292 struct mount *mp; 2293 ufs2_daddr_t blkno; 2294 { 2295 struct freework *freework; 2296 struct freeworklst *wkhd; 2297 2298 wkhd = INDIR_HASH(mp, blkno); 2299 TAILQ_FOREACH(freework, wkhd, fw_next) { 2300 if (freework->fw_blkno != blkno) 2301 continue; 2302 if (freework->fw_list.wk_mp != mp) 2303 continue; 2304 indirblk_remove(freework); 2305 return (1); 2306 } 2307 return (0); 2308 } 2309 2310 /* 2311 * Insert an indirect block represented by freework into the indirblk 2312 * hash table so that it may prevent the block from being re-used prior 2313 * to the journal being written. 2314 */ 2315 static void 2316 indirblk_insert(freework) 2317 struct freework *freework; 2318 { 2319 struct jblocks *jblocks; 2320 struct jseg *jseg; 2321 2322 jblocks = VFSTOUFS(freework->fw_list.wk_mp)->softdep_jblocks; 2323 jseg = TAILQ_LAST(&jblocks->jb_segs, jseglst); 2324 if (jseg == NULL) 2325 return; 2326 2327 LIST_INSERT_HEAD(&jseg->js_indirs, freework, fw_segs); 2328 TAILQ_INSERT_HEAD(INDIR_HASH(freework->fw_list.wk_mp, 2329 freework->fw_blkno), freework, fw_next); 2330 freework->fw_state &= ~DEPCOMPLETE; 2331 } 2332 2333 static void 2334 indirblk_remove(freework) 2335 struct freework *freework; 2336 { 2337 2338 LIST_REMOVE(freework, fw_segs); 2339 TAILQ_REMOVE(INDIR_HASH(freework->fw_list.wk_mp, 2340 freework->fw_blkno), freework, fw_next); 2341 freework->fw_state |= DEPCOMPLETE; 2342 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 2343 WORKITEM_FREE(freework, D_FREEWORK); 2344 } 2345 2346 /* 2347 * Executed during filesystem system initialization before 2348 * mounting any filesystems. 2349 */ 2350 void 2351 softdep_initialize() 2352 { 2353 int i; 2354 2355 LIST_INIT(&mkdirlisthd); 2356 max_softdeps = desiredvnodes * 4; 2357 pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, &pagedep_hash); 2358 inodedep_hashtbl = hashinit(desiredvnodes, M_INODEDEP, &inodedep_hash); 2359 newblk_hashtbl = hashinit(desiredvnodes / 5, M_NEWBLK, &newblk_hash); 2360 bmsafemap_hashtbl = hashinit(1024, M_BMSAFEMAP, &bmsafemap_hash); 2361 i = 1 << (ffs(desiredvnodes / 10) - 1); 2362 indir_hashtbl = malloc(i * sizeof(indir_hashtbl[0]), M_FREEWORK, 2363 M_WAITOK); 2364 indir_hash = i - 1; 2365 for (i = 0; i <= indir_hash; i++) 2366 TAILQ_INIT(&indir_hashtbl[i]); 2367 2368 /* initialise bioops hack */ 2369 bioops.io_start = softdep_disk_io_initiation; 2370 bioops.io_complete = softdep_disk_write_complete; 2371 bioops.io_deallocate = softdep_deallocate_dependencies; 2372 bioops.io_countdeps = softdep_count_dependencies; 2373 2374 /* Initialize the callout with an mtx. */ 2375 callout_init_mtx(&softdep_callout, &lk, 0); 2376 } 2377 2378 /* 2379 * Executed after all filesystems have been unmounted during 2380 * filesystem module unload. 2381 */ 2382 void 2383 softdep_uninitialize() 2384 { 2385 2386 callout_drain(&softdep_callout); 2387 hashdestroy(pagedep_hashtbl, M_PAGEDEP, pagedep_hash); 2388 hashdestroy(inodedep_hashtbl, M_INODEDEP, inodedep_hash); 2389 hashdestroy(newblk_hashtbl, M_NEWBLK, newblk_hash); 2390 hashdestroy(bmsafemap_hashtbl, M_BMSAFEMAP, bmsafemap_hash); 2391 free(indir_hashtbl, M_FREEWORK); 2392 } 2393 2394 /* 2395 * Called at mount time to notify the dependency code that a 2396 * filesystem wishes to use it. 2397 */ 2398 int 2399 softdep_mount(devvp, mp, fs, cred) 2400 struct vnode *devvp; 2401 struct mount *mp; 2402 struct fs *fs; 2403 struct ucred *cred; 2404 { 2405 struct csum_total cstotal; 2406 struct ufsmount *ump; 2407 struct cg *cgp; 2408 struct buf *bp; 2409 int error, cyl; 2410 2411 MNT_ILOCK(mp); 2412 mp->mnt_flag = (mp->mnt_flag & ~MNT_ASYNC) | MNT_SOFTDEP; 2413 if ((mp->mnt_kern_flag & MNTK_SOFTDEP) == 0) { 2414 mp->mnt_kern_flag = (mp->mnt_kern_flag & ~MNTK_ASYNC) | 2415 MNTK_SOFTDEP | MNTK_NOASYNC; 2416 } 2417 MNT_IUNLOCK(mp); 2418 ump = VFSTOUFS(mp); 2419 LIST_INIT(&ump->softdep_workitem_pending); 2420 LIST_INIT(&ump->softdep_journal_pending); 2421 TAILQ_INIT(&ump->softdep_unlinked); 2422 LIST_INIT(&ump->softdep_dirtycg); 2423 ump->softdep_worklist_tail = NULL; 2424 ump->softdep_on_worklist = 0; 2425 ump->softdep_deps = 0; 2426 if ((fs->fs_flags & FS_SUJ) && 2427 (error = journal_mount(mp, fs, cred)) != 0) { 2428 printf("Failed to start journal: %d\n", error); 2429 return (error); 2430 } 2431 /* 2432 * When doing soft updates, the counters in the 2433 * superblock may have gotten out of sync. Recomputation 2434 * can take a long time and can be deferred for background 2435 * fsck. However, the old behavior of scanning the cylinder 2436 * groups and recalculating them at mount time is available 2437 * by setting vfs.ffs.compute_summary_at_mount to one. 2438 */ 2439 if (compute_summary_at_mount == 0 || fs->fs_clean != 0) 2440 return (0); 2441 bzero(&cstotal, sizeof cstotal); 2442 for (cyl = 0; cyl < fs->fs_ncg; cyl++) { 2443 if ((error = bread(devvp, fsbtodb(fs, cgtod(fs, cyl)), 2444 fs->fs_cgsize, cred, &bp)) != 0) { 2445 brelse(bp); 2446 return (error); 2447 } 2448 cgp = (struct cg *)bp->b_data; 2449 cstotal.cs_nffree += cgp->cg_cs.cs_nffree; 2450 cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree; 2451 cstotal.cs_nifree += cgp->cg_cs.cs_nifree; 2452 cstotal.cs_ndir += cgp->cg_cs.cs_ndir; 2453 fs->fs_cs(fs, cyl) = cgp->cg_cs; 2454 brelse(bp); 2455 } 2456 #ifdef DEBUG 2457 if (bcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal)) 2458 printf("%s: superblock summary recomputed\n", fs->fs_fsmnt); 2459 #endif 2460 bcopy(&cstotal, &fs->fs_cstotal, sizeof cstotal); 2461 return (0); 2462 } 2463 2464 void 2465 softdep_unmount(mp) 2466 struct mount *mp; 2467 { 2468 2469 MNT_ILOCK(mp); 2470 mp->mnt_flag &= ~MNT_SOFTDEP; 2471 if (MOUNTEDSUJ(mp) == 0) { 2472 MNT_IUNLOCK(mp); 2473 return; 2474 } 2475 mp->mnt_flag &= ~MNT_SUJ; 2476 MNT_IUNLOCK(mp); 2477 journal_unmount(mp); 2478 } 2479 2480 static struct jblocks * 2481 jblocks_create(void) 2482 { 2483 struct jblocks *jblocks; 2484 2485 jblocks = malloc(sizeof(*jblocks), M_JBLOCKS, M_WAITOK | M_ZERO); 2486 TAILQ_INIT(&jblocks->jb_segs); 2487 jblocks->jb_avail = 10; 2488 jblocks->jb_extent = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2489 M_JBLOCKS, M_WAITOK | M_ZERO); 2490 2491 return (jblocks); 2492 } 2493 2494 static ufs2_daddr_t 2495 jblocks_alloc(jblocks, bytes, actual) 2496 struct jblocks *jblocks; 2497 int bytes; 2498 int *actual; 2499 { 2500 ufs2_daddr_t daddr; 2501 struct jextent *jext; 2502 int freecnt; 2503 int blocks; 2504 2505 blocks = bytes / DEV_BSIZE; 2506 jext = &jblocks->jb_extent[jblocks->jb_head]; 2507 freecnt = jext->je_blocks - jblocks->jb_off; 2508 if (freecnt == 0) { 2509 jblocks->jb_off = 0; 2510 if (++jblocks->jb_head > jblocks->jb_used) 2511 jblocks->jb_head = 0; 2512 jext = &jblocks->jb_extent[jblocks->jb_head]; 2513 freecnt = jext->je_blocks; 2514 } 2515 if (freecnt > blocks) 2516 freecnt = blocks; 2517 *actual = freecnt * DEV_BSIZE; 2518 daddr = jext->je_daddr + jblocks->jb_off; 2519 jblocks->jb_off += freecnt; 2520 jblocks->jb_free -= freecnt; 2521 2522 return (daddr); 2523 } 2524 2525 static void 2526 jblocks_free(jblocks, mp, bytes) 2527 struct jblocks *jblocks; 2528 struct mount *mp; 2529 int bytes; 2530 { 2531 2532 jblocks->jb_free += bytes / DEV_BSIZE; 2533 if (jblocks->jb_suspended) 2534 worklist_speedup(); 2535 wakeup(jblocks); 2536 } 2537 2538 static void 2539 jblocks_destroy(jblocks) 2540 struct jblocks *jblocks; 2541 { 2542 2543 if (jblocks->jb_extent) 2544 free(jblocks->jb_extent, M_JBLOCKS); 2545 free(jblocks, M_JBLOCKS); 2546 } 2547 2548 static void 2549 jblocks_add(jblocks, daddr, blocks) 2550 struct jblocks *jblocks; 2551 ufs2_daddr_t daddr; 2552 int blocks; 2553 { 2554 struct jextent *jext; 2555 2556 jblocks->jb_blocks += blocks; 2557 jblocks->jb_free += blocks; 2558 jext = &jblocks->jb_extent[jblocks->jb_used]; 2559 /* Adding the first block. */ 2560 if (jext->je_daddr == 0) { 2561 jext->je_daddr = daddr; 2562 jext->je_blocks = blocks; 2563 return; 2564 } 2565 /* Extending the last extent. */ 2566 if (jext->je_daddr + jext->je_blocks == daddr) { 2567 jext->je_blocks += blocks; 2568 return; 2569 } 2570 /* Adding a new extent. */ 2571 if (++jblocks->jb_used == jblocks->jb_avail) { 2572 jblocks->jb_avail *= 2; 2573 jext = malloc(sizeof(struct jextent) * jblocks->jb_avail, 2574 M_JBLOCKS, M_WAITOK | M_ZERO); 2575 memcpy(jext, jblocks->jb_extent, 2576 sizeof(struct jextent) * jblocks->jb_used); 2577 free(jblocks->jb_extent, M_JBLOCKS); 2578 jblocks->jb_extent = jext; 2579 } 2580 jext = &jblocks->jb_extent[jblocks->jb_used]; 2581 jext->je_daddr = daddr; 2582 jext->je_blocks = blocks; 2583 return; 2584 } 2585 2586 int 2587 softdep_journal_lookup(mp, vpp) 2588 struct mount *mp; 2589 struct vnode **vpp; 2590 { 2591 struct componentname cnp; 2592 struct vnode *dvp; 2593 ino_t sujournal; 2594 int error; 2595 2596 error = VFS_VGET(mp, ROOTINO, LK_EXCLUSIVE, &dvp); 2597 if (error) 2598 return (error); 2599 bzero(&cnp, sizeof(cnp)); 2600 cnp.cn_nameiop = LOOKUP; 2601 cnp.cn_flags = ISLASTCN; 2602 cnp.cn_thread = curthread; 2603 cnp.cn_cred = curthread->td_ucred; 2604 cnp.cn_pnbuf = SUJ_FILE; 2605 cnp.cn_nameptr = SUJ_FILE; 2606 cnp.cn_namelen = strlen(SUJ_FILE); 2607 error = ufs_lookup_ino(dvp, NULL, &cnp, &sujournal); 2608 vput(dvp); 2609 if (error != 0) 2610 return (error); 2611 error = VFS_VGET(mp, sujournal, LK_EXCLUSIVE, vpp); 2612 return (error); 2613 } 2614 2615 /* 2616 * Open and verify the journal file. 2617 */ 2618 static int 2619 journal_mount(mp, fs, cred) 2620 struct mount *mp; 2621 struct fs *fs; 2622 struct ucred *cred; 2623 { 2624 struct jblocks *jblocks; 2625 struct vnode *vp; 2626 struct inode *ip; 2627 ufs2_daddr_t blkno; 2628 int bcount; 2629 int error; 2630 int i; 2631 2632 error = softdep_journal_lookup(mp, &vp); 2633 if (error != 0) { 2634 printf("Failed to find journal. Use tunefs to create one\n"); 2635 return (error); 2636 } 2637 ip = VTOI(vp); 2638 if (ip->i_size < SUJ_MIN) { 2639 error = ENOSPC; 2640 goto out; 2641 } 2642 bcount = lblkno(fs, ip->i_size); /* Only use whole blocks. */ 2643 jblocks = jblocks_create(); 2644 for (i = 0; i < bcount; i++) { 2645 error = ufs_bmaparray(vp, i, &blkno, NULL, NULL, NULL); 2646 if (error) 2647 break; 2648 jblocks_add(jblocks, blkno, fsbtodb(fs, fs->fs_frag)); 2649 } 2650 if (error) { 2651 jblocks_destroy(jblocks); 2652 goto out; 2653 } 2654 jblocks->jb_low = jblocks->jb_free / 3; /* Reserve 33%. */ 2655 jblocks->jb_min = jblocks->jb_free / 10; /* Suspend at 10%. */ 2656 VFSTOUFS(mp)->softdep_jblocks = jblocks; 2657 out: 2658 if (error == 0) { 2659 MNT_ILOCK(mp); 2660 mp->mnt_flag |= MNT_SUJ; 2661 mp->mnt_flag &= ~MNT_SOFTDEP; 2662 MNT_IUNLOCK(mp); 2663 /* 2664 * Only validate the journal contents if the 2665 * filesystem is clean, otherwise we write the logs 2666 * but they'll never be used. If the filesystem was 2667 * still dirty when we mounted it the journal is 2668 * invalid and a new journal can only be valid if it 2669 * starts from a clean mount. 2670 */ 2671 if (fs->fs_clean) { 2672 DIP_SET(ip, i_modrev, fs->fs_mtime); 2673 ip->i_flags |= IN_MODIFIED; 2674 ffs_update(vp, 1); 2675 } 2676 } 2677 vput(vp); 2678 return (error); 2679 } 2680 2681 static void 2682 journal_unmount(mp) 2683 struct mount *mp; 2684 { 2685 struct ufsmount *ump; 2686 2687 ump = VFSTOUFS(mp); 2688 if (ump->softdep_jblocks) 2689 jblocks_destroy(ump->softdep_jblocks); 2690 ump->softdep_jblocks = NULL; 2691 } 2692 2693 /* 2694 * Called when a journal record is ready to be written. Space is allocated 2695 * and the journal entry is created when the journal is flushed to stable 2696 * store. 2697 */ 2698 static void 2699 add_to_journal(wk) 2700 struct worklist *wk; 2701 { 2702 struct ufsmount *ump; 2703 2704 mtx_assert(&lk, MA_OWNED); 2705 ump = VFSTOUFS(wk->wk_mp); 2706 if (wk->wk_state & ONWORKLIST) 2707 panic("add_to_journal: %s(0x%X) already on list", 2708 TYPENAME(wk->wk_type), wk->wk_state); 2709 wk->wk_state |= ONWORKLIST | DEPCOMPLETE; 2710 if (LIST_EMPTY(&ump->softdep_journal_pending)) { 2711 ump->softdep_jblocks->jb_age = ticks; 2712 LIST_INSERT_HEAD(&ump->softdep_journal_pending, wk, wk_list); 2713 } else 2714 LIST_INSERT_AFTER(ump->softdep_journal_tail, wk, wk_list); 2715 ump->softdep_journal_tail = wk; 2716 ump->softdep_on_journal += 1; 2717 } 2718 2719 /* 2720 * Remove an arbitrary item for the journal worklist maintain the tail 2721 * pointer. This happens when a new operation obviates the need to 2722 * journal an old operation. 2723 */ 2724 static void 2725 remove_from_journal(wk) 2726 struct worklist *wk; 2727 { 2728 struct ufsmount *ump; 2729 2730 mtx_assert(&lk, MA_OWNED); 2731 ump = VFSTOUFS(wk->wk_mp); 2732 #ifdef SUJ_DEBUG 2733 { 2734 struct worklist *wkn; 2735 2736 LIST_FOREACH(wkn, &ump->softdep_journal_pending, wk_list) 2737 if (wkn == wk) 2738 break; 2739 if (wkn == NULL) 2740 panic("remove_from_journal: %p is not in journal", wk); 2741 } 2742 #endif 2743 /* 2744 * We emulate a TAILQ to save space in most structures which do not 2745 * require TAILQ semantics. Here we must update the tail position 2746 * when removing the tail which is not the final entry. This works 2747 * only if the worklist linkage are at the beginning of the structure. 2748 */ 2749 if (ump->softdep_journal_tail == wk) 2750 ump->softdep_journal_tail = 2751 (struct worklist *)wk->wk_list.le_prev; 2752 2753 WORKLIST_REMOVE(wk); 2754 ump->softdep_on_journal -= 1; 2755 } 2756 2757 /* 2758 * Check for journal space as well as dependency limits so the prelink 2759 * code can throttle both journaled and non-journaled filesystems. 2760 * Threshold is 0 for low and 1 for min. 2761 */ 2762 static int 2763 journal_space(ump, thresh) 2764 struct ufsmount *ump; 2765 int thresh; 2766 { 2767 struct jblocks *jblocks; 2768 int avail; 2769 2770 jblocks = ump->softdep_jblocks; 2771 if (jblocks == NULL) 2772 return (1); 2773 /* 2774 * We use a tighter restriction here to prevent request_cleanup() 2775 * running in threads from running into locks we currently hold. 2776 */ 2777 if (dep_current[D_INODEDEP] > (max_softdeps / 10) * 9) 2778 return (0); 2779 if (thresh) 2780 thresh = jblocks->jb_min; 2781 else 2782 thresh = jblocks->jb_low; 2783 avail = (ump->softdep_on_journal * JREC_SIZE) / DEV_BSIZE; 2784 avail = jblocks->jb_free - avail; 2785 2786 return (avail > thresh); 2787 } 2788 2789 static void 2790 journal_suspend(ump) 2791 struct ufsmount *ump; 2792 { 2793 struct jblocks *jblocks; 2794 struct mount *mp; 2795 2796 mp = UFSTOVFS(ump); 2797 jblocks = ump->softdep_jblocks; 2798 MNT_ILOCK(mp); 2799 if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0) { 2800 stat_journal_min++; 2801 mp->mnt_kern_flag |= MNTK_SUSPEND; 2802 mp->mnt_susp_owner = FIRST_THREAD_IN_PROC(softdepproc); 2803 } 2804 jblocks->jb_suspended = 1; 2805 MNT_IUNLOCK(mp); 2806 } 2807 2808 static int 2809 journal_unsuspend(struct ufsmount *ump) 2810 { 2811 struct jblocks *jblocks; 2812 struct mount *mp; 2813 2814 mp = UFSTOVFS(ump); 2815 jblocks = ump->softdep_jblocks; 2816 2817 if (jblocks != NULL && jblocks->jb_suspended && 2818 journal_space(ump, jblocks->jb_min)) { 2819 jblocks->jb_suspended = 0; 2820 FREE_LOCK(&lk); 2821 mp->mnt_susp_owner = curthread; 2822 vfs_write_resume(mp, 0); 2823 ACQUIRE_LOCK(&lk); 2824 return (1); 2825 } 2826 return (0); 2827 } 2828 2829 /* 2830 * Called before any allocation function to be certain that there is 2831 * sufficient space in the journal prior to creating any new records. 2832 * Since in the case of block allocation we may have multiple locked 2833 * buffers at the time of the actual allocation we can not block 2834 * when the journal records are created. Doing so would create a deadlock 2835 * if any of these buffers needed to be flushed to reclaim space. Instead 2836 * we require a sufficiently large amount of available space such that 2837 * each thread in the system could have passed this allocation check and 2838 * still have sufficient free space. With 20% of a minimum journal size 2839 * of 1MB we have 6553 records available. 2840 */ 2841 int 2842 softdep_prealloc(vp, waitok) 2843 struct vnode *vp; 2844 int waitok; 2845 { 2846 struct ufsmount *ump; 2847 2848 /* 2849 * Nothing to do if we are not running journaled soft updates. 2850 * If we currently hold the snapshot lock, we must avoid handling 2851 * other resources that could cause deadlock. 2852 */ 2853 if (DOINGSUJ(vp) == 0 || IS_SNAPSHOT(VTOI(vp))) 2854 return (0); 2855 ump = VFSTOUFS(vp->v_mount); 2856 ACQUIRE_LOCK(&lk); 2857 if (journal_space(ump, 0)) { 2858 FREE_LOCK(&lk); 2859 return (0); 2860 } 2861 stat_journal_low++; 2862 FREE_LOCK(&lk); 2863 if (waitok == MNT_NOWAIT) 2864 return (ENOSPC); 2865 /* 2866 * Attempt to sync this vnode once to flush any journal 2867 * work attached to it. 2868 */ 2869 if ((curthread->td_pflags & TDP_COWINPROGRESS) == 0) 2870 ffs_syncvnode(vp, waitok, 0); 2871 ACQUIRE_LOCK(&lk); 2872 process_removes(vp); 2873 process_truncates(vp); 2874 if (journal_space(ump, 0) == 0) { 2875 softdep_speedup(); 2876 if (journal_space(ump, 1) == 0) 2877 journal_suspend(ump); 2878 } 2879 FREE_LOCK(&lk); 2880 2881 return (0); 2882 } 2883 2884 /* 2885 * Before adjusting a link count on a vnode verify that we have sufficient 2886 * journal space. If not, process operations that depend on the currently 2887 * locked pair of vnodes to try to flush space as the syncer, buf daemon, 2888 * and softdep flush threads can not acquire these locks to reclaim space. 2889 */ 2890 static void 2891 softdep_prelink(dvp, vp) 2892 struct vnode *dvp; 2893 struct vnode *vp; 2894 { 2895 struct ufsmount *ump; 2896 2897 ump = VFSTOUFS(dvp->v_mount); 2898 mtx_assert(&lk, MA_OWNED); 2899 /* 2900 * Nothing to do if we have sufficient journal space. 2901 * If we currently hold the snapshot lock, we must avoid 2902 * handling other resources that could cause deadlock. 2903 */ 2904 if (journal_space(ump, 0) || (vp && IS_SNAPSHOT(VTOI(vp)))) 2905 return; 2906 stat_journal_low++; 2907 FREE_LOCK(&lk); 2908 if (vp) 2909 ffs_syncvnode(vp, MNT_NOWAIT, 0); 2910 ffs_syncvnode(dvp, MNT_WAIT, 0); 2911 ACQUIRE_LOCK(&lk); 2912 /* Process vp before dvp as it may create .. removes. */ 2913 if (vp) { 2914 process_removes(vp); 2915 process_truncates(vp); 2916 } 2917 process_removes(dvp); 2918 process_truncates(dvp); 2919 softdep_speedup(); 2920 process_worklist_item(UFSTOVFS(ump), 2, LK_NOWAIT); 2921 if (journal_space(ump, 0) == 0) { 2922 softdep_speedup(); 2923 if (journal_space(ump, 1) == 0) 2924 journal_suspend(ump); 2925 } 2926 } 2927 2928 static void 2929 jseg_write(ump, jseg, data) 2930 struct ufsmount *ump; 2931 struct jseg *jseg; 2932 uint8_t *data; 2933 { 2934 struct jsegrec *rec; 2935 2936 rec = (struct jsegrec *)data; 2937 rec->jsr_seq = jseg->js_seq; 2938 rec->jsr_oldest = jseg->js_oldseq; 2939 rec->jsr_cnt = jseg->js_cnt; 2940 rec->jsr_blocks = jseg->js_size / ump->um_devvp->v_bufobj.bo_bsize; 2941 rec->jsr_crc = 0; 2942 rec->jsr_time = ump->um_fs->fs_mtime; 2943 } 2944 2945 static inline void 2946 inoref_write(inoref, jseg, rec) 2947 struct inoref *inoref; 2948 struct jseg *jseg; 2949 struct jrefrec *rec; 2950 { 2951 2952 inoref->if_jsegdep->jd_seg = jseg; 2953 rec->jr_ino = inoref->if_ino; 2954 rec->jr_parent = inoref->if_parent; 2955 rec->jr_nlink = inoref->if_nlink; 2956 rec->jr_mode = inoref->if_mode; 2957 rec->jr_diroff = inoref->if_diroff; 2958 } 2959 2960 static void 2961 jaddref_write(jaddref, jseg, data) 2962 struct jaddref *jaddref; 2963 struct jseg *jseg; 2964 uint8_t *data; 2965 { 2966 struct jrefrec *rec; 2967 2968 rec = (struct jrefrec *)data; 2969 rec->jr_op = JOP_ADDREF; 2970 inoref_write(&jaddref->ja_ref, jseg, rec); 2971 } 2972 2973 static void 2974 jremref_write(jremref, jseg, data) 2975 struct jremref *jremref; 2976 struct jseg *jseg; 2977 uint8_t *data; 2978 { 2979 struct jrefrec *rec; 2980 2981 rec = (struct jrefrec *)data; 2982 rec->jr_op = JOP_REMREF; 2983 inoref_write(&jremref->jr_ref, jseg, rec); 2984 } 2985 2986 static void 2987 jmvref_write(jmvref, jseg, data) 2988 struct jmvref *jmvref; 2989 struct jseg *jseg; 2990 uint8_t *data; 2991 { 2992 struct jmvrec *rec; 2993 2994 rec = (struct jmvrec *)data; 2995 rec->jm_op = JOP_MVREF; 2996 rec->jm_ino = jmvref->jm_ino; 2997 rec->jm_parent = jmvref->jm_parent; 2998 rec->jm_oldoff = jmvref->jm_oldoff; 2999 rec->jm_newoff = jmvref->jm_newoff; 3000 } 3001 3002 static void 3003 jnewblk_write(jnewblk, jseg, data) 3004 struct jnewblk *jnewblk; 3005 struct jseg *jseg; 3006 uint8_t *data; 3007 { 3008 struct jblkrec *rec; 3009 3010 jnewblk->jn_jsegdep->jd_seg = jseg; 3011 rec = (struct jblkrec *)data; 3012 rec->jb_op = JOP_NEWBLK; 3013 rec->jb_ino = jnewblk->jn_ino; 3014 rec->jb_blkno = jnewblk->jn_blkno; 3015 rec->jb_lbn = jnewblk->jn_lbn; 3016 rec->jb_frags = jnewblk->jn_frags; 3017 rec->jb_oldfrags = jnewblk->jn_oldfrags; 3018 } 3019 3020 static void 3021 jfreeblk_write(jfreeblk, jseg, data) 3022 struct jfreeblk *jfreeblk; 3023 struct jseg *jseg; 3024 uint8_t *data; 3025 { 3026 struct jblkrec *rec; 3027 3028 jfreeblk->jf_dep.jb_jsegdep->jd_seg = jseg; 3029 rec = (struct jblkrec *)data; 3030 rec->jb_op = JOP_FREEBLK; 3031 rec->jb_ino = jfreeblk->jf_ino; 3032 rec->jb_blkno = jfreeblk->jf_blkno; 3033 rec->jb_lbn = jfreeblk->jf_lbn; 3034 rec->jb_frags = jfreeblk->jf_frags; 3035 rec->jb_oldfrags = 0; 3036 } 3037 3038 static void 3039 jfreefrag_write(jfreefrag, jseg, data) 3040 struct jfreefrag *jfreefrag; 3041 struct jseg *jseg; 3042 uint8_t *data; 3043 { 3044 struct jblkrec *rec; 3045 3046 jfreefrag->fr_jsegdep->jd_seg = jseg; 3047 rec = (struct jblkrec *)data; 3048 rec->jb_op = JOP_FREEBLK; 3049 rec->jb_ino = jfreefrag->fr_ino; 3050 rec->jb_blkno = jfreefrag->fr_blkno; 3051 rec->jb_lbn = jfreefrag->fr_lbn; 3052 rec->jb_frags = jfreefrag->fr_frags; 3053 rec->jb_oldfrags = 0; 3054 } 3055 3056 static void 3057 jtrunc_write(jtrunc, jseg, data) 3058 struct jtrunc *jtrunc; 3059 struct jseg *jseg; 3060 uint8_t *data; 3061 { 3062 struct jtrncrec *rec; 3063 3064 jtrunc->jt_dep.jb_jsegdep->jd_seg = jseg; 3065 rec = (struct jtrncrec *)data; 3066 rec->jt_op = JOP_TRUNC; 3067 rec->jt_ino = jtrunc->jt_ino; 3068 rec->jt_size = jtrunc->jt_size; 3069 rec->jt_extsize = jtrunc->jt_extsize; 3070 } 3071 3072 static void 3073 jfsync_write(jfsync, jseg, data) 3074 struct jfsync *jfsync; 3075 struct jseg *jseg; 3076 uint8_t *data; 3077 { 3078 struct jtrncrec *rec; 3079 3080 rec = (struct jtrncrec *)data; 3081 rec->jt_op = JOP_SYNC; 3082 rec->jt_ino = jfsync->jfs_ino; 3083 rec->jt_size = jfsync->jfs_size; 3084 rec->jt_extsize = jfsync->jfs_extsize; 3085 } 3086 3087 static void 3088 softdep_flushjournal(mp) 3089 struct mount *mp; 3090 { 3091 struct jblocks *jblocks; 3092 struct ufsmount *ump; 3093 3094 if (MOUNTEDSUJ(mp) == 0) 3095 return; 3096 ump = VFSTOUFS(mp); 3097 jblocks = ump->softdep_jblocks; 3098 ACQUIRE_LOCK(&lk); 3099 while (ump->softdep_on_journal) { 3100 jblocks->jb_needseg = 1; 3101 softdep_process_journal(mp, NULL, MNT_WAIT); 3102 } 3103 FREE_LOCK(&lk); 3104 } 3105 3106 static void softdep_synchronize_completed(struct bio *); 3107 static void softdep_synchronize(struct bio *, struct ufsmount *, void *); 3108 3109 static void 3110 softdep_synchronize_completed(bp) 3111 struct bio *bp; 3112 { 3113 struct jseg *oldest; 3114 struct jseg *jseg; 3115 3116 /* 3117 * caller1 marks the last segment written before we issued the 3118 * synchronize cache. 3119 */ 3120 jseg = bp->bio_caller1; 3121 oldest = NULL; 3122 ACQUIRE_LOCK(&lk); 3123 /* 3124 * Mark all the journal entries waiting on the synchronize cache 3125 * as completed so they may continue on. 3126 */ 3127 while (jseg != NULL && (jseg->js_state & COMPLETE) == 0) { 3128 jseg->js_state |= COMPLETE; 3129 oldest = jseg; 3130 jseg = TAILQ_PREV(jseg, jseglst, js_next); 3131 } 3132 /* 3133 * Restart deferred journal entry processing from the oldest 3134 * completed jseg. 3135 */ 3136 if (oldest) 3137 complete_jsegs(oldest); 3138 3139 FREE_LOCK(&lk); 3140 g_destroy_bio(bp); 3141 } 3142 3143 /* 3144 * Send BIO_FLUSH/SYNCHRONIZE CACHE to the device to enforce write ordering 3145 * barriers. The journal must be written prior to any blocks that depend 3146 * on it and the journal can not be released until the blocks have be 3147 * written. This code handles both barriers simultaneously. 3148 */ 3149 static void 3150 softdep_synchronize(bp, ump, caller1) 3151 struct bio *bp; 3152 struct ufsmount *ump; 3153 void *caller1; 3154 { 3155 3156 bp->bio_cmd = BIO_FLUSH; 3157 bp->bio_flags |= BIO_ORDERED; 3158 bp->bio_data = NULL; 3159 bp->bio_offset = ump->um_cp->provider->mediasize; 3160 bp->bio_length = 0; 3161 bp->bio_done = softdep_synchronize_completed; 3162 bp->bio_caller1 = caller1; 3163 g_io_request(bp, 3164 (struct g_consumer *)ump->um_devvp->v_bufobj.bo_private); 3165 } 3166 3167 /* 3168 * Flush some journal records to disk. 3169 */ 3170 static void 3171 softdep_process_journal(mp, needwk, flags) 3172 struct mount *mp; 3173 struct worklist *needwk; 3174 int flags; 3175 { 3176 struct jblocks *jblocks; 3177 struct ufsmount *ump; 3178 struct worklist *wk; 3179 struct jseg *jseg; 3180 struct buf *bp; 3181 struct bio *bio; 3182 uint8_t *data; 3183 struct fs *fs; 3184 int shouldflush; 3185 int segwritten; 3186 int jrecmin; /* Minimum records per block. */ 3187 int jrecmax; /* Maximum records per block. */ 3188 int size; 3189 int cnt; 3190 int off; 3191 int devbsize; 3192 3193 if (MOUNTEDSUJ(mp) == 0) 3194 return; 3195 shouldflush = softdep_flushcache; 3196 bio = NULL; 3197 jseg = NULL; 3198 ump = VFSTOUFS(mp); 3199 fs = ump->um_fs; 3200 jblocks = ump->softdep_jblocks; 3201 devbsize = ump->um_devvp->v_bufobj.bo_bsize; 3202 /* 3203 * We write anywhere between a disk block and fs block. The upper 3204 * bound is picked to prevent buffer cache fragmentation and limit 3205 * processing time per I/O. 3206 */ 3207 jrecmin = (devbsize / JREC_SIZE) - 1; /* -1 for seg header */ 3208 jrecmax = (fs->fs_bsize / devbsize) * jrecmin; 3209 segwritten = 0; 3210 for (;;) { 3211 cnt = ump->softdep_on_journal; 3212 /* 3213 * Criteria for writing a segment: 3214 * 1) We have a full block. 3215 * 2) We're called from jwait() and haven't found the 3216 * journal item yet. 3217 * 3) Always write if needseg is set. 3218 * 4) If we are called from process_worklist and have 3219 * not yet written anything we write a partial block 3220 * to enforce a 1 second maximum latency on journal 3221 * entries. 3222 */ 3223 if (cnt < (jrecmax - 1) && needwk == NULL && 3224 jblocks->jb_needseg == 0 && (segwritten || cnt == 0)) 3225 break; 3226 cnt++; 3227 /* 3228 * Verify some free journal space. softdep_prealloc() should 3229 * guarantee that we don't run out so this is indicative of 3230 * a problem with the flow control. Try to recover 3231 * gracefully in any event. 3232 */ 3233 while (jblocks->jb_free == 0) { 3234 if (flags != MNT_WAIT) 3235 break; 3236 printf("softdep: Out of journal space!\n"); 3237 softdep_speedup(); 3238 msleep(jblocks, &lk, PRIBIO, "jblocks", hz); 3239 } 3240 FREE_LOCK(&lk); 3241 jseg = malloc(sizeof(*jseg), M_JSEG, M_SOFTDEP_FLAGS); 3242 workitem_alloc(&jseg->js_list, D_JSEG, mp); 3243 LIST_INIT(&jseg->js_entries); 3244 LIST_INIT(&jseg->js_indirs); 3245 jseg->js_state = ATTACHED; 3246 if (shouldflush == 0) 3247 jseg->js_state |= COMPLETE; 3248 else if (bio == NULL) 3249 bio = g_alloc_bio(); 3250 jseg->js_jblocks = jblocks; 3251 bp = geteblk(fs->fs_bsize, 0); 3252 ACQUIRE_LOCK(&lk); 3253 /* 3254 * If there was a race while we were allocating the block 3255 * and jseg the entry we care about was likely written. 3256 * We bail out in both the WAIT and NOWAIT case and assume 3257 * the caller will loop if the entry it cares about is 3258 * not written. 3259 */ 3260 cnt = ump->softdep_on_journal; 3261 if (cnt + jblocks->jb_needseg == 0 || jblocks->jb_free == 0) { 3262 bp->b_flags |= B_INVAL | B_NOCACHE; 3263 WORKITEM_FREE(jseg, D_JSEG); 3264 FREE_LOCK(&lk); 3265 brelse(bp); 3266 ACQUIRE_LOCK(&lk); 3267 break; 3268 } 3269 /* 3270 * Calculate the disk block size required for the available 3271 * records rounded to the min size. 3272 */ 3273 if (cnt == 0) 3274 size = devbsize; 3275 else if (cnt < jrecmax) 3276 size = howmany(cnt, jrecmin) * devbsize; 3277 else 3278 size = fs->fs_bsize; 3279 /* 3280 * Allocate a disk block for this journal data and account 3281 * for truncation of the requested size if enough contiguous 3282 * space was not available. 3283 */ 3284 bp->b_blkno = jblocks_alloc(jblocks, size, &size); 3285 bp->b_lblkno = bp->b_blkno; 3286 bp->b_offset = bp->b_blkno * DEV_BSIZE; 3287 bp->b_bcount = size; 3288 bp->b_bufobj = &ump->um_devvp->v_bufobj; 3289 bp->b_flags &= ~B_INVAL; 3290 bp->b_flags |= B_VALIDSUSPWRT | B_NOCOPY; 3291 /* 3292 * Initialize our jseg with cnt records. Assign the next 3293 * sequence number to it and link it in-order. 3294 */ 3295 cnt = MIN(cnt, (size / devbsize) * jrecmin); 3296 jseg->js_buf = bp; 3297 jseg->js_cnt = cnt; 3298 jseg->js_refs = cnt + 1; /* Self ref. */ 3299 jseg->js_size = size; 3300 jseg->js_seq = jblocks->jb_nextseq++; 3301 if (jblocks->jb_oldestseg == NULL) 3302 jblocks->jb_oldestseg = jseg; 3303 jseg->js_oldseq = jblocks->jb_oldestseg->js_seq; 3304 TAILQ_INSERT_TAIL(&jblocks->jb_segs, jseg, js_next); 3305 if (jblocks->jb_writeseg == NULL) 3306 jblocks->jb_writeseg = jseg; 3307 /* 3308 * Start filling in records from the pending list. 3309 */ 3310 data = bp->b_data; 3311 off = 0; 3312 while ((wk = LIST_FIRST(&ump->softdep_journal_pending)) 3313 != NULL) { 3314 if (cnt == 0) 3315 break; 3316 /* Place a segment header on every device block. */ 3317 if ((off % devbsize) == 0) { 3318 jseg_write(ump, jseg, data); 3319 off += JREC_SIZE; 3320 data = bp->b_data + off; 3321 } 3322 if (wk == needwk) 3323 needwk = NULL; 3324 remove_from_journal(wk); 3325 wk->wk_state |= INPROGRESS; 3326 WORKLIST_INSERT(&jseg->js_entries, wk); 3327 switch (wk->wk_type) { 3328 case D_JADDREF: 3329 jaddref_write(WK_JADDREF(wk), jseg, data); 3330 break; 3331 case D_JREMREF: 3332 jremref_write(WK_JREMREF(wk), jseg, data); 3333 break; 3334 case D_JMVREF: 3335 jmvref_write(WK_JMVREF(wk), jseg, data); 3336 break; 3337 case D_JNEWBLK: 3338 jnewblk_write(WK_JNEWBLK(wk), jseg, data); 3339 break; 3340 case D_JFREEBLK: 3341 jfreeblk_write(WK_JFREEBLK(wk), jseg, data); 3342 break; 3343 case D_JFREEFRAG: 3344 jfreefrag_write(WK_JFREEFRAG(wk), jseg, data); 3345 break; 3346 case D_JTRUNC: 3347 jtrunc_write(WK_JTRUNC(wk), jseg, data); 3348 break; 3349 case D_JFSYNC: 3350 jfsync_write(WK_JFSYNC(wk), jseg, data); 3351 break; 3352 default: 3353 panic("process_journal: Unknown type %s", 3354 TYPENAME(wk->wk_type)); 3355 /* NOTREACHED */ 3356 } 3357 off += JREC_SIZE; 3358 data = bp->b_data + off; 3359 cnt--; 3360 } 3361 /* 3362 * Write this one buffer and continue. 3363 */ 3364 segwritten = 1; 3365 jblocks->jb_needseg = 0; 3366 WORKLIST_INSERT(&bp->b_dep, &jseg->js_list); 3367 FREE_LOCK(&lk); 3368 BO_LOCK(bp->b_bufobj); 3369 bgetvp(ump->um_devvp, bp); 3370 BO_UNLOCK(bp->b_bufobj); 3371 /* 3372 * We only do the blocking wait once we find the journal 3373 * entry we're looking for. 3374 */ 3375 if (needwk == NULL && flags == MNT_WAIT) 3376 bwrite(bp); 3377 else 3378 bawrite(bp); 3379 ACQUIRE_LOCK(&lk); 3380 } 3381 /* 3382 * If we wrote a segment issue a synchronize cache so the journal 3383 * is reflected on disk before the data is written. Since reclaiming 3384 * journal space also requires writing a journal record this 3385 * process also enforces a barrier before reclamation. 3386 */ 3387 if (segwritten && shouldflush) { 3388 softdep_synchronize(bio, ump, 3389 TAILQ_LAST(&jblocks->jb_segs, jseglst)); 3390 } else if (bio) 3391 g_destroy_bio(bio); 3392 /* 3393 * If we've suspended the filesystem because we ran out of journal 3394 * space either try to sync it here to make some progress or 3395 * unsuspend it if we already have. 3396 */ 3397 if (flags == 0 && jblocks->jb_suspended) { 3398 if (journal_unsuspend(ump)) 3399 return; 3400 FREE_LOCK(&lk); 3401 VFS_SYNC(mp, MNT_NOWAIT); 3402 ffs_sbupdate(ump, MNT_WAIT, 0); 3403 ACQUIRE_LOCK(&lk); 3404 } 3405 } 3406 3407 /* 3408 * Complete a jseg, allowing all dependencies awaiting journal writes 3409 * to proceed. Each journal dependency also attaches a jsegdep to dependent 3410 * structures so that the journal segment can be freed to reclaim space. 3411 */ 3412 static void 3413 complete_jseg(jseg) 3414 struct jseg *jseg; 3415 { 3416 struct worklist *wk; 3417 struct jmvref *jmvref; 3418 int waiting; 3419 #ifdef INVARIANTS 3420 int i = 0; 3421 #endif 3422 3423 while ((wk = LIST_FIRST(&jseg->js_entries)) != NULL) { 3424 WORKLIST_REMOVE(wk); 3425 waiting = wk->wk_state & IOWAITING; 3426 wk->wk_state &= ~(INPROGRESS | IOWAITING); 3427 wk->wk_state |= COMPLETE; 3428 KASSERT(i++ < jseg->js_cnt, 3429 ("handle_written_jseg: overflow %d >= %d", 3430 i - 1, jseg->js_cnt)); 3431 switch (wk->wk_type) { 3432 case D_JADDREF: 3433 handle_written_jaddref(WK_JADDREF(wk)); 3434 break; 3435 case D_JREMREF: 3436 handle_written_jremref(WK_JREMREF(wk)); 3437 break; 3438 case D_JMVREF: 3439 rele_jseg(jseg); /* No jsegdep. */ 3440 jmvref = WK_JMVREF(wk); 3441 LIST_REMOVE(jmvref, jm_deps); 3442 if ((jmvref->jm_pagedep->pd_state & ONWORKLIST) == 0) 3443 free_pagedep(jmvref->jm_pagedep); 3444 WORKITEM_FREE(jmvref, D_JMVREF); 3445 break; 3446 case D_JNEWBLK: 3447 handle_written_jnewblk(WK_JNEWBLK(wk)); 3448 break; 3449 case D_JFREEBLK: 3450 handle_written_jblkdep(&WK_JFREEBLK(wk)->jf_dep); 3451 break; 3452 case D_JTRUNC: 3453 handle_written_jblkdep(&WK_JTRUNC(wk)->jt_dep); 3454 break; 3455 case D_JFSYNC: 3456 rele_jseg(jseg); /* No jsegdep. */ 3457 WORKITEM_FREE(wk, D_JFSYNC); 3458 break; 3459 case D_JFREEFRAG: 3460 handle_written_jfreefrag(WK_JFREEFRAG(wk)); 3461 break; 3462 default: 3463 panic("handle_written_jseg: Unknown type %s", 3464 TYPENAME(wk->wk_type)); 3465 /* NOTREACHED */ 3466 } 3467 if (waiting) 3468 wakeup(wk); 3469 } 3470 /* Release the self reference so the structure may be freed. */ 3471 rele_jseg(jseg); 3472 } 3473 3474 /* 3475 * Determine which jsegs are ready for completion processing. Waits for 3476 * synchronize cache to complete as well as forcing in-order completion 3477 * of journal entries. 3478 */ 3479 static void 3480 complete_jsegs(jseg) 3481 struct jseg *jseg; 3482 { 3483 struct jblocks *jblocks; 3484 struct jseg *jsegn; 3485 3486 jblocks = jseg->js_jblocks; 3487 /* 3488 * Don't allow out of order completions. If this isn't the first 3489 * block wait for it to write before we're done. 3490 */ 3491 if (jseg != jblocks->jb_writeseg) 3492 return; 3493 /* Iterate through available jsegs processing their entries. */ 3494 while (jseg && (jseg->js_state & ALLCOMPLETE) == ALLCOMPLETE) { 3495 jblocks->jb_oldestwrseq = jseg->js_oldseq; 3496 jsegn = TAILQ_NEXT(jseg, js_next); 3497 complete_jseg(jseg); 3498 jseg = jsegn; 3499 } 3500 jblocks->jb_writeseg = jseg; 3501 /* 3502 * Attempt to free jsegs now that oldestwrseq may have advanced. 3503 */ 3504 free_jsegs(jblocks); 3505 } 3506 3507 /* 3508 * Mark a jseg as DEPCOMPLETE and throw away the buffer. Attempt to handle 3509 * the final completions. 3510 */ 3511 static void 3512 handle_written_jseg(jseg, bp) 3513 struct jseg *jseg; 3514 struct buf *bp; 3515 { 3516 3517 if (jseg->js_refs == 0) 3518 panic("handle_written_jseg: No self-reference on %p", jseg); 3519 jseg->js_state |= DEPCOMPLETE; 3520 /* 3521 * We'll never need this buffer again, set flags so it will be 3522 * discarded. 3523 */ 3524 bp->b_flags |= B_INVAL | B_NOCACHE; 3525 complete_jsegs(jseg); 3526 } 3527 3528 static inline struct jsegdep * 3529 inoref_jseg(inoref) 3530 struct inoref *inoref; 3531 { 3532 struct jsegdep *jsegdep; 3533 3534 jsegdep = inoref->if_jsegdep; 3535 inoref->if_jsegdep = NULL; 3536 3537 return (jsegdep); 3538 } 3539 3540 /* 3541 * Called once a jremref has made it to stable store. The jremref is marked 3542 * complete and we attempt to free it. Any pagedeps writes sleeping waiting 3543 * for the jremref to complete will be awoken by free_jremref. 3544 */ 3545 static void 3546 handle_written_jremref(jremref) 3547 struct jremref *jremref; 3548 { 3549 struct inodedep *inodedep; 3550 struct jsegdep *jsegdep; 3551 struct dirrem *dirrem; 3552 3553 /* Grab the jsegdep. */ 3554 jsegdep = inoref_jseg(&jremref->jr_ref); 3555 /* 3556 * Remove us from the inoref list. 3557 */ 3558 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 3559 0, &inodedep) == 0) 3560 panic("handle_written_jremref: Lost inodedep"); 3561 TAILQ_REMOVE(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 3562 /* 3563 * Complete the dirrem. 3564 */ 3565 dirrem = jremref->jr_dirrem; 3566 jremref->jr_dirrem = NULL; 3567 LIST_REMOVE(jremref, jr_deps); 3568 jsegdep->jd_state |= jremref->jr_state & MKDIR_PARENT; 3569 jwork_insert(&dirrem->dm_jwork, jsegdep); 3570 if (LIST_EMPTY(&dirrem->dm_jremrefhd) && 3571 (dirrem->dm_state & COMPLETE) != 0) 3572 add_to_worklist(&dirrem->dm_list, 0); 3573 free_jremref(jremref); 3574 } 3575 3576 /* 3577 * Called once a jaddref has made it to stable store. The dependency is 3578 * marked complete and any dependent structures are added to the inode 3579 * bufwait list to be completed as soon as it is written. If a bitmap write 3580 * depends on this entry we move the inode into the inodedephd of the 3581 * bmsafemap dependency and attempt to remove the jaddref from the bmsafemap. 3582 */ 3583 static void 3584 handle_written_jaddref(jaddref) 3585 struct jaddref *jaddref; 3586 { 3587 struct jsegdep *jsegdep; 3588 struct inodedep *inodedep; 3589 struct diradd *diradd; 3590 struct mkdir *mkdir; 3591 3592 /* Grab the jsegdep. */ 3593 jsegdep = inoref_jseg(&jaddref->ja_ref); 3594 mkdir = NULL; 3595 diradd = NULL; 3596 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 3597 0, &inodedep) == 0) 3598 panic("handle_written_jaddref: Lost inodedep."); 3599 if (jaddref->ja_diradd == NULL) 3600 panic("handle_written_jaddref: No dependency"); 3601 if (jaddref->ja_diradd->da_list.wk_type == D_DIRADD) { 3602 diradd = jaddref->ja_diradd; 3603 WORKLIST_INSERT(&inodedep->id_bufwait, &diradd->da_list); 3604 } else if (jaddref->ja_state & MKDIR_PARENT) { 3605 mkdir = jaddref->ja_mkdir; 3606 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir->md_list); 3607 } else if (jaddref->ja_state & MKDIR_BODY) 3608 mkdir = jaddref->ja_mkdir; 3609 else 3610 panic("handle_written_jaddref: Unknown dependency %p", 3611 jaddref->ja_diradd); 3612 jaddref->ja_diradd = NULL; /* also clears ja_mkdir */ 3613 /* 3614 * Remove us from the inode list. 3615 */ 3616 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, if_deps); 3617 /* 3618 * The mkdir may be waiting on the jaddref to clear before freeing. 3619 */ 3620 if (mkdir) { 3621 KASSERT(mkdir->md_list.wk_type == D_MKDIR, 3622 ("handle_written_jaddref: Incorrect type for mkdir %s", 3623 TYPENAME(mkdir->md_list.wk_type))); 3624 mkdir->md_jaddref = NULL; 3625 diradd = mkdir->md_diradd; 3626 mkdir->md_state |= DEPCOMPLETE; 3627 complete_mkdir(mkdir); 3628 } 3629 jwork_insert(&diradd->da_jwork, jsegdep); 3630 if (jaddref->ja_state & NEWBLOCK) { 3631 inodedep->id_state |= ONDEPLIST; 3632 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_inodedephd, 3633 inodedep, id_deps); 3634 } 3635 free_jaddref(jaddref); 3636 } 3637 3638 /* 3639 * Called once a jnewblk journal is written. The allocdirect or allocindir 3640 * is placed in the bmsafemap to await notification of a written bitmap. If 3641 * the operation was canceled we add the segdep to the appropriate 3642 * dependency to free the journal space once the canceling operation 3643 * completes. 3644 */ 3645 static void 3646 handle_written_jnewblk(jnewblk) 3647 struct jnewblk *jnewblk; 3648 { 3649 struct bmsafemap *bmsafemap; 3650 struct freefrag *freefrag; 3651 struct freework *freework; 3652 struct jsegdep *jsegdep; 3653 struct newblk *newblk; 3654 3655 /* Grab the jsegdep. */ 3656 jsegdep = jnewblk->jn_jsegdep; 3657 jnewblk->jn_jsegdep = NULL; 3658 if (jnewblk->jn_dep == NULL) 3659 panic("handle_written_jnewblk: No dependency for the segdep."); 3660 switch (jnewblk->jn_dep->wk_type) { 3661 case D_NEWBLK: 3662 case D_ALLOCDIRECT: 3663 case D_ALLOCINDIR: 3664 /* 3665 * Add the written block to the bmsafemap so it can 3666 * be notified when the bitmap is on disk. 3667 */ 3668 newblk = WK_NEWBLK(jnewblk->jn_dep); 3669 newblk->nb_jnewblk = NULL; 3670 if ((newblk->nb_state & GOINGAWAY) == 0) { 3671 bmsafemap = newblk->nb_bmsafemap; 3672 newblk->nb_state |= ONDEPLIST; 3673 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, 3674 nb_deps); 3675 } 3676 jwork_insert(&newblk->nb_jwork, jsegdep); 3677 break; 3678 case D_FREEFRAG: 3679 /* 3680 * A newblock being removed by a freefrag when replaced by 3681 * frag extension. 3682 */ 3683 freefrag = WK_FREEFRAG(jnewblk->jn_dep); 3684 freefrag->ff_jdep = NULL; 3685 jwork_insert(&freefrag->ff_jwork, jsegdep); 3686 break; 3687 case D_FREEWORK: 3688 /* 3689 * A direct block was removed by truncate. 3690 */ 3691 freework = WK_FREEWORK(jnewblk->jn_dep); 3692 freework->fw_jnewblk = NULL; 3693 jwork_insert(&freework->fw_freeblks->fb_jwork, jsegdep); 3694 break; 3695 default: 3696 panic("handle_written_jnewblk: Unknown type %d.", 3697 jnewblk->jn_dep->wk_type); 3698 } 3699 jnewblk->jn_dep = NULL; 3700 free_jnewblk(jnewblk); 3701 } 3702 3703 /* 3704 * Cancel a jfreefrag that won't be needed, probably due to colliding with 3705 * an in-flight allocation that has not yet been committed. Divorce us 3706 * from the freefrag and mark it DEPCOMPLETE so that it may be added 3707 * to the worklist. 3708 */ 3709 static void 3710 cancel_jfreefrag(jfreefrag) 3711 struct jfreefrag *jfreefrag; 3712 { 3713 struct freefrag *freefrag; 3714 3715 if (jfreefrag->fr_jsegdep) { 3716 free_jsegdep(jfreefrag->fr_jsegdep); 3717 jfreefrag->fr_jsegdep = NULL; 3718 } 3719 freefrag = jfreefrag->fr_freefrag; 3720 jfreefrag->fr_freefrag = NULL; 3721 free_jfreefrag(jfreefrag); 3722 freefrag->ff_state |= DEPCOMPLETE; 3723 CTR1(KTR_SUJ, "cancel_jfreefrag: blkno %jd", freefrag->ff_blkno); 3724 } 3725 3726 /* 3727 * Free a jfreefrag when the parent freefrag is rendered obsolete. 3728 */ 3729 static void 3730 free_jfreefrag(jfreefrag) 3731 struct jfreefrag *jfreefrag; 3732 { 3733 3734 if (jfreefrag->fr_state & INPROGRESS) 3735 WORKLIST_REMOVE(&jfreefrag->fr_list); 3736 else if (jfreefrag->fr_state & ONWORKLIST) 3737 remove_from_journal(&jfreefrag->fr_list); 3738 if (jfreefrag->fr_freefrag != NULL) 3739 panic("free_jfreefrag: Still attached to a freefrag."); 3740 WORKITEM_FREE(jfreefrag, D_JFREEFRAG); 3741 } 3742 3743 /* 3744 * Called when the journal write for a jfreefrag completes. The parent 3745 * freefrag is added to the worklist if this completes its dependencies. 3746 */ 3747 static void 3748 handle_written_jfreefrag(jfreefrag) 3749 struct jfreefrag *jfreefrag; 3750 { 3751 struct jsegdep *jsegdep; 3752 struct freefrag *freefrag; 3753 3754 /* Grab the jsegdep. */ 3755 jsegdep = jfreefrag->fr_jsegdep; 3756 jfreefrag->fr_jsegdep = NULL; 3757 freefrag = jfreefrag->fr_freefrag; 3758 if (freefrag == NULL) 3759 panic("handle_written_jfreefrag: No freefrag."); 3760 freefrag->ff_state |= DEPCOMPLETE; 3761 freefrag->ff_jdep = NULL; 3762 jwork_insert(&freefrag->ff_jwork, jsegdep); 3763 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 3764 add_to_worklist(&freefrag->ff_list, 0); 3765 jfreefrag->fr_freefrag = NULL; 3766 free_jfreefrag(jfreefrag); 3767 } 3768 3769 /* 3770 * Called when the journal write for a jfreeblk completes. The jfreeblk 3771 * is removed from the freeblks list of pending journal writes and the 3772 * jsegdep is moved to the freeblks jwork to be completed when all blocks 3773 * have been reclaimed. 3774 */ 3775 static void 3776 handle_written_jblkdep(jblkdep) 3777 struct jblkdep *jblkdep; 3778 { 3779 struct freeblks *freeblks; 3780 struct jsegdep *jsegdep; 3781 3782 /* Grab the jsegdep. */ 3783 jsegdep = jblkdep->jb_jsegdep; 3784 jblkdep->jb_jsegdep = NULL; 3785 freeblks = jblkdep->jb_freeblks; 3786 LIST_REMOVE(jblkdep, jb_deps); 3787 jwork_insert(&freeblks->fb_jwork, jsegdep); 3788 /* 3789 * If the freeblks is all journaled, we can add it to the worklist. 3790 */ 3791 if (LIST_EMPTY(&freeblks->fb_jblkdephd) && 3792 (freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 3793 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 3794 3795 free_jblkdep(jblkdep); 3796 } 3797 3798 static struct jsegdep * 3799 newjsegdep(struct worklist *wk) 3800 { 3801 struct jsegdep *jsegdep; 3802 3803 jsegdep = malloc(sizeof(*jsegdep), M_JSEGDEP, M_SOFTDEP_FLAGS); 3804 workitem_alloc(&jsegdep->jd_list, D_JSEGDEP, wk->wk_mp); 3805 jsegdep->jd_seg = NULL; 3806 3807 return (jsegdep); 3808 } 3809 3810 static struct jmvref * 3811 newjmvref(dp, ino, oldoff, newoff) 3812 struct inode *dp; 3813 ino_t ino; 3814 off_t oldoff; 3815 off_t newoff; 3816 { 3817 struct jmvref *jmvref; 3818 3819 jmvref = malloc(sizeof(*jmvref), M_JMVREF, M_SOFTDEP_FLAGS); 3820 workitem_alloc(&jmvref->jm_list, D_JMVREF, UFSTOVFS(dp->i_ump)); 3821 jmvref->jm_list.wk_state = ATTACHED | DEPCOMPLETE; 3822 jmvref->jm_parent = dp->i_number; 3823 jmvref->jm_ino = ino; 3824 jmvref->jm_oldoff = oldoff; 3825 jmvref->jm_newoff = newoff; 3826 3827 return (jmvref); 3828 } 3829 3830 /* 3831 * Allocate a new jremref that tracks the removal of ip from dp with the 3832 * directory entry offset of diroff. Mark the entry as ATTACHED and 3833 * DEPCOMPLETE as we have all the information required for the journal write 3834 * and the directory has already been removed from the buffer. The caller 3835 * is responsible for linking the jremref into the pagedep and adding it 3836 * to the journal to write. The MKDIR_PARENT flag is set if we're doing 3837 * a DOTDOT addition so handle_workitem_remove() can properly assign 3838 * the jsegdep when we're done. 3839 */ 3840 static struct jremref * 3841 newjremref(struct dirrem *dirrem, struct inode *dp, struct inode *ip, 3842 off_t diroff, nlink_t nlink) 3843 { 3844 struct jremref *jremref; 3845 3846 jremref = malloc(sizeof(*jremref), M_JREMREF, M_SOFTDEP_FLAGS); 3847 workitem_alloc(&jremref->jr_list, D_JREMREF, UFSTOVFS(dp->i_ump)); 3848 jremref->jr_state = ATTACHED; 3849 newinoref(&jremref->jr_ref, ip->i_number, dp->i_number, diroff, 3850 nlink, ip->i_mode); 3851 jremref->jr_dirrem = dirrem; 3852 3853 return (jremref); 3854 } 3855 3856 static inline void 3857 newinoref(struct inoref *inoref, ino_t ino, ino_t parent, off_t diroff, 3858 nlink_t nlink, uint16_t mode) 3859 { 3860 3861 inoref->if_jsegdep = newjsegdep(&inoref->if_list); 3862 inoref->if_diroff = diroff; 3863 inoref->if_ino = ino; 3864 inoref->if_parent = parent; 3865 inoref->if_nlink = nlink; 3866 inoref->if_mode = mode; 3867 } 3868 3869 /* 3870 * Allocate a new jaddref to track the addition of ino to dp at diroff. The 3871 * directory offset may not be known until later. The caller is responsible 3872 * adding the entry to the journal when this information is available. nlink 3873 * should be the link count prior to the addition and mode is only required 3874 * to have the correct FMT. 3875 */ 3876 static struct jaddref * 3877 newjaddref(struct inode *dp, ino_t ino, off_t diroff, int16_t nlink, 3878 uint16_t mode) 3879 { 3880 struct jaddref *jaddref; 3881 3882 jaddref = malloc(sizeof(*jaddref), M_JADDREF, M_SOFTDEP_FLAGS); 3883 workitem_alloc(&jaddref->ja_list, D_JADDREF, UFSTOVFS(dp->i_ump)); 3884 jaddref->ja_state = ATTACHED; 3885 jaddref->ja_mkdir = NULL; 3886 newinoref(&jaddref->ja_ref, ino, dp->i_number, diroff, nlink, mode); 3887 3888 return (jaddref); 3889 } 3890 3891 /* 3892 * Create a new free dependency for a freework. The caller is responsible 3893 * for adjusting the reference count when it has the lock held. The freedep 3894 * will track an outstanding bitmap write that will ultimately clear the 3895 * freework to continue. 3896 */ 3897 static struct freedep * 3898 newfreedep(struct freework *freework) 3899 { 3900 struct freedep *freedep; 3901 3902 freedep = malloc(sizeof(*freedep), M_FREEDEP, M_SOFTDEP_FLAGS); 3903 workitem_alloc(&freedep->fd_list, D_FREEDEP, freework->fw_list.wk_mp); 3904 freedep->fd_freework = freework; 3905 3906 return (freedep); 3907 } 3908 3909 /* 3910 * Free a freedep structure once the buffer it is linked to is written. If 3911 * this is the last reference to the freework schedule it for completion. 3912 */ 3913 static void 3914 free_freedep(freedep) 3915 struct freedep *freedep; 3916 { 3917 struct freework *freework; 3918 3919 freework = freedep->fd_freework; 3920 freework->fw_freeblks->fb_cgwait--; 3921 if (--freework->fw_ref == 0) 3922 freework_enqueue(freework); 3923 WORKITEM_FREE(freedep, D_FREEDEP); 3924 } 3925 3926 /* 3927 * Allocate a new freework structure that may be a level in an indirect 3928 * when parent is not NULL or a top level block when it is. The top level 3929 * freework structures are allocated without lk held and before the freeblks 3930 * is visible outside of softdep_setup_freeblocks(). 3931 */ 3932 static struct freework * 3933 newfreework(ump, freeblks, parent, lbn, nb, frags, off, journal) 3934 struct ufsmount *ump; 3935 struct freeblks *freeblks; 3936 struct freework *parent; 3937 ufs_lbn_t lbn; 3938 ufs2_daddr_t nb; 3939 int frags; 3940 int off; 3941 int journal; 3942 { 3943 struct freework *freework; 3944 3945 freework = malloc(sizeof(*freework), M_FREEWORK, M_SOFTDEP_FLAGS); 3946 workitem_alloc(&freework->fw_list, D_FREEWORK, freeblks->fb_list.wk_mp); 3947 freework->fw_state = ATTACHED; 3948 freework->fw_jnewblk = NULL; 3949 freework->fw_freeblks = freeblks; 3950 freework->fw_parent = parent; 3951 freework->fw_lbn = lbn; 3952 freework->fw_blkno = nb; 3953 freework->fw_frags = frags; 3954 freework->fw_indir = NULL; 3955 freework->fw_ref = (MOUNTEDSUJ(UFSTOVFS(ump)) == 0 || lbn >= -NXADDR) 3956 ? 0 : NINDIR(ump->um_fs) + 1; 3957 freework->fw_start = freework->fw_off = off; 3958 if (journal) 3959 newjfreeblk(freeblks, lbn, nb, frags); 3960 if (parent == NULL) { 3961 ACQUIRE_LOCK(&lk); 3962 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 3963 freeblks->fb_ref++; 3964 FREE_LOCK(&lk); 3965 } 3966 3967 return (freework); 3968 } 3969 3970 /* 3971 * Eliminate a jfreeblk for a block that does not need journaling. 3972 */ 3973 static void 3974 cancel_jfreeblk(freeblks, blkno) 3975 struct freeblks *freeblks; 3976 ufs2_daddr_t blkno; 3977 { 3978 struct jfreeblk *jfreeblk; 3979 struct jblkdep *jblkdep; 3980 3981 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) { 3982 if (jblkdep->jb_list.wk_type != D_JFREEBLK) 3983 continue; 3984 jfreeblk = WK_JFREEBLK(&jblkdep->jb_list); 3985 if (jfreeblk->jf_blkno == blkno) 3986 break; 3987 } 3988 if (jblkdep == NULL) 3989 return; 3990 CTR1(KTR_SUJ, "cancel_jfreeblk: blkno %jd", blkno); 3991 free_jsegdep(jblkdep->jb_jsegdep); 3992 LIST_REMOVE(jblkdep, jb_deps); 3993 WORKITEM_FREE(jfreeblk, D_JFREEBLK); 3994 } 3995 3996 /* 3997 * Allocate a new jfreeblk to journal top level block pointer when truncating 3998 * a file. The caller must add this to the worklist when lk is held. 3999 */ 4000 static struct jfreeblk * 4001 newjfreeblk(freeblks, lbn, blkno, frags) 4002 struct freeblks *freeblks; 4003 ufs_lbn_t lbn; 4004 ufs2_daddr_t blkno; 4005 int frags; 4006 { 4007 struct jfreeblk *jfreeblk; 4008 4009 jfreeblk = malloc(sizeof(*jfreeblk), M_JFREEBLK, M_SOFTDEP_FLAGS); 4010 workitem_alloc(&jfreeblk->jf_dep.jb_list, D_JFREEBLK, 4011 freeblks->fb_list.wk_mp); 4012 jfreeblk->jf_dep.jb_jsegdep = newjsegdep(&jfreeblk->jf_dep.jb_list); 4013 jfreeblk->jf_dep.jb_freeblks = freeblks; 4014 jfreeblk->jf_ino = freeblks->fb_inum; 4015 jfreeblk->jf_lbn = lbn; 4016 jfreeblk->jf_blkno = blkno; 4017 jfreeblk->jf_frags = frags; 4018 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jfreeblk->jf_dep, jb_deps); 4019 4020 return (jfreeblk); 4021 } 4022 4023 /* 4024 * Allocate a new jtrunc to track a partial truncation. 4025 */ 4026 static struct jtrunc * 4027 newjtrunc(freeblks, size, extsize) 4028 struct freeblks *freeblks; 4029 off_t size; 4030 int extsize; 4031 { 4032 struct jtrunc *jtrunc; 4033 4034 jtrunc = malloc(sizeof(*jtrunc), M_JTRUNC, M_SOFTDEP_FLAGS); 4035 workitem_alloc(&jtrunc->jt_dep.jb_list, D_JTRUNC, 4036 freeblks->fb_list.wk_mp); 4037 jtrunc->jt_dep.jb_jsegdep = newjsegdep(&jtrunc->jt_dep.jb_list); 4038 jtrunc->jt_dep.jb_freeblks = freeblks; 4039 jtrunc->jt_ino = freeblks->fb_inum; 4040 jtrunc->jt_size = size; 4041 jtrunc->jt_extsize = extsize; 4042 LIST_INSERT_HEAD(&freeblks->fb_jblkdephd, &jtrunc->jt_dep, jb_deps); 4043 4044 return (jtrunc); 4045 } 4046 4047 /* 4048 * If we're canceling a new bitmap we have to search for another ref 4049 * to move into the bmsafemap dep. This might be better expressed 4050 * with another structure. 4051 */ 4052 static void 4053 move_newblock_dep(jaddref, inodedep) 4054 struct jaddref *jaddref; 4055 struct inodedep *inodedep; 4056 { 4057 struct inoref *inoref; 4058 struct jaddref *jaddrefn; 4059 4060 jaddrefn = NULL; 4061 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4062 inoref = TAILQ_NEXT(inoref, if_deps)) { 4063 if ((jaddref->ja_state & NEWBLOCK) && 4064 inoref->if_list.wk_type == D_JADDREF) { 4065 jaddrefn = (struct jaddref *)inoref; 4066 break; 4067 } 4068 } 4069 if (jaddrefn == NULL) 4070 return; 4071 jaddrefn->ja_state &= ~(ATTACHED | UNDONE); 4072 jaddrefn->ja_state |= jaddref->ja_state & 4073 (ATTACHED | UNDONE | NEWBLOCK); 4074 jaddref->ja_state &= ~(ATTACHED | UNDONE | NEWBLOCK); 4075 jaddref->ja_state |= ATTACHED; 4076 LIST_REMOVE(jaddref, ja_bmdeps); 4077 LIST_INSERT_HEAD(&inodedep->id_bmsafemap->sm_jaddrefhd, jaddrefn, 4078 ja_bmdeps); 4079 } 4080 4081 /* 4082 * Cancel a jaddref either before it has been written or while it is being 4083 * written. This happens when a link is removed before the add reaches 4084 * the disk. The jaddref dependency is kept linked into the bmsafemap 4085 * and inode to prevent the link count or bitmap from reaching the disk 4086 * until handle_workitem_remove() re-adjusts the counts and bitmaps as 4087 * required. 4088 * 4089 * Returns 1 if the canceled addref requires journaling of the remove and 4090 * 0 otherwise. 4091 */ 4092 static int 4093 cancel_jaddref(jaddref, inodedep, wkhd) 4094 struct jaddref *jaddref; 4095 struct inodedep *inodedep; 4096 struct workhead *wkhd; 4097 { 4098 struct inoref *inoref; 4099 struct jsegdep *jsegdep; 4100 int needsj; 4101 4102 KASSERT((jaddref->ja_state & COMPLETE) == 0, 4103 ("cancel_jaddref: Canceling complete jaddref")); 4104 if (jaddref->ja_state & (INPROGRESS | COMPLETE)) 4105 needsj = 1; 4106 else 4107 needsj = 0; 4108 if (inodedep == NULL) 4109 if (inodedep_lookup(jaddref->ja_list.wk_mp, jaddref->ja_ino, 4110 0, &inodedep) == 0) 4111 panic("cancel_jaddref: Lost inodedep"); 4112 /* 4113 * We must adjust the nlink of any reference operation that follows 4114 * us so that it is consistent with the in-memory reference. This 4115 * ensures that inode nlink rollbacks always have the correct link. 4116 */ 4117 if (needsj == 0) { 4118 for (inoref = TAILQ_NEXT(&jaddref->ja_ref, if_deps); inoref; 4119 inoref = TAILQ_NEXT(inoref, if_deps)) { 4120 if (inoref->if_state & GOINGAWAY) 4121 break; 4122 inoref->if_nlink--; 4123 } 4124 } 4125 jsegdep = inoref_jseg(&jaddref->ja_ref); 4126 if (jaddref->ja_state & NEWBLOCK) 4127 move_newblock_dep(jaddref, inodedep); 4128 wake_worklist(&jaddref->ja_list); 4129 jaddref->ja_mkdir = NULL; 4130 if (jaddref->ja_state & INPROGRESS) { 4131 jaddref->ja_state &= ~INPROGRESS; 4132 WORKLIST_REMOVE(&jaddref->ja_list); 4133 jwork_insert(wkhd, jsegdep); 4134 } else { 4135 free_jsegdep(jsegdep); 4136 if (jaddref->ja_state & DEPCOMPLETE) 4137 remove_from_journal(&jaddref->ja_list); 4138 } 4139 jaddref->ja_state |= (GOINGAWAY | DEPCOMPLETE); 4140 /* 4141 * Leave NEWBLOCK jaddrefs on the inodedep so handle_workitem_remove 4142 * can arrange for them to be freed with the bitmap. Otherwise we 4143 * no longer need this addref attached to the inoreflst and it 4144 * will incorrectly adjust nlink if we leave it. 4145 */ 4146 if ((jaddref->ja_state & NEWBLOCK) == 0) { 4147 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 4148 if_deps); 4149 jaddref->ja_state |= COMPLETE; 4150 free_jaddref(jaddref); 4151 return (needsj); 4152 } 4153 /* 4154 * Leave the head of the list for jsegdeps for fast merging. 4155 */ 4156 if (LIST_FIRST(wkhd) != NULL) { 4157 jaddref->ja_state |= ONWORKLIST; 4158 LIST_INSERT_AFTER(LIST_FIRST(wkhd), &jaddref->ja_list, wk_list); 4159 } else 4160 WORKLIST_INSERT(wkhd, &jaddref->ja_list); 4161 4162 return (needsj); 4163 } 4164 4165 /* 4166 * Attempt to free a jaddref structure when some work completes. This 4167 * should only succeed once the entry is written and all dependencies have 4168 * been notified. 4169 */ 4170 static void 4171 free_jaddref(jaddref) 4172 struct jaddref *jaddref; 4173 { 4174 4175 if ((jaddref->ja_state & ALLCOMPLETE) != ALLCOMPLETE) 4176 return; 4177 if (jaddref->ja_ref.if_jsegdep) 4178 panic("free_jaddref: segdep attached to jaddref %p(0x%X)\n", 4179 jaddref, jaddref->ja_state); 4180 if (jaddref->ja_state & NEWBLOCK) 4181 LIST_REMOVE(jaddref, ja_bmdeps); 4182 if (jaddref->ja_state & (INPROGRESS | ONWORKLIST)) 4183 panic("free_jaddref: Bad state %p(0x%X)", 4184 jaddref, jaddref->ja_state); 4185 if (jaddref->ja_mkdir != NULL) 4186 panic("free_jaddref: Work pending, 0x%X\n", jaddref->ja_state); 4187 WORKITEM_FREE(jaddref, D_JADDREF); 4188 } 4189 4190 /* 4191 * Free a jremref structure once it has been written or discarded. 4192 */ 4193 static void 4194 free_jremref(jremref) 4195 struct jremref *jremref; 4196 { 4197 4198 if (jremref->jr_ref.if_jsegdep) 4199 free_jsegdep(jremref->jr_ref.if_jsegdep); 4200 if (jremref->jr_state & INPROGRESS) 4201 panic("free_jremref: IO still pending"); 4202 WORKITEM_FREE(jremref, D_JREMREF); 4203 } 4204 4205 /* 4206 * Free a jnewblk structure. 4207 */ 4208 static void 4209 free_jnewblk(jnewblk) 4210 struct jnewblk *jnewblk; 4211 { 4212 4213 if ((jnewblk->jn_state & ALLCOMPLETE) != ALLCOMPLETE) 4214 return; 4215 LIST_REMOVE(jnewblk, jn_deps); 4216 if (jnewblk->jn_dep != NULL) 4217 panic("free_jnewblk: Dependency still attached."); 4218 WORKITEM_FREE(jnewblk, D_JNEWBLK); 4219 } 4220 4221 /* 4222 * Cancel a jnewblk which has been been made redundant by frag extension. 4223 */ 4224 static void 4225 cancel_jnewblk(jnewblk, wkhd) 4226 struct jnewblk *jnewblk; 4227 struct workhead *wkhd; 4228 { 4229 struct jsegdep *jsegdep; 4230 4231 CTR1(KTR_SUJ, "cancel_jnewblk: blkno %jd", jnewblk->jn_blkno); 4232 jsegdep = jnewblk->jn_jsegdep; 4233 if (jnewblk->jn_jsegdep == NULL || jnewblk->jn_dep == NULL) 4234 panic("cancel_jnewblk: Invalid state"); 4235 jnewblk->jn_jsegdep = NULL; 4236 jnewblk->jn_dep = NULL; 4237 jnewblk->jn_state |= GOINGAWAY; 4238 if (jnewblk->jn_state & INPROGRESS) { 4239 jnewblk->jn_state &= ~INPROGRESS; 4240 WORKLIST_REMOVE(&jnewblk->jn_list); 4241 jwork_insert(wkhd, jsegdep); 4242 } else { 4243 free_jsegdep(jsegdep); 4244 remove_from_journal(&jnewblk->jn_list); 4245 } 4246 wake_worklist(&jnewblk->jn_list); 4247 WORKLIST_INSERT(wkhd, &jnewblk->jn_list); 4248 } 4249 4250 static void 4251 free_jblkdep(jblkdep) 4252 struct jblkdep *jblkdep; 4253 { 4254 4255 if (jblkdep->jb_list.wk_type == D_JFREEBLK) 4256 WORKITEM_FREE(jblkdep, D_JFREEBLK); 4257 else if (jblkdep->jb_list.wk_type == D_JTRUNC) 4258 WORKITEM_FREE(jblkdep, D_JTRUNC); 4259 else 4260 panic("free_jblkdep: Unexpected type %s", 4261 TYPENAME(jblkdep->jb_list.wk_type)); 4262 } 4263 4264 /* 4265 * Free a single jseg once it is no longer referenced in memory or on 4266 * disk. Reclaim journal blocks and dependencies waiting for the segment 4267 * to disappear. 4268 */ 4269 static void 4270 free_jseg(jseg, jblocks) 4271 struct jseg *jseg; 4272 struct jblocks *jblocks; 4273 { 4274 struct freework *freework; 4275 4276 /* 4277 * Free freework structures that were lingering to indicate freed 4278 * indirect blocks that forced journal write ordering on reallocate. 4279 */ 4280 while ((freework = LIST_FIRST(&jseg->js_indirs)) != NULL) 4281 indirblk_remove(freework); 4282 if (jblocks->jb_oldestseg == jseg) 4283 jblocks->jb_oldestseg = TAILQ_NEXT(jseg, js_next); 4284 TAILQ_REMOVE(&jblocks->jb_segs, jseg, js_next); 4285 jblocks_free(jblocks, jseg->js_list.wk_mp, jseg->js_size); 4286 KASSERT(LIST_EMPTY(&jseg->js_entries), 4287 ("free_jseg: Freed jseg has valid entries.")); 4288 WORKITEM_FREE(jseg, D_JSEG); 4289 } 4290 4291 /* 4292 * Free all jsegs that meet the criteria for being reclaimed and update 4293 * oldestseg. 4294 */ 4295 static void 4296 free_jsegs(jblocks) 4297 struct jblocks *jblocks; 4298 { 4299 struct jseg *jseg; 4300 4301 /* 4302 * Free only those jsegs which have none allocated before them to 4303 * preserve the journal space ordering. 4304 */ 4305 while ((jseg = TAILQ_FIRST(&jblocks->jb_segs)) != NULL) { 4306 /* 4307 * Only reclaim space when nothing depends on this journal 4308 * set and another set has written that it is no longer 4309 * valid. 4310 */ 4311 if (jseg->js_refs != 0) { 4312 jblocks->jb_oldestseg = jseg; 4313 return; 4314 } 4315 if ((jseg->js_state & ALLCOMPLETE) != ALLCOMPLETE) 4316 break; 4317 if (jseg->js_seq > jblocks->jb_oldestwrseq) 4318 break; 4319 /* 4320 * We can free jsegs that didn't write entries when 4321 * oldestwrseq == js_seq. 4322 */ 4323 if (jseg->js_seq == jblocks->jb_oldestwrseq && 4324 jseg->js_cnt != 0) 4325 break; 4326 free_jseg(jseg, jblocks); 4327 } 4328 /* 4329 * If we exited the loop above we still must discover the 4330 * oldest valid segment. 4331 */ 4332 if (jseg) 4333 for (jseg = jblocks->jb_oldestseg; jseg != NULL; 4334 jseg = TAILQ_NEXT(jseg, js_next)) 4335 if (jseg->js_refs != 0) 4336 break; 4337 jblocks->jb_oldestseg = jseg; 4338 /* 4339 * The journal has no valid records but some jsegs may still be 4340 * waiting on oldestwrseq to advance. We force a small record 4341 * out to permit these lingering records to be reclaimed. 4342 */ 4343 if (jblocks->jb_oldestseg == NULL && !TAILQ_EMPTY(&jblocks->jb_segs)) 4344 jblocks->jb_needseg = 1; 4345 } 4346 4347 /* 4348 * Release one reference to a jseg and free it if the count reaches 0. This 4349 * should eventually reclaim journal space as well. 4350 */ 4351 static void 4352 rele_jseg(jseg) 4353 struct jseg *jseg; 4354 { 4355 4356 KASSERT(jseg->js_refs > 0, 4357 ("free_jseg: Invalid refcnt %d", jseg->js_refs)); 4358 if (--jseg->js_refs != 0) 4359 return; 4360 free_jsegs(jseg->js_jblocks); 4361 } 4362 4363 /* 4364 * Release a jsegdep and decrement the jseg count. 4365 */ 4366 static void 4367 free_jsegdep(jsegdep) 4368 struct jsegdep *jsegdep; 4369 { 4370 4371 if (jsegdep->jd_seg) 4372 rele_jseg(jsegdep->jd_seg); 4373 WORKITEM_FREE(jsegdep, D_JSEGDEP); 4374 } 4375 4376 /* 4377 * Wait for a journal item to make it to disk. Initiate journal processing 4378 * if required. 4379 */ 4380 static int 4381 jwait(wk, waitfor) 4382 struct worklist *wk; 4383 int waitfor; 4384 { 4385 4386 /* 4387 * Blocking journal waits cause slow synchronous behavior. Record 4388 * stats on the frequency of these blocking operations. 4389 */ 4390 if (waitfor == MNT_WAIT) { 4391 stat_journal_wait++; 4392 switch (wk->wk_type) { 4393 case D_JREMREF: 4394 case D_JMVREF: 4395 stat_jwait_filepage++; 4396 break; 4397 case D_JTRUNC: 4398 case D_JFREEBLK: 4399 stat_jwait_freeblks++; 4400 break; 4401 case D_JNEWBLK: 4402 stat_jwait_newblk++; 4403 break; 4404 case D_JADDREF: 4405 stat_jwait_inode++; 4406 break; 4407 default: 4408 break; 4409 } 4410 } 4411 /* 4412 * If IO has not started we process the journal. We can't mark the 4413 * worklist item as IOWAITING because we drop the lock while 4414 * processing the journal and the worklist entry may be freed after 4415 * this point. The caller may call back in and re-issue the request. 4416 */ 4417 if ((wk->wk_state & INPROGRESS) == 0) { 4418 softdep_process_journal(wk->wk_mp, wk, waitfor); 4419 if (waitfor != MNT_WAIT) 4420 return (EBUSY); 4421 return (0); 4422 } 4423 if (waitfor != MNT_WAIT) 4424 return (EBUSY); 4425 wait_worklist(wk, "jwait"); 4426 return (0); 4427 } 4428 4429 /* 4430 * Lookup an inodedep based on an inode pointer and set the nlinkdelta as 4431 * appropriate. This is a convenience function to reduce duplicate code 4432 * for the setup and revert functions below. 4433 */ 4434 static struct inodedep * 4435 inodedep_lookup_ip(ip) 4436 struct inode *ip; 4437 { 4438 struct inodedep *inodedep; 4439 int dflags; 4440 4441 KASSERT(ip->i_nlink >= ip->i_effnlink, 4442 ("inodedep_lookup_ip: bad delta")); 4443 dflags = DEPALLOC; 4444 if (IS_SNAPSHOT(ip)) 4445 dflags |= NODELAY; 4446 (void) inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, 4447 &inodedep); 4448 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 4449 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 4450 4451 return (inodedep); 4452 } 4453 4454 /* 4455 * Called prior to creating a new inode and linking it to a directory. The 4456 * jaddref structure must already be allocated by softdep_setup_inomapdep 4457 * and it is discovered here so we can initialize the mode and update 4458 * nlinkdelta. 4459 */ 4460 void 4461 softdep_setup_create(dp, ip) 4462 struct inode *dp; 4463 struct inode *ip; 4464 { 4465 struct inodedep *inodedep; 4466 struct jaddref *jaddref; 4467 struct vnode *dvp; 4468 4469 KASSERT(ip->i_nlink == 1, 4470 ("softdep_setup_create: Invalid link count.")); 4471 dvp = ITOV(dp); 4472 ACQUIRE_LOCK(&lk); 4473 inodedep = inodedep_lookup_ip(ip); 4474 if (DOINGSUJ(dvp)) { 4475 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4476 inoreflst); 4477 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 4478 ("softdep_setup_create: No addref structure present.")); 4479 } 4480 softdep_prelink(dvp, NULL); 4481 FREE_LOCK(&lk); 4482 } 4483 4484 /* 4485 * Create a jaddref structure to track the addition of a DOTDOT link when 4486 * we are reparenting an inode as part of a rename. This jaddref will be 4487 * found by softdep_setup_directory_change. Adjusts nlinkdelta for 4488 * non-journaling softdep. 4489 */ 4490 void 4491 softdep_setup_dotdot_link(dp, ip) 4492 struct inode *dp; 4493 struct inode *ip; 4494 { 4495 struct inodedep *inodedep; 4496 struct jaddref *jaddref; 4497 struct vnode *dvp; 4498 struct vnode *vp; 4499 4500 dvp = ITOV(dp); 4501 vp = ITOV(ip); 4502 jaddref = NULL; 4503 /* 4504 * We don't set MKDIR_PARENT as this is not tied to a mkdir and 4505 * is used as a normal link would be. 4506 */ 4507 if (DOINGSUJ(dvp)) 4508 jaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4509 dp->i_effnlink - 1, dp->i_mode); 4510 ACQUIRE_LOCK(&lk); 4511 inodedep = inodedep_lookup_ip(dp); 4512 if (jaddref) 4513 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4514 if_deps); 4515 softdep_prelink(dvp, ITOV(ip)); 4516 FREE_LOCK(&lk); 4517 } 4518 4519 /* 4520 * Create a jaddref structure to track a new link to an inode. The directory 4521 * offset is not known until softdep_setup_directory_add or 4522 * softdep_setup_directory_change. Adjusts nlinkdelta for non-journaling 4523 * softdep. 4524 */ 4525 void 4526 softdep_setup_link(dp, ip) 4527 struct inode *dp; 4528 struct inode *ip; 4529 { 4530 struct inodedep *inodedep; 4531 struct jaddref *jaddref; 4532 struct vnode *dvp; 4533 4534 dvp = ITOV(dp); 4535 jaddref = NULL; 4536 if (DOINGSUJ(dvp)) 4537 jaddref = newjaddref(dp, ip->i_number, 0, ip->i_effnlink - 1, 4538 ip->i_mode); 4539 ACQUIRE_LOCK(&lk); 4540 inodedep = inodedep_lookup_ip(ip); 4541 if (jaddref) 4542 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4543 if_deps); 4544 softdep_prelink(dvp, ITOV(ip)); 4545 FREE_LOCK(&lk); 4546 } 4547 4548 /* 4549 * Called to create the jaddref structures to track . and .. references as 4550 * well as lookup and further initialize the incomplete jaddref created 4551 * by softdep_setup_inomapdep when the inode was allocated. Adjusts 4552 * nlinkdelta for non-journaling softdep. 4553 */ 4554 void 4555 softdep_setup_mkdir(dp, ip) 4556 struct inode *dp; 4557 struct inode *ip; 4558 { 4559 struct inodedep *inodedep; 4560 struct jaddref *dotdotaddref; 4561 struct jaddref *dotaddref; 4562 struct jaddref *jaddref; 4563 struct vnode *dvp; 4564 4565 dvp = ITOV(dp); 4566 dotaddref = dotdotaddref = NULL; 4567 if (DOINGSUJ(dvp)) { 4568 dotaddref = newjaddref(ip, ip->i_number, DOT_OFFSET, 1, 4569 ip->i_mode); 4570 dotaddref->ja_state |= MKDIR_BODY; 4571 dotdotaddref = newjaddref(ip, dp->i_number, DOTDOT_OFFSET, 4572 dp->i_effnlink - 1, dp->i_mode); 4573 dotdotaddref->ja_state |= MKDIR_PARENT; 4574 } 4575 ACQUIRE_LOCK(&lk); 4576 inodedep = inodedep_lookup_ip(ip); 4577 if (DOINGSUJ(dvp)) { 4578 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4579 inoreflst); 4580 KASSERT(jaddref != NULL, 4581 ("softdep_setup_mkdir: No addref structure present.")); 4582 KASSERT(jaddref->ja_parent == dp->i_number, 4583 ("softdep_setup_mkdir: bad parent %ju", 4584 (uintmax_t)jaddref->ja_parent)); 4585 TAILQ_INSERT_BEFORE(&jaddref->ja_ref, &dotaddref->ja_ref, 4586 if_deps); 4587 } 4588 inodedep = inodedep_lookup_ip(dp); 4589 if (DOINGSUJ(dvp)) 4590 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, 4591 &dotdotaddref->ja_ref, if_deps); 4592 softdep_prelink(ITOV(dp), NULL); 4593 FREE_LOCK(&lk); 4594 } 4595 4596 /* 4597 * Called to track nlinkdelta of the inode and parent directories prior to 4598 * unlinking a directory. 4599 */ 4600 void 4601 softdep_setup_rmdir(dp, ip) 4602 struct inode *dp; 4603 struct inode *ip; 4604 { 4605 struct vnode *dvp; 4606 4607 dvp = ITOV(dp); 4608 ACQUIRE_LOCK(&lk); 4609 (void) inodedep_lookup_ip(ip); 4610 (void) inodedep_lookup_ip(dp); 4611 softdep_prelink(dvp, ITOV(ip)); 4612 FREE_LOCK(&lk); 4613 } 4614 4615 /* 4616 * Called to track nlinkdelta of the inode and parent directories prior to 4617 * unlink. 4618 */ 4619 void 4620 softdep_setup_unlink(dp, ip) 4621 struct inode *dp; 4622 struct inode *ip; 4623 { 4624 struct vnode *dvp; 4625 4626 dvp = ITOV(dp); 4627 ACQUIRE_LOCK(&lk); 4628 (void) inodedep_lookup_ip(ip); 4629 (void) inodedep_lookup_ip(dp); 4630 softdep_prelink(dvp, ITOV(ip)); 4631 FREE_LOCK(&lk); 4632 } 4633 4634 /* 4635 * Called to release the journal structures created by a failed non-directory 4636 * creation. Adjusts nlinkdelta for non-journaling softdep. 4637 */ 4638 void 4639 softdep_revert_create(dp, ip) 4640 struct inode *dp; 4641 struct inode *ip; 4642 { 4643 struct inodedep *inodedep; 4644 struct jaddref *jaddref; 4645 struct vnode *dvp; 4646 4647 dvp = ITOV(dp); 4648 ACQUIRE_LOCK(&lk); 4649 inodedep = inodedep_lookup_ip(ip); 4650 if (DOINGSUJ(dvp)) { 4651 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4652 inoreflst); 4653 KASSERT(jaddref->ja_parent == dp->i_number, 4654 ("softdep_revert_create: addref parent mismatch")); 4655 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4656 } 4657 FREE_LOCK(&lk); 4658 } 4659 4660 /* 4661 * Called to release the journal structures created by a failed dotdot link 4662 * creation. Adjusts nlinkdelta for non-journaling softdep. 4663 */ 4664 void 4665 softdep_revert_dotdot_link(dp, ip) 4666 struct inode *dp; 4667 struct inode *ip; 4668 { 4669 struct inodedep *inodedep; 4670 struct jaddref *jaddref; 4671 struct vnode *dvp; 4672 4673 dvp = ITOV(dp); 4674 ACQUIRE_LOCK(&lk); 4675 inodedep = inodedep_lookup_ip(dp); 4676 if (DOINGSUJ(dvp)) { 4677 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4678 inoreflst); 4679 KASSERT(jaddref->ja_parent == ip->i_number, 4680 ("softdep_revert_dotdot_link: addref parent mismatch")); 4681 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4682 } 4683 FREE_LOCK(&lk); 4684 } 4685 4686 /* 4687 * Called to release the journal structures created by a failed link 4688 * addition. Adjusts nlinkdelta for non-journaling softdep. 4689 */ 4690 void 4691 softdep_revert_link(dp, ip) 4692 struct inode *dp; 4693 struct inode *ip; 4694 { 4695 struct inodedep *inodedep; 4696 struct jaddref *jaddref; 4697 struct vnode *dvp; 4698 4699 dvp = ITOV(dp); 4700 ACQUIRE_LOCK(&lk); 4701 inodedep = inodedep_lookup_ip(ip); 4702 if (DOINGSUJ(dvp)) { 4703 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4704 inoreflst); 4705 KASSERT(jaddref->ja_parent == dp->i_number, 4706 ("softdep_revert_link: addref parent mismatch")); 4707 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4708 } 4709 FREE_LOCK(&lk); 4710 } 4711 4712 /* 4713 * Called to release the journal structures created by a failed mkdir 4714 * attempt. Adjusts nlinkdelta for non-journaling softdep. 4715 */ 4716 void 4717 softdep_revert_mkdir(dp, ip) 4718 struct inode *dp; 4719 struct inode *ip; 4720 { 4721 struct inodedep *inodedep; 4722 struct jaddref *jaddref; 4723 struct jaddref *dotaddref; 4724 struct vnode *dvp; 4725 4726 dvp = ITOV(dp); 4727 4728 ACQUIRE_LOCK(&lk); 4729 inodedep = inodedep_lookup_ip(dp); 4730 if (DOINGSUJ(dvp)) { 4731 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4732 inoreflst); 4733 KASSERT(jaddref->ja_parent == ip->i_number, 4734 ("softdep_revert_mkdir: dotdot addref parent mismatch")); 4735 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4736 } 4737 inodedep = inodedep_lookup_ip(ip); 4738 if (DOINGSUJ(dvp)) { 4739 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 4740 inoreflst); 4741 KASSERT(jaddref->ja_parent == dp->i_number, 4742 ("softdep_revert_mkdir: addref parent mismatch")); 4743 dotaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 4744 inoreflst, if_deps); 4745 cancel_jaddref(jaddref, inodedep, &inodedep->id_inowait); 4746 KASSERT(dotaddref->ja_parent == ip->i_number, 4747 ("softdep_revert_mkdir: dot addref parent mismatch")); 4748 cancel_jaddref(dotaddref, inodedep, &inodedep->id_inowait); 4749 } 4750 FREE_LOCK(&lk); 4751 } 4752 4753 /* 4754 * Called to correct nlinkdelta after a failed rmdir. 4755 */ 4756 void 4757 softdep_revert_rmdir(dp, ip) 4758 struct inode *dp; 4759 struct inode *ip; 4760 { 4761 4762 ACQUIRE_LOCK(&lk); 4763 (void) inodedep_lookup_ip(ip); 4764 (void) inodedep_lookup_ip(dp); 4765 FREE_LOCK(&lk); 4766 } 4767 4768 /* 4769 * Protecting the freemaps (or bitmaps). 4770 * 4771 * To eliminate the need to execute fsck before mounting a filesystem 4772 * after a power failure, one must (conservatively) guarantee that the 4773 * on-disk copy of the bitmaps never indicate that a live inode or block is 4774 * free. So, when a block or inode is allocated, the bitmap should be 4775 * updated (on disk) before any new pointers. When a block or inode is 4776 * freed, the bitmap should not be updated until all pointers have been 4777 * reset. The latter dependency is handled by the delayed de-allocation 4778 * approach described below for block and inode de-allocation. The former 4779 * dependency is handled by calling the following procedure when a block or 4780 * inode is allocated. When an inode is allocated an "inodedep" is created 4781 * with its DEPCOMPLETE flag cleared until its bitmap is written to disk. 4782 * Each "inodedep" is also inserted into the hash indexing structure so 4783 * that any additional link additions can be made dependent on the inode 4784 * allocation. 4785 * 4786 * The ufs filesystem maintains a number of free block counts (e.g., per 4787 * cylinder group, per cylinder and per <cylinder, rotational position> pair) 4788 * in addition to the bitmaps. These counts are used to improve efficiency 4789 * during allocation and therefore must be consistent with the bitmaps. 4790 * There is no convenient way to guarantee post-crash consistency of these 4791 * counts with simple update ordering, for two main reasons: (1) The counts 4792 * and bitmaps for a single cylinder group block are not in the same disk 4793 * sector. If a disk write is interrupted (e.g., by power failure), one may 4794 * be written and the other not. (2) Some of the counts are located in the 4795 * superblock rather than the cylinder group block. So, we focus our soft 4796 * updates implementation on protecting the bitmaps. When mounting a 4797 * filesystem, we recompute the auxiliary counts from the bitmaps. 4798 */ 4799 4800 /* 4801 * Called just after updating the cylinder group block to allocate an inode. 4802 */ 4803 void 4804 softdep_setup_inomapdep(bp, ip, newinum, mode) 4805 struct buf *bp; /* buffer for cylgroup block with inode map */ 4806 struct inode *ip; /* inode related to allocation */ 4807 ino_t newinum; /* new inode number being allocated */ 4808 int mode; 4809 { 4810 struct inodedep *inodedep; 4811 struct bmsafemap *bmsafemap; 4812 struct jaddref *jaddref; 4813 struct mount *mp; 4814 struct fs *fs; 4815 4816 mp = UFSTOVFS(ip->i_ump); 4817 fs = ip->i_ump->um_fs; 4818 jaddref = NULL; 4819 4820 /* 4821 * Allocate the journal reference add structure so that the bitmap 4822 * can be dependent on it. 4823 */ 4824 if (MOUNTEDSUJ(mp)) { 4825 jaddref = newjaddref(ip, newinum, 0, 0, mode); 4826 jaddref->ja_state |= NEWBLOCK; 4827 } 4828 4829 /* 4830 * Create a dependency for the newly allocated inode. 4831 * Panic if it already exists as something is seriously wrong. 4832 * Otherwise add it to the dependency list for the buffer holding 4833 * the cylinder group map from which it was allocated. 4834 * 4835 * We have to preallocate a bmsafemap entry in case it is needed 4836 * in bmsafemap_lookup since once we allocate the inodedep, we 4837 * have to finish initializing it before we can FREE_LOCK(). 4838 * By preallocating, we avoid FREE_LOCK() while doing a malloc 4839 * in bmsafemap_lookup. We cannot call bmsafemap_lookup before 4840 * creating the inodedep as it can be freed during the time 4841 * that we FREE_LOCK() while allocating the inodedep. We must 4842 * call workitem_alloc() before entering the locked section as 4843 * it also acquires the lock and we must avoid trying doing so 4844 * recursively. 4845 */ 4846 bmsafemap = malloc(sizeof(struct bmsafemap), 4847 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 4848 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 4849 ACQUIRE_LOCK(&lk); 4850 if ((inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep))) 4851 panic("softdep_setup_inomapdep: dependency %p for new" 4852 "inode already exists", inodedep); 4853 bmsafemap = bmsafemap_lookup(mp, bp, ino_to_cg(fs, newinum), bmsafemap); 4854 if (jaddref) { 4855 LIST_INSERT_HEAD(&bmsafemap->sm_jaddrefhd, jaddref, ja_bmdeps); 4856 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jaddref->ja_ref, 4857 if_deps); 4858 } else { 4859 inodedep->id_state |= ONDEPLIST; 4860 LIST_INSERT_HEAD(&bmsafemap->sm_inodedephd, inodedep, id_deps); 4861 } 4862 inodedep->id_bmsafemap = bmsafemap; 4863 inodedep->id_state &= ~DEPCOMPLETE; 4864 FREE_LOCK(&lk); 4865 } 4866 4867 /* 4868 * Called just after updating the cylinder group block to 4869 * allocate block or fragment. 4870 */ 4871 void 4872 softdep_setup_blkmapdep(bp, mp, newblkno, frags, oldfrags) 4873 struct buf *bp; /* buffer for cylgroup block with block map */ 4874 struct mount *mp; /* filesystem doing allocation */ 4875 ufs2_daddr_t newblkno; /* number of newly allocated block */ 4876 int frags; /* Number of fragments. */ 4877 int oldfrags; /* Previous number of fragments for extend. */ 4878 { 4879 struct newblk *newblk; 4880 struct bmsafemap *bmsafemap; 4881 struct jnewblk *jnewblk; 4882 struct fs *fs; 4883 4884 fs = VFSTOUFS(mp)->um_fs; 4885 jnewblk = NULL; 4886 /* 4887 * Create a dependency for the newly allocated block. 4888 * Add it to the dependency list for the buffer holding 4889 * the cylinder group map from which it was allocated. 4890 */ 4891 if (MOUNTEDSUJ(mp)) { 4892 jnewblk = malloc(sizeof(*jnewblk), M_JNEWBLK, M_SOFTDEP_FLAGS); 4893 workitem_alloc(&jnewblk->jn_list, D_JNEWBLK, mp); 4894 jnewblk->jn_jsegdep = newjsegdep(&jnewblk->jn_list); 4895 jnewblk->jn_state = ATTACHED; 4896 jnewblk->jn_blkno = newblkno; 4897 jnewblk->jn_frags = frags; 4898 jnewblk->jn_oldfrags = oldfrags; 4899 #ifdef SUJ_DEBUG 4900 { 4901 struct cg *cgp; 4902 uint8_t *blksfree; 4903 long bno; 4904 int i; 4905 4906 cgp = (struct cg *)bp->b_data; 4907 blksfree = cg_blksfree(cgp); 4908 bno = dtogd(fs, jnewblk->jn_blkno); 4909 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; 4910 i++) { 4911 if (isset(blksfree, bno + i)) 4912 panic("softdep_setup_blkmapdep: " 4913 "free fragment %d from %d-%d " 4914 "state 0x%X dep %p", i, 4915 jnewblk->jn_oldfrags, 4916 jnewblk->jn_frags, 4917 jnewblk->jn_state, 4918 jnewblk->jn_dep); 4919 } 4920 } 4921 #endif 4922 } 4923 4924 CTR3(KTR_SUJ, 4925 "softdep_setup_blkmapdep: blkno %jd frags %d oldfrags %d", 4926 newblkno, frags, oldfrags); 4927 ACQUIRE_LOCK(&lk); 4928 if (newblk_lookup(mp, newblkno, DEPALLOC, &newblk) != 0) 4929 panic("softdep_setup_blkmapdep: found block"); 4930 newblk->nb_bmsafemap = bmsafemap = bmsafemap_lookup(mp, bp, 4931 dtog(fs, newblkno), NULL); 4932 if (jnewblk) { 4933 jnewblk->jn_dep = (struct worklist *)newblk; 4934 LIST_INSERT_HEAD(&bmsafemap->sm_jnewblkhd, jnewblk, jn_deps); 4935 } else { 4936 newblk->nb_state |= ONDEPLIST; 4937 LIST_INSERT_HEAD(&bmsafemap->sm_newblkhd, newblk, nb_deps); 4938 } 4939 newblk->nb_bmsafemap = bmsafemap; 4940 newblk->nb_jnewblk = jnewblk; 4941 FREE_LOCK(&lk); 4942 } 4943 4944 #define BMSAFEMAP_HASH(fs, cg) \ 4945 (&bmsafemap_hashtbl[((((register_t)(fs)) >> 13) + (cg)) & bmsafemap_hash]) 4946 4947 static int 4948 bmsafemap_find(bmsafemaphd, mp, cg, bmsafemapp) 4949 struct bmsafemap_hashhead *bmsafemaphd; 4950 struct mount *mp; 4951 int cg; 4952 struct bmsafemap **bmsafemapp; 4953 { 4954 struct bmsafemap *bmsafemap; 4955 4956 LIST_FOREACH(bmsafemap, bmsafemaphd, sm_hash) 4957 if (bmsafemap->sm_list.wk_mp == mp && bmsafemap->sm_cg == cg) 4958 break; 4959 if (bmsafemap) { 4960 *bmsafemapp = bmsafemap; 4961 return (1); 4962 } 4963 *bmsafemapp = NULL; 4964 4965 return (0); 4966 } 4967 4968 /* 4969 * Find the bmsafemap associated with a cylinder group buffer. 4970 * If none exists, create one. The buffer must be locked when 4971 * this routine is called and this routine must be called with 4972 * the softdep lock held. To avoid giving up the lock while 4973 * allocating a new bmsafemap, a preallocated bmsafemap may be 4974 * provided. If it is provided but not needed, it is freed. 4975 */ 4976 static struct bmsafemap * 4977 bmsafemap_lookup(mp, bp, cg, newbmsafemap) 4978 struct mount *mp; 4979 struct buf *bp; 4980 int cg; 4981 struct bmsafemap *newbmsafemap; 4982 { 4983 struct bmsafemap_hashhead *bmsafemaphd; 4984 struct bmsafemap *bmsafemap, *collision; 4985 struct worklist *wk; 4986 struct fs *fs; 4987 4988 mtx_assert(&lk, MA_OWNED); 4989 if (bp) 4990 LIST_FOREACH(wk, &bp->b_dep, wk_list) 4991 if (wk->wk_type == D_BMSAFEMAP) { 4992 if (newbmsafemap) 4993 WORKITEM_FREE(newbmsafemap,D_BMSAFEMAP); 4994 return (WK_BMSAFEMAP(wk)); 4995 } 4996 fs = VFSTOUFS(mp)->um_fs; 4997 bmsafemaphd = BMSAFEMAP_HASH(fs, cg); 4998 if (bmsafemap_find(bmsafemaphd, mp, cg, &bmsafemap) == 1) { 4999 if (newbmsafemap) 5000 WORKITEM_FREE(newbmsafemap, D_BMSAFEMAP); 5001 return (bmsafemap); 5002 } 5003 if (newbmsafemap) { 5004 bmsafemap = newbmsafemap; 5005 } else { 5006 FREE_LOCK(&lk); 5007 bmsafemap = malloc(sizeof(struct bmsafemap), 5008 M_BMSAFEMAP, M_SOFTDEP_FLAGS); 5009 workitem_alloc(&bmsafemap->sm_list, D_BMSAFEMAP, mp); 5010 ACQUIRE_LOCK(&lk); 5011 } 5012 bmsafemap->sm_buf = bp; 5013 LIST_INIT(&bmsafemap->sm_inodedephd); 5014 LIST_INIT(&bmsafemap->sm_inodedepwr); 5015 LIST_INIT(&bmsafemap->sm_newblkhd); 5016 LIST_INIT(&bmsafemap->sm_newblkwr); 5017 LIST_INIT(&bmsafemap->sm_jaddrefhd); 5018 LIST_INIT(&bmsafemap->sm_jnewblkhd); 5019 LIST_INIT(&bmsafemap->sm_freehd); 5020 LIST_INIT(&bmsafemap->sm_freewr); 5021 if (bmsafemap_find(bmsafemaphd, mp, cg, &collision) == 1) { 5022 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 5023 return (collision); 5024 } 5025 bmsafemap->sm_cg = cg; 5026 LIST_INSERT_HEAD(bmsafemaphd, bmsafemap, sm_hash); 5027 LIST_INSERT_HEAD(&VFSTOUFS(mp)->softdep_dirtycg, bmsafemap, sm_next); 5028 WORKLIST_INSERT(&bp->b_dep, &bmsafemap->sm_list); 5029 return (bmsafemap); 5030 } 5031 5032 /* 5033 * Direct block allocation dependencies. 5034 * 5035 * When a new block is allocated, the corresponding disk locations must be 5036 * initialized (with zeros or new data) before the on-disk inode points to 5037 * them. Also, the freemap from which the block was allocated must be 5038 * updated (on disk) before the inode's pointer. These two dependencies are 5039 * independent of each other and are needed for all file blocks and indirect 5040 * blocks that are pointed to directly by the inode. Just before the 5041 * "in-core" version of the inode is updated with a newly allocated block 5042 * number, a procedure (below) is called to setup allocation dependency 5043 * structures. These structures are removed when the corresponding 5044 * dependencies are satisfied or when the block allocation becomes obsolete 5045 * (i.e., the file is deleted, the block is de-allocated, or the block is a 5046 * fragment that gets upgraded). All of these cases are handled in 5047 * procedures described later. 5048 * 5049 * When a file extension causes a fragment to be upgraded, either to a larger 5050 * fragment or to a full block, the on-disk location may change (if the 5051 * previous fragment could not simply be extended). In this case, the old 5052 * fragment must be de-allocated, but not until after the inode's pointer has 5053 * been updated. In most cases, this is handled by later procedures, which 5054 * will construct a "freefrag" structure to be added to the workitem queue 5055 * when the inode update is complete (or obsolete). The main exception to 5056 * this is when an allocation occurs while a pending allocation dependency 5057 * (for the same block pointer) remains. This case is handled in the main 5058 * allocation dependency setup procedure by immediately freeing the 5059 * unreferenced fragments. 5060 */ 5061 void 5062 softdep_setup_allocdirect(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5063 struct inode *ip; /* inode to which block is being added */ 5064 ufs_lbn_t off; /* block pointer within inode */ 5065 ufs2_daddr_t newblkno; /* disk block number being added */ 5066 ufs2_daddr_t oldblkno; /* previous block number, 0 unless frag */ 5067 long newsize; /* size of new block */ 5068 long oldsize; /* size of new block */ 5069 struct buf *bp; /* bp for allocated block */ 5070 { 5071 struct allocdirect *adp, *oldadp; 5072 struct allocdirectlst *adphead; 5073 struct freefrag *freefrag; 5074 struct inodedep *inodedep; 5075 struct pagedep *pagedep; 5076 struct jnewblk *jnewblk; 5077 struct newblk *newblk; 5078 struct mount *mp; 5079 ufs_lbn_t lbn; 5080 5081 lbn = bp->b_lblkno; 5082 mp = UFSTOVFS(ip->i_ump); 5083 if (oldblkno && oldblkno != newblkno) 5084 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5085 else 5086 freefrag = NULL; 5087 5088 CTR6(KTR_SUJ, 5089 "softdep_setup_allocdirect: ino %d blkno %jd oldblkno %jd " 5090 "off %jd newsize %ld oldsize %d", 5091 ip->i_number, newblkno, oldblkno, off, newsize, oldsize); 5092 ACQUIRE_LOCK(&lk); 5093 if (off >= NDADDR) { 5094 if (lbn > 0) 5095 panic("softdep_setup_allocdirect: bad lbn %jd, off %jd", 5096 lbn, off); 5097 /* allocating an indirect block */ 5098 if (oldblkno != 0) 5099 panic("softdep_setup_allocdirect: non-zero indir"); 5100 } else { 5101 if (off != lbn) 5102 panic("softdep_setup_allocdirect: lbn %jd != off %jd", 5103 lbn, off); 5104 /* 5105 * Allocating a direct block. 5106 * 5107 * If we are allocating a directory block, then we must 5108 * allocate an associated pagedep to track additions and 5109 * deletions. 5110 */ 5111 if ((ip->i_mode & IFMT) == IFDIR) 5112 pagedep_lookup(mp, bp, ip->i_number, off, DEPALLOC, 5113 &pagedep); 5114 } 5115 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5116 panic("softdep_setup_allocdirect: lost block"); 5117 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5118 ("softdep_setup_allocdirect: newblk already initialized")); 5119 /* 5120 * Convert the newblk to an allocdirect. 5121 */ 5122 newblk->nb_list.wk_type = D_ALLOCDIRECT; 5123 adp = (struct allocdirect *)newblk; 5124 newblk->nb_freefrag = freefrag; 5125 adp->ad_offset = off; 5126 adp->ad_oldblkno = oldblkno; 5127 adp->ad_newsize = newsize; 5128 adp->ad_oldsize = oldsize; 5129 5130 /* 5131 * Finish initializing the journal. 5132 */ 5133 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5134 jnewblk->jn_ino = ip->i_number; 5135 jnewblk->jn_lbn = lbn; 5136 add_to_journal(&jnewblk->jn_list); 5137 } 5138 if (freefrag && freefrag->ff_jdep != NULL && 5139 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5140 add_to_journal(freefrag->ff_jdep); 5141 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5142 adp->ad_inodedep = inodedep; 5143 5144 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5145 /* 5146 * The list of allocdirects must be kept in sorted and ascending 5147 * order so that the rollback routines can quickly determine the 5148 * first uncommitted block (the size of the file stored on disk 5149 * ends at the end of the lowest committed fragment, or if there 5150 * are no fragments, at the end of the highest committed block). 5151 * Since files generally grow, the typical case is that the new 5152 * block is to be added at the end of the list. We speed this 5153 * special case by checking against the last allocdirect in the 5154 * list before laboriously traversing the list looking for the 5155 * insertion point. 5156 */ 5157 adphead = &inodedep->id_newinoupdt; 5158 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5159 if (oldadp == NULL || oldadp->ad_offset <= off) { 5160 /* insert at end of list */ 5161 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5162 if (oldadp != NULL && oldadp->ad_offset == off) 5163 allocdirect_merge(adphead, adp, oldadp); 5164 FREE_LOCK(&lk); 5165 return; 5166 } 5167 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5168 if (oldadp->ad_offset >= off) 5169 break; 5170 } 5171 if (oldadp == NULL) 5172 panic("softdep_setup_allocdirect: lost entry"); 5173 /* insert in middle of list */ 5174 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5175 if (oldadp->ad_offset == off) 5176 allocdirect_merge(adphead, adp, oldadp); 5177 5178 FREE_LOCK(&lk); 5179 } 5180 5181 /* 5182 * Merge a newer and older journal record to be stored either in a 5183 * newblock or freefrag. This handles aggregating journal records for 5184 * fragment allocation into a second record as well as replacing a 5185 * journal free with an aborted journal allocation. A segment for the 5186 * oldest record will be placed on wkhd if it has been written. If not 5187 * the segment for the newer record will suffice. 5188 */ 5189 static struct worklist * 5190 jnewblk_merge(new, old, wkhd) 5191 struct worklist *new; 5192 struct worklist *old; 5193 struct workhead *wkhd; 5194 { 5195 struct jnewblk *njnewblk; 5196 struct jnewblk *jnewblk; 5197 5198 /* Handle NULLs to simplify callers. */ 5199 if (new == NULL) 5200 return (old); 5201 if (old == NULL) 5202 return (new); 5203 /* Replace a jfreefrag with a jnewblk. */ 5204 if (new->wk_type == D_JFREEFRAG) { 5205 if (WK_JNEWBLK(old)->jn_blkno != WK_JFREEFRAG(new)->fr_blkno) 5206 panic("jnewblk_merge: blkno mismatch: %p, %p", 5207 old, new); 5208 cancel_jfreefrag(WK_JFREEFRAG(new)); 5209 return (old); 5210 } 5211 if (old->wk_type != D_JNEWBLK || new->wk_type != D_JNEWBLK) 5212 panic("jnewblk_merge: Bad type: old %d new %d\n", 5213 old->wk_type, new->wk_type); 5214 /* 5215 * Handle merging of two jnewblk records that describe 5216 * different sets of fragments in the same block. 5217 */ 5218 jnewblk = WK_JNEWBLK(old); 5219 njnewblk = WK_JNEWBLK(new); 5220 if (jnewblk->jn_blkno != njnewblk->jn_blkno) 5221 panic("jnewblk_merge: Merging disparate blocks."); 5222 /* 5223 * The record may be rolled back in the cg. 5224 */ 5225 if (jnewblk->jn_state & UNDONE) { 5226 jnewblk->jn_state &= ~UNDONE; 5227 njnewblk->jn_state |= UNDONE; 5228 njnewblk->jn_state &= ~ATTACHED; 5229 } 5230 /* 5231 * We modify the newer addref and free the older so that if neither 5232 * has been written the most up-to-date copy will be on disk. If 5233 * both have been written but rolled back we only temporarily need 5234 * one of them to fix the bits when the cg write completes. 5235 */ 5236 jnewblk->jn_state |= ATTACHED | COMPLETE; 5237 njnewblk->jn_oldfrags = jnewblk->jn_oldfrags; 5238 cancel_jnewblk(jnewblk, wkhd); 5239 WORKLIST_REMOVE(&jnewblk->jn_list); 5240 free_jnewblk(jnewblk); 5241 return (new); 5242 } 5243 5244 /* 5245 * Replace an old allocdirect dependency with a newer one. 5246 * This routine must be called with splbio interrupts blocked. 5247 */ 5248 static void 5249 allocdirect_merge(adphead, newadp, oldadp) 5250 struct allocdirectlst *adphead; /* head of list holding allocdirects */ 5251 struct allocdirect *newadp; /* allocdirect being added */ 5252 struct allocdirect *oldadp; /* existing allocdirect being checked */ 5253 { 5254 struct worklist *wk; 5255 struct freefrag *freefrag; 5256 5257 freefrag = NULL; 5258 mtx_assert(&lk, MA_OWNED); 5259 if (newadp->ad_oldblkno != oldadp->ad_newblkno || 5260 newadp->ad_oldsize != oldadp->ad_newsize || 5261 newadp->ad_offset >= NDADDR) 5262 panic("%s %jd != new %jd || old size %ld != new %ld", 5263 "allocdirect_merge: old blkno", 5264 (intmax_t)newadp->ad_oldblkno, 5265 (intmax_t)oldadp->ad_newblkno, 5266 newadp->ad_oldsize, oldadp->ad_newsize); 5267 newadp->ad_oldblkno = oldadp->ad_oldblkno; 5268 newadp->ad_oldsize = oldadp->ad_oldsize; 5269 /* 5270 * If the old dependency had a fragment to free or had never 5271 * previously had a block allocated, then the new dependency 5272 * can immediately post its freefrag and adopt the old freefrag. 5273 * This action is done by swapping the freefrag dependencies. 5274 * The new dependency gains the old one's freefrag, and the 5275 * old one gets the new one and then immediately puts it on 5276 * the worklist when it is freed by free_newblk. It is 5277 * not possible to do this swap when the old dependency had a 5278 * non-zero size but no previous fragment to free. This condition 5279 * arises when the new block is an extension of the old block. 5280 * Here, the first part of the fragment allocated to the new 5281 * dependency is part of the block currently claimed on disk by 5282 * the old dependency, so cannot legitimately be freed until the 5283 * conditions for the new dependency are fulfilled. 5284 */ 5285 freefrag = newadp->ad_freefrag; 5286 if (oldadp->ad_freefrag != NULL || oldadp->ad_oldblkno == 0) { 5287 newadp->ad_freefrag = oldadp->ad_freefrag; 5288 oldadp->ad_freefrag = freefrag; 5289 } 5290 /* 5291 * If we are tracking a new directory-block allocation, 5292 * move it from the old allocdirect to the new allocdirect. 5293 */ 5294 if ((wk = LIST_FIRST(&oldadp->ad_newdirblk)) != NULL) { 5295 WORKLIST_REMOVE(wk); 5296 if (!LIST_EMPTY(&oldadp->ad_newdirblk)) 5297 panic("allocdirect_merge: extra newdirblk"); 5298 WORKLIST_INSERT(&newadp->ad_newdirblk, wk); 5299 } 5300 TAILQ_REMOVE(adphead, oldadp, ad_next); 5301 /* 5302 * We need to move any journal dependencies over to the freefrag 5303 * that releases this block if it exists. Otherwise we are 5304 * extending an existing block and we'll wait until that is 5305 * complete to release the journal space and extend the 5306 * new journal to cover this old space as well. 5307 */ 5308 if (freefrag == NULL) { 5309 if (oldadp->ad_newblkno != newadp->ad_newblkno) 5310 panic("allocdirect_merge: %jd != %jd", 5311 oldadp->ad_newblkno, newadp->ad_newblkno); 5312 newadp->ad_block.nb_jnewblk = (struct jnewblk *) 5313 jnewblk_merge(&newadp->ad_block.nb_jnewblk->jn_list, 5314 &oldadp->ad_block.nb_jnewblk->jn_list, 5315 &newadp->ad_block.nb_jwork); 5316 oldadp->ad_block.nb_jnewblk = NULL; 5317 cancel_newblk(&oldadp->ad_block, NULL, 5318 &newadp->ad_block.nb_jwork); 5319 } else { 5320 wk = (struct worklist *) cancel_newblk(&oldadp->ad_block, 5321 &freefrag->ff_list, &freefrag->ff_jwork); 5322 freefrag->ff_jdep = jnewblk_merge(freefrag->ff_jdep, wk, 5323 &freefrag->ff_jwork); 5324 } 5325 free_newblk(&oldadp->ad_block); 5326 } 5327 5328 /* 5329 * Allocate a jfreefrag structure to journal a single block free. 5330 */ 5331 static struct jfreefrag * 5332 newjfreefrag(freefrag, ip, blkno, size, lbn) 5333 struct freefrag *freefrag; 5334 struct inode *ip; 5335 ufs2_daddr_t blkno; 5336 long size; 5337 ufs_lbn_t lbn; 5338 { 5339 struct jfreefrag *jfreefrag; 5340 struct fs *fs; 5341 5342 fs = ip->i_fs; 5343 jfreefrag = malloc(sizeof(struct jfreefrag), M_JFREEFRAG, 5344 M_SOFTDEP_FLAGS); 5345 workitem_alloc(&jfreefrag->fr_list, D_JFREEFRAG, UFSTOVFS(ip->i_ump)); 5346 jfreefrag->fr_jsegdep = newjsegdep(&jfreefrag->fr_list); 5347 jfreefrag->fr_state = ATTACHED | DEPCOMPLETE; 5348 jfreefrag->fr_ino = ip->i_number; 5349 jfreefrag->fr_lbn = lbn; 5350 jfreefrag->fr_blkno = blkno; 5351 jfreefrag->fr_frags = numfrags(fs, size); 5352 jfreefrag->fr_freefrag = freefrag; 5353 5354 return (jfreefrag); 5355 } 5356 5357 /* 5358 * Allocate a new freefrag structure. 5359 */ 5360 static struct freefrag * 5361 newfreefrag(ip, blkno, size, lbn) 5362 struct inode *ip; 5363 ufs2_daddr_t blkno; 5364 long size; 5365 ufs_lbn_t lbn; 5366 { 5367 struct freefrag *freefrag; 5368 struct fs *fs; 5369 5370 CTR4(KTR_SUJ, "newfreefrag: ino %d blkno %jd size %ld lbn %jd", 5371 ip->i_number, blkno, size, lbn); 5372 fs = ip->i_fs; 5373 if (fragnum(fs, blkno) + numfrags(fs, size) > fs->fs_frag) 5374 panic("newfreefrag: frag size"); 5375 freefrag = malloc(sizeof(struct freefrag), 5376 M_FREEFRAG, M_SOFTDEP_FLAGS); 5377 workitem_alloc(&freefrag->ff_list, D_FREEFRAG, UFSTOVFS(ip->i_ump)); 5378 freefrag->ff_state = ATTACHED; 5379 LIST_INIT(&freefrag->ff_jwork); 5380 freefrag->ff_inum = ip->i_number; 5381 freefrag->ff_vtype = ITOV(ip)->v_type; 5382 freefrag->ff_blkno = blkno; 5383 freefrag->ff_fragsize = size; 5384 5385 if (MOUNTEDSUJ(UFSTOVFS(ip->i_ump))) { 5386 freefrag->ff_jdep = (struct worklist *) 5387 newjfreefrag(freefrag, ip, blkno, size, lbn); 5388 } else { 5389 freefrag->ff_state |= DEPCOMPLETE; 5390 freefrag->ff_jdep = NULL; 5391 } 5392 5393 return (freefrag); 5394 } 5395 5396 /* 5397 * This workitem de-allocates fragments that were replaced during 5398 * file block allocation. 5399 */ 5400 static void 5401 handle_workitem_freefrag(freefrag) 5402 struct freefrag *freefrag; 5403 { 5404 struct ufsmount *ump = VFSTOUFS(freefrag->ff_list.wk_mp); 5405 struct workhead wkhd; 5406 5407 CTR3(KTR_SUJ, 5408 "handle_workitem_freefrag: ino %d blkno %jd size %ld", 5409 freefrag->ff_inum, freefrag->ff_blkno, freefrag->ff_fragsize); 5410 /* 5411 * It would be illegal to add new completion items to the 5412 * freefrag after it was schedule to be done so it must be 5413 * safe to modify the list head here. 5414 */ 5415 LIST_INIT(&wkhd); 5416 ACQUIRE_LOCK(&lk); 5417 LIST_SWAP(&freefrag->ff_jwork, &wkhd, worklist, wk_list); 5418 /* 5419 * If the journal has not been written we must cancel it here. 5420 */ 5421 if (freefrag->ff_jdep) { 5422 if (freefrag->ff_jdep->wk_type != D_JNEWBLK) 5423 panic("handle_workitem_freefrag: Unexpected type %d\n", 5424 freefrag->ff_jdep->wk_type); 5425 cancel_jnewblk(WK_JNEWBLK(freefrag->ff_jdep), &wkhd); 5426 } 5427 FREE_LOCK(&lk); 5428 ffs_blkfree(ump, ump->um_fs, ump->um_devvp, freefrag->ff_blkno, 5429 freefrag->ff_fragsize, freefrag->ff_inum, freefrag->ff_vtype, &wkhd); 5430 ACQUIRE_LOCK(&lk); 5431 WORKITEM_FREE(freefrag, D_FREEFRAG); 5432 FREE_LOCK(&lk); 5433 } 5434 5435 /* 5436 * Set up a dependency structure for an external attributes data block. 5437 * This routine follows much of the structure of softdep_setup_allocdirect. 5438 * See the description of softdep_setup_allocdirect above for details. 5439 */ 5440 void 5441 softdep_setup_allocext(ip, off, newblkno, oldblkno, newsize, oldsize, bp) 5442 struct inode *ip; 5443 ufs_lbn_t off; 5444 ufs2_daddr_t newblkno; 5445 ufs2_daddr_t oldblkno; 5446 long newsize; 5447 long oldsize; 5448 struct buf *bp; 5449 { 5450 struct allocdirect *adp, *oldadp; 5451 struct allocdirectlst *adphead; 5452 struct freefrag *freefrag; 5453 struct inodedep *inodedep; 5454 struct jnewblk *jnewblk; 5455 struct newblk *newblk; 5456 struct mount *mp; 5457 ufs_lbn_t lbn; 5458 5459 if (off >= NXADDR) 5460 panic("softdep_setup_allocext: lbn %lld > NXADDR", 5461 (long long)off); 5462 5463 lbn = bp->b_lblkno; 5464 mp = UFSTOVFS(ip->i_ump); 5465 if (oldblkno && oldblkno != newblkno) 5466 freefrag = newfreefrag(ip, oldblkno, oldsize, lbn); 5467 else 5468 freefrag = NULL; 5469 5470 ACQUIRE_LOCK(&lk); 5471 if (newblk_lookup(mp, newblkno, 0, &newblk) == 0) 5472 panic("softdep_setup_allocext: lost block"); 5473 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5474 ("softdep_setup_allocext: newblk already initialized")); 5475 /* 5476 * Convert the newblk to an allocdirect. 5477 */ 5478 newblk->nb_list.wk_type = D_ALLOCDIRECT; 5479 adp = (struct allocdirect *)newblk; 5480 newblk->nb_freefrag = freefrag; 5481 adp->ad_offset = off; 5482 adp->ad_oldblkno = oldblkno; 5483 adp->ad_newsize = newsize; 5484 adp->ad_oldsize = oldsize; 5485 adp->ad_state |= EXTDATA; 5486 5487 /* 5488 * Finish initializing the journal. 5489 */ 5490 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5491 jnewblk->jn_ino = ip->i_number; 5492 jnewblk->jn_lbn = lbn; 5493 add_to_journal(&jnewblk->jn_list); 5494 } 5495 if (freefrag && freefrag->ff_jdep != NULL && 5496 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5497 add_to_journal(freefrag->ff_jdep); 5498 inodedep_lookup(mp, ip->i_number, DEPALLOC | NODELAY, &inodedep); 5499 adp->ad_inodedep = inodedep; 5500 5501 WORKLIST_INSERT(&bp->b_dep, &newblk->nb_list); 5502 /* 5503 * The list of allocdirects must be kept in sorted and ascending 5504 * order so that the rollback routines can quickly determine the 5505 * first uncommitted block (the size of the file stored on disk 5506 * ends at the end of the lowest committed fragment, or if there 5507 * are no fragments, at the end of the highest committed block). 5508 * Since files generally grow, the typical case is that the new 5509 * block is to be added at the end of the list. We speed this 5510 * special case by checking against the last allocdirect in the 5511 * list before laboriously traversing the list looking for the 5512 * insertion point. 5513 */ 5514 adphead = &inodedep->id_newextupdt; 5515 oldadp = TAILQ_LAST(adphead, allocdirectlst); 5516 if (oldadp == NULL || oldadp->ad_offset <= off) { 5517 /* insert at end of list */ 5518 TAILQ_INSERT_TAIL(adphead, adp, ad_next); 5519 if (oldadp != NULL && oldadp->ad_offset == off) 5520 allocdirect_merge(adphead, adp, oldadp); 5521 FREE_LOCK(&lk); 5522 return; 5523 } 5524 TAILQ_FOREACH(oldadp, adphead, ad_next) { 5525 if (oldadp->ad_offset >= off) 5526 break; 5527 } 5528 if (oldadp == NULL) 5529 panic("softdep_setup_allocext: lost entry"); 5530 /* insert in middle of list */ 5531 TAILQ_INSERT_BEFORE(oldadp, adp, ad_next); 5532 if (oldadp->ad_offset == off) 5533 allocdirect_merge(adphead, adp, oldadp); 5534 FREE_LOCK(&lk); 5535 } 5536 5537 /* 5538 * Indirect block allocation dependencies. 5539 * 5540 * The same dependencies that exist for a direct block also exist when 5541 * a new block is allocated and pointed to by an entry in a block of 5542 * indirect pointers. The undo/redo states described above are also 5543 * used here. Because an indirect block contains many pointers that 5544 * may have dependencies, a second copy of the entire in-memory indirect 5545 * block is kept. The buffer cache copy is always completely up-to-date. 5546 * The second copy, which is used only as a source for disk writes, 5547 * contains only the safe pointers (i.e., those that have no remaining 5548 * update dependencies). The second copy is freed when all pointers 5549 * are safe. The cache is not allowed to replace indirect blocks with 5550 * pending update dependencies. If a buffer containing an indirect 5551 * block with dependencies is written, these routines will mark it 5552 * dirty again. It can only be successfully written once all the 5553 * dependencies are removed. The ffs_fsync routine in conjunction with 5554 * softdep_sync_metadata work together to get all the dependencies 5555 * removed so that a file can be successfully written to disk. Three 5556 * procedures are used when setting up indirect block pointer 5557 * dependencies. The division is necessary because of the organization 5558 * of the "balloc" routine and because of the distinction between file 5559 * pages and file metadata blocks. 5560 */ 5561 5562 /* 5563 * Allocate a new allocindir structure. 5564 */ 5565 static struct allocindir * 5566 newallocindir(ip, ptrno, newblkno, oldblkno, lbn) 5567 struct inode *ip; /* inode for file being extended */ 5568 int ptrno; /* offset of pointer in indirect block */ 5569 ufs2_daddr_t newblkno; /* disk block number being added */ 5570 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5571 ufs_lbn_t lbn; 5572 { 5573 struct newblk *newblk; 5574 struct allocindir *aip; 5575 struct freefrag *freefrag; 5576 struct jnewblk *jnewblk; 5577 5578 if (oldblkno) 5579 freefrag = newfreefrag(ip, oldblkno, ip->i_fs->fs_bsize, lbn); 5580 else 5581 freefrag = NULL; 5582 ACQUIRE_LOCK(&lk); 5583 if (newblk_lookup(UFSTOVFS(ip->i_ump), newblkno, 0, &newblk) == 0) 5584 panic("new_allocindir: lost block"); 5585 KASSERT(newblk->nb_list.wk_type == D_NEWBLK, 5586 ("newallocindir: newblk already initialized")); 5587 newblk->nb_list.wk_type = D_ALLOCINDIR; 5588 newblk->nb_freefrag = freefrag; 5589 aip = (struct allocindir *)newblk; 5590 aip->ai_offset = ptrno; 5591 aip->ai_oldblkno = oldblkno; 5592 aip->ai_lbn = lbn; 5593 if ((jnewblk = newblk->nb_jnewblk) != NULL) { 5594 jnewblk->jn_ino = ip->i_number; 5595 jnewblk->jn_lbn = lbn; 5596 add_to_journal(&jnewblk->jn_list); 5597 } 5598 if (freefrag && freefrag->ff_jdep != NULL && 5599 freefrag->ff_jdep->wk_type == D_JFREEFRAG) 5600 add_to_journal(freefrag->ff_jdep); 5601 return (aip); 5602 } 5603 5604 /* 5605 * Called just before setting an indirect block pointer 5606 * to a newly allocated file page. 5607 */ 5608 void 5609 softdep_setup_allocindir_page(ip, lbn, bp, ptrno, newblkno, oldblkno, nbp) 5610 struct inode *ip; /* inode for file being extended */ 5611 ufs_lbn_t lbn; /* allocated block number within file */ 5612 struct buf *bp; /* buffer with indirect blk referencing page */ 5613 int ptrno; /* offset of pointer in indirect block */ 5614 ufs2_daddr_t newblkno; /* disk block number being added */ 5615 ufs2_daddr_t oldblkno; /* previous block number, 0 if none */ 5616 struct buf *nbp; /* buffer holding allocated page */ 5617 { 5618 struct inodedep *inodedep; 5619 struct freefrag *freefrag; 5620 struct allocindir *aip; 5621 struct pagedep *pagedep; 5622 struct mount *mp; 5623 int dflags; 5624 5625 if (lbn != nbp->b_lblkno) 5626 panic("softdep_setup_allocindir_page: lbn %jd != lblkno %jd", 5627 lbn, bp->b_lblkno); 5628 CTR4(KTR_SUJ, 5629 "softdep_setup_allocindir_page: ino %d blkno %jd oldblkno %jd " 5630 "lbn %jd", ip->i_number, newblkno, oldblkno, lbn); 5631 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_page"); 5632 mp = UFSTOVFS(ip->i_ump); 5633 aip = newallocindir(ip, ptrno, newblkno, oldblkno, lbn); 5634 dflags = DEPALLOC; 5635 if (IS_SNAPSHOT(ip)) 5636 dflags |= NODELAY; 5637 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 5638 /* 5639 * If we are allocating a directory page, then we must 5640 * allocate an associated pagedep to track additions and 5641 * deletions. 5642 */ 5643 if ((ip->i_mode & IFMT) == IFDIR) 5644 pagedep_lookup(mp, nbp, ip->i_number, lbn, DEPALLOC, &pagedep); 5645 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5646 freefrag = setup_allocindir_phase2(bp, ip, inodedep, aip, lbn); 5647 FREE_LOCK(&lk); 5648 if (freefrag) 5649 handle_workitem_freefrag(freefrag); 5650 } 5651 5652 /* 5653 * Called just before setting an indirect block pointer to a 5654 * newly allocated indirect block. 5655 */ 5656 void 5657 softdep_setup_allocindir_meta(nbp, ip, bp, ptrno, newblkno) 5658 struct buf *nbp; /* newly allocated indirect block */ 5659 struct inode *ip; /* inode for file being extended */ 5660 struct buf *bp; /* indirect block referencing allocated block */ 5661 int ptrno; /* offset of pointer in indirect block */ 5662 ufs2_daddr_t newblkno; /* disk block number being added */ 5663 { 5664 struct inodedep *inodedep; 5665 struct allocindir *aip; 5666 ufs_lbn_t lbn; 5667 int dflags; 5668 5669 CTR3(KTR_SUJ, 5670 "softdep_setup_allocindir_meta: ino %d blkno %jd ptrno %d", 5671 ip->i_number, newblkno, ptrno); 5672 lbn = nbp->b_lblkno; 5673 ASSERT_VOP_LOCKED(ITOV(ip), "softdep_setup_allocindir_meta"); 5674 aip = newallocindir(ip, ptrno, newblkno, 0, lbn); 5675 dflags = DEPALLOC; 5676 if (IS_SNAPSHOT(ip)) 5677 dflags |= NODELAY; 5678 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 5679 WORKLIST_INSERT(&nbp->b_dep, &aip->ai_block.nb_list); 5680 if (setup_allocindir_phase2(bp, ip, inodedep, aip, lbn)) 5681 panic("softdep_setup_allocindir_meta: Block already existed"); 5682 FREE_LOCK(&lk); 5683 } 5684 5685 static void 5686 indirdep_complete(indirdep) 5687 struct indirdep *indirdep; 5688 { 5689 struct allocindir *aip; 5690 5691 LIST_REMOVE(indirdep, ir_next); 5692 indirdep->ir_state |= DEPCOMPLETE; 5693 5694 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != NULL) { 5695 LIST_REMOVE(aip, ai_next); 5696 free_newblk(&aip->ai_block); 5697 } 5698 /* 5699 * If this indirdep is not attached to a buf it was simply waiting 5700 * on completion to clear completehd. free_indirdep() asserts 5701 * that nothing is dangling. 5702 */ 5703 if ((indirdep->ir_state & ONWORKLIST) == 0) 5704 free_indirdep(indirdep); 5705 } 5706 5707 static struct indirdep * 5708 indirdep_lookup(mp, ip, bp) 5709 struct mount *mp; 5710 struct inode *ip; 5711 struct buf *bp; 5712 { 5713 struct indirdep *indirdep, *newindirdep; 5714 struct newblk *newblk; 5715 struct worklist *wk; 5716 struct fs *fs; 5717 ufs2_daddr_t blkno; 5718 5719 mtx_assert(&lk, MA_OWNED); 5720 indirdep = NULL; 5721 newindirdep = NULL; 5722 fs = ip->i_fs; 5723 for (;;) { 5724 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 5725 if (wk->wk_type != D_INDIRDEP) 5726 continue; 5727 indirdep = WK_INDIRDEP(wk); 5728 break; 5729 } 5730 /* Found on the buffer worklist, no new structure to free. */ 5731 if (indirdep != NULL && newindirdep == NULL) 5732 return (indirdep); 5733 if (indirdep != NULL && newindirdep != NULL) 5734 panic("indirdep_lookup: simultaneous create"); 5735 /* None found on the buffer and a new structure is ready. */ 5736 if (indirdep == NULL && newindirdep != NULL) 5737 break; 5738 /* None found and no new structure available. */ 5739 FREE_LOCK(&lk); 5740 newindirdep = malloc(sizeof(struct indirdep), 5741 M_INDIRDEP, M_SOFTDEP_FLAGS); 5742 workitem_alloc(&newindirdep->ir_list, D_INDIRDEP, mp); 5743 newindirdep->ir_state = ATTACHED; 5744 if (ip->i_ump->um_fstype == UFS1) 5745 newindirdep->ir_state |= UFS1FMT; 5746 TAILQ_INIT(&newindirdep->ir_trunc); 5747 newindirdep->ir_saveddata = NULL; 5748 LIST_INIT(&newindirdep->ir_deplisthd); 5749 LIST_INIT(&newindirdep->ir_donehd); 5750 LIST_INIT(&newindirdep->ir_writehd); 5751 LIST_INIT(&newindirdep->ir_completehd); 5752 if (bp->b_blkno == bp->b_lblkno) { 5753 ufs_bmaparray(bp->b_vp, bp->b_lblkno, &blkno, bp, 5754 NULL, NULL); 5755 bp->b_blkno = blkno; 5756 } 5757 newindirdep->ir_freeblks = NULL; 5758 newindirdep->ir_savebp = 5759 getblk(ip->i_devvp, bp->b_blkno, bp->b_bcount, 0, 0, 0); 5760 newindirdep->ir_bp = bp; 5761 BUF_KERNPROC(newindirdep->ir_savebp); 5762 bcopy(bp->b_data, newindirdep->ir_savebp->b_data, bp->b_bcount); 5763 ACQUIRE_LOCK(&lk); 5764 } 5765 indirdep = newindirdep; 5766 WORKLIST_INSERT(&bp->b_dep, &indirdep->ir_list); 5767 /* 5768 * If the block is not yet allocated we don't set DEPCOMPLETE so 5769 * that we don't free dependencies until the pointers are valid. 5770 * This could search b_dep for D_ALLOCDIRECT/D_ALLOCINDIR rather 5771 * than using the hash. 5772 */ 5773 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk)) 5774 LIST_INSERT_HEAD(&newblk->nb_indirdeps, indirdep, ir_next); 5775 else 5776 indirdep->ir_state |= DEPCOMPLETE; 5777 return (indirdep); 5778 } 5779 5780 /* 5781 * Called to finish the allocation of the "aip" allocated 5782 * by one of the two routines above. 5783 */ 5784 static struct freefrag * 5785 setup_allocindir_phase2(bp, ip, inodedep, aip, lbn) 5786 struct buf *bp; /* in-memory copy of the indirect block */ 5787 struct inode *ip; /* inode for file being extended */ 5788 struct inodedep *inodedep; /* Inodedep for ip */ 5789 struct allocindir *aip; /* allocindir allocated by the above routines */ 5790 ufs_lbn_t lbn; /* Logical block number for this block. */ 5791 { 5792 struct fs *fs; 5793 struct indirdep *indirdep; 5794 struct allocindir *oldaip; 5795 struct freefrag *freefrag; 5796 struct mount *mp; 5797 5798 mtx_assert(&lk, MA_OWNED); 5799 mp = UFSTOVFS(ip->i_ump); 5800 fs = ip->i_fs; 5801 if (bp->b_lblkno >= 0) 5802 panic("setup_allocindir_phase2: not indir blk"); 5803 KASSERT(aip->ai_offset >= 0 && aip->ai_offset < NINDIR(fs), 5804 ("setup_allocindir_phase2: Bad offset %d", aip->ai_offset)); 5805 indirdep = indirdep_lookup(mp, ip, bp); 5806 KASSERT(indirdep->ir_savebp != NULL, 5807 ("setup_allocindir_phase2 NULL ir_savebp")); 5808 aip->ai_indirdep = indirdep; 5809 /* 5810 * Check for an unwritten dependency for this indirect offset. If 5811 * there is, merge the old dependency into the new one. This happens 5812 * as a result of reallocblk only. 5813 */ 5814 freefrag = NULL; 5815 if (aip->ai_oldblkno != 0) { 5816 LIST_FOREACH(oldaip, &indirdep->ir_deplisthd, ai_next) { 5817 if (oldaip->ai_offset == aip->ai_offset) { 5818 freefrag = allocindir_merge(aip, oldaip); 5819 goto done; 5820 } 5821 } 5822 LIST_FOREACH(oldaip, &indirdep->ir_donehd, ai_next) { 5823 if (oldaip->ai_offset == aip->ai_offset) { 5824 freefrag = allocindir_merge(aip, oldaip); 5825 goto done; 5826 } 5827 } 5828 } 5829 done: 5830 LIST_INSERT_HEAD(&indirdep->ir_deplisthd, aip, ai_next); 5831 return (freefrag); 5832 } 5833 5834 /* 5835 * Merge two allocindirs which refer to the same block. Move newblock 5836 * dependencies and setup the freefrags appropriately. 5837 */ 5838 static struct freefrag * 5839 allocindir_merge(aip, oldaip) 5840 struct allocindir *aip; 5841 struct allocindir *oldaip; 5842 { 5843 struct freefrag *freefrag; 5844 struct worklist *wk; 5845 5846 if (oldaip->ai_newblkno != aip->ai_oldblkno) 5847 panic("allocindir_merge: blkno"); 5848 aip->ai_oldblkno = oldaip->ai_oldblkno; 5849 freefrag = aip->ai_freefrag; 5850 aip->ai_freefrag = oldaip->ai_freefrag; 5851 oldaip->ai_freefrag = NULL; 5852 KASSERT(freefrag != NULL, ("setup_allocindir_phase2: No freefrag")); 5853 /* 5854 * If we are tracking a new directory-block allocation, 5855 * move it from the old allocindir to the new allocindir. 5856 */ 5857 if ((wk = LIST_FIRST(&oldaip->ai_newdirblk)) != NULL) { 5858 WORKLIST_REMOVE(wk); 5859 if (!LIST_EMPTY(&oldaip->ai_newdirblk)) 5860 panic("allocindir_merge: extra newdirblk"); 5861 WORKLIST_INSERT(&aip->ai_newdirblk, wk); 5862 } 5863 /* 5864 * We can skip journaling for this freefrag and just complete 5865 * any pending journal work for the allocindir that is being 5866 * removed after the freefrag completes. 5867 */ 5868 if (freefrag->ff_jdep) 5869 cancel_jfreefrag(WK_JFREEFRAG(freefrag->ff_jdep)); 5870 LIST_REMOVE(oldaip, ai_next); 5871 freefrag->ff_jdep = (struct worklist *)cancel_newblk(&oldaip->ai_block, 5872 &freefrag->ff_list, &freefrag->ff_jwork); 5873 free_newblk(&oldaip->ai_block); 5874 5875 return (freefrag); 5876 } 5877 5878 static inline void 5879 setup_freedirect(freeblks, ip, i, needj) 5880 struct freeblks *freeblks; 5881 struct inode *ip; 5882 int i; 5883 int needj; 5884 { 5885 ufs2_daddr_t blkno; 5886 int frags; 5887 5888 blkno = DIP(ip, i_db[i]); 5889 if (blkno == 0) 5890 return; 5891 DIP_SET(ip, i_db[i], 0); 5892 frags = sblksize(ip->i_fs, ip->i_size, i); 5893 frags = numfrags(ip->i_fs, frags); 5894 newfreework(ip->i_ump, freeblks, NULL, i, blkno, frags, 0, needj); 5895 } 5896 5897 static inline void 5898 setup_freeext(freeblks, ip, i, needj) 5899 struct freeblks *freeblks; 5900 struct inode *ip; 5901 int i; 5902 int needj; 5903 { 5904 ufs2_daddr_t blkno; 5905 int frags; 5906 5907 blkno = ip->i_din2->di_extb[i]; 5908 if (blkno == 0) 5909 return; 5910 ip->i_din2->di_extb[i] = 0; 5911 frags = sblksize(ip->i_fs, ip->i_din2->di_extsize, i); 5912 frags = numfrags(ip->i_fs, frags); 5913 newfreework(ip->i_ump, freeblks, NULL, -1 - i, blkno, frags, 0, needj); 5914 } 5915 5916 static inline void 5917 setup_freeindir(freeblks, ip, i, lbn, needj) 5918 struct freeblks *freeblks; 5919 struct inode *ip; 5920 int i; 5921 ufs_lbn_t lbn; 5922 int needj; 5923 { 5924 ufs2_daddr_t blkno; 5925 5926 blkno = DIP(ip, i_ib[i]); 5927 if (blkno == 0) 5928 return; 5929 DIP_SET(ip, i_ib[i], 0); 5930 newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, ip->i_fs->fs_frag, 5931 0, needj); 5932 } 5933 5934 static inline struct freeblks * 5935 newfreeblks(mp, ip) 5936 struct mount *mp; 5937 struct inode *ip; 5938 { 5939 struct freeblks *freeblks; 5940 5941 freeblks = malloc(sizeof(struct freeblks), 5942 M_FREEBLKS, M_SOFTDEP_FLAGS|M_ZERO); 5943 workitem_alloc(&freeblks->fb_list, D_FREEBLKS, mp); 5944 LIST_INIT(&freeblks->fb_jblkdephd); 5945 LIST_INIT(&freeblks->fb_jwork); 5946 freeblks->fb_ref = 0; 5947 freeblks->fb_cgwait = 0; 5948 freeblks->fb_state = ATTACHED; 5949 freeblks->fb_uid = ip->i_uid; 5950 freeblks->fb_inum = ip->i_number; 5951 freeblks->fb_vtype = ITOV(ip)->v_type; 5952 freeblks->fb_modrev = DIP(ip, i_modrev); 5953 freeblks->fb_devvp = ip->i_devvp; 5954 freeblks->fb_chkcnt = 0; 5955 freeblks->fb_len = 0; 5956 5957 return (freeblks); 5958 } 5959 5960 static void 5961 trunc_indirdep(indirdep, freeblks, bp, off) 5962 struct indirdep *indirdep; 5963 struct freeblks *freeblks; 5964 struct buf *bp; 5965 int off; 5966 { 5967 struct allocindir *aip, *aipn; 5968 5969 /* 5970 * The first set of allocindirs won't be in savedbp. 5971 */ 5972 LIST_FOREACH_SAFE(aip, &indirdep->ir_deplisthd, ai_next, aipn) 5973 if (aip->ai_offset > off) 5974 cancel_allocindir(aip, bp, freeblks, 1); 5975 LIST_FOREACH_SAFE(aip, &indirdep->ir_donehd, ai_next, aipn) 5976 if (aip->ai_offset > off) 5977 cancel_allocindir(aip, bp, freeblks, 1); 5978 /* 5979 * These will exist in savedbp. 5980 */ 5981 LIST_FOREACH_SAFE(aip, &indirdep->ir_writehd, ai_next, aipn) 5982 if (aip->ai_offset > off) 5983 cancel_allocindir(aip, NULL, freeblks, 0); 5984 LIST_FOREACH_SAFE(aip, &indirdep->ir_completehd, ai_next, aipn) 5985 if (aip->ai_offset > off) 5986 cancel_allocindir(aip, NULL, freeblks, 0); 5987 } 5988 5989 /* 5990 * Follow the chain of indirects down to lastlbn creating a freework 5991 * structure for each. This will be used to start indir_trunc() at 5992 * the right offset and create the journal records for the parrtial 5993 * truncation. A second step will handle the truncated dependencies. 5994 */ 5995 static int 5996 setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno) 5997 struct freeblks *freeblks; 5998 struct inode *ip; 5999 ufs_lbn_t lbn; 6000 ufs_lbn_t lastlbn; 6001 ufs2_daddr_t blkno; 6002 { 6003 struct indirdep *indirdep; 6004 struct indirdep *indirn; 6005 struct freework *freework; 6006 struct newblk *newblk; 6007 struct mount *mp; 6008 struct buf *bp; 6009 uint8_t *start; 6010 uint8_t *end; 6011 ufs_lbn_t lbnadd; 6012 int level; 6013 int error; 6014 int off; 6015 6016 6017 freework = NULL; 6018 if (blkno == 0) 6019 return (0); 6020 mp = freeblks->fb_list.wk_mp; 6021 bp = getblk(ITOV(ip), lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 6022 if ((bp->b_flags & B_CACHE) == 0) { 6023 bp->b_blkno = blkptrtodb(VFSTOUFS(mp), blkno); 6024 bp->b_iocmd = BIO_READ; 6025 bp->b_flags &= ~B_INVAL; 6026 bp->b_ioflags &= ~BIO_ERROR; 6027 vfs_busy_pages(bp, 0); 6028 bp->b_iooffset = dbtob(bp->b_blkno); 6029 bstrategy(bp); 6030 curthread->td_ru.ru_inblock++; 6031 error = bufwait(bp); 6032 if (error) { 6033 brelse(bp); 6034 return (error); 6035 } 6036 } 6037 level = lbn_level(lbn); 6038 lbnadd = lbn_offset(ip->i_fs, level); 6039 /* 6040 * Compute the offset of the last block we want to keep. Store 6041 * in the freework the first block we want to completely free. 6042 */ 6043 off = (lastlbn - -(lbn + level)) / lbnadd; 6044 if (off + 1 == NINDIR(ip->i_fs)) 6045 goto nowork; 6046 freework = newfreework(ip->i_ump, freeblks, NULL, lbn, blkno, 0, off+1, 6047 0); 6048 /* 6049 * Link the freework into the indirdep. This will prevent any new 6050 * allocations from proceeding until we are finished with the 6051 * truncate and the block is written. 6052 */ 6053 ACQUIRE_LOCK(&lk); 6054 indirdep = indirdep_lookup(mp, ip, bp); 6055 if (indirdep->ir_freeblks) 6056 panic("setup_trunc_indir: indirdep already truncated."); 6057 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, freework, fw_next); 6058 freework->fw_indir = indirdep; 6059 /* 6060 * Cancel any allocindirs that will not make it to disk. 6061 * We have to do this for all copies of the indirdep that 6062 * live on this newblk. 6063 */ 6064 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 6065 newblk_lookup(mp, dbtofsb(ip->i_fs, bp->b_blkno), 0, &newblk); 6066 LIST_FOREACH(indirn, &newblk->nb_indirdeps, ir_next) 6067 trunc_indirdep(indirn, freeblks, bp, off); 6068 } else 6069 trunc_indirdep(indirdep, freeblks, bp, off); 6070 FREE_LOCK(&lk); 6071 /* 6072 * Creation is protected by the buf lock. The saveddata is only 6073 * needed if a full truncation follows a partial truncation but it 6074 * is difficult to allocate in that case so we fetch it anyway. 6075 */ 6076 if (indirdep->ir_saveddata == NULL) 6077 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 6078 M_SOFTDEP_FLAGS); 6079 nowork: 6080 /* Fetch the blkno of the child and the zero start offset. */ 6081 if (ip->i_ump->um_fstype == UFS1) { 6082 blkno = ((ufs1_daddr_t *)bp->b_data)[off]; 6083 start = (uint8_t *)&((ufs1_daddr_t *)bp->b_data)[off+1]; 6084 } else { 6085 blkno = ((ufs2_daddr_t *)bp->b_data)[off]; 6086 start = (uint8_t *)&((ufs2_daddr_t *)bp->b_data)[off+1]; 6087 } 6088 if (freework) { 6089 /* Zero the truncated pointers. */ 6090 end = bp->b_data + bp->b_bcount; 6091 bzero(start, end - start); 6092 bdwrite(bp); 6093 } else 6094 bqrelse(bp); 6095 if (level == 0) 6096 return (0); 6097 lbn++; /* adjust level */ 6098 lbn -= (off * lbnadd); 6099 return setup_trunc_indir(freeblks, ip, lbn, lastlbn, blkno); 6100 } 6101 6102 /* 6103 * Complete the partial truncation of an indirect block setup by 6104 * setup_trunc_indir(). This zeros the truncated pointers in the saved 6105 * copy and writes them to disk before the freeblks is allowed to complete. 6106 */ 6107 static void 6108 complete_trunc_indir(freework) 6109 struct freework *freework; 6110 { 6111 struct freework *fwn; 6112 struct indirdep *indirdep; 6113 struct buf *bp; 6114 uintptr_t start; 6115 int count; 6116 6117 indirdep = freework->fw_indir; 6118 for (;;) { 6119 bp = indirdep->ir_bp; 6120 /* See if the block was discarded. */ 6121 if (bp == NULL) 6122 break; 6123 /* Inline part of getdirtybuf(). We dont want bremfree. */ 6124 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) 6125 break; 6126 if (BUF_LOCK(bp, 6127 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, &lk) == 0) 6128 BUF_UNLOCK(bp); 6129 ACQUIRE_LOCK(&lk); 6130 } 6131 mtx_assert(&lk, MA_OWNED); 6132 freework->fw_state |= DEPCOMPLETE; 6133 TAILQ_REMOVE(&indirdep->ir_trunc, freework, fw_next); 6134 /* 6135 * Zero the pointers in the saved copy. 6136 */ 6137 if (indirdep->ir_state & UFS1FMT) 6138 start = sizeof(ufs1_daddr_t); 6139 else 6140 start = sizeof(ufs2_daddr_t); 6141 start *= freework->fw_start; 6142 count = indirdep->ir_savebp->b_bcount - start; 6143 start += (uintptr_t)indirdep->ir_savebp->b_data; 6144 bzero((char *)start, count); 6145 /* 6146 * We need to start the next truncation in the list if it has not 6147 * been started yet. 6148 */ 6149 fwn = TAILQ_FIRST(&indirdep->ir_trunc); 6150 if (fwn != NULL) { 6151 if (fwn->fw_freeblks == indirdep->ir_freeblks) 6152 TAILQ_REMOVE(&indirdep->ir_trunc, fwn, fw_next); 6153 if ((fwn->fw_state & ONWORKLIST) == 0) 6154 freework_enqueue(fwn); 6155 } 6156 /* 6157 * If bp is NULL the block was fully truncated, restore 6158 * the saved block list otherwise free it if it is no 6159 * longer needed. 6160 */ 6161 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 6162 if (bp == NULL) 6163 bcopy(indirdep->ir_saveddata, 6164 indirdep->ir_savebp->b_data, 6165 indirdep->ir_savebp->b_bcount); 6166 free(indirdep->ir_saveddata, M_INDIRDEP); 6167 indirdep->ir_saveddata = NULL; 6168 } 6169 /* 6170 * When bp is NULL there is a full truncation pending. We 6171 * must wait for this full truncation to be journaled before 6172 * we can release this freework because the disk pointers will 6173 * never be written as zero. 6174 */ 6175 if (bp == NULL) { 6176 if (LIST_EMPTY(&indirdep->ir_freeblks->fb_jblkdephd)) 6177 handle_written_freework(freework); 6178 else 6179 WORKLIST_INSERT(&indirdep->ir_freeblks->fb_freeworkhd, 6180 &freework->fw_list); 6181 } else { 6182 /* Complete when the real copy is written. */ 6183 WORKLIST_INSERT(&bp->b_dep, &freework->fw_list); 6184 BUF_UNLOCK(bp); 6185 } 6186 } 6187 6188 /* 6189 * Calculate the number of blocks we are going to release where datablocks 6190 * is the current total and length is the new file size. 6191 */ 6192 ufs2_daddr_t 6193 blkcount(fs, datablocks, length) 6194 struct fs *fs; 6195 ufs2_daddr_t datablocks; 6196 off_t length; 6197 { 6198 off_t totblks, numblks; 6199 6200 totblks = 0; 6201 numblks = howmany(length, fs->fs_bsize); 6202 if (numblks <= NDADDR) { 6203 totblks = howmany(length, fs->fs_fsize); 6204 goto out; 6205 } 6206 totblks = blkstofrags(fs, numblks); 6207 numblks -= NDADDR; 6208 /* 6209 * Count all single, then double, then triple indirects required. 6210 * Subtracting one indirects worth of blocks for each pass 6211 * acknowledges one of each pointed to by the inode. 6212 */ 6213 for (;;) { 6214 totblks += blkstofrags(fs, howmany(numblks, NINDIR(fs))); 6215 numblks -= NINDIR(fs); 6216 if (numblks <= 0) 6217 break; 6218 numblks = howmany(numblks, NINDIR(fs)); 6219 } 6220 out: 6221 totblks = fsbtodb(fs, totblks); 6222 /* 6223 * Handle sparse files. We can't reclaim more blocks than the inode 6224 * references. We will correct it later in handle_complete_freeblks() 6225 * when we know the real count. 6226 */ 6227 if (totblks > datablocks) 6228 return (0); 6229 return (datablocks - totblks); 6230 } 6231 6232 /* 6233 * Handle freeblocks for journaled softupdate filesystems. 6234 * 6235 * Contrary to normal softupdates, we must preserve the block pointers in 6236 * indirects until their subordinates are free. This is to avoid journaling 6237 * every block that is freed which may consume more space than the journal 6238 * itself. The recovery program will see the free block journals at the 6239 * base of the truncated area and traverse them to reclaim space. The 6240 * pointers in the inode may be cleared immediately after the journal 6241 * records are written because each direct and indirect pointer in the 6242 * inode is recorded in a journal. This permits full truncation to proceed 6243 * asynchronously. The write order is journal -> inode -> cgs -> indirects. 6244 * 6245 * The algorithm is as follows: 6246 * 1) Traverse the in-memory state and create journal entries to release 6247 * the relevant blocks and full indirect trees. 6248 * 2) Traverse the indirect block chain adding partial truncation freework 6249 * records to indirects in the path to lastlbn. The freework will 6250 * prevent new allocation dependencies from being satisfied in this 6251 * indirect until the truncation completes. 6252 * 3) Read and lock the inode block, performing an update with the new size 6253 * and pointers. This prevents truncated data from becoming valid on 6254 * disk through step 4. 6255 * 4) Reap unsatisfied dependencies that are beyond the truncated area, 6256 * eliminate journal work for those records that do not require it. 6257 * 5) Schedule the journal records to be written followed by the inode block. 6258 * 6) Allocate any necessary frags for the end of file. 6259 * 7) Zero any partially truncated blocks. 6260 * 6261 * From this truncation proceeds asynchronously using the freework and 6262 * indir_trunc machinery. The file will not be extended again into a 6263 * partially truncated indirect block until all work is completed but 6264 * the normal dependency mechanism ensures that it is rolled back/forward 6265 * as appropriate. Further truncation may occur without delay and is 6266 * serialized in indir_trunc(). 6267 */ 6268 void 6269 softdep_journal_freeblocks(ip, cred, length, flags) 6270 struct inode *ip; /* The inode whose length is to be reduced */ 6271 struct ucred *cred; 6272 off_t length; /* The new length for the file */ 6273 int flags; /* IO_EXT and/or IO_NORMAL */ 6274 { 6275 struct freeblks *freeblks, *fbn; 6276 struct worklist *wk, *wkn; 6277 struct inodedep *inodedep; 6278 struct jblkdep *jblkdep; 6279 struct allocdirect *adp, *adpn; 6280 struct fs *fs; 6281 struct buf *bp; 6282 struct vnode *vp; 6283 struct mount *mp; 6284 ufs2_daddr_t extblocks, datablocks; 6285 ufs_lbn_t tmpval, lbn, lastlbn; 6286 int frags, lastoff, iboff, allocblock, needj, dflags, error, i; 6287 6288 fs = ip->i_fs; 6289 mp = UFSTOVFS(ip->i_ump); 6290 vp = ITOV(ip); 6291 needj = 1; 6292 iboff = -1; 6293 allocblock = 0; 6294 extblocks = 0; 6295 datablocks = 0; 6296 frags = 0; 6297 freeblks = newfreeblks(mp, ip); 6298 ACQUIRE_LOCK(&lk); 6299 /* 6300 * If we're truncating a removed file that will never be written 6301 * we don't need to journal the block frees. The canceled journals 6302 * for the allocations will suffice. 6303 */ 6304 dflags = DEPALLOC; 6305 if (IS_SNAPSHOT(ip)) 6306 dflags |= NODELAY; 6307 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6308 if ((inodedep->id_state & (UNLINKED | DEPCOMPLETE)) == UNLINKED && 6309 length == 0) 6310 needj = 0; 6311 CTR3(KTR_SUJ, "softdep_journal_freeblks: ip %d length %ld needj %d", 6312 ip->i_number, length, needj); 6313 FREE_LOCK(&lk); 6314 /* 6315 * Calculate the lbn that we are truncating to. This results in -1 6316 * if we're truncating the 0 bytes. So it is the last lbn we want 6317 * to keep, not the first lbn we want to truncate. 6318 */ 6319 lastlbn = lblkno(fs, length + fs->fs_bsize - 1) - 1; 6320 lastoff = blkoff(fs, length); 6321 /* 6322 * Compute frags we are keeping in lastlbn. 0 means all. 6323 */ 6324 if (lastlbn >= 0 && lastlbn < NDADDR) { 6325 frags = fragroundup(fs, lastoff); 6326 /* adp offset of last valid allocdirect. */ 6327 iboff = lastlbn; 6328 } else if (lastlbn > 0) 6329 iboff = NDADDR; 6330 if (fs->fs_magic == FS_UFS2_MAGIC) 6331 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6332 /* 6333 * Handle normal data blocks and indirects. This section saves 6334 * values used after the inode update to complete frag and indirect 6335 * truncation. 6336 */ 6337 if ((flags & IO_NORMAL) != 0) { 6338 /* 6339 * Handle truncation of whole direct and indirect blocks. 6340 */ 6341 for (i = iboff + 1; i < NDADDR; i++) 6342 setup_freedirect(freeblks, ip, i, needj); 6343 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6344 i++, lbn += tmpval, tmpval *= NINDIR(fs)) { 6345 /* Release a whole indirect tree. */ 6346 if (lbn > lastlbn) { 6347 setup_freeindir(freeblks, ip, i, -lbn -i, 6348 needj); 6349 continue; 6350 } 6351 iboff = i + NDADDR; 6352 /* 6353 * Traverse partially truncated indirect tree. 6354 */ 6355 if (lbn <= lastlbn && lbn + tmpval - 1 > lastlbn) 6356 setup_trunc_indir(freeblks, ip, -lbn - i, 6357 lastlbn, DIP(ip, i_ib[i])); 6358 } 6359 /* 6360 * Handle partial truncation to a frag boundary. 6361 */ 6362 if (frags) { 6363 ufs2_daddr_t blkno; 6364 long oldfrags; 6365 6366 oldfrags = blksize(fs, ip, lastlbn); 6367 blkno = DIP(ip, i_db[lastlbn]); 6368 if (blkno && oldfrags != frags) { 6369 oldfrags -= frags; 6370 oldfrags = numfrags(ip->i_fs, oldfrags); 6371 blkno += numfrags(ip->i_fs, frags); 6372 newfreework(ip->i_ump, freeblks, NULL, lastlbn, 6373 blkno, oldfrags, 0, needj); 6374 } else if (blkno == 0) 6375 allocblock = 1; 6376 } 6377 /* 6378 * Add a journal record for partial truncate if we are 6379 * handling indirect blocks. Non-indirects need no extra 6380 * journaling. 6381 */ 6382 if (length != 0 && lastlbn >= NDADDR) { 6383 ip->i_flag |= IN_TRUNCATED; 6384 newjtrunc(freeblks, length, 0); 6385 } 6386 ip->i_size = length; 6387 DIP_SET(ip, i_size, ip->i_size); 6388 datablocks = DIP(ip, i_blocks) - extblocks; 6389 if (length != 0) 6390 datablocks = blkcount(ip->i_fs, datablocks, length); 6391 freeblks->fb_len = length; 6392 } 6393 if ((flags & IO_EXT) != 0) { 6394 for (i = 0; i < NXADDR; i++) 6395 setup_freeext(freeblks, ip, i, needj); 6396 ip->i_din2->di_extsize = 0; 6397 datablocks += extblocks; 6398 } 6399 #ifdef QUOTA 6400 /* Reference the quotas in case the block count is wrong in the end. */ 6401 quotaref(vp, freeblks->fb_quota); 6402 (void) chkdq(ip, -datablocks, NOCRED, 0); 6403 #endif 6404 freeblks->fb_chkcnt = -datablocks; 6405 UFS_LOCK(ip->i_ump); 6406 fs->fs_pendingblocks += datablocks; 6407 UFS_UNLOCK(ip->i_ump); 6408 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6409 /* 6410 * Handle truncation of incomplete alloc direct dependencies. We 6411 * hold the inode block locked to prevent incomplete dependencies 6412 * from reaching the disk while we are eliminating those that 6413 * have been truncated. This is a partially inlined ffs_update(). 6414 */ 6415 ufs_itimes(vp); 6416 ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED); 6417 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6418 (int)fs->fs_bsize, cred, &bp); 6419 if (error) { 6420 brelse(bp); 6421 softdep_error("softdep_journal_freeblocks", error); 6422 return; 6423 } 6424 if (bp->b_bufsize == fs->fs_bsize) 6425 bp->b_flags |= B_CLUSTEROK; 6426 softdep_update_inodeblock(ip, bp, 0); 6427 if (ip->i_ump->um_fstype == UFS1) 6428 *((struct ufs1_dinode *)bp->b_data + 6429 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1; 6430 else 6431 *((struct ufs2_dinode *)bp->b_data + 6432 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2; 6433 ACQUIRE_LOCK(&lk); 6434 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6435 if ((inodedep->id_state & IOSTARTED) != 0) 6436 panic("softdep_setup_freeblocks: inode busy"); 6437 /* 6438 * Add the freeblks structure to the list of operations that 6439 * must await the zero'ed inode being written to disk. If we 6440 * still have a bitmap dependency (needj), then the inode 6441 * has never been written to disk, so we can process the 6442 * freeblks below once we have deleted the dependencies. 6443 */ 6444 if (needj) 6445 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6446 else 6447 freeblks->fb_state |= COMPLETE; 6448 if ((flags & IO_NORMAL) != 0) { 6449 TAILQ_FOREACH_SAFE(adp, &inodedep->id_inoupdt, ad_next, adpn) { 6450 if (adp->ad_offset > iboff) 6451 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6452 freeblks); 6453 /* 6454 * Truncate the allocdirect. We could eliminate 6455 * or modify journal records as well. 6456 */ 6457 else if (adp->ad_offset == iboff && frags) 6458 adp->ad_newsize = frags; 6459 } 6460 } 6461 if ((flags & IO_EXT) != 0) 6462 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6463 cancel_allocdirect(&inodedep->id_extupdt, adp, 6464 freeblks); 6465 /* 6466 * Scan the bufwait list for newblock dependencies that will never 6467 * make it to disk. 6468 */ 6469 LIST_FOREACH_SAFE(wk, &inodedep->id_bufwait, wk_list, wkn) { 6470 if (wk->wk_type != D_ALLOCDIRECT) 6471 continue; 6472 adp = WK_ALLOCDIRECT(wk); 6473 if (((flags & IO_NORMAL) != 0 && (adp->ad_offset > iboff)) || 6474 ((flags & IO_EXT) != 0 && (adp->ad_state & EXTDATA))) { 6475 cancel_jfreeblk(freeblks, adp->ad_newblkno); 6476 cancel_newblk(WK_NEWBLK(wk), NULL, &freeblks->fb_jwork); 6477 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 6478 } 6479 } 6480 /* 6481 * Add journal work. 6482 */ 6483 LIST_FOREACH(jblkdep, &freeblks->fb_jblkdephd, jb_deps) 6484 add_to_journal(&jblkdep->jb_list); 6485 FREE_LOCK(&lk); 6486 bdwrite(bp); 6487 /* 6488 * Truncate dependency structures beyond length. 6489 */ 6490 trunc_dependencies(ip, freeblks, lastlbn, frags, flags); 6491 /* 6492 * This is only set when we need to allocate a fragment because 6493 * none existed at the end of a frag-sized file. It handles only 6494 * allocating a new, zero filled block. 6495 */ 6496 if (allocblock) { 6497 ip->i_size = length - lastoff; 6498 DIP_SET(ip, i_size, ip->i_size); 6499 error = UFS_BALLOC(vp, length - 1, 1, cred, BA_CLRBUF, &bp); 6500 if (error != 0) { 6501 softdep_error("softdep_journal_freeblks", error); 6502 return; 6503 } 6504 ip->i_size = length; 6505 DIP_SET(ip, i_size, length); 6506 ip->i_flag |= IN_CHANGE | IN_UPDATE; 6507 allocbuf(bp, frags); 6508 ffs_update(vp, 0); 6509 bawrite(bp); 6510 } else if (lastoff != 0 && vp->v_type != VDIR) { 6511 int size; 6512 6513 /* 6514 * Zero the end of a truncated frag or block. 6515 */ 6516 size = sblksize(fs, length, lastlbn); 6517 error = bread(vp, lastlbn, size, cred, &bp); 6518 if (error) { 6519 softdep_error("softdep_journal_freeblks", error); 6520 return; 6521 } 6522 bzero((char *)bp->b_data + lastoff, size - lastoff); 6523 bawrite(bp); 6524 6525 } 6526 ACQUIRE_LOCK(&lk); 6527 inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6528 TAILQ_INSERT_TAIL(&inodedep->id_freeblklst, freeblks, fb_next); 6529 freeblks->fb_state |= DEPCOMPLETE | ONDEPLIST; 6530 /* 6531 * We zero earlier truncations so they don't erroneously 6532 * update i_blocks. 6533 */ 6534 if (freeblks->fb_len == 0 && (flags & IO_NORMAL) != 0) 6535 TAILQ_FOREACH(fbn, &inodedep->id_freeblklst, fb_next) 6536 fbn->fb_len = 0; 6537 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE && 6538 LIST_EMPTY(&freeblks->fb_jblkdephd)) 6539 freeblks->fb_state |= INPROGRESS; 6540 else 6541 freeblks = NULL; 6542 FREE_LOCK(&lk); 6543 if (freeblks) 6544 handle_workitem_freeblocks(freeblks, 0); 6545 trunc_pages(ip, length, extblocks, flags); 6546 6547 } 6548 6549 /* 6550 * Flush a JOP_SYNC to the journal. 6551 */ 6552 void 6553 softdep_journal_fsync(ip) 6554 struct inode *ip; 6555 { 6556 struct jfsync *jfsync; 6557 6558 if ((ip->i_flag & IN_TRUNCATED) == 0) 6559 return; 6560 ip->i_flag &= ~IN_TRUNCATED; 6561 jfsync = malloc(sizeof(*jfsync), M_JFSYNC, M_SOFTDEP_FLAGS | M_ZERO); 6562 workitem_alloc(&jfsync->jfs_list, D_JFSYNC, UFSTOVFS(ip->i_ump)); 6563 jfsync->jfs_size = ip->i_size; 6564 jfsync->jfs_ino = ip->i_number; 6565 ACQUIRE_LOCK(&lk); 6566 add_to_journal(&jfsync->jfs_list); 6567 jwait(&jfsync->jfs_list, MNT_WAIT); 6568 FREE_LOCK(&lk); 6569 } 6570 6571 /* 6572 * Block de-allocation dependencies. 6573 * 6574 * When blocks are de-allocated, the on-disk pointers must be nullified before 6575 * the blocks are made available for use by other files. (The true 6576 * requirement is that old pointers must be nullified before new on-disk 6577 * pointers are set. We chose this slightly more stringent requirement to 6578 * reduce complexity.) Our implementation handles this dependency by updating 6579 * the inode (or indirect block) appropriately but delaying the actual block 6580 * de-allocation (i.e., freemap and free space count manipulation) until 6581 * after the updated versions reach stable storage. After the disk is 6582 * updated, the blocks can be safely de-allocated whenever it is convenient. 6583 * This implementation handles only the common case of reducing a file's 6584 * length to zero. Other cases are handled by the conventional synchronous 6585 * write approach. 6586 * 6587 * The ffs implementation with which we worked double-checks 6588 * the state of the block pointers and file size as it reduces 6589 * a file's length. Some of this code is replicated here in our 6590 * soft updates implementation. The freeblks->fb_chkcnt field is 6591 * used to transfer a part of this information to the procedure 6592 * that eventually de-allocates the blocks. 6593 * 6594 * This routine should be called from the routine that shortens 6595 * a file's length, before the inode's size or block pointers 6596 * are modified. It will save the block pointer information for 6597 * later release and zero the inode so that the calling routine 6598 * can release it. 6599 */ 6600 void 6601 softdep_setup_freeblocks(ip, length, flags) 6602 struct inode *ip; /* The inode whose length is to be reduced */ 6603 off_t length; /* The new length for the file */ 6604 int flags; /* IO_EXT and/or IO_NORMAL */ 6605 { 6606 struct ufs1_dinode *dp1; 6607 struct ufs2_dinode *dp2; 6608 struct freeblks *freeblks; 6609 struct inodedep *inodedep; 6610 struct allocdirect *adp; 6611 struct buf *bp; 6612 struct fs *fs; 6613 ufs2_daddr_t extblocks, datablocks; 6614 struct mount *mp; 6615 int i, delay, error, dflags; 6616 ufs_lbn_t tmpval; 6617 ufs_lbn_t lbn; 6618 6619 CTR2(KTR_SUJ, "softdep_setup_freeblks: ip %d length %ld", 6620 ip->i_number, length); 6621 fs = ip->i_fs; 6622 mp = UFSTOVFS(ip->i_ump); 6623 if (length != 0) 6624 panic("softdep_setup_freeblocks: non-zero length"); 6625 freeblks = newfreeblks(mp, ip); 6626 extblocks = 0; 6627 datablocks = 0; 6628 if (fs->fs_magic == FS_UFS2_MAGIC) 6629 extblocks = btodb(fragroundup(fs, ip->i_din2->di_extsize)); 6630 if ((flags & IO_NORMAL) != 0) { 6631 for (i = 0; i < NDADDR; i++) 6632 setup_freedirect(freeblks, ip, i, 0); 6633 for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; 6634 i++, lbn += tmpval, tmpval *= NINDIR(fs)) 6635 setup_freeindir(freeblks, ip, i, -lbn -i, 0); 6636 ip->i_size = 0; 6637 DIP_SET(ip, i_size, 0); 6638 datablocks = DIP(ip, i_blocks) - extblocks; 6639 } 6640 if ((flags & IO_EXT) != 0) { 6641 for (i = 0; i < NXADDR; i++) 6642 setup_freeext(freeblks, ip, i, 0); 6643 ip->i_din2->di_extsize = 0; 6644 datablocks += extblocks; 6645 } 6646 #ifdef QUOTA 6647 /* Reference the quotas in case the block count is wrong in the end. */ 6648 quotaref(ITOV(ip), freeblks->fb_quota); 6649 (void) chkdq(ip, -datablocks, NOCRED, 0); 6650 #endif 6651 freeblks->fb_chkcnt = -datablocks; 6652 UFS_LOCK(ip->i_ump); 6653 fs->fs_pendingblocks += datablocks; 6654 UFS_UNLOCK(ip->i_ump); 6655 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - datablocks); 6656 /* 6657 * Push the zero'ed inode to to its disk buffer so that we are free 6658 * to delete its dependencies below. Once the dependencies are gone 6659 * the buffer can be safely released. 6660 */ 6661 if ((error = bread(ip->i_devvp, 6662 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 6663 (int)fs->fs_bsize, NOCRED, &bp)) != 0) { 6664 brelse(bp); 6665 softdep_error("softdep_setup_freeblocks", error); 6666 } 6667 if (ip->i_ump->um_fstype == UFS1) { 6668 dp1 = ((struct ufs1_dinode *)bp->b_data + 6669 ino_to_fsbo(fs, ip->i_number)); 6670 ip->i_din1->di_freelink = dp1->di_freelink; 6671 *dp1 = *ip->i_din1; 6672 } else { 6673 dp2 = ((struct ufs2_dinode *)bp->b_data + 6674 ino_to_fsbo(fs, ip->i_number)); 6675 ip->i_din2->di_freelink = dp2->di_freelink; 6676 *dp2 = *ip->i_din2; 6677 } 6678 /* 6679 * Find and eliminate any inode dependencies. 6680 */ 6681 ACQUIRE_LOCK(&lk); 6682 dflags = DEPALLOC; 6683 if (IS_SNAPSHOT(ip)) 6684 dflags |= NODELAY; 6685 (void) inodedep_lookup(mp, ip->i_number, dflags, &inodedep); 6686 if ((inodedep->id_state & IOSTARTED) != 0) 6687 panic("softdep_setup_freeblocks: inode busy"); 6688 /* 6689 * Add the freeblks structure to the list of operations that 6690 * must await the zero'ed inode being written to disk. If we 6691 * still have a bitmap dependency (delay == 0), then the inode 6692 * has never been written to disk, so we can process the 6693 * freeblks below once we have deleted the dependencies. 6694 */ 6695 delay = (inodedep->id_state & DEPCOMPLETE); 6696 if (delay) 6697 WORKLIST_INSERT(&bp->b_dep, &freeblks->fb_list); 6698 else 6699 freeblks->fb_state |= COMPLETE; 6700 /* 6701 * Because the file length has been truncated to zero, any 6702 * pending block allocation dependency structures associated 6703 * with this inode are obsolete and can simply be de-allocated. 6704 * We must first merge the two dependency lists to get rid of 6705 * any duplicate freefrag structures, then purge the merged list. 6706 * If we still have a bitmap dependency, then the inode has never 6707 * been written to disk, so we can free any fragments without delay. 6708 */ 6709 if (flags & IO_NORMAL) { 6710 merge_inode_lists(&inodedep->id_newinoupdt, 6711 &inodedep->id_inoupdt); 6712 while ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != 0) 6713 cancel_allocdirect(&inodedep->id_inoupdt, adp, 6714 freeblks); 6715 } 6716 if (flags & IO_EXT) { 6717 merge_inode_lists(&inodedep->id_newextupdt, 6718 &inodedep->id_extupdt); 6719 while ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != 0) 6720 cancel_allocdirect(&inodedep->id_extupdt, adp, 6721 freeblks); 6722 } 6723 FREE_LOCK(&lk); 6724 bdwrite(bp); 6725 trunc_dependencies(ip, freeblks, -1, 0, flags); 6726 ACQUIRE_LOCK(&lk); 6727 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 6728 (void) free_inodedep(inodedep); 6729 freeblks->fb_state |= DEPCOMPLETE; 6730 /* 6731 * If the inode with zeroed block pointers is now on disk 6732 * we can start freeing blocks. 6733 */ 6734 if ((freeblks->fb_state & ALLCOMPLETE) == ALLCOMPLETE) 6735 freeblks->fb_state |= INPROGRESS; 6736 else 6737 freeblks = NULL; 6738 FREE_LOCK(&lk); 6739 if (freeblks) 6740 handle_workitem_freeblocks(freeblks, 0); 6741 trunc_pages(ip, length, extblocks, flags); 6742 } 6743 6744 /* 6745 * Eliminate pages from the page cache that back parts of this inode and 6746 * adjust the vnode pager's idea of our size. This prevents stale data 6747 * from hanging around in the page cache. 6748 */ 6749 static void 6750 trunc_pages(ip, length, extblocks, flags) 6751 struct inode *ip; 6752 off_t length; 6753 ufs2_daddr_t extblocks; 6754 int flags; 6755 { 6756 struct vnode *vp; 6757 struct fs *fs; 6758 ufs_lbn_t lbn; 6759 off_t end, extend; 6760 6761 vp = ITOV(ip); 6762 fs = ip->i_fs; 6763 extend = OFF_TO_IDX(lblktosize(fs, -extblocks)); 6764 if ((flags & IO_EXT) != 0) 6765 vn_pages_remove(vp, extend, 0); 6766 if ((flags & IO_NORMAL) == 0) 6767 return; 6768 BO_LOCK(&vp->v_bufobj); 6769 drain_output(vp); 6770 BO_UNLOCK(&vp->v_bufobj); 6771 /* 6772 * The vnode pager eliminates file pages we eliminate indirects 6773 * below. 6774 */ 6775 vnode_pager_setsize(vp, length); 6776 /* 6777 * Calculate the end based on the last indirect we want to keep. If 6778 * the block extends into indirects we can just use the negative of 6779 * its lbn. Doubles and triples exist at lower numbers so we must 6780 * be careful not to remove those, if they exist. double and triple 6781 * indirect lbns do not overlap with others so it is not important 6782 * to verify how many levels are required. 6783 */ 6784 lbn = lblkno(fs, length); 6785 if (lbn >= NDADDR) { 6786 /* Calculate the virtual lbn of the triple indirect. */ 6787 lbn = -lbn - (NIADDR - 1); 6788 end = OFF_TO_IDX(lblktosize(fs, lbn)); 6789 } else 6790 end = extend; 6791 vn_pages_remove(vp, OFF_TO_IDX(OFF_MAX), end); 6792 } 6793 6794 /* 6795 * See if the buf bp is in the range eliminated by truncation. 6796 */ 6797 static int 6798 trunc_check_buf(bp, blkoffp, lastlbn, lastoff, flags) 6799 struct buf *bp; 6800 int *blkoffp; 6801 ufs_lbn_t lastlbn; 6802 int lastoff; 6803 int flags; 6804 { 6805 ufs_lbn_t lbn; 6806 6807 *blkoffp = 0; 6808 /* Only match ext/normal blocks as appropriate. */ 6809 if (((flags & IO_EXT) == 0 && (bp->b_xflags & BX_ALTDATA)) || 6810 ((flags & IO_NORMAL) == 0 && (bp->b_xflags & BX_ALTDATA) == 0)) 6811 return (0); 6812 /* ALTDATA is always a full truncation. */ 6813 if ((bp->b_xflags & BX_ALTDATA) != 0) 6814 return (1); 6815 /* -1 is full truncation. */ 6816 if (lastlbn == -1) 6817 return (1); 6818 /* 6819 * If this is a partial truncate we only want those 6820 * blocks and indirect blocks that cover the range 6821 * we're after. 6822 */ 6823 lbn = bp->b_lblkno; 6824 if (lbn < 0) 6825 lbn = -(lbn + lbn_level(lbn)); 6826 if (lbn < lastlbn) 6827 return (0); 6828 /* Here we only truncate lblkno if it's partial. */ 6829 if (lbn == lastlbn) { 6830 if (lastoff == 0) 6831 return (0); 6832 *blkoffp = lastoff; 6833 } 6834 return (1); 6835 } 6836 6837 /* 6838 * Eliminate any dependencies that exist in memory beyond lblkno:off 6839 */ 6840 static void 6841 trunc_dependencies(ip, freeblks, lastlbn, lastoff, flags) 6842 struct inode *ip; 6843 struct freeblks *freeblks; 6844 ufs_lbn_t lastlbn; 6845 int lastoff; 6846 int flags; 6847 { 6848 struct bufobj *bo; 6849 struct vnode *vp; 6850 struct buf *bp; 6851 struct fs *fs; 6852 int blkoff; 6853 6854 /* 6855 * We must wait for any I/O in progress to finish so that 6856 * all potential buffers on the dirty list will be visible. 6857 * Once they are all there, walk the list and get rid of 6858 * any dependencies. 6859 */ 6860 fs = ip->i_fs; 6861 vp = ITOV(ip); 6862 bo = &vp->v_bufobj; 6863 BO_LOCK(bo); 6864 drain_output(vp); 6865 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) 6866 bp->b_vflags &= ~BV_SCANNED; 6867 restart: 6868 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) { 6869 if (bp->b_vflags & BV_SCANNED) 6870 continue; 6871 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 6872 bp->b_vflags |= BV_SCANNED; 6873 continue; 6874 } 6875 if ((bp = getdirtybuf(bp, BO_MTX(bo), MNT_WAIT)) == NULL) 6876 goto restart; 6877 BO_UNLOCK(bo); 6878 if (deallocate_dependencies(bp, freeblks, blkoff)) 6879 bqrelse(bp); 6880 else 6881 brelse(bp); 6882 BO_LOCK(bo); 6883 goto restart; 6884 } 6885 /* 6886 * Now do the work of vtruncbuf while also matching indirect blocks. 6887 */ 6888 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) 6889 bp->b_vflags &= ~BV_SCANNED; 6890 cleanrestart: 6891 TAILQ_FOREACH(bp, &bo->bo_clean.bv_hd, b_bobufs) { 6892 if (bp->b_vflags & BV_SCANNED) 6893 continue; 6894 if (!trunc_check_buf(bp, &blkoff, lastlbn, lastoff, flags)) { 6895 bp->b_vflags |= BV_SCANNED; 6896 continue; 6897 } 6898 if (BUF_LOCK(bp, 6899 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, 6900 BO_MTX(bo)) == ENOLCK) { 6901 BO_LOCK(bo); 6902 goto cleanrestart; 6903 } 6904 bp->b_vflags |= BV_SCANNED; 6905 BO_LOCK(bo); 6906 bremfree(bp); 6907 BO_UNLOCK(bo); 6908 if (blkoff != 0) { 6909 allocbuf(bp, blkoff); 6910 bqrelse(bp); 6911 } else { 6912 bp->b_flags |= B_INVAL | B_NOCACHE | B_RELBUF; 6913 brelse(bp); 6914 } 6915 BO_LOCK(bo); 6916 goto cleanrestart; 6917 } 6918 drain_output(vp); 6919 BO_UNLOCK(bo); 6920 } 6921 6922 static int 6923 cancel_pagedep(pagedep, freeblks, blkoff) 6924 struct pagedep *pagedep; 6925 struct freeblks *freeblks; 6926 int blkoff; 6927 { 6928 struct jremref *jremref; 6929 struct jmvref *jmvref; 6930 struct dirrem *dirrem, *tmp; 6931 int i; 6932 6933 /* 6934 * Copy any directory remove dependencies to the list 6935 * to be processed after the freeblks proceeds. If 6936 * directory entry never made it to disk they 6937 * can be dumped directly onto the work list. 6938 */ 6939 LIST_FOREACH_SAFE(dirrem, &pagedep->pd_dirremhd, dm_next, tmp) { 6940 /* Skip this directory removal if it is intended to remain. */ 6941 if (dirrem->dm_offset < blkoff) 6942 continue; 6943 /* 6944 * If there are any dirrems we wait for the journal write 6945 * to complete and then restart the buf scan as the lock 6946 * has been dropped. 6947 */ 6948 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) { 6949 jwait(&jremref->jr_list, MNT_WAIT); 6950 return (ERESTART); 6951 } 6952 LIST_REMOVE(dirrem, dm_next); 6953 dirrem->dm_dirinum = pagedep->pd_ino; 6954 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &dirrem->dm_list); 6955 } 6956 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) { 6957 jwait(&jmvref->jm_list, MNT_WAIT); 6958 return (ERESTART); 6959 } 6960 /* 6961 * When we're partially truncating a pagedep we just want to flush 6962 * journal entries and return. There can not be any adds in the 6963 * truncated portion of the directory and newblk must remain if 6964 * part of the block remains. 6965 */ 6966 if (blkoff != 0) { 6967 struct diradd *dap; 6968 6969 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 6970 if (dap->da_offset > blkoff) 6971 panic("cancel_pagedep: diradd %p off %d > %d", 6972 dap, dap->da_offset, blkoff); 6973 for (i = 0; i < DAHASHSZ; i++) 6974 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) 6975 if (dap->da_offset > blkoff) 6976 panic("cancel_pagedep: diradd %p off %d > %d", 6977 dap, dap->da_offset, blkoff); 6978 return (0); 6979 } 6980 /* 6981 * There should be no directory add dependencies present 6982 * as the directory could not be truncated until all 6983 * children were removed. 6984 */ 6985 KASSERT(LIST_FIRST(&pagedep->pd_pendinghd) == NULL, 6986 ("deallocate_dependencies: pendinghd != NULL")); 6987 for (i = 0; i < DAHASHSZ; i++) 6988 KASSERT(LIST_FIRST(&pagedep->pd_diraddhd[i]) == NULL, 6989 ("deallocate_dependencies: diraddhd != NULL")); 6990 if ((pagedep->pd_state & NEWBLOCK) != 0) 6991 free_newdirblk(pagedep->pd_newdirblk); 6992 if (free_pagedep(pagedep) == 0) 6993 panic("Failed to free pagedep %p", pagedep); 6994 return (0); 6995 } 6996 6997 /* 6998 * Reclaim any dependency structures from a buffer that is about to 6999 * be reallocated to a new vnode. The buffer must be locked, thus, 7000 * no I/O completion operations can occur while we are manipulating 7001 * its associated dependencies. The mutex is held so that other I/O's 7002 * associated with related dependencies do not occur. 7003 */ 7004 static int 7005 deallocate_dependencies(bp, freeblks, off) 7006 struct buf *bp; 7007 struct freeblks *freeblks; 7008 int off; 7009 { 7010 struct indirdep *indirdep; 7011 struct pagedep *pagedep; 7012 struct allocdirect *adp; 7013 struct worklist *wk, *wkn; 7014 7015 ACQUIRE_LOCK(&lk); 7016 LIST_FOREACH_SAFE(wk, &bp->b_dep, wk_list, wkn) { 7017 switch (wk->wk_type) { 7018 case D_INDIRDEP: 7019 indirdep = WK_INDIRDEP(wk); 7020 if (bp->b_lblkno >= 0 || 7021 bp->b_blkno != indirdep->ir_savebp->b_lblkno) 7022 panic("deallocate_dependencies: not indir"); 7023 cancel_indirdep(indirdep, bp, freeblks); 7024 continue; 7025 7026 case D_PAGEDEP: 7027 pagedep = WK_PAGEDEP(wk); 7028 if (cancel_pagedep(pagedep, freeblks, off)) { 7029 FREE_LOCK(&lk); 7030 return (ERESTART); 7031 } 7032 continue; 7033 7034 case D_ALLOCINDIR: 7035 /* 7036 * Simply remove the allocindir, we'll find it via 7037 * the indirdep where we can clear pointers if 7038 * needed. 7039 */ 7040 WORKLIST_REMOVE(wk); 7041 continue; 7042 7043 case D_FREEWORK: 7044 /* 7045 * A truncation is waiting for the zero'd pointers 7046 * to be written. It can be freed when the freeblks 7047 * is journaled. 7048 */ 7049 WORKLIST_REMOVE(wk); 7050 wk->wk_state |= ONDEPLIST; 7051 WORKLIST_INSERT(&freeblks->fb_freeworkhd, wk); 7052 break; 7053 7054 case D_ALLOCDIRECT: 7055 adp = WK_ALLOCDIRECT(wk); 7056 if (off != 0) 7057 continue; 7058 /* FALLTHROUGH */ 7059 default: 7060 panic("deallocate_dependencies: Unexpected type %s", 7061 TYPENAME(wk->wk_type)); 7062 /* NOTREACHED */ 7063 } 7064 } 7065 FREE_LOCK(&lk); 7066 /* 7067 * Don't throw away this buf, we were partially truncating and 7068 * some deps may always remain. 7069 */ 7070 if (off) { 7071 allocbuf(bp, off); 7072 bp->b_vflags |= BV_SCANNED; 7073 return (EBUSY); 7074 } 7075 bp->b_flags |= B_INVAL | B_NOCACHE; 7076 7077 return (0); 7078 } 7079 7080 /* 7081 * An allocdirect is being canceled due to a truncate. We must make sure 7082 * the journal entry is released in concert with the blkfree that releases 7083 * the storage. Completed journal entries must not be released until the 7084 * space is no longer pointed to by the inode or in the bitmap. 7085 */ 7086 static void 7087 cancel_allocdirect(adphead, adp, freeblks) 7088 struct allocdirectlst *adphead; 7089 struct allocdirect *adp; 7090 struct freeblks *freeblks; 7091 { 7092 struct freework *freework; 7093 struct newblk *newblk; 7094 struct worklist *wk; 7095 7096 TAILQ_REMOVE(adphead, adp, ad_next); 7097 newblk = (struct newblk *)adp; 7098 freework = NULL; 7099 /* 7100 * Find the correct freework structure. 7101 */ 7102 LIST_FOREACH(wk, &freeblks->fb_freeworkhd, wk_list) { 7103 if (wk->wk_type != D_FREEWORK) 7104 continue; 7105 freework = WK_FREEWORK(wk); 7106 if (freework->fw_blkno == newblk->nb_newblkno) 7107 break; 7108 } 7109 if (freework == NULL) 7110 panic("cancel_allocdirect: Freework not found"); 7111 /* 7112 * If a newblk exists at all we still have the journal entry that 7113 * initiated the allocation so we do not need to journal the free. 7114 */ 7115 cancel_jfreeblk(freeblks, freework->fw_blkno); 7116 /* 7117 * If the journal hasn't been written the jnewblk must be passed 7118 * to the call to ffs_blkfree that reclaims the space. We accomplish 7119 * this by linking the journal dependency into the freework to be 7120 * freed when freework_freeblock() is called. If the journal has 7121 * been written we can simply reclaim the journal space when the 7122 * freeblks work is complete. 7123 */ 7124 freework->fw_jnewblk = cancel_newblk(newblk, &freework->fw_list, 7125 &freeblks->fb_jwork); 7126 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 7127 } 7128 7129 7130 /* 7131 * Cancel a new block allocation. May be an indirect or direct block. We 7132 * remove it from various lists and return any journal record that needs to 7133 * be resolved by the caller. 7134 * 7135 * A special consideration is made for indirects which were never pointed 7136 * at on disk and will never be found once this block is released. 7137 */ 7138 static struct jnewblk * 7139 cancel_newblk(newblk, wk, wkhd) 7140 struct newblk *newblk; 7141 struct worklist *wk; 7142 struct workhead *wkhd; 7143 { 7144 struct jnewblk *jnewblk; 7145 7146 CTR1(KTR_SUJ, "cancel_newblk: blkno %jd", newblk->nb_newblkno); 7147 7148 newblk->nb_state |= GOINGAWAY; 7149 /* 7150 * Previously we traversed the completedhd on each indirdep 7151 * attached to this newblk to cancel them and gather journal 7152 * work. Since we need only the oldest journal segment and 7153 * the lowest point on the tree will always have the oldest 7154 * journal segment we are free to release the segments 7155 * of any subordinates and may leave the indirdep list to 7156 * indirdep_complete() when this newblk is freed. 7157 */ 7158 if (newblk->nb_state & ONDEPLIST) { 7159 newblk->nb_state &= ~ONDEPLIST; 7160 LIST_REMOVE(newblk, nb_deps); 7161 } 7162 if (newblk->nb_state & ONWORKLIST) 7163 WORKLIST_REMOVE(&newblk->nb_list); 7164 /* 7165 * If the journal entry hasn't been written we save a pointer to 7166 * the dependency that frees it until it is written or the 7167 * superseding operation completes. 7168 */ 7169 jnewblk = newblk->nb_jnewblk; 7170 if (jnewblk != NULL && wk != NULL) { 7171 newblk->nb_jnewblk = NULL; 7172 jnewblk->jn_dep = wk; 7173 } 7174 if (!LIST_EMPTY(&newblk->nb_jwork)) 7175 jwork_move(wkhd, &newblk->nb_jwork); 7176 /* 7177 * When truncating we must free the newdirblk early to remove 7178 * the pagedep from the hash before returning. 7179 */ 7180 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7181 free_newdirblk(WK_NEWDIRBLK(wk)); 7182 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7183 panic("cancel_newblk: extra newdirblk"); 7184 7185 return (jnewblk); 7186 } 7187 7188 /* 7189 * Schedule the freefrag associated with a newblk to be released once 7190 * the pointers are written and the previous block is no longer needed. 7191 */ 7192 static void 7193 newblk_freefrag(newblk) 7194 struct newblk *newblk; 7195 { 7196 struct freefrag *freefrag; 7197 7198 if (newblk->nb_freefrag == NULL) 7199 return; 7200 freefrag = newblk->nb_freefrag; 7201 newblk->nb_freefrag = NULL; 7202 freefrag->ff_state |= COMPLETE; 7203 if ((freefrag->ff_state & ALLCOMPLETE) == ALLCOMPLETE) 7204 add_to_worklist(&freefrag->ff_list, 0); 7205 } 7206 7207 /* 7208 * Free a newblk. Generate a new freefrag work request if appropriate. 7209 * This must be called after the inode pointer and any direct block pointers 7210 * are valid or fully removed via truncate or frag extension. 7211 */ 7212 static void 7213 free_newblk(newblk) 7214 struct newblk *newblk; 7215 { 7216 struct indirdep *indirdep; 7217 struct worklist *wk; 7218 7219 KASSERT(newblk->nb_jnewblk == NULL, 7220 ("free_newblk; jnewblk %p still attached", newblk->nb_jnewblk)); 7221 mtx_assert(&lk, MA_OWNED); 7222 newblk_freefrag(newblk); 7223 if (newblk->nb_state & ONDEPLIST) 7224 LIST_REMOVE(newblk, nb_deps); 7225 if (newblk->nb_state & ONWORKLIST) 7226 WORKLIST_REMOVE(&newblk->nb_list); 7227 LIST_REMOVE(newblk, nb_hash); 7228 if ((wk = LIST_FIRST(&newblk->nb_newdirblk)) != NULL) 7229 free_newdirblk(WK_NEWDIRBLK(wk)); 7230 if (!LIST_EMPTY(&newblk->nb_newdirblk)) 7231 panic("free_newblk: extra newdirblk"); 7232 while ((indirdep = LIST_FIRST(&newblk->nb_indirdeps)) != NULL) 7233 indirdep_complete(indirdep); 7234 handle_jwork(&newblk->nb_jwork); 7235 newblk->nb_list.wk_type = D_NEWBLK; 7236 WORKITEM_FREE(newblk, D_NEWBLK); 7237 } 7238 7239 /* 7240 * Free a newdirblk. Clear the NEWBLOCK flag on its associated pagedep. 7241 * This routine must be called with splbio interrupts blocked. 7242 */ 7243 static void 7244 free_newdirblk(newdirblk) 7245 struct newdirblk *newdirblk; 7246 { 7247 struct pagedep *pagedep; 7248 struct diradd *dap; 7249 struct worklist *wk; 7250 7251 mtx_assert(&lk, MA_OWNED); 7252 WORKLIST_REMOVE(&newdirblk->db_list); 7253 /* 7254 * If the pagedep is still linked onto the directory buffer 7255 * dependency chain, then some of the entries on the 7256 * pd_pendinghd list may not be committed to disk yet. In 7257 * this case, we will simply clear the NEWBLOCK flag and 7258 * let the pd_pendinghd list be processed when the pagedep 7259 * is next written. If the pagedep is no longer on the buffer 7260 * dependency chain, then all the entries on the pd_pending 7261 * list are committed to disk and we can free them here. 7262 */ 7263 pagedep = newdirblk->db_pagedep; 7264 pagedep->pd_state &= ~NEWBLOCK; 7265 if ((pagedep->pd_state & ONWORKLIST) == 0) { 7266 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 7267 free_diradd(dap, NULL); 7268 /* 7269 * If no dependencies remain, the pagedep will be freed. 7270 */ 7271 free_pagedep(pagedep); 7272 } 7273 /* Should only ever be one item in the list. */ 7274 while ((wk = LIST_FIRST(&newdirblk->db_mkdir)) != NULL) { 7275 WORKLIST_REMOVE(wk); 7276 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 7277 } 7278 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 7279 } 7280 7281 /* 7282 * Prepare an inode to be freed. The actual free operation is not 7283 * done until the zero'ed inode has been written to disk. 7284 */ 7285 void 7286 softdep_freefile(pvp, ino, mode) 7287 struct vnode *pvp; 7288 ino_t ino; 7289 int mode; 7290 { 7291 struct inode *ip = VTOI(pvp); 7292 struct inodedep *inodedep; 7293 struct freefile *freefile; 7294 struct freeblks *freeblks; 7295 7296 /* 7297 * This sets up the inode de-allocation dependency. 7298 */ 7299 freefile = malloc(sizeof(struct freefile), 7300 M_FREEFILE, M_SOFTDEP_FLAGS); 7301 workitem_alloc(&freefile->fx_list, D_FREEFILE, pvp->v_mount); 7302 freefile->fx_mode = mode; 7303 freefile->fx_oldinum = ino; 7304 freefile->fx_devvp = ip->i_devvp; 7305 LIST_INIT(&freefile->fx_jwork); 7306 UFS_LOCK(ip->i_ump); 7307 ip->i_fs->fs_pendinginodes += 1; 7308 UFS_UNLOCK(ip->i_ump); 7309 7310 /* 7311 * If the inodedep does not exist, then the zero'ed inode has 7312 * been written to disk. If the allocated inode has never been 7313 * written to disk, then the on-disk inode is zero'ed. In either 7314 * case we can free the file immediately. If the journal was 7315 * canceled before being written the inode will never make it to 7316 * disk and we must send the canceled journal entrys to 7317 * ffs_freefile() to be cleared in conjunction with the bitmap. 7318 * Any blocks waiting on the inode to write can be safely freed 7319 * here as it will never been written. 7320 */ 7321 ACQUIRE_LOCK(&lk); 7322 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7323 if (inodedep) { 7324 /* 7325 * Clear out freeblks that no longer need to reference 7326 * this inode. 7327 */ 7328 while ((freeblks = 7329 TAILQ_FIRST(&inodedep->id_freeblklst)) != NULL) { 7330 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, 7331 fb_next); 7332 freeblks->fb_state &= ~ONDEPLIST; 7333 } 7334 /* 7335 * Remove this inode from the unlinked list. 7336 */ 7337 if (inodedep->id_state & UNLINKED) { 7338 /* 7339 * Save the journal work to be freed with the bitmap 7340 * before we clear UNLINKED. Otherwise it can be lost 7341 * if the inode block is written. 7342 */ 7343 handle_bufwait(inodedep, &freefile->fx_jwork); 7344 clear_unlinked_inodedep(inodedep); 7345 /* Re-acquire inodedep as we've dropped lk. */ 7346 inodedep_lookup(pvp->v_mount, ino, 0, &inodedep); 7347 } 7348 } 7349 if (inodedep == NULL || check_inode_unwritten(inodedep)) { 7350 FREE_LOCK(&lk); 7351 handle_workitem_freefile(freefile); 7352 return; 7353 } 7354 if ((inodedep->id_state & DEPCOMPLETE) == 0) 7355 inodedep->id_state |= GOINGAWAY; 7356 WORKLIST_INSERT(&inodedep->id_inowait, &freefile->fx_list); 7357 FREE_LOCK(&lk); 7358 if (ip->i_number == ino) 7359 ip->i_flag |= IN_MODIFIED; 7360 } 7361 7362 /* 7363 * Check to see if an inode has never been written to disk. If 7364 * so free the inodedep and return success, otherwise return failure. 7365 * This routine must be called with splbio interrupts blocked. 7366 * 7367 * If we still have a bitmap dependency, then the inode has never 7368 * been written to disk. Drop the dependency as it is no longer 7369 * necessary since the inode is being deallocated. We set the 7370 * ALLCOMPLETE flags since the bitmap now properly shows that the 7371 * inode is not allocated. Even if the inode is actively being 7372 * written, it has been rolled back to its zero'ed state, so we 7373 * are ensured that a zero inode is what is on the disk. For short 7374 * lived files, this change will usually result in removing all the 7375 * dependencies from the inode so that it can be freed immediately. 7376 */ 7377 static int 7378 check_inode_unwritten(inodedep) 7379 struct inodedep *inodedep; 7380 { 7381 7382 mtx_assert(&lk, MA_OWNED); 7383 7384 if ((inodedep->id_state & (DEPCOMPLETE | UNLINKED)) != 0 || 7385 !LIST_EMPTY(&inodedep->id_dirremhd) || 7386 !LIST_EMPTY(&inodedep->id_pendinghd) || 7387 !LIST_EMPTY(&inodedep->id_bufwait) || 7388 !LIST_EMPTY(&inodedep->id_inowait) || 7389 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7390 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7391 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7392 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7393 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7394 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7395 inodedep->id_mkdiradd != NULL || 7396 inodedep->id_nlinkdelta != 0) 7397 return (0); 7398 /* 7399 * Another process might be in initiate_write_inodeblock_ufs[12] 7400 * trying to allocate memory without holding "Softdep Lock". 7401 */ 7402 if ((inodedep->id_state & IOSTARTED) != 0 && 7403 inodedep->id_savedino1 == NULL) 7404 return (0); 7405 7406 if (inodedep->id_state & ONDEPLIST) 7407 LIST_REMOVE(inodedep, id_deps); 7408 inodedep->id_state &= ~ONDEPLIST; 7409 inodedep->id_state |= ALLCOMPLETE; 7410 inodedep->id_bmsafemap = NULL; 7411 if (inodedep->id_state & ONWORKLIST) 7412 WORKLIST_REMOVE(&inodedep->id_list); 7413 if (inodedep->id_savedino1 != NULL) { 7414 free(inodedep->id_savedino1, M_SAVEDINO); 7415 inodedep->id_savedino1 = NULL; 7416 } 7417 if (free_inodedep(inodedep) == 0) 7418 panic("check_inode_unwritten: busy inode"); 7419 return (1); 7420 } 7421 7422 /* 7423 * Try to free an inodedep structure. Return 1 if it could be freed. 7424 */ 7425 static int 7426 free_inodedep(inodedep) 7427 struct inodedep *inodedep; 7428 { 7429 7430 mtx_assert(&lk, MA_OWNED); 7431 if ((inodedep->id_state & (ONWORKLIST | UNLINKED)) != 0 || 7432 (inodedep->id_state & ALLCOMPLETE) != ALLCOMPLETE || 7433 !LIST_EMPTY(&inodedep->id_dirremhd) || 7434 !LIST_EMPTY(&inodedep->id_pendinghd) || 7435 !LIST_EMPTY(&inodedep->id_bufwait) || 7436 !LIST_EMPTY(&inodedep->id_inowait) || 7437 !TAILQ_EMPTY(&inodedep->id_inoreflst) || 7438 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 7439 !TAILQ_EMPTY(&inodedep->id_newinoupdt) || 7440 !TAILQ_EMPTY(&inodedep->id_extupdt) || 7441 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 7442 !TAILQ_EMPTY(&inodedep->id_freeblklst) || 7443 inodedep->id_mkdiradd != NULL || 7444 inodedep->id_nlinkdelta != 0 || 7445 inodedep->id_savedino1 != NULL) 7446 return (0); 7447 if (inodedep->id_state & ONDEPLIST) 7448 LIST_REMOVE(inodedep, id_deps); 7449 LIST_REMOVE(inodedep, id_hash); 7450 WORKITEM_FREE(inodedep, D_INODEDEP); 7451 return (1); 7452 } 7453 7454 /* 7455 * Free the block referenced by a freework structure. The parent freeblks 7456 * structure is released and completed when the final cg bitmap reaches 7457 * the disk. This routine may be freeing a jnewblk which never made it to 7458 * disk in which case we do not have to wait as the operation is undone 7459 * in memory immediately. 7460 */ 7461 static void 7462 freework_freeblock(freework) 7463 struct freework *freework; 7464 { 7465 struct freeblks *freeblks; 7466 struct jnewblk *jnewblk; 7467 struct ufsmount *ump; 7468 struct workhead wkhd; 7469 struct fs *fs; 7470 int bsize; 7471 int needj; 7472 7473 mtx_assert(&lk, MA_OWNED); 7474 /* 7475 * Handle partial truncate separately. 7476 */ 7477 if (freework->fw_indir) { 7478 complete_trunc_indir(freework); 7479 return; 7480 } 7481 freeblks = freework->fw_freeblks; 7482 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7483 fs = ump->um_fs; 7484 needj = MOUNTEDSUJ(freeblks->fb_list.wk_mp) != 0; 7485 bsize = lfragtosize(fs, freework->fw_frags); 7486 LIST_INIT(&wkhd); 7487 /* 7488 * DEPCOMPLETE is cleared in indirblk_insert() if the block lives 7489 * on the indirblk hashtable and prevents premature freeing. 7490 */ 7491 freework->fw_state |= DEPCOMPLETE; 7492 /* 7493 * SUJ needs to wait for the segment referencing freed indirect 7494 * blocks to expire so that we know the checker will not confuse 7495 * a re-allocated indirect block with its old contents. 7496 */ 7497 if (needj && freework->fw_lbn <= -NDADDR) 7498 indirblk_insert(freework); 7499 /* 7500 * If we are canceling an existing jnewblk pass it to the free 7501 * routine, otherwise pass the freeblk which will ultimately 7502 * release the freeblks. If we're not journaling, we can just 7503 * free the freeblks immediately. 7504 */ 7505 jnewblk = freework->fw_jnewblk; 7506 if (jnewblk != NULL) { 7507 cancel_jnewblk(jnewblk, &wkhd); 7508 needj = 0; 7509 } else if (needj) { 7510 freework->fw_state |= DELAYEDFREE; 7511 freeblks->fb_cgwait++; 7512 WORKLIST_INSERT(&wkhd, &freework->fw_list); 7513 } 7514 FREE_LOCK(&lk); 7515 freeblks_free(ump, freeblks, btodb(bsize)); 7516 CTR4(KTR_SUJ, 7517 "freework_freeblock: ino %d blkno %jd lbn %jd size %ld", 7518 freeblks->fb_inum, freework->fw_blkno, freework->fw_lbn, bsize); 7519 ffs_blkfree(ump, fs, freeblks->fb_devvp, freework->fw_blkno, bsize, 7520 freeblks->fb_inum, freeblks->fb_vtype, &wkhd); 7521 ACQUIRE_LOCK(&lk); 7522 /* 7523 * The jnewblk will be discarded and the bits in the map never 7524 * made it to disk. We can immediately free the freeblk. 7525 */ 7526 if (needj == 0) 7527 handle_written_freework(freework); 7528 } 7529 7530 /* 7531 * We enqueue freework items that need processing back on the freeblks and 7532 * add the freeblks to the worklist. This makes it easier to find all work 7533 * required to flush a truncation in process_truncates(). 7534 */ 7535 static void 7536 freework_enqueue(freework) 7537 struct freework *freework; 7538 { 7539 struct freeblks *freeblks; 7540 7541 freeblks = freework->fw_freeblks; 7542 if ((freework->fw_state & INPROGRESS) == 0) 7543 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &freework->fw_list); 7544 if ((freeblks->fb_state & 7545 (ONWORKLIST | INPROGRESS | ALLCOMPLETE)) == ALLCOMPLETE && 7546 LIST_EMPTY(&freeblks->fb_jblkdephd)) 7547 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7548 } 7549 7550 /* 7551 * Start, continue, or finish the process of freeing an indirect block tree. 7552 * The free operation may be paused at any point with fw_off containing the 7553 * offset to restart from. This enables us to implement some flow control 7554 * for large truncates which may fan out and generate a huge number of 7555 * dependencies. 7556 */ 7557 static void 7558 handle_workitem_indirblk(freework) 7559 struct freework *freework; 7560 { 7561 struct freeblks *freeblks; 7562 struct ufsmount *ump; 7563 struct fs *fs; 7564 7565 freeblks = freework->fw_freeblks; 7566 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7567 fs = ump->um_fs; 7568 if (freework->fw_state & DEPCOMPLETE) { 7569 handle_written_freework(freework); 7570 return; 7571 } 7572 if (freework->fw_off == NINDIR(fs)) { 7573 freework_freeblock(freework); 7574 return; 7575 } 7576 freework->fw_state |= INPROGRESS; 7577 FREE_LOCK(&lk); 7578 indir_trunc(freework, fsbtodb(fs, freework->fw_blkno), 7579 freework->fw_lbn); 7580 ACQUIRE_LOCK(&lk); 7581 } 7582 7583 /* 7584 * Called when a freework structure attached to a cg buf is written. The 7585 * ref on either the parent or the freeblks structure is released and 7586 * the freeblks is added back to the worklist if there is more work to do. 7587 */ 7588 static void 7589 handle_written_freework(freework) 7590 struct freework *freework; 7591 { 7592 struct freeblks *freeblks; 7593 struct freework *parent; 7594 7595 freeblks = freework->fw_freeblks; 7596 parent = freework->fw_parent; 7597 if (freework->fw_state & DELAYEDFREE) 7598 freeblks->fb_cgwait--; 7599 freework->fw_state |= COMPLETE; 7600 if ((freework->fw_state & ALLCOMPLETE) == ALLCOMPLETE) 7601 WORKITEM_FREE(freework, D_FREEWORK); 7602 if (parent) { 7603 if (--parent->fw_ref == 0) 7604 freework_enqueue(parent); 7605 return; 7606 } 7607 if (--freeblks->fb_ref != 0) 7608 return; 7609 if ((freeblks->fb_state & (ALLCOMPLETE | ONWORKLIST | INPROGRESS)) == 7610 ALLCOMPLETE && LIST_EMPTY(&freeblks->fb_jblkdephd)) 7611 add_to_worklist(&freeblks->fb_list, WK_NODELAY); 7612 } 7613 7614 /* 7615 * This workitem routine performs the block de-allocation. 7616 * The workitem is added to the pending list after the updated 7617 * inode block has been written to disk. As mentioned above, 7618 * checks regarding the number of blocks de-allocated (compared 7619 * to the number of blocks allocated for the file) are also 7620 * performed in this function. 7621 */ 7622 static int 7623 handle_workitem_freeblocks(freeblks, flags) 7624 struct freeblks *freeblks; 7625 int flags; 7626 { 7627 struct freework *freework; 7628 struct newblk *newblk; 7629 struct allocindir *aip; 7630 struct ufsmount *ump; 7631 struct worklist *wk; 7632 7633 KASSERT(LIST_EMPTY(&freeblks->fb_jblkdephd), 7634 ("handle_workitem_freeblocks: Journal entries not written.")); 7635 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7636 ACQUIRE_LOCK(&lk); 7637 while ((wk = LIST_FIRST(&freeblks->fb_freeworkhd)) != NULL) { 7638 WORKLIST_REMOVE(wk); 7639 switch (wk->wk_type) { 7640 case D_DIRREM: 7641 wk->wk_state |= COMPLETE; 7642 add_to_worklist(wk, 0); 7643 continue; 7644 7645 case D_ALLOCDIRECT: 7646 free_newblk(WK_NEWBLK(wk)); 7647 continue; 7648 7649 case D_ALLOCINDIR: 7650 aip = WK_ALLOCINDIR(wk); 7651 freework = NULL; 7652 if (aip->ai_state & DELAYEDFREE) { 7653 FREE_LOCK(&lk); 7654 freework = newfreework(ump, freeblks, NULL, 7655 aip->ai_lbn, aip->ai_newblkno, 7656 ump->um_fs->fs_frag, 0, 0); 7657 ACQUIRE_LOCK(&lk); 7658 } 7659 newblk = WK_NEWBLK(wk); 7660 if (newblk->nb_jnewblk) { 7661 freework->fw_jnewblk = newblk->nb_jnewblk; 7662 newblk->nb_jnewblk->jn_dep = &freework->fw_list; 7663 newblk->nb_jnewblk = NULL; 7664 } 7665 free_newblk(newblk); 7666 continue; 7667 7668 case D_FREEWORK: 7669 freework = WK_FREEWORK(wk); 7670 if (freework->fw_lbn <= -NDADDR) 7671 handle_workitem_indirblk(freework); 7672 else 7673 freework_freeblock(freework); 7674 continue; 7675 default: 7676 panic("handle_workitem_freeblocks: Unknown type %s", 7677 TYPENAME(wk->wk_type)); 7678 } 7679 } 7680 if (freeblks->fb_ref != 0) { 7681 freeblks->fb_state &= ~INPROGRESS; 7682 wake_worklist(&freeblks->fb_list); 7683 freeblks = NULL; 7684 } 7685 FREE_LOCK(&lk); 7686 if (freeblks) 7687 return handle_complete_freeblocks(freeblks, flags); 7688 return (0); 7689 } 7690 7691 /* 7692 * Handle completion of block free via truncate. This allows fs_pending 7693 * to track the actual free block count more closely than if we only updated 7694 * it at the end. We must be careful to handle cases where the block count 7695 * on free was incorrect. 7696 */ 7697 static void 7698 freeblks_free(ump, freeblks, blocks) 7699 struct ufsmount *ump; 7700 struct freeblks *freeblks; 7701 int blocks; 7702 { 7703 struct fs *fs; 7704 ufs2_daddr_t remain; 7705 7706 UFS_LOCK(ump); 7707 remain = -freeblks->fb_chkcnt; 7708 freeblks->fb_chkcnt += blocks; 7709 if (remain > 0) { 7710 if (remain < blocks) 7711 blocks = remain; 7712 fs = ump->um_fs; 7713 fs->fs_pendingblocks -= blocks; 7714 } 7715 UFS_UNLOCK(ump); 7716 } 7717 7718 /* 7719 * Once all of the freework workitems are complete we can retire the 7720 * freeblocks dependency and any journal work awaiting completion. This 7721 * can not be called until all other dependencies are stable on disk. 7722 */ 7723 static int 7724 handle_complete_freeblocks(freeblks, flags) 7725 struct freeblks *freeblks; 7726 int flags; 7727 { 7728 struct inodedep *inodedep; 7729 struct inode *ip; 7730 struct vnode *vp; 7731 struct fs *fs; 7732 struct ufsmount *ump; 7733 ufs2_daddr_t spare; 7734 7735 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7736 fs = ump->um_fs; 7737 flags = LK_EXCLUSIVE | flags; 7738 spare = freeblks->fb_chkcnt; 7739 7740 /* 7741 * If we did not release the expected number of blocks we may have 7742 * to adjust the inode block count here. Only do so if it wasn't 7743 * a truncation to zero and the modrev still matches. 7744 */ 7745 if (spare && freeblks->fb_len != 0) { 7746 if (ffs_vgetf(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7747 flags, &vp, FFSV_FORCEINSMQ) != 0) 7748 return (EBUSY); 7749 ip = VTOI(vp); 7750 if (DIP(ip, i_modrev) == freeblks->fb_modrev) { 7751 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) - spare); 7752 ip->i_flag |= IN_CHANGE; 7753 /* 7754 * We must wait so this happens before the 7755 * journal is reclaimed. 7756 */ 7757 ffs_update(vp, 1); 7758 } 7759 vput(vp); 7760 } 7761 if (spare < 0) { 7762 UFS_LOCK(ump); 7763 fs->fs_pendingblocks += spare; 7764 UFS_UNLOCK(ump); 7765 } 7766 #ifdef QUOTA 7767 /* Handle spare. */ 7768 if (spare) 7769 quotaadj(freeblks->fb_quota, ump, -spare); 7770 quotarele(freeblks->fb_quota); 7771 #endif 7772 ACQUIRE_LOCK(&lk); 7773 if (freeblks->fb_state & ONDEPLIST) { 7774 inodedep_lookup(freeblks->fb_list.wk_mp, freeblks->fb_inum, 7775 0, &inodedep); 7776 TAILQ_REMOVE(&inodedep->id_freeblklst, freeblks, fb_next); 7777 freeblks->fb_state &= ~ONDEPLIST; 7778 if (TAILQ_EMPTY(&inodedep->id_freeblklst)) 7779 free_inodedep(inodedep); 7780 } 7781 /* 7782 * All of the freeblock deps must be complete prior to this call 7783 * so it's now safe to complete earlier outstanding journal entries. 7784 */ 7785 handle_jwork(&freeblks->fb_jwork); 7786 WORKITEM_FREE(freeblks, D_FREEBLKS); 7787 FREE_LOCK(&lk); 7788 return (0); 7789 } 7790 7791 /* 7792 * Release blocks associated with the freeblks and stored in the indirect 7793 * block dbn. If level is greater than SINGLE, the block is an indirect block 7794 * and recursive calls to indirtrunc must be used to cleanse other indirect 7795 * blocks. 7796 * 7797 * This handles partial and complete truncation of blocks. Partial is noted 7798 * with goingaway == 0. In this case the freework is completed after the 7799 * zero'd indirects are written to disk. For full truncation the freework 7800 * is completed after the block is freed. 7801 */ 7802 static void 7803 indir_trunc(freework, dbn, lbn) 7804 struct freework *freework; 7805 ufs2_daddr_t dbn; 7806 ufs_lbn_t lbn; 7807 { 7808 struct freework *nfreework; 7809 struct workhead wkhd; 7810 struct freeblks *freeblks; 7811 struct buf *bp; 7812 struct fs *fs; 7813 struct indirdep *indirdep; 7814 struct ufsmount *ump; 7815 ufs1_daddr_t *bap1 = 0; 7816 ufs2_daddr_t nb, nnb, *bap2 = 0; 7817 ufs_lbn_t lbnadd, nlbn; 7818 int i, nblocks, ufs1fmt; 7819 int freedblocks; 7820 int goingaway; 7821 int freedeps; 7822 int needj; 7823 int level; 7824 int cnt; 7825 7826 freeblks = freework->fw_freeblks; 7827 ump = VFSTOUFS(freeblks->fb_list.wk_mp); 7828 fs = ump->um_fs; 7829 /* 7830 * Get buffer of block pointers to be freed. There are three cases: 7831 * 7832 * 1) Partial truncate caches the indirdep pointer in the freework 7833 * which provides us a back copy to the save bp which holds the 7834 * pointers we want to clear. When this completes the zero 7835 * pointers are written to the real copy. 7836 * 2) The indirect is being completely truncated, cancel_indirdep() 7837 * eliminated the real copy and placed the indirdep on the saved 7838 * copy. The indirdep and buf are discarded when this completes. 7839 * 3) The indirect was not in memory, we read a copy off of the disk 7840 * using the devvp and drop and invalidate the buffer when we're 7841 * done. 7842 */ 7843 goingaway = 1; 7844 indirdep = NULL; 7845 if (freework->fw_indir != NULL) { 7846 goingaway = 0; 7847 indirdep = freework->fw_indir; 7848 bp = indirdep->ir_savebp; 7849 if (bp == NULL || bp->b_blkno != dbn) 7850 panic("indir_trunc: Bad saved buf %p blkno %jd", 7851 bp, (intmax_t)dbn); 7852 } else if ((bp = incore(&freeblks->fb_devvp->v_bufobj, dbn)) != NULL) { 7853 /* 7854 * The lock prevents the buf dep list from changing and 7855 * indirects on devvp should only ever have one dependency. 7856 */ 7857 indirdep = WK_INDIRDEP(LIST_FIRST(&bp->b_dep)); 7858 if (indirdep == NULL || (indirdep->ir_state & GOINGAWAY) == 0) 7859 panic("indir_trunc: Bad indirdep %p from buf %p", 7860 indirdep, bp); 7861 } else if (bread(freeblks->fb_devvp, dbn, (int)fs->fs_bsize, 7862 NOCRED, &bp) != 0) { 7863 brelse(bp); 7864 return; 7865 } 7866 ACQUIRE_LOCK(&lk); 7867 /* Protects against a race with complete_trunc_indir(). */ 7868 freework->fw_state &= ~INPROGRESS; 7869 /* 7870 * If we have an indirdep we need to enforce the truncation order 7871 * and discard it when it is complete. 7872 */ 7873 if (indirdep) { 7874 if (freework != TAILQ_FIRST(&indirdep->ir_trunc) && 7875 !TAILQ_EMPTY(&indirdep->ir_trunc)) { 7876 /* 7877 * Add the complete truncate to the list on the 7878 * indirdep to enforce in-order processing. 7879 */ 7880 if (freework->fw_indir == NULL) 7881 TAILQ_INSERT_TAIL(&indirdep->ir_trunc, 7882 freework, fw_next); 7883 FREE_LOCK(&lk); 7884 return; 7885 } 7886 /* 7887 * If we're goingaway, free the indirdep. Otherwise it will 7888 * linger until the write completes. 7889 */ 7890 if (goingaway) { 7891 free_indirdep(indirdep); 7892 ump->um_numindirdeps -= 1; 7893 } 7894 } 7895 FREE_LOCK(&lk); 7896 /* Initialize pointers depending on block size. */ 7897 if (ump->um_fstype == UFS1) { 7898 bap1 = (ufs1_daddr_t *)bp->b_data; 7899 nb = bap1[freework->fw_off]; 7900 ufs1fmt = 1; 7901 } else { 7902 bap2 = (ufs2_daddr_t *)bp->b_data; 7903 nb = bap2[freework->fw_off]; 7904 ufs1fmt = 0; 7905 } 7906 level = lbn_level(lbn); 7907 needj = MOUNTEDSUJ(UFSTOVFS(ump)) != 0; 7908 lbnadd = lbn_offset(fs, level); 7909 nblocks = btodb(fs->fs_bsize); 7910 nfreework = freework; 7911 freedeps = 0; 7912 cnt = 0; 7913 /* 7914 * Reclaim blocks. Traverses into nested indirect levels and 7915 * arranges for the current level to be freed when subordinates 7916 * are free when journaling. 7917 */ 7918 for (i = freework->fw_off; i < NINDIR(fs); i++, nb = nnb) { 7919 if (i != NINDIR(fs) - 1) { 7920 if (ufs1fmt) 7921 nnb = bap1[i+1]; 7922 else 7923 nnb = bap2[i+1]; 7924 } else 7925 nnb = 0; 7926 if (nb == 0) 7927 continue; 7928 cnt++; 7929 if (level != 0) { 7930 nlbn = (lbn + 1) - (i * lbnadd); 7931 if (needj != 0) { 7932 nfreework = newfreework(ump, freeblks, freework, 7933 nlbn, nb, fs->fs_frag, 0, 0); 7934 freedeps++; 7935 } 7936 indir_trunc(nfreework, fsbtodb(fs, nb), nlbn); 7937 } else { 7938 struct freedep *freedep; 7939 7940 /* 7941 * Attempt to aggregate freedep dependencies for 7942 * all blocks being released to the same CG. 7943 */ 7944 LIST_INIT(&wkhd); 7945 if (needj != 0 && 7946 (nnb == 0 || (dtog(fs, nb) != dtog(fs, nnb)))) { 7947 freedep = newfreedep(freework); 7948 WORKLIST_INSERT_UNLOCKED(&wkhd, 7949 &freedep->fd_list); 7950 freedeps++; 7951 } 7952 CTR3(KTR_SUJ, 7953 "indir_trunc: ino %d blkno %jd size %ld", 7954 freeblks->fb_inum, nb, fs->fs_bsize); 7955 ffs_blkfree(ump, fs, freeblks->fb_devvp, nb, 7956 fs->fs_bsize, freeblks->fb_inum, 7957 freeblks->fb_vtype, &wkhd); 7958 } 7959 } 7960 if (goingaway) { 7961 bp->b_flags |= B_INVAL | B_NOCACHE; 7962 brelse(bp); 7963 } 7964 freedblocks = 0; 7965 if (level == 0) 7966 freedblocks = (nblocks * cnt); 7967 if (needj == 0) 7968 freedblocks += nblocks; 7969 freeblks_free(ump, freeblks, freedblocks); 7970 /* 7971 * If we are journaling set up the ref counts and offset so this 7972 * indirect can be completed when its children are free. 7973 */ 7974 if (needj) { 7975 ACQUIRE_LOCK(&lk); 7976 freework->fw_off = i; 7977 freework->fw_ref += freedeps; 7978 freework->fw_ref -= NINDIR(fs) + 1; 7979 if (level == 0) 7980 freeblks->fb_cgwait += freedeps; 7981 if (freework->fw_ref == 0) 7982 freework_freeblock(freework); 7983 FREE_LOCK(&lk); 7984 return; 7985 } 7986 /* 7987 * If we're not journaling we can free the indirect now. 7988 */ 7989 dbn = dbtofsb(fs, dbn); 7990 CTR3(KTR_SUJ, 7991 "indir_trunc 2: ino %d blkno %jd size %ld", 7992 freeblks->fb_inum, dbn, fs->fs_bsize); 7993 ffs_blkfree(ump, fs, freeblks->fb_devvp, dbn, fs->fs_bsize, 7994 freeblks->fb_inum, freeblks->fb_vtype, NULL); 7995 /* Non SUJ softdep does single-threaded truncations. */ 7996 if (freework->fw_blkno == dbn) { 7997 freework->fw_state |= ALLCOMPLETE; 7998 ACQUIRE_LOCK(&lk); 7999 handle_written_freework(freework); 8000 FREE_LOCK(&lk); 8001 } 8002 return; 8003 } 8004 8005 /* 8006 * Cancel an allocindir when it is removed via truncation. When bp is not 8007 * NULL the indirect never appeared on disk and is scheduled to be freed 8008 * independently of the indir so we can more easily track journal work. 8009 */ 8010 static void 8011 cancel_allocindir(aip, bp, freeblks, trunc) 8012 struct allocindir *aip; 8013 struct buf *bp; 8014 struct freeblks *freeblks; 8015 int trunc; 8016 { 8017 struct indirdep *indirdep; 8018 struct freefrag *freefrag; 8019 struct newblk *newblk; 8020 8021 newblk = (struct newblk *)aip; 8022 LIST_REMOVE(aip, ai_next); 8023 /* 8024 * We must eliminate the pointer in bp if it must be freed on its 8025 * own due to partial truncate or pending journal work. 8026 */ 8027 if (bp && (trunc || newblk->nb_jnewblk)) { 8028 /* 8029 * Clear the pointer and mark the aip to be freed 8030 * directly if it never existed on disk. 8031 */ 8032 aip->ai_state |= DELAYEDFREE; 8033 indirdep = aip->ai_indirdep; 8034 if (indirdep->ir_state & UFS1FMT) 8035 ((ufs1_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8036 else 8037 ((ufs2_daddr_t *)bp->b_data)[aip->ai_offset] = 0; 8038 } 8039 /* 8040 * When truncating the previous pointer will be freed via 8041 * savedbp. Eliminate the freefrag which would dup free. 8042 */ 8043 if (trunc && (freefrag = newblk->nb_freefrag) != NULL) { 8044 newblk->nb_freefrag = NULL; 8045 if (freefrag->ff_jdep) 8046 cancel_jfreefrag( 8047 WK_JFREEFRAG(freefrag->ff_jdep)); 8048 jwork_move(&freeblks->fb_jwork, &freefrag->ff_jwork); 8049 WORKITEM_FREE(freefrag, D_FREEFRAG); 8050 } 8051 /* 8052 * If the journal hasn't been written the jnewblk must be passed 8053 * to the call to ffs_blkfree that reclaims the space. We accomplish 8054 * this by leaving the journal dependency on the newblk to be freed 8055 * when a freework is created in handle_workitem_freeblocks(). 8056 */ 8057 cancel_newblk(newblk, NULL, &freeblks->fb_jwork); 8058 WORKLIST_INSERT(&freeblks->fb_freeworkhd, &newblk->nb_list); 8059 } 8060 8061 /* 8062 * Create the mkdir dependencies for . and .. in a new directory. Link them 8063 * in to a newdirblk so any subsequent additions are tracked properly. The 8064 * caller is responsible for adding the mkdir1 dependency to the journal 8065 * and updating id_mkdiradd. This function returns with lk held. 8066 */ 8067 static struct mkdir * 8068 setup_newdir(dap, newinum, dinum, newdirbp, mkdirp) 8069 struct diradd *dap; 8070 ino_t newinum; 8071 ino_t dinum; 8072 struct buf *newdirbp; 8073 struct mkdir **mkdirp; 8074 { 8075 struct newblk *newblk; 8076 struct pagedep *pagedep; 8077 struct inodedep *inodedep; 8078 struct newdirblk *newdirblk = 0; 8079 struct mkdir *mkdir1, *mkdir2; 8080 struct worklist *wk; 8081 struct jaddref *jaddref; 8082 struct mount *mp; 8083 8084 mp = dap->da_list.wk_mp; 8085 newdirblk = malloc(sizeof(struct newdirblk), M_NEWDIRBLK, 8086 M_SOFTDEP_FLAGS); 8087 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8088 LIST_INIT(&newdirblk->db_mkdir); 8089 mkdir1 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8090 workitem_alloc(&mkdir1->md_list, D_MKDIR, mp); 8091 mkdir1->md_state = ATTACHED | MKDIR_BODY; 8092 mkdir1->md_diradd = dap; 8093 mkdir1->md_jaddref = NULL; 8094 mkdir2 = malloc(sizeof(struct mkdir), M_MKDIR, M_SOFTDEP_FLAGS); 8095 workitem_alloc(&mkdir2->md_list, D_MKDIR, mp); 8096 mkdir2->md_state = ATTACHED | MKDIR_PARENT; 8097 mkdir2->md_diradd = dap; 8098 mkdir2->md_jaddref = NULL; 8099 if (MOUNTEDSUJ(mp) == 0) { 8100 mkdir1->md_state |= DEPCOMPLETE; 8101 mkdir2->md_state |= DEPCOMPLETE; 8102 } 8103 /* 8104 * Dependency on "." and ".." being written to disk. 8105 */ 8106 mkdir1->md_buf = newdirbp; 8107 ACQUIRE_LOCK(&lk); 8108 LIST_INSERT_HEAD(&mkdirlisthd, mkdir1, md_mkdirs); 8109 /* 8110 * We must link the pagedep, allocdirect, and newdirblk for 8111 * the initial file page so the pointer to the new directory 8112 * is not written until the directory contents are live and 8113 * any subsequent additions are not marked live until the 8114 * block is reachable via the inode. 8115 */ 8116 if (pagedep_lookup(mp, newdirbp, newinum, 0, 0, &pagedep) == 0) 8117 panic("setup_newdir: lost pagedep"); 8118 LIST_FOREACH(wk, &newdirbp->b_dep, wk_list) 8119 if (wk->wk_type == D_ALLOCDIRECT) 8120 break; 8121 if (wk == NULL) 8122 panic("setup_newdir: lost allocdirect"); 8123 if (pagedep->pd_state & NEWBLOCK) 8124 panic("setup_newdir: NEWBLOCK already set"); 8125 newblk = WK_NEWBLK(wk); 8126 pagedep->pd_state |= NEWBLOCK; 8127 pagedep->pd_newdirblk = newdirblk; 8128 newdirblk->db_pagedep = pagedep; 8129 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8130 WORKLIST_INSERT(&newdirblk->db_mkdir, &mkdir1->md_list); 8131 /* 8132 * Look up the inodedep for the parent directory so that we 8133 * can link mkdir2 into the pending dotdot jaddref or 8134 * the inode write if there is none. If the inode is 8135 * ALLCOMPLETE and no jaddref is present all dependencies have 8136 * been satisfied and mkdir2 can be freed. 8137 */ 8138 inodedep_lookup(mp, dinum, 0, &inodedep); 8139 if (MOUNTEDSUJ(mp)) { 8140 if (inodedep == NULL) 8141 panic("setup_newdir: Lost parent."); 8142 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8143 inoreflst); 8144 KASSERT(jaddref != NULL && jaddref->ja_parent == newinum && 8145 (jaddref->ja_state & MKDIR_PARENT), 8146 ("setup_newdir: bad dotdot jaddref %p", jaddref)); 8147 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 8148 mkdir2->md_jaddref = jaddref; 8149 jaddref->ja_mkdir = mkdir2; 8150 } else if (inodedep == NULL || 8151 (inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 8152 dap->da_state &= ~MKDIR_PARENT; 8153 WORKITEM_FREE(mkdir2, D_MKDIR); 8154 } else { 8155 LIST_INSERT_HEAD(&mkdirlisthd, mkdir2, md_mkdirs); 8156 WORKLIST_INSERT(&inodedep->id_bufwait, &mkdir2->md_list); 8157 } 8158 *mkdirp = mkdir2; 8159 8160 return (mkdir1); 8161 } 8162 8163 /* 8164 * Directory entry addition dependencies. 8165 * 8166 * When adding a new directory entry, the inode (with its incremented link 8167 * count) must be written to disk before the directory entry's pointer to it. 8168 * Also, if the inode is newly allocated, the corresponding freemap must be 8169 * updated (on disk) before the directory entry's pointer. These requirements 8170 * are met via undo/redo on the directory entry's pointer, which consists 8171 * simply of the inode number. 8172 * 8173 * As directory entries are added and deleted, the free space within a 8174 * directory block can become fragmented. The ufs filesystem will compact 8175 * a fragmented directory block to make space for a new entry. When this 8176 * occurs, the offsets of previously added entries change. Any "diradd" 8177 * dependency structures corresponding to these entries must be updated with 8178 * the new offsets. 8179 */ 8180 8181 /* 8182 * This routine is called after the in-memory inode's link 8183 * count has been incremented, but before the directory entry's 8184 * pointer to the inode has been set. 8185 */ 8186 int 8187 softdep_setup_directory_add(bp, dp, diroffset, newinum, newdirbp, isnewblk) 8188 struct buf *bp; /* buffer containing directory block */ 8189 struct inode *dp; /* inode for directory */ 8190 off_t diroffset; /* offset of new entry in directory */ 8191 ino_t newinum; /* inode referenced by new directory entry */ 8192 struct buf *newdirbp; /* non-NULL => contents of new mkdir */ 8193 int isnewblk; /* entry is in a newly allocated block */ 8194 { 8195 int offset; /* offset of new entry within directory block */ 8196 ufs_lbn_t lbn; /* block in directory containing new entry */ 8197 struct fs *fs; 8198 struct diradd *dap; 8199 struct newblk *newblk; 8200 struct pagedep *pagedep; 8201 struct inodedep *inodedep; 8202 struct newdirblk *newdirblk = 0; 8203 struct mkdir *mkdir1, *mkdir2; 8204 struct jaddref *jaddref; 8205 struct mount *mp; 8206 int isindir; 8207 8208 /* 8209 * Whiteouts have no dependencies. 8210 */ 8211 if (newinum == WINO) { 8212 if (newdirbp != NULL) 8213 bdwrite(newdirbp); 8214 return (0); 8215 } 8216 jaddref = NULL; 8217 mkdir1 = mkdir2 = NULL; 8218 mp = UFSTOVFS(dp->i_ump); 8219 fs = dp->i_fs; 8220 lbn = lblkno(fs, diroffset); 8221 offset = blkoff(fs, diroffset); 8222 dap = malloc(sizeof(struct diradd), M_DIRADD, 8223 M_SOFTDEP_FLAGS|M_ZERO); 8224 workitem_alloc(&dap->da_list, D_DIRADD, mp); 8225 dap->da_offset = offset; 8226 dap->da_newinum = newinum; 8227 dap->da_state = ATTACHED; 8228 LIST_INIT(&dap->da_jwork); 8229 isindir = bp->b_lblkno >= NDADDR; 8230 if (isnewblk && 8231 (isindir ? blkoff(fs, diroffset) : fragoff(fs, diroffset)) == 0) { 8232 newdirblk = malloc(sizeof(struct newdirblk), 8233 M_NEWDIRBLK, M_SOFTDEP_FLAGS); 8234 workitem_alloc(&newdirblk->db_list, D_NEWDIRBLK, mp); 8235 LIST_INIT(&newdirblk->db_mkdir); 8236 } 8237 /* 8238 * If we're creating a new directory setup the dependencies and set 8239 * the dap state to wait for them. Otherwise it's COMPLETE and 8240 * we can move on. 8241 */ 8242 if (newdirbp == NULL) { 8243 dap->da_state |= DEPCOMPLETE; 8244 ACQUIRE_LOCK(&lk); 8245 } else { 8246 dap->da_state |= MKDIR_BODY | MKDIR_PARENT; 8247 mkdir1 = setup_newdir(dap, newinum, dp->i_number, newdirbp, 8248 &mkdir2); 8249 } 8250 /* 8251 * Link into parent directory pagedep to await its being written. 8252 */ 8253 pagedep_lookup(mp, bp, dp->i_number, lbn, DEPALLOC, &pagedep); 8254 #ifdef DEBUG 8255 if (diradd_lookup(pagedep, offset) != NULL) 8256 panic("softdep_setup_directory_add: %p already at off %d\n", 8257 diradd_lookup(pagedep, offset), offset); 8258 #endif 8259 dap->da_pagedep = pagedep; 8260 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], dap, 8261 da_pdlist); 8262 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 8263 /* 8264 * If we're journaling, link the diradd into the jaddref so it 8265 * may be completed after the journal entry is written. Otherwise, 8266 * link the diradd into its inodedep. If the inode is not yet 8267 * written place it on the bufwait list, otherwise do the post-inode 8268 * write processing to put it on the id_pendinghd list. 8269 */ 8270 if (MOUNTEDSUJ(mp)) { 8271 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 8272 inoreflst); 8273 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 8274 ("softdep_setup_directory_add: bad jaddref %p", jaddref)); 8275 jaddref->ja_diroff = diroffset; 8276 jaddref->ja_diradd = dap; 8277 add_to_journal(&jaddref->ja_list); 8278 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) 8279 diradd_inode_written(dap, inodedep); 8280 else 8281 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 8282 /* 8283 * Add the journal entries for . and .. links now that the primary 8284 * link is written. 8285 */ 8286 if (mkdir1 != NULL && MOUNTEDSUJ(mp)) { 8287 jaddref = (struct jaddref *)TAILQ_PREV(&jaddref->ja_ref, 8288 inoreflst, if_deps); 8289 KASSERT(jaddref != NULL && 8290 jaddref->ja_ino == jaddref->ja_parent && 8291 (jaddref->ja_state & MKDIR_BODY), 8292 ("softdep_setup_directory_add: bad dot jaddref %p", 8293 jaddref)); 8294 mkdir1->md_jaddref = jaddref; 8295 jaddref->ja_mkdir = mkdir1; 8296 /* 8297 * It is important that the dotdot journal entry 8298 * is added prior to the dot entry since dot writes 8299 * both the dot and dotdot links. These both must 8300 * be added after the primary link for the journal 8301 * to remain consistent. 8302 */ 8303 add_to_journal(&mkdir2->md_jaddref->ja_list); 8304 add_to_journal(&jaddref->ja_list); 8305 } 8306 /* 8307 * If we are adding a new directory remember this diradd so that if 8308 * we rename it we can keep the dot and dotdot dependencies. If 8309 * we are adding a new name for an inode that has a mkdiradd we 8310 * must be in rename and we have to move the dot and dotdot 8311 * dependencies to this new name. The old name is being orphaned 8312 * soon. 8313 */ 8314 if (mkdir1 != NULL) { 8315 if (inodedep->id_mkdiradd != NULL) 8316 panic("softdep_setup_directory_add: Existing mkdir"); 8317 inodedep->id_mkdiradd = dap; 8318 } else if (inodedep->id_mkdiradd) 8319 merge_diradd(inodedep, dap); 8320 if (newdirblk) { 8321 /* 8322 * There is nothing to do if we are already tracking 8323 * this block. 8324 */ 8325 if ((pagedep->pd_state & NEWBLOCK) != 0) { 8326 WORKITEM_FREE(newdirblk, D_NEWDIRBLK); 8327 FREE_LOCK(&lk); 8328 return (0); 8329 } 8330 if (newblk_lookup(mp, dbtofsb(fs, bp->b_blkno), 0, &newblk) 8331 == 0) 8332 panic("softdep_setup_directory_add: lost entry"); 8333 WORKLIST_INSERT(&newblk->nb_newdirblk, &newdirblk->db_list); 8334 pagedep->pd_state |= NEWBLOCK; 8335 pagedep->pd_newdirblk = newdirblk; 8336 newdirblk->db_pagedep = pagedep; 8337 FREE_LOCK(&lk); 8338 /* 8339 * If we extended into an indirect signal direnter to sync. 8340 */ 8341 if (isindir) 8342 return (1); 8343 return (0); 8344 } 8345 FREE_LOCK(&lk); 8346 return (0); 8347 } 8348 8349 /* 8350 * This procedure is called to change the offset of a directory 8351 * entry when compacting a directory block which must be owned 8352 * exclusively by the caller. Note that the actual entry movement 8353 * must be done in this procedure to ensure that no I/O completions 8354 * occur while the move is in progress. 8355 */ 8356 void 8357 softdep_change_directoryentry_offset(bp, dp, base, oldloc, newloc, entrysize) 8358 struct buf *bp; /* Buffer holding directory block. */ 8359 struct inode *dp; /* inode for directory */ 8360 caddr_t base; /* address of dp->i_offset */ 8361 caddr_t oldloc; /* address of old directory location */ 8362 caddr_t newloc; /* address of new directory location */ 8363 int entrysize; /* size of directory entry */ 8364 { 8365 int offset, oldoffset, newoffset; 8366 struct pagedep *pagedep; 8367 struct jmvref *jmvref; 8368 struct diradd *dap; 8369 struct direct *de; 8370 struct mount *mp; 8371 ufs_lbn_t lbn; 8372 int flags; 8373 8374 mp = UFSTOVFS(dp->i_ump); 8375 de = (struct direct *)oldloc; 8376 jmvref = NULL; 8377 flags = 0; 8378 /* 8379 * Moves are always journaled as it would be too complex to 8380 * determine if any affected adds or removes are present in the 8381 * journal. 8382 */ 8383 if (MOUNTEDSUJ(mp)) { 8384 flags = DEPALLOC; 8385 jmvref = newjmvref(dp, de->d_ino, 8386 dp->i_offset + (oldloc - base), 8387 dp->i_offset + (newloc - base)); 8388 } 8389 lbn = lblkno(dp->i_fs, dp->i_offset); 8390 offset = blkoff(dp->i_fs, dp->i_offset); 8391 oldoffset = offset + (oldloc - base); 8392 newoffset = offset + (newloc - base); 8393 ACQUIRE_LOCK(&lk); 8394 if (pagedep_lookup(mp, bp, dp->i_number, lbn, flags, &pagedep) == 0) 8395 goto done; 8396 dap = diradd_lookup(pagedep, oldoffset); 8397 if (dap) { 8398 dap->da_offset = newoffset; 8399 newoffset = DIRADDHASH(newoffset); 8400 oldoffset = DIRADDHASH(oldoffset); 8401 if ((dap->da_state & ALLCOMPLETE) != ALLCOMPLETE && 8402 newoffset != oldoffset) { 8403 LIST_REMOVE(dap, da_pdlist); 8404 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[newoffset], 8405 dap, da_pdlist); 8406 } 8407 } 8408 done: 8409 if (jmvref) { 8410 jmvref->jm_pagedep = pagedep; 8411 LIST_INSERT_HEAD(&pagedep->pd_jmvrefhd, jmvref, jm_deps); 8412 add_to_journal(&jmvref->jm_list); 8413 } 8414 bcopy(oldloc, newloc, entrysize); 8415 FREE_LOCK(&lk); 8416 } 8417 8418 /* 8419 * Move the mkdir dependencies and journal work from one diradd to another 8420 * when renaming a directory. The new name must depend on the mkdir deps 8421 * completing as the old name did. Directories can only have one valid link 8422 * at a time so one must be canonical. 8423 */ 8424 static void 8425 merge_diradd(inodedep, newdap) 8426 struct inodedep *inodedep; 8427 struct diradd *newdap; 8428 { 8429 struct diradd *olddap; 8430 struct mkdir *mkdir, *nextmd; 8431 short state; 8432 8433 olddap = inodedep->id_mkdiradd; 8434 inodedep->id_mkdiradd = newdap; 8435 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8436 newdap->da_state &= ~DEPCOMPLETE; 8437 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 8438 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8439 if (mkdir->md_diradd != olddap) 8440 continue; 8441 mkdir->md_diradd = newdap; 8442 state = mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY); 8443 newdap->da_state |= state; 8444 olddap->da_state &= ~state; 8445 if ((olddap->da_state & 8446 (MKDIR_PARENT | MKDIR_BODY)) == 0) 8447 break; 8448 } 8449 if ((olddap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8450 panic("merge_diradd: unfound ref"); 8451 } 8452 /* 8453 * Any mkdir related journal items are not safe to be freed until 8454 * the new name is stable. 8455 */ 8456 jwork_move(&newdap->da_jwork, &olddap->da_jwork); 8457 olddap->da_state |= DEPCOMPLETE; 8458 complete_diradd(olddap); 8459 } 8460 8461 /* 8462 * Move the diradd to the pending list when all diradd dependencies are 8463 * complete. 8464 */ 8465 static void 8466 complete_diradd(dap) 8467 struct diradd *dap; 8468 { 8469 struct pagedep *pagedep; 8470 8471 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 8472 if (dap->da_state & DIRCHG) 8473 pagedep = dap->da_previous->dm_pagedep; 8474 else 8475 pagedep = dap->da_pagedep; 8476 LIST_REMOVE(dap, da_pdlist); 8477 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 8478 } 8479 } 8480 8481 /* 8482 * Cancel a diradd when a dirrem overlaps with it. We must cancel the journal 8483 * add entries and conditonally journal the remove. 8484 */ 8485 static void 8486 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref) 8487 struct diradd *dap; 8488 struct dirrem *dirrem; 8489 struct jremref *jremref; 8490 struct jremref *dotremref; 8491 struct jremref *dotdotremref; 8492 { 8493 struct inodedep *inodedep; 8494 struct jaddref *jaddref; 8495 struct inoref *inoref; 8496 struct mkdir *mkdir; 8497 8498 /* 8499 * If no remove references were allocated we're on a non-journaled 8500 * filesystem and can skip the cancel step. 8501 */ 8502 if (jremref == NULL) { 8503 free_diradd(dap, NULL); 8504 return; 8505 } 8506 /* 8507 * Cancel the primary name an free it if it does not require 8508 * journaling. 8509 */ 8510 if (inodedep_lookup(dap->da_list.wk_mp, dap->da_newinum, 8511 0, &inodedep) != 0) { 8512 /* Abort the addref that reference this diradd. */ 8513 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 8514 if (inoref->if_list.wk_type != D_JADDREF) 8515 continue; 8516 jaddref = (struct jaddref *)inoref; 8517 if (jaddref->ja_diradd != dap) 8518 continue; 8519 if (cancel_jaddref(jaddref, inodedep, 8520 &dirrem->dm_jwork) == 0) { 8521 free_jremref(jremref); 8522 jremref = NULL; 8523 } 8524 break; 8525 } 8526 } 8527 /* 8528 * Cancel subordinate names and free them if they do not require 8529 * journaling. 8530 */ 8531 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8532 LIST_FOREACH(mkdir, &mkdirlisthd, md_mkdirs) { 8533 if (mkdir->md_diradd != dap) 8534 continue; 8535 if ((jaddref = mkdir->md_jaddref) == NULL) 8536 continue; 8537 mkdir->md_jaddref = NULL; 8538 if (mkdir->md_state & MKDIR_PARENT) { 8539 if (cancel_jaddref(jaddref, NULL, 8540 &dirrem->dm_jwork) == 0) { 8541 free_jremref(dotdotremref); 8542 dotdotremref = NULL; 8543 } 8544 } else { 8545 if (cancel_jaddref(jaddref, inodedep, 8546 &dirrem->dm_jwork) == 0) { 8547 free_jremref(dotremref); 8548 dotremref = NULL; 8549 } 8550 } 8551 } 8552 } 8553 8554 if (jremref) 8555 journal_jremref(dirrem, jremref, inodedep); 8556 if (dotremref) 8557 journal_jremref(dirrem, dotremref, inodedep); 8558 if (dotdotremref) 8559 journal_jremref(dirrem, dotdotremref, NULL); 8560 jwork_move(&dirrem->dm_jwork, &dap->da_jwork); 8561 free_diradd(dap, &dirrem->dm_jwork); 8562 } 8563 8564 /* 8565 * Free a diradd dependency structure. This routine must be called 8566 * with splbio interrupts blocked. 8567 */ 8568 static void 8569 free_diradd(dap, wkhd) 8570 struct diradd *dap; 8571 struct workhead *wkhd; 8572 { 8573 struct dirrem *dirrem; 8574 struct pagedep *pagedep; 8575 struct inodedep *inodedep; 8576 struct mkdir *mkdir, *nextmd; 8577 8578 mtx_assert(&lk, MA_OWNED); 8579 LIST_REMOVE(dap, da_pdlist); 8580 if (dap->da_state & ONWORKLIST) 8581 WORKLIST_REMOVE(&dap->da_list); 8582 if ((dap->da_state & DIRCHG) == 0) { 8583 pagedep = dap->da_pagedep; 8584 } else { 8585 dirrem = dap->da_previous; 8586 pagedep = dirrem->dm_pagedep; 8587 dirrem->dm_dirinum = pagedep->pd_ino; 8588 dirrem->dm_state |= COMPLETE; 8589 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 8590 add_to_worklist(&dirrem->dm_list, 0); 8591 } 8592 if (inodedep_lookup(pagedep->pd_list.wk_mp, dap->da_newinum, 8593 0, &inodedep) != 0) 8594 if (inodedep->id_mkdiradd == dap) 8595 inodedep->id_mkdiradd = NULL; 8596 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) { 8597 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; mkdir = nextmd) { 8598 nextmd = LIST_NEXT(mkdir, md_mkdirs); 8599 if (mkdir->md_diradd != dap) 8600 continue; 8601 dap->da_state &= 8602 ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 8603 LIST_REMOVE(mkdir, md_mkdirs); 8604 if (mkdir->md_state & ONWORKLIST) 8605 WORKLIST_REMOVE(&mkdir->md_list); 8606 if (mkdir->md_jaddref != NULL) 8607 panic("free_diradd: Unexpected jaddref"); 8608 WORKITEM_FREE(mkdir, D_MKDIR); 8609 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) 8610 break; 8611 } 8612 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) != 0) 8613 panic("free_diradd: unfound ref"); 8614 } 8615 if (inodedep) 8616 free_inodedep(inodedep); 8617 /* 8618 * Free any journal segments waiting for the directory write. 8619 */ 8620 handle_jwork(&dap->da_jwork); 8621 WORKITEM_FREE(dap, D_DIRADD); 8622 } 8623 8624 /* 8625 * Directory entry removal dependencies. 8626 * 8627 * When removing a directory entry, the entry's inode pointer must be 8628 * zero'ed on disk before the corresponding inode's link count is decremented 8629 * (possibly freeing the inode for re-use). This dependency is handled by 8630 * updating the directory entry but delaying the inode count reduction until 8631 * after the directory block has been written to disk. After this point, the 8632 * inode count can be decremented whenever it is convenient. 8633 */ 8634 8635 /* 8636 * This routine should be called immediately after removing 8637 * a directory entry. The inode's link count should not be 8638 * decremented by the calling procedure -- the soft updates 8639 * code will do this task when it is safe. 8640 */ 8641 void 8642 softdep_setup_remove(bp, dp, ip, isrmdir) 8643 struct buf *bp; /* buffer containing directory block */ 8644 struct inode *dp; /* inode for the directory being modified */ 8645 struct inode *ip; /* inode for directory entry being removed */ 8646 int isrmdir; /* indicates if doing RMDIR */ 8647 { 8648 struct dirrem *dirrem, *prevdirrem; 8649 struct inodedep *inodedep; 8650 int direct; 8651 8652 /* 8653 * Allocate a new dirrem if appropriate and ACQUIRE_LOCK. We want 8654 * newdirrem() to setup the full directory remove which requires 8655 * isrmdir > 1. 8656 */ 8657 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 8658 /* 8659 * Add the dirrem to the inodedep's pending remove list for quick 8660 * discovery later. 8661 */ 8662 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8663 &inodedep) == 0) 8664 panic("softdep_setup_remove: Lost inodedep."); 8665 KASSERT((inodedep->id_state & UNLINKED) == 0, ("inode unlinked")); 8666 dirrem->dm_state |= ONDEPLIST; 8667 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 8668 8669 /* 8670 * If the COMPLETE flag is clear, then there were no active 8671 * entries and we want to roll back to a zeroed entry until 8672 * the new inode is committed to disk. If the COMPLETE flag is 8673 * set then we have deleted an entry that never made it to 8674 * disk. If the entry we deleted resulted from a name change, 8675 * then the old name still resides on disk. We cannot delete 8676 * its inode (returned to us in prevdirrem) until the zeroed 8677 * directory entry gets to disk. The new inode has never been 8678 * referenced on the disk, so can be deleted immediately. 8679 */ 8680 if ((dirrem->dm_state & COMPLETE) == 0) { 8681 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, dirrem, 8682 dm_next); 8683 FREE_LOCK(&lk); 8684 } else { 8685 if (prevdirrem != NULL) 8686 LIST_INSERT_HEAD(&dirrem->dm_pagedep->pd_dirremhd, 8687 prevdirrem, dm_next); 8688 dirrem->dm_dirinum = dirrem->dm_pagedep->pd_ino; 8689 direct = LIST_EMPTY(&dirrem->dm_jremrefhd); 8690 FREE_LOCK(&lk); 8691 if (direct) 8692 handle_workitem_remove(dirrem, 0); 8693 } 8694 } 8695 8696 /* 8697 * Check for an entry matching 'offset' on both the pd_dirraddhd list and the 8698 * pd_pendinghd list of a pagedep. 8699 */ 8700 static struct diradd * 8701 diradd_lookup(pagedep, offset) 8702 struct pagedep *pagedep; 8703 int offset; 8704 { 8705 struct diradd *dap; 8706 8707 LIST_FOREACH(dap, &pagedep->pd_diraddhd[DIRADDHASH(offset)], da_pdlist) 8708 if (dap->da_offset == offset) 8709 return (dap); 8710 LIST_FOREACH(dap, &pagedep->pd_pendinghd, da_pdlist) 8711 if (dap->da_offset == offset) 8712 return (dap); 8713 return (NULL); 8714 } 8715 8716 /* 8717 * Search for a .. diradd dependency in a directory that is being removed. 8718 * If the directory was renamed to a new parent we have a diradd rather 8719 * than a mkdir for the .. entry. We need to cancel it now before 8720 * it is found in truncate(). 8721 */ 8722 static struct jremref * 8723 cancel_diradd_dotdot(ip, dirrem, jremref) 8724 struct inode *ip; 8725 struct dirrem *dirrem; 8726 struct jremref *jremref; 8727 { 8728 struct pagedep *pagedep; 8729 struct diradd *dap; 8730 struct worklist *wk; 8731 8732 if (pagedep_lookup(UFSTOVFS(ip->i_ump), NULL, ip->i_number, 0, 0, 8733 &pagedep) == 0) 8734 return (jremref); 8735 dap = diradd_lookup(pagedep, DOTDOT_OFFSET); 8736 if (dap == NULL) 8737 return (jremref); 8738 cancel_diradd(dap, dirrem, jremref, NULL, NULL); 8739 /* 8740 * Mark any journal work as belonging to the parent so it is freed 8741 * with the .. reference. 8742 */ 8743 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 8744 wk->wk_state |= MKDIR_PARENT; 8745 return (NULL); 8746 } 8747 8748 /* 8749 * Cancel the MKDIR_PARENT mkdir component of a diradd when we're going to 8750 * replace it with a dirrem/diradd pair as a result of re-parenting a 8751 * directory. This ensures that we don't simultaneously have a mkdir and 8752 * a diradd for the same .. entry. 8753 */ 8754 static struct jremref * 8755 cancel_mkdir_dotdot(ip, dirrem, jremref) 8756 struct inode *ip; 8757 struct dirrem *dirrem; 8758 struct jremref *jremref; 8759 { 8760 struct inodedep *inodedep; 8761 struct jaddref *jaddref; 8762 struct mkdir *mkdir; 8763 struct diradd *dap; 8764 8765 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 8766 &inodedep) == 0) 8767 return (jremref); 8768 dap = inodedep->id_mkdiradd; 8769 if (dap == NULL || (dap->da_state & MKDIR_PARENT) == 0) 8770 return (jremref); 8771 for (mkdir = LIST_FIRST(&mkdirlisthd); mkdir; 8772 mkdir = LIST_NEXT(mkdir, md_mkdirs)) 8773 if (mkdir->md_diradd == dap && mkdir->md_state & MKDIR_PARENT) 8774 break; 8775 if (mkdir == NULL) 8776 panic("cancel_mkdir_dotdot: Unable to find mkdir\n"); 8777 if ((jaddref = mkdir->md_jaddref) != NULL) { 8778 mkdir->md_jaddref = NULL; 8779 jaddref->ja_state &= ~MKDIR_PARENT; 8780 if (inodedep_lookup(UFSTOVFS(ip->i_ump), jaddref->ja_ino, 0, 8781 &inodedep) == 0) 8782 panic("cancel_mkdir_dotdot: Lost parent inodedep"); 8783 if (cancel_jaddref(jaddref, inodedep, &dirrem->dm_jwork)) { 8784 journal_jremref(dirrem, jremref, inodedep); 8785 jremref = NULL; 8786 } 8787 } 8788 if (mkdir->md_state & ONWORKLIST) 8789 WORKLIST_REMOVE(&mkdir->md_list); 8790 mkdir->md_state |= ALLCOMPLETE; 8791 complete_mkdir(mkdir); 8792 return (jremref); 8793 } 8794 8795 static void 8796 journal_jremref(dirrem, jremref, inodedep) 8797 struct dirrem *dirrem; 8798 struct jremref *jremref; 8799 struct inodedep *inodedep; 8800 { 8801 8802 if (inodedep == NULL) 8803 if (inodedep_lookup(jremref->jr_list.wk_mp, 8804 jremref->jr_ref.if_ino, 0, &inodedep) == 0) 8805 panic("journal_jremref: Lost inodedep"); 8806 LIST_INSERT_HEAD(&dirrem->dm_jremrefhd, jremref, jr_deps); 8807 TAILQ_INSERT_TAIL(&inodedep->id_inoreflst, &jremref->jr_ref, if_deps); 8808 add_to_journal(&jremref->jr_list); 8809 } 8810 8811 static void 8812 dirrem_journal(dirrem, jremref, dotremref, dotdotremref) 8813 struct dirrem *dirrem; 8814 struct jremref *jremref; 8815 struct jremref *dotremref; 8816 struct jremref *dotdotremref; 8817 { 8818 struct inodedep *inodedep; 8819 8820 8821 if (inodedep_lookup(jremref->jr_list.wk_mp, jremref->jr_ref.if_ino, 0, 8822 &inodedep) == 0) 8823 panic("dirrem_journal: Lost inodedep"); 8824 journal_jremref(dirrem, jremref, inodedep); 8825 if (dotremref) 8826 journal_jremref(dirrem, dotremref, inodedep); 8827 if (dotdotremref) 8828 journal_jremref(dirrem, dotdotremref, NULL); 8829 } 8830 8831 /* 8832 * Allocate a new dirrem if appropriate and return it along with 8833 * its associated pagedep. Called without a lock, returns with lock. 8834 */ 8835 static struct dirrem * 8836 newdirrem(bp, dp, ip, isrmdir, prevdirremp) 8837 struct buf *bp; /* buffer containing directory block */ 8838 struct inode *dp; /* inode for the directory being modified */ 8839 struct inode *ip; /* inode for directory entry being removed */ 8840 int isrmdir; /* indicates if doing RMDIR */ 8841 struct dirrem **prevdirremp; /* previously referenced inode, if any */ 8842 { 8843 int offset; 8844 ufs_lbn_t lbn; 8845 struct diradd *dap; 8846 struct dirrem *dirrem; 8847 struct pagedep *pagedep; 8848 struct jremref *jremref; 8849 struct jremref *dotremref; 8850 struct jremref *dotdotremref; 8851 struct vnode *dvp; 8852 8853 /* 8854 * Whiteouts have no deletion dependencies. 8855 */ 8856 if (ip == NULL) 8857 panic("newdirrem: whiteout"); 8858 dvp = ITOV(dp); 8859 /* 8860 * If we are over our limit, try to improve the situation. 8861 * Limiting the number of dirrem structures will also limit 8862 * the number of freefile and freeblks structures. 8863 */ 8864 ACQUIRE_LOCK(&lk); 8865 if (!IS_SNAPSHOT(ip) && dep_current[D_DIRREM] > max_softdeps / 2) 8866 (void) request_cleanup(ITOV(dp)->v_mount, FLUSH_BLOCKS); 8867 FREE_LOCK(&lk); 8868 dirrem = malloc(sizeof(struct dirrem), 8869 M_DIRREM, M_SOFTDEP_FLAGS|M_ZERO); 8870 workitem_alloc(&dirrem->dm_list, D_DIRREM, dvp->v_mount); 8871 LIST_INIT(&dirrem->dm_jremrefhd); 8872 LIST_INIT(&dirrem->dm_jwork); 8873 dirrem->dm_state = isrmdir ? RMDIR : 0; 8874 dirrem->dm_oldinum = ip->i_number; 8875 *prevdirremp = NULL; 8876 /* 8877 * Allocate remove reference structures to track journal write 8878 * dependencies. We will always have one for the link and 8879 * when doing directories we will always have one more for dot. 8880 * When renaming a directory we skip the dotdot link change so 8881 * this is not needed. 8882 */ 8883 jremref = dotremref = dotdotremref = NULL; 8884 if (DOINGSUJ(dvp)) { 8885 if (isrmdir) { 8886 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 8887 ip->i_effnlink + 2); 8888 dotremref = newjremref(dirrem, ip, ip, DOT_OFFSET, 8889 ip->i_effnlink + 1); 8890 dotdotremref = newjremref(dirrem, ip, dp, DOTDOT_OFFSET, 8891 dp->i_effnlink + 1); 8892 dotdotremref->jr_state |= MKDIR_PARENT; 8893 } else 8894 jremref = newjremref(dirrem, dp, ip, dp->i_offset, 8895 ip->i_effnlink + 1); 8896 } 8897 ACQUIRE_LOCK(&lk); 8898 lbn = lblkno(dp->i_fs, dp->i_offset); 8899 offset = blkoff(dp->i_fs, dp->i_offset); 8900 pagedep_lookup(UFSTOVFS(dp->i_ump), bp, dp->i_number, lbn, DEPALLOC, 8901 &pagedep); 8902 dirrem->dm_pagedep = pagedep; 8903 dirrem->dm_offset = offset; 8904 /* 8905 * If we're renaming a .. link to a new directory, cancel any 8906 * existing MKDIR_PARENT mkdir. If it has already been canceled 8907 * the jremref is preserved for any potential diradd in this 8908 * location. This can not coincide with a rmdir. 8909 */ 8910 if (dp->i_offset == DOTDOT_OFFSET) { 8911 if (isrmdir) 8912 panic("newdirrem: .. directory change during remove?"); 8913 jremref = cancel_mkdir_dotdot(dp, dirrem, jremref); 8914 } 8915 /* 8916 * If we're removing a directory search for the .. dependency now and 8917 * cancel it. Any pending journal work will be added to the dirrem 8918 * to be completed when the workitem remove completes. 8919 */ 8920 if (isrmdir) 8921 dotdotremref = cancel_diradd_dotdot(ip, dirrem, dotdotremref); 8922 /* 8923 * Check for a diradd dependency for the same directory entry. 8924 * If present, then both dependencies become obsolete and can 8925 * be de-allocated. 8926 */ 8927 dap = diradd_lookup(pagedep, offset); 8928 if (dap == NULL) { 8929 /* 8930 * Link the jremref structures into the dirrem so they are 8931 * written prior to the pagedep. 8932 */ 8933 if (jremref) 8934 dirrem_journal(dirrem, jremref, dotremref, 8935 dotdotremref); 8936 return (dirrem); 8937 } 8938 /* 8939 * Must be ATTACHED at this point. 8940 */ 8941 if ((dap->da_state & ATTACHED) == 0) 8942 panic("newdirrem: not ATTACHED"); 8943 if (dap->da_newinum != ip->i_number) 8944 panic("newdirrem: inum %ju should be %ju", 8945 (uintmax_t)ip->i_number, (uintmax_t)dap->da_newinum); 8946 /* 8947 * If we are deleting a changed name that never made it to disk, 8948 * then return the dirrem describing the previous inode (which 8949 * represents the inode currently referenced from this entry on disk). 8950 */ 8951 if ((dap->da_state & DIRCHG) != 0) { 8952 *prevdirremp = dap->da_previous; 8953 dap->da_state &= ~DIRCHG; 8954 dap->da_pagedep = pagedep; 8955 } 8956 /* 8957 * We are deleting an entry that never made it to disk. 8958 * Mark it COMPLETE so we can delete its inode immediately. 8959 */ 8960 dirrem->dm_state |= COMPLETE; 8961 cancel_diradd(dap, dirrem, jremref, dotremref, dotdotremref); 8962 #ifdef SUJ_DEBUG 8963 if (isrmdir == 0) { 8964 struct worklist *wk; 8965 8966 LIST_FOREACH(wk, &dirrem->dm_jwork, wk_list) 8967 if (wk->wk_state & (MKDIR_BODY | MKDIR_PARENT)) 8968 panic("bad wk %p (0x%X)\n", wk, wk->wk_state); 8969 } 8970 #endif 8971 8972 return (dirrem); 8973 } 8974 8975 /* 8976 * Directory entry change dependencies. 8977 * 8978 * Changing an existing directory entry requires that an add operation 8979 * be completed first followed by a deletion. The semantics for the addition 8980 * are identical to the description of adding a new entry above except 8981 * that the rollback is to the old inode number rather than zero. Once 8982 * the addition dependency is completed, the removal is done as described 8983 * in the removal routine above. 8984 */ 8985 8986 /* 8987 * This routine should be called immediately after changing 8988 * a directory entry. The inode's link count should not be 8989 * decremented by the calling procedure -- the soft updates 8990 * code will perform this task when it is safe. 8991 */ 8992 void 8993 softdep_setup_directory_change(bp, dp, ip, newinum, isrmdir) 8994 struct buf *bp; /* buffer containing directory block */ 8995 struct inode *dp; /* inode for the directory being modified */ 8996 struct inode *ip; /* inode for directory entry being removed */ 8997 ino_t newinum; /* new inode number for changed entry */ 8998 int isrmdir; /* indicates if doing RMDIR */ 8999 { 9000 int offset; 9001 struct diradd *dap = NULL; 9002 struct dirrem *dirrem, *prevdirrem; 9003 struct pagedep *pagedep; 9004 struct inodedep *inodedep; 9005 struct jaddref *jaddref; 9006 struct mount *mp; 9007 9008 offset = blkoff(dp->i_fs, dp->i_offset); 9009 mp = UFSTOVFS(dp->i_ump); 9010 9011 /* 9012 * Whiteouts do not need diradd dependencies. 9013 */ 9014 if (newinum != WINO) { 9015 dap = malloc(sizeof(struct diradd), 9016 M_DIRADD, M_SOFTDEP_FLAGS|M_ZERO); 9017 workitem_alloc(&dap->da_list, D_DIRADD, mp); 9018 dap->da_state = DIRCHG | ATTACHED | DEPCOMPLETE; 9019 dap->da_offset = offset; 9020 dap->da_newinum = newinum; 9021 LIST_INIT(&dap->da_jwork); 9022 } 9023 9024 /* 9025 * Allocate a new dirrem and ACQUIRE_LOCK. 9026 */ 9027 dirrem = newdirrem(bp, dp, ip, isrmdir, &prevdirrem); 9028 pagedep = dirrem->dm_pagedep; 9029 /* 9030 * The possible values for isrmdir: 9031 * 0 - non-directory file rename 9032 * 1 - directory rename within same directory 9033 * inum - directory rename to new directory of given inode number 9034 * When renaming to a new directory, we are both deleting and 9035 * creating a new directory entry, so the link count on the new 9036 * directory should not change. Thus we do not need the followup 9037 * dirrem which is usually done in handle_workitem_remove. We set 9038 * the DIRCHG flag to tell handle_workitem_remove to skip the 9039 * followup dirrem. 9040 */ 9041 if (isrmdir > 1) 9042 dirrem->dm_state |= DIRCHG; 9043 9044 /* 9045 * Whiteouts have no additional dependencies, 9046 * so just put the dirrem on the correct list. 9047 */ 9048 if (newinum == WINO) { 9049 if ((dirrem->dm_state & COMPLETE) == 0) { 9050 LIST_INSERT_HEAD(&pagedep->pd_dirremhd, dirrem, 9051 dm_next); 9052 } else { 9053 dirrem->dm_dirinum = pagedep->pd_ino; 9054 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9055 add_to_worklist(&dirrem->dm_list, 0); 9056 } 9057 FREE_LOCK(&lk); 9058 return; 9059 } 9060 /* 9061 * Add the dirrem to the inodedep's pending remove list for quick 9062 * discovery later. A valid nlinkdelta ensures that this lookup 9063 * will not fail. 9064 */ 9065 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 9066 panic("softdep_setup_directory_change: Lost inodedep."); 9067 dirrem->dm_state |= ONDEPLIST; 9068 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9069 9070 /* 9071 * If the COMPLETE flag is clear, then there were no active 9072 * entries and we want to roll back to the previous inode until 9073 * the new inode is committed to disk. If the COMPLETE flag is 9074 * set, then we have deleted an entry that never made it to disk. 9075 * If the entry we deleted resulted from a name change, then the old 9076 * inode reference still resides on disk. Any rollback that we do 9077 * needs to be to that old inode (returned to us in prevdirrem). If 9078 * the entry we deleted resulted from a create, then there is 9079 * no entry on the disk, so we want to roll back to zero rather 9080 * than the uncommitted inode. In either of the COMPLETE cases we 9081 * want to immediately free the unwritten and unreferenced inode. 9082 */ 9083 if ((dirrem->dm_state & COMPLETE) == 0) { 9084 dap->da_previous = dirrem; 9085 } else { 9086 if (prevdirrem != NULL) { 9087 dap->da_previous = prevdirrem; 9088 } else { 9089 dap->da_state &= ~DIRCHG; 9090 dap->da_pagedep = pagedep; 9091 } 9092 dirrem->dm_dirinum = pagedep->pd_ino; 9093 if (LIST_EMPTY(&dirrem->dm_jremrefhd)) 9094 add_to_worklist(&dirrem->dm_list, 0); 9095 } 9096 /* 9097 * Lookup the jaddref for this journal entry. We must finish 9098 * initializing it and make the diradd write dependent on it. 9099 * If we're not journaling, put it on the id_bufwait list if the 9100 * inode is not yet written. If it is written, do the post-inode 9101 * write processing to put it on the id_pendinghd list. 9102 */ 9103 inodedep_lookup(mp, newinum, DEPALLOC | NODELAY, &inodedep); 9104 if (MOUNTEDSUJ(mp)) { 9105 jaddref = (struct jaddref *)TAILQ_LAST(&inodedep->id_inoreflst, 9106 inoreflst); 9107 KASSERT(jaddref != NULL && jaddref->ja_parent == dp->i_number, 9108 ("softdep_setup_directory_change: bad jaddref %p", 9109 jaddref)); 9110 jaddref->ja_diroff = dp->i_offset; 9111 jaddref->ja_diradd = dap; 9112 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9113 dap, da_pdlist); 9114 add_to_journal(&jaddref->ja_list); 9115 } else if ((inodedep->id_state & ALLCOMPLETE) == ALLCOMPLETE) { 9116 dap->da_state |= COMPLETE; 9117 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, da_pdlist); 9118 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 9119 } else { 9120 LIST_INSERT_HEAD(&pagedep->pd_diraddhd[DIRADDHASH(offset)], 9121 dap, da_pdlist); 9122 WORKLIST_INSERT(&inodedep->id_bufwait, &dap->da_list); 9123 } 9124 /* 9125 * If we're making a new name for a directory that has not been 9126 * committed when need to move the dot and dotdot references to 9127 * this new name. 9128 */ 9129 if (inodedep->id_mkdiradd && dp->i_offset != DOTDOT_OFFSET) 9130 merge_diradd(inodedep, dap); 9131 FREE_LOCK(&lk); 9132 } 9133 9134 /* 9135 * Called whenever the link count on an inode is changed. 9136 * It creates an inode dependency so that the new reference(s) 9137 * to the inode cannot be committed to disk until the updated 9138 * inode has been written. 9139 */ 9140 void 9141 softdep_change_linkcnt(ip) 9142 struct inode *ip; /* the inode with the increased link count */ 9143 { 9144 struct inodedep *inodedep; 9145 int dflags; 9146 9147 ACQUIRE_LOCK(&lk); 9148 dflags = DEPALLOC; 9149 if (IS_SNAPSHOT(ip)) 9150 dflags |= NODELAY; 9151 inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, dflags, &inodedep); 9152 if (ip->i_nlink < ip->i_effnlink) 9153 panic("softdep_change_linkcnt: bad delta"); 9154 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9155 FREE_LOCK(&lk); 9156 } 9157 9158 /* 9159 * Attach a sbdep dependency to the superblock buf so that we can keep 9160 * track of the head of the linked list of referenced but unlinked inodes. 9161 */ 9162 void 9163 softdep_setup_sbupdate(ump, fs, bp) 9164 struct ufsmount *ump; 9165 struct fs *fs; 9166 struct buf *bp; 9167 { 9168 struct sbdep *sbdep; 9169 struct worklist *wk; 9170 9171 if (MOUNTEDSUJ(UFSTOVFS(ump)) == 0) 9172 return; 9173 LIST_FOREACH(wk, &bp->b_dep, wk_list) 9174 if (wk->wk_type == D_SBDEP) 9175 break; 9176 if (wk != NULL) 9177 return; 9178 sbdep = malloc(sizeof(struct sbdep), M_SBDEP, M_SOFTDEP_FLAGS); 9179 workitem_alloc(&sbdep->sb_list, D_SBDEP, UFSTOVFS(ump)); 9180 sbdep->sb_fs = fs; 9181 sbdep->sb_ump = ump; 9182 ACQUIRE_LOCK(&lk); 9183 WORKLIST_INSERT(&bp->b_dep, &sbdep->sb_list); 9184 FREE_LOCK(&lk); 9185 } 9186 9187 /* 9188 * Return the first unlinked inodedep which is ready to be the head of the 9189 * list. The inodedep and all those after it must have valid next pointers. 9190 */ 9191 static struct inodedep * 9192 first_unlinked_inodedep(ump) 9193 struct ufsmount *ump; 9194 { 9195 struct inodedep *inodedep; 9196 struct inodedep *idp; 9197 9198 mtx_assert(&lk, MA_OWNED); 9199 for (inodedep = TAILQ_LAST(&ump->softdep_unlinked, inodedeplst); 9200 inodedep; inodedep = idp) { 9201 if ((inodedep->id_state & UNLINKNEXT) == 0) 9202 return (NULL); 9203 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9204 if (idp == NULL || (idp->id_state & UNLINKNEXT) == 0) 9205 break; 9206 if ((inodedep->id_state & UNLINKPREV) == 0) 9207 break; 9208 } 9209 return (inodedep); 9210 } 9211 9212 /* 9213 * Set the sujfree unlinked head pointer prior to writing a superblock. 9214 */ 9215 static void 9216 initiate_write_sbdep(sbdep) 9217 struct sbdep *sbdep; 9218 { 9219 struct inodedep *inodedep; 9220 struct fs *bpfs; 9221 struct fs *fs; 9222 9223 bpfs = sbdep->sb_fs; 9224 fs = sbdep->sb_ump->um_fs; 9225 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9226 if (inodedep) { 9227 fs->fs_sujfree = inodedep->id_ino; 9228 inodedep->id_state |= UNLINKPREV; 9229 } else 9230 fs->fs_sujfree = 0; 9231 bpfs->fs_sujfree = fs->fs_sujfree; 9232 } 9233 9234 /* 9235 * After a superblock is written determine whether it must be written again 9236 * due to a changing unlinked list head. 9237 */ 9238 static int 9239 handle_written_sbdep(sbdep, bp) 9240 struct sbdep *sbdep; 9241 struct buf *bp; 9242 { 9243 struct inodedep *inodedep; 9244 struct mount *mp; 9245 struct fs *fs; 9246 9247 mtx_assert(&lk, MA_OWNED); 9248 fs = sbdep->sb_fs; 9249 mp = UFSTOVFS(sbdep->sb_ump); 9250 /* 9251 * If the superblock doesn't match the in-memory list start over. 9252 */ 9253 inodedep = first_unlinked_inodedep(sbdep->sb_ump); 9254 if ((inodedep && fs->fs_sujfree != inodedep->id_ino) || 9255 (inodedep == NULL && fs->fs_sujfree != 0)) { 9256 bdirty(bp); 9257 return (1); 9258 } 9259 WORKITEM_FREE(sbdep, D_SBDEP); 9260 if (fs->fs_sujfree == 0) 9261 return (0); 9262 /* 9263 * Now that we have a record of this inode in stable store allow it 9264 * to be written to free up pending work. Inodes may see a lot of 9265 * write activity after they are unlinked which we must not hold up. 9266 */ 9267 for (; inodedep != NULL; inodedep = TAILQ_NEXT(inodedep, id_unlinked)) { 9268 if ((inodedep->id_state & UNLINKLINKS) != UNLINKLINKS) 9269 panic("handle_written_sbdep: Bad inodedep %p (0x%X)", 9270 inodedep, inodedep->id_state); 9271 if (inodedep->id_state & UNLINKONLIST) 9272 break; 9273 inodedep->id_state |= DEPCOMPLETE | UNLINKONLIST; 9274 } 9275 9276 return (0); 9277 } 9278 9279 /* 9280 * Mark an inodedep as unlinked and insert it into the in-memory unlinked list. 9281 */ 9282 static void 9283 unlinked_inodedep(mp, inodedep) 9284 struct mount *mp; 9285 struct inodedep *inodedep; 9286 { 9287 struct ufsmount *ump; 9288 9289 mtx_assert(&lk, MA_OWNED); 9290 if (MOUNTEDSUJ(mp) == 0) 9291 return; 9292 ump = VFSTOUFS(mp); 9293 ump->um_fs->fs_fmod = 1; 9294 if (inodedep->id_state & UNLINKED) 9295 panic("unlinked_inodedep: %p already unlinked\n", inodedep); 9296 inodedep->id_state |= UNLINKED; 9297 TAILQ_INSERT_HEAD(&ump->softdep_unlinked, inodedep, id_unlinked); 9298 } 9299 9300 /* 9301 * Remove an inodedep from the unlinked inodedep list. This may require 9302 * disk writes if the inode has made it that far. 9303 */ 9304 static void 9305 clear_unlinked_inodedep(inodedep) 9306 struct inodedep *inodedep; 9307 { 9308 struct ufsmount *ump; 9309 struct inodedep *idp; 9310 struct inodedep *idn; 9311 struct fs *fs; 9312 struct buf *bp; 9313 ino_t ino; 9314 ino_t nino; 9315 ino_t pino; 9316 int error; 9317 9318 ump = VFSTOUFS(inodedep->id_list.wk_mp); 9319 fs = ump->um_fs; 9320 ino = inodedep->id_ino; 9321 error = 0; 9322 for (;;) { 9323 mtx_assert(&lk, MA_OWNED); 9324 KASSERT((inodedep->id_state & UNLINKED) != 0, 9325 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9326 inodedep)); 9327 /* 9328 * If nothing has yet been written simply remove us from 9329 * the in memory list and return. This is the most common 9330 * case where handle_workitem_remove() loses the final 9331 * reference. 9332 */ 9333 if ((inodedep->id_state & UNLINKLINKS) == 0) 9334 break; 9335 /* 9336 * If we have a NEXT pointer and no PREV pointer we can simply 9337 * clear NEXT's PREV and remove ourselves from the list. Be 9338 * careful not to clear PREV if the superblock points at 9339 * next as well. 9340 */ 9341 idn = TAILQ_NEXT(inodedep, id_unlinked); 9342 if ((inodedep->id_state & UNLINKLINKS) == UNLINKNEXT) { 9343 if (idn && fs->fs_sujfree != idn->id_ino) 9344 idn->id_state &= ~UNLINKPREV; 9345 break; 9346 } 9347 /* 9348 * Here we have an inodedep which is actually linked into 9349 * the list. We must remove it by forcing a write to the 9350 * link before us, whether it be the superblock or an inode. 9351 * Unfortunately the list may change while we're waiting 9352 * on the buf lock for either resource so we must loop until 9353 * we lock the right one. If both the superblock and an 9354 * inode point to this inode we must clear the inode first 9355 * followed by the superblock. 9356 */ 9357 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9358 pino = 0; 9359 if (idp && (idp->id_state & UNLINKNEXT)) 9360 pino = idp->id_ino; 9361 FREE_LOCK(&lk); 9362 if (pino == 0) 9363 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9364 (int)fs->fs_sbsize, 0, 0, 0); 9365 else 9366 error = bread(ump->um_devvp, 9367 fsbtodb(fs, ino_to_fsba(fs, pino)), 9368 (int)fs->fs_bsize, NOCRED, &bp); 9369 ACQUIRE_LOCK(&lk); 9370 if (error) 9371 break; 9372 /* If the list has changed restart the loop. */ 9373 idp = TAILQ_PREV(inodedep, inodedeplst, id_unlinked); 9374 nino = 0; 9375 if (idp && (idp->id_state & UNLINKNEXT)) 9376 nino = idp->id_ino; 9377 if (nino != pino || 9378 (inodedep->id_state & UNLINKPREV) != UNLINKPREV) { 9379 FREE_LOCK(&lk); 9380 brelse(bp); 9381 ACQUIRE_LOCK(&lk); 9382 continue; 9383 } 9384 nino = 0; 9385 idn = TAILQ_NEXT(inodedep, id_unlinked); 9386 if (idn) 9387 nino = idn->id_ino; 9388 /* 9389 * Remove us from the in memory list. After this we cannot 9390 * access the inodedep. 9391 */ 9392 KASSERT((inodedep->id_state & UNLINKED) != 0, 9393 ("clear_unlinked_inodedep: inodedep %p not unlinked", 9394 inodedep)); 9395 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9396 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9397 FREE_LOCK(&lk); 9398 /* 9399 * The predecessor's next pointer is manually updated here 9400 * so that the NEXT flag is never cleared for an element 9401 * that is in the list. 9402 */ 9403 if (pino == 0) { 9404 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9405 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9406 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9407 bp); 9408 } else if (fs->fs_magic == FS_UFS1_MAGIC) 9409 ((struct ufs1_dinode *)bp->b_data + 9410 ino_to_fsbo(fs, pino))->di_freelink = nino; 9411 else 9412 ((struct ufs2_dinode *)bp->b_data + 9413 ino_to_fsbo(fs, pino))->di_freelink = nino; 9414 /* 9415 * If the bwrite fails we have no recourse to recover. The 9416 * filesystem is corrupted already. 9417 */ 9418 bwrite(bp); 9419 ACQUIRE_LOCK(&lk); 9420 /* 9421 * If the superblock pointer still needs to be cleared force 9422 * a write here. 9423 */ 9424 if (fs->fs_sujfree == ino) { 9425 FREE_LOCK(&lk); 9426 bp = getblk(ump->um_devvp, btodb(fs->fs_sblockloc), 9427 (int)fs->fs_sbsize, 0, 0, 0); 9428 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize); 9429 ffs_oldfscompat_write((struct fs *)bp->b_data, ump); 9430 softdep_setup_sbupdate(ump, (struct fs *)bp->b_data, 9431 bp); 9432 bwrite(bp); 9433 ACQUIRE_LOCK(&lk); 9434 } 9435 9436 if (fs->fs_sujfree != ino) 9437 return; 9438 panic("clear_unlinked_inodedep: Failed to clear free head"); 9439 } 9440 if (inodedep->id_ino == fs->fs_sujfree) 9441 panic("clear_unlinked_inodedep: Freeing head of free list"); 9442 inodedep->id_state &= ~(UNLINKED | UNLINKLINKS | UNLINKONLIST); 9443 TAILQ_REMOVE(&ump->softdep_unlinked, inodedep, id_unlinked); 9444 return; 9445 } 9446 9447 /* 9448 * This workitem decrements the inode's link count. 9449 * If the link count reaches zero, the file is removed. 9450 */ 9451 static int 9452 handle_workitem_remove(dirrem, flags) 9453 struct dirrem *dirrem; 9454 int flags; 9455 { 9456 struct inodedep *inodedep; 9457 struct workhead dotdotwk; 9458 struct worklist *wk; 9459 struct ufsmount *ump; 9460 struct mount *mp; 9461 struct vnode *vp; 9462 struct inode *ip; 9463 ino_t oldinum; 9464 9465 if (dirrem->dm_state & ONWORKLIST) 9466 panic("handle_workitem_remove: dirrem %p still on worklist", 9467 dirrem); 9468 oldinum = dirrem->dm_oldinum; 9469 mp = dirrem->dm_list.wk_mp; 9470 ump = VFSTOUFS(mp); 9471 flags |= LK_EXCLUSIVE; 9472 if (ffs_vgetf(mp, oldinum, flags, &vp, FFSV_FORCEINSMQ) != 0) 9473 return (EBUSY); 9474 ip = VTOI(vp); 9475 ACQUIRE_LOCK(&lk); 9476 if ((inodedep_lookup(mp, oldinum, 0, &inodedep)) == 0) 9477 panic("handle_workitem_remove: lost inodedep"); 9478 if (dirrem->dm_state & ONDEPLIST) 9479 LIST_REMOVE(dirrem, dm_inonext); 9480 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 9481 ("handle_workitem_remove: Journal entries not written.")); 9482 9483 /* 9484 * Move all dependencies waiting on the remove to complete 9485 * from the dirrem to the inode inowait list to be completed 9486 * after the inode has been updated and written to disk. Any 9487 * marked MKDIR_PARENT are saved to be completed when the .. ref 9488 * is removed. 9489 */ 9490 LIST_INIT(&dotdotwk); 9491 while ((wk = LIST_FIRST(&dirrem->dm_jwork)) != NULL) { 9492 WORKLIST_REMOVE(wk); 9493 if (wk->wk_state & MKDIR_PARENT) { 9494 wk->wk_state &= ~MKDIR_PARENT; 9495 WORKLIST_INSERT(&dotdotwk, wk); 9496 continue; 9497 } 9498 WORKLIST_INSERT(&inodedep->id_inowait, wk); 9499 } 9500 LIST_SWAP(&dirrem->dm_jwork, &dotdotwk, worklist, wk_list); 9501 /* 9502 * Normal file deletion. 9503 */ 9504 if ((dirrem->dm_state & RMDIR) == 0) { 9505 ip->i_nlink--; 9506 DIP_SET(ip, i_nlink, ip->i_nlink); 9507 ip->i_flag |= IN_CHANGE; 9508 if (ip->i_nlink < ip->i_effnlink) 9509 panic("handle_workitem_remove: bad file delta"); 9510 if (ip->i_nlink == 0) 9511 unlinked_inodedep(mp, inodedep); 9512 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9513 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9514 ("handle_workitem_remove: worklist not empty. %s", 9515 TYPENAME(LIST_FIRST(&dirrem->dm_jwork)->wk_type))); 9516 WORKITEM_FREE(dirrem, D_DIRREM); 9517 FREE_LOCK(&lk); 9518 goto out; 9519 } 9520 /* 9521 * Directory deletion. Decrement reference count for both the 9522 * just deleted parent directory entry and the reference for ".". 9523 * Arrange to have the reference count on the parent decremented 9524 * to account for the loss of "..". 9525 */ 9526 ip->i_nlink -= 2; 9527 DIP_SET(ip, i_nlink, ip->i_nlink); 9528 ip->i_flag |= IN_CHANGE; 9529 if (ip->i_nlink < ip->i_effnlink) 9530 panic("handle_workitem_remove: bad dir delta"); 9531 if (ip->i_nlink == 0) 9532 unlinked_inodedep(mp, inodedep); 9533 inodedep->id_nlinkdelta = ip->i_nlink - ip->i_effnlink; 9534 /* 9535 * Rename a directory to a new parent. Since, we are both deleting 9536 * and creating a new directory entry, the link count on the new 9537 * directory should not change. Thus we skip the followup dirrem. 9538 */ 9539 if (dirrem->dm_state & DIRCHG) { 9540 KASSERT(LIST_EMPTY(&dirrem->dm_jwork), 9541 ("handle_workitem_remove: DIRCHG and worklist not empty.")); 9542 WORKITEM_FREE(dirrem, D_DIRREM); 9543 FREE_LOCK(&lk); 9544 goto out; 9545 } 9546 dirrem->dm_state = ONDEPLIST; 9547 dirrem->dm_oldinum = dirrem->dm_dirinum; 9548 /* 9549 * Place the dirrem on the parent's diremhd list. 9550 */ 9551 if (inodedep_lookup(mp, dirrem->dm_oldinum, 0, &inodedep) == 0) 9552 panic("handle_workitem_remove: lost dir inodedep"); 9553 LIST_INSERT_HEAD(&inodedep->id_dirremhd, dirrem, dm_inonext); 9554 /* 9555 * If the allocated inode has never been written to disk, then 9556 * the on-disk inode is zero'ed and we can remove the file 9557 * immediately. When journaling if the inode has been marked 9558 * unlinked and not DEPCOMPLETE we know it can never be written. 9559 */ 9560 inodedep_lookup(mp, oldinum, 0, &inodedep); 9561 if (inodedep == NULL || 9562 (inodedep->id_state & (DEPCOMPLETE | UNLINKED)) == UNLINKED || 9563 check_inode_unwritten(inodedep)) { 9564 FREE_LOCK(&lk); 9565 vput(vp); 9566 return handle_workitem_remove(dirrem, flags); 9567 } 9568 WORKLIST_INSERT(&inodedep->id_inowait, &dirrem->dm_list); 9569 FREE_LOCK(&lk); 9570 ip->i_flag |= IN_CHANGE; 9571 out: 9572 ffs_update(vp, 0); 9573 vput(vp); 9574 return (0); 9575 } 9576 9577 /* 9578 * Inode de-allocation dependencies. 9579 * 9580 * When an inode's link count is reduced to zero, it can be de-allocated. We 9581 * found it convenient to postpone de-allocation until after the inode is 9582 * written to disk with its new link count (zero). At this point, all of the 9583 * on-disk inode's block pointers are nullified and, with careful dependency 9584 * list ordering, all dependencies related to the inode will be satisfied and 9585 * the corresponding dependency structures de-allocated. So, if/when the 9586 * inode is reused, there will be no mixing of old dependencies with new 9587 * ones. This artificial dependency is set up by the block de-allocation 9588 * procedure above (softdep_setup_freeblocks) and completed by the 9589 * following procedure. 9590 */ 9591 static void 9592 handle_workitem_freefile(freefile) 9593 struct freefile *freefile; 9594 { 9595 struct workhead wkhd; 9596 struct fs *fs; 9597 struct inodedep *idp; 9598 struct ufsmount *ump; 9599 int error; 9600 9601 ump = VFSTOUFS(freefile->fx_list.wk_mp); 9602 fs = ump->um_fs; 9603 #ifdef DEBUG 9604 ACQUIRE_LOCK(&lk); 9605 error = inodedep_lookup(UFSTOVFS(ump), freefile->fx_oldinum, 0, &idp); 9606 FREE_LOCK(&lk); 9607 if (error) 9608 panic("handle_workitem_freefile: inodedep %p survived", idp); 9609 #endif 9610 UFS_LOCK(ump); 9611 fs->fs_pendinginodes -= 1; 9612 UFS_UNLOCK(ump); 9613 LIST_INIT(&wkhd); 9614 LIST_SWAP(&freefile->fx_jwork, &wkhd, worklist, wk_list); 9615 if ((error = ffs_freefile(ump, fs, freefile->fx_devvp, 9616 freefile->fx_oldinum, freefile->fx_mode, &wkhd)) != 0) 9617 softdep_error("handle_workitem_freefile", error); 9618 ACQUIRE_LOCK(&lk); 9619 WORKITEM_FREE(freefile, D_FREEFILE); 9620 FREE_LOCK(&lk); 9621 } 9622 9623 9624 /* 9625 * Helper function which unlinks marker element from work list and returns 9626 * the next element on the list. 9627 */ 9628 static __inline struct worklist * 9629 markernext(struct worklist *marker) 9630 { 9631 struct worklist *next; 9632 9633 next = LIST_NEXT(marker, wk_list); 9634 LIST_REMOVE(marker, wk_list); 9635 return next; 9636 } 9637 9638 /* 9639 * Disk writes. 9640 * 9641 * The dependency structures constructed above are most actively used when file 9642 * system blocks are written to disk. No constraints are placed on when a 9643 * block can be written, but unsatisfied update dependencies are made safe by 9644 * modifying (or replacing) the source memory for the duration of the disk 9645 * write. When the disk write completes, the memory block is again brought 9646 * up-to-date. 9647 * 9648 * In-core inode structure reclamation. 9649 * 9650 * Because there are a finite number of "in-core" inode structures, they are 9651 * reused regularly. By transferring all inode-related dependencies to the 9652 * in-memory inode block and indexing them separately (via "inodedep"s), we 9653 * can allow "in-core" inode structures to be reused at any time and avoid 9654 * any increase in contention. 9655 * 9656 * Called just before entering the device driver to initiate a new disk I/O. 9657 * The buffer must be locked, thus, no I/O completion operations can occur 9658 * while we are manipulating its associated dependencies. 9659 */ 9660 static void 9661 softdep_disk_io_initiation(bp) 9662 struct buf *bp; /* structure describing disk write to occur */ 9663 { 9664 struct worklist *wk; 9665 struct worklist marker; 9666 struct inodedep *inodedep; 9667 struct freeblks *freeblks; 9668 struct jblkdep *jblkdep; 9669 struct newblk *newblk; 9670 9671 /* 9672 * We only care about write operations. There should never 9673 * be dependencies for reads. 9674 */ 9675 if (bp->b_iocmd != BIO_WRITE) 9676 panic("softdep_disk_io_initiation: not write"); 9677 9678 if (bp->b_vflags & BV_BKGRDINPROG) 9679 panic("softdep_disk_io_initiation: Writing buffer with " 9680 "background write in progress: %p", bp); 9681 9682 marker.wk_type = D_LAST + 1; /* Not a normal workitem */ 9683 PHOLD(curproc); /* Don't swap out kernel stack */ 9684 9685 ACQUIRE_LOCK(&lk); 9686 /* 9687 * Do any necessary pre-I/O processing. 9688 */ 9689 for (wk = LIST_FIRST(&bp->b_dep); wk != NULL; 9690 wk = markernext(&marker)) { 9691 LIST_INSERT_AFTER(wk, &marker, wk_list); 9692 switch (wk->wk_type) { 9693 9694 case D_PAGEDEP: 9695 initiate_write_filepage(WK_PAGEDEP(wk), bp); 9696 continue; 9697 9698 case D_INODEDEP: 9699 inodedep = WK_INODEDEP(wk); 9700 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) 9701 initiate_write_inodeblock_ufs1(inodedep, bp); 9702 else 9703 initiate_write_inodeblock_ufs2(inodedep, bp); 9704 continue; 9705 9706 case D_INDIRDEP: 9707 initiate_write_indirdep(WK_INDIRDEP(wk), bp); 9708 continue; 9709 9710 case D_BMSAFEMAP: 9711 initiate_write_bmsafemap(WK_BMSAFEMAP(wk), bp); 9712 continue; 9713 9714 case D_JSEG: 9715 WK_JSEG(wk)->js_buf = NULL; 9716 continue; 9717 9718 case D_FREEBLKS: 9719 freeblks = WK_FREEBLKS(wk); 9720 jblkdep = LIST_FIRST(&freeblks->fb_jblkdephd); 9721 /* 9722 * We have to wait for the freeblks to be journaled 9723 * before we can write an inodeblock with updated 9724 * pointers. Be careful to arrange the marker so 9725 * we revisit the freeblks if it's not removed by 9726 * the first jwait(). 9727 */ 9728 if (jblkdep != NULL) { 9729 LIST_REMOVE(&marker, wk_list); 9730 LIST_INSERT_BEFORE(wk, &marker, wk_list); 9731 jwait(&jblkdep->jb_list, MNT_WAIT); 9732 } 9733 continue; 9734 case D_ALLOCDIRECT: 9735 case D_ALLOCINDIR: 9736 /* 9737 * We have to wait for the jnewblk to be journaled 9738 * before we can write to a block if the contents 9739 * may be confused with an earlier file's indirect 9740 * at recovery time. Handle the marker as described 9741 * above. 9742 */ 9743 newblk = WK_NEWBLK(wk); 9744 if (newblk->nb_jnewblk != NULL && 9745 indirblk_lookup(newblk->nb_list.wk_mp, 9746 newblk->nb_newblkno)) { 9747 LIST_REMOVE(&marker, wk_list); 9748 LIST_INSERT_BEFORE(wk, &marker, wk_list); 9749 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 9750 } 9751 continue; 9752 9753 case D_SBDEP: 9754 initiate_write_sbdep(WK_SBDEP(wk)); 9755 continue; 9756 9757 case D_MKDIR: 9758 case D_FREEWORK: 9759 case D_FREEDEP: 9760 case D_JSEGDEP: 9761 continue; 9762 9763 default: 9764 panic("handle_disk_io_initiation: Unexpected type %s", 9765 TYPENAME(wk->wk_type)); 9766 /* NOTREACHED */ 9767 } 9768 } 9769 FREE_LOCK(&lk); 9770 PRELE(curproc); /* Allow swapout of kernel stack */ 9771 } 9772 9773 /* 9774 * Called from within the procedure above to deal with unsatisfied 9775 * allocation dependencies in a directory. The buffer must be locked, 9776 * thus, no I/O completion operations can occur while we are 9777 * manipulating its associated dependencies. 9778 */ 9779 static void 9780 initiate_write_filepage(pagedep, bp) 9781 struct pagedep *pagedep; 9782 struct buf *bp; 9783 { 9784 struct jremref *jremref; 9785 struct jmvref *jmvref; 9786 struct dirrem *dirrem; 9787 struct diradd *dap; 9788 struct direct *ep; 9789 int i; 9790 9791 if (pagedep->pd_state & IOSTARTED) { 9792 /* 9793 * This can only happen if there is a driver that does not 9794 * understand chaining. Here biodone will reissue the call 9795 * to strategy for the incomplete buffers. 9796 */ 9797 printf("initiate_write_filepage: already started\n"); 9798 return; 9799 } 9800 pagedep->pd_state |= IOSTARTED; 9801 /* 9802 * Wait for all journal remove dependencies to hit the disk. 9803 * We can not allow any potentially conflicting directory adds 9804 * to be visible before removes and rollback is too difficult. 9805 * lk may be dropped and re-acquired, however we hold the buf 9806 * locked so the dependency can not go away. 9807 */ 9808 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) 9809 while ((jremref = LIST_FIRST(&dirrem->dm_jremrefhd)) != NULL) 9810 jwait(&jremref->jr_list, MNT_WAIT); 9811 while ((jmvref = LIST_FIRST(&pagedep->pd_jmvrefhd)) != NULL) 9812 jwait(&jmvref->jm_list, MNT_WAIT); 9813 for (i = 0; i < DAHASHSZ; i++) { 9814 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 9815 ep = (struct direct *) 9816 ((char *)bp->b_data + dap->da_offset); 9817 if (ep->d_ino != dap->da_newinum) 9818 panic("%s: dir inum %ju != new %ju", 9819 "initiate_write_filepage", 9820 (uintmax_t)ep->d_ino, 9821 (uintmax_t)dap->da_newinum); 9822 if (dap->da_state & DIRCHG) 9823 ep->d_ino = dap->da_previous->dm_oldinum; 9824 else 9825 ep->d_ino = 0; 9826 dap->da_state &= ~ATTACHED; 9827 dap->da_state |= UNDONE; 9828 } 9829 } 9830 } 9831 9832 /* 9833 * Version of initiate_write_inodeblock that handles UFS1 dinodes. 9834 * Note that any bug fixes made to this routine must be done in the 9835 * version found below. 9836 * 9837 * Called from within the procedure above to deal with unsatisfied 9838 * allocation dependencies in an inodeblock. The buffer must be 9839 * locked, thus, no I/O completion operations can occur while we 9840 * are manipulating its associated dependencies. 9841 */ 9842 static void 9843 initiate_write_inodeblock_ufs1(inodedep, bp) 9844 struct inodedep *inodedep; 9845 struct buf *bp; /* The inode block */ 9846 { 9847 struct allocdirect *adp, *lastadp; 9848 struct ufs1_dinode *dp; 9849 struct ufs1_dinode *sip; 9850 struct inoref *inoref; 9851 struct fs *fs; 9852 ufs_lbn_t i; 9853 #ifdef INVARIANTS 9854 ufs_lbn_t prevlbn = 0; 9855 #endif 9856 int deplist; 9857 9858 if (inodedep->id_state & IOSTARTED) 9859 panic("initiate_write_inodeblock_ufs1: already started"); 9860 inodedep->id_state |= IOSTARTED; 9861 fs = inodedep->id_fs; 9862 dp = (struct ufs1_dinode *)bp->b_data + 9863 ino_to_fsbo(fs, inodedep->id_ino); 9864 9865 /* 9866 * If we're on the unlinked list but have not yet written our 9867 * next pointer initialize it here. 9868 */ 9869 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 9870 struct inodedep *inon; 9871 9872 inon = TAILQ_NEXT(inodedep, id_unlinked); 9873 dp->di_freelink = inon ? inon->id_ino : 0; 9874 } 9875 /* 9876 * If the bitmap is not yet written, then the allocated 9877 * inode cannot be written to disk. 9878 */ 9879 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 9880 if (inodedep->id_savedino1 != NULL) 9881 panic("initiate_write_inodeblock_ufs1: I/O underway"); 9882 FREE_LOCK(&lk); 9883 sip = malloc(sizeof(struct ufs1_dinode), 9884 M_SAVEDINO, M_SOFTDEP_FLAGS); 9885 ACQUIRE_LOCK(&lk); 9886 inodedep->id_savedino1 = sip; 9887 *inodedep->id_savedino1 = *dp; 9888 bzero((caddr_t)dp, sizeof(struct ufs1_dinode)); 9889 dp->di_gen = inodedep->id_savedino1->di_gen; 9890 dp->di_freelink = inodedep->id_savedino1->di_freelink; 9891 return; 9892 } 9893 /* 9894 * If no dependencies, then there is nothing to roll back. 9895 */ 9896 inodedep->id_savedsize = dp->di_size; 9897 inodedep->id_savedextsize = 0; 9898 inodedep->id_savednlink = dp->di_nlink; 9899 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 9900 TAILQ_EMPTY(&inodedep->id_inoreflst)) 9901 return; 9902 /* 9903 * Revert the link count to that of the first unwritten journal entry. 9904 */ 9905 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 9906 if (inoref) 9907 dp->di_nlink = inoref->if_nlink; 9908 /* 9909 * Set the dependencies to busy. 9910 */ 9911 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 9912 adp = TAILQ_NEXT(adp, ad_next)) { 9913 #ifdef INVARIANTS 9914 if (deplist != 0 && prevlbn >= adp->ad_offset) 9915 panic("softdep_write_inodeblock: lbn order"); 9916 prevlbn = adp->ad_offset; 9917 if (adp->ad_offset < NDADDR && 9918 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 9919 panic("%s: direct pointer #%jd mismatch %d != %jd", 9920 "softdep_write_inodeblock", 9921 (intmax_t)adp->ad_offset, 9922 dp->di_db[adp->ad_offset], 9923 (intmax_t)adp->ad_newblkno); 9924 if (adp->ad_offset >= NDADDR && 9925 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 9926 panic("%s: indirect pointer #%jd mismatch %d != %jd", 9927 "softdep_write_inodeblock", 9928 (intmax_t)adp->ad_offset - NDADDR, 9929 dp->di_ib[adp->ad_offset - NDADDR], 9930 (intmax_t)adp->ad_newblkno); 9931 deplist |= 1 << adp->ad_offset; 9932 if ((adp->ad_state & ATTACHED) == 0) 9933 panic("softdep_write_inodeblock: Unknown state 0x%x", 9934 adp->ad_state); 9935 #endif /* INVARIANTS */ 9936 adp->ad_state &= ~ATTACHED; 9937 adp->ad_state |= UNDONE; 9938 } 9939 /* 9940 * The on-disk inode cannot claim to be any larger than the last 9941 * fragment that has been written. Otherwise, the on-disk inode 9942 * might have fragments that were not the last block in the file 9943 * which would corrupt the filesystem. 9944 */ 9945 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 9946 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 9947 if (adp->ad_offset >= NDADDR) 9948 break; 9949 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 9950 /* keep going until hitting a rollback to a frag */ 9951 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 9952 continue; 9953 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 9954 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 9955 #ifdef INVARIANTS 9956 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 9957 panic("softdep_write_inodeblock: lost dep1"); 9958 #endif /* INVARIANTS */ 9959 dp->di_db[i] = 0; 9960 } 9961 for (i = 0; i < NIADDR; i++) { 9962 #ifdef INVARIANTS 9963 if (dp->di_ib[i] != 0 && 9964 (deplist & ((1 << NDADDR) << i)) == 0) 9965 panic("softdep_write_inodeblock: lost dep2"); 9966 #endif /* INVARIANTS */ 9967 dp->di_ib[i] = 0; 9968 } 9969 return; 9970 } 9971 /* 9972 * If we have zero'ed out the last allocated block of the file, 9973 * roll back the size to the last currently allocated block. 9974 * We know that this last allocated block is a full-sized as 9975 * we already checked for fragments in the loop above. 9976 */ 9977 if (lastadp != NULL && 9978 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 9979 for (i = lastadp->ad_offset; i >= 0; i--) 9980 if (dp->di_db[i] != 0) 9981 break; 9982 dp->di_size = (i + 1) * fs->fs_bsize; 9983 } 9984 /* 9985 * The only dependencies are for indirect blocks. 9986 * 9987 * The file size for indirect block additions is not guaranteed. 9988 * Such a guarantee would be non-trivial to achieve. The conventional 9989 * synchronous write implementation also does not make this guarantee. 9990 * Fsck should catch and fix discrepancies. Arguably, the file size 9991 * can be over-estimated without destroying integrity when the file 9992 * moves into the indirect blocks (i.e., is large). If we want to 9993 * postpone fsck, we are stuck with this argument. 9994 */ 9995 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 9996 dp->di_ib[adp->ad_offset - NDADDR] = 0; 9997 } 9998 9999 /* 10000 * Version of initiate_write_inodeblock that handles UFS2 dinodes. 10001 * Note that any bug fixes made to this routine must be done in the 10002 * version found above. 10003 * 10004 * Called from within the procedure above to deal with unsatisfied 10005 * allocation dependencies in an inodeblock. The buffer must be 10006 * locked, thus, no I/O completion operations can occur while we 10007 * are manipulating its associated dependencies. 10008 */ 10009 static void 10010 initiate_write_inodeblock_ufs2(inodedep, bp) 10011 struct inodedep *inodedep; 10012 struct buf *bp; /* The inode block */ 10013 { 10014 struct allocdirect *adp, *lastadp; 10015 struct ufs2_dinode *dp; 10016 struct ufs2_dinode *sip; 10017 struct inoref *inoref; 10018 struct fs *fs; 10019 ufs_lbn_t i; 10020 #ifdef INVARIANTS 10021 ufs_lbn_t prevlbn = 0; 10022 #endif 10023 int deplist; 10024 10025 if (inodedep->id_state & IOSTARTED) 10026 panic("initiate_write_inodeblock_ufs2: already started"); 10027 inodedep->id_state |= IOSTARTED; 10028 fs = inodedep->id_fs; 10029 dp = (struct ufs2_dinode *)bp->b_data + 10030 ino_to_fsbo(fs, inodedep->id_ino); 10031 10032 /* 10033 * If we're on the unlinked list but have not yet written our 10034 * next pointer initialize it here. 10035 */ 10036 if ((inodedep->id_state & (UNLINKED | UNLINKNEXT)) == UNLINKED) { 10037 struct inodedep *inon; 10038 10039 inon = TAILQ_NEXT(inodedep, id_unlinked); 10040 dp->di_freelink = inon ? inon->id_ino : 0; 10041 } 10042 /* 10043 * If the bitmap is not yet written, then the allocated 10044 * inode cannot be written to disk. 10045 */ 10046 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 10047 if (inodedep->id_savedino2 != NULL) 10048 panic("initiate_write_inodeblock_ufs2: I/O underway"); 10049 FREE_LOCK(&lk); 10050 sip = malloc(sizeof(struct ufs2_dinode), 10051 M_SAVEDINO, M_SOFTDEP_FLAGS); 10052 ACQUIRE_LOCK(&lk); 10053 inodedep->id_savedino2 = sip; 10054 *inodedep->id_savedino2 = *dp; 10055 bzero((caddr_t)dp, sizeof(struct ufs2_dinode)); 10056 dp->di_gen = inodedep->id_savedino2->di_gen; 10057 dp->di_freelink = inodedep->id_savedino2->di_freelink; 10058 return; 10059 } 10060 /* 10061 * If no dependencies, then there is nothing to roll back. 10062 */ 10063 inodedep->id_savedsize = dp->di_size; 10064 inodedep->id_savedextsize = dp->di_extsize; 10065 inodedep->id_savednlink = dp->di_nlink; 10066 if (TAILQ_EMPTY(&inodedep->id_inoupdt) && 10067 TAILQ_EMPTY(&inodedep->id_extupdt) && 10068 TAILQ_EMPTY(&inodedep->id_inoreflst)) 10069 return; 10070 /* 10071 * Revert the link count to that of the first unwritten journal entry. 10072 */ 10073 inoref = TAILQ_FIRST(&inodedep->id_inoreflst); 10074 if (inoref) 10075 dp->di_nlink = inoref->if_nlink; 10076 10077 /* 10078 * Set the ext data dependencies to busy. 10079 */ 10080 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10081 adp = TAILQ_NEXT(adp, ad_next)) { 10082 #ifdef INVARIANTS 10083 if (deplist != 0 && prevlbn >= adp->ad_offset) 10084 panic("softdep_write_inodeblock: lbn order"); 10085 prevlbn = adp->ad_offset; 10086 if (dp->di_extb[adp->ad_offset] != adp->ad_newblkno) 10087 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10088 "softdep_write_inodeblock", 10089 (intmax_t)adp->ad_offset, 10090 (intmax_t)dp->di_extb[adp->ad_offset], 10091 (intmax_t)adp->ad_newblkno); 10092 deplist |= 1 << adp->ad_offset; 10093 if ((adp->ad_state & ATTACHED) == 0) 10094 panic("softdep_write_inodeblock: Unknown state 0x%x", 10095 adp->ad_state); 10096 #endif /* INVARIANTS */ 10097 adp->ad_state &= ~ATTACHED; 10098 adp->ad_state |= UNDONE; 10099 } 10100 /* 10101 * The on-disk inode cannot claim to be any larger than the last 10102 * fragment that has been written. Otherwise, the on-disk inode 10103 * might have fragments that were not the last block in the ext 10104 * data which would corrupt the filesystem. 10105 */ 10106 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; 10107 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10108 dp->di_extb[adp->ad_offset] = adp->ad_oldblkno; 10109 /* keep going until hitting a rollback to a frag */ 10110 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10111 continue; 10112 dp->di_extsize = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10113 for (i = adp->ad_offset + 1; i < NXADDR; i++) { 10114 #ifdef INVARIANTS 10115 if (dp->di_extb[i] != 0 && (deplist & (1 << i)) == 0) 10116 panic("softdep_write_inodeblock: lost dep1"); 10117 #endif /* INVARIANTS */ 10118 dp->di_extb[i] = 0; 10119 } 10120 lastadp = NULL; 10121 break; 10122 } 10123 /* 10124 * If we have zero'ed out the last allocated block of the ext 10125 * data, roll back the size to the last currently allocated block. 10126 * We know that this last allocated block is a full-sized as 10127 * we already checked for fragments in the loop above. 10128 */ 10129 if (lastadp != NULL && 10130 dp->di_extsize <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10131 for (i = lastadp->ad_offset; i >= 0; i--) 10132 if (dp->di_extb[i] != 0) 10133 break; 10134 dp->di_extsize = (i + 1) * fs->fs_bsize; 10135 } 10136 /* 10137 * Set the file data dependencies to busy. 10138 */ 10139 for (deplist = 0, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10140 adp = TAILQ_NEXT(adp, ad_next)) { 10141 #ifdef INVARIANTS 10142 if (deplist != 0 && prevlbn >= adp->ad_offset) 10143 panic("softdep_write_inodeblock: lbn order"); 10144 if ((adp->ad_state & ATTACHED) == 0) 10145 panic("inodedep %p and adp %p not attached", inodedep, adp); 10146 prevlbn = adp->ad_offset; 10147 if (adp->ad_offset < NDADDR && 10148 dp->di_db[adp->ad_offset] != adp->ad_newblkno) 10149 panic("%s: direct pointer #%jd mismatch %jd != %jd", 10150 "softdep_write_inodeblock", 10151 (intmax_t)adp->ad_offset, 10152 (intmax_t)dp->di_db[adp->ad_offset], 10153 (intmax_t)adp->ad_newblkno); 10154 if (adp->ad_offset >= NDADDR && 10155 dp->di_ib[adp->ad_offset - NDADDR] != adp->ad_newblkno) 10156 panic("%s indirect pointer #%jd mismatch %jd != %jd", 10157 "softdep_write_inodeblock:", 10158 (intmax_t)adp->ad_offset - NDADDR, 10159 (intmax_t)dp->di_ib[adp->ad_offset - NDADDR], 10160 (intmax_t)adp->ad_newblkno); 10161 deplist |= 1 << adp->ad_offset; 10162 if ((adp->ad_state & ATTACHED) == 0) 10163 panic("softdep_write_inodeblock: Unknown state 0x%x", 10164 adp->ad_state); 10165 #endif /* INVARIANTS */ 10166 adp->ad_state &= ~ATTACHED; 10167 adp->ad_state |= UNDONE; 10168 } 10169 /* 10170 * The on-disk inode cannot claim to be any larger than the last 10171 * fragment that has been written. Otherwise, the on-disk inode 10172 * might have fragments that were not the last block in the file 10173 * which would corrupt the filesystem. 10174 */ 10175 for (lastadp = NULL, adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; 10176 lastadp = adp, adp = TAILQ_NEXT(adp, ad_next)) { 10177 if (adp->ad_offset >= NDADDR) 10178 break; 10179 dp->di_db[adp->ad_offset] = adp->ad_oldblkno; 10180 /* keep going until hitting a rollback to a frag */ 10181 if (adp->ad_oldsize == 0 || adp->ad_oldsize == fs->fs_bsize) 10182 continue; 10183 dp->di_size = fs->fs_bsize * adp->ad_offset + adp->ad_oldsize; 10184 for (i = adp->ad_offset + 1; i < NDADDR; i++) { 10185 #ifdef INVARIANTS 10186 if (dp->di_db[i] != 0 && (deplist & (1 << i)) == 0) 10187 panic("softdep_write_inodeblock: lost dep2"); 10188 #endif /* INVARIANTS */ 10189 dp->di_db[i] = 0; 10190 } 10191 for (i = 0; i < NIADDR; i++) { 10192 #ifdef INVARIANTS 10193 if (dp->di_ib[i] != 0 && 10194 (deplist & ((1 << NDADDR) << i)) == 0) 10195 panic("softdep_write_inodeblock: lost dep3"); 10196 #endif /* INVARIANTS */ 10197 dp->di_ib[i] = 0; 10198 } 10199 return; 10200 } 10201 /* 10202 * If we have zero'ed out the last allocated block of the file, 10203 * roll back the size to the last currently allocated block. 10204 * We know that this last allocated block is a full-sized as 10205 * we already checked for fragments in the loop above. 10206 */ 10207 if (lastadp != NULL && 10208 dp->di_size <= (lastadp->ad_offset + 1) * fs->fs_bsize) { 10209 for (i = lastadp->ad_offset; i >= 0; i--) 10210 if (dp->di_db[i] != 0) 10211 break; 10212 dp->di_size = (i + 1) * fs->fs_bsize; 10213 } 10214 /* 10215 * The only dependencies are for indirect blocks. 10216 * 10217 * The file size for indirect block additions is not guaranteed. 10218 * Such a guarantee would be non-trivial to achieve. The conventional 10219 * synchronous write implementation also does not make this guarantee. 10220 * Fsck should catch and fix discrepancies. Arguably, the file size 10221 * can be over-estimated without destroying integrity when the file 10222 * moves into the indirect blocks (i.e., is large). If we want to 10223 * postpone fsck, we are stuck with this argument. 10224 */ 10225 for (; adp; adp = TAILQ_NEXT(adp, ad_next)) 10226 dp->di_ib[adp->ad_offset - NDADDR] = 0; 10227 } 10228 10229 /* 10230 * Cancel an indirdep as a result of truncation. Release all of the 10231 * children allocindirs and place their journal work on the appropriate 10232 * list. 10233 */ 10234 static void 10235 cancel_indirdep(indirdep, bp, freeblks) 10236 struct indirdep *indirdep; 10237 struct buf *bp; 10238 struct freeblks *freeblks; 10239 { 10240 struct allocindir *aip; 10241 10242 /* 10243 * None of the indirect pointers will ever be visible, 10244 * so they can simply be tossed. GOINGAWAY ensures 10245 * that allocated pointers will be saved in the buffer 10246 * cache until they are freed. Note that they will 10247 * only be able to be found by their physical address 10248 * since the inode mapping the logical address will 10249 * be gone. The save buffer used for the safe copy 10250 * was allocated in setup_allocindir_phase2 using 10251 * the physical address so it could be used for this 10252 * purpose. Hence we swap the safe copy with the real 10253 * copy, allowing the safe copy to be freed and holding 10254 * on to the real copy for later use in indir_trunc. 10255 */ 10256 if (indirdep->ir_state & GOINGAWAY) 10257 panic("cancel_indirdep: already gone"); 10258 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 10259 indirdep->ir_state |= DEPCOMPLETE; 10260 LIST_REMOVE(indirdep, ir_next); 10261 } 10262 indirdep->ir_state |= GOINGAWAY; 10263 VFSTOUFS(indirdep->ir_list.wk_mp)->um_numindirdeps += 1; 10264 /* 10265 * Pass in bp for blocks still have journal writes 10266 * pending so we can cancel them on their own. 10267 */ 10268 while ((aip = LIST_FIRST(&indirdep->ir_deplisthd)) != 0) 10269 cancel_allocindir(aip, bp, freeblks, 0); 10270 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) 10271 cancel_allocindir(aip, NULL, freeblks, 0); 10272 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) 10273 cancel_allocindir(aip, NULL, freeblks, 0); 10274 while ((aip = LIST_FIRST(&indirdep->ir_completehd)) != 0) 10275 cancel_allocindir(aip, NULL, freeblks, 0); 10276 /* 10277 * If there are pending partial truncations we need to keep the 10278 * old block copy around until they complete. This is because 10279 * the current b_data is not a perfect superset of the available 10280 * blocks. 10281 */ 10282 if (TAILQ_EMPTY(&indirdep->ir_trunc)) 10283 bcopy(bp->b_data, indirdep->ir_savebp->b_data, bp->b_bcount); 10284 else 10285 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10286 WORKLIST_REMOVE(&indirdep->ir_list); 10287 WORKLIST_INSERT(&indirdep->ir_savebp->b_dep, &indirdep->ir_list); 10288 indirdep->ir_bp = NULL; 10289 indirdep->ir_freeblks = freeblks; 10290 } 10291 10292 /* 10293 * Free an indirdep once it no longer has new pointers to track. 10294 */ 10295 static void 10296 free_indirdep(indirdep) 10297 struct indirdep *indirdep; 10298 { 10299 10300 KASSERT(TAILQ_EMPTY(&indirdep->ir_trunc), 10301 ("free_indirdep: Indir trunc list not empty.")); 10302 KASSERT(LIST_EMPTY(&indirdep->ir_completehd), 10303 ("free_indirdep: Complete head not empty.")); 10304 KASSERT(LIST_EMPTY(&indirdep->ir_writehd), 10305 ("free_indirdep: write head not empty.")); 10306 KASSERT(LIST_EMPTY(&indirdep->ir_donehd), 10307 ("free_indirdep: done head not empty.")); 10308 KASSERT(LIST_EMPTY(&indirdep->ir_deplisthd), 10309 ("free_indirdep: deplist head not empty.")); 10310 KASSERT((indirdep->ir_state & DEPCOMPLETE), 10311 ("free_indirdep: %p still on newblk list.", indirdep)); 10312 KASSERT(indirdep->ir_saveddata == NULL, 10313 ("free_indirdep: %p still has saved data.", indirdep)); 10314 if (indirdep->ir_state & ONWORKLIST) 10315 WORKLIST_REMOVE(&indirdep->ir_list); 10316 WORKITEM_FREE(indirdep, D_INDIRDEP); 10317 } 10318 10319 /* 10320 * Called before a write to an indirdep. This routine is responsible for 10321 * rolling back pointers to a safe state which includes only those 10322 * allocindirs which have been completed. 10323 */ 10324 static void 10325 initiate_write_indirdep(indirdep, bp) 10326 struct indirdep *indirdep; 10327 struct buf *bp; 10328 { 10329 10330 indirdep->ir_state |= IOSTARTED; 10331 if (indirdep->ir_state & GOINGAWAY) 10332 panic("disk_io_initiation: indirdep gone"); 10333 /* 10334 * If there are no remaining dependencies, this will be writing 10335 * the real pointers. 10336 */ 10337 if (LIST_EMPTY(&indirdep->ir_deplisthd) && 10338 TAILQ_EMPTY(&indirdep->ir_trunc)) 10339 return; 10340 /* 10341 * Replace up-to-date version with safe version. 10342 */ 10343 if (indirdep->ir_saveddata == NULL) { 10344 FREE_LOCK(&lk); 10345 indirdep->ir_saveddata = malloc(bp->b_bcount, M_INDIRDEP, 10346 M_SOFTDEP_FLAGS); 10347 ACQUIRE_LOCK(&lk); 10348 } 10349 indirdep->ir_state &= ~ATTACHED; 10350 indirdep->ir_state |= UNDONE; 10351 bcopy(bp->b_data, indirdep->ir_saveddata, bp->b_bcount); 10352 bcopy(indirdep->ir_savebp->b_data, bp->b_data, 10353 bp->b_bcount); 10354 } 10355 10356 /* 10357 * Called when an inode has been cleared in a cg bitmap. This finally 10358 * eliminates any canceled jaddrefs 10359 */ 10360 void 10361 softdep_setup_inofree(mp, bp, ino, wkhd) 10362 struct mount *mp; 10363 struct buf *bp; 10364 ino_t ino; 10365 struct workhead *wkhd; 10366 { 10367 struct worklist *wk, *wkn; 10368 struct inodedep *inodedep; 10369 uint8_t *inosused; 10370 struct cg *cgp; 10371 struct fs *fs; 10372 10373 ACQUIRE_LOCK(&lk); 10374 fs = VFSTOUFS(mp)->um_fs; 10375 cgp = (struct cg *)bp->b_data; 10376 inosused = cg_inosused(cgp); 10377 if (isset(inosused, ino % fs->fs_ipg)) 10378 panic("softdep_setup_inofree: inode %ju not freed.", 10379 (uintmax_t)ino); 10380 if (inodedep_lookup(mp, ino, 0, &inodedep)) 10381 panic("softdep_setup_inofree: ino %ju has existing inodedep %p", 10382 (uintmax_t)ino, inodedep); 10383 if (wkhd) { 10384 LIST_FOREACH_SAFE(wk, wkhd, wk_list, wkn) { 10385 if (wk->wk_type != D_JADDREF) 10386 continue; 10387 WORKLIST_REMOVE(wk); 10388 /* 10389 * We can free immediately even if the jaddref 10390 * isn't attached in a background write as now 10391 * the bitmaps are reconciled. 10392 */ 10393 wk->wk_state |= COMPLETE | ATTACHED; 10394 free_jaddref(WK_JADDREF(wk)); 10395 } 10396 jwork_move(&bp->b_dep, wkhd); 10397 } 10398 FREE_LOCK(&lk); 10399 } 10400 10401 10402 /* 10403 * Called via ffs_blkfree() after a set of frags has been cleared from a cg 10404 * map. Any dependencies waiting for the write to clear are added to the 10405 * buf's list and any jnewblks that are being canceled are discarded 10406 * immediately. 10407 */ 10408 void 10409 softdep_setup_blkfree(mp, bp, blkno, frags, wkhd) 10410 struct mount *mp; 10411 struct buf *bp; 10412 ufs2_daddr_t blkno; 10413 int frags; 10414 struct workhead *wkhd; 10415 { 10416 struct bmsafemap *bmsafemap; 10417 struct jnewblk *jnewblk; 10418 struct worklist *wk; 10419 struct fs *fs; 10420 #ifdef SUJ_DEBUG 10421 uint8_t *blksfree; 10422 struct cg *cgp; 10423 ufs2_daddr_t jstart; 10424 ufs2_daddr_t jend; 10425 ufs2_daddr_t end; 10426 long bno; 10427 int i; 10428 #endif 10429 10430 CTR3(KTR_SUJ, 10431 "softdep_setup_blkfree: blkno %jd frags %d wk head %p", 10432 blkno, frags, wkhd); 10433 10434 ACQUIRE_LOCK(&lk); 10435 /* Lookup the bmsafemap so we track when it is dirty. */ 10436 fs = VFSTOUFS(mp)->um_fs; 10437 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10438 /* 10439 * Detach any jnewblks which have been canceled. They must linger 10440 * until the bitmap is cleared again by ffs_blkfree() to prevent 10441 * an unjournaled allocation from hitting the disk. 10442 */ 10443 if (wkhd) { 10444 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10445 CTR2(KTR_SUJ, 10446 "softdep_setup_blkfree: blkno %jd wk type %d", 10447 blkno, wk->wk_type); 10448 WORKLIST_REMOVE(wk); 10449 if (wk->wk_type != D_JNEWBLK) { 10450 WORKLIST_INSERT(&bmsafemap->sm_freehd, wk); 10451 continue; 10452 } 10453 jnewblk = WK_JNEWBLK(wk); 10454 KASSERT(jnewblk->jn_state & GOINGAWAY, 10455 ("softdep_setup_blkfree: jnewblk not canceled.")); 10456 #ifdef SUJ_DEBUG 10457 /* 10458 * Assert that this block is free in the bitmap 10459 * before we discard the jnewblk. 10460 */ 10461 cgp = (struct cg *)bp->b_data; 10462 blksfree = cg_blksfree(cgp); 10463 bno = dtogd(fs, jnewblk->jn_blkno); 10464 for (i = jnewblk->jn_oldfrags; 10465 i < jnewblk->jn_frags; i++) { 10466 if (isset(blksfree, bno + i)) 10467 continue; 10468 panic("softdep_setup_blkfree: not free"); 10469 } 10470 #endif 10471 /* 10472 * Even if it's not attached we can free immediately 10473 * as the new bitmap is correct. 10474 */ 10475 wk->wk_state |= COMPLETE | ATTACHED; 10476 free_jnewblk(jnewblk); 10477 } 10478 } 10479 10480 #ifdef SUJ_DEBUG 10481 /* 10482 * Assert that we are not freeing a block which has an outstanding 10483 * allocation dependency. 10484 */ 10485 fs = VFSTOUFS(mp)->um_fs; 10486 bmsafemap = bmsafemap_lookup(mp, bp, dtog(fs, blkno), NULL); 10487 end = blkno + frags; 10488 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10489 /* 10490 * Don't match against blocks that will be freed when the 10491 * background write is done. 10492 */ 10493 if ((jnewblk->jn_state & (ATTACHED | COMPLETE | DEPCOMPLETE)) == 10494 (COMPLETE | DEPCOMPLETE)) 10495 continue; 10496 jstart = jnewblk->jn_blkno + jnewblk->jn_oldfrags; 10497 jend = jnewblk->jn_blkno + jnewblk->jn_frags; 10498 if ((blkno >= jstart && blkno < jend) || 10499 (end > jstart && end <= jend)) { 10500 printf("state 0x%X %jd - %d %d dep %p\n", 10501 jnewblk->jn_state, jnewblk->jn_blkno, 10502 jnewblk->jn_oldfrags, jnewblk->jn_frags, 10503 jnewblk->jn_dep); 10504 panic("softdep_setup_blkfree: " 10505 "%jd-%jd(%d) overlaps with %jd-%jd", 10506 blkno, end, frags, jstart, jend); 10507 } 10508 } 10509 #endif 10510 FREE_LOCK(&lk); 10511 } 10512 10513 /* 10514 * Revert a block allocation when the journal record that describes it 10515 * is not yet written. 10516 */ 10517 int 10518 jnewblk_rollback(jnewblk, fs, cgp, blksfree) 10519 struct jnewblk *jnewblk; 10520 struct fs *fs; 10521 struct cg *cgp; 10522 uint8_t *blksfree; 10523 { 10524 ufs1_daddr_t fragno; 10525 long cgbno, bbase; 10526 int frags, blk; 10527 int i; 10528 10529 frags = 0; 10530 cgbno = dtogd(fs, jnewblk->jn_blkno); 10531 /* 10532 * We have to test which frags need to be rolled back. We may 10533 * be operating on a stale copy when doing background writes. 10534 */ 10535 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) 10536 if (isclr(blksfree, cgbno + i)) 10537 frags++; 10538 if (frags == 0) 10539 return (0); 10540 /* 10541 * This is mostly ffs_blkfree() sans some validation and 10542 * superblock updates. 10543 */ 10544 if (frags == fs->fs_frag) { 10545 fragno = fragstoblks(fs, cgbno); 10546 ffs_setblock(fs, blksfree, fragno); 10547 ffs_clusteracct(fs, cgp, fragno, 1); 10548 cgp->cg_cs.cs_nbfree++; 10549 } else { 10550 cgbno += jnewblk->jn_oldfrags; 10551 bbase = cgbno - fragnum(fs, cgbno); 10552 /* Decrement the old frags. */ 10553 blk = blkmap(fs, blksfree, bbase); 10554 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 10555 /* Deallocate the fragment */ 10556 for (i = 0; i < frags; i++) 10557 setbit(blksfree, cgbno + i); 10558 cgp->cg_cs.cs_nffree += frags; 10559 /* Add back in counts associated with the new frags */ 10560 blk = blkmap(fs, blksfree, bbase); 10561 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 10562 /* If a complete block has been reassembled, account for it. */ 10563 fragno = fragstoblks(fs, bbase); 10564 if (ffs_isblock(fs, blksfree, fragno)) { 10565 cgp->cg_cs.cs_nffree -= fs->fs_frag; 10566 ffs_clusteracct(fs, cgp, fragno, 1); 10567 cgp->cg_cs.cs_nbfree++; 10568 } 10569 } 10570 stat_jnewblk++; 10571 jnewblk->jn_state &= ~ATTACHED; 10572 jnewblk->jn_state |= UNDONE; 10573 10574 return (frags); 10575 } 10576 10577 static void 10578 initiate_write_bmsafemap(bmsafemap, bp) 10579 struct bmsafemap *bmsafemap; 10580 struct buf *bp; /* The cg block. */ 10581 { 10582 struct jaddref *jaddref; 10583 struct jnewblk *jnewblk; 10584 uint8_t *inosused; 10585 uint8_t *blksfree; 10586 struct cg *cgp; 10587 struct fs *fs; 10588 ino_t ino; 10589 10590 if (bmsafemap->sm_state & IOSTARTED) 10591 return; 10592 bmsafemap->sm_state |= IOSTARTED; 10593 /* 10594 * Clear any inode allocations which are pending journal writes. 10595 */ 10596 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd) != NULL) { 10597 cgp = (struct cg *)bp->b_data; 10598 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10599 inosused = cg_inosused(cgp); 10600 LIST_FOREACH(jaddref, &bmsafemap->sm_jaddrefhd, ja_bmdeps) { 10601 ino = jaddref->ja_ino % fs->fs_ipg; 10602 if (isset(inosused, ino)) { 10603 if ((jaddref->ja_mode & IFMT) == IFDIR) 10604 cgp->cg_cs.cs_ndir--; 10605 cgp->cg_cs.cs_nifree++; 10606 clrbit(inosused, ino); 10607 jaddref->ja_state &= ~ATTACHED; 10608 jaddref->ja_state |= UNDONE; 10609 stat_jaddref++; 10610 } else 10611 panic("initiate_write_bmsafemap: inode %ju " 10612 "marked free", (uintmax_t)jaddref->ja_ino); 10613 } 10614 } 10615 /* 10616 * Clear any block allocations which are pending journal writes. 10617 */ 10618 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 10619 cgp = (struct cg *)bp->b_data; 10620 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 10621 blksfree = cg_blksfree(cgp); 10622 LIST_FOREACH(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps) { 10623 if (jnewblk_rollback(jnewblk, fs, cgp, blksfree)) 10624 continue; 10625 panic("initiate_write_bmsafemap: block %jd " 10626 "marked free", jnewblk->jn_blkno); 10627 } 10628 } 10629 /* 10630 * Move allocation lists to the written lists so they can be 10631 * cleared once the block write is complete. 10632 */ 10633 LIST_SWAP(&bmsafemap->sm_inodedephd, &bmsafemap->sm_inodedepwr, 10634 inodedep, id_deps); 10635 LIST_SWAP(&bmsafemap->sm_newblkhd, &bmsafemap->sm_newblkwr, 10636 newblk, nb_deps); 10637 LIST_SWAP(&bmsafemap->sm_freehd, &bmsafemap->sm_freewr, worklist, 10638 wk_list); 10639 } 10640 10641 /* 10642 * This routine is called during the completion interrupt 10643 * service routine for a disk write (from the procedure called 10644 * by the device driver to inform the filesystem caches of 10645 * a request completion). It should be called early in this 10646 * procedure, before the block is made available to other 10647 * processes or other routines are called. 10648 * 10649 */ 10650 static void 10651 softdep_disk_write_complete(bp) 10652 struct buf *bp; /* describes the completed disk write */ 10653 { 10654 struct worklist *wk; 10655 struct worklist *owk; 10656 struct workhead reattach; 10657 struct freeblks *freeblks; 10658 struct buf *sbp; 10659 10660 /* 10661 * If an error occurred while doing the write, then the data 10662 * has not hit the disk and the dependencies cannot be unrolled. 10663 */ 10664 if ((bp->b_ioflags & BIO_ERROR) != 0 && (bp->b_flags & B_INVAL) == 0) 10665 return; 10666 LIST_INIT(&reattach); 10667 /* 10668 * This lock must not be released anywhere in this code segment. 10669 */ 10670 sbp = NULL; 10671 owk = NULL; 10672 ACQUIRE_LOCK(&lk); 10673 while ((wk = LIST_FIRST(&bp->b_dep)) != NULL) { 10674 WORKLIST_REMOVE(wk); 10675 dep_write[wk->wk_type]++; 10676 if (wk == owk) 10677 panic("duplicate worklist: %p\n", wk); 10678 owk = wk; 10679 switch (wk->wk_type) { 10680 10681 case D_PAGEDEP: 10682 if (handle_written_filepage(WK_PAGEDEP(wk), bp)) 10683 WORKLIST_INSERT(&reattach, wk); 10684 continue; 10685 10686 case D_INODEDEP: 10687 if (handle_written_inodeblock(WK_INODEDEP(wk), bp)) 10688 WORKLIST_INSERT(&reattach, wk); 10689 continue; 10690 10691 case D_BMSAFEMAP: 10692 if (handle_written_bmsafemap(WK_BMSAFEMAP(wk), bp)) 10693 WORKLIST_INSERT(&reattach, wk); 10694 continue; 10695 10696 case D_MKDIR: 10697 handle_written_mkdir(WK_MKDIR(wk), MKDIR_BODY); 10698 continue; 10699 10700 case D_ALLOCDIRECT: 10701 wk->wk_state |= COMPLETE; 10702 handle_allocdirect_partdone(WK_ALLOCDIRECT(wk), NULL); 10703 continue; 10704 10705 case D_ALLOCINDIR: 10706 wk->wk_state |= COMPLETE; 10707 handle_allocindir_partdone(WK_ALLOCINDIR(wk)); 10708 continue; 10709 10710 case D_INDIRDEP: 10711 if (handle_written_indirdep(WK_INDIRDEP(wk), bp, &sbp)) 10712 WORKLIST_INSERT(&reattach, wk); 10713 continue; 10714 10715 case D_FREEBLKS: 10716 wk->wk_state |= COMPLETE; 10717 freeblks = WK_FREEBLKS(wk); 10718 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE && 10719 LIST_EMPTY(&freeblks->fb_jblkdephd)) 10720 add_to_worklist(wk, WK_NODELAY); 10721 continue; 10722 10723 case D_FREEWORK: 10724 handle_written_freework(WK_FREEWORK(wk)); 10725 break; 10726 10727 case D_JSEGDEP: 10728 free_jsegdep(WK_JSEGDEP(wk)); 10729 continue; 10730 10731 case D_JSEG: 10732 handle_written_jseg(WK_JSEG(wk), bp); 10733 continue; 10734 10735 case D_SBDEP: 10736 if (handle_written_sbdep(WK_SBDEP(wk), bp)) 10737 WORKLIST_INSERT(&reattach, wk); 10738 continue; 10739 10740 case D_FREEDEP: 10741 free_freedep(WK_FREEDEP(wk)); 10742 continue; 10743 10744 default: 10745 panic("handle_disk_write_complete: Unknown type %s", 10746 TYPENAME(wk->wk_type)); 10747 /* NOTREACHED */ 10748 } 10749 } 10750 /* 10751 * Reattach any requests that must be redone. 10752 */ 10753 while ((wk = LIST_FIRST(&reattach)) != NULL) { 10754 WORKLIST_REMOVE(wk); 10755 WORKLIST_INSERT(&bp->b_dep, wk); 10756 } 10757 FREE_LOCK(&lk); 10758 if (sbp) 10759 brelse(sbp); 10760 } 10761 10762 /* 10763 * Called from within softdep_disk_write_complete above. Note that 10764 * this routine is always called from interrupt level with further 10765 * splbio interrupts blocked. 10766 */ 10767 static void 10768 handle_allocdirect_partdone(adp, wkhd) 10769 struct allocdirect *adp; /* the completed allocdirect */ 10770 struct workhead *wkhd; /* Work to do when inode is writtne. */ 10771 { 10772 struct allocdirectlst *listhead; 10773 struct allocdirect *listadp; 10774 struct inodedep *inodedep; 10775 long bsize; 10776 10777 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 10778 return; 10779 /* 10780 * The on-disk inode cannot claim to be any larger than the last 10781 * fragment that has been written. Otherwise, the on-disk inode 10782 * might have fragments that were not the last block in the file 10783 * which would corrupt the filesystem. Thus, we cannot free any 10784 * allocdirects after one whose ad_oldblkno claims a fragment as 10785 * these blocks must be rolled back to zero before writing the inode. 10786 * We check the currently active set of allocdirects in id_inoupdt 10787 * or id_extupdt as appropriate. 10788 */ 10789 inodedep = adp->ad_inodedep; 10790 bsize = inodedep->id_fs->fs_bsize; 10791 if (adp->ad_state & EXTDATA) 10792 listhead = &inodedep->id_extupdt; 10793 else 10794 listhead = &inodedep->id_inoupdt; 10795 TAILQ_FOREACH(listadp, listhead, ad_next) { 10796 /* found our block */ 10797 if (listadp == adp) 10798 break; 10799 /* continue if ad_oldlbn is not a fragment */ 10800 if (listadp->ad_oldsize == 0 || 10801 listadp->ad_oldsize == bsize) 10802 continue; 10803 /* hit a fragment */ 10804 return; 10805 } 10806 /* 10807 * If we have reached the end of the current list without 10808 * finding the just finished dependency, then it must be 10809 * on the future dependency list. Future dependencies cannot 10810 * be freed until they are moved to the current list. 10811 */ 10812 if (listadp == NULL) { 10813 #ifdef DEBUG 10814 if (adp->ad_state & EXTDATA) 10815 listhead = &inodedep->id_newextupdt; 10816 else 10817 listhead = &inodedep->id_newinoupdt; 10818 TAILQ_FOREACH(listadp, listhead, ad_next) 10819 /* found our block */ 10820 if (listadp == adp) 10821 break; 10822 if (listadp == NULL) 10823 panic("handle_allocdirect_partdone: lost dep"); 10824 #endif /* DEBUG */ 10825 return; 10826 } 10827 /* 10828 * If we have found the just finished dependency, then queue 10829 * it along with anything that follows it that is complete. 10830 * Since the pointer has not yet been written in the inode 10831 * as the dependency prevents it, place the allocdirect on the 10832 * bufwait list where it will be freed once the pointer is 10833 * valid. 10834 */ 10835 if (wkhd == NULL) 10836 wkhd = &inodedep->id_bufwait; 10837 for (; adp; adp = listadp) { 10838 listadp = TAILQ_NEXT(adp, ad_next); 10839 if ((adp->ad_state & ALLCOMPLETE) != ALLCOMPLETE) 10840 return; 10841 TAILQ_REMOVE(listhead, adp, ad_next); 10842 WORKLIST_INSERT(wkhd, &adp->ad_block.nb_list); 10843 } 10844 } 10845 10846 /* 10847 * Called from within softdep_disk_write_complete above. This routine 10848 * completes successfully written allocindirs. 10849 */ 10850 static void 10851 handle_allocindir_partdone(aip) 10852 struct allocindir *aip; /* the completed allocindir */ 10853 { 10854 struct indirdep *indirdep; 10855 10856 if ((aip->ai_state & ALLCOMPLETE) != ALLCOMPLETE) 10857 return; 10858 indirdep = aip->ai_indirdep; 10859 LIST_REMOVE(aip, ai_next); 10860 /* 10861 * Don't set a pointer while the buffer is undergoing IO or while 10862 * we have active truncations. 10863 */ 10864 if (indirdep->ir_state & UNDONE || !TAILQ_EMPTY(&indirdep->ir_trunc)) { 10865 LIST_INSERT_HEAD(&indirdep->ir_donehd, aip, ai_next); 10866 return; 10867 } 10868 if (indirdep->ir_state & UFS1FMT) 10869 ((ufs1_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 10870 aip->ai_newblkno; 10871 else 10872 ((ufs2_daddr_t *)indirdep->ir_savebp->b_data)[aip->ai_offset] = 10873 aip->ai_newblkno; 10874 /* 10875 * Await the pointer write before freeing the allocindir. 10876 */ 10877 LIST_INSERT_HEAD(&indirdep->ir_writehd, aip, ai_next); 10878 } 10879 10880 /* 10881 * Release segments held on a jwork list. 10882 */ 10883 static void 10884 handle_jwork(wkhd) 10885 struct workhead *wkhd; 10886 { 10887 struct worklist *wk; 10888 10889 while ((wk = LIST_FIRST(wkhd)) != NULL) { 10890 WORKLIST_REMOVE(wk); 10891 switch (wk->wk_type) { 10892 case D_JSEGDEP: 10893 free_jsegdep(WK_JSEGDEP(wk)); 10894 continue; 10895 case D_FREEDEP: 10896 free_freedep(WK_FREEDEP(wk)); 10897 continue; 10898 case D_FREEFRAG: 10899 rele_jseg(WK_JSEG(WK_FREEFRAG(wk)->ff_jdep)); 10900 WORKITEM_FREE(wk, D_FREEFRAG); 10901 continue; 10902 case D_FREEWORK: 10903 handle_written_freework(WK_FREEWORK(wk)); 10904 continue; 10905 default: 10906 panic("handle_jwork: Unknown type %s\n", 10907 TYPENAME(wk->wk_type)); 10908 } 10909 } 10910 } 10911 10912 /* 10913 * Handle the bufwait list on an inode when it is safe to release items 10914 * held there. This normally happens after an inode block is written but 10915 * may be delayed and handled later if there are pending journal items that 10916 * are not yet safe to be released. 10917 */ 10918 static struct freefile * 10919 handle_bufwait(inodedep, refhd) 10920 struct inodedep *inodedep; 10921 struct workhead *refhd; 10922 { 10923 struct jaddref *jaddref; 10924 struct freefile *freefile; 10925 struct worklist *wk; 10926 10927 freefile = NULL; 10928 while ((wk = LIST_FIRST(&inodedep->id_bufwait)) != NULL) { 10929 WORKLIST_REMOVE(wk); 10930 switch (wk->wk_type) { 10931 case D_FREEFILE: 10932 /* 10933 * We defer adding freefile to the worklist 10934 * until all other additions have been made to 10935 * ensure that it will be done after all the 10936 * old blocks have been freed. 10937 */ 10938 if (freefile != NULL) 10939 panic("handle_bufwait: freefile"); 10940 freefile = WK_FREEFILE(wk); 10941 continue; 10942 10943 case D_MKDIR: 10944 handle_written_mkdir(WK_MKDIR(wk), MKDIR_PARENT); 10945 continue; 10946 10947 case D_DIRADD: 10948 diradd_inode_written(WK_DIRADD(wk), inodedep); 10949 continue; 10950 10951 case D_FREEFRAG: 10952 wk->wk_state |= COMPLETE; 10953 if ((wk->wk_state & ALLCOMPLETE) == ALLCOMPLETE) 10954 add_to_worklist(wk, 0); 10955 continue; 10956 10957 case D_DIRREM: 10958 wk->wk_state |= COMPLETE; 10959 add_to_worklist(wk, 0); 10960 continue; 10961 10962 case D_ALLOCDIRECT: 10963 case D_ALLOCINDIR: 10964 free_newblk(WK_NEWBLK(wk)); 10965 continue; 10966 10967 case D_JNEWBLK: 10968 wk->wk_state |= COMPLETE; 10969 free_jnewblk(WK_JNEWBLK(wk)); 10970 continue; 10971 10972 /* 10973 * Save freed journal segments and add references on 10974 * the supplied list which will delay their release 10975 * until the cg bitmap is cleared on disk. 10976 */ 10977 case D_JSEGDEP: 10978 if (refhd == NULL) 10979 free_jsegdep(WK_JSEGDEP(wk)); 10980 else 10981 WORKLIST_INSERT(refhd, wk); 10982 continue; 10983 10984 case D_JADDREF: 10985 jaddref = WK_JADDREF(wk); 10986 TAILQ_REMOVE(&inodedep->id_inoreflst, &jaddref->ja_ref, 10987 if_deps); 10988 /* 10989 * Transfer any jaddrefs to the list to be freed with 10990 * the bitmap if we're handling a removed file. 10991 */ 10992 if (refhd == NULL) { 10993 wk->wk_state |= COMPLETE; 10994 free_jaddref(jaddref); 10995 } else 10996 WORKLIST_INSERT(refhd, wk); 10997 continue; 10998 10999 default: 11000 panic("handle_bufwait: Unknown type %p(%s)", 11001 wk, TYPENAME(wk->wk_type)); 11002 /* NOTREACHED */ 11003 } 11004 } 11005 return (freefile); 11006 } 11007 /* 11008 * Called from within softdep_disk_write_complete above to restore 11009 * in-memory inode block contents to their most up-to-date state. Note 11010 * that this routine is always called from interrupt level with further 11011 * splbio interrupts blocked. 11012 */ 11013 static int 11014 handle_written_inodeblock(inodedep, bp) 11015 struct inodedep *inodedep; 11016 struct buf *bp; /* buffer containing the inode block */ 11017 { 11018 struct freefile *freefile; 11019 struct allocdirect *adp, *nextadp; 11020 struct ufs1_dinode *dp1 = NULL; 11021 struct ufs2_dinode *dp2 = NULL; 11022 struct workhead wkhd; 11023 int hadchanges, fstype; 11024 ino_t freelink; 11025 11026 LIST_INIT(&wkhd); 11027 hadchanges = 0; 11028 freefile = NULL; 11029 if ((inodedep->id_state & IOSTARTED) == 0) 11030 panic("handle_written_inodeblock: not started"); 11031 inodedep->id_state &= ~IOSTARTED; 11032 if (inodedep->id_fs->fs_magic == FS_UFS1_MAGIC) { 11033 fstype = UFS1; 11034 dp1 = (struct ufs1_dinode *)bp->b_data + 11035 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11036 freelink = dp1->di_freelink; 11037 } else { 11038 fstype = UFS2; 11039 dp2 = (struct ufs2_dinode *)bp->b_data + 11040 ino_to_fsbo(inodedep->id_fs, inodedep->id_ino); 11041 freelink = dp2->di_freelink; 11042 } 11043 /* 11044 * Leave this inodeblock dirty until it's in the list. 11045 */ 11046 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) == UNLINKED) { 11047 struct inodedep *inon; 11048 11049 inon = TAILQ_NEXT(inodedep, id_unlinked); 11050 if ((inon == NULL && freelink == 0) || 11051 (inon && inon->id_ino == freelink)) { 11052 if (inon) 11053 inon->id_state |= UNLINKPREV; 11054 inodedep->id_state |= UNLINKNEXT; 11055 } 11056 hadchanges = 1; 11057 } 11058 /* 11059 * If we had to rollback the inode allocation because of 11060 * bitmaps being incomplete, then simply restore it. 11061 * Keep the block dirty so that it will not be reclaimed until 11062 * all associated dependencies have been cleared and the 11063 * corresponding updates written to disk. 11064 */ 11065 if (inodedep->id_savedino1 != NULL) { 11066 hadchanges = 1; 11067 if (fstype == UFS1) 11068 *dp1 = *inodedep->id_savedino1; 11069 else 11070 *dp2 = *inodedep->id_savedino2; 11071 free(inodedep->id_savedino1, M_SAVEDINO); 11072 inodedep->id_savedino1 = NULL; 11073 if ((bp->b_flags & B_DELWRI) == 0) 11074 stat_inode_bitmap++; 11075 bdirty(bp); 11076 /* 11077 * If the inode is clear here and GOINGAWAY it will never 11078 * be written. Process the bufwait and clear any pending 11079 * work which may include the freefile. 11080 */ 11081 if (inodedep->id_state & GOINGAWAY) 11082 goto bufwait; 11083 return (1); 11084 } 11085 inodedep->id_state |= COMPLETE; 11086 /* 11087 * Roll forward anything that had to be rolled back before 11088 * the inode could be updated. 11089 */ 11090 for (adp = TAILQ_FIRST(&inodedep->id_inoupdt); adp; adp = nextadp) { 11091 nextadp = TAILQ_NEXT(adp, ad_next); 11092 if (adp->ad_state & ATTACHED) 11093 panic("handle_written_inodeblock: new entry"); 11094 if (fstype == UFS1) { 11095 if (adp->ad_offset < NDADDR) { 11096 if (dp1->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11097 panic("%s %s #%jd mismatch %d != %jd", 11098 "handle_written_inodeblock:", 11099 "direct pointer", 11100 (intmax_t)adp->ad_offset, 11101 dp1->di_db[adp->ad_offset], 11102 (intmax_t)adp->ad_oldblkno); 11103 dp1->di_db[adp->ad_offset] = adp->ad_newblkno; 11104 } else { 11105 if (dp1->di_ib[adp->ad_offset - NDADDR] != 0) 11106 panic("%s: %s #%jd allocated as %d", 11107 "handle_written_inodeblock", 11108 "indirect pointer", 11109 (intmax_t)adp->ad_offset - NDADDR, 11110 dp1->di_ib[adp->ad_offset - NDADDR]); 11111 dp1->di_ib[adp->ad_offset - NDADDR] = 11112 adp->ad_newblkno; 11113 } 11114 } else { 11115 if (adp->ad_offset < NDADDR) { 11116 if (dp2->di_db[adp->ad_offset]!=adp->ad_oldblkno) 11117 panic("%s: %s #%jd %s %jd != %jd", 11118 "handle_written_inodeblock", 11119 "direct pointer", 11120 (intmax_t)adp->ad_offset, "mismatch", 11121 (intmax_t)dp2->di_db[adp->ad_offset], 11122 (intmax_t)adp->ad_oldblkno); 11123 dp2->di_db[adp->ad_offset] = adp->ad_newblkno; 11124 } else { 11125 if (dp2->di_ib[adp->ad_offset - NDADDR] != 0) 11126 panic("%s: %s #%jd allocated as %jd", 11127 "handle_written_inodeblock", 11128 "indirect pointer", 11129 (intmax_t)adp->ad_offset - NDADDR, 11130 (intmax_t) 11131 dp2->di_ib[adp->ad_offset - NDADDR]); 11132 dp2->di_ib[adp->ad_offset - NDADDR] = 11133 adp->ad_newblkno; 11134 } 11135 } 11136 adp->ad_state &= ~UNDONE; 11137 adp->ad_state |= ATTACHED; 11138 hadchanges = 1; 11139 } 11140 for (adp = TAILQ_FIRST(&inodedep->id_extupdt); adp; adp = nextadp) { 11141 nextadp = TAILQ_NEXT(adp, ad_next); 11142 if (adp->ad_state & ATTACHED) 11143 panic("handle_written_inodeblock: new entry"); 11144 if (dp2->di_extb[adp->ad_offset] != adp->ad_oldblkno) 11145 panic("%s: direct pointers #%jd %s %jd != %jd", 11146 "handle_written_inodeblock", 11147 (intmax_t)adp->ad_offset, "mismatch", 11148 (intmax_t)dp2->di_extb[adp->ad_offset], 11149 (intmax_t)adp->ad_oldblkno); 11150 dp2->di_extb[adp->ad_offset] = adp->ad_newblkno; 11151 adp->ad_state &= ~UNDONE; 11152 adp->ad_state |= ATTACHED; 11153 hadchanges = 1; 11154 } 11155 if (hadchanges && (bp->b_flags & B_DELWRI) == 0) 11156 stat_direct_blk_ptrs++; 11157 /* 11158 * Reset the file size to its most up-to-date value. 11159 */ 11160 if (inodedep->id_savedsize == -1 || inodedep->id_savedextsize == -1) 11161 panic("handle_written_inodeblock: bad size"); 11162 if (inodedep->id_savednlink > LINK_MAX) 11163 panic("handle_written_inodeblock: Invalid link count " 11164 "%d for inodedep %p", inodedep->id_savednlink, inodedep); 11165 if (fstype == UFS1) { 11166 if (dp1->di_nlink != inodedep->id_savednlink) { 11167 dp1->di_nlink = inodedep->id_savednlink; 11168 hadchanges = 1; 11169 } 11170 if (dp1->di_size != inodedep->id_savedsize) { 11171 dp1->di_size = inodedep->id_savedsize; 11172 hadchanges = 1; 11173 } 11174 } else { 11175 if (dp2->di_nlink != inodedep->id_savednlink) { 11176 dp2->di_nlink = inodedep->id_savednlink; 11177 hadchanges = 1; 11178 } 11179 if (dp2->di_size != inodedep->id_savedsize) { 11180 dp2->di_size = inodedep->id_savedsize; 11181 hadchanges = 1; 11182 } 11183 if (dp2->di_extsize != inodedep->id_savedextsize) { 11184 dp2->di_extsize = inodedep->id_savedextsize; 11185 hadchanges = 1; 11186 } 11187 } 11188 inodedep->id_savedsize = -1; 11189 inodedep->id_savedextsize = -1; 11190 inodedep->id_savednlink = -1; 11191 /* 11192 * If there were any rollbacks in the inode block, then it must be 11193 * marked dirty so that its will eventually get written back in 11194 * its correct form. 11195 */ 11196 if (hadchanges) 11197 bdirty(bp); 11198 bufwait: 11199 /* 11200 * Process any allocdirects that completed during the update. 11201 */ 11202 if ((adp = TAILQ_FIRST(&inodedep->id_inoupdt)) != NULL) 11203 handle_allocdirect_partdone(adp, &wkhd); 11204 if ((adp = TAILQ_FIRST(&inodedep->id_extupdt)) != NULL) 11205 handle_allocdirect_partdone(adp, &wkhd); 11206 /* 11207 * Process deallocations that were held pending until the 11208 * inode had been written to disk. Freeing of the inode 11209 * is delayed until after all blocks have been freed to 11210 * avoid creation of new <vfsid, inum, lbn> triples 11211 * before the old ones have been deleted. Completely 11212 * unlinked inodes are not processed until the unlinked 11213 * inode list is written or the last reference is removed. 11214 */ 11215 if ((inodedep->id_state & (UNLINKED | UNLINKONLIST)) != UNLINKED) { 11216 freefile = handle_bufwait(inodedep, NULL); 11217 if (freefile && !LIST_EMPTY(&wkhd)) { 11218 WORKLIST_INSERT(&wkhd, &freefile->fx_list); 11219 freefile = NULL; 11220 } 11221 } 11222 /* 11223 * Move rolled forward dependency completions to the bufwait list 11224 * now that those that were already written have been processed. 11225 */ 11226 if (!LIST_EMPTY(&wkhd) && hadchanges == 0) 11227 panic("handle_written_inodeblock: bufwait but no changes"); 11228 jwork_move(&inodedep->id_bufwait, &wkhd); 11229 11230 if (freefile != NULL) { 11231 /* 11232 * If the inode is goingaway it was never written. Fake up 11233 * the state here so free_inodedep() can succeed. 11234 */ 11235 if (inodedep->id_state & GOINGAWAY) 11236 inodedep->id_state |= COMPLETE | DEPCOMPLETE; 11237 if (free_inodedep(inodedep) == 0) 11238 panic("handle_written_inodeblock: live inodedep %p", 11239 inodedep); 11240 add_to_worklist(&freefile->fx_list, 0); 11241 return (0); 11242 } 11243 11244 /* 11245 * If no outstanding dependencies, free it. 11246 */ 11247 if (free_inodedep(inodedep) || 11248 (TAILQ_FIRST(&inodedep->id_inoreflst) == 0 && 11249 TAILQ_FIRST(&inodedep->id_inoupdt) == 0 && 11250 TAILQ_FIRST(&inodedep->id_extupdt) == 0 && 11251 LIST_FIRST(&inodedep->id_bufwait) == 0)) 11252 return (0); 11253 return (hadchanges); 11254 } 11255 11256 static int 11257 handle_written_indirdep(indirdep, bp, bpp) 11258 struct indirdep *indirdep; 11259 struct buf *bp; 11260 struct buf **bpp; 11261 { 11262 struct allocindir *aip; 11263 struct buf *sbp; 11264 int chgs; 11265 11266 if (indirdep->ir_state & GOINGAWAY) 11267 panic("handle_written_indirdep: indirdep gone"); 11268 if ((indirdep->ir_state & IOSTARTED) == 0) 11269 panic("handle_written_indirdep: IO not started"); 11270 chgs = 0; 11271 /* 11272 * If there were rollbacks revert them here. 11273 */ 11274 if (indirdep->ir_saveddata) { 11275 bcopy(indirdep->ir_saveddata, bp->b_data, bp->b_bcount); 11276 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11277 free(indirdep->ir_saveddata, M_INDIRDEP); 11278 indirdep->ir_saveddata = NULL; 11279 } 11280 chgs = 1; 11281 } 11282 indirdep->ir_state &= ~(UNDONE | IOSTARTED); 11283 indirdep->ir_state |= ATTACHED; 11284 /* 11285 * Move allocindirs with written pointers to the completehd if 11286 * the indirdep's pointer is not yet written. Otherwise 11287 * free them here. 11288 */ 11289 while ((aip = LIST_FIRST(&indirdep->ir_writehd)) != 0) { 11290 LIST_REMOVE(aip, ai_next); 11291 if ((indirdep->ir_state & DEPCOMPLETE) == 0) { 11292 LIST_INSERT_HEAD(&indirdep->ir_completehd, aip, 11293 ai_next); 11294 newblk_freefrag(&aip->ai_block); 11295 continue; 11296 } 11297 free_newblk(&aip->ai_block); 11298 } 11299 /* 11300 * Move allocindirs that have finished dependency processing from 11301 * the done list to the write list after updating the pointers. 11302 */ 11303 if (TAILQ_EMPTY(&indirdep->ir_trunc)) { 11304 while ((aip = LIST_FIRST(&indirdep->ir_donehd)) != 0) { 11305 handle_allocindir_partdone(aip); 11306 if (aip == LIST_FIRST(&indirdep->ir_donehd)) 11307 panic("disk_write_complete: not gone"); 11308 chgs = 1; 11309 } 11310 } 11311 /* 11312 * Preserve the indirdep if there were any changes or if it is not 11313 * yet valid on disk. 11314 */ 11315 if (chgs) { 11316 stat_indir_blk_ptrs++; 11317 bdirty(bp); 11318 return (1); 11319 } 11320 /* 11321 * If there were no changes we can discard the savedbp and detach 11322 * ourselves from the buf. We are only carrying completed pointers 11323 * in this case. 11324 */ 11325 sbp = indirdep->ir_savebp; 11326 sbp->b_flags |= B_INVAL | B_NOCACHE; 11327 indirdep->ir_savebp = NULL; 11328 indirdep->ir_bp = NULL; 11329 if (*bpp != NULL) 11330 panic("handle_written_indirdep: bp already exists."); 11331 *bpp = sbp; 11332 /* 11333 * The indirdep may not be freed until its parent points at it. 11334 */ 11335 if (indirdep->ir_state & DEPCOMPLETE) 11336 free_indirdep(indirdep); 11337 11338 return (0); 11339 } 11340 11341 /* 11342 * Process a diradd entry after its dependent inode has been written. 11343 * This routine must be called with splbio interrupts blocked. 11344 */ 11345 static void 11346 diradd_inode_written(dap, inodedep) 11347 struct diradd *dap; 11348 struct inodedep *inodedep; 11349 { 11350 11351 dap->da_state |= COMPLETE; 11352 complete_diradd(dap); 11353 WORKLIST_INSERT(&inodedep->id_pendinghd, &dap->da_list); 11354 } 11355 11356 /* 11357 * Returns true if the bmsafemap will have rollbacks when written. Must 11358 * only be called with lk and the buf lock on the cg held. 11359 */ 11360 static int 11361 bmsafemap_backgroundwrite(bmsafemap, bp) 11362 struct bmsafemap *bmsafemap; 11363 struct buf *bp; 11364 { 11365 int dirty; 11366 11367 dirty = !LIST_EMPTY(&bmsafemap->sm_jaddrefhd) | 11368 !LIST_EMPTY(&bmsafemap->sm_jnewblkhd); 11369 /* 11370 * If we're initiating a background write we need to process the 11371 * rollbacks as they exist now, not as they exist when IO starts. 11372 * No other consumers will look at the contents of the shadowed 11373 * buf so this is safe to do here. 11374 */ 11375 if (bp->b_xflags & BX_BKGRDMARKER) 11376 initiate_write_bmsafemap(bmsafemap, bp); 11377 11378 return (dirty); 11379 } 11380 11381 /* 11382 * Re-apply an allocation when a cg write is complete. 11383 */ 11384 static int 11385 jnewblk_rollforward(jnewblk, fs, cgp, blksfree) 11386 struct jnewblk *jnewblk; 11387 struct fs *fs; 11388 struct cg *cgp; 11389 uint8_t *blksfree; 11390 { 11391 ufs1_daddr_t fragno; 11392 ufs2_daddr_t blkno; 11393 long cgbno, bbase; 11394 int frags, blk; 11395 int i; 11396 11397 frags = 0; 11398 cgbno = dtogd(fs, jnewblk->jn_blkno); 11399 for (i = jnewblk->jn_oldfrags; i < jnewblk->jn_frags; i++) { 11400 if (isclr(blksfree, cgbno + i)) 11401 panic("jnewblk_rollforward: re-allocated fragment"); 11402 frags++; 11403 } 11404 if (frags == fs->fs_frag) { 11405 blkno = fragstoblks(fs, cgbno); 11406 ffs_clrblock(fs, blksfree, (long)blkno); 11407 ffs_clusteracct(fs, cgp, blkno, -1); 11408 cgp->cg_cs.cs_nbfree--; 11409 } else { 11410 bbase = cgbno - fragnum(fs, cgbno); 11411 cgbno += jnewblk->jn_oldfrags; 11412 /* If a complete block had been reassembled, account for it. */ 11413 fragno = fragstoblks(fs, bbase); 11414 if (ffs_isblock(fs, blksfree, fragno)) { 11415 cgp->cg_cs.cs_nffree += fs->fs_frag; 11416 ffs_clusteracct(fs, cgp, fragno, -1); 11417 cgp->cg_cs.cs_nbfree--; 11418 } 11419 /* Decrement the old frags. */ 11420 blk = blkmap(fs, blksfree, bbase); 11421 ffs_fragacct(fs, blk, cgp->cg_frsum, -1); 11422 /* Allocate the fragment */ 11423 for (i = 0; i < frags; i++) 11424 clrbit(blksfree, cgbno + i); 11425 cgp->cg_cs.cs_nffree -= frags; 11426 /* Add back in counts associated with the new frags */ 11427 blk = blkmap(fs, blksfree, bbase); 11428 ffs_fragacct(fs, blk, cgp->cg_frsum, 1); 11429 } 11430 return (frags); 11431 } 11432 11433 /* 11434 * Complete a write to a bmsafemap structure. Roll forward any bitmap 11435 * changes if it's not a background write. Set all written dependencies 11436 * to DEPCOMPLETE and free the structure if possible. 11437 */ 11438 static int 11439 handle_written_bmsafemap(bmsafemap, bp) 11440 struct bmsafemap *bmsafemap; 11441 struct buf *bp; 11442 { 11443 struct newblk *newblk; 11444 struct inodedep *inodedep; 11445 struct jaddref *jaddref, *jatmp; 11446 struct jnewblk *jnewblk, *jntmp; 11447 struct ufsmount *ump; 11448 uint8_t *inosused; 11449 uint8_t *blksfree; 11450 struct cg *cgp; 11451 struct fs *fs; 11452 ino_t ino; 11453 int chgs; 11454 11455 if ((bmsafemap->sm_state & IOSTARTED) == 0) 11456 panic("initiate_write_bmsafemap: Not started\n"); 11457 ump = VFSTOUFS(bmsafemap->sm_list.wk_mp); 11458 chgs = 0; 11459 bmsafemap->sm_state &= ~IOSTARTED; 11460 /* 11461 * Release journal work that was waiting on the write. 11462 */ 11463 handle_jwork(&bmsafemap->sm_freewr); 11464 11465 /* 11466 * Restore unwritten inode allocation pending jaddref writes. 11467 */ 11468 if (!LIST_EMPTY(&bmsafemap->sm_jaddrefhd)) { 11469 cgp = (struct cg *)bp->b_data; 11470 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11471 inosused = cg_inosused(cgp); 11472 LIST_FOREACH_SAFE(jaddref, &bmsafemap->sm_jaddrefhd, 11473 ja_bmdeps, jatmp) { 11474 if ((jaddref->ja_state & UNDONE) == 0) 11475 continue; 11476 ino = jaddref->ja_ino % fs->fs_ipg; 11477 if (isset(inosused, ino)) 11478 panic("handle_written_bmsafemap: " 11479 "re-allocated inode"); 11480 if ((bp->b_xflags & BX_BKGRDMARKER) == 0) { 11481 if ((jaddref->ja_mode & IFMT) == IFDIR) 11482 cgp->cg_cs.cs_ndir++; 11483 cgp->cg_cs.cs_nifree--; 11484 setbit(inosused, ino); 11485 chgs = 1; 11486 } 11487 jaddref->ja_state &= ~UNDONE; 11488 jaddref->ja_state |= ATTACHED; 11489 free_jaddref(jaddref); 11490 } 11491 } 11492 /* 11493 * Restore any block allocations which are pending journal writes. 11494 */ 11495 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd) != NULL) { 11496 cgp = (struct cg *)bp->b_data; 11497 fs = VFSTOUFS(bmsafemap->sm_list.wk_mp)->um_fs; 11498 blksfree = cg_blksfree(cgp); 11499 LIST_FOREACH_SAFE(jnewblk, &bmsafemap->sm_jnewblkhd, jn_deps, 11500 jntmp) { 11501 if ((jnewblk->jn_state & UNDONE) == 0) 11502 continue; 11503 if ((bp->b_xflags & BX_BKGRDMARKER) == 0 && 11504 jnewblk_rollforward(jnewblk, fs, cgp, blksfree)) 11505 chgs = 1; 11506 jnewblk->jn_state &= ~(UNDONE | NEWBLOCK); 11507 jnewblk->jn_state |= ATTACHED; 11508 free_jnewblk(jnewblk); 11509 } 11510 } 11511 while ((newblk = LIST_FIRST(&bmsafemap->sm_newblkwr))) { 11512 newblk->nb_state |= DEPCOMPLETE; 11513 newblk->nb_state &= ~ONDEPLIST; 11514 newblk->nb_bmsafemap = NULL; 11515 LIST_REMOVE(newblk, nb_deps); 11516 if (newblk->nb_list.wk_type == D_ALLOCDIRECT) 11517 handle_allocdirect_partdone( 11518 WK_ALLOCDIRECT(&newblk->nb_list), NULL); 11519 else if (newblk->nb_list.wk_type == D_ALLOCINDIR) 11520 handle_allocindir_partdone( 11521 WK_ALLOCINDIR(&newblk->nb_list)); 11522 else if (newblk->nb_list.wk_type != D_NEWBLK) 11523 panic("handle_written_bmsafemap: Unexpected type: %s", 11524 TYPENAME(newblk->nb_list.wk_type)); 11525 } 11526 while ((inodedep = LIST_FIRST(&bmsafemap->sm_inodedepwr)) != NULL) { 11527 inodedep->id_state |= DEPCOMPLETE; 11528 inodedep->id_state &= ~ONDEPLIST; 11529 LIST_REMOVE(inodedep, id_deps); 11530 inodedep->id_bmsafemap = NULL; 11531 } 11532 LIST_REMOVE(bmsafemap, sm_next); 11533 if (chgs == 0 && LIST_EMPTY(&bmsafemap->sm_jaddrefhd) && 11534 LIST_EMPTY(&bmsafemap->sm_jnewblkhd) && 11535 LIST_EMPTY(&bmsafemap->sm_newblkhd) && 11536 LIST_EMPTY(&bmsafemap->sm_inodedephd) && 11537 LIST_EMPTY(&bmsafemap->sm_freehd)) { 11538 LIST_REMOVE(bmsafemap, sm_hash); 11539 WORKITEM_FREE(bmsafemap, D_BMSAFEMAP); 11540 return (0); 11541 } 11542 LIST_INSERT_HEAD(&ump->softdep_dirtycg, bmsafemap, sm_next); 11543 bdirty(bp); 11544 return (1); 11545 } 11546 11547 /* 11548 * Try to free a mkdir dependency. 11549 */ 11550 static void 11551 complete_mkdir(mkdir) 11552 struct mkdir *mkdir; 11553 { 11554 struct diradd *dap; 11555 11556 if ((mkdir->md_state & ALLCOMPLETE) != ALLCOMPLETE) 11557 return; 11558 LIST_REMOVE(mkdir, md_mkdirs); 11559 dap = mkdir->md_diradd; 11560 dap->da_state &= ~(mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)); 11561 if ((dap->da_state & (MKDIR_PARENT | MKDIR_BODY)) == 0) { 11562 dap->da_state |= DEPCOMPLETE; 11563 complete_diradd(dap); 11564 } 11565 WORKITEM_FREE(mkdir, D_MKDIR); 11566 } 11567 11568 /* 11569 * Handle the completion of a mkdir dependency. 11570 */ 11571 static void 11572 handle_written_mkdir(mkdir, type) 11573 struct mkdir *mkdir; 11574 int type; 11575 { 11576 11577 if ((mkdir->md_state & (MKDIR_PARENT | MKDIR_BODY)) != type) 11578 panic("handle_written_mkdir: bad type"); 11579 mkdir->md_state |= COMPLETE; 11580 complete_mkdir(mkdir); 11581 } 11582 11583 static int 11584 free_pagedep(pagedep) 11585 struct pagedep *pagedep; 11586 { 11587 int i; 11588 11589 if (pagedep->pd_state & NEWBLOCK) 11590 return (0); 11591 if (!LIST_EMPTY(&pagedep->pd_dirremhd)) 11592 return (0); 11593 for (i = 0; i < DAHASHSZ; i++) 11594 if (!LIST_EMPTY(&pagedep->pd_diraddhd[i])) 11595 return (0); 11596 if (!LIST_EMPTY(&pagedep->pd_pendinghd)) 11597 return (0); 11598 if (!LIST_EMPTY(&pagedep->pd_jmvrefhd)) 11599 return (0); 11600 if (pagedep->pd_state & ONWORKLIST) 11601 WORKLIST_REMOVE(&pagedep->pd_list); 11602 LIST_REMOVE(pagedep, pd_hash); 11603 WORKITEM_FREE(pagedep, D_PAGEDEP); 11604 11605 return (1); 11606 } 11607 11608 /* 11609 * Called from within softdep_disk_write_complete above. 11610 * A write operation was just completed. Removed inodes can 11611 * now be freed and associated block pointers may be committed. 11612 * Note that this routine is always called from interrupt level 11613 * with further splbio interrupts blocked. 11614 */ 11615 static int 11616 handle_written_filepage(pagedep, bp) 11617 struct pagedep *pagedep; 11618 struct buf *bp; /* buffer containing the written page */ 11619 { 11620 struct dirrem *dirrem; 11621 struct diradd *dap, *nextdap; 11622 struct direct *ep; 11623 int i, chgs; 11624 11625 if ((pagedep->pd_state & IOSTARTED) == 0) 11626 panic("handle_written_filepage: not started"); 11627 pagedep->pd_state &= ~IOSTARTED; 11628 /* 11629 * Process any directory removals that have been committed. 11630 */ 11631 while ((dirrem = LIST_FIRST(&pagedep->pd_dirremhd)) != NULL) { 11632 LIST_REMOVE(dirrem, dm_next); 11633 dirrem->dm_state |= COMPLETE; 11634 dirrem->dm_dirinum = pagedep->pd_ino; 11635 KASSERT(LIST_EMPTY(&dirrem->dm_jremrefhd), 11636 ("handle_written_filepage: Journal entries not written.")); 11637 add_to_worklist(&dirrem->dm_list, 0); 11638 } 11639 /* 11640 * Free any directory additions that have been committed. 11641 * If it is a newly allocated block, we have to wait until 11642 * the on-disk directory inode claims the new block. 11643 */ 11644 if ((pagedep->pd_state & NEWBLOCK) == 0) 11645 while ((dap = LIST_FIRST(&pagedep->pd_pendinghd)) != NULL) 11646 free_diradd(dap, NULL); 11647 /* 11648 * Uncommitted directory entries must be restored. 11649 */ 11650 for (chgs = 0, i = 0; i < DAHASHSZ; i++) { 11651 for (dap = LIST_FIRST(&pagedep->pd_diraddhd[i]); dap; 11652 dap = nextdap) { 11653 nextdap = LIST_NEXT(dap, da_pdlist); 11654 if (dap->da_state & ATTACHED) 11655 panic("handle_written_filepage: attached"); 11656 ep = (struct direct *) 11657 ((char *)bp->b_data + dap->da_offset); 11658 ep->d_ino = dap->da_newinum; 11659 dap->da_state &= ~UNDONE; 11660 dap->da_state |= ATTACHED; 11661 chgs = 1; 11662 /* 11663 * If the inode referenced by the directory has 11664 * been written out, then the dependency can be 11665 * moved to the pending list. 11666 */ 11667 if ((dap->da_state & ALLCOMPLETE) == ALLCOMPLETE) { 11668 LIST_REMOVE(dap, da_pdlist); 11669 LIST_INSERT_HEAD(&pagedep->pd_pendinghd, dap, 11670 da_pdlist); 11671 } 11672 } 11673 } 11674 /* 11675 * If there were any rollbacks in the directory, then it must be 11676 * marked dirty so that its will eventually get written back in 11677 * its correct form. 11678 */ 11679 if (chgs) { 11680 if ((bp->b_flags & B_DELWRI) == 0) 11681 stat_dir_entry++; 11682 bdirty(bp); 11683 return (1); 11684 } 11685 /* 11686 * If we are not waiting for a new directory block to be 11687 * claimed by its inode, then the pagedep will be freed. 11688 * Otherwise it will remain to track any new entries on 11689 * the page in case they are fsync'ed. 11690 */ 11691 free_pagedep(pagedep); 11692 return (0); 11693 } 11694 11695 /* 11696 * Writing back in-core inode structures. 11697 * 11698 * The filesystem only accesses an inode's contents when it occupies an 11699 * "in-core" inode structure. These "in-core" structures are separate from 11700 * the page frames used to cache inode blocks. Only the latter are 11701 * transferred to/from the disk. So, when the updated contents of the 11702 * "in-core" inode structure are copied to the corresponding in-memory inode 11703 * block, the dependencies are also transferred. The following procedure is 11704 * called when copying a dirty "in-core" inode to a cached inode block. 11705 */ 11706 11707 /* 11708 * Called when an inode is loaded from disk. If the effective link count 11709 * differed from the actual link count when it was last flushed, then we 11710 * need to ensure that the correct effective link count is put back. 11711 */ 11712 void 11713 softdep_load_inodeblock(ip) 11714 struct inode *ip; /* the "in_core" copy of the inode */ 11715 { 11716 struct inodedep *inodedep; 11717 11718 /* 11719 * Check for alternate nlink count. 11720 */ 11721 ip->i_effnlink = ip->i_nlink; 11722 ACQUIRE_LOCK(&lk); 11723 if (inodedep_lookup(UFSTOVFS(ip->i_ump), ip->i_number, 0, 11724 &inodedep) == 0) { 11725 FREE_LOCK(&lk); 11726 return; 11727 } 11728 ip->i_effnlink -= inodedep->id_nlinkdelta; 11729 FREE_LOCK(&lk); 11730 } 11731 11732 /* 11733 * This routine is called just before the "in-core" inode 11734 * information is to be copied to the in-memory inode block. 11735 * Recall that an inode block contains several inodes. If 11736 * the force flag is set, then the dependencies will be 11737 * cleared so that the update can always be made. Note that 11738 * the buffer is locked when this routine is called, so we 11739 * will never be in the middle of writing the inode block 11740 * to disk. 11741 */ 11742 void 11743 softdep_update_inodeblock(ip, bp, waitfor) 11744 struct inode *ip; /* the "in_core" copy of the inode */ 11745 struct buf *bp; /* the buffer containing the inode block */ 11746 int waitfor; /* nonzero => update must be allowed */ 11747 { 11748 struct inodedep *inodedep; 11749 struct inoref *inoref; 11750 struct worklist *wk; 11751 struct mount *mp; 11752 struct buf *ibp; 11753 struct fs *fs; 11754 int error; 11755 11756 mp = UFSTOVFS(ip->i_ump); 11757 fs = ip->i_fs; 11758 /* 11759 * Preserve the freelink that is on disk. clear_unlinked_inodedep() 11760 * does not have access to the in-core ip so must write directly into 11761 * the inode block buffer when setting freelink. 11762 */ 11763 if (fs->fs_magic == FS_UFS1_MAGIC) 11764 DIP_SET(ip, i_freelink, ((struct ufs1_dinode *)bp->b_data + 11765 ino_to_fsbo(fs, ip->i_number))->di_freelink); 11766 else 11767 DIP_SET(ip, i_freelink, ((struct ufs2_dinode *)bp->b_data + 11768 ino_to_fsbo(fs, ip->i_number))->di_freelink); 11769 /* 11770 * If the effective link count is not equal to the actual link 11771 * count, then we must track the difference in an inodedep while 11772 * the inode is (potentially) tossed out of the cache. Otherwise, 11773 * if there is no existing inodedep, then there are no dependencies 11774 * to track. 11775 */ 11776 ACQUIRE_LOCK(&lk); 11777 again: 11778 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 11779 FREE_LOCK(&lk); 11780 if (ip->i_effnlink != ip->i_nlink) 11781 panic("softdep_update_inodeblock: bad link count"); 11782 return; 11783 } 11784 if (inodedep->id_nlinkdelta != ip->i_nlink - ip->i_effnlink) 11785 panic("softdep_update_inodeblock: bad delta"); 11786 /* 11787 * If we're flushing all dependencies we must also move any waiting 11788 * for journal writes onto the bufwait list prior to I/O. 11789 */ 11790 if (waitfor) { 11791 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 11792 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 11793 == DEPCOMPLETE) { 11794 jwait(&inoref->if_list, MNT_WAIT); 11795 goto again; 11796 } 11797 } 11798 } 11799 /* 11800 * Changes have been initiated. Anything depending on these 11801 * changes cannot occur until this inode has been written. 11802 */ 11803 inodedep->id_state &= ~COMPLETE; 11804 if ((inodedep->id_state & ONWORKLIST) == 0) 11805 WORKLIST_INSERT(&bp->b_dep, &inodedep->id_list); 11806 /* 11807 * Any new dependencies associated with the incore inode must 11808 * now be moved to the list associated with the buffer holding 11809 * the in-memory copy of the inode. Once merged process any 11810 * allocdirects that are completed by the merger. 11811 */ 11812 merge_inode_lists(&inodedep->id_newinoupdt, &inodedep->id_inoupdt); 11813 if (!TAILQ_EMPTY(&inodedep->id_inoupdt)) 11814 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_inoupdt), 11815 NULL); 11816 merge_inode_lists(&inodedep->id_newextupdt, &inodedep->id_extupdt); 11817 if (!TAILQ_EMPTY(&inodedep->id_extupdt)) 11818 handle_allocdirect_partdone(TAILQ_FIRST(&inodedep->id_extupdt), 11819 NULL); 11820 /* 11821 * Now that the inode has been pushed into the buffer, the 11822 * operations dependent on the inode being written to disk 11823 * can be moved to the id_bufwait so that they will be 11824 * processed when the buffer I/O completes. 11825 */ 11826 while ((wk = LIST_FIRST(&inodedep->id_inowait)) != NULL) { 11827 WORKLIST_REMOVE(wk); 11828 WORKLIST_INSERT(&inodedep->id_bufwait, wk); 11829 } 11830 /* 11831 * Newly allocated inodes cannot be written until the bitmap 11832 * that allocates them have been written (indicated by 11833 * DEPCOMPLETE being set in id_state). If we are doing a 11834 * forced sync (e.g., an fsync on a file), we force the bitmap 11835 * to be written so that the update can be done. 11836 */ 11837 if (waitfor == 0) { 11838 FREE_LOCK(&lk); 11839 return; 11840 } 11841 retry: 11842 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) != 0) { 11843 FREE_LOCK(&lk); 11844 return; 11845 } 11846 ibp = inodedep->id_bmsafemap->sm_buf; 11847 ibp = getdirtybuf(ibp, &lk, MNT_WAIT); 11848 if (ibp == NULL) { 11849 /* 11850 * If ibp came back as NULL, the dependency could have been 11851 * freed while we slept. Look it up again, and check to see 11852 * that it has completed. 11853 */ 11854 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) 11855 goto retry; 11856 FREE_LOCK(&lk); 11857 return; 11858 } 11859 FREE_LOCK(&lk); 11860 if ((error = bwrite(ibp)) != 0) 11861 softdep_error("softdep_update_inodeblock: bwrite", error); 11862 } 11863 11864 /* 11865 * Merge the a new inode dependency list (such as id_newinoupdt) into an 11866 * old inode dependency list (such as id_inoupdt). This routine must be 11867 * called with splbio interrupts blocked. 11868 */ 11869 static void 11870 merge_inode_lists(newlisthead, oldlisthead) 11871 struct allocdirectlst *newlisthead; 11872 struct allocdirectlst *oldlisthead; 11873 { 11874 struct allocdirect *listadp, *newadp; 11875 11876 newadp = TAILQ_FIRST(newlisthead); 11877 for (listadp = TAILQ_FIRST(oldlisthead); listadp && newadp;) { 11878 if (listadp->ad_offset < newadp->ad_offset) { 11879 listadp = TAILQ_NEXT(listadp, ad_next); 11880 continue; 11881 } 11882 TAILQ_REMOVE(newlisthead, newadp, ad_next); 11883 TAILQ_INSERT_BEFORE(listadp, newadp, ad_next); 11884 if (listadp->ad_offset == newadp->ad_offset) { 11885 allocdirect_merge(oldlisthead, newadp, 11886 listadp); 11887 listadp = newadp; 11888 } 11889 newadp = TAILQ_FIRST(newlisthead); 11890 } 11891 while ((newadp = TAILQ_FIRST(newlisthead)) != NULL) { 11892 TAILQ_REMOVE(newlisthead, newadp, ad_next); 11893 TAILQ_INSERT_TAIL(oldlisthead, newadp, ad_next); 11894 } 11895 } 11896 11897 /* 11898 * If we are doing an fsync, then we must ensure that any directory 11899 * entries for the inode have been written after the inode gets to disk. 11900 */ 11901 int 11902 softdep_fsync(vp) 11903 struct vnode *vp; /* the "in_core" copy of the inode */ 11904 { 11905 struct inodedep *inodedep; 11906 struct pagedep *pagedep; 11907 struct inoref *inoref; 11908 struct worklist *wk; 11909 struct diradd *dap; 11910 struct mount *mp; 11911 struct vnode *pvp; 11912 struct inode *ip; 11913 struct buf *bp; 11914 struct fs *fs; 11915 struct thread *td = curthread; 11916 int error, flushparent, pagedep_new_block; 11917 ino_t parentino; 11918 ufs_lbn_t lbn; 11919 11920 ip = VTOI(vp); 11921 fs = ip->i_fs; 11922 mp = vp->v_mount; 11923 ACQUIRE_LOCK(&lk); 11924 restart: 11925 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) { 11926 FREE_LOCK(&lk); 11927 return (0); 11928 } 11929 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 11930 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 11931 == DEPCOMPLETE) { 11932 jwait(&inoref->if_list, MNT_WAIT); 11933 goto restart; 11934 } 11935 } 11936 if (!LIST_EMPTY(&inodedep->id_inowait) || 11937 !TAILQ_EMPTY(&inodedep->id_extupdt) || 11938 !TAILQ_EMPTY(&inodedep->id_newextupdt) || 11939 !TAILQ_EMPTY(&inodedep->id_inoupdt) || 11940 !TAILQ_EMPTY(&inodedep->id_newinoupdt)) 11941 panic("softdep_fsync: pending ops %p", inodedep); 11942 for (error = 0, flushparent = 0; ; ) { 11943 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) == NULL) 11944 break; 11945 if (wk->wk_type != D_DIRADD) 11946 panic("softdep_fsync: Unexpected type %s", 11947 TYPENAME(wk->wk_type)); 11948 dap = WK_DIRADD(wk); 11949 /* 11950 * Flush our parent if this directory entry has a MKDIR_PARENT 11951 * dependency or is contained in a newly allocated block. 11952 */ 11953 if (dap->da_state & DIRCHG) 11954 pagedep = dap->da_previous->dm_pagedep; 11955 else 11956 pagedep = dap->da_pagedep; 11957 parentino = pagedep->pd_ino; 11958 lbn = pagedep->pd_lbn; 11959 if ((dap->da_state & (MKDIR_BODY | COMPLETE)) != COMPLETE) 11960 panic("softdep_fsync: dirty"); 11961 if ((dap->da_state & MKDIR_PARENT) || 11962 (pagedep->pd_state & NEWBLOCK)) 11963 flushparent = 1; 11964 else 11965 flushparent = 0; 11966 /* 11967 * If we are being fsync'ed as part of vgone'ing this vnode, 11968 * then we will not be able to release and recover the 11969 * vnode below, so we just have to give up on writing its 11970 * directory entry out. It will eventually be written, just 11971 * not now, but then the user was not asking to have it 11972 * written, so we are not breaking any promises. 11973 */ 11974 if (vp->v_iflag & VI_DOOMED) 11975 break; 11976 /* 11977 * We prevent deadlock by always fetching inodes from the 11978 * root, moving down the directory tree. Thus, when fetching 11979 * our parent directory, we first try to get the lock. If 11980 * that fails, we must unlock ourselves before requesting 11981 * the lock on our parent. See the comment in ufs_lookup 11982 * for details on possible races. 11983 */ 11984 FREE_LOCK(&lk); 11985 if (ffs_vgetf(mp, parentino, LK_NOWAIT | LK_EXCLUSIVE, &pvp, 11986 FFSV_FORCEINSMQ)) { 11987 error = vfs_busy(mp, MBF_NOWAIT); 11988 if (error != 0) { 11989 vfs_ref(mp); 11990 VOP_UNLOCK(vp, 0); 11991 error = vfs_busy(mp, 0); 11992 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 11993 vfs_rel(mp); 11994 if (error != 0) 11995 return (ENOENT); 11996 if (vp->v_iflag & VI_DOOMED) { 11997 vfs_unbusy(mp); 11998 return (ENOENT); 11999 } 12000 } 12001 VOP_UNLOCK(vp, 0); 12002 error = ffs_vgetf(mp, parentino, LK_EXCLUSIVE, 12003 &pvp, FFSV_FORCEINSMQ); 12004 vfs_unbusy(mp); 12005 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 12006 if (vp->v_iflag & VI_DOOMED) { 12007 if (error == 0) 12008 vput(pvp); 12009 error = ENOENT; 12010 } 12011 if (error != 0) 12012 return (error); 12013 } 12014 /* 12015 * All MKDIR_PARENT dependencies and all the NEWBLOCK pagedeps 12016 * that are contained in direct blocks will be resolved by 12017 * doing a ffs_update. Pagedeps contained in indirect blocks 12018 * may require a complete sync'ing of the directory. So, we 12019 * try the cheap and fast ffs_update first, and if that fails, 12020 * then we do the slower ffs_syncvnode of the directory. 12021 */ 12022 if (flushparent) { 12023 int locked; 12024 12025 if ((error = ffs_update(pvp, 1)) != 0) { 12026 vput(pvp); 12027 return (error); 12028 } 12029 ACQUIRE_LOCK(&lk); 12030 locked = 1; 12031 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) != 0) { 12032 if ((wk = LIST_FIRST(&inodedep->id_pendinghd)) != NULL) { 12033 if (wk->wk_type != D_DIRADD) 12034 panic("softdep_fsync: Unexpected type %s", 12035 TYPENAME(wk->wk_type)); 12036 dap = WK_DIRADD(wk); 12037 if (dap->da_state & DIRCHG) 12038 pagedep = dap->da_previous->dm_pagedep; 12039 else 12040 pagedep = dap->da_pagedep; 12041 pagedep_new_block = pagedep->pd_state & NEWBLOCK; 12042 FREE_LOCK(&lk); 12043 locked = 0; 12044 if (pagedep_new_block && (error = 12045 ffs_syncvnode(pvp, MNT_WAIT, 0))) { 12046 vput(pvp); 12047 return (error); 12048 } 12049 } 12050 } 12051 if (locked) 12052 FREE_LOCK(&lk); 12053 } 12054 /* 12055 * Flush directory page containing the inode's name. 12056 */ 12057 error = bread(pvp, lbn, blksize(fs, VTOI(pvp), lbn), td->td_ucred, 12058 &bp); 12059 if (error == 0) 12060 error = bwrite(bp); 12061 else 12062 brelse(bp); 12063 vput(pvp); 12064 if (error != 0) 12065 return (error); 12066 ACQUIRE_LOCK(&lk); 12067 if (inodedep_lookup(mp, ip->i_number, 0, &inodedep) == 0) 12068 break; 12069 } 12070 FREE_LOCK(&lk); 12071 return (0); 12072 } 12073 12074 /* 12075 * Flush all the dirty bitmaps associated with the block device 12076 * before flushing the rest of the dirty blocks so as to reduce 12077 * the number of dependencies that will have to be rolled back. 12078 * 12079 * XXX Unused? 12080 */ 12081 void 12082 softdep_fsync_mountdev(vp) 12083 struct vnode *vp; 12084 { 12085 struct buf *bp, *nbp; 12086 struct worklist *wk; 12087 struct bufobj *bo; 12088 12089 if (!vn_isdisk(vp, NULL)) 12090 panic("softdep_fsync_mountdev: vnode not a disk"); 12091 bo = &vp->v_bufobj; 12092 restart: 12093 BO_LOCK(bo); 12094 ACQUIRE_LOCK(&lk); 12095 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) { 12096 /* 12097 * If it is already scheduled, skip to the next buffer. 12098 */ 12099 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) 12100 continue; 12101 12102 if ((bp->b_flags & B_DELWRI) == 0) 12103 panic("softdep_fsync_mountdev: not dirty"); 12104 /* 12105 * We are only interested in bitmaps with outstanding 12106 * dependencies. 12107 */ 12108 if ((wk = LIST_FIRST(&bp->b_dep)) == NULL || 12109 wk->wk_type != D_BMSAFEMAP || 12110 (bp->b_vflags & BV_BKGRDINPROG)) { 12111 BUF_UNLOCK(bp); 12112 continue; 12113 } 12114 FREE_LOCK(&lk); 12115 BO_UNLOCK(bo); 12116 bremfree(bp); 12117 (void) bawrite(bp); 12118 goto restart; 12119 } 12120 FREE_LOCK(&lk); 12121 drain_output(vp); 12122 BO_UNLOCK(bo); 12123 } 12124 12125 /* 12126 * Sync all cylinder groups that were dirty at the time this function is 12127 * called. Newly dirtied cgs will be inserted before the sintenel. This 12128 * is used to flush freedep activity that may be holding up writes to a 12129 * indirect block. 12130 */ 12131 static int 12132 sync_cgs(mp, waitfor) 12133 struct mount *mp; 12134 int waitfor; 12135 { 12136 struct bmsafemap *bmsafemap; 12137 struct bmsafemap *sintenel; 12138 struct ufsmount *ump; 12139 struct buf *bp; 12140 int error; 12141 12142 sintenel = malloc(sizeof(*sintenel), M_BMSAFEMAP, M_ZERO | M_WAITOK); 12143 sintenel->sm_cg = -1; 12144 ump = VFSTOUFS(mp); 12145 error = 0; 12146 ACQUIRE_LOCK(&lk); 12147 LIST_INSERT_HEAD(&ump->softdep_dirtycg, sintenel, sm_next); 12148 for (bmsafemap = LIST_NEXT(sintenel, sm_next); bmsafemap != NULL; 12149 bmsafemap = LIST_NEXT(sintenel, sm_next)) { 12150 /* Skip sintenels and cgs with no work to release. */ 12151 if (bmsafemap->sm_cg == -1 || 12152 (LIST_EMPTY(&bmsafemap->sm_freehd) && 12153 LIST_EMPTY(&bmsafemap->sm_freewr))) { 12154 LIST_REMOVE(sintenel, sm_next); 12155 LIST_INSERT_AFTER(bmsafemap, sintenel, sm_next); 12156 continue; 12157 } 12158 /* 12159 * If we don't get the lock and we're waiting try again, if 12160 * not move on to the next buf and try to sync it. 12161 */ 12162 bp = getdirtybuf(bmsafemap->sm_buf, &lk, waitfor); 12163 if (bp == NULL && waitfor == MNT_WAIT) 12164 continue; 12165 LIST_REMOVE(sintenel, sm_next); 12166 LIST_INSERT_AFTER(bmsafemap, sintenel, sm_next); 12167 if (bp == NULL) 12168 continue; 12169 FREE_LOCK(&lk); 12170 if (waitfor == MNT_NOWAIT) 12171 bawrite(bp); 12172 else 12173 error = bwrite(bp); 12174 ACQUIRE_LOCK(&lk); 12175 if (error) 12176 break; 12177 } 12178 LIST_REMOVE(sintenel, sm_next); 12179 FREE_LOCK(&lk); 12180 free(sintenel, M_BMSAFEMAP); 12181 return (error); 12182 } 12183 12184 /* 12185 * This routine is called when we are trying to synchronously flush a 12186 * file. This routine must eliminate any filesystem metadata dependencies 12187 * so that the syncing routine can succeed. 12188 */ 12189 int 12190 softdep_sync_metadata(struct vnode *vp) 12191 { 12192 int error; 12193 12194 /* 12195 * Ensure that any direct block dependencies have been cleared, 12196 * truncations are started, and inode references are journaled. 12197 */ 12198 ACQUIRE_LOCK(&lk); 12199 /* 12200 * Write all journal records to prevent rollbacks on devvp. 12201 */ 12202 if (vp->v_type == VCHR) 12203 softdep_flushjournal(vp->v_mount); 12204 error = flush_inodedep_deps(vp, vp->v_mount, VTOI(vp)->i_number); 12205 /* 12206 * Ensure that all truncates are written so we won't find deps on 12207 * indirect blocks. 12208 */ 12209 process_truncates(vp); 12210 FREE_LOCK(&lk); 12211 12212 return (error); 12213 } 12214 12215 /* 12216 * This routine is called when we are attempting to sync a buf with 12217 * dependencies. If waitfor is MNT_NOWAIT it attempts to schedule any 12218 * other IO it can but returns EBUSY if the buffer is not yet able to 12219 * be written. Dependencies which will not cause rollbacks will always 12220 * return 0. 12221 */ 12222 int 12223 softdep_sync_buf(struct vnode *vp, struct buf *bp, int waitfor) 12224 { 12225 struct indirdep *indirdep; 12226 struct pagedep *pagedep; 12227 struct allocindir *aip; 12228 struct newblk *newblk; 12229 struct buf *nbp; 12230 struct worklist *wk; 12231 int i, error; 12232 12233 /* 12234 * For VCHR we just don't want to force flush any dependencies that 12235 * will cause rollbacks. 12236 */ 12237 if (vp->v_type == VCHR) { 12238 if (waitfor == MNT_NOWAIT && softdep_count_dependencies(bp, 0)) 12239 return (EBUSY); 12240 return (0); 12241 } 12242 ACQUIRE_LOCK(&lk); 12243 /* 12244 * As we hold the buffer locked, none of its dependencies 12245 * will disappear. 12246 */ 12247 error = 0; 12248 top: 12249 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 12250 switch (wk->wk_type) { 12251 12252 case D_ALLOCDIRECT: 12253 case D_ALLOCINDIR: 12254 newblk = WK_NEWBLK(wk); 12255 if (newblk->nb_jnewblk != NULL) { 12256 if (waitfor == MNT_NOWAIT) { 12257 error = EBUSY; 12258 goto out_unlock; 12259 } 12260 jwait(&newblk->nb_jnewblk->jn_list, waitfor); 12261 goto top; 12262 } 12263 if (newblk->nb_state & DEPCOMPLETE || 12264 waitfor == MNT_NOWAIT) 12265 continue; 12266 nbp = newblk->nb_bmsafemap->sm_buf; 12267 nbp = getdirtybuf(nbp, &lk, waitfor); 12268 if (nbp == NULL) 12269 goto top; 12270 FREE_LOCK(&lk); 12271 if ((error = bwrite(nbp)) != 0) 12272 goto out; 12273 ACQUIRE_LOCK(&lk); 12274 continue; 12275 12276 case D_INDIRDEP: 12277 indirdep = WK_INDIRDEP(wk); 12278 if (waitfor == MNT_NOWAIT) { 12279 if (!TAILQ_EMPTY(&indirdep->ir_trunc) || 12280 !LIST_EMPTY(&indirdep->ir_deplisthd)) { 12281 error = EBUSY; 12282 goto out_unlock; 12283 } 12284 } 12285 if (!TAILQ_EMPTY(&indirdep->ir_trunc)) 12286 panic("softdep_sync_buf: truncation pending."); 12287 restart: 12288 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 12289 newblk = (struct newblk *)aip; 12290 if (newblk->nb_jnewblk != NULL) { 12291 jwait(&newblk->nb_jnewblk->jn_list, 12292 waitfor); 12293 goto restart; 12294 } 12295 if (newblk->nb_state & DEPCOMPLETE) 12296 continue; 12297 nbp = newblk->nb_bmsafemap->sm_buf; 12298 nbp = getdirtybuf(nbp, &lk, waitfor); 12299 if (nbp == NULL) 12300 goto restart; 12301 FREE_LOCK(&lk); 12302 if ((error = bwrite(nbp)) != 0) 12303 goto out; 12304 ACQUIRE_LOCK(&lk); 12305 goto restart; 12306 } 12307 continue; 12308 12309 case D_PAGEDEP: 12310 /* 12311 * Only flush directory entries in synchronous passes. 12312 */ 12313 if (waitfor != MNT_WAIT) { 12314 error = EBUSY; 12315 goto out_unlock; 12316 } 12317 /* 12318 * While syncing snapshots, we must allow recursive 12319 * lookups. 12320 */ 12321 BUF_AREC(bp); 12322 /* 12323 * We are trying to sync a directory that may 12324 * have dependencies on both its own metadata 12325 * and/or dependencies on the inodes of any 12326 * recently allocated files. We walk its diradd 12327 * lists pushing out the associated inode. 12328 */ 12329 pagedep = WK_PAGEDEP(wk); 12330 for (i = 0; i < DAHASHSZ; i++) { 12331 if (LIST_FIRST(&pagedep->pd_diraddhd[i]) == 0) 12332 continue; 12333 if ((error = flush_pagedep_deps(vp, wk->wk_mp, 12334 &pagedep->pd_diraddhd[i]))) { 12335 BUF_NOREC(bp); 12336 goto out_unlock; 12337 } 12338 } 12339 BUF_NOREC(bp); 12340 continue; 12341 12342 case D_FREEWORK: 12343 case D_FREEDEP: 12344 case D_JSEGDEP: 12345 case D_JNEWBLK: 12346 continue; 12347 12348 default: 12349 panic("softdep_sync_buf: Unknown type %s", 12350 TYPENAME(wk->wk_type)); 12351 /* NOTREACHED */ 12352 } 12353 } 12354 out_unlock: 12355 FREE_LOCK(&lk); 12356 out: 12357 return (error); 12358 } 12359 12360 /* 12361 * Flush the dependencies associated with an inodedep. 12362 * Called with splbio blocked. 12363 */ 12364 static int 12365 flush_inodedep_deps(vp, mp, ino) 12366 struct vnode *vp; 12367 struct mount *mp; 12368 ino_t ino; 12369 { 12370 struct inodedep *inodedep; 12371 struct inoref *inoref; 12372 int error, waitfor; 12373 12374 /* 12375 * This work is done in two passes. The first pass grabs most 12376 * of the buffers and begins asynchronously writing them. The 12377 * only way to wait for these asynchronous writes is to sleep 12378 * on the filesystem vnode which may stay busy for a long time 12379 * if the filesystem is active. So, instead, we make a second 12380 * pass over the dependencies blocking on each write. In the 12381 * usual case we will be blocking against a write that we 12382 * initiated, so when it is done the dependency will have been 12383 * resolved. Thus the second pass is expected to end quickly. 12384 * We give a brief window at the top of the loop to allow 12385 * any pending I/O to complete. 12386 */ 12387 for (error = 0, waitfor = MNT_NOWAIT; ; ) { 12388 if (error) 12389 return (error); 12390 FREE_LOCK(&lk); 12391 ACQUIRE_LOCK(&lk); 12392 restart: 12393 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 12394 return (0); 12395 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12396 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12397 == DEPCOMPLETE) { 12398 jwait(&inoref->if_list, MNT_WAIT); 12399 goto restart; 12400 } 12401 } 12402 if (flush_deplist(&inodedep->id_inoupdt, waitfor, &error) || 12403 flush_deplist(&inodedep->id_newinoupdt, waitfor, &error) || 12404 flush_deplist(&inodedep->id_extupdt, waitfor, &error) || 12405 flush_deplist(&inodedep->id_newextupdt, waitfor, &error)) 12406 continue; 12407 /* 12408 * If pass2, we are done, otherwise do pass 2. 12409 */ 12410 if (waitfor == MNT_WAIT) 12411 break; 12412 waitfor = MNT_WAIT; 12413 } 12414 /* 12415 * Try freeing inodedep in case all dependencies have been removed. 12416 */ 12417 if (inodedep_lookup(mp, ino, 0, &inodedep) != 0) 12418 (void) free_inodedep(inodedep); 12419 return (0); 12420 } 12421 12422 /* 12423 * Flush an inode dependency list. 12424 * Called with splbio blocked. 12425 */ 12426 static int 12427 flush_deplist(listhead, waitfor, errorp) 12428 struct allocdirectlst *listhead; 12429 int waitfor; 12430 int *errorp; 12431 { 12432 struct allocdirect *adp; 12433 struct newblk *newblk; 12434 struct buf *bp; 12435 12436 mtx_assert(&lk, MA_OWNED); 12437 TAILQ_FOREACH(adp, listhead, ad_next) { 12438 newblk = (struct newblk *)adp; 12439 if (newblk->nb_jnewblk != NULL) { 12440 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12441 return (1); 12442 } 12443 if (newblk->nb_state & DEPCOMPLETE) 12444 continue; 12445 bp = newblk->nb_bmsafemap->sm_buf; 12446 bp = getdirtybuf(bp, &lk, waitfor); 12447 if (bp == NULL) { 12448 if (waitfor == MNT_NOWAIT) 12449 continue; 12450 return (1); 12451 } 12452 FREE_LOCK(&lk); 12453 if (waitfor == MNT_NOWAIT) 12454 bawrite(bp); 12455 else 12456 *errorp = bwrite(bp); 12457 ACQUIRE_LOCK(&lk); 12458 return (1); 12459 } 12460 return (0); 12461 } 12462 12463 /* 12464 * Flush dependencies associated with an allocdirect block. 12465 */ 12466 static int 12467 flush_newblk_dep(vp, mp, lbn) 12468 struct vnode *vp; 12469 struct mount *mp; 12470 ufs_lbn_t lbn; 12471 { 12472 struct newblk *newblk; 12473 struct bufobj *bo; 12474 struct inode *ip; 12475 struct buf *bp; 12476 ufs2_daddr_t blkno; 12477 int error; 12478 12479 error = 0; 12480 bo = &vp->v_bufobj; 12481 ip = VTOI(vp); 12482 blkno = DIP(ip, i_db[lbn]); 12483 if (blkno == 0) 12484 panic("flush_newblk_dep: Missing block"); 12485 ACQUIRE_LOCK(&lk); 12486 /* 12487 * Loop until all dependencies related to this block are satisfied. 12488 * We must be careful to restart after each sleep in case a write 12489 * completes some part of this process for us. 12490 */ 12491 for (;;) { 12492 if (newblk_lookup(mp, blkno, 0, &newblk) == 0) { 12493 FREE_LOCK(&lk); 12494 break; 12495 } 12496 if (newblk->nb_list.wk_type != D_ALLOCDIRECT) 12497 panic("flush_newblk_deps: Bad newblk %p", newblk); 12498 /* 12499 * Flush the journal. 12500 */ 12501 if (newblk->nb_jnewblk != NULL) { 12502 jwait(&newblk->nb_jnewblk->jn_list, MNT_WAIT); 12503 continue; 12504 } 12505 /* 12506 * Write the bitmap dependency. 12507 */ 12508 if ((newblk->nb_state & DEPCOMPLETE) == 0) { 12509 bp = newblk->nb_bmsafemap->sm_buf; 12510 bp = getdirtybuf(bp, &lk, MNT_WAIT); 12511 if (bp == NULL) 12512 continue; 12513 FREE_LOCK(&lk); 12514 error = bwrite(bp); 12515 if (error) 12516 break; 12517 ACQUIRE_LOCK(&lk); 12518 continue; 12519 } 12520 /* 12521 * Write the buffer. 12522 */ 12523 FREE_LOCK(&lk); 12524 BO_LOCK(bo); 12525 bp = gbincore(bo, lbn); 12526 if (bp != NULL) { 12527 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 12528 LK_INTERLOCK, BO_MTX(bo)); 12529 if (error == ENOLCK) { 12530 ACQUIRE_LOCK(&lk); 12531 continue; /* Slept, retry */ 12532 } 12533 if (error != 0) 12534 break; /* Failed */ 12535 if (bp->b_flags & B_DELWRI) { 12536 bremfree(bp); 12537 error = bwrite(bp); 12538 if (error) 12539 break; 12540 } else 12541 BUF_UNLOCK(bp); 12542 } else 12543 BO_UNLOCK(bo); 12544 /* 12545 * We have to wait for the direct pointers to 12546 * point at the newdirblk before the dependency 12547 * will go away. 12548 */ 12549 error = ffs_update(vp, 1); 12550 if (error) 12551 break; 12552 ACQUIRE_LOCK(&lk); 12553 } 12554 return (error); 12555 } 12556 12557 /* 12558 * Eliminate a pagedep dependency by flushing out all its diradd dependencies. 12559 * Called with splbio blocked. 12560 */ 12561 static int 12562 flush_pagedep_deps(pvp, mp, diraddhdp) 12563 struct vnode *pvp; 12564 struct mount *mp; 12565 struct diraddhd *diraddhdp; 12566 { 12567 struct inodedep *inodedep; 12568 struct inoref *inoref; 12569 struct ufsmount *ump; 12570 struct diradd *dap; 12571 struct vnode *vp; 12572 int error = 0; 12573 struct buf *bp; 12574 ino_t inum; 12575 12576 ump = VFSTOUFS(mp); 12577 restart: 12578 while ((dap = LIST_FIRST(diraddhdp)) != NULL) { 12579 /* 12580 * Flush ourselves if this directory entry 12581 * has a MKDIR_PARENT dependency. 12582 */ 12583 if (dap->da_state & MKDIR_PARENT) { 12584 FREE_LOCK(&lk); 12585 if ((error = ffs_update(pvp, 1)) != 0) 12586 break; 12587 ACQUIRE_LOCK(&lk); 12588 /* 12589 * If that cleared dependencies, go on to next. 12590 */ 12591 if (dap != LIST_FIRST(diraddhdp)) 12592 continue; 12593 if (dap->da_state & MKDIR_PARENT) 12594 panic("flush_pagedep_deps: MKDIR_PARENT"); 12595 } 12596 /* 12597 * A newly allocated directory must have its "." and 12598 * ".." entries written out before its name can be 12599 * committed in its parent. 12600 */ 12601 inum = dap->da_newinum; 12602 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12603 panic("flush_pagedep_deps: lost inode1"); 12604 /* 12605 * Wait for any pending journal adds to complete so we don't 12606 * cause rollbacks while syncing. 12607 */ 12608 TAILQ_FOREACH(inoref, &inodedep->id_inoreflst, if_deps) { 12609 if ((inoref->if_state & (DEPCOMPLETE | GOINGAWAY)) 12610 == DEPCOMPLETE) { 12611 jwait(&inoref->if_list, MNT_WAIT); 12612 goto restart; 12613 } 12614 } 12615 if (dap->da_state & MKDIR_BODY) { 12616 FREE_LOCK(&lk); 12617 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12618 FFSV_FORCEINSMQ))) 12619 break; 12620 error = flush_newblk_dep(vp, mp, 0); 12621 /* 12622 * If we still have the dependency we might need to 12623 * update the vnode to sync the new link count to 12624 * disk. 12625 */ 12626 if (error == 0 && dap == LIST_FIRST(diraddhdp)) 12627 error = ffs_update(vp, 1); 12628 vput(vp); 12629 if (error != 0) 12630 break; 12631 ACQUIRE_LOCK(&lk); 12632 /* 12633 * If that cleared dependencies, go on to next. 12634 */ 12635 if (dap != LIST_FIRST(diraddhdp)) 12636 continue; 12637 if (dap->da_state & MKDIR_BODY) { 12638 inodedep_lookup(UFSTOVFS(ump), inum, 0, 12639 &inodedep); 12640 panic("flush_pagedep_deps: MKDIR_BODY " 12641 "inodedep %p dap %p vp %p", 12642 inodedep, dap, vp); 12643 } 12644 } 12645 /* 12646 * Flush the inode on which the directory entry depends. 12647 * Having accounted for MKDIR_PARENT and MKDIR_BODY above, 12648 * the only remaining dependency is that the updated inode 12649 * count must get pushed to disk. The inode has already 12650 * been pushed into its inode buffer (via VOP_UPDATE) at 12651 * the time of the reference count change. So we need only 12652 * locate that buffer, ensure that there will be no rollback 12653 * caused by a bitmap dependency, then write the inode buffer. 12654 */ 12655 retry: 12656 if (inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep) == 0) 12657 panic("flush_pagedep_deps: lost inode"); 12658 /* 12659 * If the inode still has bitmap dependencies, 12660 * push them to disk. 12661 */ 12662 if ((inodedep->id_state & (DEPCOMPLETE | GOINGAWAY)) == 0) { 12663 bp = inodedep->id_bmsafemap->sm_buf; 12664 bp = getdirtybuf(bp, &lk, MNT_WAIT); 12665 if (bp == NULL) 12666 goto retry; 12667 FREE_LOCK(&lk); 12668 if ((error = bwrite(bp)) != 0) 12669 break; 12670 ACQUIRE_LOCK(&lk); 12671 if (dap != LIST_FIRST(diraddhdp)) 12672 continue; 12673 } 12674 /* 12675 * If the inode is still sitting in a buffer waiting 12676 * to be written or waiting for the link count to be 12677 * adjusted update it here to flush it to disk. 12678 */ 12679 if (dap == LIST_FIRST(diraddhdp)) { 12680 FREE_LOCK(&lk); 12681 if ((error = ffs_vgetf(mp, inum, LK_EXCLUSIVE, &vp, 12682 FFSV_FORCEINSMQ))) 12683 break; 12684 error = ffs_update(vp, 1); 12685 vput(vp); 12686 if (error) 12687 break; 12688 ACQUIRE_LOCK(&lk); 12689 } 12690 /* 12691 * If we have failed to get rid of all the dependencies 12692 * then something is seriously wrong. 12693 */ 12694 if (dap == LIST_FIRST(diraddhdp)) { 12695 inodedep_lookup(UFSTOVFS(ump), inum, 0, &inodedep); 12696 panic("flush_pagedep_deps: failed to flush " 12697 "inodedep %p ino %ju dap %p", 12698 inodedep, (uintmax_t)inum, dap); 12699 } 12700 } 12701 if (error) 12702 ACQUIRE_LOCK(&lk); 12703 return (error); 12704 } 12705 12706 /* 12707 * A large burst of file addition or deletion activity can drive the 12708 * memory load excessively high. First attempt to slow things down 12709 * using the techniques below. If that fails, this routine requests 12710 * the offending operations to fall back to running synchronously 12711 * until the memory load returns to a reasonable level. 12712 */ 12713 int 12714 softdep_slowdown(vp) 12715 struct vnode *vp; 12716 { 12717 struct ufsmount *ump; 12718 int jlow; 12719 int max_softdeps_hard; 12720 12721 ACQUIRE_LOCK(&lk); 12722 jlow = 0; 12723 /* 12724 * Check for journal space if needed. 12725 */ 12726 if (DOINGSUJ(vp)) { 12727 ump = VFSTOUFS(vp->v_mount); 12728 if (journal_space(ump, 0) == 0) 12729 jlow = 1; 12730 } 12731 max_softdeps_hard = max_softdeps * 11 / 10; 12732 if (dep_current[D_DIRREM] < max_softdeps_hard / 2 && 12733 dep_current[D_INODEDEP] < max_softdeps_hard && 12734 VFSTOUFS(vp->v_mount)->um_numindirdeps < maxindirdeps && 12735 dep_current[D_FREEBLKS] < max_softdeps_hard && jlow == 0) { 12736 FREE_LOCK(&lk); 12737 return (0); 12738 } 12739 if (VFSTOUFS(vp->v_mount)->um_numindirdeps >= maxindirdeps || jlow) 12740 softdep_speedup(); 12741 stat_sync_limit_hit += 1; 12742 FREE_LOCK(&lk); 12743 if (DOINGSUJ(vp)) 12744 return (0); 12745 return (1); 12746 } 12747 12748 /* 12749 * Called by the allocation routines when they are about to fail 12750 * in the hope that we can free up the requested resource (inodes 12751 * or disk space). 12752 * 12753 * First check to see if the work list has anything on it. If it has, 12754 * clean up entries until we successfully free the requested resource. 12755 * Because this process holds inodes locked, we cannot handle any remove 12756 * requests that might block on a locked inode as that could lead to 12757 * deadlock. If the worklist yields none of the requested resource, 12758 * start syncing out vnodes to free up the needed space. 12759 */ 12760 int 12761 softdep_request_cleanup(fs, vp, cred, resource) 12762 struct fs *fs; 12763 struct vnode *vp; 12764 struct ucred *cred; 12765 int resource; 12766 { 12767 struct ufsmount *ump; 12768 struct mount *mp; 12769 struct vnode *lvp, *mvp; 12770 long starttime; 12771 ufs2_daddr_t needed; 12772 int error; 12773 12774 /* 12775 * If we are being called because of a process doing a 12776 * copy-on-write, then it is not safe to process any 12777 * worklist items as we will recurse into the copyonwrite 12778 * routine. This will result in an incoherent snapshot. 12779 * If the vnode that we hold is a snapshot, we must avoid 12780 * handling other resources that could cause deadlock. 12781 */ 12782 if ((curthread->td_pflags & TDP_COWINPROGRESS) || IS_SNAPSHOT(VTOI(vp))) 12783 return (0); 12784 12785 if (resource == FLUSH_BLOCKS_WAIT) 12786 stat_cleanup_blkrequests += 1; 12787 else 12788 stat_cleanup_inorequests += 1; 12789 12790 mp = vp->v_mount; 12791 ump = VFSTOUFS(mp); 12792 mtx_assert(UFS_MTX(ump), MA_OWNED); 12793 UFS_UNLOCK(ump); 12794 error = ffs_update(vp, 1); 12795 if (error != 0) { 12796 UFS_LOCK(ump); 12797 return (0); 12798 } 12799 /* 12800 * If we are in need of resources, consider pausing for 12801 * tickdelay to give ourselves some breathing room. 12802 */ 12803 ACQUIRE_LOCK(&lk); 12804 process_removes(vp); 12805 process_truncates(vp); 12806 request_cleanup(UFSTOVFS(ump), resource); 12807 FREE_LOCK(&lk); 12808 /* 12809 * Now clean up at least as many resources as we will need. 12810 * 12811 * When requested to clean up inodes, the number that are needed 12812 * is set by the number of simultaneous writers (mnt_writeopcount) 12813 * plus a bit of slop (2) in case some more writers show up while 12814 * we are cleaning. 12815 * 12816 * When requested to free up space, the amount of space that 12817 * we need is enough blocks to allocate a full-sized segment 12818 * (fs_contigsumsize). The number of such segments that will 12819 * be needed is set by the number of simultaneous writers 12820 * (mnt_writeopcount) plus a bit of slop (2) in case some more 12821 * writers show up while we are cleaning. 12822 * 12823 * Additionally, if we are unpriviledged and allocating space, 12824 * we need to ensure that we clean up enough blocks to get the 12825 * needed number of blocks over the threshhold of the minimum 12826 * number of blocks required to be kept free by the filesystem 12827 * (fs_minfree). 12828 */ 12829 if (resource == FLUSH_INODES_WAIT) { 12830 needed = vp->v_mount->mnt_writeopcount + 2; 12831 } else if (resource == FLUSH_BLOCKS_WAIT) { 12832 needed = (vp->v_mount->mnt_writeopcount + 2) * 12833 fs->fs_contigsumsize; 12834 if (priv_check_cred(cred, PRIV_VFS_BLOCKRESERVE, 0)) 12835 needed += fragstoblks(fs, 12836 roundup((fs->fs_dsize * fs->fs_minfree / 100) - 12837 fs->fs_cstotal.cs_nffree, fs->fs_frag)); 12838 } else { 12839 UFS_LOCK(ump); 12840 printf("softdep_request_cleanup: Unknown resource type %d\n", 12841 resource); 12842 return (0); 12843 } 12844 starttime = time_second; 12845 retry: 12846 if ((resource == FLUSH_BLOCKS_WAIT && ump->softdep_on_worklist > 0 && 12847 fs->fs_cstotal.cs_nbfree <= needed) || 12848 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 12849 fs->fs_cstotal.cs_nifree <= needed)) { 12850 ACQUIRE_LOCK(&lk); 12851 if (ump->softdep_on_worklist > 0 && 12852 process_worklist_item(UFSTOVFS(ump), 12853 ump->softdep_on_worklist, LK_NOWAIT) != 0) 12854 stat_worklist_push += 1; 12855 FREE_LOCK(&lk); 12856 } 12857 /* 12858 * If we still need resources and there are no more worklist 12859 * entries to process to obtain them, we have to start flushing 12860 * the dirty vnodes to force the release of additional requests 12861 * to the worklist that we can then process to reap addition 12862 * resources. We walk the vnodes associated with the mount point 12863 * until we get the needed worklist requests that we can reap. 12864 */ 12865 if ((resource == FLUSH_BLOCKS_WAIT && 12866 fs->fs_cstotal.cs_nbfree <= needed) || 12867 (resource == FLUSH_INODES_WAIT && fs->fs_pendinginodes > 0 && 12868 fs->fs_cstotal.cs_nifree <= needed)) { 12869 MNT_VNODE_FOREACH_ALL(lvp, mp, mvp) { 12870 if (TAILQ_FIRST(&lvp->v_bufobj.bo_dirty.bv_hd) == 0) { 12871 VI_UNLOCK(lvp); 12872 continue; 12873 } 12874 if (vget(lvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT, 12875 curthread)) 12876 continue; 12877 if (lvp->v_vflag & VV_NOSYNC) { /* unlinked */ 12878 vput(lvp); 12879 continue; 12880 } 12881 (void) ffs_syncvnode(lvp, MNT_NOWAIT, 0); 12882 vput(lvp); 12883 } 12884 lvp = ump->um_devvp; 12885 if (vn_lock(lvp, LK_EXCLUSIVE | LK_NOWAIT) == 0) { 12886 VOP_FSYNC(lvp, MNT_NOWAIT, curthread); 12887 VOP_UNLOCK(lvp, 0); 12888 } 12889 if (ump->softdep_on_worklist > 0) { 12890 stat_cleanup_retries += 1; 12891 goto retry; 12892 } 12893 stat_cleanup_failures += 1; 12894 } 12895 if (time_second - starttime > stat_cleanup_high_delay) 12896 stat_cleanup_high_delay = time_second - starttime; 12897 UFS_LOCK(ump); 12898 return (1); 12899 } 12900 12901 /* 12902 * If memory utilization has gotten too high, deliberately slow things 12903 * down and speed up the I/O processing. 12904 */ 12905 extern struct thread *syncertd; 12906 static int 12907 request_cleanup(mp, resource) 12908 struct mount *mp; 12909 int resource; 12910 { 12911 struct thread *td = curthread; 12912 struct ufsmount *ump; 12913 12914 mtx_assert(&lk, MA_OWNED); 12915 /* 12916 * We never hold up the filesystem syncer or buf daemon. 12917 */ 12918 if (td->td_pflags & (TDP_SOFTDEP|TDP_NORUNNINGBUF)) 12919 return (0); 12920 ump = VFSTOUFS(mp); 12921 /* 12922 * First check to see if the work list has gotten backlogged. 12923 * If it has, co-opt this process to help clean up two entries. 12924 * Because this process may hold inodes locked, we cannot 12925 * handle any remove requests that might block on a locked 12926 * inode as that could lead to deadlock. We set TDP_SOFTDEP 12927 * to avoid recursively processing the worklist. 12928 */ 12929 if (ump->softdep_on_worklist > max_softdeps / 10) { 12930 td->td_pflags |= TDP_SOFTDEP; 12931 process_worklist_item(mp, 2, LK_NOWAIT); 12932 td->td_pflags &= ~TDP_SOFTDEP; 12933 stat_worklist_push += 2; 12934 return(1); 12935 } 12936 /* 12937 * Next, we attempt to speed up the syncer process. If that 12938 * is successful, then we allow the process to continue. 12939 */ 12940 if (softdep_speedup() && 12941 resource != FLUSH_BLOCKS_WAIT && 12942 resource != FLUSH_INODES_WAIT) 12943 return(0); 12944 /* 12945 * If we are resource constrained on inode dependencies, try 12946 * flushing some dirty inodes. Otherwise, we are constrained 12947 * by file deletions, so try accelerating flushes of directories 12948 * with removal dependencies. We would like to do the cleanup 12949 * here, but we probably hold an inode locked at this point and 12950 * that might deadlock against one that we try to clean. So, 12951 * the best that we can do is request the syncer daemon to do 12952 * the cleanup for us. 12953 */ 12954 switch (resource) { 12955 12956 case FLUSH_INODES: 12957 case FLUSH_INODES_WAIT: 12958 stat_ino_limit_push += 1; 12959 req_clear_inodedeps += 1; 12960 stat_countp = &stat_ino_limit_hit; 12961 break; 12962 12963 case FLUSH_BLOCKS: 12964 case FLUSH_BLOCKS_WAIT: 12965 stat_blk_limit_push += 1; 12966 req_clear_remove += 1; 12967 stat_countp = &stat_blk_limit_hit; 12968 break; 12969 12970 default: 12971 panic("request_cleanup: unknown type"); 12972 } 12973 /* 12974 * Hopefully the syncer daemon will catch up and awaken us. 12975 * We wait at most tickdelay before proceeding in any case. 12976 */ 12977 proc_waiting += 1; 12978 if (callout_pending(&softdep_callout) == FALSE) 12979 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 12980 pause_timer, 0); 12981 12982 msleep((caddr_t)&proc_waiting, &lk, PPAUSE, "softupdate", 0); 12983 proc_waiting -= 1; 12984 return (1); 12985 } 12986 12987 /* 12988 * Awaken processes pausing in request_cleanup and clear proc_waiting 12989 * to indicate that there is no longer a timer running. 12990 */ 12991 static void 12992 pause_timer(arg) 12993 void *arg; 12994 { 12995 12996 /* 12997 * The callout_ API has acquired mtx and will hold it around this 12998 * function call. 12999 */ 13000 *stat_countp += 1; 13001 wakeup_one(&proc_waiting); 13002 if (proc_waiting > 0) 13003 callout_reset(&softdep_callout, tickdelay > 2 ? tickdelay : 2, 13004 pause_timer, 0); 13005 } 13006 13007 /* 13008 * Flush out a directory with at least one removal dependency in an effort to 13009 * reduce the number of dirrem, freefile, and freeblks dependency structures. 13010 */ 13011 static void 13012 clear_remove(void) 13013 { 13014 struct pagedep_hashhead *pagedephd; 13015 struct pagedep *pagedep; 13016 static int next = 0; 13017 struct mount *mp; 13018 struct vnode *vp; 13019 struct bufobj *bo; 13020 int error, cnt; 13021 ino_t ino; 13022 13023 mtx_assert(&lk, MA_OWNED); 13024 13025 for (cnt = 0; cnt < pagedep_hash; cnt++) { 13026 pagedephd = &pagedep_hashtbl[next++]; 13027 if (next >= pagedep_hash) 13028 next = 0; 13029 LIST_FOREACH(pagedep, pagedephd, pd_hash) { 13030 if (LIST_EMPTY(&pagedep->pd_dirremhd)) 13031 continue; 13032 mp = pagedep->pd_list.wk_mp; 13033 ino = pagedep->pd_ino; 13034 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13035 continue; 13036 FREE_LOCK(&lk); 13037 13038 /* 13039 * Let unmount clear deps 13040 */ 13041 error = vfs_busy(mp, MBF_NOWAIT); 13042 if (error != 0) 13043 goto finish_write; 13044 error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13045 FFSV_FORCEINSMQ); 13046 vfs_unbusy(mp); 13047 if (error != 0) { 13048 softdep_error("clear_remove: vget", error); 13049 goto finish_write; 13050 } 13051 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13052 softdep_error("clear_remove: fsync", error); 13053 bo = &vp->v_bufobj; 13054 BO_LOCK(bo); 13055 drain_output(vp); 13056 BO_UNLOCK(bo); 13057 vput(vp); 13058 finish_write: 13059 vn_finished_write(mp); 13060 ACQUIRE_LOCK(&lk); 13061 return; 13062 } 13063 } 13064 } 13065 13066 /* 13067 * Clear out a block of dirty inodes in an effort to reduce 13068 * the number of inodedep dependency structures. 13069 */ 13070 static void 13071 clear_inodedeps(void) 13072 { 13073 struct inodedep_hashhead *inodedephd; 13074 struct inodedep *inodedep; 13075 static int next = 0; 13076 struct mount *mp; 13077 struct vnode *vp; 13078 struct fs *fs; 13079 int error, cnt; 13080 ino_t firstino, lastino, ino; 13081 13082 mtx_assert(&lk, MA_OWNED); 13083 /* 13084 * Pick a random inode dependency to be cleared. 13085 * We will then gather up all the inodes in its block 13086 * that have dependencies and flush them out. 13087 */ 13088 for (cnt = 0; cnt < inodedep_hash; cnt++) { 13089 inodedephd = &inodedep_hashtbl[next++]; 13090 if (next >= inodedep_hash) 13091 next = 0; 13092 if ((inodedep = LIST_FIRST(inodedephd)) != NULL) 13093 break; 13094 } 13095 if (inodedep == NULL) 13096 return; 13097 fs = inodedep->id_fs; 13098 mp = inodedep->id_list.wk_mp; 13099 /* 13100 * Find the last inode in the block with dependencies. 13101 */ 13102 firstino = inodedep->id_ino & ~(INOPB(fs) - 1); 13103 for (lastino = firstino + INOPB(fs) - 1; lastino > firstino; lastino--) 13104 if (inodedep_lookup(mp, lastino, 0, &inodedep) != 0) 13105 break; 13106 /* 13107 * Asynchronously push all but the last inode with dependencies. 13108 * Synchronously push the last inode with dependencies to ensure 13109 * that the inode block gets written to free up the inodedeps. 13110 */ 13111 for (ino = firstino; ino <= lastino; ino++) { 13112 if (inodedep_lookup(mp, ino, 0, &inodedep) == 0) 13113 continue; 13114 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) 13115 continue; 13116 FREE_LOCK(&lk); 13117 error = vfs_busy(mp, MBF_NOWAIT); /* Let unmount clear deps */ 13118 if (error != 0) { 13119 vn_finished_write(mp); 13120 ACQUIRE_LOCK(&lk); 13121 return; 13122 } 13123 if ((error = ffs_vgetf(mp, ino, LK_EXCLUSIVE, &vp, 13124 FFSV_FORCEINSMQ)) != 0) { 13125 softdep_error("clear_inodedeps: vget", error); 13126 vfs_unbusy(mp); 13127 vn_finished_write(mp); 13128 ACQUIRE_LOCK(&lk); 13129 return; 13130 } 13131 vfs_unbusy(mp); 13132 if (ino == lastino) { 13133 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0))) 13134 softdep_error("clear_inodedeps: fsync1", error); 13135 } else { 13136 if ((error = ffs_syncvnode(vp, MNT_NOWAIT, 0))) 13137 softdep_error("clear_inodedeps: fsync2", error); 13138 BO_LOCK(&vp->v_bufobj); 13139 drain_output(vp); 13140 BO_UNLOCK(&vp->v_bufobj); 13141 } 13142 vput(vp); 13143 vn_finished_write(mp); 13144 ACQUIRE_LOCK(&lk); 13145 } 13146 } 13147 13148 void 13149 softdep_buf_append(bp, wkhd) 13150 struct buf *bp; 13151 struct workhead *wkhd; 13152 { 13153 struct worklist *wk; 13154 13155 ACQUIRE_LOCK(&lk); 13156 while ((wk = LIST_FIRST(wkhd)) != NULL) { 13157 WORKLIST_REMOVE(wk); 13158 WORKLIST_INSERT(&bp->b_dep, wk); 13159 } 13160 FREE_LOCK(&lk); 13161 13162 } 13163 13164 void 13165 softdep_inode_append(ip, cred, wkhd) 13166 struct inode *ip; 13167 struct ucred *cred; 13168 struct workhead *wkhd; 13169 { 13170 struct buf *bp; 13171 struct fs *fs; 13172 int error; 13173 13174 fs = ip->i_fs; 13175 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)), 13176 (int)fs->fs_bsize, cred, &bp); 13177 if (error) { 13178 softdep_freework(wkhd); 13179 return; 13180 } 13181 softdep_buf_append(bp, wkhd); 13182 bqrelse(bp); 13183 } 13184 13185 void 13186 softdep_freework(wkhd) 13187 struct workhead *wkhd; 13188 { 13189 13190 ACQUIRE_LOCK(&lk); 13191 handle_jwork(wkhd); 13192 FREE_LOCK(&lk); 13193 } 13194 13195 /* 13196 * Function to determine if the buffer has outstanding dependencies 13197 * that will cause a roll-back if the buffer is written. If wantcount 13198 * is set, return number of dependencies, otherwise just yes or no. 13199 */ 13200 static int 13201 softdep_count_dependencies(bp, wantcount) 13202 struct buf *bp; 13203 int wantcount; 13204 { 13205 struct worklist *wk; 13206 struct bmsafemap *bmsafemap; 13207 struct freework *freework; 13208 struct inodedep *inodedep; 13209 struct indirdep *indirdep; 13210 struct freeblks *freeblks; 13211 struct allocindir *aip; 13212 struct pagedep *pagedep; 13213 struct dirrem *dirrem; 13214 struct newblk *newblk; 13215 struct mkdir *mkdir; 13216 struct diradd *dap; 13217 int i, retval; 13218 13219 retval = 0; 13220 ACQUIRE_LOCK(&lk); 13221 LIST_FOREACH(wk, &bp->b_dep, wk_list) { 13222 switch (wk->wk_type) { 13223 13224 case D_INODEDEP: 13225 inodedep = WK_INODEDEP(wk); 13226 if ((inodedep->id_state & DEPCOMPLETE) == 0) { 13227 /* bitmap allocation dependency */ 13228 retval += 1; 13229 if (!wantcount) 13230 goto out; 13231 } 13232 if (TAILQ_FIRST(&inodedep->id_inoupdt)) { 13233 /* direct block pointer dependency */ 13234 retval += 1; 13235 if (!wantcount) 13236 goto out; 13237 } 13238 if (TAILQ_FIRST(&inodedep->id_extupdt)) { 13239 /* direct block pointer dependency */ 13240 retval += 1; 13241 if (!wantcount) 13242 goto out; 13243 } 13244 if (TAILQ_FIRST(&inodedep->id_inoreflst)) { 13245 /* Add reference dependency. */ 13246 retval += 1; 13247 if (!wantcount) 13248 goto out; 13249 } 13250 continue; 13251 13252 case D_INDIRDEP: 13253 indirdep = WK_INDIRDEP(wk); 13254 13255 TAILQ_FOREACH(freework, &indirdep->ir_trunc, fw_next) { 13256 /* indirect truncation dependency */ 13257 retval += 1; 13258 if (!wantcount) 13259 goto out; 13260 } 13261 13262 LIST_FOREACH(aip, &indirdep->ir_deplisthd, ai_next) { 13263 /* indirect block pointer dependency */ 13264 retval += 1; 13265 if (!wantcount) 13266 goto out; 13267 } 13268 continue; 13269 13270 case D_PAGEDEP: 13271 pagedep = WK_PAGEDEP(wk); 13272 LIST_FOREACH(dirrem, &pagedep->pd_dirremhd, dm_next) { 13273 if (LIST_FIRST(&dirrem->dm_jremrefhd)) { 13274 /* Journal remove ref dependency. */ 13275 retval += 1; 13276 if (!wantcount) 13277 goto out; 13278 } 13279 } 13280 for (i = 0; i < DAHASHSZ; i++) { 13281 13282 LIST_FOREACH(dap, &pagedep->pd_diraddhd[i], da_pdlist) { 13283 /* directory entry dependency */ 13284 retval += 1; 13285 if (!wantcount) 13286 goto out; 13287 } 13288 } 13289 continue; 13290 13291 case D_BMSAFEMAP: 13292 bmsafemap = WK_BMSAFEMAP(wk); 13293 if (LIST_FIRST(&bmsafemap->sm_jaddrefhd)) { 13294 /* Add reference dependency. */ 13295 retval += 1; 13296 if (!wantcount) 13297 goto out; 13298 } 13299 if (LIST_FIRST(&bmsafemap->sm_jnewblkhd)) { 13300 /* Allocate block dependency. */ 13301 retval += 1; 13302 if (!wantcount) 13303 goto out; 13304 } 13305 continue; 13306 13307 case D_FREEBLKS: 13308 freeblks = WK_FREEBLKS(wk); 13309 if (LIST_FIRST(&freeblks->fb_jblkdephd)) { 13310 /* Freeblk journal dependency. */ 13311 retval += 1; 13312 if (!wantcount) 13313 goto out; 13314 } 13315 continue; 13316 13317 case D_ALLOCDIRECT: 13318 case D_ALLOCINDIR: 13319 newblk = WK_NEWBLK(wk); 13320 if (newblk->nb_jnewblk) { 13321 /* Journal allocate dependency. */ 13322 retval += 1; 13323 if (!wantcount) 13324 goto out; 13325 } 13326 continue; 13327 13328 case D_MKDIR: 13329 mkdir = WK_MKDIR(wk); 13330 if (mkdir->md_jaddref) { 13331 /* Journal reference dependency. */ 13332 retval += 1; 13333 if (!wantcount) 13334 goto out; 13335 } 13336 continue; 13337 13338 case D_FREEWORK: 13339 case D_FREEDEP: 13340 case D_JSEGDEP: 13341 case D_JSEG: 13342 case D_SBDEP: 13343 /* never a dependency on these blocks */ 13344 continue; 13345 13346 default: 13347 panic("softdep_count_dependencies: Unexpected type %s", 13348 TYPENAME(wk->wk_type)); 13349 /* NOTREACHED */ 13350 } 13351 } 13352 out: 13353 FREE_LOCK(&lk); 13354 return retval; 13355 } 13356 13357 /* 13358 * Acquire exclusive access to a buffer. 13359 * Must be called with a locked mtx parameter. 13360 * Return acquired buffer or NULL on failure. 13361 */ 13362 static struct buf * 13363 getdirtybuf(bp, mtx, waitfor) 13364 struct buf *bp; 13365 struct mtx *mtx; 13366 int waitfor; 13367 { 13368 int error; 13369 13370 mtx_assert(mtx, MA_OWNED); 13371 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) { 13372 if (waitfor != MNT_WAIT) 13373 return (NULL); 13374 error = BUF_LOCK(bp, 13375 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, mtx); 13376 /* 13377 * Even if we sucessfully acquire bp here, we have dropped 13378 * mtx, which may violates our guarantee. 13379 */ 13380 if (error == 0) 13381 BUF_UNLOCK(bp); 13382 else if (error != ENOLCK) 13383 panic("getdirtybuf: inconsistent lock: %d", error); 13384 mtx_lock(mtx); 13385 return (NULL); 13386 } 13387 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13388 if (mtx == &lk && waitfor == MNT_WAIT) { 13389 mtx_unlock(mtx); 13390 BO_LOCK(bp->b_bufobj); 13391 BUF_UNLOCK(bp); 13392 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) { 13393 bp->b_vflags |= BV_BKGRDWAIT; 13394 msleep(&bp->b_xflags, BO_MTX(bp->b_bufobj), 13395 PRIBIO | PDROP, "getbuf", 0); 13396 } else 13397 BO_UNLOCK(bp->b_bufobj); 13398 mtx_lock(mtx); 13399 return (NULL); 13400 } 13401 BUF_UNLOCK(bp); 13402 if (waitfor != MNT_WAIT) 13403 return (NULL); 13404 /* 13405 * The mtx argument must be bp->b_vp's mutex in 13406 * this case. 13407 */ 13408 #ifdef DEBUG_VFS_LOCKS 13409 if (bp->b_vp->v_type != VCHR) 13410 ASSERT_BO_LOCKED(bp->b_bufobj); 13411 #endif 13412 bp->b_vflags |= BV_BKGRDWAIT; 13413 msleep(&bp->b_xflags, mtx, PRIBIO, "getbuf", 0); 13414 return (NULL); 13415 } 13416 if ((bp->b_flags & B_DELWRI) == 0) { 13417 BUF_UNLOCK(bp); 13418 return (NULL); 13419 } 13420 bremfree(bp); 13421 return (bp); 13422 } 13423 13424 13425 /* 13426 * Check if it is safe to suspend the file system now. On entry, 13427 * the vnode interlock for devvp should be held. Return 0 with 13428 * the mount interlock held if the file system can be suspended now, 13429 * otherwise return EAGAIN with the mount interlock held. 13430 */ 13431 int 13432 softdep_check_suspend(struct mount *mp, 13433 struct vnode *devvp, 13434 int softdep_deps, 13435 int softdep_accdeps, 13436 int secondary_writes, 13437 int secondary_accwrites) 13438 { 13439 struct bufobj *bo; 13440 struct ufsmount *ump; 13441 int error; 13442 13443 ump = VFSTOUFS(mp); 13444 bo = &devvp->v_bufobj; 13445 ASSERT_BO_LOCKED(bo); 13446 13447 for (;;) { 13448 if (!TRY_ACQUIRE_LOCK(&lk)) { 13449 BO_UNLOCK(bo); 13450 ACQUIRE_LOCK(&lk); 13451 FREE_LOCK(&lk); 13452 BO_LOCK(bo); 13453 continue; 13454 } 13455 MNT_ILOCK(mp); 13456 if (mp->mnt_secondary_writes != 0) { 13457 FREE_LOCK(&lk); 13458 BO_UNLOCK(bo); 13459 msleep(&mp->mnt_secondary_writes, 13460 MNT_MTX(mp), 13461 (PUSER - 1) | PDROP, "secwr", 0); 13462 BO_LOCK(bo); 13463 continue; 13464 } 13465 break; 13466 } 13467 13468 /* 13469 * Reasons for needing more work before suspend: 13470 * - Dirty buffers on devvp. 13471 * - Softdep activity occurred after start of vnode sync loop 13472 * - Secondary writes occurred after start of vnode sync loop 13473 */ 13474 error = 0; 13475 if (bo->bo_numoutput > 0 || 13476 bo->bo_dirty.bv_cnt > 0 || 13477 softdep_deps != 0 || 13478 ump->softdep_deps != 0 || 13479 softdep_accdeps != ump->softdep_accdeps || 13480 secondary_writes != 0 || 13481 mp->mnt_secondary_writes != 0 || 13482 secondary_accwrites != mp->mnt_secondary_accwrites) 13483 error = EAGAIN; 13484 FREE_LOCK(&lk); 13485 BO_UNLOCK(bo); 13486 return (error); 13487 } 13488 13489 13490 /* 13491 * Get the number of dependency structures for the file system, both 13492 * the current number and the total number allocated. These will 13493 * later be used to detect that softdep processing has occurred. 13494 */ 13495 void 13496 softdep_get_depcounts(struct mount *mp, 13497 int *softdep_depsp, 13498 int *softdep_accdepsp) 13499 { 13500 struct ufsmount *ump; 13501 13502 ump = VFSTOUFS(mp); 13503 ACQUIRE_LOCK(&lk); 13504 *softdep_depsp = ump->softdep_deps; 13505 *softdep_accdepsp = ump->softdep_accdeps; 13506 FREE_LOCK(&lk); 13507 } 13508 13509 /* 13510 * Wait for pending output on a vnode to complete. 13511 * Must be called with vnode lock and interlock locked. 13512 * 13513 * XXX: Should just be a call to bufobj_wwait(). 13514 */ 13515 static void 13516 drain_output(vp) 13517 struct vnode *vp; 13518 { 13519 struct bufobj *bo; 13520 13521 bo = &vp->v_bufobj; 13522 ASSERT_VOP_LOCKED(vp, "drain_output"); 13523 ASSERT_BO_LOCKED(bo); 13524 13525 while (bo->bo_numoutput) { 13526 bo->bo_flag |= BO_WWAIT; 13527 msleep((caddr_t)&bo->bo_numoutput, 13528 BO_MTX(bo), PRIBIO + 1, "drainvp", 0); 13529 } 13530 } 13531 13532 /* 13533 * Called whenever a buffer that is being invalidated or reallocated 13534 * contains dependencies. This should only happen if an I/O error has 13535 * occurred. The routine is called with the buffer locked. 13536 */ 13537 static void 13538 softdep_deallocate_dependencies(bp) 13539 struct buf *bp; 13540 { 13541 13542 if ((bp->b_ioflags & BIO_ERROR) == 0) 13543 panic("softdep_deallocate_dependencies: dangling deps"); 13544 if (bp->b_vp != NULL && bp->b_vp->v_mount != NULL) 13545 softdep_error(bp->b_vp->v_mount->mnt_stat.f_mntonname, bp->b_error); 13546 else 13547 printf("softdep_deallocate_dependencies: " 13548 "got error %d while accessing filesystem\n", bp->b_error); 13549 if (bp->b_error != ENXIO) 13550 panic("softdep_deallocate_dependencies: unrecovered I/O error"); 13551 } 13552 13553 /* 13554 * Function to handle asynchronous write errors in the filesystem. 13555 */ 13556 static void 13557 softdep_error(func, error) 13558 char *func; 13559 int error; 13560 { 13561 13562 /* XXX should do something better! */ 13563 printf("%s: got error %d while accessing filesystem\n", func, error); 13564 } 13565 13566 #ifdef DDB 13567 13568 static void 13569 inodedep_print(struct inodedep *inodedep, int verbose) 13570 { 13571 db_printf("%p fs %p st %x ino %jd inoblk %jd delta %d nlink %d" 13572 " saveino %p\n", 13573 inodedep, inodedep->id_fs, inodedep->id_state, 13574 (intmax_t)inodedep->id_ino, 13575 (intmax_t)fsbtodb(inodedep->id_fs, 13576 ino_to_fsba(inodedep->id_fs, inodedep->id_ino)), 13577 inodedep->id_nlinkdelta, inodedep->id_savednlink, 13578 inodedep->id_savedino1); 13579 13580 if (verbose == 0) 13581 return; 13582 13583 db_printf("\tpendinghd %p, bufwait %p, inowait %p, inoreflst %p, " 13584 "mkdiradd %p\n", 13585 LIST_FIRST(&inodedep->id_pendinghd), 13586 LIST_FIRST(&inodedep->id_bufwait), 13587 LIST_FIRST(&inodedep->id_inowait), 13588 TAILQ_FIRST(&inodedep->id_inoreflst), 13589 inodedep->id_mkdiradd); 13590 db_printf("\tinoupdt %p, newinoupdt %p, extupdt %p, newextupdt %p\n", 13591 TAILQ_FIRST(&inodedep->id_inoupdt), 13592 TAILQ_FIRST(&inodedep->id_newinoupdt), 13593 TAILQ_FIRST(&inodedep->id_extupdt), 13594 TAILQ_FIRST(&inodedep->id_newextupdt)); 13595 } 13596 13597 DB_SHOW_COMMAND(inodedep, db_show_inodedep) 13598 { 13599 13600 if (have_addr == 0) { 13601 db_printf("Address required\n"); 13602 return; 13603 } 13604 inodedep_print((struct inodedep*)addr, 1); 13605 } 13606 13607 DB_SHOW_COMMAND(inodedeps, db_show_inodedeps) 13608 { 13609 struct inodedep_hashhead *inodedephd; 13610 struct inodedep *inodedep; 13611 struct fs *fs; 13612 int cnt; 13613 13614 fs = have_addr ? (struct fs *)addr : NULL; 13615 for (cnt = 0; cnt < inodedep_hash; cnt++) { 13616 inodedephd = &inodedep_hashtbl[cnt]; 13617 LIST_FOREACH(inodedep, inodedephd, id_hash) { 13618 if (fs != NULL && fs != inodedep->id_fs) 13619 continue; 13620 inodedep_print(inodedep, 0); 13621 } 13622 } 13623 } 13624 13625 DB_SHOW_COMMAND(worklist, db_show_worklist) 13626 { 13627 struct worklist *wk; 13628 13629 if (have_addr == 0) { 13630 db_printf("Address required\n"); 13631 return; 13632 } 13633 wk = (struct worklist *)addr; 13634 printf("worklist: %p type %s state 0x%X\n", 13635 wk, TYPENAME(wk->wk_type), wk->wk_state); 13636 } 13637 13638 DB_SHOW_COMMAND(workhead, db_show_workhead) 13639 { 13640 struct workhead *wkhd; 13641 struct worklist *wk; 13642 int i; 13643 13644 if (have_addr == 0) { 13645 db_printf("Address required\n"); 13646 return; 13647 } 13648 wkhd = (struct workhead *)addr; 13649 wk = LIST_FIRST(wkhd); 13650 for (i = 0; i < 100 && wk != NULL; i++, wk = LIST_NEXT(wk, wk_list)) 13651 db_printf("worklist: %p type %s state 0x%X", 13652 wk, TYPENAME(wk->wk_type), wk->wk_state); 13653 if (i == 100) 13654 db_printf("workhead overflow"); 13655 printf("\n"); 13656 } 13657 13658 13659 DB_SHOW_COMMAND(mkdirs, db_show_mkdirs) 13660 { 13661 struct jaddref *jaddref; 13662 struct diradd *diradd; 13663 struct mkdir *mkdir; 13664 13665 LIST_FOREACH(mkdir, &mkdirlisthd, md_mkdirs) { 13666 diradd = mkdir->md_diradd; 13667 db_printf("mkdir: %p state 0x%X dap %p state 0x%X", 13668 mkdir, mkdir->md_state, diradd, diradd->da_state); 13669 if ((jaddref = mkdir->md_jaddref) != NULL) 13670 db_printf(" jaddref %p jaddref state 0x%X", 13671 jaddref, jaddref->ja_state); 13672 db_printf("\n"); 13673 } 13674 } 13675 13676 #endif /* DDB */ 13677 13678 #endif /* SOFTUPDATES */ 13679